4 Copyright (C) 2006-2010 Jonathan Zarate
10 #include <sys/sysinfo.h>
12 #include <arpa/inet.h>
16 // #define DEBUG_NOEXECSERVICE
17 // #define DEBUG_NVRAMSET(k, v) cprintf("nvram set %s=%s\n", k, v);
18 #define DEBUG_NVRAMSET(k, v) do { } while(0);
21 char *post_buf
= NULL
;
25 static void asp_css(int argc
, char **argv
);
26 static void asp_resmsg(int argc
, char **argv
);
29 static void wo_tomato(char *url
);
30 static void wo_update(char *url
);
31 static void wo_service(char *url
);
32 static void wo_shutdown(char *url
);
33 static void wo_nvcommit(char *url
);
34 // static void wo_logout(char *url);
37 // ----------------------------------------------------------------------------
40 void exec_service(const char *action
)
44 _dprintf("exec_service: %s\n", action
);
47 while ((!nvram_match("action_service", "")) && (i
-- > 0)) {
48 _dprintf("%s: waiting before %d\n", __FUNCTION__
, i
);
52 nvram_set("action_service", action
);
56 while ((nvram_match("action_service", (char *)action
)) && (i
-- > 0)) {
57 _dprintf("%s: waiting after %d\n", __FUNCTION__
, i
);
62 if (atoi(webcgi_safeget("_service_wait", ""))) {
64 while ((nvram_match("action_service", (char *)action)) && (i-- > 0)) {
65 _dprintf("%s: waiting after %d\n", __FUNCTION__, i);
72 static void wi_generic_noid(char *url
, int len
, char *boundary
)
75 if (len
>= (32 * 1024)) {
76 // syslog(LOG_WARNING, "POST max");
80 if (!post_buf
) free(post_buf
);
81 if ((post_buf
= malloc(len
+ 1)) == NULL
) {
82 // syslog(LOG_CRIT, "Unable to allocate post buffer");
86 if (web_read_x(post_buf
, len
) != len
) {
90 webcgi_init(post_buf
);
94 void wi_generic(char *url
, int len
, char *boundary
)
96 wi_generic_noid(url
, len
, boundary
);
100 static void wo_blank(char *url
)
102 web_puts("\n\n\n\n");
105 static void wo_favicon(char *url
)
107 send_header(200, NULL
, "image/vnd.microsoft.icon", 0);
110 if (nvram_match("web_favicon", "1")) {
111 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
115 send_error(404, NULL, NULL);
120 static void wo_cfe(char *url
)
122 do_file("/dev/mtd/0ro");
125 static void wo_nvram(char *url
)
127 web_pipecmd("nvram show", WOF_NONE
);
130 static void wo_iptables(char *url
)
132 web_pipecmd("iptables -nvL; iptables -t nat -nvL; iptables -t mangle -nvL", WOF_NONE
);
136 static void wo_spin(char *url)
140 strlcpy(s, nvram_safe_get("web_css"), sizeof(s));
141 strlcat(s, "_spin.gif", sizeof(s));
142 if (f_exists(s)) do_file(s);
143 else do_file("_spin.gif");
147 void common_redirect(void)
149 if (atoi(webcgi_safeget("_ajax", ""))) {
150 send_header(200, NULL
, mime_html
, 0);
154 redirect(webcgi_safeget("_redirect", "/"));
158 // ----------------------------------------------------------------------------
160 const struct mime_handler mime_handlers
[] = {
161 { "update.cgi", mime_javascript
, 0, wi_generic
, wo_update
, 1 },
162 { "tomato.cgi", NULL
, 0, wi_generic
, wo_tomato
, 1 },
164 { "debug.js", mime_javascript
, 5, wi_generic_noid
, wo_blank
, 1 }, // while debugging
165 { "cfe/*.bin", mime_binary
, 0, wi_generic
, wo_cfe
, 1 },
166 { "nvram/*.txt", mime_binary
, 0, wi_generic
, wo_nvram
, 1 },
167 { "ipt/*.txt", mime_binary
, 0, wi_generic
, wo_iptables
, 1 },
169 { "cfg/*.cfg", NULL
, 0, wi_generic
, wo_backup
, 1 },
170 { "cfg/restore.cgi", mime_html
, 0, wi_restore
, wo_restore
, 1 },
171 { "cfg/defaults.cgi", NULL
, 0, wi_generic
, wo_defaults
, 1 },
173 { "bwm/*.gz", NULL
, 0, wi_generic
, wo_bwmbackup
, 1 },
174 { "bwm/restore.cgi", NULL
, 0, wi_bwmrestore
, wo_bwmrestore
, 1 },
176 { "logs/view.cgi", NULL
, 0, wi_generic
, wo_viewlog
, 1 },
177 { "logs/*.txt", NULL
, 0, wi_generic
, wo_syslog
, 1 },
179 { "logout.asp", NULL
, 0, wi_generic
, wo_asp
, 1 },
180 { "clearcookies.asp", NULL
, 0, wi_generic
, wo_asp
, 1 },
182 // { "spin.gif", NULL, 0, wi_generic_noid, wo_spin, 1 },
184 { "**.asp", NULL
, 0, wi_generic_noid
, wo_asp
, 1 },
185 { "**.css", "text/css", 2, wi_generic_noid
, do_file
, 1 },
186 { "**.htm", mime_html
, 2, wi_generic_noid
, do_file
, 1 },
187 { "**.gif", "image/gif", 5, wi_generic_noid
, do_file
, 1 },
188 { "**.jpg", "image/jpeg", 5, wi_generic_noid
, do_file
, 1 },
189 { "**.png", "image/png", 5, wi_generic_noid
, do_file
, 1 },
190 { "**.js", mime_javascript
, 2, wi_generic_noid
, do_file
, 1 },
191 { "**.jsx", mime_javascript
, 0, wi_generic
, wo_asp
, 1 },
192 { "**.svg", "image/svg+xml", 2, wi_generic_noid
, do_file
, 1 },
193 { "**.txt", mime_plain
, 2, wi_generic_noid
, do_file
, 1 },
194 { "**.bin", mime_binary
, 0, wi_generic_noid
, do_file
, 1 },
195 { "**.bino", mime_octetstream
, 0, wi_generic_noid
, do_file
, 1 },
196 { "favicon.ico", NULL
, 5, wi_generic_noid
, wo_favicon
, 1 },
199 { "dhcpc.cgi", NULL
, 0, wi_generic
, wo_dhcpc
, 1 },
200 { "dhcpd.cgi", mime_javascript
, 0, wi_generic
, wo_dhcpd
, 1 },
201 { "nvcommit.cgi", NULL
, 0, wi_generic
, wo_nvcommit
, 1 },
202 { "ping.cgi", mime_javascript
, 0, wi_generic
, wo_ping
, 1 },
203 { "trace.cgi", mime_javascript
, 0, wi_generic
, wo_trace
, 1 },
204 { "upgrade.cgi", mime_html
, 0, wi_upgrade
, wo_flash
, 1 },
205 { "upnp.cgi", NULL
, 0, wi_generic
, wo_upnp
, 1 },
206 { "wakeup.cgi", NULL
, 0, wi_generic
, wo_wakeup
, 1 },
207 { "wlmnoise.cgi", mime_html
, 0, wi_generic
, wo_wlmnoise
, 1 },
208 { "wlradio.cgi", NULL
, 0, wi_generic
, wo_wlradio
, 1 },
209 { "resolve.cgi", mime_javascript
, 0, wi_generic
, wo_resolve
, 1 },
210 { "expct.cgi", mime_html
, 0, wi_generic
, wo_expct
, 1 },
211 { "service.cgi", NULL
, 0, wi_generic
, wo_service
, 1 },
212 // { "logout.cgi", NULL, 0, wi_generic, wo_logout, 0 }, // see httpd.c
213 { "shutdown.cgi", mime_html
, 0, wi_generic
, wo_shutdown
, 1 },
216 { "blackhole.cgi", NULL
, 0, wi_blackhole
, NULL
, 1 },
218 // { "test", mime_html, 0, wi_generic, wo_test, 1 },
219 { NULL
, NULL
, 0, NULL
, NULL
, 1 }
222 const aspapi_t aspapi
[] = {
223 { "activeroutes", asp_activeroutes
},
224 { "arplist", asp_arplist
},
225 { "bandwidth", asp_bandwidth
},
226 { "build_time", asp_build_time
},
227 { "cgi_get", asp_cgi_get
},
228 { "compmac", asp_compmac
},
229 { "ctcount", asp_ctcount
},
230 { "ctdump", asp_ctdump
},
231 { "ddnsx", asp_ddnsx
},
232 { "devlist", asp_devlist
},
233 { "dhcpc_time", asp_dhcpc_time
},
235 { "ident", asp_ident
},
236 { "lanip", asp_lanip
},
237 { "layer7", asp_layer7
},
238 { "link_uptime", asp_link_uptime
},
239 { "lipp", asp_lipp
},
240 { "netdev", asp_netdev
},
241 { "notice", asp_notice
},
243 { "nvram", asp_nvram
},
244 { "nvramseq", asp_nvramseq
},
245 { "psup", asp_psup
},
246 { "qrate", asp_qrate
},
247 { "resmsg", asp_resmsg
},
248 { "rrule", asp_rrule
},
249 { "statfs", asp_statfs
},
250 { "sysinfo", asp_sysinfo
},
251 { "time", asp_time
},
252 { "upnpinfo", asp_upnpinfo
},
253 { "version", asp_version
},
254 { "wanstatus", asp_wanstatus
},
255 { "wanup", asp_wanup
},
256 { "wlchannel", asp_wlchannel
},
257 { "wlclient", asp_wlclient
},
258 { "wlcrssi", asp_wlcrssi
},
259 { "wlnoise", asp_wlnoise
},
260 { "wlradio", asp_wlradio
},
261 { "wlscan", asp_wlscan
},
266 // -----------------------------------------------------------------------------
268 static void asp_css(int argc
, char **argv
)
270 const char *css
= nvram_safe_get("web_css");
272 if (strcmp(css
, "tomato") != 0) {
273 web_printf("<link rel='stylesheet' type='text/css' href='%s.css'>", css
);
277 // -----------------------------------------------------------------------------
279 const char *resmsg_get(void)
281 return webcgi_safeget("resmsg", "");
284 void resmsg_set(const char *msg
)
286 webcgi_set("resmsg", strdup(msg
)); // m ok
289 int resmsg_fread(const char *fname
)
294 f_read_string(fname
, s
, sizeof(s
));
295 if ((p
= strchr(s
, '\n')) != NULL
) *p
= 0;
303 static void asp_resmsg(int argc
, char **argv
)
307 if ((p
= js_string(webcgi_safeget("resmsg", (argc
> 0) ? argv
[0] : ""))) == NULL
) return;
308 web_printf("\nresmsg='%s';\n", p
);
312 // ----------------------------------------------------------------------------
314 // verification... simple sanity checks. UI should verify all fields.
316 // todo: move and re-use for filtering - zzz
327 VT_NONE
, // no checking
328 VT_LENGTH
, // check length of string
329 VT_TEXT
, // strip \r, check length of string
330 VT_RANGE
, // expect an integer, check range
331 VT_IP
, // expect an ip address
332 VT_MAC
, // expect a mac address
333 VT_TEMP
// no checks, no commit
340 #define V_NONE VT_NONE, { }, { }
341 #define V_01 VT_RANGE, { .l = 0 }, { .l = 1 }
342 #define V_PORT VT_RANGE, { .l = 2 }, { .l = 65535 }
343 #define V_ONOFF VT_LENGTH, { .i = 2 }, { .i = 3 }
344 #define V_WORD VT_LENGTH, { .i = 1 }, { .i = 16 }
345 #define V_LENGTH(min, max) VT_LENGTH, { .i = min }, { .i = max }
346 #define V_TEXT(min, max) VT_TEXT, { .i = min }, { .i = max }
347 #define V_RANGE(min, max) VT_RANGE, { .l = min }, { .l = max }
348 #define V_IP VT_IP, { }, { }
349 #define V_OCTET VT_RANGE, { .l = 0 }, { .l = 255 }
350 #define V_NUM VT_RANGE, { .l = 0 }, { .l = 0x7FFFFFFF }
351 #define V_TEMP VT_TEMP, { }, { }
353 static const nvset_t nvset_list
[] = {
356 { "router_name", V_LENGTH(0, 32) },
357 { "wan_hostname", V_LENGTH(0, 32) },
358 { "wan_domain", V_LENGTH(0, 32) },
361 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
362 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
364 { "ntp_updates", V_RANGE(-1, 24) },
365 { "ntp_tdod", V_01
},
366 { "ntp_server", V_LENGTH(1, 150) }, // x y z
367 { "ntp_kiss", V_LENGTH(0, 255) },
370 { "dhcpd_static", V_LENGTH(0, 106*101)}, // 106 (max chars per entry) x 100 entries
373 { "ddnsx0", V_LENGTH(0, 2048) },
374 { "ddnsx1", V_LENGTH(0, 2048) },
375 { "ddnsx0_cache", V_LENGTH(0, 1) }, // only to clear
376 { "ddnsx1_cache", V_LENGTH(0, 1) },
377 { "ddnsx_ip", V_LENGTH(0, 32) },
378 { "ddnsx_save", V_01
},
379 { "ddnsx_refresh", V_RANGE(0, 365) },
383 { "wan_proto", V_LENGTH(1, 16) }, // disabled, dhcp, static, pppoe, pptp, l2tp
384 { "wan_ipaddr", V_IP
},
385 { "wan_netmask", V_IP
},
386 { "wan_gateway", V_IP
},
387 { "hb_server_ip", V_LENGTH(0, 32) },
388 { "l2tp_server_ip", V_IP
},
389 { "pptp_server_ip", V_IP
},
390 { "ppp_username", V_LENGTH(0, 60) },
391 { "ppp_passwd", V_LENGTH(0, 60) },
392 { "ppp_service", V_LENGTH(0, 50) },
393 { "ppp_demand", V_01
},
394 { "ppp_idletime", V_RANGE(0, 1440) },
395 { "ppp_redialperiod", V_RANGE(1, 86400) },
396 { "mtu_enable", V_01
},
397 { "wan_mtu", V_RANGE(576, 1500) },
398 { "wan_islan", V_01
},
401 { "lan_ipaddr", V_IP
},
402 { "lan_netmask", V_IP
},
403 { "lan_gateway", V_IP
},
404 { "wan_dns", V_LENGTH(0, 50) }, // ip ip ip
405 { "lan_proto", V_WORD
}, // static, dhcp
406 { "dhcp_start", V_RANGE(1, 254) }, // remove !
407 { "dhcp_num", V_RANGE(1, 255) }, // remove !
408 { "dhcpd_startip", V_IP
},
409 { "dhcpd_endip", V_IP
},
410 { "dhcp_lease", V_RANGE(1, 10080) },
411 { "wan_wins", V_IP
},
414 { "wl_radio", V_01
},
415 { "wl_mode", V_LENGTH(2, 3) }, // ap, sta, wet, wds
416 { "wl_net_mode", V_LENGTH(5, 8) }, // disabled, mixed, b-only, g-only, bg-mixed, n-only [speedbooster]
417 { "wl_ssid", V_LENGTH(1, 32) },
418 { "wl_closed", V_01
},
419 { "wl_channel", V_RANGE(1, 14) },
424 { "security_mode2", V_LENGTH(1, 32) }, // disabled, radius, wep, wpa_personal, wpa_enterprise, wpa2_personal, wpa2_enterprise
425 { "wl_radius_ipaddr", V_IP
},
426 { "wl_radius_port", V_PORT
},
427 { "wl_radius_key", V_LENGTH(1, 64) },
428 { "wl_wep_bit", V_RANGE(64, 128) }, // 64 or 128
429 { "wl_passphrase", V_LENGTH(0, 20) },
430 { "wl_key", V_RANGE(1, 4) },
431 { "wl_key1", V_LENGTH(0, 26) },
432 { "wl_key2", V_LENGTH(0, 26) },
433 { "wl_key3", V_LENGTH(0, 26) },
434 { "wl_key4", V_LENGTH(0, 26) },
435 { "wl_crypto", V_LENGTH(3, 8) }, // tkip, aes, tkip+aes
436 { "wl_wpa_psk", V_LENGTH(8, 64) },
437 { "wl_wpa_gtk_rekey", V_RANGE(60, 7200) },
439 { "wl_lazywds", V_01
},
440 { "wl_wds", V_LENGTH(0, 180) }, // mac mac mac (x 10)
442 { "security_mode", V_LENGTH(1, 32) }, // disabled, radius, wpa, psk,wep, wpa2, psk2, wpa wpa2, psk psk2
443 { "wds_enable", V_01
},
444 { "wl_gmode", V_RANGE(-1, 6) },
445 { "wl_wep", V_LENGTH(1, 32) }, // off, on, restricted,tkip,aes,tkip+aes
446 { "wl_akm", V_LENGTH(0, 32) }, // wpa, wpa2, psk, psk2, wpa wpa2, psk psk2, ""
447 { "wl_auth_mode", V_LENGTH(4, 6) }, // none, radius
450 { "wl_nmode", V_NONE
},
451 { "wl_nreqd", V_NONE
},
455 { "wl_macmode", V_NONE
}, // allow, deny, disabled
456 { "wl_maclist", V_LENGTH(0, 18*201) }, // 18 x 200 (11:22:33:44:55:66 ...)
457 { "macnames", V_LENGTH(0, 62*201) }, // 62 (12+1+48+1) x 50 (112233445566<..>) todo: re-use -- zzz
460 { "ct_max", V_RANGE(128, 10240) },
461 { "ct_tcp_timeout", V_LENGTH(20, 70) },
462 { "ct_udp_timeout", V_LENGTH(5, 15) },
463 { "nf_ttl", V_RANGE(-10, 10) },
471 { "dhcpd_slt", V_RANGE(-1, 43200) }, // -1=infinite, 0=follow normal lease time, >=1 custom
472 { "dhcpd_dmdns", V_01
},
473 { "dhcpd_lmax", V_NUM
},
474 { "dhcpd_gwmode", V_NUM
},
475 { "dns_addget", V_01
},
476 { "dns_intcpt", V_01
},
477 { "dhcpc_minpkt", V_01
},
478 { "dnsmasq_custom", V_TEXT(0, 2048) },
479 // { "dnsmasq_norw", V_01 },
482 { "block_wan", V_01
},
483 { "multicast_pass", V_01
},
484 { "block_loopback", V_01
},
485 { "nf_loopback", V_NUM
},
486 { "ne_syncookies", V_01
},
489 { "wait_time", V_RANGE(3, 20) },
490 { "wan_speed", V_RANGE(0, 4) },
493 { "mac_wan", V_LENGTH(0, 17) },
494 { "mac_wl", V_LENGTH(0, 17) },
497 { "routes_static", V_LENGTH(0, 2048) },
498 { "lan_stp", V_RANGE(0, 1) },
499 { "wk_mode", V_LENGTH(1, 32) }, // gateway, router
500 { "dr_setting", V_RANGE(0, 3) },
501 { "dr_lan_tx", V_LENGTH(0, 32) },
502 { "dr_lan_rx", V_LENGTH(0, 32) },
503 { "dr_wan_tx", V_LENGTH(0, 32) },
504 { "dr_wan_rx", V_LENGTH(0, 32) },
507 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
509 { "wl_rateset", V_LENGTH(2, 7) }, // all, default, 12
510 { "wl_rate", V_RANGE(0, 54 * 1000 * 1000) },
511 { "wl_mrate", V_RANGE(0, 54 * 1000 * 1000) },
512 { "wl_gmode_protection",V_LENGTH(3, 4) }, // off, auto
513 { "wl_frameburst", V_ONOFF
}, // off, on
514 { "wl_bcn", V_RANGE(1, 65535) },
515 { "wl_dtim", V_RANGE(1, 255) },
516 { "wl_frag", V_RANGE(256, 2346) },
517 { "wl_rts", V_RANGE(0, 2347) },
518 { "wl_ap_isolate", V_01
},
519 { "wl_plcphdr", V_LENGTH(4, 5) }, // long, short
520 { "wl_antdiv", V_RANGE(0, 3) },
521 { "wl_txant", V_RANGE(0, 3) },
522 { "wl_txpwr", V_RANGE(0, 255) },
523 { "wl_wme", V_ONOFF
}, // off, on
524 { "wl_wme_no_ack", V_ONOFF
}, // off, on
525 { "wl_maxassoc", V_RANGE(0, 255) },
526 { "wl_distance", V_LENGTH(0, 5) }, // "", 1-99999
527 { "wlx_hpamp", V_01
},
528 { "wlx_hperx", V_01
},
531 { "wl_nmode_protection",V_WORD
, }, // off, auto
532 { "wl_nmcsidx", V_RANGE(-2, 15), }, // -2 - 15
536 { "dmz_enable", V_01
},
537 { "dmz_ipaddr", V_LENGTH(0, 15) },
538 { "dmz_sip", V_LENGTH(0, 512) },
541 { "upnp_enable", V_NUM
},
542 #ifndef USE_MINIUPNPD
543 { "upnp_mnp", V_01
},
544 // { "upnp_config", V_01 },
545 { "upnp_ssdp_interval", V_RANGE(10, 9999) },
546 { "upnp_max_age", V_RANGE(5, 9999) },
550 { "portforward", V_LENGTH(0, 4096) },
553 { "trigforward", V_LENGTH(0, 4096) },
556 // access restriction
557 { "rruleN", V_RANGE(0, 49) },
558 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
561 { "http_enable", V_01
},
562 { "https_enable", V_01
},
563 { "https_crt_save", V_01
},
564 { "https_crt_cn", V_LENGTH(0, 64) },
565 { "https_crt_gen", V_TEMP
},
566 { "remote_management", V_01
},
567 { "remote_mgt_https", V_01
},
568 { "http_lanport", V_PORT
},
569 { "https_lanport", V_PORT
},
570 { "web_wl_filter", V_01
},
571 { "web_css", V_LENGTH(1, 32) },
572 { "web_mx", V_LENGTH(0, 128) },
573 { "http_wanport", V_PORT
},
574 { "telnetd_eas", V_01
},
575 { "telnetd_port", V_PORT
},
576 { "sshd_eas", V_01
},
577 { "sshd_pass", V_01
},
578 { "sshd_port", V_PORT
},
579 { "sshd_remote", V_01
},
580 { "sshd_rport", V_PORT
},
581 { "sshd_authkeys", V_TEXT(0, 4096) },
582 { "rmgt_sip", V_LENGTH(0, 512) },
583 { "ne_shlimit", V_TEXT(1, 50) },
586 { "rstats_enable", V_01
},
587 { "rstats_path", V_LENGTH(0, 48) },
588 { "rstats_stime", V_RANGE(1, 168) },
589 { "rstats_offset", V_RANGE(1, 31) },
590 { "rstats_exclude", V_LENGTH(0, 64) },
591 { "rstats_sshut", V_01
},
592 { "rstats_bak", V_01
},
595 { "sesx_led", V_RANGE(0, 255) }, // amber, white, aoss
596 { "sesx_b0", V_RANGE(0, 4) }, // 0-4: toggle wireless, reboot, shutdown, script
597 { "sesx_b1", V_RANGE(0, 4) }, // "
598 { "sesx_b2", V_RANGE(0, 4) }, // "
599 { "sesx_b3", V_RANGE(0, 4) }, // "
600 { "sesx_script", V_TEXT(0, 1024) }, //
601 { "script_brau", V_TEXT(0, 1024) }, //
604 { "debug_nocommit", V_01
},
605 { "debug_cprintf", V_01
},
606 { "debug_cprintf_file", V_01
},
607 // { "debug_keepfiles", V_01 },
608 { "debug_ddns", V_01
},
609 { "debug_norestart", V_TEXT(0, 128) },
610 { "console_loglevel", V_RANGE(1, 8) },
611 { "t_cafree", V_01
},
612 { "t_hidelr", V_01
},
615 { "sch_rboot", V_TEXT(0, 64) },
616 { "sch_rcon", V_TEXT(0, 64) },
617 { "sch_c1", V_TEXT(0, 64) },
618 { "sch_c1_cmd", V_TEXT(0, 2048) },
619 { "sch_c2", V_TEXT(0, 64) },
620 { "sch_c2_cmd", V_TEXT(0, 2048) },
621 { "sch_c3", V_TEXT(0, 64) },
622 { "sch_c3_cmd", V_TEXT(0, 2048) },
625 { "script_init", V_TEXT(0, 4096) },
626 { "script_shut", V_TEXT(0, 4096) },
627 { "script_fire", V_TEXT(0, 8192) },
628 { "script_wanup", V_TEXT(0, 4096) },
631 { "log_remote", V_01
},
632 { "log_remoteip", V_IP
},
633 { "log_remoteport", V_PORT
},
634 { "log_file", V_01
},
635 { "log_limit", V_RANGE(0, 2400) },
636 { "log_in", V_RANGE(0, 3) },
637 { "log_out", V_RANGE(0, 3) },
638 { "log_mark", V_RANGE(0, 1440) },
639 { "log_events", V_TEXT(0, 32) }, // "acre,crond,ntp"
642 { "cifs1", V_LENGTH(1, 1024) },
643 { "cifs2", V_LENGTH(1, 1024) },
646 { "jffs2_on", V_01
},
647 { "jffs2_exec", V_LENGTH(0, 64) },
648 { "jffs2_format", V_01
},
651 { "qos_enable", V_01
},
656 { "qos_icmp", V_01
},
657 { "qos_reset", V_01
},
658 { "qos_obw", V_RANGE(10, 999999) },
659 { "qos_ibw", V_RANGE(10, 999999) },
660 { "qos_orules", V_LENGTH(0, 4096) },
661 { "qos_default", V_RANGE(0, 9) },
662 { "qos_irates", V_LENGTH(0, 128) },
663 { "qos_orates", V_LENGTH(0, 128) },
665 { "ne_vegas", V_01
},
666 { "ne_valpha", V_NUM
},
667 { "ne_vbeta", V_NUM
},
668 { "ne_vgamma", V_NUM
},
690 port_flow_control_1 0,1
691 port_rate_limit_1 0-8
693 port_flow_control_2 0,1
694 port_rate_limit_2 0-8
696 port_flow_control_3 0,1
697 port_rate_limit_3 0-8
699 port_flow_control_4 0,1
700 port_rate_limit_4 0-8
708 static int save_variables(int write
)
718 static const char *msgf
= "The field \"%s\" is invalid. Please report this problem.";
721 for (v
= nvset_list
; v
->name
; ++v
) {
722 // _dprintf("[%s] %p\n", v->name, webcgi_get((char*)v->name));
723 if ((p
= webcgi_get((char*)v
->name
)) == NULL
) continue;
727 p
= unix_string(p
); // NOTE: p = malloc'd
731 if ((n
< v
->va
.i
) || (n
> v
->vb
.i
)) ok
= 0;
734 l
= strtol(p
, &e
, 10);
735 if ((p
== e
) || (*e
) || (l
< v
->va
.l
) || (l
> v
->vb
.l
)) ok
= 0;
738 if ((sscanf(p
, "%3u.%3u.%3u.%3u", &u
[0], &u
[1], &u
[2], &u
[3]) != 4) ||
739 (u
[0] > 255) || (u
[1] > 255) || (u
[2] > 255) || (u
[3] > 255)) ok
= 0;
742 if ((sscanf(p
, "%2x:%2x:%2x:%2x:%2x:%2x", &u
[0], &u
[1], &u
[2], &u
[3], &u
[4], &u
[5]) != 6) ||
743 (u
[0] > 255) || (u
[1] > 255) || (u
[2] > 255) || (u
[3] > 255) || (u
[4] > 255) || (u
[5] > 255)) ok
= 0;
750 if (v
->vtype
== VT_TEXT
) free(p
);
752 sprintf(s
, msgf
, v
->name
);
757 if (!nvram_match((char *)v
->name
, p
)) {
758 if (v
->vtype
!= VT_TEMP
) dirty
= 1;
759 nvram_set(v
->name
, p
);
762 if (v
->vtype
== VT_TEXT
) free(p
);
769 if (((p1
= webcgi_get("set_password_1")) != NULL
) && (strcmp(p1
, "**********") != 0)) {
770 if (((p2
= webcgi_get("set_password_2")) != NULL
) && (strcmp(p1
, p2
) == 0)) {
771 if ((write
) && (!nvram_match("http_passwd", p1
))) {
773 nvram_set("http_passwd", p1
);
777 sprintf(s
, msgf
, "password");
783 for (n
= 0; n
< 50; ++n
) {
784 sprintf(s
, "rrule%d", n
);
785 if ((p
= webcgi_get(s
)) != NULL
) {
786 if (strlen(p
) > 2048) {
791 if ((write
) && (!nvram_match(s
, p
))) {
798 return (write
) ? dirty
: 1;
801 static void wo_tomato(char *url
)
810 // _dprintf("tomato.cgi\n");
812 red
= webcgi_safeget("_redirect", "");
813 if (!*red
) send_header(200, NULL
, mime_html
, 0);
815 commit
= atoi(webcgi_safeget("_commit", "1"));
816 ajax
= atoi(webcgi_safeget("_ajax", "0"));
818 nvset
= atoi(webcgi_safeget("_nvset", "1"));
820 if (!save_variables(0)) {
822 web_printf("@msg:%s", resmsg_get());
825 parse_asp("error.asp");
829 commit
= save_variables(1) && commit
;
831 resmsg_set("Settings saved.");
834 rboot
= atoi(webcgi_safeget("_reboot", "0"));
836 parse_asp("reboot.asp");
840 web_printf("@msg:%s", resmsg_get());
842 else if (atoi(webcgi_safeget("_moveip", "0"))) {
843 parse_asp("saved-moved.asp");
846 parse_asp("saved.asp");
851 _dprintf("commit from tomato.cgi\n");
855 if ((v
= webcgi_get("_service")) != NULL
) {
857 if (ajax
) web_printf(" Some services are being restarted...");
870 for (i
= atoi(webcgi_safeget("_sleep", "0")); i
> 0; --i
) sleep(1);
872 if (*red
) redirect(red
);
882 // ----------------------------------------------------------------------------
885 static void wo_update(char *url
)
893 if ((name
= webcgi_get("exec")) != NULL
) {
894 for (api
= aspapi
; api
->name
; ++api
) {
895 if (strcmp(api
->name
, name
) == 0) {
896 for (argc
= 0; argc
< 16; ++argc
) {
897 sprintf(s
, "arg%d", argc
);
898 if ((argv
[argc
] = (char *)webcgi_get(s
)) == NULL
) break;
900 api
->exec(argc
, argv
);
907 static void wo_service(char *url
)
911 exec_service(webcgi_safeget("_service", ""));
913 if ((n
= atoi(webcgi_safeget("_sleep", "2"))) <= 0) n
= 2;
919 static void wo_shutdown(char *url
)
921 parse_asp("shutdown.asp");
928 static void wo_nvcommit(char *url
)
930 parse_asp("saved.asp");