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
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
38 #include <arpa/inet.h>
44 #include <sys/mount.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");
72 const char *router_ip
;
73 const char *lan_ifname
;
77 char *mac
, *ip
, *name
;
91 start_service("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;
107 "pid-file=/var/run/dnsmasq.pid\n"
110 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
111 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
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
);
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")) {
131 "rebind-localhost-ok\n");
132 // allow RFC1918 responses for server domain
133 switch (get_wan_proto()) {
135 nv
= nvram_get("pptp_server_ip");
138 nv
= nvram_get("l2tp_server_ip");
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
);
154 do_dhcpd
= nvram_match("lan_proto", "dhcp");
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
);
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.
169 strcpy(sdhcp_lease
, "2m");
173 // pass the dns directly
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
);
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
);
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");
203 "dhcp-option=3,%s\n" // gateway
204 "dhcp-lease-max=%d\n",
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");
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
);
237 p
= (char *)get_wanip();
238 if ((*p
== 0) || strcmp(p
, "0.0.0.0") == 0)
240 fprintf(hf
, "%s wan-ip\n", p
);
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
) {
260 if ((e
= strchr(buf
, '<')) == NULL
) continue;
265 if ((e
= strchr(ip
, '<')) == NULL
) continue;
267 if (strchr(ip
, '.') == NULL
) {
269 if ((ipn
<= 0) || (ipn
> 255)) continue;
270 sprintf(ipbuf
, "%s%d", lan
, ipn
);
274 if (inet_addr(ip
) == INADDR_NONE
) continue;
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
);
292 #ifdef TCONFIG_OPENVPN
293 write_vpn_dnsmasq_config(f
);
296 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
298 fappend(f
, "/etc/dnsmasq.custom");
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")) {
321 void stop_dnsmasq(void)
326 stop_service("dnsmasq");
332 unlink("/etc/resolv.conf");
333 symlink(dmresolv
, "/etc/resolv.conf");
335 killall_tk("dnsmasq");
340 void clear_resolv(void)
342 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
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
;
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
);
364 void dns_to_resolv(void)
367 const dns_list_t
*dns
;
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
)) {
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"));
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()) {
387 fprintf(f
, "nameserver 1.1.1.1\n");
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
));
405 // -----------------------------------------------------------------------------
407 void start_httpd(void)
410 start_service("httpd");
420 void stop_httpd(void)
423 stop_service("httpd");
430 // -----------------------------------------------------------------------------
433 void start_ipv6_sit_tunnel(void)
435 char *tun_dev
= nvram_safe_get("ipv6_ifname");
436 char ip
[INET6_ADDRSTRLEN
+ 4];
439 if (get_ipv6_service() == IPV6_6IN4
) {
441 snprintf(ip
, sizeof(ip
), "%s/%d",
442 nvram_safe_get("ipv6_tun_addr"),
443 nvram_get_int("ipv6_tun_addrlen") ? : 64);
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");
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
);
466 static pid_t pid_radvd
= -1;
468 void start_radvd(void)
473 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
477 start_service("radvd");
483 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
485 switch (get_ipv6_service()) {
486 case IPV6_NATIVE_DHCP
:
490 prefix
= nvram_safe_get("ipv6_prefix");
493 if (!(*prefix
)) prefix
= "::";
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");
504 " AdvSendAdvert on;\n"
505 " MaxRtrAdvInterval 60;\n"
506 " AdvHomeAgentFlag off;\n"
507 " AdvManagedFlag off;\n"
511 " AdvAutonomous on;\n"
515 nvram_safe_get("lan_ifname"), prefix
,
516 do_dns
? "RDNSS " : "", do_dns
? ip
: "", do_dns
? " { };" : "");
521 if (nvram_get_int("debug_ipv6")) {
526 _eval(argv
, NULL
, 0, &pid
);
528 if (!nvram_contains_word("debug_norestart", "radvd")) {
534 void stop_radvd(void)
537 stop_service("radvd");
545 void start_ipv6(void)
548 char ip
[INET6_ADDRSTRLEN
+ 4];
551 service
= get_ipv6_service();
554 // Check if turned on
559 p
= (char *)ipv6_router_address(NULL
);
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"));
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"));
575 stop_ipv6_sit_tunnel();
577 eval("ip", "-6", "addr", "flush", "scope", "global");
582 // -----------------------------------------------------------------------------
584 void start_upnp(void)
587 start_service("upnp");
591 if (get_wan_proto() == WP_DISABLED
) return;
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");
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");
612 "listening_ip=%s/%s\n"
617 "upnp_forward_chain=upnp\n"
618 "upnp_nat_chain=upnp\n"
619 "notify_interval=%d\n"
620 "system_uptime=yes\n"
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;
636 "clean_ruleset_interval=%d\n"
637 "clean_ruleset_threshold=%d\n",
639 nvram_get_int("upnp_clean_threshold")
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"));
652 // Empty parameters are not included into XML service description
653 fprintf(f
, "presentation_url=\n");
657 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
658 fprintf(f
, "uuid=%s\n", uuid
);
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) {
666 "allow %d-%d %s/%s %d-%d\n",
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");
681 xstart("miniupnpd", "-f", "/etc/upnp/config");
690 stop_service("upnp");
694 killall_tk("miniupnpd");
697 // -----------------------------------------------------------------------------
699 static pid_t pid_crond
= -1;
701 void start_cron(void)
705 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
706 if (!nvram_contains_word("debug_norestart", "crond")) {
717 // -----------------------------------------------------------------------------
720 static pid_t pid_hotplug2
= -1;
722 void start_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
732 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
737 void stop_hotplug2(void)
740 killall_tk("hotplug2");
744 // -----------------------------------------------------------------------------
746 // Written by Sparq in 2002/07/16
747 void start_zebra(void)
751 start_service("zebra");
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')) {
767 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
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");
805 xstart("zebra", "-d");
806 xstart("ripd", "-d");
810 void stop_zebra(void)
814 stop_service("zebra");
818 killall("zebra", SIGTERM
);
819 killall("ripd", SIGTERM
);
821 unlink("/etc/zebra.conf");
822 unlink("/etc/ripd.conf");
826 // -----------------------------------------------------------------------------
828 void start_syslog(void)
838 char *rot_siz
= "50";
843 if (nvram_match("log_remote", "1")) {
844 nv
= nvram_safe_get("log_remoteip");
846 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
852 if (nvram_match("log_file", "1")) {
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')))
862 if ((nv
= strtok(cfg
, " \t"))) {
867 if ((nv
= strtok(NULL
, " \t")))
870 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
871 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
883 argv
[argc
++] = rot_siz
;
885 if (isdigit(*b_opt
)) {
887 argv
[argc
++] = b_opt
;
893 _eval(argv
, NULL
, 0, NULL
);
897 _eval(argv
, NULL
, 0, NULL
);
899 // used to be available in syslogd -m
900 n
= nvram_get_int("log_mark");
904 sprintf(rem
, "*/%d * * * *", n
);
905 else if (n
< 60 * 24)
906 sprintf(rem
, "0 */%d * * *", n
/ 60);
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
);
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)
933 if (nvram_match("multicast_pass", "1")) {
934 if (get_wan_proto() == WP_DISABLED
)
937 if (f_exists("/etc/igmp.alt")) {
938 eval("igmpproxy", "/etc/igmp.alt");
940 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
943 "phyint %s upstream\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"));
950 eval("igmpproxy", "/etc/igmp.conf");
955 if (!nvram_contains_word("debug_norestart", "igmprt")) {
961 void stop_igmp_proxy(void)
964 killall_tk("igmpproxy");
968 // -----------------------------------------------------------------------------
972 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
975 void start_ntpc(void)
981 if (nvram_get_int("ntp_updates") >= 0) {
982 xstart("ntpsync", "--init");
988 killall("ntpsync", SIGTERM
);
991 // -----------------------------------------------------------------------------
993 static void stop_rstats(void)
999 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1000 if (kill(pid
, SIGTERM
) != 0) break;
1005 static void start_rstats(int new)
1007 if (nvram_match("rstats_enable", "1")) {
1009 if (new) xstart("rstats", "--new");
1010 else xstart("rstats");
1014 // -----------------------------------------------------------------------------
1016 // !!TB - FTP Server
1019 static char *get_full_storage_path(char *val
)
1021 static char buf
[128];
1025 len
= sprintf(buf
, "%s", val
);
1027 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1029 if (len
> 1 && buf
[len
- 1] == '/')
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)
1053 char *user
, *pass
, *rights
;
1055 if (getpid() != 1) {
1056 start_service("ftpd");
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
)
1068 if (nvram_get_int("ftp_super"))
1071 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1072 if ((f
= fopen(tmp
, "w")))
1075 "dirlist_enable=yes\n"
1076 "write_enable=yes\n"
1077 "download_enable=yes\n");
1082 #ifdef TCONFIG_SAMBASRV
1083 if (nvram_match("smbd_cset", "utf8"))
1084 fprintf(fp
, "utf8=yes\n");
1087 if (nvram_invmatch("ftp_anonymous", "0"))
1090 "anon_allow_writable_root=yes\n"
1091 "anon_world_readable_only=no\n"
1092 "anon_umask=022\n");
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");
1108 if (nvram_match("ftp_anonymous", "1") ||
1109 nvram_match("ftp_anonymous", "3"))
1111 "anon_upload_enable=yes\n"
1112 "anon_mkdir_write_enable=yes\n"
1113 "anon_other_write_enable=yes\n");
1115 fprintf(fp
, "anonymous_enable=no\n");
1119 "dirmessage_enable=yes\n"
1120 "download_enable=no\n"
1121 "dirlist_enable=no\n"
1123 "syslog_enable=yes\n"
1124 "local_enable=yes\n"
1127 "chroot_local_user=yes\n"
1129 "log_ftp_protocol=%s\n"
1130 "user_config_dir=%s\n"
1138 "max_login_fails=1\n"
1139 "idle_session_timeout=%s\n"
1141 "anon_max_rate=%d\n"
1142 "local_max_rate=%d\n"
1144 nvram_get_int("log_ftp") ? "yes" : "no",
1145 vsftpd_users
, vsftpd_passwd
,
1147 ipv6_enabled() ? "_ipv6" : "",
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"));
1161 /* prepare passwd file and default users */
1162 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
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",
1176 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1179 username<password<rights
1187 while ((q
= strsep(&p
, ">")) != NULL
) {
1188 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
1189 if (!user
|| !pass
) continue;
1192 if (strncmp(rights
, "Private", 7) == 0)
1194 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
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
);
1204 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1205 if ((f
= fopen(tmp
, "w")))
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");
1223 killall("vsftpd", SIGHUP
);
1225 /* start vsftpd if it's not already running */
1226 if (pidof("vsftpd") <= 0)
1230 static void stop_ftpd(void)
1232 if (getpid() != 1) {
1233 stop_service("ftpd");
1237 killall_tk("vsftpd");
1238 unlink(vsftpd_passwd
);
1239 unlink(vsftpd_conf
);
1240 eval("rm", "-rf", vsftpd_users
);
1242 #endif // TCONFIG_FTP
1244 // -----------------------------------------------------------------------------
1248 #ifdef TCONFIG_SAMBASRV
1249 static void kill_samba(int sig
)
1251 if (sig
== SIGTERM
) {
1256 killall("smbd", sig
);
1257 killall("nmbd", sig
);
1261 static void start_samba(void)
1270 if (getpid() != 1) {
1271 start_service("smbd");
1275 mode
= nvram_get_int("smbd_enable");
1276 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1279 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1282 fprintf(fp
, "[global]\n"
1283 " interfaces = %s\n"
1284 " bind interfaces only = yes\n"
1286 " netbios name = %s\n"
1287 " server string = %s\n"
1288 " guest account = nobody\n"
1289 " security = user\n"
1292 " guest only = no\n"
1293 " browseable = yes\n"
1294 " syslog only = yes\n"
1295 " timestamp logs = no\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")) {
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");
1325 #ifndef TCONFIG_SAMBA3
1326 fprintf(fp
, " client code page = %s\n", nv
);
1328 sprintf(nlsmod
, "nls_cp%s", nv
);
1330 nv
= nvram_safe_get("smbd_nlsmod");
1331 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
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"));
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 */
1356 char *name
, *path
, *comment
, *writeable
, *hidden
;
1359 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1361 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1364 while ((q
= strsep(&p
, ">")) != NULL
) {
1365 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1366 if (!path
|| !name
) continue;
1369 fprintf(fp
, "\n[%s]\n", name
);
1372 fprintf(fp
, " path = %s\n", path
);
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");
1382 fprintf(fp
, " comment = %s\n", comment
);
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
, "..")) {
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 */
1402 if (!S_ISDIR(sb
.st_mode
))
1405 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1408 thisdev
= sb
.st_dev
;
1410 ++sb
.st_dev
; /* failsafe */
1412 if (thisdev
== sb
.st_dev
)
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");
1428 if (dir
) closedir(dir
);
1431 /* by default share MOUNT_ROOT as read-only */
1432 fprintf(fp
, "\n[share]\n"
1440 mkdir_if_none("/var/run/samba");
1441 mkdir_if_none("/etc/samba");
1443 /* write smbpasswd */
1444 #ifdef TCONFIG_SAMBA3
1445 eval("smbpasswd", "nobody", "\"\"");
1447 eval("smbpasswd", "-a", "nobody", "\"\"");
1451 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1453 #ifdef TCONFIG_SAMBA3
1454 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1456 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
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");
1478 kill_samba(SIGTERM
);
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)
1492 int port
, pid
, https
;
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");
1502 if (nvram_get_int("ms_sas") == 0)
1505 if (nvram_get_int("ms_enable") != 0) {
1506 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
1510 nvram_unset("ms_rescan");
1512 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
1513 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
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
);
1524 "network_interface=%s\n"
1526 "friendly_name=%s\n"
1530 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1532 "notify_interval=600\n"
1533 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\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
1546 char *path
, *restrict
;
1548 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
1549 /* path<restrict[A|V|P|] */
1552 while ((q
= strsep(&p
, ">")) != NULL
) {
1553 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
1555 fprintf(f
, "media_dir=%s%s%s\n",
1556 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
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.
1574 if (pidof(MEDIA_SERVER_APP
) > 0)
1581 static void stop_media_server(void)
1583 if (getpid() != 1) {
1584 stop_service("media");
1588 killall_tk(MEDIA_SERVER_APP
);
1590 #endif // TCONFIG_MEDIA_SERVER
1593 static void start_nas_services(void)
1595 if (getpid() != 1) {
1596 start_service("usbapps");
1600 #ifdef TCONFIG_SAMBASRV
1606 #ifdef TCONFIG_MEDIA_SERVER
1607 start_media_server();
1611 static void stop_nas_services(void)
1613 if (getpid() != 1) {
1614 stop_service("usbapps");
1618 #ifdef TCONFIG_MEDIA_SERVER
1619 stop_media_server();
1624 #ifdef TCONFIG_SAMBASRV
1629 void restart_nas_services(int stop
, int start
)
1631 int fd
= file_lock("usb");
1632 /* restart all NAS applications */
1634 stop_nas_services();
1636 start_nas_services();
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
);
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
);
1671 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
1673 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1674 _check(pid_crond
, "crond", start_cron
);
1675 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
1677 _check(pid_radvd
, "radvd", start_radvd
);
1681 // -----------------------------------------------------------------------------
1683 void start_services(void)
1685 static int once
= 1;
1690 if (nvram_get_int("telnetd_eas")) start_telnetd();
1691 if (nvram_get_int("sshd_eas")) start_sshd();
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.
1712 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1718 void stop_services(void)
1725 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
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;
1760 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1764 act
= strsep(&next
, ",");
1765 service
= strsep(&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
;
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();
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
) {
1796 if (strcmp(service
, "firewall") == 0) {
1797 if (action
& A_STOP
) {
1801 if (action
& A_START
) {
1808 if (strcmp(service
, "restrict") == 0) {
1809 if (action
& A_STOP
) {
1812 if (action
& A_START
) {
1813 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1817 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1819 if (nvram_get_int("rrules_radio") < 0) {
1820 eval("radio", "on");
1827 if (strcmp(service
, "qos") == 0) {
1828 if (action
& A_STOP
) {
1831 stop_firewall(); start_firewall(); // always restarted
1832 if (action
& A_START
) {
1834 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
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();
1851 if (strcmp(service
, "arpbind") == 0) {
1852 if (action
& A_STOP
) new_arpbind_stop();
1853 if (action
& A_START
) new_arpbind_start();
1858 if (strcmp(service
, "upnp") == 0) {
1859 if (action
& A_STOP
) {
1862 stop_firewall(); start_firewall(); // always restarted
1863 if (action
& A_START
) {
1869 if (strcmp(service
, "telnetd") == 0) {
1870 if (action
& A_STOP
) stop_telnetd();
1871 if (action
& A_START
) start_telnetd();
1875 if (strcmp(service
, "sshd") == 0) {
1876 if (action
& A_STOP
) stop_sshd();
1877 if (action
& A_START
) start_sshd();
1881 if (strcmp(service
, "httpd") == 0) {
1882 if (action
& A_STOP
) stop_httpd();
1883 if (action
& A_START
) start_httpd();
1888 if (strcmp(service
, "ipv6") == 0) {
1889 if (action
& A_STOP
) {
1893 if (action
& A_START
) {
1900 if (strcmp(service
, "radvd") == 0) {
1901 if (action
& A_STOP
) {
1904 if (action
& A_START
) {
1910 if (strncmp(service
, "dhcp6", 5) == 0) {
1911 if (action
& A_STOP
) {
1914 if (action
& A_START
) {
1921 if (strcmp(service
, "admin") == 0) {
1922 if (action
& A_STOP
) {
1927 stop_firewall(); start_firewall(); // always restarted
1928 if (action
& A_START
) {
1931 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1932 if (nvram_match("sshd_eas", "1")) start_sshd();
1937 if (strcmp(service
, "ddns") == 0) {
1938 if (action
& A_STOP
) stop_ddns();
1939 if (action
& A_START
) start_ddns();
1943 if (strcmp(service
, "ntpc") == 0) {
1944 if (action
& A_STOP
) stop_ntpc();
1945 if (action
& A_START
) start_ntpc();
1949 if (strcmp(service
, "logging") == 0) {
1950 if (action
& A_STOP
) {
1953 if (action
& A_START
) {
1957 // always restarted except from "service" command
1958 stop_cron(); start_cron();
1959 stop_firewall(); start_firewall();
1964 if (strcmp(service
, "crond") == 0) {
1965 if (action
& A_STOP
) {
1968 if (action
& A_START
) {
1975 if (strncmp(service
, "hotplug", 7) == 0) {
1976 if (action
& A_STOP
) {
1979 if (action
& A_START
) {
1986 if (strcmp(service
, "upgrade") == 0) {
1987 if (action
& A_START
) {
1992 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2000 killall("rstats", SIGTERM
);
2001 killall("buttons", SIGTERM
);
2003 remove_storage_main(1); // !!TB - USB Support
2004 stop_usb(); // !!TB - USB Support
2010 if (strcmp(service
, "cifs") == 0) {
2011 if (action
& A_STOP
) stop_cifs();
2012 if (action
& A_START
) start_cifs();
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();
2025 if (strcmp(service
, "zebra") == 0) {
2026 if (action
& A_STOP
) stop_zebra();
2027 if (action
& A_START
) start_zebra();
2031 if (strcmp(service
, "routing") == 0) {
2032 if (action
& A_STOP
) {
2034 do_static_routes(0); // remove old '_saved'
2035 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2039 if (action
& A_START
) {
2040 do_static_routes(1); // add new
2042 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2047 if (strcmp(service
, "ctnf") == 0) {
2048 if (action
& A_START
) {
2056 if (strcmp(service
, "wan") == 0) {
2057 if (action
& A_STOP
) {
2061 if (action
& A_START
) {
2062 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2070 if (strcmp(service
, "net") == 0) {
2071 if (action
& A_STOP
) {
2082 if (action
& A_START
) {
2097 if (strcmp(service
, "nas") == 0) {
2098 if (action
& A_STOP
) {
2101 if (action
& A_START
) {
2108 if (strcmp(service
, "rstats") == 0) {
2109 if (action
& A_STOP
) stop_rstats();
2110 if (action
& A_START
) start_rstats(0);
2114 if (strcmp(service
, "rstatsnew") == 0) {
2115 if (action
& A_STOP
) stop_rstats();
2116 if (action
& A_START
) start_rstats(1);
2120 if (strcmp(service
, "sched") == 0) {
2121 if (action
& A_STOP
) stop_sched();
2122 if (action
& A_START
) start_sched();
2127 if (strcmp(service
, "bittorrent") == 0) {
2128 if (action
& A_STOP
) {
2131 stop_firewall(); start_firewall(); // always restarted
2132 if (action
& A_START
) {
2140 // !!TB - USB Support
2141 if (strcmp(service
, "usb") == 0) {
2142 if (action
& A_STOP
) stop_usb();
2143 if (action
& A_START
) {
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);
2153 if (strcmp(service
, "usbapps") == 0) {
2154 if (action
& A_STOP
) stop_nas_services();
2155 if (action
& A_START
) start_nas_services();
2161 // !!TB - FTP Server
2162 if (strcmp(service
, "ftpd") == 0) {
2163 if (action
& A_STOP
) stop_ftpd();
2167 if (action
& A_START
) start_ftpd();
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();
2180 #ifdef TCONFIG_SAMBASRV
2182 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2183 if (action
& A_STOP
) stop_samba();
2184 if (action
& A_START
) {
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]));
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]));
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
)
2224 while (!nvram_match("action_service", "")) {
2229 else if (--n
< 0) break;
2233 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2234 nvram_set("action_service", s
);
2238 while (nvram_match("action_service", s
)) {
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");
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);