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