fix ftp permissions
[tomato.git] / release / src / router / rc / services.c
blob04356d75be13b36845e740403beb90e7f2ebd604
1 /*
3 Copyright 2003, CyberTAN Inc. All Rights Reserved
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
6 the contents of this file may not be disclosed to third parties,
7 copied or duplicated in any form without the prior written
8 permission of CyberTAN Inc.
10 This software should be used as a reference only, and it not
11 intended for production use!
13 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
14 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. CYBERTAN
15 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
21 Copyright 2005, Broadcom Corporation
22 All Rights Reserved.
24 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
25 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
26 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
27 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
32 Modified for Tomato Firmware
33 Portions, Copyright (C) 2006-2009 Jonathan Zarate
36 #include "rc.h"
38 #include <arpa/inet.h>
39 #include <time.h>
40 #include <sys/time.h>
41 #include <errno.h>
43 // !!TB
44 #include <sys/mount.h>
45 #include <mntent.h>
46 #include <dirent.h>
48 // Pop an alarm to recheck pids in 500 msec.
49 static const struct itimerval pop_tv = { {0,0}, {0, 500 * 1000} };
51 // Pop an alarm to reap zombies.
52 static const struct itimerval zombie_tv = { {0,0}, {307, 0} };
54 // -----------------------------------------------------------------------------
56 static const char dmhosts[] = "/etc/dnsmasq/hosts";
57 static const char dmdhcp[] = "/etc/dnsmasq/dhcp";
58 static const char dmresolv[] = "/etc/resolv.dnsmasq";
60 static pid_t pid_dnsmasq = -1;
62 static int is_wet(int idx, int unit, int subunit, void *param)
64 return nvram_match(wl_nvname("mode", unit, subunit), "wet");
67 void start_dnsmasq()
69 FILE *f;
70 const char *nv;
71 char buf[512];
72 char lan[24];
73 const char *router_ip;
74 char sdhcp_lease[32];
75 char *e;
76 int n;
77 char *mac, *ip, *name;
78 char *p;
79 int ipn;
80 char ipbuf[32];
81 FILE *hf, *df;
82 int dhcp_start;
83 int dhcp_count;
84 int dhcp_lease;
85 int do_dhcpd;
86 int do_dns;
87 int do_dhcpd_hosts;
89 #ifdef TCONFIG_IPV6
90 char *prefix, *ipv6, *mtu;
91 int do_6to4, do_6rd;
92 int service;
93 #endif
95 TRACE_PT("begin\n");
97 if (getpid() != 1) {
98 start_service("dnsmasq");
99 return;
102 stop_dnsmasq();
104 if (foreach_wif(1, NULL, is_wet)) return;
106 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
108 router_ip = nvram_safe_get("lan_ipaddr");
110 fprintf(f,
111 "pid-file=/var/run/dnsmasq.pid\n");
112 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
113 if (*nv) fprintf(f, "domain=%s\n", nv);
116 // dns
117 const dns_list_t *dns = get_dns(); // this always points to a static buffer
119 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
120 else n = 4096;
121 fprintf(f,
122 "resolv-file=%s\n" // the real stuff is here
123 "addn-hosts=%s\n" // directory with additional hosts files
124 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
125 "expand-hosts\n" // expand hostnames in hosts file
126 "min-port=%u\n", // min port used for random src port
127 dmresolv, dmhosts, dmdhcp, n);
128 do_dns = nvram_match("dhcpd_dmdns", "1");
130 // DNS rebinding protection, will discard upstream RFC1918 responses
131 if (nvram_get_int("dns_norebind")) {
132 fprintf(f,
133 "stop-dns-rebind\n"
134 "rebind-localhost-ok\n");
135 // allow RFC1918 responses for server domain
136 switch (get_wan_proto()) {
137 case WP_PPTP:
138 nv = nvram_get("pptp_server_ip");
139 break;
140 case WP_L2TP:
141 nv = nvram_get("l2tp_server_ip");
142 break;
143 default:
144 nv = NULL;
145 break;
147 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
150 for (n = 0 ; n < dns->count; ++n) {
151 if (dns->dns[n].port != 53) {
152 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
156 if (nvram_get_int("dhcpd_static_only")) {
157 fprintf(f, "dhcp-ignore=tag:!known\n");
160 if ((n = nvram_get_int("dnsmasq_q"))) { //process quiet flags
161 if (n & 1) fprintf(f, "quiet-dhcp\n");
162 if (n & 2) fprintf(f, "quiet-dhcp6\n");
163 if (n & 4) fprintf(f, "quiet-ra\n");
166 // dhcp
167 do_dhcpd_hosts=0;
168 char lanN_proto[] = "lanXX_proto";
169 char lanN_ifname[] = "lanXX_ifname";
170 char lanN_ipaddr[] = "lanXX_ipaddr";
171 char lanN_netmask[] = "lanXX_netmask";
172 char dhcpdN_startip[] = "dhcpdXX_startip";
173 char dhcpdN_endip[] = "dhcpdXX_endip";
174 char dhcpN_start[] = "dhcpXX_start";
175 char dhcpN_num[] = "dhcpXX_num";
176 char dhcpN_lease[] = "dhcpXX_lease";
177 char br;
178 for(br=0 ; br<=3 ; br++) {
179 char bridge[2] = "0";
180 if (br!=0)
181 bridge[0]+=br;
182 else
183 strcpy(bridge, "");
185 sprintf(lanN_proto, "lan%s_proto", bridge);
186 sprintf(lanN_ifname, "lan%s_ifname", bridge);
187 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
188 do_dhcpd = nvram_match(lanN_proto, "dhcp");
189 if (do_dhcpd) {
190 do_dhcpd_hosts++;
192 router_ip = nvram_safe_get(lanN_ipaddr);
193 strlcpy(lan, router_ip, sizeof(lan));
194 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
196 fprintf(f,
197 "interface=%s\n",
198 nvram_safe_get(lanN_ifname));
200 sprintf(dhcpN_lease, "dhcp%s_lease", bridge);
201 dhcp_lease = nvram_get_int(dhcpN_lease);
203 if (dhcp_lease <= 0) dhcp_lease = 1440;
205 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
206 if (n < 0) strcpy(sdhcp_lease, "infinite");
207 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
209 if (!do_dns) {
210 // if not using dnsmasq for dns
212 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
213 // no DNS might be temporary. use a low lease time to force clients to update.
214 dhcp_lease = 2;
215 strcpy(sdhcp_lease, "2m");
216 do_dns = 1;
218 else {
219 // pass the dns directly
220 buf[0] = 0;
221 for (n = 0 ; n < dns->count; ++n) {
222 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
223 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
226 fprintf(f, "dhcp-option=tag:%s,6%s\n", nvram_safe_get(lanN_ifname), buf);
230 sprintf(dhcpdN_startip, "dhcpd%s_startip", bridge);
231 sprintf(dhcpdN_endip, "dhcpd%s_endip", bridge);
232 sprintf(lanN_netmask, "lan%s_netmask", bridge);
234 if ((p = nvram_get(dhcpdN_startip)) && (*p) && (e = nvram_get(dhcpdN_endip)) && (*e)) {
235 fprintf(f, "dhcp-range=tag:%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname), p, e, nvram_safe_get(lanN_netmask), dhcp_lease);
237 else {
238 // for compatibility
239 sprintf(dhcpN_start, "dhcp%s_start", bridge);
240 sprintf(dhcpN_num, "dhcp%s_num", bridge);
241 sprintf(lanN_netmask, "lan%s_netmask", bridge);
242 dhcp_start = nvram_get_int(dhcpN_start);
243 dhcp_count = nvram_get_int(dhcpN_num);
244 fprintf(f, "dhcp-range=tag:%s,%s%d,%s%d,%s,%dm\n",
245 nvram_safe_get(lanN_ifname), lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(lanN_netmask), dhcp_lease);
248 nv = nvram_safe_get(lanN_ipaddr);
249 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
250 p = nvram_safe_get("lan_gateway");
251 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
253 #ifdef TCONFIG_VLAN
254 fprintf(f,
255 "dhcp-option=tag:%s,3,%s\n", // gateway
256 nvram_safe_get(lanN_ifname), nv);
257 #endif
258 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
259 fprintf(f, "dhcp-option=tag:%s,44,%s\n", nvram_safe_get(lanN_ifname), nv);
261 #ifdef TCONFIG_SAMBASRV
262 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
263 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
264 // Samba will serve as a WINS server
265 fprintf(f, "dhcp-option=tag:%s,44,%s\n", nvram_safe_get(lanN_ifname), nvram_safe_get(lanN_ipaddr));
268 #endif
269 } else {
270 if (strcmp(nvram_safe_get(lanN_ifname),"")!=0) {
271 fprintf(f, "interface=%s\n", nvram_safe_get(lanN_ifname));
272 // if no dhcp range is set then no dhcp service will be offered so following
273 // line is superflous.
274 // fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname));
278 // write static lease entries & create hosts file
280 mkdir_if_none(dmhosts);
281 snprintf(buf, sizeof(buf), "%s/hosts", dmhosts);
282 if ((hf = fopen(buf, "w")) != NULL) {
283 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
284 fprintf(hf, "%s %s\n", router_ip, nv);
285 #ifdef TCONFIG_SAMBASRV
286 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
287 fprintf(hf, "%s %s\n", router_ip, nv);
288 #endif
289 p = (char *)get_wanip();
290 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
291 p = "127.0.0.1";
292 fprintf(hf, "%s wan-ip\n", p);
293 if (nv && (*nv))
294 fprintf(hf, "%s %s-wan\n", p, nv);
298 mkdir_if_none(dmdhcp);
299 snprintf(buf, sizeof(buf), "%s/dhcp-hosts", dmdhcp);
300 df = fopen(buf, "w");
302 // PREVIOUS/OLD FORMAT:
303 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 73 w/ delim
304 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
305 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 103 w/ delim
307 // NEW FORMAT (+static ARP binding flag after hostname):
308 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 75 w/ delim
309 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
310 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 105 w/ delim
312 p = nvram_safe_get("dhcpd_static");
313 while ((e = strchr(p, '>')) != NULL) {
314 n = (e - p);
315 if (n > 104) {
316 p = e + 1;
317 continue;
320 strncpy(buf, p, n);
321 buf[n] = 0;
322 p = e + 1;
324 if ((e = strchr(buf, '<')) == NULL) continue;
325 *e = 0;
326 mac = buf;
328 ip = e + 1;
329 if ((e = strchr(ip, '<')) == NULL) continue;
330 *e = 0;
331 if (strchr(ip, '.') == NULL) {
332 ipn = atoi(ip);
333 if ((ipn <= 0) || (ipn > 255)) continue;
334 sprintf(ipbuf, "%s%d", lan, ipn);
335 ip = ipbuf;
337 else {
338 if (inet_addr(ip) == INADDR_NONE) continue;
341 name = e + 1;
343 if ((e = strchr(name, '<')) != NULL) {
344 *e = 0;
347 if ((hf) && (*name != 0)) {
348 fprintf(hf, "%s %s\n", ip, name);
351 if ((do_dhcpd_hosts > 0) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
352 char static_dhcp_lease[32];
353 strcpy(static_dhcp_lease, "");
354 if (nvram_get_int("dhcpd_slt") != 0)
355 sprintf(static_dhcp_lease, ",%s", sdhcp_lease);
356 if (df)
357 fprintf(df, "%s,%s%s\n", mac, ip, static_dhcp_lease);
358 else
359 fprintf(f, "dhcp-host=%s,%s%s\n", mac, ip, static_dhcp_lease);
363 if (df) fclose(df);
364 if (hf) fclose(hf);
366 n = nvram_get_int("dhcpd_lmax");
367 fprintf(f,
368 "dhcp-lease-max=%d\n",
369 (n > 0) ? n : 255);
370 if (nvram_get_int("dhcpd_auth") >= 0) {
371 fprintf(f, "dhcp-authoritative\n");
376 #ifdef TCONFIG_OPENVPN
377 write_vpn_dnsmasq_config(f);
378 #endif
380 #ifdef TCONFIG_PPTPD
381 write_pptpd_dnsmasq_config(f);
382 #endif
384 #ifdef TCONFIG_IPV6
385 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
386 service = get_ipv6_service();
387 do_6to4 = (service == IPV6_ANYCAST_6TO4);
388 do_6rd = (service == IPV6_6RD || service == IPV6_6RD_DHCP);
389 mtu = NULL;
391 switch (service) {
392 case IPV6_NATIVE_DHCP:
393 case IPV6_ANYCAST_6TO4:
394 case IPV6_6IN4:
395 case IPV6_6RD:
396 case IPV6_6RD_DHCP:
397 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
398 // fall through
399 default:
400 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
401 break;
403 if (!(*prefix)) prefix = "::";
404 // ipv6 = (char *)ipv6_router_address(NULL);
406 fprintf(f, "enable-ra\ndhcp-range=tag:br0,%s, slaac, ra-names, 64\n", prefix);
409 #endif
411 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
413 fappend(f, "/etc/dnsmasq.custom");
417 fclose(f);
419 if (do_dns) {
420 unlink("/etc/resolv.conf");
421 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
424 TRACE_PT("run dnsmasq\n");
426 // Default to some values we like, but allow the user to override them.
427 eval("dnsmasq", "-c", "1500", "--log-async");
429 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
430 pid_dnsmasq = -2;
433 TRACE_PT("end\n");
436 void stop_dnsmasq(void)
438 TRACE_PT("begin\n");
440 if (getpid() != 1) {
441 stop_service("dnsmasq");
442 return;
445 pid_dnsmasq = -1;
447 unlink("/etc/resolv.conf");
448 symlink(dmresolv, "/etc/resolv.conf");
450 killall_tk("dnsmasq");
452 TRACE_PT("end\n");
455 void clear_resolv(void)
457 f_write(dmresolv, NULL, 0, 0, 0); // blank
460 #ifdef TCONFIG_IPV6
461 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
463 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
464 struct in6_addr addr;
465 int cnt = 0;
467 foreach(p, dns, next) {
468 // verify that this is a valid IPv6 address
469 if (inet_pton(AF_INET6, p, &addr) == 1) {
470 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
471 ++cnt;
475 return cnt;
477 #endif
479 void dns_to_resolv(void)
481 FILE *f;
482 const dns_list_t *dns;
483 int i;
484 mode_t m;
486 m = umask(022); // 077 from pppoecd
487 if ((f = fopen(dmresolv, "w")) != NULL) {
488 // Check for VPN DNS entries
489 if (!write_pptpvpn_resolv(f) && !write_vpn_resolv(f)) {
490 #ifdef TCONFIG_IPV6
491 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
492 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
493 #endif
494 dns = get_dns(); // static buffer
495 if (dns->count == 0) {
496 // Put a pseudo DNS IP to trigger Connect On Demand
497 if (nvram_match("ppp_demand", "1")) {
498 switch (get_wan_proto()) {
499 case WP_PPPOE:
500 case WP_PPP3G:
501 case WP_PPTP:
502 case WP_L2TP:
503 fprintf(f, "nameserver 1.1.1.1\n");
504 break;
508 else {
509 for (i = 0; i < dns->count; i++) {
510 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
511 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
516 fclose(f);
518 umask(m);
521 // -----------------------------------------------------------------------------
523 void start_httpd(void)
525 if (getpid() != 1) {
526 start_service("httpd");
527 return;
530 stop_httpd();
531 chdir("/www");
532 eval("httpd");
533 chdir("/");
536 void stop_httpd(void)
538 if (getpid() != 1) {
539 stop_service("httpd");
540 return;
543 killall_tk("httpd");
546 // -----------------------------------------------------------------------------
547 #ifdef TCONFIG_IPV6
549 static void add_ip6_lanaddr(void)
551 char ip[INET6_ADDRSTRLEN + 4];
552 const char *p;
554 p = ipv6_router_address(NULL);
555 if (*p) {
556 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
557 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
561 void start_ipv6_tunnel(void)
563 char ip[INET6_ADDRSTRLEN + 4];
564 struct in_addr addr4;
565 struct in6_addr addr;
566 const char *wanip, *mtu, *tun_dev;
567 int service;
569 service = get_ipv6_service();
570 tun_dev = get_wan6face();
571 wanip = get_wanip();
572 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
573 modprobe("sit");
575 if (service == IPV6_ANYCAST_6TO4)
576 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
577 else
578 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
579 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
580 "remote", ip,
581 "local", (char *)wanip,
582 "ttl", nvram_safe_get("ipv6_tun_ttl"));
584 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
585 nvram_set("ipv6_ifname", (char *)tun_dev);
587 if (service == IPV6_ANYCAST_6TO4) {
588 add_ip6_lanaddr();
589 addr4.s_addr = 0;
590 memset(&addr, 0, sizeof(addr));
591 inet_aton(wanip, &addr4);
592 addr.s6_addr16[0] = htons(0x2002);
593 ipv6_mapaddr4(&addr, 16, &addr4, 0);
594 addr.s6_addr16[7] = htons(0x0001);
595 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
596 strncat(ip, "/16", sizeof(ip));
598 else {
599 snprintf(ip, sizeof(ip), "%s/%d",
600 nvram_safe_get("ipv6_tun_addr"),
601 nvram_get_int("ipv6_tun_addrlen") ? : 64);
603 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
604 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
606 // (re)start radvd - now dnsmasq provided
607 if (service == IPV6_ANYCAST_6TO4)
608 start_dnsmasq();
611 void stop_ipv6_tunnel(void)
613 eval("ip", "tunnel", "del", (char *)get_wan6face());
614 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
615 // get rid of old IPv6 address from lan iface
616 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
618 modprobe_r("sit");
621 void start_6rd_tunnel(void)
623 const char *tun_dev, *wanip;
624 int service, mask_len, prefix_len, local_prefix_len;
625 char mtu[10], prefix[INET6_ADDRSTRLEN], relay[INET_ADDRSTRLEN];
626 struct in_addr netmask_addr, relay_addr, relay_prefix_addr, wanip_addr;
627 struct in6_addr prefix_addr, local_prefix_addr;
628 char local_prefix[INET6_ADDRSTRLEN];
629 char tmp_ipv6[INET6_ADDRSTRLEN + 4], tmp_ipv4[INET_ADDRSTRLEN + 4];
630 char tmp[256];
631 FILE *f;
633 service = get_ipv6_service();
634 wanip = get_wanip();
635 tun_dev = get_wan6face();
636 sprintf(mtu, "%d", (nvram_get_int("wan_mtu") > 0) ? (nvram_get_int("wan_mtu") - 20) : 1280);
638 // maybe we can merge the ipv6_6rd_* variables into a single ipv_6rd_string (ala wan_6rd)
639 // to save nvram space?
640 if (service == IPV6_6RD) {
641 _dprintf("starting 6rd tunnel using manual settings.\n");
642 mask_len = nvram_get_int("ipv6_6rd_ipv4masklen");
643 prefix_len = nvram_get_int("ipv6_6rd_prefix_length");
644 strcpy(prefix, nvram_safe_get("ipv6_6rd_prefix"));
645 strcpy(relay, nvram_safe_get("ipv6_6rd_borderrelay"));
647 else {
648 _dprintf("starting 6rd tunnel using automatic settings.\n");
649 char *wan_6rd = nvram_safe_get("wan_6rd");
650 if (sscanf(wan_6rd, "%d %d %s %s", &mask_len, &prefix_len, prefix, relay) < 4) {
651 _dprintf("wan_6rd string is missing or invalid (%s)\n", wan_6rd);
652 return;
656 // validate values that were passed
657 if (mask_len < 0 || mask_len > 32) {
658 _dprintf("invalid mask_len value (%d)\n", mask_len);
659 return;
661 if (prefix_len < 0 || prefix_len > 128) {
662 _dprintf("invalid prefix_len value (%d)\n", prefix_len);
663 return;
665 if ((32 - mask_len) + prefix_len > 128) {
666 _dprintf("invalid combination of mask_len and prefix_len!\n");
667 return;
670 sprintf(tmp, "ping -q -c 2 %s | grep packet", relay);
671 if ((f = popen(tmp, "r")) == NULL) {
672 _dprintf("error obtaining data\n");
673 return;
675 fgets(tmp, sizeof(tmp), f);
676 pclose(f);
677 if (strstr(tmp, " 0% packet loss") == NULL) {
678 _dprintf("failed to ping border relay\n");
679 return;
682 // get relay prefix from border relay address and mask
683 netmask_addr.s_addr = htonl(0xffffffff << (32 - mask_len));
684 inet_aton(relay, &relay_addr);
685 relay_prefix_addr.s_addr = relay_addr.s_addr & netmask_addr.s_addr;
687 // calculate the local prefix
688 inet_pton(AF_INET6, prefix, &prefix_addr);
689 inet_pton(AF_INET, wanip, &wanip_addr);
690 if (calc_6rd_local_prefix(&prefix_addr, prefix_len, mask_len,
691 &wanip_addr, &local_prefix_addr, &local_prefix_len) == 0) {
692 _dprintf("error calculating local prefix.");
693 return;
695 inet_ntop(AF_INET6, &local_prefix_addr, local_prefix, sizeof(local_prefix));
697 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1", local_prefix);
698 nvram_set("ipv6_rtr_addr", tmp_ipv6);
699 nvram_set("ipv6_prefix", local_prefix);
701 // load sit module needed for the 6rd tunnel
702 modprobe("sit");
704 // creating the 6rd tunnel
705 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit", "local", (char *)wanip, "ttl", nvram_safe_get("ipv6_tun_ttl"));
707 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s/%d", prefix, prefix_len);
708 snprintf(tmp_ipv4, sizeof(tmp_ipv4), "%s/%d", inet_ntoa(relay_prefix_addr), mask_len);
709 eval("ip", "tunnel" "6rd", "dev", (char *)tun_dev, "6rd-prefix", tmp_ipv6, "6rd-relay_prefix", tmp_ipv4);
711 // bringing up the link
712 eval("ip", "link", "set", "dev", (char *)tun_dev, "mtu", (char *)mtu, "up");
714 // setting the WAN address Note: IPv6 WAN CIDR should be: ((32 - ip6rd_ipv4masklen) + ip6rd_prefixlen)
715 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, local_prefix_len);
716 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", (char *)tun_dev);
718 // setting the LAN address Note: IPv6 LAN CIDR should be 64
719 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, nvram_get_int("ipv6_prefix_length") ? : 64);
720 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", nvram_safe_get("lan_ifname"));
722 // adding default route via the border relay
723 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "::%s", relay);
724 eval("ip", "-6", "route", "add", "default", "via", tmp_ipv6, "dev", (char *)tun_dev);
726 nvram_set("ipv6_ifname", (char *)tun_dev);
728 // (re)start radvd
729 start_radvd();
731 printf("6rd end\n");
734 void stop_6rd_tunnel(void)
736 eval("ip", "tunnel", "del", (char *)get_wan6face());
737 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
738 modprobe_r("sit");
741 static pid_t pid_radvd = -1;
743 void start_radvd(void)
745 FILE *f;
746 char *prefix, *ip, *mtu;
747 int do_dns, do_6to4, do_6rd;
748 char *argv[] = { "radvd", NULL, NULL, NULL };
749 int pid, argc, service, cnt;
751 if (getpid() != 1) {
752 start_service("radvd");
753 return;
756 stop_radvd();
758 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
759 service = get_ipv6_service();
760 do_6to4 = (service == IPV6_ANYCAST_6TO4);
761 do_6rd = (service == IPV6_6RD || service == IPV6_6RD_DHCP);
762 mtu = NULL;
764 switch (service) {
765 case IPV6_NATIVE_DHCP:
766 prefix = "::";
767 break;
768 case IPV6_ANYCAST_6TO4:
769 case IPV6_6IN4:
770 case IPV6_6RD:
771 case IPV6_6RD_DHCP:
772 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
773 // fall through
774 default:
775 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
776 break;
778 if (!(*prefix)) prefix = "::";
780 // Create radvd.conf
781 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
783 ip = (char *)ipv6_router_address(NULL);
784 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
786 fprintf(f,
787 "interface %s\n"
788 "{\n"
789 " IgnoreIfMissing on;\n"
790 " AdvSendAdvert on;\n"
791 " MaxRtrAdvInterval 60;\n"
792 " AdvHomeAgentFlag off;\n"
793 " AdvManagedFlag off;\n"
794 "%s%s%s"
795 " prefix %s/64 \n"
796 " {\n"
797 " AdvOnLink on;\n"
798 " AdvAutonomous on;\n"
799 "%s"
800 "%s%s%s"
801 " };\n",
802 nvram_safe_get("lan_ifname"),
803 mtu ? " AdvLinkMTU " : "", mtu ? : "", mtu ? ";\n" : "",
804 prefix,
805 (do_6to4 || do_6rd) ? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
806 do_6to4 ? " Base6to4Interface " : "",
807 do_6to4 ? get_wanface() : "",
808 do_6to4 ? ";\n" : "");
810 if (do_dns) {
811 fprintf(f, " RDNSS %s {};\n", ip);
813 else {
814 cnt = write_ipv6_dns_servers(f, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
815 if (cnt == 0 || nvram_get_int("dns_addget"))
816 cnt += write_ipv6_dns_servers(f, (cnt) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
817 if (cnt) fprintf(f, "{};\n");
820 fprintf(f,
821 "};\n"); // close "interface" section
822 fclose(f);
824 // Start radvd
825 argc = 1;
826 if (nvram_get_int("debug_ipv6")) {
827 argv[argc++] = "-d";
828 argv[argc++] = "10";
830 argv[argc] = NULL;
831 _eval(argv, NULL, 0, &pid);
833 if (!nvram_contains_word("debug_norestart", "radvd")) {
834 pid_radvd = -2;
839 void stop_radvd(void)
841 if (getpid() != 1) {
842 stop_service("radvd");
843 return;
846 pid_radvd = -1;
847 killall_tk("radvd");
850 void start_ipv6(void)
852 int service;
854 service = get_ipv6_service();
855 enable_ip6_forward();
857 // Check if turned on
858 switch (service) {
859 case IPV6_NATIVE:
860 case IPV6_6IN4:
861 case IPV6_MANUAL:
862 add_ip6_lanaddr();
863 break;
864 case IPV6_NATIVE_DHCP:
865 case IPV6_ANYCAST_6TO4:
866 nvram_set("ipv6_rtr_addr", "");
867 nvram_set("ipv6_prefix", "");
868 break;
871 if (service != IPV6_DISABLED) {
872 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
873 accept_ra(nvram_safe_get("lan_ifname"));
877 void stop_ipv6(void)
879 stop_ipv6_tunnel();
880 stop_dhcp6c();
881 eval("ip", "-6", "addr", "flush", "scope", "global");
884 #endif
886 // -----------------------------------------------------------------------------
888 void start_upnp(void)
890 if (getpid() != 1) {
891 start_service("upnp");
892 return;
895 if (get_wan_proto() == WP_DISABLED) return;
897 int enable;
898 FILE *f;
899 int upnp_port;
901 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
902 mkdir("/etc/upnp", 0777);
903 if (f_exists("/etc/upnp/config.alt")) {
904 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
906 else {
907 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
908 upnp_port = nvram_get_int("upnp_port");
909 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
912 fprintf(f,
913 "ext_ifname=%s\n"
914 "port=%d\n"
915 "enable_upnp=%s\n"
916 "enable_natpmp=%s\n"
917 "secure_mode=%s\n"
918 "upnp_forward_chain=upnp\n"
919 "upnp_nat_chain=upnp\n"
920 "notify_interval=%d\n"
921 "system_uptime=yes\n"
922 "\n"
924 get_wanface(),
925 upnp_port,
926 (enable & 1) ? "yes" : "no", // upnp enable
927 (enable & 2) ? "yes" : "no", // natpmp enable
928 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
929 nvram_get_int("upnp_ssdp_interval")
932 if (nvram_get_int("upnp_clean")) {
933 int interval = nvram_get_int("upnp_clean_interval");
934 if (interval < 60) interval = 60;
935 fprintf(f,
936 "clean_ruleset_interval=%d\n"
937 "clean_ruleset_threshold=%d\n",
938 interval,
939 nvram_get_int("upnp_clean_threshold")
942 else
943 fprintf(f,"clean_ruleset_interval=0\n");
945 if (nvram_match("upnp_mnp", "1")) {
946 int https = nvram_get_int("https_enable");
947 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
948 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
949 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
951 else {
952 // Empty parameters are not included into XML service description
953 fprintf(f, "presentation_url=\n");
956 char uuid[45];
957 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
958 fprintf(f, "uuid=%s\n", uuid);
960 #ifdef TCONFIG_VLAN
961 char lanN_ipaddr[] = "lanXX_ipaddr";
962 char lanN_netmask[] = "lanXX_netmask";
963 char upnp_lanN[] = "upnp_lanXX";
964 char br;
966 for(br=0 ; br<4 ; br++) {
967 char bridge[2] = "0";
968 if (br!=0)
969 bridge[0]+=br;
970 else
971 strcpy(bridge, "");
973 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
974 sprintf(lanN_netmask, "lan%s_netmask", bridge);
975 sprintf(upnp_lanN, "upnp_lan%s", bridge);
977 char *lanip = nvram_safe_get(lanN_ipaddr);
978 char *lanmask = nvram_safe_get(lanN_netmask);
979 char *lanlisten = nvram_safe_get(upnp_lanN);
981 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
982 #else
983 char *lanip = nvram_safe_get("lan_ipaddr");
984 char *lanmask = nvram_safe_get("lan_netmask");
985 #endif
986 fprintf(f,
987 "listening_ip=%s/%s\n",
988 lanip, lanmask);
989 int ports[4];
990 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
991 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
992 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
993 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
994 fprintf(f,
995 "allow %d-%d %s/%s %d-%d\n",
996 ports[0], ports[1],
997 lanip, lanmask,
998 ports[2], ports[3]
1001 else {
1002 // by default allow only redirection of ports above 1024
1003 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
1005 #ifdef TCONFIG_VLAN
1008 #endif
1010 fappend(f, "/jffs/upnpconfig.custom");
1011 fappend(f, "/etc/upnp/config.custom");
1012 fprintf(f, "%s\n", nvram_safe_get("upnp_custom"));
1013 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
1015 fclose(f);
1017 xstart("miniupnpd", "-f", "/etc/upnp/config");
1023 void stop_upnp(void)
1025 if (getpid() != 1) {
1026 stop_service("upnp");
1027 return;
1030 killall_tk("miniupnpd");
1033 // -----------------------------------------------------------------------------
1035 static pid_t pid_crond = -1;
1037 void start_cron(void)
1039 stop_cron();
1041 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
1042 if (!nvram_contains_word("debug_norestart", "crond")) {
1043 pid_crond = -2;
1047 void stop_cron(void)
1049 pid_crond = -1;
1050 killall_tk("crond");
1053 // -----------------------------------------------------------------------------
1054 #ifdef LINUX26
1056 static pid_t pid_hotplug2 = -1;
1058 void start_hotplug2()
1060 stop_hotplug2();
1062 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
1063 xstart("hotplug2", "--persistent", "--no-coldplug");
1064 // FIXME: Don't remember exactly why I put "sleep" here -
1065 // but it was not for a race with check_services()... - TB
1066 sleep(1);
1068 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
1069 pid_hotplug2 = -2;
1073 void stop_hotplug2(void)
1075 pid_hotplug2 = -1;
1076 killall_tk("hotplug2");
1079 #endif /* LINUX26 */
1080 // -----------------------------------------------------------------------------
1082 // Written by Sparq in 2002/07/16
1083 void start_zebra(void)
1085 #ifdef TCONFIG_ZEBRA
1086 if (getpid() != 1) {
1087 start_service("zebra");
1088 return;
1091 FILE *fp;
1093 char *lan_tx = nvram_safe_get("dr_lan_tx");
1094 char *lan_rx = nvram_safe_get("dr_lan_rx");
1095 #ifdef TCONFIG_VLAN
1096 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
1097 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
1098 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
1099 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
1100 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
1101 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
1102 #endif
1103 char *wan_tx = nvram_safe_get("dr_wan_tx");
1104 char *wan_rx = nvram_safe_get("dr_wan_rx");
1106 #ifdef TCONFIG_VLAN
1107 if ((*lan_tx == '0') && (*lan_rx == '0') &&
1108 (*lan1_tx == '0') && (*lan1_rx == '0') &&
1109 (*lan2_tx == '0') && (*lan2_rx == '0') &&
1110 (*lan3_tx == '0') && (*lan3_rx == '0') &&
1111 (*wan_tx == '0') && (*wan_rx == '0')) {
1112 #else
1113 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
1114 #endif
1115 return;
1118 // empty
1119 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
1120 fclose(fp);
1124 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
1125 char *lan_ifname = nvram_safe_get("lan_ifname");
1126 #ifdef TCONFIG_VLAN
1127 char *lan1_ifname = nvram_safe_get("lan1_ifname");
1128 char *lan2_ifname = nvram_safe_get("lan2_ifname");
1129 char *lan3_ifname = nvram_safe_get("lan3_ifname");
1130 #endif
1131 char *wan_ifname = nvram_safe_get("wan_ifname");
1133 fprintf(fp, "router rip\n");
1134 if(strcmp(lan_ifname,"")!=0)
1135 fprintf(fp, "network %s\n", lan_ifname);
1136 #ifdef TCONFIG_VLAN
1137 if(strcmp(lan1_ifname,"")!=0)
1138 fprintf(fp, "network %s\n", lan1_ifname);
1139 if(strcmp(lan2_ifname,"")!=0)
1140 fprintf(fp, "network %s\n", lan2_ifname);
1141 if(strcmp(lan3_ifname,"")!=0)
1142 fprintf(fp, "network %s\n", lan3_ifname);
1143 #endif
1144 fprintf(fp, "network %s\n", wan_ifname);
1145 fprintf(fp, "redistribute connected\n");
1146 //fprintf(fp, "redistribute static\n");
1148 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
1149 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
1151 if(strcmp(lan_ifname,"")!=0) {
1152 fprintf(fp, "interface %s\n", lan_ifname);
1153 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
1154 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
1156 #ifdef TCONFIG_VLAN
1157 if(strcmp(lan1_ifname,"")!=0) {
1158 fprintf(fp, "interface %s\n", lan1_ifname);
1159 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
1160 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
1162 if(strcmp(lan2_ifname,"")!=0) {
1163 fprintf(fp, "interface %s\n", lan2_ifname);
1164 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
1165 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
1167 if(strcmp(lan3_ifname,"")!=0) {
1168 fprintf(fp, "interface %s\n", lan3_ifname);
1169 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
1170 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
1172 #endif
1173 fprintf(fp, "interface %s\n", wan_ifname);
1174 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
1175 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
1177 fprintf(fp, "router rip\n");
1178 if(strcmp(lan_ifname,"")!=0) {
1179 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
1180 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
1182 #ifdef TCONFIG_VLAN
1183 if(strcmp(lan1_ifname,"")!=0) {
1184 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
1185 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
1187 if(strcmp(lan2_ifname,"")!=0) {
1188 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
1189 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
1191 if(strcmp(lan3_ifname,"")!=0) {
1192 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
1193 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
1195 #endif
1196 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
1197 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
1198 fprintf(fp, "access-list private deny any\n");
1200 //fprintf(fp, "debug rip events\n");
1201 //fprintf(fp, "log file /etc/ripd.log\n");
1202 fclose(fp);
1205 xstart("zebra", "-d");
1206 xstart("ripd", "-d");
1207 #endif
1210 void stop_zebra(void)
1212 #ifdef TCONFIG_ZEBRA
1213 if (getpid() != 1) {
1214 stop_service("zebra");
1215 return;
1218 killall("zebra", SIGTERM);
1219 killall("ripd", SIGTERM);
1221 unlink("/etc/zebra.conf");
1222 unlink("/etc/ripd.conf");
1223 #endif
1226 // -----------------------------------------------------------------------------
1228 void start_syslog(void)
1230 char *argv[16];
1231 int argc;
1232 char *nv;
1233 char *b_opt = "";
1234 char rem[256];
1235 int n;
1236 char s[64];
1237 char cfg[256];
1238 char *rot_siz = "50";
1239 char *rot_keep = "1";
1240 char *log_file_path;
1242 argv[0] = "syslogd";
1243 argc = 1;
1245 if (nvram_match("log_remote", "1")) {
1246 nv = nvram_safe_get("log_remoteip");
1247 if (*nv) {
1248 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1249 argv[argc++] = "-R";
1250 argv[argc++] = rem;
1254 if (nvram_match("log_file", "1")) {
1255 argv[argc++] = "-L";
1257 if (strcmp(nvram_safe_get("log_file_size"), "") != 0) {
1258 rot_siz = nvram_safe_get("log_file_size");
1260 if (nvram_get_int("log_file_size") > 0) {
1261 rot_keep = nvram_safe_get("log_file_keep");
1264 // log to custom path - shibby
1265 if (nvram_match("log_file_custom", "1")) {
1266 log_file_path = nvram_safe_get("log_file_path");
1267 argv[argc++] = "-s";
1268 argv[argc++] = rot_siz;
1269 argv[argc++] = "-O";
1270 argv[argc++] = log_file_path;
1271 if (strcmp(nvram_safe_get("log_file_path"), "/var/log/messages") != 0) {
1272 remove("/var/log/messages");
1273 symlink(log_file_path, "/var/log/messages");
1276 else
1278 /* Read options: rotate_size(kb) num_backups logfilename.
1279 * Ignore these settings and use defaults if the logfile cannot be written to.
1281 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1282 if ((nv = strchr(cfg, '\n')))
1283 *nv = 0;
1285 if ((nv = strtok(cfg, " \t"))) {
1286 if (isdigit(*nv))
1287 rot_siz = nv;
1290 if ((nv = strtok(NULL, " \t")))
1291 b_opt = nv;
1293 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1294 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1295 argv[argc++] = "-O";
1296 argv[argc++] = nv;
1298 else {
1299 rot_siz = "50";
1300 b_opt = "";
1305 if (nvram_match("log_file_custom", "0")) {
1306 argv[argc++] = "-s";
1307 argv[argc++] = rot_siz;
1308 struct stat sb;
1309 if (lstat("/var/log/messages", &sb) != -1)
1310 if (S_ISLNK(sb.st_mode))
1311 remove("/var/log/messages");
1314 if (isdigit(*b_opt)) {
1315 argv[argc++] = "-b";
1316 argv[argc++] = b_opt;
1317 } else
1318 if (nvram_get_int("log_file_size") > 0) {
1319 argv[argc++] = "-b";
1320 argv[argc++] = rot_keep;
1324 if (argc > 1) {
1325 argv[argc] = NULL;
1326 _eval(argv, NULL, 0, NULL);
1328 argv[0] = "klogd";
1329 argv[1] = NULL;
1330 _eval(argv, NULL, 0, NULL);
1332 // used to be available in syslogd -m
1333 n = nvram_get_int("log_mark");
1334 if (n > 0) {
1335 // n is in minutes
1336 if (n < 60)
1337 sprintf(rem, "*/%d * * * *", n);
1338 else if (n < 60 * 24)
1339 sprintf(rem, "0 */%d * * *", n / 60);
1340 else
1341 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1342 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1343 eval("cru", "a", "syslogdmark", s);
1345 else {
1346 eval("cru", "d", "syslogdmark");
1351 void stop_syslog(void)
1353 killall("klogd", SIGTERM);
1354 killall("syslogd", SIGTERM);
1357 // -----------------------------------------------------------------------------
1359 static pid_t pid_igmp = -1;
1361 void start_igmp_proxy(void)
1363 FILE *fp;
1365 pid_igmp = -1;
1366 if (nvram_match("multicast_pass", "1")) {
1367 if (get_wan_proto() == WP_DISABLED)
1368 return;
1370 if (f_exists("/etc/igmp.alt")) {
1371 eval("igmpproxy", "/etc/igmp.alt");
1373 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1374 fprintf(fp,
1375 "quickleave\n"
1376 "phyint %s upstream\n"
1377 "\taltnet %s\n",
1378 // "phyint %s downstream ratelimit 0\n",
1379 // get_wanface(),
1380 nvram_safe_get("wan_ifname"),
1381 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1382 // nvram_safe_get("lan_ifname"));
1384 #ifdef TCONFIG_VLAN
1385 char lanN_ifname[] = "lanXX_ifname";
1386 char multicast_lanN[] = "multicast_lanXX";
1387 char br;
1389 for(br=0 ; br<4 ; br++) {
1390 char bridge[2] = "0";
1391 if (br!=0)
1392 bridge[0]+=br;
1393 else
1394 strcpy(bridge, "");
1396 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1397 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1399 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1400 fprintf(fp,
1401 "phyint %s downstream ratelimit 0\n",
1402 nvram_safe_get(lanN_ifname));
1405 #else
1406 fprintf(fp,
1407 "phyint %s downstream ratelimit 0\n",
1408 nvram_safe_get("lan_ifname"));
1409 #endif
1410 fclose(fp);
1411 eval("igmpproxy", "/etc/igmp.conf");
1413 else {
1414 return;
1416 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1417 pid_igmp = -2;
1422 void stop_igmp_proxy(void)
1424 pid_igmp = -1;
1425 killall_tk("igmpproxy");
1428 // -----------------------------------------------------------------------------
1430 void start_udpxy(void)
1432 if (nvram_match("udpxy_enable", "1")) {
1433 if (get_wan_proto() == WP_DISABLED)
1434 return;
1435 eval("udpxy", (nvram_get_int("udpxy_stats") ? "-S" : ""), "-p", nvram_safe_get("udpxy_port"), "-c", nvram_safe_get("udpxy_clients"), "-m", nvram_safe_get("wan_ifname") );
1439 void stop_udpxy(void)
1441 killall_tk("udpxy");
1444 // -----------------------------------------------------------------------------
1446 #ifdef TCONFIG_NOCAT
1448 static pid_t pid_splashd = -1;
1449 void start_splashd(void)
1451 pid_splashd = -1;
1452 start_nocat();
1453 if (!nvram_contains_word("debug_norestart", "splashd")) {
1454 pid_splashd = -2;
1458 void stop_splashd(void)
1460 pid_splashd = -1;
1461 stop_nocat();
1462 start_wan(BOOT);
1464 #endif
1466 // -----------------------------------------------------------------------------
1468 void set_tz(void)
1470 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1473 void start_ntpc(void)
1475 set_tz();
1477 stop_ntpc();
1479 if (nvram_get_int("ntp_updates") >= 0) {
1480 xstart("ntpsync", "--init");
1484 void stop_ntpc(void)
1486 killall("ntpsync", SIGTERM);
1489 // -----------------------------------------------------------------------------
1491 static void stop_rstats(void)
1493 int n, m;
1494 int pid;
1495 int pidz;
1496 int ppidz;
1497 int w = 0;
1499 n = 60;
1500 m = 15;
1501 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1502 w = 1;
1503 pidz = pidof("gzip");
1504 if (pidz < 1) pidz = pidof("cp");
1505 ppidz = ppid(ppid(pidz));
1506 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1507 syslog(LOG_DEBUG, "rstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1508 --m;
1509 } else {
1510 kill(pid, SIGTERM);
1512 sleep(1);
1514 if ((w == 1) && (n > 0))
1515 syslog(LOG_DEBUG, "rstats stopped.\n");
1518 static void start_rstats(int new)
1520 if (nvram_match("rstats_enable", "1")) {
1521 stop_rstats();
1522 if (new) {
1523 syslog(LOG_DEBUG, "starting rstats (new datafile).\n");
1524 xstart("rstats", "--new");
1525 } else {
1526 syslog(LOG_DEBUG, "starting rstats.\n");
1527 xstart("rstats");
1532 static void stop_cstats(void)
1534 int n, m;
1535 int pid;
1536 int pidz;
1537 int ppidz;
1538 int w = 0;
1540 n = 60;
1541 m = 15;
1542 while ((n-- > 0) && ((pid = pidof("cstats")) > 0)) {
1543 w = 1;
1544 pidz = pidof("gzip");
1545 if (pidz < 1) pidz = pidof("cp");
1546 ppidz = ppid(ppid(pidz));
1547 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1548 syslog(LOG_DEBUG, "cstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1549 --m;
1550 } else {
1551 kill(pid, SIGTERM);
1553 sleep(1);
1555 if ((w == 1) && (n > 0))
1556 syslog(LOG_DEBUG, "cstats stopped.\n");
1559 static void start_cstats(int new)
1561 if (nvram_match("cstats_enable", "1")) {
1562 stop_cstats();
1563 if (new) {
1564 syslog(LOG_DEBUG, "starting cstats (new datafile).\n");
1565 xstart("cstats", "--new");
1566 } else {
1567 syslog(LOG_DEBUG, "starting cstats.\n");
1568 xstart("cstats");
1573 // -----------------------------------------------------------------------------
1575 // !!TB - FTP Server
1577 #ifdef TCONFIG_FTP
1578 static char *get_full_storage_path(char *val)
1580 static char buf[128];
1581 int len;
1583 if (val[0] == '/')
1584 len = sprintf(buf, "%s", val);
1585 else
1586 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1588 if (len > 1 && buf[len - 1] == '/')
1589 buf[len - 1] = 0;
1591 return buf;
1594 static char *nvram_storage_path(char *var)
1596 char *val = nvram_safe_get(var);
1597 return get_full_storage_path(val);
1600 char vsftpd_conf[] = "/etc/vsftpd.conf";
1601 char vsftpd_users[] = "/etc/vsftpd.users";
1602 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1604 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1606 static void start_ftpd(void)
1608 char tmp[256];
1609 FILE *fp, *f;
1610 char *buf;
1611 char *p, *q;
1612 char *user, *pass, *rights, *root_dir;
1613 int i;
1615 if (getpid() != 1) {
1616 start_service("ftpd");
1617 return;
1620 if (!nvram_get_int("ftp_enable")) return;
1622 mkdir_if_none(vsftpd_users);
1623 mkdir_if_none("/var/run/vsftpd");
1625 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1626 return;
1628 if (nvram_get_int("ftp_super"))
1630 /* rights */
1631 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1632 if ((f = fopen(tmp, "w")))
1634 fprintf(f,
1635 "dirlist_enable=yes\n"
1636 "write_enable=yes\n"
1637 "download_enable=yes\n");
1638 fclose(f);
1642 #ifdef TCONFIG_SAMBASRV
1643 if (nvram_match("smbd_cset", "utf8"))
1644 fprintf(fp, "utf8=yes\n");
1645 #endif
1647 if (nvram_invmatch("ftp_anonymous", "0"))
1649 fprintf(fp,
1650 "anon_allow_writable_root=yes\n"
1651 "anon_world_readable_only=no\n"
1652 "anon_umask=022\n");
1654 /* rights */
1655 sprintf(tmp, "%s/ftp", vsftpd_users);
1656 if ((f = fopen(tmp, "w")))
1658 if (nvram_match("ftp_dirlist", "0"))
1659 fprintf(f, "dirlist_enable=yes\n");
1660 if (nvram_match("ftp_anonymous", "1") ||
1661 nvram_match("ftp_anonymous", "3"))
1662 fprintf(f, "write_enable=yes\n");
1663 if (nvram_match("ftp_anonymous", "1") ||
1664 nvram_match("ftp_anonymous", "2"))
1665 fprintf(f, "download_enable=yes\n");
1666 fclose(f);
1668 if (nvram_match("ftp_anonymous", "1") ||
1669 nvram_match("ftp_anonymous", "3"))
1670 fprintf(fp,
1671 "anon_upload_enable=yes\n"
1672 "anon_mkdir_write_enable=yes\n"
1673 "anon_other_write_enable=yes\n");
1674 } else {
1675 fprintf(fp, "anonymous_enable=no\n");
1678 fprintf(fp,
1679 "dirmessage_enable=yes\n"
1680 "download_enable=no\n"
1681 "dirlist_enable=no\n"
1682 "hide_ids=yes\n"
1683 "syslog_enable=yes\n"
1684 "local_enable=yes\n"
1685 "local_umask=022\n"
1686 "chmod_enable=no\n"
1687 "chroot_local_user=yes\n"
1688 "check_shell=no\n"
1689 "log_ftp_protocol=%s\n"
1690 "user_config_dir=%s\n"
1691 "passwd_file=%s\n"
1692 "listen%s=yes\n"
1693 "listen_port=%s\n"
1694 "background=yes\n"
1695 "isolate=no\n"
1696 "max_clients=%d\n"
1697 "max_per_ip=%d\n"
1698 "max_login_fails=1\n"
1699 "idle_session_timeout=%s\n"
1700 "use_sendfile=no\n"
1701 "anon_max_rate=%d\n"
1702 "local_max_rate=%d\n"
1703 "%s\n",
1704 nvram_get_int("log_ftp") ? "yes" : "no",
1705 vsftpd_users, vsftpd_passwd,
1706 #ifdef TCONFIG_IPV6
1707 ipv6_enabled() ? "_ipv6" : "",
1708 #else
1710 #endif
1711 nvram_get("ftp_port") ? : "21",
1712 nvram_get_int("ftp_max"),
1713 nvram_get_int("ftp_ipmax"),
1714 nvram_get("ftp_staytimeout") ? : "300",
1715 nvram_get_int("ftp_anonrate") * 1024,
1716 nvram_get_int("ftp_rate") * 1024,
1717 nvram_safe_get("ftp_custom"));
1719 fclose(fp);
1721 /* prepare passwd file and default users */
1722 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1723 return;
1725 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1726 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1728 fprintf(fp, /* anonymous, admin, nobody */
1729 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1730 "%s:%s:0:0:root:/:/sbin/nologin\n"
1731 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1732 nvram_storage_path("ftp_anonroot"), user,
1733 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1734 MOUNT_ROOT);
1736 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1739 username<password<rights[<root_dir]
1740 rights:
1741 Read/Write
1742 Read Only
1743 View Only
1744 Private
1746 p = buf;
1747 while ((q = strsep(&p, ">")) != NULL) {
1748 i = vstrsep(q, "<", &user, &pass, &rights, &root_dir);
1749 if (i < 3 || i > 4) continue;
1750 if (!user || !pass) continue;
1752 if (i == 3 || !root_dir || !(*root_dir))
1753 root_dir = nvram_safe_get("ftp_pubroot");
1755 /* directory */
1756 if (strncmp(rights, "Private", 7) == 0)
1758 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1759 mkdir_if_none(tmp);
1761 else
1762 sprintf(tmp, "%s", get_full_storage_path(root_dir));
1764 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1765 user, crypt(pass, "$1$"), user, tmp);
1767 /* rights */
1768 sprintf(tmp, "%s/%s", vsftpd_users, user);
1769 if ((f = fopen(tmp, "w")))
1771 tmp[0] = 0;
1772 if (nvram_invmatch("ftp_dirlist", "1"))
1773 strcat(tmp, "dirlist_enable=yes\n");
1774 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1775 strcat(tmp, "download_enable=yes\n");
1776 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1777 strcat(tmp, "write_enable=yes\n");
1779 fputs(tmp, f);
1780 fclose(f);
1783 free(buf);
1786 fclose(fp);
1787 killall("vsftpd", SIGHUP);
1789 /* start vsftpd if it's not already running */
1790 if (pidof("vsftpd") <= 0)
1791 xstart("vsftpd");
1794 static void stop_ftpd(void)
1796 if (getpid() != 1) {
1797 stop_service("ftpd");
1798 return;
1801 killall_tk("vsftpd");
1802 unlink(vsftpd_passwd);
1803 unlink(vsftpd_conf);
1804 eval("rm", "-rf", vsftpd_users);
1806 #endif // TCONFIG_FTP
1808 // -----------------------------------------------------------------------------
1810 // !!TB - Samba
1812 #ifdef TCONFIG_SAMBASRV
1813 static void kill_samba(int sig)
1815 if (sig == SIGTERM) {
1816 killall_tk("smbd");
1817 killall_tk("nmbd");
1819 else {
1820 killall("smbd", sig);
1821 killall("nmbd", sig);
1825 static void start_samba(void)
1827 FILE *fp;
1828 DIR *dir = NULL;
1829 struct dirent *dp;
1830 char nlsmod[15];
1831 int mode;
1832 char *nv;
1834 if (getpid() != 1) {
1835 start_service("smbd");
1836 return;
1839 mode = nvram_get_int("smbd_enable");
1840 if (!mode || !nvram_invmatch("lan_hostname", ""))
1841 return;
1843 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1844 return;
1846 fprintf(fp, "[global]\n"
1847 " interfaces = %s\n"
1848 " bind interfaces only = yes\n"
1849 " workgroup = %s\n"
1850 " netbios name = %s\n"
1851 " server string = %s\n"
1852 " guest account = nobody\n"
1853 " security = user\n"
1854 " %s\n"
1855 " guest ok = %s\n"
1856 " guest only = no\n"
1857 " browseable = yes\n"
1858 " syslog only = yes\n"
1859 " timestamp logs = no\n"
1860 " syslog = 1\n"
1861 " encrypt passwords = yes\n"
1862 " preserve case = yes\n"
1863 " short preserve case = yes\n",
1864 nvram_safe_get("lan_ifname"),
1865 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1866 nvram_safe_get("lan_hostname"),
1867 nvram_get("router_name") ? : "Tomato",
1868 mode == 2 ? "" : "map to guest = Bad User",
1869 mode == 2 ? "no" : "yes" // guest ok
1872 if (nvram_get_int("smbd_wins")) {
1873 nv = nvram_safe_get("wan_wins");
1874 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1875 fprintf(fp, " wins support = yes\n");
1879 if (nvram_get_int("smbd_master")) {
1880 fprintf(fp,
1881 " domain master = yes\n"
1882 " local master = yes\n"
1883 " preferred master = yes\n"
1884 " os level = 65\n");
1887 nv = nvram_safe_get("smbd_cpage");
1888 if (*nv) {
1889 #ifndef TCONFIG_SAMBA3
1890 fprintf(fp, " client code page = %s\n", nv);
1891 #endif
1892 sprintf(nlsmod, "nls_cp%s", nv);
1894 nv = nvram_safe_get("smbd_nlsmod");
1895 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1896 modprobe_r(nv);
1898 modprobe(nlsmod);
1899 nvram_set("smbd_nlsmod", nlsmod);
1902 #ifndef TCONFIG_SAMBA3
1903 if (nvram_match("smbd_cset", "utf8"))
1904 fprintf(fp, " coding system = utf8\n");
1905 else if (nvram_invmatch("smbd_cset", ""))
1906 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1907 #endif
1909 nv = nvram_safe_get("smbd_custom");
1910 /* add socket options unless overriden by the user */
1911 if (strstr(nv, "socket options") == NULL) {
1912 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1914 fprintf(fp, "%s\n\n", nv);
1916 /* configure shares */
1918 char *buf;
1919 char *p, *q;
1920 char *name, *path, *comment, *writeable, *hidden;
1921 int cnt = 0;
1923 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1925 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1927 p = buf;
1928 while ((q = strsep(&p, ">")) != NULL) {
1929 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1930 if (!path || !name) continue;
1932 /* share name */
1933 fprintf(fp, "\n[%s]\n", name);
1935 /* path */
1936 fprintf(fp, " path = %s\n", path);
1938 /* access level */
1939 if (!strcmp(writeable, "1"))
1940 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1941 if (!strcmp(hidden, "1"))
1942 fprintf(fp, " browseable = no\n");
1944 /* comment */
1945 if (comment)
1946 fprintf(fp, " comment = %s\n", comment);
1948 cnt++;
1950 free(buf);
1953 /* Share every mountpoint below MOUNT_ROOT */
1954 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1955 while ((dp = readdir(dir))) {
1956 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1958 /* Only if is a directory and is mounted */
1959 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
1960 continue;
1962 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1963 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1964 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1965 if (nvram_match("smbd_autoshare", "3")) // Hidden
1966 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1967 dp->d_name, MOUNT_ROOT, dp->d_name);
1968 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1969 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1971 cnt++;
1975 if (dir) closedir(dir);
1977 if (cnt == 0) {
1978 /* by default share MOUNT_ROOT as read-only */
1979 fprintf(fp, "\n[share]\n"
1980 " path = %s\n"
1981 " writable = no\n",
1982 MOUNT_ROOT);
1985 fclose(fp);
1987 mkdir_if_none("/var/run/samba");
1988 mkdir_if_none("/etc/samba");
1990 /* write smbpasswd */
1991 #ifdef TCONFIG_SAMBA3
1992 eval("smbpasswd", "nobody", "\"\"");
1993 #else
1994 eval("smbpasswd", "-a", "nobody", "\"\"");
1995 #endif
1996 if (mode == 2) {
1997 char *smbd_user;
1998 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1999 smbd_user = "nas";
2000 #ifdef TCONFIG_SAMBA3
2001 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
2002 #else
2003 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
2004 #endif
2007 kill_samba(SIGHUP);
2008 int ret1 = 0, ret2 = 0;
2009 /* start samba if it's not already running */
2010 if (pidof("nmbd") <= 0)
2011 ret1 = xstart("nmbd", "-D");
2012 if (pidof("smbd") <= 0)
2013 ret2 = xstart("smbd", "-D");
2015 if (ret1 || ret2) kill_samba(SIGTERM);
2018 static void stop_samba(void)
2020 if (getpid() != 1) {
2021 stop_service("smbd");
2022 return;
2025 kill_samba(SIGTERM);
2026 /* clean up */
2027 unlink("/var/log/smb");
2028 unlink("/var/log/nmb");
2029 eval("rm", "-rf", "/var/run/samba");
2031 #endif // TCONFIG_SAMBASRV
2033 #ifdef TCONFIG_MEDIA_SERVER
2034 #define MEDIA_SERVER_APP "minidlna"
2036 static void start_media_server(void)
2038 FILE *f;
2039 int port, pid, https;
2040 char *dbdir;
2041 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
2042 static int once = 1;
2044 if (getpid() != 1) {
2045 start_service("media");
2046 return;
2049 if (nvram_get_int("ms_sas") == 0)
2050 once = 0;
2052 if (nvram_get_int("ms_enable") != 0) {
2053 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
2054 // no forced rescan
2055 argv[3] = NULL;
2057 nvram_unset("ms_rescan");
2059 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
2060 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
2062 else {
2063 if ((f = fopen(argv[2], "w")) != NULL) {
2064 port = nvram_get_int("ms_port");
2065 https = nvram_get_int("https_enable");
2066 dbdir = nvram_safe_get("ms_dbdir");
2067 if (!(*dbdir)) dbdir = NULL;
2068 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
2070 fprintf(f,
2071 "network_interface=%s\n"
2072 "port=%d\n"
2073 "friendly_name=%s\n"
2074 "db_dir=%s/.db\n"
2075 "enable_tivo=%s\n"
2076 "strict_dlna=%s\n"
2077 "presentation_url=http%s://%s:%s/nas-media.asp\n"
2078 "inotify=yes\n"
2079 "notify_interval=600\n"
2080 "album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg\n"
2081 "\n",
2082 nvram_safe_get("lan_ifname"),
2083 (port < 0) || (port >= 0xffff) ? 0 : port,
2084 nvram_get("router_name") ? : "Tomato",
2085 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
2086 nvram_get_int("ms_tivo") ? "yes" : "no",
2087 nvram_get_int("ms_stdlna") ? "yes" : "no",
2088 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
2091 // media directories
2092 char *buf, *p, *q;
2093 char *path, *restrict;
2095 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
2096 /* path<restrict[A|V|P|] */
2098 p = buf;
2099 while ((q = strsep(&p, ">")) != NULL) {
2100 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
2101 continue;
2102 fprintf(f, "media_dir=%s%s%s\n",
2103 restrict ? : "", (restrict && *restrict) ? "," : "", path);
2105 free(buf);
2108 fclose(f);
2112 /* start media server if it's not already running */
2113 if (pidof(MEDIA_SERVER_APP) <= 0) {
2114 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
2115 /* If we started the media server successfully, wait 1 sec
2116 * to let it die if it can't open the database file.
2117 * If it's still alive after that, assume it's running and
2118 * disable forced once-after-reboot rescan.
2120 sleep(1);
2121 if (pidof(MEDIA_SERVER_APP) > 0)
2122 once = 0;
2128 static void stop_media_server(void)
2130 if (getpid() != 1) {
2131 stop_service("media");
2132 return;
2135 killall_tk(MEDIA_SERVER_APP);
2137 #endif // TCONFIG_MEDIA_SERVER
2139 #ifdef TCONFIG_USB
2140 static void start_nas_services(void)
2142 if (getpid() != 1) {
2143 start_service("usbapps");
2144 return;
2147 #ifdef TCONFIG_SAMBASRV
2148 start_samba();
2149 #endif
2150 #ifdef TCONFIG_FTP
2151 start_ftpd();
2152 #endif
2153 #ifdef TCONFIG_MEDIA_SERVER
2154 start_media_server();
2155 #endif
2158 static void stop_nas_services(void)
2160 if (getpid() != 1) {
2161 stop_service("usbapps");
2162 return;
2165 #ifdef TCONFIG_MEDIA_SERVER
2166 stop_media_server();
2167 #endif
2168 #ifdef TCONFIG_FTP
2169 stop_ftpd();
2170 #endif
2171 #ifdef TCONFIG_SAMBASRV
2172 stop_samba();
2173 #endif
2176 void restart_nas_services(int stop, int start)
2178 int fd = file_lock("usb");
2179 /* restart all NAS applications */
2180 if (stop)
2181 stop_nas_services();
2182 if (start)
2183 start_nas_services();
2184 file_unlock(fd);
2186 #endif // TCONFIG_USB
2188 // -----------------------------------------------------------------------------
2190 /* -1 = Don't check for this program, it is not expected to be running.
2191 * Other = This program has been started and should be kept running. If no
2192 * process with the name is running, call func to restart it.
2193 * Note: At startup, dnsmasq forks a short-lived child which forks a
2194 * long-lived (grand)child. The parents terminate.
2195 * Many daemons use this technique.
2197 static void _check(pid_t pid, const char *name, void (*func)(void))
2199 if (pid == -1) return;
2201 if (pidof(name) > 0) return;
2203 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
2204 func();
2206 // Force recheck in 500 msec
2207 setitimer(ITIMER_REAL, &pop_tv, NULL);
2210 void check_services(void)
2212 TRACE_PT("keep alive\n");
2214 // Periodically reap any zombies
2215 setitimer(ITIMER_REAL, &zombie_tv, NULL);
2217 #ifdef LINUX26
2218 _check(pid_hotplug2, "hotplug2", start_hotplug2);
2219 #endif
2220 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
2221 _check(pid_crond, "crond", start_cron);
2222 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
2224 // #ifdef TCONFIG_NOCAT
2225 // if (nvram_get_int("NC_enable"))
2226 // _check(&pid_splashd, "splashd", start_splashd);
2227 // #endif
2231 // -----------------------------------------------------------------------------
2233 void start_services(void)
2235 static int once = 1;
2237 if (once) {
2238 once = 0;
2240 if (nvram_get_int("telnetd_eas")) start_telnetd();
2241 if (nvram_get_int("sshd_eas")) start_sshd();
2244 // start_syslog();
2245 start_nas();
2246 start_zebra();
2247 start_dnsmasq();
2248 start_cifs();
2249 start_httpd();
2250 start_cron();
2251 // start_upnp();
2252 start_rstats(0);
2253 start_cstats(0);
2254 start_sched();
2255 #ifdef TCONFIG_PPTPD
2256 start_pptpd();
2257 #endif
2258 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2260 #ifdef TCONFIG_SNMP
2261 start_snmp();
2262 #endif
2264 #ifdef TCONFIG_NOCAT
2265 start_splashd();
2266 #endif
2270 void stop_services(void)
2272 clear_resolv();
2273 // restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2276 #ifdef TCONFIG_NOCAT
2277 stop_splashd();
2278 #endif
2280 #ifdef TCONFIG_SNMP
2281 stop_snmp();
2282 #endif
2284 #ifdef TCONFIG_TOR
2285 stop_tor();
2286 #endif
2288 #ifdef TCONFIG_NFS
2289 stop_nfs();
2290 #endif
2291 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2292 #ifdef TCONFIG_PPTPD
2293 stop_pptpd();
2294 #endif
2295 stop_sched();
2296 stop_rstats();
2297 stop_cstats();
2298 // stop_upnp();
2299 stop_cron();
2300 stop_httpd();
2301 stop_cifs();
2302 stop_dnsmasq();
2303 stop_zebra();
2304 stop_nas();
2305 // stop_syslog();
2308 // -----------------------------------------------------------------------------
2310 /* nvram "action_service" is: "service-action[-modifier]"
2311 * action is something like "stop" or "start" or "restart"
2312 * optional modifier is "c" for the "service" command-line command
2314 void exec_service(void)
2316 const int A_START = 1;
2317 const int A_STOP = 2;
2318 const int A_RESTART = 1|2;
2319 char buffer[128];
2320 char *service;
2321 char *act;
2322 char *next;
2323 char *modifier;
2324 int action, user;
2325 int i;
2327 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2328 next = buffer;
2330 TOP:
2331 act = strsep(&next, ",");
2332 service = strsep(&act, "-");
2333 if (act == NULL) {
2334 next = NULL;
2335 goto CLEAR;
2337 modifier = act;
2338 strsep(&modifier, "-");
2340 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2342 if (strcmp(act, "start") == 0) action = A_START;
2343 else if (strcmp(act, "stop") == 0) action = A_STOP;
2344 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2345 else action = 0;
2346 user = (modifier != NULL && *modifier == 'c');
2348 if (strcmp(service, "dhcpc") == 0) {
2349 if (action & A_STOP) stop_dhcpc();
2350 if (action & A_START) start_dhcpc();
2351 goto CLEAR;
2354 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2355 if (action & A_STOP) stop_dnsmasq();
2356 if (action & A_START) {
2357 dns_to_resolv();
2358 start_dnsmasq();
2360 goto CLEAR;
2363 if (strcmp(service, "firewall") == 0) {
2364 if (action & A_STOP) {
2365 stop_firewall();
2366 stop_igmp_proxy();
2367 stop_udpxy();
2369 if (action & A_START) {
2370 start_firewall();
2371 start_igmp_proxy();
2372 start_udpxy();
2374 goto CLEAR;
2377 if (strcmp(service, "restrict") == 0) {
2378 if (action & A_STOP) {
2379 stop_firewall();
2381 if (action & A_START) {
2382 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2384 start_firewall();
2386 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2387 if (i == 1) {
2388 if (nvram_get_int("rrules_radio") < 0) {
2389 eval("radio", "on");
2393 goto CLEAR;
2396 if (strcmp(service, "arpbind") == 0) {
2397 if (action & A_STOP) stop_arpbind();
2398 if (action & A_START) start_arpbind();
2399 goto CLEAR;
2402 if (strcmp(service, "qos") == 0) {
2403 if (action & A_STOP) {
2404 stop_qos();
2406 stop_firewall(); start_firewall(); // always restarted
2407 if (action & A_START) {
2408 start_qos();
2409 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2411 goto CLEAR;
2414 if (strcmp(service, "qoslimit") == 0) {
2415 if (action & A_STOP) {
2416 stop_qoslimit();
2418 #ifdef TCONFIG_NOCAT
2419 stop_splashd();
2420 #endif
2421 stop_firewall(); start_firewall(); // always restarted
2422 if (action & A_START) {
2423 start_qoslimit();
2425 #ifdef TCONFIG_NOCAT
2426 start_splashd();
2427 #endif
2428 goto CLEAR;
2431 if (strcmp(service, "upnp") == 0) {
2432 if (action & A_STOP) {
2433 stop_upnp();
2435 stop_firewall(); start_firewall(); // always restarted
2436 if (action & A_START) {
2437 start_upnp();
2439 goto CLEAR;
2442 if (strcmp(service, "telnetd") == 0) {
2443 if (action & A_STOP) stop_telnetd();
2444 if (action & A_START) start_telnetd();
2445 goto CLEAR;
2448 if (strcmp(service, "sshd") == 0) {
2449 if (action & A_STOP) stop_sshd();
2450 if (action & A_START) start_sshd();
2451 goto CLEAR;
2454 if (strcmp(service, "httpd") == 0) {
2455 if (action & A_STOP) stop_httpd();
2456 if (action & A_START) start_httpd();
2457 goto CLEAR;
2460 #ifdef TCONFIG_IPV6
2461 if (strcmp(service, "ipv6") == 0) {
2462 if (action & A_STOP) {
2463 stop_dnsmasq();
2464 stop_ipv6();
2466 if (action & A_START) {
2467 start_ipv6();
2468 start_dnsmasq();
2470 goto CLEAR;
2473 if (strncmp(service, "dhcp6", 5) == 0) {
2474 if (action & A_STOP) {
2475 stop_dhcp6c();
2477 if (action & A_START) {
2478 start_dhcp6c();
2480 goto CLEAR;
2482 #endif
2484 if (strcmp(service, "admin") == 0) {
2485 if (action & A_STOP) {
2486 stop_sshd();
2487 stop_telnetd();
2488 stop_httpd();
2490 stop_firewall(); start_firewall(); // always restarted
2491 if (action & A_START) {
2492 start_httpd();
2493 create_passwd();
2494 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2495 if (nvram_match("sshd_eas", "1")) start_sshd();
2497 goto CLEAR;
2500 if (strcmp(service, "ddns") == 0) {
2501 if (action & A_STOP) stop_ddns();
2502 if (action & A_START) start_ddns();
2503 goto CLEAR;
2506 if (strcmp(service, "ntpc") == 0) {
2507 if (action & A_STOP) stop_ntpc();
2508 if (action & A_START) start_ntpc();
2509 goto CLEAR;
2512 if (strcmp(service, "logging") == 0) {
2513 if (action & A_STOP) {
2514 stop_syslog();
2516 if (action & A_START) {
2517 start_syslog();
2519 if (!user) {
2520 // always restarted except from "service" command
2521 stop_cron(); start_cron();
2522 stop_firewall(); start_firewall();
2524 goto CLEAR;
2527 if (strcmp(service, "crond") == 0) {
2528 if (action & A_STOP) {
2529 stop_cron();
2531 if (action & A_START) {
2532 start_cron();
2534 goto CLEAR;
2537 #ifdef LINUX26
2538 if (strncmp(service, "hotplug", 7) == 0) {
2539 if (action & A_STOP) {
2540 stop_hotplug2();
2542 if (action & A_START) {
2543 start_hotplug2(1);
2545 goto CLEAR;
2547 #endif
2549 if (strcmp(service, "upgrade") == 0) {
2550 if (action & A_START) {
2551 #if TOMATO_SL
2552 stop_usbevent();
2553 stop_smbd();
2554 #endif
2555 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2556 stop_jffs2();
2557 // stop_cifs();
2558 stop_zebra();
2559 stop_cron();
2560 stop_ntpc();
2561 stop_upnp();
2562 // stop_dhcpc();
2563 killall("rstats", SIGTERM);
2564 killall("cstats", SIGTERM);
2565 killall("buttons", SIGTERM);
2566 stop_syslog();
2567 remove_storage_main(1); // !!TB - USB Support
2568 stop_usb(); // !!TB - USB Support
2570 goto CLEAR;
2573 #ifdef TCONFIG_CIFS
2574 if (strcmp(service, "cifs") == 0) {
2575 if (action & A_STOP) stop_cifs();
2576 if (action & A_START) start_cifs();
2577 goto CLEAR;
2579 #endif
2581 #ifdef TCONFIG_JFFS2
2582 if (strncmp(service, "jffs", 4) == 0) {
2583 if (action & A_STOP) stop_jffs2();
2584 if (action & A_START) start_jffs2();
2585 goto CLEAR;
2587 #endif
2589 if (strcmp(service, "zebra") == 0) {
2590 if (action & A_STOP) stop_zebra();
2591 if (action & A_START) start_zebra();
2592 goto CLEAR;
2595 if (strcmp(service, "routing") == 0) {
2596 if (action & A_STOP) {
2597 stop_zebra();
2598 do_static_routes(0); // remove old '_saved'
2599 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2600 #ifdef TCONFIG_VLAN
2601 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2602 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2603 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2604 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2605 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2606 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2607 #endif
2609 stop_firewall();
2610 start_firewall();
2611 if (action & A_START) {
2612 do_static_routes(1); // add new
2613 start_zebra();
2614 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2615 #ifdef TCONFIG_VLAN
2616 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2617 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2618 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2619 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2620 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2621 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2622 #endif
2624 goto CLEAR;
2627 if (strcmp(service, "ctnf") == 0) {
2628 if (action & A_START) {
2629 setup_conntrack();
2630 stop_firewall();
2631 start_firewall();
2633 goto CLEAR;
2636 if (strcmp(service, "wan") == 0) {
2637 if (action & A_STOP) {
2638 stop_wan();
2641 if (action & A_START) {
2642 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2643 start_wan(BOOT);
2644 sleep(2);
2645 force_to_dial();
2647 goto CLEAR;
2650 if (strcmp(service, "net") == 0) {
2651 if (action & A_STOP) {
2652 #ifdef TCONFIG_USB
2653 stop_nas_services();
2654 #endif
2655 stop_httpd();
2656 stop_dnsmasq();
2657 stop_nas();
2658 stop_wan();
2659 stop_arpbind();
2660 stop_lan();
2661 stop_vlan();
2663 if (action & A_START) {
2664 start_vlan();
2665 start_lan();
2666 start_wan(BOOT);
2667 start_arpbind();
2668 start_nas();
2669 start_dnsmasq();
2670 start_httpd();
2671 start_wl();
2672 #ifdef TCONFIG_USB
2673 start_nas_services();
2674 #endif
2676 goto CLEAR;
2679 if (strcmp(service, "wireless") == 0) {
2680 if(action & A_STOP) {
2681 stop_wireless();
2683 if(action & A_START) {
2684 start_wireless();
2686 goto CLEAR;
2689 if (strcmp(service, "wl") == 0) {
2690 if(action & A_STOP) {
2691 stop_wireless();
2692 unload_wl();
2694 if(action & A_START) {
2695 load_wl();
2696 start_wireless();
2697 stop_wireless();
2698 start_wireless();
2700 goto CLEAR;
2703 if (strcmp(service, "nas") == 0) {
2704 if (action & A_STOP) {
2705 stop_nas();
2707 if (action & A_START) {
2708 start_nas();
2709 start_wl();
2711 goto CLEAR;
2714 if (strcmp(service, "rstats") == 0) {
2715 if (action & A_STOP) stop_rstats();
2716 if (action & A_START) start_rstats(0);
2717 goto CLEAR;
2720 if (strcmp(service, "rstatsnew") == 0) {
2721 if (action & A_STOP) stop_rstats();
2722 if (action & A_START) start_rstats(1);
2723 goto CLEAR;
2726 if (strcmp(service, "cstats") == 0) {
2727 if (action & A_STOP) stop_cstats();
2728 if (action & A_START) start_cstats(0);
2729 goto CLEAR;
2732 if (strcmp(service, "cstatsnew") == 0) {
2733 if (action & A_STOP) stop_cstats();
2734 if (action & A_START) start_cstats(1);
2735 goto CLEAR;
2738 if (strcmp(service, "sched") == 0) {
2739 if (action & A_STOP) stop_sched();
2740 if (action & A_START) start_sched();
2741 goto CLEAR;
2745 #ifdef TCONFIG_SNMP
2746 if (strcmp(service, "snmp") == 0) {
2747 if (action & A_STOP) stop_snmp();
2748 if (action & A_START) start_snmp();
2749 goto CLEAR;
2751 #endif
2754 #ifdef TCONFIG_USB
2755 // !!TB - USB Support
2756 if (strcmp(service, "usb") == 0) {
2757 if (action & A_STOP) stop_usb();
2758 if (action & A_START) {
2759 start_usb();
2760 // restart Samba and ftp since they may be killed by stop_usb()
2761 restart_nas_services(0, 1);
2762 // remount all partitions by simulating hotplug event
2763 add_remove_usbhost("-1", 1);
2765 goto CLEAR;
2768 if (strcmp(service, "usbapps") == 0) {
2769 if (action & A_STOP) stop_nas_services();
2770 if (action & A_START) start_nas_services();
2771 goto CLEAR;
2773 #endif
2775 #ifdef TCONFIG_FTP
2776 // !!TB - FTP Server
2777 if (strcmp(service, "ftpd") == 0) {
2778 if (action & A_STOP) stop_ftpd();
2779 setup_conntrack();
2780 stop_firewall();
2781 start_firewall();
2782 if (action & A_START) start_ftpd();
2783 goto CLEAR;
2785 #endif
2787 #ifdef TCONFIG_MEDIA_SERVER
2788 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2789 if (action & A_STOP) stop_media_server();
2790 if (action & A_START) start_media_server();
2791 goto CLEAR;
2793 #endif
2795 #ifdef TCONFIG_SAMBASRV
2796 // !!TB - Samba
2797 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2798 if (action & A_STOP) stop_samba();
2799 if (action & A_START) {
2800 create_passwd();
2801 stop_dnsmasq();
2802 start_dnsmasq();
2803 start_samba();
2805 goto CLEAR;
2807 #endif
2809 #ifdef TCONFIG_OPENVPN
2810 if (strncmp(service, "vpnclient", 9) == 0) {
2811 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2812 if (action & A_START) start_vpnclient(atoi(&service[9]));
2813 goto CLEAR;
2816 if (strncmp(service, "vpnserver", 9) == 0) {
2817 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2818 if (action & A_START) start_vpnserver(atoi(&service[9]));
2819 goto CLEAR;
2821 #endif
2823 #ifdef TCONFIG_NOCAT
2824 if (strcmp(service, "splashd") == 0) {
2825 if (action & A_STOP) stop_splashd();
2826 if (action & A_START) start_splashd();
2827 goto CLEAR;
2829 #endif
2831 #ifdef TCONFIG_PPTPD
2832 if (strcmp(service, "pptpd") == 0) {
2833 if (action & A_STOP) stop_pptpd();
2834 if (action & A_START) start_pptpd();
2835 goto CLEAR;
2837 #endif
2839 #ifdef TCONFIG_USERPPTP
2840 if (strcmp(service, "pptpclient") == 0) {
2841 if (action & A_STOP) stop_pptp_client();
2842 if (action & A_START) start_pptp_client();
2843 if (action & (A_START | A_STOP))
2845 stop_dnsmasq();
2846 dns_to_resolv();
2847 start_dnsmasq();
2848 if ((action & A_START) == 0)
2849 clear_pptp_route();
2851 goto CLEAR;
2853 #endif
2855 CLEAR:
2856 if (next) goto TOP;
2858 // some functions check action_service and must be cleared at end -- zzz
2859 nvram_set("action_service", "");
2861 // Force recheck in 500 msec
2862 setitimer(ITIMER_REAL, &pop_tv, NULL);
2865 static void do_service(const char *name, const char *action, int user)
2867 int n;
2868 char s[64];
2870 n = 150;
2871 while (!nvram_match("action_service", "")) {
2872 if (user) {
2873 putchar('*');
2874 fflush(stdout);
2876 else if (--n < 0) break;
2877 usleep(100 * 1000);
2880 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2881 nvram_set("action_service", s);
2883 if (nvram_match("debug_rc_svc", "1")) {
2884 nvram_unset("debug_rc_svc");
2885 exec_service();
2886 } else {
2887 kill(1, SIGUSR1);
2890 n = 150;
2891 while (nvram_match("action_service", s)) {
2892 if (user) {
2893 putchar('.');
2894 fflush(stdout);
2896 else if (--n < 0) {
2897 break;
2899 usleep(100 * 1000);
2903 int service_main(int argc, char *argv[])
2905 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2906 do_service(argv[1], argv[2], 1);
2907 printf("\nDone.\n");
2908 return 0;
2911 void start_service(const char *name)
2913 do_service(name, "start", 0);
2916 void stop_service(const char *name)
2918 do_service(name, "stop", 0);
2922 void restart_service(const char *name)
2924 do_service(name, "restart", 0);