]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
ITS#2864 don't use sl_mark/release.
[openldap] / servers / slapd / ad.c
1 /* ad.c - routines for dealing with attribute descriptions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "ldap_pvt.h"
28 #include "slap.h"
29 #include "lutil.h"
30
31 typedef struct Attr_option {
32         struct berval name;     /* option name or prefix */
33         int           prefix;   /* NAME is a tag and range prefix */
34 } Attr_option;
35
36 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
37
38 /* Options sorted by name, and number of options */
39 static Attr_option *options = &lang_option;
40 static int option_count = 1;
41
42 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
43
44 static int ad_keystring(
45         struct berval *bv )
46 {
47         ber_len_t i;
48
49         if( !AD_CHAR( bv->bv_val[0] ) ) {
50                 return 1;
51         }
52
53         for( i=1; i<bv->bv_len; i++ ) {
54                 if( !AD_CHAR( bv->bv_val[i] ) ) {
55                         return 1;
56                 }
57         }
58         return 0;
59 }
60
61 void ad_destroy( AttributeDescription *ad )
62 {
63         AttributeDescription *n;
64
65         for (; ad != NULL; ad = n) {
66                 n = ad->ad_next;
67                 ldap_memfree( ad );
68         }
69 }
70
71 /* Is there an AttributeDescription for this type that uses these tags? */
72 AttributeDescription * ad_find_tags(
73         AttributeType *type,
74         struct berval *tags )
75 {
76         AttributeDescription *ad;
77
78         ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
79         for (ad = type->sat_ad; ad; ad=ad->ad_next)
80         {
81                 if (ad->ad_tags.bv_len == tags->bv_len &&
82                         !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
83                         break;
84         }
85         ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
86         return ad;
87 }
88
89 int slap_str2ad(
90         const char *str,
91         AttributeDescription **ad,
92         const char **text )
93 {
94         struct berval bv;
95         bv.bv_val = (char *) str;
96         bv.bv_len = strlen( str );
97
98         return slap_bv2ad( &bv, ad, text );
99 }
100
101 static char *strchrlen(
102         const char *p, 
103         const char ch, 
104         int *len )
105 {
106         int i;
107
108         for( i=0; p[i]; i++ ) {
109                 if( p[i] == ch ) {
110                         *len = i;
111                         return (char *) &p[i];
112                 }
113         }
114
115         *len = i;
116         return NULL;
117 }
118
119 int slap_bv2ad(
120         struct berval *bv,
121         AttributeDescription **ad,
122         const char **text )
123 {
124         int rtn = LDAP_UNDEFINED_TYPE;
125         AttributeDescription desc, *d2;
126         char *name, *options;
127         char *opt, *next;
128         int ntags;
129         int tagslen;
130
131         /* hardcoded limits for speed */
132 #define MAX_TAGGING_OPTIONS 128
133         struct berval tags[MAX_TAGGING_OPTIONS+1];
134 #define MAX_TAGS_LEN 1024
135         char tagbuf[MAX_TAGS_LEN];
136
137         assert( ad != NULL );
138         assert( *ad == NULL ); /* temporary */
139
140         if( bv == NULL || bv->bv_len == 0 ) {
141                 *text = "empty attribute description";
142                 return rtn;
143         }
144
145         /* make sure description is IA5 */
146         if( ad_keystring( bv ) ) {
147                 *text = "attribute description contains inappropriate characters";
148                 return rtn;
149         }
150
151         /* find valid base attribute type; parse in place */
152         memset( &desc, 0, sizeof( desc ));
153         desc.ad_cname = *bv;
154         name = bv->bv_val;
155         options = strchr(name, ';');
156         if( options != NULL ) {
157                 desc.ad_cname.bv_len = options - name;
158         }
159         desc.ad_type = at_bvfind( &desc.ad_cname );
160         if( desc.ad_type == NULL ) {
161                 *text = "attribute type undefined";
162                 return rtn;
163         }
164
165         if( is_at_operational( desc.ad_type ) && options != NULL ) {
166                 *text = "operational attribute with options undefined";
167                 return rtn;
168         }
169
170         /*
171          * parse options in place
172          */
173         ntags = 0;
174         memset( tags, 0, sizeof( tags ));
175         tagslen = 0;
176
177         for( opt=options; opt != NULL; opt=next ) {
178                 int optlen;
179                 opt++; 
180                 next = strchrlen( opt, ';', &optlen );
181
182                 if( optlen == 0 ) {
183                         *text = "zero length option is invalid";
184                         return rtn;
185                 
186                 } else if ( optlen == sizeof("binary")-1 &&
187                         strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
188                 {
189                         /* binary option */
190                         if( slap_ad_is_binary( &desc ) ) {
191                                 *text = "option \"binary\" specified multiple times";
192                                 return rtn;
193                         }
194
195                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
196                                 /* not stored in binary, disallow option */
197                                 *text = "option \"binary\" not supported with type";
198                                 return rtn;
199                         }
200
201                         desc.ad_flags |= SLAP_DESC_BINARY;
202                         continue;
203
204                 } else if ( ad_find_option_definition( opt, optlen ) ) {
205                         int i;
206
207                         if( opt[optlen-1] == '-' ) {
208                                 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
209                         }
210
211                         if( ntags >= MAX_TAGGING_OPTIONS ) {
212                                 *text = "too many tagging options";
213                                 return rtn;
214                         }
215
216                         /*
217                          * tags should be presented in sorted order,
218                          * so run the array in reverse.
219                          */
220                         for( i=ntags-1; i>=0; i-- ) {
221                                 int rc;
222
223                                 rc = strncasecmp( opt, tags[i].bv_val,
224                                         (unsigned) optlen < tags[i].bv_len
225                                                 ? optlen : tags[i].bv_len );
226
227                                 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
228                                         /* duplicate (ignore) */
229                                         goto done;
230
231                                 } else if ( rc > 0 ||
232                                         ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
233                                 {
234                                         AC_MEMCPY( &tags[i+2], &tags[i+1],
235                                                 (ntags-i-1)*sizeof(struct berval) );
236                                         tags[i+1].bv_val = opt;
237                                         tags[i+1].bv_len = optlen;
238                                         goto done;
239                                 }
240                         }
241
242                         if( ntags ) {
243                                 AC_MEMCPY( &tags[1], &tags[0],
244                                         ntags*sizeof(struct berval) );
245                         }
246                         tags[0].bv_val = opt;
247                         tags[0].bv_len = optlen;
248
249 done:;
250                         tagslen += optlen + 1;
251                         ntags++;
252
253                 } else {
254                         *text = "unrecognized option";
255                         return rtn;
256                 }
257         }
258
259         if( ntags > 0 ) {
260                 int i;
261
262                 if( tagslen > MAX_TAGS_LEN ) {
263                         *text = "tagging options too long";
264                         return rtn;
265                 }
266
267                 desc.ad_tags.bv_val = tagbuf;
268                 tagslen = 0;
269
270                 for( i=0; i<ntags; i++ ) {
271                         AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
272                                 tags[i].bv_val, tags[i].bv_len );
273
274                         tagslen += tags[i].bv_len;
275                         desc.ad_tags.bv_val[tagslen++] = ';';
276                 }
277
278                 desc.ad_tags.bv_val[--tagslen] = '\0';
279                 desc.ad_tags.bv_len = tagslen;
280         }
281
282         /* see if a matching description is already cached */
283         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
284                 if( d2->ad_flags != desc.ad_flags ) {
285                         continue;
286                 }
287                 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
288                         continue;
289                 }
290                 if( d2->ad_tags.bv_len == 0 ) {
291                         break;
292                 }
293                 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
294                         desc.ad_tags.bv_len ) == 0 )
295                 {
296                         break;
297                 }
298         }
299
300         /* Not found, add new one */
301         while (d2 == NULL) {
302                 size_t dlen = 0;
303                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
304                 /* check again now that we've locked */
305                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
306                         if (d2->ad_flags != desc.ad_flags)
307                                 continue;
308                         if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
309                                 continue;
310                         if (d2->ad_tags.bv_len == 0)
311                                 break;
312                         if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
313                                 desc.ad_tags.bv_len) == 0)
314                                 break;
315                 }
316                 if (d2) {
317                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
318                         break;
319                 }
320
321                 /* Allocate a single contiguous block. If there are no
322                  * options, we just need space for the AttrDesc structure.
323                  * Otherwise, we need to tack on the full name length +
324                  * options length, + maybe tagging options length again.
325                  */
326                 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
327                         dlen = desc.ad_type->sat_cname.bv_len + 1;
328                         if (desc.ad_tags.bv_len) {
329                                 dlen += 1+desc.ad_tags.bv_len;
330                         }
331                         if( slap_ad_is_binary( &desc ) ) {
332                                 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
333                         }
334                 }
335
336                 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
337                 d2->ad_next = NULL;
338                 d2->ad_type = desc.ad_type;
339                 d2->ad_flags = desc.ad_flags;
340                 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
341                 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
342
343                 if (dlen == 0) {
344                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
345                         d2->ad_tags.bv_val = NULL;
346                 } else {
347                         char *cp, *op, *lp;
348                         int j;
349                         d2->ad_cname.bv_val = (char *)(d2+1);
350                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
351                         cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
352                         if( slap_ad_is_binary( &desc ) ) {
353                                 op = cp;
354                                 lp = NULL;
355                                 if( desc.ad_tags.bv_len ) {
356                                         lp = desc.ad_tags.bv_val;
357                                         while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
358                                                && (lp = strchr( lp, ';' )) != NULL )
359                                                 ++lp;
360                                         if( lp != desc.ad_tags.bv_val ) {
361                                                 *cp++ = ';';
362                                                 j = (lp
363                                                      ? lp - desc.ad_tags.bv_val - 1
364                                                      : strlen( desc.ad_tags.bv_val ));
365                                                 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
366                                         }
367                                 }
368                                 cp = lutil_strcopy(cp, ";binary");
369                                 if( lp != NULL ) {
370                                         *cp++ = ';';
371                                         cp = lutil_strcopy(cp, lp);
372                                 }
373                                 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
374                                 if( desc.ad_tags.bv_len )
375                                         ldap_pvt_str2lower(op);
376                                 j = 1;
377                         } else {
378                                 j = 0;
379                         }
380                         if( desc.ad_tags.bv_len ) {
381                                 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
382                                 if ( j == 0 )
383                                         *lp++ = ';';
384                                 d2->ad_tags.bv_val = lp;
385                                 strcpy(lp, desc.ad_tags.bv_val);
386                                 ldap_pvt_str2lower(lp);
387                                 if( j == 0 )
388                                         d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
389                         }
390                 }
391                 /* Add new desc to list. We always want the bare Desc with
392                  * no options to stay at the head of the list, assuming
393                  * that one will be used most frequently.
394                  */
395                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
396                         d2->ad_next = desc.ad_type->sat_ad;
397                         desc.ad_type->sat_ad = d2;
398                 } else {
399                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
400                         desc.ad_type->sat_ad->ad_next = d2;
401                 }
402                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
403         }
404
405         if( *ad == NULL ) {
406                 *ad = d2;
407         } else {
408                 **ad = *d2;
409         }
410
411         return LDAP_SUCCESS;
412 }
413
414 static int is_ad_subtags(
415         struct berval *subtagsbv, 
416         struct berval *suptagsbv )
417 {
418         const char *suptags, *supp, *supdelimp;
419         const char *subtags, *subp, *subdelimp;
420         int  suplen, sublen;
421
422         subtags =subtagsbv->bv_val;
423         suptags =suptagsbv->bv_val;
424
425         for( supp=suptags ; supp; supp=supdelimp ) {
426                 supdelimp = strchrlen( supp, ';', &suplen );
427                 if( supdelimp ) supdelimp++;
428
429                 for( subp=subtags ; subp; subp=subdelimp ) {
430                         subdelimp = strchrlen( subp, ';', &sublen );
431                         if( subdelimp ) subdelimp++;
432
433                         if ( suplen > sublen
434                                  ? ( suplen-1 == sublen && supp[suplen-1] == '-'
435                                          && strncmp( supp, subp, sublen ) == 0 )
436                                  : ( ( suplen == sublen || supp[suplen-1] == '-' )
437                                          && strncmp( supp, subp, suplen ) == 0 ) )
438                         {
439                                 goto match;
440                         }
441                 }
442
443                 return 0;
444 match:;
445         }
446         return 1;
447 }
448
449 int is_ad_subtype(
450         AttributeDescription *sub,
451         AttributeDescription *super
452 )
453 {
454         AttributeType *a;
455         int lr;
456
457         for ( a = sub->ad_type; a; a=a->sat_sup ) {
458                 if ( a == super->ad_type ) break;
459         }
460         if( !a ) {
461                 return 0;
462         }
463
464         /* ensure sub does support all flags of super */
465         lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
466         if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
467                 return 0;
468         }
469
470         /* check for tagging options */
471         if ( super->ad_tags.bv_len == 0 )
472                 return 1;
473         if ( sub->ad_tags.bv_len == 0 )
474                 return 0;
475
476         return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
477 }
478
479 int ad_inlist(
480         AttributeDescription *desc,
481         AttributeName *attrs )
482 {
483         if (! attrs ) return 0;
484
485         for( ; attrs->an_name.bv_val; attrs++ ) {
486                 AttributeType *a;
487                 ObjectClass *oc;
488                 int rc;
489                 
490                 if ( attrs->an_desc ) {
491                         int lr;
492
493                         if ( desc == attrs->an_desc ) {
494                                 return 1;
495                         }
496
497                         /*
498                          * EXTENSION: if requested description is preceeded by
499                          * a '-' character, do not match on subtypes.
500                          */
501                         if ( attrs->an_name.bv_val[0] == '-' ) {
502                                 continue;
503                         }
504                         
505                         /* Is this a subtype of the requested attr? */
506                         for (a = desc->ad_type; a; a=a->sat_sup) {
507                                 if ( a == attrs->an_desc->ad_type )
508                                         break;
509                         }
510                         if ( !a ) {
511                                 continue;
512                         }
513                         /* Does desc support all the requested flags? */
514                         lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
515                         if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
516                                 != attrs->an_desc->ad_flags ) {
517                                 continue;
518                         }
519                         /* Do the descs have compatible tags? */
520                         if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
521                                 return 1;
522                         }
523                         if ( desc->ad_tags.bv_len == 0) {
524                                 continue;
525                         }
526                         if ( is_ad_subtags( &desc->ad_tags,
527                                 &attrs->an_desc->ad_tags ) ) {
528                                 return 1;
529                         }
530                         continue;
531                 }
532
533                 /*
534                  * EXTENSION: see if requested description is +objectClass
535                  * if so, return attributes which the class requires/allows
536                  */
537                 oc = attrs->an_oc;
538                 if( oc == NULL && attrs->an_name.bv_val ) {
539                         switch( attrs->an_name.bv_val[0] ) {
540                         case '+': { /* new way */
541                                         struct berval ocname;
542                                         ocname.bv_len = attrs->an_name.bv_len - 1;
543                                         ocname.bv_val = &attrs->an_name.bv_val[1];
544                                         oc = oc_bvfind( &ocname );
545                                 } break;
546                         default: /* old (deprecated) way */
547                                 oc = oc_bvfind( &attrs->an_name );
548                         }
549                         attrs->an_oc = oc;
550                 }
551                 if( oc != NULL ) {
552                         if ( oc == slap_schema.si_oc_extensibleObject ) {
553                                 /* extensibleObject allows the return of anything */
554                                 return 1;
555                         }
556
557                         if( oc->soc_required ) {
558                                 /* allow return of required attributes */
559                                 int i;
560                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
561                                         for (a = desc->ad_type; a; a=a->sat_sup) {
562                                                 if ( a == oc->soc_required[i] ) {
563                                                         return 1;
564                                                 }
565                                         }
566                                 }
567                         }
568
569                         if( oc->soc_allowed ) {
570                                 /* allow return of allowed attributes */
571                                 int i;
572                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
573                                         for (a = desc->ad_type; a; a=a->sat_sup) {
574                                                 if ( a == oc->soc_allowed[i] ) {
575                                                         return 1;
576                                                 }
577                                         }
578                                 }
579                         }
580
581                 } else {
582                         /* short-circuit this search next time around */
583                         if (!slap_schema.si_at_undefined->sat_ad) {
584                                 const char *text;
585                                 slap_bv2undef_ad(&attrs->an_name,
586                                         &attrs->an_desc, &text);
587                         } else {
588                                 attrs->an_desc =
589                                         slap_schema.si_at_undefined->sat_ad;
590                         }
591                 }
592         }
593
594         return 0;
595 }
596
597
598 int slap_str2undef_ad(
599         const char *str,
600         AttributeDescription **ad,
601         const char **text )
602 {
603         struct berval bv;
604         bv.bv_val = (char *) str;
605         bv.bv_len = strlen( str );
606
607         return slap_bv2undef_ad( &bv, ad, text );
608 }
609
610 int slap_bv2undef_ad(
611         struct berval *bv,
612         AttributeDescription **ad,
613         const char **text )
614 {
615         AttributeDescription *desc;
616
617         assert( ad != NULL );
618
619         if( bv == NULL || bv->bv_len == 0 ) {
620                 *text = "empty attribute description";
621                 return LDAP_UNDEFINED_TYPE;
622         }
623
624         /* make sure description is IA5 */
625         if( ad_keystring( bv ) ) {
626                 *text = "attribute description contains inappropriate characters";
627                 return LDAP_UNDEFINED_TYPE;
628         }
629
630         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
631                 desc=desc->ad_next ) 
632         {
633                 if( desc->ad_cname.bv_len == bv->bv_len &&
634                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
635                 {
636                         break;
637                 }
638         }
639         
640         if( !desc ) {
641                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
642                         bv->bv_len);
643                 
644                 desc->ad_flags = SLAP_DESC_NONE;
645                 desc->ad_tags.bv_val = NULL;
646                 desc->ad_tags.bv_len = 0;
647
648                 desc->ad_cname.bv_len = bv->bv_len;
649                 desc->ad_cname.bv_val = (char *)(desc+1);
650                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
651
652                 /* canonical to upper case */
653                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
654
655                 desc->ad_type = slap_schema.si_at_undefined;
656                 desc->ad_next = desc->ad_type->sat_ad;
657                 desc->ad_type->sat_ad = desc;
658         }
659
660         if( !*ad ) {
661                 *ad = desc;
662         } else {
663                 **ad = *desc;
664         }
665
666         return LDAP_SUCCESS;
667 }
668
669 int
670 an_find(
671     AttributeName *a,
672     struct berval *s
673 )
674 {
675         if( a == NULL ) return 0;
676
677         for ( ; a->an_name.bv_val; a++ ) {
678                 if ( a->an_name.bv_len != s->bv_len) continue;
679                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
680                         return( 1 );
681                 }
682         }
683
684         return( 0 );
685 }
686
687 /*
688  * Convert a delimited string into a list of AttributeNames; 
689  * add on to an existing list if it was given.  If the string
690  * is not a valid attribute name, if a '-' is prepended it is 
691  * skipped and the remaining name is tried again; if a '+' is
692  * prepended, an objectclass name is searched instead.
693  * 
694  * NOTE: currently, if a valid attribute name is not found,
695  * the same string is also checked as valid objectclass name;
696  * however, this behavior is deprecated.
697  */
698 AttributeName *
699 str2anlist( AttributeName *an, char *in, const char *brkstr )
700 {
701         char    *str;
702         char    *s;
703         char    *lasts;
704         int     i, j;
705         const char *text;
706         AttributeName *anew;
707
708         /* find last element in list */
709         for (i = 0; an && an[i].an_name.bv_val; i++);
710         
711         /* protect the input string from strtok */
712         str = ch_strdup( in );
713
714         /* Count words in string */
715         j=1;
716         for ( s = str; *s; s++ ) {
717                 if ( strchr( brkstr, *s ) != NULL ) {
718                         j++;
719                 }
720         }
721
722         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
723         anew = an + i;
724         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
725                 s != NULL;
726                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
727         {
728                 anew->an_desc = NULL;
729                 anew->an_oc = NULL;
730                 ber_str2bv(s, 0, 1, &anew->an_name);
731                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
732                 if ( !anew->an_desc ) {
733                         switch( anew->an_name.bv_val[0] ) {
734                         case '-': {
735                                         struct berval adname;
736                                         adname.bv_len = anew->an_name.bv_len - 1;
737                                         adname.bv_val = &anew->an_name.bv_val[1];
738                                         slap_bv2ad(&adname, &anew->an_desc, &text);
739                                         if ( !anew->an_desc ) {
740                                                 free( an );
741                                                 /*
742                                                  * overwrites input string
743                                                  * on error!
744                                                  */
745                                                 strcpy( in, s );
746                                                 return NULL;
747                                         }
748                                 } break;
749
750                         case '+': {
751                                         struct berval ocname;
752                                         ocname.bv_len = anew->an_name.bv_len - 1;
753                                         ocname.bv_val = &anew->an_name.bv_val[1];
754                                         anew->an_oc = oc_bvfind( &ocname );
755                                         if ( !anew->an_oc ) {
756                                                 free( an );
757                                                 /*
758                                                  * overwrites input string
759                                                  * on error!
760                                                  */
761                                                 strcpy( in, s );
762                                                 return NULL;
763                                         }
764                                 } break;
765
766                         default:
767                                 /* old (deprecated) way */
768                                 anew->an_oc = oc_bvfind( &anew->an_name );
769                                 if ( !anew->an_oc ) {
770                                         free( an );
771                                         /* overwrites input string on error! */
772                                         strcpy( in, s );
773                                         return NULL;
774                                 }
775                         }
776                 }
777                 anew++;
778         }
779
780         anew->an_name.bv_val = NULL;
781         free( str );
782         return( an );
783 }
784
785
786 /* Define an attribute option. */
787 int
788 ad_define_option( const char *name, const char *fname, int lineno )
789 {
790         int i;
791         unsigned int optlen;
792
793         if ( options == &lang_option ) {
794                 options = NULL;
795                 option_count = 0;
796         }
797         if ( name == NULL )
798                 return 0;
799
800         optlen = 0;
801         do {
802                 if ( !DESC_CHAR( name[optlen] ) ) {
803 #ifdef NEW_LOGGING
804                         LDAP_LOG( CONFIG, CRIT,
805                                   "%s: line %d: illegal option name \"%s\"\n",
806                                   fname, lineno, name );
807 #else
808                         Debug( LDAP_DEBUG_ANY,
809                                "%s: line %d: illegal option name \"%s\"\n",
810                                     fname, lineno, name );
811 #endif
812                         return 1;
813                 }
814         } while ( name[++optlen] );
815
816         options = ch_realloc( options,
817                 (option_count+1) * sizeof(Attr_option) );
818
819         if ( strcasecmp( name, "binary" ) == 0
820              || ad_find_option_definition( name, optlen ) ) {
821 #ifdef NEW_LOGGING
822                 LDAP_LOG( CONFIG, CRIT,
823                           "%s: line %d: option \"%s\" is already defined\n",
824                           fname, lineno, name );
825 #else
826                 Debug( LDAP_DEBUG_ANY,
827                        "%s: line %d: option \"%s\" is already defined\n",
828                        fname, lineno, name );
829 #endif
830                 return 1;
831         }
832
833         for ( i = option_count; i; --i ) {
834                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
835                         break;
836                 options[i] = options[i-1];
837         }
838
839         options[i].name.bv_val = ch_strdup( name );
840         options[i].name.bv_len = optlen;
841         options[i].prefix = (name[optlen-1] == '-');
842
843         if ( i != option_count &&
844              options[i].prefix &&
845              optlen < options[i+1].name.bv_len &&
846              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
847 #ifdef NEW_LOGGING
848                         LDAP_LOG( CONFIG, CRIT,
849                                   "%s: line %d: option \"%s\" overrides previous option\n",
850                                   fname, lineno, name );
851 #else
852                         Debug( LDAP_DEBUG_ANY,
853                                "%s: line %d: option \"%s\" overrides previous option\n",
854                                     fname, lineno, name );
855 #endif
856                         return 1;
857         }
858
859         option_count++;
860         return 0;
861 }
862
863 /* Find the definition of the option name or prefix matching the arguments */
864 static Attr_option *
865 ad_find_option_definition( const char *opt, int optlen )
866 {
867         int top = 0, bot = option_count;
868         while ( top < bot ) {
869                 int mid = (top + bot) / 2;
870                 int mlen = options[mid].name.bv_len;
871                 char *mname = options[mid].name.bv_val;
872                 int j;
873                 if ( optlen < mlen ) {
874                         j = strncasecmp( opt, mname, optlen ) - 1;
875                 } else {
876                         j = strncasecmp( opt, mname, mlen );
877                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
878                                 return &options[mid];
879                 }
880                 if ( j < 0 )
881                         bot = mid;
882                 else
883                         top = mid + 1;
884         }
885         return NULL;
886 }
887
888 MatchingRule *ad_mr(
889         AttributeDescription *ad,
890         unsigned usage )
891 {
892         switch( usage & SLAP_MR_TYPE_MASK ) {
893         case SLAP_MR_NONE:
894         case SLAP_MR_EQUALITY:
895                 return ad->ad_type->sat_equality;
896                 break;
897         case SLAP_MR_ORDERING:
898                 return ad->ad_type->sat_ordering;
899                 break;
900         case SLAP_MR_SUBSTR:
901                 return ad->ad_type->sat_substr;
902                 break;
903         case SLAP_MR_EXT:
904         default:
905                 assert( 0 /* ad_mr: bad usage */);
906         }
907         return NULL;
908 }