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