4 Copyright (C) 2006-2009 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 // !!TB - CGI Support
101 void wi_cgi_bin(char *url
, int len
, char *boundary
)
103 if (post_buf
) free(post_buf
);
107 if (len
>= (32 * 1024)) {
108 syslog(LOG_WARNING
, "POST length exceeded maximum allowed");
113 if ((post_buf
= malloc(len
+ 1)) == NULL
) {
116 if (web_read_x(post_buf
, len
) != len
) {
124 static void _execute_command(char *url
, char *command
, char *query
, char *output
)
126 char webExecFile
[] = "/tmp/.wxXXXXXX";
127 char webQueryFile
[] = "/tmp/.wqXXXXXX";
131 if (query
) mktemp(webQueryFile
);
133 if ((f
= fopen(webExecFile
, "wb")) != NULL
) {
136 "export REQUEST_METHOD=\"%s\"\n"
140 post
? "POST" : "GET", getenv("PATH"),
141 command
? "" : "./", command
? command
: url
,
142 query
? "<" : "", query
? webQueryFile
: "");
149 chmod(webExecFile
, 0700);
152 if ((f
= fopen(webQueryFile
, "wb")) != NULL
) {
153 fprintf(f
, "%s\n", query
);
164 sprintf(cmd
, "%s >%s 2>&1", webExecFile
, output
);
166 unlink(webQueryFile
);
170 static void wo_cgi_bin(char *url
)
172 char webOutpFile
[] = "/tmp/.woXXXXXX";
175 _execute_command(url
, NULL
, post_buf
, webOutpFile
);
185 static void wo_shell(char *url
)
187 char webOutpFile
[] = "/tmp/.woXXXXXX";
190 _execute_command(NULL
, webcgi_get("command"), NULL
, webOutpFile
);
192 web_puts("\ncmdresult = '");
193 web_putfile(webOutpFile
, WOF_JAVASCRIPT
);
198 static void wo_blank(char *url
)
200 web_puts("\n\n\n\n");
203 static void wo_favicon(char *url
)
205 send_header(200, NULL
, "image/vnd.microsoft.icon", 0);
208 if (nvram_match("web_favicon", "1")) {
209 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
213 send_error(404, NULL, NULL);
218 static void wo_cfe(char *url
)
220 do_file("/dev/mtd/0ro");
223 static void wo_nvram(char *url
)
225 web_pipecmd("nvram show", WOF_NONE
);
228 static void wo_iptables(char *url
)
230 web_pipecmd("iptables -nvL; iptables -t nat -nvL; iptables -t mangle -nvL", WOF_NONE
);
234 static void wo_spin(char *url)
238 strlcpy(s, nvram_safe_get("web_css"), sizeof(s));
239 strlcat(s, "_spin.gif", sizeof(s));
240 if (f_exists(s)) do_file(s);
241 else do_file("_spin.gif");
245 void common_redirect(void)
247 if (atoi(webcgi_safeget("_ajax", ""))) {
248 send_header(200, NULL
, mime_html
, 0);
252 redirect(webcgi_safeget("_redirect", "/"));
256 // ----------------------------------------------------------------------------
258 const struct mime_handler mime_handlers
[] = {
259 { "update.cgi", mime_javascript
, 0, wi_generic
, wo_update
, 1 },
260 { "tomato.cgi", NULL
, 0, wi_generic
, wo_tomato
, 1 },
262 { "debug.js", mime_javascript
, 5, wi_generic_noid
, wo_blank
, 1 }, // while debugging
263 { "cfe/*.bin", mime_binary
, 0, wi_generic
, wo_cfe
, 1 },
264 { "nvram/*.txt", mime_binary
, 0, wi_generic
, wo_nvram
, 1 },
265 { "ipt/*.txt", mime_binary
, 0, wi_generic
, wo_iptables
, 1 },
267 { "cfg/*.cfg", NULL
, 0, wi_generic
, wo_backup
, 1 },
268 { "cfg/restore.cgi", mime_html
, 0, wi_restore
, wo_restore
, 1 },
269 { "cfg/defaults.cgi", NULL
, 0, wi_generic
, wo_defaults
, 1 },
271 { "bwm/*.gz", NULL
, 0, wi_generic
, wo_bwmbackup
, 1 },
272 { "bwm/restore.cgi", NULL
, 0, wi_bwmrestore
, wo_bwmrestore
, 1 },
274 { "logs/view.cgi", NULL
, 0, wi_generic
, wo_viewlog
, 1 },
275 { "logs/*.txt", NULL
, 0, wi_generic
, wo_syslog
, 1 },
277 { "logout.asp", NULL
, 0, wi_generic
, wo_asp
, 1 },
278 { "clearcookies.asp", NULL
, 0, wi_generic
, wo_asp
, 1 },
280 // { "spin.gif", NULL, 0, wi_generic_noid, wo_spin, 1 },
282 { "**.asp", NULL
, 0, wi_generic_noid
, wo_asp
, 1 },
283 { "**.css", "text/css", 2, wi_generic_noid
, do_file
, 1 },
284 { "**.htm", mime_html
, 2, wi_generic_noid
, do_file
, 1 },
285 { "**.gif", "image/gif", 5, wi_generic_noid
, do_file
, 1 },
286 { "**.jpg", "image/jpeg", 5, wi_generic_noid
, do_file
, 1 },
287 { "**.png", "image/png", 5, wi_generic_noid
, do_file
, 1 },
288 { "**.js", mime_javascript
, 2, wi_generic_noid
, do_file
, 1 },
289 { "**.jsx", mime_javascript
, 0, wi_generic
, wo_asp
, 1 },
290 { "**.svg", "image/svg+xml", 2, wi_generic_noid
, do_file
, 1 },
291 { "**.txt", mime_plain
, 2, wi_generic_noid
, do_file
, 1 },
292 { "**.bin", mime_binary
, 0, wi_generic_noid
, do_file
, 1 },
293 { "**.bino", mime_octetstream
, 0, wi_generic_noid
, do_file
, 1 },
294 { "favicon.ico", NULL
, 5, wi_generic_noid
, wo_favicon
, 1 },
295 // !!TB - CGI Support, enable downloading archives
296 { "**/cgi-bin/**|**.sh", NULL
, 0, wi_cgi_bin
, wo_cgi_bin
, 1 },
297 { "**.tar|**.gz", mime_binary
, 0, wi_generic_noid
, do_file
, 1 },
298 { "shell.cgi", mime_javascript
, 0, wi_generic
, wo_shell
, 1 },
300 { "dhcpc.cgi", NULL
, 0, wi_generic
, wo_dhcpc
, 1 },
301 { "dhcpd.cgi", mime_javascript
, 0, wi_generic
, wo_dhcpd
, 1 },
302 { "nvcommit.cgi", NULL
, 0, wi_generic
, wo_nvcommit
, 1 },
303 { "ping.cgi", mime_javascript
, 0, wi_generic
, wo_ping
, 1 },
304 { "trace.cgi", mime_javascript
, 0, wi_generic
, wo_trace
, 1 },
305 { "upgrade.cgi", mime_html
, 0, wi_upgrade
, wo_flash
, 1 },
306 { "upnp.cgi", NULL
, 0, wi_generic
, wo_upnp
, 1 },
307 { "wakeup.cgi", NULL
, 0, wi_generic
, wo_wakeup
, 1 },
308 { "wlmnoise.cgi", mime_html
, 0, wi_generic
, wo_wlmnoise
, 1 },
309 { "wlradio.cgi", NULL
, 0, wi_generic
, wo_wlradio
, 1 },
310 { "resolve.cgi", mime_javascript
, 0, wi_generic
, wo_resolve
, 1 },
311 { "expct.cgi", mime_html
, 0, wi_generic
, wo_expct
, 1 },
312 { "service.cgi", NULL
, 0, wi_generic
, wo_service
, 1 },
313 // { "logout.cgi", NULL, 0, wi_generic, wo_logout, 0 }, // see httpd.c
314 { "shutdown.cgi", mime_html
, 0, wi_generic
, wo_shutdown
, 1 },
317 { "blackhole.cgi", NULL
, 0, wi_blackhole
, NULL
, 1 },
319 // { "test", mime_html, 0, wi_generic, wo_test, 1 },
320 { NULL
, NULL
, 0, NULL
, NULL
, 1 }
323 const aspapi_t aspapi
[] = {
324 { "activeroutes", asp_activeroutes
},
325 { "arplist", asp_arplist
},
326 { "bandwidth", asp_bandwidth
},
327 { "build_time", asp_build_time
},
328 { "cgi_get", asp_cgi_get
},
329 { "compmac", asp_compmac
},
330 { "ctcount", asp_ctcount
},
331 { "ctdump", asp_ctdump
},
332 { "ddnsx", asp_ddnsx
},
333 { "devlist", asp_devlist
},
334 { "dhcpc_time", asp_dhcpc_time
},
336 { "ident", asp_ident
},
337 { "lanip", asp_lanip
},
338 { "layer7", asp_layer7
},
339 { "link_uptime", asp_link_uptime
},
340 { "lipp", asp_lipp
},
341 { "netdev", asp_netdev
},
342 { "notice", asp_notice
},
344 { "nvram", asp_nvram
},
345 { "nvramseq", asp_nvramseq
},
346 { "psup", asp_psup
},
347 { "qrate", asp_qrate
},
348 { "resmsg", asp_resmsg
},
349 { "rrule", asp_rrule
},
350 { "statfs", asp_statfs
},
351 { "sysinfo", asp_sysinfo
},
352 { "time", asp_time
},
353 { "upnpinfo", asp_upnpinfo
},
354 { "version", asp_version
},
355 { "wanstatus", asp_wanstatus
},
356 { "wanup", asp_wanup
},
358 { "wlchannel", asp_wlchannel
},
359 { "wlclient", asp_wlclient
},
360 { "wlcrssi", asp_wlcrssi
},
361 { "wlnoise", asp_wlnoise
},
362 { "wlradio", asp_wlradio
},
363 { "wlscan", asp_wlscan
},
365 { "sharelist", asp_sharelist
},
370 // -----------------------------------------------------------------------------
372 static void asp_css(int argc
, char **argv
)
374 const char *css
= nvram_safe_get("web_css");
376 if (strcmp(css
, "tomato") != 0) {
377 web_printf("<link rel='stylesheet' type='text/css' href='%s.css'>", css
);
381 // -----------------------------------------------------------------------------
383 const char *resmsg_get(void)
385 return webcgi_safeget("resmsg", "");
388 void resmsg_set(const char *msg
)
390 webcgi_set("resmsg", strdup(msg
)); // m ok
393 int resmsg_fread(const char *fname
)
398 f_read_string(fname
, s
, sizeof(s
));
399 if ((p
= strchr(s
, '\n')) != NULL
) *p
= 0;
407 static void asp_resmsg(int argc
, char **argv
)
411 if ((p
= js_string(webcgi_safeget("resmsg", (argc
> 0) ? argv
[0] : ""))) == NULL
) return;
412 web_printf("\nresmsg='%s';\n", p
);
416 // ----------------------------------------------------------------------------
418 // verification... simple sanity checks. UI should verify all fields.
420 // todo: move and re-use for filtering - zzz
431 VT_NONE
, // no checking
432 VT_LENGTH
, // check length of string
433 VT_TEXT
, // strip \r, check length of string
434 VT_RANGE
, // expect an integer, check range
435 VT_IP
, // expect an ip address
436 VT_MAC
, // expect a mac address
437 VT_TEMP
// no checks, no commit
444 #define V_NONE VT_NONE, { }, { }
445 #define V_01 VT_RANGE, { .l = 0 }, { .l = 1 }
446 #define V_PORT VT_RANGE, { .l = 2 }, { .l = 65535 }
447 #define V_ONOFF VT_LENGTH, { .i = 2 }, { .i = 3 }
448 #define V_WORD VT_LENGTH, { .i = 1 }, { .i = 16 }
449 #define V_LENGTH(min, max) VT_LENGTH, { .i = min }, { .i = max }
450 #define V_TEXT(min, max) VT_TEXT, { .i = min }, { .i = max }
451 #define V_RANGE(min, max) VT_RANGE, { .l = min }, { .l = max }
452 #define V_IP VT_IP, { }, { }
453 #define V_OCTET VT_RANGE, { .l = 0 }, { .l = 255 }
454 #define V_NUM VT_RANGE, { .l = 0 }, { .l = 0x7FFFFFFF }
455 #define V_TEMP VT_TEMP, { }, { }
457 static const nvset_t nvset_list
[] = {
460 { "router_name", V_LENGTH(0, 32) },
461 { "wan_hostname", V_LENGTH(0, 32) },
462 { "wan_domain", V_LENGTH(0, 32) },
465 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
466 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
468 { "ntp_updates", V_RANGE(-1, 24) },
469 { "ntp_tdod", V_01
},
470 { "ntp_server", V_LENGTH(1, 150) }, // x y z
471 { "ntp_kiss", V_LENGTH(0, 255) },
474 { "dhcpd_static", V_LENGTH(0, 85*101) }, // 85 (max chars per entry) x 100 entries
477 { "ddnsx0", V_LENGTH(0, 2048) },
478 { "ddnsx1", V_LENGTH(0, 2048) },
479 { "ddnsx0_cache", V_LENGTH(0, 1) }, // only to clear
480 { "ddnsx1_cache", V_LENGTH(0, 1) },
481 { "ddnsx_ip", V_LENGTH(0, 32) },
482 { "ddnsx_save", V_01
},
483 { "ddnsx_refresh", V_RANGE(0, 365) },
487 { "wan_proto", V_LENGTH(1, 16) }, // disabled, dhcp, static, pppoe, pptp, l2tp
488 { "wan_ipaddr", V_IP
},
489 { "wan_netmask", V_IP
},
490 { "wan_gateway", V_IP
},
491 { "hb_server_ip", V_LENGTH(0, 32) },
492 { "l2tp_server_ip", V_IP
},
493 { "pptp_server_ip", V_IP
},
494 { "ppp_username", V_LENGTH(0, 50) },
495 { "ppp_passwd", V_LENGTH(0, 50) },
496 { "ppp_service", V_LENGTH(0, 50) },
497 { "ppp_demand", V_01
},
498 { "ppp_idletime", V_RANGE(0, 1440) },
499 { "ppp_redialperiod", V_RANGE(1, 86400) },
500 { "mtu_enable", V_01
},
501 { "wan_mtu", V_RANGE(576, 1500) },
502 { "wan_islan", V_01
},
505 { "lan_ipaddr", V_IP
},
506 { "lan_netmask", V_IP
},
507 { "lan_gateway", V_IP
},
508 { "wan_dns", V_LENGTH(0, 50) }, // ip ip ip
509 { "lan_proto", V_WORD
}, // static, dhcp
510 { "dhcp_start", V_RANGE(1, 254) }, // remove !
511 { "dhcp_num", V_RANGE(1, 255) }, // remove !
512 { "dhcpd_startip", V_IP
},
513 { "dhcpd_endip", V_IP
},
514 { "dhcp_lease", V_RANGE(1, 10080) },
515 { "wan_wins", V_IP
},
518 { "wl_radio", V_01
},
519 { "wl_mode", V_LENGTH(2, 3) }, // ap, sta, wet, wds
520 { "wl_net_mode", V_LENGTH(5, 8) }, // disabled, mixed, b-only, g-only, bg-mixed, n-only [speedbooster]
521 { "wl_ssid", V_LENGTH(1, 32) },
522 { "wl_closed", V_01
},
523 { "wl_channel", V_RANGE(1, 14) },
528 { "security_mode2", V_LENGTH(1, 32) }, // disabled, radius, wep, wpa_personal, wpa_enterprise, wpa2_personal, wpa2_enterprise
529 { "wl_radius_ipaddr", V_IP
},
530 { "wl_radius_port", V_PORT
},
531 { "wl_radius_key", V_LENGTH(1, 64) },
532 { "wl_wep_bit", V_RANGE(64, 128) }, // 64 or 128
533 { "wl_passphrase", V_LENGTH(0, 20) },
534 { "wl_key", V_RANGE(1, 4) },
535 { "wl_key1", V_LENGTH(0, 26) },
536 { "wl_key2", V_LENGTH(0, 26) },
537 { "wl_key3", V_LENGTH(0, 26) },
538 { "wl_key4", V_LENGTH(0, 26) },
539 { "wl_crypto", V_LENGTH(3, 8) }, // tkip, aes, tkip+aes
540 { "wl_wpa_psk", V_LENGTH(8, 64) },
541 { "wl_wpa_gtk_rekey", V_RANGE(60, 7200) },
543 { "wl_lazywds", V_01
},
544 { "wl_wds", V_LENGTH(0, 180) }, // mac mac mac (x 10)
546 { "security_mode", V_LENGTH(1, 32) }, // disabled, radius, wpa, psk,wep, wpa2, psk2, wpa wpa2, psk psk2
547 { "wds_enable", V_01
},
548 { "wl_gmode", V_RANGE(-1, 6) },
549 { "wl_wep", V_LENGTH(1, 32) }, // off, on, restricted,tkip,aes,tkip+aes
550 { "wl_akm", V_LENGTH(0, 32) }, // wpa, wpa2, psk, psk2, wpa wpa2, psk psk2, ""
551 { "wl_auth_mode", V_LENGTH(4, 6) }, // none, radius
554 { "wl_nmode", V_NONE
},
555 { "wl_nreqd", V_NONE
},
559 { "wl_macmode", V_NONE
}, // allow, deny, disabled
560 { "wl_maclist", V_LENGTH(0, 18*201) }, // 18 x 200 (11:22:33:44:55:66 ...)
561 { "macnames", V_LENGTH(0, 62*201) }, // 62 (12+1+48+1) x 50 (112233445566<..>) todo: re-use -- zzz
564 { "ct_max", V_RANGE(128, 10240) },
565 { "ct_tcp_timeout", V_LENGTH(20, 70) },
566 { "ct_udp_timeout", V_LENGTH(5, 15) },
567 { "nf_ttl", V_RANGE(-10, 10) },
575 { "dhcpd_slt", V_RANGE(-1, 43200) }, // -1=infinite, 0=follow normal lease time, >=1 custom
576 { "dhcpd_dmdns", V_01
},
577 { "dhcpd_lmax", V_NUM
},
578 { "dns_addget", V_01
},
579 { "dns_intcpt", V_01
},
580 { "dhcpc_minpkt", V_01
},
581 { "dnsmasq_custom", V_TEXT(0, 2048) },
582 // { "dnsmasq_norw", V_01 },
585 { "block_wan", V_01
},
586 { "multicast_pass", V_01
},
587 { "block_loopback", V_01
},
588 { "nf_loopback", V_NUM
},
589 { "ne_syncookies", V_01
},
592 { "wait_time", V_RANGE(3, 20) },
593 { "wan_speed", V_RANGE(0, 4) },
596 { "mac_wan", V_LENGTH(0, 17) },
597 { "mac_wl", V_LENGTH(0, 17) },
600 { "routes_static", V_LENGTH(0, 2048) },
601 { "lan_stp", V_RANGE(0, 1) },
602 { "wk_mode", V_LENGTH(1, 32) }, // gateway, router
603 { "dr_setting", V_RANGE(0, 3) },
604 { "dr_lan_tx", V_LENGTH(0, 32) },
605 { "dr_lan_rx", V_LENGTH(0, 32) },
606 { "dr_wan_tx", V_LENGTH(0, 32) },
607 { "dr_wan_rx", V_LENGTH(0, 32) },
610 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
612 { "wl_rateset", V_LENGTH(2, 7) }, // all, default, 12
613 { "wl_rate", V_RANGE(0, 54 * 1000 * 1000) },
614 { "wl_mrate", V_RANGE(0, 54 * 1000 * 1000) },
615 { "wl_gmode_protection",V_LENGTH(3, 4) }, // off, auto
616 { "wl_frameburst", V_ONOFF
}, // off, on
617 { "wl_bcn", V_RANGE(1, 65535) },
618 { "wl_dtim", V_RANGE(1, 255) },
619 { "wl_frag", V_RANGE(256, 2346) },
620 { "wl_rts", V_RANGE(0, 2347) },
621 { "wl_ap_isolate", V_01
},
622 { "wl_plcphdr", V_LENGTH(4, 5) }, // long, short
623 { "wl_antdiv", V_RANGE(0, 3) },
624 { "wl_txant", V_RANGE(0, 3) },
625 { "wl_txpwr", V_RANGE(0, 255) },
626 { "wl_wme", V_ONOFF
}, // off, on
627 { "wl_wme_no_ack", V_ONOFF
}, // off, on
628 { "wl_maxassoc", V_RANGE(0, 255) },
629 { "wl_distance", V_LENGTH(0, 5) }, // "", 1-99999
630 { "wlx_hpamp", V_01
},
631 { "wlx_hperx", V_01
},
634 { "wl_nmode_protection",V_WORD
, }, // off, auto
635 { "wl_nmcsidx", V_RANGE(-2, 15), }, // -2 - 15
639 { "dmz_enable", V_01
},
640 { "dmz_ipaddr", V_LENGTH(0, 15) },
641 { "dmz_sip", V_LENGTH(0, 512) },
644 { "upnp_enable", V_NUM
},
645 { "upnp_secure", V_01
},
646 { "upnp_port", V_RANGE(0, 65535) },
647 { "upnp_ssdp_interval", V_RANGE(10, 9999) },
648 { "upnp_mnp", V_01
},
649 { "upnp_clean", V_01
},
650 { "upnp_clean_interval", V_RANGE(60, 65535) },
651 { "upnp_clean_threshold", V_RANGE(0, 9999) },
652 { "upnp_min_port_int", V_PORT
},
653 { "upnp_max_port_int", V_PORT
},
654 { "upnp_min_port_ext", V_PORT
},
655 { "upnp_max_port_ext", V_PORT
},
656 #ifndef USE_MINIUPNPD
657 // { "upnp_config", V_01 },
658 { "upnp_max_age", V_RANGE(5, 9999) },
662 { "portforward", V_LENGTH(0, 4096) },
665 { "trigforward", V_LENGTH(0, 4096) },
668 // access restriction
669 { "rruleN", V_RANGE(0, 49) },
670 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
673 { "http_enable", V_01
},
674 { "https_enable", V_01
},
675 { "https_crt_save", V_01
},
676 { "https_crt_cn", V_LENGTH(0, 64) },
677 { "https_crt_gen", V_TEMP
},
678 { "remote_management", V_01
},
679 { "remote_mgt_https", V_01
},
680 { "http_lanport", V_PORT
},
681 { "https_lanport", V_PORT
},
682 { "web_wl_filter", V_01
},
683 // { "web_favicon", V_01 },
684 { "web_css", V_LENGTH(1, 32) },
685 { "http_wanport", V_PORT
},
686 { "telnetd_eas", V_01
},
687 { "telnetd_port", V_PORT
},
688 { "sshd_eas", V_01
},
689 { "sshd_pass", V_01
},
690 { "sshd_port", V_PORT
},
691 { "sshd_remote", V_01
},
692 { "sshd_rport", V_PORT
},
693 { "sshd_authkeys", V_TEXT(0, 4096) },
694 { "rmgt_sip", V_LENGTH(0, 512) },
695 { "ne_shlimit", V_TEXT(1, 50) },
698 { "rstats_enable", V_01
},
699 { "rstats_path", V_LENGTH(0, 48) },
700 { "rstats_stime", V_RANGE(1, 168) },
701 { "rstats_offset", V_RANGE(1, 31) },
702 { "rstats_exclude", V_LENGTH(0, 64) },
703 { "rstats_sshut", V_01
},
704 { "rstats_bak", V_01
},
707 { "sesx_b0", V_RANGE(0, 4) }, // 0-4: toggle wireless, reboot, shutdown, script
708 { "sesx_b1", V_RANGE(0, 4) }, // "
709 { "sesx_b2", V_RANGE(0, 4) }, // "
710 { "sesx_b3", V_RANGE(0, 4) }, // "
711 { "sesx_script", V_TEXT(0, 1024) }, //
714 { "debug_nocommit", V_01
},
715 { "debug_cprintf", V_01
},
716 { "debug_cprintf_file", V_01
},
717 // { "debug_keepfiles", V_01 },
718 { "debug_ddns", V_01
},
719 { "debug_norestart", V_TEXT(0, 128) },
720 { "console_loglevel", V_RANGE(1, 8) },
721 { "t_cafree", V_01
},
722 { "t_hidelr", V_01
},
725 { "sch_rboot", V_TEXT(0, 64) },
726 { "sch_rcon", V_TEXT(0, 64) },
727 { "sch_c1", V_TEXT(0, 64) },
728 { "sch_c1_cmd", V_TEXT(0, 2048) },
729 { "sch_c2", V_TEXT(0, 64) },
730 { "sch_c2_cmd", V_TEXT(0, 2048) },
731 { "sch_c3", V_TEXT(0, 64) },
732 { "sch_c3_cmd", V_TEXT(0, 2048) },
735 { "script_init", V_TEXT(0, 4096) },
736 { "script_shut", V_TEXT(0, 4096) },
737 { "script_fire", V_TEXT(0, 8192) },
738 { "script_wanup", V_TEXT(0, 4096) },
741 { "log_remote", V_01
},
742 { "log_remoteip", V_IP
},
743 { "log_remoteport", V_PORT
},
744 { "log_file", V_01
},
745 { "log_limit", V_RANGE(0, 2400) },
746 { "log_in", V_RANGE(0, 3) },
747 { "log_out", V_RANGE(0, 3) },
748 { "log_mark", V_RANGE(0, 1440) },
749 { "log_events", V_TEXT(0, 32) }, // "acre,crond,ntp"
752 { "cifs1", V_LENGTH(1, 1024) },
753 { "cifs2", V_LENGTH(1, 1024) },
756 { "jffs2_on", V_01
},
757 { "jffs2_exec", V_LENGTH(0, 64) },
758 { "jffs2_format", V_01
},
761 { "qos_enable", V_01
},
766 { "qos_icmp", V_01
},
767 { "qos_reset", V_01
},
768 { "qos_obw", V_RANGE(10, 999999) },
769 { "qos_ibw", V_RANGE(10, 999999) },
770 { "qos_orules", V_LENGTH(0, 4096) },
771 { "qos_default", V_RANGE(0, 9) },
772 { "qos_irates", V_LENGTH(0, 128) },
773 { "qos_orates", V_LENGTH(0, 128) },
775 { "ne_vegas", V_01
},
776 { "ne_valpha", V_NUM
},
777 { "ne_vbeta", V_NUM
},
778 { "ne_vgamma", V_NUM
},
800 port_flow_control_1 0,1
801 port_rate_limit_1 0-8
803 port_flow_control_2 0,1
804 port_rate_limit_2 0-8
806 port_flow_control_3 0,1
807 port_rate_limit_3 0-8
809 port_flow_control_4 0,1
810 port_rate_limit_4 0-8
818 static int save_variables(int write
)
828 static const char *msgf
= "The field \"%s\" is invalid. Please report this problem.";
831 for (v
= nvset_list
; v
->name
; ++v
) {
832 // _dprintf("[%s] %p\n", v->name, webcgi_get((char*)v->name));
833 if ((p
= webcgi_get((char*)v
->name
)) == NULL
) continue;
837 p
= unix_string(p
); // NOTE: p = malloc'd
841 if ((n
< v
->va
.i
) || (n
> v
->vb
.i
)) ok
= 0;
844 l
= strtol(p
, &e
, 10);
845 if ((p
== e
) || (*e
) || (l
< v
->va
.l
) || (l
> v
->vb
.l
)) ok
= 0;
848 if ((sscanf(p
, "%3u.%3u.%3u.%3u", &u
[0], &u
[1], &u
[2], &u
[3]) != 4) ||
849 (u
[0] > 255) || (u
[1] > 255) || (u
[2] > 255) || (u
[3] > 255)) ok
= 0;
852 if ((sscanf(p
, "%2x:%2x:%2x:%2x:%2x:%2x", &u
[0], &u
[1], &u
[2], &u
[3], &u
[4], &u
[5]) != 6) ||
853 (u
[0] > 255) || (u
[1] > 255) || (u
[2] > 255) || (u
[3] > 255) || (u
[4] > 255) || (u
[5] > 255)) ok
= 0;
860 if (v
->vtype
== VT_TEXT
) free(p
);
862 sprintf(s
, msgf
, v
->name
);
867 if (!nvram_match((char *)v
->name
, p
)) {
868 if (v
->vtype
!= VT_TEMP
) dirty
= 1;
869 nvram_set(v
->name
, p
);
872 if (v
->vtype
== VT_TEXT
) free(p
);
879 if (((p1
= webcgi_get("set_password_1")) != NULL
) && (strcmp(p1
, "**********") != 0)) {
880 if (((p2
= webcgi_get("set_password_2")) != NULL
) && (strcmp(p1
, p2
) == 0)) {
881 if ((write
) && (!nvram_match("http_passwd", p1
))) {
883 nvram_set("http_passwd", p1
);
887 sprintf(s
, msgf
, "password");
893 for (n
= 0; n
< 50; ++n
) {
894 sprintf(s
, "rrule%d", n
);
895 if ((p
= webcgi_get(s
)) != NULL
) {
896 if (strlen(p
) > 2048) {
901 if ((write
) && (!nvram_match(s
, p
))) {
908 return (write
) ? dirty
: 1;
911 static void wo_tomato(char *url
)
920 // _dprintf("tomato.cgi\n");
922 red
= webcgi_safeget("_redirect", "");
923 if (!*red
) send_header(200, NULL
, mime_html
, 0);
925 commit
= atoi(webcgi_safeget("_commit", "1"));
926 ajax
= atoi(webcgi_safeget("_ajax", "0"));
928 nvset
= atoi(webcgi_safeget("_nvset", "1"));
930 if (!save_variables(0)) {
932 web_printf("@msg:%s", resmsg_get());
935 parse_asp("error.asp");
939 commit
= save_variables(1) && commit
;
941 resmsg_set("Settings saved.");
944 rboot
= atoi(webcgi_safeget("_reboot", "0"));
946 parse_asp("reboot.asp");
950 web_printf("@msg:%s", resmsg_get());
952 else if (atoi(webcgi_safeget("_moveip", "0"))) {
953 parse_asp("saved-moved.asp");
956 parse_asp("saved.asp");
961 _dprintf("commit from tomato.cgi\n");
965 if ((v
= webcgi_get("_service")) != NULL
) {
967 if (ajax
) web_printf(" Some services are being restarted...");
980 for (i
= atoi(webcgi_safeget("_sleep", "0")); i
> 0; --i
) sleep(1);
982 if (*red
) redirect(red
);
992 // ----------------------------------------------------------------------------
995 static void wo_update(char *url
)
1003 if ((name
= webcgi_get("exec")) != NULL
) {
1004 for (api
= aspapi
; api
->name
; ++api
) {
1005 if (strcmp(api
->name
, name
) == 0) {
1006 for (argc
= 0; argc
< 16; ++argc
) {
1007 sprintf(s
, "arg%d", argc
);
1008 if ((argv
[argc
] = (char *)webcgi_get(s
)) == NULL
) break;
1010 api
->exec(argc
, argv
);
1017 static void wo_service(char *url
)
1021 exec_service(webcgi_safeget("_service", ""));
1023 if ((n
= atoi(webcgi_safeget("_sleep", "2"))) <= 0) n
= 2;
1030 static void wo_logout(char *url)
1034 // doesn't work with all browsers...
1036 if (((user_agent) && (strstr(user_agent, "Opera") != NULL))) {
1037 sprintf(s, "%llx", (unsigned long long)time(NULL) * rand());
1038 send_authenticate(s);
1041 send_authenticate(NULL);
1046 static void wo_shutdown(char *url
)
1048 parse_asp("shutdown.asp");
1055 static void wo_nvcommit(char *url
)
1057 parse_asp("saved.asp");