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