fix IPTraffic rates (account service was started twice)
[tomato.git] / release / src / router / rc / services.c
blobeb568ec06a42fec2e3b1d50950860b8e11505e92
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 if (nvram_get_int("dhcpd_static_only")) {
151 fprintf(f, "dhcp-ignore=tag:!known\n");
154 // dhcp
155 do_dhcpd_hosts=0;
156 char lanN_proto[] = "lanXX_proto";
157 char lanN_ifname[] = "lanXX_ifname";
158 char lanN_ipaddr[] = "lanXX_ipaddr";
159 char lanN_netmask[] = "lanXX_netmask";
160 char dhcpdN_startip[] = "dhcpdXX_startip";
161 char dhcpdN_endip[] = "dhcpdXX_endip";
162 char dhcpN_start[] = "dhcpXX_start";
163 char dhcpN_num[] = "dhcpXX_num";
164 char dhcpN_lease[] = "dhcpXX_lease";
165 char br;
166 for(br=0 ; br<=3 ; br++) {
167 char bridge[2] = "0";
168 if (br!=0)
169 bridge[0]+=br;
170 else
171 strcpy(bridge, "");
173 sprintf(lanN_proto, "lan%s_proto", bridge);
174 sprintf(lanN_ifname, "lan%s_ifname", bridge);
175 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
176 do_dhcpd = nvram_match(lanN_proto, "dhcp");
177 if (do_dhcpd) {
178 do_dhcpd_hosts++;
180 router_ip = nvram_safe_get(lanN_ipaddr);
181 strlcpy(lan, router_ip, sizeof(lan));
182 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
184 fprintf(f,
185 "interface=%s\n",
186 nvram_safe_get(lanN_ifname));
188 sprintf(dhcpN_lease, "dhcp%s_lease", bridge);
189 dhcp_lease = nvram_get_int(dhcpN_lease);
191 if (dhcp_lease <= 0) dhcp_lease = 1440;
193 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
194 if (n < 0) strcpy(sdhcp_lease, "infinite");
195 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
197 if (!do_dns) {
198 // if not using dnsmasq for dns
200 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
201 // no DNS might be temporary. use a low lease time to force clients to update.
202 dhcp_lease = 2;
203 strcpy(sdhcp_lease, "2m");
204 do_dns = 1;
206 else {
207 // pass the dns directly
208 buf[0] = 0;
209 for (n = 0 ; n < dns->count; ++n) {
210 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
211 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
214 fprintf(f, "dhcp-option=%s,6%s\n", nvram_safe_get(lanN_ifname), buf);
218 sprintf(dhcpdN_startip, "dhcpd%s_startip", bridge);
219 sprintf(dhcpdN_endip, "dhcpd%s_endip", bridge);
220 sprintf(lanN_netmask, "lan%s_netmask", bridge);
222 if ((p = nvram_get(dhcpdN_startip)) && (*p) && (e = nvram_get(dhcpdN_endip)) && (*e)) {
223 fprintf(f, "dhcp-range=%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname), p, e, nvram_safe_get(lanN_netmask), dhcp_lease);
225 else {
226 // for compatibility
227 sprintf(dhcpN_start, "dhcp%s_start", bridge);
228 sprintf(dhcpN_num, "dhcp%s_num", bridge);
229 sprintf(lanN_netmask, "lan%s_netmask", bridge);
230 dhcp_start = nvram_get_int(dhcpN_start);
231 dhcp_count = nvram_get_int(dhcpN_num);
232 fprintf(f, "dhcp-range=%s,%s%d,%s%d,%s,%dm\n",
233 nvram_safe_get(lanN_ifname), lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(lanN_netmask), dhcp_lease);
236 nv = nvram_safe_get(lanN_ipaddr);
237 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
238 p = nvram_safe_get("lan_gateway");
239 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
242 fprintf(f,
243 "dhcp-option=%s,3,%s\n", // gateway
244 nvram_safe_get(lanN_ifname), nv);
246 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
247 fprintf(f, "dhcp-option=%s,44,%s\n", nvram_safe_get(lanN_ifname), nv);
249 #ifdef TCONFIG_SAMBASRV
250 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
251 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
252 // Samba will serve as a WINS server
253 fprintf(f, "dhcp-option=%s,44,0.0.0.0\n", nvram_safe_get(lanN_ifname));
256 #endif
257 } else {
258 if (strcmp(nvram_safe_get(lanN_ifname),"")!=0)
259 fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname));
262 // write static lease entries & create hosts file
264 mkdir_if_none(dmhosts);
265 snprintf(buf, sizeof(buf), "%s/hosts", dmhosts);
266 if ((hf = fopen(buf, "w")) != NULL) {
267 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
268 fprintf(hf, "%s %s\n", router_ip, nv);
269 #ifdef TCONFIG_SAMBASRV
270 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
271 fprintf(hf, "%s %s\n", router_ip, nv);
272 #endif
273 p = (char *)get_wanip();
274 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
275 p = "127.0.0.1";
276 fprintf(hf, "%s wan-ip\n", p);
277 if (nv && (*nv))
278 fprintf(hf, "%s %s-wan\n", p, nv);
281 mkdir_if_none(dmdhcp);
282 snprintf(buf, sizeof(buf), "%s/dhcp-hosts", dmdhcp);
283 df = fopen(buf, "w");
285 // PREVIOUS/OLD FORMAT:
286 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
287 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
288 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
290 // NEW FORMAT (+static ARP binding after hostname):
291 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 55 w/ delim
292 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
293 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 108 w/ delim
295 p = nvram_safe_get("dhcpd_static");
296 while ((e = strchr(p, '>')) != NULL) {
297 n = (e - p);
298 if (n > 107) {
299 p = e + 1;
300 continue;
303 strncpy(buf, p, n);
304 buf[n] = 0;
305 p = e + 1;
307 if ((e = strchr(buf, '<')) == NULL) continue;
308 *e = 0;
309 mac = buf;
311 ip = e + 1;
312 if ((e = strchr(ip, '<')) == NULL) continue;
313 *e = 0;
314 if (strchr(ip, '.') == NULL) {
315 ipn = atoi(ip);
316 if ((ipn <= 0) || (ipn > 255)) continue;
317 sprintf(ipbuf, "%s%d", lan, ipn);
318 ip = ipbuf;
320 else {
321 if (inet_addr(ip) == INADDR_NONE) continue;
324 name = e + 1;
326 if ((e = strchr(name, '<')) != NULL) {
327 *e = 0;
330 if ((hf) && (*name != 0)) {
331 fprintf(hf, "%s %s\n", ip, name);
334 if ((do_dhcpd_hosts > 0) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
335 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
339 if (df) fclose(df);
340 if (hf) fclose(hf);
342 n = nvram_get_int("dhcpd_lmax");
343 fprintf(f,
344 "dhcp-lease-max=%d\n",
345 (n > 0) ? n : 255);
346 if (nvram_get_int("dhcpd_auth") >= 0) {
347 fprintf(f, "dhcp-authoritative\n");
352 #ifdef TCONFIG_OPENVPN
353 write_vpn_dnsmasq_config(f);
354 #endif
356 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
358 fappend(f, "/etc/dnsmasq.custom");
362 fclose(f);
364 if (do_dns) {
365 unlink("/etc/resolv.conf");
366 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
369 TRACE_PT("run dnsmasq\n");
371 // Default to some values we like, but allow the user to override them.
372 eval("dnsmasq", "-c", "1500", "--log-async");
374 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
375 pid_dnsmasq = -2;
378 TRACE_PT("end\n");
381 void stop_dnsmasq(void)
383 TRACE_PT("begin\n");
385 if (getpid() != 1) {
386 stop_service("dnsmasq");
387 return;
390 pid_dnsmasq = -1;
392 unlink("/etc/resolv.conf");
393 symlink(dmresolv, "/etc/resolv.conf");
395 killall_tk("dnsmasq");
397 TRACE_PT("end\n");
400 void clear_resolv(void)
402 f_write(dmresolv, NULL, 0, 0, 0); // blank
405 #ifdef TCONFIG_IPV6
406 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
408 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
409 struct in6_addr addr;
410 int cnt = 0;
412 foreach(p, dns, next) {
413 // verify that this is a valid IPv6 address
414 if (inet_pton(AF_INET6, p, &addr) == 1) {
415 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
416 ++cnt;
420 return cnt;
422 #endif
424 void dns_to_resolv(void)
426 FILE *f;
427 const dns_list_t *dns;
428 int i;
429 mode_t m;
431 m = umask(022); // 077 from pppoecd
432 if ((f = fopen(dmresolv, "w")) != NULL) {
433 // Check for VPN DNS entries
434 if (!write_vpn_resolv(f)) {
435 #ifdef TCONFIG_IPV6
436 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
437 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
438 #endif
439 dns = get_dns(); // static buffer
440 if (dns->count == 0) {
441 // Put a pseudo DNS IP to trigger Connect On Demand
442 if (nvram_match("ppp_demand", "1")) {
443 switch (get_wan_proto()) {
444 case WP_PPPOE:
445 case WP_PPP3G:
446 case WP_PPTP:
447 case WP_L2TP:
448 fprintf(f, "nameserver 1.1.1.1\n");
449 break;
453 else {
454 for (i = 0; i < dns->count; i++) {
455 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
456 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
461 fclose(f);
463 umask(m);
466 // -----------------------------------------------------------------------------
468 void start_httpd(void)
470 if (getpid() != 1) {
471 start_service("httpd");
472 return;
475 stop_httpd();
476 chdir("/www");
477 eval("httpd");
478 chdir("/");
481 void stop_httpd(void)
483 if (getpid() != 1) {
484 stop_service("httpd");
485 return;
488 killall_tk("httpd");
491 // -----------------------------------------------------------------------------
492 #ifdef TCONFIG_IPV6
494 static void add_ip6_lanaddr(void)
496 char ip[INET6_ADDRSTRLEN + 4];
497 const char *p;
499 p = ipv6_router_address(NULL);
500 if (*p) {
501 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
502 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
506 void start_ipv6_tunnel(void)
508 char ip[INET6_ADDRSTRLEN + 4];
509 struct in_addr addr4;
510 struct in6_addr addr;
511 const char *wanip, *mtu, *tun_dev;
512 int service;
514 service = get_ipv6_service();
515 tun_dev = get_wan6face();
516 wanip = get_wanip();
517 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
518 modprobe("sit");
520 if (service == IPV6_ANYCAST_6TO4)
521 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
522 else
523 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
524 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
525 "remote", ip,
526 "local", (char *)wanip,
527 "ttl", nvram_safe_get("ipv6_tun_ttl"));
529 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
530 nvram_set("ipv6_ifname", (char *)tun_dev);
532 if (service == IPV6_ANYCAST_6TO4) {
533 add_ip6_lanaddr();
534 addr4.s_addr = 0;
535 memset(&addr, 0, sizeof(addr));
536 inet_aton(wanip, &addr4);
537 addr.s6_addr16[0] = htons(0x2002);
538 ipv6_mapaddr4(&addr, 16, &addr4, 0);
539 addr.s6_addr16[7] = htons(0x0001);
540 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
541 strncat(ip, "/16", sizeof(ip));
543 else {
544 snprintf(ip, sizeof(ip), "%s/%d",
545 nvram_safe_get("ipv6_tun_addr"),
546 nvram_get_int("ipv6_tun_addrlen") ? : 64);
548 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
549 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
551 // (re)start radvd
552 if (service == IPV6_ANYCAST_6TO4)
553 start_radvd();
556 void stop_ipv6_tunnel(void)
558 eval("ip", "tunnel", "del", (char *)get_wan6face());
559 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
560 // get rid of old IPv6 address from lan iface
561 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
563 modprobe_r("sit");
566 static pid_t pid_radvd = -1;
568 void start_radvd(void)
570 FILE *f;
571 char *prefix, *ip, *mtu;
572 int do_dns, do_6to4;
573 char *argv[] = { "radvd", NULL, NULL, NULL };
574 int pid, argc, service, cnt;
576 if (getpid() != 1) {
577 start_service("radvd");
578 return;
581 stop_radvd();
583 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
584 service = get_ipv6_service();
585 do_6to4 = (service == IPV6_ANYCAST_6TO4);
586 mtu = NULL;
588 switch (service) {
589 case IPV6_NATIVE_DHCP:
590 prefix = "::";
591 break;
592 case IPV6_ANYCAST_6TO4:
593 case IPV6_6IN4:
594 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
595 // fall through
596 default:
597 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
598 break;
600 if (!(*prefix)) prefix = "::";
602 // Create radvd.conf
603 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
605 ip = (char *)ipv6_router_address(NULL);
606 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
608 fprintf(f,
609 "interface %s\n"
610 "{\n"
611 " IgnoreIfMissing on;\n"
612 " AdvSendAdvert on;\n"
613 " MaxRtrAdvInterval 60;\n"
614 " AdvHomeAgentFlag off;\n"
615 " AdvManagedFlag off;\n"
616 "%s%s%s"
617 " prefix %s/64 \n"
618 " {\n"
619 " AdvOnLink on;\n"
620 " AdvAutonomous on;\n"
621 "%s"
622 "%s%s%s"
623 " };\n",
624 nvram_safe_get("lan_ifname"),
625 mtu ? " AdvLinkMTU " : "", mtu ? : "", mtu ? ";\n" : "",
626 prefix,
627 do_6to4 ? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
628 do_6to4 ? " Base6to4Interface " : "",
629 do_6to4 ? get_wanface() : "",
630 do_6to4 ? ";\n" : "");
632 if (do_dns) {
633 fprintf(f, " RDNSS %s {};\n", ip);
635 else {
636 cnt = write_ipv6_dns_servers(f, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
637 if (cnt == 0 || nvram_get_int("dns_addget"))
638 cnt += write_ipv6_dns_servers(f, (cnt) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
639 if (cnt) fprintf(f, "{};\n");
642 fprintf(f,
643 "};\n"); // close "interface" section
644 fclose(f);
646 // Start radvd
647 argc = 1;
648 if (nvram_get_int("debug_ipv6")) {
649 argv[argc++] = "-d";
650 argv[argc++] = "10";
652 argv[argc] = NULL;
653 _eval(argv, NULL, 0, &pid);
655 if (!nvram_contains_word("debug_norestart", "radvd")) {
656 pid_radvd = -2;
661 void stop_radvd(void)
663 if (getpid() != 1) {
664 stop_service("radvd");
665 return;
668 pid_radvd = -1;
669 killall_tk("radvd");
672 void start_ipv6(void)
674 int service;
676 service = get_ipv6_service();
677 enable_ip_forward();
679 // Check if turned on
680 switch (service) {
681 case IPV6_NATIVE:
682 case IPV6_6IN4:
683 case IPV6_MANUAL:
684 add_ip6_lanaddr();
685 break;
686 case IPV6_NATIVE_DHCP:
687 case IPV6_ANYCAST_6TO4:
688 nvram_set("ipv6_rtr_addr", "");
689 nvram_set("ipv6_prefix", "");
690 break;
693 if (service != IPV6_DISABLED) {
694 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
695 accept_ra(nvram_safe_get("lan_ifname"));
699 void stop_ipv6(void)
701 stop_ipv6_tunnel();
702 stop_dhcp6c();
703 eval("ip", "-6", "addr", "flush", "scope", "global");
706 #endif
708 // -----------------------------------------------------------------------------
710 void start_upnp(void)
712 if (getpid() != 1) {
713 start_service("upnp");
714 return;
717 if (get_wan_proto() == WP_DISABLED) return;
719 int enable;
720 FILE *f;
721 int upnp_port;
723 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
724 mkdir("/etc/upnp", 0777);
725 if (f_exists("/etc/upnp/config.alt")) {
726 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
728 else {
729 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
730 upnp_port = nvram_get_int("upnp_port");
731 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
734 fprintf(f,
735 "ext_ifname=%s\n"
736 "port=%d\n"
737 "enable_upnp=%s\n"
738 "enable_natpmp=%s\n"
739 "secure_mode=%s\n"
740 "upnp_forward_chain=upnp\n"
741 "upnp_nat_chain=upnp\n"
742 "notify_interval=%d\n"
743 "system_uptime=yes\n"
744 "\n"
746 get_wanface(),
747 upnp_port,
748 (enable & 1) ? "yes" : "no", // upnp enable
749 (enable & 2) ? "yes" : "no", // natpmp enable
750 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
751 nvram_get_int("upnp_ssdp_interval")
754 if (nvram_get_int("upnp_clean")) {
755 int interval = nvram_get_int("upnp_clean_interval");
756 if (interval < 60) interval = 60;
757 fprintf(f,
758 "clean_ruleset_interval=%d\n"
759 "clean_ruleset_threshold=%d\n",
760 interval,
761 nvram_get_int("upnp_clean_threshold")
764 else
765 fprintf(f,"clean_ruleset_interval=0\n");
767 if (nvram_match("upnp_mnp", "1")) {
768 int https = nvram_get_int("https_enable");
769 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
770 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
771 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
773 else {
774 // Empty parameters are not included into XML service description
775 fprintf(f, "presentation_url=\n");
778 char uuid[45];
779 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
780 fprintf(f, "uuid=%s\n", uuid);
782 char lanN_ipaddr[] = "lanXX_ipaddr";
783 char lanN_netmask[] = "lanXX_netmask";
784 char upnp_lanN[] = "upnp_lanXX";
785 char br;
787 for(br=0 ; br<4 ; br++) {
788 char bridge[2] = "0";
789 if (br!=0)
790 bridge[0]+=br;
791 else
792 strcpy(bridge, "");
794 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
795 sprintf(lanN_netmask, "lan%s_netmask", bridge);
796 sprintf(upnp_lanN, "upnp_lan%s", bridge);
798 char *lanip = nvram_safe_get(lanN_ipaddr);
799 char *lanmask = nvram_safe_get(lanN_netmask);
800 char *lanlisten = nvram_safe_get(upnp_lanN);
802 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
803 fprintf(f,
804 "listening_ip=%s/%s\n",
805 lanip, lanmask);
806 int ports[4];
807 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
808 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
809 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
810 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
811 fprintf(f,
812 "allow %d-%d %s/%s %d-%d\n",
813 ports[0], ports[1],
814 lanip, lanmask,
815 ports[2], ports[3]
818 else {
819 // by default allow only redirection of ports above 1024
820 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
825 fappend(f, "/etc/upnp/config.custom");
826 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
827 fclose(f);
829 xstart("miniupnpd", "-f", "/etc/upnp/config");
835 void stop_upnp(void)
837 if (getpid() != 1) {
838 stop_service("upnp");
839 return;
842 killall_tk("miniupnpd");
845 // -----------------------------------------------------------------------------
847 static pid_t pid_crond = -1;
849 void start_cron(void)
851 stop_cron();
853 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
854 if (!nvram_contains_word("debug_norestart", "crond")) {
855 pid_crond = -2;
859 void stop_cron(void)
861 pid_crond = -1;
862 killall_tk("crond");
865 // -----------------------------------------------------------------------------
866 #ifdef LINUX26
868 static pid_t pid_hotplug2 = -1;
870 void start_hotplug2()
872 stop_hotplug2();
874 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
875 xstart("hotplug2", "--persistent", "--no-coldplug");
876 // FIXME: Don't remember exactly why I put "sleep" here -
877 // but it was not for a race with check_services()... - TB
878 sleep(1);
880 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
881 pid_hotplug2 = -2;
885 void stop_hotplug2(void)
887 pid_hotplug2 = -1;
888 killall_tk("hotplug2");
891 #endif /* LINUX26 */
892 // -----------------------------------------------------------------------------
894 // Written by Sparq in 2002/07/16
895 void start_zebra(void)
897 #ifdef TCONFIG_ZEBRA
898 if (getpid() != 1) {
899 start_service("zebra");
900 return;
903 FILE *fp;
905 char *lan_tx = nvram_safe_get("dr_lan_tx");
906 char *lan_rx = nvram_safe_get("dr_lan_rx");
907 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
908 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
909 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
910 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
911 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
912 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
913 char *wan_tx = nvram_safe_get("dr_wan_tx");
914 char *wan_rx = nvram_safe_get("dr_wan_rx");
916 // if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
917 if ((*lan_tx == '0') && (*lan_rx == '0') &&
918 (*lan1_tx == '0') && (*lan1_rx == '0') &&
919 (*lan2_tx == '0') && (*lan2_rx == '0') &&
920 (*lan3_tx == '0') && (*lan3_rx == '0') &&
921 (*wan_tx == '0') && (*wan_rx == '0')) {
922 return;
925 // empty
926 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
927 fclose(fp);
931 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
932 char *lan_ifname = nvram_safe_get("lan_ifname");
933 char *lan1_ifname = nvram_safe_get("lan1_ifname");
934 char *lan2_ifname = nvram_safe_get("lan2_ifname");
935 char *lan3_ifname = nvram_safe_get("lan3_ifname");
936 char *wan_ifname = nvram_safe_get("wan_ifname");
938 fprintf(fp, "router rip\n");
939 if(strcmp(lan_ifname,"")!=0)
940 fprintf(fp, "network %s\n", lan_ifname);
941 if(strcmp(lan1_ifname,"")!=0)
942 fprintf(fp, "network %s\n", lan1_ifname);
943 if(strcmp(lan2_ifname,"")!=0)
944 fprintf(fp, "network %s\n", lan2_ifname);
945 if(strcmp(lan3_ifname,"")!=0)
946 fprintf(fp, "network %s\n", lan3_ifname);
947 fprintf(fp, "network %s\n", wan_ifname);
948 fprintf(fp, "redistribute connected\n");
949 //fprintf(fp, "redistribute static\n");
951 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
952 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
954 if(strcmp(lan_ifname,"")!=0) {
955 fprintf(fp, "interface %s\n", lan_ifname);
956 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
957 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
959 if(strcmp(lan1_ifname,"")!=0) {
960 fprintf(fp, "interface %s\n", lan1_ifname);
961 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
962 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
964 if(strcmp(lan2_ifname,"")!=0) {
965 fprintf(fp, "interface %s\n", lan2_ifname);
966 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
967 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
969 if(strcmp(lan3_ifname,"")!=0) {
970 fprintf(fp, "interface %s\n", lan3_ifname);
971 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
972 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
974 fprintf(fp, "interface %s\n", wan_ifname);
975 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
976 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
978 fprintf(fp, "router rip\n");
979 if(strcmp(lan_ifname,"")!=0) {
980 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
981 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
983 if(strcmp(lan1_ifname,"")!=0) {
984 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
985 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
987 if(strcmp(lan2_ifname,"")!=0) {
988 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
989 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
991 if(strcmp(lan3_ifname,"")!=0) {
992 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
993 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
995 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
996 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
997 fprintf(fp, "access-list private deny any\n");
999 //fprintf(fp, "debug rip events\n");
1000 //fprintf(fp, "log file /etc/ripd.log\n");
1001 fclose(fp);
1004 xstart("zebra", "-d");
1005 xstart("ripd", "-d");
1006 #endif
1009 void stop_zebra(void)
1011 #ifdef TCONFIG_ZEBRA
1012 if (getpid() != 1) {
1013 stop_service("zebra");
1014 return;
1017 killall("zebra", SIGTERM);
1018 killall("ripd", SIGTERM);
1020 unlink("/etc/zebra.conf");
1021 unlink("/etc/ripd.conf");
1022 #endif
1025 // -----------------------------------------------------------------------------
1027 void start_syslog(void)
1029 char *argv[16];
1030 int argc;
1031 char *nv;
1032 char *b_opt = "";
1033 char rem[256];
1034 int n;
1035 char s[64];
1036 char cfg[256];
1037 char *rot_siz = "50";
1038 char *log_file_path;
1040 argv[0] = "syslogd";
1041 argc = 1;
1043 if (nvram_match("log_remote", "1")) {
1044 nv = nvram_safe_get("log_remoteip");
1045 if (*nv) {
1046 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1047 argv[argc++] = "-R";
1048 argv[argc++] = rem;
1052 if (nvram_match("log_file", "1")) {
1053 argv[argc++] = "-L";
1055 // log to custom path - shibby
1056 if (nvram_match("log_file_custom", "1")) {
1057 log_file_path = nvram_safe_get("log_file_path");
1058 argv[argc++] = "-s";
1059 argv[argc++] = "5000";
1060 argv[argc++] = "-b";
1061 argv[argc++] = "5";
1062 argv[argc++] = "-O";
1063 argv[argc++] = log_file_path;
1064 remove("/var/log/messages");
1065 symlink(log_file_path, "/var/log/messages");
1067 else
1069 /* Read options: rotate_size(kb) num_backups logfilename.
1070 * Ignore these settings and use defaults if the logfile cannot be written to.
1072 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1073 if ((nv = strchr(cfg, '\n')))
1074 *nv = 0;
1076 if ((nv = strtok(cfg, " \t"))) {
1077 if (isdigit(*nv))
1078 rot_siz = nv;
1081 if ((nv = strtok(NULL, " \t")))
1082 b_opt = nv;
1084 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1085 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1086 argv[argc++] = "-O";
1087 argv[argc++] = nv;
1089 else {
1090 rot_siz = "50";
1091 b_opt = "";
1096 if (nvram_match("log_file_custom", "0")) {
1097 argv[argc++] = "-s";
1098 argv[argc++] = rot_siz;
1099 remove("/var/log/messages");
1102 if (isdigit(*b_opt)) {
1103 argv[argc++] = "-b";
1104 argv[argc++] = b_opt;
1108 if (argc > 1) {
1109 argv[argc] = NULL;
1110 _eval(argv, NULL, 0, NULL);
1112 argv[0] = "klogd";
1113 argv[1] = NULL;
1114 _eval(argv, NULL, 0, NULL);
1116 // used to be available in syslogd -m
1117 n = nvram_get_int("log_mark");
1118 if (n > 0) {
1119 // n is in minutes
1120 if (n < 60)
1121 sprintf(rem, "*/%d * * * *", n);
1122 else if (n < 60 * 24)
1123 sprintf(rem, "0 */%d * * *", n / 60);
1124 else
1125 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1126 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1127 eval("cru", "a", "syslogdmark", s);
1129 else {
1130 eval("cru", "d", "syslogdmark");
1135 void stop_syslog(void)
1137 killall("klogd", SIGTERM);
1138 killall("syslogd", SIGTERM);
1141 // -----------------------------------------------------------------------------
1143 static pid_t pid_igmp = -1;
1145 void start_igmp_proxy(void)
1147 FILE *fp;
1149 pid_igmp = -1;
1150 if (nvram_match("multicast_pass", "1")) {
1151 if (get_wan_proto() == WP_DISABLED)
1152 return;
1154 if (f_exists("/etc/igmp.alt")) {
1155 eval("igmpproxy", "/etc/igmp.alt");
1157 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1158 fprintf(fp,
1159 "quickleave\n"
1160 "phyint %s upstream\n"
1161 "\taltnet %s\n",
1162 // "phyint %s downstream ratelimit 0\n",
1163 get_wanface(),
1164 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1165 // nvram_safe_get("lan_ifname"));
1167 char lanN_ifname[] = "lanXX_ifname";
1168 char multicast_lanN[] = "multicast_lanXX";
1169 char br;
1171 for(br=0 ; br<4 ; br++) {
1172 char bridge[2] = "0";
1173 if (br!=0)
1174 bridge[0]+=br;
1175 else
1176 strcpy(bridge, "");
1178 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1179 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1181 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1182 fprintf(fp,
1183 "phyint %s downstream ratelimit 0\n",
1184 nvram_safe_get(lanN_ifname));
1187 fclose(fp);
1188 eval("igmpproxy", "/etc/igmp.conf");
1190 else {
1191 return;
1193 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1194 pid_igmp = -2;
1199 void stop_igmp_proxy(void)
1201 pid_igmp = -1;
1202 killall_tk("igmpproxy");
1205 #ifdef TCONFIG_NOCAT
1207 static pid_t pid_splashd = -1;
1208 void start_splashd(void)
1210 pid_splashd = -1;
1211 start_nocat();
1212 if (!nvram_contains_word("debug_norestart", "splashd")) {
1213 pid_splashd = -2;
1217 void stop_splashd(void)
1219 pid_splashd = -1;
1220 stop_nocat();
1221 start_wan(BOOT);
1223 #endif
1225 // -----------------------------------------------------------------------------
1227 void set_tz(void)
1229 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1232 void start_ntpc(void)
1234 set_tz();
1236 stop_ntpc();
1238 if (nvram_get_int("ntp_updates") >= 0) {
1239 xstart("ntpsync", "--init");
1243 void stop_ntpc(void)
1245 killall("ntpsync", SIGTERM);
1248 // -----------------------------------------------------------------------------
1250 static void stop_rstats(void)
1252 int n;
1253 int pid;
1255 n = 60;
1256 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1257 if (kill(pid, SIGTERM) != 0) break;
1258 sleep(1);
1262 static void start_rstats(int new)
1264 if (nvram_match("rstats_enable", "1")) {
1265 stop_rstats();
1266 if (new) xstart("rstats", "--new");
1267 else xstart("rstats");
1271 static void stop_cstats(void)
1273 int n;
1274 int pid;
1276 n = 60;
1277 while ((n-- > 0) && ((pid = pidof("cstats")) > 0)) {
1278 if (kill(pid, SIGTERM) != 0) break;
1279 sleep(1);
1283 static void start_cstats(int new)
1285 if (nvram_match("cstats_enable", "1")) {
1286 stop_cstats();
1287 if (new) xstart("cstats", "--new");
1288 else xstart("cstats");
1292 // -----------------------------------------------------------------------------
1294 // !!TB - FTP Server
1296 #ifdef TCONFIG_FTP
1297 static char *get_full_storage_path(char *val)
1299 static char buf[128];
1300 int len;
1302 if (val[0] == '/')
1303 len = sprintf(buf, "%s", val);
1304 else
1305 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1307 if (len > 1 && buf[len - 1] == '/')
1308 buf[len - 1] = 0;
1310 return buf;
1313 static char *nvram_storage_path(char *var)
1315 char *val = nvram_safe_get(var);
1316 return get_full_storage_path(val);
1319 char vsftpd_conf[] = "/etc/vsftpd.conf";
1320 char vsftpd_users[] = "/etc/vsftpd.users";
1321 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1323 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1325 static void start_ftpd(void)
1327 char tmp[256];
1328 FILE *fp, *f;
1329 char *buf;
1330 char *p, *q;
1331 char *user, *pass, *rights;
1333 if (getpid() != 1) {
1334 start_service("ftpd");
1335 return;
1338 if (!nvram_get_int("ftp_enable")) return;
1340 mkdir_if_none(vsftpd_users);
1341 mkdir_if_none("/var/run/vsftpd");
1343 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1344 return;
1346 if (nvram_get_int("ftp_super"))
1348 /* rights */
1349 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1350 if ((f = fopen(tmp, "w")))
1352 fprintf(f,
1353 "dirlist_enable=yes\n"
1354 "write_enable=yes\n"
1355 "download_enable=yes\n");
1356 fclose(f);
1360 #ifdef TCONFIG_SAMBASRV
1361 if (nvram_match("smbd_cset", "utf8"))
1362 fprintf(fp, "utf8=yes\n");
1363 #endif
1365 if (nvram_invmatch("ftp_anonymous", "0"))
1367 fprintf(fp,
1368 "anon_allow_writable_root=yes\n"
1369 "anon_world_readable_only=no\n"
1370 "anon_umask=022\n");
1372 /* rights */
1373 sprintf(tmp, "%s/ftp", vsftpd_users);
1374 if ((f = fopen(tmp, "w")))
1376 if (nvram_match("ftp_dirlist", "0"))
1377 fprintf(f, "dirlist_enable=yes\n");
1378 if (nvram_match("ftp_anonymous", "1") ||
1379 nvram_match("ftp_anonymous", "3"))
1380 fprintf(f, "write_enable=yes\n");
1381 if (nvram_match("ftp_anonymous", "1") ||
1382 nvram_match("ftp_anonymous", "2"))
1383 fprintf(f, "download_enable=yes\n");
1384 fclose(f);
1386 if (nvram_match("ftp_anonymous", "1") ||
1387 nvram_match("ftp_anonymous", "3"))
1388 fprintf(fp,
1389 "anon_upload_enable=yes\n"
1390 "anon_mkdir_write_enable=yes\n"
1391 "anon_other_write_enable=yes\n");
1392 } else {
1393 fprintf(fp, "anonymous_enable=no\n");
1396 fprintf(fp,
1397 "dirmessage_enable=yes\n"
1398 "download_enable=no\n"
1399 "dirlist_enable=no\n"
1400 "hide_ids=yes\n"
1401 "syslog_enable=yes\n"
1402 "local_enable=yes\n"
1403 "local_umask=022\n"
1404 "chmod_enable=no\n"
1405 "chroot_local_user=yes\n"
1406 "check_shell=no\n"
1407 "log_ftp_protocol=%s\n"
1408 "user_config_dir=%s\n"
1409 "passwd_file=%s\n"
1410 "listen%s=yes\n"
1411 "listen_port=%s\n"
1412 "background=yes\n"
1413 "isolate=no\n"
1414 "max_clients=%d\n"
1415 "max_per_ip=%d\n"
1416 "max_login_fails=1\n"
1417 "idle_session_timeout=%s\n"
1418 "use_sendfile=no\n"
1419 "anon_max_rate=%d\n"
1420 "local_max_rate=%d\n"
1421 "%s\n",
1422 nvram_get_int("log_ftp") ? "yes" : "no",
1423 vsftpd_users, vsftpd_passwd,
1424 #ifdef TCONFIG_IPV6
1425 ipv6_enabled() ? "_ipv6" : "",
1426 #else
1428 #endif
1429 nvram_get("ftp_port") ? : "21",
1430 nvram_get_int("ftp_max"),
1431 nvram_get_int("ftp_ipmax"),
1432 nvram_get("ftp_staytimeout") ? : "300",
1433 nvram_get_int("ftp_anonrate") * 1024,
1434 nvram_get_int("ftp_rate") * 1024,
1435 nvram_safe_get("ftp_custom"));
1437 fclose(fp);
1439 /* prepare passwd file and default users */
1440 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1441 return;
1443 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1444 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1446 fprintf(fp, /* anonymous, admin, nobody */
1447 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1448 "%s:%s:0:0:root:/:/sbin/nologin\n"
1449 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1450 nvram_storage_path("ftp_anonroot"), user,
1451 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1452 MOUNT_ROOT);
1454 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1457 username<password<rights
1458 rights:
1459 Read/Write
1460 Read Only
1461 View Only
1462 Private
1464 p = buf;
1465 while ((q = strsep(&p, ">")) != NULL) {
1466 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
1467 if (!user || !pass) continue;
1469 /* directory */
1470 if (strncmp(rights, "Private", 7) == 0)
1472 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1473 mkdir_if_none(tmp);
1475 else
1476 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
1478 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1479 user, crypt(pass, "$1$"), user, tmp);
1481 /* rights */
1482 sprintf(tmp, "%s/%s", vsftpd_users, user);
1483 if ((f = fopen(tmp, "w")))
1485 tmp[0] = 0;
1486 if (nvram_invmatch("ftp_dirlist", "1"))
1487 strcat(tmp, "dirlist_enable=yes\n");
1488 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1489 strcat(tmp, "download_enable=yes\n");
1490 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1491 strcat(tmp, "write_enable=yes\n");
1493 fputs(tmp, f);
1494 fclose(f);
1497 free(buf);
1500 fclose(fp);
1501 killall("vsftpd", SIGHUP);
1503 /* start vsftpd if it's not already running */
1504 if (pidof("vsftpd") <= 0)
1505 xstart("vsftpd");
1508 static void stop_ftpd(void)
1510 if (getpid() != 1) {
1511 stop_service("ftpd");
1512 return;
1515 killall_tk("vsftpd");
1516 unlink(vsftpd_passwd);
1517 unlink(vsftpd_conf);
1518 eval("rm", "-rf", vsftpd_users);
1520 #endif // TCONFIG_FTP
1522 // -----------------------------------------------------------------------------
1524 // !!TB - Samba
1526 #ifdef TCONFIG_SAMBASRV
1527 static void kill_samba(int sig)
1529 if (sig == SIGTERM) {
1530 killall_tk("smbd");
1531 killall_tk("nmbd");
1533 else {
1534 killall("smbd", sig);
1535 killall("nmbd", sig);
1539 static void start_samba(void)
1541 FILE *fp;
1542 DIR *dir = NULL;
1543 struct dirent *dp;
1544 char nlsmod[15];
1545 int mode;
1546 char *nv;
1548 if (getpid() != 1) {
1549 start_service("smbd");
1550 return;
1553 mode = nvram_get_int("smbd_enable");
1554 if (!mode || !nvram_invmatch("lan_hostname", ""))
1555 return;
1557 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1558 return;
1560 fprintf(fp, "[global]\n"
1561 " interfaces = %s\n"
1562 " bind interfaces only = yes\n"
1563 " workgroup = %s\n"
1564 " netbios name = %s\n"
1565 " server string = %s\n"
1566 " guest account = nobody\n"
1567 " security = user\n"
1568 " %s\n"
1569 " guest ok = %s\n"
1570 " guest only = no\n"
1571 " browseable = yes\n"
1572 " syslog only = yes\n"
1573 " timestamp logs = no\n"
1574 " syslog = 1\n"
1575 " encrypt passwords = yes\n"
1576 " preserve case = yes\n"
1577 " short preserve case = yes\n",
1578 nvram_safe_get("lan_ifname"),
1579 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1580 nvram_safe_get("lan_hostname"),
1581 nvram_get("router_name") ? : "Tomato",
1582 mode == 2 ? "" : "map to guest = Bad User",
1583 mode == 2 ? "no" : "yes" // guest ok
1586 if (nvram_get_int("smbd_wins")) {
1587 nv = nvram_safe_get("wan_wins");
1588 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1589 fprintf(fp, " wins support = yes\n");
1593 if (nvram_get_int("smbd_master")) {
1594 fprintf(fp,
1595 " domain master = yes\n"
1596 " local master = yes\n"
1597 " preferred master = yes\n"
1598 " os level = 65\n");
1601 nv = nvram_safe_get("smbd_cpage");
1602 if (*nv) {
1603 #ifndef TCONFIG_SAMBA3
1604 fprintf(fp, " client code page = %s\n", nv);
1605 #endif
1606 sprintf(nlsmod, "nls_cp%s", nv);
1608 nv = nvram_safe_get("smbd_nlsmod");
1609 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1610 modprobe_r(nv);
1612 modprobe(nlsmod);
1613 nvram_set("smbd_nlsmod", nlsmod);
1616 #ifndef TCONFIG_SAMBA3
1617 if (nvram_match("smbd_cset", "utf8"))
1618 fprintf(fp, " coding system = utf8\n");
1619 else if (nvram_invmatch("smbd_cset", ""))
1620 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1621 #endif
1623 nv = nvram_safe_get("smbd_custom");
1624 /* add socket options unless overriden by the user */
1625 if (strstr(nv, "socket options") == NULL) {
1626 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1628 fprintf(fp, "%s\n\n", nv);
1630 /* configure shares */
1632 char *buf;
1633 char *p, *q;
1634 char *name, *path, *comment, *writeable, *hidden;
1635 int cnt = 0;
1637 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1639 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1641 p = buf;
1642 while ((q = strsep(&p, ">")) != NULL) {
1643 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1644 if (!path || !name) continue;
1646 /* share name */
1647 fprintf(fp, "\n[%s]\n", name);
1649 /* path */
1650 fprintf(fp, " path = %s\n", path);
1652 /* access level */
1653 if (!strcmp(writeable, "1"))
1654 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1655 if (!strcmp(hidden, "1"))
1656 fprintf(fp, " browseable = no\n");
1658 /* comment */
1659 if (comment)
1660 fprintf(fp, " comment = %s\n", comment);
1662 cnt++;
1664 free(buf);
1667 /* Share every mountpoint below MOUNT_ROOT */
1668 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1669 while ((dp = readdir(dir))) {
1670 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1672 /* Only if is a directory and is mounted */
1673 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
1674 continue;
1676 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1677 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1678 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1679 if (nvram_match("smbd_autoshare", "3")) // Hidden
1680 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1681 dp->d_name, MOUNT_ROOT, dp->d_name);
1682 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1683 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1685 cnt++;
1689 if (dir) closedir(dir);
1691 if (cnt == 0) {
1692 /* by default share MOUNT_ROOT as read-only */
1693 fprintf(fp, "\n[share]\n"
1694 " path = %s\n"
1695 " writable = no\n",
1696 MOUNT_ROOT);
1699 fclose(fp);
1701 mkdir_if_none("/var/run/samba");
1702 mkdir_if_none("/etc/samba");
1704 /* write smbpasswd */
1705 #ifdef TCONFIG_SAMBA3
1706 eval("smbpasswd", "nobody", "\"\"");
1707 #else
1708 eval("smbpasswd", "-a", "nobody", "\"\"");
1709 #endif
1710 if (mode == 2) {
1711 char *smbd_user;
1712 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1713 smbd_user = "nas";
1714 #ifdef TCONFIG_SAMBA3
1715 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1716 #else
1717 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1718 #endif
1721 kill_samba(SIGHUP);
1722 int ret1 = 0, ret2 = 0;
1723 /* start samba if it's not already running */
1724 if (pidof("nmbd") <= 0)
1725 ret1 = xstart("nmbd", "-D");
1726 if (pidof("smbd") <= 0)
1727 ret2 = xstart("smbd", "-D");
1729 if (ret1 || ret2) kill_samba(SIGTERM);
1732 static void stop_samba(void)
1734 if (getpid() != 1) {
1735 stop_service("smbd");
1736 return;
1739 kill_samba(SIGTERM);
1740 /* clean up */
1741 unlink("/var/log/smb");
1742 unlink("/var/log/nmb");
1743 eval("rm", "-rf", "/var/run/samba");
1745 #endif // TCONFIG_SAMBASRV
1747 #ifdef TCONFIG_MEDIA_SERVER
1748 #define MEDIA_SERVER_APP "minidlna"
1750 static void start_media_server(void)
1752 FILE *f;
1753 int port, pid, https;
1754 char *dbdir;
1755 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1756 static int once = 1;
1758 if (getpid() != 1) {
1759 start_service("media");
1760 return;
1763 if (nvram_get_int("ms_sas") == 0)
1764 once = 0;
1766 if (nvram_get_int("ms_enable") != 0) {
1767 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1768 // no forced rescan
1769 argv[3] = NULL;
1771 nvram_unset("ms_rescan");
1773 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1774 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1776 else {
1777 if ((f = fopen(argv[2], "w")) != NULL) {
1778 port = nvram_get_int("ms_port");
1779 https = nvram_get_int("https_enable");
1780 dbdir = nvram_safe_get("ms_dbdir");
1781 if (!(*dbdir)) dbdir = NULL;
1782 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1784 fprintf(f,
1785 "network_interface=%s\n"
1786 "port=%d\n"
1787 "friendly_name=%s\n"
1788 "db_dir=%s/.db\n"
1789 "enable_tivo=%s\n"
1790 "strict_dlna=%s\n"
1791 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1792 "inotify=yes\n"
1793 "notify_interval=600\n"
1794 "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"
1795 "\n",
1796 nvram_safe_get("lan_ifname"),
1797 (port < 0) || (port >= 0xffff) ? 0 : port,
1798 nvram_get("router_name") ? : "Tomato",
1799 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1800 nvram_get_int("ms_tivo") ? "yes" : "no",
1801 nvram_get_int("ms_stdlna") ? "yes" : "no",
1802 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1805 // media directories
1806 char *buf, *p, *q;
1807 char *path, *restrict;
1809 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1810 /* path<restrict[A|V|P|] */
1812 p = buf;
1813 while ((q = strsep(&p, ">")) != NULL) {
1814 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1815 continue;
1816 fprintf(f, "media_dir=%s%s%s\n",
1817 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1819 free(buf);
1822 fclose(f);
1826 /* start media server if it's not already running */
1827 if (pidof(MEDIA_SERVER_APP) <= 0) {
1828 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1829 /* If we started the media server successfully, wait 1 sec
1830 * to let it die if it can't open the database file.
1831 * If it's still alive after that, assume it's running and
1832 * disable forced once-after-reboot rescan.
1834 sleep(1);
1835 if (pidof(MEDIA_SERVER_APP) > 0)
1836 once = 0;
1842 static void stop_media_server(void)
1844 if (getpid() != 1) {
1845 stop_service("media");
1846 return;
1849 killall_tk(MEDIA_SERVER_APP);
1851 #endif // TCONFIG_MEDIA_SERVER
1853 #ifdef TCONFIG_USB
1854 static void start_nas_services(void)
1856 if (getpid() != 1) {
1857 start_service("usbapps");
1858 return;
1861 #ifdef TCONFIG_SAMBASRV
1862 start_samba();
1863 #endif
1864 #ifdef TCONFIG_FTP
1865 start_ftpd();
1866 #endif
1867 #ifdef TCONFIG_MEDIA_SERVER
1868 start_media_server();
1869 #endif
1872 static void stop_nas_services(void)
1874 if (getpid() != 1) {
1875 stop_service("usbapps");
1876 return;
1879 #ifdef TCONFIG_MEDIA_SERVER
1880 stop_media_server();
1881 #endif
1882 #ifdef TCONFIG_FTP
1883 stop_ftpd();
1884 #endif
1885 #ifdef TCONFIG_SAMBASRV
1886 stop_samba();
1887 #endif
1890 void restart_nas_services(int stop, int start)
1892 int fd = file_lock("usb");
1893 /* restart all NAS applications */
1894 if (stop)
1895 stop_nas_services();
1896 if (start)
1897 start_nas_services();
1898 file_unlock(fd);
1900 #endif // TCONFIG_USB
1902 // -----------------------------------------------------------------------------
1904 /* -1 = Don't check for this program, it is not expected to be running.
1905 * Other = This program has been started and should be kept running. If no
1906 * process with the name is running, call func to restart it.
1907 * Note: At startup, dnsmasq forks a short-lived child which forks a
1908 * long-lived (grand)child. The parents terminate.
1909 * Many daemons use this technique.
1911 static void _check(pid_t pid, const char *name, void (*func)(void))
1913 if (pid == -1) return;
1915 if (pidof(name) > 0) return;
1917 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
1918 func();
1920 // Force recheck in 500 msec
1921 setitimer(ITIMER_REAL, &pop_tv, NULL);
1924 void check_services(void)
1926 TRACE_PT("keep alive\n");
1928 // Periodically reap any zombies
1929 setitimer(ITIMER_REAL, &zombie_tv, NULL);
1931 #ifdef LINUX26
1932 _check(pid_hotplug2, "hotplug2", start_hotplug2);
1933 #endif
1934 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
1935 _check(pid_crond, "crond", start_cron);
1936 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
1937 #ifdef TCONFIG_IPV6
1938 _check(pid_radvd, "radvd", start_radvd);
1939 #endif
1941 //#ifdef TCONFIG_NOCAT
1942 // if (nvram_get_int("NC_enable"))
1943 // _check(&pid_splashd, "splashd", start_splashd);
1944 //#endif
1948 // -----------------------------------------------------------------------------
1950 void start_services(void)
1952 static int once = 1;
1954 if (once) {
1955 once = 0;
1957 if (nvram_get_int("telnetd_eas")) start_telnetd();
1958 if (nvram_get_int("sshd_eas")) start_sshd();
1961 // start_syslog();
1962 start_nas();
1963 start_zebra();
1964 #ifdef TCONFIG_SDHC
1965 start_mmc();
1966 #endif
1967 start_dnsmasq();
1968 start_cifs();
1969 start_httpd();
1970 start_cron();
1971 // start_upnp();
1972 start_rstats(0);
1973 start_cstats(0);
1974 start_sched();
1975 #ifdef TCONFIG_IPV6
1976 /* note: starting radvd here might be too early in case of
1977 * DHCPv6 or 6to4 because we won't have received a prefix and
1978 * so it will disable advertisements. To restart them, we have
1979 * to send radvd a SIGHUP, or restart it.
1981 start_radvd();
1982 #endif
1983 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1985 #ifdef TCONFIG_SNMP
1986 start_snmp();
1987 #endif
1989 #ifdef TCONFIG_BT
1990 start_bittorrent();
1991 #endif
1993 #ifdef TCONFIG_NFS
1994 start_nfs();
1995 #endif
1998 void stop_services(void)
2000 clear_resolv();
2002 #ifdef TCONFIG_BT
2003 stop_bittorrent();
2004 #endif
2006 #ifdef TCONFIG_SNMP
2007 stop_snmp();
2008 #endif
2010 #ifdef TCONFIG_NFS
2011 stop_nfs();
2012 #endif
2013 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2014 #ifdef TCONFIG_IPV6
2015 stop_radvd();
2016 #endif
2017 stop_sched();
2018 stop_rstats();
2019 stop_cstats();
2020 // stop_upnp();
2021 stop_cron();
2022 stop_httpd();
2023 #ifdef TCONFIG_SDHC
2024 stop_mmc();
2025 #endif
2026 stop_cifs();
2027 stop_dnsmasq();
2028 stop_zebra();
2029 stop_nas();
2030 // stop_syslog();
2033 // -----------------------------------------------------------------------------
2035 /* nvram "action_service" is: "service-action[-modifier]"
2036 * action is something like "stop" or "start" or "restart"
2037 * optional modifier is "c" for the "service" command-line command
2039 void exec_service(void)
2041 const int A_START = 1;
2042 const int A_STOP = 2;
2043 const int A_RESTART = 1|2;
2044 char buffer[128];
2045 char *service;
2046 char *act;
2047 char *next;
2048 char *modifier;
2049 int action, user;
2050 int i;
2052 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2053 next = buffer;
2055 TOP:
2056 act = strsep(&next, ",");
2057 service = strsep(&act, "-");
2058 if (act == NULL) {
2059 next = NULL;
2060 goto CLEAR;
2062 modifier = act;
2063 strsep(&modifier, "-");
2065 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2067 if (strcmp(act, "start") == 0) action = A_START;
2068 else if (strcmp(act, "stop") == 0) action = A_STOP;
2069 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2070 else action = 0;
2071 user = (modifier != NULL && *modifier == 'c');
2073 if (strcmp(service, "dhcpc") == 0) {
2074 if (action & A_STOP) stop_dhcpc();
2075 if (action & A_START) start_dhcpc();
2076 goto CLEAR;
2079 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2080 if (action & A_STOP) stop_dnsmasq();
2081 if (action & A_START) {
2082 dns_to_resolv();
2083 start_dnsmasq();
2085 goto CLEAR;
2088 if (strcmp(service, "firewall") == 0) {
2089 if (action & A_STOP) {
2090 stop_firewall();
2091 stop_igmp_proxy();
2093 if (action & A_START) {
2094 start_firewall();
2095 start_igmp_proxy();
2097 goto CLEAR;
2100 if (strcmp(service, "restrict") == 0) {
2101 if (action & A_STOP) {
2102 stop_firewall();
2104 if (action & A_START) {
2105 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2107 start_firewall();
2109 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2110 if (i == 1) {
2111 if (nvram_get_int("rrules_radio") < 0) {
2112 eval("radio", "on");
2116 goto CLEAR;
2119 if (strcmp(service, "account") == 0) {
2120 if (action & A_STOP) stop_account();
2121 if (action & A_START) start_account();
2122 goto CLEAR;
2125 if (strcmp(service, "qos") == 0) {
2126 if (action & A_STOP) {
2127 stop_qos();
2129 stop_firewall(); start_firewall(); // always restarted
2130 if (action & A_START) {
2131 start_qos();
2132 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2134 goto CLEAR;
2137 if (strcmp(service, "qoslimit") == 0) {
2138 if (action & A_STOP) {
2139 new_qoslimit_stop();
2141 stop_firewall(); start_firewall(); // always restarted
2142 if (action & A_START) {
2143 new_qoslimit_start();
2145 goto CLEAR;
2148 if (strcmp(service, "arpbind") == 0) {
2149 if (action & A_STOP) stop_arpbind();
2150 if (action & A_START) start_arpbind();
2151 goto CLEAR;
2154 if (strcmp(service, "upnp") == 0) {
2155 if (action & A_STOP) {
2156 stop_upnp();
2158 stop_firewall(); start_firewall(); // always restarted
2159 if (action & A_START) {
2160 start_upnp();
2162 goto CLEAR;
2165 if (strcmp(service, "telnetd") == 0) {
2166 if (action & A_STOP) stop_telnetd();
2167 if (action & A_START) start_telnetd();
2168 goto CLEAR;
2171 if (strcmp(service, "sshd") == 0) {
2172 if (action & A_STOP) stop_sshd();
2173 if (action & A_START) start_sshd();
2174 goto CLEAR;
2177 if (strcmp(service, "httpd") == 0) {
2178 if (action & A_STOP) stop_httpd();
2179 if (action & A_START) start_httpd();
2180 goto CLEAR;
2183 #ifdef TCONFIG_IPV6
2184 if (strcmp(service, "ipv6") == 0) {
2185 if (action & A_STOP) {
2186 stop_radvd();
2187 stop_ipv6();
2189 if (action & A_START) {
2190 start_ipv6();
2191 start_radvd();
2193 goto CLEAR;
2196 if (strcmp(service, "radvd") == 0) {
2197 if (action & A_STOP) {
2198 stop_radvd();
2200 if (action & A_START) {
2201 start_radvd();
2203 goto CLEAR;
2206 if (strncmp(service, "dhcp6", 5) == 0) {
2207 if (action & A_STOP) {
2208 stop_dhcp6c();
2210 if (action & A_START) {
2211 start_dhcp6c();
2213 goto CLEAR;
2215 #endif
2217 if (strcmp(service, "admin") == 0) {
2218 if (action & A_STOP) {
2219 stop_sshd();
2220 stop_telnetd();
2221 stop_httpd();
2223 stop_firewall(); start_firewall(); // always restarted
2224 if (action & A_START) {
2225 start_httpd();
2226 create_passwd();
2227 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2228 if (nvram_match("sshd_eas", "1")) start_sshd();
2230 goto CLEAR;
2233 if (strcmp(service, "ddns") == 0) {
2234 if (action & A_STOP) stop_ddns();
2235 if (action & A_START) start_ddns();
2236 goto CLEAR;
2239 if (strcmp(service, "ntpc") == 0) {
2240 if (action & A_STOP) stop_ntpc();
2241 if (action & A_START) start_ntpc();
2242 goto CLEAR;
2245 if (strcmp(service, "logging") == 0) {
2246 if (action & A_STOP) {
2247 stop_syslog();
2249 if (action & A_START) {
2250 start_syslog();
2252 if (!user) {
2253 // always restarted except from "service" command
2254 stop_cron(); start_cron();
2255 stop_firewall(); start_firewall();
2257 goto CLEAR;
2260 if (strcmp(service, "crond") == 0) {
2261 if (action & A_STOP) {
2262 stop_cron();
2264 if (action & A_START) {
2265 start_cron();
2267 goto CLEAR;
2270 #ifdef LINUX26
2271 if (strncmp(service, "hotplug", 7) == 0) {
2272 if (action & A_STOP) {
2273 stop_hotplug2();
2275 if (action & A_START) {
2276 start_hotplug2(1);
2278 goto CLEAR;
2280 #endif
2282 if (strcmp(service, "upgrade") == 0) {
2283 if (action & A_START) {
2284 #if TOMATO_SL
2285 stop_usbevent();
2286 stop_smbd();
2287 #endif
2288 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2289 stop_jffs2();
2290 // stop_cifs();
2291 stop_zebra();
2292 stop_cron();
2293 stop_ntpc();
2294 stop_upnp();
2295 // stop_dhcpc();
2296 killall("rstats", SIGTERM);
2297 killall("cstats", SIGTERM);
2298 killall("buttons", SIGTERM);
2299 stop_syslog();
2300 remove_storage_main(1); // !!TB - USB Support
2301 stop_usb(); // !!TB - USB Support
2303 goto CLEAR;
2306 #ifdef TCONFIG_CIFS
2307 if (strcmp(service, "cifs") == 0) {
2308 if (action & A_STOP) stop_cifs();
2309 if (action & A_START) start_cifs();
2310 goto CLEAR;
2312 #endif
2314 #ifdef TCONFIG_JFFS2
2315 if (strncmp(service, "jffs", 4) == 0) {
2316 if (action & A_STOP) stop_jffs2();
2317 if (action & A_START) start_jffs2();
2318 goto CLEAR;
2320 #endif
2322 if (strcmp(service, "zebra") == 0) {
2323 if (action & A_STOP) stop_zebra();
2324 if (action & A_START) start_zebra();
2325 goto CLEAR;
2328 #ifdef TCONFIG_SDHC
2329 if (strcmp(service, "mmc") == 0) {
2330 if (action & A_STOP) stop_mmc();
2331 if (action & A_START) start_mmc();
2332 goto CLEAR;
2334 #endif
2336 if (strcmp(service, "routing") == 0) {
2337 if (action & A_STOP) {
2338 stop_zebra();
2339 do_static_routes(0); // remove old '_saved'
2340 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2341 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2342 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2343 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2344 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2345 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2346 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2348 stop_firewall();
2349 start_firewall();
2350 if (action & A_START) {
2351 do_static_routes(1); // add new
2352 start_zebra();
2353 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2354 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2355 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2356 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2357 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2358 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2359 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2361 goto CLEAR;
2364 if (strcmp(service, "ctnf") == 0) {
2365 if (action & A_START) {
2366 setup_conntrack();
2367 stop_firewall();
2368 start_firewall();
2370 goto CLEAR;
2373 if (strcmp(service, "wan") == 0) {
2374 if (action & A_STOP) {
2375 stop_wan();
2378 if (action & A_START) {
2379 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2380 start_wan(BOOT);
2381 sleep(2);
2382 force_to_dial();
2384 goto CLEAR;
2387 if (strcmp(service, "net") == 0) {
2388 if (action & A_STOP) {
2389 #ifdef TCONFIG_USB
2390 stop_nas_services();
2391 #endif
2392 #ifdef TCONFIG_IPV6
2393 stop_radvd();
2394 #endif
2395 stop_httpd();
2396 stop_dnsmasq();
2397 stop_nas();
2398 stop_wan();
2399 stop_arpbind();
2400 stop_lan();
2401 stop_vlan();
2403 if (action & A_START) {
2404 start_vlan();
2405 start_lan();
2406 start_arpbind();
2407 start_wan(BOOT);
2408 start_nas();
2409 start_dnsmasq();
2410 start_httpd();
2411 #ifdef TCONFIG_IPV6
2412 start_radvd();
2413 #endif
2414 start_wl();
2415 #ifdef TCONFIG_USB
2416 start_nas_services();
2417 #endif
2419 goto CLEAR;
2422 if (strcmp(service, "nas") == 0) {
2423 if (action & A_STOP) {
2424 stop_nas();
2426 if (action & A_START) {
2427 start_nas();
2428 start_wl();
2430 goto CLEAR;
2433 if (strcmp(service, "rstats") == 0) {
2434 if (action & A_STOP) stop_rstats();
2435 if (action & A_START) start_rstats(0);
2436 goto CLEAR;
2439 if (strcmp(service, "rstatsnew") == 0) {
2440 if (action & A_STOP) stop_rstats();
2441 if (action & A_START) start_rstats(1);
2442 goto CLEAR;
2445 if (strcmp(service, "cstats") == 0) {
2446 if (action & A_STOP) stop_cstats();
2447 if (action & A_START) start_cstats(0);
2448 goto CLEAR;
2451 if (strcmp(service, "cstatsnew") == 0) {
2452 if (action & A_STOP) stop_cstats();
2453 if (action & A_START) start_cstats(1);
2454 goto CLEAR;
2457 if (strcmp(service, "sched") == 0) {
2458 if (action & A_STOP) stop_sched();
2459 if (action & A_START) start_sched();
2460 goto CLEAR;
2463 #ifdef TCONFIG_BT
2464 if (strcmp(service, "bittorrent") == 0) {
2465 if (action & A_STOP) {
2466 stop_bittorrent();
2468 stop_firewall(); start_firewall(); // always restarted
2469 if (action & A_START) {
2470 start_bittorrent();
2472 goto CLEAR;
2474 #endif
2476 #ifdef TCONFIG_NFS
2477 if (strcmp(service, "nfs") == 0) {
2478 if (action & A_STOP) stop_nfs();
2479 if (action & A_START) start_nfs();
2480 goto CLEAR;
2482 #endif
2484 #ifdef TCONFIG_SNMP
2485 if (strcmp(service, "snmp") == 0) {
2486 if (action & A_STOP) stop_snmp();
2487 if (action & A_START) start_snmp();
2488 goto CLEAR;
2490 #endif
2492 #ifdef TCONFIG_USB
2493 // !!TB - USB Support
2494 if (strcmp(service, "usb") == 0) {
2495 if (action & A_STOP) stop_usb();
2496 if (action & A_START) {
2497 start_usb();
2498 // restart Samba and ftp since they may be killed by stop_usb()
2499 restart_nas_services(0, 1);
2500 // remount all partitions by simulating hotplug event
2501 add_remove_usbhost("-1", 1);
2503 goto CLEAR;
2506 if (strcmp(service, "usbapps") == 0) {
2507 if (action & A_STOP) stop_nas_services();
2508 if (action & A_START) start_nas_services();
2509 goto CLEAR;
2511 #endif
2513 #ifdef TCONFIG_FTP
2514 // !!TB - FTP Server
2515 if (strcmp(service, "ftpd") == 0) {
2516 if (action & A_STOP) stop_ftpd();
2517 setup_conntrack();
2518 stop_firewall();
2519 start_firewall();
2520 if (action & A_START) start_ftpd();
2521 goto CLEAR;
2523 #endif
2525 #ifdef TCONFIG_MEDIA_SERVER
2526 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2527 if (action & A_STOP) stop_media_server();
2528 if (action & A_START) start_media_server();
2529 goto CLEAR;
2531 #endif
2533 #ifdef TCONFIG_SAMBASRV
2534 // !!TB - Samba
2535 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2536 if (action & A_STOP) stop_samba();
2537 if (action & A_START) {
2538 create_passwd();
2539 stop_dnsmasq();
2540 start_dnsmasq();
2541 start_samba();
2543 goto CLEAR;
2545 #endif
2547 #ifdef TCONFIG_OPENVPN
2548 if (strncmp(service, "vpnclient", 9) == 0) {
2549 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2550 if (action & A_START) start_vpnclient(atoi(&service[9]));
2551 goto CLEAR;
2554 if (strncmp(service, "vpnserver", 9) == 0) {
2555 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2556 if (action & A_START) start_vpnserver(atoi(&service[9]));
2557 goto CLEAR;
2559 #endif
2561 #ifdef TCONFIG_NOCAT
2562 if (strcmp(service, "splashd") == 0) {
2563 if (action & A_STOP) stop_splashd();
2564 if (action & A_START) start_splashd();
2565 goto CLEAR;
2567 #endif
2569 CLEAR:
2570 if (next) goto TOP;
2572 // some functions check action_service and must be cleared at end -- zzz
2573 nvram_set("action_service", "");
2575 // Force recheck in 500 msec
2576 setitimer(ITIMER_REAL, &pop_tv, NULL);
2579 static void do_service(const char *name, const char *action, int user)
2581 int n;
2582 char s[64];
2584 n = 150;
2585 while (!nvram_match("action_service", "")) {
2586 if (user) {
2587 putchar('*');
2588 fflush(stdout);
2590 else if (--n < 0) break;
2591 usleep(100 * 1000);
2594 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2595 nvram_set("action_service", s);
2596 kill(1, SIGUSR1);
2598 n = 150;
2599 while (nvram_match("action_service", s)) {
2600 if (user) {
2601 putchar('.');
2602 fflush(stdout);
2604 else if (--n < 0) {
2605 break;
2607 usleep(100 * 1000);
2611 int service_main(int argc, char *argv[])
2613 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2614 do_service(argv[1], argv[2], 1);
2615 printf("\nDone.\n");
2616 return 0;
2619 void start_service(const char *name)
2621 do_service(name, "start", 0);
2624 void stop_service(const char *name)
2626 do_service(name, "stop", 0);
2630 void restart_service(const char *name)
2632 do_service(name, "restart", 0);