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