summaryrefslogtreecommitdiff
path: root/release/src/router/shared/filters.c
blob: 521660197bc315c447677f766e4e56ee374c4df2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853

/*
 *********************************************************
 *   Copyright 2003, CyberTAN  Inc.  All Rights Reserved *
 *********************************************************

 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
 the contents of this file may not be disclosed to third parties,
 copied or duplicated in any form without the prior written
 permission of CyberTAN Inc.

 This software should be used as a reference only, and it not
 intended for production use!


 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE.  CYBERTAN
 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <broadcom.h>

/* 
   Format:
     filter_rule{1...10}=$STAT:1$NAME:test1$

	Below for new UI:
	
	$STAT:0$NAME:$DENY:0$$	==> Disable/Allow
	$STAT:2$NAME:$DENY:1$$	==> Enable/Allow
	$STAT:1$NAME:$DENY:1$$	==> Enable/Deny
	$STAT:0$NAME:$DENY:1$$	==> Disable/Deny

	Below for old UI:
	
	$STAT:1$NAME:$$	==> Deny (Disable Internet Access for Listed PCs)
	$STAT:2$NAME:$$	==> Allow (Enable Internet Access for Listed PCs)

   Format:
     filter_tod{1...10} = hr:min hr:min wday
     filter_tod_buf{1...10} = sun mon tue wed thu fri sat	 //only for web page read
   Example:
     Everyday and 24-hour
     filter_todXX = 0:0 23:59 0-0
     filter_tod_bufXX = 7	(for web)

     From 9:55 to 22:00 every sun, wed and thu
     filter_todXX = 9:55 22:00 0,3-4
     filter_tod_bufXX = 1 0 1 1 0 0 0	(for web)

   Format:
     filter_ip_grp{1...10} = ip1 ip2 ip3 ip4 ip5 ip6 ip_r1-ipr2 ip_r3-ip_r4
     filter_ip_mac{1...10} = 00:11:22:33:44:55 00:12:34:56:78:90
 
   Format:
     filter_port=udp:111-222 both:22-33 disable:22-333 tcp:11-22222

   Converting Between AM/PM and 24 Hour Clock:
    Converting from AM/PM to 24 hour clock:
     12:59 AM -> 0059 	 (between 12:00 AM and 12:59 AM, subtract 12 hours)
     10:00 AM -> 1000    (between 1:00 AM and 12:59 PM, a straight conversion)
     10:59 PM -> 2259    (between 1:00 PM and 11:59 PM, add 12 hours)
    Converting from 24 hour clock to AM/PM
     0059 -> 12:59 AM    (between 0000 and 0059, add 12 hours)
     0100 -> 1:00  AM    (between 0100 and 1159, straight converion to AM)
     1259 -> 12:59 PM    (between 1200 and 1259, straight converion to PM)
     1559 -> 3:59  PM    (between 1300 and 2359, subtract 12 hours)
     
*/

int filter_id  = 1;
int day_all=0, week0=0, week1=0, week2=0, week3=0, week4=0, week5=0, week6=0;
int start_week=0, end_week=0;
int time_all=0, start_hour=0, start_min=0, start_time=0, end_hour=0, end_min=0, end_time=0;
int tod_data_null = 0;

void
validate_filter_ip_grp(webs_t wp, char *value, struct variable *v)
{
	int i=0;
	char buf[256] = "";
	char *ip0, *ip1, *ip2, *ip3, *ip4, *ip5, *ip_range0_0, *ip_range0_1, *ip_range1_0, *ip_range1_1;
	unsigned char ip[10]={0,0,0,0,0,0,0};
	struct variable filter_ip_variables[] = {
		{ longname: "TCP/UDP IP address", argv: ARGV("0", "254") },
	}, *which;
	char _filter_ip[] = "filter_ip_grpXXX";
	//char _filter_rule[] = "filter_ruleXXX";
	//char _filter_tod[] = "filter_todXXX";

	which = &filter_ip_variables[0];

	ip0 = websGetVar(wp, "ip0", "0");
	ip1 = websGetVar(wp, "ip1", "0");
	ip2 = websGetVar(wp, "ip2", "0");
	ip3 = websGetVar(wp, "ip3", "0");
	ip4 = websGetVar(wp, "ip4", "0");
	ip5 = websGetVar(wp, "ip5", "0");
	ip_range0_0 = websGetVar(wp, "ip_range0_0", "0");
	ip_range0_1 = websGetVar(wp, "ip_range0_1", "0");
	ip_range1_0 = websGetVar(wp, "ip_range1_0", "0");
	ip_range1_1 = websGetVar(wp, "ip_range1_1", "0");

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): ip=[%s %s %s %s %s %s] [%s-%s %s-%s]",__FUNCTION__,ip0,ip1,ip2,ip3,ip4,ip5,ip_range0_0,ip_range0_1,ip_range1_0,ip_range1_1);
#endif

	if (!valid_range(wp, ip0, &which[0]) || 
	    !valid_range(wp, ip1, &which[0]) ||
	    !valid_range(wp, ip2, &which[0]) ||
	    !valid_range(wp, ip3, &which[0]) ||
	    !valid_range(wp, ip4, &which[0]) ||
	    !valid_range(wp, ip5, &which[0]) ||
	    !valid_range(wp, ip_range0_0, &which[0]) ||
	    !valid_range(wp, ip_range0_1, &which[0]) ||
	    !valid_range(wp, ip_range1_0, &which[0]) ||
	    !valid_range(wp, ip_range1_0, &which[0])){
		error_value = 1;
		return;
	}

	if(atoi(ip0))	ip[i++] = atoi(ip0);
	if(atoi(ip1))	ip[i++] = atoi(ip1);
	if(atoi(ip2))	ip[i++] = atoi(ip2);
	if(atoi(ip3))	ip[i++] = atoi(ip3);
	if(atoi(ip4))	ip[i++] = atoi(ip4);
	if(atoi(ip5))	ip[i++] = atoi(ip5);

	if(atoi(ip_range0_0) > atoi(ip_range0_1))
		SWAP(ip_range0_0,ip_range0_1);		

	if(atoi(ip_range1_0) > atoi(ip_range1_1))
		SWAP(ip_range1_0,ip_range1_1);		


	sprintf(buf,"%d %d %d %d %d %d %s-%s %s-%s",ip[0],ip[1],ip[2],ip[3],ip[4],ip[5],ip_range0_0,ip_range0_1,ip_range1_0,ip_range1_1);


 	snprintf(_filter_ip, sizeof(_filter_ip), "filter_ip_grp%s", nvram_safe_get("filter_id"));
	nvram_set(_filter_ip, buf);
	
 	//snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
 	//snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
	//if(nvram_match(_filter_rule, "")){
	//	nvram_set(_filter_rule, "$STAT:1$NAME:$$");
	//	nvram_set(_filter_tod, "0:0 23:59 0-6");
	//}

}

/* Example:
 * tcp:100-200 udp:210-220 both:250-260
 */

void
validate_filter_port(webs_t wp, char *value, struct variable *v)
{
	int i;
	char buf[1000] = "", *cur = buf;
	struct variable filter_port_variables[] = {
		{ longname: "TCP/UDP Port Filter Starting LAN Port", argv: ARGV("0", "65535") },
		{ longname: "TCP/UDP Port Filter Ending LAN Port", argv: ARGV("0", "65535") },
	}, *which;

	which = &filter_port_variables[0];

	for (i = 0; i < FILTER_PORT_NUM ; i++) {
		char filter_port[] = "protoXXX";
		char filter_port_start[] = "startXXX";
		char filter_port_end[] = "endXXX";
		char *port, *start, *end;
		char *temp;

 		snprintf(filter_port, sizeof(filter_port), "proto%d", i);
 		snprintf(filter_port_start, sizeof(filter_port_start), "start%d", i);
		snprintf(filter_port_end, sizeof(filter_port_end), "end%d", i);
		port = websGetVar(wp, filter_port, NULL);
		start = websGetVar(wp, filter_port_start, NULL);
		end = websGetVar(wp, filter_port_end, NULL);

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): %s=[%s] %s=[%s] %s=[%s]",__FUNCTION__,i,filter_port,port,filter_port_start,start,filter_port_end,end);
#endif

		if (!port || !start || !end)
			continue;

		if (!*start && !*end)
			continue;

		if ((!strcmp(start,"0") || !strcmp(start,"")) &&
		    (!strcmp(end,"0") || !strcmp(end,"")))
			continue;

		if (!*start || !*end) {
			//websWrite(wp, "Invalid <b>%s</b>: must specify a LAN Port Range<br>", v->longname);
			continue;
		}
		if (!valid_range(wp, start, &which[0]) || !valid_range(wp, end, &which[1])){
			error_value = 1;
			continue;
		}
		if (atoi(start) > atoi(end)) {
			temp = start;
			start = end;
			end = temp;			
		}
		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s%s:%d-%d",
				cur == buf ? "" : " ", port, atoi(start), atoi(end));
	}
	

	nvram_set(v->name, buf);

}

void
validate_filter_dport_grp(webs_t wp, char *value, struct variable *v)
{
	int i;
	char buf[1000] = "", *cur = buf;
	struct variable filter_port_variables[] = {
		{ longname: "TCP/UDP Port Filter Starting LAN Port", argv: ARGV("0", "65535") },
		{ longname: "TCP/UDP Port Filter Ending LAN Port", argv: ARGV("0", "65535") },
	}, *which;
	char _filter_port[] = "filter_dport_grpXXX";
	//char _filter_rule[] = "filter_ruleXXX";
	//char _filter_tod[] = "filter_todXXX";

	which = &filter_port_variables[0];

	for (i = 0; i < FILTER_PORT_NUM ; i++) {
		char filter_port[] = "protoXXX";
		char filter_port_start[] = "startXXX";
		char filter_port_end[] = "endXXX";
		char *port, *start, *end;
		char *temp;

 		snprintf(filter_port, sizeof(filter_port), "proto%d", i);
 		snprintf(filter_port_start, sizeof(filter_port_start), "start%d", i);
		snprintf(filter_port_end, sizeof(filter_port_end), "end%d", i);
		port = websGetVar(wp, filter_port, NULL);
		start = websGetVar(wp, filter_port_start, NULL);
		end = websGetVar(wp, filter_port_end, NULL);

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): %s=[%s] %s=[%s] %s=[%s]",__FUNCTION__,i,filter_port,port,filter_port_start,start,filter_port_end,end);
#endif

		if (!port || !start || !end)
			continue;

		if (!*start && !*end)
			continue;

		if ((!strcmp(start,"0") || !strcmp(start,"")) &&
		    (!strcmp(end,"0") || !strcmp(end,"")))
			continue;

		if (!*start || !*end) {
			//websWrite(wp, "Invalid <b>%s</b>: must specify a LAN Port Range<br>", v->longname);
			continue;
		}
		if (!valid_range(wp, start, &which[0]) || !valid_range(wp, end, &which[1])){
			error_value = 1;
			continue;
		}
		if (atoi(start) > atoi(end)) {
			temp = start;
			start = end;
			end = temp;			
		}
		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s%s:%d-%d",
				cur == buf ? "" : " ", port, atoi(start), atoi(end));
	}
	
 	snprintf(_filter_port, sizeof(_filter_port), "filter_dport_grp%s", nvram_safe_get("filter_id"));
	nvram_set(_filter_port, buf);
	
 	//snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
 	//snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
	//if(nvram_match(_filter_rule, "")){
	//	nvram_set(_filter_rule, "$STAT:1$NAME:$$");
	//	nvram_set(_filter_tod, "0:0 23:59 0-6");
	//}

}

/* Example:
 * 2 00:11:22:33:44:55 00:11:22:33:44:56
 */

void
validate_filter_mac_grp(webs_t wp, char *value, struct variable *v)
{
	int i;
	char buf[1000] = "", *cur = buf;
	char _filter_mac[] = "filter_mac_grpXXX";
	//char _filter_rule[] = "filter_ruleXXX";
	//har _filter_tod[] = "filter_todXXX";

	for (i=0; i<FILTER_MAC_NUM ; i++) {
		char filter_mac[] = "macXXX";
		char *mac, mac1[20]="";
		

 		snprintf(filter_mac, sizeof(filter_mac), "mac%d", i);

		mac = websGetVar(wp, filter_mac, NULL);
		if(!mac)	continue;

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): [%s]=[%s]",__FUNCTION__,i,filter_mac,mac);
#endif
		if(strcmp(mac,"") && strcmp(mac,"00:00:00:00:00:00") && strcmp(mac,"000000000000")){
			if(strlen(mac) == 12){
				char hex[] = "XX";
				unsigned char h;
				while (*mac) {
		                        strncpy(hex, mac, 2);
		                        h = (unsigned char) strtoul(hex, NULL, 16);
					if(strlen(mac1))
						sprintf(mac1+strlen(mac1), ":");
					sprintf(mac1+strlen(mac1), "%02X", h);
		                        mac += 2;
		                }
				mac1[17] = '\0';
			}
			else if(strlen(mac) == 17){
				strcpy(mac1, mac);
			}
			if (!valid_hwaddr(wp, mac1, v)){
				error_value = 1;
				continue;
			}
		}	
		else{
			continue;
		}
		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s%s",
				cur == buf ? "" : " ", mac1);
	}


 	snprintf(_filter_mac, sizeof(_filter_mac), "filter_mac_grp%s", nvram_safe_get("filter_id"));
	nvram_set(_filter_mac, buf);

 	//snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
 	//snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
	//if(nvram_match(_filter_rule, "")){
	//	nvram_set(_filter_rule, "$STAT:1$NAME:$$");
	//	nvram_set(_filter_tod, "0:0 23:59 0-6");
	//}
}


/* Example:
 * 100-200 250-260 (ie. 192.168.1.100-192.168.1.200 192.168.1.250-192.168.1.260)
 */

char filter_ip_get_result[4];

char
*filter_ip_get(char *type, int which)
{
	static char word[256];
	char *start, *end, *wordlist, *next;
	char *result = filter_ip_get_result;
	int temp;
	char filter_ip[] = "filter_ip_grpXXX";
        snprintf(filter_ip, sizeof(filter_ip), "filter_ip_grp%s", nvram_safe_get("filter_id"));
	
	wordlist = nvram_safe_get(filter_ip);
	if(!wordlist)	return "0";

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"[%s]=[%s]",filter_ip,wordlist);
#endif
	temp = which;

	foreach(word, wordlist, next) {
		if (which-- == 0) {
			if(temp == 6){
				end = word;
				start = strsep(&end, "-");
				if (!strcmp(type, "ip_range0_0"))
					return start;	
				else
					return end;
			}
			else if(temp == 7){
				end = word;
				start = strsep(&end, "-");
				if (!strcmp(type, "ip_range1_0"))
					return start;	
				else
					return end;
			}
			return word;
		}
	}

	return "0";
}

/* Example:
 * tcp:100-200 udp:210-220 both:250-260
 */

