1 /* backend.c - routines for dealing with back-end databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
43 #include "slapi/slapi.h"
47 * If a module is configured as dynamic, its header should not
48 * get included into slapd. While this is a general rule and does
49 * not have much of an effect in UNIX, this rule should be adhered
50 * to for Windows, where dynamic object code should not be implicitly
51 * imported into slapd without appropriate __declspec(dllimport) directives.
54 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
55 #include "back-bdb/external.h"
57 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
58 #include "back-dnssrv/external.h"
60 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
61 #include "back-hdb/external.h"
63 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
64 #include "back-ldap/external.h"
66 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
67 #include "back-ldbm/external.h"
69 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
70 #include "back-meta/external.h"
72 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
73 #include "back-monitor/external.h"
75 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
76 #include "back-null/external.h"
78 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
79 #include "back-passwd/external.h"
81 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
82 #include "back-perl/external.h"
84 #if defined(SLAPD_RELAY) && !defined(SLAPD_RELAY_DYNAMIC)
85 #include "back-relay/external.h"
87 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
88 #include "back-shell/external.h"
90 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
91 #include "back-tcl/external.h"
93 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
94 #include "back-sql/external.h"
96 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
97 #include "private/external.h"
100 static BackendInfo binfo[] = {
101 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
102 {"bdb", bdb_initialize},
104 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
105 {"dnssrv", dnssrv_back_initialize},
107 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
108 {"hdb", hdb_initialize},
110 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
111 {"ldap", ldap_back_initialize},
113 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
114 {"ldbm", ldbm_back_initialize},
116 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
117 {"meta", meta_back_initialize},
119 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
120 {"monitor", monitor_back_initialize},
122 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
123 {"null", null_back_initialize},
125 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
126 {"passwd", passwd_back_initialize},
128 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
129 {"perl", perl_back_initialize},
131 #if defined(SLAPD_RELAY) && !defined(SLAPD_RELAY_DYNAMIC)
132 {"relay", relay_back_initialize},
134 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
135 {"shell", shell_back_initialize},
137 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
138 {"tcl", tcl_back_initialize},
140 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
141 {"sql", sql_back_initialize},
143 /* for any private backend */
144 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
145 {"private", private_back_initialize},
150 int nBackendInfo = 0;
151 BackendInfo *backendInfo = NULL;
154 BackendDB *backendDB = NULL;
156 ldap_pvt_thread_pool_t syncrepl_pool;
157 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
159 int backend_init(void)
163 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
165 if((nBackendInfo != 0) || (backendInfo != NULL)) {
166 /* already initialized */
168 LDAP_LOG( BACKEND, ERR,
169 "backend_init: backend already initialized\n", 0, 0, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "backend_init: already initialized.\n", 0, 0, 0 );
178 binfo[nBackendInfo].bi_type != NULL;
181 assert( binfo[nBackendInfo].bi_init );
183 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
187 LDAP_LOG( BACKEND, INFO,
188 "backend_init: initialized for type \"%s\"\n",
189 binfo[nBackendInfo].bi_type, 0, 0 );
191 Debug( LDAP_DEBUG_ANY,
192 "backend_init: initialized for type \"%s\"\n",
193 binfo[nBackendInfo].bi_type, 0, 0 );
195 /* destroy those we've already inited */
200 if ( binfo[nBackendInfo].bi_destroy ) {
201 binfo[nBackendInfo].bi_destroy(
202 &binfo[nBackendInfo] );
209 if ( nBackendInfo > 0) {
219 LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
221 Debug( LDAP_DEBUG_ANY,
222 "backend_init: failed\n",
227 #endif /* SLAPD_MODULES */
230 int backend_add(BackendInfo *aBackendInfo)
234 if ( aBackendInfo->bi_init == NULL ) {
236 LDAP_LOG( BACKEND, ERR,
237 "backend_add: backend type \"%s\" does not have the "
238 "(mandatory)init function\n",
239 aBackendInfo->bi_type, 0, 0 );
241 Debug( LDAP_DEBUG_ANY,
242 "backend_add: backend type \"%s\" does not have the "
243 "(mandatory)init function\n",
244 aBackendInfo->bi_type, 0, 0 );
249 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
251 LDAP_LOG( BACKEND, ERR,
252 "backend_add: initialization for type \"%s\" failed\n",
253 aBackendInfo->bi_type, 0, 0 );
255 Debug( LDAP_DEBUG_ANY,
256 "backend_add: initialization for type \"%s\" failed\n",
257 aBackendInfo->bi_type, 0, 0 );
262 /* now add the backend type to the Backend Info List */
264 BackendInfo *newBackendInfo = 0;
266 /* if backendInfo == binfo no deallocation of old backendInfo */
267 if (backendInfo == binfo) {
268 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
269 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
272 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
275 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
276 sizeof(BackendInfo));
277 backendInfo = newBackendInfo;
284 int backend_startup(Backend *be)
289 if( ! ( nBackendDB > 0 ) ) {
292 LDAP_LOG( BACKEND, INFO,
293 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
295 Debug( LDAP_DEBUG_ANY,
296 "backend_startup: %d databases to startup.\n",
303 /* startup a specific backend database */
305 LDAP_TAILQ_INIT( &be->be_pending_csn_list );
308 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
309 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
312 Debug( LDAP_DEBUG_TRACE,
313 "backend_startup: starting \"%s\"\n",
314 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
318 if ( be->bd_info->bi_open ) {
319 rc = be->bd_info->bi_open( be->bd_info );
322 LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
324 Debug( LDAP_DEBUG_ANY,
325 "backend_startup: bi_open failed!\n",
333 if ( be->bd_info->bi_db_open ) {
334 rc = be->bd_info->bi_db_open( be );
337 LDAP_LOG( BACKEND, CRIT,
338 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
340 Debug( LDAP_DEBUG_ANY,
341 "backend_startup: bi_db_open failed! (%d)\n",
351 /* open each backend type */
352 for( i = 0; i < nBackendInfo; i++ ) {
353 if( backendInfo[i].bi_nDB == 0) {
354 /* no database of this type, don't open */
358 if( backendInfo[i].bi_open ) {
359 rc = backendInfo[i].bi_open(
363 LDAP_LOG( BACKEND, CRIT,
364 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
366 Debug( LDAP_DEBUG_ANY,
367 "backend_startup: bi_open %d failed!\n",
375 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
376 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
377 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
379 /* open each backend database */
380 for( i = 0; i < nBackendDB; i++ ) {
381 /* append global access controls */
382 acl_append( &backendDB[i].be_acl, global_acl );
384 LDAP_TAILQ_INIT( &backendDB[i].be_pending_csn_list );
386 if ( backendDB[i].be_suffix == NULL ) {
388 LDAP_LOG( BACKEND, CRIT,
389 "backend_startup: warning, database %d (%s) "
391 i, backendDB[i].bd_info->bi_type, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "backend_startup: warning, database %d (%s) "
396 i, backendDB[i].bd_info->bi_type, 0 );
400 if ( backendDB[i].bd_info->bi_db_open ) {
401 rc = backendDB[i].bd_info->bi_db_open(
405 LDAP_LOG( BACKEND, CRIT,
406 "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
408 Debug( LDAP_DEBUG_ANY,
409 "backend_startup: bi_db_open(%d) failed! (%d)\n",
416 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
419 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
420 backendDB[i].be_modify && backendDB[i].be_delete )) {
422 LDAP_LOG( BACKEND, CRIT,
423 "backend_startup: database(%d) does not support "
424 "operations required for syncrepl", i, 0, 0 );
426 Debug( LDAP_DEBUG_ANY,
427 "backend_startup: database(%d) does not support "
428 "operations required for syncrepl", i, 0, 0 );
433 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
434 si->si_be = &backendDB[i];
436 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
437 ldap_pvt_runqueue_insert( &syncrepl_rq,
438 si->si_interval, do_syncrepl, (void *) si );
439 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
447 int backend_num( Backend *be )
451 if( be == NULL ) return -1;
453 for( i = 0; i < nBackendDB; i++ ) {
454 if( be == &backendDB[i] ) return i;
459 int backend_shutdown( Backend *be )
465 /* shutdown a specific backend database */
467 if ( be->bd_info->bi_nDB == 0 ) {
468 /* no database of this type, we never opened it */
472 if ( be->bd_info->bi_db_close ) {
473 be->bd_info->bi_db_close( be );
476 if( be->bd_info->bi_close ) {
477 be->bd_info->bi_close( be->bd_info );
483 /* close each backend database */
484 for( i = 0; i < nBackendDB; i++ ) {
485 if ( backendDB[i].bd_info->bi_db_close ) {
486 backendDB[i].bd_info->bi_db_close(
492 LDAP_LOG( BACKEND, NOTICE,
493 "backend_shutdown: bi_close %s failed!\n",
494 backendDB[i].be_type, 0, 0 );
496 Debug( LDAP_DEBUG_ANY,
497 "backend_close: bi_close %s failed!\n",
498 backendDB[i].be_type, 0, 0 );
503 /* close each backend type */
504 for( i = 0; i < nBackendInfo; i++ ) {
505 if( backendInfo[i].bi_nDB == 0 ) {
506 /* no database of this type */
510 if( backendInfo[i].bi_close ) {
511 backendInfo[i].bi_close(
519 int backend_destroy(void)
524 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
526 /* destroy each backend database */
527 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
528 if ( bd->bd_info->bi_db_destroy ) {
529 bd->bd_info->bi_db_destroy( bd );
531 ber_bvarray_free( bd->be_suffix );
532 ber_bvarray_free( bd->be_nsuffix );
533 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
534 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
535 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
536 acl_destroy( bd->be_acl, global_acl );
540 /* destroy each backend type */
541 for( i = 0; i < nBackendInfo; i++ ) {
542 if( backendInfo[i].bi_destroy ) {
543 backendInfo[i].bi_destroy(
549 if (backendInfo != binfo) {
552 #endif /* SLAPD_MODULES */
560 BackendInfo* backend_info(const char *type)
564 /* search for the backend type */
565 for( i = 0; i < nBackendInfo; i++ ) {
566 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
567 return &backendInfo[i];
581 BackendInfo *bi = backend_info(type);
585 fprintf( stderr, "Unrecognized database type (%s)\n", type );
589 backendDB = (BackendDB *) ch_realloc(
591 (nBackendDB + 1) * sizeof(Backend) );
593 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
595 be = &backends[nbackends++];
598 be->be_def_limit = deflimit;
599 be->be_dfltaccess = global_default_access;
601 be->be_restrictops = global_restrictops;
602 be->be_requires = global_requires;
603 be->be_ssf_set = global_ssf_set;
605 be->be_context_csn.bv_len = 0;
606 be->be_context_csn.bv_val = NULL;
607 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
608 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
610 LDAP_STAILQ_INIT( &be->be_syncinfo );
612 /* assign a default depth limit for alias deref */
613 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
616 rc = bi->bi_db_init( be );
620 fprintf( stderr, "database init failed (%s)\n", type );
634 for ( i = 0; i < nbackends; i++ ) {
635 if ( backends[i].bd_info->bi_db_close ) {
636 (*backends[i].bd_info->bi_db_close)( &backends[i] );
648 ber_len_t len, dnlen = dn->bv_len;
651 for ( i = 0; i < nbackends; i++ ) {
652 for ( j = 0; backends[i].be_nsuffix != NULL &&
653 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
655 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
661 len = backends[i].be_nsuffix[j].bv_len;
664 /* suffix is longer than DN */
669 * input DN is normalized, so the separator check
670 * need not look at escaping
672 if ( len && len < dnlen &&
673 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
678 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
679 &dn->bv_val[dnlen-len] ) == 0 )
684 if( manageDSAit && len == dnlen &&
685 !SLAP_GLUE_SUBORDINATE( be ) ) {
702 struct berval *bvsuffix
707 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
708 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
717 be_isroot( Backend *be, struct berval *ndn )
719 if ( !ndn->bv_len ) {
723 if ( !be->be_rootndn.bv_len ) {
727 return dn_match( &be->be_rootndn, ndn );
731 be_isupdate( Backend *be, struct berval *ndn )
733 if ( !ndn->bv_len ) {
737 if ( !be->be_update_ndn.bv_len ) {
741 return dn_match( &be->be_update_ndn, ndn );
745 be_root_dn( Backend *be )
747 return &be->be_rootdn;
751 be_isroot_dn( Operation *op )
753 return be_isroot( op->o_bd, &op->o_ndn );
757 be_isroot_pw( Operation *op )
762 if ( ! be_isroot_dn( op ) ) {
766 if( op->o_bd->be_rootpw.bv_len == 0 ) {
770 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
771 ldap_pvt_thread_mutex_lock( &passwd_mutex );
773 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
777 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
779 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
781 lutil_passwd_sasl_conn = NULL;
783 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
795 if ( op->o_bd->be_release ) {
796 /* free and release entry from backend */
797 return op->o_bd->be_release( op, e, rw );
806 backend_unbind( Operation *op, SlapReply *rs )
810 for ( i = 0; i < nbackends; i++ ) {
811 #if defined( LDAP_SLAPI )
814 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
815 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
816 rc = slapi_int_call_plugins( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
817 (Slapi_PBlock *)op->o_pb );
820 * A preoperation plugin failure will abort the
824 LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
825 "failed\n", 0, 0, 0);
827 Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
828 "failed.\n", 0, 0, 0);
833 #endif /* defined( LDAP_SLAPI ) */
835 if ( backends[i].be_unbind ) {
836 op->o_bd = &backends[i];
837 (*backends[i].be_unbind)( op, rs );
840 #if defined( LDAP_SLAPI )
841 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
842 (Slapi_PBlock *)op->o_pb ) < 0 ) {
844 LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
845 "failed\n", 0, 0, 0);
847 Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
848 "failed.\n", 0, 0, 0);
851 #endif /* defined( LDAP_SLAPI ) */
858 backend_connection_init(
864 for ( i = 0; i < nbackends; i++ ) {
865 if ( backends[i].be_connection_init ) {
866 (*backends[i].be_connection_init)( &backends[i], conn);
874 backend_connection_destroy(
880 for ( i = 0; i < nbackends; i++ ) {
881 if ( backends[i].be_connection_destroy ) {
882 (*backends[i].be_connection_destroy)( &backends[i], conn);
890 backend_check_controls(
894 LDAPControl **ctrls = op->o_ctrls;
895 rs->sr_err = LDAP_SUCCESS;
898 for( ; *ctrls != NULL ; ctrls++ ) {
899 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
900 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
902 rs->sr_text = "control unavailable in context";
903 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
913 backend_check_restrictions(
916 struct berval *opdata )
918 slap_mask_t restrictops;
919 slap_mask_t requires;
927 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
931 restrictops = op->o_bd->be_restrictops;
932 requires = op->o_bd->be_requires;
933 ssf = &op->o_bd->be_ssf_set;
936 restrictops = global_restrictops;
937 requires = global_requires;
938 ssf = &global_ssf_set;
941 switch( op->o_tag ) {
943 opflag = SLAP_RESTRICT_OP_ADD;
947 opflag = SLAP_RESTRICT_OP_BIND;
950 case LDAP_REQ_COMPARE:
951 opflag = SLAP_RESTRICT_OP_COMPARE;
953 case LDAP_REQ_DELETE:
955 opflag = SLAP_RESTRICT_OP_DELETE;
957 case LDAP_REQ_EXTENDED:
958 opflag = SLAP_RESTRICT_OP_EXTENDED;
961 /* treat unspecified as a modify */
962 opflag = SLAP_RESTRICT_OP_MODIFY;
967 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
973 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
977 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
981 /* treat everything else as a modify */
982 opflag = SLAP_RESTRICT_OP_MODIFY;
986 case LDAP_REQ_MODIFY:
988 opflag = SLAP_RESTRICT_OP_MODIFY;
990 case LDAP_REQ_RENAME:
992 opflag = SLAP_RESTRICT_OP_RENAME;
994 case LDAP_REQ_SEARCH:
995 opflag = SLAP_RESTRICT_OP_SEARCH;
997 case LDAP_REQ_UNBIND:
1002 rs->sr_text = "restrict operations internal error";
1003 rs->sr_err = LDAP_OTHER;
1008 /* these checks don't apply to StartTLS */
1010 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1011 if( op->o_transport_ssf < ssf->sss_transport ) {
1012 rs->sr_text = "transport confidentiality required";
1016 if( op->o_tls_ssf < ssf->sss_tls ) {
1017 rs->sr_text = "TLS confidentiality required";
1022 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1023 /* simple bind specific check */
1024 if( op->o_ssf < ssf->sss_simple_bind ) {
1025 rs->sr_text = "confidentiality required";
1030 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1031 /* these checks don't apply to SASL bind */
1033 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1034 rs->sr_text = "SASL confidentiality required";
1038 if( op->o_ssf < ssf->sss_ssf ) {
1039 rs->sr_text = "confidentiality required";
1045 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1046 rs->sr_text = "transport update confidentiality required";
1050 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1051 rs->sr_text = "TLS update confidentiality required";
1055 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1056 rs->sr_text = "SASL update confidentiality required";
1060 if( op->o_ssf < ssf->sss_update_ssf ) {
1061 rs->sr_text = "update confidentiality required";
1065 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1066 op->o_ndn.bv_len == 0 )
1068 rs->sr_text = "modifications require authentication";
1069 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1073 #ifdef SLAP_X_LISTENER_MOD
1074 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1075 /* no "w" mode means readonly */
1076 rs->sr_text = "modifications not allowed on this listener";
1077 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1080 #endif /* SLAP_X_LISTENER_MOD */
1085 /* these checks don't apply to Bind, StartTLS, or Unbind */
1087 if( requires & SLAP_REQUIRE_STRONG ) {
1088 /* should check mechanism */
1089 if( ( op->o_transport_ssf < ssf->sss_transport
1090 && op->o_authtype == LDAP_AUTH_SIMPLE )
1091 || op->o_dn.bv_len == 0 )
1093 rs->sr_text = "strong(er) authentication required";
1094 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1099 if( requires & SLAP_REQUIRE_SASL ) {
1100 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1101 rs->sr_text = "SASL authentication required";
1102 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1107 if( requires & SLAP_REQUIRE_AUTHC ) {
1108 if( op->o_dn.bv_len == 0 ) {
1109 rs->sr_text = "authentication required";
1110 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1115 if( requires & SLAP_REQUIRE_BIND ) {
1117 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1118 version = op->o_conn->c_protocol;
1119 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1122 /* no bind has occurred */
1123 rs->sr_text = "BIND required";
1124 rs->sr_err = LDAP_OPERATIONS_ERROR;
1129 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1130 if( op->o_protocol < LDAP_VERSION3 ) {
1131 /* no bind has occurred */
1132 rs->sr_text = "operation restricted to LDAPv3 clients";
1133 rs->sr_err = LDAP_OPERATIONS_ERROR;
1138 #ifdef SLAP_X_LISTENER_MOD
1139 if ( !starttls && op->o_dn.bv_len == 0 ) {
1140 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1141 /* no "x" mode means bind required */
1142 rs->sr_text = "bind required on this listener";
1143 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1148 if ( !starttls && !updateop ) {
1149 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1150 /* no "r" mode means no read */
1151 rs->sr_text = "read not allowed on this listener";
1152 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1156 #endif /* SLAP_X_LISTENER_MOD */
1160 if( restrictops & opflag ) {
1161 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1162 rs->sr_text = "read operations restricted";
1164 rs->sr_text = "operation restricted";
1166 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1170 rs->sr_err = LDAP_SUCCESS;
1174 int backend_check_referrals( Operation *op, SlapReply *rs )
1176 rs->sr_err = LDAP_SUCCESS;
1178 if( op->o_bd->be_chk_referrals ) {
1179 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1181 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1182 send_ldap_result( op, rs );
1194 AttributeDescription *at,
1202 if (op->o_bd == NULL) {
1203 rc = LDAP_NO_SUCH_OBJECT;
1204 } else if ( op->o_bd->be_fetch ) {
1205 rc = ( op->o_bd->be_fetch )( op, ndn,
1208 rc = LDAP_UNWILLING_TO_PERFORM;
1217 struct berval *gr_ndn,
1218 struct berval *op_ndn,
1219 ObjectClass *group_oc,
1220 AttributeDescription *group_at
1227 Backend *be = op->o_bd;
1229 if ( op->o_abandon ) return SLAPD_ABANDON;
1231 op->o_bd = select_backend( gr_ndn, 0, 0 );
1233 for (g = op->o_groups; g; g=g->ga_next) {
1234 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1235 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1237 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1246 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1250 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1253 a = attr_find( e->e_attrs, group_at );
1255 /* If the attribute is a subtype of labeledURI, treat this as
1256 * a dynamic group ala groupOfURLs
1258 if (is_at_subtype( group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1261 struct berval bv, nbase;
1264 Backend *b2 = op->o_bd;
1266 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1269 op->o_bd = select_backend( op_ndn, 0, 0 );
1270 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1275 for (i=0; a->a_vals[i].bv_val; i++) {
1276 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_SUCCESS )
1278 nbase.bv_val = NULL;
1279 /* host part must be empty */
1280 /* attrs and extensions parts must be empty */
1281 if (( ludp->lud_host && *ludp->lud_host )
1282 || ludp->lud_attrs || ludp->lud_exts )
1284 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1285 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1287 switch(ludp->lud_scope) {
1288 case LDAP_SCOPE_BASE:
1289 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1291 case LDAP_SCOPE_ONELEVEL:
1292 dnParent(op_ndn, &bv );
1293 if ( !dn_match( &nbase, &bv )) goto loopit;
1295 case LDAP_SCOPE_SUBTREE:
1296 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1298 #ifdef LDAP_SCOPE_SUBORDINATE
1299 case LDAP_SCOPE_SUBORDINATE:
1300 if ( dn_match( &nbase, op_ndn ) &&
1301 !dnIsSuffix(op_ndn, &nbase ))
1307 filter = str2filter_x( op, ludp->lud_filter );
1309 if ( test_filter( NULL, user, filter ) == LDAP_COMPARE_TRUE )
1313 filter_free_x( op, filter );
1316 ldap_free_urldesc( ludp );
1317 if ( nbase.bv_val ) {
1318 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1320 if ( rc == 0 ) break;
1322 if ( user != target ) {
1323 be_entry_release_r( op, user );
1328 rc = value_find_ex( group_at,
1329 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1330 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1331 a->a_nvals, op_ndn, op->o_tmpmemctx );
1334 rc = LDAP_NO_SUCH_ATTRIBUTE;
1337 be_entry_release_r( op, e );
1340 rc = LDAP_NO_SUCH_OBJECT;
1343 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1344 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len, op->o_tmpmemctx);
1345 g->ga_be = op->o_bd;
1346 g->ga_oc = group_oc;
1347 g->ga_at = group_at;
1349 g->ga_len = gr_ndn->bv_len;
1350 strcpy(g->ga_ndn, gr_ndn->bv_val);
1351 g->ga_next = op->o_groups;
1364 AttributeDescription *entry_at,
1370 int i, j, rc = LDAP_SUCCESS;
1371 AccessControlState acl_state = ACL_STATE_INIT;
1372 Backend *be = op->o_bd;
1374 op->o_bd = select_backend( edn, 0, 0 );
1376 if ( target && dn_match( &target->e_nname, edn ) ) {
1379 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1383 a = attr_find( e->e_attrs, entry_at );
1387 if ( op->o_conn && access_allowed( op,
1388 e, entry_at, NULL, ACL_AUTH,
1389 &acl_state ) == 0 ) {
1390 rc = LDAP_INSUFFICIENT_ACCESS;
1394 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1396 v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1397 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1398 if ( op->o_conn && access_allowed( op,
1401 ACL_AUTH, &acl_state ) == 0 ) {
1405 &a->a_nvals[i], op->o_tmpmemctx );
1406 if (v[j].bv_val ) j++;
1409 op->o_tmpfree( v, op->o_tmpmemctx );
1411 rc = LDAP_INSUFFICIENT_ACCESS;
1419 freeit: if (e != target ) {
1420 be_entry_release_r( op, e );
1428 Attribute *backend_operational(
1433 Attribute *a = NULL, **ap = &a;
1436 * If operational attributes (allegedly) are required,
1437 * and the backend supports specific operational attributes,
1438 * add them to the attribute list
1440 if ( opattrs || ( op->ors_attrs &&
1441 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1442 *ap = slap_operational_subschemaSubentry( op->o_bd );
1443 ap = &(*ap)->a_next;
1446 if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1447 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );