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 static void add_ip6_lanaddr(void)
435 char ip
[INET6_ADDRSTRLEN
+ 4];
438 p
= ipv6_router_address(NULL
);
440 snprintf(ip
, sizeof(ip
), "%s/%d", p
, nvram_get_int("ipv6_prefix_length") ? : 64);
441 eval("ip", "-6", "addr", "add", ip
, "dev", nvram_safe_get("lan_ifname"));
445 void start_ipv6_tunnel(void)
447 char ip
[INET6_ADDRSTRLEN
+ 4];
448 struct in_addr addr4
;
449 struct in6_addr addr
;
450 const char *wanip
, *mtu
, *tun_dev
;
453 service
= get_ipv6_service();
454 tun_dev
= nvram_safe_get("ipv6_ifname");
456 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
459 if (service
== IPV6_ANYCAST_6TO4
)
460 snprintf(ip
, sizeof(ip
), "192.88.99.%d", nvram_get_int("ipv6_relay"));
462 strlcpy(ip
, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip
));
463 eval("ip", "tunnel", "add", (char *)tun_dev
, "mode", "sit",
465 "local", (char *)wanip
,
466 "ttl", nvram_safe_get("ipv6_tun_ttl"));
468 eval("ip", "link", "set", (char *)tun_dev
, "mtu", (char *)mtu
, "up");
470 if (service
== IPV6_ANYCAST_6TO4
) {
473 memset(&addr
, 0, sizeof(addr
));
474 inet_aton(wanip
, &addr4
);
475 addr
.s6_addr16
[0] = htons(0x2002);
476 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
477 addr
.s6_addr16
[7] = htons(0x0001);
478 inet_ntop(AF_INET6
, &addr
, ip
, sizeof(ip
));
479 strncat(ip
, "/16", sizeof(ip
));
482 snprintf(ip
, sizeof(ip
), "%s/%d",
483 nvram_safe_get("ipv6_tun_addr"),
484 nvram_get_int("ipv6_tun_addrlen") ? : 64);
486 eval("ip", "addr", "add", ip
, "dev", (char *)tun_dev
);
487 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev
);
489 // notify radvd of possible change
490 if (service
== IPV6_ANYCAST_6TO4
)
491 killall("radvd", SIGHUP
);
494 void stop_ipv6_tunnel(void)
496 char *tun_dev
= nvram_safe_get("ipv6_ifname");
497 eval("ip", "tunnel", "del", tun_dev
);
501 static pid_t pid_radvd
= -1;
503 void start_radvd(void)
506 char *prefix
, *ip
, *mtu
;
508 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
509 int pid
, argc
, service
;
512 start_service("radvd");
518 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
519 service
= get_ipv6_service();
520 do_6to4
= (service
== IPV6_ANYCAST_6TO4
);
524 case IPV6_NATIVE_DHCP
:
527 case IPV6_ANYCAST_6TO4
:
529 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
532 prefix
= do_6to4
? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
535 if (!(*prefix
)) prefix
= "::";
538 if ((f
= fopen("/etc/radvd.conf", "w")) == NULL
) return;
540 ip
= (char *)ipv6_router_address(NULL
);
541 do_dns
= (*ip
) && nvram_match("dhcpd_dmdns", "1");
546 " IgnoreIfMissing on;\n"
547 " AdvSendAdvert on;\n"
548 " MaxRtrAdvInterval 60;\n"
549 " AdvHomeAgentFlag off;\n"
550 " AdvManagedFlag off;\n"
555 " AdvAutonomous on;\n"
560 nvram_safe_get("lan_ifname"),
561 mtu
? " AdvLinkMTU " : "", mtu
? : "", mtu
? ";\n" : "",
563 do_6to4
? " Base6to4Interface " : "",
564 do_6to4
? get_wanface() : "",
565 do_6to4
? ";\n" : "",
566 do_dns
? "RDNSS " : "", do_dns
? ip
: "", do_dns
? " { };" : "");
571 if (nvram_get_int("debug_ipv6")) {
576 _eval(argv
, NULL
, 0, &pid
);
578 if (!nvram_contains_word("debug_norestart", "radvd")) {
584 void stop_radvd(void)
587 stop_service("radvd");
595 void start_ipv6(void)
599 service
= get_ipv6_service();
602 // Check if turned on
609 case IPV6_NATIVE_DHCP
:
610 case IPV6_ANYCAST_6TO4
:
611 nvram_set("ipv6_rtr_addr", "");
612 nvram_set("ipv6_prefix", "");
616 if (service
!= IPV6_DISABLED
) {
617 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
618 accept_ra(nvram_safe_get("lan_ifname"));
626 eval("ip", "-6", "addr", "flush", "scope", "global");
631 // -----------------------------------------------------------------------------
633 void start_upnp(void)
636 start_service("upnp");
640 if (get_wan_proto() == WP_DISABLED
) return;
646 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
647 mkdir("/etc/upnp", 0777);
648 if (f_exists("/etc/upnp/config.alt")) {
649 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
652 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
653 upnp_port
= nvram_get_int("upnp_port");
654 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
656 char *lanip
= nvram_safe_get("lan_ipaddr");
657 char *lanmask
= nvram_safe_get("lan_netmask");
661 "listening_ip=%s/%s\n"
666 "upnp_forward_chain=upnp\n"
667 "upnp_nat_chain=upnp\n"
668 "notify_interval=%d\n"
669 "system_uptime=yes\n"
675 (enable
& 1) ? "yes" : "no", // upnp enable
676 (enable
& 2) ? "yes" : "no", // natpmp enable
677 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
678 nvram_get_int("upnp_ssdp_interval")
681 if (nvram_get_int("upnp_clean")) {
682 int interval
= nvram_get_int("upnp_clean_interval");
683 if (interval
< 60) interval
= 60;
685 "clean_ruleset_interval=%d\n"
686 "clean_ruleset_threshold=%d\n",
688 nvram_get_int("upnp_clean_threshold")
692 fprintf(f
,"clean_ruleset_interval=0\n");
694 if (nvram_match("upnp_mnp", "1")) {
695 int https
= nvram_get_int("https_enable");
696 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
697 https
? "s" : "", lanip
,
698 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
701 // Empty parameters are not included into XML service description
702 fprintf(f
, "presentation_url=\n");
706 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
707 fprintf(f
, "uuid=%s\n", uuid
);
710 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
711 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
712 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
713 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
715 "allow %d-%d %s/%s %d-%d\n",
722 // by default allow only redirection of ports above 1024
723 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
726 fappend(f
, "/jffs/upnpconfig.custom");
727 fappend(f
, "/etc/upnp/config.custom");
728 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
731 xstart("miniupnpd", "-f", "/etc/upnp/config");
740 stop_service("upnp");
744 killall_tk("miniupnpd");
747 // -----------------------------------------------------------------------------
749 static pid_t pid_crond
= -1;
751 void start_cron(void)
755 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
756 if (!nvram_contains_word("debug_norestart", "crond")) {
767 // -----------------------------------------------------------------------------
770 static pid_t pid_hotplug2
= -1;
772 void start_hotplug2()
776 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
777 xstart("hotplug2", "--persistent", "--no-coldplug");
778 // FIXME: Don't remember exactly why I put "sleep" here -
779 // but it was not for a race with check_services()... - TB
782 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
787 void stop_hotplug2(void)
790 killall_tk("hotplug2");
794 // -----------------------------------------------------------------------------
796 // Written by Sparq in 2002/07/16
797 void start_zebra(void)
801 start_service("zebra");
807 char *lan_tx
= nvram_safe_get("dr_lan_tx");
808 char *lan_rx
= nvram_safe_get("dr_lan_rx");
809 char *wan_tx
= nvram_safe_get("dr_wan_tx");
810 char *wan_rx
= nvram_safe_get("dr_wan_rx");
812 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
817 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
822 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
823 char *lan_ifname
= nvram_safe_get("lan_ifname");
824 char *wan_ifname
= nvram_safe_get("wan_ifname");
826 fprintf(fp
, "router rip\n");
827 fprintf(fp
, "network %s\n", lan_ifname
);
828 fprintf(fp
, "network %s\n", wan_ifname
);
829 fprintf(fp
, "redistribute connected\n");
830 //fprintf(fp, "redistribute static\n");
832 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
833 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
835 fprintf(fp
, "interface %s\n", lan_ifname
);
836 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
837 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
839 fprintf(fp
, "interface %s\n", wan_ifname
);
840 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
841 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
843 fprintf(fp
, "router rip\n");
844 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
845 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
846 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
847 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
848 fprintf(fp
, "access-list private deny any\n");
850 //fprintf(fp, "debug rip events\n");
851 //fprintf(fp, "log file /etc/ripd.log\n");
855 xstart("zebra", "-d");
856 xstart("ripd", "-d");
860 void stop_zebra(void)
864 stop_service("zebra");
868 killall("zebra", SIGTERM
);
869 killall("ripd", SIGTERM
);
871 unlink("/etc/zebra.conf");
872 unlink("/etc/ripd.conf");
876 // -----------------------------------------------------------------------------
878 void start_syslog(void)
888 char *rot_siz
= "50";
893 if (nvram_match("log_remote", "1")) {
894 nv
= nvram_safe_get("log_remoteip");
896 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
902 if (nvram_match("log_file", "1")) {
905 /* Read options: rotate_size(kb) num_backups logfilename.
906 * Ignore these settings and use defaults if the logfile cannot be written to.
908 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
909 if ((nv
= strchr(cfg
, '\n')))
912 if ((nv
= strtok(cfg
, " \t"))) {
917 if ((nv
= strtok(NULL
, " \t")))
920 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
921 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
933 argv
[argc
++] = rot_siz
;
935 if (isdigit(*b_opt
)) {
937 argv
[argc
++] = b_opt
;
943 _eval(argv
, NULL
, 0, NULL
);
947 _eval(argv
, NULL
, 0, NULL
);
949 // used to be available in syslogd -m
950 n
= nvram_get_int("log_mark");
954 sprintf(rem
, "*/%d * * * *", n
);
955 else if (n
< 60 * 24)
956 sprintf(rem
, "0 */%d * * *", n
/ 60);
958 sprintf(rem
, "0 0 */%d * *", n
/ (60 * 24));
959 sprintf(s
, "%s logger -p syslog.info -- -- MARK --", rem
);
960 eval("cru", "a", "syslogdmark", s
);
963 eval("cru", "d", "syslogdmark");
968 void stop_syslog(void)
970 killall("klogd", SIGTERM
);
971 killall("syslogd", SIGTERM
);
974 // -----------------------------------------------------------------------------
976 static pid_t pid_igmp
= -1;
978 void start_igmp_proxy(void)
983 if (nvram_match("multicast_pass", "1")) {
984 if (get_wan_proto() == WP_DISABLED
)
987 if (f_exists("/etc/igmp.alt")) {
988 eval("igmpproxy", "/etc/igmp.alt");
990 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
993 "phyint %s upstream\n"
995 "phyint %s downstream ratelimit 0\n",
996 nvram_safe_get("wan_ifname"),
997 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
998 nvram_safe_get("lan_ifname"));
1000 eval("igmpproxy", "/etc/igmp.conf");
1005 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1011 void stop_igmp_proxy(void)
1014 killall_tk("igmpproxy");
1018 // -----------------------------------------------------------------------------
1022 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
1025 void start_ntpc(void)
1031 if (nvram_get_int("ntp_updates") >= 0) {
1032 xstart("ntpsync", "--init");
1036 void stop_ntpc(void)
1038 killall("ntpsync", SIGTERM
);
1041 // -----------------------------------------------------------------------------
1043 static void stop_rstats(void)
1049 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1050 if (kill(pid
, SIGTERM
) != 0) break;
1055 static void start_rstats(int new)
1057 if (nvram_match("rstats_enable", "1")) {
1059 if (new) xstart("rstats", "--new");
1060 else xstart("rstats");
1064 // -----------------------------------------------------------------------------
1066 // !!TB - FTP Server
1069 static char *get_full_storage_path(char *val
)
1071 static char buf
[128];
1075 len
= sprintf(buf
, "%s", val
);
1077 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1079 if (len
> 1 && buf
[len
- 1] == '/')
1085 static char *nvram_storage_path(char *var
)
1087 char *val
= nvram_safe_get(var
);
1088 return get_full_storage_path(val
);
1091 char vsftpd_conf
[] = "/etc/vsftpd.conf";
1092 char vsftpd_users
[] = "/etc/vsftpd.users";
1093 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
1095 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1097 static void start_ftpd(void)
1103 char *user
, *pass
, *rights
;
1105 if (getpid() != 1) {
1106 start_service("ftpd");
1110 if (!nvram_get_int("ftp_enable")) return;
1112 mkdir_if_none(vsftpd_users
);
1113 mkdir_if_none("/var/run/vsftpd");
1115 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
1118 if (nvram_get_int("ftp_super"))
1121 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1122 if ((f
= fopen(tmp
, "w")))
1125 "dirlist_enable=yes\n"
1126 "write_enable=yes\n"
1127 "download_enable=yes\n");
1132 #ifdef TCONFIG_SAMBASRV
1133 if (nvram_match("smbd_cset", "utf8"))
1134 fprintf(fp
, "utf8=yes\n");
1137 if (nvram_invmatch("ftp_anonymous", "0"))
1140 "anon_allow_writable_root=yes\n"
1141 "anon_world_readable_only=no\n"
1142 "anon_umask=022\n");
1145 sprintf(tmp
, "%s/ftp", vsftpd_users
);
1146 if ((f
= fopen(tmp
, "w")))
1148 if (nvram_match("ftp_dirlist", "0"))
1149 fprintf(f
, "dirlist_enable=yes\n");
1150 if (nvram_match("ftp_anonymous", "1") ||
1151 nvram_match("ftp_anonymous", "3"))
1152 fprintf(f
, "write_enable=yes\n");
1153 if (nvram_match("ftp_anonymous", "1") ||
1154 nvram_match("ftp_anonymous", "2"))
1155 fprintf(f
, "download_enable=yes\n");
1158 if (nvram_match("ftp_anonymous", "1") ||
1159 nvram_match("ftp_anonymous", "3"))
1161 "anon_upload_enable=yes\n"
1162 "anon_mkdir_write_enable=yes\n"
1163 "anon_other_write_enable=yes\n");
1165 fprintf(fp
, "anonymous_enable=no\n");
1169 "dirmessage_enable=yes\n"
1170 "download_enable=no\n"
1171 "dirlist_enable=no\n"
1173 "syslog_enable=yes\n"
1174 "local_enable=yes\n"
1177 "chroot_local_user=yes\n"
1179 "log_ftp_protocol=%s\n"
1180 "user_config_dir=%s\n"
1188 "max_login_fails=1\n"
1189 "idle_session_timeout=%s\n"
1191 "anon_max_rate=%d\n"
1192 "local_max_rate=%d\n"
1194 nvram_get_int("log_ftp") ? "yes" : "no",
1195 vsftpd_users
, vsftpd_passwd
,
1197 ipv6_enabled() ? "_ipv6" : "",
1201 nvram_get("ftp_port") ? : "21",
1202 nvram_get_int("ftp_max"),
1203 nvram_get_int("ftp_ipmax"),
1204 nvram_get("ftp_staytimeout") ? : "300",
1205 nvram_get_int("ftp_anonrate") * 1024,
1206 nvram_get_int("ftp_rate") * 1024,
1207 nvram_safe_get("ftp_custom"));
1211 /* prepare passwd file and default users */
1212 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
1215 if (((user
= nvram_get("http_username")) == NULL
) || (*user
== 0)) user
= "admin";
1216 if (((pass
= nvram_get("http_passwd")) == NULL
) || (*pass
== 0)) pass
= "admin";
1218 fprintf(fp
, /* anonymous, admin, nobody */
1219 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1220 "%s:%s:0:0:root:/:/sbin/nologin\n"
1221 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1222 nvram_storage_path("ftp_anonroot"), user
,
1223 nvram_get_int("ftp_super") ? crypt(pass
, "$1$") : "x",
1226 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1229 username<password<rights
1237 while ((q
= strsep(&p
, ">")) != NULL
) {
1238 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
1239 if (!user
|| !pass
) continue;
1242 if (strncmp(rights
, "Private", 7) == 0)
1244 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
1248 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
1250 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1251 user
, crypt(pass
, "$1$"), user
, tmp
);
1254 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1255 if ((f
= fopen(tmp
, "w")))
1258 if (nvram_invmatch("ftp_dirlist", "1"))
1259 strcat(tmp
, "dirlist_enable=yes\n");
1260 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
1261 strcat(tmp
, "download_enable=yes\n");
1262 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
1263 strcat(tmp
, "write_enable=yes\n");
1273 killall("vsftpd", SIGHUP
);
1275 /* start vsftpd if it's not already running */
1276 if (pidof("vsftpd") <= 0)
1280 static void stop_ftpd(void)
1282 if (getpid() != 1) {
1283 stop_service("ftpd");
1287 killall_tk("vsftpd");
1288 unlink(vsftpd_passwd
);
1289 unlink(vsftpd_conf
);
1290 eval("rm", "-rf", vsftpd_users
);
1292 #endif // TCONFIG_FTP
1294 // -----------------------------------------------------------------------------
1298 #ifdef TCONFIG_SAMBASRV
1299 static void kill_samba(int sig
)
1301 if (sig
== SIGTERM
) {
1306 killall("smbd", sig
);
1307 killall("nmbd", sig
);
1311 static void start_samba(void)
1320 if (getpid() != 1) {
1321 start_service("smbd");
1325 mode
= nvram_get_int("smbd_enable");
1326 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1329 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1332 fprintf(fp
, "[global]\n"
1333 " interfaces = %s\n"
1334 " bind interfaces only = yes\n"
1336 " netbios name = %s\n"
1337 " server string = %s\n"
1338 " guest account = nobody\n"
1339 " security = user\n"
1342 " guest only = no\n"
1343 " browseable = yes\n"
1344 " syslog only = yes\n"
1345 " timestamp logs = no\n"
1347 " encrypt passwords = yes\n"
1348 " preserve case = yes\n"
1349 " short preserve case = yes\n",
1350 nvram_safe_get("lan_ifname"),
1351 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1352 nvram_safe_get("lan_hostname"),
1353 nvram_get("router_name") ? : "Tomato",
1354 mode
== 2 ? "" : "map to guest = Bad User",
1355 mode
== 2 ? "no" : "yes" // guest ok
1358 if (nvram_get_int("smbd_wins")) {
1359 nv
= nvram_safe_get("wan_wins");
1360 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1361 fprintf(fp
, " wins support = yes\n");
1365 if (nvram_get_int("smbd_master")) {
1367 " domain master = yes\n"
1368 " local master = yes\n"
1369 " preferred master = yes\n"
1370 " os level = 65\n");
1373 nv
= nvram_safe_get("smbd_cpage");
1375 #ifndef TCONFIG_SAMBA3
1376 fprintf(fp
, " client code page = %s\n", nv
);
1378 sprintf(nlsmod
, "nls_cp%s", nv
);
1380 nv
= nvram_safe_get("smbd_nlsmod");
1381 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1385 nvram_set("smbd_nlsmod", nlsmod
);
1388 #ifndef TCONFIG_SAMBA3
1389 if (nvram_match("smbd_cset", "utf8"))
1390 fprintf(fp
, " coding system = utf8\n");
1391 else if (nvram_invmatch("smbd_cset", ""))
1392 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1395 nv
= nvram_safe_get("smbd_custom");
1396 /* add socket options unless overriden by the user */
1397 if (strstr(nv
, "socket options") == NULL
) {
1398 fprintf(fp
, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1400 fprintf(fp
, "%s\n\n", nv
);
1402 /* configure shares */
1406 char *name
, *path
, *comment
, *writeable
, *hidden
;
1409 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1411 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1414 while ((q
= strsep(&p
, ">")) != NULL
) {
1415 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1416 if (!path
|| !name
) continue;
1419 fprintf(fp
, "\n[%s]\n", name
);
1422 fprintf(fp
, " path = %s\n", path
);
1425 if (!strcmp(writeable
, "1"))
1426 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1427 if (!strcmp(hidden
, "1"))
1428 fprintf(fp
, " browseable = no\n");
1432 fprintf(fp
, " comment = %s\n", comment
);
1439 /* Share every mountpoint below MOUNT_ROOT */
1440 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1441 while ((dp
= readdir(dir
))) {
1442 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1448 /* Only if is a directory and is mounted */
1449 sprintf(path
, "%s/%s", MOUNT_ROOT
, dp
->d_name
);
1450 sb
.st_mode
= S_IFDIR
; /* failsafe */
1452 if (!S_ISDIR(sb
.st_mode
))
1455 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1458 thisdev
= sb
.st_dev
;
1460 ++sb
.st_dev
; /* failsafe */
1462 if (thisdev
== sb
.st_dev
)
1465 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1466 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1467 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1468 if (nvram_match("smbd_autoshare", "3")) // Hidden
1469 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1470 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1471 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1472 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1478 if (dir
) closedir(dir
);
1481 /* by default share MOUNT_ROOT as read-only */
1482 fprintf(fp
, "\n[share]\n"
1490 mkdir_if_none("/var/run/samba");
1491 mkdir_if_none("/etc/samba");
1493 /* write smbpasswd */
1494 #ifdef TCONFIG_SAMBA3
1495 eval("smbpasswd", "nobody", "\"\"");
1497 eval("smbpasswd", "-a", "nobody", "\"\"");
1501 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1503 #ifdef TCONFIG_SAMBA3
1504 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1506 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1511 int ret1
= 0, ret2
= 0;
1512 /* start samba if it's not already running */
1513 if (pidof("nmbd") <= 0)
1514 ret1
= xstart("nmbd", "-D");
1515 if (pidof("smbd") <= 0)
1516 ret2
= xstart("smbd", "-D");
1518 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1521 static void stop_samba(void)
1523 if (getpid() != 1) {
1524 stop_service("smbd");
1528 kill_samba(SIGTERM
);
1530 unlink("/var/log/smb");
1531 unlink("/var/log/nmb");
1532 eval("rm", "-rf", "/var/run/samba");
1534 #endif // TCONFIG_SAMBASRV
1536 #ifdef TCONFIG_MEDIA_SERVER
1537 #define MEDIA_SERVER_APP "minidlna"
1539 static void start_media_server(void)
1542 int port
, pid
, https
;
1544 char *argv
[] = { MEDIA_SERVER_APP
, "-f", "/etc/"MEDIA_SERVER_APP
".conf", "-R", NULL
};
1545 static int once
= 1;
1547 if (getpid() != 1) {
1548 start_service("media");
1552 if (nvram_get_int("ms_sas") == 0)
1555 if (nvram_get_int("ms_enable") != 0) {
1556 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
1560 nvram_unset("ms_rescan");
1562 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
1563 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
1566 if ((f
= fopen(argv
[2], "w")) != NULL
) {
1567 port
= nvram_get_int("ms_port");
1568 https
= nvram_get_int("https_enable");
1569 dbdir
= nvram_safe_get("ms_dbdir");
1570 if (!(*dbdir
)) dbdir
= NULL
;
1571 mkdir_if_none(dbdir
? : "/var/run/"MEDIA_SERVER_APP
);
1574 "network_interface=%s\n"
1576 "friendly_name=%s\n"
1580 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1582 "notify_interval=600\n"
1583 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\n"
1585 nvram_safe_get("lan_ifname"),
1586 (port
< 0) || (port
>= 0xffff) ? 0 : port
,
1587 nvram_get("router_name") ? : "Tomato",
1588 dbdir
? : "/var/run/"MEDIA_SERVER_APP
,
1589 nvram_get_int("ms_tivo") ? "yes" : "no",
1590 nvram_get_int("ms_stdlna") ? "yes" : "no",
1591 https
? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https
? "https_lanport" : "http_lanport")
1594 // media directories
1596 char *path
, *restrict
;
1598 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
1599 /* path<restrict[A|V|P|] */
1602 while ((q
= strsep(&p
, ">")) != NULL
) {
1603 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
1605 fprintf(f
, "media_dir=%s%s%s\n",
1606 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
1615 /* start media server if it's not already running */
1616 if (pidof(MEDIA_SERVER_APP
) <= 0) {
1617 if ((_eval(argv
, NULL
, 0, &pid
) == 0) && (once
)) {
1618 /* If we started the media server successfully, wait 1 sec
1619 * to let it die if it can't open the database file.
1620 * If it's still alive after that, assume it's running and
1621 * disable forced once-after-reboot rescan.
1624 if (pidof(MEDIA_SERVER_APP
) > 0)
1631 static void stop_media_server(void)
1633 if (getpid() != 1) {
1634 stop_service("media");
1638 killall_tk(MEDIA_SERVER_APP
);
1640 #endif // TCONFIG_MEDIA_SERVER
1643 static void start_nas_services(void)
1645 if (getpid() != 1) {
1646 start_service("usbapps");
1650 #ifdef TCONFIG_SAMBASRV
1656 #ifdef TCONFIG_MEDIA_SERVER
1657 start_media_server();
1661 static void stop_nas_services(void)
1663 if (getpid() != 1) {
1664 stop_service("usbapps");
1668 #ifdef TCONFIG_MEDIA_SERVER
1669 stop_media_server();
1674 #ifdef TCONFIG_SAMBASRV
1679 void restart_nas_services(int stop
, int start
)
1681 int fd
= file_lock("usb");
1682 /* restart all NAS applications */
1684 stop_nas_services();
1686 start_nas_services();
1689 #endif // TCONFIG_USB
1691 // -----------------------------------------------------------------------------
1693 /* -1 = Don't check for this program, it is not expected to be running.
1694 * Other = This program has been started and should be kept running. If no
1695 * process with the name is running, call func to restart it.
1696 * Note: At startup, dnsmasq forks a short-lived child which forks a
1697 * long-lived (grand)child. The parents terminate.
1698 * Many daemons use this technique.
1700 static void _check(pid_t pid
, const char *name
, void (*func
)(void))
1702 if (pid
== -1) return;
1704 if (pidof(name
) > 0) return;
1706 syslog(LOG_DEBUG
, "%s terminated unexpectedly, restarting.\n", name
);
1709 // Force recheck in 500 msec
1710 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
1713 void check_services(void)
1715 TRACE_PT("keep alive\n");
1717 // Periodically reap any zombies
1718 setitimer(ITIMER_REAL
, &zombie_tv
, NULL
);
1721 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
1723 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1724 _check(pid_crond
, "crond", start_cron
);
1725 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
1727 _check(pid_radvd
, "radvd", start_radvd
);
1731 // -----------------------------------------------------------------------------
1733 void start_services(void)
1735 static int once
= 1;
1740 if (nvram_get_int("telnetd_eas")) start_telnetd();
1741 if (nvram_get_int("sshd_eas")) start_sshd();
1755 /* note: starting radvd here might be too early in case of
1756 * DHCPv6 because we won't have received a prefix and so it
1757 * will disable advertisements, but the SIGHUP sent from
1758 * dhcp6c-state will restart them.
1762 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1765 void stop_services(void)
1769 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1785 // -----------------------------------------------------------------------------
1787 /* nvram "action_service" is: "service-action[-modifier]"
1788 * action is something like "stop" or "start" or "restart"
1789 * optional modifier is "c" for the "service" command-line command
1791 void exec_service(void)
1793 const int A_START
= 1;
1794 const int A_STOP
= 2;
1795 const int A_RESTART
= 1|2;
1804 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1808 act
= strsep(&next
, ",");
1809 service
= strsep(&act
, "-");
1815 strsep(&modifier
, "-");
1817 TRACE_PT("service=%s action=%s modifier=%s\n", service
, act
, modifier
? : "");
1819 if (strcmp(act
, "start") == 0) action
= A_START
;
1820 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1821 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1823 user
= (modifier
!= NULL
&& *modifier
== 'c');
1825 if (strcmp(service
, "dhcpc") == 0) {
1826 if (action
& A_STOP
) stop_dhcpc();
1827 if (action
& A_START
) start_dhcpc();
1831 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1832 if (action
& A_STOP
) stop_dnsmasq();
1833 if (action
& A_START
) {
1840 if (strcmp(service
, "firewall") == 0) {
1841 if (action
& A_STOP
) {
1845 if (action
& A_START
) {
1852 if (strcmp(service
, "restrict") == 0) {
1853 if (action
& A_STOP
) {
1856 if (action
& A_START
) {
1857 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1861 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1863 if (nvram_get_int("rrules_radio") < 0) {
1864 eval("radio", "on");
1871 if (strcmp(service
, "qos") == 0) {
1872 if (action
& A_STOP
) {
1875 stop_firewall(); start_firewall(); // always restarted
1876 if (action
& A_START
) {
1878 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1883 if (strcmp(service
, "qoslimit") == 0) {
1884 if (action
& A_STOP
) {
1885 new_qoslimit_stop();
1887 stop_firewall(); start_firewall(); // always restarted
1888 if (action
& A_START
) {
1889 new_qoslimit_start();
1894 if (strcmp(service
, "arpbind") == 0) {
1895 if (action
& A_STOP
) new_arpbind_stop();
1896 if (action
& A_START
) new_arpbind_start();
1900 if (strcmp(service
, "upnp") == 0) {
1901 if (action
& A_STOP
) {
1904 stop_firewall(); start_firewall(); // always restarted
1905 if (action
& A_START
) {
1911 if (strcmp(service
, "telnetd") == 0) {
1912 if (action
& A_STOP
) stop_telnetd();
1913 if (action
& A_START
) start_telnetd();
1917 if (strcmp(service
, "sshd") == 0) {
1918 if (action
& A_STOP
) stop_sshd();
1919 if (action
& A_START
) start_sshd();
1923 if (strcmp(service
, "httpd") == 0) {
1924 if (action
& A_STOP
) stop_httpd();
1925 if (action
& A_START
) start_httpd();
1930 if (strcmp(service
, "ipv6") == 0) {
1931 if (action
& A_STOP
) {
1935 if (action
& A_START
) {
1942 if (strcmp(service
, "radvd") == 0) {
1943 if (action
& A_STOP
) {
1946 if (action
& A_START
) {
1952 if (strncmp(service
, "dhcp6", 5) == 0) {
1953 if (action
& A_STOP
) {
1956 if (action
& A_START
) {
1963 if (strcmp(service
, "admin") == 0) {
1964 if (action
& A_STOP
) {
1969 stop_firewall(); start_firewall(); // always restarted
1970 if (action
& A_START
) {
1973 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1974 if (nvram_match("sshd_eas", "1")) start_sshd();
1979 if (strcmp(service
, "ddns") == 0) {
1980 if (action
& A_STOP
) stop_ddns();
1981 if (action
& A_START
) start_ddns();
1985 if (strcmp(service
, "ntpc") == 0) {
1986 if (action
& A_STOP
) stop_ntpc();
1987 if (action
& A_START
) start_ntpc();
1991 if (strcmp(service
, "logging") == 0) {
1992 if (action
& A_STOP
) {
1995 if (action
& A_START
) {
1999 // always restarted except from "service" command
2000 stop_cron(); start_cron();
2001 stop_firewall(); start_firewall();
2006 if (strcmp(service
, "crond") == 0) {
2007 if (action
& A_STOP
) {
2010 if (action
& A_START
) {
2017 if (strncmp(service
, "hotplug", 7) == 0) {
2018 if (action
& A_STOP
) {
2021 if (action
& A_START
) {
2028 if (strcmp(service
, "upgrade") == 0) {
2029 if (action
& A_START
) {
2034 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2042 killall("rstats", SIGTERM
);
2043 killall("buttons", SIGTERM
);
2045 remove_storage_main(1); // !!TB - USB Support
2046 stop_usb(); // !!TB - USB Support
2052 if (strcmp(service
, "cifs") == 0) {
2053 if (action
& A_STOP
) stop_cifs();
2054 if (action
& A_START
) start_cifs();
2059 #ifdef TCONFIG_JFFS2
2060 if (strncmp(service
, "jffs", 4) == 0) {
2061 if (action
& A_STOP
) stop_jffs2();
2062 if (action
& A_START
) start_jffs2();
2067 if (strcmp(service
, "zebra") == 0) {
2068 if (action
& A_STOP
) stop_zebra();
2069 if (action
& A_START
) start_zebra();
2073 if (strcmp(service
, "routing") == 0) {
2074 if (action
& A_STOP
) {
2076 do_static_routes(0); // remove old '_saved'
2077 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2081 if (action
& A_START
) {
2082 do_static_routes(1); // add new
2084 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2089 if (strcmp(service
, "ctnf") == 0) {
2090 if (action
& A_START
) {
2098 if (strcmp(service
, "wan") == 0) {
2099 if (action
& A_STOP
) {
2103 if (action
& A_START
) {
2104 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2112 if (strcmp(service
, "net") == 0) {
2113 if (action
& A_STOP
) {
2124 if (action
& A_START
) {
2139 if (strcmp(service
, "nas") == 0) {
2140 if (action
& A_STOP
) {
2143 if (action
& A_START
) {
2150 if (strcmp(service
, "rstats") == 0) {
2151 if (action
& A_STOP
) stop_rstats();
2152 if (action
& A_START
) start_rstats(0);
2156 if (strcmp(service
, "rstatsnew") == 0) {
2157 if (action
& A_STOP
) stop_rstats();
2158 if (action
& A_START
) start_rstats(1);
2162 if (strcmp(service
, "sched") == 0) {
2163 if (action
& A_STOP
) stop_sched();
2164 if (action
& A_START
) start_sched();
2169 // !!TB - USB Support
2170 if (strcmp(service
, "usb") == 0) {
2171 if (action
& A_STOP
) stop_usb();
2172 if (action
& A_START
) {
2174 // restart Samba and ftp since they may be killed by stop_usb()
2175 restart_nas_services(0, 1);
2176 // remount all partitions by simulating hotplug event
2177 add_remove_usbhost("-1", 1);
2182 if (strcmp(service
, "usbapps") == 0) {
2183 if (action
& A_STOP
) stop_nas_services();
2184 if (action
& A_START
) start_nas_services();
2190 // !!TB - FTP Server
2191 if (strcmp(service
, "ftpd") == 0) {
2192 if (action
& A_STOP
) stop_ftpd();
2196 if (action
& A_START
) start_ftpd();
2201 #ifdef TCONFIG_MEDIA_SERVER
2202 if (strcmp(service
, "media") == 0 || strcmp(service
, "dlna") == 0) {
2203 if (action
& A_STOP
) stop_media_server();
2204 if (action
& A_START
) start_media_server();
2209 #ifdef TCONFIG_SAMBASRV
2211 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2212 if (action
& A_STOP
) stop_samba();
2213 if (action
& A_START
) {
2223 #ifdef TCONFIG_OPENVPN
2224 if (strncmp(service
, "vpnclient", 9) == 0) {
2225 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
2226 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
2230 if (strncmp(service
, "vpnserver", 9) == 0) {
2231 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
2232 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
2240 // some functions check action_service and must be cleared at end -- zzz
2241 nvram_set("action_service", "");
2243 // Force recheck in 500 msec
2244 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2247 static void do_service(const char *name
, const char *action
, int user
)
2253 while (!nvram_match("action_service", "")) {
2258 else if (--n
< 0) break;
2262 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2263 nvram_set("action_service", s
);
2267 while (nvram_match("action_service", s
)) {
2279 int service_main(int argc
, char *argv
[])
2281 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
2282 do_service(argv
[1], argv
[2], 1);
2283 printf("\nDone.\n");
2287 void start_service(const char *name
)
2289 do_service(name
, "start", 0);
2292 void stop_service(const char *name
)
2294 do_service(name
, "stop", 0);
2298 void restart_service(const char *name)
2300 do_service(name, "restart", 0);