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

Last change on this file since 17887 was 17887, checked in by BrainSlayer, 18 months ago

use classic way

File size: 72.8 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, NULL},
1215        {"AnchorFree", "anchorfree", 0, SERVICE_RESTART, NULL},
1216        {"Nintendo", "nintendo", 0, SERVICE_RESTART, NULL},
1217
1218        /*
1219         * APP & GAMING
1220         */
1221        {"Forward", "forward", 0, SERVICE_RESTART, NULL},
1222        {"ForwardSpec", "forward", 0, SERVICE_RESTART, NULL},
1223        {"Triggering", "filters", 0, SERVICE_RESTART, NULL},
1224        {"DMZ", "filters", 0, SERVICE_RESTART, NULL},
1225        {"Filters", "filters", 0, SERVICE_RESTART, NULL},
1226        {"FilterIPMAC", "filters", 0, SERVICE_RESTART, NULL},
1227#ifdef HAVE_UPNP
1228        {"UPnP", "forward_upnp", 0, SERVICE_RESTART, "tf_upnp"},
1229#endif
1230        /*
1231         * SECURITY
1232         */
1233        {"Firewall", "filters", 0, SERVICE_RESTART, NULL},
1234        {"VPN", "filters", 0, SERVICE_RESTART, NULL},
1235#ifdef HAVE_MILKFISH
1236        {"Milkfish", "milkfish", 0, SERVICE_RESTART, NULL},
1237#endif
1238        /*
1239         * Obsolete {"WL_WEPTable", "", 0, SERVICE_RESTART, NULL}, {"Security",
1240         * "", 1, RESTART, NULL}, {"System", "", 0, RESTART, NULL}, {"DHCP",
1241         * "dhcp", 0, SERVICE_RESTART, NULL}, {"FilterIP", "filters", 0,
1242         * SERVICE_RESTART, NULL}, {"FilterMAC", "filters", 0, SERVICE_RESTART,
1243         * NULL}, {"FilterPort", "filters", 0, SERVICE_RESTART, NULL},
1244         * {"Wireless", "wireless", 0, SERVICE_RESTART, NULL}, {"Log", "logging",
1245         * 0, SERVICE_RESTART, NULL}, //moved to Firewall {"QoS", "qos", 0,
1246         * SERVICE_RESTART, NULL}, //gozilla does the save
1247         */
1248
1249};
1250
1251struct apply_action *handle_apply_action(char *name)
1252{
1253        struct apply_action *v;
1254
1255        cprintf("apply name = \n", name);
1256        if (!name)
1257                return NULL;
1258
1259        for (v = apply_actions; v < &apply_actions[STRUCT_LEN(apply_actions)];
1260             v++) {
1261                if (!strcmp(v->name, name)) {
1262                        return v;
1263                }
1264        }
1265        return NULL;
1266}
1267
1268int getFileLen(FILE * in)
1269{
1270        int len;
1271
1272        fseek(in, 0, SEEK_END);
1273        len = ftell(in);
1274        rewind(in);
1275        return len;
1276}
1277
1278void do_logout(void)            // static functions are not exportable,
1279                                // additionally this is no ej function
1280{
1281        send_authenticate(auth_realm);
1282} static char *getdisc(void)    // works only for squashfs
1283{
1284        int i;
1285        static char ret[4];
1286        unsigned char *disks[] = {
1287                "sda2", "sdb2", "sdc2", "sdd2", "sde2", "sdf2", "sdg2", "sdh2",
1288                "sdi2"
1289        };
1290        for (i = 0; i < 9; i++) {
1291                char dev[64];
1292
1293                sprintf(dev, "/dev/%s", disks[i]);
1294                FILE *in = fopen(dev, "rb");
1295
1296                if (in == NULL)
1297                        continue;       // no second partition or disc does not
1298                // exist, skipping
1299                char buf[4];
1300
1301                fread(buf, 4, 1, in);
1302                if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q'
1303                    && buf[3] == 't') {
1304                        fclose(in);
1305                        // filesystem detected
1306                        strncpy(ret, disks[i], 3);
1307                        return ret;
1308                }
1309                fclose(in);
1310        }
1311        return NULL;
1312}
1313
1314static int
1315apply_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1316          char_t * url, char_t * path, char_t * query)
1317{
1318        int action = NOTHING;
1319        char *value;
1320        char *submit_button, *next_page;
1321        int sleep_time = 0;
1322        int need_commit = 1;
1323
1324        cprintf("need reboot\n");
1325        int need_reboot = atoi(websGetVar(wp, "need_reboot", "0"));
1326
1327        cprintf("apply");
1328
1329        /**********   get "change_action" and launch gozila_cgi if needed **********/
1330
1331        value = websGetVar(wp, "change_action", "");
1332        cprintf("get change_action = %s\n", value);
1333
1334        if (value && !strcmp(value, "gozila_cgi")) {
1335                gozila_cgi(wp, urlPrefix, webDir, arg, url, path, query);
1336                return 1;
1337        }
1338
1339  /***************************************************************************/
1340        if (!query) {
1341                goto footer;
1342        }
1343        if (legal_ip_netmask
1344            ("lan_ipaddr", "lan_netmask",
1345             nvram_safe_get("http_client_ip")) == TRUE)
1346                nvram_set("browser_method", "USE_LAN");
1347        else
1348                nvram_set("browser_method", "USE_WAN");
1349
1350  /**********   get all webs var **********/
1351
1352        submit_button = websGetVar(wp, "submit_button", "");
1353        cprintf("get submit_button = %s\n", submit_button);
1354
1355        need_commit = atoi(websGetVar(wp, "commit", "1"));
1356        cprintf("get need_commit = %d\n", need_commit);
1357
1358        value = websGetVar(wp, "action", "");
1359        cprintf("get action = %s\n", value);
1360
1361  /**********   check action to do **********/
1362
1363  /** Apply **/
1364        if (!strcmp(value, "Apply") || !strcmp(value, "ApplyTake")) {
1365                struct apply_action *act;
1366
1367                cprintf("validate cgi");
1368                validate_cgi(wp);
1369                cprintf("handle apply action\n");
1370                act = handle_apply_action(submit_button);
1371                cprintf("done\n");
1372                // If web page configuration is changed, the EZC configuration
1373                // function should be disabled.(2004-07-29)
1374                nvram_set("is_default", "0");
1375                nvram_set("is_modified", "1");
1376                if (act) {
1377                        fprintf(stderr,
1378                                "%s:submit_button=[%s] service=[%s] sleep_time=[%d] action=[%d]\n",
1379                                value, act->name, act->service, act->sleep_time,
1380                                act->action);
1381
1382                        if ((act->action == SYS_RESTART)
1383                            || (act->action == SERVICE_RESTART)) {
1384
1385                                addAction(act->service);
1386                        }
1387                        sleep_time = act->sleep_time;
1388                        action = act->action;
1389
1390                        if (act->goname)
1391                                start_gozila(act->goname, wp);
1392                } else {
1393                        // nvram_set ("action_service", "");
1394                        sleep_time = 1;
1395                        action = RESTART;
1396                }
1397                diag_led(DIAG, STOP_LED);
1398                sys_commit();
1399        }
1400
1401  /** Restore defaults **/
1402        else if (!strncmp(value, "Restore", 7)) {
1403                ACTION("ACT_SW_RESTORE");
1404                nvram_set("sv_restore_defaults", "1");
1405                killall("udhcpc", SIGKILL);
1406                sys_commit();
1407#ifdef HAVE_X86
1408#ifdef HAVE_ERC
1409                eval("nvram", "restore", "/etc/defaults/x86ree.backup");
1410                eval("reboot");
1411                eval("event", "5", "1", "15");
1412#endif
1413                char drive[64];
1414                sprintf(drive, "/dev/%s", getdisc());
1415                FILE *in = fopen(drive, "r+b");
1416                fseeko(in, 0, SEEK_END);
1417                __off_t mtdlen = ftell(in);
1418                fseeko(in, mtdlen - (65536 * 2), SEEK_SET);
1419                int i;
1420                for (i = 0; i < 65536; i++)
1421                        putc(0, in);    // erase backup area
1422                fclose(in);
1423                eval("mount", "/usr/local", "-o", "remount,rw");
1424                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1425                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1426                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1427                // database
1428                eval("mount", "/usr/local", "-o", "remount,ro");
1429                eval("sync");
1430#elif HAVE_RB600
1431                char drive[64];
1432                sprintf(drive, "/dev/sda");
1433                FILE *in = fopen(drive, "r+b");
1434                fseeko(in, 0, SEEK_END);
1435                __off_t mtdlen = ftell(in);
1436                fseeko(in, mtdlen - (65536 * 2), SEEK_SET);
1437                int i;
1438                for (i = 0; i < 65536; i++)
1439                        putc(0, in);    // erase backup area
1440                fclose(in);
1441                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1442                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1443                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1444                eval("sync");
1445#elif HAVE_OPENRISC
1446#ifdef HAVE_ERC
1447                eval("cp", "-f", "/etc/defaults/nvram.bin",
1448                     "/usr/local/nvram/nvram.bin");
1449                eval("sync");
1450                eval("sync");
1451                sleep(5);
1452                eval("event", "5", "1", "15");
1453#endif
1454                eval("mount", "/usr/local", "-o", "remount,rw");
1455                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1456                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1457                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1458                // database
1459                eval("mount", "/usr/local", "-o", "remount,ro");
1460#elif HAVE_RB500
1461                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1462                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1463                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1464#elif HAVE_MAGICBOX
1465                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1466                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1467                eval("erase", "nvram");
1468#else
1469                eval("erase", "nvram");
1470#endif
1471                action = REBOOT;
1472        }
1473
1474  /** Reboot **/
1475        else if (!strncmp(value, "Reboot", 6)) {
1476                action = REBOOT;
1477        }
1478
1479        /** GUI Logout **/// Experimental, not work yet ...
1480        else if (!strncmp(value, "Logout", 6)) {
1481                do_ej(NULL, "Logout.asp", wp, NULL);
1482                websDone(wp, 200);
1483                do_logout();
1484                return 1;
1485        }
1486
1487        /*
1488         * DEBUG : Invalid action
1489         */
1490        else
1491                websDebugWrite(wp, "Invalid action %s<br />", value);
1492
1493footer:
1494
1495        if (nvram_match("do_reboot", "1")) {
1496                action = REBOOT;
1497        }
1498        /*
1499         * The will let PC to re-get a new IP Address automatically
1500         */
1501        if (need_reboot)
1502                action = REBOOT;
1503
1504        if (!strcmp(value, "Apply")) {
1505                action = NOTHING;
1506        }
1507
1508        if (action != REBOOT) {
1509                if (my_next_page[0] != '\0')
1510                        sprintf(path, "%s", my_next_page);
1511                else {
1512                        next_page = websGetVar(wp, "next_page", NULL);
1513                        if (next_page)
1514                                sprintf(path, "%s", next_page);
1515                        else
1516                                sprintf(path, "%s.asp", submit_button);
1517                }
1518
1519                cprintf("refresh to %s\n", path);
1520                if (!strncmp(path, "WL_FilterTable", 14))
1521                        do_filtertable(NULL, path, wp, NULL);   // refresh
1522#ifdef HAVE_FREERADIUS
1523                else if (!strncmp(path, "FreeRadiusCert", 14))
1524                        do_radiuscert(NULL, path, wp, NULL);    // refresh     
1525#endif
1526                else if (!strncmp(path, "WL_ActiveTable", 14))
1527                        do_activetable(NULL, path, wp, NULL);   // refresh     
1528                else if (!strncmp(path, "Wireless_WDS", 12))
1529                        do_wds(NULL, path, wp, NULL);   // refresh
1530                else if (!strncmp(path, "Wireless_Advanced", 17))
1531                        do_wireless_adv(NULL, path, wp, NULL);  // refresh
1532                else
1533                        do_ej(NULL, path, wp, NULL);    // refresh
1534                websDone(wp, 200);
1535        } else {
1536#ifndef HAVE_WRK54G
1537                do_ej(NULL, "Reboot.asp", wp, NULL);
1538                websDone(wp, 200);
1539#endif
1540                // sleep (5);
1541                sys_reboot();
1542                return 1;
1543        }
1544
1545        nvram_set("upnp_wan_proto", "");
1546        sleep(sleep_time);
1547        if ((action == RESTART) || (action == SYS_RESTART))
1548                sys_restart();
1549        else if (action == SERVICE_RESTART)
1550                service_restart();
1551
1552        return 1;
1553
1554}
1555
1556//int auth_check( char *dirname, char *authorization )
1557int do_auth(webs_t wp, char *userid, char *passwd, char *realm,
1558            char *authorisation, int (*auth_check) (char *userid, char *passwd,
1559                                                    char *dirname,
1560                                                    char *authorisation))
1561{
1562        strncpy(userid, nvram_safe_get("http_username"), AUTH_MAX);
1563        strncpy(passwd, nvram_safe_get("http_passwd"), AUTH_MAX);
1564        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1565#ifdef HAVE_ERC
1566        strncpy(realm, "LOGIN", AUTH_MAX);
1567        wp->userid = 0;
1568        if (auth_check(userid, passwd, realm, authorisation))
1569                return 1;
1570        wp->userid = 1;
1571        strncpy(userid, zencrypt("SuperAdmin"), AUTH_MAX);
1572        strncpy(passwd, nvram_safe_get("newhttp_passwd"), AUTH_MAX);
1573        if (auth_check(userid, passwd, realm, authorisation))
1574                return 1;
1575        userid = 0;
1576#else
1577        wp->userid = 0;
1578        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1579        if (auth_check(userid, passwd, realm, authorisation))
1580                return 1;
1581#endif
1582        return 0;
1583}
1584
1585int do_cauth(webs_t wp, char *userid, char *passwd, char *realm,
1586             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1587                                                     char *dirname,
1588                                                     char *authorisation))
1589{
1590        if (nvram_match("info_passwd", "0"))
1591                return 1;
1592        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1593}
1594
1595#ifdef HAVE_REGISTER
1596int do_auth_reg(webs_t wp, char *userid, char *passwd, char *realm,
1597                char *authorisation, int (*auth_check) (char *userid,
1598                                                        char *passwd,
1599                                                        char *dirname,
1600                                                        char *authorisation))
1601{
1602        if (!isregistered())
1603                return 1;
1604        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1605}
1606#endif
1607
1608#undef HAVE_DDLAN
1609
1610#ifdef HAVE_DDLAN
1611int do_auth2(webs_t wp, char *userid, char *passwd, char *realm,
1612             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1613                                                     char *dirname,
1614                                                     char *authorisation))
1615{
1616        strncpy(userid, nvram_safe_get("http2_username"), AUTH_MAX);
1617        strncpy(passwd, nvram_safe_get("http2_passwd"), AUTH_MAX);
1618        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1619        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1620        if (auth_check(wp, userid, passwd, realm, authorisation))
1621                return 1;
1622        return 0;
1623}
1624#endif
1625// #ifdef EZC_SUPPORT
1626char ezc_version[128];
1627
1628// #endif
1629
1630extern int post;
1631
1632static char *post_buf = NULL;
1633void                            // support GET and POST 2003-08-22
1634do_apply_post(char *url, webs_t stream, int len, char *boundary)
1635{
1636        unsigned char buf[1024];
1637        int count;
1638        if (post == 1) {
1639                if (post_buf)
1640                        post_buf = (char *)realloc(post_buf, len + 1);
1641                else
1642                        post_buf = (char *)safe_malloc(len + 1);
1643
1644                if (!post_buf) {
1645                        cprintf("The POST data exceed length limit!\n");
1646                        return;
1647                }
1648                /*
1649                 * Get query
1650                 */
1651                if (!(count = wfread(post_buf, 1, len, stream)))
1652                        return;
1653                post_buf[count] = '\0';;
1654                len -= strlen(post_buf);
1655
1656                /*
1657                 * Slurp anything remaining in the request
1658                 */
1659                while (--len > 0)
1660#ifdef HAVE_HTTPS
1661                        if (do_ssl)
1662                                wfgets(buf, 1, stream);
1663                        else
1664#endif
1665                                (void)fgetc(stream->fp);
1666                init_cgi(post_buf);
1667        }
1668}
1669
1670#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
1671static void do_cfebackup(struct mime_handler *handler, char *url,
1672                         webs_t stream, char *query)
1673{
1674        system2("cat /dev/mtd/0 > /tmp/cfe.bin");
1675        do_file_attach(handler, "/tmp/cfe.bin", stream, NULL, "cfe.bin");
1676        unlink("/tmp/cfe.bin");
1677}
1678#endif
1679#ifdef HAVE_ROUTERSTYLE
1680static void do_stylecss(struct mime_handler *handler, char *url,
1681                        webs_t stream, char *query)
1682{
1683        char *style = nvram_get("router_style");
1684
1685        if (query != NULL)
1686                style = query;
1687
1688        long sdata[30];
1689        memset(sdata,0, sizeof(sdata));
1690        long blue[30] = {
1691                0x36f, 0xfff, 0x68f, 0x24d, 0x24d, 0x68f, 0x57f, 0xccf, 0x78f,
1692                0x35d,
1693                0x35c, 0x78f,
1694                0x78f, 0xfff, 0x9af, 0x46e, 0x46e, 0x9af, 0x36f,
1695                0xccf, 0xfff, 0x69f, 0xfff, 0xfff, 0x999, 0x69f,
1696                0x69f, 0xccf, 0x78f, 0xfff
1697        };
1698
1699        long cyan[30] = {
1700                0x099, 0xfff, 0x3bb, 0x066, 0x066, 0x3bb, 0x3bb, 0xcff, 0x4cc,
1701                0x1aa,
1702                0x1aa, 0x4cc,
1703                0x6cc, 0xfff, 0x8dd, 0x5bb, 0x5bb, 0x8dd, 0x099,
1704                0xcff, 0xfff, 0x3bb, 0xfff, 0xfff, 0x999, 0x3bb,
1705                0x3bb, 0xcff, 0x6cc, 0xfff
1706        };
1707
1708        long elegant[30] = {
1709                0x30519c, 0xfff, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7,
1710                0x496fc7, 0xfff, 0x6384cf, 0x6384cf, 0x6384cf,
1711                0x6384cf, 0x6384cf, 0xfff, 0x849dd9, 0x849dd9,
1712                0x849dd9, 0x849dd9, 0x30519c, 0xfff, 0xfff,
1713                0x496fc7, 0xfff, 0xfff, 0x999, 0x496fc7, 0x496fc7,
1714                0xfff, 0x6384cf, 0xfff
1715        };
1716
1717        long green[30] = {
1718                0x090, 0xfff, 0x3b3, 0x060, 0x060, 0x3b3, 0x3b3, 0xcfc, 0x4c4,
1719                0x1a1,
1720                0x1a1, 0x4c4,
1721                0x6c6, 0xfff, 0x8d8, 0x5b5, 0x5b5, 0x8d8, 0x090,
1722                0xcfc, 0xfff, 0x3b3, 0xfff, 0xfff, 0x999, 0x3b3,
1723                0x3b3, 0xcfc, 0x6c6, 0xfff
1724        };
1725
1726        long orange[30] = {
1727                0xf26522, 0xfff, 0xff8400, 0xff8400, 0xff8400, 0xff8400,
1728                0xff8400, 0xfff, 0xfeb311, 0xfeb311, 0xfeb311,
1729                0xfeb311, 0xff9000, 0xfff, 0xffa200, 0xffa200,
1730                0xffa200, 0xffa200, 0xf26522, 0xfff, 0xfff,
1731                0xff8400, 0xfff, 0xfff, 0x999, 0xff8400, 0xff8400,
1732                0xfff, 0xff9000, 0xfff
1733        };
1734
1735        long red[30] = {
1736                0xc00, 0xfff, 0xe33, 0x800, 0x800, 0xe33, 0xd55, 0xfcc, 0xe77,
1737                0xc44,
1738                0xc44, 0xe77,
1739                0xe77, 0xfff, 0xf99, 0xd55, 0xd55, 0xf99, 0xc00,
1740                0xfcc, 0xfff, 0xd55, 0xfff, 0xfff, 0x999, 0xd55,
1741                0xd55, 0xfcc, 0xe77, 0xfff
1742        };
1743
1744        long yellow[30] = {
1745                0xeec900, 0x000, 0xee3, 0x880, 0x880, 0xee3, 0xffd700, 0x660,
1746                0xee7,
1747                0xbb4,
1748                0xbb4, 0xee7,
1749                0xeec900, 0x000, 0xff9, 0xcc5, 0xcc5, 0xff9,
1750                0xeec900, 0x660, 0x000, 0xffd700, 0x000, 0xfff,
1751                0x999, 0xffd700, 0xeec900, 0x660, 0xffd700, 0x000
1752        };
1753
1754        if (!strcmp(style, "blue"))
1755                memcpy(sdata, blue, sizeof(blue));
1756        else if (!strcmp(style, "cyan"))
1757                memcpy(sdata, cyan, sizeof(cyan));
1758        else if (!strcmp(style, "yellow"))
1759                memcpy(sdata, yellow, sizeof(yellow));
1760        else if (!strcmp(style, "green"))
1761                memcpy(sdata, green, sizeof(green));
1762        else if (!strcmp(style, "orange"))
1763                memcpy(sdata, orange, sizeof(orange));
1764        else if (!strcmp(style, "red"))
1765                memcpy(sdata, red, sizeof(red));
1766        else                    // default to elegant
1767                memcpy(sdata, elegant, sizeof(elegant));
1768
1769        websWrite(stream,
1770                  "@import url(../common.css);\n#menuSub,\n#menuMainList li span,\n#help h2 {\nbackground:#%03x;\n",
1771                  sdata[0]);
1772        websWrite(stream, "color:#%03x;\n", sdata[1]);
1773        websWrite(stream,
1774                  "border-color:#%03x #%03x #%03x #%03x;\n}\n#menuSubList li a {\n",
1775                  sdata[2], sdata[3], sdata[4], sdata[5]);
1776        websWrite(stream, "background:#%03x;\n", sdata[6]);
1777        websWrite(stream, "color:#%03x;\n", sdata[7]);
1778        websWrite(stream,
1779                  "border-color:#%03x #%03x #%03x #%03x;\n}\n#menuSubList li a:hover {\n",
1780                  sdata[8], sdata[9], sdata[10], sdata[11]);
1781        websWrite(stream, "background:#%03x;\n", sdata[12]);
1782        websWrite(stream, "color:#%03x;\n", sdata[13]);
1783        websWrite(stream,
1784                  "border-color:#%03x #%03x #%03x #%03x;\n}\nfieldset legend {\n",
1785                  sdata[14], sdata[15], sdata[16], sdata[17]);
1786        websWrite(stream, "color:#%03x;\n}\n#help a {\n", sdata[18]);
1787        websWrite(stream, "color:#%03x;\n}\n#help a:hover {\n", sdata[19]);
1788        websWrite(stream, "color:#%03x;\n}\n.meter .bar {\n", sdata[20]);
1789        websWrite(stream, "background-color: #%03x;\n}\n.meter .text {\n",
1790                  sdata[21]);
1791        websWrite(stream, "color:#%03x;\n}\n.progressbar {\n", sdata[22]);
1792        websWrite(stream, "background-color: #%03x;\n", sdata[23]);
1793        websWrite(stream,
1794                  "border-color: #%03x;\nfont-size:.09em;\nborder-width:.09em;\n}\n.progressbarblock {\n",
1795                  sdata[24]);
1796        websWrite(stream,
1797                  "background-color: #%03x;\nfont-size:.09em;\n}\ninput.button {\n",
1798                  sdata[25]);
1799        websWrite(stream, "background: #%03x;\n", sdata[26]);
1800        websWrite(stream, "color: #%03x;\n}\ninput.button:hover {\n",
1801                  sdata[27]);
1802        websWrite(stream, "background: #%03x;\n", sdata[28]);
1803        websWrite(stream, "color: #%03x;\n}\n", sdata[29]);
1804
1805}
1806
1807static void do_stylecss_ie(struct mime_handler *handler, char *url,
1808                           webs_t stream, char *query)
1809{
1810        websWrite(stream, ".submitFooter input {\n"
1811                  "padding:.362em .453em;\n"
1812                  "}\n"
1813                  "fieldset {\n"
1814                  "padding-top:0;\n"
1815                  "}\n"
1816                  "fieldset legend {\n"
1817                  "margin-left:-9px;\n"
1818                  "margin-bottom:8px;\n" "padding:0 .09em;\n" "}\n");
1819}
1820#endif
1821#ifdef HAVE_REGISTER
1822static void do_trial_logo(struct mime_handler *handler, char *url,
1823                          webs_t stream, char *query)
1824{
1825#if defined(HAVE_TRIMAX) || defined(HAVE_MAKSAT) || defined(HAVE_VILIM) || defined(HAVE_TELCOM) || defined(HAVE_WIKINGS) || defined(HAVE_NEXTMEDIA)
1826        do_file(handler, url, stream, query);
1827#else
1828        if (!isregistered_real()) {
1829                do_file(handler, "style/logo-trial.png", stream, query);
1830        } else {
1831                if (iscpe()) {
1832                        do_file(handler, "style/logo-cpe.png", stream, query);
1833                } else {
1834                        do_file(handler, url, stream, query);
1835                }
1836        }
1837#endif
1838}
1839
1840#endif
1841/*
1842 * static void do_style (char *url, webs_t stream, char *query) { char *style
1843 * = nvram_get ("router_style"); if (style == NULL || strlen (style) == 0)
1844 * do_file ("kromo.css", stream, NULL); else do_file (style, stream, NULL); }
1845 */
1846
1847static void do_mypage(struct mime_handler *handler, char *url,
1848                      webs_t stream, char *query)
1849{
1850        char *snamelist = nvram_safe_get("mypage_scripts");
1851        char *next;
1852        char sname[128];
1853        int qnum;
1854        int i = 1;
1855
1856        if (query == NULL || strlen(query) == 0)
1857                qnum = 1;
1858        else
1859                qnum = atoi(query);
1860
1861        foreach(sname, snamelist, next) {
1862                if (qnum == i) {
1863                        strcat(sname, " > /tmp/mypage.tmp");
1864                        system2(sname);
1865                        do_file_attach(handler, "/tmp/mypage.tmp", stream, NULL,
1866                                       "MyPage.asp");
1867                        unlink("/tmp/mypage.tmp");
1868                }
1869                i++;
1870        }
1871
1872        return;
1873
1874}
1875
1876static void do_fetchif(struct mime_handler *handler, char *url,
1877                       webs_t stream, char *query)
1878{
1879        char line[256];
1880        int i, llen;
1881        char buffer[256];
1882        char querybuffer[32];
1883
1884        if (query == NULL || strlen(query) == 0)
1885                return;
1886
1887        strncpy(querybuffer, query, 30);
1888        strcat(querybuffer, ":");
1889
1890        int strbuffer = 0;
1891        time_t tm;
1892        struct tm tm_time;
1893
1894        time(&tm);
1895        memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
1896        char *date_fmt = "%a %b %e %H:%M:%S %Z %Y";
1897
1898        strftime(buffer, 200, date_fmt, &tm_time);
1899        strbuffer = strlen(buffer);
1900        buffer[strbuffer++] = '\n';
1901        FILE *in = fopen("/proc/net/dev", "rb");
1902
1903        if (in == NULL)
1904                return;
1905
1906        /* eat first two lines */
1907        fgets(line, sizeof(line), in);
1908        fgets(line, sizeof(line), in);
1909
1910        while (fgets(line, sizeof(line), in) != NULL) {
1911                if (strstr(line, querybuffer)) {
1912                        llen = strlen(line);
1913                        for (i = 0; i < llen; i++) {
1914                                buffer[strbuffer++] = line[i];
1915                        }
1916                        break;
1917                }
1918        }
1919        fclose(in);
1920
1921        buffer[strbuffer] = 0;
1922        websWrite(stream, "%s", buffer);
1923}
1924
1925static char *getLanguageName()
1926{
1927        char *lang = nvram_get("language");
1928
1929        cprintf("get language %s\n", lang);
1930        char *l = safe_malloc(60);
1931
1932        if (lang == NULL) {
1933                cprintf("return default\n");
1934                sprintf(l, "lang_pack/english.js");
1935                return l;
1936        }
1937        sprintf(l, "lang_pack/%s.js", lang);
1938        cprintf("return %s\n", l);
1939        return l;
1940}
1941
1942char *live_translate(char *tran)
1943{
1944
1945        FILE *fp;
1946        static char temp[256], temp1[256];
1947        char *temp2;
1948        if (tran == NULL || !strlen(tran))
1949                return "";
1950        char *lang = getLanguageName();
1951        char buf[64];
1952        int start, filelen, pos;
1953
1954        memset(temp, 0, sizeof(temp));
1955        memset(temp1, 0, sizeof(temp));
1956
1957        sprintf(buf, "%s", lang);
1958        free(lang);
1959
1960        strcpy(temp1, tran);
1961        strcat(temp1, "=\"");
1962
1963        int len = strlen(temp1);
1964
1965        fp = getWebsFile(buf);
1966        if (fp) {
1967                start = ftell(fp);
1968                filelen = getWebsFileLen(buf);
1969
1970                while (fgets(temp, 256, fp) != NULL) {
1971                        pos = ftell(fp);
1972
1973                        if ((pos - start) > filelen)
1974                                break;
1975                        if ((memcmp(temp, temp1, len)) == 0) {
1976                                temp2 = strtok(temp, "\"");
1977                                temp2 = strtok(NULL, "\"");
1978
1979                                fclose(fp);
1980                                return temp2;
1981                        }
1982                }
1983                fclose(fp);
1984        }
1985
1986        strcpy(buf, "lang_pack/english.js");    // if string not found, try english
1987        fp = getWebsFile(buf);
1988
1989        if (fp == NULL)
1990                return "Error";
1991        start = ftell(fp);
1992        filelen = getWebsFileLen(buf);
1993
1994        while (fgets(temp, 256, fp) != NULL) {
1995                pos = ftell(fp);
1996
1997                if ((pos - start) > filelen)
1998                        break;
1999                if ((memcmp(temp, temp1, len)) == 0) {
2000                        temp2 = strtok(temp, "\"");
2001                        temp2 = strtok(NULL, "\"");
2002
2003                        fclose(fp);
2004                        return temp2;
2005                }
2006        }
2007        fclose(fp);
2008
2009        return "Error";         // not found
2010
2011}
2012
2013static void do_ttgraph(struct mime_handler *handler, char *url,
2014                       webs_t stream, char *query)
2015{
2016#define COL_WIDTH 16            /* single column width */
2017
2018        char *next;
2019        char var[80];
2020
2021        unsigned int days;
2022        unsigned int month;
2023        unsigned int year;
2024        int wd;
2025        int i = 0;
2026        char months[12][12] = {
2027                "share.jan", "share.feb", "share.mar", "share.apr", "share.may",
2028                "share.jun",
2029                "share.jul", "share.aug", "share.sep", "share.oct",
2030                "share.nov", "share.dec"
2031        };
2032        unsigned long rcvd[31] = {
2033                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2034                0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2035        };
2036        unsigned long sent[31] = {
2037                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2038                0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2039        };
2040        unsigned long max = 5, smax = 5, f = 1;
2041        unsigned long totin = 0;
2042        unsigned long totout = 0;
2043
2044        if (sscanf(query, "%u-%u", &month, &year) != 2)
2045                return;
2046        if (month < 1 || month > 12)
2047                return;
2048
2049        days = daysformonth(month, year);
2050        wd = weekday(month, 1, year);   // first day in month (mon=0, tue=1,
2051        // ..., sun=6)
2052
2053        char tq[32];
2054
2055        sprintf(tq, "traff-%02u-%u", month, year);
2056        char *tdata = nvram_safe_get(tq);
2057
2058        if (tdata != NULL && strlen(tdata)) {
2059                foreach(var, tdata, next) {
2060                        if (i == days)
2061                                break;  //skip monthly total
2062                        int ret = sscanf(var, "%lu:%lu", &rcvd[i], &sent[i]);
2063                        if (ret != 2)
2064                                break;
2065                        totin += rcvd[i];
2066                        totout += sent[i];
2067                        if (rcvd[i] > max)
2068                                max = rcvd[i];
2069                        if (sent[i] > max)
2070                                max = sent[i];
2071                        i++;
2072                }
2073        }
2074
2075        while (max > smax) {
2076                if (max > (f * 5))
2077                        smax = f * 10;
2078                if (max > (f * 10))
2079                        smax = f * 25;
2080                if (max > (f * 25))
2081                        smax = f * 50;
2082                f = f * 10;
2083        }
2084
2085        char incom[32];
2086
2087        snprintf(incom, 32, "%s", live_translate("status_inet.traffin"));
2088        char outcom[32];
2089
2090        snprintf(outcom, 32, "%s", live_translate("status_inet.traffout"));
2091        char monthname[32];
2092
2093        snprintf(monthname, 32, "%s", live_translate(months[month - 1]));
2094
2095        websWrite(stream,
2096                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
2097                  "<html>\n"
2098                  "<head>\n"
2099                  "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
2100                  live_translate("lang_charset.set"));
2101
2102        websWrite(stream, "<title>dd-wrt traffic graph</title>\n"
2103                  "<script type=\"text/javascript\">\n"
2104                  "//<![CDATA[\n"
2105                  "function Show(label) {\n"
2106                  "document.getElementById(\"label\").innerHTML = label;\n"
2107                  "}\n"
2108                  "//]]>\n"
2109                  "</script>\n"
2110                  "<style type=\"text/css\">\n\n"
2111                  "#t-graph {position: relative; width: %upx; height: 300px;\n",
2112                  days * COL_WIDTH);
2113        websWrite(stream, "  margin: 1.1em 0 3.5em; padding: 0;\n"
2114                  "  border: 1px solid gray; list-style: none;\n"
2115                  "  font: 9px Tahoma, Arial, sans-serif;}\n"
2116                  "#t-graph ul {margin: 0; padding: 0; list-style: none;}\n"
2117                  "#t-graph li {position: absolute; bottom: 0; width: %dpx; z-index: 2;\n",
2118                  COL_WIDTH);
2119        websWrite(stream, "  margin: 0; padding: 0;\n"
2120                  "  text-align: center; list-style: none;}\n"
2121                  "#t-graph li.day {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #AAA;}\n"
2122                  "#t-graph li.day_sun {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #E00;}\n"
2123                  "#t-graph li.bar {width: 4px; border: 1px solid; border-bottom: none; color: #000;}\n"
2124                  "#t-graph li.bar p {margin: 5px 0 0; padding: 0;}\n"
2125                  "#t-graph li.rcvd {left: 3px; background: #228B22;}\n"
2126                  "#t-graph li.sent {left: 8px; background: #CD0000;}\n");
2127
2128        for (i = 0; i < days - 1; i++) {
2129                websWrite(stream, "#t-graph #d%d {left: %dpx;}\n", i + 1,
2130                          i * COL_WIDTH);
2131        }
2132        websWrite(stream, "#t-graph #d%u {left: %upx; border-right: none;}\n",
2133                  days, (days - 1) * COL_WIDTH);
2134
2135        websWrite(stream,
2136                  "#t-graph #ticks {width: %upx; height: 300px; z-index: 1;}\n",
2137                  days * COL_WIDTH);
2138        websWrite(stream,
2139                  "#t-graph #ticks .tick {position: relative; border-bottom: 1px solid #BBB; width: %upx;}\n",
2140                  days * COL_WIDTH);
2141        websWrite(stream,
2142                  "#t-graph #ticks .tick p {position: absolute; left: 100%%; top: -0.67em; margin: 0 0 0 0.5em;}\n"
2143                  "#t-graph #label {width: 500px; bottom: -20px;  z-index: 1; font: 12px Tahoma, Arial, sans-serif; font-weight: bold;}\n"
2144                  "</style>\n"
2145                  "</head>\n\n" "<body>\n" "<ul id=\"t-graph\">\n");
2146
2147        for (i = 0; i < days; i++) {
2148                websWrite(stream, "<li class=\"day%s\" id=\"d%d\" ",
2149                          (wd % 7) == 6 ? "_sun" : "", i + 1);
2150                wd++;
2151                websWrite(stream,
2152                          "onmouseover=\"Show(\'%s %d, %d (%s: %lu MB / %s: %lu MB)\')\" ",
2153                          monthname, i + 1, year, incom, rcvd[i], outcom,
2154                          sent[i]);
2155                websWrite(stream,
2156                          "onmouseout=\"Show(\'%s %d (%s: %lu MB / %s: %lu MB)\')\"",
2157                          monthname, year, incom, totin, outcom, totout);
2158                websWrite(stream, ">%d\n", i + 1);
2159                websWrite(stream, "<ul>\n");
2160                websWrite(stream,
2161                          "<li class=\"rcvd bar\" style=\"height: %lupx;\"><p></p></li>\n",
2162                          rcvd[i] * 300 / smax);
2163                websWrite(stream,
2164                          "<li class=\"sent bar\" style=\"height: %lupx;\"><p></p></li>\n",
2165                          sent[i] * 300 / smax);
2166                websWrite(stream, "</ul>\n");
2167                websWrite(stream, "</li>\n");
2168        }
2169
2170        websWrite(stream, "<li id=\"ticks\">\n");
2171        for (i = 5; i; i--)     // scale
2172        {
2173                websWrite(stream,
2174                          "<div class=\"tick\" style=\"height: 59px;\"><p>%d%sMB</p></div>\n",
2175                          smax * i / 5, (smax > 10000) ? " " : "&nbsp;");
2176        }
2177        websWrite(stream, "</li>\n\n");
2178
2179        websWrite(stream, "<li id=\"label\">\n");
2180        websWrite(stream, "%s %d (%s: %lu MB / %s: %lu MB)\n", monthname, year,
2181                  incom, totin, outcom, totout);
2182        websWrite(stream, "</li>\n" "</ul>\n\n" "</body>\n" "</html>\n");
2183
2184}
2185
2186static void ttraff_backup(struct mime_handler *handler, char *url,
2187                          webs_t stream, char *query)
2188{
2189        system2("echo TRAFF-DATA > /tmp/traffdata.bak");
2190        system2("nvram show | grep traff- >> /tmp/traffdata.bak");
2191        do_file_attach(handler, "/tmp/traffdata.bak", stream, NULL,
2192                       "traffdata.bak");
2193        unlink("/tmp/traffdata.bak");
2194} static void do_apply_cgi(struct mime_handler *handler, char *url,
2195                           webs_t stream, char *q)
2196{
2197        char *path, *query;
2198
2199        if (post == 1) {
2200                query = post_buf;
2201                path = url;
2202        } else {
2203                query = url;
2204                path = strsep(&query, "?") ? : url;
2205                init_cgi(query);
2206        }
2207
2208        if (!query)
2209                return;
2210
2211        apply_cgi(stream, NULL, NULL, 0, url, path, query);
2212        init_cgi(NULL);
2213}
2214
2215#ifdef HAVE_MADWIFI
2216extern int getdevicecount(void);
2217#endif
2218
2219#ifdef HAVE_LANGUAGE
2220static void do_language(struct mime_handler *handler, char *path, webs_t stream, char *query)   // jimmy,
2221                                                                        // https,
2222                                                                        // 8/4/2003
2223{
2224        char *lang = getLanguageName();
2225
2226        do_file(handler, lang, stream, NULL);
2227        free(lang);
2228        return;
2229}
2230#endif
2231extern int issuperchannel(void);
2232
2233static char no_cache[] =
2234    "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "Expires: 0";
2235
2236struct mime_handler mime_handlers[] = {
2237        // { "ezconfig.asp", "text/html", ezc_version, do_apply_ezconfig_post,
2238        // do_ezconfig_asp, do_auth },
2239#ifdef HAVE_SKYTRON
2240        {"setupindex*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2241#endif
2242#ifdef HAVE_POKER
2243        {"PokerEdit.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2244#endif
2245#ifdef HAVE_DDLAN
2246        {"Upgrade*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2247        {"Management*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2248        {"Services*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2249        {"Hotspot*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2250        {"Wireless*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2251        {"WL_*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2252        {"WPA*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2253        {"Log*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2254        {"Alive*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2255        {"Diagnostics*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2256        {"Wol*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2257        {"Factory_Defaults*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2258        {"config*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2259#endif
2260
2261        {"changepass.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2262#ifdef HAVE_REGISTER
2263        {"register.asp", "text/html", no_cache, NULL, do_ej, do_auth_reg, 1},
2264#endif
2265        {"WL_FilterTable*", "text/html", no_cache, NULL, do_filtertable,
2266         do_auth, 1},
2267#ifdef HAVE_FREERADIUS
2268        {"FreeRadiusCert*", "text/html", no_cache, NULL, do_radiuscert, do_auth,
2269         1},
2270        {"freeradius-certs/*", "application/octet-stream", no_cache, NULL,
2271         cert_file_out, do_auth, 0},
2272#endif
2273        // #endif
2274        // #ifdef HAVE_MADWIFI
2275        {"Wireless_WDS*", "text/html", no_cache, NULL, do_wds, do_auth, 1},
2276        {"WL_ActiveTable*", "text/html", no_cache, NULL, do_activetable,
2277         do_auth, 1},
2278        {"Wireless_Advanced*", "text/html", no_cache, NULL, do_wireless_adv,
2279         do_auth, 1},
2280        // #endif
2281        {"MyPage.asp*", "text/html", no_cache, NULL, do_mypage, do_auth, 1},
2282        {"**.asp", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2283        {"**.JPG", "image/jpeg", no_cache, NULL, do_file, NULL, 0},
2284        // {"style.css", "text/css", NULL, NULL, do_style, NULL},
2285        {"common.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2286#ifdef HAVE_LANGUAGE
2287        {"lang_pack/language.js", "text/javascript", NULL, NULL, do_language,
2288         NULL, 0},
2289#endif
2290#ifdef HAVE_BUFFALO
2291        {"vsp.html", "text/plain", no_cache, NULL, do_vsp_page, NULL, 1},
2292#endif
2293        {"SysInfo.htm*", "text/plain", no_cache, NULL, do_ej, do_auth, 1},
2294#ifdef HAVE_SKYTRON
2295        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2296        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2297        {"**.htm", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2298        {"**.html", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2299#else
2300        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2301        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2302        {"**.htm", "text/html", no_cache, NULL, do_ej, NULL, 1},
2303        {"**.html", "text/html", no_cache, NULL, do_ej, NULL, 1},
2304
2305#endif
2306#ifdef HAVE_ROUTERSTYLE
2307        {"style/blue/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2308        {"style/cyan/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2309        {"style/elegant/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2310         1},
2311        {"style/green/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2312        {"style/orange/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2313         1},
2314        {"style/red/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2315        {"style/yellow/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2316         1},
2317        {"style/blue/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2318         NULL,
2319         1},
2320        {"style/cyan/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2321         NULL,
2322         1},
2323        {"style/elegant/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2324         NULL, 1},
2325        {"style/green/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2326         NULL,
2327         1},
2328        {"style/orange/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2329         NULL, 1},
2330        {"style/red/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie, NULL,
2331         1},
2332        {"style/yellow/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2333         NULL, 1},
2334#endif
2335#ifdef HAVE_REGISTER
2336        {"style/logo.png", "image/png", NULL, NULL, do_trial_logo, NULL, 0},
2337#endif
2338        {"**.css", "text/css", NULL, NULL, do_file, NULL, 0},
2339        {"**.svg", "image/svg+xml", NULL, NULL, do_file, NULL, 0},
2340        {"**.gif", "image/gif", NULL, NULL, do_file, NULL, 0},
2341        {"**.png", "image/png", NULL, NULL, do_file, NULL, 0},
2342        {"**.jpg", "image/jpeg", NULL, NULL, do_file, NULL, 0},
2343        {"**.ico", "image/x-icon", NULL, NULL, do_file, NULL, 0},
2344        {"**.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2345        {"**.swf", "application/x-shockwave-flash", NULL, NULL, do_file, NULL,
2346         0},
2347        {"**.pdf", "application/pdf", NULL, NULL, do_file, NULL, 0},
2348        {"**.mp4", "video/mp4", NULL, NULL, do_file, NULL, 0},
2349        {"**.mp3", "audio/mpeg3", NULL, NULL, do_file, NULL, 0},
2350        {"**.mpg", "video/mpeg", NULL, NULL, do_file, NULL, 0},
2351        {"**.avi", "video/x-msvideo", NULL, NULL, do_file, NULL, 0},
2352        {"**.wma", "audio/x-ms-wma", NULL, NULL, do_file, NULL, 0},
2353        {"**.wmv", "video/x-ms-wmv", NULL, NULL, do_file, NULL, 0},
2354        {"**.flv", "video/x-flv", NULL, NULL, do_file, NULL, 0},
2355#ifdef HAVE_SKYTRON
2356        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2357         do_auth2, 1},
2358#elif HAVE_DDLAN
2359        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2360         NULL, 1},
2361#else
2362        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2363         do_auth, 1},
2364#endif
2365        {"fetchif.cgi*", "text/html", no_cache, NULL, do_fetchif, do_auth, 1},
2366#ifdef HAVE_DDLAN
2367        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi, NULL,
2368         1},
2369        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2370         NULL, 1},
2371#else
2372        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2373         do_auth, 1},
2374        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2375         do_auth, 1},
2376#endif
2377#ifdef HAVE_BUFFALO
2378        {"olupgrade.cgi*", "text/html", no_cache, do_olupgrade_post,
2379         do_upgrade_cgi,
2380         do_auth, 1},
2381#endif
2382        // {"Gozila.cgi*", "text/html", no_cache, NULL, do_setup_wizard,
2383        // do_auth}, // for setup wizard
2384        /*
2385         * { "**.cfg", "application/octet-stream", no_cache, NULL, do_backup,
2386         * do_auth },
2387         */
2388#ifdef HAVE_DDLAN
2389        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2390         do_upgrade_cgi,
2391         NULL, 1},
2392#else
2393        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2394         do_upgrade_cgi,
2395         do_auth, 1},
2396#endif
2397        {"test.bin**", "application/octet-stream", no_cache, NULL, do_file,
2398         do_auth, 0},
2399
2400#ifdef HAVE_DDLAN
2401        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2402         nv_file_out, do_auth2, 0},
2403        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2404         nv_file_out,
2405         do_auth2, 0},
2406        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi, NULL,
2407         1},
2408#else
2409        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2410         nv_file_out, do_auth, 0},
2411        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2412         nv_file_out,
2413         do_auth, 0},
2414        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi,
2415         do_auth,
2416         1},
2417#endif
2418#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
2419        {"backup/cfe.bin", "application/octet-stream", no_cache, NULL,
2420         do_cfebackup,
2421         do_auth, 0},
2422#endif
2423        {"ttgraph.cgi*", "text/html", no_cache, NULL, do_ttgraph, do_auth, 1},
2424        {"traffdata.bak*", "text/html", no_cache, NULL, ttraff_backup,
2425         do_auth, 0},
2426        {"tadmin.cgi*", "text/html", no_cache, td_file_in, td_config_cgi,
2427         NULL, 1},
2428        {"*", "application/octet-stream", no_cache, NULL, do_file, do_auth, 1},
2429        // for ddm
2430        {NULL, NULL, NULL, NULL, NULL, NULL, 0}
2431};
2432
2433/*
2434 * Format: type = SET : " " => "&nbsp;" , ":" => "&semi;" type = GET :
2435 * "&nbsp;" => " " , "&semi;" => ":" Example: name1 = test 123:abc
2436 * filter_name("name1", new_name, SET); new_name="test&nbsp;123&semi;abc"
2437 * name2 = test&nbsp;123&semi;abc filter_name("name2", new_name, GET);
2438 * new_name="test 123:abc"
2439 */
2440int httpd_filter_name(char *old_name, char *new_name, size_t size, int type)
2441{
2442        int i, j, match;
2443
2444        cprintf("httpd_filter_name\n");
2445
2446        struct pattern {
2447                char ch;
2448                char *string;
2449        };
2450
2451        struct pattern patterns[] = {
2452                {' ', "&nbsp;"},
2453                {':', "&semi;"},
2454                {'<', "&lt;"},
2455                {'>', "&gt;"},
2456        };
2457
2458        struct pattern *v;
2459
2460        strcpy(new_name, "");
2461
2462        switch (type) {
2463        case SET:
2464                for (i = 0; *(old_name + i); i++) {
2465                        match = 0;
2466                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2467                             v++) {
2468                                if (*(old_name + i) == v->ch) {
2469                                        if (strlen(new_name) + strlen(v->string) > size) {      // avoid overflow
2470                                                cprintf("%s(): overflow\n",
2471                                                        __FUNCTION__);
2472                                                new_name[strlen(new_name)] =
2473                                                    '\0';
2474                                                return 1;
2475                                        }
2476                                        sprintf(new_name + strlen(new_name),
2477                                                "%s", v->string);
2478                                        match = 1;
2479                                        break;
2480                                }
2481                        }
2482                        if (!match) {
2483                                if (strlen(new_name) + 1 > size) {
2484                                        cprintf("%s(): overflow\n", __FUNCTION__);      // avoid
2485                                        // overflow
2486                                        new_name[strlen(new_name)] = '\0';
2487                                        return 1;
2488                                }
2489                                sprintf(new_name + strlen(new_name), "%c",
2490                                        *(old_name + i));
2491                        }
2492                }
2493
2494                break;
2495        case GET:
2496                for (i = 0, j = 0; *(old_name + j); j++) {
2497                        match = 0;
2498                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2499                             v++) {
2500                                if (!memcmp
2501                                    (old_name + j, v->string,
2502                                     strlen(v->string))) {
2503                                        *(new_name + i) = v->ch;
2504                                        j = j + strlen(v->string) - 1;
2505                                        match = 1;
2506                                        break;
2507                                }
2508                        }
2509                        if (!match)
2510                                *(new_name + i) = *(old_name + j);
2511
2512                        i++;
2513                }
2514                *(new_name + i) = '\0';
2515                break;
2516        default:
2517                cprintf("%s():Invalid type!\n", __FUNCTION__);
2518                break;
2519        }
2520        // cprintf("%s():new_name=[%s]\n", __FUNCTION__, new_name);
2521
2522        return 1;
2523}
2524
2525#ifdef HAVE_BUFFALO
2526void do_vsp_page(struct mime_handler *handler, char *url,
2527                 webs_t stream, char *query)
2528{
2529/*
2530#ifdef HAVE_MADWIFI
2531        char *ifname = "ath0";
2532#else
2533        char *ifname = "wl0";
2534#endif
2535
2536        char *authmode = nvram_nget("%s_security_mode", ifname);
2537        char *encrypt = nvram_nget("%s_crypto", ifname);
2538        char *wpakey = nvram_nget("%s_wpa_psk ", ifname);
2539
2540        if (!strcmp(encrypt, "tkip")) {
2541                encrypt = "TKIP";
2542        } else if (!strcmp(authmode, "aes")) {
2543                encrypt = "AES";
2544        } else if (!strcmp(authmode, "tkip+aes")) {
2545                encrypt = "TKIP-AES-MIX";
2546        } else {
2547                encrypt = "";
2548        }
2549
2550        if (!strcmp(authmode, "disabled")) {
2551                authmode = "NONE";
2552                encrypt = "";
2553                wpakey = "";
2554        } else if (!strcmp(authmode, "wep")) {
2555                authmode = "WEP";
2556                encrypt = "";
2557                wpakey = "";
2558        } else if (!strcmp(authmode, "radius")
2559                   || !strcmp(authmode, "wpa")
2560                   || !strcmp(authmode, "wpa2")
2561                   || !strcmp(authmode, "wpa wpa2")) {
2562                authmode = "RADIUS";
2563                encrypt = "";
2564                wpakey = "";
2565        } else if (!strcmp(authmode, "8021X")) {
2566                authmode = "802.1X";
2567                encrypt = "";
2568                wpakey = "";
2569        } else if (!strcmp(authmode, "psk")) {
2570                authmode = "WPA";
2571        } else if (!strcmp(authmode, "psk2")) {
2572                authmode = "WPA2";
2573        } else if (!strcmp(authmode, "psk psk2")) {
2574                authmode = "WPA-WPA2-MIX";
2575        } else {
2576                authmode = "UNKNOWN";
2577                encrypt = "";
2578                wpakey = "";
2579        }
2580*/
2581        websWrite(stream, "<html>\n");
2582        websWrite(stream, "<head>\n");
2583        websWrite(stream, "<title>VSP</title>\n");
2584        websWrite(stream, "</head>\n");
2585        websWrite(stream, "<body>\n");
2586        websWrite(stream, "<pre>\n");
2587
2588        websWrite(stream, "DEVICE_VSP_VERSION=0.1\n");
2589        websWrite(stream, "DEVICE_VENDOR=BUFFALO INC.\n");
2590        websWrite(stream, "DEVICE_MODEL=%s DDWRT\n",
2591                  nvram_safe_get("DD_BOARD"));
2592        websWrite(stream, "DEVICE_FIRMWARE_VERSION=1.00\n");
2593        char *reg = getUEnv("region");
2594        if (!reg)
2595                reg = "US";
2596        websWrite(stream, "DEVICE_REGION=%s\n", reg);
2597        websWrite(stream, "WIRELESS_DEVICE_NUMBER=1\n");
2598//      websWrite(stream, "WIRELESS_1_PRESET_AUTHMODE=%s\n", authmode);
2599//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT=%s\n", encrypt);
2600//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT_KEY=%s\n", wpakey);
2601        websWrite(stream, "DEVICE_URL_GET=/vsp.html\n");
2602        websWrite(stream, "DEVICE_URL_SET=/vsp.html\n");
2603
2604        websWrite(stream, "</pre>\n");
2605        websWrite(stream, "</body>\n");
2606        websWrite(stream, "</html>\n");
2607}
2608#endif
Note: See TracBrowser for help on using the repository browser.