]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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
18 static char *   oc_check_required(Entry *e, char *ocname);
19 static int              oc_check_allowed(char *type, struct berval **ocl);
20
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",
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 char *oc_usermod_attrs[] = {
142         /*
143          * OpenLDAP doesn't support any user modification of
144          * operational attributes.
145          */
146         NULL
147 };
148
149 static char *oc_operational_attrs[] = {
150         /*
151          * these are operational attributes 
152          * most could be user modifiable
153          */
154         "objectClasses",
155         "attributeTypes",
156         "matchingRules",
157         "matchingRuleUse",
158         "dITStructureRules",
159         "dITContentRules",
160         "nameForms",
161         "ldapSyntaxes",
162         "namingContexts",
163         "supportedExtension",
164         "supportedControl",
165         "supportedSASLMechanisms",
166         "supportedLDAPversion",
167         "subschemaSubentry",            /* NO USER MOD */
168         NULL
169
170 };
171
172 /* this list should be extensible  */
173 static char *oc_no_usermod_attrs[] = {
174         /*
175          * Operational and 'no user modification' attributes
176          * which are STORED in the directory server.
177          */
178
179         /* RFC2252, 3.2.1 */
180         "creatorsName",
181         "createTimestamp",
182         "modifiersName",
183         "modifyTimestamp",
184
185         NULL
186 };
187
188
189 /*
190  * check to see if attribute is 'operational' or not.
191  */
192 int
193 oc_check_operational_attr( const char *type )
194 {
195         return charray_inlist( oc_operational_attrs, type )
196                 || charray_inlist( oc_usermod_attrs, type )
197                 || charray_inlist( oc_no_usermod_attrs, type );
198 }
199
200 /*
201  * check to see if attribute can be user modified or not.
202  */
203 int
204 oc_check_usermod_attr( const char *type )
205 {
206         return charray_inlist( oc_usermod_attrs, type );
207 }
208
209 /*
210  * check to see if attribute is 'no user modification' or not.
211  */
212 int
213 oc_check_no_usermod_attr( const char *type )
214 {
215         return charray_inlist( oc_no_usermod_attrs, type );
216 }
217
218
219 static int
220 oc_check_allowed( char *type, struct berval **ocl )
221 {
222         ObjectClass     *oc;
223         AttributeType   *at;
224         int             i, j;
225         char            **pp;
226
227         Debug( LDAP_DEBUG_TRACE,
228                "oc_check_allowed type \"%s\"\n", type, 0, 0 );
229
230         /* always allow objectclass attribute */
231         if ( strcasecmp( type, "objectclass" ) == 0 ) {
232                 return( 0 );
233         }
234
235         /*
236          * All operational attributions are allowed by schema rules.
237          * However, we only check attributions which are stored in the
238          * the directory regardless if they are user or non-user modified.
239          */
240         if ( oc_check_usermod_attr( type ) || oc_check_no_usermod_attr( type ) ) {
241                 return( 0 );
242         }
243
244         /* check that the type appears as req or opt in at least one oc */
245         for ( i = 0; ocl[i] != NULL; i++ ) {
246                 /* if we know about the oc */
247                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
248                         /* does it require the type? */
249                         for ( j = 0; oc->soc_required != NULL && 
250                                 oc->soc_required[j] != NULL; j++ ) {
251                                 at = oc->soc_required[j];
252                                 if ( at->sat_oid &&
253                                      strcmp(at->sat_oid, type ) == 0 ) {
254                                         return( 0 );
255                                 }
256                                 pp = at->sat_names;
257                                 if ( pp == NULL )
258                                         continue;
259                                 while ( *pp ) {
260                                         if ( strcasecmp( *pp, type ) == 0 ) {
261                                                 return( 0 );
262                                         }
263                                         pp++;
264                                 }
265                         }
266                         /* does it allow the type? */
267                         for ( j = 0; oc->soc_allowed != NULL && 
268                                 oc->soc_allowed[j] != NULL; j++ ) {
269                                 at = oc->soc_allowed[j];
270                                 if ( at->sat_oid &&
271                                      strcmp(at->sat_oid, type ) == 0 ) {
272                                         return( 0 );
273                                 }
274                                 pp = at->sat_names;
275                                 if ( pp == NULL )
276                                         continue;
277                                 while ( *pp ) {
278                                         if ( strcasecmp( *pp, type ) == 0 ||
279                                              strcmp( *pp, "*" ) == 0 ) {
280                                                 return( 0 );
281                                         }
282                                         pp++;
283                                 }
284                         }
285                         /* maybe the next oc allows it */
286
287                 /* we don't know about the oc. assume it allows it */
288                 } else {
289                         return( 0 );
290                 }
291         }
292
293         /* not allowed by any oc */
294         return( 1 );
295 }
296
297 struct oindexrec {
298         char            *oir_name;
299         ObjectClass     *oir_oc;
300 };
301
302 static Avlnode  *oc_index = NULL;
303 static ObjectClass *oc_list = NULL;
304
305 static int
306 oc_index_cmp(
307     struct oindexrec    *oir1,
308     struct oindexrec    *oir2
309 )
310 {
311         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
312 }
313
314 static int
315 oc_index_name_cmp(
316     char                *name,
317     struct oindexrec    *oir
318 )
319 {
320         return (strcasecmp( name, oir->oir_name ));
321 }
322
323 ObjectClass *
324 oc_find( const char *ocname )
325 {
326         struct oindexrec        *oir = NULL;
327
328         if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
329             (AVL_CMP) oc_index_name_cmp )) != NULL ) {
330                 return( oir->oir_oc );
331         }
332         return( NULL );
333 }
334
335 static int
336 oc_create_required(
337     ObjectClass         *soc,
338     char                **attrs,
339     const char          **err
340 )
341 {
342         char            **attrs1;
343         AttributeType   *sat;
344         AttributeType   **satp;
345         int             i;
346
347         if ( attrs ) {
348                 attrs1 = attrs;
349                 while ( *attrs1 ) {
350                         sat = at_find(*attrs1);
351                         if ( !sat ) {
352                                 *err = *attrs1;
353                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
354                         }
355                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
356                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
357                                         *err = *attrs1;
358                                         return SLAP_SCHERR_OUTOFMEM;
359                                 }
360                         }
361                         attrs1++;
362                 }
363                 /* Now delete duplicates from the allowed list */
364                 for ( satp = soc->soc_required; *satp; satp++ ) {
365                         i = at_find_in_list(*satp,soc->soc_allowed);
366                         if ( i >= 0 ) {
367                                 at_delete_from_list(i, &soc->soc_allowed);
368                         }
369                 }
370         }
371         return 0;
372 }
373
374 static int
375 oc_create_allowed(
376     ObjectClass         *soc,
377     char                **attrs,
378     const char          **err
379 )
380 {
381         char            **attrs1;
382         AttributeType   *sat;
383
384         if ( attrs ) {
385                 attrs1 = attrs;
386                 while ( *attrs1 ) {
387                         sat = at_find(*attrs1);
388                         if ( !sat ) {
389                                 *err = *attrs1;
390                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
391                         }
392                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
393                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
394                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
395                                         *err = *attrs1;
396                                         return SLAP_SCHERR_OUTOFMEM;
397                                 }
398                         }
399                         attrs1++;
400                 }
401         }
402         return 0;
403 }
404
405 static int
406 oc_add_sups(
407     ObjectClass         *soc,
408     char                **sups,
409     const char          **err
410 )
411 {
412         int             code;
413         ObjectClass     *soc1;
414         int             nsups;
415         char            **sups1;
416         int             add_sups = 0;
417
418         if ( sups ) {
419                 if ( !soc->soc_sups ) {
420                         /* We are at the first recursive level */
421                         add_sups = 1;
422                         nsups = 0;
423                         sups1 = sups;
424                         while ( *sups1 ) {
425                                 nsups++;
426                                 sups1++;
427                         }
428                         nsups++;
429                         soc->soc_sups = (ObjectClass **)ch_calloc(1,
430                                           nsups*sizeof(ObjectClass *));
431                 }
432                 nsups = 0;
433                 sups1 = sups;
434                 while ( *sups1 ) {
435                         soc1 = oc_find(*sups1);
436                         if ( !soc1 ) {
437                                 *err = *sups1;
438                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
439                         }
440
441                         if ( add_sups )
442                                 soc->soc_sups[nsups] = soc1;
443
444                         code = oc_add_sups(soc,soc1->soc_sup_oids, err);
445                         if ( code )
446                                 return code;
447
448                         code = oc_create_required(soc,soc1->soc_at_oids_must,err);
449                         if ( code )
450                                 return code;
451                         code = oc_create_allowed(soc,soc1->soc_at_oids_may,err);
452                         if ( code )
453                                 return code;
454
455                         nsups++;
456                         sups1++;
457                 }
458         }
459         return 0;
460 }
461
462 static int
463 oc_insert(
464     ObjectClass         *soc,
465     const char          **err
466 )
467 {
468         ObjectClass     **ocp;
469         struct oindexrec        *oir;
470         char                    **names;
471
472         ocp = &oc_list;
473         while ( *ocp != NULL ) {
474                 ocp = &(*ocp)->soc_next;
475         }
476         *ocp = soc;
477
478         if ( soc->soc_oid ) {
479                 oir = (struct oindexrec *)
480                         ch_calloc( 1, sizeof(struct oindexrec) );
481                 oir->oir_name = soc->soc_oid;
482                 oir->oir_oc = soc;
483                 if ( avl_insert( &oc_index, (caddr_t) oir,
484                                  (AVL_CMP) oc_index_cmp,
485                                  (AVL_DUP) avl_dup_error ) ) {
486                         *err = soc->soc_oid;
487                         ldap_memfree(oir);
488                         return SLAP_SCHERR_DUP_CLASS;
489                 }
490                 /* FIX: temporal consistency check */
491                 oc_find(oir->oir_name);
492         }
493         if ( (names = soc->soc_names) ) {
494                 while ( *names ) {
495                         oir = (struct oindexrec *)
496                                 ch_calloc( 1, sizeof(struct oindexrec) );
497                         oir->oir_name = ch_strdup(*names);
498                         oir->oir_oc = soc;
499                         if ( avl_insert( &oc_index, (caddr_t) oir,
500                                          (AVL_CMP) oc_index_cmp,
501                                          (AVL_DUP) avl_dup_error ) ) {
502                                 *err = *names;
503                                 ldap_memfree(oir);
504                                 return SLAP_SCHERR_DUP_CLASS;
505                         }
506                         /* FIX: temporal consistency check */
507                         oc_find(oir->oir_name);
508                         names++;
509                 }
510         }
511         return 0;
512 }
513
514 int
515 oc_add(
516     LDAP_OBJECT_CLASS   *oc,
517     const char          **err
518 )
519 {
520         ObjectClass     *soc;
521         int             code;
522
523         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
524         memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
525         if ( (code = oc_add_sups(soc,soc->soc_sup_oids,err)) != 0 )
526                 return code;
527         if ( (code = oc_create_required(soc,soc->soc_at_oids_must,err)) != 0 )
528                 return code;
529         if ( (code = oc_create_allowed(soc,soc->soc_at_oids_may,err)) != 0 )
530                 return code;
531         code = oc_insert(soc,err);
532         return code;
533 }
534
535 struct sindexrec {
536         char            *sir_name;
537         Syntax          *sir_syn;
538 };
539
540 static Avlnode  *syn_index = NULL;
541 static Syntax *syn_list = NULL;
542
543 static int
544 syn_index_cmp(
545     struct sindexrec    *sir1,
546     struct sindexrec    *sir2
547 )
548 {
549         return (strcmp( sir1->sir_name, sir2->sir_name ));
550 }
551
552 static int
553 syn_index_name_cmp(
554     char                *name,
555     struct sindexrec    *sir
556 )
557 {
558         return (strcmp( name, sir->sir_name ));
559 }
560
561 Syntax *
562 syn_find( const char *synname )
563 {
564         struct sindexrec        *sir = NULL;
565
566         if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
567             (AVL_CMP) syn_index_name_cmp )) != NULL ) {
568                 return( sir->sir_syn );
569         }
570         return( NULL );
571 }
572
573 Syntax *
574 syn_find_desc( const char *syndesc, int *len )
575 {
576         Syntax          *synp;
577
578         for (synp = syn_list; synp; synp = synp->ssyn_next)
579                 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
580                         return synp;
581         return( NULL );
582 }
583
584 static int
585 syn_insert(
586     Syntax              *ssyn,
587     const char          **err
588 )
589 {
590         Syntax          **synp;
591         struct sindexrec        *sir;
592
593         synp = &syn_list;
594         while ( *synp != NULL ) {
595                 synp = &(*synp)->ssyn_next;
596         }
597         *synp = ssyn;
598
599         if ( ssyn->ssyn_oid ) {
600                 sir = (struct sindexrec *)
601                         ch_calloc( 1, sizeof(struct sindexrec) );
602                 sir->sir_name = ssyn->ssyn_oid;
603                 sir->sir_syn = ssyn;
604                 if ( avl_insert( &syn_index, (caddr_t) sir,
605                                  (AVL_CMP) syn_index_cmp,
606                                  (AVL_DUP) avl_dup_error ) ) {
607                         *err = ssyn->ssyn_oid;
608                         ldap_memfree(sir);
609                         return SLAP_SCHERR_DUP_SYNTAX;
610                 }
611                 /* FIX: temporal consistency check */
612                 syn_find(sir->sir_name);
613         }
614         return 0;
615 }
616
617 int
618 syn_add(
619     LDAP_SYNTAX         *syn,
620     slap_syntax_check_func      *check,
621     const char          **err
622 )
623 {
624         Syntax          *ssyn;
625         int             code;
626
627         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
628         memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
629         ssyn->ssyn_check = check;
630         code = syn_insert(ssyn,err);
631         return code;
632 }
633
634 struct mindexrec {
635         char            *mir_name;
636         MatchingRule    *mir_mr;
637 };
638
639 static Avlnode  *mr_index = NULL;
640 static MatchingRule *mr_list = NULL;
641
642 static int
643 mr_index_cmp(
644     struct mindexrec    *mir1,
645     struct mindexrec    *mir2
646 )
647 {
648         return (strcmp( mir1->mir_name, mir2->mir_name ));
649 }
650
651 static int
652 mr_index_name_cmp(
653     char                *name,
654     struct mindexrec    *mir
655 )
656 {
657         return (strcmp( name, mir->mir_name ));
658 }
659
660 MatchingRule *
661 mr_find( const char *mrname )
662 {
663         struct mindexrec        *mir = NULL;
664
665         if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
666             (AVL_CMP) mr_index_name_cmp )) != NULL ) {
667                 return( mir->mir_mr );
668         }
669         return( NULL );
670 }
671
672 static int
673 mr_insert(
674     MatchingRule        *smr,
675     const char          **err
676 )
677 {
678         MatchingRule            **mrp;
679         struct mindexrec        *mir;
680         char                    **names;
681
682         mrp = &mr_list;
683         while ( *mrp != NULL ) {
684                 mrp = &(*mrp)->smr_next;
685         }
686         *mrp = smr;
687
688         if ( smr->smr_oid ) {
689                 mir = (struct mindexrec *)
690                         ch_calloc( 1, sizeof(struct mindexrec) );
691                 mir->mir_name = smr->smr_oid;
692                 mir->mir_mr = smr;
693                 if ( avl_insert( &mr_index, (caddr_t) mir,
694                                  (AVL_CMP) mr_index_cmp,
695                                  (AVL_DUP) avl_dup_error ) ) {
696                         *err = smr->smr_oid;
697                         ldap_memfree(mir);
698                         return SLAP_SCHERR_DUP_RULE;
699                 }
700                 /* FIX: temporal consistency check */
701                 mr_find(mir->mir_name);
702         }
703         if ( (names = smr->smr_names) ) {
704                 while ( *names ) {
705                         mir = (struct mindexrec *)
706                                 ch_calloc( 1, sizeof(struct mindexrec) );
707                         mir->mir_name = ch_strdup(*names);
708                         mir->mir_mr = smr;
709                         if ( avl_insert( &mr_index, (caddr_t) mir,
710                                          (AVL_CMP) mr_index_cmp,
711                                          (AVL_DUP) avl_dup_error ) ) {
712                                 *err = *names;
713                                 ldap_memfree(mir);
714                                 return SLAP_SCHERR_DUP_RULE;
715                         }
716                         /* FIX: temporal consistency check */
717                         mr_find(mir->mir_name);
718                         names++;
719                 }
720         }
721         return 0;
722 }
723
724 int
725 mr_add(
726     LDAP_MATCHING_RULE          *mr,
727     slap_mr_normalize_func      *normalize,
728     slap_mr_compare_func        *compare,
729     const char          **err
730 )
731 {
732         MatchingRule    *smr;
733         Syntax          *syn;
734         int             code;
735
736         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
737         memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
738         smr->smr_normalize = normalize;
739         smr->smr_compare = compare;
740         if ( smr->smr_syntax_oid ) {
741                 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
742                         smr->smr_syntax = syn;
743                 } else {
744                         *err = smr->smr_syntax_oid;
745                         return SLAP_SCHERR_SYN_NOT_FOUND;
746                 }
747         } else {
748                 *err = "";
749                 return SLAP_SCHERR_MR_INCOMPLETE;
750         }
751         code = mr_insert(smr,err);
752         return code;
753 }
754
755 static int
756 case_exact_normalize(
757         struct berval *val,
758         struct berval **normalized
759 )
760 {
761         struct berval *newval;
762         char *p, *q;
763
764         newval = ber_bvdup( val );
765         p = q = newval->bv_val;
766         /* Ignore initial whitespace */
767         while ( isspace( *p++ ) )
768                 ;
769         while ( *p ) {
770                 if ( isspace( *p ) ) {
771                         *q++ = *p++;
772                         /* Ignore the extra whitespace */
773                         while ( isspace(*p++) )
774                                 ;
775                 } else {
776                         *q++ = *p++;
777                 }
778         }
779         /*
780          * If the string ended in space, backup the pointer one
781          * position.  One is enough because the above loop collapsed
782          * all whitespace to a single space.
783          */
784         if ( p != newval->bv_val && isspace( *(p-1) ) ) {
785                 *(q-1) = '\0';
786         }
787         newval->bv_len = strlen( newval->bv_val );
788         normalized = &newval;
789
790         return 0;
791 }
792
793 static int
794 case_exact_compare(
795         struct berval *val1,
796         struct berval *val2
797 )
798 {
799         return strcmp( val1->bv_val, val2->bv_val );
800 }
801
802 int
803 case_ignore_normalize(
804         struct berval *val,
805         struct berval **normalized
806 )
807 {
808         struct berval *newval;
809         char *p, *q;
810
811         newval = ber_bvdup( val );
812         p = q = newval->bv_val;
813         /* Ignore initial whitespace */
814         while ( isspace( *p++ ) )
815                 ;
816         while ( *p ) {
817                 if ( isspace( *p ) ) {
818                         *q++ = *p++;
819                         /* Ignore the extra whitespace */
820                         while ( isspace(*p++) )
821                                 ;
822                 } else {
823                         *q++ = TOUPPER( *p++ );
824                 }
825         }
826         /*
827          * If the string ended in space, backup the pointer one
828          * position.  One is enough because the above loop collapsed
829          * all whitespace to a single space.
830          */
831         if ( p != newval->bv_val && isspace( *(p-1) ) ) {
832                 *(q-1) = '\0';
833         }
834         newval->bv_len = strlen( newval->bv_val );
835         normalized = &newval;
836
837         return 0;
838 }
839
840 static int
841 case_ignore_compare(
842         struct berval *val1,
843         struct berval *val2
844 )
845 {
846         return strcasecmp( val1->bv_val, val2->bv_val );
847 }
848
849 int
850 register_syntax(
851         char * desc,
852         slap_syntax_check_func *check )
853 {
854         LDAP_SYNTAX     *syn;
855         int             code;
856         const char      *err;
857
858         syn = ldap_str2syntax( desc, &code, &err);
859         if ( !syn ) {
860                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
861                     ldap_scherr2str(code), err, desc );
862                 return( -1 );
863         }
864         code = syn_add( syn, check, &err );
865         if ( code ) {
866                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
867                     scherr2str(code), err, desc );
868                 return( -1 );
869         }
870         return( 0 );
871 }
872
873 int
874 register_matching_rule(
875         char * desc,
876         slap_mr_normalize_func *normalize,
877         slap_mr_compare_func *compare )
878 {
879         LDAP_MATCHING_RULE *mr;
880         int             code;
881         const char      *err;
882
883         mr = ldap_str2matchingrule( desc, &code, &err);
884         if ( !mr ) {
885                 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
886                     ldap_scherr2str(code), err, desc );
887                 return( -1 );
888         }
889         code = mr_add( mr, normalize, compare, &err );
890         if ( code ) {
891                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
892                     scherr2str(code), err, desc );
893                 return( -1 );
894         }
895         return( 0 );
896 }
897
898 struct syntax_defs_rec {
899         char *sd_desc;
900         slap_syntax_check_func *sd_check;
901 };
902
903 struct syntax_defs_rec syntax_defs[] = {
904         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'AttributeTypeDescription' )", NULL},
905         {"( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' )", NULL},
906         {"( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' )", NULL},
907         {"( 1.3.6.1.4.1.1466.115.121.1.6 DESC 'BitString' )", NULL},
908         {"( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' )", NULL},
909         {"( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'CertificateList' )", NULL},
910         {"( 1.3.6.1.4.1.1466.115.121.1.10 DESC 'CertificatePair' )", NULL},
911         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'DN' )", NULL},
912         {"( 1.3.6.1.4.1.1466.115.121.1.14 DESC 'DeliveryMethod' )", NULL},
913         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'DirectoryString' )", NULL},
914         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DITContentRuleDescription' )", NULL},
915         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DITStructureRuleDescription' )", NULL},
916         {"( 1.3.6.1.4.1.1466.115.121.1.21 DESC 'EnhancedGuide' )", NULL},
917         {"( 1.3.6.1.4.1.1466.115.121.1.22 DESC 'FacsimileTelephoneNumber' )", NULL},
918         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'GeneralizedTime' )", NULL},
919         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )", NULL},
920         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5String' )", NULL},
921         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'Integer' )", NULL},
922         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'MatchingRuleDescription' )", NULL},
923         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'MatchingRuleUseDescription' )", NULL},
924         {"( 1.3.6.1.4.1.1466.115.121.1.32 DESC 'MailPreference' )", NULL},
925         {"( 1.3.6.1.4.1.1466.115.121.1.34 DESC 'NameAndOptionalUID' )", NULL},
926         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'NameFormDescription' )", NULL},
927         {"( 1.3.6.1.4.1.1466.115.121.1.36 DESC 'NumericString' )", NULL},
928         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'ObjectClassDescription' )", NULL},
929         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )", NULL},
930         {"( 1.3.6.1.4.1.1466.115.121.1.39 DESC 'OtherMailbox' )", NULL},
931         {"( 1.3.6.1.4.1.1466.115.121.1.40 DESC 'OctetString' )", NULL},
932         {"( 1.3.6.1.4.1.1466.115.121.1.41 DESC 'PostalAddress' )", NULL},
933         {"( 1.3.6.1.4.1.1466.115.121.1.42 DESC 'ProtocolInformation' )", NULL},
934         {"( 1.3.6.1.4.1.1466.115.121.1.43 DESC 'PresentationAddress' )", NULL},
935         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'PrintableString' )", NULL},
936         {"( 1.3.6.1.4.1.1466.115.121.1.49 DESC 'SupportedAlgorithm' )", NULL},
937         {"( 1.3.6.1.4.1.1466.115.121.1.50 DESC 'TelephoneNumber' )", NULL},
938         {"( 1.3.6.1.4.1.1466.115.121.1.51 DESC 'TeletexTerminalIdentifier' )", NULL},
939         {"( 1.3.6.1.4.1.1466.115.121.1.52 DESC 'TelexNumber' )", NULL},
940         {"( 1.3.6.1.4.1.1466.115.121.1.53 DESC 'UTCTime' )", NULL},
941         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAPSyntaxDescription' )", NULL},
942         {"( 1.3.6.1.4.1.1466.115.121.1.58 DESC 'SubstringAssertion' )", NULL},
943         {"( 1.3.6.1.1.1.0.0 DESC 'NISnetgrouptriple' )", NULL},
944         {"( 1.3.6.1.1.1.0.1 DESC 'Bootparameter' )", NULL},
945         {NULL, NULL}
946 };
947
948 struct mrule_defs_rec {
949         char *mrd_desc;
950         slap_mr_normalize_func *mrd_normalize;
951         slap_mr_compare_func *mrd_compare;
952 };
953
954 /*
955  * Other matching rules in X.520 that we do not use:
956  *
957  * 2.5.13.9     numericStringOrderingMatch
958  * 2.5.13.12    caseIgnoreListSubstringsMatch
959  * 2.5.13.13    booleanMatch
960  * 2.5.13.15    integerOrderingMatch
961  * 2.5.13.18    octetStringOrderingMatch
962  * 2.5.13.19    octetStringSubstringsMatch
963  * 2.5.13.25    uTCTimeMatch
964  * 2.5.13.26    uTCTimeOrderingMatch
965  * 2.5.13.31    directoryStringFirstComponentMatch
966  * 2.5.13.32    wordMatch
967  * 2.5.13.33    keywordMatch
968  * 2.5.13.34    certificateExactMatch
969  * 2.5.13.35    certificateMatch
970  * 2.5.13.36    certificatePairExactMatch
971  * 2.5.13.37    certificatePairMatch
972  * 2.5.13.38    certificateListExactMatch
973  * 2.5.13.39    certificateListMatch
974  * 2.5.13.40    algorithmIdentifierMatch
975  * 2.5.13.41    storedPrefixMatch
976  * 2.5.13.42    attributeCertificateMatch
977  * 2.5.13.43    readerAndKeyIDMatch
978  * 2.5.13.44    attributeIntegrityMatch
979  */
980
981 struct mrule_defs_rec mrule_defs[] = {
982         {"( 2.5.13.0 NAME 'objectIdentifierMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )", NULL, NULL},
983         {"( 2.5.13.1 NAME 'distinguishedNameMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )", NULL, NULL},
984         {"( 2.5.13.2 NAME 'caseIgnoreMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
985          case_ignore_normalize, case_ignore_compare},
986         {"( 2.5.13.3 NAME 'caseIgnoreOrderingMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
987          case_ignore_normalize, case_ignore_compare},
988         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
989          case_ignore_normalize, case_ignore_compare},
990         /* Next three are not in the RFC's, but are needed for compatibility */
991         {"( 2.5.13.5 NAME 'caseExactMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
992          case_exact_normalize, case_exact_compare},
993         {"( 2.5.13.6 NAME 'caseExactOrderingMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
994          case_exact_normalize, case_exact_compare},
995         {"( 2.5.13.7 NAME 'caseExactSubstringsMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )",
996          case_exact_normalize, case_exact_compare},
997         {"( 2.5.13.8 NAME 'numericStringMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )", NULL, NULL},
998         {"( 2.5.13.10 NAME 'numericStringSubstringsMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )", NULL, NULL},
999         {"( 2.5.13.11 NAME 'caseIgnoreListMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.41 )", NULL, NULL},
1000         {"( 2.5.13.14 NAME 'integerMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )", NULL, NULL},
1001         {"( 2.5.13.16 NAME 'bitStringMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.6 )", NULL, NULL},
1002         {"( 2.5.13.17 NAME 'octetStringMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )", NULL, NULL},
1003         {"( 2.5.13.20 NAME 'telephoneNumberMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.50 )", NULL, NULL},
1004         {"( 2.5.13.21 NAME 'telephoneNumberSubstringsMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )", NULL, NULL},
1005         {"( 2.5.13.22 NAME 'presentationAddressMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.43 )", NULL, NULL},
1006         {"( 2.5.13.23 NAME 'uniqueMemberMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.34 )", NULL, NULL},
1007         {"( 2.5.13.24 NAME 'protocolInformationMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.42 )", NULL, NULL},
1008         {"( 2.5.13.27 NAME 'generalizedTimeMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )", NULL, NULL},
1009         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )", NULL, NULL},
1010         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )", NULL, NULL},
1011         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )", NULL, NULL},
1012         {"( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1013          case_exact_normalize, case_exact_compare},
1014         {"( 1.3.6.1.4.1.1466.109.114.2 NAME 'caseIgnoreIA5Match' SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
1015          case_ignore_normalize, case_ignore_compare},
1016         {NULL, NULL, NULL}
1017 };
1018
1019 int
1020 schema_init( void )
1021 {
1022         int             res;
1023         int             i;
1024         static int      schema_init_done = 0;
1025
1026         /* We are called from read_config that is recursive */
1027         if ( schema_init_done )
1028                 return( 0 );
1029         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
1030                 res = register_syntax( syntax_defs[i].sd_desc,
1031                     syntax_defs[i].sd_check );
1032                 if ( res ) {
1033                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
1034                                  syntax_defs[i].sd_desc );
1035                         exit( EXIT_FAILURE );
1036                 }
1037         }
1038         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
1039                 res = register_matching_rule( mrule_defs[i].mrd_desc,
1040                     ( mrule_defs[i].mrd_normalize ?
1041                       mrule_defs[i].mrd_normalize : case_ignore_normalize ),
1042                     ( mrule_defs[i].mrd_compare ?
1043                       mrule_defs[i].mrd_compare : case_ignore_compare ) );
1044                 if ( res ) {
1045                         fprintf( stderr, "schema_init: Error registering matching rule %s\n",
1046                                  mrule_defs[i].mrd_desc );
1047                         exit( EXIT_FAILURE );
1048                 }
1049         }
1050         schema_init_done = 1;
1051         return( 0 );
1052 }
1053
1054 #if defined( SLAPD_SCHEMA_DN )
1055
1056 static int
1057 syn_schema_info( Entry *e )
1058 {
1059         struct berval   val;
1060         struct berval   *vals[2];
1061         Syntax          *syn;
1062
1063         vals[0] = &val;
1064         vals[1] = NULL;
1065
1066         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
1067                 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
1068                 if ( val.bv_val ) {
1069                         val.bv_len = strlen( val.bv_val );
1070                         Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
1071                                (long) val.bv_len, val.bv_val, 0 );
1072                         attr_merge( e, "ldapSyntaxes", vals );
1073                         ldap_memfree( val.bv_val );
1074                 } else {
1075                         return -1;
1076                 }
1077         }
1078         return 0;
1079 }
1080
1081 static int
1082 mr_schema_info( Entry *e )
1083 {
1084         struct berval   val;
1085         struct berval   *vals[2];
1086         MatchingRule    *mr;
1087
1088         vals[0] = &val;
1089         vals[1] = NULL;
1090
1091         for ( mr = mr_list; mr; mr = mr->smr_next ) {
1092                 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
1093                 if ( val.bv_val ) {
1094                         val.bv_len = strlen( val.bv_val );
1095                         Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
1096                                (long) val.bv_len, val.bv_val, 0 );
1097                         attr_merge( e, "matchingRules", vals );
1098                         ldap_memfree( val.bv_val );
1099                 } else {
1100                         return -1;
1101                 }
1102         }
1103         return 0;
1104 }
1105
1106 static int
1107 oc_schema_info( Entry *e )
1108 {
1109         struct berval   val;
1110         struct berval   *vals[2];
1111         ObjectClass     *oc;
1112
1113         vals[0] = &val;
1114         vals[1] = NULL;
1115
1116         for ( oc = oc_list; oc; oc = oc->soc_next ) {
1117                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
1118                 if ( val.bv_val ) {
1119                         val.bv_len = strlen( val.bv_val );
1120                         Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
1121                                (long) val.bv_len, val.bv_val, 0 );
1122                         attr_merge( e, "objectClasses", vals );
1123                         ldap_memfree( val.bv_val );
1124                 } else {
1125                         return -1;
1126                 }
1127         }
1128         return 0;
1129 }
1130
1131 void
1132 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
1133 {
1134         Entry           *e;
1135         struct berval   val;
1136         struct berval   *vals[2];
1137
1138         vals[0] = &val;
1139         vals[1] = NULL;
1140
1141         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
1142
1143         e->e_attrs = NULL;
1144         e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
1145         e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
1146         (void) dn_normalize_case( e->e_ndn );
1147         e->e_private = NULL;
1148
1149         {
1150                 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
1151                 val.bv_val = strchr( rdn, '=' );
1152
1153                 if( val.bv_val != NULL ) {
1154                         *val.bv_val = '\0';
1155                         val.bv_len = strlen( ++val.bv_val );
1156
1157                         attr_merge( e, rdn, vals );
1158                 }
1159
1160                 free( rdn );
1161         }
1162
1163         if ( syn_schema_info( e ) ) {
1164                 /* Out of memory, do something about it */
1165                 entry_free( e );
1166                 return;
1167         }
1168         if ( mr_schema_info( e ) ) {
1169                 /* Out of memory, do something about it */
1170                 entry_free( e );
1171                 return;
1172         }
1173         if ( at_schema_info( e ) ) {
1174                 /* Out of memory, do something about it */
1175                 entry_free( e );
1176                 return;
1177         }
1178         if ( oc_schema_info( e ) ) {
1179                 /* Out of memory, do something about it */
1180                 entry_free( e );
1181                 return;
1182         }
1183         
1184         val.bv_val = "top";
1185         val.bv_len = sizeof("top")-1;
1186         attr_merge( e, "objectClass", vals );
1187
1188         val.bv_val = "LDAPsubentry";
1189         val.bv_len = sizeof("LDAPsubentry")-1;
1190         attr_merge( e, "objectClass", vals );
1191
1192         val.bv_val = "subschema";
1193         val.bv_len = sizeof("subschema")-1;
1194         attr_merge( e, "objectClass", vals );
1195
1196         val.bv_val = "extensibleObject";
1197         val.bv_len = sizeof("extensibleObject")-1;
1198         attr_merge( e, "objectClass", vals );
1199
1200         send_search_entry( &backends[0], conn, op,
1201                 e, attrs, attrsonly, NULL );
1202         send_search_result( conn, op, LDAP_SUCCESS,
1203                 NULL, NULL, NULL, NULL, 1 );
1204
1205         entry_free( e );
1206 }
1207 #endif
1208
1209 #ifdef LDAP_DEBUG
1210
1211 static void
1212 oc_print( ObjectClass *oc )
1213 {
1214         int     i;
1215         const char *mid;
1216
1217         printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1218         if ( oc->soc_required != NULL ) {
1219                 mid = "\trequires ";
1220                 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1221                         printf( "%s%s", mid,
1222                                 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1223                 printf( "\n" );
1224         }
1225         if ( oc->soc_allowed != NULL ) {
1226                 mid = "\tallows ";
1227                 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1228                         printf( "%s%s", mid,
1229                                 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1230                 printf( "\n" );
1231         }
1232 }
1233
1234 #endif
1235
1236
1237 int is_entry_objectclass(
1238         Entry*  e,
1239         const char*     oc)
1240 {
1241         Attribute *attr;
1242         struct berval bv;
1243
1244         if( e == NULL || oc == NULL || *oc == '\0' )
1245                 return 0;
1246
1247         /*
1248          * find objectClass attribute
1249          */
1250         attr = attr_find(e->e_attrs, "objectclass");
1251
1252         if( attr == NULL ) {
1253                 /* no objectClass attribute */
1254                 return 0;
1255         }
1256
1257         bv.bv_val = (char *) oc;
1258         bv.bv_len = strlen( bv.bv_val );
1259
1260         if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1261                 /* entry is not of this objectclass */
1262                 return 0;
1263         }
1264
1265         return 1;
1266 }