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-monitor/external.h"
40 #include "back-passwd/external.h"
43 #include "back-perl/external.h"
46 #include "back-shell/external.h"
49 #include "back-tcl/external.h"
52 #include "back-sql/external.h"
55 #include "private/external.h"
58 static BackendInfo binfo[] = {
59 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
60 {"bdb", bdb_initialize},
62 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
63 {"dnssrv", dnssrv_back_initialize},
65 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
66 {"ldap", ldap_back_initialize},
68 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
69 {"ldbm", ldbm_back_initialize},
71 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
72 {"meta", meta_back_initialize},
74 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
75 {"monitor", monitor_back_initialize},
77 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
78 {"passwd", passwd_back_initialize},
80 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
81 {"perl", perl_back_initialize},
83 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
84 {"shell", shell_back_initialize},
86 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
87 {"tcl", tcl_back_initialize},
89 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
90 {"sql", sql_back_initialize},
92 /* for any private backend */
93 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
94 {"private", private_back_initialize},
100 BackendInfo *backendInfo = NULL;
103 BackendDB *backendDB = NULL;
105 int backend_init(void)
109 if((nBackendInfo != 0) || (backendInfo != NULL)) {
110 /* already initialized */
112 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
113 "backend_init: backend already initialized\n" ));
115 Debug( LDAP_DEBUG_ANY,
116 "backend_init: already initialized.\n", 0, 0, 0 );
122 binfo[nBackendInfo].bi_type != NULL;
125 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
129 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
130 "backend_init: initialized for type \"%s\"\n",
131 binfo[nBackendInfo].bi_type ));
133 Debug( LDAP_DEBUG_ANY,
134 "backend_init: initialized for type \"%s\"\n",
135 binfo[nBackendInfo].bi_type, 0, 0 );
137 /* destroy those we've already inited */
142 if ( binfo[nBackendInfo].bi_destroy ) {
143 binfo[nBackendInfo].bi_destroy(
144 &binfo[nBackendInfo] );
151 if ( nBackendInfo > 0) {
161 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
162 "backend_init: failed\n" ));
164 Debug( LDAP_DEBUG_ANY,
165 "backend_init: failed\n",
170 #endif /* SLAPD_MODULES */
173 int backend_add(BackendInfo *aBackendInfo)
177 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
179 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
180 "backend_add: initialization for type \"%s\" failed\n",
181 aBackendInfo->bi_type ));
183 Debug( LDAP_DEBUG_ANY,
184 "backend_add: initialization for type \"%s\" failed\n",
185 aBackendInfo->bi_type, 0, 0 );
190 /* now add the backend type to the Backend Info List */
192 BackendInfo *newBackendInfo = 0;
194 /* if backendInfo == binfo no deallocation of old backendInfo */
195 if (backendInfo == binfo) {
196 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
197 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
200 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
203 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
204 sizeof(BackendInfo));
205 backendInfo = newBackendInfo;
212 int backend_startup(Backend *be)
217 if( ! ( nBackendDB > 0 ) ) {
220 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
221 "backend_startup: %d databases to startup. \n",
224 Debug( LDAP_DEBUG_ANY,
225 "backend_startup: %d databases to startup.\n",
232 /* startup a specific backend database */
234 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
235 "backend_startup: starting \"%s\"\n",
238 Debug( LDAP_DEBUG_TRACE,
239 "backend_startup: starting \"%s\"\n",
240 be->be_suffix[0], 0, 0 );
243 if ( be->bd_info->bi_open ) {
244 rc = be->bd_info->bi_open( be->bd_info );
249 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
250 "backend_startup: bi_open failed!\n" ));
252 Debug( LDAP_DEBUG_ANY,
253 "backend_startup: bi_open failed!\n",
260 if ( be->bd_info->bi_db_open ) {
261 rc = be->bd_info->bi_db_open( be );
266 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
267 "backend_startup: bi_db_open failed!\n" ));
269 Debug( LDAP_DEBUG_ANY,
270 "backend_startup: bi_db_open failed!\n",
279 /* open each backend type */
280 for( i = 0; i < nBackendInfo; i++ ) {
281 if( backendInfo[i].bi_nDB == 0) {
282 /* no database of this type, don't open */
286 if( backendInfo[i].bi_open ) {
287 rc = backendInfo[i].bi_open(
293 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
294 "backend_startup: bi_open %d failed!\n", i ));
296 Debug( LDAP_DEBUG_ANY,
297 "backend_startup: bi_open %d failed!\n",
304 /* open each backend database */
305 for( i = 0; i < nBackendDB; i++ ) {
306 /* append global access controls */
307 acl_append( &backendDB[i].be_acl, global_acl );
309 if ( backendDB[i].bd_info->bi_db_open ) {
310 rc = backendDB[i].bd_info->bi_db_open(
316 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
317 "backend_startup: bi_db_open %d failed!\n", i ));
319 Debug( LDAP_DEBUG_ANY,
320 "backend_startup: bi_db_open %d failed!\n",
330 int backend_num( Backend *be )
334 if( be == NULL ) return -1;
336 for( i = 0; i < nBackendDB; i++ ) {
337 if( be == &backendDB[i] ) return i;
342 int backend_shutdown( Backend *be )
348 /* shutdown a specific backend database */
350 if ( be->bd_info->bi_nDB == 0 ) {
351 /* no database of this type, we never opened it */
355 if ( be->bd_info->bi_db_close ) {
356 be->bd_info->bi_db_close( be );
359 if( be->bd_info->bi_close ) {
360 be->bd_info->bi_close( be->bd_info );
366 /* close each backend database */
367 for( i = 0; i < nBackendDB; i++ ) {
368 if ( backendDB[i].bd_info->bi_db_close ) {
369 backendDB[i].bd_info->bi_db_close(
375 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
376 "backend_shutdown: bi_close %s failed!\n",
377 backendDB[i].be_type ));
379 Debug( LDAP_DEBUG_ANY,
380 "backend_close: bi_close %s failed!\n",
381 backendDB[i].be_type, 0, 0 );
386 /* close each backend type */
387 for( i = 0; i < nBackendInfo; i++ ) {
388 if( backendInfo[i].bi_nDB == 0 ) {
389 /* no database of this type */
393 if( backendInfo[i].bi_close ) {
394 backendInfo[i].bi_close(
402 int backend_destroy(void)
406 /* destroy each backend database */
407 for( i = 0; i < nBackendDB; i++ ) {
408 if ( backendDB[i].bd_info->bi_db_destroy ) {
409 backendDB[i].bd_info->bi_db_destroy(
414 /* destroy each backend type */
415 for( i = 0; i < nBackendInfo; i++ ) {
416 if( backendInfo[i].bi_destroy ) {
417 backendInfo[i].bi_destroy(
423 if (backendInfo != binfo) {
426 #endif /* SLAPD_MODULES */
434 BackendInfo* backend_info(const char *type)
438 /* search for the backend type */
439 for( i = 0; i < nBackendInfo; i++ ) {
440 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
441 return &backendInfo[i];
455 BackendInfo *bi = backend_info(type);
459 fprintf( stderr, "Unrecognized database type (%s)\n", type );
463 backendDB = (BackendDB *) ch_realloc(
465 (nBackendDB + 1) * sizeof(Backend) );
467 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
469 be = &backends[nbackends++];
472 be->be_sizelimit = defsize;
473 be->be_timelimit = deftime;
474 be->be_dfltaccess = global_default_access;
476 be->be_restrictops = global_restrictops;
477 be->be_requires = global_requires;
479 /* assign a default depth limit for alias deref */
480 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
483 rc = bi->bi_db_init( be );
487 fprintf( stderr, "database init failed (%s)\n", type );
501 for ( i = 0; i < nbackends; i++ ) {
502 if ( backends[i].bd_info->bi_db_close ) {
503 (*backends[i].bd_info->bi_db_close)( &backends[i] );
513 int i, j, len, dnlen;
516 dnlen = strlen( dn );
517 for ( i = 0; i < nbackends; i++ ) {
518 for ( j = 0; backends[i].be_nsuffix != NULL &&
519 backends[i].be_nsuffix[j] != NULL; j++ )
521 len = strlen( backends[i].be_nsuffix[j] );
524 /* suffix is longer than DN */
529 if ( len && len < dnlen && !DN_SEPARATOR( dn[(dnlen-len)-1] ) ) {
530 /* make sure we have a separator */
535 if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
539 if( manageDSAit && len == dnlen ) {
561 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
562 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
571 be_isroot( Backend *be, const char *ndn )
575 if ( ndn == NULL || *ndn == '\0' ) {
579 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
583 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
589 be_root_dn( Backend *be )
591 if ( be->be_root_dn == NULL ) {
595 return be->be_root_dn;
599 be_isroot_pw( Backend *be,
602 struct berval *cred )
606 if ( ! be_isroot( be, ndn ) ) {
610 if( be->be_root_pw.bv_len == 0 ) {
614 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
615 ldap_pvt_thread_mutex_lock( &passwd_mutex );
617 lutil_passwd_sasl_conn = conn->c_sasl_context;
621 result = lutil_passwd( &be->be_root_pw, cred, NULL );
623 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
625 lutil_passwd_sasl_conn = NULL;
627 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
641 if ( be->be_release ) {
642 /* free and release entry from backend */
643 return be->be_release( be, conn, op, e, rw );
659 for ( i = 0; i < nbackends; i++ ) {
660 if ( backends[i].be_unbind ) {
661 (*backends[i].be_unbind)( &backends[i], conn, op );
669 backend_connection_init(
675 for ( i = 0; i < nbackends; i++ ) {
676 if ( backends[i].be_connection_init ) {
677 (*backends[i].be_connection_init)( &backends[i], conn);
685 backend_connection_destroy(
691 for ( i = 0; i < nbackends; i++ ) {
692 if ( backends[i].be_connection_destroy ) {
693 (*backends[i].be_connection_destroy)( &backends[i], conn);
701 backend_check_controls(
709 if( ctrls == NULL ) {
713 for( ; *ctrls != NULL ; ctrls++ ) {
714 if( (*ctrls)->ldctl_iscritical &&
715 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
717 *text = "control unavailable in NamingContext";
718 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
726 backend_check_restrictions(
734 slap_mask_t restrictops;
735 slap_mask_t requires;
741 rc = backend_check_controls( be, conn, op, text );
743 if( rc != LDAP_SUCCESS ) {
747 restrictops = be->be_restrictops;
748 requires = be->be_requires;
749 ssf = &be->be_ssf_set;
752 restrictops = global_restrictops;
753 requires = global_requires;
754 ssf = &global_ssf_set;
757 switch( op->o_tag ) {
759 opflag = SLAP_RESTRICT_OP_ADD;
763 opflag = SLAP_RESTRICT_OP_BIND;
765 case LDAP_REQ_COMPARE:
766 opflag = SLAP_RESTRICT_OP_COMPARE;
768 case LDAP_REQ_DELETE:
770 opflag = SLAP_RESTRICT_OP_DELETE;
772 case LDAP_REQ_EXTENDED:
773 opflag = SLAP_RESTRICT_OP_EXTENDED;
775 case LDAP_REQ_MODIFY:
777 opflag = SLAP_RESTRICT_OP_MODIFY;
779 case LDAP_REQ_RENAME:
781 opflag = SLAP_RESTRICT_OP_RENAME;
783 case LDAP_REQ_SEARCH:
784 opflag = SLAP_RESTRICT_OP_SEARCH;
786 case LDAP_REQ_UNBIND:
790 *text = "restrict operations internal error";
794 if ( op->o_tag != LDAP_REQ_EXTENDED
795 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
797 /* these checks don't apply to StartTLS */
799 if( op->o_tag == LDAP_REQ_EXTENDED ) {
800 /* threat other extended operations as update ops */
804 if( op->o_transport_ssf < ssf->sss_transport ) {
805 *text = "transport confidentiality required";
806 return LDAP_CONFIDENTIALITY_REQUIRED;
809 if( op->o_tls_ssf < ssf->sss_tls ) {
810 *text = "TLS confidentiality required";
811 return LDAP_CONFIDENTIALITY_REQUIRED;
814 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
815 /* these checks don't apply to SASL bind */
817 if( op->o_sasl_ssf < ssf->sss_sasl ) {
818 *text = "SASL confidentiality required";
819 return LDAP_CONFIDENTIALITY_REQUIRED;
822 if( op->o_ssf < ssf->sss_ssf ) {
823 *text = "confidentiality required";
824 return LDAP_CONFIDENTIALITY_REQUIRED;
829 if( op->o_transport_ssf < ssf->sss_update_transport ) {
830 *text = "transport update confidentiality required";
831 return LDAP_CONFIDENTIALITY_REQUIRED;
834 if( op->o_tls_ssf < ssf->sss_update_tls ) {
835 *text = "TLS update confidentiality required";
836 return LDAP_CONFIDENTIALITY_REQUIRED;
839 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
840 *text = "SASL update confidentiality required";
841 return LDAP_CONFIDENTIALITY_REQUIRED;
844 if( op->o_ssf < ssf->sss_update_ssf ) {
845 *text = "update confidentiality required";
846 return LDAP_CONFIDENTIALITY_REQUIRED;
849 if( op->o_ndn == NULL ) {
850 *text = "modifications require authentication";
851 return LDAP_OPERATIONS_ERROR;
856 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
857 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
859 /* these checks don't apply to Bind or StartTLS */
861 if( requires & SLAP_REQUIRE_STRONG ) {
862 /* should check mechanism */
863 if( op->o_authmech == NULL ||
864 op->o_dn == NULL || *op->o_dn == '\0' )
866 *text = "strong authentication required";
867 return LDAP_STRONG_AUTH_REQUIRED;
871 if( requires & SLAP_REQUIRE_SASL ) {
872 if( op->o_authmech == NULL ||
873 op->o_dn == NULL || *op->o_dn == '\0' )
875 *text = "SASL authentication required";
876 return LDAP_STRONG_AUTH_REQUIRED;
880 if( requires & SLAP_REQUIRE_AUTHC ) {
881 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
882 *text = "authentication required";
883 return LDAP_UNWILLING_TO_PERFORM;
887 if( requires & SLAP_REQUIRE_BIND ) {
889 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
890 version = conn->c_protocol;
891 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
894 /* no bind has occurred */
895 *text = "BIND required";
896 return LDAP_OPERATIONS_ERROR;
900 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
901 if( op->o_protocol < LDAP_VERSION3 ) {
902 /* no bind has occurred */
903 *text = "operation restricted to LDAPv3 clients";
904 return LDAP_OPERATIONS_ERROR;
909 if( restrictops & opflag ) {
910 if( restrictops == SLAP_RESTRICT_OP_READS ) {
911 *text = "read operations restricted";
913 *text = "operation restricted";
915 return LDAP_UNWILLING_TO_PERFORM;
921 int backend_check_referrals(
928 int rc = LDAP_SUCCESS;
930 if( be->be_chk_referrals ) {
933 rc = be->be_chk_referrals( be,
934 conn, op, dn, ndn, &text );
936 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
937 send_ldap_result( conn, op, rc,
938 NULL, text, NULL, NULL );
953 ObjectClass *group_oc,
954 AttributeDescription *group_at
957 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
958 /* we won't attempt to send it to a different backend */
960 be = select_backend(gr_ndn, 0);
963 return LDAP_NO_SUCH_OBJECT;
968 return be->be_group( be, conn, op,
969 target, gr_ndn, op_ndn,
970 group_oc, group_at );
973 return LDAP_UNWILLING_TO_PERFORM;
983 AttributeDescription *entry_at,
984 struct berval ***vals
987 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
988 /* we won't attempt to send it to a different backend */
990 be = select_backend(e_ndn, 0);
993 return LDAP_NO_SUCH_OBJECT;
997 if( be->be_attribute ) {
998 return be->be_attribute( be, conn, op, target, e_ndn,
1002 return LDAP_UNWILLING_TO_PERFORM;
1005 Attribute *backend_operational(
1011 Attribute *a = NULL;
1013 #ifdef SLAPD_SCHEMA_DN
1014 a = ch_malloc( sizeof( Attribute ) );
1015 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1017 /* Should be backend specific */
1018 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1019 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1020 a->a_vals[1] = NULL;