3 Copyright (C) 2008-2010 Keith Moyer, tomatovpn@keithmoyer.com
5 No part of this file may be used without permission.
10 #include <sys/types.h>
14 // Line number as text string
15 #define __LINE_T__ __LINE_T_(__LINE__)
16 #define __LINE_T_(x) __LINE_T(x)
17 #define __LINE_T(x) # x
19 #define VPN_LOG_ERROR -1
20 #define VPN_LOG_NOTE 0
21 #define VPN_LOG_INFO 1
22 #define VPN_LOG_EXTRA 2
23 #define vpnlog(level,x...) if(nvram_get_int("vpn_debug")>=level) syslog(LOG_INFO, #level ": " __LINE_T__ ": " x)
25 #define CLIENT_IF_START 10
26 #define SERVER_IF_START 20
31 void start_vpnclient(int clientNum
)
35 char buffer
[BUF_SIZE
];
38 enum { TLS
, SECRET
, CUSTOM
} cryptMode
= CUSTOM
;
39 enum { TAP
, TUN
} ifType
= TUN
;
40 enum { BRIDGE
, NAT
, NONE
} routeMode
= NONE
;
41 int nvi
, ip
[4], nm
[4];
44 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend starting...");
46 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
47 if ( pidof(&buffer
[0]) >= 0 )
49 vpnlog(VPN_LOG_NOTE
, "VPN Client %d already running...", clientNum
);
50 vpnlog(VPN_LOG_INFO
,"PID: %d", pidof(&buffer
[0]));
54 // Determine interface
55 sprintf(&buffer
[0], "vpn_client%d_if", clientNum
);
56 if ( nvram_contains_word(&buffer
[0], "tap") )
58 else if ( nvram_contains_word(&buffer
[0], "tun") )
62 vpnlog(VPN_LOG_ERROR
, "Invalid interface type, %.3s", nvram_safe_get(&buffer
[0]));
66 // Build interface name
67 snprintf(&iface
[0], IF_SIZE
, "%s%d", nvram_safe_get(&buffer
[0]), clientNum
+CLIENT_IF_START
);
69 // Determine encryption mode
70 sprintf(&buffer
[0], "vpn_client%d_crypt", clientNum
);
71 if ( nvram_contains_word(&buffer
[0], "tls") )
73 else if ( nvram_contains_word(&buffer
[0], "secret") )
75 else if ( nvram_contains_word(&buffer
[0], "custom") )
79 vpnlog(VPN_LOG_ERROR
,"Invalid encryption mode, %.6s", nvram_safe_get(&buffer
[0]));
83 // Determine if we should bridge the tunnel
84 sprintf(&buffer
[0], "vpn_client%d_bridge", clientNum
);
85 if ( ifType
== TAP
&& nvram_get_int(&buffer
[0]) == 1 )
88 // Determine if we should NAT the tunnel
89 sprintf(&buffer
[0], "vpn_client%d_nat", clientNum
);
90 if ( (ifType
== TUN
|| routeMode
!= BRIDGE
) && nvram_get_int(&buffer
[0]) == 1 )
93 // Make sure openvpn directory exists
94 mkdir("/etc/openvpn", 0700);
95 sprintf(&buffer
[0], "/etc/openvpn/client%d", clientNum
);
96 mkdir(&buffer
[0], 0700);
98 // Make sure symbolic link exists
99 sprintf(&buffer
[0], "/etc/openvpn/vpnclient%d", clientNum
);
101 if ( symlink("/usr/sbin/openvpn", &buffer
[0]) )
103 vpnlog(VPN_LOG_ERROR
,"Creating symlink failed...");
104 stop_vpnclient(clientNum
);
108 // Make sure module is loaded
111 // Create tap/tun interface
112 sprintf(&buffer
[0], "openvpn --mktun --dev %s", &iface
[0]);
113 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
114 if ( _eval(argv
, NULL
, 0, NULL
) )
116 vpnlog(VPN_LOG_ERROR
,"Creating tunnel interface failed...");
117 stop_vpnclient(clientNum
);
121 // Bring interface up (TAP only)
124 if ( routeMode
== BRIDGE
)
126 snprintf(&buffer
[0], BUF_SIZE
, "brctl addif %s %s", nvram_safe_get("lan_ifname"), &iface
[0]);
127 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
128 if ( _eval(argv
, NULL
, 0, NULL
) )
130 vpnlog(VPN_LOG_ERROR
,"Adding tunnel interface to bridge failed...");
131 stop_vpnclient(clientNum
);
136 snprintf(&buffer
[0], BUF_SIZE
, "ifconfig %s promisc up", &iface
[0]);
137 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
138 if ( _eval(argv
, NULL
, 0, NULL
) )
140 vpnlog(VPN_LOG_ERROR
,"Bringing interface up failed...");
141 stop_vpnclient(clientNum
);
146 // Build and write config file
147 vpnlog(VPN_LOG_EXTRA
,"Writing config file");
148 sprintf(&buffer
[0], "/etc/openvpn/client%d/config.ovpn", clientNum
);
149 fp
= fopen(&buffer
[0], "w");
150 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
151 fprintf(fp
, "# Automatically generated configuration\n");
152 fprintf(fp
, "daemon\n");
153 if ( cryptMode
== TLS
)
154 fprintf(fp
, "client\n");
155 fprintf(fp
, "dev %s\n", &iface
[0]);
156 sprintf(&buffer
[0], "vpn_client%d_proto", clientNum
);
157 fprintf(fp
, "proto %s\n", nvram_safe_get(&buffer
[0]));
158 sprintf(&buffer
[0], "vpn_client%d_addr", clientNum
);
159 fprintf(fp
, "remote %s ", nvram_safe_get(&buffer
[0]));
160 sprintf(&buffer
[0], "vpn_client%d_port", clientNum
);
161 fprintf(fp
, "%d\n", nvram_get_int(&buffer
[0]));
162 if ( cryptMode
== SECRET
)
166 sprintf(&buffer
[0], "vpn_client%d_local", clientNum
);
167 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
168 sprintf(&buffer
[0], "vpn_client%d_remote", clientNum
);
169 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
171 else if ( ifType
== TAP
)
173 sprintf(&buffer
[0], "vpn_client%d_local", clientNum
);
174 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
175 sprintf(&buffer
[0], "vpn_client%d_nm", clientNum
);
176 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
179 sprintf(&buffer
[0], "vpn_client%d_retry", clientNum
);
180 if ( (nvi
= nvram_get_int(&buffer
[0])) >= 0 )
181 fprintf(fp
, "resolv-retry %d\n", nvi
);
183 fprintf(fp
, "resolv-retry infinite\n");
184 sprintf(&buffer
[0], "vpn_client%d_reneg", clientNum
);
185 if ( (nvl
= atol(nvram_safe_get(&buffer
[0]))) >= 0 )
186 fprintf(fp
, "reneg-sec %ld\n", nvl
);
187 fprintf(fp
, "nobind\n");
188 fprintf(fp
, "persist-key\n");
189 fprintf(fp
, "persist-tun\n");
190 sprintf(&buffer
[0], "vpn_client%d_comp", clientNum
);
191 if ( nvram_get_int(&buffer
[0]) >= 0 )
192 fprintf(fp
, "comp-lzo %s\n", nvram_safe_get(&buffer
[0]));
193 sprintf(&buffer
[0], "vpn_client%d_cipher", clientNum
);
194 if ( !nvram_contains_word(&buffer
[0], "default") )
195 fprintf(fp
, "cipher %s\n", nvram_safe_get(&buffer
[0]));
196 sprintf(&buffer
[0], "vpn_client%d_rgw", clientNum
);
197 if ( (nvi
= nvram_get_int(&buffer
[0])) )
199 sprintf(&buffer
[0], "vpn_client%d_gw", clientNum
);
200 if ( ifType
== TAP
&& nvram_safe_get(&buffer
[0])[0] != '\0' )
201 fprintf(fp
, "route-gateway %s\n", nvram_safe_get(&buffer
[0]));
202 fprintf(fp
, "redirect-gateway%s\n", nvi
>1? "": " def1");
204 fprintf(fp
, "verb 3\n");
205 if ( cryptMode
== TLS
)
207 sprintf(&buffer
[0], "vpn_client%d_adns", clientNum
);
208 if ( nvram_get_int(&buffer
[0]) > 0 )
210 sprintf(&buffer
[0], "/etc/openvpn/client%d/updown.sh", clientNum
);
211 symlink("/rom/openvpn/updown.sh", &buffer
[0]);
212 fprintf(fp
, "script-security 2\n");
213 fprintf(fp
, "up updown.sh\n");
214 fprintf(fp
, "down updown.sh\n");
217 sprintf(&buffer
[0], "vpn_client%d_hmac", clientNum
);
218 nvi
= nvram_get_int(&buffer
[0]);
219 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
220 if ( !nvram_is_empty(&buffer
[0]) && nvi
>= 0 )
222 fprintf(fp
, "tls-auth static.key");
224 fprintf(fp
, " %d", nvi
);
228 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
229 if ( !nvram_is_empty(&buffer
[0]) )
230 fprintf(fp
, "ca ca.crt\n");
231 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
232 if ( !nvram_is_empty(&buffer
[0]) )
233 fprintf(fp
, "cert client.crt\n");
234 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
235 if ( !nvram_is_empty(&buffer
[0]) )
236 fprintf(fp
, "key client.key\n");
238 else if ( cryptMode
== SECRET
)
240 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
241 if ( !nvram_is_empty(&buffer
[0]) )
242 fprintf(fp
, "secret static.key\n");
244 fprintf(fp
, "status-version 2\n");
245 fprintf(fp
, "status status\n");
246 fprintf(fp
, "\n# Custom Configuration\n");
247 sprintf(&buffer
[0], "vpn_client%d_custom", clientNum
);
248 fprintf(fp
, nvram_safe_get(&buffer
[0]));
250 vpnlog(VPN_LOG_EXTRA
,"Done writing config file");
252 // Write certification and key files
253 vpnlog(VPN_LOG_EXTRA
,"Writing certs/keys");
254 if ( cryptMode
== TLS
)
256 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
257 if ( !nvram_is_empty(&buffer
[0]) )
259 sprintf(&buffer
[0], "/etc/openvpn/client%d/ca.crt", clientNum
);
260 fp
= fopen(&buffer
[0], "w");
261 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
262 sprintf(&buffer
[0], "vpn_client%d_ca", clientNum
);
263 fprintf(fp
, nvram_safe_get(&buffer
[0]));
267 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
268 if ( !nvram_is_empty(&buffer
[0]) )
270 sprintf(&buffer
[0], "/etc/openvpn/client%d/client.key", clientNum
);
271 fp
= fopen(&buffer
[0], "w");
272 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
273 sprintf(&buffer
[0], "vpn_client%d_key", clientNum
);
274 fprintf(fp
, nvram_safe_get(&buffer
[0]));
278 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
279 if ( !nvram_is_empty(&buffer
[0]) )
281 sprintf(&buffer
[0], "/etc/openvpn/client%d/client.crt", clientNum
);
282 fp
= fopen(&buffer
[0], "w");
283 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
284 sprintf(&buffer
[0], "vpn_client%d_crt", clientNum
);
285 fprintf(fp
, nvram_safe_get(&buffer
[0]));
289 sprintf(&buffer
[0], "vpn_client%d_hmac", clientNum
);
290 if ( cryptMode
== SECRET
|| (cryptMode
== TLS
&& nvram_get_int(&buffer
[0]) >= 0) )
292 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
293 if ( !nvram_is_empty(&buffer
[0]) )
295 sprintf(&buffer
[0], "/etc/openvpn/client%d/static.key", clientNum
);
296 fp
= fopen(&buffer
[0], "w");
297 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
298 sprintf(&buffer
[0], "vpn_client%d_static", clientNum
);
299 fprintf(fp
, nvram_safe_get(&buffer
[0]));
303 vpnlog(VPN_LOG_EXTRA
,"Done writing certs/keys");
305 // Start the VPN client
306 sprintf(&buffer
[0], "/etc/openvpn/vpnclient%d --cd /etc/openvpn/client%d --config config.ovpn", clientNum
, clientNum
);
307 vpnlog(VPN_LOG_INFO
,"Starting OpenVPN: %s",&buffer
[0]);
308 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
309 if ( _eval(argv
, NULL
, 0, NULL
) )
311 vpnlog(VPN_LOG_ERROR
,"Starting OpenVPN failed...");
312 stop_vpnclient(clientNum
);
315 vpnlog(VPN_LOG_EXTRA
,"Done starting openvpn");
317 // Handle firewall rules if appropriate
318 sprintf(&buffer
[0], "vpn_client%d_firewall", clientNum
);
319 if ( !nvram_contains_word(&buffer
[0], "custom") )
321 // Create firewall rules
322 vpnlog(VPN_LOG_EXTRA
,"Creating firewall rules");
323 mkdir("/etc/openvpn/fw", 0700);
324 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
325 fp
= fopen(&buffer
[0], "w");
326 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
|S_IXUSR
);
327 fprintf(fp
, "#!/bin/sh\n");
328 fprintf(fp
, "iptables -I INPUT -i %s -j ACCEPT\n", &iface
[0]);
329 fprintf(fp
, "iptables -I FORWARD -i %s -j ACCEPT\n", &iface
[0]);
330 if ( routeMode
== NAT
)
332 sscanf(nvram_safe_get("lan_ipaddr"), "%d.%d.%d.%d", &ip
[0], &ip
[1], &ip
[2], &ip
[3]);
333 sscanf(nvram_safe_get("lan_netmask"), "%d.%d.%d.%d", &nm
[0], &nm
[1], &nm
[2], &nm
[3]);
334 fprintf(fp
, "iptables -t nat -I POSTROUTING -s %d.%d.%d.%d/%s -o %s -j MASQUERADE\n",
335 ip
[0]&nm
[0], ip
[1]&nm
[1], ip
[2]&nm
[2], ip
[3]&nm
[3], nvram_safe_get("lan_netmask"), &iface
[0]);
338 vpnlog(VPN_LOG_EXTRA
,"Done creating firewall rules");
340 // Run the firewall rules
341 vpnlog(VPN_LOG_EXTRA
,"Running firewall rules");
342 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
343 argv
[0] = &buffer
[0];
345 _eval(argv
, NULL
, 0, NULL
);
346 vpnlog(VPN_LOG_EXTRA
,"Done running firewall rules");
350 sprintf(&buffer
[0], "vpn_client%d_poll", clientNum
);
351 if ( (nvi
= nvram_get_int(&buffer
[0])) > 0 )
353 vpnlog(VPN_LOG_EXTRA
,"Adding cron job");
356 sprintf(&buffer
[0], "CheckVPNClient%d", clientNum
);
357 argv
[2] = &buffer
[0];
358 sprintf(&buffer
[strlen(&buffer
[0])+1], "*/%d * * * * service vpnclient%d start", nvi
, clientNum
);
359 argv
[3] = &buffer
[strlen(&buffer
[0])+1];
361 _eval(argv
, NULL
, 0, NULL
);
362 vpnlog(VPN_LOG_EXTRA
,"Done adding cron job");
365 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend complete.");
368 void stop_vpnclient(int clientNum
)
372 char buffer
[BUF_SIZE
];
374 vpnlog(VPN_LOG_INFO
,"Stopping VPN GUI client backend.");
377 vpnlog(VPN_LOG_EXTRA
,"Removing cron job");
380 sprintf(&buffer
[0], "CheckVPNClient%d", clientNum
);
381 argv
[2] = &buffer
[0];
383 _eval(argv
, NULL
, 0, NULL
);
384 vpnlog(VPN_LOG_EXTRA
,"Done removing cron job");
386 // Remove firewall rules
387 vpnlog(VPN_LOG_EXTRA
,"Removing firewall rules.");
388 sprintf(&buffer
[0], "/etc/openvpn/fw/client%d-fw.sh", clientNum
);
391 argv
[2] = "s/-A/-D/g;s/-I/-D/g";
392 argv
[3] = &buffer
[0];
394 if (!_eval(argv
, NULL
, 0, NULL
))
396 argv
[0] = &buffer
[0];
398 _eval(argv
, NULL
, 0, NULL
);
400 vpnlog(VPN_LOG_EXTRA
,"Done removing firewall rules.");
402 // Stop the VPN client
403 vpnlog(VPN_LOG_EXTRA
,"Stopping OpenVPN client.");
404 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
405 killall(&buffer
[0], SIGTERM
);
406 vpnlog(VPN_LOG_EXTRA
,"OpenVPN client stopped.");
408 // NVRAM setting for device type could have changed, just try to remove both
409 vpnlog(VPN_LOG_EXTRA
,"Removing VPN device.");
410 sprintf(&buffer
[0], "openvpn --rmtun --dev tap%d", clientNum
+CLIENT_IF_START
);
411 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
412 _eval(argv
, NULL
, 0, NULL
);
414 sprintf(&buffer
[0], "openvpn --rmtun --dev tun%d", clientNum
+CLIENT_IF_START
);
415 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
416 _eval(argv
, NULL
, 0, NULL
);
417 vpnlog(VPN_LOG_EXTRA
,"VPN device removed.");
421 if ( nvram_get_int("vpn_debug") <= VPN_LOG_EXTRA
)
423 vpnlog(VPN_LOG_EXTRA
,"Removing generated files.");
424 // Delete all files for this client
425 sprintf(&buffer
[0], "rm -rf /etc/openvpn/client%d /etc/openvpn/fw/client%d-fw.sh /etc/openvpn/vpnclient%d",clientNum
,clientNum
,clientNum
);
426 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
427 _eval(argv
, NULL
, 0, NULL
);
429 // Attempt to remove directories. Will fail if not empty
430 rmdir("/etc/openvpn/fw");
431 rmdir("/etc/openvpn");
432 vpnlog(VPN_LOG_EXTRA
,"Done removing generated files.");
435 // Force OpenVPN process to end. If we don't do this then it doesn't actually exit until
436 // all current queued service actions are run, including starting vpn back up (which
437 // will bail since the process is still running
438 vpnlog(VPN_LOG_EXTRA
,"Killing OpenVPN client.");
439 sprintf(&buffer
[0], "vpnclient%d", clientNum
);
440 killall(&buffer
[0], SIGKILL
);
441 vpnlog(VPN_LOG_EXTRA
,"OpenVPN client killed.");
443 vpnlog(VPN_LOG_INFO
,"VPN GUI client backend stopped.");
446 void start_vpnserver(int serverNum
)
450 char buffer
[BUF_SIZE
];
451 char *argv
[6], *chp
, *route
;
454 enum { TAP
, TUN
} ifType
= TUN
;
455 enum { TLS
, SECRET
, CUSTOM
} cryptMode
= CUSTOM
;
456 int nvi
, ip
[4], nm
[4];
459 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend starting...");
461 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
462 if ( pidof(&buffer
[0]) >= 0 )
464 vpnlog(VPN_LOG_NOTE
, "VPN Server %d already running...", serverNum
);
465 vpnlog(VPN_LOG_INFO
,"PID: %d", pidof(&buffer
[0]));
469 // Determine interface type
470 sprintf(&buffer
[0], "vpn_server%d_if", serverNum
);
471 if ( nvram_contains_word(&buffer
[0], "tap") )
473 else if ( nvram_contains_word(&buffer
[0], "tun") )
477 vpnlog(VPN_LOG_ERROR
,"Invalid interface type, %.3s", nvram_safe_get(&buffer
[0]));
481 // Build interface name
482 snprintf(&iface
[0], IF_SIZE
, "%s%d", nvram_safe_get(&buffer
[0]), serverNum
+SERVER_IF_START
);
484 // Determine encryption mode
485 sprintf(&buffer
[0], "vpn_server%d_crypt", serverNum
);
486 if ( nvram_contains_word(&buffer
[0], "tls") )
488 else if ( nvram_contains_word(&buffer
[0], "secret") )
490 else if ( nvram_contains_word(&buffer
[0], "custom") )
494 vpnlog(VPN_LOG_ERROR
,"Invalid encryption mode, %.6s", nvram_safe_get(&buffer
[0]));
498 // Make sure openvpn directory exists
499 mkdir("/etc/openvpn", 0700);
500 sprintf(&buffer
[0], "/etc/openvpn/server%d", serverNum
);
501 mkdir(&buffer
[0], 0700);
503 // Make sure symbolic link exists
504 sprintf(&buffer
[0], "/etc/openvpn/vpnserver%d", serverNum
);
506 if ( symlink("/usr/sbin/openvpn", &buffer
[0]) )
508 vpnlog(VPN_LOG_ERROR
,"Creating symlink failed...");
509 stop_vpnserver(serverNum
);
513 // Make sure module is loaded
516 // Create tap/tun interface
517 sprintf(&buffer
[0], "openvpn --mktun --dev %s", &iface
[0]);
518 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
519 if ( _eval(argv
, NULL
, 0, NULL
) )
521 vpnlog(VPN_LOG_ERROR
,"Creating tunnel interface failed...");
522 stop_vpnserver(serverNum
);
526 // Add interface to LAN bridge (TAP only)
529 snprintf(&buffer
[0], BUF_SIZE
, "brctl addif %s %s", nvram_safe_get("lan_ifname"), &iface
[0]);
530 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
531 if ( _eval(argv
, NULL
, 0, NULL
) )
533 vpnlog(VPN_LOG_ERROR
,"Adding tunnel interface to bridge failed...");
534 stop_vpnserver(serverNum
);
539 // Bring interface up
540 sprintf(&buffer
[0], "ifconfig %s 0.0.0.0 promisc up", &iface
[0]);
541 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
542 if ( _eval(argv
, NULL
, 0, NULL
) )
544 vpnlog(VPN_LOG_ERROR
,"Bringing up tunnel interface failed...");
545 stop_vpnserver(serverNum
);
549 // Build and write config files
550 vpnlog(VPN_LOG_EXTRA
,"Writing config file");
551 sprintf(&buffer
[0], "/etc/openvpn/server%d/config.ovpn", serverNum
);
552 fp
= fopen(&buffer
[0], "w");
553 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
554 fprintf(fp
, "# Automatically generated configuration\n");
555 fprintf(fp
, "daemon\n");
556 if ( cryptMode
== TLS
)
560 sprintf(&buffer
[0], "vpn_server%d_sn", serverNum
);
561 fprintf(fp
, "server %s ", nvram_safe_get(&buffer
[0]));
562 sprintf(&buffer
[0], "vpn_server%d_nm", serverNum
);
563 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
565 else if ( ifType
== TAP
)
567 fprintf(fp
, "server-bridge");
568 sprintf(&buffer
[0], "vpn_server%d_dhcp", serverNum
);
569 if ( nvram_get_int(&buffer
[0]) == 0 )
571 fprintf(fp
, " %s ", nvram_safe_get("lan_ipaddr"));
572 fprintf(fp
, "%s ", nvram_safe_get("lan_netmask"));
573 sprintf(&buffer
[0], "vpn_server%d_r1", serverNum
);
574 fprintf(fp
, "%s ", nvram_safe_get(&buffer
[0]));
575 sprintf(&buffer
[0], "vpn_server%d_r2", serverNum
);
576 fprintf(fp
, "%s", nvram_safe_get(&buffer
[0]));
581 else if ( cryptMode
== SECRET
)
585 sprintf(&buffer
[0], "vpn_server%d_local", serverNum
);
586 fprintf(fp
, "ifconfig %s ", nvram_safe_get(&buffer
[0]));
587 sprintf(&buffer
[0], "vpn_server%d_remote", serverNum
);
588 fprintf(fp
, "%s\n", nvram_safe_get(&buffer
[0]));
591 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
592 fprintf(fp
, "proto %s\n", nvram_safe_get(&buffer
[0]));
593 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
594 fprintf(fp
, "port %d\n", nvram_get_int(&buffer
[0]));
595 fprintf(fp
, "dev %s\n", &iface
[0]);
596 sprintf(&buffer
[0], "vpn_server%d_cipher", serverNum
);
597 if ( !nvram_contains_word(&buffer
[0], "default") )
598 fprintf(fp
, "cipher %s\n", nvram_safe_get(&buffer
[0]));
599 sprintf(&buffer
[0], "vpn_server%d_comp", serverNum
);
600 if ( nvram_get_int(&buffer
[0]) >= 0 )
601 fprintf(fp
, "comp-lzo %s\n", nvram_safe_get(&buffer
[0]));
602 sprintf(&buffer
[0], "vpn_server%d_reneg", serverNum
);
603 if ( (nvl
= atol(nvram_safe_get(&buffer
[0]))) >= 0 )
604 fprintf(fp
, "reneg-sec %ld\n", nvl
);
605 fprintf(fp
, "keepalive 15 60\n");
606 fprintf(fp
, "verb 3\n");
607 if ( cryptMode
== TLS
)
609 sprintf(&buffer
[0], "vpn_server%d_plan", serverNum
);
610 if ( ifType
== TUN
&& nvram_get_int(&buffer
[0]) )
612 sscanf(nvram_safe_get("lan_ipaddr"), "%d.%d.%d.%d", &ip
[0], &ip
[1], &ip
[2], &ip
[3]);
613 sscanf(nvram_safe_get("lan_netmask"), "%d.%d.%d.%d", &nm
[0], &nm
[1], &nm
[2], &nm
[3]);
614 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],
615 nvram_safe_get("lan_netmask"));
618 sprintf(&buffer
[0], "vpn_server%d_ccd", serverNum
);
619 if ( nvram_get_int(&buffer
[0]) )
621 fprintf(fp
, "client-config-dir ccd\n");
623 sprintf(&buffer
[0], "vpn_server%d_c2c", serverNum
);
624 if ( (c2c
= nvram_get_int(&buffer
[0])) )
625 fprintf(fp
, "client-to-client\n");
627 sprintf(&buffer
[0], "vpn_server%d_ccd_excl", serverNum
);
628 if ( nvram_get_int(&buffer
[0]) )
629 fprintf(fp
, "ccd-exclusive\n");
631 sprintf(&buffer
[0], "/etc/openvpn/server%d/ccd", serverNum
);
632 mkdir(&buffer
[0], 0700);
635 sprintf(&buffer
[0], "vpn_server%d_ccd_val", serverNum
);
636 strcpy(&buffer
[0], nvram_safe_get(&buffer
[0]));
637 chp
= strtok(&buffer
[0],">");
638 while ( chp
!= NULL
)
642 chp
[strcspn(chp
,"<")] = '\0';
643 vpnlog(VPN_LOG_EXTRA
,"CCD: enabled: %d", atoi(chp
));
644 if ( atoi(chp
) == 1 )
646 nvi
-= strlen(chp
)+1;
647 chp
+= strlen(chp
)+1;
653 chp
[strcspn(chp
,"<")] = '\0';
654 vpnlog(VPN_LOG_EXTRA
,"CCD: Common name: %s", chp
);
655 ccd
= fopen(chp
, "w");
656 chmod(chp
, S_IRUSR
|S_IWUSR
);
658 nvi
-= strlen(chp
)+1;
659 chp
+= strlen(chp
)+1;
661 if ( nvi
> 0 && ccd
!= NULL
&& strcspn(chp
,"<") != strlen(chp
) )
663 chp
[strcspn(chp
,"<")] = ' ';
664 chp
[strcspn(chp
,"<")] = '\0';
666 vpnlog(VPN_LOG_EXTRA
,"CCD: Route: %s", chp
);
667 if ( strlen(route
) > 1 )
669 fprintf(ccd
, "iroute %s\n", route
);
670 fprintf(fp
, "route %s\n", route
);
673 nvi
-= strlen(chp
)+1;
674 chp
+= strlen(chp
)+1;
678 if ( nvi
> 0 && route
!= NULL
)
680 chp
[strcspn(chp
,"<")] = '\0';
681 vpnlog(VPN_LOG_EXTRA
,"CCD: Push: %d", atoi(chp
));
682 if ( c2c
&& atoi(chp
) == 1 && strlen(route
) > 1 )
683 fprintf(fp
, "push \"route %s\"\n", route
);
685 nvi
-= strlen(chp
)+1;
686 chp
+= strlen(chp
)+1;
689 vpnlog(VPN_LOG_EXTRA
,"CCD leftover: %d", nvi
+1);
691 // Advance to next entry
692 chp
= strtok(NULL
, ">");
694 vpnlog(VPN_LOG_EXTRA
,"CCD processing complete");
697 sprintf(&buffer
[0], "vpn_server%d_pdns", serverNum
);
698 if ( nvram_get_int(&buffer
[0]) )
700 if ( nvram_safe_get("wan_domain")[0] != '\0' )
701 fprintf(fp
, "push \"dhcp-option DOMAIN %s\"\n", nvram_safe_get("wan_domain"));
702 if ( (nvram_safe_get("wan_wins")[0] != '\0' && strcmp(nvram_safe_get("wan_wins"), "0.0.0.0") != 0) )
703 fprintf(fp
, "push \"dhcp-option WINS %s\"\n", nvram_safe_get("wan_wins"));
704 fprintf(fp
, "push \"dhcp-option DNS %s\"\n", nvram_safe_get("lan_ipaddr"));
707 sprintf(&buffer
[0], "vpn_server%d_rgw", serverNum
);
708 if ( (nvi
= nvram_get_int(&buffer
[0])) )
711 fprintf(fp
, "push \"route-gateway %s\"\n", nvram_safe_get("lan_ipaddr"));
712 fprintf(fp
, "push \"redirect-gateway%s\"\n", nvi
>1? "": "def1");
715 sprintf(&buffer
[0], "vpn_server%d_hmac", serverNum
);
716 nvi
= nvram_get_int(&buffer
[0]);
717 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
718 if ( !nvram_is_empty(&buffer
[0]) && nvi
>= 0 )
720 fprintf(fp
, "tls-auth static.key");
722 fprintf(fp
, " %d", nvi
);
726 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
727 if ( !nvram_is_empty(&buffer
[0]) )
728 fprintf(fp
, "ca ca.crt\n");
729 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
730 if ( !nvram_is_empty(&buffer
[0]) )
731 fprintf(fp
, "dh dh.pem\n");
732 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
733 if ( !nvram_is_empty(&buffer
[0]) )
734 fprintf(fp
, "cert server.crt\n");
735 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
736 if ( !nvram_is_empty(&buffer
[0]) )
737 fprintf(fp
, "key server.key\n");
739 else if ( cryptMode
== SECRET
)
741 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
742 if ( !nvram_is_empty(&buffer
[0]) )
743 fprintf(fp
, "secret static.key\n");
745 fprintf(fp
, "status-version 2\n");
746 fprintf(fp
, "status status\n");
747 fprintf(fp
, "\n# Custom Configuration\n");
748 sprintf(&buffer
[0], "vpn_server%d_custom", serverNum
);
749 fprintf(fp
, nvram_safe_get(&buffer
[0]));
751 vpnlog(VPN_LOG_EXTRA
,"Done writing config file");
753 // Write certification and key files
754 vpnlog(VPN_LOG_EXTRA
,"Writing certs/keys");
755 if ( cryptMode
== TLS
)
757 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
758 if ( !nvram_is_empty(&buffer
[0]) )
760 sprintf(&buffer
[0], "/etc/openvpn/server%d/ca.crt", serverNum
);
761 fp
= fopen(&buffer
[0], "w");
762 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
763 sprintf(&buffer
[0], "vpn_server%d_ca", serverNum
);
764 fprintf(fp
, nvram_safe_get(&buffer
[0]));
768 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
769 if ( !nvram_is_empty(&buffer
[0]) )
771 sprintf(&buffer
[0], "/etc/openvpn/server%d/server.key", serverNum
);
772 fp
= fopen(&buffer
[0], "w");
773 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
774 sprintf(&buffer
[0], "vpn_server%d_key", serverNum
);
775 fprintf(fp
, nvram_safe_get(&buffer
[0]));
779 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
780 if ( !nvram_is_empty(&buffer
[0]) )
782 sprintf(&buffer
[0], "/etc/openvpn/server%d/server.crt", serverNum
);
783 fp
= fopen(&buffer
[0], "w");
784 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
785 sprintf(&buffer
[0], "vpn_server%d_crt", serverNum
);
786 fprintf(fp
, nvram_safe_get(&buffer
[0]));
790 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
791 if ( !nvram_is_empty(&buffer
[0]) )
793 sprintf(&buffer
[0], "/etc/openvpn/server%d/dh.pem", serverNum
);
794 fp
= fopen(&buffer
[0], "w");
795 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
796 sprintf(&buffer
[0], "vpn_server%d_dh", serverNum
);
797 fprintf(fp
, nvram_safe_get(&buffer
[0]));
801 sprintf(&buffer
[0], "vpn_server%d_hmac", serverNum
);
802 if ( cryptMode
== SECRET
|| (cryptMode
== TLS
&& nvram_get_int(&buffer
[0]) >= 0) )
804 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
805 if ( !nvram_is_empty(&buffer
[0]) )
807 sprintf(&buffer
[0], "/etc/openvpn/server%d/static.key", serverNum
);
808 fp
= fopen(&buffer
[0], "w");
809 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
);
810 sprintf(&buffer
[0], "vpn_server%d_static", serverNum
);
811 fprintf(fp
, nvram_safe_get(&buffer
[0]));
815 vpnlog(VPN_LOG_EXTRA
,"Done writing certs/keys");
817 sprintf(&buffer
[0], "/etc/openvpn/vpnserver%d --cd /etc/openvpn/server%d --config config.ovpn", serverNum
, serverNum
);
818 vpnlog(VPN_LOG_INFO
,"Starting OpenVPN: %s",&buffer
[0]);
819 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
820 if ( _eval(argv
, NULL
, 0, NULL
) )
822 vpnlog(VPN_LOG_ERROR
,"Starting VPN instance failed...");
823 stop_vpnserver(serverNum
);
826 vpnlog(VPN_LOG_EXTRA
,"Done starting openvpn");
828 // Handle firewall rules if appropriate
829 sprintf(&buffer
[0], "vpn_server%d_firewall", serverNum
);
830 if ( !nvram_contains_word(&buffer
[0], "custom") )
832 // Create firewall rules
833 vpnlog(VPN_LOG_EXTRA
,"Creating firewall rules");
834 mkdir("/etc/openvpn/fw", 0700);
835 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
836 fp
= fopen(&buffer
[0], "w");
837 chmod(&buffer
[0], S_IRUSR
|S_IWUSR
|S_IXUSR
);
838 fprintf(fp
, "#!/bin/sh\n");
839 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
840 strncpy(&buffer
[0], nvram_safe_get(&buffer
[0]), BUF_SIZE
);
841 fprintf(fp
, "iptables -t nat -I PREROUTING -p %s ", strtok(&buffer
[0], "-"));
842 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
843 fprintf(fp
, "--dport %d -j ACCEPT\n", nvram_get_int(&buffer
[0]));
844 sprintf(&buffer
[0], "vpn_server%d_proto", serverNum
);
845 strncpy(&buffer
[0], nvram_safe_get(&buffer
[0]), BUF_SIZE
);
846 fprintf(fp
, "iptables -I INPUT -p %s ", strtok(&buffer
[0], "-"));
847 sprintf(&buffer
[0], "vpn_server%d_port", serverNum
);
848 fprintf(fp
, "--dport %d -j ACCEPT\n", nvram_get_int(&buffer
[0]));
849 sprintf(&buffer
[0], "vpn_server%d_firewall", serverNum
);
850 if ( !nvram_contains_word(&buffer
[0], "external") )
852 fprintf(fp
, "iptables -I INPUT -i %s -j ACCEPT\n", &iface
[0]);
853 fprintf(fp
, "iptables -I FORWARD -i %s -j ACCEPT\n", &iface
[0]);
856 vpnlog(VPN_LOG_EXTRA
,"Done creating firewall rules");
858 // Run the firewall rules
859 vpnlog(VPN_LOG_EXTRA
,"Running firewall rules");
860 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
861 argv
[0] = &buffer
[0];
863 _eval(argv
, NULL
, 0, NULL
);
864 vpnlog(VPN_LOG_EXTRA
,"Done running firewall rules");
868 sprintf(&buffer
[0], "vpn_server%d_poll", serverNum
);
869 if ( (nvi
= nvram_get_int(&buffer
[0])) > 0 )
871 vpnlog(VPN_LOG_EXTRA
,"Adding cron job");
874 sprintf(&buffer
[0], "CheckVPNServer%d", serverNum
);
875 argv
[2] = &buffer
[0];
876 sprintf(&buffer
[strlen(&buffer
[0])+1], "*/%d * * * * service vpnserver%d start", nvi
, serverNum
);
877 argv
[3] = &buffer
[strlen(&buffer
[0])+1];
879 _eval(argv
, NULL
, 0, NULL
);
880 vpnlog(VPN_LOG_EXTRA
,"Done adding cron job");
883 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend complete.");
886 void stop_vpnserver(int serverNum
)
890 char buffer
[BUF_SIZE
];
892 vpnlog(VPN_LOG_INFO
,"Stopping VPN GUI server backend.");
895 vpnlog(VPN_LOG_EXTRA
,"Removing cron job");
898 sprintf(&buffer
[0], "CheckVPNServer%d", serverNum
);
899 argv
[2] = &buffer
[0];
901 _eval(argv
, NULL
, 0, NULL
);
902 vpnlog(VPN_LOG_EXTRA
,"Done removing cron job");
904 // Remove firewall rules
905 vpnlog(VPN_LOG_EXTRA
,"Removing firewall rules.");
906 sprintf(&buffer
[0], "/etc/openvpn/fw/server%d-fw.sh", serverNum
);
909 argv
[2] = "s/-A/-D/g;s/-I/-D/g";
910 argv
[3] = &buffer
[0];
912 if (!_eval(argv
, NULL
, 0, NULL
))
914 argv
[0] = &buffer
[0];
916 _eval(argv
, NULL
, 0, NULL
);
918 vpnlog(VPN_LOG_EXTRA
,"Done removing firewall rules.");
920 // Stop the VPN server
921 vpnlog(VPN_LOG_EXTRA
,"Stopping OpenVPN server.");
922 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
923 killall(&buffer
[0], SIGTERM
);
924 vpnlog(VPN_LOG_EXTRA
,"OpenVPN server stopped.");
926 // NVRAM setting for device type could have changed, just try to remove both
927 vpnlog(VPN_LOG_EXTRA
,"Removing VPN device.");
928 sprintf(&buffer
[0], "openvpn --rmtun --dev tap%d", serverNum
+SERVER_IF_START
);
929 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
930 _eval(argv
, NULL
, 0, NULL
);
932 sprintf(&buffer
[0], "openvpn --rmtun --dev tun%d", serverNum
+SERVER_IF_START
);
933 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
934 _eval(argv
, NULL
, 0, NULL
);
935 vpnlog(VPN_LOG_EXTRA
,"VPN device removed.");
939 if ( nvram_get_int("vpn_debug") <= VPN_LOG_EXTRA
)
941 vpnlog(VPN_LOG_EXTRA
,"Removing generated files.");
942 // Delete all files for this server
943 sprintf(&buffer
[0], "rm -rf /etc/openvpn/server%d /etc/openvpn/fw/server%d-fw.sh /etc/openvpn/vpnserver%d",serverNum
,serverNum
,serverNum
);
944 for (argv
[argc
=0] = strtok(&buffer
[0], " "); argv
[argc
] != NULL
; argv
[++argc
] = strtok(NULL
, " "));
945 _eval(argv
, NULL
, 0, NULL
);
947 // Attempt to remove directories. Will fail if not empty
948 rmdir("/etc/openvpn/fw");
949 rmdir("/etc/openvpn");
950 vpnlog(VPN_LOG_EXTRA
,"Done removing generated files.");
953 // Force OpenVPN process to end. If we don't do this then it doesn't actually exit until
954 // all current queued service actions are run, including starting vpn back up (which
955 // will bail since the process is still running
956 vpnlog(VPN_LOG_EXTRA
,"Killing OpenVPN client.");
957 sprintf(&buffer
[0], "vpnserver%d", serverNum
);
958 killall(&buffer
[0], SIGKILL
);
959 vpnlog(VPN_LOG_EXTRA
,"OpenVPN server killed.");
961 vpnlog(VPN_LOG_INFO
,"VPN GUI server backend stopped.");
966 char buffer
[16], *cur
;
969 // Parse and start servers
970 strlcpy(&buffer
[0], nvram_safe_get("vpn_server_eas"), sizeof(buffer
));
971 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Starting servers (eas): %s", &buffer
[0]);
973 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
975 for( i
= 0; nums
[i
] > 0; i
++ )
977 sprintf(&buffer
[0], "vpnserver%d", nums
[i
]);
978 if ( pidof(&buffer
[0]) >= 0 )
980 vpnlog(VPN_LOG_INFO
, "Stopping server %d (eas)", nums
[i
]);
981 stop_vpnserver(nums
[i
]);
985 vpnlog(VPN_LOG_INFO
, "Starting server %d (eas)", nums
[i
]);
986 start_vpnserver(nums
[i
]);
989 // Parse and start clients
990 strlcpy(&buffer
[0], nvram_safe_get("vpn_client_eas"), sizeof(buffer
));
991 if ( strlen(&buffer
[0]) != 0 ) vpnlog(VPN_LOG_INFO
, "Starting clients (eas): %s", &buffer
[0]);
993 for( cur
= strtok(&buffer
[0],","); cur
!= NULL
&& i
< 4; cur
= strtok(NULL
, ",")) { nums
[i
++] = atoi(cur
); }
995 for( i
= 0; nums
[i
] > 0; i
++ )
997 sprintf(&buffer
[0], "vpnclient%d", nums
[i
]);
998 if ( pidof(&buffer
[0]) >= 0 )
1000 vpnlog(VPN_LOG_INFO
, "Stopping client %d (eas)", nums
[i
]);
1001 stop_vpnclient(nums
[i
]);
1005 vpnlog(VPN_LOG_INFO
, "Starting client %d (eas)", nums
[i
]);
1006 start_vpnclient(nums
[i
]);
1010 void run_vpn_firewall_scripts()
1013 struct dirent
*file
;
1017 if ( chdir("/etc/openvpn/fw") )
1020 dir
= opendir("/etc/openvpn/fw");
1022 vpnlog(VPN_LOG_EXTRA
,"Beginning all firewall scripts...");
1023 while ( (file
= readdir(dir
)) != NULL
)
1028 vpnlog(VPN_LOG_INFO
,"Running firewall script: %s", fn
);
1029 argv
[0] = "/bin/sh";
1032 _eval(argv
, NULL
, 0, NULL
);
1034 vpnlog(VPN_LOG_EXTRA
,"Done with all firewall scripts...");
1039 void write_vpn_dnsmasq_config(FILE* f
)
1046 struct dirent
*file
;
1049 strlcpy(&buf
[0], nvram_safe_get("vpn_server_dns"), sizeof(buf
));
1050 for ( pos
= strtok(&buf
[0],","); pos
!= NULL
; pos
=strtok(NULL
, ",") )
1055 vpnlog(VPN_LOG_EXTRA
, "Adding server %d interface to dns config", cur
);
1056 snprintf(&nv
[0], sizeof(nv
), "vpn_server%d_if", cur
);
1057 fprintf(f
, "interface=%s%d\n", nvram_safe_get(&nv
[0]), SERVER_IF_START
+cur
);
1061 if ( (dir
= opendir("/etc/openvpn/dns")) != NULL
)
1063 while ( (file
= readdir(dir
)) != NULL
)
1065 if ( file
->d_name
[0] == '.' )
1068 if ( sscanf(file
->d_name
, "client%d.resol%c", &cur
, &ch
) == 2 )
1070 vpnlog(VPN_LOG_EXTRA
, "Checking ADNS settings for client %d", cur
);
1071 snprintf(&buf
[0], sizeof(buf
), "vpn_client%d_adns", cur
);
1072 if ( nvram_get_int(&buf
[0]) == 2 )
1074 vpnlog(VPN_LOG_INFO
, "Adding strict-order to dnsmasq config for client %d", cur
);
1075 fprintf(f
, "strict-order\n");
1080 if ( sscanf(file
->d_name
, "client%d.con%c", &cur
, &ch
) == 2 )
1082 if ( (dnsf
= fopen(file
->d_name
, "r")) != NULL
)
1084 vpnlog(VPN_LOG_INFO
, "Adding Dnsmasq config from %s", file
->d_name
);
1086 while( !feof(dnsf
) )
1089 fputc(ch
==EOF
?'\n':ch
, f
);
1099 int write_vpn_resolv(FILE* f
)
1102 struct dirent
*file
;
1103 char *fn
, ch
, num
, buf
[24];
1107 if ( chdir("/etc/openvpn/dns") )
1110 dir
= opendir("/etc/openvpn/dns");
1112 vpnlog(VPN_LOG_EXTRA
, "Adding DNS entries...");
1113 while ( (file
= readdir(dir
)) != NULL
)
1120 if ( sscanf(fn
, "client%c.resol%c", &num
, &ch
) == 2 )
1122 if ( (dnsf
= fopen(fn
, "r")) == NULL
)
1125 vpnlog(VPN_LOG_INFO
,"Adding DNS entries from %s", fn
);
1127 while( !feof(dnsf
) )
1130 fputc(ch
==EOF
?'\n':ch
, f
);
1135 snprintf(&buf
[0], sizeof(buf
), "vpn_client%c_adns", num
);
1136 if ( nvram_get_int(&buf
[0]) == 3 )
1140 vpnlog(VPN_LOG_EXTRA
, "Done with DNS entries...");