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