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-passwd/external.h"
40 #include "back-perl/external.h"
43 #include "back-shell/external.h"
46 #include "back-tcl/external.h"
49 #include "back-sql/external.h"
52 #include "private/external.h"
55 static BackendInfo binfo[] = {
56 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
57 {"bdb", bdb_initialize},
59 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
60 {"dnssrv", dnssrv_back_initialize},
62 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
63 {"ldap", ldap_back_initialize},
65 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
66 {"ldbm", ldbm_back_initialize},
68 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
69 {"meta", meta_back_initialize},
71 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
72 {"passwd", passwd_back_initialize},
74 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
75 {"perl", perl_back_initialize},
77 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
78 {"shell", shell_back_initialize},
80 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
81 {"tcl", tcl_back_initialize},
83 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
84 {"sql", sql_back_initialize},
86 /* for any private backend */
87 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
88 {"private", private_back_initialize},
94 BackendInfo *backendInfo = NULL;
97 BackendDB *backendDB = NULL;
99 int backend_init(void)
103 if((nBackendInfo != 0) || (backendInfo != NULL)) {
104 /* already initialized */
106 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
107 "backend_init: backend already initialized\n" ));
109 Debug( LDAP_DEBUG_ANY,
110 "backend_init: already initialized.\n", 0, 0, 0 );
116 binfo[nBackendInfo].bi_type != NULL;
119 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
123 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
124 "backend_init: initialized for type \"%s\"\n",
125 binfo[nBackendInfo].bi_type ));
127 Debug( LDAP_DEBUG_ANY,
128 "backend_init: initialized for type \"%s\"\n",
129 binfo[nBackendInfo].bi_type, 0, 0 );
131 /* destroy those we've already inited */
136 if ( binfo[nBackendInfo].bi_destroy ) {
137 binfo[nBackendInfo].bi_destroy(
138 &binfo[nBackendInfo] );
145 if ( nBackendInfo > 0) {
155 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
156 "backend_init: failed\n" ));
158 Debug( LDAP_DEBUG_ANY,
159 "backend_init: failed\n",
164 #endif /* SLAPD_MODULES */
167 int backend_add(BackendInfo *aBackendInfo)
171 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
173 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
174 "backend_add: initialization for type \"%s\" failed\n",
175 aBackendInfo->bi_type ));
177 Debug( LDAP_DEBUG_ANY,
178 "backend_add: initialization for type \"%s\" failed\n",
179 aBackendInfo->bi_type, 0, 0 );
184 /* now add the backend type to the Backend Info List */
186 BackendInfo *newBackendInfo = 0;
188 /* if backendInfo == binfo no deallocation of old backendInfo */
189 if (backendInfo == binfo) {
190 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
191 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
194 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
197 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
198 sizeof(BackendInfo));
199 backendInfo = newBackendInfo;
206 int backend_startup(Backend *be)
211 if( ! ( nBackendDB > 0 ) ) {
214 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
215 "backend_startup: %d databases to startup. \n",
218 Debug( LDAP_DEBUG_ANY,
219 "backend_startup: %d databases to startup.\n",
226 /* startup a specific backend database */
228 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
229 "backend_startup: starting \"%s\"\n",
232 Debug( LDAP_DEBUG_TRACE,
233 "backend_startup: starting \"%s\"\n",
234 be->be_suffix[0], 0, 0 );
237 if ( be->bd_info->bi_open ) {
238 rc = be->bd_info->bi_open( be->bd_info );
243 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
244 "backend_startup: bi_open failed!\n" ));
246 Debug( LDAP_DEBUG_ANY,
247 "backend_startup: bi_open failed!\n",
254 if ( be->bd_info->bi_db_open ) {
255 rc = be->bd_info->bi_db_open( be );
260 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
261 "backend_startup: bi_db_open failed!\n" ));
263 Debug( LDAP_DEBUG_ANY,
264 "backend_startup: bi_db_open failed!\n",
273 /* open each backend type */
274 for( i = 0; i < nBackendInfo; i++ ) {
275 if( backendInfo[i].bi_nDB == 0) {
276 /* no database of this type, don't open */
280 if( backendInfo[i].bi_open ) {
281 rc = backendInfo[i].bi_open(
287 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
288 "backend_startup: bi_open %d failed!\n", i ));
290 Debug( LDAP_DEBUG_ANY,
291 "backend_startup: bi_open %d failed!\n",
298 /* open each backend database */
299 for( i = 0; i < nBackendDB; i++ ) {
300 /* append global access controls */
301 acl_append( &backendDB[i].be_acl, global_acl );
303 if ( backendDB[i].bd_info->bi_db_open ) {
304 rc = backendDB[i].bd_info->bi_db_open(
310 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
311 "backend_startup: bi_db_open %d failed!\n", i ));
313 Debug( LDAP_DEBUG_ANY,
314 "backend_startup: bi_db_open %d failed!\n",
324 int backend_num( Backend *be )
328 if( be == NULL ) return -1;
330 for( i = 0; i < nBackendDB; i++ ) {
331 if( be == &backendDB[i] ) return i;
336 int backend_shutdown( Backend *be )
342 /* shutdown a specific backend database */
344 if ( be->bd_info->bi_nDB == 0 ) {
345 /* no database of this type, we never opened it */
349 if ( be->bd_info->bi_db_close ) {
350 be->bd_info->bi_db_close( be );
353 if( be->bd_info->bi_close ) {
354 be->bd_info->bi_close( be->bd_info );
360 /* close each backend database */
361 for( i = 0; i < nBackendDB; i++ ) {
362 if ( backendDB[i].bd_info->bi_db_close ) {
363 backendDB[i].bd_info->bi_db_close(
369 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
370 "backend_shutdown: bi_close %s failed!\n",
371 backendDB[i].be_type ));
373 Debug( LDAP_DEBUG_ANY,
374 "backend_close: bi_close %s failed!\n",
375 backendDB[i].be_type, 0, 0 );
380 /* close each backend type */
381 for( i = 0; i < nBackendInfo; i++ ) {
382 if( backendInfo[i].bi_nDB == 0 ) {
383 /* no database of this type */
387 if( backendInfo[i].bi_close ) {
388 backendInfo[i].bi_close(
396 int backend_destroy(void)
400 /* destroy each backend database */
401 for( i = 0; i < nBackendDB; i++ ) {
402 if ( backendDB[i].bd_info->bi_db_destroy ) {
403 backendDB[i].bd_info->bi_db_destroy(
408 /* destroy each backend type */
409 for( i = 0; i < nBackendInfo; i++ ) {
410 if( backendInfo[i].bi_destroy ) {
411 backendInfo[i].bi_destroy(
417 if (backendInfo != binfo) {
420 #endif /* SLAPD_MODULES */
428 BackendInfo* backend_info(const char *type)
432 /* search for the backend type */
433 for( i = 0; i < nBackendInfo; i++ ) {
434 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
435 return &backendInfo[i];
449 BackendInfo *bi = backend_info(type);
453 fprintf( stderr, "Unrecognized database type (%s)\n", type );
457 backendDB = (BackendDB *) ch_realloc(
459 (nBackendDB + 1) * sizeof(Backend) );
461 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
463 be = &backends[nbackends++];
466 be->be_sizelimit = defsize;
467 be->be_timelimit = deftime;
468 be->be_dfltaccess = global_default_access;
470 be->be_restrictops = global_restrictops;
471 be->be_requires = global_requires;
473 /* assign a default depth limit for alias deref */
474 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
477 rc = bi->bi_db_init( be );
481 fprintf( stderr, "database init failed (%s)\n", type );
495 for ( i = 0; i < nbackends; i++ ) {
496 if ( backends[i].bd_info->bi_db_close ) {
497 (*backends[i].bd_info->bi_db_close)( &backends[i] );
507 int i, j, len, dnlen;
510 dnlen = strlen( dn );
511 for ( i = 0; i < nbackends; i++ ) {
512 for ( j = 0; backends[i].be_nsuffix != NULL &&
513 backends[i].be_nsuffix[j] != NULL; j++ )
515 len = strlen( backends[i].be_nsuffix[j] );
518 /* suffix is longer than DN */
523 if ( (len < dnlen) && !(DN_SEPARATOR( dn[(dnlen-len)-1] )) ) {
524 /* make sure we have a separator */
529 if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
533 if( manageDSAit && len == dnlen ) {
555 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
556 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
565 be_isroot( Backend *be, const char *ndn )
569 if ( ndn == NULL || *ndn == '\0' ) {
573 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
577 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
583 be_root_dn( Backend *be )
585 if ( be->be_root_dn == NULL ) {
589 return be->be_root_dn;
593 be_isroot_pw( Backend *be,
596 struct berval *cred )
600 if ( ! be_isroot( be, ndn ) ) {
604 if( be->be_root_pw.bv_len == 0 ) {
608 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
609 ldap_pvt_thread_mutex_lock( &passwd_mutex );
611 lutil_passwd_sasl_conn = conn->c_sasl_context;
615 result = lutil_passwd( &be->be_root_pw, cred, NULL );
617 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
619 lutil_passwd_sasl_conn = NULL;
621 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
635 if ( be->be_release ) {
636 /* free and release entry from backend */
637 return be->be_release( be, conn, op, e, rw );
653 for ( i = 0; i < nbackends; i++ ) {
654 if ( backends[i].be_unbind ) {
655 (*backends[i].be_unbind)( &backends[i], conn, op );
663 backend_connection_init(
669 for ( i = 0; i < nbackends; i++ ) {
670 if ( backends[i].be_connection_init ) {
671 (*backends[i].be_connection_init)( &backends[i], conn);
679 backend_connection_destroy(
685 for ( i = 0; i < nbackends; i++ ) {
686 if ( backends[i].be_connection_destroy ) {
687 (*backends[i].be_connection_destroy)( &backends[i], conn);
695 backend_check_controls(
703 if( ctrls == NULL ) {
707 for( ; *ctrls != NULL ; ctrls++ ) {
708 if( (*ctrls)->ldctl_iscritical &&
709 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
711 *text = "control unavailable in NamingContext";
712 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
720 backend_check_restrictions(
728 slap_mask_t restrictops;
729 slap_mask_t requires;
735 rc = backend_check_controls( be, conn, op, text );
737 if( rc != LDAP_SUCCESS ) {
741 restrictops = be->be_restrictops;
742 requires = be->be_requires;
743 ssf = &be->be_ssf_set;
746 restrictops = global_restrictops;
747 requires = global_requires;
748 ssf = &global_ssf_set;
751 switch( op->o_tag ) {
753 opflag = SLAP_RESTRICT_OP_ADD;
757 opflag = SLAP_RESTRICT_OP_BIND;
759 case LDAP_REQ_COMPARE:
760 opflag = SLAP_RESTRICT_OP_COMPARE;
762 case LDAP_REQ_DELETE:
764 opflag = SLAP_RESTRICT_OP_DELETE;
766 case LDAP_REQ_EXTENDED:
767 opflag = SLAP_RESTRICT_OP_EXTENDED;
769 case LDAP_REQ_MODIFY:
771 opflag = SLAP_RESTRICT_OP_MODIFY;
773 case LDAP_REQ_RENAME:
775 opflag = SLAP_RESTRICT_OP_RENAME;
777 case LDAP_REQ_SEARCH:
778 opflag = SLAP_RESTRICT_OP_SEARCH;
780 case LDAP_REQ_UNBIND:
784 *text = "restrict operations internal error";
788 if ( op->o_tag != LDAP_REQ_EXTENDED
789 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
791 /* these checks don't apply to StartTLS */
793 if( op->o_tag == LDAP_REQ_EXTENDED ) {
794 /* threat other extended operations as update ops */
798 if( op->o_transport_ssf < ssf->sss_transport ) {
799 *text = "transport confidentiality required";
800 return LDAP_CONFIDENTIALITY_REQUIRED;
803 if( op->o_tls_ssf < ssf->sss_tls ) {
804 *text = "TLS confidentiality required";
805 return LDAP_CONFIDENTIALITY_REQUIRED;
808 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
809 /* these checks don't apply to SASL bind */
811 if( op->o_sasl_ssf < ssf->sss_sasl ) {
812 *text = "SASL confidentiality required";
813 return LDAP_CONFIDENTIALITY_REQUIRED;
816 if( op->o_ssf < ssf->sss_ssf ) {
817 *text = "confidentiality required";
818 return LDAP_CONFIDENTIALITY_REQUIRED;
823 if( op->o_transport_ssf < ssf->sss_update_transport ) {
824 *text = "transport update confidentiality required";
825 return LDAP_CONFIDENTIALITY_REQUIRED;
828 if( op->o_tls_ssf < ssf->sss_update_tls ) {
829 *text = "TLS update confidentiality required";
830 return LDAP_CONFIDENTIALITY_REQUIRED;
833 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
834 *text = "SASL update confidentiality required";
835 return LDAP_CONFIDENTIALITY_REQUIRED;
838 if( op->o_ssf < ssf->sss_update_ssf ) {
839 *text = "update confidentiality required";
840 return LDAP_CONFIDENTIALITY_REQUIRED;
843 if( op->o_ndn == NULL ) {
844 *text = "modifications require authentication";
845 return LDAP_OPERATIONS_ERROR;
850 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
851 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
853 /* these checks don't apply to Bind or StartTLS */
855 if( requires & SLAP_REQUIRE_STRONG ) {
856 /* should check mechanism */
857 if( op->o_authmech == NULL ||
858 op->o_dn == NULL || *op->o_dn == '\0' )
860 *text = "strong authentication required";
861 return LDAP_STRONG_AUTH_REQUIRED;
865 if( requires & SLAP_REQUIRE_SASL ) {
866 if( op->o_authmech == NULL ||
867 op->o_dn == NULL || *op->o_dn == '\0' )
869 *text = "SASL authentication required";
870 return LDAP_STRONG_AUTH_REQUIRED;
874 if( requires & SLAP_REQUIRE_AUTHC ) {
875 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
876 *text = "authentication required";
877 return LDAP_UNWILLING_TO_PERFORM;
881 if( requires & SLAP_REQUIRE_BIND ) {
883 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
884 version = conn->c_protocol;
885 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
888 /* no bind has occurred */
889 *text = "BIND required";
890 return LDAP_OPERATIONS_ERROR;
894 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
895 if( op->o_protocol < LDAP_VERSION3 ) {
896 /* no bind has occurred */
897 *text = "operation restricted to LDAPv3 clients";
898 return LDAP_OPERATIONS_ERROR;
903 if( restrictops & opflag ) {
904 if( restrictops == SLAP_RESTRICT_OP_READS ) {
905 *text = "read operations restricted";
907 *text = "operation restricted";
909 return LDAP_UNWILLING_TO_PERFORM;
915 int backend_check_referrals(
922 int rc = LDAP_SUCCESS;
924 if( be->be_chk_referrals ) {
927 rc = be->be_chk_referrals( be,
928 conn, op, dn, ndn, &text );
930 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
931 send_ldap_result( conn, op, rc,
932 NULL, text, NULL, NULL );
947 ObjectClass *group_oc,
948 AttributeDescription *group_at
951 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
952 /* we won't attempt to send it to a different backend */
954 be = select_backend(gr_ndn, 0);
957 return LDAP_NO_SUCH_OBJECT;
962 return be->be_group( be, conn, op,
963 target, gr_ndn, op_ndn,
964 group_oc, group_at );
967 return LDAP_UNWILLING_TO_PERFORM;
977 AttributeDescription *entry_at,
978 struct berval ***vals
981 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
982 /* we won't attempt to send it to a different backend */
984 be = select_backend(e_ndn, 0);
987 return LDAP_NO_SUCH_OBJECT;
991 if( be->be_attribute ) {
992 return be->be_attribute( be, conn, op, target, e_ndn,
996 return LDAP_UNWILLING_TO_PERFORM;
999 Attribute *backend_operational(
1005 Attribute *a = NULL;
1007 #ifdef SLAPD_SCHEMA_DN
1008 a = ch_malloc( sizeof( Attribute ) );
1009 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1011 /* Should be backend specific */
1012 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1013 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1014 a->a_vals[1] = NULL;