]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
validate object class before checking required and allowed
[openldap] / servers / slapd / schema.c
1 /* schema.c - routines to enforce schema definitions */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "ldap_defaults.h"
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                         ret = 1;
50                 }
51                 else
52                 {
53                         char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
54
55                         if (s != NULL) {
56                                 Debug( LDAP_DEBUG_ANY,
57                                         "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
58                                         e->e_dn, aoc->a_vals[i]->bv_val, s );
59                                 ret = 1;
60                         }
61                 }
62         }
63
64         if ( ret != 0 ) {
65             return( ret );
66         }
67
68         /* check that each attr in the entry is allowed by some oc */
69         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
70                 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
71                         Debug( LDAP_DEBUG_ANY,
72                             "Entry (%s), attr \"%s\" not allowed\n",
73                             e->e_dn, a->a_type, 0 );
74                         ret = 1;
75                 }
76         }
77
78         return( ret );
79 }
80
81 static char *
82 oc_check_required( Entry *e, char *ocname )
83 {
84         ObjectClass     *oc;
85         AttributeType   *at;
86         int             i;
87         Attribute       *a;
88         char            **pp;
89
90         Debug( LDAP_DEBUG_TRACE,
91                "oc_check_required entry (%s), objectclass \"%s\"\n",
92                e->e_dn, ocname, 0 );
93
94         /* find global oc defn. it we don't know about it assume it's ok */
95         if ( (oc = oc_find( ocname )) == NULL ) {
96                 return( 0 );
97         }
98
99         /* check for empty oc_required */
100         if(oc->soc_required == NULL) {
101                 return( 0 );
102         }
103
104         /* for each required attribute */
105         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
106                 at = oc->soc_required[i];
107                 /* see if it's in the entry */
108                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
109                         if ( at->sat_oid &&
110                              strcmp( a->a_type, at->sat_oid ) == 0 ) {
111                                 break;
112                         }
113                         pp = at->sat_names;
114                         if ( pp  == NULL ) {
115                                 /* Empty name list => not found */
116                                 a = NULL;
117                                 break;
118                         }
119                         while ( *pp ) {
120                                 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
121                                         break;
122                                 }
123                                 pp++;
124                         }
125                         if ( *pp ) {
126                                 break;
127                         }
128                 }
129                 /* not there => schema violation */
130                 if ( a == NULL ) {
131                         if ( at->sat_names && at->sat_names[0] ) {
132                                 return at->sat_names[0];
133                         } else {
134                                 return at->sat_oid;
135                         }
136                 }
137         }
138
139         return( NULL );
140 }
141
142 static char *oc_usermod_attrs[] = {
143         /*
144          * OpenLDAP doesn't support any user modification of
145          * operational attributes.
146          */
147         NULL
148 };
149
150 static char *oc_operational_attrs[] = {
151         /*
152          * these are operational attributes 
153          * most could be user modifiable
154          */
155         "objectClasses",
156         "attributeTypes",
157         "matchingRules",
158         "matchingRuleUse",
159         "dITStructureRules",
160         "dITContentRules",
161         "nameForms",
162         "ldapSyntaxes",
163         "namingContexts",
164         "supportedExtension",
165         "supportedControl",
166         "supportedSASLMechanisms",
167         "supportedLDAPversion",
168         "subschemaSubentry",            /* NO USER MOD */
169         NULL
170
171 };
172
173 /* this list should be extensible  */
174 static char *oc_no_usermod_attrs[] = {
175         /*
176          * Operational and 'no user modification' attributes
177          * which are STORED in the directory server.
178          */
179
180         /* RFC2252, 3.2.1 */
181         "creatorsName",
182         "createTimestamp",
183         "modifiersName",
184         "modifyTimestamp",
185
186         NULL
187 };
188
189
190 /*
191  * check to see if attribute is 'operational' or not.
192  */
193 int
194 oc_check_operational_attr( const char *type )
195 {
196         return charray_inlist( oc_operational_attrs, type )
197                 || charray_inlist( oc_usermod_attrs, type )
198                 || charray_inlist( oc_no_usermod_attrs, type );
199 }
200
201 /*
202  * check to see if attribute can be user modified or not.
203  */
204 int
205 oc_check_usermod_attr( const char *type )
206 {
207         return charray_inlist( oc_usermod_attrs, type );
208 }
209
210 /*
211  * check to see if attribute is 'no user modification' or not.
212  */
213 int
214 oc_check_no_usermod_attr( const char *type )
215 {
216         return charray_inlist( oc_no_usermod_attrs, type );
217 }
218
219
220 static int
221 oc_check_allowed( char *type, struct berval **ocl )
222 {
223         ObjectClass     *oc;
224         AttributeType   *at;
225         int             i, j;
226         char            **pp;
227
228         Debug( LDAP_DEBUG_TRACE,
229                "oc_check_allowed type \"%s\"\n", type, 0, 0 );
230
231         /* always allow objectclass attribute */
232         if ( strcasecmp( type, "objectclass" ) == 0 ) {
233                 return( 0 );
234         }
235
236         /*
237          * All operational attributions are allowed by schema rules.
238          * However, we only check attributions which are stored in the
239          * the directory regardless if they are user or non-user modified.
240          */
241         if ( oc_check_usermod_attr( type ) || oc_check_no_usermod_attr( type ) ) {
242                 return( 0 );
243         }
244
245         /* check that the type appears as req or opt in at least one oc */
246         for ( i = 0; ocl[i] != NULL; i++ ) {
247                 /* if we know about the oc */
248                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
249                         /* does it require the type? */
250                         for ( j = 0; oc->soc_required != NULL && 
251                                 oc->soc_required[j] != NULL; j++ ) {
252                                 at = oc->soc_required[j];
253                                 if ( at->sat_oid &&
254                                      strcmp(at->sat_oid, type ) == 0 ) {
255                                         return( 0 );
256                                 }
257                                 pp = at->sat_names;
258                                 if ( pp == NULL )
259                                         continue;
260                                 while ( *pp ) {
261                                         if ( strcasecmp( *pp, type ) == 0 ) {
262                                                 return( 0 );
263                                         }
264                                         pp++;
265                                 }
266                         }
267                         /* does it allow the type? */
268                         for ( j = 0; oc->soc_allowed != NULL && 
269                                 oc->soc_allowed[j] != NULL; j++ ) {
270                                 at = oc->soc_allowed[j];
271                                 if ( at->sat_oid &&
272                                      strcmp(at->sat_oid, type ) == 0 ) {
273                                         return( 0 );
274                                 }
275                                 pp = at->sat_names;
276                                 if ( pp == NULL )
277                                         continue;
278                                 while ( *pp ) {
279                                         if ( strcasecmp( *pp, type ) == 0 ||
280                                              strcmp( *pp, "*" ) == 0 ) {
281                                                 return( 0 );
282                                         }
283                                         pp++;
284                                 }
285                         }
286                         /* maybe the next oc allows it */
287
288                 /* we don't know about the oc. assume it allows it */
289                 } else {
290                         return( 0 );
291                 }
292         }
293
294         /* not allowed by any oc */
295         return( 1 );
296 }
297
298 struct oindexrec {
299         char            *oir_name;
300         ObjectClass     *oir_oc;
301 };
302
303 static Avlnode  *oc_index = NULL;
304 static ObjectClass *oc_list = NULL;
305
306 static int
307 oc_index_cmp(
308     struct oindexrec    *oir1,
309     struct oindexrec    *oir2
310 )
311 {
312         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
313 }
314
315 static int
316 oc_index_name_cmp(
317     char                *name,
318     struct oindexrec    *oir
319 )
320 {
321         return (strcasecmp( name, oir->oir_name ));
322 }
323
324 ObjectClass *
325 oc_find( const char *ocname )
326 {
327         struct oindexrec        *oir = NULL;
328
329         if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
330             (AVL_CMP) oc_index_name_cmp )) != NULL ) {
331                 return( oir->oir_oc );
332         }
333         return( NULL );
334 }
335
336 static int
337 oc_create_required(
338     ObjectClass         *soc,
339     char                **attrs,
340     const char          **err
341 )
342 {
343         char            **attrs1;
344         AttributeType   *sat;
345         AttributeType   **satp;
346         int             i;
347
348         if ( attrs ) {
349                 attrs1 = attrs;
350                 while ( *attrs1 ) {
351                         sat = at_find(*attrs1);
352                         if ( !sat ) {
353                                 *err = *attrs1;
354                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
355                         }
356                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
357                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
358                                         *err = *attrs1;
359                                         return SLAP_SCHERR_OUTOFMEM;
360                                 }
361                         }
362                         attrs1++;
363                 }
364                 /* Now delete duplicates from the allowed list */
365                 for ( satp = soc->soc_required; *satp; satp++ ) {
366                         i = at_find_in_list(*satp,soc->soc_allowed);
367                         if ( i >= 0 ) {
368                                 at_delete_from_list(i, &soc->soc_allowed);
369                         }
370                 }
371         }
372         return 0;
373 }
374
375 static int
376 oc_create_allowed(
377     ObjectClass         *soc,
378     char                **attrs,
379     const char          **err
380 )
381 {
382         char            **attrs1;
383         AttributeType   *sat;
384
385         if ( attrs ) {
386                 attrs1 = attrs;
387                 while ( *attrs1 ) {
388                         sat = at_find(*attrs1);
389                         if ( !sat ) {
390                                 *err = *attrs1;
391                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
392                         }
393                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
394                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
395                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
396                                         *err = *attrs1;
397                                         return SLAP_SCHERR_OUTOFMEM;
398                                 }
399                         }
400                         attrs1++;
401                 }
402         }
403         return 0;
404 }
405
406 static int
407 oc_add_sups(
408     ObjectClass         *soc,
409     char                **sups,
410     const char          **err
411 )
412 {
413         int             code;
414         ObjectClass     *soc1;
415         int             nsups;
416         char            **sups1;
417         int             add_sups = 0;
418
419         if ( sups ) {
420                 if ( !soc->soc_sups ) {
421                         /* We are at the first recursive level */
422                         add_sups = 1;
423                         nsups = 0;
424                         sups1 = sups;
425                         while ( *sups1 ) {
426                                 nsups++;
427                                 sups1++;
428                         }
429                         nsups++;
430                         soc->soc_sups = (ObjectClass **)ch_calloc(1,
431                                           nsups*sizeof(ObjectClass *));
432                 }
433                 nsups = 0;
434                 sups1 = sups;
435                 while ( *sups1 ) {
436                         soc1 = oc_find(*sups1);
437                         if ( !soc1 ) {
438                                 *err = *sups1;
439                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
440                         }
441
442                         if ( add_sups )
443                                 soc->soc_sups[nsups] = soc1;
444
445                         code = oc_add_sups(soc,soc1->soc_sup_oids, err);
446                         if ( code )
447                                 return code;
448                         
449                         if ( code = oc_create_required(soc,
450                                 soc1->soc_at_oids_must,err) )
451                                 return code;
452                         if ( code = oc_create_allowed(soc,
453                                 soc1->soc_at_oids_may,err) )
454                                 return code;
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) )
526                 return code;
527         if ( code = oc_create_required(soc,soc->soc_at_oids_must,err) )
528                 return code;
529         if ( code = oc_create_allowed(soc,soc->soc_at_oids_may,err) )
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 [%d] %s\n",
1071                                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 [%d] %s\n",
1096                                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 [%d] %s\n",
1121                                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 }