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 /* append global access controls */
340 acl_append( &backendDB[i].be_acl, global_acl );
342 LDAP_TAILQ_INIT( &backendDB[i].be_pending_csn_list );
344 if ( backendDB[i].bd_info->bi_db_open ) {
345 rc = backendDB[i].bd_info->bi_db_open(
349 LDAP_LOG( BACKEND, CRIT,
350 "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
352 Debug( LDAP_DEBUG_ANY,
353 "backend_startup: bi_db_open(%d) failed! (%d)\n",
360 if ( backendDB[i].syncinfo != NULL ) {
361 syncinfo_t *si = ( syncinfo_t * ) backendDB[i].syncinfo;
362 si->be = &backendDB[i];
363 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
364 ldap_pvt_runqueue_insert( &syncrepl_rq, si->interval,
365 do_syncrepl, (void *) backendDB[i].syncinfo );
366 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
373 int backend_num( Backend *be )
377 if( be == NULL ) return -1;
379 for( i = 0; i < nBackendDB; i++ ) {
380 if( be == &backendDB[i] ) return i;
385 int backend_shutdown( Backend *be )
391 /* shutdown a specific backend database */
393 if ( be->bd_info->bi_nDB == 0 ) {
394 /* no database of this type, we never opened it */
398 if ( be->bd_info->bi_db_close ) {
399 be->bd_info->bi_db_close( be );
402 if( be->bd_info->bi_close ) {
403 be->bd_info->bi_close( be->bd_info );
409 /* close each backend database */
410 for( i = 0; i < nBackendDB; i++ ) {
411 if ( backendDB[i].bd_info->bi_db_close ) {
412 backendDB[i].bd_info->bi_db_close(
418 LDAP_LOG( BACKEND, NOTICE,
419 "backend_shutdown: bi_close %s failed!\n",
420 backendDB[i].be_type, 0, 0 );
422 Debug( LDAP_DEBUG_ANY,
423 "backend_close: bi_close %s failed!\n",
424 backendDB[i].be_type, 0, 0 );
429 /* close each backend type */
430 for( i = 0; i < nBackendInfo; i++ ) {
431 if( backendInfo[i].bi_nDB == 0 ) {
432 /* no database of this type */
436 if( backendInfo[i].bi_close ) {
437 backendInfo[i].bi_close(
445 int backend_destroy(void)
450 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
452 /* destroy each backend database */
453 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
454 if ( bd->bd_info->bi_db_destroy ) {
455 bd->bd_info->bi_db_destroy( bd );
457 ber_bvarray_free( bd->be_suffix );
458 ber_bvarray_free( bd->be_nsuffix );
459 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
460 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
461 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
462 acl_destroy( bd->be_acl, global_acl );
466 /* destroy each backend type */
467 for( i = 0; i < nBackendInfo; i++ ) {
468 if( backendInfo[i].bi_destroy ) {
469 backendInfo[i].bi_destroy(
475 if (backendInfo != binfo) {
478 #endif /* SLAPD_MODULES */
486 BackendInfo* backend_info(const char *type)
490 /* search for the backend type */
491 for( i = 0; i < nBackendInfo; i++ ) {
492 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
493 return &backendInfo[i];
507 BackendInfo *bi = backend_info(type);
511 fprintf( stderr, "Unrecognized database type (%s)\n", type );
515 backendDB = (BackendDB *) ch_realloc(
517 (nBackendDB + 1) * sizeof(Backend) );
519 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
521 be = &backends[nbackends++];
524 be->be_def_limit = deflimit;
525 be->be_dfltaccess = global_default_access;
527 be->be_restrictops = global_restrictops;
528 be->be_requires = global_requires;
529 be->be_ssf_set = global_ssf_set;
531 be->be_context_csn.bv_len = 0;
532 be->be_context_csn.bv_val = NULL;
533 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
534 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
538 /* assign a default depth limit for alias deref */
539 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
542 rc = bi->bi_db_init( be );
546 fprintf( stderr, "database init failed (%s)\n", type );
560 for ( i = 0; i < nbackends; i++ ) {
561 if ( backends[i].bd_info->bi_db_close ) {
562 (*backends[i].bd_info->bi_db_close)( &backends[i] );
574 ber_len_t len, dnlen = dn->bv_len;
577 for ( i = 0; i < nbackends; i++ ) {
578 for ( j = 0; backends[i].be_nsuffix != NULL &&
579 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
581 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
587 len = backends[i].be_nsuffix[j].bv_len;
590 /* suffix is longer than DN */
595 * input DN is normalized, so the separator check
596 * need not look at escaping
598 if ( len && len < dnlen &&
599 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
604 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
605 &dn->bv_val[dnlen-len] ) == 0 )
610 if( manageDSAit && len == dnlen ) {
627 struct berval *bvsuffix
632 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
633 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
642 be_isroot( Backend *be, struct berval *ndn )
644 if ( !ndn->bv_len ) {
648 if ( !be->be_rootndn.bv_len ) {
652 return dn_match( &be->be_rootndn, ndn );
656 be_isupdate( Backend *be, struct berval *ndn )
658 if ( !ndn->bv_len ) {
662 if ( !be->be_update_ndn.bv_len ) {
666 return dn_match( &be->be_update_ndn, ndn );
670 be_root_dn( Backend *be )
672 return &be->be_rootdn;
676 be_isroot_pw( Operation *op )
681 if ( ! be_isroot( op->o_bd, &op->o_req_ndn ) ) {
685 if( op->o_bd->be_rootpw.bv_len == 0 ) {
689 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
690 ldap_pvt_thread_mutex_lock( &passwd_mutex );
692 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
696 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
698 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
700 lutil_passwd_sasl_conn = NULL;
702 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
714 if ( op->o_bd->be_release ) {
715 /* free and release entry from backend */
716 return op->o_bd->be_release( op, e, rw );
725 backend_unbind( Operation *op, SlapReply *rs )
728 #if defined( LDAP_SLAPI )
729 Slapi_PBlock *pb = op->o_pb;
732 slapi_x_pblock_set_operation( pb, op );
733 #endif /* defined( LDAP_SLAPI ) */
735 for ( i = 0; i < nbackends; i++ ) {
736 #if defined( LDAP_SLAPI )
737 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
738 rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
739 (Slapi_PBlock *)pb );
742 * A preoperation plugin failure will abort the
746 LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
747 "failed\n", 0, 0, 0);
749 Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
750 "failed.\n", 0, 0, 0);
754 #endif /* defined( LDAP_SLAPI ) */
756 if ( backends[i].be_unbind ) {
757 op->o_bd = &backends[i];
758 (*backends[i].be_unbind)( op, rs );
761 #if defined( LDAP_SLAPI )
762 if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
763 (Slapi_PBlock *)pb ) < 0 ) {
765 LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
766 "failed\n", 0, 0, 0);
768 Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
769 "failed.\n", 0, 0, 0);
772 #endif /* defined( LDAP_SLAPI ) */
779 backend_connection_init(
785 for ( i = 0; i < nbackends; i++ ) {
786 if ( backends[i].be_connection_init ) {
787 (*backends[i].be_connection_init)( &backends[i], conn);
795 backend_connection_destroy(
801 for ( i = 0; i < nbackends; i++ ) {
802 if ( backends[i].be_connection_destroy ) {
803 (*backends[i].be_connection_destroy)( &backends[i], conn);
811 backend_check_controls(
815 LDAPControl **ctrls = op->o_ctrls;
816 rs->sr_err = LDAP_SUCCESS;
819 for( ; *ctrls != NULL ; ctrls++ ) {
820 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
821 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
823 rs->sr_text = "control unavailable in context";
824 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
834 backend_check_restrictions(
837 struct berval *opdata )
839 slap_mask_t restrictops;
840 slap_mask_t requires;
848 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
852 restrictops = op->o_bd->be_restrictops;
853 requires = op->o_bd->be_requires;
854 ssf = &op->o_bd->be_ssf_set;
857 restrictops = global_restrictops;
858 requires = global_requires;
859 ssf = &global_ssf_set;
862 switch( op->o_tag ) {
864 opflag = SLAP_RESTRICT_OP_ADD;
868 opflag = SLAP_RESTRICT_OP_BIND;
871 case LDAP_REQ_COMPARE:
872 opflag = SLAP_RESTRICT_OP_COMPARE;
874 case LDAP_REQ_DELETE:
876 opflag = SLAP_RESTRICT_OP_DELETE;
878 case LDAP_REQ_EXTENDED:
879 opflag = SLAP_RESTRICT_OP_EXTENDED;
882 /* treat unspecified as a modify */
883 opflag = SLAP_RESTRICT_OP_MODIFY;
889 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
897 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
902 #ifdef LDAP_EXOP_X_CANCEL
904 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
910 /* treat everything else as a modify */
911 opflag = SLAP_RESTRICT_OP_MODIFY;
915 case LDAP_REQ_MODIFY:
917 opflag = SLAP_RESTRICT_OP_MODIFY;
919 case LDAP_REQ_RENAME:
921 opflag = SLAP_RESTRICT_OP_RENAME;
923 case LDAP_REQ_SEARCH:
924 opflag = SLAP_RESTRICT_OP_SEARCH;
926 case LDAP_REQ_UNBIND:
931 rs->sr_text = "restrict operations internal error";
932 rs->sr_err = LDAP_OTHER;
937 /* these checks don't apply to StartTLS */
939 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
940 if( op->o_transport_ssf < ssf->sss_transport ) {
941 rs->sr_text = "transport confidentiality required";
945 if( op->o_tls_ssf < ssf->sss_tls ) {
946 rs->sr_text = "TLS confidentiality required";
951 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
952 /* simple bind specific check */
953 if( op->o_ssf < ssf->sss_simple_bind ) {
954 rs->sr_text = "confidentiality required";
959 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
960 /* these checks don't apply to SASL bind */
962 if( op->o_sasl_ssf < ssf->sss_sasl ) {
963 rs->sr_text = "SASL confidentiality required";
967 if( op->o_ssf < ssf->sss_ssf ) {
968 rs->sr_text = "confidentiality required";
974 if( op->o_transport_ssf < ssf->sss_update_transport ) {
975 rs->sr_text = "transport update confidentiality required";
979 if( op->o_tls_ssf < ssf->sss_update_tls ) {
980 rs->sr_text = "TLS update confidentiality required";
984 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
985 rs->sr_text = "SASL update confidentiality required";
989 if( op->o_ssf < ssf->sss_update_ssf ) {
990 rs->sr_text = "update confidentiality required";
994 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
995 op->o_ndn.bv_len == 0 )
997 rs->sr_text = "modifications require authentication";
998 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1002 #ifdef SLAP_X_LISTENER_MOD
1003 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1004 /* no "w" mode means readonly */
1005 rs->sr_text = "modifications not allowed on this listener";
1006 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1009 #endif /* SLAP_X_LISTENER_MOD */
1014 /* these checks don't apply to Bind, StartTLS, or Unbind */
1016 if( requires & SLAP_REQUIRE_STRONG ) {
1017 /* should check mechanism */
1018 if( ( op->o_transport_ssf < ssf->sss_transport
1019 && op->o_authmech.bv_len == 0 ) || op->o_dn.bv_len == 0 )
1021 rs->sr_text = "strong authentication required";
1022 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1027 if( requires & SLAP_REQUIRE_SASL ) {
1028 if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 ) {
1029 rs->sr_text = "SASL authentication required";
1030 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1035 if( requires & SLAP_REQUIRE_AUTHC ) {
1036 if( op->o_dn.bv_len == 0 ) {
1037 rs->sr_text = "authentication required";
1038 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1043 if( requires & SLAP_REQUIRE_BIND ) {
1045 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1046 version = op->o_conn->c_protocol;
1047 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1050 /* no bind has occurred */
1051 rs->sr_text = "BIND required";
1052 rs->sr_err = LDAP_OPERATIONS_ERROR;
1057 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1058 if( op->o_protocol < LDAP_VERSION3 ) {
1059 /* no bind has occurred */
1060 rs->sr_text = "operation restricted to LDAPv3 clients";
1061 rs->sr_err = LDAP_OPERATIONS_ERROR;
1066 #ifdef SLAP_X_LISTENER_MOD
1067 if ( !starttls && op->o_dn.bv_len == 0 ) {
1068 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1069 /* no "x" mode means bind required */
1070 rs->sr_text = "bind required on this listener";
1071 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1076 if ( !starttls && !updateop ) {
1077 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1078 /* no "r" mode means no read */
1079 rs->sr_text = "read not allowed on this listener";
1080 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1084 #endif /* SLAP_X_LISTENER_MOD */
1088 if( restrictops & opflag ) {
1089 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1090 rs->sr_text = "read operations restricted";
1092 rs->sr_text = "operation restricted";
1094 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1098 rs->sr_err = LDAP_SUCCESS;
1102 int backend_check_referrals( Operation *op, SlapReply *rs )
1104 rs->sr_err = LDAP_SUCCESS;
1106 if( op->o_bd->be_chk_referrals ) {
1107 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1109 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1110 send_ldap_result( op, rs );
1122 AttributeDescription *at,
1130 if (op->o_bd == NULL) {
1131 rc = LDAP_NO_SUCH_OBJECT;
1132 } else if ( op->o_bd->be_fetch ) {
1133 rc = ( op->o_bd->be_fetch )( op, ndn,
1136 rc = LDAP_UNWILLING_TO_PERFORM;
1145 struct berval *gr_ndn,
1146 struct berval *op_ndn,
1147 ObjectClass *group_oc,
1148 AttributeDescription *group_at
1155 Backend *be = op->o_bd;
1157 if ( op->o_abandon ) return SLAPD_ABANDON;
1159 op->o_bd = select_backend( gr_ndn, 0, 0 );
1161 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1163 for (g = op->o_conn->c_groups; g; g=g->ga_next) {
1164 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1165 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1167 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1171 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1178 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1181 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1184 a = attr_find( e->e_attrs, group_at );
1186 /* If the attribute is a subtype of labeledURI, treat this as
1187 * a dynamic group ala groupOfURLs
1189 if (is_at_subtype( group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1192 struct berval bv, nbase;
1195 Backend *b2 = op->o_bd;
1197 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1200 op->o_bd = select_backend( op_ndn, 0, 0 );
1201 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1206 for (i=0; a->a_vals[i].bv_val; i++) {
1207 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_SUCCESS )
1209 nbase.bv_val = NULL;
1210 /* host part must be empty */
1211 /* attrs and extensions parts must be empty */
1212 if (( ludp->lud_host && *ludp->lud_host )
1213 || ludp->lud_attrs || ludp->lud_exts )
1215 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1216 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1218 switch(ludp->lud_scope) {
1219 case LDAP_SCOPE_BASE:
1220 if ( !dn_match(&nbase, op_ndn)) goto loopit;
1222 case LDAP_SCOPE_ONELEVEL:
1223 dnParent(op_ndn, &bv );
1224 if ( !dn_match(&nbase, &bv)) goto loopit;
1226 case LDAP_SCOPE_SUBTREE:
1227 if ( !dnIsSuffix(op_ndn, &nbase)) goto loopit;
1230 filter = str2filter_x( op, ludp->lud_filter );
1232 if ( test_filter( NULL, user, filter ) == LDAP_COMPARE_TRUE )
1236 filter_free_x( op, filter );
1239 ldap_free_urldesc( ludp );
1240 if ( nbase.bv_val ) {
1241 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1243 if ( rc == 0 ) break;
1245 if ( user != target ) {
1246 be_entry_release_r( op, user );
1251 rc = value_find_ex( group_at,
1252 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1253 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1254 a->a_nvals, op_ndn, op->o_tmpmemctx );
1257 rc = LDAP_NO_SUCH_ATTRIBUTE;
1260 be_entry_release_r( op, e );
1263 rc = LDAP_NO_SUCH_OBJECT;
1266 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1267 g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
1268 g->ga_be = op->o_bd;
1269 g->ga_oc = group_oc;
1270 g->ga_at = group_at;
1272 g->ga_len = gr_ndn->bv_len;
1273 strcpy(g->ga_ndn, gr_ndn->bv_val);
1274 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1275 g->ga_next = op->o_conn->c_groups;
1276 op->o_conn->c_groups = g;
1277 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1289 AttributeDescription *entry_at,
1295 int i, j, rc = LDAP_SUCCESS;
1296 AccessControlState acl_state = ACL_STATE_INIT;
1297 Backend *be = op->o_bd;
1299 op->o_bd = select_backend( edn, 0, 0 );
1301 if ( target && dn_match( &target->e_nname, edn ) ) {
1304 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1308 a = attr_find( e->e_attrs, entry_at );
1312 if ( op->o_conn && access_allowed( op,
1313 e, entry_at, NULL, ACL_AUTH,
1314 &acl_state ) == 0 ) {
1315 rc = LDAP_INSUFFICIENT_ACCESS;
1319 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1321 v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1322 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1323 if ( op->o_conn && access_allowed( op,
1326 ACL_AUTH, &acl_state ) == 0 ) {
1330 &a->a_nvals[i], op->o_tmpmemctx );
1331 if (v[j].bv_val ) j++;
1334 op->o_tmpfree( v, op->o_tmpmemctx );
1336 rc = LDAP_INSUFFICIENT_ACCESS;
1344 freeit: if (e != target ) {
1345 be_entry_release_r( op, e );
1353 Attribute *backend_operational(
1358 Attribute *a = NULL, **ap = &a;
1361 * If operational attributes (allegedly) are required,
1362 * and the backend supports specific operational attributes,
1363 * add them to the attribute list
1365 if ( opattrs || ( op->ors_attrs &&
1366 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1367 *ap = slap_operational_subschemaSubentry( op->o_bd );
1368 ap = &(*ap)->a_next;
1371 if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1372 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );