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-dnssrv/external.h"
25 #include "back-ldap/external.h"
28 #include "back-ldbm/external.h"
31 #include "back-passwd/external.h"
34 #include "back-perl/external.h"
37 #include "back-shell/external.h"
40 #include "back-tcl/external.h"
43 #include "back-sql/external.h"
46 #include "private/external.h"
49 static BackendInfo binfo[] = {
50 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
51 {"dnssrv", dnssrv_back_initialize},
53 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
54 {"ldap", ldap_back_initialize},
56 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
57 {"ldbm", ldbm_back_initialize},
59 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
60 {"passwd", passwd_back_initialize},
62 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
63 {"perl", perl_back_initialize},
65 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
66 {"shell", shell_back_initialize},
68 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
69 {"tcl", tcl_back_initialize},
71 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
72 {"sql", sql_back_initialize},
74 /* for any private backend */
75 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
76 {"private", private_back_initialize},
82 BackendInfo *backendInfo = NULL;
85 BackendDB *backendDB = NULL;
87 int backend_init(void)
91 if((nBackendInfo != 0) || (backendInfo != NULL)) {
92 /* already initialized */
93 Debug( LDAP_DEBUG_ANY,
94 "backend_init: already initialized.\n", 0, 0, 0 );
99 binfo[nBackendInfo].bi_type != NULL;
102 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
105 Debug( LDAP_DEBUG_ANY,
106 "backend_init: initialized for type \"%s\"\n",
107 binfo[nBackendInfo].bi_type, 0, 0 );
109 /* destroy those we've already inited */
114 if ( binfo[nBackendInfo].bi_destroy ) {
115 binfo[nBackendInfo].bi_destroy(
116 &binfo[nBackendInfo] );
123 if ( nBackendInfo > 0) {
131 Debug( LDAP_DEBUG_ANY,
132 "backend_init: failed\n",
136 #endif /* SLAPD_MODULES */
139 int backend_add(BackendInfo *aBackendInfo)
143 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
144 Debug( LDAP_DEBUG_ANY,
145 "backend_add: initialization for type \"%s\" failed\n",
146 aBackendInfo->bi_type, 0, 0 );
150 /* now add the backend type to the Backend Info List */
152 BackendInfo *newBackendInfo = 0;
154 /* if backendInfo == binfo no deallocation of old backendInfo */
155 if (backendInfo == binfo) {
156 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
157 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
160 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
163 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
164 sizeof(BackendInfo));
165 backendInfo = newBackendInfo;
172 int backend_startup(Backend *be)
177 if( ! ( nBackendDB > 0 ) ) {
179 Debug( LDAP_DEBUG_ANY,
180 "backend_startup: %d databases to startup.\n",
186 /* startup a specific backend database */
187 Debug( LDAP_DEBUG_TRACE,
188 "backend_startup: starting database\n",
191 if ( be->bd_info->bi_open ) {
192 rc = be->bd_info->bi_open( be->bd_info );
196 Debug( LDAP_DEBUG_ANY,
197 "backend_startup: bi_open failed!\n",
202 if ( be->bd_info->bi_db_open ) {
203 rc = be->bd_info->bi_db_open( be );
207 Debug( LDAP_DEBUG_ANY,
208 "backend_startup: bi_db_open failed!\n",
216 /* open each backend type */
217 for( i = 0; i < nBackendInfo; i++ ) {
218 if( backendInfo[i].bi_nDB == 0) {
219 /* no database of this type, don't open */
223 if( backendInfo[i].bi_open ) {
224 rc = backendInfo[i].bi_open(
229 Debug( LDAP_DEBUG_ANY,
230 "backend_startup: bi_open %d failed!\n",
236 /* open each backend database */
237 for( i = 0; i < nBackendDB; i++ ) {
238 /* append global access controls */
239 acl_append( &backendDB[i].be_acl, global_acl );
241 if ( backendDB[i].bd_info->bi_db_open ) {
242 rc = backendDB[i].bd_info->bi_db_open(
247 Debug( LDAP_DEBUG_ANY,
248 "backend_startup: bi_db_open %d failed!\n",
257 int backend_num( Backend *be )
261 if( be == NULL ) return -1;
263 for( i = 0; i < nBackendDB; i++ ) {
264 if( be == &backendDB[i] ) return i;
269 int backend_shutdown( Backend *be )
275 /* shutdown a specific backend database */
277 if ( be->bd_info->bi_nDB == 0 ) {
278 /* no database of this type, we never opened it */
282 if ( be->bd_info->bi_db_close ) {
283 be->bd_info->bi_db_close( be );
286 if( be->bd_info->bi_close ) {
287 be->bd_info->bi_close( be->bd_info );
293 /* close each backend database */
294 for( i = 0; i < nBackendDB; i++ ) {
295 if ( backendDB[i].bd_info->bi_db_close ) {
296 backendDB[i].bd_info->bi_db_close(
301 Debug( LDAP_DEBUG_ANY,
302 "backend_close: bi_close %s failed!\n",
303 backendDB[i].be_type, 0, 0 );
307 /* close each backend type */
308 for( i = 0; i < nBackendInfo; i++ ) {
309 if( backendInfo[i].bi_nDB == 0 ) {
310 /* no database of this type */
314 if( backendInfo[i].bi_close ) {
315 backendInfo[i].bi_close(
323 int backend_destroy(void)
327 /* destroy each backend database */
328 for( i = 0; i < nBackendDB; i++ ) {
329 if ( backendDB[i].bd_info->bi_db_destroy ) {
330 backendDB[i].bd_info->bi_db_destroy(
335 /* destroy each backend type */
336 for( i = 0; i < nBackendInfo; i++ ) {
337 if( backendInfo[i].bi_destroy ) {
338 backendInfo[i].bi_destroy(
344 if (backendInfo != binfo) {
347 #endif /* SLAPD_MODULES */
355 BackendInfo* backend_info(const char *type)
359 /* search for the backend type */
360 for( i = 0; i < nBackendInfo; i++ ) {
361 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
362 return &backendInfo[i];
376 BackendInfo *bi = backend_info(type);
380 fprintf( stderr, "Unrecognized database type (%s)\n", type );
384 backendDB = (BackendDB *) ch_realloc(
386 (nBackendDB + 1) * sizeof(Backend) );
388 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
390 be = &backends[nbackends++];
393 be->be_sizelimit = defsize;
394 be->be_timelimit = deftime;
395 be->be_dfltaccess = global_default_access;
397 be->be_restrictops = global_restrictops;
398 be->be_requires = global_requires;
400 /* assign a default depth limit for alias deref */
401 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
403 be->be_realm = global_realm != NULL
404 ? ch_strdup( global_realm ) : NULL;
407 rc = bi->bi_db_init( be );
411 fprintf( stderr, "database init failed (%s)\n", type );
425 for ( i = 0; i < nbackends; i++ ) {
426 if ( backends[i].bd_info->bi_db_close ) {
427 (*backends[i].bd_info->bi_db_close)( &backends[i] );
433 select_backend( const char * dn )
435 int i, j, len, dnlen;
437 dnlen = strlen( dn );
438 for ( i = 0; i < nbackends; i++ ) {
439 for ( j = 0; backends[i].be_nsuffix != NULL &&
440 backends[i].be_nsuffix[j] != NULL; j++ )
442 len = strlen( backends[i].be_nsuffix[j] );
448 if ( strcmp( backends[i].be_nsuffix[j],
449 dn + (dnlen - len) ) == 0 ) {
450 return( &backends[i] );
466 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
467 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
476 be_isroot( Backend *be, const char *ndn )
480 if ( ndn == NULL || *ndn == '\0' ) {
484 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
488 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
494 be_root_dn( Backend *be )
496 if ( be->be_root_dn == NULL ) {
500 return be->be_root_dn;
504 be_isroot_pw( Backend *be, const char *ndn, struct berval *cred )
508 if ( ! be_isroot( be, ndn ) ) {
512 if( be->be_root_pw.bv_len == 0 ) {
517 ldap_pvt_thread_mutex_lock( &crypt_mutex );
520 result = lutil_passwd( &be->be_root_pw, cred, NULL );
523 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
530 be_entry_release_rw( Backend *be, Entry *e, int rw )
532 if ( be->be_release ) {
533 /* free and release entry from backend */
534 return be->be_release( be, e, rw );
550 for ( i = 0; i < nbackends; i++ ) {
551 if ( backends[i].be_unbind ) {
552 (*backends[i].be_unbind)( &backends[i], conn, op );
560 backend_connection_init(
566 for ( i = 0; i < nbackends; i++ ) {
567 if ( backends[i].be_connection_init ) {
568 (*backends[i].be_connection_init)( &backends[i], conn);
576 backend_connection_destroy(
582 for ( i = 0; i < nbackends; i++ ) {
583 if ( backends[i].be_connection_destroy ) {
584 (*backends[i].be_connection_destroy)( &backends[i], conn);
592 backend_check_controls(
600 if( ctrls == NULL ) {
604 for( ; *ctrls != NULL ; ctrls++ ) {
605 if( (*ctrls)->ldctl_iscritical &&
606 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
608 *text = "control unavailable in NamingContext";
609 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
617 backend_check_restrictions(
625 slap_mask_t restrictops;
626 slap_mask_t requires;
632 rc = backend_check_controls( be, conn, op, text );
634 if( rc != LDAP_SUCCESS ) {
638 restrictops = be->be_restrictops;
639 requires = be->be_requires;
640 ssf = &be->be_ssf_set;
643 restrictops = global_restrictops;
644 requires = global_requires;
645 ssf = &global_ssf_set;
648 switch( op->o_tag ) {
650 opflag = SLAP_RESTRICT_OP_ADD;
654 opflag = SLAP_RESTRICT_OP_BIND;
656 case LDAP_REQ_COMPARE:
657 opflag = SLAP_RESTRICT_OP_COMPARE;
659 case LDAP_REQ_DELETE:
661 opflag = SLAP_RESTRICT_OP_DELETE;
663 case LDAP_REQ_EXTENDED:
664 opflag = SLAP_RESTRICT_OP_EXTENDED;
666 case LDAP_REQ_MODIFY:
668 opflag = SLAP_RESTRICT_OP_MODIFY;
670 case LDAP_REQ_RENAME:
672 opflag = SLAP_RESTRICT_OP_RENAME;
674 case LDAP_REQ_SEARCH:
675 opflag = SLAP_RESTRICT_OP_SEARCH;
677 case LDAP_REQ_UNBIND:
681 *text = "restrict operations internal error";
685 if( ( extoid == NULL || strcmp( extoid, LDAP_EXOP_START_TLS ) )
686 && op->o_tag != LDAP_REQ_BIND )
688 /* these checks don't apply to bind nor StartTLS */
690 if( op->o_tag == LDAP_REQ_EXTENDED ) {
691 /* threat other extended operations as update ops */
695 if( op->o_ssf < ssf->sss_ssf ) {
696 *text = "confidentiality required";
697 return LDAP_CONFIDENTIALITY_REQUIRED;
699 if( op->o_transport_ssf < ssf->sss_transport ) {
700 *text = "transport confidentiality required";
701 return LDAP_CONFIDENTIALITY_REQUIRED;
703 if( op->o_tls_ssf < ssf->sss_tls ) {
704 *text = "TLS confidentiality required";
705 return LDAP_CONFIDENTIALITY_REQUIRED;
707 if( op->o_sasl_ssf < ssf->sss_sasl ) {
708 *text = "SASL confidentiality required";
709 return LDAP_CONFIDENTIALITY_REQUIRED;
713 if( op->o_ssf < ssf->sss_update_ssf ) {
714 *text = "update confidentiality required";
715 return LDAP_CONFIDENTIALITY_REQUIRED;
717 if( op->o_transport_ssf < ssf->sss_update_transport ) {
718 *text = "transport update confidentiality required";
719 return LDAP_CONFIDENTIALITY_REQUIRED;
721 if( op->o_tls_ssf < ssf->sss_update_tls ) {
722 *text = "TLS update confidentiality required";
723 return LDAP_CONFIDENTIALITY_REQUIRED;
725 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
726 *text = "SASL update confidentiality required";
727 return LDAP_CONFIDENTIALITY_REQUIRED;
731 if( requires & SLAP_REQUIRE_STRONG ) {
732 /* should check mechanism */
733 if( op->o_authmech == NULL ||
734 op->o_dn == NULL || *op->o_dn == '\0' )
736 *text = "SASL authentication required";
737 return LDAP_STRONG_AUTH_REQUIRED;
741 if( requires & SLAP_REQUIRE_SASL ) {
742 if( op->o_authmech == NULL ||
743 op->o_dn == NULL || *op->o_dn == '\0' )
745 *text = "SASL authentication required";
746 return LDAP_STRONG_AUTH_REQUIRED;
750 if( requires & SLAP_REQUIRE_AUTHC ) {
751 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
752 *text = "authentication required";
753 return LDAP_UNWILLING_TO_PERFORM;
757 if( requires & SLAP_REQUIRE_BIND ) {
759 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
760 version = conn->c_protocol;
761 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
764 /* no bind has occurred */
765 *text = "BIND required";
766 return LDAP_OPERATIONS_ERROR;
770 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
771 if( op->o_protocol < LDAP_VERSION3 ) {
772 /* no bind has occurred */
773 *text = "operation restricted to LDAPv3 clients";
774 return LDAP_OPERATIONS_ERROR;
779 if( restrictops & opflag ) {
780 if( (restrictops & SLAP_RESTRICT_OP_READS)
781 == SLAP_RESTRICT_OP_READS )
783 *text = "read operations restricted";
785 *text = "operation restricted";
787 return LDAP_UNWILLING_TO_PERFORM;
793 int backend_check_referrals(
800 int rc = LDAP_SUCCESS;
802 if( be->be_chk_referrals ) {
805 rc = be->be_chk_referrals( be,
806 conn, op, dn, ndn, &text );
808 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
809 send_ldap_result( conn, op, rc,
810 NULL, text, NULL, NULL );
823 ObjectClass *group_oc,
824 AttributeDescription *group_at
827 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
828 /* we won't attempt to send it to a different backend */
830 be = select_backend(gr_ndn);
833 return LDAP_NO_SUCH_OBJECT;
838 return be->be_group( be, target, gr_ndn, op_ndn,
839 group_oc, group_at );
842 return LDAP_UNWILLING_TO_PERFORM;
852 AttributeDescription *entry_at,
853 struct berval ***vals
856 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
857 /* we won't attempt to send it to a different backend */
859 be = select_backend(e_ndn);
862 return LDAP_NO_SUCH_OBJECT;
866 if( be->be_attribute ) {
867 return be->be_attribute( be, conn, op, target, e_ndn,
871 return LDAP_UNWILLING_TO_PERFORM;
874 Attribute *backend_operational(
880 #ifdef SLAPD_SCHEMA_DN
881 a = ch_malloc( sizeof( Attribute ) );
882 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
884 /* Should be backend specific */
885 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
886 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );