summaryrefslogtreecommitdiff
path: root/validate/validate.c
blob: e72b8117ecd8b680778b0f5c7637ed6546a7736b (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
/*
 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

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

#include <arpa/inet.h>
#include <netinet/ether.h>
#include <sys/stat.h>

#include <sys/types.h>
#include <regex.h>

#include <uci.h>

#include "libvalidate.h"

enum dt_optype {
	OP_UNKNOWN,
	OP_NUMBER,
	OP_STRING,
	OP_FUNCTION
};

struct dt_fun;

struct dt_op {
	enum dt_optype type;
	const char *next;
	int length;
	int nextop;
	union {
		bool boolean;
		double number;
		const char *string;
		struct dt_fun *function;
	} value;
};

struct dt_state {
	int pos;
	int depth;
	struct uci_context *ctx;
	const char *value;
	enum dt_type valtype;
	struct dt_op stack[32];
};

struct dt_fun {
	const char *name;
	enum dt_type valtype;
	bool (*call)(struct dt_state *s, int nargs);
};

static bool
dt_test_number(double number, const char *value)
{
	char *e;
	double n;

	n = strtod(value, &e);

	return (e > value && *e == 0 && n == number);
}

static bool
dt_test_string(const char *s, const char *end, const char *value)
{
	bool esc = false;

	while (*value)
	{
		if (s > end)
			return false;

		if (!esc && *s == '\\')
		{
			s++;

			if (s >= end)
				break;

			esc = true;
			continue;
		}

		if (*s != *value)
			return false;

		esc = false;
		value++;
		s++;
	}

	return (*s == *value || (s >= end && *value == 0));
}

static bool
dt_step(struct dt_state *s);

static bool
dt_call(struct dt_state *s);

#define dt_getint(n, v) \
	((n < nargs && s->stack[s->pos + n].type == OP_NUMBER) \
		? (v = s->stack[s->pos + n].value.number, 1) : 0)

static bool
dt_type_or(struct dt_state *s, int nargs)
{
	while (nargs--)
		if (dt_step(s))
			return true;

	return false;
}

static bool
dt_type_and(struct dt_state *s, int nargs)
{
	while (nargs--)
		if (!dt_step(s))
			return false;

	return true;
}

static bool
dt_type_not(struct dt_state *s, int nargs)
{
	if (!nargs)
		return false;

	return !dt_step(s);
}

static bool
dt_type_neg(struct dt_state *s, int nargs)
{
	bool rv;
	const char *value = s->value;

	if (!nargs)
		return false;

	if (*s->value == '!')
		while (isspace(*++s->value));

	rv = dt_step(s);
	s->value = value;

	return rv;
}

static bool
dt_type_list(struct dt_state *s, int nargs)
{
	bool rv = true;
	int pos = s->pos;
	char *p, *str = strdup(s->value);
	const char *value = s->value;

	if (!str || !nargs) {
		free(str);
		return false;
	}

	for (p = strtok(str, " \t"); p; p = strtok(NULL, " \t"))
	{
		s->value = p;

		if (!dt_step(s))
		{
			rv = false;
			break;
		}

		s->pos = pos;
	}

	s->value = value;
	free(str);

	return rv;
}

static bool
dt_type_min(struct dt_state *s, int nargs)
{
	int n;
	int min = 0;
	char *e;

	if (dt_getint(0, min))
	{
		n = strtol(s->value, &e, 0);
		return (e > s->value && *e == 0 && n >= min);
	}

	return false;
}

static bool
dt_type_max(struct dt_state *s, int nargs)
{
	int n;
	int max = 0;
	char *e;

	if (dt_getint(0, max))
	{
		n = strtol(s->value, &e, 0);
		return (e > s->value && *e == 0 && n <= max);
	}

	return false;
}

static bool
dt_type_range(struct dt_state *s, int nargs)
{
	int n;
	int min = 0;
	int max = 0;
	char *e;

	if (dt_getint(0, min) && dt_getint(1, max))
	{
		n = strtol(s->value, &e, 0);
		return (e > s->value && *e == 0 && n >= min && n <= max);
	}

	return false;
}

static bool
dt_type_minlen(struct dt_state *s, int nargs)
{
	int min = 0;

	if (dt_getint(0, min))
		return (strlen(s->value) >= min);

	return false;
}

static bool
dt_type_maxlen(struct dt_state *s, int nargs)
{
	int max = 0;

	if (dt_getint(0, max))
		return (strlen(s->value) <= max);

	return false;
}

static bool
dt_type_rangelen(struct dt_state *s, int nargs)
{
	int min = 0;
	int max = 0;
	int len = strlen(s->value);

	if (dt_getint(0, min) && dt_getint(1, max))
		return (len >= min && len <= max);

	return false;
}

static bool
dt_type_int(struct dt_state *s, int nargs)
{
	char *e;
	int base = 0;

	if (!isxdigit(*s->value) && *s->value != '-')
		return false;

	dt_getint(0, base);
	strtol(s->value, &e, base);

	return (e > s->value && *e == 0);
}

static bool
dt_type_uint(struct dt_state *s, int nargs)
{
	char *e;
	int base = 0;

	if (!isxdigit(*s->value))
		return false;

	dt_getint(0, base);
	strtoul(s->value, &e, base);

	return (e > s->value && *e == 0);
}

static bool
dt_type_float(struct dt_state *s, int nargs)
{
	char *e;

	strtod(s->value, &e);

	return (e > s->value && *e == 0);
}

static bool
dt_type_ufloat(struct dt_state *s, int nargs)
{
	int n;
	char *e;

	n = strtod(s->value, &e);

	return (e > s->value && *e == 0 && n >= 0.0);
}

static bool
dt_type_bool(struct dt_state *s, int nargs)
{
	int i;
	const char *values[] = {
		"0", "off", "false", "no", "disabled",
		"1", "on", "true", "yes", "enabled"
	};

	for (i = 0; i < sizeof(values) / sizeof(values[0]); i++)
		if (!strcasecmp(values[i], s->value))
			return true;

	return false;
}

static bool
dt_type_string(struct dt_state *s, int nargs)
{
	int min = 0;
	int max = 0;
	int len = strlen(s->value);

	if (dt_getint(0, min) && (len < min))
		return false;

	if (dt_getint(1, max) && (len > max))
		return false;

	return true;
}

static bool
dt_type_hexstring(struct dt_state *s, int nargs)
{
	int min = 0;
	int max = 0;
	int len = strlen(s->value);
	const char *p;

	if (len % 2)
		return false;

	if (dt_getint(0, min) && (len < min))
		return false;

	if (dt_getint(1, max) && (len > max))
		return false;

	for (p = s->value; *p; p++)
		if (!isxdigit(*p))
			return false;

	return true;
}

static bool
dt_type_ip4addr(struct dt_state *s, int nargs)
{
	struct in6_addr a;
	return inet_pton(AF_INET, s->value, &a);
}

static bool
dt_type_ip6addr(struct dt_state *s, int nargs)
{
	struct in6_addr a;
	return inet_pton(AF_INET6, s->value, &a);
}

static bool
dt_type_ipaddr(struct dt_state *s, int nargs)
{
	return (dt_type_ip4addr(s, 0) || dt_type_ip6addr(s, 0));
}

static bool
dt_type_netmask4(struct dt_state *s, int nargs)
{
	int i;
	struct in_addr a;

	if (!inet_pton(AF_INET, s->value, &a))
		return false;

	if (a.s_addr == 0)
		return true;

	a.s_addr = ntohl(a.s_addr);

	for (i = 0; (i < 32) && !(a.s_addr & (1 << i)); i++);

	return ((uint32_t)(~((1 << i) - 1)) == a.s_addr);
}

static bool
dt_type_netmask6(struct dt_state *s, int nargs)
{
	int i;
	struct in6_addr a;

	if (!inet_pton(AF_INET6, s->value, &a))
		return false;

	for (i = 0; (i < 16) && (a.s6_addr[i] == 0xFF); i++);

	if (i == 16)
		return true;

	if ((a.s6_addr[i] != 255) && (a.s6_addr[i] != 254) &&
		(a.s6_addr[i] != 252) && (a.s6_addr[i] != 248) &&
		(a.s6_addr[i] != 240) && (a.s6_addr[i] != 224) &&
		(a.s6_addr[i] != 192) && (a.s6_addr[i] != 128) &&
		(a.s6_addr[i] != 0))
		return false;

	for (; (i < 16) && (a.s6_addr[i] == 0); i++);

	return (i == 16);
}

static bool
dt_type_cidr4(struct dt_state *s, int nargs)
{
	int n;
	struct in_addr a;
	char *p, buf[sizeof("255.255.255.255/32\0")];

	if (strlen(s->value) >= sizeof(buf))
		return false;

	strcpy(buf, s->value);
	p = strchr(buf, '/');

	if (p)
	{
		*p++ = 0;

		n = strtoul(p, &p, 10);

		if ((*p != 0) || (n > 32))
			return false;
	}

	return inet_pton(AF_INET, buf, &a);
}

static bool
dt_type_cidr6(struct dt_state *s, int nargs)
{
	int n;
	struct in6_addr a;
	char *p, buf[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255/128\0")];

	if (strlen(s->value) >= sizeof(buf))
		return false;

	strcpy(buf, s->value);
	p = strchr(buf, '/');

	if (p)
	{
		*p++ = 0;

		n = strtoul(p, &p, 10);

		if ((*p != 0) || (n > 128))
			return false;
	}

	return inet_pton(AF_INET6, buf, &a);
}

static bool
dt_type_cidr(struct dt_state *s, int nargs)
{
	return (dt_type_cidr4(s, 0) || dt_type_cidr6(s, 0));
}

static bool
dt_type_ipmask4(struct dt_state *s, int nargs)
{
	bool rv;
	struct in_addr a;
	const char *value;
	char *p, buf[sizeof("255.255.255.255/255.255.255.255\0")];

	if (strlen(s->value) >= sizeof(buf))
		return false;

	strcpy(buf, s->value);
	p = strchr(buf, '/');

	if (p)
	{
		*p++ = 0;

		value = s->value;
		s->value = p;
		rv = dt_type_netmask4(s, 0);
		s->value = value;

		if (!rv)
			return false;
	}

	return inet_pton(AF_INET, buf, &a);
}

static bool
dt_type_ipmask6(struct dt_state *s, int nargs)
{
	bool rv;
	struct in6_addr a;
	const char *value;
	char *p, buf[sizeof("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255/"
	                    "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255\0")];

	if (strlen(s->value) >= sizeof(buf))
		return false;

	strcpy(buf, s->value);
	p = strchr(buf, '/');

	if (p)
	{
		*p++ = 0;

		value = s->value;
		s->value = p;
		rv = dt_type_netmask6(s, 0);
		s->value = value;

		if (!rv)
			return false;
	}

	return inet_pton(AF_INET6, buf, &a);
}

static bool
dt_type_ipmask(struct dt_state *s, int nargs)
{
	return (dt_type_ipmask4(s, 0) || dt_type_ipmask6(s, 0));
}

static bool
dt_type_port(struct dt_state *s, int nargs)
{
	int n;
	char *e;

	n = strtoul(s->value, &e, 10);

	return (e > s->value && *e == 0 && n <= 65535);
}

static bool
dt_type_portrange(struct dt_state *s, int nargs)
{
	int n, m;
	char *e;

	n = strtoul(s->value, &e, 10);

	if (e == s->value || *e != '-')
		return false;

	m = strtoul(e + 1, &e, 10);

	return (*e == 0 && n <= 65535 && m <= 65535 && n <= m);
}

static bool
dt_type_macaddr(struct dt_state *s, int nargs)
{
	return !!ether_aton(s->value);
}

static bool
dt_type_uciname(struct dt_state *s, int nargs)
{
	const char *p;

	for (p = s->value;
	     *p && ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') ||
	            (*p >= '0' && *p <= '9') || (*p == '_'));
		 p++);

	return (*p == 0);
}

static bool
dt_type_wpakey(struct dt_state *s, int nargs)
{
	int len = strlen(s->value);
	const char *p = s->value;

	if (len == 64)
	{
		while (isxdigit(*p))
			p++;

		return (*p == 0);
	}

	return (len >= 8 && len <= 63);
}

static bool
dt_type_wepkey(struct dt_state *s, int nargs)
{
	int len = strlen(s->value);
	const char *p = s->value;

	if (!strncmp(p, "s:", 2))
	{
		len -= 2;
		p += 2;
	}

	if (len == 10 || len == 26)
	{
		while (isxdigit(*p))
			p++;

		return (*p == 0);
	}

	return (len == 5 || len == 13);
}

static bool
dt_type_hostname(struct dt_state *s, int nargs)
{
	const char *p, *last;

	for (p = last = s->value; *p; p++)
	{
		if (*p == '.')
		{
			if ((p - last) == 0 || (p - last) > 63)
				return false;

			last = p + 1;
			continue;
		}
		else if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') ||
		         (*p >= '0' && *p <= '9') || (*p == '_') || (*p == '-'))
		{
			continue;
		}

		return false;
	}

	return ((p - last) > 0 && (p - last) <= 255);
}

static bool
dt_type_host(struct dt_state *s, int nargs)
{
	return (dt_type_hostname(s, 0) || dt_type_ipaddr(s, 0));
}

static bool
dt_type_network(struct dt_state *s, int nargs)
{
	return (dt_type_uciname(s, 0) || dt_type_host(s, 0));
}

static bool
dt_type_phonedigit(struct dt_state *s, int nargs)
{
	const char *p;

	for (p = s->value;
	     *p && ((*p >= '0' && *p <= '9') || (*p == '*') || (*p == '#') ||
	            (*p == '!') || (*p == '.'));
		 p++);

	return (*p == 0);
}

static bool
dt_type_directory(struct dt_state *s, int nargs)
{
	struct stat st;
	return (!stat(s->value, &st) && S_ISDIR(st.st_mode));
}


static bool
dt_type_device(struct dt_state *s, int nargs)
{
	struct stat st;
	return (!stat(s->value, &st) &&
	        (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)));
}

static bool
dt_type_file(struct dt_state *s, int nargs)
{
	struct stat st;
	return (!stat(s->value, &st) && S_ISREG(st.st_mode));
}

static bool
dt_type_regex(struct dt_state *s, int nargs)
{
	bool rv;
	int relen;
	regex_t pattern;
	char *re = NULL;

	if (nargs < 1 || s->stack[s->pos].type != OP_STRING)
		return false;

	relen = s->stack[s->pos].length;
	re = alloca(relen + 3);

	if (!re)
		return false;

	memset(re, 0, relen + 3);
	memcpy(re + 1, s->stack[s->pos].value.string, relen);

	re[0] = '^';
	re[relen + 1] = '$';

	if (regcomp(&pattern, re, REG_EXTENDED | REG_NOSUB))
		return false;

	rv = !regexec(&pattern, s->value, 0, NULL, 0);

	regfree(&pattern);

	return rv;
}

static void *
dt_uci_lookup(struct dt_state *s, const char *pkg,
              const char *sct, const char *opt, enum uci_type type)
{
	struct uci_ptr ptr = {
		.package = pkg,
		.section = sct,
		.option  = opt
	};

	if (!s->ctx || uci_lookup_ptr(s->ctx, &ptr, NULL, false) ||
	    !(ptr.flags & UCI_LOOKUP_COMPLETE))
		return NULL;

	if (ptr.last->type != type)
		return NULL;

	switch (type)
	{
	case UCI_TYPE_PACKAGE:
		return uci_to_package(ptr.last);

	case UCI_TYPE_SECTION:
		return uci_to_section(ptr.last);

	case UCI_TYPE_OPTION:
		return uci_to_option(ptr.last);

	default:
		return NULL;
	}
}

static bool
dt_uci_cmp(struct dt_state *s,
           const char *pkg, const char *sct, const char *opt)
{
	struct uci_element *e;
	struct uci_option *o = dt_uci_lookup(s, pkg, sct, opt, UCI_TYPE_OPTION);

	if (!o)
		return false;

	switch (o->type)
	{
	case UCI_TYPE_STRING:
		if (!strcmp(s->value, o->v.string))
			return true;
		break;

	case UCI_TYPE_LIST:
		uci_foreach_element(&o->v.list, e)
			if (!strcmp(s->value, e->name))
				return true;
		break;
	}

	return false;
}

static bool
dt_type_uci(struct dt_state *s, int nargs)
{
	int i, len;
	struct uci_element *e;
	struct uci_package *p;
	char *cso[3] = { };

	if (!s->ctx)
		return false;

	for (i = 0; i < nargs && i < 3; i++)
	{
		if (s->stack[s->pos + i].type != OP_STRING)
			continue;

		len = s->stack[s->pos + i].length;
		cso[i] = alloca(len + 1);

		if (!cso[i])
			continue;

		memset(cso[i], 0, len + 1);
		memcpy(cso[i], s->stack[s->pos + i].value.string, len);
	}

	if (!cso[0] || !cso[1] || (*cso[1] != '@' && !cso[2]))
		return false;

	if (*cso[1] != '@')
		return dt_uci_cmp(s, cso[0], cso[1], cso[2]);

	p = dt_uci_lookup(s, cso[0], NULL, NULL, UCI_TYPE_PACKAGE);

	if (!p)
		return false;

	uci_foreach_element(&p->sections, e)
	{
		if (strcmp(uci_to_section(e)->type, cso[1] + 1))
			continue;

		if (!cso[2])
		{
			if (!strcmp(s->value, e->name))
				return true;
		}
		else
		{
			if (dt_uci_cmp(s, cso[0], e->name, cso[2]))
				return true;
		}
	}

	return false;
}


static struct dt_fun dt_types[] = {
	{ "or",			DT_INVALID,	dt_type_or		},
	{ "and",		DT_INVALID,	dt_type_and		},
	{ "not",		DT_INVALID,	dt_type_not		},
	{ "neg",		DT_INVALID,	dt_type_neg		},
	{ "list",		DT_INVALID,	dt_type_list		},
	{ "min",		DT_NUMBER,	dt_type_min		},
	{ "max",		DT_NUMBER,	dt_type_max		},
	{ "range",		DT_NUMBER,	dt_type_range		},
	{ "minlength",		DT_STRING,	dt_type_minlen		},
	{ "maxlength",		DT_STRING,	dt_type_maxlen		},
	{ "rangelength",	DT_STRING,	dt_type_rangelen	},
	{ "integer",		DT_NUMBER,	dt_type_int		},
	{ "uinteger",		DT_NUMBER,	dt_type_uint		},
	{ "float",		DT_NUMBER,	dt_type_float		},
	{ "ufloat",		DT_NUMBER,	dt_type_ufloat		},
	{ "bool",		DT_BOOL,	dt_type_bool		},
	{ "string",		DT_STRING,	dt_type_string		},
	{ "hexstring",		DT_STRING,	dt_type_hexstring	},
	{ "ip4addr",		DT_STRING,	dt_type_ip4addr		},
	{ "ip6addr",		DT_STRING,	dt_type_ip6addr		},
	{ "ipaddr",		DT_STRING,	dt_type_ipaddr		},
	{ "cidr4",		DT_STRING,	dt_type_cidr4		},
	{ "cidr6",		DT_STRING,	dt_type_cidr6		},
	{ "cidr",		DT_STRING,	dt_type_cidr		},
	{ "netmask4",		DT_STRING,	dt_type_netmask4	},
	{ "netmask6",		DT_STRING,	dt_type_netmask6	},
	{ "ipmask4",		DT_STRING,	dt_type_ipmask4		},
	{ "ipmask6",		DT_STRING,	dt_type_ipmask6		},
	{ "ipmask",		DT_STRING,	dt_type_ipmask		},
	{ "port",		DT_NUMBER,	dt_type_port		},
	{ "portrange",		DT_STRING,	dt_type_portrange	},
	{ "macaddr",		DT_STRING,	dt_type_macaddr		},
	{ "uciname",		DT_STRING,	dt_type_uciname		},
	{ "wpakey",		DT_STRING,	dt_type_wpakey		},
	{ "wepkey",		DT_STRING,	dt_type_wepkey		},
	{ "hostname",		DT_STRING,	dt_type_hostname	},
	{ "host",		DT_STRING,	dt_type_host		},
	{ "network",		DT_STRING,	dt_type_network		},
	{ "phonedigit",		DT_STRING,	dt_type_phonedigit	},
	{ "directory",		DT_STRING,	dt_type_directory	},
	{ "device",		DT_STRING,	dt_type_device		},
	{ "file",		DT_STRING,	dt_type_file		},
	{ "regex",		DT_STRING,	dt_type_regex		},
	{ "uci",		DT_STRING,	dt_type_uci		},

	{ }
};

static struct dt_fun *
dt_lookup_function(const char *s, const char *e)
{
	struct dt_fun *fun = dt_types;

	while (fun->name)
	{
		if (!strncmp(fun->name, s, e - s) && *(fun->name + (e - s)) == '\0')
			return fun;

		fun++;
	}

	return NULL;
}

static bool
dt_parse_atom(struct dt_state *s, const char *label, const char *end)
{
	char q, *e;
	const char *p;
	bool esc;
	double dval;
	struct dt_fun *func;
	struct dt_op *op = &s->stack[s->depth];

	if ((s->depth + 1) >= (sizeof(s->stack) / sizeof(s->stack[0])))
	{
		printf("Syntax error, expression too long\n");
		return false;
	}

	while (isspace(*label))
		label++;

	/* test whether label is a float */
	dval = strtod(label, &e);

	if (e > label)
	{
		op->next = e;
		op->type = OP_NUMBER;
		op->value.number = dval;
		op->nextop = ++s->depth;

		return true;
	}
	else if ((*label == '"') || (*label == '\''))
	{
		for (p = label + 1, q = *label, esc = false; p <= end; p++)
		{
			if (esc)
			{
				esc = false;
				continue;
			}
			else if (*p == '\\')
			{
				esc = true;
				continue;
			}
			else if (*p == q)
			{
				op->next = p + 1;
				op->type = OP_STRING;
				op->length = (p - label) - 1;
				op->value.string = label + 1;
				op->nextop = ++s->depth;

				return true;
			}
		}

		printf("Syntax error, unterminated string\n");
		return false;
	}
	else if (*label)
	{
		for (p = label;
		     p <= end && ((*p >= 'A' && *p <= 'Z') ||
		                  (*p >= 'a' && *p <= 'z') ||
		                  (*p >= '0' && *p <= '9') ||
		                  (*p == '_'));
		     p++);

		func = dt_lookup_function(label, p);

		if (!func)
		{
			printf("Syntax error, unrecognized function\n");
			return false;
		}

		op->next = p;
		op->type = OP_FUNCTION;
		op->value.function = func;
		op->nextop = ++s->depth;

		return true;
	}

	printf("Syntax error, unexpected EOF\n");
	return false;
}

