static void ez_action_wan_down(void) { if (get_ap_mode()) return; logmessage("watchdog", "Perform ez-button %s...", "WAN disconnect"); stop_wan(); }
static void ez_action_wan_reconnect(void) { if (get_ap_mode()) return; logmessage("watchdog", "Perform ez-button %s...", "WAN reconnect"); full_restart_wan(); }
void reload_vpn_server(void) { int i_type; if (nvram_invmatch("vpns_enable", "1") || get_ap_mode()) return; i_type = nvram_get_int("vpns_type"); #if defined(APP_OPENVPN) if (i_type != 2) #endif create_vpns_pppd_options(i_type); }
static void watchdog_on_timer(void) { int is_ap_mode; /* if timer is set to less than 1 sec, then check buttons only */ if (wd_itv.it_value.tv_sec == 0) { int i_ret = 0; /* handle buttons */ #if defined (BOARD_GPIO_BTN_RESET) i_ret |= btn_check_reset(); #endif #if defined (BOARD_GPIO_BTN_WPS) i_ret |= btn_check_wps(); #endif #if defined (BOARD_GPIO_BTN_WLTOG) i_ret |= btn_check_wlt(); #endif if (i_ret) { if (wd_itv.it_value.tv_usec != WD_URGENT_PERIOD) wd_alarmtimer(0, WD_URGENT_PERIOD); } else { wd_alarmtimer(WD_NORMAL_PERIOD, 0); } return; } is_ap_mode = get_ap_mode(); /* check for time-dependent services */ svc_timecheck(); /* http server check */ httpd_process_check(); /* DNS/DHCP server check */ if (!is_ap_mode) dnsmasq_process_check(); inet_handler(is_ap_mode); /* update kernel timezone daylight */ setkernel_tz(); storage_save_time(10); }
void shutdown_router(int use_reboot) { int is_ap_mode = get_ap_mode(); stop_misc(); stop_services(1); #if (BOARD_NUM_USB_PORTS > 0) stop_usb_printer_spoolers(); safe_remove_usb_device(0, NULL, !use_reboot); #endif #if defined (BOARD_GPIO_LED_USB) LED_CONTROL(BOARD_GPIO_LED_USB, LED_OFF); #endif stop_wan(); stop_services_lan_wan(); set_ipv4_forward(0); #if defined (BOARD_GPIO_LED_WAN) LED_CONTROL(BOARD_GPIO_LED_WAN, LED_OFF); #endif storage_save_time(10); write_storage_to_mtd(); stop_8021x_all(); stop_wifi_all_wl(); stop_wifi_all_rt(); stop_logger(); stop_lan(is_ap_mode); umount_rwfs_partition(); #if defined (BOARD_GPIO_LED_LAN) LED_CONTROL(BOARD_GPIO_LED_LAN, LED_OFF); #endif #if defined (BOARD_GPIO_LED_POWER) LED_CONTROL(BOARD_GPIO_LED_POWER, LED_OFF); #endif if (!use_reboot) module_smart_unload("rt_timer_wdg", 0); }
static void ez_action_wan_toggle(void) { if (get_ap_mode()) return; if (is_interface_up(get_man_ifname(0))) { logmessage("watchdog", "Perform ez-button %s...", "WAN disconnect"); stop_wan(); } else { logmessage("watchdog", "Perform ez-button %s...", "WAN reconnect"); full_restart_wan(); } }
static void load_usb_modules(void) { /* load usb printer module before storage */ doSystem("modprobe %s", "usblp"); /* load usb storage module */ doSystem("modprobe %s", "usb-storage"); /* load usb modem modules */ if (!get_ap_mode() && nvram_get_int("modem_rule") > 0) reload_modem_modules(nvram_get_int("modem_type"), 0); /* start usb host */ #if defined (USE_USB3) doSystem("modprobe %s %s=%d", "xhci-hcd", "usb3_disable", nvram_get_int("usb3_disable")); #else doSystem("modprobe %s", "ehci-hcd"); doSystem("modprobe %s", "ohci-hcd"); #endif }
static void try_start_usb_modem_to_wan(void) { int modem_prio, has_link; if (get_ap_mode()) return; /* check modem prio mode */ modem_prio = nvram_get_int("modem_prio"); if (modem_prio < 1) return; /* check modem already selected to WAN */ if (get_usb_modem_wan(0)) return; /* check modem enabled and ready */ if (!get_modem_devnum()) return; if (modem_prio == 2) { if (get_apcli_wisp_ifname()) return; has_link = get_wan_ether_link_direct(0); if (has_link < 0) has_link = 0; if (has_link) return; } logmessage("USB hotplug", "try start USB Modem as WAN connection..."); try_wan_reconnect(1, 0); }
void handle_notifications(void) { int i, stop_handle = 0; char notify_name[256]; DIR *directory = opendir(DIR_RC_NOTIFY); if (!directory) return; // handle max 10 requests at once (prevent deadlock) for (i=0; i < 10; i++) { struct dirent *entry; FILE *test_fp; entry = readdir(directory); if (!entry) break; if (strcmp(entry->d_name, ".") == 0) continue; if (strcmp(entry->d_name, "..") == 0) continue; /* Remove the marker file. */ snprintf(notify_name, sizeof(notify_name), "%s/%s", DIR_RC_NOTIFY, entry->d_name); remove(notify_name); printf("rc notification: %s\n", entry->d_name); /* Take the appropriate action. */ if (!strcmp(entry->d_name, RCN_RESTART_REBOOT)) { stop_handle = 1; sys_exit(); } else if (!strcmp(entry->d_name, "flash_firmware")) { stop_handle = 1; flash_firmware(); } #if defined (USE_IPV6) else if (!strcmp(entry->d_name, RCN_RESTART_IPV6)) { if (!get_ap_mode()) { full_restart_ipv6(nvram_ipv6_type); nvram_ipv6_type = get_ipv6_type(); } } else if (strcmp(entry->d_name, RCN_RESTART_RADVD) == 0) { restart_dhcpd(); restart_radvd(); } #endif else if (!strcmp(entry->d_name, RCN_RESTART_WAN)) { full_restart_wan(); } else if (!strcmp(entry->d_name, RCN_RESTART_LAN)) { full_restart_lan(); } else if (!strcmp(entry->d_name, "stop_whole_wan")) { stop_wan(); } else if (!strcmp(entry->d_name, RCN_RESTART_IPTV)) { int is_ap_mode = get_ap_mode(); restart_iptv(is_ap_mode); if (!is_ap_mode) restart_firewall(); } else if(!strcmp(entry->d_name, "deferred_wan_connect")) { deferred_wan_connect(); } else if(!strcmp(entry->d_name, "auto_wan_reconnect")) { auto_wan_reconnect(); } else if(!strcmp(entry->d_name, "auto_wan_reconnect_pause")) { auto_wan_reconnect_pause(); } else if(!strcmp(entry->d_name, "manual_wan_reconnect")) { manual_wan_reconnect(); } else if(!strcmp(entry->d_name, "manual_wan_disconnect")) { manual_wan_disconnect(); } else if(!strcmp(entry->d_name, "manual_ddns_hostname_check")) { manual_ddns_hostname_check(); } #if (BOARD_NUM_USB_PORTS > 0) else if (!strcmp(entry->d_name, RCN_RESTART_MODEM)) { int wan_stopped = 0; int modules_reloaded = 0; int need_restart_wan = get_usb_modem_wan(0); int modem_rule = nvram_get_int("modem_rule"); int modem_type = nvram_get_int("modem_type"); if (nvram_modem_rule != modem_rule) { nvram_modem_rule = modem_rule; if (need_restart_wan) { wan_stopped = 1; stop_wan(); } if (modem_rule > 0) { modules_reloaded = 1; reload_modem_modules(modem_type, 1); } else { unload_modem_modules(); } } if (nvram_modem_type != modem_type) { if (nvram_modem_type == 3 || modem_type == 3) { if (modem_rule > 0 && !modules_reloaded) { if (need_restart_wan && !wan_stopped) stop_wan(); reload_modem_modules(modem_type, 1); } } nvram_modem_type = modem_type; } if (need_restart_wan) full_restart_wan(); } else if (strcmp(entry->d_name, RCN_RESTART_SPOOLER) == 0) { restart_usb_printer_spoolers(); } else if (strcmp(entry->d_name, RCN_RESTART_HDDTUNE) == 0) { system("/sbin/hddtune.sh"); set_pagecache_reclaim(); } #if defined(APP_FTPD) else if (strcmp(entry->d_name, RCN_RESTART_FTPD) == 0) { restart_ftpd(); } #endif #if defined(APP_SMBD) else if (strcmp(entry->d_name, RCN_RESTART_SMBD) == 0) { restart_smbd(); } #endif #if defined(APP_NFSD) else if (strcmp(entry->d_name, RCN_RESTART_NFSD) == 0) { restart_nfsd(); } #endif #if defined(APP_MINIDLNA) else if (strcmp(entry->d_name, "restart_dms_rescan") == 0) { restart_dms(1); } else if (strcmp(entry->d_name, RCN_RESTART_DMS) == 0) { restart_dms(0); } #endif #if defined(APP_FIREFLY) else if (strcmp(entry->d_name, RCN_RESTART_ITUNES) == 0) { restart_itunes(); } #endif #if defined(APP_TRMD) else if (strcmp(entry->d_name, RCN_RESTART_TRMD) == 0) { restart_torrent(); } #endif #if defined(APP_ARIA) else if (strcmp(entry->d_name, RCN_RESTART_ARIA) == 0) { restart_aria(); } #endif else if (!strcmp(entry->d_name, "on_hotplug_usb_storage")) { // deferred run usb apps nvram_set_int_temp("usb_hotplug_ms", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_storage")) { umount_ejected(); } else if (!strcmp(entry->d_name, "on_hotplug_usb_printer")) { // deferred run usb printer daemons nvram_set_int_temp("usb_hotplug_lp", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_printer")) { // deferred stop usb printer daemons nvram_set_int_temp("usb_unplug_lp", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_hotplug_usb_modem")) { // deferred run usb modem to wan nvram_set_int_temp("usb_hotplug_md", 1); alarm(5); } else if (!strcmp(entry->d_name, "on_unplug_usb_modem")) { // deferred restart wan nvram_set_int_temp("usb_unplug_md", 1); alarm(5); } #endif else if (strcmp(entry->d_name, RCN_RESTART_HTTPD) == 0) { restart_httpd(); } else if (strcmp(entry->d_name, RCN_RESTART_TELNETD) == 0) { stop_telnetd(); start_telnetd(); } #if defined(APP_SSHD) else if (strcmp(entry->d_name, RCN_RESTART_SSHD) == 0) { restart_sshd(); } #endif #if defined(APP_SMBD) || defined(APP_NMBD) else if (strcmp(entry->d_name, RCN_RESTART_NMBD) == 0) { restart_nmbd(); } else if (strcmp(entry->d_name, RCN_RESTART_WINS) == 0) { restart_nmbd(); restart_dhcpd(); reload_vpn_server(); } #endif else if (strcmp(entry->d_name, RCN_RESTART_LLTD) == 0) { restart_lltd(); } else if (strcmp(entry->d_name, RCN_RESTART_ADSC) == 0) { restart_infosvr(); } else if (strcmp(entry->d_name, RCN_RESTART_VPNSVR) == 0) { restart_vpn_server(); } else if (strcmp(entry->d_name, RCN_RESTART_VPNCLI) == 0) { restart_vpn_client(); } else if (strcmp(entry->d_name, "start_vpn_client") == 0) { start_vpn_client(); } else if (strcmp(entry->d_name, "stop_vpn_client") == 0) { stop_vpn_client(); } else if (strcmp(entry->d_name, RCN_RESTART_DDNS) == 0) { stop_ddns(); start_ddns(1); } else if (strcmp(entry->d_name, RCN_RESTART_DI) == 0) { if (get_ap_mode() || has_wan_ip4(0)) notify_run_detect_internet(2); } else if (strcmp(entry->d_name, RCN_RESTART_DHCPD) == 0) { if (get_ap_mode()) update_hosts_ap(); restart_dhcpd(); } else if (strcmp(entry->d_name, RCN_RESTART_UPNP) == 0) { restart_upnp(); } else if (strcmp(entry->d_name, RCN_RESTART_SWITCH_CFG) == 0) { config_bridge(get_ap_mode()); switch_config_base(); switch_config_storm(); switch_config_link(); } else if (strcmp(entry->d_name, RCN_RESTART_SWITCH_VLAN) == 0) { notify_reset_detect_link(); switch_config_vlan(0); } else if (strcmp(entry->d_name, RCN_RESTART_SYSLOG) == 0) { stop_logger(); start_logger(0); } else if (strcmp(entry->d_name, RCN_RESTART_WDG) == 0) { restart_watchdog_cpu(); } else if (strcmp(entry->d_name, RCN_RESTART_TWEAKS) == 0) { notify_leds_detect_link(); } else if (strcmp(entry->d_name, "restart_firewall_wan") == 0) { restart_firewall(); } else if (strcmp(entry->d_name, RCN_RESTART_FIREWALL) == 0) { reload_nat_modules(); restart_firewall(); } else if (strcmp(entry->d_name, RCN_RESTART_NTPC) == 0) { notify_watchdog_time(); } else if (strcmp(entry->d_name, RCN_RESTART_TIME) == 0) { stop_logger(); set_timezone(); notify_watchdog_time(); notify_rstats_time(); start_logger(0); } else if (strcmp(entry->d_name, RCN_RESTART_SYSCTL) == 0) { int nf_nat_type = nvram_get_int("nf_nat_type"); restart_all_sysctl(); /* flush conntrack after NAT model changing */ if (nvram_nf_nat_type != nf_nat_type) { nvram_nf_nat_type = nf_nat_type; flush_conntrack_table(NULL); } } else if (!strcmp(entry->d_name, RCN_RESTART_WIFI5)) { int radio_on = get_enabled_radio_wl(); if (radio_on) radio_on = is_radio_allowed_wl(); restart_wifi_wl(radio_on, 1); } else if (!strcmp(entry->d_name, RCN_RESTART_WIFI2)) { int radio_on = get_enabled_radio_rt(); if (radio_on) radio_on = is_radio_allowed_rt(); restart_wifi_rt(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl")) { int guest_on = is_guest_allowed_wl(); control_guest_wl(guest_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt")) { int guest_on = is_guest_allowed_rt(); control_guest_rt(guest_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl_on")) { control_guest_wl(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_wl_off")) { control_guest_wl(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt_on")) { control_guest_rt(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_guest_rt_off")) { control_guest_rt(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl")) { int radio_on = get_enabled_radio_wl(); if (radio_on) radio_on = is_radio_allowed_wl(); control_radio_wl(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt")) { int radio_on = get_enabled_radio_rt(); if (radio_on) radio_on = is_radio_allowed_rt(); control_radio_rt(radio_on, 1); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl_on")) { control_radio_wl(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_wl_off")) { control_radio_wl(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt_on")) { control_radio_rt(1, 0); } else if (!strcmp(entry->d_name, "control_wifi_radio_rt_off")) { control_radio_rt(0, 0); } else if (!strcmp(entry->d_name, "control_wifi_config_wl")) { gen_ralink_config_5g(0); } else if (!strcmp(entry->d_name, "control_wifi_config_rt")) { gen_ralink_config_2g(0); } else { dbg("WARNING: rc notified of unrecognized event `%s'.\n", entry->d_name); } /* * If there hasn't been another request for the same event made since * we started, we can safely remove the ``action incomplete'' marker. * Otherwise, we leave the marker because we'll go through here again * for this even and mark it complete only after we've completed it * without getting another request for the same event while handling * it. */ test_fp = fopen(notify_name, "r"); if (test_fp != NULL) { fclose(test_fp); } else { /* Remove the marker file. */ snprintf(notify_name, sizeof(notify_name), "%s/%s", DIR_RC_INCOMPLETE, entry->d_name); remove(notify_name); } if (stop_handle) break; } closedir(directory); }
void init_router(void) { int log_remote, is_ap_mode, nvram_need_commit; #if defined (USE_RTL8367) rtl8367_node(); #endif #if defined (USE_MTK_ESW) || defined (USE_MTK_GSW) mtk_esw_node(); #endif nvram_convert_old_params(); nvram_need_commit = nvram_restore_defaults(); get_eeprom_params(); nvram_convert_misc_values(); if (nvram_need_commit) nvram_commit(); init_gpio_leds_buttons(); gen_ralink_config_2g(0); gen_ralink_config_5g(0); load_wireless_modules(); #if (BOARD_NUM_USB_PORTS > 0) load_usb_modules(); #endif recreate_passwd_unix(1); set_timezone(); set_pagecache_reclaim(); storage_load_time(); is_ap_mode = get_ap_mode(); log_remote = nvram_invmatch("log_ipaddr", ""); if (!log_remote) start_logger(1); init_loopback(); init_bridge(is_ap_mode); #if defined (USE_IPV6) init_ipv6(); #endif start_detect_link(); start_detect_internet(0); start_lan(is_ap_mode, 0); if (log_remote) start_logger(1); start_dns_dhcpd(is_ap_mode); #if defined(APP_SMBD) || defined(APP_NMBD) start_wins(); #endif if (!is_ap_mode) { ipt_nat_default(); ipt_filter_default(); #if defined (USE_IPV6) ip6t_filter_default(); #endif start_wan(); } start_services_once(is_ap_mode); notify_leds_detect_link(); // system ready system("/etc/storage/started_script.sh &"); }
int start_vpn_client(void) { FILE *fp; int i_type, i_mppe, i_auth; char *vpnc_peer, *vpnc_opt, tmp[256]; if (nvram_invmatch("vpnc_enable", "1") || get_ap_mode()) return 1; vpnc_peer = nvram_safe_get("vpnc_peer"); if (strlen(vpnc_peer) < 1) { logmessage(VPNC_LOG_NAME, "Unable to start - remote server host is not defined!"); return 1; } nvram_set_temp("vpnc_dns_t", ""); nvram_set_temp("vpnc_dom_t", ""); nvram_set_int_temp("vpnc_state_t", 0); i_type = nvram_get_int("vpnc_type"); #if defined(APP_OPENVPN) if (i_type == 2) return start_openvpn_client(); #endif vpnc_opt = VPN_CLIENT_PPPD_OPTIONS; mkdir("/tmp/ppp", 0777); symlink("/sbin/rc", VPNC_PPP_UP_SCRIPT); symlink("/sbin/rc", VPNC_PPP_DW_SCRIPT); i_auth = nvram_get_int("vpnc_auth"); i_mppe = nvram_get_int("vpnc_mppe"); // Create options for pppd if (!(fp = fopen(vpnc_opt, "w"))) { return -1; } fprintf(fp, "noauth\n"); fprintf(fp, "user '%s'\n", safe_pppd_line(nvram_safe_get("vpnc_user"), tmp, sizeof(tmp))); fprintf(fp, "password '%s'\n", safe_pppd_line(nvram_safe_get("vpnc_pass"), tmp, sizeof(tmp))); fprintf(fp, "refuse-eap\n"); if (i_auth == 1) { /* MS-CHAPv2 */ fprintf(fp, "refuse-pap\n"); fprintf(fp, "refuse-chap\n"); fprintf(fp, "refuse-mschap\n"); } else if (i_auth == 2) { /* CHAP */ fprintf(fp, "refuse-pap\n"); fprintf(fp, "refuse-mschap\n"); fprintf(fp, "refuse-mschap-v2\n"); } else if (i_auth == 3) { /* PAP */ fprintf(fp, "refuse-chap\n"); fprintf(fp, "refuse-mschap\n"); fprintf(fp, "refuse-mschap-v2\n"); } if (i_type != 1) { fprintf(fp, "plugin pptp.so\n"); fprintf(fp, "pptp_server '%s'\n", vpnc_peer); fprintf(fp, "route_rdgw %d\n", (nvram_match("vpnc_dgw", "1")) ? 2 : 0); fprintf(fp, "persist\n"); fprintf(fp, "linkname %s\n", VPNC_PPP_LINK_NAME); } fprintf(fp, "mtu %d\n", nvram_safe_get_int("vpnc_mtu", 1450, 1000, 1460)); fprintf(fp, "mru %d\n", nvram_safe_get_int("vpnc_mru", 1450, 1000, 1460)); fprintf(fp, "maxfail %d\n", 0); // pppd re-call count (0=infinite) fprintf(fp, "holdoff %d\n", 10); // pppd re-call time (10s) fprintf(fp, "ipcp-accept-remote ipcp-accept-local\n"); fprintf(fp, "noipdefault\n"); fprintf(fp, "usepeerdns\n"); fprintf(fp, "default-asyncmap\n"); /* looks like pptp also likes them */ fprintf(fp, "nopcomp noaccomp\n"); /* ccp should still be enabled - mppe/mppc requires this */ fprintf(fp, "novj nobsdcomp nodeflate\n"); if (i_mppe == 3) { fprintf(fp, "nomppe nomppc\n"); } else { if (i_mppe == 1) { fprintf(fp, "+mppe\n"); fprintf(fp, "-mppe-40\n"); fprintf(fp, "+mppe-128\n"); } else if (i_mppe == 2) { fprintf(fp, "+mppe\n"); fprintf(fp, "+mppe-40\n"); fprintf(fp, "-mppe-128\n"); } else { fprintf(fp, "+mppe-40\n"); fprintf(fp, "+mppe-128\n"); } fprintf(fp, "nomppe-stateful\n"); } if (i_type == 1) { // Don't wait for LCP term responses; exit immediately when killed fprintf(fp, "lcp-max-terminate %d\n", 0); } /* echo failures (6*20s) */ fprintf(fp, "lcp-echo-interval %d\n", 20); fprintf(fp, "lcp-echo-failure %d\n", 6); fprintf(fp, "lcp-echo-adaptive\n"); fprintf(fp, "ip-up-script %s\n", VPNC_PPP_UP_SCRIPT); fprintf(fp, "ip-down-script %s\n", VPNC_PPP_DW_SCRIPT); fprintf(fp, "minunit %d\n", VPNC_PPP_UNIT); fprintf(fp, "ktune\n"); /* user specific options */ fprintf(fp, "%s\n", nvram_safe_get("vpnc_pppd")); fclose(fp); chmod(vpnc_opt, 0600); if (i_type == 1) { nvram_set_int_temp("l2tp_cli_t", 1); if (safe_start_xl2tpd() != 0) control_xl2tpd("c", "VPNC"); } else { nvram_set_int_temp("l2tp_cli_t", 0); return eval("/usr/sbin/pppd", "file", vpnc_opt); } return 0; }
static void write_nfsd_exports(void) { FILE *procpt, *fp; char line[256], devname[32], mpname[128], system_type[16], mount_mode[164], acl_lan[32], acl_vpn[32]; const char *exports_link = "/etc/storage/exports"; const char *exports_file = "/etc/exports"; const char *exports_rule = "async,insecure,no_root_squash,no_subtree_check"; char *nfsmm, *acl_addr, *acl_mask; unlink(exports_file); if (check_if_file_exist(exports_link)) { symlink(exports_link, exports_file); return; } fp = fopen(exports_file, "w"); if (!fp) return; acl_addr = nvram_safe_get("lan_ipaddr_t"); acl_mask = nvram_safe_get("lan_netmask_t"); if (!is_valid_ipv4(acl_addr) || !is_valid_ipv4(acl_mask)) { acl_addr = nvram_safe_get("lan_ipaddr"); acl_mask = nvram_safe_get("lan_netmask"); } acl_lan[0] = 0; ip2class(acl_addr, acl_mask, acl_lan, sizeof(acl_lan)); acl_vpn[0] = 0; if (!get_ap_mode() && nvram_get_int("vpns_enable") && nvram_get_int("vpns_vuse")) { acl_addr = nvram_safe_get("vpns_vnet"); acl_mask = VPN_SERVER_SUBNET_MASK; #if defined (APP_OPENVPN) if (nvram_get_int("vpns_type") == 2) { if (nvram_get_int("vpns_ov_mode") == 1) ip2class(acl_addr, acl_mask, acl_vpn, sizeof(acl_vpn)); } else #endif ip2class(acl_addr, acl_mask, acl_vpn, sizeof(acl_vpn)); if (strcmp(acl_lan, acl_vpn) == 0) acl_vpn[0] = 0; } fprintf(fp, "# %s\n\n", "auto-created file"); procpt = fopen("/proc/mounts", "r"); if (procpt) { while (fgets(line, sizeof(line), procpt)) { if (sscanf(line, "%31s %127s %15s %163s %*s %*s", devname, mpname, system_type, mount_mode) != 4) continue; if (!strcmp(system_type, "fuseblk")) continue; if (!strncmp(devname, "/dev/sd", 7) && !strncmp(mpname, "/media/", 7)) { nfsmm = (strncmp(mount_mode, "ro", 2) == 0) ? "ro" : "rw"; fprintf(fp, "%s\t", mpname); fprintf(fp, " %s(%s,%s)", acl_lan, nfsmm, exports_rule); if (acl_vpn[0]) fprintf(fp, " %s(%s,%s)", acl_vpn, nfsmm, exports_rule); fprintf(fp, "\n"); } } fclose(procpt); } fclose(fp); }
int start_vpn_server(void) { FILE *fp; int i, i_type, i_vuse, i_cli0, i_cli1; char *vpns_cfg, *vpns_sec, *lanip; struct in_addr pool_in; unsigned int laddr, lmask, lsnet; if (nvram_invmatch("vpns_enable", "1") || get_ap_mode()) return 0; unlink(VPN_SERVER_LEASE_FILE); i_type = nvram_get_int("vpns_type"); #if defined(APP_OPENVPN) if (i_type == 2) return start_openvpn_server(); #endif vpns_cfg = "/etc/pptpd.conf"; vpns_sec = "/tmp/ppp/chap-secrets"; mkdir("/tmp/ppp", 0777); symlink("/sbin/rc", VPNS_PPP_UP_SCRIPT); symlink("/sbin/rc", VPNS_PPP_DW_SCRIPT); i_vuse = nvram_get_int("vpns_vuse"); lanip = nvram_safe_get("lan_ipaddr"); if (i_vuse == 0) { laddr = ntohl(inet_addr(lanip)); lmask = ntohl(inet_addr(nvram_safe_get("lan_netmask"))); lsnet = (~lmask) - 1; i_cli0 = nvram_safe_get_int("vpns_cli0", 245, 1, 254); i_cli1 = nvram_safe_get_int("vpns_cli1", 254, 2, 254); if (i_cli0 >= (int)lsnet) i_cli0 = (int)(lsnet - 1); if (i_cli1 > (int)lsnet) i_cli1 = (int)lsnet; if (i_cli1 <= i_cli0) i_cli1 = i_cli0 + 1; laddr = (laddr & lmask) | (unsigned int)i_cli0; i_cli0 += 1; } else { laddr = ntohl(inet_addr(nvram_safe_get("vpns_vnet"))); lmask = ntohl(inet_addr(VPN_SERVER_SUBNET_MASK)); laddr = (laddr & lmask) | 1; i_cli0 = 2; i_cli1 = i_cli0 + MAX_CLIENTS_NUM - 1; } if (i_type != 1) { if (!(fp = fopen(vpns_cfg, "w"))) return -1; fprintf(fp, "option %s\n", VPN_SERVER_PPPD_OPTIONS); fprintf(fp, "connections %d\n", MAX_CLIENTS_NUM); pool_in.s_addr = htonl(laddr); fprintf(fp, "localip %s\n", inet_ntoa(pool_in)); pool_in.s_addr = htonl((laddr & lmask) | (unsigned int)i_cli0); fprintf(fp, "remoteip %s-%d\n", inet_ntoa(pool_in), i_cli1); fclose(fp); chmod(vpns_cfg, 0644); } create_vpns_pppd_options(i_type); /* create /tmp/ppp/chap-secrets */ fp = fopen(vpns_sec, "w+"); if (fp) { char *acl_user, *acl_pass; char acl_user_var[32], acl_pass_var[32], acl_addr_var[32]; int i_cli2; int i_max = nvram_get_int("vpns_num_x"); if (i_max > MAX_CLIENTS_NUM) i_max = MAX_CLIENTS_NUM; for (i = 0; i < i_max; i++) { sprintf(acl_user_var, "vpns_user_x%d", i); sprintf(acl_pass_var, "vpns_pass_x%d", i); acl_user = nvram_safe_get(acl_user_var); acl_pass = nvram_safe_get(acl_pass_var); if (*acl_user && *acl_pass) { sprintf(acl_addr_var, "vpns_addr_x%d", i); i_cli2 = nvram_get_int(acl_addr_var); if (i_cli2 >= i_cli0 && i_cli2 <= i_cli1 ) { pool_in.s_addr = htonl((laddr & lmask) | (unsigned int)i_cli2); strcpy(acl_addr_var, inet_ntoa(pool_in)); } else strcpy(acl_addr_var, "*"); fprintf(fp, "\"%s\" * \"%s\" %s\n", acl_user, acl_pass, acl_addr_var); } } fclose(fp); chmod(vpns_sec, 0600); } if (i_type == 1) { nvram_set_int_temp("l2tp_srv_t", 1); safe_start_xl2tpd(); } else { nvram_set_int_temp("l2tp_srv_t", 0); /* execute pptpd daemon */ return eval("/usr/sbin/pptpd", "-c", vpns_cfg); } return 0; }