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

Last change on this file since 17205 was 17205, 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        unsigned char *disks[]={"/dev/sda2","/dev/sdb2","/dev/sdc2","/dev/sdd2","/dev/sde2","/dev/sdf2","/dev/sdg2","/dev/sdh2","/dev/sdi2"};
1533        for (i = 0; i < 9; i++) {
1534                char dev[64];
1535
1536                strcpy(dev, disks[i]);
1537                FILE *in = fopen(dev, "rb");
1538
1539                if (in == NULL)
1540                        continue;       // no second partition or disc does not
1541                // exist, skipping
1542                char buf[4];
1543
1544                fread(buf, 4, 1, in);
1545                if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q'
1546                    && buf[3] == 't') {
1547                        fclose(in);
1548                        // filesystem detected
1549                        return disks[i];
1550                }
1551                fclose(in);
1552        }
1553        return NULL;
1554}
1555
1556static int
1557apply_cgi(webs_t wp, char_t * urlPrefix, char_t * webDir, int arg,
1558          char_t * url, char_t * path, char_t * query)
1559{
1560        int action = NOTHING;
1561        char *value;
1562        char *submit_button, *next_page;
1563        int sleep_time = 0;
1564        int need_commit = 1;
1565
1566        cprintf("need reboot\n");
1567        int need_reboot = atoi(websGetVar(wp, "need_reboot", "0"));
1568
1569        cprintf("apply");
1570
1571        /**********   get "change_action" and launch gozila_cgi if needed **********/
1572
1573        value = websGetVar(wp, "change_action", "");
1574        cprintf("get change_action = %s\n", value);
1575
1576        if (value && !strcmp(value, "gozila_cgi")) {
1577                gozila_cgi(wp, urlPrefix, webDir, arg, url, path, query);
1578                return 1;
1579        }
1580
1581  /***************************************************************************/
1582
1583        if (!query) {
1584                goto footer;
1585        }
1586        if (legal_ip_netmask
1587            ("lan_ipaddr", "lan_netmask",
1588             nvram_safe_get("http_client_ip")) == TRUE)
1589                nvram_set("browser_method", "USE_LAN");
1590        else
1591                nvram_set("browser_method", "USE_WAN");
1592
1593  /**********   get all webs var **********/
1594
1595        submit_button = websGetVar(wp, "submit_button", "");
1596        cprintf("get submit_button = %s\n", submit_button);
1597
1598        need_commit = atoi(websGetVar(wp, "commit", "1"));
1599        cprintf("get need_commit = %d\n", need_commit);
1600
1601        value = websGetVar(wp, "action", "");
1602        cprintf("get action = %s\n", value);
1603
1604  /**********   check action to do **********/
1605
1606  /** Apply **/
1607        if (!strcmp(value, "Apply") || !strcmp(value, "ApplyTake")) {
1608                struct apply_action *act;
1609
1610                cprintf("validate cgi");
1611                validate_cgi(wp);
1612                cprintf("handle apply action\n");
1613                act = handle_apply_action(submit_button);
1614                cprintf("done\n");
1615                // If web page configuration is changed, the EZC configuration
1616                // function should be disabled.(2004-07-29)
1617                nvram_set("is_default", "0");
1618                nvram_set("is_modified", "1");
1619                if (act) {
1620                        fprintf(stderr,
1621                                "%s:submit_button=[%s] service=[%s] sleep_time=[%d] action=[%d]\n",
1622                                value, act->name, act->service, act->sleep_time,
1623                                act->action);
1624
1625                        if ((act->action == SYS_RESTART)
1626                            || (act->action == SERVICE_RESTART)) {
1627
1628                                addAction(act->service);
1629                        }
1630                        sleep_time = act->sleep_time;
1631                        action = act->action;
1632
1633                        if (act->goname)
1634                                start_gozila(act->goname, wp);
1635                } else {
1636                        // nvram_set ("action_service", "");
1637                        sleep_time = 1;
1638                        action = RESTART;
1639                }
1640                diag_led(DIAG, STOP_LED);
1641                sys_commit();
1642        }
1643
1644  /** Restore defaults **/
1645        else if (!strncmp(value, "Restore", 7)) {
1646                ACTION("ACT_SW_RESTORE");
1647                nvram_set("sv_restore_defaults", "1");
1648                killall("udhcpc", SIGKILL);
1649                sys_commit();
1650#ifdef HAVE_X86
1651#ifdef HAVE_ERC
1652                eval("nvram","restore","/etc/defaults/x86ree.backup");
1653                eval("reboot");
1654                eval("event", "5", "1", "15");
1655#endif
1656                char drive[64];
1657                strcpy(drive, getdisc());
1658                FILE *in = fopen64(drive, "r+b");
1659                fseeko64(in, 0, SEEK_END);
1660                __off64_t mtdlen = ftell(in);
1661                fseeko64(in, mtdlen - (65536 * 2), SEEK_SET);
1662                int i;
1663                for (i = 0; i < 65536; i++)
1664                        putc(0, in);    // erase backup area
1665                fclose(in);
1666                eval("mount", "/usr/local", "-o", "remount,rw");
1667                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1668                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1669                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1670                // database
1671                eval("mount", "/usr/local", "-o", "remount,ro");
1672                eval("sync");
1673#elif HAVE_RB600
1674                char drive[64];
1675                sprintf(drive, "/dev/sda");
1676                FILE *in = fopen64(drive, "r+b");
1677                fseeko64(in, 0, SEEK_END);
1678                __off64_t mtdlen = ftell(in);
1679                fseeko64(in, mtdlen - (65536 * 2), SEEK_SET);
1680                int i;
1681                for (i = 0; i < 65536; i++)
1682                        putc(0, in);    // erase backup area
1683                fclose(in);
1684                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1685                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1686                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1687                eval("sync");
1688#elif HAVE_OPENRISC
1689#ifdef HAVE_ERC
1690                eval("cp", "-f", "/etc/defaults/nvram.bin",
1691                     "/usr/local/nvram/nvram.bin");
1692                eval("sync");
1693                eval("sync");
1694                sleep(5);
1695                eval("event", "5", "1", "15");
1696#endif
1697                eval("mount", "/usr/local", "-o", "remount,rw");
1698                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1699                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1700                eval("rm", "-f", "/usr/local/nvram/*"); // delete nvram
1701                // database
1702                eval("mount", "/usr/local", "-o", "remount,ro");
1703#elif HAVE_RB500
1704                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1705                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1706                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1707#elif HAVE_MAGICBOX
1708                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1709                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1710                eval("erase", "nvram");
1711#else
1712                eval("erase", "nvram");
1713#endif
1714                action = REBOOT;
1715        }
1716
1717  /** Reboot **/
1718        else if (!strncmp(value, "Reboot", 6)) {
1719                action = REBOOT;
1720        }
1721
1722        /** GUI Logout **/// Experimental, not work yet ...
1723        else if (!strncmp(value, "Logout", 6)) {
1724                do_ej(NULL, "Logout.asp", wp, NULL);
1725                websDone(wp, 200);
1726                do_logout();
1727                return 1;
1728        }
1729
1730        /*
1731         * DEBUG : Invalid action
1732         */
1733        else
1734                websDebugWrite(wp, "Invalid action %s<br />", value);
1735
1736footer:
1737
1738        if (nvram_match("do_reboot", "1")) {
1739                action = REBOOT;
1740        }
1741        /*
1742         * The will let PC to re-get a new IP Address automatically
1743         */
1744        if (need_reboot)
1745                action = REBOOT;
1746
1747        if (!strcmp(value, "Apply")) {
1748                action = NOTHING;
1749        }
1750
1751        if (action != REBOOT) {
1752                if (my_next_page[0] != '\0')
1753                        sprintf(path, "%s", my_next_page);
1754                else {
1755                        next_page = websGetVar(wp, "next_page", NULL);
1756                        if (next_page)
1757                                sprintf(path, "%s", next_page);
1758                        else
1759                                sprintf(path, "%s.asp", submit_button);
1760                }
1761
1762                cprintf("refresh to %s\n", path);
1763                if (!strncmp(path, "WL_FilterTable", 14))
1764                        do_filtertable(NULL, path, wp, NULL);   // refresh
1765#ifdef HAVE_FREERADIUS
1766                else if (!strncmp(path, "FreeRadiusCert", 14))
1767                        do_radiuscert(NULL, path, wp, NULL);    // refresh     
1768#endif
1769                else if (!strncmp(path, "WL_ActiveTable", 14))
1770                        do_activetable(NULL, path, wp, NULL);   // refresh     
1771                else if (!strncmp(path, "Wireless_WDS", 12))
1772                        do_wds(NULL, path, wp, NULL);   // refresh
1773                else if (!strncmp(path, "Wireless_Advanced", 17))
1774                        do_wireless_adv(NULL, path, wp, NULL);  // refresh
1775                else
1776                        do_ej(NULL, path, wp, NULL);    // refresh
1777                websDone(wp, 200);
1778        } else {
1779#ifndef HAVE_WRK54G
1780                do_ej(NULL, "Reboot.asp", wp, NULL);
1781                websDone(wp, 200);
1782#endif
1783                // sleep (5);
1784                sys_reboot();
1785                return 1;
1786        }
1787
1788        nvram_set("upnp_wan_proto", "");
1789        sleep(sleep_time);
1790        if ((action == RESTART) || (action == SYS_RESTART))
1791                sys_restart();
1792        else if (action == SERVICE_RESTART)
1793                service_restart();
1794
1795        return 1;
1796
1797}
1798
1799//int auth_check( char *dirname, char *authorization )
1800int do_auth(webs_t wp, char *userid, char *passwd, char *realm,
1801            char *authorisation, int (*auth_check) (char *userid, char *passwd,
1802                                                    char *dirname,
1803                                                    char *authorisation))
1804{
1805        strncpy(userid, nvram_safe_get("http_username"), AUTH_MAX);
1806        strncpy(passwd, nvram_safe_get("http_passwd"), AUTH_MAX);
1807        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1808#ifdef HAVE_ERC
1809        strncpy(realm, "LOGIN", AUTH_MAX);
1810        wp->userid = 0;
1811        if (auth_check(userid, passwd, realm, authorisation))
1812                return 1;
1813        wp->userid = 1;
1814        strncpy(userid, zencrypt("SuperAdmin"), AUTH_MAX);
1815        strncpy(passwd, nvram_safe_get("newhttp_passwd"), AUTH_MAX);
1816        if (auth_check(userid, passwd, realm, authorisation))
1817                return 1;
1818        userid = 0;
1819#else
1820        wp->userid = 0;
1821        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1822        if (auth_check(userid, passwd, realm, authorisation))
1823                return 1;
1824#endif
1825        return 0;
1826}
1827
1828int do_cauth(webs_t wp, char *userid, char *passwd, char *realm,
1829             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1830                                                     char *dirname,
1831                                                     char *authorisation))
1832{
1833        if (nvram_match("info_passwd", "0"))
1834                return 1;
1835        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1836}
1837
1838#ifdef HAVE_REGISTER
1839int do_auth_reg(webs_t wp, char *userid, char *passwd, char *realm,
1840                char *authorisation, int (*auth_check) (char *userid,
1841                                                        char *passwd,
1842                                                        char *dirname,
1843                                                        char *authorisation))
1844{
1845        if (!isregistered())
1846                return 1;
1847        return do_auth(wp, userid, passwd, realm, authorisation, auth_check);
1848}
1849#endif
1850
1851#undef HAVE_DDLAN
1852
1853#ifdef HAVE_DDLAN
1854int do_auth2(webs_t wp, char *userid, char *passwd, char *realm,
1855             char *authorisation, int (*auth_check) (char *userid, char *passwd,
1856                                                     char *dirname,
1857                                                     char *authorisation))
1858{
1859        strncpy(userid, nvram_safe_get("http2_username"), AUTH_MAX);
1860        strncpy(passwd, nvram_safe_get("http2_passwd"), AUTH_MAX);
1861        // strncpy(realm, MODEL_NAME, AUTH_MAX);
1862        strncpy(realm, nvram_safe_get("router_name"), AUTH_MAX);
1863        if (auth_check(wp, userid, passwd, realm, authorisation))
1864                return 1;
1865        return 0;
1866}
1867#endif
1868// #ifdef EZC_SUPPORT
1869char ezc_version[128];
1870
1871// #endif
1872
1873extern int post;
1874
1875static char *post_buf = NULL;
1876static void                     // support GET and POST 2003-08-22
1877do_apply_post(char *url, webs_t stream, int len, char *boundary)
1878{
1879        unsigned char buf[1024];
1880        int count;
1881        if (post == 1) {
1882                if (post_buf)
1883                        post_buf = (char *)realloc(post_buf, len + 1);
1884                else
1885                        post_buf = (char *)safe_malloc(len + 1);
1886
1887                if (!post_buf) {
1888                        cprintf("The POST data exceed length limit!\n");
1889                        return;
1890                }
1891                /*
1892                 * Get query
1893                 */
1894                if (!(count = wfread(post_buf, 1, len, stream)))
1895                        return;
1896                post_buf[count] = '\0';;
1897                len -= strlen(post_buf);
1898
1899                /*
1900                 * Slurp anything remaining in the request
1901                 */
1902                while (--len > 0)
1903#ifdef HAVE_HTTPS
1904                        if (do_ssl)
1905                                wfgets(buf, 1, stream);
1906                        else
1907#endif
1908                                (void)fgetc(stream->fp);
1909                init_cgi(post_buf);
1910        }
1911}
1912
1913#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
1914static void do_cfebackup(struct mime_handler *handler, char *url,
1915                         webs_t stream, char *query)
1916{
1917        system2("cat /dev/mtd/0 > /tmp/cfe.bin");
1918        do_file_attach(handler, "/tmp/cfe.bin", stream, NULL, "cfe.bin");
1919        unlink("/tmp/cfe.bin");
1920}
1921#endif
1922
1923#ifdef HAVE_ROUTERSTYLE
1924static void do_stylecss(struct mime_handler *handler, char *url,
1925                        webs_t stream, char *query)
1926{
1927        char *style = nvram_get("router_style");
1928
1929        if (query != NULL)
1930                style = query;
1931
1932        long sdata[30];
1933
1934        long blue[30] =
1935            { 0x36f, 0xfff, 0x68f, 0x24d, 0x24d, 0x68f, 0x57f, 0xccf, 0x78f,
1936                0x35d,
1937                0x35c, 0x78f,
1938                0x78f, 0xfff, 0x9af, 0x46e, 0x46e, 0x9af, 0x36f, 0xccf, 0xfff,
1939                0x69f,
1940                0xfff, 0xfff,
1941                0x999, 0x69f, 0x69f, 0xccf, 0x78f, 0xfff
1942        };
1943
1944        long cyan[30] =
1945            { 0x099, 0xfff, 0x3bb, 0x066, 0x066, 0x3bb, 0x3bb, 0xcff, 0x4cc,
1946                0x1aa,
1947                0x1aa, 0x4cc,
1948                0x6cc, 0xfff, 0x8dd, 0x5bb, 0x5bb, 0x8dd, 0x099, 0xcff, 0xfff,
1949                0x3bb,
1950                0xfff, 0xfff,
1951                0x999, 0x3bb, 0x3bb, 0xcff, 0x6cc, 0xfff
1952        };
1953
1954        long elegant[30] =
1955            { 0x30519c, 0xfff, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7, 0x496fc7,
1956                0xfff, 0x6384cf, 0x6384cf, 0x6384cf, 0x6384cf,
1957                0x6384cf, 0xfff, 0x849dd9, 0x849dd9, 0x849dd9, 0x849dd9,
1958                0x30519c,
1959                0xfff,
1960                0xfff, 0x496fc7, 0xfff, 0xfff,
1961                0x999, 0x496fc7, 0x496fc7, 0xfff, 0x6384cf, 0xfff
1962        };
1963
1964        long green[30] =
1965            { 0x090, 0xfff, 0x3b3, 0x060, 0x060, 0x3b3, 0x3b3, 0xcfc, 0x4c4,
1966                0x1a1,
1967                0x1a1, 0x4c4,
1968                0x6c6, 0xfff, 0x8d8, 0x5b5, 0x5b5, 0x8d8, 0x090, 0xcfc, 0xfff,
1969                0x3b3,
1970                0xfff, 0xfff,
1971                0x999, 0x3b3, 0x3b3, 0xcfc, 0x6c6, 0xfff
1972        };
1973
1974        long orange[30] =
1975            { 0xf26522, 0xfff, 0xff8400, 0xff8400, 0xff8400, 0xff8400, 0xff8400,
1976                0xfff, 0xfeb311, 0xfeb311, 0xfeb311, 0xfeb311,
1977                0xff9000, 0xfff, 0xffa200, 0xffa200, 0xffa200, 0xffa200,
1978                0xf26522,
1979                0xfff,
1980                0xfff, 0xff8400, 0xfff, 0xfff,
1981                0x999, 0xff8400, 0xff8400, 0xfff, 0xff9000, 0xfff
1982        };
1983
1984        long red[30] =
1985            { 0xc00, 0xfff, 0xe33, 0x800, 0x800, 0xe33, 0xd55, 0xfcc, 0xe77,
1986                0xc44,
1987                0xc44, 0xe77,
1988                0xe77, 0xfff, 0xf99, 0xd55, 0xd55, 0xf99, 0xc00, 0xfcc, 0xfff,
1989                0xd55,
1990                0xfff, 0xfff,
1991                0x999, 0xd55, 0xd55, 0xfcc, 0xe77, 0xfff
1992        };
1993
1994        long yellow[30] =
1995            { 0xeec900, 0x000, 0xee3, 0x880, 0x880, 0xee3, 0xffd700, 0x660,
1996                0xee7,
1997                0xbb4,
1998                0xbb4, 0xee7,
1999                0xeec900, 0x000, 0xff9, 0xcc5, 0xcc5, 0xff9, 0xeec900, 0x660,
2000                0x000,
2001                0xffd700,
2002                0x000, 0xfff,
2003                0x999, 0xffd700, 0xeec900, 0x660, 0xffd700, 0x000
2004        };
2005
2006        if (!strcmp(style, "blue"))
2007                memcpy(sdata, blue, 30 * sizeof(long));
2008        else if (!strcmp(style, "cyan"))
2009                memcpy(sdata, cyan, 30 * sizeof(long));
2010        else if (!strcmp(style, "yellow"))
2011                memcpy(sdata, yellow, 30 * sizeof(long));
2012        else if (!strcmp(style, "green"))
2013                memcpy(sdata, green, 30 * sizeof(long));
2014        else if (!strcmp(style, "orange"))
2015                memcpy(sdata, orange, 30 * sizeof(long));
2016        else if (!strcmp(style, "red"))
2017                memcpy(sdata, red, 30 * sizeof(long));
2018        else                    // default to elegant
2019                memcpy(sdata, elegant, 30 * sizeof(long));
2020
2021        websWrite(stream, "@import url(../common.css);\n");
2022        websWrite(stream, "#menuSub,\n");
2023        websWrite(stream, "#menuMainList li span,\n");
2024        websWrite(stream, "#help h2 {\n");
2025        websWrite(stream, "background:#%03x;\n", sdata[0]);
2026        websWrite(stream, "color:#%03x;\n", sdata[1]);
2027        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[2],
2028                  sdata[3], sdata[4], sdata[5]);
2029        websWrite(stream, "}\n");
2030        websWrite(stream, "#menuSubList li a {\n");
2031        websWrite(stream, "background:#%03x;\n", sdata[6]);
2032        websWrite(stream, "color:#%03x;\n", sdata[7]);
2033        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[8],
2034                  sdata[9], sdata[10], sdata[11]);
2035        websWrite(stream, "}\n");
2036        websWrite(stream, "#menuSubList li a:hover {\n");
2037        websWrite(stream, "background:#%03x;\n", sdata[12]);
2038        websWrite(stream, "color:#%03x;\n", sdata[13]);
2039        websWrite(stream, "border-color:#%03x #%03x #%03x #%03x;\n", sdata[14],
2040                  sdata[15], sdata[16], sdata[17]);
2041        websWrite(stream, "}\n");
2042        websWrite(stream, "fieldset legend {\n");
2043        websWrite(stream, "color:#%03x;\n", sdata[18]);
2044        websWrite(stream, "}\n");
2045        websWrite(stream, "#help a {\n");
2046        websWrite(stream, "color:#%03x;\n", sdata[19]);
2047        websWrite(stream, "}\n");
2048        websWrite(stream, "#help a:hover {\n");
2049        websWrite(stream, "color:#%03x;\n", sdata[20]);
2050        websWrite(stream, "}\n");
2051        websWrite(stream, ".meter .bar {\n");
2052        websWrite(stream, "background-color: #%03x;\n", sdata[21]);
2053        websWrite(stream, "}\n");
2054        websWrite(stream, ".meter .text {\n");
2055        websWrite(stream, "color:#%03x;\n", sdata[22]);
2056        websWrite(stream, "}\n");
2057        websWrite(stream, ".progressbar {\n");
2058        websWrite(stream, "background-color: #%03x;\n", sdata[23]);
2059        websWrite(stream, "border-color: #%03x;\n", sdata[24]);
2060        websWrite(stream, "font-size:.09em;\n");
2061        websWrite(stream, "border-width:.09em;\n");
2062        websWrite(stream, "}\n");
2063        websWrite(stream, ".progressbarblock {\n");
2064        websWrite(stream, "background-color: #%03x;\n", sdata[25]);
2065        websWrite(stream, "font-size:.09em;\n");
2066        websWrite(stream, "}\n");
2067        websWrite(stream, "input.button {\n");
2068        websWrite(stream, "background: #%03x;\n", sdata[26]);
2069        websWrite(stream, "color: #%03x;\n", sdata[27]);
2070        websWrite(stream, "}\n");
2071        websWrite(stream, "input.button:hover {\n");
2072        websWrite(stream, "background: #%03x;\n", sdata[28]);
2073        websWrite(stream, "color: #%03x;\n", sdata[29]);
2074        websWrite(stream, "}\n");
2075
2076}
2077
2078static void do_stylecss_ie(struct mime_handler *handler, char *url,
2079                           webs_t stream, char *query)
2080{
2081        websWrite(stream, ".submitFooter input {\n"
2082                  "padding:.362em .453em;\n"
2083                  "}\n"
2084                  "fieldset {\n"
2085                  "padding-top:0;\n"
2086                  "}\n"
2087                  "fieldset legend {\n"
2088                  "margin-left:-9px;\n"
2089                  "margin-bottom:8px;\n" "padding:0 .09em;\n" "}\n");
2090}
2091#endif
2092
2093#ifdef HAVE_REGISTER
2094static void do_trial_logo(struct mime_handler *handler, char *url,
2095                          webs_t stream, char *query)
2096{
2097#if defined(HAVE_TRIMAX) || defined(HAVE_MAKSAT) || defined(HAVE_VILIM) || defined(HAVE_TELCOM) || defined(HAVE_WIKINGS) || defined(HAVE_NEXTMEDIA)
2098        do_file(handler, url, stream, query);
2099#else
2100        if (!isregistered_real()) {
2101                do_file(handler, "style/logo-trial.png", stream, query);
2102        } else {
2103                if (iscpe()) {
2104                        do_file(handler, "style/logo-cpe.png", stream, query);
2105                } else {
2106                        do_file(handler, url, stream, query);
2107                }
2108        }
2109#endif
2110}
2111
2112#endif
2113/*
2114 * static void do_style (char *url, webs_t stream, char *query) { char *style
2115 * = nvram_get ("router_style"); if (style == NULL || strlen (style) == 0)
2116 * do_file ("kromo.css", stream, NULL); else do_file (style, stream, NULL); }
2117 */
2118
2119static void do_mypage(struct mime_handler *handler, char *url,
2120                      webs_t stream, char *query)
2121{
2122        char *snamelist = nvram_safe_get("mypage_scripts");
2123        char *next;
2124        char sname[128];
2125        int qnum;
2126        int i = 1;
2127
2128        if (query == NULL || strlen(query) == 0)
2129                qnum = 1;
2130        else
2131                qnum = atoi(query);
2132
2133        foreach(sname, snamelist, next) {
2134                if (qnum == i) {
2135                        strcat(sname, " > /tmp/mypage.tmp");
2136                        system2(sname);
2137                        do_file_attach(handler, "/tmp/mypage.tmp", stream, NULL,
2138                                       "MyPage.asp");
2139                        unlink("/tmp/mypage.tmp");
2140                }
2141                i++;
2142        }
2143
2144        return;
2145
2146}
2147
2148static void do_fetchif(struct mime_handler *handler, char *url,
2149                       webs_t stream, char *query)
2150{
2151        char line[256];
2152        int i, llen;
2153        char buffer[256];
2154        char querybuffer[32];
2155
2156        if (query == NULL || strlen(query) == 0)
2157                return;
2158
2159        strncpy(querybuffer, query, 30);
2160        strcat(querybuffer, ":");
2161
2162        int strbuffer = 0;
2163        time_t tm;
2164        struct tm tm_time;
2165
2166        time(&tm);
2167        memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
2168        char *date_fmt = "%a %b %e %H:%M:%S %Z %Y";
2169
2170        strftime(buffer, 200, date_fmt, &tm_time);
2171        strbuffer = strlen(buffer);
2172        buffer[strbuffer++] = '\n';
2173        FILE *in = fopen("/proc/net/dev", "rb");
2174
2175        if (in == NULL)
2176                return;
2177
2178        /* eat first two lines */
2179        fgets(line, sizeof(line), in);
2180        fgets(line, sizeof(line), in);
2181
2182        while (fgets(line, sizeof(line), in) != NULL) {
2183                if (strstr(line, querybuffer)) {
2184                        llen = strlen(line);
2185                        for (i = 0; i < llen; i++) {
2186                                buffer[strbuffer++] = line[i];
2187                        }
2188                        break;
2189                }
2190        }
2191        fclose(in);
2192
2193        buffer[strbuffer] = 0;
2194        websWrite(stream, "%s", buffer);
2195}
2196
2197static char *getLanguageName()
2198{
2199        char *lang = nvram_get("language");
2200
2201        cprintf("get language %s\n", lang);
2202        char *l = safe_malloc(60);
2203
2204        if (lang == NULL) {
2205                cprintf("return default\n");
2206                sprintf(l, "lang_pack/english.js");
2207                return l;
2208        }
2209        sprintf(l, "lang_pack/%s.js", lang);
2210        cprintf("return %s\n", l);
2211        return l;
2212}
2213
2214char *live_translate(char *tran)
2215{
2216
2217        FILE *fp;
2218        static char temp[256], temp1[256];
2219        char *temp2;
2220        if (tran == NULL || !strlen(tran))
2221                return "";
2222        char *lang = getLanguageName();
2223        char buf[64];
2224        int start, filelen, pos;
2225
2226        memset(temp, 0, sizeof(temp));
2227        memset(temp1, 0, sizeof(temp));
2228
2229        sprintf(buf, "%s", lang);
2230        free(lang);
2231
2232        strcpy(temp1, tran);
2233        strcat(temp1, "=\"");
2234
2235        int len = strlen(temp1);
2236
2237        fp = getWebsFile(buf);
2238        if (fp) {
2239                start = ftell(fp);
2240                filelen = getWebsFileLen(buf);
2241
2242                while (fgets(temp, 256, fp) != NULL) {
2243                        pos = ftell(fp);
2244
2245                        if ((pos - start) > filelen)
2246                                break;
2247                        if ((memcmp(temp, temp1, len)) == 0) {
2248                                temp2 = strtok(temp, "\"");
2249                                temp2 = strtok(NULL, "\"");
2250
2251                                fclose(fp);
2252                                return temp2;
2253                        }
2254                }
2255                fclose(fp);
2256        }
2257
2258        strcpy(buf, "lang_pack/english.js");    // if string not found, try english
2259        fp = getWebsFile(buf);
2260
2261        if (fp == NULL)
2262                return "Error";
2263        start = ftell(fp);
2264        filelen = getWebsFileLen(buf);
2265
2266        while (fgets(temp, 256, fp) != NULL) {
2267                pos = ftell(fp);
2268
2269                if ((pos - start) > filelen)
2270                        break;
2271                if ((memcmp(temp, temp1, len)) == 0) {
2272                        temp2 = strtok(temp, "\"");
2273                        temp2 = strtok(NULL, "\"");
2274
2275                        fclose(fp);
2276                        return temp2;
2277                }
2278        }
2279        fclose(fp);
2280
2281        return "Error";         // not found
2282
2283}
2284
2285static void do_ttgraph(struct mime_handler *handler, char *url,
2286                       webs_t stream, char *query)
2287{
2288#define COL_WIDTH 16            /* single column width */
2289
2290        char *next;
2291        char var[80];
2292
2293        unsigned int days;
2294        unsigned int month;
2295        unsigned int year;
2296        int wd;
2297        int i = 0;
2298        char months[12][12] =
2299            { "share.jan", "share.feb", "share.mar", "share.apr", "share.may",
2300                "share.jun",
2301                "share.jul", "share.aug", "share.sep", "share.oct", "share.nov",
2302                "share.dec"
2303        };
2304        unsigned long rcvd[31] =
2305            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2306                0,
2307                0,
2308                0, 0, 0, 0, 0, 0, 0
2309        };
2310        unsigned long sent[31] =
2311            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2312                0,
2313                0,
2314                0, 0, 0, 0, 0, 0, 0
2315        };
2316        unsigned long max = 5, smax = 5, f = 1;
2317        unsigned long totin = 0;
2318        unsigned long totout = 0;
2319
2320        if (sscanf(query, "%u-%u", &month, &year) != 2)
2321                return;
2322        if (month < 1 || month > 12)
2323                return;
2324
2325        days = daysformonth(month, year);
2326        wd = weekday(month, 1, year);   // first day in month (mon=0, tue=1,
2327        // ..., sun=6)
2328
2329        char tq[32];
2330
2331        sprintf(tq, "traff-%02u-%u", month, year);
2332        char *tdata = nvram_safe_get(tq);
2333
2334        if (tdata != NULL && strlen(tdata)) {
2335                foreach(var, tdata, next) {
2336                        if (i == days)
2337                                break;  //skip monthly total
2338                        int ret = sscanf(var, "%lu:%lu", &rcvd[i], &sent[i]);
2339                        if (ret != 2)
2340                                break;
2341                        totin += rcvd[i];
2342                        totout += sent[i];
2343                        if (rcvd[i] > max)
2344                                max = rcvd[i];
2345                        if (sent[i] > max)
2346                                max = sent[i];
2347                        i++;
2348                }
2349        }
2350
2351        while (max > smax) {
2352                if (max > (f * 5))
2353                        smax = f * 10;
2354                if (max > (f * 10))
2355                        smax = f * 25;
2356                if (max > (f * 25))
2357                        smax = f * 50;
2358                f = f * 10;
2359        }
2360
2361        char incom[32];
2362
2363        snprintf(incom, 32, "%s", live_translate("status_inet.traffin"));
2364        char outcom[32];
2365
2366        snprintf(outcom, 32, "%s", live_translate("status_inet.traffout"));
2367        char monthname[32];
2368
2369        snprintf(monthname, 32, "%s", live_translate(months[month - 1]));
2370
2371        websWrite(stream,
2372                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
2373        websWrite(stream, "<html>\n");
2374        websWrite(stream, "<head>\n");
2375        websWrite(stream,
2376                  "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
2377                  live_translate("lang_charset.set"));
2378        websWrite(stream, "<title>dd-wrt traffic graph</title>\n");
2379
2380        websWrite(stream, "<script type=\"text/javascript\">\n");
2381        websWrite(stream, "//<![CDATA[\n");
2382        websWrite(stream, "function Show(label) {\n");
2383        websWrite(stream,
2384                  "document.getElementById(\"label\").innerHTML = label;\n");
2385        websWrite(stream, "}\n");
2386        websWrite(stream, "//]]>\n");
2387        websWrite(stream, "</script>\n");
2388
2389        websWrite(stream, "<style type=\"text/css\">\n\n");
2390        websWrite(stream,
2391                  "#t-graph {position: relative; width: %upx; height: 300px;\n",
2392                  days * COL_WIDTH);
2393        websWrite(stream, "  margin: 1.1em 0 3.5em; padding: 0;\n");
2394        websWrite(stream, "  border: 1px solid gray; list-style: none;\n");
2395        websWrite(stream, "  font: 9px Tahoma, Arial, sans-serif;}\n");
2396        websWrite(stream,
2397                  "#t-graph ul {margin: 0; padding: 0; list-style: none;}\n");
2398        websWrite(stream,
2399                  "#t-graph li {position: absolute; bottom: 0; width: %dpx; z-index: 2;\n",
2400                  COL_WIDTH);
2401        websWrite(stream, "  margin: 0; padding: 0;\n");
2402        websWrite(stream, "  text-align: center; list-style: none;}\n");
2403        websWrite(stream,
2404                  "#t-graph li.day {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #AAA;}\n");
2405        websWrite(stream,
2406                  "#t-graph li.day_sun {height: 298px; padding-top: 2px; border-right: 1px dotted #C4C4C4; color: #E00;}\n");
2407        websWrite(stream,
2408                  "#t-graph li.bar {width: 4px; border: 1px solid; border-bottom: none; color: #000;}\n");
2409        websWrite(stream, "#t-graph li.bar p {margin: 5px 0 0; padding: 0;}\n");
2410        websWrite(stream, "#t-graph li.rcvd {left: 3px; background: #228B22;}\n");      // set
2411        // rcvd
2412        // bar
2413        // colour
2414        // here
2415        // (green)
2416        websWrite(stream, "#t-graph li.sent {left: 8px; background: #CD0000;}\n");      // set
2417        // sent
2418        // bar
2419        // colour
2420        // here
2421        // (red)
2422
2423        for (i = 0; i < days - 1; i++) {
2424                websWrite(stream, "#t-graph #d%d {left: %dpx;}\n", i + 1,
2425                          i * COL_WIDTH);
2426        }
2427        websWrite(stream, "#t-graph #d%u {left: %upx; border-right: none;}\n",
2428                  days, (days - 1) * COL_WIDTH);
2429
2430        websWrite(stream,
2431                  "#t-graph #ticks {width: %upx; height: 300px; z-index: 1;}\n",
2432                  days * COL_WIDTH);
2433        websWrite(stream,
2434                  "#t-graph #ticks .tick {position: relative; border-bottom: 1px solid #BBB; width: %upx;}\n",
2435                  days * COL_WIDTH);
2436        websWrite(stream,
2437                  "#t-graph #ticks .tick p {position: absolute; left: 100%%; top: -0.67em; margin: 0 0 0 0.5em;}\n");
2438        websWrite(stream,
2439                  "#t-graph #label {width: 500px; bottom: -20px;  z-index: 1; font: 12px Tahoma, Arial, sans-serif; font-weight: bold;}\n");
2440        websWrite(stream, "</style>\n");
2441        websWrite(stream, "</head>\n\n");
2442        websWrite(stream, "<body>\n");
2443        websWrite(stream, "<ul id=\"t-graph\">\n");
2444
2445        for (i = 0; i < days; i++) {
2446                websWrite(stream, "<li class=\"day%s\" id=\"d%d\" ",
2447                          (wd % 7) == 6 ? "_sun" : "", i + 1);
2448                wd++;
2449                websWrite(stream,
2450                          "onmouseover=\"Show(\'%s %d, %d (%s: %lu MB / %s: %lu MB)\')\" ",
2451                          monthname, i + 1, year, incom, rcvd[i], outcom,
2452                          sent[i]);
2453                websWrite(stream,
2454                          "onmouseout=\"Show(\'%s %d (%s: %lu MB / %s: %lu MB)\')\"",
2455                          monthname, year, incom, totin, outcom, totout);
2456                websWrite(stream, ">%d\n", i + 1);
2457                websWrite(stream, "<ul>\n");
2458                websWrite(stream,
2459                          "<li class=\"rcvd bar\" style=\"height: %lupx;\"><p></p></li>\n",
2460                          rcvd[i] * 300 / smax);
2461                websWrite(stream,
2462                          "<li class=\"sent bar\" style=\"height: %lupx;\"><p></p></li>\n",
2463                          sent[i] * 300 / smax);
2464                websWrite(stream, "</ul>\n");
2465                websWrite(stream, "</li>\n");
2466        }
2467
2468        websWrite(stream, "<li id=\"ticks\">\n");
2469        for (i = 5; i; i--)     // scale
2470        {
2471                websWrite(stream,
2472                          "<div class=\"tick\" style=\"height: 59px;\"><p>%d%sMB</p></div>\n",
2473                          smax * i / 5, (smax > 10000) ? " " : "&nbsp;");
2474        }
2475        websWrite(stream, "</li>\n\n");
2476
2477        websWrite(stream, "<li id=\"label\">\n");
2478        websWrite(stream, "%s %d (%s: %lu MB / %s: %lu MB)\n", monthname, year,
2479                  incom, totin, outcom, totout);
2480        websWrite(stream, "</li>\n");
2481
2482        websWrite(stream, "</ul>\n\n");
2483        websWrite(stream, "</body>\n");
2484        websWrite(stream, "</html>\n");
2485
2486}
2487
2488static void ttraff_backup(struct mime_handler *handler, char *url,
2489                          webs_t stream, char *query)
2490{
2491        system2("echo TRAFF-DATA > /tmp/traffdata.bak");
2492        system2("nvram show | grep traff- >> /tmp/traffdata.bak");
2493        do_file_attach(handler, "/tmp/traffdata.bak", stream, NULL,
2494                       "traffdata.bak");
2495        unlink("/tmp/traffdata.bak");
2496}
2497
2498static void do_apply_cgi(struct mime_handler *handler, char *url,
2499                         webs_t stream, char *q)
2500{
2501        char *path, *query;
2502
2503        if (post == 1) {
2504                query = post_buf;
2505                path = url;
2506        } else {
2507                query = url;
2508                path = strsep(&query, "?") ? : url;
2509                init_cgi(query);
2510        }
2511
2512        if (!query)
2513                return;
2514
2515        apply_cgi(stream, NULL, NULL, 0, url, path, query);
2516        init_cgi(NULL);
2517}
2518
2519#ifdef HAVE_MADWIFI
2520extern int getdevicecount(void);
2521#endif
2522
2523#ifdef HAVE_LANGUAGE
2524static void do_language(struct mime_handler *handler, char *path, webs_t stream, char *query)   // jimmy,
2525                                                                        // https,
2526                                                                        // 8/4/2003
2527{
2528        char *lang = getLanguageName();
2529
2530        do_file(handler, lang, stream, NULL);
2531        free(lang);
2532        return;
2533}
2534#endif
2535
2536extern int issuperchannel(void);
2537
2538static char no_cache[] =
2539    "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "Expires: 0";
2540
2541struct mime_handler mime_handlers[] = {
2542        // { "ezconfig.asp", "text/html", ezc_version, do_apply_ezconfig_post,
2543        // do_ezconfig_asp, do_auth },
2544#ifdef HAVE_SKYTRON
2545        {"setupindex*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2546#endif
2547#ifdef HAVE_POKER
2548        {"PokerEdit.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2549#endif
2550#ifdef HAVE_DDLAN
2551        {"Upgrade*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2552        {"Management*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2553        {"Services*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2554        {"Hotspot*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2555        {"Wireless*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2556        {"WL_*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2557        {"WPA*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2558        {"Log*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2559        {"Alive*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2560        {"Diagnostics*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2561        {"Wol*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2562        {"Factory_Defaults*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2563        {"config*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2564#endif
2565
2566        {"changepass.asp", "text/html", no_cache, NULL, do_ej, NULL, 1},
2567#ifdef HAVE_REGISTER
2568        {"register.asp", "text/html", no_cache, NULL, do_ej, do_auth_reg, 1},
2569#endif
2570        {"WL_FilterTable*", "text/html", no_cache, NULL, do_filtertable,
2571         do_auth, 1},
2572#ifdef HAVE_FREERADIUS
2573        {"FreeRadiusCert*", "text/html", no_cache, NULL, do_radiuscert, do_auth,
2574         1},
2575        {"freeradius-certs/*", "application/octet-stream", no_cache, NULL,
2576         cert_file_out, do_auth, 0},
2577#endif
2578        // #endif
2579        // #ifdef HAVE_MADWIFI
2580        {"Wireless_WDS*", "text/html", no_cache, NULL, do_wds, do_auth, 1},
2581        {"WL_ActiveTable*", "text/html", no_cache, NULL, do_activetable,
2582         do_auth, 1},
2583        {"Wireless_Advanced*", "text/html", no_cache, NULL, do_wireless_adv,
2584         do_auth, 1},
2585        // #endif
2586        {"MyPage.asp*", "text/html", no_cache, NULL, do_mypage, do_auth, 1},
2587        {"**.asp", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2588        {"**.JPG", "image/jpeg", no_cache, NULL, do_file, NULL, 0},
2589        // {"style.css", "text/css", NULL, NULL, do_style, NULL},
2590        {"common.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2591#ifdef HAVE_LANGUAGE
2592        {"lang_pack/language.js", "text/javascript", NULL, NULL, do_language,
2593         NULL, 0},
2594#endif
2595#ifdef HAVE_BUFFALO
2596        {"vsp.html", "text/plain", no_cache, NULL, do_vsp_page, NULL, 1},
2597#endif
2598        {"SysInfo.htm*", "text/plain", no_cache, NULL, do_ej, do_auth, 1},
2599#ifdef HAVE_SKYTRON
2600        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2601        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_auth, 1},
2602        {"**.htm", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2603        {"**.html", "text/html", no_cache, NULL, do_ej, do_auth2, 1},
2604#else
2605        {"Info.htm*", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2606        {"Info.live.htm", "text/html", no_cache, NULL, do_ej, do_cauth, 1},
2607        {"**.htm", "text/html", no_cache, NULL, do_ej, NULL, 1},
2608        {"**.html", "text/html", no_cache, NULL, do_ej, NULL, 1},
2609
2610#endif
2611#ifdef HAVE_ROUTERSTYLE
2612        {"style/blue/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2613        {"style/cyan/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2614        {"style/elegant/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2615         1},
2616        {"style/green/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2617        {"style/orange/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2618         1},
2619        {"style/red/style.css", "text/css", NULL, NULL, do_stylecss, NULL, 1},
2620        {"style/yellow/style.css", "text/css", NULL, NULL, do_stylecss, NULL,
2621         1},
2622        {"style/blue/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2623         NULL,
2624         1},
2625        {"style/cyan/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2626         NULL,
2627         1},
2628        {"style/elegant/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2629         NULL, 1},
2630        {"style/green/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2631         NULL,
2632         1},
2633        {"style/orange/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2634         NULL, 1},
2635        {"style/red/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie, NULL,
2636         1},
2637        {"style/yellow/style_ie.css", "text/css", NULL, NULL, do_stylecss_ie,
2638         NULL, 1},
2639#endif
2640#ifdef HAVE_REGISTER
2641        {"style/logo.png", "image/png", NULL, NULL, do_trial_logo, NULL, 0},
2642#endif
2643        {"**.css", "text/css", NULL, NULL, do_file, NULL, 0},
2644        {"**.svg", "image/svg+xml", NULL, NULL, do_file, NULL, 0},
2645        {"**.gif", "image/gif", NULL, NULL, do_file, NULL, 0},
2646        {"**.png", "image/png", NULL, NULL, do_file, NULL, 0},
2647        {"**.jpg", "image/jpeg", NULL, NULL, do_file, NULL, 0},
2648        {"**.ico", "image/x-icon", NULL, NULL, do_file, NULL, 0},
2649        {"**.js", "text/javascript", NULL, NULL, do_file, NULL, 0},
2650        {"**.swf", "application/x-shockwave-flash", NULL, NULL, do_file, NULL,
2651         0},
2652        {"**.pdf", "application/pdf", NULL, NULL, do_file, NULL, 0},
2653        {"**.mp4", "video/mp4", NULL, NULL, do_file, NULL, 0},
2654        {"**.mp3", "audio/mpeg3", NULL, NULL, do_file, NULL, 0},
2655        {"**.mpg", "video/mpeg", NULL, NULL, do_file, NULL, 0},
2656        {"**.avi", "video/x-msvideo", NULL, NULL, do_file, NULL, 0},
2657        {"**.wma", "audio/x-ms-wma", NULL, NULL, do_file, NULL, 0},
2658        {"**.wmv", "video/x-ms-wmv", NULL, NULL, do_file, NULL, 0},
2659        {"**.flv", "video/x-flv", NULL, NULL, do_file, NULL, 0},
2660#ifdef HAVE_SKYTRON
2661        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2662         do_auth2, 1},
2663#elif HAVE_DDLAN
2664        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2665         NULL, 1},
2666#else
2667        {"applyuser.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2668         do_auth, 1},
2669#endif
2670        {"fetchif.cgi*", "text/html", no_cache, NULL, do_fetchif, do_auth, 1},
2671#ifdef HAVE_DDLAN
2672        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi, NULL,
2673         1},
2674        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2675         NULL, 1},
2676#else
2677        {"apply.cgi*", "text/html", no_cache, do_apply_post, do_apply_cgi,
2678         do_auth, 1},
2679        {"upgrade.cgi*", "text/html", no_cache, do_upgrade_post, do_upgrade_cgi,
2680         do_auth, 1},
2681#endif
2682        // {"Gozila.cgi*", "text/html", no_cache, NULL, do_setup_wizard,
2683        // do_auth}, // for setup wizard
2684        /*
2685         * { "**.cfg", "application/octet-stream", no_cache, NULL, do_backup,
2686         * do_auth },
2687         */
2688#ifdef HAVE_DDLAN
2689        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2690         do_upgrade_cgi,
2691         NULL, 1},
2692#else
2693        {"restore.cgi**", "text/html", no_cache, do_upgrade_post,
2694         do_upgrade_cgi,
2695         do_auth, 1},
2696#endif
2697        {"test.bin**", "application/octet-stream", no_cache, NULL, do_file,
2698         do_auth, 0},
2699
2700#ifdef HAVE_DDLAN
2701        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2702         nv_file_out, do_auth2, 0},
2703        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2704         nv_file_out,
2705         do_auth2, 0},
2706        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi, NULL,
2707         1},
2708#else
2709        {"nvrambak.bin*", "application/octet-stream", no_cache, NULL,
2710         nv_file_out, do_auth, 0},
2711        {"nvrambak**.bin*", "application/octet-stream", no_cache, NULL,
2712         nv_file_out,
2713         do_auth, 0},
2714        {"nvram.cgi*", "text/html", no_cache, nv_file_in, sr_config_cgi,
2715         do_auth,
2716         1},
2717#endif
2718#if !defined(HAVE_X86) && !defined(HAVE_MAGICBOX)
2719        {"backup/cfe.bin", "application/octet-stream", no_cache, NULL,
2720         do_cfebackup,
2721         do_auth, 0},
2722#endif
2723        {"ttgraph.cgi*", "text/html", no_cache, NULL, do_ttgraph, do_auth, 1},
2724        {"traffdata.bak*", "text/html", no_cache, NULL, ttraff_backup,
2725         do_auth, 0},
2726        {"tadmin.cgi*", "text/html", no_cache, td_file_in, td_config_cgi,
2727         NULL, 1},
2728        {"*", "application/octet-stream", no_cache, NULL, do_file, do_auth, 1},
2729        // for ddm
2730        {NULL, NULL, NULL, NULL, NULL, NULL, 0}
2731};
2732
2733/*
2734 * Format: type = SET : " " => "&nbsp;" , ":" => "&semi;" type = GET :
2735 * "&nbsp;" => " " , "&semi;" => ":" Example: name1 = test 123:abc
2736 * filter_name("name1", new_name, SET); new_name="test&nbsp;123&semi;abc"
2737 * name2 = test&nbsp;123&semi;abc filter_name("name2", new_name, GET);
2738 * new_name="test 123:abc"
2739 */
2740int httpd_filter_name(char *old_name, char *new_name, size_t size, int type)
2741{
2742        int i, j, match;
2743
2744        cprintf("httpd_filter_name\n");
2745
2746        struct pattern {
2747                char ch;
2748                char *string;
2749        };
2750
2751        struct pattern patterns[] = {
2752                {' ', "&nbsp;"},
2753                {':', "&semi;"},
2754                {'<', "&lt;"},
2755                {'>', "&gt;"},
2756        };
2757
2758        struct pattern *v;
2759
2760        strcpy(new_name, "");
2761
2762        switch (type) {
2763        case SET:
2764                for (i = 0; *(old_name + i); i++) {
2765                        match = 0;
2766                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2767                             v++) {
2768                                if (*(old_name + i) == v->ch) {
2769                                        if (strlen(new_name) + strlen(v->string) > size) {      // avoid overflow
2770                                                cprintf("%s(): overflow\n",
2771                                                        __FUNCTION__);
2772                                                new_name[strlen(new_name)] =
2773                                                    '\0';
2774                                                return 1;
2775                                        }
2776                                        sprintf(new_name + strlen(new_name),
2777                                                "%s", v->string);
2778                                        match = 1;
2779                                        break;
2780                                }
2781                        }
2782                        if (!match) {
2783                                if (strlen(new_name) + 1 > size) {
2784                                        cprintf("%s(): overflow\n", __FUNCTION__);      // avoid
2785                                        // overflow
2786                                        new_name[strlen(new_name)] = '\0';
2787                                        return 1;
2788                                }
2789                                sprintf(new_name + strlen(new_name), "%c",
2790                                        *(old_name + i));
2791                        }
2792                }
2793
2794                break;
2795        case GET:
2796                for (i = 0, j = 0; *(old_name + j); j++) {
2797                        match = 0;
2798                        for (v = patterns; v < &patterns[STRUCT_LEN(patterns)];
2799                             v++) {
2800                                if (!memcmp
2801                                    (old_name + j, v->string,
2802                                     strlen(v->string))) {
2803                                        *(new_name + i) = v->ch;
2804                                        j = j + strlen(v->string) - 1;
2805                                        match = 1;
2806                                        break;
2807                                }
2808                        }
2809                        if (!match)
2810                                *(new_name + i) = *(old_name + j);
2811
2812                        i++;
2813                }
2814                *(new_name + i) = '\0';
2815                break;
2816        default:
2817                cprintf("%s():Invalid type!\n", __FUNCTION__);
2818                break;
2819        }
2820        // cprintf("%s():new_name=[%s]\n", __FUNCTION__, new_name);
2821
2822        return 1;
2823}
2824
2825#ifdef HAVE_BUFFALO
2826void do_vsp_page(struct mime_handler *handler, char *url,
2827                 webs_t stream, char *query)
2828{
2829/*
2830#ifdef HAVE_MADWIFI
2831        char *ifname = "ath0";
2832#else
2833        char *ifname = "wl0";
2834#endif
2835
2836        char *authmode = nvram_nget("%s_security_mode", ifname);
2837        char *encrypt = nvram_nget("%s_crypto", ifname);
2838        char *wpakey = nvram_nget("%s_wpa_psk ", ifname);
2839
2840        if (!strcmp(encrypt, "tkip")) {
2841                encrypt = "TKIP";
2842        } else if (!strcmp(authmode, "aes")) {
2843                encrypt = "AES";
2844        } else if (!strcmp(authmode, "tkip+aes")) {
2845                encrypt = "TKIP-AES-MIX";
2846        } else {
2847                encrypt = "";
2848        }
2849
2850        if (!strcmp(authmode, "disabled")) {
2851                authmode = "NONE";
2852                encrypt = "";
2853                wpakey = "";
2854        } else if (!strcmp(authmode, "wep")) {
2855                authmode = "WEP";
2856                encrypt = "";
2857                wpakey = "";
2858        } else if (!strcmp(authmode, "radius")
2859                   || !strcmp(authmode, "wpa")
2860                   || !strcmp(authmode, "wpa2")
2861                   || !strcmp(authmode, "wpa wpa2")) {
2862                authmode = "RADIUS";
2863                encrypt = "";
2864                wpakey = "";
2865        } else if (!strcmp(authmode, "8021X")) {
2866                authmode = "802.1X";
2867                encrypt = "";
2868                wpakey = "";
2869        } else if (!strcmp(authmode, "psk")) {
2870                authmode = "WPA";
2871        } else if (!strcmp(authmode, "psk2")) {
2872                authmode = "WPA2";
2873        } else if (!strcmp(authmode, "psk psk2")) {
2874                authmode = "WPA-WPA2-MIX";
2875        } else {
2876                authmode = "UNKNOWN";
2877                encrypt = "";
2878                wpakey = "";
2879        }
2880*/
2881        websWrite(stream, "<html>\n");
2882        websWrite(stream, "<head>\n");
2883        websWrite(stream, "<title>VSP</title>\n");
2884        websWrite(stream, "</head>\n");
2885        websWrite(stream, "<body>\n");
2886        websWrite(stream, "<pre>\n");
2887
2888        websWrite(stream, "DEVICE_VSP_VERSION=0.1\n");
2889        websWrite(stream, "DEVICE_VENDOR=BUFFALO INC.\n");
2890        websWrite(stream, "DEVICE_MODEL=%s DDWRT\n",
2891                  nvram_safe_get("DD_BOARD"));
2892        websWrite(stream, "DEVICE_FIRMWARE_VERSION=1.00\n");
2893        websWrite(stream, "WIRELESS_DEVICE_NUMBER=1\n");
2894//      websWrite(stream, "WIRELESS_1_PRESET_AUTHMODE=%s\n", authmode);
2895//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT=%s\n", encrypt);
2896//      websWrite(stream, "WIRELESS_1_PRESET_ENCRYPT_KEY=%s\n", wpakey);
2897        websWrite(stream, "DEVICE_URL_GET=/vsp.html\n");
2898        websWrite(stream, "DEVICE_URL_SET=/vsp.html\n");
2899
2900        websWrite(stream, "</pre>\n");
2901        websWrite(stream, "</body>\n");
2902        websWrite(stream, "</html>\n");
2903}
2904#endif
Note: See TracBrowser for help on using the repository browser.