]> git.sur5r.net Git - openldap/blob - servers/slapd/schemaparse.c
Suck in HEAD changes since 2.1alpha
[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 >= (int)(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
188         at = ldap_str2attributetype( line, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
189         if ( !at ) {
190                 fprintf( stderr, "%s: line %d: %s before %s\n",
191                          fname, lineno, ldap_scherr2str(code), err );
192                 at_usage();
193                 return 1;
194         }
195
196         if ( at->at_oid == NULL ) {
197                 fprintf( stderr,
198                         "%s: line %d: attributeType has no OID\n",
199                         fname, lineno );
200                 at_usage();
201                 return 1;
202         }
203
204         /* operational attributes should be defined internally */
205         if ( at->at_usage ) {
206                 fprintf( stderr, "%s: line %d: attribute type \"%s\" is operational\n",
207                          fname, lineno, at->at_oid );
208                 return 1;
209         }
210
211         code = at_add(at,&err);
212         if ( code ) {
213                 fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
214                          fname, lineno, scherr2str(code), err);
215                 return 1;
216         }
217         ldap_memfree(at);
218         return 0;
219 }