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