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