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