rc: dnsmasq options for cache size and async logging
[tomato.git] / release / src / router / rc / services.c
blobf80c32eb77e948b15358d8f5555fc232566a993c
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_IPV6
556 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns, const char *suffix, int once)
558 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
559 struct in6_addr addr;
560 int cnt = 0;
562 foreach(p, dns, next) {
563 // verify that this is a valid IPv6 address
564 if (inet_pton(AF_INET6, p, &addr) == 1) {
565 fprintf(f, "%s%s%s", (once && cnt) ? "" : prefix, p, suffix);
566 ++cnt;
570 return cnt;
572 #endif
574 void dns_to_resolv(void)
576 FILE *f;
577 const dns_list_t *dns;
578 int i;
579 mode_t m;
581 m = umask(022); // 077 from pppoecd
582 if ((f = fopen(dmresolv, "w")) != NULL) {
583 // Check for VPN DNS entries
584 if (!write_pptpvpn_resolv(f) && !write_vpn_resolv(f)) {
585 #ifdef TCONFIG_IPV6
586 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
587 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
588 #endif
589 dns = get_dns(); // static buffer
590 if (dns->count == 0) {
591 // Put a pseudo DNS IP to trigger Connect On Demand
592 if (nvram_match("ppp_demand", "1")) {
593 switch (get_wan_proto()) {
594 case WP_PPPOE:
595 case WP_PPP3G:
596 case WP_PPTP:
597 case WP_L2TP:
598 fprintf(f, "nameserver 1.1.1.1\n");
599 break;
603 else {
604 for (i = 0; i < dns->count; i++) {
605 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
606 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
611 fclose(f);
613 umask(m);
616 // -----------------------------------------------------------------------------
618 void start_httpd(void)
620 if (getpid() != 1) {
621 start_service("httpd");
622 return;
625 if( nvram_match( "web_css", "online" ) )
626 xstart( "/usr/sbin/ttb" );
628 stop_httpd();
630 // set www dir
631 if ( nvram_match( "web_dir", "jffs" ) ) { chdir("/jffs/www"); }
632 else if ( nvram_match( "web_dir", "opt" ) ) { chdir("/opt/www"); }
633 else if ( nvram_match( "web_dir", "tmp" ) ) { chdir("/tmp/www");}
634 else { chdir("/www"); }
636 eval("httpd");
637 chdir("/");
640 void stop_httpd(void)
642 if (getpid() != 1) {
643 stop_service("httpd");
644 return;
647 killall_tk("httpd");
650 // -----------------------------------------------------------------------------
651 #ifdef TCONFIG_IPV6
653 static void add_ip6_lanaddr(void)
655 char ip[INET6_ADDRSTRLEN + 4];
656 const char *p;
658 p = ipv6_router_address(NULL);
659 if (*p) {
660 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
661 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
665 void start_ipv6_tunnel(void)
667 char ip[INET6_ADDRSTRLEN + 4];
668 struct in_addr addr4;
669 struct in6_addr addr;
670 const char *wanip, *mtu, *tun_dev;
671 int service;
673 service = get_ipv6_service();
674 tun_dev = get_wan6face();
675 wanip = get_wanip();
676 mtu = (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
677 modprobe("sit");
679 if (service == IPV6_ANYCAST_6TO4)
680 snprintf(ip, sizeof(ip), "192.88.99.%d", nvram_get_int("ipv6_relay"));
681 else
682 strlcpy(ip, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip));
683 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit",
684 "remote", ip,
685 "local", (char *)wanip,
686 "ttl", nvram_safe_get("ipv6_tun_ttl"));
688 eval("ip", "link", "set", (char *)tun_dev, "mtu", (char *)mtu, "up");
689 nvram_set("ipv6_ifname", (char *)tun_dev);
691 if (service == IPV6_ANYCAST_6TO4) {
692 add_ip6_lanaddr();
693 addr4.s_addr = 0;
694 memset(&addr, 0, sizeof(addr));
695 inet_aton(wanip, &addr4);
696 addr.s6_addr16[0] = htons(0x2002);
697 ipv6_mapaddr4(&addr, 16, &addr4, 0);
698 addr.s6_addr16[7] = htons(0x0001);
699 inet_ntop(AF_INET6, &addr, ip, sizeof(ip));
700 strncat(ip, "/16", sizeof(ip));
702 else {
703 snprintf(ip, sizeof(ip), "%s/%d",
704 nvram_safe_get("ipv6_tun_addr"),
705 nvram_get_int("ipv6_tun_addrlen") ? : 64);
707 eval("ip", "addr", "add", ip, "dev", (char *)tun_dev);
708 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev);
710 // (re)start radvd - now dnsmasq provided
711 if (service == IPV6_ANYCAST_6TO4)
712 start_dnsmasq();
715 void stop_ipv6_tunnel(void)
717 eval("ip", "tunnel", "del", (char *)get_wan6face());
718 if (get_ipv6_service() == IPV6_ANYCAST_6TO4) {
719 // get rid of old IPv6 address from lan iface
720 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
722 modprobe_r("sit");
725 void start_6rd_tunnel(void)
727 const char *tun_dev, *wanip;
728 int service, mask_len, prefix_len, local_prefix_len;
729 char mtu[10], prefix[INET6_ADDRSTRLEN], relay[INET_ADDRSTRLEN];
730 struct in_addr netmask_addr, relay_addr, relay_prefix_addr, wanip_addr;
731 struct in6_addr prefix_addr, local_prefix_addr;
732 char local_prefix[INET6_ADDRSTRLEN];
733 char tmp_ipv6[INET6_ADDRSTRLEN + 4], tmp_ipv4[INET_ADDRSTRLEN + 4];
734 char tmp[256];
735 FILE *f;
737 service = get_ipv6_service();
738 wanip = get_wanip();
739 tun_dev = get_wan6face();
740 sprintf(mtu, "%d", (nvram_get_int("wan_mtu") > 0) ? (nvram_get_int("wan_mtu") - 20) : 1280);
742 // maybe we can merge the ipv6_6rd_* variables into a single ipv_6rd_string (ala wan_6rd)
743 // to save nvram space?
744 if (service == IPV6_6RD) {
745 _dprintf("starting 6rd tunnel using manual settings.\n");
746 mask_len = nvram_get_int("ipv6_6rd_ipv4masklen");
747 prefix_len = nvram_get_int("ipv6_6rd_prefix_length");
748 strcpy(prefix, nvram_safe_get("ipv6_6rd_prefix"));
749 strcpy(relay, nvram_safe_get("ipv6_6rd_borderrelay"));
751 else {
752 _dprintf("starting 6rd tunnel using automatic settings.\n");
753 char *wan_6rd = nvram_safe_get("wan_6rd");
754 if (sscanf(wan_6rd, "%d %d %s %s", &mask_len, &prefix_len, prefix, relay) < 4) {
755 _dprintf("wan_6rd string is missing or invalid (%s)\n", wan_6rd);
756 return;
760 // validate values that were passed
761 if (mask_len < 0 || mask_len > 32) {
762 _dprintf("invalid mask_len value (%d)\n", mask_len);
763 return;
765 if (prefix_len < 0 || prefix_len > 128) {
766 _dprintf("invalid prefix_len value (%d)\n", prefix_len);
767 return;
769 if ((32 - mask_len) + prefix_len > 128) {
770 _dprintf("invalid combination of mask_len and prefix_len!\n");
771 return;
774 sprintf(tmp, "ping -q -c 2 %s | grep packet", relay);
775 if ((f = popen(tmp, "r")) == NULL) {
776 _dprintf("error obtaining data\n");
777 return;
779 fgets(tmp, sizeof(tmp), f);
780 pclose(f);
781 if (strstr(tmp, " 0% packet loss") == NULL) {
782 _dprintf("failed to ping border relay\n");
783 return;
786 // get relay prefix from border relay address and mask
787 netmask_addr.s_addr = htonl(0xffffffff << (32 - mask_len));
788 inet_aton(relay, &relay_addr);
789 relay_prefix_addr.s_addr = relay_addr.s_addr & netmask_addr.s_addr;
791 // calculate the local prefix
792 inet_pton(AF_INET6, prefix, &prefix_addr);
793 inet_pton(AF_INET, wanip, &wanip_addr);
794 if (calc_6rd_local_prefix(&prefix_addr, prefix_len, mask_len,
795 &wanip_addr, &local_prefix_addr, &local_prefix_len) == 0) {
796 _dprintf("error calculating local prefix.");
797 return;
799 inet_ntop(AF_INET6, &local_prefix_addr, local_prefix, sizeof(local_prefix));
801 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1", local_prefix);
802 nvram_set("ipv6_rtr_addr", tmp_ipv6);
803 nvram_set("ipv6_prefix", local_prefix);
805 // load sit module needed for the 6rd tunnel
806 modprobe("sit");
808 // creating the 6rd tunnel
809 eval("ip", "tunnel", "add", (char *)tun_dev, "mode", "sit", "local", (char *)wanip, "ttl", nvram_safe_get("ipv6_tun_ttl"));
811 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s/%d", prefix, prefix_len);
812 snprintf(tmp_ipv4, sizeof(tmp_ipv4), "%s/%d", inet_ntoa(relay_prefix_addr), mask_len);
813 eval("ip", "tunnel" "6rd", "dev", (char *)tun_dev, "6rd-prefix", tmp_ipv6, "6rd-relay_prefix", tmp_ipv4);
815 // bringing up the link
816 eval("ip", "link", "set", "dev", (char *)tun_dev, "mtu", (char *)mtu, "up");
818 // setting the WAN address Note: IPv6 WAN CIDR should be: ((32 - ip6rd_ipv4masklen) + ip6rd_prefixlen)
819 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, local_prefix_len);
820 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", (char *)tun_dev);
822 // setting the LAN address Note: IPv6 LAN CIDR should be 64
823 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "%s1/%d", local_prefix, nvram_get_int("ipv6_prefix_length") ? : 64);
824 eval("ip", "-6", "addr", "add", tmp_ipv6, "dev", nvram_safe_get("lan_ifname"));
826 // adding default route via the border relay
827 snprintf(tmp_ipv6, sizeof(tmp_ipv6), "::%s", relay);
828 eval("ip", "-6", "route", "add", "default", "via", tmp_ipv6, "dev", (char *)tun_dev);
830 nvram_set("ipv6_ifname", (char *)tun_dev);
832 // (re)start radvd now dnsmasq
833 start_dnsmasq();
835 printf("6rd end\n");
838 void stop_6rd_tunnel(void)
840 eval("ip", "tunnel", "del", (char *)get_wan6face());
841 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
842 modprobe_r("sit");
846 void start_ipv6(void)
848 int service;
850 service = get_ipv6_service();
851 enable_ip6_forward();
853 // Check if turned on
854 switch (service) {
855 case IPV6_NATIVE:
856 case IPV6_6IN4:
857 case IPV6_MANUAL:
858 add_ip6_lanaddr();
859 break;
860 case IPV6_NATIVE_DHCP:
861 case IPV6_ANYCAST_6TO4:
862 nvram_set("ipv6_rtr_addr", "");
863 nvram_set("ipv6_prefix", "");
864 break;
867 if (service != IPV6_DISABLED) {
868 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
869 accept_ra(nvram_safe_get("lan_ifname"));
873 void stop_ipv6(void)
875 stop_ipv6_tunnel();
876 stop_dhcp6c();
877 eval("ip", "-6", "addr", "flush", "scope", "global");
880 #endif
882 // -----------------------------------------------------------------------------
884 void start_upnp(void)
886 if (getpid() != 1) {
887 start_service("upnp");
888 return;
891 if (get_wan_proto() == WP_DISABLED) return;
893 int enable;
894 FILE *f;
895 int upnp_port;
897 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
898 mkdir("/etc/upnp", 0777);
899 if (f_exists("/etc/upnp/config.alt")) {
900 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
902 else {
903 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
904 upnp_port = nvram_get_int("upnp_port");
905 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
908 fprintf(f,
909 "ext_ifname=%s\n"
910 "port=%d\n"
911 "enable_upnp=%s\n"
912 "enable_natpmp=%s\n"
913 "secure_mode=%s\n"
914 "upnp_forward_chain=upnp\n"
915 "upnp_nat_chain=upnp\n"
916 "notify_interval=%d\n"
917 "system_uptime=yes\n"
918 "friendly_name=%s"" Router\n"
919 "model_name=%s\n"
920 "model_url=http://linksysinfo.org/index.php?forums/tomato-firmware.33/\n"
921 "manufacturer_name=Tomato Firmware\n"
922 "manufacturer_url=http://linksysinfo.org/index.php?forums/tomato-firmware.33/\n"
923 "\n"
925 get_wanface(),
926 upnp_port,
927 (enable & 1) ? "yes" : "no", // upnp enable
928 (enable & 2) ? "yes" : "no", // natpmp enable
929 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
930 nvram_get_int("upnp_ssdp_interval"),
931 nvram_safe_get("router_name"),
932 nvram_safe_get("t_model_name")
935 if (nvram_get_int("upnp_clean")) {
936 int interval = nvram_get_int("upnp_clean_interval");
937 if (interval < 60) interval = 60;
938 fprintf(f,
939 "clean_ruleset_interval=%d\n"
940 "clean_ruleset_threshold=%d\n",
941 interval,
942 nvram_get_int("upnp_clean_threshold")
945 else
946 fprintf(f,"clean_ruleset_interval=0\n");
948 if (nvram_match("upnp_mnp", "1")) {
949 int https = nvram_get_int("https_enable");
950 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
951 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
952 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
954 else {
955 // Empty parameters are not included into XML service description
956 fprintf(f, "presentation_url=\n");
959 char uuid[45];
960 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
961 fprintf(f, "uuid=%s\n", uuid);
963 // shibby - move custom configuration before "allow" statements
964 // discussion: http://www.linksysinfo.org/index.php?threads/miniupnpd-custom-config-syntax.70863/#post-256291
965 fappend(f, "/etc/upnp/config.custom");
966 fprintf(f, "%s\n", nvram_safe_get("upnp_custom"));
968 char lanN_ipaddr[] = "lanXX_ipaddr";
969 char lanN_netmask[] = "lanXX_netmask";
970 char upnp_lanN[] = "upnp_lanXX";
971 char br;
973 for(br=0 ; br<4 ; br++) {
974 char bridge[2] = "0";
975 if (br!=0)
976 bridge[0]+=br;
977 else
978 strcpy(bridge, "");
980 sprintf(lanN_ipaddr, "lan%s_ipaddr", bridge);
981 sprintf(lanN_netmask, "lan%s_netmask", bridge);
982 sprintf(upnp_lanN, "upnp_lan%s", bridge);
984 char *lanip = nvram_safe_get(lanN_ipaddr);
985 char *lanmask = nvram_safe_get(lanN_netmask);
986 char *lanlisten = nvram_safe_get(upnp_lanN);
988 if((strcmp(lanlisten,"1")==0) && (strcmp(lanip,"")!=0) && (strcmp(lanip,"0.0.0.0")!=0)) {
989 fprintf(f,
990 "listening_ip=%s/%s\n",
991 lanip, lanmask);
992 int ports[4];
993 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
994 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
995 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
996 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
997 fprintf(f,
998 "allow %d-%d %s/%s %d-%d\n",
999 ports[0], ports[1],
1000 lanip, lanmask,
1001 ports[2], ports[3]
1004 else {
1005 // by default allow only redirection of ports above 1024
1006 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
1010 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
1011 fclose(f);
1013 xstart("miniupnpd", "-f", "/etc/upnp/config");
1019 void stop_upnp(void)
1021 if (getpid() != 1) {
1022 stop_service("upnp");
1023 return;
1026 killall_tk("miniupnpd");
1029 // -----------------------------------------------------------------------------
1031 static pid_t pid_crond = -1;
1033 void start_cron(void)
1035 stop_cron();
1037 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
1038 if (!nvram_contains_word("debug_norestart", "crond")) {
1039 pid_crond = -2;
1043 void stop_cron(void)
1045 pid_crond = -1;
1046 killall_tk("crond");
1049 // -----------------------------------------------------------------------------
1050 #ifdef LINUX26
1052 static pid_t pid_hotplug2 = -1;
1054 void start_hotplug2()
1056 stop_hotplug2();
1058 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
1059 xstart("hotplug2", "--persistent", "--no-coldplug");
1060 // FIXME: Don't remember exactly why I put "sleep" here -
1061 // but it was not for a race with check_services()... - TB
1062 sleep(1);
1064 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
1065 pid_hotplug2 = -2;
1069 void stop_hotplug2(void)
1071 pid_hotplug2 = -1;
1072 killall_tk("hotplug2");
1075 #endif /* LINUX26 */
1076 // -----------------------------------------------------------------------------
1078 // Written by Sparq in 2002/07/16
1079 void start_zebra(void)
1081 #ifdef TCONFIG_ZEBRA
1082 if (getpid() != 1) {
1083 start_service("zebra");
1084 return;
1087 FILE *fp;
1089 char *lan_tx = nvram_safe_get("dr_lan_tx");
1090 char *lan_rx = nvram_safe_get("dr_lan_rx");
1091 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
1092 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
1093 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
1094 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
1095 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
1096 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
1097 char *wan_tx = nvram_safe_get("dr_wan_tx");
1098 char *wan_rx = nvram_safe_get("dr_wan_rx");
1100 if ((*lan_tx == '0') && (*lan_rx == '0') &&
1101 (*lan1_tx == '0') && (*lan1_rx == '0') &&
1102 (*lan2_tx == '0') && (*lan2_rx == '0') &&
1103 (*lan3_tx == '0') && (*lan3_rx == '0') &&
1104 (*wan_tx == '0') && (*wan_rx == '0')) {
1105 return;
1108 // empty
1109 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
1110 fclose(fp);
1114 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
1115 char *lan_ifname = nvram_safe_get("lan_ifname");
1116 char *lan1_ifname = nvram_safe_get("lan1_ifname");
1117 char *lan2_ifname = nvram_safe_get("lan2_ifname");
1118 char *lan3_ifname = nvram_safe_get("lan3_ifname");
1119 char *wan_ifname = nvram_safe_get("wan_ifname");
1121 fprintf(fp, "router rip\n");
1122 if(strcmp(lan_ifname,"")!=0)
1123 fprintf(fp, "network %s\n", lan_ifname);
1124 if(strcmp(lan1_ifname,"")!=0)
1125 fprintf(fp, "network %s\n", lan1_ifname);
1126 if(strcmp(lan2_ifname,"")!=0)
1127 fprintf(fp, "network %s\n", lan2_ifname);
1128 if(strcmp(lan3_ifname,"")!=0)
1129 fprintf(fp, "network %s\n", lan3_ifname);
1130 fprintf(fp, "network %s\n", wan_ifname);
1131 fprintf(fp, "redistribute connected\n");
1132 //fprintf(fp, "redistribute static\n");
1134 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
1135 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
1137 if(strcmp(lan_ifname,"")!=0) {
1138 fprintf(fp, "interface %s\n", lan_ifname);
1139 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
1140 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
1142 if(strcmp(lan1_ifname,"")!=0) {
1143 fprintf(fp, "interface %s\n", lan1_ifname);
1144 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
1145 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
1147 if(strcmp(lan2_ifname,"")!=0) {
1148 fprintf(fp, "interface %s\n", lan2_ifname);
1149 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
1150 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
1152 if(strcmp(lan3_ifname,"")!=0) {
1153 fprintf(fp, "interface %s\n", lan3_ifname);
1154 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
1155 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
1157 fprintf(fp, "interface %s\n", wan_ifname);
1158 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
1159 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
1161 fprintf(fp, "router rip\n");
1162 if(strcmp(lan_ifname,"")!=0) {
1163 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
1164 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
1166 if(strcmp(lan1_ifname,"")!=0) {
1167 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
1168 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
1170 if(strcmp(lan2_ifname,"")!=0) {
1171 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
1172 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
1174 if(strcmp(lan3_ifname,"")!=0) {
1175 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
1176 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
1178 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
1179 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
1180 fprintf(fp, "access-list private deny any\n");
1182 //fprintf(fp, "debug rip events\n");
1183 //fprintf(fp, "log file /etc/ripd.log\n");
1184 fclose(fp);
1187 xstart("zebra", "-d");
1188 xstart("ripd", "-d");
1189 #endif
1192 void stop_zebra(void)
1194 #ifdef TCONFIG_ZEBRA
1195 if (getpid() != 1) {
1196 stop_service("zebra");
1197 return;
1200 killall("zebra", SIGTERM);
1201 killall("ripd", SIGTERM);
1203 unlink("/etc/zebra.conf");
1204 unlink("/etc/ripd.conf");
1205 #endif
1208 // -----------------------------------------------------------------------------
1210 void start_syslog(void)
1212 char *argv[16];
1213 int argc;
1214 char *nv;
1215 char *b_opt = "";
1216 char rem[256];
1217 int n;
1218 char s[64];
1219 char cfg[256];
1220 char *rot_siz = "50";
1221 char *rot_keep = "1";
1222 char *log_file_path;
1224 argv[0] = "syslogd";
1225 argc = 1;
1227 if (nvram_match("log_remote", "1")) {
1228 nv = nvram_safe_get("log_remoteip");
1229 if (*nv) {
1230 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
1231 argv[argc++] = "-R";
1232 argv[argc++] = rem;
1236 if (nvram_match("log_file", "1")) {
1237 argv[argc++] = "-L";
1239 if (strcmp(nvram_safe_get("log_file_size"), "") != 0) {
1240 rot_siz = nvram_safe_get("log_file_size");
1242 if (nvram_get_int("log_file_size") > 0) {
1243 rot_keep = nvram_safe_get("log_file_keep");
1246 // log to custom path - shibby
1247 if (nvram_match("log_file_custom", "1")) {
1248 log_file_path = nvram_safe_get("log_file_path");
1249 argv[argc++] = "-s";
1250 argv[argc++] = rot_siz;
1251 argv[argc++] = "-O";
1252 argv[argc++] = log_file_path;
1253 if (strcmp(nvram_safe_get("log_file_path"), "/var/log/messages") != 0) {
1254 remove("/var/log/messages");
1255 symlink(log_file_path, "/var/log/messages");
1258 else
1260 /* Read options: rotate_size(kb) num_backups logfilename.
1261 * Ignore these settings and use defaults if the logfile cannot be written to.
1263 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
1264 if ((nv = strchr(cfg, '\n')))
1265 *nv = 0;
1267 if ((nv = strtok(cfg, " \t"))) {
1268 if (isdigit(*nv))
1269 rot_siz = nv;
1272 if ((nv = strtok(NULL, " \t")))
1273 b_opt = nv;
1275 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1276 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1277 argv[argc++] = "-O";
1278 argv[argc++] = nv;
1280 else {
1281 rot_siz = "50";
1282 b_opt = "";
1287 if (nvram_match("log_file_custom", "0")) {
1288 argv[argc++] = "-s";
1289 argv[argc++] = rot_siz;
1290 struct stat sb;
1291 if (lstat("/var/log/messages", &sb) != -1)
1292 if (S_ISLNK(sb.st_mode))
1293 remove("/var/log/messages");
1296 if (isdigit(*b_opt)) {
1297 argv[argc++] = "-b";
1298 argv[argc++] = b_opt;
1299 } else
1300 if (nvram_get_int("log_file_size") > 0) {
1301 argv[argc++] = "-b";
1302 argv[argc++] = rot_keep;
1306 if (argc > 1) {
1307 argv[argc] = NULL;
1308 _eval(argv, NULL, 0, NULL);
1310 argv[0] = "klogd";
1311 argv[1] = NULL;
1312 _eval(argv, NULL, 0, NULL);
1314 // used to be available in syslogd -m
1315 n = nvram_get_int("log_mark");
1316 if (n > 0) {
1317 // n is in minutes
1318 if (n < 60)
1319 sprintf(rem, "*/%d * * * *", n);
1320 else if (n < 60 * 24)
1321 sprintf(rem, "0 */%d * * *", n / 60);
1322 else
1323 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1324 sprintf(s, "%s logger -p syslog.info -- -- MARK --", rem);
1325 eval("cru", "a", "syslogdmark", s);
1327 else {
1328 eval("cru", "d", "syslogdmark");
1333 void stop_syslog(void)
1335 killall("klogd", SIGTERM);
1336 killall("syslogd", SIGTERM);
1339 // -----------------------------------------------------------------------------
1341 static pid_t pid_igmp = -1;
1343 void start_igmp_proxy(void)
1345 FILE *fp;
1347 pid_igmp = -1;
1348 if (nvram_match("multicast_pass", "1")) {
1349 if (get_wan_proto() == WP_DISABLED)
1350 return;
1352 if (f_exists("/etc/igmp.alt")) {
1353 eval("igmpproxy", "/etc/igmp.alt");
1355 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1356 fprintf(fp,
1357 "quickleave\n"
1358 "phyint %s upstream\n"
1359 "\taltnet %s\n",
1360 // "phyint %s downstream ratelimit 0\n",
1361 get_wanface(),
1362 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1363 // nvram_safe_get("lan_ifname"));
1365 char lanN_ifname[] = "lanXX_ifname";
1366 char multicast_lanN[] = "multicast_lanXX";
1367 char br;
1369 for(br=0 ; br<4 ; br++) {
1370 char bridge[2] = "0";
1371 if (br!=0)
1372 bridge[0]+=br;
1373 else
1374 strcpy(bridge, "");
1376 sprintf(lanN_ifname, "lan%s_ifname", bridge);
1377 sprintf(multicast_lanN, "multicast_lan%s", bridge);
1379 if((strcmp(nvram_safe_get(multicast_lanN),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname),"")!=0)) {
1380 fprintf(fp,
1381 "phyint %s downstream ratelimit 0\n",
1382 nvram_safe_get(lanN_ifname));
1385 fclose(fp);
1386 eval("igmpproxy", "/etc/igmp.conf");
1388 else {
1389 return;
1391 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1392 pid_igmp = -2;
1397 void stop_igmp_proxy(void)
1399 pid_igmp = -1;
1400 killall_tk("igmpproxy");
1403 // -----------------------------------------------------------------------------
1405 void start_udpxy(void)
1407 if (nvram_match("udpxy_enable", "1")) {
1408 if (get_wan_proto() == WP_DISABLED)
1409 return;
1410 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") );
1414 void stop_udpxy(void)
1416 killall_tk("udpxy");
1419 // -----------------------------------------------------------------------------
1421 #ifdef TCONFIG_NOCAT
1423 static pid_t pid_splashd = -1;
1424 void start_splashd(void)
1426 pid_splashd = -1;
1427 start_nocat();
1428 if (!nvram_contains_word("debug_norestart", "splashd")) {
1429 pid_splashd = -2;
1433 void stop_splashd(void)
1435 pid_splashd = -1;
1436 stop_nocat();
1437 start_wan(BOOT);
1439 #endif
1441 // -----------------------------------------------------------------------------
1442 #ifdef TCONFIG_NGINX
1444 static pid_t pid_nginx = -1;
1445 void start_enginex(void)
1447 pid_nginx =-1;
1448 start_nginx();
1449 if (!nvram_contains_word("debug_norestart","enginex")) {
1450 pid_nginx = -2;
1454 void stop_enginex(void)
1456 pid_nginx = -1;
1457 stop_nginx();
1460 void start_nginxfastpath(void)
1462 pid_nginx =-1;
1463 start_nginxfp();
1464 if (!nvram_contains_word("debug_norestart","nginxfp")) {
1465 pid_nginx = -2;
1468 void stop_nginxfastpath(void)
1470 pid_nginx = -1;
1471 stop_nginxfp();
1473 #endif
1475 // -----------------------------------------------------------------------------
1477 void set_tz(void)
1479 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1482 void start_ntpc(void)
1484 set_tz();
1486 stop_ntpc();
1488 if (nvram_match("dnscrypt_proxy", "1"))
1489 eval("ntp2ip");
1491 if (nvram_get_int("ntp_updates") >= 0) {
1492 xstart("ntpsync", "--init");
1496 void stop_ntpc(void)
1498 killall("ntpsync", SIGTERM);
1501 // -----------------------------------------------------------------------------
1503 static void stop_rstats(void)
1505 int n, m;
1506 int pid;
1507 int pidz;
1508 int ppidz;
1509 int w = 0;
1511 n = 60;
1512 m = 15;
1513 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1514 w = 1;
1515 pidz = pidof("gzip");
1516 if (pidz < 1) pidz = pidof("cp");
1517 ppidz = ppid(ppid(pidz));
1518 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1519 syslog(LOG_DEBUG, "rstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1520 --m;
1521 } else {
1522 kill(pid, SIGTERM);
1524 sleep(1);
1526 if ((w == 1) && (n > 0))
1527 syslog(LOG_DEBUG, "rstats stopped.\n");
1530 static void start_rstats(int new)
1532 if (nvram_match("rstats_enable", "1")) {
1533 stop_rstats();
1534 if (new) {
1535 syslog(LOG_DEBUG, "starting rstats (new datafile).\n");
1536 xstart("rstats", "--new");
1537 } else {
1538 syslog(LOG_DEBUG, "starting rstats.\n");
1539 xstart("rstats");
1544 static void stop_cstats(void)
1546 int n, m;
1547 int pid;
1548 int pidz;
1549 int ppidz;
1550 int w = 0;
1552 n = 60;
1553 m = 15;
1554 while ((n-- > 0) && ((pid = pidof("cstats")) > 0)) {
1555 w = 1;
1556 pidz = pidof("gzip");
1557 if (pidz < 1) pidz = pidof("cp");
1558 ppidz = ppid(ppid(pidz));
1559 if ((m > 0) && (pidz > 0) && (pid == ppidz)) {
1560 syslog(LOG_DEBUG, "cstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid, pidz, ppidz);
1561 --m;
1562 } else {
1563 kill(pid, SIGTERM);
1565 sleep(1);
1567 if ((w == 1) && (n > 0))
1568 syslog(LOG_DEBUG, "cstats stopped.\n");
1571 static void start_cstats(int new)
1573 if (nvram_match("cstats_enable", "1")) {
1574 stop_cstats();
1575 if (new) {
1576 syslog(LOG_DEBUG, "starting cstats (new datafile).\n");
1577 xstart("cstats", "--new");
1578 } else {
1579 syslog(LOG_DEBUG, "starting cstats.\n");
1580 xstart("cstats");
1585 // -----------------------------------------------------------------------------
1587 // !!TB - FTP Server
1589 #ifdef TCONFIG_FTP
1590 static char *get_full_storage_path(char *val)
1592 static char buf[128];
1593 int len;
1595 if (val[0] == '/')
1596 len = sprintf(buf, "%s", val);
1597 else
1598 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1600 if (len > 1 && buf[len - 1] == '/')
1601 buf[len - 1] = 0;
1603 return buf;
1606 static char *nvram_storage_path(char *var)
1608 char *val = nvram_safe_get(var);
1609 return get_full_storage_path(val);
1612 char vsftpd_conf[] = "/etc/vsftpd.conf";
1613 char vsftpd_users[] = "/etc/vsftpd.users";
1614 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1616 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1618 static void start_ftpd(void)
1620 char tmp[256];
1621 FILE *fp, *f;
1622 char *buf;
1623 char *p, *q;
1624 int i;
1625 char *user, *pass, *rights, *root_dir;
1627 if (getpid() != 1) {
1628 start_service("ftpd");
1629 return;
1632 if (!nvram_get_int("ftp_enable")) return;
1634 mkdir_if_none(vsftpd_users);
1635 mkdir_if_none("/var/run/vsftpd");
1637 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1638 return;
1640 if (nvram_get_int("ftp_super"))
1642 /* rights */
1643 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1644 if ((f = fopen(tmp, "w")))
1646 fprintf(f,
1647 "dirlist_enable=yes\n"
1648 "write_enable=yes\n"
1649 "download_enable=yes\n");
1650 fclose(f);
1654 #ifdef TCONFIG_SAMBASRV
1655 if (nvram_match("smbd_cset", "utf8"))
1656 fprintf(fp, "utf8=yes\n");
1657 #endif
1659 if (nvram_invmatch("ftp_anonymous", "0"))
1661 fprintf(fp,
1662 "anon_allow_writable_root=yes\n"
1663 "anon_world_readable_only=no\n"
1664 "anon_umask=022\n");
1666 /* rights */
1667 sprintf(tmp, "%s/ftp", vsftpd_users);
1668 if ((f = fopen(tmp, "w")))
1670 if (nvram_match("ftp_dirlist", "0"))
1671 fprintf(f, "dirlist_enable=yes\n");
1672 if (nvram_match("ftp_anonymous", "1") ||
1673 nvram_match("ftp_anonymous", "3"))
1674 fprintf(f, "write_enable=yes\n");
1675 if (nvram_match("ftp_anonymous", "1") ||
1676 nvram_match("ftp_anonymous", "2"))
1677 fprintf(f, "download_enable=yes\n");
1678 fclose(f);
1680 if (nvram_match("ftp_anonymous", "1") ||
1681 nvram_match("ftp_anonymous", "3"))
1682 fprintf(fp,
1683 "anon_upload_enable=yes\n"
1684 "anon_mkdir_write_enable=yes\n"
1685 "anon_other_write_enable=yes\n");
1686 } else {
1687 fprintf(fp, "anonymous_enable=no\n");
1690 fprintf(fp,
1691 "dirmessage_enable=yes\n"
1692 "download_enable=no\n"
1693 "dirlist_enable=no\n"
1694 "hide_ids=yes\n"
1695 "syslog_enable=yes\n"
1696 "local_enable=yes\n"
1697 "local_umask=022\n"
1698 "chmod_enable=no\n"
1699 "chroot_local_user=yes\n"
1700 "check_shell=no\n"
1701 "log_ftp_protocol=%s\n"
1702 "user_config_dir=%s\n"
1703 "passwd_file=%s\n"
1704 "listen%s=yes\n"
1705 "listen_port=%s\n"
1706 "background=yes\n"
1707 "isolate=no\n"
1708 "max_clients=%d\n"
1709 "max_per_ip=%d\n"
1710 "max_login_fails=1\n"
1711 "idle_session_timeout=%s\n"
1712 "use_sendfile=no\n"
1713 "anon_max_rate=%d\n"
1714 "local_max_rate=%d\n"
1715 "%s\n",
1716 nvram_get_int("log_ftp") ? "yes" : "no",
1717 vsftpd_users, vsftpd_passwd,
1718 #ifdef TCONFIG_IPV6
1719 ipv6_enabled() ? "_ipv6" : "",
1720 #else
1722 #endif
1723 nvram_get("ftp_port") ? : "21",
1724 nvram_get_int("ftp_max"),
1725 nvram_get_int("ftp_ipmax"),
1726 nvram_get("ftp_staytimeout") ? : "300",
1727 nvram_get_int("ftp_anonrate") * 1024,
1728 nvram_get_int("ftp_rate") * 1024,
1729 nvram_safe_get("ftp_custom"));
1731 fclose(fp);
1733 /* prepare passwd file and default users */
1734 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1735 return;
1737 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1738 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1740 fprintf(fp, /* anonymous, admin, nobody */
1741 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1742 "%s:%s:0:0:root:/:/sbin/nologin\n"
1743 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1744 nvram_storage_path("ftp_anonroot"), user,
1745 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1746 MOUNT_ROOT);
1748 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1751 username<password<rights[<root_dir>]
1752 rights:
1753 Read/Write
1754 Read Only
1755 View Only
1756 Private
1758 p = buf;
1759 while ((q = strsep(&p, ">")) != NULL) {
1760 i = vstrsep(q, "<", &user, &pass, &rights, &root_dir);
1761 if (i < 3 || i > 4) continue;
1762 if (!user || !pass) continue;
1764 if (i == 3 || !root_dir || !(*root_dir))
1766 root_dir = nvram_safe_get("ftp_pubroot");
1768 /* directory */
1769 if (strncmp(rights, "Private", 7) == 0)
1771 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1772 mkdir_if_none(tmp);
1774 else
1775 sprintf(tmp, "%s", get_full_storage_path(root_dir));
1777 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1778 user, crypt(pass, "$1$"), user, tmp);
1780 /* rights */
1781 sprintf(tmp, "%s/%s", vsftpd_users, user);
1782 if ((f = fopen(tmp, "w")))
1784 tmp[0] = 0;
1785 if (nvram_invmatch("ftp_dirlist", "1"))
1786 strcat(tmp, "dirlist_enable=yes\n");
1787 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1788 strcat(tmp, "download_enable=yes\n");
1789 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1790 strcat(tmp, "write_enable=yes\n");
1792 fputs(tmp, f);
1793 fclose(f);
1796 free(buf);
1799 fclose(fp);
1800 killall("vsftpd", SIGHUP);
1802 /* start vsftpd if it's not already running */
1803 if (pidof("vsftpd") <= 0)
1804 xstart("vsftpd");
1807 static void stop_ftpd(void)
1809 if (getpid() != 1) {
1810 stop_service("ftpd");
1811 return;
1814 killall_tk("vsftpd");
1815 unlink(vsftpd_passwd);
1816 unlink(vsftpd_conf);
1817 eval("rm", "-rf", vsftpd_users);
1819 #endif // TCONFIG_FTP
1821 // -----------------------------------------------------------------------------
1823 // !!TB - Samba
1825 #ifdef TCONFIG_SAMBASRV
1826 static void kill_samba(int sig)
1828 if (sig == SIGTERM) {
1829 killall_tk("smbd");
1830 killall_tk("nmbd");
1832 else {
1833 killall("smbd", sig);
1834 killall("nmbd", sig);
1838 static void start_samba(void)
1840 FILE *fp;
1841 DIR *dir = NULL;
1842 struct dirent *dp;
1843 char nlsmod[15];
1844 int mode;
1845 char *nv;
1847 if (getpid() != 1) {
1848 start_service("smbd");
1849 return;
1852 mode = nvram_get_int("smbd_enable");
1853 if (!mode || !nvram_invmatch("lan_hostname", ""))
1854 return;
1856 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1857 return;
1859 fprintf(fp, "[global]\n"
1860 " interfaces = %s\n"
1861 " bind interfaces only = yes\n"
1862 " workgroup = %s\n"
1863 " netbios name = %s\n"
1864 " server string = %s\n"
1865 " guest account = nobody\n"
1866 " security = user\n"
1867 " %s\n"
1868 " guest ok = %s\n"
1869 " guest only = no\n"
1870 " browseable = yes\n"
1871 " syslog only = yes\n"
1872 " timestamp logs = no\n"
1873 " syslog = 1\n"
1874 " encrypt passwords = yes\n"
1875 " preserve case = yes\n"
1876 " short preserve case = yes\n",
1877 nvram_safe_get("lan_ifname"),
1878 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1879 nvram_safe_get("lan_hostname"),
1880 nvram_get("router_name") ? : "Tomato",
1881 mode == 2 ? "" : "map to guest = Bad User",
1882 mode == 2 ? "no" : "yes" // guest ok
1885 if (nvram_get_int("smbd_wins")) {
1886 nv = nvram_safe_get("wan_wins");
1887 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1888 fprintf(fp, " wins support = yes\n");
1892 if (nvram_get_int("smbd_master")) {
1893 fprintf(fp,
1894 " domain master = yes\n"
1895 " local master = yes\n"
1896 " preferred master = yes\n"
1897 " os level = 65\n");
1900 nv = nvram_safe_get("smbd_cpage");
1901 if (*nv) {
1902 #ifndef TCONFIG_SAMBA3
1903 fprintf(fp, " client code page = %s\n", nv);
1904 #endif
1905 sprintf(nlsmod, "nls_cp%s", nv);
1907 nv = nvram_safe_get("smbd_nlsmod");
1908 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1909 modprobe_r(nv);
1911 modprobe(nlsmod);
1912 nvram_set("smbd_nlsmod", nlsmod);
1915 #ifndef TCONFIG_SAMBA3
1916 if (nvram_match("smbd_cset", "utf8"))
1917 fprintf(fp, " coding system = utf8\n");
1918 else if (nvram_invmatch("smbd_cset", ""))
1919 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1920 #endif
1922 nv = nvram_safe_get("smbd_custom");
1923 /* add socket options unless overriden by the user */
1924 if (strstr(nv, "socket options") == NULL) {
1925 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1927 fprintf(fp, "%s\n\n", nv);
1929 /* configure shares */
1931 char *buf;
1932 char *p, *q;
1933 char *name, *path, *comment, *writeable, *hidden;
1934 int cnt = 0;
1936 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1938 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1940 p = buf;
1941 while ((q = strsep(&p, ">")) != NULL) {
1942 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1943 if (!path || !name) continue;
1945 /* share name */
1946 fprintf(fp, "\n[%s]\n", name);
1948 /* path */
1949 fprintf(fp, " path = %s\n", path);
1951 /* access level */
1952 if (!strcmp(writeable, "1"))
1953 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1954 if (!strcmp(hidden, "1"))
1955 fprintf(fp, " browseable = no\n");
1957 /* comment */
1958 if (comment)
1959 fprintf(fp, " comment = %s\n", comment);
1961 cnt++;
1963 free(buf);
1966 /* Share every mountpoint below MOUNT_ROOT */
1967 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1968 while ((dp = readdir(dir))) {
1969 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1971 /* Only if is a directory and is mounted */
1972 if (!dir_is_mountpoint(MOUNT_ROOT, dp->d_name))
1973 continue;
1975 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1976 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1977 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1978 if (nvram_match("smbd_autoshare", "3")) // Hidden
1979 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1980 dp->d_name, MOUNT_ROOT, dp->d_name);
1981 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1982 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1984 cnt++;
1988 if (dir) closedir(dir);
1990 if (cnt == 0) {
1991 /* by default share MOUNT_ROOT as read-only */
1992 fprintf(fp, "\n[share]\n"
1993 " path = %s\n"
1994 " writable = no\n",
1995 MOUNT_ROOT);
1998 fclose(fp);
2000 mkdir_if_none("/var/run/samba");
2001 mkdir_if_none("/etc/samba");
2003 /* write smbpasswd */
2004 #ifdef TCONFIG_SAMBA3
2005 eval("smbpasswd", "nobody", "\"\"");
2006 #else
2007 eval("smbpasswd", "-a", "nobody", "\"\"");
2008 #endif
2009 if (mode == 2) {
2010 char *smbd_user;
2011 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
2012 smbd_user = "nas";
2013 #ifdef TCONFIG_SAMBA3
2014 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
2015 #else
2016 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
2017 #endif
2020 kill_samba(SIGHUP);
2021 int ret1 = 0, ret2 = 0;
2022 /* start samba if it's not already running */
2023 if (pidof("nmbd") <= 0)
2024 ret1 = xstart("nmbd", "-D");
2025 if (pidof("smbd") <= 0)
2026 ret2 = xstart("smbd", "-D");
2028 if (ret1 || ret2) kill_samba(SIGTERM);
2031 static void stop_samba(void)
2033 if (getpid() != 1) {
2034 stop_service("smbd");
2035 return;
2038 kill_samba(SIGTERM);
2039 /* clean up */
2040 unlink("/var/log/smb");
2041 unlink("/var/log/nmb");
2042 eval("rm", "-rf", "/var/run/samba");
2044 #endif // TCONFIG_SAMBASRV
2046 #ifdef TCONFIG_MEDIA_SERVER
2047 #define MEDIA_SERVER_APP "minidlna"
2049 static void start_media_server(void)
2051 FILE *f;
2052 int port, pid, https;
2053 char *dbdir;
2054 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
2055 static int once = 1;
2057 if (getpid() != 1) {
2058 start_service("media");
2059 return;
2062 if (nvram_get_int("ms_sas") == 0)
2063 once = 0;
2065 if (nvram_get_int("ms_enable") != 0) {
2066 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
2067 // no forced rescan
2068 argv[3] = NULL;
2070 nvram_unset("ms_rescan");
2072 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
2073 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
2075 else {
2076 if ((f = fopen(argv[2], "w")) != NULL) {
2077 port = nvram_get_int("ms_port");
2078 https = nvram_get_int("https_enable");
2079 dbdir = nvram_safe_get("ms_dbdir");
2080 if (!(*dbdir)) dbdir = NULL;
2081 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
2083 fprintf(f,
2084 "network_interface=%s\n"
2085 "port=%d\n"
2086 "friendly_name=%s\n"
2087 "db_dir=%s/.db\n"
2088 "enable_tivo=%s\n"
2089 "strict_dlna=%s\n"
2090 "presentation_url=http%s://%s:%s/nas-media.asp\n"
2091 "inotify=yes\n"
2092 "notify_interval=600\n"
2093 "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"
2094 "log_dir=/var/log\n"
2095 "log_level=general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn\n"
2096 "\n",
2097 nvram_safe_get("lan_ifname"),
2098 (port < 0) || (port >= 0xffff) ? 0 : port,
2099 nvram_get("router_name") ? : "Tomato",
2100 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
2101 nvram_get_int("ms_tivo") ? "yes" : "no",
2102 nvram_get_int("ms_stdlna") ? "yes" : "no",
2103 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
2106 // media directories
2107 char *buf, *p, *q;
2108 char *path, *restrict;
2110 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
2111 /* path<restrict[A|V|P|] */
2113 p = buf;
2114 while ((q = strsep(&p, ">")) != NULL) {
2115 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
2116 continue;
2117 fprintf(f, "media_dir=%s%s%s\n",
2118 restrict ? : "", (restrict && *restrict) ? "," : "", path);
2120 free(buf);
2123 fclose(f);
2127 /* start media server if it's not already running */
2128 if (pidof(MEDIA_SERVER_APP) <= 0) {
2129 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
2130 /* If we started the media server successfully, wait 1 sec
2131 * to let it die if it can't open the database file.
2132 * If it's still alive after that, assume it's running and
2133 * disable forced once-after-reboot rescan.
2135 sleep(1);
2136 if (pidof(MEDIA_SERVER_APP) > 0)
2137 once = 0;
2143 static void stop_media_server(void)
2145 if (getpid() != 1) {
2146 stop_service("media");
2147 return;
2150 killall_tk(MEDIA_SERVER_APP);
2152 #endif // TCONFIG_MEDIA_SERVER
2154 #ifdef TCONFIG_USB
2155 static void start_nas_services(void)
2157 if (getpid() != 1) {
2158 start_service("usbapps");
2159 return;
2162 #ifdef TCONFIG_SAMBASRV
2163 start_samba();
2164 #endif
2165 #ifdef TCONFIG_FTP
2166 start_ftpd();
2167 #endif
2168 #ifdef TCONFIG_MEDIA_SERVER
2169 start_media_server();
2170 #endif
2173 static void stop_nas_services(void)
2175 if (getpid() != 1) {
2176 stop_service("usbapps");
2177 return;
2180 #ifdef TCONFIG_MEDIA_SERVER
2181 stop_media_server();
2182 #endif
2183 #ifdef TCONFIG_FTP
2184 stop_ftpd();
2185 #endif
2186 #ifdef TCONFIG_SAMBASRV
2187 stop_samba();
2188 #endif
2191 void restart_nas_services(int stop, int start)
2193 int fd = file_lock("usb");
2194 /* restart all NAS applications */
2195 if (stop)
2196 stop_nas_services();
2197 if (start)
2198 start_nas_services();
2199 file_unlock(fd);
2201 #endif // TCONFIG_USB
2203 // -----------------------------------------------------------------------------
2205 /* -1 = Don't check for this program, it is not expected to be running.
2206 * Other = This program has been started and should be kept running. If no
2207 * process with the name is running, call func to restart it.
2208 * Note: At startup, dnsmasq forks a short-lived child which forks a
2209 * long-lived (grand)child. The parents terminate.
2210 * Many daemons use this technique.
2212 static void _check(pid_t pid, const char *name, void (*func)(void))
2214 if (pid == -1) return;
2216 if (pidof(name) > 0) return;
2218 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
2219 func();
2221 // Force recheck in 500 msec
2222 setitimer(ITIMER_REAL, &pop_tv, NULL);
2225 void check_services(void)
2227 TRACE_PT("keep alive\n");
2229 // Periodically reap any zombies
2230 setitimer(ITIMER_REAL, &zombie_tv, NULL);
2232 #ifdef LINUX26
2233 _check(pid_hotplug2, "hotplug2", start_hotplug2);
2234 #endif
2235 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
2236 _check(pid_crond, "crond", start_cron);
2237 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
2239 //#ifdef TCONFIG_NOCAT
2240 // if (nvram_get_int("NC_enable"))
2241 // _check(&pid_splashd, "splashd", start_splashd);
2242 //#endif
2246 // -----------------------------------------------------------------------------
2248 void start_services(void)
2250 static int once = 1;
2252 if (once) {
2253 once = 0;
2255 if (nvram_get_int("telnetd_eas")) start_telnetd();
2256 if (nvram_get_int("sshd_eas")) start_sshd();
2259 // start_syslog();
2260 start_nas();
2261 start_zebra();
2262 #ifdef TCONFIG_SDHC
2263 start_mmc();
2264 #endif
2265 start_dnsmasq();
2266 start_cifs();
2267 start_httpd();
2268 #ifdef TCONFIG_NGINX
2269 start_enginex();
2270 start_mysql();
2271 #endif
2272 start_cron();
2273 // start_upnp();
2274 start_rstats(0);
2275 start_cstats(0);
2276 start_sched();
2277 #ifdef TCONFIG_PPTPD
2278 start_pptpd();
2279 #endif
2280 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2282 #ifdef TCONFIG_SNMP
2283 start_snmp();
2284 #endif
2286 start_tomatoanon();
2288 #ifdef TCONFIG_TOR
2289 start_tor();
2290 #endif
2292 #ifdef TCONFIG_BT
2293 start_bittorrent();
2294 #endif
2296 #ifdef TCONFIG_NOCAT
2297 start_splashd();
2298 #endif
2300 #ifdef TCONFIG_NFS
2301 start_nfs();
2302 #endif
2305 void stop_services(void)
2307 clear_resolv();
2309 #ifdef TCONFIG_BT
2310 stop_bittorrent();
2311 #endif
2313 #ifdef TCONFIG_NOCAT
2314 stop_splashd();
2315 #endif
2317 #ifdef TCONFIG_SNMP
2318 stop_snmp();
2319 #endif
2321 #ifdef TCONFIG_TOR
2322 stop_tor();
2323 #endif
2325 stop_tomatoanon();
2327 #ifdef TCONFIG_NFS
2328 stop_nfs();
2329 #endif
2330 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2331 #ifdef TCONFIG_PPTPD
2332 stop_pptpd();
2333 #endif
2334 stop_sched();
2335 stop_rstats();
2336 stop_cstats();
2337 // stop_upnp();
2338 stop_cron();
2339 stop_httpd();
2340 #ifdef TCONFIG_NGINX
2341 stop_mysql();
2342 stop_enginex();
2343 #endif
2344 #ifdef TCONFIG_SDHC
2345 stop_mmc();
2346 #endif
2347 stop_cifs();
2348 stop_dnsmasq();
2349 stop_zebra();
2350 stop_nas();
2351 // stop_syslog();
2354 // -----------------------------------------------------------------------------
2356 /* nvram "action_service" is: "service-action[-modifier]"
2357 * action is something like "stop" or "start" or "restart"
2358 * optional modifier is "c" for the "service" command-line command
2360 void exec_service(void)
2362 const int A_START = 1;
2363 const int A_STOP = 2;
2364 const int A_RESTART = 1|2;
2365 char buffer[128];
2366 char *service;
2367 char *act;
2368 char *next;
2369 char *modifier;
2370 int action, user;
2371 int i;
2373 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
2374 next = buffer;
2376 TOP:
2377 act = strsep(&next, ",");
2378 service = strsep(&act, "-");
2379 if (act == NULL) {
2380 next = NULL;
2381 goto CLEAR;
2383 modifier = act;
2384 strsep(&modifier, "-");
2386 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
2388 if (strcmp(act, "start") == 0) action = A_START;
2389 else if (strcmp(act, "stop") == 0) action = A_STOP;
2390 else if (strcmp(act, "restart") == 0) action = A_RESTART;
2391 else action = 0;
2392 user = (modifier != NULL && *modifier == 'c');
2394 if (strcmp(service, "dhcpc") == 0) {
2395 if (action & A_STOP) stop_dhcpc();
2396 if (action & A_START) start_dhcpc();
2397 goto CLEAR;
2400 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
2401 if (action & A_STOP) stop_dnsmasq();
2402 if (action & A_START) {
2403 dns_to_resolv();
2404 start_dnsmasq();
2406 goto CLEAR;
2409 if (strcmp(service, "firewall") == 0) {
2410 if (action & A_STOP) {
2411 stop_firewall();
2412 stop_igmp_proxy();
2413 stop_udpxy();
2415 if (action & A_START) {
2416 start_firewall();
2417 start_igmp_proxy();
2418 start_udpxy();
2420 goto CLEAR;
2423 if (strcmp(service, "restrict") == 0) {
2424 if (action & A_STOP) {
2425 stop_firewall();
2427 if (action & A_START) {
2428 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2430 start_firewall();
2432 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2433 if (i == 1) {
2434 if (nvram_get_int("rrules_radio") < 0) {
2435 eval("radio", "on");
2439 goto CLEAR;
2442 if (strcmp(service, "arpbind") == 0) {
2443 if (action & A_STOP) stop_arpbind();
2444 if (action & A_START) start_arpbind();
2445 goto CLEAR;
2448 if (strcmp(service, "qos") == 0) {
2449 if (action & A_STOP) {
2450 stop_qos();
2452 stop_firewall(); start_firewall(); // always restarted
2453 if (action & A_START) {
2454 start_qos();
2455 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2457 goto CLEAR;
2460 if (strcmp(service, "qoslimit") == 0) {
2461 if (action & A_STOP) {
2462 new_qoslimit_stop();
2464 #ifdef TCONFIG_NOCAT
2465 stop_splashd();
2466 #endif
2467 stop_firewall(); start_firewall(); // always restarted
2468 if (action & A_START) {
2469 new_qoslimit_start();
2471 #ifdef TCONFIG_NOCAT
2472 start_splashd();
2473 #endif
2474 goto CLEAR;
2477 if (strcmp(service, "upnp") == 0) {
2478 if (action & A_STOP) {
2479 stop_upnp();
2481 stop_firewall(); start_firewall(); // always restarted
2482 if (action & A_START) {
2483 start_upnp();
2485 goto CLEAR;
2488 if (strcmp(service, "telnetd") == 0) {
2489 if (action & A_STOP) stop_telnetd();
2490 if (action & A_START) start_telnetd();
2491 goto CLEAR;
2494 if (strcmp(service, "sshd") == 0) {
2495 if (action & A_STOP) stop_sshd();
2496 if (action & A_START) start_sshd();
2497 goto CLEAR;
2500 if (strcmp(service, "httpd") == 0) {
2501 if (action & A_STOP) stop_httpd();
2502 if (action & A_START) start_httpd();
2503 goto CLEAR;
2506 #ifdef TCONFIG_IPV6
2507 if (strcmp(service, "ipv6") == 0) {
2508 if (action & A_STOP) {
2509 stop_dnsmasq();
2510 stop_ipv6();
2512 if (action & A_START) {
2513 start_ipv6();
2514 start_dnsmasq();
2516 goto CLEAR;
2519 if (strncmp(service, "dhcp6", 5) == 0) {
2520 if (action & A_STOP) {
2521 stop_dhcp6c();
2523 if (action & A_START) {
2524 start_dhcp6c();
2526 goto CLEAR;
2528 #endif
2530 if (strcmp(service, "admin") == 0) {
2531 if (action & A_STOP) {
2532 stop_sshd();
2533 stop_telnetd();
2534 stop_httpd();
2536 stop_firewall(); start_firewall(); // always restarted
2537 if (action & A_START) {
2538 start_httpd();
2539 create_passwd();
2540 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2541 if (nvram_match("sshd_eas", "1")) start_sshd();
2543 goto CLEAR;
2546 if (strcmp(service, "ddns") == 0) {
2547 if (action & A_STOP) stop_ddns();
2548 if (action & A_START) start_ddns();
2549 goto CLEAR;
2552 if (strcmp(service, "ntpc") == 0) {
2553 if (action & A_STOP) stop_ntpc();
2554 if (action & A_START) start_ntpc();
2555 goto CLEAR;
2558 if (strcmp(service, "logging") == 0) {
2559 if (action & A_STOP) {
2560 stop_syslog();
2562 if (action & A_START) {
2563 start_syslog();
2565 if (!user) {
2566 // always restarted except from "service" command
2567 stop_cron(); start_cron();
2568 stop_firewall(); start_firewall();
2570 goto CLEAR;
2573 if (strcmp(service, "crond") == 0) {
2574 if (action & A_STOP) {
2575 stop_cron();
2577 if (action & A_START) {
2578 start_cron();
2580 goto CLEAR;
2583 #ifdef LINUX26
2584 if (strncmp(service, "hotplug", 7) == 0) {
2585 if (action & A_STOP) {
2586 stop_hotplug2();
2588 if (action & A_START) {
2589 start_hotplug2(1);
2591 goto CLEAR;
2593 #endif
2595 if (strcmp(service, "upgrade") == 0) {
2596 if (action & A_START) {
2597 #if TOMATO_SL
2598 stop_usbevent();
2599 stop_smbd();
2600 #endif
2601 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2602 stop_jffs2();
2603 // stop_cifs();
2604 stop_zebra();
2605 stop_cron();
2606 stop_ntpc();
2607 stop_upnp();
2608 // stop_dhcpc();
2609 killall("rstats", SIGTERM);
2610 killall("cstats", SIGTERM);
2611 killall("buttons", SIGTERM);
2612 stop_syslog();
2613 remove_storage_main(1); // !!TB - USB Support
2614 stop_usb(); // !!TB - USB Support
2616 goto CLEAR;
2619 #ifdef TCONFIG_CIFS
2620 if (strcmp(service, "cifs") == 0) {
2621 if (action & A_STOP) stop_cifs();
2622 if (action & A_START) start_cifs();
2623 goto CLEAR;
2625 #endif
2627 #ifdef TCONFIG_JFFS2
2628 if (strncmp(service, "jffs", 4) == 0) {
2629 if (action & A_STOP) stop_jffs2();
2630 if (action & A_START) start_jffs2();
2631 goto CLEAR;
2633 #endif
2635 if (strcmp(service, "zebra") == 0) {
2636 if (action & A_STOP) stop_zebra();
2637 if (action & A_START) start_zebra();
2638 goto CLEAR;
2641 #ifdef TCONFIG_SDHC
2642 if (strcmp(service, "mmc") == 0) {
2643 if (action & A_STOP) stop_mmc();
2644 if (action & A_START) start_mmc();
2645 goto CLEAR;
2647 #endif
2649 if (strcmp(service, "routing") == 0) {
2650 if (action & A_STOP) {
2651 stop_zebra();
2652 do_static_routes(0); // remove old '_saved'
2653 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2654 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2655 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2656 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2657 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2658 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2659 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2661 stop_firewall();
2662 start_firewall();
2663 if (action & A_START) {
2664 do_static_routes(1); // add new
2665 start_zebra();
2666 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2667 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2668 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2669 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2670 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2671 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2672 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2674 goto CLEAR;
2677 if (strcmp(service, "ctnf") == 0) {
2678 if (action & A_START) {
2679 setup_conntrack();
2680 stop_firewall();
2681 start_firewall();
2683 goto CLEAR;
2686 if (strcmp(service, "wan") == 0) {
2687 if (action & A_STOP) {
2688 stop_wan();
2691 if (action & A_START) {
2692 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2693 start_wan(BOOT);
2694 sleep(2);
2695 force_to_dial();
2697 goto CLEAR;
2700 if (strcmp(service, "net") == 0) {
2701 if (action & A_STOP) {
2702 #ifdef TCONFIG_USB
2703 stop_nas_services();
2704 #endif
2705 stop_httpd();
2706 stop_dnsmasq();
2707 stop_nas();
2708 stop_wan();
2709 stop_arpbind();
2710 stop_lan();
2711 stop_vlan();
2713 if (action & A_START) {
2714 start_vlan();
2715 start_lan();
2716 start_arpbind();
2717 start_wan(BOOT);
2718 start_nas();
2719 start_dnsmasq();
2720 start_httpd();
2721 start_wl();
2722 #ifdef TCONFIG_USB
2723 start_nas_services();
2724 #endif
2726 goto CLEAR;
2729 if (strcmp(service, "wireless") == 0) {
2730 if(action & A_STOP) {
2731 stop_wireless();
2733 if(action & A_START) {
2734 start_wireless();
2736 goto CLEAR;
2739 if (strcmp(service, "wl") == 0) {
2740 if(action & A_STOP) {
2741 stop_wireless();
2742 unload_wl();
2744 if(action & A_START) {
2745 load_wl();
2746 start_wireless();
2747 stop_wireless();
2748 start_wireless();
2750 goto CLEAR;
2753 if (strcmp(service, "nas") == 0) {
2754 if (action & A_STOP) {
2755 stop_nas();
2757 if (action & A_START) {
2758 start_nas();
2759 start_wl();
2761 goto CLEAR;
2764 if (strcmp(service, "rstats") == 0) {
2765 if (action & A_STOP) stop_rstats();
2766 if (action & A_START) start_rstats(0);
2767 goto CLEAR;
2770 if (strcmp(service, "rstatsnew") == 0) {
2771 if (action & A_STOP) stop_rstats();
2772 if (action & A_START) start_rstats(1);
2773 goto CLEAR;
2776 if (strcmp(service, "cstats") == 0) {
2777 if (action & A_STOP) stop_cstats();
2778 if (action & A_START) start_cstats(0);
2779 goto CLEAR;
2782 if (strcmp(service, "cstatsnew") == 0) {
2783 if (action & A_STOP) stop_cstats();
2784 if (action & A_START) start_cstats(1);
2785 goto CLEAR;
2788 if (strcmp(service, "sched") == 0) {
2789 if (action & A_STOP) stop_sched();
2790 if (action & A_START) start_sched();
2791 goto CLEAR;
2794 #ifdef TCONFIG_BT
2795 if (strcmp(service, "bittorrent") == 0) {
2796 if (action & A_STOP) {
2797 stop_bittorrent();
2799 stop_firewall(); start_firewall(); // always restarted
2800 if (action & A_START) {
2801 start_bittorrent();
2803 goto CLEAR;
2805 #endif
2807 #ifdef TCONFIG_NFS
2808 if (strcmp(service, "nfs") == 0) {
2809 if (action & A_STOP) stop_nfs();
2810 if (action & A_START) start_nfs();
2811 goto CLEAR;
2813 #endif
2815 #ifdef TCONFIG_SNMP
2816 if (strcmp(service, "snmp") == 0) {
2817 if (action & A_STOP) stop_snmp();
2818 if (action & A_START) start_snmp();
2819 goto CLEAR;
2821 #endif
2823 #ifdef TCONFIG_TOR
2824 if (strcmp(service, "tor") == 0) {
2825 if (action & A_STOP) stop_tor();
2827 stop_firewall(); start_firewall(); // always restarted
2829 if (action & A_START) start_tor();
2830 goto CLEAR;
2832 #endif
2834 #ifdef TCONFIG_UPS
2835 if (strcmp(service, "ups") == 0) {
2836 if (action & A_STOP) stop_ups();
2837 if (action & A_START) start_ups();
2838 goto CLEAR;
2840 #endif
2842 if (strcmp(service, "tomatoanon") == 0) {
2843 if (action & A_STOP) stop_tomatoanon();
2844 if (action & A_START) start_tomatoanon();
2845 goto CLEAR;
2848 #ifdef TCONFIG_USB
2849 // !!TB - USB Support
2850 if (strcmp(service, "usb") == 0) {
2851 if (action & A_STOP) stop_usb();
2852 if (action & A_START) {
2853 start_usb();
2854 // restart Samba and ftp since they may be killed by stop_usb()
2855 restart_nas_services(0, 1);
2856 // remount all partitions by simulating hotplug event
2857 add_remove_usbhost("-1", 1);
2859 goto CLEAR;
2862 if (strcmp(service, "usbapps") == 0) {
2863 if (action & A_STOP) stop_nas_services();
2864 if (action & A_START) start_nas_services();
2865 goto CLEAR;
2867 #endif
2869 #ifdef TCONFIG_FTP
2870 // !!TB - FTP Server
2871 if (strcmp(service, "ftpd") == 0) {
2872 if (action & A_STOP) stop_ftpd();
2873 setup_conntrack();
2874 stop_firewall();
2875 start_firewall();
2876 if (action & A_START) start_ftpd();
2877 goto CLEAR;
2879 #endif
2881 #ifdef TCONFIG_MEDIA_SERVER
2882 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2883 if (action & A_STOP) stop_media_server();
2884 if (action & A_START) start_media_server();
2885 goto CLEAR;
2887 #endif
2889 #ifdef TCONFIG_SAMBASRV
2890 // !!TB - Samba
2891 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2892 if (action & A_STOP) stop_samba();
2893 if (action & A_START) {
2894 create_passwd();
2895 stop_dnsmasq();
2896 start_dnsmasq();
2897 start_samba();
2899 goto CLEAR;
2901 #endif
2903 #ifdef TCONFIG_OPENVPN
2904 if (strncmp(service, "vpnclient", 9) == 0) {
2905 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2906 if (action & A_START) start_vpnclient(atoi(&service[9]));
2907 goto CLEAR;
2910 if (strncmp(service, "vpnserver", 9) == 0) {
2911 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2912 if (action & A_START) start_vpnserver(atoi(&service[9]));
2913 goto CLEAR;
2915 #endif
2917 #ifdef TCONFIG_TINC
2918 if (strcmp(service, "tinc") == 0) {
2919 if (action & A_STOP) stop_tinc();
2920 if (action & A_START) start_tinc();
2921 goto CLEAR;
2923 #endif
2925 #ifdef TCONFIG_NOCAT
2926 if (strcmp(service, "splashd") == 0) {
2927 if (action & A_STOP) stop_splashd();
2928 if (action & A_START) start_splashd();
2929 goto CLEAR;
2931 #endif
2933 #ifdef TCONFIG_NGINX
2934 if (strcmp(service, "enginex") == 0) {
2935 if (action & A_STOP) stop_enginex();
2936 stop_firewall(); start_firewall(); // always restarted
2937 if (action & A_START) start_enginex();
2938 goto CLEAR;
2940 if (strcmp(service, "nginxfp") == 0) {
2941 if (action & A_STOP) stop_nginxfastpath();
2942 stop_firewall(); start_firewall(); // always restarted
2943 if (action & A_START) start_nginxfastpath();
2944 goto CLEAR;
2946 if (strcmp(service, "mysql") == 0) {
2947 if (action & A_STOP) stop_mysql();
2948 stop_firewall(); start_firewall(); // always restarted
2949 if (action & A_START) start_mysql();
2950 goto CLEAR;
2952 #endif
2954 #ifdef TCONFIG_PPTPD
2955 if (strcmp(service, "pptpd") == 0) {
2956 if (action & A_STOP) stop_pptpd();
2957 if (action & A_START) start_pptpd();
2958 goto CLEAR;
2960 #endif
2962 #ifdef TCONFIG_PPTPD
2963 if (strcmp(service, "pptpclient") == 0) {
2964 if (action & A_STOP) stop_pptp_client();
2965 if (action & A_START) start_pptp_client();
2966 if (action & (A_START | A_STOP))
2968 stop_dnsmasq();
2969 dns_to_resolv();
2970 start_dnsmasq();
2971 if ((action & A_START) == 0)
2972 clear_pptp_route();
2974 goto CLEAR;
2976 #endif
2978 CLEAR:
2979 if (next) goto TOP;
2981 // some functions check action_service and must be cleared at end -- zzz
2982 nvram_set("action_service", "");
2984 // Force recheck in 500 msec
2985 setitimer(ITIMER_REAL, &pop_tv, NULL);
2988 static void do_service(const char *name, const char *action, int user)
2990 int n;
2991 char s[64];
2993 n = 150;
2994 while (!nvram_match("action_service", "")) {
2995 if (user) {
2996 putchar('*');
2997 fflush(stdout);
2999 else if (--n < 0) break;
3000 usleep(100 * 1000);
3003 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
3004 nvram_set("action_service", s);
3006 if (nvram_match("debug_rc_svc", "1")) {
3007 nvram_unset("debug_rc_svc");
3008 exec_service();
3009 } else {
3010 kill(1, SIGUSR1);
3013 n = 150;
3014 while (nvram_match("action_service", s)) {
3015 if (user) {
3016 putchar('.');
3017 fflush(stdout);
3019 else if (--n < 0) {
3020 break;
3022 usleep(100 * 1000);
3026 int service_main(int argc, char *argv[])
3028 if (argc != 3) usage_exit(argv[0], "<service> <action>");
3029 do_service(argv[1], argv[2], 1);
3030 printf("\nDone.\n");
3031 return 0;
3034 void start_service(const char *name)
3036 do_service(name, "start", 0);
3039 void stop_service(const char *name)
3041 do_service(name, "stop", 0);
3045 void restart_service(const char *name)
3047 do_service(name, "restart", 0);