source: src/router/httpd/modules/broadcom.c @ 18882

Last change on this file since 18882 was 18882, checked in by BrainSlayer, 14 months ago

small adjustment for factory defaults

File size: 73.1 KB
Line 
1
2/*
3 * Broadcom Home Gateway Reference Design
4 * Web Page Configuration Support Routines
5 *
6 * Copyright 2001-2003, Broadcom Corporation
7 * All Rights Reserved.
8 *
9 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
10 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
11 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
12 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
13 * $Id: broadcom.c,v 1.9 2005/11/30 11:53:42 seg Exp $
14 */
15
16#ifdef WEBS
17#include <webs.h>
18#include <uemf.h>
19#include <ej.h>
20#else                           /* !WEBS */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25#include <unistd.h>
26#include <limits.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <httpd.h>
33#include <errno.h>
34#endif                          /* WEBS */
35
36#include <proto/ethernet.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <time.h>
40#include <sys/klog.h>
41#include <sys/wait.h>
42#include <cyutils.h>
43#include <support.h>
44#include <cy_conf.h>
45// #ifdef EZC_SUPPORT
46#include <ezc.h>
47// #endif
48#include <broadcom.h>
49#include <wlutils.h>
50#include <netdb.h>
51#include <utils.h>
52#include <dlfcn.h>
53
54int debug_value = 0;
55
56// static char * rfctime(const time_t *timep);
57// static char * reltime(unsigned int seconds);
58
59// #if defined(linux)
60
61#include <fcntl.h>
62#include <signal.h>
63#include <time.h>
64#include <sys/klog.h>
65#include <sys/wait.h>
66#define sys_stats(url) eval("stats", (url))
67
68// tofu
69
70char *live_translate(char *tran);
71#ifdef HAVE_BUFFALO
72void do_vsp_page(struct mime_handler *handler, char *url, webs_t stream,
73                 char *query);
74#endif
75/*
76 * Deal with side effects before committing
77 */
78int sys_commit(void)
79{
80        if (nvram_match("dhcpnvram", "1")) {
81                killall("dnsmasq", SIGUSR2);    // update lease -- tofu
82                sleep(1);
83        }
84        // if (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto",
85        // "pptp") )
86        // nvram_set("wan_ifname", "ppp0");
87        // else
88        // nvram_set("wan_ifname", nvram_get("pppoe_ifname"));
89        return nvram_commit();
90}
91
92/*
93 * Variables are set in order (put dependent variables later). Set
94 * nullok to TRUE to ignore zero-length values of the variable itself.
95 * For more complicated validation that cannot be done in one pass or
96 * depends on additional form components or can throw an error in a
97 * unique painful way, write your own validation routine and assign it
98 * to a hidden variable (e.g. filter_ip).
99 */
100/*
101 * DD-WRT enhancement by seg This functions parses all
102 * /etc/config/xxxxx.nvramconfig files and creates the web var tab. so these
103 * vars arent defined anymore staticly
104 */
105
106#include <stdlib.h>
107#include <malloc.h>
108#include <dirent.h>
109#include <stdlib.h>
110
111char *toUP(char *a)
112{
113        int i;
114        int slen = strlen(a);
115
116        for (i = 0; i < slen; i++) {
117                if (a[i] > 'a' - 1 && a[i] < 'z' + 1)
118                        a[i] -= 'a' + 'A';
119        }
120        return a;
121}
122
123int stricmp(char *a, char *b)
124{
125        if (strlen(a) != strlen(b))
126                return -1;
127        return strcmp(toUP(a), toUP(b));
128}
129
130void StringStart(FILE * in)
131{
132        while (getc(in) != '"') {
133                if (feof(in))
134                        return;
135        }
136}
137
138char *getFileString(FILE * in)
139{
140        char *buf;
141        int i, b;
142
143        buf = safe_malloc(1024);
144        StringStart(in);
145        for (i = 0; i < 1024; i++) {
146                b = getc(in);
147                if (b == EOF)
148                        return NULL;
149                if (b == '"') {
150                        buf[i] = 0;
151                        buf = realloc(buf, strlen(buf) + 1);
152                        return buf;
153                }
154                buf[i] = b;
155        }
156        return buf;
157}
158
159void skipFileString(FILE * in)
160{
161        int i, b;
162
163        StringStart(in);
164        for (i = 0; i < 1024; i++) {
165                b = getc(in);
166                if (b == EOF)
167                        return;
168                if (b == '"') {
169                        return;
170                }
171        }
172        return;
173}
174
175static char *directories[] = {
176        "/etc/config",
177        "/jffs/etc/config",
178        "/mmc/etc/config"
179};
180
181struct SIMPLEVAL {
182        char *name;
183        char *validator;
184        int args;
185};
186
187struct variable **variables;
188void Initnvramtab()
189{
190        struct dirent *entry;
191        DIR *directory;
192        FILE *in;
193        int varcount = 0, len, i;
194        char *tmpstr;
195        struct variable *tmp;
196        static struct SIMPLEVAL simpleval[] = {
197                {"WMEPARAM", "validate_wl_wme_params", 0},
198                {"WMETXPARAM", "validate_wl_wme_tx_params", 0},
199                {"WANIPADDR", "validate_wan_ipaddr", 0},
200                {"MERGEREMOTEIP", "validate_remote_ip", 0},
201                {"MERGEIPADDRS", "validate_merge_ipaddrs", 0},
202                {"DNS", "validate_dns", 0},
203                {"SAVEWDS", "save_wds", 0},
204                {"DHCP", "dhcp_check", 0},
205                {"STATICS", "validate_statics", 0},
206#ifdef HAVE_PORTSETUP
207                {"PORTSETUP", "validate_portsetup", 0},
208#endif
209                {"REBOOT", "validate_reboot", 0},
210                {"IPADDR", "validate_ipaddr", 0},
211                {"STATICLEASES", "validate_staticleases", 0},
212#ifdef HAVE_CHILLILOCAL
213                {"USERLIST", "validate_userlist", 0},
214#endif
215#ifdef HAVE_RADLOCAL
216                {"IRADIUSUSERLIST", "validate_iradius", 0},
217#endif
218                {"IPADDRS", "validate_ipaddrs", 0},
219                {"NETMASK", "validate_netmask", 0},
220                {"MERGENETMASK", "validate_merge_netmask", 0},
221                {"WDS", "validate_wds", 0},
222                {"STATICROUTE", "validate_static_route", 0},
223                {"MERGEMAC", "validate_merge_mac", 0},
224                {"FILTERPOLICY", "validate_filter_policy", 0},
225                {"FILTERIPGRP", "validate_filter_ip_grp", 0},
226                {"FILTERPORT", "validate_filter_port", 0},
227                {"FILTERDPORTGRP", "validate_filter_dport_grp", 0},
228                {"BLOCKEDSERVICE", "validate_blocked_service", 0},
229                {"FILTERP2P", "validate_catchall", 0},
230                {"FILTERMACGRP", "validate_filter_mac_grp", 0},
231                {"FILTERWEB", "validate_filter_web", 0},
232                {"WLHWADDRS", "validate_wl_hwaddrs", 0},
233                {"FORWARDPROTO", "validate_forward_proto", 0},
234                {"FORWARDSPEC", "validate_forward_spec", 0},
235                {"PORTTRIGGER", "validate_port_trigger", 0},
236                {"HWADDR", "validate_hwaddr", 0},
237                {"HWADDRS", "validate_hwaddrs", 0},
238                {"WLWEPKEY", "validate_wl_wep_key", 0},
239#ifdef HAVE_PPPOESERVER
240                {"CHAPTABLE", "validate_chaps", 0},
241#endif
242#ifdef HAVE_MILKFISH
243                {"MFSUBSCRIBERS", "validate_subscribers", 0},
244                {"MFALIASES", "validate_aliases", 0},
245#endif
246                {"RANGE", "validate_range", 2},
247                {"CHOICE", "validate_choice", -1},
248                {"NOACK", "validate_noack", 2},
249                {"NAME", "validate_name", 1},
250                {"PASSWORD", "validate_password", 1},
251                {"PASSWORD2", "validate_password2", 1},
252                {"LANIPADDR", "validate_lan_ipaddr", 1},
253                {"WPAPSK", "validate_wpa_psk", 1},
254                {"WLAUTH", "validate_wl_auth", 2},
255                {"WLWEP", "validate_wl_wep", -1},
256                {"DYNAMICROUTE", "validate_dynamic_route", -1},
257                {"WLGMODE", "validate_wl_gmode", -1},
258                {"WLNETMODE", "validate_wl_net_mode", -1},
259                {"AUTHMODE", "validate_auth_mode", -1},
260                {NULL, NULL},
261        };
262
263        variables = NULL;
264        char buf[1024];
265
266        // format = VARNAME VARDESC VARVALID VARVALIDARGS FLAGS FLAGS
267        // open config directory directory =
268        int idx;
269
270        for (idx = 0; idx < 3; idx++) {
271                directory = opendir(directories[idx]);
272                if (directory == NULL)
273                        continue;
274                // list all files in this directory
275                while ((entry = readdir(directory)) != NULL) {
276                        if (endswith(entry->d_name, ".nvramconfig")) {
277                                sprintf(buf, "%s/%s", directories[idx],
278                                        entry->d_name);
279                                in = fopen(buf, "rb");
280                                if (in == NULL) {
281                                        return;
282                                }
283                                while (1) {
284                                        tmp = (struct variable *)
285                                            safe_malloc(sizeof
286                                                        (struct variable));
287                                        memset(tmp, 0, sizeof(struct variable));
288                                        tmp->name = getFileString(in);
289                                        if (tmp->name == NULL)
290                                                break;
291                                        skipFileString(in);     // long string
292                                        tmpstr = getFileString(in);
293                                        tmp->argv = NULL;
294                                        if (!stricmp(tmpstr, "NULL")) {
295                                        }
296#ifdef HAVE_SPUTNIK_APD
297                                        if (!stricmp(tmpstr, "MJIDTYPE")) {
298                                                tmp->validatename =
299                                                    "validate_choice";
300                                                free(tmpstr);
301                                                tmpstr = getFileString(in);
302                                                len = atoi(tmpstr);
303                                                tmp->argv = (char **)
304                                                    safe_malloc(sizeof(char **)
305                                                                * (len + 1));
306                                                for (i = 0; i < len; i++) {
307                                                        tmp->argv[i] =
308                                                            getFileString(in);
309                                                }
310                                                tmp->argv[i] = NULL;
311                                                nvram_set("sputnik_rereg", "1");
312                                        }
313#endif
314                                        if (tmp->validatename == NULL) {
315                                                int scount = 0;
316                                                while (simpleval[scount].name != NULL) {        //
317                                                        if (!stricmp(tmpstr, simpleval[scount].name)) { //
318//                                                              fprintf(stderr,"match %s %s\n",tmpstr,tmp->name);
319                                                                tmp->validatename = simpleval[scount].validator;        //
320                                                                int arglen = 0;
321                                                                if (simpleval[scount].args == -1) {     //
322                                                                        free(tmpstr);
323                                                                        tmpstr = getFileString(in);     //
324                                                                        arglen = atoi(tmpstr);  //
325                                                                }
326                                                                if (simpleval[scount].args > 0) {       //
327                                                                        arglen = simpleval[scount].args;        //
328                                                                }
329                                                                if (arglen) {   //
330                                                                        tmp->argv = (char **)safe_malloc(sizeof(char **) * (arglen + 1));       //
331                                                                        for (i = 0; i < arglen; i++) {  //
332                                                                                tmp->argv[i] = getFileString(in);       //
333                                                                        }
334                                                                        tmp->argv[arglen] = NULL;       //
335
336                                                                }
337                                                                break;
338                                                        }
339                                                        scount++;
340                                                }
341//                                              if (simpleval[scount].name ==
342//                                                  NULL) {
343//                                                      fprintf(stderr,
344//                                                              "danger %s is missing\n",
345//                                                              tmpstr);
346//                                              }
347                                        }
348                                        free(tmpstr);
349                                        tmpstr = getFileString(in);
350                                        if (!stricmp(tmpstr, "TRUE")) {
351                                                tmp->nullok = TRUE;
352                                        } else {
353                                                tmp->nullok = FALSE;
354                                        }
355                                        free(tmpstr);
356                                        skipFileString(in);     // todo: remove it
357                                        // tmpstr = getFileString (in);
358                                        // tmp->ezc_flags = atoi (tmpstr);
359                                        // free (tmpstr);
360                                        variables = (struct variable **)
361                                            realloc(variables,
362                                                    sizeof(struct variable **) *
363                                                    (varcount + 2));
364                                        variables[varcount++] = tmp;
365                                        variables[varcount] = NULL;
366                                }
367                                fclose(in);
368                        }
369                }
370                closedir(directory);
371        }
372}
373
374#ifdef HAVE_MACBIND
375#include "../../../opt/mac.h"
376#endif
377// Added by Daniel(2004-07-29) for EZC
378int variables_arraysize(void)
379{
380        int varcount = 0;
381
382        if (variables == NULL)
383                return 0;
384        while (variables[varcount] != NULL) {
385                varcount++;
386        }
387        // return ARRAYSIZE(variables);
388        return varcount;
389}
390
391// and now the tricky part (more dirty as dirty)
392void do_filtertable(struct mime_handler *handler, char *path, webs_t stream,
393                    char *query)
394{
395        char *temp2 = &path[indexof(path, '-') + 1];
396        char ifname[16];
397
398        strcpy(ifname, temp2);
399
400        char *temp3 = websGetVar(stream, "ifname", NULL);
401        if (temp3 != NULL) {
402                if (strlen(temp3) > 0) {
403                        strcpy(ifname, temp3);
404                }
405        }
406
407        ifname[indexof(ifname, '.')] = 0;
408        FILE *web = getWebsFile("WL_FilterTable.asp");
409        char temp[4096];
410
411        memset(temp, 0, 4096);
412        unsigned int len = getWebsFileLen("WL_FilterTable.asp");
413        char *webfile = (char *)safe_malloc(len + 1);
414
415        fread(webfile, len, 1, web);
416        webfile[len] = 0;
417        rep(ifname, '.', 'X');
418        sprintf(temp, webfile, ifname, ifname, ifname, ifname, ifname, ifname);
419        free(webfile);
420        fclose(web);
421        do_ej_buffer(temp, stream);
422}
423
424#ifdef HAVE_FREERADIUS
425#include <radiusdb.h>
426
427static void cert_file_out(struct mime_handler *handler, char *path,
428                          webs_t stream, char *query)
429{
430        char *temp2 = &path[indexof(path, '/') + 1];
431        fprintf(stderr, "down %s\n", temp2);
432        char link[128];
433        sprintf(link, "/jffs/etc/freeradius/certs/clients/%s", temp2);
434        do_file_attach(handler, link, stream, NULL, temp2);
435}
436
437static void show_certfield(webs_t wp, char *title, char *file)
438{
439        websWrite(wp, "<div class=\"setting\">\n");
440        websWrite(wp, "<div class=\"label\">%s</div>\n", title);
441        websWrite(wp, "<script type=\"text/javascript\">\n");
442        websWrite(wp, "//<![CDATA[\n");
443        websWrite(wp,
444                  "document.write(\"<input class=\\\"button\\\" type=\\\"button\\\" name=\\\"download_button\\\" value=\\\"\" + sbutton.download + \"\\\" onclick=\\\"window.location.href='/freeradius-certs/%s';\\\" />\");\n",
445                  file);
446        websWrite(wp, "//]]>\n");
447        websWrite(wp, "</script>\n");
448        websWrite(wp, "</div>\n");
449
450}
451
452void do_radiuscert(struct mime_handler *handler, char *path, webs_t stream,
453                   char *query)
454{
455        char *temp2 = &path[indexof(path, '-') + 1];
456        char number[16];
457        webs_t wp = stream;
458        strcpy(number, temp2);
459        number[indexof(number, '.')] = 0;
460        int radiusindex = atoi(number);
461
462        if (radiusindex == -1)
463                return;
464        struct radiusdb *db = loadradiusdb();
465        if (db == NULL)         // database empty
466                return;
467        if (radiusindex >= db->usercount)       // index out of bound
468        {
469                goto out;
470        }
471        if (db->users[radiusindex].usersize == 0
472            || db->users[radiusindex].passwordsize == 0
473            || strlen(db->users[radiusindex].user) == 0
474            || strlen(db->users[radiusindex].passwd) == 0) {
475                //define username fail
476                char *argv[] = { "freeradius.clientcert" };
477                call_ej("do_pagehead", NULL, wp, 1, argv);      // thats dirty
478                websWrite(wp, "</head>\n"
479                          "<body>\n"
480                          "<div id=\"main\">\n" "<div id=\"contentsInfo\">\n"
481                          "<h2>%s</h2>\n"
482                          "Error: please specify a value username and password\n"
483                          "<div class=\"submitFooter\">\n"
484                          "<script type=\"text/javascript\">\n"
485                          "//<![CDATA[\n"
486                          "submitFooterButton(0,0,0,0,0,1);\n"
487                          "//]]>\n"
488                          "</script>\n"
489                          "</div>\n"
490                          "</div>\n"
491                          "</div>\n"
492                          "</body>\n"
493                          "</html>\n", live_translate("freeradius.clientcert"));
494                goto out;
495        }
496        char filename[128];
497        char exec[512];
498        int generate = 0;
499        sprintf(filename, "/jffs/etc/freeradius/certs/clients/%s-cert.pem",
500                db->users[radiusindex].user);
501        if (!f_exists(filename))
502                generate = 1;
503        sprintf(filename, "/jffs/etc/freeradius/certs/clients/%s-cert.p12",
504                db->users[radiusindex].user);
505        if (!f_exists(filename))
506                generate = 1;
507        sprintf(filename, "/jffs/etc/freeradius/certs/clients/%s-key.pem",
508                db->users[radiusindex].user);
509        if (!f_exists(filename))
510                generate = 1;
511        sprintf(filename, "/jffs/etc/freeradius/certs/clients/%s-req.pem",
512                db->users[radiusindex].user);
513        if (!f_exists(filename))
514                generate = 1;
515
516        if (generate)           //do not regenerate certificates if they are already created
517        {
518
519                char expiration_days[64];
520                strcpy(expiration_days, nvram_safe_get("radius_expiration"));
521                long expiration = 0;    //never
522                if (db->users[radiusindex].expiration) {
523                        time_t tm;
524                        time(&tm);
525                        long curtime = ((tm / 60) / 60) / 24;   //in days
526                        expiration =
527                            db->users[radiusindex].expiration - curtime;
528                        sprintf(expiration_days, "%ld", expiration);
529                }
530                //erase line from database
531                FILE *fp = fopen("/jffs/etc/freeradius/certs/index.txt", "rb");
532                if (fp) {
533                        fseek(fp, 0, SEEK_END);
534                        int len = ftell(fp);
535                        rewind(fp);
536                        char *serial = safe_malloc(len + 1);
537                        char *output = safe_malloc(len + 1);
538                        fread(serial, len, 1, fp);
539                        fclose(fp);
540                        //look for existing entry
541                        char common[128];
542                        sprintf(common, "CN=%s", db->users[radiusindex].user);
543                        int i;
544                        int clen = strlen(common);
545                        int llen = len - clen;
546                        int line = -1;
547                        int oc = 0;
548                        for (i = 0; i < llen; i++) {
549                                if (serial[i] == 0xa) {
550                                        if (line == -1)
551                                                line++;
552                                        line++;
553                                }
554                                if (!strncmp
555                                    (&serial[i], common, strlen(common))) {
556                                        //found line
557                                        int lines = 0;
558                                        int ic = 0;
559                                        while (1) {
560                                                if (serial[ic] == 0xa) {
561                                                        lines++;
562                                                }
563                                                if (line != lines)
564                                                        output[oc++] =
565                                                            serial[ic];
566                                                ic++;
567                                                if (ic == len)
568                                                        break;
569                                        }
570                                        break;
571                                }
572                        }
573                        if (oc) {
574                                fp = fopen
575                                    ("/jffs/etc/freeradius/certs/index.txt",
576                                     "wb");
577                                if (fp) {
578                                        fwrite(output, oc, 1, fp);
579                                        fclose(fp);
580                                }
581                        }
582                        free(output);
583                        free(serial);
584                }
585                sprintf(exec,
586                        "cd /jffs/etc/freeradius/certs && ./doclientcert \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
587                        expiration_days,
588                        nvram_safe_get("radius_country"),
589                        nvram_safe_get("radius_state"),
590                        nvram_safe_get("radius_locality"),
591                        nvram_safe_get("radius_organisation"),
592                        nvram_safe_get("radius_email"),
593                        db->users[radiusindex].user,
594                        db->users[radiusindex].passwd,
595                        nvram_safe_get("radius_passphrase"));
596                system(exec);
597        }
598        char *argv[] = {
599                "freeradius.clientcert"
600        };
601        call_ej("do_pagehead", NULL, wp, 1, argv);      // thats dirty
602        websWrite(wp, "</head>\n"
603                  "<body>\n"
604                  "<div id=\"main\">\n"
605                  "<div id=\"contentsInfo\">\n"
606                  "<h2>%s</h2>\n", live_translate("freeradius.clientcert"));
607        sprintf(filename, "%s-cert.pem", db->users[radiusindex].user);
608        show_certfield(wp, "Certificate PEM", filename);
609        sprintf(filename, "%s-cert.p12", db->users[radiusindex].user);
610        show_certfield(wp, "Certificate P12 (Windows)", filename);
611        sprintf(filename, "%s-req.pem", db->users[radiusindex].user);
612        show_certfield(wp, "Certificate Request", filename);
613        sprintf(filename, "%s-key.pem", db->users[radiusindex].user);
614        show_certfield(wp, "Private Key PEM", filename);
615        websWrite(wp, "<div class=\"submitFooter\">\n"
616                  "<script type=\"text/javascript\">\n"
617                  "//<![CDATA[\n"
618                  "submitFooterButton(0,0,0,0,0,1);\n"
619                  "//]]>\n"
620                  "</script>\n"
621                  "</div>\n" "</div>\n" "</div>\n" "</body>\n" "</html>\n");
622
623        //make certificates
624      out:;
625        freeradiusdb(db);
626
627}
628
629#endif
630void do_activetable(struct mime_handler *handler, char *path, webs_t stream,
631                    char *query)
632{
633        char *temp2 = &path[indexof(path, '-') + 1];
634        char ifname[16];
635
636        strcpy(ifname, temp2);
637
638        char *temp3 = websGetVar(stream, "ifname", NULL);
639        if (temp3 != NULL) {
640                if (strlen(temp3) > 0) {
641                        strcpy(ifname, temp3);
642                }
643        }
644
645        ifname[indexof(ifname, '.')] = 0;
646        FILE *web = getWebsFile("WL_ActiveTable.asp");
647        unsigned int len = getWebsFileLen("WL_ActiveTable.asp");
648        char *webfile = (char *)safe_malloc(len + 1);
649
650        fread(webfile, len, 1, web);
651        webfile[len] = 0;
652        fclose(web);
653
654        char temp[32768];
655
656        memset(temp, 0, 32768);
657        int ai = 0;
658        int i = 0;
659        int weblen = strlen(webfile);
660
661        for (i = 0; i < weblen; i++) {
662                if (webfile[i] == '%') {
663                        i++;
664                        switch (webfile[i]) {
665                        case '%':
666                                temp[ai++] = '%';
667                                break;
668                        case 's':
669                                strcpy(&temp[ai], ifname);
670                                ai += strlen(ifname);
671                                break;
672                        default:
673                                temp[ai++] = webfile[i];
674                                break;
675                        }
676                } else
677                        temp[ai++] = webfile[i];
678        }
679        free(webfile);
680        do_ej_buffer(temp, stream);
681}
682
683void do_wds(struct mime_handler *handler, char *path, webs_t stream,
684            char *query)
685{
686        char *temp2 = &path[indexof(path, '-') + 1];
687        char ifname[16];
688
689        strcpy(ifname, temp2);
690        ifname[indexof(ifname, '.')] = 0;
691        FILE *web = getWebsFile("Wireless_WDS.asp");
692        unsigned int len = getWebsFileLen("Wireless_WDS.asp");
693        char *webfile = (char *)safe_malloc(len + 1);
694
695        fread(webfile, len, 1, web);
696        webfile[len] = 0;
697        fclose(web);
698
699        char temp[32768];
700
701        memset(temp, 0, 32768);
702        int ai = 0;
703        int i = 0;
704        int weblen = strlen(webfile);
705
706        for (i = 0; i < weblen; i++) {
707                if (webfile[i] == '%') {
708                        i++;
709                        switch (webfile[i]) {
710                        case '%':
711                                temp[ai++] = '%';
712                                break;
713                        case 's':
714                                strcpy(&temp[ai], ifname);
715                                ai += strlen(ifname);
716                                break;
717                        default:
718                                temp[ai++] = webfile[i];
719                                break;
720                        }
721                } else
722                        temp[ai++] = webfile[i];
723        }
724        free(webfile);
725        do_ej_buffer(temp, stream);
726}
727
728void do_wireless_adv(struct mime_handler *handler, char *path, webs_t stream,
729                     char *query)
730{
731        char *temp2 = &path[indexof(path, '-') + 1];
732        char ifname[16];
733
734        strcpy(ifname, temp2);
735        ifname[indexof(ifname, '.')] = 0;
736        FILE *web = getWebsFile("Wireless_Advanced.asp");
737        unsigned int len = getWebsFileLen("Wireless_Advanced.asp");
738        char *webfile = (char *)safe_malloc(len + 1);
739
740        char index[2];
741        substring(strlen(ifname) - 1, strlen(ifname), ifname, index);
742
743        fread(webfile, len, 1, web);
744        webfile[len] = 0;
745        fclose(web);
746
747        char temp[65536];
748
749        memset(temp, 0, 65536);
750        int ai = 0;
751        int i = 0;
752        int weblen = strlen(webfile);
753
754        for (i = 0; i < weblen; i++) {
755                if (webfile[i] == '%') {
756                        i++;
757                        switch (webfile[i]) {
758                        case '%':
759                                temp[ai++] = '%';
760                                break;
761                        case 'd':
762                                strcpy(&temp[ai], index);
763                                ai++;
764                                break;
765                        case 's':
766                                strcpy(&temp[ai], ifname);
767                                ai += strlen(ifname);
768                                break;
769                        default:
770                                temp[ai++] = webfile[i];
771                                break;
772                        }
773                } else
774                        temp[ai++] = webfile[i];
775        }
776        free(webfile);
777        do_ej_buffer(temp, stream);
778}
779
780void validate_cgi(webs_t wp)
781{
782        char *value;
783        int i;
784
785#ifdef HAVE_MACBIND
786        if (!nvram_match("et0macaddr", MACBRAND))
787                return;
788#endif
789        int alen = variables_arraysize();
790        void *handle = NULL;
791
792        for (i = 0; i < alen; i++) {
793                if (variables[i] == NULL)
794                        return;
795                value = websGetVar(wp, variables[i]->name, NULL);
796                if (!value)
797                        continue;
798                if ((!*value && variables[i]->nullok)
799                    || (!variables[i]->validate2name
800                        && !variables[i]->validatename))
801                        nvram_set(variables[i]->name, value);
802                else {
803                        if (variables[i]->validatename) {
804                                cprintf("call validator_nofree %s\n",
805                                        variables[i]->validatename);
806                                handle =
807                                    start_validator_nofree(variables
808                                                           [i]->validatename,
809                                                           handle, wp, value,
810                                                           variables[i]);
811                        } else if (variables[i]->validate2name) {
812                                cprintf("call gozila %s\n",
813                                        variables[i]->validate2name);
814                                start_gozila(variables[i]->validate2name, wp);
815                                // fprintf(stderr,"validating %s =
816                                // %s\n",variables[i]->name,value);
817                                // variables[i]->validate (wp, value, variables[i]);
818                        } else {
819                                // variables[i]->validate2 (wp);
820                        }
821                }
822
823        }
824        cprintf("close handle\n");
825        if (handle)
826                dlclose(handle);
827        cprintf("all vars validated\n");
828}
829
830enum {
831        NOTHING,
832        REBOOT,
833        RESTART,
834        SERVICE_RESTART,
835        SYS_RESTART,
836        REFRESH,
837};
838static struct gozila_action gozila_actions[] = {
839        /*
840         * SETUP
841         */
842        {"index", "wan_proto", "", 1, REFRESH, "wan_proto"},
843        {"index", "dhcpfwd", "", 1, REFRESH, "dhcpfwd"},
844        // {"index", "clone_mac", "", 1, REFRESH, clone_mac}, //OBSOLETE
845#ifdef HAVE_CCONTROL
846        {"ccontrol", "execute", "", 1, REFRESH, "execute"},
847#endif
848        {"WanMAC", "clone_mac", "", 1, REFRESH, "clone_mac"},   // for cisco
849        // style
850        {"DHCPTable", "delete", "", 2, REFRESH, "delete_leases"},
851#ifdef HAVE_PPTPD
852        {"DHCPTable", "deletepptp", "", 2, REFRESH, "delete_pptp"},
853#endif
854        {"Info", "refresh", "", 0, REFRESH, "save_wifi"},
855        {"Status_Wireless", "refresh", "", 0, REFRESH, "save_wifi"},
856        // {"Status", "release", "dhcp_release", 0, SYS_RESTART, "dhcp_release"},
857        // {"Status", "renew", "", 3, REFRESH, "dhcp_renew"},
858        // {"Status", "Connect", "start_pppoe", 1, RESTART, NULL},
859        {"Status_Internet", "release", "dhcp_release", 0, SERVICE_RESTART, "dhcp_release"},     // for
860        {"Status_Internet", "renew", "", 3, REFRESH, "dhcp_renew"},     // for cisco
861        {"Status_Internet", "Disconnect", "stop_pppoe", 2, SERVICE_RESTART, "stop_ppp"},        // for
862#ifdef HAVE_3G
863        {"Status_Internet", "Connect_3g", "start_3g", 1, RESTART, NULL},        // for
864        {"Status_Internet", "Disconnect_3g", "stop_3g", 2, SERVICE_RESTART, "stop_ppp"},        // for
865#endif
866#ifdef HAVE_PPPOATM
867        {"Status_Internet", "Connect_pppoa", "start_pppoa", 1, RESTART, NULL},  // for
868        {"Status_Internet", "Disconnect_pppoa", "stop_pppoa", 2, SERVICE_RESTART, "stop_ppp"},  // for
869#endif
870        {"Status_Internet", "Connect_pppoe", "start_pppoe", 1, RESTART, NULL},  // for
871        {"Status_Internet", "Disconnect_pppoe", "stop_pppoe", 2, SERVICE_RESTART, "stop_ppp"},  // for
872
873        {"Status_Internet", "Connect_pptp", "start_pptp", 1, RESTART, NULL},    // for
874        {"Status_Internet", "Disconnect_pptp", "stop_pptp", 2, SERVICE_RESTART, "stop_ppp"},    // for
875        {"Status_Internet", "Connect_l2tp", "start_l2tp", 1, RESTART, NULL},    // for
876        {"Status_Internet", "Disconnect_l2tp", "stop_l2tp", 2, SERVICE_RESTART, "stop_ppp"},    // for
877        // cisco
878        // style{
879        // "Status_Router",
880        // "Connect_heartbeat",
881        // "start_heartbeat",
882        // 1,
883        // RESTART,
884        // NULL},
885        // //
886        // for
887        // cisco
888        // style
889        {"Status_Internet", "Disconnect_heartbeat", "stop_heartbeat", 2, SERVICE_RESTART, "stop_ppp"},  // for
890        // cisco
891        // style
892        {"Status_Internet", "delete_ttraffdata", "", 0, REFRESH,
893         "ttraff_erase"},
894        {"Filters", "save", "filters", 1, REFRESH, "save_policy"},
895        {"Filters", "delete", "filters", 1, REFRESH, "single_delete_policy"},
896        {"FilterSummary", "delete", "filters", 1, REFRESH,
897         "summary_delete_policy"},
898        {"Routing", "del", "static_route_del", 1, REFRESH,
899         "delete_static_route"},
900        {"RouteStatic", "del", "static_route_del", 1, REFRESH,
901         "delete_static_route"},
902        {"WL_WPATable", "wep_key_generate", "", 1, REFRESH, "generate_wep_key"},
903        {"WL_WPATable", "security", "", 1, REFRESH, "set_security"},
904        {"WL_WPATable", "save", "wireless_2", 1, REFRESH, "security_save"},
905        {"WL_WPATable", "keysize", "wireless_2", 1, REFRESH, "security_save"},
906        {"WL_ActiveTable", "add_mac", "", 1, REFRESH, "add_active_mac"},
907        {"WL_ActiveTable-wl0", "add_mac", "", 1, REFRESH, "add_active_mac"},
908        {"WL_ActiveTable-wl1", "add_mac", "", 1, REFRESH, "add_active_mac"},
909        /*
910         * Siafu addition
911         */
912        {"Ping", "wol", "", 1, REFRESH, "ping_wol"},
913        /*
914         * Sveasoft addition
915         */
916        // {"Wireless_WDS", "save", "", 0, REFRESH, save_wds},
917#ifndef HAVE_MADWIFI
918        {"Wireless_WDS-wl0", "save", "wireless_2", 0, REFRESH, "save_wds"},
919        {"Wireless_WDS-wl1", "save", "wireless_2", 0, REFRESH, "save_wds"},
920        {"Wireless_Advanced-wl0", "save", "wireless_2", 0, REFRESH,
921         "save_wireless_advanced"},
922        {"Wireless_Advanced-wl1", "save", "wireless_2", 0, REFRESH,
923         "save_wireless_advanced"},
924#else
925        {"Wireless_WDS-ath0", "save", "wireless_2", 0, REFRESH, "save_wds"},
926        {"Wireless_WDS-ath1", "save", "wireless_2", 0, REFRESH, "save_wds"},
927        {"Wireless_WDS-ath2", "save", "wireless_2", 0, REFRESH, "save_wds"},
928        {"Wireless_WDS-ath3", "save", "wireless_2", 0, REFRESH, "save_wds"},
929#endif
930        {"Ping", "startup", "", 1, REFRESH, "ping_startup"},
931        {"Ping", "shutdown", "", 1, REFRESH, "ping_shutdown"},
932        {"Ping", "firewall", "", 1, SYS_RESTART, "ping_firewall"},
933        {"Ping", "custom", "", 0, REFRESH, "ping_custom"},
934        {"QoS", "add_svc", "", 0, REFRESH, "qos_add_svc"},
935        {"QoS", "add_ip", "", 0, REFRESH, "qos_add_ip"},
936        {"QoS", "add_mac", "", 0, REFRESH, "qos_add_mac"},
937        {"QoS", "save", "filters", 1, REFRESH, "qos_save"},
938        /*
939         * end Sveasoft addition
940         */
941        {"Forward", "add_forward", "", 0, REFRESH, "forward_add"},
942        {"Forward", "remove_forward", "", 0, REFRESH, "forward_remove"},
943        {"Wireless_Basic", "add_vifs", "", 0, REFRESH, "add_vifs"},
944        {"Wireless_Basic", "remove_vifs", "", 0, REFRESH, "remove_vifs"},
945#ifdef HAVE_FREERADIUS
946        {"FreeRadius", "generate_certificate", "", 0, REFRESH,
947         "radius_generate_certificate"},
948        {"FreeRadius", "add_radius_user", "", 0, REFRESH, "add_radius_user"},
949        {"FreeRadius", "del_radius_user", "", 0, REFRESH, "del_radius_user"},
950        {"FreeRadius", "add_radius_client", "", 0, REFRESH,
951         "add_radius_client"},
952        {"FreeRadius", "del_radius_client", "", 0, REFRESH,
953         "del_radius_client"},
954        {"FreeRadius", "save_radius_user", "", 0, REFRESH, "save_radius_user"},
955#endif
956#ifdef HAVE_POKER
957        {"Poker", "add_poker_user", "", 0, REFRESH, "add_poker_user"},
958        {"Poker", "del_poker_user", "", 0, REFRESH, "del_poker_user"},
959        {"Poker", "save_poker_user", "", 0, REFRESH, "save_poker_user"},
960        {"PokerEdit", "poker_loaduser", "", 0, REFRESH, "poker_loaduser"},
961        {"PokerEdit", "poker_checkout", "", 0, REFRESH, "poker_checkout"},
962        {"PokerEdit", "poker_buy", "", 0, REFRESH, "poker_buy"},
963        {"PokerEdit", "poker_credit", "", 0, REFRESH, "poker_credit"},
964        {"PokerEdit", "poker_back", "", 0, REFRESH, "poker_back"},
965#endif
966#ifdef HAVE_BONDING
967        {"Networking", "add_bond", "", 0, REFRESH, "add_bond"},
968        {"Networking", "del_bond", "", 0, REFRESH, "del_bond"},
969#endif
970#ifdef HAVE_OLSRD
971        {"Routing", "add_olsrd", "", 0, REFRESH, "add_olsrd"},
972        {"Routing", "del_olsrd", "", 0, REFRESH, "del_olsrd"},
973#endif
974#ifdef HAVE_VLANTAGGING
975        {"Networking", "add_vlan", "", 0, REFRESH, "add_vlan"},
976        {"Networking", "add_bridge", "", 0, REFRESH, "add_bridge"},
977        {"Networking", "add_bridgeif", "", 0, REFRESH, "add_bridgeif"},
978        {"Networking", "del_vlan", "", 0, REFRESH, "del_vlan"},
979        {"Networking", "del_bridge", "", 0, REFRESH, "del_bridge"},
980        {"Networking", "del_bridgeif", "", 0, REFRESH, "del_bridgeif"},
981        {"Networking", "save_networking", "index", 0, REFRESH,
982         "save_networking"},
983        {"Networking", "add_mdhcp", "", 0, REFRESH, "add_mdhcp"},
984        {"Networking", "del_mdhcp", "", 0, REFRESH, "del_mdhcp"},
985#endif
986        {"Wireless_Basic", "save", "wireless", 1, REFRESH, "wireless_save"},
987#ifdef HAVE_WIVIZ
988        {"Wiviz_Survey", "Set", "", 0, REFRESH, "set_wiviz"},
989#endif
990#ifdef HAVE_REGISTER
991        {"Register", "activate", "", 1, RESTART, "reg_validate"},
992#endif
993        {"index", "changepass", "", 1, REFRESH, "changepass"},
994#ifdef HAVE_SUPERCHANNEL
995        {"SuperChannel", "activate", "", 1, REFRESH, "superchannel_validate"},
996#endif
997        {"Services", "add_lease", "", 0, REFRESH, "lease_add"},
998        {"Services", "remove_lease", "", 0, REFRESH, "lease_remove"},
999#ifdef HAVE_PPPOESERVER
1000        {"PPPoE_Server", "add_chap_user", "", 0, REFRESH, "chap_user_add"},
1001        {"PPPoE_Server", "remove_chap_user", "", 0, REFRESH,
1002         "chap_user_remove"},
1003#endif
1004#ifdef HAVE_CHILLILOCAL
1005        {"Hotspot", "add_user", "", 0, REFRESH, "user_add"},
1006        {"Hotspot", "remove_user", "", 0, REFRESH, "user_remove"},
1007#endif
1008#ifdef HAVE_RADLOCAL
1009        {"Hotspot", "add_iradius", "", 0, REFRESH, "raduser_add"},
1010#endif
1011        {"ForwardSpec", "add_forward_spec", "", 0, REFRESH, "forwardspec_add"},
1012        {"ForwardSpec", "remove_forward_spec", "", 0, REFRESH,
1013         "forwardspec_remove"},
1014        {"Triggering", "add_trigger", "", 0, REFRESH, "trigger_add"},
1015        {"Triggering", "remove_trigger", "", 0, REFRESH, "trigger_remove"},
1016        {"Port_Services", "save_services", "filters", 2, REFRESH,
1017         "save_services_port"},
1018        {"QOSPort_Services", "save_qosservices", "filters", 2, REFRESH,
1019         "save_services_port"},
1020        {"Ping", "start", "", 1, SERVICE_RESTART, "diag_ping_start"},
1021        {"Ping", "stop", "", 0, REFRESH, "diag_ping_stop"},
1022        {"Ping", "clear", "", 0, REFRESH, "diag_ping_clear"},
1023#ifdef HAVE_MILKFISH
1024        {"Milkfish_database", "add_milkfish_user", "", 0, REFRESH,
1025         "milkfish_user_add"},
1026        {"Milkfish_database", "remove_milkfish_user", "", 0, REFRESH,
1027         "milkfish_user_remove"},
1028        {"Milkfish_aliases", "add_milkfish_alias", "", 0, REFRESH,
1029         "milkfish_alias_add"},
1030        {"Milkfish_aliases", "remove_milkfish_alias", "", 0, REFRESH,
1031         "milkfish_alias_remove"},
1032        {"Milkfish_messaging", "send_message", "", 1, SERVICE_RESTART,
1033         "milkfish_sip_message"},
1034#endif
1035#ifdef HAVE_BUFFALO
1036        {"SetupAssistant", "save", "setupassistant", 1, REFRESH,
1037         "setupassistant_save"},
1038        {"SetupAssistant", "wep_key_generate", "setupassistant", 1, REFRESH,
1039         "generate_wep_key"},
1040        {"SetupAssistant", "security", "setupassistant", 1, REFRESH,
1041         "set_security"},
1042        {"SetupAssistant", "keysize", "setupassistant", 1, REFRESH,
1043         "security_save"},
1044        {"AOSS", "save", "aoss", 1, REFRESH, "aoss_save"},
1045        {"AOSS", "start", "aoss", 1, REFRESH, "aoss_start"},
1046        {"Upgrade", "get_upgrades", "firmware", 1, REFRESH,
1047         "get_airstation_upgrades"},
1048#ifdef HAVE_WPS
1049        {"AOSS", "wps_register", "aoss", 1, REFRESH, "wps_register"},
1050        {"AOSS", "wps_ap_register", "aoss", 1, REFRESH, "wps_ap_register"},
1051        {"AOSS", "wps_forcerelease", "aoss", 1, REFRESH, "wps_forcerelease"},
1052        {"AOSS", "wps_configure", "aoss", 1, REFRESH, "wps_configure"},
1053#endif
1054#ifdef HAVE_SPOTPASS
1055        {"Nintendo", "save", "spotpass", 1, REFRESH, "nintendo_save"},
1056#endif
1057#endif
1058        {"Join", "Join", "wireless", 1, REFRESH, "wireless_join"},
1059#ifdef HAVE_NAS_SERVER
1060        {"NAS", "save", "nassrv", 1, REFRESH, "nassrv_save"},
1061#endif
1062};
1063
1064struct gozila_action *handle_gozila_action(char *name, char *type)
1065{
1066        struct gozila_action *v;
1067
1068        if (!name || !type)
1069                return NULL;
1070
1071        for (v = gozila_actions;
1072             v < &gozila_actions[STRUCT_LEN(gozila_actions)]; v++) {
1073                if (!strcmp(v->name, name) && !strcmp(v->type, type)) {
1074                        return v;
1075                }
1076        }
1077        return NULL;
1078}
1079
1080char my_next_page[30] = "";
1081int gozila_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1082               char_t * url, char_t * path, char_t * query)
1083{
1084        char *submit_button, *submit_type, *next_page;
1085        int action = REFRESH;
1086        int sleep_time;
1087        struct gozila_action *act;
1088
1089        nvram_set("gozila_action", "1");
1090        my_next_page[0] = '\0';
1091        submit_button = websGetVar(wp, "submit_button", NULL);  /* every html
1092                                                                 * must have
1093                                                                 * the name */
1094        submit_type = websGetVar(wp, "submit_type", NULL);      /* add, del,
1095                                                                 * renew,
1096                                                                 * release
1097                                                                 * ..... */
1098
1099        fprintf(stderr, "submit_button=[%s] submit_type=[%s]\n", submit_button,
1100                submit_type);
1101        act = handle_gozila_action(submit_button, submit_type);
1102
1103        if (act) {
1104                fprintf(stderr,
1105                        "name=[%s] type=[%s] service=[%s] sleep=[%d] action=[%d]\n",
1106                        act->name, act->type, act->service, act->sleep_time,
1107                        act->action);
1108                addAction(act->service);
1109                sleep_time = act->sleep_time;
1110                action = act->action;
1111                if (act->goname) {
1112                        start_gozila(act->goname, wp);
1113                }
1114        } else {
1115                sleep_time = 0;
1116                action = REFRESH;
1117        }
1118
1119        if (action == REFRESH) {
1120                sleep(sleep_time);
1121        } else if (action == SERVICE_RESTART) {
1122                sys_commit();
1123                service_restart();
1124                sleep(sleep_time);
1125        } else if (action == SYS_RESTART) {
1126                sys_commit();
1127                sys_restart();
1128        } else if (action == RESTART) {
1129                sys_commit();
1130                sys_restart();
1131        }
1132
1133        if (my_next_page[0] != '\0') {
1134                sprintf(path, "%s", my_next_page);
1135        } else {
1136                next_page = websGetVar(wp, "next_page", NULL);
1137                if (next_page)
1138                        sprintf(path, "%s", next_page);
1139                else
1140                        sprintf(path, "%s.asp", submit_button);
1141        }
1142
1143        cprintf("refresh to %s\n", path);
1144        if (!strncmp(path, "WL_FilterTable", 14))
1145                do_filtertable(NULL, path, wp, NULL);   // refresh
1146#ifdef HAVE_FREERADIUS
1147        else if (!strncmp(path, "FreeRadiusCert", 14))
1148                do_radiuscert(NULL, path, wp, NULL);    // refresh
1149#endif
1150        // #ifdef HAVE_MADWIFI
1151        else if (!strncmp(path, "WL_ActiveTable", 14))
1152                do_activetable(NULL, path, wp, NULL);   // refresh
1153        else if (!strncmp(path, "Wireless_WDS", 12))
1154                do_wds(NULL, path, wp, NULL);   // refresh
1155        // #endif
1156        else if (!strncmp(path, "Wireless_Advanced", 17))
1157                do_wireless_adv(NULL, path, wp, NULL);  // refresh
1158        else
1159                do_ej(NULL, path, wp, NULL);    // refresh
1160        websDone(wp, 200);
1161
1162        nvram_set("gozila_action", "0");
1163        nvram_set("generate_key", "0");
1164        nvram_set("clone_wan_mac", "0");
1165
1166        return 1;
1167}
1168
1169struct apply_action apply_actions[] = {
1170        /*
1171         * name, service, sleep_time, action, function_to_execute
1172         */
1173
1174        /*
1175         * SETUP
1176         */
1177        {"index", "index", 0, SERVICE_RESTART, NULL},
1178        {"DDNS", "ddns", 0, SERVICE_RESTART, "ddns_save_value"},
1179        {"Routing", "routing", 0, SERVICE_RESTART, NULL},
1180        {"Vlan", "", 0, SYS_RESTART, "port_vlan_table_save"},
1181        {"eop-tunnel", "eop", 0, SERVICE_RESTART, NULL},
1182
1183        /*
1184         * WIRELESS
1185         */
1186        {"Wireless_Basic", "wireless", 0, SERVICE_RESTART, NULL},       // Only for
1187        // V23, since
1188        // V24 it's a
1189        // gozilla
1190        // save
1191        {"Wireless_Advanced-wl0", "wireless_2", 0, SERVICE_RESTART,
1192         "save_wireless_advanced"},
1193        {"Wireless_Advanced-wl1", "wireless_2", 0, SERVICE_RESTART,
1194         "save_wireless_advanced"},
1195        {"Wireless_MAC", "wireless_2", 0, SERVICE_RESTART, "save_macmode"},
1196        {"WL_FilterTable", "macfilter", 0, SERVICE_RESTART, NULL},
1197        {"Wireless_WDS", "wireless_2", 0, SERVICE_RESTART, NULL},
1198        {"WL_WPATable", "wireless_2", 0, SERVICE_RESTART, NULL},
1199
1200        /*
1201         * MANAGEMENT
1202         */
1203        {"Management", "management", 0, SYS_RESTART, NULL},
1204        {"Services", "services", 0, SERVICE_RESTART, NULL},
1205        {"Alive", "alive", 0, SERVICE_RESTART, NULL},
1206
1207        /*
1208         * SERVICES
1209         */
1210        {"PPPoE_Server", "services", 0, SERVICE_RESTART, NULL},
1211        {"PPTP", "services", 0, SERVICE_RESTART, NULL},
1212        {"USB", "usbdrivers", 0, SERVICE_RESTART, NULL},
1213        {"NAS", "nassrv", 0, SERVICE_RESTART, NULL},
1214        {"Hotspot", "hotspot", 0, SERVICE_RESTART, "hotspot_save"},
1215        {"Hotspot", "hotspot", 0, SERVICE_RESTART, NULL},
1216        {"AnchorFree", "anchorfree", 0, SERVICE_RESTART, NULL},
1217        {"Nintendo", "nintendo", 0, SERVICE_RESTART, NULL},
1218
1219        /*
1220         * APP & GAMING
1221         */
1222        {"Forward", "forward", 0, SERVICE_RESTART, NULL},
1223        {"ForwardSpec", "forward", 0, SERVICE_RESTART, NULL},
1224        {"Triggering", "filters", 0, SERVICE_RESTART, NULL},
1225        {"DMZ", "filters", 0, SERVICE_RESTART, NULL},
1226        {"Filters", "filters", 0, SERVICE_RESTART, NULL},
1227        {"FilterIPMAC", "filters", 0, SERVICE_RESTART, NULL},
1228#ifdef HAVE_UPNP
1229        {"UPnP", "forward_upnp", 0, SERVICE_RESTART, "tf_upnp"},
1230#endif
1231        /*
1232         * SECURITY
1233         */
1234        {"Firewall", "filters", 0, SERVICE_RESTART, NULL},
1235        {"VPN", "filters", 0, SERVICE_RESTART, NULL},
1236#ifdef HAVE_MILKFISH
1237        {"Milkfish", "milkfish", 0, SERVICE_RESTART, NULL},
1238#endif
1239        /*
1240         * Obsolete {"WL_WEPTable", "", 0, SERVICE_RESTART, NULL}, {"Security",
1241         * "", 1, RESTART, NULL}, {"System", "", 0, RESTART, NULL}, {"DHCP",
1242         * "dhcp", 0, SERVICE_RESTART, NULL}, {"FilterIP", "filters", 0,
1243         * SERVICE_RESTART, NULL}, {"FilterMAC", "filters", 0, SERVICE_RESTART,
1244         * NULL}, {"FilterPort", "filters", 0, SERVICE_RESTART, NULL},
1245         * {"Wireless", "wireless", 0, SERVICE_RESTART, NULL}, {"Log", "logging",
1246         * 0, SERVICE_RESTART, NULL}, //moved to Firewall {"QoS", "qos", 0,
1247         * SERVICE_RESTART, NULL}, //gozilla does the save
1248         */
1249
1250};
1251
1252struct apply_action *handle_apply_action(char *name)
1253{
1254        struct apply_action *v;
1255
1256        cprintf("apply name = \n", name);
1257        if (!name)
1258                return NULL;
1259
1260        for (v = apply_actions; v < &apply_actions[STRUCT_LEN(apply_actions)];
1261             v++) {
1262                if (!strcmp(v->name, name)) {
1263                        return v;
1264                }
1265        }
1266        return NULL;
1267}
1268
1269int getFileLen(FILE * in)
1270{
1271        int len;
1272
1273        fseek(in, 0, SEEK_END);
1274        len = ftell(in);
1275        rewind(in);
1276        return len;
1277}
1278
1279void do_logout(void)            // static functions are not exportable,
1280                                // additionally this is no ej function
1281{
1282        send_authenticate(auth_realm);
1283} static char *getdisc(void)    // works only for squashfs
1284{
1285        int i;
1286        static char ret[4];
1287        unsigned char *disks[] = {
1288                "sda2", "sdb2", "sdc2", "sdd2", "sde2", "sdf2", "sdg2", "sdh2",
1289                "sdi2"
1290        };
1291        for (i = 0; i < 9; i++) {
1292                char dev[64];
1293
1294                sprintf(dev, "/dev/%s", disks[i]);
1295                FILE *in = fopen(dev, "rb");
1296
1297                if (in == NULL)
1298                        continue;       // no second partition or disc does not
1299                // exist, skipping
1300                char buf[4];
1301
1302                fread(buf, 4, 1, in);
1303                if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q'
1304                    && buf[3] == 't') {
1305                        fclose(in);
1306                        // filesystem detected
1307                        strncpy(ret, disks[i], 3);
1308                        return ret;
1309                }
1310                fclose(in);
1311        }
1312        return NULL;
1313}
1314
1315static int
1316apply_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1317          char_t * url, char_t * path, char_t * query)
1318{
1319        int action = NOTHING;
1320        char *value;
1321        char *submit_button, *next_page;
1322        int sleep_time = 0;
1323        int need_commit = 1;
1324
1325        cprintf("need reboot\n");
1326        int need_reboot = atoi(websGetVar(wp, "need_reboot", "0"));
1327
1328        cprintf("apply");
1329
1330        /**********   get "change_action" and launch gozila_cgi if needed **********/
1331
1332        value = websGetVar(wp, "change_action", "");
1333        cprintf("get change_action = %s\n", value);
1334
1335        if (value && !strcmp(value, "gozila_cgi")) {
1336                gozila_cgi(wp, urlPrefix, webDir, arg, url, path, query);
1337                return 1;
1338        }
1339
1340  /***************************************************************************/
1341        if (!query) {
1342                goto footer;
1343        }
1344        if (legal_ip_netmask
1345            ("lan_ipaddr", "lan_netmask",
1346             nvram_safe_get("http_client_ip")) == TRUE)
1347                nvram_set("browser_method", "USE_LAN");
1348        else
1349                nvram_set("browser_method", "USE_WAN");
1350
1351  /**********   get all webs var **********/
1352
1353        submit_button = websGetVar(wp, "submit_button", "");
1354        cprintf("get submit_button = %s\n", submit_button);
1355
1356        need_commit = atoi(websGetVar(wp, "commit", "1"));
1357        cprintf("get need_commit = %d\n", need_commit);
1358
1359        value = websGetVar(wp, "action", "");
1360        cprintf("get action = %s\n", value);
1361
1362  /**********   check action to do **********/
1363
1364  /** Apply **/
1365        if (!strcmp(value, "Apply") || !strcmp(value, "ApplyTake")) {
1366                struct apply_action *act;
1367
1368                cprintf("validate cgi");
1369                validate_cgi(wp);
1370                cprintf("handle apply action\n");
1371                act = handle_apply_action(submit_button);
1372                cprintf("done\n");
1373                // If web page configuration is changed, the EZC configuration
1374                // function should be disabled.(2004-07-29)
1375                nvram_set("is_default", "0");
1376                nvram_set("is_modified", "1");
1377                if (act) {
1378                        fprintf(stderr,
1379                                "%s:submit_button=[%s] service=[%s] sleep_time=[%d] action=[%d]\n",
1380                                value, act->name, act->service, act->sleep_time,
1381                                act->action);
1382
1383                        if ((act->action == SYS_RESTART)
1384                            || (act->action == SERVICE_RESTART)) {
1385
1386                                addAction(act->service);
1387                        }
1388                        sleep_time = act->sleep_time;
1389                        action = act->action;
1390
1391                        if (act->goname)
1392                                start_gozila(act->goname, wp);
1393                } else {
1394                        // nvram_set ("action_service", "");
1395                        sleep_time = 1;
1396                        action = RESTART;
1397                }
1398                diag_led(DIAG, STOP_LED);
1399                sys_commit();
1400        }
1401
1402  /** Restore defaults **/
1403        else if (!strncmp(value, "Restore", 7)) {
1404                ACTION("ACT_SW_RESTORE");
1405                nvram_set("sv_restore_defaults", "1");
1406#ifdef HAVE_BUFFALO_SA
1407                int region_sa = 0;
1408                if(nvram_default_match("region", "SA", ""))
1409                        region_sa = 1;
1410#endif
1411                killall("udhcpc", SIGKILL);
1412                sys_commit();
1413#ifdef HAVE_X86
1414#ifdef HAVE_ERC
1415                eval("nvram", "restore", "/etc/defaults/x86ree.backup");
1416                eval("reboot");
1417                eval("event", "5", "1", "15");
1418#endif
1419                char drive[64];
1420                sprintf(drive, "/dev/%s", getdisc());
1421                FILE *in = fopen(drive, "r+b");
1422                fseeko(in, 0, SEEK_END);
1423                __off_t mtdlen = ftell(in);
1424                fseeko(in, mtdlen - (65536 * 2), SEEK_SET);
1425                int i;
1426                for (i = 0; i < 65536; i++)
1427                        putc(0, in);    // erase backup area
1428                fclose(in);
1429                eval("mount", "/usr/local", "-o", "remount,rw");
1430                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1431                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1432                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1433                // database
1434                eval("mount", "/usr/local", "-o", "remount,ro");
1435                eval("sync");
1436#elif HAVE_RB600
1437                char drive[64];
1438                sprintf(drive, "/dev/sda");
1439                FILE *in = fopen(drive, "r+b");
1440                fseeko(in, 0, SEEK_END);
1441                __off_t mtdlen = ftell(in);
1442                fseeko(in, mtdlen - (65536 * 2), SEEK_SET);
1443                int i;
1444                for (i = 0; i < 65536; i++)
1445                        putc(0, in);    // erase backup area
1446                fclose(in);
1447                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1448                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1449                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1450                eval("sync");
1451#elif HAVE_OPENRISC
1452#ifdef HAVE_ERC
1453                eval("cp", "-f", "/etc/defaults/nvram.bin",
1454                     "/usr/local/nvram/nvram.bin");
1455                eval("sync");
1456                eval("sync");
1457                sleep(5);
1458                eval("event", "5", "1", "15");
1459#endif
1460                eval("mount", "/usr/local", "-o", "remount,rw");
1461                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1462                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1463                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1464                // database
1465                eval("mount", "/usr/local", "-o", "remount,ro");
1466#elif HAVE_RB500
1467                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1468                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1469                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1470#elif HAVE_MAGICBOX
1471                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1472                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1473                eval("erase", "nvram");
1474#else
1475                eval("erase", "nvram");
1476#endif
1477#ifdef HAVE_BUFFALO_SA
1478                nvram_set("sv_restore_defaults", "1");
1479                if(region_sa)
1480                        nvram_set("region", "SA");
1481#endif
1482                sys_commit();
1483               
1484                action = REBOOT;
1485        }
1486
1487  /** Reboot **/
1488        else if (!strncmp(value, "Reboot", 6)) {
1489                action = REBOOT;
1490        }
1491
1492        /** GUI Logout **/// Experimental, not work yet ...
1493        else if (!strncmp(value, "Logout", 6)) {
1494                do_ej(NULL, "Logout.asp", wp, NULL);
1495                websDone(wp, 200);
1496                do_logout();
1497                return 1;
1498        }
1499
1500        /*
1501         * DEBUG : Invalid action
1502         */
1503        else
1504                websDebugWrite(wp, "Invalid action %s<br />", value);
1505
1506footer:
1507
1508        if (nvram_match("do_reboot", "1")) {
1509                action = REBOOT;
1510        }
1511        /*
1512         * The will let PC to re-get a new IP Address automatically
1513         */
1514        if (need_reboot)
1515                action = REBOOT;
1516
1517        if (!strcmp(value, "Apply")) {
1518                action = NOTHING;
1519        }
1520
1521        if (action != REBOOT) {
1522                if (my_next_page[0] != '\0')
1523                        sprintf(path, "%s", my_next_page);
1524                else {
1525                        next_page = websGetVar(wp, "next_page", NULL);
1526                        if (next_page)
1527                                sprintf(path, "%s", next_page);
1528                        else
1529                                sprintf(path, "%s.asp", submit_button);
1530                }
1531
1532                cprintf("refresh to %s\n", path);
1533                if (!strncmp(path, "WL_FilterTable", 14))
1534                        do_filtertable(NULL, path, wp, NULL);   // refresh
1535#ifdef HAVE_FREERADIUS
1536                else if (!strncmp(path, "FreeRadiusCert", 14))
1537                        do_radiuscert(NULL, path, wp, NULL);    // refresh     
1538#endif
1539                else if (!strncmp(path, "WL_ActiveTable", 14))
1540                        do_activetable(NULL, path, wp, NULL);   // refresh     
1541                else if (!strncmp(path, "Wireless_WDS", 12))
1542                        do_wds(NULL, path, wp, NULL);   // refresh
1543                else if (!strncmp(path, "Wireless_Advanced", 17))
1544                        do_wireless_adv(NULL, path, wp, NULL);  // refresh
1545                else
1546                        do_ej(NULL, path, wp, NULL);    // refresh
1547                websDone(wp, 200);
1548        } else {
1549#ifndef HAVE_WRK54G
1550                do_ej(NULL, "Reboot.asp", wp, NULL);
1551                websDone(wp, 200);
1552#endif
1553                // sleep (5);
1554                sys_reboot();
1555                return 1;
1556        }
1557
1558        nvram_set("upnp_wan_proto", "");
1559        sleep(sleep_time);
1560        if ((action == RESTART) || (action == SYS_RESTART))
1561                sys_restart();
1562        else if (action == SERVICE_RESTART)
1563                service_restart();
1564
1565        return 1;
1566
1567}
1568
1569//int auth_check( char *dirname, char *authorization )
1570int do_auth(webs_t wp, char *userid, char *passwd, char *realm,
1571            char *authorisation, int (*auth_check) (char *userid, char *passwd,
1572                                                    char *dirname,
1573                                                    char *authorisation))
1574{
1575        strncpy(userid, nvram_safe_get("http_username"), AUTH_MAX);
1576        strncpy(passwd, nvram_safe_get("http_passwd"), AUTH_MAX);
1577        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1578#if defined(HAVE_ERC) || defined(HAVE_IPR)
1579        strncpy(realm, "LOGIN", AUTH_MAX);
1580        wp->userid = 0;
1581        if (auth_check(userid, passwd, realm, authorisation))
1582                return 1;
1583        wp->userid = 1;
1584        strncpy(userid, zencrypt("SuperAdmin"), AUTH_MAX);
1585        strncpy(passwd, nvram_safe_get("newhttp_passwd"), AUTH_MAX);
1586        if (auth_check(userid, passwd, realm, authorisation))
1587                return 1;
1588        userid = 0;
1589#else
1590        wp->userid = 0;
1591        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1592        if (auth_check(userid, passwd, realm, authorisation))
1593                return 1;
1594#endif
1595        return 0;
1596}
1597
1598int do_cauth(webs_t wp, char *userid, char *passwd, char *realm,
1599             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1600                                                     char *dirname,
1601                                                     char *authorisation))
1602{
1603        if (nvram_match("info_passwd", "0"))
1604                return 1;
1605        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1606}
1607
1608#ifdef HAVE_REGISTER
1609int do_auth_reg(webs_t wp, char *userid, char *passwd, char *realm,
1610                char *authorisation, int (*auth_check) (char *userid,
1611                                                        char *passwd,
1612                                                        char *dirname,
1613                                                        char *authorisation))
1614{
1615        if (!isregistered())
1616                return 1;
1617        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1618}
1619#endif
1620
1621#undef HAVE_DDLAN
1622
1623#ifdef HAVE_DDLAN
1624int do_auth2(webs_t wp, char *userid, char *passwd, char *realm,
1625             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1626                                                     char *dirname,
1627                                                     char *authorisation))
1628{
1629        strncpy(userid, nvram_safe_get("http2_username"), AUTH_MAX);
1630        strncpy(passwd, nvram_safe_get("http2_passwd"), AUTH_MAX);
1631        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1632        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1633        if (auth_check(wp, userid, passwd, realm, authorisation))
1634                return 1;
1635        return 0;
1636}
1637#endif
1638// #ifdef EZC_SUPPORT
1639char ezc_version[128];
1640
1641// #endif
1642
1643extern int post;
1644
1645static char *post_buf = NULL;
1646void                            // support GET and POST 2003-08-22
1647do_apply_post(char *url, webs_t stream, int len, char *boundary)
1648{
1649        unsigned char buf[1024];
1650        int count;
1651        if (post == 1) {
1652                if (post_buf)
1653                        post_buf = (char *)realloc(post_buf, len + 1);
1654                else
1655                        post_buf = (char *)safe_malloc(len + 1);
1656
1657                if (!post_buf) {
1658                        cprintf("The POST data exceed length limit!\n");
1659                        return;
1660                }
1661                /*
1662                 * Get query
1663                 */
1664                if (!(count = wfread(post_buf, 1, len, stream)))
1665                        return;
1666                post_buf[count] = '\0';;
1667                len -= strlen(post_buf);
1668
1669                /*
1670                 * Slurp anything remaining in the request
1671                 */
1672                while (--len > 0)
1673#ifdef HAVE_HTTPS
1674                        if (do_ssl)
1675                                wfgets(buf, 1, stream);
1676                        else
1677#endif
1678                                (void)fgetc(stream->fp);
1679                init_cgi(post_buf);
1680        }
1681}
1682
1683#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
1684static void do_cfebackup(struct mime_handler *handler, char *url,
1685                         webs_t stream, char *query)
1686{
1687        system2("cat /dev/mtd/0 > /tmp/cfe.bin");
1688        do_file_attach(handler, "/tmp/cfe.bin", stream, NULL, "cfe.bin");
1689        unlink("/tmp/cfe.bin");
1690}
1691#endif
1692#ifdef HAVE_ROUTERSTYLE
1693static void do_stylecss(struct mime_handler *handler, char *url,
1694                        webs_t stream, char *query)
1695{
1696        char *style = nvram_get("router_style");
1697
1698        if (query != NULL)
1699                style = query;
1700
1701        long sdata[30];
1702        memset(sdata,0, sizeof(sdata));
1703        long blue[30] = {
1704                0x36f, 0xfff, 0x68f, 0x24d, 0x24d, 0x68f, 0x57f, 0xccf, 0x78f,
1705                0x35d,
1706                0x35c, 0x78f,
1707                0x78f, 0xfff, 0x9af, 0x46e, 0x46e, 0x9af, 0x36f,
1708                0xccf, 0xfff, 0x69f, 0xfff, 0xfff, 0x999, 0x69f,
1709                0x69f, 0xccf, 0x78f, 0xfff
1710        };
1711
1712        long cyan[30] = {
1713                0x099, 0xfff, 0x3bb, 0x066, 0x066, 0x3bb, 0x3bb, 0xcff, 0x4cc,
1714                0x1aa,
1715                0x1aa, 0x4cc,
1716                0x6cc, 0xfff, 0x8dd, 0x5bb, 0x5bb, 0x8dd, 0x099,
1717                0xcff, 0xfff, 0x3bb, 0xfff, 0xfff, 0x999, 0x3bb,
1718                0x3bb, 0xcff, 0x6cc, 0xfff
1719        };
1720
1721        long elegant[30] = {
1722                0x30519c, 0xfff, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7,
1723                0x496fc7, 0xfff, 0x6384cf, 0x6384cf, 0x6384cf,
1724                0x6384cf, 0x6384cf, 0xfff, 0x849dd9, 0x849dd9,
1725                0x849dd9, 0x849dd9, 0x30519c, 0xfff, 0xfff,
1726                0x496fc7, 0xfff, 0xfff, 0x999, 0x496fc7, 0x496fc7,
1727                0xfff, 0x6384cf, 0xfff
1728        };
1729
1730        long green[30] = {
1731                0x090, 0xfff, 0x3b3, 0x060, 0x060, 0x3b3, 0x3b3, 0xcfc, 0x4c4,
1732                0x1a1,
1733                0x1a1, 0x4c4,
1734                0x6c6, 0xfff, 0x8d8, 0x5b5, 0x5b5, 0x8d8, 0x090,
1735                0xcfc, 0xfff, 0x3b3, 0xfff, 0xfff, 0x999, 0x3b3,
1736                0x3b3, 0xcfc, 0x6c6, 0xfff
1737        };
1738
1739        long orange[30] = {
1740                0xf26522, 0xfff, 0xff8400, 0xff8400, 0xff8400, 0xff8400,
1741                0xff8400, 0xfff, 0xfeb311, 0xfeb311, 0xfeb311,
1742                0xfeb311, 0xff9000, 0xfff, 0xffa200, 0xffa200,
1743                0xffa200, 0xffa200, 0xf26522, 0xfff, 0xfff,
1744                0xff8400, 0xfff, 0xfff, 0x999, 0xff8400, 0xff8400,
1745                0xfff, 0xff9000, 0xfff
1746        };
1747
1748        long red[30] = {
1749                0xc00, 0xfff, 0xe33, 0x800, 0x800, 0xe33, 0xd55, 0xfcc, 0xe77,
1750                0xc44,
1751                0xc44, 0xe77,
1752                0xe77, 0xfff, 0xf99, 0xd55, 0xd55, 0xf99, 0xc00,
1753                0xfcc, 0xfff, 0xd55, 0xfff, 0xfff, 0x999, 0xd55,
1754                0xd55, 0xfcc, 0xe77, 0xfff
1755        };
1756
1757        long yellow[30] = {
1758                0xeec900, 0x000, 0xee3, 0x880, 0x880, 0xee3, 0xffd700, 0x660,
1759                0xee7,
1760                0xbb4,
1761                0xbb4, 0xee7,
1762                0xeec900, 0x000, 0xff9, 0xcc5, 0xcc5, 0xff9,
1763                0xeec900, 0x660, 0x000, 0xffd700, 0x000, 0xfff,
1764                0x999, 0xffd700, 0xeec900, 0x660, 0xffd700, 0x000
1765        };
1766
1767        if (!strcmp(style, "blue"))
1768                memcpy(sdata, blue, sizeof(blue));
1769        else if (!strcmp(style, "cyan"))
1770                memcpy(sdata, cyan, sizeof(cyan));
1771        else if (!strcmp(style, "yellow"))
1772                memcpy(sdata, yellow, sizeof(yellow));
1773        else if (!strcmp(style, "green"))
1774                memcpy(sdata, green, sizeof(green));
1775        else if (!strcmp(style, "orange"))
1776                memcpy(sdata, orange, sizeof(orange));
1777        else if (!strcmp(style, "red"))
1778                memcpy(sdata, red, sizeof(red));
1779        else                    // default to elegant
1780                memcpy(sdata, elegant, sizeof(elegant));
1781
1782        websWrite(stream,
1783                  "@import url(../common.css);\n#menuSub,\n#menuMainList li span,\n#help h2 {\nbackground:#%03x;\n",
1784                  sdata[0]);
1785        websWrite(stream, "color:#%03x;\n", sdata[1]);
1786        websWrite(stream,
1787                  "border-color:#%03x #%03x #%03x #%03x;\n}\n#menuSubList li a {\n",
1788                  sdata[2], sdata[3], sdata[4], sdata[5]);
1789        websWrite(stream, "background:#%03x;\n", sdata[6]);
1790        websWrite(stream, "color:#%03x;\n", sdata[7]);
1791        websWrite(stream,
1792                  "border-color:#%03x #%03x #%03x #%03x;\n}\n#menuSubList li a:hover {\n",
1793                  sdata[8], sdata[9], sdata[10], sdata[11]);
1794        websWrite(stream, "background:#%03x;\n", sdata[12]);
1795        websWrite(stream, "color:#%03x;\n", sdata[13]);
1796        websWrite(stream,
1797                  "border-color:#%03x #%03x #%03x #%03x;\n}\nfieldset legend {\n",
1798                  sdata[14], sdata[15], sdata[16], sdata[17]);
1799        websWrite(stream, "color:#%03x;\n}\n#help a {\n", sdata[18]);
1800        websWrite(stream, "color:#%03x;\n}\n#help a:hover {\n", sdata[19]);
1801        websWrite(stream, "color:#%03x;\n}\n.meter .bar {\n", sdata[20]);
1802        websWrite(stream, "background-color: #%03x;\n}\n.meter .text {\n",
1803                  sdata[21]);
1804        websWrite(stream, "color:#%03x;\n}\n.progressbar {\n", sdata[22]);
1805        websWrite(stream, "background-color: #%03x;\n", sdata[23]);
1806        websWrite(stream,
1807                  "border-color: #%03x;\nfont-size:.09em;\nborder-width:.09em;\n}\n.progressbarblock {\n",
1808                  sdata[24]);
1809        websWrite(stream,
1810                  "background-color: #%03x;\nfont-size:.09em;\n}\ninput.button {\n",
1811                  sdata[25]);
1812        websWrite(stream, "background: #%03x;\n", sdata[26]);
1813        websWrite(stream, "color: #%03x;\n}\ninput.button:hover {\n",
1814                  sdata[27]);
1815        websWrite(stream, "background: #%03x;\n", sdata[28]);
1816        websWrite(stream, "color: #%03x;\n}\n", sdata[29]);
1817
1818}
1819
1820static void do_stylecss_ie(struct mime_handler *handler, char *url,
1821                           webs_t stream, char *query)
1822{
1823        websWrite(stream, ".submitFooter input {\n"
1824                  "padding:.362em .453em;\n"
1825                  "}\n"
1826                  "fieldset {\n"
1827                  "padding-top:0;\n"
1828                  "}\n"
1829                  "fieldset legend {\n"
1830                  "margin-left:-9px;\n"
1831                  "margin-bottom:8px;\n" "padding:0 .09em;\n" "}\n");
1832}
1833#endif
1834#ifdef HAVE_REGISTER
1835static void do_trial_logo(struct mime_handler *handler, char *url,
1836                          webs_t stream, char *query)
1837{
1838#if defined(HAVE_TRIMAX) || defined(HAVE_MAKSAT) || defined(HAVE_VILIM) || defined(HAVE_TELCOM) || defined(HAVE_WIKINGS) || defined(HAVE_NEXTMEDIA)
1839        do_file(handler, url, stream, query);
1840#else
1841        if (!isregistered_real()) {
1842                do_file(handler, "style/logo-trial.png", stream, query);
1843        } else {
1844                if (iscpe()) {
1845                        do_file(handler, "style/logo-cpe.png", stream, query);
1846                } else {
1847                        do_file(handler, url, stream, query);
1848                }
1849        }
1850#endif
1851}
1852
1853#endif
1854/*
1855 * static void do_style (char *url, webs_t stream, char *query) { char *style
1856 * = nvram_get ("router_style"); if (style == NULL || strlen (style) == 0)
1857 * do_file ("kromo.css", stream, NULL); else do_file (style, stream, NULL); }
1858 */
1859
1860static void do_mypage(struct mime_handler *handler, char *url,
1861                      webs_t stream, char *query)
1862{
1863        char *snamelist = nvram_safe_get("mypage_scripts");
1864        char *next;
1865        char sname[128];
1866        int qnum;
1867        int i = 1;
1868
1869        if (query == NULL || strlen(query) == 0)
1870                qnum = 1;
1871        else
1872                qnum = atoi(query);
1873
1874        foreach(sname, snamelist, next) {
1875                if (qnum == i) {
1876                        strcat(sname, " > /tmp/mypage.tmp");
1877                        system2(sname);
1878                        do_file_attach(handler, "/tmp/mypage.tmp", stream, NULL,
1879                                       "MyPage.asp");
1880                        unlink("/tmp/mypage.tmp");
1881                }
1882                i++;
1883        }
1884
1885        return;
1886
1887}
1888
1889static void do_fetchif(struct mime_handler *handler, char *url,
1890                       webs_t stream, char *query)
1891{
1892        char line[256];
1893        int i, llen;
1894        char buffer[256];
1895        char querybuffer[32];
1896
1897        if (query == NULL || strlen(query) == 0)
1898                return;
1899
1900        strncpy(querybuffer, query, 30);
1901        strcat(querybuffer, ":");
1902
1903        int strbuffer = 0;
1904        time_t tm;
1905        struct tm tm_time;
1906
1907        time(&tm);
1908        memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
1909        char *date_fmt = "%a %b %e %H:%M:%S %Z %Y";
1910
1911        strftime(buffer, 200, date_fmt, &tm_time);
1912        strbuffer = strlen(buffer);
1913        buffer[strbuffer++] = '\n';
1914        FILE *in = fopen("/proc/net/dev", "rb");
1915
1916        if (in == NULL)
1917                return;
1918
1919        /* eat first two lines */
1920        fgets(line, sizeof(line), in);
1921        fgets(line, sizeof(line), in);
1922
1923        while (fgets(line, sizeof(line), in) != NULL) {
1924                if (strstr(line, querybuffer)) {
1925                        llen = strlen(line);
1926                        for (i = 0; i < llen; i++) {
1927                                buffer[strbuffer++] = line[i];
1928                        }
1929                        break;
1930                }
1931        }
1932        fclose(in);
1933
1934        buffer[strbuffer] = 0;
1935        websWrite(stream, "%s", buffer);
1936}
1937
1938static char *getLanguageName()
1939{
1940        char *lang = nvram_get("language");
1941
1942        cprintf("get language %s\n", lang);
1943        char *l = safe_malloc(60);
1944
1945        if (lang == NULL) {
1946                cprintf("return default\n");
1947                sprintf(l, "lang_pack/english.js");
1948                return l;
1949        }
1950        sprintf(l, "lang_pack/%s.js", lang);
1951        cprintf("return %s\n", l);
1952        return l;
1953}
1954
1955char *live_translate(char *tran)
1956{
1957
1958        FILE *fp;
1959        static char temp[256], temp1[256];
1960        char *temp2;
1961        if (tran == NULL || !strlen(tran))
1962                return "";
1963        char *lang = getLanguageName();
1964        char buf[64];
1965        int start, filelen, pos;
1966
1967        memset(temp, 0, sizeof(temp));
1968        memset(temp1, 0, sizeof(temp));
1969
1970        sprintf(buf, "%s", lang);
1971        free(lang);
1972
1973        strcpy(temp1, tran);
1974        strcat(temp1, "=\"");
1975
1976        int len = strlen(temp1);
1977
1978        fp = getWebsFile(buf);
1979        if (fp) {
1980                start = ftell(fp);
1981                filelen = getWebsFileLen(buf);
1982
1983                while (fgets(temp, 256, fp) != NULL) {
1984                        pos = ftell(fp);
1985
1986                        if ((pos - start) > filelen)
1987                                break;
1988                        if ((memcmp(temp, temp1, len)) == 0) {
1989                                temp2 = strtok(temp, "\"");
1990                                temp2 = strtok(NULL, "\"");
1991
1992                                fclose(fp);
1993                                return temp2;
1994                        }
1995                }
1996                fclose(fp);
1997        }
1998
1999        strcpy(buf, "lang_pack/english.js");    // if string not found, try english
2000        fp = getWebsFile(buf);
2001
2002        if (fp == NULL)
2003                return "Error";
2004        start = ftell(fp);
2005        filelen = getWebsFileLen(buf);
2006
2007        while (fgets(temp, 256, fp) != NULL) {
2008                pos = ftell(fp);
2009
2010                if ((pos - start) > filelen)
2011                        break;
2012                if ((memcmp(temp, temp1, len)) == 0) {
2013                        temp2 = strtok(temp, "\"");
2014                        temp2 = strtok(NULL, "\"");
2015
2016                        fclose(fp);
2017                        return temp2;
2018                }
2019        }
2020        fclose(fp);
2021
2022        return "Error";         // not found
2023
2024}
2025
2026static void do_ttgraph(struct mime_handler *handler, char *url,
2027                       webs_t stream, char *query)
2028{
2029#define COL_WIDTH 16            /* single column width */
2030
2031        char *next;
2032        char var[80];
2033
2034        unsigned int days;
2035        unsigned int month;
2036        unsigned int year;
2037        int wd;
2038        int i = 0;
2039        char months[12][12] = {
2040                "share.jan", "share.feb", "share.mar", "share.apr", "share.may",
2041                "share.jun",
2042                "share.jul", "share.aug", "share.sep", "share.oct",
2043                "share.nov", "share.dec"
2044        };
2045        unsigned long rcvd[31] = {
2046                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2047                0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2048        };
2049        unsigned long sent[31] = {
2050                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2051                0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2052        };
2053        unsigned long max = 5, smax = 5, f = 1;
2054        unsigned long totin = 0;
2055        unsigned long totout = 0;
2056
2057        if (sscanf(query, "%u-%u", &month, &year) != 2)
2058                return;
2059        if (month < 1 || month > 12)
2060                return;
2061
2062        days = daysformonth(month, year);
2063        wd = weekday(month, 1, year);   // first day in month (mon=0, tue=1,
2064        // ..., sun=6)
2065
2066        char tq[32];
2067
2068        sprintf(tq, "traff-%02u-%u", month, year);
2069        char *tdata = nvram_safe_get(tq);
2070
2071        if (tdata != NULL && strlen(tdata)) {
2072                foreach(var, tdata, next) {
2073                        if (i == days)
2074                                break;  //skip monthly total
2075                        int ret = sscanf(var, "%lu:%lu", &rcvd[i], &sent[i]);
2076                        if (ret != 2)
2077                                break;
2078                        totin += rcvd[i];
2079                        totout += sent[i];
2080                        if (rcvd[i] > max)
2081                                max = rcvd[i];
2082                        if (sent[i] > max)
2083                                max = sent[i];
2084                        i++;
2085                }
2086        }
2087
2088        while (max > smax) {
2089                if (max > (f * 5))
2090                        smax = f * 10;
2091                if (max > (f * 10))
2092                        smax = f * 25;
2093                if (max > (f * 25))
2094                        smax = f * 50;
2095                f = f * 10;
2096        }
2097
2098        char incom[32];
2099
2100        snprintf(incom, 32, "%s", live_translate("status_inet.traffin"));
2101        char outcom[32];
2102
2103        snprintf(outcom, 32, "%s", live_translate("status_inet.traffout"));
2104        char monthname[32];
2105
2106        snprintf(monthname, 32, "%s", live_translate(months[month - 1]));
2107
2108        websWrite(stream,
2109                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
2110                  "<html>\n"
2111                  "<head>\n"
2112                  "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
2113                  live_translate("lang_charset.set"));
2114
2115        websWrite(stream, "<title>dd-wrt traffic graph</title>\n"
2116                  "<script type=\"text/javascript\">\n"
2117                  "//<![CDATA[\n"
2118                  "function Show(label) {\n"
2119                  "document.getElementById(\"label\").innerHTML = label;\n"
2120                  "}\n"
2121                  "//]]>\n"
2122                  "</script>\n"
2123                  "<style type=\"text/css\">\n\n"
2124                  "#t-graph {position: relative; width: %upx; height: 300px;\n",
2125                  days * COL_WIDTH);
2126        websWrite(stream, "  margin: 1.1em 0 3.5em; padding: 0;\n"
2127                  "  border: 1px solid gray; list-style: none;\n"
2128                  "  font: 9px Tahoma, Arial, sans-serif;}\n"
2129                  "#t-graph ul {margin: 0; padding: 0; list-style: none;}\n"
2130                  "#t-graph li {position: absolute; bottom: 0; width: %dpx; z-index: 2;\n",
2131                  COL_WIDTH);
2132        websWrite(stream, "  margin: 0; padding: 0;\n"
2133                  "  text-align: center; list-style: none;}\n"
2134                  "#t-graph li.day {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #AAA;}\n"
2135                  "#t-graph li.day_sun {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #E00;}\n"
2136                  "#t-graph li.bar {width: 4px; border: 1px solid; border-bottom: none; color: #000;}\n"
2137                  "#t-graph li.bar p {margin: 5px 0 0; padding: 0;}\n"
2138                  "#t-graph li.rcvd {left: 3px; background: #228B22;}\n"
2139                  "#t-graph li.sent {left: 8px; background: #CD0000;}\n");
2140
2141        for (i = 0; i < days - 1; i++) {
2142                websWrite(stream, "#t-graph #d%d {left: %dpx;}\n", i + 1,
2143                          i * COL_WIDTH);
2144        }
2145        websWrite(stream, "#t-graph #d%u {left: %upx; border-right: none;}\n",
2146                  days, (days - 1) * COL_WIDTH);
2147
2148        websWrite(stream,
2149                  "#t-graph #ticks {width: %upx; height: 300px; z-index: 1;}\n",
2150                  days * COL_WIDTH);
2151        websWrite(stream,
2152                  "#t-graph #ticks .tick {position: relative; border-bottom: 1px solid #BBB; width: %upx;}\n",
2153                  days * COL_WIDTH);
2154        websWrite(stream,
2155                  "#t-graph #ticks .tick p {position: absolute; left: 100%%; top: -0.67em; margin: 0 0 0 0.5em;}\n"
2156                  "#t-graph #label {width: 500px; bottom: -20px;  z-index: 1; font: 12px Tahoma, Arial, sans-serif; font-weight: bold;}\n"
2157                  "</style>\n"
2158                  "</head>\n\n" "<body>\n" "<ul id=\"t-graph\">\n");
2159
2160        for (i = 0; i < days; i++) {
2161                websWrite(stream, "<li class=\"day%s\" id=\"d%d\" ",
2162                          (wd % 7) == 6 ? "_sun" : "", i + 1);
2163                wd++;
2164                websWrite(stream,
2165                          "onmouseover=\"Show(\'%s %d, %d (%s: %lu MB / %s: %lu MB)\')\" ",
2166                          monthname, i + 1, year, incom, rcvd[i], outcom,
2167                          sent[i]);
2168                websWrite(stream,
2169                          "onmouseout=\"Show(\'%s %d (%s: %lu MB / %s: %lu MB)\')\"",
2170                          monthname, year, incom, totin, outcom, totout);
2171                websWrite(stream, ">%d\n", i + 1);
2172                websWrite(stream, "<ul>\n");
2173                websWrite(stream,
2174                          "<li class=\"rcvd bar\" style=\"height: %lupx;\"><p></p></li>\n",
2175                          rcvd[i] * 300 / smax);
2176                websWrite(stream,
2177                          "<li class=\"sent bar\" style=\"height: %lupx;\"><p></p></li>\n",
2178                          sent[i] * 300 / smax);
2179                websWrite(stream, "</ul>\n");
2180                websWrite(stream, "</li>\n");
2181        }
2182
2183        websWrite(stream, "<li id=\"ticks\">\n");
2184        for (i = 5; i; i--)     // scale
2185        {
2186                websWrite(stream,
2187                          "<div class=\"tick\" style=\"height: 59px;\"><p>%d%sMB</p></div>\n",
2188                          smax * i / 5, (smax > 10000) ? " " : "&nbsp;");
2189        }
2190        websWrite(stream, "</li>\n\n");
2191
2192        websWrite(stream, "<li id=\"label\">\n");
2193        websWrite(stream, "%s %d (%s: %lu MB / %s: %lu MB)\n", monthname, year,
2194                  incom, totin, outcom, totout);
2195        websWrite(stream, "</li>\n" "</ul>\n\n" "</body>\n" "</html>\n");
2196
2197}
2198
2199static void ttraff_backup(struct mime_handler *handler, char *url,
2200                          webs_t stream, char *query)
2201{
2202        system2("echo TRAFF-DATA > /tmp/traffdata.bak");
2203        system2("nvram show | grep traff- >> /tmp/traffdata.bak");
2204        do_file_attach(handler, "/tmp/traffdata.bak", stream, NULL,
2205                       "traffdata.bak");
2206        unlink("/tmp/traffdata.bak");
2207} static void do_apply_cgi(struct mime_handler *handler, char *url,
2208                           webs_t stream, char *q)
2209{
2210        char *path, *query;
2211
2212        if (post == 1) {
2213                query = post_buf;
2214                path = url;
2215        } else {
2216                query = url;
2217                path = strsep(&query, "?") ? : url;
2218                init_cgi(query);
2219        }
2220
2221        if (!query)
2222                return;
2223
2224        apply_cgi(stream, NULL, NULL, 0, url, path, query);
2225        init_cgi(NULL);
2226}
2227
2228#ifdef HAVE_MADWIFI
2229extern int getdevicecount(void);
2230#endif
2231
2232#ifdef HAVE_LANGUAGE
2233static void do_language(struct mime_handler *handler, char *path, webs_t stream, char *query)   // jimmy,
2234                                                                        // https,
2235                                                                        // 8/4/2003
2236{
2237        char *lang = getLanguageName();
2238
2239        do_file(handler, lang, stream, NULL);
2240        free(lang);
2241        return;
2242}
2243#endif
2244extern int issuperchannel(void);
2245
2246static char no_cache[] =
2247    "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "Expires: 0";
2248
2249struct mime_handler mime_handlers[] = {
2250        // { "ezconfig.asp", "text/html", ezc_version, do_apply_ezconfig_post,
2251        // do_ezconfig_asp, do_auth },
2252#ifdef HAVE_SKYTRON
2253        {"setupindex*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2254#endif
2255#ifdef HAVE_POKER
2256        {"PokerEdit.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2257#endif
2258#ifdef HAVE_DDLAN
2259        {"Upgrade*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2260        {"Management*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2261        {"Services*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2262        {"Hotspot*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2263        {"Wireless*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2264        {"WL_*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2265        {"WPA*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2266        {"Log*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2267        {"Alive*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2268        {"Diagnostics*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2269        {"Wol*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2270        {"Factory_Defaults*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2271        {"config*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2272#endif
2273
2274        {"changepass.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2275#ifdef HAVE_REGISTER
2276        {"register.asp", "text/html", no_cache, NULL, do_ej, do_auth_reg, 1},
2277#endif
2278        {"WL_FilterTable*", "text/html", no_cache, NULL, do_filtertable,
2279         do_auth, 1},
2280#ifdef HAVE_FREERADIUS
2281        {"FreeRadiusCert*", "text/html", no_cache, NULL, do_radiuscert, do_auth,
2282         1},
2283        {"freeradius-certs/*", "application/octet-stream", no_cache, NULL,
2284         cert_file_out, do_auth, 0},
2285#endif
2286        // #endif
2287        // #ifdef HAVE_MADWIFI
2288        {"Wireless_WDS*", "text/html", no_cache, NULL, do_wds, do_auth, 1},
2289        {"WL_ActiveTable*", "text/html", no_cache, NULL, do_activetable,
2290         do_auth, 1},
2291        {"Wireless_Advanced*", "text/html", no_cache, NULL, do_wireless_adv,
2292         do_auth, 1},
2293        // #endif
2294        {"MyPage.asp*", "text/html", no_cache, NULL, do_mypage, do_auth, 1},
2295        {"**.asp", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2296        {"**.JPG", "image/jpeg", no_cache, NULL, do_file, NULL, 0},
2297        // {"style.css", "text/css", NULL, NULL, do_style, NULL},
2298        {"common.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2299#ifdef HAVE_LANGUAGE
2300        {"lang_pack/language.js", "text/javascript", NULL, NULL, do_language,
2301         NULL, 0},
2302#endif
2303#ifdef HAVE_BUFFALO
2304        {"vsp.html", "text/plain", no_cache, NULL, do_vsp_page, NULL, 1},
2305#endif
2306        {"SysInfo.htm*", "text/plain", no_cache, NULL, do_ej, do_auth, 1},
2307#ifdef HAVE_SKYTRON
2308        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2309        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2310        {"**.htm", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2311        {"**.html", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2312#else
2313        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2314        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2315        {"**.htm", "text/html", no_cache, NULL, do_ej, NULL, 1},
2316        {"**.html", "text/html", no_cache, NULL, do_ej, NULL, 1},
2317
2318#endif
2319#ifdef HAVE_ROUTERSTYLE
2320        {"style/blue/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2321        {"style/cyan/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2322        {"style/elegant/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2323         1},
2324        {"style/green/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2325        {"style/orange/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2326         1},
2327        {"style/red/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2328        {"style/yellow/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2329         1},
2330        {"style/blue/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2331         NULL,
2332         1},
2333        {"style/cyan/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2334         NULL,
2335         1},
2336        {"style/elegant/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2337         NULL, 1},
2338        {"style/green/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2339         NULL,
2340         1},
2341        {"style/orange/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2342         NULL, 1},
2343        {"style/red/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie, NULL,
2344         1},
2345        {"style/yellow/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2346         NULL, 1},
2347#endif
2348#ifdef HAVE_REGISTER
2349        {"style/logo.png", "image/png", NULL, NULL, do_trial_logo, NULL, 0},
2350#endif
2351        {"**.css", "text/css", NULL, NULL, do_file, NULL, 0},
2352        {"**.svg", "image/svg+xml", NULL, NULL, do_file, NULL, 0},
2353        {"**.gif", "image/gif", NULL, NULL, do_file, NULL, 0},
2354        {"**.png", "image/png", NULL, NULL, do_file, NULL, 0},
2355        {"**.jpg", "image/jpeg", NULL, NULL, do_file, NULL, 0},
2356        {"**.ico", "image/x-icon", NULL, NULL, do_file, NULL, 0},
2357        {"**.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2358        {"**.swf", "application/x-shockwave-flash", NULL, NULL, do_file, NULL,
2359         0},
2360        {"**.pdf", "application/pdf", NULL, NULL, do_file, NULL, 0},
2361        {"**.mp4", "video/mp4", NULL, NULL, do_file, NULL, 0},
2362        {"**.mp3", "audio/mpeg3", NULL, NULL, do_file, NULL, 0},
2363        {"**.mpg", "video/mpeg", NULL, NULL, do_file, NULL, 0},
2364        {"**.avi", "video/x-msvideo", NULL, NULL, do_file, NULL, 0},
2365        {"**.wma", "audio/x-ms-wma", NULL, NULL, do_file, NULL, 0},
2366        {"**.wmv", "video/x-ms-wmv", NULL, NULL, do_file, NULL, 0},
2367        {"**.flv", "video/x-flv", NULL, NULL, do_file, NULL, 0},
2368#ifdef HAVE_SKYTRON
2369        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2370         do_auth2, 1},
2371#elif HAVE_DDLAN
2372        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2373         NULL, 1},
2374#else
2375        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2376         do_auth, 1},
2377#endif
2378        {"fetchif.cgi*", "text/html", no_cache, NULL, do_fetchif, do_auth, 1},
2379#ifdef HAVE_DDLAN
2380        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi, NULL,
2381         1},
2382        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2383         NULL, 1},
2384#else
2385        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2386         do_auth, 1},
2387        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2388         do_auth, 1},
2389#endif
2390#ifdef HAVE_BUFFALO
2391        {"olupgrade.cgi*", "text/html", no_cache, do_olupgrade_post,
2392         do_upgrade_cgi,
2393         do_auth, 1},
2394#endif
2395        // {"Gozila.cgi*", "text/html", no_cache, NULL, do_setup_wizard,
2396        // do_auth}, // for setup wizard
2397        /*
2398         * { "**.cfg", "application/octet-stream", no_cache, NULL, do_backup,
2399         * do_auth },
2400         */
2401#ifdef HAVE_DDLAN
2402        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2403         do_upgrade_cgi,
2404         NULL, 1},
2405#else
2406        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2407         do_upgrade_cgi,
2408         do_auth, 1},
2409#endif
2410        {"test.bin**", "application/octet-stream", no_cache, NULL, do_file,
2411         do_auth, 0},
2412
2413#ifdef HAVE_DDLAN
2414        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2415         nv_file_out, do_auth2, 0},
2416        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2417         nv_file_out,
2418         do_auth2, 0},
2419        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi, NULL,
2420         1},
2421#else
2422        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2423         nv_file_out, do_auth, 0},
2424        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2425         nv_file_out,
2426         do_auth, 0},
2427        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi,
2428         do_auth,
2429         1},
2430#endif
2431#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
2432        {"backup/cfe.bin", "application/octet-stream", no_cache, NULL,
2433         do_cfebackup,
2434         do_auth, 0},
2435#endif
2436        {"ttgraph.cgi*", "text/html", no_cache, NULL, do_ttgraph, do_auth, 1},
2437        {"traffdata.bak*", "text/html", no_cache, NULL, ttraff_backup,
2438         do_auth, 0},
2439        {"tadmin.cgi*", "text/html", no_cache, td_file_in, td_config_cgi,
2440         NULL, 1},
2441        {"*", "application/octet-stream", no_cache, NULL, do_file, do_auth, 1},
2442        // for ddm
2443        {NULL, NULL, NULL, NULL, NULL, NULL, 0}
2444};
2445
2446/*
2447 * Format: type = SET : " " => "&nbsp;" , ":" => "&semi;" type = GET :
2448 * "&nbsp;" => " " , "&semi;" => ":" Example: name1 = test 123:abc
2449 * filter_name("name1", new_name, SET); new_name="test&nbsp;123&semi;abc"
2450 * name2 = test&nbsp;123&semi;abc filter_name("name2", new_name, GET);
2451 * new_name="test 123:abc"
2452 */
2453int httpd_filter_name(char *old_name, char *new_name, size_t size, int type)
2454{
2455        int i, j, match;
2456
2457        cprintf("httpd_filter_name\n");
2458
2459        struct pattern {
2460                char ch;
2461                char *string;
2462        };
2463
2464        struct pattern patterns[] = {
2465                {' ', "&nbsp;"},
2466                {':', "&semi;"},
2467                {'<', "&lt;"},
2468                {'>', "&gt;"},
2469        };
2470
2471        struct pattern *v;
2472
2473        strcpy(new_name, "");
2474
2475        switch (type) {
2476        case SET:
2477                for (i = 0; *(old_name + i); i++) {
2478                        match = 0;
2479                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2480                             v++) {
2481                                if (*(old_name + i) == v->ch) {
2482                                        if (strlen(new_name) + strlen(v->string) > size) {      // avoid overflow
2483                                                cprintf("%s(): overflow\n",
2484                                                        __FUNCTION__);
2485                                                new_name[strlen(new_name)] =
2486                                                    '\0';
2487                                                return 1;
2488                                        }
2489                                        sprintf(new_name + strlen(new_name),
2490                                                "%s", v->string);
2491                                        match = 1;
2492                                        break;
2493                                }
2494                        }
2495                        if (!match) {
2496                                if (strlen(new_name) + 1 > size) {
2497                                        cprintf("%s(): overflow\n", __FUNCTION__);      // avoid
2498                                        // overflow
2499                                        new_name[strlen(new_name)] = '\0';
2500                                        return 1;
2501                                }
2502                                sprintf(new_name + strlen(new_name), "%c",
2503                                        *(old_name + i));
2504                        }
2505                }
2506
2507                break;
2508        case GET:
2509                for (i = 0, j = 0; *(old_name + j); j++) {
2510                        match = 0;
2511                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2512                             v++) {
2513                                if (!memcmp
2514                                    (old_name + j, v->string,
2515                                     strlen(v->string))) {
2516                                        *(new_name + i) = v->ch;
2517                                        j = j + strlen(v->string) - 1;
2518                                        match = 1;
2519                                        break;
2520                                }
2521                        }
2522                        if (!match)
2523                                *(new_name + i) = *(old_name + j);
2524
2525                        i++;
2526                }
2527                *(new_name + i) = '\0';
2528                break;
2529        default:
2530                cprintf("%s():Invalid type!\n", __FUNCTION__);
2531                break;
2532        }
2533        // cprintf("%s():new_name=[%s]\n", __FUNCTION__, new_name);
2534
2535        return 1;
2536}
2537
2538#ifdef HAVE_BUFFALO
2539void do_vsp_page(struct mime_handler *handler, char *url,
2540                 webs_t stream, char *query)
2541{
2542/*
2543#ifdef HAVE_MADWIFI
2544        char *ifname = "ath0";
2545#else
2546        char *ifname = "wl0";
2547#endif
2548
2549        char *authmode = nvram_nget("%s_security_mode", ifname);
2550        char *encrypt = nvram_nget("%s_crypto", ifname);
2551        char *wpakey = nvram_nget("%s_wpa_psk ", ifname);
2552
2553        if (!strcmp(encrypt, "tkip")) {
2554                encrypt = "TKIP";
2555        } else if (!strcmp(authmode, "aes")) {
2556                encrypt = "AES";
2557        } else if (!strcmp(authmode, "tkip+aes")) {
2558                encrypt = "TKIP-AES-MIX";
2559        } else {
2560                encrypt = "";
2561        }
2562
2563        if (!strcmp(authmode, "disabled")) {
2564                authmode = "NONE";
2565                encrypt = "";
2566                wpakey = "";
2567        } else if (!strcmp(authmode, "wep")) {
2568                authmode = "WEP";
2569                encrypt = "";
2570                wpakey = "";
2571        } else if (!strcmp(authmode, "radius")
2572                   || !strcmp(authmode, "wpa")
2573                   || !strcmp(authmode, "wpa2")
2574                   || !strcmp(authmode, "wpa wpa2")) {
2575                authmode = "RADIUS";
2576                encrypt = "";
2577                wpakey = "";
2578        } else if (!strcmp(authmode, "8021X")) {
2579                authmode = "802.1X";
2580                encrypt = "";
2581                wpakey = "";
2582        } else if (!strcmp(authmode, "psk")) {
2583                authmode = "WPA";
2584        } else if (!strcmp(authmode, "psk2")) {
2585                authmode = "WPA2";
2586        } else if (!strcmp(authmode, "psk psk2")) {
2587                authmode = "WPA-WPA2-MIX";
2588        } else {
2589                authmode = "UNKNOWN";
2590                encrypt = "";
2591                wpakey = "";
2592        }
2593*/
2594        websWrite(stream, "<html>\n");
2595        websWrite(stream, "<head>\n");
2596        websWrite(stream, "<title>VSP</title>\n");
2597        websWrite(stream, "</head>\n");
2598        websWrite(stream, "<body>\n");
2599        websWrite(stream, "<pre>\n");
2600
2601        websWrite(stream, "DEVICE_VSP_VERSION=0.1\n");
2602        websWrite(stream, "DEVICE_VENDOR=BUFFALO INC.\n");
2603        websWrite(stream, "DEVICE_MODEL=%s DDWRT\n",
2604                  nvram_safe_get("DD_BOARD"));
2605        websWrite(stream, "DEVICE_FIRMWARE_VERSION=1.00\n");
2606        char *reg = getUEnv("region");
2607        if (!reg)
2608                reg = "US";
2609        websWrite(stream, "DEVICE_REGION=%s\n", reg);
2610        websWrite(stream, "WIRELESS_DEVICE_NUMBER=1\n");
2611//      websWrite(stream, "WIRELESS_1_PRESET_AUTHMODE=%s\n", authmode);
2612//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT=%s\n", encrypt);
2613//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT_KEY=%s\n", wpakey);
2614        websWrite(stream, "DEVICE_URL_GET=/vsp.html\n");
2615        websWrite(stream, "DEVICE_URL_SET=/vsp.html\n");
2616
2617        websWrite(stream, "</pre>\n");
2618        websWrite(stream, "</body>\n");
2619        websWrite(stream, "</html>\n");
2620}
2621#endif
Note: See TracBrowser for help on using the repository browser.