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