usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / httpd / config.c
blob1ce148ab5960607e74ca66060272eb70482d8945
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <sys/stat.h>
13 #include <sys/wait.h>
14 #include <typedefs.h>
15 #include <sys/reboot.h>
17 // #define DEBUG
19 #ifdef DEBUG
20 #define NVRAMCMD "/tmp/nvram"
21 #else
22 #define NVRAMCMD "nvram"
23 #endif
25 void wo_defaults(char *url)
27 const char *v;
28 int mode;
30 if ((v = webcgi_get("mode")) != NULL) {
31 mode = atoi(v);
32 if ((mode == 1) || (mode == 2)) {
33 prepare_upgrade();
35 parse_asp("reboot-default.asp");
36 web_close();
38 // disconnect ppp - need this for PPTP/L2TP/PPPOE to finish gracefully
39 killall("xl2tpd", SIGTERM);
40 killall("pppd", SIGTERM);
42 led(LED_DIAG, 1);
43 sleep(2);
45 if (mode == 1) {
46 // eval(NVRAMCMD, "defaults", "--yes");
47 nvram_set("restore_defaults", "1");
48 nvram_commit();
50 else {
51 eval("mtd-erase", "-d", "nvram");
54 set_action(ACT_REBOOT);
55 // kill(1, SIGTERM);
56 reboot(RB_AUTOBOOT);
57 exit(0);
61 redirect("/admin-config.asp");
64 void wo_backup(char *url)
66 char tmp[64];
67 char msg[64];
68 static char *args[] = {
69 NVRAMCMD, "backup", NULL, NULL
72 strcpy(tmp, "/tmp/backupXXXXXX");
73 mktemp(tmp);
74 args[2] = tmp;
76 sprintf(msg, ">%s.msg", tmp);
78 if (_eval(args, msg, 0, NULL) == 0) {
79 send_header(200, NULL, mime_binary, 0);
80 do_file(tmp);
81 unlink(tmp);
83 else {
84 resmsg_fread(msg + 1);
85 send_header(200, NULL, mime_html, 0);
86 parse_asp("error.asp");
89 unlink(msg + 1);
92 void wi_restore(char *url, int len, char *boundary)
94 char *buf;
95 const char *error;
96 int ok;
97 int n;
98 char tmp[64];
100 check_id(url);
102 tmp[0] = 0;
103 buf = NULL;
104 error = "Error reading file";
105 ok = 0;
107 if (!skip_header(&len)) {
108 goto ERROR;
111 if ((len < 64) || (len > (NVRAM_SPACE * 2))) {
112 error = "Invalid file";
113 goto ERROR;
116 if ((buf = malloc(len)) == NULL) {
117 error = "Not enough memory";
118 goto ERROR;
121 n = web_read(buf, len);
122 len -= n;
124 strcpy(tmp, "/tmp/restoreXXXXXX");
125 mktemp(tmp);
126 if (f_write(tmp, buf, n, 0, 0600) != n) {
127 error = "Error writing temporary file";
128 goto ERROR;
131 rboot = 1;
132 prepare_upgrade();
134 char msg[64];
135 static char *args[] = {
136 NVRAMCMD, "restore", NULL, NULL
139 args[2] = tmp;
141 sprintf(msg, ">%s.msg", tmp);
143 if (_eval(args, msg, 0, NULL) != 0) {
144 resmsg_fread(msg + 1);
146 #ifndef DEBUG
147 unlink(msg + 1);
148 #endif
149 error = NULL;
151 ERROR:
152 free(buf);
153 if (error != NULL) resmsg_set(error);
154 web_eat(len);
155 #ifndef DEBUG
156 if (tmp[0]) unlink(tmp);
157 #endif
161 void wo_restore(char *url)
163 if (rboot) {
164 parse_asp("reboot.asp");
165 web_close();
167 // disconnect ppp - need this for PPTP/L2TP/PPPOE to finish gracefully
168 killall("xl2tpd", SIGTERM);
169 killall("pppd", SIGTERM);
170 sleep(2);
172 #ifdef DEBUG
173 cprintf("---reboot=%d\n", rboot);
174 #else
175 set_action(ACT_REBOOT);
176 // kill(1, SIGTERM);
177 sync();
178 reboot(RB_AUTOBOOT);
179 #endif
180 exit(0);
183 parse_asp("error.asp");