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
);
173 #ifdef TCONFIG_SAMBASRV
174 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
175 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
176 // Samba will serve as a WINS server
177 fprintf(f
, "dhcp-option=44,0.0.0.0\n");
183 fprintf(f
, "no-dhcp-interface=%s\n", lan_ifname
);
186 // write static lease entries & create hosts file
188 if ((hf
= fopen(dmhosts
, "w")) != NULL
) {
189 if (((nv
= nvram_get("wan_hostname")) != NULL
) && (*nv
))
190 fprintf(hf
, "%s %s\n", router_ip
, nv
);
191 #ifdef TCONFIG_SAMBASRV
192 else if (((nv
= nvram_get("lan_hostname")) != NULL
) && (*nv
))
193 fprintf(hf
, "%s %s\n", router_ip
, nv
);
197 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
198 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
199 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
200 p
= nvram_safe_get("dhcpd_static");
201 while ((e
= strchr(p
, '>')) != NULL
) {
212 if ((e
= strchr(buf
, '<')) == NULL
) continue;
217 if ((e
= strchr(ip
, '<')) == NULL
) continue;
219 if (strchr(ip
, '.') == NULL
) {
221 if ((ipn
<= 0) || (ipn
> 255)) continue;
222 sprintf(ipbuf
, "%s%d", lan
, ipn
);
226 if (inet_addr(ip
) == INADDR_NONE
) continue;
231 if ((hf
) && (*name
!= 0)) {
232 fprintf(hf
, "%s %s\n", ip
, name
);
235 if ((do_dhcpd
) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
236 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
244 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
246 fappend(f
, "/etc/dnsmasq.custom");
253 unlink("/etc/resolv.conf");
254 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
257 TRACE_PT("run dnsmasq\n");
261 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
262 f_read_string(dmpid
, buf
, sizeof(buf
));
263 pid_dnsmasq
= atol(buf
);
269 void stop_dnsmasq(void)
274 stop_service("dnsmasq");
280 unlink("/etc/resolv.conf");
281 symlink(dmresolv
, "/etc/resolv.conf");
283 killall_tk("dnsmasq");
288 void clear_resolv(void)
290 _dprintf("%s\n", __FUNCTION__
);
292 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
295 void dns_to_resolv(void)
298 const dns_list_t
*dns
;
302 _dprintf("%s\n", __FUNCTION__
);
304 m
= umask(022); // 077 from pppoecd
305 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
306 dns
= get_dns(); // static buffer
307 if (dns
->count
== 0) {
308 // Put a pseudo DNS IP to trigger Connect On Demand
309 if ((nvram_match("ppp_demand", "1")) &&
310 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
311 fprintf(f
, "nameserver 1.1.1.1\n");
315 for (i
= 0; i
< dns
->count
; i
++) {
316 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
]));
324 // -----------------------------------------------------------------------------
326 void start_httpd(void)
329 if (!nvram_match("http_enable", "0")) {
332 if (!nvram_match("https_enable", "0")) {
333 xstart("httpd", "-s");
338 void stop_httpd(void)
343 // -----------------------------------------------------------------------------
345 void start_upnp(void)
347 if (get_wan_proto() == WP_DISABLED
) return;
354 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
355 mkdir("/etc/upnp", 0777);
356 if (f_exists("/etc/upnp/config.alt")) {
357 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
360 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
361 upnp_port
= nvram_get_int("upnp_port");
362 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
364 char *lanip
= nvram_safe_get("lan_ipaddr");
365 char *lanmask
= nvram_safe_get("lan_netmask");
369 "listening_ip=%s/%s\n"
374 "upnp_forward_chain=upnp\n"
375 "upnp_nat_chain=upnp\n"
376 "notify_interval=%d\n"
377 "system_uptime=yes\n"
380 nvram_safe_get("wan_iface"),
383 (enable
& 1) ? "yes" : "no", // upnp enable
384 (enable
& 2) ? "yes" : "no", // natpmp enable
385 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
386 nvram_get_int("upnp_ssdp_interval")
389 if (nvram_get_int("upnp_clean")) {
390 int interval
= nvram_get_int("upnp_clean_interval");
391 if (interval
< 60) interval
= 60;
393 "clean_ruleset_interval=%d\n"
394 "clean_ruleset_threshold=%d\n",
396 nvram_get_int("upnp_clean_threshold")
400 fprintf(f
,"clean_ruleset_interval=0\n");
402 if (nvram_match("upnp_mnp", "1")) {
403 int https
= nvram_get_int("https_enable");
404 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
405 https
? "s" : "", lanip
,
406 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
409 // Empty parameters are not included into XML service description
410 fprintf(f
, "presentation_url=\n");
414 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
415 fprintf(f
, "uuid=%s\n", uuid
);
418 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
419 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
420 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
421 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
423 "allow %d-%d %s/%s %d-%d\n",
430 // by default allow only redirection of ports above 1024
431 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
434 fappend(f
, "/etc/upnp/config.custom");
435 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
438 xstart("miniupnpd", "-f", "/etc/upnp/config");
443 if (nvram_get_int("upnp_enable")) {
446 "-L", nvram_safe_get("lan_ifname"),
447 "-W", nvram_safe_get("wan_iface"),
448 "-I", nvram_safe_get("upnp_ssdp_interval"),
449 "-A", nvram_safe_get("upnp_max_age"));
457 killall_tk("miniupnpd");
463 // -----------------------------------------------------------------------------
465 static pid_t pid_crond
= -1;
467 void start_cron(void)
469 char *argv
[] = { "crond", "-l", "9", NULL
};
473 if (nvram_contains_word("log_events", "crond")) argv
[1] = NULL
;
474 _eval(argv
, NULL
, 0, NULL
);
475 if (!nvram_contains_word("debug_norestart", "crond")) {
487 // -----------------------------------------------------------------------------
489 // Written by Sparq in 2002/07/16
490 void start_zebra(void)
495 char *lan_tx
= nvram_safe_get("dr_lan_tx");
496 char *lan_rx
= nvram_safe_get("dr_lan_rx");
497 char *wan_tx
= nvram_safe_get("dr_wan_tx");
498 char *wan_rx
= nvram_safe_get("dr_wan_rx");
500 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
505 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
510 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
511 char *lan_ifname
= nvram_safe_get("lan_ifname");
512 char *wan_ifname
= nvram_safe_get("wan_ifname");
514 fprintf(fp
, "router rip\n");
515 fprintf(fp
, "network %s\n", lan_ifname
);
516 fprintf(fp
, "network %s\n", wan_ifname
);
517 fprintf(fp
, "redistribute connected\n");
518 //fprintf(fp, "redistribute static\n");
520 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
521 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
523 fprintf(fp
, "interface %s\n", lan_ifname
);
524 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
525 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
527 fprintf(fp
, "interface %s\n", wan_ifname
);
528 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
529 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
531 fprintf(fp
, "router rip\n");
532 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
533 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
534 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
535 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
536 fprintf(fp
, "access-list private deny any\n");
538 //fprintf(fp, "debug rip events\n");
539 //fprintf(fp, "log file /etc/ripd.log\n");
543 xstart("zebra", "-d");
544 xstart("ripd", "-d");
548 void stop_zebra(void)
551 killall("zebra", SIGTERM
);
552 killall("ripd", SIGTERM
);
554 unlink("/etc/zebra.conf");
555 unlink("/etc/ripd.conf");
559 // -----------------------------------------------------------------------------
561 void start_syslog(void)
574 if (nvram_match("log_remote", "1")) {
575 nv
= nvram_safe_get("log_remoteip");
577 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
583 if (nvram_match("log_file", "1")) {
591 _eval(argv
, NULL
, 0, NULL
);
596 _eval(argv
, NULL
, 0, NULL
);
599 // used to be available in syslogd -m
600 n
= nvram_get_int("log_mark");
602 sprintf(s
, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
603 (n
< 60) ? "*/30" : "0", (n
< 120) ? "*" : "*/2");
607 system("cru d syslogdmark");
619 argv
[2] = nvram_get("log_mark");
622 if (nvram_match("log_remote", "1")) {
623 nv
= nvram_safe_get("log_remoteip");
625 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
631 if (nvram_match("log_file", "1")) {
639 _eval(argv
, NULL
, 0, NULL
);
644 _eval(argv
, NULL
, 0, NULL
);
650 void stop_syslog(void)
652 killall("klogd", SIGTERM
);
653 killall("syslogd", SIGTERM
);
656 // -----------------------------------------------------------------------------
658 static pid_t pid_igmp
= -1;
660 void start_igmp_proxy(void)
666 if (nvram_match("multicast_pass", "1")) {
667 switch (get_wan_proto()) {
680 if (f_exists("/etc/igmp.alt")) {
681 xstart("igmpproxy", "/etc/igmp.alt");
683 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
686 "phyint %s upstream\n"
688 "phyint %s downstream ratelimit 0\n",
690 nvram_get("multicast_altnet") ? : "0.0.0.0/0",
691 nvram_safe_get("lan_ifname"));
693 xstart("igmpproxy", "/etc/igmp.conf");
698 if (!nvram_contains_word("debug_norestart", "igmprt")) {
704 void stop_igmp_proxy(void)
707 killall("igmpproxy", SIGTERM
);
711 // -----------------------------------------------------------------------------
715 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
718 void start_ntpc(void)
724 if (nvram_get_int("ntp_updates") >= 0) {
725 xstart("ntpsync", "--init");
731 killall("ntpsync", SIGTERM
);
734 // -----------------------------------------------------------------------------
736 static void stop_rstats(void)
742 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
743 if (kill(pid
, SIGTERM
) != 0) break;
748 static void start_rstats(int new)
750 if (nvram_match("rstats_enable", "1")) {
752 if (new) xstart("rstats", "--new");
753 else xstart("rstats");
757 // -----------------------------------------------------------------------------
763 * Return non-zero if we created the directory,
764 * and zero if it already existed.
766 int mkdir_if_none(char *dir
)
769 if (!(dp
=opendir(dir
))) {
778 char *get_full_storage_path(char *val
)
780 static char buf
[128];
784 len
= sprintf(buf
, "%s", val
);
786 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
788 if (len
> 1 && buf
[len
- 1] == '/')
794 char *nvram_storage_path(char *var
)
796 char *val
= nvram_safe_get(var
);
797 return get_full_storage_path(val
);
799 #endif // TCONFIG_USB
803 char vsftpd_conf
[] = "/etc/vsftpd.conf";
804 char vsftpd_users
[] = "/etc/vsftpd.users";
805 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
809 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
810 static void do_start_stop_ftpd(int stop
, int start
)
812 if (stop
) killall("vsftpd", SIGTERM
);
817 if (!start
|| !nvram_get_int("ftp_enable")) return;
819 mkdir_if_none(vsftpd_users
);
820 mkdir_if_none("/var/run/vsftpd");
822 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
825 if (nvram_get_int("ftp_super"))
828 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
829 if ((f
= fopen(tmp
, "w")))
832 "dirlist_enable=yes\n"
834 "download_enable=yes\n");
839 #ifdef TCONFIG_SAMBASRV
840 if (nvram_match("smbd_cset", "utf8"))
841 fprintf(fp
, "utf8=yes\n");
844 if (nvram_invmatch("ftp_anonymous", "0"))
847 "anon_allow_writable_root=yes\n"
848 "anon_world_readable_only=no\n"
852 sprintf(tmp
, "%s/ftp", vsftpd_users
);
853 if ((f
= fopen(tmp
, "w")))
855 if (nvram_match("ftp_dirlist", "0"))
856 fprintf(f
, "dirlist_enable=yes\n");
857 if (nvram_match("ftp_anonymous", "1") ||
858 nvram_match("ftp_anonymous", "3"))
859 fprintf(f
, "write_enable=yes\n");
860 if (nvram_match("ftp_anonymous", "1") ||
861 nvram_match("ftp_anonymous", "2"))
862 fprintf(f
, "download_enable=yes\n");
865 if (nvram_match("ftp_anonymous", "1") ||
866 nvram_match("ftp_anonymous", "3"))
868 "anon_upload_enable=yes\n"
869 "anon_mkdir_write_enable=yes\n"
870 "anon_other_write_enable=yes\n");
872 fprintf(fp
, "anonymous_enable=no\n");
876 "dirmessage_enable=yes\n"
877 "download_enable=no\n"
878 "dirlist_enable=no\n"
880 "syslog_enable=yes\n"
884 "chroot_local_user=yes\n"
886 "log_ftp_protocol=%s\n"
887 "user_config_dir=%s\n"
894 "idle_session_timeout=%s\n"
897 "local_max_rate=%d\n"
899 nvram_get_int("log_ftp") ? "yes" : "no",
900 vsftpd_users
, vsftpd_passwd
,
901 nvram_get("ftp_port") ? : "21",
902 nvram_get_int("ftp_max"),
903 nvram_get_int("ftp_ipmax"),
904 nvram_get("ftp_staytimeout") ? : "300",
905 nvram_get_int("ftp_anonrate") * 1024,
906 nvram_get_int("ftp_rate") * 1024,
907 nvram_safe_get("ftp_custom"));
911 /* prepare passwd file and default users */
912 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
915 fprintf(fp
, /* anonymous, admin, nobody */
916 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
917 "%s:%s:0:0:root:/:/sbin/nologin\n"
918 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
919 nvram_storage_path("ftp_anonroot"), "admin",
920 nvram_get_int("ftp_super") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
925 char *user
, *pass
, *rights
;
927 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
930 username<password<rights
938 while ((q
= strsep(&p
, ">")) != NULL
) {
939 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
940 if (!user
|| !pass
) continue;
943 if (strncmp(rights
, "Private", 7) == 0)
945 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
949 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
951 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
952 user
, crypt(pass
, "$1$"), user
, tmp
);
955 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
956 if ((f
= fopen(tmp
, "w")))
959 if (nvram_invmatch("ftp_dirlist", "1"))
960 strcat(tmp
, "dirlist_enable=yes\n");
961 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
962 strcat(tmp
, "download_enable=yes\n");
963 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
964 strcat(tmp
, "write_enable=yes\n");
974 killall("vsftpd", SIGHUP
);
976 /* start vsftpd if it's not already running */
977 if (pidof("vsftpd") <= 0)
982 void start_ftpd(void)
985 int fd
= file_lock("usb");
986 do_start_stop_ftpd(0, 1);
994 int fd
= file_lock("usb");
995 do_start_stop_ftpd(1, 0);
996 unlink(vsftpd_passwd
);
998 eval("rm", "-rf", vsftpd_users
);
1003 // -----------------------------------------------------------------------------
1007 #ifdef TCONFIG_SAMBASRV
1008 void kill_samba(int sig
)
1010 killall("smbd", sig
);
1011 killall("nmbd", sig
);
1015 #ifdef TCONFIG_SAMBASRV
1016 static void do_start_stop_samba(int stop
, int start
)
1018 if (stop
) kill_samba(SIGTERM
);
1027 mode
= nvram_get_int("smbd_enable");
1028 if (!start
|| !mode
|| !nvram_invmatch("lan_hostname", ""))
1031 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1034 fprintf(fp
, "[global]\n"
1035 " interfaces = %s\n"
1036 " bind interfaces only = yes\n"
1038 " server string = %s\n"
1039 " guest account = nobody\n"
1041 " browseable = yes\n"
1043 " guest only = no\n"
1045 " syslog only = yes\n"
1047 " encrypt passwords = yes\n"
1048 " preserve case = yes\n"
1049 " short preserve case = yes\n",
1050 nvram_safe_get("lan_ifname"),
1051 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1052 nvram_get("router_name") ? : "Tomato",
1053 mode
== 2 ? "user" : "share",
1054 nvram_get_int("smbd_loglevel")
1057 if (nvram_get_int("smbd_wins")) {
1058 nv
= nvram_safe_get("wan_wins");
1059 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1060 fprintf(fp
, " wins support = yes\n");
1064 if (nvram_get_int("smbd_master")) {
1066 " domain master = yes\n"
1067 " local master = yes\n"
1068 " preferred master = yes\n"
1069 " os level = 65\n");
1072 nv
= nvram_safe_get("smbd_cpage");
1074 fprintf(fp
, " client code page = %s\n", nv
);
1075 sprintf(nlsmod
, "nls_cp%s", nv
);
1077 nv
= nvram_safe_get("smbd_nlsmod");
1078 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1082 nvram_set("smbd_nlsmod", nlsmod
);
1085 if (nvram_match("smbd_cset", "utf8"))
1086 fprintf(fp
, " coding system = utf8\n");
1087 else if (nvram_invmatch("smbd_cset", ""))
1088 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1090 fprintf(fp
, "%s\n\n", nvram_safe_get("smbd_custom"));
1092 /* configure shares */
1096 char *name
, *path
, *comment
, *writeable
, *hidden
;
1099 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1101 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1104 while ((q
= strsep(&p
, ">")) != NULL
) {
1105 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1106 if (!path
|| !name
) continue;
1109 fprintf(fp
, "\n[%s]\n", name
);
1112 fprintf(fp
, " path = %s\n", path
);
1115 if (!strcmp(writeable
, "1"))
1116 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1117 if (!strcmp(hidden
, "1"))
1118 fprintf(fp
, " browseable = no\n");
1122 fprintf(fp
, " comment = %s\n", comment
);
1129 /* share everything below MOUNT_ROOT */
1130 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1131 while ((dp
= readdir(dir
))) {
1132 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1134 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1135 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1136 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1137 if (nvram_match("smbd_autoshare", "3")) // Hidden
1138 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1139 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1140 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1141 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1147 if (dir
) closedir(dir
);
1150 /* by default share MOUNT_ROOT as read-only */
1151 fprintf(fp
, "\n[share]\n"
1159 mkdir_if_none("/var/run/samba");
1160 mkdir_if_none("/etc/samba");
1162 /* write smbpasswd */
1163 eval("smbpasswd", "-a", "nobody", "\"\"");
1166 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1168 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1172 int ret1
= 0, ret2
= 0;
1173 /* start samba if it's not already running */
1174 if (pidof("nmbd") <= 0)
1175 ret1
= eval("nmbd", "-D");
1176 if (pidof("smbd") <= 0)
1177 ret2
= eval("smbd", "-D");
1179 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1183 void start_samba(void)
1185 #ifdef TCONFIG_SAMBASRV
1186 int fd
= file_lock("usb");
1187 do_start_stop_samba(0, 1);
1192 void stop_samba(void)
1194 #ifdef TCONFIG_SAMBASRV
1195 int fd
= file_lock("usb");
1196 do_start_stop_samba(1, 0);
1197 sleep(2); /* wait for smbd to finish */
1199 if (nvram_invmatch("smbd_nlsmod", "")) {
1200 modprobe_r(nvram_get("smbd_nlsmod"));
1201 nvram_set("smbd_nlsmod", "");
1205 unlink("/var/log/smb");
1206 unlink("/var/log/nmb");
1207 eval("rm", "-rf", "/var/run/samba");
1213 void restart_nas_services(int start
)
1215 /* restart all NAS applications */
1216 #if TCONFIG_SAMBASRV || TCONFIG_FTP
1217 int fd
= file_lock("usb");
1218 #ifdef TCONFIG_SAMBASRV
1219 if (start
&& nvram_get_int("smbd_enable"))
1220 do_start_stop_samba(0, 1);
1222 do_start_stop_samba(1, 0);
1225 if (start
&& nvram_get_int("ftp_enable"))
1226 do_start_stop_ftpd(0, 1);
1228 do_start_stop_ftpd(1, 0);
1231 #endif // TCONFIG_SAMBASRV || TCONFIG_FTP
1233 #endif // TCONFIG_USB
1235 // -----------------------------------------------------------------------------
1237 static void _check(pid_t
*pid
, const char *name
, void (*func
)(void) )
1240 if (kill(*pid
, 0) != 0) {
1241 if ((*pid
= pidof(name
)) == -1) func();
1246 void check_services(void)
1248 _check(&pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1249 _check(&pid_crond
, "crond", start_cron
);
1250 _check(&pid_igmp
, "igmpproxy", start_igmp_proxy
);
1253 // -----------------------------------------------------------------------------
1255 void start_services(void)
1257 static int once
= 1;
1263 if (nvram_get_int("telnetd_eas")) start_telnetd();
1264 if (nvram_get_int("sshd_eas")) start_sshd();
1277 //start_upnp(); //!!TB - already in start_wan_done()
1280 #ifdef TCONFIG_SAMBA
1283 start_samba(); // !!TB - Samba
1284 start_ftpd(); // !!TB - FTP Server
1287 void stop_services(void)
1291 stop_ftpd(); // !!TB - FTP Server
1292 stop_samba(); // !!TB - Samba
1293 #ifdef TCONFIG_SAMBA
1298 //stop_upnp(); //!!TB - moved to stop_wan()
1311 // -----------------------------------------------------------------------------
1313 void exec_service(void)
1315 const int A_START
= 1;
1316 const int A_STOP
= 2;
1317 const int A_RESTART
= 1|2;
1325 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1329 act
= strsep(&next
, ",");
1330 service
= strsep(&act
, "-");
1336 TRACE_PT("service=%s action=%s\n", service
, act
);
1338 if (strcmp(act
, "start") == 0) action
= A_START
;
1339 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1340 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1344 if (strcmp(service
, "dhcpc") == 0) {
1345 if (action
& A_STOP
) stop_dhcpc();
1346 if (action
& A_START
) start_dhcpc();
1350 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1351 if (action
& A_STOP
) stop_dnsmasq();
1352 if (action
& A_START
) {
1359 if (strcmp(service
, "firewall") == 0) {
1360 if (action
& A_STOP
) {
1364 if (action
& A_START
) {
1371 if (strcmp(service
, "restrict") == 0) {
1372 if (action
& A_STOP
) {
1375 if (action
& A_START
) {
1376 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1380 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1382 if (nvram_get_int("rrules_radio") < 0) {
1383 if (!get_radio()) eval("radio", "on");
1390 if (strcmp(service
, "qos") == 0) {
1391 if (action
& A_STOP
) {
1394 stop_firewall(); start_firewall(); // always restarted
1395 if (action
& A_START
) {
1397 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1402 if (strcmp(service
, "upnp") == 0) {
1403 if (action
& A_STOP
) {
1406 stop_firewall(); start_firewall(); // always restarted
1407 if (action
& A_START
) {
1413 if (strcmp(service
, "telnetd") == 0) {
1414 if (action
& A_STOP
) stop_telnetd();
1415 if (action
& A_START
) start_telnetd();
1419 if (strcmp(service
, "sshd") == 0) {
1420 if (action
& A_STOP
) stop_sshd();
1421 if (action
& A_START
) start_sshd();
1425 if (strcmp(service
, "httpd") == 0) {
1426 if (action
& A_STOP
) stop_httpd();
1427 if (action
& A_START
) start_httpd();
1431 if (strcmp(service
, "admin") == 0) {
1432 if (action
& A_STOP
) {
1437 stop_firewall(); start_firewall(); // always restarted
1438 if (action
& A_START
) {
1441 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1442 if (nvram_match("sshd_eas", "1")) start_sshd();
1447 if (strcmp(service
, "ddns") == 0) {
1448 if (action
& A_STOP
) stop_ddns();
1449 if (action
& A_START
) start_ddns();
1453 if (strcmp(service
, "ntpc") == 0) {
1454 if (action
& A_STOP
) stop_ntpc();
1455 if (action
& A_START
) start_ntpc();
1459 if (strcmp(service
, "logging") == 0) {
1460 if (action
& A_STOP
) {
1464 stop_firewall(); start_firewall(); // always restarted
1465 if (action
& A_START
) {
1472 if (strcmp(service
, "crond") == 0) {
1473 if (action
& A_STOP
) {
1476 if (action
& A_START
) {
1482 if (strcmp(service
, "upgrade") == 0) {
1483 if (action
& A_START
) {
1488 stop_ftpd(); // !!TB - FTP Server
1489 stop_samba(); // !!TB - Samba
1497 killall("rstats", SIGTERM
);
1498 killall("buttons", SIGTERM
);
1500 remove_storage_main(); // !!TB - USB Support
1501 stop_usb(); // !!TB - USB Support
1507 if (strcmp(service
, "cifs") == 0) {
1508 if (action
& A_STOP
) stop_cifs();
1509 if (action
& A_START
) start_cifs();
1514 #ifdef TCONFIG_JFFS2
1515 if (strcmp(service
, "jffs2") == 0) {
1516 if (action
& A_STOP
) stop_jffs2();
1517 if (action
& A_START
) start_jffs2();
1522 if (strcmp(service
, "routing") == 0) {
1523 if (action
& A_STOP
) {
1525 do_static_routes(0); // remove old '_saved'
1526 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1530 if (action
& A_START
) {
1531 do_static_routes(1); // add new
1533 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1538 if (strcmp(service
, "ctnf") == 0) {
1539 if (action
& A_START
) {
1547 if (strcmp(service
, "wan") == 0) {
1548 if (action
& A_STOP
) {
1549 if (get_wan_proto() == WP_PPPOE
) {
1552 stop_singe_pppoe(PPPOE0
);
1553 if (((action
& A_START
) == 0) && (nvram_match("ppp_demand", "1"))) {
1555 start_pppoe(PPPOE0
);
1564 if (action
& A_START
) {
1565 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1567 if (get_wan_proto() == WP_PPPOE
) {
1568 stop_singe_pppoe(PPPOE0
);
1569 start_pppoe(PPPOE0
);
1570 if (nvram_invmatch("ppp_demand", "1")) {
1583 if (strcmp(service
, "net") == 0) {
1584 if (action
& A_STOP
) {
1590 if (action
& A_START
) {
1599 if (strcmp(service
, "rstats") == 0) {
1600 if (action
& A_STOP
) stop_rstats();
1601 if (action
& A_START
) start_rstats(0);
1605 if (strcmp(service
, "rstatsnew") == 0) {
1606 if (action
& A_STOP
) stop_rstats();
1607 if (action
& A_START
) start_rstats(1);
1611 if (strcmp(service
, "sched") == 0) {
1612 if (action
& A_STOP
) stop_sched();
1613 if (action
& A_START
) start_sched();
1618 // !!TB - USB Support
1619 if (strcmp(service
, "usb") == 0) {
1620 if (action
& A_STOP
) stop_usb();
1621 if (action
& A_START
) {
1623 // restart Samba and ftp since they may be killed by stop_usb()
1624 restart_nas_services(1);
1631 // !!TB - FTP Server
1632 if (strcmp(service
, "ftpd") == 0) {
1633 if (action
& A_STOP
) stop_ftpd();
1637 if (action
& A_START
) start_ftpd();
1642 #ifdef TCONFIG_SAMBASRV
1644 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
1645 if (action
& A_STOP
) stop_samba();
1646 if (action
& A_START
) {
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
)
1669 while (!nvram_match("action_service", "")) {
1674 else if (--n
< 0) break;
1678 snprintf(s
, sizeof(s
), "%s-%s", name
, action
);
1679 nvram_set("action_service", s
);
1683 while (nvram_match("action_service", s
)) {
1695 int service_main(int argc
, char *argv
[])
1697 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
1698 do_service(argv
[1], argv
[2], 1);
1699 printf("\nDone.\n");
1703 void start_service(const char *name
)
1705 do_service(name
, "start", 0);
1708 void stop_service(const char *name
)
1710 do_service(name
, "stop", 0);
1714 void restart_service(const char *name)
1716 do_service(name, "restart", 0);