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 SLAPD_BDB == SLAPD_MOD_STATIC
55 #include "back-bdb/external.h"
57 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
58 #include "back-dnssrv/external.h"
60 #if SLAPD_HDB == SLAPD_MOD_STATIC
61 #include "back-hdb/external.h"
63 #if SLAPD_LDAP == SLAPD_MOD_STATIC
64 #include "back-ldap/external.h"
66 #if SLAPD_LDBM == SLAPD_MOD_STATIC
67 #include "back-ldbm/external.h"
69 #if SLAPD_META == SLAPD_MOD_STATIC
70 #include "back-meta/external.h"
72 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
73 #include "back-monitor/external.h"
75 #if SLAPD_NULL == SLAPD_MOD_STATIC
76 #include "back-null/external.h"
78 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
79 #include "back-passwd/external.h"
81 #if SLAPD_PERL == SLAPD_MOD_STATIC
82 #include "back-perl/external.h"
84 #if SLAPD_RELAY == SLAPD_MOD_STATIC
85 #include "back-relay/external.h"
87 #if SLAPD_SHELL == SLAPD_MOD_STATIC
88 #include "back-shell/external.h"
90 #if SLAPD_TCL == SLAPD_MOD_STATIC
91 #include "back-tcl/external.h"
93 #if SLAPD_SQL == SLAPD_MOD_STATIC
94 #include "back-sql/external.h"
96 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
97 #include "private/external.h"
100 static BackendInfo binfo[] = {
101 #if SLAPD_BDB == SLAPD_MOD_STATIC
102 {"bdb", bdb_initialize},
104 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
105 {"dnssrv", dnssrv_back_initialize},
107 #if SLAPD_HDB == SLAPD_MOD_STATIC
108 {"hdb", hdb_initialize},
110 #if SLAPD_LDAP == SLAPD_MOD_STATIC
111 {"ldap", ldap_back_initialize},
113 #if SLAPD_LDBM == SLAPD_MOD_STATIC
114 {"ldbm", ldbm_back_initialize},
116 #if SLAPD_META == SLAPD_MOD_STATIC
117 {"meta", meta_back_initialize},
119 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
120 {"monitor", monitor_back_initialize},
122 #if SLAPD_NULL == SLAPD_MOD_STATIC
123 {"null", null_back_initialize},
125 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
126 {"passwd", passwd_back_initialize},
128 #if SLAPD_PERL == SLAPD_MOD_STATIC
129 {"perl", perl_back_initialize},
131 #if SLAPD_RELAY == SLAPD_MOD_STATIC
132 {"relay", relay_back_initialize},
134 #if SLAPD_SHELL == SLAPD_MOD_STATIC
135 {"shell", shell_back_initialize},
137 #if SLAPD_TCL == SLAPD_MOD_STATIC
138 {"tcl", tcl_back_initialize},
140 #if SLAPD_SQL == SLAPD_MOD_STATIC
141 {"sql", sql_back_initialize},
143 /* for any private backend */
144 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
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, "backend_add: "
237 "backend type \"%s\" does not have the (mandatory)init function\n",
238 aBackendInfo->bi_type, 0, 0 );
240 Debug( LDAP_DEBUG_ANY, "backend_add: "
241 "backend type \"%s\" does not have the (mandatory)init function\n",
242 aBackendInfo->bi_type, 0, 0 );
247 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
249 LDAP_LOG( BACKEND, ERR,
250 "backend_add: initialization for type \"%s\" failed\n",
251 aBackendInfo->bi_type, 0, 0 );
253 Debug( LDAP_DEBUG_ANY,
254 "backend_add: initialization for type \"%s\" failed\n",
255 aBackendInfo->bi_type, 0, 0 );
260 /* now add the backend type to the Backend Info List */
262 BackendInfo *newBackendInfo = 0;
264 /* if backendInfo == binfo no deallocation of old backendInfo */
265 if (backendInfo == binfo) {
266 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
267 AC_MEMCPY(newBackendInfo, backendInfo,
268 sizeof(BackendInfo) * nBackendInfo);
270 newBackendInfo = ch_realloc(backendInfo,
271 sizeof(BackendInfo) * (nBackendInfo + 1));
274 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
275 sizeof(BackendInfo));
276 backendInfo = newBackendInfo;
282 /* startup a specific backend database */
283 int backend_startup_one(Backend *be)
289 be->be_pending_csn_list = (struct be_pcl *)
290 ch_calloc( 1, sizeof( struct be_pcl ));
291 build_new_dn( &be->be_context_csn, be->be_nsuffix,
292 (struct berval *)&slap_ldapsync_cn_bv, NULL );
294 LDAP_TAILQ_INIT( be->be_pending_csn_list );
297 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
298 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
301 Debug( LDAP_DEBUG_TRACE,
302 "backend_startup: starting \"%s\"\n",
303 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
306 if ( be->bd_info->bi_db_open ) {
307 rc = be->bd_info->bi_db_open( be );
310 LDAP_LOG( BACKEND, CRIT,
311 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
313 Debug( LDAP_DEBUG_ANY,
314 "backend_startup: bi_db_open failed! (%d)\n",
322 int backend_startup(Backend *be)
327 if( ! ( nBackendDB > 0 ) ) {
330 LDAP_LOG( BACKEND, INFO,
331 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
333 Debug( LDAP_DEBUG_ANY,
334 "backend_startup: %d databases to startup.\n",
341 if ( be->bd_info->bi_open ) {
342 rc = be->bd_info->bi_open( be->bd_info );
345 LDAP_LOG( BACKEND, CRIT,
346 "backend_startup: bi_open failed!\n", 0, 0, 0 );
348 Debug( LDAP_DEBUG_ANY,
349 "backend_startup: bi_open failed!\n",
357 return backend_startup_one( be );
360 /* open each backend type */
361 for( i = 0; i < nBackendInfo; i++ ) {
362 if( backendInfo[i].bi_nDB == 0) {
363 /* no database of this type, don't open */
367 if( backendInfo[i].bi_open ) {
368 rc = backendInfo[i].bi_open(
372 LDAP_LOG( BACKEND, CRIT,
373 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
375 Debug( LDAP_DEBUG_ANY,
376 "backend_startup: bi_open %d failed!\n",
384 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
385 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
386 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
388 /* open each backend database */
389 for( i = 0; i < nBackendDB; i++ ) {
390 if ( backendDB[i].be_suffix == NULL ) {
392 LDAP_LOG( BACKEND, CRIT,
393 "backend_startup: warning, database %d (%s) "
395 i, backendDB[i].bd_info->bi_type, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "backend_startup: warning, database %d (%s) "
400 i, backendDB[i].bd_info->bi_type, 0 );
403 /* append global access controls */
404 acl_append( &backendDB[i].be_acl, global_acl );
406 rc = backend_startup_one( &backendDB[i] );
411 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
414 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
415 backendDB[i].be_modify && backendDB[i].be_delete )) {
417 LDAP_LOG( BACKEND, CRIT,
418 "backend_startup: database(%d) does not support "
419 "operations required for syncrepl", i, 0, 0 );
421 Debug( LDAP_DEBUG_ANY,
422 "backend_startup: database(%d) does not support "
423 "operations required for syncrepl", i, 0, 0 );
428 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
429 si->si_be = &backendDB[i];
431 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
432 ldap_pvt_runqueue_insert( &syncrepl_rq,
433 si->si_interval, do_syncrepl, (void *) si );
434 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
442 int backend_num( Backend *be )
446 if( be == NULL ) return -1;
448 for( i = 0; i < nBackendDB; i++ ) {
449 if( be == &backendDB[i] ) return i;
454 int backend_shutdown( Backend *be )
460 /* shutdown a specific backend database */
462 if ( be->bd_info->bi_nDB == 0 ) {
463 /* no database of this type, we never opened it */
467 if ( be->bd_info->bi_db_close ) {
468 be->bd_info->bi_db_close( be );
471 if( be->bd_info->bi_close ) {
472 be->bd_info->bi_close( be->bd_info );
478 /* close each backend database */
479 for( i = 0; i < nBackendDB; i++ ) {
480 if ( backendDB[i].bd_info->bi_db_close ) {
481 backendDB[i].bd_info->bi_db_close(
487 LDAP_LOG( BACKEND, NOTICE,
488 "backend_shutdown: bi_close %s failed!\n",
489 backendDB[i].be_type, 0, 0 );
491 Debug( LDAP_DEBUG_ANY,
492 "backend_close: bi_close %s failed!\n",
493 backendDB[i].be_type, 0, 0 );
498 /* close each backend type */
499 for( i = 0; i < nBackendInfo; i++ ) {
500 if( backendInfo[i].bi_nDB == 0 ) {
501 /* no database of this type */
505 if( backendInfo[i].bi_close ) {
506 backendInfo[i].bi_close(
514 int backend_destroy(void)
519 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
521 /* destroy each backend database */
522 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
523 if ( bd->bd_info->bi_db_destroy ) {
524 bd->bd_info->bi_db_destroy( bd );
526 ber_bvarray_free( bd->be_suffix );
527 ber_bvarray_free( bd->be_nsuffix );
528 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
529 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
530 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
531 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
532 acl_destroy( bd->be_acl, global_acl );
536 /* destroy each backend type */
537 for( i = 0; i < nBackendInfo; i++ ) {
538 if( backendInfo[i].bi_destroy ) {
539 backendInfo[i].bi_destroy(
545 if (backendInfo != binfo) {
548 #endif /* SLAPD_MODULES */
556 BackendInfo* backend_info(const char *type)
560 /* search for the backend type */
561 for( i = 0; i < nBackendInfo; i++ ) {
562 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
563 return &backendInfo[i];
576 BackendInfo *bi = backend_info(type);
580 fprintf( stderr, "Unrecognized database type (%s)\n", type );
584 backendDB = (BackendDB *) ch_realloc(
586 (nBackendDB + 1) * sizeof(Backend) );
588 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
590 be = &backends[nbackends++];
593 be->be_def_limit = deflimit;
594 be->be_dfltaccess = global_default_access;
596 be->be_restrictops = global_restrictops;
597 be->be_requires = global_requires;
598 be->be_ssf_set = global_ssf_set;
600 be->be_context_csn.bv_len = 0;
601 be->be_context_csn.bv_val = NULL;
602 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
603 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
605 LDAP_STAILQ_INIT( &be->be_syncinfo );
607 /* assign a default depth limit for alias deref */
608 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
611 rc = bi->bi_db_init( be );
615 fprintf( stderr, "database init failed (%s)\n", type );
629 for ( i = 0; i < nbackends; i++ ) {
630 if ( backends[i].bd_info->bi_db_close ) {
631 (*backends[i].bd_info->bi_db_close)( &backends[i] );
643 ber_len_t len, dnlen = dn->bv_len;
646 for ( i = 0; i < nbackends; i++ ) {
647 for ( j = 0; backends[i].be_nsuffix != NULL &&
648 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
650 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
656 len = backends[i].be_nsuffix[j].bv_len;
659 /* suffix is longer than DN */
664 * input DN is normalized, so the separator check
665 * need not look at escaping
667 if ( len && len < dnlen &&
668 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
673 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
674 &dn->bv_val[dnlen-len] ) == 0 )
679 if( manageDSAit && len == dnlen &&
680 !SLAP_GLUE_SUBORDINATE( be ) ) {
697 struct berval *bvsuffix )
702 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
705 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
714 be_isroot_dn( Backend *be, struct berval *ndn )
716 if ( !ndn->bv_len ) {
720 if ( !be->be_rootndn.bv_len ) {
724 return dn_match( &be->be_rootndn, ndn );
728 be_sync_update( Operation *op )
730 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
734 be_slurp_update( Operation *op )
736 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
737 be_isupdate_dn( op->o_bd, &op->o_ndn ));
741 be_shadow_update( Operation *op )
743 return ( SLAP_SHADOW( op->o_bd ) &&
744 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
748 be_isupdate_dn( Backend *be, struct berval *ndn )
750 if ( !ndn->bv_len ) return( 0 );
752 if ( !be->be_update_ndn.bv_len ) return( 0 );
754 return dn_match( &be->be_update_ndn, ndn );
758 be_root_dn( Backend *be )
760 return &be->be_rootdn;
764 be_isroot( Operation *op )
766 return be_isroot_dn( op->o_bd, &op->o_ndn );
770 be_isroot_pw( Operation *op )
775 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
779 if( op->o_bd->be_rootpw.bv_len == 0 ) {
783 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
784 ldap_pvt_thread_mutex_lock( &passwd_mutex );
786 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
790 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
792 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
794 lutil_passwd_sasl_conn = NULL;
796 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
808 if ( op->o_bd->be_release ) {
809 /* free and release entry from backend */
810 return op->o_bd->be_release( op, e, rw );
819 backend_unbind( Operation *op, SlapReply *rs )
823 for ( i = 0; i < nbackends; i++ ) {
824 #if defined( LDAP_SLAPI )
827 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
828 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
829 rc = slapi_int_call_plugins( &backends[i],
830 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
833 * A preoperation plugin failure will abort the
837 LDAP_LOG( OPERATION, INFO,
838 "do_bind: Unbind preoperation plugin failed\n",
841 Debug(LDAP_DEBUG_TRACE,
842 "do_bind: Unbind preoperation plugin failed\n",
848 #endif /* defined( LDAP_SLAPI ) */
850 if ( backends[i].be_unbind ) {
851 op->o_bd = &backends[i];
852 (*backends[i].be_unbind)( op, rs );
855 #if defined( LDAP_SLAPI )
856 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
857 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
860 LDAP_LOG( OPERATION, INFO,
861 "do_unbind: Unbind postoperation plugins failed\n",
864 Debug(LDAP_DEBUG_TRACE,
865 "do_unbind: Unbind postoperation plugins failed\n",
869 #endif /* defined( LDAP_SLAPI ) */
876 backend_connection_init(
881 for ( i = 0; i < nbackends; i++ ) {
882 if ( backends[i].be_connection_init ) {
883 (*backends[i].be_connection_init)( &backends[i], conn);
891 backend_connection_destroy(
896 for ( i = 0; i < nbackends; i++ ) {
897 if ( backends[i].be_connection_destroy ) {
898 (*backends[i].be_connection_destroy)( &backends[i], conn);
906 backend_check_controls(
910 LDAPControl **ctrls = op->o_ctrls;
911 rs->sr_err = LDAP_SUCCESS;
914 for( ; *ctrls != NULL ; ctrls++ ) {
915 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
916 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
918 rs->sr_text = "control unavailable in context";
919 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
929 backend_check_restrictions(
932 struct berval *opdata )
934 slap_mask_t restrictops;
935 slap_mask_t requires;
943 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
947 restrictops = op->o_bd->be_restrictops;
948 requires = op->o_bd->be_requires;
949 ssf = &op->o_bd->be_ssf_set;
952 restrictops = global_restrictops;
953 requires = global_requires;
954 ssf = &global_ssf_set;
957 switch( op->o_tag ) {
959 opflag = SLAP_RESTRICT_OP_ADD;
963 opflag = SLAP_RESTRICT_OP_BIND;
966 case LDAP_REQ_COMPARE:
967 opflag = SLAP_RESTRICT_OP_COMPARE;
969 case LDAP_REQ_DELETE:
971 opflag = SLAP_RESTRICT_OP_DELETE;
973 case LDAP_REQ_EXTENDED:
974 opflag = SLAP_RESTRICT_OP_EXTENDED;
977 /* treat unspecified as a modify */
978 opflag = SLAP_RESTRICT_OP_MODIFY;
983 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
989 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
993 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
997 /* treat everything else as a modify */
998 opflag = SLAP_RESTRICT_OP_MODIFY;
1002 case LDAP_REQ_MODIFY:
1004 opflag = SLAP_RESTRICT_OP_MODIFY;
1006 case LDAP_REQ_RENAME:
1008 opflag = SLAP_RESTRICT_OP_RENAME;
1010 case LDAP_REQ_SEARCH:
1011 opflag = SLAP_RESTRICT_OP_SEARCH;
1013 case LDAP_REQ_UNBIND:
1018 rs->sr_text = "restrict operations internal error";
1019 rs->sr_err = LDAP_OTHER;
1024 /* these checks don't apply to StartTLS */
1026 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1027 if( op->o_transport_ssf < ssf->sss_transport ) {
1028 rs->sr_text = op->o_transport_ssf
1029 ? "stronger transport confidentiality required"
1030 : "transport confidentiality required";
1034 if( op->o_tls_ssf < ssf->sss_tls ) {
1035 rs->sr_text = op->o_tls_ssf
1036 ? "stronger TLS confidentiality required"
1037 : "TLS confidentiality required";
1042 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1043 /* simple bind specific check */
1044 if( op->o_ssf < ssf->sss_simple_bind ) {
1045 rs->sr_text = op->o_ssf
1046 ? "stronger confidentiality required"
1047 : "confidentiality required";
1052 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1053 /* these checks don't apply to SASL bind */
1055 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1056 rs->sr_text = op->o_sasl_ssf
1057 ? "stronger SASL confidentiality required"
1058 : "SASL confidentiality required";
1062 if( op->o_ssf < ssf->sss_ssf ) {
1063 rs->sr_text = op->o_ssf
1064 ? "stronger confidentiality required"
1065 : "confidentiality required";
1071 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1072 rs->sr_text = op->o_transport_ssf
1073 ? "stronger transport confidentiality required for update"
1074 : "transport confidentiality required for update";
1078 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1079 rs->sr_text = op->o_tls_ssf
1080 ? "stronger TLS confidentiality required for update"
1081 : "TLS confidentiality required for update";
1085 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1086 rs->sr_text = op->o_sasl_ssf
1087 ? "stronger SASL confidentiality required for update"
1088 : "SASL confidentiality required for update";
1092 if( op->o_ssf < ssf->sss_update_ssf ) {
1093 rs->sr_text = op->o_ssf
1094 ? "stronger confidentiality required for update"
1095 : "confidentiality required for update";
1099 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1100 op->o_ndn.bv_len == 0 )
1102 rs->sr_text = "modifications require authentication";
1103 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1107 #ifdef SLAP_X_LISTENER_MOD
1108 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1109 /* no "w" mode means readonly */
1110 rs->sr_text = "modifications not allowed on this listener";
1111 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1114 #endif /* SLAP_X_LISTENER_MOD */
1119 /* these checks don't apply to Bind, StartTLS, or Unbind */
1121 if( requires & SLAP_REQUIRE_STRONG ) {
1122 /* should check mechanism */
1123 if( ( op->o_transport_ssf < ssf->sss_transport
1124 && op->o_authtype == LDAP_AUTH_SIMPLE )
1125 || op->o_dn.bv_len == 0 )
1127 rs->sr_text = "strong(er) authentication required";
1128 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1133 if( requires & SLAP_REQUIRE_SASL ) {
1134 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1135 rs->sr_text = "SASL authentication required";
1136 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1141 if( requires & SLAP_REQUIRE_AUTHC ) {
1142 if( op->o_dn.bv_len == 0 ) {
1143 rs->sr_text = "authentication required";
1144 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1149 if( requires & SLAP_REQUIRE_BIND ) {
1151 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1152 version = op->o_conn->c_protocol;
1153 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1156 /* no bind has occurred */
1157 rs->sr_text = "BIND required";
1158 rs->sr_err = LDAP_OPERATIONS_ERROR;
1163 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1164 if( op->o_protocol < LDAP_VERSION3 ) {
1165 /* no bind has occurred */
1166 rs->sr_text = "operation restricted to LDAPv3 clients";
1167 rs->sr_err = LDAP_OPERATIONS_ERROR;
1172 #ifdef SLAP_X_LISTENER_MOD
1173 if ( !starttls && op->o_dn.bv_len == 0 ) {
1174 if ( op->o_conn->c_listener &&
1175 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1177 /* no "x" mode means bind required */
1178 rs->sr_text = "bind required on this listener";
1179 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1184 if ( !starttls && !updateop ) {
1185 if ( op->o_conn->c_listener &&
1186 !( op->o_conn->c_listener->sl_perms &
1187 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1189 /* no "r" mode means no read */
1190 rs->sr_text = "read not allowed on this listener";
1191 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1195 #endif /* SLAP_X_LISTENER_MOD */
1199 if( restrictops & opflag ) {
1200 if( restrictops == SLAP_RESTRICT_OP_READS ) {
1201 rs->sr_text = "read operations restricted";
1203 rs->sr_text = "operation restricted";
1205 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1209 rs->sr_err = LDAP_SUCCESS;
1213 int backend_check_referrals( Operation *op, SlapReply *rs )
1215 rs->sr_err = LDAP_SUCCESS;
1217 if( op->o_bd->be_chk_referrals ) {
1218 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1220 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1221 send_ldap_result( op, rs );
1233 AttributeDescription *at,
1241 if (op->o_bd == NULL) {
1242 rc = LDAP_NO_SUCH_OBJECT;
1243 } else if ( op->o_bd->be_fetch ) {
1244 rc = ( op->o_bd->be_fetch )( op, ndn,
1247 rc = LDAP_UNWILLING_TO_PERFORM;
1256 struct berval *gr_ndn,
1257 struct berval *op_ndn,
1258 ObjectClass *group_oc,
1259 AttributeDescription *group_at )
1265 Backend *be = op->o_bd;
1267 if ( op->o_abandon ) return SLAPD_ABANDON;
1269 op->o_bd = select_backend( gr_ndn, 0, 0 );
1271 for (g = op->o_groups; g; g=g->ga_next) {
1272 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1273 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1275 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1284 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1288 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1291 a = attr_find( e->e_attrs, group_at );
1293 /* If the attribute is a subtype of labeledURI, treat this as
1294 * a dynamic group ala groupOfURLs
1296 if (is_at_subtype( group_at->ad_type,
1297 slap_schema.si_ad_labeledURI->ad_type ) )
1301 struct berval bv, nbase;
1304 Backend *b2 = op->o_bd;
1306 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1309 op->o_bd = select_backend( op_ndn, 0, 0 );
1310 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1315 for (i=0; a->a_vals[i].bv_val; i++) {
1316 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1321 nbase.bv_val = NULL;
1322 /* host part must be empty */
1323 /* attrs and extensions parts must be empty */
1324 if (( ludp->lud_host && *ludp->lud_host ) ||
1325 ludp->lud_attrs || ludp->lud_exts )
1329 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1330 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1331 op->o_tmpmemctx ) != LDAP_SUCCESS )
1335 switch(ludp->lud_scope) {
1336 case LDAP_SCOPE_BASE:
1337 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1339 case LDAP_SCOPE_ONELEVEL:
1340 dnParent(op_ndn, &bv );
1341 if ( !dn_match( &nbase, &bv )) goto loopit;
1343 case LDAP_SCOPE_SUBTREE:
1344 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1346 #ifdef LDAP_SCOPE_SUBORDINATE
1347 case LDAP_SCOPE_SUBORDINATE:
1348 if ( dn_match( &nbase, op_ndn ) &&
1349 !dnIsSuffix(op_ndn, &nbase ))
1355 filter = str2filter_x( op, ludp->lud_filter );
1357 if ( test_filter( NULL, user, filter ) ==
1362 filter_free_x( op, filter );
1365 ldap_free_urldesc( ludp );
1366 if ( nbase.bv_val ) {
1367 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1369 if ( rc == 0 ) break;
1371 if ( user != target ) {
1372 be_entry_release_r( op, user );
1377 rc = value_find_ex( group_at,
1378 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1379 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1380 a->a_nvals, op_ndn, op->o_tmpmemctx );
1383 rc = LDAP_NO_SUCH_ATTRIBUTE;
1386 be_entry_release_r( op, e );
1389 rc = LDAP_NO_SUCH_OBJECT;
1392 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1393 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1395 g->ga_be = op->o_bd;
1396 g->ga_oc = group_oc;
1397 g->ga_at = group_at;
1399 g->ga_len = gr_ndn->bv_len;
1400 strcpy(g->ga_ndn, gr_ndn->bv_val);
1401 g->ga_next = op->o_groups;
1414 AttributeDescription *entry_at,
1419 int i, j, rc = LDAP_SUCCESS;
1420 AccessControlState acl_state = ACL_STATE_INIT;
1421 Backend *be = op->o_bd;
1423 op->o_bd = select_backend( edn, 0, 0 );
1425 if ( target && dn_match( &target->e_nname, edn ) ) {
1428 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1432 a = attr_find( e->e_attrs, entry_at );
1436 if ( op->o_conn && access_allowed( op,
1437 e, entry_at, NULL, ACL_AUTH,
1438 &acl_state ) == 0 ) {
1439 rc = LDAP_INSUFFICIENT_ACCESS;
1443 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1445 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1447 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1448 if ( op->o_conn && access_allowed( op,
1451 ACL_AUTH, &acl_state ) == 0 ) {
1455 &a->a_nvals[i], op->o_tmpmemctx );
1456 if (v[j].bv_val ) j++;
1459 op->o_tmpfree( v, op->o_tmpmemctx );
1461 rc = LDAP_INSUFFICIENT_ACCESS;
1469 freeit: if (e != target ) {
1470 be_entry_release_r( op, e );
1478 Attribute *backend_operational(
1483 Attribute *a = NULL, **ap = &a;
1486 * If operational attributes (allegedly) are required,
1487 * and the backend supports specific operational attributes,
1488 * add them to the attribute list
1490 if ( opattrs || ( op->ors_attrs &&
1491 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1492 *ap = slap_operational_subschemaSubentry( op->o_bd );
1493 ap = &(*ap)->a_next;
1496 if ( ( opattrs || op->ors_attrs ) && op->o_bd &&
1497 op->o_bd->be_operational != NULL )
1499 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );