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