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 */
100 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
101 "backend_init: backend already initialized\n" ));
103 Debug( LDAP_DEBUG_ANY,
104 "backend_init: already initialized.\n", 0, 0, 0 );
110 binfo[nBackendInfo].bi_type != NULL;
113 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
117 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
118 "backend_init: initialized for type \"%s\"\n",
119 binfo[nBackendInfo].bi_type ));
121 Debug( LDAP_DEBUG_ANY,
122 "backend_init: initialized for type \"%s\"\n",
123 binfo[nBackendInfo].bi_type, 0, 0 );
125 /* destroy those we've already inited */
130 if ( binfo[nBackendInfo].bi_destroy ) {
131 binfo[nBackendInfo].bi_destroy(
132 &binfo[nBackendInfo] );
139 if ( nBackendInfo > 0) {
149 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
150 "backend_init: failed\n" ));
152 Debug( LDAP_DEBUG_ANY,
153 "backend_init: failed\n",
158 #endif /* SLAPD_MODULES */
161 int backend_add(BackendInfo *aBackendInfo)
165 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
167 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
168 "backend_add: initialization for type \"%s\" failed\n",
169 aBackendInfo->bi_type ));
171 Debug( LDAP_DEBUG_ANY,
172 "backend_add: initialization for type \"%s\" failed\n",
173 aBackendInfo->bi_type, 0, 0 );
178 /* now add the backend type to the Backend Info List */
180 BackendInfo *newBackendInfo = 0;
182 /* if backendInfo == binfo no deallocation of old backendInfo */
183 if (backendInfo == binfo) {
184 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
185 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
188 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
191 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
192 sizeof(BackendInfo));
193 backendInfo = newBackendInfo;
200 int backend_startup(Backend *be)
205 if( ! ( nBackendDB > 0 ) ) {
208 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
209 "backend_startup: %d databases to startup. \n",
212 Debug( LDAP_DEBUG_ANY,
213 "backend_startup: %d databases to startup.\n",
220 /* startup a specific backend database */
222 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
223 "backend_startup: starting \"%s\"\n",
226 Debug( LDAP_DEBUG_TRACE,
227 "backend_startup: starting \"%s\"\n",
228 be->be_suffix[0], 0, 0 );
231 if ( be->bd_info->bi_open ) {
232 rc = be->bd_info->bi_open( be->bd_info );
237 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
238 "backend_startup: bi_open failed!\n" ));
240 Debug( LDAP_DEBUG_ANY,
241 "backend_startup: bi_open failed!\n",
248 if ( be->bd_info->bi_db_open ) {
249 rc = be->bd_info->bi_db_open( be );
254 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
255 "backend_startup: bi_db_open failed!\n" ));
257 Debug( LDAP_DEBUG_ANY,
258 "backend_startup: bi_db_open failed!\n",
267 /* open each backend type */
268 for( i = 0; i < nBackendInfo; i++ ) {
269 if( backendInfo[i].bi_nDB == 0) {
270 /* no database of this type, don't open */
274 if( backendInfo[i].bi_open ) {
275 rc = backendInfo[i].bi_open(
281 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
282 "backend_startup: bi_open %d failed!\n", i ));
284 Debug( LDAP_DEBUG_ANY,
285 "backend_startup: bi_open %d failed!\n",
292 /* open each backend database */
293 for( i = 0; i < nBackendDB; i++ ) {
294 /* append global access controls */
295 acl_append( &backendDB[i].be_acl, global_acl );
297 if ( backendDB[i].bd_info->bi_db_open ) {
298 rc = backendDB[i].bd_info->bi_db_open(
304 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
305 "backend_startup: bi_db_open %d failed!\n", i ));
307 Debug( LDAP_DEBUG_ANY,
308 "backend_startup: bi_db_open %d failed!\n",
318 int backend_num( Backend *be )
322 if( be == NULL ) return -1;
324 for( i = 0; i < nBackendDB; i++ ) {
325 if( be == &backendDB[i] ) return i;
330 int backend_shutdown( Backend *be )
336 /* shutdown a specific backend database */
338 if ( be->bd_info->bi_nDB == 0 ) {
339 /* no database of this type, we never opened it */
343 if ( be->bd_info->bi_db_close ) {
344 be->bd_info->bi_db_close( be );
347 if( be->bd_info->bi_close ) {
348 be->bd_info->bi_close( be->bd_info );
354 /* close each backend database */
355 for( i = 0; i < nBackendDB; i++ ) {
356 if ( backendDB[i].bd_info->bi_db_close ) {
357 backendDB[i].bd_info->bi_db_close(
363 LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
364 "backend_shutdown: bi_close %s failed!\n",
365 backendDB[i].be_type ));
367 Debug( LDAP_DEBUG_ANY,
368 "backend_close: bi_close %s failed!\n",
369 backendDB[i].be_type, 0, 0 );
374 /* close each backend type */
375 for( i = 0; i < nBackendInfo; i++ ) {
376 if( backendInfo[i].bi_nDB == 0 ) {
377 /* no database of this type */
381 if( backendInfo[i].bi_close ) {
382 backendInfo[i].bi_close(
390 int backend_destroy(void)
394 /* destroy each backend database */
395 for( i = 0; i < nBackendDB; i++ ) {
396 if ( backendDB[i].bd_info->bi_db_destroy ) {
397 backendDB[i].bd_info->bi_db_destroy(
402 /* destroy each backend type */
403 for( i = 0; i < nBackendInfo; i++ ) {
404 if( backendInfo[i].bi_destroy ) {
405 backendInfo[i].bi_destroy(
411 if (backendInfo != binfo) {
414 #endif /* SLAPD_MODULES */
422 BackendInfo* backend_info(const char *type)
426 /* search for the backend type */
427 for( i = 0; i < nBackendInfo; i++ ) {
428 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
429 return &backendInfo[i];
443 BackendInfo *bi = backend_info(type);
447 fprintf( stderr, "Unrecognized database type (%s)\n", type );
451 backendDB = (BackendDB *) ch_realloc(
453 (nBackendDB + 1) * sizeof(Backend) );
455 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
457 be = &backends[nbackends++];
460 be->be_sizelimit = defsize;
461 be->be_timelimit = deftime;
462 be->be_dfltaccess = global_default_access;
464 be->be_restrictops = global_restrictops;
465 be->be_requires = global_requires;
467 /* assign a default depth limit for alias deref */
468 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
471 rc = bi->bi_db_init( be );
475 fprintf( stderr, "database init failed (%s)\n", type );
489 for ( i = 0; i < nbackends; i++ ) {
490 if ( backends[i].bd_info->bi_db_close ) {
491 (*backends[i].bd_info->bi_db_close)( &backends[i] );
501 int i, j, len, dnlen;
504 dnlen = strlen( dn );
505 for ( i = 0; i < nbackends; i++ ) {
506 for ( j = 0; backends[i].be_nsuffix != NULL &&
507 backends[i].be_nsuffix[j] != NULL; j++ )
509 len = strlen( backends[i].be_nsuffix[j] );
512 /* suffix is longer than DN */
517 if ( (len < dnlen) && !(DN_SEPARATOR( dn[(dnlen-len)-1] )) ) {
518 /* make sure we have a separator */
523 if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
527 if( manageDSAit && len == dnlen ) {
549 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
550 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
559 be_isroot( Backend *be, const char *ndn )
563 if ( ndn == NULL || *ndn == '\0' ) {
567 if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
571 rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
577 be_root_dn( Backend *be )
579 if ( be->be_root_dn == NULL ) {
583 return be->be_root_dn;
587 be_isroot_pw( Backend *be,
590 struct berval *cred )
594 if ( ! be_isroot( be, ndn ) ) {
598 if( be->be_root_pw.bv_len == 0 ) {
602 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
603 ldap_pvt_thread_mutex_lock( &passwd_mutex );
605 lutil_passwd_sasl_conn = conn->c_sasl_context;
609 result = lutil_passwd( &be->be_root_pw, cred, NULL );
611 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
613 lutil_passwd_sasl_conn = NULL;
615 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
629 if ( be->be_release ) {
630 /* free and release entry from backend */
631 return be->be_release( be, conn, op, e, rw );
647 for ( i = 0; i < nbackends; i++ ) {
648 if ( backends[i].be_unbind ) {
649 (*backends[i].be_unbind)( &backends[i], conn, op );
657 backend_connection_init(
663 for ( i = 0; i < nbackends; i++ ) {
664 if ( backends[i].be_connection_init ) {
665 (*backends[i].be_connection_init)( &backends[i], conn);
673 backend_connection_destroy(
679 for ( i = 0; i < nbackends; i++ ) {
680 if ( backends[i].be_connection_destroy ) {
681 (*backends[i].be_connection_destroy)( &backends[i], conn);
689 backend_check_controls(
697 if( ctrls == NULL ) {
701 for( ; *ctrls != NULL ; ctrls++ ) {
702 if( (*ctrls)->ldctl_iscritical &&
703 !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
705 *text = "control unavailable in NamingContext";
706 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
714 backend_check_restrictions(
722 slap_mask_t restrictops;
723 slap_mask_t requires;
729 rc = backend_check_controls( be, conn, op, text );
731 if( rc != LDAP_SUCCESS ) {
735 restrictops = be->be_restrictops;
736 requires = be->be_requires;
737 ssf = &be->be_ssf_set;
740 restrictops = global_restrictops;
741 requires = global_requires;
742 ssf = &global_ssf_set;
745 switch( op->o_tag ) {
747 opflag = SLAP_RESTRICT_OP_ADD;
751 opflag = SLAP_RESTRICT_OP_BIND;
753 case LDAP_REQ_COMPARE:
754 opflag = SLAP_RESTRICT_OP_COMPARE;
756 case LDAP_REQ_DELETE:
758 opflag = SLAP_RESTRICT_OP_DELETE;
760 case LDAP_REQ_EXTENDED:
761 opflag = SLAP_RESTRICT_OP_EXTENDED;
763 case LDAP_REQ_MODIFY:
765 opflag = SLAP_RESTRICT_OP_MODIFY;
767 case LDAP_REQ_RENAME:
769 opflag = SLAP_RESTRICT_OP_RENAME;
771 case LDAP_REQ_SEARCH:
772 opflag = SLAP_RESTRICT_OP_SEARCH;
774 case LDAP_REQ_UNBIND:
778 *text = "restrict operations internal error";
782 if ( op->o_tag != LDAP_REQ_EXTENDED
783 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
785 /* these checks don't apply to StartTLS */
787 if( op->o_tag == LDAP_REQ_EXTENDED ) {
788 /* threat other extended operations as update ops */
792 if( op->o_transport_ssf < ssf->sss_transport ) {
793 *text = "transport confidentiality required";
794 return LDAP_CONFIDENTIALITY_REQUIRED;
797 if( op->o_tls_ssf < ssf->sss_tls ) {
798 *text = "TLS confidentiality required";
799 return LDAP_CONFIDENTIALITY_REQUIRED;
802 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
803 /* these checks don't apply to SASL bind */
805 if( op->o_sasl_ssf < ssf->sss_sasl ) {
806 *text = "SASL confidentiality required";
807 return LDAP_CONFIDENTIALITY_REQUIRED;
810 if( op->o_ssf < ssf->sss_ssf ) {
811 *text = "confidentiality required";
812 return LDAP_CONFIDENTIALITY_REQUIRED;
817 if( op->o_transport_ssf < ssf->sss_update_transport ) {
818 *text = "transport update confidentiality required";
819 return LDAP_CONFIDENTIALITY_REQUIRED;
822 if( op->o_tls_ssf < ssf->sss_update_tls ) {
823 *text = "TLS update confidentiality required";
824 return LDAP_CONFIDENTIALITY_REQUIRED;
827 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
828 *text = "SASL update confidentiality required";
829 return LDAP_CONFIDENTIALITY_REQUIRED;
832 if( op->o_ssf < ssf->sss_update_ssf ) {
833 *text = "update confidentiality required";
834 return LDAP_CONFIDENTIALITY_REQUIRED;
837 if( op->o_ndn == NULL ) {
838 *text = "modifications require authentication";
839 return LDAP_OPERATIONS_ERROR;
844 if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
845 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
847 /* these checks don't apply to Bind or StartTLS */
849 if( requires & SLAP_REQUIRE_STRONG ) {
850 /* should check mechanism */
851 if( op->o_authmech == NULL ||
852 op->o_dn == NULL || *op->o_dn == '\0' )
854 *text = "strong authentication required";
855 return LDAP_STRONG_AUTH_REQUIRED;
859 if( requires & SLAP_REQUIRE_SASL ) {
860 if( op->o_authmech == NULL ||
861 op->o_dn == NULL || *op->o_dn == '\0' )
863 *text = "SASL authentication required";
864 return LDAP_STRONG_AUTH_REQUIRED;
868 if( requires & SLAP_REQUIRE_AUTHC ) {
869 if( op->o_dn == NULL || *op->o_dn == '\0' ) {
870 *text = "authentication required";
871 return LDAP_UNWILLING_TO_PERFORM;
875 if( requires & SLAP_REQUIRE_BIND ) {
877 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
878 version = conn->c_protocol;
879 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
882 /* no bind has occurred */
883 *text = "BIND required";
884 return LDAP_OPERATIONS_ERROR;
888 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
889 if( op->o_protocol < LDAP_VERSION3 ) {
890 /* no bind has occurred */
891 *text = "operation restricted to LDAPv3 clients";
892 return LDAP_OPERATIONS_ERROR;
897 if( restrictops & opflag ) {
898 if( restrictops == SLAP_RESTRICT_OP_READS ) {
899 *text = "read operations restricted";
901 *text = "operation restricted";
903 return LDAP_UNWILLING_TO_PERFORM;
909 int backend_check_referrals(
916 int rc = LDAP_SUCCESS;
918 if( be->be_chk_referrals ) {
921 rc = be->be_chk_referrals( be,
922 conn, op, dn, ndn, &text );
924 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
925 send_ldap_result( conn, op, rc,
926 NULL, text, NULL, NULL );
941 ObjectClass *group_oc,
942 AttributeDescription *group_at
945 if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
946 /* we won't attempt to send it to a different backend */
948 be = select_backend(gr_ndn, 0);
951 return LDAP_NO_SUCH_OBJECT;
956 return be->be_group( be, conn, op,
957 target, gr_ndn, op_ndn,
958 group_oc, group_at );
961 return LDAP_UNWILLING_TO_PERFORM;
971 AttributeDescription *entry_at,
972 struct berval ***vals
975 if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
976 /* we won't attempt to send it to a different backend */
978 be = select_backend(e_ndn, 0);
981 return LDAP_NO_SUCH_OBJECT;
985 if( be->be_attribute ) {
986 return be->be_attribute( be, conn, op, target, e_ndn,
990 return LDAP_UNWILLING_TO_PERFORM;
993 Attribute *backend_operational(
1001 #ifdef SLAPD_SCHEMA_DN
1002 a = ch_malloc( sizeof( Attribute ) );
1003 a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1005 /* Should be backend specific */
1006 a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1007 a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1008 a->a_vals[1] = NULL;