]> git.sur5r.net Git - openldap/blob - servers/slapd/schemaparse.c
Memory context tweaks for Bind
[openldap] / servers / slapd / schemaparse.c
1 /* schemaparse.c - routines to parse config file objectclass definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_schema.h"
18
19 int     global_schemacheck = 1; /* schemacheck ON is default */
20
21 static void             oc_usage(void); 
22 static void             at_usage(void);
23
24 static char *const err2text[] = {
25         "Success",
26         "Out of memory",
27         "ObjectClass not found",
28         "user-defined ObjectClass includes operational attributes",
29         "user-defined ObjectClass has inappropriate SUPerior",
30         "Duplicate objectClass",
31         "AttributeType not found",
32         "AttributeType inappropriate matching rule",
33         "AttributeType inappropriate USAGE",
34         "AttributeType inappropriate SUPerior",
35         "AttributeType SYNTAX or SUPerior required",
36         "Duplicate attributeType",
37         "MatchingRule not found",
38         "MatchingRule incomplete",
39         "Duplicate matchingRule",
40         "Syntax not found",
41         "Duplicate ldapSyntax",
42         "OID or name required",
43         "Qualifier not supported",
44         "Invalid NAME",
45         "OID could not be expanded",
46         "Duplicate Content Rule",
47         "Content Rule not for STRUCTURAL object class",
48         "Content Rule AUX contains non-AUXILIARY object class"
49         "Content Rule attribute type list contains duplicate"
50 };
51
52 char *
53 scherr2str(int code)
54 {
55         if ( code < 0 || SLAP_SCHERR_LAST <= code ) {
56                 return "Unknown error";
57         } else {
58                 return err2text[code];
59         }
60 }
61
62 /* check schema descr validity */
63 int slap_valid_descr( const char *descr )
64 {
65         int i=0;
66
67         if( !DESC_LEADCHAR( descr[i] ) ) {
68                 return 0;
69         }
70
71         while( descr[++i] ) {
72                 if( !DESC_CHAR( descr[i] ) ) {
73                         return 0;
74                 }
75         }
76
77         return 1;
78 }
79
80
81 /* OID Macros */
82
83 /* String compare with delimiter check. Return 0 if not
84  * matched, otherwise return length matched.
85  */
86 int
87 dscompare(const char *s1, const char *s2, char delim)
88 {
89         const char *orig = s1;
90         while (*s1++ == *s2++)
91                 if (!s1[-1]) break;
92         --s1;
93         --s2;
94         if (!*s1 && (!*s2 || *s2 == delim))
95                 return s1 - orig;
96         return 0;
97 }
98
99 #ifdef SLAP_EXTENDED_SCHEMA
100
101 static void
102 cr_usage( void )
103 {
104         fprintf( stderr,
105                 "DITContentRuleDescription = \"(\" whsp\n"
106                 "  numericoid whsp       ; StructuralObjectClass identifier\n"
107                 "  [ \"NAME\" qdescrs ]\n"
108                 "  [ \"DESC\" qdstring ]\n"
109                 "  [ \"OBSOLETE\" whsp ]\n"
110                 "  [ \"AUX\" oids ]      ; Auxiliary ObjectClasses\n"
111                 "  [ \"MUST\" oids ]     ; AttributeTypes\n"
112                 "  [ \"MAY\" oids ]      ; AttributeTypes\n"
113                 "  [ \"NOT\" oids ]      ; AttributeTypes\n"
114                 "  whsp \")\"\n" );
115 }
116
117 int
118 parse_cr(
119     const char  *fname,
120     int         lineno,
121     char        *line,
122     char        **argv
123 )
124 {
125         LDAPContentRule *cr;
126         int             code;
127         const char      *err;
128
129         cr = ldap_str2contentrule(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
130         if ( !cr ) {
131                 fprintf( stderr, "%s: line %d: %s before %s\n",
132                          fname, lineno, ldap_scherr2str(code), err );
133                 cr_usage();
134                 return 1;
135         }
136
137         if ( cr->cr_oid == NULL ) {
138                 fprintf( stderr,
139                         "%s: line %d: Content rule has no OID\n",
140                         fname, lineno );
141                 cr_usage();
142                 return 1;
143         }
144
145         code = cr_add(cr,1,&err);
146         if ( code ) {
147                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
148                          fname, lineno, scherr2str(code), err);
149                 return 1;
150         }
151
152         ldap_memfree(cr);
153         return 0;
154 }
155
156 #endif
157
158 int
159 parse_oc(
160     const char  *fname,
161     int         lineno,
162     char        *line,
163     char        **argv
164 )
165 {
166         LDAPObjectClass *oc;
167         int             code;
168         const char      *err;
169
170         oc = ldap_str2objectclass(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
171         if ( !oc ) {
172                 fprintf( stderr, "%s: line %d: %s before %s\n",
173                          fname, lineno, ldap_scherr2str(code), err );
174                 oc_usage();
175                 return 1;
176         }
177
178         if ( oc->oc_oid == NULL ) {
179                 fprintf( stderr,
180                         "%s: line %d: objectclass has no OID\n",
181                         fname, lineno );
182                 oc_usage();
183                 return 1;
184         }
185
186         code = oc_add(oc,1,&err);
187         if ( code ) {
188                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
189                          fname, lineno, scherr2str(code), err);
190                 return 1;
191         }
192
193         ldap_memfree(oc);
194         return 0;
195 }
196
197 static void
198 oc_usage( void )
199 {
200         fprintf( stderr,
201                 "ObjectClassDescription = \"(\" whsp\n"
202                 "  numericoid whsp                 ; ObjectClass identifier\n"
203                 "  [ \"NAME\" qdescrs ]\n"
204                 "  [ \"DESC\" qdstring ]\n"
205                 "  [ \"OBSOLETE\" whsp ]\n"
206                 "  [ \"SUP\" oids ]                ; Superior ObjectClasses\n"
207                 "  [ ( \"ABSTRACT\" / \"STRUCTURAL\" / \"AUXILIARY\" ) whsp ]\n"
208                 "                                  ; default structural\n"
209                 "  [ \"MUST\" oids ]               ; AttributeTypes\n"
210                 "  [ \"MAY\" oids ]                ; AttributeTypes\n"
211                 "  whsp \")\"\n" );
212 }
213
214 static void
215 at_usage( void )
216 {
217         fprintf( stderr,
218                 "AttributeTypeDescription = \"(\" whsp\n"
219                 "  numericoid whsp      ; AttributeType identifier\n"
220                 "  [ \"NAME\" qdescrs ]             ; name used in AttributeType\n"
221                 "  [ \"DESC\" qdstring ]            ; description\n"
222                 "  [ \"OBSOLETE\" whsp ]\n"
223                 "  [ \"SUP\" woid ]                 ; derived from this other\n"
224                 "                                   ; AttributeType\n"
225                 "  [ \"EQUALITY\" woid ]            ; Matching Rule name\n"
226                 "  [ \"ORDERING\" woid ]            ; Matching Rule name\n"
227                 "  [ \"SUBSTR\" woid ]              ; Matching Rule name\n"
228                 "  [ \"SYNTAX\" whsp noidlen whsp ] ; see section 4.3\n"
229                 "  [ \"SINGLE-VALUE\" whsp ]        ; default multi-valued\n"
230                 "  [ \"COLLECTIVE\" whsp ]          ; default not collective\n"
231                 "  [ \"NO-USER-MODIFICATION\" whsp ]; default user modifiable\n"
232                 "  [ \"USAGE\" whsp AttributeUsage ]; default userApplications\n"
233                 "                                   ; userApplications\n"
234                 "                                   ; directoryOperation\n"
235                 "                                   ; distributedOperation\n"
236                 "                                   ; dSAOperation\n"
237                 "  whsp \")\"\n");
238 }
239
240 int
241 parse_at(
242     const char  *fname,
243     int         lineno,
244     char        *line,
245     char        **argv
246 )
247 {
248         LDAPAttributeType *at;
249         int             code;
250         const char      *err;
251
252         at = ldap_str2attributetype( line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
253         if ( !at ) {
254                 fprintf( stderr, "%s: line %d: %s before %s\n",
255                          fname, lineno, ldap_scherr2str(code), err );
256                 at_usage();
257                 return 1;
258         }
259
260         if ( at->at_oid == NULL ) {
261                 fprintf( stderr,
262                         "%s: line %d: attributeType has no OID\n",
263                         fname, lineno );
264                 at_usage();
265                 return 1;
266         }
267
268         /* operational attributes should be defined internally */
269         if ( at->at_usage ) {
270                 fprintf( stderr, "%s: line %d: attribute type \"%s\" is operational\n",
271                          fname, lineno, at->at_oid );
272                 return 1;
273         }
274
275         code = at_add(at,&err);
276         if ( code ) {
277                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
278                          fname, lineno, scherr2str(code), err);
279                 return 1;
280         }
281         ldap_memfree(at);
282         return 0;
283 }