3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* backend.c - routines for dealing with back-end databases */
13 #include <ac/string.h>
14 #include <ac/socket.h>
22 #include "back-bdb/external.h"
25 #include "back-dnssrv/external.h"
28 #include "back-ldap/external.h"
31 #include "back-ldbm/external.h"
34 #include "back-meta/external.h"
37 #include "back-monitor/external.h"
40 #include "back-passwd/external.h"
43 #include "back-perl/external.h"
46 #include "back-shell/external.h"
49 #include "back-tcl/external.h"
52 #include "back-sql/external.h"
55 #include "private/external.h"
58 static BackendInfo binfo[] = {
59 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
60 {"bdb", bdb_initialize},
62 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
63 {"dnssrv", dnssrv_back_initialize},
65 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
66 {"ldap", ldap_back_initialize},
68 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
69 {"ldbm", ldbm_back_initialize},
71 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
72 {"meta", meta_back_initialize},
74 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
75 {"monitor", monitor_back_initialize},
77 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
78 {"passwd", passwd_back_initialize},
80 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
81 {"perl", perl_back_initialize},
83 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
84 {"shell", shell_back_initialize},
86 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
87 {"tcl", tcl_back_initialize},
89 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
90 {"sql", sql_back_initialize},
92 /* for any private backend */
93 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
94 {"private", private_back_initialize},
100 BackendInfo *backendInfo = NULL;
103 BackendDB *backendDB = NULL;
105 int backend_init(void)
109 if((nBackendInfo != 0) || (backendInfo != NULL)) {
110 /* already initialized */
112 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
113 "backend_init: backend already initialized\n" ));
115 Debug( LDAP_DEBUG_ANY,
116 "backend_init: already initialized.\n", 0, 0, 0 );
122 binfo[nBackendInfo].bi_type != NULL;
125 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
129 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
130 "backend_init: initialized for type \"%s\"\n",
131 binfo[nBackendInfo].bi_type ));
133 Debug( LDAP_DEBUG_ANY,
134 "backend_init: initialized for type \"%s\"\n",
135 binfo[nBackendInfo].bi_type, 0, 0 );
137 /* destroy those we've already inited */
142 if ( binfo[nBackendInfo].bi_destroy ) {
143 binfo[nBackendInfo].bi_destroy(
144 &binfo[nBackendInfo] );
151 if ( nBackendInfo > 0) {
161 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
162 "backend_init: failed\n" ));
164 Debug( LDAP_DEBUG_ANY,
165 "backend_init: failed\n",
170 #endif /* SLAPD_MODULES */
173 int backend_add(BackendInfo *aBackendInfo)
177 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
179 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
180 "backend_add: initialization for type \"%s\" failed\n",
181 aBackendInfo->bi_type ));
183 Debug( LDAP_DEBUG_ANY,
184 "backend_add: initialization for type \"%s\" failed\n",
185 aBackendInfo->bi_type, 0, 0 );
190 /* now add the backend type to the Backend Info List */
192 BackendInfo *newBackendInfo = 0;
194 /* if backendInfo == binfo no deallocation of old backendInfo */
195 if (backendInfo == binfo) {
196 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
197 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
200 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
203 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
204 sizeof(BackendInfo));
205 backendInfo = newBackendInfo;
212 int backend_startup(Backend *be)
217 if( ! ( nBackendDB > 0 ) ) {
220 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
221 "backend_startup: %d databases to startup. \n",
224 Debug( LDAP_DEBUG_ANY,
225 "backend_startup: %d databases to startup.\n",
232 /* startup a specific backend database */
234 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
235 "backend_startup: starting \"%s\"\n",
238 Debug( LDAP_DEBUG_TRACE,
239 "backend_startup: starting \"%s\"\n",
240 be->be_suffix[0], 0, 0 );
243 if ( be->bd_info->bi_open ) {
244 rc = be->bd_info->bi_open( be->bd_info );
249 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
250 "backend_startup: bi_open failed!\n" ));
252 Debug( LDAP_DEBUG_ANY,
253 "backend_startup: bi_open failed!\n",
260 if ( be->bd_info->bi_db_open ) {
261 rc = be->bd_info->bi_db_open( be );
266 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
267 "backend_startup: bi_db_open failed!\n" ));
269 Debug( LDAP_DEBUG_ANY,
270 "backend_startup: bi_db_open failed!\n",
279 /* open each backend type */
280 for( i = 0; i < nBackendInfo; i++ ) {
281 if( backendInfo[i].bi_nDB == 0) {
282 /* no database of this type, don't open */
286 if( backendInfo[i].bi_open ) {
287 rc = backendInfo[i].bi_open(
293 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
294 "backend_startup: bi_open %d failed!\n", i ));
296 Debug( LDAP_DEBUG_ANY,
297 "backend_startup: bi_open %d failed!\n",
304 /* open each backend database */
305 for( i = 0; i < nBackendDB; i++ ) {
306 /* append global access controls */
307 acl_append( &backendDB[i].be_acl, global_acl );
309 if ( backendDB[i].bd_info->bi_db_open ) {
310 rc = backendDB[i].bd_info->bi_db_open(
316 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
317 "backend_startup: bi_db_open %d failed!\n", i ));
319 Debug( LDAP_DEBUG_ANY,
320 "backend_startup: bi_db_open %d failed!\n",
330 int backend_num( Backend *be )
334 if( be == NULL ) return -1;
336 for( i = 0; i < nBackendDB; i++ ) {
337 if( be == &backendDB[i] ) return i;
342 int backend_shutdown( Backend *be )
348 /* shutdown a specific backend database */
350 if ( be->bd_info->bi_nDB == 0 ) {
351 /* no database of this type, we never opened it */
355 if ( be->bd_info->bi_db_close ) {
356 be->bd_info->bi_db_close( be );
359 if( be->bd_info->bi_close ) {
360 be->bd_info->bi_close( be->bd_info );
366 /* close each backend database */
367 for( i = 0; i < nBackendDB; i++ ) {
368 if ( backendDB[i].bd_info->bi_db_close ) {
369 backendDB[i].bd_info->bi_db_close(
375 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
376 "backend_shutdown: bi_close %s failed!\n",
377 backendDB[i].be_type ));
379 Debug( LDAP_DEBUG_ANY,
380 "backend_close: bi_close %s failed!\n",
381 backendDB[i].be_type, 0, 0 );
386 /* close each backend type */
387 for( i = 0; i < nBackendInfo; i++ ) {
388 if( backendInfo[i].bi_nDB == 0 ) {
389 /* no database of this type */
393 if( backendInfo[i].bi_close ) {
394 backendInfo[i].bi_close(
402 int backend_destroy(void)
406 /* destroy each backend database */
407 for( i = 0; i < nBackendDB; i++ ) {
408 if ( backendDB[i].bd_info->bi_db_destroy ) {
409 backendDB[i].bd_info->bi_db_destroy(
414 /* destroy each backend type */
415 for( i = 0; i < nBackendInfo; i++ ) {
416 if( backendInfo[i].bi_destroy ) {
417 backendInfo[i].bi_destroy(
423 if (backendInfo != binfo) {
426 #endif /* SLAPD_MODULES */
434 BackendInfo* backend_info(const char *type)
438 /* search for the backend type */
439 for( i = 0; i < nBackendInfo; i++ ) {
440 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
441 return &backendInfo[i];
455 BackendInfo *bi = backend_info(type);
459 fprintf( stderr, "Unrecognized database type (%s)\n", type );
463 backendDB = (BackendDB *) ch_realloc(
465 (nBackendDB + 1) * sizeof(Backend) );
467 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
469 be = &backends[nbackends++];
472 be->be_def_limit = deflimit;
473 be->be_dfltaccess = global_default_access;
475 be->be_restrictops = global_restrictops;
476 be->be_requires = global_requires;
478 /* assign a default depth limit for alias deref */
479 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
482 rc = bi->bi_db_init( be );
486 fprintf( stderr, "database init failed (%s)\n", type );
500 for ( i = 0; i < nbackends; i++ ) {
501 if ( backends[i].bd_info->bi_db_close ) {
502 (*backends[i].bd_info->bi_db_close)( &backends[i] );
512 int i, j, len, dnlen;
515 dnlen = strlen( dn );
516 for ( i = 0; i < nbackends; i++ ) {
517 for ( j = 0; backends[i].be_nsuffix != NULL &&
518 backends[i].be_nsuffix[j] != NULL; j++ )
520 len = strlen( backends[i].be_nsuffix[j] );
523 /* suffix is longer than DN */
528 if ( len && len < dnlen && !DN_SEPARATOR( dn[(dnlen-len)-1] ) ) {
529 /* make sure we have a separator */
534 if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
538 if( manageDSAit && len == dnlen ) {
560 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
561 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
570 be_isroot( Backend *be, const char *ndn )
574 if ( ndn == NULL || *ndn == '\0' ) {
578 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
582 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
588 be_root_dn( Backend *be )
590 if ( be->be_root_dn == NULL ) {
594 return be->be_root_dn;
598 be_isroot_pw( Backend *be,
601 struct berval *cred )
605 if ( ! be_isroot( be, ndn ) ) {
609 if( be->be_root_pw.bv_len == 0 ) {
613 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
614 ldap_pvt_thread_mutex_lock( &passwd_mutex );
616 lutil_passwd_sasl_conn = conn->c_sasl_context;
620 result = lutil_passwd( &be->be_root_pw, cred, NULL );
622 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
624 lutil_passwd_sasl_conn = NULL;
626 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
640 if ( be->be_release ) {
641 /* free and release entry from backend */
642 return be->be_release( be, conn, op, e, rw );
658 for ( i = 0; i < nbackends; i++ ) {
659 if ( backends[i].be_unbind ) {
660 (*backends[i].be_unbind)( &backends[i], conn, op );
668 backend_connection_init(
674 for ( i = 0; i < nbackends; i++ ) {
675 if ( backends[i].be_connection_init ) {
676 (*backends[i].be_connection_init)( &backends[i], conn);
684 backend_connection_destroy(
690 for ( i = 0; i < nbackends; i++ ) {
691 if ( backends[i].be_connection_destroy ) {
692 (*backends[i].be_connection_destroy)( &backends[i], conn);
700 backend_check_controls(
708 if( ctrls == NULL ) {
712 for( ; *ctrls != NULL ; ctrls++ ) {
713 if( (*ctrls)->ldctl_iscritical &&
714 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
716 *text = "control unavailable in NamingContext";
717 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
725 backend_check_restrictions(
733 slap_mask_t restrictops;
734 slap_mask_t requires;
740 rc = backend_check_controls( be, conn, op, text );
742 if( rc != LDAP_SUCCESS ) {
746 restrictops = be->be_restrictops;
747 requires = be->be_requires;
748 ssf = &be->be_ssf_set;
751 restrictops = global_restrictops;
752 requires = global_requires;
753 ssf = &global_ssf_set;
756 switch( op->o_tag ) {
758 opflag = SLAP_RESTRICT_OP_ADD;
762 opflag = SLAP_RESTRICT_OP_BIND;
764 case LDAP_REQ_COMPARE:
765 opflag = SLAP_RESTRICT_OP_COMPARE;
767 case LDAP_REQ_DELETE:
769 opflag = SLAP_RESTRICT_OP_DELETE;
771 case LDAP_REQ_EXTENDED:
772 opflag = SLAP_RESTRICT_OP_EXTENDED;
774 case LDAP_REQ_MODIFY:
776 opflag = SLAP_RESTRICT_OP_MODIFY;
778 case LDAP_REQ_RENAME:
780 opflag = SLAP_RESTRICT_OP_RENAME;
782 case LDAP_REQ_SEARCH:
783 opflag = SLAP_RESTRICT_OP_SEARCH;
785 case LDAP_REQ_UNBIND:
789 *text = "restrict operations internal error";
793 if ( op->o_tag != LDAP_REQ_EXTENDED
794 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
796 /* these checks don't apply to StartTLS */
798 if( op->o_tag == LDAP_REQ_EXTENDED ) {
799 /* threat other extended operations as update ops */
803 if( op->o_transport_ssf < ssf->sss_transport ) {
804 *text = "transport confidentiality required";
805 return LDAP_CONFIDENTIALITY_REQUIRED;
808 if( op->o_tls_ssf < ssf->sss_tls ) {
809 *text = "TLS confidentiality required";
810 return LDAP_CONFIDENTIALITY_REQUIRED;
813 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
814 /* these checks don't apply to SASL bind */
816 if( op->o_sasl_ssf < ssf->sss_sasl ) {
817 *text = "SASL confidentiality required";
818 return LDAP_CONFIDENTIALITY_REQUIRED;
821 if( op->o_ssf < ssf->sss_ssf ) {
822 *text = "confidentiality required";
823 return LDAP_CONFIDENTIALITY_REQUIRED;
828 if( op->o_transport_ssf < ssf->sss_update_transport ) {
829 *text = "transport update confidentiality required";
830 return LDAP_CONFIDENTIALITY_REQUIRED;
833 if( op->o_tls_ssf < ssf->sss_update_tls ) {
834 *text = "TLS update confidentiality required";
835 return LDAP_CONFIDENTIALITY_REQUIRED;
838 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
839 *text = "SASL update confidentiality required";
840 return LDAP_CONFIDENTIALITY_REQUIRED;
843 if( op->o_ssf < ssf->sss_update_ssf ) {
844 *text = "update confidentiality required";
845 return LDAP_CONFIDENTIALITY_REQUIRED;
848 if( op->o_ndn == NULL ) {
849 *text = "modifications require authentication";
850 return LDAP_OPERATIONS_ERROR;
855 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
856 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
858 /* these checks don't apply to Bind or StartTLS */
860 if( requires & SLAP_REQUIRE_STRONG ) {
861 /* should check mechanism */
862 if( op->o_authmech == NULL ||
863 op->o_dn == NULL || *op->o_dn == '\0' )
865 *text = "strong authentication required";
866 return LDAP_STRONG_AUTH_REQUIRED;
870 if( requires & SLAP_REQUIRE_SASL ) {
871 if( op->o_authmech == NULL ||
872 op->o_dn == NULL || *op->o_dn == '\0' )
874 *text = "SASL authentication required";
875 return LDAP_STRONG_AUTH_REQUIRED;
879 if( requires & SLAP_REQUIRE_AUTHC ) {
880 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
881 *text = "authentication required";
882 return LDAP_UNWILLING_TO_PERFORM;
886 if( requires & SLAP_REQUIRE_BIND ) {
888 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
889 version = conn->c_protocol;
890 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
893 /* no bind has occurred */
894 *text = "BIND required";
895 return LDAP_OPERATIONS_ERROR;
899 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
900 if( op->o_protocol < LDAP_VERSION3 ) {
901 /* no bind has occurred */
902 *text = "operation restricted to LDAPv3 clients";
903 return LDAP_OPERATIONS_ERROR;
908 if( restrictops & opflag ) {
909 if( restrictops == SLAP_RESTRICT_OP_READS ) {
910 *text = "read operations restricted";
912 *text = "operation restricted";
914 return LDAP_UNWILLING_TO_PERFORM;
920 int backend_check_referrals(
927 int rc = LDAP_SUCCESS;
929 if( be->be_chk_referrals ) {
932 rc = be->be_chk_referrals( be,
933 conn, op, dn, ndn, &text );
935 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
936 send_ldap_result( conn, op, rc,
937 NULL, text, NULL, NULL );
952 ObjectClass *group_oc,
953 AttributeDescription *group_at
956 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
957 /* we won't attempt to send it to a different backend */
959 be = select_backend(gr_ndn, 0);
962 return LDAP_NO_SUCH_OBJECT;
967 return be->be_group( be, conn, op,
968 target, gr_ndn, op_ndn,
969 group_oc, group_at );
972 return LDAP_UNWILLING_TO_PERFORM;
982 AttributeDescription *entry_at,
983 struct berval ***vals
986 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
987 /* we won't attempt to send it to a different backend */
989 be = select_backend(e_ndn, 0);
992 return LDAP_NO_SUCH_OBJECT;
996 if( be->be_attribute ) {
997 return be->be_attribute( be, conn, op, target, e_ndn,
1001 return LDAP_UNWILLING_TO_PERFORM;
1004 Attribute *backend_operational(
1010 Attribute *a = NULL;
1012 #ifdef SLAPD_SCHEMA_DN
1013 a = ch_malloc( sizeof( Attribute ) );
1014 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1016 /* Should be backend specific */
1017 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1018 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1019 a->a_vals[1] = NULL;