Routing is now multi-LAN aware (static routes and Zebra/RIP, if enabled)
[tomato.git] / release / src / router / rc / services.c
blob6edc95c70266f78976e564803e954d4b5a1724bb
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 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
50 // Pop an alarm to recheck pids in 500 msec.
51 static const struct itimerval pop_tv = { {0,0}, {0, 500 * 1000} };
53 // Pop an alarm to reap zombies.
54 static const struct itimerval zombie_tv = { {0,0}, {307, 0} };
56 // -----------------------------------------------------------------------------
58 static const char dmhosts[] = "/etc/hosts.dnsmasq";
59 static const char dmresolv[] = "/etc/resolv.dnsmasq";
61 static pid_t pid_dnsmasq = -1;
63 static int is_wet(int idx, int unit, int subunit, void *param)
65 return nvram_match(wl_nvname("mode", unit, subunit), "wet");
68 void start_dnsmasq()
70 FILE *f;
71 const char *nv;
72 char buf[512];
73 char lan[24];
74 const char *router_ip;
75 // const char *lan_ifname;
76 char sdhcp_lease[32];
77 char *e;
78 int n;
79 char *mac, *ip, *name;
80 char *p;
81 int ipn;
82 char ipbuf[32];
83 FILE *hf;
84 int dhcp_start;
85 int dhcp_count;
86 int dhcp_lease;
87 int do_dhcpd;
88 int do_dns;
90 TRACE_PT("begin\n");
92 if (getpid() != 1) {
93 start_service("dnsmasq");
94 return;
97 stop_dnsmasq();
99 if (foreach_wif(1, NULL, is_wet)) return;
101 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
103 // lan_ifname = nvram_safe_get("lan_ifname");
104 router_ip = nvram_safe_get("lan_ipaddr");
105 // strlcpy(lan, router_ip, sizeof(lan));
106 // if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
108 fprintf(f,
109 "pid-file=/var/run/dnsmasq.pid\n");
110 // "interface=%s\n",
111 // lan_ifname);
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" // "
124 "expand-hosts\n" // expand hostnames in hosts file
125 "min-port=%u\n", // min port used for random src port
126 dmresolv, dmhosts, n);
127 do_dns = nvram_match("dhcpd_dmdns", "1");
129 // DNS rebinding protection, will discard upstream RFC1918 responses
130 if (nvram_get_int("dns_norebind")) {
131 fprintf(f,
132 "stop-dns-rebind\n"
133 "rebind-localhost-ok\n");
134 // allow RFC1918 responses for server domain
135 switch (get_wan_proto()) {
136 case WP_PPTP:
137 nv = nvram_get("pptp_server_ip");
138 break;
139 case WP_L2TP:
140 nv = nvram_get("l2tp_server_ip");
141 break;
142 default:
143 nv = NULL;
144 break;
146 if (nv && *nv) fprintf(f, "rebind-domain-ok=%s\n", nv);
149 for (n = 0 ; n < dns->count; ++n) {
150 if (dns->dns[n].port != 53) {
151 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
155 // dhcp
156 char tmp[32];
157 char tmp2[32];
158 char tmp3[32];
159 char br;
160 for(br=0 ; br<4 ; br++) {
161 char bridge[2] = "0";
162 if (br!=0)
163 bridge[0]+=br;
164 else
165 strcpy(bridge, "");
167 strcpy(tmp,"lan");
168 strcat(tmp,bridge);
169 strcat(tmp, "_proto");
170 do_dhcpd = nvram_match(tmp, "dhcp");
171 // do_dhcpd = nvram_match("lan_proto", "dhcp");
172 if (do_dhcpd) {
173 strcpy(tmp,"lan");
174 strcat(tmp,bridge);
175 strcat(tmp, "_ipaddr");
176 router_ip = nvram_safe_get(tmp);
177 strlcpy(lan, router_ip, sizeof(lan));
178 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
180 strcpy(tmp,"lan");
181 strcat(tmp,bridge);
182 strcat(tmp, "_ifname");
184 fprintf(f,
185 "interface=%s\n",
186 nvram_safe_get(tmp));
188 strcpy(tmp,"dhcp");
189 strcat(tmp,bridge);
190 strcat(tmp, "_lease");
191 // dhcp_lease = nvram_get_int("dhcp_lease");
192 dhcp_lease = nvram_get_int(tmp);
193 if (dhcp_lease <= 0) dhcp_lease = 1440;
195 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
196 if (n < 0) strcpy(sdhcp_lease, "infinite");
197 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
199 if (!do_dns) {
200 // if not using dnsmasq for dns
202 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
203 // no DNS might be temporary. use a low lease time to force clients to update.
204 dhcp_lease = 2;
205 strcpy(sdhcp_lease, "2m");
206 do_dns = 1;
208 else {
209 // pass the dns directly
210 buf[0] = 0;
211 for (n = 0 ; n < dns->count; ++n) {
212 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
213 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
216 fprintf(f, "dhcp-option=6%s\n", buf);
220 strcpy(tmp,"dhcpd");
221 strcat(tmp,bridge);
222 strcat(tmp, "_startip");
223 strcpy(tmp2,"dhcpd");
224 strcat(tmp2,bridge);
225 strcat(tmp2, "_endip");
226 strcpy(tmp3,"lan");
227 strcat(tmp3,bridge);
228 strcat(tmp3, "_netmask");
229 // if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
230 // fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
231 if ((p = nvram_get(tmp)) && (*p) && (e = nvram_get(tmp2)) && (*e)) {
232 fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get(tmp3), dhcp_lease);
234 else {
235 // for compatibility
236 // dhcp_start = nvram_get_int("dhcp_start");
237 // dhcp_count = nvram_get_int("dhcp_num");
238 strcpy(tmp,"dhcp");
239 strcat(tmp,bridge);
240 strcat(tmp, "_start");
241 strcpy(tmp2,"dhcp");
242 strcat(tmp2,bridge);
243 strcat(tmp2, "_num");
244 strcpy(tmp3,"lan");
245 strcat(tmp3,bridge);
246 strcat(tmp3, "_netmask");
247 dhcp_start = nvram_get_int(tmp);
248 dhcp_count = nvram_get_int(tmp2);
249 fprintf(f, "dhcp-range=%s%d,%s%d,%s,%dm\n",
250 // lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
251 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get(tmp3), dhcp_lease);
254 strcpy(tmp,"lan");
255 strcat(tmp,bridge);
256 strcat(tmp, "_ipaddr");
257 nv = nvram_safe_get(tmp);
258 // nv = router_ip;
259 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
260 p = nvram_safe_get("lan_gateway");
261 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
264 n = nvram_get_int("dhcpd_lmax");
265 fprintf(f,
266 "dhcp-option=3,%s\n" // gateway
267 "dhcp-lease-max=%d\n",
269 (n > 0) ? n : 255);
271 if (nvram_get_int("dhcpd_auth") >= 0) {
272 fprintf(f, "dhcp-authoritative\n");
275 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
276 fprintf(f, "dhcp-option=44,%s\n", nv);
278 #ifdef TCONFIG_SAMBASRV
279 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
280 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
281 // Samba will serve as a WINS server
282 fprintf(f, "dhcp-option=44,0.0.0.0\n");
285 #endif
286 // }
287 } else {
288 strcpy(tmp,"lan");
289 strcat(tmp,bridge);
290 strcat(tmp, "_ifname");
291 // fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
292 if (strcmp(nvram_safe_get(tmp),"")!=0)
293 fprintf(f, "no-dhcp-interface=%s\n", nvram_safe_get(tmp));
296 // write static lease entries & create hosts file
298 if ((hf = fopen(dmhosts, "w")) != NULL) {
299 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
300 fprintf(hf, "%s %s\n", router_ip, nv);
301 #ifdef TCONFIG_SAMBASRV
302 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
303 fprintf(hf, "%s %s\n", router_ip, nv);
304 #endif
305 p = (char *)get_wanip();
306 if ((*p == 0) || strcmp(p, "0.0.0.0") == 0)
307 p = "127.0.0.1";
308 fprintf(hf, "%s wan-ip\n", p);
309 if (nv && (*nv))
310 fprintf(hf, "%s %s-wan\n", p, nv);
313 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
314 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
315 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
316 p = nvram_safe_get("dhcpd_static");
317 while ((e = strchr(p, '>')) != NULL) {
318 n = (e - p);
319 if (n > 105) {
320 p = e + 1;
321 continue;
324 strncpy(buf, p, n);
325 buf[n] = 0;
326 p = e + 1;
328 if ((e = strchr(buf, '<')) == NULL) continue;
329 *e = 0;
330 mac = buf;
332 ip = e + 1;
333 if ((e = strchr(ip, '<')) == NULL) continue;
334 *e = 0;
335 if (strchr(ip, '.') == NULL) {
336 ipn = atoi(ip);
337 if ((ipn <= 0) || (ipn > 255)) continue;
338 sprintf(ipbuf, "%s%d", lan, ipn);
339 ip = ipbuf;
341 else {
342 if (inet_addr(ip) == INADDR_NONE) continue;
345 name = e + 1;
347 if ((hf) && (*name != 0)) {
348 fprintf(hf, "%s %s\n", ip, name);
351 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
352 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
356 if (hf) fclose(hf);
360 #ifdef TCONFIG_OPENVPN
361 write_vpn_dnsmasq_config(f);
362 #endif
364 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
366 fappend(f, "/etc/dnsmasq.custom");
370 fclose(f);
372 if (do_dns) {
373 unlink("/etc/resolv.conf");
374 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
377 TRACE_PT("run dnsmasq\n");
379 // Default to some values we like, but allow the user to override them.
380 eval("dnsmasq", "-c", "1500", "--log-async");
382 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
383 pid_dnsmasq = -2;
386 TRACE_PT("end\n");
389 void stop_dnsmasq(void)
391 TRACE_PT("begin\n");
393 if (getpid() != 1) {
394 stop_service("dnsmasq");
395 return;
398 pid_dnsmasq = -1;
400 unlink("/etc/resolv.conf");
401 symlink(dmresolv, "/etc/resolv.conf");
403 killall_tk("dnsmasq");
405 TRACE_PT("end\n");
408 void clear_resolv(void)
410 f_write(dmresolv, NULL, 0, 0, 0); // blank
413 #ifdef TCONFIG_IPV6
414 static int write_ipv6_dns_servers(FILE *f, const char *prefix, char *dns)
416 char p[INET6_ADDRSTRLEN + 1], *next = NULL;
417 struct in6_addr addr;
418 int cnt = 0;
420 foreach(p, dns, next) {
421 // verify that this is a valid IPv6 address
422 if (inet_pton(AF_INET6, p, &addr) == 1) {
423 fprintf(f, "%s%s\n", prefix, p);
424 ++cnt;
428 return cnt;
430 #endif
432 void dns_to_resolv(void)
434 FILE *f;
435 const dns_list_t *dns;
436 int i;
437 mode_t m;
439 m = umask(022); // 077 from pppoecd
440 if ((f = fopen(dmresolv, "w")) != NULL) {
441 // Check for VPN DNS entries
442 if (!write_vpn_resolv(f)) {
443 #ifdef TCONFIG_IPV6
444 if (write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_dns")) == 0 || nvram_get_int("dns_addget"))
445 write_ipv6_dns_servers(f, "nameserver ", nvram_safe_get("ipv6_get_dns"));
446 #endif
447 dns = get_dns(); // static buffer
448 if (dns->count == 0) {
449 // Put a pseudo DNS IP to trigger Connect On Demand
450 if (nvram_match("ppp_demand", "1")) {
451 switch (get_wan_proto()) {
452 case WP_PPPOE:
453 case WP_PPTP:
454 case WP_L2TP:
455 fprintf(f, "nameserver 1.1.1.1\n");
456 break;
460 else {
461 for (i = 0; i < dns->count; i++) {
462 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
463 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
468 fclose(f);
470 umask(m);
473 // -----------------------------------------------------------------------------
475 void start_httpd(void)
477 stop_httpd();
478 chdir("/www");
479 xstart("httpd");
480 chdir("/");
483 void stop_httpd(void)
485 killall_tk("httpd");
488 // -----------------------------------------------------------------------------
489 #ifdef TCONFIG_IPV6
491 void start_ipv6_sit_tunnel(void)
493 char *tun_dev = nvram_safe_get("ipv6_ifname");
494 char ip[INET6_ADDRSTRLEN + 4];
495 const char *wanip;
497 if (get_ipv6_service() == IPV6_6IN4) {
498 modprobe("sit");
499 snprintf(ip, sizeof(ip), "%s/%d",
500 nvram_safe_get("ipv6_tun_addr"),
501 nvram_get_int("ipv6_tun_addrlen") ? : 64);
502 wanip = get_wanip();
504 eval("ip", "tunnel", "add", tun_dev, "mode", "sit",
505 "remote", nvram_safe_get("ipv6_tun_v4end"),
506 "local", (char *)wanip,
507 "ttl", nvram_safe_get("ipv6_tun_ttl"));
508 if (nvram_get_int("ipv6_tun_mtu") > 0)
509 eval("ip", "link", "set", tun_dev, "mtu", nvram_safe_get("ipv6_tun_mtu"), "up");
510 else
511 eval("ip", "link", "set", tun_dev, "up");
512 eval("ip", "addr", "add", ip, "dev", tun_dev);
513 eval("ip", "route", "add", "::/0", "dev", tun_dev);
517 void stop_ipv6_sit_tunnel(void)
519 char *tun_dev = nvram_safe_get("ipv6_ifname");
520 eval("ip", "tunnel", "del", tun_dev);
521 modprobe_r("sit");
524 static pid_t pid_radvd = -1;
526 void start_radvd(void)
528 FILE *f;
529 char *prefix, *ip;
530 int do_dns;
531 char *argv[] = { "radvd", NULL, NULL, NULL };
532 int pid, argc;
534 if (getpid() != 1) {
535 start_service("radvd");
536 return;
539 stop_radvd();
541 if (ipv6_enabled() && nvram_match("ipv6_radvd", "1")) {
543 switch (get_ipv6_service()) {
544 case IPV6_NATIVE_DHCP:
545 prefix = "::";
546 break;
547 default:
548 prefix = nvram_safe_get("ipv6_prefix");
549 break;
551 if (!(*prefix)) prefix = "::";
553 // Create radvd.conf
554 if ((f = fopen("/etc/radvd.conf", "w")) == NULL) return;
556 ip = (char *)ipv6_router_address(NULL);
557 do_dns = (*ip) && nvram_match("dhcpd_dmdns", "1");
559 fprintf(f,
560 "interface %s\n"
561 "{\n"
562 " AdvSendAdvert on;\n"
563 " MaxRtrAdvInterval 60;\n"
564 " AdvHomeAgentFlag off;\n"
565 " AdvManagedFlag off;\n"
566 " prefix %s/64 \n"
567 " {\n"
568 " AdvOnLink on;\n"
569 " AdvAutonomous on;\n"
570 " };\n"
571 " %s%s%s\n"
572 "};\n",
573 nvram_safe_get("lan_ifname"), prefix,
574 do_dns ? "RDNSS " : "", do_dns ? ip : "", do_dns ? " { };" : "");
575 fclose(f);
577 // Start radvd
578 argc = 1;
579 if (nvram_get_int("debug_ipv6")) {
580 argv[argc++] = "-d";
581 argv[argc++] = "10";
583 argv[argc] = NULL;
584 _eval(argv, NULL, 0, &pid);
586 if (!nvram_contains_word("debug_norestart", "radvd")) {
587 pid_radvd = -2;
592 void stop_radvd(void)
594 if (getpid() != 1) {
595 stop_service("radvd");
596 return;
599 pid_radvd = -1;
600 killall_tk("radvd");
603 void start_ipv6(void)
605 char *p;
606 char ip[INET6_ADDRSTRLEN + 4];
607 int service;
609 service = get_ipv6_service();
610 enable_ip_forward();
612 // Check if turned on
613 switch (service) {
614 case IPV6_NATIVE:
615 case IPV6_6IN4:
616 case IPV6_MANUAL:
617 p = (char *)ipv6_router_address(NULL);
618 if (*p) {
619 snprintf(ip, sizeof(ip), "%s/%d", p, nvram_get_int("ipv6_prefix_length") ? : 64);
620 eval("ip", "-6", "addr", "add", ip, "dev", nvram_safe_get("lan_ifname"));
622 break;
625 if (service != IPV6_DISABLED) {
626 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0)
627 accept_ra(nvram_safe_get("lan_ifname"));
631 void stop_ipv6(void)
633 stop_ipv6_sit_tunnel();
634 stop_dhcp6c();
635 eval("ip", "-6", "addr", "flush", "scope", "global");
638 #endif
640 // -----------------------------------------------------------------------------
642 void start_upnp(void)
644 if (getpid() != 1) {
645 start_service("upnp");
646 return;
649 if (get_wan_proto() == WP_DISABLED) return;
651 int enable;
652 FILE *f;
653 int upnp_port;
655 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
656 mkdir("/etc/upnp", 0777);
657 if (f_exists("/etc/upnp/config.alt")) {
658 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
660 else {
661 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
662 upnp_port = nvram_get_int("upnp_port");
663 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
665 // char *lanip = nvram_safe_get("lan_ipaddr");
666 // char *lanmask = nvram_safe_get("lan_netmask");
668 fprintf(f,
669 "ext_ifname=%s\n"
670 // "listening_ip=%s/%s\n"
671 "port=%d\n"
672 "enable_upnp=%s\n"
673 "enable_natpmp=%s\n"
674 "secure_mode=%s\n"
675 "upnp_forward_chain=upnp\n"
676 "upnp_nat_chain=upnp\n"
677 "notify_interval=%d\n"
678 "system_uptime=yes\n"
679 "\n"
681 get_wanface(),
682 // lanip, lanmask,
683 upnp_port,
684 (enable & 1) ? "yes" : "no", // upnp enable
685 (enable & 2) ? "yes" : "no", // natpmp enable
686 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
687 nvram_get_int("upnp_ssdp_interval")
690 if (nvram_get_int("upnp_clean")) {
691 int interval = nvram_get_int("upnp_clean_interval");
692 if (interval < 60) interval = 60;
693 fprintf(f,
694 "clean_ruleset_interval=%d\n"
695 "clean_ruleset_threshold=%d\n",
696 interval,
697 nvram_get_int("upnp_clean_threshold")
700 else
701 fprintf(f,"clean_ruleset_interval=0\n");
703 if (nvram_match("upnp_mnp", "1")) {
704 int https = nvram_get_int("https_enable");
705 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
706 // https ? "s" : "", lanip,
707 https ? "s" : "", nvram_safe_get("lan_ipaddr"),
708 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
710 else {
711 // Empty parameters are not included into XML service description
712 fprintf(f, "presentation_url=\n");
715 char uuid[45];
716 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
717 fprintf(f, "uuid=%s\n", uuid);
719 char tmp[32];
720 char tmp2[32];
721 char br;
723 for(br=0 ; br<4 ; br++) {
724 char bridge[2] = "0";
725 if (br!=0)
726 bridge[0]+=br;
727 else
728 strcpy(bridge, "");
730 strcpy(tmp,"lan");
731 strcat(tmp,bridge);
732 strcat(tmp, "_ipaddr");
733 strcpy(tmp2,"lan");
734 strcat(tmp2,bridge);
735 strcat(tmp2, "_netmask");
737 char *lanip = nvram_safe_get(tmp);
738 char *lanmask = nvram_safe_get(tmp2);
740 if(strcmp(lanip,"")!=0) {
741 fprintf(f,
742 "listening_ip=%s/%s\n",
743 lanip, lanmask);
745 int ports[4];
746 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
747 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
748 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
749 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
750 fprintf(f,
751 "allow %d-%d %s/%s %d-%d\n",
752 ports[0], ports[1],
753 lanip, lanmask,
754 ports[2], ports[3]
757 else {
758 // by default allow only redirection of ports above 1024
759 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
764 fappend(f, "/etc/upnp/config.custom");
765 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
766 fclose(f);
768 xstart("miniupnpd", "-f", "/etc/upnp/config");
774 void stop_upnp(void)
776 if (getpid() != 1) {
777 stop_service("upnp");
778 return;
781 killall_tk("miniupnpd");
784 // -----------------------------------------------------------------------------
786 static pid_t pid_crond = -1;
788 void start_cron(void)
790 stop_cron();
792 eval("crond", nvram_contains_word("log_events", "crond") ? NULL : "-l", "9");
793 if (!nvram_contains_word("debug_norestart", "crond")) {
794 pid_crond = -2;
798 void stop_cron(void)
800 pid_crond = -1;
801 killall_tk("crond");
804 // -----------------------------------------------------------------------------
805 #ifdef LINUX26
807 static pid_t pid_hotplug2 = -1;
809 void start_hotplug2()
811 stop_hotplug2();
813 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
814 xstart("hotplug2", "--persistent", "--no-coldplug");
815 // FIXME: Don't remember exactly why I put "sleep" here -
816 // but it was not for a race with check_services()... - TB
817 sleep(1);
819 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
820 pid_hotplug2 = -2;
824 void stop_hotplug2(void)
826 pid_hotplug2 = -1;
827 killall_tk("hotplug2");
830 #endif /* LINUX26 */
831 // -----------------------------------------------------------------------------
833 // Written by Sparq in 2002/07/16
834 void start_zebra(void)
836 #ifdef TCONFIG_ZEBRA
837 if (getpid() != 1) {
838 start_service("zebra");
839 return;
842 FILE *fp;
844 char *lan_tx = nvram_safe_get("dr_lan_tx");
845 char *lan_rx = nvram_safe_get("dr_lan_rx");
846 char *lan1_tx = nvram_safe_get("dr_lan1_tx");
847 char *lan1_rx = nvram_safe_get("dr_lan1_rx");
848 char *lan2_tx = nvram_safe_get("dr_lan2_tx");
849 char *lan2_rx = nvram_safe_get("dr_lan2_rx");
850 char *lan3_tx = nvram_safe_get("dr_lan3_tx");
851 char *lan3_rx = nvram_safe_get("dr_lan3_rx");
852 char *wan_tx = nvram_safe_get("dr_wan_tx");
853 char *wan_rx = nvram_safe_get("dr_wan_rx");
855 // if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
856 if ((*lan_tx == '0') && (*lan_rx == '0') &&
857 (*lan1_tx == '0') && (*lan1_rx == '0') &&
858 (*lan2_tx == '0') && (*lan2_rx == '0') &&
859 (*lan3_tx == '0') && (*lan3_rx == '0') &&
860 (*wan_tx == '0') && (*wan_rx == '0')) {
861 return;
864 // empty
865 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
866 fclose(fp);
870 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
871 char *lan_ifname = nvram_safe_get("lan_ifname");
872 char *lan1_ifname = nvram_safe_get("lan1_ifname");
873 char *lan2_ifname = nvram_safe_get("lan2_ifname");
874 char *lan3_ifname = nvram_safe_get("lan3_ifname");
875 char *wan_ifname = nvram_safe_get("wan_ifname");
877 fprintf(fp, "router rip\n");
878 if(strcmp(lan_ifname,"")!=0)
879 fprintf(fp, "network %s\n", lan_ifname);
880 if(strcmp(lan1_ifname,"")!=0)
881 fprintf(fp, "network %s\n", lan1_ifname);
882 if(strcmp(lan2_ifname,"")!=0)
883 fprintf(fp, "network %s\n", lan2_ifname);
884 if(strcmp(lan3_ifname,"")!=0)
885 fprintf(fp, "network %s\n", lan3_ifname);
886 fprintf(fp, "network %s\n", wan_ifname);
887 fprintf(fp, "redistribute connected\n");
888 //fprintf(fp, "redistribute static\n");
890 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
891 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
893 if(strcmp(lan_ifname,"")!=0) {
894 fprintf(fp, "interface %s\n", lan_ifname);
895 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
896 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
898 if(strcmp(lan1_ifname,"")!=0) {
899 fprintf(fp, "interface %s\n", lan1_ifname);
900 if (*lan1_tx != '0') fprintf(fp, "ip rip send version %s\n", lan1_tx);
901 if (*lan1_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan1_rx);
903 if(strcmp(lan2_ifname,"")!=0) {
904 fprintf(fp, "interface %s\n", lan2_ifname);
905 if (*lan2_tx != '0') fprintf(fp, "ip rip send version %s\n", lan2_tx);
906 if (*lan2_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan2_rx);
908 if(strcmp(lan3_ifname,"")!=0) {
909 fprintf(fp, "interface %s\n", lan3_ifname);
910 if (*lan3_tx != '0') fprintf(fp, "ip rip send version %s\n", lan3_tx);
911 if (*lan3_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan3_rx);
913 fprintf(fp, "interface %s\n", wan_ifname);
914 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
915 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
917 fprintf(fp, "router rip\n");
918 if(strcmp(lan_ifname,"")!=0) {
919 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
920 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
922 if(strcmp(lan1_ifname,"")!=0) {
923 if (*lan1_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan1_ifname);
924 if (*lan1_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan1_ifname);
926 if(strcmp(lan2_ifname,"")!=0) {
927 if (*lan2_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan2_ifname);
928 if (*lan2_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan2_ifname);
930 if(strcmp(lan3_ifname,"")!=0) {
931 if (*lan3_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan3_ifname);
932 if (*lan3_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan3_ifname);
934 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
935 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
936 fprintf(fp, "access-list private deny any\n");
938 //fprintf(fp, "debug rip events\n");
939 //fprintf(fp, "log file /etc/ripd.log\n");
940 fclose(fp);
943 xstart("zebra", "-d");
944 xstart("ripd", "-d");
945 #endif
948 void stop_zebra(void)
950 #ifdef TCONFIG_ZEBRA
951 if (getpid() != 1) {
952 stop_service("zebra");
953 return;
956 killall("zebra", SIGTERM);
957 killall("ripd", SIGTERM);
959 unlink("/etc/zebra.conf");
960 unlink("/etc/ripd.conf");
961 #endif
964 // -----------------------------------------------------------------------------
966 void start_syslog(void)
968 char *argv[16];
969 int argc;
970 char *nv;
971 char *b_opt = "";
972 char rem[256];
973 int n;
974 char s[64];
975 char cfg[256];
976 char *rot_siz = "50";
978 argv[0] = "syslogd";
979 argc = 1;
981 if (nvram_match("log_remote", "1")) {
982 nv = nvram_safe_get("log_remoteip");
983 if (*nv) {
984 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
985 argv[argc++] = "-R";
986 argv[argc++] = rem;
990 if (nvram_match("log_file", "1")) {
991 argv[argc++] = "-L";
993 /* Read options: rotate_size(kb) num_backups logfilename.
994 * Ignore these settings and use defaults if the logfile cannot be written to.
996 if (f_read_string("/etc/syslogd.cfg", cfg, sizeof(cfg)) > 0) {
997 if ((nv = strchr(cfg, '\n')))
998 *nv = 0;
1000 if ((nv = strtok(cfg, " \t"))) {
1001 if (isdigit(*nv))
1002 rot_siz = nv;
1005 if ((nv = strtok(NULL, " \t")))
1006 b_opt = nv;
1008 if ((nv = strtok(NULL, " \t")) && *nv == '/') {
1009 if (f_write(nv, cfg, 0, FW_APPEND, 0) >= 0) {
1010 argv[argc++] = "-O";
1011 argv[argc++] = nv;
1013 else {
1014 rot_siz = "50";
1015 b_opt = "";
1020 argv[argc++] = "-s";
1021 argv[argc++] = rot_siz;
1023 if (isdigit(*b_opt)) {
1024 argv[argc++] = "-b";
1025 argv[argc++] = b_opt;
1029 if (argc > 1) {
1030 argv[argc] = NULL;
1031 _eval(argv, NULL, 0, NULL);
1033 argv[0] = "klogd";
1034 argv[1] = NULL;
1035 _eval(argv, NULL, 0, NULL);
1037 // used to be available in syslogd -m
1038 n = nvram_get_int("log_mark");
1039 if (n > 0) {
1040 // n is in minutes
1041 if (n < 60)
1042 sprintf(rem, "*/%d * * * *", n);
1043 else if (n < 60 * 24)
1044 sprintf(rem, "0 */%d * * *", n / 60);
1045 else
1046 sprintf(rem, "0 0 */%d * *", n / (60 * 24));
1047 sprintf(s, "cru a syslogdmark \"%s logger -p syslog.info -- -- MARK --\"", rem);
1048 system(s);
1050 else {
1051 system("cru d syslogdmark");
1056 void stop_syslog(void)
1058 killall("klogd", SIGTERM);
1059 killall("syslogd", SIGTERM);
1062 // -----------------------------------------------------------------------------
1064 static pid_t pid_igmp = -1;
1066 void start_igmp_proxy(void)
1068 FILE *fp;
1070 pid_igmp = -1;
1071 if (nvram_match("multicast_pass", "1")) {
1072 if (get_wan_proto() == WP_DISABLED)
1073 return;
1075 if (f_exists("/etc/igmp.alt")) {
1076 eval("igmpproxy", "/etc/igmp.alt");
1078 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
1079 fprintf(fp,
1080 "quickleave\n"
1081 "phyint %s upstream\n"
1082 "\taltnet %s\n"
1083 "phyint %s downstream ratelimit 0\n",
1084 get_wanface(),
1085 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
1086 nvram_safe_get("lan_ifname"));
1087 fclose(fp);
1088 eval("igmpproxy", "/etc/igmp.conf");
1090 else {
1091 return;
1093 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1094 pid_igmp = -2;
1099 void stop_igmp_proxy(void)
1101 pid_igmp = -1;
1102 killall_tk("igmpproxy");
1106 // -----------------------------------------------------------------------------
1108 void set_tz(void)
1110 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
1113 void start_ntpc(void)
1115 set_tz();
1117 stop_ntpc();
1119 if (nvram_get_int("ntp_updates") >= 0) {
1120 xstart("ntpsync", "--init");
1124 void stop_ntpc(void)
1126 killall("ntpsync", SIGTERM);
1129 // -----------------------------------------------------------------------------
1131 static void stop_rstats(void)
1133 int n;
1134 int pid;
1136 n = 60;
1137 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
1138 if (kill(pid, SIGTERM) != 0) break;
1139 sleep(1);
1143 static void start_rstats(int new)
1145 if (nvram_match("rstats_enable", "1")) {
1146 stop_rstats();
1147 if (new) xstart("rstats", "--new");
1148 else xstart("rstats");
1152 // -----------------------------------------------------------------------------
1154 // !!TB - FTP Server
1156 #ifdef TCONFIG_FTP
1157 static char *get_full_storage_path(char *val)
1159 static char buf[128];
1160 int len;
1162 if (val[0] == '/')
1163 len = sprintf(buf, "%s", val);
1164 else
1165 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
1167 if (len > 1 && buf[len - 1] == '/')
1168 buf[len - 1] = 0;
1170 return buf;
1173 static char *nvram_storage_path(char *var)
1175 char *val = nvram_safe_get(var);
1176 return get_full_storage_path(val);
1179 char vsftpd_conf[] = "/etc/vsftpd.conf";
1180 char vsftpd_users[] = "/etc/vsftpd.users";
1181 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
1183 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1185 static void start_ftpd(void)
1187 char tmp[256];
1188 FILE *fp, *f;
1189 char *buf;
1190 char *p, *q;
1191 char *user, *pass, *rights;
1193 if (getpid() != 1) {
1194 start_service("ftpd");
1195 return;
1198 if (!nvram_get_int("ftp_enable")) return;
1200 mkdir_if_none(vsftpd_users);
1201 mkdir_if_none("/var/run/vsftpd");
1203 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
1204 return;
1206 if (nvram_get_int("ftp_super"))
1208 /* rights */
1209 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
1210 if ((f = fopen(tmp, "w")))
1212 fprintf(f,
1213 "dirlist_enable=yes\n"
1214 "write_enable=yes\n"
1215 "download_enable=yes\n");
1216 fclose(f);
1220 #ifdef TCONFIG_SAMBASRV
1221 if (nvram_match("smbd_cset", "utf8"))
1222 fprintf(fp, "utf8=yes\n");
1223 #endif
1225 if (nvram_invmatch("ftp_anonymous", "0"))
1227 fprintf(fp,
1228 "anon_allow_writable_root=yes\n"
1229 "anon_world_readable_only=no\n"
1230 "anon_umask=022\n");
1232 /* rights */
1233 sprintf(tmp, "%s/ftp", vsftpd_users);
1234 if ((f = fopen(tmp, "w")))
1236 if (nvram_match("ftp_dirlist", "0"))
1237 fprintf(f, "dirlist_enable=yes\n");
1238 if (nvram_match("ftp_anonymous", "1") ||
1239 nvram_match("ftp_anonymous", "3"))
1240 fprintf(f, "write_enable=yes\n");
1241 if (nvram_match("ftp_anonymous", "1") ||
1242 nvram_match("ftp_anonymous", "2"))
1243 fprintf(f, "download_enable=yes\n");
1244 fclose(f);
1246 if (nvram_match("ftp_anonymous", "1") ||
1247 nvram_match("ftp_anonymous", "3"))
1248 fprintf(fp,
1249 "anon_upload_enable=yes\n"
1250 "anon_mkdir_write_enable=yes\n"
1251 "anon_other_write_enable=yes\n");
1252 } else {
1253 fprintf(fp, "anonymous_enable=no\n");
1256 fprintf(fp,
1257 "dirmessage_enable=yes\n"
1258 "download_enable=no\n"
1259 "dirlist_enable=no\n"
1260 "hide_ids=yes\n"
1261 "syslog_enable=yes\n"
1262 "local_enable=yes\n"
1263 "local_umask=022\n"
1264 "chmod_enable=no\n"
1265 "chroot_local_user=yes\n"
1266 "check_shell=no\n"
1267 "log_ftp_protocol=%s\n"
1268 "user_config_dir=%s\n"
1269 "passwd_file=%s\n"
1270 "listen%s=yes\n"
1271 "listen_port=%s\n"
1272 "background=yes\n"
1273 "isolate=no\n"
1274 "max_clients=%d\n"
1275 "max_per_ip=%d\n"
1276 "max_login_fails=1\n"
1277 "idle_session_timeout=%s\n"
1278 "use_sendfile=no\n"
1279 "anon_max_rate=%d\n"
1280 "local_max_rate=%d\n"
1281 "%s\n",
1282 nvram_get_int("log_ftp") ? "yes" : "no",
1283 vsftpd_users, vsftpd_passwd,
1284 #ifdef TCONFIG_IPV6
1285 ipv6_enabled() ? "_ipv6" : "",
1286 #else
1288 #endif
1289 nvram_get("ftp_port") ? : "21",
1290 nvram_get_int("ftp_max"),
1291 nvram_get_int("ftp_ipmax"),
1292 nvram_get("ftp_staytimeout") ? : "300",
1293 nvram_get_int("ftp_anonrate") * 1024,
1294 nvram_get_int("ftp_rate") * 1024,
1295 nvram_safe_get("ftp_custom"));
1297 fclose(fp);
1299 /* prepare passwd file and default users */
1300 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
1301 return;
1303 if (((user = nvram_get("http_username")) == NULL) || (*user == 0)) user = "admin";
1304 if (((pass = nvram_get("http_passwd")) == NULL) || (*pass == 0)) pass = "admin";
1306 fprintf(fp, /* anonymous, admin, nobody */
1307 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1308 "%s:%s:0:0:root:/:/sbin/nologin\n"
1309 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1310 nvram_storage_path("ftp_anonroot"), user,
1311 nvram_get_int("ftp_super") ? crypt(pass, "$1$") : "x",
1312 MOUNT_ROOT);
1314 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
1317 username<password<rights
1318 rights:
1319 Read/Write
1320 Read Only
1321 View Only
1322 Private
1324 p = buf;
1325 while ((q = strsep(&p, ">")) != NULL) {
1326 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
1327 if (!user || !pass) continue;
1329 /* directory */
1330 if (strncmp(rights, "Private", 7) == 0)
1332 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
1333 mkdir_if_none(tmp);
1335 else
1336 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
1338 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1339 user, crypt(pass, "$1$"), user, tmp);
1341 /* rights */
1342 sprintf(tmp, "%s/%s", vsftpd_users, user);
1343 if ((f = fopen(tmp, "w")))
1345 tmp[0] = 0;
1346 if (nvram_invmatch("ftp_dirlist", "1"))
1347 strcat(tmp, "dirlist_enable=yes\n");
1348 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
1349 strcat(tmp, "download_enable=yes\n");
1350 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
1351 strcat(tmp, "write_enable=yes\n");
1353 fputs(tmp, f);
1354 fclose(f);
1357 free(buf);
1360 fclose(fp);
1361 killall("vsftpd", SIGHUP);
1363 /* start vsftpd if it's not already running */
1364 if (pidof("vsftpd") <= 0)
1365 xstart("vsftpd");
1368 static void stop_ftpd(void)
1370 if (getpid() != 1) {
1371 stop_service("ftpd");
1372 return;
1375 killall_tk("vsftpd");
1376 unlink(vsftpd_passwd);
1377 unlink(vsftpd_conf);
1378 eval("rm", "-rf", vsftpd_users);
1380 #endif // TCONFIG_FTP
1382 // -----------------------------------------------------------------------------
1384 // !!TB - Samba
1386 #ifdef TCONFIG_SAMBASRV
1387 static void kill_samba(int sig)
1389 if (sig == SIGTERM) {
1390 killall_tk("smbd");
1391 killall_tk("nmbd");
1393 else {
1394 killall("smbd", sig);
1395 killall("nmbd", sig);
1399 static void start_samba(void)
1401 FILE *fp;
1402 DIR *dir = NULL;
1403 struct dirent *dp;
1404 char nlsmod[15];
1405 int mode;
1406 char *nv;
1408 if (getpid() != 1) {
1409 start_service("smbd");
1410 return;
1413 mode = nvram_get_int("smbd_enable");
1414 if (!mode || !nvram_invmatch("lan_hostname", ""))
1415 return;
1417 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1418 return;
1420 fprintf(fp, "[global]\n"
1421 " interfaces = %s\n"
1422 " bind interfaces only = yes\n"
1423 " workgroup = %s\n"
1424 " netbios name = %s\n"
1425 " server string = %s\n"
1426 " guest account = nobody\n"
1427 " security = user\n"
1428 " %s\n"
1429 " guest ok = %s\n"
1430 " guest only = no\n"
1431 " browseable = yes\n"
1432 " syslog only = yes\n"
1433 " timestamp logs = no\n"
1434 " syslog = 1\n"
1435 " encrypt passwords = yes\n"
1436 " preserve case = yes\n"
1437 " short preserve case = yes\n",
1438 nvram_safe_get("lan_ifname"),
1439 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1440 nvram_safe_get("lan_hostname"),
1441 nvram_get("router_name") ? : "Tomato",
1442 mode == 2 ? "" : "map to guest = Bad User",
1443 mode == 2 ? "no" : "yes" // guest ok
1446 if (nvram_get_int("smbd_wins")) {
1447 nv = nvram_safe_get("wan_wins");
1448 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1449 fprintf(fp, " wins support = yes\n");
1453 if (nvram_get_int("smbd_master")) {
1454 fprintf(fp,
1455 " domain master = yes\n"
1456 " local master = yes\n"
1457 " preferred master = yes\n"
1458 " os level = 65\n");
1461 nv = nvram_safe_get("smbd_cpage");
1462 if (*nv) {
1463 #ifndef TCONFIG_SAMBA3
1464 fprintf(fp, " client code page = %s\n", nv);
1465 #endif
1466 sprintf(nlsmod, "nls_cp%s", nv);
1468 nv = nvram_safe_get("smbd_nlsmod");
1469 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1470 modprobe_r(nv);
1472 modprobe(nlsmod);
1473 nvram_set("smbd_nlsmod", nlsmod);
1476 #ifndef TCONFIG_SAMBA3
1477 if (nvram_match("smbd_cset", "utf8"))
1478 fprintf(fp, " coding system = utf8\n");
1479 else if (nvram_invmatch("smbd_cset", ""))
1480 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1481 #endif
1483 nv = nvram_safe_get("smbd_custom");
1484 /* add socket options unless overriden by the user */
1485 if (strstr(nv, "socket options") == NULL) {
1486 fprintf(fp, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1488 fprintf(fp, "%s\n\n", nv);
1490 /* configure shares */
1492 char *buf;
1493 char *p, *q;
1494 char *name, *path, *comment, *writeable, *hidden;
1495 int cnt = 0;
1497 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1499 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1501 p = buf;
1502 while ((q = strsep(&p, ">")) != NULL) {
1503 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1504 if (!path || !name) continue;
1506 /* share name */
1507 fprintf(fp, "\n[%s]\n", name);
1509 /* path */
1510 fprintf(fp, " path = %s\n", path);
1512 /* access level */
1513 if (!strcmp(writeable, "1"))
1514 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1515 if (!strcmp(hidden, "1"))
1516 fprintf(fp, " browseable = no\n");
1518 /* comment */
1519 if (comment)
1520 fprintf(fp, " comment = %s\n", comment);
1522 cnt++;
1524 free(buf);
1527 /* Share every mountpoint below MOUNT_ROOT */
1528 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1529 while ((dp = readdir(dir))) {
1530 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1532 char path[256];
1533 struct stat sb;
1534 int thisdev;
1536 /* Only if is a directory and is mounted */
1537 sprintf(path, "%s/%s", MOUNT_ROOT, dp->d_name);
1538 sb.st_mode = S_IFDIR; /* failsafe */
1539 stat(path, &sb);
1540 if (!S_ISDIR(sb.st_mode))
1541 continue;
1543 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
1544 strcat(path, "/.");
1545 stat(path, &sb);
1546 thisdev = sb.st_dev;
1547 strcat(path, ".");
1548 ++sb.st_dev; /* failsafe */
1549 stat(path, &sb);
1550 if (thisdev == sb.st_dev)
1551 continue;
1553 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1554 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1555 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1556 if (nvram_match("smbd_autoshare", "3")) // Hidden
1557 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1558 dp->d_name, MOUNT_ROOT, dp->d_name);
1559 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1560 fprintf(fp, " writable = yes\n delete readonly = yes\n force user = root\n");
1562 cnt++;
1566 if (dir) closedir(dir);
1568 if (cnt == 0) {
1569 /* by default share MOUNT_ROOT as read-only */
1570 fprintf(fp, "\n[share]\n"
1571 " path = %s\n"
1572 " writable = no\n",
1573 MOUNT_ROOT);
1576 fclose(fp);
1578 mkdir_if_none("/var/run/samba");
1579 mkdir_if_none("/etc/samba");
1581 /* write smbpasswd */
1582 #ifdef TCONFIG_SAMBA3
1583 eval("smbpasswd", "nobody", "\"\"");
1584 #else
1585 eval("smbpasswd", "-a", "nobody", "\"\"");
1586 #endif
1587 if (mode == 2) {
1588 char *smbd_user;
1589 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1590 smbd_user = "nas";
1591 #ifdef TCONFIG_SAMBA3
1592 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1593 #else
1594 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1595 #endif
1598 kill_samba(SIGHUP);
1599 int ret1 = 0, ret2 = 0;
1600 /* start samba if it's not already running */
1601 if (pidof("nmbd") <= 0)
1602 ret1 = xstart("nmbd", "-D");
1603 if (pidof("smbd") <= 0)
1604 ret2 = xstart("smbd", "-D");
1606 if (ret1 || ret2) kill_samba(SIGTERM);
1609 static void stop_samba(void)
1611 if (getpid() != 1) {
1612 stop_service("smbd");
1613 return;
1616 kill_samba(SIGTERM);
1617 /* clean up */
1618 unlink("/var/log/smb");
1619 unlink("/var/log/nmb");
1620 eval("rm", "-rf", "/var/run/samba");
1622 #endif // TCONFIG_SAMBASRV
1624 #ifdef TCONFIG_MEDIA_SERVER
1625 #define MEDIA_SERVER_APP "minidlna"
1627 static void start_media_server(void)
1629 FILE *f;
1630 int port, pid, https;
1631 char *dbdir;
1632 char *argv[] = { MEDIA_SERVER_APP, "-f", "/etc/"MEDIA_SERVER_APP".conf", "-R", NULL };
1633 static int once = 1;
1635 if (getpid() != 1) {
1636 start_service("media");
1637 return;
1640 if (nvram_get_int("ms_sas") == 0)
1641 once = 0;
1643 if (nvram_get_int("ms_enable") != 0) {
1644 if ((!once) && (nvram_get_int("ms_rescan") == 0)) {
1645 // no forced rescan
1646 argv[3] = NULL;
1648 nvram_unset("ms_rescan");
1650 if (f_exists("/etc/"MEDIA_SERVER_APP".alt")) {
1651 argv[2] = "/etc/"MEDIA_SERVER_APP".alt";
1653 else {
1654 if ((f = fopen(argv[2], "w")) != NULL) {
1655 port = nvram_get_int("ms_port");
1656 https = nvram_get_int("https_enable");
1657 dbdir = nvram_safe_get("ms_dbdir");
1658 if (!(*dbdir)) dbdir = NULL;
1659 mkdir_if_none(dbdir ? : "/var/run/"MEDIA_SERVER_APP);
1661 fprintf(f,
1662 "network_interface=%s\n"
1663 "port=%d\n"
1664 "friendly_name=%s\n"
1665 "db_dir=%s/.db\n"
1666 "enable_tivo=%s\n"
1667 "strict_dlna=%s\n"
1668 "presentation_url=http%s://%s:%s/nas-media.asp\n"
1669 "inotify=yes\n"
1670 "notify_interval=600\n"
1671 "album_art_names=Cover.jpg/cover.jpg/Thumb.jpg/thumb.jpg\n"
1672 "\n",
1673 nvram_safe_get("lan_ifname"),
1674 (port < 0) || (port >= 0xffff) ? 0 : port,
1675 nvram_get("router_name") ? : "Tomato",
1676 dbdir ? : "/var/run/"MEDIA_SERVER_APP,
1677 nvram_get_int("ms_tivo") ? "yes" : "no",
1678 nvram_get_int("ms_stdlna") ? "yes" : "no",
1679 https ? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https ? "https_lanport" : "http_lanport")
1682 // media directories
1683 char *buf, *p, *q;
1684 char *path, *restrict;
1686 if ((buf = strdup(nvram_safe_get("ms_dirs"))) != NULL) {
1687 /* path<restrict[A|V|P|] */
1689 p = buf;
1690 while ((q = strsep(&p, ">")) != NULL) {
1691 if (vstrsep(q, "<", &path, &restrict) < 1 || !path || !(*path))
1692 continue;
1693 fprintf(f, "media_dir=%s%s%s\n",
1694 restrict ? : "", (restrict && *restrict) ? "," : "", path);
1696 free(buf);
1699 fclose(f);
1703 /* start media server if it's not already running */
1704 if (pidof(MEDIA_SERVER_APP) <= 0) {
1705 if ((_eval(argv, NULL, 0, &pid) == 0) && (once)) {
1706 /* If we started the media server successfully, wait 1 sec
1707 * to let it die if it can't open the database file.
1708 * If it's still alive after that, assume it's running and
1709 * disable forced once-after-reboot rescan.
1711 sleep(1);
1712 if (pidof(MEDIA_SERVER_APP) > 0)
1713 once = 0;
1719 static void stop_media_server(void)
1721 if (getpid() != 1) {
1722 stop_service("media");
1723 return;
1726 killall_tk(MEDIA_SERVER_APP);
1728 #endif // TCONFIG_MEDIA_SERVER
1730 #ifdef TCONFIG_USB
1731 static void start_nas_services(void)
1733 if (getpid() != 1) {
1734 start_service("usbapps");
1735 return;
1738 #ifdef TCONFIG_SAMBASRV
1739 start_samba();
1740 #endif
1741 #ifdef TCONFIG_FTP
1742 start_ftpd();
1743 #endif
1744 #ifdef TCONFIG_MEDIA_SERVER
1745 start_media_server();
1746 #endif
1749 static void stop_nas_services(void)
1751 if (getpid() != 1) {
1752 stop_service("usbapps");
1753 return;
1756 #ifdef TCONFIG_MEDIA_SERVER
1757 stop_media_server();
1758 #endif
1759 #ifdef TCONFIG_FTP
1760 stop_ftpd();
1761 #endif
1762 #ifdef TCONFIG_SAMBASRV
1763 stop_samba();
1764 #endif
1767 void restart_nas_services(int stop, int start)
1769 int fd = file_lock("usb");
1770 /* restart all NAS applications */
1771 if (stop)
1772 stop_nas_services();
1773 if (start)
1774 start_nas_services();
1775 file_unlock(fd);
1777 #endif // TCONFIG_USB
1779 // -----------------------------------------------------------------------------
1781 /* -1 = Don't check for this program, it is not expected to be running.
1782 * Other = This program has been started and should be kept running. If no
1783 * process with the name is running, call func to restart it.
1784 * Note: At startup, dnsmasq forks a short-lived child which forks a
1785 * long-lived (grand)child. The parents terminate.
1786 * Many daemons use this technique.
1788 static void _check(pid_t pid, const char *name, void (*func)(void))
1790 if (pid == -1) return;
1792 if (pidof(name) > 0) return;
1794 syslog(LOG_DEBUG, "%s terminated unexpectedly, restarting.\n", name);
1795 func();
1797 // Force recheck in 500 msec
1798 setitimer(ITIMER_REAL, &pop_tv, NULL);
1801 void check_services(void)
1803 TRACE_PT("keep alive\n");
1805 // Periodically reap any zombies
1806 setitimer(ITIMER_REAL, &zombie_tv, NULL);
1808 #ifdef LINUX26
1809 _check(pid_hotplug2, "hotplug2", start_hotplug2);
1810 #endif
1811 _check(pid_dnsmasq, "dnsmasq", start_dnsmasq);
1812 _check(pid_crond, "crond", start_cron);
1813 _check(pid_igmp, "igmpproxy", start_igmp_proxy);
1814 #ifdef TCONFIG_IPV6
1815 _check(pid_radvd, "radvd", start_radvd);
1816 #endif
1819 // -----------------------------------------------------------------------------
1821 void start_services(void)
1823 static int once = 1;
1825 if (once) {
1826 once = 0;
1828 if (nvram_get_int("telnetd_eas")) start_telnetd();
1829 if (nvram_get_int("sshd_eas")) start_sshd();
1832 // start_syslog();
1833 start_nas();
1834 start_zebra();
1835 start_dnsmasq();
1836 start_cifs();
1837 start_httpd();
1838 start_cron();
1839 // start_upnp();
1840 start_rstats(0);
1841 start_sched();
1842 #ifdef TCONFIG_IPV6
1843 /* note: starting radvd here might be too early in case of
1844 * DHCPv6 because we won't have received a prefix and so it
1845 * will disable advertisements, but the SIGHUP sent from
1846 * dhcp6c-state will restart them.
1848 start_radvd();
1849 #endif
1850 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
1853 void stop_services(void)
1855 clear_resolv();
1857 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
1858 #ifdef TCONFIG_IPV6
1859 stop_radvd();
1860 #endif
1861 stop_sched();
1862 stop_rstats();
1863 // stop_upnp();
1864 stop_cron();
1865 stop_httpd();
1866 stop_cifs();
1867 stop_dnsmasq();
1868 stop_zebra();
1869 stop_nas();
1870 // stop_syslog();
1873 // -----------------------------------------------------------------------------
1875 /* nvram "action_service" is: "service-action[-modifier]"
1876 * action is something like "stop" or "start" or "restart"
1877 * optional modifier is "c" for the "service" command-line command
1879 void exec_service(void)
1881 const int A_START = 1;
1882 const int A_STOP = 2;
1883 const int A_RESTART = 1|2;
1884 char buffer[128];
1885 char *service;
1886 char *act;
1887 char *next;
1888 char *modifier;
1889 int action, user;
1890 int i;
1892 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1893 next = buffer;
1895 TOP:
1896 act = strsep(&next, ",");
1897 service = strsep(&act, "-");
1898 if (act == NULL) {
1899 next = NULL;
1900 goto CLEAR;
1902 modifier = act;
1903 strsep(&modifier, "-");
1905 TRACE_PT("service=%s action=%s modifier=%s\n", service, act, modifier ? : "");
1907 if (strcmp(act, "start") == 0) action = A_START;
1908 else if (strcmp(act, "stop") == 0) action = A_STOP;
1909 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1910 else action = 0;
1911 user = (modifier != NULL && *modifier == 'c');
1913 if (strcmp(service, "dhcpc") == 0) {
1914 if (action & A_STOP) stop_dhcpc();
1915 if (action & A_START) start_dhcpc();
1916 goto CLEAR;
1919 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1920 if (action & A_STOP) stop_dnsmasq();
1921 if (action & A_START) {
1922 dns_to_resolv();
1923 start_dnsmasq();
1925 goto CLEAR;
1928 if (strcmp(service, "firewall") == 0) {
1929 if (action & A_STOP) {
1930 stop_firewall();
1931 stop_igmp_proxy();
1933 if (action & A_START) {
1934 start_firewall();
1935 start_igmp_proxy();
1937 goto CLEAR;
1940 if (strcmp(service, "restrict") == 0) {
1941 if (action & A_STOP) {
1942 stop_firewall();
1944 if (action & A_START) {
1945 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1947 start_firewall();
1949 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1950 if (i == 1) {
1951 if (nvram_get_int("rrules_radio") < 0) {
1952 eval("radio", "on");
1956 goto CLEAR;
1959 if (strcmp(service, "qos") == 0) {
1960 if (action & A_STOP) {
1961 stop_qos();
1963 stop_firewall(); start_firewall(); // always restarted
1964 if (action & A_START) {
1965 start_qos();
1966 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1968 goto CLEAR;
1971 if (strcmp(service, "upnp") == 0) {
1972 if (action & A_STOP) {
1973 stop_upnp();
1975 stop_firewall(); start_firewall(); // always restarted
1976 if (action & A_START) {
1977 start_upnp();
1979 goto CLEAR;
1982 if (strcmp(service, "telnetd") == 0) {
1983 if (action & A_STOP) stop_telnetd();
1984 if (action & A_START) start_telnetd();
1985 goto CLEAR;
1988 if (strcmp(service, "sshd") == 0) {
1989 if (action & A_STOP) stop_sshd();
1990 if (action & A_START) start_sshd();
1991 goto CLEAR;
1994 if (strcmp(service, "httpd") == 0) {
1995 if (action & A_STOP) stop_httpd();
1996 if (action & A_START) start_httpd();
1997 goto CLEAR;
2000 #ifdef TCONFIG_IPV6
2001 if (strcmp(service, "ipv6") == 0) {
2002 if (action & A_STOP) {
2003 stop_radvd();
2004 stop_ipv6();
2006 if (action & A_START) {
2007 start_ipv6();
2008 start_radvd();
2010 goto CLEAR;
2013 if (strcmp(service, "radvd") == 0) {
2014 if (action & A_STOP) {
2015 stop_radvd();
2017 if (action & A_START) {
2018 start_radvd();
2020 goto CLEAR;
2023 if (strncmp(service, "dhcp6", 5) == 0) {
2024 if (action & A_STOP) {
2025 stop_dhcp6c();
2027 if (action & A_START) {
2028 start_dhcp6c();
2030 goto CLEAR;
2032 #endif
2034 if (strcmp(service, "admin") == 0) {
2035 if (action & A_STOP) {
2036 stop_sshd();
2037 stop_telnetd();
2038 stop_httpd();
2040 stop_firewall(); start_firewall(); // always restarted
2041 if (action & A_START) {
2042 start_httpd();
2043 create_passwd();
2044 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2045 if (nvram_match("sshd_eas", "1")) start_sshd();
2047 goto CLEAR;
2050 if (strcmp(service, "ddns") == 0) {
2051 if (action & A_STOP) stop_ddns();
2052 if (action & A_START) start_ddns();
2053 goto CLEAR;
2056 if (strcmp(service, "ntpc") == 0) {
2057 if (action & A_STOP) stop_ntpc();
2058 if (action & A_START) start_ntpc();
2059 goto CLEAR;
2062 if (strcmp(service, "logging") == 0) {
2063 if (action & A_STOP) {
2064 stop_syslog();
2066 if (action & A_START) {
2067 start_syslog();
2069 if (!user) {
2070 // always restarted except from "service" command
2071 stop_cron(); start_cron();
2072 stop_firewall(); start_firewall();
2074 goto CLEAR;
2077 if (strcmp(service, "crond") == 0) {
2078 if (action & A_STOP) {
2079 stop_cron();
2081 if (action & A_START) {
2082 start_cron();
2084 goto CLEAR;
2087 #ifdef LINUX26
2088 if (strncmp(service, "hotplug", 7) == 0) {
2089 if (action & A_STOP) {
2090 stop_hotplug2();
2092 if (action & A_START) {
2093 start_hotplug2(1);
2095 goto CLEAR;
2097 #endif
2099 if (strcmp(service, "upgrade") == 0) {
2100 if (action & A_START) {
2101 #if TOMATO_SL
2102 stop_usbevent();
2103 stop_smbd();
2104 #endif
2105 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2106 stop_jffs2();
2107 // stop_cifs();
2108 stop_zebra();
2109 stop_cron();
2110 stop_ntpc();
2111 stop_upnp();
2112 // stop_dhcpc();
2113 killall("rstats", SIGTERM);
2114 killall("buttons", SIGTERM);
2115 stop_syslog();
2116 remove_storage_main(1); // !!TB - USB Support
2117 stop_usb(); // !!TB - USB Support
2119 goto CLEAR;
2122 #ifdef TCONFIG_CIFS
2123 if (strcmp(service, "cifs") == 0) {
2124 if (action & A_STOP) stop_cifs();
2125 if (action & A_START) start_cifs();
2126 goto CLEAR;
2128 #endif
2130 #ifdef TCONFIG_JFFS2
2131 if (strncmp(service, "jffs", 4) == 0) {
2132 if (action & A_STOP) stop_jffs2();
2133 if (action & A_START) start_jffs2();
2134 goto CLEAR;
2136 #endif
2138 if (strcmp(service, "zebra") == 0) {
2139 if (action & A_STOP) stop_zebra();
2140 if (action & A_START) start_zebra();
2141 goto CLEAR;
2144 if (strcmp(service, "routing") == 0) {
2145 if (action & A_STOP) {
2146 stop_zebra();
2147 do_static_routes(0); // remove old '_saved'
2148 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2150 stop_firewall();
2151 start_firewall();
2152 if (action & A_START) {
2153 do_static_routes(1); // add new
2154 start_zebra();
2155 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2157 goto CLEAR;
2160 if (strcmp(service, "ctnf") == 0) {
2161 if (action & A_START) {
2162 setup_conntrack();
2163 stop_firewall();
2164 start_firewall();
2166 goto CLEAR;
2169 if (strcmp(service, "wan") == 0) {
2170 if (action & A_STOP) {
2171 stop_wan();
2174 if (action & A_START) {
2175 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2176 start_wan(BOOT);
2177 sleep(2);
2178 force_to_dial();
2180 goto CLEAR;
2183 if (strcmp(service, "net") == 0) {
2184 if (action & A_STOP) {
2185 #ifdef TCONFIG_IPV6
2186 stop_radvd();
2187 #endif
2188 stop_httpd();
2189 stop_dnsmasq();
2190 stop_nas();
2191 stop_wan();
2192 stop_lan();
2193 stop_vlan();
2195 if (action & A_START) {
2196 start_vlan();
2197 start_lan();
2198 start_wan(BOOT);
2199 start_nas();
2200 start_dnsmasq();
2201 start_httpd();
2202 #ifdef TCONFIG_IPV6
2203 start_radvd();
2204 #endif
2205 start_wl();
2207 goto CLEAR;
2210 if (strcmp(service, "nas") == 0) {
2211 if (action & A_STOP) {
2212 stop_nas();
2214 if (action & A_START) {
2215 start_nas();
2216 start_wl();
2218 goto CLEAR;
2221 if (strcmp(service, "rstats") == 0) {
2222 if (action & A_STOP) stop_rstats();
2223 if (action & A_START) start_rstats(0);
2224 goto CLEAR;
2227 if (strcmp(service, "rstatsnew") == 0) {
2228 if (action & A_STOP) stop_rstats();
2229 if (action & A_START) start_rstats(1);
2230 goto CLEAR;
2233 if (strcmp(service, "sched") == 0) {
2234 if (action & A_STOP) stop_sched();
2235 if (action & A_START) start_sched();
2236 goto CLEAR;
2239 #ifdef TCONFIG_USB
2240 // !!TB - USB Support
2241 if (strcmp(service, "usb") == 0) {
2242 if (action & A_STOP) stop_usb();
2243 if (action & A_START) {
2244 start_usb();
2245 // restart Samba and ftp since they may be killed by stop_usb()
2246 restart_nas_services(0, 1);
2247 // remount all partitions by simulating hotplug event
2248 add_remove_usbhost("-1", 1);
2250 goto CLEAR;
2253 if (strcmp(service, "usbapps") == 0) {
2254 if (action & A_STOP) stop_nas_services();
2255 if (action & A_START) start_nas_services();
2256 goto CLEAR;
2258 #endif
2260 #ifdef TCONFIG_FTP
2261 // !!TB - FTP Server
2262 if (strcmp(service, "ftpd") == 0) {
2263 if (action & A_STOP) stop_ftpd();
2264 setup_conntrack();
2265 stop_firewall();
2266 start_firewall();
2267 if (action & A_START) start_ftpd();
2268 goto CLEAR;
2270 #endif
2272 #ifdef TCONFIG_MEDIA_SERVER
2273 if (strcmp(service, "media") == 0 || strcmp(service, "dlna") == 0) {
2274 if (action & A_STOP) stop_media_server();
2275 if (action & A_START) start_media_server();
2276 goto CLEAR;
2278 #endif
2280 #ifdef TCONFIG_SAMBASRV
2281 // !!TB - Samba
2282 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
2283 if (action & A_STOP) stop_samba();
2284 if (action & A_START) {
2285 create_passwd();
2286 stop_dnsmasq();
2287 start_dnsmasq();
2288 start_samba();
2290 goto CLEAR;
2292 #endif
2294 #ifdef TCONFIG_OPENVPN
2295 if (strncmp(service, "vpnclient", 9) == 0) {
2296 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
2297 if (action & A_START) start_vpnclient(atoi(&service[9]));
2298 goto CLEAR;
2301 if (strncmp(service, "vpnserver", 9) == 0) {
2302 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
2303 if (action & A_START) start_vpnserver(atoi(&service[9]));
2304 goto CLEAR;
2306 #endif
2308 CLEAR:
2309 if (next) goto TOP;
2311 // some functions check action_service and must be cleared at end -- zzz
2312 nvram_set("action_service", "");
2314 // Force recheck in 500 msec
2315 setitimer(ITIMER_REAL, &pop_tv, NULL);
2318 static void do_service(const char *name, const char *action, int user)
2320 int n;
2321 char s[64];
2323 n = 150;
2324 while (!nvram_match("action_service", "")) {
2325 if (user) {
2326 putchar('*');
2327 fflush(stdout);
2329 else if (--n < 0) break;
2330 usleep(100 * 1000);
2333 snprintf(s, sizeof(s), "%s-%s%s", name, action, (user ? "-c" : ""));
2334 nvram_set("action_service", s);
2335 kill(1, SIGUSR1);
2337 n = 150;
2338 while (nvram_match("action_service", s)) {
2339 if (user) {
2340 putchar('.');
2341 fflush(stdout);
2343 else if (--n < 0) {
2344 break;
2346 usleep(100 * 1000);
2350 int service_main(int argc, char *argv[])
2352 if (argc != 3) usage_exit(argv[0], "<service> <action>");
2353 do_service(argv[1], argv[2], 1);
2354 printf("\nDone.\n");
2355 return 0;
2358 void start_service(const char *name)
2360 do_service(name, "start", 0);
2363 void stop_service(const char *name)
2365 do_service(name, "stop", 0);
2369 void restart_service(const char *name)
2371 do_service(name, "restart", 0);