]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
4d94aced307ccb7f85d5bbaa55b8048afd9cbb41
[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 #define SYNTAX_DS_OID   "1.3.6.1.4.1.1466.115.121.1.15"
286 #define SYNTAX_DSCE_OID "2.5.13.5"
287 #define SYNTAX_IA5_OID  "1.3.6.1.4.1.1466.115.121.1.26"
288 #define SYNTAX_IA5CE_OID        "1.3.6.1.4.1.1466.109.114.1"
289 #define SYNTAX_DN_OID   "1.3.6.1.4.1.1466.115.121.1.12"
290 #define SYNTAX_TEL_OID  "1.3.6.1.4.1.1466.115.121.1.50"
291 #define SYNTAX_BIN_OID  "1.3.6.1.4.1.1466.115.121.1.40" /* octetString */
292
293         lasti = argc - 1;
294         if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
295             strcasecmp( argv[lasti], "cis" ) == 0 ) {
296                 at->at_syntax_oid = SYNTAX_DS_OID;
297                 at->at_equality_oid = "2.5.13.2";
298                 at->at_ordering_oid = "2.5.13.3";
299                 at->at_substr_oid = "2.5.13.4";
300
301         } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
302             strcasecmp( argv[lasti], "tel" ) == 0 ) {
303                 at->at_syntax_oid = SYNTAX_TEL_OID;
304                 at->at_equality_oid = "2.5.13.20";
305                 at->at_substr_oid = "2.5.13.21";
306
307         } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
308                 at->at_syntax_oid = SYNTAX_DN_OID;
309                 at->at_equality_oid = "2.5.13.1";
310
311         } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
312             strcasecmp( argv[lasti], "ces" ) == 0 ) {
313                 at->at_syntax_oid = SYNTAX_DS_OID;
314                 at->at_equality_oid = SYNTAX_DSCE_OID;
315                 at->at_ordering_oid = "2.5.13.6";
316                 at->at_substr_oid = "2.5.13.7";
317
318         } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
319             strcasecmp( argv[lasti], "bin" ) == 0 ) {
320                 /* bin -> octetString, not binary! */
321                 at->at_syntax_oid = SYNTAX_BIN_OID;
322                 at->at_equality_oid = "2.5.13.17";
323
324         } else {
325                 Debug( LDAP_DEBUG_ANY,
326             "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
327                     fname, lineno, argv[lasti] );
328                 Debug( LDAP_DEBUG_ANY,
329     "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
330                     0, 0, 0 );
331                 free( (AttributeType *) at );
332                 return;
333         }
334
335         save = argv[lasti];
336         argv[lasti] = NULL;
337         at->at_names = charray_dup( argv );
338         argv[lasti] = save;
339
340         code = at_add( at, &err );
341         if ( code ) {
342                 fprintf( stderr, "%s: line %d: %s %s\n",
343                          fname, lineno, scherr2str(code), err);
344                 exit( EXIT_FAILURE );
345         }
346         ldap_memfree(at);
347 }
348
349 int
350 at_fake_if_needed(
351     char        *name
352 )
353 {
354         char *argv[3];
355
356         if ( at_find( name ) ) {
357                 return 0;
358         } else {
359                 argv[0] = name;
360                 argv[1] = "cis";
361                 argv[2] = NULL;
362                 attr_syntax_config( "implicit", 0, 2, argv );
363                 return 0;
364         }
365 }
366
367 struct aindexrec {
368         char            *air_name;
369         AttributeType   *air_at;
370 };
371
372 static Avlnode  *attr_index = NULL;
373 static AttributeType *attr_list = NULL;
374
375 static int
376 attr_index_cmp(
377     struct aindexrec    *air1,
378     struct aindexrec    *air2
379 )
380 {
381         return (strcasecmp( air1->air_name, air2->air_name ));
382 }
383
384 static int
385 attr_index_name_cmp(
386     char                *type,
387     struct aindexrec    *air
388 )
389 {
390         return (strcasecmp( type, air->air_name ));
391 }
392
393 AttributeType *
394 at_find(
395     const char          *name
396 )
397 {
398         struct aindexrec        *air = NULL;
399         char                    *p, *tmpname = NULL;
400
401         /*
402          * The name may actually be an AttributeDescription, i.e. it may
403          * contain options.  Let's deal with it.
404          */
405         p = strchr( name, ';' );
406         if ( p ) {
407                 tmpname = ch_malloc( p-name+1 );
408                 strncpy( tmpname, name, p-name );
409                 tmpname[p-name] = '\0';
410         } else
411                 tmpname = (char *)name;
412         if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
413             (AVL_CMP) attr_index_name_cmp )) != NULL ) {
414                 if ( tmpname != name )
415                         ldap_memfree( tmpname );
416                 return( air->air_at );
417         }
418         if ( tmpname != name )
419                 ldap_memfree( tmpname );
420         return( NULL );
421 }
422
423 int
424 at_append_to_list(
425     AttributeType       *sat,
426     AttributeType       ***listp
427 )
428 {
429         AttributeType   **list;
430         AttributeType   **list1;
431         int             size;
432
433         list = *listp;
434         if ( !list ) {
435                 size = 2;
436                 list = calloc(size, sizeof(AttributeType *));
437                 if ( !list ) {
438                         return -1;
439                 }
440         } else {
441                 size = 0;
442                 list1 = *listp;
443                 while ( *list1 ) {
444                         size++;
445                         list1++;
446                 }
447                 size += 2;
448                 list1 = realloc(list, size*sizeof(AttributeType *));
449                 if ( !list1 ) {
450                         return -1;
451                 }
452                 list = list1;
453         }
454         list[size-2] = sat;
455         list[size-1] = NULL;
456         *listp = list;
457         return 0;
458 }
459
460 int
461 at_delete_from_list(
462     int                 pos,
463     AttributeType       ***listp
464 )
465 {
466         AttributeType   **list;
467         AttributeType   **list1;
468         int             i;
469         int             j;
470
471         if ( pos < 0 ) {
472                 return -2;
473         }
474         list = *listp;
475         for ( i=0; list[i]; i++ )
476                 ;
477         if ( pos >= i ) {
478                 return -2;
479         }
480         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
481                 list[i] = list[j];
482         }
483         list[i] = NULL;
484         /* Tell the runtime this can be shrinked */
485         list1 = realloc(list, (i+1)*sizeof(AttributeType **));
486         if ( !list1 ) {
487                 return -1;
488         }
489         *listp = list1;
490         return 0;
491 }
492
493 int
494 at_find_in_list(
495     AttributeType       *sat,
496     AttributeType       **list
497 )
498 {
499         int     i;
500
501         if ( !list ) {
502                 return -1;
503         }
504         for ( i=0; list[i]; i++ ) {
505                 if ( sat == list[i] ) {
506                         return i;
507                 }
508         }
509         return -1;
510 }
511
512 static int
513 at_insert(
514     AttributeType       *sat,
515     const char          **err
516 )
517 {
518         AttributeType           **atp;
519         struct aindexrec        *air;
520         char                    **names;
521
522         atp = &attr_list;
523         while ( *atp != NULL ) {
524                 atp = &(*atp)->sat_next;
525         }
526         *atp = sat;
527
528         if ( sat->sat_oid ) {
529                 air = (struct aindexrec *)
530                         ch_calloc( 1, sizeof(struct aindexrec) );
531                 air->air_name = sat->sat_oid;
532                 air->air_at = sat;
533                 if ( avl_insert( &attr_index, (caddr_t) air,
534                                  (AVL_CMP) attr_index_cmp,
535                                  (AVL_DUP) avl_dup_error ) ) {
536                         *err = sat->sat_oid;
537                         ldap_memfree(air);
538                         return SLAP_SCHERR_DUP_ATTR;
539                 }
540                 /* FIX: temporal consistency check */
541                 at_find(air->air_name);
542         }
543         if ( (names = sat->sat_names) ) {
544                 while ( *names ) {
545                         air = (struct aindexrec *)
546                                 ch_calloc( 1, sizeof(struct aindexrec) );
547                         air->air_name = ch_strdup(*names);
548                         air->air_at = sat;
549                         if ( avl_insert( &attr_index, (caddr_t) air,
550                                          (AVL_CMP) attr_index_cmp,
551                                          (AVL_DUP) avl_dup_error ) ) {
552                                 *err = *names;
553                                 ldap_memfree(air->air_name);
554                                 ldap_memfree(air);
555                                 return SLAP_SCHERR_DUP_ATTR;
556                         }
557                         /* FIX: temporal consistency check */
558                         at_find(air->air_name);
559                         names++;
560                 }
561         }
562
563         return 0;
564 }
565
566 int
567 at_add(
568     LDAP_ATTRIBUTE_TYPE *at,
569     const char          **err
570 )
571 {
572         AttributeType   *sat;
573         AttributeType   *sat1;
574         MatchingRule    *mr;
575         Syntax          *syn;
576         int             code;
577         char            *errattr;
578
579         if ( at->at_names && at->at_names[0] ) {
580                 errattr = at->at_names[0];
581         } else if ( at->at_oid ) {
582                 errattr = at->at_oid;
583         } else {
584                 errattr = "";
585                 return SLAP_SCHERR_ATTR_INCOMPLETE;
586         }
587         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
588         memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
589
590         if ( at->at_sup_oid ) {
591                 if ( (sat1 = at_find(at->at_sup_oid)) ) {
592                         sat->sat_sup = sat1;
593                         if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
594                                 *err = errattr;
595                                 return SLAP_SCHERR_OUTOFMEM;
596                         }
597                 } else {
598                         *err = at->at_sup_oid;
599                         return SLAP_SCHERR_ATTR_NOT_FOUND;
600                 }
601         }
602
603         /*
604          * Inherit definitions from superiors.  We only check the
605          * direct superior since that one has already inherited from
606          * its own superiorss
607          */
608         if ( sat->sat_sup ) {
609                 sat->sat_syntax = sat->sat_sup->sat_syntax;
610
611                 sat->sat_equality = sat->sat_sup->sat_equality;
612                 sat->sat_ordering = sat->sat_sup->sat_ordering;
613                 sat->sat_substr = sat->sat_sup->sat_substr;
614         }
615
616         if ( at->at_syntax_oid ) {
617                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
618                         sat->sat_syntax = syn;
619                 } else {
620                         *err = sat->sat_syntax_oid;
621                         return SLAP_SCHERR_SYN_NOT_FOUND;
622                 }
623
624                 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
625                         if ( at->at_equality_oid && (
626                                 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
627                         {
628                                 sat->sat_syntax_compat = SYNTAX_CES;
629                         } else {
630                                 sat->sat_syntax_compat = SYNTAX_CIS;
631                         }
632
633                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
634                         if ( at->at_equality_oid && (
635                                 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
636                         {
637                                 sat->sat_syntax_compat = SYNTAX_CES;
638                         } else {
639                                 sat->sat_syntax_compat = SYNTAX_CIS;
640                         }
641
642                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
643                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
644
645                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
646                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
647
648                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
649                         sat->sat_syntax_compat = SYNTAX_BIN;
650
651                 } else {
652                         sat->sat_syntax_compat = DEFAULT_SYNTAX;
653                 }
654
655         } else if ( sat->sat_syntax == NULL ) {
656                 return SLAP_SCHERR_ATTR_INCOMPLETE;
657         }
658
659         if ( sat->sat_equality_oid ) {
660                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
661                         sat->sat_equality = mr;
662                 } else {
663                         *err = sat->sat_equality_oid;
664                         return SLAP_SCHERR_MR_NOT_FOUND;
665                 }
666
667         }
668
669         if ( sat->sat_ordering_oid ) {
670                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
671                         sat->sat_ordering = mr;
672                 } else {
673                         *err = sat->sat_ordering_oid;
674                         return SLAP_SCHERR_MR_NOT_FOUND;
675                 }
676         }
677
678         if ( sat->sat_substr_oid ) {
679                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
680                         sat->sat_substr = mr;
681                 } else {
682                         *err = sat->sat_substr_oid;
683                         return SLAP_SCHERR_MR_NOT_FOUND;
684                 }
685         }
686
687         code = at_insert(sat,err);
688         return code;
689 }
690
691
692 char *
693 at_canonical_name( char * a_type )
694 {
695         AttributeType   *atp;
696
697         if ( (atp=at_find(a_type)) == NULL ) {
698
699                 return a_type;
700
701         } else  if ( atp->sat_names 
702                      && atp->sat_names[0]
703                      && (*(atp->sat_names[0]) != '\0') ) {
704             
705                 return atp->sat_names[0];
706
707         } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
708
709                 return atp->sat_oid;
710                 
711         } else {
712
713                 return a_type;
714
715         }
716
717 }/* char * at_canonica_name() */
718
719
720 #if defined( SLAPD_SCHEMA_DN )
721
722 int
723 at_schema_info( Entry *e )
724 {
725         struct berval   val;
726         struct berval   *vals[2];
727         AttributeType   *at;
728
729         vals[0] = &val;
730         vals[1] = NULL;
731
732         for ( at = attr_list; at; at = at->sat_next ) {
733                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
734                 if ( val.bv_val ) {
735                         val.bv_len = strlen( val.bv_val );
736                         Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
737                                (long) val.bv_len, val.bv_val, 0 );
738                         attr_merge( e, "attributeTypes", vals );
739                         ldap_memfree( val.bv_val );
740                 } else {
741                         return -1;
742                 }
743         }
744         return 0;
745 }
746 #endif
747
748 #ifdef LDAP_DEBUG
749
750 static int
751 at_index_printnode( struct aindexrec *air )
752 {
753
754         printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
755         return( 0 );
756 }
757
758 static void
759 at_index_print( void )
760 {
761         printf("Printing attribute type index:\n");
762         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
763                 0, -1, AVL_INORDER );
764 }
765
766 #endif