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/dnsmasq/hosts";
57 static const char dmdhcp
[] = "/etc/dnsmasq/dhcp";
58 static const char dmresolv
[] = "/etc/resolv.dnsmasq";
60 static pid_t pid_dnsmasq
= -1;
62 static int is_wet(int idx
, int unit
, int subunit
, void *param
)
64 return nvram_match(wl_nvname("mode", unit
, subunit
), "wet");
73 const char *router_ip
;
74 const char *lan_ifname
;
78 char *mac
, *ip
, *name
;
92 start_service("dnsmasq");
98 if (foreach_wif(1, NULL
, is_wet
)) return;
100 if ((f
= fopen("/etc/dnsmasq.conf", "w")) == NULL
) return;
102 lan_ifname
= nvram_safe_get("lan_ifname");
103 router_ip
= nvram_safe_get("lan_ipaddr");
104 strlcpy(lan
, router_ip
, sizeof(lan
));
105 if ((p
= strrchr(lan
, '.')) != NULL
) *(p
+ 1) = 0;
108 "pid-file=/var/run/dnsmasq.pid\n"
111 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
112 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
116 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
118 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
121 "resolv-file=%s\n" // the real stuff is here
122 "addn-hosts=%s\n" // directory with additional hosts files
123 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
124 "expand-hosts\n" // expand hostnames in hosts file
125 "min-port=%u\n", // min port used for random src port
126 dmresolv
, dmhosts
, dmdhcp
, n
);
127 do_dns
= nvram_match("dhcpd_dmdns", "1");
129 // DNS rebinding protection, will discard upstream RFC1918 responses
130 if (nvram_get_int("dns_norebind")) {
133 "rebind-localhost-ok\n");
134 // allow RFC1918 responses for server domain
135 switch (get_wan_proto()) {
137 nv
= nvram_get("pptp_server_ip");
140 nv
= nvram_get("l2tp_server_ip");
146 if (nv
&& *nv
) fprintf(f
, "rebind-domain-ok=%s\n", nv
);
149 for (n
= 0 ; n
< dns
->count
; ++n
) {
150 if (dns
->dns
[n
].port
!= 53) {
151 fprintf(f
, "server=%s#%u\n", inet_ntoa(dns
->dns
[n
].addr
), dns
->dns
[n
].port
);
156 do_dhcpd
= nvram_match("lan_proto", "dhcp");
158 dhcp_lease
= nvram_get_int("dhcp_lease");
159 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
161 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
162 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
163 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
166 // if not using dnsmasq for dns
168 if ((dns
->count
== 0) && (nvram_get_int("dhcpd_llndns"))) {
169 // no DNS might be temporary. use a low lease time to force clients to update.
171 strcpy(sdhcp_lease
, "2m");
175 // pass the dns directly
177 for (n
= 0 ; n
< dns
->count
; ++n
) {
178 if (dns
->dns
[n
].port
== 53) { // check: option 6 doesn't seem to support other ports
179 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
].addr
));
182 fprintf(f
, "dhcp-option=6%s\n", buf
);
186 if ((p
= nvram_get("dhcpd_startip")) && (*p
) && (e
= nvram_get("dhcpd_endip")) && (*e
)) {
187 fprintf(f
, "dhcp-range=%s,%s,%s,%dm\n", p
, e
, nvram_safe_get("lan_netmask"), dhcp_lease
);
191 dhcp_start
= nvram_get_int("dhcp_start");
192 dhcp_count
= nvram_get_int("dhcp_num");
193 fprintf(f
, "dhcp-range=%s%d,%s%d,%s,%dm\n",
194 lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get("lan_netmask"), dhcp_lease
);
198 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED
)) {
199 p
= nvram_safe_get("lan_gateway");
200 if ((*p
) && (strcmp(p
, "0.0.0.0") != 0)) nv
= p
;
203 n
= nvram_get_int("dhcpd_lmax");
205 "dhcp-option=3,%s\n" // gateway
206 "dhcp-lease-max=%d\n",
210 if (nvram_get_int("dhcpd_auth") >= 0) {
211 fprintf(f
, "dhcp-authoritative\n");
214 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
215 fprintf(f
, "dhcp-option=44,%s\n", nv
);
217 #ifdef TCONFIG_SAMBASRV
218 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
219 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
220 // Samba will serve as a WINS server
221 fprintf(f
, "dhcp-option=44,0.0.0.0\n");
227 fprintf(f
, "no-dhcp-interface=%s\n", lan_ifname
);
230 // write static lease entries & create hosts file
232 mkdir_if_none(dmhosts
);
233 snprintf(buf
, sizeof(buf
), "%s/hosts", dmhosts
);
234 if ((hf
= fopen(buf
, "w")) != NULL
) {
235 if (((nv
= nvram_get("wan_hostname")) != NULL
) && (*nv
))
236 fprintf(hf
, "%s %s\n", router_ip
, nv
);
237 #ifdef TCONFIG_SAMBASRV
238 else if (((nv
= nvram_get("lan_hostname")) != NULL
) && (*nv
))
239 fprintf(hf
, "%s %s\n", router_ip
, nv
);
241 p
= (char *)get_wanip();
242 if ((*p
== 0) || strcmp(p
, "0.0.0.0") == 0)
244 fprintf(hf
, "%s wan-ip\n", p
);
246 fprintf(hf
, "%s %s-wan\n", p
, nv
);
249 mkdir_if_none(dmdhcp
);
250 snprintf(buf
, sizeof(buf
), "%s/dhcp-hosts", dmdhcp
);
251 df
= fopen(buf
, "w");
253 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
254 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
255 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
256 p
= nvram_safe_get("dhcpd_static");
257 while ((e
= strchr(p
, '>')) != NULL
) {
268 if ((e
= strchr(buf
, '<')) == NULL
) continue;
273 if ((e
= strchr(ip
, '<')) == NULL
) continue;
275 if (strchr(ip
, '.') == NULL
) {
277 if ((ipn
<= 0) || (ipn
> 255)) continue;
278 sprintf(ipbuf
, "%s%d", lan
, ipn
);
282 if (inet_addr(ip
) == INADDR_NONE
) continue;
287 if ((hf
) && (*name
!= 0)) {
288 fprintf(hf
, "%s %s\n", ip
, name
);
291 if ((do_dhcpd
) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
293 fprintf(df
, "%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
295 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
304 #ifdef TCONFIG_OPENVPN
305 write_vpn_dnsmasq_config(f
);
308 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
310 fappend(f
, "/etc/dnsmasq.custom");
317 unlink("/etc/resolv.conf");
318 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
321 TRACE_PT("run dnsmasq\n");
323 // Default to some values we like, but allow the user to override them.
324 eval("dnsmasq", "-c", "1500", "--log-async");
326 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
333 void stop_dnsmasq(void)
338 stop_service("dnsmasq");
344 unlink("/etc/resolv.conf");
345 symlink(dmresolv
, "/etc/resolv.conf");
347 killall_tk("dnsmasq");
352 void clear_resolv(void)
354 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
358 static int write_ipv6_dns_servers(FILE *f
, const char *prefix
, char *dns
, const char *suffix
, int once
)
360 char p
[INET6_ADDRSTRLEN
+ 1], *next
= NULL
;
361 struct in6_addr addr
;
364 foreach(p
, dns
, next
) {
365 // verify that this is a valid IPv6 address
366 if (inet_pton(AF_INET6
, p
, &addr
) == 1) {
367 fprintf(f
, "%s%s%s", (once
&& cnt
) ? "" : prefix
, p
, suffix
);
376 void dns_to_resolv(void)
379 const dns_list_t
*dns
;
383 m
= umask(022); // 077 from pppoecd
384 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
385 // Check for VPN DNS entries
386 if (!write_vpn_resolv(f
)) {
388 if (write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
389 write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
391 dns
= get_dns(); // static buffer
392 if (dns
->count
== 0) {
393 // Put a pseudo DNS IP to trigger Connect On Demand
394 if (nvram_match("ppp_demand", "1")) {
395 switch (get_wan_proto()) {
399 fprintf(f
, "nameserver 1.1.1.1\n");
405 for (i
= 0; i
< dns
->count
; i
++) {
406 if (dns
->dns
[i
].port
== 53) { // resolv.conf doesn't allow for an alternate port
407 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
].addr
));
417 // -----------------------------------------------------------------------------
419 void start_httpd(void)
422 start_service("httpd");
432 void stop_httpd(void)
435 stop_service("httpd");
442 // -----------------------------------------------------------------------------
445 static void add_ip6_lanaddr(void)
447 char ip
[INET6_ADDRSTRLEN
+ 4];
450 p
= ipv6_router_address(NULL
);
452 snprintf(ip
, sizeof(ip
), "%s/%d", p
, nvram_get_int("ipv6_prefix_length") ? : 64);
453 eval("ip", "-6", "addr", "add", ip
, "dev", nvram_safe_get("lan_ifname"));
457 void start_ipv6_tunnel(void)
459 char ip
[INET6_ADDRSTRLEN
+ 4];
460 struct in_addr addr4
;
461 struct in6_addr addr
;
462 const char *wanip
, *mtu
, *tun_dev
;
465 service
= get_ipv6_service();
466 tun_dev
= get_wan6face();
468 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
471 if (service
== IPV6_ANYCAST_6TO4
)
472 snprintf(ip
, sizeof(ip
), "192.88.99.%d", nvram_get_int("ipv6_relay"));
474 strlcpy(ip
, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip
));
475 eval("ip", "tunnel", "add", (char *)tun_dev
, "mode", "sit",
477 "local", (char *)wanip
,
478 "ttl", nvram_safe_get("ipv6_tun_ttl"));
480 eval("ip", "link", "set", (char *)tun_dev
, "mtu", (char *)mtu
, "up");
481 nvram_set("ipv6_ifname", (char *)tun_dev
);
483 if (service
== IPV6_ANYCAST_6TO4
) {
486 memset(&addr
, 0, sizeof(addr
));
487 inet_aton(wanip
, &addr4
);
488 addr
.s6_addr16
[0] = htons(0x2002);
489 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
490 addr
.s6_addr16
[7] = htons(0x0001);
491 inet_ntop(AF_INET6
, &addr
, ip
, sizeof(ip
));
492 strncat(ip
, "/16", sizeof(ip
));
495 snprintf(ip
, sizeof(ip
), "%s/%d",
496 nvram_safe_get("ipv6_tun_addr"),
497 nvram_get_int("ipv6_tun_addrlen") ? : 64);
499 eval("ip", "addr", "add", ip
, "dev", (char *)tun_dev
);
500 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev
);
503 if (service
== IPV6_ANYCAST_6TO4
)
507 void stop_ipv6_tunnel(void)
509 eval("ip", "tunnel", "del", (char *)get_wan6face());
510 if (get_ipv6_service() == IPV6_ANYCAST_6TO4
) {
511 // get rid of old IPv6 address from lan iface
512 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
517 static pid_t pid_radvd
= -1;
519 void start_radvd(void)
522 char *prefix
, *ip
, *mtu
;
524 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
525 int pid
, argc
, service
, cnt
;
528 start_service("radvd");
534 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
535 service
= get_ipv6_service();
536 do_6to4
= (service
== IPV6_ANYCAST_6TO4
);
540 case IPV6_NATIVE_DHCP
:
543 case IPV6_ANYCAST_6TO4
:
545 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
548 prefix
= do_6to4
? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
551 if (!(*prefix
)) prefix
= "::";
554 if ((f
= fopen("/etc/radvd.conf", "w")) == NULL
) return;
556 ip
= (char *)ipv6_router_address(NULL
);
557 do_dns
= (*ip
) && nvram_match("dhcpd_dmdns", "1");
562 " IgnoreIfMissing on;\n"
563 " AdvSendAdvert on;\n"
564 " MaxRtrAdvInterval 60;\n"
565 " AdvHomeAgentFlag off;\n"
566 " AdvManagedFlag off;\n"
571 " AdvAutonomous on;\n"
575 nvram_safe_get("lan_ifname"),
576 mtu
? " AdvLinkMTU " : "", mtu
? : "", mtu
? ";\n" : "",
578 do_6to4
? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
579 do_6to4
? " Base6to4Interface " : "",
580 do_6to4
? get_wanface() : "",
581 do_6to4
? ";\n" : "");
584 fprintf(f
, " RDNSS %s {};\n", ip
);
587 cnt
= write_ipv6_dns_servers(f
, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
588 if (cnt
== 0 || nvram_get_int("dns_addget"))
589 cnt
+= write_ipv6_dns_servers(f
, (cnt
) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
590 if (cnt
) fprintf(f
, "{};\n");
594 "};\n"); // close "interface" section
599 if (nvram_get_int("debug_ipv6")) {
604 _eval(argv
, NULL
, 0, &pid
);
606 if (!nvram_contains_word("debug_norestart", "radvd")) {
612 void stop_radvd(void)
615 stop_service("radvd");
623 void start_ipv6(void)
627 service
= get_ipv6_service();
630 // Check if turned on
637 case IPV6_NATIVE_DHCP
:
638 case IPV6_ANYCAST_6TO4
:
639 nvram_set("ipv6_rtr_addr", "");
640 nvram_set("ipv6_prefix", "");
644 if (service
!= IPV6_DISABLED
) {
645 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
646 accept_ra(nvram_safe_get("lan_ifname"));
654 eval("ip", "-6", "addr", "flush", "scope", "global");
659 // -----------------------------------------------------------------------------
661 void start_upnp(void)
664 start_service("upnp");
668 if (get_wan_proto() == WP_DISABLED
) return;
674 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
675 mkdir("/etc/upnp", 0777);
676 if (f_exists("/etc/upnp/config.alt")) {
677 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
680 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
681 upnp_port
= nvram_get_int("upnp_port");
682 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
684 char *lanip
= nvram_safe_get("lan_ipaddr");
685 char *lanmask
= nvram_safe_get("lan_netmask");
689 "listening_ip=%s/%s\n"
694 "upnp_forward_chain=upnp\n"
695 "upnp_nat_chain=upnp\n"
696 "notify_interval=%d\n"
697 "system_uptime=yes\n"
703 (enable
& 1) ? "yes" : "no", // upnp enable
704 (enable
& 2) ? "yes" : "no", // natpmp enable
705 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
706 nvram_get_int("upnp_ssdp_interval")
709 if (nvram_get_int("upnp_clean")) {
710 int interval
= nvram_get_int("upnp_clean_interval");
711 if (interval
< 60) interval
= 60;
713 "clean_ruleset_interval=%d\n"
714 "clean_ruleset_threshold=%d\n",
716 nvram_get_int("upnp_clean_threshold")
720 fprintf(f
,"clean_ruleset_interval=0\n");
722 if (nvram_match("upnp_mnp", "1")) {
723 int https
= nvram_get_int("https_enable");
724 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
725 https
? "s" : "", lanip
,
726 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
729 // Empty parameters are not included into XML service description
730 fprintf(f
, "presentation_url=\n");
734 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
735 fprintf(f
, "uuid=%s\n", uuid
);
738 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
739 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
740 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
741 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
743 "allow %d-%d %s/%s %d-%d\n",
750 // by default allow only redirection of ports above 1024
751 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
754 fappend(f
, "/jffs/upnpconfig.custom");
755 fappend(f
, "/etc/upnp/config.custom");
756 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
759 xstart("miniupnpd", "-f", "/etc/upnp/config");
768 stop_service("upnp");
772 killall_tk("miniupnpd");
775 // -----------------------------------------------------------------------------
777 static pid_t pid_crond
= -1;
779 void start_cron(void)
783 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
784 if (!nvram_contains_word("debug_norestart", "crond")) {
795 // -----------------------------------------------------------------------------
798 static pid_t pid_hotplug2
= -1;
800 void start_hotplug2()
804 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
805 xstart("hotplug2", "--persistent", "--no-coldplug");
806 // FIXME: Don't remember exactly why I put "sleep" here -
807 // but it was not for a race with check_services()... - TB
810 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
815 void stop_hotplug2(void)
818 killall_tk("hotplug2");
822 // -----------------------------------------------------------------------------
824 // Written by Sparq in 2002/07/16
825 void start_zebra(void)
829 start_service("zebra");
835 char *lan_tx
= nvram_safe_get("dr_lan_tx");
836 char *lan_rx
= nvram_safe_get("dr_lan_rx");
837 char *wan_tx
= nvram_safe_get("dr_wan_tx");
838 char *wan_rx
= nvram_safe_get("dr_wan_rx");
840 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
845 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
850 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
851 char *lan_ifname
= nvram_safe_get("lan_ifname");
852 char *wan_ifname
= nvram_safe_get("wan_ifname");
854 fprintf(fp
, "router rip\n");
855 fprintf(fp
, "network %s\n", lan_ifname
);
856 fprintf(fp
, "network %s\n", wan_ifname
);
857 fprintf(fp
, "redistribute connected\n");
858 //fprintf(fp, "redistribute static\n");
860 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
861 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
863 fprintf(fp
, "interface %s\n", lan_ifname
);
864 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
865 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
867 fprintf(fp
, "interface %s\n", wan_ifname
);
868 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
869 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
871 fprintf(fp
, "router rip\n");
872 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
873 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
874 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
875 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
876 fprintf(fp
, "access-list private deny any\n");
878 //fprintf(fp, "debug rip events\n");
879 //fprintf(fp, "log file /etc/ripd.log\n");
883 xstart("zebra", "-d");
884 xstart("ripd", "-d");
888 void stop_zebra(void)
892 stop_service("zebra");
896 killall("zebra", SIGTERM
);
897 killall("ripd", SIGTERM
);
899 unlink("/etc/zebra.conf");
900 unlink("/etc/ripd.conf");
904 // -----------------------------------------------------------------------------
906 void start_syslog(void)
916 char *rot_siz
= "50";
922 if (nvram_match("log_remote", "1")) {
923 nv
= nvram_safe_get("log_remoteip");
925 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
931 if (nvram_match("log_file", "1")) {
934 // log to custom path - shibby
935 if (nvram_match("log_file_custom", "1")) {
936 log_file_path
= nvram_safe_get("log_file_path");
938 argv
[argc
++] = "5000";
942 argv
[argc
++] = log_file_path
;
943 remove("/var/log/messages");
944 symlink(log_file_path
, "/var/log/messages");
948 /* Read options: rotate_size(kb) num_backups logfilename.
949 * Ignore these settings and use defaults if the logfile cannot be written to.
951 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
952 if ((nv
= strchr(cfg
, '\n')))
955 if ((nv
= strtok(cfg
, " \t"))) {
960 if ((nv
= strtok(NULL
, " \t")))
963 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
964 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
975 if (nvram_match("log_file_custom", "0")) {
977 argv
[argc
++] = rot_siz
;
978 remove("/var/log/messages");
982 if (isdigit(*b_opt
)) {
984 argv
[argc
++] = b_opt
;
990 _eval(argv
, NULL
, 0, NULL
);
994 _eval(argv
, NULL
, 0, NULL
);
996 // used to be available in syslogd -m
997 n
= nvram_get_int("log_mark");
1001 sprintf(rem
, "*/%d * * * *", n
);
1002 else if (n
< 60 * 24)
1003 sprintf(rem
, "0 */%d * * *", n
/ 60);
1005 sprintf(rem
, "0 0 */%d * *", n
/ (60 * 24));
1006 sprintf(s
, "%s logger -p syslog.info -- -- MARK --", rem
);
1007 eval("cru", "a", "syslogdmark", s
);
1010 eval("cru", "d", "syslogdmark");
1015 void stop_syslog(void)
1017 killall("klogd", SIGTERM
);
1018 killall("syslogd", SIGTERM
);
1021 // -----------------------------------------------------------------------------
1023 static pid_t pid_igmp
= -1;
1025 void start_igmp_proxy(void)
1030 if (nvram_match("multicast_pass", "1")) {
1031 if (get_wan_proto() == WP_DISABLED
)
1034 if (f_exists("/etc/igmp.alt")) {
1035 eval("igmpproxy", "/etc/igmp.alt");
1037 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
1040 "phyint %s upstream\n"
1042 "phyint %s downstream ratelimit 0\n",
1043 nvram_safe_get("wan_ifname"),
1044 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
1045 nvram_safe_get("lan_ifname"));
1047 eval("igmpproxy", "/etc/igmp.conf");
1052 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1058 void stop_igmp_proxy(void)
1061 killall_tk("igmpproxy");
1065 // -----------------------------------------------------------------------------
1069 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
1072 void start_ntpc(void)
1078 if (nvram_get_int("ntp_updates") >= 0) {
1079 xstart("ntpsync", "--init");
1083 void stop_ntpc(void)
1085 killall("ntpsync", SIGTERM
);
1088 // -----------------------------------------------------------------------------
1090 static void stop_rstats(void)
1096 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1097 if (kill(pid
, SIGTERM
) != 0) break;
1102 static void start_rstats(int new)
1104 if (nvram_match("rstats_enable", "1")) {
1106 if (new) xstart("rstats", "--new");
1107 else xstart("rstats");
1111 // -----------------------------------------------------------------------------
1113 // !!TB - FTP Server
1116 static char *get_full_storage_path(char *val
)
1118 static char buf
[128];
1122 len
= sprintf(buf
, "%s", val
);
1124 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1126 if (len
> 1 && buf
[len
- 1] == '/')
1132 static char *nvram_storage_path(char *var
)
1134 char *val
= nvram_safe_get(var
);
1135 return get_full_storage_path(val
);
1138 char vsftpd_conf
[] = "/etc/vsftpd.conf";
1139 char vsftpd_users
[] = "/etc/vsftpd.users";
1140 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
1142 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1144 static void start_ftpd(void)
1150 char *user
, *pass
, *rights
;
1152 if (getpid() != 1) {
1153 start_service("ftpd");
1157 if (!nvram_get_int("ftp_enable")) return;
1159 mkdir_if_none(vsftpd_users
);
1160 mkdir_if_none("/var/run/vsftpd");
1162 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
1165 if (nvram_get_int("ftp_super"))
1168 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1169 if ((f
= fopen(tmp
, "w")))
1172 "dirlist_enable=yes\n"
1173 "write_enable=yes\n"
1174 "download_enable=yes\n");
1179 #ifdef TCONFIG_SAMBASRV
1180 if (nvram_match("smbd_cset", "utf8"))
1181 fprintf(fp
, "utf8=yes\n");
1184 if (nvram_invmatch("ftp_anonymous", "0"))
1187 "anon_allow_writable_root=yes\n"
1188 "anon_world_readable_only=no\n"
1189 "anon_umask=022\n");
1192 sprintf(tmp
, "%s/ftp", vsftpd_users
);
1193 if ((f
= fopen(tmp
, "w")))
1195 if (nvram_match("ftp_dirlist", "0"))
1196 fprintf(f
, "dirlist_enable=yes\n");
1197 if (nvram_match("ftp_anonymous", "1") ||
1198 nvram_match("ftp_anonymous", "3"))
1199 fprintf(f
, "write_enable=yes\n");
1200 if (nvram_match("ftp_anonymous", "1") ||
1201 nvram_match("ftp_anonymous", "2"))
1202 fprintf(f
, "download_enable=yes\n");
1205 if (nvram_match("ftp_anonymous", "1") ||
1206 nvram_match("ftp_anonymous", "3"))
1208 "anon_upload_enable=yes\n"
1209 "anon_mkdir_write_enable=yes\n"
1210 "anon_other_write_enable=yes\n");
1212 fprintf(fp
, "anonymous_enable=no\n");
1216 "dirmessage_enable=yes\n"
1217 "download_enable=no\n"
1218 "dirlist_enable=no\n"
1220 "syslog_enable=yes\n"
1221 "local_enable=yes\n"
1224 "chroot_local_user=yes\n"
1226 "log_ftp_protocol=%s\n"
1227 "user_config_dir=%s\n"
1235 "max_login_fails=1\n"
1236 "idle_session_timeout=%s\n"
1238 "anon_max_rate=%d\n"
1239 "local_max_rate=%d\n"
1241 nvram_get_int("log_ftp") ? "yes" : "no",
1242 vsftpd_users
, vsftpd_passwd
,
1244 ipv6_enabled() ? "_ipv6" : "",
1248 nvram_get("ftp_port") ? : "21",
1249 nvram_get_int("ftp_max"),
1250 nvram_get_int("ftp_ipmax"),
1251 nvram_get("ftp_staytimeout") ? : "300",
1252 nvram_get_int("ftp_anonrate") * 1024,
1253 nvram_get_int("ftp_rate") * 1024,
1254 nvram_safe_get("ftp_custom"));
1258 /* prepare passwd file and default users */
1259 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
1262 if (((user
= nvram_get("http_username")) == NULL
) || (*user
== 0)) user
= "admin";
1263 if (((pass
= nvram_get("http_passwd")) == NULL
) || (*pass
== 0)) pass
= "admin";
1265 fprintf(fp
, /* anonymous, admin, nobody */
1266 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1267 "%s:%s:0:0:root:/:/sbin/nologin\n"
1268 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1269 nvram_storage_path("ftp_anonroot"), user
,
1270 nvram_get_int("ftp_super") ? crypt(pass
, "$1$") : "x",
1273 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1276 username<password<rights
1284 while ((q
= strsep(&p
, ">")) != NULL
) {
1285 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
1286 if (!user
|| !pass
) continue;
1289 if (strncmp(rights
, "Private", 7) == 0)
1291 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
1295 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
1297 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1298 user
, crypt(pass
, "$1$"), user
, tmp
);
1301 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1302 if ((f
= fopen(tmp
, "w")))
1305 if (nvram_invmatch("ftp_dirlist", "1"))
1306 strcat(tmp
, "dirlist_enable=yes\n");
1307 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
1308 strcat(tmp
, "download_enable=yes\n");
1309 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
1310 strcat(tmp
, "write_enable=yes\n");
1320 killall("vsftpd", SIGHUP
);
1322 /* start vsftpd if it's not already running */
1323 if (pidof("vsftpd") <= 0)
1327 static void stop_ftpd(void)
1329 if (getpid() != 1) {
1330 stop_service("ftpd");
1334 killall_tk("vsftpd");
1335 unlink(vsftpd_passwd
);
1336 unlink(vsftpd_conf
);
1337 eval("rm", "-rf", vsftpd_users
);
1339 #endif // TCONFIG_FTP
1341 // -----------------------------------------------------------------------------
1345 #ifdef TCONFIG_SAMBASRV
1346 static void kill_samba(int sig
)
1348 if (sig
== SIGTERM
) {
1353 killall("smbd", sig
);
1354 killall("nmbd", sig
);
1358 static void start_samba(void)
1367 if (getpid() != 1) {
1368 start_service("smbd");
1372 mode
= nvram_get_int("smbd_enable");
1373 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1376 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1379 fprintf(fp
, "[global]\n"
1380 " interfaces = %s\n"
1381 " bind interfaces only = yes\n"
1383 " netbios name = %s\n"
1384 " server string = %s\n"
1385 " guest account = nobody\n"
1386 " security = user\n"
1389 " guest only = no\n"
1390 " browseable = yes\n"
1391 " syslog only = yes\n"
1392 " timestamp logs = no\n"
1394 " encrypt passwords = yes\n"
1395 " preserve case = yes\n"
1396 " short preserve case = yes\n",
1397 nvram_safe_get("lan_ifname"),
1398 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1399 nvram_safe_get("lan_hostname"),
1400 nvram_get("router_name") ? : "Tomato",
1401 mode
== 2 ? "" : "map to guest = Bad User",
1402 mode
== 2 ? "no" : "yes" // guest ok
1405 if (nvram_get_int("smbd_wins")) {
1406 nv
= nvram_safe_get("wan_wins");
1407 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1408 fprintf(fp
, " wins support = yes\n");
1412 if (nvram_get_int("smbd_master")) {
1414 " domain master = yes\n"
1415 " local master = yes\n"
1416 " preferred master = yes\n"
1417 " os level = 65\n");
1420 nv
= nvram_safe_get("smbd_cpage");
1422 #ifndef TCONFIG_SAMBA3
1423 fprintf(fp
, " client code page = %s\n", nv
);
1425 sprintf(nlsmod
, "nls_cp%s", nv
);
1427 nv
= nvram_safe_get("smbd_nlsmod");
1428 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1432 nvram_set("smbd_nlsmod", nlsmod
);
1435 #ifndef TCONFIG_SAMBA3
1436 if (nvram_match("smbd_cset", "utf8"))
1437 fprintf(fp
, " coding system = utf8\n");
1438 else if (nvram_invmatch("smbd_cset", ""))
1439 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1442 nv
= nvram_safe_get("smbd_custom");
1443 /* add socket options unless overriden by the user */
1444 if (strstr(nv
, "socket options") == NULL
) {
1445 fprintf(fp
, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1447 fprintf(fp
, "%s\n\n", nv
);
1449 /* configure shares */
1453 char *name
, *path
, *comment
, *writeable
, *hidden
;
1456 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1458 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1461 while ((q
= strsep(&p
, ">")) != NULL
) {
1462 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1463 if (!path
|| !name
) continue;
1466 fprintf(fp
, "\n[%s]\n", name
);
1469 fprintf(fp
, " path = %s\n", path
);
1472 if (!strcmp(writeable
, "1"))
1473 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1474 if (!strcmp(hidden
, "1"))
1475 fprintf(fp
, " browseable = no\n");
1479 fprintf(fp
, " comment = %s\n", comment
);
1486 /* Share every mountpoint below MOUNT_ROOT */
1487 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1488 while ((dp
= readdir(dir
))) {
1489 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1495 /* Only if is a directory and is mounted */
1496 sprintf(path
, "%s/%s", MOUNT_ROOT
, dp
->d_name
);
1497 sb
.st_mode
= S_IFDIR
; /* failsafe */
1499 if (!S_ISDIR(sb
.st_mode
))
1502 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1505 thisdev
= sb
.st_dev
;
1507 ++sb
.st_dev
; /* failsafe */
1509 if (thisdev
== sb
.st_dev
)
1512 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1513 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1514 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1515 if (nvram_match("smbd_autoshare", "3")) // Hidden
1516 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1517 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1518 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1519 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1525 if (dir
) closedir(dir
);
1528 /* by default share MOUNT_ROOT as read-only */
1529 fprintf(fp
, "\n[share]\n"
1537 mkdir_if_none("/var/run/samba");
1538 mkdir_if_none("/etc/samba");
1540 /* write smbpasswd */
1541 #ifdef TCONFIG_SAMBA3
1542 eval("smbpasswd", "nobody", "\"\"");
1544 eval("smbpasswd", "-a", "nobody", "\"\"");
1548 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1550 #ifdef TCONFIG_SAMBA3
1551 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1553 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1558 int ret1
= 0, ret2
= 0;
1559 /* start samba if it's not already running */
1560 if (pidof("nmbd") <= 0)
1561 ret1
= xstart("nmbd", "-D");
1562 if (pidof("smbd") <= 0)
1563 ret2
= xstart("smbd", "-D");
1565 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1568 static void stop_samba(void)
1570 if (getpid() != 1) {
1571 stop_service("smbd");
1575 kill_samba(SIGTERM
);
1577 unlink("/var/log/smb");
1578 unlink("/var/log/nmb");
1579 eval("rm", "-rf", "/var/run/samba");
1581 #endif // TCONFIG_SAMBASRV
1583 #ifdef TCONFIG_MEDIA_SERVER
1584 #define MEDIA_SERVER_APP "minidlna"
1586 static void start_media_server(void)
1589 int port
, pid
, https
;
1591 char *argv
[] = { MEDIA_SERVER_APP
, "-f", "/etc/"MEDIA_SERVER_APP
".conf", "-R", NULL
};
1592 static int once
= 1;
1594 if (getpid() != 1) {
1595 start_service("media");
1599 if (nvram_get_int("ms_sas") == 0)
1602 if (nvram_get_int("ms_enable") != 0) {
1603 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
1607 nvram_unset("ms_rescan");
1609 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
1610 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
1613 if ((f
= fopen(argv
[2], "w")) != NULL
) {
1614 port
= nvram_get_int("ms_port");
1615 https
= nvram_get_int("https_enable");
1616 dbdir
= nvram_safe_get("ms_dbdir");
1617 if (!(*dbdir
)) dbdir
= NULL
;
1618 mkdir_if_none(dbdir
? : "/var/run/"MEDIA_SERVER_APP
);
1621 "network_interface=%s\n"
1623 "friendly_name=%s\n"
1627 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1629 "notify_interval=600\n"
1630 "album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg\n"
1632 nvram_safe_get("lan_ifname"),
1633 (port
< 0) || (port
>= 0xffff) ? 0 : port
,
1634 nvram_get("router_name") ? : "Tomato",
1635 dbdir
? : "/var/run/"MEDIA_SERVER_APP
,
1636 nvram_get_int("ms_tivo") ? "yes" : "no",
1637 nvram_get_int("ms_stdlna") ? "yes" : "no",
1638 https
? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https
? "https_lanport" : "http_lanport")
1641 // media directories
1643 char *path
, *restrict
;
1645 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
1646 /* path<restrict[A|V|P|] */
1649 while ((q
= strsep(&p
, ">")) != NULL
) {
1650 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
1652 fprintf(f
, "media_dir=%s%s%s\n",
1653 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
1662 /* start media server if it's not already running */
1663 if (pidof(MEDIA_SERVER_APP
) <= 0) {
1664 if ((_eval(argv
, NULL
, 0, &pid
) == 0) && (once
)) {
1665 /* If we started the media server successfully, wait 1 sec
1666 * to let it die if it can't open the database file.
1667 * If it's still alive after that, assume it's running and
1668 * disable forced once-after-reboot rescan.
1671 if (pidof(MEDIA_SERVER_APP
) > 0)
1678 static void stop_media_server(void)
1680 if (getpid() != 1) {
1681 stop_service("media");
1685 killall_tk(MEDIA_SERVER_APP
);
1687 #endif // TCONFIG_MEDIA_SERVER
1690 static void start_nas_services(void)
1692 if (getpid() != 1) {
1693 start_service("usbapps");
1697 #ifdef TCONFIG_SAMBASRV
1703 #ifdef TCONFIG_MEDIA_SERVER
1704 start_media_server();
1708 static void stop_nas_services(void)
1710 if (getpid() != 1) {
1711 stop_service("usbapps");
1715 #ifdef TCONFIG_MEDIA_SERVER
1716 stop_media_server();
1721 #ifdef TCONFIG_SAMBASRV
1726 void restart_nas_services(int stop
, int start
)
1728 int fd
= file_lock("usb");
1729 /* restart all NAS applications */
1731 stop_nas_services();
1733 start_nas_services();
1736 #endif // TCONFIG_USB
1738 // -----------------------------------------------------------------------------
1740 /* -1 = Don't check for this program, it is not expected to be running.
1741 * Other = This program has been started and should be kept running. If no
1742 * process with the name is running, call func to restart it.
1743 * Note: At startup, dnsmasq forks a short-lived child which forks a
1744 * long-lived (grand)child. The parents terminate.
1745 * Many daemons use this technique.
1747 static void _check(pid_t pid
, const char *name
, void (*func
)(void))
1749 if (pid
== -1) return;
1751 if (pidof(name
) > 0) return;
1753 syslog(LOG_DEBUG
, "%s terminated unexpectedly, restarting.\n", name
);
1756 // Force recheck in 500 msec
1757 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
1760 void check_services(void)
1762 TRACE_PT("keep alive\n");
1764 // Periodically reap any zombies
1765 setitimer(ITIMER_REAL
, &zombie_tv
, NULL
);
1768 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
1770 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1771 _check(pid_crond
, "crond", start_cron
);
1772 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
1774 _check(pid_radvd
, "radvd", start_radvd
);
1778 // -----------------------------------------------------------------------------
1780 void start_services(void)
1782 static int once
= 1;
1787 if (nvram_get_int("telnetd_eas")) start_telnetd();
1788 if (nvram_get_int("sshd_eas")) start_sshd();
1802 /* note: starting radvd here might be too early in case of
1803 * DHCPv6 or 6to4 because we won't have received a prefix and
1804 * so it will disable advertisements. To restart them, we have
1805 * to send radvd a SIGHUP, or restart it.
1809 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1812 void stop_services(void)
1816 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1832 // -----------------------------------------------------------------------------
1834 /* nvram "action_service" is: "service-action[-modifier]"
1835 * action is something like "stop" or "start" or "restart"
1836 * optional modifier is "c" for the "service" command-line command
1838 void exec_service(void)
1840 const int A_START
= 1;
1841 const int A_STOP
= 2;
1842 const int A_RESTART
= 1|2;
1851 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1855 act
= strsep(&next
, ",");
1856 service
= strsep(&act
, "-");
1862 strsep(&modifier
, "-");
1864 TRACE_PT("service=%s action=%s modifier=%s\n", service
, act
, modifier
? : "");
1866 if (strcmp(act
, "start") == 0) action
= A_START
;
1867 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1868 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1870 user
= (modifier
!= NULL
&& *modifier
== 'c');
1872 if (strcmp(service
, "dhcpc") == 0) {
1873 if (action
& A_STOP
) stop_dhcpc();
1874 if (action
& A_START
) start_dhcpc();
1878 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1879 if (action
& A_STOP
) stop_dnsmasq();
1880 if (action
& A_START
) {
1887 if (strcmp(service
, "firewall") == 0) {
1888 if (action
& A_STOP
) {
1892 if (action
& A_START
) {
1899 if (strcmp(service
, "restrict") == 0) {
1900 if (action
& A_STOP
) {
1904 if (action
& A_START
) {
1905 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1910 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1912 if (nvram_get_int("rrules_radio") < 0) {
1913 eval("radio", "on");
1920 if (strcmp(service
, "qos") == 0) {
1921 if (action
& A_STOP
) {
1925 stop_firewall(); start_firewall(); // always restarted
1926 if (action
& A_START
) {
1929 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1934 if (strcmp(service
, "qoslimit") == 0) {
1935 if (action
& A_STOP
) {
1939 stop_firewall(); start_firewall(); // always restarted
1940 if (action
& A_START
) {
1948 if (strcmp(service
, "arpbind") == 0) {
1949 if (action
& A_STOP
) stop_arpbind();
1950 if (action
& A_START
) stop_arpbind();
1954 if (strcmp(service
, "cmon") == 0) { // TOASTMAN CLIENT MONITOR - EXPERIMENTAL
1955 if (action
& A_STOP
) {
1958 if (action
& A_START
) {
1964 if (strcmp(service
, "upnp") == 0) {
1965 if (action
& A_STOP
) {
1969 stop_firewall(); start_firewall(); // always restarted
1970 if (action
& A_START
) {
1977 if (strcmp(service
, "telnetd") == 0) {
1978 if (action
& A_STOP
) stop_telnetd();
1979 if (action
& A_START
) start_telnetd();
1983 if (strcmp(service
, "sshd") == 0) {
1984 if (action
& A_STOP
) stop_sshd();
1985 if (action
& A_START
) start_sshd();
1989 if (strcmp(service
, "httpd") == 0) {
1990 if (action
& A_STOP
) stop_httpd();
1991 if (action
& A_START
) start_httpd();
1996 if (strcmp(service
, "ipv6") == 0) {
1997 if (action
& A_STOP
) {
2001 if (action
& A_START
) {
2008 if (strcmp(service
, "radvd") == 0) {
2009 if (action
& A_STOP
) {
2012 if (action
& A_START
) {
2018 if (strncmp(service
, "dhcp6", 5) == 0) {
2019 if (action
& A_STOP
) {
2022 if (action
& A_START
) {
2029 if (strcmp(service
, "admin") == 0) {
2030 if (action
& A_STOP
) {
2035 stop_firewall(); start_firewall(); // always restarted
2036 if (action
& A_START
) {
2039 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2040 if (nvram_match("sshd_eas", "1")) start_sshd();
2045 if (strcmp(service
, "ddns") == 0) {
2046 if (action
& A_STOP
) stop_ddns();
2047 if (action
& A_START
) start_ddns();
2051 if (strcmp(service
, "ntpc") == 0) {
2052 if (action
& A_STOP
) stop_ntpc();
2053 if (action
& A_START
) start_ntpc();
2057 if (strcmp(service
, "logging") == 0) {
2058 if (action
& A_STOP
) {
2062 if (action
& A_START
) {
2067 // always restarted except from "service" command
2068 stop_cron(); start_cron();
2069 stop_firewall(); start_firewall();
2075 if (strcmp(service
, "crond") == 0) {
2076 if (action
& A_STOP
) {
2079 if (action
& A_START
) {
2086 if (strncmp(service
, "hotplug", 7) == 0) {
2087 if (action
& A_STOP
) {
2090 if (action
& A_START
) {
2097 if (strcmp(service
, "upgrade") == 0) {
2098 if (action
& A_START
) {
2103 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2111 killall("rstats", SIGTERM
);
2112 killall("buttons", SIGTERM
);
2114 remove_storage_main(1); // !!TB - USB Support
2115 stop_usb(); // !!TB - USB Support
2121 if (strcmp(service
, "cifs") == 0) {
2122 if (action
& A_STOP
) stop_cifs();
2123 if (action
& A_START
) start_cifs();
2128 #ifdef TCONFIG_JFFS2
2129 if (strncmp(service
, "jffs", 4) == 0) {
2130 if (action
& A_STOP
) stop_jffs2();
2131 if (action
& A_START
) start_jffs2();
2136 if (strcmp(service
, "zebra") == 0) {
2137 if (action
& A_STOP
) stop_zebra();
2138 if (action
& A_START
) start_zebra();
2142 if (strcmp(service
, "routing") == 0) {
2143 if (action
& A_STOP
) {
2145 do_static_routes(0); // remove old '_saved'
2146 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2150 if (action
& A_START
) {
2151 do_static_routes(1); // add new
2153 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2158 if (strcmp(service
, "ctnf") == 0) {
2159 if (action
& A_START
) {
2167 if (strcmp(service
, "wan") == 0) {
2168 if (action
& A_STOP
) {
2172 if (action
& A_START
) {
2173 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2181 if (strcmp(service
, "net") == 0) {
2182 if (action
& A_STOP
) {
2184 stop_nas_services();
2196 if (action
& A_START
) {
2208 start_nas_services();
2214 if (strcmp(service
, "nas") == 0) {
2215 if (action
& A_STOP
) {
2218 if (action
& A_START
) {
2225 if (strcmp(service
, "rstats") == 0) {
2226 if (action
& A_STOP
) stop_rstats();
2227 if (action
& A_START
) start_rstats(0);
2231 if (strcmp(service
, "rstatsnew") == 0) {
2232 if (action
& A_STOP
) stop_rstats();
2233 if (action
& A_START
) start_rstats(1);
2237 if (strcmp(service
, "sched") == 0) {
2238 if (action
& A_STOP
) stop_sched();
2239 if (action
& A_START
) start_sched();
2244 // !!TB - USB Support
2245 if (strcmp(service
, "usb") == 0) {
2246 if (action
& A_STOP
) stop_usb();
2247 if (action
& A_START
) {
2249 // restart Samba and ftp since they may be killed by stop_usb()
2250 restart_nas_services(0, 1);
2251 // remount all partitions by simulating hotplug event
2252 add_remove_usbhost("-1", 1);
2257 if (strcmp(service
, "usbapps") == 0) {
2258 if (action
& A_STOP
) stop_nas_services();
2259 if (action
& A_START
) start_nas_services();
2265 // !!TB - FTP Server
2266 if (strcmp(service
, "ftpd") == 0) {
2267 if (action
& A_STOP
) stop_ftpd();
2271 if (action
& A_START
) start_ftpd();
2276 #ifdef TCONFIG_MEDIA_SERVER
2277 if (strcmp(service
, "media") == 0 || strcmp(service
, "dlna") == 0) {
2278 if (action
& A_STOP
) stop_media_server();
2279 if (action
& A_START
) start_media_server();
2284 #ifdef TCONFIG_SAMBASRV
2286 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2287 if (action
& A_STOP
) stop_samba();
2288 if (action
& A_START
) {
2298 #ifdef TCONFIG_OPENVPN
2299 if (strncmp(service
, "vpnclient", 9) == 0) {
2300 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
2301 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
2305 if (strncmp(service
, "vpnserver", 9) == 0) {
2306 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
2307 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
2315 // some functions check action_service and must be cleared at end -- zzz
2316 nvram_set("action_service", "");
2318 // Force recheck in 500 msec
2319 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2322 static void do_service(const char *name
, const char *action
, int user
)
2328 while (!nvram_match("action_service", "")) {
2333 else if (--n
< 0) break;
2337 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2338 nvram_set("action_service", s
);
2342 while (nvram_match("action_service", s
)) {
2354 int service_main(int argc
, char *argv
[])
2356 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
2357 do_service(argv
[1], argv
[2], 1);
2358 printf("\nDone.\n");
2362 void start_service(const char *name
)
2364 do_service(name
, "start", 0);
2367 void stop_service(const char *name
)
2369 do_service(name
, "stop", 0);
2373 void restart_service(const char *name)
2375 do_service(name, "restart", 0);