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

Last change on this file since 14066 was 14066, checked in by eko, 3 years ago

latest vsp.html page / for Buffalo

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