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