char 
*filter_dport_get(char *type,int which)
{
	static char word[256];
	char *wordlist, *next;
	char *start, *end, *proto;
	int temp;
	char name[] = "filter_dport_grpXXX";
	
	sprintf(name, "filter_dport_grp%s", nvram_safe_get("filter_id"));
	wordlist = nvram_safe_get(name);
	temp = which;

	foreach(word, wordlist, next) {
		if (which-- == 0) {
			start = word;
			proto = strsep(&start, ":"); 
			end = start;
			start = strsep(&end, "-");
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%s,%d): proto=[%s] start=[%s] end=[%s]",__FUNCTION__,type,temp,proto,start,end);
#endif
			if (!strcmp(type, "disable")){
				if (!strcmp(proto,"disable"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "both")){
				if (!strcmp(proto,"both"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "tcp")){
				if (!strcmp(proto,"tcp"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "udp")){
				if (!strcmp(proto,"udp"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "start"))
				return start;
			else if (!strcmp(type, "end"))
				return end;
		}
	}

	if (!strcmp(type, "start") || !strcmp(type, "end"))
		return "0";
	else
		return "";
}

int
ej_filter_dport_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret, which;
	char *type;

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): type=[%s] which=[%d]",__FUNCTION__, type, which);
#endif

	ret = websWrite(wp,"%s",filter_dport_get(type,which));


	return ret;

}

/* Example:
 * tcp:100-200 udp:210-220 both:250-260
 */

char 
*filter_port_get(char *type,int which)
{
	static char word[256];
	char *wordlist, *next;
	char *start, *end, *proto;
	int temp;

	wordlist = nvram_safe_get("filter_port");
	temp = which;

	foreach(word, wordlist, next) {
		if (which-- == 0) {
			start = word;
			proto = strsep(&start, ":"); 
			end = start;
			start = strsep(&end, "-");
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%s,%d): proto=[%s] start=[%s] end=[%s]",__FUNCTION__,type,temp,proto,start,end);
#endif
			if (!strcmp(type, "disable")){
				if (!strcmp(proto,"disable"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "both")){
				if (!strcmp(proto,"both"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "tcp")){
				if (!strcmp(proto,"tcp"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "udp")){
				if (!strcmp(proto,"udp"))
					return "selected";
				else
					return " ";
			}
			else if (!strcmp(type, "start"))
				return start;
			else if (!strcmp(type, "end"))
				return end;
		}
	}

	if (!strcmp(type, "start") || !strcmp(type, "end"))
		return "0";
	else
		return "";
}

int
ej_filter_port_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret, which;
	char *type;

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): type=[%s] which=[%d]",__FUNCTION__, type, which);
#endif

	ret = websWrite(wp,"%s",filter_port_get(type,which));


	return ret;

}

/* Example:
 * 00:11:22:33:44:55 00:11:22:33:44:56
 */

char 
*filter_mac_get(int which)
{
	static char word[256];
	char *wordlist, *next;
	char *mac;
	int temp;
	char filter_mac[] = "filter_mac_grpXXX";

        snprintf(filter_mac, sizeof(filter_mac), "filter_mac_grp%s", nvram_safe_get("filter_id"));
	
	wordlist = nvram_safe_get(filter_mac);
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"[%s]=[%s]",filter_mac,wordlist);
#endif
	if(!wordlist)	return "";
		
	temp = which;

	foreach(word, wordlist, next) {
		if (which-- == 0) {
			mac = word;
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): mac=[%s]",__FUNCTION__,temp,mac);
#endif
			return mac;
		}
	}
	return "00:00:00:00:00:00";
}


int
ej_filter_ip_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret=0, which;
	char *type;

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): which=[%d] type=[%s]",__FUNCTION__,which,type);
#endif

	ret = websWrite(wp,"%s",filter_ip_get(type,which));


	return ret;
}


int
ej_filter_mac_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret, which;

        if (ejArgs(argc, argv, "%d", &which) < 1) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): which=[%d]",__FUNCTION__,which);
#endif

	ret = websWrite(wp,"%s",filter_mac_get(which));

	return ret;
}
#if 0
void
validate_filter_ip_grp(webs_t wp, char *value, struct variable *v)
{
	char *filter_ip_grp_1, *filter_ip_grp_2;	

	filter_ip_grp_1= websGetVar(wp, "filter_ip_grp_1", NULL);
	filter_ip_grp_2= websGetVar(wp, "filter_ip_grp_2", NULL);

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): filter_ip_grp_1=[%s] filter_ip_grp_2=[%s]",__FUNCTION__,filter_ip_grp_1,filter_ip_grp_2);
#endif
	if(!filter_ip_grp_1)	return;

	if(!strcmp(filter_ip_grp_1,"0"))
		nvram_set(v->name,"0");
	else if(!strcmp(filter_ip_grp_1,"other")){
		if(!filter_ip_grp_2)	return;
                nvram_set(v->name,filter_ip_grp_2);
	}
}

void
validate_filter_mac_grp(webs_t wp, char *value, struct variable *v)
{
	char *filter_mac_grp_1, *filter_mac_grp_2;	

	filter_mac_grp_1= websGetVar(wp, "filter_mac_grp_1", NULL);
	filter_mac_grp_2= websGetVar(wp, "filter_mac_grp_2", NULL);

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): filter_mac_grp_1=[%s] filter_mac_grp_2=[%s]",__FUNCTION__,filter_mac_grp_1,filter_mac_grp_2);
#endif

	if(!filter_mac_grp_1)	return;

	if(!strcmp(filter_mac_grp_1,"0"))
		nvram_set(v->name,"0");
	else if(!strcmp(filter_mac_grp_1,"other")){
		if(!filter_mac_grp_2)	return;
                nvram_set(v->name,filter_mac_grp_2);
	}
}
#endif

/*  Format: url0=www.kimo.com.tw, ...
 *	    keywd0=sex, ...
 */
void
validate_filter_web(webs_t wp, char *value, struct variable *v)
{
	int i;
	char buf[1000] = "", *cur = buf;
	char buf1[1000] = "", *cur1 = buf1;
	char filter_host[] = "filter_web_hostXXX";
	char filter_url[] = "filter_web_urlXXX";

	/* Handle Website Blocking by URL Address */	
	for (i=0; i<4 ; i++) {
		char filter_host[] = "urlXXX";
		char *host;

 		snprintf(filter_host, sizeof(filter_host), "host%d", i);
		host = websGetVar(wp, filter_host, "");
		
		if(!strcmp(host, ""))	continue;

		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s%s",
				cur == buf ? "" : "<&nbsp;>", host);
	}

	if(strcmp(buf, ""))
		strcat(buf,"<&nbsp;>");

 	snprintf(filter_host, sizeof(filter_host), "filter_web_host%s", nvram_safe_get("filter_id"));
	nvram_set(filter_host, buf);
	
	/* Handle Website Blocking by Keyword */
	for (i=0; i<6 ; i++) {
		char filter_url[] = "urlXXX";
		char *url;

 		snprintf(filter_url, sizeof(filter_url), "url%d", i);
		url = websGetVar(wp, filter_url, "");
		
		if(!strcmp(url, ""))	continue;

		cur1 += snprintf(cur1, buf1 + sizeof(buf1) - cur1, "%s%s",
				cur1 == buf1 ? "" : "<&nbsp;>", url);
	}
	if(strcmp(buf1,""))
		strcat(buf1, "<&nbsp;>");

 	snprintf(filter_url, sizeof(filter_url), "filter_web_url%s", nvram_safe_get("filter_id"));
	nvram_set(filter_url, buf1);

}

