New "Static ARP" and "Clients Monitor" by Augusto Bott (thx teaman).
[tomato.git] / release / src / router / rc / services.c
blob75765478c6e6c22d932cb8d15eddc77cd786f332
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 TRACE_PT("begin\n");
91 if (getpid() != 1) {
92 start_service("dnsmasq");
93 return;
96 stop_dnsmasq();
98 if (foreach_wif(1, NULL, is_wet)) return;
100 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
102 router_ip = nvram_safe_get("lan_ipaddr");
104 fprintf(f,
105 "pid-file=/var/run/dnsmasq.pid\n");
106 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
107 if (*nv) fprintf(f, "domain=%s\n", nv);
110 // dns
111 const dns_list_t *dns = get_dns(); // this always points to a static buffer
113 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
114 else n = 4096;
115 fprintf(f,
116 "resolv-file=%s\n" // the real stuff is here
117 "addn-hosts=%s\n" // directory with additional hosts files
118 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv, dmhosts, dmdhcp, n);
122 do_dns = nvram_match("dhcpd_dmdns", "1");
124 // DNS rebinding protection, will discard upstream RFC1918 responses
125 if (nvram_get_int("dns_norebind")) {
126 fprintf(f,
127 "stop-dns-rebind\n"
128 "rebind-localhost-ok\n");
129 // allow RFC1918 responses for server domain
130 switch (get_wan_proto()) {
131 case WP_PPTP:
132 nv = nvram_get("pptp_server_ip");
133 break;
134 case WP_L2TP:
135 nv = nvram_get("l2tp_server_ip");
136 break;
137 default:
138 nv = NULL;
139 break;
141 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
144 for (n = 0 ; n < dns->count; ++n) {
145 if (dns->dns[n].port != 53) {
146 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
150 // dhcp
151 do_dhcpd_hosts=0;
152 char lanN_proto[] = "lanXX_proto";
153 char lanN_ifname[] = "lanXX_ifname";
154 char lanN_ipaddr[] = "lanXX_ipaddr";
155 char lanN_netmask[] = "lanXX_netmask";
156 char dhcpdN_startip[] = "dhcpdXX_startip";
157 char dhcpdN_endip[] = "dhcpdXX_endip";
158 char dhcpN_start[] = "dhcpXX_start";
159 char dhcpN_num[] = "dhcpXX_num";
160 char dhcpN_lease[] = "dhcpXX_lease";
161 char br;
162 for(br=0 ; br<=3 ; br++) {
163 char bridge[2] = "0";
164 if (br!=0)
165 bridge[0]+=br;
166 else
167 strcpy(bridge, "");
169 sprintf(lanN_proto, "lan%s_proto", bridge);
170 sprintf(lanN_ifname, "lan%s_ifname", bridge);
171 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
172 do_dhcpd = nvram_match(lanN_proto, "dhcp");
173 if (do_dhcpd) {
174 if (nvram_get_int("dhcpd_static_only")) {
175 fprintf(f, "dhcp-ignore=tag:!known\n");
177 do_dhcpd_hosts++;
179 router_ip = nvram_safe_get(lanN_ipaddr);
180 strlcpy(lan, router_ip, sizeof(lan));
181 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
183 fprintf(f,
184 "interface=%s\n",
185 nvram_safe_get(lanN_ifname));
187 sprintf(dhcpN_lease, "dhcp%s_lease", bridge);
188 dhcp_lease = nvram_get_int(dhcpN_lease);
190 if (dhcp_lease <= 0) dhcp_lease = 1440;
192 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
193 if (n < 0) strcpy(sdhcp_lease, "infinite");
194 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
196 if (!do_dns) {
197 // if not using dnsmasq for dns
199 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
200 // no DNS might be temporary. use a low lease time to force clients to update.
201 dhcp_lease = 2;
202 strcpy(sdhcp_lease, "2m");
203 do_dns = 1;
205 else {
206 // pass the dns directly
207 buf[0] = 0;
208 for (n = 0 ; n < dns->count; ++n) {
209 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
210 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
213 fprintf(f, "dhcp-option=%s,6%s\n", nvram_safe_get(lanN_ifname), buf);
217 sprintf(dhcpdN_startip, "dhcpd%s_startip", bridge);
218 sprintf(dhcpdN_endip, "dhcpd%s_endip", bridge);
219 sprintf(lanN_netmask, "lan%s_netmask", bridge);
221 if ((p = nvram_get(dhcpdN_startip)) && (*p) && (e = nvram_get(dhcpdN_endip)) && (*e)) {
222 fprintf(f, "dhcp-range=%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname), p, e, nvram_safe_get(lanN_netmask), dhcp_lease);
224 else {
225 // for compatibility
226 sprintf(dhcpN_start, "dhcp%s_start", bridge);
227 sprintf(dhcpN_num, "dhcp%s_num", bridge);
228 sprintf(lanN_netmask, "lan%s_netmask", bridge);
229 dhcp_start = nvram_get_int(dhcpN_start);
230 dhcp_count = nvram_get_int(dhcpN_num);
231 fprintf(f, "dhcp-range=%s,%s%d,%s%d,%s,%dm\n",
232 nvram_safe_get(lanN_ifname), lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(lanN_netmask), dhcp_lease);
235 nv = nvram_safe_get(lanN_ipaddr);
236 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
237 p = nvram_safe_get("lan_gateway");
238 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
241 fprintf(f,
242 "dhcp-option=%s,3,%s\n", // gateway
243 nvram_safe_get(lanN_ifname), nv);
245 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
246 fprintf(f, "dhcp-option=%s,44,%s\n", nvram_safe_get(lanN_ifname), nv);
248 #ifdef TCONFIG_SAMBASRV
249 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
250 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
251 // Samba will serve as a WINS server
252 fprintf(f, "dhcp-option=%s,44,0.0.0.0\n", nvram_safe_get(lanN_ifname));
255 #endif
256 } else {
257 if (strcmp(nvram_safe_get(lanN_ifname),"")!=0)
258 fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname));
261 // write static lease entries & create hosts file
263 mkdir_if_none(dmhosts);
264 snprintf(buf, sizeof(buf), "%s/hosts", dmhosts);
265 if ((hf = fopen(buf, "w")) != NULL) {
266 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
267 fprintf(hf, "%s %s\n", router_ip, nv);
268 #ifdef TCONFIG_SAMBASRV
269 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
270 fprintf(hf, "%s %s\n", router_ip, nv);
271 #endif
272 p = (char *)get_wanip();
273 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
274 p = "127.0.0.1";
275 fprintf(hf, "%s wan-ip\n", p);
276 if (nv && (*nv))
277 fprintf(hf, "%s %s-wan\n", p, nv);
280 mkdir_if_none(dmdhcp);
281 snprintf(buf, sizeof(buf), "%s/dhcp-hosts", dmdhcp);
282 df = fopen(buf, "w");
284 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
285 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
286 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
287 p = nvram_safe_get("dhcpd_static");
288 while ((e = strchr(p, '>')) != NULL) {
289 n = (e - p);
290 if (n > 105) {
291 p = e + 1;
292 continue;
295 strncpy(buf, p, n);
296 buf[n] = 0;
297 p = e + 1;
299 if ((e = strchr(buf, '<')) == NULL) continue;
300 *e = 0;
301 mac = buf;
303 ip = e + 1;
304 if ((e = strchr(ip, '<')) == NULL) continue;
305 *e = 0;
306 if (strchr(ip, '.') == NULL) {
307 ipn = atoi(ip);
308 if ((ipn <= 0) || (ipn > 255)) continue;
309 sprintf(ipbuf, "%s%d", lan, ipn);
310 ip = ipbuf;
312 else {
313 if (inet_addr(ip) == INADDR_NONE) continue;
316 name = e + 1;
318 if ((hf) && (*name != 0)) {
319 fprintf(hf, "%s %s\n", ip, name);
322 if ((do_dhcpd_hosts > 0) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
323 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
327 if (df) fclose(df);
328 if (hf) fclose(hf);
330 n = nvram_get_int("dhcpd_lmax");
331 fprintf(f,
332 "dhcp-lease-max=%d\n",
333 (n > 0) ? n : 255);
334 if (nvram_get_int("dhcpd_auth") >= 0) {
335 fprintf(f, "dhcp-authoritative\n");
340 #ifdef TCONFIG_OPENVPN
341 write_vpn_dnsmasq_config(f);
342 #endif
344 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
346 fappend(f, "/etc/dnsmasq.custom");
350 fclose(f);
352 if (do_dns) {
353 unlink("/etc/resolv.conf");
354 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
357 TRACE_PT("run dnsmasq\n");
359 // Default to some values we like, but allow the user to override them.
360 eval("dnsmasq", "-c", "1500", "--log-async");
362 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
363 pid_dnsmasq = -2;
366 TRACE_PT("end\n");
369 void stop_dnsmasq(void)
371 TRACE_PT("begin\n");
373 if (getpid() != 1) {
374 stop_service("dnsmasq");
375 return;
378 pid_dnsmasq = -1;
380 unlink("/etc/resolv.conf");
381 symlink(dmresolv, "/etc/resolv.conf");
383 killall_tk("dnsmasq");
385 TRACE_PT("end\n");
388 void clear_resolv(void)
390 f_write(dmresolv, NULL, 0, 0, 0); // blank
393 #ifdef TCONFIG_IPV6
394 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
396 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
397 struct in6_addr addr;
398 int cnt = 0;
400 foreach(p, dns, next) {
401 // verify that this is a valid IPv6 address
402 if (inet_pton(AF_INET6, p, &addr) == 1) {
403 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
404 ++cnt;
408 return cnt;
410 #endif
412 void dns_to_resolv(void)
414 FILE *f;
415 const dns_list_t *dns;
416 int i;
417 mode_t m;
419 m = umask(022); // 077 from pppoecd
420 if ((f = fopen(dmresolv, "w")) != NULL) {
421 // Check for VPN DNS entries
422 if (!write_vpn_resolv(f)) {
423 #ifdef TCONFIG_IPV6
424 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
425 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
426 #endif
427 dns = get_dns(); // static buffer
428 if (dns->count == 0) {
429 // Put a pseudo DNS IP to trigger Connect On Demand
430 if (nvram_match("ppp_demand", "1")) {
431 switch (get_wan_proto()) {
432 case WP_PPPOE:
433 case WP_PPTP:
434 case WP_L2TP:
435 fprintf(f, "nameserver 1.1.1.1\n");
436 break;
440 else {
441 for (i = 0; i < dns->count; i++) {
442 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
443 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
448 fclose(f);
450 umask(m);
453 // -----------------------------------------------------------------------------
455 void start_httpd(void)
457 if (getpid() != 1) {
458 start_service("httpd");
459 return;
462 stop_httpd();
463 chdir("/www");
464 eval("httpd");
465 chdir("/");
468 void stop_httpd(void)
470 if (getpid() != 1) {
471 stop_service("httpd");
472 return;
475 killall_tk("httpd");
478 // -----------------------------------------------------------------------------
479 #ifdef TCONFIG_IPV6
481 static void add_ip6_lanaddr(void)
483 char ip[INET6_ADDRSTRLEN + 4];
484 const char *p;
486 p = ipv6_router_address(NULL);
487 if (*p) {
488 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
489 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
493 void start_ipv6_tunnel(void)
495 char ip[INET6_ADDRSTRLEN + 4];
496 struct in_addr addr4;
497 struct in6_addr addr;
498 const char *wanip, *mtu, *tun_dev;
499 int service;
501 service = get_ipv6_service();
502 tun_dev = get_wan6face();
503 wanip = get_wanip();
504 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
505 modprobe("sit");
507 if (service == IPV6_ANYCAST_6TO4)
508 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
509 else
510 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
511 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
512 "remote", ip,
513 "local", (char *)wanip,
514 "ttl", nvram_safe_get("ipv6_tun_ttl"));
516 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
517 nvram_set("ipv6_ifname", (char *)tun_dev);
519 if (service == IPV6_ANYCAST_6TO4) {
520 add_ip6_lanaddr();
521 addr4.s_addr = 0;
522 memset(&addr, 0, sizeof(addr));
523 inet_aton(wanip, &addr4);
524 addr.s6_addr16[0] = htons(0x2002);
525 ipv6_mapaddr4(&addr, 16, &addr4, 0);
526 addr.s6_addr16[7] = htons(0x0001);
527 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
528 strncat(ip, "/16", sizeof(ip));
530 else {
531 snprintf(ip, sizeof(ip), "%s/%d",
532 nvram_safe_get("ipv6_tun_addr"),
533 nvram_get_int("ipv6_tun_addrlen") ? : 64);
535 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
536 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
538 // (re)start radvd
539 if (service == IPV6_ANYCAST_6TO4)
540 start_radvd();
543 void stop_ipv6_tunnel(void)
545 eval("ip", "tunnel", "del", (char *)get_wan6face());
546 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
547 // get rid of old IPv6 address from lan iface
548 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
550 modprobe_r("sit");
553 static pid_t pid_radvd = -1;
555 void start_radvd(void)
557 FILE *f;
558 char *prefix, *ip, *mtu;
559 int do_dns, do_6to4;
560 char *argv[] = { "radvd", NULL, NULL, NULL };
561 int pid, argc, service, cnt;
563 if (getpid() != 1) {
564 start_service("radvd");
565 return;
568 stop_radvd();
570 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
571 service = get_ipv6_service();
572 do_6to4 = (service == IPV6_ANYCAST_6TO4);
573 mtu = NULL;
575 switch (service) {
576 case IPV6_NATIVE_DHCP:
577 prefix = "::";
578 break;
579 case IPV6_ANYCAST_6TO4:
580 case IPV6_6IN4:
581 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
582 // fall through
583 default:
584 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
585 break;
587 if (!(*prefix)) prefix = "::";
589 // Create radvd.conf
590 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
592 ip = (char *)ipv6_router_address(NULL);
593 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
595 fprintf(f,
596 "interface %s\n"
597 "{\n"
598 " IgnoreIfMissing on;\n"
599 " AdvSendAdvert on;\n"
600 " MaxRtrAdvInterval 60;\n"
601 " AdvHomeAgentFlag off;\n"
602 " AdvManagedFlag off;\n"
603 "%s%s%s"
604 " prefix %s/64 \n"
605 " {\n"
606 " AdvOnLink on;\n"
607 " AdvAutonomous on;\n"
608 "%s"
609 "%s%s%s"
610 " };\n",
611 nvram_safe_get("lan_ifname"),
612 mtu ? " AdvLinkMTU " : "", mtu ? : "", mtu ? ";\n" : "",
613 prefix,
614 do_6to4 ? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
615 do_6to4 ? " Base6to4Interface " : "",
616 do_6to4 ? get_wanface() : "",
617 do_6to4 ? ";\n" : "");
619 if (do_dns) {
620 fprintf(f, " RDNSS %s {};\n", ip);
622 else {
623 cnt = write_ipv6_dns_servers(f, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
624 if (cnt == 0 || nvram_get_int("dns_addget"))
625 cnt += write_ipv6_dns_servers(f, (cnt) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
626 if (cnt) fprintf(f, "{};\n");
629 fprintf(f,
630 "};\n"); // close "interface" section
631 fclose(f);
633 // Start radvd
634 argc = 1;
635 if (nvram_get_int("debug_ipv6")) {
636 argv[argc++] = "-d";
637 argv[argc++] = "10";
639 argv[argc] = NULL;
640 _eval(argv, NULL, 0, &pid);
642 if (!nvram_contains_word("debug_norestart", "radvd")) {
643 pid_radvd = -2;
648 void stop_radvd(void)
650 if (getpid() != 1) {
651 stop_service("radvd");
652 return;
655 pid_radvd = -1;
656 killall_tk("radvd");
659 void start_ipv6(void)
661 int service;
663 service = get_ipv6_service();
664 enable_ip_forward();
666 // Check if turned on
667 switch (service) {
668 case IPV6_NATIVE:
669 case IPV6_6IN4:
670 case IPV6_MANUAL:
671 add_ip6_lanaddr();
672 break;
673 case IPV6_NATIVE_DHCP:
674 case IPV6_ANYCAST_6TO4:
675 nvram_set("ipv6_rtr_addr", "");
676 nvram_set("ipv6_prefix", "");
677 break;
680 if (service != IPV6_DISABLED) {
681 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
682 accept_ra(nvram_safe_get("lan_ifname"));
686 void stop_ipv6(void)
688 stop_ipv6_tunnel();
689 stop_dhcp6c();
690 eval("ip", "-6", "addr", "flush", "scope", "global");
693 #endif
695 // -----------------------------------------------------------------------------
697 void start_upnp(void)
699 if (getpid() != 1) {
700 start_service("upnp");
701 return;
704 if (get_wan_proto() == WP_DISABLED) return;
706 int enable;
707 FILE *f;
708 int upnp_port;
710 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
711 mkdir("/etc/upnp", 0777);
712 if (f_exists("/etc/upnp/config.alt")) {
713 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
715 else {
716 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
717 upnp_port = nvram_get_int("upnp_port");
718 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
721 fprintf(f,
722 "ext_ifname=%s\n"
723 "port=%d\n"
724 "enable_upnp=%s\n"
725 "enable_natpmp=%s\n"
726 "secure_mode=%s\n"
727 "upnp_forward_chain=upnp\n"
728 "upnp_nat_chain=upnp\n"
729 "notify_interval=%d\n"
730 "system_uptime=yes\n"
731 "\n"
733 get_wanface(),
734 upnp_port,
735 (enable & 1) ? "yes" : "no", // upnp enable
736 (enable & 2) ? "yes" : "no", // natpmp enable
737 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
738 nvram_get_int("upnp_ssdp_interval")
741 if (nvram_get_int("upnp_clean")) {
742 int interval = nvram_get_int("upnp_clean_interval");
743 if (interval < 60) interval = 60;
744 fprintf(f,
745 "clean_ruleset_interval=%d\n"
746 "clean_ruleset_threshold=%d\n",
747 interval,
748 nvram_get_int("upnp_clean_threshold")
751 else
752 fprintf(f,"clean_ruleset_interval=0\n");
754 if (nvram_match("upnp_mnp", "1")) {
755 int https = nvram_get_int("https_enable");
756 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
757 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
758 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
760 else {
761 // Empty parameters are not included into XML service description
762 fprintf(f, "presentation_url=\n");
765 char uuid[45];
766 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
767 fprintf(f, "uuid=%s\n", uuid);
769 char lanN_ipaddr[] = "lanXX_ipaddr";
770 char lanN_netmask[] = "lanXX_netmask";
771 char upnp_lanN[] = "upnp_lanXX";
772 char br;
774 for(br=0 ; br<4 ; br++) {
775 char bridge[2] = "0";
776 if (br!=0)
777 bridge[0]+=br;
778 else
779 strcpy(bridge, "");
781 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
782 sprintf(lanN_netmask, "lan%s_netmask", bridge);
783 sprintf(upnp_lanN, "upnp_lan%s", bridge);
785 char *lanip = nvram_safe_get(lanN_ipaddr);
786 char *lanmask = nvram_safe_get(lanN_netmask);
787 char *lanlisten = nvram_safe_get(upnp_lanN);
789 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
790 fprintf(f,
791 "listening_ip=%s/%s\n",
792 lanip, lanmask);
793 int ports[4];
794 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
795 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
796 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
797 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
798 fprintf(f,
799 "allow %d-%d %s/%s %d-%d\n",
800 ports[0], ports[1],
801 lanip, lanmask,
802 ports[2], ports[3]
805 else {
806 // by default allow only redirection of ports above 1024
807 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
812 fappend(f, "/etc/upnp/config.custom");
813 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
814 fclose(f);
816 xstart("miniupnpd", "-f", "/etc/upnp/config");
822 void stop_upnp(void)
824 if (getpid() != 1) {
825 stop_service("upnp");
826 return;
829 killall_tk("miniupnpd");
832 // -----------------------------------------------------------------------------
834 static pid_t pid_crond = -1;
836 void start_cron(void)
838 stop_cron();
840 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
841 if (!nvram_contains_word("debug_norestart", "crond")) {
842 pid_crond = -2;
846 void stop_cron(void)
848 pid_crond = -1;
849 killall_tk("crond");
852 // -----------------------------------------------------------------------------
853 #ifdef LINUX26
855 static pid_t pid_hotplug2 = -1;
857 void start_hotplug2()
859 stop_hotplug2();
861 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
862 xstart("hotplug2", "--persistent", "--no-coldplug");
863 // FIXME: Don't remember exactly why I put "sleep" here -
864 // but it was not for a race with check_services()... - TB
865 sleep(1);
867 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
868 pid_hotplug2 = -2;
872 void stop_hotplug2(void)
874 pid_hotplug2 = -1;
875 killall_tk("hotplug2");
878 #endif /* LINUX26 */
879 // -----------------------------------------------------------------------------
881 // Written by Sparq in 2002/07/16
882 void start_zebra(void)
884 #ifdef TCONFIG_ZEBRA
885 if (getpid() != 1) {
886 start_service("zebra");
887 return;
890 FILE *fp;
892 char *lan_tx = nvram_safe_get("dr_lan_tx");
893 char *lan_rx = nvram_safe_get("dr_lan_rx");
894 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
895 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
896 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
897 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
898 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
899 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
900 char *wan_tx = nvram_safe_get("dr_wan_tx");
901 char *wan_rx = nvram_safe_get("dr_wan_rx");
903 // if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
904 if ((*lan_tx == '0') && (*lan_rx == '0') &&
905 (*lan1_tx == '0') && (*lan1_rx == '0') &&
906 (*lan2_tx == '0') && (*lan2_rx == '0') &&
907 (*lan3_tx == '0') && (*lan3_rx == '0') &&
908 (*wan_tx == '0') && (*wan_rx == '0')) {
909 return;
912 // empty
913 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
914 fclose(fp);
918 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
919 char *lan_ifname = nvram_safe_get("lan_ifname");
920 char *lan1_ifname = nvram_safe_get("lan1_ifname");
921 char *lan2_ifname = nvram_safe_get("lan2_ifname");
922 char *lan3_ifname = nvram_safe_get("lan3_ifname");
923 char *wan_ifname = nvram_safe_get("wan_ifname");
925 fprintf(fp, "router rip\n");
926 if(strcmp(lan_ifname,"")!=0)
927 fprintf(fp, "network %s\n", lan_ifname);
928 if(strcmp(lan1_ifname,"")!=0)
929 fprintf(fp, "network %s\n", lan1_ifname);
930 if(strcmp(lan2_ifname,"")!=0)
931 fprintf(fp, "network %s\n", lan2_ifname);
932 if(strcmp(lan3_ifname,"")!=0)
933 fprintf(fp, "network %s\n", lan3_ifname);
934 fprintf(fp, "network %s\n", wan_ifname);
935 fprintf(fp, "redistribute connected\n");
936 //fprintf(fp, "redistribute static\n");
938 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
939 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
941 if(strcmp(lan_ifname,"")!=0) {
942 fprintf(fp, "interface %s\n", lan_ifname);
943 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
944 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
946 if(strcmp(lan1_ifname,"")!=0) {
947 fprintf(fp, "interface %s\n", lan1_ifname);
948 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
949 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
951 if(strcmp(lan2_ifname,"")!=0) {
952 fprintf(fp, "interface %s\n", lan2_ifname);
953 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
954 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
956 if(strcmp(lan3_ifname,"")!=0) {
957 fprintf(fp, "interface %s\n", lan3_ifname);
958 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
959 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
961 fprintf(fp, "interface %s\n", wan_ifname);
962 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
963 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
965 fprintf(fp, "router rip\n");
966 if(strcmp(lan_ifname,"")!=0) {
967 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
968 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
970 if(strcmp(lan1_ifname,"")!=0) {
971 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
972 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
974 if(strcmp(lan2_ifname,"")!=0) {
975 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
976 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
978 if(strcmp(lan3_ifname,"")!=0) {
979 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
980 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
982 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
983 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
984 fprintf(fp, "access-list private deny any\n");
986 //fprintf(fp, "debug rip events\n");
987 //fprintf(fp, "log file /etc/ripd.log\n");
988 fclose(fp);
991 xstart("zebra", "-d");
992 xstart("ripd", "-d");
993 #endif
996 void stop_zebra(void)
998 #ifdef TCONFIG_ZEBRA
999 if (getpid() != 1) {
1000 stop_service("zebra");
1001 return;
1004 killall("zebra", SIGTERM);
1005 killall("ripd", SIGTERM);
1007 unlink("/etc/zebra.conf");
1008 unlink("/etc/ripd.conf");
1009 #endif
1012 // -----------------------------------------------------------------------------
1014 void start_syslog(void)
1016 char *argv[16];
1017 int argc;
1018 char *nv;
1019 char *b_opt = "";
1020 char rem[256];
1021 int n;
1022 char s[64];
1023 char cfg[256];
1024 char *rot_siz = "50";
1025 char *log_file_path;
1027 argv[0] = "syslogd";
1028 argc = 1;
1030 if (nvram_match("log_remote", "1")) {
1031 nv = nvram_safe_get("log_remoteip");
1032 if (*nv) {
1033 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1034 argv[argc++] = "-R";
1035 argv[argc++] = rem;
1039 if (nvram_match("log_file", "1")) {
1040 argv[argc++] = "-L";
1042 // log to custom path - shibby
1043 if (nvram_match("log_file_custom", "1")) {
1044 log_file_path = nvram_safe_get("log_file_path");
1045 argv[argc++] = "-s";
1046 argv[argc++] = "5000";
1047 argv[argc++] = "-b";
1048 argv[argc++] = "5";
1049 argv[argc++] = "-O";
1050 argv[argc++] = log_file_path;
1051 remove("/var/log/messages");
1052 symlink(log_file_path, "/var/log/messages");
1054 else
1056 /* Read options: rotate_size(kb) num_backups logfilename.
1057 * Ignore these settings and use defaults if the logfile cannot be written to.
1059 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1060 if ((nv = strchr(cfg, '\n')))
1061 *nv = 0;
1063 if ((nv = strtok(cfg, " \t"))) {
1064 if (isdigit(*nv))
1065 rot_siz = nv;
1068 if ((nv = strtok(NULL, " \t")))
1069 b_opt = nv;
1071 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1072 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1073 argv[argc++] = "-O";
1074 argv[argc++] = nv;
1076 else {
1077 rot_siz = "50";
1078 b_opt = "";
1083 if (nvram_match("log_file_custom", "0")) {
1084 argv[argc++] = "-s";
1085 argv[argc++] = rot_siz;
1086 remove("/var/log/messages");
1089 if (isdigit(*b_opt)) {
1090 argv[argc++] = "-b";
1091 argv[argc++] = b_opt;
1095 if (argc > 1) {
1096 argv[argc] = NULL;
1097 _eval(argv, NULL, 0, NULL);
1099 argv[0] = "klogd";
1100 argv[1] = NULL;
1101 _eval(argv, NULL, 0, NULL);
1103 // used to be available in syslogd -m
1104 n = nvram_get_int("log_mark");
1105 if (n > 0) {
1106 // n is in minutes
1107 if (n < 60)
1108 sprintf(rem, "*/%d * * * *", n);
1109 else if (n < 60 * 24)
1110 sprintf(rem, "0 */%d * * *", n / 60);
1111 else
1112 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1113 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1114 eval("cru", "a", "syslogdmark", s);
1116 else {
1117 eval("cru", "d", "syslogdmark");
1122 void stop_syslog(void)
1124 killall("klogd", SIGTERM);
1125 killall("syslogd", SIGTERM);
1128 // -----------------------------------------------------------------------------
1130 static pid_t pid_igmp = -1;
1132 void start_igmp_proxy(void)
1134 FILE *fp;
1136 pid_igmp = -1;
1137 if (nvram_match("multicast_pass", "1")) {
1138 if (get_wan_proto() == WP_DISABLED)
1139 return;
1141 if (f_exists("/etc/igmp.alt")) {
1142 eval("igmpproxy", "/etc/igmp.alt");
1144 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1145 fprintf(fp,
1146 "quickleave\n"
1147 "phyint %s upstream\n"
1148 "\taltnet %s\n",
1149 // "phyint %s downstream ratelimit 0\n",
1150 get_wanface(),
1151 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1152 // nvram_safe_get("lan_ifname"));
1154 char lanN_ifname[] = "lanXX_ifname";
1155 char multicast_lanN[] = "multicast_lanXX";
1156 char br;
1158 for(br=0 ; br<4 ; br++) {
1159 char bridge[2] = "0";
1160 if (br!=0)
1161 bridge[0]+=br;
1162 else
1163 strcpy(bridge, "");
1165 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1166 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1168 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1169 fprintf(fp,
1170 "phyint %s downstream ratelimit 0\n",
1171 nvram_safe_get(lanN_ifname));
1174 fclose(fp);
1175 eval("igmpproxy", "/etc/igmp.conf");
1177 else {
1178 return;
1180 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1181 pid_igmp = -2;
1186 void stop_igmp_proxy(void)
1188 pid_igmp = -1;
1189 killall_tk("igmpproxy");
1192 #ifdef TCONFIG_NOCAT
1194 static pid_t pid_splashd = -1;
1195 void start_splashd(void)
1197 pid_splashd = -1;
1198 start_nocat();
1199 if (!nvram_contains_word("debug_norestart", "splashd")) {
1200 pid_splashd = -2;
1204 void stop_splashd(void)
1206 pid_splashd = -1;
1207 stop_nocat();
1208 start_wan(BOOT);
1210 #endif
1212 // -----------------------------------------------------------------------------
1214 void set_tz(void)
1216 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1219 void start_ntpc(void)
1221 set_tz();
1223 stop_ntpc();
1225 if (nvram_get_int("ntp_updates") >= 0) {
1226 xstart("ntpsync", "--init");
1230 void stop_ntpc(void)
1232 killall("ntpsync", SIGTERM);
1235 // -----------------------------------------------------------------------------
1237 static void stop_rstats(void)
1239 int n;
1240 int pid;
1242 n = 60;
1243 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1244 if (kill(pid, SIGTERM) != 0) break;
1245 sleep(1);
1249 static void start_rstats(int new)
1251 if (nvram_match("rstats_enable", "1")) {
1252 stop_rstats();
1253 if (new) xstart("rstats", "--new");
1254 else xstart("rstats");
1258 // -----------------------------------------------------------------------------
1260 // !!TB - FTP Server
1262 #ifdef TCONFIG_FTP
1263 static char *get_full_storage_path(char *val)
1265 static char buf[128];
1266 int len;
1268 if (val[0] == '/')
1269 len = sprintf(buf, "%s", val);
1270 else
1271 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1273 if (len > 1 && buf[len - 1] == '/')
1274 buf[len - 1] = 0;
1276 return buf;
1279 static char *nvram_storage_path(char *var)
1281 char *val = nvram_safe_get(var);
1282 return get_full_storage_path(val);
1285 char vsftpd_conf[] = "/etc/vsftpd.conf";
1286 char vsftpd_users[] = "/etc/vsftpd.users";
1287 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1289 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1291 static void start_ftpd(void)
1293 char tmp[256];
1294 FILE *fp, *f;
1295 char *buf;
1296 char *p, *q;
1297 char *user, *pass, *rights;
1299 if (getpid() != 1) {
1300 start_service("ftpd");
1301 return;
1304 if (!nvram_get_int("ftp_enable")) return;
1306 mkdir_if_none(vsftpd_users);
1307 mkdir_if_none("/var/run/vsftpd");
1309 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1310 return;
1312 if (nvram_get_int("ftp_super"))
1314 /* rights */
1315 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1316 if ((f = fopen(tmp, "w")))
1318 fprintf(f,
1319 "dirlist_enable=yes\n"
1320 "write_enable=yes\n"
1321 "download_enable=yes\n");
1322 fclose(f);
1326 #ifdef TCONFIG_SAMBASRV
1327 if (nvram_match("smbd_cset", "utf8"))
1328 fprintf(fp, "utf8=yes\n");
1329 #endif
1331 if (nvram_invmatch("ftp_anonymous", "0"))
1333 fprintf(fp,
1334 "anon_allow_writable_root=yes\n"
1335 "anon_world_readable_only=no\n"
1336 "anon_umask=022\n");
1338 /* rights */
1339 sprintf(tmp, "%s/ftp", vsftpd_users);
1340 if ((f = fopen(tmp, "w")))
1342 if (nvram_match("ftp_dirlist", "0"))
1343 fprintf(f, "dirlist_enable=yes\n");
1344 if (nvram_match("ftp_anonymous", "1") ||
1345 nvram_match("ftp_anonymous", "3"))
1346 fprintf(f, "write_enable=yes\n");
1347 if (nvram_match("ftp_anonymous", "1") ||
1348 nvram_match("ftp_anonymous", "2"))
1349 fprintf(f, "download_enable=yes\n");
1350 fclose(f);
1352 if (nvram_match("ftp_anonymous", "1") ||
1353 nvram_match("ftp_anonymous", "3"))
1354 fprintf(fp,
1355 "anon_upload_enable=yes\n"
1356 "anon_mkdir_write_enable=yes\n"
1357 "anon_other_write_enable=yes\n");
1358 } else {
1359 fprintf(fp, "anonymous_enable=no\n");
1362 fprintf(fp,
1363 "dirmessage_enable=yes\n"
1364 "download_enable=no\n"
1365 "dirlist_enable=no\n"
1366 "hide_ids=yes\n"
1367 "syslog_enable=yes\n"
1368 "local_enable=yes\n"
1369 "local_umask=022\n"
1370 "chmod_enable=no\n"
1371 "chroot_local_user=yes\n"
1372 "check_shell=no\n"
1373 "log_ftp_protocol=%s\n"
1374 "user_config_dir=%s\n"
1375 "passwd_file=%s\n"
1376 "listen%s=yes\n"
1377 "listen_port=%s\n"
1378 "background=yes\n"
1379 "isolate=no\n"
1380 "max_clients=%d\n"
1381 "max_per_ip=%d\n"
1382 "max_login_fails=1\n"
1383 "idle_session_timeout=%s\n"
1384 "use_sendfile=no\n"
1385 "anon_max_rate=%d\n"
1386 "local_max_rate=%d\n"
1387 "%s\n",
1388 nvram_get_int("log_ftp") ? "yes" : "no",
1389 vsftpd_users, vsftpd_passwd,
1390 #ifdef TCONFIG_IPV6
1391 ipv6_enabled() ? "_ipv6" : "",
1392 #else
1394 #endif
1395 nvram_get("ftp_port") ? : "21",
1396 nvram_get_int("ftp_max"),
1397 nvram_get_int("ftp_ipmax"),
1398 nvram_get("ftp_staytimeout") ? : "300",
1399 nvram_get_int("ftp_anonrate") * 1024,
1400 nvram_get_int("ftp_rate") * 1024,
1401 nvram_safe_get("ftp_custom"));
1403 fclose(fp);
1405 /* prepare passwd file and default users */
1406 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1407 return;
1409 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1410 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1412 fprintf(fp, /* anonymous, admin, nobody */
1413 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1414 "%s:%s:0:0:root:/:/sbin/nologin\n"
1415 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1416 nvram_storage_path("ftp_anonroot"), user,
1417 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1418 MOUNT_ROOT);
1420 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1423 username<password<rights
1424 rights:
1425 Read/Write
1426 Read Only
1427 View Only
1428 Private
1430 p = buf;
1431 while ((q = strsep(&p, ">")) != NULL) {
1432 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
1433 if (!user || !pass) continue;
1435 /* directory */
1436 if (strncmp(rights, "Private", 7) == 0)
1438 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1439 mkdir_if_none(tmp);
1441 else
1442 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
1444 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1445 user, crypt(pass, "$1$"), user, tmp);
1447 /* rights */
1448 sprintf(tmp, "%s/%s", vsftpd_users, user);
1449 if ((f = fopen(tmp, "w")))
1451 tmp[0] = 0;
1452 if (nvram_invmatch("ftp_dirlist", "1"))
1453 strcat(tmp, "dirlist_enable=yes\n");
1454 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1455 strcat(tmp, "download_enable=yes\n");
1456 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1457 strcat(tmp, "write_enable=yes\n");
1459 fputs(tmp, f);
1460 fclose(f);
1463 free(buf);
1466 fclose(fp);
1467 killall("vsftpd", SIGHUP);
1469 /* start vsftpd if it's not already running */
1470 if (pidof("vsftpd") <= 0)
1471 xstart("vsftpd");
1474 static void stop_ftpd(void)
1476 if (getpid() != 1) {
1477 stop_service("ftpd");
1478 return;
1481 killall_tk("vsftpd");
1482 unlink(vsftpd_passwd);
1483 unlink(vsftpd_conf);
1484 eval("rm", "-rf", vsftpd_users);
1486 #endif // TCONFIG_FTP
1488 // -----------------------------------------------------------------------------
1490 // !!TB - Samba
1492 #ifdef TCONFIG_SAMBASRV
1493 static void kill_samba(int sig)
1495 if (sig == SIGTERM) {
1496 killall_tk("smbd");
1497 killall_tk("nmbd");
1499 else {
1500 killall("smbd", sig);
1501 killall("nmbd", sig);
1505 static void start_samba(void)
1507 FILE *fp;
1508 DIR *dir = NULL;
1509 struct dirent *dp;
1510 char nlsmod[15];
1511 int mode;
1512 char *nv;
1514 if (getpid() != 1) {
1515 start_service("smbd");
1516 return;
1519 mode = nvram_get_int("smbd_enable");
1520 if (!mode || !nvram_invmatch("lan_hostname", ""))
1521 return;
1523 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1524 return;
1526 fprintf(fp, "[global]\n"
1527 " interfaces = %s\n"
1528 " bind interfaces only = yes\n"
1529 " workgroup = %s\n"
1530 " netbios name = %s\n"
1531 " server string = %s\n"
1532 " guest account = nobody\n"
1533 " security = user\n"
1534 " %s\n"
1535 " guest ok = %s\n"
1536 " guest only = no\n"
1537 " browseable = yes\n"
1538 " syslog only = yes\n"
1539 " timestamp logs = no\n"
1540 " syslog = 1\n"
1541 " encrypt passwords = yes\n"
1542 " preserve case = yes\n"
1543 " short preserve case = yes\n",
1544 nvram_safe_get("lan_ifname"),
1545 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1546 nvram_safe_get("lan_hostname"),
1547 nvram_get("router_name") ? : "Tomato",
1548 mode == 2 ? "" : "map to guest = Bad User",
1549 mode == 2 ? "no" : "yes" // guest ok
1552 if (nvram_get_int("smbd_wins")) {
1553 nv = nvram_safe_get("wan_wins");
1554 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1555 fprintf(fp, " wins support = yes\n");
1559 if (nvram_get_int("smbd_master")) {
1560 fprintf(fp,
1561 " domain master = yes\n"
1562 " local master = yes\n"
1563 " preferred master = yes\n"
1564 " os level = 65\n");
1567 nv = nvram_safe_get("smbd_cpage");
1568 if (*nv) {
1569 #ifndef TCONFIG_SAMBA3
1570 fprintf(fp, " client code page = %s\n", nv);
1571 #endif
1572 sprintf(nlsmod, "nls_cp%s", nv);
1574 nv = nvram_safe_get("smbd_nlsmod");
1575 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1576 modprobe_r(nv);
1578 modprobe(nlsmod);
1579 nvram_set("smbd_nlsmod", nlsmod);
1582 #ifndef TCONFIG_SAMBA3
1583 if (nvram_match("smbd_cset", "utf8"))
1584 fprintf(fp, " coding system = utf8\n");
1585 else if (nvram_invmatch("smbd_cset", ""))
1586 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1587 #endif
1589 nv = nvram_safe_get("smbd_custom");
1590 /* add socket options unless overriden by the user */
1591 if (strstr(nv, "socket options") == NULL) {
1592 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1594 fprintf(fp, "%s\n\n", nv);
1596 /* configure shares */
1598 char *buf;
1599 char *p, *q;
1600 char *name, *path, *comment, *writeable, *hidden;
1601 int cnt = 0;
1603 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1605 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1607 p = buf;
1608 while ((q = strsep(&p, ">")) != NULL) {
1609 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1610 if (!path || !name) continue;
1612 /* share name */
1613 fprintf(fp, "\n[%s]\n", name);
1615 /* path */
1616 fprintf(fp, " path = %s\n", path);
1618 /* access level */
1619 if (!strcmp(writeable, "1"))
1620 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1621 if (!strcmp(hidden, "1"))
1622 fprintf(fp, " browseable = no\n");
1624 /* comment */
1625 if (comment)
1626 fprintf(fp, " comment = %s\n", comment);
1628 cnt++;
1630 free(buf);
1633 /* Share every mountpoint below MOUNT_ROOT */
1634 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1635 while ((dp = readdir(dir))) {
1636 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1638 /* Only if is a directory and is mounted */
1639 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
1640 continue;
1642 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1643 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1644 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1645 if (nvram_match("smbd_autoshare", "3")) // Hidden
1646 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1647 dp->d_name, MOUNT_ROOT, dp->d_name);
1648 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1649 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1651 cnt++;
1655 if (dir) closedir(dir);
1657 if (cnt == 0) {
1658 /* by default share MOUNT_ROOT as read-only */
1659 fprintf(fp, "\n[share]\n"
1660 " path = %s\n"
1661 " writable = no\n",
1662 MOUNT_ROOT);
1665 fclose(fp);
1667 mkdir_if_none("/var/run/samba");
1668 mkdir_if_none("/etc/samba");
1670 /* write smbpasswd */
1671 #ifdef TCONFIG_SAMBA3
1672 eval("smbpasswd", "nobody", "\"\"");
1673 #else
1674 eval("smbpasswd", "-a", "nobody", "\"\"");
1675 #endif
1676 if (mode == 2) {
1677 char *smbd_user;
1678 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1679 smbd_user = "nas";
1680 #ifdef TCONFIG_SAMBA3
1681 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1682 #else
1683 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1684 #endif
1687 kill_samba(SIGHUP);
1688 int ret1 = 0, ret2 = 0;
1689 /* start samba if it's not already running */
1690 if (pidof("nmbd") <= 0)
1691 ret1 = xstart("nmbd", "-D");
1692 if (pidof("smbd") <= 0)
1693 ret2 = xstart("smbd", "-D");
1695 if (ret1 || ret2) kill_samba(SIGTERM);
1698 static void stop_samba(void)
1700 if (getpid() != 1) {
1701 stop_service("smbd");
1702 return;
1705 kill_samba(SIGTERM);
1706 /* clean up */
1707 unlink("/var/log/smb");
1708 unlink("/var/log/nmb");
1709 eval("rm", "-rf", "/var/run/samba");
1711 #endif // TCONFIG_SAMBASRV
1713 #ifdef TCONFIG_MEDIA_SERVER
1714 #define MEDIA_SERVER_APP "minidlna"
1716 static void start_media_server(void)
1718 FILE *f;
1719 int port, pid, https;
1720 char *dbdir;
1721 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1722 static int once = 1;
1724 if (getpid() != 1) {
1725 start_service("media");
1726 return;
1729 if (nvram_get_int("ms_sas") == 0)
1730 once = 0;
1732 if (nvram_get_int("ms_enable") != 0) {
1733 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1734 // no forced rescan
1735 argv[3] = NULL;
1737 nvram_unset("ms_rescan");
1739 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1740 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1742 else {
1743 if ((f = fopen(argv[2], "w")) != NULL) {
1744 port = nvram_get_int("ms_port");
1745 https = nvram_get_int("https_enable");
1746 dbdir = nvram_safe_get("ms_dbdir");
1747 if (!(*dbdir)) dbdir = NULL;
1748 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1750 fprintf(f,
1751 "network_interface=%s\n"
1752 "port=%d\n"
1753 "friendly_name=%s\n"
1754 "db_dir=%s/.db\n"
1755 "enable_tivo=%s\n"
1756 "strict_dlna=%s\n"
1757 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1758 "inotify=yes\n"
1759 "notify_interval=600\n"
1760 "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"
1761 "\n",
1762 nvram_safe_get("lan_ifname"),
1763 (port < 0) || (port >= 0xffff) ? 0 : port,
1764 nvram_get("router_name") ? : "Tomato",
1765 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1766 nvram_get_int("ms_tivo") ? "yes" : "no",
1767 nvram_get_int("ms_stdlna") ? "yes" : "no",
1768 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1771 // media directories
1772 char *buf, *p, *q;
1773 char *path, *restrict;
1775 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1776 /* path<restrict[A|V|P|] */
1778 p = buf;
1779 while ((q = strsep(&p, ">")) != NULL) {
1780 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1781 continue;
1782 fprintf(f, "media_dir=%s%s%s\n",
1783 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1785 free(buf);
1788 fclose(f);
1792 /* start media server if it's not already running */
1793 if (pidof(MEDIA_SERVER_APP) <= 0) {
1794 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1795 /* If we started the media server successfully, wait 1 sec
1796 * to let it die if it can't open the database file.
1797 * If it's still alive after that, assume it's running and
1798 * disable forced once-after-reboot rescan.
1800 sleep(1);
1801 if (pidof(MEDIA_SERVER_APP) > 0)
1802 once = 0;
1808 static void stop_media_server(void)
1810 if (getpid() != 1) {
1811 stop_service("media");
1812 return;
1815 killall_tk(MEDIA_SERVER_APP);
1817 #endif // TCONFIG_MEDIA_SERVER
1819 #ifdef TCONFIG_USB
1820 static void start_nas_services(void)
1822 if (getpid() != 1) {
1823 start_service("usbapps");
1824 return;
1827 #ifdef TCONFIG_SAMBASRV
1828 start_samba();
1829 #endif
1830 #ifdef TCONFIG_FTP
1831 start_ftpd();
1832 #endif
1833 #ifdef TCONFIG_MEDIA_SERVER
1834 start_media_server();
1835 #endif
1838 static void stop_nas_services(void)
1840 if (getpid() != 1) {
1841 stop_service("usbapps");
1842 return;
1845 #ifdef TCONFIG_MEDIA_SERVER
1846 stop_media_server();
1847 #endif
1848 #ifdef TCONFIG_FTP
1849 stop_ftpd();
1850 #endif
1851 #ifdef TCONFIG_SAMBASRV
1852 stop_samba();
1853 #endif
1856 void restart_nas_services(int stop, int start)
1858 int fd = file_lock("usb");
1859 /* restart all NAS applications */
1860 if (stop)
1861 stop_nas_services();
1862 if (start)
1863 start_nas_services();
1864 file_unlock(fd);
1866 #endif // TCONFIG_USB
1868 // -----------------------------------------------------------------------------
1870 /* -1 = Don't check for this program, it is not expected to be running.
1871 * Other = This program has been started and should be kept running. If no
1872 * process with the name is running, call func to restart it.
1873 * Note: At startup, dnsmasq forks a short-lived child which forks a
1874 * long-lived (grand)child. The parents terminate.
1875 * Many daemons use this technique.
1877 static void _check(pid_t pid, const char *name, void (*func)(void))
1879 if (pid == -1) return;
1881 if (pidof(name) > 0) return;
1883 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
1884 func();
1886 // Force recheck in 500 msec
1887 setitimer(ITIMER_REAL, &pop_tv, NULL);
1890 void check_services(void)
1892 TRACE_PT("keep alive\n");
1894 // Periodically reap any zombies
1895 setitimer(ITIMER_REAL, &zombie_tv, NULL);
1897 #ifdef LINUX26
1898 _check(pid_hotplug2, "hotplug2", start_hotplug2);
1899 #endif
1900 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
1901 _check(pid_crond, "crond", start_cron);
1902 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
1903 #ifdef TCONFIG_IPV6
1904 _check(pid_radvd, "radvd", start_radvd);
1905 #endif
1907 //#ifdef TCONFIG_NOCAT
1908 // if (nvram_get_int("NC_enable"))
1909 // _check(&pid_splashd, "splashd", start_splashd);
1910 //#endif
1914 // -----------------------------------------------------------------------------
1916 void start_services(void)
1918 static int once = 1;
1920 if (once) {
1921 once = 0;
1923 if (nvram_get_int("telnetd_eas")) start_telnetd();
1924 if (nvram_get_int("sshd_eas")) start_sshd();
1927 // start_syslog();
1928 start_nas();
1929 start_zebra();
1930 start_dnsmasq();
1931 start_cifs();
1932 start_httpd();
1933 start_cron();
1934 // start_upnp();
1935 start_rstats(0);
1936 start_sched();
1937 #ifdef TCONFIG_IPV6
1938 /* note: starting radvd here might be too early in case of
1939 * DHCPv6 or 6to4 because we won't have received a prefix and
1940 * so it will disable advertisements. To restart them, we have
1941 * to send radvd a SIGHUP, or restart it.
1943 start_radvd();
1944 #endif
1945 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1947 #ifdef TCONFIG_SNMP
1948 start_snmp();
1949 #endif
1951 #ifdef TCONFIG_BT
1952 start_bittorrent();
1953 #endif
1955 #ifdef TCONFIG_NFS
1956 start_nfs();
1957 #endif
1960 void stop_services(void)
1962 clear_resolv();
1964 #ifdef TCONFIG_BT
1965 stop_bittorrent();
1966 #endif
1968 #ifdef TCONFIG_SNMP
1969 stop_snmp();
1970 #endif
1972 #ifdef TCONFIG_NFS
1973 stop_nfs();
1974 #endif
1975 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1976 #ifdef TCONFIG_IPV6
1977 stop_radvd();
1978 #endif
1979 stop_sched();
1980 stop_rstats();
1981 // stop_upnp();
1982 stop_cron();
1983 stop_httpd();
1984 stop_cifs();
1985 stop_dnsmasq();
1986 stop_zebra();
1987 stop_nas();
1988 // stop_syslog();
1991 // -----------------------------------------------------------------------------
1993 /* nvram "action_service" is: "service-action[-modifier]"
1994 * action is something like "stop" or "start" or "restart"
1995 * optional modifier is "c" for the "service" command-line command
1997 void exec_service(void)
1999 const int A_START = 1;
2000 const int A_STOP = 2;
2001 const int A_RESTART = 1|2;
2002 char buffer[128];
2003 char *service;
2004 char *act;
2005 char *next;
2006 char *modifier;
2007 int action, user;
2008 int i;
2010 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2011 next = buffer;
2013 TOP:
2014 act = strsep(&next, ",");
2015 service = strsep(&act, "-");
2016 if (act == NULL) {
2017 next = NULL;
2018 goto CLEAR;
2020 modifier = act;
2021 strsep(&modifier, "-");
2023 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2025 if (strcmp(act, "start") == 0) action = A_START;
2026 else if (strcmp(act, "stop") == 0) action = A_STOP;
2027 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2028 else action = 0;
2029 user = (modifier != NULL && *modifier == 'c');
2031 if (strcmp(service, "dhcpc") == 0) {
2032 if (action & A_STOP) stop_dhcpc();
2033 if (action & A_START) start_dhcpc();
2034 goto CLEAR;
2037 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2038 if (action & A_STOP) stop_dnsmasq();
2039 if (action & A_START) {
2040 dns_to_resolv();
2041 start_dnsmasq();
2043 goto CLEAR;
2046 if (strcmp(service, "firewall") == 0) {
2047 if (action & A_STOP) {
2048 stop_firewall();
2049 stop_igmp_proxy();
2051 if (action & A_START) {
2052 start_firewall();
2053 start_igmp_proxy();
2055 goto CLEAR;
2058 if (strcmp(service, "restrict") == 0) {
2059 if (action & A_STOP) {
2060 stop_firewall();
2062 if (action & A_START) {
2063 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2065 start_firewall();
2067 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2068 if (i == 1) {
2069 if (nvram_get_int("rrules_radio") < 0) {
2070 eval("radio", "on");
2074 goto CLEAR;
2077 if (strcmp(service, "qos") == 0) {
2078 if (action & A_STOP) {
2079 stop_qos();
2081 stop_firewall(); start_firewall(); // always restarted
2082 if (action & A_START) {
2083 start_qos();
2084 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2086 goto CLEAR;
2089 if (strcmp(service, "qoslimit") == 0) {
2090 if (action & A_STOP) {
2091 new_qoslimit_stop();
2093 stop_firewall(); start_firewall(); // always restarted
2094 if (action & A_START) {
2095 new_qoslimit_start();
2097 goto CLEAR;
2100 if (strcmp(service, "arpbind") == 0) {
2101 if (action & A_STOP) stop_arpbind();
2102 if (action & A_START) start_arpbind();
2103 goto CLEAR;
2106 if (strcmp(service, "bwclimon") == 0) {
2107 if (action & A_STOP) stop_bwclimon();
2108 if (action & A_START) start_bwclimon();
2109 goto CLEAR;
2112 if (strcmp(service, "upnp") == 0) {
2113 if (action & A_STOP) {
2114 stop_upnp();
2116 stop_firewall(); start_firewall(); // always restarted
2117 if (action & A_START) {
2118 start_upnp();
2120 goto CLEAR;
2123 if (strcmp(service, "telnetd") == 0) {
2124 if (action & A_STOP) stop_telnetd();
2125 if (action & A_START) start_telnetd();
2126 goto CLEAR;
2129 if (strcmp(service, "sshd") == 0) {
2130 if (action & A_STOP) stop_sshd();
2131 if (action & A_START) start_sshd();
2132 goto CLEAR;
2135 if (strcmp(service, "httpd") == 0) {
2136 if (action & A_STOP) stop_httpd();
2137 if (action & A_START) start_httpd();
2138 goto CLEAR;
2141 #ifdef TCONFIG_IPV6
2142 if (strcmp(service, "ipv6") == 0) {
2143 if (action & A_STOP) {
2144 stop_radvd();
2145 stop_ipv6();
2147 if (action & A_START) {
2148 start_ipv6();
2149 start_radvd();
2151 goto CLEAR;
2154 if (strcmp(service, "radvd") == 0) {
2155 if (action & A_STOP) {
2156 stop_radvd();
2158 if (action & A_START) {
2159 start_radvd();
2161 goto CLEAR;
2164 if (strncmp(service, "dhcp6", 5) == 0) {
2165 if (action & A_STOP) {
2166 stop_dhcp6c();
2168 if (action & A_START) {
2169 start_dhcp6c();
2171 goto CLEAR;
2173 #endif
2175 if (strcmp(service, "admin") == 0) {
2176 if (action & A_STOP) {
2177 stop_sshd();
2178 stop_telnetd();
2179 stop_httpd();
2181 stop_firewall(); start_firewall(); // always restarted
2182 if (action & A_START) {
2183 start_httpd();
2184 create_passwd();
2185 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2186 if (nvram_match("sshd_eas", "1")) start_sshd();
2188 goto CLEAR;
2191 if (strcmp(service, "ddns") == 0) {
2192 if (action & A_STOP) stop_ddns();
2193 if (action & A_START) start_ddns();
2194 goto CLEAR;
2197 if (strcmp(service, "ntpc") == 0) {
2198 if (action & A_STOP) stop_ntpc();
2199 if (action & A_START) start_ntpc();
2200 goto CLEAR;
2203 if (strcmp(service, "logging") == 0) {
2204 if (action & A_STOP) {
2205 stop_syslog();
2207 if (action & A_START) {
2208 start_syslog();
2210 if (!user) {
2211 // always restarted except from "service" command
2212 stop_cron(); start_cron();
2213 stop_firewall(); start_firewall();
2215 goto CLEAR;
2218 if (strcmp(service, "crond") == 0) {
2219 if (action & A_STOP) {
2220 stop_cron();
2222 if (action & A_START) {
2223 start_cron();
2225 goto CLEAR;
2228 #ifdef LINUX26
2229 if (strncmp(service, "hotplug", 7) == 0) {
2230 if (action & A_STOP) {
2231 stop_hotplug2();
2233 if (action & A_START) {
2234 start_hotplug2(1);
2236 goto CLEAR;
2238 #endif
2240 if (strcmp(service, "upgrade") == 0) {
2241 if (action & A_START) {
2242 #if TOMATO_SL
2243 stop_usbevent();
2244 stop_smbd();
2245 #endif
2246 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2247 stop_jffs2();
2248 // stop_cifs();
2249 stop_zebra();
2250 stop_cron();
2251 stop_ntpc();
2252 stop_upnp();
2253 // stop_dhcpc();
2254 killall("rstats", SIGTERM);
2255 killall("buttons", SIGTERM);
2256 stop_syslog();
2257 remove_storage_main(1); // !!TB - USB Support
2258 stop_usb(); // !!TB - USB Support
2260 goto CLEAR;
2263 #ifdef TCONFIG_CIFS
2264 if (strcmp(service, "cifs") == 0) {
2265 if (action & A_STOP) stop_cifs();
2266 if (action & A_START) start_cifs();
2267 goto CLEAR;
2269 #endif
2271 #ifdef TCONFIG_JFFS2
2272 if (strncmp(service, "jffs", 4) == 0) {
2273 if (action & A_STOP) stop_jffs2();
2274 if (action & A_START) start_jffs2();
2275 goto CLEAR;
2277 #endif
2279 if (strcmp(service, "zebra") == 0) {
2280 if (action & A_STOP) stop_zebra();
2281 if (action & A_START) start_zebra();
2282 goto CLEAR;
2285 if (strcmp(service, "routing") == 0) {
2286 if (action & A_STOP) {
2287 stop_zebra();
2288 do_static_routes(0); // remove old '_saved'
2289 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2290 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2291 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2292 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2293 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2294 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2295 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2297 stop_firewall();
2298 start_firewall();
2299 if (action & A_START) {
2300 do_static_routes(1); // add new
2301 start_zebra();
2302 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2303 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2304 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2305 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2306 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2307 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2308 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2310 goto CLEAR;
2313 if (strcmp(service, "ctnf") == 0) {
2314 if (action & A_START) {
2315 setup_conntrack();
2316 stop_firewall();
2317 start_firewall();
2319 goto CLEAR;
2322 if (strcmp(service, "wan") == 0) {
2323 if (action & A_STOP) {
2324 stop_wan();
2327 if (action & A_START) {
2328 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2329 start_wan(BOOT);
2330 sleep(2);
2331 force_to_dial();
2333 goto CLEAR;
2336 if (strcmp(service, "net") == 0) {
2337 if (action & A_STOP) {
2338 #ifdef TCONFIG_USB
2339 stop_nas_services();
2340 #endif
2341 #ifdef TCONFIG_IPV6
2342 stop_radvd();
2343 #endif
2344 stop_httpd();
2345 stop_dnsmasq();
2346 stop_nas();
2347 stop_wan();
2348 stop_arpbind();
2349 stop_lan();
2350 stop_vlan();
2352 if (action & A_START) {
2353 start_vlan();
2354 start_lan();
2355 start_arpbind();
2356 start_wan(BOOT);
2357 start_nas();
2358 start_dnsmasq();
2359 start_httpd();
2360 #ifdef TCONFIG_IPV6
2361 start_radvd();
2362 #endif
2363 start_wl();
2364 #ifdef TCONFIG_USB
2365 start_nas_services();
2366 #endif
2368 goto CLEAR;
2371 if (strcmp(service, "nas") == 0) {
2372 if (action & A_STOP) {
2373 stop_nas();
2375 if (action & A_START) {
2376 start_nas();
2377 start_wl();
2379 goto CLEAR;
2382 if (strcmp(service, "rstats") == 0) {
2383 if (action & A_STOP) stop_rstats();
2384 if (action & A_START) start_rstats(0);
2385 goto CLEAR;
2388 if (strcmp(service, "rstatsnew") == 0) {
2389 if (action & A_STOP) stop_rstats();
2390 if (action & A_START) start_rstats(1);
2391 goto CLEAR;
2394 if (strcmp(service, "sched") == 0) {
2395 if (action & A_STOP) stop_sched();
2396 if (action & A_START) start_sched();
2397 goto CLEAR;
2400 #ifdef TCONFIG_BT
2401 if (strcmp(service, "bittorrent") == 0) {
2402 if (action & A_STOP) {
2403 stop_bittorrent();
2405 stop_firewall(); start_firewall(); // always restarted
2406 if (action & A_START) {
2407 start_bittorrent();
2409 goto CLEAR;
2411 #endif
2413 #ifdef TCONFIG_NFS
2414 if (strcmp(service, "nfs") == 0) {
2415 if (action & A_STOP) stop_nfs();
2416 if (action & A_START) start_nfs();
2417 goto CLEAR;
2419 #endif
2421 #ifdef TCONFIG_SNMP
2422 if (strcmp(service, "snmp") == 0) {
2423 if (action & A_STOP) stop_snmp();
2424 if (action & A_START) start_snmp();
2425 goto CLEAR;
2427 #endif
2429 #ifdef TCONFIG_USB
2430 // !!TB - USB Support
2431 if (strcmp(service, "usb") == 0) {
2432 if (action & A_STOP) stop_usb();
2433 if (action & A_START) {
2434 start_usb();
2435 // restart Samba and ftp since they may be killed by stop_usb()
2436 restart_nas_services(0, 1);
2437 // remount all partitions by simulating hotplug event
2438 add_remove_usbhost("-1", 1);
2440 goto CLEAR;
2443 if (strcmp(service, "usbapps") == 0) {
2444 if (action & A_STOP) stop_nas_services();
2445 if (action & A_START) start_nas_services();
2446 goto CLEAR;
2448 #endif
2450 #ifdef TCONFIG_FTP
2451 // !!TB - FTP Server
2452 if (strcmp(service, "ftpd") == 0) {
2453 if (action & A_STOP) stop_ftpd();
2454 setup_conntrack();
2455 stop_firewall();
2456 start_firewall();
2457 if (action & A_START) start_ftpd();
2458 goto CLEAR;
2460 #endif
2462 #ifdef TCONFIG_MEDIA_SERVER
2463 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2464 if (action & A_STOP) stop_media_server();
2465 if (action & A_START) start_media_server();
2466 goto CLEAR;
2468 #endif
2470 #ifdef TCONFIG_SAMBASRV
2471 // !!TB - Samba
2472 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2473 if (action & A_STOP) stop_samba();
2474 if (action & A_START) {
2475 create_passwd();
2476 stop_dnsmasq();
2477 start_dnsmasq();
2478 start_samba();
2480 goto CLEAR;
2482 #endif
2484 #ifdef TCONFIG_OPENVPN
2485 if (strncmp(service, "vpnclient", 9) == 0) {
2486 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2487 if (action & A_START) start_vpnclient(atoi(&service[9]));
2488 goto CLEAR;
2491 if (strncmp(service, "vpnserver", 9) == 0) {
2492 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2493 if (action & A_START) start_vpnserver(atoi(&service[9]));
2494 goto CLEAR;
2496 #endif
2498 #ifdef TCONFIG_NOCAT
2499 if (strcmp(service, "splashd") == 0) {
2500 if (action & A_STOP) stop_splashd();
2501 if (action & A_START) start_splashd();
2502 goto CLEAR;
2504 #endif
2506 CLEAR:
2507 if (next) goto TOP;
2509 // some functions check action_service and must be cleared at end -- zzz
2510 nvram_set("action_service", "");
2512 // Force recheck in 500 msec
2513 setitimer(ITIMER_REAL, &pop_tv, NULL);
2516 static void do_service(const char *name, const char *action, int user)
2518 int n;
2519 char s[64];
2521 n = 150;
2522 while (!nvram_match("action_service", "")) {
2523 if (user) {
2524 putchar('*');
2525 fflush(stdout);
2527 else if (--n < 0) break;
2528 usleep(100 * 1000);
2531 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2532 nvram_set("action_service", s);
2533 kill(1, SIGUSR1);
2535 n = 150;
2536 while (nvram_match("action_service", s)) {
2537 if (user) {
2538 putchar('.');
2539 fflush(stdout);
2541 else if (--n < 0) {
2542 break;
2544 usleep(100 * 1000);
2548 int service_main(int argc, char *argv[])
2550 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2551 do_service(argv[1], argv[2], 1);
2552 printf("\nDone.\n");
2553 return 0;
2556 void start_service(const char *name)
2558 do_service(name, "start", 0);
2561 void stop_service(const char *name)
2563 do_service(name, "stop", 0);
2567 void restart_service(const char *name)
2569 do_service(name, "restart", 0);