]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
c541da91e5521d2d9c8a911cd29c8fe52bec8702
[openldap] / servers / slapd / attr.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* attr.c - routines for dealing with attributes */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #ifdef HAVE_FCNTL_H
12 #include <fcntl.h>
13 #endif
14
15 #include <ac/ctype.h>
16 #include <ac/errno.h>
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
23 #endif
24
25 #include "ldap_pvt.h"
26 #include "ldap_defaults.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
390         if ( (air = (struct aindexrec *) avl_find( attr_index, name,
391             (AVL_CMP) attr_index_name_cmp )) != NULL ) {
392                 return( air->air_at );
393         }
394         return( NULL );
395 }
396
397 int
398 at_append_to_list(
399     AttributeType       *sat,
400     AttributeType       ***listp
401 )
402 {
403         AttributeType   **list;
404         AttributeType   **list1;
405         int             size;
406
407         list = *listp;
408         if ( !list ) {
409                 size = 2;
410                 list = calloc(size, sizeof(AttributeType *));
411                 if ( !list ) {
412                         return -1;
413                 }
414         } else {
415                 size = 0;
416                 list1 = *listp;
417                 while ( *list1 ) {
418                         size++;
419                         list1++;
420                 }
421                 size += 2;
422                 list1 = realloc(list, size*sizeof(AttributeType *));
423                 if ( !list1 ) {
424                         return -1;
425                 }
426                 list = list1;
427         }
428         list[size-2] = sat;
429         list[size-1] = NULL;
430         *listp = list;
431         return 0;
432 }
433
434 int
435 at_delete_from_list(
436     int                 pos,
437     AttributeType       ***listp
438 )
439 {
440         AttributeType   **list;
441         AttributeType   **list1;
442         int             i;
443         int             j;
444
445         if ( pos < 0 ) {
446                 return -2;
447         }
448         list = *listp;
449         for ( i=0; list[i]; i++ )
450                 ;
451         if ( pos >= i ) {
452                 return -2;
453         }
454         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
455                 list[i] = list[j];
456         }
457         list[i] = NULL;
458         /* Tell the runtime this can be shrinked */
459         list1 = realloc(list, (i+1)*sizeof(AttributeType **));
460         if ( !list1 ) {
461                 return -1;
462         }
463         *listp = list1;
464         return 0;
465 }
466
467 int
468 at_find_in_list(
469     AttributeType       *sat,
470     AttributeType       **list
471 )
472 {
473         int     i;
474
475         if ( !list ) {
476                 return -1;
477         }
478         for ( i=0; list[i]; i++ ) {
479                 if ( sat == list[i] ) {
480                         return i;
481                 }
482         }
483         return -1;
484 }
485
486 static int
487 at_insert(
488     AttributeType       *sat,
489     const char          **err
490 )
491 {
492         AttributeType           **atp;
493         struct aindexrec        *air;
494         char                    **names;
495
496         atp = &attr_list;
497         while ( *atp != NULL ) {
498                 atp = &(*atp)->sat_next;
499         }
500         *atp = sat;
501
502         if ( sat->sat_oid ) {
503                 air = (struct aindexrec *)
504                         ch_calloc( 1, sizeof(struct aindexrec) );
505                 air->air_name = sat->sat_oid;
506                 air->air_at = sat;
507                 if ( avl_insert( &attr_index, (caddr_t) air,
508                                  (AVL_CMP) attr_index_cmp,
509                                  (AVL_DUP) avl_dup_error ) ) {
510                         *err = sat->sat_oid;
511                         ldap_memfree(air);
512                         return SLAP_SCHERR_DUP_ATTR;
513                 }
514                 /* FIX: temporal consistency check */
515                 at_find(air->air_name);
516         }
517         if ( (names = sat->sat_names) ) {
518                 while ( *names ) {
519                         air = (struct aindexrec *)
520                                 ch_calloc( 1, sizeof(struct aindexrec) );
521                         air->air_name = ch_strdup(*names);
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 = *names;
527                                 ldap_memfree(air);
528                                 return SLAP_SCHERR_DUP_ATTR;
529                         }
530                         /* FIX: temporal consistency check */
531                         at_find(air->air_name);
532                         names++;
533                 }
534         }
535
536         return 0;
537 }
538
539 int
540 at_add(
541     LDAP_ATTRIBUTE_TYPE *at,
542     const char          **err
543 )
544 {
545         AttributeType   *sat;
546         AttributeType   *sat1;
547         MatchingRule    *mr;
548         Syntax          *syn;
549         int             code;
550         char            *errattr;
551
552         if ( at->at_names && at->at_names[0] ) {
553                 errattr = at->at_names[0];
554         } else if ( at->at_oid ) {
555                 errattr = at->at_oid;
556         } else {
557                 errattr = "";
558                 return SLAP_SCHERR_ATTR_INCOMPLETE;
559         }
560         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
561         memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
562         if ( at->at_sup_oid ) {
563                 if ( (sat1 = at_find(at->at_sup_oid)) ) {
564                         sat->sat_sup = sat1;
565                         if ( at_append_to_list(sat, &sat1->sat_subtypes) ) {
566                                 *err = errattr;
567                                 return SLAP_SCHERR_OUTOFMEM;
568                         }
569                 } else {
570                         *err = at->at_sup_oid;
571                         return SLAP_SCHERR_ATTR_NOT_FOUND;
572                 }
573         }
574
575         if ( at->at_syntax_oid ) {
576                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
577                         sat->sat_syntax = syn;
578                 } else {
579                         *err = sat->sat_syntax_oid;
580                         return SLAP_SCHERR_SYN_NOT_FOUND;
581                 }
582                 if ( !strcmp(at->at_syntax_oid,
583                              "1.3.6.1.4.1.1466.115.121.1.15") ) {
584                         if ( at->at_equality_oid &&
585                              !strcmp(at->at_equality_oid, "2.5.13.5") ) {
586                                 sat->sat_syntax_compat = SYNTAX_CES;
587                         } else {
588                                 sat->sat_syntax_compat = SYNTAX_CIS;
589                         }
590                 } else if ( !strcmp(at->at_syntax_oid,
591                                     "1.3.6.1.4.1.1466.115.121.1.50") ) {
592                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
593                 } else if ( !strcmp(at->at_syntax_oid,
594                                     "1.3.6.1.4.1.1466.115.121.1.12") ) {
595                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
596                 } else if ( !strcmp(at->at_syntax_oid, "1.3.6.1.4.1.1466.115.121.1.5") ) {
597                         sat->sat_syntax_compat = SYNTAX_BIN;
598                 } else {
599                         sat->sat_syntax_compat = DEFAULT_SYNTAX;
600                 }
601         } else {
602                 sat->sat_syntax_compat = DEFAULT_SYNTAX;
603         }
604
605         if ( sat->sat_equality_oid ) {
606                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
607                         sat->sat_equality = mr;
608                 } else {
609                         *err = sat->sat_equality_oid;
610                         return SLAP_SCHERR_MR_NOT_FOUND;
611                 }
612         }
613         if ( sat->sat_ordering_oid ) {
614                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
615                         sat->sat_ordering = mr;
616                 } else {
617                         *err = sat->sat_ordering_oid;
618                         return SLAP_SCHERR_MR_NOT_FOUND;
619                 }
620         }
621         if ( sat->sat_substr_oid ) {
622                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
623                         sat->sat_substr = mr;
624                 } else {
625                         *err = sat->sat_substr_oid;
626                         return SLAP_SCHERR_MR_NOT_FOUND;
627                 }
628         }
629
630         /*
631          * Now inherit definitions from superiors.  We only check the
632          * direct superior since that one has already inherited from
633          * its own superiorss
634          */
635         if ( sat->sat_sup ) {
636                 if ( !sat->sat_syntax ) {
637                         sat->sat_syntax = sat->sat_sup->sat_syntax;
638                         sat->sat_syntax_len = sat->sat_sup->sat_syntax_len;
639                 }
640                 if ( !sat->sat_equality ) {
641                         sat->sat_equality = sat->sat_sup->sat_equality;
642                 }
643                 if ( !sat->sat_ordering ) {
644                         sat->sat_ordering = sat->sat_sup->sat_ordering;
645                 }
646                 if ( !sat->sat_substr ) {
647                         sat->sat_substr = sat->sat_sup->sat_substr;
648                 }
649         }
650         code = at_insert(sat,err);
651         return code;
652 }
653
654
655 char *
656 at_canonical_name( char * a_type )
657 {
658         AttributeType   *atp;
659
660         if ( (atp=at_find(a_type)) == NULL ) {
661
662                 return a_type;
663
664         } else  if ( atp->sat_names 
665                      && atp->sat_names[0]
666                      && (*(atp->sat_names[0]) != '\0') ) {
667             
668                 return atp->sat_names[0];
669
670         } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
671
672                 return atp->sat_oid;
673                 
674         } else {
675
676                 return a_type;
677
678         }
679
680 }/* char * at_canonica_name() */
681
682
683 #if defined( SLAPD_SCHEMA_DN )
684
685 int
686 at_schema_info( Entry *e )
687 {
688         struct berval   val;
689         struct berval   *vals[2];
690         AttributeType   *at;
691
692         vals[0] = &val;
693         vals[1] = NULL;
694
695         for ( at = attr_list; at; at = at->sat_next ) {
696                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
697                 if ( val.bv_val ) {
698                         val.bv_len = strlen( val.bv_val );
699                         Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
700                                (long) val.bv_len, val.bv_val, 0 );
701                         attr_merge( e, "attributeTypes", vals );
702                         ldap_memfree( val.bv_val );
703                 } else {
704                         return -1;
705                 }
706         }
707         return 0;
708 }
709 #endif
710
711 #ifdef LDAP_DEBUG
712
713 static int
714 at_index_printnode( struct aindexrec *air )
715 {
716
717         printf( "%s = %s\n", air->air_name, ldap_attributetype2str(&air->air_at->sat_atype) );
718         return( 0 );
719 }
720
721 static void
722 at_index_print( void )
723 {
724         printf("Printing attribute type index:\n");
725         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
726                 0, -1, AVL_INORDER );
727 }
728
729 #endif