1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
|
/* $OpenBSD: schema.c,v 1.17 2017/01/20 11:55:08 benno Exp $ */
/*
* Copyright (c) 2010 Martin Hedenfalk <martinh@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "ldapd.h"
#include "log.h"
#define ERROR -1
#define STRING 1
static int
attr_oid_cmp(struct attr_type *a, struct attr_type *b)
{
return strcasecmp(a->oid, b->oid);
}
static int
obj_oid_cmp(struct object *a, struct object *b)
{
return strcasecmp(a->oid, b->oid);
}
static int
oidname_cmp(struct oidname *a, struct oidname *b)
{
return strcasecmp(a->on_name, b->on_name);
}
static int
symoid_cmp(struct symoid *a, struct symoid *b)
{
return strcasecmp(a->name, b->name);
}
RB_GENERATE(attr_type_tree, attr_type, link, attr_oid_cmp);
RB_GENERATE(object_tree, object, link, obj_oid_cmp);
RB_GENERATE(oidname_tree, oidname, link, oidname_cmp);
RB_GENERATE(symoid_tree, symoid, link, symoid_cmp);
static struct attr_list *push_attr(struct attr_list *alist, struct attr_type *a);
static struct obj_list *push_obj(struct obj_list *olist, struct object *obj);
static struct name_list *push_name(struct name_list *nl, char *name);
int is_oidstr(const char *oidstr);
struct attr_type *
lookup_attribute_by_name(struct schema *schema, char *name)
{
struct oidname *on, find;
find.on_name = name;
on = RB_FIND(oidname_tree, &schema->attr_names, &find);
if (on)
return on->on_attr_type;
return NULL;
}
struct attr_type *
lookup_attribute_by_oid(struct schema *schema, char *oid)
{
struct attr_type find;
find.oid = oid;
return RB_FIND(attr_type_tree, &schema->attr_types, &find);
}
struct attr_type *
lookup_attribute(struct schema *schema, char *oid_or_name)
{
if (is_oidstr(oid_or_name))
return lookup_attribute_by_oid(schema, oid_or_name);
return lookup_attribute_by_name(schema, oid_or_name);
}
struct object *
lookup_object_by_oid(struct schema *schema, char *oid)
{
struct object find;
find.oid = oid;
return RB_FIND(object_tree, &schema->objects, &find);
}
struct object *
lookup_object_by_name(struct schema *schema, char *name)
{
struct oidname *on, find;
find.on_name = name;
on = RB_FIND(oidname_tree, &schema->object_names, &find);
if (on)
return on->on_object;
return NULL;
}
struct object *
lookup_object(struct schema *schema, char *oid_or_name)
{
if (is_oidstr(oid_or_name))
return lookup_object_by_oid(schema, oid_or_name);
return lookup_object_by_name(schema, oid_or_name);
}
/*
* Looks up a symbolic OID, optionally with a suffix OID, so if
* SYMBOL = 1.2.3.4
* then
* SYMBOL:5.6 = 1.2.3.4.5.6
*
* Returned string must be freed by the caller.
* Modifies the name argument.
*/
char *
lookup_symbolic_oid(struct schema *schema, char *name)
{
struct symoid *symoid, find;
char *colon, *oid;
size_t sz;
colon = strchr(name, ':');
if (colon != NULL) {
if (!is_oidstr(colon + 1)) {
log_warnx("invalid OID after colon: %s", colon + 1);
return NULL;
}
*colon = '\0';
}
find.name = name;
symoid = RB_FIND(symoid_tree, &schema->symbolic_oids, &find);
if (symoid == NULL)
return NULL;
if (colon == NULL)
return strdup(symoid->oid);
/* Expand SYMBOL:OID. */
sz = strlen(symoid->oid) + 1 + strlen(colon + 1) + 1;
if ((oid = malloc(sz)) == NULL) {
log_warnx("malloc");
return NULL;
}
strlcpy(oid, symoid->oid, sz);
strlcat(oid, ".", sz);
strlcat(oid, colon + 1, sz);
return oid;
}
/*
* Push a symbol-OID pair on the tree. Name and OID must be valid pointers
* during the lifetime of the tree.
*/
static struct symoid *
push_symbolic_oid(struct schema *schema, char *name, char *oid)
{
struct symoid *symoid, find;
find.name = name;
symoid = RB_FIND(symoid_tree, &schema->symbolic_oids, &find);
if (symoid == NULL) {
symoid = calloc(1, sizeof(*symoid));
if (symoid == NULL) {
log_warnx("calloc");
return NULL;
}
symoid->name = name;
RB_INSERT(symoid_tree, &schema->symbolic_oids, symoid);
}
free(symoid->oid);
symoid->oid = oid;
return symoid;
}
static struct attr_list *
push_attr(struct attr_list *alist, struct attr_type *a)
{
struct attr_ptr *aptr;
if (alist == NULL) {
if ((alist = calloc(1, sizeof(*alist))) == NULL) {
log_warn("calloc");
return NULL;
}
SLIST_INIT(alist);
}
if ((aptr = calloc(1, sizeof(*aptr))) == NULL) {
log_warn("calloc");
free(alist);
return NULL;
}
aptr->attr_type = a;
SLIST_INSERT_HEAD(alist, aptr, next);
return alist;
}
static struct obj_list *
push_obj(struct obj_list *olist, struct object *obj)
{
struct obj_ptr *optr;
if (olist == NULL) {
if ((olist = calloc(1, sizeof(*olist))) == NULL) {
log_warn("calloc");
return NULL;
}
SLIST_INIT(olist);
}
if ((optr = calloc(1, sizeof(*optr))) == NULL) {
log_warn("calloc");
free(olist);
return NULL;
}
optr->object = obj;
SLIST_INSERT_HEAD(olist, optr, next);
return olist;
}
int
is_oidstr(const char *oidstr)
{
struct ber_oid oid;
return (ber_string2oid(oidstr, &oid) == 0);
}
static struct name_list *
push_name(struct name_list *nl, char *name)
{
struct name *n;
if (nl == NULL) {
if ((nl = calloc(1, sizeof(*nl))) == NULL) {
log_warn("calloc");
return NULL;
}
SLIST_INIT(nl);
}
if ((n = calloc(1, sizeof(*n))) == NULL) {
log_warn("calloc");
free(nl);
return NULL;
}
n->name = name;
SLIST_INSERT_HEAD(nl, n, next);
return nl;
}
static int
schema_getc(struct schema *schema, int quotec)
{
int c, next;
if (schema->pushback_index)
return (schema->pushback_buffer[--schema->pushback_index]);
if (quotec) {
if ((c = getc(schema->fp)) == EOF) {
log_warnx("reached end of file while parsing "
"quoted string");
return EOF;
}
return (c);
}
while ((c = getc(schema->fp)) == '\\') {
next = getc(schema->fp);
if (next != '\n') {
c = next;
break;
}
schema->lineno++;
}
return (c);
}
static int
schema_ungetc(struct schema *schema, int c)
{
if (c == EOF)
return EOF;
if (schema->pushback_index < SCHEMA_MAXPUSHBACK-1)
return (schema->pushback_buffer[schema->pushback_index++] = c);
else
return (EOF);
}
static int
findeol(struct schema *schema)
{
int c;
/* skip to either EOF or the first real EOL */
while (1) {
if (schema->pushback_index)
c = schema->pushback_buffer[--schema->pushback_index];
else
c = schema_getc(schema, 0);
if (c == '\n') {
schema->lineno++;
break;
}
if (c == EOF)
break;
}
return (ERROR);
}
static int
schema_lex(struct schema *schema, char **kw)
{
char buf[8096];
char *p;
int quotec, next, c;
if (kw)
*kw = NULL;
top:
p = buf;
while ((c = schema_getc(schema, 0)) == ' ' || c == '\t')
; /* nothing */
if (c == '#')
while ((c = schema_getc(schema, 0)) != '\n' && c != EOF)
; /* nothing */
switch (c) {
case '\'':
case '"':
quotec = c;
while (1) {
if ((c = schema_getc(schema, quotec)) == EOF)
return (0);
if (c == '\n') {
schema->lineno++;
continue;
} else if (c == '\\') {
if ((next = schema_getc(schema, quotec)) == EOF)
return (0);
if (next == quotec || c == ' ' || c == '\t')
c = next;
else if (next == '\n')
continue;
else
schema_ungetc(schema, next);
} else if (c == quotec) {
*p = '\0';
break;
}
if (p + 1 >= buf + sizeof(buf) - 1) {
log_warnx("string too long");
return (findeol(schema));
}
*p++ = (char)c;
}
if (kw != NULL && (*kw = strdup(buf)) == NULL)
fatal("schema_lex: strdup");
return (STRING);
}
#define allowed_in_string(x) \
(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
x != '{' && x != '}' && x != '<' && x != '>' && \
x != '!' && x != '=' && x != '/' && x != '#' && \
x != ','))
if (isalnum(c) || c == ':' || c == '_' || c == '*') {
do {
*p++ = c;
if ((unsigned)(p-buf) >= sizeof(buf)) {
log_warnx("string too long");
return (findeol(schema));
}
} while ((c = schema_getc(schema, 0)) != EOF && (allowed_in_string(c)));
schema_ungetc(schema, c);
*p = '\0';
if (kw != NULL && (*kw = strdup(buf)) == NULL)
fatal("schema_lex: strdup");
return STRING;
}
if (c == '\n') {
schema->lineno++;
goto top;
}
if (c == EOF)
return (0);
return (c);
}
struct schema *
schema_new(void)
{
struct schema *schema;
if ((schema = calloc(1, sizeof(*schema))) == NULL)
return NULL;
RB_INIT(&schema->attr_types);
RB_INIT(&schema->attr_names);
RB_INIT(&schema->objects);
RB_INIT(&schema->object_names);
RB_INIT(&schema->symbolic_oids);
return schema;
}
static void
schema_err(struct schema *schema, const char *fmt, ...)
{
va_list ap;
char *msg;
va_start(ap, fmt);
if (vasprintf(&msg, fmt, ap) == -1)
fatal("vasprintf");
va_end(ap);
logit(LOG_CRIT, "%s:%d: %s", schema->filename, schema->lineno, msg);
free(msg);
schema->error++;
}
static int
schema_link_attr_name(struct schema *schema, const char *name, struct attr_type *attr)
{
struct oidname *oidname, *prev;
if ((oidname = calloc(1, sizeof(*oidname))) == NULL) {
log_warn("calloc");
return -1;
}
oidname->on_name = name;
oidname->on_attr_type = attr;
prev = RB_INSERT(oidname_tree, &schema->attr_names, oidname);
if (prev != NULL) {
schema_err(schema, "attribute type name '%s'"
" already defined for oid %s",
name, prev->on_attr_type->oid);
free(oidname);
return -1;
}
return 0;
}
static int
schema_link_attr_names(struct schema *schema, struct attr_type *attr)
{
struct name *name;
SLIST_FOREACH(name, attr->names, next) {
if (schema_link_attr_name(schema, name->name, attr) != 0)
return -1;
}
return 0;
}
static int
schema_link_obj_name(struct schema *schema, const char *name, struct object *obj)
{
struct oidname *oidname, *prev;
if ((oidname = calloc(1, sizeof(*oidname))) == NULL) {
log_warn("calloc");
return -1;
}
oidname->on_name = name;
oidname->on_object = obj;
prev = RB_INSERT(oidname_tree, &schema->object_names, oidname);
if (prev != NULL) {
schema_err(schema, "object class name '%s'"
" already defined for oid %s",
name, prev->on_object->oid);
free(oidname);
return -1;
}
return 0;
}
static int
schema_link_obj_names(struct schema *schema, struct object *obj)
{
struct name *name;
SLIST_FOREACH(name, obj->names, next) {
if (schema_link_obj_name(schema, name->name, obj) != 0)
return -1;
}
return 0;
}
static struct name_list *
schema_parse_names(struct schema *schema)
{
struct name_list *nlist = NULL;
char *kw;
int token;
token = schema_lex(schema, &kw);
if (token == STRING)
return push_name(NULL, kw);
if (token != '(')
goto fail;
for (;;) {
token = schema_lex(schema, &kw);
if (token == ')')
break;
if (token != STRING)
goto fail;
nlist = push_name(nlist, kw);
}
return nlist;
fail:
free(kw);
/* FIXME: leaks nlist here */
return NULL;
}
static void
schema_free_name_list(struct name_list *nlist)
{
struct name *name;
while ((name = SLIST_FIRST(nlist)) != NULL) {
SLIST_REMOVE_HEAD(nlist, next);
free(name->name);
free(name);
}
free(nlist);
}
static struct attr_list *
schema_parse_attrlist(struct schema *schema)
{
struct attr_list *alist = NULL;
struct attr_type *attr;
char *kw;
int token, want_dollar = 0;
token = schema_lex(schema, &kw);
if (token == STRING) {
if ((attr = lookup_attribute(schema, kw)) == NULL) {
schema_err(schema, "undeclared attribute type '%s'", kw);
goto fail;
}
free(kw);
return push_attr(NULL, attr);
}
if (token != '(')
goto fail;
for (;;) {
token = schema_lex(schema, &kw);
if (token == ')')
break;
if (token == '$') {
if (!want_dollar)
goto fail;
want_dollar = 0;
continue;
}
if (token != STRING)
goto fail;
if ((attr = lookup_attribute(schema, kw)) == NULL) {
schema_err(schema, "%s: no such attribute", kw);
goto fail;
}
alist = push_attr(alist, attr);
free(kw);
want_dollar = 1;
}
return alist;
fail:
free(kw);
/* FIXME: leaks alist here */
return NULL;
}
static struct obj_list *
schema_parse_objlist(struct schema *schema)
{
struct obj_list *olist = NULL;
struct object *obj;
char *kw;
int token, want_dollar = 0;
token = schema_lex(schema, &kw);
if (token == STRING) {
if ((obj = lookup_object(schema, kw)) == NULL) {
schema_err(schema, "undeclared object class '%s'", kw);
goto fail;
}
free(kw);
return push_obj(NULL, obj);
}
if (token != '(')
goto fail;
for (;;) {
token = schema_lex(schema, &kw);
if (token == ')')
break;
if (token == '$') {
if (!want_dollar)
goto fail;
want_dollar = 0;
continue;
}
if (token != STRING)
goto fail;
if ((obj = lookup_object(schema, kw)) == NULL)
goto fail;
olist = push_obj(olist, obj);
want_dollar = 1;
}
return olist;
fail:
free(kw);
/* FIXME: leaks olist here */
return NULL;
}
static int
schema_validate_match_rule(struct schema *schema, struct attr_type *at,
const struct match_rule *mrule, enum match_rule_type type)
{
int i;
if (mrule == NULL)
return 0;
if ((mrule->type & type) != type) {
schema_err(schema, "%s: bad matching rule '%s'",
ATTR_NAME(at), mrule->name);
return -1;
}
/* Is this matching rule compatible with the attribute syntax? */
if (strcmp(mrule->syntax_oid, at->syntax->oid) == 0)
return 0;
/* Check any alternative syntaxes for compatibility. */
for (i = 0; mrule->alt_syntax_oids && mrule->alt_syntax_oids[i]; i++)
if (strcmp(mrule->alt_syntax_oids[i], at->syntax->oid) == 0)
return 0;
schema_err(schema, "%s: inappropriate matching rule '%s' for syntax [%s]",
ATTR_NAME(at), mrule->name, at->syntax->oid);
return -1;
}
static int
schema_parse_attributetype(struct schema *schema)
{
struct attr_type *attr = NULL, *prev, *sup;
struct name_list *xnames;
char *kw = NULL, *arg = NULL;
int token, ret = 0, c;
if (schema_lex(schema, NULL) != '(')
goto fail;
if (schema_lex(schema, &kw) != STRING)
goto fail;
if ((attr = calloc(1, sizeof(*attr))) == NULL) {
log_warn("calloc");
goto fail;
}
attr->usage = USAGE_USER_APP;
if (is_oidstr(kw))
attr->oid = kw;
else {
attr->oid = lookup_symbolic_oid(schema, kw);
if (attr->oid == NULL)
goto fail;
free(kw);
}
kw = NULL;
prev = RB_INSERT(attr_type_tree, &schema->attr_types, attr);
if (prev != NULL) {
schema_err(schema, "attribute type %s already defined", attr->oid);
goto fail;
}
while (ret == 0) {
token = schema_lex(schema, &kw);
if (token == ')')
break;
else if (token != STRING)
goto fail;
if (strcasecmp(kw, "NAME") == 0) {
attr->names = schema_parse_names(schema);
if (attr->names == NULL)
goto fail;
schema_link_attr_names(schema, attr);
} else if (strcasecmp(kw, "DESC") == 0) {
if (schema_lex(schema, &attr->desc) != STRING)
goto fail;
} else if (strcasecmp(kw, "OBSOLETE") == 0) {
attr->obsolete = 1;
} else if (strcasecmp(kw, "SUP") == 0) {
if (schema_lex(schema, &arg) != STRING)
goto fail;
if ((attr->sup = lookup_attribute(schema, arg)) == NULL) {
schema_err(schema, "%s: no such attribute", arg);
goto fail;
}
free(arg);
} else if (strcasecmp(kw, "EQUALITY") == 0) {
if (schema_lex(schema, &arg) != STRING)
goto fail;
if ((attr->equality = match_rule_lookup(arg)) == NULL) {
schema_err(schema, "%s: unknown matching rule",
arg);
goto fail;
}
free(arg);
} else if (strcasecmp(kw, "ORDERING") == 0) {
if (schema_lex(schema, &arg) != STRING)
goto fail;
if ((attr->ordering = match_rule_lookup(arg)) == NULL) {
schema_err(schema, "%s: unknown matching rule",
arg);
goto fail;
}
free(arg);
} else if (strcasecmp(kw, "SUBSTR") == 0) {
if (schema_lex(schema, &arg) != STRING)
goto fail;
if ((attr->substr = match_rule_lookup(arg)) == NULL) {
schema_err(schema, "%s: unknown matching rule",
arg);
goto fail;
}
free(arg);
} else if (strcasecmp(kw, "SYNTAX") == 0) {
if (schema_lex(schema, &arg) != STRING ||
!is_oidstr(arg))
goto fail;
if ((attr->syntax = syntax_lookup(arg)) == NULL) {
schema_err(schema, "syntax not supported: %s",
arg);
goto fail;
}
if ((c = schema_getc(schema, 0)) == '{') {
if (schema_lex(schema, NULL) != STRING ||
schema_lex(schema, NULL) != '}')
goto fail;
} else
schema_ungetc(schema, c);
free(arg);
} else if (strcasecmp(kw, "SINGLE-VALUE") == 0) {
attr->single = 1;
} else if (strcasecmp(kw, "COLLECTIVE") == 0) {
attr->collective = 1;
} else if (strcasecmp(kw, "NO-USER-MODIFICATION") == 0) {
attr->immutable = 1;
} else if (strcasecmp(kw, "USAGE") == 0) {
if (schema_lex(schema, &arg) != STRING)
goto fail;
if (strcasecmp(arg, "dSAOperation") == 0)
attr->usage = USAGE_DSA_OP;
else if (strcasecmp(arg, "directoryOperation") == 0)
attr->usage = USAGE_DIR_OP;
else if (strcasecmp(arg, "distributedOperation") == 0)
attr->usage = USAGE_DIST_OP;
else if (strcasecmp(arg, "userApplications") == 0)
attr->usage = USAGE_USER_APP;
else {
schema_err(schema, "invalid usage '%s'", arg);
goto fail;
}
free(arg);
} else if (strncmp(kw, "X-", 2) == 0) {
/* unknown extension, eat argument(s) */
xnames = schema_parse_names(schema);
if (xnames == NULL)
goto fail;
schema_free_name_list(xnames);
} else {
schema_err(schema, "syntax error at token '%s'", kw);
goto fail;
}
free(kw);
}
/* Check that a syntax is defined, either directly or
* indirectly via a superior attribute type.
*/
sup = attr->sup;
while (attr->syntax == NULL && sup != NULL) {
attr->syntax = sup->syntax;
sup = sup->sup;
}
if (attr->syntax == NULL) {
schema_err(schema, "%s: no syntax defined", ATTR_NAME(attr));
goto fail;
}
/* If the attribute type doesn't explicitly define equality, check
* if any superior attribute type does.
*/
sup = attr->sup;
while (attr->equality == NULL && sup != NULL) {
attr->equality = sup->equality;
sup = sup->sup;
}
/* Same thing with ordering matching rule. */
sup = attr->sup;
while (attr->ordering == NULL && sup != NULL) {
attr->ordering = sup->ordering;
sup = sup->sup;
}
/* ...and substring matching rule. */
sup = attr->sup;
while (attr->substr == NULL && sup != NULL) {
attr->substr = sup->substr;
sup = sup->sup;
}
if (schema_validate_match_rule(schema, attr, attr->equality, MATCH_EQUALITY) != 0 ||
schema_validate_match_rule(schema, attr, attr->ordering, MATCH_ORDERING) != 0 ||
schema_validate_match_rule(schema, attr, attr->substr, MATCH_SUBSTR) != 0)
goto fail;
return 0;
fail:
free(kw);
if (attr != NULL) {
if (attr->oid != NULL) {
RB_REMOVE(attr_type_tree, &schema->attr_types, attr);
free(attr->oid);
}
free(attr->desc);
free(attr);
}
return -1;
}
static int
schema_parse_objectclass(struct schema *schema)
{
struct object *obj = NULL, *prev;
struct obj_ptr *optr;
struct name_list *xnames;
char *kw = NULL;
int token, ret = 0;
if (schema_lex(schema, NULL) != '(')
goto fail;
if (schema_lex(schema, &kw) != STRING)
goto fail;
if ((obj = calloc(1, sizeof(*obj))) == NULL) {
log_warn("calloc");
goto fail;
}
obj->kind = KIND_STRUCTURAL;
if (is_oidstr(kw))
obj->oid = kw;
else {
obj->oid = lookup_symbolic_oid(schema, kw);
if (obj->oid == NULL)
goto fail;
free(kw);
}
kw = NULL;
prev = RB_INSERT(object_tree, &schema->objects, obj);
if (prev != NULL) {
schema_err(schema, "object class %s already defined", obj->oid);
goto fail;
}
while (ret == 0) {
token = schema_lex(schema, &kw);
if (token == ')')
break;
else if (token != STRING)
goto fail;
if (strcasecmp(kw, "NAME") == 0) {
obj->names = schema_parse_names(schema);
if (obj->names == NULL)
goto fail;
schema_link_obj_names(schema, obj);
} else if (strcasecmp(kw, "DESC") == 0) {
if (schema_lex(schema, &obj->desc) != STRING)
goto fail;
} else if (strcasecmp(kw, "OBSOLETE") == 0) {
obj->obsolete = 1;
} else if (strcasecmp(kw, "SUP") == 0) {
obj->sup = schema_parse_objlist(schema);
if (obj->sup == NULL)
goto fail;
} else if (strcasecmp(kw, "ABSTRACT") == 0) {
obj->kind = KIND_ABSTRACT;
} else if (strcasecmp(kw, "STRUCTURAL") == 0) {
obj->kind = KIND_STRUCTURAL;
} else if (strcasecmp(kw, "AUXILIARY") == 0) {
obj->kind = KIND_AUXILIARY;
} else if (strcasecmp(kw, "MUST") == 0) {
obj->must = schema_parse_attrlist(schema);
if (obj->must == NULL)
goto fail;
} else if (strcasecmp(kw, "MAY") == 0) {
obj->may = schema_parse_attrlist(schema);
if (obj->may == NULL)
goto fail;
} else if (strncasecmp(kw, "X-", 2) == 0) {
/* unknown extension, eat argument(s) */
xnames = schema_parse_names(schema);
if (xnames == NULL)
goto fail;
schema_free_name_list(xnames);
} else {
schema_err(schema, "syntax error at token '%s'", kw);
goto fail;
}
free(kw);
}
/* Verify the subclassing is allowed.
*
* Structural object classes cannot subclass auxiliary object classes.
* Auxiliary object classes cannot subclass structural object classes.
* Abstract object classes cannot derive from structural or auxiliary
* object classes.
*/
if (obj->sup != NULL) {
SLIST_FOREACH(optr, obj->sup, next) {
if (obj->kind == KIND_STRUCTURAL &&
optr->object->kind == KIND_AUXILIARY) {
log_warnx("structural object class '%s' cannot"
" subclass auxiliary object class '%s'",
OBJ_NAME(obj), OBJ_NAME(optr->object));
goto fail;
}
if (obj->kind == KIND_AUXILIARY &&
optr->object->kind == KIND_STRUCTURAL) {
log_warnx("auxiliary object class '%s' cannot"
" subclass structural object class '%s'",
OBJ_NAME(obj), OBJ_NAME(optr->object));
goto fail;
}
if (obj->kind == KIND_ABSTRACT &&
optr->object->kind != KIND_ABSTRACT) {
log_warnx("abstract object class '%s' cannot"
" subclass non-abstract object class '%s'",
OBJ_NAME(obj), OBJ_NAME(optr->object));
goto fail;
}
}
}
return 0;
fail:
free(kw);
if (obj != NULL) {
if (obj->oid != NULL) {
RB_REMOVE(object_tree, &schema->objects, obj);
free(obj->oid);
}
free(obj->desc);
free(obj);
}
return -1;
}
static int
schema_parse_objectidentifier(struct schema *schema)
{
char *symname = NULL, *symoid = NULL;
char *oid = NULL;
if (schema_lex(schema, &symname) != STRING)
goto fail;
if (schema_lex(schema, &symoid) != STRING)
goto fail;
if (is_oidstr(symoid)) {
oid = symoid;
symoid = NULL;
} else if ((oid = lookup_symbolic_oid(schema, symoid)) == NULL)
goto fail;
if (push_symbolic_oid(schema, symname, oid) == NULL)
goto fail;
free(symoid);
return 0;
fail:
free(symname);
free(symoid);
free(oid);
return -1;
}
int
schema_parse(struct schema *schema, const char *filename)
{
char *kw;
int token, ret = 0;
log_debug("parsing schema file '%s'", filename);
if ((schema->fp = fopen(filename, "r")) == NULL) {
log_warn("%s", filename);
return -1;
}
schema->filename = filename;
schema->lineno = 1;
while (ret == 0) {
token = schema_lex(schema, &kw);
if (token == STRING) {
if (strcasecmp(kw, "attributetype") == 0)
ret = schema_parse_attributetype(schema);
else if (strcasecmp(kw, "objectclass") == 0)
ret = schema_parse_objectclass(schema);
else if (strcasecmp(kw, "objectidentifier") == 0)
ret = schema_parse_objectidentifier(schema);
else {
schema_err(schema, "syntax error at '%s'", kw);
ret = -1;
}
if (ret == -1 && schema->error == 0)
schema_err(schema, "syntax error");
free(kw);
} else if (token == 0) { /* EOF */
break;
} else {
schema_err(schema, "syntax error");
ret = -1;
}
}
fclose(schema->fp);
schema->fp = NULL;
schema->filename = NULL;
return ret;
}
static int
schema_dump_names(const char *desc, struct name_list *nlist,
char *buf, size_t size)
{
struct name *name;
if (nlist == NULL || SLIST_EMPTY(nlist))
return 0;
if (strlcat(buf, " ", size) >= size ||
strlcat(buf, desc, size) >= size)
return -1;
name = SLIST_FIRST(nlist);
if (SLIST_NEXT(name, next) == NULL) {
/* single name, no parenthesis */
if (strlcat(buf, " '", size) >= size ||
strlcat(buf, name->name, size) >= size ||
strlcat(buf, "'", size) >= size)
return -1;
} else {
if (strlcat(buf, " ( ", size) >= size)
return -1;
SLIST_FOREACH(name, nlist, next)
if (strlcat(buf, "'", size) >= size ||
strlcat(buf, name->name, size) >= size ||
strlcat(buf, "' ", size) >= size)
return -1;
if (strlcat(buf, ")", size) >= size)
return -1;
}
return 0;
}
static int
schema_dump_attrlist(const char *desc, struct attr_list *alist,
char *buf, size_t size)
{
struct attr_ptr *aptr;
if (alist == NULL || SLIST_EMPTY(alist))
return 0;
if (strlcat(buf, " ", size) >= size ||
strlcat(buf, desc, size) >= size)
return -1;
aptr = SLIST_FIRST(alist);
if (SLIST_NEXT(aptr, next) == NULL) {
/* single attribute, no parenthesis */
if (strlcat(buf, " ", size) >= size ||
strlcat(buf, ATTR_NAME(aptr->attr_type), size) >= size)
return -1;
} else {
if (strlcat(buf, " ( ", size) >= size)
return -1;
SLIST_FOREACH(aptr, alist, next) {
if (strlcat(buf, ATTR_NAME(aptr->attr_type),
size) >= size ||
strlcat(buf, " ", size) >= size)
return -1;
if (SLIST_NEXT(aptr, next) != NULL &&
strlcat(buf, "$ ", size) >= size)
return -1;
}
if (strlcat(buf, ")", size) >= size)
return -1;
}
return 0;
}
static int
schema_dump_objlist(const char *desc, struct obj_list *olist,
char *buf, size_t size)
{
struct obj_ptr *optr;
if (olist == NULL || SLIST_EMPTY(olist))
return 0;
if (strlcat(buf, " ", size) >= size ||
strlcat(buf, desc, size) >= size)
return -1;
optr = SLIST_FIRST(olist);
if (SLIST_NEXT(optr, next) == NULL) {
/* single attribute, no parenthesis */
if (strlcat(buf, " ", size) >= size ||
strlcat(buf, OBJ_NAME(optr->object), size) >= size)
return -1;
} else {
if (strlcat(buf, " ( ", size) >= size)
return -1;
SLIST_FOREACH(optr, olist, next) {
if (strlcat(buf, OBJ_NAME(optr->object), size) >= size ||
strlcat(buf, " ", size) >= size)
return -1;
if (SLIST_NEXT(optr, next) != NULL &&
strlcat(buf, "$ ", size) >= size)
return -1;
}
if (strlcat(buf, ")", size) >= size)
return -1;
}
return 0;
}
int
schema_dump_object(struct object *obj, char *buf, size_t size)
{
if (strlcpy(buf, "( ", size) >= size ||
strlcat(buf, obj->oid, size) >= size)
return -1;
if (schema_dump_names("NAME", obj->names, buf, size) != 0)
return -1;
if (obj->desc != NULL)
if (strlcat(buf, " DESC '", size) >= size ||
strlcat(buf, obj->desc, size) >= size ||
strlcat(buf, "'", size) >= size)
return -1;
switch (obj->kind) {
case KIND_STRUCTURAL:
if (strlcat(buf, " STRUCTURAL", size) >= size)
return -1;
break;
case KIND_ABSTRACT:
if (strlcat(buf, " ABSTRACT", size) >= size)
return -1;
break;
case KIND_AUXILIARY:
if (strlcat(buf, " AUXILIARY", size) >= size)
return -1;
break;
}
if (schema_dump_objlist("SUP", obj->sup, buf, size) != 0)
return -1;
if (obj->obsolete && strlcat(buf, " OBSOLETE", size) >= size)
return -1;
if (schema_dump_attrlist("MUST", obj->must, buf, size) != 0)
return -1;
if (schema_dump_attrlist("MAY", obj->may, buf, size) != 0)
return -1;
if (strlcat(buf, " )", size) >= size)
return -1;
return 0;
}
int
schema_dump_attribute(struct attr_type *at, char *buf, size_t size)
{
if (strlcpy(buf, "( ", size) >= size ||
strlcat(buf, at->oid, size) >= size)
return -1;
if (schema_dump_names("NAME", at->names, buf, size) != 0)
return -1;
if (at->desc != NULL)
if (strlcat(buf, " DESC '", size) >= size ||
strlcat(buf, at->desc, size) >= size ||
strlcat(buf, "'", size) >= size)
return -1;
if (at->obsolete && strlcat(buf, " OBSOLETE", size) >= size)
return -1;
if (at->sup != NULL)
if (strlcat(buf, " SUP ", size) >= size ||
strlcat(buf, ATTR_NAME(at->sup), size) >= size)
return -1;
if (at->equality != NULL)
if (strlcat(buf, " EQUALITY ", size) >= size ||
strlcat(buf, at->equality->name, size) >= size)
return -1;
if (at->ordering != NULL)
if (strlcat(buf, " ORDERING ", size) >= size ||
strlcat(buf, at->ordering->name, size) >= size)
return -1;
if (at->substr != NULL)
if (strlcat(buf, " SUBSTR ", size) >= size ||
strlcat(buf, at->substr->name, size) >= size)
return -1;
if (at->syntax != NULL)
if (strlcat(buf, " SYNTAX ", size) >= size ||
strlcat(buf, at->syntax->oid, size) >= size)
return -1;
if (at->single && strlcat(buf, " SINGLE-VALUE", size) >= size)
return -1;
if (at->collective && strlcat(buf, " COLLECTIVE", size) >= size)
return -1;
if (at->immutable && strlcat(buf, " NO-USER-MODIFICATION", size) >= size)
return -1;
switch (at->usage) {
case USAGE_USER_APP:
/* User application usage is the default. */
break;
case USAGE_DIR_OP:
if (strlcat(buf, " USAGE directoryOperation", size) >= size)
return -1;
break;
case USAGE_DIST_OP:
if (strlcat(buf, " USAGE distributedOperation", size) >= size)
return -1;
break;
case USAGE_DSA_OP:
if (strlcat(buf, " USAGE dSAOperation", size) >= size)
return -1;
break;
}
if (strlcat(buf, " )", size) >= size)
return -1;
return 0;
}
int
schema_dump_match_rule(struct match_rule *mr, char *buf, size_t size)
{
if (strlcpy(buf, "( ", size) >= size ||
strlcat(buf, mr->oid, size) >= size ||
strlcat(buf, " NAME '", size) >= size ||
strlcat(buf, mr->name, size) >= size ||
strlcat(buf, "' SYNTAX ", size) >= size ||
strlcat(buf, mr->syntax_oid, size) >= size ||
strlcat(buf, " )", size) >= size)
return -1;
return 0;
}
|