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