Tomato 1.26 beta (1786)
[tomato.git] / release / src / router / httpd / tomato.c
blob310f95c27593fb885e33a89cf0f19dbefeca2492
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 },
215 #ifdef BLACKHOLE
216 { "blackhole.cgi", NULL, 0, wi_blackhole, NULL, 1 },
217 #endif
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 },
234 { "dns", asp_dns },
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 },
242 { "nv", asp_nv },
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 },
262 { "css", asp_css },
263 { NULL, NULL }
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)
291 char s[256];
292 char *p;
294 f_read_string(fname, s, sizeof(s));
295 if ((p = strchr(s, '\n')) != NULL) *p = 0;
296 if (s[0]) {
297 resmsg_set(s);
298 return 1;
300 return 0;
303 static void asp_resmsg(int argc, char **argv)
305 char *p;
307 if ((p = js_string(webcgi_safeget("resmsg", (argc > 0) ? argv[0] : ""))) == NULL) return;
308 web_printf("\nresmsg='%s';\n", p);
309 free(p);
312 // ----------------------------------------------------------------------------
314 // verification... simple sanity checks. UI should verify all fields.
316 // todo: move and re-use for filtering - zzz
318 typedef union {
319 int i;
320 long l;
321 const char *s;
322 } nvset_varg_t;
324 typedef struct {
325 const char *name;
326 enum {
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
334 } vtype;
335 nvset_varg_t va;
336 nvset_varg_t vb;
337 } nvset_t;
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[] = {
355 // basic-ident
356 { "router_name", V_LENGTH(0, 32) },
357 { "wan_hostname", V_LENGTH(0, 32) },
358 { "wan_domain", V_LENGTH(0, 32) },
360 // basic-time
361 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
362 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
363 { "tm_dst", V_01 },
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) },
369 // basic-static
370 { "dhcpd_static", V_LENGTH(0, 106*101)}, // 106 (max chars per entry) x 100 entries
372 // basic-ddns
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) },
381 // basic-network
382 // WAN
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, 50) },
391 { "ppp_passwd", V_LENGTH(0, 50) },
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 },
400 // LAN
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 },
413 // wireless
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) },
420 #if TOMATO_N
421 // ! update
422 #endif
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
449 #if TOMATO_N
450 { "wl_nmode", V_NONE },
451 { "wl_nreqd", V_NONE },
452 #endif
454 // basic-wfilter
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
459 // advanced-ctnf
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) },
464 { "nf_l7in", V_01 },
465 { "nf_rtsp", V_01 },
466 { "nf_pptp", V_01 },
467 { "nf_h323", V_01 },
468 { "nf_ftp", V_01 },
470 // advanced-dhcpdns
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 },
481 // advanced-firewall
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 },
488 // advanced-misc
489 { "wait_time", V_RANGE(3, 20) },
490 { "wan_speed", V_RANGE(0, 4) },
492 // advanced-mac
493 { "mac_wan", V_LENGTH(0, 17) },
494 { "mac_wl", V_LENGTH(0, 17) },
496 // advanced-routing
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) },
506 // advanced-wireless
507 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
508 { "wl_auth", V_01 },
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 },
530 #if TOMATO_N
531 { "wl_nmode_protection",V_WORD, }, // off, auto
532 { "wl_nmcsidx", V_RANGE(-2, 15), }, // -2 - 15
533 #endif
535 // forward-dmz
536 { "dmz_enable", V_01 },
537 { "dmz_ipaddr", V_LENGTH(0, 15) },
538 { "dmz_sip", V_LENGTH(0, 512) },
540 // forward-upnp
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) },
547 #endif
549 // forward-basic
550 { "portforward", V_LENGTH(0, 4096) },
552 // forward-triggered
553 { "trigforward", V_LENGTH(0, 4096) },
556 // access restriction
557 { "rruleN", V_RANGE(0, 49) },
558 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
560 // admin-access
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_favicon", V_01 },
572 { "web_css", V_LENGTH(1, 32) },
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) },
585 // admin-bwm
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 },
594 // admin-buttons
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) }, //
602 // admin-debug
603 { "debug_nocommit", V_01 },
604 { "debug_cprintf", V_01 },
605 { "debug_cprintf_file", V_01 },
606 // { "debug_keepfiles", V_01 },
607 { "debug_ddns", V_01 },
608 { "debug_norestart", V_TEXT(0, 128) },
609 { "console_loglevel", V_RANGE(1, 8) },
610 { "t_cafree", V_01 },
611 { "t_hidelr", V_01 },
613 // admin-sched
614 { "sch_rboot", V_TEXT(0, 64) },
615 { "sch_rcon", V_TEXT(0, 64) },
616 { "sch_c1", V_TEXT(0, 64) },
617 { "sch_c1_cmd", V_TEXT(0, 2048) },
618 { "sch_c2", V_TEXT(0, 64) },
619 { "sch_c2_cmd", V_TEXT(0, 2048) },
620 { "sch_c3", V_TEXT(0, 64) },
621 { "sch_c3_cmd", V_TEXT(0, 2048) },
623 // admin-scripts
624 { "script_init", V_TEXT(0, 4096) },
625 { "script_shut", V_TEXT(0, 4096) },
626 { "script_fire", V_TEXT(0, 8192) },
627 { "script_wanup", V_TEXT(0, 4096) },
629 // admin-log
630 { "log_remote", V_01 },
631 { "log_remoteip", V_IP },
632 { "log_remoteport", V_PORT },
633 { "log_file", V_01 },
634 { "log_limit", V_RANGE(0, 2400) },
635 { "log_in", V_RANGE(0, 3) },
636 { "log_out", V_RANGE(0, 3) },
637 { "log_mark", V_RANGE(0, 1440) },
638 { "log_events", V_TEXT(0, 32) }, // "acre,crond,ntp"
640 // admin-cifs
641 { "cifs1", V_LENGTH(1, 1024) },
642 { "cifs2", V_LENGTH(1, 1024) },
644 // admin-jffs2
645 { "jffs2_on", V_01 },
646 { "jffs2_exec", V_LENGTH(0, 64) },
647 { "jffs2_format", V_01 },
649 // qos
650 { "qos_enable", V_01 },
651 { "qos_ack", V_01 },
652 { "qos_syn", V_01 },
653 { "qos_fin", V_01 },
654 { "qos_rst", V_01 },
655 { "qos_icmp", V_01 },
656 { "qos_reset", V_01 },
657 { "qos_obw", V_RANGE(10, 999999) },
658 { "qos_ibw", V_RANGE(10, 999999) },
659 { "qos_orules", V_LENGTH(0, 4096) },
660 { "qos_default", V_RANGE(0, 9) },
661 { "qos_irates", V_LENGTH(0, 128) },
662 { "qos_orates", V_LENGTH(0, 128) },
664 { "ne_vegas", V_01 },
665 { "ne_valpha", V_NUM },
666 { "ne_vbeta", V_NUM },
667 { "ne_vgamma", V_NUM },
671 ppp_static 0/1
672 ppp_static_ip IP
673 wl_enable 0/1
674 wl_wds_timeout
675 wl_maxassoc 1-256
676 wl_phytype a,b,g
677 wl_net_reauth
678 wl_preauth
679 wl_wme_ap_bk
680 wl_wme_ap_be
681 wl_wme_ap_vi
682 wl_wme_ap_vo
683 wl_wme_sta_bk
684 wl_wme_sta_be
685 wl_wme_sta_vi
686 wl_wme_sta_vo
688 port_priority_1 0-2
689 port_flow_control_1 0,1
690 port_rate_limit_1 0-8
691 port_priority_2 0-2
692 port_flow_control_2 0,1
693 port_rate_limit_2 0-8
694 port_priority_3 0-2
695 port_flow_control_3 0,1
696 port_rate_limit_3 0-8
697 port_priority_4 0-2
698 port_flow_control_4 0,1
699 port_rate_limit_4 0-8
700 wl_ap_ip
701 wl_ap_ssid
704 { NULL }
707 static int save_variables(int write)
709 const nvset_t *v;
710 char *p, *e;
711 int n;
712 long l;
713 unsigned u[6];
714 int ok;
715 char s[256];
716 int dirty;
717 static const char *msgf = "The field \"%s\" is invalid. Please report this problem.";
719 dirty = 0;
720 for (v = nvset_list; v->name; ++v) {
721 // _dprintf("[%s] %p\n", v->name, webcgi_get((char*)v->name));
722 if ((p = webcgi_get((char*)v->name)) == NULL) continue;
723 ok = 1;
724 switch (v->vtype) {
725 case VT_TEXT:
726 p = unix_string(p); // NOTE: p = malloc'd
727 // drop
728 case VT_LENGTH:
729 n = strlen(p);
730 if ((n < v->va.i) || (n > v->vb.i)) ok = 0;
731 break;
732 case VT_RANGE:
733 l = strtol(p, &e, 10);
734 if ((p == e) || (*e) || (l < v->va.l) || (l > v->vb.l)) ok = 0;
735 break;
736 case VT_IP:
737 if ((sscanf(p, "%3u.%3u.%3u.%3u", &u[0], &u[1], &u[2], &u[3]) != 4) ||
738 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255)) ok = 0;
739 break;
740 case VT_MAC:
741 if ((sscanf(p, "%2x:%2x:%2x:%2x:%2x:%2x", &u[0], &u[1], &u[2], &u[3], &u[4], &u[5]) != 6) ||
742 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255) || (u[4] > 255) || (u[5] > 255)) ok = 0;
743 break;
744 default:
745 // shutup gcc
746 break;
748 if (!ok) {
749 if (v->vtype == VT_TEXT) free(p);
751 sprintf(s, msgf, v->name);
752 resmsg_set(s);
753 return 0;
755 if (write) {
756 if (!nvram_match((char *)v->name, p)) {
757 if (v->vtype != VT_TEMP) dirty = 1;
758 nvram_set(v->name, p);
761 if (v->vtype == VT_TEXT) free(p);
765 // special cases
767 char *p1, *p2;
768 if (((p1 = webcgi_get("set_password_1")) != NULL) && (strcmp(p1, "**********") != 0)) {
769 if (((p2 = webcgi_get("set_password_2")) != NULL) && (strcmp(p1, p2) == 0)) {
770 if ((write) && (!nvram_match("http_passwd", p1))) {
771 dirty = 1;
772 nvram_set("http_passwd", p1);
775 else {
776 sprintf(s, msgf, "password");
777 resmsg_set(s);
778 return 0;
782 for (n = 0; n < 50; ++n) {
783 sprintf(s, "rrule%d", n);
784 if ((p = webcgi_get(s)) != NULL) {
785 if (strlen(p) > 2048) {
786 sprintf(s, msgf, s);
787 resmsg_set(s);
788 return 0;
790 if ((write) && (!nvram_match(s, p))) {
791 dirty = 1;
792 nvram_set(s, p);
797 return (write) ? dirty : 1;
800 static void wo_tomato(char *url)
802 char *v;
803 int i;
804 int ajax;
805 int nvset;
806 const char *red;
807 int commit;
809 // _dprintf("tomato.cgi\n");
811 red = webcgi_safeget("_redirect", "");
812 if (!*red) send_header(200, NULL, mime_html, 0);
814 commit = atoi(webcgi_safeget("_commit", "1"));
815 ajax = atoi(webcgi_safeget("_ajax", "0"));
817 nvset = atoi(webcgi_safeget("_nvset", "1"));
818 if (nvset) {
819 if (!save_variables(0)) {
820 if (ajax) {
821 web_printf("@msg:%s", resmsg_get());
823 else {
824 parse_asp("error.asp");
826 return;
828 commit = save_variables(1) && commit;
830 resmsg_set("Settings saved.");
833 rboot = atoi(webcgi_safeget("_reboot", "0"));
834 if (rboot) {
835 parse_asp("reboot.asp");
837 else {
838 if (ajax) {
839 web_printf("@msg:%s", resmsg_get());
841 else if (atoi(webcgi_safeget("_moveip", "0"))) {
842 parse_asp("saved-moved.asp");
844 else if (!*red) {
845 parse_asp("saved.asp");
849 if (commit) {
850 _dprintf("commit from tomato.cgi\n");
851 nvram_commit_x();
854 if ((v = webcgi_get("_service")) != NULL) {
855 if (!*red) {
856 if (ajax) web_printf(" Some services are being restarted...");
857 web_close();
859 sleep(1);
861 if (*v == '*') {
862 kill(1, SIGHUP);
864 else if (*v != 0) {
865 exec_service(v);
869 for (i = atoi(webcgi_safeget("_sleep", "0")); i > 0; --i) sleep(1);
871 if (*red) redirect(red);
873 if (rboot) {
874 web_close();
875 sleep(1);
876 kill(1, SIGTERM);
881 // ----------------------------------------------------------------------------
884 static void wo_update(char *url)
886 const aspapi_t *api;
887 const char *name;
888 int argc;
889 char *argv[16];
890 char s[32];
892 if ((name = webcgi_get("exec")) != NULL) {
893 for (api = aspapi; api->name; ++api) {
894 if (strcmp(api->name, name) == 0) {
895 for (argc = 0; argc < 16; ++argc) {
896 sprintf(s, "arg%d", argc);
897 if ((argv[argc] = (char *)webcgi_get(s)) == NULL) break;
899 api->exec(argc, argv);
900 break;
906 static void wo_service(char *url)
908 int n;
910 exec_service(webcgi_safeget("_service", ""));
912 if ((n = atoi(webcgi_safeget("_sleep", "2"))) <= 0) n = 2;
913 sleep(n);
915 common_redirect();
918 static void wo_shutdown(char *url)
920 parse_asp("shutdown.asp");
921 web_close();
922 sleep(1);
924 kill(1, SIGQUIT);
927 static void wo_nvcommit(char *url)
929 parse_asp("saved.asp");
930 web_close();
931 nvram_commit();