]> git.sur5r.net Git - openldap/blob - servers/slapd/oc.c
First round of imports from HEAD
[openldap] / servers / slapd / oc.c
1 /* oc.c - object class routines */
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_pvt.h"
18
19 int is_object_subclass(
20         ObjectClass *sup,
21         ObjectClass *sub )
22 {
23         int i;
24
25         if( sub == NULL || sup == NULL ) return 0;
26
27 #if 1
28 #ifdef NEW_LOGGING
29         LDAP_LOG ( OPERATION, ARGS, 
30                 "is_object_subclass(%s,%s) %d\n",
31                 sup->soc_oid, sub->soc_oid, sup == sub );
32 #else
33         Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
34                 sup->soc_oid, sub->soc_oid, sup == sub );
35 #endif
36 #endif
37
38         if( sup == sub ) {
39                 return 1;
40         }
41
42         if( sub->soc_sups == NULL ) {
43                 return 0;
44         }
45
46         for( i=0; sub->soc_sups[i] != NULL; i++ ) {
47                 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
48                         return 1;
49                 }
50         }
51
52         return 0;
53 }
54
55 int is_entry_objectclass(
56         Entry*  e,
57         ObjectClass *oc,
58         int set_flags )
59 {
60         /*
61          * set_flags should only be true if oc is one of operational
62          * object classes which we support objectClass flags for
63          * (e.g., referral, alias, ...).  See <slap.h>.
64          */
65
66         Attribute *attr;
67         struct berval *bv;
68         AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
69
70         assert(!( e == NULL || oc == NULL ));
71
72         if( e == NULL || oc == NULL ) {
73                 return 0;
74         }
75
76         if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
77                 /* flags are set, use them */
78                 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
79         }
80
81         /*
82          * find objectClass attribute
83          */
84         attr = attr_find(e->e_attrs, objectClass);
85         if( attr == NULL ) {
86                 /* no objectClass attribute */
87 #ifdef NEW_LOGGING
88                 LDAP_LOG( OPERATION, ERR, 
89                         "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
90                         "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
91                         oc->soc_oclass.oc_oid, 0 );
92 #else
93                 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
94                         "no objectClass attribute\n",
95                         e->e_dn == NULL ? "" : e->e_dn,
96                         oc->soc_oclass.oc_oid, 0 );
97 #endif
98
99                 return 0;
100         }
101
102         for( bv=attr->a_vals; bv->bv_val; bv++ ) {
103                 ObjectClass *objectClass = oc_bvfind( bv );
104
105                 if ( !set_flags && objectClass == oc ) {
106                         return 1;
107                 }
108                 
109                 if ( objectClass != NULL ) {
110                         e->e_ocflags |= objectClass->soc_flags;
111                 }
112         }
113
114         /* mark flags as set */
115         e->e_ocflags |= SLAP_OC__END;
116
117         return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
118 }
119
120
121 struct oindexrec {
122         struct berval   oir_name;
123         ObjectClass     *oir_oc;
124 };
125
126 static Avlnode  *oc_index = NULL;
127 static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
128         = LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
129
130 static int
131 oc_index_cmp(
132         const void *v_oir1,
133         const void *v_oir2 )
134 {
135         const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
136         int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
137         if (i)
138                 return i;
139         return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
140 }
141
142 static int
143 oc_index_name_cmp(
144         const void *v_name,
145         const void *v_oir )
146 {
147         const struct berval    *name = v_name;
148         const struct oindexrec *oir  = v_oir;
149         int i = name->bv_len - oir->oir_name.bv_len;
150         if (i)
151                 return i;
152         return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
153 }
154
155 ObjectClass *
156 oc_find( const char *ocname )
157 {
158         struct berval bv;
159
160         bv.bv_val = (char *)ocname;
161         bv.bv_len = strlen( ocname );
162
163         return( oc_bvfind( &bv ) );
164 }
165
166 ObjectClass *
167 oc_bvfind( struct berval *ocname )
168 {
169         struct oindexrec        *oir;
170
171         oir = avl_find( oc_index, ocname, oc_index_name_cmp );
172
173         if ( oir != NULL ) {
174                 return( oir->oir_oc );
175         }
176
177         return( NULL );
178 }
179
180 static int
181 oc_create_required(
182     ObjectClass         *soc,
183     char                **attrs,
184         int                     *op,
185     const char          **err )
186 {
187         char            **attrs1;
188         AttributeType   *sat;
189         AttributeType   **satp;
190         int             i;
191
192         if ( attrs ) {
193                 attrs1 = attrs;
194                 while ( *attrs1 ) {
195                         sat = at_find(*attrs1);
196                         if ( !sat ) {
197                                 *err = *attrs1;
198                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
199                         }
200
201                         if( is_at_operational( sat )) (*op)++;
202
203                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
204                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
205                                         *err = *attrs1;
206                                         return SLAP_SCHERR_OUTOFMEM;
207                                 }
208                         }
209                         attrs1++;
210                 }
211                 /* Now delete duplicates from the allowed list */
212                 for ( satp = soc->soc_required; *satp; satp++ ) {
213                         i = at_find_in_list(*satp,soc->soc_allowed);
214                         if ( i >= 0 ) {
215                                 at_delete_from_list(i, &soc->soc_allowed);
216                         }
217                 }
218         }
219         return 0;
220 }
221
222 static int
223 oc_create_allowed(
224     ObjectClass         *soc,
225     char                **attrs,
226         int                     *op,
227     const char          **err )
228 {
229         char            **attrs1;
230         AttributeType   *sat;
231
232         if ( attrs ) {
233                 attrs1 = attrs;
234                 while ( *attrs1 ) {
235                         sat = at_find(*attrs1);
236                         if ( !sat ) {
237                                 *err = *attrs1;
238                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
239                         }
240
241                         if( is_at_operational( sat )) (*op)++;
242
243                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
244                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
245                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
246                                         *err = *attrs1;
247                                         return SLAP_SCHERR_OUTOFMEM;
248                                 }
249                         }
250                         attrs1++;
251                 }
252         }
253         return 0;
254 }
255
256 static int
257 oc_add_sups(
258     ObjectClass         *soc,
259     char                        **sups,
260         int                     *op,
261     const char          **err )
262 {
263         int             code;
264         ObjectClass     *soc1;
265         int             nsups;
266         char    **sups1;
267         int             add_sups = 0;
268
269         if ( sups ) {
270                 if ( !soc->soc_sups ) {
271                         /* We are at the first recursive level */
272                         add_sups = 1;
273                         nsups = 1;
274                         sups1 = sups;
275                         while ( *sups1 ) {
276                                 nsups++;
277                                 sups1++;
278                         }
279                         soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
280                                           sizeof(ObjectClass *));
281                 }
282
283                 nsups = 0;
284                 sups1 = sups;
285                 while ( *sups1 ) {
286                         soc1 = oc_find(*sups1);
287                         if ( !soc1 ) {
288                                 *err = *sups1;
289                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
290                         }
291
292                         /* check object class usage
293                          * abstract classes can only sup abstract classes 
294                          * structural classes can not sup auxiliary classes
295                          * auxiliary classes can not sup structural classes
296                          */
297                         if( soc->soc_kind != soc1->soc_kind
298                                 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
299                         {
300                                 *err = *sups1;
301                                 return SLAP_SCHERR_CLASS_BAD_SUP;
302                         }
303
304                         if( soc1->soc_obsolete && !soc->soc_obsolete ) {
305                                 *err = *sups1;
306                                 return SLAP_SCHERR_CLASS_BAD_SUP;
307                         }
308
309                         if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
310
311                         if ( add_sups ) {
312                                 soc->soc_sups[nsups] = soc1;
313                         }
314
315                         code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
316                         if ( code ) return code;
317
318                         code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
319                         if ( code ) return code;
320
321                         code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
322                         if ( code ) return code;
323
324                         nsups++;
325                         sups1++;
326                 }
327         }
328
329         return 0;
330 }
331
332 void
333 oc_destroy( void )
334 {
335         ObjectClass *o;
336
337         avl_free(oc_index, ldap_memfree);
338         while( !LDAP_SLIST_EMPTY(&oc_list) ) {
339                 o = LDAP_SLIST_FIRST(&oc_list);
340                 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
341
342                 if (o->soc_sups) ldap_memfree(o->soc_sups);
343                 if (o->soc_required) ldap_memfree(o->soc_required);
344                 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
345                 ldap_objectclass_free((LDAPObjectClass *)o);
346         }
347 }
348
349 static int
350 oc_insert(
351     ObjectClass         *soc,
352     const char          **err
353 )
354 {
355         struct oindexrec        *oir;
356         char                    **names;
357
358         LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
359
360         if ( soc->soc_oid ) {
361                 oir = (struct oindexrec *)
362                         ch_calloc( 1, sizeof(struct oindexrec) );
363                 oir->oir_name.bv_val = soc->soc_oid;
364                 oir->oir_name.bv_len = strlen( soc->soc_oid );
365                 oir->oir_oc = soc;
366
367                 assert( oir->oir_name.bv_val );
368                 assert( oir->oir_oc );
369
370                 if ( avl_insert( &oc_index, (caddr_t) oir,
371                                  oc_index_cmp, avl_dup_error ) )
372                 {
373                         *err = soc->soc_oid;
374                         ldap_memfree(oir);
375                         return SLAP_SCHERR_CLASS_DUP;
376                 }
377
378                 /* FIX: temporal consistency check */
379                 assert( oc_bvfind(&oir->oir_name) != NULL );
380         }
381
382         if ( (names = soc->soc_names) ) {
383                 while ( *names ) {
384                         oir = (struct oindexrec *)
385                                 ch_calloc( 1, sizeof(struct oindexrec) );
386                         oir->oir_name.bv_val = *names;
387                         oir->oir_name.bv_len = strlen( *names );
388                         oir->oir_oc = soc;
389
390                         assert( oir->oir_name.bv_val );
391                         assert( oir->oir_oc );
392
393                         if ( avl_insert( &oc_index, (caddr_t) oir,
394                                          oc_index_cmp, avl_dup_error ) )
395                         {
396                                 *err = *names;
397                                 ldap_memfree(oir);
398                                 return SLAP_SCHERR_CLASS_DUP;
399                         }
400
401                         /* FIX: temporal consistency check */
402                         assert( oc_bvfind(&oir->oir_name) != NULL );
403
404                         names++;
405                 }
406         }
407
408         return 0;
409 }
410
411 int
412 oc_add(
413     LDAPObjectClass     *oc,
414         int user,
415     const char          **err
416 )
417 {
418         ObjectClass     *soc;
419         int             code;
420         int             op = 0;
421
422         if ( oc->oc_names != NULL ) {
423                 int i;
424
425                 for( i=0; oc->oc_names[i]; i++ ) {
426                         if( !slap_valid_descr( oc->oc_names[i] ) ) {
427                                 return SLAP_SCHERR_BAD_DESCR;
428                         }
429                 }
430         }
431
432         if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
433                 /* Expand OID macros */
434                 char *oid = oidm_find( oc->oc_oid );
435                 if ( !oid ) {
436                         *err = oc->oc_oid;
437                         return SLAP_SCHERR_OIDM;
438                 }
439                 if ( oid != oc->oc_oid ) {
440                         ldap_memfree( oc->oc_oid );
441                         oc->oc_oid = oid;
442                 }
443         }
444
445         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
446         AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
447
448         if( oc->oc_names != NULL ) {
449                 soc->soc_cname.bv_val = soc->soc_names[0];
450         } else {
451                 soc->soc_cname.bv_val = soc->soc_oid;
452         }
453         soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
454
455         if( soc->soc_sup_oids == NULL &&
456                 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
457         {
458                 /* structural object classes implicitly inherit from 'top' */
459                 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
460                 code = oc_add_sups( soc, top_oids, &op, err );
461         } else {
462                 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
463         }
464
465         if ( code != 0 ) return code;
466
467         code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
468         if ( code != 0 ) return code;
469
470         code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
471         if ( code != 0 ) return code;
472
473         if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
474
475         code = oc_insert(soc,err);
476         return code;
477 }
478
479 int
480 oc_schema_info( Entry *e )
481 {
482         struct berval   vals[2];
483         ObjectClass     *oc;
484
485         AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
486
487         vals[1].bv_val = NULL;
488
489         LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
490                 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
491
492                 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
493                         return -1;
494                 }
495
496 #if 0
497                 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
498                (long) vals[0].bv_len, vals[0].bv_val, 0 );
499 #endif
500                 if( attr_merge( e, ad_objectClasses, vals ) )
501                         return -1;
502                 ldap_memfree( vals[0].bv_val );
503         }
504         return 0;
505 }