2 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
20 #include <arpa/inet.h>
21 #include <netinet/ether.h>
24 #include <sys/types.h>
29 #include "libvalidate.h"
49 struct dt_fun
*function
;
56 struct uci_context
*ctx
;
59 struct dt_op stack
[32];
65 bool (*call
)(struct dt_state
*s
, int nargs
);
69 dt_test_number(double number
, const char *value
)
74 n
= strtod(value
, &e
);
76 return (e
> value
&& *e
== 0 && n
== number
);
80 dt_test_string(const char *s
, const char *end
, const char *value
)
89 if (!esc
&& *s
== '\\')
108 return (*s
== *value
|| (s
>= end
&& *value
== 0));
112 dt_step(struct dt_state
*s
);
115 dt_call(struct dt_state
*s
);
117 #define dt_getint(n, v) \
118 ((n < nargs && s->stack[s->pos + n].type == OP_NUMBER) \
119 ? (v = s->stack[s->pos + n].value.number, 1) : 0)
122 dt_type_or(struct dt_state
*s
, int nargs
)
132 dt_type_and(struct dt_state
*s
, int nargs
)
142 dt_type_not(struct dt_state
*s
, int nargs
)
151 dt_type_neg(struct dt_state
*s
, int nargs
)
154 const char *value
= s
->value
;
159 if (*s
->value
== '!')
160 while (isspace(*++s
->value
));
169 dt_type_list(struct dt_state
*s
, int nargs
)
173 char *p
, *str
= strdup(s
->value
);
174 const char *value
= s
->value
;
176 if (!str
|| !nargs
) {
181 for (p
= strtok(str
, " \t"); p
; p
= strtok(NULL
, " \t"))
201 dt_type_min(struct dt_state
*s
, int nargs
)
206 if (dt_getint(0, min
))
208 n
= strtol(s
->value
, &e
, 0);
209 return (e
> s
->value
&& *e
== 0 && n
>= min
);
216 dt_type_max(struct dt_state
*s
, int nargs
)
221 if (dt_getint(0, max
))
223 n
= strtol(s
->value
, &e
, 0);
224 return (e
> s
->value
&& *e
== 0 && n
<= max
);
231 dt_type_range(struct dt_state
*s
, int nargs
)
236 if (dt_getint(0, min
) && dt_getint(1, max
))
238 n
= strtol(s
->value
, &e
, 0);
239 return (e
> s
->value
&& *e
== 0 && n
>= min
&& n
<= max
);
246 dt_type_minlen(struct dt_state
*s
, int nargs
)
250 if (dt_getint(0, min
))
251 return (strlen(s
->value
) >= min
);
257 dt_type_maxlen(struct dt_state
*s
, int nargs
)
261 if (dt_getint(0, max
))
262 return (strlen(s
->value
) <= max
);
268 dt_type_rangelen(struct dt_state
*s
, int nargs
)
271 int len
= strlen(s
->value
);
273 if (dt_getint(0, min
) && dt_getint(1, max
))
274 return (len
>= min
&& len
<= max
);
280 dt_type_int(struct dt_state
*s
, int nargs
)
285 if (!isxdigit(*s
->value
) && *s
->value
!= '-')
289 strtol(s
->value
, &e
, base
);
291 return (e
> s
->value
&& *e
== 0);
295 dt_type_uint(struct dt_state
*s
, int nargs
)
300 if (!isxdigit(*s
->value
))
304 strtoul(s
->value
, &e
, base
);
306 return (e
> s
->value
&& *e
== 0);
310 dt_type_float(struct dt_state
*s
, int nargs
)
314 strtod(s
->value
, &e
);
316 return (e
> s
->value
&& *e
== 0);
320 dt_type_ufloat(struct dt_state
*s
, int nargs
)
325 n
= strtod(s
->value
, &e
);
327 return (e
> s
->value
&& *e
== 0 && n
>= 0.0);
331 dt_type_bool(struct dt_state
*s
, int nargs
)
334 const char *values
[] = {
335 "0", "off", "false", "no", "disabled",
336 "1", "on", "true", "yes", "enabled"
339 for (i
= 0; i
< sizeof(values
) / sizeof(values
[0]); i
++)
340 if (!strcasecmp(values
[i
], s
->value
))
347 dt_type_string(struct dt_state
*s
, int nargs
)
350 int len
= strlen(s
->value
);
352 if (dt_getint(0, min
) && (len
< min
))
355 if (dt_getint(1, max
) && (len
> max
))
362 dt_type_hexstring(struct dt_state
*s
, int nargs
)
365 int len
= strlen(s
->value
);
371 if (dt_getint(0, min
) && (len
< min
))
374 if (dt_getint(1, max
) && (len
> max
))
377 for (p
= s
->value
; *p
; p
++)
385 dt_type_ip4addr(struct dt_state
*s
, int nargs
)
388 return inet_pton(AF_INET
, s
->value
, &a
);
392 dt_type_ip6addr(struct dt_state
*s
, int nargs
)
395 return inet_pton(AF_INET6
, s
->value
, &a
);
399 dt_type_ipaddr(struct dt_state
*s
, int nargs
)
401 return (dt_type_ip4addr(s
, 0) || dt_type_ip6addr(s
, 0));
405 dt_type_netmask4(struct dt_state
*s
, int nargs
)
410 if (!inet_pton(AF_INET
, s
->value
, &a
))
416 a
.s_addr
= ntohl(a
.s_addr
);
418 for (i
= 0; (i
< 32) && !(a
.s_addr
& (1 << i
)); i
++);
420 return ((uint32_t)(~((1 << i
) - 1)) == a
.s_addr
);
424 dt_type_netmask6(struct dt_state
*s
, int nargs
)
429 if (!inet_pton(AF_INET6
, s
->value
, &a
))
432 for (i
= 0; (i
< 16) && (a
.s6_addr
[i
] == 0xFF); i
++);
437 if ((a
.s6_addr
[i
] != 255) && (a
.s6_addr
[i
] != 254) &&
438 (a
.s6_addr
[i
] != 252) && (a
.s6_addr
[i
] != 248) &&
439 (a
.s6_addr
[i
] != 240) && (a
.s6_addr
[i
] != 224) &&
440 (a
.s6_addr
[i
] != 192) && (a
.s6_addr
[i
] != 128) &&
444 for (; (i
< 16) && (a
.s6_addr
[i
] == 0); i
++);
450 dt_type_cidr4(struct dt_state
*s
, int nargs
)
454 char *p
, buf
[sizeof("255.255.255.255/32\0")];
456 if (strlen(s
->value
) >= sizeof(buf
))
459 strcpy(buf
, s
->value
);
460 p
= strchr(buf
, '/');
466 n
= strtoul(p
, &p
, 10);
468 if ((*p
!= 0) || (n
> 32))
472 return inet_pton(AF_INET
, buf
, &a
);
476 dt_type_cidr6(struct dt_state
*s
, int nargs
)
480 char *p
, buf
[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255/128\0")];
482 if (strlen(s
->value
) >= sizeof(buf
))
485 strcpy(buf
, s
->value
);
486 p
= strchr(buf
, '/');
492 n
= strtoul(p
, &p
, 10);
494 if ((*p
!= 0) || (n
> 128))
498 return inet_pton(AF_INET6
, buf
, &a
);
502 dt_type_cidr(struct dt_state
*s
, int nargs
)
504 return (dt_type_cidr4(s
, 0) || dt_type_cidr6(s
, 0));
508 dt_type_ipmask4(struct dt_state
*s
, int nargs
)
513 char *p
, buf
[sizeof("255.255.255.255/255.255.255.255\0")];
515 if (strlen(s
->value
) >= sizeof(buf
))
518 strcpy(buf
, s
->value
);
519 p
= strchr(buf
, '/');
527 rv
= dt_type_netmask4(s
, 0);
534 return inet_pton(AF_INET
, buf
, &a
);
538 dt_type_ipmask6(struct dt_state
*s
, int nargs
)
543 char *p
, buf
[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255/"
544 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255\0")];
546 if (strlen(s
->value
) >= sizeof(buf
))
549 strcpy(buf
, s
->value
);
550 p
= strchr(buf
, '/');
558 rv
= dt_type_netmask6(s
, 0);
565 return inet_pton(AF_INET6
, buf
, &a
);
569 dt_type_ipmask(struct dt_state
*s
, int nargs
)
571 return (dt_type_ipmask4(s
, 0) || dt_type_ipmask6(s
, 0));
575 dt_type_port(struct dt_state
*s
, int nargs
)
580 n
= strtoul(s
->value
, &e
, 10);
582 return (e
> s
->value
&& *e
== 0 && n
<= 65535);
586 dt_type_portrange(struct dt_state
*s
, int nargs
)
591 n
= strtoul(s
->value
, &e
, 10);
593 if (e
== s
->value
|| *e
!= '-')
596 m
= strtoul(e
+ 1, &e
, 10);
598 return (*e
== 0 && n
<= 65535 && m
<= 65535 && n
<= m
);
602 dt_type_macaddr(struct dt_state
*s
, int nargs
)
604 return !!ether_aton(s
->value
);
608 dt_type_uciname(struct dt_state
*s
, int nargs
)
613 *p
&& ((*p
>= 'A' && *p
<= 'Z') || (*p
>= 'a' && *p
<= 'z') ||
614 (*p
>= '0' && *p
<= '9') || (*p
== '_'));
621 dt_type_wpakey(struct dt_state
*s
, int nargs
)
623 int len
= strlen(s
->value
);
624 const char *p
= s
->value
;
634 return (len
>= 8 && len
<= 63);
638 dt_type_wepkey(struct dt_state
*s
, int nargs
)
640 int len
= strlen(s
->value
);
641 const char *p
= s
->value
;
643 if (!strncmp(p
, "s:", 2))
649 if (len
== 10 || len
== 26)
657 return (len
== 5 || len
== 13);
661 dt_type_hostname(struct dt_state
*s
, int nargs
)
663 const char *p
, *last
;
665 for (p
= last
= s
->value
; *p
; p
++)
669 if ((p
- last
) == 0 || (p
- last
) > 63)
675 else if ((*p
>= 'A' && *p
<= 'Z') || (*p
>= 'a' && *p
<= 'z') ||
676 (*p
>= '0' && *p
<= '9') || (*p
== '_') || (*p
== '-'))
684 return ((p
- last
) > 0 && (p
- last
) <= 255);
688 dt_type_host(struct dt_state
*s
, int nargs
)
690 return (dt_type_hostname(s
, 0) || dt_type_ipaddr(s
, 0));
694 dt_type_network(struct dt_state
*s
, int nargs
)
696 return (dt_type_uciname(s
, 0) || dt_type_host(s
, 0));
700 dt_type_phonedigit(struct dt_state
*s
, int nargs
)
705 *p
&& ((*p
>= '0' && *p
<= '9') || (*p
== '*') || (*p
== '#') ||
706 (*p
== '!') || (*p
== '.'));
713 dt_type_directory(struct dt_state
*s
, int nargs
)
716 return (!stat(s
->value
, &st
) && S_ISDIR(st
.st_mode
));
721 dt_type_device(struct dt_state
*s
, int nargs
)
724 return (!stat(s
->value
, &st
) &&
725 (S_ISBLK(st
.st_mode
) || S_ISCHR(st
.st_mode
)));
729 dt_type_file(struct dt_state
*s
, int nargs
)
732 return (!stat(s
->value
, &st
) && S_ISREG(st
.st_mode
));
736 dt_type_regex(struct dt_state
*s
, int nargs
)
743 if (nargs
< 1 || s
->stack
[s
->pos
].type
!= OP_STRING
)
746 relen
= s
->stack
[s
->pos
].length
;
747 re
= alloca(relen
+ 3);
752 memset(re
, 0, relen
+ 3);
753 memcpy(re
+ 1, s
->stack
[s
->pos
].value
.string
, relen
);
758 if (regcomp(&pattern
, re
, REG_EXTENDED
| REG_NOSUB
))
761 rv
= !regexec(&pattern
, s
->value
, 0, NULL
, 0);
769 dt_uci_lookup(struct dt_state
*s
, const char *pkg
,
770 const char *sct
, const char *opt
, enum uci_type type
)
772 struct uci_ptr ptr
= {
778 if (!s
->ctx
|| uci_lookup_ptr(s
->ctx
, &ptr
, NULL
, false) ||
779 !(ptr
.flags
& UCI_LOOKUP_COMPLETE
))
782 if (ptr
.last
->type
!= type
)
787 case UCI_TYPE_PACKAGE
:
788 return uci_to_package(ptr
.last
);
790 case UCI_TYPE_SECTION
:
791 return uci_to_section(ptr
.last
);
793 case UCI_TYPE_OPTION
:
794 return uci_to_option(ptr
.last
);
802 dt_uci_cmp(struct dt_state
*s
,
803 const char *pkg
, const char *sct
, const char *opt
)
805 struct uci_element
*e
;
806 struct uci_option
*o
= dt_uci_lookup(s
, pkg
, sct
, opt
, UCI_TYPE_OPTION
);
813 case UCI_TYPE_STRING
:
814 if (!strcmp(s
->value
, o
->v
.string
))
819 uci_foreach_element(&o
->v
.list
, e
)
820 if (!strcmp(s
->value
, e
->name
))
829 dt_type_uci(struct dt_state
*s
, int nargs
)
832 struct uci_element
*e
;
833 struct uci_package
*p
;
839 for (i
= 0; i
< nargs
&& i
< 3; i
++)
841 if (s
->stack
[s
->pos
+ i
].type
!= OP_STRING
)
844 len
= s
->stack
[s
->pos
+ i
].length
;
845 cso
[i
] = alloca(len
+ 1);
850 memset(cso
[i
], 0, len
+ 1);
851 memcpy(cso
[i
], s
->stack
[s
->pos
+ i
].value
.string
, len
);
854 if (!cso
[0] || !cso
[1] || (*cso
[1] != '@' && !cso
[2]))
858 return dt_uci_cmp(s
, cso
[0], cso
[1], cso
[2]);
860 p
= dt_uci_lookup(s
, cso
[0], NULL
, NULL
, UCI_TYPE_PACKAGE
);
865 uci_foreach_element(&p
->sections
, e
)
867 if (strcmp(uci_to_section(e
)->type
, cso
[1] + 1))
872 if (!strcmp(s
->value
, e
->name
))
877 if (dt_uci_cmp(s
, cso
[0], e
->name
, cso
[2]))
886 static struct dt_fun dt_types
[] = {
887 { "or", DT_INVALID
, dt_type_or
},
888 { "and", DT_INVALID
, dt_type_and
},
889 { "not", DT_INVALID
, dt_type_not
},
890 { "neg", DT_INVALID
, dt_type_neg
},
891 { "list", DT_INVALID
, dt_type_list
},
892 { "min", DT_NUMBER
, dt_type_min
},
893 { "max", DT_NUMBER
, dt_type_max
},
894 { "range", DT_NUMBER
, dt_type_range
},
895 { "minlength", DT_STRING
, dt_type_minlen
},
896 { "maxlength", DT_STRING
, dt_type_maxlen
},
897 { "rangelength", DT_STRING
, dt_type_rangelen
},
898 { "integer", DT_NUMBER
, dt_type_int
},
899 { "uinteger", DT_NUMBER
, dt_type_uint
},
900 { "float", DT_NUMBER
, dt_type_float
},
901 { "ufloat", DT_NUMBER
, dt_type_ufloat
},
902 { "bool", DT_BOOL
, dt_type_bool
},
903 { "string", DT_STRING
, dt_type_string
},
904 { "hexstring", DT_STRING
, dt_type_hexstring
},
905 { "ip4addr", DT_STRING
, dt_type_ip4addr
},
906 { "ip6addr", DT_STRING
, dt_type_ip6addr
},
907 { "ipaddr", DT_STRING
, dt_type_ipaddr
},
908 { "cidr4", DT_STRING
, dt_type_cidr4
},
909 { "cidr6", DT_STRING
, dt_type_cidr6
},
910 { "cidr", DT_STRING
, dt_type_cidr
},
911 { "netmask4", DT_STRING
, dt_type_netmask4
},
912 { "netmask6", DT_STRING
, dt_type_netmask6
},
913 { "ipmask4", DT_STRING
, dt_type_ipmask4
},
914 { "ipmask6", DT_STRING
, dt_type_ipmask6
},
915 { "ipmask", DT_STRING
, dt_type_ipmask
},
916 { "port", DT_NUMBER
, dt_type_port
},
917 { "portrange", DT_STRING
, dt_type_portrange
},
918 { "macaddr", DT_STRING
, dt_type_macaddr
},
919 { "uciname", DT_STRING
, dt_type_uciname
},
920 { "wpakey", DT_STRING
, dt_type_wpakey
},
921 { "wepkey", DT_STRING
, dt_type_wepkey
},
922 { "hostname", DT_STRING
, dt_type_hostname
},
923 { "host", DT_STRING
, dt_type_host
},
924 { "network", DT_STRING
, dt_type_network
},
925 { "phonedigit", DT_STRING
, dt_type_phonedigit
},
926 { "directory", DT_STRING
, dt_type_directory
},
927 { "device", DT_STRING
, dt_type_device
},
928 { "file", DT_STRING
, dt_type_file
},
929 { "regex", DT_STRING
, dt_type_regex
},
930 { "uci", DT_STRING
, dt_type_uci
},
935 static struct dt_fun
*
936 dt_lookup_function(const char *s
, const char *e
)
938 struct dt_fun
*fun
= dt_types
;
942 if (!strncmp(fun
->name
, s
, e
- s
) && *(fun
->name
+ (e
- s
)) == '\0')
952 dt_parse_atom(struct dt_state
*s
, const char *label
, const char *end
)
959 struct dt_op
*op
= &s
->stack
[s
->depth
];
961 if ((s
->depth
+ 1) >= (sizeof(s
->stack
) / sizeof(s
->stack
[0])))
963 printf("Syntax error, expression too long\n");
967 while (isspace(*label
))
970 /* test whether label is a float */
971 dval
= strtod(label
, &e
);
976 op
->type
= OP_NUMBER
;
977 op
->value
.number
= dval
;
978 op
->nextop
= ++s
->depth
;
982 else if ((*label
== '"') || (*label
== '\''))
984 for (p
= label
+ 1, q
= *label
, esc
= false; p
<= end
; p
++)
999 op
->type
= OP_STRING
;
1000 op
->length
= (p
- label
) - 1;
1001 op
->value
.string
= label
+ 1;
1002 op
->nextop
= ++s
->depth
;
1008 printf("Syntax error, unterminated string\n");
1014 p
<= end
&& ((*p
>= 'A' && *p
<= 'Z') ||
1015 (*p
>= 'a' && *p
<= 'z') ||
1016 (*p
>= '0' && *p
<= '9') ||
1020 func
= dt_lookup_function(label
, p
);
1024 printf("Syntax error, unrecognized function\n");
1029 op
->type
= OP_FUNCTION
;
1030 op
->value
.function
= func
;
1031 op
->nextop
= ++s
->depth
;
1036 printf("Syntax error, unexpected EOF\n");
1041 dt_parse_list(struct dt_state
*s
, const char *code
, const char *end
);
1044 dt_parse_expr(const char *code
, const char *end
, struct dt_state
*s
)
1048 if (!dt_parse_atom(s
, code
, end
))
1051 tok
= &s
->stack
[s
->depth
- 1];
1053 while (isspace(*tok
->next
))
1056 if (tok
->type
== OP_FUNCTION
)
1058 if (*tok
->next
== '(')
1062 while (isspace(*end
) && end
> tok
->next
+ 1)
1065 return dt_parse_list(s
, tok
->next
+ 1, end
);
1067 else if (tok
->next
== end
)
1069 return dt_parse_list(s
, tok
->next
, tok
->next
);
1072 printf("Syntax error, expected '(' or EOF after function label\n");
1075 else if (tok
->next
== end
)
1080 printf("Syntax error, expected ',' after literal\n");
1085 dt_parse_list(struct dt_state
*s
, const char *code
, const char *end
)
1090 const char *p
, *last
;
1096 fptr
= &s
->stack
[s
->depth
- 1];
1098 for (nest
= 0, p
= last
= code
, esc
= false, c
= *p
;
1100 p
++, c
= (p
< end
) ? *p
: '\0')
1128 if (!dt_parse_expr(last
, p
, s
))
1141 fptr
->nextop
= s
->depth
;
1146 dt_step(struct dt_state
*s
)
1149 struct dt_op
*op
= &s
->stack
[s
->pos
];
1154 rv
= dt_test_number(op
->value
.number
, s
->value
);
1156 s
->valtype
= DT_NUMBER
;
1160 rv
= dt_test_string(op
->value
.string
, op
->value
.string
+ op
->length
, s
->value
);
1162 s
->valtype
= DT_STRING
;
1174 s
->pos
= op
->nextop
;
1179 dt_call(struct dt_state
*s
)
1182 struct dt_op
*fptr
= &s
->stack
[s
->pos
];
1183 struct dt_fun
*func
= fptr
->value
.function
;
1187 rv
= func
->call(s
, fptr
->length
);
1189 if (rv
&& func
->valtype
)
1190 s
->valtype
= func
->valtype
;
1192 s
->pos
= fptr
->nextop
;
1198 dt_parse(const char *code
, const char *value
)
1200 enum dt_type rv
= DT_INVALID
;
1202 struct dt_state s
= {
1206 .type
= OP_FUNCTION
,
1207 .value
.function
= &dt_types
[0],
1213 if (!value
|| !*value
)
1216 if (!dt_parse_list(&s
, code
, code
+ strlen(code
)))
1219 s
.ctx
= uci_alloc_context();
1226 uci_free_context(s
.ctx
);