]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
e9eee40396c07ac753bd4dec5a4affc2ab8526e0
[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/string.h>
8 #include <ac/socket.h>
9
10 #include "ldapconfig.h"
11 #include "slap.h"
12
13 static char *   oc_check_required(Entry *e, char *ocname);
14 static int              oc_check_allowed(char *type, struct berval **ocl);
15
16 /*
17  * oc_check - check that entry e conforms to the schema required by
18  * its object class(es). returns 0 if so, non-zero otherwise.
19  */
20
21 int
22 oc_schema_check( Entry *e )
23 {
24         Attribute       *a, *aoc;
25         int             i;
26         int             ret = 0;
27
28         /* find the object class attribute - could error out here */
29         if ( (aoc = attr_find( e->e_attrs, "objectclass" )) == NULL ) {
30                 Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
31                     e->e_dn, 0, 0 );
32                 return( 0 );
33         }
34
35         /* check that the entry has required attrs for each oc */
36         for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
37                 char *s = oc_check_required( e, aoc->a_vals[i]->bv_val );
38
39                 if (s != NULL) {
40                         Debug( LDAP_DEBUG_ANY,
41                             "Entry (%s), oc \"%s\" requires attr \"%s\"\n",
42                             e->e_dn, aoc->a_vals[i]->bv_val, s );
43                         ret = 1;
44                 }
45         }
46
47         if ( ret != 0 ) {
48             return( ret );
49         }
50
51         /* check that each attr in the entry is allowed by some oc */
52         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
53                 if ( oc_check_allowed( a->a_type, aoc->a_vals ) != 0 ) {
54                         Debug( LDAP_DEBUG_ANY,
55                             "Entry (%s), attr \"%s\" not allowed\n",
56                             e->e_dn, a->a_type, 0 );
57                         ret = 1;
58                 }
59         }
60
61         return( ret );
62 }
63
64 static char *
65 oc_check_required( Entry *e, char *ocname )
66 {
67         ObjectClass     *oc;
68         AttributeType   *at;
69         int             i;
70         Attribute       *a;
71         char            **pp;
72
73         Debug( LDAP_DEBUG_TRACE,
74                "oc_check_required entry (%s), objectclass \"%s\"\n",
75                e->e_dn, ocname, 0 );
76
77         /* find global oc defn. it we don't know about it assume it's ok */
78         if ( (oc = oc_find( ocname )) == NULL ) {
79                 return( 0 );
80         }
81
82         /* check for empty oc_required */
83         if(oc->soc_required == NULL) {
84                 return( 0 );
85         }
86
87         /* for each required attribute */
88         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
89                 at = oc->soc_required[i];
90                 /* see if it's in the entry */
91                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
92                         if ( at->sat_oid &&
93                              strcmp( a->a_type, at->sat_oid ) == 0 ) {
94                                 break;
95                         }
96                         pp = at->sat_names;
97                         if ( pp  == NULL ) {
98                                 /* Empty name list => not found */
99                                 a = NULL;
100                                 break;
101                         }
102                         while ( *pp ) {
103                                 if ( strcasecmp( a->a_type, *pp ) == 0 ) {
104                                         break;
105                                 }
106                                 pp++;
107                         }
108                         if ( *pp ) {
109                                 break;
110                         }
111                 }
112                 /* not there => schema violation */
113                 if ( a == NULL ) {
114                         if ( at->sat_names && at->sat_names[0] ) {
115                                 return at->sat_names[0];
116                         } else {
117                                 return at->sat_oid;
118                         }
119                 }
120         }
121
122         return( NULL );
123 }
124
125 /*
126  * check to see if attribute is 'operational' or not.
127  * this function should be externalized...
128  */
129 static int
130 oc_check_operational( char *type )
131 {
132         return ( strcasecmp( type, "modifiersname" ) == 0 ||
133                 strcasecmp( type, "modifytimestamp" ) == 0 ||
134                 strcasecmp( type, "creatorsname" ) == 0 ||
135                 strcasecmp( type, "createtimestamp" ) == 0 )
136                 ? 1 : 0;
137 }
138
139 static int
140 oc_check_allowed( char *type, struct berval **ocl )
141 {
142         ObjectClass     *oc;
143         AttributeType   *at;
144         int             i, j;
145         char            **pp;
146
147         Debug( LDAP_DEBUG_TRACE,
148                "oc_check_allowed type \"%s\"\n", type, 0, 0 );
149
150         /* always allow objectclass attribute */
151         if ( strcasecmp( type, "objectclass" ) == 0 ) {
152                 return( 0 );
153         }
154
155         if ( oc_check_operational( type ) ) {
156                 return( 0 );
157         }
158
159         /* check that the type appears as req or opt in at least one oc */
160         for ( i = 0; ocl[i] != NULL; i++ ) {
161                 /* if we know about the oc */
162                 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
163                         /* does it require the type? */
164                         for ( j = 0; oc->soc_required != NULL && 
165                                 oc->soc_required[j] != NULL; j++ ) {
166                                 at = oc->soc_required[j];
167                                 if ( at->sat_oid &&
168                                      strcmp(at->sat_oid, type ) == 0 ) {
169                                         return( 0 );
170                                 }
171                                 pp = at->sat_names;
172                                 if ( pp == NULL )
173                                         continue;
174                                 while ( *pp ) {
175                                         if ( strcasecmp( *pp, type ) == 0 ) {
176                                                 return( 0 );
177                                         }
178                                         pp++;
179                                 }
180                         }
181                         /* does it allow the type? */
182                         for ( j = 0; oc->soc_allowed != NULL && 
183                                 oc->soc_allowed[j] != NULL; j++ ) {
184                                 at = oc->soc_allowed[j];
185                                 if ( at->sat_oid &&
186                                      strcmp(at->sat_oid, type ) == 0 ) {
187                                         return( 0 );
188                                 }
189                                 pp = at->sat_names;
190                                 if ( pp == NULL )
191                                         continue;
192                                 while ( *pp ) {
193                                         if ( strcasecmp( *pp, type ) == 0 ||
194                                              strcmp( *pp, "*" ) == 0 ) {
195                                                 return( 0 );
196                                         }
197                                         pp++;
198                                 }
199                         }
200                         /* maybe the next oc allows it */
201
202                 /* we don't know about the oc. assume it allows it */
203                 } else {
204                         return( 0 );
205                 }
206         }
207
208         /* not allowed by any oc */
209         return( 1 );
210 }
211
212 struct oindexrec {
213         char            *oir_name;
214         ObjectClass     *oir_oc;
215 };
216
217 static Avlnode  *oc_index = NULL;
218 static ObjectClass *oc_list = NULL;
219
220 static int
221 oc_index_cmp(
222     struct oindexrec    *oir1,
223     struct oindexrec    *oir2
224 )
225 {
226         return (strcasecmp( oir1->oir_name, oir2->oir_name ));
227 }
228
229 static int
230 oc_index_name_cmp(
231     char                *name,
232     struct oindexrec    *oir
233 )
234 {
235         return (strcasecmp( name, oir->oir_name ));
236 }
237
238 ObjectClass *
239 oc_find( const char *ocname )
240 {
241         struct oindexrec        *oir = NULL;
242
243         if ( (oir = (struct oindexrec *) avl_find( oc_index, ocname,
244             (AVL_CMP) oc_index_name_cmp )) != NULL ) {
245                 return( oir->oir_oc );
246         }
247         return( NULL );
248 }
249
250 static int
251 oc_create_required(
252     ObjectClass         *soc,
253     char                **attrs,
254     const char          **err
255 )
256 {
257         char            **attrs1;
258         AttributeType   *sat;
259         AttributeType   **satp;
260         int             i;
261
262         if ( attrs ) {
263                 attrs1 = attrs;
264                 while ( *attrs1 ) {
265                         sat = at_find(*attrs1);
266                         if ( !sat ) {
267                                 *err = *attrs1;
268                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
269                         }
270                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
271                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
272                                         *err = *attrs1;
273                                         return SLAP_SCHERR_OUTOFMEM;
274                                 }
275                         }
276                         attrs1++;
277                 }
278                 /* Now delete duplicates from the allowed list */
279                 for ( satp = soc->soc_required; *satp; satp++ ) {
280                         i = at_find_in_list(*satp,soc->soc_allowed);
281                         if ( i >= 0 ) {
282                                 at_delete_from_list(i, &soc->soc_allowed);
283                         }
284                 }
285         }
286         return 0;
287 }
288
289 static int
290 oc_create_allowed(
291     ObjectClass         *soc,
292     char                **attrs,
293     const char          **err
294 )
295 {
296         char            **attrs1;
297         AttributeType   *sat;
298
299         if ( attrs ) {
300                 attrs1 = attrs;
301                 while ( *attrs1 ) {
302                         sat = at_find(*attrs1);
303                         if ( !sat ) {
304                                 *err = *attrs1;
305                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
306                         }
307                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
308                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
309                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
310                                         *err = *attrs1;
311                                         return SLAP_SCHERR_OUTOFMEM;
312                                 }
313                         }
314                         attrs1++;
315                 }
316         }
317         return 0;
318 }
319
320 static int
321 oc_add_sups(
322     ObjectClass         *soc,
323     char                **sups,
324     const char          **err
325 )
326 {
327         int             code;
328         ObjectClass     *soc1;
329         int             nsups;
330         char            **sups1;
331         int             add_sups = 0;
332
333         if ( sups ) {
334                 if ( !soc->soc_sups ) {
335                         /* We are at the first recursive level */
336                         add_sups = 1;
337                         nsups = 0;
338                         sups1 = sups;
339                         while ( *sups1 ) {
340                                 nsups++;
341                                 sups1++;
342                         }
343                         nsups++;
344                         soc->soc_sups = (ObjectClass **)ch_calloc(1,
345                                           nsups*sizeof(ObjectClass *));
346                 }
347                 nsups = 0;
348                 sups1 = sups;
349                 while ( *sups1 ) {
350                         soc1 = oc_find(*sups1);
351                         if ( !soc1 ) {
352                                 *err = *sups1;
353                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
354                         }
355
356                         if ( add_sups )
357                                 soc->soc_sups[nsups] = soc1;
358
359                         code = oc_add_sups(soc,soc1->soc_sup_oids, err);
360                         if ( code )
361                                 return code;
362                         
363                         if ( code = oc_create_required(soc,
364                                 soc1->soc_at_oids_must,err) )
365                                 return code;
366                         if ( code = oc_create_allowed(soc,
367                                 soc1->soc_at_oids_may,err) )
368                                 return code;
369                         nsups++;
370                         sups1++;
371                 }
372         }
373         return 0;
374 }
375
376 static int
377 oc_insert(
378     ObjectClass         *soc,
379     const char          **err
380 )
381 {
382         ObjectClass     **ocp;
383         struct oindexrec        *oir;
384         char                    **names;
385
386         ocp = &oc_list;
387         while ( *ocp != NULL ) {
388                 ocp = &(*ocp)->soc_next;
389         }
390         *ocp = soc;
391
392         if ( soc->soc_oid ) {
393                 oir = (struct oindexrec *)
394                         ch_calloc( 1, sizeof(struct oindexrec) );
395                 oir->oir_name = soc->soc_oid;
396                 oir->oir_oc = soc;
397                 if ( avl_insert( &oc_index, (caddr_t) oir,
398                                  (AVL_CMP) oc_index_cmp,
399                                  (AVL_DUP) avl_dup_error ) ) {
400                         *err = soc->soc_oid;
401                         ldap_memfree(oir);
402                         return SLAP_SCHERR_DUP_CLASS;
403                 }
404                 /* FIX: temporal consistency check */
405                 oc_find(oir->oir_name);
406         }
407         if ( (names = soc->soc_names) ) {
408                 while ( *names ) {
409                         oir = (struct oindexrec *)
410                                 ch_calloc( 1, sizeof(struct oindexrec) );
411                         oir->oir_name = ch_strdup(*names);
412                         oir->oir_oc = soc;
413                         if ( avl_insert( &oc_index, (caddr_t) oir,
414                                          (AVL_CMP) oc_index_cmp,
415                                          (AVL_DUP) avl_dup_error ) ) {
416                                 *err = *names;
417                                 ldap_memfree(oir);
418                                 return SLAP_SCHERR_DUP_CLASS;
419                         }
420                         /* FIX: temporal consistency check */
421                         oc_find(oir->oir_name);
422                         names++;
423                 }
424         }
425         return 0;
426 }
427
428 int
429 oc_add(
430     LDAP_OBJECT_CLASS   *oc,
431     const char          **err
432 )
433 {
434         ObjectClass     *soc;
435         int             code;
436
437         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
438         memcpy( &soc->soc_oclass, oc, sizeof(LDAP_OBJECT_CLASS));
439         if ( code = oc_add_sups(soc,soc->soc_sup_oids,err) )
440                 return code;
441         if ( code = oc_create_required(soc,soc->soc_at_oids_must,err) )
442                 return code;
443         if ( code = oc_create_allowed(soc,soc->soc_at_oids_may,err) )
444                 return code;
445         code = oc_insert(soc,err);
446         return code;
447 }
448
449 struct sindexrec {
450         char            *sir_name;
451         Syntax          *sir_syn;
452 };
453
454 static Avlnode  *syn_index = NULL;
455 static Syntax *syn_list = NULL;
456
457 static int
458 syn_index_cmp(
459     struct sindexrec    *sir1,
460     struct sindexrec    *sir2
461 )
462 {
463         return (strcmp( sir1->sir_name, sir2->sir_name ));
464 }
465
466 static int
467 syn_index_name_cmp(
468     char                *name,
469     struct sindexrec    *sir
470 )
471 {
472         return (strcmp( name, sir->sir_name ));
473 }
474
475 Syntax *
476 syn_find( const char *synname )
477 {
478         struct sindexrec        *sir = NULL;
479
480         if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
481             (AVL_CMP) syn_index_name_cmp )) != NULL ) {
482                 return( sir->sir_syn );
483         }
484         return( NULL );
485 }
486
487 static int
488 syn_insert(
489     Syntax              *ssyn,
490     const char          **err
491 )
492 {
493         Syntax          **synp;
494         struct sindexrec        *sir;
495
496         synp = &syn_list;
497         while ( *synp != NULL ) {
498                 synp = &(*synp)->ssyn_next;
499         }
500         *synp = ssyn;
501
502         if ( ssyn->ssyn_oid ) {
503                 sir = (struct sindexrec *)
504                         ch_calloc( 1, sizeof(struct sindexrec) );
505                 sir->sir_name = ssyn->ssyn_oid;
506                 sir->sir_syn = ssyn;
507                 if ( avl_insert( &syn_index, (caddr_t) sir,
508                                  (AVL_CMP) syn_index_cmp,
509                                  (AVL_DUP) avl_dup_error ) ) {
510                         *err = ssyn->ssyn_oid;
511                         ldap_memfree(sir);
512                         return SLAP_SCHERR_DUP_SYNTAX;
513                 }
514                 /* FIX: temporal consistency check */
515                 syn_find(sir->sir_name);
516         }
517         return 0;
518 }
519
520 int
521 syn_add(
522     LDAP_SYNTAX         *syn,
523     slap_syntax_check_func      *check,
524     const char          **err
525 )
526 {
527         Syntax          *ssyn;
528         int             code;
529
530         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
531         memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
532         ssyn->ssyn_check = check;
533         code = syn_insert(ssyn,err);
534         return code;
535 }
536
537 struct mindexrec {
538         char            *mir_name;
539         MatchingRule    *mir_mr;
540 };
541
542 static Avlnode  *mr_index = NULL;
543 static MatchingRule *mr_list = NULL;
544
545 static int
546 mr_index_cmp(
547     struct mindexrec    *mir1,
548     struct mindexrec    *mir2
549 )
550 {
551         return (strcmp( mir1->mir_name, mir2->mir_name ));
552 }
553
554 static int
555 mr_index_name_cmp(
556     char                *name,
557     struct mindexrec    *mir
558 )
559 {
560         return (strcmp( name, mir->mir_name ));
561 }
562
563 MatchingRule *
564 mr_find( const char *mrname )
565 {
566         struct mindexrec        *mir = NULL;
567
568         if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
569             (AVL_CMP) mr_index_name_cmp )) != NULL ) {
570                 return( mir->mir_mr );
571         }
572         return( NULL );
573 }
574
575 static int
576 mr_insert(
577     MatchingRule        *smr,
578     const char          **err
579 )
580 {
581         MatchingRule            **mrp;
582         struct mindexrec        *mir;
583         char                    **names;
584
585         mrp = &mr_list;
586         while ( *mrp != NULL ) {
587                 mrp = &(*mrp)->smr_next;
588         }
589         *mrp = smr;
590
591         if ( smr->smr_oid ) {
592                 mir = (struct mindexrec *)
593                         ch_calloc( 1, sizeof(struct mindexrec) );
594                 mir->mir_name = smr->smr_oid;
595                 mir->mir_mr = smr;
596                 if ( avl_insert( &mr_index, (caddr_t) mir,
597                                  (AVL_CMP) mr_index_cmp,
598                                  (AVL_DUP) avl_dup_error ) ) {
599                         *err = smr->smr_oid;
600                         ldap_memfree(mir);
601                         return SLAP_SCHERR_DUP_RULE;
602                 }
603                 /* FIX: temporal consistency check */
604                 mr_find(mir->mir_name);
605         }
606         if ( (names = smr->smr_names) ) {
607                 while ( *names ) {
608                         mir = (struct mindexrec *)
609                                 ch_calloc( 1, sizeof(struct mindexrec) );
610                         mir->mir_name = ch_strdup(*names);
611                         mir->mir_mr = smr;
612                         if ( avl_insert( &mr_index, (caddr_t) mir,
613                                          (AVL_CMP) mr_index_cmp,
614                                          (AVL_DUP) avl_dup_error ) ) {
615                                 *err = *names;
616                                 ldap_memfree(mir);
617                                 return SLAP_SCHERR_DUP_RULE;
618                         }
619                         /* FIX: temporal consistency check */
620                         mr_find(mir->mir_name);
621                         names++;
622                 }
623         }
624         return 0;
625 }
626
627 int
628 mr_add(
629     LDAP_MATCHING_RULE          *mr,
630     slap_mr_normalize_func      *normalize,
631     slap_mr_compare_func        *compare,
632     const char          **err
633 )
634 {
635         MatchingRule    *smr;
636         int             code;
637
638         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
639         memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
640         smr->smr_normalize = normalize;
641         smr->smr_compare = compare;
642         code = mr_insert(smr,err);
643         return code;
644 }
645
646 int
647 register_syntax(
648         char * desc,
649         slap_syntax_check_func *check )
650 {
651         LDAP_SYNTAX     *syn;
652         int             code;
653         const char      *err;
654
655         syn = ldap_str2syntax( desc, &code, &err);
656         if ( !syn ) {
657                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
658                     ldap_scherr2str(code), err, desc );
659                 return( -1 );
660         }
661         code = syn_add( syn, check, &err );
662         if ( code ) {
663                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
664                     scherr2str(code), err, desc );
665                 return( -1 );
666         }
667         return( 0 );
668 }
669
670 int
671 register_matching_rule(
672         char * desc,
673         slap_mr_normalize_func *normalize,
674         slap_mr_compare_func *compare )
675 {
676         LDAP_MATCHING_RULE *mr;
677         int             code;
678         const char      *err;
679
680         mr = ldap_str2matchingrule( desc, &code, &err);
681         if ( !mr ) {
682                 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
683                     ldap_scherr2str(code), err, desc );
684                 return( -1 );
685         }
686         code = mr_add( mr, normalize, compare, &err );
687         if ( code ) {
688                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
689                     scherr2str(code), err, desc );
690                 return( -1 );
691         }
692         return( 0 );
693 }
694
695 struct syntax_defs_rec {
696         char *sd_desc;
697         slap_syntax_check_func *sd_check;
698 };
699
700 struct syntax_defs_rec syntax_defs[] = {
701         {"( 1.3.6.1.4.1.1466.115.121.1.3 DESC 'Attribute Type Description' )", NULL},
702         {"( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'DN' )", NULL},
703         {"( 1.3.6.1.4.1.1466.115.121.1.15 DESC 'Directory String' )", NULL},
704         {"( 1.3.6.1.4.1.1466.115.121.1.16 DESC 'DIT Content Rule Description' )", NULL},
705         {"( 1.3.6.1.4.1.1466.115.121.1.17 DESC 'DIT Structure Rule Description' )", NULL},
706         {"( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )", NULL},
707         {"( 1.3.6.1.4.1.1466.115.121.1.25 DESC 'Guide' )", NULL},
708         {"( 1.3.6.1.4.1.1466.115.121.1.26 DESC 'IA5 String' )", NULL},
709         {"( 1.3.6.1.4.1.1466.115.121.1.27 DESC 'INTEGER' )", NULL},
710         {"( 1.3.6.1.4.1.1466.115.121.1.30 DESC 'Matching Rule Description' )", NULL},
711         {"( 1.3.6.1.4.1.1466.115.121.1.31 DESC 'Matching Rule Use Description' )", NULL},
712         {"( 1.3.6.1.4.1.1466.115.121.1.35 DESC 'Name Form Description' )", NULL},
713         {"( 1.3.6.1.4.1.1466.115.121.1.37 DESC 'Object Class Description' )", NULL},
714         {"( 1.3.6.1.4.1.1466.115.121.1.38 DESC 'OID' )", NULL},
715         {"( 1.3.6.1.4.1.1466.115.121.1.44 DESC 'Printable String' )", NULL},
716         {"( 1.3.6.1.4.1.1466.115.121.1.54 DESC 'LDAP Syntax Description' )", NULL},
717         {NULL, NULL}
718 };
719
720 struct mrule_defs_rec {
721         char *mrd_desc;
722         slap_mr_normalize_func *mrd_normalize;
723         slap_mr_compare_func *mrd_compare;
724 };
725
726 struct mrule_defs_rec mrule_defs[] = {
727         {"( 2.5.13.0 NAME 'objectIdentifierMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )", NULL, NULL},
728         {"( 2.5.13.1 NAME 'distinguishedNameMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )", NULL, NULL},
729         {"( 2.5.13.2 NAME 'caseIgnoreMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )", NULL, NULL},
730         {"( 2.5.13.4 NAME 'caseIgnoreSubstringsMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.58 )", NULL, NULL},
731         {"( 2.5.13.8 NAME 'numericStringMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.36 )", NULL, NULL},
732         {"( 2.5.13.27 NAME 'generalizedTimeMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )", NULL, NULL},
733         {"( 2.5.13.28 NAME 'generalizedTimeOrderingMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 )", NULL, NULL},
734         {"( 2.5.13.29 NAME 'integerFirstComponentMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )", NULL, NULL},
735         {"( 2.5.13.30 NAME 'objectIdentifierFirstComponentMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )", NULL, NULL},
736         {NULL, NULL, NULL}
737 };
738
739 int
740 schema_init( void )
741 {
742         int             res;
743         int             code;
744         const char      *err;
745         int             i;
746
747         /* For now */
748         return( 0 );
749         for ( i=0; syntax_defs[i].sd_desc != NULL; i++ ) {
750                 res = register_syntax( syntax_defs[i].sd_desc,
751                     syntax_defs[i].sd_check );
752                 if ( res ) {
753                         fprintf( stderr, "schema_init: Error registering syntax %s\n",
754                                  syntax_defs[i].sd_desc );
755                         exit( 1 );
756                 }
757         }
758         for ( i=0; mrule_defs[i].mrd_desc != NULL; i++ ) {
759                 res = register_matching_rule( mrule_defs[i].mrd_desc,
760                     mrule_defs[i].mrd_normalize,
761                     mrule_defs[i].mrd_compare );
762                 if ( res ) {
763                         fprintf( stderr, "schema_init: Error registering matching rule %s\n",
764                                  mrule_defs[i].mrd_desc );
765                         exit( 1 );
766                 }
767         }
768 }
769
770 #if defined( SLAPD_SCHEMA_DN )
771
772 static int
773 syn_schema_info( Entry *e )
774 {
775         struct berval   val;
776         struct berval   *vals[2];
777         Syntax          *syn;
778
779         vals[0] = &val;
780         vals[1] = NULL;
781
782         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
783                 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
784                 if ( val.bv_val ) {
785                         val.bv_len = strlen( val.bv_val );
786                         Debug( LDAP_DEBUG_TRACE, "Merging syn [%d] %s\n",
787                                val.bv_len, val.bv_val, 0 );
788                         attr_merge( e, "ldapSyntaxes", vals );
789                         ldap_memfree( val.bv_val );
790                 } else {
791                         return -1;
792                 }
793         }
794         return 0;
795 }
796
797 static int
798 mr_schema_info( Entry *e )
799 {
800         struct berval   val;
801         struct berval   *vals[2];
802         MatchingRule    *mr;
803
804         vals[0] = &val;
805         vals[1] = NULL;
806
807         for ( mr = mr_list; mr; mr = mr->smr_next ) {
808                 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
809                 if ( val.bv_val ) {
810                         val.bv_len = strlen( val.bv_val );
811                         Debug( LDAP_DEBUG_TRACE, "Merging mr [%d] %s\n",
812                                val.bv_len, val.bv_val, 0 );
813                         attr_merge( e, "matchingRules", vals );
814                         ldap_memfree( val.bv_val );
815                 } else {
816                         return -1;
817                 }
818         }
819         return 0;
820 }
821
822 static int
823 oc_schema_info( Entry *e )
824 {
825         struct berval   val;
826         struct berval   *vals[2];
827         ObjectClass     *oc;
828
829         vals[0] = &val;
830         vals[1] = NULL;
831
832         for ( oc = oc_list; oc; oc = oc->soc_next ) {
833                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
834                 if ( val.bv_val ) {
835                         val.bv_len = strlen( val.bv_val );
836                         Debug( LDAP_DEBUG_TRACE, "Merging oc [%d] %s\n",
837                                val.bv_len, val.bv_val, 0 );
838                         attr_merge( e, "objectClasses", vals );
839                         ldap_memfree( val.bv_val );
840                 } else {
841                         return -1;
842                 }
843         }
844         return 0;
845 }
846
847 void
848 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
849 {
850         Entry           *e;
851         struct berval   val;
852         struct berval   *vals[2];
853
854         vals[0] = &val;
855         vals[1] = NULL;
856
857         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
858
859         e->e_attrs = NULL;
860         e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
861         e->e_ndn = dn_normalize_case( ch_strdup( SLAPD_SCHEMA_DN ));
862         e->e_private = NULL;
863
864         val.bv_val = ch_strdup( "top" );
865         val.bv_len = strlen( val.bv_val );
866         attr_merge( e, "objectclass", vals );
867         ldap_memfree( val.bv_val );
868
869         val.bv_val = ch_strdup( "subschema" );
870         val.bv_len = strlen( val.bv_val );
871         attr_merge( e, "objectclass", vals );
872         ldap_memfree( val.bv_val );
873
874         if ( syn_schema_info( e ) ) {
875                 /* Out of memory, do something about it */
876                 entry_free( e );
877                 return;
878         }
879         if ( mr_schema_info( e ) ) {
880                 /* Out of memory, do something about it */
881                 entry_free( e );
882                 return;
883         }
884         if ( at_schema_info( e ) ) {
885                 /* Out of memory, do something about it */
886                 entry_free( e );
887                 return;
888         }
889         if ( oc_schema_info( e ) ) {
890                 /* Out of memory, do something about it */
891                 entry_free( e );
892                 return;
893         }
894         
895         send_search_entry( &backends[0], conn, op, e, attrs, attrsonly );
896         send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL, 1 );
897
898         entry_free( e );
899 }
900 #endif
901
902 #ifdef LDAP_DEBUG
903
904 static void
905 oc_print( ObjectClass *oc )
906 {
907         int     i;
908
909         if ( oc->soc_names && oc->soc_names[0] ) {
910                 printf( "objectclass %s\n", oc->soc_names[0] );
911         } else {
912                 printf( "objectclass %s\n", oc->soc_oid );
913         }
914         if ( oc->soc_required != NULL ) {
915                 printf( "\trequires %s", oc->soc_required[0] );
916                 for ( i = 1; oc->soc_required[i] != NULL; i++ ) {
917                         printf( ",%s", oc->soc_required[i] );
918                 }
919                 printf( "\n" );
920         }
921         if ( oc->soc_allowed != NULL ) {
922                 printf( "\tallows %s", oc->soc_allowed[0] );
923                 for ( i = 1; oc->soc_allowed[i] != NULL; i++ ) {
924                         printf( ",%s", oc->soc_allowed[i] );
925                 }
926                 printf( "\n" );
927         }
928 }
929
930 #endif