This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
/
web.c
15800 lines (13628 loc) · 420 KB
/
web.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/*
* ASUS Home Gateway Reference Design
* Web Page Configuration Support Routines
*
* Copyright 2001, ASUSTeK Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of ASUSTeK Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of ASUSTeK Inc..
*
* $Id: web_ex.c,v 1.4 2007/04/09 12:01:50 shinjung Exp $
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <httpd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/klog.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <dirent.h>
#include <proto/ethernet.h> //add by Viz 2010.08
#include <net/route.h>
#include <sys/ioctl.h>
#include <math.h> //for ceil()
#include <typedefs.h>
#include <bcmutils.h>
#include <shutils.h>
#include <bcmnvram.h>
#include <bcmnvram_f.h>
#include <common.h>
#include <shared.h>
#include <rtstate.h>
#include "iptraffic.h"
#include <sys/sysinfo.h>
#ifdef RTCONFIG_FANCTRL
#include <wlutils.h>
#endif
#ifdef RTCONFIG_DSL
#include <web-dsl.h>
#include <web-dsl-upg.h>
#endif
#ifdef RTCONFIG_USB
#include <usb_info.h>
#include <disk_io_tools.h>
#include <disk_initial.h>
#include <disk_share.h>
#ifdef RTCONFIG_NOTIFICATION_CENTER
#include <libnt.h>
#endif
#include <apps.h>
#ifdef RTCONFIG_USB_SMS_MODEM
#include "libsmspdu.h"
#endif
#else
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
#endif
#include "initial_web_hook.h"
//#endif
//#ifdef RTCONFIG_OPENVPN
#include "openvpn_options.h"
//#endif
#include <net/if.h>
#include <linux/sockios.h>
#include "../networkmap/networkmap.h"
//#include <networkmap.h> //2011.03 Yau add for new networkmap
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sysinfo.h>
#include "sysinfo.h"
#include "data_arrays.h"
#ifdef RTCONFIG_QTN
#include "web-qtn.h"
#endif
#ifdef RTCONFIG_BWDPI
#include "bwdpi.h"
#include "sqlite3.h"
#include "bwdpi_sqlite.h"
#endif
#ifdef RTCONFIG_TRAFFIC_LIMITER
#include "traffic_limiter.h"
#endif
#include <json.h>
#ifdef RTCONFIG_QCA_PLC_UTILS
#include <plc_utils.h>
#endif
#ifdef RTCONFIG_HTTPS
extern int do_ssl;
extern int ssl_stream_fd;
#endif
extern int ej_wl_sta_list_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_sta_list_5g(int eid, webs_t wp, int argc, char_t **argv);
#ifndef RTCONFIG_QTN
extern int ej_wl_sta_list_5g_2(int eid, webs_t wp, int argc, char_t **argv);
#endif
#ifdef RTCONFIG_STAINFO
extern int ej_wl_stainfo_list_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_stainfo_list_5g(int eid, webs_t wp, int argc, char_t **argv);
#ifndef RTCONFIG_QTN
extern int ej_wl_stainfo_list_5g_2(int eid, webs_t wp, int argc, char_t **argv);
#endif
#endif
extern int ej_wl_auth_list(int eid, webs_t wp, int argc, char_t **argv);
#if defined(CONFIG_BCMWL5) || defined(RTCONFIG_QCA)
extern int ej_wl_control_channel(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_extent_channel(int eid, webs_t wp, int argc, char_t **argv);
#endif
#ifdef RTCONFIG_RALINK
#elif defined(RTCONFIG_QCA)
#else
extern int ej_SiteSurvey(int eid, webs_t wp, int argc, char_t **argv);
#endif
extern int ej_wl_scan_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_scan_5g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_scan_5g_2(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_channel_list_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_channel_list_5g(int eid, webs_t wp, int argc, char_t **argv);
#ifdef RTCONFIG_QTN
extern int ej_wl_channel_list_5g_20m(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_channel_list_5g_40m(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_channel_list_5g_80m(int eid, webs_t wp, int argc, char_t **argv);
#endif
extern int ej_wl_channel_list_5g_2(int eid, webs_t wp, int argc, char_t **argv);
#ifdef CONFIG_BCMWL5
extern int ej_wl_chanspecs_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_chanspecs_5g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_chanspecs_5g_2(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_rssi_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_rssi_5g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_rssi_5g_2(int eid, webs_t wp, int argc, char_t **argv);
#endif
#if defined(CONFIG_BCMWL5) || (defined(RTCONFIG_RALINK) && defined(RTCONFIG_WIRELESSREPEATER)) || defined(RTCONFIG_QCA)
extern int ej_wl_rate_2g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_rate_5g(int eid, webs_t wp, int argc, char_t **argv);
extern int ej_wl_rate_5g_2(int eid, webs_t wp, int argc, char_t **argv);
#endif
#ifdef RTCONFIG_PROXYSTA
int ej_wl_auth_psta(int eid, webs_t wp, int argc, char_t **argv);
#endif
#ifdef RTCONFIG_IPV6
extern int ej_lan_ipv6_network(int eid, webs_t wp, int argc, char_t **argv);
#endif
extern int ej_get_default_reboot_time(int eid, webs_t wp, int argc, char_t **argv);
static int b64_decode( const char* str, unsigned char* space, int size );
extern void send_login_page(int fromapp_flag, int error_status, char* url, char* file, int lock_time);
extern void __send_login_page(int fromapp_flag, int error_status, char* url, char* file, int lock_time);
extern char *get_cgi_json(char *name, json_object *root);
extern int ej_generate_region(int eid, webs_t wp, int argc, char_t **argv);
void add_asus_token(char *token);
#define wan_prefix(unit, prefix) snprintf(prefix, sizeof(prefix), "wan%d_", unit)
#define nvram_default_safe_get(name) (nvram_default_get(name) ? : "")
/*
#define csprintf(fmt, args...) do{\
FILE *cp = fopen("/dev/console", "w");\
if (cp) {\
fprintf(cp, fmt, ## args);\
fclose(cp);\
}\
}while (0)
*/
// 2008.08 magic }
#include <sys/mman.h>
typedef uint32_t __u32; //2008.08 magic
#ifndef O_BINARY /* should be define'd on __WIN32__ */
#define O_BINARY 0
#endif
#ifndef MAP_FAILED
#define MAP_FAILED (-1)
#endif
/* #define sys_upgrade(image) eval("mtd-write", "-i", image, "-d", "linux"); */
#define sys_upload(image) eval("nvram", "restore", image)
#define sys_download(file) eval("nvram", "save", file)
#define sys_default() notify_rc("resetdefault"); // eval("mtd-erase", "-d", "nvram")
#define sys_reboot() notify_rc("reboot");
#define PROFILE_HEADER "HDR1"
#ifdef RTCONFIG_DSL
#define PROFILE_HEADER_NEW "N55U"
#else
#if RTCONFIG_QCA
#define PROFILE_HEADER_NEW "AC55U"
#else
#define PROFILE_HEADER_NEW "HDR2"
#endif
#endif
#define IH_MAGIC 0x27051956 /* Image Magic Number */
int count_sddev_mountpoint();
#define Ralink_WPS 1 //2009.01 magic
char ibuf2[8192];
//static int ezc_error = 0;
#define ACTION_UPGRADE_OK 0
#define ACTION_UPGRADE_FAIL 1
int action;
char *serviceId;
#define MAX_GROUP_ITEM 10
#define MAX_GROUP_COUNT 300
#define MAX_LINE_SIZE 512
char *groupItem[MAX_GROUP_ITEM];
char urlcache[128];
char *next_host;
int delMap[MAX_GROUP_COUNT];
char SystemCmd[128];
char UserID[32]="";
char UserPass[32]="";
char ProductID[32]="";
extern int redirect;
extern int change_passwd; // 2008.08 magic
extern int reget_passwd; // 2008.08 magic
extern int skip_auth;
extern int lock_flag;
extern char host_name[64];
extern char referer_host[64];
extern char current_page_name[128];
extern unsigned int login_ip_tmp;
extern time_t login_timestamp; // the timestamp of the logined ip
extern time_t login_timestamp_tmp; // the timestamp of the current session.
extern time_t last_login_timestamp; // the timestamp of the current session.
extern unsigned int login_try;
extern unsigned int MAX_login;
extern time_t login_dt;
extern char login_url[128];
extern int login_error_status;
extern char cloud_file[256];
#ifdef RTCONFIG_JFFS2USERICON
#define JFFS_USERICON "/jffs/usericon/"
#endif
static void insert_hook_func(webs_t wp, char *fname, char *param)
{
if (!wp || !fname) {
_dprintf("%s: invalid parameter (%p, %p, %p)\n", __func__, wp, fname, param);
return;
}
if (!param)
param = "";
websWrite(wp, "<script>\n");
websWrite(wp, "%s(%s);\n", fname, param);
websWrite(wp, "</script>\n");
}
char *
rfctime(const time_t *timep)
{
static char s[201];
struct tm tm;
//it suppose to be convert after applying
//time_zone_x_mapping();
setenv("TZ", nvram_safe_get_x("", "time_zone_x"), 1);
memcpy(&tm, localtime(timep), sizeof(struct tm));
strftime(s, 200, "%a, %d %b %Y %H:%M:%S %z", &tm);
return s;
}
void
reltime(unsigned int seconds, char *cs)
{
#ifdef SHOWALL
int days=0, hours=0, minutes=0;
if (seconds > 60*60*24) {
days = seconds / (60*60*24);
seconds %= 60*60*24;
}
if (seconds > 60*60) {
hours = seconds / (60*60);
seconds %= 60*60;
}
if (seconds > 60) {
minutes = seconds / 60;
seconds %= 60;
}
sprintf(cs, "%d days, %d hours, %d minutes, %d seconds", days, hours, minutes, seconds);
#else
sprintf(cs, "%d secs", seconds);
#endif
}
/******************************************************************************/
/*
* Redirect the user to another webs page
*/
//2008.08 magic{
void websRedirect(webs_t wp, char_t *url)
{
char url_str[128];
if(check_xss_blacklist(url, 1)){
memset(url_str, 0, sizeof(url_str));
strlcpy(url_str, "index.asp", sizeof(url_str));
}else
strlcpy(url_str, url, sizeof(url_str));
websWrite(wp, T("<html><head>\r\n"));
if(strchr(url, '>') || strchr(url, '<'))
{
websWrite(wp,"<script>parent.location.href='/index.asp';</script>\n");
}
else
{
#ifdef RTCONFIG_HTTPS
if(do_ssl){
//websWrite(wp, T("<meta http-equiv=\"refresh\" content=\"0; url=https://%s/%s\">\r\n"), gethost(), url);
websWrite(wp,"<script>parent.location.href='/%s';</script>\n",url_str);
}else
#endif
{
//websWrite(wp, T("<meta http-equiv=\"refresh\" content=\"0; url=http://%s/%s\">\r\n"), gethost(), url);
websWrite(wp,"<script>parent.location.href='/%s';</script>\n",url_str);
}
}
websWrite(wp, T("<meta http-equiv=\"Content-Type\" content=\"text/html\">\r\n"));
websWrite(wp, T("</head></html>\r\n"));
websDone(wp, 200);
}
void websRedirect_iframe(webs_t wp, char_t *url)
{
char url_str[32];
if(check_xss_blacklist(url, 1)){
memset(url_str, 0, sizeof(url_str));
strlcpy(url_str, "index.asp", sizeof(url_str));
}else
strlcpy(url_str, url, sizeof(url_str));
websWrite(wp, T("<html><head>\r\n"));
if(strchr(url, '>') || strchr(url, '<'))
{
websWrite(wp,"<script>parent.location.href='/index.asp';</script>\n");
}
else
{
#ifdef RTCONFIG_HTTPS
if(do_ssl){
websWrite(wp, T("<meta http-equiv=\"refresh\" content=\"0; url=https://%s/%s\">\r\n"), gethost(), url_str);
}else
#endif
{
websWrite(wp, T("<meta http-equiv=\"refresh\" content=\"0; url=http://%s/%s\">\r\n"), gethost(), url_str);
}
}
websWrite(wp, T("<meta http-equiv=\"Content-Type\" content=\"text/html\">\r\n"));
websWrite(wp, T("</head></html>\r\n"));
websDone(wp, 200);
}
//2008.08 magic}
void sys_script(char *name)
{
char scmd[64];
snprintf(scmd, sizeof(scmd), "/tmp/%s", name);
//printf("run %s %d %s\n", name, strlen(name), scmd); // tmp test
//handle special scirpt first
if (strcmp(name,"syscmd.sh")==0)
{
if (strcmp(SystemCmd, "")!=0)
{
snprintf(SystemCmd, sizeof(SystemCmd), "%s > /tmp/syscmd.log 2>&1 && echo 'XU6J03M6' >> /tmp/syscmd.log &\n", SystemCmd); // oleg patch
system(SystemCmd);
strcpy(SystemCmd,""); // Ensure we don't re-execute it again
}
else
{
f_write_string("/tmp/syscmd.log", "", 0, 0);
}
}
//#ifdef U2EC
else if (strcmp(name,"eject-usb.sh")==0)
{
eval("rmstorage");
}
#ifdef ASUS_DDNS //2007.03.22 Yau add
else if (strcmp(name,"ddnsclient")==0)
{
notify_rc("restart_ddns");
}
else if (strstr(name,"asusddns_register") != NULL)
{
notify_rc("asusddns_reg_domain");
}
#endif
else if (strcmp(name, "leases.sh")==0 || strcmp(name, "dleases.sh")==0)
{
// do nothing
}
else if (strstr(scmd, " ") == 0) // no parameter, run script with eval
{
eval(scmd);
}
else system(scmd);
}
void websScan(char_t *str)
{
unsigned int i, flag;
char_t *v1, *v2, *v3, *sp;
char_t groupid[64];
char_t value[MAX_LINE_SIZE];
char_t name[MAX_LINE_SIZE];
v1 = strchr(str, '?');
i = 0;
flag = 0;
while (v1!=NULL)
{
v2 = strchr(v1+1, '=');
v3 = strchr(v1+1, '&');
// 2008.08 magic {
if (v2 == NULL)
break;
// 2008.08 magic }
if (v3!=NULL)
{
strncpy(value, v2+1, v3-v2-1);
value[v3-v2-1] = 0;
}
else
{
strcpy(value, v2+1);
}
strncpy(name, v1+1, v2-v1-1);
name[v2-v1-1] = 0;
/*printf("Value: %s %s\n", name, value);*/
if (v2 != NULL && ((sp = strchr(v1+1, ' ')) == NULL || (sp > v2)))
{
if (flag && strncmp(v1+1, groupid, strlen(groupid))==0)
{
delMap[i] = atoi(value);
/*printf("Del Scan : %x\n", delMap[i]);*/
if (delMap[i]==-1) break;
i++;
}
else if (strncmp(v1+1,"group_id", 8)==0)
{
sprintf(groupid, "%s_s", value);
flag = 1;
}
}
v1 = strchr(v1+1, '&');
}
delMap[i] = -1;
return;
}
void websApply(webs_t wp, char_t *url)
{
#ifdef TRANSLATE_ON_FLY
do_ej (url, wp);
websDone (wp, 200);
#else // define TRANSLATE_ON_FLY
FILE *fp;
char buf[MAX_LINE_SIZE];
fp = fopen(url, "r");
if (fp==NULL) return;
while (fgets(buf, sizeof(buf), fp))
{
websWrite(wp, buf);
}
websDone(wp, 200);
fclose(fp);
#endif
}
/*
* Example:
* lan_ipaddr=192.168.1.1
* <% nvram_get("lan_ipaddr"); %> produces "192.168.1.1"
* <% nvram_get("undefined"); %> produces ""
*/
static int
ej_nvram_get(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *c;
int ret = 0;
// char sid_dummy = "",
int from_app = 0;
from_app = check_user_agent(user_agent);
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (strcmp(name, "modem_spn") == 0 && !nvram_invmatch(name, ""))
name = "modem_isp";
for (c = nvram_safe_get(name); *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>' && *c != '\\')
ret += websWrite(wp, "%c", *c);
else if(*c == '\\'){
if(from_app != 0)
ret += websWrite(wp, "\\\\");
else
ret += websWrite(wp, "%c", *c);
}
else
ret += websWrite(wp, "&#%d", *c);
}
return ret;
}
/* This will return properly encoded HTML entities - required
for retrieving stored certs/keys.
*/
static int
ej_nvram_clean_get(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *c;
int ret = 0;
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
for (c = nvram_safe_get(name); *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>')
ret += websWrite(wp, "%c", *c);
else
ret += websWrite(wp, "&#%d;", *c);
}
return ret;
}
static int
ej_nvram_default_get(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *c;
int ret = 0;
// char sid_dummy = "",
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
for (c = nvram_default_safe_get(name); *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>')
ret += websWrite(wp, "%c", *c);
else
ret += websWrite(wp, "&#%d", *c);
}
return ret;
}
/*
* Example:
* lan_ipaddr=192.168.1.1
* <% nvram_get_x("lan_ipaddr"); %> produces "192.168.1.1"
* <% nvram_get_x("undefined"); %> produces ""
*/
static int
ej_nvram_get_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *c;
int ret = 0;
if (ejArgs(argc, argv, "%s %s", &sid, &name) < 2) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
for (c = nvram_safe_get_x(sid, name); *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>')
ret += websWrite(wp, "%c", *c);
else
ret += websWrite(wp, "&#%d", *c);
}
return ret;
}
#ifdef ASUS_DDNS
static int
ej_nvram_get_ddns(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *c;
int ret = 0;
if (ejArgs(argc, argv, "%s %s", &sid, &name) < 2) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
for (c = nvram_safe_get_x(sid, name); *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>')
ret += websWrite(wp, "%c", *c);
else
ret += websWrite(wp, "&#%d", *c);
}
if (strcmp(name,"ddns_return_code")==0) {
if(!nvram_match("ddns_return_code", "ddns_query")) {
nvram_set("ddns_return_code","");
}
}
return ret;
}
#endif
/*
* Example:
* lan_ipaddr=192.168.1.1
* <% nvram_get_x("lan_ipaddr"); %> produces "192.168.1.1"
* <% nvram_get_x("undefined"); %> produces ""
*/
static int
ej_nvram_get_f(int eid, webs_t wp, int argc, char_t **argv)
{
char *file, *field, *c, buf[64];
int ret = 0;
if (ejArgs(argc, argv, "%s %s", &file, &field) < 2) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
strcpy(buf, nvram_safe_get_f(file, field));
for (c = buf; *c; c++) {
if (isprint(*c) &&
*c != '"' && *c != '&' && *c != '<' && *c != '>')
ret += websWrite(wp, "%c", *c);
else
ret += websWrite(wp, "&#%d", *c);
}
return ret;
}
static int
ej_nvram_show_chinese_char(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *c;
int ret = 0;
if (ejArgs(argc, argv, "%s", &name) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
for (c = nvram_safe_get(name); *c; c++) {
ret += websWrite(wp, "%c", *c);
}
return ret;
}
/*
* Example:
* wan_proto=dhcp
* <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
* <% nvram_match("wan_proto", "static", "selected"); %> does not produce
*/
static int
ej_nvram_match(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *match, *output;
if (ejArgs(argc, argv, "%s %s %s", &name, &match, &output) < 3) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match(name, match))
{
return websWrite(wp, output);
}
return 0;
}
/*
* Example:
* wan_proto=dhcp
* <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
* <% nvram_match("wan_proto", "static", "selected"); %> does not produce
*/
static int
ej_nvram_match_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *match, *output;
if (ejArgs(argc, argv, "%s %s %s %s", &sid, &name, &match, &output) < 4) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match_x(sid, name, match))
{
return websWrite(wp, output);
}
return 0;
}
static int
ej_nvram_double_match(int eid, webs_t wp, int argc, char_t **argv)
{
char *name, *match, *output;
char *name2, *match2;
if (ejArgs(argc, argv, "%s %s %s %s %s", &name, &match, &name2, &match2, &output) < 5) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match(name, match) && nvram_match(name2, match2))
{
return websWrite(wp, output);
}
return 0;
}
static int
ej_nvram_double_match_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *match, *output;
char *sid2, *name2, *match2;
if (ejArgs(argc, argv, "%s %s %s %s %s %s %s", &sid, &name, &match, &sid2, &name2, &match2, &output) < 7) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match_x(sid, name, match) && nvram_match_x(sid2, name2, match2))
{
return websWrite(wp, output);
}
return 0;
}
/*
* Example:
* wan_proto=dhcp
* <% nvram_match("wan_proto", "dhcp", "selected"); %> produces "selected"
* <% nvram_match("wan_proto", "static", "selected"); %> does not produce
*/
static int
ej_nvram_match_both_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *match, *output, *output_not;
if (ejArgs(argc, argv, "%s %s %s %s %s", &sid, &name, &match, &output, &output_not) < 5)
{
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match_x(sid, name, match))
{
return websWrite(wp, output);
}
else
{
return websWrite(wp, output_not);
}
}
/*
* Example:
* lan_ipaddr=192.168.1.1 192.168.39.248
* <% nvram_get_list("lan_ipaddr", 0); %> produces "192.168.1.1"
* <% nvram_get_list("lan_ipaddr", 1); %> produces "192.168.39.248"
*/
static int
ej_nvram_get_list_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name;
int which;
int ret = 0;
if (ejArgs(argc, argv, "%s %s %d", &sid, &name, &which) < 3) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
ret += websWrite(wp, nvram_get_list_x(sid, name, which));
return ret;
}
/*
* Example:
* lan_ipaddr=192.168.1.1 192.168.39.248
* <% nvram_get_list("lan_ipaddr", 0); %> produces "192.168.1.1"
* <% nvram_get_list("lan_ipaddr", 1); %> produces "192.168.39.248"
*/
static int
ej_nvram_get_buf_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name;
int which;
if (ejArgs(argc, argv, "%s %s %d", &sid, &name, &which) < 3) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
return 0;
}
/*
* Example:
* wan_proto=dhcp;dns
* <% nvram_match_list("wan_proto", "dhcp", "selected", 0); %> produces "selected"
* <% nvram_match_list("wan_proto", "static", "selected", 1); %> does not produce
*/
static int
ej_nvram_match_list_x(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name, *match, *output;
int which;
if (ejArgs(argc, argv, "%s %s %s %s %d", &sid, &name, &match, &output, &which) < 5) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
if (nvram_match_list_x(sid, name, match, which))
return websWrite(wp, output);
else
return 0;
}
static int
ej_select_channel(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, chstr[32];
int ret = 0;
int idx = 0, channel;
char *value = nvram_safe_get("wl0_country_code");
char *channel_s = nvram_safe_get("wl_channel");
if (ejArgs(argc, argv, "%s", &sid) < 1) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
channel = (channel_s == NULL)? 0 : atoi(channel_s);
for (idx = 0; idx < 12; idx++)
{
if (idx == 0)
strcpy(chstr, "Auto");
else
sprintf(chstr, "%d", idx);
ret += websWrite(wp, "<option value=\"%d\" %s>%s</option>", idx, (idx == channel)? "selected" : "", chstr);
}
if ( strcasecmp(value, "CA") && strcasecmp(value, "CO") && strcasecmp(value, "DO") &&
strcasecmp(value, "GT") && strcasecmp(value, "MX") && strcasecmp(value, "NO") &&
strcasecmp(value, "PA") && strcasecmp(value, "PR") && strcasecmp(value, "TW") &&
strcasecmp(value, "US") && strcasecmp(value, "UZ") )
{
for (idx = 12; idx < 14; idx++)
{
sprintf(chstr, "%d", idx);
ret += websWrite(wp, "<option value=\"%d\" %s>%s</option>", idx, (idx == channel)? "selected" : "", chstr);
}
}
if ((strcmp(value, "") == 0) || (strcasecmp(value, "DB") == 0)/* || (strcasecmp(value, "JP") == 0)*/)
ret += websWrite(wp, "<option value=\"14\" %s>14</option>", (14 == channel)? "selected" : "");
return ret;
}
static int
ej_nvram_char_to_ascii(int eid, webs_t wp, int argc, char_t **argv)
{
char *sid, *name;
char tmp[MAX_LINE_SIZE];
char *buf = tmp, *str;
int ret;
if (ejArgs(argc, argv, "%s %s", &sid, &name) < 2) {
websError(wp, 400, "Insufficient args\n");
return -1;
}
str = nvram_safe_get_x(sid, name);
/* each char expands to %XX at max */
ret = strlen(str) * sizeof(char)*3 + sizeof(char);
if (ret > sizeof(tmp)) {