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