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