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

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

new x86 code

File size: 79.2 KB
Line 
1
2/*
3 * Broadcom Home Gateway Reference Design
4 * Web Page Configuration Support Routines
5 *
6 * Copyright 2001-2003, Broadcom Corporation
7 * All Rights Reserved.
8 *
9 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
10 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
11 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
12 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
13 * $Id: broadcom.c,v 1.9 2005/11/30 11:53:42 seg Exp $
14 */
15
16#ifdef WEBS
17#include <webs.h>
18#include <uemf.h>
19#include <ej.h>
20#else                           /* !WEBS */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25#include <unistd.h>
26#include <limits.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <httpd.h>
33#include <errno.h>
34#endif                          /* WEBS */
35
36#include <proto/ethernet.h>
37#include <fcntl.h>
38#include <signal.h>
39#include <time.h>
40#include <sys/klog.h>
41#include <sys/wait.h>
42#include <cyutils.h>
43#include <support.h>
44#include <cy_conf.h>
45// #ifdef EZC_SUPPORT
46#include <ezc.h>
47// #endif
48#include <broadcom.h>
49#include <wlutils.h>
50#include <netdb.h>
51#include <utils.h>
52#include <dlfcn.h>
53
54int debug_value = 0;
55
56// static char * rfctime(const time_t *timep);
57// static char * reltime(unsigned int seconds);
58
59// #if defined(linux)
60
61#include <fcntl.h>
62#include <signal.h>
63#include <time.h>
64#include <sys/klog.h>
65#include <sys/wait.h>
66#define sys_stats(url) eval("stats", (url))
67
68// tofu
69
70char *live_translate(char *tran);
71#ifdef HAVE_BUFFALO
72void do_vsp_page(struct mime_handler *handler, char *url, webs_t stream,
73                 char *query);
74#endif
75/*
76 * Deal with side effects before committing
77 */
78int sys_commit(void)
79{
80        if (nvram_match("dhcpnvram", "1")) {
81                killall("dnsmasq", SIGUSR2);    // update lease -- tofu
82                sleep(1);
83        }
84        // if (nvram_match("wan_proto", "pppoe") || nvram_match("wan_proto",
85        // "pptp") )
86        // nvram_set("wan_ifname", "ppp0");
87        // else
88        // nvram_set("wan_ifname", nvram_get("pppoe_ifname"));
89        return nvram_commit();
90}
91
92/*
93 * Variables are set in order (put dependent variables later). Set
94 * nullok to TRUE to ignore zero-length values of the variable itself.
95 * For more complicated validation that cannot be done in one pass or
96 * depends on additional form components or can throw an error in a
97 * unique painful way, write your own validation routine and assign it
98 * to a hidden variable (e.g. filter_ip).
99 */
100/*
101 * DD-WRT enhancement by seg This functions parses all
102 * /etc/config/xxxxx.nvramconfig files and creates the web var tab. so these
103 * vars arent defined anymore staticly
104 */
105
106#include <stdlib.h>
107#include <malloc.h>
108#include <dirent.h>
109#include <stdlib.h>
110
111char *toUP(char *a)
112{
113        int i;
114        int slen = strlen(a);
115
116        for (i = 0; i < slen; i++) {
117                if (a[i] > 'a' - 1 && a[i] < 'z' + 1)
118                        a[i] -= 'a' + 'A';
119        }
120        return a;
121}
122
123int stricmp(char *a, char *b)
124{
125        if (strlen(a) != strlen(b))
126                return -1;
127        return strcmp(toUP(a), toUP(b));
128}
129
130void StringStart(FILE * in)
131{
132        while (getc(in) != '"') {
133                if (feof(in))
134                        return;
135        }
136}
137
138char *getFileString(FILE * in)
139{
140        char *buf;
141        int i, b;
142
143        buf = safe_malloc(1024);
144        StringStart(in);
145        for (i = 0; i < 1024; i++) {
146                b = getc(in);
147                if (b == EOF)
148                        return NULL;
149                if (b == '"') {
150                        buf[i] = 0;
151                        buf = realloc(buf, strlen(buf) + 1);
152                        return buf;
153                }
154                buf[i] = b;
155        }
156        return buf;
157}
158
159void skipFileString(FILE * in)
160{
161        int i, b;
162
163        StringStart(in);
164        for (i = 0; i < 1024; i++) {
165                b = getc(in);
166                if (b == EOF)
167                        return;
168                if (b == '"') {
169                        return;
170                }
171        }
172        return;
173}
174
175static char *directories[] = {
176        "/etc/config",
177        "/jffs/etc/config",
178        "/mmc/etc/config"
179};
180
181struct variable **variables;
182void Initnvramtab()
183{
184        struct dirent *entry;
185        DIR *directory;
186        FILE *in;
187        int varcount = 0, len, i;
188        char *tmpstr;
189        struct variable *tmp;
190
191        variables = NULL;
192        char buf[1024];
193
194        // format = VARNAME VARDESC VARVALID VARVALIDARGS FLAGS FLAGS
195        // open config directory directory =
196        int idx;
197
198        for (idx = 0; idx < 3; idx++) {
199                directory = opendir(directories[idx]);
200                if (directory == NULL)
201                        continue;
202                // list all files in this directory
203                while ((entry = readdir(directory)) != NULL) {
204                        if (endswith(entry->d_name, ".nvramconfig")) {
205                                sprintf(buf, "%s/%s", directories[idx],
206                                        entry->d_name);
207                                in = fopen(buf, "rb");
208                                if (in == NULL) {
209                                        return;
210                                }
211                                while (1) {
212                                        tmp = (struct variable *)
213                                            safe_malloc(sizeof
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#endif
1304        {"Join", "Join", "wireless",1, REFRESH, "wireless_join"},
1305        {"NAS", "save", "nassrv", 1, REFRESH, "nassrv_save"},
1306};
1307
1308struct gozila_action *handle_gozila_action(char *name, char *type)
1309{
1310        struct gozila_action *v;
1311
1312        if (!name || !type)
1313                return NULL;
1314
1315        for (v = gozila_actions;
1316             v < &gozila_actions[STRUCT_LEN(gozila_actions)]; v++) {
1317                if (!strcmp(v->name, name) && !strcmp(v->type, type)) {
1318                        return v;
1319                }
1320        }
1321        return NULL;
1322}
1323
1324char my_next_page[30] = "";
1325int
1326gozila_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1327           char_t * url, char_t * path, char_t * query)
1328{
1329        char *submit_button, *submit_type, *next_page;
1330        int action = REFRESH;
1331        int sleep_time;
1332        struct gozila_action *act;
1333
1334        nvram_set("gozila_action", "1");
1335        my_next_page[0] = '\0';
1336        submit_button = websGetVar(wp, "submit_button", NULL);  /* every html
1337                                                                 * must have
1338                                                                 * the name */
1339        submit_type = websGetVar(wp, "submit_type", NULL);      /* add, del,
1340                                                                 * renew,
1341                                                                 * release
1342                                                                 * ..... */
1343
1344        fprintf(stderr, "submit_button=[%s] submit_type=[%s]\n", submit_button,
1345                submit_type);
1346        act = handle_gozila_action(submit_button, submit_type);
1347
1348        if (act) {
1349                fprintf(stderr,
1350                        "name=[%s] type=[%s] service=[%s] sleep=[%d] action=[%d]\n",
1351                        act->name, act->type, act->service, act->sleep_time,
1352                        act->action);
1353                addAction(act->service);
1354                sleep_time = act->sleep_time;
1355                action = act->action;
1356                if (act->goname) {
1357                        start_gozila(act->goname, wp);
1358                }
1359        } else {
1360                sleep_time = 0;
1361                action = REFRESH;
1362        }
1363
1364        if (action == REFRESH) {
1365                sleep(sleep_time);
1366        } else if (action == SERVICE_RESTART) {
1367                sys_commit();
1368                service_restart();
1369                sleep(sleep_time);
1370        } else if (action == SYS_RESTART) {
1371                sys_commit();
1372                sys_restart();
1373        } else if (action == RESTART) {
1374                sys_commit();
1375                sys_restart();
1376        }
1377
1378        if (my_next_page[0] != '\0') {
1379                sprintf(path, "%s", my_next_page);
1380        } else {
1381                next_page = websGetVar(wp, "next_page", NULL);
1382                if (next_page)
1383                        sprintf(path, "%s", next_page);
1384                else
1385                        sprintf(path, "%s.asp", submit_button);
1386        }
1387
1388        cprintf("refresh to %s\n", path);
1389        if (!strncmp(path, "WL_FilterTable", 14))
1390                do_filtertable(NULL, path, wp, NULL);   // refresh
1391#ifdef HAVE_FREERADIUS
1392        else if (!strncmp(path, "FreeRadiusCert", 14))
1393                do_radiuscert(NULL, path, wp, NULL);    // refresh
1394#endif
1395        // #ifdef HAVE_MADWIFI
1396        else if (!strncmp(path, "WL_ActiveTable", 14))
1397                do_activetable(NULL, path, wp, NULL);   // refresh
1398        else if (!strncmp(path, "Wireless_WDS", 12))
1399                do_wds(NULL, path, wp, NULL);   // refresh
1400        // #endif
1401        else if (!strncmp(path, "Wireless_Advanced", 17))
1402                do_wireless_adv(NULL, path, wp, NULL);  // refresh
1403        else
1404                do_ej(NULL, path, wp, NULL);    // refresh
1405        websDone(wp, 200);
1406
1407        nvram_set("gozila_action", "0");
1408        nvram_set("generate_key", "0");
1409        nvram_set("clone_wan_mac", "0");
1410
1411        return 1;
1412}
1413
1414struct apply_action apply_actions[] = {
1415        /*
1416         * name, service, sleep_time, action, function_to_execute
1417         */
1418
1419        /*
1420         * SETUP
1421         */
1422        {"index", "index", 0, SERVICE_RESTART, NULL},
1423        {"DDNS", "ddns", 0, SERVICE_RESTART, "ddns_save_value"},
1424        {"Routing", "routing", 0, SERVICE_RESTART, NULL},
1425        {"Vlan", "", 0, SYS_RESTART, "port_vlan_table_save"},
1426        {"eop-tunnel", "eop", 0, SERVICE_RESTART, NULL},
1427
1428        /*
1429         * WIRELESS
1430         */
1431        {"Wireless_Basic", "wireless", 0, SERVICE_RESTART, NULL},       // Only for
1432        // V23, since
1433        // V24 it's a
1434        // gozilla
1435        // save
1436        {"Wireless_Advanced-wl0", "wireless_2", 0, SERVICE_RESTART,
1437         "save_wireless_advanced"},
1438        {"Wireless_Advanced-wl1", "wireless_2", 0, SERVICE_RESTART,
1439         "save_wireless_advanced"},
1440        {"Wireless_MAC", "wireless_2", 0, SERVICE_RESTART, "save_macmode"},
1441        {"WL_FilterTable", "macfilter", 0, SERVICE_RESTART, NULL},
1442        {"Wireless_WDS", "wireless_2", 0, SERVICE_RESTART, NULL},
1443        {"WL_WPATable", "wireless_2", 0, SERVICE_RESTART, NULL},
1444
1445        /*
1446         * MANAGEMENT
1447         */
1448        {"Management", "management", 0, SYS_RESTART, NULL},
1449        {"Services", "services", 0, SERVICE_RESTART, NULL},
1450        {"Alive", "alive", 0, SERVICE_RESTART, NULL},
1451
1452        /*
1453         * SERVICES
1454         */
1455        {"PPPoE_Server", "services", 0, SERVICE_RESTART, NULL},
1456        {"PPTP", "services", 0, SERVICE_RESTART, NULL},
1457        {"USB", "usbdrivers", 0, SERVICE_RESTART, NULL},
1458        {"NAS", "nassrv", 0, SERVICE_RESTART, NULL},
1459        {"Hotspot", "hotspot", 0, SERVICE_RESTART, NULL},
1460        {"AnchorFree", "anchorfree", 0, SERVICE_RESTART, NULL},
1461
1462        /*
1463         * APP & GAMING
1464         */
1465        {"Forward", "forward", 0, SERVICE_RESTART, NULL},
1466        {"ForwardSpec", "forward", 0, SERVICE_RESTART, NULL},
1467        {"Triggering", "filters", 0, SERVICE_RESTART, NULL},
1468        {"DMZ", "filters", 0, SERVICE_RESTART, NULL},
1469        {"Filters", "filters", 0, SERVICE_RESTART, NULL},
1470        {"FilterIPMAC", "filters", 0, SERVICE_RESTART, NULL},
1471#ifdef HAVE_UPNP
1472        {"UPnP", "forward_upnp", 0, SERVICE_RESTART, "tf_upnp"},
1473#endif
1474        /*
1475         * SECURITY
1476         */
1477        {"Firewall", "filters", 0, SERVICE_RESTART, NULL},
1478        {"VPN", "filters", 0, SERVICE_RESTART, NULL},
1479#ifdef HAVE_MILKFISH
1480        {"Milkfish", "milkfish", 0, SERVICE_RESTART, NULL},
1481#endif
1482        /*
1483         * Obsolete {"WL_WEPTable", "", 0, SERVICE_RESTART, NULL}, {"Security",
1484         * "", 1, RESTART, NULL}, {"System", "", 0, RESTART, NULL}, {"DHCP",
1485         * "dhcp", 0, SERVICE_RESTART, NULL}, {"FilterIP", "filters", 0,
1486         * SERVICE_RESTART, NULL}, {"FilterMAC", "filters", 0, SERVICE_RESTART,
1487         * NULL}, {"FilterPort", "filters", 0, SERVICE_RESTART, NULL},
1488         * {"Wireless", "wireless", 0, SERVICE_RESTART, NULL}, {"Log", "logging",
1489         * 0, SERVICE_RESTART, NULL}, //moved to Firewall {"QoS", "qos", 0,
1490         * SERVICE_RESTART, NULL}, //gozilla does the save
1491         */
1492
1493};
1494
1495struct apply_action *handle_apply_action(char *name)
1496{
1497        struct apply_action *v;
1498
1499        cprintf("apply name = \n", name);
1500        if (!name)
1501                return NULL;
1502
1503        for (v = apply_actions; v < &apply_actions[STRUCT_LEN(apply_actions)];
1504             v++) {
1505                if (!strcmp(v->name, name)) {
1506                        return v;
1507                }
1508        }
1509
1510        return NULL;
1511}
1512
1513int getFileLen(FILE * in)
1514{
1515        int len;
1516
1517        fseek(in, 0, SEEK_END);
1518        len = ftell(in);
1519        rewind(in);
1520        return len;
1521}
1522
1523void do_logout(void)            // static functions are not exportable,
1524                                // additionally this is no ej function
1525{
1526        send_authenticate(auth_realm);
1527}
1528
1529static char *getdisc(void)      // works only for squashfs
1530{
1531        int i;
1532        static char ret[4];
1533        unsigned char *disks[]={"sda2","sdb2","sdc2","sdd2","sde2","sdf2","sdg2","sdh2","sdi2"};
1534        for (i = 0; i < 9; i++) {
1535                char dev[64];
1536
1537                sprintf(dev, "/dev/%s", disks[i]);
1538                FILE *in = fopen(dev, "rb");
1539
1540                if (in == NULL)
1541                        continue;       // no second partition or disc does not
1542                // exist, skipping
1543                char buf[4];
1544
1545                fread(buf, 4, 1, in);
1546                if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q'
1547                    && buf[3] == 't') {
1548                        fclose(in);
1549                        // filesystem detected
1550                        strncpy(ret,disks[i],3);
1551                        return ret;
1552                }
1553                fclose(in);
1554        }
1555        return NULL;
1556}
1557
1558static int
1559apply_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1560          char_t * url, char_t * path, char_t * query)
1561{
1562        int action = NOTHING;
1563        char *value;
1564        char *submit_button, *next_page;
1565        int sleep_time = 0;
1566        int need_commit = 1;
1567
1568        cprintf("need reboot\n");
1569        int need_reboot = atoi(websGetVar(wp, "need_reboot", "0"));
1570
1571        cprintf("apply");
1572
1573        /**********   get "change_action" and launch gozila_cgi if needed **********/
1574
1575        value = websGetVar(wp, "change_action", "");
1576        cprintf("get change_action = %s\n", value);
1577
1578        if (value && !strcmp(value, "gozila_cgi")) {
1579                gozila_cgi(wp, urlPrefix, webDir, arg, url, path, query);
1580                return 1;
1581        }
1582
1583  /***************************************************************************/
1584
1585        if (!query) {
1586                goto footer;
1587        }
1588        if (legal_ip_netmask
1589            ("lan_ipaddr", "lan_netmask",
1590             nvram_safe_get("http_client_ip")) == TRUE)
1591                nvram_set("browser_method", "USE_LAN");
1592        else
1593                nvram_set("browser_method", "USE_WAN");
1594
1595  /**********   get all webs var **********/
1596
1597        submit_button = websGetVar(wp, "submit_button", "");
1598        cprintf("get submit_button = %s\n", submit_button);
1599
1600        need_commit = atoi(websGetVar(wp, "commit", "1"));
1601        cprintf("get need_commit = %d\n", need_commit);
1602
1603        value = websGetVar(wp, "action", "");
1604        cprintf("get action = %s\n", value);
1605
1606  /**********   check action to do **********/
1607
1608  /** Apply **/
1609        if (!strcmp(value, "Apply") || !strcmp(value, "ApplyTake")) {
1610                struct apply_action *act;
1611
1612                cprintf("validate cgi");
1613                validate_cgi(wp);
1614                cprintf("handle apply action\n");
1615                act = handle_apply_action(submit_button);
1616                cprintf("done\n");
1617                // If web page configuration is changed, the EZC configuration
1618                // function should be disabled.(2004-07-29)
1619                nvram_set("is_default", "0");
1620                nvram_set("is_modified", "1");
1621                if (act) {
1622                        fprintf(stderr,
1623                                "%s:submit_button=[%s] service=[%s] sleep_time=[%d] action=[%d]\n",
1624                                value, act->name, act->service, act->sleep_time,
1625                                act->action);
1626
1627                        if ((act->action == SYS_RESTART)
1628                            || (act->action == SERVICE_RESTART)) {
1629
1630                                addAction(act->service);
1631                        }
1632                        sleep_time = act->sleep_time;
1633                        action = act->action;
1634
1635                        if (act->goname)
1636                                start_gozila(act->goname, wp);
1637                } else {
1638                        // nvram_set ("action_service", "");
1639                        sleep_time = 1;
1640                        action = RESTART;
1641                }
1642                diag_led(DIAG, STOP_LED);
1643                sys_commit();
1644        }
1645
1646  /** Restore defaults **/
1647        else if (!strncmp(value, "Restore", 7)) {
1648                ACTION("ACT_SW_RESTORE");
1649                nvram_set("sv_restore_defaults", "1");
1650                killall("udhcpc", SIGKILL);
1651                sys_commit();
1652#ifdef HAVE_X86
1653#ifdef HAVE_ERC
1654                eval("nvram","restore","/etc/defaults/x86ree.backup");
1655                eval("reboot");
1656                eval("event", "5", "1", "15");
1657#endif
1658                char drive[64];
1659                sprintf(drive,"/dev/%s",getdisc());
1660                FILE *in = fopen64(drive, "r+b");
1661                fseeko64(in, 0, SEEK_END);
1662                __off64_t mtdlen = ftell(in);
1663                fseeko64(in, mtdlen - (65536 * 2), SEEK_SET);
1664                int i;
1665                for (i = 0; i < 65536; i++)
1666                        putc(0, in);    // erase backup area
1667                fclose(in);
1668                eval("mount", "/usr/local", "-o", "remount,rw");
1669                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1670                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1671                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1672                // database
1673                eval("mount", "/usr/local", "-o", "remount,ro");
1674                eval("sync");
1675#elif HAVE_RB600
1676                char drive[64];
1677                sprintf(drive, "/dev/sda");
1678                FILE *in = fopen64(drive, "r+b");
1679                fseeko64(in, 0, SEEK_END);
1680                __off64_t mtdlen = ftell(in);
1681                fseeko64(in, mtdlen - (65536 * 2), SEEK_SET);
1682                int i;
1683                for (i = 0; i < 65536; i++)
1684                        putc(0, in);    // erase backup area
1685                fclose(in);
1686                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1687                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1688                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1689                eval("sync");
1690#elif HAVE_OPENRISC
1691#ifdef HAVE_ERC
1692                eval("cp", "-f", "/etc/defaults/nvram.bin",
1693                     "/usr/local/nvram/nvram.bin");
1694                eval("sync");
1695                eval("sync");
1696                sleep(5);
1697                eval("event", "5", "1", "15");
1698#endif
1699                eval("mount", "/usr/local", "-o", "remount,rw");
1700                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1701                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1702                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1703                // database
1704                eval("mount", "/usr/local", "-o", "remount,ro");
1705#elif HAVE_RB500
1706                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1707                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1708                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1709#elif HAVE_MAGICBOX
1710                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1711                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1712                eval("erase", "nvram");
1713#else
1714                eval("erase", "nvram");
1715#endif
1716                action = REBOOT;
1717        }
1718
1719  /** Reboot **/
1720        else if (!strncmp(value, "Reboot", 6)) {
1721                action = REBOOT;
1722        }
1723
1724        /** GUI Logout **/// Experimental, not work yet ...
1725        else if (!strncmp(value, "Logout", 6)) {
1726                do_ej(NULL, "Logout.asp", wp, NULL);
1727                websDone(wp, 200);
1728                do_logout();
1729                return 1;
1730        }
1731
1732        /*
1733         * DEBUG : Invalid action
1734         */
1735        else
1736                websDebugWrite(wp, "Invalid action %s<br />", value);
1737
1738footer:
1739
1740        if (nvram_match("do_reboot", "1")) {
1741                action = REBOOT;
1742        }
1743        /*
1744         * The will let PC to re-get a new IP Address automatically
1745         */
1746        if (need_reboot)
1747                action = REBOOT;
1748
1749        if (!strcmp(value, "Apply")) {
1750                action = NOTHING;
1751        }
1752
1753        if (action != REBOOT) {
1754                if (my_next_page[0] != '\0')
1755                        sprintf(path, "%s", my_next_page);
1756                else {
1757                        next_page = websGetVar(wp, "next_page", NULL);
1758                        if (next_page)
1759                                sprintf(path, "%s", next_page);
1760                        else
1761                                sprintf(path, "%s.asp", submit_button);
1762                }
1763
1764                cprintf("refresh to %s\n", path);
1765                if (!strncmp(path, "WL_FilterTable", 14))
1766                        do_filtertable(NULL, path, wp, NULL);   // refresh
1767#ifdef HAVE_FREERADIUS
1768                else if (!strncmp(path, "FreeRadiusCert", 14))
1769                        do_radiuscert(NULL, path, wp, NULL);    // refresh     
1770#endif
1771                else if (!strncmp(path, "WL_ActiveTable", 14))
1772                        do_activetable(NULL, path, wp, NULL);   // refresh     
1773                else if (!strncmp(path, "Wireless_WDS", 12))
1774                        do_wds(NULL, path, wp, NULL);   // refresh
1775                else if (!strncmp(path, "Wireless_Advanced", 17))
1776                        do_wireless_adv(NULL, path, wp, NULL);  // refresh
1777                else
1778                        do_ej(NULL, path, wp, NULL);    // refresh
1779                websDone(wp, 200);
1780        } else {
1781#ifndef HAVE_WRK54G
1782                do_ej(NULL, "Reboot.asp", wp, NULL);
1783                websDone(wp, 200);
1784#endif
1785                // sleep (5);
1786                sys_reboot();
1787                return 1;
1788        }
1789
1790        nvram_set("upnp_wan_proto", "");
1791        sleep(sleep_time);
1792        if ((action == RESTART) || (action == SYS_RESTART))
1793                sys_restart();
1794        else if (action == SERVICE_RESTART)
1795                service_restart();
1796
1797        return 1;
1798
1799}
1800
1801//int auth_check( char *dirname, char *authorization )
1802int do_auth(webs_t wp, char *userid, char *passwd, char *realm,
1803            char *authorisation, int (*auth_check) (char *userid, char *passwd,
1804                                                    char *dirname,
1805                                                    char *authorisation))
1806{
1807        strncpy(userid, nvram_safe_get("http_username"), AUTH_MAX);
1808        strncpy(passwd, nvram_safe_get("http_passwd"), AUTH_MAX);
1809        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1810#ifdef HAVE_ERC
1811        strncpy(realm, "LOGIN", AUTH_MAX);
1812        wp->userid = 0;
1813        if (auth_check(userid, passwd, realm, authorisation))
1814                return 1;
1815        wp->userid = 1;
1816        strncpy(userid, zencrypt("SuperAdmin"), AUTH_MAX);
1817        strncpy(passwd, nvram_safe_get("newhttp_passwd"), AUTH_MAX);
1818        if (auth_check(userid, passwd, realm, authorisation))
1819                return 1;
1820        userid = 0;
1821#else
1822        wp->userid = 0;
1823        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1824        if (auth_check(userid, passwd, realm, authorisation))
1825                return 1;
1826#endif
1827        return 0;
1828}
1829
1830int do_cauth(webs_t wp, char *userid, char *passwd, char *realm,
1831             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1832                                                     char *dirname,
1833                                                     char *authorisation))
1834{
1835        if (nvram_match("info_passwd", "0"))
1836                return 1;
1837        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1838}
1839
1840#ifdef HAVE_REGISTER
1841int do_auth_reg(webs_t wp, char *userid, char *passwd, char *realm,
1842                char *authorisation, int (*auth_check) (char *userid,
1843                                                        char *passwd,
1844                                                        char *dirname,
1845                                                        char *authorisation))
1846{
1847        if (!isregistered())
1848                return 1;
1849        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1850}
1851#endif
1852
1853#undef HAVE_DDLAN
1854
1855#ifdef HAVE_DDLAN
1856int do_auth2(webs_t wp, char *userid, char *passwd, char *realm,
1857             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1858                                                     char *dirname,
1859                                                     char *authorisation))
1860{
1861        strncpy(userid, nvram_safe_get("http2_username"), AUTH_MAX);
1862        strncpy(passwd, nvram_safe_get("http2_passwd"), AUTH_MAX);
1863        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1864        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1865        if (auth_check(wp, userid, passwd, realm, authorisation))
1866                return 1;
1867        return 0;
1868}
1869#endif
1870// #ifdef EZC_SUPPORT
1871char ezc_version[128];
1872
1873// #endif
1874
1875extern int post;
1876
1877static char *post_buf = NULL;
1878static void                     // support GET and POST 2003-08-22
1879do_apply_post(char *url, webs_t stream, int len, char *boundary)
1880{
1881        unsigned char buf[1024];
1882        int count;
1883        if (post == 1) {
1884                if (post_buf)
1885                        post_buf = (char *)realloc(post_buf, len + 1);
1886                else
1887                        post_buf = (char *)safe_malloc(len + 1);
1888
1889                if (!post_buf) {
1890                        cprintf("The POST data exceed length limit!\n");
1891                        return;
1892                }
1893                /*
1894                 * Get query
1895                 */
1896                if (!(count = wfread(post_buf, 1, len, stream)))
1897                        return;
1898                post_buf[count] = '\0';;
1899                len -= strlen(post_buf);
1900
1901                /*
1902                 * Slurp anything remaining in the request
1903                 */
1904                while (--len > 0)
1905#ifdef HAVE_HTTPS
1906                        if (do_ssl)
1907                                wfgets(buf, 1, stream);
1908                        else
1909#endif
1910                                (void)fgetc(stream->fp);
1911                init_cgi(post_buf);
1912        }
1913}
1914
1915#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
1916static void do_cfebackup(struct mime_handler *handler, char *url,
1917                         webs_t stream, char *query)
1918{
1919        system2("cat /dev/mtd/0 > /tmp/cfe.bin");
1920        do_file_attach(handler, "/tmp/cfe.bin", stream, NULL, "cfe.bin");
1921        unlink("/tmp/cfe.bin");
1922}
1923#endif
1924
1925#ifdef HAVE_ROUTERSTYLE
1926static void do_stylecss(struct mime_handler *handler, char *url,
1927                        webs_t stream, char *query)
1928{
1929        char *style = nvram_get("router_style");
1930
1931        if (query != NULL)
1932                style = query;
1933
1934        long sdata[30];
1935
1936        long blue[30] =
1937            { 0x36f, 0xfff, 0x68f, 0x24d, 0x24d, 0x68f, 0x57f, 0xccf, 0x78f,
1938                0x35d,
1939                0x35c, 0x78f,
1940                0x78f, 0xfff, 0x9af, 0x46e, 0x46e, 0x9af, 0x36f, 0xccf, 0xfff,
1941                0x69f,
1942                0xfff, 0xfff,
1943                0x999, 0x69f, 0x69f, 0xccf, 0x78f, 0xfff
1944        };
1945
1946        long cyan[30] =
1947            { 0x099, 0xfff, 0x3bb, 0x066, 0x066, 0x3bb, 0x3bb, 0xcff, 0x4cc,
1948                0x1aa,
1949                0x1aa, 0x4cc,
1950                0x6cc, 0xfff, 0x8dd, 0x5bb, 0x5bb, 0x8dd, 0x099, 0xcff, 0xfff,
1951                0x3bb,
1952                0xfff, 0xfff,
1953                0x999, 0x3bb, 0x3bb, 0xcff, 0x6cc, 0xfff
1954        };
1955
1956        long elegant[30] =
1957            { 0x30519c, 0xfff, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7,
1958                0xfff, 0x6384cf, 0x6384cf, 0x6384cf, 0x6384cf,
1959                0x6384cf, 0xfff, 0x849dd9, 0x849dd9, 0x849dd9, 0x849dd9,
1960                0x30519c,
1961                0xfff,
1962                0xfff, 0x496fc7, 0xfff, 0xfff,
1963                0x999, 0x496fc7, 0x496fc7, 0xfff, 0x6384cf, 0xfff
1964        };
1965
1966        long green[30] =
1967            { 0x090, 0xfff, 0x3b3, 0x060, 0x060, 0x3b3, 0x3b3, 0xcfc, 0x4c4,
1968                0x1a1,
1969                0x1a1, 0x4c4,
1970                0x6c6, 0xfff, 0x8d8, 0x5b5, 0x5b5, 0x8d8, 0x090, 0xcfc, 0xfff,
1971                0x3b3,
1972                0xfff, 0xfff,
1973                0x999, 0x3b3, 0x3b3, 0xcfc, 0x6c6, 0xfff
1974        };
1975
1976        long orange[30] =
1977            { 0xf26522, 0xfff, 0xff8400, 0xff8400, 0xff8400, 0xff8400, 0xff8400,
1978                0xfff, 0xfeb311, 0xfeb311, 0xfeb311, 0xfeb311,
1979                0xff9000, 0xfff, 0xffa200, 0xffa200, 0xffa200, 0xffa200,
1980                0xf26522,
1981                0xfff,
1982                0xfff, 0xff8400, 0xfff, 0xfff,
1983                0x999, 0xff8400, 0xff8400, 0xfff, 0xff9000, 0xfff
1984        };
1985
1986        long red[30] =
1987            { 0xc00, 0xfff, 0xe33, 0x800, 0x800, 0xe33, 0xd55, 0xfcc, 0xe77,
1988                0xc44,
1989                0xc44, 0xe77,
1990                0xe77, 0xfff, 0xf99, 0xd55, 0xd55, 0xf99, 0xc00, 0xfcc, 0xfff,
1991                0xd55,
1992                0xfff, 0xfff,
1993                0x999, 0xd55, 0xd55, 0xfcc, 0xe77, 0xfff
1994        };
1995
1996        long yellow[30] =
1997            { 0xeec900, 0x000, 0xee3, 0x880, 0x880, 0xee3, 0xffd700, 0x660,
1998                0xee7,
1999                0xbb4,
2000                0xbb4, 0xee7,
2001                0xeec900, 0x000, 0xff9, 0xcc5, 0xcc5, 0xff9, 0xeec900, 0x660,
2002                0x000,
2003                0xffd700,
2004                0x000, 0xfff,
2005                0x999, 0xffd700, 0xeec900, 0x660, 0xffd700, 0x000
2006        };
2007
2008        if (!strcmp(style, "blue"))
2009                memcpy(sdata, blue, 30 * sizeof(long));
2010        else if (!strcmp(style, "cyan"))
2011                memcpy(sdata, cyan, 30 * sizeof(long));
2012        else if (!strcmp(style, "yellow"))
2013                memcpy(sdata, yellow, 30 * sizeof(long));
2014        else if (!strcmp(style, "green"))
2015                memcpy(sdata, green, 30 * sizeof(long));
2016        else if (!strcmp(style, "orange"))
2017                memcpy(sdata, orange, 30 * sizeof(long));
2018        else if (!strcmp(style, "red"))
2019                memcpy(sdata, red, 30 * sizeof(long));
2020        else                    // default to elegant
2021                memcpy(sdata, elegant, 30 * sizeof(long));
2022
2023        websWrite(stream, "@import url(../common.css);\n");
2024        websWrite(stream, "#menuSub,\n");
2025        websWrite(stream, "#menuMainList li span,\n");
2026        websWrite(stream, "#help h2 {\n");
2027        websWrite(stream, "background:#%03x;\n", sdata[0]);
2028        websWrite(stream, "color:#%03x;\n", sdata[1]);
2029        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[2],
2030                  sdata[3], sdata[4], sdata[5]);
2031        websWrite(stream, "}\n");
2032        websWrite(stream, "#menuSubList li a {\n");
2033        websWrite(stream, "background:#%03x;\n", sdata[6]);
2034        websWrite(stream, "color:#%03x;\n", sdata[7]);
2035        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[8],
2036                  sdata[9], sdata[10], sdata[11]);
2037        websWrite(stream, "}\n");
2038        websWrite(stream, "#menuSubList li a:hover {\n");
2039        websWrite(stream, "background:#%03x;\n", sdata[12]);
2040        websWrite(stream, "color:#%03x;\n", sdata[13]);
2041        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[14],
2042                  sdata[15], sdata[16], sdata[17]);
2043        websWrite(stream, "}\n");
2044        websWrite(stream, "fieldset legend {\n");
2045        websWrite(stream, "color:#%03x;\n", sdata[18]);
2046        websWrite(stream, "}\n");
2047        websWrite(stream, "#help a {\n");
2048        websWrite(stream, "color:#%03x;\n", sdata[19]);
2049        websWrite(stream, "}\n");
2050        websWrite(stream, "#help a:hover {\n");
2051        websWrite(stream, "color:#%03x;\n", sdata[20]);
2052        websWrite(stream, "}\n");
2053        websWrite(stream, ".meter .bar {\n");
2054        websWrite(stream, "background-color: #%03x;\n", sdata[21]);
2055        websWrite(stream, "}\n");
2056        websWrite(stream, ".meter .text {\n");
2057        websWrite(stream, "color:#%03x;\n", sdata[22]);
2058        websWrite(stream, "}\n");
2059        websWrite(stream, ".progressbar {\n");
2060        websWrite(stream, "background-color: #%03x;\n", sdata[23]);
2061        websWrite(stream, "border-color: #%03x;\n", sdata[24]);
2062        websWrite(stream, "font-size:.09em;\n");
2063        websWrite(stream, "border-width:.09em;\n");
2064        websWrite(stream, "}\n");
2065        websWrite(stream, ".progressbarblock {\n");
2066        websWrite(stream, "background-color: #%03x;\n", sdata[25]);
2067        websWrite(stream, "font-size:.09em;\n");
2068        websWrite(stream, "}\n");
2069        websWrite(stream, "input.button {\n");
2070        websWrite(stream, "background: #%03x;\n", sdata[26]);
2071        websWrite(stream, "color: #%03x;\n", sdata[27]);
2072        websWrite(stream, "}\n");
2073        websWrite(stream, "input.button:hover {\n");
2074        websWrite(stream, "background: #%03x;\n", sdata[28]);
2075        websWrite(stream, "color: #%03x;\n", sdata[29]);
2076        websWrite(stream, "}\n");
2077
2078}
2079
2080static void do_stylecss_ie(struct mime_handler *handler, char *url,
2081                           webs_t stream, char *query)
2082{
2083        websWrite(stream, ".submitFooter input {\n"
2084                  "padding:.362em .453em;\n"
2085                  "}\n"
2086                  "fieldset {\n"
2087                  "padding-top:0;\n"
2088                  "}\n"
2089                  "fieldset legend {\n"
2090                  "margin-left:-9px;\n"
2091                  "margin-bottom:8px;\n" "padding:0 .09em;\n" "}\n");
2092}
2093#endif
2094
2095#ifdef HAVE_REGISTER
2096static void do_trial_logo(struct mime_handler *handler, char *url,
2097                          webs_t stream, char *query)
2098{
2099#if defined(HAVE_TRIMAX) || defined(HAVE_MAKSAT) || defined(HAVE_VILIM) || defined(HAVE_TELCOM) || defined(HAVE_WIKINGS) || defined(HAVE_NEXTMEDIA)
2100        do_file(handler, url, stream, query);
2101#else
2102        if (!isregistered_real()) {
2103                do_file(handler, "style/logo-trial.png", stream, query);
2104        } else {
2105                if (iscpe()) {
2106                        do_file(handler, "style/logo-cpe.png", stream, query);
2107                } else {
2108                        do_file(handler, url, stream, query);
2109                }
2110        }
2111#endif
2112}
2113
2114#endif
2115/*
2116 * static void do_style (char *url, webs_t stream, char *query) { char *style
2117 * = nvram_get ("router_style"); if (style == NULL || strlen (style) == 0)
2118 * do_file ("kromo.css", stream, NULL); else do_file (style, stream, NULL); }
2119 */
2120
2121static void do_mypage(struct mime_handler *handler, char *url,
2122                      webs_t stream, char *query)
2123{
2124        char *snamelist = nvram_safe_get("mypage_scripts");
2125        char *next;
2126        char sname[128];
2127        int qnum;
2128        int i = 1;
2129
2130        if (query == NULL || strlen(query) == 0)
2131                qnum = 1;
2132        else
2133                qnum = atoi(query);
2134
2135        foreach(sname, snamelist, next) {
2136                if (qnum == i) {
2137                        strcat(sname, " > /tmp/mypage.tmp");
2138                        system2(sname);
2139                        do_file_attach(handler, "/tmp/mypage.tmp", stream, NULL,
2140                                       "MyPage.asp");
2141                        unlink("/tmp/mypage.tmp");
2142                }
2143                i++;
2144        }
2145
2146        return;
2147
2148}
2149
2150static void do_fetchif(struct mime_handler *handler, char *url,
2151                       webs_t stream, char *query)
2152{
2153        char line[256];
2154        int i, llen;
2155        char buffer[256];
2156        char querybuffer[32];
2157
2158        if (query == NULL || strlen(query) == 0)
2159                return;
2160
2161        strncpy(querybuffer, query, 30);
2162        strcat(querybuffer, ":");
2163
2164        int strbuffer = 0;
2165        time_t tm;
2166        struct tm tm_time;
2167
2168        time(&tm);
2169        memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
2170        char *date_fmt = "%a %b %e %H:%M:%S %Z %Y";
2171
2172        strftime(buffer, 200, date_fmt, &tm_time);
2173        strbuffer = strlen(buffer);
2174        buffer[strbuffer++] = '\n';
2175        FILE *in = fopen("/proc/net/dev", "rb");
2176
2177        if (in == NULL)
2178                return;
2179
2180        /* eat first two lines */
2181        fgets(line, sizeof(line), in);
2182        fgets(line, sizeof(line), in);
2183
2184        while (fgets(line, sizeof(line), in) != NULL) {
2185                if (strstr(line, querybuffer)) {
2186                        llen = strlen(line);
2187                        for (i = 0; i < llen; i++) {
2188                                buffer[strbuffer++] = line[i];
2189                        }
2190                        break;
2191                }
2192        }
2193        fclose(in);
2194
2195        buffer[strbuffer] = 0;
2196        websWrite(stream, "%s", buffer);
2197}
2198
2199static char *getLanguageName()
2200{
2201        char *lang = nvram_get("language");
2202
2203        cprintf("get language %s\n", lang);
2204        char *l = safe_malloc(60);
2205
2206        if (lang == NULL) {
2207                cprintf("return default\n");
2208                sprintf(l, "lang_pack/english.js");
2209                return l;
2210        }
2211        sprintf(l, "lang_pack/%s.js", lang);
2212        cprintf("return %s\n", l);
2213        return l;
2214}
2215
2216char *live_translate(char *tran)
2217{
2218
2219        FILE *fp;
2220        static char temp[256], temp1[256];
2221        char *temp2;
2222        if (tran == NULL || !strlen(tran))
2223                return "";
2224        char *lang = getLanguageName();
2225        char buf[64];
2226        int start, filelen, pos;
2227
2228        memset(temp, 0, sizeof(temp));
2229        memset(temp1, 0, sizeof(temp));
2230
2231        sprintf(buf, "%s", lang);
2232        free(lang);
2233
2234        strcpy(temp1, tran);
2235        strcat(temp1, "=\"");
2236
2237        int len = strlen(temp1);
2238
2239        fp = getWebsFile(buf);
2240        if (fp) {
2241                start = ftell(fp);
2242                filelen = getWebsFileLen(buf);
2243
2244                while (fgets(temp, 256, fp) != NULL) {
2245                        pos = ftell(fp);
2246
2247                        if ((pos - start) > filelen)
2248                                break;
2249                        if ((memcmp(temp, temp1, len)) == 0) {
2250                                temp2 = strtok(temp, "\"");
2251                                temp2 = strtok(NULL, "\"");
2252
2253                                fclose(fp);
2254                                return temp2;
2255                        }
2256                }
2257                fclose(fp);
2258        }
2259
2260        strcpy(buf, "lang_pack/english.js");    // if string not found, try english
2261        fp = getWebsFile(buf);
2262
2263        if (fp == NULL)
2264                return "Error";
2265        start = ftell(fp);
2266        filelen = getWebsFileLen(buf);
2267
2268        while (fgets(temp, 256, fp) != NULL) {
2269                pos = ftell(fp);
2270
2271                if ((pos - start) > filelen)
2272                        break;
2273                if ((memcmp(temp, temp1, len)) == 0) {
2274                        temp2 = strtok(temp, "\"");
2275                        temp2 = strtok(NULL, "\"");
2276
2277                        fclose(fp);
2278                        return temp2;
2279                }
2280        }
2281        fclose(fp);
2282
2283        return "Error";         // not found
2284
2285}
2286
2287static void do_ttgraph(struct mime_handler *handler, char *url,
2288                       webs_t stream, char *query)
2289{
2290#define COL_WIDTH 16            /* single column width */
2291
2292        char *next;
2293        char var[80];
2294
2295        unsigned int days;
2296        unsigned int month;
2297        unsigned int year;
2298        int wd;
2299        int i = 0;
2300        char months[12][12] =
2301            { "share.jan", "share.feb", "share.mar", "share.apr", "share.may",
2302                "share.jun",
2303                "share.jul", "share.aug", "share.sep", "share.oct", "share.nov",
2304                "share.dec"
2305        };
2306        unsigned long rcvd[31] =
2307            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2308                0,
2309                0,
2310                0, 0, 0, 0, 0, 0, 0
2311        };
2312        unsigned long sent[31] =
2313            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2314                0,
2315                0,
2316                0, 0, 0, 0, 0, 0, 0
2317        };
2318        unsigned long max = 5, smax = 5, f = 1;
2319        unsigned long totin = 0;
2320        unsigned long totout = 0;
2321
2322        if (sscanf(query, "%u-%u", &month, &year) != 2)
2323                return;
2324        if (month < 1 || month > 12)
2325                return;
2326
2327        days = daysformonth(month, year);
2328        wd = weekday(month, 1, year);   // first day in month (mon=0, tue=1,
2329        // ..., sun=6)
2330
2331        char tq[32];
2332
2333        sprintf(tq, "traff-%02u-%u", month, year);
2334        char *tdata = nvram_safe_get(tq);
2335
2336        if (tdata != NULL && strlen(tdata)) {
2337                foreach(var, tdata, next) {
2338                        if (i == days)
2339                                break;  //skip monthly total
2340                        int ret = sscanf(var, "%lu:%lu", &rcvd[i], &sent[i]);
2341                        if (ret != 2)
2342                                break;
2343                        totin += rcvd[i];
2344                        totout += sent[i];
2345                        if (rcvd[i] > max)
2346                                max = rcvd[i];
2347                        if (sent[i] > max)
2348                                max = sent[i];
2349                        i++;
2350                }
2351        }
2352
2353        while (max > smax) {
2354                if (max > (f * 5))
2355                        smax = f * 10;
2356                if (max > (f * 10))
2357                        smax = f * 25;
2358                if (max > (f * 25))
2359                        smax = f * 50;
2360                f = f * 10;
2361        }
2362
2363        char incom[32];
2364
2365        snprintf(incom, 32, "%s", live_translate("status_inet.traffin"));
2366        char outcom[32];
2367
2368        snprintf(outcom, 32, "%s", live_translate("status_inet.traffout"));
2369        char monthname[32];
2370
2371        snprintf(monthname, 32, "%s", live_translate(months[month - 1]));
2372
2373        websWrite(stream,
2374                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
2375        websWrite(stream, "<html>\n");
2376        websWrite(stream, "<head>\n");
2377        websWrite(stream,
2378                  "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
2379                  live_translate("lang_charset.set"));
2380        websWrite(stream, "<title>dd-wrt traffic graph</title>\n");
2381
2382        websWrite(stream, "<script type=\"text/javascript\">\n");
2383        websWrite(stream, "//<![CDATA[\n");
2384        websWrite(stream, "function Show(label) {\n");
2385        websWrite(stream,
2386                  "document.getElementById(\"label\").innerHTML = label;\n");
2387        websWrite(stream, "}\n");
2388        websWrite(stream, "//]]>\n");
2389        websWrite(stream, "</script>\n");
2390
2391        websWrite(stream, "<style type=\"text/css\">\n\n");
2392        websWrite(stream,
2393                  "#t-graph {position: relative; width: %upx; height: 300px;\n",
2394                  days * COL_WIDTH);
2395        websWrite(stream, "  margin: 1.1em 0 3.5em; padding: 0;\n");
2396        websWrite(stream, "  border: 1px solid gray; list-style: none;\n");
2397        websWrite(stream, "  font: 9px Tahoma, Arial, sans-serif;}\n");
2398        websWrite(stream,
2399                  "#t-graph ul {margin: 0; padding: 0; list-style: none;}\n");
2400        websWrite(stream,
2401                  "#t-graph li {position: absolute; bottom: 0; width: %dpx; z-index: 2;\n",
2402                  COL_WIDTH);
2403        websWrite(stream, "  margin: 0; padding: 0;\n");
2404        websWrite(stream, "  text-align: center; list-style: none;}\n");
2405        websWrite(stream,
2406                  "#t-graph li.day {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #AAA;}\n");
2407        websWrite(stream,
2408                  "#t-graph li.day_sun {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #E00;}\n");
2409        websWrite(stream,
2410                  "#t-graph li.bar {width: 4px; border: 1px solid; border-bottom: none; color: #000;}\n");
2411        websWrite(stream, "#t-graph li.bar p {margin: 5px 0 0; padding: 0;}\n");
2412        websWrite(stream, "#t-graph li.rcvd {left: 3px; background: #228B22;}\n");      // set
2413        // rcvd
2414        // bar
2415        // colour
2416        // here
2417        // (green)
2418        websWrite(stream, "#t-graph li.sent {left: 8px; background: #CD0000;}\n");      // set
2419        // sent
2420        // bar
2421        // colour
2422        // here
2423        // (red)
2424
2425        for (i = 0; i < days - 1; i++) {
2426                websWrite(stream, "#t-graph #d%d {left: %dpx;}\n", i + 1,
2427                          i * COL_WIDTH);
2428        }
2429        websWrite(stream, "#t-graph #d%u {left: %upx; border-right: none;}\n",
2430                  days, (days - 1) * COL_WIDTH);
2431
2432        websWrite(stream,
2433                  "#t-graph #ticks {width: %upx; height: 300px; z-index: 1;}\n",
2434                  days * COL_WIDTH);
2435        websWrite(stream,
2436                  "#t-graph #ticks .tick {position: relative; border-bottom: 1px solid #BBB; width: %upx;}\n",
2437                  days * COL_WIDTH);
2438        websWrite(stream,
2439                  "#t-graph #ticks .tick p {position: absolute; left: 100%%; top: -0.67em; margin: 0 0 0 0.5em;}\n");
2440        websWrite(stream,
2441                  "#t-graph #label {width: 500px; bottom: -20px;  z-index: 1; font: 12px Tahoma, Arial, sans-serif; font-weight: bold;}\n");
2442        websWrite(stream, "</style>\n");
2443        websWrite(stream, "</head>\n\n");
2444        websWrite(stream, "<body>\n");
2445        websWrite(stream, "<ul id=\"t-graph\">\n");
2446
2447        for (i = 0; i < days; i++) {
2448                websWrite(stream, "<li class=\"day%s\" id=\"d%d\" ",
2449                          (wd % 7) == 6 ? "_sun" : "", i + 1);
2450                wd++;
2451                websWrite(stream,
2452                          "onmouseover=\"Show(\'%s %d, %d (%s: %lu MB / %s: %lu MB)\')\" ",
2453                          monthname, i + 1, year, incom, rcvd[i], outcom,
2454                          sent[i]);
2455                websWrite(stream,
2456                          "onmouseout=\"Show(\'%s %d (%s: %lu MB / %s: %lu MB)\')\"",
2457                          monthname, year, incom, totin, outcom, totout);
2458                websWrite(stream, ">%d\n", i + 1);
2459                websWrite(stream, "<ul>\n");
2460                websWrite(stream,
2461                          "<li class=\"rcvd bar\" style=\"height: %lupx;\"><p></p></li>\n",
2462                          rcvd[i] * 300 / smax);
2463                websWrite(stream,
2464                          "<li class=\"sent bar\" style=\"height: %lupx;\"><p></p></li>\n",
2465                          sent[i] * 300 / smax);
2466                websWrite(stream, "</ul>\n");
2467                websWrite(stream, "</li>\n");
2468        }
2469
2470        websWrite(stream, "<li id=\"ticks\">\n");
2471        for (i = 5; i; i--)     // scale
2472        {
2473                websWrite(stream,
2474                          "<div class=\"tick\" style=\"height: 59px;\"><p>%d%sMB</p></div>\n",
2475                          smax * i / 5, (smax > 10000) ? " " : "&nbsp;");
2476        }
2477        websWrite(stream, "</li>\n\n");
2478
2479        websWrite(stream, "<li id=\"label\">\n");
2480        websWrite(stream, "%s %d (%s: %lu MB / %s: %lu MB)\n", monthname, year,
2481                  incom, totin, outcom, totout);
2482        websWrite(stream, "</li>\n");
2483
2484        websWrite(stream, "</ul>\n\n");
2485        websWrite(stream, "</body>\n");
2486        websWrite(stream, "</html>\n");
2487
2488}
2489
2490static void ttraff_backup(struct mime_handler *handler, char *url,
2491                          webs_t stream, char *query)
2492{
2493        system2("echo TRAFF-DATA > /tmp/traffdata.bak");
2494        system2("nvram show | grep traff- >> /tmp/traffdata.bak");
2495        do_file_attach(handler, "/tmp/traffdata.bak", stream, NULL,
2496                       "traffdata.bak");
2497        unlink("/tmp/traffdata.bak");
2498}
2499
2500static void do_apply_cgi(struct mime_handler *handler, char *url,
2501                         webs_t stream, char *q)
2502{
2503        char *path, *query;
2504
2505        if (post == 1) {
2506                query = post_buf;
2507                path = url;
2508        } else {
2509                query = url;
2510                path = strsep(&query, "?") ? : url;
2511                init_cgi(query);
2512        }
2513
2514        if (!query)
2515                return;
2516
2517        apply_cgi(stream, NULL, NULL, 0, url, path, query);
2518        init_cgi(NULL);
2519}
2520
2521#ifdef HAVE_MADWIFI
2522extern int getdevicecount(void);
2523#endif
2524
2525#ifdef HAVE_LANGUAGE
2526static void do_language(struct mime_handler *handler, char *path, webs_t stream, char *query)   // jimmy,
2527                                                                        // https,
2528                                                                        // 8/4/2003
2529{
2530        char *lang = getLanguageName();
2531
2532        do_file(handler, lang, stream, NULL);
2533        free(lang);
2534        return;
2535}
2536#endif
2537
2538extern int issuperchannel(void);
2539
2540static char no_cache[] =
2541    "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "Expires: 0";
2542
2543struct mime_handler mime_handlers[] = {
2544        // { "ezconfig.asp", "text/html", ezc_version, do_apply_ezconfig_post,
2545        // do_ezconfig_asp, do_auth },
2546#ifdef HAVE_SKYTRON
2547        {"setupindex*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2548#endif
2549#ifdef HAVE_POKER
2550        {"PokerEdit.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2551#endif
2552#ifdef HAVE_DDLAN
2553        {"Upgrade*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2554        {"Management*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2555        {"Services*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2556        {"Hotspot*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2557        {"Wireless*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2558        {"WL_*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2559        {"WPA*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2560        {"Log*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2561        {"Alive*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2562        {"Diagnostics*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2563        {"Wol*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2564        {"Factory_Defaults*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2565        {"config*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2566#endif
2567
2568        {"changepass.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2569#ifdef HAVE_REGISTER
2570        {"register.asp", "text/html", no_cache, NULL, do_ej, do_auth_reg, 1},
2571#endif
2572        {"WL_FilterTable*", "text/html", no_cache, NULL, do_filtertable,
2573         do_auth, 1},
2574#ifdef HAVE_FREERADIUS
2575        {"FreeRadiusCert*", "text/html", no_cache, NULL, do_radiuscert, do_auth,
2576         1},
2577        {"freeradius-certs/*", "application/octet-stream", no_cache, NULL,
2578         cert_file_out, do_auth, 0},
2579#endif
2580        // #endif
2581        // #ifdef HAVE_MADWIFI
2582        {"Wireless_WDS*", "text/html", no_cache, NULL, do_wds, do_auth, 1},
2583        {"WL_ActiveTable*", "text/html", no_cache, NULL, do_activetable,
2584         do_auth, 1},
2585        {"Wireless_Advanced*", "text/html", no_cache, NULL, do_wireless_adv,
2586         do_auth, 1},
2587        // #endif
2588        {"MyPage.asp*", "text/html", no_cache, NULL, do_mypage, do_auth, 1},
2589        {"**.asp", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2590        {"**.JPG", "image/jpeg", no_cache, NULL, do_file, NULL, 0},
2591        // {"style.css", "text/css", NULL, NULL, do_style, NULL},
2592        {"common.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2593#ifdef HAVE_LANGUAGE
2594        {"lang_pack/language.js", "text/javascript", NULL, NULL, do_language,
2595         NULL, 0},
2596#endif
2597#ifdef HAVE_BUFFALO
2598        {"vsp.html", "text/plain", no_cache, NULL, do_vsp_page, NULL, 1},
2599#endif
2600        {"SysInfo.htm*", "text/plain", no_cache, NULL, do_ej, do_auth, 1},
2601#ifdef HAVE_SKYTRON
2602        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2603        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2604        {"**.htm", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2605        {"**.html", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2606#else
2607        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2608        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2609        {"**.htm", "text/html", no_cache, NULL, do_ej, NULL, 1},
2610        {"**.html", "text/html", no_cache, NULL, do_ej, NULL, 1},
2611
2612#endif
2613#ifdef HAVE_ROUTERSTYLE
2614        {"style/blue/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2615        {"style/cyan/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2616        {"style/elegant/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2617         1},
2618        {"style/green/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2619        {"style/orange/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2620         1},
2621        {"style/red/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2622        {"style/yellow/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2623         1},
2624        {"style/blue/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2625         NULL,
2626         1},
2627        {"style/cyan/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2628         NULL,
2629         1},
2630        {"style/elegant/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2631         NULL, 1},
2632        {"style/green/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2633         NULL,
2634         1},
2635        {"style/orange/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2636         NULL, 1},
2637        {"style/red/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie, NULL,
2638         1},
2639        {"style/yellow/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2640         NULL, 1},
2641#endif
2642#ifdef HAVE_REGISTER
2643        {"style/logo.png", "image/png", NULL, NULL, do_trial_logo, NULL, 0},
2644#endif
2645        {"**.css", "text/css", NULL, NULL, do_file, NULL, 0},
2646        {"**.svg", "image/svg+xml", NULL, NULL, do_file, NULL, 0},
2647        {"**.gif", "image/gif", NULL, NULL, do_file, NULL, 0},
2648        {"**.png", "image/png", NULL, NULL, do_file, NULL, 0},
2649        {"**.jpg", "image/jpeg", NULL, NULL, do_file, NULL, 0},
2650        {"**.ico", "image/x-icon", NULL, NULL, do_file, NULL, 0},
2651        {"**.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2652        {"**.swf", "application/x-shockwave-flash", NULL, NULL, do_file, NULL,
2653         0},
2654        {"**.pdf", "application/pdf", NULL, NULL, do_file, NULL, 0},
2655        {"**.mp4", "video/mp4", NULL, NULL, do_file, NULL, 0},
2656        {"**.mp3", "audio/mpeg3", NULL, NULL, do_file, NULL, 0},
2657        {"**.mpg", "video/mpeg", NULL, NULL, do_file, NULL, 0},
2658        {"**.avi", "video/x-msvideo", NULL, NULL, do_file, NULL, 0},
2659        {"**.wma", "audio/x-ms-wma", NULL, NULL, do_file, NULL, 0},
2660        {"**.wmv", "video/x-ms-wmv", NULL, NULL, do_file, NULL, 0},
2661        {"**.flv", "video/x-flv", NULL, NULL, do_file, NULL, 0},
2662#ifdef HAVE_SKYTRON
2663        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2664         do_auth2, 1},
2665#elif HAVE_DDLAN
2666        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2667         NULL, 1},
2668#else
2669        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2670         do_auth, 1},
2671#endif
2672        {"fetchif.cgi*", "text/html", no_cache, NULL, do_fetchif, do_auth, 1},
2673#ifdef HAVE_DDLAN
2674        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi, NULL,
2675         1},
2676        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2677         NULL, 1},
2678#else
2679        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2680         do_auth, 1},
2681        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2682         do_auth, 1},
2683#endif
2684        // {"Gozila.cgi*", "text/html", no_cache, NULL, do_setup_wizard,
2685        // do_auth}, // for setup wizard
2686        /*
2687         * { "**.cfg", "application/octet-stream", no_cache, NULL, do_backup,
2688         * do_auth },
2689         */
2690#ifdef HAVE_DDLAN
2691        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2692         do_upgrade_cgi,
2693         NULL, 1},
2694#else
2695        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2696         do_upgrade_cgi,
2697         do_auth, 1},
2698#endif
2699        {"test.bin**", "application/octet-stream", no_cache, NULL, do_file,
2700         do_auth, 0},
2701
2702#ifdef HAVE_DDLAN
2703        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2704         nv_file_out, do_auth2, 0},
2705        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2706         nv_file_out,
2707         do_auth2, 0},
2708        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi, NULL,
2709         1},
2710#else
2711        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2712         nv_file_out, do_auth, 0},
2713        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2714         nv_file_out,
2715         do_auth, 0},
2716        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi,
2717         do_auth,
2718         1},
2719#endif
2720#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
2721        {"backup/cfe.bin", "application/octet-stream", no_cache, NULL,
2722         do_cfebackup,
2723         do_auth, 0},
2724#endif
2725        {"ttgraph.cgi*", "text/html", no_cache, NULL, do_ttgraph, do_auth, 1},
2726        {"traffdata.bak*", "text/html", no_cache, NULL, ttraff_backup,
2727         do_auth, 0},
2728        {"tadmin.cgi*", "text/html", no_cache, td_file_in, td_config_cgi,
2729         NULL, 1},
2730        {"*", "application/octet-stream", no_cache, NULL, do_file, do_auth, 1},
2731        // for ddm
2732        {NULL, NULL, NULL, NULL, NULL, NULL, 0}
2733};
2734
2735/*
2736 * Format: type = SET : " " => "&nbsp;" , ":" => "&semi;" type = GET :
2737 * "&nbsp;" => " " , "&semi;" => ":" Example: name1 = test 123:abc
2738 * filter_name("name1", new_name, SET); new_name="test&nbsp;123&semi;abc"
2739 * name2 = test&nbsp;123&semi;abc filter_name("name2", new_name, GET);
2740 * new_name="test 123:abc"
2741 */
2742int httpd_filter_name(char *old_name, char *new_name, size_t size, int type)
2743{
2744        int i, j, match;
2745
2746        cprintf("httpd_filter_name\n");
2747
2748        struct pattern {
2749                char ch;
2750                char *string;
2751        };
2752
2753        struct pattern patterns[] = {
2754                {' ', "&nbsp;"},
2755                {':', "&semi;"},
2756                {'<', "&lt;"},
2757                {'>', "&gt;"},
2758        };
2759
2760        struct pattern *v;
2761
2762        strcpy(new_name, "");
2763
2764        switch (type) {
2765        case SET:
2766                for (i = 0; *(old_name + i); i++) {
2767                        match = 0;
2768                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2769                             v++) {
2770                                if (*(old_name + i) == v->ch) {
2771                                        if (strlen(new_name) + strlen(v->string) > size) {      // avoid overflow
2772                                                cprintf("%s(): overflow\n",
2773                                                        __FUNCTION__);
2774                                                new_name[strlen(new_name)] =
2775                                                    '\0';
2776                                                return 1;
2777                                        }
2778                                        sprintf(new_name + strlen(new_name),
2779                                                "%s", v->string);
2780                                        match = 1;
2781                                        break;
2782                                }
2783                        }
2784                        if (!match) {
2785                                if (strlen(new_name) + 1 > size) {
2786                                        cprintf("%s(): overflow\n", __FUNCTION__);      // avoid
2787                                        // overflow
2788                                        new_name[strlen(new_name)] = '\0';
2789                                        return 1;
2790                                }
2791                                sprintf(new_name + strlen(new_name), "%c",
2792                                        *(old_name + i));
2793                        }
2794                }
2795
2796                break;
2797        case GET:
2798                for (i = 0, j = 0; *(old_name + j); j++) {
2799                        match = 0;
2800                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2801                             v++) {
2802                                if (!memcmp
2803                                    (old_name + j, v->string,
2804                                     strlen(v->string))) {
2805                                        *(new_name + i) = v->ch;
2806                                        j = j + strlen(v->string) - 1;
2807                                        match = 1;
2808                                        break;
2809                                }
2810                        }
2811                        if (!match)
2812                                *(new_name + i) = *(old_name + j);
2813
2814                        i++;
2815                }
2816                *(new_name + i) = '\0';
2817                break;
2818        default:
2819                cprintf("%s():Invalid type!\n", __FUNCTION__);
2820                break;
2821        }
2822        // cprintf("%s():new_name=[%s]\n", __FUNCTION__, new_name);
2823
2824        return 1;
2825}
2826
2827#ifdef HAVE_BUFFALO
2828void do_vsp_page(struct mime_handler *handler, char *url,
2829                 webs_t stream, char *query)
2830{
2831/*
2832#ifdef HAVE_MADWIFI
2833        char *ifname = "ath0";
2834#else
2835        char *ifname = "wl0";
2836#endif
2837
2838        char *authmode = nvram_nget("%s_security_mode", ifname);
2839        char *encrypt = nvram_nget("%s_crypto", ifname);
2840        char *wpakey = nvram_nget("%s_wpa_psk ", ifname);
2841
2842        if (!strcmp(encrypt, "tkip")) {
2843                encrypt = "TKIP";
2844        } else if (!strcmp(authmode, "aes")) {
2845                encrypt = "AES";
2846        } else if (!strcmp(authmode, "tkip+aes")) {
2847                encrypt = "TKIP-AES-MIX";
2848        } else {
2849                encrypt = "";
2850        }
2851
2852        if (!strcmp(authmode, "disabled")) {
2853                authmode = "NONE";
2854                encrypt = "";
2855                wpakey = "";
2856        } else if (!strcmp(authmode, "wep")) {
2857                authmode = "WEP";
2858                encrypt = "";
2859                wpakey = "";
2860        } else if (!strcmp(authmode, "radius")
2861                   || !strcmp(authmode, "wpa")
2862                   || !strcmp(authmode, "wpa2")
2863                   || !strcmp(authmode, "wpa wpa2")) {
2864                authmode = "RADIUS";
2865                encrypt = "";
2866                wpakey = "";
2867        } else if (!strcmp(authmode, "8021X")) {
2868                authmode = "802.1X";
2869                encrypt = "";
2870                wpakey = "";
2871        } else if (!strcmp(authmode, "psk")) {
2872                authmode = "WPA";
2873        } else if (!strcmp(authmode, "psk2")) {
2874                authmode = "WPA2";
2875        } else if (!strcmp(authmode, "psk psk2")) {
2876                authmode = "WPA-WPA2-MIX";
2877        } else {
2878                authmode = "UNKNOWN";
2879                encrypt = "";
2880                wpakey = "";
2881        }
2882*/
2883        websWrite(stream, "<html>\n");
2884        websWrite(stream, "<head>\n");
2885        websWrite(stream, "<title>VSP</title>\n");
2886        websWrite(stream, "</head>\n");
2887        websWrite(stream, "<body>\n");
2888        websWrite(stream, "<pre>\n");
2889
2890        websWrite(stream, "DEVICE_VSP_VERSION=0.1\n");
2891        websWrite(stream, "DEVICE_VENDOR=BUFFALO INC.\n");
2892        websWrite(stream, "DEVICE_MODEL=%s DDWRT\n",
2893                  nvram_safe_get("DD_BOARD"));
2894        websWrite(stream, "DEVICE_FIRMWARE_VERSION=1.00\n");
2895        websWrite(stream, "WIRELESS_DEVICE_NUMBER=1\n");
2896//      websWrite(stream, "WIRELESS_1_PRESET_AUTHMODE=%s\n", authmode);
2897//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT=%s\n", encrypt);
2898//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT_KEY=%s\n", wpakey);
2899        websWrite(stream, "DEVICE_URL_GET=/vsp.html\n");
2900        websWrite(stream, "DEVICE_URL_SET=/vsp.html\n");
2901
2902        websWrite(stream, "</pre>\n");
2903        websWrite(stream, "</body>\n");
2904        websWrite(stream, "</html>\n");
2905}
2906#endif
Note: See TracBrowser for help on using the repository browser.