]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Put type;option trimming codes behind SLAPD_SCHEMA_COMPAT.
[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 #ifndef SLAPD_SCHEMA_NOT_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 != LDAP_SCHEMA_USER_APPLICATIONS;
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 #ifdef SLAPD_SCHEMA_COMPAT
267         /* Treat any attribute type with option as an unknown attribute type */
268         /*
269          * The "type" we have received is actually an AttributeDescription.
270          * Let's find out the corresponding type.
271          */
272         p = strchr( type, ';' );
273         if ( p ) {
274                 t = ch_malloc( p-type+1 );
275                 strncpy( t, type, p-type );
276                 t[p-type] = '\0';
277                 Debug( LDAP_DEBUG_TRACE,
278                        "oc_check_allowed type \"%s\" from \"%s\"\n",
279                        t, type, 0 );
280
281         } else
282 #endif
283         {
284                 t = type;
285         }
286
287
288         /*
289          * All operational attributions are allowed by schema rules.
290          */
291         if ( oc_check_op_attr( t ) ) {
292                 return( 0 );
293         }
294
295         /* check that the type appears as req or opt in at least one oc */
296         for ( i = 0; ocl[i] != NULL; i++ ) {
297                 /* if we know about the oc */
298                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
299                         /* does it require the type? */
300                         for ( j = 0; oc->soc_required != NULL && 
301                                 oc->soc_required[j] != NULL; j++ ) {
302                                 at = oc->soc_required[j];
303                                 if ( at->sat_oid &&
304                                      strcmp(at->sat_oid, t ) == 0 ) {
305                                         if ( t != type )
306                                                 ldap_memfree( t );
307                                         return( 0 );
308                                 }
309                                 pp = at->sat_names;
310                                 if ( pp == NULL )
311                                         continue;
312                                 while ( *pp ) {
313                                         if ( strcasecmp( *pp, t ) == 0 ) {
314                                                 if ( t != type )
315                                                         ldap_memfree( t );
316                                                 return( 0 );
317                                         }
318                                         pp++;
319                                 }
320                         }
321                         /* does it allow the type? */
322                         for ( j = 0; oc->soc_allowed != NULL && 
323                                 oc->soc_allowed[j] != NULL; j++ ) {
324                                 at = oc->soc_allowed[j];
325                                 if ( at->sat_oid &&
326                                      strcmp( at->sat_oid, t ) == 0 ) {
327                                         if ( t != type )
328                                                 ldap_memfree( t );
329                                         return( 0 );
330                                 }
331                                 pp = at->sat_names;
332                                 if ( pp == NULL )
333                                         continue;
334                                 while ( *pp ) {
335                                         if ( strcasecmp( *pp, t ) == 0 ||
336                                              strcmp( *pp, "*" ) == 0 ) {
337                                                 if ( t != type )
338                                                         ldap_memfree( t );
339                                                 return( 0 );
340                                         }
341                                         pp++;
342                                 }
343                         }
344                         /* maybe the next oc allows it */
345
346 #ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
347                 /* we don't know about the oc. assume it allows it */
348                 } else {
349                         if ( t != type )
350                                 ldap_memfree( t );
351                         return( 0 );
352 #endif
353                 }
354         }
355
356         if ( t != type )
357                 ldap_memfree( t );
358         /* not allowed by any oc */
359         return( 1 );
360 }
361
362 struct oindexrec {
363         char            *oir_name;
364         ObjectClass     *oir_oc;
365 };
366
367 static Avlnode  *oc_index = NULL;
368 static ObjectClass *oc_list = NULL;
369
370 static int
371 oc_index_cmp(
372     struct oindexrec    *oir1,
373     struct oindexrec    *oir2
374 )
375 {
376         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
377 }
378
379 static int
380 oc_index_name_cmp(
381     char                *name,
382     struct oindexrec    *oir
383 )
384 {
385         return (strcasecmp( name, oir->oir_name ));
386 }
387
388 ObjectClass *
389 oc_find( const char *ocname )
390 {
391         struct oindexrec        *oir = NULL;
392
393         if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
394             (AVL_CMP) oc_index_name_cmp )) != NULL ) {
395                 return( oir->oir_oc );
396         }
397         return( NULL );
398 }
399
400 static int
401 oc_create_required(
402     ObjectClass         *soc,
403     char                **attrs,
404     const char          **err
405 )
406 {
407         char            **attrs1;
408         AttributeType   *sat;
409         AttributeType   **satp;
410         int             i;
411
412         if ( attrs ) {
413                 attrs1 = attrs;
414                 while ( *attrs1 ) {
415                         sat = at_find(*attrs1);
416                         if ( !sat ) {
417                                 *err = *attrs1;
418                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
419                         }
420                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
421                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
422                                         *err = *attrs1;
423                                         return SLAP_SCHERR_OUTOFMEM;
424                                 }
425                         }
426                         attrs1++;
427                 }
428                 /* Now delete duplicates from the allowed list */
429                 for ( satp = soc->soc_required; *satp; satp++ ) {
430                         i = at_find_in_list(*satp,soc->soc_allowed);
431                         if ( i >= 0 ) {
432                                 at_delete_from_list(i, &soc->soc_allowed);
433                         }
434                 }
435         }
436         return 0;
437 }
438
439 static int
440 oc_create_allowed(
441     ObjectClass         *soc,
442     char                **attrs,
443     const char          **err
444 )
445 {
446         char            **attrs1;
447         AttributeType   *sat;
448
449         if ( attrs ) {
450                 attrs1 = attrs;
451                 while ( *attrs1 ) {
452                         sat = at_find(*attrs1);
453                         if ( !sat ) {
454                                 *err = *attrs1;
455                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
456                         }
457                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
458                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
459                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
460                                         *err = *attrs1;
461                                         return SLAP_SCHERR_OUTOFMEM;
462                                 }
463                         }
464                         attrs1++;
465                 }
466         }
467         return 0;
468 }
469
470 static int
471 oc_add_sups(
472     ObjectClass         *soc,
473     char                **sups,
474     const char          **err
475 )
476 {
477         int             code;
478         ObjectClass     *soc1;
479         int             nsups;
480         char            **sups1;
481         int             add_sups = 0;
482
483         if ( sups ) {
484                 if ( !soc->soc_sups ) {
485                         /* We are at the first recursive level */
486                         add_sups = 1;
487                         nsups = 0;
488                         sups1 = sups;
489                         while ( *sups1 ) {
490                                 nsups++;
491                                 sups1++;
492                         }
493                         nsups++;
494                         soc->soc_sups = (ObjectClass **)ch_calloc(1,
495                                           nsups*sizeof(ObjectClass *));
496                 }
497                 nsups = 0;
498                 sups1 = sups;
499                 while ( *sups1 ) {
500                         soc1 = oc_find(*sups1);
501                         if ( !soc1 ) {
502                                 *err = *sups1;
503                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
504                         }
505
506                         if ( add_sups )
507                                 soc->soc_sups[nsups] = soc1;
508
509                         code = oc_add_sups(soc,soc1->soc_sup_oids, err);
510                         if ( code )
511                                 return code;
512
513                         code = oc_create_required(soc,soc1->soc_at_oids_must,err);
514                         if ( code )
515                                 return code;
516                         code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
517                         if ( code )
518                                 return code;
519
520                         nsups++;
521                         sups1++;
522                 }
523         }
524         return 0;
525 }
526
527 static int
528 oc_insert(
529     ObjectClass         *soc,
530     const char          **err
531 )
532 {
533         ObjectClass     **ocp;
534         struct oindexrec        *oir;
535         char                    **names;
536
537         ocp = &oc_list;
538         while ( *ocp != NULL ) {
539                 ocp = &(*ocp)->soc_next;
540         }
541         *ocp = soc;
542
543         if ( soc->soc_oid ) {
544                 oir = (struct oindexrec *)
545                         ch_calloc( 1, sizeof(struct oindexrec) );
546                 oir->oir_name = soc->soc_oid;
547                 oir->oir_oc = soc;
548                 if ( avl_insert( &oc_index, (caddr_t) oir,
549                                  (AVL_CMP) oc_index_cmp,
550                                  (AVL_DUP) avl_dup_error ) ) {
551                         *err = soc->soc_oid;
552                         ldap_memfree(oir);
553                         return SLAP_SCHERR_DUP_CLASS;
554                 }
555                 /* FIX: temporal consistency check */
556                 oc_find(oir->oir_name);
557         }
558         if ( (names = soc->soc_names) ) {
559                 while ( *names ) {
560                         oir = (struct oindexrec *)
561                                 ch_calloc( 1, sizeof(struct oindexrec) );
562                         oir->oir_name = ch_strdup(*names);
563                         oir->oir_oc = soc;
564                         if ( avl_insert( &oc_index, (caddr_t) oir,
565                                          (AVL_CMP) oc_index_cmp,
566                                          (AVL_DUP) avl_dup_error ) ) {
567                                 *err = *names;
568                                 ldap_memfree(oir);
569                                 return SLAP_SCHERR_DUP_CLASS;
570                         }
571                         /* FIX: temporal consistency check */
572                         oc_find(oir->oir_name);
573                         names++;
574                 }
575         }
576         return 0;
577 }
578
579 int
580 oc_add(
581     LDAP_OBJECT_CLASS   *oc,
582     const char          **err
583 )
584 {
585         ObjectClass     *soc;
586         int             code;
587
588         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
589         memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
590         if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
591                 return code;
592         if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
593                 return code;
594         if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
595                 return code;
596         code = oc_insert(soc,err);
597         return code;
598 }
599
600 struct sindexrec {
601         char            *sir_name;
602         Syntax          *sir_syn;
603 };
604
605 static Avlnode  *syn_index = NULL;
606 static Syntax *syn_list = NULL;
607
608 static int
609 syn_index_cmp(
610     struct sindexrec    *sir1,
611     struct sindexrec    *sir2
612 )
613 {
614         return (strcmp( sir1->sir_name, sir2->sir_name ));
615 }
616
617 static int
618 syn_index_name_cmp(
619     char                *name,
620     struct sindexrec    *sir
621 )
622 {
623         return (strcmp( name, sir->sir_name ));
624 }
625
626 Syntax *
627 syn_find( const char *synname )
628 {
629         struct sindexrec        *sir = NULL;
630
631         if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
632             (AVL_CMP) syn_index_name_cmp )) != NULL ) {
633                 return( sir->sir_syn );
634         }
635         return( NULL );
636 }
637
638 Syntax *
639 syn_find_desc( const char *syndesc, int *len )
640 {
641         Syntax          *synp;
642
643         for (synp = syn_list; synp; synp = synp->ssyn_next)
644                 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
645                         return synp;
646         return( NULL );
647 }
648
649 static int
650 syn_insert(
651     Syntax              *ssyn,
652     const char          **err
653 )
654 {
655         Syntax          **synp;
656         struct sindexrec        *sir;
657
658         synp = &syn_list;
659         while ( *synp != NULL ) {
660                 synp = &(*synp)->ssyn_next;
661         }
662         *synp = ssyn;
663
664         if ( ssyn->ssyn_oid ) {
665                 sir = (struct sindexrec *)
666                         ch_calloc( 1, sizeof(struct sindexrec) );
667                 sir->sir_name = ssyn->ssyn_oid;
668                 sir->sir_syn = ssyn;
669                 if ( avl_insert( &syn_index, (caddr_t) sir,
670                                  (AVL_CMP) syn_index_cmp,
671                                  (AVL_DUP) avl_dup_error ) ) {
672                         *err = ssyn->ssyn_oid;
673                         ldap_memfree(sir);
674                         return SLAP_SCHERR_DUP_SYNTAX;
675                 }
676                 /* FIX: temporal consistency check */
677                 syn_find(sir->sir_name);
678         }
679         return 0;
680 }
681
682 int
683 syn_add(
684     LDAP_SYNTAX         *syn,
685         int flags,
686     slap_syntax_validate_func   *validate,
687     slap_syntax_transform_func  *ber2str,
688     slap_syntax_transform_func  *str2ber,
689     const char          **err
690 )
691 {
692         Syntax          *ssyn;
693         int             code;
694
695         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
696         memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
697
698         ssyn->ssyn_flags = flags;
699         ssyn->ssyn_validate = validate;
700         ssyn->ssyn_ber2str = ber2str;
701         ssyn->ssyn_str2ber = str2ber;
702
703         code = syn_insert(ssyn,err);
704         return code;
705 }
706
707 struct mindexrec {
708         char            *mir_name;
709         MatchingRule    *mir_mr;
710 };
711
712 static Avlnode  *mr_index = NULL;
713 static MatchingRule *mr_list = NULL;
714
715 static int
716 mr_index_cmp(
717     struct mindexrec    *mir1,
718     struct mindexrec    *mir2
719 )
720 {
721         return (strcmp( mir1->mir_name, mir2->mir_name ));
722 }
723
724 static int
725 mr_index_name_cmp(
726     char                *name,
727     struct mindexrec    *mir
728 )
729 {
730         return (strcmp( name, mir->mir_name ));
731 }
732
733 MatchingRule *
734 mr_find( const char *mrname )
735 {
736         struct mindexrec        *mir = NULL;
737
738         if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
739             (AVL_CMP) mr_index_name_cmp )) != NULL ) {
740                 return( mir->mir_mr );
741         }
742         return( NULL );
743 }
744
745 static int
746 mr_insert(
747     MatchingRule        *smr,
748     const char          **err
749 )
750 {
751         MatchingRule            **mrp;
752         struct mindexrec        *mir;
753         char                    **names;
754
755         mrp = &mr_list;
756         while ( *mrp != NULL ) {
757                 mrp = &(*mrp)->smr_next;
758         }
759         *mrp = smr;
760
761         if ( smr->smr_oid ) {
762                 mir = (struct mindexrec *)
763                         ch_calloc( 1, sizeof(struct mindexrec) );
764                 mir->mir_name = smr->smr_oid;
765                 mir->mir_mr = smr;
766                 if ( avl_insert( &mr_index, (caddr_t) mir,
767                                  (AVL_CMP) mr_index_cmp,
768                                  (AVL_DUP) avl_dup_error ) ) {
769                         *err = smr->smr_oid;
770                         ldap_memfree(mir);
771                         return SLAP_SCHERR_DUP_RULE;
772                 }
773                 /* FIX: temporal consistency check */
774                 mr_find(mir->mir_name);
775         }
776         if ( (names = smr->smr_names) ) {
777                 while ( *names ) {
778                         mir = (struct mindexrec *)
779                                 ch_calloc( 1, sizeof(struct mindexrec) );
780                         mir->mir_name = ch_strdup(*names);
781                         mir->mir_mr = smr;
782                         if ( avl_insert( &mr_index, (caddr_t) mir,
783                                          (AVL_CMP) mr_index_cmp,
784                                          (AVL_DUP) avl_dup_error ) ) {
785                                 *err = *names;
786                                 ldap_memfree(mir);
787                                 return SLAP_SCHERR_DUP_RULE;
788                         }
789                         /* FIX: temporal consistency check */
790                         mr_find(mir->mir_name);
791                         names++;
792                 }
793         }
794         return 0;
795 }
796
797 int
798 mr_add(
799     LDAP_MATCHING_RULE          *mr,
800         slap_mr_convert_func *convert,
801         slap_mr_normalize_func *normalize,
802     slap_mr_match_func  *match,
803         slap_mr_indexer_func *indexer,
804     slap_mr_filter_func *filter,
805     const char          **err
806 )
807 {
808         MatchingRule    *smr;
809         Syntax          *syn;
810         int             code;
811
812         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
813         memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
814
815         smr->smr_convert = convert;
816         smr->smr_normalize = normalize;
817         smr->smr_match = match;
818         smr->smr_indexer = indexer;
819         smr->smr_filter = filter;
820
821         if ( smr->smr_syntax_oid ) {
822                 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
823                         smr->smr_syntax = syn;
824                 } else {
825                         *err = smr->smr_syntax_oid;
826                         return SLAP_SCHERR_SYN_NOT_FOUND;
827                 }
828         } else {
829                 *err = "";
830                 return SLAP_SCHERR_MR_INCOMPLETE;
831         }
832         code = mr_insert(smr,err);
833         return code;
834 }
835
836 int
837 register_syntax(
838         char * desc, int flags,
839         slap_syntax_validate_func *validate,
840         slap_syntax_transform_func *ber2str,
841         slap_syntax_transform_func *str2ber )
842 {
843         LDAP_SYNTAX     *syn;
844         int             code;
845         const char      *err;
846
847         syn = ldap_str2syntax( desc, &code, &err);
848         if ( !syn ) {
849                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
850                     ldap_scherr2str(code), err, desc );
851                 return( -1 );
852         }
853
854         code = syn_add( syn, flags, validate, ber2str, str2ber, &err );
855         if ( code ) {
856                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
857                     scherr2str(code), err, desc );
858                 return( -1 );
859         }
860
861         return( 0 );
862 }
863
864 int
865 register_matching_rule(
866         char * desc,
867         slap_mr_convert_func *convert,
868         slap_mr_normalize_func *normalize,
869         slap_mr_match_func *match,
870         slap_mr_indexer_func *indexer,
871         slap_mr_filter_func *filter )
872 {
873         LDAP_MATCHING_RULE *mr;
874         int             code;
875         const char      *err;
876
877         mr = ldap_str2matchingrule( desc, &code, &err);
878         if ( !mr ) {
879                 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
880                     ldap_scherr2str(code), err, desc );
881                 return( -1 );
882         }
883
884         code = mr_add( mr, convert, normalize, match, indexer, filter, &err );
885         if ( code ) {
886                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
887                     scherr2str(code), err, desc );
888                 return( -1 );
889         }
890         return( 0 );
891 }
892
893
894 #if defined( SLAPD_SCHEMA_DN )
895
896 static int
897 syn_schema_info( Entry *e )
898 {
899         struct berval   val;
900         struct berval   *vals[2];
901         Syntax          *syn;
902
903         vals[0] = &val;
904         vals[1] = NULL;
905
906         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
907                 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
908                 if ( val.bv_val ) {
909                         val.bv_len = strlen( val.bv_val );
910                         Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
911                                (long) val.bv_len, val.bv_val, 0 );
912                         attr_merge( e, "ldapSyntaxes", vals );
913                         ldap_memfree( val.bv_val );
914                 } else {
915                         return -1;
916                 }
917         }
918         return 0;
919 }
920
921 static int
922 mr_schema_info( Entry *e )
923 {
924         struct berval   val;
925         struct berval   *vals[2];
926         MatchingRule    *mr;
927
928         vals[0] = &val;
929         vals[1] = NULL;
930
931         for ( mr = mr_list; mr; mr = mr->smr_next ) {
932                 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
933                 if ( val.bv_val ) {
934                         val.bv_len = strlen( val.bv_val );
935                         Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
936                                (long) val.bv_len, val.bv_val, 0 );
937                         attr_merge( e, "matchingRules", vals );
938                         ldap_memfree( val.bv_val );
939                 } else {
940                         return -1;
941                 }
942         }
943         return 0;
944 }
945
946 static int
947 oc_schema_info( Entry *e )
948 {
949         struct berval   val;
950         struct berval   *vals[2];
951         ObjectClass     *oc;
952
953         vals[0] = &val;
954         vals[1] = NULL;
955
956         for ( oc = oc_list; oc; oc = oc->soc_next ) {
957                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
958                 if ( val.bv_val ) {
959                         val.bv_len = strlen( val.bv_val );
960                         Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
961                                (long) val.bv_len, val.bv_val, 0 );
962                         attr_merge( e, "objectClasses", vals );
963                         ldap_memfree( val.bv_val );
964                 } else {
965                         return -1;
966                 }
967         }
968         return 0;
969 }
970
971 void
972 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
973 {
974         Entry           *e;
975         struct berval   val;
976         struct berval   *vals[2];
977
978         vals[0] = &val;
979         vals[1] = NULL;
980
981         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
982
983         e->e_attrs = NULL;
984         e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
985         e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
986         (void) dn_normalize( e->e_ndn );
987         e->e_private = NULL;
988
989         {
990                 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
991                 val.bv_val = strchr( rdn, '=' );
992
993                 if( val.bv_val != NULL ) {
994                         *val.bv_val = '\0';
995                         val.bv_len = strlen( ++val.bv_val );
996
997                         attr_merge( e, rdn, vals );
998                 }
999
1000                 free( rdn );
1001         }
1002
1003         if ( syn_schema_info( e ) ) {
1004                 /* Out of memory, do something about it */
1005                 entry_free( e );
1006                 return;
1007         }
1008         if ( mr_schema_info( e ) ) {
1009                 /* Out of memory, do something about it */
1010                 entry_free( e );
1011                 return;
1012         }
1013         if ( at_schema_info( e ) ) {
1014                 /* Out of memory, do something about it */
1015                 entry_free( e );
1016                 return;
1017         }
1018         if ( oc_schema_info( e ) ) {
1019                 /* Out of memory, do something about it */
1020                 entry_free( e );
1021                 return;
1022         }
1023         
1024         val.bv_val = "top";
1025         val.bv_len = sizeof("top")-1;
1026         attr_merge( e, "objectClass", vals );
1027
1028         val.bv_val = "LDAPsubentry";
1029         val.bv_len = sizeof("LDAPsubentry")-1;
1030         attr_merge( e, "objectClass", vals );
1031
1032         val.bv_val = "subschema";
1033         val.bv_len = sizeof("subschema")-1;
1034         attr_merge( e, "objectClass", vals );
1035
1036         val.bv_val = "extensibleObject";
1037         val.bv_len = sizeof("extensibleObject")-1;
1038         attr_merge( e, "objectClass", vals );
1039
1040         send_search_entry( &backends[0], conn, op,
1041                 e, attrs, attrsonly, NULL );
1042         send_search_result( conn, op, LDAP_SUCCESS,
1043                 NULL, NULL, NULL, NULL, 1 );
1044
1045         entry_free( e );
1046 }
1047 #endif
1048
1049 #ifdef LDAP_DEBUG
1050
1051 static void
1052 oc_print( ObjectClass *oc )
1053 {
1054         int     i;
1055         const char *mid;
1056
1057         printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1058         if ( oc->soc_required != NULL ) {
1059                 mid = "\trequires ";
1060                 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1061                         printf( "%s%s", mid,
1062                                 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1063                 printf( "\n" );
1064         }
1065         if ( oc->soc_allowed != NULL ) {
1066                 mid = "\tallows ";
1067                 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1068                         printf( "%s%s", mid,
1069                                 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1070                 printf( "\n" );
1071         }
1072 }
1073
1074 #endif
1075
1076 int is_entry_objectclass(
1077         Entry*  e,
1078         const char*     oc)
1079 {
1080         Attribute *attr;
1081         struct berval bv;
1082
1083         if( e == NULL || oc == NULL || *oc == '\0' )
1084                 return 0;
1085
1086         /*
1087          * find objectClass attribute
1088          */
1089         attr = attr_find(e->e_attrs, "objectclass");
1090
1091         if( attr == NULL ) {
1092                 /* no objectClass attribute */
1093                 return 0;
1094         }
1095
1096         bv.bv_val = (char *) oc;
1097         bv.bv_len = strlen( bv.bv_val );
1098
1099 #ifdef SLAPD_SCHEMA_COMPAT
1100         if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1101                 /* entry is not of this objectclass */
1102                 return 0;
1103         }
1104 #endif
1105
1106         return 1;
1107 }