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