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
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
38 #include <arpa/inet.h>
44 #include <sys/mount.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;
66 const char *router_ip
;
67 const char *lan_ifname
;
71 char *mac
, *ip
, *name
;
85 start_service("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;
103 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
104 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
108 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
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");
120 do_dhcpd
= nvram_match("lan_proto", "dhcp");
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
);
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.
136 strcpy(sdhcp_lease
, "2m");
140 // pass the dns directly
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
);
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");
161 "dhcp-option=3,%s\n" // gateway
162 "dhcp-lease-max=%d\n",
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
);
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
);
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
) {
194 if (n
> sizeof(buf
)-1) {
203 if ((e
= strchr(buf
, '<')) == NULL
) continue;
208 if ((e
= strchr(ip
, '<')) == NULL
) continue;
210 if (strchr(ip
, '.') == NULL
) {
212 if ((ipn
<= 0) || (ipn
> 255)) continue;
213 sprintf(ipbuf
, "%s%d", lan
, ipn
);
217 if (inet_addr(ip
) == INADDR_NONE
) continue;
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
);
235 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
237 fappend(f
, "/etc/dnsmasq.custom");
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");
252 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
253 f_read_string(dmpid
, buf
, sizeof(buf
));
254 pid_dnsmasq
= atol(buf
);
260 void stop_dnsmasq(void)
265 stop_service("dnsmasq");
271 unlink("/etc/resolv.conf");
272 symlink(dmresolv
, "/etc/resolv.conf");
274 killall_tk("dnsmasq");
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)
289 const dns_list_t
*dns
;
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");
306 for (i
= 0; i
< dns
->count
; i
++) {
307 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
]));
315 // -----------------------------------------------------------------------------
317 void start_httpd(void)
320 if (!nvram_match("http_enable", "0")) {
323 if (!nvram_match("https_enable", "0")) {
324 xstart("httpd", "-s");
329 void stop_httpd(void)
334 // -----------------------------------------------------------------------------
336 void start_upnp(void)
338 if (get_wan_proto() == WP_DISABLED
) return;
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");
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");
360 "listening_ip=%s/%s\n"
365 "upnp_forward_chain=upnp\n"
366 "upnp_nat_chain=upnp\n"
367 "notify_interval=%d\n"
368 "system_uptime=yes\n"
371 nvram_safe_get("wan_iface"),
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;
384 "clean_ruleset_interval=%d\n"
385 "clean_ruleset_threshold=%d\n",
387 nvram_get_int("upnp_clean_threshold")
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"));
400 // Empty parameters are not included into XML service description
401 fprintf(f
, "presentation_url=\n");
405 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
406 fprintf(f
, "uuid=%s\n", uuid
);
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) {
414 "allow %d-%d %s/%s %d-%d\n",
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");
429 xstart("miniupnpd", "-f", "/etc/upnp/config");
434 if (nvram_get_int("upnp_enable")) {
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"));
448 killall_tk("miniupnpd");
454 // -----------------------------------------------------------------------------
456 static pid_t pid_crond
= -1;
458 void start_cron(void)
460 char *argv
[] = { "crond", "-l", "9", NULL
};
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")) {
478 // -----------------------------------------------------------------------------
480 // Written by Sparq in 2002/07/16
481 void start_zebra(void)
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')) {
496 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
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");
534 xstart("zebra", "-d");
535 xstart("ripd", "-d");
539 void stop_zebra(void)
542 killall("zebra", SIGTERM
);
543 killall("ripd", SIGTERM
);
545 unlink("/etc/zebra.conf");
546 unlink("/etc/ripd.conf");
550 // -----------------------------------------------------------------------------
552 void start_syslog(void)
565 if (nvram_match("log_remote", "1")) {
566 nv
= nvram_safe_get("log_remoteip");
568 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
574 if (nvram_match("log_file", "1")) {
582 _eval(argv
, NULL
, 0, NULL
);
587 _eval(argv
, NULL
, 0, NULL
);
590 // used to be available in syslogd -m
591 n
= nvram_get_int("log_mark");
593 sprintf(s
, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
594 (n
< 60) ? "*/30" : "0", (n
< 120) ? "*" : "*/2");
598 system("cru d syslogdmark");
610 argv
[2] = nvram_get("log_mark");
613 if (nvram_match("log_remote", "1")) {
614 nv
= nvram_safe_get("log_remoteip");
616 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
622 if (nvram_match("log_file", "1")) {
630 _eval(argv
, NULL
, 0, NULL
);
635 _eval(argv
, NULL
, 0, NULL
);
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)
657 if (nvram_match("multicast_pass", "1")) {
658 switch (get_wan_proto()) {
671 if (f_exists("/etc/igmp.alt")) {
672 xstart("igmpproxy", "/etc/igmp.alt");
674 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
677 "phyint %s upstream\n"
679 "phyint %s downstream ratelimit 0\n",
681 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
682 nvram_safe_get("lan_ifname"));
684 xstart("igmpproxy", "/etc/igmp.conf");
689 if (!nvram_contains_word("debug_norestart", "igmprt")) {
695 void stop_igmp_proxy(void)
698 killall("igmpproxy", SIGTERM
);
702 // -----------------------------------------------------------------------------
706 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
709 void start_ntpc(void)
715 if (nvram_get_int("ntp_updates") >= 0) {
716 xstart("ntpsync", "--init");
722 killall("ntpsync", SIGTERM
);
725 // -----------------------------------------------------------------------------
727 static void stop_rstats(void)
733 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
734 if (kill(pid
, SIGTERM
) != 0) break;
739 static void start_rstats(int new)
741 if (nvram_match("rstats_enable", "1")) {
743 if (new) xstart("rstats", "--new");
744 else xstart("rstats");
748 // -----------------------------------------------------------------------------
754 * Return non-zero if we created the directory,
755 * and zero if it already existed.
757 int mkdir_if_none(char *dir
)
760 if (!(dp
=opendir(dir
))) {
769 char *get_full_storage_path(char *val
)
771 static char buf
[128];
775 len
= sprintf(buf
, "%s", val
);
777 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
779 if (len
> 1 && buf
[len
- 1] == '/')
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
794 char vsftpd_conf
[] = "/etc/vsftpd.conf";
795 char vsftpd_users
[] = "/etc/vsftpd.users";
796 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
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
);
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
)
816 if (nvram_get_int("ftp_super"))
819 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
820 if ((f
= fopen(tmp
, "w")))
823 "dirlist_enable=yes\n"
825 "download_enable=yes\n");
830 #ifdef TCONFIG_SAMBASRV
831 if (nvram_match("smbd_cset", "utf8"))
832 fprintf(fp
, "utf8=yes\n");
835 if (nvram_invmatch("ftp_anonymous", "0"))
838 "anon_allow_writable_root=yes\n"
839 "anon_world_readable_only=no\n"
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");
856 if (nvram_match("ftp_anonymous", "1") ||
857 nvram_match("ftp_anonymous", "3"))
859 "anon_upload_enable=yes\n"
860 "anon_mkdir_write_enable=yes\n"
861 "anon_other_write_enable=yes\n");
863 fprintf(fp
, "anonymous_enable=no\n");
867 "dirmessage_enable=yes\n"
868 "download_enable=no\n"
869 "dirlist_enable=no\n"
871 "syslog_enable=yes\n"
875 "chroot_local_user=yes\n"
877 "log_ftp_protocol=%s\n"
878 "user_config_dir=%s\n"
885 "idle_session_timeout=%s\n"
888 "local_max_rate=%d\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"));
902 /* prepare passwd file and default users */
903 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
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",
916 char *user
, *pass
, *rights
;
918 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
921 username<password<rights
929 while ((q
= strsep(&p
, ">")) != NULL
) {
930 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
931 if (!user
|| !pass
) continue;
934 if (strncmp(rights
, "Private", 7) == 0)
936 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
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
);
946 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
947 if ((f
= fopen(tmp
, "w")))
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");
965 killall("vsftpd", SIGHUP
);
967 /* start vsftpd if it's not already running */
968 if (pidof("vsftpd") <= 0)
973 void start_ftpd(void)
976 int fd
= file_lock("usb");
977 do_start_stop_ftpd(0, 1);
985 int fd
= file_lock("usb");
986 do_start_stop_ftpd(1, 0);
987 unlink(vsftpd_passwd
);
989 eval("rm", "-rf", vsftpd_users
);
994 // -----------------------------------------------------------------------------
998 #ifdef TCONFIG_SAMBASRV
999 void kill_samba(int sig
)
1001 killall("smbd", sig
);
1002 killall("nmbd", sig
);
1006 #ifdef TCONFIG_SAMBASRV
1007 static void do_start_stop_samba(int stop
, int start
)
1009 if (stop
) kill_samba(SIGTERM
);
1017 mode
= nvram_get_int("smbd_enable");
1018 if (!start
|| !mode
|| !nvram_invmatch("lan_hostname", ""))
1021 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1024 fprintf(fp
, "[global]\n"
1025 " interfaces = %s\n"
1026 " bind interfaces only = yes\n"
1028 " server string = %s\n"
1029 " guest account = nobody\n"
1031 " browseable = yes\n"
1033 " guest only = no\n"
1035 " syslog only = yes\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";
1050 " local master = %s\n"
1051 " preferred master = %s\n",
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))
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 */
1080 char *name
, *path
, *comment
, *writeable
, *hidden
;
1083 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1085 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1088 while ((q
= strsep(&p
, ">")) != NULL
) {
1089 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1090 if (!path
|| !name
) continue;
1093 fprintf(fp
, "\n[%s]\n", name
);
1096 fprintf(fp
, " path = %s\n", path
);
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");
1106 fprintf(fp
, " comment = %s\n", comment
);
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");
1131 if (dir
) closedir(dir
);
1134 /* by default share MOUNT_ROOT as read-only */
1135 fprintf(fp
, "\n[share]\n"
1143 mkdir_if_none("/var/run/samba");
1144 mkdir_if_none("/etc/samba");
1146 /* write smbpasswd */
1147 eval("smbpasswd", "-a", "nobody", "\"\"");
1150 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1152 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
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
);
1167 void start_samba(void)
1169 #ifdef TCONFIG_SAMBASRV
1170 int fd
= file_lock("usb");
1171 do_start_stop_samba(0, 1);
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", "");
1189 unlink("/var/log/smb");
1190 unlink("/var/log/nmb");
1191 eval("rm", "-rf", "/var/run/samba");
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);
1206 do_start_stop_samba(1, 0);
1209 if (start
&& nvram_get_int("ftp_enable"))
1210 do_start_stop_ftpd(0, 1);
1212 do_start_stop_ftpd(1, 0);
1215 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1217 #endif // TCONFIG_USB
1219 // -----------------------------------------------------------------------------
1221 static void _check(pid_t
*pid
, const char *name
, void (*func
)(void) )
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;
1247 if (nvram_get_int("telnetd_eas")) start_telnetd();
1248 if (nvram_get_int("sshd_eas")) start_sshd();
1261 //start_upnp(); //!!TB - already in start_wan_done()
1264 #ifdef TCONFIG_SAMBA
1267 start_samba(); // !!TB - Samba
1268 start_ftpd(); // !!TB - FTP Server
1271 void stop_services(void)
1275 stop_ftpd(); // !!TB - FTP Server
1276 stop_samba(); // !!TB - Samba
1277 #ifdef TCONFIG_SAMBA
1282 //stop_upnp(); //!!TB - moved to stop_wan()
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;
1309 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1313 act
= strsep(&next
, ",");
1314 service
= strsep(&act
, "-");
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
;
1328 if (strcmp(service
, "dhcpc") == 0) {
1329 if (action
& A_STOP
) stop_dhcpc();
1330 if (action
& A_START
) start_dhcpc();
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
) {
1343 if (strcmp(service
, "firewall") == 0) {
1344 if (action
& A_STOP
) {
1348 if (action
& A_START
) {
1355 if (strcmp(service
, "restrict") == 0) {
1356 if (action
& A_STOP
) {
1359 if (action
& A_START
) {
1360 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1364 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1366 if (nvram_get_int("rrules_radio") < 0) {
1367 if (!get_radio()) eval("radio", "on");
1374 if (strcmp(service
, "qos") == 0) {
1375 if (action
& A_STOP
) {
1378 stop_firewall(); start_firewall(); // always restarted
1379 if (action
& A_START
) {
1381 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1386 if (strcmp(service
, "upnp") == 0) {
1387 if (action
& A_STOP
) {
1390 stop_firewall(); start_firewall(); // always restarted
1391 if (action
& A_START
) {
1397 if (strcmp(service
, "telnetd") == 0) {
1398 if (action
& A_STOP
) stop_telnetd();
1399 if (action
& A_START
) start_telnetd();
1403 if (strcmp(service
, "sshd") == 0) {
1404 if (action
& A_STOP
) stop_sshd();
1405 if (action
& A_START
) start_sshd();
1409 if (strcmp(service
, "httpd") == 0) {
1410 if (action
& A_STOP
) stop_httpd();
1411 if (action
& A_START
) start_httpd();
1415 if (strcmp(service
, "admin") == 0) {
1416 if (action
& A_STOP
) {
1421 stop_firewall(); start_firewall(); // always restarted
1422 if (action
& A_START
) {
1425 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1426 if (nvram_match("sshd_eas", "1")) start_sshd();
1431 if (strcmp(service
, "ddns") == 0) {
1432 if (action
& A_STOP
) stop_ddns();
1433 if (action
& A_START
) start_ddns();
1437 if (strcmp(service
, "ntpc") == 0) {
1438 if (action
& A_STOP
) stop_ntpc();
1439 if (action
& A_START
) start_ntpc();
1443 if (strcmp(service
, "logging") == 0) {
1444 if (action
& A_STOP
) {
1448 stop_firewall(); start_firewall(); // always restarted
1449 if (action
& A_START
) {
1456 if (strcmp(service
, "crond") == 0) {
1457 if (action
& A_STOP
) {
1460 if (action
& A_START
) {
1466 if (strcmp(service
, "upgrade") == 0) {
1467 if (action
& A_START
) {
1472 stop_ftpd(); // !!TB - FTP Server
1473 stop_samba(); // !!TB - Samba
1481 killall("rstats", SIGTERM
);
1482 killall("buttons", SIGTERM
);
1484 remove_storage_main(); // !!TB - USB Support
1485 stop_usb(); // !!TB - USB Support
1491 if (strcmp(service
, "cifs") == 0) {
1492 if (action
& A_STOP
) stop_cifs();
1493 if (action
& A_START
) start_cifs();
1498 #ifdef TCONFIG_JFFS2
1499 if (strcmp(service
, "jffs2") == 0) {
1500 if (action
& A_STOP
) stop_jffs2();
1501 if (action
& A_START
) start_jffs2();
1506 if (strcmp(service
, "routing") == 0) {
1507 if (action
& A_STOP
) {
1509 do_static_routes(0); // remove old '_saved'
1510 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1514 if (action
& A_START
) {
1515 do_static_routes(1); // add new
1517 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1522 if (strcmp(service
, "ctnf") == 0) {
1523 if (action
& A_START
) {
1531 if (strcmp(service
, "wan") == 0) {
1532 if (action
& A_STOP
) {
1533 if (get_wan_proto() == WP_PPPOE
) {
1536 stop_singe_pppoe(PPPOE0
);
1537 if (((action
& A_START
) == 0) && (nvram_match("ppp_demand", "1"))) {
1539 start_pppoe(PPPOE0
);
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")) {
1567 if (strcmp(service
, "net") == 0) {
1568 if (action
& A_STOP
) {
1573 if (action
& A_START
) {
1581 if (strcmp(service
, "rstats") == 0) {
1582 if (action
& A_STOP
) stop_rstats();
1583 if (action
& A_START
) start_rstats(0);
1587 if (strcmp(service
, "rstatsnew") == 0) {
1588 if (action
& A_STOP
) stop_rstats();
1589 if (action
& A_START
) start_rstats(1);
1593 if (strcmp(service
, "sched") == 0) {
1594 if (action
& A_STOP
) stop_sched();
1595 if (action
& A_START
) start_sched();
1600 // !!TB - USB Support
1601 if (strcmp(service
, "usb") == 0) {
1602 if (action
& A_STOP
) stop_usb();
1603 if (action
& A_START
) {
1605 // restart Samba and ftp since they may be killed by stop_usb()
1606 restart_nas_services(1);
1613 // !!TB - FTP Server
1614 if (strcmp(service
, "ftpd") == 0) {
1615 if (action
& A_STOP
) stop_ftpd();
1619 if (action
& A_START
) start_ftpd();
1624 #ifdef TCONFIG_SAMBASRV
1626 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
1627 if (action
& A_STOP
) stop_samba();
1628 if (action
& A_START
) {
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
)
1649 while (!nvram_match("action_service", "")) {
1654 else if (--n
< 0) break;
1658 snprintf(s
, sizeof(s
), "%s-%s", name
, action
);
1659 nvram_set("action_service", s
);
1663 while (nvram_match("action_service", s
)) {
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");
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);