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