static bool
dt_parse_list(struct dt_state *s, const char *code, const char *end);

static bool
dt_parse_expr(const char *code, const char *end, struct dt_state *s)
{
	struct dt_op *tok;

	if (!dt_parse_atom(s, code, end))
		return false;

	tok = &s->stack[s->depth - 1];

	while (isspace(*tok->next))
		tok->next++;

	if (tok->type == OP_FUNCTION)
	{
		if (*tok->next == '(')
		{
			end--;

			while (isspace(*end) && end > tok->next + 1)
				end--;

			return dt_parse_list(s, tok->next + 1, end);
		}
		else if (tok->next == end)
		{
			return dt_parse_list(s, tok->next, tok->next);
		}

		printf("Syntax error, expected '(' or EOF after function label\n");
		return false;
	}
	else if (tok->next == end)
	{
		return true;
	}

	printf("Syntax error, expected ',' after literal\n");
	return false;
}

static bool
dt_parse_list(struct dt_state *s, const char *code, const char *end)
{
	char c;
	bool esc;
	int nest;
	const char *p, *last;
	struct dt_op *fptr;

	if (!code)
		return false;

	fptr = &s->stack[s->depth - 1];

	for (nest = 0, p = last = code, esc = false, c = *p;
	     p <= end;
	     p++, c = (p < end) ? *p : '\0')
	{
		if (esc)
		{
			esc = false;
			continue;
		}

		switch (c)
		{
		case '\\':
			esc = true;
			break;

		case '(':
			nest++;
			break;

		case ')':
			nest--;
			break;

		case ',':
		case '\0':
			if (nest <= 0)
			{
				if (p > last)
				{
					if (!dt_parse_expr(last, p, s))
						return false;

					fptr->length++;
				}

				last = p + 1;
			}

			break;
		}
	}

	fptr->nextop = s->depth;
	return true;
}

