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},
107 int nBackendInfo = 0;
108 BackendInfo *backendInfo = NULL;
111 BackendDB *backendDB = NULL;
113 int backend_init(void)
117 if((nBackendInfo != 0) || (backendInfo != NULL)) {
118 /* already initialized */
120 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
121 "backend_init: backend already initialized\n" ));
123 Debug( LDAP_DEBUG_ANY,
124 "backend_init: already initialized.\n", 0, 0, 0 );
130 binfo[nBackendInfo].bi_type != NULL;
133 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
137 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
138 "backend_init: initialized for type \"%s\"\n",
139 binfo[nBackendInfo].bi_type ));
141 Debug( LDAP_DEBUG_ANY,
142 "backend_init: initialized for type \"%s\"\n",
143 binfo[nBackendInfo].bi_type, 0, 0 );
145 /* destroy those we've already inited */
150 if ( binfo[nBackendInfo].bi_destroy ) {
151 binfo[nBackendInfo].bi_destroy(
152 &binfo[nBackendInfo] );
159 if ( nBackendInfo > 0) {
169 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
170 "backend_init: failed\n" ));
172 Debug( LDAP_DEBUG_ANY,
173 "backend_init: failed\n",
178 #endif /* SLAPD_MODULES */
181 int backend_add(BackendInfo *aBackendInfo)
185 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
187 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
188 "backend_add: initialization for type \"%s\" failed\n",
189 aBackendInfo->bi_type ));
191 Debug( LDAP_DEBUG_ANY,
192 "backend_add: initialization for type \"%s\" failed\n",
193 aBackendInfo->bi_type, 0, 0 );
198 /* now add the backend type to the Backend Info List */
200 BackendInfo *newBackendInfo = 0;
202 /* if backendInfo == binfo no deallocation of old backendInfo */
203 if (backendInfo == binfo) {
204 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
205 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
208 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
211 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
212 sizeof(BackendInfo));
213 backendInfo = newBackendInfo;
220 int backend_startup(Backend *be)
225 if( ! ( nBackendDB > 0 ) ) {
228 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
229 "backend_startup: %d databases to startup. \n",
232 Debug( LDAP_DEBUG_ANY,
233 "backend_startup: %d databases to startup.\n",
240 /* startup a specific backend database */
242 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
243 "backend_startup: starting \"%s\"\n",
246 Debug( LDAP_DEBUG_TRACE,
247 "backend_startup: starting \"%s\"\n",
248 be->be_suffix[0], 0, 0 );
251 if ( be->bd_info->bi_open ) {
252 rc = be->bd_info->bi_open( be->bd_info );
257 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
258 "backend_startup: bi_open failed!\n" ));
260 Debug( LDAP_DEBUG_ANY,
261 "backend_startup: bi_open failed!\n",
268 if ( be->bd_info->bi_db_open ) {
269 rc = be->bd_info->bi_db_open( be );
274 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
275 "backend_startup: bi_db_open failed!\n" ));
277 Debug( LDAP_DEBUG_ANY,
278 "backend_startup: bi_db_open failed!\n",
287 /* open each backend type */
288 for( i = 0; i < nBackendInfo; i++ ) {
289 if( backendInfo[i].bi_nDB == 0) {
290 /* no database of this type, don't open */
294 if( backendInfo[i].bi_open ) {
295 rc = backendInfo[i].bi_open(
301 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
302 "backend_startup: bi_open %d failed!\n", i ));
304 Debug( LDAP_DEBUG_ANY,
305 "backend_startup: bi_open %d failed!\n",
312 /* open each backend database */
313 for( i = 0; i < nBackendDB; i++ ) {
314 /* append global access controls */
315 acl_append( &backendDB[i].be_acl, global_acl );
317 if ( backendDB[i].bd_info->bi_db_open ) {
318 rc = backendDB[i].bd_info->bi_db_open(
324 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
325 "backend_startup: bi_db_open %d failed!\n", i ));
327 Debug( LDAP_DEBUG_ANY,
328 "backend_startup: bi_db_open %d failed!\n",
338 int backend_num( Backend *be )
342 if( be == NULL ) return -1;
344 for( i = 0; i < nBackendDB; i++ ) {
345 if( be == &backendDB[i] ) return i;
350 int backend_shutdown( Backend *be )
356 /* shutdown a specific backend database */
358 if ( be->bd_info->bi_nDB == 0 ) {
359 /* no database of this type, we never opened it */
363 if ( be->bd_info->bi_db_close ) {
364 be->bd_info->bi_db_close( be );
367 if( be->bd_info->bi_close ) {
368 be->bd_info->bi_close( be->bd_info );
374 /* close each backend database */
375 for( i = 0; i < nBackendDB; i++ ) {
376 if ( backendDB[i].bd_info->bi_db_close ) {
377 backendDB[i].bd_info->bi_db_close(
383 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
384 "backend_shutdown: bi_close %s failed!\n",
385 backendDB[i].be_type ));
387 Debug( LDAP_DEBUG_ANY,
388 "backend_close: bi_close %s failed!\n",
389 backendDB[i].be_type, 0, 0 );
394 /* close each backend type */
395 for( i = 0; i < nBackendInfo; i++ ) {
396 if( backendInfo[i].bi_nDB == 0 ) {
397 /* no database of this type */
401 if( backendInfo[i].bi_close ) {
402 backendInfo[i].bi_close(
410 int backend_destroy(void)
414 /* destroy each backend database */
415 for( i = 0; i < nBackendDB; i++ ) {
416 if ( backendDB[i].bd_info->bi_db_destroy ) {
417 backendDB[i].bd_info->bi_db_destroy(
420 charray_free( backendDB[i].be_suffix );
421 ber_bvecfree( backendDB[i].be_nsuffix );
422 free( backendDB[i].be_root_dn );
423 free( backendDB[i].be_root_ndn );
424 free( backendDB[i].be_root_pw.bv_val );
425 acl_destroy( backendDB[i].be_acl, global_acl );
429 /* destroy each backend type */
430 for( i = 0; i < nBackendInfo; i++ ) {
431 if( backendInfo[i].bi_destroy ) {
432 backendInfo[i].bi_destroy(
438 if (backendInfo != binfo) {
441 #endif /* SLAPD_MODULES */
449 BackendInfo* backend_info(const char *type)
453 /* search for the backend type */
454 for( i = 0; i < nBackendInfo; i++ ) {
455 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
456 return &backendInfo[i];
470 BackendInfo *bi = backend_info(type);
474 fprintf( stderr, "Unrecognized database type (%s)\n", type );
478 backendDB = (BackendDB *) ch_realloc(
480 (nBackendDB + 1) * sizeof(Backend) );
482 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
484 be = &backends[nbackends++];
487 be->be_def_limit = deflimit;
488 be->be_dfltaccess = global_default_access;
490 be->be_restrictops = global_restrictops;
491 be->be_requires = global_requires;
493 /* assign a default depth limit for alias deref */
494 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
497 rc = bi->bi_db_init( be );
501 fprintf( stderr, "database init failed (%s)\n", type );
515 for ( i = 0; i < nbackends; i++ ) {
516 if ( backends[i].bd_info->bi_db_close ) {
517 (*backends[i].bd_info->bi_db_close)( &backends[i] );
528 int i, j, len, dnlen;
531 dnlen = strlen( dn );
532 for ( i = 0; i < nbackends; i++ ) {
533 for ( j = 0; backends[i].be_nsuffix != NULL &&
534 backends[i].be_nsuffix[j] != NULL; j++ )
536 if ((backends[i].be_glueflags&SLAP_GLUE_SUBORDINATE)&&
540 len = backends[i].be_nsuffix[j]->bv_len;
543 /* suffix is longer than DN */
548 if ( len && len < dnlen && ( !DN_SEPARATOR( dn[(dnlen-len)-1] ) || DN_ESCAPE( dn[(dnlen-len)-2] ) ) ) {
549 /* make sure we have a separator */
554 if ( strcmp( backends[i].be_nsuffix[j]->bv_val, &dn[dnlen-len] ) == 0 ) {
558 if( manageDSAit && len == dnlen ) {
579 size_t len = strlen(suffix);
581 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
582 if ( len != be->be_nsuffix[i]->bv_len ) {
585 if ( strcmp( be->be_nsuffix[i]->bv_val, suffix ) == 0 ) {
594 be_isroot( Backend *be, const char *ndn )
598 if ( ndn == NULL || *ndn == '\0' ) {
602 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
606 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
612 be_isupdate( Backend *be, const char *ndn )
616 if ( ndn == NULL || *ndn == '\0' ) {
620 if ( be->be_update_ndn == NULL || *be->be_update_ndn == '\0' ) {
624 rc = strcmp( be->be_update_ndn, ndn ) ? 0 : 1;
630 be_root_dn( Backend *be )
632 if ( be->be_root_dn == NULL ) {
636 return be->be_root_dn;
640 be_isroot_pw( Backend *be,
643 struct berval *cred )
647 if ( ! be_isroot( be, ndn ) ) {
651 if( be->be_root_pw.bv_len == 0 ) {
655 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
656 ldap_pvt_thread_mutex_lock( &passwd_mutex );
658 lutil_passwd_sasl_conn = conn->c_sasl_context;
662 result = lutil_passwd( &be->be_root_pw, cred, NULL );
664 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
666 lutil_passwd_sasl_conn = NULL;
668 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
682 if ( be->be_release ) {
683 /* free and release entry from backend */
684 return be->be_release( be, conn, op, e, rw );
700 for ( i = 0; i < nbackends; i++ ) {
701 if ( backends[i].be_unbind ) {
702 (*backends[i].be_unbind)( &backends[i], conn, op );
710 backend_connection_init(
716 for ( i = 0; i < nbackends; i++ ) {
717 if ( backends[i].be_connection_init ) {
718 (*backends[i].be_connection_init)( &backends[i], conn);
726 backend_connection_destroy(
732 for ( i = 0; i < nbackends; i++ ) {
733 if ( backends[i].be_connection_destroy ) {
734 (*backends[i].be_connection_destroy)( &backends[i], conn);
742 backend_check_controls(
750 if( ctrls == NULL ) {
754 for( ; *ctrls != NULL ; ctrls++ ) {
755 if( (*ctrls)->ldctl_iscritical &&
756 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
758 *text = "control unavailable in NamingContext";
759 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
767 backend_check_restrictions(
775 slap_mask_t restrictops;
776 slap_mask_t requires;
782 rc = backend_check_controls( be, conn, op, text );
784 if( rc != LDAP_SUCCESS ) {
788 restrictops = be->be_restrictops;
789 requires = be->be_requires;
790 ssf = &be->be_ssf_set;
793 restrictops = global_restrictops;
794 requires = global_requires;
795 ssf = &global_ssf_set;
798 switch( op->o_tag ) {
800 opflag = SLAP_RESTRICT_OP_ADD;
804 opflag = SLAP_RESTRICT_OP_BIND;
806 case LDAP_REQ_COMPARE:
807 opflag = SLAP_RESTRICT_OP_COMPARE;
809 case LDAP_REQ_DELETE:
811 opflag = SLAP_RESTRICT_OP_DELETE;
813 case LDAP_REQ_EXTENDED:
814 opflag = SLAP_RESTRICT_OP_EXTENDED;
816 case LDAP_REQ_MODIFY:
818 opflag = SLAP_RESTRICT_OP_MODIFY;
820 case LDAP_REQ_RENAME:
822 opflag = SLAP_RESTRICT_OP_RENAME;
824 case LDAP_REQ_SEARCH:
825 opflag = SLAP_RESTRICT_OP_SEARCH;
827 case LDAP_REQ_UNBIND:
831 *text = "restrict operations internal error";
835 if ( op->o_tag != LDAP_REQ_EXTENDED
836 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
838 /* these checks don't apply to StartTLS */
840 if( op->o_tag == LDAP_REQ_EXTENDED ) {
841 /* threat other extended operations as update ops */
845 if( op->o_transport_ssf < ssf->sss_transport ) {
846 *text = "transport confidentiality required";
847 return LDAP_CONFIDENTIALITY_REQUIRED;
850 if( op->o_tls_ssf < ssf->sss_tls ) {
851 *text = "TLS confidentiality required";
852 return LDAP_CONFIDENTIALITY_REQUIRED;
855 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
856 /* these checks don't apply to SASL bind */
858 if( op->o_sasl_ssf < ssf->sss_sasl ) {
859 *text = "SASL confidentiality required";
860 return LDAP_CONFIDENTIALITY_REQUIRED;
863 if( op->o_ssf < ssf->sss_ssf ) {
864 *text = "confidentiality required";
865 return LDAP_CONFIDENTIALITY_REQUIRED;
870 if( op->o_transport_ssf < ssf->sss_update_transport ) {
871 *text = "transport update confidentiality required";
872 return LDAP_CONFIDENTIALITY_REQUIRED;
875 if( op->o_tls_ssf < ssf->sss_update_tls ) {
876 *text = "TLS update confidentiality required";
877 return LDAP_CONFIDENTIALITY_REQUIRED;
880 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
881 *text = "SASL update confidentiality required";
882 return LDAP_CONFIDENTIALITY_REQUIRED;
885 if( op->o_ssf < ssf->sss_update_ssf ) {
886 *text = "update confidentiality required";
887 return LDAP_CONFIDENTIALITY_REQUIRED;
890 if( op->o_ndn.bv_len == 0 ) {
891 *text = "modifications require authentication";
892 return LDAP_OPERATIONS_ERROR;
897 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
898 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
900 /* these checks don't apply to Bind or StartTLS */
902 if( requires & SLAP_REQUIRE_STRONG ) {
903 /* should check mechanism */
904 if( op->o_authmech == NULL || op->o_dn.bv_len == 0 )
906 *text = "strong authentication required";
907 return LDAP_STRONG_AUTH_REQUIRED;
911 if( requires & SLAP_REQUIRE_SASL ) {
912 if( op->o_authmech == NULL || op->o_dn.bv_len == 0 )
914 *text = "SASL authentication required";
915 return LDAP_STRONG_AUTH_REQUIRED;
919 if( requires & SLAP_REQUIRE_AUTHC ) {
920 if( op->o_dn.bv_len == 0 ) {
921 *text = "authentication required";
922 return LDAP_UNWILLING_TO_PERFORM;
926 if( requires & SLAP_REQUIRE_BIND ) {
928 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
929 version = conn->c_protocol;
930 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
933 /* no bind has occurred */
934 *text = "BIND required";
935 return LDAP_OPERATIONS_ERROR;
939 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
940 if( op->o_protocol < LDAP_VERSION3 ) {
941 /* no bind has occurred */
942 *text = "operation restricted to LDAPv3 clients";
943 return LDAP_OPERATIONS_ERROR;
948 if( restrictops & opflag ) {
949 if( restrictops == SLAP_RESTRICT_OP_READS ) {
950 *text = "read operations restricted";
952 *text = "operation restricted";
954 return LDAP_UNWILLING_TO_PERFORM;
960 int backend_check_referrals(
967 int rc = LDAP_SUCCESS;
969 if( be->be_chk_referrals ) {
972 rc = be->be_chk_referrals( be,
973 conn, op, dn, ndn, &text );
975 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
976 send_ldap_result( conn, op, rc,
977 NULL, text, NULL, NULL );
992 ObjectClass *group_oc,
993 AttributeDescription *group_at
997 int len = strlen(gr_ndn);
1000 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
1002 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
1004 return SLAPD_ABANDON;
1006 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
1007 /* we won't attempt to send it to a different backend */
1009 be = select_backend(gr_ndn, 0,
1010 (be->be_glueflags & SLAP_GLUE_INSTANCE));
1013 return LDAP_NO_SUCH_OBJECT;
1017 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1018 for (g = conn->c_groups; g; g=g->next) {
1019 if (g->be != be || g->oc != group_oc || g->at != group_at ||
1022 if (strcmp( g->ndn, gr_ndn ) == 0)
1025 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1029 if( be->be_group ) {
1030 int res = be->be_group( be, conn, op,
1031 target, gr_ndn, op_ndn,
1032 group_oc, group_at );
1034 if (op->o_tag != LDAP_REQ_BIND) {
1035 g = ch_malloc(sizeof(GroupAssertion) + len);
1041 strcpy(g->ndn, gr_ndn);
1042 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1043 g->next = conn->c_groups;
1045 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1051 return LDAP_UNWILLING_TO_PERFORM;
1060 const char *entry_ndn,
1061 AttributeDescription *entry_at,
1062 struct berval ***vals
1065 if( target == NULL ||
1066 strcmp( target->e_ndn, entry_ndn ) != 0 )
1068 /* we won't attempt to send it to a different backend */
1070 be = select_backend(entry_ndn, 0,
1071 (be->be_glueflags & SLAP_GLUE_INSTANCE));
1074 return LDAP_NO_SUCH_OBJECT;
1078 if( be->be_attribute ) {
1079 return be->be_attribute( be, conn, op, target, entry_ndn,
1083 return LDAP_UNWILLING_TO_PERFORM;
1086 Attribute *backend_operational(
1094 Attribute *a = NULL, **ap = &a;
1096 #ifdef SLAPD_SCHEMA_DN
1097 *ap = slap_operational_subschemaSubentry();
1098 ap = &(*ap)->a_next;
1102 * If operational attributes (allegedly) are required,
1103 * and the backend supports specific operational attributes,
1104 * add them to the attribute list
1106 if ( ( opattrs || attrs ) && be && be->be_operational != NULL ) {
1107 ( void )be->be_operational( be, conn, op, e,
1108 attrs, opattrs, ap );