]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Fix Debug() with too few arguments.
[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                 BackendInfo  *bi;
281
282                 if ( backendDB[i].bd_info->bi_db_close ) {
283                         backendDB[i].bd_info->bi_db_close(
284                                 &backendDB[i] );
285                 }
286
287                 if(rc != 0) {
288                         Debug( LDAP_DEBUG_ANY,
289                                 "backend_close: bi_close %s failed!\n",
290                                 bi->bi_type, 0, 0 );
291                 }
292         }
293
294         /* close each backend type */
295         for( i = 0; i < nBackendInfo; i++ ) {
296                 if( backendInfo[i].bi_nDB == 0 ) {
297                         /* no database of this type */
298                         continue;
299                 }
300
301                 if( backendInfo[i].bi_close ) {
302                         backendInfo[i].bi_close(
303                                 &backendInfo[i] );
304                 }
305         }
306
307         return 0;
308 }
309
310 int backend_destroy(void)
311 {
312         int i;
313
314         /* destroy each backend database */
315         for( i = 0; i < nBackendDB; i++ ) {
316                 if ( backendDB[i].bd_info->bi_db_destroy ) {
317                         backendDB[i].bd_info->bi_db_destroy(
318                                 &backendDB[i] );
319                 }
320         }
321
322         /* destroy each backend type */
323         for( i = 0; i < nBackendInfo; i++ ) {
324                 if( backendInfo[i].bi_destroy ) {
325                         backendInfo[i].bi_destroy(
326                                 &backendInfo[i] );
327                 }
328         }
329
330 #ifdef SLAPD_MODULES
331         if (backendInfo != binfo) {
332            free(backendInfo);
333         }
334 #endif /* SLAPD_MODULES */
335
336         nBackendInfo = 0;
337         backendInfo = NULL;
338
339         return 0;
340 }
341
342 BackendInfo* backend_info(const char *type)
343 {
344         int i;
345
346         /* search for the backend type */
347         for( i = 0; i < nBackendInfo; i++ ) {
348                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
349                         return &backendInfo[i];
350                 }
351         }
352
353         return NULL;
354 }
355
356
357 BackendDB *
358 backend_db_init(
359     const char  *type
360 )
361 {
362         Backend *be;
363         BackendInfo *bi = backend_info(type);
364         int     rc = 0;
365
366         if( bi == NULL ) {
367                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
368                 return NULL;
369         }
370
371         backendDB = (BackendDB *) ch_realloc(
372                         (char *) backendDB,
373                     (nBackendDB + 1) * sizeof(Backend) );
374
375         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
376
377         be = &backends[nbackends++];
378
379         be->bd_info = bi;
380         be->be_sizelimit = defsize;
381         be->be_timelimit = deftime;
382
383         /* assign a default depth limit for alias deref */
384         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
385
386         be->be_realm = global_realm != NULL
387                 ? ch_strdup( global_realm ) : NULL;
388
389         if(bi->bi_db_init) {
390                 rc = bi->bi_db_init( be );
391         }
392
393         if(rc != 0) {
394                 fprintf( stderr, "database init failed (%s)\n", type );
395                 nbackends--;
396                 return NULL;
397         }
398
399         bi->bi_nDB++;
400         return( be );
401 }
402
403 void
404 be_db_close( void )
405 {
406         int     i;
407
408         for ( i = 0; i < nbackends; i++ ) {
409                 if ( backends[i].bd_info->bi_db_close ) {
410                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
411                 }
412         }
413 }
414
415 Backend *
416 select_backend( const char * dn )
417 {
418         int     i, j, len, dnlen;
419
420         dnlen = strlen( dn );
421         for ( i = 0; i < nbackends; i++ ) {
422                 for ( j = 0; backends[i].be_nsuffix != NULL &&
423                     backends[i].be_nsuffix[j] != NULL; j++ )
424                 {
425                         len = strlen( backends[i].be_nsuffix[j] );
426
427                         if ( len > dnlen ) {
428                                 continue;
429                         }
430
431                         if ( strcmp( backends[i].be_nsuffix[j],
432                             dn + (dnlen - len) ) == 0 ) {
433                                 return( &backends[i] );
434                         }
435                 }
436         }
437
438 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
439         /* Add greg@greg.rim.or.jp
440          * It's quick hack for cheap client
441          * Some browser offer a NULL base at ldap_search
442          *
443          * Should only be used as a last resort. -Kdz
444          */
445         if(dnlen == 0) {
446                 Debug( LDAP_DEBUG_TRACE,
447                         "select_backend: use default backend\n", 0, 0, 0 );
448                 return( &backends[0] );
449         }
450 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
451
452         return( NULL );
453 }
454
455 int
456 be_issuffix(
457     Backend     *be,
458     const char  *suffix
459 )
460 {
461         int     i;
462
463         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
464                 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
465                         return( 1 );
466                 }
467         }
468
469         return( 0 );
470 }
471
472 int
473 be_isroot( Backend *be, const char *ndn )
474 {
475         int rc;
476
477         if ( ndn == NULL || be->be_root_ndn == NULL ) {
478                 return( 0 );
479         }
480
481         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
482
483         return(rc);
484 }
485
486 char *
487 be_root_dn( Backend *be )
488 {
489         if ( be->be_root_dn == NULL ) {
490                 return( "" );
491         }
492
493         return be->be_root_dn;
494 }
495
496 int
497 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
498 {
499         int result;
500
501         if ( ! be_isroot( be, ndn ) ) {
502                 return( 0 );
503         }
504
505 #ifdef SLAPD_CRYPT
506         ldap_pvt_thread_mutex_lock( &crypt_mutex );
507 #endif
508
509         result = lutil_passwd( cred->bv_val, be->be_root_pw, NULL );
510
511 #ifdef SLAPD_CRYPT
512         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
513 #endif
514
515         return result == 0;
516 }
517
518 int
519 be_entry_release_rw( Backend *be, Entry *e, int rw )
520 {
521         if ( be->be_release ) {
522                 /* free and release entry from backend */
523                 return be->be_release( be, e, rw );
524         } else {
525                 /* free entry */
526                 entry_free( e );
527                 return 0;
528         }
529 }
530
531 int
532 backend_unbind(
533         Connection   *conn,
534         Operation    *op
535 )
536 {
537         int     i;
538
539         for ( i = 0; i < nbackends; i++ ) {
540                 if ( backends[i].be_unbind ) {
541                         (*backends[i].be_unbind)( &backends[i], conn, op );
542                 }
543         }
544
545         return 0;
546 }
547
548 int
549 backend_connection_init(
550         Connection   *conn
551 )
552 {
553         int     i;
554
555         for ( i = 0; i < nbackends; i++ ) {
556                 if ( backends[i].be_connection_init ) {
557                         (*backends[i].be_connection_init)( &backends[i], conn);
558                 }
559         }
560
561         return 0;
562 }
563
564 int
565 backend_connection_destroy(
566         Connection   *conn
567 )
568 {
569         int     i;
570
571         for ( i = 0; i < nbackends; i++ ) {
572                 if ( backends[i].be_connection_destroy ) {
573                         (*backends[i].be_connection_destroy)( &backends[i], conn);
574                 }
575         }
576
577         return 0;
578 }
579
580 int 
581 backend_group(
582         Backend *be,
583         Entry   *target,
584         const char      *gr_ndn,
585         const char      *op_ndn,
586         const char      *objectclassValue,
587         const char      *groupattrName
588 )
589 {
590         if (be->be_group)
591                 return( be->be_group(be, target, gr_ndn, op_ndn,
592                         objectclassValue, groupattrName) );
593         else
594                 return(1);
595 }
596
597 #ifdef SLAPD_SCHEMA_DN
598 Attribute *backend_subschemasubentry( Backend *be )
599 {
600         /* should be backend specific */
601         static struct berval ss_val = {
602                 sizeof(SLAPD_SCHEMA_DN)-1,
603                 SLAPD_SCHEMA_DN };
604         static struct berval *ss_vals[2] = { &ss_val, NULL };
605         static Attribute ss_attr = {
606                 "subschemasubentry",
607                 ss_vals,
608                 SYNTAX_DN | SYNTAX_CIS,
609                 NULL
610         };
611
612         return &ss_attr;
613 }
614 #endif