3 * Copyright 1998-2003 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>
28 * If a module is configured as dynamic, its header should not
29 * get included into slapd. While this is a general rule and does
30 * not have much of an effect in UNIX, this rule should be adhered
31 * to for Windows, where dynamic object code should not be implicitly
32 * imported into slapd without appropriate __declspec(dllimport) directives.
35 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
36 #include "back-bdb/external.h"
38 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
39 #include "back-dnssrv/external.h"
41 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
42 #include "back-hdb/external.h"
44 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
45 #include "back-ldap/external.h"
47 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
48 #include "back-ldbm/external.h"
50 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
51 #include "back-meta/external.h"
53 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
54 #include "back-monitor/external.h"
56 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
57 #include "back-null/external.h"
59 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
60 #include "back-passwd/external.h"
62 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
63 #include "back-perl/external.h"
65 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
66 #include "back-shell/external.h"
68 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
69 #include "back-tcl/external.h"
71 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
72 #include "back-sql/external.h"
74 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
75 #include "private/external.h"
78 static BackendInfo binfo[] = {
79 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
80 {"bdb", bdb_initialize},
82 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
83 {"dnssrv", dnssrv_back_initialize},
85 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
86 {"hdb", hdb_initialize},
88 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
89 {"ldap", ldap_back_initialize},
91 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
92 {"ldbm", ldbm_back_initialize},
94 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
95 {"meta", meta_back_initialize},
97 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
98 {"monitor", monitor_back_initialize},
100 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
101 {"null", null_back_initialize},
103 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
104 {"passwd", passwd_back_initialize},
106 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
107 {"perl", perl_back_initialize},
109 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
110 {"shell", shell_back_initialize},
112 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
113 {"tcl", tcl_back_initialize},
115 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
116 {"sql", sql_back_initialize},
118 /* for any private backend */
119 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
120 {"private", private_back_initialize},
125 int nBackendInfo = 0;
126 BackendInfo *backendInfo = NULL;
129 BackendDB *backendDB = NULL;
131 ldap_pvt_thread_pool_t syncrepl_pool;
132 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
134 int backend_init(void)
138 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
140 if((nBackendInfo != 0) || (backendInfo != NULL)) {
141 /* already initialized */
143 LDAP_LOG( BACKEND, ERR,
144 "backend_init: backend already initialized\n", 0, 0, 0 );
146 Debug( LDAP_DEBUG_ANY,
147 "backend_init: already initialized.\n", 0, 0, 0 );
153 binfo[nBackendInfo].bi_type != NULL;
156 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
160 LDAP_LOG( BACKEND, INFO,
161 "backend_init: initialized for type \"%s\"\n",
162 binfo[nBackendInfo].bi_type, 0, 0 );
164 Debug( LDAP_DEBUG_ANY,
165 "backend_init: initialized for type \"%s\"\n",
166 binfo[nBackendInfo].bi_type, 0, 0 );
168 /* destroy those we've already inited */
173 if ( binfo[nBackendInfo].bi_destroy ) {
174 binfo[nBackendInfo].bi_destroy(
175 &binfo[nBackendInfo] );
182 if ( nBackendInfo > 0) {
192 LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
194 Debug( LDAP_DEBUG_ANY,
195 "backend_init: failed\n",
200 #endif /* SLAPD_MODULES */
203 int backend_add(BackendInfo *aBackendInfo)
207 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
209 LDAP_LOG( BACKEND, ERR,
210 "backend_add: initialization for type \"%s\" failed\n",
211 aBackendInfo->bi_type, 0, 0 );
213 Debug( LDAP_DEBUG_ANY,
214 "backend_add: initialization for type \"%s\" failed\n",
215 aBackendInfo->bi_type, 0, 0 );
220 /* now add the backend type to the Backend Info List */
222 BackendInfo *newBackendInfo = 0;
224 /* if backendInfo == binfo no deallocation of old backendInfo */
225 if (backendInfo == binfo) {
226 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
227 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
230 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
233 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
234 sizeof(BackendInfo));
235 backendInfo = newBackendInfo;
242 int backend_startup(Backend *be)
249 if( ! ( nBackendDB > 0 ) ) {
252 LDAP_LOG( BACKEND, INFO,
253 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
255 Debug( LDAP_DEBUG_ANY,
256 "backend_startup: %d databases to startup.\n",
263 /* startup a specific backend database */
265 LDAP_TAILQ_INIT( &be->be_pending_csn_list );
268 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
269 be->be_suffix[0].bv_val, 0, 0 );
271 Debug( LDAP_DEBUG_TRACE,
272 "backend_startup: starting \"%s\"\n",
273 be->be_suffix[0].bv_val, 0, 0 );
276 if ( be->bd_info->bi_open ) {
277 rc = be->bd_info->bi_open( be->bd_info );
280 LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
282 Debug( LDAP_DEBUG_ANY,
283 "backend_startup: bi_open failed!\n",
291 if ( be->bd_info->bi_db_open ) {
292 rc = be->bd_info->bi_db_open( be );
295 LDAP_LOG( BACKEND, CRIT,
296 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
298 Debug( LDAP_DEBUG_ANY,
299 "backend_startup: bi_db_open failed! (%d)\n",
309 /* open each backend type */
310 for( i = 0; i < nBackendInfo; i++ ) {
311 if( backendInfo[i].bi_nDB == 0) {
312 /* no database of this type, don't open */
316 if( backendInfo[i].bi_open ) {
317 rc = backendInfo[i].bi_open(
321 LDAP_LOG( BACKEND, CRIT,
322 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
324 Debug( LDAP_DEBUG_ANY,
325 "backend_startup: bi_open %d failed!\n",
333 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
334 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
335 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
337 /* open each backend database */
338 for( i = 0; i < nBackendDB; i++ ) {
339 if ( backendDB[i].be_update_ndn.bv_val && (
340 !backendDB[i].be_update_refs &&
341 !backendDB[i].syncinfo &&
342 !default_referral ) ) {
344 LDAP_LOG( BACKEND, CRIT,
345 "backend_startup: slave \"%s\" updateref missing\n",
346 backendDB[i].be_suffix[0].bv_val, 0, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 "backend_startup: slave \"%s\" updateref missing\n",
351 backendDB[i].be_suffix[0].bv_val, 0, 0 );
356 /* append global access controls */
357 acl_append( &backendDB[i].be_acl, global_acl );
359 LDAP_TAILQ_INIT( &backendDB[i].be_pending_csn_list );
361 if ( backendDB[i].bd_info->bi_db_open ) {
362 rc = backendDB[i].bd_info->bi_db_open(
366 LDAP_LOG( BACKEND, CRIT,
367 "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
369 Debug( LDAP_DEBUG_ANY,
370 "backend_startup: bi_db_open(%d) failed! (%d)\n",
377 if ( backendDB[i].syncinfo != NULL ) {
378 syncinfo_t *si = ( syncinfo_t * ) backendDB[i].syncinfo;
379 si->be = &backendDB[i];
380 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
381 ldap_pvt_runqueue_insert( &syncrepl_rq, si->interval,
382 do_syncrepl, (void *) backendDB[i].syncinfo );
383 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
390 int backend_num( Backend *be )
394 if( be == NULL ) return -1;
396 for( i = 0; i < nBackendDB; i++ ) {
397 if( be == &backendDB[i] ) return i;
402 int backend_shutdown( Backend *be )
408 /* shutdown a specific backend database */
410 if ( be->bd_info->bi_nDB == 0 ) {
411 /* no database of this type, we never opened it */
415 if ( be->bd_info->bi_db_close ) {
416 be->bd_info->bi_db_close( be );
419 if( be->bd_info->bi_close ) {
420 be->bd_info->bi_close( be->bd_info );
426 /* close each backend database */
427 for( i = 0; i < nBackendDB; i++ ) {
428 if ( backendDB[i].bd_info->bi_db_close ) {
429 backendDB[i].bd_info->bi_db_close(
435 LDAP_LOG( BACKEND, NOTICE,
436 "backend_shutdown: bi_close %s failed!\n",
437 backendDB[i].be_type, 0, 0 );
439 Debug( LDAP_DEBUG_ANY,
440 "backend_close: bi_close %s failed!\n",
441 backendDB[i].be_type, 0, 0 );
446 /* close each backend type */
447 for( i = 0; i < nBackendInfo; i++ ) {
448 if( backendInfo[i].bi_nDB == 0 ) {
449 /* no database of this type */
453 if( backendInfo[i].bi_close ) {
454 backendInfo[i].bi_close(
462 int backend_destroy(void)
467 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
469 /* destroy each backend database */
470 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
471 if ( bd->bd_info->bi_db_destroy ) {
472 bd->bd_info->bi_db_destroy( bd );
474 ber_bvarray_free( bd->be_suffix );
475 ber_bvarray_free( bd->be_nsuffix );
476 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
477 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
478 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
479 acl_destroy( bd->be_acl, global_acl );
483 /* destroy each backend type */
484 for( i = 0; i < nBackendInfo; i++ ) {
485 if( backendInfo[i].bi_destroy ) {
486 backendInfo[i].bi_destroy(
492 if (backendInfo != binfo) {
495 #endif /* SLAPD_MODULES */
503 BackendInfo* backend_info(const char *type)
507 /* search for the backend type */
508 for( i = 0; i < nBackendInfo; i++ ) {
509 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
510 return &backendInfo[i];
524 BackendInfo *bi = backend_info(type);
528 fprintf( stderr, "Unrecognized database type (%s)\n", type );
532 backendDB = (BackendDB *) ch_realloc(
534 (nBackendDB + 1) * sizeof(Backend) );
536 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
538 be = &backends[nbackends++];
541 be->be_def_limit = deflimit;
542 be->be_dfltaccess = global_default_access;
544 be->be_restrictops = global_restrictops;
545 be->be_requires = global_requires;
546 be->be_ssf_set = global_ssf_set;
548 be->be_context_csn.bv_len = 0;
549 be->be_context_csn.bv_val = NULL;
550 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
551 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
555 /* assign a default depth limit for alias deref */
556 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
559 rc = bi->bi_db_init( be );
563 fprintf( stderr, "database init failed (%s)\n", type );
577 for ( i = 0; i < nbackends; i++ ) {
578 if ( backends[i].bd_info->bi_db_close ) {
579 (*backends[i].bd_info->bi_db_close)( &backends[i] );
591 ber_len_t len, dnlen = dn->bv_len;
594 for ( i = 0; i < nbackends; i++ ) {
595 for ( j = 0; backends[i].be_nsuffix != NULL &&
596 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
598 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
604 len = backends[i].be_nsuffix[j].bv_len;
607 /* suffix is longer than DN */
612 * input DN is normalized, so the separator check
613 * need not look at escaping
615 if ( len && len < dnlen &&
616 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
621 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
622 &dn->bv_val[dnlen-len] ) == 0 )
627 if( manageDSAit && len == dnlen &&
628 !SLAP_GLUE_SUBORDINATE( be ) ) {
645 struct berval *bvsuffix
650 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
651 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
660 be_isroot( Backend *be, struct berval *ndn )
662 if ( !ndn->bv_len ) {
666 if ( !be->be_rootndn.bv_len ) {
670 return dn_match( &be->be_rootndn, ndn );
674 be_isupdate( Backend *be, struct berval *ndn )
676 if ( !ndn->bv_len ) {
680 if ( !be->be_update_ndn.bv_len ) {
684 return dn_match( &be->be_update_ndn, ndn );
688 be_root_dn( Backend *be )
690 return &be->be_rootdn;
694 be_isroot_pw( Operation *op )
699 if ( ! be_isroot( op->o_bd, &op->o_req_ndn ) ) {
703 if( op->o_bd->be_rootpw.bv_len == 0 ) {
707 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
708 ldap_pvt_thread_mutex_lock( &passwd_mutex );
710 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
714 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
716 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
718 lutil_passwd_sasl_conn = NULL;
720 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
732 if ( op->o_bd->be_release ) {
733 /* free and release entry from backend */
734 return op->o_bd->be_release( op, e, rw );
743 backend_unbind( Operation *op, SlapReply *rs )
746 #if defined( LDAP_SLAPI )
747 Slapi_PBlock *pb = op->o_pb;
750 slapi_x_pblock_set_operation( pb, op );
751 #endif /* defined( LDAP_SLAPI ) */
753 for ( i = 0; i < nbackends; i++ ) {
754 #if defined( LDAP_SLAPI )
755 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
756 rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
757 (Slapi_PBlock *)pb );
760 * A preoperation plugin failure will abort the
764 LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
765 "failed\n", 0, 0, 0);
767 Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
768 "failed.\n", 0, 0, 0);
772 #endif /* defined( LDAP_SLAPI ) */
774 if ( backends[i].be_unbind ) {
775 op->o_bd = &backends[i];
776 (*backends[i].be_unbind)( op, rs );
779 #if defined( LDAP_SLAPI )
780 if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
781 (Slapi_PBlock *)pb ) < 0 ) {
783 LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
784 "failed\n", 0, 0, 0);
786 Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
787 "failed.\n", 0, 0, 0);
790 #endif /* defined( LDAP_SLAPI ) */
797 backend_connection_init(
803 for ( i = 0; i < nbackends; i++ ) {
804 if ( backends[i].be_connection_init ) {
805 (*backends[i].be_connection_init)( &backends[i], conn);
813 backend_connection_destroy(
819 for ( i = 0; i < nbackends; i++ ) {
820 if ( backends[i].be_connection_destroy ) {
821 (*backends[i].be_connection_destroy)( &backends[i], conn);
829 backend_check_controls(
833 LDAPControl **ctrls = op->o_ctrls;
834 rs->sr_err = LDAP_SUCCESS;
837 for( ; *ctrls != NULL ; ctrls++ ) {
838 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
839 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
841 rs->sr_text = "control unavailable in context";
842 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
852 backend_check_restrictions(
855 struct berval *opdata )
857 slap_mask_t restrictops;
858 slap_mask_t requires;
866 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
870 restrictops = op->o_bd->be_restrictops;
871 requires = op->o_bd->be_requires;
872 ssf = &op->o_bd->be_ssf_set;
875 restrictops = global_restrictops;
876 requires = global_requires;
877 ssf = &global_ssf_set;
880 switch( op->o_tag ) {
882 opflag = SLAP_RESTRICT_OP_ADD;
886 opflag = SLAP_RESTRICT_OP_BIND;
889 case LDAP_REQ_COMPARE:
890 opflag = SLAP_RESTRICT_OP_COMPARE;
892 case LDAP_REQ_DELETE:
894 opflag = SLAP_RESTRICT_OP_DELETE;
896 case LDAP_REQ_EXTENDED:
897 opflag = SLAP_RESTRICT_OP_EXTENDED;
900 /* treat unspecified as a modify */
901 opflag = SLAP_RESTRICT_OP_MODIFY;
907 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
915 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
920 #ifdef LDAP_EXOP_X_CANCEL
922 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
928 /* treat everything else as a modify */
929 opflag = SLAP_RESTRICT_OP_MODIFY;
933 case LDAP_REQ_MODIFY:
935 opflag = SLAP_RESTRICT_OP_MODIFY;
937 case LDAP_REQ_RENAME:
939 opflag = SLAP_RESTRICT_OP_RENAME;
941 case LDAP_REQ_SEARCH:
942 opflag = SLAP_RESTRICT_OP_SEARCH;
944 case LDAP_REQ_UNBIND:
949 rs->sr_text = "restrict operations internal error";
950 rs->sr_err = LDAP_OTHER;
955 /* these checks don't apply to StartTLS */
957 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
958 if( op->o_transport_ssf < ssf->sss_transport ) {
959 rs->sr_text = "transport confidentiality required";
963 if( op->o_tls_ssf < ssf->sss_tls ) {
964 rs->sr_text = "TLS confidentiality required";
969 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
970 /* simple bind specific check */
971 if( op->o_ssf < ssf->sss_simple_bind ) {
972 rs->sr_text = "confidentiality required";
977 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
978 /* these checks don't apply to SASL bind */
980 if( op->o_sasl_ssf < ssf->sss_sasl ) {
981 rs->sr_text = "SASL confidentiality required";
985 if( op->o_ssf < ssf->sss_ssf ) {
986 rs->sr_text = "confidentiality required";
992 if( op->o_transport_ssf < ssf->sss_update_transport ) {
993 rs->sr_text = "transport update confidentiality required";
997 if( op->o_tls_ssf < ssf->sss_update_tls ) {
998 rs->sr_text = "TLS update confidentiality required";
1002 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1003 rs->sr_text = "SASL update confidentiality required";
1007 if( op->o_ssf < ssf->sss_update_ssf ) {
1008 rs->sr_text = "update confidentiality required";
1012 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1013 op->o_ndn.bv_len == 0 )
1015 rs->sr_text = "modifications require authentication";
1016 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1020 #ifdef SLAP_X_LISTENER_MOD
1021 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1022 /* no "w" mode means readonly */
1023 rs->sr_text = "modifications not allowed on this listener";
1024 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1027 #endif /* SLAP_X_LISTENER_MOD */
1032 /* these checks don't apply to Bind, StartTLS, or Unbind */
1034 if( requires & SLAP_REQUIRE_STRONG ) {
1035 /* should check mechanism */
1036 if( ( op->o_transport_ssf < ssf->sss_transport
1037 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
1039 rs->sr_text = "strong authentication required";
1040 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1045 if( requires & SLAP_REQUIRE_SASL ) {
1046 if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
1047 rs->sr_text = "SASL authentication required";
1048 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1053 if( requires & SLAP_REQUIRE_AUTHC ) {
1054 if( op->o_dn.bv_len == 0 ) {
1055 rs->sr_text = "authentication required";
1056 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1061 if( requires & SLAP_REQUIRE_BIND ) {
1063 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1064 version = op->o_conn->c_protocol;
1065 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1068 /* no bind has occurred */
1069 rs->sr_text = "BIND required";
1070 rs->sr_err = LDAP_OPERATIONS_ERROR;
1075 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1076 if( op->o_protocol < LDAP_VERSION3 ) {
1077 /* no bind has occurred */
1078 rs->sr_text = "operation restricted to LDAPv3 clients";
1079 rs->sr_err = LDAP_OPERATIONS_ERROR;
1084 #ifdef SLAP_X_LISTENER_MOD
1085 if ( !starttls && op->o_dn.bv_len == 0 ) {
1086 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1087 /* no "x" mode means bind required */
1088 rs->sr_text = "bind required on this listener";
1089 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1094 if ( !starttls && !updateop ) {
1095 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1096 /* no "r" mode means no read */
1097 rs->sr_text = "read not allowed on this listener";
1098 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1102 #endif /* SLAP_X_LISTENER_MOD */
1106 if( restrictops & opflag ) {
1107 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1108 rs->sr_text = "read operations restricted";
1110 rs->sr_text = "operation restricted";
1112 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1116 rs->sr_err = LDAP_SUCCESS;
1120 int backend_check_referrals( Operation *op, SlapReply *rs )
1122 rs->sr_err = LDAP_SUCCESS;
1124 if( op->o_bd->be_chk_referrals ) {
1125 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1127 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1128 send_ldap_result( op, rs );
1140 AttributeDescription *at,
1148 if (op->o_bd == NULL) {
1149 rc = LDAP_NO_SUCH_OBJECT;
1150 } else if ( op->o_bd->be_fetch ) {
1151 rc = ( op->o_bd->be_fetch )( op, ndn,
1154 rc = LDAP_UNWILLING_TO_PERFORM;
1163 struct berval *gr_ndn,
1164 struct berval *op_ndn,
1165 ObjectClass *group_oc,
1166 AttributeDescription *group_at
1173 Backend *be = op->o_bd;
1175 if ( op->o_abandon ) return SLAPD_ABANDON;
1177 op->o_bd = select_backend( gr_ndn, 0, 0 );
1179 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1181 for (g = op->o_conn->c_groups; g; g=g->ga_next) {
1182 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1183 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1185 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1189 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1196 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1199 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1202 a = attr_find( e->e_attrs, group_at );
1204 /* If the attribute is a subtype of labeledURI, treat this as
1205 * a dynamic group ala groupOfURLs
1207 if (is_at_subtype( group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1210 struct berval bv, nbase;
1213 Backend *b2 = op->o_bd;
1215 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1218 op->o_bd = select_backend( op_ndn, 0, 0 );
1219 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1224 for (i=0; a->a_vals[i].bv_val; i++) {
1225 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_SUCCESS )
1227 nbase.bv_val = NULL;
1228 /* host part must be empty */
1229 /* attrs and extensions parts must be empty */
1230 if (( ludp->lud_host && *ludp->lud_host )
1231 || ludp->lud_attrs || ludp->lud_exts )
1233 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1234 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1236 switch(ludp->lud_scope) {
1237 case LDAP_SCOPE_BASE:
1238 if ( !dn_match(&nbase, op_ndn)) goto loopit;
1240 case LDAP_SCOPE_ONELEVEL:
1241 dnParent(op_ndn, &bv );
1242 if ( !dn_match(&nbase, &bv)) goto loopit;
1244 case LDAP_SCOPE_SUBTREE:
1245 if ( !dnIsSuffix(op_ndn, &nbase)) goto loopit;
1248 filter = str2filter_x( op, ludp->lud_filter );
1250 if ( test_filter( NULL, user, filter ) == LDAP_COMPARE_TRUE )
1254 filter_free_x( op, filter );
1257 ldap_free_urldesc( ludp );
1258 if ( nbase.bv_val ) {
1259 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1261 if ( rc == 0 ) break;
1263 if ( user != target ) {
1264 be_entry_release_r( op, user );
1269 rc = value_find_ex( group_at,
1270 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1271 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1272 a->a_nvals, op_ndn, op->o_tmpmemctx );
1275 rc = LDAP_NO_SUCH_ATTRIBUTE;
1278 be_entry_release_r( op, e );
1281 rc = LDAP_NO_SUCH_OBJECT;
1284 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1285 g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1286 g->ga_be = op->o_bd;
1287 g->ga_oc = group_oc;
1288 g->ga_at = group_at;
1290 g->ga_len = gr_ndn->bv_len;
1291 strcpy(g->ga_ndn, gr_ndn->bv_val);
1292 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1293 g->ga_next = op->o_conn->c_groups;
1294 op->o_conn->c_groups = g;
1295 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1307 AttributeDescription *entry_at,
1313 int i, j, rc = LDAP_SUCCESS;
1314 AccessControlState acl_state = ACL_STATE_INIT;
1315 Backend *be = op->o_bd;
1317 op->o_bd = select_backend( edn, 0, 0 );
1319 if ( target && dn_match( &target->e_nname, edn ) ) {
1322 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1326 a = attr_find( e->e_attrs, entry_at );
1330 if ( op->o_conn && access_allowed( op,
1331 e, entry_at, NULL, ACL_AUTH,
1332 &acl_state ) == 0 ) {
1333 rc = LDAP_INSUFFICIENT_ACCESS;
1337 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1339 v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1340 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1341 if ( op->o_conn && access_allowed( op,
1344 ACL_AUTH, &acl_state ) == 0 ) {
1348 &a->a_nvals[i], op->o_tmpmemctx );
1349 if (v[j].bv_val ) j++;
1352 op->o_tmpfree( v, op->o_tmpmemctx );
1354 rc = LDAP_INSUFFICIENT_ACCESS;
1362 freeit: if (e != target ) {
1363 be_entry_release_r( op, e );
1371 Attribute *backend_operational(
1376 Attribute *a = NULL, **ap = &a;
1379 * If operational attributes (allegedly) are required,
1380 * and the backend supports specific operational attributes,
1381 * add them to the attribute list
1383 if ( opattrs || ( op->ors_attrs &&
1384 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1385 *ap = slap_operational_subschemaSubentry( op->o_bd );
1386 ap = &(*ap)->a_next;
1389 if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1390 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );