]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Rework ac/socket.h for HAVE_WINSOCK:
[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 #include "lutil.h"
15
16 #ifdef SLAPD_LDBM
17 #include "back-ldbm/external.h"
18 #endif
19 #ifdef SLAPD_BDB2
20 #include "back-bdb2/external.h"
21 #endif
22 #ifdef SLAPD_PASSWD
23 #include "back-passwd/external.h"
24 #endif
25 #ifdef SLAPD_PERL
26 #include "back-perl/external.h"
27 #endif
28 #ifdef SLAPD_SHELL
29 #include "back-shell/external.h"
30 #endif
31 #ifdef SLAPD_TCL
32 #include "back-tcl/external.h"
33 #endif
34
35 static BackendInfo binfo[] = {
36 #ifdef SLAPD_LDBM
37         {"ldbm",        ldbm_back_initialize},
38 #endif
39 #ifdef SLAPD_BDB2
40         {"bdb2",        bdb2_back_initialize},
41 #endif
42 #ifdef SLAPD_PASSWD
43         {"passwd",      passwd_back_initialize},
44 #endif
45 #ifdef SLAPD_PERL
46         {"perl",        perl_back_initialize},
47 #endif
48 #ifdef SLAPD_SHELL
49         {"shell",       shell_back_initialize},
50 #endif
51 #ifdef SLAPD_TCL
52         {"tcl",         tcl_back_initialize},
53 #endif
54         {NULL}
55 };
56
57 int                     nBackendInfo = 0;
58 BackendInfo     *backendInfo = NULL;
59
60 int                     nBackendDB = 0; 
61 BackendDB       *backendDB = NULL;
62
63 int backend_init(void)
64 {
65         int rc = -1;
66
67         if((nBackendInfo != 0) || (backendInfo != NULL)) {
68                 /* already initialized */
69                 Debug( LDAP_DEBUG_ANY,
70                         "backend_init: already initialized.\n", 0, 0, 0 );
71                 return -1;
72         }
73
74         for( ;
75                 binfo[nBackendInfo].bi_type !=  NULL;
76                 nBackendInfo++ )
77         {
78                 rc = binfo[nBackendInfo].bi_init(
79                         &binfo[nBackendInfo] );
80
81                 if(rc != 0) {
82                         Debug( LDAP_DEBUG_ANY,
83                                 "backend_init: initialized for type \"%s\"\n",
84                                         binfo[nBackendInfo].bi_type, 0, 0 );
85
86                         /* destroy those we've already inited */
87                         for( nBackendInfo--;
88                                 nBackendInfo >= 0 ;
89                                 nBackendInfo-- )
90                         { 
91                                 if ( binfo[nBackendInfo].bi_destroy ) {
92                                         binfo[nBackendInfo].bi_destroy(
93                                                 &binfo[nBackendInfo] );
94                                 }
95                         }
96                         return rc;
97                 }
98         }
99
100         if ( nBackendInfo > 0) {
101                 backendInfo = binfo;
102                 return 0;
103         }
104
105         Debug( LDAP_DEBUG_ANY,
106                 "backend_init: failed\n",
107                 0, 0, 0 );
108
109         return rc;
110 }
111
112 int backend_startup(int n)
113 {
114         int i;
115         int rc = 0;
116
117         if( ! ( nBackendDB > 0 ) ) {
118                 /* no databases */
119                 Debug( LDAP_DEBUG_ANY,
120                         "backend_startup: %d databases to startup.\n",
121                         nBackendDB, 0, 0 );
122                 return 1;
123         }
124
125         if(n >= 0) {
126                 /* startup a specific backend database */
127                 Debug( LDAP_DEBUG_TRACE,
128                         "backend_startup: starting database %d\n",
129                         n, 0, 0 );
130
131                 /* make sure, n does not exceed the number of backend databases */
132                 if ( n >= nbackends ) {
133
134                         Debug( LDAP_DEBUG_ANY,
135                                 "backend_startup: database number %d exceeding maximum (%d)\n",
136                                 n, nbackends, 0 );
137                         return 1;
138                 }
139
140                 if ( backendDB[n].bd_info->bi_open ) {
141                         rc = backendDB[n].bd_info->bi_open(
142                                 backendDB[n].bd_info );
143                 }
144
145                 if(rc != 0) {
146                         Debug( LDAP_DEBUG_ANY,
147                                 "backend_startup: bi_open failed!\n",
148                                 0, 0, 0 );
149                         return rc;
150                 }
151
152                 if ( backendDB[n].bd_info->bi_db_open ) {
153                         rc = backendDB[n].bd_info->bi_db_open(
154                                 &backendDB[n] );
155                 }
156
157                 if(rc != 0) {
158                         Debug( LDAP_DEBUG_ANY,
159                                 "backend_startup: bi_db_open failed!\n",
160                                 0, 0, 0 );
161                         return rc;
162                 }
163
164                 return rc;
165         }
166
167         /* open each backend type */
168         for( i = 0; i < nBackendInfo; i++ ) {
169                 if( backendInfo[i].bi_nDB == 0) {
170                         /* no database of this type, don't open */
171                         continue;
172                 }
173
174                 if( backendInfo[i].bi_open ) {
175                         rc = backendInfo[i].bi_open(
176                                 &backendInfo[i] );
177                 }
178
179                 if(rc != 0) {
180                         Debug( LDAP_DEBUG_ANY,
181                                 "backend_startup: bi_open %d failed!\n",
182                                 i, 0, 0 );
183                         return rc;
184                 }
185         }
186
187         /* open each backend database */
188         for( i = 0; i < nBackendDB; i++ ) {
189                 if ( backendDB[i].bd_info->bi_db_open ) {
190                         rc = backendDB[i].bd_info->bi_db_open(
191                                 &backendDB[i] );
192                 }
193
194                 if(rc != 0) {
195                         Debug( LDAP_DEBUG_ANY,
196                                 "backend_startup: bi_db_open %d failed!\n",
197                                 i, 0, 0 );
198                         return rc;
199                 }
200         }
201
202         return rc;
203 }
204
205 int backend_shutdown(int n)
206 {
207         int i;
208         int rc = 0;
209
210         if(n >= 0) {
211                 /* shutdown a specific backend database */
212
213                 /* make sure, n does not exceed the number of backend databases */
214                 if ( n >= nbackends ) {
215
216                         Debug( LDAP_DEBUG_ANY,
217                                 "backend_startup: database number %d exceeding maximum (%d)\n",
218                                 n, nbackends, 0 );
219                         return 1;
220                 }
221
222                 if ( backendDB[n].bd_info->bi_nDB == 0 ) {
223                         /* no database of this type, we never opened it */
224                         return 0;
225                 }
226
227                 if ( backendDB[n].bd_info->bi_db_close ) {
228                         backendDB[n].bd_info->bi_db_close(
229                                 &backendDB[n] );
230                 }
231
232                 if( backendDB[n].bd_info->bi_close ) {
233                         backendDB[n].bd_info->bi_close(
234                                 backendDB[n].bd_info );
235                 }
236
237                 return 0;
238         }
239
240         /* close each backend database */
241         for( i = 0; i < nBackendDB; i++ ) {
242                 BackendInfo  *bi;
243
244                 if ( backendDB[i].bd_info->bi_db_close ) {
245                         backendDB[i].bd_info->bi_db_close(
246                                 &backendDB[i] );
247                 }
248
249                 if(rc != 0) {
250                         Debug( LDAP_DEBUG_ANY,
251                                 "backend_close: bi_close %s failed!\n",
252                                 bi->bi_type, 0, 0 );
253                 }
254         }
255
256         /* close each backend type */
257         for( i = 0; i < nBackendInfo; i++ ) {
258                 if( backendInfo[i].bi_nDB == 0 ) {
259                         /* no database of this type */
260                         continue;
261                 }
262
263                 if( backendInfo[i].bi_close ) {
264                         backendInfo[i].bi_close(
265                                 &backendInfo[i] );
266                 }
267         }
268
269         return 0;
270 }
271
272 int backend_destroy(void)
273 {
274         int i;
275
276         /* destroy each backend database */
277         for( i = 0; i < nBackendDB; i++ ) {
278                 if ( backendDB[i].bd_info->bi_db_destroy ) {
279                         backendDB[i].bd_info->bi_db_destroy(
280                                 &backendDB[i] );
281                 }
282         }
283
284         /* destroy each backend type */
285         for( i = 0; i < nBackendInfo; i++ ) {
286                 if( backendInfo[i].bi_destroy ) {
287                         backendInfo[i].bi_destroy(
288                                 &backendInfo[i] );
289                 }
290         }
291
292         return 0;
293 }
294
295 BackendInfo* backend_info(char *type)
296 {
297         int i;
298
299         /* search for the backend type */
300         for( i = 0; i < nBackendInfo; i++ ) {
301                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
302                         return &backendInfo[i];
303                 }
304         }
305
306         return NULL;
307 }
308
309
310 BackendDB *
311 backend_db_init(
312     char        *type
313 )
314 {
315         Backend *be;
316         BackendInfo *bi = backend_info(type);
317         int     rc = 0;
318
319         if( bi == NULL ) {
320                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
321                 return NULL;
322         }
323
324         backendDB = (BackendDB *) ch_realloc(
325                         (char *) backendDB,
326                     (nBackendDB + 1) * sizeof(Backend) );
327
328         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
329
330         be = &backends[nbackends++];
331
332         be->bd_info = bi;
333         be->be_sizelimit = defsize;
334         be->be_timelimit = deftime;
335
336         if(bi->bi_db_init) {
337                 rc = bi->bi_db_init( be );
338         }
339
340         if(rc != 0) {
341                 fprintf( stderr, "database init failed (%s)\n", type );
342                 nbackends--;
343                 return NULL;
344         }
345
346         bi->bi_nDB++;
347         return( be );
348 }
349
350 void
351 be_db_close( void )
352 {
353         int     i;
354
355         for ( i = 0; i < nbackends; i++ ) {
356                 if ( backends[i].bd_info->bi_db_close ) {
357                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
358                 }
359         }
360 }
361
362 Backend *
363 select_backend( char * dn )
364 {
365         int     i, j, len, dnlen;
366
367         dnlen = strlen( dn );
368         for ( i = 0; i < nbackends; i++ ) {
369                 for ( j = 0; backends[i].be_suffix != NULL &&
370                     backends[i].be_suffix[j] != NULL; j++ )
371                 {
372                         len = strlen( backends[i].be_suffix[j] );
373
374                         if ( len > dnlen ) {
375                                 continue;
376                         }
377
378                         if ( strcmp( backends[i].be_suffix[j],
379                             dn + (dnlen - len) ) == 0 ) {
380                                 return( &backends[i] );
381                         }
382                 }
383         }
384
385         /* if no proper suffix could be found then check for aliases */
386         for ( i = 0; i < nbackends; i++ ) {
387                 for ( j = 0; 
388                       backends[i].be_suffixAlias != NULL && 
389                       backends[i].be_suffixAlias[j] != NULL; 
390                       j += 2 )
391                 {
392                         len = strlen( backends[i].be_suffixAlias[j] );
393
394                         if ( len > dnlen ) {
395                                 continue;
396                         }
397
398                         if ( strcmp( backends[i].be_suffixAlias[j],
399                             dn + (dnlen - len) ) == 0 ) {
400                                 return( &backends[i] );
401                         }
402                 }
403         }
404
405 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
406         /* Add greg@greg.rim.or.jp
407          * It's quick hack for cheap client
408          * Some browser offer a NULL base at ldap_search
409          *
410          * Should only be used as a last resort. -Kdz
411          */
412         if(dnlen == 0) {
413                 Debug( LDAP_DEBUG_TRACE,
414                         "select_backend: use default backend\n", 0, 0, 0 );
415                 return( &backends[0] );
416         }
417 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
418
419         return( NULL );
420 }
421
422 int
423 be_issuffix(
424     Backend     *be,
425     char        *suffix
426 )
427 {
428         int     i;
429
430         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
431                 if ( strcmp( be->be_suffix[i], suffix ) == 0 ) {
432                         return( 1 );
433                 }
434         }
435
436         return( 0 );
437 }
438
439 int
440 be_isroot( Backend *be, char *ndn )
441 {
442         int rc;
443
444         if ( ndn == NULL || be->be_root_ndn == NULL ) {
445                 return( 0 );
446         }
447
448         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
449
450         return(rc);
451 }
452
453 char *
454 be_root_dn( Backend *be )
455 {
456         if ( be->be_root_dn == NULL ) {
457                 return( "" );
458         }
459
460         return be->be_root_dn;
461 }
462
463 int
464 be_isroot_pw( Backend *be, char *ndn, struct berval *cred )
465 {
466         int result;
467
468         if ( ! be_isroot( be, ndn ) ) {
469                 return( 0 );
470         }
471
472 #ifdef SLAPD_CRYPT
473         ldap_pvt_thread_mutex_lock( &crypt_mutex );
474 #endif
475
476         result = lutil_passwd( cred->bv_val, be->be_root_pw );
477
478 #ifdef SLAPD_CRYPT
479         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
480 #endif
481
482         return result == 0;
483 }
484
485 int
486 backend_unbind(
487         Connection   *conn,
488         Operation    *op
489 )
490 {
491         int     i;
492
493         for ( i = 0; i < nbackends; i++ ) {
494                 if ( backends[i].be_unbind ) {
495                         (*backends[i].be_unbind)( &backends[i], conn, op );
496                 }
497         }
498
499         return 0;
500 }
501
502 #ifdef SLAPD_ACLGROUPS
503 int 
504 backend_group(
505         Backend *be,
506         Entry   *target,
507         char    *gr_ndn,
508         char    *op_ndn,
509         char    *objectclassValue,
510         char    *groupattrName
511 )
512 {
513         if (be->be_group)
514                 return( be->be_group(be, target, gr_ndn, op_ndn,
515                         objectclassValue, groupattrName) );
516         else
517                 return(1);
518 }
519 #endif