2 * Copyright 1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * schema.c: parsing routines used by servers and clients to process
15 #include <ac/string.h>
20 #include <ldap_schema.h>
23 * When pretty printing the entities we will be appending to a buffer.
24 * Since checking for overflow, realloc'ing and checking if no error
25 * is extremely boring, we will use a protection layer that will let
26 * us blissfully ignore the error until the end. This layer is
27 * implemented with the help of the next type.
30 typedef struct safe_string {
38 new_safe_string(int size)
42 ss = LDAP_MALLOC(sizeof(safe_string));
47 ss->val = LDAP_MALLOC(size);
57 safe_string_free(safe_string * ss)
61 ldap_memfree(ss->val);
66 safe_string_val(safe_string * ss)
68 ss->val[ss->pos] = '\0';
73 append_to_safe_string(safe_string * ss, char * s)
79 * Some runaway process is trying to append to a string that
80 * overflowed and we could not extend.
85 /* We always make sure there is at least one position available */
86 if ( ss->pos + l >= ss->size-1 ) {
88 temp = LDAP_REALLOC(ss->val, ss->size);
90 /* Trouble, out of memory */
96 strncpy(&ss->val[ss->pos], s, l);
98 if ( ss->pos > 0 && ss->val[ss->pos-1] == ' ' )
107 print_literal(safe_string *ss, char *s)
109 return(append_to_safe_string(ss,s));
113 print_whsp(safe_string *ss)
116 return(append_to_safe_string(ss,""));
118 return(append_to_safe_string(ss," "));
122 print_numericoid(safe_string *ss, char *s)
125 return(append_to_safe_string(ss,s));
128 /* This one is identical to print_qdescr */
130 print_qdstring(safe_string *ss, char *s)
133 print_literal(ss,"'");
134 append_to_safe_string(ss,s);
135 print_literal(ss,"'");
136 return(print_whsp(ss));
140 print_qdescr(safe_string *ss, char *s)
143 print_literal(ss,"'");
144 append_to_safe_string(ss,s);
145 print_literal(ss,"'");
146 return(print_whsp(ss));
150 print_qdescrlist(safe_string *ss, char **sa)
155 for (sp=sa; *sp; sp++) {
156 ret = print_qdescr(ss,*sp);
158 /* If the list was empty, we return zero that is potentially
159 * incorrect, but since we will be still appending things, the
160 * overflow will be detected later. Maybe FIX.
166 print_qdescrs(safe_string *ss, char **sa)
168 /* The only way to represent an empty list is as a qdescrlist
169 * so, if the list is empty we treat it as a long list.
170 * Really, this is what the syntax mandates. We should not
171 * be here if the list was empty, but if it happens, a label
172 * has already been output and we cannot undo it.
174 if ( !sa[0] || ( sa[0] && sa[1] ) ) {
176 print_literal(ss,"(");
177 print_qdescrlist(ss,sa);
178 print_literal(ss,")");
179 return(print_whsp(ss));
181 return(print_qdescr(ss,*sa));
186 print_woid(safe_string *ss, char *s)
189 append_to_safe_string(ss,s);
190 return print_whsp(ss);
194 print_oidlist(safe_string *ss, char **sa)
198 for (sp=sa; *(sp+1); sp++) {
200 print_literal(ss,"$");
202 return(print_woid(ss,*sp));
206 print_oids(safe_string *ss, char **sa)
208 if ( sa[0] && sa[1] ) {
209 print_literal(ss,"(");
210 print_oidlist(ss,sa);
212 return(print_literal(ss,")"));
214 return(print_woid(ss,*sa));
219 print_noidlen(safe_string *ss, char *s, int l)
224 ret = print_numericoid(ss,s);
226 sprintf(buf,"{%d}",l);
227 ret = print_literal(ss,buf);
233 ldap_objectclass2str( LDAP_OBJECT_CLASS * oc )
238 ss = new_safe_string(256);
242 print_literal(ss,"(");
245 print_numericoid(ss, oc->oc_oid);
248 if ( oc->oc_names ) {
249 print_literal(ss,"NAME");
250 print_qdescrs(ss,oc->oc_names);
254 print_literal(ss,"DESC");
255 print_qdstring(ss,oc->oc_desc);
258 if ( oc->oc_obsolete == LDAP_SCHEMA_YES ) {
259 print_literal(ss, "OBSOLETE");
263 if ( oc->oc_sup_oids ) {
264 print_literal(ss,"SUP");
265 print_oids(ss,oc->oc_sup_oids);
268 switch (oc->oc_kind) {
269 case LDAP_SCHEMA_ABSTRACT:
270 print_literal(ss,"ABSTRACT");
272 case LDAP_SCHEMA_STRUCTURAL:
273 print_literal(ss,"STRUCTURAL");
275 case LDAP_SCHEMA_AUXILIARY:
276 print_literal(ss,"AUXILIARY");
279 print_literal(ss,"KIND-UNKNOWN");
284 if ( oc->oc_at_oids_must ) {
285 print_literal(ss,"MUST");
287 print_oids(ss,oc->oc_at_oids_must);
291 if ( oc->oc_at_oids_may ) {
292 print_literal(ss,"MAY");
294 print_oids(ss,oc->oc_at_oids_may);
299 print_literal(ss,")");
301 retstring = strdup(safe_string_val(ss));
302 safe_string_free(ss);
307 ldap_attributetype2str( LDAP_ATTRIBUTE_TYPE * at )
312 ss = new_safe_string(256);
316 print_literal(ss,"(");
319 print_numericoid(ss, at->at_oid);
322 if ( at->at_names ) {
323 print_literal(ss,"NAME");
324 print_qdescrs(ss,at->at_names);
328 print_literal(ss,"DESC");
329 print_qdstring(ss,at->at_desc);
332 if ( at->at_obsolete == LDAP_SCHEMA_YES ) {
333 print_literal(ss, "OBSOLETE");
337 if ( at->at_sup_oid ) {
338 print_literal(ss,"SUP");
339 print_woid(ss,at->at_sup_oid);
342 if ( at->at_equality_oid ) {
343 print_literal(ss,"EQUALITY");
344 print_woid(ss,at->at_equality_oid);
347 if ( at->at_ordering_oid ) {
348 print_literal(ss,"ORDERING");
349 print_woid(ss,at->at_ordering_oid);
352 if ( at->at_substr_oid ) {
353 print_literal(ss,"SUBSTR");
354 print_woid(ss,at->at_substr_oid);
357 if ( at->at_syntax_oid ) {
358 print_literal(ss,"SYNTAX");
360 print_noidlen(ss,at->at_syntax_oid,at->at_syntax_len);
363 if ( at->at_single_value == LDAP_SCHEMA_YES ) {
364 print_literal(ss,"SINGLE-VALUE");
368 if ( at->at_collective == LDAP_SCHEMA_YES ) {
369 print_literal(ss,"COLLECTIVE");
373 if ( at->at_no_user_mod == LDAP_SCHEMA_YES ) {
374 print_literal(ss,"NO-USER-MODIFICATION");
378 if ( at->at_usage != LDAP_SCHEMA_USER_APPLICATIONS ) {
379 print_literal(ss,"USAGE");
381 switch (at->at_usage) {
382 case LDAP_SCHEMA_DIRECTORY_OPERATION:
383 print_literal(ss,"directoryOperation");
385 case LDAP_SCHEMA_DISTRIBUTED_OPERATION:
386 print_literal(ss,"distributedOperation");
388 case LDAP_SCHEMA_DSA_OPERATION:
389 print_literal(ss,"dSAOperation");
392 print_literal(ss,"UNKNOWN");
398 print_literal(ss,")");
400 retstring = strdup(safe_string_val(ss));
401 safe_string_free(ss);
406 * This is ripped from servers/slapd/charray.c that should be promoted
407 * to -lldap or something so that it is used everywhere.
410 charray_free( char **array )
414 if ( array == NULL ) {
418 for ( a = array; *a != NULL; a++ ) {
423 LDAP_FREE( (char *) array );
427 * Now come the parsers. There is one parser for each entity type:
428 * objectclasses, attributetypes, etc.
430 * Each of them is written as a recursive-descent parser, except that
431 * none of them is really recursive. But the idea is kept: there
432 * is one routine per non-terminal that eithers gobbles lexical tokens
433 * or calls lower-level routines, etc.
435 * The scanner is implemented in the routine get_token. Actually,
436 * get_token is more than a scanner and will return tokens that are
437 * in fact non-terminals in the grammar. So you can see the whole
438 * approach as the combination of a low-level bottom-up recognizer
439 * combined with a scanner and a number of top-down parsers. Or just
440 * consider that the real grammars recognized by the parsers are not
441 * those of the standards. As a matter of fact, our parsers are more
442 * liberal than the spec when there is no ambiguity.
444 * The difference is pretty academic (modulo bugs or incorrect
445 * interpretation of the specs).
448 #define TK_NOENDQUOTE -2
449 #define TK_OUTOFMEM -1
451 #define TK_UNEXPCHAR 1
452 #define TK_BAREWORD 2
453 #define TK_QDSTRING 3
454 #define TK_LEFTPAREN 4
455 #define TK_RIGHTPAREN 5
457 #define TK_QDESCR TK_QDSTRING
465 get_token(char ** sp, char ** token_val)
482 kind = TK_RIGHTPAREN;
493 while ( **sp != '\'' && **sp != '\0' )
495 if ( **sp == '\'' ) {
497 res = LDAP_MALLOC(q-p+1);
507 kind = TK_NOENDQUOTE;
513 while ( !isspace(**sp) && **sp != '\0' )
516 res = LDAP_MALLOC(q-p+1);
525 /* kind = TK_UNEXPCHAR; */
532 /* Gobble optional whitespace */
534 parse_whsp(char **sp)
536 while (isspace(**sp))
541 * General note for all parsers: to guarantee the algorithm halts they
542 * must always advance the pointer even when an error is found. For
543 * this one is not that important since an error here is fatal at the
544 * upper layers, but it is a simple strategy that will not get in
548 /* Parse a sequence of dot-separated decimal strings */
550 parse_numericoid(char **sp, int *code)
556 /* Each iteration of this loops gets one decimal string */
558 if ( !isdigit(**sp) ) {
560 * Initial char is not a digit or char after dot is
563 *code = LDAP_SCHERR_NODIGIT;
567 while ( isdigit(**sp) )
571 /* Otherwise, gobble the dot and loop again */
574 /* Now *sp points at the char past the numericoid. Perfect. */
576 res = LDAP_MALLOC(len+1);
578 *code = LDAP_SCHERR_OUTOFMEM;
581 strncpy(res,start,len);
586 /* Parse a qdescr or a list of them enclosed in () */
588 parse_qdescrs(char **sp, int *code)
598 kind = get_token(sp,&sval);
599 if ( kind == TK_LEFTPAREN ) {
600 /* Let's presume there will be at least 2 entries */
602 res = LDAP_CALLOC(3,sizeof(char *));
604 *code = LDAP_SCHERR_OUTOFMEM;
610 kind = get_token(sp,&sval);
611 if ( kind == TK_RIGHTPAREN )
613 if ( kind == TK_QDESCR ) {
614 if ( pos == size-2 ) {
616 res1 = LDAP_REALLOC(res,size*sizeof(char *));
619 *code = LDAP_SCHERR_OUTOFMEM;
629 *code = LDAP_SCHERR_UNEXPTOKEN;
636 } else if ( kind == TK_QDESCR ) {
637 res = LDAP_CALLOC(2,sizeof(char *));
639 *code = LDAP_SCHERR_OUTOFMEM;
647 *code = LDAP_SCHERR_BADNAME;
654 parse_woid(char **sp, int *code)
660 kind = get_token(sp, &sval);
661 if ( kind != TK_BAREWORD ) {
662 *code = LDAP_SCHERR_UNEXPTOKEN;
669 /* Parse a noidlen */
671 parse_noidlen(char **sp, int *code, int *len)
677 kind = get_token(sp, &sval);
678 if ( kind != TK_BAREWORD ) {
679 *code = LDAP_SCHERR_UNEXPTOKEN;
685 while ( isdigit(**sp) )
689 *code = LDAP_SCHERR_UNEXPTOKEN;
699 * Next routine will accept a qdstring in place of an oid. This is
700 * necessary to interoperate with Netscape Directory server that
701 * will improperly quote each oid (at least those of the descr kind)
705 /* Parse a woid or a $-separated list of them enclosed in () */
707 parse_oids(char **sp, int *code)
717 * Strictly speaking, doing this here accepts whsp before the
718 * ( at the begining of an oidlist, but his is harmless. Also,
719 * we are very liberal in what we accept as an OID. Maybe
723 kind = get_token(sp,&sval);
724 if ( kind == TK_LEFTPAREN ) {
725 /* Let's presume there will be at least 2 entries */
727 res = LDAP_CALLOC(3,sizeof(char *));
729 *code = LDAP_SCHERR_OUTOFMEM;
734 kind = get_token(sp,&sval);
735 if ( kind == TK_BAREWORD || kind == TK_QDSTRING ) {
739 *code = LDAP_SCHERR_UNEXPTOKEN;
745 kind = get_token(sp,&sval);
746 if ( kind == TK_RIGHTPAREN )
748 if ( kind == TK_DOLLAR ) {
750 kind = get_token(sp,&sval);
751 if ( kind == TK_BAREWORD ||
752 kind == TK_QDSTRING ) {
753 if ( pos == size-2 ) {
755 res1 = LDAP_REALLOC(res,size*sizeof(char *));
758 *code = LDAP_SCHERR_OUTOFMEM;
766 *code = LDAP_SCHERR_UNEXPTOKEN;
772 *code = LDAP_SCHERR_UNEXPTOKEN;
780 } else if ( kind == TK_BAREWORD || kind == TK_QDSTRING ) {
781 res = LDAP_CALLOC(2,sizeof(char *));
783 *code = LDAP_SCHERR_OUTOFMEM;
791 *code = LDAP_SCHERR_BADNAME;
797 free_at(LDAP_ATTRIBUTE_TYPE * at)
799 ldap_memfree(at->at_oid);
800 charray_free(at->at_names);
801 ldap_memfree(at->at_desc);
802 ldap_memfree(at->at_sup_oid);
803 ldap_memfree(at->at_equality_oid);
804 ldap_memfree(at->at_ordering_oid);
805 ldap_memfree(at->at_substr_oid);
806 ldap_memfree(at->at_syntax_oid);
810 LDAP_ATTRIBUTE_TYPE *
811 ldap_str2attributetype( char * s, int * code, char ** errp )
818 int seen_obsolete = 0;
820 int seen_equality = 0;
821 int seen_ordering = 0;
828 LDAP_ATTRIBUTE_TYPE * at;
831 *code = LDAP_SCHERR_EMPTY;
837 at = LDAP_CALLOC(1,sizeof(LDAP_ATTRIBUTE_TYPE));
840 *code = LDAP_SCHERR_OUTOFMEM;
844 kind = get_token(&ss,&sval);
845 if ( kind != TK_LEFTPAREN ) {
846 *code = LDAP_SCHERR_NOLEFTPAREN;
852 at->at_oid = parse_numericoid(&ss,code);
861 * Beyond this point we will be liberal and accept the items
865 kind = get_token(&ss,&sval);
868 *code = LDAP_SCHERR_NORIGHTPAREN;
875 if ( !strcmp(sval,"NAME") ) {
877 *code = LDAP_SCHERR_DUPOPT;
883 at->at_names = parse_qdescrs(&ss,code);
884 if ( !at->at_names ) {
885 if ( *code != LDAP_SCHERR_OUTOFMEM )
886 *code = LDAP_SCHERR_BADNAME;
891 } else if ( !strcmp(sval,"DESC") ) {
893 *code = LDAP_SCHERR_DUPOPT;
900 kind = get_token(&ss,&sval);
901 if ( kind != TK_QDSTRING ) {
902 *code = LDAP_SCHERR_UNEXPTOKEN;
909 } else if ( !strcmp(sval,"OBSOLETE") ) {
910 if ( seen_obsolete ) {
911 *code = LDAP_SCHERR_DUPOPT;
917 at->at_obsolete = LDAP_SCHEMA_YES;
919 } else if ( !strcmp(sval,"SUP") ) {
921 *code = LDAP_SCHERR_DUPOPT;
927 at->at_sup_oid = parse_woid(&ss,code);
928 if ( !at->at_sup_oid ) {
933 } else if ( !strcmp(sval,"EQUALITY") ) {
934 if ( seen_equality ) {
935 *code = LDAP_SCHERR_DUPOPT;
941 at->at_equality_oid = parse_woid(&ss,code);
942 if ( !at->at_equality_oid ) {
947 } else if ( !strcmp(sval,"ORDERING") ) {
948 if ( seen_ordering ) {
949 *code = LDAP_SCHERR_DUPOPT;
955 at->at_ordering_oid = parse_woid(&ss,code);
956 if ( !at->at_ordering_oid ) {
961 } else if ( !strcmp(sval,"SUBSTR") ) {
963 *code = LDAP_SCHERR_DUPOPT;
969 at->at_substr_oid = parse_woid(&ss,code);
970 if ( !at->at_substr_oid ) {
975 } else if ( !strcmp(sval,"SYNTAX") ) {
977 *code = LDAP_SCHERR_DUPOPT;
984 at->at_syntax_oid = parse_noidlen(&ss,code,&at->at_syntax_len);
985 if ( !at->at_syntax_oid ) {
991 } else if ( !strcmp(sval,"SINGLE-VALUE") ) {
992 if ( at->at_single_value ) {
993 *code = LDAP_SCHERR_DUPOPT;
998 at->at_single_value = LDAP_SCHEMA_YES;
1000 } else if ( !strcmp(sval,"COLLECTIVE") ) {
1001 if ( at->at_collective ) {
1002 *code = LDAP_SCHERR_DUPOPT;
1007 at->at_collective = LDAP_SCHEMA_YES;
1009 } else if ( !strcmp(sval,"NO-USER-MODIFICATION") ) {
1010 if ( at->at_no_user_mod ) {
1011 *code = LDAP_SCHERR_DUPOPT;
1016 at->at_no_user_mod = LDAP_SCHEMA_YES;
1018 } else if ( !strcmp(sval,"USAGE") ) {
1020 *code = LDAP_SCHERR_DUPOPT;
1027 kind = get_token(&ss,&sval);
1028 if ( kind != TK_BAREWORD ) {
1029 *code = LDAP_SCHERR_UNEXPTOKEN;
1034 if ( !strcasecmp(sval,"userApplications") )
1036 LDAP_SCHEMA_USER_APPLICATIONS;
1037 else if ( !strcasecmp(sval,"directoryOperation") )
1039 LDAP_SCHEMA_DIRECTORY_OPERATION;
1040 else if ( !strcasecmp(sval,"distributedOperation") )
1042 LDAP_SCHEMA_DISTRIBUTED_OPERATION;
1043 else if ( !strcasecmp(sval,"dSAOperation") )
1045 LDAP_SCHEMA_DSA_OPERATION;
1047 *code = LDAP_SCHERR_UNEXPTOKEN;
1054 *code = LDAP_SCHERR_UNEXPTOKEN;
1061 *code = LDAP_SCHERR_UNEXPTOKEN;
1070 free_oc(LDAP_OBJECT_CLASS * oc)
1072 ldap_memfree(oc->oc_oid);
1073 charray_free(oc->oc_names);
1074 ldap_memfree(oc->oc_desc);
1075 charray_free(oc->oc_sup_oids);
1076 charray_free(oc->oc_at_oids_must);
1077 charray_free(oc->oc_at_oids_may);
1082 ldap_str2objectclass( char * s, int * code, char ** errp )
1089 int seen_obsolete = 0;
1094 LDAP_OBJECT_CLASS * oc;
1097 *code = LDAP_SCHERR_EMPTY;
1103 oc = LDAP_CALLOC(1,sizeof(LDAP_OBJECT_CLASS));
1106 *code = LDAP_SCHERR_OUTOFMEM;
1110 kind = get_token(&ss,&sval);
1111 if ( kind != TK_LEFTPAREN ) {
1112 *code = LDAP_SCHERR_NOLEFTPAREN;
1118 oc->oc_oid = parse_numericoid(&ss,code);
1119 if ( !oc->oc_oid ) {
1127 * Beyond this point we will be liberal an accept the items
1131 kind = get_token(&ss,&sval);
1134 *code = LDAP_SCHERR_NORIGHTPAREN;
1141 if ( !strcmp(sval,"NAME") ) {
1143 *code = LDAP_SCHERR_DUPOPT;
1149 oc->oc_names = parse_qdescrs(&ss,code);
1150 if ( !oc->oc_names ) {
1151 if ( *code != LDAP_SCHERR_OUTOFMEM )
1152 *code = LDAP_SCHERR_BADNAME;
1157 } else if ( !strcmp(sval,"DESC") ) {
1159 *code = LDAP_SCHERR_DUPOPT;
1166 kind = get_token(&ss,&sval);
1167 if ( kind != TK_QDSTRING ) {
1168 *code = LDAP_SCHERR_UNEXPTOKEN;
1175 } else if ( !strcmp(sval,"OBSOLETE") ) {
1176 if ( seen_obsolete ) {
1177 *code = LDAP_SCHERR_DUPOPT;
1183 oc->oc_obsolete = LDAP_SCHEMA_YES;
1185 } else if ( !strcmp(sval,"SUP") ) {
1187 *code = LDAP_SCHERR_DUPOPT;
1193 oc->oc_sup_oids = parse_oids(&ss,code);
1194 if ( !oc->oc_sup_oids ) {
1199 } else if ( !strcmp(sval,"ABSTRACT") ) {
1201 *code = LDAP_SCHERR_DUPOPT;
1207 oc->oc_kind = LDAP_SCHEMA_ABSTRACT;
1209 } else if ( !strcmp(sval,"STRUCTURAL") ) {
1211 *code = LDAP_SCHERR_DUPOPT;
1217 oc->oc_kind = LDAP_SCHEMA_STRUCTURAL;
1219 } else if ( !strcmp(sval,"AUXILIARY") ) {
1221 *code = LDAP_SCHERR_DUPOPT;
1227 oc->oc_kind = LDAP_SCHEMA_AUXILIARY;
1229 } else if ( !strcmp(sval,"MUST") ) {
1231 *code = LDAP_SCHERR_DUPOPT;
1237 oc->oc_at_oids_must = parse_oids(&ss,code);
1238 if ( !oc->oc_at_oids_must ) {
1244 } else if ( !strcmp(sval,"MAY") ) {
1246 *code = LDAP_SCHERR_DUPOPT;
1252 oc->oc_at_oids_may = parse_oids(&ss,code);
1253 if ( !oc->oc_at_oids_may ) {
1260 *code = LDAP_SCHERR_UNEXPTOKEN;
1267 *code = LDAP_SCHERR_UNEXPTOKEN;
1275 static char *err2text[] = {
1279 "Missing opening parenthesis",
1280 "Missing closing parenthesis",
1286 "Unexpected end of data"
1290 ldap_scherr2str(int code)
1292 if ( code < 1 || code >= (sizeof(err2text)/sizeof(char *)) ) {
1293 return "Unknown error";
1295 return err2text[code];