]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Never let ldif_parse_line() return a NULL value with success.
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ad.c - routines for dealing with attribute descriptions */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21 static int ad_keystring(
22         struct berval *bv )
23 {
24         ber_len_t i;
25
26         if( !AD_CHAR( bv->bv_val[0] ) ) {
27                 return 1;
28         }
29
30         for( i=1; i<bv->bv_len; i++ ) {
31                 if( !AD_CHAR( bv->bv_val[i] ) ) {
32                         return 1;
33                 }
34         }
35         return 0;
36 }
37
38 void ad_destroy( AttributeDescription *ad )
39 {
40         AttributeDescription *n;
41
42         for (; ad != NULL; ad = n) {
43                 n = ad->ad_next;
44                 ldap_memfree( ad );
45         }
46 }
47
48 /* Is there an AttributeDescription for this type that uses this language? */
49 AttributeDescription * ad_find_lang(
50         AttributeType *type,
51         struct berval *lang )
52 {
53         AttributeDescription *ad;
54
55         ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
56         for (ad = type->sat_ad; ad; ad=ad->ad_next)
57         {
58                 if (ad->ad_lang.bv_len == lang->bv_len &&
59                         !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
60                         break;
61         }
62         ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
63         return ad;
64 }
65
66 int slap_str2ad(
67         const char *str,
68         AttributeDescription **ad,
69         const char **text )
70 {
71         struct berval bv;
72         bv.bv_val = (char *) str;
73         bv.bv_len = strlen( str );
74
75         return slap_bv2ad( &bv, ad, text );
76 }
77
78 static char *strchrlen(
79         const char *p, 
80         const char ch, 
81         int *len )
82 {
83         int i;
84
85         for( i=0; p[i]; i++ ) {
86                 if( p[i] == ch ) {
87                         *len = i;
88                         return (char *) &p[i];
89                 }
90         }
91
92         *len = i;
93         return NULL;
94 }
95
96 int slap_bv2ad(
97         struct berval *bv,
98         AttributeDescription **ad,
99         const char **text )
100 {
101         int rtn = LDAP_UNDEFINED_TYPE;
102         AttributeDescription desc, *d2;
103         char *name, *options;
104         char *opt, *next;
105         int nlang;
106         int langlen;
107
108         /* hardcoded limits for speed */
109 #define MAX_LANG_OPTIONS 128
110         struct berval langs[MAX_LANG_OPTIONS+1];
111 #define MAX_LANG_LEN 1024
112         char langbuf[MAX_LANG_LEN];
113
114         assert( ad != NULL );
115         assert( *ad == NULL ); /* temporary */
116
117         if( bv == NULL || bv->bv_len == 0 ) {
118                 *text = "empty attribute description";
119                 return rtn;
120         }
121
122         /* make sure description is IA5 */
123         if( ad_keystring( bv ) ) {
124                 *text = "attribute description contains inappropriate characters";
125                 return rtn;
126         }
127
128         /* find valid base attribute type; parse in place */
129         memset( &desc, 0, sizeof( desc ));
130         desc.ad_cname = *bv;
131         name = bv->bv_val;
132         options = strchr(name, ';');
133         if( options != NULL ) {
134                 desc.ad_cname.bv_len = options - name;
135         }
136         desc.ad_type = at_bvfind( &desc.ad_cname );
137         if( desc.ad_type == NULL ) {
138                 *text = "attribute type undefined";
139                 return rtn;
140         }
141
142         if( is_at_operational( desc.ad_type ) && options != NULL ) {
143                 *text = "operational attribute with options undefined";
144                 return rtn;
145         }
146
147         /*
148          * parse options in place
149          */
150         nlang = 0;
151         memset( langs, 0, sizeof( langs ));
152         langlen = 0;
153
154         for( opt=options; opt != NULL; opt=next ) {
155                 int optlen;
156                 opt++; 
157                 next = strchrlen( opt, ';', &optlen );
158
159                 if( optlen == 0 ) {
160                         *text = "zero length option is invalid";
161                         return rtn;
162                 
163                 } else if ( optlen == sizeof("binary")-1 &&
164                         strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
165                 {
166                         /* binary option */
167                         if( slap_ad_is_binary( &desc ) ) {
168                                 *text = "option \"binary\" specified multiple times";
169                                 return rtn;
170                         }
171
172                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
173                                 /* not stored in binary, disallow option */
174                                 *text = "option \"binary\" not supported with type";
175                                 return rtn;
176                         }
177
178                         desc.ad_flags |= SLAP_DESC_BINARY;
179                         continue;
180
181                 } else if ( optlen >= sizeof("lang-")-1 &&
182                         strncasecmp( opt, "lang-", sizeof("lang-")-1 ) == 0 )
183                 {
184                         int i;
185
186                         if( opt[optlen-1] == '-' ) {
187                                 desc.ad_flags |= SLAP_DESC_LANG_RANGE;
188                         }
189
190                         if( nlang >= MAX_LANG_OPTIONS ) {
191                                 *text = "too many language options";
192                                 return rtn;
193                         }
194
195                         /*
196                          * tags should be presented in sorted order,
197                          * so run the array in reverse.
198                          */
199                         for( i=nlang-1; i>=0; i-- ) {
200                                 int rc;
201
202                                 rc = strncasecmp( opt, langs[i].bv_val,
203                                         (unsigned) optlen < langs[i].bv_len
204                                                 ? optlen : langs[i].bv_len );
205
206                                 if( rc == 0 && (unsigned)optlen == langs[i].bv_len ) {
207                                         /* duplicate (ignore) */
208                                         goto done;
209
210                                 } else if ( rc > 0 ||
211                                         ( rc == 0 && (unsigned)optlen > langs[i].bv_len ))
212                                 {
213                                         AC_MEMCPY( &langs[i+1], &langs[i],
214                                                 (nlang-i)*sizeof(struct berval) );
215                                         langs[i].bv_val = opt;
216                                         langs[i].bv_len = optlen;
217                                         goto done;
218                                 }
219                         }
220
221                         if( nlang ) {
222                                 AC_MEMCPY( &langs[1], &langs[0],
223                                         nlang*sizeof(struct berval) );
224                         }
225                         langs[0].bv_val = opt;
226                         langs[0].bv_len = optlen;
227
228 done:;
229                         langlen += optlen + 1;
230                         nlang++;
231
232                 } else {
233                         *text = "unrecognized option";
234                         return rtn;
235                 }
236         }
237
238         if( nlang > 0 ) {
239                 int i;
240
241                 if( langlen > MAX_LANG_LEN ) {
242                         *text = "language options too long";
243                         return rtn;
244                 }
245
246                 desc.ad_lang.bv_val = langbuf;
247                 langlen = 0;
248
249                 for( i=0; i<nlang; i++ ) {
250                         AC_MEMCPY( &desc.ad_lang.bv_val[langlen],
251                                 langs[i].bv_val, langs[i].bv_len );
252
253                         langlen += langs[i].bv_len;
254                         desc.ad_lang.bv_val[langlen++] = ';';
255                 }
256
257                 desc.ad_lang.bv_val[--langlen] = '\0';
258                 desc.ad_lang.bv_len = langlen;
259         }
260
261         /* see if a matching description is already cached */
262         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
263                 if( d2->ad_flags != desc.ad_flags ) {
264                         continue;
265                 }
266                 if( d2->ad_lang.bv_len != desc.ad_lang.bv_len ) {
267                         continue;
268                 }
269                 if( d2->ad_lang.bv_len == 0 ) {
270                         break;
271                 }
272                 if( strncasecmp( d2->ad_lang.bv_val, desc.ad_lang.bv_val,
273                         desc.ad_lang.bv_len ) == 0 )
274                 {
275                         break;
276                 }
277         }
278
279         /* Not found, add new one */
280         while (d2 == NULL) {
281                 size_t dlen = 0;
282                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
283                 /* check again now that we've locked */
284                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
285                         if (d2->ad_flags != desc.ad_flags)
286                                 continue;
287                         if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
288                                 continue;
289                         if (d2->ad_lang.bv_len == 0)
290                                 break;
291                         if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
292                                 desc.ad_lang.bv_len) == 0)
293                                 break;
294                 }
295                 if (d2) {
296                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
297                         break;
298                 }
299
300                 /* Allocate a single contiguous block. If there are no
301                  * options, we just need space for the AttrDesc structure.
302                  * Otherwise, we need to tack on the full name length +
303                  * options length.
304                  */
305                 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
306                         dlen = desc.ad_type->sat_cname.bv_len;
307                         if (desc.ad_lang.bv_len) {
308                                 dlen += 1+desc.ad_lang.bv_len;
309                         }
310                         if( slap_ad_is_binary( &desc ) ) {
311                                 dlen += sizeof(";binary")-1;
312                         }
313                 }
314
315                 d2 = ch_malloc(sizeof(AttributeDescription) + dlen + 1);
316                 d2->ad_type = desc.ad_type;
317                 d2->ad_flags = desc.ad_flags;
318                 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
319                 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
320
321                 if (dlen == 0) {
322                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
323                         d2->ad_lang.bv_val = NULL;
324                 } else {
325                         d2->ad_cname.bv_val = (char *)(d2+1);
326                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
327                         if( slap_ad_is_binary( &desc ) ) {
328                                 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
329                                         ";binary");
330                                 d2->ad_cname.bv_len += sizeof(";binary")-1;
331                         }
332                         if( d2->ad_lang.bv_len ) {
333                                 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
334                                 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
335                                         d2->ad_cname.bv_len;
336                                 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
337                                         d2->ad_lang.bv_len);
338                                 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
339                                 ldap_pvt_str2lower(d2->ad_lang.bv_val);
340                                 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
341                         }
342                 }
343                 /* Add new desc to list. We always want the bare Desc with
344                  * no options to stay at the head of the list, assuming
345                  * that one will be used most frequently.
346                  */
347                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
348                         d2->ad_next = desc.ad_type->sat_ad;
349                         desc.ad_type->sat_ad = d2;
350                 } else {
351                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
352                         desc.ad_type->sat_ad->ad_next = d2;
353                 }
354                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
355         }
356
357         if( *ad == NULL ) {
358                 *ad = d2;
359         } else {
360                 **ad = *d2;
361         }
362
363         return LDAP_SUCCESS;
364 }
365
366 static int is_ad_sublang(
367         struct berval *sublangbv, 
368         struct berval *suplangbv )
369 {
370         const char *suplang, *supp, *supdelimp;
371         const char *sublang, *subp, *subdelimp;
372         int  suplen, sublen;
373
374         if( suplangbv->bv_len == 0 ) return 1;
375         if( sublangbv->bv_len == 0 ) return 0;
376
377         sublang =sublangbv->bv_val;
378         suplang =suplangbv->bv_val;
379
380         for( supp=suplang ; supp; supp=supdelimp ) {
381                 supdelimp = strchrlen( supp, ';', &suplen );
382                 if( supdelimp ) supdelimp++;
383
384                 for( subp=sublang ; subp; subp=subdelimp ) {
385                         subdelimp = strchrlen( subp, ';', &sublen );
386                         if( subdelimp ) subdelimp++;
387
388                         if ( suplen > sublen
389                                  ? ( suplen-1 == sublen && supp[suplen-1] == '-'
390                                          && strncmp( supp, subp, sublen ) == 0 )
391                                  : ( ( suplen == sublen || supp[suplen-1] == '-' )
392                                          && strncmp( supp, subp, suplen ) == 0 ) )
393                         {
394                                 goto match;
395                         }
396                 }
397
398                 return 0;
399 match:;
400         }
401         return 1;
402 }
403
404 int is_ad_subtype(
405         AttributeDescription *sub,
406         AttributeDescription *super
407 )
408 {
409         int lr;
410
411         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
412                 return 0;
413         }
414
415         /* ensure sub does support all flags of super */
416         lr = sub->ad_lang.bv_len ? SLAP_DESC_LANG_RANGE : 0;
417         if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
418                 return 0;
419         }
420
421         /* check for language tags */
422         if ( !is_ad_sublang( &sub->ad_lang, &super->ad_lang )) {
423                 return 0;
424         }
425
426         return 1;
427 }
428
429 int ad_inlist(
430         AttributeDescription *desc,
431         AttributeName *attrs )
432 {
433         if (! attrs ) return 0;
434
435         for( ; attrs->an_name.bv_val; attrs++ ) {
436                 ObjectClass *oc;
437                 int rc;
438                 
439                 if ( attrs->an_desc ) {
440                         if ( desc == attrs->an_desc ) {
441                                 return 1;
442                         }
443
444                         /*
445                          * EXTENSION: if requested description is preceeded by an
446                          * a '-' character, do not match on subtypes.
447                          */
448                         if ( attrs->an_name.bv_val[0] != '-' &&
449                                 is_ad_subtype( desc, attrs->an_desc ))
450                         {
451                                 return 1;
452                         }
453
454                         continue;
455                 }
456
457                 /*
458                  * EXTENSION: see if requested description is +objectClass
459                  * if so, return attributes which the class requires/allows
460                  */
461                 oc = attrs->an_oc;
462                 if( oc == NULL && attrs->an_name.bv_val ) {
463                         switch( attrs->an_name.bv_val[0] ) {
464                         case '+': { /* new way */
465                                         struct berval ocname;
466                                         ocname.bv_len = attrs->an_name.bv_len - 1;
467                                         ocname.bv_val = &attrs->an_name.bv_val[1];
468                                         oc = oc_bvfind( &ocname );
469                                 } break;
470                         default: /* old (deprecated) way */
471                                 oc = oc_bvfind( &attrs->an_name );
472                         }
473                         attrs->an_oc = oc;
474                 }
475                 if( oc != NULL ) {
476                         if ( oc == slap_schema.si_oc_extensibleObject ) {
477                                 /* extensibleObject allows the return of anything */
478                                 return 1;
479                         }
480
481                         if( oc->soc_required ) {
482                                 /* allow return of required attributes */
483                                 int i;
484                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
485                                         rc = is_at_subtype( desc->ad_type,
486                                                 oc->soc_required[i] );
487                                         if( rc ) return 1;
488                                 }
489                         }
490
491                         if( oc->soc_allowed ) {
492                                 /* allow return of allowed attributes */
493                                 int i;
494                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
495                                         rc = is_at_subtype( desc->ad_type,
496                                                 oc->soc_allowed[i] );
497                                         if( rc ) return 1;
498                                 }
499                         }
500
501                 } else {
502                         /* short-circuit this search next time around */
503                         if (!slap_schema.si_at_undefined->sat_ad) {
504                                 const char *text;
505                                 slap_bv2undef_ad(&attrs->an_name,
506                                         &attrs->an_desc, &text);
507                         } else {
508                                 attrs->an_desc =
509                                         slap_schema.si_at_undefined->sat_ad;
510                         }
511                 }
512         }
513
514         return 0;
515 }
516
517
518 int slap_str2undef_ad(
519         const char *str,
520         AttributeDescription **ad,
521         const char **text )
522 {
523         struct berval bv;
524         bv.bv_val = (char *) str;
525         bv.bv_len = strlen( str );
526
527         return slap_bv2undef_ad( &bv, ad, text );
528 }
529
530 int slap_bv2undef_ad(
531         struct berval *bv,
532         AttributeDescription **ad,
533         const char **text )
534 {
535         AttributeDescription *desc;
536
537         assert( ad != NULL );
538
539         if( bv == NULL || bv->bv_len == 0 ) {
540                 *text = "empty attribute description";
541                 return LDAP_UNDEFINED_TYPE;
542         }
543
544         /* make sure description is IA5 */
545         if( ad_keystring( bv ) ) {
546                 *text = "attribute description contains inappropriate characters";
547                 return LDAP_UNDEFINED_TYPE;
548         }
549
550         for( desc = slap_schema.si_at_undefined->sat_ad; desc;
551                 desc=desc->ad_next ) 
552         {
553                 if( desc->ad_cname.bv_len == bv->bv_len &&
554                     !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
555                 {
556                         break;
557                 }
558         }
559         
560         if( !desc ) {
561                 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
562                         bv->bv_len);
563                 
564                 desc->ad_flags = SLAP_DESC_NONE;
565                 desc->ad_lang.bv_val = NULL;
566                 desc->ad_lang.bv_len = 0;
567
568                 desc->ad_cname.bv_len = bv->bv_len;
569                 desc->ad_cname.bv_val = (char *)(desc+1);
570                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
571
572                 /* canonical to upper case */
573                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
574
575                 desc->ad_type = slap_schema.si_at_undefined;
576                 desc->ad_next = desc->ad_type->sat_ad;
577                 desc->ad_type->sat_ad = desc;
578         }
579
580         if( !*ad ) {
581                 *ad = desc;
582         } else {
583                 **ad = *desc;
584         }
585
586         return LDAP_SUCCESS;
587 }
588
589 int
590 an_find(
591     AttributeName *a,
592     struct berval *s
593 )
594 {
595         if( a == NULL ) return 0;
596
597         for ( ; a->an_name.bv_val; a++ ) {
598                 if ( a->an_name.bv_len != s->bv_len) continue;
599                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
600                         return( 1 );
601                 }
602         }
603
604         return( 0 );
605 }
606
607 /*
608  * Convert a delimited string into a list of AttributeNames; 
609  * add on to an existing list if it was given.  If the string
610  * is not a valid attribute name, if a '-' is prepended it is 
611  * skipped and the remaining name is tried again; if a '+' is
612  * prepended, an objectclass name is searched instead.
613  * 
614  * NOTE: currently, if a valid attribute name is not found,
615  * the same string is also checked as valid objectclass name;
616  * however, this behavior is deprecated.
617  */
618 AttributeName *
619 str2anlist( AttributeName *an, char *in, const char *brkstr )
620 {
621         char    *str;
622         char    *s;
623         char    *lasts;
624         int     i, j;
625         const char *text;
626         AttributeName *anew;
627
628         /* find last element in list */
629         for (i = 0; an && an[i].an_name.bv_val; i++);
630         
631         /* protect the input string from strtok */
632         str = ch_strdup( in );
633
634         /* Count words in string */
635         j=1;
636         for ( s = str; *s; s++ ) {
637                 if ( strchr( brkstr, *s ) != NULL ) {
638                         j++;
639                 }
640         }
641
642         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
643         anew = an + i;
644         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
645                 s != NULL;
646                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
647         {
648                 anew->an_desc = NULL;
649                 anew->an_oc = NULL;
650                 ber_str2bv(s, 0, 1, &anew->an_name);
651                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
652                 if ( !anew->an_desc ) {
653                         switch( anew->an_name.bv_val[0] ) {
654                         case '-': {
655                                         struct berval adname;
656                                         adname.bv_len = anew->an_name.bv_len - 1;
657                                         adname.bv_val = &anew->an_name.bv_val[1];
658                                         slap_bv2ad(&adname, &anew->an_desc, &text);
659                                         if ( !anew->an_desc ) {
660                                                 free( an );
661                                                 /*
662                                                  * overwrites input string
663                                                  * on error!
664                                                  */
665                                                 strcpy( in, s );
666                                                 return NULL;
667                                         }
668                                 } break;
669
670                         case '+': {
671                                         struct berval ocname;
672                                         ocname.bv_len = anew->an_name.bv_len - 1;
673                                         ocname.bv_val = &anew->an_name.bv_val[1];
674                                         anew->an_oc = oc_bvfind( &ocname );
675                                         if ( !anew->an_oc ) {
676                                                 free( an );
677                                                 /*
678                                                  * overwrites input string
679                                                  * on error!
680                                                  */
681                                                 strcpy( in, s );
682                                                 return NULL;
683                                         }
684                                 } break;
685
686                         default:
687                                 /* old (deprecated) way */
688                                 anew->an_oc = oc_bvfind( &anew->an_name );
689                                 if ( !anew->an_oc ) {
690                                         free( an );
691                                         /* overwrites input string on error! */
692                                         strcpy( in, s );
693                                         return NULL;
694                                 }
695                         }
696                 }
697                 anew++;
698         }
699
700         anew->an_name.bv_val = NULL;
701         free( str );
702         return( an );
703 }
704