int
validate_filter_tod(webs_t wp)
{
	char buf[256] = "";
	char tod_buf[20];
	struct variable filter_tod_variables[] = {
		{ longname: "Tod name", argv: ARGV("20") },
		{ longname: "Tod Status", argv: ARGV("0", "1","2") },

	}, *which;


	char *day_all, *week0, *week1, *week2, *week3, *week4, *week5, *week6;
	char *time_all, *start_hour, *start_min, *start_time, *end_hour, *end_min, *end_time;
	int _start_hour, _start_min, _end_hour, _end_min;
	char time[20];
	int week[7];
	int i, flag=-1, dash=0;
	char filter_tod[] = "filter_todXXX";
	char filter_tod_buf[] = "filter_tod_bufXXX";

	which = &filter_tod_variables[0];

	day_all = websGetVar(wp, "day_all", "0");
	week0 = websGetVar(wp, "week0", "0");
	week1 = websGetVar(wp, "week1", "0");
	week2 = websGetVar(wp, "week2", "0");
	week3 = websGetVar(wp, "week3", "0");
	week4 = websGetVar(wp, "week4", "0");
	week5 = websGetVar(wp, "week5", "0");
	week6 = websGetVar(wp, "week6", "0");
	time_all = websGetVar(wp, "time_all", "0");
	start_hour = websGetVar(wp, "start_hour", "0");
	start_min = websGetVar(wp, "start_min", "0");
	start_time = websGetVar(wp, "start_time", "0");
	end_hour = websGetVar(wp, "end_hour", "0");
	end_min = websGetVar(wp, "end_min", "0");
	end_time = websGetVar(wp, "end_time", "0");

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): day_all=[%s] week=[%s %s %s %s %s %s %s] time_all=[%s] start=[%s:%s:%s] end=[%s:%s:%s]",__FUNCTION__,filter_id,day_all,week0,week1,week2,week3,week4,week5,week6,time_all,start_hour,start_min,start_time,end_hour,end_min,end_time);
#endif
	//if(atoi(time_all) == 0)
	//	if(!start_hour || !start_min || !start_time || !end_hour || !end_min || !end_time)
	//		return 1;

	if(atoi(day_all) == 1){
		strcpy(time,"0-6");
		strcpy(tod_buf,"7");
	}
	else{
		week[0] = atoi(week0);
		week[1] = atoi(week1);
		week[2] = atoi(week2);
		week[3] = atoi(week3);
		week[4] = atoi(week4);
		week[5] = atoi(week5);
		week[6] = atoi(week6);
		strcpy(time,"");

		for(i = 0 ; i < 7 ; i ++){
			if(week[i] == 1){
				if(i == 6){
					 if(dash == 0 && flag == 1)
                                                sprintf(time+strlen(time),"%c",'-');
					 sprintf(time+strlen(time),"%d",i);
				}
				else if(flag == 1 && dash == 0){
					sprintf(time+strlen(time),"%c",'-');
					dash = 1;
				}
				else if(dash ==0){
					sprintf(time+strlen(time),"%d",i);
					flag = 1;
					dash = 0;
				}
			}
			else{
				if(!strcmp(time,""))	continue;
				if(dash == 1)
					sprintf(time+strlen(time),"%d",i-1);
				if(flag != 0)
					sprintf(time+strlen(time),"%c",',');
				flag = 0;
				dash = 0;
			}
		}
		if(time[strlen(time)-1] == ',')
			time[strlen(time)-1] = '\0';

		snprintf(tod_buf,sizeof(tod_buf),"%s %s %s %s %s %s %s",week0, week1, week2, week3, week4, week5, week6);
	}
	if(atoi(time_all) == 1){
		_start_hour = 0;
		_start_min = 0;
		_end_hour = 23;
		_end_min = 59;
	}
	else{
		_start_hour = atoi(start_time) ? 12 + atoi(start_hour) : atoi(start_hour);
		_start_min = atoi(start_min);
		_end_hour = atoi(end_time) ? 12 + atoi(end_hour) : atoi(end_hour);
		_end_min = atoi(end_min);
	}

	sprintf(buf,"%d:%d %d:%d %s",_start_hour,_start_min,_end_hour,_end_min,time);
 	snprintf(filter_tod, sizeof(filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
 	snprintf(filter_tod_buf, sizeof(filter_tod_buf), "filter_tod_buf%s", nvram_safe_get("filter_id"));
	
	nvram_set(filter_tod, buf);
	nvram_set(filter_tod_buf, tod_buf);

	return 0;
	
}

int
delete_policy(webs_t wp, int which)
{
	char filter_rule[] = "filter_ruleXXX";
	char filter_tod[] = "filter_todXXX";
	char filter_tod_buf[] = "filter_tod_bufXXX";
	char filter_host[] = "filter_web_hostXXX";
	char filter_url[] = "filter_web_urlXXX";
	char filter_ip_grp[] = "filter_ip_grpXXX";
	char filter_mac_grp[] = "filter_mac_grpXXX";
	char filter_port_grp[] = "filter_port_grpXXX";
	char filter_dport_grp[] = "filter_dport_grpXXX";
	char filter_dest_ipgrp[] = "filter_dest_ipgrpXXX";
	char filter_in_out[] = "filter_in_outXXX";
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): which=[%d]",__FUNCTION__,which);
#endif

 	snprintf(filter_rule, sizeof(filter_rule), "filter_rule%d", which);
 	snprintf(filter_tod, sizeof(filter_tod), "filter_tod%d", which);
 	snprintf(filter_tod_buf, sizeof(filter_tod_buf), "filter_tod_buf%d", which);
 	snprintf(filter_host, sizeof(filter_host), "filter_web_host%d", which);
 	snprintf(filter_url, sizeof(filter_url), "filter_web_url%d", which);
 	snprintf(filter_ip_grp, sizeof(filter_ip_grp), "filter_ip_grp%d", which);
 	snprintf(filter_mac_grp, sizeof(filter_mac_grp), "filter_mac_grp%d", which);
 	snprintf(filter_port_grp, sizeof(filter_port_grp), "filter_port_grp%d", which);
 	snprintf(filter_dport_grp, sizeof(filter_dport_grp), "filter_dport_grp%d", which);

	snprintf(filter_in_out, sizeof(filter_in_out), "filter_in_out%d", which);
	
	nvram_set(filter_rule,"");
	nvram_set(filter_tod,"");
	nvram_set(filter_tod_buf,"");
	nvram_set(filter_host,"");
	nvram_set(filter_url,"");
	nvram_set(filter_ip_grp,"");
	nvram_set(filter_mac_grp,"");
	nvram_set(filter_port_grp,"");
	nvram_set(filter_dport_grp,"");

	nvram_set("filter_summary", "1");
	
	return 1;
}

int
single_delete_policy(webs_t wp)
{
	int ret = 0;
	char *id = nvram_safe_get("filter_id");

	ret = delete_policy(wp, atoi(id));

	return ret;
}

int
summary_delete_policy(webs_t wp)
{
	int i, ret = 0;

	for(i=1 ; i<=10 ; i++){
		char filter_sum[] = "sumXXX";
		char *sum;
	 	snprintf(filter_sum, sizeof(filter_sum), "sum%d", i);
		sum = websGetVar(wp, filter_sum, NULL);
		if(sum)
			ret += delete_policy(wp, i);
	}

	return ret;
}

int
save_policy(webs_t wp)
{
	char *f_id, *f_name, *f_status, *f_status2;
	char buf[256] = "";
	struct variable filter_variables[] = {
		{ longname: "Filter ID", argv: ARGV("1","10") },
		{ longname: "Filter Status", argv: ARGV("0","1","2") },
		{ longname: "Filter Status", argv: ARGV("deny","allow") },
		{ longname: "Filter Name", argv: ARGV("30") },

	}, *which;
	char filter_buf[] = "filter_ruleXXX";
	which = &filter_variables[0];

	f_id = websGetVar(wp, "f_id", NULL);
	f_name = websGetVar(wp, "f_name", NULL);
	f_status = websGetVar(wp, "f_status", NULL);	// 0=>Disable / 1,2=>Enable
	f_status2 = websGetVar(wp, "f_status2", NULL);	// deny=>Deny / allow=>Allow
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): f_id=[%s] f_name=[%s] f_status=[%s]",__FUNCTION__,f_id,f_name,f_status);
#endif
	if(!f_id || !f_name || !f_status || !f_status2){
		error_value = 1;
		return -1;
	}
	if(!valid_range(wp, f_id, &which[0])){
		error_value = 1;
		return -1;
        }
	if(!valid_choice(wp, f_status, &which[1])){
		error_value = 1;
		return -1;
        }
	if(!valid_choice(wp, f_status2, &which[2])){
		error_value = 1;
		return -1;
        }
	if(!valid_name(wp, f_name, &which[3])){
		error_value = 1;
		return -1;
	}
	else {
		int i;
		for(i=0 ; *(f_name+i) ; i++) {
			if(!isalnum(*(f_name+i)) && *(f_name+i) != '-' && *(f_name+i) != '_'){
				error_value = 1;
				return -1;
			}
		}
	}

	validate_filter_tod(wp);

 	snprintf(filter_buf, sizeof(filter_buf), "filter_rule%s", nvram_safe_get("filter_id"));

	// Add $DENY to decide that users select Allow or Deny, if status is Disable	// 2003/10/21
	snprintf(buf,sizeof(buf),"$STAT:%s$NAME:%s$DENY:%d$$", f_status, f_name, !strcmp(f_status2,"deny") ? 1 : 0);
	
	nvram_set(filter_buf,buf);

	return 0;
}

