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