Merge Tomato 1.26
[tomato.git] / release / src / router / httpd / tomato.c
blob48230932665ddf42c2ad73c4e49789662f7f63f9
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 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);
108 do_file(url);
110 if (nvram_match("web_favicon", "1")) {
111 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
112 do_file(url);
114 else {
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)
138 char s[64];
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);
151 web_puts("OK");
153 else {
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 },
214 #ifdef TCONFIG_USB
215 { "usbcmd.cgi", mime_javascript, 0, wi_generic, wo_usbcommand, 1 }, //!!TB - USB
216 #endif
217 #ifdef BLACKHOLE
218 { "blackhole.cgi", NULL, 0, wi_blackhole, NULL, 1 },
219 #endif
220 // { "test", mime_html, 0, wi_generic, wo_test, 1 },
221 { NULL, NULL, 0, NULL, NULL, 1 }
224 const aspapi_t aspapi[] = {
225 { "activeroutes", asp_activeroutes },
226 { "arplist", asp_arplist },
227 { "bandwidth", asp_bandwidth },
228 { "build_time", asp_build_time },
229 { "cgi_get", asp_cgi_get },
230 { "compmac", asp_compmac },
231 { "ctcount", asp_ctcount },
232 { "ctdump", asp_ctdump },
233 { "ddnsx", asp_ddnsx },
234 { "devlist", asp_devlist },
235 { "dhcpc_time", asp_dhcpc_time },
236 { "dns", asp_dns },
237 { "ident", asp_ident },
238 { "lanip", asp_lanip },
239 { "layer7", asp_layer7 },
240 { "link_uptime", asp_link_uptime },
241 { "lipp", asp_lipp },
242 { "netdev", asp_netdev },
243 { "notice", asp_notice },
244 { "nv", asp_nv },
245 { "nvram", asp_nvram },
246 { "nvramseq", asp_nvramseq },
247 { "psup", asp_psup },
248 { "qrate", asp_qrate },
249 { "resmsg", asp_resmsg },
250 { "rrule", asp_rrule },
251 { "statfs", asp_statfs },
252 { "sysinfo", asp_sysinfo },
253 { "time", asp_time },
254 { "upnpinfo", asp_upnpinfo },
255 { "version", asp_version },
256 { "wanstatus", asp_wanstatus },
257 { "wanup", asp_wanup },
258 { "wlchannel", asp_wlchannel },
259 { "wlclient", asp_wlclient },
260 { "wlcrssi", asp_wlcrssi },
261 { "wlnoise", asp_wlnoise },
262 { "wlradio", asp_wlradio },
263 { "wlscan", asp_wlscan },
264 #ifdef TCONFIG_USB
265 { "usbdevices", asp_usbdevices }, //!!TB - USB Support
266 #endif
267 { "css", asp_css },
268 { NULL, NULL }
271 // -----------------------------------------------------------------------------
273 static void asp_css(int argc, char **argv)
275 const char *css = nvram_safe_get("web_css");
277 if (strcmp(css, "tomato") != 0) {
278 web_printf("<link rel='stylesheet' type='text/css' href='%s.css'>", css);
282 // -----------------------------------------------------------------------------
284 const char *resmsg_get(void)
286 return webcgi_safeget("resmsg", "");
289 void resmsg_set(const char *msg)
291 webcgi_set("resmsg", strdup(msg)); // m ok
294 int resmsg_fread(const char *fname)
296 char s[256];
297 char *p;
299 f_read_string(fname, s, sizeof(s));
300 if ((p = strchr(s, '\n')) != NULL) *p = 0;
301 if (s[0]) {
302 resmsg_set(s);
303 return 1;
305 return 0;
308 static void asp_resmsg(int argc, char **argv)
310 char *p;
312 if ((p = js_string(webcgi_safeget("resmsg", (argc > 0) ? argv[0] : ""))) == NULL) return;
313 web_printf("\nresmsg='%s';\n", p);
314 free(p);
317 // ----------------------------------------------------------------------------
319 // verification... simple sanity checks. UI should verify all fields.
321 // todo: move and re-use for filtering - zzz
323 typedef union {
324 int i;
325 long l;
326 const char *s;
327 } nvset_varg_t;
329 typedef struct {
330 const char *name;
331 enum {
332 VT_NONE, // no checking
333 VT_LENGTH, // check length of string
334 VT_TEXT, // strip \r, check length of string
335 VT_RANGE, // expect an integer, check range
336 VT_IP, // expect an ip address
337 VT_MAC, // expect a mac address
338 VT_TEMP // no checks, no commit
339 } vtype;
340 nvset_varg_t va;
341 nvset_varg_t vb;
342 } nvset_t;
345 #define V_NONE VT_NONE, { }, { }
346 #define V_01 VT_RANGE, { .l = 0 }, { .l = 1 }
347 #define V_PORT VT_RANGE, { .l = 2 }, { .l = 65535 }
348 #define V_ONOFF VT_LENGTH, { .i = 2 }, { .i = 3 }
349 #define V_WORD VT_LENGTH, { .i = 1 }, { .i = 16 }
350 #define V_LENGTH(min, max) VT_LENGTH, { .i = min }, { .i = max }
351 #define V_TEXT(min, max) VT_TEXT, { .i = min }, { .i = max }
352 #define V_RANGE(min, max) VT_RANGE, { .l = min }, { .l = max }
353 #define V_IP VT_IP, { }, { }
354 #define V_OCTET VT_RANGE, { .l = 0 }, { .l = 255 }
355 #define V_NUM VT_RANGE, { .l = 0 }, { .l = 0x7FFFFFFF }
356 #define V_TEMP VT_TEMP, { }, { }
358 static const nvset_t nvset_list[] = {
360 // basic-ident
361 { "router_name", V_LENGTH(0, 32) },
362 { "wan_hostname", V_LENGTH(0, 32) },
363 { "wan_domain", V_LENGTH(0, 32) },
365 // basic-time
366 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
367 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
368 { "tm_dst", V_01 },
369 { "ntp_updates", V_RANGE(-1, 24) },
370 { "ntp_tdod", V_01 },
371 { "ntp_server", V_LENGTH(1, 150) }, // x y z
372 { "ntp_kiss", V_LENGTH(0, 255) },
374 // basic-static
375 { "dhcpd_static", V_LENGTH(0, 106*101)}, // 106 (max chars per entry) x 100 entries
377 // basic-ddns
378 { "ddnsx0", V_LENGTH(0, 2048) },
379 { "ddnsx1", V_LENGTH(0, 2048) },
380 { "ddnsx0_cache", V_LENGTH(0, 1) }, // only to clear
381 { "ddnsx1_cache", V_LENGTH(0, 1) },
382 { "ddnsx_ip", V_LENGTH(0, 32) },
383 { "ddnsx_save", V_01 },
384 { "ddnsx_refresh", V_RANGE(0, 365) },
386 // basic-network
387 // WAN
388 { "wan_proto", V_LENGTH(1, 16) }, // disabled, dhcp, static, pppoe, pptp, l2tp
389 { "wan_ipaddr", V_IP },
390 { "wan_netmask", V_IP },
391 { "wan_gateway", V_IP },
392 { "hb_server_ip", V_LENGTH(0, 32) },
393 { "l2tp_server_ip", V_IP },
394 { "pptp_server_ip", V_IP },
395 { "ppp_username", V_LENGTH(0, 50) },
396 { "ppp_passwd", V_LENGTH(0, 50) },
397 { "ppp_service", V_LENGTH(0, 50) },
398 { "ppp_demand", V_01 },
399 { "ppp_idletime", V_RANGE(0, 1440) },
400 { "ppp_redialperiod", V_RANGE(1, 86400) },
401 { "mtu_enable", V_01 },
402 { "wan_mtu", V_RANGE(576, 1500) },
403 { "wan_islan", V_01 },
405 // LAN
406 { "lan_ipaddr", V_IP },
407 { "lan_netmask", V_IP },
408 { "lan_gateway", V_IP },
409 { "wan_dns", V_LENGTH(0, 50) }, // ip ip ip
410 { "lan_proto", V_WORD }, // static, dhcp
411 { "dhcp_start", V_RANGE(1, 254) }, // remove !
412 { "dhcp_num", V_RANGE(1, 255) }, // remove !
413 { "dhcpd_startip", V_IP },
414 { "dhcpd_endip", V_IP },
415 { "dhcp_lease", V_RANGE(1, 10080) },
416 { "wan_wins", V_IP },
418 // wireless
419 { "wl_radio", V_01 },
420 { "wl_mode", V_LENGTH(2, 3) }, // ap, sta, wet, wds
421 { "wl_net_mode", V_LENGTH(5, 8) }, // disabled, mixed, b-only, g-only, bg-mixed, n-only [speedbooster]
422 { "wl_ssid", V_LENGTH(1, 32) },
423 { "wl_closed", V_01 },
424 { "wl_channel", V_RANGE(1, 14) },
425 #if TOMATO_N
426 // ! update
427 #endif
429 { "security_mode2", V_LENGTH(1, 32) }, // disabled, radius, wep, wpa_personal, wpa_enterprise, wpa2_personal, wpa2_enterprise
430 { "wl_radius_ipaddr", V_IP },
431 { "wl_radius_port", V_PORT },
432 { "wl_radius_key", V_LENGTH(1, 64) },
433 { "wl_wep_bit", V_RANGE(64, 128) }, // 64 or 128
434 { "wl_passphrase", V_LENGTH(0, 20) },
435 { "wl_key", V_RANGE(1, 4) },
436 { "wl_key1", V_LENGTH(0, 26) },
437 { "wl_key2", V_LENGTH(0, 26) },
438 { "wl_key3", V_LENGTH(0, 26) },
439 { "wl_key4", V_LENGTH(0, 26) },
440 { "wl_crypto", V_LENGTH(3, 8) }, // tkip, aes, tkip+aes
441 { "wl_wpa_psk", V_LENGTH(8, 64) },
442 { "wl_wpa_gtk_rekey", V_RANGE(60, 7200) },
444 { "wl_lazywds", V_01 },
445 { "wl_wds", V_LENGTH(0, 180) }, // mac mac mac (x 10)
447 { "security_mode", V_LENGTH(1, 32) }, // disabled, radius, wpa, psk,wep, wpa2, psk2, wpa wpa2, psk psk2
448 { "wds_enable", V_01 },
449 { "wl_gmode", V_RANGE(-1, 6) },
450 { "wl_wep", V_LENGTH(1, 32) }, // off, on, restricted,tkip,aes,tkip+aes
451 { "wl_akm", V_LENGTH(0, 32) }, // wpa, wpa2, psk, psk2, wpa wpa2, psk psk2, ""
452 { "wl_auth_mode", V_LENGTH(4, 6) }, // none, radius
454 #if TOMATO_N
455 { "wl_nmode", V_NONE },
456 { "wl_nreqd", V_NONE },
457 #endif
459 // basic-wfilter
460 { "wl_macmode", V_NONE }, // allow, deny, disabled
461 { "wl_maclist", V_LENGTH(0, 18*201) }, // 18 x 200 (11:22:33:44:55:66 ...)
462 { "macnames", V_LENGTH(0, 62*201) }, // 62 (12+1+48+1) x 50 (112233445566<..>) todo: re-use -- zzz
464 // advanced-ctnf
465 { "ct_max", V_RANGE(128, 10240) },
466 { "ct_tcp_timeout", V_LENGTH(20, 70) },
467 { "ct_udp_timeout", V_LENGTH(5, 15) },
468 { "nf_ttl", V_RANGE(-10, 10) },
469 { "nf_l7in", V_01 },
470 { "nf_rtsp", V_01 },
471 { "nf_pptp", V_01 },
472 { "nf_h323", V_01 },
473 { "nf_ftp", V_01 },
475 // advanced-dhcpdns
476 { "dhcpd_slt", V_RANGE(-1, 43200) }, // -1=infinite, 0=follow normal lease time, >=1 custom
477 { "dhcpd_dmdns", V_01 },
478 { "dhcpd_lmax", V_NUM },
479 { "dhcpd_gwmode", V_NUM },
480 { "dns_addget", V_01 },
481 { "dns_intcpt", V_01 },
482 { "dhcpc_minpkt", V_01 },
483 { "dnsmasq_custom", V_TEXT(0, 2048) },
484 // { "dnsmasq_norw", V_01 },
486 // advanced-firewall
487 { "block_wan", V_01 },
488 { "multicast_pass", V_01 },
489 { "block_loopback", V_01 },
490 { "nf_loopback", V_NUM },
491 { "ne_syncookies", V_01 },
493 // advanced-misc
494 { "wait_time", V_RANGE(3, 20) },
495 { "wan_speed", V_RANGE(0, 4) },
497 // advanced-mac
498 { "mac_wan", V_LENGTH(0, 17) },
499 { "mac_wl", V_LENGTH(0, 17) },
501 // advanced-routing
502 { "routes_static", V_LENGTH(0, 2048) },
503 { "lan_stp", V_RANGE(0, 1) },
504 { "wk_mode", V_LENGTH(1, 32) }, // gateway, router
505 #ifdef TCONFIG_ZEBRA
506 { "dr_setting", V_RANGE(0, 3) },
507 { "dr_lan_tx", V_LENGTH(0, 32) },
508 { "dr_lan_rx", V_LENGTH(0, 32) },
509 { "dr_wan_tx", V_LENGTH(0, 32) },
510 { "dr_wan_rx", V_LENGTH(0, 32) },
511 #endif
513 // advanced-wireless
514 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
515 { "wl_auth", V_01 },
516 { "wl_rateset", V_LENGTH(2, 7) }, // all, default, 12
517 { "wl_rate", V_RANGE(0, 54 * 1000 * 1000) },
518 { "wl_mrate", V_RANGE(0, 54 * 1000 * 1000) },
519 { "wl_gmode_protection",V_LENGTH(3, 4) }, // off, auto
520 { "wl_frameburst", V_ONOFF }, // off, on
521 { "wl_bcn", V_RANGE(1, 65535) },
522 { "wl_dtim", V_RANGE(1, 255) },
523 { "wl_frag", V_RANGE(256, 2346) },
524 { "wl_rts", V_RANGE(0, 2347) },
525 { "wl_ap_isolate", V_01 },
526 { "wl_plcphdr", V_LENGTH(4, 5) }, // long, short
527 { "wl_antdiv", V_RANGE(0, 3) },
528 { "wl_txant", V_RANGE(0, 3) },
529 { "wl_txpwr", V_RANGE(0, 255) },
530 { "wl_wme", V_ONOFF }, // off, on
531 { "wl_wme_no_ack", V_ONOFF }, // off, on
532 { "wl_maxassoc", V_RANGE(0, 255) },
533 { "wl_distance", V_LENGTH(0, 5) }, // "", 1-99999
534 { "wlx_hpamp", V_01 },
535 { "wlx_hperx", V_01 },
537 #if TOMATO_N
538 { "wl_nmode_protection",V_WORD, }, // off, auto
539 { "wl_nmcsidx", V_RANGE(-2, 15), }, // -2 - 15
540 #endif
542 // forward-dmz
543 { "dmz_enable", V_01 },
544 { "dmz_ipaddr", V_LENGTH(0, 15) },
545 { "dmz_sip", V_LENGTH(0, 512) },
547 // forward-upnp
548 { "upnp_enable", V_NUM },
549 #ifndef USE_MINIUPNPD
550 { "upnp_mnp", V_01 },
551 // { "upnp_config", V_01 },
552 { "upnp_ssdp_interval", V_RANGE(10, 9999) },
553 { "upnp_max_age", V_RANGE(5, 9999) },
554 #endif
556 // forward-basic
557 { "portforward", V_LENGTH(0, 4096) },
559 // forward-triggered
560 { "trigforward", V_LENGTH(0, 4096) },
563 // access restriction
564 { "rruleN", V_RANGE(0, 49) },
565 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
567 // admin-access
568 { "http_enable", V_01 },
569 { "https_enable", V_01 },
570 { "https_crt_save", V_01 },
571 { "https_crt_cn", V_LENGTH(0, 64) },
572 { "https_crt_gen", V_TEMP },
573 { "remote_management", V_01 },
574 { "remote_mgt_https", V_01 },
575 { "http_lanport", V_PORT },
576 { "https_lanport", V_PORT },
577 { "web_wl_filter", V_01 },
578 // { "web_favicon", V_01 },
579 { "web_css", V_LENGTH(1, 32) },
580 { "http_wanport", V_PORT },
581 { "telnetd_eas", V_01 },
582 { "telnetd_port", V_PORT },
583 { "sshd_eas", V_01 },
584 { "sshd_pass", V_01 },
585 { "sshd_port", V_PORT },
586 { "sshd_remote", V_01 },
587 { "sshd_rport", V_PORT },
588 { "sshd_authkeys", V_TEXT(0, 4096) },
589 { "rmgt_sip", V_LENGTH(0, 512) },
590 { "ne_shlimit", V_TEXT(1, 50) },
592 // admin-bwm
593 { "rstats_enable", V_01 },
594 { "rstats_path", V_LENGTH(0, 48) },
595 { "rstats_stime", V_RANGE(1, 168) },
596 { "rstats_offset", V_RANGE(1, 31) },
597 { "rstats_exclude", V_LENGTH(0, 64) },
598 { "rstats_sshut", V_01 },
599 { "rstats_bak", V_01 },
601 // admin-buttons
602 { "sesx_led", V_RANGE(0, 255) }, // amber, white, aoss
603 { "sesx_b0", V_RANGE(0, 5) }, // 0-5: toggle wireless, reboot, shutdown, script, usb unmount
604 { "sesx_b1", V_RANGE(0, 5) }, // "
605 { "sesx_b2", V_RANGE(0, 5) }, // "
606 { "sesx_b3", V_RANGE(0, 5) }, // "
607 { "sesx_script", V_TEXT(0, 1024) }, //
609 // admin-debug
610 { "debug_nocommit", V_01 },
611 { "debug_cprintf", V_01 },
612 { "debug_cprintf_file", V_01 },
613 // { "debug_keepfiles", V_01 },
614 { "debug_ddns", V_01 },
615 { "debug_norestart", V_TEXT(0, 128) },
616 { "console_loglevel", V_RANGE(1, 8) },
617 { "t_cafree", V_01 },
618 { "t_hidelr", V_01 },
620 // admin-sched
621 { "sch_rboot", V_TEXT(0, 64) },
622 { "sch_rcon", V_TEXT(0, 64) },
623 { "sch_c1", V_TEXT(0, 64) },
624 { "sch_c1_cmd", V_TEXT(0, 2048) },
625 { "sch_c2", V_TEXT(0, 64) },
626 { "sch_c2_cmd", V_TEXT(0, 2048) },
627 { "sch_c3", V_TEXT(0, 64) },
628 { "sch_c3_cmd", V_TEXT(0, 2048) },
630 // admin-scripts
631 { "script_init", V_TEXT(0, 4096) },
632 { "script_shut", V_TEXT(0, 4096) },
633 { "script_fire", V_TEXT(0, 8192) },
634 { "script_wanup", V_TEXT(0, 4096) },
636 // admin-log
637 { "log_remote", V_01 },
638 { "log_remoteip", V_IP },
639 { "log_remoteport", V_PORT },
640 { "log_file", V_01 },
641 { "log_limit", V_RANGE(0, 2400) },
642 { "log_in", V_RANGE(0, 3) },
643 { "log_out", V_RANGE(0, 3) },
644 { "log_mark", V_RANGE(0, 1440) },
645 { "log_events", V_TEXT(0, 32) }, // "acre,crond,ntp"
647 // admin-cifs
648 { "cifs1", V_LENGTH(1, 1024) },
649 { "cifs2", V_LENGTH(1, 1024) },
651 // admin-jffs2
652 { "jffs2_on", V_01 },
653 { "jffs2_exec", V_LENGTH(0, 64) },
654 { "jffs2_format", V_01 },
656 // nas-usb - !!TB
657 #ifdef TCONFIG_USB
658 { "usb_enable", V_01 },
659 { "usb_uhci", V_01 },
660 { "usb_ohci", V_01 },
661 { "usb_usb2", V_01 },
662 { "usb_storage", V_01 },
663 { "usb_printer", V_01 },
664 { "usb_printer_bidirect", V_01 },
665 { "usb_fs_ext3", V_01 },
666 { "usb_fs_fat", V_01 },
667 #ifdef TCONFIG_NTFS
668 { "usb_fs_ntfs", V_01 },
669 #endif
670 { "usb_automount", V_01 },
671 { "script_usbhotplug", V_TEXT(0, 2048) },
672 { "script_usbmount", V_TEXT(0, 2048) },
673 { "script_usbumount", V_TEXT(0, 2048) },
674 #endif
676 // nas-ftp - !!TB
677 #ifdef TCONFIG_FTP
678 { "ftp_enable", V_RANGE(0, 2) },
679 { "ftp_super", V_01 },
680 { "ftp_anonymous", V_RANGE(0, 3) },
681 { "ftp_dirlist", V_RANGE(0, 2) },
682 { "ftp_port", V_PORT },
683 { "ftp_max", V_RANGE(0, 12) },
684 { "ftp_ipmax", V_RANGE(0, 12) },
685 { "ftp_staytimeout", V_RANGE(0, 65535) },
686 { "ftp_rate", V_RANGE(0, 99999) },
687 { "ftp_anonrate", V_RANGE(0, 99999) },
688 { "ftp_anonroot", V_LENGTH(0, 256) },
689 { "ftp_pubroot", V_LENGTH(0, 256) },
690 { "ftp_pvtroot", V_LENGTH(0, 256) },
691 { "ftp_users", V_LENGTH(0, 4096) },
692 { "ftp_custom", V_TEXT(0, 2048) },
693 { "ftp_sip", V_LENGTH(0, 512) },
694 { "ftp_limit", V_TEXT(1, 50) },
695 { "log_ftp", V_01 },
696 #endif
698 #ifdef TCONFIG_SAMBASRV
699 // nas-samba - !!TB
700 { "smbd_enable", V_RANGE(0, 2) },
701 { "smbd_wgroup", V_LENGTH(0, 20) },
702 { "smbd_master", V_01 },
703 { "smbd_wins", V_01 },
704 { "smbd_cpage", V_LENGTH(0, 4) },
705 { "smbd_cset", V_LENGTH(0, 20) },
706 { "smbd_loglevel", V_RANGE(0, 100) },
707 { "smbd_custom", V_TEXT(0, 2048) },
708 { "smbd_autoshare", V_RANGE(0, 3) },
709 { "smbd_shares", V_LENGTH(0, 4096) },
710 { "smbd_user", V_LENGTH(0, 50) },
711 { "smbd_passwd", V_LENGTH(0, 50) },
712 #endif
714 // qos
715 { "qos_enable", V_01 },
716 { "qos_ack", V_01 },
717 { "qos_syn", V_01 },
718 { "qos_fin", V_01 },
719 { "qos_rst", V_01 },
720 { "qos_icmp", V_01 },
721 { "qos_reset", V_01 },
722 { "qos_obw", V_RANGE(10, 999999) },
723 { "qos_ibw", V_RANGE(10, 999999) },
724 { "qos_orules", V_LENGTH(0, 4096) },
725 { "qos_default", V_RANGE(0, 9) },
726 { "qos_irates", V_LENGTH(0, 128) },
727 { "qos_orates", V_LENGTH(0, 128) },
729 { "ne_vegas", V_01 },
730 { "ne_valpha", V_NUM },
731 { "ne_vbeta", V_NUM },
732 { "ne_vgamma", V_NUM },
736 ppp_static 0/1
737 ppp_static_ip IP
738 wl_enable 0/1
739 wl_wds_timeout
740 wl_maxassoc 1-256
741 wl_phytype a,b,g
742 wl_net_reauth
743 wl_preauth
744 wl_wme_ap_bk
745 wl_wme_ap_be
746 wl_wme_ap_vi
747 wl_wme_ap_vo
748 wl_wme_sta_bk
749 wl_wme_sta_be
750 wl_wme_sta_vi
751 wl_wme_sta_vo
753 port_priority_1 0-2
754 port_flow_control_1 0,1
755 port_rate_limit_1 0-8
756 port_priority_2 0-2
757 port_flow_control_2 0,1
758 port_rate_limit_2 0-8
759 port_priority_3 0-2
760 port_flow_control_3 0,1
761 port_rate_limit_3 0-8
762 port_priority_4 0-2
763 port_flow_control_4 0,1
764 port_rate_limit_4 0-8
765 wl_ap_ip
766 wl_ap_ssid
769 { NULL }
772 static int save_variables(int write)
774 const nvset_t *v;
775 char *p, *e;
776 int n;
777 long l;
778 unsigned u[6];
779 int ok;
780 char s[256];
781 int dirty;
782 static const char *msgf = "The field \"%s\" is invalid. Please report this problem.";
784 dirty = 0;
785 for (v = nvset_list; v->name; ++v) {
786 // _dprintf("[%s] %p\n", v->name, webcgi_get((char*)v->name));
787 if ((p = webcgi_get((char*)v->name)) == NULL) continue;
788 ok = 1;
789 switch (v->vtype) {
790 case VT_TEXT:
791 p = unix_string(p); // NOTE: p = malloc'd
792 // drop
793 case VT_LENGTH:
794 n = strlen(p);
795 if ((n < v->va.i) || (n > v->vb.i)) ok = 0;
796 break;
797 case VT_RANGE:
798 l = strtol(p, &e, 10);
799 if ((p == e) || (*e) || (l < v->va.l) || (l > v->vb.l)) ok = 0;
800 break;
801 case VT_IP:
802 if ((sscanf(p, "%3u.%3u.%3u.%3u", &u[0], &u[1], &u[2], &u[3]) != 4) ||
803 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255)) ok = 0;
804 break;
805 case VT_MAC:
806 if ((sscanf(p, "%2x:%2x:%2x:%2x:%2x:%2x", &u[0], &u[1], &u[2], &u[3], &u[4], &u[5]) != 6) ||
807 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255) || (u[4] > 255) || (u[5] > 255)) ok = 0;
808 break;
809 default:
810 // shutup gcc
811 break;
813 if (!ok) {
814 if (v->vtype == VT_TEXT) free(p);
816 sprintf(s, msgf, v->name);
817 resmsg_set(s);
818 return 0;
820 if (write) {
821 if (!nvram_match((char *)v->name, p)) {
822 if (v->vtype != VT_TEMP) dirty = 1;
823 nvram_set(v->name, p);
826 if (v->vtype == VT_TEXT) free(p);
830 // special cases
832 char *p1, *p2;
833 if (((p1 = webcgi_get("set_password_1")) != NULL) && (strcmp(p1, "**********") != 0)) {
834 if (((p2 = webcgi_get("set_password_2")) != NULL) && (strcmp(p1, p2) == 0)) {
835 if ((write) && (!nvram_match("http_passwd", p1))) {
836 dirty = 1;
837 nvram_set("http_passwd", p1);
840 else {
841 sprintf(s, msgf, "password");
842 resmsg_set(s);
843 return 0;
847 for (n = 0; n < 50; ++n) {
848 sprintf(s, "rrule%d", n);
849 if ((p = webcgi_get(s)) != NULL) {
850 if (strlen(p) > 2048) {
851 sprintf(s, msgf, s);
852 resmsg_set(s);
853 return 0;
855 if ((write) && (!nvram_match(s, p))) {
856 dirty = 1;
857 nvram_set(s, p);
862 return (write) ? dirty : 1;
865 static void wo_tomato(char *url)
867 char *v;
868 int i;
869 int ajax;
870 int nvset;
871 const char *red;
872 int commit;
874 // _dprintf("tomato.cgi\n");
876 red = webcgi_safeget("_redirect", "");
877 if (!*red) send_header(200, NULL, mime_html, 0);
879 commit = atoi(webcgi_safeget("_commit", "1"));
880 ajax = atoi(webcgi_safeget("_ajax", "0"));
882 nvset = atoi(webcgi_safeget("_nvset", "1"));
883 if (nvset) {
884 if (!save_variables(0)) {
885 if (ajax) {
886 web_printf("@msg:%s", resmsg_get());
888 else {
889 parse_asp("error.asp");
891 return;
893 commit = save_variables(1) && commit;
895 resmsg_set("Settings saved.");
898 rboot = atoi(webcgi_safeget("_reboot", "0"));
899 if (rboot) {
900 parse_asp("reboot.asp");
902 else {
903 if (ajax) {
904 web_printf("@msg:%s", resmsg_get());
906 else if (atoi(webcgi_safeget("_moveip", "0"))) {
907 parse_asp("saved-moved.asp");
909 else if (!*red) {
910 parse_asp("saved.asp");
914 if (commit) {
915 _dprintf("commit from tomato.cgi\n");
916 nvram_commit_x();
919 if ((v = webcgi_get("_service")) != NULL) {
920 if (!*red) {
921 if (ajax) web_printf(" Some services are being restarted...");
922 web_close();
924 sleep(1);
926 if (*v == '*') {
927 kill(1, SIGHUP);
929 else if (*v != 0) {
930 exec_service(v);
934 for (i = atoi(webcgi_safeget("_sleep", "0")); i > 0; --i) sleep(1);
936 if (*red) redirect(red);
938 if (rboot) {
939 web_close();
940 sleep(1);
941 kill(1, SIGTERM);
946 // ----------------------------------------------------------------------------
949 static void wo_update(char *url)
951 const aspapi_t *api;
952 const char *name;
953 int argc;
954 char *argv[16];
955 char s[32];
957 if ((name = webcgi_get("exec")) != NULL) {
958 for (api = aspapi; api->name; ++api) {
959 if (strcmp(api->name, name) == 0) {
960 for (argc = 0; argc < 16; ++argc) {
961 sprintf(s, "arg%d", argc);
962 if ((argv[argc] = (char *)webcgi_get(s)) == NULL) break;
964 api->exec(argc, argv);
965 break;
971 static void wo_service(char *url)
973 int n;
975 exec_service(webcgi_safeget("_service", ""));
977 if ((n = atoi(webcgi_safeget("_sleep", "2"))) <= 0) n = 2;
978 sleep(n);
980 common_redirect();
983 static void wo_shutdown(char *url)
985 parse_asp("shutdown.asp");
986 web_close();
987 sleep(1);
989 kill(1, SIGQUIT);
992 static void wo_nvcommit(char *url)
994 parse_asp("saved.asp");
995 web_close();
996 nvram_commit();