void
validate_filter_policy(webs_t wp, char *value, struct variable *v)
{
	char *f_id = websGetVar(wp, "f_id", NULL);

	if(f_id)
		nvram_set("filter_id", f_id);
	else
		nvram_set("filter_id", "1");

	save_policy(wp);

}

/*   Format: 21:21:tcp:FTP(&nbsp;)500:1000:both:TEST1
 *
 */

int
validate_services_port(webs_t wp)
{
	char buf[1000] = "", *cur = buf;
	char *services_array = websGetVar(wp, "services_array", NULL);
	char *services_length = websGetVar(wp, "services_length", NULL);
	char word[1024], *next;
	char delim[] = "(&nbsp;)";
	
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): services_array=[%s] service_length=[%s]", __FUNCTION__, services_array, services_length);
#endif	

	if(!services_array || !services_length)		return 0;
	
	split(word, services_array, next, delim){
		int from, to, proto;
		char name[80];
		if(sscanf(word, "%d:%d:%d:%s", &from, &to, &proto, name) != 4 )
			continue;
#ifdef MY_DEBUG
		LOG(LOG_DEBUG,"%s(): from=[%d] to=[%d] proto=[%d] name=[%s]", __FUNCTION__, from, to, proto, name);
#endif

		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s$NAME:%03d:%s$PROT:%03d:%s$PORT:%03d:%d:%d",
				cur == buf ? "" : "<&nbsp;>", 
				strlen(name), name, 
				strlen(num_to_protocol(proto)), num_to_protocol(proto),
				(get_int_len(from)+get_int_len(to)+strlen(":")), 
				from, to);
	}
	nvram_set("filter_services", buf);
	return 1;
}

int
save_services_port(webs_t wp)
{
	return validate_services_port(wp);
}

void
validate_blocked_service(webs_t wp, char *value, struct variable *v)
{
	int i;
	char buf[1000] = "", *cur = buf;
	char port_grp[] = "filter_port_grpXXX";

	for(i=0 ; i<BLOCKED_SERVICE_NUM ; i++){
		char blocked_service[] = "blocked_serviceXXX";
		char *service;
		snprintf(blocked_service, sizeof(blocked_service), "blocked_service%d", i);
		service = websGetVar(wp, blocked_service, NULL);
		if(!service || !strcmp(service, "None"))	continue;
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): %s=[%s]", __FUNCTION__, blocked_service, service);
#endif	

		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s%s", service, "<&nbsp;>");
                       //cur == buf ? "" : "<&nbsp;>", service);
	}

 	snprintf(port_grp, sizeof(port_grp), "filter_port_grp%s", nvram_safe_get("filter_id"));
	nvram_set(port_grp, buf);

}
	

int
ej_filter_policy_select(int eid, webs_t wp, int argc, char_t **argv)
{
	int i, ret = 0;

	for(i = 1 ; i <= 10 ; i ++ ){
		char filter[] = "filter_ruleXXX";
		char *data="";
        	char name[50]="";
 		snprintf(filter, sizeof(filter), "filter_rule%d", i);
		data = nvram_safe_get(filter);

		if(data && strcmp(data,"")){
			find_match_pattern(name, sizeof(name), data, "$NAME:","");	// get name value
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%d): data=[%s] name=[%s]",__FUNCTION__,i,data,name);
#endif	
		}
		ret += websWrite(wp,"<option value=%d %s>%d ( %s ) </option>\n",
			i,
			(atoi(nvram_safe_get("filter_id")) == i ? "selected" : ""),
			i,
			name);
	}
	return ret;
}


