]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
5ce6f905e2b4185e18bfc505d294c50a46365d17
[openldap] / servers / slapd / schema.c
1 /* schema.c - routines to enforce schema definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 static char *   oc_check_required(Entry *e, char *ocname);
20 static int              oc_check_allowed(char *type, struct berval **ocl);
21
22 /*
23  * oc_check - check that entry e conforms to the schema required by
24  * its object class(es). returns 0 if so, non-zero otherwise.
25  */
26
27 int
28 oc_schema_check( Entry *e )
29 {
30         Attribute       *a, *aoc;
31         ObjectClass *oc;
32         int             i;
33         int             ret = 0;
34
35
36         /* find the object class attribute - could error out here */
37         if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
38                 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
39                     e->e_dn, 0, 0 );
40                 return( 1 );
41         }
42
43         /* check that the entry has required attrs for each oc */
44         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
45                 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
46                         Debug( LDAP_DEBUG_ANY,
47                                 "Objectclass \"%s\" not defined\n",
48                                 aoc->a_vals[i]->bv_val, 0, 0 );
49                 }
50                 else
51                 {
52                         char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
53
54                         if (s != NULL) {
55                                 Debug( LDAP_DEBUG_ANY,
56                                         "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
57                                         e->e_dn, aoc->a_vals[i]->bv_val, s );
58                                 ret = 1;
59                         }
60                 }
61         }
62
63         if ( ret != 0 ) {
64             return( ret );
65         }
66
67         /* check that each attr in the entry is allowed by some oc */
68         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
69                 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
70                         Debug( LDAP_DEBUG_ANY,
71                             "Entry (%s), attr \"%s\" not allowed\n",
72                             e->e_dn, a->a_type, 0 );
73                         ret = 1;
74                 }
75         }
76
77         return( ret );
78 }
79
80 static char *
81 oc_check_required( Entry *e, char *ocname )
82 {
83         ObjectClass     *oc;
84         AttributeType   *at;
85         int             i;
86         Attribute       *a;
87         char            **pp;
88
89         Debug( LDAP_DEBUG_TRACE,
90                "oc_check_required entry (%s), objectclass \"%s\"\n",
91                e->e_dn, ocname, 0 );
92
93         /* find global oc defn. it we don't know about it assume it's ok */
94         if ( (oc = oc_find( ocname )) == NULL ) {
95                 return( 0 );
96         }
97
98         /* check for empty oc_required */
99         if(oc->soc_required == NULL) {
100                 return( 0 );
101         }
102
103         /* for each required attribute */
104         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
105                 at = oc->soc_required[i];
106                 /* see if it's in the entry */
107                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
108                         if ( at->sat_oid &&
109                              strcmp( a->a_type, at->sat_oid ) == 0 ) {
110                                 break;
111                         }
112                         pp = at->sat_names;
113                         if ( pp  == NULL ) {
114                                 /* Empty name list => not found */
115                                 a = NULL;
116                                 break;
117                         }
118                         while ( *pp ) {
119                                 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
120                                         break;
121                                 }
122                                 pp++;
123                         }
124                         if ( *pp ) {
125                                 break;
126                         }
127                 }
128                 /* not there => schema violation */
129                 if ( a == NULL ) {
130                         if ( at->sat_names && at->sat_names[0] ) {
131                                 return at->sat_names[0];
132                         } else {
133                                 return at->sat_oid;
134                         }
135                 }
136         }
137
138         return( NULL );
139 }
140
141 #ifdef SLAPD_SCHEMA_COMPAT
142         /* these shouldn't be hardcoded */
143
144 static char *oc_op_usermod_attrs[] = {
145         /*
146          * these are operational attributes which are
147          * not defined as NO-USER_MODIFICATION and
148          * which slapd supports modification of.
149          *
150          * Currently none.
151          * Likely candidate, "aci"
152          */
153         NULL
154 };
155
156 static char *oc_op_attrs[] = {
157         /*
158          * these are operational attributes 
159          * most could be user modifiable
160          */
161         "objectClasses",
162         "attributeTypes",
163         "matchingRules",
164         "matchingRuleUse",
165         "dITStructureRules",
166         "dITContentRules",
167         "nameForms",
168         "ldapSyntaxes",
169         "namingContexts",
170         "supportedExtension",
171         "supportedControl",
172         "supportedSASLMechanisms",
173         "supportedLDAPversion",
174         "supportedACIMechanisms",
175         "subschemaSubentry",            /* NO USER MOD */
176         NULL
177
178 };
179
180 /* this list should be extensible  */
181 static char *oc_op_no_usermod_attrs[] = {
182         /*
183          * Operational and 'no user modification' attributes
184          * which are STORED in the directory server.
185          */
186
187         /* RFC2252, 3.2.1 */
188         "creatorsName",
189         "createTimestamp",
190         "modifiersName",
191         "modifyTimestamp",
192
193         NULL
194 };
195 #endif
196
197
198 /*
199  * check to see if attribute is 'operational' or not.
200  */
201 int
202 oc_check_op_attr( const char *type )
203 {
204 #ifdef SLAPD_SCHEMA_COMPAT
205         return charray_inlist( oc_op_attrs, type )
206                 || charray_inlist( oc_op_usermod_attrs, type )
207                 || charray_inlist( oc_op_no_usermod_attrs, type );
208 #else
209         AttributeType *at = at_find( type );
210
211         if( at == NULL ) return 0;
212
213         return at->sat_usage != 0;
214 #endif
215 }
216
217 /*
218  * check to see if attribute can be user modified or not.
219  */
220 int
221 oc_check_op_usermod_attr( const char *type )
222 {
223 #ifdef SLAPD_SCHEMA_COMPAT
224         return charray_inlist( oc_op_usermod_attrs, type );
225 #else
226         /* not (yet) in schema */
227         return 0;
228 #endif
229 }
230
231 /*
232  * check to see if attribute is 'no user modification' or not.
233  */
234 int
235 oc_check_op_no_usermod_attr( const char *type )
236 {
237 #ifdef SLAPD_SCHEMA_COMPAT
238         return charray_inlist( oc_op_no_usermod_attrs, type );
239 #else
240         AttributeType *at = at_find( type );
241
242         if( at == NULL ) return 0;
243
244         return at->sat_no_user_mod;
245 #endif
246 }
247
248
249 static int
250 oc_check_allowed( char *type, struct berval **ocl )
251 {
252         ObjectClass     *oc;
253         AttributeType   *at;
254         int             i, j;
255         char            **pp;
256         char            *p, *t;
257
258         Debug( LDAP_DEBUG_TRACE,
259                "oc_check_allowed type \"%s\"\n", type, 0, 0 );
260
261         /* always allow objectclass attribute */
262         if ( strcasecmp( type, "objectclass" ) == 0 ) {
263                 return( 0 );
264         }
265
266         /*
267          * The "type" we have received is actually an AttributeDescription.
268          * Let's find out the corresponding type.
269          */
270         p = strchr( type, ';' );
271         if ( p ) {
272                 t = ch_malloc( p-type+1 );
273                 strncpy( t, type, p-type );
274                 t[p-type] = '\0';
275                 Debug( LDAP_DEBUG_TRACE,
276                        "oc_check_allowed type \"%s\" from \"%s\"\n",
277                        t, type, 0 );
278
279         } else {
280                 t = type;
281         }
282
283         /*
284          * All operational attributions are allowed by schema rules.
285          */
286         if ( oc_check_op_attr( t ) ) {
287                 return( 0 );
288         }
289
290         /* check that the type appears as req or opt in at least one oc */
291         for ( i = 0; ocl[i] != NULL; i++ ) {
292                 /* if we know about the oc */
293                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
294                         /* does it require the type? */
295                         for ( j = 0; oc->soc_required != NULL && 
296                                 oc->soc_required[j] != NULL; j++ ) {
297                                 at = oc->soc_required[j];
298                                 if ( at->sat_oid &&
299                                      strcmp(at->sat_oid, t ) == 0 ) {
300                                         if ( t != type )
301                                                 ldap_memfree( t );
302                                         return( 0 );
303                                 }
304                                 pp = at->sat_names;
305                                 if ( pp == NULL )
306                                         continue;
307                                 while ( *pp ) {
308                                         if ( strcasecmp( *pp, t ) == 0 ) {
309                                                 if ( t != type )
310                                                         ldap_memfree( t );
311                                                 return( 0 );
312                                         }
313                                         pp++;
314                                 }
315                         }
316                         /* does it allow the type? */
317                         for ( j = 0; oc->soc_allowed != NULL && 
318                                 oc->soc_allowed[j] != NULL; j++ ) {
319                                 at = oc->soc_allowed[j];
320                                 if ( at->sat_oid &&
321                                      strcmp( at->sat_oid, t ) == 0 ) {
322                                         if ( t != type )
323                                                 ldap_memfree( t );
324                                         return( 0 );
325                                 }
326                                 pp = at->sat_names;
327                                 if ( pp == NULL )
328                                         continue;
329                                 while ( *pp ) {
330                                         if ( strcasecmp( *pp, t ) == 0 ||
331                                              strcmp( *pp, "*" ) == 0 ) {
332                                                 if ( t != type )
333                                                         ldap_memfree( t );
334                                                 return( 0 );
335                                         }
336                                         pp++;
337                                 }
338                         }
339                         /* maybe the next oc allows it */
340
341 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
342                 /* we don't know about the oc. assume it allows it */
343                 } else {
344                         if ( t != type )
345                                 ldap_memfree( t );
346                         return( 0 );
347 #endif
348                 }
349         }
350
351         if ( t != type )
352                 ldap_memfree( t );
353         /* not allowed by any oc */
354         return( 1 );
355 }
356
357 struct oindexrec {
358         char            *oir_name;
359         ObjectClass     *oir_oc;
360 };
361
362 static Avlnode  *oc_index = NULL;
363 static ObjectClass *oc_list = NULL;
364
365 static int
366 oc_index_cmp(
367     struct oindexrec    *oir1,
368     struct oindexrec    *oir2
369 )
370 {
371         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
372 }
373
374 static int
375 oc_index_name_cmp(
376     char                *name,
377     struct oindexrec    *oir
378 )
379 {
380         return (strcasecmp( name, oir->oir_name ));
381 }
382
383 ObjectClass *
384 oc_find( const char *ocname )
385 {
386         struct oindexrec        *oir = NULL;
387
388         if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
389             (AVL_CMP) oc_index_name_cmp )) != NULL ) {
390                 return( oir->oir_oc );
391         }
392         return( NULL );
393 }
394
395 static int
396 oc_create_required(
397     ObjectClass         *soc,
398     char                **attrs,
399     const char          **err
400 )
401 {
402         char            **attrs1;
403         AttributeType   *sat;
404         AttributeType   **satp;
405         int             i;
406
407         if ( attrs ) {
408                 attrs1 = attrs;
409                 while ( *attrs1 ) {
410                         sat = at_find(*attrs1);
411                         if ( !sat ) {
412                                 *err = *attrs1;
413                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
414                         }
415                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
416                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
417                                         *err = *attrs1;
418                                         return SLAP_SCHERR_OUTOFMEM;
419                                 }
420                         }
421                         attrs1++;
422                 }
423                 /* Now delete duplicates from the allowed list */
424                 for ( satp = soc->soc_required; *satp; satp++ ) {
425                         i = at_find_in_list(*satp,soc->soc_allowed);
426                         if ( i >= 0 ) {
427                                 at_delete_from_list(i, &soc->soc_allowed);
428                         }
429                 }
430         }
431         return 0;
432 }
433
434 static int
435 oc_create_allowed(
436     ObjectClass         *soc,
437     char                **attrs,
438     const char          **err
439 )
440 {
441         char            **attrs1;
442         AttributeType   *sat;
443
444         if ( attrs ) {
445                 attrs1 = attrs;
446                 while ( *attrs1 ) {
447                         sat = at_find(*attrs1);
448                         if ( !sat ) {
449                                 *err = *attrs1;
450                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
451                         }
452                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
453                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
454                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
455                                         *err = *attrs1;
456                                         return SLAP_SCHERR_OUTOFMEM;
457                                 }
458                         }
459                         attrs1++;
460                 }
461         }
462         return 0;
463 }
464
465 static int
466 oc_add_sups(
467     ObjectClass         *soc,
468     char                **sups,
469     const char          **err
470 )
471 {
472         int             code;
473         ObjectClass     *soc1;
474         int             nsups;
475         char            **sups1;
476         int             add_sups = 0;
477
478         if ( sups ) {
479                 if ( !soc->soc_sups ) {
480                         /* We are at the first recursive level */
481                         add_sups = 1;
482                         nsups = 0;
483                         sups1 = sups;
484                         while ( *sups1 ) {
485                                 nsups++;
486                                 sups1++;
487                         }
488                         nsups++;
489                         soc->soc_sups = (ObjectClass **)ch_calloc(1,
490                                           nsups*sizeof(ObjectClass *));
491                 }
492                 nsups = 0;
493                 sups1 = sups;
494                 while ( *sups1 ) {
495                         soc1 = oc_find(*sups1);
496                         if ( !soc1 ) {
497                                 *err = *sups1;
498                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
499                         }
500
501                         if ( add_sups )
502                                 soc->soc_sups[nsups] = soc1;
503
504                         code = oc_add_sups(soc,soc1->soc_sup_oids, err);
505                         if ( code )
506                                 return code;
507
508                         code = oc_create_required(soc,soc1->soc_at_oids_must,err);
509                         if ( code )
510                                 return code;
511                         code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
512                         if ( code )
513                                 return code;
514
515                         nsups++;
516                         sups1++;
517                 }
518         }
519         return 0;
520 }
521
522 static int
523 oc_insert(
524     ObjectClass         *soc,
525     const char          **err
526 )
527 {
528         ObjectClass     **ocp;
529         struct oindexrec        *oir;
530         char                    **names;
531
532         ocp = &oc_list;
533         while ( *ocp != NULL ) {
534                 ocp = &(*ocp)->soc_next;
535         }
536         *ocp = soc;
537
538         if ( soc->soc_oid ) {
539                 oir = (struct oindexrec *)
540                         ch_calloc( 1, sizeof(struct oindexrec) );
541                 oir->oir_name = soc->soc_oid;
542                 oir->oir_oc = soc;
543                 if ( avl_insert( &oc_index, (caddr_t) oir,
544                                  (AVL_CMP) oc_index_cmp,
545                                  (AVL_DUP) avl_dup_error ) ) {
546                         *err = soc->soc_oid;
547                         ldap_memfree(oir);
548                         return SLAP_SCHERR_DUP_CLASS;
549                 }
550                 /* FIX: temporal consistency check */
551                 oc_find(oir->oir_name);
552         }
553         if ( (names = soc->soc_names) ) {
554                 while ( *names ) {
555                         oir = (struct oindexrec *)
556                                 ch_calloc( 1, sizeof(struct oindexrec) );
557                         oir->oir_name = ch_strdup(*names);
558                         oir->oir_oc = soc;
559                         if ( avl_insert( &oc_index, (caddr_t) oir,
560                                          (AVL_CMP) oc_index_cmp,
561                                          (AVL_DUP) avl_dup_error ) ) {
562                                 *err = *names;
563                                 ldap_memfree(oir);
564                                 return SLAP_SCHERR_DUP_CLASS;
565                         }
566                         /* FIX: temporal consistency check */
567                         oc_find(oir->oir_name);
568                         names++;
569                 }
570         }
571         return 0;
572 }
573
574 int
575 oc_add(
576     LDAP_OBJECT_CLASS   *oc,
577     const char          **err
578 )
579 {
580         ObjectClass     *soc;
581         int             code;
582
583         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
584         memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
585         if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
586                 return code;
587         if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
588                 return code;
589         if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
590                 return code;
591         code = oc_insert(soc,err);
592         return code;
593 }
594
595 struct sindexrec {
596         char            *sir_name;
597         Syntax          *sir_syn;
598 };
599
600 static Avlnode  *syn_index = NULL;
601 static Syntax *syn_list = NULL;
602
603 static int
604 syn_index_cmp(
605     struct sindexrec    *sir1,
606     struct sindexrec    *sir2
607 )
608 {
609         return (strcmp( sir1->sir_name, sir2->sir_name ));
610 }
611
612 static int
613 syn_index_name_cmp(
614     char                *name,
615     struct sindexrec    *sir
616 )
617 {
618         return (strcmp( name, sir->sir_name ));
619 }
620
621 Syntax *
622 syn_find( const char *synname )
623 {
624         struct sindexrec        *sir = NULL;
625
626         if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
627             (AVL_CMP) syn_index_name_cmp )) != NULL ) {
628                 return( sir->sir_syn );
629         }
630         return( NULL );
631 }
632
633 Syntax *
634 syn_find_desc( const char *syndesc, int *len )
635 {
636         Syntax          *synp;
637
638         for (synp = syn_list; synp; synp = synp->ssyn_next)
639                 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
640                         return synp;
641         return( NULL );
642 }
643
644 static int
645 syn_insert(
646     Syntax              *ssyn,
647     const char          **err
648 )
649 {
650         Syntax          **synp;
651         struct sindexrec        *sir;
652
653         synp = &syn_list;
654         while ( *synp != NULL ) {
655                 synp = &(*synp)->ssyn_next;
656         }
657         *synp = ssyn;
658
659         if ( ssyn->ssyn_oid ) {
660                 sir = (struct sindexrec *)
661                         ch_calloc( 1, sizeof(struct sindexrec) );
662                 sir->sir_name = ssyn->ssyn_oid;
663                 sir->sir_syn = ssyn;
664                 if ( avl_insert( &syn_index, (caddr_t) sir,
665                                  (AVL_CMP) syn_index_cmp,
666                                  (AVL_DUP) avl_dup_error ) ) {
667                         *err = ssyn->ssyn_oid;
668                         ldap_memfree(sir);
669                         return SLAP_SCHERR_DUP_SYNTAX;
670                 }
671                 /* FIX: temporal consistency check */
672                 syn_find(sir->sir_name);
673         }
674         return 0;
675 }
676
677 int
678 syn_add(
679     LDAP_SYNTAX         *syn,
680     slap_syntax_validate_func   *validate,
681     slap_syntax_transform_func  *ber2str,
682     slap_syntax_transform_func  *str2ber,
683     const char          **err
684 )
685 {
686         Syntax          *ssyn;
687         int             code;
688
689         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
690         memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
691
692         ssyn->ssyn_validate = validate;
693         ssyn->ssyn_ber2str = ber2str;
694         ssyn->ssyn_str2ber = str2ber;
695
696         code = syn_insert(ssyn,err);
697         return code;
698 }
699
700 struct mindexrec {
701         char            *mir_name;
702         MatchingRule    *mir_mr;
703 };
704
705 static Avlnode  *mr_index = NULL;
706 static MatchingRule *mr_list = NULL;
707
708 static int
709 mr_index_cmp(
710     struct mindexrec    *mir1,
711     struct mindexrec    *mir2
712 )
713 {
714         return (strcmp( mir1->mir_name, mir2->mir_name ));
715 }
716
717 static int
718 mr_index_name_cmp(
719     char                *name,
720     struct mindexrec    *mir
721 )
722 {
723         return (strcmp( name, mir->mir_name ));
724 }
725
726 MatchingRule *
727 mr_find( const char *mrname )
728 {
729         struct mindexrec        *mir = NULL;
730
731         if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
732             (AVL_CMP) mr_index_name_cmp )) != NULL ) {
733                 return( mir->mir_mr );
734         }
735         return( NULL );
736 }
737
738 static int
739 mr_insert(
740     MatchingRule        *smr,
741     const char          **err
742 )
743 {
744         MatchingRule            **mrp;
745         struct mindexrec        *mir;
746         char                    **names;
747
748         mrp = &mr_list;
749         while ( *mrp != NULL ) {
750                 mrp = &(*mrp)->smr_next;
751         }
752         *mrp = smr;
753
754         if ( smr->smr_oid ) {
755                 mir = (struct mindexrec *)
756                         ch_calloc( 1, sizeof(struct mindexrec) );
757                 mir->mir_name = smr->smr_oid;
758                 mir->mir_mr = smr;
759                 if ( avl_insert( &mr_index, (caddr_t) mir,
760                                  (AVL_CMP) mr_index_cmp,
761                                  (AVL_DUP) avl_dup_error ) ) {
762                         *err = smr->smr_oid;
763                         ldap_memfree(mir);
764                         return SLAP_SCHERR_DUP_RULE;
765                 }
766                 /* FIX: temporal consistency check */
767                 mr_find(mir->mir_name);
768         }
769         if ( (names = smr->smr_names) ) {
770                 while ( *names ) {
771                         mir = (struct mindexrec *)
772                                 ch_calloc( 1, sizeof(struct mindexrec) );
773                         mir->mir_name = ch_strdup(*names);
774                         mir->mir_mr = smr;
775                         if ( avl_insert( &mr_index, (caddr_t) mir,
776                                          (AVL_CMP) mr_index_cmp,
777                                          (AVL_DUP) avl_dup_error ) ) {
778                                 *err = *names;
779                                 ldap_memfree(mir);
780                                 return SLAP_SCHERR_DUP_RULE;
781                         }
782                         /* FIX: temporal consistency check */
783                         mr_find(mir->mir_name);
784                         names++;
785                 }
786         }
787         return 0;
788 }
789
790 int
791 mr_add(
792     LDAP_MATCHING_RULE          *mr,
793         slap_mr_convert_func *convert,
794         slap_mr_normalize_func *normalize,
795     slap_mr_match_func  *match,
796     const char          **err
797 )
798 {
799         MatchingRule    *smr;
800         Syntax          *syn;
801         int             code;
802
803         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
804         memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
805
806         smr->smr_convert = convert;
807         smr->smr_normalize = normalize;
808         smr->smr_match = match;
809
810         if ( smr->smr_syntax_oid ) {
811                 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
812                         smr->smr_syntax = syn;
813                 } else {
814                         *err = smr->smr_syntax_oid;
815                         return SLAP_SCHERR_SYN_NOT_FOUND;
816                 }
817         } else {
818                 *err = "";
819                 return SLAP_SCHERR_MR_INCOMPLETE;
820         }
821         code = mr_insert(smr,err);
822         return code;
823 }
824
825 int
826 register_syntax(
827         char * desc,
828         slap_syntax_validate_func *validate,
829         slap_syntax_transform_func *ber2str,
830         slap_syntax_transform_func *str2ber )
831 {
832         LDAP_SYNTAX     *syn;
833         int             code;
834         const char      *err;
835
836         syn = ldap_str2syntax( desc, &code, &err);
837         if ( !syn ) {
838                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
839                     ldap_scherr2str(code), err, desc );
840                 return( -1 );
841         }
842
843         code = syn_add( syn, validate, ber2str, str2ber, &err );
844         if ( code ) {
845                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
846                     scherr2str(code), err, desc );
847                 return( -1 );
848         }
849
850         return( 0 );
851 }
852
853 int
854 register_matching_rule(
855         char * desc,
856         slap_mr_convert_func *convert,
857         slap_mr_normalize_func *normalize,
858         slap_mr_match_func *match )
859 {
860         LDAP_MATCHING_RULE *mr;
861         int             code;
862         const char      *err;
863
864         mr = ldap_str2matchingrule( desc, &code, &err);
865         if ( !mr ) {
866                 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
867                     ldap_scherr2str(code), err, desc );
868                 return( -1 );
869         }
870
871         code = mr_add( mr, convert, normalize, match, &err );
872         if ( code ) {
873                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
874                     scherr2str(code), err, desc );
875                 return( -1 );
876         }
877         return( 0 );
878 }
879
880
881 #if defined( SLAPD_SCHEMA_DN )
882
883 static int
884 syn_schema_info( Entry *e )
885 {
886         struct berval   val;
887         struct berval   *vals[2];
888         Syntax          *syn;
889
890         vals[0] = &val;
891         vals[1] = NULL;
892
893         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
894                 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
895                 if ( val.bv_val ) {
896                         val.bv_len = strlen( val.bv_val );
897                         Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
898                                (long) val.bv_len, val.bv_val, 0 );
899                         attr_merge( e, "ldapSyntaxes", vals );
900                         ldap_memfree( val.bv_val );
901                 } else {
902                         return -1;
903                 }
904         }
905         return 0;
906 }
907
908 static int
909 mr_schema_info( Entry *e )
910 {
911         struct berval   val;
912         struct berval   *vals[2];
913         MatchingRule    *mr;
914
915         vals[0] = &val;
916         vals[1] = NULL;
917
918         for ( mr = mr_list; mr; mr = mr->smr_next ) {
919                 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
920                 if ( val.bv_val ) {
921                         val.bv_len = strlen( val.bv_val );
922                         Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
923                                (long) val.bv_len, val.bv_val, 0 );
924                         attr_merge( e, "matchingRules", vals );
925                         ldap_memfree( val.bv_val );
926                 } else {
927                         return -1;
928                 }
929         }
930         return 0;
931 }
932
933 static int
934 oc_schema_info( Entry *e )
935 {
936         struct berval   val;
937         struct berval   *vals[2];
938         ObjectClass     *oc;
939
940         vals[0] = &val;
941         vals[1] = NULL;
942
943         for ( oc = oc_list; oc; oc = oc->soc_next ) {
944                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
945                 if ( val.bv_val ) {
946                         val.bv_len = strlen( val.bv_val );
947                         Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
948                                (long) val.bv_len, val.bv_val, 0 );
949                         attr_merge( e, "objectClasses", vals );
950                         ldap_memfree( val.bv_val );
951                 } else {
952                         return -1;
953                 }
954         }
955         return 0;
956 }
957
958 void
959 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
960 {
961         Entry           *e;
962         struct berval   val;
963         struct berval   *vals[2];
964
965         vals[0] = &val;
966         vals[1] = NULL;
967
968         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
969
970         e->e_attrs = NULL;
971         e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
972         e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
973         (void) dn_normalize( e->e_ndn );
974         e->e_private = NULL;
975
976         {
977                 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
978                 val.bv_val = strchr( rdn, '=' );
979
980                 if( val.bv_val != NULL ) {
981                         *val.bv_val = '\0';
982                         val.bv_len = strlen( ++val.bv_val );
983
984                         attr_merge( e, rdn, vals );
985                 }
986
987                 free( rdn );
988         }
989
990         if ( syn_schema_info( e ) ) {
991                 /* Out of memory, do something about it */
992                 entry_free( e );
993                 return;
994         }
995         if ( mr_schema_info( e ) ) {
996                 /* Out of memory, do something about it */
997                 entry_free( e );
998                 return;
999         }
1000         if ( at_schema_info( e ) ) {
1001                 /* Out of memory, do something about it */
1002                 entry_free( e );
1003                 return;
1004         }
1005         if ( oc_schema_info( e ) ) {
1006                 /* Out of memory, do something about it */
1007                 entry_free( e );
1008                 return;
1009         }
1010         
1011         val.bv_val = "top";
1012         val.bv_len = sizeof("top")-1;
1013         attr_merge( e, "objectClass", vals );
1014
1015         val.bv_val = "LDAPsubentry";
1016         val.bv_len = sizeof("LDAPsubentry")-1;
1017         attr_merge( e, "objectClass", vals );
1018
1019         val.bv_val = "subschema";
1020         val.bv_len = sizeof("subschema")-1;
1021         attr_merge( e, "objectClass", vals );
1022
1023         val.bv_val = "extensibleObject";
1024         val.bv_len = sizeof("extensibleObject")-1;
1025         attr_merge( e, "objectClass", vals );
1026
1027         send_search_entry( &backends[0], conn, op,
1028                 e, attrs, attrsonly, NULL );
1029         send_search_result( conn, op, LDAP_SUCCESS,
1030                 NULL, NULL, NULL, NULL, 1 );
1031
1032         entry_free( e );
1033 }
1034 #endif
1035
1036 #ifdef LDAP_DEBUG
1037
1038 static void
1039 oc_print( ObjectClass *oc )
1040 {
1041         int     i;
1042         const char *mid;
1043
1044         printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1045         if ( oc->soc_required != NULL ) {
1046                 mid = "\trequires ";
1047                 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1048                         printf( "%s%s", mid,
1049                                 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1050                 printf( "\n" );
1051         }
1052         if ( oc->soc_allowed != NULL ) {
1053                 mid = "\tallows ";
1054                 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1055                         printf( "%s%s", mid,
1056                                 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1057                 printf( "\n" );
1058         }
1059 }
1060
1061 #endif
1062
1063 int is_entry_objectclass(
1064         Entry*  e,
1065         const char*     oc)
1066 {
1067         Attribute *attr;
1068         struct berval bv;
1069
1070         if( e == NULL || oc == NULL || *oc == '\0' )
1071                 return 0;
1072
1073         /*
1074          * find objectClass attribute
1075          */
1076         attr = attr_find(e->e_attrs, "objectclass");
1077
1078         if( attr == NULL ) {
1079                 /* no objectClass attribute */
1080                 return 0;
1081         }
1082
1083         bv.bv_val = (char *) oc;
1084         bv.bv_len = strlen( bv.bv_val );
1085
1086 #ifdef SLAPD_SCHEMA_COMPAT
1087         if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1088                 /* entry is not of this objectclass */
1089                 return 0;
1090         }
1091 #endif
1092
1093         return 1;
1094 }