Merge branch 'tomato-ND-usbmod-vpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / services.c
bloba30001d5d110aeea05279df3cb8b1302e6f9ad8f
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 #ifdef TCONFIG_OPENVPN
258 write_vpn_dnsmasq_config(f);
259 #endif
261 fprintf(f, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
265 FILE *af;
267 fflush(f);
268 if ((af = fopen("/etc/dnsmasq.custom", "r")) != NULL) {
269 while ((n = fread(buf, 1, sizeof(buf), af)) > 0) {
270 fwrite(buf, 1, n, f);
272 fclose(af);
277 fclose(f);
279 if (do_dns) {
280 unlink("/etc/resolv.conf");
281 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
284 // RUN:
285 eval("dnsmasq");
287 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
288 f_read_string(dmpid, buf, sizeof(buf));
289 pid_dnsmasq = atol(buf);
293 void stop_dnsmasq(void)
295 if (getpid() != 1) {
296 stop_service("dnsmasq");
297 return;
300 killall("dnsmasq", SIGUSR1); //!!TB - write dnsmasq stats to the system log before stopping
301 pid_dnsmasq = -1;
303 unlink("/etc/resolv.conf");
304 symlink(dmresolv, "/etc/resolv.conf");
306 killall_tk("dnsmasq");
309 void clear_resolv(void)
311 _dprintf("%s\n", __FUNCTION__);
313 f_write(dmresolv, NULL, 0, 0, 0); // blank
316 void dns_to_resolv(void)
318 FILE *f;
319 const dns_list_t *dns;
320 int i;
321 mode_t m;
323 _dprintf("%s\n", __FUNCTION__);
325 m = umask(022); // 077 from pppoecd
326 if ((f = fopen(dmresolv, "w")) != NULL) {
327 dns = get_dns(); // static buffer
328 if (dns->count == 0) {
329 // Put a pseudo DNS IP to trigger Connect On Demand
330 if ((nvram_match("ppp_demand", "1")) &&
331 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
332 fprintf(f, "nameserver 1.1.1.1\n");
335 else {
336 for (i = 0; i < dns->count; i++) {
337 fprintf(f, "nameserver %s\n", inet_ntoa(dns->dns[i]));
340 fclose(f);
342 umask(m);
345 // -----------------------------------------------------------------------------
347 void start_httpd(void)
349 chdir("/www");
350 if (!nvram_match("http_enable", "0")) {
351 xstart("httpd");
353 if (!nvram_match("https_enable", "0")) {
354 xstart("httpd", "-s");
356 chdir("/");
359 void stop_httpd(void)
361 killall_tk("httpd");
364 // -----------------------------------------------------------------------------
366 //!!TB - miniupnpd - most of the code is stolen from Tarifa 034RC1 sources
368 #if 0 // read UUID from /proc/sys/kernel/random/uuid instead
369 void uuidstr_create(char *str)
371 typedef unsigned int u_int32;
372 typedef unsigned short u_int16;
373 typedef unsigned char u_int8;
375 static int uuid_count = 0;
376 u_int d[16];
378 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
379 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]);
380 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
381 &d[6], &d[7], &d[8], &d[9], &d[10], &d[11]);
382 *((int *)&d[12]) = uuid_count++;
384 sprintf(str, "fc4ec57e-b051-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
385 (u_int8)d[6], (u_int8)d[7], (u_int8)d[8], (u_int8)d[9], (u_int8)d[10],
386 (u_int8)d[11], (u_int8)d[12], (u_int8)d[13], (u_int8)d[14], (u_int8)d[15]);
388 #endif //0
390 void start_upnp(void)
392 //!!TB - miniupnpd
393 #if 0
394 if ((nvram_match("upnp_enable", "1")) && (get_wan_proto() != WP_DISABLED)) {
395 #ifdef TEST_MINIUPNP
396 xstart("miniupnpd",
397 "-i", nvram_safe_get("wan_iface"),
398 "-a", nvram_safe_get("lan_ipaddr"),
399 "-p", "5555",
400 "-U");
401 #else
402 xstart("upnp",
403 "-D",
404 "-L", nvram_safe_get("lan_ifname"),
405 "-W", nvram_safe_get("wan_iface"),
406 "-I", nvram_safe_get("upnp_ssdp_interval"),
407 "-A", nvram_safe_get("upnp_max_age"));
409 #endif
410 #else //0
411 char uuid[45];
412 char fname[] = "/etc/miniupnpd.conf";
413 char *lanip;
414 int interval, r;
415 FILE *fp = NULL;
417 if ((!nvram_invmatch("upnp_enable", "0")) & (!nvram_invmatch("upnp_nat_pmp_enable", "0")) || (get_wan_proto() == WP_DISABLED))
418 return;
420 fp = fopen(fname, "w");
421 if(NULL == fp)
422 return;
424 lanip = nvram_safe_get("lan_ipaddr");
426 fprintf(fp, "ext_ifname=%s\n", nvram_safe_get("wan_iface"));
427 fprintf(fp, "listening_ip=%s\n", lanip);
428 fprintf(fp, "port=%s\n", nvram_safe_get("upnp_port"));
429 fprintf(fp, "upnp_forward_chain=upnp\n");
430 fprintf(fp, "upnp_nat_chain=upnp\n");
431 fprintf(fp, "enable_upnp=%s\n", nvram_match("upnp_enable", "1") ? "yes" : "no");
432 fprintf(fp, "enable_natpmp=%s\n", nvram_match("upnp_nat_pmp_enable", "1") ? "yes" : "no");
433 fprintf(fp, "secure_mode=%s\n", nvram_match("upnp_secure_mode", "1") ? "yes" : "no");
434 fprintf(fp, "system_uptime=no\n");
435 fprintf(fp, "notify_interval=%d\n", nvram_get_int("upnp_ssdp_interval"));
437 r = nvram_get_int("upnp_bitrate_up");
438 if (r > 0) fprintf(fp, "bitrate_up=%d\n", r);
439 r = nvram_get_int("upnp_bitrate_down");
440 if (r > 0) fprintf(fp, "bitrate_down=%d\n", r);
442 if (nvram_match("upnp_clean_ruleset_enable", "1")) {
443 interval = nvram_get_int("upnp_clean_ruleset_interval");
444 if (interval < 60) interval = 60;
445 fprintf(fp, "clean_ruleset_interval=%d\n", interval);
446 fprintf(fp, "clean_ruleset_threshold=%d\n", nvram_get_int("upnp_clean_ruleset_threshold"));
448 else
449 fprintf(fp,"clean_ruleset_interval=0\n");
451 if (nvram_match("upnp_mnp", "1")) {
452 int https = nvram_match("https_enable", "1");
453 fprintf(fp, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
454 https ? "s" : "", lanip,
455 nvram_safe_get(https ? "https_lanport" : "http_lanport"));
457 else {
458 // Empty parameters are not included into XML service description
459 fprintf(fp, "presentation_url=\n");
462 f_read_string("/proc/sys/kernel/random/uuid", uuid, sizeof(uuid));
463 fprintf(fp, "uuid=%s\n", uuid);
465 if ((nvram_get_int("upnp_min_port_int") > 0) &&
466 (nvram_get_int("upnp_max_port_int") > 0) &&
467 (nvram_get_int("upnp_min_port_ext") > 0) &&
468 (nvram_get_int("upnp_max_port_ext") > 0)) {
470 fprintf(fp, "allow %s", nvram_safe_get("upnp_min_port_int"));
471 fprintf(fp, "-%s", nvram_safe_get("upnp_max_port_int"));
472 fprintf(fp, " %s/24", lanip);
473 fprintf(fp, " %s", nvram_safe_get("upnp_min_port_ext"));
474 fprintf(fp, "-%s\n", nvram_safe_get("upnp_max_port_ext"));
476 else {
477 // by default allow only redirection of ports above 1024
478 fprintf(fp, "allow 1024-65535 %s/24 1024-65535\n", lanip);
480 fprintf(fp, "deny 0-65535 0.0.0.0/0 0-65535\n");
482 fclose(fp);
484 xstart("miniupnpd", "-f", fname);
485 #endif //0
488 void stop_upnp(void)
490 #if 0
491 //!!TB - miniupnpd
492 #ifdef TEST_MINIUPNP
493 killall_tk("miniupnpd");
494 #else
495 killall_tk("upnp");
496 #endif
497 #else //0
498 killall_tk("miniupnpd");
499 unlink("/var/run/miniupnpd.pid");
500 unlink("/etc/miniupnpd.conf");
501 #endif //0
504 // -----------------------------------------------------------------------------
506 static pid_t pid_crond = -1;
508 void start_cron(void)
510 _dprintf("%s\n", __FUNCTION__);
512 stop_cron();
514 char *argv[] = { "crond", "-l", "9", NULL };
516 if (nvram_contains_word("log_events", "crond")) argv[1] = NULL;
517 _eval(argv, NULL, 0, NULL);
519 if (!nvram_contains_word("debug_norestart", "crond")) {
520 pid_crond = -2; // intentionally fail test_cron()
525 void stop_cron(void)
527 _dprintf("%s\n", __FUNCTION__);
529 pid_crond = -1;
530 killall_tk("crond");
533 // -----------------------------------------------------------------------------
535 // Written by Sparq in 2002/07/16
536 void start_zebra(void)
538 #ifdef TCONFIG_ZEBRA
539 FILE *fp;
541 char *lan_tx = nvram_safe_get("dr_lan_tx");
542 char *lan_rx = nvram_safe_get("dr_lan_rx");
543 char *wan_tx = nvram_safe_get("dr_wan_tx");
544 char *wan_rx = nvram_safe_get("dr_wan_rx");
546 if ((*lan_tx == '0') && (*lan_rx == '0') && (*wan_tx == '0') && (*wan_rx == '0')) {
547 return;
550 // empty
551 if ((fp = fopen("/etc/zebra.conf", "w")) != NULL) {
552 fclose(fp);
556 if ((fp = fopen("/etc/ripd.conf", "w")) != NULL) {
557 char *lan_ifname = nvram_safe_get("lan_ifname");
558 char *wan_ifname = nvram_safe_get("wan_ifname");
560 fprintf(fp, "router rip\n");
561 fprintf(fp, "network %s\n", lan_ifname);
562 fprintf(fp, "network %s\n", wan_ifname);
563 fprintf(fp, "redistribute connected\n");
564 //fprintf(fp, "redistribute static\n");
566 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
567 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
569 fprintf(fp, "interface %s\n", lan_ifname);
570 if (*lan_tx != '0') fprintf(fp, "ip rip send version %s\n", lan_tx);
571 if (*lan_rx != '0') fprintf(fp, "ip rip receive version %s\n", lan_rx);
573 fprintf(fp, "interface %s\n", wan_ifname);
574 if (*wan_tx != '0') fprintf(fp, "ip rip send version %s\n", wan_tx);
575 if (*wan_rx != '0') fprintf(fp, "ip rip receive version %s\n", wan_rx);
577 fprintf(fp, "router rip\n");
578 if (*lan_tx == '0') fprintf(fp, "distribute-list private out %s\n", lan_ifname);
579 if (*lan_rx == '0') fprintf(fp, "distribute-list private in %s\n", lan_ifname);
580 if (*wan_tx == '0') fprintf(fp, "distribute-list private out %s\n", wan_ifname);
581 if (*wan_rx == '0') fprintf(fp, "distribute-list private in %s\n", wan_ifname);
582 fprintf(fp, "access-list private deny any\n");
584 //fprintf(fp, "debug rip events\n");
585 //fprintf(fp, "log file /etc/ripd.log\n");
586 fclose(fp);
589 xstart("zebra", "-d", "-f", "/etc/zebra.conf");
590 xstart("ripd", "-d", "-f", "/etc/ripd.conf");
591 #endif
594 void stop_zebra(void)
596 #ifdef TCONFIG_ZEBRA
597 killall("zebra", SIGTERM);
598 killall("ripd", SIGTERM);
600 unlink("/etc/zebra.conf");
601 unlink("/etc/ripd.conf");
602 #endif
605 // -----------------------------------------------------------------------------
607 void start_syslog(void)
609 #if 1
610 char *argv[12];
611 int argc;
612 char *nv;
613 char rem[256];
614 int n;
615 char s[64];
617 argv[0] = "syslogd";
618 argc = 1;
620 if (nvram_match("log_remote", "1")) {
621 nv = nvram_safe_get("log_remoteip");
622 if (*nv) {
623 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
624 argv[argc++] = "-R";
625 argv[argc++] = rem;
629 if (nvram_match("log_file", "1")) {
630 argv[argc++] = "-L";
631 argv[argc++] = "-s";
632 argv[argc++] = "50";
635 if (argc > 1) {
636 argv[argc] = NULL;
637 _eval(argv, NULL, 0, NULL);
638 usleep(500000);
640 argv[0] = "klogd";
641 argv[1] = NULL;
642 _eval(argv, NULL, 0, NULL);
643 usleep(500000);
645 // used to be available in syslogd -m
646 n = nvram_get_int("log_mark");
647 if (n > 0) {
648 sprintf(s, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
649 (n < 60) ? "*/30" : "0", (n < 120) ? "*" : "*/2");
650 system(s);
654 #else
655 char *argv[12];
656 int argc;
657 char *nv;
658 char rem[256];
660 argv[0] = "syslogd";
661 argv[1] = "-m";
662 argv[2] = nvram_get("log_mark");
663 argc = 3;
665 if (nvram_match("log_remote", "1")) {
666 nv = nvram_safe_get("log_remoteip");
667 if (*nv) {
668 snprintf(rem, sizeof(rem), "%s:%s", nv, nvram_safe_get("log_remoteport"));
669 argv[argc++] = "-R";
670 argv[argc++] = rem;
674 if (nvram_match("log_file", "1")) {
675 argv[argc++] = "-L";
676 argv[argc++] = "-s";
677 argv[argc++] = "50";
680 if (argc > 3) {
681 argv[argc] = NULL;
682 _eval(argv, NULL, 0, NULL);
683 usleep(500000);
685 argv[0] = "klogd";
686 argv[1] = NULL;
687 _eval(argv, NULL, 0, NULL);
688 usleep(500000);
690 #endif
693 void stop_syslog(void)
695 killall("klogd", SIGTERM);
696 killall("syslogd", SIGTERM);
699 // -----------------------------------------------------------------------------
701 static pid_t pid_igmp = -1;
703 void start_igmp_proxy(void)
705 char *p;
707 pid_igmp = -1;
708 if (nvram_match("multicast_pass", "1")) {
709 switch (get_wan_proto()) {
710 case WP_PPPOE:
711 case WP_PPTP:
712 case WP_L2TP:
713 p = "wan_iface";
714 break;
715 default:
716 p = "wan_ifname";
717 break;
719 xstart("igmprt", "-f", "-i", nvram_safe_get(p));
721 if (!nvram_contains_word("debug_norestart", "igmprt")) {
722 pid_igmp = -2;
727 void stop_igmp_proxy(void)
729 pid_igmp = -1;
730 killall("igmprt", SIGTERM);
734 // -----------------------------------------------------------------------------
736 void set_tz(void)
738 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE|FW_NEWLINE, 0644);
741 void start_ntpc(void)
743 set_tz();
745 stop_ntpc();
747 if (nvram_get_int("ntp_updates") >= 0) {
748 xstart("ntpsync", "--init");
752 void stop_ntpc(void)
754 killall("ntpsync", SIGTERM);
757 // -----------------------------------------------------------------------------
759 static void stop_rstats(void)
761 int n;
762 int pid;
764 n = 60;
765 while ((n-- > 0) && ((pid = pidof("rstats")) > 0)) {
766 if (kill(pid, SIGTERM) != 0) break;
767 sleep(1);
771 static void start_rstats(int new)
773 if (nvram_match("rstats_enable", "1")) {
774 stop_rstats();
775 if (new) xstart("rstats", "--new");
776 else xstart("rstats");
780 // -----------------------------------------------------------------------------
782 // !!TB - FTP Server
785 * Return non-zero if we created the directory,
786 * and zero if it already existed.
788 int mkdir_if_none(char *dir)
790 DIR *dp;
791 if (!(dp=opendir(dir))) {
792 umask(0000);
793 mkdir(dir, 0777);
794 return 1;
796 closedir(dp);
797 return 0;
800 char *get_full_storage_path(char *val)
802 static char buf[128];
803 int len;
805 if (val[0] == '/')
806 len = sprintf(buf, "%s", val);
807 else
808 len = sprintf(buf, "%s/%s", MOUNT_ROOT, val);
810 if (len > 1 && buf[len - 1] == '/')
811 buf[len - 1] = 0;
813 return buf;
816 char *nvram_storage_path(char *var)
818 char *val = nvram_safe_get(var);
819 return get_full_storage_path(val);
822 #ifdef TCONFIG_FTP
824 char vsftpd_conf[] = "/etc/vsftpd.conf";
825 char vsftpd_users[] = "/etc/vsftpd.users";
826 char vsftpd_passwd[] = "/etc/vsftpd.passwd";
827 #endif
829 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
830 void start_ftpd(void)
832 #ifdef TCONFIG_FTP
834 char tmp[256];
835 FILE *fp, *f;
837 killall("vsftpd", SIGTERM);
838 if (!nvram_get_int("ftp_enable")) return;
840 mkdir_if_none(vsftpd_users);
841 mkdir_if_none("/var/run/vsftpd");
843 if ((fp = fopen(vsftpd_conf, "w")) == NULL)
844 return;
846 if (nvram_match("ftp_super", "1"))
848 /* rights */
849 sprintf(tmp, "%s/%s", vsftpd_users, "admin");
850 if ((f = fopen(tmp, "w")))
852 fprintf(f,
853 "dirlist_enable=yes\n"
854 "write_enable=yes\n"
855 "download_enable=yes\n");
856 fclose(f);
860 #ifdef TCONFIG_SAMBASRV
861 if (nvram_match("smbd_cset", "utf8"))
862 fprintf(fp, "utf8=yes\n");
863 #endif
865 if (nvram_invmatch("ftp_anonymous", "0"))
867 fprintf(fp,
868 "anon_allow_writable_root=yes\n"
869 "anon_world_readable_only=no\n"
870 "anon_umask=022\n");
872 /* rights */
873 sprintf(tmp, "%s/ftp", vsftpd_users);
874 if ((f = fopen(tmp, "w")))
876 if (nvram_match("ftp_dirlist", "0"))
877 fprintf(f, "dirlist_enable=yes\n");
878 if (nvram_match("ftp_anonymous", "1") ||
879 nvram_match("ftp_anonymous", "3"))
880 fprintf(f, "write_enable=yes\n");
881 if (nvram_match("ftp_anonymous", "1") ||
882 nvram_match("ftp_anonymous", "2"))
883 fprintf(f, "download_enable=yes\n");
884 fclose(f);
886 if (nvram_match("ftp_anonymous", "1") ||
887 nvram_match("ftp_anonymous", "3"))
888 fprintf(fp,
889 "anon_upload_enable=yes\n"
890 "anon_mkdir_write_enable=yes\n"
891 "anon_other_write_enable=yes\n");
892 } else {
893 fprintf(fp, "anonymous_enable=no\n");
896 fprintf(fp,
897 "dirmessage_enable=yes\n"
898 "download_enable=no\n"
899 "dirlist_enable=no\n"
900 "hide_ids=yes\n"
901 "syslog_enable=yes\n"
902 "local_enable=yes\n"
903 "local_umask=022\n"
904 "chmod_enable=no\n"
905 "chroot_local_user=yes\n"
906 "check_shell=no\n"
907 "user_config_dir=%s\n"
908 "passwd_file=%s\n",
909 vsftpd_users, vsftpd_passwd);
911 if (nvram_match("log_ftp", "1")) {
912 fprintf(fp, "log_ftp_protocol=yes\n");
914 else {
915 fprintf(fp, "log_ftp_protocol=no\n");
918 fprintf(fp, "listen=yes\nlisten_port=%s\nbackground=yes\n",
919 nvram_get("ftp_port") ? : "21");
920 fprintf(fp, "max_clients=%s\n", nvram_get("ftp_max") ? : "0");
921 fprintf(fp, "max_per_ip=%s\n", nvram_get("ftp_ipmax") ? : "0");
922 fprintf(fp, "idle_session_timeout=%s\n", nvram_get("ftp_staytimeout") ? : "300");
923 fprintf(fp, "use_sendfile=no\n");
924 //fprintf(fp, "ftpd_banner=Welcome to the %s FTP service.\n", nvram_get("t_model_name") ? : "router");
926 /* bandwidth */
927 fprintf(fp, "anon_max_rate=%d\nlocal_max_rate=%d\n",
928 atoi(nvram_safe_get("ftp_anonrate")) * 1024,
929 atoi(nvram_safe_get("ftp_rate")) * 1024);
931 fprintf(fp, "%s\n\n", nvram_safe_get("ftp_custom"));
933 fclose(fp);
935 /* prepare passwd file and default users */
936 if ((fp = fopen(vsftpd_passwd, "w")) == NULL)
937 return;
939 fprintf(fp, /* anonymous, admin, nobody */
940 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
941 "%s:%s:0:0:root:/:/sbin/nologin\n"
942 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
943 nvram_storage_path("ftp_anonroot"), "admin",
944 nvram_match("ftp_super", "1") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
945 MOUNT_ROOT);
947 char *buf;
948 char *p, *q;
949 char *user, *pass, *rights;
951 if ((buf = strdup(nvram_safe_get("ftp_users"))) != NULL)
954 username<password<rights
955 rights:
956 Read/Write
957 Read Only
958 View Only
959 Private
961 p = buf;
962 while ((q = strsep(&p, ">")) != NULL) {
963 if (vstrsep(q, "<", &user, &pass, &rights) != 3) continue;
964 if (!user || !pass) continue;
966 /* directory */
967 if (strncmp(rights, "Private", 7) == 0)
969 sprintf(tmp, "%s/%s", nvram_storage_path("ftp_pvtroot"), user);
970 mkdir_if_none(tmp);
972 else
973 sprintf(tmp, "%s", nvram_storage_path("ftp_pubroot"));
975 fprintf(fp, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
976 user, crypt(pass, "$1$"), user, tmp);
978 /* rights */
979 sprintf(tmp, "%s/%s", vsftpd_users, user);
980 if ((f = fopen(tmp, "w")))
982 tmp[0] = 0;
983 if (nvram_invmatch("ftp_dirlist", "1"))
984 strcat(tmp, "dirlist_enable=yes\n");
985 if (strstr(rights, "Read") || !strcmp(rights, "Private"))
986 strcat(tmp, "download_enable=yes\n");
987 if (strstr(rights, "Write") || !strncmp(rights, "Private", 7))
988 strcat(tmp, "write_enable=yes\n");
990 fputs(tmp, f);
991 fclose(f);
994 free(buf);
997 fclose(fp);
999 eval("vsftpd");
1000 #endif
1003 void stop_ftpd(void)
1005 #ifdef TCONFIG_FTP
1006 killall("vsftpd", SIGTERM);
1007 unlink(vsftpd_passwd);
1008 unlink(vsftpd_conf);
1009 eval("rm", "-rf", vsftpd_users);
1010 #endif
1013 // -----------------------------------------------------------------------------
1015 // !!TB - Samba
1017 #ifdef TCONFIG_SAMBASRV
1018 void kill_samba(int sig)
1020 killall("smbd", sig);
1021 killall("nmbd", sig);
1023 #endif
1025 void start_samba(void)
1027 #ifdef TCONFIG_SAMBASRV
1029 FILE *fp;
1030 DIR *dir = NULL;
1031 struct dirent *dp;
1032 char nlsmod[15];
1033 int mode;
1035 mode = nvram_get_int("smbd_enable");
1036 if (!mode || !nvram_invmatch("lan_hostname", ""))
1037 return;
1039 if ((fp = fopen("/etc/smb.conf", "w")) == NULL) {
1040 perror("/etc/smb.conf");
1041 return;
1044 fprintf(fp, "[global]\n"
1045 " interfaces = %s\n"
1046 " bind interfaces only = yes\n"
1047 " workgroup = %s\n"
1048 " server string = %s\n"
1049 " guest account = nobody\n"
1050 " security = %s\n"
1051 " browseable = yes\n"
1052 " guest ok = yes\n"
1053 " guest only = no\n"
1054 " log level = %d\n"
1055 " syslog only = yes\n"
1056 " syslog = 1\n"
1057 " encrypt passwords = yes\n"
1058 " local master = %s\n"
1059 " preserve case = yes\n"
1060 " short preserve case = yes\n",
1061 nvram_get("lan_ifname") ? : "br0",
1062 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1063 nvram_get("router_name") ? : "Tomato",
1064 mode == 2 ? "user" : "share",
1065 nvram_get_int("smbd_loglevel"),
1066 nvram_get_int("smbd_master") ? "yes" : "no"
1069 if (nvram_invmatch("smbd_cpage", "")) {
1070 char *cp = nvram_get("smbd_cpage");
1072 fprintf(fp, " client code page = %s\n", cp);
1073 sprintf(nlsmod, "nls_cp%s", cp);
1075 cp = nvram_get("smbd_nlsmod");
1076 if ((cp) && (*cp != 0) && (strcmp(cp, nlsmod) != 0))
1077 modprobe_r(cp);
1079 modprobe(nlsmod);
1080 nvram_set("smbd_nlsmod", nlsmod);
1083 if (nvram_match("smbd_cset", "utf8"))
1084 fprintf(fp, " coding system = utf8\n");
1085 else if (nvram_invmatch("smbd_cset", ""))
1086 fprintf(fp, " character set = %s\n", nvram_get("smbd_cset"));
1088 fprintf(fp, "%s\n\n", nvram_safe_get("smbd_custom"));
1090 /* configure shares */
1092 char *buf;
1093 char *p, *q;
1094 char *name, *path, *comment, *writeable, *hidden;
1095 int cnt = 0;
1097 if ((buf = strdup(nvram_safe_get("smbd_shares"))) != NULL)
1099 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1101 p = buf;
1102 while ((q = strsep(&p, ">")) != NULL) {
1103 if (vstrsep(q, "<", &name, &path, &comment, &writeable, &hidden) != 5) continue;
1104 if (!path || !name) continue;
1106 /* share name */
1107 fprintf(fp, "\n[%s]\n", name);
1109 /* path */
1110 fprintf(fp, " path = %s\n", path);
1112 /* access level */
1113 if (!strcmp(writeable, "1"))
1114 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1115 if (!strcmp(hidden, "1"))
1116 fprintf(fp, " browseable = no\n");
1118 /* comment */
1119 if (comment)
1120 fprintf(fp, " comment = %s\n", comment);
1122 cnt++;
1124 free(buf);
1127 /* share everything below MOUNT_ROOT */
1128 if (nvram_get_int("smbd_autoshare") && (dir = opendir(MOUNT_ROOT))) {
1129 while ((dp = readdir(dir))) {
1130 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
1132 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1133 fprintf(fp, "\n[%s]\n path = %s/%s\n",
1134 dp->d_name, MOUNT_ROOT, dp->d_name);
1135 if (nvram_match("smbd_autoshare", "3")) // Hidden
1136 fprintf(fp, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1137 dp->d_name, MOUNT_ROOT, dp->d_name);
1138 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1139 fprintf(fp, " writable = yes\n force user = %s\n", "root");
1141 cnt++;
1145 if (dir) closedir(dir);
1147 if (cnt == 0) {
1148 /* by default share MOUNT_ROOT as read-only */
1149 fprintf(fp, "\n[share]\n"
1150 " path = %s\n"
1151 " writable = no\n",
1152 MOUNT_ROOT);
1155 fclose(fp);
1157 mkdir_if_none("/var/run/samba");
1158 mkdir_if_none("/etc/samba");
1160 /* write smbpasswd */
1161 eval("smbpasswd", "-a", "nobody", "\"\"");
1162 if (mode == 2) {
1163 char *smbd_user;
1164 if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
1165 smbd_user = "nas";
1166 eval("smbpasswd", "-a", smbd_user, nvram_safe_get("smbd_passwd"));
1169 kill_samba(SIGHUP);
1170 int ret1 = 0, ret2 = 0;
1171 /* start samba if it's not already running */
1172 if (pidof("nmbd") <= 0)
1173 ret1 = eval("nmbd", "-D");
1174 if (pidof("smbd") <= 0)
1175 ret2 = eval("smbd", "-D");
1177 if (ret1 || ret2) kill_samba(SIGTERM);
1178 #endif
1181 void stop_samba(void)
1183 #ifdef TCONFIG_SAMBASRV
1184 kill_samba(SIGTERM);
1185 sleep(2); /* wait for smbd to finish */
1187 if (nvram_invmatch("smbd_nlsmod", "")) {
1188 modprobe_r(nvram_get("smbd_nlsmod"));
1189 nvram_set("smbd_nlsmod", "");
1192 /* clean up */
1193 unlink("/var/log/smb");
1194 unlink("/var/log/nmb");
1195 eval("rm", "-rf", "/var/run/samba");
1196 #endif
1199 void restart_nas_services(int start)
1201 /* restart all NAS applications */
1202 #ifdef TCONFIG_SAMBASRV
1203 if (start && nvram_get_int("smbd_enable"))
1204 start_samba();
1205 else
1206 kill_samba(SIGTERM);
1207 #endif
1208 #ifdef TCONFIG_FTP
1209 if (start && nvram_get_int("ftp_enable"))
1210 start_ftpd();
1211 else
1212 killall("vsftpd", SIGTERM);
1213 #endif
1216 // -----------------------------------------------------------------------------
1218 static void _check(pid_t *pid, const char *name, void (*func)(void) )
1220 if (*pid != -1) {
1221 if (kill(*pid, 0) != 0) {
1222 if ((*pid = pidof(name)) == -1) func();
1227 void check_services(void)
1229 _check(&pid_dnsmasq, "dnsmasq", start_dnsmasq);
1230 _check(&pid_crond, "crond", start_cron);
1231 _check(&pid_igmp, "igmprt", start_igmp_proxy);
1234 // -----------------------------------------------------------------------------
1236 void start_services(void)
1238 static int once = 1;
1240 if (once) {
1241 once = 0;
1243 create_passwd();
1244 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1245 if (nvram_match("sshd_eas", "1")) start_sshd();
1248 start_syslog();
1249 #if TOMATO_SL
1250 start_usbevent();
1251 #endif
1252 start_nas();
1253 start_zebra();
1254 start_dnsmasq();
1255 start_cifs();
1256 start_httpd();
1257 start_cron();
1258 start_upnp();
1259 start_rstats(0);
1260 start_sched();
1261 #ifdef TCONFIG_SAMBA
1262 start_smbd();
1263 #endif
1264 start_samba(); // !!TB - Samba
1265 start_ftpd(); // !!TB - FTP Server
1266 #ifdef TCONFIG_OPENVPN
1267 start_vpn_eas();
1268 #endif
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();
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 if (strcmp(act, "start") == 0) action = A_START;
1321 else if (strcmp(act, "stop") == 0) action = A_STOP;
1322 else if (strcmp(act, "restart") == 0) action = A_RESTART;
1323 else action = 0;
1325 _dprintf("%s %s service=%s action=%s\n", __FILE__, __FUNCTION__, service, act);
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, "admin") == 0) {
1410 if (action & A_STOP) {
1411 stop_sshd();
1412 stop_telnetd();
1413 stop_httpd();
1415 stop_firewall(); start_firewall(); // always restarted
1416 if (action & A_START) {
1417 start_httpd();
1418 create_passwd();
1419 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1420 if (nvram_match("sshd_eas", "1")) start_sshd();
1422 goto CLEAR;
1425 if (strcmp(service, "ddns") == 0) {
1426 if (action & A_STOP) stop_ddns();
1427 if (action & A_START) start_ddns();
1428 goto CLEAR;
1431 if (strcmp(service, "ntpc") == 0) {
1432 if (action & A_STOP) stop_ntpc();
1433 if (action & A_START) start_ntpc();
1434 goto CLEAR;
1437 if (strcmp(service, "logging") == 0) {
1438 if (action & A_STOP) {
1439 stop_syslog();
1440 stop_cron();
1442 stop_firewall(); start_firewall(); // always restarted
1443 if (action & A_START) {
1444 start_cron();
1445 start_syslog();
1447 goto CLEAR;
1450 if (strcmp(service, "crond") == 0) {
1451 if (action & A_STOP) {
1452 stop_cron();
1454 if (action & A_START) {
1455 start_cron();
1457 goto CLEAR;
1460 if (strcmp(service, "upgrade") == 0) {
1461 if (action & A_START) {
1462 #if TOMATO_SL
1463 stop_usbevent();
1464 stop_smbd();
1465 #endif
1466 stop_ftpd(); // !!TB - FTP Server
1467 stop_samba(); // !!TB - Samba
1468 stop_jffs2();
1469 // stop_cifs();
1470 stop_zebra();
1471 stop_cron();
1472 stop_ntpc();
1473 stop_upnp();
1474 // stop_dhcpc();
1475 killall("rstats", SIGTERM);
1476 killall("buttons", SIGTERM);
1477 stop_syslog();
1478 remove_storage_main(); // !!TB - USB Support
1479 stop_usb(); // !!TB - USB Support
1481 goto CLEAR;
1484 #ifdef TCONFIG_CIFS
1485 if (strcmp(service, "cifs") == 0) {
1486 if (action & A_STOP) stop_cifs();
1487 if (action & A_START) start_cifs();
1488 goto CLEAR;
1490 #endif
1492 #ifdef TCONFIG_JFFS2
1493 if (strcmp(service, "jffs2") == 0) {
1494 if (action & A_STOP) stop_jffs2();
1495 if (action & A_START) start_jffs2();
1496 goto CLEAR;
1498 #endif
1500 if (strcmp(service, "routing") == 0) {
1501 if (action & A_STOP) {
1502 stop_zebra();
1503 do_static_routes(0); // remove old '_saved'
1504 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1506 stop_firewall();
1507 start_firewall();
1508 if (action & A_START) {
1509 do_static_routes(1); // add new
1510 start_zebra();
1511 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1513 goto CLEAR;
1516 if (strcmp(service, "ctnf") == 0) {
1517 if (action & A_START) {
1518 setup_conntrack();
1519 stop_firewall();
1520 start_firewall();
1522 goto CLEAR;
1525 if (strcmp(service, "wan") == 0) {
1526 if (action & A_STOP) {
1527 if (get_wan_proto() == WP_PPPOE) {
1528 stop_dnsmasq();
1529 stop_redial();
1530 stop_singe_pppoe(PPPOE0);
1531 if (((action & A_START) == 0) && (nvram_match("ppp_demand", "1"))) {
1532 sleep(1);
1533 start_pppoe(PPPOE0);
1535 start_dnsmasq();
1537 else {
1538 stop_wan();
1542 if (action & A_START) {
1543 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1545 if (get_wan_proto() == WP_PPPOE) {
1546 stop_singe_pppoe(PPPOE0);
1547 start_pppoe(PPPOE0);
1548 if (nvram_invmatch("ppp_demand", "1")) {
1549 start_redial();
1552 else {
1553 start_wan(BOOT);
1555 sleep(2);
1556 force_to_dial();
1558 goto CLEAR;
1561 if (strcmp(service, "net") == 0) {
1562 if (action & A_STOP) {
1563 stop_wan();
1564 stop_lan();
1565 stop_vlan();
1567 if (action & A_START) {
1568 start_vlan();
1569 start_lan();
1570 start_wan(BOOT);
1572 goto CLEAR;
1575 if (strcmp(service, "rstats") == 0) {
1576 if (action & A_STOP) stop_rstats();
1577 if (action & A_START) start_rstats(0);
1578 goto CLEAR;
1581 if (strcmp(service, "rstatsnew") == 0) {
1582 if (action & A_STOP) stop_rstats();
1583 if (action & A_START) start_rstats(1);
1584 goto CLEAR;
1587 if (strcmp(service, "sched") == 0) {
1588 if (action & A_STOP) stop_sched();
1589 if (action & A_START) start_sched();
1590 goto CLEAR;
1593 // !!TB - USB Support
1594 if (strcmp(service, "usb") == 0) {
1595 if (action & A_STOP) stop_usb();
1596 if (action & A_START) {
1597 start_usb();
1598 // restart Samba and ftp since they may be killed by stop_usb()
1599 restart_nas_services(1);
1601 goto CLEAR;
1604 #ifdef TCONFIG_FTP
1605 // !!TB - FTP Server
1606 if (strcmp(service, "ftpd") == 0) {
1607 if (action & A_STOP) stop_ftpd();
1608 setup_conntrack();
1609 stop_firewall();
1610 start_firewall();
1611 if (action & A_START) start_ftpd();
1612 goto CLEAR;
1614 #endif
1616 #ifdef TCONFIG_SAMBASRV
1617 // !!TB - Samba
1618 if (strcmp(service, "samba") == 0) {
1619 if (action & A_STOP) stop_samba();
1620 if (action & A_START) {
1621 create_passwd();
1622 start_samba();
1624 goto CLEAR;
1626 #endif
1628 #if TOMATO_SL
1629 if (strcmp(service, "smbd") == 0) {
1630 if (action & A_STOP) stop_smbd();
1631 if (action & A_START) start_smbd();
1632 goto CLEAR;
1635 if (strcmp(service, "test1") == 0) {
1636 if (action & A_STOP) stop_test_1();
1637 if (action & A_START) start_test_1();
1638 goto CLEAR;
1640 #endif
1642 #ifdef TCONFIG_OPENVPN
1643 if (strncmp(service, "vpnclient", 9) == 0) {
1644 if (action & A_STOP) stop_vpnclient(atoi(&service[9]));
1645 if (action & A_START) start_vpnclient(atoi(&service[9]));
1646 goto CLEAR;
1649 if (strncmp(service, "vpnserver", 9) == 0) {
1650 if (action & A_STOP) stop_vpnserver(atoi(&service[9]));
1651 if (action & A_START) start_vpnserver(atoi(&service[9]));
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) break;
1689 sleep(1);
1693 int service_main(int argc, char *argv[])
1695 if (argc != 3) usage_exit(argv[0], "<service> <action>");
1696 do_service(argv[1], argv[2], 1);
1697 printf("\nDone.\n");
1698 return 0;
1701 void start_service(const char *name)
1703 do_service(name, "start", 0);
1706 void stop_service(const char *name)
1708 do_service(name, "stop", 0);