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