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