Merge branch 'tomato-ND-usbmod-base' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / services.c
blobfce5c55a2e4a88bb14c509a1680ba831da1644b7
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-2008 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 if (getpid() != 1) {
83 start_service("dnsmasq");
84 return;
87 stop_dnsmasq();
89 if (nvram_match("wl_mode", "wet")) return;
90 /*
91 if (nvram_get_int("dnsmasq_norw")) {
92 if (f_exists("/etc/dnsmasq.conf")) {
93 syslog(LOG_INFO, "%s exists. Not rewriting.", "/etc/dnsmasq.conf");
94 goto RUN;
98 if ((f = fopen("/etc/dnsmasq.conf", "w")) == NULL) return;
100 lan_ifname = nvram_safe_get("lan_ifname");
101 router_ip = nvram_safe_get("lan_ipaddr");
102 strlcpy(lan, router_ip, sizeof(lan));
103 if ((p = strrchr(lan, '.')) != NULL) *(p + 1) = 0;
105 fprintf(f,
106 "pid-file=%s\n"
107 "interface=%s\n",
108 dmpid, lan_ifname);
109 if (((nv = nvram_get("wan_domain")) != NULL) || ((nv = nvram_get("wan_get_domain")) != NULL)) {
110 if (*nv) fprintf(f, "domain=%s\n", nv);
113 // dns
114 if (((nv = nvram_get("dns_minport")) != NULL) && (*nv)) n = atoi(nv);
115 else n = 4096;
116 fprintf(f,
117 "resolv-file=%s\n" // the real stuff is here
118 "addn-hosts=%s\n" // "
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv, dmhosts, n);
122 do_dns = nvram_match("dhcpd_dmdns", "1");
125 // dhcp
126 do_dhcpd = nvram_match("lan_proto", "dhcp");
127 if (do_dhcpd) {
128 dhcp_lease = nvram_get_int("dhcp_lease");
129 if (dhcp_lease <= 0) dhcp_lease = 1440;
131 if ((e = nvram_get("dhcpd_slt")) != NULL) n = atoi(e); else n = 0;
132 if (n < 0) strcpy(sdhcp_lease, "infinite");
133 else sprintf(sdhcp_lease, "%dm", (n > 0) ? n : dhcp_lease);
135 if (!do_dns) {
136 // if not using dnsmasq for dns
138 const dns_list_t *dns = get_dns(); // this always points to a static buffer
139 if ((dns->count == 0) && (nvram_match("dhcpd_llndns", "1"))) {
140 // no DNS might be temporary. use a low lease time to force clients to update.
141 dhcp_lease = 2;
142 strcpy(sdhcp_lease, "2m");
143 do_dns = 1;
145 else {
146 // pass the dns directly
147 buf[0] = 0;
148 for (n = 0 ; n < dns->count; ++n) {
149 sprintf(buf + strlen(buf), ",%s", inet_ntoa(dns->dns[n]));
151 fprintf(f, "dhcp-option=6%s\n", buf);
155 if ((p = nvram_get("dhcpd_startip")) && (*p) && (e = nvram_get("dhcpd_endip")) && (*e)) {
156 fprintf(f, "dhcp-range=%s,%s,%s,%dm\n", p, e, nvram_safe_get("lan_netmask"), dhcp_lease);
158 else {
159 // for compatibility
160 dhcp_start = nvram_get_int("dhcp_start");
161 dhcp_count = nvram_get_int("dhcp_num");
162 fprintf(f, "dhcp-range=%s%d,%s%d,%s,%dm\n",
163 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease);
165 n = nvram_get_int("dhcpd_lmax");
166 fprintf(f,
167 "dhcp-option=3,%s\n" // gateway
168 "dhcp-lease-max=%d\n",
169 router_ip,
170 (n > 0) ? n : 255);
173 dhcp_start = nvram_get_int("dhcp_start");
174 dhcp_count = nvram_get_int("dhcp_num");
175 n = nvram_get_int("dhcpd_lmax");
176 fprintf(f,
177 "dhcp-range=%s%d,%s%d,%s,%dm\n" // lease config
178 "dhcp-option=3,%s\n" // gateway
179 "dhcp-lease-max=%d\n",
180 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease,
181 router_ip,
182 (n > 0) ? n : 255);
185 if (nvram_get_int("dhcpd_auth") >= 0) {
186 fprintf(f, "dhcp-authoritative\n");
189 if (check_wanup()) {
190 // avoid leasing wan ip incase the modem gives an ip in our range
191 fprintf(f, "dhcp-host=01:02:03:04:05:06,%s\n", nvram_safe_get("wan_ipaddr"));
194 if (((nv = nvram_get("wan_wins")) != NULL) && (*nv) && (strcmp(nv, "0.0.0.0") != 0)) {
195 fprintf(f, "dhcp-option=44,%s\n", nv);
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 p = nvram_safe_get("dhcpd_static"); // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
214 while ((e = strchr(p, '>')) != NULL) {
215 n = (e - p);
216 if (n > 52) {
217 p = e + 1;
218 continue;
221 strncpy(buf, p, n);
222 buf[n] = 0;
223 p = e + 1;
225 if ((e = strchr(buf, '<')) == NULL) continue;
226 *e = 0;
227 mac = buf;
229 ip = e + 1;
230 if ((e = strchr(ip, '<')) == NULL) continue;
231 *e = 0;
232 if (strchr(ip, '.') == NULL) {
233 ipn = atoi(ip);
234 if ((ipn <= 0) || (ipn > 255)) continue;
235 sprintf(ipbuf, "%s%d", lan, ipn);
236 ip = ipbuf;
238 else {
239 if (inet_addr(ip) == INADDR_NONE) continue;
242 name = e + 1;
244 if ((hf) && (*name != 0)) {
245 fprintf(hf, "%s %s\n", ip, name);
248 if ((do_dhcpd) && (*mac != 0) && (strcmp(mac, "00:00:00:00:00:00") != 0)) {
249 fprintf(f, "dhcp-host=%s,%s,%s\n", mac, ip, sdhcp_lease);
253 if (hf) fclose(hf);
257 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
261 FILE *af;
263 fflush(f);
264 if ((af = fopen("/etc/dnsmasq.custom", "r")) != NULL) {
265 while ((n = fread(buf, 1, sizeof(buf), af)) > 0) {
266 fwrite(buf, 1, n, f);
268 fclose(af);
273 fclose(f);
275 if (do_dns) {
276 unlink("/etc/resolv.conf");
277 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
280 // RUN:
281 eval("dnsmasq");
283 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
284 f_read_string(dmpid, buf, sizeof(buf));
285 pid_dnsmasq = atol(buf);
289 void stop_dnsmasq(void)
291 if (getpid() != 1) {
292 stop_service("dnsmasq");
293 return;
296 pid_dnsmasq = -1;
298 unlink("/etc/resolv.conf");
299 symlink(dmresolv, "/etc/resolv.conf");
301 killall_tk("dnsmasq");
304 void clear_resolv(void)
306 _dprintf("%s\n", __FUNCTION__);
308 f_write(dmresolv, NULL, 0, 0, 0); // blank
311 void dns_to_resolv(void)
313 FILE *f;
314 const dns_list_t *dns;
315 int i;
316 mode_t m;
318 _dprintf("%s\n", __FUNCTION__);
320 m = umask(022); // 077 from pppoecd
321 if ((f = fopen(dmresolv, "w")) != NULL) {
322 dns = get_dns(); // static buffer
323 if (dns->count == 0) {
324 // Put a pseudo DNS IP to trigger Connect On Demand
325 if ((nvram_match("ppp_demand", "1")) &&
326 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
327 fprintf(f, "nameserver 1.1.1.1\n");
330 else {
331 for (i = 0; i < dns->count; i++) {
332 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i]));
335 fclose(f);
337 umask(m);
340 // -----------------------------------------------------------------------------
342 void start_httpd(void)
344 chdir("/www");
345 if (!nvram_match("http_enable", "0")) {
346 xstart("httpd");
348 if (!nvram_match("https_enable", "0")) {
349 xstart("httpd", "-s");
351 chdir("/");
354 void stop_httpd(void)
356 killall_tk("httpd");
359 // -----------------------------------------------------------------------------
361 //!!TB - miniupnpd - most of the code is stolen from Tarifa 034RC1 sources
363 #if 0 // read UUID from /proc/sys/kernel/random/uuid instead
364 void uuidstr_create(char *str)
366 typedef unsigned int u_int32;
367 typedef unsigned short u_int16;
368 typedef unsigned char u_int8;
370 static int uuid_count = 0;
371 u_int d[16];
373 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
374 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]);
375 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
376 &d[6], &d[7], &d[8], &d[9], &d[10], &d[11]);
377 *((int *)&d[12]) = uuid_count++;
379 sprintf(str, "fc4ec57e-b051-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
380 (u_int8)d[6], (u_int8)d[7], (u_int8)d[8], (u_int8)d[9], (u_int8)d[10],
381 (u_int8)d[11], (u_int8)d[12], (u_int8)d[13], (u_int8)d[14], (u_int8)d[15]);
383 #endif //0
385 void start_upnp(void)
387 //!!TB - miniupnpd
388 #if 0
389 if ((nvram_match("upnp_enable", "1")) && (get_wan_proto() != WP_DISABLED)) {
390 #ifdef TEST_MINIUPNP
391 xstart("miniupnpd",
392 "-i", nvram_safe_get("wan_iface"),
393 "-a", nvram_safe_get("lan_ipaddr"),
394 "-p", "5555",
395 "-U");
396 #else
397 xstart("upnp",
398 "-D",
399 "-L", nvram_safe_get("lan_ifname"),
400 "-W", nvram_safe_get("wan_iface"),
401 "-I", nvram_safe_get("upnp_ssdp_interval"),
402 "-A", nvram_safe_get("upnp_max_age"));
404 #endif
405 #else //0
406 char uuid[45];
407 char fname[] = "/etc/miniupnpd.conf";
408 char *lanip;
409 int interval, r;
410 FILE *fp = NULL;
412 if ((!nvram_invmatch("upnp_enable", "0")) & (!nvram_invmatch("upnp_nat_pmp_enable", "0")) || (get_wan_proto() == WP_DISABLED))
413 return;
415 fp = fopen(fname, "w");
416 if(NULL == fp)
417 return;
419 lanip = nvram_safe_get("lan_ipaddr");
421 fprintf(fp, "ext_ifname=%s\n", nvram_safe_get("wan_iface"));
422 fprintf(fp, "listening_ip=%s\n", lanip);
423 fprintf(fp, "port=%s\n", nvram_safe_get("upnp_port"));
424 fprintf(fp, "upnp_forward_chain=upnp\n");
425 fprintf(fp, "upnp_nat_chain=upnp\n");
426 fprintf(fp, "enable_upnp=%s\n", nvram_match("upnp_enable", "1") ? "yes" : "no");
427 fprintf(fp, "enable_natpmp=%s\n", nvram_match("upnp_nat_pmp_enable", "1") ? "yes" : "no");
428 fprintf(fp, "secure_mode=%s\n", nvram_match("upnp_secure_mode", "1") ? "yes" : "no");
429 fprintf(fp, "system_uptime=no\n");
430 fprintf(fp, "notify_interval=%d\n", nvram_get_int("upnp_ssdp_interval"));
432 r = nvram_get_int("upnp_bitrate_up");
433 if (r > 0) fprintf(fp, "bitrate_up=%d\n", r);
434 r = nvram_get_int("upnp_bitrate_down");
435 if (r > 0) fprintf(fp, "bitrate_down=%d\n", r);
437 if (nvram_match("upnp_clean_ruleset_enable", "1")) {
438 interval = nvram_get_int("upnp_clean_ruleset_interval");
439 if (interval < 60) interval = 60;
440 fprintf(fp, "clean_ruleset_interval=%d\n", interval);
441 fprintf(fp, "clean_ruleset_threshold=%d\n", nvram_get_int("upnp_clean_ruleset_threshold"));
443 else
444 fprintf(fp,"clean_ruleset_interval=0\n");
446 if (nvram_match("upnp_mnp", "1")) {
447 int https = nvram_match("https_enable", "1");
448 fprintf(fp, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
449 https ? "s" : "", lanip,
450 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
452 else {
453 // Empty parameters are not included into XML service description
454 fprintf(fp, "presentation_url=\n");
457 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
458 fprintf(fp, "uuid=%s\n", uuid);
460 if ((nvram_get_int("upnp_min_port_int") > 0) &&
461 (nvram_get_int("upnp_max_port_int") > 0) &&
462 (nvram_get_int("upnp_min_port_ext") > 0) &&
463 (nvram_get_int("upnp_max_port_ext") > 0)) {
465 fprintf(fp, "allow %s", nvram_safe_get("upnp_min_port_int"));
466 fprintf(fp, "-%s", nvram_safe_get("upnp_max_port_int"));
467 fprintf(fp, " %s/24", lanip);
468 fprintf(fp, " %s", nvram_safe_get("upnp_min_port_ext"));
469 fprintf(fp, "-%s\n", nvram_safe_get("upnp_max_port_ext"));
471 else {
472 // by default allow only redirection of ports above 1024
473 fprintf(fp, "allow 1024-65535 %s/24 1024-65535\n", lanip);
475 fprintf(fp, "deny 0-65535 0.0.0.0/0 0-65535\n");
477 fclose(fp);
479 xstart("miniupnpd", "-f", fname);
480 #endif //0
483 void stop_upnp(void)
485 #if 0
486 //!!TB - miniupnpd
487 #ifdef TEST_MINIUPNP
488 killall_tk("miniupnpd");
489 #else
490 killall_tk("upnp");
491 #endif
492 #else //0
493 killall_tk("miniupnpd");
494 unlink("/var/run/miniupnpd.pid");
495 unlink("/etc/miniupnpd.conf");
496 #endif //0
499 // -----------------------------------------------------------------------------
501 static pid_t pid_crond = -1;
503 void start_cron(void)
505 _dprintf("%s\n", __FUNCTION__);
507 stop_cron();
509 char *argv[] = { "crond", "-l", "9", NULL };
511 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
512 _eval(argv, NULL, 0, NULL);
514 if (!nvram_contains_word("debug_norestart", "crond")) {
515 pid_crond = -2; // intentionally fail test_cron()
520 void stop_cron(void)
522 _dprintf("%s\n", __FUNCTION__);
524 pid_crond = -1;
525 killall_tk("crond");
528 // -----------------------------------------------------------------------------
530 // Written by Sparq in 2002/07/16
531 void start_zebra(void)
533 #ifdef TCONFIG_ZEBRA
534 FILE *fp;
536 char *lan_tx = nvram_safe_get("dr_lan_tx");
537 char *lan_rx = nvram_safe_get("dr_lan_rx");
538 char *wan_tx = nvram_safe_get("dr_wan_tx");
539 char *wan_rx = nvram_safe_get("dr_wan_rx");
541 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
542 return;
545 // empty
546 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
547 fclose(fp);
551 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
552 char *lan_ifname = nvram_safe_get("lan_ifname");
553 char *wan_ifname = nvram_safe_get("wan_ifname");
555 fprintf(fp, "router rip\n");
556 fprintf(fp, "network %s\n", lan_ifname);
557 fprintf(fp, "network %s\n", wan_ifname);
558 fprintf(fp, "redistribute connected\n");
559 //fprintf(fp, "redistribute static\n");
561 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
562 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
564 fprintf(fp, "interface %s\n", lan_ifname);
565 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
566 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
568 fprintf(fp, "interface %s\n", wan_ifname);
569 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
570 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
572 fprintf(fp, "router rip\n");
573 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
574 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
575 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
576 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
577 fprintf(fp, "access-list private deny any\n");
579 //fprintf(fp, "debug rip events\n");
580 //fprintf(fp, "log file /etc/ripd.log\n");
581 fclose(fp);
584 xstart("zebra", "-d", "-f", "/etc/zebra.conf");
585 xstart("ripd", "-d", "-f", "/etc/ripd.conf");
586 #endif
589 void stop_zebra(void)
591 #ifdef TCONFIG_ZEBRA
592 killall("zebra", SIGTERM);
593 killall("ripd", SIGTERM);
595 unlink("/etc/zebra.conf");
596 unlink("/etc/ripd.conf");
597 #endif
600 // -----------------------------------------------------------------------------
602 void start_syslog(void)
604 #if 1
605 char *argv[12];
606 int argc;
607 char *nv;
608 char rem[256];
609 int n;
610 char s[64];
612 argv[0] = "syslogd";
613 argc = 1;
615 if (nvram_match("log_remote", "1")) {
616 nv = nvram_safe_get("log_remoteip");
617 if (*nv) {
618 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
619 argv[argc++] = "-R";
620 argv[argc++] = rem;
624 if (nvram_match("log_file", "1")) {
625 argv[argc++] = "-L";
626 argv[argc++] = "-s";
627 argv[argc++] = "50";
630 if (argc > 1) {
631 argv[argc] = NULL;
632 _eval(argv, NULL, 0, NULL);
633 usleep(500000);
635 argv[0] = "klogd";
636 argv[1] = NULL;
637 _eval(argv, NULL, 0, NULL);
638 usleep(500000);
640 // used to be available in syslogd -m
641 n = nvram_get_int("log_mark");
642 if (n > 0) {
643 sprintf(s, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
644 (n < 60) ? "*/30" : "0", (n < 120) ? "*" : "*/2");
645 system(s);
649 #else
650 char *argv[12];
651 int argc;
652 char *nv;
653 char rem[256];
655 argv[0] = "syslogd";
656 argv[1] = "-m";
657 argv[2] = nvram_get("log_mark");
658 argc = 3;
660 if (nvram_match("log_remote", "1")) {
661 nv = nvram_safe_get("log_remoteip");
662 if (*nv) {
663 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
664 argv[argc++] = "-R";
665 argv[argc++] = rem;
669 if (nvram_match("log_file", "1")) {
670 argv[argc++] = "-L";
671 argv[argc++] = "-s";
672 argv[argc++] = "50";
675 if (argc > 3) {
676 argv[argc] = NULL;
677 _eval(argv, NULL, 0, NULL);
678 usleep(500000);
680 argv[0] = "klogd";
681 argv[1] = NULL;
682 _eval(argv, NULL, 0, NULL);
683 usleep(500000);
685 #endif
688 void stop_syslog(void)
690 killall("klogd", SIGTERM);
691 killall("syslogd", SIGTERM);
694 // -----------------------------------------------------------------------------
696 static pid_t pid_igmp = -1;
698 void start_igmp_proxy(void)
700 char *p;
702 pid_igmp = -1;
703 if (nvram_match("multicast_pass", "1")) {
704 switch (get_wan_proto()) {
705 case WP_PPPOE:
706 case WP_PPTP:
707 case WP_L2TP:
708 p = "wan_iface";
709 break;
710 default:
711 p = "wan_ifname";
712 break;
714 xstart("igmprt", "-f", "-i", nvram_safe_get(p));
716 if (!nvram_contains_word("debug_norestart", "igmprt")) {
717 pid_igmp = -2;
722 void stop_igmp_proxy(void)
724 pid_igmp = -1;
725 killall("igmprt", SIGTERM);
729 // -----------------------------------------------------------------------------
731 void set_tz(void)
733 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
736 void start_ntpc(void)
738 set_tz();
740 stop_ntpc();
742 if (nvram_get_int("ntp_updates") >= 0) {
743 xstart("ntpsync", "--init");
747 void stop_ntpc(void)
749 killall("ntpsync", SIGTERM);
752 // -----------------------------------------------------------------------------
754 static void stop_rstats(void)
756 int n;
757 int pid;
759 n = 60;
760 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
761 if (kill(pid, SIGTERM) != 0) break;
762 sleep(1);
766 static void start_rstats(int new)
768 if (nvram_match("rstats_enable", "1")) {
769 stop_rstats();
770 if (new) xstart("rstats", "--new");
771 else xstart("rstats");
775 // -----------------------------------------------------------------------------
777 // !!TB - FTP Server
780 * Return non-zero if we created the directory,
781 * and zero if it already existed.
783 int mkdir_if_none(char *dir)
785 DIR *dp;
786 if (!(dp=opendir(dir))) {
787 umask(0000);
788 mkdir(dir, 0777);
789 return 1;
791 closedir(dp);
792 return 0;
795 char *get_full_storage_path(char *val)
797 static char buf[128];
798 int len;
800 if (val[0] == '/')
801 len = sprintf(buf, "%s", val);
802 else
803 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
805 if (len > 1 && buf[len - 1] == '/')
806 buf[len - 1] = 0;
808 return buf;
811 char *nvram_storage_path(char *var)
813 char *val = nvram_safe_get(var);
814 return get_full_storage_path(val);
817 #ifdef TCONFIG_FTP
819 char vsftpd_conf[] = "/etc/vsftpd.conf";
820 char vsftpd_users[] = "/etc/vsftpd.users";
821 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
822 #endif
824 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
825 void start_ftpd(void)
827 #ifdef TCONFIG_FTP
829 char tmp[256];
830 FILE *fp, *f;
832 killall("vsftpd", SIGTERM);
833 if (!nvram_get_int("ftp_enable")) return;
835 mkdir_if_none(vsftpd_users);
836 mkdir_if_none("/var/run/vsftpd");
838 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
839 return;
841 if (nvram_match("ftp_super", "1"))
843 /* rights */
844 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
845 if ((f = fopen(tmp, "w")))
847 fprintf(f,
848 "dirlist_enable=yes\n"
849 "write_enable=yes\n"
850 "download_enable=yes\n");
851 fclose(f);
855 #ifdef TCONFIG_SAMBASRV
856 if (nvram_match("smbd_cset", "utf8"))
857 fprintf(fp, "utf8=yes\n");
858 #endif
860 if (nvram_invmatch("ftp_anonymous", "0"))
862 fprintf(fp,
863 "anon_allow_writable_root=yes\n"
864 "anon_world_readable_only=no\n"
865 "anon_umask=022\n");
867 /* rights */
868 sprintf(tmp, "%s/ftp", vsftpd_users);
869 if ((f = fopen(tmp, "w")))
871 if (nvram_match("ftp_dirlist", "0"))
872 fprintf(f, "dirlist_enable=yes\n");
873 if (nvram_match("ftp_anonymous", "1") ||
874 nvram_match("ftp_anonymous", "3"))
875 fprintf(f, "write_enable=yes\n");
876 if (nvram_match("ftp_anonymous", "1") ||
877 nvram_match("ftp_anonymous", "2"))
878 fprintf(f, "download_enable=yes\n");
879 fclose(f);
881 if (nvram_match("ftp_anonymous", "1") ||
882 nvram_match("ftp_anonymous", "3"))
883 fprintf(fp,
884 "anon_upload_enable=yes\n"
885 "anon_mkdir_write_enable=yes\n"
886 "anon_other_write_enable=yes\n");
887 } else {
888 fprintf(fp, "anonymous_enable=no\n");
891 fprintf(fp,
892 "dirmessage_enable=yes\n"
893 "download_enable=no\n"
894 "dirlist_enable=no\n"
895 "hide_ids=yes\n"
896 "syslog_enable=yes\n"
897 "local_enable=yes\n"
898 "local_umask=022\n"
899 "chmod_enable=no\n"
900 "chroot_local_user=yes\n"
901 "check_shell=no\n"
902 "user_config_dir=%s\n"
903 "passwd_file=%s\n",
904 vsftpd_users, vsftpd_passwd);
906 if (nvram_match("log_ftp", "1")) {
907 fprintf(fp, "log_ftp_protocol=yes\n");
909 else {
910 fprintf(fp, "log_ftp_protocol=no\n");
913 fprintf(fp, "listen=yes\nlisten_port=%s\nbackground=yes\n",
914 nvram_get("ftp_port") ? : "21");
915 fprintf(fp, "max_clients=%s\n", nvram_get("ftp_max") ? : "0");
916 fprintf(fp, "max_per_ip=%s\n", nvram_get("ftp_ipmax") ? : "0");
917 fprintf(fp, "idle_session_timeout=%s\n", nvram_get("ftp_staytimeout") ? : "300");
918 fprintf(fp, "use_sendfile=no\n");
919 //fprintf(fp, "ftpd_banner=Welcome to the %s FTP service.\n", nvram_get("t_model_name") ? : "router");
921 /* bandwidth */
922 fprintf(fp, "anon_max_rate=%d\nlocal_max_rate=%d\n",
923 atoi(nvram_safe_get("ftp_anonrate")) * 1024,
924 atoi(nvram_safe_get("ftp_rate")) * 1024);
926 fprintf(fp, "%s\n\n", nvram_safe_get("ftp_custom"));
928 fclose(fp);
930 /* prepare passwd file and default users */
931 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
932 return;
934 fprintf(fp, /* anonymous, admin, nobody */
935 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
936 "%s:%s:0:0:root:/:/sbin/nologin\n"
937 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
938 nvram_storage_path("ftp_anonroot"), "admin",
939 nvram_match("ftp_super", "1") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
940 MOUNT_ROOT);
942 char *buf;
943 char *p, *q;
944 char *user, *pass, *rights;
946 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
949 username<password<rights
950 rights:
951 Read/Write
952 Read Only
953 View Only
954 Private
956 p = buf;
957 while ((q = strsep(&p, ">")) != NULL) {
958 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
959 if (!user || !pass) continue;
961 /* directory */
962 if (strncmp(rights, "Private", 7) == 0)
964 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
965 mkdir_if_none(tmp);
967 else
968 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
970 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
971 user, crypt(pass, "$1$"), user, tmp);
973 /* rights */
974 sprintf(tmp, "%s/%s", vsftpd_users, user);
975 if ((f = fopen(tmp, "w")))
977 tmp[0] = 0;
978 if (nvram_invmatch("ftp_dirlist", "1"))
979 strcat(tmp, "dirlist_enable=yes\n");
980 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
981 strcat(tmp, "download_enable=yes\n");
982 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
983 strcat(tmp, "write_enable=yes\n");
985 fputs(tmp, f);
986 fclose(f);
989 free(buf);
992 fclose(fp);
994 eval("vsftpd");
995 #endif
998 void stop_ftpd(void)
1000 #ifdef TCONFIG_FTP
1001 killall("vsftpd", SIGTERM);
1002 unlink(vsftpd_passwd);
1003 unlink(vsftpd_conf);
1004 eval("rm", "-rf", vsftpd_users);
1005 #endif
1008 // -----------------------------------------------------------------------------
1010 // !!TB - Samba
1012 #ifdef TCONFIG_SAMBASRV
1013 void kill_samba(int sig)
1015 killall("smbd", sig);
1016 killall("nmbd", sig);
1018 #endif
1020 void start_samba(void)
1022 #ifdef TCONFIG_SAMBASRV
1024 FILE *fp;
1025 DIR *dir = NULL;
1026 struct dirent *dp;
1027 char nlsmod[15];
1028 int mode;
1030 mode = nvram_get_int("smbd_enable");
1031 if (!mode || !nvram_invmatch("lan_hostname", ""))
1032 return;
1034 if ((fp = fopen("/etc/smb.conf", "w")) == NULL) {
1035 perror("/etc/smb.conf");
1036 return;
1039 fprintf(fp, "[global]\n"
1040 " interfaces = %s\n"
1041 " bind interfaces only = yes\n"
1042 " workgroup = %s\n"
1043 " server string = %s\n"
1044 " guest account = nobody\n"
1045 " security = %s\n"
1046 " browseable = yes\n"
1047 " guest ok = yes\n"
1048 " guest only = no\n"
1049 " log level = %d\n"
1050 " syslog only = yes\n"
1051 " syslog = 1\n"
1052 " encrypt passwords = yes\n"
1053 " local master = %s\n"
1054 " preserve case = yes\n"
1055 " short preserve case = yes\n",
1056 nvram_get("lan_ifname") ? : "br0",
1057 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1058 nvram_get("router_name") ? : "Tomato",
1059 mode == 2 ? "user" : "share",
1060 nvram_get_int("smbd_loglevel"),
1061 nvram_get_int("smbd_master") ? "yes" : "no"
1064 if (nvram_invmatch("smbd_cpage", "")) {
1065 char *cp = nvram_get("smbd_cpage");
1067 fprintf(fp, " client code page = %s\n", cp);
1068 sprintf(nlsmod, "nls_cp%s", cp);
1070 cp = nvram_get("smbd_nlsmod");
1071 if ((cp) && (*cp != 0) && (strcmp(cp, nlsmod) != 0))
1072 modprobe_r(cp);
1074 modprobe(nlsmod);
1075 nvram_set("smbd_nlsmod", nlsmod);
1078 if (nvram_match("smbd_cset", "utf8"))
1079 fprintf(fp, " coding system = utf8\n");
1080 else if (nvram_invmatch("smbd_cset", ""))
1081 fprintf(fp, " character set = %s\n", nvram_get("smbd_cset"));
1083 fprintf(fp, "%s\n\n", nvram_safe_get("smbd_custom"));
1085 /* configure shares */
1087 char *buf;
1088 char *p, *q;
1089 char *name, *path, *comment, *writeable, *hidden;
1090 int cnt = 0;
1092 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1094 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1096 p = buf;
1097 while ((q = strsep(&p, ">")) != NULL) {
1098 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1099 if (!path || !name) continue;
1101 /* share name */
1102 fprintf(fp, "\n[%s]\n", name);
1104 /* path */
1105 fprintf(fp, " path = %s\n", path);
1107 /* access level */
1108 if (!strcmp(writeable, "1"))
1109 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1110 if (!strcmp(hidden, "1"))
1111 fprintf(fp, " browseable = no\n");
1113 /* comment */
1114 if (comment)
1115 fprintf(fp, " comment = %s\n", comment);
1117 cnt++;
1119 free(buf);
1122 /* share everything below MOUNT_ROOT */
1123 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1124 while ((dp = readdir(dir))) {
1125 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1127 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1128 fprintf(fp, "\n[%s]\n path = %s/%s\n",
1129 dp->d_name, MOUNT_ROOT, dp->d_name);
1130 if (nvram_match("smbd_autoshare", "3")) // Hidden
1131 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1132 dp->d_name, MOUNT_ROOT, dp->d_name);
1133 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1134 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1136 cnt++;
1140 if (dir) closedir(dir);
1142 if (cnt == 0) {
1143 /* by default share MOUNT_ROOT as read-only */
1144 fprintf(fp, "\n[share]\n"
1145 " path = %s\n"
1146 " writable = no\n",
1147 MOUNT_ROOT);
1150 fclose(fp);
1152 mkdir_if_none("/var/run/samba");
1153 mkdir_if_none("/etc/samba");
1155 /* write smbpasswd */
1156 eval("smbpasswd", "-a", "nobody", "\"\"");
1157 if (mode == 2) {
1158 char *smbd_user;
1159 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1160 smbd_user = "nas";
1161 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1164 kill_samba(SIGHUP);
1165 int ret1 = 0, ret2 = 0;
1166 /* start samba if it's not already running */
1167 if (pidof("nmbd") <= 0)
1168 ret1 = eval("nmbd", "-D");
1169 if (pidof("smbd") <= 0)
1170 ret2 = eval("smbd", "-D");
1172 if (ret1 || ret2) kill_samba(SIGTERM);
1173 #endif
1176 void stop_samba(void)
1178 #ifdef TCONFIG_SAMBASRV
1179 kill_samba(SIGTERM);
1180 sleep(2); /* wait for smbd to finish */
1182 if (nvram_invmatch("smbd_nlsmod", "")) {
1183 modprobe_r(nvram_get("smbd_nlsmod"));
1184 nvram_set("smbd_nlsmod", "");
1187 /* clean up */
1188 unlink("/var/log/smb");
1189 unlink("/var/log/nmb");
1190 eval("rm", "-rf", "/var/run/samba");
1191 #endif
1194 void restart_nas_services(int start)
1196 /* restart all NAS applications */
1197 #ifdef TCONFIG_SAMBASRV
1198 if (start && nvram_get_int("smbd_enable"))
1199 start_samba();
1200 else
1201 kill_samba(SIGTERM);
1202 #endif
1203 #ifdef TCONFIG_FTP
1204 if (start && nvram_get_int("ftp_enable"))
1205 start_ftpd();
1206 else
1207 killall("vsftpd", SIGTERM);
1208 #endif
1211 // -----------------------------------------------------------------------------
1213 static void _check(pid_t *pid, const char *name, void (*func)(void) )
1215 if (*pid != -1) {
1216 if (kill(*pid, 0) != 0) {
1217 if ((*pid = pidof(name)) == -1) func();
1222 void check_services(void)
1224 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
1225 _check(&pid_crond, "crond", start_cron);
1226 _check(&pid_igmp, "igmprt", start_igmp_proxy);
1229 // -----------------------------------------------------------------------------
1231 void start_services(void)
1233 static int once = 1;
1235 if (once) {
1236 once = 0;
1238 create_passwd();
1239 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1240 if (nvram_match("sshd_eas", "1")) start_sshd();
1243 start_syslog();
1244 #if TOMATO_SL
1245 start_usbevent();
1246 #endif
1247 start_nas();
1248 start_zebra();
1249 start_dnsmasq();
1250 start_cifs();
1251 start_httpd();
1252 start_cron();
1253 start_upnp();
1254 start_rstats(0);
1255 start_sched();
1256 #ifdef TCONFIG_SAMBA
1257 start_smbd();
1258 #endif
1259 start_samba(); // !!TB - Samba
1260 start_ftpd(); // !!TB - FTP Server
1263 void stop_services(void)
1265 clear_resolv();
1267 stop_ftpd(); // !!TB - FTP Server
1268 stop_samba(); // !!TB - Samba
1269 #ifdef TCONFIG_SAMBA
1270 stop_smbd();
1271 #endif
1272 stop_sched();
1273 stop_rstats();
1274 stop_upnp();
1275 stop_cron();
1276 stop_httpd();
1277 stop_cifs();
1278 stop_dnsmasq();
1279 stop_zebra();
1280 stop_nas();
1281 #if TOMATO_SL
1282 stop_usbevent();
1283 #endif
1284 stop_syslog();
1287 // -----------------------------------------------------------------------------
1289 void exec_service(void)
1291 const int A_START = 1;
1292 const int A_STOP = 2;
1293 const int A_RESTART = 1|2;
1294 char buffer[128];
1295 char *service;
1296 char *act;
1297 char *next;
1298 int action;
1299 int i;
1301 strlcpy(buffer, nvram_safe_get("action_service"), sizeof(buffer));
1302 next = buffer;
1304 TOP:
1305 act = strsep(&next, ",");
1306 service = strsep(&act, "-");
1307 if (act == NULL) {
1308 next = NULL;
1309 goto CLEAR;
1312 if (strcmp(act, "start") == 0) action = A_START;
1313 else if (strcmp(act, "stop") == 0) action = A_STOP;
1314 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1315 else action = 0;
1317 _dprintf("%s %s service=%s action=%s\n", __FILE__, __FUNCTION__, service, act);
1320 if (strcmp(service, "dhcpc") == 0) {
1321 if (action & A_STOP) stop_dhcpc();
1322 if (action & A_START) start_dhcpc();
1323 goto CLEAR;
1326 if ((strcmp(service, "dhcpd") == 0) || (strcmp(service, "dns") == 0) || (strcmp(service, "dnsmasq") == 0)) {
1327 if (action & A_STOP) stop_dnsmasq();
1328 if (action & A_START) {
1329 dns_to_resolv();
1330 start_dnsmasq();
1332 goto CLEAR;
1335 if (strcmp(service, "firewall") == 0) {
1336 if (action & A_STOP) {
1337 stop_firewall();
1338 stop_igmp_proxy();
1340 if (action & A_START) {
1341 start_firewall();
1342 start_igmp_proxy();
1344 goto CLEAR;
1347 if (strcmp(service, "restrict") == 0) {
1348 if (action & A_STOP) {
1349 stop_firewall();
1351 if (action & A_START) {
1352 i = nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1354 start_firewall();
1356 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1357 if (i == 1) {
1358 if (nvram_get_int("rrules_radio") < 0) {
1359 if (!get_radio()) eval("radio", "on");
1363 goto CLEAR;
1366 if (strcmp(service, "qos") == 0) {
1367 if (action & A_STOP) {
1368 stop_qos();
1370 stop_firewall(); start_firewall(); // always restarted
1371 if (action & A_START) {
1372 start_qos();
1373 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1375 goto CLEAR;
1378 if (strcmp(service, "upnp") == 0) {
1379 if (action & A_STOP) {
1380 stop_upnp();
1382 stop_firewall(); start_firewall(); // always restarted
1383 if (action & A_START) {
1384 start_upnp();
1386 goto CLEAR;
1389 if (strcmp(service, "telnetd") == 0) {
1390 if (action & A_STOP) stop_telnetd();
1391 if (action & A_START) start_telnetd();
1392 goto CLEAR;
1395 if (strcmp(service, "sshd") == 0) {
1396 if (action & A_STOP) stop_sshd();
1397 if (action & A_START) start_sshd();
1398 goto CLEAR;
1401 if (strcmp(service, "admin") == 0) {
1402 if (action & A_STOP) {
1403 stop_sshd();
1404 stop_telnetd();
1405 stop_httpd();
1407 stop_firewall(); start_firewall(); // always restarted
1408 if (action & A_START) {
1409 start_httpd();
1410 create_passwd();
1411 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1412 if (nvram_match("sshd_eas", "1")) start_sshd();
1414 goto CLEAR;
1417 if (strcmp(service, "ddns") == 0) {
1418 if (action & A_STOP) stop_ddns();
1419 if (action & A_START) start_ddns();
1420 goto CLEAR;
1423 if (strcmp(service, "ntpc") == 0) {
1424 if (action & A_STOP) stop_ntpc();
1425 if (action & A_START) start_ntpc();
1426 goto CLEAR;
1429 if (strcmp(service, "logging") == 0) {
1430 if (action & A_STOP) {
1431 stop_syslog();
1432 stop_cron();
1434 stop_firewall(); start_firewall(); // always restarted
1435 if (action & A_START) {
1436 start_cron();
1437 start_syslog();
1439 goto CLEAR;
1442 if (strcmp(service, "crond") == 0) {
1443 if (action & A_STOP) {
1444 stop_cron();
1446 if (action & A_START) {
1447 start_cron();
1449 goto CLEAR;
1452 if (strcmp(service, "upgrade") == 0) {
1453 if (action & A_START) {
1454 #if TOMATO_SL
1455 stop_usbevent();
1456 stop_smbd();
1457 #endif
1458 stop_ftpd(); // !!TB - FTP Server
1459 stop_samba(); // !!TB - Samba
1460 stop_jffs2();
1461 // stop_cifs();
1462 stop_zebra();
1463 stop_cron();
1464 stop_ntpc();
1465 stop_upnp();
1466 // stop_dhcpc();
1467 killall("rstats", SIGTERM);
1468 killall("buttons", SIGTERM);
1469 stop_syslog();
1470 remove_storage_main(); // !!TB - USB Support
1471 stop_usb(); // !!TB - USB Support
1473 goto CLEAR;
1476 #ifdef TCONFIG_CIFS
1477 if (strcmp(service, "cifs") == 0) {
1478 if (action & A_STOP) stop_cifs();
1479 if (action & A_START) start_cifs();
1480 goto CLEAR;
1482 #endif
1484 #ifdef TCONFIG_JFFS2
1485 if (strcmp(service, "jffs2") == 0) {
1486 if (action & A_STOP) stop_jffs2();
1487 if (action & A_START) start_jffs2();
1488 goto CLEAR;
1490 #endif
1492 if (strcmp(service, "routing") == 0) {
1493 if (action & A_STOP) {
1494 stop_zebra();
1495 do_static_routes(0); // remove old '_saved'
1496 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1498 stop_firewall();
1499 start_firewall();
1500 if (action & A_START) {
1501 do_static_routes(1); // add new
1502 start_zebra();
1503 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1505 goto CLEAR;
1508 if (strcmp(service, "ctnf") == 0) {
1509 if (action & A_START) {
1510 setup_conntrack();
1511 stop_firewall();
1512 start_firewall();
1514 goto CLEAR;
1517 if (strcmp(service, "wan") == 0) {
1518 if (action & A_STOP) {
1519 if (get_wan_proto() == WP_PPPOE) {
1520 stop_dnsmasq();
1521 stop_redial();
1522 stop_singe_pppoe(PPPOE0);
1523 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
1524 sleep(1);
1525 start_pppoe(PPPOE0);
1527 start_dnsmasq();
1529 else {
1530 stop_wan();
1534 if (action & A_START) {
1535 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1537 if (get_wan_proto() == WP_PPPOE) {
1538 stop_singe_pppoe(PPPOE0);
1539 start_pppoe(PPPOE0);
1540 if (nvram_invmatch("ppp_demand", "1")) {
1541 start_redial();
1544 else {
1545 start_wan(BOOT);
1547 sleep(2);
1548 force_to_dial();
1550 goto CLEAR;
1553 if (strcmp(service, "net") == 0) {
1554 if (action & A_STOP) {
1555 stop_wan();
1556 stop_lan();
1557 stop_vlan();
1559 if (action & A_START) {
1560 start_vlan();
1561 start_lan();
1562 start_wan(BOOT);
1564 goto CLEAR;
1567 if (strcmp(service, "rstats") == 0) {
1568 if (action & A_STOP) stop_rstats();
1569 if (action & A_START) start_rstats(0);
1570 goto CLEAR;
1573 if (strcmp(service, "rstatsnew") == 0) {
1574 if (action & A_STOP) stop_rstats();
1575 if (action & A_START) start_rstats(1);
1576 goto CLEAR;
1579 if (strcmp(service, "sched") == 0) {
1580 if (action & A_STOP) stop_sched();
1581 if (action & A_START) start_sched();
1582 goto CLEAR;
1585 // !!TB - USB Support
1586 if (strcmp(service, "usb") == 0) {
1587 if (action & A_STOP) stop_usb();
1588 if (action & A_START) {
1589 start_usb();
1590 // restart Samba and ftp since they may be killed by stop_usb()
1591 restart_nas_services(1);
1593 goto CLEAR;
1596 #ifdef TCONFIG_FTP
1597 // !!TB - FTP Server
1598 if (strcmp(service, "ftpd") == 0) {
1599 if (action & A_STOP) stop_ftpd();
1600 setup_conntrack();
1601 stop_firewall();
1602 start_firewall();
1603 if (action & A_START) start_ftpd();
1604 goto CLEAR;
1606 #endif
1608 #ifdef TCONFIG_SAMBASRV
1609 // !!TB - Samba
1610 if (strcmp(service, "samba") == 0) {
1611 if (action & A_STOP) stop_samba();
1612 if (action & A_START) {
1613 create_passwd();
1614 start_samba();
1616 goto CLEAR;
1618 #endif
1620 #if TOMATO_SL
1621 if (strcmp(service, "smbd") == 0) {
1622 if (action & A_STOP) stop_smbd();
1623 if (action & A_START) start_smbd();
1624 goto CLEAR;
1627 if (strcmp(service, "test1") == 0) {
1628 if (action & A_STOP) stop_test_1();
1629 if (action & A_START) start_test_1();
1630 goto CLEAR;
1632 #endif
1635 CLEAR:
1636 if (next) goto TOP;
1638 // some functions check action_service and must be cleared at end -- zzz
1639 nvram_set("action_service", "");
1642 static void do_service(const char *name, const char *action, int user)
1644 int n;
1645 char s[64];
1647 n = 15;
1648 while (!nvram_match("action_service", "")) {
1649 if (user) {
1650 putchar('*');
1651 fflush(stdout);
1653 else if (--n < 0) break;
1654 sleep(1);
1657 snprintf(s, sizeof(s), "%s-%s", name, action);
1658 nvram_set("action_service", s);
1659 kill(1, SIGUSR1);
1661 n = 15;
1662 while (nvram_match("action_service", s)) {
1663 if (user) {
1664 putchar('.');
1665 fflush(stdout);
1667 else if (--n < 0) break;
1668 sleep(1);
1672 int service_main(int argc, char *argv[])
1674 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1675 do_service(argv[1], argv[2], 1);
1676 printf("\nDone.\n");
1677 return 0;
1680 void start_service(const char *name)
1682 do_service(name, "start", 0);
1685 void stop_service(const char *name)
1687 do_service(name, "stop", 0);