source: src/router/httpd/modules/filters.c @ 7427

Last change on this file since 7427 was 7427, checked in by eko, 6 years ago

small code optim.

File size: 46.2 KB
Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <signal.h>
6
7#include <broadcom.h>
8
9#ifdef FILTER_DEBUG
10extern FILE *debout;
11#define D(a) fprintf(debout,"%s\n",a); fflush(debout);
12#else
13#define D(a)
14#endif
15
16/*
17   Format:
18     filter_rule{1...10}=$STAT:1$NAME:test1$
19        (1=>disable 2=>enable)
20
21   Format:
22     filter_tod{1...10} = hr:min hr:min wday
23     filter_tod_buf{1...10} = sun mon tue wed thu fri sat        //only for web page read
24   Example:
25     Everyday and 24-hour
26     filter_todXX = 0:0 23:59 0-0
27     filter_tod_bufXX = 7       (for web)
28
29     From 9:55 to 22:00 every sun, wed and thu
30     filter_todXX = 9:55 22:00 0,3-4
31     filter_tod_bufXX = 1 0 1 1 0 0 0   (for web)
32
33   Format:
34     filter_ip_grp{1...10} = ip1 ip2 ip3 ip4 ip5 ip6 ip_r1-ipr2 ip_r3-ip_r4
35     filter_ip_mac{1...10} = 00:11:22:33:44:55 00:12:34:56:78:90
36 
37   Format:
38     filter_port=udp:111-222 both:22-33 disable:22-333 tcp:11-22222
39
40   Converting Between AM/PM and 24 Hour Clock:
41    Converting from AM/PM to 24 hour clock:
42     12:59 AM -> 0059    (between 12:00 AM and 12:59 AM, subtract 12 hours)
43     10:00 AM -> 1000    (between 1:00 AM and 12:59 PM, a straight conversion)
44     10:59 PM -> 2259    (between 1:00 PM and 11:59 PM, add 12 hours)
45    Converting from 24 hour clock to AM/PM
46     0059 -> 12:59 AM    (between 0000 and 0059, add 12 hours)
47     0100 -> 1:00  AM    (between 0100 and 1159, straight converion to AM)
48     1259 -> 12:59 PM    (between 1200 and 1259, straight converion to PM)
49     1559 -> 3:59  PM    (between 1300 and 2359, subtract 12 hours)
50     
51*/
52
53int filter_id = 1;
54int day_all = 0, week0 = 0, week1 = 0, week2 = 0, week3 = 0, week4 =
55  0, week5 = 0, week6 = 0;
56int start_week = 0, end_week = 0;
57int time_all = 0, start_hour = 0, start_min = 0, start_time = 0, end_hour =
58  0, end_min = 0, end_time = 0;
59int tod_data_null = 0;
60
61void
62validate_filter_ip_grp (webs_t wp, char *value, struct variable *v)
63{
64  D ("validate_filter_ip_grp");
65  int i = 0;
66  char buf[256] = "";
67  char *ip0, *ip1, *ip2, *ip3, *ip4, *ip5, *ip_range0_0, *ip_range0_1,
68    *ip_range1_0, *ip_range1_1;
69  unsigned char ip[10] = { 0, 0, 0, 0, 0, 0, 0 };
70  struct variable filter_ip_variables[] = {
71  {argv:ARGV ("0", "255")},
72  }, *which;
73  char _filter_ip[] = "filter_ip_grpXXX";
74  //char _filter_rule[] = "filter_ruleXXX";
75  //char _filter_tod[] = "filter_todXXX";
76
77  which = &filter_ip_variables[0];
78
79  ip0 = websGetVar (wp, "ip0", "0");
80  ip1 = websGetVar (wp, "ip1", "0");
81  ip2 = websGetVar (wp, "ip2", "0");
82  ip3 = websGetVar (wp, "ip3", "0");
83  ip4 = websGetVar (wp, "ip4", "0");
84  ip5 = websGetVar (wp, "ip5", "0");
85  ip_range0_0 = websGetVar (wp, "ip_range0_0", "0");
86  ip_range0_1 = websGetVar (wp, "ip_range0_1", "0");
87  ip_range1_0 = websGetVar (wp, "ip_range1_0", "0");
88  ip_range1_1 = websGetVar (wp, "ip_range1_1", "0");
89
90
91  if (!valid_range (wp, ip0, &which[0]) ||
92      !valid_range (wp, ip1, &which[0]) ||
93      !valid_range (wp, ip2, &which[0]) ||
94      !valid_range (wp, ip3, &which[0]) ||
95      !valid_range (wp, ip4, &which[0]) ||
96      !valid_range (wp, ip5, &which[0]) ||
97      !valid_range (wp, ip_range0_0, &which[0]) ||
98      !valid_range (wp, ip_range0_1, &which[0]) ||
99      !valid_range (wp, ip_range1_0, &which[0]) ||
100      !valid_range (wp, ip_range1_0, &which[0]))
101    {
102      error_value = 1;
103      D ("invalid range, return");
104      return;
105    }
106
107  if (atoi (ip0))
108    ip[i++] = atoi (ip0);
109  if (atoi (ip1))
110    ip[i++] = atoi (ip1);
111  if (atoi (ip2))
112    ip[i++] = atoi (ip2);
113  if (atoi (ip3))
114    ip[i++] = atoi (ip3);
115  if (atoi (ip4))
116    ip[i++] = atoi (ip4);
117  if (atoi (ip5))
118    ip[i++] = atoi (ip5);
119
120  if (atoi (ip_range0_0) > atoi (ip_range0_1))
121    SWAP (ip_range0_0, ip_range0_1);
122
123  if (atoi (ip_range1_0) > atoi (ip_range1_1))
124    SWAP (ip_range1_0, ip_range1_1);
125
126
127  sprintf (buf, "%d %d %d %d %d %d %s-%s %s-%s", ip[0], ip[1], ip[2], ip[3],
128           ip[4], ip[5], ip_range0_0, ip_range0_1, ip_range1_0, ip_range1_1);
129
130
131  snprintf (_filter_ip, sizeof (_filter_ip), "filter_ip_grp%s",
132            nvram_safe_get ("filter_id"));
133  nvram_set (_filter_ip, buf);
134
135  //snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
136  //snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
137  //if(nvram_match(_filter_rule, "")){
138  //      nvram_set(_filter_rule, "$STAT:1$NAME:$$");
139  //      nvram_set(_filter_tod, "0:0 23:59 0-6");
140  //}
141  D ("success return");
142
143}
144
145/* Example:
146 * tcp:100-200 udp:210-220 both:250-260
147 */
148
149void
150validate_filter_port (webs_t wp, char *value, struct variable *v)
151{
152  int i;
153  char buf[1000] = "", *cur = buf;
154  struct variable filter_port_variables[] = {
155  {argv:ARGV ("0",
156          "65535")},
157  {argv:ARGV ("0",
158          "65535")},
159  }, *which;
160  D ("validate_filter_port");
161  which = &filter_port_variables[0];
162
163  for (i = 0; i < FILTER_PORT_NUM; i++)
164    {
165      char filter_port[] = "protoXXX";
166      char filter_port_start[] = "startXXX";
167      char filter_port_end[] = "endXXX";
168      char *port, *start, *end;
169      char *temp;
170
171      snprintf (filter_port, sizeof (filter_port), "proto%d", i);
172      snprintf (filter_port_start, sizeof (filter_port_start), "start%d", i);
173      snprintf (filter_port_end, sizeof (filter_port_end), "end%d", i);
174      port = websGetVar (wp, filter_port, NULL);
175      start = websGetVar (wp, filter_port_start, NULL);
176      end = websGetVar (wp, filter_port_end, NULL);
177
178
179      if (!port || !start || !end)
180        continue;
181
182      if (!*start && !*end)
183        continue;
184
185      if ((!strcmp (start, "0") || !strcmp (start, "")) &&
186          (!strcmp (end, "0") || !strcmp (end, "")))
187        continue;
188
189      if (!*start || !*end)
190        {
191          //websWrite(wp, "Invalid <b>%s</b>: must specify a LAN Port Range<br>", v->longname);
192          continue;
193        }
194      if (!valid_range (wp, start, &which[0])
195          || !valid_range (wp, end, &which[1]))
196        {
197          error_value = 1;
198          continue;
199        }
200      if (atoi (start) > atoi (end))
201        {
202          temp = start;
203          start = end;
204          end = temp;
205        }
206      cur += snprintf (cur, buf + sizeof (buf) - cur, "%s%s:%d-%d",
207                       cur == buf ? "" : " ", port, atoi (start), atoi (end));
208    }
209
210
211  nvram_set (v->name, buf);
212  D ("success return");
213}
214
215void
216validate_filter_dport_grp (webs_t wp, char *value, struct variable *v)
217{
218  int i;
219  char buf[1000] = "", *cur = buf;
220  struct variable filter_port_variables[] = {
221  {argv:ARGV ("0",
222          "65535")},
223  {argv:ARGV ("0",
224          "65535")},
225  }, *which;
226  char _filter_port[] = "filter_dport_grpXXX";
227  //char _filter_rule[] = "filter_ruleXXX";
228  //char _filter_tod[] = "filter_todXXX";
229  D ("validate_filter-dport-grp");
230  which = &filter_port_variables[0];
231
232  for (i = 0; i < FILTER_PORT_NUM; i++)
233    {
234      char filter_port[] = "protoXXX";
235      char filter_port_start[] = "startXXX";
236      char filter_port_end[] = "endXXX";
237      char *port, *start, *end;
238      char *temp;
239
240      snprintf (filter_port, sizeof (filter_port), "proto%d", i);
241      snprintf (filter_port_start, sizeof (filter_port_start), "start%d", i);
242      snprintf (filter_port_end, sizeof (filter_port_end), "end%d", i);
243      port = websGetVar (wp, filter_port, NULL);
244      start = websGetVar (wp, filter_port_start, NULL);
245      end = websGetVar (wp, filter_port_end, NULL);
246
247
248      if (!port || !start || !end)
249        continue;
250
251      if (!*start && !*end)
252        continue;
253
254      if ((!strcmp (start, "0") || !strcmp (start, "")) &&
255          (!strcmp (end, "0") || !strcmp (end, "")))
256        continue;
257
258      if (!*start || !*end)
259        {
260          //websWrite(wp, "Invalid <b>%s</b>: must specify a LAN Port Range<br>", v->longname);
261          continue;
262        }
263      if (!valid_range (wp, start, &which[0])
264          || !valid_range (wp, end, &which[1]))
265        {
266          error_value = 1;
267          continue;
268        }
269      if (atoi (start) > atoi (end))
270        {
271          temp = start;
272          start = end;
273          end = temp;
274        }
275      cur += snprintf (cur, buf + sizeof (buf) - cur, "%s%s:%d-%d",
276                       cur == buf ? "" : " ", port, atoi (start), atoi (end));
277    }
278
279  snprintf (_filter_port, sizeof (_filter_port), "filter_dport_grp%s",
280            nvram_safe_get ("filter_id"));
281  nvram_set (_filter_port, buf);
282
283  //snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
284  //snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
285  //if(nvram_match(_filter_rule, "")){
286  //      nvram_set(_filter_rule, "$STAT:1$NAME:$$");
287  //      nvram_set(_filter_tod, "0:0 23:59 0-6");
288  //}
289  D ("success return");
290}
291
292/* Example:
293 * 2 00:11:22:33:44:55 00:11:22:33:44:56
294 */
295
296void
297validate_filter_mac_grp (webs_t wp, char *value, struct variable *v)
298{
299
300  int i;
301  char buf[1000] = "", *cur = buf;
302  char _filter_mac[] = "filter_mac_grpXXX";
303  //char _filter_rule[] = "filter_ruleXXX";
304  //har _filter_tod[] = "filter_todXXX";
305  D ("validate_filter__mac_grp");
306
307  for (i = 0; i < FILTER_MAC_NUM; i++)
308    {
309      char filter_mac[] = "macXXX";
310      char *mac, mac1[20] = "";
311
312
313      snprintf (filter_mac, sizeof (filter_mac), "mac%d", i);
314
315      mac = websGetVar (wp, filter_mac, NULL);
316      if (!mac)
317        continue;
318
319      if (strcmp (mac, "") && strcmp (mac, "00:00:00:00:00:00")
320          && strcmp (mac, "000000000000"))
321        {
322          if (strlen (mac) == 12)
323            {
324              char hex[] = "XX";
325              unsigned char h;
326              while (*mac)
327                {
328                  strncpy (hex, mac, 2);
329                  h = (unsigned char) strtoul (hex, NULL, 16);
330                  if (strlen (mac1))
331                    sprintf (mac1 + strlen (mac1), ":");
332                  sprintf (mac1 + strlen (mac1), "%02X", h);
333                  mac += 2;
334                }
335              mac1[17] = '\0';
336            }
337          else if (strlen (mac) == 17)
338            {
339              strcpy (mac1, mac);
340            }
341          if (!valid_hwaddr (wp, mac1, v))
342            {
343              error_value = 1;
344              continue;
345            }
346        }
347      else
348        {
349          continue;
350        }
351      cur += snprintf (cur, buf + sizeof (buf) - cur, "%s%s",
352                       cur == buf ? "" : " ", mac1);
353    }
354
355
356  snprintf (_filter_mac, sizeof (_filter_mac), "filter_mac_grp%s",
357            nvram_safe_get ("filter_id"));
358  nvram_set (_filter_mac, buf);
359
360  //snprintf(_filter_rule, sizeof(_filter_rule), "filter_rule%s", nvram_safe_get("filter_id"));
361  //snprintf(_filter_tod, sizeof(_filter_tod), "filter_tod%s", nvram_safe_get("filter_id"));
362  //if(nvram_match(_filter_rule, "")){
363  //      nvram_set(_filter_rule, "$STAT:1$NAME:$$");
364  //      nvram_set(_filter_tod, "0:0 23:59 0-6");
365  //}
366  D ("success return");
367}
368
369
370/* Example:
371 * 100-200 250-260 (ie. 192.168.1.100-192.168.1.200 192.168.1.250-192.168.1.260)
372 */
373
374char *
375filter_ip_get (char *type, int which)
376{
377  static char word[256];
378  char *start, *end, *wordlist, *next;
379  int temp;
380  char filter_ip[] = "filter_ip_grpXXX";
381  D ("filter_ip_get");
382  snprintf (filter_ip, sizeof (filter_ip), "filter_ip_grp%s",
383            nvram_safe_get ("filter_id"));
384
385  wordlist = nvram_safe_get (filter_ip);
386  if (!wordlist)
387    return "0";
388
389  temp = which;
390
391  foreach (word, wordlist, next)
392  {
393    if (which-- == 0)
394      {
395        if (temp == 6)
396          {
397            end = word;
398            start = strsep (&end, "-");
399            if (!strcmp (type, "ip_range0_0"))
400              return start;
401            else
402              return end;
403          }
404        else if (temp == 7)
405          {
406            end = word;
407            start = strsep (&end, "-");
408            if (!strcmp (type, "ip_range1_0"))
409              return start;
410            else
411              return end;
412          }
413        D ("return word");
414        return word;
415      }
416  }
417  D ("return zero");
418  return "0";
419}
420
421/* Example:
422 * tcp:100-200 udp:210-220 both:250-260
423 */
424
425char *
426filter_dport_get (char *type, int which)
427{
428  static char word[256];
429  char *wordlist, *next;
430  char *start, *end, *proto;
431  int temp;
432  char name[] = "filter_dport_grpXXX";
433
434  sprintf (name, "filter_dport_grp%s", nvram_safe_get ("filter_id"));
435  wordlist = nvram_safe_get (name);
436  temp = which;
437  D ("filter dport get");
438  foreach (word, wordlist, next)
439  {
440    if (which-- == 0)
441      {
442        start = word;
443        proto = strsep (&start, ":");
444        end = start;
445        start = strsep (&end, "-");
446        if (!strcmp (type, "disable"))
447          {
448            if (!strcmp (proto, "disable"))
449              return "selected";
450            else
451              return " ";
452          }
453        else if (!strcmp (type, "both"))
454          {
455            if (!strcmp (proto, "both"))
456              return "selected";
457            else
458              return " ";
459          }
460        else if (!strcmp (type, "tcp"))
461          {
462            if (!strcmp (proto, "tcp"))
463              return "selected";
464            else
465              return " ";
466          }
467        else if (!strcmp (type, "udp"))
468          {
469            if (!strcmp (proto, "udp"))
470              return "selected";
471            else
472              return " ";
473          }
474        else if (!strcmp (type, "l7"))
475          {
476            if (!strcmp (proto, "l7"))
477              return "selected";
478            else
479              return " ";
480          }
481        else if (!strcmp (type, "start"))
482          return start;
483        else if (!strcmp (type, "end"))
484          return end;
485      }
486  }
487  D ("check type and return");
488  if (!strcmp (type, "start") || !strcmp (type, "end"))
489    return "0";
490  else
491    return "";
492}
493
494void
495ej_filter_dport_get (webs_t wp, int argc, char_t ** argv)
496{
497  int which;
498  char *type;
499  D ("ej filter dport get");
500  if (ejArgs (argc, argv, "%s %d", &type, &which) < 2)
501    {
502      websError (wp, 400, "Insufficient args\n");
503      D ("bad value");
504      return;
505    }
506
507  websWrite (wp, "%s", filter_dport_get (type, which));
508  D ("good value");
509
510  return;
511
512}
513
514/* Example:
515 * tcp:100-200 udp:210-220 both:250-260
516 */
517
518char *
519filter_port_get (char *type, int which)
520{
521  static char word[256];
522  char *wordlist, *next;
523  char *start, *end, *proto;
524  int temp;
525  D ("filter port get");
526  wordlist = nvram_safe_get ("filter_port");
527  temp = which;
528
529  foreach (word, wordlist, next)
530  {
531    if (which-- == 0)
532      {
533        start = word;
534        proto = strsep (&start, ":");
535        end = start;
536        start = strsep (&end, "-");
537        if (!strcmp (type, "disable"))
538          {
539            if (!strcmp (proto, "disable"))
540              return "selected";
541            else
542              return " ";
543          }
544        else if (!strcmp (type, "both"))
545          {
546            if (!strcmp (proto, "both"))
547              return "selected";
548            else
549              return " ";
550          }
551        else if (!strcmp (type, "tcp"))
552          {
553            if (!strcmp (proto, "tcp"))
554              return "selected";
555            else
556              return " ";
557          }
558        else if (!strcmp (type, "udp"))
559          {
560            if (!strcmp (proto, "udp"))
561              return "selected";
562            else
563              return " ";
564          }
565        else if (!strcmp (type, "start"))
566          return start;
567        else if (!strcmp (type, "end"))
568          return end;
569      }
570  }
571  D ("return type");
572  if (!strcmp (type, "start") || !strcmp (type, "end"))
573    return "0";
574  else
575    return "";
576}
577
578void
579ej_filter_port_get (webs_t wp, int argc, char_t ** argv)
580{
581  int which;
582  char *type;
583  D ("ej filter port get");
584  if (ejArgs (argc, argv, "%s %d", &type, &which) < 2)
585    {
586      websError (wp, 400, "Insufficient args\n");
587      D ("bad value");
588      return;
589    }
590
591
592  websWrite (wp, "%s", filter_port_get (type, which));
593
594  D ("good value");
595  return;
596
597}
598
599/* Example:
600 * 00:11:22:33:44:55 00:11:22:33:44:56
601 */
602
603char *
604filter_mac_get (int which)
605{
606  static char word[256];
607  char *wordlist, *next;
608  char *mac;
609  int temp;
610  char filter_mac[] = "filter_mac_grpXXX";
611  D ("filter mac get");
612  snprintf (filter_mac, sizeof (filter_mac), "filter_mac_grp%s",
613            nvram_safe_get ("filter_id"));
614
615  wordlist = nvram_safe_get (filter_mac);
616  if (!wordlist)
617    return "";
618
619  temp = which;
620
621  foreach (word, wordlist, next)
622  {
623    if (which-- == 0)
624      {
625        mac = word;
626        D ("return mac");
627        return mac;
628      }
629  }
630  D ("return zero mac");
631  return "00:00:00:00:00:00";
632}
633
634
635void
636ej_filter_ip_get (webs_t wp, int argc, char_t ** argv)
637{
638  int which;
639  char *type;
640  D ("ej-filter ip get");
641  if (ejArgs (argc, argv, "%s %d", &type, &which) < 2)
642    {
643
644      websError (wp, 400, "Insufficient args\n");
645      D ("BAD VALUE");
646      return;
647    }
648
649  websWrite (wp, "%s", filter_ip_get (type, which));
650
651  D ("good value");
652  return;
653}
654
655
656void
657ej_filter_mac_get (webs_t wp, int argc, char_t ** argv)
658{
659  int which;
660  D ("ej filter mac get");
661  if (ejArgs (argc, argv, "%d", &which) < 1)
662    {
663      websError (wp, 400, "Insufficient args\n");
664      D ("bad value");
665      return;
666    }
667
668  websWrite (wp, "%s", filter_mac_get (which));
669  D ("good value");
670  return;
671}
672
673#if 0
674void
675validate_filter_ip_grp (webs_t wp, char *value, struct variable *v)
676{
677  char *filter_ip_grp_1, *filter_ip_grp_2;
678
679  filter_ip_grp_1 = websGetVar (wp, "filter_ip_grp_1", NULL);
680  filter_ip_grp_2 = websGetVar (wp, "filter_ip_grp_2", NULL);
681
682  if (!filter_ip_grp_1)
683    return;
684
685  if (!strcmp (filter_ip_grp_1, "0"))
686    nvram_set (v->name, "0");
687  else if (!strcmp (filter_ip_grp_1, "other"))
688    {
689      if (!filter_ip_grp_2)
690        return;
691      nvram_set (v->name, filter_ip_grp_2);
692    }
693}
694
695void
696validate_filter_mac_grp (webs_t wp, char *value, struct variable *v)
697{
698  char *filter_mac_grp_1, *filter_mac_grp_2;
699
700  filter_mac_grp_1 = websGetVar (wp, "filter_mac_grp_1", NULL);
701  filter_mac_grp_2 = websGetVar (wp, "filter_mac_grp_2", NULL);
702
703
704  if (!filter_mac_grp_1)
705    return;
706
707  if (!strcmp (filter_mac_grp_1, "0"))
708    nvram_set (v->name, "0");
709  else if (!strcmp (filter_mac_grp_1, "other"))
710    {
711      if (!filter_mac_grp_2)
712        return;
713      nvram_set (v->name, filter_mac_grp_2);
714    }
715}
716#endif
717
718/*  Format: url0=www.kimo.com.tw, ...
719 *          keywd0=sex, ...
720 */
721void
722validate_filter_web (webs_t wp, char *value, struct variable *v)
723{
724  int i;
725  char buf[1000] = "", *cur = buf;
726  char buf1[1000] = "", *cur1 = buf1;
727  char filter_host[] = "filter_web_hostXXX";
728  char filter_url[] = "filter_web_urlXXX";
729  D ("validate_filter_web");
730  /* Handle Website Blocking by URL Address */
731  for (i = 0; i < 9; i++)
732    {
733      char filter_host[] = "hostXXX";
734      char *host;
735
736      snprintf (filter_host, sizeof (filter_host), "host%d", i);
737      host = websGetVar (wp, filter_host, "");
738
739      if (!strcmp (host, ""))
740        continue;
741
742      cur += snprintf (cur, buf + sizeof (buf) - cur, "%s%s",
743                       cur == buf ? "" : "<&nbsp;>", host);
744    }
745
746  if (strcmp (buf, ""))
747    strcat (buf, "<&nbsp;>");
748
749  snprintf (filter_host, sizeof (filter_host), "filter_web_host%s",
750            nvram_safe_get ("filter_id"));
751  nvram_set (filter_host, buf);
752
753  /* Handle Website Blocking by Keyword */
754  for (i = 0; i < 8; i++)
755    {
756      char filter_url[] = "urlXXX";
757      char *url;
758
759      snprintf (filter_url, sizeof (filter_url), "url%d", i);
760      url = websGetVar (wp, filter_url, "");
761
762      if (!strcmp (url, ""))
763        continue;
764
765      cur1 += snprintf (cur1, buf1 + sizeof (buf1) - cur1, "%s%s",
766                        cur1 == buf1 ? "" : "<&nbsp;>", url);
767    }
768  if (strcmp (buf1, ""))
769    strcat (buf1, "<&nbsp;>");
770
771  snprintf (filter_url, sizeof (filter_url), "filter_web_url%s",
772            nvram_safe_get ("filter_id"));
773  nvram_set (filter_url, buf1);
774  D ("everything okay");
775}
776
777int
778validate_filter_tod (webs_t wp)
779{
780  char buf[256] = "";
781  char tod_buf[20];
782  struct variable filter_tod_variables[] = {
783  {argv:ARGV ("20")},
784  {argv:ARGV ("0", "1", "2")},
785
786  }, *which;
787  D ("validate filter tod");
788
789  char *day_all, *week0, *week1, *week2, *week3, *week4, *week5, *week6;
790  char *time_all, *start_hour, *start_min, *end_hour, *end_min;
791  int _start_hour, _start_min, _end_hour, _end_min;
792  char time[20];
793  int week[7];
794  int i, flag = -1, dash = 0;
795  char filter_tod[] = "filter_todXXX";
796  char filter_tod_buf[] = "filter_tod_bufXXX";
797
798  which = &filter_tod_variables[0];
799
800  day_all = websGetVar (wp, "day_all", "0");
801  week0 = websGetVar (wp, "week0", "0");
802  week1 = websGetVar (wp, "week1", "0");
803  week2 = websGetVar (wp, "week2", "0");
804  week3 = websGetVar (wp, "week3", "0");
805  week4 = websGetVar (wp, "week4", "0");
806  week5 = websGetVar (wp, "week5", "0");
807  week6 = websGetVar (wp, "week6", "0");
808  time_all = websGetVar (wp, "time_all", "0");
809  start_hour = websGetVar (wp, "start_hour", "0");
810  start_min = websGetVar (wp, "start_min", "0");
811//  start_time = websGetVar (wp, "start_time", "0");
812  end_hour = websGetVar (wp, "end_hour", "0");
813  end_min = websGetVar (wp, "end_min", "0");
814//  end_time = websGetVar (wp, "end_time", "0");
815
816  //if(atoi(time_all) == 0)
817  //      if(!start_hour || !start_min || !start_time || !end_hour || !end_min || !end_time)
818  //              return 1;
819
820  if (atoi (day_all) == 1)
821    {
822      strcpy (time, "0-6");
823      strcpy (tod_buf, "7");
824    }
825  else
826    {
827      week[0] = atoi (week0);
828      week[1] = atoi (week1);
829      week[2] = atoi (week2);
830      week[3] = atoi (week3);
831      week[4] = atoi (week4);
832      week[5] = atoi (week5);
833      week[6] = atoi (week6);
834      strcpy (time, "");
835
836      for (i = 0; i < 7; i++)
837        {
838          if (week[i] == 1)
839            {
840              if (i == 6)
841                {
842                  if (dash == 0 && flag == 1)
843                    sprintf (time + strlen (time), "%c", '-');
844                  sprintf (time + strlen (time), "%d", i);
845                }
846              else if (flag == 1 && dash == 0)
847                {
848                  sprintf (time + strlen (time), "%c", '-');
849                  dash = 1;
850                }
851              else if (dash == 0)
852                {
853                  sprintf (time + strlen (time), "%d", i);
854                  flag = 1;
855                  dash = 0;
856                }
857            }
858          else
859            {
860              if (!strcmp (time, ""))
861                continue;
862              if (dash == 1)
863                sprintf (time + strlen (time), "%d", i - 1);
864              if (flag != 0)
865                sprintf (time + strlen (time), "%c", ',');
866              flag = 0;
867              dash = 0;
868            }
869        }
870      if (time[strlen (time) - 1] == ',')
871        time[strlen (time) - 1] = '\0';
872
873      snprintf (tod_buf, sizeof (tod_buf), "%s %s %s %s %s %s %s", week0,
874                week1, week2, week3, week4, week5, week6);
875    }
876  if (atoi (time_all) == 1)
877    {
878      _start_hour = 0;
879      _start_min = 0;
880      _end_hour = 23;
881      _end_min = 59;
882    }
883  else
884    {
885      _start_hour = atoi (start_hour);
886      _start_min = atoi (start_min);
887      _end_hour = atoi (end_hour);
888      _end_min = atoi (end_min);
889    }
890
891  sprintf (buf, "%d:%d %d:%d %s", _start_hour, _start_min, _end_hour,
892           _end_min, time);
893  snprintf (filter_tod, sizeof (filter_tod), "filter_tod%s",
894            nvram_safe_get ("filter_id"));
895  snprintf (filter_tod_buf, sizeof (filter_tod_buf), "filter_tod_buf%s",
896            nvram_safe_get ("filter_id"));
897
898  nvram_set (filter_tod, buf);
899  nvram_set (filter_tod_buf, tod_buf);
900  D ("everything okay");
901  return 0;
902
903}
904
905int
906delete_policy (webs_t wp, int which)
907{
908  char filter_rule[] = "filter_ruleXXX";
909  char filter_tod[] = "filter_todXXX";
910  char filter_tod_buf[] = "filter_tod_bufXXX";
911  char filter_host[] = "filter_web_hostXXX";
912  char filter_url[] = "filter_web_urlXXX";
913  char filter_ip_grp[] = "filter_ip_grpXXX";
914  char filter_mac_grp[] = "filter_mac_grpXXX";
915  char filter_port_grp[] = "filter_port_grpXXX";
916  char filter_dport_grp[] = "filter_dport_grpXXX";
917  D ("delete policy");
918
919  snprintf (filter_rule, sizeof (filter_rule), "filter_rule%d", which);
920  snprintf (filter_tod, sizeof (filter_tod), "filter_tod%d", which);
921  snprintf (filter_tod_buf, sizeof (filter_tod_buf), "filter_tod_buf%d",
922            which);
923  snprintf (filter_host, sizeof (filter_host), "filter_web_host%d", which);
924  snprintf (filter_url, sizeof (filter_url), "filter_web_url%d", which);
925  snprintf (filter_ip_grp, sizeof (filter_ip_grp), "filter_ip_grp%d", which);
926  snprintf (filter_mac_grp, sizeof (filter_mac_grp), "filter_mac_grp%d",
927            which);
928  snprintf (filter_port_grp, sizeof (filter_port_grp), "filter_port_grp%d",
929            which);
930  snprintf (filter_dport_grp, sizeof (filter_dport_grp), "filter_dport_grp%d",
931            which);
932
933  nvram_set (filter_rule, "");
934  nvram_set (filter_tod, "");
935  nvram_set (filter_tod_buf, "");
936  nvram_set (filter_host, "");
937  nvram_set (filter_url, "");
938  nvram_set (filter_ip_grp, "");
939  nvram_set (filter_mac_grp, "");
940  nvram_set (filter_port_grp, "");
941  nvram_set (filter_dport_grp, "");
942  D ("okay");
943  return 1;
944}
945
946int
947single_delete_policy (webs_t wp)
948{
949  int ret = 0;
950  char *id = nvram_safe_get ("filter_id");
951  D ("single delete policy");
952  ret = delete_policy (wp, atoi (id));
953  D ("okay");
954  return ret;
955}
956
957int
958summary_delete_policy (webs_t wp)
959{
960  int i, ret = 0;
961  D ("summary delete policy");
962  for (i = 1; i <= 10; i++)
963    {
964      char filter_sum[] = "sumXXX";
965      char *sum;
966      snprintf (filter_sum, sizeof (filter_sum), "sum%d", i);
967      sum = websGetVar (wp, filter_sum, NULL);
968      if (sum)
969        ret += delete_policy (wp, i);
970    }
971  D ("okay");
972  return ret;
973}
974
975int
976save_policy (webs_t wp)
977{
978  char *f_id, *f_name, *f_status, *f_status2;
979  char buf[256] = "";
980  struct variable filter_variables[] = {
981  {argv:ARGV ("1", "10")},
982  {argv:ARGV ("0", "1", "2")},
983  {argv:ARGV ("deny", "allow")},
984
985
986  }, *which;
987  char filter_buf[] = "filter_ruleXXX";
988  D ("save policy");
989  which = &filter_variables[0];
990  f_id = websGetVar (wp, "f_id", NULL);
991  f_name = websGetVar (wp, "f_name", NULL);
992  f_status = websGetVar (wp, "f_status", NULL); // 0=>Disable / 1,2=>Enable
993  f_status2 = websGetVar (wp, "f_status2", NULL);       // deny=>Deny / allow=>Allow
994  if (!f_id || !f_name || !f_status || !f_status2)
995    {
996      error_value = 1;
997      D ("invalid");
998      return -1;
999    }
1000  if (!valid_range (wp, f_id, &which[0]))
1001    {
1002      error_value = 1;
1003      D ("invalid");
1004      return -1;
1005    }
1006  if (!valid_choice (wp, f_status, &which[1]))
1007    {
1008      error_value = 1;
1009      D ("invalid");
1010      return -1;
1011    }
1012  if (!valid_choice (wp, f_status2, &which[2]))
1013    {
1014      error_value = 1;
1015      D ("invalid");
1016      return -1;
1017    }
1018
1019  validate_filter_tod (wp);
1020
1021  snprintf (filter_buf, sizeof (filter_buf), "filter_rule%s",
1022            nvram_safe_get ("filter_id"));
1023
1024  // Add $DENY to decide that users select Allow or Deny, if status is Disable    // 2003/10/21
1025  snprintf (buf, sizeof (buf), "$STAT:%s$NAME:%s$DENY:%d$$", f_status, f_name,
1026            !strcmp (f_status2, "deny") ? 1 : 0);
1027
1028  nvram_set (filter_buf, buf);
1029  D ("okay");
1030  return 0;
1031}
1032
1033void
1034validate_filter_policy (webs_t wp, char *value, struct variable *v)
1035{
1036  char *f_id = websGetVar (wp, "f_id", NULL);
1037  D ("validate filter policy");
1038  if (f_id)
1039    nvram_set ("filter_id", f_id);
1040  else
1041    nvram_set ("filter_id", "1");
1042
1043  save_policy (wp);
1044  D ("okay");
1045}
1046
1047/*   Format: 21:21:tcp:FTP(&nbsp;)500:1000:both:TEST1
1048 *
1049 */
1050
1051int
1052validate_services_port (webs_t wp)
1053{
1054  char buf[8192] = "", services[8192] = "", *cur = buf, *svcs = NULL;
1055  char *services_array = websGetVar (wp, "services_array0", NULL);
1056//  char *services_length = websGetVar (wp, "services_length0", NULL);
1057  char word[1025], *next;
1058  char delim[] = "(&nbsp;)";
1059  char var[32] = "";
1060  int index = 0;
1061  D ("validate services port");
1062//printf("services_array: %s\n", services_array);
1063
1064//printf("services_length: %s\n", services_length);
1065
1066//      if(!services_array || !services_length)         return 0;
1067
1068  do
1069    {
1070      snprintf (var, 31, "services_array%d", index++);
1071      svcs = websGetVar (wp, var, NULL);
1072      if (svcs)
1073        strcat (services, svcs);
1074//      printf ("services_array%d: %s\n", index, svcs);
1075
1076    }
1077  while (svcs);
1078
1079  services_array = services;
1080
1081  split (word, services_array, next, delim)
1082  {
1083    int from, to, proto;
1084    char name[80];
1085
1086    if (sscanf (word, "%d:%d:%d:%s", &from, &to, &proto, name) != 4)
1087      continue;
1088
1089    cur +=
1090      snprintf (cur, buf + sizeof (buf) - cur,
1091                "%s$NAME:%03d:%s$PROT:%03d:%s$PORT:%03d:%d:%d",
1092                cur == buf ? "" : "<&nbsp;>", strlen (name), name,
1093                strlen (num_to_protocol (proto)), num_to_protocol (proto),
1094                (int) (get_int_len (from) + get_int_len (to) + strlen (":")),
1095                from, to);
1096  }
1097
1098  /* segment filter_services into <= 1024 byte lengths */
1099  cur = buf;
1100  index = 0;
1101  while (strlen (cur) >= 1024)
1102    {
1103      snprintf (var, 31, "filter_services%d", index);
1104      memcpy (word, cur, 1024);
1105      word[1025] = 0;
1106      nvram_set (var, word);
1107      cur += 1024;
1108      index++;
1109    }
1110
1111  if (strlen (cur) > 0)
1112    {
1113      snprintf (var, 31, "filter_services%d", index);
1114      nvram_set (var, cur);
1115    }
1116  D ("okay");
1117  return 1;
1118}
1119
1120int
1121save_services_port (webs_t wp)
1122{
1123  int val = validate_services_port (wp);
1124  char *value = websGetVar (wp, "action", "");
1125  if (!strcmp (value, "ApplyTake"))
1126    {
1127      nvram_commit ();
1128      nvram_set ("action_service", "index");
1129      service_restart ();
1130    }
1131  return val;
1132}
1133
1134void
1135validate_blocked_service (webs_t wp, char *value, struct variable *v)
1136{
1137  int i;
1138  char buf[1000] = "", *cur = buf;
1139  char port_grp[] = "filter_port_grpXXX";
1140  D ("validate_blocked_service");
1141  for (i = 0; i < BLOCKED_SERVICE_NUM; i++)
1142    {
1143      char blocked_service[] = "blocked_serviceXXX";
1144      char *service;
1145      snprintf (blocked_service, sizeof (blocked_service),
1146                "blocked_service%d", i);
1147      service = websGetVar (wp, blocked_service, NULL);
1148      if (!service || !strcmp (service, "None"))
1149        continue;
1150
1151      cur +=
1152        snprintf (cur, buf + sizeof (buf) - cur, "%s%s", service, "<&nbsp;>");
1153      //cur == buf ? "" : "<&nbsp;>", service);
1154    }
1155
1156  snprintf (port_grp, sizeof (port_grp), "filter_port_grp%s",
1157            nvram_safe_get ("filter_id"));
1158  nvram_set (port_grp, buf);
1159  D ("right");
1160}
1161
1162/*
1163validates the p2p catchall filter
1164*/
1165void
1166validate_catchall (webs_t wp, char *value, struct variable *v)
1167{
1168  char *p2p;
1169  char port_grp[] = "filter_p2p_grpXXX";
1170  p2p = websGetVar (wp, "filter_p2p", NULL);
1171  if (p2p)
1172    {
1173      snprintf (port_grp, sizeof (port_grp), "filter_p2p_grp%s",
1174                nvram_safe_get ("filter_id"));
1175      nvram_set (port_grp, p2p);
1176    }
1177
1178  return;
1179}
1180
1181
1182void
1183ej_filter_policy_select (webs_t wp, int argc, char_t ** argv)
1184{
1185  int i;
1186  D ("ej policy select");
1187  for (i = 1; i <= 10; i++)
1188    {
1189      char filter[] = "filter_ruleXXX";
1190      char *data = "";
1191      char name[50] = "";
1192      snprintf (filter, sizeof (filter), "filter_rule%d", i);
1193      data = nvram_safe_get (filter);
1194
1195      if (data && strcmp (data, ""))
1196        {
1197          find_match_pattern (name, sizeof (name), data, "$NAME:", ""); // get name value
1198        }
1199      websWrite (wp, "<option value=%d %s>%d ( %s ) </option>\n",
1200                 i,
1201                 (atoi (nvram_safe_get ("filter_id")) ==
1202                  i ? "selected=\"selected\" " : ""), i, name);
1203    }
1204  D ("okay");
1205  return;
1206}
1207
1208
1209void
1210ej_filter_policy_get (webs_t wp, int argc, char_t ** argv)
1211{
1212
1213  char *type, *part;
1214
1215  D ("ej filter policy get");
1216  if (ejArgs (argc, argv, "%s %s", &type, &part) < 2)
1217    {
1218      websError (wp, 400, "Insufficient args\n");
1219      return;
1220    }
1221
1222  if (!strcmp (type, "f_id"))
1223    {
1224      websWrite (wp, "%s", nvram_safe_get ("filter_id"));
1225    }
1226  else if (!strcmp (type, "f_name"))
1227    {
1228      char name[50] = "";
1229      char filter[] = "filter_ruleXXX";
1230      char *data = "";
1231      snprintf (filter, sizeof (filter), "filter_rule%s",
1232                nvram_safe_get ("filter_id"));
1233      data = nvram_safe_get (filter);
1234      if (strcmp (data, ""))
1235        {
1236          find_match_pattern (name, sizeof (name), data, "$NAME:", ""); // get name value
1237          websWrite (wp, "%s", name);
1238        }
1239    }
1240  else if (!strcmp (type, "f_status"))
1241    {
1242      char status[50] = "", deny[50] = "";
1243      char filter[] = "filter_ruleXXX";
1244      char *data = "";
1245      snprintf (filter, sizeof (filter), "filter_rule%s",
1246                nvram_safe_get ("filter_id"));
1247      data = nvram_safe_get (filter);
1248      if (strcmp (data, ""))
1249        {                       // have data
1250          find_match_pattern (status, sizeof (status), data, "$STAT:", "1");    // get status value
1251          find_match_pattern (deny, sizeof (deny), data, "$DENY:", ""); // get deny value
1252          if (!strcmp (deny, ""))
1253            {                   // old format
1254              if (!strcmp (status, "0") || !strcmp (status, "1"))
1255                strcpy (deny, "1");     // Deny
1256              else
1257                strcpy (deny, "0");     // Allow
1258            }
1259#if 0
1260          if (!strcmp (part, "disable"))
1261            {
1262              if (!strcmp (status, "1"))
1263                websWrite (wp, "checked=\"checked\" ");
1264            }
1265          else if (!strcmp (part, "enable"))
1266            {
1267              if (!strcmp (status, "2"))
1268                websWrite (wp, "checked=\"checked\" ");
1269            }
1270#endif
1271          if (!strcmp (part, "disable"))
1272            {
1273              if (!strcmp (status, "0"))
1274                websWrite (wp, "checked=\"checked\" ");
1275            }
1276          else if (!strcmp (part, "enable"))
1277            {
1278              if (strcmp (status, "0"))
1279                websWrite (wp, "checked=\"checked\" ");
1280            }
1281          else if (!strcmp (part, "deny"))
1282            {
1283              if (!strcmp (deny, "1"))
1284                websWrite (wp, "checked=\"checked\" ");
1285            }
1286          else if (!strcmp (part, "allow"))
1287            {
1288              if (!strcmp (deny, "0"))
1289                websWrite (wp, "checked=\"checked\" ");
1290            }
1291          else if (!strcmp (part, "onload_status"))
1292            {
1293              if (!strcmp (deny, "1"))
1294                websWrite (wp, "deny");
1295              else
1296                websWrite (wp, "allow");
1297
1298            }
1299          else if (!strcmp (part, "init"))
1300            {
1301              if (!strcmp (status, "1"))
1302                websWrite (wp, "disable");
1303              else if (!strcmp (status, "2"))
1304                websWrite (wp, "enable");
1305              else
1306                websWrite (wp, "disable");
1307            }
1308        }
1309      else
1310        {                       // no data
1311          if (!strcmp (part, "disable"))
1312            websWrite (wp, "checked=\"checked\" ");
1313          else if (!strcmp (part, "allow"))     // default policy is allow, 2003-10-21
1314            websWrite (wp, "checked=\"checked\" ");
1315          else if (!strcmp (part, "onload_status"))     // default policy is allow, 2003-10-21
1316            websWrite (wp, "allow");
1317          else if (!strcmp (part, "init"))
1318            websWrite (wp, "disable");
1319        }
1320    }
1321  D ("okay");
1322  return;
1323}
1324
1325int
1326filter_tod_init (int which)
1327{
1328  int ret;
1329  char *tod_data, *tod_buf_data;
1330  char filter_tod[] = "filter_todXXX";
1331  char filter_tod_buf[] = "filter_tod_bufXXX";
1332  char temp[3][20];
1333
1334  tod_data_null = 0;
1335  day_all = week0 = week1 = week2 = week3 = week4 = week5 = week6 = 0;
1336  time_all = start_hour = start_min = start_time = end_hour = end_min =
1337    end_time = 0;
1338  start_week = end_week = 0;
1339  D ("filter tod init");
1340  snprintf (filter_tod, sizeof (filter_tod), "filter_tod%d", which);
1341  snprintf (filter_tod_buf, sizeof (filter_tod_buf), "filter_tod_buf%d",
1342            which);
1343
1344  /* Parse filter_tod{1...10} */
1345  tod_data = nvram_safe_get (filter_tod);
1346  if (!tod_data)
1347    return -1;                  // no data
1348  if (strcmp (tod_data, ""))
1349    {
1350      sscanf (tod_data, "%s %s %s", temp[0], temp[1], temp[2]);
1351      sscanf (temp[0], "%d:%d", &start_hour, &start_min);
1352      sscanf (temp[1], "%d:%d", &end_hour, &end_min);
1353      ret = sscanf (temp[2], "%d-%d", &start_week, &end_week);
1354      if (ret == 1)
1355        end_week = start_week;
1356
1357      if (start_hour == 0 && start_min == 0 && end_hour == 23
1358          && end_min == 59)
1359        {                       // 24 Hours
1360          time_all = 1;
1361          start_hour = end_hour = 0;
1362          start_min = start_time = end_min = end_time = 0;
1363        }
1364/*      else
1365        {                       // check AM or PM
1366          time_all = 0;
1367          if (start_hour > 11)
1368            {
1369              start_hour = start_hour - 12;
1370              start_time = 1;
1371            }
1372          if (end_hour > 11)
1373            {
1374              end_hour = end_hour - 12;
1375              end_time = 1;
1376            }
1377        } */
1378    }
1379  else
1380    {                           // default Everyday and 24 Hours
1381      tod_data_null = 1;
1382      day_all = 1;
1383      time_all = 1;
1384    }
1385
1386  if (tod_data_null == 0)
1387    {
1388      /* Parse filter_tod_buf{1...10} */
1389      tod_buf_data = nvram_safe_get (filter_tod_buf);
1390      if (!strcmp (tod_buf_data, "7"))
1391        {
1392          day_all = 1;
1393        }
1394      else if (strcmp (tod_buf_data, ""))
1395        {
1396          sscanf (tod_buf_data, "%d %d %d %d %d %d %d", &week0, &week1,
1397                  &week2, &week3, &week4, &week5, &week6);
1398          day_all = 0;
1399        }
1400    }
1401  D ("okay");
1402  return 0;
1403}
1404
1405void
1406ej_filter_tod_get (webs_t wp, int argc, char_t ** argv)
1407{
1408  char *type;
1409  int i;
1410  D ("ej-filter-tod_get");
1411  if (ejArgs (argc, argv, "%s", &type) < 1)
1412    {
1413      websError (wp, 400, "Insufficient args\n");
1414      return;
1415    }
1416
1417  filter_tod_init (atoi (nvram_safe_get ("filter_id")));
1418
1419  if (!strcmp (type, "day_all_init"))
1420    {
1421      if (day_all == 0)
1422        websWrite (wp, "1");
1423      else
1424        websWrite (wp, "0");
1425    }
1426  else if (!strcmp (type, "time_all_init"))
1427    {
1428      if (time_all == 0)
1429        websWrite (wp, "1");
1430      else
1431        websWrite (wp, "0");
1432    }
1433  else if (!strcmp (type, "day_all"))
1434    {
1435      websWrite (wp, "%s", day_all == 1 ? "checked=\"checked\" " : "");
1436    }
1437  else if (!strcmp (type, "start_week"))
1438    {
1439      websWrite (wp, "%d", start_week);
1440    }
1441  else if (!strcmp (type, "end_week"))
1442    {
1443      websWrite (wp, "%d", end_week);
1444    }
1445  else if (!strcmp (type, "week0"))
1446    {                           // Sun
1447      websWrite (wp, "%s", week0 == 1 ? "checked=\"checked\" " : "");
1448    }
1449  else if (!strcmp (type, "week1"))
1450    {                           // Mon
1451      websWrite (wp, "%s", week1 == 1 ? "checked=\"checked\" " : "");
1452    }
1453  else if (!strcmp (type, "week2"))
1454    {                           // Tue
1455      websWrite (wp, "%s", week2 == 1 ? "checked=\"checked\" " : "");
1456    }
1457  else if (!strcmp (type, "week3"))
1458    {                           // Wed
1459      websWrite (wp, "%s", week3 == 1 ? "checked=\"checked\" " : "");
1460    }
1461  else if (!strcmp (type, "week4"))
1462    {                           // Thu
1463      websWrite (wp, "%s", week4 == 1 ? "checked=\"checked\" " : "");
1464    }
1465  else if (!strcmp (type, "week5"))
1466    {                           // Fri
1467      websWrite (wp, "%s", week5 == 1 ? "checked=\"checked\" " : "");
1468    }
1469  else if (!strcmp (type, "week6"))
1470    {                           // Sat
1471      websWrite (wp, "%s", week6 == 1 ? "checked=\"checked\" " : "");
1472    }
1473  else if (!strcmp (type, "time_all_en"))
1474    {                           // for linksys
1475      websWrite (wp, "%s", time_all == 1 ? "checked=\"checked\" " : "");
1476    }
1477  else if (!strcmp (type, "time_all_dis"))
1478    {                           // for linksys
1479      websWrite (wp, "%s", time_all == 0 ? "checked=\"checked\" " : "");
1480    }
1481  else if (!strcmp (type, "time_all"))
1482    {
1483      websWrite (wp, "%s", time_all == 1 ? "checked=\"checked\" " : "");
1484    }
1485  else if (!strcmp (type, "start_hour_24"))
1486    {                           // 00 -> 23
1487      for (i = 0; i < 24; i++)
1488        {
1489
1490          websWrite (wp, "<option value=%d %s>%d</option>\n", i,
1491                     i == start_hour ? "selected=\"selected\" " : "", i);
1492        }
1493    }
1494  else if (!strcmp (type, "start_min_1"))
1495    {                           // 0 1 2 3 4 .... -> 58 59
1496      for (i = 0; i < 60; i++)
1497        {
1498
1499          websWrite (wp, "<option value=%02d %s>%02d</option>\n", i,
1500                     i == start_min ? "selected=\"selected\" " : "", i);
1501        }
1502    }
1503  else if (!strcmp (type, "end_hour_24"))
1504    {                           // 00 ->23
1505      for (i = 0; i < 24; i++)
1506        {
1507
1508          websWrite (wp, "<option value=%d %s>%d</option>\n", i,
1509                     i == end_hour ? "selected=\"selected\" " : "", i);
1510        }
1511    }
1512  else if (!strcmp (type, "end_min_1"))
1513    {                           // 0 1 2 3 4 .... -> 58 59
1514      for (i = 0; i < 60; i++)
1515        {
1516
1517          websWrite (wp, "<option value=%02d %s>%02d</option>\n", i,
1518                     i == end_min ? "selected=\"selected\" " : "", i);
1519        }
1520    }
1521/*  else if (!strcmp (type, "start_hour_12"))
1522    {                           // 1 -> 12
1523      for (i = 1; i <= 12; i++)
1524        {
1525          int j;
1526          if (i == 12)
1527            j = 0;
1528          else
1529            j = i;
1530
1531          websWrite (wp, "<option value=%d %s>%d</option>\n", j,
1532                     j == start_hour ? "selected=\"selected\" " : "", i);
1533        }
1534    }
1535  else if (!strcmp (type, "start_min_5"))
1536    {                           // 0 5 10 15 20 25 30 35 40 45 50 55
1537      for (i = 0; i < 12; i++)
1538        {
1539
1540          websWrite (wp, "<option value=%02d %s>%02d</option>\n", i * 5,
1541                     i * 5 == start_min ? "selected=\"selected\" " : "",
1542                     i * 5);
1543        }
1544    }
1545  else if (!strcmp (type, "start_time_am"))
1546    {
1547      websWrite (wp, "%s", start_time == 1 ? "" : "selected=\"selected\" ");
1548    }
1549  else if (!strcmp (type, "start_time_pm"))
1550    {
1551      websWrite (wp, "%s", start_time == 1 ? "selected=\"selected\" " : "");
1552    }
1553  else if (!strcmp (type, "end_hour_12"))
1554    {                           // 1 -> 12
1555      for (i = 1; i <= 12; i++)
1556        {
1557          int j;
1558          if (i == 12)
1559            j = 0;
1560          else
1561            j = i;
1562
1563          websWrite (wp, "<option value=%d %s>%d</option>\n", j,
1564                     j == end_hour ? "selected=\"selected\" " : "", i);
1565        }
1566    }
1567  else if (!strcmp (type, "end_min_5"))
1568    {                           // 0 5 10 15 20 25 30 35 40 45 50 55
1569      for (i = 0; i < 12; i++)
1570        {
1571
1572          websWrite (wp, "<option value=%02d %s>%02d</option>\n", i * 5,
1573                     i * 5 == end_min ? "selected=\"selected\" " : "", i * 5);
1574        }
1575    }
1576  else if (!strcmp (type, "end_time_am"))
1577    {
1578      websWrite (wp, "%s", end_time == 1 ? "" : "selected=\"selected\" ");
1579    }
1580  else if (!strcmp (type, "end_time_pm"))
1581    {
1582      websWrite (wp, "%s", end_time == 1 ? "selected=\"selected\" " : "");
1583    } */
1584  D ("right");
1585  return;
1586}
1587
1588/*  Format: url0, url1, url2, url3, ....
1589 *          keywd0, keywd1, keywd2, keywd3, keywd4, keywd5, ....
1590 */
1591void
1592ej_filter_web_get (webs_t wp, int argc, char_t ** argv)
1593{
1594  char *type;
1595  int which;
1596  char *token = "<&nbsp;>";
1597  D ("filter-web-get");
1598  if (ejArgs (argc, argv, "%s %d", &type, &which) < 1)
1599    {
1600      websError (wp, 400, "Insufficient args\n");
1601      return;
1602    }
1603
1604
1605  if (!strcmp (type, "host"))
1606    {
1607      char *host_data, filter_host[] = "filter_web_hostXXX";;
1608      char host[80];
1609
1610      snprintf (filter_host, sizeof (filter_host), "filter_web_host%s",
1611                nvram_safe_get ("filter_id"));
1612      host_data = nvram_safe_get (filter_host);
1613      if (!strcmp (host_data, ""))
1614        return;                 // no data
1615      find_each (host, sizeof (host), host_data, token, which, "");
1616      websWrite (wp, "%s", host);
1617    }
1618  else if (!strcmp (type, "url"))
1619    {
1620      char *url_data, filter_url[] = "filter_web_urlXXX";
1621      char url[80];
1622
1623      snprintf (filter_url, sizeof (filter_url), "filter_web_url%s",
1624                nvram_safe_get ("filter_id"));
1625      url_data = nvram_safe_get (filter_url);
1626      if (!strcmp (url_data, ""))
1627        return;                 // no data
1628      find_each (url, sizeof (url), url_data, token, which, "");
1629      websWrite (wp, "%s", url);
1630    }
1631  D ("okay");
1632  return;
1633}
1634
1635void
1636ej_filter_summary_show (webs_t wp, int argc, char_t ** argv)
1637{
1638  int i;
1639#if LANGUAGE == JAPANESE
1640  char w[7][10] = { "“ú", "ŒŽ", "‰Î", "?…", "–Ø", "‹à", "“y" };
1641  char am[] = "Œß‘O";
1642  char pm[] = "ŒßŒã";
1643  char _24h[] = "24 ŽžŠÔ";
1644#else
1645  char w[7][15] =
1646    { "share.sun_s1", "share.mon_s1", "share.tue_s1", "share.wed_s1",
1647    "share.thu_s1", "share.fri_s1", "share.sat_s1"
1648  };
1649//  char am[] = "AM";
1650//  char pm[] = "PM";
1651  char _24h[] = "24 Hours.";
1652#endif
1653  D ("filter summary show");
1654  for (i = 0; i < 10; i++)
1655    {
1656      char name[50] = "---";
1657      char status[5] = "---";
1658      char filter[] = "filter_ruleXXX";
1659      char *data = "";
1660      char time_buf[50] = "---";
1661      snprintf (filter, sizeof (filter), "filter_rule%d", i + 1);
1662      data = nvram_safe_get (filter);
1663      if (data && strcmp (data, ""))
1664        {
1665          find_match_pattern (name, sizeof (name), data, "$NAME:", "&nbsp;");   // get name value
1666          find_match_pattern (status, sizeof (status), data, "$STAT:", "---");  // get name value
1667        }
1668
1669      filter_tod_init (i + 1);
1670
1671      websWrite (wp, " \
1672                <tr align=\"center\" bgcolor=\"#CCCCCC\" >\n\
1673                        <td width=\"50\" ><font face=\"Arial\" size=\"2\" >%d.</font></td>\n\
1674                        <td width=\"200\" ><font face=\"Arial\" size=\"2\" >%s</font></td>\n\
1675                        <td height=\"30\" width=\"150\" >\n\
1676                        <table width=\"150\" height=\"30\" border=\"1\" cellspacing=\"1\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse\" >\n\
1677                                <tr>\n", i + 1, name);
1678      websWrite (wp, " \
1679                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1680                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1681                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1682                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n", 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]);
1683      websWrite (wp, " \
1684                <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1685                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1686                        <td align=\"center\" width=\"17\" bgcolor=\"%s\" style=\"border-style: solid\"><script type=\"text/javascript\">Capture(%s)</script></td>\n\
1687                </tr>\n\
1688                </table>\n\
1689                </td>\n", 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]);
1690
1691      if (tod_data_null == 0)
1692        {
1693          if (time_all == 1)
1694            strcpy (time_buf, _24h);
1695          else
1696            {
1697              snprintf (time_buf, sizeof (time_buf),
1698                        "%02d:%02d - %02d:%02d",
1699                        start_hour, start_min, end_hour, end_min);
1700            }
1701        }
1702      websWrite (wp, " \
1703        <td width=\"150\" ><font face=\"Arial\" size=\"2\" > %s </font></td>\n\
1704        <td width=\"70\" ><input type=\"checkbox\" name=\"sum%d\" value=\"1\" ></td>\n\
1705      </tr>\n", time_buf, i + 1);
1706    }
1707  D ("okay");
1708  return;
1709
1710}
1711
1712void
1713ej_filter_init (webs_t wp, int argc, char_t ** argv)
1714{
1715  char *f_id = websGetVar (wp, "f_id", NULL);
1716  D ("ej_filter-init");
1717  if (f_id)                     // for first time enter this page and don't press apply.
1718    nvram_set ("filter_id", f_id);
1719  else
1720    nvram_set ("filter_id", "1");
1721
1722  return;
1723}
1724
1725void
1726ej_filter_port_services_get (webs_t wp, int argc, char_t ** argv)
1727{
1728  char *type;
1729  int which;
1730  char word[1024], *next, services[8192] = "", svcs_var[32] = "";
1731  char delim[] = "<&nbsp;>";
1732  int index = 0;
1733  D ("ej_filter_port_services get");
1734  if (ejArgs (argc, argv, "%s %d", &type, &which) < 2)
1735    {
1736      websError (wp, 400, "Insufficient args\n");
1737      return;
1738    }
1739
1740  services = get_filter_services ();
1741
1742  if (!strcmp (type, "all_list"))
1743    {
1744      int count = 0;
1745//              services = nvram_safe_get("filter_services");
1746      split (word, services, next, delim)
1747      {
1748        int len = 0;
1749        char *name, *prot, *port;
1750        char protocol[100], ports[100];
1751        int from = 0, to = 0;
1752        //int proto;
1753
1754        if ((name = strstr (word, "$NAME:")) == NULL ||
1755            (prot = strstr (word, "$PROT:")) == NULL ||
1756            (port = strstr (word, "$PORT:")) == NULL)
1757          continue;
1758
1759        /* $NAME */
1760        if (sscanf (name, "$NAME:%3d:", &len) != 1)
1761          continue;
1762        strncpy (name, name + sizeof ("$NAME:nnn:") - 1, len);
1763        name[len] = '\0';
1764
1765        /* $PROT */
1766        if (sscanf (prot, "$PROT:%3d:", &len) != 1)
1767          continue;
1768        strncpy (protocol, prot + sizeof ("$PROT:nnn:") - 1, len);
1769        protocol[len] = '\0';
1770
1771        /* $PORT */
1772        if (sscanf (port, "$PORT:%3d:", &len) != 1)
1773          continue;
1774        strncpy (ports, port + sizeof ("$PORT:nnn:") - 1, len);
1775        ports[len] = '\0';
1776        if (sscanf (ports, "%d:%d", &from, &to) != 2)
1777          continue;
1778
1779        //cprintf("match:: name=%s, protocol=%s, ports=%s\n",
1780        //      word, protocol, ports);
1781
1782
1783        websWrite (wp,
1784                   "services[%d]=new service(%d, \"%s\", %d, %d, %d);\n",
1785                   count, count, name, from, to, protocol_to_num (protocol));
1786        count++;
1787
1788      }
1789
1790      websWrite (wp, "services_length = %d;\n", count);
1791    }
1792  else if (!strcmp (type, "service"))
1793    {
1794      char *port_data, filter_port[] = "filter_port_grpXXX";
1795      char name[80];
1796
1797      snprintf (filter_port, sizeof (filter_port), "filter_port_grp%s",
1798                nvram_safe_get ("filter_id"));
1799      port_data = nvram_safe_get (filter_port);
1800      if (!strcmp (port_data, ""))
1801        return;                 // no data
1802      find_each (name, sizeof (name), port_data, "<&nbsp;>", which, "");
1803      websWrite (wp, "%s", name);
1804
1805    }
1806  else if (!strcmp (type, "p2p"))
1807    {
1808      char *port_data, filter_port[] = "filter_p2p_grpXXX";
1809
1810      snprintf (filter_port, sizeof (filter_port), "filter_p2p_grp%s",
1811                nvram_safe_get ("filter_id"));
1812      port_data = nvram_safe_get (filter_port);
1813      if (!strcmp (port_data, ""))
1814        return;                 // no data
1815      websWrite (wp, "%s", port_data);
1816
1817    }
1818  D ("okay");
1819  return;
1820}
1821
1822int
1823filtersummary_onload (webs_t wp, char *arg)
1824{
1825  int ret = 0;
1826  D ("filter summary unload");
1827  if (!strcmp (nvram_safe_get ("filter_summary"), "1"))
1828    {
1829      websWrite (wp, arg);
1830    }
1831  D ("okay");
1832  return ret;
1833}
Note: See TracBrowser for help on using the repository browser.