]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Introduction of a backend startup/shutdown function to make backend-specific
[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
8 #include <ac/string.h>
9 #include <ac/socket.h>
10
11 #include <sys/stat.h>
12
13 #include "slap.h"
14
15
16 #define BACKEND_GRAB_SIZE       10
17
18 int             nbackends;
19 Backend         *backends;
20 static int      maxbackends;
21
22 Backend *
23 new_backend(
24     char        *type
25 )
26 {
27         Backend *be;
28         int     foundit;
29
30         if ( nbackends == maxbackends ) {
31                 maxbackends += BACKEND_GRAB_SIZE;
32                 backends = (Backend *) ch_realloc( (char *) backends,
33                     maxbackends * sizeof(Backend) );
34                 memset( &backends[nbackends], '\0', BACKEND_GRAB_SIZE *
35                     sizeof(Backend) );
36         }
37
38         be = &backends[nbackends++];
39         be->be_sizelimit = defsize;
40         be->be_timelimit = deftime;
41         foundit = 0;
42
43 #ifdef SLAPD_LDBM
44         if ( strcasecmp( type, "ldbm" ) == 0 ) {
45                 be->be_bind = ldbm_back_bind;
46                 be->be_unbind = ldbm_back_unbind;
47                 be->be_search = ldbm_back_search;
48                 be->be_compare = ldbm_back_compare;
49                 be->be_modify = ldbm_back_modify;
50                 be->be_modrdn = ldbm_back_modrdn;
51                 be->be_add = ldbm_back_add;
52                 be->be_delete = ldbm_back_delete;
53                 be->be_abandon = ldbm_back_abandon;
54                 be->be_config = ldbm_back_config;
55                 be->be_init = ldbm_back_init;
56                 be->be_close = ldbm_back_close;
57                 be->be_startup  = ldbm_back_startup;
58                 be->be_shutdown = ldbm_back_shutdown;
59 #ifdef SLAPD_ACLGROUPS
60                 be->be_group = ldbm_back_group;
61 #endif
62                 be->be_type = "ldbm";
63                 foundit = 1;
64         }
65 #endif
66
67 #ifdef SLAPD_PASSWD
68         if ( strcasecmp( type, "passwd" ) == 0 ) {
69                 be->be_bind = NULL;
70                 be->be_unbind = NULL;
71                 be->be_search = passwd_back_search;
72                 be->be_compare = NULL;
73                 be->be_modify = NULL;
74                 be->be_modrdn = NULL;
75                 be->be_add = NULL;
76                 be->be_delete = NULL;
77                 be->be_abandon = NULL;
78                 be->be_config = passwd_back_config;
79                 be->be_init = NULL;
80                 be->be_close = NULL;
81 #ifdef SLAPD_ACLGROUPS
82                 be->be_group = NULL;
83 #endif
84                 be->be_type = "passwd";
85                 foundit = 1;
86         }
87 #endif
88
89 #ifdef SLAPD_SHELL
90         if ( strcasecmp( type, "shell" ) == 0 ) {
91                 be->be_bind = shell_back_bind;
92                 be->be_unbind = shell_back_unbind;
93                 be->be_search = shell_back_search;
94                 be->be_compare = shell_back_compare;
95                 be->be_modify = shell_back_modify;
96                 be->be_modrdn = shell_back_modrdn;
97                 be->be_add = shell_back_add;
98                 be->be_delete = shell_back_delete;
99                 be->be_abandon = shell_back_abandon;
100                 be->be_config = shell_back_config;
101                 be->be_init = shell_back_init;
102                 be->be_close = NULL;
103 #ifdef SLAPD_ACLGROUPS
104                 be->be_group = NULL;
105 #endif
106                 be->be_type = "shell";
107                 foundit = 1;
108         }
109 #endif
110
111
112 #ifdef SLAPD_PERL
113         if ( strcasecmp( type, "perl" ) == 0 ) {
114 #ifdef notdef
115                 be->be_abandon = perl_back_abandon;
116                 be->be_bind = perl_back_bind;
117 #else
118                 be->be_abandon = NULL;
119                 be->be_bind = NULL;
120 #endif
121                 be->be_unbind = perl_back_unbind;
122                 be->be_search = perl_back_search;
123                 be->be_compare = perl_back_compare;
124                 be->be_modify = perl_back_modify;
125                 be->be_modrdn = perl_back_modrdn;
126                 be->be_add = perl_back_add;
127                 be->be_delete = perl_back_delete;
128                 be->be_config = perl_back_config;
129                 be->be_init = perl_back_init;
130                 be->be_close = perl_back_close;
131                 be->be_type = "perl";
132                 foundit = 1;
133         }
134 #endif
135
136
137
138         if ( be->be_init != NULL ) {
139                 (*be->be_init)( be );
140         }
141
142         if ( foundit == 0 ) {
143                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
144                 exit( 1 );
145         }
146
147         return( be );
148 }
149
150 Backend *
151 select_backend( char * dn )
152 {
153         int     i, j, len, dnlen;
154
155         dnlen = strlen( dn );
156         for ( i = 0; i < nbackends; i++ ) {
157                 for ( j = 0; backends[i].be_suffix != NULL &&
158                     backends[i].be_suffix[j] != NULL; j++ )
159                 {
160                         len = strlen( backends[i].be_suffix[j] );
161
162                         if ( len > dnlen ) {
163                                 continue;
164                         }
165
166                         if ( strcmp( backends[i].be_suffix[j],
167                             dn + (dnlen - len) ) == 0 ) {
168                                 return( &backends[i] );
169                         }
170                 }
171         }
172
173         /* if no proper suffix could be found then check for aliases */
174         for ( i = 0; i < nbackends; i++ ) {
175                 for ( j = 0; 
176                       backends[i].be_suffixAlias != NULL && 
177                       backends[i].be_suffixAlias[j] != NULL; 
178                       j += 2 )
179                 {
180                         len = strlen( backends[i].be_suffixAlias[j] );
181
182                         if ( len > dnlen ) {
183                                 continue;
184                         }
185
186                         if ( strcmp( backends[i].be_suffixAlias[j],
187                             dn + (dnlen - len) ) == 0 ) {
188                                 return( &backends[i] );
189                         }
190                 }
191         }
192
193 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
194         /* Add greg@greg.rim.or.jp
195          * It's quick hack for cheap client
196          * Some browser offer a NULL base at ldap_search
197          *
198          * Should only be used as a last resort. -Kdz
199          */
200         if(dnlen == 0) {
201                 Debug( LDAP_DEBUG_TRACE,
202                         "select_backend: use default backend\n", 0, 0, 0 );
203                 return( &backends[0] );
204         }
205 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
206
207         return( NULL );
208 }
209
210 int
211 be_issuffix(
212     Backend     *be,
213     char        *suffix
214 )
215 {
216         int     i;
217
218         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
219                 if ( strcmp( be->be_suffix[i], suffix ) == 0 ) {
220                         return( 1 );
221                 }
222         }
223
224         return( 0 );
225 }
226
227 int
228 be_isroot( Backend *be, char *ndn )
229 {
230         int rc;
231
232         if ( ndn == NULL || be->be_root_ndn == NULL ) {
233                 return( 0 );
234         }
235
236         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
237
238         return(rc);
239 }
240
241 char *
242 be_root_dn( Backend *be )
243 {
244         int rc;
245
246         if ( be->be_root_dn == NULL ) {
247                 return( "" );
248         }
249
250         return be->be_root_dn;
251 }
252
253 int
254 be_isroot_pw( Backend *be, char *ndn, struct berval *cred )
255 {
256         int result;
257
258         if ( ! be_isroot( be, ndn ) ) {
259                 return( 0 );
260         }
261
262 #ifdef SLAPD_CRYPT
263         ldap_pvt_thread_mutex_lock( &crypt_mutex );
264 #endif
265
266         result = lutil_passwd( cred->bv_val, be->be_root_pw );
267
268 #ifdef SLAPD_CRYPT
269         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
270 #endif
271
272         return result == 0;
273 }
274
275 void
276 be_startup( void )
277 {
278         int     i;
279
280         for ( i = 0; i < nbackends; i++ ) {
281                 if ( backends[i].be_startup != NULL ) {
282                         (*backends[i].be_startup)( &backends[i] );
283                 }
284         }
285 }
286
287
288 void
289 be_shutdown( void )
290 {
291         int     i;
292
293         for ( i = 0; i < nbackends; i++ ) {
294                 if ( backends[i].be_shutdown != NULL ) {
295                         (*backends[i].be_shutdown)( &backends[i] );
296                 }
297         }
298 }
299
300
301 void
302 be_close( void )
303 {
304         int     i;
305
306         for ( i = 0; i < nbackends; i++ ) {
307                 if ( backends[i].be_close != NULL ) {
308                         (*backends[i].be_close)( &backends[i] );
309                 }
310         }
311 }
312
313
314 void
315 be_unbind(
316         Connection   *conn,
317         Operation    *op
318 )
319 {
320         int     i;
321
322         for ( i = 0; i < nbackends; i++ ) {
323                 if ( backends[i].be_unbind != NULL ) {
324                         (*backends[i].be_unbind)( &backends[i], conn, op );
325                 }
326         }
327 }
328
329 #ifdef SLAPD_ACLGROUPS
330 int 
331 be_group(
332         Backend *be,
333         Entry   *target,
334         char    *gr_ndn,
335         char    *op_ndn,
336         char    *objectclassValue,
337         char    *groupattrName
338 )
339 {
340         if (be->be_group)
341                 return( be->be_group(be, target, gr_ndn, op_ndn,
342                         objectclassValue, groupattrName) );
343         else
344                 return(1);
345 }
346 #endif