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 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
50 // Pop an alarm to recheck pids in 500 msec.
51 static const struct itimerval pop_tv
= { {0,0}, {0, 500 * 1000} };
53 // Pop an alarm to reap zombies.
54 static const struct itimerval zombie_tv
= { {0,0}, {307, 0} };
56 // -----------------------------------------------------------------------------
58 static const char dmhosts
[] = "/etc/hosts.dnsmasq";
59 static const char dmresolv
[] = "/etc/resolv.dnsmasq";
61 static pid_t pid_dnsmasq
= -1;
63 static int is_wet(int idx
, int unit
, int subunit
, void *param
)
65 return nvram_match(wl_nvname("mode", unit
, subunit
), "wet");
74 const char *router_ip
;
75 // const char *lan_ifname;
79 char *mac
, *ip
, *name
;
93 start_service("dnsmasq");
99 if (foreach_wif(1, NULL
, is_wet
)) return;
101 if ((f
= fopen("/etc/dnsmasq.conf", "w")) == NULL
) return;
103 // lan_ifname = nvram_safe_get("lan_ifname");
104 router_ip
= nvram_safe_get("lan_ipaddr");
105 // strlcpy(lan, router_ip, sizeof(lan));
106 // if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
109 "pid-file=/var/run/dnsmasq.pid\n");
112 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
113 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
117 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
119 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
122 "resolv-file=%s\n" // the real stuff is here
123 "addn-hosts=%s\n" // "
124 "expand-hosts\n" // expand hostnames in hosts file
125 "min-port=%u\n", // min port used for random src port
126 dmresolv
, dmhosts
, 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
);
160 for(br
=0 ; br
<4 ; br
++) {
161 char bridge
[2] = "0";
169 strcat(tmp
, "_proto");
170 do_dhcpd
= nvram_match(tmp
, "dhcp");
171 // do_dhcpd = nvram_match("lan_proto", "dhcp");
175 strcat(tmp
, "_ipaddr");
176 router_ip
= nvram_safe_get(tmp
);
177 strlcpy(lan
, router_ip
, sizeof(lan
));
178 if ((p
= strrchr(lan
, '.')) != NULL
) *(p
+ 1) = 0;
182 strcat(tmp
, "_ifname");
186 nvram_safe_get(tmp
));
190 strcat(tmp
, "_lease");
191 // dhcp_lease = nvram_get_int("dhcp_lease");
192 dhcp_lease
= nvram_get_int(tmp
);
193 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
195 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
196 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
197 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
200 // if not using dnsmasq for dns
202 if ((dns
->count
== 0) && (nvram_get_int("dhcpd_llndns"))) {
203 // no DNS might be temporary. use a low lease time to force clients to update.
205 strcpy(sdhcp_lease
, "2m");
209 // pass the dns directly
211 for (n
= 0 ; n
< dns
->count
; ++n
) {
212 if (dns
->dns
[n
].port
== 53) { // check: option 6 doesn't seem to support other ports
213 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
].addr
));
216 fprintf(f
, "dhcp-option=6%s\n", buf
);
222 strcat(tmp
, "_startip");
223 strcpy(tmp2
,"dhcpd");
225 strcat(tmp2
, "_endip");
228 strcat(tmp3
, "_netmask");
229 // if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
230 // fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
231 if ((p
= nvram_get(tmp
)) && (*p
) && (e
= nvram_get(tmp2
)) && (*e
)) {
232 fprintf(f
, "dhcp-range=%s,%s,%s,%dm\n", p
, e
, nvram_safe_get(tmp3
), dhcp_lease
);
236 // dhcp_start = nvram_get_int("dhcp_start");
237 // dhcp_count = nvram_get_int("dhcp_num");
240 strcat(tmp
, "_start");
243 strcat(tmp2
, "_num");
246 strcat(tmp3
, "_netmask");
247 dhcp_start
= nvram_get_int(tmp
);
248 dhcp_count
= nvram_get_int(tmp2
);
249 fprintf(f
, "dhcp-range=%s%d,%s%d,%s,%dm\n",
250 // lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
251 lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get(tmp3
), dhcp_lease
);
256 strcat(tmp
, "_ipaddr");
257 nv
= nvram_safe_get(tmp
);
259 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED
)) {
260 p
= nvram_safe_get("lan_gateway");
261 if ((*p
) && (strcmp(p
, "0.0.0.0") != 0)) nv
= p
;
264 n
= nvram_get_int("dhcpd_lmax");
266 "dhcp-option=3,%s\n" // gateway
267 "dhcp-lease-max=%d\n",
271 if (nvram_get_int("dhcpd_auth") >= 0) {
272 fprintf(f
, "dhcp-authoritative\n");
275 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
276 fprintf(f
, "dhcp-option=44,%s\n", nv
);
278 #ifdef TCONFIG_SAMBASRV
279 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
280 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
281 // Samba will serve as a WINS server
282 fprintf(f
, "dhcp-option=44,0.0.0.0\n");
290 strcat(tmp
, "_ifname");
291 // fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
292 if (strcmp(nvram_safe_get(tmp
),"")!=0)
293 fprintf(f
, "no-dhcp-interface=%s\n", nvram_safe_get(tmp
));
296 // write static lease entries & create hosts file
298 if ((hf
= fopen(dmhosts
, "w")) != NULL
) {
299 if (((nv
= nvram_get("wan_hostname")) != NULL
) && (*nv
))
300 fprintf(hf
, "%s %s\n", router_ip
, nv
);
301 #ifdef TCONFIG_SAMBASRV
302 else if (((nv
= nvram_get("lan_hostname")) != NULL
) && (*nv
))
303 fprintf(hf
, "%s %s\n", router_ip
, nv
);
305 p
= (char *)get_wanip();
306 if ((*p
== 0) || strcmp(p
, "0.0.0.0") == 0)
308 fprintf(hf
, "%s wan-ip\n", p
);
310 fprintf(hf
, "%s %s-wan\n", p
, nv
);
313 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
314 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
315 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
316 p
= nvram_safe_get("dhcpd_static");
317 while ((e
= strchr(p
, '>')) != NULL
) {
328 if ((e
= strchr(buf
, '<')) == NULL
) continue;
333 if ((e
= strchr(ip
, '<')) == NULL
) continue;
335 if (strchr(ip
, '.') == NULL
) {
337 if ((ipn
<= 0) || (ipn
> 255)) continue;
338 sprintf(ipbuf
, "%s%d", lan
, ipn
);
342 if (inet_addr(ip
) == INADDR_NONE
) continue;
347 if ((hf
) && (*name
!= 0)) {
348 fprintf(hf
, "%s %s\n", ip
, name
);
351 if ((do_dhcpd
) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
352 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
360 #ifdef TCONFIG_OPENVPN
361 write_vpn_dnsmasq_config(f
);
364 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
366 fappend(f
, "/etc/dnsmasq.custom");
373 unlink("/etc/resolv.conf");
374 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
377 TRACE_PT("run dnsmasq\n");
379 // Default to some values we like, but allow the user to override them.
380 eval("dnsmasq", "-c", "1500", "--log-async");
382 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
389 void stop_dnsmasq(void)
394 stop_service("dnsmasq");
400 unlink("/etc/resolv.conf");
401 symlink(dmresolv
, "/etc/resolv.conf");
403 killall_tk("dnsmasq");
408 void clear_resolv(void)
410 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
414 static int write_ipv6_dns_servers(FILE *f
, const char *prefix
, char *dns
)
416 char p
[INET6_ADDRSTRLEN
+ 1], *next
= NULL
;
417 struct in6_addr addr
;
420 foreach(p
, dns
, next
) {
421 // verify that this is a valid IPv6 address
422 if (inet_pton(AF_INET6
, p
, &addr
) == 1) {
423 fprintf(f
, "%s%s\n", prefix
, p
);
432 void dns_to_resolv(void)
435 const dns_list_t
*dns
;
439 m
= umask(022); // 077 from pppoecd
440 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
441 // Check for VPN DNS entries
442 if (!write_vpn_resolv(f
)) {
444 if (write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_dns")) == 0 || nvram_get_int("dns_addget"))
445 write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_get_dns"));
447 dns
= get_dns(); // static buffer
448 if (dns
->count
== 0) {
449 // Put a pseudo DNS IP to trigger Connect On Demand
450 if (nvram_match("ppp_demand", "1")) {
451 switch (get_wan_proto()) {
455 fprintf(f
, "nameserver 1.1.1.1\n");
461 for (i
= 0; i
< dns
->count
; i
++) {
462 if (dns
->dns
[i
].port
== 53) { // resolv.conf doesn't allow for an alternate port
463 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
].addr
));
473 // -----------------------------------------------------------------------------
475 void start_httpd(void)
483 void stop_httpd(void)
488 // -----------------------------------------------------------------------------
491 void start_ipv6_sit_tunnel(void)
493 char *tun_dev
= nvram_safe_get("ipv6_ifname");
494 char ip
[INET6_ADDRSTRLEN
+ 4];
497 if (get_ipv6_service() == IPV6_6IN4
) {
499 snprintf(ip
, sizeof(ip
), "%s/%d",
500 nvram_safe_get("ipv6_tun_addr"),
501 nvram_get_int("ipv6_tun_addrlen") ? : 64);
504 eval("ip", "tunnel", "add", tun_dev
, "mode", "sit",
505 "remote", nvram_safe_get("ipv6_tun_v4end"),
506 "local", (char *)wanip
,
507 "ttl", nvram_safe_get("ipv6_tun_ttl"));
508 if (nvram_get_int("ipv6_tun_mtu") > 0)
509 eval("ip", "link", "set", tun_dev
, "mtu", nvram_safe_get("ipv6_tun_mtu"), "up");
511 eval("ip", "link", "set", tun_dev
, "up");
512 eval("ip", "addr", "add", ip
, "dev", tun_dev
);
513 eval("ip", "route", "add", "::/0", "dev", tun_dev
);
517 void stop_ipv6_sit_tunnel(void)
519 char *tun_dev
= nvram_safe_get("ipv6_ifname");
520 eval("ip", "tunnel", "del", tun_dev
);
524 static pid_t pid_radvd
= -1;
526 void start_radvd(void)
531 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
535 start_service("radvd");
541 if (ipv6_enabled() && nvram_match("ipv6_radvd", "1")) {
543 switch (get_ipv6_service()) {
544 case IPV6_NATIVE_DHCP
:
548 prefix
= 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 " AdvSendAdvert on;\n"
563 " MaxRtrAdvInterval 60;\n"
564 " AdvHomeAgentFlag off;\n"
565 " AdvManagedFlag off;\n"
569 " AdvAutonomous on;\n"
573 nvram_safe_get("lan_ifname"), prefix
,
574 do_dns
? "RDNSS " : "", do_dns
? ip
: "", do_dns
? " { };" : "");
579 if (nvram_get_int("debug_ipv6")) {
584 _eval(argv
, NULL
, 0, &pid
);
586 if (!nvram_contains_word("debug_norestart", "radvd")) {
592 void stop_radvd(void)
595 stop_service("radvd");
603 void start_ipv6(void)
606 char ip
[INET6_ADDRSTRLEN
+ 4];
609 service
= get_ipv6_service();
612 // Check if turned on
617 p
= (char *)ipv6_router_address(NULL
);
619 snprintf(ip
, sizeof(ip
), "%s/%d", p
, nvram_get_int("ipv6_prefix_length") ? : 64);
620 eval("ip", "-6", "addr", "add", ip
, "dev", nvram_safe_get("lan_ifname"));
625 if (service
!= IPV6_DISABLED
) {
626 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0)
627 accept_ra(nvram_safe_get("lan_ifname"));
633 stop_ipv6_sit_tunnel();
635 eval("ip", "-6", "addr", "flush", "scope", "global");
640 // -----------------------------------------------------------------------------
642 void start_upnp(void)
645 start_service("upnp");
649 if (get_wan_proto() == WP_DISABLED
) return;
655 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
656 mkdir("/etc/upnp", 0777);
657 if (f_exists("/etc/upnp/config.alt")) {
658 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
661 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
662 upnp_port
= nvram_get_int("upnp_port");
663 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
665 // char *lanip = nvram_safe_get("lan_ipaddr");
666 // char *lanmask = nvram_safe_get("lan_netmask");
670 // "listening_ip=%s/%s\n"
675 "upnp_forward_chain=upnp\n"
676 "upnp_nat_chain=upnp\n"
677 "notify_interval=%d\n"
678 "system_uptime=yes\n"
684 (enable
& 1) ? "yes" : "no", // upnp enable
685 (enable
& 2) ? "yes" : "no", // natpmp enable
686 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
687 nvram_get_int("upnp_ssdp_interval")
690 if (nvram_get_int("upnp_clean")) {
691 int interval
= nvram_get_int("upnp_clean_interval");
692 if (interval
< 60) interval
= 60;
694 "clean_ruleset_interval=%d\n"
695 "clean_ruleset_threshold=%d\n",
697 nvram_get_int("upnp_clean_threshold")
701 fprintf(f
,"clean_ruleset_interval=0\n");
703 if (nvram_match("upnp_mnp", "1")) {
704 int https
= nvram_get_int("https_enable");
705 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
706 // https ? "s" : "", lanip,
707 https
? "s" : "", nvram_safe_get("lan_ipaddr"),
708 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
711 // Empty parameters are not included into XML service description
712 fprintf(f
, "presentation_url=\n");
716 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
717 fprintf(f
, "uuid=%s\n", uuid
);
723 for(br
=0 ; br
<4 ; br
++) {
724 char bridge
[2] = "0";
732 strcat(tmp
, "_ipaddr");
735 strcat(tmp2
, "_netmask");
737 char *lanip
= nvram_safe_get(tmp
);
738 char *lanmask
= nvram_safe_get(tmp2
);
740 if(strcmp(lanip
,"")!=0) {
742 "listening_ip=%s/%s\n",
746 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
747 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
748 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
749 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
751 "allow %d-%d %s/%s %d-%d\n",
758 // by default allow only redirection of ports above 1024
759 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
764 fappend(f
, "/etc/upnp/config.custom");
765 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
768 xstart("miniupnpd", "-f", "/etc/upnp/config");
777 stop_service("upnp");
781 killall_tk("miniupnpd");
784 // -----------------------------------------------------------------------------
786 static pid_t pid_crond
= -1;
788 void start_cron(void)
792 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
793 if (!nvram_contains_word("debug_norestart", "crond")) {
804 // -----------------------------------------------------------------------------
807 static pid_t pid_hotplug2
= -1;
809 void start_hotplug2()
813 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
814 xstart("hotplug2", "--persistent", "--no-coldplug");
815 // FIXME: Don't remember exactly why I put "sleep" here -
816 // but it was not for a race with check_services()... - TB
819 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
824 void stop_hotplug2(void)
827 killall_tk("hotplug2");
831 // -----------------------------------------------------------------------------
833 // Written by Sparq in 2002/07/16
834 void start_zebra(void)
838 start_service("zebra");
844 char *lan_tx
= nvram_safe_get("dr_lan_tx");
845 char *lan_rx
= nvram_safe_get("dr_lan_rx");
846 char *lan1_tx
= nvram_safe_get("dr_lan1_tx");
847 char *lan1_rx
= nvram_safe_get("dr_lan1_rx");
848 char *lan2_tx
= nvram_safe_get("dr_lan2_tx");
849 char *lan2_rx
= nvram_safe_get("dr_lan2_rx");
850 char *lan3_tx
= nvram_safe_get("dr_lan3_tx");
851 char *lan3_rx
= nvram_safe_get("dr_lan3_rx");
852 char *wan_tx
= nvram_safe_get("dr_wan_tx");
853 char *wan_rx
= nvram_safe_get("dr_wan_rx");
855 // if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
856 if ((*lan_tx
== '0') && (*lan_rx
== '0') &&
857 (*lan1_tx
== '0') && (*lan1_rx
== '0') &&
858 (*lan2_tx
== '0') && (*lan2_rx
== '0') &&
859 (*lan3_tx
== '0') && (*lan3_rx
== '0') &&
860 (*wan_tx
== '0') && (*wan_rx
== '0')) {
865 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
870 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
871 char *lan_ifname
= nvram_safe_get("lan_ifname");
872 char *lan1_ifname
= nvram_safe_get("lan1_ifname");
873 char *lan2_ifname
= nvram_safe_get("lan2_ifname");
874 char *lan3_ifname
= nvram_safe_get("lan3_ifname");
875 char *wan_ifname
= nvram_safe_get("wan_ifname");
877 fprintf(fp
, "router rip\n");
878 if(strcmp(lan_ifname
,"")!=0)
879 fprintf(fp
, "network %s\n", lan_ifname
);
880 if(strcmp(lan1_ifname
,"")!=0)
881 fprintf(fp
, "network %s\n", lan1_ifname
);
882 if(strcmp(lan2_ifname
,"")!=0)
883 fprintf(fp
, "network %s\n", lan2_ifname
);
884 if(strcmp(lan3_ifname
,"")!=0)
885 fprintf(fp
, "network %s\n", lan3_ifname
);
886 fprintf(fp
, "network %s\n", wan_ifname
);
887 fprintf(fp
, "redistribute connected\n");
888 //fprintf(fp, "redistribute static\n");
890 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
891 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
893 if(strcmp(lan_ifname
,"")!=0) {
894 fprintf(fp
, "interface %s\n", lan_ifname
);
895 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
896 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
898 if(strcmp(lan1_ifname
,"")!=0) {
899 fprintf(fp
, "interface %s\n", lan1_ifname
);
900 if (*lan1_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan1_tx
);
901 if (*lan1_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan1_rx
);
903 if(strcmp(lan2_ifname
,"")!=0) {
904 fprintf(fp
, "interface %s\n", lan2_ifname
);
905 if (*lan2_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan2_tx
);
906 if (*lan2_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan2_rx
);
908 if(strcmp(lan3_ifname
,"")!=0) {
909 fprintf(fp
, "interface %s\n", lan3_ifname
);
910 if (*lan3_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan3_tx
);
911 if (*lan3_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan3_rx
);
913 fprintf(fp
, "interface %s\n", wan_ifname
);
914 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
915 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
917 fprintf(fp
, "router rip\n");
918 if(strcmp(lan_ifname
,"")!=0) {
919 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
920 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
922 if(strcmp(lan1_ifname
,"")!=0) {
923 if (*lan1_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan1_ifname
);
924 if (*lan1_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan1_ifname
);
926 if(strcmp(lan2_ifname
,"")!=0) {
927 if (*lan2_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan2_ifname
);
928 if (*lan2_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan2_ifname
);
930 if(strcmp(lan3_ifname
,"")!=0) {
931 if (*lan3_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan3_ifname
);
932 if (*lan3_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan3_ifname
);
934 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
935 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
936 fprintf(fp
, "access-list private deny any\n");
938 //fprintf(fp, "debug rip events\n");
939 //fprintf(fp, "log file /etc/ripd.log\n");
943 xstart("zebra", "-d");
944 xstart("ripd", "-d");
948 void stop_zebra(void)
952 stop_service("zebra");
956 killall("zebra", SIGTERM
);
957 killall("ripd", SIGTERM
);
959 unlink("/etc/zebra.conf");
960 unlink("/etc/ripd.conf");
964 // -----------------------------------------------------------------------------
966 void start_syslog(void)
976 char *rot_siz
= "50";
981 if (nvram_match("log_remote", "1")) {
982 nv
= nvram_safe_get("log_remoteip");
984 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
990 if (nvram_match("log_file", "1")) {
993 /* Read options: rotate_size(kb) num_backups logfilename.
994 * Ignore these settings and use defaults if the logfile cannot be written to.
996 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
997 if ((nv
= strchr(cfg
, '\n')))
1000 if ((nv
= strtok(cfg
, " \t"))) {
1005 if ((nv
= strtok(NULL
, " \t")))
1008 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
1009 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
1010 argv
[argc
++] = "-O";
1020 argv
[argc
++] = "-s";
1021 argv
[argc
++] = rot_siz
;
1023 if (isdigit(*b_opt
)) {
1024 argv
[argc
++] = "-b";
1025 argv
[argc
++] = b_opt
;
1031 _eval(argv
, NULL
, 0, NULL
);
1035 _eval(argv
, NULL
, 0, NULL
);
1037 // used to be available in syslogd -m
1038 n
= nvram_get_int("log_mark");
1042 sprintf(rem
, "*/%d * * * *", n
);
1043 else if (n
< 60 * 24)
1044 sprintf(rem
, "0 */%d * * *", n
/ 60);
1046 sprintf(rem
, "0 0 */%d * *", n
/ (60 * 24));
1047 sprintf(s
, "cru a syslogdmark \"%s logger -p syslog.info -- -- MARK --\"", rem
);
1051 system("cru d syslogdmark");
1056 void stop_syslog(void)
1058 killall("klogd", SIGTERM
);
1059 killall("syslogd", SIGTERM
);
1062 // -----------------------------------------------------------------------------
1064 static pid_t pid_igmp
= -1;
1066 void start_igmp_proxy(void)
1071 if (nvram_match("multicast_pass", "1")) {
1072 if (get_wan_proto() == WP_DISABLED
)
1075 if (f_exists("/etc/igmp.alt")) {
1076 eval("igmpproxy", "/etc/igmp.alt");
1078 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
1081 "phyint %s upstream\n"
1083 "phyint %s downstream ratelimit 0\n",
1085 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
1086 nvram_safe_get("lan_ifname"));
1088 eval("igmpproxy", "/etc/igmp.conf");
1093 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1099 void stop_igmp_proxy(void)
1102 killall_tk("igmpproxy");
1106 // -----------------------------------------------------------------------------
1110 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
1113 void start_ntpc(void)
1119 if (nvram_get_int("ntp_updates") >= 0) {
1120 xstart("ntpsync", "--init");
1124 void stop_ntpc(void)
1126 killall("ntpsync", SIGTERM
);
1129 // -----------------------------------------------------------------------------
1131 static void stop_rstats(void)
1137 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1138 if (kill(pid
, SIGTERM
) != 0) break;
1143 static void start_rstats(int new)
1145 if (nvram_match("rstats_enable", "1")) {
1147 if (new) xstart("rstats", "--new");
1148 else xstart("rstats");
1152 // -----------------------------------------------------------------------------
1154 // !!TB - FTP Server
1157 static char *get_full_storage_path(char *val
)
1159 static char buf
[128];
1163 len
= sprintf(buf
, "%s", val
);
1165 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1167 if (len
> 1 && buf
[len
- 1] == '/')
1173 static char *nvram_storage_path(char *var
)
1175 char *val
= nvram_safe_get(var
);
1176 return get_full_storage_path(val
);
1179 char vsftpd_conf
[] = "/etc/vsftpd.conf";
1180 char vsftpd_users
[] = "/etc/vsftpd.users";
1181 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
1183 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1185 static void start_ftpd(void)
1191 char *user
, *pass
, *rights
;
1193 if (getpid() != 1) {
1194 start_service("ftpd");
1198 if (!nvram_get_int("ftp_enable")) return;
1200 mkdir_if_none(vsftpd_users
);
1201 mkdir_if_none("/var/run/vsftpd");
1203 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
1206 if (nvram_get_int("ftp_super"))
1209 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1210 if ((f
= fopen(tmp
, "w")))
1213 "dirlist_enable=yes\n"
1214 "write_enable=yes\n"
1215 "download_enable=yes\n");
1220 #ifdef TCONFIG_SAMBASRV
1221 if (nvram_match("smbd_cset", "utf8"))
1222 fprintf(fp
, "utf8=yes\n");
1225 if (nvram_invmatch("ftp_anonymous", "0"))
1228 "anon_allow_writable_root=yes\n"
1229 "anon_world_readable_only=no\n"
1230 "anon_umask=022\n");
1233 sprintf(tmp
, "%s/ftp", vsftpd_users
);
1234 if ((f
= fopen(tmp
, "w")))
1236 if (nvram_match("ftp_dirlist", "0"))
1237 fprintf(f
, "dirlist_enable=yes\n");
1238 if (nvram_match("ftp_anonymous", "1") ||
1239 nvram_match("ftp_anonymous", "3"))
1240 fprintf(f
, "write_enable=yes\n");
1241 if (nvram_match("ftp_anonymous", "1") ||
1242 nvram_match("ftp_anonymous", "2"))
1243 fprintf(f
, "download_enable=yes\n");
1246 if (nvram_match("ftp_anonymous", "1") ||
1247 nvram_match("ftp_anonymous", "3"))
1249 "anon_upload_enable=yes\n"
1250 "anon_mkdir_write_enable=yes\n"
1251 "anon_other_write_enable=yes\n");
1253 fprintf(fp
, "anonymous_enable=no\n");
1257 "dirmessage_enable=yes\n"
1258 "download_enable=no\n"
1259 "dirlist_enable=no\n"
1261 "syslog_enable=yes\n"
1262 "local_enable=yes\n"
1265 "chroot_local_user=yes\n"
1267 "log_ftp_protocol=%s\n"
1268 "user_config_dir=%s\n"
1276 "max_login_fails=1\n"
1277 "idle_session_timeout=%s\n"
1279 "anon_max_rate=%d\n"
1280 "local_max_rate=%d\n"
1282 nvram_get_int("log_ftp") ? "yes" : "no",
1283 vsftpd_users
, vsftpd_passwd
,
1285 ipv6_enabled() ? "_ipv6" : "",
1289 nvram_get("ftp_port") ? : "21",
1290 nvram_get_int("ftp_max"),
1291 nvram_get_int("ftp_ipmax"),
1292 nvram_get("ftp_staytimeout") ? : "300",
1293 nvram_get_int("ftp_anonrate") * 1024,
1294 nvram_get_int("ftp_rate") * 1024,
1295 nvram_safe_get("ftp_custom"));
1299 /* prepare passwd file and default users */
1300 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
1303 if (((user
= nvram_get("http_username")) == NULL
) || (*user
== 0)) user
= "admin";
1304 if (((pass
= nvram_get("http_passwd")) == NULL
) || (*pass
== 0)) pass
= "admin";
1306 fprintf(fp
, /* anonymous, admin, nobody */
1307 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1308 "%s:%s:0:0:root:/:/sbin/nologin\n"
1309 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1310 nvram_storage_path("ftp_anonroot"), user
,
1311 nvram_get_int("ftp_super") ? crypt(pass
, "$1$") : "x",
1314 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1317 username<password<rights
1325 while ((q
= strsep(&p
, ">")) != NULL
) {
1326 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
1327 if (!user
|| !pass
) continue;
1330 if (strncmp(rights
, "Private", 7) == 0)
1332 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
1336 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
1338 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1339 user
, crypt(pass
, "$1$"), user
, tmp
);
1342 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1343 if ((f
= fopen(tmp
, "w")))
1346 if (nvram_invmatch("ftp_dirlist", "1"))
1347 strcat(tmp
, "dirlist_enable=yes\n");
1348 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
1349 strcat(tmp
, "download_enable=yes\n");
1350 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
1351 strcat(tmp
, "write_enable=yes\n");
1361 killall("vsftpd", SIGHUP
);
1363 /* start vsftpd if it's not already running */
1364 if (pidof("vsftpd") <= 0)
1368 static void stop_ftpd(void)
1370 if (getpid() != 1) {
1371 stop_service("ftpd");
1375 killall_tk("vsftpd");
1376 unlink(vsftpd_passwd
);
1377 unlink(vsftpd_conf
);
1378 eval("rm", "-rf", vsftpd_users
);
1380 #endif // TCONFIG_FTP
1382 // -----------------------------------------------------------------------------
1386 #ifdef TCONFIG_SAMBASRV
1387 static void kill_samba(int sig
)
1389 if (sig
== SIGTERM
) {
1394 killall("smbd", sig
);
1395 killall("nmbd", sig
);
1399 static void start_samba(void)
1408 if (getpid() != 1) {
1409 start_service("smbd");
1413 mode
= nvram_get_int("smbd_enable");
1414 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1417 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1420 fprintf(fp
, "[global]\n"
1421 " interfaces = %s\n"
1422 " bind interfaces only = yes\n"
1424 " netbios name = %s\n"
1425 " server string = %s\n"
1426 " guest account = nobody\n"
1427 " security = user\n"
1430 " guest only = no\n"
1431 " browseable = yes\n"
1432 " syslog only = yes\n"
1433 " timestamp logs = no\n"
1435 " encrypt passwords = yes\n"
1436 " preserve case = yes\n"
1437 " short preserve case = yes\n",
1438 nvram_safe_get("lan_ifname"),
1439 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1440 nvram_safe_get("lan_hostname"),
1441 nvram_get("router_name") ? : "Tomato",
1442 mode
== 2 ? "" : "map to guest = Bad User",
1443 mode
== 2 ? "no" : "yes" // guest ok
1446 if (nvram_get_int("smbd_wins")) {
1447 nv
= nvram_safe_get("wan_wins");
1448 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1449 fprintf(fp
, " wins support = yes\n");
1453 if (nvram_get_int("smbd_master")) {
1455 " domain master = yes\n"
1456 " local master = yes\n"
1457 " preferred master = yes\n"
1458 " os level = 65\n");
1461 nv
= nvram_safe_get("smbd_cpage");
1463 #ifndef TCONFIG_SAMBA3
1464 fprintf(fp
, " client code page = %s\n", nv
);
1466 sprintf(nlsmod
, "nls_cp%s", nv
);
1468 nv
= nvram_safe_get("smbd_nlsmod");
1469 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1473 nvram_set("smbd_nlsmod", nlsmod
);
1476 #ifndef TCONFIG_SAMBA3
1477 if (nvram_match("smbd_cset", "utf8"))
1478 fprintf(fp
, " coding system = utf8\n");
1479 else if (nvram_invmatch("smbd_cset", ""))
1480 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1483 nv
= nvram_safe_get("smbd_custom");
1484 /* add socket options unless overriden by the user */
1485 if (strstr(nv
, "socket options") == NULL
) {
1486 fprintf(fp
, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1488 fprintf(fp
, "%s\n\n", nv
);
1490 /* configure shares */
1494 char *name
, *path
, *comment
, *writeable
, *hidden
;
1497 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1499 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1502 while ((q
= strsep(&p
, ">")) != NULL
) {
1503 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1504 if (!path
|| !name
) continue;
1507 fprintf(fp
, "\n[%s]\n", name
);
1510 fprintf(fp
, " path = %s\n", path
);
1513 if (!strcmp(writeable
, "1"))
1514 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1515 if (!strcmp(hidden
, "1"))
1516 fprintf(fp
, " browseable = no\n");
1520 fprintf(fp
, " comment = %s\n", comment
);
1527 /* Share every mountpoint below MOUNT_ROOT */
1528 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1529 while ((dp
= readdir(dir
))) {
1530 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1536 /* Only if is a directory and is mounted */
1537 sprintf(path
, "%s/%s", MOUNT_ROOT
, dp
->d_name
);
1538 sb
.st_mode
= S_IFDIR
; /* failsafe */
1540 if (!S_ISDIR(sb
.st_mode
))
1543 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1546 thisdev
= sb
.st_dev
;
1548 ++sb
.st_dev
; /* failsafe */
1550 if (thisdev
== sb
.st_dev
)
1553 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1554 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1555 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1556 if (nvram_match("smbd_autoshare", "3")) // Hidden
1557 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1558 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1559 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1560 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1566 if (dir
) closedir(dir
);
1569 /* by default share MOUNT_ROOT as read-only */
1570 fprintf(fp
, "\n[share]\n"
1578 mkdir_if_none("/var/run/samba");
1579 mkdir_if_none("/etc/samba");
1581 /* write smbpasswd */
1582 #ifdef TCONFIG_SAMBA3
1583 eval("smbpasswd", "nobody", "\"\"");
1585 eval("smbpasswd", "-a", "nobody", "\"\"");
1589 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1591 #ifdef TCONFIG_SAMBA3
1592 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1594 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1599 int ret1
= 0, ret2
= 0;
1600 /* start samba if it's not already running */
1601 if (pidof("nmbd") <= 0)
1602 ret1
= xstart("nmbd", "-D");
1603 if (pidof("smbd") <= 0)
1604 ret2
= xstart("smbd", "-D");
1606 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1609 static void stop_samba(void)
1611 if (getpid() != 1) {
1612 stop_service("smbd");
1616 kill_samba(SIGTERM
);
1618 unlink("/var/log/smb");
1619 unlink("/var/log/nmb");
1620 eval("rm", "-rf", "/var/run/samba");
1622 #endif // TCONFIG_SAMBASRV
1624 #ifdef TCONFIG_MEDIA_SERVER
1625 #define MEDIA_SERVER_APP "minidlna"
1627 static void start_media_server(void)
1630 int port
, pid
, https
;
1632 char *argv
[] = { MEDIA_SERVER_APP
, "-f", "/etc/"MEDIA_SERVER_APP
".conf", "-R", NULL
};
1633 static int once
= 1;
1635 if (getpid() != 1) {
1636 start_service("media");
1640 if (nvram_get_int("ms_sas") == 0)
1643 if (nvram_get_int("ms_enable") != 0) {
1644 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
1648 nvram_unset("ms_rescan");
1650 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
1651 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
1654 if ((f
= fopen(argv
[2], "w")) != NULL
) {
1655 port
= nvram_get_int("ms_port");
1656 https
= nvram_get_int("https_enable");
1657 dbdir
= nvram_safe_get("ms_dbdir");
1658 if (!(*dbdir
)) dbdir
= NULL
;
1659 mkdir_if_none(dbdir
? : "/var/run/"MEDIA_SERVER_APP
);
1662 "network_interface=%s\n"
1664 "friendly_name=%s\n"
1668 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1670 "notify_interval=600\n"
1671 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\n"
1673 nvram_safe_get("lan_ifname"),
1674 (port
< 0) || (port
>= 0xffff) ? 0 : port
,
1675 nvram_get("router_name") ? : "Tomato",
1676 dbdir
? : "/var/run/"MEDIA_SERVER_APP
,
1677 nvram_get_int("ms_tivo") ? "yes" : "no",
1678 nvram_get_int("ms_stdlna") ? "yes" : "no",
1679 https
? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https
? "https_lanport" : "http_lanport")
1682 // media directories
1684 char *path
, *restrict
;
1686 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
1687 /* path<restrict[A|V|P|] */
1690 while ((q
= strsep(&p
, ">")) != NULL
) {
1691 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
1693 fprintf(f
, "media_dir=%s%s%s\n",
1694 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
1703 /* start media server if it's not already running */
1704 if (pidof(MEDIA_SERVER_APP
) <= 0) {
1705 if ((_eval(argv
, NULL
, 0, &pid
) == 0) && (once
)) {
1706 /* If we started the media server successfully, wait 1 sec
1707 * to let it die if it can't open the database file.
1708 * If it's still alive after that, assume it's running and
1709 * disable forced once-after-reboot rescan.
1712 if (pidof(MEDIA_SERVER_APP
) > 0)
1719 static void stop_media_server(void)
1721 if (getpid() != 1) {
1722 stop_service("media");
1726 killall_tk(MEDIA_SERVER_APP
);
1728 #endif // TCONFIG_MEDIA_SERVER
1731 static void start_nas_services(void)
1733 if (getpid() != 1) {
1734 start_service("usbapps");
1738 #ifdef TCONFIG_SAMBASRV
1744 #ifdef TCONFIG_MEDIA_SERVER
1745 start_media_server();
1749 static void stop_nas_services(void)
1751 if (getpid() != 1) {
1752 stop_service("usbapps");
1756 #ifdef TCONFIG_MEDIA_SERVER
1757 stop_media_server();
1762 #ifdef TCONFIG_SAMBASRV
1767 void restart_nas_services(int stop
, int start
)
1769 int fd
= file_lock("usb");
1770 /* restart all NAS applications */
1772 stop_nas_services();
1774 start_nas_services();
1777 #endif // TCONFIG_USB
1779 // -----------------------------------------------------------------------------
1781 /* -1 = Don't check for this program, it is not expected to be running.
1782 * Other = This program has been started and should be kept running. If no
1783 * process with the name is running, call func to restart it.
1784 * Note: At startup, dnsmasq forks a short-lived child which forks a
1785 * long-lived (grand)child. The parents terminate.
1786 * Many daemons use this technique.
1788 static void _check(pid_t pid
, const char *name
, void (*func
)(void))
1790 if (pid
== -1) return;
1792 if (pidof(name
) > 0) return;
1794 syslog(LOG_DEBUG
, "%s terminated unexpectedly, restarting.\n", name
);
1797 // Force recheck in 500 msec
1798 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
1801 void check_services(void)
1803 TRACE_PT("keep alive\n");
1805 // Periodically reap any zombies
1806 setitimer(ITIMER_REAL
, &zombie_tv
, NULL
);
1809 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
1811 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1812 _check(pid_crond
, "crond", start_cron
);
1813 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
1815 _check(pid_radvd
, "radvd", start_radvd
);
1819 // -----------------------------------------------------------------------------
1821 void start_services(void)
1823 static int once
= 1;
1828 if (nvram_get_int("telnetd_eas")) start_telnetd();
1829 if (nvram_get_int("sshd_eas")) start_sshd();
1843 /* note: starting radvd here might be too early in case of
1844 * DHCPv6 because we won't have received a prefix and so it
1845 * will disable advertisements, but the SIGHUP sent from
1846 * dhcp6c-state will restart them.
1850 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1853 void stop_services(void)
1857 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1873 // -----------------------------------------------------------------------------
1875 /* nvram "action_service" is: "service-action[-modifier]"
1876 * action is something like "stop" or "start" or "restart"
1877 * optional modifier is "c" for the "service" command-line command
1879 void exec_service(void)
1881 const int A_START
= 1;
1882 const int A_STOP
= 2;
1883 const int A_RESTART
= 1|2;
1892 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1896 act
= strsep(&next
, ",");
1897 service
= strsep(&act
, "-");
1903 strsep(&modifier
, "-");
1905 TRACE_PT("service=%s action=%s modifier=%s\n", service
, act
, modifier
? : "");
1907 if (strcmp(act
, "start") == 0) action
= A_START
;
1908 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1909 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1911 user
= (modifier
!= NULL
&& *modifier
== 'c');
1913 if (strcmp(service
, "dhcpc") == 0) {
1914 if (action
& A_STOP
) stop_dhcpc();
1915 if (action
& A_START
) start_dhcpc();
1919 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1920 if (action
& A_STOP
) stop_dnsmasq();
1921 if (action
& A_START
) {
1928 if (strcmp(service
, "firewall") == 0) {
1929 if (action
& A_STOP
) {
1933 if (action
& A_START
) {
1940 if (strcmp(service
, "restrict") == 0) {
1941 if (action
& A_STOP
) {
1944 if (action
& A_START
) {
1945 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1949 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1951 if (nvram_get_int("rrules_radio") < 0) {
1952 eval("radio", "on");
1959 if (strcmp(service
, "qos") == 0) {
1960 if (action
& A_STOP
) {
1963 stop_firewall(); start_firewall(); // always restarted
1964 if (action
& A_START
) {
1966 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1971 if (strcmp(service
, "upnp") == 0) {
1972 if (action
& A_STOP
) {
1975 stop_firewall(); start_firewall(); // always restarted
1976 if (action
& A_START
) {
1982 if (strcmp(service
, "telnetd") == 0) {
1983 if (action
& A_STOP
) stop_telnetd();
1984 if (action
& A_START
) start_telnetd();
1988 if (strcmp(service
, "sshd") == 0) {
1989 if (action
& A_STOP
) stop_sshd();
1990 if (action
& A_START
) start_sshd();
1994 if (strcmp(service
, "httpd") == 0) {
1995 if (action
& A_STOP
) stop_httpd();
1996 if (action
& A_START
) start_httpd();
2001 if (strcmp(service
, "ipv6") == 0) {
2002 if (action
& A_STOP
) {
2006 if (action
& A_START
) {
2013 if (strcmp(service
, "radvd") == 0) {
2014 if (action
& A_STOP
) {
2017 if (action
& A_START
) {
2023 if (strncmp(service
, "dhcp6", 5) == 0) {
2024 if (action
& A_STOP
) {
2027 if (action
& A_START
) {
2034 if (strcmp(service
, "admin") == 0) {
2035 if (action
& A_STOP
) {
2040 stop_firewall(); start_firewall(); // always restarted
2041 if (action
& A_START
) {
2044 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2045 if (nvram_match("sshd_eas", "1")) start_sshd();
2050 if (strcmp(service
, "ddns") == 0) {
2051 if (action
& A_STOP
) stop_ddns();
2052 if (action
& A_START
) start_ddns();
2056 if (strcmp(service
, "ntpc") == 0) {
2057 if (action
& A_STOP
) stop_ntpc();
2058 if (action
& A_START
) start_ntpc();
2062 if (strcmp(service
, "logging") == 0) {
2063 if (action
& A_STOP
) {
2066 if (action
& A_START
) {
2070 // always restarted except from "service" command
2071 stop_cron(); start_cron();
2072 stop_firewall(); start_firewall();
2077 if (strcmp(service
, "crond") == 0) {
2078 if (action
& A_STOP
) {
2081 if (action
& A_START
) {
2088 if (strncmp(service
, "hotplug", 7) == 0) {
2089 if (action
& A_STOP
) {
2092 if (action
& A_START
) {
2099 if (strcmp(service
, "upgrade") == 0) {
2100 if (action
& A_START
) {
2105 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2113 killall("rstats", SIGTERM
);
2114 killall("buttons", SIGTERM
);
2116 remove_storage_main(1); // !!TB - USB Support
2117 stop_usb(); // !!TB - USB Support
2123 if (strcmp(service
, "cifs") == 0) {
2124 if (action
& A_STOP
) stop_cifs();
2125 if (action
& A_START
) start_cifs();
2130 #ifdef TCONFIG_JFFS2
2131 if (strncmp(service
, "jffs", 4) == 0) {
2132 if (action
& A_STOP
) stop_jffs2();
2133 if (action
& A_START
) start_jffs2();
2138 if (strcmp(service
, "zebra") == 0) {
2139 if (action
& A_STOP
) stop_zebra();
2140 if (action
& A_START
) start_zebra();
2144 if (strcmp(service
, "routing") == 0) {
2145 if (action
& A_STOP
) {
2147 do_static_routes(0); // remove old '_saved'
2148 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2152 if (action
& A_START
) {
2153 do_static_routes(1); // add new
2155 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2160 if (strcmp(service
, "ctnf") == 0) {
2161 if (action
& A_START
) {
2169 if (strcmp(service
, "wan") == 0) {
2170 if (action
& A_STOP
) {
2174 if (action
& A_START
) {
2175 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2183 if (strcmp(service
, "net") == 0) {
2184 if (action
& A_STOP
) {
2195 if (action
& A_START
) {
2210 if (strcmp(service
, "nas") == 0) {
2211 if (action
& A_STOP
) {
2214 if (action
& A_START
) {
2221 if (strcmp(service
, "rstats") == 0) {
2222 if (action
& A_STOP
) stop_rstats();
2223 if (action
& A_START
) start_rstats(0);
2227 if (strcmp(service
, "rstatsnew") == 0) {
2228 if (action
& A_STOP
) stop_rstats();
2229 if (action
& A_START
) start_rstats(1);
2233 if (strcmp(service
, "sched") == 0) {
2234 if (action
& A_STOP
) stop_sched();
2235 if (action
& A_START
) start_sched();
2240 // !!TB - USB Support
2241 if (strcmp(service
, "usb") == 0) {
2242 if (action
& A_STOP
) stop_usb();
2243 if (action
& A_START
) {
2245 // restart Samba and ftp since they may be killed by stop_usb()
2246 restart_nas_services(0, 1);
2247 // remount all partitions by simulating hotplug event
2248 add_remove_usbhost("-1", 1);
2253 if (strcmp(service
, "usbapps") == 0) {
2254 if (action
& A_STOP
) stop_nas_services();
2255 if (action
& A_START
) start_nas_services();
2261 // !!TB - FTP Server
2262 if (strcmp(service
, "ftpd") == 0) {
2263 if (action
& A_STOP
) stop_ftpd();
2267 if (action
& A_START
) start_ftpd();
2272 #ifdef TCONFIG_MEDIA_SERVER
2273 if (strcmp(service
, "media") == 0 || strcmp(service
, "dlna") == 0) {
2274 if (action
& A_STOP
) stop_media_server();
2275 if (action
& A_START
) start_media_server();
2280 #ifdef TCONFIG_SAMBASRV
2282 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2283 if (action
& A_STOP
) stop_samba();
2284 if (action
& A_START
) {
2294 #ifdef TCONFIG_OPENVPN
2295 if (strncmp(service
, "vpnclient", 9) == 0) {
2296 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
2297 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
2301 if (strncmp(service
, "vpnserver", 9) == 0) {
2302 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
2303 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
2311 // some functions check action_service and must be cleared at end -- zzz
2312 nvram_set("action_service", "");
2314 // Force recheck in 500 msec
2315 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2318 static void do_service(const char *name
, const char *action
, int user
)
2324 while (!nvram_match("action_service", "")) {
2329 else if (--n
< 0) break;
2333 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2334 nvram_set("action_service", s
);
2338 while (nvram_match("action_service", s
)) {
2350 int service_main(int argc
, char *argv
[])
2352 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
2353 do_service(argv
[1], argv
[2], 1);
2354 printf("\nDone.\n");
2358 void start_service(const char *name
)
2360 do_service(name
, "start", 0);
2363 void stop_service(const char *name
)
2365 do_service(name
, "stop", 0);
2369 void restart_service(const char *name)
2371 do_service(name, "restart", 0);