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-passwd/external.h"
40 #include "back-perl/external.h"
43 #include "back-shell/external.h"
46 #include "back-tcl/external.h"
49 #include "back-sql/external.h"
52 #include "private/external.h"
55 static BackendInfo binfo[] = {
56 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
57 {"bdb", bdb_initialize},
59 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
60 {"dnssrv", dnssrv_back_initialize},
62 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
63 {"ldap", ldap_back_initialize},
65 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
66 {"ldbm", ldbm_back_initialize},
68 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
69 {"meta", meta_back_initialize},
71 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
72 {"passwd", passwd_back_initialize},
74 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
75 {"perl", perl_back_initialize},
77 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
78 {"shell", shell_back_initialize},
80 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
81 {"tcl", tcl_back_initialize},
83 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
84 {"sql", sql_back_initialize},
86 /* for any private backend */
87 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
88 {"private", private_back_initialize},
94 BackendInfo *backendInfo = NULL;
97 BackendDB *backendDB = NULL;
99 int backend_init(void)
103 if((nBackendInfo != 0) || (backendInfo != NULL)) {
104 /* already initialized */
106 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
107 "backend_init: backend already initialized\n" ));
109 Debug( LDAP_DEBUG_ANY,
110 "backend_init: already initialized.\n", 0, 0, 0 );
116 binfo[nBackendInfo].bi_type != NULL;
119 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
123 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
124 "backend_init: initialized for type \"%s\"\n",
125 binfo[nBackendInfo].bi_type ));
127 Debug( LDAP_DEBUG_ANY,
128 "backend_init: initialized for type \"%s\"\n",
129 binfo[nBackendInfo].bi_type, 0, 0 );
131 /* destroy those we've already inited */
136 if ( binfo[nBackendInfo].bi_destroy ) {
137 binfo[nBackendInfo].bi_destroy(
138 &binfo[nBackendInfo] );
145 if ( nBackendInfo > 0) {
155 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
156 "backend_init: failed\n" ));
158 Debug( LDAP_DEBUG_ANY,
159 "backend_init: failed\n",
164 #endif /* SLAPD_MODULES */
167 int backend_add(BackendInfo *aBackendInfo)
171 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
173 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
174 "backend_add: initialization for type \"%s\" failed\n",
175 aBackendInfo->bi_type ));
177 Debug( LDAP_DEBUG_ANY,
178 "backend_add: initialization for type \"%s\" failed\n",
179 aBackendInfo->bi_type, 0, 0 );
184 /* now add the backend type to the Backend Info List */
186 BackendInfo *newBackendInfo = 0;
188 /* if backendInfo == binfo no deallocation of old backendInfo */
189 if (backendInfo == binfo) {
190 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
191 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
194 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
197 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
198 sizeof(BackendInfo));
199 backendInfo = newBackendInfo;
206 int backend_startup(Backend *be)
211 if( ! ( nBackendDB > 0 ) ) {
214 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
215 "backend_startup: %d databases to startup. \n",
218 Debug( LDAP_DEBUG_ANY,
219 "backend_startup: %d databases to startup.\n",
226 /* startup a specific backend database */
228 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
229 "backend_startup: starting \"%s\"\n",
232 Debug( LDAP_DEBUG_TRACE,
233 "backend_startup: starting \"%s\"\n",
234 be->be_suffix[0], 0, 0 );
237 if ( be->bd_info->bi_open ) {
238 rc = be->bd_info->bi_open( be->bd_info );
243 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
244 "backend_startup: bi_open failed!\n" ));
246 Debug( LDAP_DEBUG_ANY,
247 "backend_startup: bi_open failed!\n",
254 if ( be->bd_info->bi_db_open ) {
255 rc = be->bd_info->bi_db_open( be );
260 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
261 "backend_startup: bi_db_open failed!\n" ));
263 Debug( LDAP_DEBUG_ANY,
264 "backend_startup: bi_db_open failed!\n",
273 /* open each backend type */
274 for( i = 0; i < nBackendInfo; i++ ) {
275 if( backendInfo[i].bi_nDB == 0) {
276 /* no database of this type, don't open */
280 if( backendInfo[i].bi_open ) {
281 rc = backendInfo[i].bi_open(
287 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
288 "backend_startup: bi_open %d failed!\n", i ));
290 Debug( LDAP_DEBUG_ANY,
291 "backend_startup: bi_open %d failed!\n",
298 /* open each backend database */
299 for( i = 0; i < nBackendDB; i++ ) {
300 /* append global access controls */
301 acl_append( &backendDB[i].be_acl, global_acl );
303 if ( backendDB[i].bd_info->bi_db_open ) {
304 rc = backendDB[i].bd_info->bi_db_open(
310 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
311 "backend_startup: bi_db_open %d failed!\n", i ));
313 Debug( LDAP_DEBUG_ANY,
314 "backend_startup: bi_db_open %d failed!\n",
324 int backend_num( Backend *be )
328 if( be == NULL ) return -1;
330 for( i = 0; i < nBackendDB; i++ ) {
331 if( be == &backendDB[i] ) return i;
337 int backend_sync( Backend *be )
343 /* sync a specific backend database */
345 if ( be->bd_info->bi_nDB == 0 ) {
346 /* no database of this type, we never opened it */
350 if ( be->bd_info->bi_db_sync ) {
351 be->bd_info->bi_db_sync( be );
357 /* sync each backend database */
358 for( i = 0; i < nBackendDB; i++ ) {
359 if ( backendDB[i].bd_info->bi_db_sync ) {
360 rc = backendDB[i].bd_info->bi_db_sync(
365 Debug( LDAP_DEBUG_ANY,
366 "backend_sync: bi_sync %s failed!\n",
367 backendDB[i].be_type, 0, 0 );
375 int backend_shutdown( Backend *be )
381 /* shutdown a specific backend database */
383 if ( be->bd_info->bi_nDB == 0 ) {
384 /* no database of this type, we never opened it */
388 if ( be->bd_info->bi_db_close ) {
389 be->bd_info->bi_db_close( be );
392 if( be->bd_info->bi_close ) {
393 be->bd_info->bi_close( be->bd_info );
399 /* close each backend database */
400 for( i = 0; i < nBackendDB; i++ ) {
401 if ( backendDB[i].bd_info->bi_db_close ) {
402 backendDB[i].bd_info->bi_db_close(
408 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
409 "backend_shutdown: bi_close %s failed!\n",
410 backendDB[i].be_type ));
412 Debug( LDAP_DEBUG_ANY,
413 "backend_close: bi_close %s failed!\n",
414 backendDB[i].be_type, 0, 0 );
419 /* close each backend type */
420 for( i = 0; i < nBackendInfo; i++ ) {
421 if( backendInfo[i].bi_nDB == 0 ) {
422 /* no database of this type */
426 if( backendInfo[i].bi_close ) {
427 backendInfo[i].bi_close(
435 int backend_destroy(void)
439 /* destroy each backend database */
440 for( i = 0; i < nBackendDB; i++ ) {
441 if ( backendDB[i].bd_info->bi_db_destroy ) {
442 backendDB[i].bd_info->bi_db_destroy(
447 /* destroy each backend type */
448 for( i = 0; i < nBackendInfo; i++ ) {
449 if( backendInfo[i].bi_destroy ) {
450 backendInfo[i].bi_destroy(
456 if (backendInfo != binfo) {
459 #endif /* SLAPD_MODULES */
467 BackendInfo* backend_info(const char *type)
471 /* search for the backend type */
472 for( i = 0; i < nBackendInfo; i++ ) {
473 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
474 return &backendInfo[i];
488 BackendInfo *bi = backend_info(type);
492 fprintf( stderr, "Unrecognized database type (%s)\n", type );
496 backendDB = (BackendDB *) ch_realloc(
498 (nBackendDB + 1) * sizeof(Backend) );
500 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
502 be = &backends[nbackends++];
505 be->be_sizelimit = defsize;
506 be->be_timelimit = deftime;
507 be->be_dfltaccess = global_default_access;
509 be->be_restrictops = global_restrictops;
510 be->be_requires = global_requires;
512 /* assign a default depth limit for alias deref */
513 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
516 rc = bi->bi_db_init( be );
520 fprintf( stderr, "database init failed (%s)\n", type );
534 for ( i = 0; i < nbackends; i++ ) {
535 if ( backends[i].bd_info->bi_db_close ) {
536 (*backends[i].bd_info->bi_db_close)( &backends[i] );
546 int i, j, len, dnlen;
549 dnlen = strlen( dn );
550 for ( i = 0; i < nbackends; i++ ) {
551 for ( j = 0; backends[i].be_nsuffix != NULL &&
552 backends[i].be_nsuffix[j] != NULL; j++ )
554 len = strlen( backends[i].be_nsuffix[j] );
557 /* suffix is longer than DN */
562 if ( len && len < dnlen && !DN_SEPARATOR( dn[(dnlen-len)-1] ) ) {
563 /* make sure we have a separator */
568 if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
572 if( manageDSAit && len == dnlen ) {
594 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
595 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
604 be_isroot( Backend *be, const char *ndn )
608 if ( ndn == NULL || *ndn == '\0' ) {
612 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
616 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
622 be_root_dn( Backend *be )
624 if ( be->be_root_dn == NULL ) {
628 return be->be_root_dn;
632 be_isroot_pw( Backend *be,
635 struct berval *cred )
639 if ( ! be_isroot( be, ndn ) ) {
643 if( be->be_root_pw.bv_len == 0 ) {
647 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
648 ldap_pvt_thread_mutex_lock( &passwd_mutex );
650 lutil_passwd_sasl_conn = conn->c_sasl_context;
654 result = lutil_passwd( &be->be_root_pw, cred, NULL );
656 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
658 lutil_passwd_sasl_conn = NULL;
660 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
674 if ( be->be_release ) {
675 /* free and release entry from backend */
676 return be->be_release( be, conn, op, e, rw );
692 for ( i = 0; i < nbackends; i++ ) {
693 if ( backends[i].be_unbind ) {
694 (*backends[i].be_unbind)( &backends[i], conn, op );
702 backend_connection_init(
708 for ( i = 0; i < nbackends; i++ ) {
709 if ( backends[i].be_connection_init ) {
710 (*backends[i].be_connection_init)( &backends[i], conn);
718 backend_connection_destroy(
724 for ( i = 0; i < nbackends; i++ ) {
725 if ( backends[i].be_connection_destroy ) {
726 (*backends[i].be_connection_destroy)( &backends[i], conn);
734 backend_check_controls(
742 if( ctrls == NULL ) {
746 for( ; *ctrls != NULL ; ctrls++ ) {
747 if( (*ctrls)->ldctl_iscritical &&
748 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
750 *text = "control unavailable in NamingContext";
751 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
759 backend_check_restrictions(
767 slap_mask_t restrictops;
768 slap_mask_t requires;
774 rc = backend_check_controls( be, conn, op, text );
776 if( rc != LDAP_SUCCESS ) {
780 restrictops = be->be_restrictops;
781 requires = be->be_requires;
782 ssf = &be->be_ssf_set;
785 restrictops = global_restrictops;
786 requires = global_requires;
787 ssf = &global_ssf_set;
790 switch( op->o_tag ) {
792 opflag = SLAP_RESTRICT_OP_ADD;
796 opflag = SLAP_RESTRICT_OP_BIND;
798 case LDAP_REQ_COMPARE:
799 opflag = SLAP_RESTRICT_OP_COMPARE;
801 case LDAP_REQ_DELETE:
803 opflag = SLAP_RESTRICT_OP_DELETE;
805 case LDAP_REQ_EXTENDED:
806 opflag = SLAP_RESTRICT_OP_EXTENDED;
808 case LDAP_REQ_MODIFY:
810 opflag = SLAP_RESTRICT_OP_MODIFY;
812 case LDAP_REQ_RENAME:
814 opflag = SLAP_RESTRICT_OP_RENAME;
816 case LDAP_REQ_SEARCH:
817 opflag = SLAP_RESTRICT_OP_SEARCH;
819 case LDAP_REQ_UNBIND:
823 *text = "restrict operations internal error";
827 if ( op->o_tag != LDAP_REQ_EXTENDED
828 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
830 /* these checks don't apply to StartTLS */
832 if( op->o_tag == LDAP_REQ_EXTENDED ) {
833 /* threat other extended operations as update ops */
837 if( op->o_transport_ssf < ssf->sss_transport ) {
838 *text = "transport confidentiality required";
839 return LDAP_CONFIDENTIALITY_REQUIRED;
842 if( op->o_tls_ssf < ssf->sss_tls ) {
843 *text = "TLS confidentiality required";
844 return LDAP_CONFIDENTIALITY_REQUIRED;
847 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
848 /* these checks don't apply to SASL bind */
850 if( op->o_sasl_ssf < ssf->sss_sasl ) {
851 *text = "SASL confidentiality required";
852 return LDAP_CONFIDENTIALITY_REQUIRED;
855 if( op->o_ssf < ssf->sss_ssf ) {
856 *text = "confidentiality required";
857 return LDAP_CONFIDENTIALITY_REQUIRED;
862 if( op->o_transport_ssf < ssf->sss_update_transport ) {
863 *text = "transport update confidentiality required";
864 return LDAP_CONFIDENTIALITY_REQUIRED;
867 if( op->o_tls_ssf < ssf->sss_update_tls ) {
868 *text = "TLS update confidentiality required";
869 return LDAP_CONFIDENTIALITY_REQUIRED;
872 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
873 *text = "SASL update confidentiality required";
874 return LDAP_CONFIDENTIALITY_REQUIRED;
877 if( op->o_ssf < ssf->sss_update_ssf ) {
878 *text = "update confidentiality required";
879 return LDAP_CONFIDENTIALITY_REQUIRED;
882 if( op->o_ndn == NULL ) {
883 *text = "modifications require authentication";
884 return LDAP_OPERATIONS_ERROR;
889 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
890 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
892 /* these checks don't apply to Bind or StartTLS */
894 if( requires & SLAP_REQUIRE_STRONG ) {
895 /* should check mechanism */
896 if( op->o_authmech == NULL ||
897 op->o_dn == NULL || *op->o_dn == '\0' )
899 *text = "strong authentication required";
900 return LDAP_STRONG_AUTH_REQUIRED;
904 if( requires & SLAP_REQUIRE_SASL ) {
905 if( op->o_authmech == NULL ||
906 op->o_dn == NULL || *op->o_dn == '\0' )
908 *text = "SASL authentication required";
909 return LDAP_STRONG_AUTH_REQUIRED;
913 if( requires & SLAP_REQUIRE_AUTHC ) {
914 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
915 *text = "authentication required";
916 return LDAP_UNWILLING_TO_PERFORM;
920 if( requires & SLAP_REQUIRE_BIND ) {
922 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
923 version = conn->c_protocol;
924 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
927 /* no bind has occurred */
928 *text = "BIND required";
929 return LDAP_OPERATIONS_ERROR;
933 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
934 if( op->o_protocol < LDAP_VERSION3 ) {
935 /* no bind has occurred */
936 *text = "operation restricted to LDAPv3 clients";
937 return LDAP_OPERATIONS_ERROR;
942 if( restrictops & opflag ) {
943 if( restrictops == SLAP_RESTRICT_OP_READS ) {
944 *text = "read operations restricted";
946 *text = "operation restricted";
948 return LDAP_UNWILLING_TO_PERFORM;
954 int backend_check_referrals(
961 int rc = LDAP_SUCCESS;
963 if( be->be_chk_referrals ) {
966 rc = be->be_chk_referrals( be,
967 conn, op, dn, ndn, &text );
969 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
970 send_ldap_result( conn, op, rc,
971 NULL, text, NULL, NULL );
986 ObjectClass *group_oc,
987 AttributeDescription *group_at
990 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
991 /* we won't attempt to send it to a different backend */
993 be = select_backend(gr_ndn, 0);
996 return LDAP_NO_SUCH_OBJECT;
1000 if( be->be_group ) {
1001 return be->be_group( be, conn, op,
1002 target, gr_ndn, op_ndn,
1003 group_oc, group_at );
1006 return LDAP_UNWILLING_TO_PERFORM;
1016 AttributeDescription *entry_at,
1017 struct berval ***vals
1020 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
1021 /* we won't attempt to send it to a different backend */
1023 be = select_backend(e_ndn, 0);
1026 return LDAP_NO_SUCH_OBJECT;
1030 if( be->be_attribute ) {
1031 return be->be_attribute( be, conn, op, target, e_ndn,
1035 return LDAP_UNWILLING_TO_PERFORM;
1038 Attribute *backend_operational(
1044 Attribute *a = NULL;
1046 #ifdef SLAPD_SCHEMA_DN
1047 a = ch_malloc( sizeof( Attribute ) );
1048 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1050 /* Should be backend specific */
1051 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1052 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1053 a->a_vals[1] = NULL;