3 Copyright (C) 2008-2010 Keith Moyer, tomatovpn@keithmoyer.com
5 No part of this file may be used without permission.
11 #include <sys/types.h>
17 // Line number as text string
18 #define __LINE_T__ __LINE_T_(__LINE__)
19 #define __LINE_T_(x) __LINE_T(x)
20 #define __LINE_T(x) # x
22 #define VPN_LOG_ERROR -1
23 #define VPN_LOG_NOTE 0
24 #define VPN_LOG_INFO 1
25 #define VPN_LOG_EXTRA 2
26 #define vpnlog(level,x...) if(nvram_get_int("vpn_debug")>=level) syslog(LOG_INFO, #level ": " __LINE_T__ ": " x)
28 #define CLIENT_IF_START 10
29 #define SERVER_IF_START 20
34 static int waitfor(const char *name
)
39 while ( (pid
= pidof(name
)) >= 0 && (n
-- > 0) )
41 // Reap the zombie if it has terminated
42 waitpid(pid
, NULL
, WNOHANG
);
48 void start_vpnclient(int clientNum
)
52 char buffer
[BUF_SIZE
];
55 enum { TLS
, SECRET
, CUSTOM
} cryptMode
= CUSTOM
;
56 enum { TAP
, TUN
} ifType
= TUN
;
57 enum { BRIDGE
, NAT
, NONE
} routeMode
= NONE
;
58 int nvi
, ip
[4], nm
[4];
61 int userauth
, useronly
;
63 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
65 start_service(&buffer
[0]);
69 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend starting...");
71 if ( (pid
= pidof(&buffer
[0])) >= 0 )
73 vpnlog(VPN_LOG_INFO
, "VPN Client %d already running...", clientNum
);
74 vpnlog(VPN_LOG_INFO
,"PID: %d", pid
);
78 // Determine interface
79 sprintf(&buffer
[0], "vpn_client%d_if", clientNum
);
80 if ( nvram_contains_word(&buffer
[0], "tap") )
82 else if ( nvram_contains_word(&buffer
[0], "tun") )
86 vpnlog(VPN_LOG_ERROR
, "Invalid interface type, %.3s", nvram_safe_get(&buffer
[0]));
90 // Build interface name
91 snprintf(&iface
[0], IF_SIZE
, "%s%d", nvram_safe_get(&buffer
[0]), clientNum
+CLIENT_IF_START
);
93 // Determine encryption mode
94 sprintf(&buffer
[0], "vpn_client%d_crypt", clientNum
);
95 if ( nvram_contains_word(&buffer
[0], "tls") )
97 else if ( nvram_contains_word(&buffer
[0], "secret") )
99 else if ( nvram_contains_word(&buffer
[0], "custom") )
103 vpnlog(VPN_LOG_ERROR
,"Invalid encryption mode, %.6s", nvram_safe_get(&buffer
[0]));
107 // Determine if we should bridge the tunnel
108 sprintf(&buffer
[0], "vpn_client%d_bridge", clientNum
);
109 if ( ifType
== TAP
&& nvram_get_int(&buffer
[0]) == 1 )
112 // Determine if we should NAT the tunnel
113 sprintf(&buffer
[0], "vpn_client%d_nat", clientNum
);
114 if ( (ifType
== TUN
|| routeMode
!= BRIDGE
) && nvram_get_int(&buffer
[0]) == 1 )
117 // Make sure openvpn directory exists
118 mkdir("/etc/openvpn", 0700);
119 sprintf(&buffer
[0], "/etc/openvpn/client%d", clientNum
);
120 mkdir(&buffer
[0], 0700);
122 // Make sure symbolic link exists
123 sprintf(&buffer
[0], "/etc/openvpn/vpnclient%d", clientNum
);
125 if ( symlink("/usr/sbin/openvpn", &buffer
[0]) )
127 vpnlog(VPN_LOG_ERROR
,"Creating symlink failed...");
128 stop_vpnclient(clientNum
);
132 // Make sure module is loaded
134 f_wait_exists("/dev/net/tun", 5);
136 // Create tap/tun interface
137 sprintf(&buffer
[0], "openvpn --mktun --dev %s", &iface
[0]);
138 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
139 if ( _eval(argv
, NULL
, 0, NULL
) )
141 vpnlog(VPN_LOG_ERROR
,"Creating tunnel interface failed...");
142 stop_vpnclient(clientNum
);
146 // Bring interface up (TAP only)
149 if ( routeMode
== BRIDGE
)
151 snprintf(&buffer
[0], BUF_SIZE
, "brctl addif %s %s", nvram_safe_get("lan_ifname"), &iface
[0]);
152 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
153 if ( _eval(argv
, NULL
, 0, NULL
) )
155 vpnlog(VPN_LOG_ERROR
,"Adding tunnel interface to bridge failed...");
156 stop_vpnclient(clientNum
);
161 snprintf(&buffer
[0], BUF_SIZE
, "ifconfig %s promisc up", &iface
[0]);
162 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
163 if ( _eval(argv
, NULL
, 0, NULL
) )
165 vpnlog(VPN_LOG_ERROR
,"Bringing interface up failed...");
166 stop_vpnclient(clientNum
);
171 sprintf(&buffer
[0], "vpn_client%d_userauth", clientNum
);
172 userauth
= nvram_get_int(&buffer
[0]);
173 sprintf(&buffer
[0], "vpn_client%d_useronly", clientNum
);
174 useronly
= userauth
&& nvram_get_int(&buffer
[0]);
176 // Build and write config file
177 vpnlog(VPN_LOG_EXTRA
,"Writing config file");
178 sprintf(&buffer
[0], "/etc/openvpn/client%d/config.ovpn", clientNum
);
179 fp
= fopen(&buffer
[0], "w");
180 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
181 fprintf(fp
, "# Automatically generated configuration\n");
182 fprintf(fp
, "daemon\n");
183 if ( cryptMode
== TLS
)
184 fprintf(fp
, "client\n");
185 fprintf(fp
, "dev %s\n", &iface
[0]);
186 sprintf(&buffer
[0], "vpn_client%d_proto", clientNum
);
187 fprintf(fp
, "proto %s\n", nvram_safe_get(&buffer
[0]));
188 sprintf(&buffer
[0], "vpn_client%d_addr", clientNum
);
189 fprintf(fp
, "remote %s ", nvram_safe_get(&buffer
[0]));
190 sprintf(&buffer
[0], "vpn_client%d_port", clientNum
);
191 fprintf(fp
, "%d\n", nvram_get_int(&buffer
[0]));
192 if ( cryptMode
== SECRET
)
196 sprintf(&buffer
[0], "vpn_client%d_local", clientNum
);
197 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
198 sprintf(&buffer
[0], "vpn_client%d_remote", clientNum
);
199 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
201 else if ( ifType
== TAP
)
203 sprintf(&buffer
[0], "vpn_client%d_local", clientNum
);
204 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
205 sprintf(&buffer
[0], "vpn_client%d_nm", clientNum
);
206 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
209 sprintf(&buffer
[0], "vpn_client%d_retry", clientNum
);
210 if ( (nvi
= nvram_get_int(&buffer
[0])) >= 0 )
211 fprintf(fp
, "resolv-retry %d\n", nvi
);
213 fprintf(fp
, "resolv-retry infinite\n");
214 sprintf(&buffer
[0], "vpn_client%d_reneg", clientNum
);
215 if ( (nvl
= atol(nvram_safe_get(&buffer
[0]))) >= 0 )
216 fprintf(fp
, "reneg-sec %ld\n", nvl
);
217 fprintf(fp
, "nobind\n");
218 fprintf(fp
, "persist-key\n");
219 fprintf(fp
, "persist-tun\n");
220 sprintf(&buffer
[0], "vpn_client%d_comp", clientNum
);
221 if ( nvram_get_int(&buffer
[0]) >= 0 )
222 fprintf(fp
, "comp-lzo %s\n", nvram_safe_get(&buffer
[0]));
223 sprintf(&buffer
[0], "vpn_client%d_cipher", clientNum
);
224 if ( !nvram_contains_word(&buffer
[0], "default") )
225 fprintf(fp
, "cipher %s\n", nvram_safe_get(&buffer
[0]));
226 sprintf(&buffer
[0], "vpn_client%d_rgw", clientNum
);
227 if ( nvram_get_int(&buffer
[0]) )
229 sprintf(&buffer
[0], "vpn_client%d_gw", clientNum
);
230 if ( ifType
== TAP
&& nvram_safe_get(&buffer
[0])[0] != '\0' )
231 fprintf(fp
, "route-gateway %s\n", nvram_safe_get(&buffer
[0]));
232 fprintf(fp
, "redirect-gateway def1\n");
234 fprintf(fp
, "verb 3\n");
235 if ( cryptMode
== TLS
)
237 sprintf(&buffer
[0], "vpn_client%d_adns", clientNum
);
238 if ( nvram_get_int(&buffer
[0]) > 0 )
240 sprintf(&buffer
[0], "/etc/openvpn/client%d/updown.sh", clientNum
);
241 symlink("/rom/openvpn/updown.sh", &buffer
[0]);
242 fprintf(fp
, "script-security 2\n");
243 fprintf(fp
, "up updown.sh\n");
244 fprintf(fp
, "down updown.sh\n");
247 sprintf(&buffer
[0], "vpn_client%d_hmac", clientNum
);
248 nvi
= nvram_get_int(&buffer
[0]);
249 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
250 if ( !nvram_is_empty(&buffer
[0]) && nvi
>= 0 )
252 fprintf(fp
, "tls-auth static.key");
254 fprintf(fp
, " %d", nvi
);
258 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
259 if ( !nvram_is_empty(&buffer
[0]) )
260 fprintf(fp
, "ca ca.crt\n");
263 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
264 if ( !nvram_is_empty(&buffer
[0]) )
265 fprintf(fp
, "cert client.crt\n");
266 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
267 if ( !nvram_is_empty(&buffer
[0]) )
268 fprintf(fp
, "key client.key\n");
270 sprintf(&buffer
[0], "vpn_client%d_tlsremote", clientNum
);
271 if (nvram_get_int(&buffer
[0]))
273 sprintf(&buffer
[0], "vpn_client%d_cn", clientNum
);
274 fprintf(fp
, "tls-remote %s\n", nvram_safe_get(&buffer
[0]));
277 fprintf(fp
, "auth-user-pass up\n");
279 else if ( cryptMode
== SECRET
)
281 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
282 if ( !nvram_is_empty(&buffer
[0]) )
283 fprintf(fp
, "secret static.key\n");
285 fprintf(fp
, "status-version 2\n");
286 fprintf(fp
, "status status\n");
287 fprintf(fp
, "\n# Custom Configuration\n");
288 sprintf(&buffer
[0], "vpn_client%d_custom", clientNum
);
289 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
291 vpnlog(VPN_LOG_EXTRA
,"Done writing config file");
293 // Write certification and key files
294 vpnlog(VPN_LOG_EXTRA
,"Writing certs/keys");
295 if ( cryptMode
== TLS
)
297 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
298 if ( !nvram_is_empty(&buffer
[0]) )
300 sprintf(&buffer
[0], "/etc/openvpn/client%d/ca.crt", clientNum
);
301 fp
= fopen(&buffer
[0], "w");
302 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
303 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
304 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
310 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
311 if ( !nvram_is_empty(&buffer
[0]) )
313 sprintf(&buffer
[0], "/etc/openvpn/client%d/client.key", clientNum
);
314 fp
= fopen(&buffer
[0], "w");
315 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
316 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
317 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
321 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
322 if ( !nvram_is_empty(&buffer
[0]) )
324 sprintf(&buffer
[0], "/etc/openvpn/client%d/client.crt", clientNum
);
325 fp
= fopen(&buffer
[0], "w");
326 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
327 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
328 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
334 sprintf(&buffer
[0], "/etc/openvpn/client%d/up", clientNum
);
335 fp
= fopen(&buffer
[0], "w");
336 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
337 sprintf(&buffer
[0], "vpn_client%d_username", clientNum
);
338 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
339 sprintf(&buffer
[0], "vpn_client%d_password", clientNum
);
340 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
344 sprintf(&buffer
[0], "vpn_client%d_hmac", clientNum
);
345 if ( cryptMode
== SECRET
|| (cryptMode
== TLS
&& nvram_get_int(&buffer
[0]) >= 0) )
347 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
348 if ( !nvram_is_empty(&buffer
[0]) )
350 sprintf(&buffer
[0], "/etc/openvpn/client%d/static.key", clientNum
);
351 fp
= fopen(&buffer
[0], "w");
352 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
353 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
354 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
358 vpnlog(VPN_LOG_EXTRA
,"Done writing certs/keys");
360 // Start the VPN client
361 sprintf(&buffer
[0], "/etc/openvpn/vpnclient%d --cd /etc/openvpn/client%d --config config.ovpn", clientNum
, clientNum
);
362 vpnlog(VPN_LOG_INFO
,"Starting OpenVPN: %s",&buffer
[0]);
363 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
364 if ( _eval(argv
, NULL
, 0, &pid
) )
366 vpnlog(VPN_LOG_ERROR
,"Starting OpenVPN failed...");
367 stop_vpnclient(clientNum
);
370 vpnlog(VPN_LOG_EXTRA
,"Done starting openvpn");
372 // Handle firewall rules if appropriate
373 sprintf(&buffer
[0], "vpn_client%d_firewall", clientNum
);
374 if ( !nvram_contains_word(&buffer
[0], "custom") )
376 // Create firewall rules
377 vpnlog(VPN_LOG_EXTRA
,"Creating firewall rules");
378 mkdir("/etc/openvpn/fw", 0700);
379 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
380 fp
= fopen(&buffer
[0], "w");
381 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
|S_IXUSR
);
382 fprintf(fp
, "#!/bin/sh\n");
383 fprintf(fp
, "iptables -I INPUT -i %s -j ACCEPT\n", &iface
[0]);
384 fprintf(fp
, "iptables -I FORWARD -i %s -j ACCEPT\n", &iface
[0]);
385 if ( routeMode
== NAT
)
387 sscanf(nvram_safe_get("lan_ipaddr"), "%d.%d.%d.%d", &ip
[0], &ip
[1], &ip
[2], &ip
[3]);
388 sscanf(nvram_safe_get("lan_netmask"), "%d.%d.%d.%d", &nm
[0], &nm
[1], &nm
[2], &nm
[3]);
389 fprintf(fp
, "iptables -t nat -I POSTROUTING -s %d.%d.%d.%d/%s -o %s -j MASQUERADE\n",
390 ip
[0]&nm
[0], ip
[1]&nm
[1], ip
[2]&nm
[2], ip
[3]&nm
[3], nvram_safe_get("lan_netmask"), &iface
[0]);
393 vpnlog(VPN_LOG_EXTRA
,"Done creating firewall rules");
395 // Run the firewall rules
396 vpnlog(VPN_LOG_EXTRA
,"Running firewall rules");
397 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
398 argv
[0] = &buffer
[0];
400 _eval(argv
, NULL
, 0, NULL
);
401 vpnlog(VPN_LOG_EXTRA
,"Done running firewall rules");
405 sprintf(&buffer
[0], "vpn_client%d_poll", clientNum
);
406 if ( (nvi
= nvram_get_int(&buffer
[0])) > 0 )
408 vpnlog(VPN_LOG_EXTRA
,"Adding cron job");
411 sprintf(&buffer
[0], "CheckVPNClient%d", clientNum
);
412 argv
[2] = &buffer
[0];
413 sprintf(&buffer
[strlen(&buffer
[0])+1], "*/%d * * * * service vpnclient%d start", nvi
, clientNum
);
414 argv
[3] = &buffer
[strlen(&buffer
[0])+1];
416 _eval(argv
, NULL
, 0, NULL
);
417 vpnlog(VPN_LOG_EXTRA
,"Done adding cron job");
421 sprintf(&buffer
[0], "vpn_client%d", clientNum
);
422 allow_fastnat(buffer
, 0);
423 try_enabling_fastnat();
425 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend complete.");
428 void stop_vpnclient(int clientNum
)
432 char buffer
[BUF_SIZE
];
434 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
436 stop_service(&buffer
[0]);
440 vpnlog(VPN_LOG_INFO
,"Stopping VPN GUI client backend.");
443 vpnlog(VPN_LOG_EXTRA
,"Removing cron job");
446 sprintf(&buffer
[0], "CheckVPNClient%d", clientNum
);
447 argv
[2] = &buffer
[0];
449 _eval(argv
, NULL
, 0, NULL
);
450 vpnlog(VPN_LOG_EXTRA
,"Done removing cron job");
452 // Remove firewall rules
453 vpnlog(VPN_LOG_EXTRA
,"Removing firewall rules.");
454 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
457 argv
[2] = "s/-A/-D/g;s/-I/-D/g";
458 argv
[3] = &buffer
[0];
460 if (!_eval(argv
, NULL
, 0, NULL
))
462 argv
[0] = &buffer
[0];
464 _eval(argv
, NULL
, 0, NULL
);
466 vpnlog(VPN_LOG_EXTRA
,"Done removing firewall rules.");
468 // Stop the VPN client
469 vpnlog(VPN_LOG_EXTRA
,"Stopping OpenVPN client.");
470 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
471 if ( !waitfor(&buffer
[0]) )
472 vpnlog(VPN_LOG_EXTRA
,"OpenVPN client stopped.");
474 // NVRAM setting for device type could have changed, just try to remove both
475 vpnlog(VPN_LOG_EXTRA
,"Removing VPN device.");
476 sprintf(&buffer
[0], "openvpn --rmtun --dev tap%d", clientNum
+CLIENT_IF_START
);
477 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
478 _eval(argv
, NULL
, 0, NULL
);
480 sprintf(&buffer
[0], "openvpn --rmtun --dev tun%d", clientNum
+CLIENT_IF_START
);
481 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
482 _eval(argv
, NULL
, 0, NULL
);
483 vpnlog(VPN_LOG_EXTRA
,"VPN device removed.");
487 if ( nvram_get_int("vpn_debug") <= VPN_LOG_EXTRA
)
489 vpnlog(VPN_LOG_EXTRA
,"Removing generated files.");
490 // Delete all files for this client
491 sprintf(&buffer
[0], "rm -rf /etc/openvpn/client%d /etc/openvpn/fw/client%d-fw.sh /etc/openvpn/vpnclient%d",clientNum
,clientNum
,clientNum
);
492 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
493 _eval(argv
, NULL
, 0, NULL
);
495 // Attempt to remove directories. Will fail if not empty
496 rmdir("/etc/openvpn/fw");
497 rmdir("/etc/openvpn");
498 vpnlog(VPN_LOG_EXTRA
,"Done removing generated files.");
502 sprintf(&buffer
[0], "vpn_client%d", clientNum
);
503 allow_fastnat(buffer
, 1);
504 try_enabling_fastnat();
506 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend stopped.");
509 void start_vpnserver(int serverNum
)
513 char buffer
[BUF_SIZE
];
514 char *argv
[6], *chp
, *route
;
517 enum { TAP
, TUN
} ifType
= TUN
;
518 enum { TLS
, SECRET
, CUSTOM
} cryptMode
= CUSTOM
;
519 int nvi
, ip
[4], nm
[4];
523 int current_security_level
= 1;
524 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
526 start_service(&buffer
[0]);
530 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend starting...");
532 if ( (pid
= pidof(&buffer
[0])) >= 0 )
534 vpnlog(VPN_LOG_INFO
, "VPN Server %d already running...", serverNum
);
535 vpnlog(VPN_LOG_INFO
,"PID: %d", pid
);
539 // Determine interface type
540 sprintf(&buffer
[0], "vpn_server%d_if", serverNum
);
541 if ( nvram_contains_word(&buffer
[0], "tap") )
543 else if ( nvram_contains_word(&buffer
[0], "tun") )
547 vpnlog(VPN_LOG_ERROR
,"Invalid interface type, %.3s", nvram_safe_get(&buffer
[0]));
551 // Build interface name
552 snprintf(&iface
[0], IF_SIZE
, "%s%d", nvram_safe_get(&buffer
[0]), serverNum
+SERVER_IF_START
);
554 // Determine encryption mode
555 sprintf(&buffer
[0], "vpn_server%d_crypt", serverNum
);
556 if ( nvram_contains_word(&buffer
[0], "tls") )
558 else if ( nvram_contains_word(&buffer
[0], "secret") )
560 else if ( nvram_contains_word(&buffer
[0], "custom") )
564 vpnlog(VPN_LOG_ERROR
,"Invalid encryption mode, %.6s", nvram_safe_get(&buffer
[0]));
568 // Make sure openvpn directory exists
569 mkdir("/etc/openvpn", 0700);
570 sprintf(&buffer
[0], "/etc/openvpn/server%d", serverNum
);
571 mkdir(&buffer
[0], 0700);
573 // Make sure symbolic link exists
574 sprintf(&buffer
[0], "/etc/openvpn/vpnserver%d", serverNum
);
576 if ( symlink("/usr/sbin/openvpn", &buffer
[0]) )
578 vpnlog(VPN_LOG_ERROR
,"Creating symlink failed...");
579 stop_vpnserver(serverNum
);
583 // Make sure module is loaded
585 f_wait_exists("/dev/net/tun", 5);
587 // Create tap/tun interface
588 sprintf(&buffer
[0], "openvpn --mktun --dev %s", &iface
[0]);
589 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
590 if ( _eval(argv
, NULL
, 0, NULL
) )
592 vpnlog(VPN_LOG_ERROR
,"Creating tunnel interface failed...");
593 stop_vpnserver(serverNum
);
597 // Add interface to LAN bridge (TAP only)
600 snprintf(&buffer
[0], BUF_SIZE
, "brctl addif %s %s", nvram_safe_get("lan_ifname"), &iface
[0]);
601 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
602 if ( _eval(argv
, NULL
, 0, NULL
) )
604 vpnlog(VPN_LOG_ERROR
,"Adding tunnel interface to bridge failed...");
605 stop_vpnserver(serverNum
);
610 // Bring interface up
611 sprintf(&buffer
[0], "ifconfig %s 0.0.0.0 promisc up", &iface
[0]);
612 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
613 if ( _eval(argv
, NULL
, 0, NULL
) )
615 vpnlog(VPN_LOG_ERROR
,"Bringing up tunnel interface failed...");
616 stop_vpnserver(serverNum
);
620 // Build and write config files
621 vpnlog(VPN_LOG_EXTRA
,"Writing config file");
622 sprintf(&buffer
[0], "/etc/openvpn/server%d/config.ovpn", serverNum
);
623 fp
= fopen(&buffer
[0], "w");
624 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
625 fprintf(fp
, "# Automatically generated configuration\n");
626 fprintf(fp
, "daemon\n");
627 if ( cryptMode
== TLS
)
631 sprintf(&buffer
[0], "vpn_server%d_sn", serverNum
);
632 fprintf(fp
, "server %s ", nvram_safe_get(&buffer
[0]));
633 sprintf(&buffer
[0], "vpn_server%d_nm", serverNum
);
634 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
636 else if ( ifType
== TAP
)
638 fprintf(fp
, "server-bridge");
639 sprintf(&buffer
[0], "vpn_server%d_dhcp", serverNum
);
640 if ( nvram_get_int(&buffer
[0]) == 0 )
642 fprintf(fp
, " %s ", nvram_safe_get("lan_ipaddr"));
643 fprintf(fp
, "%s ", nvram_safe_get("lan_netmask"));
644 sprintf(&buffer
[0], "vpn_server%d_r1", serverNum
);
645 fprintf(fp
, "%s ", nvram_safe_get(&buffer
[0]));
646 sprintf(&buffer
[0], "vpn_server%d_r2", serverNum
);
647 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
652 else if ( cryptMode
== SECRET
)
656 sprintf(&buffer
[0], "vpn_server%d_local", serverNum
);
657 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
658 sprintf(&buffer
[0], "vpn_server%d_remote", serverNum
);
659 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
662 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
663 fprintf(fp
, "proto %s\n", nvram_safe_get(&buffer
[0]));
664 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
665 fprintf(fp
, "port %d\n", nvram_get_int(&buffer
[0]));
666 fprintf(fp
, "dev %s\n", &iface
[0]);
667 sprintf(&buffer
[0], "vpn_server%d_cipher", serverNum
);
668 if ( !nvram_contains_word(&buffer
[0], "default") )
669 fprintf(fp
, "cipher %s\n", nvram_safe_get(&buffer
[0]));
670 sprintf(&buffer
[0], "vpn_server%d_comp", serverNum
);
671 if ( nvram_get_int(&buffer
[0]) >= 0 )
672 fprintf(fp
, "comp-lzo %s\n", nvram_safe_get(&buffer
[0]));
673 sprintf(&buffer
[0], "vpn_server%d_reneg", serverNum
);
674 if ( (nvl
= atol(nvram_safe_get(&buffer
[0]))) >= 0 )
675 fprintf(fp
, "reneg-sec %ld\n", nvl
);
676 fprintf(fp
, "keepalive 15 60\n");
677 fprintf(fp
, "verb 3\n");
678 if ( cryptMode
== TLS
)
680 sprintf(&buffer
[0], "vpn_server%d_plan", serverNum
);
681 if ( ifType
== TUN
&& nvram_get_int(&buffer
[0]) )
683 sscanf(nvram_safe_get("lan_ipaddr"), "%d.%d.%d.%d", &ip
[0], &ip
[1], &ip
[2], &ip
[3]);
684 sscanf(nvram_safe_get("lan_netmask"), "%d.%d.%d.%d", &nm
[0], &nm
[1], &nm
[2], &nm
[3]);
685 fprintf(fp
, "push \"route %d.%d.%d.%d %s\"\n", ip
[0]&nm
[0], ip
[1]&nm
[1], ip
[2]&nm
[2], ip
[3]&nm
[3],
686 nvram_safe_get("lan_netmask"));
689 sprintf(&buffer
[0], "vpn_server%d_ccd", serverNum
);
690 if ( nvram_get_int(&buffer
[0]) )
692 fprintf(fp
, "client-config-dir ccd\n");
694 sprintf(&buffer
[0], "vpn_server%d_c2c", serverNum
);
695 if ( (c2c
= nvram_get_int(&buffer
[0])) )
696 fprintf(fp
, "client-to-client\n");
698 sprintf(&buffer
[0], "vpn_server%d_ccd_excl", serverNum
);
699 if ( nvram_get_int(&buffer
[0]) )
700 fprintf(fp
, "ccd-exclusive\n");
702 sprintf(&buffer
[0], "/etc/openvpn/server%d/ccd", serverNum
);
703 mkdir(&buffer
[0], 0700);
706 sprintf(&buffer
[0], "vpn_server%d_ccd_val", serverNum
);
707 strcpy(&buffer
[0], nvram_safe_get(&buffer
[0]));
708 chp
= strtok(&buffer
[0],">");
709 while ( chp
!= NULL
)
713 chp
[strcspn(chp
,"<")] = '\0';
714 vpnlog(VPN_LOG_EXTRA
,"CCD: enabled: %d", atoi(chp
));
715 if ( atoi(chp
) == 1 )
717 nvi
-= strlen(chp
)+1;
718 chp
+= strlen(chp
)+1;
724 chp
[strcspn(chp
,"<")] = '\0';
725 vpnlog(VPN_LOG_EXTRA
,"CCD: Common name: %s", chp
);
726 ccd
= fopen(chp
, "w");
727 chmod(chp
, S_IRUSR
|S_IWUSR
);
729 nvi
-= strlen(chp
)+1;
730 chp
+= strlen(chp
)+1;
732 if ( nvi
> 0 && ccd
!= NULL
&& strcspn(chp
,"<") != strlen(chp
) )
734 chp
[strcspn(chp
,"<")] = ' ';
735 chp
[strcspn(chp
,"<")] = '\0';
737 vpnlog(VPN_LOG_EXTRA
,"CCD: Route: %s", chp
);
738 if ( strlen(route
) > 1 )
740 fprintf(ccd
, "iroute %s\n", route
);
741 fprintf(fp
, "route %s\n", route
);
744 nvi
-= strlen(chp
)+1;
745 chp
+= strlen(chp
)+1;
749 if ( nvi
> 0 && route
!= NULL
)
751 chp
[strcspn(chp
,"<")] = '\0';
752 vpnlog(VPN_LOG_EXTRA
,"CCD: Push: %d", atoi(chp
));
753 if ( c2c
&& atoi(chp
) == 1 && strlen(route
) > 1 )
754 fprintf(fp
, "push \"route %s\"\n", route
);
756 nvi
-= strlen(chp
)+1;
757 chp
+= strlen(chp
)+1;
760 vpnlog(VPN_LOG_EXTRA
,"CCD leftover: %d", nvi
+1);
762 // Advance to next entry
763 chp
= strtok(NULL
, ">");
765 vpnlog(VPN_LOG_EXTRA
,"CCD processing complete");
768 sprintf(&buffer
[0], "vpn_server%d_userpass", serverNum
);
769 if ( nvram_get_int(&buffer
[0]) )
771 fprintf(fp
, "plugin /lib/openvpn_plugin_auth_nvram.so vpn_server%d_users_val\n",serverNum
);
772 if(current_security_level
< 2){
773 fprintf(fp
, "script-security 2\n");
774 current_security_level
= 2;
776 fprintf(fp
, "username-as-common-name\n");
777 sprintf(&buffer
[0], "vpn_server%d_nocert", serverNum
);
778 if ( nvram_get_int(&buffer
[0]) )
779 fprintf(fp
, "client-cert-not-required\n");
782 sprintf(&buffer
[0], "vpn_server%d_pdns", serverNum
);
783 if ( nvram_get_int(&buffer
[0]) )
785 if ( nvram_safe_get("wan_domain")[0] != '\0' )
786 fprintf(fp
, "push \"dhcp-option DOMAIN %s\"\n", nvram_safe_get("wan_domain"));
787 if ( (nvram_safe_get("wan_wins")[0] != '\0' && strcmp(nvram_safe_get("wan_wins"), "0.0.0.0") != 0) )
788 fprintf(fp
, "push \"dhcp-option WINS %s\"\n", nvram_safe_get("wan_wins"));
789 fprintf(fp
, "push \"dhcp-option DNS %s\"\n", nvram_safe_get("lan_ipaddr"));
792 sprintf(&buffer
[0], "vpn_server%d_rgw", serverNum
);
793 if ( nvram_get_int(&buffer
[0]) )
796 fprintf(fp
, "push \"route-gateway %s\"\n", nvram_safe_get("lan_ipaddr"));
797 fprintf(fp
, "push \"redirect-gateway def1\"\n");
800 sprintf(&buffer
[0], "vpn_server%d_hmac", serverNum
);
801 nvi
= nvram_get_int(&buffer
[0]);
802 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
803 if ( !nvram_is_empty(&buffer
[0]) && nvi
>= 0 )
805 fprintf(fp
, "tls-auth static.key");
807 fprintf(fp
, " %d", nvi
);
811 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
812 if ( !nvram_is_empty(&buffer
[0]) )
813 fprintf(fp
, "ca ca.crt\n");
814 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
815 if ( !nvram_is_empty(&buffer
[0]) )
816 fprintf(fp
, "dh dh.pem\n");
817 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
818 if ( !nvram_is_empty(&buffer
[0]) )
819 fprintf(fp
, "cert server.crt\n");
820 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
821 if ( !nvram_is_empty(&buffer
[0]) )
822 fprintf(fp
, "key server.key\n");
824 else if ( cryptMode
== SECRET
)
826 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
827 if ( !nvram_is_empty(&buffer
[0]) )
828 fprintf(fp
, "secret static.key\n");
830 fprintf(fp
, "status-version 2\n");
831 fprintf(fp
, "status status\n");
832 fprintf(fp
, "\n# Custom Configuration\n");
833 sprintf(&buffer
[0], "vpn_server%d_custom", serverNum
);
834 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
836 vpnlog(VPN_LOG_EXTRA
,"Done writing config file");
838 // Write certification and key files
839 vpnlog(VPN_LOG_EXTRA
,"Writing certs/keys");
840 if ( cryptMode
== TLS
)
842 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
843 if ( !nvram_is_empty(&buffer
[0]) )
845 sprintf(&buffer
[0], "/etc/openvpn/server%d/ca.crt", serverNum
);
846 fp
= fopen(&buffer
[0], "w");
847 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
848 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
849 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
853 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
854 if ( !nvram_is_empty(&buffer
[0]) )
856 sprintf(&buffer
[0], "/etc/openvpn/server%d/server.key", serverNum
);
857 fp
= fopen(&buffer
[0], "w");
858 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
859 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
860 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
864 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
865 if ( !nvram_is_empty(&buffer
[0]) )
867 sprintf(&buffer
[0], "/etc/openvpn/server%d/server.crt", serverNum
);
868 fp
= fopen(&buffer
[0], "w");
869 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
870 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
871 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
875 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
876 if ( !nvram_is_empty(&buffer
[0]) )
878 sprintf(&buffer
[0], "/etc/openvpn/server%d/dh.pem", serverNum
);
879 fp
= fopen(&buffer
[0], "w");
880 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
881 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
882 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
886 sprintf(&buffer
[0], "vpn_server%d_hmac", serverNum
);
887 if ( cryptMode
== SECRET
|| (cryptMode
== TLS
&& nvram_get_int(&buffer
[0]) >= 0) )
889 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
890 if ( !nvram_is_empty(&buffer
[0]) )
892 sprintf(&buffer
[0], "/etc/openvpn/server%d/static.key", serverNum
);
893 fp
= fopen(&buffer
[0], "w");
894 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
895 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
896 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
900 vpnlog(VPN_LOG_EXTRA
,"Done writing certs/keys");
902 sprintf(&buffer
[0], "/etc/openvpn/vpnserver%d --cd /etc/openvpn/server%d --config config.ovpn", serverNum
, serverNum
);
903 vpnlog(VPN_LOG_INFO
,"Starting OpenVPN: %s",&buffer
[0]);
904 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
905 if ( _eval(argv
, NULL
, 0, &pid
) )
907 vpnlog(VPN_LOG_ERROR
,"Starting VPN instance failed...");
908 stop_vpnserver(serverNum
);
911 vpnlog(VPN_LOG_EXTRA
,"Done starting openvpn");
913 // Handle firewall rules if appropriate
914 sprintf(&buffer
[0], "vpn_server%d_firewall", serverNum
);
915 if ( !nvram_contains_word(&buffer
[0], "custom") )
917 // Create firewall rules
918 vpnlog(VPN_LOG_EXTRA
,"Creating firewall rules");
919 mkdir("/etc/openvpn/fw", 0700);
920 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
921 fp
= fopen(&buffer
[0], "w");
922 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
|S_IXUSR
);
923 fprintf(fp
, "#!/bin/sh\n");
924 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
925 strncpy(&buffer
[0], nvram_safe_get(&buffer
[0]), BUF_SIZE
);
926 fprintf(fp
, "iptables -t nat -I PREROUTING -p %s ", strtok(&buffer
[0], "-"));
927 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
928 fprintf(fp
, "--dport %d -j ACCEPT\n", nvram_get_int(&buffer
[0]));
929 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
930 strncpy(&buffer
[0], nvram_safe_get(&buffer
[0]), BUF_SIZE
);
931 fprintf(fp
, "iptables -I INPUT -p %s ", strtok(&buffer
[0], "-"));
932 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
933 fprintf(fp
, "--dport %d -j ACCEPT\n", nvram_get_int(&buffer
[0]));
934 sprintf(&buffer
[0], "vpn_server%d_firewall", serverNum
);
935 if ( !nvram_contains_word(&buffer
[0], "external") )
937 fprintf(fp
, "iptables -I INPUT -i %s -j ACCEPT\n", &iface
[0]);
938 fprintf(fp
, "iptables -I FORWARD -i %s -j ACCEPT\n", &iface
[0]);
941 vpnlog(VPN_LOG_EXTRA
,"Done creating firewall rules");
943 // Run the firewall rules
944 vpnlog(VPN_LOG_EXTRA
,"Running firewall rules");
945 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
946 argv
[0] = &buffer
[0];
948 _eval(argv
, NULL
, 0, NULL
);
949 vpnlog(VPN_LOG_EXTRA
,"Done running firewall rules");
953 sprintf(&buffer
[0], "vpn_server%d_poll", serverNum
);
954 if ( (nvi
= nvram_get_int(&buffer
[0])) > 0 )
956 vpnlog(VPN_LOG_EXTRA
,"Adding cron job");
959 sprintf(&buffer
[0], "CheckVPNServer%d", serverNum
);
960 argv
[2] = &buffer
[0];
961 sprintf(&buffer
[strlen(&buffer
[0])+1], "*/%d * * * * service vpnserver%d start", nvi
, serverNum
);
962 argv
[3] = &buffer
[strlen(&buffer
[0])+1];
964 _eval(argv
, NULL
, 0, NULL
);
965 vpnlog(VPN_LOG_EXTRA
,"Done adding cron job");
969 sprintf(&buffer
[0], "vpn_server%d", serverNum
);
970 allow_fastnat(buffer
, 0);
971 try_enabling_fastnat();
973 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend complete.");
976 void stop_vpnserver(int serverNum
)
980 char buffer
[BUF_SIZE
];
982 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
984 stop_service(&buffer
[0]);
988 vpnlog(VPN_LOG_INFO
,"Stopping VPN GUI server backend.");
991 vpnlog(VPN_LOG_EXTRA
,"Removing cron job");
994 sprintf(&buffer
[0], "CheckVPNServer%d", serverNum
);
995 argv
[2] = &buffer
[0];
997 _eval(argv
, NULL
, 0, NULL
);
998 vpnlog(VPN_LOG_EXTRA
,"Done removing cron job");
1000 // Remove firewall rules
1001 vpnlog(VPN_LOG_EXTRA
,"Removing firewall rules.");
1002 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
1005 argv
[2] = "s/-A/-D/g;s/-I/-D/g";
1006 argv
[3] = &buffer
[0];
1008 if (!_eval(argv
, NULL
, 0, NULL
))
1010 argv
[0] = &buffer
[0];
1012 _eval(argv
, NULL
, 0, NULL
);
1014 vpnlog(VPN_LOG_EXTRA
,"Done removing firewall rules.");
1016 // Stop the VPN server
1017 vpnlog(VPN_LOG_EXTRA
,"Stopping OpenVPN server.");
1018 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
1019 if ( !waitfor(&buffer
[0]) )
1020 vpnlog(VPN_LOG_EXTRA
,"OpenVPN server stopped.");
1022 // NVRAM setting for device type could have changed, just try to remove both
1023 vpnlog(VPN_LOG_EXTRA
,"Removing VPN device.");
1024 sprintf(&buffer
[0], "openvpn --rmtun --dev tap%d", serverNum
+SERVER_IF_START
);
1025 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
1026 _eval(argv
, NULL
, 0, NULL
);
1028 sprintf(&buffer
[0], "openvpn --rmtun --dev tun%d", serverNum
+SERVER_IF_START
);
1029 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
1030 _eval(argv
, NULL
, 0, NULL
);
1031 vpnlog(VPN_LOG_EXTRA
,"VPN device removed.");
1035 if ( nvram_get_int("vpn_debug") <= VPN_LOG_EXTRA
)
1037 vpnlog(VPN_LOG_EXTRA
,"Removing generated files.");
1038 // Delete all files for this server
1039 sprintf(&buffer
[0], "rm -rf /etc/openvpn/server%d /etc/openvpn/fw/server%d-fw.sh /etc/openvpn/vpnserver%d",serverNum
,serverNum
,serverNum
);
1040 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
1041 _eval(argv
, NULL
, 0, NULL
);
1043 // Attempt to remove directories. Will fail if not empty
1044 rmdir("/etc/openvpn/fw");
1045 rmdir("/etc/openvpn");
1046 vpnlog(VPN_LOG_EXTRA
,"Done removing generated files.");
1050 sprintf(&buffer
[0], "vpn_server%d", serverNum
);
1051 allow_fastnat(buffer
, 1);
1052 try_enabling_fastnat();
1054 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend stopped.");
1057 void start_vpn_eas()
1059 char buffer
[16], *cur
;
1062 if (strlen(nvram_safe_get("vpn_server_eas")) == 0 && strlen(nvram_safe_get("vpn_client_eas")) == 0) return;
1063 // wait for time sync for a while
1065 while (time(0) < Y2K
&& i
--) {
1069 // Parse and start servers
1070 strlcpy(&buffer
[0], nvram_safe_get("vpn_server_eas"), sizeof(buffer
));
1071 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Starting OpenVPN servers (eas): %s", &buffer
[0]);
1073 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
1075 for( i
= 0; nums
[i
] > 0; i
++ )
1077 sprintf(&buffer
[0], "vpnserver%d", nums
[i
]);
1078 if ( pidof(&buffer
[0]) >= 0 )
1080 vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN server %d (eas)", nums
[i
]);
1081 stop_vpnserver(nums
[i
]);
1084 vpnlog(VPN_LOG_INFO
, "Starting OpenVPN server %d (eas)", nums
[i
]);
1085 start_vpnserver(nums
[i
]);
1088 // Parse and start clients
1089 strlcpy(&buffer
[0], nvram_safe_get("vpn_client_eas"), sizeof(buffer
));
1090 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Starting clients (eas): %s", &buffer
[0]);
1092 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
1094 for( i
= 0; nums
[i
] > 0; i
++ )
1096 sprintf(&buffer
[0], "vpnclient%d", nums
[i
]);
1097 if ( pidof(&buffer
[0]) >= 0 )
1099 vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN client %d (eas)", nums
[i
]);
1100 stop_vpnclient(nums
[i
]);
1103 vpnlog(VPN_LOG_INFO
, "Starting OpenVPN client %d (eas)", nums
[i
]);
1104 start_vpnclient(nums
[i
]);
1110 char buffer
[16], *cur
;
1113 // Parse and stop servers
1114 strlcpy(&buffer
[0], nvram_safe_get("vpn_server_eas"), sizeof(buffer
));
1115 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN servers (eas): %s", &buffer
[0]);
1117 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
1119 for( i
= 0; nums
[i
] > 0; i
++ )
1121 sprintf(&buffer
[0], "vpnserver%d", nums
[i
]);
1122 if ( pidof(&buffer
[0]) >= 0 )
1124 vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN server %d (eas)", nums
[i
]);
1125 stop_vpnserver(nums
[i
]);
1129 // Parse and stop clients
1130 strlcpy(&buffer
[0], nvram_safe_get("vpn_client_eas"), sizeof(buffer
));
1131 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN clients (eas): %s", &buffer
[0]);
1133 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
1135 for( i
= 0; nums
[i
] > 0; i
++ )
1137 sprintf(&buffer
[0], "vpnclient%d", nums
[i
]);
1138 if ( pidof(&buffer
[0]) >= 0 )
1140 vpnlog(VPN_LOG_INFO
, "Stopping OpenVPN client %d (eas)", nums
[i
]);
1141 stop_vpnclient(nums
[i
]);
1146 void run_vpn_firewall_scripts()
1149 struct dirent
*file
;
1153 if ( chdir("/etc/openvpn/fw") )
1156 dir
= opendir("/etc/openvpn/fw");
1158 vpnlog(VPN_LOG_EXTRA
,"Beginning all firewall scripts...");
1159 while ( (file
= readdir(dir
)) != NULL
)
1164 vpnlog(VPN_LOG_INFO
,"Running firewall script: %s", fn
);
1165 argv
[0] = "/bin/sh";
1168 _eval(argv
, NULL
, 0, NULL
);
1170 vpnlog(VPN_LOG_EXTRA
,"Done with all firewall scripts...");
1175 void write_vpn_dnsmasq_config(FILE* f
)
1182 struct dirent
*file
;
1185 strlcpy(&buf
[0], nvram_safe_get("vpn_server_dns"), sizeof(buf
));
1186 for ( pos
= strtok(&buf
[0],","); pos
!= NULL
; pos
=strtok(NULL
, ",") )
1191 vpnlog(VPN_LOG_EXTRA
, "Adding server %d interface to dns config", cur
);
1192 snprintf(&nv
[0], sizeof(nv
), "vpn_server%d_if", cur
);
1193 fprintf(f
, "interface=%s%d\n", nvram_safe_get(&nv
[0]), SERVER_IF_START
+cur
);
1197 if ( (dir
= opendir("/etc/openvpn/dns")) != NULL
)
1199 while ( (file
= readdir(dir
)) != NULL
)
1201 if ( file
->d_name
[0] == '.' )
1204 if ( sscanf(file
->d_name
, "client%d.resol%c", &cur
, &ch
) == 2 )
1206 vpnlog(VPN_LOG_EXTRA
, "Checking ADNS settings for client %d", cur
);
1207 snprintf(&buf
[0], sizeof(buf
), "vpn_client%d_adns", cur
);
1208 if ( nvram_get_int(&buf
[0]) == 2 )
1210 vpnlog(VPN_LOG_INFO
, "Adding strict-order to dnsmasq config for client %d", cur
);
1211 fprintf(f
, "strict-order\n");
1216 if ( sscanf(file
->d_name
, "client%d.con%c", &cur
, &ch
) == 2 )
1218 if ( (dnsf
= fopen(file
->d_name
, "r")) != NULL
)
1220 vpnlog(VPN_LOG_INFO
, "Adding Dnsmasq config from %s", file
->d_name
);
1222 while( !feof(dnsf
) )
1225 fputc(ch
==EOF
?'\n':ch
, f
);
1235 int write_vpn_resolv(FILE* f
)
1238 struct dirent
*file
;
1239 char *fn
, ch
, num
, buf
[24];
1243 if ( chdir("/etc/openvpn/dns") )
1246 dir
= opendir("/etc/openvpn/dns");
1248 vpnlog(VPN_LOG_EXTRA
, "Adding DNS entries...");
1249 while ( (file
= readdir(dir
)) != NULL
)
1256 if ( sscanf(fn
, "client%c.resol%c", &num
, &ch
) == 2 )
1258 if ( (dnsf
= fopen(fn
, "r")) == NULL
)
1261 vpnlog(VPN_LOG_INFO
,"Adding DNS entries from %s", fn
);
1263 while( !feof(dnsf
) )
1266 fputc(ch
==EOF
?'\n':ch
, f
);
1271 snprintf(&buf
[0], sizeof(buf
), "vpn_client%c_adns", num
);
1272 if ( nvram_get_int(&buf
[0]) == 3 )
1276 vpnlog(VPN_LOG_EXTRA
, "Done with DNS entries...");