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
;
77 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 router_ip
= nvram_safe_get("lan_ipaddr");
105 "pid-file=/var/run/dnsmasq.pid\n");
106 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
107 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
111 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
113 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
116 "resolv-file=%s\n" // the real stuff is here
117 "addn-hosts=%s\n" // directory with additional hosts files
118 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv
, dmhosts
, dmdhcp
, n
);
122 do_dns
= nvram_match("dhcpd_dmdns", "1");
124 // DNS rebinding protection, will discard upstream RFC1918 responses
125 if (nvram_get_int("dns_norebind")) {
128 "rebind-localhost-ok\n");
129 // allow RFC1918 responses for server domain
130 switch (get_wan_proto()) {
132 nv
= nvram_get("pptp_server_ip");
135 nv
= nvram_get("l2tp_server_ip");
141 if (nv
&& *nv
) fprintf(f
, "rebind-domain-ok=%s\n", nv
);
144 for (n
= 0 ; n
< dns
->count
; ++n
) {
145 if (dns
->dns
[n
].port
!= 53) {
146 fprintf(f
, "server=%s#%u\n", inet_ntoa(dns
->dns
[n
].addr
), dns
->dns
[n
].port
);
150 if (nvram_get_int("dhcpd_static_only")) {
151 fprintf(f
, "dhcp-ignore=tag:!known\n");
156 char lanN_proto
[] = "lanXX_proto";
157 char lanN_ifname
[] = "lanXX_ifname";
158 char lanN_ipaddr
[] = "lanXX_ipaddr";
159 char lanN_netmask
[] = "lanXX_netmask";
160 char dhcpdN_startip
[] = "dhcpdXX_startip";
161 char dhcpdN_endip
[] = "dhcpdXX_endip";
162 char dhcpN_start
[] = "dhcpXX_start";
163 char dhcpN_num
[] = "dhcpXX_num";
164 char dhcpN_lease
[] = "dhcpXX_lease";
166 for(br
=0 ; br
<=3 ; br
++) {
167 char bridge
[2] = "0";
173 sprintf(lanN_proto
, "lan%s_proto", bridge
);
174 sprintf(lanN_ifname
, "lan%s_ifname", bridge
);
175 sprintf(lanN_ipaddr
, "lan%s_ipaddr", bridge
);
176 do_dhcpd
= nvram_match(lanN_proto
, "dhcp");
180 router_ip
= nvram_safe_get(lanN_ipaddr
);
181 strlcpy(lan
, router_ip
, sizeof(lan
));
182 if ((p
= strrchr(lan
, '.')) != NULL
) *(p
+ 1) = 0;
186 nvram_safe_get(lanN_ifname
));
188 sprintf(dhcpN_lease
, "dhcp%s_lease", bridge
);
189 dhcp_lease
= nvram_get_int(dhcpN_lease
);
191 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
193 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
194 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
195 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
198 // if not using dnsmasq for dns
200 if ((dns
->count
== 0) && (nvram_get_int("dhcpd_llndns"))) {
201 // no DNS might be temporary. use a low lease time to force clients to update.
203 strcpy(sdhcp_lease
, "2m");
207 // pass the dns directly
209 for (n
= 0 ; n
< dns
->count
; ++n
) {
210 if (dns
->dns
[n
].port
== 53) { // check: option 6 doesn't seem to support other ports
211 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
].addr
));
214 fprintf(f
, "dhcp-option=%s,6%s\n", nvram_safe_get(lanN_ifname
), buf
);
218 sprintf(dhcpdN_startip
, "dhcpd%s_startip", bridge
);
219 sprintf(dhcpdN_endip
, "dhcpd%s_endip", bridge
);
220 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
222 if ((p
= nvram_get(dhcpdN_startip
)) && (*p
) && (e
= nvram_get(dhcpdN_endip
)) && (*e
)) {
223 fprintf(f
, "dhcp-range=%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname
), p
, e
, nvram_safe_get(lanN_netmask
), dhcp_lease
);
227 sprintf(dhcpN_start
, "dhcp%s_start", bridge
);
228 sprintf(dhcpN_num
, "dhcp%s_num", bridge
);
229 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
230 dhcp_start
= nvram_get_int(dhcpN_start
);
231 dhcp_count
= nvram_get_int(dhcpN_num
);
232 fprintf(f
, "dhcp-range=%s,%s%d,%s%d,%s,%dm\n",
233 nvram_safe_get(lanN_ifname
), lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get(lanN_netmask
), dhcp_lease
);
236 nv
= nvram_safe_get(lanN_ipaddr
);
237 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED
)) {
238 p
= nvram_safe_get("lan_gateway");
239 if ((*p
) && (strcmp(p
, "0.0.0.0") != 0)) nv
= p
;
243 "dhcp-option=%s,3,%s\n", // gateway
244 nvram_safe_get(lanN_ifname
), nv
);
246 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
247 fprintf(f
, "dhcp-option=%s,44,%s\n", nvram_safe_get(lanN_ifname
), nv
);
249 #ifdef TCONFIG_SAMBASRV
250 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
251 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
252 // Samba will serve as a WINS server
253 fprintf(f
, "dhcp-option=%s,44,0.0.0.0\n", nvram_safe_get(lanN_ifname
));
258 if (strcmp(nvram_safe_get(lanN_ifname
),"")!=0)
259 fprintf(f
, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname
));
262 // write static lease entries & create hosts file
264 mkdir_if_none(dmhosts
);
265 snprintf(buf
, sizeof(buf
), "%s/hosts", dmhosts
);
266 if ((hf
= fopen(buf
, "w")) != NULL
) {
267 if (((nv
= nvram_get("wan_hostname")) != NULL
) && (*nv
))
268 fprintf(hf
, "%s %s\n", router_ip
, nv
);
269 #ifdef TCONFIG_SAMBASRV
270 else if (((nv
= nvram_get("lan_hostname")) != NULL
) && (*nv
))
271 fprintf(hf
, "%s %s\n", router_ip
, nv
);
273 p
= (char *)get_wanip();
274 if ((*p
== 0) || strcmp(p
, "0.0.0.0") == 0)
276 fprintf(hf
, "%s wan-ip\n", p
);
278 fprintf(hf
, "%s %s-wan\n", p
, nv
);
282 mkdir_if_none(dmdhcp
);
283 snprintf(buf
, sizeof(buf
), "%s/dhcp-hosts", dmdhcp
);
284 df
= fopen(buf
, "w");
287 // PREVIOUS/OLD FORMAT:
289 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
290 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
291 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
293 // NEW FORMAT (+static ARP binding after hostname):
294 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 55 w/ delim
295 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
296 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 108 w/ delim
298 p
= nvram_safe_get("dhcpd_static");
299 while ((e
= strchr(p
, '>')) != NULL
) {
310 if ((e
= strchr(buf
, '<')) == NULL
) continue;
315 if ((e
= strchr(ip
, '<')) == NULL
) continue;
317 if (strchr(ip
, '.') == NULL
) {
319 if ((ipn
<= 0) || (ipn
> 255)) continue;
320 sprintf(ipbuf
, "%s%d", lan
, ipn
);
324 if (inet_addr(ip
) == INADDR_NONE
) continue;
329 if ((e
= strchr(name
, '<')) != NULL
) {
333 if ((hf
) && (*name
!= 0)) {
334 fprintf(hf
, "%s %s\n", ip
, name
);
337 if ((do_dhcpd_hosts
> 0) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
339 fprintf(df
, "%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
341 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
348 n
= nvram_get_int("dhcpd_lmax");
350 "dhcp-lease-max=%d\n",
352 if (nvram_get_int("dhcpd_auth") >= 0) {
353 fprintf(f
, "dhcp-authoritative\n");
358 #ifdef TCONFIG_OPENVPN
359 write_vpn_dnsmasq_config(f
);
362 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
364 fappend(f
, "/etc/dnsmasq.custom");
371 unlink("/etc/resolv.conf");
372 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
375 TRACE_PT("run dnsmasq\n");
377 // Default to some values we like, but allow the user to override them.
378 eval("dnsmasq", "-c", "1500", "--log-async");
380 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
387 void stop_dnsmasq(void)
392 stop_service("dnsmasq");
398 unlink("/etc/resolv.conf");
399 symlink(dmresolv
, "/etc/resolv.conf");
401 killall_tk("dnsmasq");
406 void clear_resolv(void)
408 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
412 static int write_ipv6_dns_servers(FILE *f
, const char *prefix
, char *dns
, const char *suffix
, int once
)
414 char p
[INET6_ADDRSTRLEN
+ 1], *next
= NULL
;
415 struct in6_addr addr
;
418 foreach(p
, dns
, next
) {
419 // verify that this is a valid IPv6 address
420 if (inet_pton(AF_INET6
, p
, &addr
) == 1) {
421 fprintf(f
, "%s%s%s", (once
&& cnt
) ? "" : prefix
, p
, suffix
);
430 void dns_to_resolv(void)
433 const dns_list_t
*dns
;
437 m
= umask(022); // 077 from pppoecd
438 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
439 // Check for VPN DNS entries
440 if (!write_vpn_resolv(f
)) {
442 if (write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
443 write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
445 dns
= get_dns(); // static buffer
446 if (dns
->count
== 0) {
447 // Put a pseudo DNS IP to trigger Connect On Demand
448 if (nvram_match("ppp_demand", "1")) {
449 switch (get_wan_proto()) {
453 fprintf(f
, "nameserver 1.1.1.1\n");
459 for (i
= 0; i
< dns
->count
; i
++) {
460 if (dns
->dns
[i
].port
== 53) { // resolv.conf doesn't allow for an alternate port
461 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
].addr
));
471 // -----------------------------------------------------------------------------
473 void start_httpd(void)
476 start_service("httpd");
486 void stop_httpd(void)
489 stop_service("httpd");
496 // -----------------------------------------------------------------------------
499 static void add_ip6_lanaddr(void)
501 char ip
[INET6_ADDRSTRLEN
+ 4];
504 p
= ipv6_router_address(NULL
);
506 snprintf(ip
, sizeof(ip
), "%s/%d", p
, nvram_get_int("ipv6_prefix_length") ? : 64);
507 eval("ip", "-6", "addr", "add", ip
, "dev", nvram_safe_get("lan_ifname"));
511 void start_ipv6_tunnel(void)
513 char ip
[INET6_ADDRSTRLEN
+ 4];
514 struct in_addr addr4
;
515 struct in6_addr addr
;
516 const char *wanip
, *mtu
, *tun_dev
;
519 service
= get_ipv6_service();
520 tun_dev
= get_wan6face();
522 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
525 if (service
== IPV6_ANYCAST_6TO4
)
526 snprintf(ip
, sizeof(ip
), "192.88.99.%d", nvram_get_int("ipv6_relay"));
528 strlcpy(ip
, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip
));
529 eval("ip", "tunnel", "add", (char *)tun_dev
, "mode", "sit",
531 "local", (char *)wanip
,
532 "ttl", nvram_safe_get("ipv6_tun_ttl"));
534 eval("ip", "link", "set", (char *)tun_dev
, "mtu", (char *)mtu
, "up");
535 nvram_set("ipv6_ifname", (char *)tun_dev
);
537 if (service
== IPV6_ANYCAST_6TO4
) {
540 memset(&addr
, 0, sizeof(addr
));
541 inet_aton(wanip
, &addr4
);
542 addr
.s6_addr16
[0] = htons(0x2002);
543 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
544 addr
.s6_addr16
[7] = htons(0x0001);
545 inet_ntop(AF_INET6
, &addr
, ip
, sizeof(ip
));
546 strncat(ip
, "/16", sizeof(ip
));
549 snprintf(ip
, sizeof(ip
), "%s/%d",
550 nvram_safe_get("ipv6_tun_addr"),
551 nvram_get_int("ipv6_tun_addrlen") ? : 64);
553 eval("ip", "addr", "add", ip
, "dev", (char *)tun_dev
);
554 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev
);
557 if (service
== IPV6_ANYCAST_6TO4
)
561 void stop_ipv6_tunnel(void)
563 eval("ip", "tunnel", "del", (char *)get_wan6face());
564 if (get_ipv6_service() == IPV6_ANYCAST_6TO4
) {
565 // get rid of old IPv6 address from lan iface
566 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
571 static pid_t pid_radvd
= -1;
573 void start_radvd(void)
576 char *prefix
, *ip
, *mtu
;
578 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
579 int pid
, argc
, service
, cnt
;
582 start_service("radvd");
588 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
589 service
= get_ipv6_service();
590 do_6to4
= (service
== IPV6_ANYCAST_6TO4
);
594 case IPV6_NATIVE_DHCP
:
597 case IPV6_ANYCAST_6TO4
:
599 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
602 prefix
= do_6to4
? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
605 if (!(*prefix
)) prefix
= "::";
608 if ((f
= fopen("/etc/radvd.conf", "w")) == NULL
) return;
610 ip
= (char *)ipv6_router_address(NULL
);
611 do_dns
= (*ip
) && nvram_match("dhcpd_dmdns", "1");
616 " IgnoreIfMissing on;\n"
617 " AdvSendAdvert on;\n"
618 " MaxRtrAdvInterval 60;\n"
619 " AdvHomeAgentFlag off;\n"
620 " AdvManagedFlag off;\n"
625 " AdvAutonomous on;\n"
629 nvram_safe_get("lan_ifname"),
630 mtu
? " AdvLinkMTU " : "", mtu
? : "", mtu
? ";\n" : "",
632 do_6to4
? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
633 do_6to4
? " Base6to4Interface " : "",
634 do_6to4
? get_wanface() : "",
635 do_6to4
? ";\n" : "");
638 fprintf(f
, " RDNSS %s {};\n", ip
);
641 cnt
= write_ipv6_dns_servers(f
, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
642 if (cnt
== 0 || nvram_get_int("dns_addget"))
643 cnt
+= write_ipv6_dns_servers(f
, (cnt
) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
644 if (cnt
) fprintf(f
, "{};\n");
648 "};\n"); // close "interface" section
653 if (nvram_get_int("debug_ipv6")) {
658 _eval(argv
, NULL
, 0, &pid
);
660 if (!nvram_contains_word("debug_norestart", "radvd")) {
666 void stop_radvd(void)
669 stop_service("radvd");
677 void start_ipv6(void)
681 service
= get_ipv6_service();
684 // Check if turned on
691 case IPV6_NATIVE_DHCP
:
692 case IPV6_ANYCAST_6TO4
:
693 nvram_set("ipv6_rtr_addr", "");
694 nvram_set("ipv6_prefix", "");
698 if (service
!= IPV6_DISABLED
) {
699 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
700 accept_ra(nvram_safe_get("lan_ifname"));
708 eval("ip", "-6", "addr", "flush", "scope", "global");
713 // -----------------------------------------------------------------------------
715 void start_upnp(void)
718 start_service("upnp");
722 if (get_wan_proto() == WP_DISABLED
) return;
728 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
729 mkdir("/etc/upnp", 0777);
730 if (f_exists("/etc/upnp/config.alt")) {
731 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
734 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
735 upnp_port
= nvram_get_int("upnp_port");
736 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
745 "upnp_forward_chain=upnp\n"
746 "upnp_nat_chain=upnp\n"
747 "notify_interval=%d\n"
748 "system_uptime=yes\n"
753 (enable
& 1) ? "yes" : "no", // upnp enable
754 (enable
& 2) ? "yes" : "no", // natpmp enable
755 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
756 nvram_get_int("upnp_ssdp_interval")
759 if (nvram_get_int("upnp_clean")) {
760 int interval
= nvram_get_int("upnp_clean_interval");
761 if (interval
< 60) interval
= 60;
763 "clean_ruleset_interval=%d\n"
764 "clean_ruleset_threshold=%d\n",
766 nvram_get_int("upnp_clean_threshold")
770 fprintf(f
,"clean_ruleset_interval=0\n");
772 if (nvram_match("upnp_mnp", "1")) {
773 int https
= nvram_get_int("https_enable");
774 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
775 https
? "s" : "", nvram_safe_get("lan_ipaddr"),
776 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
779 // Empty parameters are not included into XML service description
780 fprintf(f
, "presentation_url=\n");
784 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
785 fprintf(f
, "uuid=%s\n", uuid
);
788 char lanN_ipaddr
[] = "lanXX_ipaddr";
789 char lanN_netmask
[] = "lanXX_netmask";
790 char upnp_lanN
[] = "upnp_lanXX";
793 for(br
=0 ; br
<4 ; br
++) {
794 char bridge
[2] = "0";
800 sprintf(lanN_ipaddr
, "lan%s_ipaddr", bridge
);
801 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
802 sprintf(upnp_lanN
, "upnp_lan%s", bridge
);
804 char *lanip
= nvram_safe_get(lanN_ipaddr
);
805 char *lanmask
= nvram_safe_get(lanN_netmask
);
806 char *lanlisten
= nvram_safe_get(upnp_lanN
);
807 if((strcmp(lanlisten
,"1")==0) && (strcmp(lanip
,"")!=0) && (strcmp(lanip
,"0.0.0.0")!=0)) {
809 char *lanip
= nvram_safe_get("lan_ipaddr");
810 char *lanmask
= nvram_safe_get("lan_netmask");
813 "listening_ip=%s/%s\n",
816 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
817 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
818 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
819 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
821 "allow %d-%d %s/%s %d-%d\n",
828 // by default allow only redirection of ports above 1024
829 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
836 fappend(f
, "/jffs/upnpconfig.custom");
837 fappend(f
, "/etc/upnp/config.custom");
838 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
841 xstart("miniupnpd", "-f", "/etc/upnp/config");
850 stop_service("upnp");
854 killall_tk("miniupnpd");
857 // -----------------------------------------------------------------------------
859 static pid_t pid_crond
= -1;
861 void start_cron(void)
865 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
866 if (!nvram_contains_word("debug_norestart", "crond")) {
877 // -----------------------------------------------------------------------------
880 static pid_t pid_hotplug2
= -1;
882 void start_hotplug2()
886 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
887 xstart("hotplug2", "--persistent", "--no-coldplug");
888 // FIXME: Don't remember exactly why I put "sleep" here -
889 // but it was not for a race with check_services()... - TB
892 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
897 void stop_hotplug2(void)
900 killall_tk("hotplug2");
904 // -----------------------------------------------------------------------------
906 // Written by Sparq in 2002/07/16
907 void start_zebra(void)
911 start_service("zebra");
917 char *lan_tx
= nvram_safe_get("dr_lan_tx");
918 char *lan_rx
= nvram_safe_get("dr_lan_rx");
920 char *lan1_tx
= nvram_safe_get("dr_lan1_tx");
921 char *lan1_rx
= nvram_safe_get("dr_lan1_rx");
922 char *lan2_tx
= nvram_safe_get("dr_lan2_tx");
923 char *lan2_rx
= nvram_safe_get("dr_lan2_rx");
924 char *lan3_tx
= nvram_safe_get("dr_lan3_tx");
925 char *lan3_rx
= nvram_safe_get("dr_lan3_rx");
927 char *wan_tx
= nvram_safe_get("dr_wan_tx");
928 char *wan_rx
= nvram_safe_get("dr_wan_rx");
931 if ((*lan_tx
== '0') && (*lan_rx
== '0') &&
932 (*lan1_tx
== '0') && (*lan1_rx
== '0') &&
933 (*lan2_tx
== '0') && (*lan2_rx
== '0') &&
934 (*lan3_tx
== '0') && (*lan3_rx
== '0') &&
935 (*wan_tx
== '0') && (*wan_rx
== '0')) {
937 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
943 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
948 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
949 char *lan_ifname
= nvram_safe_get("lan_ifname");
951 char *lan1_ifname
= nvram_safe_get("lan1_ifname");
952 char *lan2_ifname
= nvram_safe_get("lan2_ifname");
953 char *lan3_ifname
= nvram_safe_get("lan3_ifname");
955 char *wan_ifname
= nvram_safe_get("wan_ifname");
957 fprintf(fp
, "router rip\n");
958 if(strcmp(lan_ifname
,"")!=0)
959 fprintf(fp
, "network %s\n", lan_ifname
);
961 if(strcmp(lan1_ifname
,"")!=0)
962 fprintf(fp
, "network %s\n", lan1_ifname
);
963 if(strcmp(lan2_ifname
,"")!=0)
964 fprintf(fp
, "network %s\n", lan2_ifname
);
965 if(strcmp(lan3_ifname
,"")!=0)
966 fprintf(fp
, "network %s\n", lan3_ifname
);
968 fprintf(fp
, "network %s\n", wan_ifname
);
969 fprintf(fp
, "redistribute connected\n");
970 //fprintf(fp, "redistribute static\n");
972 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
973 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
975 if(strcmp(lan_ifname
,"")!=0) {
976 fprintf(fp
, "interface %s\n", lan_ifname
);
977 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
978 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
981 if(strcmp(lan1_ifname
,"")!=0) {
982 fprintf(fp
, "interface %s\n", lan1_ifname
);
983 if (*lan1_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan1_tx
);
984 if (*lan1_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan1_rx
);
986 if(strcmp(lan2_ifname
,"")!=0) {
987 fprintf(fp
, "interface %s\n", lan2_ifname
);
988 if (*lan2_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan2_tx
);
989 if (*lan2_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan2_rx
);
991 if(strcmp(lan3_ifname
,"")!=0) {
992 fprintf(fp
, "interface %s\n", lan3_ifname
);
993 if (*lan3_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan3_tx
);
994 if (*lan3_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan3_rx
);
997 fprintf(fp
, "interface %s\n", wan_ifname
);
998 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
999 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
1001 fprintf(fp
, "router rip\n");
1002 if(strcmp(lan_ifname
,"")!=0) {
1003 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
1004 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
1007 if(strcmp(lan1_ifname
,"")!=0) {
1008 if (*lan1_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan1_ifname
);
1009 if (*lan1_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan1_ifname
);
1011 if(strcmp(lan2_ifname
,"")!=0) {
1012 if (*lan2_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan2_ifname
);
1013 if (*lan2_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan2_ifname
);
1015 if(strcmp(lan3_ifname
,"")!=0) {
1016 if (*lan3_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan3_ifname
);
1017 if (*lan3_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan3_ifname
);
1020 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
1021 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
1022 fprintf(fp
, "access-list private deny any\n");
1024 //fprintf(fp, "debug rip events\n");
1025 //fprintf(fp, "log file /etc/ripd.log\n");
1029 xstart("zebra", "-d");
1030 xstart("ripd", "-d");
1034 void stop_zebra(void)
1036 #ifdef TCONFIG_ZEBRA
1037 if (getpid() != 1) {
1038 stop_service("zebra");
1042 killall("zebra", SIGTERM
);
1043 killall("ripd", SIGTERM
);
1045 unlink("/etc/zebra.conf");
1046 unlink("/etc/ripd.conf");
1050 // -----------------------------------------------------------------------------
1052 void start_syslog(void)
1062 char *rot_siz
= "50";
1063 char *log_file_path
;
1065 argv
[0] = "syslogd";
1068 if (nvram_match("log_remote", "1")) {
1069 nv
= nvram_safe_get("log_remoteip");
1071 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
1072 argv
[argc
++] = "-R";
1077 if (nvram_match("log_file", "1")) {
1078 argv
[argc
++] = "-L";
1080 // log to custom path - shibby
1081 if (nvram_match("log_file_custom", "1")) {
1082 log_file_path
= nvram_safe_get("log_file_path");
1083 argv
[argc
++] = "-s";
1084 argv
[argc
++] = "5000";
1085 argv
[argc
++] = "-b";
1087 argv
[argc
++] = "-O";
1088 argv
[argc
++] = log_file_path
;
1089 remove("/var/log/messages");
1090 symlink(log_file_path
, "/var/log/messages");
1094 /* Read options: rotate_size(kb) num_backups logfilename.
1095 * Ignore these settings and use defaults if the logfile cannot be written to.
1097 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
1098 if ((nv
= strchr(cfg
, '\n')))
1101 if ((nv
= strtok(cfg
, " \t"))) {
1106 if ((nv
= strtok(NULL
, " \t")))
1109 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
1110 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
1111 argv
[argc
++] = "-O";
1121 if (nvram_match("log_file_custom", "0")) {
1122 argv
[argc
++] = "-s";
1123 argv
[argc
++] = rot_siz
;
1124 remove("/var/log/messages");
1128 if (isdigit(*b_opt
)) {
1129 argv
[argc
++] = "-b";
1130 argv
[argc
++] = b_opt
;
1136 _eval(argv
, NULL
, 0, NULL
);
1140 _eval(argv
, NULL
, 0, NULL
);
1142 // used to be available in syslogd -m
1143 n
= nvram_get_int("log_mark");
1147 sprintf(rem
, "*/%d * * * *", n
);
1148 else if (n
< 60 * 24)
1149 sprintf(rem
, "0 */%d * * *", n
/ 60);
1151 sprintf(rem
, "0 0 */%d * *", n
/ (60 * 24));
1152 sprintf(s
, "%s logger -p syslog.info -- -- MARK --", rem
);
1153 eval("cru", "a", "syslogdmark", s
);
1156 eval("cru", "d", "syslogdmark");
1161 void stop_syslog(void)
1163 killall("klogd", SIGTERM
);
1164 killall("syslogd", SIGTERM
);
1167 // -----------------------------------------------------------------------------
1169 static pid_t pid_igmp
= -1;
1171 void start_igmp_proxy(void)
1176 if (nvram_match("multicast_pass", "1")) {
1177 if (get_wan_proto() == WP_DISABLED
)
1180 if (f_exists("/etc/igmp.alt")) {
1181 eval("igmpproxy", "/etc/igmp.alt");
1183 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
1186 "phyint %s upstream\n"
1188 // "phyint %s downstream ratelimit 0\n",
1190 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1191 // nvram_safe_get("lan_ifname"));
1194 char lanN_ifname
[] = "lanXX_ifname";
1195 char multicast_lanN
[] = "multicast_lanXX";
1198 for(br
=0 ; br
<4 ; br
++) {
1199 char bridge
[2] = "0";
1205 sprintf(lanN_ifname
, "lan%s_ifname", bridge
);
1206 sprintf(multicast_lanN
, "multicast_lan%s", bridge
);
1208 if((strcmp(nvram_safe_get(multicast_lanN
),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname
),"")!=0)) {
1210 "phyint %s downstream ratelimit 0\n",
1211 nvram_safe_get(lanN_ifname
));
1216 "phyint %s downstream ratelimit 0\n",
1217 nvram_safe_get("lan_ifname"));
1220 eval("igmpproxy", "/etc/igmp.conf");
1225 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1231 void stop_igmp_proxy(void)
1234 killall_tk("igmpproxy");
1237 #ifdef TCONFIG_NOCAT
1239 static pid_t pid_splashd
= -1;
1240 void start_splashd(void)
1244 if (!nvram_contains_word("debug_norestart", "splashd")) {
1249 void stop_splashd(void)
1256 // -----------------------------------------------------------------------------
1260 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
1263 void start_ntpc(void)
1269 if (nvram_get_int("ntp_updates") >= 0) {
1270 xstart("ntpsync", "--init");
1274 void stop_ntpc(void)
1276 killall("ntpsync", SIGTERM
);
1279 // -----------------------------------------------------------------------------
1281 static void stop_rstats(void)
1287 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1288 if (kill(pid
, SIGTERM
) != 0) break;
1293 static void start_rstats(int new)
1295 if (nvram_match("rstats_enable", "1")) {
1297 if (new) xstart("rstats", "--new");
1298 else xstart("rstats");
1302 static void stop_cstats(void)
1308 while ((n
-- > 0) && ((pid
= pidof("cstats")) > 0)) {
1309 if (kill(pid
, SIGTERM
) != 0) break;
1314 static void start_cstats(int new)
1316 if (nvram_match("cstats_enable", "1")) {
1318 if (new) xstart("cstats", "--new");
1319 else xstart("cstats");
1323 // -----------------------------------------------------------------------------
1325 // !!TB - FTP Server
1328 static char *get_full_storage_path(char *val
)
1330 static char buf
[128];
1334 len
= sprintf(buf
, "%s", val
);
1336 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1338 if (len
> 1 && buf
[len
- 1] == '/')
1344 static char *nvram_storage_path(char *var
)
1346 char *val
= nvram_safe_get(var
);
1347 return get_full_storage_path(val
);
1350 char vsftpd_conf
[] = "/etc/vsftpd.conf";
1351 char vsftpd_users
[] = "/etc/vsftpd.users";
1352 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
1354 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1356 static void start_ftpd(void)
1362 char *user
, *pass
, *rights
, *root_dir
;
1365 if (getpid() != 1) {
1366 start_service("ftpd");
1370 if (!nvram_get_int("ftp_enable")) return;
1372 mkdir_if_none(vsftpd_users
);
1373 mkdir_if_none("/var/run/vsftpd");
1375 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
1378 if (nvram_get_int("ftp_super"))
1381 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1382 if ((f
= fopen(tmp
, "w")))
1385 "dirlist_enable=yes\n"
1386 "write_enable=yes\n"
1387 "download_enable=yes\n");
1392 #ifdef TCONFIG_SAMBASRV
1393 if (nvram_match("smbd_cset", "utf8"))
1394 fprintf(fp
, "utf8=yes\n");
1397 if (nvram_invmatch("ftp_anonymous", "0"))
1400 "anon_allow_writable_root=yes\n"
1401 "anon_world_readable_only=no\n"
1402 "anon_umask=022\n");
1405 sprintf(tmp
, "%s/ftp", vsftpd_users
);
1406 if ((f
= fopen(tmp
, "w")))
1408 if (nvram_match("ftp_dirlist", "0"))
1409 fprintf(f
, "dirlist_enable=yes\n");
1410 if (nvram_match("ftp_anonymous", "1") ||
1411 nvram_match("ftp_anonymous", "3"))
1412 fprintf(f
, "write_enable=yes\n");
1413 if (nvram_match("ftp_anonymous", "1") ||
1414 nvram_match("ftp_anonymous", "2"))
1415 fprintf(f
, "download_enable=yes\n");
1418 if (nvram_match("ftp_anonymous", "1") ||
1419 nvram_match("ftp_anonymous", "3"))
1421 "anon_upload_enable=yes\n"
1422 "anon_mkdir_write_enable=yes\n"
1423 "anon_other_write_enable=yes\n");
1425 fprintf(fp
, "anonymous_enable=no\n");
1429 "dirmessage_enable=yes\n"
1430 "download_enable=no\n"
1431 "dirlist_enable=no\n"
1433 "syslog_enable=yes\n"
1434 "local_enable=yes\n"
1437 "chroot_local_user=yes\n"
1439 "log_ftp_protocol=%s\n"
1440 "user_config_dir=%s\n"
1448 "max_login_fails=1\n"
1449 "idle_session_timeout=%s\n"
1451 "anon_max_rate=%d\n"
1452 "local_max_rate=%d\n"
1454 nvram_get_int("log_ftp") ? "yes" : "no",
1455 vsftpd_users
, vsftpd_passwd
,
1457 ipv6_enabled() ? "_ipv6" : "",
1461 nvram_get("ftp_port") ? : "21",
1462 nvram_get_int("ftp_max"),
1463 nvram_get_int("ftp_ipmax"),
1464 nvram_get("ftp_staytimeout") ? : "300",
1465 nvram_get_int("ftp_anonrate") * 1024,
1466 nvram_get_int("ftp_rate") * 1024,
1467 nvram_safe_get("ftp_custom"));
1471 /* prepare passwd file and default users */
1472 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
1475 if (((user
= nvram_get("http_username")) == NULL
) || (*user
== 0)) user
= "admin";
1476 if (((pass
= nvram_get("http_passwd")) == NULL
) || (*pass
== 0)) pass
= "admin";
1478 fprintf(fp
, /* anonymous, admin, nobody */
1479 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1480 "%s:%s:0:0:root:/:/sbin/nologin\n"
1481 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1482 nvram_storage_path("ftp_anonroot"), user
,
1483 nvram_get_int("ftp_super") ? crypt(pass
, "$1$") : "x",
1486 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1489 username<password<rights[<root_dir]
1497 while ((q
= strsep(&p
, ">")) != NULL
) {
1498 i
= vstrsep(q
, "<", &user
, &pass
, &rights
, &root_dir
);
1499 if (i
< 3 || i
> 4) continue;
1500 if (!user
|| !pass
) continue;
1502 if (i
== 3 || !root_dir
|| !(*root_dir
))
1503 root_dir
= nvram_safe_get("ftp_pubroot");
1506 if (strncmp(rights
, "Private", 7) == 0)
1508 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
1512 sprintf(tmp
, "%s", get_full_storage_path(root_dir
));
1514 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1515 user
, crypt(pass
, "$1$"), user
, tmp
);
1518 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1519 if ((f
= fopen(tmp
, "w")))
1522 if (nvram_invmatch("ftp_dirlist", "1"))
1523 strcat(tmp
, "dirlist_enable=yes\n");
1524 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
1525 strcat(tmp
, "download_enable=yes\n");
1526 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
1527 strcat(tmp
, "write_enable=yes\n");
1537 killall("vsftpd", SIGHUP
);
1539 /* start vsftpd if it's not already running */
1540 if (pidof("vsftpd") <= 0)
1544 static void stop_ftpd(void)
1546 if (getpid() != 1) {
1547 stop_service("ftpd");
1551 killall_tk("vsftpd");
1552 unlink(vsftpd_passwd
);
1553 unlink(vsftpd_conf
);
1554 eval("rm", "-rf", vsftpd_users
);
1556 #endif // TCONFIG_FTP
1558 // -----------------------------------------------------------------------------
1562 #ifdef TCONFIG_SAMBASRV
1563 static void kill_samba(int sig
)
1565 if (sig
== SIGTERM
) {
1570 killall("smbd", sig
);
1571 killall("nmbd", sig
);
1575 static void start_samba(void)
1584 if (getpid() != 1) {
1585 start_service("smbd");
1589 mode
= nvram_get_int("smbd_enable");
1590 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1593 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1596 fprintf(fp
, "[global]\n"
1597 " interfaces = %s\n"
1598 " bind interfaces only = yes\n"
1600 " netbios name = %s\n"
1601 " server string = %s\n"
1602 " guest account = nobody\n"
1603 " security = user\n"
1606 " guest only = no\n"
1607 " browseable = yes\n"
1608 " syslog only = yes\n"
1609 " timestamp logs = no\n"
1611 " encrypt passwords = yes\n"
1612 " preserve case = yes\n"
1613 " short preserve case = yes\n",
1614 nvram_safe_get("lan_ifname"),
1615 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1616 nvram_safe_get("lan_hostname"),
1617 nvram_get("router_name") ? : "Tomato",
1618 mode
== 2 ? "" : "map to guest = Bad User",
1619 mode
== 2 ? "no" : "yes" // guest ok
1622 if (nvram_get_int("smbd_wins")) {
1623 nv
= nvram_safe_get("wan_wins");
1624 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1625 fprintf(fp
, " wins support = yes\n");
1629 if (nvram_get_int("smbd_master")) {
1631 " domain master = yes\n"
1632 " local master = yes\n"
1633 " preferred master = yes\n"
1634 " os level = 65\n");
1637 nv
= nvram_safe_get("smbd_cpage");
1639 #ifndef TCONFIG_SAMBA3
1640 fprintf(fp
, " client code page = %s\n", nv
);
1642 sprintf(nlsmod
, "nls_cp%s", nv
);
1644 nv
= nvram_safe_get("smbd_nlsmod");
1645 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1649 nvram_set("smbd_nlsmod", nlsmod
);
1652 #ifndef TCONFIG_SAMBA3
1653 if (nvram_match("smbd_cset", "utf8"))
1654 fprintf(fp
, " coding system = utf8\n");
1655 else if (nvram_invmatch("smbd_cset", ""))
1656 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1659 nv
= nvram_safe_get("smbd_custom");
1660 /* add socket options unless overriden by the user */
1661 if (strstr(nv
, "socket options") == NULL
) {
1662 fprintf(fp
, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1664 fprintf(fp
, "%s\n\n", nv
);
1666 /* configure shares */
1670 char *name
, *path
, *comment
, *writeable
, *hidden
;
1673 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1675 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1678 while ((q
= strsep(&p
, ">")) != NULL
) {
1679 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1680 if (!path
|| !name
) continue;
1683 fprintf(fp
, "\n[%s]\n", name
);
1686 fprintf(fp
, " path = %s\n", path
);
1689 if (!strcmp(writeable
, "1"))
1690 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1691 if (!strcmp(hidden
, "1"))
1692 fprintf(fp
, " browseable = no\n");
1696 fprintf(fp
, " comment = %s\n", comment
);
1703 /* Share every mountpoint below MOUNT_ROOT */
1704 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1705 while ((dp
= readdir(dir
))) {
1706 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1708 /* Only if is a directory and is mounted */
1709 if (!dir_is_mountpoint(MOUNT_ROOT
, dp
->d_name
))
1712 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1713 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1714 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1715 if (nvram_match("smbd_autoshare", "3")) // Hidden
1716 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1717 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1718 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1719 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1725 if (dir
) closedir(dir
);
1728 /* by default share MOUNT_ROOT as read-only */
1729 fprintf(fp
, "\n[share]\n"
1737 mkdir_if_none("/var/run/samba");
1738 mkdir_if_none("/etc/samba");
1740 /* write smbpasswd */
1741 #ifdef TCONFIG_SAMBA3
1742 eval("smbpasswd", "nobody", "\"\"");
1744 eval("smbpasswd", "-a", "nobody", "\"\"");
1748 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1750 #ifdef TCONFIG_SAMBA3
1751 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1753 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1758 int ret1
= 0, ret2
= 0;
1759 /* start samba if it's not already running */
1760 if (pidof("nmbd") <= 0)
1761 ret1
= xstart("nmbd", "-D");
1762 if (pidof("smbd") <= 0)
1763 ret2
= xstart("smbd", "-D");
1765 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1768 static void stop_samba(void)
1770 if (getpid() != 1) {
1771 stop_service("smbd");
1775 kill_samba(SIGTERM
);
1777 unlink("/var/log/smb");
1778 unlink("/var/log/nmb");
1779 eval("rm", "-rf", "/var/run/samba");
1781 #endif // TCONFIG_SAMBASRV
1783 #ifdef TCONFIG_MEDIA_SERVER
1784 #define MEDIA_SERVER_APP "minidlna"
1786 static void start_media_server(void)
1789 int port
, pid
, https
;
1791 char *argv
[] = { MEDIA_SERVER_APP
, "-f", "/etc/"MEDIA_SERVER_APP
".conf", "-R", NULL
};
1792 static int once
= 1;
1794 if (getpid() != 1) {
1795 start_service("media");
1799 if (nvram_get_int("ms_sas") == 0)
1802 if (nvram_get_int("ms_enable") != 0) {
1803 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
1807 nvram_unset("ms_rescan");
1809 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
1810 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
1813 if ((f
= fopen(argv
[2], "w")) != NULL
) {
1814 port
= nvram_get_int("ms_port");
1815 https
= nvram_get_int("https_enable");
1816 dbdir
= nvram_safe_get("ms_dbdir");
1817 if (!(*dbdir
)) dbdir
= NULL
;
1818 mkdir_if_none(dbdir
? : "/var/run/"MEDIA_SERVER_APP
);
1821 "network_interface=%s\n"
1823 "friendly_name=%s\n"
1827 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1829 "notify_interval=600\n"
1830 "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"
1832 nvram_safe_get("lan_ifname"),
1833 (port
< 0) || (port
>= 0xffff) ? 0 : port
,
1834 nvram_get("router_name") ? : "Tomato",
1835 dbdir
? : "/var/run/"MEDIA_SERVER_APP
,
1836 nvram_get_int("ms_tivo") ? "yes" : "no",
1837 nvram_get_int("ms_stdlna") ? "yes" : "no",
1838 https
? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https
? "https_lanport" : "http_lanport")
1841 // media directories
1843 char *path
, *restrict
;
1845 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
1846 /* path<restrict[A|V|P|] */
1849 while ((q
= strsep(&p
, ">")) != NULL
) {
1850 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
1852 fprintf(f
, "media_dir=%s%s%s\n",
1853 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
1862 /* start media server if it's not already running */
1863 if (pidof(MEDIA_SERVER_APP
) <= 0) {
1864 if ((_eval(argv
, NULL
, 0, &pid
) == 0) && (once
)) {
1865 /* If we started the media server successfully, wait 1 sec
1866 * to let it die if it can't open the database file.
1867 * If it's still alive after that, assume it's running and
1868 * disable forced once-after-reboot rescan.
1871 if (pidof(MEDIA_SERVER_APP
) > 0)
1878 static void stop_media_server(void)
1880 if (getpid() != 1) {
1881 stop_service("media");
1885 killall_tk(MEDIA_SERVER_APP
);
1887 #endif // TCONFIG_MEDIA_SERVER
1890 static void start_nas_services(void)
1892 if (getpid() != 1) {
1893 start_service("usbapps");
1897 #ifdef TCONFIG_SAMBASRV
1903 #ifdef TCONFIG_MEDIA_SERVER
1904 start_media_server();
1908 static void stop_nas_services(void)
1910 if (getpid() != 1) {
1911 stop_service("usbapps");
1915 #ifdef TCONFIG_MEDIA_SERVER
1916 stop_media_server();
1921 #ifdef TCONFIG_SAMBASRV
1926 void restart_nas_services(int stop
, int start
)
1928 int fd
= file_lock("usb");
1929 /* restart all NAS applications */
1931 stop_nas_services();
1933 start_nas_services();
1936 #endif // TCONFIG_USB
1938 // -----------------------------------------------------------------------------
1940 /* -1 = Don't check for this program, it is not expected to be running.
1941 * Other = This program has been started and should be kept running. If no
1942 * process with the name is running, call func to restart it.
1943 * Note: At startup, dnsmasq forks a short-lived child which forks a
1944 * long-lived (grand)child. The parents terminate.
1945 * Many daemons use this technique.
1947 static void _check(pid_t pid
, const char *name
, void (*func
)(void))
1949 if (pid
== -1) return;
1951 if (pidof(name
) > 0) return;
1953 syslog(LOG_DEBUG
, "%s terminated unexpectedly, restarting.\n", name
);
1956 // Force recheck in 500 msec
1957 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
1960 void check_services(void)
1962 TRACE_PT("keep alive\n");
1964 // Periodically reap any zombies
1965 setitimer(ITIMER_REAL
, &zombie_tv
, NULL
);
1968 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
1970 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1971 _check(pid_crond
, "crond", start_cron
);
1972 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
1974 _check(pid_radvd
, "radvd", start_radvd
);
1977 // #ifdef TCONFIG_NOCAT
1978 // if (nvram_get_int("NC_enable"))
1979 // _check(&pid_splashd, "splashd", start_splashd);
1984 // -----------------------------------------------------------------------------
1986 void start_services(void)
1988 static int once
= 1;
1993 if (nvram_get_int("telnetd_eas")) start_telnetd();
1994 if (nvram_get_int("sshd_eas")) start_sshd();
2009 /* note: starting radvd here might be too early in case of
2010 * DHCPv6 or 6to4 because we won't have received a prefix and
2011 * so it will disable advertisements. To restart them, we have
2012 * to send radvd a SIGHUP, or restart it.
2016 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2024 void stop_services(void)
2027 // restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2052 // -----------------------------------------------------------------------------
2054 /* nvram "action_service" is: "service-action[-modifier]"
2055 * action is something like "stop" or "start" or "restart"
2056 * optional modifier is "c" for the "service" command-line command
2058 void exec_service(void)
2060 const int A_START
= 1;
2061 const int A_STOP
= 2;
2062 const int A_RESTART
= 1|2;
2071 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
2075 act
= strsep(&next
, ",");
2076 service
= strsep(&act
, "-");
2082 strsep(&modifier
, "-");
2084 TRACE_PT("service=%s action=%s modifier=%s\n", service
, act
, modifier
? : "");
2086 if (strcmp(act
, "start") == 0) action
= A_START
;
2087 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
2088 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
2090 user
= (modifier
!= NULL
&& *modifier
== 'c');
2092 if (strcmp(service
, "dhcpc") == 0) {
2093 if (action
& A_STOP
) stop_dhcpc();
2094 if (action
& A_START
) start_dhcpc();
2098 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
2099 if (action
& A_STOP
) stop_dnsmasq();
2100 if (action
& A_START
) {
2107 if (strcmp(service
, "firewall") == 0) {
2108 if (action
& A_STOP
) {
2112 if (action
& A_START
) {
2119 if (strcmp(service
, "bwclimon") == 0) {
2120 if (action
& A_STOP
) stop_bwclimon();
2121 if (action
& A_START
) start_bwclimon();
2125 if (strcmp(service
, "account") == 0) {
2126 if (action
& A_STOP
) stop_account();
2127 if (action
& A_START
) start_account();
2131 if (strcmp(service
, "arpbind") == 0) {
2132 if (action
& A_STOP
) stop_arpbind();
2133 if (action
& A_START
) start_arpbind();
2137 if (strcmp(service
, "restrict") == 0) {
2138 if (action
& A_STOP
) {
2141 if (action
& A_START
) {
2142 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2146 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2148 if (nvram_get_int("rrules_radio") < 0) {
2149 eval("radio", "on");
2156 if (strcmp(service
, "qos") == 0) {
2157 if (action
& A_STOP
) {
2160 stop_firewall(); start_firewall(); // always restarted
2161 if (action
& A_START
) {
2163 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2168 if (strcmp(service
, "qoslimit") == 0) {
2169 if (action
& A_STOP
) {
2172 stop_firewall(); start_firewall(); // always restarted
2173 if (action
& A_START
) {
2179 if (strcmp(service
, "arpbind") == 0) {
2180 if (action
& A_STOP
) stop_arpbind();
2181 if (action
& A_START
) stop_arpbind();
2185 if (strcmp(service
, "upnp") == 0) {
2186 if (action
& A_STOP
) {
2189 stop_firewall(); start_firewall(); // always restarted
2190 if (action
& A_START
) {
2196 if (strcmp(service
, "telnetd") == 0) {
2197 if (action
& A_STOP
) stop_telnetd();
2198 if (action
& A_START
) start_telnetd();
2202 if (strcmp(service
, "sshd") == 0) {
2203 if (action
& A_STOP
) stop_sshd();
2204 if (action
& A_START
) start_sshd();
2208 if (strcmp(service
, "httpd") == 0) {
2209 if (action
& A_STOP
) stop_httpd();
2210 if (action
& A_START
) start_httpd();
2215 if (strcmp(service
, "ipv6") == 0) {
2216 if (action
& A_STOP
) {
2220 if (action
& A_START
) {
2227 if (strcmp(service
, "radvd") == 0) {
2228 if (action
& A_STOP
) {
2231 if (action
& A_START
) {
2237 if (strncmp(service
, "dhcp6", 5) == 0) {
2238 if (action
& A_STOP
) {
2241 if (action
& A_START
) {
2248 if (strcmp(service
, "admin") == 0) {
2249 if (action
& A_STOP
) {
2254 stop_firewall(); start_firewall(); // always restarted
2255 if (action
& A_START
) {
2258 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2259 if (nvram_match("sshd_eas", "1")) start_sshd();
2264 if (strcmp(service
, "ddns") == 0) {
2265 if (action
& A_STOP
) stop_ddns();
2266 if (action
& A_START
) start_ddns();
2270 if (strcmp(service
, "ntpc") == 0) {
2271 if (action
& A_STOP
) stop_ntpc();
2272 if (action
& A_START
) start_ntpc();
2276 if (strcmp(service
, "logging") == 0) {
2277 if (action
& A_STOP
) {
2280 if (action
& A_START
) {
2284 // always restarted except from "service" command
2285 stop_cron(); start_cron();
2286 stop_firewall(); start_firewall();
2291 if (strcmp(service
, "crond") == 0) {
2292 if (action
& A_STOP
) {
2295 if (action
& A_START
) {
2302 if (strncmp(service
, "hotplug", 7) == 0) {
2303 if (action
& A_STOP
) {
2306 if (action
& A_START
) {
2313 if (strcmp(service
, "upgrade") == 0) {
2314 if (action
& A_START
) {
2319 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2327 killall("rstats", SIGTERM
);
2328 killall("cstats", SIGTERM
);
2329 killall("buttons", SIGTERM
);
2331 remove_storage_main(1); // !!TB - USB Support
2332 stop_usb(); // !!TB - USB Support
2338 if (strcmp(service
, "cifs") == 0) {
2339 if (action
& A_STOP
) stop_cifs();
2340 if (action
& A_START
) start_cifs();
2345 #ifdef TCONFIG_JFFS2
2346 if (strncmp(service
, "jffs", 4) == 0) {
2347 if (action
& A_STOP
) stop_jffs2();
2348 if (action
& A_START
) start_jffs2();
2353 if (strcmp(service
, "zebra") == 0) {
2354 if (action
& A_STOP
) stop_zebra();
2355 if (action
& A_START
) start_zebra();
2359 if (strcmp(service
, "routing") == 0) {
2360 if (action
& A_STOP
) {
2362 do_static_routes(0); // remove old '_saved'
2363 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2365 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2366 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2367 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2368 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2369 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2370 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2375 if (action
& A_START
) {
2376 do_static_routes(1); // add new
2378 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2380 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2381 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2382 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2383 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2384 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2385 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2391 if (strcmp(service
, "ctnf") == 0) {
2392 if (action
& A_START
) {
2400 if (strcmp(service
, "wan") == 0) {
2401 if (action
& A_STOP
) {
2405 if (action
& A_START
) {
2406 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2414 if (strcmp(service
, "net") == 0) {
2415 if (action
& A_STOP
) {
2417 stop_nas_services();
2430 if (action
& A_START
) {
2443 start_nas_services();
2449 if (strcmp(service
, "nas") == 0) {
2450 if (action
& A_STOP
) {
2453 if (action
& A_START
) {
2460 if (strcmp(service
, "rstats") == 0) {
2461 if (action
& A_STOP
) stop_rstats();
2462 if (action
& A_START
) start_rstats(0);
2466 if (strcmp(service
, "rstatsnew") == 0) {
2467 if (action
& A_STOP
) stop_rstats();
2468 if (action
& A_START
) start_rstats(1);
2472 if (strcmp(service
, "cstats") == 0) {
2473 if (action
& A_STOP
) stop_cstats();
2474 if (action
& A_START
) start_cstats(0);
2478 if (strcmp(service
, "cstatsnew") == 0) {
2479 if (action
& A_STOP
) stop_cstats();
2480 if (action
& A_START
) start_cstats(1);
2484 if (strcmp(service
, "sched") == 0) {
2485 if (action
& A_STOP
) stop_sched();
2486 if (action
& A_START
) start_sched();
2492 if (strcmp(service
, "snmp") == 0) {
2493 if (action
& A_STOP
) stop_snmp();
2494 if (action
& A_START
) start_snmp();
2501 // !!TB - USB Support
2502 if (strcmp(service
, "usb") == 0) {
2503 if (action
& A_STOP
) stop_usb();
2504 if (action
& A_START
) {
2506 // restart Samba and ftp since they may be killed by stop_usb()
2507 restart_nas_services(0, 1);
2508 // remount all partitions by simulating hotplug event
2509 add_remove_usbhost("-1", 1);
2514 if (strcmp(service
, "usbapps") == 0) {
2515 if (action
& A_STOP
) stop_nas_services();
2516 if (action
& A_START
) start_nas_services();
2522 // !!TB - FTP Server
2523 if (strcmp(service
, "ftpd") == 0) {
2524 if (action
& A_STOP
) stop_ftpd();
2528 if (action
& A_START
) start_ftpd();
2533 #ifdef TCONFIG_MEDIA_SERVER
2534 if (strcmp(service
, "media") == 0 || strcmp(service
, "dlna") == 0) {
2535 if (action
& A_STOP
) stop_media_server();
2536 if (action
& A_START
) start_media_server();
2541 #ifdef TCONFIG_SAMBASRV
2543 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2544 if (action
& A_STOP
) stop_samba();
2545 if (action
& A_START
) {
2555 #ifdef TCONFIG_OPENVPN
2556 if (strncmp(service
, "vpnclient", 9) == 0) {
2557 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
2558 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
2562 if (strncmp(service
, "vpnserver", 9) == 0) {
2563 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
2564 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
2569 #ifdef TCONFIG_NOCAT
2570 if (strcmp(service
, "splashd") == 0) {
2571 if (action
& A_STOP
) stop_splashd();
2572 if (action
& A_START
) start_splashd();
2580 // some functions check action_service and must be cleared at end -- zzz
2581 nvram_set("action_service", "");
2583 // Force recheck in 500 msec
2584 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2587 static void do_service(const char *name
, const char *action
, int user
)
2593 while (!nvram_match("action_service", "")) {
2598 else if (--n
< 0) break;
2602 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2603 nvram_set("action_service", s
);
2607 while (nvram_match("action_service", s
)) {
2619 int service_main(int argc
, char *argv
[])
2621 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
2622 do_service(argv
[1], argv
[2], 1);
2623 printf("\nDone.\n");
2627 void start_service(const char *name
)
2629 do_service(name
, "start", 0);
2632 void stop_service(const char *name
)
2634 do_service(name
, "stop", 0);
2638 void restart_service(const char *name)
2640 do_service(name, "restart", 0);