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