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 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
264 if ((af
= fopen("/etc/dnsmasq.custom", "r")) != NULL
) {
265 while ((n
= fread(buf
, 1, sizeof(buf
), af
)) > 0) {
266 fwrite(buf
, 1, n
, f
);
276 unlink("/etc/resolv.conf");
277 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
283 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
284 f_read_string(dmpid
, buf
, sizeof(buf
));
285 pid_dnsmasq
= atol(buf
);
289 void stop_dnsmasq(void)
292 stop_service("dnsmasq");
298 unlink("/etc/resolv.conf");
299 symlink(dmresolv
, "/etc/resolv.conf");
301 killall_tk("dnsmasq");
304 void clear_resolv(void)
306 _dprintf("%s\n", __FUNCTION__
);
308 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
311 void dns_to_resolv(void)
314 const dns_list_t
*dns
;
318 _dprintf("%s\n", __FUNCTION__
);
320 m
= umask(022); // 077 from pppoecd
321 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
322 dns
= get_dns(); // static buffer
323 if (dns
->count
== 0) {
324 // Put a pseudo DNS IP to trigger Connect On Demand
325 if ((nvram_match("ppp_demand", "1")) &&
326 (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto", "pptp") || nvram_match("wan_proto", "l2tp"))) {
327 fprintf(f
, "nameserver 1.1.1.1\n");
331 for (i
= 0; i
< dns
->count
; i
++) {
332 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
]));
340 // -----------------------------------------------------------------------------
342 void start_httpd(void)
345 if (!nvram_match("http_enable", "0")) {
348 if (!nvram_match("https_enable", "0")) {
349 xstart("httpd", "-s");
354 void stop_httpd(void)
359 // -----------------------------------------------------------------------------
361 //!!TB - miniupnpd - most of the code is stolen from Tarifa 034RC1 sources
363 #if 0 // read UUID from /proc/sys/kernel/random/uuid instead
364 void uuidstr_create(char *str
)
366 typedef unsigned int u_int32
;
367 typedef unsigned short u_int16
;
368 typedef unsigned char u_int8
;
370 static int uuid_count
= 0;
373 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
374 &d
[0], &d
[1], &d
[2], &d
[3], &d
[4], &d
[5]);
375 sscanf(nvram_safe_get("lan_hwaddr"), "%x:%x:%x:%x:%x:%x",
376 &d
[6], &d
[7], &d
[8], &d
[9], &d
[10], &d
[11]);
377 *((int *)&d
[12]) = uuid_count
++;
379 sprintf(str
, "fc4ec57e-b051-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
380 (u_int8
)d
[6], (u_int8
)d
[7], (u_int8
)d
[8], (u_int8
)d
[9], (u_int8
)d
[10],
381 (u_int8
)d
[11], (u_int8
)d
[12], (u_int8
)d
[13], (u_int8
)d
[14], (u_int8
)d
[15]);
385 void start_upnp(void)
389 if ((nvram_match("upnp_enable", "1")) && (get_wan_proto() != WP_DISABLED
)) {
392 "-i", nvram_safe_get("wan_iface"),
393 "-a", nvram_safe_get("lan_ipaddr"),
399 "-L", nvram_safe_get("lan_ifname"),
400 "-W", nvram_safe_get("wan_iface"),
401 "-I", nvram_safe_get("upnp_ssdp_interval"),
402 "-A", nvram_safe_get("upnp_max_age"));
407 char fname
[] = "/etc/miniupnpd.conf";
412 if ((!nvram_invmatch("upnp_enable", "0")) & (!nvram_invmatch("upnp_nat_pmp_enable", "0")) || (get_wan_proto() == WP_DISABLED
))
415 fp
= fopen(fname
, "w");
419 lanip
= nvram_safe_get("lan_ipaddr");
421 fprintf(fp
, "ext_ifname=%s\n", nvram_safe_get("wan_iface"));
422 fprintf(fp
, "listening_ip=%s\n", lanip
);
423 fprintf(fp
, "port=%s\n", nvram_safe_get("upnp_port"));
424 fprintf(fp
, "upnp_forward_chain=upnp\n");
425 fprintf(fp
, "upnp_nat_chain=upnp\n");
426 fprintf(fp
, "enable_upnp=%s\n", nvram_match("upnp_enable", "1") ? "yes" : "no");
427 fprintf(fp
, "enable_natpmp=%s\n", nvram_match("upnp_nat_pmp_enable", "1") ? "yes" : "no");
428 fprintf(fp
, "secure_mode=%s\n", nvram_match("upnp_secure_mode", "1") ? "yes" : "no");
429 fprintf(fp
, "system_uptime=no\n");
430 fprintf(fp
, "notify_interval=%d\n", nvram_get_int("upnp_ssdp_interval"));
432 r
= nvram_get_int("upnp_bitrate_up");
433 if (r
> 0) fprintf(fp
, "bitrate_up=%d\n", r
);
434 r
= nvram_get_int("upnp_bitrate_down");
435 if (r
> 0) fprintf(fp
, "bitrate_down=%d\n", r
);
437 if (nvram_match("upnp_clean_ruleset_enable", "1")) {
438 interval
= nvram_get_int("upnp_clean_ruleset_interval");
439 if (interval
< 60) interval
= 60;
440 fprintf(fp
, "clean_ruleset_interval=%d\n", interval
);
441 fprintf(fp
, "clean_ruleset_threshold=%d\n", nvram_get_int("upnp_clean_ruleset_threshold"));
444 fprintf(fp
,"clean_ruleset_interval=0\n");
446 if (nvram_match("upnp_mnp", "1")) {
447 int https
= nvram_match("https_enable", "1");
448 fprintf(fp
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
449 https
? "s" : "", lanip
,
450 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
453 // Empty parameters are not included into XML service description
454 fprintf(fp
, "presentation_url=\n");
457 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
458 fprintf(fp
, "uuid=%s\n", uuid
);
460 if ((nvram_get_int("upnp_min_port_int") > 0) &&
461 (nvram_get_int("upnp_max_port_int") > 0) &&
462 (nvram_get_int("upnp_min_port_ext") > 0) &&
463 (nvram_get_int("upnp_max_port_ext") > 0)) {
465 fprintf(fp
, "allow %s", nvram_safe_get("upnp_min_port_int"));
466 fprintf(fp
, "-%s", nvram_safe_get("upnp_max_port_int"));
467 fprintf(fp
, " %s/24", lanip
);
468 fprintf(fp
, " %s", nvram_safe_get("upnp_min_port_ext"));
469 fprintf(fp
, "-%s\n", nvram_safe_get("upnp_max_port_ext"));
472 // by default allow only redirection of ports above 1024
473 fprintf(fp
, "allow 1024-65535 %s/24 1024-65535\n", lanip
);
475 fprintf(fp
, "deny 0-65535 0.0.0.0/0 0-65535\n");
479 xstart("miniupnpd", "-f", fname
);
488 killall_tk("miniupnpd");
493 killall_tk("miniupnpd");
494 unlink("/var/run/miniupnpd.pid");
495 unlink("/etc/miniupnpd.conf");
499 // -----------------------------------------------------------------------------
501 static pid_t pid_crond
= -1;
503 void start_cron(void)
505 _dprintf("%s\n", __FUNCTION__
);
509 char *argv
[] = { "crond", "-l", "9", NULL
};
511 if (nvram_contains_word("log_events", "crond")) argv
[1] = NULL
;
512 _eval(argv
, NULL
, 0, NULL
);
514 if (!nvram_contains_word("debug_norestart", "crond")) {
515 pid_crond
= -2; // intentionally fail test_cron()
522 _dprintf("%s\n", __FUNCTION__
);
528 // -----------------------------------------------------------------------------
530 // Written by Sparq in 2002/07/16
531 void start_zebra(void)
536 char *lan_tx
= nvram_safe_get("dr_lan_tx");
537 char *lan_rx
= nvram_safe_get("dr_lan_rx");
538 char *wan_tx
= nvram_safe_get("dr_wan_tx");
539 char *wan_rx
= nvram_safe_get("dr_wan_rx");
541 if ((*lan_tx
== '0') && (*lan_rx
== '0') && (*wan_tx
== '0') && (*wan_rx
== '0')) {
546 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
551 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
552 char *lan_ifname
= nvram_safe_get("lan_ifname");
553 char *wan_ifname
= nvram_safe_get("wan_ifname");
555 fprintf(fp
, "router rip\n");
556 fprintf(fp
, "network %s\n", lan_ifname
);
557 fprintf(fp
, "network %s\n", wan_ifname
);
558 fprintf(fp
, "redistribute connected\n");
559 //fprintf(fp, "redistribute static\n");
561 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
562 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
564 fprintf(fp
, "interface %s\n", lan_ifname
);
565 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
566 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
568 fprintf(fp
, "interface %s\n", wan_ifname
);
569 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
570 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
572 fprintf(fp
, "router rip\n");
573 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
574 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
575 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
576 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
577 fprintf(fp
, "access-list private deny any\n");
579 //fprintf(fp, "debug rip events\n");
580 //fprintf(fp, "log file /etc/ripd.log\n");
584 xstart("zebra", "-d", "-f", "/etc/zebra.conf");
585 xstart("ripd", "-d", "-f", "/etc/ripd.conf");
589 void stop_zebra(void)
592 killall("zebra", SIGTERM
);
593 killall("ripd", SIGTERM
);
595 unlink("/etc/zebra.conf");
596 unlink("/etc/ripd.conf");
600 // -----------------------------------------------------------------------------
602 void start_syslog(void)
615 if (nvram_match("log_remote", "1")) {
616 nv
= nvram_safe_get("log_remoteip");
618 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
624 if (nvram_match("log_file", "1")) {
632 _eval(argv
, NULL
, 0, NULL
);
637 _eval(argv
, NULL
, 0, NULL
);
640 // used to be available in syslogd -m
641 n
= nvram_get_int("log_mark");
643 sprintf(s
, "cru a syslogdmark \"%s %s * * * logger -p syslog.info -- -- MARK --\"",
644 (n
< 60) ? "*/30" : "0", (n
< 120) ? "*" : "*/2");
657 argv
[2] = nvram_get("log_mark");
660 if (nvram_match("log_remote", "1")) {
661 nv
= nvram_safe_get("log_remoteip");
663 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
669 if (nvram_match("log_file", "1")) {
677 _eval(argv
, NULL
, 0, NULL
);
682 _eval(argv
, NULL
, 0, NULL
);
688 void stop_syslog(void)
690 killall("klogd", SIGTERM
);
691 killall("syslogd", SIGTERM
);
694 // -----------------------------------------------------------------------------
696 static pid_t pid_igmp
= -1;
698 void start_igmp_proxy(void)
703 if (nvram_match("multicast_pass", "1")) {
704 switch (get_wan_proto()) {
714 xstart("igmprt", "-f", "-i", nvram_safe_get(p
));
716 if (!nvram_contains_word("debug_norestart", "igmprt")) {
722 void stop_igmp_proxy(void)
725 killall("igmprt", SIGTERM
);
729 // -----------------------------------------------------------------------------
733 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
736 void start_ntpc(void)
742 if (nvram_get_int("ntp_updates") >= 0) {
743 xstart("ntpsync", "--init");
749 killall("ntpsync", SIGTERM
);
752 // -----------------------------------------------------------------------------
754 static void stop_rstats(void)
760 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
761 if (kill(pid
, SIGTERM
) != 0) break;
766 static void start_rstats(int new)
768 if (nvram_match("rstats_enable", "1")) {
770 if (new) xstart("rstats", "--new");
771 else xstart("rstats");
775 // -----------------------------------------------------------------------------
780 * Return non-zero if we created the directory,
781 * and zero if it already existed.
783 int mkdir_if_none(char *dir
)
786 if (!(dp
=opendir(dir
))) {
795 char *get_full_storage_path(char *val
)
797 static char buf
[128];
801 len
= sprintf(buf
, "%s", val
);
803 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
805 if (len
> 1 && buf
[len
- 1] == '/')
811 char *nvram_storage_path(char *var
)
813 char *val
= nvram_safe_get(var
);
814 return get_full_storage_path(val
);
819 char vsftpd_conf
[] = "/etc/vsftpd.conf";
820 char vsftpd_users
[] = "/etc/vsftpd.users";
821 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
824 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
825 void start_ftpd(void)
832 killall("vsftpd", SIGTERM
);
833 if (!nvram_get_int("ftp_enable")) return;
835 mkdir_if_none(vsftpd_users
);
836 mkdir_if_none("/var/run/vsftpd");
838 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
841 if (nvram_match("ftp_super", "1"))
844 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
845 if ((f
= fopen(tmp
, "w")))
848 "dirlist_enable=yes\n"
850 "download_enable=yes\n");
855 #ifdef TCONFIG_SAMBASRV
856 if (nvram_match("smbd_cset", "utf8"))
857 fprintf(fp
, "utf8=yes\n");
860 if (nvram_invmatch("ftp_anonymous", "0"))
863 "anon_allow_writable_root=yes\n"
864 "anon_world_readable_only=no\n"
868 sprintf(tmp
, "%s/ftp", vsftpd_users
);
869 if ((f
= fopen(tmp
, "w")))
871 if (nvram_match("ftp_dirlist", "0"))
872 fprintf(f
, "dirlist_enable=yes\n");
873 if (nvram_match("ftp_anonymous", "1") ||
874 nvram_match("ftp_anonymous", "3"))
875 fprintf(f
, "write_enable=yes\n");
876 if (nvram_match("ftp_anonymous", "1") ||
877 nvram_match("ftp_anonymous", "2"))
878 fprintf(f
, "download_enable=yes\n");
881 if (nvram_match("ftp_anonymous", "1") ||
882 nvram_match("ftp_anonymous", "3"))
884 "anon_upload_enable=yes\n"
885 "anon_mkdir_write_enable=yes\n"
886 "anon_other_write_enable=yes\n");
888 fprintf(fp
, "anonymous_enable=no\n");
892 "dirmessage_enable=yes\n"
893 "download_enable=no\n"
894 "dirlist_enable=no\n"
896 "syslog_enable=yes\n"
900 "chroot_local_user=yes\n"
902 "user_config_dir=%s\n"
904 vsftpd_users
, vsftpd_passwd
);
906 if (nvram_match("log_ftp", "1")) {
907 fprintf(fp
, "log_ftp_protocol=yes\n");
910 fprintf(fp
, "log_ftp_protocol=no\n");
913 fprintf(fp
, "listen=yes\nlisten_port=%s\nbackground=yes\n",
914 nvram_get("ftp_port") ? : "21");
915 fprintf(fp
, "max_clients=%s\n", nvram_get("ftp_max") ? : "0");
916 fprintf(fp
, "max_per_ip=%s\n", nvram_get("ftp_ipmax") ? : "0");
917 fprintf(fp
, "idle_session_timeout=%s\n", nvram_get("ftp_staytimeout") ? : "300");
918 fprintf(fp
, "use_sendfile=no\n");
919 //fprintf(fp, "ftpd_banner=Welcome to the %s FTP service.\n", nvram_get("t_model_name") ? : "router");
922 fprintf(fp
, "anon_max_rate=%d\nlocal_max_rate=%d\n",
923 atoi(nvram_safe_get("ftp_anonrate")) * 1024,
924 atoi(nvram_safe_get("ftp_rate")) * 1024);
926 fprintf(fp
, "%s\n\n", nvram_safe_get("ftp_custom"));
930 /* prepare passwd file and default users */
931 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
934 fprintf(fp
, /* anonymous, admin, nobody */
935 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
936 "%s:%s:0:0:root:/:/sbin/nologin\n"
937 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
938 nvram_storage_path("ftp_anonroot"), "admin",
939 nvram_match("ftp_super", "1") ? crypt(nvram_safe_get("http_passwd"), "$1$") : "x",
944 char *user
, *pass
, *rights
;
946 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
949 username<password<rights
957 while ((q
= strsep(&p
, ">")) != NULL
) {
958 if (vstrsep(q
, "<", &user
, &pass
, &rights
) != 3) continue;
959 if (!user
|| !pass
) continue;
962 if (strncmp(rights
, "Private", 7) == 0)
964 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
968 sprintf(tmp
, "%s", nvram_storage_path("ftp_pubroot"));
970 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
971 user
, crypt(pass
, "$1$"), user
, tmp
);
974 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
975 if ((f
= fopen(tmp
, "w")))
978 if (nvram_invmatch("ftp_dirlist", "1"))
979 strcat(tmp
, "dirlist_enable=yes\n");
980 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
981 strcat(tmp
, "download_enable=yes\n");
982 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
983 strcat(tmp
, "write_enable=yes\n");
1001 killall("vsftpd", SIGTERM
);
1002 unlink(vsftpd_passwd
);
1003 unlink(vsftpd_conf
);
1004 eval("rm", "-rf", vsftpd_users
);
1008 // -----------------------------------------------------------------------------
1012 #ifdef TCONFIG_SAMBASRV
1013 void kill_samba(int sig
)
1015 killall("smbd", sig
);
1016 killall("nmbd", sig
);
1020 void start_samba(void)
1022 #ifdef TCONFIG_SAMBASRV
1030 mode
= nvram_get_int("smbd_enable");
1031 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1034 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
) {
1035 perror("/etc/smb.conf");
1039 fprintf(fp
, "[global]\n"
1040 " interfaces = %s\n"
1041 " bind interfaces only = yes\n"
1043 " server string = %s\n"
1044 " guest account = nobody\n"
1046 " browseable = yes\n"
1048 " guest only = no\n"
1050 " syslog only = yes\n"
1052 " encrypt passwords = yes\n"
1053 " local master = %s\n"
1054 " preserve case = yes\n"
1055 " short preserve case = yes\n",
1056 nvram_get("lan_ifname") ? : "br0",
1057 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1058 nvram_get("router_name") ? : "Tomato",
1059 mode
== 2 ? "user" : "share",
1060 nvram_get_int("smbd_loglevel"),
1061 nvram_get_int("smbd_master") ? "yes" : "no"
1064 if (nvram_invmatch("smbd_cpage", "")) {
1065 char *cp
= nvram_get("smbd_cpage");
1067 fprintf(fp
, " client code page = %s\n", cp
);
1068 sprintf(nlsmod
, "nls_cp%s", cp
);
1070 cp
= nvram_get("smbd_nlsmod");
1071 if ((cp
) && (*cp
!= 0) && (strcmp(cp
, nlsmod
) != 0))
1075 nvram_set("smbd_nlsmod", nlsmod
);
1078 if (nvram_match("smbd_cset", "utf8"))
1079 fprintf(fp
, " coding system = utf8\n");
1080 else if (nvram_invmatch("smbd_cset", ""))
1081 fprintf(fp
, " character set = %s\n", nvram_get("smbd_cset"));
1083 fprintf(fp
, "%s\n\n", nvram_safe_get("smbd_custom"));
1085 /* configure shares */
1089 char *name
, *path
, *comment
, *writeable
, *hidden
;
1092 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1094 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1097 while ((q
= strsep(&p
, ">")) != NULL
) {
1098 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1099 if (!path
|| !name
) continue;
1102 fprintf(fp
, "\n[%s]\n", name
);
1105 fprintf(fp
, " path = %s\n", path
);
1108 if (!strcmp(writeable
, "1"))
1109 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1110 if (!strcmp(hidden
, "1"))
1111 fprintf(fp
, " browseable = no\n");
1115 fprintf(fp
, " comment = %s\n", comment
);
1122 /* share everything below MOUNT_ROOT */
1123 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1124 while ((dp
= readdir(dir
))) {
1125 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1127 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1128 fprintf(fp
, "\n[%s]\n path = %s/%s\n",
1129 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1130 if (nvram_match("smbd_autoshare", "3")) // Hidden
1131 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1132 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1133 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1134 fprintf(fp
, " writable = yes\n force user = %s\n", "root");
1140 if (dir
) closedir(dir
);
1143 /* by default share MOUNT_ROOT as read-only */
1144 fprintf(fp
, "\n[share]\n"
1152 mkdir_if_none("/var/run/samba");
1153 mkdir_if_none("/etc/samba");
1155 /* write smbpasswd */
1156 eval("smbpasswd", "-a", "nobody", "\"\"");
1159 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1161 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1165 int ret1
= 0, ret2
= 0;
1166 /* start samba if it's not already running */
1167 if (pidof("nmbd") <= 0)
1168 ret1
= eval("nmbd", "-D");
1169 if (pidof("smbd") <= 0)
1170 ret2
= eval("smbd", "-D");
1172 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1176 void stop_samba(void)
1178 #ifdef TCONFIG_SAMBASRV
1179 kill_samba(SIGTERM
);
1180 sleep(2); /* wait for smbd to finish */
1182 if (nvram_invmatch("smbd_nlsmod", "")) {
1183 modprobe_r(nvram_get("smbd_nlsmod"));
1184 nvram_set("smbd_nlsmod", "");
1188 unlink("/var/log/smb");
1189 unlink("/var/log/nmb");
1190 eval("rm", "-rf", "/var/run/samba");
1194 void restart_nas_services(int start
)
1196 /* restart all NAS applications */
1197 #ifdef TCONFIG_SAMBASRV
1198 if (start
&& nvram_get_int("smbd_enable"))
1201 kill_samba(SIGTERM
);
1204 if (start
&& nvram_get_int("ftp_enable"))
1207 killall("vsftpd", SIGTERM
);
1211 // -----------------------------------------------------------------------------
1213 static void _check(pid_t
*pid
, const char *name
, void (*func
)(void) )
1216 if (kill(*pid
, 0) != 0) {
1217 if ((*pid
= pidof(name
)) == -1) func();
1222 void check_services(void)
1224 _check(&pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
1225 _check(&pid_crond
, "crond", start_cron
);
1226 _check(&pid_igmp
, "igmprt", start_igmp_proxy
);
1229 // -----------------------------------------------------------------------------
1231 void start_services(void)
1233 static int once
= 1;
1239 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1240 if (nvram_match("sshd_eas", "1")) start_sshd();
1256 #ifdef TCONFIG_SAMBA
1259 start_samba(); // !!TB - Samba
1260 start_ftpd(); // !!TB - FTP Server
1263 void stop_services(void)
1267 stop_ftpd(); // !!TB - FTP Server
1268 stop_samba(); // !!TB - Samba
1269 #ifdef TCONFIG_SAMBA
1287 // -----------------------------------------------------------------------------
1289 void exec_service(void)
1291 const int A_START
= 1;
1292 const int A_STOP
= 2;
1293 const int A_RESTART
= 1|2;
1301 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
1305 act
= strsep(&next
, ",");
1306 service
= strsep(&act
, "-");
1312 if (strcmp(act
, "start") == 0) action
= A_START
;
1313 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
1314 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
1317 _dprintf("%s %s service=%s action=%s\n", __FILE__
, __FUNCTION__
, service
, act
);
1320 if (strcmp(service
, "dhcpc") == 0) {
1321 if (action
& A_STOP
) stop_dhcpc();
1322 if (action
& A_START
) start_dhcpc();
1326 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
1327 if (action
& A_STOP
) stop_dnsmasq();
1328 if (action
& A_START
) {
1335 if (strcmp(service
, "firewall") == 0) {
1336 if (action
& A_STOP
) {
1340 if (action
& A_START
) {
1347 if (strcmp(service
, "restrict") == 0) {
1348 if (action
& A_STOP
) {
1351 if (action
& A_START
) {
1352 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
1356 // if radio was disabled by access restriction, but no rule is handling it now, enable it
1358 if (nvram_get_int("rrules_radio") < 0) {
1359 if (!get_radio()) eval("radio", "on");
1366 if (strcmp(service
, "qos") == 0) {
1367 if (action
& A_STOP
) {
1370 stop_firewall(); start_firewall(); // always restarted
1371 if (action
& A_START
) {
1373 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
1378 if (strcmp(service
, "upnp") == 0) {
1379 if (action
& A_STOP
) {
1382 stop_firewall(); start_firewall(); // always restarted
1383 if (action
& A_START
) {
1389 if (strcmp(service
, "telnetd") == 0) {
1390 if (action
& A_STOP
) stop_telnetd();
1391 if (action
& A_START
) start_telnetd();
1395 if (strcmp(service
, "sshd") == 0) {
1396 if (action
& A_STOP
) stop_sshd();
1397 if (action
& A_START
) start_sshd();
1401 if (strcmp(service
, "admin") == 0) {
1402 if (action
& A_STOP
) {
1407 stop_firewall(); start_firewall(); // always restarted
1408 if (action
& A_START
) {
1411 if (nvram_match("telnetd_eas", "1")) start_telnetd();
1412 if (nvram_match("sshd_eas", "1")) start_sshd();
1417 if (strcmp(service
, "ddns") == 0) {
1418 if (action
& A_STOP
) stop_ddns();
1419 if (action
& A_START
) start_ddns();
1423 if (strcmp(service
, "ntpc") == 0) {
1424 if (action
& A_STOP
) stop_ntpc();
1425 if (action
& A_START
) start_ntpc();
1429 if (strcmp(service
, "logging") == 0) {
1430 if (action
& A_STOP
) {
1434 stop_firewall(); start_firewall(); // always restarted
1435 if (action
& A_START
) {
1442 if (strcmp(service
, "crond") == 0) {
1443 if (action
& A_STOP
) {
1446 if (action
& A_START
) {
1452 if (strcmp(service
, "upgrade") == 0) {
1453 if (action
& A_START
) {
1458 stop_ftpd(); // !!TB - FTP Server
1459 stop_samba(); // !!TB - Samba
1467 killall("rstats", SIGTERM
);
1468 killall("buttons", SIGTERM
);
1470 remove_storage_main(); // !!TB - USB Support
1471 stop_usb(); // !!TB - USB Support
1477 if (strcmp(service
, "cifs") == 0) {
1478 if (action
& A_STOP
) stop_cifs();
1479 if (action
& A_START
) start_cifs();
1484 #ifdef TCONFIG_JFFS2
1485 if (strcmp(service
, "jffs2") == 0) {
1486 if (action
& A_STOP
) stop_jffs2();
1487 if (action
& A_START
) start_jffs2();
1492 if (strcmp(service
, "routing") == 0) {
1493 if (action
& A_STOP
) {
1495 do_static_routes(0); // remove old '_saved'
1496 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1500 if (action
& A_START
) {
1501 do_static_routes(1); // add new
1503 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
1508 if (strcmp(service
, "ctnf") == 0) {
1509 if (action
& A_START
) {
1517 if (strcmp(service
, "wan") == 0) {
1518 if (action
& A_STOP
) {
1519 if (get_wan_proto() == WP_PPPOE
) {
1522 stop_singe_pppoe(PPPOE0
);
1523 if (((action
& A_START
) == 0) && (nvram_match("ppp_demand", "1"))) {
1525 start_pppoe(PPPOE0
);
1534 if (action
& A_START
) {
1535 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
1537 if (get_wan_proto() == WP_PPPOE
) {
1538 stop_singe_pppoe(PPPOE0
);
1539 start_pppoe(PPPOE0
);
1540 if (nvram_invmatch("ppp_demand", "1")) {
1553 if (strcmp(service
, "net") == 0) {
1554 if (action
& A_STOP
) {
1559 if (action
& A_START
) {
1567 if (strcmp(service
, "rstats") == 0) {
1568 if (action
& A_STOP
) stop_rstats();
1569 if (action
& A_START
) start_rstats(0);
1573 if (strcmp(service
, "rstatsnew") == 0) {
1574 if (action
& A_STOP
) stop_rstats();
1575 if (action
& A_START
) start_rstats(1);
1579 if (strcmp(service
, "sched") == 0) {
1580 if (action
& A_STOP
) stop_sched();
1581 if (action
& A_START
) start_sched();
1585 // !!TB - USB Support
1586 if (strcmp(service
, "usb") == 0) {
1587 if (action
& A_STOP
) stop_usb();
1588 if (action
& A_START
) {
1590 // restart Samba and ftp since they may be killed by stop_usb()
1591 restart_nas_services(1);
1597 // !!TB - FTP Server
1598 if (strcmp(service
, "ftpd") == 0) {
1599 if (action
& A_STOP
) stop_ftpd();
1603 if (action
& A_START
) start_ftpd();
1608 #ifdef TCONFIG_SAMBASRV
1610 if (strcmp(service
, "samba") == 0) {
1611 if (action
& A_STOP
) stop_samba();
1612 if (action
& A_START
) {
1621 if (strcmp(service
, "smbd") == 0) {
1622 if (action
& A_STOP
) stop_smbd();
1623 if (action
& A_START
) start_smbd();
1627 if (strcmp(service
, "test1") == 0) {
1628 if (action
& A_STOP
) stop_test_1();
1629 if (action
& A_START
) start_test_1();
1638 // some functions check action_service and must be cleared at end -- zzz
1639 nvram_set("action_service", "");
1642 static void do_service(const char *name
, const char *action
, int user
)
1648 while (!nvram_match("action_service", "")) {
1653 else if (--n
< 0) break;
1657 snprintf(s
, sizeof(s
), "%s-%s", name
, action
);
1658 nvram_set("action_service", s
);
1662 while (nvram_match("action_service", s
)) {
1667 else if (--n
< 0) break;
1672 int service_main(int argc
, char *argv
[])
1674 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
1675 do_service(argv
[1], argv
[2], 1);
1676 printf("\nDone.\n");
1680 void start_service(const char *name
)
1682 do_service(name
, "start", 0);
1685 void stop_service(const char *name
)
1687 do_service(name
, "stop", 0);