]> git.sur5r.net Git - openldap/blob - servers/slapd/schemaparse.c
Some definition reordering to satisfy dependencies.
[openldap] / servers / slapd / schemaparse.c
1 /* schemaparse.c - routines to parse config file objectclass definitions */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "ldap_schema.h"
12
13 static Avlnode          *object_classes = NULL;
14
15 int     global_schemacheck = 1; /* schemacheck on is default */
16
17 static void             oc_usage_old(void);
18 static void             oc_usage(void);
19
20 static char *err2text[] = {
21         "",
22         "Out of memory",
23         "Objectclass not found",
24         "Attribute type not found",
25         "Duplicate objectclass",
26         "Duplicate attributetype"
27 };
28
29 char *
30 scherr2str(int code)
31 {
32         if ( code < 1 || code >= (sizeof(err2text)/sizeof(char *)) ) {
33                 return "Unknown error";
34         } else {
35                 return err2text[code];
36         }
37 }
38
39 void
40 parse_oc_old(
41     Backend     *be,
42     char        *fname,
43     int         lineno,
44     int         argc,
45     char        **argv
46 )
47 {
48         int             i;
49         char            last;
50         LDAP_OBJECT_CLASS       *oc;
51         int             code;
52         char            *err;
53         char            **namep;
54
55         oc = (LDAP_OBJECT_CLASS *) ch_calloc( 1, sizeof(LDAP_OBJECT_CLASS) );
56         oc->oc_names = ch_calloc( 2, sizeof(char *) );
57         oc->oc_names[0] = ch_strdup( argv[1] );
58         oc->oc_names[1] = NULL;
59         for ( i = 2; i < argc; i++ ) {
60                 /* required attributes */
61                 if ( strcasecmp( argv[i], "requires" ) == 0 ) {
62                         do {
63                                 i++;
64                                 if ( i < argc ) {
65                                         char **s = str2charray( argv[i], "," );
66                                         last = argv[i][strlen( argv[i] ) - 1];
67                                         charray_merge( &oc->oc_at_oids_must, s );
68                                         charray_free( s );
69                                 }
70                         } while ( i < argc && last == ',' );
71
72                 /* optional attributes */
73                 } else if ( strcasecmp( argv[i], "allows" ) == 0 ) {
74                         do {
75                                 i++;
76                                 if ( i < argc ) {
77                                         char **s = str2charray( argv[i], "," );
78                                         last = argv[i][strlen( argv[i] ) - 1];
79                                         
80                                         charray_merge( &oc->oc_at_oids_may, s );
81                                         charray_free( s );
82                                 }
83                         } while ( i < argc && last == ',' );
84
85                 } else {
86                         fprintf( stderr,
87             "%s: line %d: expecting \"requires\" or \"allows\" got \"%s\"\n",
88                             fname, lineno, argv[i] );
89                         oc_usage_old();
90                 }
91         }
92
93         /*
94          * There was no requirement in the old schema that all attributes
95          * types were defined before use and they would just default to
96          * SYNTAX_CIS.  To support this, we need to make attribute types
97          * out of thin air.
98          */
99         if ( oc->oc_at_oids_must ) {
100                 namep = oc->oc_at_oids_must;
101                 while ( *namep ) {
102                         code = at_fake_if_needed( *namep );
103                         if ( code ) {
104                                 fprintf( stderr, "%s: line %d: %s %s\n",
105                                          fname, lineno, scherr2str(code), *namep);
106                                 exit( 1 );
107                         }
108                         namep++;
109                 }
110         }
111         if ( oc->oc_at_oids_may ) {
112                 namep = oc->oc_at_oids_may;
113                 while ( *namep ) {
114                         code = at_fake_if_needed( *namep );
115                         if ( code ) {
116                                 fprintf( stderr, "%s: line %d: %s %s\n",
117                                          fname, lineno, scherr2str(code), *namep);
118                                 exit( 1 );
119                         }
120                         namep++;
121                 }
122         }
123         
124         code = oc_add(oc,&err);
125         if ( code ) {
126                 fprintf( stderr, "%s: line %d: %s %s\n",
127                          fname, lineno, scherr2str(code), err);
128                 exit( 1 );
129         }
130         ldap_memfree(oc);
131 }
132
133 void
134 parse_oc(
135     char        *fname,
136     int         lineno,
137     char        *line
138 )
139 {
140         LDAP_OBJECT_CLASS *oc;
141         int             code;
142         char            *err;
143
144         oc = ldap_str2objectclass(line,&code,&err);
145         if ( !oc ) {
146                 fprintf( stderr, "%s: line %d: %s before %s\n",
147                          fname, lineno, ldap_scherr2str(code), err );
148                 oc_usage();
149         }
150         code = oc_add(oc,&err);
151         if ( code ) {
152                 fprintf( stderr, "%s: line %d: %s %s\n",
153                          fname, lineno, scherr2str(code), err);
154                 exit( 1 );
155         }
156         ldap_memfree(oc);
157 }
158
159 static void
160 oc_usage( void )
161 {
162         fprintf( stderr, "ObjectClassDescription = \"(\" whsp\n");
163         fprintf( stderr, "  numericoid whsp      ; ObjectClass identifier\n");
164         fprintf( stderr, "  [ \"NAME\" qdescrs ]\n");
165         fprintf( stderr, "  [ \"DESC\" qdstring ]\n");
166         fprintf( stderr, "  [ \"OBSOLETE\" whsp ]\n");
167         fprintf( stderr, "  [ \"SUP\" oids ]       ; Superior ObjectClasses\n");
168         fprintf( stderr, "  [ ( \"ABSTRACT\" / \"STRUCTURAL\" / \"AUXILIARY\" ) whsp ]\n");
169         fprintf( stderr, "                       ; default structural\n");
170         fprintf( stderr, "  [ \"MUST\" oids ]      ; AttributeTypes\n");
171         fprintf( stderr, "  [ \"MAY\" oids ]       ; AttributeTypes\n");
172         fprintf( stderr, "whsp \")\"\n");
173         exit( 1 );
174 }
175
176 static void
177 oc_usage_old( void )
178 {
179         fprintf( stderr, "<oc clause> ::= objectclass <ocname>\n" );
180         fprintf( stderr, "                [ requires <attrlist> ]\n" );
181         fprintf( stderr, "                [ allows <attrlist> ]\n" );
182         exit( 1 );
183 }
184
185 static void
186 at_usage( void )
187 {
188         fprintf( stderr, "AttributeTypeDescription = \"(\" whsp\n");
189         fprintf( stderr, "  numericoid whsp      ; AttributeType identifier\n");
190         fprintf( stderr, "  [ \"NAME\" qdescrs ]             ; name used in AttributeType\n");
191         fprintf( stderr, "  [ \"DESC\" qdstring ]            ; description\n");
192         fprintf( stderr, "  [ \"OBSOLETE\" whsp ]\n");
193         fprintf( stderr, "  [ \"SUP\" woid ]                 ; derived from this other\n");
194         fprintf( stderr, "                                 ; AttributeType\n");
195         fprintf( stderr, "  [ \"EQUALITY\" woid ]            ; Matching Rule name\n");
196         fprintf( stderr, "  [ \"ORDERING\" woid ]            ; Matching Rule name\n");
197         fprintf( stderr, "  [ \"SUBSTR\" woid ]              ; Matching Rule name\n");
198         fprintf( stderr, "  [ \"SYNTAX\" whsp noidlen whsp ] ; see section 4.3\n");
199         fprintf( stderr, "  [ \"SINGLE-VALUE\" whsp ]        ; default multi-valued\n");
200         fprintf( stderr, "  [ \"COLLECTIVE\" whsp ]          ; default not collective\n");
201         fprintf( stderr, "  [ \"NO-USER-MODIFICATION\" whsp ]; default user modifiable\n");
202         fprintf( stderr, "  [ \"USAGE\" whsp AttributeUsage ]; default userApplications\n");
203         fprintf( stderr, "                                 ; userApplications\n");
204         fprintf( stderr, "                                 ; directoryOperation\n");
205         fprintf( stderr, "                                 ; distributedOperation\n");
206         fprintf( stderr, "                                 ; dSAOperation\n");
207         fprintf( stderr, "whsp \")\"\n");
208         exit( 1 );
209 }
210
211 void
212 parse_at(
213     char        *fname,
214     int         lineno,
215     char        *line
216 )
217 {
218         LDAP_ATTRIBUTE_TYPE *at;
219         int             code;
220         char            *err;
221
222         at = ldap_str2attributetype(line,&code,&err);
223         if ( !at ) {
224                 fprintf( stderr, "%s: line %d: %s before %s\n",
225                          fname, lineno, ldap_scherr2str(code), err );
226                 at_usage();
227         }
228         code = at_add(at,&err);
229         if ( code ) {
230                 fprintf( stderr, "%s: line %d: %s %s\n",
231                          fname, lineno, scherr2str(code), err);
232                 exit( 1 );
233         }
234         ldap_memfree(at);
235 }