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_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
85 #include "back-shell/external.h"
87 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
88 #include "back-tcl/external.h"
90 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
91 #include "back-sql/external.h"
93 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
94 #include "private/external.h"
97 static BackendInfo binfo[] = {
98 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
99 {"bdb", bdb_initialize},
101 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
102 {"dnssrv", dnssrv_back_initialize},
104 #if defined(SLAPD_HDB) && !defined(SLAPD_HDB_DYNAMIC)
105 {"hdb", hdb_initialize},
107 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
108 {"ldap", ldap_back_initialize},
110 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
111 {"ldbm", ldbm_back_initialize},
113 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
114 {"meta", meta_back_initialize},
116 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
117 {"monitor", monitor_back_initialize},
119 #if defined(SLAPD_NULL) && !defined(SLAPD_NULL_DYNAMIC)
120 {"null", null_back_initialize},
122 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
123 {"passwd", passwd_back_initialize},
125 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
126 {"perl", perl_back_initialize},
128 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
129 {"shell", shell_back_initialize},
131 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
132 {"tcl", tcl_back_initialize},
134 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
135 {"sql", sql_back_initialize},
137 /* for any private backend */
138 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
139 {"private", private_back_initialize},
144 int nBackendInfo = 0;
145 BackendInfo *backendInfo = NULL;
148 BackendDB *backendDB = NULL;
150 ldap_pvt_thread_pool_t syncrepl_pool;
151 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
153 int backend_init(void)
157 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
159 if((nBackendInfo != 0) || (backendInfo != NULL)) {
160 /* already initialized */
162 LDAP_LOG( BACKEND, ERR,
163 "backend_init: backend already initialized\n", 0, 0, 0 );
165 Debug( LDAP_DEBUG_ANY,
166 "backend_init: already initialized.\n", 0, 0, 0 );
172 binfo[nBackendInfo].bi_type != NULL;
175 assert( binfo[nBackendInfo].bi_init );
177 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
181 LDAP_LOG( BACKEND, INFO,
182 "backend_init: initialized for type \"%s\"\n",
183 binfo[nBackendInfo].bi_type, 0, 0 );
185 Debug( LDAP_DEBUG_ANY,
186 "backend_init: initialized for type \"%s\"\n",
187 binfo[nBackendInfo].bi_type, 0, 0 );
189 /* destroy those we've already inited */
194 if ( binfo[nBackendInfo].bi_destroy ) {
195 binfo[nBackendInfo].bi_destroy(
196 &binfo[nBackendInfo] );
203 if ( nBackendInfo > 0) {
213 LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
215 Debug( LDAP_DEBUG_ANY,
216 "backend_init: failed\n",
221 #endif /* SLAPD_MODULES */
224 int backend_add(BackendInfo *aBackendInfo)
228 if ( aBackendInfo->bi_init == NULL ) {
230 LDAP_LOG( BACKEND, ERR,
231 "backend_add: backend type \"%s\" does not have the "
232 "(mandatory)init function\n",
233 aBackendInfo->bi_type, 0, 0 );
235 Debug( LDAP_DEBUG_ANY,
236 "backend_add: backend type \"%s\" does not have the "
237 "(mandatory)init function\n",
238 aBackendInfo->bi_type, 0, 0 );
243 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
245 LDAP_LOG( BACKEND, ERR,
246 "backend_add: initialization for type \"%s\" failed\n",
247 aBackendInfo->bi_type, 0, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "backend_add: initialization for type \"%s\" failed\n",
251 aBackendInfo->bi_type, 0, 0 );
256 /* now add the backend type to the Backend Info List */
258 BackendInfo *newBackendInfo = 0;
260 /* if backendInfo == binfo no deallocation of old backendInfo */
261 if (backendInfo == binfo) {
262 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
263 AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) *
266 newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) *
269 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
270 sizeof(BackendInfo));
271 backendInfo = newBackendInfo;
278 int backend_startup(Backend *be)
283 if( ! ( nBackendDB > 0 ) ) {
286 LDAP_LOG( BACKEND, INFO,
287 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
289 Debug( LDAP_DEBUG_ANY,
290 "backend_startup: %d databases to startup.\n",
297 /* startup a specific backend database */
299 LDAP_TAILQ_INIT( &be->be_pending_csn_list );
302 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
303 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
306 Debug( LDAP_DEBUG_TRACE,
307 "backend_startup: starting \"%s\"\n",
308 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
312 if ( be->bd_info->bi_open ) {
313 rc = be->bd_info->bi_open( be->bd_info );
316 LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
318 Debug( LDAP_DEBUG_ANY,
319 "backend_startup: bi_open failed!\n",
327 if ( be->bd_info->bi_db_open ) {
328 rc = be->bd_info->bi_db_open( be );
331 LDAP_LOG( BACKEND, CRIT,
332 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
334 Debug( LDAP_DEBUG_ANY,
335 "backend_startup: bi_db_open failed! (%d)\n",
345 /* open each backend type */
346 for( i = 0; i < nBackendInfo; i++ ) {
347 if( backendInfo[i].bi_nDB == 0) {
348 /* no database of this type, don't open */
352 if( backendInfo[i].bi_open ) {
353 rc = backendInfo[i].bi_open(
357 LDAP_LOG( BACKEND, CRIT,
358 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
360 Debug( LDAP_DEBUG_ANY,
361 "backend_startup: bi_open %d failed!\n",
369 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
370 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
371 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
373 /* open each backend database */
374 for( i = 0; i < nBackendDB; i++ ) {
375 /* append global access controls */
376 acl_append( &backendDB[i].be_acl, global_acl );
378 LDAP_TAILQ_INIT( &backendDB[i].be_pending_csn_list );
380 if ( backendDB[i].be_suffix == NULL ) {
382 LDAP_LOG( BACKEND, CRIT,
383 "backend_startup: warning, database %d (%s) "
385 i, backendDB[i].bd_info->bi_type, 0 );
387 Debug( LDAP_DEBUG_ANY,
388 "backend_startup: warning, database %d (%s) "
390 i, backendDB[i].bd_info->bi_type, 0 );
394 if ( backendDB[i].bd_info->bi_db_open ) {
395 rc = backendDB[i].bd_info->bi_db_open(
399 LDAP_LOG( BACKEND, CRIT,
400 "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
402 Debug( LDAP_DEBUG_ANY,
403 "backend_startup: bi_db_open(%d) failed! (%d)\n",
410 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
413 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
414 backendDB[i].be_modify && backendDB[i].be_delete )) {
416 LDAP_LOG( BACKEND, CRIT,
417 "backend_startup: database(%d) does not support "
418 "operations required for syncrepl", i, 0, 0 );
420 Debug( LDAP_DEBUG_ANY,
421 "backend_startup: database(%d) does not support "
422 "operations required for syncrepl", i, 0, 0 );
427 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
428 si->si_be = &backendDB[i];
430 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
431 ldap_pvt_runqueue_insert( &syncrepl_rq,
432 si->si_interval, do_syncrepl, (void *) si );
433 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
441 int backend_num( Backend *be )
445 if( be == NULL ) return -1;
447 for( i = 0; i < nBackendDB; i++ ) {
448 if( be == &backendDB[i] ) return i;
453 int backend_shutdown( Backend *be )
459 /* shutdown a specific backend database */
461 if ( be->bd_info->bi_nDB == 0 ) {
462 /* no database of this type, we never opened it */
466 if ( be->bd_info->bi_db_close ) {
467 be->bd_info->bi_db_close( be );
470 if( be->bd_info->bi_close ) {
471 be->bd_info->bi_close( be->bd_info );
477 /* close each backend database */
478 for( i = 0; i < nBackendDB; i++ ) {
479 if ( backendDB[i].bd_info->bi_db_close ) {
480 backendDB[i].bd_info->bi_db_close(
486 LDAP_LOG( BACKEND, NOTICE,
487 "backend_shutdown: bi_close %s failed!\n",
488 backendDB[i].be_type, 0, 0 );
490 Debug( LDAP_DEBUG_ANY,
491 "backend_close: bi_close %s failed!\n",
492 backendDB[i].be_type, 0, 0 );
497 /* close each backend type */
498 for( i = 0; i < nBackendInfo; i++ ) {
499 if( backendInfo[i].bi_nDB == 0 ) {
500 /* no database of this type */
504 if( backendInfo[i].bi_close ) {
505 backendInfo[i].bi_close(
513 int backend_destroy(void)
518 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
520 /* destroy each backend database */
521 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
522 if ( bd->bd_info->bi_db_destroy ) {
523 bd->bd_info->bi_db_destroy( bd );
525 ber_bvarray_free( bd->be_suffix );
526 ber_bvarray_free( bd->be_nsuffix );
527 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
528 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
529 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
530 acl_destroy( bd->be_acl, global_acl );
534 /* destroy each backend type */
535 for( i = 0; i < nBackendInfo; i++ ) {
536 if( backendInfo[i].bi_destroy ) {
537 backendInfo[i].bi_destroy(
543 if (backendInfo != binfo) {
546 #endif /* SLAPD_MODULES */
554 BackendInfo* backend_info(const char *type)
558 /* search for the backend type */
559 for( i = 0; i < nBackendInfo; i++ ) {
560 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
561 return &backendInfo[i];
575 BackendInfo *bi = backend_info(type);
579 fprintf( stderr, "Unrecognized database type (%s)\n", type );
583 backendDB = (BackendDB *) ch_realloc(
585 (nBackendDB + 1) * sizeof(Backend) );
587 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
589 be = &backends[nbackends++];
592 be->be_def_limit = deflimit;
593 be->be_dfltaccess = global_default_access;
595 be->be_restrictops = global_restrictops;
596 be->be_requires = global_requires;
597 be->be_ssf_set = global_ssf_set;
599 be->be_context_csn.bv_len = 0;
600 be->be_context_csn.bv_val = NULL;
601 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
602 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
604 LDAP_STAILQ_INIT( &be->be_syncinfo );
606 /* assign a default depth limit for alias deref */
607 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
610 rc = bi->bi_db_init( be );
614 fprintf( stderr, "database init failed (%s)\n", type );
628 for ( i = 0; i < nbackends; i++ ) {
629 if ( backends[i].bd_info->bi_db_close ) {
630 (*backends[i].bd_info->bi_db_close)( &backends[i] );
642 ber_len_t len, dnlen = dn->bv_len;
645 for ( i = 0; i < nbackends; i++ ) {
646 for ( j = 0; backends[i].be_nsuffix != NULL &&
647 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
649 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
655 len = backends[i].be_nsuffix[j].bv_len;
658 /* suffix is longer than DN */
663 * input DN is normalized, so the separator check
664 * need not look at escaping
666 if ( len && len < dnlen &&
667 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
672 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
673 &dn->bv_val[dnlen-len] ) == 0 )
678 if( manageDSAit && len == dnlen &&
679 !SLAP_GLUE_SUBORDINATE( be ) ) {
696 struct berval *bvsuffix
701 for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
702 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
711 be_isroot( Backend *be, struct berval *ndn )
713 if ( !ndn->bv_len ) {
717 if ( !be->be_rootndn.bv_len ) {
721 return dn_match( &be->be_rootndn, ndn );
725 be_isupdate( Backend *be, struct berval *ndn )
727 if ( !ndn->bv_len ) {
731 if ( !be->be_update_ndn.bv_len ) {
735 return dn_match( &be->be_update_ndn, ndn );
739 be_root_dn( Backend *be )
741 return &be->be_rootdn;
745 be_isroot_pw( Operation *op )
750 if ( ! be_isroot( op->o_bd, &op->o_req_ndn ) ) {
754 if( op->o_bd->be_rootpw.bv_len == 0 ) {
758 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
759 ldap_pvt_thread_mutex_lock( &passwd_mutex );
761 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
765 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
767 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
769 lutil_passwd_sasl_conn = NULL;
771 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
783 if ( op->o_bd->be_release ) {
784 /* free and release entry from backend */
785 return op->o_bd->be_release( op, e, rw );
794 backend_unbind( Operation *op, SlapReply *rs )
798 for ( i = 0; i < nbackends; i++ ) {
799 #if defined( LDAP_SLAPI )
802 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
803 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
804 rc = slapi_int_call_plugins( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
805 (Slapi_PBlock *)op->o_pb );
808 * A preoperation plugin failure will abort the
812 LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
813 "failed\n", 0, 0, 0);
815 Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
816 "failed.\n", 0, 0, 0);
821 #endif /* defined( LDAP_SLAPI ) */
823 if ( backends[i].be_unbind ) {
824 op->o_bd = &backends[i];
825 (*backends[i].be_unbind)( op, rs );
828 #if defined( LDAP_SLAPI )
829 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
830 (Slapi_PBlock *)op->o_pb ) < 0 ) {
832 LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
833 "failed\n", 0, 0, 0);
835 Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
836 "failed.\n", 0, 0, 0);
839 #endif /* defined( LDAP_SLAPI ) */
846 backend_connection_init(
852 for ( i = 0; i < nbackends; i++ ) {
853 if ( backends[i].be_connection_init ) {
854 (*backends[i].be_connection_init)( &backends[i], conn);
862 backend_connection_destroy(
868 for ( i = 0; i < nbackends; i++ ) {
869 if ( backends[i].be_connection_destroy ) {
870 (*backends[i].be_connection_destroy)( &backends[i], conn);
878 backend_check_controls(
882 LDAPControl **ctrls = op->o_ctrls;
883 rs->sr_err = LDAP_SUCCESS;
886 for( ; *ctrls != NULL ; ctrls++ ) {
887 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
888 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
890 rs->sr_text = "control unavailable in context";
891 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
901 backend_check_restrictions(
904 struct berval *opdata )
906 slap_mask_t restrictops;
907 slap_mask_t requires;
915 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
919 restrictops = op->o_bd->be_restrictops;
920 requires = op->o_bd->be_requires;
921 ssf = &op->o_bd->be_ssf_set;
924 restrictops = global_restrictops;
925 requires = global_requires;
926 ssf = &global_ssf_set;
929 switch( op->o_tag ) {
931 opflag = SLAP_RESTRICT_OP_ADD;
935 opflag = SLAP_RESTRICT_OP_BIND;
938 case LDAP_REQ_COMPARE:
939 opflag = SLAP_RESTRICT_OP_COMPARE;
941 case LDAP_REQ_DELETE:
943 opflag = SLAP_RESTRICT_OP_DELETE;
945 case LDAP_REQ_EXTENDED:
946 opflag = SLAP_RESTRICT_OP_EXTENDED;
949 /* treat unspecified as a modify */
950 opflag = SLAP_RESTRICT_OP_MODIFY;
955 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
961 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
965 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
969 /* treat everything else as a modify */
970 opflag = SLAP_RESTRICT_OP_MODIFY;
974 case LDAP_REQ_MODIFY:
976 opflag = SLAP_RESTRICT_OP_MODIFY;
978 case LDAP_REQ_RENAME:
980 opflag = SLAP_RESTRICT_OP_RENAME;
982 case LDAP_REQ_SEARCH:
983 opflag = SLAP_RESTRICT_OP_SEARCH;
985 case LDAP_REQ_UNBIND:
990 rs->sr_text = "restrict operations internal error";
991 rs->sr_err = LDAP_OTHER;
996 /* these checks don't apply to StartTLS */
998 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
999 if( op->o_transport_ssf < ssf->sss_transport ) {
1000 rs->sr_text = "transport confidentiality required";
1004 if( op->o_tls_ssf < ssf->sss_tls ) {
1005 rs->sr_text = "TLS confidentiality required";
1010 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1011 /* simple bind specific check */
1012 if( op->o_ssf < ssf->sss_simple_bind ) {
1013 rs->sr_text = "confidentiality required";
1018 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1019 /* these checks don't apply to SASL bind */
1021 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1022 rs->sr_text = "SASL confidentiality required";
1026 if( op->o_ssf < ssf->sss_ssf ) {
1027 rs->sr_text = "confidentiality required";
1033 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1034 rs->sr_text = "transport update confidentiality required";
1038 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1039 rs->sr_text = "TLS update confidentiality required";
1043 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1044 rs->sr_text = "SASL update confidentiality required";
1048 if( op->o_ssf < ssf->sss_update_ssf ) {
1049 rs->sr_text = "update confidentiality required";
1053 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1054 op->o_ndn.bv_len == 0 )
1056 rs->sr_text = "modifications require authentication";
1057 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1061 #ifdef SLAP_X_LISTENER_MOD
1062 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1063 /* no "w" mode means readonly */
1064 rs->sr_text = "modifications not allowed on this listener";
1065 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1068 #endif /* SLAP_X_LISTENER_MOD */
1073 /* these checks don't apply to Bind, StartTLS, or Unbind */
1075 if( requires & SLAP_REQUIRE_STRONG ) {
1076 /* should check mechanism */
1077 if( ( op->o_transport_ssf < ssf->sss_transport
1078 && op->o_authtype == LDAP_AUTH_SIMPLE )
1079 || op->o_dn.bv_len == 0 )
1081 rs->sr_text = "strong(er) authentication required";
1082 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1087 if( requires & SLAP_REQUIRE_SASL ) {
1088 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1089 rs->sr_text = "SASL authentication required";
1090 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1095 if( requires & SLAP_REQUIRE_AUTHC ) {
1096 if( op->o_dn.bv_len == 0 ) {
1097 rs->sr_text = "authentication required";
1098 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1103 if( requires & SLAP_REQUIRE_BIND ) {
1105 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1106 version = op->o_conn->c_protocol;
1107 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1110 /* no bind has occurred */
1111 rs->sr_text = "BIND required";
1112 rs->sr_err = LDAP_OPERATIONS_ERROR;
1117 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1118 if( op->o_protocol < LDAP_VERSION3 ) {
1119 /* no bind has occurred */
1120 rs->sr_text = "operation restricted to LDAPv3 clients";
1121 rs->sr_err = LDAP_OPERATIONS_ERROR;
1126 #ifdef SLAP_X_LISTENER_MOD
1127 if ( !starttls && op->o_dn.bv_len == 0 ) {
1128 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & S_IXOTH ) ) {
1129 /* no "x" mode means bind required */
1130 rs->sr_text = "bind required on this listener";
1131 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1136 if ( !starttls && !updateop ) {
1137 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH ) ) ) {
1138 /* no "r" mode means no read */
1139 rs->sr_text = "read not allowed on this listener";
1140 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1144 #endif /* SLAP_X_LISTENER_MOD */
1148 if( restrictops & opflag ) {
1149 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1150 rs->sr_text = "read operations restricted";
1152 rs->sr_text = "operation restricted";
1154 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1158 rs->sr_err = LDAP_SUCCESS;
1162 int backend_check_referrals( Operation *op, SlapReply *rs )
1164 rs->sr_err = LDAP_SUCCESS;
1166 if( op->o_bd->be_chk_referrals ) {
1167 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1169 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1170 send_ldap_result( op, rs );
1182 AttributeDescription *at,
1190 if (op->o_bd == NULL) {
1191 rc = LDAP_NO_SUCH_OBJECT;
1192 } else if ( op->o_bd->be_fetch ) {
1193 rc = ( op->o_bd->be_fetch )( op, ndn,
1196 rc = LDAP_UNWILLING_TO_PERFORM;
1205 struct berval *gr_ndn,
1206 struct berval *op_ndn,
1207 ObjectClass *group_oc,
1208 AttributeDescription *group_at
1215 Backend *be = op->o_bd;
1217 if ( op->o_abandon ) return SLAPD_ABANDON;
1219 op->o_bd = select_backend( gr_ndn, 0, 0 );
1221 for (g = op->o_groups; g; g=g->ga_next) {
1222 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1223 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1225 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1234 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1238 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1241 a = attr_find( e->e_attrs, group_at );
1243 /* If the attribute is a subtype of labeledURI, treat this as
1244 * a dynamic group ala groupOfURLs
1246 if (is_at_subtype( group_at->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1249 struct berval bv, nbase;
1252 Backend *b2 = op->o_bd;
1254 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1257 op->o_bd = select_backend( op_ndn, 0, 0 );
1258 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1263 for (i=0; a->a_vals[i].bv_val; i++) {
1264 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) != LDAP_SUCCESS )
1266 nbase.bv_val = NULL;
1267 /* host part must be empty */
1268 /* attrs and extensions parts must be empty */
1269 if (( ludp->lud_host && *ludp->lud_host )
1270 || ludp->lud_attrs || ludp->lud_exts )
1272 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1273 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase, op->o_tmpmemctx ) != LDAP_SUCCESS )
1275 switch(ludp->lud_scope) {
1276 case LDAP_SCOPE_BASE:
1277 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1279 case LDAP_SCOPE_ONELEVEL:
1280 dnParent(op_ndn, &bv );
1281 if ( !dn_match( &nbase, &bv )) goto loopit;
1283 case LDAP_SCOPE_SUBTREE:
1284 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1286 case LDAP_SCOPE_SUBORDINATE:
1287 if ( dn_match( &nbase, op_ndn ) &&
1288 !dnIsSuffix(op_ndn, &nbase ))
1293 filter = str2filter_x( op, ludp->lud_filter );
1295 if ( test_filter( NULL, user, filter ) == LDAP_COMPARE_TRUE )
1299 filter_free_x( op, filter );
1302 ldap_free_urldesc( ludp );
1303 if ( nbase.bv_val ) {
1304 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1306 if ( rc == 0 ) break;
1308 if ( user != target ) {
1309 be_entry_release_r( op, user );
1314 rc = value_find_ex( group_at,
1315 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1316 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1317 a->a_nvals, op_ndn, op->o_tmpmemctx );
1320 rc = LDAP_NO_SUCH_ATTRIBUTE;
1323 be_entry_release_r( op, e );
1326 rc = LDAP_NO_SUCH_OBJECT;
1329 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1330 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len, op->o_tmpmemctx);
1331 g->ga_be = op->o_bd;
1332 g->ga_oc = group_oc;
1333 g->ga_at = group_at;
1335 g->ga_len = gr_ndn->bv_len;
1336 strcpy(g->ga_ndn, gr_ndn->bv_val);
1337 g->ga_next = op->o_groups;
1350 AttributeDescription *entry_at,
1356 int i, j, rc = LDAP_SUCCESS;
1357 AccessControlState acl_state = ACL_STATE_INIT;
1358 Backend *be = op->o_bd;
1360 op->o_bd = select_backend( edn, 0, 0 );
1362 if ( target && dn_match( &target->e_nname, edn ) ) {
1365 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1369 a = attr_find( e->e_attrs, entry_at );
1373 if ( op->o_conn && access_allowed( op,
1374 e, entry_at, NULL, ACL_AUTH,
1375 &acl_state ) == 0 ) {
1376 rc = LDAP_INSUFFICIENT_ACCESS;
1380 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1382 v = op->o_tmpalloc( sizeof(struct berval) * (i+1), op->o_tmpmemctx );
1383 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1384 if ( op->o_conn && access_allowed( op,
1387 ACL_AUTH, &acl_state ) == 0 ) {
1391 &a->a_nvals[i], op->o_tmpmemctx );
1392 if (v[j].bv_val ) j++;
1395 op->o_tmpfree( v, op->o_tmpmemctx );
1397 rc = LDAP_INSUFFICIENT_ACCESS;
1405 freeit: if (e != target ) {
1406 be_entry_release_r( op, e );
1414 Attribute *backend_operational(
1419 Attribute *a = NULL, **ap = &a;
1422 * If operational attributes (allegedly) are required,
1423 * and the backend supports specific operational attributes,
1424 * add them to the attribute list
1426 if ( opattrs || ( op->ors_attrs &&
1427 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1428 *ap = slap_operational_subschemaSubentry( op->o_bd );
1429 ap = &(*ap)->a_next;
1432 if ( ( opattrs || op->ors_attrs ) && op->o_bd && op->o_bd->be_operational != NULL ) {
1433 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );