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