]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
honor '!' (objectClass negation) when checking attribute presence in list
[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 ( attrs->an_oc_exclude ) {
559                                 int gotit = 0;
560
561                                 if ( oc == slap_schema.si_oc_extensibleObject ) {
562                                         /* extensibleObject allows the return of anything */
563                                         return 0;
564                                 }
565
566                                 if( oc->soc_required ) {
567                                         /* allow return of required attributes */
568                                         int i;
569                                 
570                                         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
571                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
572                                                         if ( a == oc->soc_required[i] ) {
573                                                                 return 0;
574                                                         }
575                                                 }
576                                         }
577                                 }
578
579                                 if( oc->soc_allowed ) {
580                                         /* allow return of allowed attributes */
581                                         int i;
582                                         for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
583                                                 for (a = desc->ad_type; a; a=a->sat_sup) {
584                                                         if ( a == oc->soc_allowed[i] ) {
585                                                                 return 0;
586                                                         }
587                                                 }
588                                         }
589                                 }
590
591                                 return 1;
592                         }
593                         
594                         if ( oc == slap_schema.si_oc_extensibleObject ) {
595                                 /* extensibleObject allows the return of anything */
596                                 return 1;
597                         }
598
599                         if( oc->soc_required ) {
600                                 /* allow return of required attributes */
601                                 int i;
602                                 
603                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
604                                         for (a = desc->ad_type; a; a=a->sat_sup) {
605                                                 if ( a == oc->soc_required[i] ) {
606                                                         return 1;
607                                                 }
608                                         }
609                                 }
610                         }
611
612                         if( oc->soc_allowed ) {
613                                 /* allow return of allowed attributes */
614                                 int i;
615                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
616                                         for (a = desc->ad_type; a; a=a->sat_sup) {
617                                                 if ( a == oc->soc_allowed[i] ) {
618                                                         return 1;
619                                                 }
620                                         }
621                                 }
622                         }
623
624                 } else {
625                         /* short-circuit this search next time around */
626                         if (!slap_schema.si_at_undefined->sat_ad) {
627                                 const char *text;
628                                 slap_bv2undef_ad(&attrs->an_name,
629                                         &attrs->an_desc, &text);
630                         } else {
631                                 attrs->an_desc =
632                                         slap_schema.si_at_undefined->sat_ad;
633                         }
634                 }
635         }
636
637         return 0;
638 }
639
640
641 int slap_str2undef_ad(
642         const char *str,
643         AttributeDescription **ad,
644         const char **text )
645 {
646         struct berval bv;
647         bv.bv_val = (char *) str;
648         bv.bv_len = strlen( str );
649
650         return slap_bv2undef_ad( &bv, ad, text );
651 }
652
653 int slap_bv2undef_ad(
654         struct berval *bv,
655         AttributeDescription **ad,
656         const char **text )
657 {
658         AttributeDescription *desc;
659
660         assert( ad != NULL );
661
662         if( bv == NULL || bv->bv_len == 0 ) {
663                 *text = "empty attribute description";
664                 return LDAP_UNDEFINED_TYPE;
665         }
666
667         /* make sure description is IA5 */
668         if( ad_keystring( bv ) ) {
669                 *text = "attribute description contains inappropriate characters";
670                 return LDAP_UNDEFINED_TYPE;
671         }
672
673         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
674                 desc=desc->ad_next ) 
675         {
676                 if( desc->ad_cname.bv_len == bv->bv_len &&
677                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
678                 {
679                         break;
680                 }
681         }
682         
683         if( !desc ) {
684                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
685                         bv->bv_len);
686                 
687                 desc->ad_flags = SLAP_DESC_NONE;
688                 desc->ad_tags.bv_val = NULL;
689                 desc->ad_tags.bv_len = 0;
690
691                 desc->ad_cname.bv_len = bv->bv_len;
692                 desc->ad_cname.bv_val = (char *)(desc+1);
693                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
694
695                 /* canonical to upper case */
696                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
697
698                 desc->ad_type = slap_schema.si_at_undefined;
699                 desc->ad_next = desc->ad_type->sat_ad;
700                 desc->ad_type->sat_ad = desc;
701         }
702
703         if( !*ad ) {
704                 *ad = desc;
705         } else {
706                 **ad = *desc;
707         }
708
709         return LDAP_SUCCESS;
710 }
711
712 int
713 an_find(
714     AttributeName *a,
715     struct berval *s
716 )
717 {
718         if( a == NULL ) return 0;
719
720         for ( ; a->an_name.bv_val; a++ ) {
721                 if ( a->an_name.bv_len != s->bv_len) continue;
722                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
723                         return( 1 );
724                 }
725         }
726
727         return( 0 );
728 }
729
730 /*
731  * Convert a delimited string into a list of AttributeNames; 
732  * add on to an existing list if it was given.  If the string
733  * is not a valid attribute name, if a '-' is prepended it is 
734  * skipped and the remaining name is tried again; if a '+' is
735  * prepended, an objectclass name is searched instead; if a
736  * '!' is prepended, the objectclass name is negated.
737  * 
738  * NOTE: currently, if a valid attribute name is not found,
739  * the same string is also checked as valid objectclass name;
740  * however, this behavior is deprecated.
741  */
742 AttributeName *
743 str2anlist( AttributeName *an, char *in, const char *brkstr )
744 {
745         char    *str;
746         char    *s;
747         char    *lasts;
748         int     i, j;
749         const char *text;
750         AttributeName *anew;
751
752         /* find last element in list */
753         for (i = 0; an && an[i].an_name.bv_val; i++);
754         
755         /* protect the input string from strtok */
756         str = ch_strdup( in );
757
758         /* Count words in string */
759         j=1;
760         for ( s = str; *s; s++ ) {
761                 if ( strchr( brkstr, *s ) != NULL ) {
762                         j++;
763                 }
764         }
765
766         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
767         anew = an + i;
768         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
769                 s != NULL;
770                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
771         {
772                 anew->an_desc = NULL;
773                 anew->an_oc = NULL;
774                 anew->an_oc_exclude = 0;
775                 ber_str2bv(s, 0, 1, &anew->an_name);
776                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
777                 if ( !anew->an_desc ) {
778                         switch( anew->an_name.bv_val[0] ) {
779                         case '-': {
780                                         struct berval adname;
781                                         adname.bv_len = anew->an_name.bv_len - 1;
782                                         adname.bv_val = &anew->an_name.bv_val[1];
783                                         slap_bv2ad(&adname, &anew->an_desc, &text);
784                                         if ( !anew->an_desc ) {
785                                                 free( an );
786                                                 /*
787                                                  * overwrites input string
788                                                  * on error!
789                                                  */
790                                                 strcpy( in, s );
791                                                 return NULL;
792                                         }
793                                 } break;
794
795                         case '+':
796                         case '!': {
797                                         struct berval ocname;
798                                         ocname.bv_len = anew->an_name.bv_len - 1;
799                                         ocname.bv_val = &anew->an_name.bv_val[1];
800                                         anew->an_oc = oc_bvfind( &ocname );
801                                         if ( !anew->an_oc ) {
802                                                 free( an );
803                                                 /*
804                                                  * overwrites input string
805                                                  * on error!
806                                                  */
807                                                 strcpy( in, s );
808                                                 return NULL;
809                                         }
810
811                                         if ( anew->an_name.bv_val[0] == '!' ) {
812                                                 anew->an_oc_exclude = 1;
813                                         }
814                                 } break;
815
816                         default:
817                                 /* old (deprecated) way */
818                                 anew->an_oc = oc_bvfind( &anew->an_name );
819                                 if ( !anew->an_oc ) {
820                                         free( an );
821                                         /* overwrites input string on error! */
822                                         strcpy( in, s );
823                                         return NULL;
824                                 }
825                         }
826                 }
827                 anew++;
828         }
829
830         anew->an_name.bv_val = NULL;
831         free( str );
832         return( an );
833 }
834
835
836 /* Define an attribute option. */
837 int
838 ad_define_option( const char *name, const char *fname, int lineno )
839 {
840         int i;
841         unsigned int optlen;
842
843         if ( options == &lang_option ) {
844                 options = NULL;
845                 option_count = 0;
846         }
847         if ( name == NULL )
848                 return 0;
849
850         optlen = 0;
851         do {
852                 if ( !DESC_CHAR( name[optlen] ) ) {
853 #ifdef NEW_LOGGING
854                         LDAP_LOG( CONFIG, CRIT,
855                                   "%s: line %d: illegal option name \"%s\"\n",
856                                   fname, lineno, name );
857 #else
858                         Debug( LDAP_DEBUG_ANY,
859                                "%s: line %d: illegal option name \"%s\"\n",
860                                     fname, lineno, name );
861 #endif
862                         return 1;
863                 }
864         } while ( name[++optlen] );
865
866         options = ch_realloc( options,
867                 (option_count+1) * sizeof(Attr_option) );
868
869         if ( strcasecmp( name, "binary" ) == 0
870              || ad_find_option_definition( name, optlen ) ) {
871 #ifdef NEW_LOGGING
872                 LDAP_LOG( CONFIG, CRIT,
873                           "%s: line %d: option \"%s\" is already defined\n",
874                           fname, lineno, name );
875 #else
876                 Debug( LDAP_DEBUG_ANY,
877                        "%s: line %d: option \"%s\" is already defined\n",
878                        fname, lineno, name );
879 #endif
880                 return 1;
881         }
882
883         for ( i = option_count; i; --i ) {
884                 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
885                         break;
886                 options[i] = options[i-1];
887         }
888
889         options[i].name.bv_val = ch_strdup( name );
890         options[i].name.bv_len = optlen;
891         options[i].prefix = (name[optlen-1] == '-');
892
893         if ( i != option_count &&
894              options[i].prefix &&
895              optlen < options[i+1].name.bv_len &&
896              strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
897 #ifdef NEW_LOGGING
898                         LDAP_LOG( CONFIG, CRIT,
899                                   "%s: line %d: option \"%s\" overrides previous option\n",
900                                   fname, lineno, name );
901 #else
902                         Debug( LDAP_DEBUG_ANY,
903                                "%s: line %d: option \"%s\" overrides previous option\n",
904                                     fname, lineno, name );
905 #endif
906                         return 1;
907         }
908
909         option_count++;
910         return 0;
911 }
912
913 /* Find the definition of the option name or prefix matching the arguments */
914 static Attr_option *
915 ad_find_option_definition( const char *opt, int optlen )
916 {
917         int top = 0, bot = option_count;
918         while ( top < bot ) {
919                 int mid = (top + bot) / 2;
920                 int mlen = options[mid].name.bv_len;
921                 char *mname = options[mid].name.bv_val;
922                 int j;
923                 if ( optlen < mlen ) {
924                         j = strncasecmp( opt, mname, optlen ) - 1;
925                 } else {
926                         j = strncasecmp( opt, mname, mlen );
927                         if ( j==0 && (optlen==mlen || options[mid].prefix) )
928                                 return &options[mid];
929                 }
930                 if ( j < 0 )
931                         bot = mid;
932                 else
933                         top = mid + 1;
934         }
935         return NULL;
936 }
937
938 MatchingRule *ad_mr(
939         AttributeDescription *ad,
940         unsigned usage )
941 {
942         switch( usage & SLAP_MR_TYPE_MASK ) {
943         case SLAP_MR_NONE:
944         case SLAP_MR_EQUALITY:
945                 return ad->ad_type->sat_equality;
946                 break;
947         case SLAP_MR_ORDERING:
948                 return ad->ad_type->sat_ordering;
949                 break;
950         case SLAP_MR_SUBSTR:
951                 return ad->ad_type->sat_substr;
952                 break;
953         case SLAP_MR_EXT:
954         default:
955                 assert( 0 /* ad_mr: bad usage */);
956         }
957         return NULL;
958 }