]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
569e995ccbc5f5ecbb7afd319c0fe74caa919f24
[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+1], &tags[i],
226                                                 (ntags-i)*sizeof(struct berval) );
227                                         tags[i].bv_val = opt;
228                                         tags[i].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         if( suptagsbv->bv_len == 0 ) return 1;
414         if( subtagsbv->bv_len == 0 ) return 0;
415
416         subtags =subtagsbv->bv_val;
417         suptags =suptagsbv->bv_val;
418
419         for( supp=suptags ; supp; supp=supdelimp ) {
420                 supdelimp = strchrlen( supp, ';', &suplen );
421                 if( supdelimp ) supdelimp++;
422
423                 for( subp=subtags ; subp; subp=subdelimp ) {
424                         subdelimp = strchrlen( subp, ';', &sublen );
425                         if( subdelimp ) subdelimp++;
426
427                         if ( suplen > sublen
428                                  ? ( suplen-1 == sublen && supp[suplen-1] == '-'
429                                          && strncmp( supp, subp, sublen ) == 0 )
430                                  : ( ( suplen == sublen || supp[suplen-1] == '-' )
431                                          && strncmp( supp, subp, suplen ) == 0 ) )
432                         {
433                                 goto match;
434                         }
435                 }
436
437                 return 0;
438 match:;
439         }
440         return 1;
441 }
442
443 int is_ad_subtype(
444         AttributeDescription *sub,
445         AttributeDescription *super
446 )
447 {
448         int lr;
449
450         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
451                 return 0;
452         }
453
454         /* ensure sub does support all flags of super */
455         lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
456         if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
457                 return 0;
458         }
459
460         /* check for tagging options */
461         if ( !is_ad_subtags( &sub->ad_tags, &super->ad_tags )) {
462                 return 0;
463         }
464
465         return 1;
466 }
467
468 int ad_inlist(
469         AttributeDescription *desc,
470         AttributeName *attrs )
471 {
472         if (! attrs ) return 0;
473
474         for( ; attrs->an_name.bv_val; attrs++ ) {
475                 ObjectClass *oc;
476                 int rc;
477                 
478                 if ( attrs->an_desc ) {
479                         if ( desc == attrs->an_desc ) {
480                                 return 1;
481                         }
482
483                         /*
484                          * EXTENSION: if requested description is preceeded by an
485                          * a '-' character, do not match on subtypes.
486                          */
487                         if ( attrs->an_name.bv_val[0] != '-' &&
488                                 is_ad_subtype( desc, attrs->an_desc ))
489                         {
490                                 return 1;
491                         }
492
493                         continue;
494                 }
495
496                 /*
497                  * EXTENSION: see if requested description is +objectClass
498                  * if so, return attributes which the class requires/allows
499                  */
500                 oc = attrs->an_oc;
501                 if( oc == NULL && attrs->an_name.bv_val ) {
502                         switch( attrs->an_name.bv_val[0] ) {
503                         case '+': { /* new way */
504                                         struct berval ocname;
505                                         ocname.bv_len = attrs->an_name.bv_len - 1;
506                                         ocname.bv_val = &attrs->an_name.bv_val[1];
507                                         oc = oc_bvfind( &ocname );
508                                 } break;
509                         default: /* old (deprecated) way */
510                                 oc = oc_bvfind( &attrs->an_name );
511                         }
512                         attrs->an_oc = oc;
513                 }
514                 if( oc != NULL ) {
515                         if ( oc == slap_schema.si_oc_extensibleObject ) {
516                                 /* extensibleObject allows the return of anything */
517                                 return 1;
518                         }
519
520                         if( oc->soc_required ) {
521                                 /* allow return of required attributes */
522                                 int i;
523                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
524                                         rc = is_at_subtype( desc->ad_type,
525                                                 oc->soc_required[i] );
526                                         if( rc ) return 1;
527                                 }
528                         }
529
530                         if( oc->soc_allowed ) {
531                                 /* allow return of allowed attributes */
532                                 int i;
533                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
534                                         rc = is_at_subtype( desc->ad_type,
535                                                 oc->soc_allowed[i] );
536                                         if( rc ) return 1;
537                                 }
538                         }
539
540                 } else {
541                         /* short-circuit this search next time around */
542                         if (!slap_schema.si_at_undefined->sat_ad) {
543                                 const char *text;
544                                 slap_bv2undef_ad(&attrs->an_name,
545                                         &attrs->an_desc, &text);
546                         } else {
547                                 attrs->an_desc =
548                                         slap_schema.si_at_undefined->sat_ad;
549                         }
550                 }
551         }
552
553         return 0;
554 }
555
556
557 int slap_str2undef_ad(
558         const char *str,
559         AttributeDescription **ad,
560         const char **text )
561 {
562         struct berval bv;
563         bv.bv_val = (char *) str;
564         bv.bv_len = strlen( str );
565
566         return slap_bv2undef_ad( &bv, ad, text );
567 }
568
569 int slap_bv2undef_ad(
570         struct berval *bv,
571         AttributeDescription **ad,
572         const char **text )
573 {
574         AttributeDescription *desc;
575
576         assert( ad != NULL );
577
578         if( bv == NULL || bv->bv_len == 0 ) {
579                 *text = "empty attribute description";
580                 return LDAP_UNDEFINED_TYPE;
581         }
582
583         /* make sure description is IA5 */
584         if( ad_keystring( bv ) ) {
585                 *text = "attribute description contains inappropriate characters";
586                 return LDAP_UNDEFINED_TYPE;
587         }
588
589         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
590                 desc=desc->ad_next ) 
591         {
592                 if( desc->ad_cname.bv_len == bv->bv_len &&
593                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
594                 {
595                         break;
596                 }
597         }
598         
599         if( !desc ) {
600                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
601                         bv->bv_len);
602                 
603                 desc->ad_flags = SLAP_DESC_NONE;
604                 desc->ad_tags.bv_val = NULL;
605                 desc->ad_tags.bv_len = 0;
606
607                 desc->ad_cname.bv_len = bv->bv_len;
608                 desc->ad_cname.bv_val = (char *)(desc+1);
609                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
610
611                 /* canonical to upper case */
612                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
613
614                 desc->ad_type = slap_schema.si_at_undefined;
615                 desc->ad_next = desc->ad_type->sat_ad;
616                 desc->ad_type->sat_ad = desc;
617         }
618
619         if( !*ad ) {
620                 *ad = desc;
621         } else {
622                 **ad = *desc;
623         }
624
625         return LDAP_SUCCESS;
626 }
627
628 int
629 an_find(
630     AttributeName *a,
631     struct berval *s
632 )
633 {
634         if( a == NULL ) return 0;
635
636         for ( ; a->an_name.bv_val; a++ ) {
637                 if ( a->an_name.bv_len != s->bv_len) continue;
638                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
639                         return( 1 );
640                 }
641         }
642
643         return( 0 );
644 }
645
646 /*
647  * Convert a delimited string into a list of AttributeNames; 
648  * add on to an existing list if it was given.  If the string
649  * is not a valid attribute name, if a '-' is prepended it is 
650  * skipped and the remaining name is tried again; if a '+' is
651  * prepended, an objectclass name is searched instead.
652  * 
653  * NOTE: currently, if a valid attribute name is not found,
654  * the same string is also checked as valid objectclass name;
655  * however, this behavior is deprecated.
656  */
657 AttributeName *
658 str2anlist( AttributeName *an, char *in, const char *brkstr )
659 {
660         char    *str;
661         char    *s;
662         char    *lasts;
663         int     i, j;
664         const char *text;
665         AttributeName *anew;
666
667         /* find last element in list */
668         for (i = 0; an && an[i].an_name.bv_val; i++);
669         
670         /* protect the input string from strtok */
671         str = ch_strdup( in );
672
673         /* Count words in string */
674         j=1;
675         for ( s = str; *s; s++ ) {
676                 if ( strchr( brkstr, *s ) != NULL ) {
677                         j++;
678                 }
679         }
680
681         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
682         anew = an + i;
683         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
684                 s != NULL;
685                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
686         {
687                 anew->an_desc = NULL;
688                 anew->an_oc = NULL;
689                 ber_str2bv(s, 0, 1, &anew->an_name);
690                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
691                 if ( !anew->an_desc ) {
692                         switch( anew->an_name.bv_val[0] ) {
693                         case '-': {
694                                         struct berval adname;
695                                         adname.bv_len = anew->an_name.bv_len - 1;
696                                         adname.bv_val = &anew->an_name.bv_val[1];
697                                         slap_bv2ad(&adname, &anew->an_desc, &text);
698                                         if ( !anew->an_desc ) {
699                                                 free( an );
700                                                 /*
701                                                  * overwrites input string
702                                                  * on error!
703                                                  */
704                                                 strcpy( in, s );
705                                                 return NULL;
706                                         }
707                                 } break;
708
709                         case '+': {
710                                         struct berval ocname;
711                                         ocname.bv_len = anew->an_name.bv_len - 1;
712                                         ocname.bv_val = &anew->an_name.bv_val[1];
713                                         anew->an_oc = oc_bvfind( &ocname );
714                                         if ( !anew->an_oc ) {
715                                                 free( an );
716                                                 /*
717                                                  * overwrites input string
718                                                  * on error!
719                                                  */
720                                                 strcpy( in, s );
721                                                 return NULL;
722                                         }
723                                 } break;
724
725                         default:
726                                 /* old (deprecated) way */
727                                 anew->an_oc = oc_bvfind( &anew->an_name );
728                                 if ( !anew->an_oc ) {
729                                         free( an );
730                                         /* overwrites input string on error! */
731                                         strcpy( in, s );
732                                         return NULL;
733                                 }
734                         }
735                 }
736                 anew++;
737         }
738
739         anew->an_name.bv_val = NULL;
740         free( str );
741         return( an );
742 }
743
744
745 /* Define an attribute option. */
746 int
747 ad_define_option( const char *name, const char *fname, int lineno )
748 {
749         int i;
750         unsigned int optlen;
751
752         if ( options == &lang_option ) {
753                 options = NULL;
754                 option_count = 0;
755         }
756         if ( name == NULL )
757                 return 0;
758
759         optlen = 0;
760         do {
761                 if ( !DESC_CHAR( name[optlen] ) ) {
762 #ifdef NEW_LOGGING
763                         LDAP_LOG( CONFIG, CRIT,
764                                   "%s: line %d: illegal option name \"%s\"\n",
765                                   fname, lineno, name );
766 #else
767                         Debug( LDAP_DEBUG_ANY,
768                                "%s: line %d: illegal option name \"%s\"\n",
769                                     fname, lineno, name );
770 #endif
771                         return 1;
772                 }
773         } while ( name[++optlen] );
774
775         options = ch_realloc( options,
776                 (option_count+1) * sizeof(Attr_option) );
777
778         if ( strcasecmp( name, "binary" ) == 0
779              || ad_find_option_definition( name, optlen ) ) {
780 #ifdef NEW_LOGGING
781                 LDAP_LOG( CONFIG, CRIT,
782                           "%s: line %d: option \"%s\" is already defined\n",
783                           fname, lineno, name );
784 #else
785                 Debug( LDAP_DEBUG_ANY,
786                        "%s: line %d: option \"%s\" is already defined\n",
787                        fname, lineno, name );
788 #endif
789                 return 1;
790         }
791
792         for ( i = option_count; i; --i ) {
793                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
794                         break;
795                 options[i] = options[i-1];
796         }
797
798         options[i].name.bv_val = ch_strdup( name );
799         options[i].name.bv_len = optlen;
800         options[i].prefix = (name[optlen-1] == '-');
801
802         if ( i != option_count &&
803              options[i].prefix &&
804              optlen < options[i+1].name.bv_len &&
805              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
806 #ifdef NEW_LOGGING
807                         LDAP_LOG( CONFIG, CRIT,
808                                   "%s: line %d: option \"%s\" overrides previous option\n",
809                                   fname, lineno, name );
810 #else
811                         Debug( LDAP_DEBUG_ANY,
812                                "%s: line %d: option \"%s\" overrides previous option\n",
813                                     fname, lineno, name );
814 #endif
815                         return 1;
816         }
817
818         option_count++;
819         return 0;
820 }
821
822 /* Find the definition of the option name or prefix matching the arguments */
823 static Attr_option *
824 ad_find_option_definition( const char *opt, int optlen )
825 {
826         int top = 0, bot = option_count;
827         while ( top < bot ) {
828                 int mid = (top + bot) / 2;
829                 int mlen = options[mid].name.bv_len;
830                 char *mname = options[mid].name.bv_val;
831                 int j;
832                 if ( optlen < mlen ) {
833                         j = strncasecmp( opt, mname, optlen ) - 1;
834                 } else {
835                         j = strncasecmp( opt, mname, mlen );
836                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
837                                 return &options[mid];
838                 }
839                 if ( j < 0 )
840                         bot = mid;
841                 else
842                         top = mid + 1;
843         }
844         return NULL;
845 }
846
847 MatchingRule *ad_mr(
848         AttributeDescription *ad,
849         unsigned usage )
850 {
851         switch( usage & SLAP_MR_TYPE_MASK ) {
852         case SLAP_MR_NONE:
853         case SLAP_MR_EQUALITY:
854                 return ad->ad_type->sat_equality;
855                 break;
856         case SLAP_MR_ORDERING:
857                 return ad->ad_type->sat_ordering;
858                 break;
859         case SLAP_MR_SUBSTR:
860                 return ad->ad_type->sat_substr;
861                 break;
862         case SLAP_MR_EXT:
863         default:
864                 assert( 0 /* ad_mr: bad usage */);
865         }
866         return NULL;
867 }