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

Last change on this file since 17357 was 17357, checked in by BrainSlayer, 2 years ago

some spotpass related modifications

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