]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/attr.c
72f26d31dc9e77bda95855142ee8e8faee7f73cf
[openldap] / servers / slapd / back-ldbm / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 /* for the cache of attribute information (which are indexed, etc.) */
19 typedef struct ldbm_attrinfo {
20         char    *ai_type;       /* type name (cn, sn, ...)      */
21         int     ai_indexmask;   /* how the attr is indexed      */
22 } AttrInfo;
23
24 static int
25 ainfo_type_cmp(
26     char                *type,
27     AttrInfo    *a
28 )
29 {
30         return( strcasecmp( type, a->ai_type ) );
31 }
32
33 static int
34 ainfo_cmp(
35     AttrInfo    *a,
36     AttrInfo    *b
37 )
38 {
39         return( strcasecmp( a->ai_type, b->ai_type ) );
40 }
41
42 /*
43  * Called when a duplicate "index" line is encountered.
44  *
45  * returns 1 => original from init code, indexmask updated
46  *         2 => original not from init code, warn the user
47  */
48
49 static int
50 ainfo_dup(
51     AttrInfo    *a,
52     AttrInfo    *b
53 )
54 {
55         /*
56          * if the duplicate definition is because we initialized the attr,
57          * just add what came from the config file. otherwise, complain.
58          */
59         if ( a->ai_indexmask & SLAP_INDEX_FROMINIT ) {
60                 a->ai_indexmask |= b->ai_indexmask;
61
62                 return( 1 );
63         }
64
65         return( 2 );
66 }
67
68 void
69 attr_mask(
70     struct ldbminfo     *li,
71     char                *type,
72     int                 *indexmask )
73 {
74         AttrInfo        *a;
75
76         *indexmask = 0;
77         if ( (a = (AttrInfo *) avl_find( li->li_attrs, type,
78             (AVL_CMP) ainfo_type_cmp )) == NULL ) {
79                 if ( (a = (AttrInfo *) avl_find( li->li_attrs, "default",
80                     (AVL_CMP) ainfo_type_cmp )) == NULL ) {
81                         return;
82                 }
83         }
84         *indexmask = a->ai_indexmask;
85 }
86
87 void
88 attr_index_config(
89     struct ldbminfo     *li,
90     const char          *fname,
91     int                 lineno,
92     int                 argc,
93     char                **argv,
94     int                 init
95 )
96 {
97         int             i, j;
98         char            **attrs, **indexes;
99         AttrInfo        *a;
100
101         attrs = str2charray( argv[0], "," );
102         if ( argc > 1 ) {
103                 indexes = str2charray( argv[1], "," );
104         }
105         for ( i = 0; attrs[i] != NULL; i++ ) {
106                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
107                 a->ai_type = ch_strdup( attrs[i] );
108 #ifdef SLAPD_SCHEMA_COMPAT
109                 a->ai_syntaxmask = attr_syntax( a->ai_type );
110 #endif
111                 if ( argc == 1 ) {
112                         a->ai_indexmask = (
113                                 SLAP_INDEX_PRESENCE | SLAP_INDEX_EQUALITY |
114                             SLAP_INDEX_APPROX | SLAP_INDEX_SUB);
115                 } else {
116                         a->ai_indexmask = 0;
117                         for ( j = 0; indexes[j] != NULL; j++ ) {
118                                 if ( strncasecmp( indexes[j], "pres", 4 )
119                                     == 0 ) {
120                                         a->ai_indexmask |= SLAP_INDEX_PRESENCE;
121                                 } else if ( strncasecmp( indexes[j], "eq", 2 )
122                                     == 0 ) {
123                                         a->ai_indexmask |= SLAP_INDEX_EQUALITY;
124                                 } else if ( strncasecmp( indexes[j], "approx",
125                                     6 ) == 0 ) {
126                                         a->ai_indexmask |= SLAP_INDEX_APPROX;
127                                 } else if ( strncasecmp( indexes[j], "sub", 3 )
128                                     == 0 ) {
129                                         a->ai_indexmask |= SLAP_INDEX_SUB;
130                                 } else if ( strncasecmp( indexes[j], "none", 4 )
131                                     == 0 ) {
132                                         if ( a->ai_indexmask != 0 ) {
133                                                 fprintf( stderr,
134 "%s: line %d: index type \"none\" cannot be combined with other types\n",
135                                                     fname, lineno );
136                                         }
137                                         a->ai_indexmask = 0;
138                                 } else {
139                                         fprintf( stderr,
140                         "%s: line %d: unknown index type \"%s\" (ignored)\n",
141                                             fname, lineno, indexes[j] );
142                                         fprintf( stderr,
143         "valid index types are \"pres\", \"eq\", \"approx\", or \"sub\"\n" );
144                                 }
145                         }
146                 }
147                 if ( init ) {
148                         a->ai_indexmask |= SLAP_INDEX_FROMINIT;
149                 }
150
151                 switch (avl_insert( &li->li_attrs, (caddr_t) a,
152                         (AVL_CMP) ainfo_cmp, (AVL_DUP) ainfo_dup ))
153                 {
154                 case 1:         /* duplicate - updating init version */
155                         free( a->ai_type );
156                         free( (char *) a );
157                         break;
158
159                 case 2:         /* user duplicate - ignore and warn */
160                         fprintf( stderr,
161     "%s: line %d: duplicate index definition for attr \"%s\" (ignored)\n",
162                             fname, lineno, a->ai_type );
163                         free( a->ai_type );
164                         free( (char *) a );
165                         break;
166
167                 default:;       /* inserted ok */
168                         /* FALL */
169                 }
170         }
171         charray_free( attrs );
172         if ( argc > 1 )
173                 charray_free( indexes );
174 }
175
176
177 static void
178 ainfo_free( void *attr )
179 {
180         AttrInfo *ai = attr;
181         free( ai->ai_type );
182         free( ai );
183 }
184
185 void
186 attr_index_destroy( Avlnode *tree )
187 {
188         avl_free( tree, ainfo_free );
189 }
190