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