Merge branch 'tomato-ND-fixes' into tomato-ND-USBmod
[tomato.git] / release / src / router / httpd / tomato.c
blob2389b2d1d29d951ee9060d8619b1aca19d706e3c
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <sys/sysinfo.h>
11 #include <sys/stat.h>
12 #include <arpa/inet.h>
13 #include <time.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;
22 int rboot = 0;
23 extern int post;
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)
42 int i;
44 _dprintf("exec_service: %s\n", action);
46 i = 10;
47 while ((!nvram_match("action_service", "")) && (i-- > 0)) {
48 _dprintf("%s: waiting before %d\n", __FUNCTION__, i);
49 sleep(1);
52 nvram_set("action_service", action);
53 kill(1, SIGUSR1);
55 i = 3;
56 while ((nvram_match("action_service", (char *)action)) && (i-- > 0)) {
57 _dprintf("%s: waiting after %d\n", __FUNCTION__, i);
58 sleep(1);
62 if (atoi(webcgi_safeget("_service_wait", ""))) {
63 i = 10;
64 while ((nvram_match("action_service", (char *)action)) && (i-- > 0)) {
65 _dprintf("%s: waiting after %d\n", __FUNCTION__, i);
66 sleep(1);
72 static void wi_generic_noid(char *url, int len, char *boundary)
74 if (post == 1) {
75 if (len >= (32 * 1024)) {
76 // syslog(LOG_WARNING, "POST max");
77 exit(1);
80 if (post_buf) free(post_buf);
81 if ((post_buf = malloc(len + 1)) == NULL) {
82 // syslog(LOG_CRIT, "Unable to allocate post buffer");
83 exit(1);
86 if (web_read_x(post_buf, len) != len) {
87 exit(1);
89 post_buf[len] = 0;
90 webcgi_init(post_buf);
94 void wi_generic(char *url, int len, char *boundary)
96 wi_generic_noid(url, len, boundary);
97 check_id(url);
100 // !!TB - CGI Support
101 void wi_cgi_bin(char *url, int len, char *boundary)
103 if (post_buf) free(post_buf);
104 post_buf = NULL;
106 if (post) {
107 if (len >= (32 * 1024)) {
108 syslog(LOG_WARNING, "POST length exceeded maximum allowed");
109 exit(1);
112 if (len > 0) {
113 if ((post_buf = malloc(len + 1)) == NULL) {
114 exit(1);
116 if (web_read_x(post_buf, len) != len) {
117 exit(1);
119 post_buf[len] = 0;
124 static void _execute_command(char *url, char *command, char *query, char *output)
126 char webExecFile[] = "/tmp/.wxXXXXXX";
127 char webQueryFile[] = "/tmp/.wqXXXXXX";
128 FILE *f;
130 mktemp(webExecFile);
131 if (query) mktemp(webQueryFile);
133 if ((f = fopen(webExecFile, "wb")) != NULL) {
134 fprintf(f,
135 "#!/bin/sh\n"
136 "export REQUEST_METHOD=\"%s\"\n"
137 "export PATH=%s\n"
138 ". /etc/profile\n"
139 "%s%s %s%s\n",
140 post ? "POST" : "GET", getenv("PATH"),
141 command ? "" : "./", command ? command : url,
142 query ? "<" : "", query ? webQueryFile : "");
143 fclose(f);
145 else {
146 unlink(output);
147 exit(1);
149 chmod(webExecFile, 0700);
151 if (query) {
152 if ((f = fopen(webQueryFile, "wb")) != NULL) {
153 fprintf(f, "%s\n", query);
154 fclose(f);
156 else {
157 unlink(output);
158 unlink(webExecFile);
159 exit(1);
163 char cmd[128];
164 sprintf(cmd, "%s >%s 2>&1", webExecFile, output);
165 system(cmd);
166 unlink(webQueryFile);
167 unlink(webExecFile);
170 static void wo_cgi_bin(char *url)
172 char webOutpFile[] = "/tmp/.woXXXXXX";
174 mktemp(webOutpFile);
175 _execute_command(url, NULL, post_buf, webOutpFile);
177 if (post_buf) {
178 free(post_buf);
179 post_buf = NULL;
181 wo_asp(webOutpFile);
182 unlink(webOutpFile);
185 static void wo_shell(char *url)
187 char webOutpFile[] = "/tmp/.woXXXXXX";
189 mktemp(webOutpFile);
190 _execute_command(NULL, webcgi_get("command"), NULL, webOutpFile);
192 web_puts("\ncmdresult = '");
193 web_putfile(webOutpFile, WOF_JAVASCRIPT);
194 web_puts("';");
195 unlink(webOutpFile);
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);
206 do_file(url);
208 if (nvram_match("web_favicon", "1")) {
209 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
210 do_file(url);
212 else {
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)
236 char s[64];
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);
249 web_puts("OK");
251 else {
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 },
316 #ifdef BLACKHOLE
317 { "blackhole.cgi", NULL, 0, wi_blackhole, NULL, 1 },
318 #endif
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 },
335 { "dns", asp_dns },
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 },
343 { "nv", asp_nv },
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 },
357 { "css", asp_css },
358 { "wlchannel", asp_wlchannel },
359 { "wlclient", asp_wlclient },
360 { "wlcrssi", asp_wlcrssi },
361 { "wlnoise", asp_wlnoise },
362 { "wlradio", asp_wlradio },
363 { "wlscan", asp_wlscan },
364 #if TOMATO_SL
365 { "sharelist", asp_sharelist },
366 #endif
367 { NULL, NULL }
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)
395 char s[256];
396 char *p;
398 f_read_string(fname, s, sizeof(s));
399 if ((p = strchr(s, '\n')) != NULL) *p = 0;
400 if (s[0]) {
401 resmsg_set(s);
402 return 1;
404 return 0;
407 static void asp_resmsg(int argc, char **argv)
409 char *p;
411 if ((p = js_string(webcgi_safeget("resmsg", (argc > 0) ? argv[0] : ""))) == NULL) return;
412 web_printf("\nresmsg='%s';\n", p);
413 free(p);
416 // ----------------------------------------------------------------------------
418 // verification... simple sanity checks. UI should verify all fields.
420 // todo: move and re-use for filtering - zzz
422 typedef union {
423 int i;
424 long l;
425 const char *s;
426 } nvset_varg_t;
428 typedef struct {
429 const char *name;
430 enum {
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
438 } vtype;
439 nvset_varg_t va;
440 nvset_varg_t vb;
441 } nvset_t;
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[] = {
459 // basic-ident
460 { "router_name", V_LENGTH(0, 32) },
461 { "wan_hostname", V_LENGTH(0, 32) },
462 { "wan_domain", V_LENGTH(0, 32) },
464 // basic-time
465 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
466 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
467 { "tm_dst", V_01 },
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) },
473 // basic-static
474 { "dhcpd_static", V_LENGTH(0, 85*101) }, // 85 (max chars per entry) x 100 entries
476 // basic-ddns
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) },
485 // basic-network
486 // WAN
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 },
504 // LAN
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 },
517 // wireless
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) },
524 #if TOMATO_N
525 // ! update
526 #endif
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
553 #if TOMATO_N
554 { "wl_nmode", V_NONE },
555 { "wl_nreqd", V_NONE },
556 #endif
558 // basic-wfilter
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
563 // advanced-ctnf
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) },
568 { "nf_l7in", V_01 },
569 { "nf_rtsp", V_01 },
570 { "nf_pptp", V_01 },
571 { "nf_h323", V_01 },
572 { "nf_ftp", V_01 },
574 // advanced-dhcpdns
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 },
584 // advanced-firewall
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 },
591 // advanced-misc
592 { "wait_time", V_RANGE(3, 20) },
593 { "wan_speed", V_RANGE(0, 4) },
595 // advanced-mac
596 { "mac_wan", V_LENGTH(0, 17) },
597 { "mac_wl", V_LENGTH(0, 17) },
599 // advanced-routing
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) },
609 // advanced-wireless
610 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
611 { "wl_auth", V_01 },
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 },
633 #if TOMATO_N
634 { "wl_nmode_protection",V_WORD, }, // off, auto
635 { "wl_nmcsidx", V_RANGE(-2, 15), }, // -2 - 15
636 #endif
638 // forward-dmz
639 { "dmz_enable", V_01 },
640 { "dmz_ipaddr", V_LENGTH(0, 15) },
641 { "dmz_sip", V_LENGTH(0, 512) },
643 // forward-upnp
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) },
659 #endif
661 // forward-basic
662 { "portforward", V_LENGTH(0, 4096) },
664 // forward-triggered
665 { "trigforward", V_LENGTH(0, 4096) },
668 // access restriction
669 { "rruleN", V_RANGE(0, 49) },
670 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
672 // admin-access
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) },
697 // admin-bwm
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 },
706 // admin-buttons
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) }, //
713 // admin-debug
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 },
724 // admin-sched
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) },
734 // admin-scripts
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) },
740 // admin-log
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"
751 // admin-cifs
752 { "cifs1", V_LENGTH(1, 1024) },
753 { "cifs2", V_LENGTH(1, 1024) },
755 // admin-jffs2
756 { "jffs2_on", V_01 },
757 { "jffs2_exec", V_LENGTH(0, 64) },
758 { "jffs2_format", V_01 },
760 // qos
761 { "qos_enable", V_01 },
762 { "qos_ack", V_01 },
763 { "qos_syn", V_01 },
764 { "qos_fin", V_01 },
765 { "qos_rst", 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 },
782 ppp_static 0/1
783 ppp_static_ip IP
784 wl_enable 0/1
785 wl_wds_timeout
786 wl_maxassoc 1-256
787 wl_phytype a,b,g
788 wl_net_reauth
789 wl_preauth
790 wl_wme_ap_bk
791 wl_wme_ap_be
792 wl_wme_ap_vi
793 wl_wme_ap_vo
794 wl_wme_sta_bk
795 wl_wme_sta_be
796 wl_wme_sta_vi
797 wl_wme_sta_vo
799 port_priority_1 0-2
800 port_flow_control_1 0,1
801 port_rate_limit_1 0-8
802 port_priority_2 0-2
803 port_flow_control_2 0,1
804 port_rate_limit_2 0-8
805 port_priority_3 0-2
806 port_flow_control_3 0,1
807 port_rate_limit_3 0-8
808 port_priority_4 0-2
809 port_flow_control_4 0,1
810 port_rate_limit_4 0-8
811 wl_ap_ip
812 wl_ap_ssid
815 { NULL }
818 static int save_variables(int write)
820 const nvset_t *v;
821 char *p, *e;
822 int n;
823 long l;
824 unsigned u[6];
825 int ok;
826 char s[256];
827 int dirty;
828 static const char *msgf = "The field \"%s\" is invalid. Please report this problem.";
830 dirty = 0;
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;
834 ok = 1;
835 switch (v->vtype) {
836 case VT_TEXT:
837 p = unix_string(p); // NOTE: p = malloc'd
838 // drop
839 case VT_LENGTH:
840 n = strlen(p);
841 if ((n < v->va.i) || (n > v->vb.i)) ok = 0;
842 break;
843 case VT_RANGE:
844 l = strtol(p, &e, 10);
845 if ((p == e) || (*e) || (l < v->va.l) || (l > v->vb.l)) ok = 0;
846 break;
847 case VT_IP:
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;
850 break;
851 case VT_MAC:
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;
854 break;
855 default:
856 // shutup gcc
857 break;
859 if (!ok) {
860 if (v->vtype == VT_TEXT) free(p);
862 sprintf(s, msgf, v->name);
863 resmsg_set(s);
864 return 0;
866 if (write) {
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);
876 // special cases
878 char *p1, *p2;
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))) {
882 dirty = 1;
883 nvram_set("http_passwd", p1);
886 else {
887 sprintf(s, msgf, "password");
888 resmsg_set(s);
889 return 0;
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) {
897 sprintf(s, msgf, s);
898 resmsg_set(s);
899 return 0;
901 if ((write) && (!nvram_match(s, p))) {
902 dirty = 1;
903 nvram_set(s, p);
908 return (write) ? dirty : 1;
911 static void wo_tomato(char *url)
913 char *v;
914 int i;
915 int ajax;
916 int nvset;
917 const char *red;
918 int commit;
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"));
929 if (nvset) {
930 if (!save_variables(0)) {
931 if (ajax) {
932 web_printf("@msg:%s", resmsg_get());
934 else {
935 parse_asp("error.asp");
937 return;
939 commit = save_variables(1) && commit;
941 resmsg_set("Settings saved.");
944 rboot = atoi(webcgi_safeget("_reboot", "0"));
945 if (rboot) {
946 parse_asp("reboot.asp");
948 else {
949 if (ajax) {
950 web_printf("@msg:%s", resmsg_get());
952 else if (atoi(webcgi_safeget("_moveip", "0"))) {
953 parse_asp("saved-moved.asp");
955 else if (!*red) {
956 parse_asp("saved.asp");
960 if (commit) {
961 _dprintf("commit from tomato.cgi\n");
962 nvram_commit_x();
965 if ((v = webcgi_get("_service")) != NULL) {
966 if (!*red) {
967 if (ajax) web_printf(" Some services are being restarted...");
968 web_close();
970 sleep(1);
972 if (*v == '*') {
973 kill(1, SIGHUP);
975 else if (*v != 0) {
976 exec_service(v);
980 for (i = atoi(webcgi_safeget("_sleep", "0")); i > 0; --i) sleep(1);
982 if (*red) redirect(red);
984 if (rboot) {
985 web_close();
986 sleep(1);
987 kill(1, SIGTERM);
992 // ----------------------------------------------------------------------------
995 static void wo_update(char *url)
997 const aspapi_t *api;
998 const char *name;
999 int argc;
1000 char *argv[16];
1001 char s[32];
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);
1011 break;
1017 static void wo_service(char *url)
1019 int n;
1021 exec_service(webcgi_safeget("_service", ""));
1023 if ((n = atoi(webcgi_safeget("_sleep", "2"))) <= 0) n = 2;
1024 sleep(n);
1026 common_redirect();
1030 static void wo_logout(char *url)
1032 char s[256];
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);
1040 else {
1041 send_authenticate(NULL);
1046 static void wo_shutdown(char *url)
1048 parse_asp("shutdown.asp");
1049 web_close();
1050 sleep(1);
1052 kill(1, SIGQUIT);
1055 static void wo_nvcommit(char *url)
1057 parse_asp("saved.asp");
1058 web_close();
1059 nvram_commit();