]> git.sur5r.net Git - openldap/blob - servers/slapd/schemaparse.c
More system schema checks
[openldap] / servers / slapd / schemaparse.c
1 /* schemaparse.c - routines to parse config file objectclass definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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[SLAP_SCHERR_LAST+1] = {
25         "Success",
26         "Out of memory",
27         "ObjectClass not found",
28         "ObjectClass inappropriate SUPerior",
29         "AttributeType not found",
30         "AttributeType inappropriate USAGE",
31         "Duplicate objectClass",
32         "Duplicate attributeType",
33         "Duplicate ldapSyntax",
34         "Duplicate matchingRule",
35         "OID or name required",
36         "SYNTAX or SUPerior required",
37         "MatchingRule not found",
38         "Syntax not found",
39         "Syntax required",
40         "Qualifier not supported",
41         "Invalid NAME",
42         "OID could not be expanded"
43 };
44
45 char *
46 scherr2str(int code)
47 {
48         if ( code < 0 || code >= (sizeof(err2text)/sizeof(char *)) ) {
49                 return "Unknown error";
50         } else {
51                 return err2text[code];
52         }
53 }
54
55 /* check schema descr validity */
56 int slap_valid_descr( const char *descr )
57 {
58         int i=0;
59
60         if( !DESC_LEADCHAR( descr[i] ) ) {
61                 return 0;
62         }
63
64         while( descr[++i] ) {
65                 if( !DESC_CHAR( descr[i] ) ) {
66                         return 0;
67                 }
68         }
69
70         return 1;
71 }
72
73
74 /* OID Macros */
75
76 /* String compare with delimiter check. Return 0 if not
77  * matched, otherwise return length matched.
78  */
79 int
80 dscompare(const char *s1, const char *s2, char delim)
81 {
82         const char *orig = s1;
83         while (*s1++ == *s2++)
84                 if (!s1[-1]) break;
85         --s1;
86         --s2;
87         if (!*s1 && (!*s2 || *s2 == delim))
88                 return s1 - orig;
89         return 0;
90 }
91
92
93 int
94 parse_oc(
95     const char  *fname,
96     int         lineno,
97     char        *line,
98     char        **argv
99 )
100 {
101         LDAPObjectClass *oc;
102         int             code;
103         const char      *err;
104
105         oc = ldap_str2objectclass(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
106         if ( !oc ) {
107                 fprintf( stderr, "%s: line %d: %s before %s\n",
108                          fname, lineno, ldap_scherr2str(code), err );
109                 oc_usage();
110                 return 1;
111         }
112
113         if ( oc->oc_oid == NULL ) {
114                 fprintf( stderr,
115                         "%s: line %d: objectclass has no OID\n",
116                         fname, lineno );
117                 oc_usage();
118                 return 1;
119         }
120
121         code = oc_add(oc,&err);
122         if ( code ) {
123                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
124                          fname, lineno, scherr2str(code), err);
125                 return 1;
126         }
127
128         ldap_memfree(oc);
129         return 0;
130 }
131
132 static void
133 oc_usage( void )
134 {
135         fprintf( stderr,
136                 "ObjectClassDescription = \"(\" whsp\n"
137                 "  numericoid whsp                 ; ObjectClass identifier\n"
138                 "  [ \"NAME\" qdescrs ]\n"
139                 "  [ \"DESC\" qdstring ]\n"
140                 "  [ \"OBSOLETE\" whsp ]\n"
141                 "  [ \"SUP\" oids ]                ; Superior ObjectClasses\n"
142                 "  [ ( \"ABSTRACT\" / \"STRUCTURAL\" / \"AUXILIARY\" ) whsp ]\n"
143                 "                                  ; default structural\n"
144                 "  [ \"MUST\" oids ]               ; AttributeTypes\n"
145                 "  [ \"MAY\" oids ]                ; AttributeTypes\n"
146                 "  whsp \")\"\n" );
147 }
148
149
150 static void
151 at_usage( void )
152 {
153         fprintf( stderr,
154                 "AttributeTypeDescription = \"(\" whsp\n"
155                 "  numericoid whsp      ; AttributeType identifier\n"
156                 "  [ \"NAME\" qdescrs ]             ; name used in AttributeType\n"
157                 "  [ \"DESC\" qdstring ]            ; description\n"
158                 "  [ \"OBSOLETE\" whsp ]\n"
159                 "  [ \"SUP\" woid ]                 ; derived from this other\n"
160                 "                                   ; AttributeType\n"
161                 "  [ \"EQUALITY\" woid ]            ; Matching Rule name\n"
162                 "  [ \"ORDERING\" woid ]            ; Matching Rule name\n"
163                 "  [ \"SUBSTR\" woid ]              ; Matching Rule name\n"
164                 "  [ \"SYNTAX\" whsp noidlen whsp ] ; see section 4.3\n"
165                 "  [ \"SINGLE-VALUE\" whsp ]        ; default multi-valued\n"
166                 "  [ \"COLLECTIVE\" whsp ]          ; default not collective\n"
167                 "  [ \"NO-USER-MODIFICATION\" whsp ]; default user modifiable\n"
168                 "  [ \"USAGE\" whsp AttributeUsage ]; default userApplications\n"
169                 "                                   ; userApplications\n"
170                 "                                   ; directoryOperation\n"
171                 "                                   ; distributedOperation\n"
172                 "                                   ; dSAOperation\n"
173                 "  whsp \")\"\n");
174 }
175
176 int
177 parse_at(
178     const char  *fname,
179     int         lineno,
180     char        *line,
181     char        **argv
182 )
183 {
184         LDAPAttributeType *at;
185         int             code;
186         const char      *err;
187         char            *oid = NULL;
188
189         at = ldap_str2attributetype( line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
190         if ( !at ) {
191                 fprintf( stderr, "%s: line %d: %s before %s\n",
192                          fname, lineno, ldap_scherr2str(code), err );
193                 at_usage();
194                 return 1;
195         }
196
197         if ( at->at_oid == NULL ) {
198                 fprintf( stderr,
199                         "%s: line %d: attributeType has no OID\n",
200                         fname, lineno );
201                 at_usage();
202                 return 1;
203         }
204
205         /* operational attributes should be defined internally */
206         if ( at->at_usage ) {
207                 fprintf( stderr, "%s: line %d: attribute type \"%s\" is operational\n",
208                          fname, lineno, at->at_oid );
209                 return 1;
210         }
211
212         code = at_add(at,&err);
213         if ( code ) {
214                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
215                          fname, lineno, scherr2str(code), err);
216                 return 1;
217         }
218         ldap_memfree(at);
219         return 0;
220 }