Added check for failed memory allocation. Set binary to output to the
[ovpnpermit.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5
6 // This program will execute the openvpn executable
7 // passing in through the command line parameters
8 // The purpose is to allow openvpn to be executed
9 // as a different user (e.g. root)
10
11 int main (int argc, char *argv[]) {
12 char * const command = "openvpn";
13 char ** newargv = malloc (sizeof(char*) * (argc + 1));
14 int arg;
15
16 if (newargv != NULL) {
17 // Transfer the arguments passed in to a new argument array
18 for (arg = 0; arg < argc; arg++) {
19 newargv[arg] = argv[arg];
20 }
21 // Ensure the argument list is NULL terminated (for execvp)
22 newargv[argc] = NULL;
23 // Ensure the first argument matches the command
24 newargv[0] = command;
25
26 // Execute the external command
27 //setuid (0);
28 execvp (command, newargv);
29
30 // This part of the code shouldn't be reached unless there was a problem
31 fprintf (stderr, "Error executing openvpn\n");
32
33 free (newargv);
34 }
35 else {
36 // There was a problem with thee malloc call
37 fprintf (stderr, "Error allocating memory for command argument list\n");
38 }
39 return 0;
40 }
41