int
ej_filter_policy_get(int eid, webs_t wp, int argc, char_t **argv)
{

	char *type, *part;
	int ret = 0;


        if (ejArgs(argc, argv, "%s %s", &type, &part) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

	if(!strcmp(type,"f_id")){
		ret = websWrite(wp,"%s",nvram_safe_get("filter_id"));	
	}
	else if(!strcmp(type,"f_name")){
     		char name[50]="";
		char filter[] = "filter_ruleXXX";
		char *data="";
 		snprintf(filter, sizeof(filter), "filter_rule%s", nvram_safe_get("filter_id"));
		data = nvram_safe_get(filter);
		if(strcmp(data,"")){
			find_match_pattern(name, sizeof(name), data, "$NAME:","");	// get name value
			ret = websWrite(wp,"%s",name);	
		}
	}
	else if(!strcmp(type,"f_status")){
     		char status[50]="", deny[50]="";
		char filter[] = "filter_ruleXXX";
		char *data="";
 		snprintf(filter, sizeof(filter), "filter_rule%s", nvram_safe_get("filter_id"));
		data = nvram_safe_get(filter);
		if(strcmp(data,"")){	// have data
			find_match_pattern(status, sizeof(status), data, "$STAT:","1");	// get status value
			find_match_pattern(deny, sizeof(deny), data, "$DENY:", "");	// get deny value
			if(!strcmp(deny,"")){	// old format
				if(!strcmp(status,"0") || !strcmp(status,"1"))
					strcpy(deny,"1");	// Deny
				else
					strcpy(deny,"0");	// Allow
			}
#if 0
			if(!strcmp(part,"disable")){
				if(!strcmp(status,"1"))
					ret = websWrite(wp,"checked");	
			}
			else if(!strcmp(part,"enable")){
				if(!strcmp(status,"2"))
					ret = websWrite(wp,"checked");	
			}
#endif
			if(!strcmp(part,"disable")){
				if(!strcmp(status,"0"))
					ret = websWrite(wp,"checked");	
			}
			else if(!strcmp(part,"enable")){
				if(strcmp(status,"0"))
					ret = websWrite(wp,"checked");	
			}
			else if(!strcmp(part,"deny")){
				if(!strcmp(deny,"1"))
					ret = websWrite(wp,"checked");	
			}
			else if(!strcmp(part,"allow")){
				if(!strcmp(deny,"0"))
					ret = websWrite(wp,"checked");	
			}
			else if(!strcmp(part,"onload_status")){
				if(!strcmp(deny,"1"))
					ret = websWrite(wp,"deny");	
				else
					ret = websWrite(wp,"allow");	
				
			}
			else if(!strcmp(part,"init")){
				if(!strcmp(status,"1"))
					ret = websWrite(wp,"disable");	
				else if(!strcmp(status,"2"))
					ret = websWrite(wp,"enable");
				else
					ret = websWrite(wp,"disable");
			}
		}
		else{	// no data
			if(!strcmp(part,"disable"))
				ret = websWrite(wp,"checked");
			else if(!strcmp(part,"allow"))		// default policy is allow, 2003-10-21
				ret = websWrite(wp,"checked");
			else if(!strcmp(part,"onload_status"))	// default policy is allow, 2003-10-21
				ret = websWrite(wp,"allow");	
			else if(!strcmp(part,"init"))
				ret = websWrite(wp,"disable");	
		}
	}

	return ret;
}

int
filter_tod_init(int which)
{
	char  *tod_data, *tod_buf_data;
	char filter_tod[] = "filter_todXXX";
	char filter_tod_buf[] = "filter_tod_bufXXX";
	char temp[3][20];
	int ret;

	tod_data_null = 0;
	day_all = week0 = week1 = week2 = week3 = week4 = week5 = week6 = 0;
	time_all = start_hour = start_min = start_time = end_hour = end_min = end_time = 0;
	start_week = end_week = 0;

 	snprintf(filter_tod, sizeof(filter_tod), "filter_tod%d", which);
 	snprintf(filter_tod_buf, sizeof(filter_tod_buf), "filter_tod_buf%d", which);
	
	/* Parse filter_tod{1...10} */
	tod_data = nvram_safe_get(filter_tod);
	if(!tod_data)	return -1;	// no data
	if(strcmp(tod_data,"")){
		sscanf(tod_data,"%s %s %s",temp[0],temp[1],temp[2]);
		sscanf(temp[0],"%d:%d",&start_hour,&start_min);
		sscanf(temp[1],"%d:%d",&end_hour,&end_min);
		ret = sscanf(temp[2],"%d-%d",&start_week,&end_week);	
		if(ret == 1)	end_week = start_week;

		if(start_hour == 0 && start_min ==0 && end_hour == 23 && end_min== 59){	// 24 Hours
			time_all = 1;
			start_hour = end_hour = 0;
			start_min = start_time = end_min = end_time = 0;	
		}
		else{	// check AM or PM
			time_all = 0;
			if(start_hour > 11 ){
				start_hour = start_hour - 12;
				start_time = 1;
			}
			if(end_hour > 11 ){
				end_hour = end_hour - 12;
				end_time = 1;
			}
		}
	}
	else{	// default Everyday and 24 Hours
		tod_data_null = 1;
		day_all = 1;
		time_all = 1;
	}

	if(tod_data_null == 0){
		/* Parse filter_tod_buf{1...10} */
		tod_buf_data = nvram_safe_get(filter_tod_buf);
		if(!strcmp(tod_buf_data,"7")){
				day_all = 1;
		}
		else if(strcmp(tod_buf_data,"")){
			sscanf(tod_buf_data,"%d %d %d %d %d %d %d",&week0, &week1, &week2, &week3, &week4, &week5, &week6);	
			day_all = 0;
		}
	}

#ifdef MY_DEBUG
	if(tod_data_null == 1)
		LOG(LOG_DEBUG,"%s(%d): tod data null",__FUNCTION__,which);
	else
		LOG(LOG_DEBUG,"%s(%d):day_all=[%d] week=[%d %d %d %d %d %d %d] time_all=[%d] [%d %d %d %d %d %d]",__FUNCTION__,which,day_all,week0,week1,week2,week3,week4,week5,week6,time_all,start_hour,start_min,start_time,end_hour,end_min,end_time);
#endif	
	return 0;
}

int
ej_filter_tod_get(int eid, webs_t wp, int argc, char_t **argv)
{
	char *type;
	int ret=0 ,i;

        if (ejArgs(argc, argv, "%s", &type) < 1) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

	filter_tod_init(atoi(nvram_safe_get("filter_id")));

	if(!strcmp(type,"day_all_init")){
		if(day_all == 0)
			ret = websWrite(wp,"1");
		else
			ret = websWrite(wp,"0");	
	}
	else if(!strcmp(type,"time_all_init")){
		if(time_all == 0)
			ret = websWrite(wp,"1");	
		else
			ret = websWrite(wp,"0");	
	}
	else if(!strcmp(type,"day_all")){
		ret = websWrite(wp,"%s",day_all == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"start_week")){
		ret = websWrite(wp,"%d",start_week);	
	}
	else if(!strcmp(type,"end_week")){
		ret = websWrite(wp,"%d",end_week);	
	}
	else if(!strcmp(type,"week0")){	// Sun
		ret = websWrite(wp,"%s",week0 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week1")){	// Mon
		ret = websWrite(wp,"%s",week1 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week2")){	// Tue
		ret = websWrite(wp,"%s",week2 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week3")){	// Wed
		ret = websWrite(wp,"%s",week3 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week4")){	// Thu
		ret = websWrite(wp,"%s",week4 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week5")){	// Fri
		ret = websWrite(wp,"%s",week5 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"week6")){	// Sat
		ret = websWrite(wp,"%s",week6 == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"time_all_en")){
		ret = websWrite(wp,"%s",time_all == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"time_all_dis")){
		ret = websWrite(wp,"%s",time_all == 0 ? "checked" : "");	
	}
	else if(!strcmp(type,"time_all")){
		ret = websWrite(wp,"%s",time_all == 1 ? "checked" : "");	
	}
	else if(!strcmp(type,"start_hour_24")){	// 00 -> 23
		for(i=0 ; i<24 ; i++){
			ret = websWrite(wp,"<option value=%d %s>%d</option>\n",i, i == start_hour+start_time*12 ? "selected" : "", i);
		}
	}
	else if(!strcmp(type,"start_min_15")){	// 0 15 30 45
		for(i=0 ; i<4 ; i++){
			ret = websWrite(wp,"<option value=%02d %s>%02d</option>\n",i*15, i*15 == start_min ? "selected" : "", i*15);	
		}
	}
	else if(!strcmp(type,"end_hour_24")){	// 00 ->23
		for(i=0 ; i<24 ; i++){
			ret = websWrite(wp,"<option value=%d %s>%d</option>\n",i, i == end_hour+end_time*12 ? "selected" : "", i);
		}
	}
	else if(!strcmp(type,"end_min_15")){	// 0 15 30 45
		for(i=0 ; i<4 ; i++){
			ret = websWrite(wp,"<option value=%02d %s>%02d</option>\n",i*15, i*15 == end_min ? "selected" : "", i*15);	
		}
	}
	else if(!strcmp(type,"start_hour_12")){	// 1 -> 12
		for(i=1 ; i<=12 ; i++){		
			int j;
			if(i == 12)	j = 0;
			else		j = i;
			ret = websWrite(wp,"<option value=%d %s>%d</option>\n",j, j == start_hour ? "selected" : "", i);
		}
	}
	else if(!strcmp(type,"start_min_5")){	// 0 5 10 15 20 25 30 35 40 45 50 55
		for(i=0 ; i<12 ; i++){
			ret = websWrite(wp,"<option value=%02d %s>%02d</option>\n",i*5, i*5 == start_min ? "selected" : "", i*5);	
		}
	}
	else if(!strcmp(type,"start_time_am")){
		ret = websWrite(wp,"%s",start_time == 1 ? "" : "selected");	
	}
	else if(!strcmp(type,"start_time_pm")){
		ret = websWrite(wp,"%s",start_time == 1 ? "selected" : "");	
	}
	else if(!strcmp(type,"end_hour_12")){	// 1 -> 12
		for(i=1 ; i<=12 ; i++){	
			int j;
			if(i == 12)     j = 0;
			else		j = i;
			ret = websWrite(wp,"<option value=%d %s>%d</option>\n",j, j == end_hour ? "selected" : "", i);	
		}
	}
	else if(!strcmp(type,"end_min_5")){	// 0 5 10 15 20 25 30 35 40 45 50 55
		for(i=0 ; i<12 ; i++){
			ret = websWrite(wp,"<option value=%02d %s>%02d</option>\n",i*5, i*5 == end_min ? "selected" : "", i*5);	
		}
	}
	else if(!strcmp(type,"end_time_am")){
		ret = websWrite(wp,"%s",end_time == 1 ? "" : "selected");	
	}
	else if(!strcmp(type,"end_time_pm")){
		ret = websWrite(wp,"%s",end_time == 1 ? "selected" : "");	
	}

	return ret;
}	

/*  Format: url0, url1, url2, url3, ....
 *	    keywd0, keywd1, keywd2, keywd3, keywd4, keywd5, ....
 */
int
ej_filter_web_get(int eid, webs_t wp, int argc, char_t **argv)
{
	char *type;
	int ret = 0;
	int which;
	char *token = "<&nbsp;>";

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 1) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }
	
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(%s,%d)\n",__FUNCTION__, type, which);
#endif
	
	if(!strcmp(type, "host")){
		char *host_data, filter_host[] = "filter_web_hostXXX";;
		char host[80];
		
 		snprintf(filter_host, sizeof(filter_host), "filter_web_host%s", nvram_safe_get("filter_id"));
		host_data = nvram_safe_get(filter_host);
		if(!strcmp(host_data, ""))	return -1;	// no data
		find_each(host, sizeof(host), host_data, token, which, "");
		ret = websWrite(wp, "%s", host);
	}
	else if(!strcmp(type, "url")){
		char *url_data, filter_url[] = "filter_web_urlXXX";
		char url[80];
		
 		snprintf(filter_url, sizeof(filter_url), "filter_web_url%s", nvram_safe_get("filter_id"));
		url_data = nvram_safe_get(filter_url);
		if(!strcmp(url_data,""))	return -1;	// no data
		find_each(url, sizeof(url), url_data, token, which, "");
		ret = websWrite(wp, "%s", url);
	}

	return ret;
}

int
ej_filter_summary_show(int eid, webs_t wp, int argc, char_t **argv)
{
	int i,ret;
#if LANGUAGE == JAPANESE
	char w[7][10] = { "","","","","","","y"};
	char week_d[7][10] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	char week_s[7][10] = { "j","j","Ηj","j","ؗj","j","yj"};
	char am[] ="ߑO";
	char pm[] ="ߌ";
	char _24h[] ="24 ";
	char everyday[] ="";
#elif LANGUAGE == KOREAN && OEM == REENET	// <remove the line>
	char w[7][10] = { "","","ȭ","","","",""};	// <remove the line>
	char week_d[7][10] = { "","","ȭ","","","",""};	// <remove the line>
	char am[] ="";	// <remove the line>
	char pm[] ="";	// <remove the line>
	char _24h[] ="";	// <remove the line>
	char everyday[] ="24ð";	// <remove the line>
#else
	char w[7][2] = { "S","M","T","W","T","F","S"};
	char week_d[7][10] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	char week_s[7][10] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
	char am[] ="AM";
	char pm[] ="PM";
	char _24h[] ="24 Hours.";
	char everyday[] ="Everyday";
#endif

	for(i=0 ; i<10 ; i++){
     		char name[50]="---";
     		char status[5]="---";
		char filter[] = "filter_ruleXXX";
		char *data = "";
		char status_buf[50]="---";
		char day_buf[50]="---";
		char time_buf[50]="---";
 		snprintf(filter, sizeof(filter), "filter_rule%d", i+1);
		data = nvram_safe_get(filter);
		if(data && strcmp(data,"")){
			find_match_pattern(name, sizeof(name), data, "$NAME:","&nbsp;");	// get name value
			find_match_pattern(status, sizeof(status), data, "$STAT:","---");	// get name value
		}

		filter_tod_init(i+1);

ret = websWrite(wp," \
	<tr bgcolor=cccccc align=middle>\n\
        <td width=50><font face=Arial size=2>%d.</font></td>\n\
        <td width=200><font face=Arial size=2>%s</font></td>\n",i+1,name);
#if OEM == LINKSYS
ret = websWrite(wp,"\n\
	 <td height=30 width=150 bordercolor=#000000 bgcolor=#CCCCCC> \n\
              <table border=1 cellspacing=1 bordercolor=#000000 style=\"border-collapse: collapse\" width=124 bgcolor=#FFFFFF>\n\
                <tr>");

ret = websWrite(wp,"\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.sun1) {Capture(filter.sun1);} else {document.write(\"%s\");}</script></td>\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.mon1) {Capture(filter.mon1);} else {document.write(\"%s\");}</script></td>\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.tue1) {Capture(filter.tue1);} else {document.write(\"%s\");}</script></td>\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.wed1) {Capture(filter.wed1);} else {document.write(\"%s\");}</script></td>", tod_data_null == 0 && (day_all == 1 || week0 == 1) ? "#C0C0C0" : "#FFFFFF", w[0],
				tod_data_null == 0 && (day_all == 1 || week1 == 1) ? "#C0C0C0" : "#FFFFFF", w[1],
				tod_data_null == 0 && (day_all == 1 || week2 == 1) ? "#C0C0C0" : "#FFFFFF", w[2],
				tod_data_null == 0 && (day_all == 1 || week3 == 1) ? "#C0C0C0" : "#FFFFFF", w[3]);

ret = websWrite(wp,"\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.thu1) {Capture(filter.thu1);} else {document.write(\"%s\");}</script></td>\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.fri1) {Capture(filter.fri1);} else {document.write(\"%s\");}</script></td>\n\
                  <td width=17 bgcolor=\"%s\" style=\"border-style: solid; border-width: 1\"><script>if (filter.sat1) {Capture(filter.sat1);} else {document.write(\"%s\");}</script></td>\n\
                </tr>\n\
              </table></td>",tod_data_null == 0 && (day_all == 1 || week4 == 1) ? "#C0C0C0" : "#FFFFFF", w[4],
				tod_data_null == 0 && (day_all == 1 || week5 == 1) ? "#C0C0C0" : "#FFFFFF", w[5],
				tod_data_null == 0 && (day_all == 1 || week6 == 1) ? "#C0C0C0" : "#FFFFFF", w[6]);

	if(tod_data_null == 0){
		if(time_all == 1)
			strcpy(time_buf, _24h);
		else{
			snprintf(time_buf,sizeof(time_buf),"%02d:%02d %s - %02d:%02d %s",
				start_hour == 0 ? 12 : start_hour,
				start_min,
				start_time == 0 ? am : pm,
				end_hour == 0 ? 12 : end_hour,
				end_min,
				end_time == 0 ? am : pm);
		}
	}
#elif OEM == PCI || OEM == ELSA
	//if(!day_all){
	if(tod_data_null == 0){
		if(day_all == 1)
			strcpy(day_buf, everyday);
		else
			snprintf(day_buf,sizeof(day_buf),"%s - %s",week_s[start_week], week_s[end_week]);
#if LANGUAGE == ENGLISH	// <remove the line>
		snprintf(status_buf,sizeof(status_buf),"%s",atoi(status)==1 ? "Deny" : "Allow");
#else	// <remove the line>
		snprintf(status_buf,sizeof(status_buf),"%s",atoi(status)==1 ? "" : "");
#endif	// <remove the line>
	}
ret = websWrite(wp,"<td width=80><font face=Arial size=2>%s</font></td>\n",status_buf);
ret = websWrite(wp,"<td width=124><font face=Arial size=2>%s</font></td>\n",day_buf);
	if(tod_data_null == 0){
		if(time_all == 1)
			strcpy(time_buf, _24h);
		else
			snprintf(time_buf,sizeof(time_buf),"%02d:%02d - %02d:%02d",start_hour+start_time*12, start_min,
									   end_hour+end_time*12, end_min);
	}
#endif
ret = websWrite(wp," \
        <td width=150><font face=Arial size=2> %s </font> </td>\n\
        <td width=70><input type=checkbox name=sum%d value=1 ></td>\n\
      </tr>\n",time_buf, i+1);
	}
	return ret;
	
}

int
ej_filter_init(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;
	char *f_id = websGetVar(wp, "f_id", NULL);	

	if(f_id)	// for first time enter this page and don't press apply.
		nvram_set("filter_id", f_id);
	else
		nvram_set("filter_id", "1");

	nvram_set("filter_summary", "0");

	return ret;
}

int
ej_filter_port_services_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;
	char *type;
	int which;
	char word[1024], *next, *services;
	char delim[] = "<&nbsp;>";
	
        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }
	
	if(!strcmp(type, "all_list")){
		int count = 0;
		services = nvram_safe_get("filter_services");
		split(word, services, next, delim) {
			int len = 0;
			char *name, *prot, *port;
			char protocol[100], ports[100];
			int from = 0, to = 0;
			//int proto;
			
			if ((name=strstr(word, "$NAME:")) == NULL ||
			    (prot=strstr(word, "$PROT:")) == NULL ||
			    (port=strstr(word, "$PORT:")) == NULL) 
			    continue;

			/* $NAME */
			if (sscanf(name, "$NAME:%3d:", &len) != 1) 
			    continue;
			strncpy(name, name + sizeof("$NAME:nnn:") - 1, len);
                        name[len] = '\0';
	
			/* $PROT */
			if (sscanf(prot, "$PROT:%3d:", &len) != 1) 
			    continue;
			strncpy(protocol, prot + sizeof("$PROT:nnn:") - 1, len);
			protocol[len] = '\0';

			/* $PORT */
			if (sscanf(port, "$PORT:%3d:", &len) != 1) 
			    continue;
			strncpy(ports, port + sizeof("$PORT:nnn:") - 1, len);
			ports[len] = '\0';
			if (sscanf(ports, "%d:%d", &from, &to) != 2)
				continue;

			//cprintf("match:: name=%s, protocol=%s, ports=%s\n", 
			//	word, protocol, ports);
			
			ret = websWrite(wp, "services[%d]=new service(%d, \"%s\", %d, %d, %d);\n",
					    count, count, name, from, to, protocol_to_num(protocol));
			count ++;
			    
		}
				    
		ret += websWrite(wp, "services_length = %d;\n", count);
	}
	else if (!strcmp(type, "service")){
		char *port_data, filter_port[] = "filter_port_grpXXX";
		char name[80];
		
 		snprintf(filter_port, sizeof(filter_port), "filter_port_grp%s", nvram_safe_get("filter_id"));
		port_data = nvram_safe_get(filter_port);
		if(!strcmp(port_data, ""))	return -1;	// no data
		find_each(name, sizeof(name), port_data, "<&nbsp;>", which, "");
		ret += websWrite(wp, "%s", name);
		
	}
	
	return ret;
}

int 
filtersummary_onload(webs_t wp, char *arg)
{
	int ret = 0;

	if(!strcmp(nvram_safe_get("filter_summary"),"1")){
		ret += websWrite(wp, arg);	
	}

	return ret;
}

#ifdef MULTIPLE_LOGIN_SUPPORT //r
int
ej_user_account_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;
	char *type;
	int which;
	char word[1024], *next, *accounts;
	char delim[] = "<&nbsp;>";

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }

	int count = 0;
	accounts = nvram_safe_get("http_login");
	split(word, accounts, next, delim) {
		int len = 0;
		char *name, *prot, *port;
		char psw[100], mod[100];

		if ((name=strstr(word, "$NAME:")) == NULL ||
		    (prot=strstr(word, "$PSW:")) == NULL ||
		    (port=strstr(word, "$MOD:")) == NULL)
		    continue;

		/* $NAME */
		if (sscanf(name, "$NAME:%3d:", &len) != 1)
		    continue;
		strncpy(name, name + sizeof("$NAME:nnn:") - 1, len);
                    name[len] = '\0';

		/* $PSW */
		if (sscanf(prot, "$PSW:%3d:", &len) != 1)
		    continue;
		strncpy(psw, prot + sizeof("$PSW:nnn:") - 1, len);
		psw[len] = '\0';

		/* $MOD */
		if (sscanf(port, "$MOD:%3d:", &len) != 1)
		    continue;
		strncpy(mod, port + sizeof("$MOD:nnn:") - 1, len);
		mod[len] = '\0';

		if(count != 0)
			ret = websWrite(wp, "accounts[%d]=new account(%d, \"%s\", \"%s\", \"%s\");\n",
				    count-1, count-1, name, psw, mod);
		count ++;
	}
	ret += websWrite(wp, "accounts_length = %d;\n", count-1);
	return ret;
}

