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