]> git.sur5r.net Git - openldap/blob - servers/slapd/oc.c
Update SASL code to reuse context through life of session.
[openldap] / servers / slapd / oc.c
1 /* oc.c - object class routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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_pvt.h"
18
19 int is_object_subclass(
20         ObjectClass *sub,
21         ObjectClass *sup )
22 {
23         int i;
24
25         if( sub == NULL || sup == NULL ) return 0;
26
27 #if 0
28         Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
29                 sub->soc_oid, sup->soc_oid, sup == sub );
30 #endif
31
32         if( sup == sub ) {
33                 return 1;
34         }
35
36         if( sup->soc_sups == NULL ) {
37                 return 0;
38         }
39
40         for( i=0; sup->soc_sups[i] != NULL; i++ ) {
41                 if( is_object_subclass( sub, sup->soc_sups[i] ) ) {
42                         return 1;
43                 }
44         }
45
46         return 0;
47 }
48
49 int is_entry_objectclass(
50         Entry*  e,
51         ObjectClass *oc )
52 {
53         Attribute *attr;
54         int i;
55         AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
56         assert(!( e == NULL || oc == NULL ));
57
58         if( e == NULL || oc == NULL ) {
59                 return 0;
60         }
61
62         /*
63          * find objectClass attribute
64          */
65         attr = attr_find(e->e_attrs, objectClass);
66
67         if( attr == NULL ) {
68                 /* no objectClass attribute */
69                 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
70                         "no objectClass attribute\n",
71                         e->e_dn == NULL ? "" : e->e_dn,
72                         oc->soc_oclass.oc_oid, 0 );
73
74                 return 0;
75         }
76
77         for( i=0; attr->a_vals[i]; i++ ) {
78                 ObjectClass *objectClass = oc_find( attr->a_vals[i]->bv_val );
79
80                 if( objectClass == oc ) {
81                         return 1;
82                 }
83         }
84
85         return 0;
86
87 }
88
89
90 struct oindexrec {
91         char            *oir_name;
92         ObjectClass     *oir_oc;
93 };
94
95 static Avlnode  *oc_index = NULL;
96 static ObjectClass *oc_list = NULL;
97
98 static int
99 oc_index_cmp(
100     struct oindexrec    *oir1,
101     struct oindexrec    *oir2 )
102 {
103         assert( oir1->oir_name );
104         assert( oir1->oir_oc );
105         assert( oir2->oir_name );
106         assert( oir2->oir_oc );
107
108         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
109 }
110
111 static int
112 oc_index_name_cmp(
113     char                *name,
114     struct oindexrec    *oir )
115 {
116         assert( oir->oir_name );
117         assert( oir->oir_oc );
118
119         return (strcasecmp( name, oir->oir_name ));
120 }
121
122 ObjectClass *
123 oc_find( const char *ocname )
124 {
125         struct oindexrec        *oir;
126
127         oir = (struct oindexrec *) avl_find( oc_index, ocname,
128             (AVL_CMP) oc_index_name_cmp );
129
130         if ( oir != NULL ) {
131                 assert( oir->oir_name );
132                 assert( oir->oir_oc );
133
134                 return( oir->oir_oc );
135         }
136
137         return( NULL );
138 }
139
140 static int
141 oc_create_required(
142     ObjectClass         *soc,
143     char                **attrs,
144     const char          **err )
145 {
146         char            **attrs1;
147         AttributeType   *sat;
148         AttributeType   **satp;
149         int             i;
150
151         if ( attrs ) {
152                 attrs1 = attrs;
153                 while ( *attrs1 ) {
154                         sat = at_find(*attrs1);
155                         if ( !sat ) {
156                                 *err = *attrs1;
157                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
158                         }
159                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
160                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
161                                         *err = *attrs1;
162                                         return SLAP_SCHERR_OUTOFMEM;
163                                 }
164                         }
165                         attrs1++;
166                 }
167                 /* Now delete duplicates from the allowed list */
168                 for ( satp = soc->soc_required; *satp; satp++ ) {
169                         i = at_find_in_list(*satp,soc->soc_allowed);
170                         if ( i >= 0 ) {
171                                 at_delete_from_list(i, &soc->soc_allowed);
172                         }
173                 }
174         }
175         return 0;
176 }
177
178 static int
179 oc_create_allowed(
180     ObjectClass         *soc,
181     char                **attrs,
182     const char          **err )
183 {
184         char            **attrs1;
185         AttributeType   *sat;
186
187         if ( attrs ) {
188                 attrs1 = attrs;
189                 while ( *attrs1 ) {
190                         sat = at_find(*attrs1);
191                         if ( !sat ) {
192                                 *err = *attrs1;
193                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
194                         }
195                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
196                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
197                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
198                                         *err = *attrs1;
199                                         return SLAP_SCHERR_OUTOFMEM;
200                                 }
201                         }
202                         attrs1++;
203                 }
204         }
205         return 0;
206 }
207
208 static int
209 oc_add_sups(
210     ObjectClass         *soc,
211     char                        **sups,
212     const char          **err )
213 {
214         int             code;
215         ObjectClass     *soc1;
216         int             nsups;
217         char            **sups1;
218         int             add_sups = 0;
219
220         if ( sups ) {
221                 if ( !soc->soc_sups ) {
222                         /* We are at the first recursive level */
223                         add_sups = 1;
224                         nsups = 0;
225                         sups1 = sups;
226                         while ( *sups1 ) {
227                                 nsups++;
228                                 sups1++;
229                         }
230                         nsups++;
231                         soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
232                                           sizeof(ObjectClass *));
233                 }
234                 nsups = 0;
235                 sups1 = sups;
236                 while ( *sups1 ) {
237                         soc1 = oc_find(*sups1);
238                         if ( !soc1 ) {
239                                 *err = *sups1;
240                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
241                         }
242
243                         if ( add_sups )
244                                 soc->soc_sups[nsups] = soc1;
245
246                         code = oc_add_sups( soc, soc1->soc_sup_oids, err );
247                         if ( code ) return code;
248
249                         code = oc_create_required( soc, soc1->soc_at_oids_must, err );
250                         if ( code ) return code;
251
252                         code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
253                         if ( code ) return code;
254
255                         nsups++;
256                         sups1++;
257                 }
258         }
259         return 0;
260 }
261
262 static int
263 oc_insert(
264     ObjectClass         *soc,
265     const char          **err
266 )
267 {
268         ObjectClass     **ocp;
269         struct oindexrec        *oir;
270         char                    **names;
271
272         ocp = &oc_list;
273         while ( *ocp != NULL ) {
274                 ocp = &(*ocp)->soc_next;
275         }
276         *ocp = soc;
277
278         if ( soc->soc_oid ) {
279                 oir = (struct oindexrec *)
280                         ch_calloc( 1, sizeof(struct oindexrec) );
281                 oir->oir_name = soc->soc_oid;
282                 oir->oir_oc = soc;
283
284                 assert( oir->oir_name );
285                 assert( oir->oir_oc );
286
287                 if ( avl_insert( &oc_index, (caddr_t) oir,
288                                  (AVL_CMP) oc_index_cmp,
289                                  (AVL_DUP) avl_dup_error ) )
290                 {
291                         *err = soc->soc_oid;
292                         ldap_memfree(oir->oir_name);
293                         ldap_memfree(oir);
294                         return SLAP_SCHERR_DUP_CLASS;
295                 }
296
297                 /* FIX: temporal consistency check */
298                 assert( oc_find(oir->oir_name) != NULL );
299         }
300
301         if ( (names = soc->soc_names) ) {
302                 while ( *names ) {
303                         oir = (struct oindexrec *)
304                                 ch_calloc( 1, sizeof(struct oindexrec) );
305                         oir->oir_name = ch_strdup(*names);
306                         oir->oir_oc = soc;
307
308                         assert( oir->oir_name );
309                         assert( oir->oir_oc );
310
311                         if ( avl_insert( &oc_index, (caddr_t) oir,
312                                          (AVL_CMP) oc_index_cmp,
313                                          (AVL_DUP) avl_dup_error ) )
314                         {
315                                 *err = *names;
316                                 ldap_memfree(oir->oir_name);
317                                 ldap_memfree(oir);
318                                 return SLAP_SCHERR_DUP_CLASS;
319                         }
320
321                         /* FIX: temporal consistency check */
322                         assert( oc_find(oir->oir_name) != NULL );
323
324                         names++;
325                 }
326         }
327
328         return 0;
329 }
330
331 int
332 oc_add(
333     LDAP_OBJECT_CLASS   *oc,
334     const char          **err
335 )
336 {
337         ObjectClass     *soc;
338         int             code;
339
340         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
341         memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS) );
342
343         if( soc->soc_sup_oids == NULL &&
344                 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
345         {
346                 /* structural object classes implicitly inherit from 'top' */
347                 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
348                 code = oc_add_sups( soc, top_oids, err );
349         } else {
350                 code = oc_add_sups( soc, soc->soc_sup_oids, err );
351         }
352         if ( code != 0 ) return code;
353
354         code = oc_create_required( soc, soc->soc_at_oids_must, err );
355         if ( code != 0 ) return code;
356
357         code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
358         if ( code != 0 ) return code;
359
360         code = oc_insert(soc,err);
361         return code;
362 }
363
364 #ifdef LDAP_DEBUG
365
366 static void
367 oc_print( ObjectClass *oc )
368 {
369         int     i;
370         const char *mid;
371
372         printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
373         if ( oc->soc_required != NULL ) {
374                 mid = "\trequires ";
375                 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
376                         printf( "%s%s", mid,
377                                 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
378                 printf( "\n" );
379         }
380         if ( oc->soc_allowed != NULL ) {
381                 mid = "\tallows ";
382                 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
383                         printf( "%s%s", mid,
384                                 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
385                 printf( "\n" );
386         }
387 }
388
389 #endif
390
391
392 #if defined( SLAPD_SCHEMA_DN )
393
394 int
395 oc_schema_info( Entry *e )
396 {
397         struct berval   val;
398         struct berval   *vals[2];
399         ObjectClass     *oc;
400
401         AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
402
403         vals[0] = &val;
404         vals[1] = NULL;
405
406         for ( oc = oc_list; oc; oc = oc->soc_next ) {
407                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
408                 if ( val.bv_val == NULL ) {
409                         return -1;
410                 }
411                 val.bv_len = strlen( val.bv_val );
412 #if 0
413                 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
414                (long) val.bv_len, val.bv_val, 0 );
415 #endif
416                 attr_merge( e, ad_objectClasses, vals );
417                 ldap_memfree( val.bv_val );
418         }
419         return 0;
420 }
421
422 #endif