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

Last change on this file since 17608 was 17608, checked in by BrainSlayer, 21 months ago

fix arguments

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