int
validate_user_account(webs_t wp)
{
	char buf[1000] = "", *cur = buf;
	char *accounts_array = websGetVar(wp, "accounts_array", NULL);
	char *accounts_length = websGetVar(wp, "accounts_length", NULL);
	char word[1024], *next;
	char delim[] = "(&nbsp;)";
	char name[80],psw[80],mod[80];

#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): accounts_array=[%s] accounts_length=[%s]", __FUNCTION__, accounts_array, accounts_length);
#endif
	if(!accounts_array || !accounts_length)		return 0;

	strcpy(name , nvram_safe_get("http_username"));
	strcpy(psw , nvram_safe_get("http_passwd"));
	strcpy(mod , "admin");
	cur += snprintf(cur, buf + sizeof(buf) - cur, "%s$NAME:%03d:%s$PSW:%03d:%s$MOD:%03d:%s",
		cur == buf ? "" : "<&nbsp;>",
		strlen(name), name,
		strlen(psw), psw,
		strlen(mod), mod);

	split(word, accounts_array, next, delim){
		if(sscanf(word, "%s %s %s", mod, psw, name) != 3 )
			continue;

#ifdef MY_DEBUG
		LOG(LOG_DEBUG,"%s(): psw=[%s] mod=[%s] name=[%s]", __FUNCTION__, psw, mod, name);
#endif
		cur += snprintf(cur, buf + sizeof(buf) - cur, "%s$NAME:%03d:%s$PSW:%03d:%s$MOD:%03d:%s",
				cur == buf ? "" : "<&nbsp;>",
				strlen(name), name,
				strlen(psw), psw,
				strlen(mod), mod);
	}
	nvram_set("http_login", buf);
#ifdef TINYLOGIN_SUPPORT
	set_tinylogin_info();
#endif
	return 1;
}

