]> git.sur5r.net Git - openldap/blob - servers/slapd/schema.c
Add SLAP_SYNTAX_BINARY flag to indicate binary storage is used for
[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         int flags,
681     slap_syntax_validate_func   *validate,
682     slap_syntax_transform_func  *ber2str,
683     slap_syntax_transform_func  *str2ber,
684     const char          **err
685 )
686 {
687         Syntax          *ssyn;
688         int             code;
689
690         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
691         memcpy( &ssyn->ssyn_syn, syn, sizeof(LDAP_SYNTAX));
692
693         ssyn->ssyn_flags = flags;
694         ssyn->ssyn_validate = validate;
695         ssyn->ssyn_ber2str = ber2str;
696         ssyn->ssyn_str2ber = str2ber;
697
698         code = syn_insert(ssyn,err);
699         return code;
700 }
701
702 struct mindexrec {
703         char            *mir_name;
704         MatchingRule    *mir_mr;
705 };
706
707 static Avlnode  *mr_index = NULL;
708 static MatchingRule *mr_list = NULL;
709
710 static int
711 mr_index_cmp(
712     struct mindexrec    *mir1,
713     struct mindexrec    *mir2
714 )
715 {
716         return (strcmp( mir1->mir_name, mir2->mir_name ));
717 }
718
719 static int
720 mr_index_name_cmp(
721     char                *name,
722     struct mindexrec    *mir
723 )
724 {
725         return (strcmp( name, mir->mir_name ));
726 }
727
728 MatchingRule *
729 mr_find( const char *mrname )
730 {
731         struct mindexrec        *mir = NULL;
732
733         if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
734             (AVL_CMP) mr_index_name_cmp )) != NULL ) {
735                 return( mir->mir_mr );
736         }
737         return( NULL );
738 }
739
740 static int
741 mr_insert(
742     MatchingRule        *smr,
743     const char          **err
744 )
745 {
746         MatchingRule            **mrp;
747         struct mindexrec        *mir;
748         char                    **names;
749
750         mrp = &mr_list;
751         while ( *mrp != NULL ) {
752                 mrp = &(*mrp)->smr_next;
753         }
754         *mrp = smr;
755
756         if ( smr->smr_oid ) {
757                 mir = (struct mindexrec *)
758                         ch_calloc( 1, sizeof(struct mindexrec) );
759                 mir->mir_name = smr->smr_oid;
760                 mir->mir_mr = smr;
761                 if ( avl_insert( &mr_index, (caddr_t) mir,
762                                  (AVL_CMP) mr_index_cmp,
763                                  (AVL_DUP) avl_dup_error ) ) {
764                         *err = smr->smr_oid;
765                         ldap_memfree(mir);
766                         return SLAP_SCHERR_DUP_RULE;
767                 }
768                 /* FIX: temporal consistency check */
769                 mr_find(mir->mir_name);
770         }
771         if ( (names = smr->smr_names) ) {
772                 while ( *names ) {
773                         mir = (struct mindexrec *)
774                                 ch_calloc( 1, sizeof(struct mindexrec) );
775                         mir->mir_name = ch_strdup(*names);
776                         mir->mir_mr = smr;
777                         if ( avl_insert( &mr_index, (caddr_t) mir,
778                                          (AVL_CMP) mr_index_cmp,
779                                          (AVL_DUP) avl_dup_error ) ) {
780                                 *err = *names;
781                                 ldap_memfree(mir);
782                                 return SLAP_SCHERR_DUP_RULE;
783                         }
784                         /* FIX: temporal consistency check */
785                         mr_find(mir->mir_name);
786                         names++;
787                 }
788         }
789         return 0;
790 }
791
792 int
793 mr_add(
794     LDAP_MATCHING_RULE          *mr,
795         slap_mr_convert_func *convert,
796         slap_mr_normalize_func *normalize,
797     slap_mr_match_func  *match,
798         slap_mr_indexer_func *indexer,
799     slap_mr_filter_func *filter,
800     const char          **err
801 )
802 {
803         MatchingRule    *smr;
804         Syntax          *syn;
805         int             code;
806
807         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
808         memcpy( &smr->smr_mrule, mr, sizeof(LDAP_MATCHING_RULE));
809
810         smr->smr_convert = convert;
811         smr->smr_normalize = normalize;
812         smr->smr_match = match;
813         smr->smr_indexer = indexer;
814         smr->smr_filter = filter;
815
816         if ( smr->smr_syntax_oid ) {
817                 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
818                         smr->smr_syntax = syn;
819                 } else {
820                         *err = smr->smr_syntax_oid;
821                         return SLAP_SCHERR_SYN_NOT_FOUND;
822                 }
823         } else {
824                 *err = "";
825                 return SLAP_SCHERR_MR_INCOMPLETE;
826         }
827         code = mr_insert(smr,err);
828         return code;
829 }
830
831 int
832 register_syntax(
833         char * desc, int flags,
834         slap_syntax_validate_func *validate,
835         slap_syntax_transform_func *ber2str,
836         slap_syntax_transform_func *str2ber )
837 {
838         LDAP_SYNTAX     *syn;
839         int             code;
840         const char      *err;
841
842         syn = ldap_str2syntax( desc, &code, &err);
843         if ( !syn ) {
844                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
845                     ldap_scherr2str(code), err, desc );
846                 return( -1 );
847         }
848
849         code = syn_add( syn, flags, validate, ber2str, str2ber, &err );
850         if ( code ) {
851                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
852                     scherr2str(code), err, desc );
853                 return( -1 );
854         }
855
856         return( 0 );
857 }
858
859 int
860 register_matching_rule(
861         char * desc,
862         slap_mr_convert_func *convert,
863         slap_mr_normalize_func *normalize,
864         slap_mr_match_func *match,
865         slap_mr_indexer_func *indexer,
866         slap_mr_filter_func *filter )
867 {
868         LDAP_MATCHING_RULE *mr;
869         int             code;
870         const char      *err;
871
872         mr = ldap_str2matchingrule( desc, &code, &err);
873         if ( !mr ) {
874                 Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
875                     ldap_scherr2str(code), err, desc );
876                 return( -1 );
877         }
878
879         code = mr_add( mr, convert, normalize, match, indexer, filter, &err );
880         if ( code ) {
881                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s for %s in %s\n",
882                     scherr2str(code), err, desc );
883                 return( -1 );
884         }
885         return( 0 );
886 }
887
888
889 #if defined( SLAPD_SCHEMA_DN )
890
891 static int
892 syn_schema_info( Entry *e )
893 {
894         struct berval   val;
895         struct berval   *vals[2];
896         Syntax          *syn;
897
898         vals[0] = &val;
899         vals[1] = NULL;
900
901         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
902                 val.bv_val = ldap_syntax2str( &syn->ssyn_syn );
903                 if ( val.bv_val ) {
904                         val.bv_len = strlen( val.bv_val );
905                         Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
906                                (long) val.bv_len, val.bv_val, 0 );
907                         attr_merge( e, "ldapSyntaxes", vals );
908                         ldap_memfree( val.bv_val );
909                 } else {
910                         return -1;
911                 }
912         }
913         return 0;
914 }
915
916 static int
917 mr_schema_info( Entry *e )
918 {
919         struct berval   val;
920         struct berval   *vals[2];
921         MatchingRule    *mr;
922
923         vals[0] = &val;
924         vals[1] = NULL;
925
926         for ( mr = mr_list; mr; mr = mr->smr_next ) {
927                 val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
928                 if ( val.bv_val ) {
929                         val.bv_len = strlen( val.bv_val );
930                         Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
931                                (long) val.bv_len, val.bv_val, 0 );
932                         attr_merge( e, "matchingRules", vals );
933                         ldap_memfree( val.bv_val );
934                 } else {
935                         return -1;
936                 }
937         }
938         return 0;
939 }
940
941 static int
942 oc_schema_info( Entry *e )
943 {
944         struct berval   val;
945         struct berval   *vals[2];
946         ObjectClass     *oc;
947
948         vals[0] = &val;
949         vals[1] = NULL;
950
951         for ( oc = oc_list; oc; oc = oc->soc_next ) {
952                 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
953                 if ( val.bv_val ) {
954                         val.bv_len = strlen( val.bv_val );
955                         Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
956                                (long) val.bv_len, val.bv_val, 0 );
957                         attr_merge( e, "objectClasses", vals );
958                         ldap_memfree( val.bv_val );
959                 } else {
960                         return -1;
961                 }
962         }
963         return 0;
964 }
965
966 void
967 schema_info( Connection *conn, Operation *op, char **attrs, int attrsonly )
968 {
969         Entry           *e;
970         struct berval   val;
971         struct berval   *vals[2];
972
973         vals[0] = &val;
974         vals[1] = NULL;
975
976         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
977
978         e->e_attrs = NULL;
979         e->e_dn = ch_strdup( SLAPD_SCHEMA_DN );
980         e->e_ndn = ch_strdup( SLAPD_SCHEMA_DN );
981         (void) dn_normalize( e->e_ndn );
982         e->e_private = NULL;
983
984         {
985                 char *rdn = ch_strdup( SLAPD_SCHEMA_DN );
986                 val.bv_val = strchr( rdn, '=' );
987
988                 if( val.bv_val != NULL ) {
989                         *val.bv_val = '\0';
990                         val.bv_len = strlen( ++val.bv_val );
991
992                         attr_merge( e, rdn, vals );
993                 }
994
995                 free( rdn );
996         }
997
998         if ( syn_schema_info( e ) ) {
999                 /* Out of memory, do something about it */
1000                 entry_free( e );
1001                 return;
1002         }
1003         if ( mr_schema_info( e ) ) {
1004                 /* Out of memory, do something about it */
1005                 entry_free( e );
1006                 return;
1007         }
1008         if ( at_schema_info( e ) ) {
1009                 /* Out of memory, do something about it */
1010                 entry_free( e );
1011                 return;
1012         }
1013         if ( oc_schema_info( e ) ) {
1014                 /* Out of memory, do something about it */
1015                 entry_free( e );
1016                 return;
1017         }
1018         
1019         val.bv_val = "top";
1020         val.bv_len = sizeof("top")-1;
1021         attr_merge( e, "objectClass", vals );
1022
1023         val.bv_val = "LDAPsubentry";
1024         val.bv_len = sizeof("LDAPsubentry")-1;
1025         attr_merge( e, "objectClass", vals );
1026
1027         val.bv_val = "subschema";
1028         val.bv_len = sizeof("subschema")-1;
1029         attr_merge( e, "objectClass", vals );
1030
1031         val.bv_val = "extensibleObject";
1032         val.bv_len = sizeof("extensibleObject")-1;
1033         attr_merge( e, "objectClass", vals );
1034
1035         send_search_entry( &backends[0], conn, op,
1036                 e, attrs, attrsonly, NULL );
1037         send_search_result( conn, op, LDAP_SUCCESS,
1038                 NULL, NULL, NULL, NULL, 1 );
1039
1040         entry_free( e );
1041 }
1042 #endif
1043
1044 #ifdef LDAP_DEBUG
1045
1046 static void
1047 oc_print( ObjectClass *oc )
1048 {
1049         int     i;
1050         const char *mid;
1051
1052         printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
1053         if ( oc->soc_required != NULL ) {
1054                 mid = "\trequires ";
1055                 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
1056                         printf( "%s%s", mid,
1057                                 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
1058                 printf( "\n" );
1059         }
1060         if ( oc->soc_allowed != NULL ) {
1061                 mid = "\tallows ";
1062                 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
1063                         printf( "%s%s", mid,
1064                                 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
1065                 printf( "\n" );
1066         }
1067 }
1068
1069 #endif
1070
1071 int is_entry_objectclass(
1072         Entry*  e,
1073         const char*     oc)
1074 {
1075         Attribute *attr;
1076         struct berval bv;
1077
1078         if( e == NULL || oc == NULL || *oc == '\0' )
1079                 return 0;
1080
1081         /*
1082          * find objectClass attribute
1083          */
1084         attr = attr_find(e->e_attrs, "objectclass");
1085
1086         if( attr == NULL ) {
1087                 /* no objectClass attribute */
1088                 return 0;
1089         }
1090
1091         bv.bv_val = (char *) oc;
1092         bv.bv_len = strlen( bv.bv_val );
1093
1094 #ifdef SLAPD_SCHEMA_COMPAT
1095         if( value_find(attr->a_vals, &bv, attr->a_syntax, 1) != 0) {
1096                 /* entry is not of this objectclass */
1097                 return 0;
1098         }
1099 #endif
1100
1101         return 1;
1102 }