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