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