]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Per ITS#419, don't require SLAPD_RLOOKUPS when HAVE_TCPD
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* attr.c - routines for dealing with attributes */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #ifdef HAVE_FCNTL_H
13 #include <fcntl.h>
14 #endif
15
16 #include <ac/ctype.h>
17 #include <ac/errno.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap_pvt.h"
23 #include "slap.h"
24
25 #ifdef LDAP_DEBUG
26 static void at_index_print( void );
27 #endif
28
29 void
30 attr_free( Attribute *a )
31 {
32         free( a->a_type );
33         ber_bvecfree( a->a_vals );
34         free( a );
35 }
36
37 void
38 attrs_free( Attribute *a )
39 {
40         Attribute *next;
41
42         for( ; a != NULL ; a = next ) {
43                 next = a->a_next;
44                 attr_free( a );
45         }
46 }
47
48 Attribute *attr_dup( Attribute *a )
49 {
50         Attribute *tmp;
51
52         if( a == NULL) return NULL;
53
54         tmp = ch_malloc( sizeof(Attribute) );
55
56         if( a->a_vals != NULL ) {
57                 int i;
58
59                 for( i=0; a->a_vals[i] != NULL; i++ ) {
60                         /* EMPTY */ ;
61                 }
62
63                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
64
65                 for( i=0; a->a_vals[i] != NULL; i++ ) {
66                         tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
67
68                         if( tmp->a_vals[i] == NULL ) break;
69                 }
70
71                 tmp->a_vals[i] = NULL;
72
73         } else {
74                 tmp->a_vals = NULL;
75         }
76
77         tmp->a_type = ch_strdup( a->a_type );
78         tmp->a_syntax = a->a_syntax;
79         tmp->a_next = NULL;
80
81         return tmp;
82 }
83
84 Attribute *attrs_dup( Attribute *a )
85 {
86         Attribute *tmp, **next;
87
88         if( a == NULL ) return NULL;
89
90         tmp = NULL;
91         next = &tmp;
92
93         for( ; a != NULL ; a = a->a_next ) {
94                 *next = attr_dup( a );
95                 next = &((*next)->a_next);
96         }
97         *next = NULL;
98
99         return tmp;
100 }
101
102 /*
103  * attr_normalize - normalize an attribute name (make it all lowercase)
104  */
105
106 char *
107 attr_normalize( char *s )
108 {
109         assert( s != NULL );
110
111         return( ldap_pvt_str2lower( s ) );
112 }
113
114 /*
115  * attr_merge_fast - merge the given type and value with the list of
116  * attributes in attrs. called from str2entry(), where we can make some
117  * assumptions to make things faster.
118  * returns      0       everything went ok
119  *              -1      trouble
120  */
121
122 int
123 attr_merge_fast(
124     Entry               *e,
125     char                *type,
126     struct berval       **vals,
127     int                 nvals,
128     int                 naddvals,
129     int                 *maxvals,
130     Attribute           ***a
131 )
132 {
133         if ( *a == NULL ) {
134                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
135                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
136                                 break;
137                         }
138                 }
139         }
140
141         if ( **a == NULL ) {
142                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
143                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
144                 (**a)->a_vals = NULL;
145                 (**a)->a_syntax = attr_syntax( type );
146                 (**a)->a_next = NULL;
147         }
148
149         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
150             maxvals ) );
151 }
152
153 /*
154  * attr_merge - merge the given type and value with the list of
155  * attributes in attrs.
156  * returns      0       everything went ok
157  *              -1      trouble
158  */
159
160 int
161 attr_merge(
162     Entry               *e,
163     char                *type,
164     struct berval       **vals
165 )
166 {
167         Attribute       **a;
168
169         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
170                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
171                         break;
172                 }
173         }
174
175         if ( *a == NULL ) {
176                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
177                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
178                 (*a)->a_vals = NULL;
179                 (*a)->a_syntax = attr_syntax( type );
180                 (*a)->a_next = NULL;
181         }
182
183         return( value_add( &(*a)->a_vals, vals ) );
184 }
185
186 /*
187  * attr_find - find and return attribute type in list a
188  */
189
190 Attribute *
191 attr_find(
192     Attribute   *a,
193     const char  *type
194 )
195 {
196         for ( ; a != NULL; a = a->a_next ) {
197                 if ( strcasecmp( a->a_type, type ) == 0 ) {
198                         return( a );
199                 }
200         }
201
202         return( NULL );
203 }
204
205 /*
206  * attr_delete - delete the attribute type in list pointed to by attrs
207  * return       0       deleted ok
208  *              1       not found in list a
209  *              -1      something bad happened
210  */
211
212 int
213 attr_delete(
214     Attribute   **attrs,
215     const char  *type
216 )
217 {
218         Attribute       **a;
219         Attribute       *save;
220
221         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
222                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
223                         break;
224                 }
225         }
226
227         if ( *a == NULL ) {
228                 return( 1 );
229         }
230
231         save = *a;
232         *a = (*a)->a_next;
233         attr_free( save );
234
235         return( 0 );
236 }
237
238 #define DEFAULT_SYNTAX  SYNTAX_CIS
239
240 /*
241  * attr_syntax - return the syntax of attribute type
242  */
243
244 int
245 attr_syntax( char *type )
246 {
247         AttributeType   *sat;
248
249         sat = at_find(type);
250         if ( sat ) {
251                 return( sat->sat_syntax_compat );
252         }
253
254         return( DEFAULT_SYNTAX );
255 }
256
257 /*
258  * attr_syntax_config - process an attribute syntax config line
259  */
260
261 void
262 attr_syntax_config(
263     const char  *fname,
264     int         lineno,
265     int         argc,
266     char        **argv
267 )
268 {
269         char                    *save;
270         LDAP_ATTRIBUTE_TYPE     *at;
271         int                     lasti;
272         int                     code;
273         const char              *err;
274
275         if ( argc < 2 ) {
276                 Debug( LDAP_DEBUG_ANY,
277 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
278                     fname, lineno, 0 );
279                 return;
280         }
281
282         at = (LDAP_ATTRIBUTE_TYPE *)
283                 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
284
285         lasti = argc - 1;
286         if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
287             strcasecmp( argv[lasti], "cis" ) == 0 ) {
288                 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
289                 at->at_equality_oid = "2.5.13.2";
290                 at->at_ordering_oid = "2.5.13.3";
291                 at->at_substr_oid = "2.5.13.4";
292         } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
293             strcasecmp( argv[lasti], "tel" ) == 0 ) {
294                 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.50";
295                 at->at_equality_oid = "2.5.13.20";
296                 at->at_substr_oid = "2.5.13.21";
297         } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
298                 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.12";
299                 at->at_equality_oid = "2.5.13.1";
300         } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
301             strcasecmp( argv[lasti], "ces" ) == 0 ) {
302                 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.15";
303                 at->at_equality_oid = "2.5.13.5";
304                 at->at_ordering_oid = "2.5.13.6";
305                 at->at_substr_oid = "2.5.13.7";
306         } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
307             strcasecmp( argv[lasti], "bin" ) == 0 ) {
308                 at->at_syntax_oid = "1.3.6.1.4.1.1466.115.121.1.5";
309                 /* There is no match for binary syntax. Really */
310         } else {
311                 Debug( LDAP_DEBUG_ANY,
312             "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
313                     fname, lineno, argv[lasti] );
314                 Debug( LDAP_DEBUG_ANY,
315     "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
316                     0, 0, 0 );
317                 free( (AttributeType *) at );
318                 return;
319         }
320
321         save = argv[lasti];
322         argv[lasti] = NULL;
323         at->at_names = charray_dup( argv );
324         argv[lasti] = save;
325
326         code = at_add( at, &err );
327         if ( code ) {
328                 fprintf( stderr, "%s: line %d: %s %s\n",
329                          fname, lineno, scherr2str(code), err);
330                 exit( EXIT_FAILURE );
331         }
332         ldap_memfree(at);
333 }
334
335 int
336 at_fake_if_needed(
337     char        *name
338 )
339 {
340         char *argv[3];
341
342         if ( at_find( name ) ) {
343                 return 0;
344         } else {
345                 argv[0] = name;
346                 argv[1] = "cis";
347                 argv[2] = NULL;
348                 attr_syntax_config( "implicit", 0, 2, argv );
349                 return 0;
350         }
351 }
352
353 struct aindexrec {
354         char            *air_name;
355         AttributeType   *air_at;
356 };
357
358 static Avlnode  *attr_index = NULL;
359 static AttributeType *attr_list = NULL;
360
361 static int
362 attr_index_cmp(
363     struct aindexrec    *air1,
364     struct aindexrec    *air2
365 )
366 {
367         return (strcasecmp( air1->air_name, air2->air_name ));
368 }
369
370 static int
371 attr_index_name_cmp(
372     char                *type,
373     struct aindexrec    *air
374 )
375 {
376         return (strcasecmp( type, air->air_name ));
377 }
378
379 AttributeType *
380 at_find(
381     const char          *name
382 )
383 {
384         struct aindexrec        *air = NULL;
385         char                    *p, *tmpname = NULL;
386
387         /*
388          * The name may actually be an AttributeDescription, i.e. it may
389          * contain options.  Let's deal with it.
390          */
391         p = strchr( name, ';' );
392         if ( p ) {
393                 tmpname = ch_malloc( p-name+1 );
394                 strncpy( tmpname, name, p-name );
395                 tmpname[p-name] = '\0';
396         } else
397                 tmpname = (char *)name;
398         if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
399             (AVL_CMP) attr_index_name_cmp )) != NULL ) {
400                 if ( tmpname != name )
401                         ldap_memfree( tmpname );
402                 return( air->air_at );
403         }
404         if ( tmpname != name )
405                 ldap_memfree( tmpname );
406         return( NULL );
407 }
408
409 int
410 at_append_to_list(
411     AttributeType       *sat,
412     AttributeType       ***listp
413 )
414 {
415         AttributeType   **list;
416         AttributeType   **list1;
417         int             size;
418
419         list = *listp;
420         if ( !list ) {
421                 size = 2;
422                 list = calloc(size, sizeof(AttributeType *));
423                 if ( !list ) {
424                         return -1;
425                 }
426         } else {
427                 size = 0;
428                 list1 = *listp;
429                 while ( *list1 ) {
430                         size++;
431                         list1++;
432                 }
433                 size += 2;
434                 list1 = realloc(list, size*sizeof(AttributeType *));
435                 if ( !list1 ) {
436                         return -1;
437                 }
438                 list = list1;
439         }
440         list[size-2] = sat;
441         list[size-1] = NULL;
442         *listp = list;
443         return 0;
444 }
445
446 int
447 at_delete_from_list(
448     int                 pos,
449     AttributeType       ***listp
450 )
451 {
452         AttributeType   **list;
453         AttributeType   **list1;
454         int             i;
455         int             j;
456
457         if ( pos < 0 ) {
458                 return -2;
459         }
460         list = *listp;
461         for ( i=0; list[i]; i++ )
462                 ;
463         if ( pos >= i ) {
464                 return -2;
465         }
466         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
467                 list[i] = list[j];
468         }
469         list[i] = NULL;
470         /* Tell the runtime this can be shrinked */
471         list1 = realloc(list, (i+1)*sizeof(AttributeType **));
472         if ( !list1 ) {
473                 return -1;
474         }
475         *listp = list1;
476         return 0;
477 }
478
479 int
480 at_find_in_list(
481     AttributeType       *sat,
482     AttributeType       **list
483 )
484 {
485         int     i;
486
487         if ( !list ) {
488                 return -1;
489         }
490         for ( i=0; list[i]; i++ ) {
491                 if ( sat == list[i] ) {
492                         return i;
493                 }
494         }
495         return -1;
496 }
497
498 static int
499 at_insert(
500     AttributeType       *sat,
501     const char          **err
502 )
503 {
504         AttributeType           **atp;
505         struct aindexrec        *air;
506         char                    **names;
507
508         atp = &attr_list;
509         while ( *atp != NULL ) {
510                 atp = &(*atp)->sat_next;
511         }
512         *atp = sat;
513
514         if ( sat->sat_oid ) {
515                 air = (struct aindexrec *)
516                         ch_calloc( 1, sizeof(struct aindexrec) );
517                 air->air_name = sat->sat_oid;
518                 air->air_at = sat;
519                 if ( avl_insert( &attr_index, (caddr_t) air,
520                                  (AVL_CMP) attr_index_cmp,
521                                  (AVL_DUP) avl_dup_error ) ) {
522                         *err = sat->sat_oid;
523                         ldap_memfree(air);
524                         return SLAP_SCHERR_DUP_ATTR;
525                 }
526                 /* FIX: temporal consistency check */
527                 at_find(air->air_name);
528         }
529         if ( (names = sat->sat_names) ) {
530                 while ( *names ) {
531                         air = (struct aindexrec *)
532                                 ch_calloc( 1, sizeof(struct aindexrec) );
533                         air->air_name = ch_strdup(*names);
534                         air->air_at = sat;
535                         if ( avl_insert( &attr_index, (caddr_t) air,
536                                          (AVL_CMP) attr_index_cmp,
537                                          (AVL_DUP) avl_dup_error ) ) {
538                                 *err = *names;
539                                 ldap_memfree(air->air_name);
540                                 ldap_memfree(air);
541                                 return SLAP_SCHERR_DUP_ATTR;
542                         }
543                         /* FIX: temporal consistency check */
544                         at_find(air->air_name);
545                         names++;
546                 }
547         }
548
549         return 0;
550 }
551
552 int
553 at_add(
554     LDAP_ATTRIBUTE_TYPE *at,
555     const char          **err
556 )
557 {
558         AttributeType   *sat;
559         AttributeType   *sat1;
560         MatchingRule    *mr;
561         Syntax          *syn;
562         int             code;
563         char            *errattr;
564
565         if ( at->at_names && at->at_names[0] ) {
566                 errattr = at->at_names[0];
567         } else if ( at->at_oid ) {
568                 errattr = at->at_oid;
569         } else {
570                 errattr = "";
571                 return SLAP_SCHERR_ATTR_INCOMPLETE;
572         }
573         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
574         memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
575         if ( at->at_sup_oid ) {
576                 if ( (sat1 = at_find(at->at_sup_oid)) ) {
577                         sat->sat_sup = sat1;
578                         if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
579                                 *err = errattr;
580                                 return SLAP_SCHERR_OUTOFMEM;
581                         }
582                 } else {
583                         *err = at->at_sup_oid;
584                         return SLAP_SCHERR_ATTR_NOT_FOUND;
585                 }
586         }
587
588         if ( at->at_syntax_oid ) {
589                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
590                         sat->sat_syntax = syn;
591                 } else {
592                         *err = sat->sat_syntax_oid;
593                         return SLAP_SCHERR_SYN_NOT_FOUND;
594                 }
595                 if ( !strcmp(at->at_syntax_oid,
596                              "1.3.6.1.4.1.1466.115.121.1.15") ) {
597                         if ( at->at_equality_oid &&
598                              !strcmp(at->at_equality_oid, "2.5.13.5") ) {
599                                 sat->sat_syntax_compat = SYNTAX_CES;
600                         } else {
601                                 sat->sat_syntax_compat = SYNTAX_CIS;
602                         }
603                 } else if ( !strcmp(at->at_syntax_oid,
604                                     "1.3.6.1.4.1.1466.115.121.1.50") ) {
605                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
606                 } else if ( !strcmp(at->at_syntax_oid,
607                                     "1.3.6.1.4.1.1466.115.121.1.12") ) {
608                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
609                 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
610                         sat->sat_syntax_compat = SYNTAX_BIN;
611                 } else {
612                         sat->sat_syntax_compat = DEFAULT_SYNTAX;
613                 }
614         } else {
615                 sat->sat_syntax_compat = DEFAULT_SYNTAX;
616         }
617
618         if ( sat->sat_equality_oid ) {
619                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
620                         sat->sat_equality = mr;
621                 } else {
622                         *err = sat->sat_equality_oid;
623                         return SLAP_SCHERR_MR_NOT_FOUND;
624                 }
625         }
626         if ( sat->sat_ordering_oid ) {
627                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
628                         sat->sat_ordering = mr;
629                 } else {
630                         *err = sat->sat_ordering_oid;
631                         return SLAP_SCHERR_MR_NOT_FOUND;
632                 }
633         }
634         if ( sat->sat_substr_oid ) {
635                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
636                         sat->sat_substr = mr;
637                 } else {
638                         *err = sat->sat_substr_oid;
639                         return SLAP_SCHERR_MR_NOT_FOUND;
640                 }
641         }
642
643         /*
644          * Now inherit definitions from superiors.  We only check the
645          * direct superior since that one has already inherited from
646          * its own superiorss
647          */
648         if ( sat->sat_sup ) {
649                 if ( !sat->sat_syntax ) {
650                         sat->sat_syntax = sat->sat_sup->sat_syntax;
651                         sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
652                 }
653                 if ( !sat->sat_equality ) {
654                         sat->sat_equality = sat->sat_sup->sat_equality;
655                 }
656                 if ( !sat->sat_ordering ) {
657                         sat->sat_ordering = sat->sat_sup->sat_ordering;
658                 }
659                 if ( !sat->sat_substr ) {
660                         sat->sat_substr = sat->sat_sup->sat_substr;
661                 }
662         }
663         code = at_insert(sat,err);
664         return code;
665 }
666
667
668 char *
669 at_canonical_name( char * a_type )
670 {
671         AttributeType   *atp;
672
673         if ( (atp=at_find(a_type)) == NULL ) {
674
675                 return a_type;
676
677         } else  if ( atp->sat_names 
678                      && atp->sat_names[0]
679                      && (*(atp->sat_names[0]) != '\0') ) {
680             
681                 return atp->sat_names[0];
682
683         } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
684
685                 return atp->sat_oid;
686                 
687         } else {
688
689                 return a_type;
690
691         }
692
693 }/* char * at_canonica_name() */
694
695
696 #if defined( SLAPD_SCHEMA_DN )
697
698 int
699 at_schema_info( Entry *e )
700 {
701         struct berval   val;
702         struct berval   *vals[2];
703         AttributeType   *at;
704
705         vals[0] = &val;
706         vals[1] = NULL;
707
708         for ( at = attr_list; at; at = at->sat_next ) {
709                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
710                 if ( val.bv_val ) {
711                         val.bv_len = strlen( val.bv_val );
712                         Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
713                                (long) val.bv_len, val.bv_val, 0 );
714                         attr_merge( e, "attributeTypes", vals );
715                         ldap_memfree( val.bv_val );
716                 } else {
717                         return -1;
718                 }
719         }
720         return 0;
721 }
722 #endif
723
724 #ifdef LDAP_DEBUG
725
726 static int
727 at_index_printnode( struct aindexrec *air )
728 {
729
730         printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
731         return( 0 );
732 }
733
734 static void
735 at_index_print( void )
736 {
737         printf("Printing attribute type index:\n");
738         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
739                 0, -1, AVL_INORDER );
740 }
741
742 #endif