Merge branch 'tomato-ND-usbmod-mixvpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / services.c
blob42c184fdf3678ff920c0c10bd05f4a2882fc13e2
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 IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
49 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
51 // -----------------------------------------------------------------------------
53 static const char dmhosts[] = "/etc/hosts.dnsmasq";
54 static const char dmresolv[] = "/etc/resolv.dnsmasq";
55 static const char dmpid[] = "/var/run/dnsmasq.pid";
57 static pid_t pid_dnsmasq = -1;
60 void start_dnsmasq()
62 FILE *f;
63 const char *nv;
64 char buf[512];
65 char lan[24];
66 const char *router_ip;
67 const char *lan_ifname;
68 char sdhcp_lease[32];
69 char *e;
70 int n;
71 char *mac, *ip, *name;
72 char *p;
73 int ipn;
74 char ipbuf[32];
75 FILE *hf;
76 int dhcp_start;
77 int dhcp_count;
78 int dhcp_lease;
79 int do_dhcpd;
80 int do_dns;
82 TRACE_PT("begin\n");
84 if (getpid() != 1) {
85 start_service("dnsmasq");
86 return;
89 stop_dnsmasq();
91 if (nvram_match("wl_mode", "wet")) return;
92 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
94 lan_ifname = nvram_safe_get("lan_ifname");
95 router_ip = nvram_safe_get("lan_ipaddr");
96 strlcpy(lan, router_ip, sizeof(lan));
97 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
99 fprintf(f,
100 "pid-file=%s\n"
101 "interface=%s\n",
102 dmpid, lan_ifname);
103 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
104 if (*nv) fprintf(f, "domain=%s\n", nv);
107 // dns
108 const dns_list_t *dns = get_dns(); // this always points to a static buffer
110 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
111 else n = 4096;
112 fprintf(f,
113 "resolv-file=%s\n" // the real stuff is here
114 "addn-hosts=%s\n" // "
115 "expand-hosts\n" // expand hostnames in hosts file
116 "min-port=%u\n", // min port used for random src port
117 dmresolv, dmhosts, n);
118 do_dns = nvram_match("dhcpd_dmdns", "1");
120 for (n = 0 ; n < dns->count; ++n) {
121 if (dns->dns[n].port != 53) {
122 fprintf(f, "server=%s#%u\n", inet_ntoa(dns->dns[n].addr), dns->dns[n].port);
127 // dhcp
128 do_dhcpd = nvram_match("lan_proto", "dhcp");
129 if (do_dhcpd) {
130 dhcp_lease = nvram_get_int("dhcp_lease");
131 if (dhcp_lease <= 0) dhcp_lease = 1440;
133 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
134 if (n < 0) strcpy(sdhcp_lease, "infinite");
135 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
137 if (!do_dns) {
138 // if not using dnsmasq for dns
140 if ((dns->count == 0) && (nvram_get_int("dhcpd_llndns"))) {
141 // no DNS might be temporary. use a low lease time to force clients to update.
142 dhcp_lease = 2;
143 strcpy(sdhcp_lease, "2m");
144 do_dns = 1;
146 else {
147 // pass the dns directly
148 buf[0] = 0;
149 for (n = 0 ; n < dns->count; ++n) {
150 if (dns->dns[n].port == 53) { // check: option 6 doesn't seem to support other ports
151 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n].addr));
154 fprintf(f, "dhcp-option=6%s\n", buf);
158 if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
159 fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
161 else {
162 // for compatibility
163 dhcp_start = nvram_get_int("dhcp_start");
164 dhcp_count = nvram_get_int("dhcp_num");
165 fprintf(f, "dhcp-range=%s%d,%s%d,%s,%dm\n",
166 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
169 nv = router_ip;
170 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED)) {
171 p = nvram_safe_get("lan_gateway");
172 if ((*p) && (strcmp(p, "0.0.0.0") != 0)) nv = p;
175 n = nvram_get_int("dhcpd_lmax");
176 fprintf(f,
177 "dhcp-option=3,%s\n" // gateway
178 "dhcp-lease-max=%d\n",
180 (n > 0) ? n : 255);
182 if (nvram_get_int("dhcpd_auth") >= 0) {
183 fprintf(f, "dhcp-authoritative\n");
186 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
187 fprintf(f, "dhcp-option=44,%s\n", nv);
189 #ifdef TCONFIG_SAMBASRV
190 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
191 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
192 // Samba will serve as a WINS server
193 fprintf(f, "dhcp-option=44,0.0.0.0\n");
196 #endif
198 else {
199 fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
202 // write static lease entries & create hosts file
204 if ((hf = fopen(dmhosts, "w")) != NULL) {
205 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
206 fprintf(hf, "%s %s\n", router_ip, nv);
207 #ifdef TCONFIG_SAMBASRV
208 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
209 fprintf(hf, "%s %s\n", router_ip, nv);
210 #endif
213 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
214 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
215 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
216 p = nvram_safe_get("dhcpd_static");
217 while ((e = strchr(p, '>')) != NULL) {
218 n = (e - p);
219 if (n > 105) {
220 p = e + 1;
221 continue;
224 strncpy(buf, p, n);
225 buf[n] = 0;
226 p = e + 1;
228 if ((e = strchr(buf, '<')) == NULL) continue;
229 *e = 0;
230 mac = buf;
232 ip = e + 1;
233 if ((e = strchr(ip, '<')) == NULL) continue;
234 *e = 0;
235 if (strchr(ip, '.') == NULL) {
236 ipn = atoi(ip);
237 if ((ipn <= 0) || (ipn > 255)) continue;
238 sprintf(ipbuf, "%s%d", lan, ipn);
239 ip = ipbuf;
241 else {
242 if (inet_addr(ip) == INADDR_NONE) continue;
245 name = e + 1;
247 if ((hf) && (*name != 0)) {
248 fprintf(hf, "%s %s\n", ip, name);
251 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
252 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
256 if (hf) fclose(hf);
260 #ifdef TCONFIG_OPENVPN
261 write_vpn_dnsmasq_config(f);
262 #endif
264 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
266 fappend(f, "/etc/dnsmasq.custom");
270 fclose(f);
272 if (do_dns) {
273 unlink("/etc/resolv.conf");
274 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
277 TRACE_PT("run dnsmasq\n");
279 eval("dnsmasq");
281 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
282 f_read_string(dmpid, buf, sizeof(buf));
283 pid_dnsmasq = atol(buf);
286 TRACE_PT("end\n");
289 void stop_dnsmasq(void)
291 TRACE_PT("begin\n");
293 if (getpid() != 1) {
294 stop_service("dnsmasq");
295 return;
298 pid_dnsmasq = -1;
300 unlink("/etc/resolv.conf");
301 symlink(dmresolv, "/etc/resolv.conf");
303 killall_tk("dnsmasq");
305 TRACE_PT("end\n");
308 void clear_resolv(void)
310 f_write(dmresolv, NULL, 0, 0, 0); // blank
313 void dns_to_resolv(void)
315 FILE *f;
316 const dns_list_t *dns;
317 int i;
318 mode_t m;
320 m = umask(022); // 077 from pppoecd
321 if ((f = fopen(dmresolv, "w")) != NULL) {
322 // Check for VPN DNS entries
323 if (!write_vpn_resolv(f)) {
324 dns = get_dns(); // static buffer
325 if (dns->count == 0) {
326 // Put a pseudo DNS IP to trigger Connect On Demand
327 if ((nvram_match("ppp_demand", "1")) &&
328 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
329 fprintf(f, "nameserver 1.1.1.1\n");
332 else {
333 for (i = 0; i < dns->count; i++) {
334 if (dns->dns[i].port == 53) { // resolv.conf doesn't allow for an alternate port
335 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i].addr));
340 fclose(f);
342 umask(m);
345 // -----------------------------------------------------------------------------
347 void start_httpd(void)
349 chdir("/www");
350 if (!nvram_match("http_enable", "0")) {
351 xstart("httpd");
353 if (!nvram_match("https_enable", "0")) {
354 xstart("httpd", "-s");
356 chdir("/");
359 void stop_httpd(void)
361 killall_tk("httpd");
364 // -----------------------------------------------------------------------------
366 void start_upnp(void)
368 if (get_wan_proto() == WP_DISABLED) return;
370 int enable;
371 FILE *f;
372 int upnp_port;
374 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
375 mkdir("/etc/upnp", 0777);
376 if (f_exists("/etc/upnp/config.alt")) {
377 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
379 else {
380 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
381 upnp_port = nvram_get_int("upnp_port");
382 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
384 char *lanip = nvram_safe_get("lan_ipaddr");
385 char *lanmask = nvram_safe_get("lan_netmask");
387 fprintf(f,
388 "ext_ifname=%s\n"
389 "listening_ip=%s/%s\n"
390 "port=%d\n"
391 "enable_upnp=%s\n"
392 "enable_natpmp=%s\n"
393 "secure_mode=%s\n"
394 "upnp_forward_chain=upnp\n"
395 "upnp_nat_chain=upnp\n"
396 "notify_interval=%d\n"
397 "system_uptime=yes\n"
398 "\n"
400 nvram_safe_get("wan_iface"),
401 lanip, lanmask,
402 upnp_port,
403 (enable & 1) ? "yes" : "no", // upnp enable
404 (enable & 2) ? "yes" : "no", // natpmp enable
405 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
406 nvram_get_int("upnp_ssdp_interval")
409 if (nvram_get_int("upnp_clean")) {
410 int interval = nvram_get_int("upnp_clean_interval");
411 if (interval < 60) interval = 60;
412 fprintf(f,
413 "clean_ruleset_interval=%d\n"
414 "clean_ruleset_threshold=%d\n",
415 interval,
416 nvram_get_int("upnp_clean_threshold")
419 else
420 fprintf(f,"clean_ruleset_interval=0\n");
422 if (nvram_match("upnp_mnp", "1")) {
423 int https = nvram_get_int("https_enable");
424 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
425 https ? "s" : "", lanip,
426 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
428 else {
429 // Empty parameters are not included into XML service description
430 fprintf(f, "presentation_url=\n");
433 char uuid[45];
434 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
435 fprintf(f, "uuid=%s\n", uuid);
437 int ports[4];
438 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
439 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
440 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
441 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
442 fprintf(f,
443 "allow %d-%d %s/%s %d-%d\n",
444 ports[0], ports[1],
445 lanip, lanmask,
446 ports[2], ports[3]
449 else {
450 // by default allow only redirection of ports above 1024
451 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
454 fappend(f, "/etc/upnp/config.custom");
455 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
456 fclose(f);
458 xstart("miniupnpd", "-f", "/etc/upnp/config");
464 void stop_upnp(void)
466 killall_tk("miniupnpd");
469 // -----------------------------------------------------------------------------
471 static pid_t pid_crond = -1;
473 void start_cron(void)
475 char *argv[] = { "crond", "-l", "9", NULL };
477 stop_cron();
479 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
480 _eval(argv, NULL, 0, NULL);
481 if (!nvram_contains_word("debug_norestart", "crond")) {
482 pid_crond = -2;
487 void stop_cron(void)
489 pid_crond = -1;
490 killall_tk("crond");
493 // -----------------------------------------------------------------------------
494 #ifdef LINUX26
496 static pid_t pid_hotplug2 = -1;
498 void start_hotplug2()
500 stop_hotplug2();
502 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE, 0);
503 xstart("hotplug2", "--persistent", "--no-coldplug");
504 sleep(1);
506 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
507 pid_hotplug2 = -2;
511 void stop_hotplug2(void)
513 pid_hotplug2 = -1;
514 killall_tk("hotplug2");
517 #endif /* LINUX26 */
518 // -----------------------------------------------------------------------------
520 // Written by Sparq in 2002/07/16
521 void start_zebra(void)
523 #ifdef TCONFIG_ZEBRA
524 FILE *fp;
526 char *lan_tx = nvram_safe_get("dr_lan_tx");
527 char *lan_rx = nvram_safe_get("dr_lan_rx");
528 char *wan_tx = nvram_safe_get("dr_wan_tx");
529 char *wan_rx = nvram_safe_get("dr_wan_rx");
531 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
532 return;
535 // empty
536 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
537 fclose(fp);
541 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
542 char *lan_ifname = nvram_safe_get("lan_ifname");
543 char *wan_ifname = nvram_safe_get("wan_ifname");
545 fprintf(fp, "router rip\n");
546 fprintf(fp, "network %s\n", lan_ifname);
547 fprintf(fp, "network %s\n", wan_ifname);
548 fprintf(fp, "redistribute connected\n");
549 //fprintf(fp, "redistribute static\n");
551 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
552 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
554 fprintf(fp, "interface %s\n", lan_ifname);
555 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
556 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
558 fprintf(fp, "interface %s\n", wan_ifname);
559 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
560 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
562 fprintf(fp, "router rip\n");
563 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
564 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
565 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
566 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
567 fprintf(fp, "access-list private deny any\n");
569 //fprintf(fp, "debug rip events\n");
570 //fprintf(fp, "log file /etc/ripd.log\n");
571 fclose(fp);
574 xstart("zebra", "-d");
575 xstart("ripd", "-d");
576 #endif
579 void stop_zebra(void)
581 #ifdef TCONFIG_ZEBRA
582 killall("zebra", SIGTERM);
583 killall("ripd", SIGTERM);
585 unlink("/etc/zebra.conf");
586 unlink("/etc/ripd.conf");
587 #endif
590 // -----------------------------------------------------------------------------
592 void start_syslog(void)
594 #if 1
595 char *argv[12];
596 int argc;
597 char *nv;
598 char rem[256];
599 int n;
600 char s[64];
602 argv[0] = "syslogd";
603 argc = 1;
605 if (nvram_match("log_remote", "1")) {
606 nv = nvram_safe_get("log_remoteip");
607 if (*nv) {
608 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
609 argv[argc++] = "-R";
610 argv[argc++] = rem;
614 if (nvram_match("log_file", "1")) {
615 argv[argc++] = "-L";
616 argv[argc++] = "-s";
617 argv[argc++] = "50";
620 if (argc > 1) {
621 argv[argc] = NULL;
622 _eval(argv, NULL, 0, NULL);
623 usleep(500000);
625 argv[0] = "klogd";
626 argv[1] = NULL;
627 _eval(argv, NULL, 0, NULL);
628 usleep(500000);
630 // used to be available in syslogd -m
631 n = nvram_get_int("log_mark");
632 if (n > 0) {
633 sprintf(s, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
634 (n < 60) ? "*/30" : "0", (n < 120) ? "*" : "*/2");
635 system(s);
637 else {
638 system("cru d syslogdmark");
642 #else
643 char *argv[12];
644 int argc;
645 char *nv;
646 char rem[256];
648 argv[0] = "syslogd";
649 argv[1] = "-m";
650 argv[2] = nvram_get("log_mark");
651 argc = 3;
653 if (nvram_match("log_remote", "1")) {
654 nv = nvram_safe_get("log_remoteip");
655 if (*nv) {
656 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
657 argv[argc++] = "-R";
658 argv[argc++] = rem;
662 if (nvram_match("log_file", "1")) {
663 argv[argc++] = "-L";
664 argv[argc++] = "-s";
665 argv[argc++] = "50";
668 if (argc > 3) {
669 argv[argc] = NULL;
670 _eval(argv, NULL, 0, NULL);
671 usleep(500000);
673 argv[0] = "klogd";
674 argv[1] = NULL;
675 _eval(argv, NULL, 0, NULL);
676 usleep(500000);
678 #endif
681 void stop_syslog(void)
683 killall("klogd", SIGTERM);
684 killall("syslogd", SIGTERM);
687 // -----------------------------------------------------------------------------
689 static pid_t pid_igmp = -1;
691 void start_igmp_proxy(void)
693 FILE *fp;
694 char *p;
696 pid_igmp = -1;
697 if (nvram_match("multicast_pass", "1")) {
698 switch (get_wan_proto()) {
699 case WP_DISABLED:
700 return;
701 case WP_PPPOE:
702 case WP_PPTP:
703 case WP_L2TP:
704 p = "wan_iface";
705 break;
706 default:
707 p = "wan_ifname";
708 break;
711 if (f_exists("/etc/igmp.alt")) {
712 xstart("igmpproxy", "/etc/igmp.alt");
714 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
715 fprintf(fp,
716 "quickleave\n"
717 "phyint %s upstream\n"
718 "\taltnet %s\n"
719 "phyint %s downstream ratelimit 0\n",
720 nvram_safe_get(p),
721 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
722 nvram_safe_get("lan_ifname"));
723 fclose(fp);
724 xstart("igmpproxy", "/etc/igmp.conf");
726 else {
727 return;
729 if (!nvram_contains_word("debug_norestart", "igmprt")) {
730 pid_igmp = -2;
735 void stop_igmp_proxy(void)
737 pid_igmp = -1;
738 killall("igmpproxy", SIGTERM);
742 // -----------------------------------------------------------------------------
744 void set_tz(void)
746 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
749 void start_ntpc(void)
751 set_tz();
753 stop_ntpc();
755 if (nvram_get_int("ntp_updates") >= 0) {
756 xstart("ntpsync", "--init");
760 void stop_ntpc(void)
762 killall("ntpsync", SIGTERM);
765 // -----------------------------------------------------------------------------
767 static void stop_rstats(void)
769 int n;
770 int pid;
772 n = 60;
773 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
774 if (kill(pid, SIGTERM) != 0) break;
775 sleep(1);
779 static void start_rstats(int new)
781 if (nvram_match("rstats_enable", "1")) {
782 stop_rstats();
783 if (new) xstart("rstats", "--new");
784 else xstart("rstats");
788 // -----------------------------------------------------------------------------
790 // !!TB - FTP Server
792 #ifdef TCONFIG_USB
794 * Return non-zero if we created the directory,
795 * and zero if it already existed.
797 int mkdir_if_none(char *dir)
799 DIR *dp;
800 if (!(dp=opendir(dir))) {
801 umask(0000);
802 mkdir(dir, 0777);
803 return 1;
805 closedir(dp);
806 return 0;
809 char *get_full_storage_path(char *val)
811 static char buf[128];
812 int len;
814 if (val[0] == '/')
815 len = sprintf(buf, "%s", val);
816 else
817 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
819 if (len > 1 && buf[len - 1] == '/')
820 buf[len - 1] = 0;
822 return buf;
825 char *nvram_storage_path(char *var)
827 char *val = nvram_safe_get(var);
828 return get_full_storage_path(val);
830 #endif // TCONFIG_USB
832 #ifdef TCONFIG_FTP
834 char vsftpd_conf[] = "/etc/vsftpd.conf";
835 char vsftpd_users[] = "/etc/vsftpd.users";
836 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
837 #endif
839 #ifdef TCONFIG_FTP
840 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
841 static void do_start_stop_ftpd(int stop, int start)
843 if (stop) killall_tk("vsftpd");
845 char tmp[256];
846 FILE *fp, *f;
848 if (!start || !nvram_get_int("ftp_enable")) return;
850 mkdir_if_none(vsftpd_users);
851 mkdir_if_none("/var/run/vsftpd");
853 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
854 return;
856 if (nvram_get_int("ftp_super"))
858 /* rights */
859 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
860 if ((f = fopen(tmp, "w")))
862 fprintf(f,
863 "dirlist_enable=yes\n"
864 "write_enable=yes\n"
865 "download_enable=yes\n");
866 fclose(f);
870 #ifdef TCONFIG_SAMBASRV
871 if (nvram_match("smbd_cset", "utf8"))
872 fprintf(fp, "utf8=yes\n");
873 #endif
875 if (nvram_invmatch("ftp_anonymous", "0"))
877 fprintf(fp,
878 "anon_allow_writable_root=yes\n"
879 "anon_world_readable_only=no\n"
880 "anon_umask=022\n");
882 /* rights */
883 sprintf(tmp, "%s/ftp", vsftpd_users);
884 if ((f = fopen(tmp, "w")))
886 if (nvram_match("ftp_dirlist", "0"))
887 fprintf(f, "dirlist_enable=yes\n");
888 if (nvram_match("ftp_anonymous", "1") ||
889 nvram_match("ftp_anonymous", "3"))
890 fprintf(f, "write_enable=yes\n");
891 if (nvram_match("ftp_anonymous", "1") ||
892 nvram_match("ftp_anonymous", "2"))
893 fprintf(f, "download_enable=yes\n");
894 fclose(f);
896 if (nvram_match("ftp_anonymous", "1") ||
897 nvram_match("ftp_anonymous", "3"))
898 fprintf(fp,
899 "anon_upload_enable=yes\n"
900 "anon_mkdir_write_enable=yes\n"
901 "anon_other_write_enable=yes\n");
902 } else {
903 fprintf(fp, "anonymous_enable=no\n");
906 fprintf(fp,
907 "dirmessage_enable=yes\n"
908 "download_enable=no\n"
909 "dirlist_enable=no\n"
910 "hide_ids=yes\n"
911 "syslog_enable=yes\n"
912 "local_enable=yes\n"
913 "local_umask=022\n"
914 "chmod_enable=no\n"
915 "chroot_local_user=yes\n"
916 "check_shell=no\n"
917 "log_ftp_protocol=%s\n"
918 "user_config_dir=%s\n"
919 "passwd_file=%s\n"
920 "listen=yes\n"
921 "listen_port=%s\n"
922 "background=yes\n"
923 "isolate=no\n"
924 "max_clients=%d\n"
925 "max_per_ip=%d\n"
926 "idle_session_timeout=%s\n"
927 "use_sendfile=no\n"
928 "anon_max_rate=%d\n"
929 "local_max_rate=%d\n"
930 "%s\n",
931 nvram_get_int("log_ftp") ? "yes" : "no",
932 vsftpd_users, vsftpd_passwd,
933 nvram_get("ftp_port") ? : "21",
934 nvram_get_int("ftp_max"),
935 nvram_get_int("ftp_ipmax"),
936 nvram_get("ftp_staytimeout") ? : "300",
937 nvram_get_int("ftp_anonrate") * 1024,
938 nvram_get_int("ftp_rate") * 1024,
939 nvram_safe_get("ftp_custom"));
941 fclose(fp);
943 /* prepare passwd file and default users */
944 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
945 return;
947 fprintf(fp, /* anonymous, admin, nobody */
948 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
949 "%s:%s:0:0:root:/:/sbin/nologin\n"
950 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
951 nvram_storage_path("ftp_anonroot"), "admin",
952 nvram_get_int("ftp_super") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
953 MOUNT_ROOT);
955 char *buf;
956 char *p, *q;
957 char *user, *pass, *rights;
959 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
962 username<password<rights
963 rights:
964 Read/Write
965 Read Only
966 View Only
967 Private
969 p = buf;
970 while ((q = strsep(&p, ">")) != NULL) {
971 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
972 if (!user || !pass) continue;
974 /* directory */
975 if (strncmp(rights, "Private", 7) == 0)
977 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
978 mkdir_if_none(tmp);
980 else
981 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
983 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
984 user, crypt(pass, "$1$"), user, tmp);
986 /* rights */
987 sprintf(tmp, "%s/%s", vsftpd_users, user);
988 if ((f = fopen(tmp, "w")))
990 tmp[0] = 0;
991 if (nvram_invmatch("ftp_dirlist", "1"))
992 strcat(tmp, "dirlist_enable=yes\n");
993 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
994 strcat(tmp, "download_enable=yes\n");
995 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
996 strcat(tmp, "write_enable=yes\n");
998 fputs(tmp, f);
999 fclose(f);
1002 free(buf);
1005 fclose(fp);
1006 killall("vsftpd", SIGHUP);
1008 /* start vsftpd if it's not already running */
1009 if (pidof("vsftpd") <= 0)
1010 eval("vsftpd");
1012 #endif
1014 void start_ftpd(void)
1016 #ifdef TCONFIG_FTP
1017 int fd = file_lock("usb");
1018 do_start_stop_ftpd(0, 1);
1019 file_unlock(fd);
1020 #endif
1023 void stop_ftpd(void)
1025 #ifdef TCONFIG_FTP
1026 int fd = file_lock("usb");
1027 do_start_stop_ftpd(1, 0);
1028 unlink(vsftpd_passwd);
1029 unlink(vsftpd_conf);
1030 eval("rm", "-rf", vsftpd_users);
1031 file_unlock(fd);
1032 #endif
1035 // -----------------------------------------------------------------------------
1037 // !!TB - Samba
1039 #ifdef TCONFIG_SAMBASRV
1040 void kill_samba(int sig)
1042 if (sig == SIGTERM) {
1043 killall_tk("smbd");
1044 killall_tk("nmbd");
1046 else {
1047 killall("smbd", sig);
1048 killall("nmbd", sig);
1051 #endif
1053 #ifdef TCONFIG_SAMBASRV
1054 static void do_start_stop_samba(int stop, int start)
1056 if (stop) kill_samba(SIGTERM);
1058 FILE *fp;
1059 DIR *dir = NULL;
1060 struct dirent *dp;
1061 char nlsmod[15];
1062 int mode;
1063 char *nv;
1065 mode = nvram_get_int("smbd_enable");
1066 if (!start || !mode || !nvram_invmatch("lan_hostname", ""))
1067 return;
1069 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1070 return;
1072 fprintf(fp, "[global]\n"
1073 " interfaces = %s\n"
1074 " bind interfaces only = yes\n"
1075 " workgroup = %s\n"
1076 " netbios name = %s\n"
1077 " server string = %s\n"
1078 " guest account = nobody\n"
1079 " security = %s\n"
1080 " browseable = yes\n"
1081 " guest ok = yes\n"
1082 " guest only = no\n"
1083 " log level = %d\n"
1084 " syslog only = yes\n"
1085 " timestamp logs = no\n"
1086 " syslog = 1\n"
1087 " dns proxy = no\n"
1088 " encrypt passwords = yes\n"
1089 " preserve case = yes\n"
1090 " short preserve case = yes\n",
1091 nvram_safe_get("lan_ifname"),
1092 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1093 nvram_safe_get("lan_hostname"),
1094 nvram_get("router_name") ? : "Tomato",
1095 mode == 2 ? "user" : "share",
1096 nvram_get_int("smbd_loglevel")
1099 if (nvram_get_int("smbd_wins")) {
1100 nv = nvram_safe_get("wan_wins");
1101 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1102 fprintf(fp, " wins support = yes\n");
1106 if (nvram_get_int("smbd_master")) {
1107 fprintf(fp,
1108 " domain master = yes\n"
1109 " local master = yes\n"
1110 " preferred master = yes\n"
1111 " os level = 65\n");
1114 nv = nvram_safe_get("smbd_cpage");
1115 if (*nv) {
1116 #ifndef TCONFIG_SAMBA3
1117 fprintf(fp, " client code page = %s\n", nv);
1118 #endif
1119 sprintf(nlsmod, "nls_cp%s", nv);
1121 nv = nvram_safe_get("smbd_nlsmod");
1122 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1123 modprobe_r(nv);
1125 modprobe(nlsmod);
1126 nvram_set("smbd_nlsmod", nlsmod);
1129 #ifndef TCONFIG_SAMBA3
1130 if (nvram_match("smbd_cset", "utf8"))
1131 fprintf(fp, " coding system = utf8\n");
1132 else if (nvram_invmatch("smbd_cset", ""))
1133 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1134 #endif
1136 fprintf(fp, "%s\n\n", nvram_safe_get("smbd_custom"));
1138 /* configure shares */
1140 char *buf;
1141 char *p, *q;
1142 char *name, *path, *comment, *writeable, *hidden;
1143 int cnt = 0;
1145 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1147 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1149 p = buf;
1150 while ((q = strsep(&p, ">")) != NULL) {
1151 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1152 if (!path || !name) continue;
1154 /* share name */
1155 fprintf(fp, "\n[%s]\n", name);
1157 /* path */
1158 fprintf(fp, " path = %s\n", path);
1160 /* access level */
1161 if (!strcmp(writeable, "1"))
1162 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1163 if (!strcmp(hidden, "1"))
1164 fprintf(fp, " browseable = no\n");
1166 /* comment */
1167 if (comment)
1168 fprintf(fp, " comment = %s\n", comment);
1170 cnt++;
1172 free(buf);
1175 /* share everything below MOUNT_ROOT */
1176 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1177 while ((dp = readdir(dir))) {
1178 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1180 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1181 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1182 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1183 if (nvram_match("smbd_autoshare", "3")) // Hidden
1184 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1185 dp->d_name, MOUNT_ROOT, dp->d_name);
1186 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1187 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1189 cnt++;
1193 if (dir) closedir(dir);
1195 if (cnt == 0) {
1196 /* by default share MOUNT_ROOT as read-only */
1197 fprintf(fp, "\n[share]\n"
1198 " path = %s\n"
1199 " writable = no\n",
1200 MOUNT_ROOT);
1203 fclose(fp);
1205 mkdir_if_none("/var/run/samba");
1206 mkdir_if_none("/etc/samba");
1208 /* write smbpasswd */
1209 #ifdef TCONFIG_SAMBA3
1210 eval("smbpasswd", "nobody", "\"\"");
1211 #else
1212 eval("smbpasswd", "-a", "nobody", "\"\"");
1213 #endif
1214 if (mode == 2) {
1215 char *smbd_user;
1216 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1217 smbd_user = "nas";
1218 #ifdef TCONFIG_SAMBA3
1219 eval("smbpasswd", smbd_user, nvram_safe_get("smbd_passwd"));
1220 #else
1221 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1222 #endif
1225 kill_samba(SIGHUP);
1226 int ret1 = 0, ret2 = 0;
1227 /* start samba if it's not already running */
1228 if (pidof("nmbd") <= 0)
1229 ret1 = eval("nmbd", "-D");
1230 if (pidof("smbd") <= 0)
1231 ret2 = eval("smbd", "-D");
1233 if (ret1 || ret2) kill_samba(SIGTERM);
1235 #endif
1237 void start_samba(void)
1239 #ifdef TCONFIG_SAMBASRV
1240 int fd = file_lock("usb");
1241 do_start_stop_samba(0, 1);
1242 file_unlock(fd);
1243 #endif
1246 void stop_samba(void)
1248 #ifdef TCONFIG_SAMBASRV
1249 int fd = file_lock("usb");
1250 do_start_stop_samba(1, 0);
1252 if (nvram_invmatch("smbd_nlsmod", "")) {
1253 modprobe_r(nvram_get("smbd_nlsmod"));
1254 nvram_set("smbd_nlsmod", "");
1257 /* clean up */
1258 unlink("/var/log/smb");
1259 unlink("/var/log/nmb");
1260 eval("rm", "-rf", "/var/run/samba");
1261 file_unlock(fd);
1262 #endif
1265 #ifdef TCONFIG_USB
1266 void restart_nas_services(int stop, int start)
1268 /* restart all NAS applications */
1269 #if TCONFIG_SAMBASRV || TCONFIG_FTP
1270 int fd = file_lock("usb");
1271 #ifdef TCONFIG_SAMBASRV
1272 do_start_stop_samba(stop, start && nvram_get_int("smbd_enable"));
1273 #endif
1274 #ifdef TCONFIG_FTP
1275 do_start_stop_ftpd(stop, start && nvram_get_int("ftp_enable"));
1276 #endif
1277 file_unlock(fd);
1278 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1280 #endif // TCONFIG_USB
1282 // -----------------------------------------------------------------------------
1284 static void _check(pid_t *pid, const char *name, void (*func)(void) )
1286 if (*pid != -1) {
1287 if (kill(*pid, 0) != 0) {
1288 if ((*pid = pidof(name)) == -1) func();
1293 void check_services(void)
1295 #ifdef LINUX26
1296 _check(&pid_hotplug2, "hotplug2", start_hotplug2);
1297 #endif
1298 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
1299 _check(&pid_crond, "crond", start_cron);
1300 _check(&pid_igmp, "igmpproxy", start_igmp_proxy);
1303 // -----------------------------------------------------------------------------
1305 void start_services(void)
1307 static int once = 1;
1309 if (once) {
1310 once = 0;
1312 create_passwd();
1313 if (nvram_get_int("telnetd_eas")) start_telnetd();
1314 if (nvram_get_int("sshd_eas")) start_sshd();
1317 // start_syslog();
1318 start_nas();
1319 start_zebra();
1320 start_dnsmasq();
1321 start_cifs();
1322 start_httpd();
1323 start_cron();
1324 // start_upnp();
1325 start_rstats(0);
1326 start_sched();
1327 restart_nas_services(1, 1); // !!TB - Samba and FTP Server
1330 void stop_services(void)
1332 clear_resolv();
1334 stop_ftpd(); // !!TB - FTP Server
1335 stop_samba(); // !!TB - Samba
1336 stop_sched();
1337 stop_rstats();
1338 // stop_upnp();
1339 stop_cron();
1340 stop_httpd();
1341 stop_cifs();
1342 stop_dnsmasq();
1343 stop_zebra();
1344 stop_nas();
1345 // stop_syslog();
1348 // -----------------------------------------------------------------------------
1350 void exec_service(void)
1352 const int A_START = 1;
1353 const int A_STOP = 2;
1354 const int A_RESTART = 1|2;
1355 char buffer[128];
1356 char *service;
1357 char *act;
1358 char *next;
1359 int action;
1360 int i;
1362 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1363 next = buffer;
1365 TOP:
1366 act = strsep(&next, ",");
1367 service = strsep(&act, "-");
1368 if (act == NULL) {
1369 next = NULL;
1370 goto CLEAR;
1373 TRACE_PT("service=%s action=%s\n", service, act);
1375 if (strcmp(act, "start") == 0) action = A_START;
1376 else if (strcmp(act, "stop") == 0) action = A_STOP;
1377 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1378 else action = 0;
1381 if (strcmp(service, "dhcpc") == 0) {
1382 if (action & A_STOP) stop_dhcpc();
1383 if (action & A_START) start_dhcpc();
1384 goto CLEAR;
1387 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1388 if (action & A_STOP) stop_dnsmasq();
1389 if (action & A_START) {
1390 dns_to_resolv();
1391 start_dnsmasq();
1393 goto CLEAR;
1396 if (strcmp(service, "firewall") == 0) {
1397 if (action & A_STOP) {
1398 stop_firewall();
1399 stop_igmp_proxy();
1401 if (action & A_START) {
1402 start_firewall();
1403 start_igmp_proxy();
1405 goto CLEAR;
1408 if (strcmp(service, "restrict") == 0) {
1409 if (action & A_STOP) {
1410 stop_firewall();
1412 if (action & A_START) {
1413 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1415 start_firewall();
1417 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1418 if (i == 1) {
1419 if (nvram_get_int("rrules_radio") < 0) {
1420 if (!get_radio()) eval("radio", "on");
1424 goto CLEAR;
1427 if (strcmp(service, "qos") == 0) {
1428 if (action & A_STOP) {
1429 stop_qos();
1431 stop_firewall(); start_firewall(); // always restarted
1432 if (action & A_START) {
1433 start_qos();
1434 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1436 goto CLEAR;
1439 if (strcmp(service, "upnp") == 0) {
1440 if (action & A_STOP) {
1441 stop_upnp();
1443 stop_firewall(); start_firewall(); // always restarted
1444 if (action & A_START) {
1445 start_upnp();
1447 goto CLEAR;
1450 if (strcmp(service, "telnetd") == 0) {
1451 if (action & A_STOP) stop_telnetd();
1452 if (action & A_START) start_telnetd();
1453 goto CLEAR;
1456 if (strcmp(service, "sshd") == 0) {
1457 if (action & A_STOP) stop_sshd();
1458 if (action & A_START) start_sshd();
1459 goto CLEAR;
1462 if (strcmp(service, "httpd") == 0) {
1463 if (action & A_STOP) stop_httpd();
1464 if (action & A_START) start_httpd();
1465 goto CLEAR;
1468 if (strcmp(service, "admin") == 0) {
1469 if (action & A_STOP) {
1470 stop_sshd();
1471 stop_telnetd();
1472 stop_httpd();
1474 stop_firewall(); start_firewall(); // always restarted
1475 if (action & A_START) {
1476 start_httpd();
1477 create_passwd();
1478 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1479 if (nvram_match("sshd_eas", "1")) start_sshd();
1481 goto CLEAR;
1484 if (strcmp(service, "ddns") == 0) {
1485 if (action & A_STOP) stop_ddns();
1486 if (action & A_START) start_ddns();
1487 goto CLEAR;
1490 if (strcmp(service, "ntpc") == 0) {
1491 if (action & A_STOP) stop_ntpc();
1492 if (action & A_START) start_ntpc();
1493 goto CLEAR;
1496 if (strcmp(service, "logging") == 0) {
1497 if (action & A_STOP) {
1498 stop_syslog();
1499 stop_cron();
1501 stop_firewall(); start_firewall(); // always restarted
1502 if (action & A_START) {
1503 start_cron();
1504 start_syslog();
1506 goto CLEAR;
1509 if (strcmp(service, "crond") == 0) {
1510 if (action & A_STOP) {
1511 stop_cron();
1513 if (action & A_START) {
1514 start_cron();
1516 goto CLEAR;
1519 #ifdef LINUX26
1520 if (strncmp(service, "hotplug", 7) == 0) {
1521 if (action & A_STOP) {
1522 stop_hotplug2();
1524 if (action & A_START) {
1525 start_hotplug2(1);
1527 goto CLEAR;
1529 #endif
1531 if (strcmp(service, "upgrade") == 0) {
1532 if (action & A_START) {
1533 #if TOMATO_SL
1534 stop_usbevent();
1535 stop_smbd();
1536 #endif
1537 stop_ftpd(); // !!TB - FTP Server
1538 stop_samba(); // !!TB - Samba
1539 stop_jffs2();
1540 // stop_cifs();
1541 stop_zebra();
1542 stop_cron();
1543 stop_ntpc();
1544 stop_upnp();
1545 // stop_dhcpc();
1546 killall("rstats", SIGTERM);
1547 killall("buttons", SIGTERM);
1548 stop_syslog();
1549 remove_storage_main(1); // !!TB - USB Support
1550 stop_usb(); // !!TB - USB Support
1552 goto CLEAR;
1555 #ifdef TCONFIG_CIFS
1556 if (strcmp(service, "cifs") == 0) {
1557 if (action & A_STOP) stop_cifs();
1558 if (action & A_START) start_cifs();
1559 goto CLEAR;
1561 #endif
1563 #ifdef TCONFIG_JFFS2
1564 if (strcmp(service, "jffs2") == 0) {
1565 if (action & A_STOP) stop_jffs2();
1566 if (action & A_START) start_jffs2();
1567 goto CLEAR;
1569 #endif
1571 if (strcmp(service, "routing") == 0) {
1572 if (action & A_STOP) {
1573 stop_zebra();
1574 do_static_routes(0); // remove old '_saved'
1575 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1577 stop_firewall();
1578 start_firewall();
1579 if (action & A_START) {
1580 do_static_routes(1); // add new
1581 start_zebra();
1582 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1584 goto CLEAR;
1587 if (strcmp(service, "ctnf") == 0) {
1588 if (action & A_START) {
1589 setup_conntrack();
1590 stop_firewall();
1591 start_firewall();
1593 goto CLEAR;
1596 if (strcmp(service, "wan") == 0) {
1597 if (action & A_STOP) {
1598 if (get_wan_proto() == WP_PPPOE) {
1599 stop_dnsmasq();
1600 stop_redial();
1601 stop_singe_pppoe(PPPOE0);
1602 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
1603 sleep(1);
1604 start_pppoe(PPPOE0);
1606 start_dnsmasq();
1608 else {
1609 stop_wan();
1613 if (action & A_START) {
1614 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1616 if (get_wan_proto() == WP_PPPOE) {
1617 stop_singe_pppoe(PPPOE0);
1618 start_pppoe(PPPOE0);
1619 if (nvram_invmatch("ppp_demand", "1")) {
1620 start_redial();
1623 else {
1624 start_wan(BOOT);
1626 sleep(2);
1627 force_to_dial();
1629 goto CLEAR;
1632 if (strcmp(service, "net") == 0) {
1633 if (action & A_STOP) {
1634 stop_wan();
1635 stop_nas();
1636 stop_lan();
1637 stop_vlan();
1639 if (action & A_START) {
1640 start_vlan();
1641 start_lan();
1642 start_nas();
1643 start_wan(BOOT);
1644 start_wl();
1646 goto CLEAR;
1649 if (strcmp(service, "rstats") == 0) {
1650 if (action & A_STOP) stop_rstats();
1651 if (action & A_START) start_rstats(0);
1652 goto CLEAR;
1655 if (strcmp(service, "rstatsnew") == 0) {
1656 if (action & A_STOP) stop_rstats();
1657 if (action & A_START) start_rstats(1);
1658 goto CLEAR;
1661 if (strcmp(service, "sched") == 0) {
1662 if (action & A_STOP) stop_sched();
1663 if (action & A_START) start_sched();
1664 goto CLEAR;
1667 #ifdef TCONFIG_USB
1668 // !!TB - USB Support
1669 if (strcmp(service, "usb") == 0) {
1670 if (action & A_STOP) stop_usb();
1671 if (action & A_START) {
1672 start_usb();
1673 // restart Samba and ftp since they may be killed by stop_usb()
1674 restart_nas_services(0, 1);
1676 goto CLEAR;
1678 #endif
1680 #ifdef TCONFIG_FTP
1681 // !!TB - FTP Server
1682 if (strcmp(service, "ftpd") == 0) {
1683 if (action & A_STOP) stop_ftpd();
1684 setup_conntrack();
1685 stop_firewall();
1686 start_firewall();
1687 if (action & A_START) start_ftpd();
1688 goto CLEAR;
1690 #endif
1692 #ifdef TCONFIG_SAMBASRV
1693 // !!TB - Samba
1694 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
1695 if (action & A_STOP) stop_samba();
1696 if (action & A_START) {
1697 create_passwd();
1698 stop_dnsmasq();
1699 start_dnsmasq();
1700 start_samba();
1702 goto CLEAR;
1704 #endif
1706 #ifdef TCONFIG_OPENVPN
1707 if (strncmp(service, "vpnclient", 9) == 0) {
1708 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
1709 if (action & A_START) start_vpnclient(atoi(&service[9]));
1710 goto CLEAR;
1713 if (strncmp(service, "vpnserver", 9) == 0) {
1714 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
1715 if (action & A_START) start_vpnserver(atoi(&service[9]));
1716 goto CLEAR;
1718 #endif
1720 CLEAR:
1721 if (next) goto TOP;
1723 // some functions check action_service and must be cleared at end -- zzz
1724 nvram_set("action_service", "");
1727 static void do_service(const char *name, const char *action, int user)
1729 int n;
1730 char s[64];
1732 n = 15;
1733 while (!nvram_match("action_service", "")) {
1734 if (user) {
1735 putchar('*');
1736 fflush(stdout);
1738 else if (--n < 0) break;
1739 sleep(1);
1742 snprintf(s, sizeof(s), "%s-%s", name, action);
1743 nvram_set("action_service", s);
1744 kill(1, SIGUSR1);
1746 n = 15;
1747 while (nvram_match("action_service", s)) {
1748 if (user) {
1749 putchar('.');
1750 fflush(stdout);
1752 else if (--n < 0) {
1753 break;
1755 sleep(1);
1759 int service_main(int argc, char *argv[])
1761 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1762 do_service(argv[1], argv[2], 1);
1763 printf("\nDone.\n");
1764 return 0;
1767 void start_service(const char *name)
1769 do_service(name, "start", 0);
1772 void stop_service(const char *name)
1774 do_service(name, "stop", 0);
1778 void restart_service(const char *name)
1780 do_service(name, "restart", 0);