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

Last change on this file since 17576 was 17576, checked in by BrainSlayer, 21 months ago

cgi hook added

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