]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
872d08776aba6a90ced62c43b92f470469bc33c2
[openldap] / servers / slapd / backend.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* backend.c - routines for dealing with back-end databases */
6
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include <sys/stat.h>
16
17 #include "slap.h"
18 #include "lutil.h"
19
20 #include "ldap_defaults.h"
21
22 #ifdef SLAPD_LDAP
23 #include "back-ldap/external.h"
24 #endif
25 #ifdef SLAPD_LDBM
26 #include "back-ldbm/external.h"
27 #endif
28 #ifdef SLAPD_BDB2
29 #include "back-bdb2/external.h"
30 #endif
31 #ifdef SLAPD_PASSWD
32 #include "back-passwd/external.h"
33 #endif
34 #ifdef SLAPD_PERL
35 #include "back-perl/external.h"
36 #endif
37 #ifdef SLAPD_SHELL
38 #include "back-shell/external.h"
39 #endif
40 #ifdef SLAPD_TCL
41 #include "back-tcl/external.h"
42 #endif
43
44 static BackendInfo binfo[] = {
45 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
46         {"ldap",        ldap_back_initialize},
47 #endif
48 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
49         {"ldbm",        ldbm_back_initialize},
50 #endif
51 #if defined(SLAPD_BDB2) && !defined(SLAPD_BDB2_DYNAMIC)
52         {"bdb2",        bdb2_back_initialize},
53 #endif
54 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
55         {"passwd",      passwd_back_initialize},
56 #endif
57 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
58         {"perl",        perl_back_initialize},
59 #endif
60 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
61         {"shell",       shell_back_initialize},
62 #endif
63 #if defined(SLAPD_TCL) && !defined(SLAPD_LDAP_TCL)
64         {"tcl",         tcl_back_initialize},
65 #endif
66         {NULL}
67 };
68
69 int                     nBackendInfo = 0;
70 BackendInfo     *backendInfo = NULL;
71
72 int                     nBackendDB = 0; 
73 BackendDB       *backendDB = NULL;
74
75 int backend_init(void)
76 {
77         int rc = -1;
78
79         if((nBackendInfo != 0) || (backendInfo != NULL)) {
80                 /* already initialized */
81                 Debug( LDAP_DEBUG_ANY,
82                         "backend_init: already initialized.\n", 0, 0, 0 );
83                 return -1;
84         }
85
86         for( ;
87                 binfo[nBackendInfo].bi_type != NULL;
88                 nBackendInfo++ )
89         {
90                 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
91
92                 if(rc != 0) {
93                         Debug( LDAP_DEBUG_ANY,
94                                 "backend_init: initialized for type \"%s\"\n",
95                                         binfo[nBackendInfo].bi_type, 0, 0 );
96
97                         /* destroy those we've already inited */
98                         for( nBackendInfo--;
99                                 nBackendInfo >= 0 ;
100                                 nBackendInfo-- )
101                         { 
102                                 if ( binfo[nBackendInfo].bi_destroy ) {
103                                         binfo[nBackendInfo].bi_destroy(
104                                                 &binfo[nBackendInfo] );
105                                 }
106                         }
107                         return rc;
108                 }
109         }
110
111         if ( nBackendInfo > 0) {
112                 backendInfo = binfo;
113                 return 0;
114         }
115
116 #ifdef SLAPD_MODULES    
117         return 0;
118 #else
119         Debug( LDAP_DEBUG_ANY,
120                 "backend_init: failed\n",
121                 0, 0, 0 );
122
123         return rc;
124 #endif /* SLAPD_MODULES */
125 }
126
127 int backend_add(BackendInfo *aBackendInfo)
128 {
129    int rc = 0;
130
131    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
132       Debug( LDAP_DEBUG_ANY,
133              "backend_add: initialization for type \"%s\" failed\n",
134              aBackendInfo->bi_type, 0, 0 );
135       return rc;
136    }
137
138    /* now add the backend type to the Backend Info List */
139    {
140       BackendInfo *newBackendInfo = 0;
141
142       /* if backendInfo == binfo no deallocation of old backendInfo */
143       if (backendInfo == binfo) {
144          newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
145          memcpy(newBackendInfo, backendInfo, sizeof(BackendInfo) * 
146                 nBackendInfo);
147       } else {
148          newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) * 
149                                      (nBackendInfo + 1));
150       }
151       memcpy(&newBackendInfo[nBackendInfo], aBackendInfo, 
152              sizeof(BackendInfo));
153       backendInfo = newBackendInfo;
154       nBackendInfo++;
155
156       return 0;
157    }        
158 }
159
160 int backend_startup(Backend *be)
161 {
162         int i;
163         int rc = 0;
164
165         if( ! ( nBackendDB > 0 ) ) {
166                 /* no databases */
167                 Debug( LDAP_DEBUG_ANY,
168                         "backend_startup: %d databases to startup.\n",
169                         nBackendDB, 0, 0 );
170                 return 1;
171         }
172
173         if(be != NULL) {
174                 /* startup a specific backend database */
175                 Debug( LDAP_DEBUG_TRACE,
176                         "backend_startup: starting database\n",
177                         0, 0, 0 );
178
179                 if ( be->bd_info->bi_open ) {
180                         rc = be->bd_info->bi_open( be->bd_info );
181                 }
182
183                 if(rc != 0) {
184                         Debug( LDAP_DEBUG_ANY,
185                                 "backend_startup: bi_open failed!\n",
186                                 0, 0, 0 );
187                         return rc;
188                 }
189
190                 if ( be->bd_info->bi_db_open ) {
191                         rc = be->bd_info->bi_db_open( be );
192                 }
193
194                 if(rc != 0) {
195                         Debug( LDAP_DEBUG_ANY,
196                                 "backend_startup: bi_db_open failed!\n",
197                                 0, 0, 0 );
198                         return rc;
199                 }
200
201                 return rc;
202         }
203
204         /* open each backend type */
205         for( i = 0; i < nBackendInfo; i++ ) {
206                 if( backendInfo[i].bi_nDB == 0) {
207                         /* no database of this type, don't open */
208                         continue;
209                 }
210
211                 if( backendInfo[i].bi_open ) {
212                         rc = backendInfo[i].bi_open(
213                                 &backendInfo[i] );
214                 }
215
216                 if(rc != 0) {
217                         Debug( LDAP_DEBUG_ANY,
218                                 "backend_startup: bi_open %d failed!\n",
219                                 i, 0, 0 );
220                         return rc;
221                 }
222         }
223
224         /* open each backend database */
225         for( i = 0; i < nBackendDB; i++ ) {
226                 if ( backendDB[i].bd_info->bi_db_open ) {
227                         rc = backendDB[i].bd_info->bi_db_open(
228                                 &backendDB[i] );
229                 }
230
231                 if(rc != 0) {
232                         Debug( LDAP_DEBUG_ANY,
233                                 "backend_startup: bi_db_open %d failed!\n",
234                                 i, 0, 0 );
235                         return rc;
236                 }
237         }
238
239         return rc;
240 }
241
242 int backend_num( Backend *be )
243 {
244         int i;
245
246         if( be == NULL ) return -1;
247
248         for( i = 0; i < nBackendDB; i++ ) {
249                 if( be == &backendDB[i] ) return i;
250         }
251         return -1;
252 }
253
254 int backend_shutdown( Backend *be )
255 {
256         int i;
257         int rc = 0;
258
259         if( be != NULL ) {
260                 /* shutdown a specific backend database */
261
262                 if ( be->bd_info->bi_nDB == 0 ) {
263                         /* no database of this type, we never opened it */
264                         return 0;
265                 }
266
267                 if ( be->bd_info->bi_db_close ) {
268                         be->bd_info->bi_db_close( be );
269                 }
270
271                 if( be->bd_info->bi_close ) {
272                         be->bd_info->bi_close( be->bd_info );
273                 }
274
275                 return 0;
276         }
277
278         /* close each backend database */
279         for( i = 0; i < nBackendDB; i++ ) {
280                 if ( backendDB[i].bd_info->bi_db_close ) {
281                         backendDB[i].bd_info->bi_db_close(
282                                 &backendDB[i] );
283                 }
284
285                 if(rc != 0) {
286                         Debug( LDAP_DEBUG_ANY,
287                                 "backend_close: bi_close %s failed!\n",
288                                 backendDB[i].be_type, 0, 0 );
289                 }
290         }
291
292         /* close each backend type */
293         for( i = 0; i < nBackendInfo; i++ ) {
294                 if( backendInfo[i].bi_nDB == 0 ) {
295                         /* no database of this type */
296                         continue;
297                 }
298
299                 if( backendInfo[i].bi_close ) {
300                         backendInfo[i].bi_close(
301                                 &backendInfo[i] );
302                 }
303         }
304
305         return 0;
306 }
307
308 int backend_destroy(void)
309 {
310         int i;
311
312         /* destroy each backend database */
313         for( i = 0; i < nBackendDB; i++ ) {
314                 if ( backendDB[i].bd_info->bi_db_destroy ) {
315                         backendDB[i].bd_info->bi_db_destroy(
316                                 &backendDB[i] );
317                 }
318         }
319
320         /* destroy each backend type */
321         for( i = 0; i < nBackendInfo; i++ ) {
322                 if( backendInfo[i].bi_destroy ) {
323                         backendInfo[i].bi_destroy(
324                                 &backendInfo[i] );
325                 }
326         }
327
328 #ifdef SLAPD_MODULES
329         if (backendInfo != binfo) {
330            free(backendInfo);
331         }
332 #endif /* SLAPD_MODULES */
333
334         nBackendInfo = 0;
335         backendInfo = NULL;
336
337         return 0;
338 }
339
340 BackendInfo* backend_info(const char *type)
341 {
342         int i;
343
344         /* search for the backend type */
345         for( i = 0; i < nBackendInfo; i++ ) {
346                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
347                         return &backendInfo[i];
348                 }
349         }
350
351         return NULL;
352 }
353
354
355 BackendDB *
356 backend_db_init(
357     const char  *type
358 )
359 {
360         Backend *be;
361         BackendInfo *bi = backend_info(type);
362         int     rc = 0;
363
364         if( bi == NULL ) {
365                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
366                 return NULL;
367         }
368
369         backendDB = (BackendDB *) ch_realloc(
370                         (char *) backendDB,
371                     (nBackendDB + 1) * sizeof(Backend) );
372
373         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
374
375         be = &backends[nbackends++];
376
377         be->bd_info = bi;
378         be->be_sizelimit = defsize;
379         be->be_timelimit = deftime;
380
381         /* assign a default depth limit for alias deref */
382         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
383
384         be->be_realm = global_realm != NULL
385                 ? ch_strdup( global_realm ) : NULL;
386
387         if(bi->bi_db_init) {
388                 rc = bi->bi_db_init( be );
389         }
390
391         if(rc != 0) {
392                 fprintf( stderr, "database init failed (%s)\n", type );
393                 nbackends--;
394                 return NULL;
395         }
396
397         bi->bi_nDB++;
398         return( be );
399 }
400
401 void
402 be_db_close( void )
403 {
404         int     i;
405
406         for ( i = 0; i < nbackends; i++ ) {
407                 if ( backends[i].bd_info->bi_db_close ) {
408                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
409                 }
410         }
411 }
412
413 Backend *
414 select_backend( const char * dn )
415 {
416         int     i, j, len, dnlen;
417
418         dnlen = strlen( dn );
419         for ( i = 0; i < nbackends; i++ ) {
420                 for ( j = 0; backends[i].be_nsuffix != NULL &&
421                     backends[i].be_nsuffix[j] != NULL; j++ )
422                 {
423                         len = strlen( backends[i].be_nsuffix[j] );
424
425                         if ( len > dnlen ) {
426                                 continue;
427                         }
428
429                         if ( strcmp( backends[i].be_nsuffix[j],
430                             dn + (dnlen - len) ) == 0 ) {
431                                 return( &backends[i] );
432                         }
433                 }
434         }
435
436 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
437         /* Add greg@greg.rim.or.jp
438          * It's quick hack for cheap client
439          * Some browser offer a NULL base at ldap_search
440          *
441          * Should only be used as a last resort. -Kdz
442          */
443         if(dnlen == 0) {
444                 Debug( LDAP_DEBUG_TRACE,
445                         "select_backend: use default backend\n", 0, 0, 0 );
446                 return( &backends[0] );
447         }
448 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
449
450         return( NULL );
451 }
452
453 int
454 be_issuffix(
455     Backend     *be,
456     const char  *suffix
457 )
458 {
459         int     i;
460
461         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
462                 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
463                         return( 1 );
464                 }
465         }
466
467         return( 0 );
468 }
469
470 int
471 be_isroot( Backend *be, const char *ndn )
472 {
473         int rc;
474
475         if ( ndn == NULL || be->be_root_ndn == NULL ) {
476                 return( 0 );
477         }
478
479         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
480
481         return(rc);
482 }
483
484 char *
485 be_root_dn( Backend *be )
486 {
487         if ( be->be_root_dn == NULL ) {
488                 return( "" );
489         }
490
491         return be->be_root_dn;
492 }
493
494 int
495 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
496 {
497         int result;
498
499         if ( ! be_isroot( be, ndn ) ) {
500                 return( 0 );
501         }
502
503 #ifdef SLAPD_CRYPT
504         ldap_pvt_thread_mutex_lock( &crypt_mutex );
505 #endif
506
507         result = lutil_passwd( cred->bv_val, be->be_root_pw, NULL );
508
509 #ifdef SLAPD_CRYPT
510         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
511 #endif
512
513         return result == 0;
514 }
515
516 int
517 be_entry_release_rw( Backend *be, Entry *e, int rw )
518 {
519         if ( be->be_release ) {
520                 /* free and release entry from backend */
521                 return be->be_release( be, e, rw );
522         } else {
523                 /* free entry */
524                 entry_free( e );
525                 return 0;
526         }
527 }
528
529 int
530 backend_unbind(
531         Connection   *conn,
532         Operation    *op
533 )
534 {
535         int     i;
536
537         for ( i = 0; i < nbackends; i++ ) {
538                 if ( backends[i].be_unbind ) {
539                         (*backends[i].be_unbind)( &backends[i], conn, op );
540                 }
541         }
542
543         return 0;
544 }
545
546 int
547 backend_connection_init(
548         Connection   *conn
549 )
550 {
551         int     i;
552
553         for ( i = 0; i < nbackends; i++ ) {
554                 if ( backends[i].be_connection_init ) {
555                         (*backends[i].be_connection_init)( &backends[i], conn);
556                 }
557         }
558
559         return 0;
560 }
561
562 int
563 backend_connection_destroy(
564         Connection   *conn
565 )
566 {
567         int     i;
568
569         for ( i = 0; i < nbackends; i++ ) {
570                 if ( backends[i].be_connection_destroy ) {
571                         (*backends[i].be_connection_destroy)( &backends[i], conn);
572                 }
573         }
574
575         return 0;
576 }
577
578 int 
579 backend_group(
580         Backend *be,
581         Entry   *target,
582         const char      *gr_ndn,
583         const char      *op_ndn,
584         const char      *objectclassValue,
585         const char      *groupattrName
586 )
587 {
588         if (be->be_group)
589                 return( be->be_group(be, target, gr_ndn, op_ndn,
590                         objectclassValue, groupattrName) );
591         else
592                 return(1);
593 }
594
595 #ifdef SLAPD_SCHEMA_DN
596 Attribute *backend_subschemasubentry( Backend *be )
597 {
598         /* should be backend specific */
599         static struct berval ss_val = {
600                 sizeof(SLAPD_SCHEMA_DN)-1,
601                 SLAPD_SCHEMA_DN };
602         static struct berval *ss_vals[2] = { &ss_val, NULL };
603         static Attribute ss_attr = {
604                 "subschemasubentry",
605                 ss_vals,
606                 SYNTAX_DN | SYNTAX_CIS,
607                 NULL
608         };
609
610         return &ss_attr;
611 }
612 #endif