source: src/router/httpd/visuals/ejs.c @ 17713

Last change on this file since 17713 was 17713, checked in by BrainSlayer, 20 months ago

handling gui special case

File size: 92.0 KB
Line 
1#define VISUALSOURCE 1
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <stdarg.h>
6#include <string.h>
7#include <unistd.h>
8#include <ctype.h>
9#include <signal.h>
10
11#include <sys/ioctl.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <sys/socket.h>
15#include <sys/statfs.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <broadcom.h>
19#include <cymac.h>
20#include <wlutils.h>
21#include <bcmparams.h>
22#include <dirent.h>
23#include <netdb.h>
24#include <utils.h>
25#include <bcmnvram.h>
26#include <revision.h>
27#include <shutils.h>
28#ifdef HAVE_SAMBA_SERVER
29#include <jansson.h>
30#endif
31#include "webs.h"
32
33void (*do_ej_buffer) (char *buffer, webs_t stream) = NULL;
34int (*httpd_filter_name) (char *old_name, char *new_name, size_t size,
35                          int type) = NULL;
36char *(*websGetVar) (webs_t wp, char *var, char *d) = NULL;
37int (*websWrite) (webs_t wp, char *fmt, ...) = NULL;
38struct wl_client_mac *wl_client_macs = NULL;
39void (*do_ej) (struct mime_handler * handler, char *path, webs_t stream, char *query) = NULL;   // jimmy,
40                                                                        // https,
41                                                                        // 8/4/2003
42int (*ejArgs) (int argc, char_t ** argv, char_t * fmt, ...) = NULL;
43FILE *(*getWebsFile) (char *path) = NULL;
44int (*wfflush) (webs_t fp) = NULL;
45int (*wfputc) (char c, webs_t fp) = NULL;
46int (*wfputs) (char *buf, webs_t fp) = NULL;
47char *(*live_translate) (char *tran) = NULL;
48websRomPageIndexType *websRomPageIndex = NULL;
49char *(*GOZILA_GET) (webs_t wp, char *name) = NULL;
50void (*validate_cgi) (webs_t fp) = NULL;
51
52#ifdef HAVE_HTTPS
53int do_ssl;
54#endif
55
56void initWeb(struct Webenvironment *env)
57{
58
59        websGetVar = env->PwebsGetVar;
60        httpd_filter_name = env->Phttpd_filter_name;
61        wl_client_macs = env->Pwl_client_macs;
62        websWrite = env->PwebsWrite;
63        do_ej_buffer = env->Pdo_ej_buffer;
64        do_ej = env->Pdo_ej;
65#ifdef HAVE_HTTPS
66        do_ssl = env->Pdo_ssl;
67#endif
68        ejArgs = env->PejArgs;
69        getWebsFile = env->PgetWebsFile;
70        wfflush = env->Pwfflush;
71        wfputc = env->Pwfputc;
72        wfputs = env->Pwfputs;
73        websRomPageIndex = env->PwebsRomPageIndex;
74        live_translate = env->Plive_translate;
75        GOZILA_GET = env->PGOZILA_GET;
76        validate_cgi = env->Pvalidate_cgi;
77}
78
79struct onload onloads[] = {
80        // { "Filters", filter_onload },
81        {"WL_ActiveTable-wl0", wl_active_onload},
82        {"WL_ActiveTable-wl1", wl_active_onload},
83        {"MACClone", macclone_onload},
84        {"FilterSummary", filtersummary_onload},
85        {"Ping", ping_onload},
86        // {"Traceroute", traceroute_onload},
87};
88
89void ej_onload(webs_t wp, int argc, char_t ** argv)
90{
91        char *type, *arg;
92        struct onload *v;
93
94#ifndef FASTWEB
95        if (argc < 2) {
96                websError(wp, 400, "Insufficient args\n");
97                return;
98        }
99#endif
100        type = argv[0];
101        arg = argv[1];
102
103        for (v = onloads; v < &onloads[STRUCT_LEN(onloads)]; v++) {
104                if (!strcmp(v->name, type)) {
105                        v->go(wp, arg);
106                        return;
107                }
108        }
109
110        return;
111}
112
113/*
114 * Meta tag command that will no allow page cached by browsers. The will
115 * force the page to be refreshed when visited.
116 */
117void ej_no_cache(webs_t wp, int argc, char_t ** argv)
118{
119        websWrite(wp, "<meta http-equiv=\"expires\" content=\"0\">\n");
120        websWrite(wp,
121                  "<meta http-equiv=\"cache-control\" content=\"no-cache\">\n");
122        websWrite(wp, "<meta http-equiv=\"pragma\" content=\"no-cache\">\n");
123
124        return;
125}
126
127void prefix_ip_get(char *name, char *buf, int type)
128{
129        if (type == 1)
130                sprintf(buf, "%d.%d.%d.",
131                        get_single_ip(nvram_safe_get(name), 0),
132                        get_single_ip(nvram_safe_get(name), 1),
133                        get_single_ip(nvram_safe_get(name), 2));
134        if (type == 2)
135                sprintf(buf, "%d.%d.", get_single_ip(nvram_safe_get(name), 0),
136                        get_single_ip(nvram_safe_get(name), 1));
137}
138
139/*
140 * Example:
141 * lan_ipaddr=192.168.1.1
142 * <% prefix_ip_get("lan_ipaddr",1); %> produces "192.168.1."
143 */
144void ej_prefix_ip_get(webs_t wp, int argc, char_t ** argv)
145{
146        char *name;
147        int type;
148
149#ifndef FASTWEB
150        if (argc < 2) {
151                websError(wp, 400, "Insufficient args\n");
152                return;
153        }
154#endif
155        name = argv[0];
156        type = atoi(argv[1]);
157
158        if (type == 1)
159                websWrite(wp, "%d.%d.%d.",
160                          get_single_ip(nvram_safe_get(name), 0),
161                          get_single_ip(nvram_safe_get(name), 1),
162                          get_single_ip(nvram_safe_get(name), 2));
163        if (type == 2)
164                websWrite(wp, "%d.%d.", get_single_ip(nvram_safe_get(name), 0),
165                          get_single_ip(nvram_safe_get(name), 1));
166
167        return;
168}
169
170/*
171 * Example:
172 * lan_ipaddr = 192.168.1.1
173 * <% nvram_get("lan_ipaddr"); %> produces "192.168.1.1"
174 */
175void ej_nvram_get(webs_t wp, int argc, char_t ** argv)
176{
177
178#ifndef FASTWEB
179        if (argc < 1) {
180                websError(wp, 400, "Insufficient args\n");
181                return;
182        }
183#endif
184
185#if COUNTRY == JAPAN
186        websWrite(wp, "%s", nvram_safe_get(argv[0]));
187#else
188
189        tf_webWriteESCNV(wp, argv[0]);  // test: buffered version of above
190
191        return;
192#endif
193
194        return;
195}
196
197void ej_nvram_real_get(webs_t wp, int argc, char_t ** argv)
198{
199
200#ifndef FASTWEB
201        if (argc < 1) {
202                websError(wp, 400, "Insufficient args\n");
203                return;
204        }
205#endif
206        websWrite(wp, "%s", nvram_safe_get(argv[0]));
207
208        return;
209}
210
211/*
212 * Example:
213 * lan_ipaddr = 192.168.1.1, gozila_action = 0
214 * <% nvram_selget("lan_ipaddr"); %> produces "192.168.1.1"
215 * lan_ipaddr = 192.168.1.1, gozila_action = 1, websGetVar(wp, "lan_proto", NULL) = 192.168.1.2;
216 * <% nvram_selget("lan_ipaddr"); %> produces "192.168.1.2"
217 */
218void ej_nvram_selget(webs_t wp, int argc, char_t ** argv)
219{
220        char *name;
221
222#ifndef FASTWEB
223        if (argc < 1) {
224                websError(wp, 400, "Insufficient args\n");
225                return;
226        }
227#endif
228        name = argv[0];
229        if (nvram_match("gozila_action", "1")) {
230                char *buf = websGetVar(wp, name, NULL);
231
232                if (buf) {
233                        websWrite(wp, "%s", buf);
234                        return;
235                }
236        }
237        tf_webWriteESCNV(wp, name);     // test: buffered version of above
238
239        return;
240}
241
242/*
243 * Example:
244 * wan_mac = 00:11:22:33:44:55
245 * <% nvram_mac_get("wan_mac"); %> produces "00-11-22-33-44-55"
246 */
247void ej_nvram_mac_get(webs_t wp, int argc, char_t ** argv)
248{
249        char *c;
250        char *mac;
251        int i;
252
253#ifndef FASTWEB
254        if (argc < 1) {
255                websError(wp, 400, "Insufficient args\n");
256                return;
257        }
258#endif
259        c = nvram_safe_get(argv[0]);
260
261        if (c) {
262                mac = strdup(c);
263                for (i = 0; *(mac + i); i++) {
264                        if (*(mac + i) == ':')
265                                *(mac + i) = '-';
266                }
267                websWrite(wp, "%s", mac);
268                free(mac);      // leak, thx tofu
269        }
270
271        return;
272
273}
274
275/*
276 * Example:
277 * wan_proto = dhcp; gozilla = 0;
278 * <% nvram_gozila_get("wan_proto"); %> produces "dhcp"
279 *
280 * wan_proto = dhcp; gozilla = 1; websGetVar(wp, "wan_proto", NULL) = static;
281 * <% nvram_gozila_get("wan_proto"); %> produces "static"
282 */
283void ej_nvram_gozila_get(webs_t wp, int argc, char_t ** argv)
284{
285        char *type;
286
287#ifndef FASTWEB
288        if (argc < 1) {
289                websError(wp, 400, "Insufficient args\n");
290                return;
291        }
292#endif
293        type = GOZILA_GET(wp, argv[0]);
294        if (type == NULL)
295                type = nvram_safe_get(argv[0]);
296
297        websWrite(wp, "%s", type);
298}
299
300void ej_webs_get(webs_t wp, int argc, char_t ** argv)
301{
302        char *value;
303
304#ifndef FASTWEB
305        if (argc < 1) {
306                websError(wp, 400, "Insufficient args\n");
307                return;
308        }
309#endif
310
311        value = websGetVar(wp, argv[0], NULL);
312
313        if (value)
314                websWrite(wp, "%s", value);
315
316        return;
317}
318
319/*
320 * Example:
321 * lan_ipaddr = 192.168.1.1
322 * <% get_single_ip("lan_ipaddr","1"); %> produces "168"
323 */
324void ej_get_single_ip(webs_t wp, int argc, char_t ** argv)
325{
326        char *c;
327
328#ifndef FASTWEB
329        if (argc < 1) {
330                websError(wp, 400, "Insufficient args\n");
331                return;
332        }
333#endif
334
335        c = nvram_safe_get(argv[0]);
336        if (c) {
337                if (!strcmp(c, PPP_PSEUDO_IP) || !strcmp(c, PPP_PSEUDO_GW))
338                        c = "0.0.0.0";
339                else if (!strcmp(c, PPP_PSEUDO_NM))
340                        c = "255.255.255.0";
341
342                websWrite(wp, "%d", get_single_ip(c, atoi(argv[1])));
343        } else
344                websWrite(wp, "0");
345
346        return;
347}
348
349void ej_get_single_nm(webs_t wp, int argc, char_t ** argv)
350{
351        char *c;
352
353#ifndef FASTWEB
354        if (argc < 1) {
355                websError(wp, 400, "Insufficient args\n");
356                return;
357        }
358#endif
359
360        c = nvram_safe_get(argv[0]);
361        if (c) {
362                websWrite(wp, "%d", get_single_ip(c, atoi(argv[1])));
363        } else
364                websWrite(wp, "0");
365
366        return;
367}
368
369/*
370 * Example: wan_mac = 00:11:22:33:44:55 get_single_mac("wan_mac", 1);
371 * produces "11"
372 */
373int get_single_mac(char *macaddr, int which)
374{
375        int mac[6] = { 0, 0, 0, 0, 0, 0 };
376        int ret;
377
378        ret =
379            sscanf(macaddr, "%2X:%2X:%2X:%2X:%2X:%2X", &mac[0], &mac[1],
380                   &mac[2], &mac[3], &mac[4], &mac[5]);
381        return mac[which];
382}
383
384/*
385 * Example:
386 * wan_mac = 00:11:22:33:44:55
387 * <% get_single_mac("wan_mac","1"); %> produces "11"
388 */
389void ej_get_single_mac(webs_t wp, int argc, char_t ** argv)
390{
391        char *c;
392        int mac;
393
394#ifndef FASTWEB
395        if (argc < 2) {
396                websError(wp, 400, "Insufficient args\n");
397                return;
398        }
399#endif
400
401        c = nvram_safe_get(argv[0]);
402        if (c) {
403                mac = get_single_mac(c, atoi(argv[1]));
404                websWrite(wp, "%02X", mac);
405        } else
406                websWrite(wp, "00");
407
408        return;
409}
410
411/*
412 * Example:
413 * wan_proto = dhcp; gozilla = 0;
414 * <% nvram_selmatch("wan_proto", "dhcp", "selected"); %> produces "selected"
415 *
416 * wan_proto = dhcp; gozilla = 1; websGetVar(wp, "wan_proto", NULL) = static;
417 * <% nvram_selmatch("wan_proto", "static", "selected"); %> produces "selected"
418 */
419
420int nvram_selmatch(webs_t wp, char *name, char *match)
421{
422        char *type = GOZILA_GET(wp, name);
423
424        if (!type) {
425                if (nvram_match(name, match)) {
426                        return 1;
427                }
428        } else {
429                if (!strcmp(type, match)) {
430                        return 1;
431                }
432        }
433        return 0;
434}
435
436void ej_nvram_selmatch(webs_t wp, int argc, char_t ** argv)
437{
438
439#ifndef FASTWEB
440        if (argv < 3) {
441                websError(wp, 400, "Insufficient args\n");
442                return;
443        }
444#endif
445        if (nvram_selmatch(wp, argv[0], argv[1])) {
446                websWrite(wp, argv[2]);
447        }
448        return;
449}
450
451void ej_nvram_else_selmatch(webs_t wp, int argc, char_t ** argv)
452{
453        char *type;
454
455#ifndef FASTWEB
456        if (argc < 4) {
457                websError(wp, 400, "Insufficient args\n");
458                return;
459        }
460#endif
461
462        type = GOZILA_GET(wp, argv[0]);
463
464        if (!type) {
465                if (nvram_match(argv[0], argv[1])) {
466                        websWrite(wp, argv[2]);
467                } else
468                        websWrite(wp, argv[3]);
469        } else {
470                if (!strcmp(type, argv[1])) {
471                        websWrite(wp, argv[2]);
472                } else
473                        websWrite(wp, argv[3]);
474        }
475
476        return;
477}
478
479/*
480 * Example:
481 * wan_proto=dhcp
482 * <% nvram_else_match("wan_proto", "dhcp", "0","1"); %> produces "0"
483 * <% nvram_else_match("wan_proto", "static", "0","1"); %> produces "1"
484 */
485void ej_nvram_else_match(webs_t wp, int argc, char_t ** argv)
486{
487
488#ifndef FASTWEB
489        if (argc < 4) {
490                websError(wp, 400, "Insufficient args\n");
491                return;
492        }
493#endif
494
495        if (nvram_match(argv[0], argv[1]))
496                websWrite(wp, argv[2]);
497        else
498                websWrite(wp, argv[3]);
499
500        return;
501}
502
503void ej_startswith(webs_t wp, int argc, char_t ** argv)
504{
505
506#ifndef FASTWEB
507        if (argc < 3) {
508                websError(wp, 400, "Insufficient args\n");
509                return;
510        }
511#endif
512        if (startswith(nvram_safe_get(argv[0]), argv[1]))
513                websWrite(wp, argv[2]);
514
515        return;
516}
517
518static char *s_conditions[] = {
519#ifdef HAVE_MICRO
520        "MICRO",
521#endif
522#ifdef HAVE_EXTHELP
523        "EXTHELP",
524#endif
525#ifdef HAVE_MULTICAST
526        "MULTICAST",
527#endif
528#ifdef HAVE_WIVIZ
529        "WIVIZ",
530#endif
531#ifdef HAVE_RSTATS
532        "RSTATS",
533#endif
534#ifdef HAVE_ACK
535        "ACK",
536#endif
537#ifdef HAVE_SSHD
538        "SSHD",
539#endif
540#ifdef HAVE_PPTPD
541        "PPTPD",
542#endif
543#ifdef HAVE_QUAGGA
544        "QUAGGA",
545#endif
546#ifdef HAVE_SAMBA
547        "SAMBA",
548#endif
549#ifdef HAVE_SAMBA3
550        "SAMBA3",
551#endif
552#ifdef HAVE_JFFS2
553        "JFFS2",
554#endif
555#ifdef HAVE_GPSI
556        "GPSI",
557#endif
558#ifdef HAVE_MMC
559        "MMC",
560#endif
561#ifdef HAVE_SPUTNIK_APD
562        "SPUTNIK_APD",
563#endif
564#ifdef HAVE_RFLOW
565        "RFLOW",
566#endif
567#ifdef HAVE_USB
568        "USB",
569#endif
570#ifdef HAVE_RADIUSPLUGIN
571        "RADIUSPLUGIN",
572#endif
573#ifdef HAVE_PPPOESERVER
574        "PPPOESERVER",
575#endif
576#ifdef HAVE_MILKFISH
577        "MILKFISH",
578#endif
579#ifdef HAVE_LANGUAGE
580        "LANGUAGE",
581#endif
582#ifdef HAVE_BUFFALO
583        "HAVE_BUFFALO",
584#endif
585#ifdef HAVE_WPS
586        "HAVE_WPS",
587#endif
588#ifdef HAVE_ATH9K
589        "HAVE_ATH9K",
590#endif
591#ifdef HAVE_USBIP
592        "USBIP",
593#endif
594        NULL
595};
596
597void ej_ifdef(webs_t wp, int argc, char_t ** argv)
598{
599        char *name, *output;
600
601#ifndef FASTWEB
602        if (argc < 2) {
603                websError(wp, 400, "Insufficient args\n");
604                return;
605        }
606#endif
607        name = argv[0];
608        output = argv[1];
609        int cnt = 0;
610        while (s_conditions[cnt]) {
611                if (!strcmp(name, s_conditions[cnt++])) {
612                        websWrite(wp, output);
613                        return;
614                }
615        }
616
617        if (!strcmp(name, "MINI"))      // to include mini + mini-special
618        {
619                if (startswith(nvram_safe_get("dist_type"), "mini")) {
620                        websWrite(wp, output);
621                        return;
622                }
623        }
624        if (!strcmp(name, "VPN"))       // to include vpn + vpn-special
625        {
626                if (startswith(nvram_safe_get("dist_type"), "vpn")) {
627                        websWrite(wp, output);
628                        return;
629                }
630        }
631        if (!strcmp(name, "WET")) {
632                if (getWET())
633                        websWrite(wp, output);
634                return;
635        }
636        if (!strcmp(name, "STA")) {
637                if (getSTA())
638                        websWrite(wp, output);
639                return;
640        }
641
642        return;
643}
644
645void ej_ifndef(webs_t wp, int argc, char_t ** argv)
646{
647        char *name, *output;
648
649#ifndef FASTWEB
650        if (argc < 2) {
651                websError(wp, 400, "Insufficient args\n");
652                return;
653        }
654#endif
655        name = argv[0];
656        output = argv[1];
657
658        int cnt = 0;
659        while (s_conditions[cnt]) {
660                if (!strcmp(name, s_conditions[cnt++])) {
661                        return;
662                }
663        }
664
665        // HAVE_AFTERBURNER
666        if (!strncmp(name, "AFTERBURNER", 11)) {
667#if defined(HAVE_MADWIFI) || defined(HAVE_RT2880)
668                return;
669#else
670                int afterburner = 0;
671                char cap[WLC_IOCTL_SMLEN];
672                char caps[WLC_IOCTL_SMLEN];
673                char *ifname;
674                name = name + 11;
675                if (!strncmp(name, "_wl0", 4))
676                        ifname = nvram_safe_get("wl0_ifname");
677                else            // "_wl1"
678                        ifname = nvram_safe_get("wl1_ifname");
679                char *next;
680
681                if (wl_iovar_get(ifname, "cap", (void *)caps, WLC_IOCTL_SMLEN)
682                    == 0) {
683                        foreach(cap, caps, next) {
684                                if (!strcmp(cap, "afterburner"))
685                                        afterburner = 1;
686                        }
687
688                        if (afterburner)
689                                return;
690                }
691#endif
692        }
693        // end HAVE_AFTERBURNER
694        // HAVE_HASWIFI
695        if (!strcmp(name, "HASWIFI")) {
696                if (haswifi())
697                        return;
698        }
699        // end HAVE_HASWIFI
700        if (!strcmp(name, "WET")) {
701                if (getWET())
702                        return;
703        }
704        if (!strcmp(name, "STA")) {
705                if (getSTA())
706                        return;
707        }
708
709        websWrite(wp, output);
710
711        return;
712}
713
714/*
715 * Example:
716 * wan_proto=dhcp
717 * <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
718 * <% nvram_match("wan_proto", "static", "selected"); %> does not produce
719 */
720void ej_nvram_match(webs_t wp, int argc, char_t ** argv)
721{
722
723#ifndef FASTWEB
724        if (argc < 3) {
725                websError(wp, 400, "Insufficient args\n");
726                return;
727        }
728#endif
729
730        if (nvram_match(argv[0], argv[1]))
731                websWrite(wp, argv[2]);
732
733        return;
734}
735
736/*
737 * Example:
738 * wan_proto=dhcp
739 * <% nvram_invmatch("wan_proto", "dhcp", "disabled"); %> does not produce
740 * <% nvram_invmatch("wan_proto", "static", "disabled"); %> produces "disabled"
741 */
742void ej_nvram_invmatch(webs_t wp, int argc, char_t ** argv)
743{
744        char *name, *invmatch, *output;
745
746#ifdef FASTWEB
747        ejArgs(argc, argv, "%s %s %s", &name, &invmatch, &output);
748#else
749        if (ejArgs(argc, argv, "%s %s %s", &name, &invmatch, &output) < 3) {
750                websError(wp, 400, "Insufficient args\n");
751                return;
752        }
753#endif
754
755        if (!nvram_match(name, invmatch))
756                websWrite(wp, output);
757
758        return;
759}
760
761/*
762 * static void ej_scroll (webs_t wp, int argc, char_t ** argv) { char *type;
763 * int y;
764 *
765 * #ifdef FASTWEB ejArgs (argc, argv, "%s %d", &type, &y); #else if (ejArgs
766 * (argc, argv, "%s %d", &type, &y) < 2) { websError (wp, 400, "Insufficient
767 * args\n"); return; } #endif if (gozila_action) websWrite (wp, "%d", y);
768 * else websWrite (wp, "0");
769 *
770 * return; }
771 */
772/*
773 * Example:
774 * filter_mac=00:12:34:56:78:00 00:87:65:43:21:00
775 * <% nvram_list("filter_mac", 1); %> produces "00:87:65:43:21:00"
776 * <% nvram_list("filter_mac", 100); %> produces ""
777 */
778void ej_nvram_list(webs_t wp, int argc, char_t ** argv)
779{
780        int which;
781        char word[256], *next;
782
783#ifndef FASTWEB
784        if (argc < 2) {
785                websError(wp, 400, "Insufficient args\n");
786                return;
787        }
788#endif
789        which = atoi(argv[1]);
790        char *list = nvram_safe_get(argv[0]);
791
792        foreach(word, list, next) {
793                if (which-- == 0)
794                        websWrite(wp, word);
795        }
796
797        return;
798}
799
800/*
801 * Example: wan_dns = 1.2.3.4 10.20.30.40 15.25.35.45 get_dns_ip("wan_dns",
802 * 1, 2); produces "20"
803 */
804int get_dns_ip(char *name, int which, int count)
805{
806        static char word[256];
807        char *next;
808        int ip;
809        char *list = nvram_safe_get(name);
810
811        foreach(word, list, next) {
812                if (which-- == 0) {
813                        ip = get_single_ip(word, count);
814                        return ip;
815                }
816        }
817        return 0;
818}
819
820/*
821 * Example: wan_dns = 168.95.1.1 210.66.161.125 168.95.192.1 <%
822 * get_dns_ip("wan_dns", "1", "2"); %> produces "161" <%
823 * get_dns_ip("wan_dns", "2", "3"); %> produces "1"
824 */
825void ej_get_dns_ip(webs_t wp, int argc, char_t ** argv)
826{
827        int which;
828        char word[256], *next;
829
830#ifndef FASTWEB
831        if (argc < 3) {
832                websError(wp, 400, "Insufficient args\n");
833                return;
834        }
835#endif
836        which = atoi(argv[1]);
837        char *list = nvram_safe_get(argv[0]);
838
839        foreach(word, list, next) {
840                if (which-- == 0) {
841                        websWrite(wp, "%d", get_single_ip(word, atoi(argv[2])));
842                        return;
843                }
844        }
845
846        websWrite(wp, "0");     // not find
847}
848
849void ej_get_http_prefix(webs_t wp, int argc, char_t ** argv)
850{
851        char http[10];
852        char ipaddr[20];
853        char port[10];
854
855        char *http_enable = websGetVar(wp, "http_enable", NULL);
856
857#ifdef HAVE_HTTPS
858        char *https_enable = websGetVar(wp, "https_enable", NULL);
859
860        if (do_ssl && http_enable == NULL && https_enable == NULL) {
861                strcpy(http, "https");
862        } else if (do_ssl && http_enable && https_enable) {
863                if (atoi(https_enable) && atoi(http_enable))
864                        strcpy(http, "https");
865                else if (atoi(https_enable) && !atoi(http_enable))
866                        strcpy(http, "https");
867                else            // !atoi(https_enable) && atoi(http_enable)
868                        strcpy(http, "http");
869        } else if (do_ssl && !http_enable && !https_enable) {
870                strcpy(http, "http");
871        } else if (!do_ssl && http_enable && https_enable) {
872                if (atoi(https_enable) && atoi(http_enable))
873                        strcpy(http, "http");
874                else if (atoi(https_enable) && !atoi(http_enable))
875                        strcpy(http, "https");
876                else            // !atoi(https_enable) && atoi(http_enable)
877                        strcpy(http, "http");
878        } else
879#endif
880                strcpy(http, "http");
881
882        if (nvram_match("browser_method", "USE_LAN")) { // Use LAN to browser
883                if (nvram_match("restore_defaults", "1")
884                    || nvram_match("sv_restore_defaults", "1")) {
885
886                        strcpy(http, "http");
887                } else
888                        strcpy(ipaddr, nvram_safe_get("lan_ipaddr"));
889                strcpy(port, "");
890        } else {
891
892                if (nvram_match("wan_proto", "pptp"))
893                        strcpy(ipaddr, nvram_safe_get("pptp_get_ip"));
894                else if (nvram_match("wan_proto", "l2tp"))
895                        strcpy(ipaddr, nvram_safe_get("l2tp_get_ip"));
896                else
897                        strcpy(ipaddr, nvram_safe_get("wan_ipaddr"));
898
899                sprintf(port, ":%s", nvram_safe_get("http_wanport"));
900        }
901
902        websWrite(wp, "%s://%s%s/", http, ipaddr, port);
903
904        return;
905}
906
907void ej_get_mtu(webs_t wp, int argc, char_t ** argv)
908{
909        struct mtu_lists *mtu_list;
910        char *type;
911        char *proto = GOZILA_GET(wp, "wan_proto");
912
913        type = argv[1];
914#ifndef FASTWEB
915        if (argc < 1) {
916                websError(wp, 400, "Insufficient args\n");
917                return;
918        }
919#endif
920        if (proto == NULL)
921                proto = nvram_safe_get("wan_proto");
922        mtu_list = get_mtu(proto);
923
924        if (!strcmp(type, "min"))
925                websWrite(wp, "%s", mtu_list->min);
926        else if (!strcmp(type, "max"))
927                websWrite(wp, "%s", mtu_list->max);
928
929        return;
930}
931
932void ej_show_forward(webs_t wp, int argc, char_t ** argv)
933// ej_show_forward(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
934// char_t *url, char_t *path, char_t *query)
935{
936        int i;
937        char *count;
938        int c = 0;
939
940        count = nvram_safe_get("forward_entries");
941        if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
942                // return -1; botho 07/03/06 add "- None -" if empty
943                websWrite(wp, "<tr>\n");
944                websWrite(wp,
945                          "<td colspan=\"6\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
946                websWrite(wp, "</tr>\n");
947        }
948        for (i = 0; i < c; i++) {
949                websWrite(wp, "<tr><td>\n");
950                websWrite(wp,
951                          "<input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
952                          i);
953                port_forward_table(wp, "name", i);
954                websWrite(wp, "\" /></td>\n");
955                websWrite(wp, "<td>\n");
956                websWrite(wp,
957                          "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
958                          i);
959                port_forward_table(wp, "from", i);
960                websWrite(wp, "\" /></td>\n");
961                websWrite(wp, "<td>\n");
962                websWrite(wp,
963                          "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
964                          i);
965                port_forward_table(wp, "to", i);
966                websWrite(wp, "\"/></td>\n");
967                websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
968                websWrite(wp, "<option value=\"tcp\" ");
969                port_forward_table(wp, "sel_tcp", i);
970                websWrite(wp, ">TCP</option>\n");
971                websWrite(wp, "<option value=\"udp\" ");
972                port_forward_table(wp, "sel_udp", i);
973                websWrite(wp, ">UDP</option>\n");
974                websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
975                                document.write(\"<option value=\\\"both\\\" ");
976                port_forward_table(wp, "sel_both", i);
977                websWrite(wp, " >\" + share.both + \"</option>\");\n\
978        \n//]]>\n</script>\n");
979                websWrite(wp, "</select></td>\n");
980                websWrite(wp, "<td>\n");
981                websWrite(wp,
982                          "<input class=\"num\" maxlength=\"15\" size=\"15\" name=\"ip%d\" value=\"",
983                          i);
984                port_forward_table(wp, "ip", i);
985                websWrite(wp, "\" /></td>\n");
986                websWrite(wp, "<td>\n");
987                websWrite(wp,
988                          "<input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
989                          i);
990                port_forward_table(wp, "enable", i);
991                websWrite(wp, " /></td>\n");
992                websWrite(wp, "</tr>\n");
993        }
994        return;
995}
996
997void ej_show_forward_spec(webs_t wp, int argc, char_t ** argv)
998// ej_show_forward(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg,
999// char_t *url, char_t *path, char_t *query)
1000{
1001        int i;
1002        char *count;
1003        int c = 0;
1004
1005        count = nvram_safe_get("forwardspec_entries");
1006        if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
1007                // return -1; botho 07/03/06 add "- None -" if empty
1008                // websWrite (wp, "<tr></tr><tr></tr>\n");
1009                websWrite(wp, "<tr>\n");
1010                websWrite(wp,
1011                          "<td colspan=\"7\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
1012                websWrite(wp, "</tr>\n");
1013        }
1014        for (i = 0; i < c; i++) {
1015                //name
1016                websWrite(wp, "<tr><td>\n");
1017                websWrite(wp,
1018                          "<input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
1019                          i);
1020                port_forward_spec(wp, "name", i);
1021                websWrite(wp, "\" /></td>\n");
1022
1023                //proto
1024                websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
1025                websWrite(wp, "<option value=\"tcp\" ");
1026                port_forward_spec(wp, "sel_tcp", i);
1027                websWrite(wp, ">TCP</option>\n");
1028                websWrite(wp, "<option value=\"udp\" ");
1029                port_forward_spec(wp, "sel_udp", i);
1030                websWrite(wp, ">UDP</option>\n");
1031                websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
1032                                document.write(\"<option value=\\\"both\\\" ");
1033                port_forward_spec(wp, "sel_both", i);
1034                websWrite(wp, " >\" + share.both + \"</option>\");\n\
1035                \n//]]>\n</script>\n");
1036                websWrite(wp, "</select></td>\n");
1037
1038                //src net
1039                websWrite(wp, "<td>\n");
1040                websWrite(wp,
1041                          "<input class=\"num\" maxlength=\"15\" size=\"15\" name=\"src%d\" value=\"",
1042                          i);
1043                port_forward_spec(wp, "src", i);
1044                websWrite(wp, "\" /></td>\n");
1045
1046                //from
1047                websWrite(wp, "<td>\n");
1048                websWrite(wp,
1049                          "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1050                          i);
1051                port_forward_spec(wp, "from", i);
1052                websWrite(wp, "\" /></td>\n");
1053
1054                //dest ip
1055                websWrite(wp, "<td>\n");
1056                websWrite(wp,
1057                          "<input class=\"num\" maxlength=\"15\" size=\"15\" name=\"ip%d\" value=\"",
1058                          i);
1059                port_forward_spec(wp, "ip", i);
1060                websWrite(wp, "\" /></td>\n");
1061
1062                //port to
1063                websWrite(wp, "<td>\n");
1064                websWrite(wp,
1065                          "<input class=\"num\" maxlength=\"5\" size=\"5\" name=\"to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1066                          i);
1067                port_forward_spec(wp, "to", i);
1068                websWrite(wp, "\" /></td>\n");
1069
1070                //checkbox
1071                websWrite(wp, "<td>\n");
1072                websWrite(wp,
1073                          "<input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
1074                          i);
1075                port_forward_spec(wp, "enable", i);
1076                websWrite(wp, " /></td>\n");
1077
1078                //end of table
1079                websWrite(wp, "</tr>\n");
1080        }
1081        return;
1082}
1083
1084void ej_show_triggering(webs_t wp, int argc, char_t ** argv)
1085{
1086        int i;
1087        char *count;
1088        int c = 0;
1089
1090        count = nvram_safe_get("trigger_entries");
1091        if (count == NULL || strlen(count) == 0 || (c = atoi(count)) <= 0) {
1092                websWrite(wp, "<tr>\n");
1093                websWrite(wp,
1094                          "<td colspan=\"6\" align=\"center\" valign=\"middle\">- <script type=\"text/javascript\">Capture(share.none)</script> -</td>\n");
1095                websWrite(wp, "</tr>\n");
1096        }
1097        for (i = 0; i < c; i++) {
1098                websWrite(wp, "<tr>\n");
1099                websWrite(wp,
1100                          "<td><input maxlength=\"12\" size=\"12\" name=\"name%d\" onblur=\"valid_name(this,'Name')\" value=\"",
1101                          i);
1102                port_trigger_table(wp, "name", i);
1103                websWrite(wp, "\" /></td>\n");
1104                websWrite(wp,
1105                          "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"i_from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1106                          i);
1107                port_trigger_table(wp, "i_from", i);
1108                websWrite(wp, "\" /></td>\n");
1109                websWrite(wp,
1110                          "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"i_to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1111                          i);
1112                port_trigger_table(wp, "i_to", i);
1113                websWrite(wp, "\" /></td>\n");
1114                websWrite(wp, "<td><select size=\"1\" name=\"pro%d\">\n", i);
1115                websWrite(wp, "<option value=\"tcp\" ");
1116                port_trigger_table(wp, "sel_tcp", i);
1117                websWrite(wp, ">TCP</option>\n");
1118                websWrite(wp, "<option value=\"udp\" ");
1119                port_trigger_table(wp, "sel_udp", i);
1120                websWrite(wp, ">UDP</option>\n");
1121                websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n\
1122                                document.write(\"<option value=\\\"both\\\" ");
1123                port_trigger_table(wp, "sel_both", i);
1124                websWrite(wp, " >\" + share.both + \"</option>\");\n\
1125                \n//]]>\n</script>\n");
1126                websWrite(wp, "</select></td>\n");
1127                websWrite(wp,
1128                          "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"o_from%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1129                          i);
1130                port_trigger_table(wp, "o_from", i);
1131                websWrite(wp, "\" /></td>\n");
1132                websWrite(wp,
1133                          "<td><input class=\"num\" maxlength=\"5\" size=\"5\" name=\"o_to%d\" onblur=\"valid_range(this,1,65535,'Port')\" value=\"",
1134                          i);
1135                port_trigger_table(wp, "o_to", i);
1136                websWrite(wp, "\" /></td>\n");
1137                websWrite(wp,
1138                          "<td><input type=\"checkbox\" value=\"on\" name=\"enable%d\" ",
1139                          i);
1140                port_trigger_table(wp, "enable", i);
1141                websWrite(wp, " /></td>\n");
1142                websWrite(wp, "</tr>\n");
1143        }
1144        return;
1145}
1146
1147// SEG DD-WRT addition
1148void ej_show_styles(webs_t wp, int argc, char_t ** argv)
1149{
1150        // <option value="blue" <% nvram_selected("router_style", "blue");
1151        // %>>Blue</option>
1152        DIR *directory;
1153        char buf[256];
1154
1155        directory = opendir("/www/style");
1156        struct dirent *entry;
1157
1158        while ((entry = readdir(directory)) != NULL) {
1159                sprintf(buf, "style/%s/style.css", entry->d_name);
1160                FILE *web = getWebsFile(buf);
1161
1162                if (web == NULL) {
1163                        sprintf(buf, "/www/style/%s/style.css", entry->d_name);
1164                        if (!f_exists(buf))
1165                                continue;
1166                }
1167                fclose(web);
1168
1169                websWrite(wp, "<option value=\"%s\" %s>%s</option>\n",
1170                          entry->d_name, nvram_match("router_style",
1171                                                     entry->d_name) ?
1172                          "selected=\"selected\"" : "", entry->d_name);
1173        }
1174        closedir(directory);
1175        return;
1176}
1177
1178#ifdef HAVE_LANGUAGE
1179// extern websRomPageIndexType websRomPageIndex[];
1180void ej_show_languages(webs_t wp, int argc, char_t ** argv)
1181{
1182        char buf[256];
1183
1184        websWrite(wp, "<script type=\"text/javascript\">\n//<![CDATA[\n");
1185        int i = 0;
1186
1187        while (websRomPageIndex[i].path != NULL) {
1188                cprintf("checking %s\n", websRomPageIndex[i].path);
1189                if (!strncmp
1190                    (websRomPageIndex[i].path, "lang_pack/",
1191                     strlen("lang_pack/"))) {
1192                        cprintf("found language\n");
1193                        if (strlen(websRomPageIndex[i].path) < 14)
1194                                continue;
1195                        strcpy(buf, websRomPageIndex[i].path);
1196                        char *mybuf = &buf[strlen("lang_pack/")];
1197
1198                        mybuf[strlen(mybuf) - 3] = 0;   // strip .js
1199                        websWrite(wp,
1200                                  "document.write(\"<option value=\\\"%s\\\" %s >\" + management.lang_%s + \"</option>\");\n",
1201                                  mybuf, nvram_match("language",
1202                                                     mybuf) ?
1203                                  "selected=\\\"selected\\\"" : "", mybuf);
1204                }
1205                i++;
1206        }
1207        websWrite(wp, "//]]>\n</script>\n");
1208        return;
1209}
1210#endif
1211
1212static char *directories[] = {
1213        "/etc/config",
1214        "/jffs/etc/config",
1215        "/mmc/etc/config"
1216};
1217
1218void ej_show_modules(webs_t wp, int argc, char_t ** argv)
1219{
1220        char buf[256];
1221        struct dirent *entry;
1222        DIR *directory;
1223        int resultcount = 0;
1224        char *result[256];
1225        result[0] = NULL;
1226
1227        // display modules
1228        int idx;
1229
1230        for (idx = 0; idx < 3; idx++) {
1231                directory = opendir(directories[idx]);
1232                if (directory == NULL)
1233                        continue;
1234                // list all files in this directory
1235                while ((entry = readdir(directory)) != NULL) {
1236                        if (argc > 0) {
1237                                if (endswith(entry->d_name, argv[0])) {
1238#ifdef HAVE_ERC
1239                                        if (strcmp(entry->d_name, "base.webconfig") && !wp->userid)     //show only base.webconfig for this user and nothing else
1240                                        {
1241                                                continue;
1242                                        }
1243#endif
1244                                        sprintf(buf, "%s/%s", directories[idx],
1245                                                entry->d_name);
1246                                        result[resultcount] =
1247                                            malloc(strlen(entry->d_name) + 1);
1248                                        strcpy(result[resultcount],
1249                                               entry->d_name);
1250                                        resultcount++;
1251                                        result[resultcount] = NULL;
1252                                }
1253                        } else {
1254                                if (endswith(entry->d_name, ".webconfig")) {
1255                                        sprintf(buf, "%s/%s", directories[idx],
1256                                                entry->d_name);
1257                                        result[resultcount] =
1258                                            malloc(strlen(entry->d_name) + 1);
1259                                        strcpy(result[resultcount],
1260                                               entry->d_name);
1261                                        resultcount++;
1262                                        result[resultcount] = NULL;
1263                                }
1264                        }
1265
1266                }
1267                /* now sort entries to solve EXT2 unsorted problem */
1268                int i, a;
1269                for (a = 0; a < resultcount; a++) {
1270                        int change = 0;
1271                        for (i = 0; i < resultcount - 1; i++) {
1272                                int step = 0;
1273                              again:;
1274                                if (!result[i][step] || !result[i + 1][step])
1275                                        continue;
1276                                if (result[i][step] == result[i + 1][step]) {
1277                                        step++;
1278                                        goto again;
1279                                }
1280                                if (result[i][step] > result[i + 1][step]) {
1281                                        char *temp = result[i + 1];
1282                                        result[i + 1] = result[i];
1283                                        result[i] = temp;
1284                                        step = 0;
1285                                        change++;
1286                                }
1287                        }
1288                        if (!change)
1289                                break;  //no more sortable entries found, so just break up here
1290                }
1291                for (i = 0; i < resultcount; i++) {
1292                        sprintf(buf, "%s/%s", directories[idx], result[i]);
1293                        do_ej(NULL, buf, wp, NULL);
1294                }
1295                for (i = 0; i < resultcount; i++) {
1296                        free(result[i]);
1297                }
1298                resultcount = 0;
1299                result[0] = NULL;
1300                closedir(directory);
1301        }
1302        return;
1303}
1304
1305void ej_get_sysmodel(webs_t wp, int argc, char_t ** argv)
1306{
1307#ifdef HAVE_XIOCOM
1308        websWrite(wp, "XWR");
1309#else
1310        websWrite(wp, "%s", nvram_safe_get("DD_BOARD"));
1311#endif
1312}
1313
1314void ej_get_totaltraff(webs_t wp, int argc, char_t ** argv)
1315{
1316        char *type;
1317        static char wanface[32];
1318        char line[256];
1319        unsigned long rcvd, sent, megcounti, megcounto;
1320        FILE *in;
1321        int ifl;
1322
1323#ifndef FASTWEB
1324        if (argc < 1) {
1325                websError(wp, 400, "Insufficient args\n");
1326                return;
1327        }
1328#endif
1329        type = argv[0];
1330
1331        if (!nvram_match("ttraff_enable", "1"))
1332                return;
1333
1334        if (nvram_match("ttraff_iface", "") || !nvram_get("ttraff_iface"))
1335                strncpy(wanface, get_wan_face(), sizeof(wanface));
1336        else
1337                strncpy(wanface, nvram_safe_get("ttraff_iface"),
1338                        sizeof(wanface));
1339        strcat(wanface, ":");
1340
1341        in = fopen("/proc/net/dev", "rb");
1342        if (in == NULL)
1343                return;
1344
1345        /* eat first two lines */
1346        fgets(line, sizeof(line), in);
1347        fgets(line, sizeof(line), in);
1348        while (fgets(line, sizeof(line), in) != NULL) {
1349                ifl = 0;
1350
1351                if (strstr(line, wanface)) {
1352                        while (line[ifl] != ':')
1353                                ifl++;
1354                        line[ifl] = 0;
1355
1356                        sscanf(line + ifl + 1,
1357                               "%lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld %lu %*ld %*ld %*ld %*ld %*ld %*ld %*ld",
1358                               &rcvd, &sent);
1359                }
1360        }
1361
1362        fclose(in);
1363
1364        rcvd >>= 20;            // output in MBytes
1365        sent >>= 20;
1366
1367        if ((in = fopen("/tmp/.megc", "r")) != NULL) {
1368                fgets(line, sizeof(line), in);
1369                sscanf(line, "%lu:%lu", &megcounti, &megcounto);
1370                rcvd += megcounti;
1371                sent += megcounto;
1372                fclose(in);
1373        }
1374
1375        if (!strcmp(type, "in")) {
1376                websWrite(wp, "%lu", rcvd);     // output in MBytes
1377        } else if (!strcmp(type, "out")) {
1378                websWrite(wp, "%lu", sent);
1379        }
1380        return;
1381}
1382
1383void show_bwif(webs_t wp, char *ifname, char *name)
1384{
1385        websWrite(wp, "<h2>%s - %s</h2>\n", live_translate("status_band.h2"),
1386                  name);
1387        websWrite(wp, "<fieldset>\n");
1388        websWrite(wp,
1389                  "<iframe src=\"/graph_if.svg?%s\" width=\"555\" height=\"275\" frameborder=\"0\" type=\"image/svg+xml\">\n",
1390                  ifname);
1391        websWrite(wp, "</iframe>\n");
1392        websWrite(wp, "</fieldset>\n");
1393        websWrite(wp, "<br />\n");
1394}
1395
1396void ej_show_bandwidth(webs_t wp, int argc, char_t ** argv)
1397{
1398        char name[32];
1399
1400        show_bwif(wp, nvram_safe_get("lan_ifname"), "LAN");
1401
1402        if (!nvram_match("wan_proto", "disabled")) {
1403#ifdef HAVE_MADWIFI
1404                if (getSTA()) {
1405                        sprintf(name, "%s WAN",
1406                                live_translate("share.wireless"));
1407                        show_bwif(wp, get_wan_face(), name);
1408                } else
1409                        show_bwif(wp, get_wan_face(), "WAN");
1410#else
1411                if (!getSTA())
1412                        show_bwif(wp, get_wan_face(), "WAN");
1413#endif
1414
1415                if (nvram_match("dtag_vlan8", "1")) {
1416                        if (getRouterBrand() == ROUTER_WRT600N
1417                            || getRouterBrand() == ROUTER_WRT610N)
1418                                show_bwif(wp, "eth2.0008", "IPTV");
1419                        else
1420                                show_bwif(wp, "eth0.0008", "IPTV");
1421                }
1422
1423        }
1424#ifdef HAVE_MADWIFI
1425        char var[80];
1426        char *next;
1427        int c = getdevicecount();
1428        int i;
1429
1430        for (i = 0; i < c; i++) {
1431                char dev[32];
1432
1433                sprintf(dev, "ath%d", i);
1434
1435                sprintf(name, "%s (%s)", live_translate("share.wireless"), dev);
1436                show_bwif(wp, dev, name);
1437                char *vifs = nvram_nget("%s_vifs", dev);
1438
1439                if (vifs == NULL)
1440                        continue;
1441                foreach(var, vifs, next) {
1442                        sprintf(name, "%s (%s)",
1443                                live_translate("share.wireless"), var);
1444                        show_bwif(wp, var, name);
1445                }
1446                int s;
1447
1448                for (s = 1; s <= 10; s++) {
1449                        char *wdsdev;
1450
1451                        wdsdev = nvram_nget("%s_wds%d_if", dev, s);
1452                        if (strlen(wdsdev) == 0)
1453                                continue;
1454                        if (nvram_nmatch("0", "%s_wds%d_enable", dev, s))
1455                                continue;
1456                        sprintf(name, "%s (%s)",
1457                                live_translate("share.wireless"), wdsdev);
1458                        show_bwif(wp, wdsdev, name);
1459                }
1460
1461        }
1462
1463#else
1464        int cnt = get_wl_instances();
1465        int c;
1466
1467        for (c = 0; c < cnt; c++) {
1468                sprintf(name, "%s (wl%d)", live_translate("share.wireless"), c);
1469                show_bwif(wp, get_wl_instance_name(c), name);
1470        }
1471#endif
1472#ifdef HAVE_WAVESAT
1473
1474        sprintf(name, "%s", live_translate("wl_wimax.titl"));
1475        show_bwif(wp, "ofdm", name);
1476#endif
1477}
1478
1479void ej_do_menu(webs_t wp, int argc, char_t ** argv)
1480{
1481        char *mainmenu, *submenu;
1482
1483#ifndef FASTWEB
1484        if (argc < 2) {
1485                websError(wp, 400, "Insufficient args\n");
1486                return;
1487        }
1488#endif
1489        mainmenu = argv[0];
1490        submenu = argv[1];
1491
1492        int vlan_supp = check_vlan_support();
1493
1494#ifdef HAVE_SPUTNIK_APD
1495        int sputnik = nvram_match("apd_enable", "1");
1496#else
1497        int sputnik = 0;
1498#endif
1499        int openvpn = nvram_match("openvpn_enable",
1500                                  "1") | nvram_match("openvpncl_enable",
1501                                                     "1");
1502        int auth = nvram_match("status_auth", "1");
1503        int registered = 1;
1504#ifdef HAVE_REGISTER
1505        if (!isregistered_real())
1506                registered = 0;
1507        int cpeonly = iscpe();
1508#else
1509        int cpeonly = 0;
1510#endif
1511
1512#ifdef HAVE_MADWIFI
1513#ifdef HAVE_NOWIFI
1514        int wifi = 0;
1515#else
1516        int wifi = haswifi();
1517#endif
1518#endif
1519#ifdef HAVE_MADWIFI
1520        int wimaxwifi = 0;
1521#endif
1522#ifdef HAVE_ERC
1523        static char menu_s[8][12][32] =
1524            { {"index.asp", "DDNS.asp", "", "", "", "", "", "", "", "", "", ""},
1525        {"Wireless_Basic.asp", "WL_WPATable.asp", "", "", "", "", "", "", "",
1526         "", "", ""},
1527        {"ForwardSpec.asp", "", "", "", "", "", "", "", "", "", "", ""},
1528        {"Filters.asp", "", "", "", "", "", "", "", "", "", "", ""},
1529        {"Management.asp", "", "", "", "", "", "", "", "", "", "", ""},
1530        {"", "", "", "", "", "", "", "", "", "", "", ""},
1531        {"", "", "", "", "", "", "", "", "", "", "", ""},
1532        {"", "", "", "", "", "", "", "", "", "", "", ""},
1533        {"", "", "", "", "", "", "", "", "", "", "", ""}
1534        };
1535        /*
1536         * real name is bmenu.menuname[i][j]
1537         */
1538        static char menuname_s[8][13][32] =
1539            { {"setup", "setupbasic", "setupddns", "", "", "", "", "", "", "",
1540               "", "", ""},
1541        {"wireless", "wirelessBasic", "wirelessSecurity", "", "", "", "", "",
1542         "", "", "", "", ""},
1543        {"applications", "applicationspforwarding", "", "", "", "", "", "", "",
1544         "", "", "", ""},
1545        {"accrestriction", "webaccess", "", "", "", "", "", "", "", "", "", "",
1546         ""},
1547        {"admin", "adminManagement", "", "", "", "", "", "", "", "", "", "",
1548         ""},
1549        {"", "", "", "", "", "", "", "", "", "", "", "", ""},
1550        {"", "", "", "", "", "", "", "", "", "", "", "", ""},
1551        {"", "", "", "", "", "", "", "", "", "", "", "", ""},
1552        {"", "", "", "", "", "", "", "", "", "", "", "", ""}
1553        };
1554
1555#endif
1556
1557        static char menu_t[8][12][32] =
1558            { {"index.asp", "DDNS.asp", "WanMAC.asp", "Routing.asp", "Vlan.asp",
1559               "Networking.asp", "eop-tunnel.asp", "", "", "", "", ""},
1560        {"Wireless_Basic.asp", "SuperChannel.asp", "WiMAX.asp",
1561         "Wireless_radauth.asp", "WL_WPATable.asp", "AOSS.asp",
1562         "Wireless_MAC.asp", "Wireless_Advanced.asp", "Wireless_WDS.asp", "",
1563         "", ""},
1564        {"Services.asp", "FreeRadius.asp", "PPPoE_Server.asp", "PPTP.asp",
1565         "USB.asp", "NAS.asp", "Hotspot.asp", "Nintendo.asp", "Milkfish.asp",
1566         "AnchorFree.asp", "", ""},
1567        {"Firewall.asp", "VPN.asp", "", "", "", "", "", "", "", "", "", ""},
1568        {"Filters.asp", "", "", "", "", "", "", "", "", "", "", ""},
1569        {"ForwardSpec.asp", "Forward.asp", "Triggering.asp", "UPnP.asp",
1570         "DMZ.asp", "QoS.asp", "P2P.asp", "", "", "", "", ""},
1571        {"Management.asp", "Alive.asp", "Diagnostics.asp", "Wol.asp",
1572         "Factory_Defaults.asp", "Upgrade.asp", "config.asp", "", "", "", "",
1573         ""},
1574        {"Status_Router.asp", "Status_Internet.asp", "Status_Lan.asp",
1575         "Status_Wireless.asp", "Status_SputnikAPD.asp", "Status_OpenVPN.asp",
1576         "Status_Bandwidth.asp", "Info.htm", "register.asp", "MyPage.asp", "",
1577         ""}
1578        };
1579        /*
1580         * real name is bmenu.menuname[i][j]
1581         */
1582        static char menuname_t[8][13][32] = {
1583                {"setup", "setupbasic", "setupddns", "setupmacclone",
1584                 "setuprouting", "setupvlan", "networking", "setupeop", "", "",
1585                 "", "", ""},
1586                {"wireless", "wirelessBasic", "wirelessSuperchannel", "wimax",
1587                 "wirelessRadius", "wirelessSecurity",
1588#ifdef HAVE_WPS
1589                 "wirelessAossWPS",
1590#else
1591                 "wirelessAoss",
1592#endif
1593                 "wirelessMac", "wirelessAdvanced", "wirelessWds", "", "", ""},
1594                {"services", "servicesServices", "servicesRadius",
1595                 "servicesPppoesrv", "servicesPptp", "servicesUSB",
1596                 "servicesNAS", "servicesHotspot", "servicesNintendo",
1597                 "servicesMilkfish", "servicesAnchorFree", "", ""},
1598                {"security", "firwall", "vpn", "", "", "", "", "", "", "", "",
1599                 "", ""},
1600                {"accrestriction", "webaccess", "", "", "", "", "", "", "", "",
1601                 "", "", ""},
1602                {"applications", "applicationspforwarding",
1603                 "applicationsprforwarding", "applicationsptriggering",
1604                 "applicationsUpnp", "applicationsDMZ", "applicationsQoS",
1605                 "applicationsP2P", "", "", "", "", ""},
1606                {"admin", "adminManagement", "adminAlive", "adminDiag",
1607                 "adminWol", "adminFactory", "adminUpgrade", "adminBackup", "",
1608                 "", "", "", ""},
1609                {"statu", "statuRouter", "statuInet", "statuLAN", "statuWLAN",
1610                 "statuSputnik", "statuVPN", "statuBand", "statuSysInfo",
1611                 "statuActivate", "statuMyPage", "", ""}
1612        };
1613        static char menu[8][12][32];
1614        static char menuname[8][13][32];
1615        memcpy(menu, menu_t, 8 * 12 * 32);
1616        memcpy(menuname, menuname_t, 8 * 13 * 32);
1617#ifdef HAVE_ERC
1618        if (!wp->userid) {
1619                memcpy(menu, menu_s, 8 * 12 * 32);
1620                memcpy(menuname, menuname_s, 8 * 13 * 32);
1621        }
1622#endif
1623//fprintf(stderr,"generate menu content\n");
1624#ifdef HAVE_MADWIFI
1625#if defined(HAVE_BUFFALO) && !defined(HAVE_ATH9K)
1626        sprintf(&menu[1][8][0], "");
1627        sprintf(&menuname[1][9][0], "");
1628#else
1629        // fill up WDS
1630        int ifcount = getdevicecount();
1631        if (ifcount > 4)
1632                ifcount = 4;    //truncate to max of 4
1633        int a;
1634
1635        for (a = 0; a < ifcount; a++) {
1636                sprintf(&menu[1][a + 8][0], "Wireless_WDS-ath%d.asp", a);
1637                if (ifcount == 1)
1638                        sprintf(&menuname[1][a + 9][0], "wirelessWds");
1639                else
1640                        sprintf(&menuname[1][a + 9][0], "wirelessWds%d", a);
1641        }
1642#endif
1643#else
1644#ifdef HAVE_ERC
1645        if (wp->userid) {
1646#endif
1647
1648                int ifcount = get_wl_instances();
1649                int a;
1650
1651                for (a = 0; a < ifcount; a++) {
1652                        sprintf(&menu[1][a * 2 + 7][0],
1653                                "Wireless_Advanced-wl%d.asp", a);
1654                        sprintf(&menu[1][a * 2 + 8][0], "Wireless_WDS-wl%d.asp",
1655                                a);
1656                        if (ifcount == 1) {
1657                                sprintf(&menuname[1][a * 2 + 8][0],
1658                                        "wirelessAdvanced");
1659                                sprintf(&menuname[1][a * 2 + 9][0],
1660                                        "wirelessWds");
1661                        } else {
1662                                sprintf(&menuname[1][a * 2 + 8][0],
1663                                        "wirelessAdvancedwl%d", a);
1664                                sprintf(&menuname[1][a * 2 + 9][0],
1665                                        "wirelessWdswl%d", a);
1666                        }
1667                }
1668#ifdef HAVE_ERC
1669        }
1670#endif
1671#endif
1672//fprintf(stderr,"generate menu header content\n");
1673
1674        int i, j;
1675
1676        websWrite(wp, "<div id=\"menu\">\n");
1677        websWrite(wp, " <div id=\"menuMain\">\n");
1678        websWrite(wp, "  <ul id=\"menuMainList\">\n");
1679#ifdef HAVE_WAVESAT
1680        wimaxwifi = 1;
1681#endif
1682        for (i = 0; i < 8; i++) {
1683                if (strlen(menu[i][0]) == 0)
1684                        continue;
1685#ifdef HAVE_MADWIFI
1686                if (!wifi && !wimaxwifi
1687                    && !strcmp(menu[i][0], "Wireless_Basic.asp"))
1688                        i++;
1689#endif
1690//fprintf(stderr,"generate menu %s\n",menu[i][0]);
1691                if (!strcmp(menu[i][0], mainmenu)) {
1692#ifdef HAVE_MADWIFI
1693                        if (!wifi && wimaxwifi
1694                            && !strcmp(menu[i][0], "Wireless_Basic.asp"))
1695                                websWrite(wp,
1696                                          "   <li class=\"current\"><span><strong><script type=\"text/javascript\">Capture(bmenu.wimax)</script></strong></span>\n");
1697                        else
1698#endif
1699                                websWrite(wp,
1700                                          "   <li class=\"current\"><span><strong><script type=\"text/javascript\">Capture(bmenu.%s)</script></strong></span>\n",
1701                                          menuname[i][0]);
1702                        websWrite(wp, "    <div id=\"menuSub\">\n");
1703                        websWrite(wp, "     <ul id=\"menuSubList\">\n");
1704
1705                        for (j = 0; j < 12; j++) {
1706
1707#ifdef HAVE_MADWIFI
1708                                if (!wifi
1709                                    && !strncmp(menu[i][j],
1710                                                "Wireless_Basic.asp", 8))
1711                                        j++;
1712#ifndef HAVE_SUPERCHANNEL
1713                                if (!strcmp(menu[i][j], "SuperChannel.asp"))    // jump over
1714                                        // PPTP in
1715                                        // micro
1716                                        // build
1717                                        j++;
1718#else
1719                                if (!strcmp(menu[i][j], "SuperChannel.asp") && (issuperchannel() || !wifi))     // jump
1720                                        // over
1721                                        // PPTP
1722                                        // in
1723                                        // micro
1724                                        // build
1725                                        j++;
1726#endif
1727#else
1728                                if (!strcmp(menu[i][j], "SuperChannel.asp"))    // jump over
1729                                        // PPTP in
1730                                        // micro
1731                                        // build
1732                                        j++;
1733#endif
1734#ifndef HAVE_WAVESAT
1735                                if (!strcmp(menu[i][j], "WiMAX.asp"))   // jump over
1736                                        // WiMAX
1737                                        j++;
1738#else
1739                                if (!wimaxwifi && !strcmp(menu[i][j], "WiMAX.asp"))     // jump
1740                                        // over
1741                                        // WiMAX
1742                                        j++;
1743#endif
1744#ifndef HAVE_AOSS
1745                                if (!strcmp(menu[i][j], "AOSS.asp"))    // jump over
1746                                        // AOSS
1747                                        j++;
1748#endif
1749#ifndef HAVE_SPOTPASS
1750                                if (!strcmp(menu[i][j], "Nintendo.asp"))        // jump over
1751                                        // Nintendo
1752                                        j++;
1753#endif
1754#ifdef HAVE_CORENET
1755                                if (!strcmp(menu[i][j], "Firewall.asp") ||
1756                                    !strcmp(menu[i][j], "Filters.asp") ||
1757                                    !strcmp(menu[i][j], "ForwardSpec.asp"))     // jump over
1758                                        // Corenet
1759                                        j++;
1760#endif
1761#ifdef HAVE_MADWIFI
1762                                if (!wifi && !strcmp(menu[i][j], "WL_WPATable.asp"))    // jump
1763                                        // over
1764                                        // PPTP
1765                                        // in
1766                                        // micro
1767                                        // build
1768                                        j++;
1769                                if (!strcmp(menu[i][j], "Wireless_radauth.asp"))
1770                                        j++;
1771                                if (!wifi
1772                                    && !strncmp(menu[i][j], "Wireless_MAC.asp",
1773                                                8))
1774                                        j++;
1775                                if (!strncmp
1776                                    (menu[i][j], "Wireless_Advanced", 17))
1777                                        j++;
1778                                if ((!wifi || cpeonly)
1779                                    && !strncmp(menu[i][j], "Wireless_WDS", 12))
1780                                        j++;
1781                                if (!wifi
1782                                    && !strcmp(menu[i][j],
1783                                               "Status_Wireless.asp"))
1784                                        j++;
1785
1786#endif
1787                                if ((!vlan_supp) && !strcmp(menu[i][j], "Vlan.asp"))    // jump
1788                                        // over
1789                                        // VLANs
1790                                        // if
1791                                        // vlan
1792                                        // not
1793                                        // supported
1794                                        j++;
1795#ifndef HAVE_FREERADIUS
1796                                if (!strcmp(menu[i][j], "FreeRadius.asp"))
1797                                        j++;
1798#endif
1799#ifndef HAVE_PPPOESERVER
1800                                if (!strcmp(menu[i][j], "PPPoE_Server.asp"))
1801                                        j++;
1802#endif
1803#ifdef HAVE_MICRO
1804                                if (!strcmp(menu[i][j], "PPTP.asp"))    // jump over PPTP in
1805                                        // micro build
1806                                        j++;
1807#endif
1808#ifndef HAVE_USB
1809                                if (!strcmp(menu[i][j], "USB.asp"))     // jump over USB
1810                                        j++;
1811#endif
1812#ifndef HAVE_NAS_SERVER
1813                                if (!strcmp(menu[i][j], "NAS.asp"))     // jump over NAS
1814                                        j++;
1815#endif
1816#ifdef HAVE_GLAUCO
1817                                if (!strcmp(menu[i][j], "Factory_Defaults.asp"))
1818                                        j++;
1819                                if (!strcmp(menu[i][j], "Upgrade.asp"))
1820                                        j++;
1821#endif
1822#ifndef HAVE_MILKFISH
1823                                if (!strcmp(menu[i][j], "Milkfish.asp"))
1824                                        j++;
1825#endif
1826#ifdef HAVE_WIKINGS
1827                                if (!strcmp(menu[i][j], "AnchorFree.asp"))
1828                                        j++;
1829#endif
1830#ifdef HAVE_ESPOD
1831                                if (!strcmp(menu[i][j], "AnchorFree.asp"))
1832                                        j++;
1833#endif
1834#ifdef HAVE_CARLSONWIRELESS
1835                                if (!strcmp(menu[i][j], "AnchorFree.asp"))
1836                                        j++;
1837#endif
1838#ifndef HAVE_WOL
1839                                if (!strcmp(menu[i][j], "Wol.asp"))
1840                                        j++;
1841#endif
1842#ifndef HAVE_EOP_TUNNEL
1843                                if (!strcmp(menu[i][j], "eop-tunnel.asp"))
1844                                        j++;
1845#endif
1846#ifndef HAVE_VLANTAGGING
1847                                if (!strcmp(menu[i][j], "Networking.asp"))
1848                                        j++;
1849#endif
1850#ifndef HAVE_CTORRENT
1851                                if (!strcmp(menu[i][j], "P2P.asp"))
1852                                        j++;
1853#endif
1854                                if ((!sputnik) && !strcmp(menu[i][j], "Status_SputnikAPD.asp")) // jump
1855                                        // over
1856                                        // Sputnik
1857                                        j++;
1858                                if ((!openvpn) && !strcmp(menu[i][j], "Status_OpenVPN.asp"))    // jump
1859                                        // over
1860                                        // OpenVPN
1861                                        j++;
1862                                if ((!auth) && !strcmp(menu[i][j], "Info.htm")) // jump
1863                                        // over
1864                                        // Sys-Info
1865                                        j++;
1866                                if ((registered) && !cpeonly && !strcmp(menu[i][j], "register.asp"))    // jump
1867                                        // over
1868                                        // register.asp
1869                                        j++;
1870                                if ((!strlen(nvram_safe_get("mypage_scripts"))) && !strcmp(menu[i][j], "MyPage.asp"))   // jump
1871                                        // over
1872                                        // MyPage.asp
1873                                        j++;
1874#ifdef HAVE_MADWIFI
1875                                if (!strcmp(menu[i][j], submenu)
1876                                    && (strlen(menu[i][j])
1877                                        && !strcmp(menu[i][j],
1878                                                   "Wireless_Basic.asp")
1879                                        && !wifi && wimaxwifi)) {
1880                                        websWrite(wp,
1881                                                  "      <li><span><strong><script type=\"text/javascript\">Capture(bmenu.wimax)</script></strong></span></li>\n");
1882                                }
1883#endif
1884                                else if (!strcmp(menu[i][j], submenu)
1885                                         && (strlen(menu[i][j]))) {
1886                                        websWrite(wp,
1887                                                  "      <li><span><strong><script type=\"text/javascript\">Capture(bmenu.%s)</script></strong></span></li>\n",
1888                                                  menuname[i][j + 1]);
1889                                }
1890#ifdef HAVE_HTTPS               // until https will allow upgrade and backup
1891#ifdef HAVE_MATRIXSSL
1892                                else if ((strlen(menu[i][j]) != 0) && (do_ssl)
1893                                         && ((!strcmp(menu[i][j], "Upgrade.asp")
1894                                              ||
1895                                              (!strcmp
1896                                               (menu[i][j], "config.asp"))))) {
1897                                        websWrite(wp,
1898                                                  "      <script type=\"text/javascript\">\n//<![CDATA[\n");
1899                                        websWrite(wp,
1900                                                  "      document.write(\"<li><a style=\\\"cursor:pointer\\\" title=\\\"\" + errmsg.err46 + \"\\\" onclick=\\\"alert(errmsg.err45)\\\" ><em>\" + bmenu.%s + \"</em></a></li>\");\n",
1901                                                  menuname[i][j + 1]);
1902                                        websWrite(wp,
1903                                                  "      \n//]]>\n</script>\n");
1904                                }
1905#endif
1906#endif
1907#ifdef HAVE_MADWIFI
1908                                else if (strlen(menu[i][j])
1909                                         && !strcmp(menu[i][j],
1910                                                    "Wireless_Basic.asp")
1911                                         && !wifi && wimaxwifi) {
1912                                        websWrite(wp,
1913                                                  "      <li><a href=\"WiMAX.asp\"><strong><script type=\"text/javascript\">Capture(bmenu.wimax)</script></strong></a></li>\n");
1914                                }
1915#endif
1916                                else if (strlen(menu[i][j])) {
1917                                        websWrite(wp,
1918                                                  "      <li><a href=\"%s\"><strong><script type=\"text/javascript\">Capture(bmenu.%s)</script></strong></a></li>\n",
1919                                                  menu[i][j],
1920                                                  menuname[i][j + 1]);
1921                                }
1922                        }
1923                        websWrite(wp, "     </ul>\n");
1924                        websWrite(wp, "    </div>\n");
1925                        websWrite(wp, "    </li>\n");
1926                }
1927#ifdef HAVE_MADWIFI
1928                else if (!strcmp(menu[i][0], "Wireless_Basic.asp") && !wifi
1929                         && wimaxwifi) {
1930                        websWrite(wp,
1931                                  "      <li><a href=\"WiMAX.asp\"><strong><script type=\"text/javascript\">Capture(bmenu.wimax)</script></strong></a></li>\n");
1932                }
1933#endif
1934                else {
1935                        websWrite(wp,
1936                                  "   <li><a href=\"%s\"><strong><script type=\"text/javascript\">Capture(bmenu.%s)</script></strong></a></li>\n",
1937                                  menu[i][0], menuname[i][0]);
1938                }
1939        }
1940        websWrite(wp, "  </ul>\n");
1941        websWrite(wp, " </div>\n");
1942        websWrite(wp, "</div>\n");
1943
1944        return;
1945}
1946
1947void ej_do_pagehead(webs_t wp, int argc, char_t ** argv)        // Eko
1948{
1949        char *style = nvram_get("router_style");
1950
1951#ifndef FASTWEB
1952        if (argc < 1) {
1953                websError(wp, 400, "Insufficient args\n");
1954                return;
1955        }
1956#endif
1957
1958        /*
1959         * websWrite (wp, "<\?xml version=\"1.0\" encoding=\"%s\"\?>\n",
1960         * live_translate("lang_charset.set")); IE Problem ...
1961         */
1962        websWrite(wp,
1963                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
1964        websWrite(wp, "<html>\n");
1965        websWrite(wp, "\t<head>\n");
1966        websWrite(wp,
1967                  "\t\t<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
1968                  live_translate("lang_charset.set"));
1969#ifndef HAVE_MICRO
1970        websWrite(wp,
1971                  "\t\t<link rel=\"icon\" href=\"images/favicon.ico\" type=\"image/x-icon\" />\n");
1972        websWrite(wp,
1973                  "\t\t<link rel=\"shortcut icon\" href=\"images/favicon.ico\" type=\"image/x-icon\" />\n");
1974#endif
1975        websWrite(wp,
1976                  "\t\t<script type=\"text/javascript\" src=\"common.js\"></script>\n");
1977        websWrite(wp,
1978                  "\t\t<script type=\"text/javascript\" src=\"lang_pack/english.js\"></script>\n");
1979#ifdef HAVE_LANGUAGE
1980        websWrite(wp,
1981                  "\t\t<script type=\"text/javascript\" src=\"lang_pack/language.js\"></script>\n");
1982#endif
1983        websWrite(wp,
1984                  "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/%s/style.css\" />\n",
1985                  style);
1986        websWrite(wp,
1987                  "\t\t<!--[if IE]><link type=\"text/css\" rel=\"stylesheet\" href=\"style/%s/style_ie.css\" /><![endif]-->\n",
1988                  style);
1989
1990#ifdef HAVE_PWC
1991        websWrite(wp,
1992                  "\t\t<script type=\"text/javascript\" src=\"js/prototype.js\"></script>\n");
1993        websWrite(wp,
1994                  "\t\t<script type=\"text/javascript\" src=\"js/effects.js\"></script>\n");
1995        websWrite(wp,
1996                  "\t\t<script type=\"text/javascript\" src=\"js/window.js\"></script>\n");
1997        websWrite(wp,
1998                  "\t\t<script type=\"text/javascript\" src=\"js/window_effects.js\"></script>\n");
1999        websWrite(wp,
2000                  "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/pwc/default.css\" />\n");
2001        websWrite(wp,
2002                  "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"style/pwc/ddwrt.css\" />\n");
2003#endif
2004#ifdef HAVE_WIKINGS
2005        websWrite(wp, "\t\t<title>:::: Excel Networks ::::");
2006#elif HAVE_ESPOD
2007        websWrite(wp, "\t\t<title>ESPOD Technologies");
2008#else
2009        websWrite(wp, "\t\t<title>%s (build %s)", nvram_get("router_name"),
2010                  SVN_REVISION);
2011#endif
2012        if (strlen(argv[0]) != 0) {
2013                websWrite(wp, " - %s", live_translate(argv[0]));
2014        }
2015        websWrite(wp, "</title>\n");
2016
2017}
2018
2019void ej_do_hpagehead(webs_t wp, int argc, char_t ** argv)       // Eko
2020{
2021        char *htitle;
2022
2023#ifdef FASTWEB
2024        ejArgs(argc, argv, "%s", &htitle);
2025#else
2026        if (ejArgs(argc, argv, "%s", &htitle) < 1) {
2027                websError(wp, 400, "Insufficient args\n");
2028                return;
2029        }
2030#endif
2031        websWrite(wp,
2032                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
2033        if (!strcmp(htitle, "doctype_only"))
2034                return;         // stop here, for About.htm
2035        websWrite(wp, "<html>\n");
2036        websWrite(wp, "\t<head>\n");
2037        websWrite(wp,
2038                  "\t\t<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=%s\" />\n",
2039                  live_translate("lang_charset.set"));
2040        websWrite(wp,
2041                  "\t\t<script type=\"text/javascript\" src=\"../common.js\"></script>\n");
2042        websWrite(wp,
2043                  "\t\t<script type=\"text/javascript\" src=\"../lang_pack/english.js\"></script>\n");
2044#ifdef HAVE_LANGUAGE
2045        websWrite(wp,
2046                  "\t\t<script type=\"text/javascript\" src=\"../lang_pack/language.js\"></script>\n");
2047#endif
2048        websWrite(wp,
2049                  "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"help.css\">\n");
2050        websWrite(wp, "\t\t<title>%s (build %s)", live_translate("share.help"),
2051                  SVN_REVISION);
2052        websWrite(wp, " - %s</title>\n", live_translate(htitle));
2053        websWrite(wp, "\t</head>\n");
2054
2055}
2056
2057void ej_show_timeoptions(webs_t wp, int argc, char_t ** argv)   // Eko
2058{
2059
2060        char timediffs[39][8] =
2061            { "-12", "-11", "-10", "-09.5", "-09", "-08", "-07", "-06", "-05",
2062                "-04.5", "-04",
2063                "-03.5", "-03", "-02", "-01", "+00",
2064                "+01", "+02", "+03", "+03.5", "+04", "+04.5", "+05", "+05.5",
2065                "+05.75",
2066                "+06",
2067                "+06.5", "+07", "+08", "+09", "+09.5", "+10", "+10.5", "+11",
2068                "+11.5",
2069                "+12", "+12.75", "+13", "+14"
2070        };
2071
2072        char timezones[39][8] =
2073            { "-12:00", "-11:00", "-10:00", "-09:30", "-09:00", "-08:00",
2074                "-07:00",
2075                "-06:00", "-05:00", "-04:30", "-04:00", "-03:30", "-03:00",
2076                "-02:00",
2077                "-01:00", "",
2078                "+01:00", "+02:00", "+03:00", "+03:30", "+04:00", "+04:30",
2079                "+05:00",
2080                "+05:30",
2081                "+05:45", "+06:00", "+06:30", "+07:00", "+08:00", "+09:00",
2082                "+09:30",
2083                "+10:00", "+10:30", "+11:00", "+11:30", "+12:00", "+12:45",
2084                "+13:00",
2085                "+14:00"
2086        };
2087
2088        int i;
2089
2090        for (i = 0; i < 39; i++) {
2091                websWrite(wp,
2092                          "<option value=\"%s\" %s>UTC%s</option>\n",
2093                          timediffs[i], nvram_match("time_zone",
2094                                                    timediffs[i]) ?
2095                          "selected=\"selected\"" : "", timezones[i]);
2096
2097        }
2098
2099}
2100
2101void ej_show_wanipinfo(webs_t wp, int argc, char_t ** argv)     // Eko
2102{
2103        char *wan_ipaddr;
2104        int wan_link;
2105
2106        if (getWET() || nvram_match("wan_proto", "disabled")
2107            || nvram_match("wan_proto", "bridge")) {
2108                websWrite(wp, ": %s", live_translate("share.disabled"));
2109                return;
2110        }
2111
2112        wan_link = check_wan_link(0);
2113        char *wan_proto = nvram_safe_get("wan_proto");
2114        if (!strcmp(wan_proto, "pptp")) {
2115                wan_ipaddr =
2116                    wan_link ? nvram_safe_get("pptp_get_ip") :
2117                    nvram_safe_get("wan_ipaddr");
2118        } else if (!strcmp(wan_proto, "pppoe")
2119#ifdef HAVE_PPPOATM
2120                   || !strcmp(wan_proto, "pppoa")
2121#endif
2122#ifdef HAVE_3G
2123                   || !strcmp(wan_proto, "3g")
2124#endif
2125            ) {
2126                wan_ipaddr =
2127                    wan_link ? nvram_safe_get("wan_ipaddr") : "0.0.0.0";
2128#ifdef HAVE_L2TP
2129        } else if (nvram_match("wan_proto", "l2tp")) {
2130                wan_ipaddr =
2131                    wan_link ? nvram_safe_get("l2tp_get_ip") :
2132                    nvram_safe_get("wan_ipaddr");
2133#endif
2134        } else {
2135                wan_ipaddr = nvram_safe_get("wan_ipaddr");
2136        }
2137
2138        websWrite(wp, "&nbsp;IP: %s", wan_ipaddr);
2139
2140        return;
2141}
2142
2143/*
2144 * Example:
2145 * wan_proto=dhcp
2146 * <% nvram_selected("wan_proto", "dhcp",); %> produces: selected="selected"
2147 * <% nvram_selected_js("wan_proto", "dhcp"); %> produces: selected=\"selected\"
2148 * <% nvram_selected("wan_proto", "static"); %> does not produce
2149 */
2150void ej_nvram_selected(webs_t wp, int argc, char_t ** argv)
2151{
2152
2153#ifndef FASTWEB
2154        if (argc < 2) ;
2155        {
2156                websError(wp, 400, "Insufficient args\n");
2157                return;
2158        }
2159#endif
2160
2161        if (nvram_match(argv[0], argv[1])) {
2162                websWrite(wp, "selected=\"selected\"");
2163        }
2164        return;
2165}
2166
2167void ej_nvram_selected_js(webs_t wp, int argc, char_t ** argv)
2168{
2169
2170#ifndef FASTWEB
2171        if (argc < 2) ;
2172        {
2173                websError(wp, 400, "Insufficient args\n");
2174                return;
2175        }
2176#endif
2177
2178        if (nvram_match(argv[0], argv[1])) {
2179                websWrite(wp, "selected=\\\"selected\\\"");
2180        }
2181        return;
2182}
2183
2184void ej_getrebootflags(webs_t wp, int argc, char_t ** argv)
2185{
2186#ifdef HAVE_RB500
2187        websWrite(wp, "1");
2188#elif HAVE_MAGICBOX
2189        websWrite(wp, "2");
2190#elif HAVE_RB600
2191        websWrite(wp, "2");
2192#elif HAVE_FONERA
2193        websWrite(wp, "2");
2194#elif HAVE_MERAKI
2195        websWrite(wp, "2");
2196#elif HAVE_LS2
2197        websWrite(wp, "2");
2198#elif HAVE_LS5
2199        websWrite(wp, "2");
2200#elif HAVE_WHRAG108
2201        websWrite(wp, "2");
2202#elif HAVE_TW6600
2203        websWrite(wp, "2");
2204#elif HAVE_CA8
2205        websWrite(wp, "2");
2206#elif HAVE_GATEWORX
2207        websWrite(wp, "1");
2208#elif HAVE_X86
2209        websWrite(wp, "1");
2210#else
2211        websWrite(wp, "0");
2212#endif
2213}
2214
2215void ej_tran(webs_t wp, int argc, char_t ** argv)
2216{
2217
2218#ifndef FASTWEB
2219        if (argc != 1)
2220                return;
2221#endif
2222        websWrite(wp, "<script type=\"text/javascript\">Capture(%s)</script>",
2223                  argv[0]);
2224        return;
2225}
2226
2227/*
2228 * Example:
2229 * wan_proto=dhcp
2230 * <% nvram_checked("wan_proto", "dhcp"); %> produces: checked="checked"
2231 * <% nvram_checked_js("wan_proto", "dhcp"); %> produces: checked=\"checked\"
2232 * <% nvram_checked("wan_proto", "static"); %> does not produce
2233 */
2234void ej_nvram_checked(webs_t wp, int argc, char_t ** argv)
2235{
2236
2237#ifdef FASTWEB
2238        if (argc < 2) {
2239                websError(wp, 400, "Insufficient args\n");
2240                return;
2241        }
2242#endif
2243
2244        if (nvram_match(argv[0], argv[1])) {
2245                websWrite(wp, "checked=\"checked\"");
2246        }
2247
2248        return;
2249}
2250
2251void ej_nvram_checked_js(webs_t wp, int argc, char_t ** argv)
2252{
2253
2254#ifndef FASTWEB
2255        if (argc < 2) {
2256                websError(wp, 400, "Insufficient args\n");
2257                return;
2258        }
2259#endif
2260
2261        if (nvram_match(argv[0], argv[1])) {
2262                websWrite(wp, "checked=\\\"checked\\\"");
2263        }
2264
2265        return;
2266}
2267
2268void ej_make_time_list(webs_t wp, int argc, char_t ** argv)
2269{
2270        int i, st, en;
2271        char ic[16];
2272
2273#ifndef FASTWEB
2274        if (argc < 3) {
2275                websError(wp, 400, "Insufficient args\n");
2276                return;
2277        }
2278#endif
2279
2280        st = atoi(argv[1]);
2281        en = atoi(argv[2]);
2282
2283        for (i = st; i <= en; i++) {
2284                sprintf(ic, "%d", i);
2285                websWrite(wp, "<option value=\"%d\" %s >%02d</option>\n", i,
2286                          nvram_match(argv[0],
2287                                      ic) ? "selected=\"selected\"" : "", i);
2288        }
2289
2290        return;
2291}
2292
2293#ifdef HAVE_CPUTEMP
2294void ej_get_cputemp(webs_t wp, int argc, char_t ** argv)
2295{
2296#ifdef HAVE_GATEWORX
2297        int TEMP_MUL = 100;
2298
2299        if (getRouterBrand() == ROUTER_BOARD_GATEWORX_SWAP)
2300                TEMP_MUL = 200;
2301
2302        FILE *fp =
2303            fopen
2304            ("/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028/temp_input",
2305             "rb");
2306        if (!fp)
2307                fp = fopen
2308                    ("/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0028/temp1_input",
2309                     "rb");
2310#elif HAVE_LAGUNA
2311        int TEMP_MUL = 10;
2312        FILE *fp = fopen("/sys/bus/i2c/devices/0-0029/temp0_input", "rb");
2313#else
2314#define TEMP_MUL 1000
2315#ifdef HAVE_X86
2316        FILE *fp =
2317            fopen("/sys/devices/platform/i2c-1/1-0048/temp1_input", "rb");
2318#else
2319        FILE *fp =
2320            fopen("/sys/devices/platform/i2c-0/0-0048/temp1_input", "rb");
2321#endif
2322#endif
2323
2324        if (fp == NULL) {
2325                websWrite(wp, "%s", live_translate("status_router.notavail"));  // no
2326                // i2c
2327                // lm75
2328                // found
2329                return;
2330        }
2331        int temp;
2332
2333        fscanf(fp, "%d", &temp);
2334        fclose(fp);
2335        int high = temp / TEMP_MUL;
2336        int low = (temp - (high * TEMP_MUL)) / (TEMP_MUL / 10);
2337
2338        websWrite(wp, "%d.%d &#176;C", high, low);      // no i2c lm75 found
2339}
2340
2341void ej_show_cpu_temperature(webs_t wp, int argc, char_t ** argv)
2342{
2343        websWrite(wp, "<div class=\"setting\">\n");
2344        websWrite(wp,
2345                  "<div class=\"label\"><script type=\"text/javascript\">Capture(status_router.cputemp)</script></div>\n");
2346        websWrite(wp, "<span id=\"cpu_temp\">");
2347        ej_get_cputemp(wp, argc, argv);
2348        websWrite(wp, "</span>&nbsp;\n");
2349        websWrite(wp, "</div>\n");
2350}
2351#endif
2352
2353#ifdef HAVE_VOLT
2354void ej_get_voltage(webs_t wp, int argc, char_t ** argv)
2355{
2356#ifdef HAVE_LAGUNA
2357        FILE *fp = fopen("/sys/bus/i2c/devices/0-0029/in0_input", "rb");
2358#else
2359        FILE *fp =
2360            fopen
2361            ("/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028/volt",
2362             "rb");
2363        if (!fp)
2364                fp = fopen
2365                    ("/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0028/in1_input",
2366                     "rb");
2367#endif
2368        if (fp == NULL) {
2369                websWrite(wp, "%s", live_translate("status_router.notavail"));  // no
2370                // i2c
2371                // lm75
2372                // found
2373                return;
2374        }
2375        int temp;
2376
2377        fscanf(fp, "%d", &temp);
2378        fclose(fp);
2379        // temp*=564;
2380        int high = temp / 1000;
2381        int low = (temp - (high * 1000)) / 100;
2382
2383        websWrite(wp, "%d.%d V", high, low);    // no i2c lm75 found
2384}
2385
2386void ej_show_voltage(webs_t wp, int argc, char_t ** argv)
2387{
2388        websWrite(wp, "<div class=\"setting\">\n");
2389        websWrite(wp,
2390                  "<div class=\"label\"><script type=\"text/javascript\">Capture(status_router.inpvolt)</script></div>\n");
2391        websWrite(wp, "<span id=\"voltage\">");
2392        ej_get_voltage(wp, argc, argv);
2393        websWrite(wp, "</span>&nbsp;\n");
2394        websWrite(wp, "</div>\n");
2395}
2396#endif
2397
2398static void showencstatus(webs_t wp, char *prefix)
2399{
2400        char akm[64];
2401
2402        sprintf(akm, "%s_akm", prefix);
2403        websWrite(wp, "<div class=\"setting\">\n");
2404        websWrite(wp,
2405                  "<div class=\"label\"><script type=\"text/javascript\">Capture(share.encrypt)</script>&nbsp;-&nbsp;<script type=\"text/javascript\">Capture(share.intrface)</script>&nbsp;%s</div>\n",
2406                  prefix);
2407        websWrite(wp, "<script type=\"text/javascript\">");
2408        if (nvram_match(akm, "disabled")) {
2409                websWrite(wp, "Capture(share.disabled)");
2410                websWrite(wp, "</script>");
2411        } else {
2412                websWrite(wp, "Capture(share.enabled)");
2413                websWrite(wp, "</script>,&nbsp;");
2414
2415                if (nvram_match(akm, "psk"))
2416                        websWrite(wp, "WPA Personal");
2417                if (nvram_match(akm, "wpa"))
2418                        websWrite(wp, "WPA Enterprise");
2419                if (nvram_match(akm, "psk2"))
2420                        websWrite(wp, "WPA2 Personal");
2421                if (nvram_match(akm, "wpa2"))
2422                        websWrite(wp, "WPA2 Enterprise");
2423                if (nvram_match(akm, "psk psk2"))
2424                        websWrite(wp, "WPA2 Personal Mixed");
2425                if (nvram_match(akm, "wpa wpa2"))
2426                        websWrite(wp, "WPA Enterprise Mixed");
2427                if (nvram_match(akm, "radius"))
2428                        websWrite(wp, "RADIUS");
2429                if (nvram_match(akm, "wep"))
2430                        websWrite(wp, "WEP");
2431                if (nvram_match(akm, "8021X"))
2432                        websWrite(wp, "802.1x");
2433        }
2434
2435        websWrite(wp, "\n</div>\n");
2436        return;
2437}
2438
2439void ej_get_txpower(webs_t wp, int argc, char_t ** argv)
2440{
2441        char txpwr[32];
2442        char m[32];
2443        int txpower;
2444        char mode[32];
2445
2446        strncpy(m, nvram_safe_get("wifi_display"), 4);
2447        m[4] = 0;
2448        sprintf(mode, "%s_net_mode", m);
2449        if (nvram_match(mode, "disabled")) {
2450                txpower = 0;
2451        } else {
2452
2453                sprintf(txpwr, "%s_txpwr", m);
2454#ifdef HAVE_MADWIFI
2455                txpower = wifi_gettxpower(m);
2456#elif HAVE_RT2880
2457                txpower = atoi(nvram_safe_get(txpwr));
2458#else                           //broadcom
2459                txpower = bcm_gettxpower(m);
2460#endif
2461#ifdef HAVE_BUFFALO
2462                get_txpower_extended(wp, txpower, m);
2463#else
2464#ifdef HAVE_MADWIFI
2465                websWrite(wp, "%d dBm", txpower);
2466#elif HAVE_RT2880
2467                websWrite(wp, "%d mW", txpower);
2468#else                           //broadcom
2469                websWrite(wp, "%d mW", txpower);
2470#endif
2471#endif
2472        }
2473}
2474
2475void ej_getencryptionstatus(webs_t wp, int argc, char_t ** argv)
2476{
2477        char *mode = nvram_safe_get("wifi_display");
2478
2479        showencstatus(wp, mode);
2480}
2481
2482void ej_getwirelessstatus(webs_t wp, int argc, char_t ** argv)
2483{
2484        char var[32];
2485        char m[32];
2486        int showap = 0, showcli = 0;
2487
2488        strncpy(m, nvram_safe_get("wifi_display"), 4);
2489        m[4] = 0;
2490        sprintf(var, "%s_mode", m);
2491
2492        if (nvram_match(var, "ap") || nvram_match(var, "wdsap")) {
2493                showap = 1;     // "Clients"
2494        } else {
2495                showcli = 1;    // "Access Point"
2496                sprintf(var, "%s_vifs", m);
2497                if (strlen(nvram_safe_get(var)) > 0)
2498                        showap = 1;     // " & Clients"
2499        }
2500
2501        if (showcli)
2502                websWrite(wp,
2503                          "<script type=\"text/javascript\">Capture(info.ap)</script>");
2504        if (showcli && showap)
2505                websWrite(wp, " & ");
2506        if (showap)
2507                websWrite(wp,
2508                          "<script type=\"text/javascript\">Capture(status_wireless.legend3)</script>");
2509
2510}
2511
2512void ej_getwirelessssid(webs_t wp, int argc, char_t ** argv)
2513{
2514        char ssid[32];
2515
2516        sprintf(ssid, "%s_ssid", nvram_safe_get("wifi_display"));
2517        tf_webWriteESCNV(wp, ssid);
2518
2519}
2520
2521void ej_getwirelessmode(webs_t wp, int argc, char_t ** argv)
2522{
2523        char mode[32];
2524
2525        sprintf(mode, "%s_mode", nvram_safe_get("wifi_display"));
2526
2527        websWrite(wp, "<script type=\"text/javascript\">");
2528        if (nvram_match(mode, "wet"))
2529                websWrite(wp, "Capture(wl_basic.clientBridge)");
2530        if (nvram_match(mode, "ap"))
2531                websWrite(wp, "Capture(wl_basic.ap)");
2532        if (nvram_match(mode, "sta"))
2533                websWrite(wp, "Capture(wl_basic.client)");
2534        if (nvram_match(mode, "infra"))
2535                websWrite(wp, "Capture(wl_basic.adhoc)");
2536        if (nvram_match(mode, "apsta"))
2537                websWrite(wp, "Capture(wl_basic.repeater)");
2538        if (nvram_match(mode, "apstawet"))
2539                websWrite(wp, "Capture(wl_basic.repeaterbridge)");
2540        if (nvram_match(mode, "wdssta"))
2541                websWrite(wp, "Capture(wl_basic.wdssta)");
2542        if (nvram_match(mode, "wdsap"))
2543                websWrite(wp, "Capture(wl_basic.wdsap)");
2544        websWrite(wp, "</script>&nbsp;\n");
2545}
2546
2547void ej_getwirelessnetmode(webs_t wp, int argc, char_t ** argv)
2548{
2549
2550        char mode[32];
2551        char m[32];
2552
2553        strncpy(m, nvram_safe_get("wifi_display"), 4);
2554        m[4] = 0;
2555        sprintf(mode, "%s_net_mode", m);
2556
2557        websWrite(wp, "<script type=\"text/javascript\">");
2558        if (nvram_match(mode, "disabled"))
2559                websWrite(wp, "Capture(share.disabled)");
2560        if (nvram_match(mode, "mixed"))
2561                websWrite(wp, "Capture(wl_basic.mixed)");
2562        if (nvram_match(mode, "bg-mixed"))
2563                websWrite(wp, "Capture(wl_basic.bg)");
2564        if (nvram_match(mode, "g-only"))
2565                websWrite(wp, "Capture(wl_basic.g)");
2566        if (nvram_match(mode, "b-only"))
2567                websWrite(wp, "Capture(wl_basic.b)");
2568        if (nvram_match(mode, "n-only"))
2569                websWrite(wp, "Capture(wl_basic.n)");
2570        if (nvram_match(mode, "a-only"))
2571                websWrite(wp, "Capture(wl_basic.a)");
2572        if (nvram_match(mode, "na-only"))
2573                websWrite(wp, "Capture(wl_basic.na)");
2574        if (nvram_match(mode, "ng-only"))
2575                websWrite(wp, "Capture(wl_basic.ng)");
2576        if (nvram_match(mode, "n2-only"))
2577                websWrite(wp, "Capture(wl_basic.n2)");
2578        if (nvram_match(mode, "n5-only"))
2579                websWrite(wp, "Capture(wl_basic.n5)");
2580        websWrite(wp, "</script>&nbsp;\n");
2581}
2582
2583void ej_show_openvpn_status(webs_t wp, int argc, char_t ** argv)
2584{
2585        websWrite(wp,
2586                  "<fieldset>\n<legend><script type=\"text/javascript\">Capture(share.state)</script></legend>\n");
2587
2588        system2("/etc/openvpnstate.sh > /tmp/.temp");
2589        FILE *in = fopen("/tmp/.temp", "r");
2590
2591        while (!feof(in)) {
2592                int b = getc(in);
2593
2594                if (b != EOF)
2595                        wfputc(b, wp);
2596        }
2597        fclose(in);
2598        websWrite(wp, "</fieldset><br />");
2599        websWrite(wp,
2600                  "<fieldset>\n<legend><script type=\"text/javascript\">Capture(share.statu)</script></legend>\n");
2601        system2("/etc/openvpnstatus.sh > /tmp/.temp");
2602        in = fopen("/tmp/.temp", "r");
2603        while (!feof(in)) {
2604                int b = getc(in);
2605
2606                if (b != EOF)
2607                        wfputc(b, wp);
2608        }
2609        fclose(in);
2610        websWrite(wp, "</fieldset><br />");
2611        websWrite(wp,
2612                  "<fieldset>\n<legend><script type=\"text/javascript\">Capture(log.legend)</script></legend>\n");
2613        system2("/etc/openvpnlog.sh > /tmp/.temp");
2614        in = fopen("/tmp/.temp", "r");
2615        while (!feof(in)) {
2616                int b = getc(in);
2617
2618                if (b != EOF)
2619                        wfputc(b, wp);
2620        }
2621        fclose(in);
2622        websWrite(wp, "</fieldset><br />");
2623
2624}
2625
2626void ej_radio_on(webs_t wp, int argc, char_t ** argv)
2627{
2628        int radiooff = -1;
2629
2630#ifdef HAVE_MADWIFI
2631        char *ifname = nvram_safe_get("wifi_display");
2632
2633        if (strlen(ifname) > 0) {
2634                int state = get_radiostate(ifname);
2635
2636                switch (state) {
2637                case 1:
2638                        websWrite(wp, "1");
2639                        break;
2640                default:
2641                        websWrite(wp, "0");
2642                        break;
2643                }
2644        } else {
2645                websWrite(wp, "0");
2646        }
2647#elif HAVE_RT2880
2648
2649        int state = get_radiostate("wl0");
2650
2651        switch (state) {
2652        case 1:
2653                websWrite(wp, "1");
2654                break;
2655        default:
2656                websWrite(wp, "0");
2657                break;
2658        }
2659
2660#else
2661        char name[32];
2662        sprintf(name, "%s_ifname", nvram_safe_get("wifi_display"));
2663
2664        char *ifname = nvram_safe_get(name);
2665
2666        wl_ioctl(ifname, WLC_GET_RADIO, &radiooff, sizeof(int));
2667
2668        switch ((radiooff & WL_RADIO_SW_DISABLE)) {
2669        case 0:
2670                websWrite(wp, "1");
2671                break;
2672        default:
2673                websWrite(wp, "0");
2674                break;
2675        }
2676#endif
2677}
2678
2679void ej_get_radio_state(webs_t wp, int argc, char_t ** argv)
2680{
2681        int radiooff = -1;
2682
2683#ifdef HAVE_MADWIFI
2684        char *ifname = nvram_safe_get("wifi_display");
2685
2686        if (strlen(ifname) > 0) {
2687                int state = get_radiostate(ifname);
2688
2689                switch (state) {
2690                case 1:
2691                        websWrite(wp, "%s",
2692                                  live_translate("wl_basic.radio_on"));
2693                        break;
2694                case -1:
2695                        websWrite(wp, "%s", live_translate("share.unknown"));
2696                        break;
2697                default:        // 1: software disabled, 2: hardware
2698                        // disabled, 3: both are disabled
2699                        websWrite(wp, "%s",
2700                                  live_translate("wl_basic.radio_off"));
2701                        break;
2702                }
2703        } else {
2704                websWrite(wp, "%s", live_translate("share.unknown"));
2705        }
2706#elif HAVE_RT2880
2707
2708        int state = get_radiostate("wl0");
2709
2710        switch (state) {
2711        case 1:
2712                websWrite(wp, "%s", live_translate("wl_basic.radio_on"));
2713                break;
2714        case -1:
2715                websWrite(wp, "%s", live_translate("share.unknown"));
2716                break;
2717        default:                // 1: software disabled, 2: hardware
2718                // disabled, 3: both are disabled
2719                websWrite(wp, "%s", live_translate("wl_basic.radio_off"));
2720                break;
2721        }
2722
2723#else
2724        char name[32];
2725        sprintf(name, "%s_ifname", nvram_safe_get("wifi_display"));
2726
2727        char *ifname = nvram_safe_get(name);
2728
2729        wl_ioctl(ifname, WLC_GET_RADIO, &radiooff, sizeof(int));
2730
2731        switch ((radiooff & WL_RADIO_SW_DISABLE)) {
2732        case 0:
2733                websWrite(wp, "%s", live_translate("wl_basic.radio_on"));
2734                break;
2735        case -1:
2736                websWrite(wp, "%s", live_translate("share.unknown"));
2737                break;
2738        default:                // 1: software disabled, 2: hardware
2739                // disabled, 3: both are disabled
2740                websWrite(wp, "%s", live_translate("wl_basic.radio_off"));
2741                break;
2742        }
2743#endif
2744}
2745
2746void ej_dumparptable(webs_t wp, int argc, char_t ** argv)
2747{
2748        FILE *f;
2749        FILE *host;
2750        FILE *conn;
2751        char buf[256];
2752        char hostname[128];
2753        char ip[16];
2754        char ip2[20];
2755        char fullip[18];
2756        char mac[18];
2757        char landev[16];
2758        int count = 0;
2759        int conn_count = 0;
2760
2761        if ((f = fopen("/proc/net/arp", "r")) != NULL) {
2762                while (fgets(buf, sizeof(buf), f)) {
2763                        if (sscanf
2764                            (buf, "%15s %*s %*s %17s %*s %s", ip, mac,
2765                             landev) != 3)
2766                                continue;
2767                        if ((strlen(mac) != 17)
2768                            || (strcmp(mac, "00:00:00:00:00:00") == 0))
2769                                continue;
2770                        if (strcmp(landev, nvram_safe_get("wan_iface")) == 0)
2771                                continue;       // skip all but LAN arp entries
2772                        strcpy(hostname, "*");  // set name to *
2773
2774                        /*
2775                         * count open connections per IP
2776                         */
2777                        if ((conn =
2778                             fopen("/proc/net/ip_conntrack", "r")) != NULL) {
2779                                strcpy(ip2, ip);
2780                                strcat(ip2, " ");
2781
2782                                while (fgets(buf, sizeof(buf), conn)) {
2783                                        if (strstr(buf, ip2))
2784                                                conn_count++;
2785                                }
2786                                fclose(conn);
2787                        }
2788
2789                        /*
2790                         * end count
2791                         */
2792
2793                        /*
2794                         * do nslookup
2795                         */
2796
2797                        // struct servent *servp;
2798                        // char buf1[256];
2799                        //
2800                        // getHostName (buf1, ip);
2801                        // if (strcmp(buf1, "unknown"))
2802                        // strcpy (hostname, buf1);
2803                        /*
2804                         * end nslookup
2805                         */
2806
2807                        /*
2808                         * look into hosts file for hostnames (static leases)
2809                         */
2810                        if ((host = fopen("/tmp/hosts", "r")) != NULL
2811                            && !strcmp(hostname, "*")) {
2812                                while (fgets(buf, sizeof(buf), host)) {
2813                                        sscanf(buf, "%15s %*s", fullip);
2814
2815                                        if (!strcmp(ip, fullip)) {
2816                                                sscanf(buf, "%*15s %s",
2817                                                       hostname);
2818                                        }
2819                                }
2820                                fclose(host);
2821                        }
2822                        /*
2823                         * end hosts file lookup
2824                         */
2825
2826                        /*
2827                         * check for dnsmasq leases in /tmp/dnsmasq.leases and /jffs/ if
2828                         * hostname is still unknown
2829                         */
2830
2831                        if (!strcmp(hostname, "*")
2832                            && nvram_match("dhcp_dnsmasq", "1")
2833                            && nvram_match("dhcpd_usenvram", "0")) {
2834                                if (!(host = fopen("/tmp/dnsmasq.leases", "r")))
2835                                        host =
2836                                            fopen("/jffs/dnsmasq.leases", "r");
2837
2838                                if (host) {
2839
2840                                        while (fgets(buf, sizeof(buf), host)) {
2841                                                sscanf(buf, "%*s %*s %15s %*s",
2842                                                       fullip);
2843
2844                                                if (strcmp(ip, fullip) == 0) {
2845                                                        sscanf(buf,
2846                                                               "%*s %*s %*s %s",
2847                                                               hostname);
2848                                                }
2849                                        }
2850                                        fclose(host);
2851                                }
2852                        }
2853                        /*
2854                         * end dnsmasq.leases check
2855                         */
2856
2857                        /*
2858                         * check nvram for dnsmasq leases in nvram if hostname is still
2859                         * unknown
2860                         */
2861
2862                        if (!strcmp(hostname, "*")
2863                            && nvram_match("dhcp_dnsmasq", "1")
2864                            && nvram_match("dhcpd_usenvram", "1")) {
2865                                sscanf(nvram_nget("dnsmasq_lease_%s", ip),
2866                                       "%*s %*s %*s %s", hostname);
2867                        }
2868                        /*
2869                         * end nvram check
2870                         */
2871
2872                        websWrite(wp, "%c'%s','%s','%s','%d'",
2873                                  (count ? ',' : ' '), hostname, ip, mac,
2874                                  conn_count);
2875                        ++count;
2876                        conn_count = 0;
2877                }
2878                fclose(f);
2879        }
2880}
2881
2882#ifdef HAVE_PPPOESERVER
2883
2884void ej_dumppppoe(webs_t wp, int argc, char_t ** argv)
2885{
2886        FILE *in = fopen("/tmp/pppoe_connected", "rb");
2887        if (!in)
2888                return;
2889        char pid[32];
2890        char ifname[32];
2891        char local[32];
2892        char remote[32];
2893        char peer[64];
2894        int count = 0;
2895        while (fscanf(in, "%s %s %s %s", pid, ifname, local, peer) == 4) {
2896                websWrite(wp, "%c\"%s\",\"%s\",\"%s\",\"%s\"",
2897                          count ? ',' : ' ', ifname, peer, local, pid);
2898                count++;
2899                if (feof(in))
2900                        break;
2901        }
2902
2903        fclose(in);
2904        return;
2905}
2906
2907#endif
2908
2909#ifdef HAVE_EOP_TUNNEL
2910
2911void ej_show_eop_tunnels(webs_t wp, int argc, char_t ** argv)
2912{
2913
2914        int tun;
2915        char temp[32];
2916
2917        for (tun = 1; tun < 11; tun++) {
2918
2919                websWrite(wp, "<fieldset>\n");
2920                websWrite(wp,
2921                          "<legend><script type=\"text/javascript\">Capture(eoip.tunnel)</script> %d</legend>\n",
2922                          tun);
2923                websWrite(wp, "<div class=\"setting\">\n");
2924                websWrite(wp,
2925                          "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.srv)</script></div>\n");
2926                sprintf(temp, "oet%d_en", tun);
2927                websWrite(wp,
2928                          "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idoet%d', true)\" /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2929                          temp,
2930                          (nvram_match(temp, "1") ? "checked=\"checked\"" : ""),
2931                          tun);
2932                websWrite(wp,
2933                          "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idoet%d', false)\" /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2934                          temp,
2935                          (nvram_match(temp, "0") ? "checked=\"checked\"" : ""),
2936                          tun);
2937                websWrite(wp, "</div>\n");
2938                websWrite(wp, "<div id=\"idoet%d\">\n", tun);
2939                websWrite(wp, "<div class=\"setting\">\n");
2940                websWrite(wp,
2941                          "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.remoteIP)</script></div>\n");
2942                websWrite(wp,
2943                          "<input type=\"hidden\" name=\"oet%d_rem\" value=\"0.0.0.0\"/>\n",
2944                          tun);
2945                sprintf(temp, "oet%d_rem", tun);
2946                websWrite(wp,
2947                          "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,eoip.remoteIP)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,eoip.tunnelID)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,eoip.tunnelID)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,1,254,eoip.tunnelID)\" value=\"%d\" />\n",
2948                          temp, get_single_ip(nvram_safe_get(temp), 0), temp,
2949                          get_single_ip(nvram_safe_get(temp), 1), temp,
2950                          get_single_ip(nvram_safe_get(temp), 2), temp,
2951                          get_single_ip(nvram_safe_get(temp), 3));
2952                websWrite(wp, "</div>\n");
2953/*
2954        websWrite( wp, "<div class=\"setting\">\n" );
2955        websWrite( wp,
2956                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.tunnelID)</script></div>\n" );
2957                        sprintf( temp, "oet%d_id", tun );
2958        websWrite( wp,
2959                   "<input size=\"4\" maxlength=\"3\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,999,eoip.tunnelID)\" value=\"%s\" />\n",
2960                   temp, nvram_get( temp ) );
2961        websWrite( wp, "</div>\n" );
2962
2963        websWrite( wp, "<div class=\"setting\">\n" );
2964        websWrite( wp,
2965                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.comp)</script></div>\n" );
2966        sprintf( temp, "oet%d_comp", tun );
2967        websWrite( wp,
2968                   "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2969                   temp,
2970                   ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
2971                     "" ) );
2972        websWrite( wp,
2973                   "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2974                   temp,
2975                   ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
2976                     "" ) );
2977        websWrite( wp, "</div>\n" );
2978        websWrite( wp, "<div class=\"setting\">\n" );
2979        websWrite( wp,
2980                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.passtos)</script></div>\n" );
2981        sprintf( temp, "oet%d_pt", tun );
2982        websWrite( wp,
2983                   "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
2984                   temp,
2985                   ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
2986                     "" ) );
2987        websWrite( wp,
2988                   "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
2989                   temp,
2990                   ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
2991                     "" ) );
2992        websWrite( wp, "</div>\n" );
2993        websWrite( wp, "<div class=\"setting\">\n" );
2994        websWrite( wp,
2995                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.frag)</script></div>\n" );
2996        sprintf( temp, "oet%d_fragment", tun );
2997        websWrite( wp,
2998                   "<input size=\"4\" maxlength=\"4\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,1500,eoip.frag)\" value=\"%s\" />\n",
2999                   temp, nvram_get( temp ) );
3000        websWrite( wp, "</div>\n" );
3001        websWrite( wp, "<div class=\"setting\">\n" );
3002        websWrite( wp,
3003                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.mssfix)</script></div>\n" );
3004        sprintf( temp, "oet%d_mssfix", tun );
3005        websWrite( wp,
3006                   "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
3007                   temp,
3008                   ( nvram_match( temp, "1" ) ? "checked=\"checked\"" :
3009                     "" ) );
3010        websWrite( wp,
3011                   "<input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
3012                   temp,
3013                   ( nvram_match( temp, "0" ) ? "checked=\"checked\"" :
3014                     "" ) );
3015        websWrite( wp, "</div>\n" );
3016        websWrite( wp, "<div class=\"setting\">\n" );
3017        websWrite( wp,
3018                   "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.shaper)</script></div>\n" );
3019        sprintf( temp, "oet%d_shaper", tun );
3020        websWrite( wp,
3021                   "<input size=\"6\" maxlength=\"6\" class=\"num\" name=\"%s\" onblur=\"valid_range(this,0,100000,eoip.shaper)\" value=\"%s\" />\n",
3022                   temp, nvram_get( temp ) );
3023        websWrite( wp, "</div>\n" );
3024*/
3025                websWrite(wp, "<div class=\"setting\">\n");
3026                websWrite(wp,
3027                          "<div class=\"label\"><script type=\"text/javascript\">Capture(eoip.bridging)</script></div>\n");
3028                sprintf(temp, "oet%d_bridged", tun);
3029                websWrite(wp,
3030                          "<input class=\"spaceradio\" type=\"radio\" value=\"1\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idbridged%d', false)\" /><script type=\"text/javascript\">Capture(share.enable)</script>&nbsp;\n",
3031                          temp,
3032                          (nvram_match(temp, "1") ? "checked=\"checked\"" : ""),
3033                          tun);
3034                websWrite(wp,
3035                          " <input class=\"spaceradio\" type=\"radio\" value=\"0\" name=\"%s\" %s onclick=\"show_layer_ext(this, 'idbridged%d', true)\" /><script type=\"text/javascript\">Capture(share.disable)</script>\n",
3036                          temp,
3037                          (nvram_match(temp, "0") ? "checked=\"checked\"" : ""),
3038                          tun);
3039                websWrite(wp, "</div>\n");
3040                websWrite(wp, "<div id=\"idbridged%d\">\n", tun);
3041                websWrite(wp, "<div class=\"setting\">\n");
3042                websWrite(wp,
3043                          "<div class=\"label\"><script type=\"text/javascript\">Capture(share.ip)</script></div>\n");
3044                websWrite(wp,
3045                          "<input type=\"hidden\" name=\"oet%d_ipaddr\" value=\"0.0.0.0\"/>\n",
3046                          tun);
3047                sprintf(temp, "oet%d_ipaddr", tun);
3048                websWrite(wp,
3049                          "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,share.ip)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,1,254,share.ip)\" value=\"%d\" />\n",
3050                          temp, get_single_ip(nvram_safe_get(temp), 0), temp,
3051                          get_single_ip(nvram_safe_get(temp), 1), temp,
3052                          get_single_ip(nvram_safe_get(temp), 2), temp,
3053                          get_single_ip(nvram_safe_get(temp), 3));
3054                websWrite(wp, "</div>\n");
3055                websWrite(wp, "<div class=\"setting\">\n");
3056                websWrite(wp,
3057                          "<div class=\"label\"><script type=\"text/javascript\">Capture(share.subnet)</script></div>\n");
3058                websWrite(wp,
3059                          "<input type=\"hidden\" name=\"oet%d_netmask\" value=\"0.0.0.0\"/>\n",
3060                          tun);
3061                sprintf(temp, "oet%d_netmask", tun);
3062                websWrite(wp,
3063                          "<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_0\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_1\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_2\" onblur=\"valid_range(this,0,255,share.subnet)\" value=\"%d\" />.<input size=\"3\" maxlength=\"3\" class=\"num\" name=\"%s_3\" onblur=\"valid_range(this,0,254,share.subnet)\" value=\"%d\" />\n",
3064                          temp, get_single_ip(nvram_safe_get(temp), 0), temp,
3065                          get_single_ip(nvram_safe_get(temp), 1), temp,
3066                          get_single_ip(nvram_safe_get(temp), 2), temp,
3067                          get_single_ip(nvram_safe_get(temp), 3));
3068                websWrite(wp, "</div>\n");
3069                websWrite(wp, "</div>\n");
3070                websWrite(wp, "</div>\n");
3071                websWrite(wp, "</fieldset><br/>\n");
3072        }
3073}
3074
3075#endif
3076
3077// web-writes html escaped (&#xxx;) nvram entries / test buffered
3078int tf_webWriteESCNV(webs_t wp, const char *nvname)
3079{
3080        char buf[512];
3081        int n;
3082        int r;
3083        const char *c;
3084
3085        n = 0;
3086        r = 0;
3087        for (c = nvram_safe_get(nvname); *c; c++) {
3088                if ((isprint(*c)) && (*c != '"') && (*c != '&')
3089                    && (*c != '<') && (*c != '>') && (*c != '\'')
3090                    && (*c != '\\')) {
3091                        buf[n++] = *c;
3092                } else {
3093                        sprintf(buf + n, "&#%d;", *c);
3094                        n += strlen(buf + n);
3095                }
3096                if (n > (sizeof(buf) - 10)) {   // ! extra space for &...
3097                        buf[n] = 0;
3098                        n = 0;
3099                        r += wfputs(buf, wp);
3100                }
3101        }
3102        if (n > 0) {
3103                buf[n] = 0;
3104                r += wfputs(buf, wp);
3105        }
3106        wfflush(wp);
3107        return r;
3108}
3109
3110int tf_webWriteJS(webs_t wp, const char *s)
3111{
3112        char buf[512];
3113        int n;
3114        int r;
3115
3116        n = 0;
3117        r = 0;
3118        for (; *s; s++) {
3119                if (*s == '<') {
3120                        sprintf(buf + n, "&lt;");
3121                        n += 4;
3122                } else if (*s == '>') {
3123                        sprintf(buf + n, "&gt;");
3124                        n += 4;
3125                } else if ((*s != '"') && (*s != '\\') && (*s != '/')
3126                           && (*s != '*') && (*s != '\'') && (isprint(*s))) {
3127                        buf[n++] = *s;
3128                } else {
3129                        sprintf(buf + n, "\\x%02x", *s);
3130                        n += 4;
3131                }
3132                if (n > (sizeof(buf) - 10)) {   // ! extra space for \xHH
3133                        buf[n] = 0;
3134                        n = 0;
3135                        r += wfputs(buf, wp);
3136                }
3137        }
3138        if (n > 0) {
3139                buf[n] = 0;
3140                r += wfputs(buf, wp);
3141        }
3142        wfflush(wp);
3143        return r;
3144}
3145
3146#ifdef HAVE_SAMBA_SERVER
3147struct fsentry {
3148        char fs[32];
3149        char mp[64];
3150        char fstype[16];
3151        char perms[4];
3152        struct fsentry *next;
3153};
3154
3155struct fsentry *parsefsentry(char line[256])
3156{
3157
3158        struct fsentry *entry = calloc(1, sizeof(struct fsentry));
3159        char *token, *perm;
3160        int tokcount = 0;
3161
3162        line[strlen(line) - 1] = '\0';  // replace new line with null
3163        token = strtok(line, " ");
3164        while (token != NULL) {
3165                // check for values
3166                if (tokcount == 0) {
3167                        if (!strncmp(token, "/tmp/mnt/", 9)) {
3168                                char newpath[64];
3169                                strcpy(newpath, token);
3170                                char *slash = strrchr(newpath, '/') + 1;
3171                                sprintf(entry->fs, "/mnt/%s", slash);   // convert symbolic link to absolute path
3172                        } else {
3173                                strcpy(entry->fs, token);
3174                        }
3175                } else if (tokcount == 2) {
3176                        strcpy(entry->mp, token);
3177                } else if (tokcount == 4) {
3178                        strcpy(entry->fstype, token);
3179                } else if (tokcount == 5) {
3180                        perm = strtok(token, ",");
3181                        strncpy(entry->perms, perm + 1, strlen(perm) - 1);
3182                }
3183                // next token
3184                token = strtok(NULL, " ");
3185                tokcount++;
3186        }
3187        return entry;
3188}
3189
3190struct fsentry *getfsentries()
3191{
3192
3193        char line[256];
3194        FILE *fp;
3195        struct fsentry *list, *current;
3196        int count = 0;
3197
3198        if ((fp = popen("mount", "r"))) {
3199                //current = list;
3200                while (fgets(line, sizeof(line), fp)) {
3201                        //fprintf(stderr, "[MOUNTS] %s\n", line);
3202                        if (count == 0) {
3203                                list = parsefsentry(line);
3204                                current = list;
3205                        } else {
3206                                current->next = parsefsentry(line);
3207                                current = current->next;
3208                        }
3209                        count++;
3210                }
3211                pclose(fp);
3212        }
3213        return list;
3214}
3215
3216#include <samba3.h>
3217
3218void ej_samba3_sharepaths(webs_t wp, int argc, char_t ** argv)
3219{
3220
3221        struct fsentry *fs, *current;
3222        struct samba3_share *cs, *csnext;
3223        char buffer[64], number[4], perms[16];
3224        int found, rows = 0;
3225
3226        fs = getfsentries();
3227        struct samba3_share *samba3shares = getsamba3shares();
3228
3229        // share count var
3230        for (cs = samba3shares; cs; cs = cs->next) {
3231                rows++;
3232        }
3233        rows--;
3234        websWrite(wp,
3235                  "     <input type=\"hidden\" name=\"samba_shares_count\" id=\"samba_shares_count\" value=\"%d\">\n",
3236                  rows);
3237        rows = 5;
3238
3239        websWrite(wp,
3240                  "     <input type=\"hidden\" name=\"samba_shares_count_limit\" id=\"samba_shares_count_limit\" value=\"%d\">\n",
3241                  rows);
3242        rows = 0;
3243
3244        // table header
3245        websWrite(wp,
3246                  "     <table id=\"samba_shares\" class=\"table center\" summary=\"samba share table\">\n");
3247        websWrite(wp,
3248                  "             <tr><th colspan=\"5\"><script type=\"text/javascript\">Capture(service.samba3_shares)</script></th></tr>\n");
3249        websWrite(wp, "         <tr>\n");
3250        websWrite(wp,
3251                  "                     <th><script type=\"text/javascript\">Capture(service.samba3_share_path)</script></th>\n");
3252        websWrite(wp,
3253                  "                     <th><script type=\"text/javascript\">Capture(service.samba3_share_label)</script></th>\n");
3254        websWrite(wp,
3255                  "                     <th><script type=\"text/javascript\">Capture(service.samba3_share_public)</script></th>\n");
3256        websWrite(wp,
3257                  "                     <th><script type=\"text/javascript\">Capture(service.samba3_share_access)</script></th>\n");
3258        websWrite(wp,
3259                  "                     <th style=\"width: 50px;\">&nbsp;</th>\n");
3260        websWrite(wp, "         </tr>\n");
3261
3262        for (cs = samba3shares; cs; cs = csnext) {
3263
3264                if (rows == 0) {
3265                        // dummy entry
3266                        sprintf(buffer, "%s",
3267                                "id=\"samba_shares_row_template\" style=\"display: none;\"");
3268                        //sprintf(number, '\0');
3269                        number[0] = '\0';
3270                } else {
3271                        sprintf(buffer, "id=\"samba_shares_row_%d\"", rows);
3272                        sprintf(number, "_%d", rows);
3273                }
3274
3275                websWrite(wp, "         <tr %s>\n", buffer);
3276
3277                // display filesystems to mount
3278                found = 0;
3279                //sprintf( perms, "");
3280                perms[0] = '\0';
3281                websWrite(wp,
3282                          "                     <td id=\"n_share_mp%s\" style=\"width: 17.816em;\"><select name=\"smbshare_mp%s\" id=\"smbshare_mp%s\" style=\"width: 100%%;\" onchange=\"setSambaShareAccessOptions(this);\">\n",
3283                          number, number, number);
3284                websWrite(wp,
3285                          "                             <option value=\"\" rel=\"\">-</option>\n");
3286                //fprintf(stderr, "[SAMBA] FS %s:%s public:%d\n", cs->label, cs->mp, cs->public );
3287                for (current = fs; current; current = current->next) {
3288                        if (strcmp(current->fstype, "squashfs")
3289                            && strcmp(current->fstype, "rootfs")
3290                            && strcmp(current->fstype, "proc")
3291                            && strcmp(current->fstype, "sysfs")
3292                            && strcmp(current->fstype, "sysdebug")
3293                            && strcmp(current->fstype, "debugfs")
3294                            && strcmp(current->fstype, "ramfs")
3295                            && strcmp(current->fstype, "tmpfs")
3296                            && strcmp(current->fstype, "devpts")
3297                            && strcmp(current->fstype, "usbfs")) {
3298                                // adjust the rights
3299                                if ( /*rows == 0 || */ !strcmp(current->mp, "")) {
3300                                        sprintf(buffer, "%s", "");
3301                                } else if (!strcmp(current->perms, "rw")) {
3302                                        sprintf(buffer, "%s", "\"rw\",\"ro\"");
3303                                } else {
3304                                        sprintf(buffer, "%s", "\"ro\"");
3305                                }
3306
3307                                if (!strcmp(current->mp, cs->mp)) {
3308                                        found = 1;
3309                                        sprintf(perms, "%s", current->perms);
3310                                }
3311
3312                                websWrite(wp,
3313                                          "                             <option value=\"%s\" rel='{\"fstype\":\"%s\",\"perms\":[%s],\"avail\":1}' %s>%s</option>\n",
3314                                          current->mp,
3315                                          current->fstype,
3316                                          buffer,
3317                                          strcmp
3318                                          (current->mp,
3319                                           cs->mp) ? "" :
3320                                          "selected=\"selected\"", current->mp);
3321                        }
3322                }
3323                // fs not available -> add stored entry for display
3324                if (found == 0 && rows > 0) {
3325                        websWrite(wp,
3326                                  "                             <option value=\"%s\" rel='{\"fstype\":\"\",\"perms\":[\"%s\"],\"avail\":0}' selected>%s [not available!]</option>\n",
3327                                  cs->mp, cs->access_perms, cs->mp);
3328                        sprintf(perms, "%s", cs->access_perms);
3329                }
3330                websWrite(wp, "                         </select></td>\n");
3331                websWrite(wp,
3332                          "                             <td style=\"width: 1%%;\"><input type=\"text\" name=\"smbshare_label%s\" id=\"smbshare_label%s\" value=\"%s\" style=\"width: 150px;\" onChange=\"updateSambaUserShare(this);\" /></td>\n",
3333                          number, number, cs->label);
3334                websWrite(wp,
3335                          "                             <td style=\"width: 25px; text-align: center;\"><input type=\"checkbox\" name=\"smbshare_public%s\" id=\"smbshare_public%s\" value=\"1\" %s></td>\n",
3336                          number, number, cs->public == 1 ? "checked" : "");
3337                websWrite(wp, "                         <td>\n");
3338                websWrite(wp,
3339                          "                                     <select name=\"smbshare_access_perms%s\" id=\"smbshare_access_perms%s\" style=\"width: 100%%;\"%s>\n",
3340                          number, number,
3341                          !strcmp(perms, "") ? " disabled" : "");
3342                if (rows == 0 || strcmp(perms, "")) {
3343                        websWrite(wp,
3344                                  "                                             <option value=\"rw\"%s>Read/Write</option>\n",
3345                                  !strcmp
3346                                  (cs->access_perms, "rw") ? " selected" : "");
3347                        websWrite(wp,
3348                                  "                                             <option value=\"ro\"%s>Read Only</option>\n",
3349                                  !strcmp
3350                                  (cs->access_perms, "ro") ? " selected" : "");
3351                }
3352                websWrite(wp, "                                 </select>\n");
3353                websWrite(wp,
3354                          "                                     <input type=\"hidden\" name=\"smbshare_access_perms_prev_%d\" value=\"%s\">\n",
3355                          rows, cs->access_perms);
3356                websWrite(wp,
3357                          "                             <td style=\"width: 50px; text-align: center;\">\n");
3358                websWrite(wp,
3359                          "                                     <input type=\"button\" class=\"button\" name=\"smbshare_del%s\" value=\"Remove\"  style=\"width: 100%%;\" onclick=\"removeSambaShare(this);\">\n",
3360                          number);
3361                websWrite(wp, "                         </td>\n");
3362                websWrite(wp, "                 </tr>\n");
3363
3364                rows++;
3365                csnext = cs->next;
3366                free(cs);
3367        }
3368
3369        websWrite(wp, "         </table>\n");
3370
3371        // add button
3372        websWrite(wp,
3373                  "<div id=\"samba_shares_add\" style=\"text-align: center;\"><input type=\"button\" class=\"button\" name=\"share_add\" value=\"Add Share\" onclick=\"addSambaShare();\" /></div>");
3374
3375        for (current = fs; fs; current = fs) {
3376                fs = current->next;
3377                free(current);
3378        }
3379}
3380
3381void ej_samba3_users(webs_t wp, int argc, char_t ** argv)
3382{
3383
3384        struct samba3_share *cs, *csnext;
3385        struct samba3_shareuser *csu, *csunext;
3386        struct samba3_user *samba3users, *cu, *cunext;
3387        char buffer[64], number[4];
3388        int rows = 0, usershares = 0;
3389
3390        samba3users = getsamba3users();
3391        struct samba3_share *samba3shares = getsamba3shares();
3392
3393        // share count var
3394        for (cu = samba3users; cu; cu = cu->next) {
3395                rows++;
3396        }
3397        rows--;
3398        websWrite(wp,
3399                  "     <input type=\"hidden\" name=\"samba_users_count\" id=\"samba_users_count\" value=\"%d\">\n",
3400                  rows);
3401        rows = 10;
3402
3403        websWrite(wp,
3404                  "     <input type=\"hidden\" name=\"samba_users_count_limit\" id=\"samba_users_count_limit\" value=\"%d\">\n",
3405                  rows);
3406        rows = 0;
3407
3408        // table header
3409        websWrite(wp,
3410                  "     <table id=\"samba_users\" class=\"table center\" summary=\"samba user table\">\n");
3411
3412        websWrite(wp,
3413                  "             <tr><th colspan=\"4\"><script type=\"text/javascript\">Capture(service.samba3_users)</script></th></tr>\n");
3414        websWrite(wp, "         <tr>\n");
3415        websWrite(wp, "                 <th>username</th>\n");
3416        websWrite(wp,
3417                  "                     <th style=\"width:180px;\">password</th>\n");
3418        websWrite(wp,
3419                  "                     <th><script type=\"text/javascript\">Capture(service.samba3_user_shares)</script></th>\n");
3420        websWrite(wp,
3421                  "                     <th style=\"width:50px;\">&nbsp;</th>\n");
3422        websWrite(wp, "         </tr>\n");
3423
3424        for (cu = samba3users; cu; cu = cunext) {
3425
3426                if (rows == 0) {
3427                        // dummy entry
3428                        sprintf(buffer, "%s",
3429                                "id=\"n_smbuser_template\" style=\"display: none;\"");
3430                        //sprintf(number, '\0');
3431                        number[0] = '\0';
3432                } else {
3433                        sprintf(buffer, "id=\"samba_users_row_%d\"", rows);
3434                        sprintf(number, "_%d", rows);
3435                }
3436
3437                websWrite(wp, "         <tr %s>\n", buffer);
3438
3439                websWrite(wp,
3440                          "                     <td id=\"n_smbuser_user\" valign=\"top\" width=\"1%%\" align=\"center\">\n");
3441                websWrite(wp,
3442                          "                             <input type=\"text\" name=\"smbuser_username%s\" value=\"%s\" size=\"20\">\n",
3443                          number, cu->username);
3444                websWrite(wp, "                 </td>\n");
3445
3446                websWrite(wp,
3447                          "                     <td id=\"n_smbuser_pass\" valign=\"top\" align=\"left\">\n");
3448                websWrite(wp,
3449                          "                             <input type=\"password\" name=\"smbuser_password%s\" id=\"smbuser_password%s\" value=\"%s\" size=\"12\">&nbsp;\n",
3450                          number, number, cu->password);
3451                //websWrite(wp, "                               <div style=\"float: left;padding-top: 2px;\">\n");
3452                websWrite(wp,
3453                          "                                     <input type=\"checkbox\" name=\"smbuser_password%s_unmask\" value=\"0\" onclick=\"setElementMask('smbuser_password%s', this.checked);\" />Unmask\n",
3454                          number, number);
3455                //websWrite(wp, "                               </div>\n");
3456                websWrite(wp, "                 </td>\n");
3457
3458                //fprintf( stderr, "[USERS] %s:%s\n", cu->username, cu->password );
3459                websWrite(wp,
3460                          "                     <td id=\"n_smbuser_shareaccess\" valign=\"top\">\n");
3461                if (rows == 0) {
3462                        websWrite(wp,
3463                                  "                             <div id=\"n_smbuser_share\"><input type=\"checkbox\" value=\"1\">&nbsp;<span>&nbsp</span></div>\n");
3464                } else {
3465                        usershares = 0;
3466                        for (cs = samba3shares; cs; cs = cs->next) {
3467                                buffer[0] = '\0';
3468                                for (csu = cs->users; csu; csu = csu->next) {
3469                                        if (!strcmp
3470                                            (csu->username, cu->username)) {
3471                                                //fprintf( stderr, "[USERSHARES] %s: %s\n", cs->label, csu->username );
3472                                                sprintf(buffer, " checked");
3473                                        }
3474                                }
3475                                if (usershares > 0) {
3476                                        websWrite(wp,
3477                                                  "                             <div id=\"n_smbuser_share\"><input type=\"checkbox\" name=\"smbshare_%d_user_%d\"%s value=\"1\">&nbsp;<span>%s</span></div>\n",
3478                                                  usershares,
3479                                                  rows, buffer, cs->label);
3480                                }
3481                                usershares++;
3482                        }
3483                }
3484                websWrite(wp, "                 </td>\n");
3485
3486                websWrite(wp,
3487                          "                     <td valign=\"top\" style=\"width: 50px; text-align: center;\">\n");
3488                websWrite(wp,
3489                          "                             <input type=\"button\" class=\"button\" name=\"smbuser_del%s\" value=\"Remove\" style=\"width: 100%%;\" onclick=\"removeTableEntry('samba_users', this);\">\n",
3490                          number);
3491                websWrite(wp, "                 </td>\n");
3492
3493                websWrite(wp, "         </tr>\n");
3494                rows++;
3495
3496                cunext = cu->next;
3497                free(cu);
3498        }
3499        for (cs = samba3shares; cs; cs = csnext) {
3500                for (csu = cs->users; csu; csu = csunext) {
3501                        csunext = csu->next;
3502                        free(csu);
3503                }
3504                csnext = cs->next;
3505                free(csnext);
3506        }
3507
3508        websWrite(wp, "         </table>\n");
3509
3510        // add button
3511        websWrite(wp,
3512                  "<div id=\"samba_users_add\" style=\"text-align: center;\"><input type=\"button\" class=\"button\" name=\"user_add\" value=\"Add User\" onclick=\"addSambaUser();\" /></div>");
3513
3514        // free memory
3515
3516}
3517#endif
3518
3519#ifdef HAVE_UPNP
3520// changed by steve
3521// writes javascript-string safe text
3522
3523// <% tf_upnp(); %>
3524// returns all "forward_port#" nvram entries containing upnp port forwardings
3525void ej_tf_upnp(webs_t wp, int argc, char_t ** argv)
3526{
3527        int i;
3528        int len, pos, count;
3529        char *temp;
3530
3531        if (nvram_match("upnp_enable", "1")) {
3532                for (i = 0; i < 50; i++) {
3533                        websWrite(wp, (i > 0) ? ",'" : "'");
3534
3535                        // fix: some entries are missing the desc. - this breaks the
3536                        // upnp.asp page, so we add ,*
3537                        temp = nvram_nget("forward_port%d", i);
3538                        count = 0;
3539                        len = strlen(temp);
3540
3541                        for (pos = len; pos != 0; pos--) {
3542                                if (temp[pos] == ',')
3543                                        count++;
3544                        }
3545
3546                        tf_webWriteJS(wp, temp);
3547                        if (count == 2)
3548                                websWrite(wp, ",*");
3549
3550                        websWrite(wp, "'");
3551                }
3552        }
3553}
3554
3555// end changed by steve
3556#endif
3557
3558//extern void show_onlineupdates(webs_t wp, int argc, char_t ** argv);
3559
3560void ej_show_upgrade_options(webs_t wp, int argc, char_t ** argv)
3561{
3562#ifdef HAVE_BUFFALO
3563        show_onlineupdates(wp, argc, argv);
3564#endif
3565}
3566
3567extern char *request_url;
3568void ej_getsetuppage(webs_t wp, int argc, char_t ** argv)
3569{
3570#ifdef HAVE_BUFFALO
3571        if(endswith(request_url, ".asp") || endswith(request_url, ".htm") || endswith(request_url, ".html")) {
3572fprintf(stderr, "[EJS] request_url: %s", request_url);
3573                websWrite(wp, "%s", request_url);
3574        } else {
3575fprintf(stderr, "[EJS] request_url: %s => SetupAssistant.aps", request_url);
3576                websWrite(wp, "SetupAssistant.asp");
3577        }
3578#else
3579                websWrite(wp, "Info.htm");
3580#endif
3581}
3582
3583#ifdef HAVE_SPOTPASS
3584void ej_spotpass_servers(webs_t wp, int argc, char_t ** argv)
3585{
3586        char url[128], proto[8], ports[64];
3587        char dummy1[1], dummy2[8];
3588        int port1, port2;
3589        char *ptr;
3590        char *serverlist = (char *)
3591            safe_malloc(strlen(nvram_default_get("spotpass_servers", "")) + 1);
3592
3593        strcpy(serverlist, nvram_get("spotpass_servers"));
3594        ptr = strtok(serverlist, "|");
3595        while (ptr != NULL) {
3596                if (sscanf
3597                    (ptr, "%s %s %s %s %d %d", &dummy1, &url, &proto, &dummy2,
3598                     &port1, &port2) == 6) {
3599                        websWrite(wp, "%s %s %d,%d", url, proto, port1, port2);
3600                } else
3601                    if (sscanf
3602                        (ptr, "%s %s %s %d %d", &dummy1, &url, &proto, &port1,
3603                         &port2) == 5) {
3604                        websWrite(wp, "%s %s %d,%d", url, proto, port1, port2);
3605                } else if (sscanf(ptr, "%s %s %s", &url, &proto, &ports) == 3) {
3606                        websWrite(wp, "%s %s %s", url, proto, ports);
3607                }
3608                ptr = strtok(NULL, "|");
3609                if (ptr != NULL) {
3610                        websWrite(wp, "\n");
3611                }
3612        }
3613}
3614#endif
Note: See TracBrowser for help on using the repository browser.