Add Xiaomi R1D support
[tomato.git] / release / src-rt-6.x.4708 / router / rc / services.c
blob5dfd8d201feb5991c27552abab6081b3f2939f4f
1 /*
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
22 All Rights Reserved.
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
36 #include "rc.h"
38 #include <arpa/inet.h>
39 #include <time.h>
40 #include <sys/time.h>
41 #include <errno.h>
43 // !!TB
44 #include <sys/mount.h>
45 #include <mntent.h>
46 #include <dirent.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");
67 void start_dnsmasq()
69 FILE *f;
70 const char *nv;
71 char buf[512];
72 char lan[24];
73 const char *router_ip;
74 char sdhcp_lease[32];
75 char *e;
76 int n;
77 char *mac, *ip, *name;
78 char *p;
79 int ipn;
80 char ipbuf[32];
81 FILE *hf, *df;
82 int dhcp_start;
83 int dhcp_count;
84 int dhcp_lease;
85 int do_dhcpd;
86 int do_dns;
87 int do_dhcpd_hosts;
89 #ifdef TCONFIG_IPV6
90 char *prefix, *ipv6, *mtu;
91 int do_6to4, do_6rd;
92 int service;
93 #endif
95 TRACE_PT("begin\n");
97 if (getpid() != 1) {
98 start_service("dnsmasq");
99 return;
102 stop_dnsmasq();
104 if (foreach_wif(1, NULL, is_wet)) return;
106 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
108 router_ip = nvram_safe_get("lan_ipaddr");
110 fprintf(f,
111 "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);
116 // dns
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);
120 else n = 4096;
121 fprintf(f,
122 "resolv-file=%s\n" // the real stuff is here
123 "addn-hosts=%s\n" // directory with additional hosts files
124 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
125 "expand-hosts\n" // expand hostnames in hosts file
126 "min-port=%u\n", // min port used for random src port
127 dmresolv, dmhosts, dmdhcp, n);
128 do_dns = nvram_match("dhcpd_dmdns", "1");
130 // DNS rebinding protection, will discard upstream RFC1918 responses
131 if (nvram_get_int("dns_norebind")) {
132 fprintf(f,
133 "stop-dns-rebind\n"
134 "rebind-localhost-ok\n");
135 // allow RFC1918 responses for server domain
136 switch (get_wan_proto()) {
137 case WP_PPTP:
138 nv = nvram_get("pptp_server_ip");
139 break;
140 case WP_L2TP:
141 nv = nvram_get("l2tp_server_ip");
142 break;
143 default:
144 nv = NULL;
145 break;
147 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
150 #ifdef TCONFIG_DNSCRYPT
151 if (nvram_match("dnscrypt_proxy", "1")) {
152 fprintf(f, "server=127.0.0.1#%s\n", nvram_safe_get("dnscrypt_port") );
154 #endif
156 for (n = 0 ; n < dns->count; ++n) {
157 if (dns->dns[n].port != 53) {
158 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
162 if (nvram_get_int("dhcpd_static_only")) {
163 fprintf(f, "dhcp-ignore=tag:!known\n");
166 if ((n = nvram_get_int("dnsmasq_q"))) { //process quiet flags
167 if (n & 1) fprintf(f, "quiet-dhcp\n");
168 if (n & 2) fprintf(f, "quiet-dhcp6\n");
169 if (n & 4) fprintf(f, "quiet-ra\n");
172 // dhcp
173 do_dhcpd_hosts=0;
174 char lanN_proto[] = "lanXX_proto";
175 char lanN_ifname[] = "lanXX_ifname";
176 char lanN_ipaddr[] = "lanXX_ipaddr";
177 char lanN_netmask[] = "lanXX_netmask";
178 char dhcpdN_startip[] = "dhcpdXX_startip";
179 char dhcpdN_endip[] = "dhcpdXX_endip";
180 char dhcpN_start[] = "dhcpXX_start";
181 char dhcpN_num[] = "dhcpXX_num";
182 char dhcpN_lease[] = "dhcpXX_lease";
183 char br;
184 for(br=0 ; br<=3 ; br++) {
185 char bridge[2] = "0";
186 if (br!=0)
187 bridge[0]+=br;
188 else
189 strcpy(bridge, "");
191 sprintf(lanN_proto, "lan%s_proto", bridge);
192 sprintf(lanN_ifname, "lan%s_ifname", bridge);
193 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
194 do_dhcpd = nvram_match(lanN_proto, "dhcp");
195 if (do_dhcpd) {
196 do_dhcpd_hosts++;
198 router_ip = nvram_safe_get(lanN_ipaddr);
199 strlcpy(lan, router_ip, sizeof(lan));
200 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
202 fprintf(f,
203 "interface=%s\n",
204 nvram_safe_get(lanN_ifname));
206 sprintf(dhcpN_lease, "dhcp%s_lease", bridge);
207 dhcp_lease = nvram_get_int(dhcpN_lease);
209 if (dhcp_lease <= 0) dhcp_lease = 1440;
211 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
212 if (n < 0) strcpy(sdhcp_lease, "infinite");
213 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
215 if (!do_dns) {
216 // if not using dnsmasq for dns
218 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
219 // no DNS might be temporary. use a low lease time to force clients to update.
220 dhcp_lease = 2;
221 strcpy(sdhcp_lease, "2m");
222 do_dns = 1;
224 else {
225 // pass the dns directly
226 buf[0] = 0;
227 for (n = 0 ; n < dns->count; ++n) {
228 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
229 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
232 fprintf(f, "dhcp-option=tag:%s,6%s\n", nvram_safe_get(lanN_ifname), buf);
236 sprintf(dhcpdN_startip, "dhcpd%s_startip", bridge);
237 sprintf(dhcpdN_endip, "dhcpd%s_endip", bridge);
238 sprintf(lanN_netmask, "lan%s_netmask", bridge);
240 if ((p = nvram_get(dhcpdN_startip)) && (*p) && (e = nvram_get(dhcpdN_endip)) && (*e)) {
241 fprintf(f, "dhcp-range=tag:%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname), p, e, nvram_safe_get(lanN_netmask), dhcp_lease);
243 else {
244 // for compatibility
245 sprintf(dhcpN_start, "dhcp%s_start", bridge);
246 sprintf(dhcpN_num, "dhcp%s_num", bridge);
247 sprintf(lanN_netmask, "lan%s_netmask", bridge);
248 dhcp_start = nvram_get_int(dhcpN_start);
249 dhcp_count = nvram_get_int(dhcpN_num);
250 fprintf(f, "dhcp-range=tag:%s,%s%d,%s%d,%s,%dm\n",
251 nvram_safe_get(lanN_ifname), lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(lanN_netmask), dhcp_lease);
254 nv = nvram_safe_get(lanN_ipaddr);
255 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
256 p = nvram_safe_get("lan_gateway");
257 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
260 fprintf(f,
261 "dhcp-option=tag:%s,3,%s\n", // gateway
262 nvram_safe_get(lanN_ifname), nv);
264 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
265 fprintf(f, "dhcp-option=tag:%s,44,%s\n", nvram_safe_get(lanN_ifname), nv);
267 #ifdef TCONFIG_SAMBASRV
268 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
269 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
270 // Samba will serve as a WINS server
271 fprintf(f, "dhcp-option=tag:%s,44,%s\n", nvram_safe_get(lanN_ifname), nvram_safe_get(lanN_ipaddr));
274 #endif
275 } else {
276 if (strcmp(nvram_safe_get(lanN_ifname),"")!=0) {
277 fprintf(f, "interface=%s\n", nvram_safe_get(lanN_ifname));
278 // if no dhcp range is set then no dhcp service will be offered so following
279 // line is superflous.
280 // fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname));
284 // write static lease entries & create hosts file
286 mkdir_if_none(dmhosts);
287 snprintf(buf, sizeof(buf), "%s/hosts", dmhosts);
288 if ((hf = fopen(buf, "w")) != NULL) {
289 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
290 fprintf(hf, "%s %s\n", router_ip, nv);
291 #ifdef TCONFIG_SAMBASRV
292 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
293 fprintf(hf, "%s %s\n", router_ip, nv);
294 #endif
295 p = (char *)get_wanip();
296 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
297 p = "127.0.0.1";
298 fprintf(hf, "%s wan-ip\n", p);
299 if (nv && (*nv))
300 fprintf(hf, "%s %s-wan\n", p, nv);
303 mkdir_if_none(dmdhcp);
304 snprintf(buf, sizeof(buf), "%s/dhcp-hosts", dmdhcp);
305 df = fopen(buf, "w");
307 // PREVIOUS/OLD FORMAT:
308 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
309 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
310 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
312 // NEW FORMAT (+static ARP binding after hostname):
313 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 55 w/ delim
314 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
315 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 108 w/ delim
317 p = nvram_safe_get("dhcpd_static");
318 while ((e = strchr(p, '>')) != NULL) {
319 n = (e - p);
320 if (n > 107) {
321 p = e + 1;
322 continue;
325 strncpy(buf, p, n);
326 buf[n] = 0;
327 p = e + 1;
329 if ((e = strchr(buf, '<')) == NULL) continue;
330 *e = 0;
331 mac = buf;
333 ip = e + 1;
334 if ((e = strchr(ip, '<')) == NULL) continue;
335 *e = 0;
336 if (strchr(ip, '.') == NULL) {
337 ipn = atoi(ip);
338 if ((ipn <= 0) || (ipn > 255)) continue;
339 sprintf(ipbuf, "%s%d", lan, ipn);
340 ip = ipbuf;
342 else {
343 if (inet_addr(ip) == INADDR_NONE) continue;
346 name = e + 1;
348 if ((e = strchr(name, '<')) != NULL) {
349 *e = 0;
352 if ((hf) && (*name != 0)) {
353 fprintf(hf, "%s %s\n", ip, name);
356 if ((do_dhcpd_hosts > 0) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
357 if (nvram_get_int("dhcpd_slt") == 0) {
358 fprintf(f, "dhcp-host=%s,%s\n", mac, ip);
359 } else {
360 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
365 if (df) fclose(df);
366 if (hf) fclose(hf);
368 n = nvram_get_int("dhcpd_lmax");
369 fprintf(f,
370 "dhcp-lease-max=%d\n",
371 (n > 0) ? n : 255);
372 if (nvram_get_int("dhcpd_auth") >= 0) {
373 fprintf(f, "dhcp-authoritative\n");
376 #ifdef TCONFIG_DNSSEC
377 if (nvram_match("dnssec_enable", "1")) {
378 fprintf(f, "conf-file=/etc/trust-anchors.conf\n"
379 "dnssec\n"
380 "dnssec-no-timecheck\n");
382 #endif
384 #ifdef TCONFIG_DNSCRYPT
385 if (nvram_match("dnscrypt_proxy", "1")) {
386 if (nvram_match("dnscrypt_priority", "1"))
387 fprintf(f, "strict-order\n");
389 if (nvram_match("dnscrypt_priority", "2"))
390 fprintf(f, "no-resolv\n");
392 #endif
396 #ifdef TCONFIG_OPENVPN
397 write_vpn_dnsmasq_config(f);
398 #endif
400 #ifdef TCONFIG_PPTPD
401 write_pptpd_dnsmasq_config(f);
402 #endif
404 #ifdef TCONFIG_IPV6
405 if (ipv6_enabled()) {
407 service = get_ipv6_service();
408 do_6to4 = (service == IPV6_ANYCAST_6TO4);
409 do_6rd = (service == IPV6_6RD || service == IPV6_6RD_DHCP);
410 mtu = NULL;
412 switch (service) {
413 case IPV6_NATIVE_DHCP:
414 case IPV6_ANYCAST_6TO4:
415 case IPV6_6IN4:
416 case IPV6_6RD:
417 case IPV6_6RD_DHCP:
418 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
419 // fall through
420 default:
421 prefix = do_6to4 ? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
422 break;
424 if (!(*prefix)) prefix = "::";
425 ipv6 = (char *)ipv6_router_address(NULL);
427 fprintf(f, "enable-ra\ndhcp-range=tag:br0,%s, slaac, ra-names, 64\n", prefix);
430 // enable-ra should be enabled in both cases
431 if (nvram_get_int("ipv6_radvd") || nvram_get_int("ipv6_dhcpd"))
432 fprintf(f,"enable-ra\n");
434 // Only SLAAC and NO DHCPv6
435 if (nvram_get_int("ipv6_radvd") && !nvram_get_int("ipv6_dhcpd"))
436 fprintf(f,"dhcp-range=::, constructor:br*, ra-names, ra-stateless, 64, 12h\n");
438 // Only DHCPv6 and NO SLAAC
439 if (nvram_get_int("ipv6_dhcpd") && !nvram_get_int("ipv6_radvd"))
440 fprintf(f,"dhcp-range=::1, ::FFFF:FFFF, constructor:br*, 64, 12h\n");
442 // SLAAC and DHCPv6 (2 IPv6 IPs)
443 if (nvram_get_int("ipv6_radvd") && nvram_get_int("ipv6_dhcpd"))
444 fprintf(f,"dhcp-range=::1, ::FFFF:FFFF, constructor:br*, ra-names, 64, 12h\n");
446 #endif
448 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
450 fappend(f, "/etc/dnsmasq.custom");
451 fappend(f, "/etc/dnsmasq.ipset");
455 fclose(f);
457 if (do_dns) {
458 unlink("/etc/resolv.conf");
459 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
462 TRACE_PT("run dnsmasq\n");
464 // Default to some values we like, but allow the user to override them.
465 eval("dnsmasq", "-c", "1500", "--log-async");
467 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
468 pid_dnsmasq = -2;
471 TRACE_PT("end\n");
473 #ifdef TCONFIG_DNSCRYPT
474 //start dnscrypt-proxy
475 if (nvram_match("dnscrypt_proxy", "1")) {
476 char dnscrypt_local[30];
477 sprintf(dnscrypt_local, "127.0.0.1:%s", nvram_safe_get("dnscrypt_port") );
479 eval("ntp2ip");
481 if (nvram_match("dnscrypt_manual", "1")) {
482 eval("dnscrypt-proxy", "-d",
483 "-a", dnscrypt_local,
484 "-m", nvram_safe_get("dnscrypt_log"),
485 "-N", nvram_safe_get("dnscrypt_provider_name"),
486 "-k", nvram_safe_get("dnscrypt_provider_key"),
487 "-r", nvram_safe_get("dnscrypt_resolver_address") );
488 } else {
489 eval("dnscrypt-proxy", "-d",
490 "-a", dnscrypt_local,
491 "-m", nvram_safe_get("dnscrypt_log"),
492 "-R", nvram_safe_get("dnscrypt_resolver"),
493 "-L", "/etc/dnscrypt-resolvers.csv" );
496 #ifdef TCONFIG_IPV6
497 char dnscrypt_local_ipv6[30];
498 sprintf(dnscrypt_local_ipv6, "::1:%s", nvram_safe_get("dnscrypt_port") );
500 if (get_ipv6_service() != *("NULL")) { // when ipv6 enabled
501 if (nvram_match("dnscrypt_manual", "1")) {
502 eval("dnscrypt-proxy", "-d",
503 "-a", dnscrypt_local,
504 "-m", nvram_safe_get("dnscrypt_log"),
505 "-N", nvram_safe_get("dnscrypt_provider_name"),
506 "-k", nvram_safe_get("dnscrypt_provider_key"),
507 "-r", nvram_safe_get("dnscrypt_resolver_address") );
508 } else {
509 eval("dnscrypt-proxy", "-d",
510 "-a", dnscrypt_local,
511 "-m", nvram_safe_get("dnscrypt_log"),
512 "-R", nvram_safe_get("dnscrypt_resolver"),
513 "-L", "/etc/dnscrypt-resolvers.csv" );
516 #endif
518 #endif
520 #ifdef TCONFIG_DNSSEC
521 if ((time(0) > Y2K) && nvram_match("dnssec_enable", "1")){
522 killall("dnsmasq", SIGHUP);
524 #endif
528 void stop_dnsmasq(void)
530 TRACE_PT("begin\n");
532 if (getpid() != 1) {
533 stop_service("dnsmasq");
534 return;
537 pid_dnsmasq = -1;
539 unlink("/etc/resolv.conf");
540 symlink(dmresolv, "/etc/resolv.conf");
542 killall_tk("dnsmasq");
543 #ifdef TCONFIG_DNSCRYPT
544 killall_tk("dnscrypt-proxy");
545 #endif
547 TRACE_PT("end\n");
550 void clear_resolv(void)
552 f_write(dmresolv, NULL, 0, 0, 0); // blank
555 #ifdef TCONFIG_FANCTRL
556 static pid_t pid_phy_tempsense = -1;
558 void start_phy_tempsense()
560 stop_phy_tempsense();
562 char *phy_tempsense_argv[] = {"phy_tempsense", NULL};
563 _eval(phy_tempsense_argv, NULL, 0, &pid_phy_tempsense);
566 void stop_phy_tempsense()
568 pid_phy_tempsense = -1;
569 killall_tk("phy_tempsense");
571 #endif
574 #ifdef TCONFIG_IPV6
575 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
577 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
578 struct in6_addr addr;
579 int cnt = 0;
581 foreach(p, dns, next) {
582 // verify that this is a valid IPv6 address
583 if (inet_pton(AF_INET6, p, &addr) == 1) {
584 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
585 ++cnt;
589 return cnt;
591 #endif
593 void dns_to_resolv(void)
595 FILE *f;
596 const dns_list_t *dns;
597 int i;
598 mode_t m;
600 m = umask(022); // 077 from pppoecd
601 if ((f = fopen(dmresolv, "w")) != NULL) {
602 // Check for VPN DNS entries
603 if (!write_pptpvpn_resolv(f) && !write_vpn_resolv(f)) {
604 #ifdef TCONFIG_IPV6
605 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
606 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
607 #endif
608 dns = get_dns(); // static buffer
609 if (dns->count == 0) {
610 // Put a pseudo DNS IP to trigger Connect On Demand
611 if (nvram_match("ppp_demand", "1")) {
612 switch (get_wan_proto()) {
613 case WP_PPPOE:
614 case WP_PPP3G:
615 case WP_PPTP:
616 case WP_L2TP:
617 fprintf(f, "nameserver 1.1.1.1\n");
618 break;
622 else {
623 for (i = 0; i < dns->count; i++) {
624 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
625 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
630 fclose(f);
632 umask(m);
635 // -----------------------------------------------------------------------------
637 void start_httpd(void)
639 if (getpid() != 1) {
640 start_service("httpd");
641 return;
644 if( nvram_match( "web_css", "online" ) )
645 xstart( "/usr/sbin/ttb" );
647 stop_httpd();
649 // set www dir
650 if ( nvram_match( "web_dir", "jffs" ) ) { chdir("/jffs/www"); }
651 else if ( nvram_match( "web_dir", "opt" ) ) { chdir("/opt/www"); }
652 else if ( nvram_match( "web_dir", "tmp" ) ) { chdir("/tmp/www");}
653 else { chdir("/www"); }
655 eval("httpd");
656 chdir("/");
659 void stop_httpd(void)
661 if (getpid() != 1) {
662 stop_service("httpd");
663 return;
666 killall_tk("httpd");
669 // -----------------------------------------------------------------------------
670 #ifdef TCONFIG_IPV6
672 static void add_ip6_lanaddr(void)
674 char ip[INET6_ADDRSTRLEN + 4];
675 const char *p;
677 p = ipv6_router_address(NULL);
678 if (*p) {
679 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
680 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
684 void start_ipv6_tunnel(void)
686 char ip[INET6_ADDRSTRLEN + 4];
687 struct in_addr addr4;
688 struct in6_addr addr;
689 const char *wanip, *mtu, *tun_dev;
690 int service;
692 service = get_ipv6_service();
693 tun_dev = get_wan6face();
694 wanip = get_wanip();
695 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
696 modprobe("sit");
698 if (service == IPV6_ANYCAST_6TO4)
699 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
700 else
701 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
702 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
703 "remote", ip,
704 "local", (char *)wanip,
705 "ttl", nvram_safe_get("ipv6_tun_ttl"));
707 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
708 nvram_set("ipv6_ifname", (char *)tun_dev);
710 if (service == IPV6_ANYCAST_6TO4) {
711 add_ip6_lanaddr();
712 addr4.s_addr = 0;
713 memset(&addr, 0, sizeof(addr));
714 inet_aton(wanip, &addr4);
715 addr.s6_addr16[0] = htons(0x2002);
716 ipv6_mapaddr4(&addr, 16, &addr4, 0);
717 addr.s6_addr16[7] = htons(0x0001);
718 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
719 strncat(ip, "/16", sizeof(ip));
721 else {
722 snprintf(ip, sizeof(ip), "%s/%d",
723 nvram_safe_get("ipv6_tun_addr"),
724 nvram_get_int("ipv6_tun_addrlen") ? : 64);
726 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
727 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
729 // (re)start radvd - now dnsmasq provided
730 if (service == IPV6_ANYCAST_6TO4)
731 start_dnsmasq();
734 void stop_ipv6_tunnel(void)
736 eval("ip", "tunnel", "del", (char *)get_wan6face());
737 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
738 // get rid of old IPv6 address from lan iface
739 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
741 modprobe_r("sit");
744 void start_6rd_tunnel(void)
746 const char *tun_dev, *wanip;
747 int service, mask_len, prefix_len, local_prefix_len;
748 char mtu[10], prefix[INET6_ADDRSTRLEN], relay[INET_ADDRSTRLEN];
749 struct in_addr netmask_addr, relay_addr, relay_prefix_addr, wanip_addr;
750 struct in6_addr prefix_addr, local_prefix_addr;
751 char local_prefix[INET6_ADDRSTRLEN];
752 char tmp_ipv6[INET6_ADDRSTRLEN + 4], tmp_ipv4[INET_ADDRSTRLEN + 4];
753 char tmp[256];
754 FILE *f;
756 service = get_ipv6_service();
757 wanip = get_wanip();
758 tun_dev = get_wan6face();
759 sprintf(mtu, "%d", (nvram_get_int("wan_mtu") > 0) ? (nvram_get_int("wan_mtu") - 20) : 1280);
761 // maybe we can merge the ipv6_6rd_* variables into a single ipv_6rd_string (ala wan_6rd)
762 // to save nvram space?
763 if (service == IPV6_6RD) {
764 _dprintf("starting 6rd tunnel using manual settings.\n");
765 mask_len = nvram_get_int("ipv6_6rd_ipv4masklen");
766 prefix_len = nvram_get_int("ipv6_6rd_prefix_length");
767 strcpy(prefix, nvram_safe_get("ipv6_6rd_prefix"));
768 strcpy(relay, nvram_safe_get("ipv6_6rd_borderrelay"));
770 else {
771 _dprintf("starting 6rd tunnel using automatic settings.\n");
772 char *wan_6rd = nvram_safe_get("wan_6rd");
773 if (sscanf(wan_6rd, "%d %d %s %s", &mask_len, &prefix_len, prefix, relay) < 4) {
774 _dprintf("wan_6rd string is missing or invalid (%s)\n", wan_6rd);
775 return;
779 // validate values that were passed
780 if (mask_len < 0 || mask_len > 32) {
781 _dprintf("invalid mask_len value (%d)\n", mask_len);
782 return;
784 if (prefix_len < 0 || prefix_len > 128) {
785 _dprintf("invalid prefix_len value (%d)\n", prefix_len);
786 return;
788 if ((32 - mask_len) + prefix_len > 128) {
789 _dprintf("invalid combination of mask_len and prefix_len!\n");
790 return;
793 sprintf(tmp, "ping -q -c 2 %s | grep packet", relay);
794 if ((f = popen(tmp, "r")) == NULL) {
795 _dprintf("error obtaining data\n");
796 return;
798 fgets(tmp, sizeof(tmp), f);
799 pclose(f);
800 if (strstr(tmp, " 0% packet loss") == NULL) {
801 _dprintf("failed to ping border relay\n");
802 return;
805 // get relay prefix from border relay address and mask
806 netmask_addr.s_addr = htonl(0xffffffff << (32 - mask_len));
807 inet_aton(relay, &relay_addr);
808 relay_prefix_addr.s_addr = relay_addr.s_addr & netmask_addr.s_addr;
810 // calculate the local prefix
811 inet_pton(AF_INET6, prefix, &prefix_addr);
812 inet_pton(AF_INET, wanip, &wanip_addr);
813 if (calc_6rd_local_prefix(&prefix_addr, prefix_len, mask_len,
814 &wanip_addr, &local_prefix_addr, &local_prefix_len) == 0) {
815 _dprintf("error calculating local prefix.");
816 return;
818 inet_ntop(AF_INET6, &local_prefix_addr, local_prefix, sizeof(local_prefix));
820 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1", local_prefix);
821 nvram_set("ipv6_rtr_addr", tmp_ipv6);
822 nvram_set("ipv6_prefix", local_prefix);
824 // load sit module needed for the 6rd tunnel
825 modprobe("sit");
827 // creating the 6rd tunnel
828 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit", "local", (char *)wanip, "ttl", nvram_safe_get("ipv6_tun_ttl"));
830 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s/%d", prefix, prefix_len);
831 snprintf(tmp_ipv4, sizeof(tmp_ipv4), "%s/%d", inet_ntoa(relay_prefix_addr), mask_len);
832 eval("ip", "tunnel" "6rd", "dev", (char *)tun_dev, "6rd-prefix", tmp_ipv6, "6rd-relay_prefix", tmp_ipv4);
834 // bringing up the link
835 eval("ip", "link", "set", "dev", (char *)tun_dev, "mtu", (char *)mtu, "up");
837 // setting the WAN address Note: IPv6 WAN CIDR should be: ((32 - ip6rd_ipv4masklen) + ip6rd_prefixlen)
838 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, local_prefix_len);
839 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", (char *)tun_dev);
841 // setting the LAN address Note: IPv6 LAN CIDR should be 64
842 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, nvram_get_int("ipv6_prefix_length") ? : 64);
843 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", nvram_safe_get("lan_ifname"));
845 // adding default route via the border relay
846 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "::%s", relay);
847 eval("ip", "-6", "route", "add", "default", "via", tmp_ipv6, "dev", (char *)tun_dev);
849 nvram_set("ipv6_ifname", (char *)tun_dev);
851 // (re)start radvd now dnsmasq
852 start_dnsmasq();
854 printf("6rd end\n");
857 void stop_6rd_tunnel(void)
859 eval("ip", "tunnel", "del", (char *)get_wan6face());
860 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
861 modprobe_r("sit");
865 void start_ipv6(void)
867 int service;
869 service = get_ipv6_service();
870 enable_ip6_forward();
872 // Check if turned on
873 switch (service) {
874 case IPV6_NATIVE:
875 case IPV6_6IN4:
876 case IPV6_MANUAL:
877 add_ip6_lanaddr();
878 break;
879 case IPV6_NATIVE_DHCP:
880 case IPV6_ANYCAST_6TO4:
881 nvram_set("ipv6_rtr_addr", "");
882 nvram_set("ipv6_prefix", "");
883 break;
886 if (service != IPV6_DISABLED) {
887 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
888 accept_ra(nvram_safe_get("lan_ifname"));
892 void stop_ipv6(void)
894 stop_ipv6_tunnel();
895 stop_dhcp6c();
896 eval("ip", "-6", "addr", "flush", "scope", "global");
897 eval("ip", "-6", "route", "flush", "scope", "global");
900 #endif
902 // -----------------------------------------------------------------------------
904 void start_upnp(void)
906 if (getpid() != 1) {
907 start_service("upnp");
908 return;
911 if (get_wan_proto() == WP_DISABLED) return;
913 int enable;
914 FILE *f;
915 int upnp_port;
917 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
918 mkdir("/etc/upnp", 0777);
919 if (f_exists("/etc/upnp/config.alt")) {
920 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
922 else {
923 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
924 upnp_port = nvram_get_int("upnp_port");
925 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
928 fprintf(f,
929 "ext_ifname=%s\n"
930 "port=%d\n"
931 "enable_upnp=%s\n"
932 "enable_natpmp=%s\n"
933 "secure_mode=%s\n"
934 "upnp_forward_chain=upnp\n"
935 "upnp_nat_chain=upnp\n"
936 "notify_interval=%d\n"
937 "system_uptime=yes\n"
938 "friendly_name=%s"" Router\n"
939 "model_name=%s\n"
940 "model_url=http://linksysinfo.org/index.php?forums/tomato-firmware.33/\n"
941 "manufacturer_name=Tomato Firmware\n"
942 "manufacturer_url=http://linksysinfo.org/index.php?forums/tomato-firmware.33/\n"
943 "\n"
945 get_wanface(),
946 upnp_port,
947 (enable & 1) ? "yes" : "no", // upnp enable
948 (enable & 2) ? "yes" : "no", // natpmp enable
949 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
950 nvram_get_int("upnp_ssdp_interval"),
951 nvram_safe_get("router_name"),
952 nvram_safe_get("t_model_name")
955 if (nvram_get_int("upnp_clean")) {
956 int interval = nvram_get_int("upnp_clean_interval");
957 if (interval < 60) interval = 60;
958 fprintf(f,
959 "clean_ruleset_interval=%d\n"
960 "clean_ruleset_threshold=%d\n",
961 interval,
962 nvram_get_int("upnp_clean_threshold")
965 else
966 fprintf(f,"clean_ruleset_interval=0\n");
968 if (nvram_match("upnp_mnp", "1")) {
969 int https = nvram_get_int("https_enable");
970 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
971 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
972 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
974 else {
975 // Empty parameters are not included into XML service description
976 fprintf(f, "presentation_url=\n");
979 char uuid[45];
980 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
981 fprintf(f, "uuid=%s\n", uuid);
983 // shibby - move custom configuration before "allow" statements
984 // discussion: http://www.linksysinfo.org/index.php?threads/miniupnpd-custom-config-syntax.70863/#post-256291
985 fappend(f, "/etc/upnp/config.custom");
986 fprintf(f, "%s\n", nvram_safe_get("upnp_custom"));
988 char lanN_ipaddr[] = "lanXX_ipaddr";
989 char lanN_netmask[] = "lanXX_netmask";
990 char upnp_lanN[] = "upnp_lanXX";
991 char br;
993 for(br=0 ; br<4 ; br++) {
994 char bridge[2] = "0";
995 if (br!=0)
996 bridge[0]+=br;
997 else
998 strcpy(bridge, "");
1000 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
1001 sprintf(lanN_netmask, "lan%s_netmask", bridge);
1002 sprintf(upnp_lanN, "upnp_lan%s", bridge);
1004 char *lanip = nvram_safe_get(lanN_ipaddr);
1005 char *lanmask = nvram_safe_get(lanN_netmask);
1006 char *lanlisten = nvram_safe_get(upnp_lanN);
1008 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
1009 fprintf(f,
1010 "listening_ip=%s/%s\n",
1011 lanip, lanmask);
1012 int ports[4];
1013 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
1014 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
1015 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
1016 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
1017 fprintf(f,
1018 "allow %d-%d %s/%s %d-%d\n",
1019 ports[0], ports[1],
1020 lanip, lanmask,
1021 ports[2], ports[3]
1024 else {
1025 // by default allow only redirection of ports above 1024
1026 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
1030 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
1031 fclose(f);
1033 xstart("miniupnpd", "-f", "/etc/upnp/config");
1039 void stop_upnp(void)
1041 if (getpid() != 1) {
1042 stop_service("upnp");
1043 return;
1046 killall_tk("miniupnpd");
1049 // -----------------------------------------------------------------------------
1051 static pid_t pid_crond = -1;
1053 void start_cron(void)
1055 stop_cron();
1057 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
1058 if (!nvram_contains_word("debug_norestart", "crond")) {
1059 pid_crond = -2;
1063 void stop_cron(void)
1065 pid_crond = -1;
1066 killall_tk("crond");
1069 // -----------------------------------------------------------------------------
1070 #ifdef LINUX26
1072 static pid_t pid_hotplug2 = -1;
1074 void start_hotplug2()
1076 stop_hotplug2();
1078 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
1079 xstart("hotplug2", "--persistent", "--no-coldplug");
1080 // FIXME: Don't remember exactly why I put "sleep" here -
1081 // but it was not for a race with check_services()... - TB
1082 sleep(1);
1084 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
1085 pid_hotplug2 = -2;
1089 void stop_hotplug2(void)
1091 pid_hotplug2 = -1;
1092 killall_tk("hotplug2");
1095 #endif /* LINUX26 */
1096 // -----------------------------------------------------------------------------
1098 // Written by Sparq in 2002/07/16
1099 void start_zebra(void)
1101 #ifdef TCONFIG_ZEBRA
1102 if (getpid() != 1) {
1103 start_service("zebra");
1104 return;
1107 FILE *fp;
1109 char *lan_tx = nvram_safe_get("dr_lan_tx");
1110 char *lan_rx = nvram_safe_get("dr_lan_rx");
1111 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
1112 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
1113 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
1114 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
1115 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
1116 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
1117 char *wan_tx = nvram_safe_get("dr_wan_tx");
1118 char *wan_rx = nvram_safe_get("dr_wan_rx");
1120 if ((*lan_tx == '0') && (*lan_rx == '0') &&
1121 (*lan1_tx == '0') && (*lan1_rx == '0') &&
1122 (*lan2_tx == '0') && (*lan2_rx == '0') &&
1123 (*lan3_tx == '0') && (*lan3_rx == '0') &&
1124 (*wan_tx == '0') && (*wan_rx == '0')) {
1125 return;
1128 // empty
1129 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
1130 fclose(fp);
1134 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
1135 char *lan_ifname = nvram_safe_get("lan_ifname");
1136 char *lan1_ifname = nvram_safe_get("lan1_ifname");
1137 char *lan2_ifname = nvram_safe_get("lan2_ifname");
1138 char *lan3_ifname = nvram_safe_get("lan3_ifname");
1139 char *wan_ifname = nvram_safe_get("wan_ifname");
1141 fprintf(fp, "router rip\n");
1142 if(strcmp(lan_ifname,"")!=0)
1143 fprintf(fp, "network %s\n", lan_ifname);
1144 if(strcmp(lan1_ifname,"")!=0)
1145 fprintf(fp, "network %s\n", lan1_ifname);
1146 if(strcmp(lan2_ifname,"")!=0)
1147 fprintf(fp, "network %s\n", lan2_ifname);
1148 if(strcmp(lan3_ifname,"")!=0)
1149 fprintf(fp, "network %s\n", lan3_ifname);
1150 fprintf(fp, "network %s\n", wan_ifname);
1151 fprintf(fp, "redistribute connected\n");
1152 //fprintf(fp, "redistribute static\n");
1154 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
1155 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
1157 if(strcmp(lan_ifname,"")!=0) {
1158 fprintf(fp, "interface %s\n", lan_ifname);
1159 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
1160 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
1162 if(strcmp(lan1_ifname,"")!=0) {
1163 fprintf(fp, "interface %s\n", lan1_ifname);
1164 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
1165 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
1167 if(strcmp(lan2_ifname,"")!=0) {
1168 fprintf(fp, "interface %s\n", lan2_ifname);
1169 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
1170 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
1172 if(strcmp(lan3_ifname,"")!=0) {
1173 fprintf(fp, "interface %s\n", lan3_ifname);
1174 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
1175 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
1177 fprintf(fp, "interface %s\n", wan_ifname);
1178 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
1179 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
1181 fprintf(fp, "router rip\n");
1182 if(strcmp(lan_ifname,"")!=0) {
1183 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
1184 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
1186 if(strcmp(lan1_ifname,"")!=0) {
1187 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
1188 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
1190 if(strcmp(lan2_ifname,"")!=0) {
1191 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
1192 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
1194 if(strcmp(lan3_ifname,"")!=0) {
1195 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
1196 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
1198 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
1199 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
1200 fprintf(fp, "access-list private deny any\n");
1202 //fprintf(fp, "debug rip events\n");
1203 //fprintf(fp, "log file /etc/ripd.log\n");
1204 fclose(fp);
1207 xstart("zebra", "-d");
1208 xstart("ripd", "-d");
1209 #endif
1212 void stop_zebra(void)
1214 #ifdef TCONFIG_ZEBRA
1215 if (getpid() != 1) {
1216 stop_service("zebra");
1217 return;
1220 killall("zebra", SIGTERM);
1221 killall("ripd", SIGTERM);
1223 unlink("/etc/zebra.conf");
1224 unlink("/etc/ripd.conf");
1225 #endif
1228 // -----------------------------------------------------------------------------
1230 void start_syslog(void)
1232 char *argv[16];
1233 int argc;
1234 char *nv;
1235 char *b_opt = "";
1236 char rem[256];
1237 int n;
1238 char s[64];
1239 char cfg[256];
1240 char *rot_siz = "50";
1241 char *rot_keep = "1";
1242 char *log_file_path;
1244 argv[0] = "syslogd";
1245 argc = 1;
1247 if (nvram_match("log_remote", "1")) {
1248 nv = nvram_safe_get("log_remoteip");
1249 if (*nv) {
1250 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1251 argv[argc++] = "-R";
1252 argv[argc++] = rem;
1256 if (nvram_match("log_file", "1")) {
1257 argv[argc++] = "-L";
1259 if (strcmp(nvram_safe_get("log_file_size"), "") != 0) {
1260 rot_siz = nvram_safe_get("log_file_size");
1262 if (nvram_get_int("log_file_size") > 0) {
1263 rot_keep = nvram_safe_get("log_file_keep");
1266 // log to custom path - shibby
1267 if (nvram_match("log_file_custom", "1")) {
1268 log_file_path = nvram_safe_get("log_file_path");
1269 argv[argc++] = "-s";
1270 argv[argc++] = rot_siz;
1271 argv[argc++] = "-O";
1272 argv[argc++] = log_file_path;
1273 if (strcmp(nvram_safe_get("log_file_path"), "/var/log/messages") != 0) {
1274 remove("/var/log/messages");
1275 symlink(log_file_path, "/var/log/messages");
1278 else
1280 /* Read options: rotate_size(kb) num_backups logfilename.
1281 * Ignore these settings and use defaults if the logfile cannot be written to.
1283 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1284 if ((nv = strchr(cfg, '\n')))
1285 *nv = 0;
1287 if ((nv = strtok(cfg, " \t"))) {
1288 if (isdigit(*nv))
1289 rot_siz = nv;
1292 if ((nv = strtok(NULL, " \t")))
1293 b_opt = nv;
1295 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1296 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1297 argv[argc++] = "-O";
1298 argv[argc++] = nv;
1300 else {
1301 rot_siz = "50";
1302 b_opt = "";
1307 if (nvram_match("log_file_custom", "0")) {
1308 argv[argc++] = "-s";
1309 argv[argc++] = rot_siz;
1310 struct stat sb;
1311 if (lstat("/var/log/messages", &sb) != -1)
1312 if (S_ISLNK(sb.st_mode))
1313 remove("/var/log/messages");
1316 if (isdigit(*b_opt)) {
1317 argv[argc++] = "-b";
1318 argv[argc++] = b_opt;
1319 } else
1320 if (nvram_get_int("log_file_size") > 0) {
1321 argv[argc++] = "-b";
1322 argv[argc++] = rot_keep;
1326 if (argc > 1) {
1327 argv[argc] = NULL;
1328 _eval(argv, NULL, 0, NULL);
1330 argv[0] = "klogd";
1331 argv[1] = NULL;
1332 _eval(argv, NULL, 0, NULL);
1334 // used to be available in syslogd -m
1335 n = nvram_get_int("log_mark");
1336 if (n > 0) {
1337 // n is in minutes
1338 if (n < 60)
1339 sprintf(rem, "*/%d * * * *", n);
1340 else if (n < 60 * 24)
1341 sprintf(rem, "0 */%d * * *", n / 60);
1342 else
1343 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1344 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1345 eval("cru", "a", "syslogdmark", s);
1347 else {
1348 eval("cru", "d", "syslogdmark");
1353 void stop_syslog(void)
1355 killall("klogd", SIGTERM);
1356 killall("syslogd", SIGTERM);
1359 // -----------------------------------------------------------------------------
1361 static pid_t pid_igmp = -1;
1363 void start_igmp_proxy(void)
1365 FILE *fp;
1367 pid_igmp = -1;
1368 if (nvram_match("multicast_pass", "1")) {
1369 if (get_wan_proto() == WP_DISABLED)
1370 return;
1372 if (f_exists("/etc/igmp.alt")) {
1373 eval("igmpproxy", "/etc/igmp.alt");
1375 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1376 fprintf(fp,
1377 "quickleave\n"
1378 "phyint %s upstream\n"
1379 "\taltnet %s\n",
1380 // "phyint %s downstream ratelimit 0\n",
1381 get_wanface(),
1382 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1383 // nvram_safe_get("lan_ifname"));
1385 char lanN_ifname[] = "lanXX_ifname";
1386 char multicast_lanN[] = "multicast_lanXX";
1387 char br;
1389 for(br=0 ; br<4 ; br++) {
1390 char bridge[2] = "0";
1391 if (br!=0)
1392 bridge[0]+=br;
1393 else
1394 strcpy(bridge, "");
1396 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1397 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1399 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1400 fprintf(fp,
1401 "phyint %s downstream ratelimit 0\n",
1402 nvram_safe_get(lanN_ifname));
1405 fclose(fp);
1406 eval("igmpproxy", "/etc/igmp.conf");
1408 else {
1409 return;
1411 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1412 pid_igmp = -2;
1417 void stop_igmp_proxy(void)
1419 pid_igmp = -1;
1420 killall_tk("igmpproxy");
1423 // -----------------------------------------------------------------------------
1425 void start_udpxy(void)
1427 if (nvram_match("udpxy_enable", "1")) {
1428 if (get_wan_proto() == WP_DISABLED)
1429 return;
1430 eval("udpxy", (nvram_get_int("udpxy_stats") ? "-S" : ""), "-p", nvram_safe_get("udpxy_port"), "-c", nvram_safe_get("udpxy_clients"), "-m", nvram_safe_get("wan_ifname") );
1434 void stop_udpxy(void)
1436 killall_tk("udpxy");
1439 // -----------------------------------------------------------------------------
1441 #ifdef TCONFIG_NOCAT
1443 static pid_t pid_splashd = -1;
1444 void start_splashd(void)
1446 pid_splashd = -1;
1447 start_nocat();
1448 if (!nvram_contains_word("debug_norestart", "splashd")) {
1449 pid_splashd = -2;
1453 void stop_splashd(void)
1455 pid_splashd = -1;
1456 stop_nocat();
1457 start_wan(BOOT);
1459 #endif
1461 // -----------------------------------------------------------------------------
1462 #ifdef TCONFIG_NGINX
1464 static pid_t pid_nginx = -1;
1465 void start_enginex(void)
1467 pid_nginx =-1;
1468 start_nginx();
1469 if (!nvram_contains_word("debug_norestart","enginex")) {
1470 pid_nginx = -2;
1474 void stop_enginex(void)
1476 pid_nginx = -1;
1477 stop_nginx();
1480 void start_nginxfastpath(void)
1482 pid_nginx =-1;
1483 start_nginxfp();
1484 if (!nvram_contains_word("debug_norestart","nginxfp")) {
1485 pid_nginx = -2;
1488 void stop_nginxfastpath(void)
1490 pid_nginx = -1;
1491 stop_nginxfp();
1493 #endif
1495 // -----------------------------------------------------------------------------
1497 void set_tz(void)
1499 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1502 void start_ntpc(void)
1504 static char servers[32];
1506 set_tz();
1508 stop_ntpc();
1510 if (nvram_get_int("ntp_updates") >= 0) {
1511 strcpy(servers, nvram_safe_get("ntp_server"));
1513 if (nvram_match("dnscrypt_proxy", "1"))
1514 eval("ntp2ip");
1516 xstart("ntpclient", "-h", servers, "-i", "3", "-l", "-s");
1520 void stop_ntpc(void)
1522 killall("ntpclient", SIGTERM);
1525 // -----------------------------------------------------------------------------
1527 static void stop_rstats(void)
1529 int n, m;
1530 int pid;
1531 int pidz;
1532 int ppidz;
1533 int w = 0;
1535 n = 60;
1536 m = 15;
1537 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1538 w = 1;
1539 pidz = pidof("gzip");
1540 if (pidz < 1) pidz = pidof("cp");
1541 ppidz = ppid(ppid(pidz));
1542 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1543 syslog(LOG_DEBUG, "rstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1544 --m;
1545 } else {
1546 kill(pid, SIGTERM);
1548 sleep(1);
1550 if ((w == 1) && (n > 0))
1551 syslog(LOG_DEBUG, "rstats stopped.\n");
1554 static void start_rstats(int new)
1556 if (nvram_match("rstats_enable", "1")) {
1557 stop_rstats();
1558 if (new) {
1559 syslog(LOG_DEBUG, "starting rstats (new datafile).\n");
1560 xstart("rstats", "--new");
1561 } else {
1562 syslog(LOG_DEBUG, "starting rstats.\n");
1563 xstart("rstats");
1568 static void stop_cstats(void)
1570 int n, m;
1571 int pid;
1572 int pidz;
1573 int ppidz;
1574 int w = 0;
1576 n = 60;
1577 m = 15;
1578 while ((n-- > 0) && ((pid = pidof("cstats")) > 0)) {
1579 w = 1;
1580 pidz = pidof("gzip");
1581 if (pidz < 1) pidz = pidof("cp");
1582 ppidz = ppid(ppid(pidz));
1583 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1584 syslog(LOG_DEBUG, "cstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1585 --m;
1586 } else {
1587 kill(pid, SIGTERM);
1589 sleep(1);
1591 if ((w == 1) && (n > 0))
1592 syslog(LOG_DEBUG, "cstats stopped.\n");
1595 static void start_cstats(int new)
1597 if (nvram_match("cstats_enable", "1")) {
1598 stop_cstats();
1599 if (new) {
1600 syslog(LOG_DEBUG, "starting cstats (new datafile).\n");
1601 xstart("cstats", "--new");
1602 } else {
1603 syslog(LOG_DEBUG, "starting cstats.\n");
1604 xstart("cstats");
1609 // -----------------------------------------------------------------------------
1611 // !!TB - FTP Server
1613 #ifdef TCONFIG_FTP
1614 static char *get_full_storage_path(char *val)
1616 static char buf[128];
1617 int len;
1619 if (val[0] == '/')
1620 len = sprintf(buf, "%s", val);
1621 else
1622 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1624 if (len > 1 && buf[len - 1] == '/')
1625 buf[len - 1] = 0;
1627 return buf;
1630 static char *nvram_storage_path(char *var)
1632 char *val = nvram_safe_get(var);
1633 return get_full_storage_path(val);
1636 char vsftpd_conf[] = "/etc/vsftpd.conf";
1637 char vsftpd_users[] = "/etc/vsftpd.users";
1638 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1640 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1642 static void start_ftpd(void)
1644 char tmp[256];
1645 FILE *fp, *f;
1646 char *buf;
1647 char *p, *q;
1648 char *user, *pass, *rights, *root_dir;
1649 int i;
1651 if (getpid() != 1) {
1652 start_service("ftpd");
1653 return;
1656 if (!nvram_get_int("ftp_enable")) return;
1658 mkdir_if_none(vsftpd_users);
1659 mkdir_if_none("/var/run/vsftpd");
1661 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1662 return;
1664 if (nvram_get_int("ftp_super"))
1666 /* rights */
1667 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1668 if ((f = fopen(tmp, "w")))
1670 fprintf(f,
1671 "dirlist_enable=yes\n"
1672 "write_enable=yes\n"
1673 "download_enable=yes\n");
1674 fclose(f);
1678 #ifdef TCONFIG_SAMBASRV
1679 if (nvram_match("smbd_cset", "utf8"))
1680 fprintf(fp, "utf8=yes\n");
1681 #endif
1683 if (nvram_invmatch("ftp_anonymous", "0"))
1685 fprintf(fp,
1686 "anon_allow_writable_root=yes\n"
1687 "anon_world_readable_only=no\n"
1688 "anon_umask=022\n");
1690 /* rights */
1691 sprintf(tmp, "%s/ftp", vsftpd_users);
1692 if ((f = fopen(tmp, "w")))
1694 if (nvram_match("ftp_dirlist", "0"))
1695 fprintf(f, "dirlist_enable=yes\n");
1696 if (nvram_match("ftp_anonymous", "1") ||
1697 nvram_match("ftp_anonymous", "3"))
1698 fprintf(f, "write_enable=yes\n");
1699 if (nvram_match("ftp_anonymous", "1") ||
1700 nvram_match("ftp_anonymous", "2"))
1701 fprintf(f, "download_enable=yes\n");
1702 fclose(f);
1704 if (nvram_match("ftp_anonymous", "1") ||
1705 nvram_match("ftp_anonymous", "3"))
1706 fprintf(fp,
1707 "anon_upload_enable=yes\n"
1708 "anon_mkdir_write_enable=yes\n"
1709 "anon_other_write_enable=yes\n");
1710 } else {
1711 fprintf(fp, "anonymous_enable=no\n");
1714 fprintf(fp,
1715 "dirmessage_enable=yes\n"
1716 "download_enable=no\n"
1717 "dirlist_enable=no\n"
1718 "hide_ids=yes\n"
1719 "syslog_enable=yes\n"
1720 "local_enable=yes\n"
1721 "local_umask=022\n"
1722 "chmod_enable=no\n"
1723 "chroot_local_user=yes\n"
1724 "check_shell=no\n"
1725 "log_ftp_protocol=%s\n"
1726 "user_config_dir=%s\n"
1727 "passwd_file=%s\n"
1728 "listen%s=yes\n"
1729 "listen_port=%s\n"
1730 "background=yes\n"
1731 "isolate=no\n"
1732 "max_clients=%d\n"
1733 "max_per_ip=%d\n"
1734 "max_login_fails=1\n"
1735 "idle_session_timeout=%s\n"
1736 "use_sendfile=no\n"
1737 "anon_max_rate=%d\n"
1738 "local_max_rate=%d\n"
1739 "%s\n",
1740 nvram_get_int("log_ftp") ? "yes" : "no",
1741 vsftpd_users, vsftpd_passwd,
1742 #ifdef TCONFIG_IPV6
1743 ipv6_enabled() ? "_ipv6" : "",
1744 #else
1746 #endif
1747 nvram_get("ftp_port") ? : "21",
1748 nvram_get_int("ftp_max"),
1749 nvram_get_int("ftp_ipmax"),
1750 nvram_get("ftp_staytimeout") ? : "300",
1751 nvram_get_int("ftp_anonrate") * 1024,
1752 nvram_get_int("ftp_rate") * 1024,
1753 nvram_safe_get("ftp_custom"));
1755 fclose(fp);
1757 /* prepare passwd file and default users */
1758 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1759 return;
1761 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1762 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1764 fprintf(fp, /* anonymous, admin, nobody */
1765 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1766 "%s:%s:0:0:root:/:/sbin/nologin\n"
1767 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1768 nvram_storage_path("ftp_anonroot"), user,
1769 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1770 MOUNT_ROOT);
1772 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1775 username<password<rights[<root_dir>]
1776 rights:
1777 Read/Write
1778 Read Only
1779 View Only
1780 Private
1782 p = buf;
1783 while ((q = strsep(&p, ">")) != NULL) {
1784 i = vstrsep(q, "<", &user, &pass, &rights, &root_dir);
1785 if (i < 3 || i > 4) continue;
1786 if (!user || !pass) continue;
1788 if (i == 3 || !root_dir || !(*root_dir))
1790 root_dir = nvram_safe_get("ftp_pubroot");
1792 /* directory */
1793 if (strncmp(rights, "Private", 7) == 0)
1795 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1796 mkdir_if_none(tmp);
1798 else
1799 sprintf(tmp, "%s", get_full_storage_path(root_dir));
1801 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1802 user, crypt(pass, "$1$"), user, tmp);
1804 /* rights */
1805 sprintf(tmp, "%s/%s", vsftpd_users, user);
1806 if ((f = fopen(tmp, "w")))
1808 tmp[0] = 0;
1809 if (nvram_invmatch("ftp_dirlist", "1"))
1810 strcat(tmp, "dirlist_enable=yes\n");
1811 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1812 strcat(tmp, "download_enable=yes\n");
1813 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1814 strcat(tmp, "write_enable=yes\n");
1816 fputs(tmp, f);
1817 fclose(f);
1820 free(buf);
1823 fclose(fp);
1824 killall("vsftpd", SIGHUP);
1826 /* start vsftpd if it's not already running */
1827 if (pidof("vsftpd") <= 0)
1828 xstart("vsftpd");
1831 static void stop_ftpd(void)
1833 if (getpid() != 1) {
1834 stop_service("ftpd");
1835 return;
1838 killall_tk("vsftpd");
1839 unlink(vsftpd_passwd);
1840 unlink(vsftpd_conf);
1841 eval("rm", "-rf", vsftpd_users);
1843 #endif // TCONFIG_FTP
1845 // -----------------------------------------------------------------------------
1847 // !!TB - Samba
1849 #ifdef TCONFIG_SAMBASRV
1850 static void kill_samba(int sig)
1852 if (sig == SIGTERM) {
1853 killall_tk("smbd");
1854 killall_tk("nmbd");
1856 else {
1857 killall("smbd", sig);
1858 killall("nmbd", sig);
1862 #if 0
1863 #ifdef TCONFIG_BCMARM
1864 extern void del_samba_rules(void);
1865 extern void add_samba_rules(void);
1866 #endif
1867 #endif
1869 static void start_samba(void)
1871 FILE *fp;
1872 DIR *dir = NULL;
1873 struct dirent *dp;
1874 char nlsmod[15];
1875 int mode;
1876 char *nv;
1877 #ifdef TCONFIG_BCMARM
1878 int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
1879 int taskset_ret = -1;
1880 #endif
1882 if (getpid() != 1) {
1883 start_service("smbd");
1884 return;
1887 mode = nvram_get_int("smbd_enable");
1888 if (!mode || !nvram_invmatch("lan_hostname", ""))
1889 return;
1891 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1892 return;
1894 #ifdef TCONFIG_GROCTRL
1895 enable_gro(2);
1896 #endif
1898 fprintf(fp, "[global]\n"
1899 " interfaces = %s\n"
1900 " bind interfaces only = yes\n"
1901 " workgroup = %s\n"
1902 " netbios name = %s\n"
1903 " server string = %s\n"
1904 " guest account = nobody\n"
1905 " security = user\n"
1906 " %s\n"
1907 " guest ok = %s\n"
1908 " guest only = no\n"
1909 " browseable = yes\n"
1910 " syslog only = yes\n"
1911 " timestamp logs = no\n"
1912 " syslog = 1\n"
1913 " encrypt passwords = yes\n"
1914 " preserve case = yes\n"
1915 " short preserve case = yes\n",
1916 nvram_safe_get("lan_ifname"),
1917 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1918 nvram_safe_get("lan_hostname"),
1919 nvram_get("router_name") ? : "Tomato",
1920 mode == 2 ? "" : "map to guest = Bad User",
1921 mode == 2 ? "no" : "yes" // guest ok
1924 if (nvram_get_int("smbd_wins")) {
1925 nv = nvram_safe_get("wan_wins");
1926 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1927 fprintf(fp, " wins support = yes\n");
1931 if (nvram_get_int("smbd_master")) {
1932 fprintf(fp,
1933 " domain master = yes\n"
1934 " local master = yes\n"
1935 " preferred master = yes\n"
1936 " os level = 65\n");
1939 nv = nvram_safe_get("smbd_cpage");
1940 if (*nv) {
1941 #ifndef TCONFIG_SAMBA3
1942 fprintf(fp, " client code page = %s\n", nv);
1943 #endif
1944 sprintf(nlsmod, "nls_cp%s", nv);
1946 nv = nvram_safe_get("smbd_nlsmod");
1947 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1948 modprobe_r(nv);
1950 modprobe(nlsmod);
1951 nvram_set("smbd_nlsmod", nlsmod);
1954 #ifndef TCONFIG_SAMBA3
1955 if (nvram_match("smbd_cset", "utf8"))
1956 fprintf(fp, " coding system = utf8\n");
1957 else if (nvram_invmatch("smbd_cset", ""))
1958 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1959 #endif
1961 nv = nvram_safe_get("smbd_custom");
1962 /* add socket options unless overriden by the user */
1963 if (strstr(nv, "socket options") == NULL) {
1964 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1966 fprintf(fp, "%s\n\n", nv);
1968 /* configure shares */
1970 char *buf;
1971 char *p, *q;
1972 char *name, *path, *comment, *writeable, *hidden;
1973 int cnt = 0;
1975 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1977 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1979 p = buf;
1980 while ((q = strsep(&p, ">")) != NULL) {
1981 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1982 if (!path || !name) continue;
1984 /* share name */
1985 fprintf(fp, "\n[%s]\n", name);
1987 /* path */
1988 fprintf(fp, " path = %s\n", path);
1990 /* access level */
1991 if (!strcmp(writeable, "1"))
1992 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1993 if (!strcmp(hidden, "1"))
1994 fprintf(fp, " browseable = no\n");
1996 /* comment */
1997 if (comment)
1998 fprintf(fp, " comment = %s\n", comment);
2000 cnt++;
2002 free(buf);
2005 /* Share every mountpoint below MOUNT_ROOT */
2006 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
2007 while ((dp = readdir(dir))) {
2008 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
2010 /* Only if is a directory and is mounted */
2011 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
2012 continue;
2014 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
2015 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
2016 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
2017 if (nvram_match("smbd_autoshare", "3")) // Hidden
2018 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
2019 dp->d_name, MOUNT_ROOT, dp->d_name);
2020 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
2021 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
2023 cnt++;
2027 if (dir) closedir(dir);
2029 if (cnt == 0) {
2030 /* by default share MOUNT_ROOT as read-only */
2031 fprintf(fp, "\n[share]\n"
2032 " path = %s\n"
2033 " writable = no\n",
2034 MOUNT_ROOT);
2037 fclose(fp);
2039 mkdir_if_none("/var/run/samba");
2040 mkdir_if_none("/etc/samba");
2042 /* write smbpasswd */
2043 #ifdef TCONFIG_SAMBA3
2044 eval("smbpasswd", "nobody", "\"\"");
2045 #else
2046 eval("smbpasswd", "-a", "nobody", "\"\"");
2047 #endif
2048 if (mode == 2) {
2049 char *smbd_user;
2050 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
2051 smbd_user = "nas";
2052 #ifdef TCONFIG_SAMBA3
2053 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
2054 #else
2055 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
2056 #endif
2059 kill_samba(SIGHUP);
2060 int ret1 = 0, ret2 = 0;
2061 /* start samba if it's not already running */
2062 if (pidof("nmbd") <= 0)
2063 ret1 = xstart("nmbd", "-D");
2064 if (pidof("smbd") <= 0) {
2065 #ifdef TCONFIG_BCMARM
2066 #ifdef TCONFIG_BCMSMP
2067 if (cpu_num > 1)
2068 taskset_ret = cpu_eval(NULL, "1", "ionice", "-c1", "-n0", "smbd", "-D");
2069 else
2070 taskset_ret = eval("ionice", "-c1", "-n0", "smbd", "-D");
2072 if (taskset_ret != 0)
2073 #endif
2074 #endif
2075 ret2 = xstart("smbd", "-D");
2078 if (ret1 || ret2) kill_samba(SIGTERM);
2081 static void stop_samba(void)
2083 if (getpid() != 1) {
2084 stop_service("smbd");
2085 return;
2088 kill_samba(SIGTERM);
2089 /* clean up */
2090 unlink("/var/log/smb");
2091 unlink("/var/log/nmb");
2092 eval("rm", "-rf", "/var/run/samba");
2094 #ifdef TCONFIG_GROCTRL
2095 enable_gro(0);
2096 #endif
2099 #endif // TCONFIG_SAMBASRV
2101 #ifdef TCONFIG_GROCTRL
2102 void enable_gro(int interval)
2104 char *argv[3] = {"echo", "", NULL};
2105 char lan_ifname[32], *lan_ifnames, *next;
2106 char path[64] = {0};
2107 char parm[32] = {0};
2109 if(nvram_get_int("gro_disable"))
2110 return;
2112 /* enabled gso on vlan interface */
2113 lan_ifnames = nvram_safe_get("lan_ifnames");
2114 foreach(lan_ifname, lan_ifnames, next) {
2115 if (!strncmp(lan_ifname, "vlan", 4)) {
2116 sprintf(path, ">>/proc/net/vlan/%s", lan_ifname);
2117 sprintf(parm, "-gro %d", interval);
2118 argv[1] = parm;
2119 _eval(argv, path, 0, NULL);
2123 #endif
2125 #ifdef TCONFIG_MEDIA_SERVER
2126 #define MEDIA_SERVER_APP "minidlna"
2128 static void start_media_server(void)
2130 FILE *f;
2131 int port, pid, https;
2132 char *dbdir;
2133 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
2134 static int once = 1;
2136 if (getpid() != 1) {
2137 start_service("media");
2138 return;
2141 if (nvram_get_int("ms_sas") == 0)
2142 once = 0;
2144 if (nvram_get_int("ms_enable") != 0) {
2145 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
2146 // no forced rescan
2147 argv[3] = NULL;
2149 nvram_unset("ms_rescan");
2151 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
2152 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
2154 else {
2155 if ((f = fopen(argv[2], "w")) != NULL) {
2156 port = nvram_get_int("ms_port");
2157 https = nvram_get_int("https_enable");
2158 dbdir = nvram_safe_get("ms_dbdir");
2159 if (!(*dbdir)) dbdir = NULL;
2160 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
2162 fprintf(f,
2163 "network_interface=%s\n"
2164 "port=%d\n"
2165 "friendly_name=%s\n"
2166 "db_dir=%s/.db\n"
2167 "enable_tivo=%s\n"
2168 "strict_dlna=%s\n"
2169 "presentation_url=http%s://%s:%s/nas-media.asp\n"
2170 "inotify=yes\n"
2171 "notify_interval=600\n"
2172 "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"
2173 "log_dir=/var/log\n"
2174 "log_level=general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn\n"
2175 "\n",
2176 nvram_safe_get("lan_ifname"),
2177 (port < 0) || (port >= 0xffff) ? 0 : port,
2178 nvram_get("router_name") ? : "Tomato",
2179 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
2180 nvram_get_int("ms_tivo") ? "yes" : "no",
2181 nvram_get_int("ms_stdlna") ? "yes" : "no",
2182 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
2185 // media directories
2186 char *buf, *p, *q;
2187 char *path, *restrict;
2189 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
2190 /* path<restrict[A|V|P|] */
2192 p = buf;
2193 while ((q = strsep(&p, ">")) != NULL) {
2194 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
2195 continue;
2196 fprintf(f, "media_dir=%s%s%s\n",
2197 restrict ? : "", (restrict && *restrict) ? "," : "", path);
2199 free(buf);
2202 fclose(f);
2206 /* start media server if it's not already running */
2207 if (pidof(MEDIA_SERVER_APP) <= 0) {
2208 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
2209 /* If we started the media server successfully, wait 1 sec
2210 * to let it die if it can't open the database file.
2211 * If it's still alive after that, assume it's running and
2212 * disable forced once-after-reboot rescan.
2214 sleep(1);
2215 if (pidof(MEDIA_SERVER_APP) > 0)
2216 once = 0;
2222 static void stop_media_server(void)
2224 if (getpid() != 1) {
2225 stop_service("media");
2226 return;
2229 killall_tk(MEDIA_SERVER_APP);
2231 #endif // TCONFIG_MEDIA_SERVER
2233 #ifdef TCONFIG_USB
2234 static void start_nas_services(void)
2236 if (getpid() != 1) {
2237 start_service("usbapps");
2238 return;
2241 #ifdef TCONFIG_SAMBASRV
2242 start_samba();
2243 #endif
2244 #ifdef TCONFIG_FTP
2245 start_ftpd();
2246 #endif
2247 #ifdef TCONFIG_MEDIA_SERVER
2248 start_media_server();
2249 #endif
2252 static void stop_nas_services(void)
2254 if (getpid() != 1) {
2255 stop_service("usbapps");
2256 return;
2259 #ifdef TCONFIG_MEDIA_SERVER
2260 stop_media_server();
2261 #endif
2262 #ifdef TCONFIG_FTP
2263 stop_ftpd();
2264 #endif
2265 #ifdef TCONFIG_SAMBASRV
2266 stop_samba();
2267 #endif
2270 void restart_nas_services(int stop, int start)
2272 int fd = file_lock("usb");
2273 /* restart all NAS applications */
2274 if (stop)
2275 stop_nas_services();
2276 if (start)
2277 start_nas_services();
2278 file_unlock(fd);
2280 #endif // TCONFIG_USB
2282 // -----------------------------------------------------------------------------
2284 /* -1 = Don't check for this program, it is not expected to be running.
2285 * Other = This program has been started and should be kept running. If no
2286 * process with the name is running, call func to restart it.
2287 * Note: At startup, dnsmasq forks a short-lived child which forks a
2288 * long-lived (grand)child. The parents terminate.
2289 * Many daemons use this technique.
2291 static void _check(pid_t pid, const char *name, void (*func)(void))
2293 if (pid == -1) return;
2295 if (pidof(name) > 0) return;
2297 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
2298 func();
2300 // Force recheck in 500 msec
2301 setitimer(ITIMER_REAL, &pop_tv, NULL);
2304 void check_services(void)
2306 TRACE_PT("keep alive\n");
2308 // Periodically reap any zombies
2309 setitimer(ITIMER_REAL, &zombie_tv, NULL);
2311 #ifdef LINUX26
2312 _check(pid_hotplug2, "hotplug2", start_hotplug2);
2313 #endif
2314 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
2315 _check(pid_crond, "crond", start_cron);
2316 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
2318 //#ifdef TCONFIG_NOCAT
2319 // if (nvram_get_int("NC_enable"))
2320 // _check(&pid_splashd, "splashd", start_splashd);
2321 //#endif
2325 // -----------------------------------------------------------------------------
2327 void start_services(void)
2329 static int once = 1;
2331 if (once) {
2332 once = 0;
2334 if (nvram_get_int("telnetd_eas")) start_telnetd();
2335 if (nvram_get_int("sshd_eas")) start_sshd();
2338 // start_syslog();
2339 start_nas();
2340 start_zebra();
2341 #ifdef TCONFIG_SDHC
2342 start_mmc();
2343 #endif
2344 start_dnsmasq();
2345 start_cifs();
2346 start_httpd();
2347 #ifdef TCONFIG_NGINX
2348 start_enginex();
2349 start_mysql();
2350 #endif
2351 start_cron();
2352 // start_upnp();
2353 start_rstats(0);
2354 start_cstats(0);
2355 start_sched();
2356 #ifdef TCONFIG_PPTPD
2357 start_pptpd();
2358 #endif
2359 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2361 #ifdef TCONFIG_SNMP
2362 start_snmp();
2363 #endif
2365 start_tomatoanon();
2367 #ifdef TCONFIG_TOR
2368 start_tor();
2369 #endif
2371 #ifdef TCONFIG_BT
2372 start_bittorrent();
2373 #endif
2375 #ifdef TCONFIG_NOCAT
2376 start_splashd();
2377 #endif
2379 #ifdef TCONFIG_NFS
2380 start_nfs();
2381 #endif
2383 #ifdef TCONFIG_FANCTRL
2384 start_phy_tempsense();
2385 #endif
2388 if (get_model() == MODEL_R7000) {
2389 //enable WAN port led
2390 system("/usr/sbin/et robowr 0x0 0x10 0x3000");
2391 system("/usr/sbin/et robowr 0x0 0x12 0x78");
2392 system("/usr/sbin/et robowr 0x0 0x14 0x01");
2393 system("gpio disable 9");
2397 void stop_services(void)
2399 clear_resolv();
2401 #ifdef TCONFIG_FANCTRL
2402 stop_phy_tempsense();
2403 #endif
2405 #ifdef TCONFIG_BT
2406 stop_bittorrent();
2407 #endif
2409 #ifdef TCONFIG_NOCAT
2410 stop_splashd();
2411 #endif
2413 #ifdef TCONFIG_SNMP
2414 stop_snmp();
2415 #endif
2417 #ifdef TCONFIG_TOR
2418 stop_tor();
2419 #endif
2421 stop_tomatoanon();
2423 #ifdef TCONFIG_NFS
2424 stop_nfs();
2425 #endif
2426 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2427 #ifdef TCONFIG_PPTPD
2428 stop_pptpd();
2429 #endif
2430 stop_sched();
2431 stop_rstats();
2432 stop_cstats();
2433 // stop_upnp();
2434 stop_cron();
2435 stop_httpd();
2436 #ifdef TCONFIG_NGINX
2437 stop_mysql();
2438 stop_enginex();
2439 #endif
2440 #ifdef TCONFIG_SDHC
2441 stop_mmc();
2442 #endif
2443 stop_cifs();
2444 stop_dnsmasq();
2445 stop_zebra();
2446 stop_nas();
2447 // stop_syslog();
2450 // -----------------------------------------------------------------------------
2452 /* nvram "action_service" is: "service-action[-modifier]"
2453 * action is something like "stop" or "start" or "restart"
2454 * optional modifier is "c" for the "service" command-line command
2456 void exec_service(void)
2458 const int A_START = 1;
2459 const int A_STOP = 2;
2460 const int A_RESTART = 1|2;
2461 char buffer[128];
2462 char *service;
2463 char *act;
2464 char *next;
2465 char *modifier;
2466 int action, user;
2467 int i;
2469 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2470 next = buffer;
2472 TOP:
2473 act = strsep(&next, ",");
2474 service = strsep(&act, "-");
2475 if (act == NULL) {
2476 next = NULL;
2477 goto CLEAR;
2479 modifier = act;
2480 strsep(&modifier, "-");
2482 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2484 if (strcmp(act, "start") == 0) action = A_START;
2485 else if (strcmp(act, "stop") == 0) action = A_STOP;
2486 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2487 else action = 0;
2488 user = (modifier != NULL && *modifier == 'c');
2490 if (strcmp(service, "dhcpc") == 0) {
2491 if (action & A_STOP) stop_dhcpc();
2492 if (action & A_START) start_dhcpc();
2493 goto CLEAR;
2496 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2497 if (action & A_STOP) stop_dnsmasq();
2498 if (action & A_START) {
2499 dns_to_resolv();
2500 start_dnsmasq();
2502 goto CLEAR;
2505 if (strcmp(service, "firewall") == 0) {
2506 if (action & A_STOP) {
2507 stop_firewall();
2508 stop_igmp_proxy();
2509 stop_udpxy();
2511 if (action & A_START) {
2512 start_firewall();
2513 start_igmp_proxy();
2514 start_udpxy();
2516 goto CLEAR;
2519 if (strcmp(service, "restrict") == 0) {
2520 if (action & A_STOP) {
2521 stop_firewall();
2523 if (action & A_START) {
2524 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2526 start_firewall();
2528 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2529 if (i == 1) {
2530 if (nvram_get_int("rrules_radio") < 0) {
2531 eval("radio", "on");
2535 goto CLEAR;
2538 if (strcmp(service, "arpbind") == 0) {
2539 if (action & A_STOP) stop_arpbind();
2540 if (action & A_START) start_arpbind();
2541 goto CLEAR;
2544 if (strcmp(service, "qos") == 0) {
2545 if (action & A_STOP) {
2546 stop_qos();
2548 stop_firewall(); start_firewall(); // always restarted
2549 if (action & A_START) {
2550 start_qos();
2551 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2553 goto CLEAR;
2556 if (strcmp(service, "qoslimit") == 0) {
2557 if (action & A_STOP) {
2558 new_qoslimit_stop();
2560 #ifdef TCONFIG_NOCAT
2561 stop_splashd();
2562 #endif
2563 stop_firewall(); start_firewall(); // always restarted
2564 if (action & A_START) {
2565 new_qoslimit_start();
2567 #ifdef TCONFIG_NOCAT
2568 start_splashd();
2569 #endif
2570 goto CLEAR;
2573 if (strcmp(service, "upnp") == 0) {
2574 if (action & A_STOP) {
2575 stop_upnp();
2577 stop_firewall(); start_firewall(); // always restarted
2578 if (action & A_START) {
2579 start_upnp();
2581 goto CLEAR;
2584 if (strcmp(service, "telnetd") == 0) {
2585 if (action & A_STOP) stop_telnetd();
2586 if (action & A_START) start_telnetd();
2587 goto CLEAR;
2590 if (strcmp(service, "sshd") == 0) {
2591 if (action & A_STOP) stop_sshd();
2592 if (action & A_START) start_sshd();
2593 goto CLEAR;
2596 if (strcmp(service, "httpd") == 0) {
2597 if (action & A_STOP) stop_httpd();
2598 if (action & A_START) start_httpd();
2599 goto CLEAR;
2602 #ifdef TCONFIG_IPV6
2603 if (strcmp(service, "ipv6") == 0) {
2604 if (action & A_STOP) {
2605 stop_dnsmasq();
2606 stop_ipv6();
2608 if (action & A_START) {
2609 start_ipv6();
2610 start_dnsmasq();
2612 goto CLEAR;
2615 if (strncmp(service, "dhcp6", 5) == 0) {
2616 if (action & A_STOP) {
2617 stop_dhcp6c();
2619 if (action & A_START) {
2620 start_dhcp6c();
2622 goto CLEAR;
2624 #endif
2626 if (strcmp(service, "admin") == 0) {
2627 if (action & A_STOP) {
2628 stop_sshd();
2629 stop_telnetd();
2630 stop_httpd();
2632 stop_firewall(); start_firewall(); // always restarted
2633 if (action & A_START) {
2634 start_httpd();
2635 create_passwd();
2636 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2637 if (nvram_match("sshd_eas", "1")) start_sshd();
2639 goto CLEAR;
2642 if (strcmp(service, "ddns") == 0) {
2643 if (action & A_STOP) stop_ddns();
2644 if (action & A_START) start_ddns();
2645 goto CLEAR;
2648 if (strcmp(service, "ntpc") == 0) {
2649 if (action & A_STOP) stop_ntpc();
2650 if (action & A_START) start_ntpc();
2651 goto CLEAR;
2654 if (strcmp(service, "logging") == 0) {
2655 if (action & A_STOP) {
2656 stop_syslog();
2658 if (action & A_START) {
2659 start_syslog();
2661 if (!user) {
2662 // always restarted except from "service" command
2663 stop_cron(); start_cron();
2664 stop_firewall(); start_firewall();
2666 goto CLEAR;
2669 if (strcmp(service, "crond") == 0) {
2670 if (action & A_STOP) {
2671 stop_cron();
2673 if (action & A_START) {
2674 start_cron();
2676 goto CLEAR;
2679 #ifdef LINUX26
2680 if (strncmp(service, "hotplug", 7) == 0) {
2681 if (action & A_STOP) {
2682 stop_hotplug2();
2684 if (action & A_START) {
2685 start_hotplug2(1);
2687 goto CLEAR;
2689 #endif
2691 if (strcmp(service, "upgrade") == 0) {
2692 if (action & A_START) {
2693 #if TOMATO_SL
2694 stop_usbevent();
2695 stop_smbd();
2696 #endif
2697 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2698 stop_jffs2();
2699 // stop_cifs();
2700 stop_zebra();
2701 stop_cron();
2702 stop_ntpc();
2703 stop_upnp();
2704 // stop_dhcpc();
2705 killall("rstats", SIGTERM);
2706 killall("cstats", SIGTERM);
2707 killall("buttons", SIGTERM);
2708 stop_syslog();
2709 remove_storage_main(1); // !!TB - USB Support
2710 stop_usb(); // !!TB - USB Support
2712 goto CLEAR;
2715 #ifdef TCONFIG_CIFS
2716 if (strcmp(service, "cifs") == 0) {
2717 if (action & A_STOP) stop_cifs();
2718 if (action & A_START) start_cifs();
2719 goto CLEAR;
2721 #endif
2723 #ifdef TCONFIG_JFFS2
2724 if (strncmp(service, "jffs", 4) == 0) {
2725 if (action & A_STOP) stop_jffs2();
2726 if (action & A_START) start_jffs2();
2727 goto CLEAR;
2729 #endif
2731 if (strcmp(service, "zebra") == 0) {
2732 if (action & A_STOP) stop_zebra();
2733 if (action & A_START) start_zebra();
2734 goto CLEAR;
2737 #ifdef TCONFIG_SDHC
2738 if (strcmp(service, "mmc") == 0) {
2739 if (action & A_STOP) stop_mmc();
2740 if (action & A_START) start_mmc();
2741 goto CLEAR;
2743 #endif
2745 if (strcmp(service, "routing") == 0) {
2746 if (action & A_STOP) {
2747 stop_zebra();
2748 do_static_routes(0); // remove old '_saved'
2749 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2750 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2751 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2752 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2753 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2754 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2755 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2757 stop_firewall();
2758 start_firewall();
2759 if (action & A_START) {
2760 do_static_routes(1); // add new
2761 start_zebra();
2762 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2763 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2764 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2765 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2766 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2767 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2768 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2770 goto CLEAR;
2773 if (strcmp(service, "ctnf") == 0) {
2774 if (action & A_START) {
2775 setup_conntrack();
2776 stop_firewall();
2777 start_firewall();
2779 goto CLEAR;
2782 if (strcmp(service, "wan") == 0) {
2783 if (action & A_STOP) {
2784 stop_wan();
2787 if (action & A_START) {
2788 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2789 start_wan(BOOT);
2790 sleep(2);
2791 force_to_dial();
2793 goto CLEAR;
2796 if (strcmp(service, "net") == 0) {
2797 if (action & A_STOP) {
2798 #ifdef TCONFIG_USB
2799 stop_nas_services();
2800 #endif
2801 stop_httpd();
2802 stop_dnsmasq();
2803 stop_nas();
2804 stop_wan();
2805 stop_arpbind();
2806 stop_lan();
2807 stop_vlan();
2809 if (action & A_START) {
2810 start_vlan();
2811 start_lan();
2812 start_arpbind();
2813 start_wan(BOOT);
2814 start_nas();
2815 start_dnsmasq();
2816 start_httpd();
2817 start_wl();
2818 #ifdef TCONFIG_USB
2819 start_nas_services();
2820 #endif
2822 goto CLEAR;
2825 if (strcmp(service, "wireless") == 0) {
2826 if(action & A_STOP) {
2827 stop_wireless();
2829 if(action & A_START) {
2830 start_wireless();
2832 goto CLEAR;
2835 if (strcmp(service, "wl") == 0) {
2836 if(action & A_STOP) {
2837 stop_wireless();
2838 unload_wl();
2840 if(action & A_START) {
2841 load_wl();
2842 start_wireless();
2843 stop_wireless();
2844 start_wireless();
2846 goto CLEAR;
2849 if (strcmp(service, "nas") == 0) {
2850 if (action & A_STOP) {
2851 stop_nas();
2853 if (action & A_START) {
2854 start_nas();
2855 start_wl();
2857 goto CLEAR;
2860 if (strcmp(service, "rstats") == 0) {
2861 if (action & A_STOP) stop_rstats();
2862 if (action & A_START) start_rstats(0);
2863 goto CLEAR;
2866 if (strcmp(service, "rstatsnew") == 0) {
2867 if (action & A_STOP) stop_rstats();
2868 if (action & A_START) start_rstats(1);
2869 goto CLEAR;
2872 if (strcmp(service, "cstats") == 0) {
2873 if (action & A_STOP) stop_cstats();
2874 if (action & A_START) start_cstats(0);
2875 goto CLEAR;
2878 if (strcmp(service, "cstatsnew") == 0) {
2879 if (action & A_STOP) stop_cstats();
2880 if (action & A_START) start_cstats(1);
2881 goto CLEAR;
2884 if (strcmp(service, "sched") == 0) {
2885 if (action & A_STOP) stop_sched();
2886 if (action & A_START) start_sched();
2887 goto CLEAR;
2890 #ifdef TCONFIG_BT
2891 if (strcmp(service, "bittorrent") == 0) {
2892 if (action & A_STOP) {
2893 stop_bittorrent();
2895 stop_firewall(); start_firewall(); // always restarted
2896 if (action & A_START) {
2897 start_bittorrent();
2899 goto CLEAR;
2901 #endif
2903 #ifdef TCONFIG_NFS
2904 if (strcmp(service, "nfs") == 0) {
2905 if (action & A_STOP) stop_nfs();
2906 if (action & A_START) start_nfs();
2907 goto CLEAR;
2909 #endif
2911 #ifdef TCONFIG_SNMP
2912 if (strcmp(service, "snmp") == 0) {
2913 if (action & A_STOP) stop_snmp();
2914 if (action & A_START) start_snmp();
2915 goto CLEAR;
2917 #endif
2919 #ifdef TCONFIG_TOR
2920 if (strcmp(service, "tor") == 0) {
2921 if (action & A_STOP) stop_tor();
2923 stop_firewall(); start_firewall(); // always restarted
2925 if (action & A_START) start_tor();
2926 goto CLEAR;
2928 #endif
2930 #ifdef TCONFIG_UPS
2931 if (strcmp(service, "ups") == 0) {
2932 if (action & A_STOP) stop_ups();
2933 if (action & A_START) start_ups();
2934 goto CLEAR;
2936 #endif
2938 if (strcmp(service, "tomatoanon") == 0) {
2939 if (action & A_STOP) stop_tomatoanon();
2940 if (action & A_START) start_tomatoanon();
2941 goto CLEAR;
2944 #ifdef TCONFIG_USB
2945 // !!TB - USB Support
2946 if (strcmp(service, "usb") == 0) {
2947 if (action & A_STOP) stop_usb();
2948 if (action & A_START) {
2949 start_usb();
2950 // restart Samba and ftp since they may be killed by stop_usb()
2951 restart_nas_services(0, 1);
2952 // remount all partitions by simulating hotplug event
2953 add_remove_usbhost("-1", 1);
2955 goto CLEAR;
2958 if (strcmp(service, "usbapps") == 0) {
2959 if (action & A_STOP) stop_nas_services();
2960 if (action & A_START) start_nas_services();
2961 goto CLEAR;
2963 #endif
2965 #ifdef TCONFIG_FTP
2966 // !!TB - FTP Server
2967 if (strcmp(service, "ftpd") == 0) {
2968 if (action & A_STOP) stop_ftpd();
2969 setup_conntrack();
2970 stop_firewall();
2971 start_firewall();
2972 if (action & A_START) start_ftpd();
2973 goto CLEAR;
2975 #endif
2977 #ifdef TCONFIG_MEDIA_SERVER
2978 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2979 if (action & A_STOP) stop_media_server();
2980 if (action & A_START) start_media_server();
2981 goto CLEAR;
2983 #endif
2985 #ifdef TCONFIG_SAMBASRV
2986 // !!TB - Samba
2987 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2988 if (action & A_STOP) stop_samba();
2989 if (action & A_START) {
2990 create_passwd();
2991 stop_dnsmasq();
2992 start_dnsmasq();
2993 start_samba();
2995 goto CLEAR;
2997 #endif
2999 #ifdef TCONFIG_OPENVPN
3000 if (strncmp(service, "vpnclient", 9) == 0) {
3001 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
3002 if (action & A_START) start_vpnclient(atoi(&service[9]));
3003 goto CLEAR;
3006 if (strncmp(service, "vpnserver", 9) == 0) {
3007 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
3008 if (action & A_START) start_vpnserver(atoi(&service[9]));
3009 goto CLEAR;
3011 #endif
3013 #ifdef TCONFIG_TINC
3014 if (strcmp(service, "tinc") == 0) {
3015 if (action & A_STOP) stop_tinc();
3016 if (action & A_START) start_tinc();
3017 goto CLEAR;
3019 #endif
3021 #ifdef TCONFIG_FANCTRL
3022 if (strcmp(service, "fanctrl") == 0) {
3023 if (action & A_STOP) stop_phy_tempsense();
3024 if (action & A_START) start_phy_tempsense();
3025 goto CLEAR;
3027 #endif
3029 #ifdef TCONFIG_NOCAT
3030 if (strcmp(service, "splashd") == 0) {
3031 if (action & A_STOP) stop_splashd();
3032 if (action & A_START) start_splashd();
3033 goto CLEAR;
3035 #endif
3037 #ifdef TCONFIG_NGINX
3038 if (strcmp(service, "enginex") == 0) {
3039 if (action & A_STOP) stop_enginex();
3040 stop_firewall(); start_firewall(); // always restarted
3041 if (action & A_START) start_enginex();
3042 goto CLEAR;
3044 if (strcmp(service, "nginxfp") == 0) {
3045 if (action & A_STOP) stop_nginxfastpath();
3046 stop_firewall(); start_firewall(); // always restarted
3047 if (action & A_START) start_nginxfastpath();
3048 goto CLEAR;
3050 if (strcmp(service, "mysql") == 0) {
3051 if (action & A_STOP) stop_mysql();
3052 stop_firewall(); start_firewall(); // always restarted
3053 if (action & A_START) start_mysql();
3054 goto CLEAR;
3056 #endif
3058 #ifdef TCONFIG_PPTPD
3059 if (strcmp(service, "pptpd") == 0) {
3060 if (action & A_STOP) stop_pptpd();
3061 if (action & A_START) start_pptpd();
3062 goto CLEAR;
3064 #endif
3066 #ifdef TCONFIG_PPTPD
3067 if (strcmp(service, "pptpclient") == 0) {
3068 if (action & A_STOP) stop_pptp_client();
3069 if (action & A_START) start_pptp_client();
3070 if (action & (A_START | A_STOP))
3072 stop_dnsmasq();
3073 dns_to_resolv();
3074 start_dnsmasq();
3075 if ((action & A_START) == 0)
3076 clear_pptp_route();
3078 goto CLEAR;
3080 #endif
3082 CLEAR:
3083 if (next) goto TOP;
3085 // some functions check action_service and must be cleared at end -- zzz
3086 nvram_set("action_service", "");
3088 // Force recheck in 500 msec
3089 setitimer(ITIMER_REAL, &pop_tv, NULL);
3092 static void do_service(const char *name, const char *action, int user)
3094 int n;
3095 char s[64];
3097 n = 150;
3098 while (!nvram_match("action_service", "")) {
3099 if (user) {
3100 putchar('*');
3101 fflush(stdout);
3103 else if (--n < 0) break;
3104 usleep(100 * 1000);
3107 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
3108 nvram_set("action_service", s);
3110 if (nvram_match("debug_rc_svc", "1")) {
3111 nvram_unset("debug_rc_svc");
3112 exec_service();
3113 } else {
3114 kill(1, SIGUSR1);
3117 n = 150;
3118 while (nvram_match("action_service", s)) {
3119 if (user) {
3120 putchar('.');
3121 fflush(stdout);
3123 else if (--n < 0) {
3124 break;
3126 usleep(100 * 1000);
3130 int service_main(int argc, char *argv[])
3132 if (argc != 3) usage_exit(argv[0], "<service> <action>");
3133 do_service(argv[1], argv[2], 1);
3134 printf("\nDone.\n");
3135 return 0;
3138 void start_service(const char *name)
3140 do_service(name, "start", 0);
3143 void stop_service(const char *name)
3145 do_service(name, "stop", 0);
3149 void restart_service(const char *name)
3151 do_service(name, "restart", 0);