forked from Constructor-io/okta-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefer_simple.c
105 lines (87 loc) · 2.7 KB
/
defer_simple.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <[email protected]> (defer/simple.c)
* Copyright (C) 2014 Mozilla Corporation <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <openvpn-plugin.h>
#include <sys/types.h>
#include <sys/wait.h>
struct context {
char *script_path;
};
void handle_sigchld(int sig)
{
while(waitpid((pid_t)(-1), 0, WNOHANG) > 0) {}
}
static int
generic_deferred_handler(char *script_path, const char * envp[])
{
int pid;
struct sigaction sa;
char *argv[] = {script_path, 0};
sa.sa_handler = &handle_sigchld;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, 0) == -1) {
return OPENVPN_PLUGIN_FUNC_ERROR;
}
pid = fork();
if (pid < 0) {
return OPENVPN_PLUGIN_FUNC_ERROR;
}
if (pid > 0) {
return OPENVPN_PLUGIN_FUNC_DEFERRED;
}
execve(argv[0], &argv[0], (char *const*)envp);
exit(127);
}
OPENVPN_EXPORT int
openvpn_plugin_func_v2(openvpn_plugin_handle_t handle, const int type, const char *argv[],
const char *envp[], void *per_client_context,
struct openvpn_plugin_string_list **return_list)
{
struct context *ctx = (struct context *) handle;
if (type == OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY) {
return generic_deferred_handler(ctx->script_path, envp);
} else {
return OPENVPN_PLUGIN_FUNC_ERROR;
}
}
OPENVPN_EXPORT openvpn_plugin_handle_t
openvpn_plugin_open_v2(unsigned int *type_mask, const char *argv[], const char *envp[],
struct openvpn_plugin_string_list **return_list)
{
struct context *ctx;
ctx = (struct context *) calloc(1, sizeof(struct context));
if (argv[1]) {
ctx->script_path = strdup(argv[1]);
}
*type_mask = OPENVPN_PLUGIN_MASK(OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY);
return (openvpn_plugin_handle_t) ctx;
}
OPENVPN_EXPORT void
openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
{
struct context *ctx = (struct context *) handle;
free(ctx->script_path);
free(ctx);
}