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