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-passwd/external.h"
37 #include "back-perl/external.h"
40 #include "back-shell/external.h"
43 #include "back-tcl/external.h"
46 #include "back-sql/external.h"
49 #include "private/external.h"
52 static BackendInfo binfo[] = {
53 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
54 {"bdb", bdb_initialize},
56 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
57 {"dnssrv", dnssrv_back_initialize},
59 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
60 {"ldap", ldap_back_initialize},
62 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
63 {"ldbm", ldbm_back_initialize},
65 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
66 {"passwd", passwd_back_initialize},
68 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
69 {"perl", perl_back_initialize},
71 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
72 {"shell", shell_back_initialize},
74 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
75 {"tcl", tcl_back_initialize},
77 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
78 {"sql", sql_back_initialize},
80 /* for any private backend */
81 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
82 {"private", private_back_initialize},
88 BackendInfo *backendInfo = NULL;
91 BackendDB *backendDB = NULL;
93 int backend_init(void)
97 if((nBackendInfo != 0) || (backendInfo != NULL)) {
98 /* already initialized */
99 Debug( LDAP_DEBUG_ANY,
100 "backend_init: already initialized.\n", 0, 0, 0 );
105 binfo[nBackendInfo].bi_type != NULL;
108 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
111 Debug( LDAP_DEBUG_ANY,
112 "backend_init: initialized for type \"%s\"\n",
113 binfo[nBackendInfo].bi_type, 0, 0 );
115 /* destroy those we've already inited */
120 if ( binfo[nBackendInfo].bi_destroy ) {
121 binfo[nBackendInfo].bi_destroy(
122 &binfo[nBackendInfo] );
129 if ( nBackendInfo > 0) {
137 Debug( LDAP_DEBUG_ANY,
138 "backend_init: failed\n",
142 #endif /* SLAPD_MODULES */
145 int backend_add(BackendInfo *aBackendInfo)
149 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
150 Debug( LDAP_DEBUG_ANY,
151 "backend_add: initialization for type \"%s\" failed\n",
152 aBackendInfo->bi_type, 0, 0 );
156 /* now add the backend type to the Backend Info List */
158 BackendInfo *newBackendInfo = 0;
160 /* if backendInfo == binfo no deallocation of old backendInfo */
161 if (backendInfo == binfo) {
162 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
163 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
166 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
169 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
170 sizeof(BackendInfo));
171 backendInfo = newBackendInfo;
178 int backend_startup(Backend *be)
183 if( ! ( nBackendDB > 0 ) ) {
185 Debug( LDAP_DEBUG_ANY,
186 "backend_startup: %d databases to startup.\n",
192 /* startup a specific backend database */
193 Debug( LDAP_DEBUG_TRACE,
194 "backend_startup: starting \"%s\"\n",
195 be->be_suffix[0], 0, 0 );
197 if ( be->bd_info->bi_open ) {
198 rc = be->bd_info->bi_open( be->bd_info );
202 Debug( LDAP_DEBUG_ANY,
203 "backend_startup: bi_open failed!\n",
208 if ( be->bd_info->bi_db_open ) {
209 rc = be->bd_info->bi_db_open( be );
213 Debug( LDAP_DEBUG_ANY,
214 "backend_startup: bi_db_open failed!\n",
222 /* open each backend type */
223 for( i = 0; i < nBackendInfo; i++ ) {
224 if( backendInfo[i].bi_nDB == 0) {
225 /* no database of this type, don't open */
229 if( backendInfo[i].bi_open ) {
230 rc = backendInfo[i].bi_open(
235 Debug( LDAP_DEBUG_ANY,
236 "backend_startup: bi_open %d failed!\n",
242 /* open each backend database */
243 for( i = 0; i < nBackendDB; i++ ) {
244 /* append global access controls */
245 acl_append( &backendDB[i].be_acl, global_acl );
247 if ( backendDB[i].bd_info->bi_db_open ) {
248 rc = backendDB[i].bd_info->bi_db_open(
253 Debug( LDAP_DEBUG_ANY,
254 "backend_startup: bi_db_open %d failed!\n",
263 int backend_num( Backend *be )
267 if( be == NULL ) return -1;
269 for( i = 0; i < nBackendDB; i++ ) {
270 if( be == &backendDB[i] ) return i;
275 int backend_shutdown( Backend *be )
281 /* shutdown a specific backend database */
283 if ( be->bd_info->bi_nDB == 0 ) {
284 /* no database of this type, we never opened it */
288 if ( be->bd_info->bi_db_close ) {
289 be->bd_info->bi_db_close( be );
292 if( be->bd_info->bi_close ) {
293 be->bd_info->bi_close( be->bd_info );
299 /* close each backend database */
300 for( i = 0; i < nBackendDB; i++ ) {
301 if ( backendDB[i].bd_info->bi_db_close ) {
302 backendDB[i].bd_info->bi_db_close(
307 Debug( LDAP_DEBUG_ANY,
308 "backend_close: bi_close %s failed!\n",
309 backendDB[i].be_type, 0, 0 );
313 /* close each backend type */
314 for( i = 0; i < nBackendInfo; i++ ) {
315 if( backendInfo[i].bi_nDB == 0 ) {
316 /* no database of this type */
320 if( backendInfo[i].bi_close ) {
321 backendInfo[i].bi_close(
329 int backend_destroy(void)
333 /* destroy each backend database */
334 for( i = 0; i < nBackendDB; i++ ) {
335 if ( backendDB[i].bd_info->bi_db_destroy ) {
336 backendDB[i].bd_info->bi_db_destroy(
341 /* destroy each backend type */
342 for( i = 0; i < nBackendInfo; i++ ) {
343 if( backendInfo[i].bi_destroy ) {
344 backendInfo[i].bi_destroy(
350 if (backendInfo != binfo) {
353 #endif /* SLAPD_MODULES */
361 BackendInfo* backend_info(const char *type)
365 /* search for the backend type */
366 for( i = 0; i < nBackendInfo; i++ ) {
367 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
368 return &backendInfo[i];
382 BackendInfo *bi = backend_info(type);
386 fprintf( stderr, "Unrecognized database type (%s)\n", type );
390 backendDB = (BackendDB *) ch_realloc(
392 (nBackendDB + 1) * sizeof(Backend) );
394 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
396 be = &backends[nbackends++];
399 be->be_sizelimit = defsize;
400 be->be_timelimit = deftime;
401 be->be_dfltaccess = global_default_access;
403 be->be_restrictops = global_restrictops;
404 be->be_requires = global_requires;
406 /* assign a default depth limit for alias deref */
407 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
410 rc = bi->bi_db_init( be );
414 fprintf( stderr, "database init failed (%s)\n", type );
428 for ( i = 0; i < nbackends; i++ ) {
429 if ( backends[i].bd_info->bi_db_close ) {
430 (*backends[i].bd_info->bi_db_close)( &backends[i] );
436 select_backend( const char * dn )
438 int i, j, len, dnlen;
440 dnlen = strlen( dn );
441 for ( i = 0; i < nbackends; i++ ) {
442 for ( j = 0; backends[i].be_nsuffix != NULL &&
443 backends[i].be_nsuffix[j] != NULL; j++ )
445 len = strlen( backends[i].be_nsuffix[j] );
451 if ( strcmp( backends[i].be_nsuffix[j],
452 dn + (dnlen - len) ) == 0 ) {
453 return( &backends[i] );
469 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
470 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
479 be_isroot( Backend *be, const char *ndn )
483 if ( ndn == NULL || *ndn == '\0' ) {
487 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
491 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
497 be_root_dn( Backend *be )
499 if ( be->be_root_dn == NULL ) {
503 return be->be_root_dn;
507 be_isroot_pw( Backend *be,
510 struct berval *cred )
514 if ( ! be_isroot( be, ndn ) ) {
518 if( be->be_root_pw.bv_len == 0 ) {
522 #if defined( SLAPD_CRYPT ) || defined( SLAPD_PASSWD )
523 ldap_pvt_thread_mutex_lock( &passwd_mutex );
525 lutil_passwd_sasl_conn = conn->c_sasl_context;
529 result = lutil_passwd( &be->be_root_pw, cred, NULL );
531 #if defined( SLAPD_CRYPT ) || defined( SLAPD_PASSWD )
533 lutil_passwd_sasl_conn = NULL;
535 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
542 be_entry_release_rw( Backend *be, Entry *e, int rw )
544 if ( be->be_release ) {
545 /* free and release entry from backend */
546 return be->be_release( be, e, rw );
562 for ( i = 0; i < nbackends; i++ ) {
563 if ( backends[i].be_unbind ) {
564 (*backends[i].be_unbind)( &backends[i], conn, op );
572 backend_connection_init(
578 for ( i = 0; i < nbackends; i++ ) {
579 if ( backends[i].be_connection_init ) {
580 (*backends[i].be_connection_init)( &backends[i], conn);
588 backend_connection_destroy(
594 for ( i = 0; i < nbackends; i++ ) {
595 if ( backends[i].be_connection_destroy ) {
596 (*backends[i].be_connection_destroy)( &backends[i], conn);
604 backend_check_controls(
612 if( ctrls == NULL ) {
616 for( ; *ctrls != NULL ; ctrls++ ) {
617 if( (*ctrls)->ldctl_iscritical &&
618 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
620 *text = "control unavailable in NamingContext";
621 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
629 backend_check_restrictions(
637 slap_mask_t restrictops;
638 slap_mask_t requires;
644 rc = backend_check_controls( be, conn, op, text );
646 if( rc != LDAP_SUCCESS ) {
650 restrictops = be->be_restrictops;
651 requires = be->be_requires;
652 ssf = &be->be_ssf_set;
655 restrictops = global_restrictops;
656 requires = global_requires;
657 ssf = &global_ssf_set;
660 switch( op->o_tag ) {
662 opflag = SLAP_RESTRICT_OP_ADD;
666 opflag = SLAP_RESTRICT_OP_BIND;
668 case LDAP_REQ_COMPARE:
669 opflag = SLAP_RESTRICT_OP_COMPARE;
671 case LDAP_REQ_DELETE:
673 opflag = SLAP_RESTRICT_OP_DELETE;
675 case LDAP_REQ_EXTENDED:
676 opflag = SLAP_RESTRICT_OP_EXTENDED;
678 case LDAP_REQ_MODIFY:
680 opflag = SLAP_RESTRICT_OP_MODIFY;
682 case LDAP_REQ_RENAME:
684 opflag = SLAP_RESTRICT_OP_RENAME;
686 case LDAP_REQ_SEARCH:
687 opflag = SLAP_RESTRICT_OP_SEARCH;
689 case LDAP_REQ_UNBIND:
693 *text = "restrict operations internal error";
697 if (( extoid == NULL || strcmp( extoid, LDAP_EXOP_START_TLS ) ) ) {
698 /* these checks don't apply to StartTLS */
700 if( op->o_tag == LDAP_REQ_EXTENDED ) {
701 /* threat other extended operations as update ops */
705 if( op->o_ssf < ssf->sss_ssf ) {
706 *text = "confidentiality required";
707 return LDAP_CONFIDENTIALITY_REQUIRED;
709 if( op->o_transport_ssf < ssf->sss_transport ) {
710 *text = "transport confidentiality required";
711 return LDAP_CONFIDENTIALITY_REQUIRED;
713 if( op->o_tls_ssf < ssf->sss_tls ) {
714 *text = "TLS confidentiality required";
715 return LDAP_CONFIDENTIALITY_REQUIRED;
717 if( op->o_sasl_ssf < ssf->sss_sasl ) {
718 *text = "SASL confidentiality required";
719 return LDAP_CONFIDENTIALITY_REQUIRED;
723 if( op->o_ssf < ssf->sss_update_ssf ) {
724 *text = "update confidentiality required";
725 return LDAP_CONFIDENTIALITY_REQUIRED;
727 if( op->o_transport_ssf < ssf->sss_update_transport ) {
728 *text = "transport update confidentiality required";
729 return LDAP_CONFIDENTIALITY_REQUIRED;
731 if( op->o_tls_ssf < ssf->sss_update_tls ) {
732 *text = "TLS update confidentiality required";
733 return LDAP_CONFIDENTIALITY_REQUIRED;
735 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
736 *text = "SASL update confidentiality required";
737 return LDAP_CONFIDENTIALITY_REQUIRED;
742 if (( extoid == NULL || strcmp( extoid, LDAP_EXOP_START_TLS ) )
743 || op->o_tag == LDAP_REQ_BIND )
745 /* these checks don't apply to StartTLS or Bind */
747 if( requires & SLAP_REQUIRE_STRONG ) {
748 /* should check mechanism */
749 if( op->o_authmech == NULL ||
750 op->o_dn == NULL || *op->o_dn == '\0' )
752 *text = "strong authentication required";
753 return LDAP_STRONG_AUTH_REQUIRED;
757 if( requires & SLAP_REQUIRE_SASL ) {
758 if( op->o_authmech == NULL ||
759 op->o_dn == NULL || *op->o_dn == '\0' )
761 *text = "SASL authentication required";
762 return LDAP_STRONG_AUTH_REQUIRED;
766 if( requires & SLAP_REQUIRE_AUTHC ) {
767 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
768 *text = "authentication required";
769 return LDAP_UNWILLING_TO_PERFORM;
773 if( requires & SLAP_REQUIRE_BIND ) {
775 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
776 version = conn->c_protocol;
777 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
780 /* no bind has occurred */
781 *text = "BIND required";
782 return LDAP_OPERATIONS_ERROR;
786 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
787 if( op->o_protocol < LDAP_VERSION3 ) {
788 /* no bind has occurred */
789 *text = "operation restricted to LDAPv3 clients";
790 return LDAP_OPERATIONS_ERROR;
795 if( restrictops & opflag ) {
796 if( restrictops == SLAP_RESTRICT_OP_READS ) {
797 *text = "read operations restricted";
799 *text = "operation restricted";
801 return LDAP_UNWILLING_TO_PERFORM;
807 int backend_check_referrals(
814 int rc = LDAP_SUCCESS;
816 if( be->be_chk_referrals ) {
819 rc = be->be_chk_referrals( be,
820 conn, op, dn, ndn, &text );
822 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
823 send_ldap_result( conn, op, rc,
824 NULL, text, NULL, NULL );
837 ObjectClass *group_oc,
838 AttributeDescription *group_at
841 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
842 /* we won't attempt to send it to a different backend */
844 be = select_backend(gr_ndn);
847 return LDAP_NO_SUCH_OBJECT;
852 return be->be_group( be, target, gr_ndn, op_ndn,
853 group_oc, group_at );
856 return LDAP_UNWILLING_TO_PERFORM;
866 AttributeDescription *entry_at,
867 struct berval ***vals
870 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
871 /* we won't attempt to send it to a different backend */
873 be = select_backend(e_ndn);
876 return LDAP_NO_SUCH_OBJECT;
880 if( be->be_attribute ) {
881 return be->be_attribute( be, conn, op, target, e_ndn,
885 return LDAP_UNWILLING_TO_PERFORM;
888 Attribute *backend_operational(
894 #ifdef SLAPD_SCHEMA_DN
895 a = ch_malloc( sizeof( Attribute ) );
896 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
898 /* Should be backend specific */
899 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
900 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );