rc: Dnsmasq config optimization, allow to specify DNS cache size
[tomato.git] / release / src / router / httpd / tomato.c
blob8222de7fd55292d71c6561b810b3e3cbaee055f9
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2010 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;
129 int fe, fq = -1;
131 if ((fe = mkstemp(webExecFile)) < 0)
132 exit(1);
133 if (query) {
134 if ((fq = mkstemp(webQueryFile)) < 0) {
135 close(fe);
136 unlink(webExecFile);
137 exit(1);
141 if ((f = fdopen(fe, "wb")) != NULL) {
142 fprintf(f,
143 "#!/bin/sh\n"
144 "export REQUEST_METHOD=\"%s\"\n"
145 "export PATH=%s\n"
146 ". /etc/profile\n"
147 "%s%s %s%s\n",
148 post ? "POST" : "GET", getenv("PATH"),
149 command ? "" : "./", command ? command : url,
150 query ? "<" : "", query ? webQueryFile : "");
151 fclose(f);
153 else {
154 unlink(output);
155 close(fe);
156 unlink(webExecFile);
157 if (query) {
158 close(fq);
159 unlink(webQueryFile);
161 exit(1);
163 chmod(webExecFile, 0700);
165 if (query) {
166 if ((f = fdopen(fq, "wb")) != NULL) {
167 fprintf(f, "%s\n", query);
168 fclose(f);
170 else {
171 unlink(output);
172 unlink(webExecFile);
173 close(fq);
174 unlink(webQueryFile);
175 exit(1);
179 char *cmd[] = { webExecFile, NULL };
180 _eval(cmd, output, 0, NULL);
181 unlink(webQueryFile);
182 unlink(webExecFile);
185 static void wo_cgi_bin(char *url)
187 char webOutpFile[] = "/tmp/.woXXXXXX";
188 int fd;
190 if ((fd = mkstemp(webOutpFile)) < 0)
191 exit(1);
192 close(fd);
194 _execute_command(url, NULL, post_buf, webOutpFile);
196 if (post_buf) {
197 free(post_buf);
198 post_buf = NULL;
200 wo_asp(webOutpFile);
201 unlink(webOutpFile);
204 static void wo_shell(char *url)
206 char webOutpFile[] = "/tmp/.woXXXXXX";
207 int fd;
209 if ((fd = mkstemp(webOutpFile)) < 0)
210 exit(1);
211 close(fd);
213 _execute_command(NULL, webcgi_get("command"), NULL, webOutpFile);
215 web_puts("\ncmdresult = '");
216 web_putfile(webOutpFile, WOF_JAVASCRIPT);
217 web_puts("';");
218 unlink(webOutpFile);
221 static void wo_blank(char *url)
223 web_puts("\n\n\n\n");
226 static void wo_favicon(char *url)
228 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
229 do_file(url);
231 if (nvram_match("web_favicon", "1")) {
232 send_header(200, NULL, "image/vnd.microsoft.icon", 0);
233 do_file(url);
235 else {
236 send_error(404, NULL, NULL);
241 static void wo_cfe(char *url)
243 do_file(MTD_DEV(0ro));
246 static void wo_nvram(char *url)
248 web_pipecmd("nvram show", WOF_NONE);
251 static void wo_iptables(char *url)
253 web_pipecmd("iptables -nvL; iptables -t nat -nvL; iptables -t mangle -nvL", WOF_NONE);
257 static void wo_spin(char *url)
259 char s[64];
261 strlcpy(s, nvram_safe_get("web_css"), sizeof(s));
262 strlcat(s, "_spin.gif", sizeof(s));
263 if (f_exists(s)) do_file(s);
264 else do_file("_spin.gif");
268 void common_redirect(void)
270 if (atoi(webcgi_safeget("_ajax", ""))) {
271 send_header(200, NULL, mime_html, 0);
272 web_puts("OK");
274 else {
275 redirect(webcgi_safeget("_redirect", "/"));
279 // ----------------------------------------------------------------------------
281 const struct mime_handler mime_handlers[] = {
282 { "update.cgi", mime_javascript, 0, wi_generic, wo_update, 1 },
283 { "tomato.cgi", NULL, 0, wi_generic, wo_tomato, 1 },
285 { "debug.js", mime_javascript, 5, wi_generic_noid, wo_blank, 1 }, // while debugging
286 { "cfe/*.bin", mime_binary, 0, wi_generic, wo_cfe, 1 },
287 { "nvram/*.txt", mime_binary, 0, wi_generic, wo_nvram, 1 },
288 { "ipt/*.txt", mime_binary, 0, wi_generic, wo_iptables, 1 },
290 { "cfg/*.cfg", NULL, 0, wi_generic, wo_backup, 1 },
291 { "cfg/restore.cgi", mime_html, 0, wi_restore, wo_restore, 1 },
292 { "cfg/defaults.cgi", NULL, 0, wi_generic, wo_defaults, 1 },
294 { "bwm/*.gz", NULL, 0, wi_generic, wo_bwmbackup, 1 },
295 { "bwm/restore.cgi", NULL, 0, wi_bwmrestore, wo_bwmrestore, 1 },
297 { "logs/view.cgi", NULL, 0, wi_generic, wo_viewlog, 1 },
298 { "logs/*.txt", NULL, 0, wi_generic, wo_syslog, 1 },
299 { "webmon_**", NULL, 0, wi_generic, wo_syslog, 1 },
301 { "logout.asp", NULL, 0, wi_generic, wo_asp, 1 },
302 { "clearcookies.asp", NULL, 0, wi_generic, wo_asp, 1 },
304 // { "spin.gif", NULL, 0, wi_generic_noid, wo_spin, 1 },
306 { "**.asp", NULL, 0, wi_generic_noid, wo_asp, 1 },
307 { "**.css", "text/css", 2, wi_generic_noid, do_file, 1 },
308 { "**.htm|**.html", mime_html, 2, wi_generic_noid, do_file, 1 },
309 { "**.gif", "image/gif", 5, wi_generic_noid, do_file, 1 },
310 { "**.jpg", "image/jpeg", 5, wi_generic_noid, do_file, 1 },
311 { "**.png", "image/png", 5, wi_generic_noid, do_file, 1 },
312 { "**.js", mime_javascript, 2, wi_generic_noid, do_file, 1 },
313 { "**.jsx", mime_javascript, 0, wi_generic, wo_asp, 1 },
314 { "**.svg", "image/svg+xml", 2, wi_generic_noid, do_file, 1 },
315 { "**.txt", mime_plain, 2, wi_generic_noid, do_file, 1 },
316 { "**.bin", mime_binary, 0, wi_generic_noid, do_file, 1 },
317 { "**.bino", mime_octetstream, 0, wi_generic_noid, do_file, 1 },
318 { "favicon.ico", NULL, 5, wi_generic_noid, wo_favicon, 1 },
319 // !!TB - CGI Support, enable downloading archives
320 { "**/cgi-bin/**|**.sh", NULL, 0, wi_cgi_bin, wo_cgi_bin, 1 },
321 { "**.tar|**.gz", mime_binary, 0, wi_generic_noid, do_file, 1 },
322 { "shell.cgi", mime_javascript, 0, wi_generic, wo_shell, 1 },
323 { "wpad.dat|proxy.pac", "application/x-ns-proxy-autoconfig", 0, wi_generic_noid, do_file, 0 },
325 { "webmon.cgi", mime_javascript, 0, wi_generic, wo_webmon, 1 },
326 { "dhcpc.cgi", NULL, 0, wi_generic, wo_dhcpc, 1 },
327 { "dhcpd.cgi", mime_javascript, 0, wi_generic, wo_dhcpd, 1 },
328 { "nvcommit.cgi", NULL, 0, wi_generic, wo_nvcommit, 1 },
329 { "ping.cgi", mime_javascript, 0, wi_generic, wo_ping, 1 },
330 { "trace.cgi", mime_javascript, 0, wi_generic, wo_trace, 1 },
331 { "upgrade.cgi", mime_html, 0, wi_upgrade, wo_flash, 1 },
332 { "upnp.cgi", NULL, 0, wi_generic, wo_upnp, 1 },
333 { "wakeup.cgi", NULL, 0, wi_generic, wo_wakeup, 1 },
334 { "wlmnoise.cgi", mime_html, 0, wi_generic, wo_wlmnoise, 1 },
335 { "wlradio.cgi", NULL, 0, wi_generic, wo_wlradio, 1 },
336 { "resolve.cgi", mime_javascript, 0, wi_generic, wo_resolve, 1 },
337 { "expct.cgi", mime_html, 0, wi_generic, wo_expct, 1 },
338 { "service.cgi", NULL, 0, wi_generic, wo_service, 1 },
339 // { "logout.cgi", NULL, 0, wi_generic, wo_logout, 0 }, // see httpd.c
340 { "shutdown.cgi", mime_html, 0, wi_generic, wo_shutdown, 1 },
341 #ifdef TCONFIG_OPENVPN
342 { "vpnstatus.cgi", mime_javascript, 0, wi_generic, wo_vpn_status, 1 },
343 #endif
344 #ifdef TCONFIG_USB
345 { "usbcmd.cgi", mime_javascript, 0, wi_generic, wo_usbcommand, 1 }, //!!TB - USB
346 #endif
347 #ifdef BLACKHOLE
348 { "blackhole.cgi", NULL, 0, wi_blackhole, NULL, 1 },
349 #endif
350 // { "test", mime_html, 0, wi_generic, wo_test, 1 },
351 { NULL, NULL, 0, NULL, NULL, 1 }
354 const aspapi_t aspapi[] = {
355 { "activeroutes", asp_activeroutes },
356 { "arplist", asp_arplist },
357 { "bandwidth", asp_bandwidth },
358 { "build_time", asp_build_time },
359 { "cgi_get", asp_cgi_get },
360 { "compmac", asp_compmac },
361 { "ctcount", asp_ctcount },
362 { "ctdump", asp_ctdump },
363 { "ddnsx", asp_ddnsx },
364 { "devlist", asp_devlist },
365 { "webmon", asp_webmon },
366 { "dhcpc_time", asp_dhcpc_time },
367 { "dns", asp_dns },
368 { "ident", asp_ident },
369 { "lanip", asp_lanip },
370 { "layer7", asp_layer7 },
371 { "link_uptime", asp_link_uptime },
372 { "lipp", asp_lipp },
373 { "netdev", asp_netdev },
374 { "notice", asp_notice },
375 { "nv", asp_nv },
376 { "nvram", asp_nvram },
377 { "nvramseq", asp_nvramseq },
378 { "nvstat", asp_nvstat },
379 { "psup", asp_psup },
380 { "qrate", asp_qrate },
381 { "resmsg", asp_resmsg },
382 { "rrule", asp_rrule },
383 { "statfs", asp_statfs },
384 { "sysinfo", asp_sysinfo },
385 { "time", asp_time },
386 { "upnpinfo", asp_upnpinfo },
387 { "version", asp_version },
388 { "wanstatus", asp_wanstatus },
389 { "wanup", asp_wanup },
390 { "wlchannel", asp_wlchannel },
391 { "wlclient", asp_wlclient },
392 { "wlcrssi", asp_wlcrssi },
393 { "wlnoise", asp_wlnoise },
394 { "wlnbw", asp_wlnbw },
395 { "wlnctrlsb", asp_wlnctrlsb },
396 { "wlradio", asp_wlradio },
397 { "wlscan", asp_wlscan },
398 { "wlchannels", asp_wlchannels }, //!!TB
399 { "wlcountries", asp_wlcountries },
400 { "wlrate", asp_wlrate },
401 { "wlbands", asp_wlbands },
402 #ifdef TCONFIG_USB
403 { "usbdevices", asp_usbdevices }, //!!TB - USB Support
404 #endif
405 { "css", asp_css },
406 { NULL, NULL }
409 // -----------------------------------------------------------------------------
411 static void asp_css(int argc, char **argv)
413 const char *css = nvram_safe_get("web_css");
415 if (strcmp(css, "tomato") != 0) {
416 web_printf("<link rel='stylesheet' type='text/css' href='%s.css'>", css);
420 // -----------------------------------------------------------------------------
422 const char *resmsg_get(void)
424 return webcgi_safeget("resmsg", "");
427 void resmsg_set(const char *msg)
429 webcgi_set("resmsg", strdup(msg)); // m ok
432 int resmsg_fread(const char *fname)
434 char s[256];
435 char *p;
437 f_read_string(fname, s, sizeof(s));
438 if ((p = strchr(s, '\n')) != NULL) *p = 0;
439 if (s[0]) {
440 resmsg_set(s);
441 return 1;
443 return 0;
446 static void asp_resmsg(int argc, char **argv)
448 char *p;
450 if ((p = js_string(webcgi_safeget("resmsg", (argc > 0) ? argv[0] : ""))) == NULL) return;
451 web_printf("\nresmsg='%s';\n", p);
452 free(p);
455 // ----------------------------------------------------------------------------
457 // verification... simple sanity checks. UI should verify all fields.
459 // todo: move and re-use for filtering - zzz
461 typedef union {
462 int i;
463 long l;
464 const char *s;
465 } nvset_varg_t;
467 typedef struct {
468 const char *name;
469 enum {
470 VT_NONE, // no checking
471 VT_LENGTH, // check length of string
472 VT_TEXT, // strip \r, check length of string
473 VT_RANGE, // expect an integer, check range
474 VT_IP, // expect an ip address
475 VT_MAC, // expect a mac address
476 VT_TEMP // no checks, no commit
477 } vtype;
478 nvset_varg_t va;
479 nvset_varg_t vb;
480 } nvset_t;
483 #define V_NONE VT_NONE, { }, { }
484 #define V_01 VT_RANGE, { .l = 0 }, { .l = 1 }
485 #define V_PORT VT_RANGE, { .l = 2 }, { .l = 65535 }
486 #define V_ONOFF VT_LENGTH, { .i = 2 }, { .i = 3 }
487 #define V_WORD VT_LENGTH, { .i = 1 }, { .i = 16 }
488 #define V_LENGTH(min, max) VT_LENGTH, { .i = min }, { .i = max }
489 #define V_TEXT(min, max) VT_TEXT, { .i = min }, { .i = max }
490 #define V_RANGE(min, max) VT_RANGE, { .l = min }, { .l = max }
491 #define V_IP VT_IP, { }, { }
492 #define V_OCTET VT_RANGE, { .l = 0 }, { .l = 255 }
493 #define V_NUM VT_RANGE, { .l = 0 }, { .l = 0x7FFFFFFF }
494 #define V_TEMP VT_TEMP, { }, { }
496 static const nvset_t nvset_list[] = {
498 // basic-ident
499 { "router_name", V_LENGTH(0, 32) },
500 { "wan_hostname", V_LENGTH(0, 32) },
501 { "wan_domain", V_LENGTH(0, 32) },
503 // basic-time
504 { "tm_tz", V_LENGTH(1, 64) }, // PST8PDT
505 { "tm_sel", V_LENGTH(1, 64) }, // PST8PDT
506 { "tm_dst", V_01 },
507 { "ntp_updates", V_RANGE(-1, 24) },
508 { "ntp_tdod", V_01 },
509 { "ntp_server", V_LENGTH(1, 150) }, // x y z
510 { "ntp_kiss", V_LENGTH(0, 255) },
512 // basic-static
513 { "dhcpd_static", V_LENGTH(0, 106*101)}, // 106 (max chars per entry) x 100 entries
515 // basic-ddns
516 { "ddnsx0", V_LENGTH(0, 2048) },
517 { "ddnsx1", V_LENGTH(0, 2048) },
518 { "ddnsx0_cache", V_LENGTH(0, 1) }, // only to clear
519 { "ddnsx1_cache", V_LENGTH(0, 1) },
520 { "ddnsx_ip", V_LENGTH(0, 32) },
521 { "ddnsx_save", V_01 },
522 { "ddnsx_refresh", V_RANGE(0, 365) },
524 // basic-network
525 // WAN
526 { "wan_proto", V_LENGTH(1, 16) }, // disabled, dhcp, static, pppoe, pptp, l2tp
527 { "wan_ipaddr", V_IP },
528 { "wan_netmask", V_IP },
529 { "wan_gateway", V_IP },
530 { "hb_server_ip", V_LENGTH(0, 32) },
531 { "l2tp_server_ip", V_LENGTH(0, 128) },
532 { "pptp_server_ip", V_LENGTH(0, 128) },
533 { "pptp_dhcp", V_01 },
534 { "ppp_defgw", V_01 },
535 { "ppp_username", V_LENGTH(0, 60) },
536 { "ppp_passwd", V_LENGTH(0, 60) },
537 { "ppp_service", V_LENGTH(0, 50) },
538 { "ppp_demand", V_01 },
539 { "ppp_custom", V_LENGTH(0, 256) },
540 { "ppp_idletime", V_RANGE(0, 1440) },
541 { "ppp_redialperiod", V_RANGE(1, 86400) },
542 { "mtu_enable", V_01 },
543 { "wan_mtu", V_RANGE(576, 1500) },
544 { "wan_islan", V_01 },
546 // LAN
547 { "lan_ipaddr", V_IP },
548 { "lan_netmask", V_IP },
549 { "lan_gateway", V_IP },
550 { "wan_dns", V_LENGTH(0, 50) }, // ip ip ip
551 { "lan_proto", V_WORD }, // static, dhcp
552 { "dhcp_start", V_RANGE(1, 254) }, // remove !
553 { "dhcp_num", V_RANGE(1, 255) }, // remove !
554 { "dhcpd_startip", V_IP },
555 { "dhcpd_endip", V_IP },
556 { "dhcp_lease", V_RANGE(1, 10080) },
557 { "wan_wins", V_IP },
559 // wireless
560 { "wl_radio", V_01 },
561 { "wl_mode", V_LENGTH(2, 3) }, // ap, sta, wet, wds
562 { "wl_net_mode", V_LENGTH(5, 8) }, // disabled, mixed, b-only, g-only, bg-mixed, n-only [speedbooster]
563 { "wl_ssid", V_LENGTH(1, 32) },
564 { "wl_closed", V_01 },
565 { "wl_channel", V_RANGE(0, 216) },
567 { "security_mode2", V_LENGTH(1, 32) }, // disabled, radius, wep, wpa_personal, wpa_enterprise, wpa2_personal, wpa2_enterprise
568 { "wl_radius_ipaddr", V_IP },
569 { "wl_radius_port", V_PORT },
570 { "wl_radius_key", V_LENGTH(1, 64) },
571 { "wl_wep_bit", V_RANGE(64, 128) }, // 64 or 128
572 { "wl_passphrase", V_LENGTH(0, 20) },
573 { "wl_key", V_RANGE(1, 4) },
574 { "wl_key1", V_LENGTH(0, 26) },
575 { "wl_key2", V_LENGTH(0, 26) },
576 { "wl_key3", V_LENGTH(0, 26) },
577 { "wl_key4", V_LENGTH(0, 26) },
578 { "wl_crypto", V_LENGTH(3, 8) }, // tkip, aes, tkip+aes
579 { "wl_wpa_psk", V_LENGTH(8, 64) },
580 { "wl_wpa_gtk_rekey", V_RANGE(60, 7200) },
582 { "wl_lazywds", V_01 },
583 { "wl_wds", V_LENGTH(0, 180) }, // mac mac mac (x 10)
585 { "security_mode", V_LENGTH(1, 32) }, // disabled, radius, wpa, psk,wep, wpa2, psk2, wpa wpa2, psk psk2
586 { "wds_enable", V_01 },
587 { "wl_gmode", V_RANGE(-1, 6) },
588 { "wl_wep", V_LENGTH(1, 32) }, // off, on, restricted,tkip,aes,tkip+aes
589 { "wl_akm", V_LENGTH(0, 32) }, // wpa, wpa2, psk, psk2, wpa wpa2, psk psk2, ""
590 { "wl_auth_mode", V_LENGTH(4, 6) }, // none, radius
592 { "wl_nmode", V_NONE },
593 { "wl_nband", V_RANGE(0, 2) }, // 2 - 2.4GHz, 1 - 5GHz, 0 - Auto
594 { "wl_nreqd", V_NONE },
595 { "wl_nbw_cap", V_RANGE(0, 2) }, // 0 - 20MHz, 1 - 40MHz, 2 - Auto
596 { "wl_nbw", V_NONE },
597 { "wl_mimo_preamble", V_WORD }, // 802.11n Preamble: mm/gf/auto/gfbcm
598 { "wl_nctrlsb", V_NONE }, // none, lower, upper
600 // basic-wfilter
601 { "wl_macmode", V_NONE }, // allow, deny, disabled
602 { "wl_maclist", V_LENGTH(0, 18*201) }, // 18 x 200 (11:22:33:44:55:66 ...)
603 { "macnames", V_LENGTH(0, 62*201) }, // 62 (12+1+48+1) x 50 (112233445566<..>) todo: re-use -- zzz
605 // advanced-ctnf
606 { "ct_max", V_RANGE(128, 300000) },
607 { "ct_tcp_timeout", V_LENGTH(20, 70) },
608 { "ct_udp_timeout", V_LENGTH(5, 15) },
609 { "ct_timeout", V_LENGTH(5, 15) },
610 { "nf_ttl", V_LENGTH(1, 6) },
611 { "nf_l7in", V_01 },
612 #ifdef LINUX26
613 { "nf_sip", V_01 },
614 { "ct_hashsize", V_RANGE(127, 65535) },
615 #endif
616 { "nf_rtsp", V_01 },
617 { "nf_pptp", V_01 },
618 { "nf_h323", V_01 },
619 { "nf_ftp", V_01 },
621 // advanced-dhcpdns
622 { "dhcpd_slt", V_RANGE(-1, 43200) }, // -1=infinite, 0=follow normal lease time, >=1 custom
623 { "dhcpd_dmdns", V_01 },
624 { "dhcpd_lmax", V_NUM },
625 { "dhcpd_gwmode", V_NUM },
626 { "dns_addget", V_01 },
627 { "dns_intcpt", V_01 },
628 { "dns_cache", V_NUM },
629 { "dhcpc_minpkt", V_01 },
630 { "dhcpc_vendorclass", V_LENGTH(0, 80) },
631 { "dhcpc_requestip", V_LENGTH(0, 16) },
632 { "dns_norebind", V_01 },
633 { "dnsmasq_custom", V_TEXT(0, 2048) },
634 // { "dnsmasq_norw", V_01 },
636 // advanced-firewall
637 { "block_wan", V_01 },
638 { "multicast_pass", V_01 },
639 { "block_loopback", V_01 },
640 { "nf_loopback", V_NUM },
641 { "ne_syncookies", V_01 },
642 { "dhcp_pass", V_01 },
644 // advanced-misc
645 { "wait_time", V_RANGE(3, 20) },
646 { "wan_speed", V_RANGE(0, 4) },
647 { "jumbo_frame_enable", V_01 }, // Jumbo Frames support (for RT-N16/WNR3500L)
648 { "jumbo_frame_size", V_RANGE(1, 9720) },
650 // advanced-mac
651 { "mac_wan", V_LENGTH(0, 17) },
652 { "mac_wl", V_LENGTH(0, 17) },
654 // advanced-routing
655 { "routes_static", V_LENGTH(0, 2048) },
656 { "dhcp_routes", V_01 },
657 { "lan_stp", V_RANGE(0, 1) },
658 { "wk_mode", V_LENGTH(1, 32) }, // gateway, router
659 #ifdef TCONFIG_ZEBRA
660 { "dr_setting", V_RANGE(0, 3) },
661 { "dr_lan_tx", V_LENGTH(0, 32) },
662 { "dr_lan_rx", V_LENGTH(0, 32) },
663 { "dr_wan_tx", V_LENGTH(0, 32) },
664 { "dr_wan_rx", V_LENGTH(0, 32) },
665 #endif
667 // advanced-wireless
668 { "wl_country", V_LENGTH(0, 64) }, // !!TB - Country code
669 { "wl_country_code", V_LENGTH(0, 4) }, // !!TB - Country code
670 { "wl_btc_mode", V_RANGE(0, 2) }, // !!TB - BT Coexistence Mode: 0 (disable), 1 (enable), 2 (preemption)
671 { "wl_afterburner", V_LENGTH(2, 4) }, // off, on, auto
672 { "wl_auth", V_01 },
673 { "wl_rateset", V_LENGTH(2, 7) }, // all, default, 12
674 { "wl_rate", V_RANGE(0, 54 * 1000 * 1000) },
675 { "wl_mrate", V_RANGE(0, 54 * 1000 * 1000) },
676 { "wl_gmode_protection",V_LENGTH(3, 4) }, // off, auto
677 { "wl_frameburst", V_ONOFF }, // off, on
678 { "wl_bcn", V_RANGE(1, 65535) },
679 { "wl_dtim", V_RANGE(1, 255) },
680 { "wl_frag", V_RANGE(256, 2346) },
681 { "wl_rts", V_RANGE(0, 2347) },
682 { "wl_ap_isolate", V_01 },
683 { "wl_plcphdr", V_LENGTH(4, 5) }, // long, short
684 { "wl_antdiv", V_RANGE(0, 3) },
685 { "wl_txant", V_RANGE(0, 3) },
686 { "wl_txpwr", V_RANGE(0, 400) },
687 { "wl_wme", V_WORD }, // auto, off, on
688 { "wl_wme_no_ack", V_ONOFF }, // off, on
689 { "wl_wme_apsd", V_ONOFF }, // off, on
690 { "wl_maxassoc", V_RANGE(0, 255) },
691 { "wl_distance", V_LENGTH(0, 5) }, // "", 1-99999
692 { "wlx_hpamp", V_01 },
693 { "wlx_hperx", V_01 },
694 { "wl_reg_mode", V_LENGTH(1, 3) }, // !!TB - Regulatory: off, h, d
695 { "wl_interfmode", V_RANGE(0, 3) }, // Interference Mitigation Mode (0|1|2|3)
697 { "wl_nmode_protection", V_WORD, }, // off, auto
698 { "wl_nmcsidx", V_RANGE(-2, 32), }, // -2 - 32
699 { "wl_obss_coex", V_01 },
701 // forward-dmz
702 { "dmz_enable", V_01 },
703 { "dmz_ipaddr", V_LENGTH(0, 15) },
704 { "dmz_sip", V_LENGTH(0, 512) },
706 // forward-upnp
707 { "upnp_enable", V_NUM },
708 { "upnp_secure", V_01 },
709 { "upnp_port", V_RANGE(0, 65535) },
710 { "upnp_ssdp_interval", V_RANGE(10, 9999) },
711 { "upnp_mnp", V_01 },
712 { "upnp_clean", V_01 },
713 { "upnp_clean_interval", V_RANGE(60, 65535) },
714 { "upnp_clean_threshold", V_RANGE(0, 9999) },
715 { "upnp_min_port_int", V_PORT },
716 { "upnp_max_port_int", V_PORT },
717 { "upnp_min_port_ext", V_PORT },
718 { "upnp_max_port_ext", V_PORT },
719 #ifndef USE_MINIUPNPD
720 // { "upnp_config", V_01 },
721 { "upnp_max_age", V_RANGE(5, 9999) },
722 #endif
724 // forward-basic
725 { "portforward", V_LENGTH(0, 4096) },
727 // forward-triggered
728 { "trigforward", V_LENGTH(0, 4096) },
731 // access restriction
732 { "rruleN", V_RANGE(0, 49) },
733 // { "rrule##", V_LENGTH(0, 2048) }, // in save_variables()
735 // admin-access
736 { "http_enable", V_01 },
737 { "https_enable", V_01 },
738 { "https_crt_save", V_01 },
739 { "https_crt_cn", V_LENGTH(0, 64) },
740 { "https_crt_gen", V_TEMP },
741 { "remote_management", V_01 },
742 { "remote_mgt_https", V_01 },
743 { "http_lanport", V_PORT },
744 { "https_lanport", V_PORT },
745 { "web_wl_filter", V_01 },
746 { "web_css", V_LENGTH(1, 32) },
747 { "web_mx", V_LENGTH(0, 128) },
748 { "http_wanport", V_PORT },
749 { "telnetd_eas", V_01 },
750 { "telnetd_port", V_PORT },
751 { "sshd_eas", V_01 },
752 { "sshd_pass", V_01 },
753 { "sshd_port", V_PORT },
754 { "sshd_remote", V_01 },
755 { "sshd_forwarding", V_01 },
756 { "sshd_rport", V_PORT },
757 { "sshd_authkeys", V_TEXT(0, 4096) },
758 { "rmgt_sip", V_LENGTH(0, 512) },
759 { "ne_shlimit", V_TEXT(1, 50) },
761 // admin-bwm
762 { "rstats_enable", V_01 },
763 { "rstats_path", V_LENGTH(0, 48) },
764 { "rstats_stime", V_RANGE(1, 168) },
765 { "rstats_offset", V_RANGE(1, 31) },
766 { "rstats_exclude", V_LENGTH(0, 64) },
767 { "rstats_sshut", V_01 },
768 { "rstats_bak", V_01 },
770 // admin-buttons
771 { "sesx_led", V_RANGE(0, 255) }, // amber, white, aoss
772 { "sesx_b0", V_RANGE(0, 5) }, // 0-5: toggle wireless, reboot, shutdown, script, usb unmount
773 { "sesx_b1", V_RANGE(0, 5) }, // "
774 { "sesx_b2", V_RANGE(0, 5) }, // "
775 { "sesx_b3", V_RANGE(0, 5) }, // "
776 { "sesx_script", V_TEXT(0, 1024) }, //
777 { "script_brau", V_TEXT(0, 1024) }, //
779 // admin-debug
780 { "debug_nocommit", V_01 },
781 { "debug_cprintf", V_01 },
782 { "debug_cprintf_file", V_01 },
783 // { "debug_keepfiles", V_01 },
784 { "debug_ddns", V_01 },
785 { "debug_norestart", V_TEXT(0, 128) },
786 { "console_loglevel", V_RANGE(1, 8) },
787 { "t_cafree", V_01 },
788 { "t_hidelr", V_01 },
790 // admin-sched
791 { "sch_rboot", V_TEXT(0, 64) },
792 { "sch_rcon", V_TEXT(0, 64) },
793 { "sch_c1", V_TEXT(0, 64) },
794 { "sch_c1_cmd", V_TEXT(0, 2048) },
795 { "sch_c2", V_TEXT(0, 64) },
796 { "sch_c2_cmd", V_TEXT(0, 2048) },
797 { "sch_c3", V_TEXT(0, 64) },
798 { "sch_c3_cmd", V_TEXT(0, 2048) },
800 // admin-scripts
801 { "script_init", V_TEXT(0, 4096) },
802 { "script_shut", V_TEXT(0, 4096) },
803 { "script_fire", V_TEXT(0, 8192) },
804 { "script_wanup", V_TEXT(0, 4096) },
806 // admin-log
807 { "log_remote", V_01 },
808 { "log_remoteip", V_LENGTH(0, 512) },
809 { "log_remoteport", V_PORT },
810 { "log_file", V_01 },
811 { "log_limit", V_RANGE(0, 2400) },
812 { "log_in", V_RANGE(0, 3) },
813 { "log_out", V_RANGE(0, 3) },
814 { "log_mark", V_RANGE(0, 99999) },
815 { "log_events", V_TEXT(0, 32) }, // "acre,crond,ntp"
817 // admin-log-webmonitor
818 { "log_wm", V_01 },
819 { "log_wmtype", V_RANGE(0, 2) },
820 { "log_wmip", V_LENGTH(0, 512) },
821 { "log_wmdmax", V_RANGE(0, 9999) },
822 { "log_wmsmax", V_RANGE(0, 9999) },
824 // admin-cifs
825 { "cifs1", V_LENGTH(1, 1024) },
826 { "cifs2", V_LENGTH(1, 1024) },
828 // admin-jffs2
829 { "jffs2_on", V_01 },
830 { "jffs2_exec", V_LENGTH(0, 64) },
831 { "jffs2_format", V_01 },
833 // nas-usb - !!TB
834 #ifdef TCONFIG_USB
835 { "usb_enable", V_01 },
836 { "usb_uhci", V_RANGE(-1, 1) }, // -1 - disabled, 0 - off, 1 - on
837 { "usb_ohci", V_RANGE(-1, 1) },
838 { "usb_usb2", V_RANGE(-1, 1) },
839 { "usb_irq_thresh", V_RANGE(0, 6) },
840 { "usb_storage", V_01 },
841 { "usb_printer", V_01 },
842 { "usb_printer_bidirect", V_01 },
843 { "usb_fs_ext3", V_01 },
844 { "usb_fs_fat", V_01 },
845 #ifdef TCONFIG_NTFS
846 { "usb_fs_ntfs", V_01 },
847 #endif
848 { "usb_automount", V_01 },
849 { "script_usbhotplug", V_TEXT(0, 2048) },
850 { "script_usbmount", V_TEXT(0, 2048) },
851 { "script_usbumount", V_TEXT(0, 2048) },
852 #endif
854 // nas-ftp - !!TB
855 #ifdef TCONFIG_FTP
856 { "ftp_enable", V_RANGE(0, 2) },
857 { "ftp_super", V_01 },
858 { "ftp_anonymous", V_RANGE(0, 3) },
859 { "ftp_dirlist", V_RANGE(0, 2) },
860 { "ftp_port", V_PORT },
861 { "ftp_max", V_RANGE(0, 12) },
862 { "ftp_ipmax", V_RANGE(0, 12) },
863 { "ftp_staytimeout", V_RANGE(0, 65535) },
864 { "ftp_rate", V_RANGE(0, 99999) },
865 { "ftp_anonrate", V_RANGE(0, 99999) },
866 { "ftp_anonroot", V_LENGTH(0, 256) },
867 { "ftp_pubroot", V_LENGTH(0, 256) },
868 { "ftp_pvtroot", V_LENGTH(0, 256) },
869 { "ftp_users", V_LENGTH(0, 4096) },
870 { "ftp_custom", V_TEXT(0, 2048) },
871 { "ftp_sip", V_LENGTH(0, 512) },
872 { "ftp_limit", V_TEXT(1, 50) },
873 { "log_ftp", V_01 },
874 #endif
876 #ifdef TCONFIG_SAMBASRV
877 // nas-samba - !!TB
878 { "smbd_enable", V_RANGE(0, 2) },
879 { "smbd_wgroup", V_LENGTH(0, 20) },
880 { "smbd_master", V_01 },
881 { "smbd_wins", V_01 },
882 { "smbd_cpage", V_LENGTH(0, 4) },
883 { "smbd_cset", V_LENGTH(0, 20) },
884 { "smbd_custom", V_TEXT(0, 2048) },
885 { "smbd_autoshare", V_RANGE(0, 3) },
886 { "smbd_shares", V_LENGTH(0, 4096) },
887 { "smbd_user", V_LENGTH(0, 50) },
888 { "smbd_passwd", V_LENGTH(0, 50) },
889 #endif
891 #ifdef TCONFIG_MEDIA_SERVER
892 // nas-media
893 { "ms_enable", V_01 },
894 { "ms_dirs", V_LENGTH(0, 1024) },
895 { "ms_port", V_RANGE(0, 65535) },
896 { "ms_dbdir", V_LENGTH(0, 256) },
897 { "ms_tivo", V_01 },
898 { "ms_stdlna", V_01 },
899 { "ms_rescan", V_01 },
900 { "ms_sas", V_01 },
901 #endif
903 // qos
904 { "qos_enable", V_01 },
905 { "qos_ack", V_01 },
906 { "qos_syn", V_01 },
907 { "qos_fin", V_01 },
908 { "qos_rst", V_01 },
909 { "qos_icmp", V_01 },
910 { "qos_reset", V_01 },
911 { "qos_pfifo", V_01 }, // !!TB
912 { "qos_obw", V_RANGE(10, 999999) },
913 { "qos_ibw", V_RANGE(10, 999999) },
914 { "qos_orules", V_LENGTH(0, 4096) },
915 { "qos_default", V_RANGE(0, 9) },
916 { "qos_irates", V_LENGTH(0, 128) },
917 { "qos_orates", V_LENGTH(0, 128) },
919 { "ne_vegas", V_01 },
920 { "ne_valpha", V_NUM },
921 { "ne_vbeta", V_NUM },
922 { "ne_vgamma", V_NUM },
924 #ifdef TCONFIG_OPENVPN
925 // vpn
926 { "vpn_debug", V_01 },
927 { "vpn_server_eas", V_NONE },
928 { "vpn_server_dns", V_NONE },
929 { "vpn_server1_poll", V_RANGE(0, 1440) },
930 { "vpn_server1_if", V_TEXT(3, 3) }, // tap, tun
931 { "vpn_server1_proto", V_TEXT(3, 10) }, // udp, tcp-server
932 { "vpn_server1_port", V_PORT },
933 { "vpn_server1_firewall", V_TEXT(0, 8) }, // auto, external, custom
934 { "vpn_server1_crypt", V_TEXT(0, 6) }, // tls, secret, custom
935 { "vpn_server1_comp", V_TEXT(0, 8) }, // yes, no, adaptive
936 { "vpn_server1_cipher", V_TEXT(0, 16) },
937 { "vpn_server1_dhcp", V_01 },
938 { "vpn_server1_r1", V_IP },
939 { "vpn_server1_r2", V_IP },
940 { "vpn_server1_sn", V_IP },
941 { "vpn_server1_nm", V_IP },
942 { "vpn_server1_local", V_IP },
943 { "vpn_server1_remote", V_IP },
944 { "vpn_server1_reneg", V_RANGE(-1,2147483647)},
945 { "vpn_server1_hmac", V_RANGE(-1, 2) },
946 { "vpn_server1_plan", V_01 },
947 { "vpn_server1_ccd", V_01 },
948 { "vpn_server1_c2c", V_01 },
949 { "vpn_server1_ccd_excl", V_01 },
950 { "vpn_server1_ccd_val", V_NONE },
951 { "vpn_server1_pdns", V_01 },
952 { "vpn_server1_rgw", V_01 },
953 { "vpn_server1_custom", V_NONE },
954 { "vpn_server1_static", V_NONE },
955 { "vpn_server1_ca", V_NONE },
956 { "vpn_server1_crt", V_NONE },
957 { "vpn_server1_key", V_NONE },
958 { "vpn_server1_dh", V_NONE },
959 { "vpn_server2_poll", V_RANGE(0, 1440) },
960 { "vpn_server2_if", V_TEXT(3, 3) }, // tap, tun
961 { "vpn_server2_proto", V_TEXT(3, 10) }, // udp, tcp-server
962 { "vpn_server2_port", V_PORT },
963 { "vpn_server2_firewall", V_TEXT(0, 8) }, // auto, external, custom
964 { "vpn_server2_crypt", V_TEXT(0, 6) }, // tls, secret, custom
965 { "vpn_server2_comp", V_TEXT(0, 8) }, // yes, no, adaptive
966 { "vpn_server2_cipher", V_TEXT(0, 16) },
967 { "vpn_server2_dhcp", V_01 },
968 { "vpn_server2_r1", V_IP },
969 { "vpn_server2_r2", V_IP },
970 { "vpn_server2_sn", V_IP },
971 { "vpn_server2_nm", V_IP },
972 { "vpn_server2_local", V_IP },
973 { "vpn_server2_remote", V_IP },
974 { "vpn_server2_reneg", V_RANGE(-1,2147483647)},
975 { "vpn_server2_hmac", V_RANGE(-1, 2) },
976 { "vpn_server2_plan", V_01 },
977 { "vpn_server2_pdns", V_01 },
978 { "vpn_server2_rgw", V_01 },
979 { "vpn_server2_custom", V_NONE },
980 { "vpn_server2_ccd", V_01 },
981 { "vpn_server2_c2c", V_01 },
982 { "vpn_server2_ccd_excl", V_01 },
983 { "vpn_server2_ccd_val", V_NONE },
984 { "vpn_server2_static", V_NONE },
985 { "vpn_server2_ca", V_NONE },
986 { "vpn_server2_crt", V_NONE },
987 { "vpn_server2_key", V_NONE },
988 { "vpn_server2_dh", V_NONE },
989 { "vpn_client_eas", V_NONE },
990 { "vpn_client1_poll", V_RANGE(0, 1440) },
991 { "vpn_client1_if", V_TEXT(3, 3) }, // tap, tun
992 { "vpn_client1_bridge", V_01 },
993 { "vpn_client1_nat", V_01 },
994 { "vpn_client1_proto", V_TEXT(3, 10) }, // udp, tcp-server
995 { "vpn_client1_addr", V_NONE },
996 { "vpn_client1_port", V_PORT },
997 { "vpn_client1_retry", V_RANGE(-1,32767) }, // -1 infinite, 0 disabled, >= 1 custom
998 { "vpn_client1_firewall", V_TEXT(0, 6) }, // auto, custom
999 { "vpn_client1_crypt", V_TEXT(0, 6) }, // tls, secret, custom
1000 { "vpn_client1_comp", V_TEXT(0, 8) }, // yes, no, adaptive
1001 { "vpn_client1_cipher", V_TEXT(0, 16) },
1002 { "vpn_client1_local", V_IP },
1003 { "vpn_client1_remote", V_IP },
1004 { "vpn_client1_nm", V_IP },
1005 { "vpn_client1_reneg", V_RANGE(-1,2147483647)},
1006 { "vpn_client1_hmac", V_RANGE(-1, 2) },
1007 { "vpn_client1_adns", V_RANGE(0, 3) },
1008 { "vpn_client1_rgw", V_01 },
1009 { "vpn_client1_gw", V_TEXT(0, 15) },
1010 { "vpn_client1_custom", V_NONE },
1011 { "vpn_client1_static", V_NONE },
1012 { "vpn_client1_ca", V_NONE },
1013 { "vpn_client1_crt", V_NONE },
1014 { "vpn_client1_key", V_NONE },
1015 { "vpn_client2_poll", V_RANGE(0, 1440) },
1016 { "vpn_client2_if", V_TEXT(3, 3) }, // tap, tun
1017 { "vpn_client2_bridge", V_01 },
1018 { "vpn_client2_nat", V_01 },
1019 { "vpn_client2_proto", V_TEXT(3, 10) }, // udp, tcp-server
1020 { "vpn_client2_addr", V_NONE },
1021 { "vpn_client2_port", V_PORT },
1022 { "vpn_client2_retry", V_RANGE(-1,32767) }, // -1 infinite, 0 disabled, >= 1 custom
1023 { "vpn_client2_firewall", V_TEXT(0, 6) }, // auto, custom
1024 { "vpn_client2_crypt", V_TEXT(0, 6) }, // tls, secret, custom
1025 { "vpn_client2_comp", V_TEXT(0, 8) }, // yes, no, adaptive
1026 { "vpn_client2_cipher", V_TEXT(0, 16) },
1027 { "vpn_client2_local", V_IP },
1028 { "vpn_client2_remote", V_IP },
1029 { "vpn_client2_nm", V_IP },
1030 { "vpn_client2_reneg", V_RANGE(-1,2147483647)},
1031 { "vpn_client2_hmac", V_RANGE(-1, 2) },
1032 { "vpn_client2_adns", V_RANGE(0, 3) },
1033 { "vpn_client2_rgw", V_01 },
1034 { "vpn_client2_gw", V_TEXT(0, 15) },
1035 { "vpn_client2_custom", V_NONE },
1036 { "vpn_client2_static", V_NONE },
1037 { "vpn_client2_ca", V_NONE },
1038 { "vpn_client2_crt", V_NONE },
1039 { "vpn_client2_key", V_NONE },
1040 #endif // vpn
1043 ppp_static 0/1
1044 ppp_static_ip IP
1045 wl_enable 0/1
1046 wl_wds_timeout
1047 wl_maxassoc 1-256
1048 wl_phytype a,b,g
1049 wl_net_reauth
1050 wl_preauth
1051 wl_wme_ap_bk
1052 wl_wme_ap_be
1053 wl_wme_ap_vi
1054 wl_wme_ap_vo
1055 wl_wme_sta_bk
1056 wl_wme_sta_be
1057 wl_wme_sta_vi
1058 wl_wme_sta_vo
1060 port_priority_1 0-2
1061 port_flow_control_1 0,1
1062 port_rate_limit_1 0-8
1063 port_priority_2 0-2
1064 port_flow_control_2 0,1
1065 port_rate_limit_2 0-8
1066 port_priority_3 0-2
1067 port_flow_control_3 0,1
1068 port_rate_limit_3 0-8
1069 port_priority_4 0-2
1070 port_flow_control_4 0,1
1071 port_rate_limit_4 0-8
1072 wl_ap_ip
1073 wl_ap_ssid
1076 { NULL }
1079 static int save_variables(int write)
1081 const nvset_t *v;
1082 char *p, *e;
1083 int n;
1084 long l;
1085 unsigned u[6];
1086 int ok;
1087 char s[256];
1088 int dirty;
1089 static const char *msgf = "The field \"%s\" is invalid. Please report this problem.";
1091 dirty = 0;
1092 for (v = nvset_list; v->name; ++v) {
1093 // _dprintf("[%s] %p\n", v->name, webcgi_get((char*)v->name));
1094 if ((p = webcgi_get((char*)v->name)) == NULL) continue;
1095 ok = 1;
1096 switch (v->vtype) {
1097 case VT_TEXT:
1098 p = unix_string(p); // NOTE: p = malloc'd
1099 // drop
1100 case VT_LENGTH:
1101 n = strlen(p);
1102 if ((n < v->va.i) || (n > v->vb.i)) ok = 0;
1103 break;
1104 case VT_RANGE:
1105 l = strtol(p, &e, 10);
1106 if ((p == e) || (*e) || (l < v->va.l) || (l > v->vb.l)) ok = 0;
1107 break;
1108 case VT_IP:
1109 if ((sscanf(p, "%3u.%3u.%3u.%3u", &u[0], &u[1], &u[2], &u[3]) != 4) ||
1110 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255)) ok = 0;
1111 break;
1112 case VT_MAC:
1113 if ((sscanf(p, "%2x:%2x:%2x:%2x:%2x:%2x", &u[0], &u[1], &u[2], &u[3], &u[4], &u[5]) != 6) ||
1114 (u[0] > 255) || (u[1] > 255) || (u[2] > 255) || (u[3] > 255) || (u[4] > 255) || (u[5] > 255)) ok = 0;
1115 break;
1116 default:
1117 // shutup gcc
1118 break;
1120 if (!ok) {
1121 if (v->vtype == VT_TEXT) free(p);
1123 sprintf(s, msgf, v->name);
1124 resmsg_set(s);
1125 return 0;
1127 if (write) {
1128 if (!nvram_match((char *)v->name, p)) {
1129 if (v->vtype != VT_TEMP) dirty = 1;
1130 nvram_set(v->name, p);
1133 if (v->vtype == VT_TEXT) free(p);
1137 // special cases
1139 char *p1, *p2;
1140 if (((p1 = webcgi_get("set_password_1")) != NULL) && (strcmp(p1, "**********") != 0)) {
1141 if (((p2 = webcgi_get("set_password_2")) != NULL) && (strcmp(p1, p2) == 0)) {
1142 if ((write) && (!nvram_match("http_passwd", p1))) {
1143 dirty = 1;
1144 nvram_set("http_passwd", p1);
1147 else {
1148 sprintf(s, msgf, "password");
1149 resmsg_set(s);
1150 return 0;
1154 for (n = 0; n < 50; ++n) {
1155 sprintf(s, "rrule%d", n);
1156 if ((p = webcgi_get(s)) != NULL) {
1157 if (strlen(p) > 2048) {
1158 sprintf(s, msgf, s);
1159 resmsg_set(s);
1160 return 0;
1162 if ((write) && (!nvram_match(s, p))) {
1163 dirty = 1;
1164 nvram_set(s, p);
1169 return (write) ? dirty : 1;
1172 static void wo_tomato(char *url)
1174 char *v;
1175 int i;
1176 int ajax;
1177 int nvset;
1178 const char *red;
1179 int commit;
1181 // _dprintf("tomato.cgi\n");
1183 red = webcgi_safeget("_redirect", "");
1184 if (!*red) send_header(200, NULL, mime_html, 0);
1186 commit = atoi(webcgi_safeget("_commit", "1"));
1187 ajax = atoi(webcgi_safeget("_ajax", "0"));
1189 nvset = atoi(webcgi_safeget("_nvset", "1"));
1190 if (nvset) {
1191 if (!save_variables(0)) {
1192 if (ajax) {
1193 web_printf("@msg:%s", resmsg_get());
1195 else {
1196 parse_asp("error.asp");
1198 return;
1200 commit = save_variables(1) && commit;
1202 resmsg_set("Settings saved.");
1205 rboot = atoi(webcgi_safeget("_reboot", "0"));
1206 if (rboot) {
1207 parse_asp("reboot.asp");
1209 else {
1210 if (ajax) {
1211 web_printf("@msg:%s", resmsg_get());
1213 else if (atoi(webcgi_safeget("_moveip", "0"))) {
1214 parse_asp("saved-moved.asp");
1216 else if (!*red) {
1217 parse_asp("saved.asp");
1221 if (commit) {
1222 _dprintf("commit from tomato.cgi\n");
1223 nvram_commit_x();
1226 if ((v = webcgi_get("_service")) != NULL && *v != 0) {
1227 if (!*red) {
1228 if (ajax) web_printf(" Some services are being restarted...");
1229 web_close();
1231 sleep(1);
1233 if (*v == '*') {
1234 kill(1, SIGHUP);
1236 else {
1237 exec_service(v);
1241 for (i = atoi(webcgi_safeget("_sleep", "0")); i > 0; --i) sleep(1);
1243 if (*red) redirect(red);
1245 if (rboot) {
1246 web_close();
1247 sleep(1);
1248 kill(1, SIGTERM);
1253 // ----------------------------------------------------------------------------
1256 static void wo_update(char *url)
1258 const aspapi_t *api;
1259 const char *name;
1260 int argc;
1261 char *argv[16];
1262 char s[32];
1264 if ((name = webcgi_get("exec")) != NULL) {
1265 for (api = aspapi; api->name; ++api) {
1266 if (strcmp(api->name, name) == 0) {
1267 for (argc = 0; argc < 16; ++argc) {
1268 sprintf(s, "arg%d", argc);
1269 if ((argv[argc] = (char *)webcgi_get(s)) == NULL) break;
1271 api->exec(argc, argv);
1272 break;
1278 static void wo_service(char *url)
1280 int n;
1282 exec_service(webcgi_safeget("_service", ""));
1284 if ((n = atoi(webcgi_safeget("_sleep", "2"))) <= 0) n = 2;
1285 sleep(n);
1287 common_redirect();
1290 static void wo_shutdown(char *url)
1292 parse_asp("shutdown.asp");
1293 web_close();
1294 sleep(1);
1296 kill(1, SIGQUIT);
1299 static void wo_nvcommit(char *url)
1301 parse_asp("saved.asp");
1302 web_close();
1303 nvram_commit();