]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
include portable.h
[openldap] / servers / slapd / backend.c
1 /* backend.c - routines for dealing with back-end databases */
2
3
4 #include "portable.h"
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/stat.h>
11 #include "slap.h"
12
13 #ifdef LDAP_LDBM
14 extern int      ldbm_back_bind();
15 extern int      ldbm_back_unbind();
16 extern int      ldbm_back_search();
17 extern int      ldbm_back_compare();
18 extern int      ldbm_back_modify();
19 extern int      ldbm_back_modrdn();
20 extern int      ldbm_back_add();
21 extern int      ldbm_back_delete();
22 extern int      ldbm_back_abandon();
23 extern int      ldbm_back_config();
24 extern int      ldbm_back_init();
25 extern int      ldbm_back_close();
26 extern int      ldbm_back_group();
27 #endif
28
29 #ifdef LDAP_PASSWD
30 extern int      passwd_back_search();
31 extern int      passwd_back_config();
32 #endif
33
34 #ifdef LDAP_SHELL
35 extern int      shell_back_bind();
36 extern int      shell_back_unbind();
37 extern int      shell_back_search();
38 extern int      shell_back_compare();
39 extern int      shell_back_modify();
40 extern int      shell_back_modrdn();
41 extern int      shell_back_add();
42 extern int      shell_back_delete();
43 extern int      shell_back_abandon();
44 extern int      shell_back_config();
45 extern int      shell_back_init();
46 #endif
47
48 extern int      defsize;
49 extern int      deftime;
50
51 #define BACKEND_GRAB_SIZE       10
52
53 int             nbackends;
54 Backend         *backends;
55 static int      maxbackends;
56
57 Backend *
58 new_backend(
59     char        *type
60 )
61 {
62         Backend *be;
63         int     foundit;
64
65         if ( nbackends == maxbackends ) {
66                 maxbackends += BACKEND_GRAB_SIZE;
67                 backends = (Backend *) ch_realloc( (char *) backends,
68                     maxbackends * sizeof(Backend) );
69                 memset( &backends[nbackends], '\0', BACKEND_GRAB_SIZE *
70                     sizeof(Backend) );
71         }
72
73         be = &backends[nbackends++];
74         be->be_sizelimit = defsize;
75         be->be_timelimit = deftime;
76         foundit = 0;
77
78 #ifdef LDAP_LDBM
79         if ( strcasecmp( type, "ldbm" ) == 0 ) {
80                 be->be_bind = ldbm_back_bind;
81                 be->be_unbind = ldbm_back_unbind;
82                 be->be_search = ldbm_back_search;
83                 be->be_compare = ldbm_back_compare;
84                 be->be_modify = ldbm_back_modify;
85                 be->be_modrdn = ldbm_back_modrdn;
86                 be->be_add = ldbm_back_add;
87                 be->be_delete = ldbm_back_delete;
88                 be->be_abandon = ldbm_back_abandon;
89                 be->be_config = ldbm_back_config;
90                 be->be_init = ldbm_back_init;
91                 be->be_close = ldbm_back_close;
92 #ifdef ACLGROUP
93                 be->be_group = ldbm_back_group;
94 #endif
95                 be->be_type = "ldbm";
96                 foundit = 1;
97         }
98 #endif
99
100 #ifdef LDAP_PASSWD
101         if ( strcasecmp( type, "passwd" ) == 0 ) {
102                 be->be_bind = NULL;
103                 be->be_unbind = NULL;
104                 be->be_search = passwd_back_search;
105                 be->be_compare = NULL;
106                 be->be_modify = NULL;
107                 be->be_modrdn = NULL;
108                 be->be_add = NULL;
109                 be->be_delete = NULL;
110                 be->be_abandon = NULL;
111                 be->be_config = passwd_back_config;
112                 be->be_init = NULL;
113                 be->be_close = NULL;
114 #ifdef ACLGROUP
115                 be->be_group = NULL;
116 #endif
117                 be->be_type = "passwd";
118                 foundit = 1;
119         }
120 #endif
121
122 #ifdef LDAP_SHELL
123         if ( strcasecmp( type, "shell" ) == 0 ) {
124                 be->be_bind = shell_back_bind;
125                 be->be_unbind = shell_back_unbind;
126                 be->be_search = shell_back_search;
127                 be->be_compare = shell_back_compare;
128                 be->be_modify = shell_back_modify;
129                 be->be_modrdn = shell_back_modrdn;
130                 be->be_add = shell_back_add;
131                 be->be_delete = shell_back_delete;
132                 be->be_abandon = shell_back_abandon;
133                 be->be_config = shell_back_config;
134                 be->be_init = shell_back_init;
135                 be->be_close = NULL;
136 #ifdef ACLGROUP
137                 be->be_group = NULL;
138 #endif
139                 be->be_type = "shell";
140                 foundit = 1;
141         }
142 #endif
143
144         if ( be->be_init != NULL ) {
145                 (*be->be_init)( be );
146         }
147
148         if ( foundit == 0 ) {
149                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
150                 exit( 1 );
151         }
152
153         return( be );
154 }
155
156 Backend *
157 select_backend( char * dn )
158 {
159         int     i, j, len, dnlen;
160
161         dnlen = strlen( dn );
162         for ( i = 0; i < nbackends; i++ ) {
163                 for ( j = 0; backends[i].be_suffix != NULL &&
164                     backends[i].be_suffix[j] != NULL; j++ )
165                 {
166 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
167                         /* Add greg@greg.rim.or.jp
168                          * It's quick hack for cheap client
169                          * Some browser offer a NULL base at ldap_search
170                          */
171                         if(dnlen == 0) {
172                                 Debug( LDAP_DEBUG_TRACE,
173                                         "select_backend: use default backend\n", 0, 0, 0 );
174                                 return (&backends[i]);
175                         }
176 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
177
178                         len = strlen( backends[i].be_suffix[j] );
179
180                         if ( len > dnlen ) {
181                                 continue;
182                         }
183
184                         if ( strcasecmp( backends[i].be_suffix[j],
185                             dn + (dnlen - len) ) == 0 ) {
186                                 return( &backends[i] );
187                         }
188                 }
189         }
190
191         return( NULL );
192 }
193
194 int
195 be_issuffix(
196     Backend     *be,
197     char        *suffix
198 )
199 {
200         int     i;
201
202         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
203                 if ( strcasecmp( be->be_suffix[i], suffix ) == 0 ) {
204                         return( 1 );
205                 }
206         }
207
208         return( 0 );
209 }
210
211 int
212 be_isroot( Backend *be, char *dn )
213 {
214         if ( dn == NULL ) {
215                 return( 0 );
216         }
217
218         return( be->be_rootdn ? strcasecmp( be->be_rootdn, dn ) == 0
219             : 0 );
220 }
221
222 int
223 be_isroot_pw( Backend *be, char *dn, struct berval *cred )
224 {
225         if ( ! be_isroot( be, dn ) || be->be_rootpw == NULL ) {
226                 return( 0 );
227         }
228
229         return( strcmp( be->be_rootpw, cred->bv_val ) == 0 );
230 }
231
232 void
233 be_close()
234 {
235         int     i;
236
237         for ( i = 0; i < nbackends; i++ ) {
238                 if ( backends[i].be_close != NULL ) {
239                         (*backends[i].be_close)( &backends[i] );
240                 }
241         }
242 }
243
244
245 void
246 be_unbind(
247         Connection   *conn,
248         Operation    *op
249 )
250 {
251         int     i;
252
253         for ( i = 0; i < nbackends; i++ ) {
254                 if ( backends[i].be_unbind != NULL ) {
255                         (*backends[i].be_unbind)( &backends[i], conn, op );
256                 }
257         }
258 }
259
260 #ifdef ACLGROUP
261 int 
262 be_group(Backend *be, char *bdn, char *edn)
263 {
264         if (be->be_group)
265                 return(be->be_group(be, bdn, edn));
266         else
267                 return(1);
268 }
269 #endif