Merge branch 'tomato-ND-usbmod-base' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / services.c
blob2dab32b7f2d0fe5589de0dcc9410b27ac7bfba66
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);
174 else {
175 fprintf(f, "no-dhcp-interface=%s\n", lan_ifname);
178 // write static lease entries & create hosts file
180 if ((hf = fopen(dmhosts, "w")) != NULL) {
181 if (((nv = nvram_get("wan_hostname")) != NULL) && (*nv))
182 fprintf(hf, "%s %s\n", router_ip, nv);
183 #ifdef TCONFIG_SAMBASRV
184 else if (((nv = nvram_get("lan_hostname")) != NULL) && (*nv))
185 fprintf(hf, "%s %s\n", router_ip, nv);
186 #endif
189 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
190 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
191 p = nvram_safe_get("dhcpd_static");
192 while ((e = strchr(p, '>')) != NULL) {
193 n = (e - p);
194 if (n > sizeof(buf)-1) {
195 p = e + 1;
196 continue;
199 strncpy(buf, p, n);
200 buf[n] = 0;
201 p = e + 1;
203 if ((e = strchr(buf, '<')) == NULL) continue;
204 *e = 0;
205 mac = buf;
207 ip = e + 1;
208 if ((e = strchr(ip, '<')) == NULL) continue;
209 *e = 0;
210 if (strchr(ip, '.') == NULL) {
211 ipn = atoi(ip);
212 if ((ipn <= 0) || (ipn > 255)) continue;
213 sprintf(ipbuf, "%s%d", lan, ipn);
214 ip = ipbuf;
216 else {
217 if (inet_addr(ip) == INADDR_NONE) continue;
220 name = e + 1;
222 if ((hf) && (*name != 0)) {
223 fprintf(hf, "%s %s\n", ip, name);
226 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
227 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
231 if (hf) fclose(hf);
235 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
237 fappend(f, "/etc/dnsmasq.custom");
241 fclose(f);
243 if (do_dns) {
244 unlink("/etc/resolv.conf");
245 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
248 TRACE_PT("run dnsmasq\n");
250 eval("dnsmasq");
252 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
253 f_read_string(dmpid, buf, sizeof(buf));
254 pid_dnsmasq = atol(buf);
257 TRACE_PT("end\n");
260 void stop_dnsmasq(void)
262 TRACE_PT("begin\n");
264 if (getpid() != 1) {
265 stop_service("dnsmasq");
266 return;
269 pid_dnsmasq = -1;
271 unlink("/etc/resolv.conf");
272 symlink(dmresolv, "/etc/resolv.conf");
274 killall_tk("dnsmasq");
276 TRACE_PT("end\n");
279 void clear_resolv(void)
281 _dprintf("%s\n", __FUNCTION__);
283 f_write(dmresolv, NULL, 0, 0, 0); // blank
286 void dns_to_resolv(void)
288 FILE *f;
289 const dns_list_t *dns;
290 int i;
291 mode_t m;
293 _dprintf("%s\n", __FUNCTION__);
295 m = umask(022); // 077 from pppoecd
296 if ((f = fopen(dmresolv, "w")) != NULL) {
297 dns = get_dns(); // static buffer
298 if (dns->count == 0) {
299 // Put a pseudo DNS IP to trigger Connect On Demand
300 if ((nvram_match("ppp_demand", "1")) &&
301 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
302 fprintf(f, "nameserver 1.1.1.1\n");
305 else {
306 for (i = 0; i < dns->count; i++) {
307 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i]));
310 fclose(f);
312 umask(m);
315 // -----------------------------------------------------------------------------
317 void start_httpd(void)
319 chdir("/www");
320 if (!nvram_match("http_enable", "0")) {
321 xstart("httpd");
323 if (!nvram_match("https_enable", "0")) {
324 xstart("httpd", "-s");
326 chdir("/");
329 void stop_httpd(void)
331 killall_tk("httpd");
334 // -----------------------------------------------------------------------------
336 void start_upnp(void)
338 if (get_wan_proto() == WP_DISABLED) return;
340 #ifdef USE_MINIUPNPD
341 int enable;
342 FILE *f;
343 int upnp_port;
345 if (((enable = nvram_get_int("upnp_enable")) & 3) != 0) {
346 mkdir("/etc/upnp", 0777);
347 if (f_exists("/etc/upnp/config.alt")) {
348 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
350 else {
351 if ((f = fopen("/etc/upnp/config", "w")) != NULL) {
352 upnp_port = nvram_get_int("upnp_port");
353 if ((upnp_port < 0) || (upnp_port >= 0xFFFF)) upnp_port = 0;
355 char *lanip = nvram_safe_get("lan_ipaddr");
356 char *lanmask = nvram_safe_get("lan_netmask");
358 fprintf(f,
359 "ext_ifname=%s\n"
360 "listening_ip=%s/%s\n"
361 "port=%d\n"
362 "enable_upnp=%s\n"
363 "enable_natpmp=%s\n"
364 "secure_mode=%s\n"
365 "upnp_forward_chain=upnp\n"
366 "upnp_nat_chain=upnp\n"
367 "notify_interval=%d\n"
368 "system_uptime=yes\n"
369 "\n"
371 nvram_safe_get("wan_iface"),
372 lanip, lanmask,
373 upnp_port,
374 (enable & 1) ? "yes" : "no", // upnp enable
375 (enable & 2) ? "yes" : "no", // natpmp enable
376 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
377 nvram_get_int("upnp_ssdp_interval")
380 if (nvram_get_int("upnp_clean")) {
381 int interval = nvram_get_int("upnp_clean_interval");
382 if (interval < 60) interval = 60;
383 fprintf(f,
384 "clean_ruleset_interval=%d\n"
385 "clean_ruleset_threshold=%d\n",
386 interval,
387 nvram_get_int("upnp_clean_threshold")
390 else
391 fprintf(f,"clean_ruleset_interval=0\n");
393 if (nvram_match("upnp_mnp", "1")) {
394 int https = nvram_get_int("https_enable");
395 fprintf(f, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
396 https ? "s" : "", lanip,
397 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
399 else {
400 // Empty parameters are not included into XML service description
401 fprintf(f, "presentation_url=\n");
404 char uuid[45];
405 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
406 fprintf(f, "uuid=%s\n", uuid);
408 int ports[4];
409 if ((ports[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
410 (ports[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
411 (ports[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
412 (ports[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
413 fprintf(f,
414 "allow %d-%d %s/%s %d-%d\n",
415 ports[0], ports[1],
416 lanip, lanmask,
417 ports[2], ports[3]
420 else {
421 // by default allow only redirection of ports above 1024
422 fprintf(f, "allow 1024-65535 %s/%s 1024-65535\n", lanip, lanmask);
425 fappend(f, "/etc/upnp/config.custom");
426 fprintf(f, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
427 fclose(f);
429 xstart("miniupnpd", "-f", "/etc/upnp/config");
433 #else
434 if (nvram_get_int("upnp_enable")) {
435 xstart("upnp",
436 "-D",
437 "-L", nvram_safe_get("lan_ifname"),
438 "-W", nvram_safe_get("wan_iface"),
439 "-I", nvram_safe_get("upnp_ssdp_interval"),
440 "-A", nvram_safe_get("upnp_max_age"));
442 #endif
445 void stop_upnp(void)
447 #ifdef USE_MINIUPNPD
448 killall_tk("miniupnpd");
449 #else
450 killall_tk("upnp");
451 #endif
454 // -----------------------------------------------------------------------------
456 static pid_t pid_crond = -1;
458 void start_cron(void)
460 char *argv[] = { "crond", "-l", "9", NULL };
462 stop_cron();
464 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
465 _eval(argv, NULL, 0, NULL);
466 if (!nvram_contains_word("debug_norestart", "crond")) {
467 pid_crond = -2;
472 void stop_cron(void)
474 pid_crond = -1;
475 killall_tk("crond");
478 // -----------------------------------------------------------------------------
480 // Written by Sparq in 2002/07/16
481 void start_zebra(void)
483 #ifdef TCONFIG_ZEBRA
484 FILE *fp;
486 char *lan_tx = nvram_safe_get("dr_lan_tx");
487 char *lan_rx = nvram_safe_get("dr_lan_rx");
488 char *wan_tx = nvram_safe_get("dr_wan_tx");
489 char *wan_rx = nvram_safe_get("dr_wan_rx");
491 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
492 return;
495 // empty
496 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
497 fclose(fp);
501 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
502 char *lan_ifname = nvram_safe_get("lan_ifname");
503 char *wan_ifname = nvram_safe_get("wan_ifname");
505 fprintf(fp, "router rip\n");
506 fprintf(fp, "network %s\n", lan_ifname);
507 fprintf(fp, "network %s\n", wan_ifname);
508 fprintf(fp, "redistribute connected\n");
509 //fprintf(fp, "redistribute static\n");
511 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
512 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
514 fprintf(fp, "interface %s\n", lan_ifname);
515 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
516 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
518 fprintf(fp, "interface %s\n", wan_ifname);
519 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
520 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
522 fprintf(fp, "router rip\n");
523 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
524 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
525 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
526 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
527 fprintf(fp, "access-list private deny any\n");
529 //fprintf(fp, "debug rip events\n");
530 //fprintf(fp, "log file /etc/ripd.log\n");
531 fclose(fp);
534 xstart("zebra", "-d");
535 xstart("ripd", "-d");
536 #endif
539 void stop_zebra(void)
541 #ifdef TCONFIG_ZEBRA
542 killall("zebra", SIGTERM);
543 killall("ripd", SIGTERM);
545 unlink("/etc/zebra.conf");
546 unlink("/etc/ripd.conf");
547 #endif
550 // -----------------------------------------------------------------------------
552 void start_syslog(void)
554 #if 1
555 char *argv[12];
556 int argc;
557 char *nv;
558 char rem[256];
559 int n;
560 char s[64];
562 argv[0] = "syslogd";
563 argc = 1;
565 if (nvram_match("log_remote", "1")) {
566 nv = nvram_safe_get("log_remoteip");
567 if (*nv) {
568 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
569 argv[argc++] = "-R";
570 argv[argc++] = rem;
574 if (nvram_match("log_file", "1")) {
575 argv[argc++] = "-L";
576 argv[argc++] = "-s";
577 argv[argc++] = "50";
580 if (argc > 1) {
581 argv[argc] = NULL;
582 _eval(argv, NULL, 0, NULL);
583 usleep(500000);
585 argv[0] = "klogd";
586 argv[1] = NULL;
587 _eval(argv, NULL, 0, NULL);
588 usleep(500000);
590 // used to be available in syslogd -m
591 n = nvram_get_int("log_mark");
592 if (n > 0) {
593 sprintf(s, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
594 (n < 60) ? "*/30" : "0", (n < 120) ? "*" : "*/2");
595 system(s);
597 else {
598 system("cru d syslogdmark");
602 #else
603 char *argv[12];
604 int argc;
605 char *nv;
606 char rem[256];
608 argv[0] = "syslogd";
609 argv[1] = "-m";
610 argv[2] = nvram_get("log_mark");
611 argc = 3;
613 if (nvram_match("log_remote", "1")) {
614 nv = nvram_safe_get("log_remoteip");
615 if (*nv) {
616 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
617 argv[argc++] = "-R";
618 argv[argc++] = rem;
622 if (nvram_match("log_file", "1")) {
623 argv[argc++] = "-L";
624 argv[argc++] = "-s";
625 argv[argc++] = "50";
628 if (argc > 3) {
629 argv[argc] = NULL;
630 _eval(argv, NULL, 0, NULL);
631 usleep(500000);
633 argv[0] = "klogd";
634 argv[1] = NULL;
635 _eval(argv, NULL, 0, NULL);
636 usleep(500000);
638 #endif
641 void stop_syslog(void)
643 killall("klogd", SIGTERM);
644 killall("syslogd", SIGTERM);
647 // -----------------------------------------------------------------------------
649 static pid_t pid_igmp = -1;
651 void start_igmp_proxy(void)
653 FILE *fp;
654 char *p;
656 pid_igmp = -1;
657 if (nvram_match("multicast_pass", "1")) {
658 switch (get_wan_proto()) {
659 case WP_DISABLED:
660 return;
661 case WP_PPPOE:
662 case WP_PPTP:
663 case WP_L2TP:
664 p = "wan_iface";
665 break;
666 default:
667 p = "wan_ifname";
668 break;
671 if (f_exists("/etc/igmp.alt")) {
672 xstart("igmpproxy", "/etc/igmp.alt");
674 else if ((fp = fopen("/etc/igmp.conf", "w")) != NULL) {
675 fprintf(fp,
676 "quickleave\n"
677 "phyint %s upstream\n"
678 "\taltnet %s\n"
679 "phyint %s downstream ratelimit 0\n",
680 nvram_safe_get(p),
681 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
682 nvram_safe_get("lan_ifname"));
683 fclose(fp);
684 xstart("igmpproxy", "/etc/igmp.conf");
686 else {
687 return;
689 if (!nvram_contains_word("debug_norestart", "igmprt")) {
690 pid_igmp = -2;
695 void stop_igmp_proxy(void)
697 pid_igmp = -1;
698 killall("igmpproxy", SIGTERM);
702 // -----------------------------------------------------------------------------
704 void set_tz(void)
706 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
709 void start_ntpc(void)
711 set_tz();
713 stop_ntpc();
715 if (nvram_get_int("ntp_updates") >= 0) {
716 xstart("ntpsync", "--init");
720 void stop_ntpc(void)
722 killall("ntpsync", SIGTERM);
725 // -----------------------------------------------------------------------------
727 static void stop_rstats(void)
729 int n;
730 int pid;
732 n = 60;
733 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
734 if (kill(pid, SIGTERM) != 0) break;
735 sleep(1);
739 static void start_rstats(int new)
741 if (nvram_match("rstats_enable", "1")) {
742 stop_rstats();
743 if (new) xstart("rstats", "--new");
744 else xstart("rstats");
748 // -----------------------------------------------------------------------------
750 // !!TB - FTP Server
752 #ifdef TCONFIG_USB
754 * Return non-zero if we created the directory,
755 * and zero if it already existed.
757 int mkdir_if_none(char *dir)
759 DIR *dp;
760 if (!(dp=opendir(dir))) {
761 umask(0000);
762 mkdir(dir, 0777);
763 return 1;
765 closedir(dp);
766 return 0;
769 char *get_full_storage_path(char *val)
771 static char buf[128];
772 int len;
774 if (val[0] == '/')
775 len = sprintf(buf, "%s", val);
776 else
777 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
779 if (len > 1 && buf[len - 1] == '/')
780 buf[len - 1] = 0;
782 return buf;
785 char *nvram_storage_path(char *var)
787 char *val = nvram_safe_get(var);
788 return get_full_storage_path(val);
790 #endif // TCONFIG_USB
792 #ifdef TCONFIG_FTP
794 char vsftpd_conf[] = "/etc/vsftpd.conf";
795 char vsftpd_users[] = "/etc/vsftpd.users";
796 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
797 #endif
799 #ifdef TCONFIG_FTP
800 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
801 static void do_start_stop_ftpd(int stop, int start)
803 if (stop) killall("vsftpd", SIGTERM);
805 char tmp[256];
806 FILE *fp, *f;
808 if (!start || !nvram_get_int("ftp_enable")) return;
810 mkdir_if_none(vsftpd_users);
811 mkdir_if_none("/var/run/vsftpd");
813 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
814 return;
816 if (nvram_get_int("ftp_super"))
818 /* rights */
819 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
820 if ((f = fopen(tmp, "w")))
822 fprintf(f,
823 "dirlist_enable=yes\n"
824 "write_enable=yes\n"
825 "download_enable=yes\n");
826 fclose(f);
830 #ifdef TCONFIG_SAMBASRV
831 if (nvram_match("smbd_cset", "utf8"))
832 fprintf(fp, "utf8=yes\n");
833 #endif
835 if (nvram_invmatch("ftp_anonymous", "0"))
837 fprintf(fp,
838 "anon_allow_writable_root=yes\n"
839 "anon_world_readable_only=no\n"
840 "anon_umask=022\n");
842 /* rights */
843 sprintf(tmp, "%s/ftp", vsftpd_users);
844 if ((f = fopen(tmp, "w")))
846 if (nvram_match("ftp_dirlist", "0"))
847 fprintf(f, "dirlist_enable=yes\n");
848 if (nvram_match("ftp_anonymous", "1") ||
849 nvram_match("ftp_anonymous", "3"))
850 fprintf(f, "write_enable=yes\n");
851 if (nvram_match("ftp_anonymous", "1") ||
852 nvram_match("ftp_anonymous", "2"))
853 fprintf(f, "download_enable=yes\n");
854 fclose(f);
856 if (nvram_match("ftp_anonymous", "1") ||
857 nvram_match("ftp_anonymous", "3"))
858 fprintf(fp,
859 "anon_upload_enable=yes\n"
860 "anon_mkdir_write_enable=yes\n"
861 "anon_other_write_enable=yes\n");
862 } else {
863 fprintf(fp, "anonymous_enable=no\n");
866 fprintf(fp,
867 "dirmessage_enable=yes\n"
868 "download_enable=no\n"
869 "dirlist_enable=no\n"
870 "hide_ids=yes\n"
871 "syslog_enable=yes\n"
872 "local_enable=yes\n"
873 "local_umask=022\n"
874 "chmod_enable=no\n"
875 "chroot_local_user=yes\n"
876 "check_shell=no\n"
877 "log_ftp_protocol=%s\n"
878 "user_config_dir=%s\n"
879 "passwd_file=%s\n"
880 "listen=yes\n"
881 "listen_port=%s\n"
882 "background=yes\n"
883 "max_clients=%d\n"
884 "max_per_ip=%d\n"
885 "idle_session_timeout=%s\n"
886 "use_sendfile=no\n"
887 "anon_max_rate=%d\n"
888 "local_max_rate=%d\n"
889 "%s\n",
890 nvram_get_int("log_ftp") ? "yes" : "no",
891 vsftpd_users, vsftpd_passwd,
892 nvram_get("ftp_port") ? : "21",
893 nvram_get_int("ftp_max"),
894 nvram_get_int("ftp_ipmax"),
895 nvram_get("ftp_staytimeout") ? : "300",
896 nvram_get_int("ftp_anonrate") * 1024,
897 nvram_get_int("ftp_rate") * 1024,
898 nvram_safe_get("ftp_custom"));
900 fclose(fp);
902 /* prepare passwd file and default users */
903 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
904 return;
906 fprintf(fp, /* anonymous, admin, nobody */
907 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
908 "%s:%s:0:0:root:/:/sbin/nologin\n"
909 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
910 nvram_storage_path("ftp_anonroot"), "admin",
911 nvram_get_int("ftp_super") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
912 MOUNT_ROOT);
914 char *buf;
915 char *p, *q;
916 char *user, *pass, *rights;
918 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
921 username<password<rights
922 rights:
923 Read/Write
924 Read Only
925 View Only
926 Private
928 p = buf;
929 while ((q = strsep(&p, ">")) != NULL) {
930 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
931 if (!user || !pass) continue;
933 /* directory */
934 if (strncmp(rights, "Private", 7) == 0)
936 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
937 mkdir_if_none(tmp);
939 else
940 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
942 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
943 user, crypt(pass, "$1$"), user, tmp);
945 /* rights */
946 sprintf(tmp, "%s/%s", vsftpd_users, user);
947 if ((f = fopen(tmp, "w")))
949 tmp[0] = 0;
950 if (nvram_invmatch("ftp_dirlist", "1"))
951 strcat(tmp, "dirlist_enable=yes\n");
952 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
953 strcat(tmp, "download_enable=yes\n");
954 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
955 strcat(tmp, "write_enable=yes\n");
957 fputs(tmp, f);
958 fclose(f);
961 free(buf);
964 fclose(fp);
965 killall("vsftpd", SIGHUP);
967 /* start vsftpd if it's not already running */
968 if (pidof("vsftpd") <= 0)
969 eval("vsftpd");
971 #endif
973 void start_ftpd(void)
975 #ifdef TCONFIG_FTP
976 int fd = file_lock("usb");
977 do_start_stop_ftpd(0, 1);
978 file_unlock(fd);
979 #endif
982 void stop_ftpd(void)
984 #ifdef TCONFIG_FTP
985 int fd = file_lock("usb");
986 do_start_stop_ftpd(1, 0);
987 unlink(vsftpd_passwd);
988 unlink(vsftpd_conf);
989 eval("rm", "-rf", vsftpd_users);
990 file_unlock(fd);
991 #endif
994 // -----------------------------------------------------------------------------
996 // !!TB - Samba
998 #ifdef TCONFIG_SAMBASRV
999 void kill_samba(int sig)
1001 killall("smbd", sig);
1002 killall("nmbd", sig);
1004 #endif
1006 #ifdef TCONFIG_SAMBASRV
1007 static void do_start_stop_samba(int stop, int start)
1009 if (stop) kill_samba(SIGTERM);
1011 FILE *fp;
1012 DIR *dir = NULL;
1013 struct dirent *dp;
1014 char nlsmod[15];
1015 int mode;
1017 mode = nvram_get_int("smbd_enable");
1018 if (!start || !mode || !nvram_invmatch("lan_hostname", ""))
1019 return;
1021 if ((fp = fopen("/etc/smb.conf", "w")) == NULL)
1022 return;
1024 fprintf(fp, "[global]\n"
1025 " interfaces = %s\n"
1026 " bind interfaces only = yes\n"
1027 " workgroup = %s\n"
1028 " server string = %s\n"
1029 " guest account = nobody\n"
1030 " security = %s\n"
1031 " browseable = yes\n"
1032 " guest ok = yes\n"
1033 " guest only = no\n"
1034 " log level = %d\n"
1035 " syslog only = yes\n"
1036 " syslog = 1\n"
1037 " encrypt passwords = yes\n"
1038 " preserve case = yes\n"
1039 " short preserve case = yes\n",
1040 nvram_safe_get("lan_ifname"),
1041 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1042 nvram_get("router_name") ? : "Tomato",
1043 mode == 2 ? "user" : "share",
1044 nvram_get_int("smbd_loglevel")
1047 if (nvram_invmatch("smbd_master", "")) {
1048 char *master = nvram_get_int("smbd_master") ? "yes" : "no";
1049 fprintf(fp,
1050 " local master = %s\n"
1051 " preferred master = %s\n",
1052 master, master);
1055 if (nvram_invmatch("smbd_cpage", "")) {
1056 char *cp = nvram_safe_get("smbd_cpage");
1058 fprintf(fp, " client code page = %s\n", cp);
1059 sprintf(nlsmod, "nls_cp%s", cp);
1061 cp = nvram_get("smbd_nlsmod");
1062 if ((cp) && (*cp != 0) && (strcmp(cp, nlsmod) != 0))
1063 modprobe_r(cp);
1065 modprobe(nlsmod);
1066 nvram_set("smbd_nlsmod", nlsmod);
1069 if (nvram_match("smbd_cset", "utf8"))
1070 fprintf(fp, " coding system = utf8\n");
1071 else if (nvram_invmatch("smbd_cset", ""))
1072 fprintf(fp, " character set = %s\n", nvram_safe_get("smbd_cset"));
1074 fprintf(fp, "%s\n\n", nvram_safe_get("smbd_custom"));
1076 /* configure shares */
1078 char *buf;
1079 char *p, *q;
1080 char *name, *path, *comment, *writeable, *hidden;
1081 int cnt = 0;
1083 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1085 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1087 p = buf;
1088 while ((q = strsep(&p, ">")) != NULL) {
1089 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1090 if (!path || !name) continue;
1092 /* share name */
1093 fprintf(fp, "\n[%s]\n", name);
1095 /* path */
1096 fprintf(fp, " path = %s\n", path);
1098 /* access level */
1099 if (!strcmp(writeable, "1"))
1100 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1101 if (!strcmp(hidden, "1"))
1102 fprintf(fp, " browseable = no\n");
1104 /* comment */
1105 if (comment)
1106 fprintf(fp, " comment = %s\n", comment);
1108 cnt++;
1110 free(buf);
1113 /* share everything below MOUNT_ROOT */
1114 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1115 while ((dp = readdir(dir))) {
1116 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1118 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1119 fprintf(fp, "\n[%s]\n path = %s/%s\n comment = %s\n",
1120 dp->d_name, MOUNT_ROOT, dp->d_name, dp->d_name);
1121 if (nvram_match("smbd_autoshare", "3")) // Hidden
1122 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1123 dp->d_name, MOUNT_ROOT, dp->d_name);
1124 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1125 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1127 cnt++;
1131 if (dir) closedir(dir);
1133 if (cnt == 0) {
1134 /* by default share MOUNT_ROOT as read-only */
1135 fprintf(fp, "\n[share]\n"
1136 " path = %s\n"
1137 " writable = no\n",
1138 MOUNT_ROOT);
1141 fclose(fp);
1143 mkdir_if_none("/var/run/samba");
1144 mkdir_if_none("/etc/samba");
1146 /* write smbpasswd */
1147 eval("smbpasswd", "-a", "nobody", "\"\"");
1148 if (mode == 2) {
1149 char *smbd_user;
1150 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1151 smbd_user = "nas";
1152 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1155 kill_samba(SIGHUP);
1156 int ret1 = 0, ret2 = 0;
1157 /* start samba if it's not already running */
1158 if (pidof("nmbd") <= 0)
1159 ret1 = eval("nmbd", "-D");
1160 if (pidof("smbd") <= 0)
1161 ret2 = eval("smbd", "-D");
1163 if (ret1 || ret2) kill_samba(SIGTERM);
1165 #endif
1167 void start_samba(void)
1169 #ifdef TCONFIG_SAMBASRV
1170 int fd = file_lock("usb");
1171 do_start_stop_samba(0, 1);
1172 file_unlock(fd);
1173 #endif
1176 void stop_samba(void)
1178 #ifdef TCONFIG_SAMBASRV
1179 int fd = file_lock("usb");
1180 do_start_stop_samba(1, 0);
1181 sleep(2); /* wait for smbd to finish */
1183 if (nvram_invmatch("smbd_nlsmod", "")) {
1184 modprobe_r(nvram_get("smbd_nlsmod"));
1185 nvram_set("smbd_nlsmod", "");
1188 /* clean up */
1189 unlink("/var/log/smb");
1190 unlink("/var/log/nmb");
1191 eval("rm", "-rf", "/var/run/samba");
1192 file_unlock(fd);
1193 #endif
1196 #ifdef TCONFIG_USB
1197 void restart_nas_services(int start)
1199 /* restart all NAS applications */
1200 #if TCONFIG_SAMBASRV || TCONFIG_FTP
1201 int fd = file_lock("usb");
1202 #ifdef TCONFIG_SAMBASRV
1203 if (start && nvram_get_int("smbd_enable"))
1204 do_start_stop_samba(0, 1);
1205 else
1206 do_start_stop_samba(1, 0);
1207 #endif
1208 #ifdef TCONFIG_FTP
1209 if (start && nvram_get_int("ftp_enable"))
1210 do_start_stop_ftpd(0, 1);
1211 else
1212 do_start_stop_ftpd(1, 0);
1213 #endif
1214 file_unlock(fd);
1215 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1217 #endif // TCONFIG_USB
1219 // -----------------------------------------------------------------------------
1221 static void _check(pid_t *pid, const char *name, void (*func)(void) )
1223 if (*pid != -1) {
1224 if (kill(*pid, 0) != 0) {
1225 if ((*pid = pidof(name)) == -1) func();
1230 void check_services(void)
1232 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
1233 _check(&pid_crond, "crond", start_cron);
1234 _check(&pid_igmp, "igmpproxy", start_igmp_proxy);
1237 // -----------------------------------------------------------------------------
1239 void start_services(void)
1241 static int once = 1;
1243 if (once) {
1244 once = 0;
1246 create_passwd();
1247 if (nvram_get_int("telnetd_eas")) start_telnetd();
1248 if (nvram_get_int("sshd_eas")) start_sshd();
1251 start_syslog();
1252 #if TOMATO_SL
1253 start_usbevent();
1254 #endif
1255 start_nas();
1256 start_zebra();
1257 start_dnsmasq();
1258 start_cifs();
1259 start_httpd();
1260 start_cron();
1261 //start_upnp(); //!!TB - already in start_wan_done()
1262 start_rstats(0);
1263 start_sched();
1264 #ifdef TCONFIG_SAMBA
1265 start_smbd();
1266 #endif
1267 start_samba(); // !!TB - Samba
1268 start_ftpd(); // !!TB - FTP Server
1271 void stop_services(void)
1273 clear_resolv();
1275 stop_ftpd(); // !!TB - FTP Server
1276 stop_samba(); // !!TB - Samba
1277 #ifdef TCONFIG_SAMBA
1278 stop_smbd();
1279 #endif
1280 stop_sched();
1281 stop_rstats();
1282 //stop_upnp(); //!!TB - moved to stop_wan()
1283 stop_cron();
1284 stop_httpd();
1285 stop_cifs();
1286 stop_dnsmasq();
1287 stop_zebra();
1288 stop_nas();
1289 #if TOMATO_SL
1290 stop_usbevent();
1291 #endif
1292 stop_syslog();
1295 // -----------------------------------------------------------------------------
1297 void exec_service(void)
1299 const int A_START = 1;
1300 const int A_STOP = 2;
1301 const int A_RESTART = 1|2;
1302 char buffer[128];
1303 char *service;
1304 char *act;
1305 char *next;
1306 int action;
1307 int i;
1309 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1310 next = buffer;
1312 TOP:
1313 act = strsep(&next, ",");
1314 service = strsep(&act, "-");
1315 if (act == NULL) {
1316 next = NULL;
1317 goto CLEAR;
1320 TRACE_PT("service=%s action=%s\n", service, act);
1322 if (strcmp(act, "start") == 0) action = A_START;
1323 else if (strcmp(act, "stop") == 0) action = A_STOP;
1324 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1325 else action = 0;
1328 if (strcmp(service, "dhcpc") == 0) {
1329 if (action & A_STOP) stop_dhcpc();
1330 if (action & A_START) start_dhcpc();
1331 goto CLEAR;
1334 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1335 if (action & A_STOP) stop_dnsmasq();
1336 if (action & A_START) {
1337 dns_to_resolv();
1338 start_dnsmasq();
1340 goto CLEAR;
1343 if (strcmp(service, "firewall") == 0) {
1344 if (action & A_STOP) {
1345 stop_firewall();
1346 stop_igmp_proxy();
1348 if (action & A_START) {
1349 start_firewall();
1350 start_igmp_proxy();
1352 goto CLEAR;
1355 if (strcmp(service, "restrict") == 0) {
1356 if (action & A_STOP) {
1357 stop_firewall();
1359 if (action & A_START) {
1360 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1362 start_firewall();
1364 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1365 if (i == 1) {
1366 if (nvram_get_int("rrules_radio") < 0) {
1367 if (!get_radio()) eval("radio", "on");
1371 goto CLEAR;
1374 if (strcmp(service, "qos") == 0) {
1375 if (action & A_STOP) {
1376 stop_qos();
1378 stop_firewall(); start_firewall(); // always restarted
1379 if (action & A_START) {
1380 start_qos();
1381 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1383 goto CLEAR;
1386 if (strcmp(service, "upnp") == 0) {
1387 if (action & A_STOP) {
1388 stop_upnp();
1390 stop_firewall(); start_firewall(); // always restarted
1391 if (action & A_START) {
1392 start_upnp();
1394 goto CLEAR;
1397 if (strcmp(service, "telnetd") == 0) {
1398 if (action & A_STOP) stop_telnetd();
1399 if (action & A_START) start_telnetd();
1400 goto CLEAR;
1403 if (strcmp(service, "sshd") == 0) {
1404 if (action & A_STOP) stop_sshd();
1405 if (action & A_START) start_sshd();
1406 goto CLEAR;
1409 if (strcmp(service, "httpd") == 0) {
1410 if (action & A_STOP) stop_httpd();
1411 if (action & A_START) start_httpd();
1412 goto CLEAR;
1415 if (strcmp(service, "admin") == 0) {
1416 if (action & A_STOP) {
1417 stop_sshd();
1418 stop_telnetd();
1419 stop_httpd();
1421 stop_firewall(); start_firewall(); // always restarted
1422 if (action & A_START) {
1423 start_httpd();
1424 create_passwd();
1425 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1426 if (nvram_match("sshd_eas", "1")) start_sshd();
1428 goto CLEAR;
1431 if (strcmp(service, "ddns") == 0) {
1432 if (action & A_STOP) stop_ddns();
1433 if (action & A_START) start_ddns();
1434 goto CLEAR;
1437 if (strcmp(service, "ntpc") == 0) {
1438 if (action & A_STOP) stop_ntpc();
1439 if (action & A_START) start_ntpc();
1440 goto CLEAR;
1443 if (strcmp(service, "logging") == 0) {
1444 if (action & A_STOP) {
1445 stop_syslog();
1446 stop_cron();
1448 stop_firewall(); start_firewall(); // always restarted
1449 if (action & A_START) {
1450 start_cron();
1451 start_syslog();
1453 goto CLEAR;
1456 if (strcmp(service, "crond") == 0) {
1457 if (action & A_STOP) {
1458 stop_cron();
1460 if (action & A_START) {
1461 start_cron();
1463 goto CLEAR;
1466 if (strcmp(service, "upgrade") == 0) {
1467 if (action & A_START) {
1468 #if TOMATO_SL
1469 stop_usbevent();
1470 stop_smbd();
1471 #endif
1472 stop_ftpd(); // !!TB - FTP Server
1473 stop_samba(); // !!TB - Samba
1474 stop_jffs2();
1475 // stop_cifs();
1476 stop_zebra();
1477 stop_cron();
1478 stop_ntpc();
1479 stop_upnp();
1480 // stop_dhcpc();
1481 killall("rstats", SIGTERM);
1482 killall("buttons", SIGTERM);
1483 stop_syslog();
1484 remove_storage_main(); // !!TB - USB Support
1485 stop_usb(); // !!TB - USB Support
1487 goto CLEAR;
1490 #ifdef TCONFIG_CIFS
1491 if (strcmp(service, "cifs") == 0) {
1492 if (action & A_STOP) stop_cifs();
1493 if (action & A_START) start_cifs();
1494 goto CLEAR;
1496 #endif
1498 #ifdef TCONFIG_JFFS2
1499 if (strcmp(service, "jffs2") == 0) {
1500 if (action & A_STOP) stop_jffs2();
1501 if (action & A_START) start_jffs2();
1502 goto CLEAR;
1504 #endif
1506 if (strcmp(service, "routing") == 0) {
1507 if (action & A_STOP) {
1508 stop_zebra();
1509 do_static_routes(0); // remove old '_saved'
1510 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1512 stop_firewall();
1513 start_firewall();
1514 if (action & A_START) {
1515 do_static_routes(1); // add new
1516 start_zebra();
1517 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1519 goto CLEAR;
1522 if (strcmp(service, "ctnf") == 0) {
1523 if (action & A_START) {
1524 setup_conntrack();
1525 stop_firewall();
1526 start_firewall();
1528 goto CLEAR;
1531 if (strcmp(service, "wan") == 0) {
1532 if (action & A_STOP) {
1533 if (get_wan_proto() == WP_PPPOE) {
1534 stop_dnsmasq();
1535 stop_redial();
1536 stop_singe_pppoe(PPPOE0);
1537 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
1538 sleep(1);
1539 start_pppoe(PPPOE0);
1541 start_dnsmasq();
1543 else {
1544 stop_wan();
1548 if (action & A_START) {
1549 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1551 if (get_wan_proto() == WP_PPPOE) {
1552 stop_singe_pppoe(PPPOE0);
1553 start_pppoe(PPPOE0);
1554 if (nvram_invmatch("ppp_demand", "1")) {
1555 start_redial();
1558 else {
1559 start_wan(BOOT);
1561 sleep(2);
1562 force_to_dial();
1564 goto CLEAR;
1567 if (strcmp(service, "net") == 0) {
1568 if (action & A_STOP) {
1569 stop_wan();
1570 stop_lan();
1571 stop_vlan();
1573 if (action & A_START) {
1574 start_vlan();
1575 start_lan();
1576 start_wan(BOOT);
1578 goto CLEAR;
1581 if (strcmp(service, "rstats") == 0) {
1582 if (action & A_STOP) stop_rstats();
1583 if (action & A_START) start_rstats(0);
1584 goto CLEAR;
1587 if (strcmp(service, "rstatsnew") == 0) {
1588 if (action & A_STOP) stop_rstats();
1589 if (action & A_START) start_rstats(1);
1590 goto CLEAR;
1593 if (strcmp(service, "sched") == 0) {
1594 if (action & A_STOP) stop_sched();
1595 if (action & A_START) start_sched();
1596 goto CLEAR;
1599 #ifdef TCONFIG_USB
1600 // !!TB - USB Support
1601 if (strcmp(service, "usb") == 0) {
1602 if (action & A_STOP) stop_usb();
1603 if (action & A_START) {
1604 start_usb();
1605 // restart Samba and ftp since they may be killed by stop_usb()
1606 restart_nas_services(1);
1608 goto CLEAR;
1610 #endif
1612 #ifdef TCONFIG_FTP
1613 // !!TB - FTP Server
1614 if (strcmp(service, "ftpd") == 0) {
1615 if (action & A_STOP) stop_ftpd();
1616 setup_conntrack();
1617 stop_firewall();
1618 start_firewall();
1619 if (action & A_START) start_ftpd();
1620 goto CLEAR;
1622 #endif
1624 #ifdef TCONFIG_SAMBASRV
1625 // !!TB - Samba
1626 if (strcmp(service, "samba") == 0 || strcmp(service, "smbd") == 0) {
1627 if (action & A_STOP) stop_samba();
1628 if (action & A_START) {
1629 create_passwd();
1630 start_samba();
1632 goto CLEAR;
1634 #endif
1636 CLEAR:
1637 if (next) goto TOP;
1639 // some functions check action_service and must be cleared at end -- zzz
1640 nvram_set("action_service", "");
1643 static void do_service(const char *name, const char *action, int user)
1645 int n;
1646 char s[64];
1648 n = 15;
1649 while (!nvram_match("action_service", "")) {
1650 if (user) {
1651 putchar('*');
1652 fflush(stdout);
1654 else if (--n < 0) break;
1655 sleep(1);
1658 snprintf(s, sizeof(s), "%s-%s", name, action);
1659 nvram_set("action_service", s);
1660 kill(1, SIGUSR1);
1662 n = 15;
1663 while (nvram_match("action_service", s)) {
1664 if (user) {
1665 putchar('.');
1666 fflush(stdout);
1668 else if (--n < 0) {
1669 break;
1671 sleep(1);
1675 int service_main(int argc, char *argv[])
1677 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1678 do_service(argv[1], argv[2], 1);
1679 printf("\nDone.\n");
1680 return 0;
1683 void start_service(const char *name)
1685 do_service(name, "start", 0);
1688 void stop_service(const char *name)
1690 do_service(name, "stop", 0);
1694 void restart_service(const char *name)
1696 do_service(name, "restart", 0);