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

Last change on this file since 14735 was 14735, checked in by BrainSlayer, 3 years ago

no auth here

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