]> git.sur5r.net Git - openldap/blob - servers/slapd/oc.c
7fa10b01771c3292a60c56909aab9f558d44fcb6
[openldap] / servers / slapd / oc.c
1 /* oc.c - object class routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 int is_object_subclass(
28         ObjectClass *sup,
29         ObjectClass *sub )
30 {
31         int i;
32
33         if( sub == NULL || sup == NULL ) return 0;
34
35 #if 0
36         Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
37                 sup->soc_oid, sub->soc_oid, sup == sub );
38 #endif
39
40         if ( sup == sub ) {
41                 return 1;
42         }
43
44         if ( sub->soc_sups == NULL ) {
45                 return 0;
46         }
47
48         for ( i = 0; sub->soc_sups[i] != NULL; i++ ) {
49                 if ( is_object_subclass( sup, sub->soc_sups[i] ) ) {
50                         return 1;
51                 }
52         }
53
54         return 0;
55 }
56
57 int is_entry_objectclass(
58         Entry*  e,
59         ObjectClass *oc,
60         unsigned flags )
61 {
62         /*
63          * set_flags should only be true if oc is one of operational
64          * object classes which we support objectClass flags for
65          * (e.g., referral, alias, ...).  See <slap.h>.
66          */
67
68         Attribute *attr;
69         struct berval *bv;
70
71         assert( !( e == NULL || oc == NULL ) );
72         assert( ( flags & SLAP_OCF_MASK ) != SLAP_OCF_MASK );
73
74         if ( e == NULL || oc == NULL ) {
75                 return 0;
76         }
77
78         if ( flags == SLAP_OCF_SET_FLAGS && ( e->e_ocflags & SLAP_OC__END ) )
79         {
80                 /* flags are set, use them */
81                 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
82         }
83
84         /*
85          * find objectClass attribute
86          */
87         attr = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
88         if ( attr == NULL ) {
89                 /* no objectClass attribute */
90                 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
91                         "no objectClass attribute\n",
92                         e->e_dn == NULL ? "" : e->e_dn,
93                         oc->soc_oclass.oc_oid, 0 );
94
95                 return 0;
96         }
97
98         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
99                 ObjectClass *objectClass = oc_bvfind( bv );
100
101                 if ( objectClass == NULL ) {
102                         /* FIXME: is this acceptable? */
103                         continue;
104                 }
105
106                 if ( !( flags & SLAP_OCF_SET_FLAGS ) ) {
107                         if ( objectClass == oc ) {
108                                 return 1;
109                         }
110
111                         if ( ( flags & SLAP_OCF_CHECK_SUP )
112                                 && is_object_subclass( oc, objectClass ) )
113                         {
114                                 return 1;
115                         }
116                 }
117                 
118                 e->e_ocflags |= objectClass->soc_flags;
119         }
120
121         /* mark flags as set */
122         e->e_ocflags |= SLAP_OC__END;
123
124         return ( e->e_ocflags & oc->soc_flags & SLAP_OC__MASK ) != 0;
125 }
126
127
128 struct oindexrec {
129         struct berval oir_name;
130         ObjectClass     *oir_oc;
131 };
132
133 static Avlnode  *oc_index = NULL;
134 static Avlnode  *oc_cache = NULL;
135 static LDAP_STAILQ_HEAD(OCList, ObjectClass) oc_list
136         = LDAP_STAILQ_HEAD_INITIALIZER(oc_list);
137
138 ObjectClass *oc_sys_tail;
139
140 static int
141 oc_index_cmp(
142         const void *v_oir1,
143         const void *v_oir2 )
144 {
145         const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
146         int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
147         if (i) return i;
148         return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
149 }
150
151 static int
152 oc_index_name_cmp(
153         const void *v_name,
154         const void *v_oir )
155 {
156         const struct berval    *name = v_name;
157         const struct oindexrec *oir  = v_oir;
158         int i = name->bv_len - oir->oir_name.bv_len;
159         if (i) return i;
160         return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
161 }
162
163 ObjectClass *
164 oc_find( const char *ocname )
165 {
166         struct berval bv;
167
168         bv.bv_val = (char *)ocname;
169         bv.bv_len = strlen( ocname );
170
171         return( oc_bvfind( &bv ) );
172 }
173
174 ObjectClass *
175 oc_bvfind( struct berval *ocname )
176 {
177         struct oindexrec        *oir;
178
179         if ( oc_cache ) {
180                 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
181                 if ( oir ) return oir->oir_oc;
182         }
183         oir = avl_find( oc_index, ocname, oc_index_name_cmp );
184
185         if ( oir != NULL ) {
186                 if ( at_oc_cache ) {
187                         avl_insert( &oc_cache, (caddr_t) oir,
188                                 oc_index_cmp, avl_dup_error );
189                 }
190                 return( oir->oir_oc );
191         }
192
193         return( NULL );
194 }
195
196 static LDAP_STAILQ_HEAD(OCUList, ObjectClass) oc_undef_list
197         = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
198
199 ObjectClass *
200 oc_bvfind_undef( struct berval *ocname )
201 {
202         ObjectClass     *oc = oc_bvfind( ocname );
203
204         if ( oc ) {
205                 return oc;
206         }
207
208         LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
209                 int     d = oc->soc_cname.bv_len - ocname->bv_len;
210
211                 if ( d ) {
212                         continue;
213                 }
214
215                 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
216                         break;
217                 }
218         }
219         
220         if ( oc ) {
221                 return oc;
222         }
223         
224         oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
225         memset( oc, 0, sizeof( ObjectClass ) );
226
227         oc->soc_cname.bv_len = ocname->bv_len;
228         oc->soc_cname.bv_val = (char *)&oc[ 1 ];
229         AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
230
231         /* canonical to upper case */
232         ldap_pvt_str2upper( oc->soc_cname.bv_val );
233
234         LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
235         ldap_pvt_thread_mutex_lock( &oc_undef_mutex );
236         LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
237         ldap_pvt_thread_mutex_unlock( &oc_undef_mutex );
238
239         return oc;
240 }
241
242 static int
243 oc_create_required(
244         ObjectClass             *soc,
245         char                    **attrs,
246         int                     *op,
247         const char              **err )
248 {
249         char            **attrs1;
250         AttributeType   *sat;
251         AttributeType   **satp;
252         int             i;
253
254         if ( attrs ) {
255                 attrs1 = attrs;
256                 while ( *attrs1 ) {
257                         sat = at_find(*attrs1);
258                         if ( !sat ) {
259                                 *err = *attrs1;
260                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
261                         }
262
263                         if( is_at_operational( sat )) (*op)++;
264
265                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
266                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
267                                         *err = *attrs1;
268                                         return SLAP_SCHERR_OUTOFMEM;
269                                 }
270                         }
271                         attrs1++;
272                 }
273                 /* Now delete duplicates from the allowed list */
274                 for ( satp = soc->soc_required; *satp; satp++ ) {
275                         i = at_find_in_list(*satp, soc->soc_allowed);
276                         if ( i >= 0 ) {
277                                 at_delete_from_list(i, &soc->soc_allowed);
278                         }
279                 }
280         }
281         return 0;
282 }
283
284 static int
285 oc_create_allowed(
286     ObjectClass         *soc,
287     char                **attrs,
288         int                     *op,
289     const char          **err )
290 {
291         char            **attrs1;
292         AttributeType   *sat;
293
294         if ( attrs ) {
295                 attrs1 = attrs;
296                 while ( *attrs1 ) {
297                         sat = at_find(*attrs1);
298                         if ( !sat ) {
299                                 *err = *attrs1;
300                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
301                         }
302
303                         if( is_at_operational( sat )) (*op)++;
304
305                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
306                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
307                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
308                                         *err = *attrs1;
309                                         return SLAP_SCHERR_OUTOFMEM;
310                                 }
311                         }
312                         attrs1++;
313                 }
314         }
315         return 0;
316 }
317
318 static int
319 oc_add_sups(
320         ObjectClass             *soc,
321         char                    **sups,
322         int                     *op,
323         const char              **err )
324 {
325         int             code;
326         ObjectClass     *soc1;
327         int             nsups;
328         char    **sups1;
329         int             add_sups = 0;
330
331         if ( sups ) {
332                 if ( !soc->soc_sups ) {
333                         /* We are at the first recursive level */
334                         add_sups = 1;
335                         nsups = 1;
336                         sups1 = sups;
337                         while ( *sups1 ) {
338                                 nsups++;
339                                 sups1++;
340                         }
341                         soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
342                                           sizeof(ObjectClass *));
343                 }
344
345                 nsups = 0;
346                 sups1 = sups;
347                 while ( *sups1 ) {
348                         soc1 = oc_find(*sups1);
349                         if ( !soc1 ) {
350                                 *err = *sups1;
351                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
352                         }
353
354                         /* check object class usage
355                          * abstract classes can only sup abstract classes 
356                          * structural classes can not sup auxiliary classes
357                          * auxiliary classes can not sup structural classes
358                          */
359                         if( soc->soc_kind != soc1->soc_kind
360                                 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
361                         {
362                                 *err = *sups1;
363                                 return SLAP_SCHERR_CLASS_BAD_SUP;
364                         }
365
366                         if( soc1->soc_obsolete && !soc->soc_obsolete ) {
367                                 *err = *sups1;
368                                 return SLAP_SCHERR_CLASS_BAD_SUP;
369                         }
370
371                         if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
372
373                         if ( add_sups ) {
374                                 soc->soc_sups[nsups] = soc1;
375                         }
376
377                         code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
378                         if ( code ) return code;
379
380                         code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
381                         if ( code ) return code;
382
383                         code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
384                         if ( code ) return code;
385
386                         nsups++;
387                         sups1++;
388                 }
389         }
390
391         return 0;
392 }
393
394 static void
395 oc_delete_names( ObjectClass *oc )
396 {
397         char                    **names = oc->soc_names;
398
399         while (*names) {
400                 struct oindexrec        tmpoir, *oir;
401
402                 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
403                 tmpoir.oir_oc = oc;
404                 oir = (struct oindexrec *)avl_delete( &oc_index,
405                         (caddr_t)&tmpoir, oc_index_cmp );
406                 assert( oir != NULL );
407                 ldap_memfree( oir );
408                 names++;
409         }
410 }
411
412 /* Mark the ObjectClass as deleted, remove from list, and remove all its
413  * names from the AVL tree. Leave the OID in the tree.
414  */
415 void
416 oc_delete( ObjectClass *oc )
417 {
418         oc->soc_flags |= SLAP_OC_DELETED;
419
420         LDAP_STAILQ_REMOVE(&oc_list, oc, ObjectClass, soc_next);
421
422         oc_delete_names( oc );
423 }
424
425 static void
426 oc_clean( ObjectClass *o )
427 {
428         if (o->soc_sups) {
429                 ldap_memfree(o->soc_sups);
430                 o->soc_sups = NULL;
431         }
432         if (o->soc_required) {
433                 ldap_memfree(o->soc_required);
434                 o->soc_required = NULL;
435         }
436         if (o->soc_allowed) {
437                 ldap_memfree(o->soc_allowed);
438                 o->soc_allowed = NULL;
439         }
440         if (o->soc_oidmacro) {
441                 ldap_memfree(o->soc_oidmacro);
442                 o->soc_oidmacro = NULL;
443         }
444 }
445
446 static void
447 oc_destroy_one( void *v )
448 {
449         struct oindexrec *oir = v;
450         ObjectClass *o = oir->oir_oc;
451
452         oc_clean( o );
453         ldap_objectclass_free((LDAPObjectClass *)o);
454         ldap_memfree(oir);
455 }
456
457 void
458 oc_destroy( void )
459 {
460         ObjectClass *o;
461
462         while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
463                 o = LDAP_STAILQ_FIRST(&oc_list);
464                 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
465
466                 oc_delete_names( o );
467         }
468         
469         avl_free( oc_index, oc_destroy_one );
470
471         while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
472                 o = LDAP_STAILQ_FIRST(&oc_undef_list);
473                 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
474
475                 ch_free( (ObjectClass *)o );
476         }
477 }
478
479 int
480 oc_start( ObjectClass **oc )
481 {
482         assert( oc != NULL );
483
484         *oc = LDAP_STAILQ_FIRST(&oc_list);
485
486         return (*oc != NULL);
487 }
488
489 int
490 oc_next( ObjectClass **oc )
491 {
492         assert( oc != NULL );
493
494 #if 0   /* pedantic check: breaks when deleting an oc, don't use it. */
495         {
496                 ObjectClass *tmp = NULL;
497
498                 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
499                         if ( tmp == *oc ) {
500                                 break;
501                         }
502                 }
503
504                 assert( tmp != NULL );
505         }
506 #endif
507
508         if ( *oc == NULL ) {
509                 return 0;
510         }
511
512         *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
513
514         return (*oc != NULL);
515 }
516
517 /*
518  * check whether the two ObjectClasses actually __are__ identical,
519  * or rather inconsistent
520  */
521 static int
522 oc_check_dup(
523         ObjectClass     *soc,
524         ObjectClass     *new_soc )
525 {
526         if ( new_soc->soc_oid != NULL ) {
527                 if ( soc->soc_oid == NULL ) {
528                         return SLAP_SCHERR_CLASS_INCONSISTENT;
529                 }
530
531                 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
532                         return SLAP_SCHERR_CLASS_INCONSISTENT;
533                 }
534
535         } else {
536                 if ( soc->soc_oid != NULL ) {
537                         return SLAP_SCHERR_CLASS_INCONSISTENT;
538                 }
539         }
540
541         if ( new_soc->soc_names ) {
542                 int     i;
543
544                 if ( soc->soc_names == NULL ) {
545                         return SLAP_SCHERR_CLASS_INCONSISTENT;
546                 }
547
548                 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
549                         if ( soc->soc_names[ i ] == NULL ) {
550                                 return SLAP_SCHERR_CLASS_INCONSISTENT;
551                         }
552                         
553                         if ( strcasecmp( soc->soc_names[ i ],
554                                         new_soc->soc_names[ i ] ) != 0 )
555                         {
556                                 return SLAP_SCHERR_CLASS_INCONSISTENT;
557                         }
558                 }
559         } else {
560                 if ( soc->soc_names != NULL ) {
561                         return SLAP_SCHERR_CLASS_INCONSISTENT;
562                 }
563         }
564
565         return SLAP_SCHERR_CLASS_DUP;
566 }
567
568 static struct oindexrec *oir_old;
569
570 static int
571 oc_dup_error( void *left, void *right )
572 {
573         oir_old = left;
574         return -1;
575 }
576
577 static int
578 oc_insert(
579     ObjectClass         **roc,
580         ObjectClass             *prev,
581     const char          **err )
582 {
583         struct oindexrec        *oir;
584         char                    **names;
585         ObjectClass             *soc = *roc;
586
587         if ( soc->soc_oid ) {
588                 oir = (struct oindexrec *)
589                         ch_calloc( 1, sizeof(struct oindexrec) );
590                 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
591                 oir->oir_oc = soc;
592                 oir_old = NULL;
593
594                 if ( avl_insert( &oc_index, (caddr_t) oir,
595                         oc_index_cmp, oc_dup_error ) )
596                 {
597                         ObjectClass     *old_soc;
598                         int             rc;
599
600                         *err = soc->soc_oid;
601
602                         assert( oir_old != NULL );
603                         old_soc = oir_old->oir_oc;
604
605                         /* replacing a deleted definition? */
606                         if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
607                                 ObjectClass tmp;
608
609                                 /* Keep old oid, free new oid;
610                                  * Keep new everything else, free old
611                                  */
612                                 tmp = *old_soc;
613                                 *old_soc = *soc;
614                                 old_soc->soc_oid = tmp.soc_oid;
615                                 tmp.soc_oid = soc->soc_oid;
616                                 *soc = tmp;
617
618                                 oc_clean( soc );
619                                 oc_destroy_one( oir );
620
621                                 oir = oir_old;
622                                 soc = old_soc;
623                                 *roc = soc;
624                         } else {
625                                 rc = oc_check_dup( old_soc, soc );
626
627                                 ldap_memfree( oir );
628                                 return rc;
629                         }
630                 }
631
632                 /* FIX: temporal consistency check */
633                 assert( oc_bvfind( &oir->oir_name ) != NULL );
634         }
635
636         if ( (names = soc->soc_names) ) {
637                 while ( *names ) {
638                         oir = (struct oindexrec *)
639                                 ch_calloc( 1, sizeof(struct oindexrec) );
640                         oir->oir_name.bv_val = *names;
641                         oir->oir_name.bv_len = strlen( *names );
642                         oir->oir_oc = soc;
643
644                         assert( oir->oir_name.bv_val != NULL );
645                         assert( oir->oir_oc != NULL );
646
647                         if ( avl_insert( &oc_index, (caddr_t) oir,
648                                 oc_index_cmp, avl_dup_error ) )
649                         {
650                                 ObjectClass     *old_soc;
651                                 int             rc;
652
653                                 *err = *names;
654
655                                 old_soc = oc_bvfind( &oir->oir_name );
656                                 assert( old_soc != NULL );
657                                 rc = oc_check_dup( old_soc, soc );
658
659                                 ldap_memfree( oir );
660
661                                 while ( names > soc->soc_names ) {
662                                         struct oindexrec        tmpoir;
663
664                                         names--;
665                                         ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
666                                         tmpoir.oir_oc = soc;
667                                         oir = (struct oindexrec *)avl_delete( &oc_index,
668                                                 (caddr_t)&tmpoir, oc_index_cmp );
669                                         assert( oir != NULL );
670                                         ldap_memfree( oir );
671                                 }
672
673                                 if ( soc->soc_oid ) {
674                                         struct oindexrec        tmpoir;
675
676                                         ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
677                                         tmpoir.oir_oc = soc;
678                                         oir = (struct oindexrec *)avl_delete( &oc_index,
679                                                 (caddr_t)&tmpoir, oc_index_cmp );
680                                         assert( oir != NULL );
681                                         ldap_memfree( oir );
682                                 }
683
684                                 return rc;
685                         }
686
687                         /* FIX: temporal consistency check */
688                         assert( oc_bvfind(&oir->oir_name) != NULL );
689
690                         names++;
691                 }
692         }
693         if ( soc->soc_flags & SLAP_OC_HARDCODE ) {
694                 prev = oc_sys_tail;
695                 oc_sys_tail = soc;
696         }
697         if ( prev ) {
698                 LDAP_STAILQ_INSERT_AFTER( &oc_list, prev, soc, soc_next );
699         } else {
700                 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
701         }
702
703         return 0;
704 }
705
706 int
707 oc_add(
708     LDAPObjectClass     *oc,
709         int user,
710         ObjectClass             **rsoc,
711         ObjectClass             *prev,
712     const char          **err )
713 {
714         ObjectClass     *soc;
715         int             code;
716         int             op = 0;
717         char    *oidm = NULL;
718
719         if ( oc->oc_names != NULL ) {
720                 int i;
721
722                 for( i=0; oc->oc_names[i]; i++ ) {
723                         if( !slap_valid_descr( oc->oc_names[i] ) ) {
724                                 return SLAP_SCHERR_BAD_DESCR;
725                         }
726                 }
727         }
728
729         if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
730                 /* Expand OID macros */
731                 char *oid = oidm_find( oc->oc_oid );
732                 if ( !oid ) {
733                         *err = oc->oc_oid;
734                         return SLAP_SCHERR_OIDM;
735                 }
736                 if ( oid != oc->oc_oid ) {
737                         oidm = oc->oc_oid;
738                         oc->oc_oid = oid;
739                 }
740         }
741
742         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
743         AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
744
745         soc->soc_oidmacro = oidm;
746         if( oc->oc_names != NULL ) {
747                 soc->soc_cname.bv_val = soc->soc_names[0];
748         } else {
749                 soc->soc_cname.bv_val = soc->soc_oid;
750         }
751         soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
752
753         if( soc->soc_sup_oids == NULL &&
754                 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
755         {
756                 /* structural object classes implicitly inherit from 'top' */
757                 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
758                 code = oc_add_sups( soc, top_oids, &op, err );
759         } else {
760                 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
761         }
762
763         if ( code != 0 ) {
764                 goto done;
765         }
766
767         if ( user && op ) {
768                 code = SLAP_SCHERR_CLASS_BAD_SUP;
769                 goto done;
770         }
771
772         code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
773         if ( code != 0 ) {
774                 goto done;
775         }
776
777         code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
778         if ( code != 0 ) {
779                 goto done;
780         }
781
782         if ( user && op ) {
783                 code = SLAP_SCHERR_CLASS_BAD_USAGE;
784                 goto done;
785         }
786
787         if ( !user ) {
788                 soc->soc_flags |= SLAP_OC_HARDCODE;
789         }
790
791         code = oc_insert(&soc,prev,err);
792 done:;
793         if ( code != 0 ) {
794                 if ( soc->soc_sups ) {
795                         ch_free( soc->soc_sups );
796                 }
797
798                 if ( soc->soc_required ) {
799                         ch_free( soc->soc_required );
800                 }
801
802                 if ( soc->soc_allowed ) {
803                         ch_free( soc->soc_allowed );
804                 }
805
806                 ch_free( soc );
807
808         } else if ( rsoc ) {
809                 *rsoc = soc;
810         }
811         return code;
812 }
813
814 void
815 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
816 {
817         ObjectClass *oc;
818         int i, num;
819         struct berval bv, *bva = NULL, idx;
820         char ibuf[32];
821
822         if ( !start )
823                 start = LDAP_STAILQ_FIRST( &oc_list );
824
825         /* count the result size */
826         i = 0;
827         for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
828                 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
829                 i++;
830                 if ( oc == end ) break;
831         }
832         if (!i) return;
833
834         num = i;
835         bva = ch_malloc( (num+1) * sizeof(struct berval) );
836         BER_BVZERO( bva );
837         idx.bv_val = ibuf;
838         if ( sys ) {
839                 idx.bv_len = 0;
840                 ibuf[0] = '\0';
841         }
842         i = 0;
843         for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
844                 LDAPObjectClass loc, *locp;
845                 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
846                 if ( oc->soc_oidmacro ) {
847                         loc = oc->soc_oclass;
848                         loc.oc_oid = oc->soc_oidmacro;
849                         locp = &loc;
850                 } else {
851                         locp = &oc->soc_oclass;
852                 }
853                 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
854                         ber_bvarray_free( bva );
855                 }
856                 if ( !sys ) {
857                         idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
858                 }
859                 bva[i].bv_len = idx.bv_len + bv.bv_len;
860                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
861                 strcpy( bva[i].bv_val, ibuf );
862                 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
863                 i++;
864                 bva[i].bv_val = NULL;
865                 ldap_memfree( bv.bv_val );
866                 if ( oc == end ) break;
867         }
868         *res = bva;
869 }
870
871 int
872 oc_schema_info( Entry *e )
873 {
874         AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
875         ObjectClass     *oc;
876         struct berval   val;
877         struct berval   nval;
878
879         LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
880                 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
881
882                 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
883                         return -1;
884                 }
885
886                 nval = oc->soc_cname;
887
888 #if 0
889                 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
890                (long) val.bv_len, val.bv_val, nval.bv_val );
891 #endif
892
893                 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
894                         return -1;
895                 }
896                 ldap_memfree( val.bv_val );
897         }
898         return 0;
899 }
900
901 int
902 register_oc( const char *def, ObjectClass **soc, int dupok )
903 {
904         LDAPObjectClass *oc;
905         int code;
906         const char *err;
907
908         oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
909         if ( !oc ) {
910                 Debug( LDAP_DEBUG_ANY,
911                         "register_oc: objectclass \"%s\": %s, %s\n",
912                         def, ldap_scherr2str(code), err );
913                 return code;
914         }
915         code = oc_add(oc,0,NULL,NULL,&err);
916         if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
917                 Debug( LDAP_DEBUG_ANY,
918                         "register_oc: objectclass \"%s\": %s, %s\n",
919                         def, scherr2str(code), err );
920                 ldap_objectclass_free(oc);
921                 return code;
922         }
923         if ( soc )
924                 *soc = oc_find(oc->oc_names[0]);
925         if ( code ) {
926                 ldap_objectclass_free(oc);
927         } else {
928                 ldap_memfree(oc);
929         }
930         return 0;
931 }