static bool
dt_step(struct dt_state *s)
{
	bool rv;
	struct dt_op *op = &s->stack[s->pos];

	switch (op->type)
	{
	case OP_NUMBER:
		rv = dt_test_number(op->value.number, s->value);
		if (rv)
			s->valtype = DT_NUMBER;
		break;

	case OP_STRING:
		rv = dt_test_string(op->value.string, op->value.string + op->length, s->value);
		if (rv)
			s->valtype = DT_STRING;
		break;

	case OP_FUNCTION:
		rv = dt_call(s);
		break;

	default:
		rv = false;
		break;
	}

	s->pos = op->nextop;
	return rv;
}

static bool
dt_call(struct dt_state *s)
{
	bool rv;
	struct dt_op *fptr = &s->stack[s->pos];
	struct dt_fun *func = fptr->value.function;

	s->pos++;

	rv = func->call(s, fptr->length);

	if (rv && func->valtype)
		s->valtype = func->valtype;

	s->pos = fptr->nextop;

	return rv;
}

enum dt_type
dt_parse(const char *code, const char *value)
{
	enum dt_type rv = DT_INVALID;

	struct dt_state s = {
		.depth = 1,
		.stack = {
			{
				.type = OP_FUNCTION,
				.value.function = &dt_types[0],
				.next = code
			}
		}
	};

	if (!value || !*value)
		return false;

	if (!dt_parse_list(&s, code, code + strlen(code)))
		return false;

	s.ctx = uci_alloc_context();
	s.value = value;

	if (dt_call(&s))
		rv = s.valtype;

	if (s.ctx)
		uci_free_context(s.ctx);

	return rv;
}