Merge branch 'tomato-RT' into Toastman-RT
[tomato.git] / release / src / router / rc / services.c
bloba51de985082d86dea17266ed6bb7a00ff4bb2aed
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/hosts.dnsmasq";
57 static const char dmresolv[] = "/etc/resolv.dnsmasq";
59 static pid_t pid_dnsmasq = -1;
61 static int is_wet(int idx, int unit, int subunit, void *param)
63 return nvram_match(wl_nvname("mode", unit, subunit), "wet");
66 void start_dnsmasq()
68 FILE *f;
69 const char *nv;
70 char buf[512];
71 char lan[24];
72 const char *router_ip;
73 const char *lan_ifname;
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;
82 int dhcp_start;
83 int dhcp_count;
84 int dhcp_lease;
85 int do_dhcpd;
86 int do_dns;
88 TRACE_PT("begin\n");
90 if (getpid() != 1) {
91 start_service("dnsmasq");
92 return;
95 stop_dnsmasq();
97 if (foreach_wif(1, NULL, is_wet)) return;
99 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
101 lan_ifname = nvram_safe_get("lan_ifname");
102 router_ip = nvram_safe_get("lan_ipaddr");
103 strlcpy(lan, router_ip, sizeof(lan));
104 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
106 fprintf(f,
107 "pid-file=/var/run/dnsmasq.pid\n"
108 "interface=%s\n",
109 lan_ifname);
110 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
111 if (*nv) fprintf(f, "domain=%s\n", nv);
114 // dns
115 const dns_list_t *dns = get_dns(); // this always points to a static buffer
117 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
118 else n = 4096;
119 fprintf(f,
120 "resolv-file=%s\n" // the real stuff is here
121 "addn-hosts=%s\n" // "
122 "expand-hosts\n" // expand hostnames in hosts file
123 "min-port=%u\n", // min port used for random src port
124 dmresolv, dmhosts, n);
125 do_dns = nvram_match("dhcpd_dmdns", "1");
127 // DNS rebinding protection, will discard upstream RFC1918 responses
128 if (nvram_get_int("dns_norebind")) {
129 fprintf(f,
130 "stop-dns-rebind\n"
131 "rebind-localhost-ok\n");
132 // allow RFC1918 responses for server domain
133 switch (get_wan_proto()) {
134 case WP_PPTP:
135 nv = nvram_get("pptp_server_ip");
136 break;
137 case WP_L2TP:
138 nv = nvram_get("l2tp_server_ip");
139 break;
140 default:
141 nv = NULL;
142 break;
144 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
147 for (n = 0 ; n < dns->count; ++n) {
148 if (dns->dns[n].port != 53) {
149 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
153 // dhcp
154 do_dhcpd = nvram_match("lan_proto", "dhcp");
155 if (do_dhcpd) {
156 dhcp_lease = nvram_get_int("dhcp_lease");
157 if (dhcp_lease <= 0) dhcp_lease = 1440;
159 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
160 if (n < 0) strcpy(sdhcp_lease, "infinite");
161 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
163 if (!do_dns) {
164 // if not using dnsmasq for dns
166 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
167 // no DNS might be temporary. use a low lease time to force clients to update.
168 dhcp_lease = 2;
169 strcpy(sdhcp_lease, "2m");
170 do_dns = 1;
172 else {
173 // pass the dns directly
174 buf[0] = 0;
175 for (n = 0 ; n < dns->count; ++n) {
176 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
177 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
180 fprintf(f, "dhcp-option=6%s\n", buf);
184 if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
185 fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
187 else {
188 // for compatibility
189 dhcp_start = nvram_get_int("dhcp_start");
190 dhcp_count = nvram_get_int("dhcp_num");
191 fprintf(f, "dhcp-range=%s%d,%s%d,%s,%dm\n",
192 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
195 nv = router_ip;
196 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
197 p = nvram_safe_get("lan_gateway");
198 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
201 n = nvram_get_int("dhcpd_lmax");
202 fprintf(f,
203 "dhcp-option=3,%s\n" // gateway
204 "dhcp-lease-max=%d\n",
206 (n > 0) ? n : 255);
208 if (nvram_get_int("dhcpd_auth") >= 0) {
209 fprintf(f, "dhcp-authoritative\n");
212 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
213 fprintf(f, "dhcp-option=44,%s\n", nv);
215 #ifdef TCONFIG_SAMBASRV
216 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
217 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
218 // Samba will serve as a WINS server
219 fprintf(f, "dhcp-option=44,0.0.0.0\n");
222 #endif
224 else {
225 fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
228 // write static lease entries & create hosts file
230 if ((hf = fopen(dmhosts, "w")) != NULL) {
231 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
232 fprintf(hf, "%s %s\n", router_ip, nv);
233 #ifdef TCONFIG_SAMBASRV
234 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
235 fprintf(hf, "%s %s\n", router_ip, nv);
236 #endif
237 p = (char *)get_wanip();
238 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
239 p = "127.0.0.1";
240 fprintf(hf, "%s wan-ip\n", p);
241 if (nv && (*nv))
242 fprintf(hf, "%s %s-wan\n", p, nv);
245 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
246 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
247 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
248 p = nvram_safe_get("dhcpd_static");
249 while ((e = strchr(p, '>')) != NULL) {
250 n = (e - p);
251 if (n > 105) {
252 p = e + 1;
253 continue;
256 strncpy(buf, p, n);
257 buf[n] = 0;
258 p = e + 1;
260 if ((e = strchr(buf, '<')) == NULL) continue;
261 *e = 0;
262 mac = buf;
264 ip = e + 1;
265 if ((e = strchr(ip, '<')) == NULL) continue;
266 *e = 0;
267 if (strchr(ip, '.') == NULL) {
268 ipn = atoi(ip);
269 if ((ipn <= 0) || (ipn > 255)) continue;
270 sprintf(ipbuf, "%s%d", lan, ipn);
271 ip = ipbuf;
273 else {
274 if (inet_addr(ip) == INADDR_NONE) continue;
277 name = e + 1;
279 if ((hf) && (*name != 0)) {
280 fprintf(hf, "%s %s\n", ip, name);
283 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
284 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
288 if (hf) fclose(hf);
292 #ifdef TCONFIG_OPENVPN
293 write_vpn_dnsmasq_config(f);
294 #endif
296 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
298 fappend(f, "/etc/dnsmasq.custom");
302 fclose(f);
304 if (do_dns) {
305 unlink("/etc/resolv.conf");
306 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
309 TRACE_PT("run dnsmasq\n");
311 // Default to some values we like, but allow the user to override them.
312 eval("dnsmasq", "-c", "1500", "--log-async");
314 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
315 pid_dnsmasq = -2;
318 TRACE_PT("end\n");
321 void stop_dnsmasq(void)
323 TRACE_PT("begin\n");
325 if (getpid() != 1) {
326 stop_service("dnsmasq");
327 return;
330 pid_dnsmasq = -1;
332 unlink("/etc/resolv.conf");
333 symlink(dmresolv, "/etc/resolv.conf");
335 killall_tk("dnsmasq");
337 TRACE_PT("end\n");
340 void clear_resolv(void)
342 f_write(dmresolv, NULL, 0, 0, 0); // blank
345 #ifdef TCONFIG_IPV6
346 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns)
348 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
349 struct in6_addr addr;
350 int cnt = 0;
352 foreach(p, dns, next) {
353 // verify that this is a valid IPv6 address
354 if (inet_pton(AF_INET6, p, &addr) == 1) {
355 fprintf(f, "%s%s\n", prefix, p);
356 ++cnt;
360 return cnt;
362 #endif
364 void dns_to_resolv(void)
366 FILE *f;
367 const dns_list_t *dns;
368 int i;
369 mode_t m;
371 m = umask(022); // 077 from pppoecd
372 if ((f = fopen(dmresolv, "w")) != NULL) {
373 // Check for VPN DNS entries
374 if (!write_vpn_resolv(f)) {
375 #ifdef TCONFIG_IPV6
376 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns")) == 0 || nvram_get_int("dns_addget"))
377 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"));
378 #endif
379 dns = get_dns(); // static buffer
380 if (dns->count == 0) {
381 // Put a pseudo DNS IP to trigger Connect On Demand
382 if (nvram_match("ppp_demand", "1")) {
383 switch (get_wan_proto()) {
384 case WP_PPPOE:
385 case WP_PPTP:
386 case WP_L2TP:
387 fprintf(f, "nameserver 1.1.1.1\n");
388 break;
392 else {
393 for (i = 0; i < dns->count; i++) {
394 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
395 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
400 fclose(f);
402 umask(m);
405 // -----------------------------------------------------------------------------
407 void start_httpd(void)
409 if (getpid() != 1) {
410 start_service("httpd");
411 return;
414 stop_httpd();
415 chdir("/www");
416 eval("httpd");
417 chdir("/");
420 void stop_httpd(void)
422 if (getpid() != 1) {
423 stop_service("httpd");
424 return;
427 killall_tk("httpd");
430 // -----------------------------------------------------------------------------
431 #ifdef TCONFIG_IPV6
433 static void add_ip6_lanaddr(void)
435 char ip[INET6_ADDRSTRLEN + 4];
436 const char *p;
438 p = ipv6_router_address(NULL);
439 if (*p) {
440 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
441 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
445 void start_ipv6_tunnel(void)
447 char ip[INET6_ADDRSTRLEN + 4];
448 struct in_addr addr4;
449 struct in6_addr addr;
450 const char *wanip, *mtu, *tun_dev;
451 int service;
453 service = get_ipv6_service();
454 tun_dev = nvram_safe_get("ipv6_ifname");
455 wanip = get_wanip();
456 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
457 modprobe("sit");
459 if (service == IPV6_ANYCAST_6TO4)
460 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
461 else
462 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
463 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
464 "remote", ip,
465 "local", (char *)wanip,
466 "ttl", nvram_safe_get("ipv6_tun_ttl"));
468 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
470 if (service == IPV6_ANYCAST_6TO4) {
471 add_ip6_lanaddr();
472 addr4.s_addr = 0;
473 memset(&addr, 0, sizeof(addr));
474 inet_aton(wanip, &addr4);
475 addr.s6_addr16[0] = htons(0x2002);
476 ipv6_mapaddr4(&addr, 16, &addr4, 0);
477 addr.s6_addr16[7] = htons(0x0001);
478 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
479 strncat(ip, "/16", sizeof(ip));
481 else {
482 snprintf(ip, sizeof(ip), "%s/%d",
483 nvram_safe_get("ipv6_tun_addr"),
484 nvram_get_int("ipv6_tun_addrlen") ? : 64);
486 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
487 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
489 // notify radvd of possible change
490 if (service == IPV6_ANYCAST_6TO4)
491 killall("radvd", SIGHUP);
494 void stop_ipv6_tunnel(void)
496 char *tun_dev = nvram_safe_get("ipv6_ifname");
497 eval("ip", "tunnel", "del", tun_dev);
498 modprobe_r("sit");
501 static pid_t pid_radvd = -1;
503 void start_radvd(void)
505 FILE *f;
506 char *prefix, *ip, *mtu;
507 int do_dns, do_6to4;
508 char *argv[] = { "radvd", NULL, NULL, NULL };
509 int pid, argc, service;
511 if (getpid() != 1) {
512 start_service("radvd");
513 return;
516 stop_radvd();
518 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
519 service = get_ipv6_service();
520 do_6to4 = (service == IPV6_ANYCAST_6TO4);
521 mtu = NULL;
523 switch (service) {
524 case IPV6_NATIVE_DHCP:
525 prefix = "::";
526 break;
527 case IPV6_ANYCAST_6TO4:
528 case IPV6_6IN4:
529 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
530 // fall through
531 default:
532 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
533 break;
535 if (!(*prefix)) prefix = "::";
537 // Create radvd.conf
538 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
540 ip = (char *)ipv6_router_address(NULL);
541 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
543 fprintf(f,
544 "interface %s\n"
545 "{\n"
546 " IgnoreIfMissing on;\n"
547 " AdvSendAdvert on;\n"
548 " MaxRtrAdvInterval 60;\n"
549 " AdvHomeAgentFlag off;\n"
550 " AdvManagedFlag off;\n"
551 "%s%s%s"
552 " prefix %s/64 \n"
553 " {\n"
554 " AdvOnLink on;\n"
555 " AdvAutonomous on;\n"
556 "%s%s%s"
557 " };\n"
558 " %s%s%s\n"
559 "};\n",
560 nvram_safe_get("lan_ifname"),
561 mtu ? " AdvLinkMTU " : "", mtu ? : "", mtu ? ";\n" : "",
562 prefix,
563 do_6to4 ? " Base6to4Interface " : "",
564 do_6to4 ? get_wanface() : "",
565 do_6to4 ? ";\n" : "",
566 do_dns ? "RDNSS " : "", do_dns ? ip : "", do_dns ? " { };" : "");
567 fclose(f);
569 // Start radvd
570 argc = 1;
571 if (nvram_get_int("debug_ipv6")) {
572 argv[argc++] = "-d";
573 argv[argc++] = "10";
575 argv[argc] = NULL;
576 _eval(argv, NULL, 0, &pid);
578 if (!nvram_contains_word("debug_norestart", "radvd")) {
579 pid_radvd = -2;
584 void stop_radvd(void)
586 if (getpid() != 1) {
587 stop_service("radvd");
588 return;
591 pid_radvd = -1;
592 killall_tk("radvd");
595 void start_ipv6(void)
597 int service;
599 service = get_ipv6_service();
600 enable_ip_forward();
602 // Check if turned on
603 switch (service) {
604 case IPV6_NATIVE:
605 case IPV6_6IN4:
606 case IPV6_MANUAL:
607 add_ip6_lanaddr();
608 break;
609 case IPV6_NATIVE_DHCP:
610 case IPV6_ANYCAST_6TO4:
611 nvram_set("ipv6_rtr_addr", "");
612 nvram_set("ipv6_prefix", "");
613 break;
616 if (service != IPV6_DISABLED) {
617 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
618 accept_ra(nvram_safe_get("lan_ifname"));
622 void stop_ipv6(void)
624 stop_ipv6_tunnel();
625 stop_dhcp6c();
626 eval("ip", "-6", "addr", "flush", "scope", "global");
629 #endif
631 // -----------------------------------------------------------------------------
633 void start_upnp(void)
635 if (getpid() != 1) {
636 start_service("upnp");
637 return;
640 if (get_wan_proto() == WP_DISABLED) return;
642 int enable;
643 FILE *f;
644 int upnp_port;
646 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
647 mkdir("/etc/upnp", 0777);
648 if (f_exists("/etc/upnp/config.alt")) {
649 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
651 else {
652 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
653 upnp_port = nvram_get_int("upnp_port");
654 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
656 char *lanip = nvram_safe_get("lan_ipaddr");
657 char *lanmask = nvram_safe_get("lan_netmask");
659 fprintf(f,
660 "ext_ifname=%s\n"
661 "listening_ip=%s/%s\n"
662 "port=%d\n"
663 "enable_upnp=%s\n"
664 "enable_natpmp=%s\n"
665 "secure_mode=%s\n"
666 "upnp_forward_chain=upnp\n"
667 "upnp_nat_chain=upnp\n"
668 "notify_interval=%d\n"
669 "system_uptime=yes\n"
670 "\n"
672 get_wanface(),
673 lanip, lanmask,
674 upnp_port,
675 (enable & 1) ? "yes" : "no", // upnp enable
676 (enable & 2) ? "yes" : "no", // natpmp enable
677 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
678 nvram_get_int("upnp_ssdp_interval")
681 if (nvram_get_int("upnp_clean")) {
682 int interval = nvram_get_int("upnp_clean_interval");
683 if (interval < 60) interval = 60;
684 fprintf(f,
685 "clean_ruleset_interval=%d\n"
686 "clean_ruleset_threshold=%d\n",
687 interval,
688 nvram_get_int("upnp_clean_threshold")
691 else
692 fprintf(f,"clean_ruleset_interval=0\n");
694 if (nvram_match("upnp_mnp", "1")) {
695 int https = nvram_get_int("https_enable");
696 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
697 https ? "s" : "", lanip,
698 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
700 else {
701 // Empty parameters are not included into XML service description
702 fprintf(f, "presentation_url=\n");
705 char uuid[45];
706 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
707 fprintf(f, "uuid=%s\n", uuid);
709 int ports[4];
710 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
711 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
712 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
713 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
714 fprintf(f,
715 "allow %d-%d %s/%s %d-%d\n",
716 ports[0], ports[1],
717 lanip, lanmask,
718 ports[2], ports[3]
721 else {
722 // by default allow only redirection of ports above 1024
723 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
726 fappend(f, "/jffs/upnpconfig.custom");
727 fappend(f, "/etc/upnp/config.custom");
728 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
729 fclose(f);
731 xstart("miniupnpd", "-f", "/etc/upnp/config");
737 void stop_upnp(void)
739 if (getpid() != 1) {
740 stop_service("upnp");
741 return;
744 killall_tk("miniupnpd");
747 // -----------------------------------------------------------------------------
749 static pid_t pid_crond = -1;
751 void start_cron(void)
753 stop_cron();
755 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
756 if (!nvram_contains_word("debug_norestart", "crond")) {
757 pid_crond = -2;
761 void stop_cron(void)
763 pid_crond = -1;
764 killall_tk("crond");
767 // -----------------------------------------------------------------------------
768 #ifdef LINUX26
770 static pid_t pid_hotplug2 = -1;
772 void start_hotplug2()
774 stop_hotplug2();
776 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
777 xstart("hotplug2", "--persistent", "--no-coldplug");
778 // FIXME: Don't remember exactly why I put "sleep" here -
779 // but it was not for a race with check_services()... - TB
780 sleep(1);
782 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
783 pid_hotplug2 = -2;
787 void stop_hotplug2(void)
789 pid_hotplug2 = -1;
790 killall_tk("hotplug2");
793 #endif /* LINUX26 */
794 // -----------------------------------------------------------------------------
796 // Written by Sparq in 2002/07/16
797 void start_zebra(void)
799 #ifdef TCONFIG_ZEBRA
800 if (getpid() != 1) {
801 start_service("zebra");
802 return;
805 FILE *fp;
807 char *lan_tx = nvram_safe_get("dr_lan_tx");
808 char *lan_rx = nvram_safe_get("dr_lan_rx");
809 char *wan_tx = nvram_safe_get("dr_wan_tx");
810 char *wan_rx = nvram_safe_get("dr_wan_rx");
812 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
813 return;
816 // empty
817 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
818 fclose(fp);
822 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
823 char *lan_ifname = nvram_safe_get("lan_ifname");
824 char *wan_ifname = nvram_safe_get("wan_ifname");
826 fprintf(fp, "router rip\n");
827 fprintf(fp, "network %s\n", lan_ifname);
828 fprintf(fp, "network %s\n", wan_ifname);
829 fprintf(fp, "redistribute connected\n");
830 //fprintf(fp, "redistribute static\n");
832 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
833 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
835 fprintf(fp, "interface %s\n", lan_ifname);
836 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
837 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
839 fprintf(fp, "interface %s\n", wan_ifname);
840 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
841 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
843 fprintf(fp, "router rip\n");
844 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
845 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
846 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
847 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
848 fprintf(fp, "access-list private deny any\n");
850 //fprintf(fp, "debug rip events\n");
851 //fprintf(fp, "log file /etc/ripd.log\n");
852 fclose(fp);
855 xstart("zebra", "-d");
856 xstart("ripd", "-d");
857 #endif
860 void stop_zebra(void)
862 #ifdef TCONFIG_ZEBRA
863 if (getpid() != 1) {
864 stop_service("zebra");
865 return;
868 killall("zebra", SIGTERM);
869 killall("ripd", SIGTERM);
871 unlink("/etc/zebra.conf");
872 unlink("/etc/ripd.conf");
873 #endif
876 // -----------------------------------------------------------------------------
878 void start_syslog(void)
880 char *argv[16];
881 int argc;
882 char *nv;
883 char *b_opt = "";
884 char rem[256];
885 int n;
886 char s[64];
887 char cfg[256];
888 char *rot_siz = "50";
890 argv[0] = "syslogd";
891 argc = 1;
893 if (nvram_match("log_remote", "1")) {
894 nv = nvram_safe_get("log_remoteip");
895 if (*nv) {
896 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
897 argv[argc++] = "-R";
898 argv[argc++] = rem;
902 if (nvram_match("log_file", "1")) {
903 argv[argc++] = "-L";
905 /* Read options: rotate_size(kb) num_backups logfilename.
906 * Ignore these settings and use defaults if the logfile cannot be written to.
908 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
909 if ((nv = strchr(cfg, '\n')))
910 *nv = 0;
912 if ((nv = strtok(cfg, " \t"))) {
913 if (isdigit(*nv))
914 rot_siz = nv;
917 if ((nv = strtok(NULL, " \t")))
918 b_opt = nv;
920 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
921 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
922 argv[argc++] = "-O";
923 argv[argc++] = nv;
925 else {
926 rot_siz = "50";
927 b_opt = "";
932 argv[argc++] = "-s";
933 argv[argc++] = rot_siz;
935 if (isdigit(*b_opt)) {
936 argv[argc++] = "-b";
937 argv[argc++] = b_opt;
941 if (argc > 1) {
942 argv[argc] = NULL;
943 _eval(argv, NULL, 0, NULL);
945 argv[0] = "klogd";
946 argv[1] = NULL;
947 _eval(argv, NULL, 0, NULL);
949 // used to be available in syslogd -m
950 n = nvram_get_int("log_mark");
951 if (n > 0) {
952 // n is in minutes
953 if (n < 60)
954 sprintf(rem, "*/%d * * * *", n);
955 else if (n < 60 * 24)
956 sprintf(rem, "0 */%d * * *", n / 60);
957 else
958 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
959 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
960 eval("cru", "a", "syslogdmark", s);
962 else {
963 eval("cru", "d", "syslogdmark");
968 void stop_syslog(void)
970 killall("klogd", SIGTERM);
971 killall("syslogd", SIGTERM);
974 // -----------------------------------------------------------------------------
976 static pid_t pid_igmp = -1;
978 void start_igmp_proxy(void)
980 FILE *fp;
982 pid_igmp = -1;
983 if (nvram_match("multicast_pass", "1")) {
984 if (get_wan_proto() == WP_DISABLED)
985 return;
987 if (f_exists("/etc/igmp.alt")) {
988 eval("igmpproxy", "/etc/igmp.alt");
990 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
991 fprintf(fp,
992 "quickleave\n"
993 "phyint %s upstream\n"
994 "\taltnet %s\n"
995 "phyint %s downstream ratelimit 0\n",
996 nvram_safe_get("wan_ifname"),
997 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
998 nvram_safe_get("lan_ifname"));
999 fclose(fp);
1000 eval("igmpproxy", "/etc/igmp.conf");
1002 else {
1003 return;
1005 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1006 pid_igmp = -2;
1011 void stop_igmp_proxy(void)
1013 pid_igmp = -1;
1014 killall_tk("igmpproxy");
1018 // -----------------------------------------------------------------------------
1020 void set_tz(void)
1022 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1025 void start_ntpc(void)
1027 set_tz();
1029 stop_ntpc();
1031 if (nvram_get_int("ntp_updates") >= 0) {
1032 xstart("ntpsync", "--init");
1036 void stop_ntpc(void)
1038 killall("ntpsync", SIGTERM);
1041 // -----------------------------------------------------------------------------
1043 static void stop_rstats(void)
1045 int n;
1046 int pid;
1048 n = 60;
1049 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1050 if (kill(pid, SIGTERM) != 0) break;
1051 sleep(1);
1055 static void start_rstats(int new)
1057 if (nvram_match("rstats_enable", "1")) {
1058 stop_rstats();
1059 if (new) xstart("rstats", "--new");
1060 else xstart("rstats");
1064 // -----------------------------------------------------------------------------
1066 // !!TB - FTP Server
1068 #ifdef TCONFIG_FTP
1069 static char *get_full_storage_path(char *val)
1071 static char buf[128];
1072 int len;
1074 if (val[0] == '/')
1075 len = sprintf(buf, "%s", val);
1076 else
1077 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1079 if (len > 1 && buf[len - 1] == '/')
1080 buf[len - 1] = 0;
1082 return buf;
1085 static char *nvram_storage_path(char *var)
1087 char *val = nvram_safe_get(var);
1088 return get_full_storage_path(val);
1091 char vsftpd_conf[] = "/etc/vsftpd.conf";
1092 char vsftpd_users[] = "/etc/vsftpd.users";
1093 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1095 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1097 static void start_ftpd(void)
1099 char tmp[256];
1100 FILE *fp, *f;
1101 char *buf;
1102 char *p, *q;
1103 char *user, *pass, *rights;
1105 if (getpid() != 1) {
1106 start_service("ftpd");
1107 return;
1110 if (!nvram_get_int("ftp_enable")) return;
1112 mkdir_if_none(vsftpd_users);
1113 mkdir_if_none("/var/run/vsftpd");
1115 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1116 return;
1118 if (nvram_get_int("ftp_super"))
1120 /* rights */
1121 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1122 if ((f = fopen(tmp, "w")))
1124 fprintf(f,
1125 "dirlist_enable=yes\n"
1126 "write_enable=yes\n"
1127 "download_enable=yes\n");
1128 fclose(f);
1132 #ifdef TCONFIG_SAMBASRV
1133 if (nvram_match("smbd_cset", "utf8"))
1134 fprintf(fp, "utf8=yes\n");
1135 #endif
1137 if (nvram_invmatch("ftp_anonymous", "0"))
1139 fprintf(fp,
1140 "anon_allow_writable_root=yes\n"
1141 "anon_world_readable_only=no\n"
1142 "anon_umask=022\n");
1144 /* rights */
1145 sprintf(tmp, "%s/ftp", vsftpd_users);
1146 if ((f = fopen(tmp, "w")))
1148 if (nvram_match("ftp_dirlist", "0"))
1149 fprintf(f, "dirlist_enable=yes\n");
1150 if (nvram_match("ftp_anonymous", "1") ||
1151 nvram_match("ftp_anonymous", "3"))
1152 fprintf(f, "write_enable=yes\n");
1153 if (nvram_match("ftp_anonymous", "1") ||
1154 nvram_match("ftp_anonymous", "2"))
1155 fprintf(f, "download_enable=yes\n");
1156 fclose(f);
1158 if (nvram_match("ftp_anonymous", "1") ||
1159 nvram_match("ftp_anonymous", "3"))
1160 fprintf(fp,
1161 "anon_upload_enable=yes\n"
1162 "anon_mkdir_write_enable=yes\n"
1163 "anon_other_write_enable=yes\n");
1164 } else {
1165 fprintf(fp, "anonymous_enable=no\n");
1168 fprintf(fp,
1169 "dirmessage_enable=yes\n"
1170 "download_enable=no\n"
1171 "dirlist_enable=no\n"
1172 "hide_ids=yes\n"
1173 "syslog_enable=yes\n"
1174 "local_enable=yes\n"
1175 "local_umask=022\n"
1176 "chmod_enable=no\n"
1177 "chroot_local_user=yes\n"
1178 "check_shell=no\n"
1179 "log_ftp_protocol=%s\n"
1180 "user_config_dir=%s\n"
1181 "passwd_file=%s\n"
1182 "listen%s=yes\n"
1183 "listen_port=%s\n"
1184 "background=yes\n"
1185 "isolate=no\n"
1186 "max_clients=%d\n"
1187 "max_per_ip=%d\n"
1188 "max_login_fails=1\n"
1189 "idle_session_timeout=%s\n"
1190 "use_sendfile=no\n"
1191 "anon_max_rate=%d\n"
1192 "local_max_rate=%d\n"
1193 "%s\n",
1194 nvram_get_int("log_ftp") ? "yes" : "no",
1195 vsftpd_users, vsftpd_passwd,
1196 #ifdef TCONFIG_IPV6
1197 ipv6_enabled() ? "_ipv6" : "",
1198 #else
1200 #endif
1201 nvram_get("ftp_port") ? : "21",
1202 nvram_get_int("ftp_max"),
1203 nvram_get_int("ftp_ipmax"),
1204 nvram_get("ftp_staytimeout") ? : "300",
1205 nvram_get_int("ftp_anonrate") * 1024,
1206 nvram_get_int("ftp_rate") * 1024,
1207 nvram_safe_get("ftp_custom"));
1209 fclose(fp);
1211 /* prepare passwd file and default users */
1212 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1213 return;
1215 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1216 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1218 fprintf(fp, /* anonymous, admin, nobody */
1219 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1220 "%s:%s:0:0:root:/:/sbin/nologin\n"
1221 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1222 nvram_storage_path("ftp_anonroot"), user,
1223 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1224 MOUNT_ROOT);
1226 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1229 username<password<rights
1230 rights:
1231 Read/Write
1232 Read Only
1233 View Only
1234 Private
1236 p = buf;
1237 while ((q = strsep(&p, ">")) != NULL) {
1238 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
1239 if (!user || !pass) continue;
1241 /* directory */
1242 if (strncmp(rights, "Private", 7) == 0)
1244 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1245 mkdir_if_none(tmp);
1247 else
1248 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
1250 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1251 user, crypt(pass, "$1$"), user, tmp);
1253 /* rights */
1254 sprintf(tmp, "%s/%s", vsftpd_users, user);
1255 if ((f = fopen(tmp, "w")))
1257 tmp[0] = 0;
1258 if (nvram_invmatch("ftp_dirlist", "1"))
1259 strcat(tmp, "dirlist_enable=yes\n");
1260 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1261 strcat(tmp, "download_enable=yes\n");
1262 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1263 strcat(tmp, "write_enable=yes\n");
1265 fputs(tmp, f);
1266 fclose(f);
1269 free(buf);
1272 fclose(fp);
1273 killall("vsftpd", SIGHUP);
1275 /* start vsftpd if it's not already running */
1276 if (pidof("vsftpd") <= 0)
1277 xstart("vsftpd");
1280 static void stop_ftpd(void)
1282 if (getpid() != 1) {
1283 stop_service("ftpd");
1284 return;
1287 killall_tk("vsftpd");
1288 unlink(vsftpd_passwd);
1289 unlink(vsftpd_conf);
1290 eval("rm", "-rf", vsftpd_users);
1292 #endif // TCONFIG_FTP
1294 // -----------------------------------------------------------------------------
1296 // !!TB - Samba
1298 #ifdef TCONFIG_SAMBASRV
1299 static void kill_samba(int sig)
1301 if (sig == SIGTERM) {
1302 killall_tk("smbd");
1303 killall_tk("nmbd");
1305 else {
1306 killall("smbd", sig);
1307 killall("nmbd", sig);
1311 static void start_samba(void)
1313 FILE *fp;
1314 DIR *dir = NULL;
1315 struct dirent *dp;
1316 char nlsmod[15];
1317 int mode;
1318 char *nv;
1320 if (getpid() != 1) {
1321 start_service("smbd");
1322 return;
1325 mode = nvram_get_int("smbd_enable");
1326 if (!mode || !nvram_invmatch("lan_hostname", ""))
1327 return;
1329 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1330 return;
1332 fprintf(fp, "[global]\n"
1333 " interfaces = %s\n"
1334 " bind interfaces only = yes\n"
1335 " workgroup = %s\n"
1336 " netbios name = %s\n"
1337 " server string = %s\n"
1338 " guest account = nobody\n"
1339 " security = user\n"
1340 " %s\n"
1341 " guest ok = %s\n"
1342 " guest only = no\n"
1343 " browseable = yes\n"
1344 " syslog only = yes\n"
1345 " timestamp logs = no\n"
1346 " syslog = 1\n"
1347 " encrypt passwords = yes\n"
1348 " preserve case = yes\n"
1349 " short preserve case = yes\n",
1350 nvram_safe_get("lan_ifname"),
1351 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1352 nvram_safe_get("lan_hostname"),
1353 nvram_get("router_name") ? : "Tomato",
1354 mode == 2 ? "" : "map to guest = Bad User",
1355 mode == 2 ? "no" : "yes" // guest ok
1358 if (nvram_get_int("smbd_wins")) {
1359 nv = nvram_safe_get("wan_wins");
1360 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1361 fprintf(fp, " wins support = yes\n");
1365 if (nvram_get_int("smbd_master")) {
1366 fprintf(fp,
1367 " domain master = yes\n"
1368 " local master = yes\n"
1369 " preferred master = yes\n"
1370 " os level = 65\n");
1373 nv = nvram_safe_get("smbd_cpage");
1374 if (*nv) {
1375 #ifndef TCONFIG_SAMBA3
1376 fprintf(fp, " client code page = %s\n", nv);
1377 #endif
1378 sprintf(nlsmod, "nls_cp%s", nv);
1380 nv = nvram_safe_get("smbd_nlsmod");
1381 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1382 modprobe_r(nv);
1384 modprobe(nlsmod);
1385 nvram_set("smbd_nlsmod", nlsmod);
1388 #ifndef TCONFIG_SAMBA3
1389 if (nvram_match("smbd_cset", "utf8"))
1390 fprintf(fp, " coding system = utf8\n");
1391 else if (nvram_invmatch("smbd_cset", ""))
1392 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1393 #endif
1395 nv = nvram_safe_get("smbd_custom");
1396 /* add socket options unless overriden by the user */
1397 if (strstr(nv, "socket options") == NULL) {
1398 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1400 fprintf(fp, "%s\n\n", nv);
1402 /* configure shares */
1404 char *buf;
1405 char *p, *q;
1406 char *name, *path, *comment, *writeable, *hidden;
1407 int cnt = 0;
1409 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1411 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1413 p = buf;
1414 while ((q = strsep(&p, ">")) != NULL) {
1415 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1416 if (!path || !name) continue;
1418 /* share name */
1419 fprintf(fp, "\n[%s]\n", name);
1421 /* path */
1422 fprintf(fp, " path = %s\n", path);
1424 /* access level */
1425 if (!strcmp(writeable, "1"))
1426 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1427 if (!strcmp(hidden, "1"))
1428 fprintf(fp, " browseable = no\n");
1430 /* comment */
1431 if (comment)
1432 fprintf(fp, " comment = %s\n", comment);
1434 cnt++;
1436 free(buf);
1439 /* Share every mountpoint below MOUNT_ROOT */
1440 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1441 while ((dp = readdir(dir))) {
1442 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1444 char path[256];
1445 struct stat sb;
1446 int thisdev;
1448 /* Only if is a directory and is mounted */
1449 sprintf(path, "%s/%s", MOUNT_ROOT, dp->d_name);
1450 sb.st_mode = S_IFDIR; /* failsafe */
1451 stat(path, &sb);
1452 if (!S_ISDIR(sb.st_mode))
1453 continue;
1455 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1456 strcat(path, "/.");
1457 stat(path, &sb);
1458 thisdev = sb.st_dev;
1459 strcat(path, ".");
1460 ++sb.st_dev; /* failsafe */
1461 stat(path, &sb);
1462 if (thisdev == sb.st_dev)
1463 continue;
1465 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1466 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1467 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1468 if (nvram_match("smbd_autoshare", "3")) // Hidden
1469 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1470 dp->d_name, MOUNT_ROOT, dp->d_name);
1471 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1472 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1474 cnt++;
1478 if (dir) closedir(dir);
1480 if (cnt == 0) {
1481 /* by default share MOUNT_ROOT as read-only */
1482 fprintf(fp, "\n[share]\n"
1483 " path = %s\n"
1484 " writable = no\n",
1485 MOUNT_ROOT);
1488 fclose(fp);
1490 mkdir_if_none("/var/run/samba");
1491 mkdir_if_none("/etc/samba");
1493 /* write smbpasswd */
1494 #ifdef TCONFIG_SAMBA3
1495 eval("smbpasswd", "nobody", "\"\"");
1496 #else
1497 eval("smbpasswd", "-a", "nobody", "\"\"");
1498 #endif
1499 if (mode == 2) {
1500 char *smbd_user;
1501 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1502 smbd_user = "nas";
1503 #ifdef TCONFIG_SAMBA3
1504 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1505 #else
1506 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1507 #endif
1510 kill_samba(SIGHUP);
1511 int ret1 = 0, ret2 = 0;
1512 /* start samba if it's not already running */
1513 if (pidof("nmbd") <= 0)
1514 ret1 = xstart("nmbd", "-D");
1515 if (pidof("smbd") <= 0)
1516 ret2 = xstart("smbd", "-D");
1518 if (ret1 || ret2) kill_samba(SIGTERM);
1521 static void stop_samba(void)
1523 if (getpid() != 1) {
1524 stop_service("smbd");
1525 return;
1528 kill_samba(SIGTERM);
1529 /* clean up */
1530 unlink("/var/log/smb");
1531 unlink("/var/log/nmb");
1532 eval("rm", "-rf", "/var/run/samba");
1534 #endif // TCONFIG_SAMBASRV
1536 #ifdef TCONFIG_MEDIA_SERVER
1537 #define MEDIA_SERVER_APP "minidlna"
1539 static void start_media_server(void)
1541 FILE *f;
1542 int port, pid, https;
1543 char *dbdir;
1544 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1545 static int once = 1;
1547 if (getpid() != 1) {
1548 start_service("media");
1549 return;
1552 if (nvram_get_int("ms_sas") == 0)
1553 once = 0;
1555 if (nvram_get_int("ms_enable") != 0) {
1556 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1557 // no forced rescan
1558 argv[3] = NULL;
1560 nvram_unset("ms_rescan");
1562 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1563 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1565 else {
1566 if ((f = fopen(argv[2], "w")) != NULL) {
1567 port = nvram_get_int("ms_port");
1568 https = nvram_get_int("https_enable");
1569 dbdir = nvram_safe_get("ms_dbdir");
1570 if (!(*dbdir)) dbdir = NULL;
1571 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1573 fprintf(f,
1574 "network_interface=%s\n"
1575 "port=%d\n"
1576 "friendly_name=%s\n"
1577 "db_dir=%s/.db\n"
1578 "enable_tivo=%s\n"
1579 "strict_dlna=%s\n"
1580 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1581 "inotify=yes\n"
1582 "notify_interval=600\n"
1583 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\n"
1584 "\n",
1585 nvram_safe_get("lan_ifname"),
1586 (port < 0) || (port >= 0xffff) ? 0 : port,
1587 nvram_get("router_name") ? : "Tomato",
1588 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1589 nvram_get_int("ms_tivo") ? "yes" : "no",
1590 nvram_get_int("ms_stdlna") ? "yes" : "no",
1591 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1594 // media directories
1595 char *buf, *p, *q;
1596 char *path, *restrict;
1598 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1599 /* path<restrict[A|V|P|] */
1601 p = buf;
1602 while ((q = strsep(&p, ">")) != NULL) {
1603 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1604 continue;
1605 fprintf(f, "media_dir=%s%s%s\n",
1606 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1608 free(buf);
1611 fclose(f);
1615 /* start media server if it's not already running */
1616 if (pidof(MEDIA_SERVER_APP) <= 0) {
1617 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1618 /* If we started the media server successfully, wait 1 sec
1619 * to let it die if it can't open the database file.
1620 * If it's still alive after that, assume it's running and
1621 * disable forced once-after-reboot rescan.
1623 sleep(1);
1624 if (pidof(MEDIA_SERVER_APP) > 0)
1625 once = 0;
1631 static void stop_media_server(void)
1633 if (getpid() != 1) {
1634 stop_service("media");
1635 return;
1638 killall_tk(MEDIA_SERVER_APP);
1640 #endif // TCONFIG_MEDIA_SERVER
1642 #ifdef TCONFIG_USB
1643 static void start_nas_services(void)
1645 if (getpid() != 1) {
1646 start_service("usbapps");
1647 return;
1650 #ifdef TCONFIG_SAMBASRV
1651 start_samba();
1652 #endif
1653 #ifdef TCONFIG_FTP
1654 start_ftpd();
1655 #endif
1656 #ifdef TCONFIG_MEDIA_SERVER
1657 start_media_server();
1658 #endif
1661 static void stop_nas_services(void)
1663 if (getpid() != 1) {
1664 stop_service("usbapps");
1665 return;
1668 #ifdef TCONFIG_MEDIA_SERVER
1669 stop_media_server();
1670 #endif
1671 #ifdef TCONFIG_FTP
1672 stop_ftpd();
1673 #endif
1674 #ifdef TCONFIG_SAMBASRV
1675 stop_samba();
1676 #endif
1679 void restart_nas_services(int stop, int start)
1681 int fd = file_lock("usb");
1682 /* restart all NAS applications */
1683 if (stop)
1684 stop_nas_services();
1685 if (start)
1686 start_nas_services();
1687 file_unlock(fd);
1689 #endif // TCONFIG_USB
1691 // -----------------------------------------------------------------------------
1693 /* -1 = Don't check for this program, it is not expected to be running.
1694 * Other = This program has been started and should be kept running. If no
1695 * process with the name is running, call func to restart it.
1696 * Note: At startup, dnsmasq forks a short-lived child which forks a
1697 * long-lived (grand)child. The parents terminate.
1698 * Many daemons use this technique.
1700 static void _check(pid_t pid, const char *name, void (*func)(void))
1702 if (pid == -1) return;
1704 if (pidof(name) > 0) return;
1706 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
1707 func();
1709 // Force recheck in 500 msec
1710 setitimer(ITIMER_REAL, &pop_tv, NULL);
1713 void check_services(void)
1715 TRACE_PT("keep alive\n");
1717 // Periodically reap any zombies
1718 setitimer(ITIMER_REAL, &zombie_tv, NULL);
1720 #ifdef LINUX26
1721 _check(pid_hotplug2, "hotplug2", start_hotplug2);
1722 #endif
1723 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
1724 _check(pid_crond, "crond", start_cron);
1725 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
1726 #ifdef TCONFIG_IPV6
1727 _check(pid_radvd, "radvd", start_radvd);
1728 #endif
1731 // -----------------------------------------------------------------------------
1733 void start_services(void)
1735 static int once = 1;
1737 if (once) {
1738 once = 0;
1740 if (nvram_get_int("telnetd_eas")) start_telnetd();
1741 if (nvram_get_int("sshd_eas")) start_sshd();
1744 // start_syslog();
1745 start_nas();
1746 start_zebra();
1747 start_dnsmasq();
1748 start_cifs();
1749 start_httpd();
1750 start_cron();
1751 // start_upnp();
1752 start_rstats(0);
1753 start_sched();
1754 #ifdef TCONFIG_IPV6
1755 /* note: starting radvd here might be too early in case of
1756 * DHCPv6 because we won't have received a prefix and so it
1757 * will disable advertisements, but the SIGHUP sent from
1758 * dhcp6c-state will restart them.
1760 start_radvd();
1761 #endif
1762 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1765 void stop_services(void)
1767 clear_resolv();
1769 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1770 #ifdef TCONFIG_IPV6
1771 stop_radvd();
1772 #endif
1773 stop_sched();
1774 stop_rstats();
1775 // stop_upnp();
1776 stop_cron();
1777 stop_httpd();
1778 stop_cifs();
1779 stop_dnsmasq();
1780 stop_zebra();
1781 stop_nas();
1782 // stop_syslog();
1785 // -----------------------------------------------------------------------------
1787 /* nvram "action_service" is: "service-action[-modifier]"
1788 * action is something like "stop" or "start" or "restart"
1789 * optional modifier is "c" for the "service" command-line command
1791 void exec_service(void)
1793 const int A_START = 1;
1794 const int A_STOP = 2;
1795 const int A_RESTART = 1|2;
1796 char buffer[128];
1797 char *service;
1798 char *act;
1799 char *next;
1800 char *modifier;
1801 int action, user;
1802 int i;
1804 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1805 next = buffer;
1807 TOP:
1808 act = strsep(&next, ",");
1809 service = strsep(&act, "-");
1810 if (act == NULL) {
1811 next = NULL;
1812 goto CLEAR;
1814 modifier = act;
1815 strsep(&modifier, "-");
1817 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
1819 if (strcmp(act, "start") == 0) action = A_START;
1820 else if (strcmp(act, "stop") == 0) action = A_STOP;
1821 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1822 else action = 0;
1823 user = (modifier != NULL && *modifier == 'c');
1825 if (strcmp(service, "dhcpc") == 0) {
1826 if (action & A_STOP) stop_dhcpc();
1827 if (action & A_START) start_dhcpc();
1828 goto CLEAR;
1831 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1832 if (action & A_STOP) stop_dnsmasq();
1833 if (action & A_START) {
1834 dns_to_resolv();
1835 start_dnsmasq();
1837 goto CLEAR;
1840 if (strcmp(service, "firewall") == 0) {
1841 if (action & A_STOP) {
1842 stop_firewall();
1843 stop_igmp_proxy();
1845 if (action & A_START) {
1846 start_firewall();
1847 start_igmp_proxy();
1849 goto CLEAR;
1852 if (strcmp(service, "restrict") == 0) {
1853 if (action & A_STOP) {
1854 stop_firewall();
1856 if (action & A_START) {
1857 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1859 start_firewall();
1861 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1862 if (i == 1) {
1863 if (nvram_get_int("rrules_radio") < 0) {
1864 eval("radio", "on");
1868 goto CLEAR;
1871 if (strcmp(service, "qos") == 0) {
1872 if (action & A_STOP) {
1873 stop_qos();
1875 stop_firewall(); start_firewall(); // always restarted
1876 if (action & A_START) {
1877 start_qos();
1878 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1880 goto CLEAR;
1883 if (strcmp(service, "qoslimit") == 0) {
1884 if (action & A_STOP) {
1885 new_qoslimit_stop();
1887 stop_firewall(); start_firewall(); // always restarted
1888 if (action & A_START) {
1889 new_qoslimit_start();
1891 goto CLEAR;
1894 if (strcmp(service, "arpbind") == 0) {
1895 if (action & A_STOP) new_arpbind_stop();
1896 if (action & A_START) new_arpbind_start();
1897 goto CLEAR;
1900 if (strcmp(service, "upnp") == 0) {
1901 if (action & A_STOP) {
1902 stop_upnp();
1904 stop_firewall(); start_firewall(); // always restarted
1905 if (action & A_START) {
1906 start_upnp();
1908 goto CLEAR;
1911 if (strcmp(service, "telnetd") == 0) {
1912 if (action & A_STOP) stop_telnetd();
1913 if (action & A_START) start_telnetd();
1914 goto CLEAR;
1917 if (strcmp(service, "sshd") == 0) {
1918 if (action & A_STOP) stop_sshd();
1919 if (action & A_START) start_sshd();
1920 goto CLEAR;
1923 if (strcmp(service, "httpd") == 0) {
1924 if (action & A_STOP) stop_httpd();
1925 if (action & A_START) start_httpd();
1926 goto CLEAR;
1929 #ifdef TCONFIG_IPV6
1930 if (strcmp(service, "ipv6") == 0) {
1931 if (action & A_STOP) {
1932 stop_radvd();
1933 stop_ipv6();
1935 if (action & A_START) {
1936 start_ipv6();
1937 start_radvd();
1939 goto CLEAR;
1942 if (strcmp(service, "radvd") == 0) {
1943 if (action & A_STOP) {
1944 stop_radvd();
1946 if (action & A_START) {
1947 start_radvd();
1949 goto CLEAR;
1952 if (strncmp(service, "dhcp6", 5) == 0) {
1953 if (action & A_STOP) {
1954 stop_dhcp6c();
1956 if (action & A_START) {
1957 start_dhcp6c();
1959 goto CLEAR;
1961 #endif
1963 if (strcmp(service, "admin") == 0) {
1964 if (action & A_STOP) {
1965 stop_sshd();
1966 stop_telnetd();
1967 stop_httpd();
1969 stop_firewall(); start_firewall(); // always restarted
1970 if (action & A_START) {
1971 start_httpd();
1972 create_passwd();
1973 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1974 if (nvram_match("sshd_eas", "1")) start_sshd();
1976 goto CLEAR;
1979 if (strcmp(service, "ddns") == 0) {
1980 if (action & A_STOP) stop_ddns();
1981 if (action & A_START) start_ddns();
1982 goto CLEAR;
1985 if (strcmp(service, "ntpc") == 0) {
1986 if (action & A_STOP) stop_ntpc();
1987 if (action & A_START) start_ntpc();
1988 goto CLEAR;
1991 if (strcmp(service, "logging") == 0) {
1992 if (action & A_STOP) {
1993 stop_syslog();
1995 if (action & A_START) {
1996 start_syslog();
1998 if (!user) {
1999 // always restarted except from "service" command
2000 stop_cron(); start_cron();
2001 stop_firewall(); start_firewall();
2003 goto CLEAR;
2006 if (strcmp(service, "crond") == 0) {
2007 if (action & A_STOP) {
2008 stop_cron();
2010 if (action & A_START) {
2011 start_cron();
2013 goto CLEAR;
2016 #ifdef LINUX26
2017 if (strncmp(service, "hotplug", 7) == 0) {
2018 if (action & A_STOP) {
2019 stop_hotplug2();
2021 if (action & A_START) {
2022 start_hotplug2(1);
2024 goto CLEAR;
2026 #endif
2028 if (strcmp(service, "upgrade") == 0) {
2029 if (action & A_START) {
2030 #if TOMATO_SL
2031 stop_usbevent();
2032 stop_smbd();
2033 #endif
2034 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2035 stop_jffs2();
2036 // stop_cifs();
2037 stop_zebra();
2038 stop_cron();
2039 stop_ntpc();
2040 stop_upnp();
2041 // stop_dhcpc();
2042 killall("rstats", SIGTERM);
2043 killall("buttons", SIGTERM);
2044 stop_syslog();
2045 remove_storage_main(1); // !!TB - USB Support
2046 stop_usb(); // !!TB - USB Support
2048 goto CLEAR;
2051 #ifdef TCONFIG_CIFS
2052 if (strcmp(service, "cifs") == 0) {
2053 if (action & A_STOP) stop_cifs();
2054 if (action & A_START) start_cifs();
2055 goto CLEAR;
2057 #endif
2059 #ifdef TCONFIG_JFFS2
2060 if (strncmp(service, "jffs", 4) == 0) {
2061 if (action & A_STOP) stop_jffs2();
2062 if (action & A_START) start_jffs2();
2063 goto CLEAR;
2065 #endif
2067 if (strcmp(service, "zebra") == 0) {
2068 if (action & A_STOP) stop_zebra();
2069 if (action & A_START) start_zebra();
2070 goto CLEAR;
2073 if (strcmp(service, "routing") == 0) {
2074 if (action & A_STOP) {
2075 stop_zebra();
2076 do_static_routes(0); // remove old '_saved'
2077 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2079 stop_firewall();
2080 start_firewall();
2081 if (action & A_START) {
2082 do_static_routes(1); // add new
2083 start_zebra();
2084 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2086 goto CLEAR;
2089 if (strcmp(service, "ctnf") == 0) {
2090 if (action & A_START) {
2091 setup_conntrack();
2092 stop_firewall();
2093 start_firewall();
2095 goto CLEAR;
2098 if (strcmp(service, "wan") == 0) {
2099 if (action & A_STOP) {
2100 stop_wan();
2103 if (action & A_START) {
2104 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2105 start_wan(BOOT);
2106 sleep(2);
2107 force_to_dial();
2109 goto CLEAR;
2112 if (strcmp(service, "net") == 0) {
2113 if (action & A_STOP) {
2114 #ifdef TCONFIG_IPV6
2115 stop_radvd();
2116 #endif
2117 stop_httpd();
2118 stop_dnsmasq();
2119 stop_nas();
2120 stop_wan();
2121 stop_lan();
2122 stop_vlan();
2124 if (action & A_START) {
2125 start_vlan();
2126 start_lan();
2127 start_wan(BOOT);
2128 start_nas();
2129 start_dnsmasq();
2130 start_httpd();
2131 #ifdef TCONFIG_IPV6
2132 start_radvd();
2133 #endif
2134 start_wl();
2136 goto CLEAR;
2139 if (strcmp(service, "nas") == 0) {
2140 if (action & A_STOP) {
2141 stop_nas();
2143 if (action & A_START) {
2144 start_nas();
2145 start_wl();
2147 goto CLEAR;
2150 if (strcmp(service, "rstats") == 0) {
2151 if (action & A_STOP) stop_rstats();
2152 if (action & A_START) start_rstats(0);
2153 goto CLEAR;
2156 if (strcmp(service, "rstatsnew") == 0) {
2157 if (action & A_STOP) stop_rstats();
2158 if (action & A_START) start_rstats(1);
2159 goto CLEAR;
2162 if (strcmp(service, "sched") == 0) {
2163 if (action & A_STOP) stop_sched();
2164 if (action & A_START) start_sched();
2165 goto CLEAR;
2168 #ifdef TCONFIG_USB
2169 // !!TB - USB Support
2170 if (strcmp(service, "usb") == 0) {
2171 if (action & A_STOP) stop_usb();
2172 if (action & A_START) {
2173 start_usb();
2174 // restart Samba and ftp since they may be killed by stop_usb()
2175 restart_nas_services(0, 1);
2176 // remount all partitions by simulating hotplug event
2177 add_remove_usbhost("-1", 1);
2179 goto CLEAR;
2182 if (strcmp(service, "usbapps") == 0) {
2183 if (action & A_STOP) stop_nas_services();
2184 if (action & A_START) start_nas_services();
2185 goto CLEAR;
2187 #endif
2189 #ifdef TCONFIG_FTP
2190 // !!TB - FTP Server
2191 if (strcmp(service, "ftpd") == 0) {
2192 if (action & A_STOP) stop_ftpd();
2193 setup_conntrack();
2194 stop_firewall();
2195 start_firewall();
2196 if (action & A_START) start_ftpd();
2197 goto CLEAR;
2199 #endif
2201 #ifdef TCONFIG_MEDIA_SERVER
2202 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2203 if (action & A_STOP) stop_media_server();
2204 if (action & A_START) start_media_server();
2205 goto CLEAR;
2207 #endif
2209 #ifdef TCONFIG_SAMBASRV
2210 // !!TB - Samba
2211 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2212 if (action & A_STOP) stop_samba();
2213 if (action & A_START) {
2214 create_passwd();
2215 stop_dnsmasq();
2216 start_dnsmasq();
2217 start_samba();
2219 goto CLEAR;
2221 #endif
2223 #ifdef TCONFIG_OPENVPN
2224 if (strncmp(service, "vpnclient", 9) == 0) {
2225 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2226 if (action & A_START) start_vpnclient(atoi(&service[9]));
2227 goto CLEAR;
2230 if (strncmp(service, "vpnserver", 9) == 0) {
2231 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2232 if (action & A_START) start_vpnserver(atoi(&service[9]));
2233 goto CLEAR;
2235 #endif
2237 CLEAR:
2238 if (next) goto TOP;
2240 // some functions check action_service and must be cleared at end -- zzz
2241 nvram_set("action_service", "");
2243 // Force recheck in 500 msec
2244 setitimer(ITIMER_REAL, &pop_tv, NULL);
2247 static void do_service(const char *name, const char *action, int user)
2249 int n;
2250 char s[64];
2252 n = 150;
2253 while (!nvram_match("action_service", "")) {
2254 if (user) {
2255 putchar('*');
2256 fflush(stdout);
2258 else if (--n < 0) break;
2259 usleep(100 * 1000);
2262 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2263 nvram_set("action_service", s);
2264 kill(1, SIGUSR1);
2266 n = 150;
2267 while (nvram_match("action_service", s)) {
2268 if (user) {
2269 putchar('.');
2270 fflush(stdout);
2272 else if (--n < 0) {
2273 break;
2275 usleep(100 * 1000);
2279 int service_main(int argc, char *argv[])
2281 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2282 do_service(argv[1], argv[2], 1);
2283 printf("\nDone.\n");
2284 return 0;
2287 void start_service(const char *name)
2289 do_service(name, "start", 0);
2292 void stop_service(const char *name)
2294 do_service(name, "stop", 0);
2298 void restart_service(const char *name)
2300 do_service(name, "restart", 0);