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

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

some private fun

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