int
save_user_account(webs_t wp)
{
	return validate_user_account(wp);
}

int
ej_http_user_name_get(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret=0, which;
	char *type;
	char word[1024], *next, *accounts;
	char delim[] = "<&nbsp;>";
	char *name,*psw,*mod;

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): type=[%s] which=[%d]",__FUNCTION__, type, which);
#endif
	accounts = nvram_safe_get("current_login_name");

	ret = websWrite(wp,"%s",accounts);
	return ret;
}
#endif

int
ej_http_name_get(int eid, webs_t wp, int argc, char_t **argv)
{
	char *name;
	int ret=0;
#ifdef MULTIPLE_LOGIN_SUPPORT
	int which;
	char *type;
	char word[1024], *next, *accounts;
	char delim[] = "<&nbsp;>";
	char *psw,*mod;

        if (ejArgs(argc, argv, "%s %d", &type, &which) < 2) {
             websError(wp, 400, "Insufficient args\n");
             return -1;
        }
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"%s(): type=[%s] which=[%d]",__FUNCTION__, type, which);
#endif
	accounts = nvram_safe_get("http_login");

	split(word, accounts, next, delim){
		int len = 0;

		if ((name=strstr(word, "$NAME:")) == NULL ||
		    (psw=strstr(word, "$PSW:")) == NULL ||
		    (mod=strstr(word, "$MOD:")) == NULL)
		    continue;

		/* $NAME */
		if (sscanf(name, "$NAME:%3d:", &len) != 1)
		    continue;
		strncpy(name, name + sizeof("$NAME:nnn:") - 1, len);
        	              name[len] = '\0';

	ret = websWrite(wp,"%s",name);
	return ret;
	}
#else
	name = nvram_safe_get("http_username");
	ret = websWrite(wp,"%s",name);
	return ret;
#endif
}