Merge branch 'tomato-ND-usbmod-base' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / services.c
blobc3f24dd225c61da197dacdb3d7198dbbac20a849
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 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
109 else n = 4096;
110 fprintf(f,
111 "resolv-file=%s\n" // the real stuff is here
112 "addn-hosts=%s\n" // "
113 "expand-hosts\n" // expand hostnames in hosts file
114 "min-port=%u\n", // min port used for random src port
115 dmresolv, dmhosts, n);
116 do_dns = nvram_match("dhcpd_dmdns", "1");
119 // dhcp
120 do_dhcpd = nvram_match("lan_proto", "dhcp");
121 if (do_dhcpd) {
122 dhcp_lease = nvram_get_int("dhcp_lease");
123 if (dhcp_lease <= 0) dhcp_lease = 1440;
125 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
126 if (n < 0) strcpy(sdhcp_lease, "infinite");
127 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
129 if (!do_dns) {
130 // if not using dnsmasq for dns
132 const dns_list_t *dns = get_dns(); // this always points to a static buffer
133 if ((dns->count == 0) && (nvram_match("dhcpd_llndns", "1"))) {
134 // no DNS might be temporary. use a low lease time to force clients to update.
135 dhcp_lease = 2;
136 strcpy(sdhcp_lease, "2m");
137 do_dns = 1;
139 else {
140 // pass the dns directly
141 buf[0] = 0;
142 for (n = 0 ; n < dns->count; ++n) {
143 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n]));
145 fprintf(f, "dhcp-option=6%s\n", buf);
149 if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
150 fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
152 else {
153 // for compatibility
154 dhcp_start = nvram_get_int("dhcp_start");
155 dhcp_count = nvram_get_int("dhcp_num");
156 fprintf(f, "dhcp-range=%s%d,%s%d,%s,%dm\n",
157 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
159 n = nvram_get_int("dhcpd_lmax");
160 fprintf(f,
161 "dhcp-option=3,%s\n" // gateway
162 "dhcp-lease-max=%d\n",
163 router_ip,
164 (n > 0) ? n : 255);
166 if (nvram_get_int("dhcpd_auth") >= 0) {
167 fprintf(f, "dhcp-authoritative\n");
170 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
171 fprintf(f, "dhcp-option=44,%s\n", nv);
173 #ifdef TCONFIG_SAMBASRV
174 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
175 if ((nv == NULL) || (*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
176 // Samba will serve as a WINS server
177 fprintf(f, "dhcp-option=44,0.0.0.0\n");
180 #endif
182 else {
183 fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
186 // write static lease entries & create hosts file
188 if ((hf = fopen(dmhosts, "w")) != NULL) {
189 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
190 fprintf(hf, "%s %s\n", router_ip, nv);
191 #ifdef TCONFIG_SAMBASRV
192 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
193 fprintf(hf, "%s %s\n", router_ip, nv);
194 #endif
197 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
198 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
199 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
200 p = nvram_safe_get("dhcpd_static");
201 while ((e = strchr(p, '>')) != NULL) {
202 n = (e - p);
203 if (n > 105) {
204 p = e + 1;
205 continue;
208 strncpy(buf, p, n);
209 buf[n] = 0;
210 p = e + 1;
212 if ((e = strchr(buf, '<')) == NULL) continue;
213 *e = 0;
214 mac = buf;
216 ip = e + 1;
217 if ((e = strchr(ip, '<')) == NULL) continue;
218 *e = 0;
219 if (strchr(ip, '.') == NULL) {
220 ipn = atoi(ip);
221 if ((ipn <= 0) || (ipn > 255)) continue;
222 sprintf(ipbuf, "%s%d", lan, ipn);
223 ip = ipbuf;
225 else {
226 if (inet_addr(ip) == INADDR_NONE) continue;
229 name = e + 1;
231 if ((hf) && (*name != 0)) {
232 fprintf(hf, "%s %s\n", ip, name);
235 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
236 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
240 if (hf) fclose(hf);
244 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
246 fappend(f, "/etc/dnsmasq.custom");
250 fclose(f);
252 if (do_dns) {
253 unlink("/etc/resolv.conf");
254 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
257 TRACE_PT("run dnsmasq\n");
259 eval("dnsmasq");
261 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
262 f_read_string(dmpid, buf, sizeof(buf));
263 pid_dnsmasq = atol(buf);
266 TRACE_PT("end\n");
269 void stop_dnsmasq(void)
271 TRACE_PT("begin\n");
273 if (getpid() != 1) {
274 stop_service("dnsmasq");
275 return;
278 pid_dnsmasq = -1;
280 unlink("/etc/resolv.conf");
281 symlink(dmresolv, "/etc/resolv.conf");
283 killall_tk("dnsmasq");
285 TRACE_PT("end\n");
288 void clear_resolv(void)
290 _dprintf("%s\n", __FUNCTION__);
292 f_write(dmresolv, NULL, 0, 0, 0); // blank
295 void dns_to_resolv(void)
297 FILE *f;
298 const dns_list_t *dns;
299 int i;
300 mode_t m;
302 _dprintf("%s\n", __FUNCTION__);
304 m = umask(022); // 077 from pppoecd
305 if ((f = fopen(dmresolv, "w")) != NULL) {
306 dns = get_dns(); // static buffer
307 if (dns->count == 0) {
308 // Put a pseudo DNS IP to trigger Connect On Demand
309 if ((nvram_match("ppp_demand", "1")) &&
310 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
311 fprintf(f, "nameserver 1.1.1.1\n");
314 else {
315 for (i = 0; i < dns->count; i++) {
316 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i]));
319 fclose(f);
321 umask(m);
324 // -----------------------------------------------------------------------------
326 void start_httpd(void)
328 chdir("/www");
329 if (!nvram_match("http_enable", "0")) {
330 xstart("httpd");
332 if (!nvram_match("https_enable", "0")) {
333 xstart("httpd", "-s");
335 chdir("/");
338 void stop_httpd(void)
340 killall_tk("httpd");
343 // -----------------------------------------------------------------------------
345 void start_upnp(void)
347 if (get_wan_proto() == WP_DISABLED) return;
349 #ifdef USE_MINIUPNPD
350 int enable;
351 FILE *f;
352 int upnp_port;
354 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
355 mkdir("/etc/upnp", 0777);
356 if (f_exists("/etc/upnp/config.alt")) {
357 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
359 else {
360 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
361 upnp_port = nvram_get_int("upnp_port");
362 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
364 char *lanip = nvram_safe_get("lan_ipaddr");
365 char *lanmask = nvram_safe_get("lan_netmask");
367 fprintf(f,
368 "ext_ifname=%s\n"
369 "listening_ip=%s/%s\n"
370 "port=%d\n"
371 "enable_upnp=%s\n"
372 "enable_natpmp=%s\n"
373 "secure_mode=%s\n"
374 "upnp_forward_chain=upnp\n"
375 "upnp_nat_chain=upnp\n"
376 "notify_interval=%d\n"
377 "system_uptime=yes\n"
378 "\n"
380 nvram_safe_get("wan_iface"),
381 lanip, lanmask,
382 upnp_port,
383 (enable & 1) ? "yes" : "no", // upnp enable
384 (enable & 2) ? "yes" : "no", // natpmp enable
385 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
386 nvram_get_int("upnp_ssdp_interval")
389 if (nvram_get_int("upnp_clean")) {
390 int interval = nvram_get_int("upnp_clean_interval");
391 if (interval < 60) interval = 60;
392 fprintf(f,
393 "clean_ruleset_interval=%d\n"
394 "clean_ruleset_threshold=%d\n",
395 interval,
396 nvram_get_int("upnp_clean_threshold")
399 else
400 fprintf(f,"clean_ruleset_interval=0\n");
402 if (nvram_match("upnp_mnp", "1")) {
403 int https = nvram_get_int("https_enable");
404 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
405 https ? "s" : "", lanip,
406 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
408 else {
409 // Empty parameters are not included into XML service description
410 fprintf(f, "presentation_url=\n");
413 char uuid[45];
414 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
415 fprintf(f, "uuid=%s\n", uuid);
417 int ports[4];
418 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
419 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
420 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
421 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
422 fprintf(f,
423 "allow %d-%d %s/%s %d-%d\n",
424 ports[0], ports[1],
425 lanip, lanmask,
426 ports[2], ports[3]
429 else {
430 // by default allow only redirection of ports above 1024
431 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
434 fappend(f, "/etc/upnp/config.custom");
435 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
436 fclose(f);
438 xstart("miniupnpd", "-f", "/etc/upnp/config");
442 #else
443 if (nvram_get_int("upnp_enable")) {
444 xstart("upnp",
445 "-D",
446 "-L", nvram_safe_get("lan_ifname"),
447 "-W", nvram_safe_get("wan_iface"),
448 "-I", nvram_safe_get("upnp_ssdp_interval"),
449 "-A", nvram_safe_get("upnp_max_age"));
451 #endif
454 void stop_upnp(void)
456 #ifdef USE_MINIUPNPD
457 killall_tk("miniupnpd");
458 #else
459 killall_tk("upnp");
460 #endif
463 // -----------------------------------------------------------------------------
465 static pid_t pid_crond = -1;
467 void start_cron(void)
469 char *argv[] = { "crond", "-l", "9", NULL };
471 stop_cron();
473 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
474 _eval(argv, NULL, 0, NULL);
475 if (!nvram_contains_word("debug_norestart", "crond")) {
476 pid_crond = -2;
481 void stop_cron(void)
483 pid_crond = -1;
484 killall_tk("crond");
487 // -----------------------------------------------------------------------------
489 // Written by Sparq in 2002/07/16
490 void start_zebra(void)
492 #ifdef TCONFIG_ZEBRA
493 FILE *fp;
495 char *lan_tx = nvram_safe_get("dr_lan_tx");
496 char *lan_rx = nvram_safe_get("dr_lan_rx");
497 char *wan_tx = nvram_safe_get("dr_wan_tx");
498 char *wan_rx = nvram_safe_get("dr_wan_rx");
500 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
501 return;
504 // empty
505 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
506 fclose(fp);
510 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
511 char *lan_ifname = nvram_safe_get("lan_ifname");
512 char *wan_ifname = nvram_safe_get("wan_ifname");
514 fprintf(fp, "router rip\n");
515 fprintf(fp, "network %s\n", lan_ifname);
516 fprintf(fp, "network %s\n", wan_ifname);
517 fprintf(fp, "redistribute connected\n");
518 //fprintf(fp, "redistribute static\n");
520 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
521 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
523 fprintf(fp, "interface %s\n", lan_ifname);
524 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
525 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
527 fprintf(fp, "interface %s\n", wan_ifname);
528 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
529 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
531 fprintf(fp, "router rip\n");
532 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
533 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
534 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
535 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
536 fprintf(fp, "access-list private deny any\n");
538 //fprintf(fp, "debug rip events\n");
539 //fprintf(fp, "log file /etc/ripd.log\n");
540 fclose(fp);
543 xstart("zebra", "-d");
544 xstart("ripd", "-d");
545 #endif
548 void stop_zebra(void)
550 #ifdef TCONFIG_ZEBRA
551 killall("zebra", SIGTERM);
552 killall("ripd", SIGTERM);
554 unlink("/etc/zebra.conf");
555 unlink("/etc/ripd.conf");
556 #endif
559 // -----------------------------------------------------------------------------
561 void start_syslog(void)
563 #if 1
564 char *argv[12];
565 int argc;
566 char *nv;
567 char rem[256];
568 int n;
569 char s[64];
571 argv[0] = "syslogd";
572 argc = 1;
574 if (nvram_match("log_remote", "1")) {
575 nv = nvram_safe_get("log_remoteip");
576 if (*nv) {
577 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
578 argv[argc++] = "-R";
579 argv[argc++] = rem;
583 if (nvram_match("log_file", "1")) {
584 argv[argc++] = "-L";
585 argv[argc++] = "-s";
586 argv[argc++] = "50";
589 if (argc > 1) {
590 argv[argc] = NULL;
591 _eval(argv, NULL, 0, NULL);
592 usleep(500000);
594 argv[0] = "klogd";
595 argv[1] = NULL;
596 _eval(argv, NULL, 0, NULL);
597 usleep(500000);
599 // used to be available in syslogd -m
600 n = nvram_get_int("log_mark");
601 if (n > 0) {
602 sprintf(s, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
603 (n < 60) ? "*/30" : "0", (n < 120) ? "*" : "*/2");
604 system(s);
606 else {
607 system("cru d syslogdmark");
611 #else
612 char *argv[12];
613 int argc;
614 char *nv;
615 char rem[256];
617 argv[0] = "syslogd";
618 argv[1] = "-m";
619 argv[2] = nvram_get("log_mark");
620 argc = 3;
622 if (nvram_match("log_remote", "1")) {
623 nv = nvram_safe_get("log_remoteip");
624 if (*nv) {
625 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
626 argv[argc++] = "-R";
627 argv[argc++] = rem;
631 if (nvram_match("log_file", "1")) {
632 argv[argc++] = "-L";
633 argv[argc++] = "-s";
634 argv[argc++] = "50";
637 if (argc > 3) {
638 argv[argc] = NULL;
639 _eval(argv, NULL, 0, NULL);
640 usleep(500000);
642 argv[0] = "klogd";
643 argv[1] = NULL;
644 _eval(argv, NULL, 0, NULL);
645 usleep(500000);
647 #endif
650 void stop_syslog(void)
652 killall("klogd", SIGTERM);
653 killall("syslogd", SIGTERM);
656 // -----------------------------------------------------------------------------
658 static pid_t pid_igmp = -1;
660 void start_igmp_proxy(void)
662 FILE *fp;
663 char *p;
665 pid_igmp = -1;
666 if (nvram_match("multicast_pass", "1")) {
667 switch (get_wan_proto()) {
668 case WP_DISABLED:
669 return;
670 case WP_PPPOE:
671 case WP_PPTP:
672 case WP_L2TP:
673 p = "wan_iface";
674 break;
675 default:
676 p = "wan_ifname";
677 break;
680 if (f_exists("/etc/igmp.alt")) {
681 xstart("igmpproxy", "/etc/igmp.alt");
683 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
684 fprintf(fp,
685 "quickleave\n"
686 "phyint %s upstream\n"
687 "\taltnet %s\n"
688 "phyint %s downstream ratelimit 0\n",
689 nvram_safe_get(p),
690 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
691 nvram_safe_get("lan_ifname"));
692 fclose(fp);
693 xstart("igmpproxy", "/etc/igmp.conf");
695 else {
696 return;
698 if (!nvram_contains_word("debug_norestart", "igmprt")) {
699 pid_igmp = -2;
704 void stop_igmp_proxy(void)
706 pid_igmp = -1;
707 killall("igmpproxy", SIGTERM);
711 // -----------------------------------------------------------------------------
713 void set_tz(void)
715 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
718 void start_ntpc(void)
720 set_tz();
722 stop_ntpc();
724 if (nvram_get_int("ntp_updates") >= 0) {
725 xstart("ntpsync", "--init");
729 void stop_ntpc(void)
731 killall("ntpsync", SIGTERM);
734 // -----------------------------------------------------------------------------
736 static void stop_rstats(void)
738 int n;
739 int pid;
741 n = 60;
742 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
743 if (kill(pid, SIGTERM) != 0) break;
744 sleep(1);
748 static void start_rstats(int new)
750 if (nvram_match("rstats_enable", "1")) {
751 stop_rstats();
752 if (new) xstart("rstats", "--new");
753 else xstart("rstats");
757 // -----------------------------------------------------------------------------
759 // !!TB - FTP Server
761 #ifdef TCONFIG_USB
763 * Return non-zero if we created the directory,
764 * and zero if it already existed.
766 int mkdir_if_none(char *dir)
768 DIR *dp;
769 if (!(dp=opendir(dir))) {
770 umask(0000);
771 mkdir(dir, 0777);
772 return 1;
774 closedir(dp);
775 return 0;
778 char *get_full_storage_path(char *val)
780 static char buf[128];
781 int len;
783 if (val[0] == '/')
784 len = sprintf(buf, "%s", val);
785 else
786 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
788 if (len > 1 && buf[len - 1] == '/')
789 buf[len - 1] = 0;
791 return buf;
794 char *nvram_storage_path(char *var)
796 char *val = nvram_safe_get(var);
797 return get_full_storage_path(val);
799 #endif // TCONFIG_USB
801 #ifdef TCONFIG_FTP
803 char vsftpd_conf[] = "/etc/vsftpd.conf";
804 char vsftpd_users[] = "/etc/vsftpd.users";
805 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
806 #endif
808 #ifdef TCONFIG_FTP
809 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
810 static void do_start_stop_ftpd(int stop, int start)
812 if (stop) killall("vsftpd", SIGTERM);
814 char tmp[256];
815 FILE *fp, *f;
817 if (!start || !nvram_get_int("ftp_enable")) return;
819 mkdir_if_none(vsftpd_users);
820 mkdir_if_none("/var/run/vsftpd");
822 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
823 return;
825 if (nvram_get_int("ftp_super"))
827 /* rights */
828 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
829 if ((f = fopen(tmp, "w")))
831 fprintf(f,
832 "dirlist_enable=yes\n"
833 "write_enable=yes\n"
834 "download_enable=yes\n");
835 fclose(f);
839 #ifdef TCONFIG_SAMBASRV
840 if (nvram_match("smbd_cset", "utf8"))
841 fprintf(fp, "utf8=yes\n");
842 #endif
844 if (nvram_invmatch("ftp_anonymous", "0"))
846 fprintf(fp,
847 "anon_allow_writable_root=yes\n"
848 "anon_world_readable_only=no\n"
849 "anon_umask=022\n");
851 /* rights */
852 sprintf(tmp, "%s/ftp", vsftpd_users);
853 if ((f = fopen(tmp, "w")))
855 if (nvram_match("ftp_dirlist", "0"))
856 fprintf(f, "dirlist_enable=yes\n");
857 if (nvram_match("ftp_anonymous", "1") ||
858 nvram_match("ftp_anonymous", "3"))
859 fprintf(f, "write_enable=yes\n");
860 if (nvram_match("ftp_anonymous", "1") ||
861 nvram_match("ftp_anonymous", "2"))
862 fprintf(f, "download_enable=yes\n");
863 fclose(f);
865 if (nvram_match("ftp_anonymous", "1") ||
866 nvram_match("ftp_anonymous", "3"))
867 fprintf(fp,
868 "anon_upload_enable=yes\n"
869 "anon_mkdir_write_enable=yes\n"
870 "anon_other_write_enable=yes\n");
871 } else {
872 fprintf(fp, "anonymous_enable=no\n");
875 fprintf(fp,
876 "dirmessage_enable=yes\n"
877 "download_enable=no\n"
878 "dirlist_enable=no\n"
879 "hide_ids=yes\n"
880 "syslog_enable=yes\n"
881 "local_enable=yes\n"
882 "local_umask=022\n"
883 "chmod_enable=no\n"
884 "chroot_local_user=yes\n"
885 "check_shell=no\n"
886 "log_ftp_protocol=%s\n"
887 "user_config_dir=%s\n"
888 "passwd_file=%s\n"
889 "listen=yes\n"
890 "listen_port=%s\n"
891 "background=yes\n"
892 "max_clients=%d\n"
893 "max_per_ip=%d\n"
894 "idle_session_timeout=%s\n"
895 "use_sendfile=no\n"
896 "anon_max_rate=%d\n"
897 "local_max_rate=%d\n"
898 "%s\n",
899 nvram_get_int("log_ftp") ? "yes" : "no",
900 vsftpd_users, vsftpd_passwd,
901 nvram_get("ftp_port") ? : "21",
902 nvram_get_int("ftp_max"),
903 nvram_get_int("ftp_ipmax"),
904 nvram_get("ftp_staytimeout") ? : "300",
905 nvram_get_int("ftp_anonrate") * 1024,
906 nvram_get_int("ftp_rate") * 1024,
907 nvram_safe_get("ftp_custom"));
909 fclose(fp);
911 /* prepare passwd file and default users */
912 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
913 return;
915 fprintf(fp, /* anonymous, admin, nobody */
916 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
917 "%s:%s:0:0:root:/:/sbin/nologin\n"
918 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
919 nvram_storage_path("ftp_anonroot"), "admin",
920 nvram_get_int("ftp_super") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
921 MOUNT_ROOT);
923 char *buf;
924 char *p, *q;
925 char *user, *pass, *rights;
927 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
930 username<password<rights
931 rights:
932 Read/Write
933 Read Only
934 View Only
935 Private
937 p = buf;
938 while ((q = strsep(&p, ">")) != NULL) {
939 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
940 if (!user || !pass) continue;
942 /* directory */
943 if (strncmp(rights, "Private", 7) == 0)
945 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
946 mkdir_if_none(tmp);
948 else
949 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
951 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
952 user, crypt(pass, "$1$"), user, tmp);
954 /* rights */
955 sprintf(tmp, "%s/%s", vsftpd_users, user);
956 if ((f = fopen(tmp, "w")))
958 tmp[0] = 0;
959 if (nvram_invmatch("ftp_dirlist", "1"))
960 strcat(tmp, "dirlist_enable=yes\n");
961 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
962 strcat(tmp, "download_enable=yes\n");
963 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
964 strcat(tmp, "write_enable=yes\n");
966 fputs(tmp, f);
967 fclose(f);
970 free(buf);
973 fclose(fp);
974 killall("vsftpd", SIGHUP);
976 /* start vsftpd if it's not already running */
977 if (pidof("vsftpd") <= 0)
978 eval("vsftpd");
980 #endif
982 void start_ftpd(void)
984 #ifdef TCONFIG_FTP
985 int fd = file_lock("usb");
986 do_start_stop_ftpd(0, 1);
987 file_unlock(fd);
988 #endif
991 void stop_ftpd(void)
993 #ifdef TCONFIG_FTP
994 int fd = file_lock("usb");
995 do_start_stop_ftpd(1, 0);
996 unlink(vsftpd_passwd);
997 unlink(vsftpd_conf);
998 eval("rm", "-rf", vsftpd_users);
999 file_unlock(fd);
1000 #endif
1003 // -----------------------------------------------------------------------------
1005 // !!TB - Samba
1007 #ifdef TCONFIG_SAMBASRV
1008 void kill_samba(int sig)
1010 killall("smbd", sig);
1011 killall("nmbd", sig);
1013 #endif
1015 #ifdef TCONFIG_SAMBASRV
1016 static void do_start_stop_samba(int stop, int start)
1018 if (stop) kill_samba(SIGTERM);
1020 FILE *fp;
1021 DIR *dir = NULL;
1022 struct dirent *dp;
1023 char nlsmod[15];
1024 int mode, master;
1025 char *nv;
1027 mode = nvram_get_int("smbd_enable");
1028 if (!start || !mode || !nvram_invmatch("lan_hostname", ""))
1029 return;
1031 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1032 return;
1034 fprintf(fp, "[global]\n"
1035 " interfaces = %s\n"
1036 " bind interfaces only = yes\n"
1037 " workgroup = %s\n"
1038 " server string = %s\n"
1039 " guest account = nobody\n"
1040 " security = %s\n"
1041 " browseable = yes\n"
1042 " guest ok = yes\n"
1043 " guest only = no\n"
1044 " log level = %d\n"
1045 " syslog only = yes\n"
1046 " syslog = 1\n"
1047 " encrypt passwords = yes\n"
1048 " preserve case = yes\n"
1049 " short preserve case = yes\n",
1050 nvram_safe_get("lan_ifname"),
1051 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1052 nvram_get("router_name") ? : "Tomato",
1053 mode == 2 ? "user" : "share",
1054 nvram_get_int("smbd_loglevel")
1057 if (nvram_get_int("smbd_wins")) {
1058 nv = nvram_safe_get("wan_wins");
1059 if ((*nv == 0) || (strcmp(nv, "0.0.0.0") == 0)) {
1060 fprintf(fp, " wins support = yes\n");
1064 if (nvram_get_int("smbd_master")) {
1065 fprintf(fp,
1066 " domain master = yes\n"
1067 " local master = yes\n"
1068 " preferred master = yes\n"
1069 " os level = 65\n");
1072 nv = nvram_safe_get("smbd_cpage");
1073 if (*nv) {
1074 fprintf(fp, " client code page = %s\n", nv);
1075 sprintf(nlsmod, "nls_cp%s", nv);
1077 nv = nvram_safe_get("smbd_nlsmod");
1078 if ((*nv) && (strcmp(nv, nlsmod) != 0))
1079 modprobe_r(nv);
1081 modprobe(nlsmod);
1082 nvram_set("smbd_nlsmod", nlsmod);
1085 if (nvram_match("smbd_cset", "utf8"))
1086 fprintf(fp, " coding system = utf8\n");
1087 else if (nvram_invmatch("smbd_cset", ""))
1088 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1090 fprintf(fp, "%s\n\n", nvram_safe_get("smbd_custom"));
1092 /* configure shares */
1094 char *buf;
1095 char *p, *q;
1096 char *name, *path, *comment, *writeable, *hidden;
1097 int cnt = 0;
1099 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1101 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1103 p = buf;
1104 while ((q = strsep(&p, ">")) != NULL) {
1105 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1106 if (!path || !name) continue;
1108 /* share name */
1109 fprintf(fp, "\n[%s]\n", name);
1111 /* path */
1112 fprintf(fp, " path = %s\n", path);
1114 /* access level */
1115 if (!strcmp(writeable, "1"))
1116 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1117 if (!strcmp(hidden, "1"))
1118 fprintf(fp, " browseable = no\n");
1120 /* comment */
1121 if (comment)
1122 fprintf(fp, " comment = %s\n", comment);
1124 cnt++;
1126 free(buf);
1129 /* share everything below MOUNT_ROOT */
1130 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1131 while ((dp = readdir(dir))) {
1132 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1134 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1135 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1136 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1137 if (nvram_match("smbd_autoshare", "3")) // Hidden
1138 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1139 dp->d_name, MOUNT_ROOT, dp->d_name);
1140 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1141 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1143 cnt++;
1147 if (dir) closedir(dir);
1149 if (cnt == 0) {
1150 /* by default share MOUNT_ROOT as read-only */
1151 fprintf(fp, "\n[share]\n"
1152 " path = %s\n"
1153 " writable = no\n",
1154 MOUNT_ROOT);
1157 fclose(fp);
1159 mkdir_if_none("/var/run/samba");
1160 mkdir_if_none("/etc/samba");
1162 /* write smbpasswd */
1163 eval("smbpasswd", "-a", "nobody", "\"\"");
1164 if (mode == 2) {
1165 char *smbd_user;
1166 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1167 smbd_user = "nas";
1168 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1171 kill_samba(SIGHUP);
1172 int ret1 = 0, ret2 = 0;
1173 /* start samba if it's not already running */
1174 if (pidof("nmbd") <= 0)
1175 ret1 = eval("nmbd", "-D");
1176 if (pidof("smbd") <= 0)
1177 ret2 = eval("smbd", "-D");
1179 if (ret1 || ret2) kill_samba(SIGTERM);
1181 #endif
1183 void start_samba(void)
1185 #ifdef TCONFIG_SAMBASRV
1186 int fd = file_lock("usb");
1187 do_start_stop_samba(0, 1);
1188 file_unlock(fd);
1189 #endif
1192 void stop_samba(void)
1194 #ifdef TCONFIG_SAMBASRV
1195 int fd = file_lock("usb");
1196 do_start_stop_samba(1, 0);
1197 sleep(2); /* wait for smbd to finish */
1199 if (nvram_invmatch("smbd_nlsmod", "")) {
1200 modprobe_r(nvram_get("smbd_nlsmod"));
1201 nvram_set("smbd_nlsmod", "");
1204 /* clean up */
1205 unlink("/var/log/smb");
1206 unlink("/var/log/nmb");
1207 eval("rm", "-rf", "/var/run/samba");
1208 file_unlock(fd);
1209 #endif
1212 #ifdef TCONFIG_USB
1213 void restart_nas_services(int start)
1215 /* restart all NAS applications */
1216 #if TCONFIG_SAMBASRV || TCONFIG_FTP
1217 int fd = file_lock("usb");
1218 #ifdef TCONFIG_SAMBASRV
1219 if (start && nvram_get_int("smbd_enable"))
1220 do_start_stop_samba(0, 1);
1221 else
1222 do_start_stop_samba(1, 0);
1223 #endif
1224 #ifdef TCONFIG_FTP
1225 if (start && nvram_get_int("ftp_enable"))
1226 do_start_stop_ftpd(0, 1);
1227 else
1228 do_start_stop_ftpd(1, 0);
1229 #endif
1230 file_unlock(fd);
1231 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1233 #endif // TCONFIG_USB
1235 // -----------------------------------------------------------------------------
1237 static void _check(pid_t *pid, const char *name, void (*func)(void) )
1239 if (*pid != -1) {
1240 if (kill(*pid, 0) != 0) {
1241 if ((*pid = pidof(name)) == -1) func();
1246 void check_services(void)
1248 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
1249 _check(&pid_crond, "crond", start_cron);
1250 _check(&pid_igmp, "igmpproxy", start_igmp_proxy);
1253 // -----------------------------------------------------------------------------
1255 void start_services(void)
1257 static int once = 1;
1259 if (once) {
1260 once = 0;
1262 create_passwd();
1263 if (nvram_get_int("telnetd_eas")) start_telnetd();
1264 if (nvram_get_int("sshd_eas")) start_sshd();
1267 start_syslog();
1268 #if TOMATO_SL
1269 start_usbevent();
1270 #endif
1271 start_nas();
1272 start_zebra();
1273 start_dnsmasq();
1274 start_cifs();
1275 start_httpd();
1276 start_cron();
1277 //start_upnp(); //!!TB - already in start_wan_done()
1278 start_rstats(0);
1279 start_sched();
1280 #ifdef TCONFIG_SAMBA
1281 start_smbd();
1282 #endif
1283 start_samba(); // !!TB - Samba
1284 start_ftpd(); // !!TB - FTP Server
1287 void stop_services(void)
1289 clear_resolv();
1291 stop_ftpd(); // !!TB - FTP Server
1292 stop_samba(); // !!TB - Samba
1293 #ifdef TCONFIG_SAMBA
1294 stop_smbd();
1295 #endif
1296 stop_sched();
1297 stop_rstats();
1298 //stop_upnp(); //!!TB - moved to stop_wan()
1299 stop_cron();
1300 stop_httpd();
1301 stop_cifs();
1302 stop_dnsmasq();
1303 stop_zebra();
1304 stop_nas();
1305 #if TOMATO_SL
1306 stop_usbevent();
1307 #endif
1308 stop_syslog();
1311 // -----------------------------------------------------------------------------
1313 void exec_service(void)
1315 const int A_START = 1;
1316 const int A_STOP = 2;
1317 const int A_RESTART = 1|2;
1318 char buffer[128];
1319 char *service;
1320 char *act;
1321 char *next;
1322 int action;
1323 int i;
1325 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1326 next = buffer;
1328 TOP:
1329 act = strsep(&next, ",");
1330 service = strsep(&act, "-");
1331 if (act == NULL) {
1332 next = NULL;
1333 goto CLEAR;
1336 TRACE_PT("service=%s action=%s\n", service, act);
1338 if (strcmp(act, "start") == 0) action = A_START;
1339 else if (strcmp(act, "stop") == 0) action = A_STOP;
1340 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1341 else action = 0;
1344 if (strcmp(service, "dhcpc") == 0) {
1345 if (action & A_STOP) stop_dhcpc();
1346 if (action & A_START) start_dhcpc();
1347 goto CLEAR;
1350 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1351 if (action & A_STOP) stop_dnsmasq();
1352 if (action & A_START) {
1353 dns_to_resolv();
1354 start_dnsmasq();
1356 goto CLEAR;
1359 if (strcmp(service, "firewall") == 0) {
1360 if (action & A_STOP) {
1361 stop_firewall();
1362 stop_igmp_proxy();
1364 if (action & A_START) {
1365 start_firewall();
1366 start_igmp_proxy();
1368 goto CLEAR;
1371 if (strcmp(service, "restrict") == 0) {
1372 if (action & A_STOP) {
1373 stop_firewall();
1375 if (action & A_START) {
1376 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1378 start_firewall();
1380 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1381 if (i == 1) {
1382 if (nvram_get_int("rrules_radio") < 0) {
1383 if (!get_radio()) eval("radio", "on");
1387 goto CLEAR;
1390 if (strcmp(service, "qos") == 0) {
1391 if (action & A_STOP) {
1392 stop_qos();
1394 stop_firewall(); start_firewall(); // always restarted
1395 if (action & A_START) {
1396 start_qos();
1397 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1399 goto CLEAR;
1402 if (strcmp(service, "upnp") == 0) {
1403 if (action & A_STOP) {
1404 stop_upnp();
1406 stop_firewall(); start_firewall(); // always restarted
1407 if (action & A_START) {
1408 start_upnp();
1410 goto CLEAR;
1413 if (strcmp(service, "telnetd") == 0) {
1414 if (action & A_STOP) stop_telnetd();
1415 if (action & A_START) start_telnetd();
1416 goto CLEAR;
1419 if (strcmp(service, "sshd") == 0) {
1420 if (action & A_STOP) stop_sshd();
1421 if (action & A_START) start_sshd();
1422 goto CLEAR;
1425 if (strcmp(service, "httpd") == 0) {
1426 if (action & A_STOP) stop_httpd();
1427 if (action & A_START) start_httpd();
1428 goto CLEAR;
1431 if (strcmp(service, "admin") == 0) {
1432 if (action & A_STOP) {
1433 stop_sshd();
1434 stop_telnetd();
1435 stop_httpd();
1437 stop_firewall(); start_firewall(); // always restarted
1438 if (action & A_START) {
1439 start_httpd();
1440 create_passwd();
1441 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1442 if (nvram_match("sshd_eas", "1")) start_sshd();
1444 goto CLEAR;
1447 if (strcmp(service, "ddns") == 0) {
1448 if (action & A_STOP) stop_ddns();
1449 if (action & A_START) start_ddns();
1450 goto CLEAR;
1453 if (strcmp(service, "ntpc") == 0) {
1454 if (action & A_STOP) stop_ntpc();
1455 if (action & A_START) start_ntpc();
1456 goto CLEAR;
1459 if (strcmp(service, "logging") == 0) {
1460 if (action & A_STOP) {
1461 stop_syslog();
1462 stop_cron();
1464 stop_firewall(); start_firewall(); // always restarted
1465 if (action & A_START) {
1466 start_cron();
1467 start_syslog();
1469 goto CLEAR;
1472 if (strcmp(service, "crond") == 0) {
1473 if (action & A_STOP) {
1474 stop_cron();
1476 if (action & A_START) {
1477 start_cron();
1479 goto CLEAR;
1482 if (strcmp(service, "upgrade") == 0) {
1483 if (action & A_START) {
1484 #if TOMATO_SL
1485 stop_usbevent();
1486 stop_smbd();
1487 #endif
1488 stop_ftpd(); // !!TB - FTP Server
1489 stop_samba(); // !!TB - Samba
1490 stop_jffs2();
1491 // stop_cifs();
1492 stop_zebra();
1493 stop_cron();
1494 stop_ntpc();
1495 stop_upnp();
1496 // stop_dhcpc();
1497 killall("rstats", SIGTERM);
1498 killall("buttons", SIGTERM);
1499 stop_syslog();
1500 remove_storage_main(); // !!TB - USB Support
1501 stop_usb(); // !!TB - USB Support
1503 goto CLEAR;
1506 #ifdef TCONFIG_CIFS
1507 if (strcmp(service, "cifs") == 0) {
1508 if (action & A_STOP) stop_cifs();
1509 if (action & A_START) start_cifs();
1510 goto CLEAR;
1512 #endif
1514 #ifdef TCONFIG_JFFS2
1515 if (strcmp(service, "jffs2") == 0) {
1516 if (action & A_STOP) stop_jffs2();
1517 if (action & A_START) start_jffs2();
1518 goto CLEAR;
1520 #endif
1522 if (strcmp(service, "routing") == 0) {
1523 if (action & A_STOP) {
1524 stop_zebra();
1525 do_static_routes(0); // remove old '_saved'
1526 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1528 stop_firewall();
1529 start_firewall();
1530 if (action & A_START) {
1531 do_static_routes(1); // add new
1532 start_zebra();
1533 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1535 goto CLEAR;
1538 if (strcmp(service, "ctnf") == 0) {
1539 if (action & A_START) {
1540 setup_conntrack();
1541 stop_firewall();
1542 start_firewall();
1544 goto CLEAR;
1547 if (strcmp(service, "wan") == 0) {
1548 if (action & A_STOP) {
1549 if (get_wan_proto() == WP_PPPOE) {
1550 stop_dnsmasq();
1551 stop_redial();
1552 stop_singe_pppoe(PPPOE0);
1553 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
1554 sleep(1);
1555 start_pppoe(PPPOE0);
1557 start_dnsmasq();
1559 else {
1560 stop_wan();
1564 if (action & A_START) {
1565 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1567 if (get_wan_proto() == WP_PPPOE) {
1568 stop_singe_pppoe(PPPOE0);
1569 start_pppoe(PPPOE0);
1570 if (nvram_invmatch("ppp_demand", "1")) {
1571 start_redial();
1574 else {
1575 start_wan(BOOT);
1577 sleep(2);
1578 force_to_dial();
1580 goto CLEAR;
1583 if (strcmp(service, "net") == 0) {
1584 if (action & A_STOP) {
1585 stop_wan();
1586 stop_lan();
1587 stop_vlan();
1588 stop_nas();
1590 if (action & A_START) {
1591 start_vlan();
1592 start_lan();
1593 start_wan(BOOT);
1594 start_nas();
1596 goto CLEAR;
1599 if (strcmp(service, "rstats") == 0) {
1600 if (action & A_STOP) stop_rstats();
1601 if (action & A_START) start_rstats(0);
1602 goto CLEAR;
1605 if (strcmp(service, "rstatsnew") == 0) {
1606 if (action & A_STOP) stop_rstats();
1607 if (action & A_START) start_rstats(1);
1608 goto CLEAR;
1611 if (strcmp(service, "sched") == 0) {
1612 if (action & A_STOP) stop_sched();
1613 if (action & A_START) start_sched();
1614 goto CLEAR;
1617 #ifdef TCONFIG_USB
1618 // !!TB - USB Support
1619 if (strcmp(service, "usb") == 0) {
1620 if (action & A_STOP) stop_usb();
1621 if (action & A_START) {
1622 start_usb();
1623 // restart Samba and ftp since they may be killed by stop_usb()
1624 restart_nas_services(1);
1626 goto CLEAR;
1628 #endif
1630 #ifdef TCONFIG_FTP
1631 // !!TB - FTP Server
1632 if (strcmp(service, "ftpd") == 0) {
1633 if (action & A_STOP) stop_ftpd();
1634 setup_conntrack();
1635 stop_firewall();
1636 start_firewall();
1637 if (action & A_START) start_ftpd();
1638 goto CLEAR;
1640 #endif
1642 #ifdef TCONFIG_SAMBASRV
1643 // !!TB - Samba
1644 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
1645 if (action & A_STOP) stop_samba();
1646 if (action & A_START) {
1647 create_passwd();
1648 stop_dnsmasq();
1649 start_dnsmasq();
1650 start_samba();
1652 goto CLEAR;
1654 #endif
1656 CLEAR:
1657 if (next) goto TOP;
1659 // some functions check action_service and must be cleared at end -- zzz
1660 nvram_set("action_service", "");
1663 static void do_service(const char *name, const char *action, int user)
1665 int n;
1666 char s[64];
1668 n = 15;
1669 while (!nvram_match("action_service", "")) {
1670 if (user) {
1671 putchar('*');
1672 fflush(stdout);
1674 else if (--n < 0) break;
1675 sleep(1);
1678 snprintf(s, sizeof(s), "%s-%s", name, action);
1679 nvram_set("action_service", s);
1680 kill(1, SIGUSR1);
1682 n = 15;
1683 while (nvram_match("action_service", s)) {
1684 if (user) {
1685 putchar('.');
1686 fflush(stdout);
1688 else if (--n < 0) {
1689 break;
1691 sleep(1);
1695 int service_main(int argc, char *argv[])
1697 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1698 do_service(argv[1], argv[2], 1);
1699 printf("\nDone.\n");
1700 return 0;
1703 void start_service(const char *name)
1705 do_service(name, "start", 0);
1708 void stop_service(const char *name)
1710 do_service(name, "stop", 0);
1714 void restart_service(const char *name)
1716 do_service(name, "restart", 0);