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 * If a module is configured as dynamic, its header should not
23 * get included into slapd. While this is a general rule and does
24 * not have much of an effect in UNIX, this rule should be adhered
25 * to for Windows, where dynamic object code should not be implicitly
26 * imported into slapd without appropriate __declspec(dllimport) directives.
29 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
30 #include "back-bdb/external.h"
32 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
33 #include "back-dnssrv/external.h"
35 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
36 #include "back-ldap/external.h"
38 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
39 #include "back-ldbm/external.h"
41 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
42 #include "back-meta/external.h"
44 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
45 #include "back-monitor/external.h"
47 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
48 #include "back-passwd/external.h"
50 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
51 #include "back-perl/external.h"
53 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
54 #include "back-shell/external.h"
56 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
57 #include "back-tcl/external.h"
59 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
60 #include "back-sql/external.h"
62 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
63 #include "private/external.h"
66 static BackendInfo binfo[] = {
67 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
68 {"bdb", bdb_initialize},
70 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
71 {"dnssrv", dnssrv_back_initialize},
73 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
74 {"ldap", ldap_back_initialize},
76 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
77 {"ldbm", ldbm_back_initialize},
79 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
80 {"meta", meta_back_initialize},
82 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
83 {"monitor", monitor_back_initialize},
85 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
86 {"passwd", passwd_back_initialize},
88 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
89 {"perl", perl_back_initialize},
91 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
92 {"shell", shell_back_initialize},
94 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
95 {"tcl", tcl_back_initialize},
97 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
98 {"sql", sql_back_initialize},
100 /* for any private backend */
101 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
102 {"private", private_back_initialize},
104 {"glue", glue_back_initialize},
108 int nBackendInfo = 0;
109 BackendInfo *backendInfo = NULL;
112 BackendDB *backendDB = NULL;
114 int backend_init(void)
118 if((nBackendInfo != 0) || (backendInfo != NULL)) {
119 /* already initialized */
121 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
122 "backend_init: backend already initialized\n" ));
124 Debug( LDAP_DEBUG_ANY,
125 "backend_init: already initialized.\n", 0, 0, 0 );
131 binfo[nBackendInfo].bi_type != NULL;
134 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
138 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
139 "backend_init: initialized for type \"%s\"\n",
140 binfo[nBackendInfo].bi_type ));
142 Debug( LDAP_DEBUG_ANY,
143 "backend_init: initialized for type \"%s\"\n",
144 binfo[nBackendInfo].bi_type, 0, 0 );
146 /* destroy those we've already inited */
151 if ( binfo[nBackendInfo].bi_destroy ) {
152 binfo[nBackendInfo].bi_destroy(
153 &binfo[nBackendInfo] );
160 if ( nBackendInfo > 0) {
170 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
171 "backend_init: failed\n" ));
173 Debug( LDAP_DEBUG_ANY,
174 "backend_init: failed\n",
179 #endif /* SLAPD_MODULES */
182 int backend_add(BackendInfo *aBackendInfo)
186 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
188 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
189 "backend_add: initialization for type \"%s\" failed\n",
190 aBackendInfo->bi_type ));
192 Debug( LDAP_DEBUG_ANY,
193 "backend_add: initialization for type \"%s\" failed\n",
194 aBackendInfo->bi_type, 0, 0 );
199 /* now add the backend type to the Backend Info List */
201 BackendInfo *newBackendInfo = 0;
203 /* if backendInfo == binfo no deallocation of old backendInfo */
204 if (backendInfo == binfo) {
205 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
206 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
209 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
212 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
213 sizeof(BackendInfo));
214 backendInfo = newBackendInfo;
221 int backend_startup(Backend *be)
226 if( ! ( nBackendDB > 0 ) ) {
229 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
230 "backend_startup: %d databases to startup. \n",
233 Debug( LDAP_DEBUG_ANY,
234 "backend_startup: %d databases to startup.\n",
241 /* startup a specific backend database */
243 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
244 "backend_startup: starting \"%s\"\n",
247 Debug( LDAP_DEBUG_TRACE,
248 "backend_startup: starting \"%s\"\n",
249 be->be_suffix[0], 0, 0 );
252 if ( be->bd_info->bi_open ) {
253 rc = be->bd_info->bi_open( be->bd_info );
258 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
259 "backend_startup: bi_open failed!\n" ));
261 Debug( LDAP_DEBUG_ANY,
262 "backend_startup: bi_open failed!\n",
269 if ( be->bd_info->bi_db_open ) {
270 rc = be->bd_info->bi_db_open( be );
275 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
276 "backend_startup: bi_db_open failed!\n" ));
278 Debug( LDAP_DEBUG_ANY,
279 "backend_startup: bi_db_open failed!\n",
288 /* open each backend type */
289 for( i = 0; i < nBackendInfo; i++ ) {
290 if( backendInfo[i].bi_nDB == 0) {
291 /* no database of this type, don't open */
295 if( backendInfo[i].bi_open ) {
296 rc = backendInfo[i].bi_open(
302 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
303 "backend_startup: bi_open %d failed!\n", i ));
305 Debug( LDAP_DEBUG_ANY,
306 "backend_startup: bi_open %d failed!\n",
313 /* open each backend database */
314 for( i = 0; i < nBackendDB; i++ ) {
315 /* append global access controls */
316 acl_append( &backendDB[i].be_acl, global_acl );
318 if ( backendDB[i].bd_info->bi_db_open ) {
319 rc = backendDB[i].bd_info->bi_db_open(
325 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
326 "backend_startup: bi_db_open %d failed!\n", i ));
328 Debug( LDAP_DEBUG_ANY,
329 "backend_startup: bi_db_open %d failed!\n",
339 int backend_num( Backend *be )
343 if( be == NULL ) return -1;
345 for( i = 0; i < nBackendDB; i++ ) {
346 if( be == &backendDB[i] ) return i;
351 int backend_shutdown( Backend *be )
357 /* shutdown a specific backend database */
359 if ( be->bd_info->bi_nDB == 0 ) {
360 /* no database of this type, we never opened it */
364 if ( be->bd_info->bi_db_close ) {
365 be->bd_info->bi_db_close( be );
368 if( be->bd_info->bi_close ) {
369 be->bd_info->bi_close( be->bd_info );
375 /* close each backend database */
376 for( i = 0; i < nBackendDB; i++ ) {
377 if ( backendDB[i].bd_info->bi_db_close ) {
378 backendDB[i].bd_info->bi_db_close(
384 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
385 "backend_shutdown: bi_close %s failed!\n",
386 backendDB[i].be_type ));
388 Debug( LDAP_DEBUG_ANY,
389 "backend_close: bi_close %s failed!\n",
390 backendDB[i].be_type, 0, 0 );
395 /* close each backend type */
396 for( i = 0; i < nBackendInfo; i++ ) {
397 if( backendInfo[i].bi_nDB == 0 ) {
398 /* no database of this type */
402 if( backendInfo[i].bi_close ) {
403 backendInfo[i].bi_close(
411 int backend_destroy(void)
415 /* destroy each backend database */
416 for( i = 0; i < nBackendDB; i++ ) {
417 if ( backendDB[i].bd_info->bi_db_destroy ) {
418 backendDB[i].bd_info->bi_db_destroy(
423 /* destroy each backend type */
424 for( i = 0; i < nBackendInfo; i++ ) {
425 if( backendInfo[i].bi_destroy ) {
426 backendInfo[i].bi_destroy(
432 if (backendInfo != binfo) {
435 #endif /* SLAPD_MODULES */
443 BackendInfo* backend_info(const char *type)
447 /* search for the backend type */
448 for( i = 0; i < nBackendInfo; i++ ) {
449 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
450 return &backendInfo[i];
464 BackendInfo *bi = backend_info(type);
468 fprintf( stderr, "Unrecognized database type (%s)\n", type );
472 backendDB = (BackendDB *) ch_realloc(
474 (nBackendDB + 1) * sizeof(Backend) );
476 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
478 be = &backends[nbackends++];
481 be->be_def_limit = deflimit;
482 be->be_dfltaccess = global_default_access;
484 be->be_restrictops = global_restrictops;
485 be->be_requires = global_requires;
487 /* assign a default depth limit for alias deref */
488 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
491 rc = bi->bi_db_init( be );
495 fprintf( stderr, "database init failed (%s)\n", type );
509 for ( i = 0; i < nbackends; i++ ) {
510 if ( backends[i].bd_info->bi_db_close ) {
511 (*backends[i].bd_info->bi_db_close)( &backends[i] );
521 int i, j, len, dnlen;
524 dnlen = strlen( dn );
525 for ( i = 0; i < nbackends; i++ ) {
526 for ( j = 0; backends[i].be_nsuffix != NULL &&
527 backends[i].be_nsuffix[j] != NULL; j++ )
529 len = backends[i].be_nsuffix[j]->bv_len;
532 /* suffix is longer than DN */
537 if ( len && len < dnlen && ( !DN_SEPARATOR( dn[(dnlen-len)-1] ) || DN_ESCAPE( dn[(dnlen-len)-2] ) ) ) {
538 /* make sure we have a separator */
543 if ( strcmp( backends[i].be_nsuffix[j]->bv_val, &dn[dnlen-len] ) == 0 ) {
547 if( manageDSAit && len == dnlen ) {
568 int len = strlen(suffix);
570 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
571 if ( len != be->be_nsuffix[i]->bv_len ) {
574 if ( strcmp( be->be_nsuffix[i]->bv_val, suffix ) == 0 ) {
583 be_isroot( Backend *be, const char *ndn )
587 if ( ndn == NULL || *ndn == '\0' ) {
591 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
595 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
601 be_isupdate( Backend *be, const char *ndn )
605 if ( ndn == NULL || *ndn == '\0' ) {
609 if ( be->be_update_ndn == NULL || *be->be_update_ndn == '\0' ) {
613 rc = strcmp( be->be_update_ndn, ndn ) ? 0 : 1;
619 be_root_dn( Backend *be )
621 if ( be->be_root_dn == NULL ) {
625 return be->be_root_dn;
629 be_isroot_pw( Backend *be,
632 struct berval *cred )
636 if ( ! be_isroot( be, ndn ) ) {
640 if( be->be_root_pw.bv_len == 0 ) {
644 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
645 ldap_pvt_thread_mutex_lock( &passwd_mutex );
647 lutil_passwd_sasl_conn = conn->c_sasl_context;
651 result = lutil_passwd( &be->be_root_pw, cred, NULL );
653 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
655 lutil_passwd_sasl_conn = NULL;
657 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
671 if ( be->be_release ) {
672 /* free and release entry from backend */
673 return be->be_release( be, conn, op, e, rw );
689 for ( i = 0; i < nbackends; i++ ) {
690 if ( backends[i].be_unbind ) {
691 (*backends[i].be_unbind)( &backends[i], conn, op );
699 backend_connection_init(
705 for ( i = 0; i < nbackends; i++ ) {
706 if ( backends[i].be_connection_init ) {
707 (*backends[i].be_connection_init)( &backends[i], conn);
715 backend_connection_destroy(
721 for ( i = 0; i < nbackends; i++ ) {
722 if ( backends[i].be_connection_destroy ) {
723 (*backends[i].be_connection_destroy)( &backends[i], conn);
731 backend_check_controls(
739 if( ctrls == NULL ) {
743 for( ; *ctrls != NULL ; ctrls++ ) {
744 if( (*ctrls)->ldctl_iscritical &&
745 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
747 *text = "control unavailable in NamingContext";
748 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
756 backend_check_restrictions(
764 slap_mask_t restrictops;
765 slap_mask_t requires;
771 rc = backend_check_controls( be, conn, op, text );
773 if( rc != LDAP_SUCCESS ) {
777 restrictops = be->be_restrictops;
778 requires = be->be_requires;
779 ssf = &be->be_ssf_set;
782 restrictops = global_restrictops;
783 requires = global_requires;
784 ssf = &global_ssf_set;
787 switch( op->o_tag ) {
789 opflag = SLAP_RESTRICT_OP_ADD;
793 opflag = SLAP_RESTRICT_OP_BIND;
795 case LDAP_REQ_COMPARE:
796 opflag = SLAP_RESTRICT_OP_COMPARE;
798 case LDAP_REQ_DELETE:
800 opflag = SLAP_RESTRICT_OP_DELETE;
802 case LDAP_REQ_EXTENDED:
803 opflag = SLAP_RESTRICT_OP_EXTENDED;
805 case LDAP_REQ_MODIFY:
807 opflag = SLAP_RESTRICT_OP_MODIFY;
809 case LDAP_REQ_RENAME:
811 opflag = SLAP_RESTRICT_OP_RENAME;
813 case LDAP_REQ_SEARCH:
814 opflag = SLAP_RESTRICT_OP_SEARCH;
816 case LDAP_REQ_UNBIND:
820 *text = "restrict operations internal error";
824 if ( op->o_tag != LDAP_REQ_EXTENDED
825 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
827 /* these checks don't apply to StartTLS */
829 if( op->o_tag == LDAP_REQ_EXTENDED ) {
830 /* threat other extended operations as update ops */
834 if( op->o_transport_ssf < ssf->sss_transport ) {
835 *text = "transport confidentiality required";
836 return LDAP_CONFIDENTIALITY_REQUIRED;
839 if( op->o_tls_ssf < ssf->sss_tls ) {
840 *text = "TLS confidentiality required";
841 return LDAP_CONFIDENTIALITY_REQUIRED;
844 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
845 /* these checks don't apply to SASL bind */
847 if( op->o_sasl_ssf < ssf->sss_sasl ) {
848 *text = "SASL confidentiality required";
849 return LDAP_CONFIDENTIALITY_REQUIRED;
852 if( op->o_ssf < ssf->sss_ssf ) {
853 *text = "confidentiality required";
854 return LDAP_CONFIDENTIALITY_REQUIRED;
859 if( op->o_transport_ssf < ssf->sss_update_transport ) {
860 *text = "transport update confidentiality required";
861 return LDAP_CONFIDENTIALITY_REQUIRED;
864 if( op->o_tls_ssf < ssf->sss_update_tls ) {
865 *text = "TLS update confidentiality required";
866 return LDAP_CONFIDENTIALITY_REQUIRED;
869 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
870 *text = "SASL update confidentiality required";
871 return LDAP_CONFIDENTIALITY_REQUIRED;
874 if( op->o_ssf < ssf->sss_update_ssf ) {
875 *text = "update confidentiality required";
876 return LDAP_CONFIDENTIALITY_REQUIRED;
879 if( op->o_ndn == NULL ) {
880 *text = "modifications require authentication";
881 return LDAP_OPERATIONS_ERROR;
886 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
887 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
889 /* these checks don't apply to Bind or StartTLS */
891 if( requires & SLAP_REQUIRE_STRONG ) {
892 /* should check mechanism */
893 if( op->o_authmech == NULL ||
894 op->o_dn == NULL || *op->o_dn == '\0' )
896 *text = "strong authentication required";
897 return LDAP_STRONG_AUTH_REQUIRED;
901 if( requires & SLAP_REQUIRE_SASL ) {
902 if( op->o_authmech == NULL ||
903 op->o_dn == NULL || *op->o_dn == '\0' )
905 *text = "SASL authentication required";
906 return LDAP_STRONG_AUTH_REQUIRED;
910 if( requires & SLAP_REQUIRE_AUTHC ) {
911 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
912 *text = "authentication required";
913 return LDAP_UNWILLING_TO_PERFORM;
917 if( requires & SLAP_REQUIRE_BIND ) {
919 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
920 version = conn->c_protocol;
921 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
924 /* no bind has occurred */
925 *text = "BIND required";
926 return LDAP_OPERATIONS_ERROR;
930 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
931 if( op->o_protocol < LDAP_VERSION3 ) {
932 /* no bind has occurred */
933 *text = "operation restricted to LDAPv3 clients";
934 return LDAP_OPERATIONS_ERROR;
939 if( restrictops & opflag ) {
940 if( restrictops == SLAP_RESTRICT_OP_READS ) {
941 *text = "read operations restricted";
943 *text = "operation restricted";
945 return LDAP_UNWILLING_TO_PERFORM;
951 int backend_check_referrals(
958 int rc = LDAP_SUCCESS;
960 if( be->be_chk_referrals ) {
963 rc = be->be_chk_referrals( be,
964 conn, op, dn, ndn, &text );
966 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
967 send_ldap_result( conn, op, rc,
968 NULL, text, NULL, NULL );
983 ObjectClass *group_oc,
984 AttributeDescription *group_at
988 int len = strlen(gr_ndn);
991 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
993 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
995 return SLAPD_ABANDON;
997 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
998 /* we won't attempt to send it to a different backend */
1000 be = select_backend(gr_ndn, 0);
1003 return LDAP_NO_SUCH_OBJECT;
1007 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1008 for (g = conn->c_groups; g; g=g->next) {
1009 if (g->be != be || g->oc != group_oc || g->at != group_at ||
1012 if (strcmp( g->ndn, gr_ndn ) == 0)
1015 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1019 if( be->be_group ) {
1020 int res = be->be_group( be, conn, op,
1021 target, gr_ndn, op_ndn,
1022 group_oc, group_at );
1024 if (op->o_tag != LDAP_REQ_BIND) {
1025 g = ch_malloc(sizeof(GroupAssertion) + len);
1031 strcpy(g->ndn, gr_ndn);
1032 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1033 g->next = conn->c_groups;
1035 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1041 return LDAP_UNWILLING_TO_PERFORM;
1051 AttributeDescription *entry_at,
1052 struct berval ***vals
1055 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
1056 /* we won't attempt to send it to a different backend */
1058 be = select_backend(e_ndn, 0);
1061 return LDAP_NO_SUCH_OBJECT;
1065 if( be->be_attribute ) {
1066 return be->be_attribute( be, conn, op, target, e_ndn,
1070 return LDAP_UNWILLING_TO_PERFORM;
1073 Attribute *backend_operational(
1079 Attribute *a = NULL;
1081 #ifdef SLAPD_SCHEMA_DN
1082 a = ch_malloc( sizeof( Attribute ) );
1083 a->a_desc = slap_schema.si_ad_subschemaSubentry;
1085 /* Should be backend specific */
1086 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1087 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1088 a->a_vals[1] = NULL;