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-2008 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
;
83 start_service("dnsmasq");
89 if (nvram_match("wl_mode", "wet")) return;
91 if (nvram_get_int("dnsmasq_norw")) {
92 if (f_exists("/etc/dnsmasq.conf")) {
93 syslog(LOG_INFO, "%s exists. Not rewriting.", "/etc/dnsmasq.conf");
98 if ((f
= fopen("/etc/dnsmasq.conf", "w")) == NULL
) return;
100 lan_ifname
= nvram_safe_get("lan_ifname");
101 router_ip
= nvram_safe_get("lan_ipaddr");
102 strlcpy(lan
, router_ip
, sizeof(lan
));
103 if ((p
= strrchr(lan
, '.')) != NULL
) *(p
+ 1) = 0;
109 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
110 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
114 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
117 "resolv-file=%s\n" // the real stuff is here
118 "addn-hosts=%s\n" // "
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv
, dmhosts
, n
);
122 do_dns
= nvram_match("dhcpd_dmdns", "1");
126 do_dhcpd
= nvram_match("lan_proto", "dhcp");
128 dhcp_lease
= nvram_get_int("dhcp_lease");
129 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
131 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
132 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
133 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
136 // if not using dnsmasq for dns
138 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
139 if ((dns
->count
== 0) && (nvram_match("dhcpd_llndns", "1"))) {
140 // no DNS might be temporary. use a low lease time to force clients to update.
142 strcpy(sdhcp_lease
, "2m");
146 // pass the dns directly
148 for (n
= 0 ; n
< dns
->count
; ++n
) {
149 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
]));
151 fprintf(f
, "dhcp-option=6%s\n", buf
);
155 if ((p
= nvram_get("dhcpd_startip")) && (*p
) && (e
= nvram_get("dhcpd_endip")) && (*e
)) {
156 fprintf(f
, "dhcp-range=%s,%s,%s,%dm\n", p
, e
, nvram_safe_get("lan_netmask"), dhcp_lease
);
160 dhcp_start
= nvram_get_int("dhcp_start");
161 dhcp_count
= nvram_get_int("dhcp_num");
162 fprintf(f
, "dhcp-range=%s%d,%s%d,%s,%dm\n",
163 lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get("lan_netmask"), dhcp_lease
);
165 n
= nvram_get_int("dhcpd_lmax");
167 "dhcp-option=3,%s\n" // gateway
168 "dhcp-lease-max=%d\n",
173 dhcp_start = nvram_get_int("dhcp_start");
174 dhcp_count = nvram_get_int("dhcp_num");
175 n = nvram_get_int("dhcpd_lmax");
177 "dhcp-range=%s%d,%s%d,%s,%dm\n" // lease config
178 "dhcp-option=3,%s\n" // gateway
179 "dhcp-lease-max=%d\n",
180 lan, dhcp_start, lan, dhcp_start + dhcp_count - 1, nvram_safe_get("lan_netmask"), dhcp_lease,
185 if (nvram_get_int("dhcpd_auth") >= 0) {
186 fprintf(f
, "dhcp-authoritative\n");
190 // avoid leasing wan ip incase the modem gives an ip in our range
191 fprintf(f
, "dhcp-host=01:02:03:04:05:06,%s\n", nvram_safe_get("wan_ipaddr"));
194 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
195 fprintf(f
, "dhcp-option=44,%s\n", nv
);
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 p
= nvram_safe_get("dhcpd_static"); // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
214 while ((e
= strchr(p
, '>')) != NULL
) {
225 if ((e
= strchr(buf
, '<')) == NULL
) continue;
230 if ((e
= strchr(ip
, '<')) == NULL
) continue;
232 if (strchr(ip
, '.') == NULL
) {
234 if ((ipn
<= 0) || (ipn
> 255)) continue;
235 sprintf(ipbuf
, "%s%d", lan
, ipn
);
239 if (inet_addr(ip
) == INADDR_NONE
) continue;
244 if ((hf
) && (*name
!= 0)) {
245 fprintf(hf
, "%s %s\n", ip
, name
);
248 if ((do_dhcpd
) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
249 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
257 #ifdef TCONFIG_OPENVPN
258 write_vpn_dnsmasq_config(f
);
261 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
268 if ((af
= fopen("/etc/dnsmasq.custom", "r")) != NULL
) {
269 while ((n
= fread(buf
, 1, sizeof(buf
), af
)) > 0) {
270 fwrite(buf
, 1, n
, f
);
280 unlink("/etc/resolv.conf");
281 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
287 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
288 f_read_string(dmpid
, buf
, sizeof(buf
));
289 pid_dnsmasq
= atol(buf
);
293 void stop_dnsmasq(void)
296 stop_service("dnsmasq");
300 killall("dnsmasq", SIGUSR1
); //!!TB - write dnsmasq stats to the system log before stopping
303 unlink("/etc/resolv.conf");
304 symlink(dmresolv
, "/etc/resolv.conf");
306 killall_tk("dnsmasq");
309 void clear_resolv(void)
311 _dprintf("%s\n", __FUNCTION__
);
313 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
316 void dns_to_resolv(void)
319 const dns_list_t
*dns
;
323 _dprintf("%s\n", __FUNCTION__
);
325 m
= umask(022); // 077 from pppoecd
326 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
327 dns
= get_dns(); // static buffer
328 if (dns
->count
== 0) {
329 // Put a pseudo DNS IP to trigger Connect On Demand
330 if ((nvram_match("ppp_demand", "1")) &&
331 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
332 fprintf(f
, "nameserver 1.1.1.1\n");
336 for (i
= 0; i
< dns
->count
; i
++) {
337 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
]));
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 //!!TB - miniupnpd - most of the code is stolen from Tarifa 034RC1 sources
368 #if 0 // read UUID from /proc/sys/kernel/random/uuid instead
369 void uuidstr_create(char *str
)
371 typedef unsigned int u_int32
;
372 typedef unsigned short u_int16
;
373 typedef unsigned char u_int8
;
375 static int uuid_count
= 0;
378 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
379 &d
[0], &d
[1], &d
[2], &d
[3], &d
[4], &d
[5]);
380 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
381 &d
[6], &d
[7], &d
[8], &d
[9], &d
[10], &d
[11]);
382 *((int *)&d
[12]) = uuid_count
++;
384 sprintf(str
, "fc4ec57e-b051-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
385 (u_int8
)d
[6], (u_int8
)d
[7], (u_int8
)d
[8], (u_int8
)d
[9], (u_int8
)d
[10],
386 (u_int8
)d
[11], (u_int8
)d
[12], (u_int8
)d
[13], (u_int8
)d
[14], (u_int8
)d
[15]);
390 void start_upnp(void)
394 if ((nvram_match("upnp_enable", "1")) && (get_wan_proto() != WP_DISABLED
)) {
397 "-i", nvram_safe_get("wan_iface"),
398 "-a", nvram_safe_get("lan_ipaddr"),
404 "-L", nvram_safe_get("lan_ifname"),
405 "-W", nvram_safe_get("wan_iface"),
406 "-I", nvram_safe_get("upnp_ssdp_interval"),
407 "-A", nvram_safe_get("upnp_max_age"));
412 char fname
[] = "/etc/miniupnpd.conf";
417 if ((!nvram_invmatch("upnp_enable", "0")) & (!nvram_invmatch("upnp_nat_pmp_enable", "0")) || (get_wan_proto() == WP_DISABLED
))
420 fp
= fopen(fname
, "w");
424 lanip
= nvram_safe_get("lan_ipaddr");
426 fprintf(fp
, "ext_ifname=%s\n", nvram_safe_get("wan_iface"));
427 fprintf(fp
, "listening_ip=%s\n", lanip
);
428 fprintf(fp
, "port=%s\n", nvram_safe_get("upnp_port"));
429 fprintf(fp
, "upnp_forward_chain=upnp\n");
430 fprintf(fp
, "upnp_nat_chain=upnp\n");
431 fprintf(fp
, "enable_upnp=%s\n", nvram_match("upnp_enable", "1") ? "yes" : "no");
432 fprintf(fp
, "enable_natpmp=%s\n", nvram_match("upnp_nat_pmp_enable", "1") ? "yes" : "no");
433 fprintf(fp
, "secure_mode=%s\n", nvram_match("upnp_secure_mode", "1") ? "yes" : "no");
434 fprintf(fp
, "system_uptime=no\n");
435 fprintf(fp
, "notify_interval=%d\n", nvram_get_int("upnp_ssdp_interval"));
437 r
= nvram_get_int("upnp_bitrate_up");
438 if (r
> 0) fprintf(fp
, "bitrate_up=%d\n", r
);
439 r
= nvram_get_int("upnp_bitrate_down");
440 if (r
> 0) fprintf(fp
, "bitrate_down=%d\n", r
);
442 if (nvram_match("upnp_clean_ruleset_enable", "1")) {
443 interval
= nvram_get_int("upnp_clean_ruleset_interval");
444 if (interval
< 60) interval
= 60;
445 fprintf(fp
, "clean_ruleset_interval=%d\n", interval
);
446 fprintf(fp
, "clean_ruleset_threshold=%d\n", nvram_get_int("upnp_clean_ruleset_threshold"));
449 fprintf(fp
,"clean_ruleset_interval=0\n");
451 if (nvram_match("upnp_mnp", "1")) {
452 int https
= nvram_match("https_enable", "1");
453 fprintf(fp
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
454 https
? "s" : "", lanip
,
455 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
458 // Empty parameters are not included into XML service description
459 fprintf(fp
, "presentation_url=\n");
462 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
463 fprintf(fp
, "uuid=%s\n", uuid
);
465 if ((nvram_get_int("upnp_min_port_int") > 0) &&
466 (nvram_get_int("upnp_max_port_int") > 0) &&
467 (nvram_get_int("upnp_min_port_ext") > 0) &&
468 (nvram_get_int("upnp_max_port_ext") > 0)) {
470 fprintf(fp
, "allow %s", nvram_safe_get("upnp_min_port_int"));
471 fprintf(fp
, "-%s", nvram_safe_get("upnp_max_port_int"));
472 fprintf(fp
, " %s/24", lanip
);
473 fprintf(fp
, " %s", nvram_safe_get("upnp_min_port_ext"));
474 fprintf(fp
, "-%s\n", nvram_safe_get("upnp_max_port_ext"));
477 // by default allow only redirection of ports above 1024
478 fprintf(fp
, "allow 1024-65535 %s/24 1024-65535\n", lanip
);
480 fprintf(fp
, "deny 0-65535 0.0.0.0/0 0-65535\n");
484 xstart("miniupnpd", "-f", fname
);
493 killall_tk("miniupnpd");
498 killall_tk("miniupnpd");
499 unlink("/var/run/miniupnpd.pid");
500 unlink("/etc/miniupnpd.conf");
504 // -----------------------------------------------------------------------------
506 static pid_t pid_crond
= -1;
508 void start_cron(void)
510 _dprintf("%s\n", __FUNCTION__
);
514 char *argv
[] = { "crond", "-l", "9", NULL
};
516 if (nvram_contains_word("log_events", "crond")) argv
[1] = NULL
;
517 _eval(argv
, NULL
, 0, NULL
);
519 if (!nvram_contains_word("debug_norestart", "crond")) {
520 pid_crond
= -2; // intentionally fail test_cron()
527 _dprintf("%s\n", __FUNCTION__
);
533 // -----------------------------------------------------------------------------
535 // Written by Sparq in 2002/07/16
536 void start_zebra(void)
541 char *lan_tx
= nvram_safe_get("dr_lan_tx");
542 char *lan_rx
= nvram_safe_get("dr_lan_rx");
543 char *wan_tx
= nvram_safe_get("dr_wan_tx");
544 char *wan_rx
= nvram_safe_get("dr_wan_rx");
546 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
551 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
556 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
557 char *lan_ifname
= nvram_safe_get("lan_ifname");
558 char *wan_ifname
= nvram_safe_get("wan_ifname");
560 fprintf(fp
, "router rip\n");
561 fprintf(fp
, "network %s\n", lan_ifname
);
562 fprintf(fp
, "network %s\n", wan_ifname
);
563 fprintf(fp
, "redistribute connected\n");
564 //fprintf(fp, "redistribute static\n");
566 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
567 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
569 fprintf(fp
, "interface %s\n", lan_ifname
);
570 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
571 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
573 fprintf(fp
, "interface %s\n", wan_ifname
);
574 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
575 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
577 fprintf(fp
, "router rip\n");
578 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
579 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
580 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
581 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
582 fprintf(fp
, "access-list private deny any\n");
584 //fprintf(fp, "debug rip events\n");
585 //fprintf(fp, "log file /etc/ripd.log\n");
589 xstart("zebra", "-d", "-f", "/etc/zebra.conf");
590 xstart("ripd", "-d", "-f", "/etc/ripd.conf");
594 void stop_zebra(void)
597 killall("zebra", SIGTERM
);
598 killall("ripd", SIGTERM
);
600 unlink("/etc/zebra.conf");
601 unlink("/etc/ripd.conf");
605 // -----------------------------------------------------------------------------
607 void start_syslog(void)
620 if (nvram_match("log_remote", "1")) {
621 nv
= nvram_safe_get("log_remoteip");
623 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
629 if (nvram_match("log_file", "1")) {
637 _eval(argv
, NULL
, 0, NULL
);
642 _eval(argv
, NULL
, 0, NULL
);
645 // used to be available in syslogd -m
646 n
= nvram_get_int("log_mark");
648 sprintf(s
, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
649 (n
< 60) ? "*/30" : "0", (n
< 120) ? "*" : "*/2");
662 argv
[2] = nvram_get("log_mark");
665 if (nvram_match("log_remote", "1")) {
666 nv
= nvram_safe_get("log_remoteip");
668 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
674 if (nvram_match("log_file", "1")) {
682 _eval(argv
, NULL
, 0, NULL
);
687 _eval(argv
, NULL
, 0, NULL
);
693 void stop_syslog(void)
695 killall("klogd", SIGTERM
);
696 killall("syslogd", SIGTERM
);
699 // -----------------------------------------------------------------------------
701 static pid_t pid_igmp
= -1;
703 void start_igmp_proxy(void)
708 if (nvram_match("multicast_pass", "1")) {
709 switch (get_wan_proto()) {
719 xstart("igmprt", "-f", "-i", nvram_safe_get(p
));
721 if (!nvram_contains_word("debug_norestart", "igmprt")) {
727 void stop_igmp_proxy(void)
730 killall("igmprt", SIGTERM
);
734 // -----------------------------------------------------------------------------
738 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
741 void start_ntpc(void)
747 if (nvram_get_int("ntp_updates") >= 0) {
748 xstart("ntpsync", "--init");
754 killall("ntpsync", SIGTERM
);
757 // -----------------------------------------------------------------------------
759 static void stop_rstats(void)
765 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
766 if (kill(pid
, SIGTERM
) != 0) break;
771 static void start_rstats(int new)
773 if (nvram_match("rstats_enable", "1")) {
775 if (new) xstart("rstats", "--new");
776 else xstart("rstats");
780 // -----------------------------------------------------------------------------
785 * Return non-zero if we created the directory,
786 * and zero if it already existed.
788 int mkdir_if_none(char *dir
)
791 if (!(dp
=opendir(dir
))) {
800 char *get_full_storage_path(char *val
)
802 static char buf
[128];
806 len
= sprintf(buf
, "%s", val
);
808 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
810 if (len
> 1 && buf
[len
- 1] == '/')
816 char *nvram_storage_path(char *var
)
818 char *val
= nvram_safe_get(var
);
819 return get_full_storage_path(val
);
824 char vsftpd_conf
[] = "/etc/vsftpd.conf";
825 char vsftpd_users
[] = "/etc/vsftpd.users";
826 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
829 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
830 void start_ftpd(void)
837 killall("vsftpd", SIGTERM
);
838 if (!nvram_get_int("ftp_enable")) return;
840 mkdir_if_none(vsftpd_users
);
841 mkdir_if_none("/var/run/vsftpd");
843 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
846 if (nvram_match("ftp_super", "1"))
849 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
850 if ((f
= fopen(tmp
, "w")))
853 "dirlist_enable=yes\n"
855 "download_enable=yes\n");
860 #ifdef TCONFIG_SAMBASRV
861 if (nvram_match("smbd_cset", "utf8"))
862 fprintf(fp
, "utf8=yes\n");
865 if (nvram_invmatch("ftp_anonymous", "0"))
868 "anon_allow_writable_root=yes\n"
869 "anon_world_readable_only=no\n"
873 sprintf(tmp
, "%s/ftp", vsftpd_users
);
874 if ((f
= fopen(tmp
, "w")))
876 if (nvram_match("ftp_dirlist", "0"))
877 fprintf(f
, "dirlist_enable=yes\n");
878 if (nvram_match("ftp_anonymous", "1") ||
879 nvram_match("ftp_anonymous", "3"))
880 fprintf(f
, "write_enable=yes\n");
881 if (nvram_match("ftp_anonymous", "1") ||
882 nvram_match("ftp_anonymous", "2"))
883 fprintf(f
, "download_enable=yes\n");
886 if (nvram_match("ftp_anonymous", "1") ||
887 nvram_match("ftp_anonymous", "3"))
889 "anon_upload_enable=yes\n"
890 "anon_mkdir_write_enable=yes\n"
891 "anon_other_write_enable=yes\n");
893 fprintf(fp
, "anonymous_enable=no\n");
897 "dirmessage_enable=yes\n"
898 "download_enable=no\n"
899 "dirlist_enable=no\n"
901 "syslog_enable=yes\n"
905 "chroot_local_user=yes\n"
907 "user_config_dir=%s\n"
909 vsftpd_users
, vsftpd_passwd
);
911 if (nvram_match("log_ftp", "1")) {
912 fprintf(fp
, "log_ftp_protocol=yes\n");
915 fprintf(fp
, "log_ftp_protocol=no\n");
918 fprintf(fp
, "listen=yes\nlisten_port=%s\nbackground=yes\n",
919 nvram_get("ftp_port") ? : "21");
920 fprintf(fp
, "max_clients=%s\n", nvram_get("ftp_max") ? : "0");
921 fprintf(fp
, "max_per_ip=%s\n", nvram_get("ftp_ipmax") ? : "0");
922 fprintf(fp
, "idle_session_timeout=%s\n", nvram_get("ftp_staytimeout") ? : "300");
923 fprintf(fp
, "use_sendfile=no\n");
924 //fprintf(fp, "ftpd_banner=Welcome to the %s FTP service.\n", nvram_get("t_model_name") ? : "router");
927 fprintf(fp
, "anon_max_rate=%d\nlocal_max_rate=%d\n",
928 atoi(nvram_safe_get("ftp_anonrate")) * 1024,
929 atoi(nvram_safe_get("ftp_rate")) * 1024);
931 fprintf(fp
, "%s\n\n", nvram_safe_get("ftp_custom"));
935 /* prepare passwd file and default users */
936 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
939 fprintf(fp
, /* anonymous, admin, nobody */
940 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
941 "%s:%s:0:0:root:/:/sbin/nologin\n"
942 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
943 nvram_storage_path("ftp_anonroot"), "admin",
944 nvram_match("ftp_super", "1") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
949 char *user
, *pass
, *rights
;
951 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
954 username<password<rights
962 while ((q
= strsep(&p
, ">")) != NULL
) {
963 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
964 if (!user
|| !pass
) continue;
967 if (strncmp(rights
, "Private", 7) == 0)
969 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
973 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
975 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
976 user
, crypt(pass
, "$1$"), user
, tmp
);
979 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
980 if ((f
= fopen(tmp
, "w")))
983 if (nvram_invmatch("ftp_dirlist", "1"))
984 strcat(tmp
, "dirlist_enable=yes\n");
985 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
986 strcat(tmp
, "download_enable=yes\n");
987 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
988 strcat(tmp
, "write_enable=yes\n");
1003 void stop_ftpd(void)
1006 killall("vsftpd", SIGTERM
);
1007 unlink(vsftpd_passwd
);
1008 unlink(vsftpd_conf
);
1009 eval("rm", "-rf", vsftpd_users
);
1013 // -----------------------------------------------------------------------------
1017 #ifdef TCONFIG_SAMBASRV
1018 void kill_samba(int sig
)
1020 killall("smbd", sig
);
1021 killall("nmbd", sig
);
1025 void start_samba(void)
1027 #ifdef TCONFIG_SAMBASRV
1035 mode
= nvram_get_int("smbd_enable");
1036 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1039 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
) {
1040 perror("/etc/smb.conf");
1044 fprintf(fp
, "[global]\n"
1045 " interfaces = %s\n"
1046 " bind interfaces only = yes\n"
1048 " server string = %s\n"
1049 " guest account = nobody\n"
1051 " browseable = yes\n"
1053 " guest only = no\n"
1055 " syslog only = yes\n"
1057 " encrypt passwords = yes\n"
1058 " local master = %s\n"
1059 " preserve case = yes\n"
1060 " short preserve case = yes\n",
1061 nvram_get("lan_ifname") ? : "br0",
1062 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1063 nvram_get("router_name") ? : "Tomato",
1064 mode
== 2 ? "user" : "share",
1065 nvram_get_int("smbd_loglevel"),
1066 nvram_get_int("smbd_master") ? "yes" : "no"
1069 if (nvram_invmatch("smbd_cpage", "")) {
1070 char *cp
= nvram_get("smbd_cpage");
1072 fprintf(fp
, " client code page = %s\n", cp
);
1073 sprintf(nlsmod
, "nls_cp%s", cp
);
1075 cp
= nvram_get("smbd_nlsmod");
1076 if ((cp
) && (*cp
!= 0) && (strcmp(cp
, nlsmod
) != 0))
1080 nvram_set("smbd_nlsmod", nlsmod
);
1083 if (nvram_match("smbd_cset", "utf8"))
1084 fprintf(fp
, " coding system = utf8\n");
1085 else if (nvram_invmatch("smbd_cset", ""))
1086 fprintf(fp
, " character set = %s\n", nvram_get("smbd_cset"));
1088 fprintf(fp
, "%s\n\n", nvram_safe_get("smbd_custom"));
1090 /* configure shares */
1094 char *name
, *path
, *comment
, *writeable
, *hidden
;
1097 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1099 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1102 while ((q
= strsep(&p
, ">")) != NULL
) {
1103 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1104 if (!path
|| !name
) continue;
1107 fprintf(fp
, "\n[%s]\n", name
);
1110 fprintf(fp
, " path = %s\n", path
);
1113 if (!strcmp(writeable
, "1"))
1114 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1115 if (!strcmp(hidden
, "1"))
1116 fprintf(fp
, " browseable = no\n");
1120 fprintf(fp
, " comment = %s\n", comment
);
1127 /* share everything below MOUNT_ROOT */
1128 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1129 while ((dp
= readdir(dir
))) {
1130 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1132 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1133 fprintf(fp
, "\n[%s]\n path = %s/%s\n",
1134 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1135 if (nvram_match("smbd_autoshare", "3")) // Hidden
1136 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1137 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1138 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1139 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1145 if (dir
) closedir(dir
);
1148 /* by default share MOUNT_ROOT as read-only */
1149 fprintf(fp
, "\n[share]\n"
1157 mkdir_if_none("/var/run/samba");
1158 mkdir_if_none("/etc/samba");
1160 /* write smbpasswd */
1161 eval("smbpasswd", "-a", "nobody", "\"\"");
1164 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1166 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1170 int ret1
= 0, ret2
= 0;
1171 /* start samba if it's not already running */
1172 if (pidof("nmbd") <= 0)
1173 ret1
= eval("nmbd", "-D");
1174 if (pidof("smbd") <= 0)
1175 ret2
= eval("smbd", "-D");
1177 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1181 void stop_samba(void)
1183 #ifdef TCONFIG_SAMBASRV
1184 kill_samba(SIGTERM
);
1185 sleep(2); /* wait for smbd to finish */
1187 if (nvram_invmatch("smbd_nlsmod", "")) {
1188 modprobe_r(nvram_get("smbd_nlsmod"));
1189 nvram_set("smbd_nlsmod", "");
1193 unlink("/var/log/smb");
1194 unlink("/var/log/nmb");
1195 eval("rm", "-rf", "/var/run/samba");
1199 void restart_nas_services(int start
)
1201 /* restart all NAS applications */
1202 #ifdef TCONFIG_SAMBASRV
1203 if (start
&& nvram_get_int("smbd_enable"))
1206 kill_samba(SIGTERM
);
1209 if (start
&& nvram_get_int("ftp_enable"))
1212 killall("vsftpd", SIGTERM
);
1216 // -----------------------------------------------------------------------------
1218 static void _check(pid_t
*pid
, const char *name
, void (*func
)(void) )
1221 if (kill(*pid
, 0) != 0) {
1222 if ((*pid
= pidof(name
)) == -1) func();
1227 void check_services(void)
1229 _check(&pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1230 _check(&pid_crond
, "crond", start_cron
);
1231 _check(&pid_igmp
, "igmprt", start_igmp_proxy
);
1234 // -----------------------------------------------------------------------------
1236 void start_services(void)
1238 static int once
= 1;
1244 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1245 if (nvram_match("sshd_eas", "1")) start_sshd();
1261 #ifdef TCONFIG_SAMBA
1264 start_samba(); // !!TB - Samba
1265 start_ftpd(); // !!TB - FTP Server
1266 #ifdef TCONFIG_OPENVPN
1271 void stop_services(void)
1275 stop_ftpd(); // !!TB - FTP Server
1276 stop_samba(); // !!TB - Samba
1277 #ifdef TCONFIG_SAMBA
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 if (strcmp(act
, "start") == 0) action
= A_START
;
1321 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1322 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1325 _dprintf("%s %s service=%s action=%s\n", __FILE__
, __FUNCTION__
, service
, act
);
1328 if (strcmp(service
, "dhcpc") == 0) {
1329 if (action
& A_STOP
) stop_dhcpc();
1330 if (action
& A_START
) start_dhcpc();
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
, "admin") == 0) {
1410 if (action
& A_STOP
) {
1415 stop_firewall(); start_firewall(); // always restarted
1416 if (action
& A_START
) {
1419 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1420 if (nvram_match("sshd_eas", "1")) start_sshd();
1425 if (strcmp(service
, "ddns") == 0) {
1426 if (action
& A_STOP
) stop_ddns();
1427 if (action
& A_START
) start_ddns();
1431 if (strcmp(service
, "ntpc") == 0) {
1432 if (action
& A_STOP
) stop_ntpc();
1433 if (action
& A_START
) start_ntpc();
1437 if (strcmp(service
, "logging") == 0) {
1438 if (action
& A_STOP
) {
1442 stop_firewall(); start_firewall(); // always restarted
1443 if (action
& A_START
) {
1450 if (strcmp(service
, "crond") == 0) {
1451 if (action
& A_STOP
) {
1454 if (action
& A_START
) {
1460 if (strcmp(service
, "upgrade") == 0) {
1461 if (action
& A_START
) {
1466 stop_ftpd(); // !!TB - FTP Server
1467 stop_samba(); // !!TB - Samba
1475 killall("rstats", SIGTERM
);
1476 killall("buttons", SIGTERM
);
1478 remove_storage_main(); // !!TB - USB Support
1479 stop_usb(); // !!TB - USB Support
1485 if (strcmp(service
, "cifs") == 0) {
1486 if (action
& A_STOP
) stop_cifs();
1487 if (action
& A_START
) start_cifs();
1492 #ifdef TCONFIG_JFFS2
1493 if (strcmp(service
, "jffs2") == 0) {
1494 if (action
& A_STOP
) stop_jffs2();
1495 if (action
& A_START
) start_jffs2();
1500 if (strcmp(service
, "routing") == 0) {
1501 if (action
& A_STOP
) {
1503 do_static_routes(0); // remove old '_saved'
1504 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1508 if (action
& A_START
) {
1509 do_static_routes(1); // add new
1511 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1516 if (strcmp(service
, "ctnf") == 0) {
1517 if (action
& A_START
) {
1525 if (strcmp(service
, "wan") == 0) {
1526 if (action
& A_STOP
) {
1527 if (get_wan_proto() == WP_PPPOE
) {
1530 stop_singe_pppoe(PPPOE0
);
1531 if (((action
& A_START
) == 0) && (nvram_match("ppp_demand", "1"))) {
1533 start_pppoe(PPPOE0
);
1542 if (action
& A_START
) {
1543 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1545 if (get_wan_proto() == WP_PPPOE
) {
1546 stop_singe_pppoe(PPPOE0
);
1547 start_pppoe(PPPOE0
);
1548 if (nvram_invmatch("ppp_demand", "1")) {
1561 if (strcmp(service
, "net") == 0) {
1562 if (action
& A_STOP
) {
1567 if (action
& A_START
) {
1575 if (strcmp(service
, "rstats") == 0) {
1576 if (action
& A_STOP
) stop_rstats();
1577 if (action
& A_START
) start_rstats(0);
1581 if (strcmp(service
, "rstatsnew") == 0) {
1582 if (action
& A_STOP
) stop_rstats();
1583 if (action
& A_START
) start_rstats(1);
1587 if (strcmp(service
, "sched") == 0) {
1588 if (action
& A_STOP
) stop_sched();
1589 if (action
& A_START
) start_sched();
1593 // !!TB - USB Support
1594 if (strcmp(service
, "usb") == 0) {
1595 if (action
& A_STOP
) stop_usb();
1596 if (action
& A_START
) {
1598 // restart Samba and ftp since they may be killed by stop_usb()
1599 restart_nas_services(1);
1605 // !!TB - FTP Server
1606 if (strcmp(service
, "ftpd") == 0) {
1607 if (action
& A_STOP
) stop_ftpd();
1611 if (action
& A_START
) start_ftpd();
1616 #ifdef TCONFIG_SAMBASRV
1618 if (strcmp(service
, "samba") == 0) {
1619 if (action
& A_STOP
) stop_samba();
1620 if (action
& A_START
) {
1629 if (strcmp(service
, "smbd") == 0) {
1630 if (action
& A_STOP
) stop_smbd();
1631 if (action
& A_START
) start_smbd();
1635 if (strcmp(service
, "test1") == 0) {
1636 if (action
& A_STOP
) stop_test_1();
1637 if (action
& A_START
) start_test_1();
1642 #ifdef TCONFIG_OPENVPN
1643 if (strncmp(service
, "vpnclient", 9) == 0) {
1644 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
1645 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
1649 if (strncmp(service
, "vpnserver", 9) == 0) {
1650 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
1651 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
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
)) {
1688 else if (--n
< 0) break;
1693 int service_main(int argc
, char *argv
[])
1695 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
1696 do_service(argv
[1], argv
[2], 1);
1697 printf("\nDone.\n");
1701 void start_service(const char *name
)
1703 do_service(name
, "start", 0);
1706 void stop_service(const char *name
)
1708 do_service(name
, "stop", 0);