Merge commit 'origin/tomato-RT' into tomato-shibby
[tomato.git] / release / src / router / rc / services.c
blob1f2b0b07ef7ff785867fd44ed2d13c67d7a4ce0a
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 void start_ipv6_sit_tunnel(void)
435 char *tun_dev = nvram_safe_get("ipv6_ifname");
436 char ip[INET6_ADDRSTRLEN + 4];
437 const char *wanip;
439 if (get_ipv6_service() == IPV6_6IN4) {
440 modprobe("sit");
441 snprintf(ip, sizeof(ip), "%s/%d",
442 nvram_safe_get("ipv6_tun_addr"),
443 nvram_get_int("ipv6_tun_addrlen") ? : 64);
444 wanip = get_wanip();
446 eval("ip", "tunnel", "add", tun_dev, "mode", "sit",
447 "remote", nvram_safe_get("ipv6_tun_v4end"),
448 "local", (char *)wanip,
449 "ttl", nvram_safe_get("ipv6_tun_ttl"));
450 if (nvram_get_int("ipv6_tun_mtu") > 0)
451 eval("ip", "link", "set", tun_dev, "mtu", nvram_safe_get("ipv6_tun_mtu"), "up");
452 else
453 eval("ip", "link", "set", tun_dev, "up");
454 eval("ip", "addr", "add", ip, "dev", tun_dev);
455 eval("ip", "route", "add", "::/0", "dev", tun_dev);
459 void stop_ipv6_sit_tunnel(void)
461 char *tun_dev = nvram_safe_get("ipv6_ifname");
462 eval("ip", "tunnel", "del", tun_dev);
463 modprobe_r("sit");
466 static pid_t pid_radvd = -1;
468 void start_radvd(void)
470 FILE *f;
471 char *prefix, *ip;
472 int do_dns;
473 char *argv[] = { "radvd", NULL, NULL, NULL };
474 int pid, argc;
476 if (getpid() != 1) {
477 start_service("radvd");
478 return;
481 stop_radvd();
483 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
485 switch (get_ipv6_service()) {
486 case IPV6_NATIVE_DHCP:
487 prefix = "::";
488 break;
489 default:
490 prefix = nvram_safe_get("ipv6_prefix");
491 break;
493 if (!(*prefix)) prefix = "::";
495 // Create radvd.conf
496 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
498 ip = (char *)ipv6_router_address(NULL);
499 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
501 fprintf(f,
502 "interface %s\n"
503 "{\n"
504 " AdvSendAdvert on;\n"
505 " MaxRtrAdvInterval 60;\n"
506 " AdvHomeAgentFlag off;\n"
507 " AdvManagedFlag off;\n"
508 " prefix %s/64 \n"
509 " {\n"
510 " AdvOnLink on;\n"
511 " AdvAutonomous on;\n"
512 " };\n"
513 " %s%s%s\n"
514 "};\n",
515 nvram_safe_get("lan_ifname"), prefix,
516 do_dns ? "RDNSS " : "", do_dns ? ip : "", do_dns ? " { };" : "");
517 fclose(f);
519 // Start radvd
520 argc = 1;
521 if (nvram_get_int("debug_ipv6")) {
522 argv[argc++] = "-d";
523 argv[argc++] = "10";
525 argv[argc] = NULL;
526 _eval(argv, NULL, 0, &pid);
528 if (!nvram_contains_word("debug_norestart", "radvd")) {
529 pid_radvd = -2;
534 void stop_radvd(void)
536 if (getpid() != 1) {
537 stop_service("radvd");
538 return;
541 pid_radvd = -1;
542 killall_tk("radvd");
545 void start_ipv6(void)
547 char *p;
548 char ip[INET6_ADDRSTRLEN + 4];
549 int service;
551 service = get_ipv6_service();
552 enable_ip_forward();
554 // Check if turned on
555 switch (service) {
556 case IPV6_NATIVE:
557 case IPV6_6IN4:
558 case IPV6_MANUAL:
559 p = (char *)ipv6_router_address(NULL);
560 if (*p) {
561 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
562 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
564 break;
567 if (service != IPV6_DISABLED) {
568 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
569 accept_ra(nvram_safe_get("lan_ifname"));
573 void stop_ipv6(void)
575 stop_ipv6_sit_tunnel();
576 stop_dhcp6c();
577 eval("ip", "-6", "addr", "flush", "scope", "global");
580 #endif
582 // -----------------------------------------------------------------------------
584 void start_upnp(void)
586 if (getpid() != 1) {
587 start_service("upnp");
588 return;
591 if (get_wan_proto() == WP_DISABLED) return;
593 int enable;
594 FILE *f;
595 int upnp_port;
597 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
598 mkdir("/etc/upnp", 0777);
599 if (f_exists("/etc/upnp/config.alt")) {
600 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
602 else {
603 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
604 upnp_port = nvram_get_int("upnp_port");
605 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
607 char *lanip = nvram_safe_get("lan_ipaddr");
608 char *lanmask = nvram_safe_get("lan_netmask");
610 fprintf(f,
611 "ext_ifname=%s\n"
612 "listening_ip=%s/%s\n"
613 "port=%d\n"
614 "enable_upnp=%s\n"
615 "enable_natpmp=%s\n"
616 "secure_mode=%s\n"
617 "upnp_forward_chain=upnp\n"
618 "upnp_nat_chain=upnp\n"
619 "notify_interval=%d\n"
620 "system_uptime=yes\n"
621 "\n"
623 get_wanface(),
624 lanip, lanmask,
625 upnp_port,
626 (enable & 1) ? "yes" : "no", // upnp enable
627 (enable & 2) ? "yes" : "no", // natpmp enable
628 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
629 nvram_get_int("upnp_ssdp_interval")
632 if (nvram_get_int("upnp_clean")) {
633 int interval = nvram_get_int("upnp_clean_interval");
634 if (interval < 60) interval = 60;
635 fprintf(f,
636 "clean_ruleset_interval=%d\n"
637 "clean_ruleset_threshold=%d\n",
638 interval,
639 nvram_get_int("upnp_clean_threshold")
642 else
643 fprintf(f,"clean_ruleset_interval=0\n");
645 if (nvram_match("upnp_mnp", "1")) {
646 int https = nvram_get_int("https_enable");
647 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
648 https ? "s" : "", lanip,
649 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
651 else {
652 // Empty parameters are not included into XML service description
653 fprintf(f, "presentation_url=\n");
656 char uuid[45];
657 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
658 fprintf(f, "uuid=%s\n", uuid);
660 int ports[4];
661 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
662 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
663 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
664 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
665 fprintf(f,
666 "allow %d-%d %s/%s %d-%d\n",
667 ports[0], ports[1],
668 lanip, lanmask,
669 ports[2], ports[3]
672 else {
673 // by default allow only redirection of ports above 1024
674 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
677 fappend(f, "/etc/upnp/config.custom");
678 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
679 fclose(f);
681 xstart("miniupnpd", "-f", "/etc/upnp/config");
687 void stop_upnp(void)
689 if (getpid() != 1) {
690 stop_service("upnp");
691 return;
694 killall_tk("miniupnpd");
697 // -----------------------------------------------------------------------------
699 static pid_t pid_crond = -1;
701 void start_cron(void)
703 stop_cron();
705 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
706 if (!nvram_contains_word("debug_norestart", "crond")) {
707 pid_crond = -2;
711 void stop_cron(void)
713 pid_crond = -1;
714 killall_tk("crond");
717 // -----------------------------------------------------------------------------
718 #ifdef LINUX26
720 static pid_t pid_hotplug2 = -1;
722 void start_hotplug2()
724 stop_hotplug2();
726 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
727 xstart("hotplug2", "--persistent", "--no-coldplug");
728 // FIXME: Don't remember exactly why I put "sleep" here -
729 // but it was not for a race with check_services()... - TB
730 sleep(1);
732 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
733 pid_hotplug2 = -2;
737 void stop_hotplug2(void)
739 pid_hotplug2 = -1;
740 killall_tk("hotplug2");
743 #endif /* LINUX26 */
744 // -----------------------------------------------------------------------------
746 // Written by Sparq in 2002/07/16
747 void start_zebra(void)
749 #ifdef TCONFIG_ZEBRA
750 if (getpid() != 1) {
751 start_service("zebra");
752 return;
755 FILE *fp;
757 char *lan_tx = nvram_safe_get("dr_lan_tx");
758 char *lan_rx = nvram_safe_get("dr_lan_rx");
759 char *wan_tx = nvram_safe_get("dr_wan_tx");
760 char *wan_rx = nvram_safe_get("dr_wan_rx");
762 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
763 return;
766 // empty
767 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
768 fclose(fp);
772 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
773 char *lan_ifname = nvram_safe_get("lan_ifname");
774 char *wan_ifname = nvram_safe_get("wan_ifname");
776 fprintf(fp, "router rip\n");
777 fprintf(fp, "network %s\n", lan_ifname);
778 fprintf(fp, "network %s\n", wan_ifname);
779 fprintf(fp, "redistribute connected\n");
780 //fprintf(fp, "redistribute static\n");
782 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
783 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
785 fprintf(fp, "interface %s\n", lan_ifname);
786 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
787 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
789 fprintf(fp, "interface %s\n", wan_ifname);
790 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
791 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
793 fprintf(fp, "router rip\n");
794 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
795 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
796 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
797 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
798 fprintf(fp, "access-list private deny any\n");
800 //fprintf(fp, "debug rip events\n");
801 //fprintf(fp, "log file /etc/ripd.log\n");
802 fclose(fp);
805 xstart("zebra", "-d");
806 xstart("ripd", "-d");
807 #endif
810 void stop_zebra(void)
812 #ifdef TCONFIG_ZEBRA
813 if (getpid() != 1) {
814 stop_service("zebra");
815 return;
818 killall("zebra", SIGTERM);
819 killall("ripd", SIGTERM);
821 unlink("/etc/zebra.conf");
822 unlink("/etc/ripd.conf");
823 #endif
826 // -----------------------------------------------------------------------------
828 void start_syslog(void)
830 char *argv[16];
831 int argc;
832 char *nv;
833 char *b_opt = "";
834 char rem[256];
835 int n;
836 char s[64];
837 char cfg[256];
838 char *rot_siz = "50";
840 argv[0] = "syslogd";
841 argc = 1;
843 if (nvram_match("log_remote", "1")) {
844 nv = nvram_safe_get("log_remoteip");
845 if (*nv) {
846 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
847 argv[argc++] = "-R";
848 argv[argc++] = rem;
852 if (nvram_match("log_file", "1")) {
853 argv[argc++] = "-L";
855 /* Read options: rotate_size(kb) num_backups logfilename.
856 * Ignore these settings and use defaults if the logfile cannot be written to.
858 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
859 if ((nv = strchr(cfg, '\n')))
860 *nv = 0;
862 if ((nv = strtok(cfg, " \t"))) {
863 if (isdigit(*nv))
864 rot_siz = nv;
867 if ((nv = strtok(NULL, " \t")))
868 b_opt = nv;
870 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
871 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
872 argv[argc++] = "-O";
873 argv[argc++] = nv;
875 else {
876 rot_siz = "50";
877 b_opt = "";
882 argv[argc++] = "-s";
883 argv[argc++] = rot_siz;
885 if (isdigit(*b_opt)) {
886 argv[argc++] = "-b";
887 argv[argc++] = b_opt;
891 if (argc > 1) {
892 argv[argc] = NULL;
893 _eval(argv, NULL, 0, NULL);
895 argv[0] = "klogd";
896 argv[1] = NULL;
897 _eval(argv, NULL, 0, NULL);
899 // used to be available in syslogd -m
900 n = nvram_get_int("log_mark");
901 if (n > 0) {
902 // n is in minutes
903 if (n < 60)
904 sprintf(rem, "*/%d * * * *", n);
905 else if (n < 60 * 24)
906 sprintf(rem, "0 */%d * * *", n / 60);
907 else
908 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
909 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
910 eval("cru", "a", "syslogdmark", s);
912 else {
913 eval("cru", "d", "syslogdmark");
918 void stop_syslog(void)
920 killall("klogd", SIGTERM);
921 killall("syslogd", SIGTERM);
924 // -----------------------------------------------------------------------------
926 static pid_t pid_igmp = -1;
928 void start_igmp_proxy(void)
930 FILE *fp;
932 pid_igmp = -1;
933 if (nvram_match("multicast_pass", "1")) {
934 if (get_wan_proto() == WP_DISABLED)
935 return;
937 if (f_exists("/etc/igmp.alt")) {
938 eval("igmpproxy", "/etc/igmp.alt");
940 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
941 fprintf(fp,
942 "quickleave\n"
943 "phyint %s upstream\n"
944 "\taltnet %s\n"
945 "phyint %s downstream ratelimit 0\n",
946 nvram_safe_get("wan_ifname"),
947 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
948 nvram_safe_get("lan_ifname"));
949 fclose(fp);
950 eval("igmpproxy", "/etc/igmp.conf");
952 else {
953 return;
955 if (!nvram_contains_word("debug_norestart", "igmprt")) {
956 pid_igmp = -2;
961 void stop_igmp_proxy(void)
963 pid_igmp = -1;
964 killall_tk("igmpproxy");
968 // -----------------------------------------------------------------------------
970 void set_tz(void)
972 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
975 void start_ntpc(void)
977 set_tz();
979 stop_ntpc();
981 if (nvram_get_int("ntp_updates") >= 0) {
982 xstart("ntpsync", "--init");
986 void stop_ntpc(void)
988 killall("ntpsync", SIGTERM);
991 // -----------------------------------------------------------------------------
993 static void stop_rstats(void)
995 int n;
996 int pid;
998 n = 60;
999 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1000 if (kill(pid, SIGTERM) != 0) break;
1001 sleep(1);
1005 static void start_rstats(int new)
1007 if (nvram_match("rstats_enable", "1")) {
1008 stop_rstats();
1009 if (new) xstart("rstats", "--new");
1010 else xstart("rstats");
1014 // -----------------------------------------------------------------------------
1016 // !!TB - FTP Server
1018 #ifdef TCONFIG_FTP
1019 static char *get_full_storage_path(char *val)
1021 static char buf[128];
1022 int len;
1024 if (val[0] == '/')
1025 len = sprintf(buf, "%s", val);
1026 else
1027 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1029 if (len > 1 && buf[len - 1] == '/')
1030 buf[len - 1] = 0;
1032 return buf;
1035 static char *nvram_storage_path(char *var)
1037 char *val = nvram_safe_get(var);
1038 return get_full_storage_path(val);
1041 char vsftpd_conf[] = "/etc/vsftpd.conf";
1042 char vsftpd_users[] = "/etc/vsftpd.users";
1043 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1045 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1047 static void start_ftpd(void)
1049 char tmp[256];
1050 FILE *fp, *f;
1051 char *buf;
1052 char *p, *q;
1053 char *user, *pass, *rights;
1055 if (getpid() != 1) {
1056 start_service("ftpd");
1057 return;
1060 if (!nvram_get_int("ftp_enable")) return;
1062 mkdir_if_none(vsftpd_users);
1063 mkdir_if_none("/var/run/vsftpd");
1065 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1066 return;
1068 if (nvram_get_int("ftp_super"))
1070 /* rights */
1071 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1072 if ((f = fopen(tmp, "w")))
1074 fprintf(f,
1075 "dirlist_enable=yes\n"
1076 "write_enable=yes\n"
1077 "download_enable=yes\n");
1078 fclose(f);
1082 #ifdef TCONFIG_SAMBASRV
1083 if (nvram_match("smbd_cset", "utf8"))
1084 fprintf(fp, "utf8=yes\n");
1085 #endif
1087 if (nvram_invmatch("ftp_anonymous", "0"))
1089 fprintf(fp,
1090 "anon_allow_writable_root=yes\n"
1091 "anon_world_readable_only=no\n"
1092 "anon_umask=022\n");
1094 /* rights */
1095 sprintf(tmp, "%s/ftp", vsftpd_users);
1096 if ((f = fopen(tmp, "w")))
1098 if (nvram_match("ftp_dirlist", "0"))
1099 fprintf(f, "dirlist_enable=yes\n");
1100 if (nvram_match("ftp_anonymous", "1") ||
1101 nvram_match("ftp_anonymous", "3"))
1102 fprintf(f, "write_enable=yes\n");
1103 if (nvram_match("ftp_anonymous", "1") ||
1104 nvram_match("ftp_anonymous", "2"))
1105 fprintf(f, "download_enable=yes\n");
1106 fclose(f);
1108 if (nvram_match("ftp_anonymous", "1") ||
1109 nvram_match("ftp_anonymous", "3"))
1110 fprintf(fp,
1111 "anon_upload_enable=yes\n"
1112 "anon_mkdir_write_enable=yes\n"
1113 "anon_other_write_enable=yes\n");
1114 } else {
1115 fprintf(fp, "anonymous_enable=no\n");
1118 fprintf(fp,
1119 "dirmessage_enable=yes\n"
1120 "download_enable=no\n"
1121 "dirlist_enable=no\n"
1122 "hide_ids=yes\n"
1123 "syslog_enable=yes\n"
1124 "local_enable=yes\n"
1125 "local_umask=022\n"
1126 "chmod_enable=no\n"
1127 "chroot_local_user=yes\n"
1128 "check_shell=no\n"
1129 "log_ftp_protocol=%s\n"
1130 "user_config_dir=%s\n"
1131 "passwd_file=%s\n"
1132 "listen%s=yes\n"
1133 "listen_port=%s\n"
1134 "background=yes\n"
1135 "isolate=no\n"
1136 "max_clients=%d\n"
1137 "max_per_ip=%d\n"
1138 "max_login_fails=1\n"
1139 "idle_session_timeout=%s\n"
1140 "use_sendfile=no\n"
1141 "anon_max_rate=%d\n"
1142 "local_max_rate=%d\n"
1143 "%s\n",
1144 nvram_get_int("log_ftp") ? "yes" : "no",
1145 vsftpd_users, vsftpd_passwd,
1146 #ifdef TCONFIG_IPV6
1147 ipv6_enabled() ? "_ipv6" : "",
1148 #else
1150 #endif
1151 nvram_get("ftp_port") ? : "21",
1152 nvram_get_int("ftp_max"),
1153 nvram_get_int("ftp_ipmax"),
1154 nvram_get("ftp_staytimeout") ? : "300",
1155 nvram_get_int("ftp_anonrate") * 1024,
1156 nvram_get_int("ftp_rate") * 1024,
1157 nvram_safe_get("ftp_custom"));
1159 fclose(fp);
1161 /* prepare passwd file and default users */
1162 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1163 return;
1165 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1166 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1168 fprintf(fp, /* anonymous, admin, nobody */
1169 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1170 "%s:%s:0:0:root:/:/sbin/nologin\n"
1171 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1172 nvram_storage_path("ftp_anonroot"), user,
1173 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1174 MOUNT_ROOT);
1176 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1179 username<password<rights
1180 rights:
1181 Read/Write
1182 Read Only
1183 View Only
1184 Private
1186 p = buf;
1187 while ((q = strsep(&p, ">")) != NULL) {
1188 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
1189 if (!user || !pass) continue;
1191 /* directory */
1192 if (strncmp(rights, "Private", 7) == 0)
1194 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1195 mkdir_if_none(tmp);
1197 else
1198 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
1200 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1201 user, crypt(pass, "$1$"), user, tmp);
1203 /* rights */
1204 sprintf(tmp, "%s/%s", vsftpd_users, user);
1205 if ((f = fopen(tmp, "w")))
1207 tmp[0] = 0;
1208 if (nvram_invmatch("ftp_dirlist", "1"))
1209 strcat(tmp, "dirlist_enable=yes\n");
1210 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1211 strcat(tmp, "download_enable=yes\n");
1212 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1213 strcat(tmp, "write_enable=yes\n");
1215 fputs(tmp, f);
1216 fclose(f);
1219 free(buf);
1222 fclose(fp);
1223 killall("vsftpd", SIGHUP);
1225 /* start vsftpd if it's not already running */
1226 if (pidof("vsftpd") <= 0)
1227 xstart("vsftpd");
1230 static void stop_ftpd(void)
1232 if (getpid() != 1) {
1233 stop_service("ftpd");
1234 return;
1237 killall_tk("vsftpd");
1238 unlink(vsftpd_passwd);
1239 unlink(vsftpd_conf);
1240 eval("rm", "-rf", vsftpd_users);
1242 #endif // TCONFIG_FTP
1244 // -----------------------------------------------------------------------------
1246 // !!TB - Samba
1248 #ifdef TCONFIG_SAMBASRV
1249 static void kill_samba(int sig)
1251 if (sig == SIGTERM) {
1252 killall_tk("smbd");
1253 killall_tk("nmbd");
1255 else {
1256 killall("smbd", sig);
1257 killall("nmbd", sig);
1261 static void start_samba(void)
1263 FILE *fp;
1264 DIR *dir = NULL;
1265 struct dirent *dp;
1266 char nlsmod[15];
1267 int mode;
1268 char *nv;
1270 if (getpid() != 1) {
1271 start_service("smbd");
1272 return;
1275 mode = nvram_get_int("smbd_enable");
1276 if (!mode || !nvram_invmatch("lan_hostname", ""))
1277 return;
1279 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1280 return;
1282 fprintf(fp, "[global]\n"
1283 " interfaces = %s\n"
1284 " bind interfaces only = yes\n"
1285 " workgroup = %s\n"
1286 " netbios name = %s\n"
1287 " server string = %s\n"
1288 " guest account = nobody\n"
1289 " security = user\n"
1290 " %s\n"
1291 " guest ok = %s\n"
1292 " guest only = no\n"
1293 " browseable = yes\n"
1294 " syslog only = yes\n"
1295 " timestamp logs = no\n"
1296 " syslog = 1\n"
1297 " encrypt passwords = yes\n"
1298 " preserve case = yes\n"
1299 " short preserve case = yes\n",
1300 nvram_safe_get("lan_ifname"),
1301 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1302 nvram_safe_get("lan_hostname"),
1303 nvram_get("router_name") ? : "Tomato",
1304 mode == 2 ? "" : "map to guest = Bad User",
1305 mode == 2 ? "no" : "yes" // guest ok
1308 if (nvram_get_int("smbd_wins")) {
1309 nv = nvram_safe_get("wan_wins");
1310 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1311 fprintf(fp, " wins support = yes\n");
1315 if (nvram_get_int("smbd_master")) {
1316 fprintf(fp,
1317 " domain master = yes\n"
1318 " local master = yes\n"
1319 " preferred master = yes\n"
1320 " os level = 65\n");
1323 nv = nvram_safe_get("smbd_cpage");
1324 if (*nv) {
1325 #ifndef TCONFIG_SAMBA3
1326 fprintf(fp, " client code page = %s\n", nv);
1327 #endif
1328 sprintf(nlsmod, "nls_cp%s", nv);
1330 nv = nvram_safe_get("smbd_nlsmod");
1331 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1332 modprobe_r(nv);
1334 modprobe(nlsmod);
1335 nvram_set("smbd_nlsmod", nlsmod);
1338 #ifndef TCONFIG_SAMBA3
1339 if (nvram_match("smbd_cset", "utf8"))
1340 fprintf(fp, " coding system = utf8\n");
1341 else if (nvram_invmatch("smbd_cset", ""))
1342 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1343 #endif
1345 nv = nvram_safe_get("smbd_custom");
1346 /* add socket options unless overriden by the user */
1347 if (strstr(nv, "socket options") == NULL) {
1348 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1350 fprintf(fp, "%s\n\n", nv);
1352 /* configure shares */
1354 char *buf;
1355 char *p, *q;
1356 char *name, *path, *comment, *writeable, *hidden;
1357 int cnt = 0;
1359 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1361 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1363 p = buf;
1364 while ((q = strsep(&p, ">")) != NULL) {
1365 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1366 if (!path || !name) continue;
1368 /* share name */
1369 fprintf(fp, "\n[%s]\n", name);
1371 /* path */
1372 fprintf(fp, " path = %s\n", path);
1374 /* access level */
1375 if (!strcmp(writeable, "1"))
1376 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1377 if (!strcmp(hidden, "1"))
1378 fprintf(fp, " browseable = no\n");
1380 /* comment */
1381 if (comment)
1382 fprintf(fp, " comment = %s\n", comment);
1384 cnt++;
1386 free(buf);
1389 /* Share every mountpoint below MOUNT_ROOT */
1390 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1391 while ((dp = readdir(dir))) {
1392 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1394 char path[256];
1395 struct stat sb;
1396 int thisdev;
1398 /* Only if is a directory and is mounted */
1399 sprintf(path, "%s/%s", MOUNT_ROOT, dp->d_name);
1400 sb.st_mode = S_IFDIR; /* failsafe */
1401 stat(path, &sb);
1402 if (!S_ISDIR(sb.st_mode))
1403 continue;
1405 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1406 strcat(path, "/.");
1407 stat(path, &sb);
1408 thisdev = sb.st_dev;
1409 strcat(path, ".");
1410 ++sb.st_dev; /* failsafe */
1411 stat(path, &sb);
1412 if (thisdev == sb.st_dev)
1413 continue;
1415 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1416 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1417 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1418 if (nvram_match("smbd_autoshare", "3")) // Hidden
1419 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1420 dp->d_name, MOUNT_ROOT, dp->d_name);
1421 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1422 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1424 cnt++;
1428 if (dir) closedir(dir);
1430 if (cnt == 0) {
1431 /* by default share MOUNT_ROOT as read-only */
1432 fprintf(fp, "\n[share]\n"
1433 " path = %s\n"
1434 " writable = no\n",
1435 MOUNT_ROOT);
1438 fclose(fp);
1440 mkdir_if_none("/var/run/samba");
1441 mkdir_if_none("/etc/samba");
1443 /* write smbpasswd */
1444 #ifdef TCONFIG_SAMBA3
1445 eval("smbpasswd", "nobody", "\"\"");
1446 #else
1447 eval("smbpasswd", "-a", "nobody", "\"\"");
1448 #endif
1449 if (mode == 2) {
1450 char *smbd_user;
1451 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1452 smbd_user = "nas";
1453 #ifdef TCONFIG_SAMBA3
1454 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1455 #else
1456 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1457 #endif
1460 kill_samba(SIGHUP);
1461 int ret1 = 0, ret2 = 0;
1462 /* start samba if it's not already running */
1463 if (pidof("nmbd") <= 0)
1464 ret1 = xstart("nmbd", "-D");
1465 if (pidof("smbd") <= 0)
1466 ret2 = xstart("smbd", "-D");
1468 if (ret1 || ret2) kill_samba(SIGTERM);
1471 static void stop_samba(void)
1473 if (getpid() != 1) {
1474 stop_service("smbd");
1475 return;
1478 kill_samba(SIGTERM);
1479 /* clean up */
1480 unlink("/var/log/smb");
1481 unlink("/var/log/nmb");
1482 eval("rm", "-rf", "/var/run/samba");
1484 #endif // TCONFIG_SAMBASRV
1486 #ifdef TCONFIG_MEDIA_SERVER
1487 #define MEDIA_SERVER_APP "minidlna"
1489 static void start_media_server(void)
1491 FILE *f;
1492 int port, pid, https;
1493 char *dbdir;
1494 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1495 static int once = 1;
1497 if (getpid() != 1) {
1498 start_service("media");
1499 return;
1502 if (nvram_get_int("ms_sas") == 0)
1503 once = 0;
1505 if (nvram_get_int("ms_enable") != 0) {
1506 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1507 // no forced rescan
1508 argv[3] = NULL;
1510 nvram_unset("ms_rescan");
1512 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1513 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1515 else {
1516 if ((f = fopen(argv[2], "w")) != NULL) {
1517 port = nvram_get_int("ms_port");
1518 https = nvram_get_int("https_enable");
1519 dbdir = nvram_safe_get("ms_dbdir");
1520 if (!(*dbdir)) dbdir = NULL;
1521 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1523 fprintf(f,
1524 "network_interface=%s\n"
1525 "port=%d\n"
1526 "friendly_name=%s\n"
1527 "db_dir=%s/.db\n"
1528 "enable_tivo=%s\n"
1529 "strict_dlna=%s\n"
1530 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1531 "inotify=yes\n"
1532 "notify_interval=600\n"
1533 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\n"
1534 "\n",
1535 nvram_safe_get("lan_ifname"),
1536 (port < 0) || (port >= 0xffff) ? 0 : port,
1537 nvram_get("router_name") ? : "Tomato",
1538 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1539 nvram_get_int("ms_tivo") ? "yes" : "no",
1540 nvram_get_int("ms_stdlna") ? "yes" : "no",
1541 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1544 // media directories
1545 char *buf, *p, *q;
1546 char *path, *restrict;
1548 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1549 /* path<restrict[A|V|P|] */
1551 p = buf;
1552 while ((q = strsep(&p, ">")) != NULL) {
1553 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1554 continue;
1555 fprintf(f, "media_dir=%s%s%s\n",
1556 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1558 free(buf);
1561 fclose(f);
1565 /* start media server if it's not already running */
1566 if (pidof(MEDIA_SERVER_APP) <= 0) {
1567 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1568 /* If we started the media server successfully, wait 1 sec
1569 * to let it die if it can't open the database file.
1570 * If it's still alive after that, assume it's running and
1571 * disable forced once-after-reboot rescan.
1573 sleep(1);
1574 if (pidof(MEDIA_SERVER_APP) > 0)
1575 once = 0;
1581 static void stop_media_server(void)
1583 if (getpid() != 1) {
1584 stop_service("media");
1585 return;
1588 killall_tk(MEDIA_SERVER_APP);
1590 #endif // TCONFIG_MEDIA_SERVER
1592 #ifdef TCONFIG_USB
1593 static void start_nas_services(void)
1595 if (getpid() != 1) {
1596 start_service("usbapps");
1597 return;
1600 #ifdef TCONFIG_SAMBASRV
1601 start_samba();
1602 #endif
1603 #ifdef TCONFIG_FTP
1604 start_ftpd();
1605 #endif
1606 #ifdef TCONFIG_MEDIA_SERVER
1607 start_media_server();
1608 #endif
1611 static void stop_nas_services(void)
1613 if (getpid() != 1) {
1614 stop_service("usbapps");
1615 return;
1618 #ifdef TCONFIG_MEDIA_SERVER
1619 stop_media_server();
1620 #endif
1621 #ifdef TCONFIG_FTP
1622 stop_ftpd();
1623 #endif
1624 #ifdef TCONFIG_SAMBASRV
1625 stop_samba();
1626 #endif
1629 void restart_nas_services(int stop, int start)
1631 int fd = file_lock("usb");
1632 /* restart all NAS applications */
1633 if (stop)
1634 stop_nas_services();
1635 if (start)
1636 start_nas_services();
1637 file_unlock(fd);
1639 #endif // TCONFIG_USB
1641 // -----------------------------------------------------------------------------
1643 /* -1 = Don't check for this program, it is not expected to be running.
1644 * Other = This program has been started and should be kept running. If no
1645 * process with the name is running, call func to restart it.
1646 * Note: At startup, dnsmasq forks a short-lived child which forks a
1647 * long-lived (grand)child. The parents terminate.
1648 * Many daemons use this technique.
1650 static void _check(pid_t pid, const char *name, void (*func)(void))
1652 if (pid == -1) return;
1654 if (pidof(name) > 0) return;
1656 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
1657 func();
1659 // Force recheck in 500 msec
1660 setitimer(ITIMER_REAL, &pop_tv, NULL);
1663 void check_services(void)
1665 TRACE_PT("keep alive\n");
1667 // Periodically reap any zombies
1668 setitimer(ITIMER_REAL, &zombie_tv, NULL);
1670 #ifdef LINUX26
1671 _check(pid_hotplug2, "hotplug2", start_hotplug2);
1672 #endif
1673 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
1674 _check(pid_crond, "crond", start_cron);
1675 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
1676 #ifdef TCONFIG_IPV6
1677 _check(pid_radvd, "radvd", start_radvd);
1678 #endif
1681 // -----------------------------------------------------------------------------
1683 void start_services(void)
1685 static int once = 1;
1687 if (once) {
1688 once = 0;
1690 if (nvram_get_int("telnetd_eas")) start_telnetd();
1691 if (nvram_get_int("sshd_eas")) start_sshd();
1694 // start_syslog();
1695 start_nas();
1696 start_zebra();
1697 start_dnsmasq();
1698 start_cifs();
1699 start_httpd();
1700 start_cron();
1701 // start_upnp();
1702 start_rstats(0);
1703 start_sched();
1704 #ifdef TCONFIG_IPV6
1705 /* note: starting radvd here might be too early in case of
1706 * DHCPv6 because we won't have received a prefix and so it
1707 * will disable advertisements, but the SIGHUP sent from
1708 * dhcp6c-state will restart them.
1710 start_radvd();
1711 #endif
1712 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1713 #ifdef TCONFIG_BT
1714 start_bittorrent();
1715 #endif
1718 void stop_services(void)
1720 clear_resolv();
1722 #ifdef TCONFIG_BT
1723 stop_bittorrent();
1724 #endif
1725 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1726 #ifdef TCONFIG_IPV6
1727 stop_radvd();
1728 #endif
1729 stop_sched();
1730 stop_rstats();
1731 // stop_upnp();
1732 stop_cron();
1733 stop_httpd();
1734 stop_cifs();
1735 stop_dnsmasq();
1736 stop_zebra();
1737 stop_nas();
1738 // stop_syslog();
1741 // -----------------------------------------------------------------------------
1743 /* nvram "action_service" is: "service-action[-modifier]"
1744 * action is something like "stop" or "start" or "restart"
1745 * optional modifier is "c" for the "service" command-line command
1747 void exec_service(void)
1749 const int A_START = 1;
1750 const int A_STOP = 2;
1751 const int A_RESTART = 1|2;
1752 char buffer[128];
1753 char *service;
1754 char *act;
1755 char *next;
1756 char *modifier;
1757 int action, user;
1758 int i;
1760 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1761 next = buffer;
1763 TOP:
1764 act = strsep(&next, ",");
1765 service = strsep(&act, "-");
1766 if (act == NULL) {
1767 next = NULL;
1768 goto CLEAR;
1770 modifier = act;
1771 strsep(&modifier, "-");
1773 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
1775 if (strcmp(act, "start") == 0) action = A_START;
1776 else if (strcmp(act, "stop") == 0) action = A_STOP;
1777 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1778 else action = 0;
1779 user = (modifier != NULL && *modifier == 'c');
1781 if (strcmp(service, "dhcpc") == 0) {
1782 if (action & A_STOP) stop_dhcpc();
1783 if (action & A_START) start_dhcpc();
1784 goto CLEAR;
1787 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1788 if (action & A_STOP) stop_dnsmasq();
1789 if (action & A_START) {
1790 dns_to_resolv();
1791 start_dnsmasq();
1793 goto CLEAR;
1796 if (strcmp(service, "firewall") == 0) {
1797 if (action & A_STOP) {
1798 stop_firewall();
1799 stop_igmp_proxy();
1801 if (action & A_START) {
1802 start_firewall();
1803 start_igmp_proxy();
1805 goto CLEAR;
1808 if (strcmp(service, "restrict") == 0) {
1809 if (action & A_STOP) {
1810 stop_firewall();
1812 if (action & A_START) {
1813 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1815 start_firewall();
1817 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1818 if (i == 1) {
1819 if (nvram_get_int("rrules_radio") < 0) {
1820 eval("radio", "on");
1824 goto CLEAR;
1827 if (strcmp(service, "qos") == 0) {
1828 if (action & A_STOP) {
1829 stop_qos();
1831 stop_firewall(); start_firewall(); // always restarted
1832 if (action & A_START) {
1833 start_qos();
1834 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1836 goto CLEAR;
1840 if (strcmp(service, "qoslimit") == 0) {
1841 if (action & A_STOP) {
1842 new_qoslimit_stop();
1844 stop_firewall(); start_firewall(); // always restarted
1845 if (action & A_START) {
1846 new_qoslimit_start();
1848 goto CLEAR;
1851 if (strcmp(service, "arpbind") == 0) {
1852 if (action & A_STOP) new_arpbind_stop();
1853 if (action & A_START) new_arpbind_start();
1854 goto CLEAR;
1858 if (strcmp(service, "upnp") == 0) {
1859 if (action & A_STOP) {
1860 stop_upnp();
1862 stop_firewall(); start_firewall(); // always restarted
1863 if (action & A_START) {
1864 start_upnp();
1866 goto CLEAR;
1869 if (strcmp(service, "telnetd") == 0) {
1870 if (action & A_STOP) stop_telnetd();
1871 if (action & A_START) start_telnetd();
1872 goto CLEAR;
1875 if (strcmp(service, "sshd") == 0) {
1876 if (action & A_STOP) stop_sshd();
1877 if (action & A_START) start_sshd();
1878 goto CLEAR;
1881 if (strcmp(service, "httpd") == 0) {
1882 if (action & A_STOP) stop_httpd();
1883 if (action & A_START) start_httpd();
1884 goto CLEAR;
1887 #ifdef TCONFIG_IPV6
1888 if (strcmp(service, "ipv6") == 0) {
1889 if (action & A_STOP) {
1890 stop_radvd();
1891 stop_ipv6();
1893 if (action & A_START) {
1894 start_ipv6();
1895 start_radvd();
1897 goto CLEAR;
1900 if (strcmp(service, "radvd") == 0) {
1901 if (action & A_STOP) {
1902 stop_radvd();
1904 if (action & A_START) {
1905 start_radvd();
1907 goto CLEAR;
1910 if (strncmp(service, "dhcp6", 5) == 0) {
1911 if (action & A_STOP) {
1912 stop_dhcp6c();
1914 if (action & A_START) {
1915 start_dhcp6c();
1917 goto CLEAR;
1919 #endif
1921 if (strcmp(service, "admin") == 0) {
1922 if (action & A_STOP) {
1923 stop_sshd();
1924 stop_telnetd();
1925 stop_httpd();
1927 stop_firewall(); start_firewall(); // always restarted
1928 if (action & A_START) {
1929 start_httpd();
1930 create_passwd();
1931 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1932 if (nvram_match("sshd_eas", "1")) start_sshd();
1934 goto CLEAR;
1937 if (strcmp(service, "ddns") == 0) {
1938 if (action & A_STOP) stop_ddns();
1939 if (action & A_START) start_ddns();
1940 goto CLEAR;
1943 if (strcmp(service, "ntpc") == 0) {
1944 if (action & A_STOP) stop_ntpc();
1945 if (action & A_START) start_ntpc();
1946 goto CLEAR;
1949 if (strcmp(service, "logging") == 0) {
1950 if (action & A_STOP) {
1951 stop_syslog();
1953 if (action & A_START) {
1954 start_syslog();
1956 if (!user) {
1957 // always restarted except from "service" command
1958 stop_cron(); start_cron();
1959 stop_firewall(); start_firewall();
1961 goto CLEAR;
1964 if (strcmp(service, "crond") == 0) {
1965 if (action & A_STOP) {
1966 stop_cron();
1968 if (action & A_START) {
1969 start_cron();
1971 goto CLEAR;
1974 #ifdef LINUX26
1975 if (strncmp(service, "hotplug", 7) == 0) {
1976 if (action & A_STOP) {
1977 stop_hotplug2();
1979 if (action & A_START) {
1980 start_hotplug2(1);
1982 goto CLEAR;
1984 #endif
1986 if (strcmp(service, "upgrade") == 0) {
1987 if (action & A_START) {
1988 #if TOMATO_SL
1989 stop_usbevent();
1990 stop_smbd();
1991 #endif
1992 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1993 stop_jffs2();
1994 // stop_cifs();
1995 stop_zebra();
1996 stop_cron();
1997 stop_ntpc();
1998 stop_upnp();
1999 // stop_dhcpc();
2000 killall("rstats", SIGTERM);
2001 killall("buttons", SIGTERM);
2002 stop_syslog();
2003 remove_storage_main(1); // !!TB - USB Support
2004 stop_usb(); // !!TB - USB Support
2006 goto CLEAR;
2009 #ifdef TCONFIG_CIFS
2010 if (strcmp(service, "cifs") == 0) {
2011 if (action & A_STOP) stop_cifs();
2012 if (action & A_START) start_cifs();
2013 goto CLEAR;
2015 #endif
2017 #ifdef TCONFIG_JFFS2
2018 if (strncmp(service, "jffs", 4) == 0) {
2019 if (action & A_STOP) stop_jffs2();
2020 if (action & A_START) start_jffs2();
2021 goto CLEAR;
2023 #endif
2025 if (strcmp(service, "zebra") == 0) {
2026 if (action & A_STOP) stop_zebra();
2027 if (action & A_START) start_zebra();
2028 goto CLEAR;
2031 if (strcmp(service, "routing") == 0) {
2032 if (action & A_STOP) {
2033 stop_zebra();
2034 do_static_routes(0); // remove old '_saved'
2035 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2037 stop_firewall();
2038 start_firewall();
2039 if (action & A_START) {
2040 do_static_routes(1); // add new
2041 start_zebra();
2042 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2044 goto CLEAR;
2047 if (strcmp(service, "ctnf") == 0) {
2048 if (action & A_START) {
2049 setup_conntrack();
2050 stop_firewall();
2051 start_firewall();
2053 goto CLEAR;
2056 if (strcmp(service, "wan") == 0) {
2057 if (action & A_STOP) {
2058 stop_wan();
2061 if (action & A_START) {
2062 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2063 start_wan(BOOT);
2064 sleep(2);
2065 force_to_dial();
2067 goto CLEAR;
2070 if (strcmp(service, "net") == 0) {
2071 if (action & A_STOP) {
2072 #ifdef TCONFIG_IPV6
2073 stop_radvd();
2074 #endif
2075 stop_httpd();
2076 stop_dnsmasq();
2077 stop_nas();
2078 stop_wan();
2079 stop_lan();
2080 stop_vlan();
2082 if (action & A_START) {
2083 start_vlan();
2084 start_lan();
2085 start_wan(BOOT);
2086 start_nas();
2087 start_dnsmasq();
2088 start_httpd();
2089 #ifdef TCONFIG_IPV6
2090 start_radvd();
2091 #endif
2092 start_wl();
2094 goto CLEAR;
2097 if (strcmp(service, "nas") == 0) {
2098 if (action & A_STOP) {
2099 stop_nas();
2101 if (action & A_START) {
2102 start_nas();
2103 start_wl();
2105 goto CLEAR;
2108 if (strcmp(service, "rstats") == 0) {
2109 if (action & A_STOP) stop_rstats();
2110 if (action & A_START) start_rstats(0);
2111 goto CLEAR;
2114 if (strcmp(service, "rstatsnew") == 0) {
2115 if (action & A_STOP) stop_rstats();
2116 if (action & A_START) start_rstats(1);
2117 goto CLEAR;
2120 if (strcmp(service, "sched") == 0) {
2121 if (action & A_STOP) stop_sched();
2122 if (action & A_START) start_sched();
2123 goto CLEAR;
2126 #ifdef TCONFIG_BT
2127 if (strcmp(service, "bittorrent") == 0) {
2128 if (action & A_STOP) {
2129 stop_bittorrent();
2131 stop_firewall(); start_firewall(); // always restarted
2132 if (action & A_START) {
2133 start_bittorrent();
2135 goto CLEAR;
2137 #endif
2139 #ifdef TCONFIG_USB
2140 // !!TB - USB Support
2141 if (strcmp(service, "usb") == 0) {
2142 if (action & A_STOP) stop_usb();
2143 if (action & A_START) {
2144 start_usb();
2145 // restart Samba and ftp since they may be killed by stop_usb()
2146 restart_nas_services(0, 1);
2147 // remount all partitions by simulating hotplug event
2148 add_remove_usbhost("-1", 1);
2150 goto CLEAR;
2153 if (strcmp(service, "usbapps") == 0) {
2154 if (action & A_STOP) stop_nas_services();
2155 if (action & A_START) start_nas_services();
2156 goto CLEAR;
2158 #endif
2160 #ifdef TCONFIG_FTP
2161 // !!TB - FTP Server
2162 if (strcmp(service, "ftpd") == 0) {
2163 if (action & A_STOP) stop_ftpd();
2164 setup_conntrack();
2165 stop_firewall();
2166 start_firewall();
2167 if (action & A_START) start_ftpd();
2168 goto CLEAR;
2170 #endif
2172 #ifdef TCONFIG_MEDIA_SERVER
2173 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2174 if (action & A_STOP) stop_media_server();
2175 if (action & A_START) start_media_server();
2176 goto CLEAR;
2178 #endif
2180 #ifdef TCONFIG_SAMBASRV
2181 // !!TB - Samba
2182 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2183 if (action & A_STOP) stop_samba();
2184 if (action & A_START) {
2185 create_passwd();
2186 stop_dnsmasq();
2187 start_dnsmasq();
2188 start_samba();
2190 goto CLEAR;
2192 #endif
2194 #ifdef TCONFIG_OPENVPN
2195 if (strncmp(service, "vpnclient", 9) == 0) {
2196 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2197 if (action & A_START) start_vpnclient(atoi(&service[9]));
2198 goto CLEAR;
2201 if (strncmp(service, "vpnserver", 9) == 0) {
2202 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2203 if (action & A_START) start_vpnserver(atoi(&service[9]));
2204 goto CLEAR;
2206 #endif
2208 CLEAR:
2209 if (next) goto TOP;
2211 // some functions check action_service and must be cleared at end -- zzz
2212 nvram_set("action_service", "");
2214 // Force recheck in 500 msec
2215 setitimer(ITIMER_REAL, &pop_tv, NULL);
2218 static void do_service(const char *name, const char *action, int user)
2220 int n;
2221 char s[64];
2223 n = 150;
2224 while (!nvram_match("action_service", "")) {
2225 if (user) {
2226 putchar('*');
2227 fflush(stdout);
2229 else if (--n < 0) break;
2230 usleep(100 * 1000);
2233 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2234 nvram_set("action_service", s);
2235 kill(1, SIGUSR1);
2237 n = 150;
2238 while (nvram_match("action_service", s)) {
2239 if (user) {
2240 putchar('.');
2241 fflush(stdout);
2243 else if (--n < 0) {
2244 break;
2246 usleep(100 * 1000);
2250 int service_main(int argc, char *argv[])
2252 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2253 do_service(argv[1], argv[2], 1);
2254 printf("\nDone.\n");
2255 return 0;
2258 void start_service(const char *name)
2260 do_service(name, "start", 0);
2263 void stop_service(const char *name)
2265 do_service(name, "stop", 0);
2269 void restart_service(const char *name)
2271 do_service(name, "restart", 0);