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