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-2005 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"
45 static void init_group_pblock( Operation *op, Entry *target,
46 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
47 static int call_group_preop_plugins( Operation *op );
48 static void call_group_postop_plugins( Operation *op );
49 #endif /* LDAP_SLAPI */
52 * If a module is configured as dynamic, its header should not
53 * get included into slapd. While this is a general rule and does
54 * not have much of an effect in UNIX, this rule should be adhered
55 * to for Windows, where dynamic object code should not be implicitly
56 * imported into slapd without appropriate __declspec(dllimport) directives.
60 BackendInfo *backendInfo = NULL;
63 BackendDB *backendDB = NULL;
65 ldap_pvt_thread_pool_t syncrepl_pool;
66 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
69 backend_init_controls( BackendInfo *bi )
71 if ( bi->bi_controls ) {
74 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
77 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
78 == LDAP_CONTROL_NOT_FOUND )
80 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
87 bi->bi_ctrls[ cid ] = 1;
94 int backend_init(void)
98 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
100 if((nBackendInfo != 0) || (backendInfo != NULL)) {
101 /* already initialized */
102 Debug( LDAP_DEBUG_ANY,
103 "backend_init: already initialized\n", 0, 0, 0 );
108 slap_binfo[nBackendInfo].bi_type != NULL;
111 assert( slap_binfo[nBackendInfo].bi_init );
113 rc = slap_binfo[nBackendInfo].bi_init( &slap_binfo[nBackendInfo] );
116 Debug( LDAP_DEBUG_ANY,
117 "backend_init: initialized for type \"%s\"\n",
118 slap_binfo[nBackendInfo].bi_type, 0, 0 );
119 /* destroy those we've already inited */
124 if ( slap_binfo[nBackendInfo].bi_destroy ) {
125 slap_binfo[nBackendInfo].bi_destroy(
126 &slap_binfo[nBackendInfo] );
133 if ( nBackendInfo > 0) {
134 backendInfo = slap_binfo;
142 Debug( LDAP_DEBUG_ANY,
143 "backend_init: failed\n",
147 #endif /* SLAPD_MODULES */
150 int backend_add(BackendInfo *aBackendInfo)
154 if ( aBackendInfo->bi_init == NULL ) {
155 Debug( LDAP_DEBUG_ANY, "backend_add: "
156 "backend type \"%s\" does not have the (mandatory)init function\n",
157 aBackendInfo->bi_type, 0, 0 );
161 rc = aBackendInfo->bi_init(aBackendInfo);
163 Debug( LDAP_DEBUG_ANY,
164 "backend_add: initialization for type \"%s\" failed\n",
165 aBackendInfo->bi_type, 0, 0 );
169 (void)backend_init_controls( aBackendInfo );
171 /* now add the backend type to the Backend Info List */
173 BackendInfo *newBackendInfo = 0;
175 /* if backendInfo == slap_binfo no deallocation of old backendInfo */
176 if (backendInfo == slap_binfo) {
177 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
178 AC_MEMCPY(newBackendInfo, backendInfo,
179 sizeof(BackendInfo) * nBackendInfo);
181 newBackendInfo = ch_realloc(backendInfo,
182 sizeof(BackendInfo) * (nBackendInfo + 1));
185 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
186 sizeof(BackendInfo));
187 backendInfo = newBackendInfo;
193 /* startup a specific backend database */
194 int backend_startup_one(Backend *be)
197 BackendInfo *bi = be->bd_info;
201 be->be_pending_csn_list = (struct be_pcl *)
202 ch_calloc( 1, sizeof( struct be_pcl ));
204 LDAP_TAILQ_INIT( be->be_pending_csn_list );
206 /* back-relay takes care of itself; so may do other */
207 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
208 if ( overlay_is_over( be ) ) {
209 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
212 if ( bi->bi_controls ) {
213 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls, sizeof( be->be_ctrls ) );
216 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
219 Debug( LDAP_DEBUG_TRACE,
220 "backend_startup_one: starting \"%s\"\n",
221 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
223 if ( be->bd_info->bi_db_open ) {
224 rc = be->bd_info->bi_db_open( be );
226 Debug( LDAP_DEBUG_ANY,
227 "backend_startup_one: bi_db_open failed! (%d)\n",
232 /* back-relay takes care of itself; so may do other */
234 if ( overlay_is_over( be ) ) {
235 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
238 if ( bi->bi_controls ) {
239 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
240 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls, sizeof( be->be_ctrls ) );
241 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
246 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
247 if ( bi->bi_ctrls[ i ] ) {
248 be->be_ctrls[ i ] = 1;
257 int backend_startup(Backend *be)
262 if( ! ( nBackendDB > 0 ) ) {
264 Debug( LDAP_DEBUG_ANY,
265 "backend_startup: %d databases to startup.\n",
271 if ( be->bd_info->bi_open ) {
272 rc = be->bd_info->bi_open( be->bd_info );
274 Debug( LDAP_DEBUG_ANY,
275 "backend_startup: bi_open failed!\n",
282 return backend_startup_one( be );
285 /* open frontend, if required */
286 if ( frontendDB->bd_info->bi_db_open ) {
287 rc = frontendDB->bd_info->bi_db_open( frontendDB );
289 Debug( LDAP_DEBUG_ANY,
290 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
296 /* open each backend type */
297 for( i = 0; i < nBackendInfo; i++ ) {
298 if( backendInfo[i].bi_nDB == 0) {
299 /* no database of this type, don't open */
303 if( backendInfo[i].bi_open ) {
304 rc = backendInfo[i].bi_open( &backendInfo[i] );
306 Debug( LDAP_DEBUG_ANY,
307 "backend_startup: bi_open %d failed!\n",
313 (void)backend_init_controls( &backendInfo[i] );
316 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
317 LDAP_STAILQ_INIT( &slapd_rq.task_list );
318 LDAP_STAILQ_INIT( &slapd_rq.run_list );
320 /* open each backend database */
321 for( i = 0; i < nBackendDB; i++ ) {
322 if ( backendDB[i].be_suffix == NULL ) {
323 Debug( LDAP_DEBUG_ANY,
324 "backend_startup: warning, database %d (%s) "
326 i, backendDB[i].bd_info->bi_type, 0 );
328 /* append global access controls */
329 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
331 rc = backend_startup_one( &backendDB[i] );
336 if ( backendDB[i].be_syncinfo ) {
339 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
340 backendDB[i].be_modify && backendDB[i].be_delete )) {
341 Debug( LDAP_DEBUG_ANY,
342 "backend_startup: database(%d) does not support "
343 "operations required for syncrepl", i, 0, 0 );
348 si = backendDB[i].be_syncinfo;
349 si->si_be = &backendDB[i];
351 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
352 ldap_pvt_runqueue_insert( &slapd_rq,
353 si->si_interval, do_syncrepl, (void *) si );
354 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
362 int backend_num( Backend *be )
366 if( be == NULL ) return -1;
368 for( i = 0; i < nBackendDB; i++ ) {
369 if( be == &backendDB[i] ) return i;
374 int backend_shutdown( Backend *be )
380 /* shutdown a specific backend database */
382 if ( be->bd_info->bi_nDB == 0 ) {
383 /* no database of this type, we never opened it */
387 if ( be->bd_info->bi_db_close ) {
388 be->bd_info->bi_db_close( be );
391 if( be->bd_info->bi_close ) {
392 be->bd_info->bi_close( be->bd_info );
398 /* close each backend database */
399 for( i = 0; i < nBackendDB; i++ ) {
400 if ( backendDB[i].bd_info->bi_db_close ) {
401 backendDB[i].bd_info->bi_db_close(
406 Debug( LDAP_DEBUG_ANY,
407 "backend_close: bi_db_close %s failed!\n",
408 backendDB[i].be_type, 0, 0 );
412 /* close each backend type */
413 for( i = 0; i < nBackendInfo; i++ ) {
414 if( backendInfo[i].bi_nDB == 0 ) {
415 /* no database of this type */
419 if( backendInfo[i].bi_close ) {
420 backendInfo[i].bi_close(
425 /* close frontend, if required */
426 if ( frontendDB->bd_info->bi_db_close ) {
427 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
429 Debug( LDAP_DEBUG_ANY,
430 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
438 int backend_destroy(void)
442 struct slap_csn_entry *csne;
444 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
446 /* destroy each backend database */
447 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
449 if ( bd->be_syncinfo ) {
450 syncinfo_free( bd->be_syncinfo );
453 if ( bd->be_pending_csn_list ) {
454 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
456 struct slap_csn_entry *tmp_csne = csne;
458 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
459 ch_free( csne->ce_csn.bv_val );
460 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
465 if ( bd->bd_info->bi_db_destroy ) {
466 bd->bd_info->bi_db_destroy( bd );
468 ber_bvarray_free( bd->be_suffix );
469 ber_bvarray_free( bd->be_nsuffix );
470 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
471 free( bd->be_rootdn.bv_val );
473 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
474 free( bd->be_rootndn.bv_val );
476 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
477 free( bd->be_rootpw.bv_val );
479 acl_destroy( bd->be_acl, frontendDB->be_acl );
483 /* destroy each backend type */
484 for( i = 0; i < nBackendInfo; i++ ) {
485 if( backendInfo[i].bi_destroy ) {
486 backendInfo[i].bi_destroy(
492 if (backendInfo != slap_binfo) {
495 #endif /* SLAPD_MODULES */
500 /* destroy frontend database */
503 if ( bd->bd_info->bi_db_destroy ) {
504 bd->bd_info->bi_db_destroy( bd );
506 ber_bvarray_free( bd->be_suffix );
507 ber_bvarray_free( bd->be_nsuffix );
508 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
509 free( bd->be_rootdn.bv_val );
511 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
512 free( bd->be_rootndn.bv_val );
514 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
515 free( bd->be_rootpw.bv_val );
517 acl_destroy( bd->be_acl, frontendDB->be_acl );
523 BackendInfo* backend_info(const char *type)
527 /* search for the backend type */
528 for( i = 0; i < nBackendInfo; i++ ) {
529 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
530 return &backendInfo[i];
543 BackendInfo *bi = backend_info(type);
547 fprintf( stderr, "Unrecognized database type (%s)\n", type );
553 backendDB = (BackendDB *) ch_realloc(
555 (nBackendDB + 1) * sizeof(Backend) );
557 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
559 /* did realloc move our table? if so, fix up dependent pointers */
560 if ( be != backendDB ) {
562 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
563 be->be_pcl_mutexp = &be->be_pcl_mutex;
567 be = &backends[nbackends++];
571 be->be_def_limit = frontendDB->be_def_limit;
572 be->be_dfltaccess = frontendDB->be_dfltaccess;
574 be->be_restrictops = frontendDB->be_restrictops;
575 be->be_requires = frontendDB->be_requires;
576 be->be_ssf_set = frontendDB->be_ssf_set;
578 be->be_pcl_mutexp = &be->be_pcl_mutex;
579 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
581 /* assign a default depth limit for alias deref */
582 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
584 if ( bi->bi_db_init ) {
585 rc = bi->bi_db_init( be );
589 fprintf( stderr, "database init failed (%s)\n", type );
603 for ( i = 0; i < nbackends; i++ ) {
604 if ( backends[i].bd_info->bi_db_close ) {
605 (*backends[i].bd_info->bi_db_close)( &backends[i] );
609 if ( frontendDB->bd_info->bi_db_close ) {
610 (*frontendDB->bd_info->bi_db_close)( frontendDB );
622 ber_len_t len, dnlen = dn->bv_len;
625 for ( i = 0; i < nbackends; i++ ) {
626 if ( backends[i].be_nsuffix == NULL ) {
630 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
632 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
638 len = backends[i].be_nsuffix[j].bv_len;
641 /* suffix is longer than DN */
646 * input DN is normalized, so the separator check
647 * need not look at escaping
649 if ( len && len < dnlen &&
650 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
655 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
656 &dn->bv_val[dnlen-len] ) == 0 )
661 if( manageDSAit && len == dnlen &&
662 !SLAP_GLUE_SUBORDINATE( be ) ) {
679 struct berval *bvsuffix )
683 if ( be->be_nsuffix == NULL ) {
687 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
688 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
697 be_isroot_dn( Backend *be, struct berval *ndn )
699 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
703 return dn_match( &be->be_rootndn, ndn );
707 be_slurp_update( Operation *op )
709 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
710 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
714 be_shadow_update( Operation *op )
716 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
717 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
721 be_isupdate_dn( Backend *be, struct berval *ndn )
723 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
727 return dn_match( &be->be_update_ndn, ndn );
731 be_root_dn( Backend *be )
733 return &be->be_rootdn;
737 be_isroot( Operation *op )
739 return be_isroot_dn( op->o_bd, &op->o_ndn );
743 be_isroot_pw( Operation *op )
747 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
751 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
755 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
756 ldap_pvt_thread_mutex_lock( &passwd_mutex );
758 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
762 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
764 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
766 lutil_passwd_sasl_conn = NULL;
768 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
780 if ( op->o_bd->be_release ) {
781 /* free and release entry from backend */
782 return op->o_bd->be_release( op, e, rw );
791 backend_unbind( Operation *op, SlapReply *rs )
795 for ( i = 0; i < nbackends; i++ ) {
796 #if defined( LDAP_SLAPI )
799 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
800 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
801 rc = slapi_int_call_plugins( &backends[i],
802 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
805 * A preoperation plugin failure will abort the
808 Debug(LDAP_DEBUG_TRACE,
809 "do_bind: Unbind preoperation plugin failed\n",
814 #endif /* defined( LDAP_SLAPI ) */
816 if ( backends[i].be_unbind ) {
817 op->o_bd = &backends[i];
818 (*backends[i].be_unbind)( op, rs );
821 #if defined( LDAP_SLAPI )
822 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
823 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
825 Debug(LDAP_DEBUG_TRACE,
826 "do_unbind: Unbind postoperation plugins failed\n",
829 #endif /* defined( LDAP_SLAPI ) */
836 backend_connection_init(
841 for ( i = 0; i < nbackends; i++ ) {
842 if ( backends[i].be_connection_init ) {
843 (*backends[i].be_connection_init)( &backends[i], conn);
851 backend_connection_destroy(
856 for ( i = 0; i < nbackends; i++ ) {
857 if ( backends[i].be_connection_destroy ) {
858 (*backends[i].be_connection_destroy)( &backends[i], conn);
866 backend_check_controls(
870 LDAPControl **ctrls = op->o_ctrls;
871 rs->sr_err = LDAP_SUCCESS;
874 for( ; *ctrls != NULL ; ctrls++ ) {
876 if( slap_find_control_id( (*ctrls)->ldctl_oid, &cid ) ==
877 LDAP_CONTROL_NOT_FOUND )
879 /* unrecognized control */
880 if ( (*ctrls)->ldctl_iscritical ) {
881 /* should not be reachable */
882 Debug( LDAP_DEBUG_ANY,
883 "backend_check_controls: unrecognized control: %s\n",
884 (*ctrls)->ldctl_oid, 0, 0 );
888 } else if ( !slap_global_control( op, (*ctrls)->ldctl_oid ) &&
889 !op->o_bd->be_ctrls[ cid ] )
891 /* Per RFC 2251 (and LDAPBIS discussions), if the control
892 * is recognized and appropriate for the operation (which
893 * we've already verified), then the server should make
894 * use of the control when performing the operation.
896 * Here we find that operation extended by the control
897 * is not unavailable in a particular context, hence the
898 * return of unwillingToPerform.
900 rs->sr_text = "control unavailable in context";
901 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
911 backend_check_restrictions(
914 struct berval *opdata )
916 slap_mask_t restrictops;
917 slap_mask_t requires;
919 slap_mask_t exopflag = 0;
926 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
930 restrictops = op->o_bd->be_restrictops;
931 requires = op->o_bd->be_requires;
932 ssf = &op->o_bd->be_ssf_set;
935 restrictops = frontendDB->be_restrictops;
936 requires = frontendDB->be_requires;
937 ssf = &frontendDB->be_ssf_set;
940 switch( op->o_tag ) {
942 opflag = SLAP_RESTRICT_OP_ADD;
946 opflag = SLAP_RESTRICT_OP_BIND;
949 case LDAP_REQ_COMPARE:
950 opflag = SLAP_RESTRICT_OP_COMPARE;
952 case LDAP_REQ_DELETE:
954 opflag = SLAP_RESTRICT_OP_DELETE;
956 case LDAP_REQ_EXTENDED:
957 opflag = SLAP_RESTRICT_OP_EXTENDED;
960 /* treat unspecified as a modify */
961 opflag = SLAP_RESTRICT_OP_MODIFY;
966 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
969 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
973 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
974 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
978 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
979 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
983 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
984 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
989 /* treat everything else as a modify */
990 opflag = SLAP_RESTRICT_OP_MODIFY;
994 case LDAP_REQ_MODIFY:
996 opflag = SLAP_RESTRICT_OP_MODIFY;
998 case LDAP_REQ_RENAME:
1000 opflag = SLAP_RESTRICT_OP_RENAME;
1002 case LDAP_REQ_SEARCH:
1003 opflag = SLAP_RESTRICT_OP_SEARCH;
1005 case LDAP_REQ_UNBIND:
1010 rs->sr_text = "restrict operations internal error";
1011 rs->sr_err = LDAP_OTHER;
1016 /* these checks don't apply to StartTLS */
1018 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1019 if( op->o_transport_ssf < ssf->sss_transport ) {
1020 rs->sr_text = op->o_transport_ssf
1021 ? "stronger transport confidentiality required"
1022 : "transport confidentiality required";
1026 if( op->o_tls_ssf < ssf->sss_tls ) {
1027 rs->sr_text = op->o_tls_ssf
1028 ? "stronger TLS confidentiality required"
1029 : "TLS confidentiality required";
1034 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1035 /* simple bind specific check */
1036 if( op->o_ssf < ssf->sss_simple_bind ) {
1037 rs->sr_text = op->o_ssf
1038 ? "stronger confidentiality required"
1039 : "confidentiality required";
1044 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1045 /* these checks don't apply to SASL bind */
1047 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1048 rs->sr_text = op->o_sasl_ssf
1049 ? "stronger SASL confidentiality required"
1050 : "SASL confidentiality required";
1054 if( op->o_ssf < ssf->sss_ssf ) {
1055 rs->sr_text = op->o_ssf
1056 ? "stronger confidentiality required"
1057 : "confidentiality required";
1063 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1064 rs->sr_text = op->o_transport_ssf
1065 ? "stronger transport confidentiality required for update"
1066 : "transport confidentiality required for update";
1070 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1071 rs->sr_text = op->o_tls_ssf
1072 ? "stronger TLS confidentiality required for update"
1073 : "TLS confidentiality required for update";
1077 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1078 rs->sr_text = op->o_sasl_ssf
1079 ? "stronger SASL confidentiality required for update"
1080 : "SASL confidentiality required for update";
1084 if( op->o_ssf < ssf->sss_update_ssf ) {
1085 rs->sr_text = op->o_ssf
1086 ? "stronger confidentiality required for update"
1087 : "confidentiality required for update";
1091 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1092 BER_BVISEMPTY( &op->o_ndn ) )
1094 rs->sr_text = "modifications require authentication";
1095 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1099 #ifdef SLAP_X_LISTENER_MOD
1100 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1101 /* no "w" mode means readonly */
1102 rs->sr_text = "modifications not allowed on this listener";
1103 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1106 #endif /* SLAP_X_LISTENER_MOD */
1111 /* these checks don't apply to Bind, StartTLS, or Unbind */
1113 if( requires & SLAP_REQUIRE_STRONG ) {
1114 /* should check mechanism */
1115 if( ( op->o_transport_ssf < ssf->sss_transport
1116 && op->o_authtype == LDAP_AUTH_SIMPLE )
1117 || BER_BVISEMPTY( &op->o_dn ) )
1119 rs->sr_text = "strong(er) authentication required";
1120 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1125 if( requires & SLAP_REQUIRE_SASL ) {
1126 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1127 rs->sr_text = "SASL authentication required";
1128 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1133 if( requires & SLAP_REQUIRE_AUTHC ) {
1134 if( BER_BVISEMPTY( &op->o_dn ) ) {
1135 rs->sr_text = "authentication required";
1136 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1141 if( requires & SLAP_REQUIRE_BIND ) {
1143 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1144 version = op->o_conn->c_protocol;
1145 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1148 /* no bind has occurred */
1149 rs->sr_text = "BIND required";
1150 rs->sr_err = LDAP_OPERATIONS_ERROR;
1155 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1156 if( op->o_protocol < LDAP_VERSION3 ) {
1157 /* no bind has occurred */
1158 rs->sr_text = "operation restricted to LDAPv3 clients";
1159 rs->sr_err = LDAP_OPERATIONS_ERROR;
1164 #ifdef SLAP_X_LISTENER_MOD
1165 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1166 if ( op->o_conn->c_listener &&
1167 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1169 /* no "x" mode means bind required */
1170 rs->sr_text = "bind required on this listener";
1171 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1176 if ( !starttls && !updateop ) {
1177 if ( op->o_conn->c_listener &&
1178 !( op->o_conn->c_listener->sl_perms &
1179 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1181 /* no "r" mode means no read */
1182 rs->sr_text = "read not allowed on this listener";
1183 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1187 #endif /* SLAP_X_LISTENER_MOD */
1191 if( ( restrictops & opflag )
1192 || ( exopflag && ( restrictops & exopflag ) ) ) {
1193 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1194 rs->sr_text = "read operations restricted";
1195 } else if ( restrictops & exopflag ) {
1196 rs->sr_text = "extended operation restricted";
1198 rs->sr_text = "operation restricted";
1200 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1204 rs->sr_err = LDAP_SUCCESS;
1208 int backend_check_referrals( Operation *op, SlapReply *rs )
1210 rs->sr_err = LDAP_SUCCESS;
1212 if( op->o_bd->be_chk_referrals ) {
1213 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1215 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1216 send_ldap_result( op, rs );
1228 AttributeDescription *at,
1236 if (op->o_bd == NULL) {
1237 rc = LDAP_NO_SUCH_OBJECT;
1238 } else if ( op->o_bd->be_fetch ) {
1239 rc = ( op->o_bd->be_fetch )( op, ndn,
1242 rc = LDAP_UNWILLING_TO_PERFORM;
1251 struct berval *gr_ndn,
1252 struct berval *op_ndn,
1253 ObjectClass *group_oc,
1254 AttributeDescription *group_at )
1260 Backend *be = op->o_bd;
1262 if ( op->o_abandon ) return SLAPD_ABANDON;
1264 op->o_bd = select_backend( gr_ndn, 0, 0 );
1266 for ( g = op->o_groups; g; g = g->ga_next ) {
1267 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1268 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1272 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1282 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1286 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1290 if ( op->o_pb != NULL ) {
1291 init_group_pblock( op, target, e, op_ndn, group_at );
1293 rc = call_group_preop_plugins( op );
1294 if ( rc == LDAP_SUCCESS ) {
1298 #endif /* LDAP_SLAPI */
1300 a = attr_find( e->e_attrs, group_at );
1302 /* If the attribute is a subtype of labeledURI, treat this as
1303 * a dynamic group ala groupOfURLs
1305 if (is_at_subtype( group_at->ad_type,
1306 slap_schema.si_ad_labeledURI->ad_type ) )
1310 struct berval bv, nbase;
1313 Backend *b2 = op->o_bd;
1315 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1318 op->o_bd = select_backend( op_ndn, 0, 0 );
1319 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1324 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1325 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1330 BER_BVZERO( &nbase );
1331 /* host part must be empty */
1332 /* attrs and extensions parts must be empty */
1333 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1334 ludp->lud_attrs || ludp->lud_exts )
1338 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1339 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1340 op->o_tmpmemctx ) != LDAP_SUCCESS )
1344 switch ( ludp->lud_scope ) {
1345 case LDAP_SCOPE_BASE:
1346 if ( !dn_match( &nbase, op_ndn ) ) {
1350 case LDAP_SCOPE_ONELEVEL:
1351 dnParent( op_ndn, &bv );
1352 if ( !dn_match( &nbase, &bv ) ) {
1356 case LDAP_SCOPE_SUBTREE:
1357 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1361 #ifdef LDAP_SCOPE_SUBORDINATE
1362 case LDAP_SCOPE_SUBORDINATE:
1363 if ( dn_match( &nbase, op_ndn ) ||
1364 !dnIsSuffix( op_ndn, &nbase ) )
1370 filter = str2filter_x( op, ludp->lud_filter );
1372 if ( test_filter( NULL, user, filter ) ==
1377 filter_free_x( op, filter );
1380 ldap_free_urldesc( ludp );
1381 if ( !BER_BVISNULL( &nbase ) ) {
1382 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1384 if ( rc == 0 ) break;
1386 if ( user != target ) {
1387 be_entry_release_r( op, user );
1392 rc = value_find_ex( group_at,
1393 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1394 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1395 a->a_nvals, op_ndn, op->o_tmpmemctx );
1398 rc = LDAP_NO_SUCH_ATTRIBUTE;
1401 be_entry_release_r( op, e );
1404 rc = LDAP_NO_SUCH_OBJECT;
1408 if ( op->o_pb ) call_group_postop_plugins( op );
1409 #endif /* LDAP_SLAPI */
1411 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1412 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1414 g->ga_be = op->o_bd;
1415 g->ga_oc = group_oc;
1416 g->ga_at = group_at;
1418 g->ga_len = gr_ndn->bv_len;
1419 strcpy( g->ga_ndn, gr_ndn->bv_val );
1420 g->ga_next = op->o_groups;
1429 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1433 BerVarray *vals = (BerVarray *)c->cac_private;
1434 Operation *op = NULL;
1437 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1442 if ( op->o_conn && access_allowed( op,
1443 e, a->a_desc, NULL, ACL_AUTH,
1444 &c->cac_acl_state ) == 0 ) {
1448 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1450 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1452 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1453 if ( op->o_conn && access_allowed( op,
1456 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1460 &a->a_nvals[i], op->o_tmpmemctx );
1461 if ( !BER_BVISNULL( &v[j] ) ) {
1467 op->o_tmpfree( v, op->o_tmpmemctx );
1471 BER_BVZERO( &v[j] );
1478 #endif /* LDAP_SLAPI */
1485 AttributeDescription *entry_at,
1487 slap_access_t access )
1490 Attribute *a = NULL;
1491 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1492 AccessControlState acl_state = ACL_STATE_INIT;
1493 Backend *be = op->o_bd;
1495 op->o_bd = select_backend( edn, 0, 0 );
1497 if ( target && dn_match( &target->e_nname, edn ) ) {
1501 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1505 a = attr_find( e->e_attrs, entry_at );
1507 SlapReply rs = { 0 };
1508 AttributeName anlist[ 2 ];
1510 anlist[ 0 ].an_name = entry_at->ad_cname;
1511 anlist[ 0 ].an_desc = entry_at;
1512 BER_BVZERO( &anlist[ 1 ].an_name );
1513 rs.sr_attrs = anlist;
1515 /* NOTE: backend_operational() is also called
1516 * when returning results, so it's supposed
1517 * to do no harm to entries */
1519 rc = backend_operational( op, &rs );
1522 if ( rc == LDAP_SUCCESS ) {
1523 if ( rs.sr_operational_attrs ) {
1525 a = rs.sr_operational_attrs;
1528 rc = LDAP_NO_SUCH_ATTRIBUTE;
1536 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1537 e, entry_at, NULL, access,
1538 &acl_state ) == 0 ) {
1539 rc = LDAP_INSUFFICIENT_ACCESS;
1543 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1546 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1548 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1550 if ( op->o_conn && access > ACL_NONE &&
1551 access_allowed( op, e,
1559 ber_dupbv_x( &v[j], &a->a_nvals[i],
1561 if ( !BER_BVISNULL( &v[j] ) ) {
1566 op->o_tmpfree( v, op->o_tmpmemctx );
1568 rc = LDAP_INSUFFICIENT_ACCESS;
1571 BER_BVZERO( &v[j] );
1577 else if ( op->o_pb ) {
1578 /* try any computed attributes */
1579 computed_attr_context ctx;
1581 slapi_int_pblock_set_operation( op->o_pb, op );
1583 ctx.cac_pb = op->o_pb;
1584 ctx.cac_attrs = NULL;
1585 ctx.cac_userattrs = 0;
1586 ctx.cac_opattrs = 0;
1587 ctx.cac_acl_state = acl_state;
1588 ctx.cac_private = (void *)vals;
1590 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1592 rc = LDAP_INSUFFICIENT_ACCESS;
1598 #endif /* LDAP_SLAPI */
1599 freeit: if ( e != target ) {
1600 be_entry_release_r( op, e );
1612 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1614 struct berval *nval = (struct berval *)c->cac_private;
1615 Operation *op = NULL;
1617 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1622 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1624 #endif /* LDAP_SLAPI */
1631 AttributeDescription *entry_at,
1632 struct berval *nval,
1633 slap_access_t access,
1637 int rc = LDAP_INSUFFICIENT_ACCESS;
1638 Backend *be = op->o_bd;
1642 assert( op->o_conn );
1644 assert( access > ACL_NONE );
1646 op->o_bd = select_backend( edn, 0, 0 );
1648 if ( target && dn_match( &target->e_nname, edn ) ) {
1652 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1656 Attribute *a = NULL;
1659 if ( entry_at == NULL ) {
1660 entry_at = slap_schema.si_ad_entry;
1663 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1665 if ( access_allowed_mask( op, e, entry_at,
1666 NULL, access, NULL, mask ) == 0 )
1668 rc = LDAP_INSUFFICIENT_ACCESS;
1675 a = attr_find( e->e_attrs, entry_at );
1677 SlapReply rs = { 0 };
1678 AttributeName anlist[ 2 ];
1680 anlist[ 0 ].an_name = entry_at->ad_cname;
1681 anlist[ 0 ].an_desc = entry_at;
1682 BER_BVZERO( &anlist[ 1 ].an_name );
1683 rs.sr_attrs = anlist;
1685 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1687 /* NOTE: backend_operational() is also called
1688 * when returning results, so it's supposed
1689 * to do no harm to entries */
1691 rc = backend_operational( op, &rs );
1694 if ( rc == LDAP_SUCCESS ) {
1695 if ( rs.sr_operational_attrs ) {
1697 a = rs.sr_operational_attrs;
1700 rc = LDAP_NO_SUCH_OBJECT;
1706 if ( access_allowed_mask( op, e, entry_at,
1707 nval, access, NULL, mask ) == 0 )
1709 rc = LDAP_INSUFFICIENT_ACCESS;
1715 else if ( op->o_pb ) {
1716 /* try any computed attributes */
1717 computed_attr_context ctx;
1719 slapi_int_pblock_set_operation( op->o_pb, op );
1721 ctx.cac_pb = op->o_pb;
1722 ctx.cac_attrs = NULL;
1723 ctx.cac_userattrs = 0;
1724 ctx.cac_opattrs = 0;
1725 ctx.cac_private = (void *)nval;
1727 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1729 rc = LDAP_INSUFFICIENT_ACCESS;
1735 #endif /* LDAP_SLAPI */
1737 freeit: if ( e != target ) {
1738 be_entry_release_r( op, e );
1749 int backend_operational(
1757 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1758 /* just count them */ ;
1761 * If operational attributes (allegedly) are required,
1762 * and the backend supports specific operational attributes,
1763 * add them to the attribute list
1765 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1766 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1768 *ap = slap_operational_entryDN( rs->sr_entry );
1769 ap = &(*ap)->a_next;
1772 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1773 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1775 *ap = slap_operational_subschemaSubentry( op->o_bd );
1776 ap = &(*ap)->a_next;
1779 /* Let the overlays have a chance at this */
1781 if ( SLAP_ISOVERLAY( be_orig ))
1782 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1784 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1785 op->o_bd && op->o_bd->be_operational != NULL )
1789 a = rs->sr_operational_attrs;
1790 rs->sr_operational_attrs = NULL;
1791 rc = op->o_bd->be_operational( op, rs );
1792 *ap = rs->sr_operational_attrs;
1794 rs->sr_operational_attrs = a;
1797 for ( ; *ap; ap = &(*ap)->a_next )
1798 /* just count them */ ;
1806 static void init_group_pblock( Operation *op, Entry *target,
1807 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1809 slapi_int_pblock_set_operation( op->o_pb, op );
1811 slapi_pblock_set( op->o_pb,
1812 SLAPI_X_GROUP_ENTRY, (void *)e );
1813 slapi_pblock_set( op->o_pb,
1814 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1815 slapi_pblock_set( op->o_pb,
1816 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1817 slapi_pblock_set( op->o_pb,
1818 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1821 static int call_group_preop_plugins( Operation *op )
1825 rc = slapi_int_call_plugins( op->o_bd,
1826 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1828 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1829 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1831 rc = LDAP_NO_SUCH_ATTRIBUTE;
1840 static void call_group_postop_plugins( Operation *op )
1842 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1844 #endif /* LDAP_SLAPI */