/
runvpn.c
166 lines (133 loc) · 3.2 KB
/
runvpn.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
//#include <dirent.h>
#include <string.h>
//#include <signal.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <errno.h>
//#include <unistd.h>
//#include <glob.h>
//#include <fcntl.h>
#include "runvpn.h"
static void
oom()
{
static const char e[] = "Out of memory\n";
fprintf(stderr, e);
#ifdef SIGQUIT
raise(SIGQUIT);
#endif
_Exit(EXIT_FAILURE);
}
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (p == NULL)
oom();
return p;
}
char *
xstrdup(const char *str)
{
char *dup = strdup(str);
if (dup == NULL)
oom();
return dup;
}
int
main(int argc, char *argv[])
{
const char *root_folder = getenv("runvpn_root");
struct vpn *vpn = NULL;
struct vpn *prev = NULL;
if (root_folder == NULL) {
printf("Environment variable \"runvpn_root\" is not defined.\n");
return EXIT_FAILURE;
}
if (argc == 1) {
/* fprintf(stdout, "%30s%s%s\n", BLUE_GRAY, "Listing VPNS", RESET); */
puts(" " BLUE_GRAY "Listing VPNS" RESET);
vpn = get_vpns(root_folder);
while(vpn != NULL)
{
//for (vpn = get_vpns(root_folder); vpn; vpn = vpn->next) {
fprintf(stdout, "%25s - ", vpn->name);
switch (vpn_status(vpn)) {
case VPN_ERROR:
return EXIT_FAILURE;
case VPN_DEAD:
print_color("Down", YELLOW);
break;
case VPN_RUNNING:
print_color("Up", GREEN);
break;
case VPN_PERM_DENIED:
print_color("Permission denied", RED);
break;
case VPN_STALE_PID:
print_color("Down", YELLOW);
vpn_delete_logfile(vpn);
vpn_delete_pidfile(vpn);
break;
default:
print_color("Unknown", RED);
break;
}
prev = vpn;
vpn = prev->next;
vpn_free(prev);
free(prev);
}
} else if (argc == 2) {
struct vpn vpn;
if (vpn_init(&vpn, root_folder, argv[argc - 1])) {
fprintf(stderr, "Problem finding vpn: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (vpn_status(&vpn) == VPN_ERROR ||
vpn_start(&vpn, NO_DAEMON))
return EXIT_FAILURE;
vpn_start(&vpn, NO_DAEMON);
vpn_free(&vpn);
} else if (argc == 3) {
struct vpn vpn;
char *name = *++argv;
char *argument = *++argv;
if (vpn_init(&vpn, root_folder, name) == -1) {
fprintf(stderr, "Problem finding vpn: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (vpn_status(&vpn) == VPN_ERROR)
return EXIT_FAILURE;
/* Do different things depending on 2nd argument pased. */
if (strcmp(argument, "stop") == 0) {
if (vpn_stop(&vpn))
return EXIT_FAILURE;
} else if (strcmp(argument, "reload") == 0) {
if (vpn_reload(&vpn))
return EXIT_FAILURE;
} else if (strcmp(argument, "daemon") == 0) {
if (vpn_start(&vpn, DAEMON))
return EXIT_FAILURE;
} else if (strcmp(argument, "drestart") == 0) {
if (vpn_stop(&vpn) || vpn_start(&vpn, DAEMON))
return EXIT_FAILURE;
} else if (strcmp(argument, "restart") == 0) {
if (vpn_stop(&vpn) || vpn_start(&vpn, NO_DAEMON))
return EXIT_FAILURE;
} else if (strcmp(argument, "log") == 0) {
vpn_dumplog(&vpn);
} else {
fprintf(stderr, "Unknown action '%s'\n", argument);
}
vpn_free(&vpn);
}
return EXIT_SUCCESS;
}
void
print_color(const char *text, char *color)
{
fprintf(stdout, "%s%s%s\n", color, text, RESET);
}