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 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
110 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
113 "resolv-file=%s\n" // the real stuff is here
114 "addn-hosts=%s\n" // "
115 "expand-hosts\n" // expand hostnames in hosts file
116 "min-port=%u\n", // min port used for random src port
117 dmresolv
, dmhosts
, n
);
118 do_dns
= nvram_match("dhcpd_dmdns", "1");
120 for (n
= 0 ; n
< dns
->count
; ++n
) {
121 if (dns
->dns
[n
].port
!= 53) {
122 fprintf(f
, "server=%s#%u\n", inet_ntoa(dns
->dns
[n
].addr
), dns
->dns
[n
].port
);
128 do_dhcpd
= nvram_match("lan_proto", "dhcp");
130 dhcp_lease
= nvram_get_int("dhcp_lease");
131 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
133 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
134 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
135 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
138 // if not using dnsmasq for dns
140 if ((dns
->count
== 0) && (nvram_get_int("dhcpd_llndns"))) {
141 // no DNS might be temporary. use a low lease time to force clients to update.
143 strcpy(sdhcp_lease
, "2m");
147 // pass the dns directly
149 for (n
= 0 ; n
< dns
->count
; ++n
) {
150 if (dns
->dns
[n
].port
== 53) { // check: option 6 doesn't seem to support other ports
151 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
].addr
));
154 fprintf(f
, "dhcp-option=6%s\n", buf
);
158 if ((p
= nvram_get("dhcpd_startip")) && (*p
) && (e
= nvram_get("dhcpd_endip")) && (*e
)) {
159 fprintf(f
, "dhcp-range=%s,%s,%s,%dm\n", p
, e
, nvram_safe_get("lan_netmask"), dhcp_lease
);
163 dhcp_start
= nvram_get_int("dhcp_start");
164 dhcp_count
= nvram_get_int("dhcp_num");
165 fprintf(f
, "dhcp-range=%s%d,%s%d,%s,%dm\n",
166 lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get("lan_netmask"), dhcp_lease
);
170 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED
)) {
171 p
= nvram_safe_get("lan_gateway");
172 if ((*p
) && (strcmp(p
, "0.0.0.0") != 0)) nv
= p
;
175 n
= nvram_get_int("dhcpd_lmax");
177 "dhcp-option=3,%s\n" // gateway
178 "dhcp-lease-max=%d\n",
182 if (nvram_get_int("dhcpd_auth") >= 0) {
183 fprintf(f
, "dhcp-authoritative\n");
186 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
187 fprintf(f
, "dhcp-option=44,%s\n", nv
);
189 #ifdef TCONFIG_SAMBASRV
190 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
191 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
192 // Samba will serve as a WINS server
193 fprintf(f
, "dhcp-option=44,0.0.0.0\n");
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
);
213 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
214 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
215 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
216 p
= nvram_safe_get("dhcpd_static");
217 while ((e
= strchr(p
, '>')) != NULL
) {
228 if ((e
= strchr(buf
, '<')) == NULL
) continue;
233 if ((e
= strchr(ip
, '<')) == NULL
) continue;
235 if (strchr(ip
, '.') == NULL
) {
237 if ((ipn
<= 0) || (ipn
> 255)) continue;
238 sprintf(ipbuf
, "%s%d", lan
, ipn
);
242 if (inet_addr(ip
) == INADDR_NONE
) continue;
247 if ((hf
) && (*name
!= 0)) {
248 fprintf(hf
, "%s %s\n", ip
, name
);
251 if ((do_dhcpd
) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
252 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
260 #ifdef TCONFIG_OPENVPN
261 write_vpn_dnsmasq_config(f
);
264 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
266 fappend(f
, "/etc/dnsmasq.custom");
273 unlink("/etc/resolv.conf");
274 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
277 TRACE_PT("run dnsmasq\n");
281 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
282 f_read_string(dmpid
, buf
, sizeof(buf
));
283 pid_dnsmasq
= atol(buf
);
289 void stop_dnsmasq(void)
294 stop_service("dnsmasq");
300 unlink("/etc/resolv.conf");
301 symlink(dmresolv
, "/etc/resolv.conf");
303 killall_tk("dnsmasq");
308 void clear_resolv(void)
310 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
313 void dns_to_resolv(void)
316 const dns_list_t
*dns
;
320 m
= umask(022); // 077 from pppoecd
321 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
322 // Check for VPN DNS entries
323 if (!write_vpn_resolv(f
)) {
324 dns
= get_dns(); // static buffer
325 if (dns
->count
== 0) {
326 // Put a pseudo DNS IP to trigger Connect On Demand
327 if ((nvram_match("ppp_demand", "1")) &&
328 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
329 fprintf(f
, "nameserver 1.1.1.1\n");
333 for (i
= 0; i
< dns
->count
; i
++) {
334 if (dns
->dns
[i
].port
== 53) { // resolv.conf doesn't allow for an alternate port
335 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
].addr
));
345 // -----------------------------------------------------------------------------
347 void start_httpd(void)
350 if (!nvram_match("http_enable", "0")) {
353 if (!nvram_match("https_enable", "0")) {
354 xstart("httpd", "-s");
359 void stop_httpd(void)
364 // -----------------------------------------------------------------------------
366 void start_upnp(void)
368 if (get_wan_proto() == WP_DISABLED
) return;
374 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
375 mkdir("/etc/upnp", 0777);
376 if (f_exists("/etc/upnp/config.alt")) {
377 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
380 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
381 upnp_port
= nvram_get_int("upnp_port");
382 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
384 char *lanip
= nvram_safe_get("lan_ipaddr");
385 char *lanmask
= nvram_safe_get("lan_netmask");
389 "listening_ip=%s/%s\n"
394 "upnp_forward_chain=upnp\n"
395 "upnp_nat_chain=upnp\n"
396 "notify_interval=%d\n"
397 "system_uptime=yes\n"
400 nvram_safe_get("wan_iface"),
403 (enable
& 1) ? "yes" : "no", // upnp enable
404 (enable
& 2) ? "yes" : "no", // natpmp enable
405 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
406 nvram_get_int("upnp_ssdp_interval")
409 if (nvram_get_int("upnp_clean")) {
410 int interval
= nvram_get_int("upnp_clean_interval");
411 if (interval
< 60) interval
= 60;
413 "clean_ruleset_interval=%d\n"
414 "clean_ruleset_threshold=%d\n",
416 nvram_get_int("upnp_clean_threshold")
420 fprintf(f
,"clean_ruleset_interval=0\n");
422 if (nvram_match("upnp_mnp", "1")) {
423 int https
= nvram_get_int("https_enable");
424 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
425 https
? "s" : "", lanip
,
426 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
429 // Empty parameters are not included into XML service description
430 fprintf(f
, "presentation_url=\n");
434 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
435 fprintf(f
, "uuid=%s\n", uuid
);
438 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
439 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
440 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
441 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
443 "allow %d-%d %s/%s %d-%d\n",
450 // by default allow only redirection of ports above 1024
451 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
454 fappend(f
, "/etc/upnp/config.custom");
455 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
458 xstart("miniupnpd", "-f", "/etc/upnp/config");
466 killall_tk("miniupnpd");
469 // -----------------------------------------------------------------------------
471 static pid_t pid_crond
= -1;
473 void start_cron(void)
475 char *argv
[] = { "crond", "-l", "9", NULL
};
479 if (nvram_contains_word("log_events", "crond")) argv
[1] = NULL
;
480 _eval(argv
, NULL
, 0, NULL
);
481 if (!nvram_contains_word("debug_norestart", "crond")) {
493 // -----------------------------------------------------------------------------
496 static pid_t pid_hotplug2
= -1;
498 void start_hotplug2()
502 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
503 xstart("hotplug2", "--persistent", "--no-coldplug");
506 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
511 void stop_hotplug2(void)
514 killall_tk("hotplug2");
518 // -----------------------------------------------------------------------------
520 // Written by Sparq in 2002/07/16
521 void start_zebra(void)
526 char *lan_tx
= nvram_safe_get("dr_lan_tx");
527 char *lan_rx
= nvram_safe_get("dr_lan_rx");
528 char *wan_tx
= nvram_safe_get("dr_wan_tx");
529 char *wan_rx
= nvram_safe_get("dr_wan_rx");
531 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
536 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
541 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
542 char *lan_ifname
= nvram_safe_get("lan_ifname");
543 char *wan_ifname
= nvram_safe_get("wan_ifname");
545 fprintf(fp
, "router rip\n");
546 fprintf(fp
, "network %s\n", lan_ifname
);
547 fprintf(fp
, "network %s\n", wan_ifname
);
548 fprintf(fp
, "redistribute connected\n");
549 //fprintf(fp, "redistribute static\n");
551 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
552 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
554 fprintf(fp
, "interface %s\n", lan_ifname
);
555 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
556 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
558 fprintf(fp
, "interface %s\n", wan_ifname
);
559 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
560 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
562 fprintf(fp
, "router rip\n");
563 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
564 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
565 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
566 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
567 fprintf(fp
, "access-list private deny any\n");
569 //fprintf(fp, "debug rip events\n");
570 //fprintf(fp, "log file /etc/ripd.log\n");
574 xstart("zebra", "-d");
575 xstart("ripd", "-d");
579 void stop_zebra(void)
582 killall("zebra", SIGTERM
);
583 killall("ripd", SIGTERM
);
585 unlink("/etc/zebra.conf");
586 unlink("/etc/ripd.conf");
590 // -----------------------------------------------------------------------------
592 void start_syslog(void)
605 if (nvram_match("log_remote", "1")) {
606 nv
= nvram_safe_get("log_remoteip");
608 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
614 if (nvram_match("log_file", "1")) {
622 _eval(argv
, NULL
, 0, NULL
);
628 _eval(argv
, NULL
, 0, NULL
);
631 // used to be available in syslogd -m
632 n
= nvram_get_int("log_mark");
634 sprintf(s
, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
635 (n
< 60) ? "*/30" : "0", (n
< 120) ? "*" : "*/2");
639 system("cru d syslogdmark");
651 argv
[2] = nvram_get("log_mark");
654 if (nvram_match("log_remote", "1")) {
655 nv
= nvram_safe_get("log_remoteip");
657 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
663 if (nvram_match("log_file", "1")) {
671 _eval(argv
, NULL
, 0, NULL
);
676 _eval(argv
, NULL
, 0, NULL
);
682 void stop_syslog(void)
684 killall("klogd", SIGTERM
);
685 killall("syslogd", SIGTERM
);
688 // -----------------------------------------------------------------------------
690 static pid_t pid_igmp
= -1;
692 void start_igmp_proxy(void)
698 if (nvram_match("multicast_pass", "1")) {
699 switch (get_wan_proto()) {
712 if (f_exists("/etc/igmp.alt")) {
713 xstart("igmpproxy", "/etc/igmp.alt");
715 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
718 "phyint %s upstream\n"
720 "phyint %s downstream ratelimit 0\n",
722 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
723 nvram_safe_get("lan_ifname"));
725 xstart("igmpproxy", "/etc/igmp.conf");
730 if (!nvram_contains_word("debug_norestart", "igmprt")) {
736 void stop_igmp_proxy(void)
739 killall("igmpproxy", SIGTERM
);
743 // -----------------------------------------------------------------------------
747 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
750 void start_ntpc(void)
756 if (nvram_get_int("ntp_updates") >= 0) {
757 xstart("ntpsync", "--init");
763 killall("ntpsync", SIGTERM
);
766 // -----------------------------------------------------------------------------
768 static void stop_rstats(void)
774 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
775 if (kill(pid
, SIGTERM
) != 0) break;
780 static void start_rstats(int new)
782 if (nvram_match("rstats_enable", "1")) {
784 if (new) xstart("rstats", "--new");
785 else xstart("rstats");
789 // -----------------------------------------------------------------------------
795 * Return non-zero if we created the directory,
796 * and zero if it already existed.
798 int mkdir_if_none(char *dir
)
801 if (!(dp
=opendir(dir
))) {
810 char *get_full_storage_path(char *val
)
812 static char buf
[128];
816 len
= sprintf(buf
, "%s", val
);
818 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
820 if (len
> 1 && buf
[len
- 1] == '/')
826 char *nvram_storage_path(char *var
)
828 char *val
= nvram_safe_get(var
);
829 return get_full_storage_path(val
);
831 #endif // TCONFIG_USB
835 char vsftpd_conf
[] = "/etc/vsftpd.conf";
836 char vsftpd_users
[] = "/etc/vsftpd.users";
837 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
841 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
842 static void do_start_stop_ftpd(int stop
, int start
)
844 if (stop
) killall_tk("vsftpd");
849 if (!start
|| !nvram_get_int("ftp_enable")) return;
851 mkdir_if_none(vsftpd_users
);
852 mkdir_if_none("/var/run/vsftpd");
854 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
857 if (nvram_get_int("ftp_super"))
860 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
861 if ((f
= fopen(tmp
, "w")))
864 "dirlist_enable=yes\n"
866 "download_enable=yes\n");
871 #ifdef TCONFIG_SAMBASRV
872 if (nvram_match("smbd_cset", "utf8"))
873 fprintf(fp
, "utf8=yes\n");
876 if (nvram_invmatch("ftp_anonymous", "0"))
879 "anon_allow_writable_root=yes\n"
880 "anon_world_readable_only=no\n"
884 sprintf(tmp
, "%s/ftp", vsftpd_users
);
885 if ((f
= fopen(tmp
, "w")))
887 if (nvram_match("ftp_dirlist", "0"))
888 fprintf(f
, "dirlist_enable=yes\n");
889 if (nvram_match("ftp_anonymous", "1") ||
890 nvram_match("ftp_anonymous", "3"))
891 fprintf(f
, "write_enable=yes\n");
892 if (nvram_match("ftp_anonymous", "1") ||
893 nvram_match("ftp_anonymous", "2"))
894 fprintf(f
, "download_enable=yes\n");
897 if (nvram_match("ftp_anonymous", "1") ||
898 nvram_match("ftp_anonymous", "3"))
900 "anon_upload_enable=yes\n"
901 "anon_mkdir_write_enable=yes\n"
902 "anon_other_write_enable=yes\n");
904 fprintf(fp
, "anonymous_enable=no\n");
908 "dirmessage_enable=yes\n"
909 "download_enable=no\n"
910 "dirlist_enable=no\n"
912 "syslog_enable=yes\n"
916 "chroot_local_user=yes\n"
918 "log_ftp_protocol=%s\n"
919 "user_config_dir=%s\n"
927 "idle_session_timeout=%s\n"
930 "local_max_rate=%d\n"
932 nvram_get_int("log_ftp") ? "yes" : "no",
933 vsftpd_users
, vsftpd_passwd
,
934 nvram_get("ftp_port") ? : "21",
935 nvram_get_int("ftp_max"),
936 nvram_get_int("ftp_ipmax"),
937 nvram_get("ftp_staytimeout") ? : "300",
938 nvram_get_int("ftp_anonrate") * 1024,
939 nvram_get_int("ftp_rate") * 1024,
940 nvram_safe_get("ftp_custom"));
944 /* prepare passwd file and default users */
945 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
948 fprintf(fp
, /* anonymous, admin, nobody */
949 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
950 "%s:%s:0:0:root:/:/sbin/nologin\n"
951 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
952 nvram_storage_path("ftp_anonroot"), "admin",
953 nvram_get_int("ftp_super") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
958 char *user
, *pass
, *rights
;
960 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
963 username<password<rights
971 while ((q
= strsep(&p
, ">")) != NULL
) {
972 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
973 if (!user
|| !pass
) continue;
976 if (strncmp(rights
, "Private", 7) == 0)
978 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
982 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
984 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
985 user
, crypt(pass
, "$1$"), user
, tmp
);
988 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
989 if ((f
= fopen(tmp
, "w")))
992 if (nvram_invmatch("ftp_dirlist", "1"))
993 strcat(tmp
, "dirlist_enable=yes\n");
994 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
995 strcat(tmp
, "download_enable=yes\n");
996 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
997 strcat(tmp
, "write_enable=yes\n");
1007 killall("vsftpd", SIGHUP
);
1009 /* start vsftpd if it's not already running */
1010 if (pidof("vsftpd") <= 0)
1015 void start_ftpd(void)
1018 int fd
= file_lock("usb");
1019 do_start_stop_ftpd(0, 1);
1024 void stop_ftpd(void)
1027 int fd
= file_lock("usb");
1028 do_start_stop_ftpd(1, 0);
1029 unlink(vsftpd_passwd
);
1030 unlink(vsftpd_conf
);
1031 eval("rm", "-rf", vsftpd_users
);
1036 // -----------------------------------------------------------------------------
1040 #ifdef TCONFIG_SAMBASRV
1041 void kill_samba(int sig
)
1043 if (sig
== SIGTERM
) {
1048 killall("smbd", sig
);
1049 killall("nmbd", sig
);
1054 #ifdef TCONFIG_SAMBASRV
1055 static void do_start_stop_samba(int stop
, int start
)
1057 if (stop
) kill_samba(SIGTERM
);
1066 mode
= nvram_get_int("smbd_enable");
1067 if (!start
|| !mode
|| !nvram_invmatch("lan_hostname", ""))
1070 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1073 fprintf(fp
, "[global]\n"
1074 " interfaces = %s\n"
1075 " bind interfaces only = yes\n"
1077 " netbios name = %s\n"
1078 " server string = %s\n"
1079 " guest account = nobody\n"
1081 " browseable = yes\n"
1083 " guest only = no\n"
1085 " syslog only = yes\n"
1086 " timestamp logs = no\n"
1089 " encrypt passwords = yes\n"
1090 " preserve case = yes\n"
1091 " short preserve case = yes\n",
1092 nvram_safe_get("lan_ifname"),
1093 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1094 nvram_safe_get("lan_hostname"),
1095 nvram_get("router_name") ? : "Tomato",
1096 mode
== 2 ? "user" : "share",
1097 nvram_get_int("smbd_loglevel")
1100 if (nvram_get_int("smbd_wins")) {
1101 nv
= nvram_safe_get("wan_wins");
1102 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1103 fprintf(fp
, " wins support = yes\n");
1107 if (nvram_get_int("smbd_master")) {
1109 " domain master = yes\n"
1110 " local master = yes\n"
1111 " preferred master = yes\n"
1112 " os level = 65\n");
1115 nv
= nvram_safe_get("smbd_cpage");
1117 #ifndef TCONFIG_SAMBA3
1118 fprintf(fp
, " client code page = %s\n", nv
);
1120 sprintf(nlsmod
, "nls_cp%s", nv
);
1122 nv
= nvram_safe_get("smbd_nlsmod");
1123 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1127 nvram_set("smbd_nlsmod", nlsmod
);
1130 #ifndef TCONFIG_SAMBA3
1131 if (nvram_match("smbd_cset", "utf8"))
1132 fprintf(fp
, " coding system = utf8\n");
1133 else if (nvram_invmatch("smbd_cset", ""))
1134 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1137 fprintf(fp
, "%s\n\n", nvram_safe_get("smbd_custom"));
1139 /* configure shares */
1143 char *name
, *path
, *comment
, *writeable
, *hidden
;
1146 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1148 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1151 while ((q
= strsep(&p
, ">")) != NULL
) {
1152 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1153 if (!path
|| !name
) continue;
1156 fprintf(fp
, "\n[%s]\n", name
);
1159 fprintf(fp
, " path = %s\n", path
);
1162 if (!strcmp(writeable
, "1"))
1163 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1164 if (!strcmp(hidden
, "1"))
1165 fprintf(fp
, " browseable = no\n");
1169 fprintf(fp
, " comment = %s\n", comment
);
1176 /* share everything below MOUNT_ROOT */
1177 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1178 while ((dp
= readdir(dir
))) {
1179 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1181 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1182 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1183 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1184 if (nvram_match("smbd_autoshare", "3")) // Hidden
1185 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1186 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1187 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1188 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1194 if (dir
) closedir(dir
);
1197 /* by default share MOUNT_ROOT as read-only */
1198 fprintf(fp
, "\n[share]\n"
1206 mkdir_if_none("/var/run/samba");
1207 mkdir_if_none("/etc/samba");
1209 /* write smbpasswd */
1210 #ifdef TCONFIG_SAMBA3
1211 eval("smbpasswd", "nobody", "\"\"");
1213 eval("smbpasswd", "-a", "nobody", "\"\"");
1217 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1219 #ifdef TCONFIG_SAMBA3
1220 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1222 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1227 int ret1
= 0, ret2
= 0;
1228 /* start samba if it's not already running */
1229 if (pidof("nmbd") <= 0)
1230 ret1
= eval("nmbd", "-D");
1231 if (pidof("smbd") <= 0)
1232 ret2
= eval("smbd", "-D");
1234 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1238 void start_samba(void)
1240 #ifdef TCONFIG_SAMBASRV
1241 int fd
= file_lock("usb");
1242 do_start_stop_samba(0, 1);
1247 void stop_samba(void)
1249 #ifdef TCONFIG_SAMBASRV
1250 int fd
= file_lock("usb");
1251 do_start_stop_samba(1, 0);
1254 if (nvram_invmatch("smbd_nlsmod", "")) {
1255 modprobe_r(nvram_get("smbd_nlsmod"));
1256 nvram_set("smbd_nlsmod", "");
1261 unlink("/var/log/smb");
1262 unlink("/var/log/nmb");
1263 eval("rm", "-rf", "/var/run/samba");
1269 void restart_nas_services(int stop
, int start
)
1271 /* restart all NAS applications */
1272 #if TCONFIG_SAMBASRV || TCONFIG_FTP
1273 int fd
= file_lock("usb");
1274 #ifdef TCONFIG_SAMBASRV
1275 do_start_stop_samba(stop
, start
&& nvram_get_int("smbd_enable"));
1278 do_start_stop_ftpd(stop
, start
&& nvram_get_int("ftp_enable"));
1281 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1283 #endif // TCONFIG_USB
1285 // -----------------------------------------------------------------------------
1287 static void _check(pid_t
*pid
, const char *name
, void (*func
)(void) )
1290 if (kill(*pid
, 0) != 0) {
1291 if ((*pid
= pidof(name
)) == -1) func();
1296 void check_services(void)
1299 _check(&pid_hotplug2
, "hotplug2", start_hotplug2
);
1301 _check(&pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1302 _check(&pid_crond
, "crond", start_cron
);
1303 _check(&pid_igmp
, "igmpproxy", start_igmp_proxy
);
1306 // -----------------------------------------------------------------------------
1308 void start_services(void)
1310 static int once
= 1;
1316 if (nvram_get_int("telnetd_eas")) start_telnetd();
1317 if (nvram_get_int("sshd_eas")) start_sshd();
1330 restart_nas_services(1, 1); // !!TB - Samba and FTP Server
1333 void stop_services(void)
1337 stop_ftpd(); // !!TB - FTP Server
1338 stop_samba(); // !!TB - Samba
1351 // -----------------------------------------------------------------------------
1353 void exec_service(void)
1355 const int A_START
= 1;
1356 const int A_STOP
= 2;
1357 const int A_RESTART
= 1|2;
1365 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1369 act
= strsep(&next
, ",");
1370 service
= strsep(&act
, "-");
1376 TRACE_PT("service=%s action=%s\n", service
, act
);
1378 if (strcmp(act
, "start") == 0) action
= A_START
;
1379 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1380 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1384 if (strcmp(service
, "dhcpc") == 0) {
1385 if (action
& A_STOP
) stop_dhcpc();
1386 if (action
& A_START
) start_dhcpc();
1390 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1391 if (action
& A_STOP
) stop_dnsmasq();
1392 if (action
& A_START
) {
1399 if (strcmp(service
, "firewall") == 0) {
1400 if (action
& A_STOP
) {
1404 if (action
& A_START
) {
1411 if (strcmp(service
, "restrict") == 0) {
1412 if (action
& A_STOP
) {
1415 if (action
& A_START
) {
1416 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1420 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1422 if (nvram_get_int("rrules_radio") < 0) {
1423 if (!get_radio()) eval("radio", "on");
1430 if (strcmp(service
, "qos") == 0) {
1431 if (action
& A_STOP
) {
1434 stop_firewall(); start_firewall(); // always restarted
1435 if (action
& A_START
) {
1437 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1442 if (strcmp(service
, "upnp") == 0) {
1443 if (action
& A_STOP
) {
1446 stop_firewall(); start_firewall(); // always restarted
1447 if (action
& A_START
) {
1453 if (strcmp(service
, "telnetd") == 0) {
1454 if (action
& A_STOP
) stop_telnetd();
1455 if (action
& A_START
) start_telnetd();
1459 if (strcmp(service
, "sshd") == 0) {
1460 if (action
& A_STOP
) stop_sshd();
1461 if (action
& A_START
) start_sshd();
1465 if (strcmp(service
, "httpd") == 0) {
1466 if (action
& A_STOP
) stop_httpd();
1467 if (action
& A_START
) start_httpd();
1471 if (strcmp(service
, "admin") == 0) {
1472 if (action
& A_STOP
) {
1477 stop_firewall(); start_firewall(); // always restarted
1478 if (action
& A_START
) {
1481 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1482 if (nvram_match("sshd_eas", "1")) start_sshd();
1487 if (strcmp(service
, "ddns") == 0) {
1488 if (action
& A_STOP
) stop_ddns();
1489 if (action
& A_START
) start_ddns();
1493 if (strcmp(service
, "ntpc") == 0) {
1494 if (action
& A_STOP
) stop_ntpc();
1495 if (action
& A_START
) start_ntpc();
1499 if (strcmp(service
, "logging") == 0) {
1500 if (action
& A_STOP
) {
1504 stop_firewall(); start_firewall(); // always restarted
1505 if (action
& A_START
) {
1512 if (strcmp(service
, "crond") == 0) {
1513 if (action
& A_STOP
) {
1516 if (action
& A_START
) {
1523 if (strncmp(service
, "hotplug", 7) == 0) {
1524 if (action
& A_STOP
) {
1527 if (action
& A_START
) {
1534 if (strcmp(service
, "upgrade") == 0) {
1535 if (action
& A_START
) {
1540 stop_ftpd(); // !!TB - FTP Server
1541 stop_samba(); // !!TB - Samba
1549 killall("rstats", SIGTERM
);
1550 killall("buttons", SIGTERM
);
1552 remove_storage_main(1); // !!TB - USB Support
1553 stop_usb(); // !!TB - USB Support
1559 if (strcmp(service
, "cifs") == 0) {
1560 if (action
& A_STOP
) stop_cifs();
1561 if (action
& A_START
) start_cifs();
1566 #ifdef TCONFIG_JFFS2
1567 if (strcmp(service
, "jffs2") == 0) {
1568 if (action
& A_STOP
) stop_jffs2();
1569 if (action
& A_START
) start_jffs2();
1574 if (strcmp(service
, "routing") == 0) {
1575 if (action
& A_STOP
) {
1577 do_static_routes(0); // remove old '_saved'
1578 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1582 if (action
& A_START
) {
1583 do_static_routes(1); // add new
1585 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1590 if (strcmp(service
, "ctnf") == 0) {
1591 if (action
& A_START
) {
1599 if (strcmp(service
, "wan") == 0) {
1600 if (action
& A_STOP
) {
1601 if (get_wan_proto() == WP_PPPOE
) {
1604 stop_singe_pppoe(PPPOE0
);
1605 if (((action
& A_START
) == 0) && (nvram_match("ppp_demand", "1"))) {
1607 start_pppoe(PPPOE0
);
1616 if (action
& A_START
) {
1617 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1619 if (get_wan_proto() == WP_PPPOE
) {
1620 stop_singe_pppoe(PPPOE0
);
1621 start_pppoe(PPPOE0
);
1622 if (nvram_invmatch("ppp_demand", "1")) {
1635 if (strcmp(service
, "net") == 0) {
1636 if (action
& A_STOP
) {
1643 if (action
& A_START
) {
1654 if (strcmp(service
, "nas") == 0) {
1655 if (action
& A_STOP
) {
1658 if (action
& A_START
) {
1665 if (strcmp(service
, "rstats") == 0) {
1666 if (action
& A_STOP
) stop_rstats();
1667 if (action
& A_START
) start_rstats(0);
1671 if (strcmp(service
, "rstatsnew") == 0) {
1672 if (action
& A_STOP
) stop_rstats();
1673 if (action
& A_START
) start_rstats(1);
1677 if (strcmp(service
, "sched") == 0) {
1678 if (action
& A_STOP
) stop_sched();
1679 if (action
& A_START
) start_sched();
1684 // !!TB - USB Support
1685 if (strcmp(service
, "usb") == 0) {
1686 if (action
& A_STOP
) stop_usb();
1687 if (action
& A_START
) {
1689 // restart Samba and ftp since they may be killed by stop_usb()
1690 restart_nas_services(0, 1);
1697 // !!TB - FTP Server
1698 if (strcmp(service
, "ftpd") == 0) {
1699 if (action
& A_STOP
) stop_ftpd();
1703 if (action
& A_START
) start_ftpd();
1708 #ifdef TCONFIG_SAMBASRV
1710 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
1711 if (action
& A_STOP
) stop_samba();
1712 if (action
& A_START
) {
1722 #ifdef TCONFIG_OPENVPN
1723 if (strncmp(service
, "vpnclient", 9) == 0) {
1724 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
1725 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
1729 if (strncmp(service
, "vpnserver", 9) == 0) {
1730 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
1731 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
1739 // some functions check action_service and must be cleared at end -- zzz
1740 nvram_set("action_service", "");
1743 static void do_service(const char *name
, const char *action
, int user
)
1749 while (!nvram_match("action_service", "")) {
1754 else if (--n
< 0) break;
1758 snprintf(s
, sizeof(s
), "%s-%s", name
, action
);
1759 nvram_set("action_service", s
);
1763 while (nvram_match("action_service", s
)) {
1775 int service_main(int argc
, char *argv
[])
1777 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
1778 do_service(argv
[1], argv
[2], 1);
1779 printf("\nDone.\n");
1783 void start_service(const char *name
)
1785 do_service(name
, "start", 0);
1788 void stop_service(const char *name
)
1790 do_service(name
, "stop", 0);
1794 void restart_service(const char *name)
1796 do_service(name, "restart", 0);