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