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