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;
194 backend_set_controls( BackendDB *be )
196 BackendInfo *bi = be->bd_info;
198 /* back-relay takes care of itself; so may do other */
199 if ( overlay_is_over( be ) ) {
200 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
203 if ( bi->bi_controls ) {
204 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
205 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
206 sizeof( be->be_ctrls ) );
207 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
212 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
213 if ( bi->bi_ctrls[ i ] ) {
214 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
224 /* startup a specific backend database */
225 int backend_startup_one(Backend *be)
231 be->be_pending_csn_list = (struct be_pcl *)
232 ch_calloc( 1, sizeof( struct be_pcl ));
234 LDAP_TAILQ_INIT( be->be_pending_csn_list );
236 Debug( LDAP_DEBUG_TRACE,
237 "backend_startup_one: starting \"%s\"\n",
238 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
241 /* set database controls */
242 (void)backend_set_controls( be );
244 if ( be->bd_info->bi_db_open ) {
245 rc = be->bd_info->bi_db_open( be );
247 (void)backend_set_controls( be );
250 Debug( LDAP_DEBUG_ANY,
251 "backend_startup_one: bi_db_open failed! (%d)\n",
259 int backend_startup(Backend *be)
264 if( ! ( nBackendDB > 0 ) ) {
266 Debug( LDAP_DEBUG_ANY,
267 "backend_startup: %d databases to startup.\n",
273 if ( be->bd_info->bi_open ) {
274 rc = be->bd_info->bi_open( be->bd_info );
276 Debug( LDAP_DEBUG_ANY,
277 "backend_startup: bi_open failed!\n",
284 return backend_startup_one( be );
287 /* open frontend, if required */
288 if ( frontendDB->bd_info->bi_db_open ) {
289 rc = frontendDB->bd_info->bi_db_open( frontendDB );
291 Debug( LDAP_DEBUG_ANY,
292 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
298 /* open each backend type */
299 for( i = 0; i < nBackendInfo; i++ ) {
300 if( backendInfo[i].bi_nDB == 0) {
301 /* no database of this type, don't open */
305 if( backendInfo[i].bi_open ) {
306 rc = backendInfo[i].bi_open( &backendInfo[i] );
308 Debug( LDAP_DEBUG_ANY,
309 "backend_startup: bi_open %d failed!\n",
315 (void)backend_init_controls( &backendInfo[i] );
318 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
319 LDAP_STAILQ_INIT( &slapd_rq.task_list );
320 LDAP_STAILQ_INIT( &slapd_rq.run_list );
322 /* open each backend database */
323 for( i = 0; i < nBackendDB; i++ ) {
324 if ( backendDB[i].be_suffix == NULL ) {
325 Debug( LDAP_DEBUG_ANY,
326 "backend_startup: warning, database %d (%s) "
328 i, backendDB[i].bd_info->bi_type, 0 );
330 /* append global access controls */
331 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
333 rc = backend_startup_one( &backendDB[i] );
338 if ( backendDB[i].be_syncinfo ) {
341 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
342 backendDB[i].be_modify && backendDB[i].be_delete )) {
343 Debug( LDAP_DEBUG_ANY,
344 "backend_startup: database(%d) does not support "
345 "operations required for syncrepl", i, 0, 0 );
350 si = backendDB[i].be_syncinfo;
351 si->si_be = &backendDB[i];
353 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
354 ldap_pvt_runqueue_insert( &slapd_rq,
355 si->si_interval, do_syncrepl, (void *) si );
356 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
364 int backend_num( Backend *be )
368 if( be == NULL ) return -1;
370 for( i = 0; i < nBackendDB; i++ ) {
371 if( be == &backendDB[i] ) return i;
376 int backend_shutdown( Backend *be )
382 /* shutdown a specific backend database */
384 if ( be->bd_info->bi_nDB == 0 ) {
385 /* no database of this type, we never opened it */
389 if ( be->bd_info->bi_db_close ) {
390 be->bd_info->bi_db_close( be );
393 if( be->bd_info->bi_close ) {
394 be->bd_info->bi_close( be->bd_info );
400 /* close each backend database */
401 for( i = 0; i < nBackendDB; i++ ) {
402 if ( backendDB[i].bd_info->bi_db_close ) {
403 backendDB[i].bd_info->bi_db_close(
408 Debug( LDAP_DEBUG_ANY,
409 "backend_close: bi_db_close %s failed!\n",
410 backendDB[i].be_type, 0, 0 );
414 /* close each backend type */
415 for( i = 0; i < nBackendInfo; i++ ) {
416 if( backendInfo[i].bi_nDB == 0 ) {
417 /* no database of this type */
421 if( backendInfo[i].bi_close ) {
422 backendInfo[i].bi_close(
427 /* close frontend, if required */
428 if ( frontendDB->bd_info->bi_db_close ) {
429 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
431 Debug( LDAP_DEBUG_ANY,
432 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
440 int backend_destroy(void)
444 struct slap_csn_entry *csne;
446 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
448 /* destroy each backend database */
449 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
451 if ( bd->be_syncinfo ) {
452 syncinfo_free( bd->be_syncinfo );
455 if ( bd->be_pending_csn_list ) {
456 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
458 struct slap_csn_entry *tmp_csne = csne;
460 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
461 ch_free( csne->ce_csn.bv_val );
462 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
467 if ( bd->bd_info->bi_db_destroy ) {
468 bd->bd_info->bi_db_destroy( bd );
470 ber_bvarray_free( bd->be_suffix );
471 ber_bvarray_free( bd->be_nsuffix );
472 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
473 free( bd->be_rootdn.bv_val );
475 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
476 free( bd->be_rootndn.bv_val );
478 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
479 free( bd->be_rootpw.bv_val );
481 acl_destroy( bd->be_acl, frontendDB->be_acl );
485 /* destroy each backend type */
486 for( i = 0; i < nBackendInfo; i++ ) {
487 if( backendInfo[i].bi_destroy ) {
488 backendInfo[i].bi_destroy(
494 if (backendInfo != slap_binfo) {
497 #endif /* SLAPD_MODULES */
502 /* destroy frontend database */
505 if ( bd->bd_info->bi_db_destroy ) {
506 bd->bd_info->bi_db_destroy( bd );
508 ber_bvarray_free( bd->be_suffix );
509 ber_bvarray_free( bd->be_nsuffix );
510 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
511 free( bd->be_rootdn.bv_val );
513 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
514 free( bd->be_rootndn.bv_val );
516 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
517 free( bd->be_rootpw.bv_val );
519 acl_destroy( bd->be_acl, frontendDB->be_acl );
525 BackendInfo* backend_info(const char *type)
529 /* search for the backend type */
530 for( i = 0; i < nBackendInfo; i++ ) {
531 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
532 return &backendInfo[i];
545 BackendInfo *bi = backend_info(type);
549 fprintf( stderr, "Unrecognized database type (%s)\n", type );
555 backendDB = (BackendDB *) ch_realloc(
557 (nBackendDB + 1) * sizeof(Backend) );
559 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
561 /* did realloc move our table? if so, fix up dependent pointers */
562 if ( be != backendDB ) {
564 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
565 be->be_pcl_mutexp = &be->be_pcl_mutex;
569 be = &backends[nbackends++];
573 be->be_def_limit = frontendDB->be_def_limit;
574 be->be_dfltaccess = frontendDB->be_dfltaccess;
576 be->be_restrictops = frontendDB->be_restrictops;
577 be->be_requires = frontendDB->be_requires;
578 be->be_ssf_set = frontendDB->be_ssf_set;
580 be->be_pcl_mutexp = &be->be_pcl_mutex;
581 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
583 /* assign a default depth limit for alias deref */
584 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
586 if ( bi->bi_db_init ) {
587 rc = bi->bi_db_init( be );
591 fprintf( stderr, "database init failed (%s)\n", type );
605 for ( i = 0; i < nbackends; i++ ) {
606 if ( backends[i].bd_info->bi_db_close ) {
607 (*backends[i].bd_info->bi_db_close)( &backends[i] );
611 if ( frontendDB->bd_info->bi_db_close ) {
612 (*frontendDB->bd_info->bi_db_close)( frontendDB );
624 ber_len_t len, dnlen = dn->bv_len;
627 for ( i = 0; i < nbackends; i++ ) {
628 if ( backends[i].be_nsuffix == NULL ) {
632 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
634 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
640 len = backends[i].be_nsuffix[j].bv_len;
643 /* suffix is longer than DN */
648 * input DN is normalized, so the separator check
649 * need not look at escaping
651 if ( len && len < dnlen &&
652 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
657 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
658 &dn->bv_val[dnlen-len] ) == 0 )
663 if( manageDSAit && len == dnlen &&
664 !SLAP_GLUE_SUBORDINATE( be ) ) {
681 struct berval *bvsuffix )
685 if ( be->be_nsuffix == NULL ) {
689 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
690 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
699 be_isroot_dn( Backend *be, struct berval *ndn )
701 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
705 return dn_match( &be->be_rootndn, ndn );
709 be_slurp_update( Operation *op )
711 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
712 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
716 be_shadow_update( Operation *op )
718 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
719 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
723 be_isupdate_dn( Backend *be, struct berval *ndn )
725 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
729 return dn_match( &be->be_update_ndn, ndn );
733 be_root_dn( Backend *be )
735 return &be->be_rootdn;
739 be_isroot( Operation *op )
741 return be_isroot_dn( op->o_bd, &op->o_ndn );
745 be_isroot_pw( Operation *op )
749 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
753 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
757 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
758 ldap_pvt_thread_mutex_lock( &passwd_mutex );
760 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
764 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
766 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
768 lutil_passwd_sasl_conn = NULL;
770 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
782 if ( op->o_bd->be_release ) {
783 /* free and release entry from backend */
784 return op->o_bd->be_release( op, e, rw );
793 backend_unbind( Operation *op, SlapReply *rs )
797 for ( i = 0; i < nbackends; i++ ) {
798 #if defined( LDAP_SLAPI )
801 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
802 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
803 rc = slapi_int_call_plugins( &backends[i],
804 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
807 * A preoperation plugin failure will abort the
810 Debug(LDAP_DEBUG_TRACE,
811 "do_bind: Unbind preoperation plugin failed\n",
816 #endif /* defined( LDAP_SLAPI ) */
818 if ( backends[i].be_unbind ) {
819 op->o_bd = &backends[i];
820 (*backends[i].be_unbind)( op, rs );
823 #if defined( LDAP_SLAPI )
824 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
825 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
827 Debug(LDAP_DEBUG_TRACE,
828 "do_unbind: Unbind postoperation plugins failed\n",
831 #endif /* defined( LDAP_SLAPI ) */
838 backend_connection_init(
843 for ( i = 0; i < nbackends; i++ ) {
844 if ( backends[i].be_connection_init ) {
845 (*backends[i].be_connection_init)( &backends[i], conn);
853 backend_connection_destroy(
858 for ( i = 0; i < nbackends; i++ ) {
859 if ( backends[i].be_connection_destroy ) {
860 (*backends[i].be_connection_destroy)( &backends[i], conn);
868 backend_check_controls(
872 LDAPControl **ctrls = op->o_ctrls;
873 rs->sr_err = LDAP_SUCCESS;
876 for( ; *ctrls != NULL ; ctrls++ ) {
879 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
880 case LDAP_CONTROL_NOT_FOUND:
881 /* unrecognized control */
882 if ( (*ctrls)->ldctl_iscritical ) {
883 /* should not be reachable */
884 Debug( LDAP_DEBUG_ANY,
885 "backend_check_controls: unrecognized control: %s\n",
886 (*ctrls)->ldctl_oid, 0, 0 );
891 case LDAP_COMPARE_FALSE:
892 if ( !op->o_bd->be_ctrls[ cid ] )
894 /* Per RFC 2251 (and LDAPBIS discussions), if the control
895 * is recognized and appropriate for the operation (which
896 * we've already verified), then the server should make
897 * use of the control when performing the operation.
899 * Here we find that operation extended by the control
900 * is not unavailable in a particular context, hence the
901 * return of unwillingToPerform.
903 rs->sr_text = "control unavailable in context";
904 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
909 case LDAP_COMPARE_TRUE:
914 rs->sr_text = "unable to check control";
915 rs->sr_err = LDAP_OTHER;
926 backend_check_restrictions(
929 struct berval *opdata )
931 slap_mask_t restrictops;
932 slap_mask_t requires;
934 slap_mask_t exopflag = 0;
941 int rc = SLAP_CB_CONTINUE;
943 if ( op->o_bd->be_chk_controls ) {
944 rc = ( *op->o_bd->be_chk_controls )( op, rs );
947 if ( rc == SLAP_CB_CONTINUE ) {
948 rc = backend_check_controls( op, rs );
951 if ( rc != LDAP_SUCCESS ) {
955 restrictops = op->o_bd->be_restrictops;
956 requires = op->o_bd->be_requires;
957 ssf = &op->o_bd->be_ssf_set;
960 restrictops = frontendDB->be_restrictops;
961 requires = frontendDB->be_requires;
962 ssf = &frontendDB->be_ssf_set;
965 switch( op->o_tag ) {
967 opflag = SLAP_RESTRICT_OP_ADD;
971 opflag = SLAP_RESTRICT_OP_BIND;
974 case LDAP_REQ_COMPARE:
975 opflag = SLAP_RESTRICT_OP_COMPARE;
977 case LDAP_REQ_DELETE:
979 opflag = SLAP_RESTRICT_OP_DELETE;
981 case LDAP_REQ_EXTENDED:
982 opflag = SLAP_RESTRICT_OP_EXTENDED;
985 /* treat unspecified as a modify */
986 opflag = SLAP_RESTRICT_OP_MODIFY;
991 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
994 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
998 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
999 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1003 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1004 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1008 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1009 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1014 /* treat everything else as a modify */
1015 opflag = SLAP_RESTRICT_OP_MODIFY;
1019 case LDAP_REQ_MODIFY:
1021 opflag = SLAP_RESTRICT_OP_MODIFY;
1023 case LDAP_REQ_RENAME:
1025 opflag = SLAP_RESTRICT_OP_RENAME;
1027 case LDAP_REQ_SEARCH:
1028 opflag = SLAP_RESTRICT_OP_SEARCH;
1030 case LDAP_REQ_UNBIND:
1035 rs->sr_text = "restrict operations internal error";
1036 rs->sr_err = LDAP_OTHER;
1041 /* these checks don't apply to StartTLS */
1043 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1044 if( op->o_transport_ssf < ssf->sss_transport ) {
1045 rs->sr_text = op->o_transport_ssf
1046 ? "stronger transport confidentiality required"
1047 : "transport confidentiality required";
1051 if( op->o_tls_ssf < ssf->sss_tls ) {
1052 rs->sr_text = op->o_tls_ssf
1053 ? "stronger TLS confidentiality required"
1054 : "TLS confidentiality required";
1059 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1060 /* simple bind specific check */
1061 if( op->o_ssf < ssf->sss_simple_bind ) {
1062 rs->sr_text = op->o_ssf
1063 ? "stronger confidentiality required"
1064 : "confidentiality required";
1069 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1070 /* these checks don't apply to SASL bind */
1072 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1073 rs->sr_text = op->o_sasl_ssf
1074 ? "stronger SASL confidentiality required"
1075 : "SASL confidentiality required";
1079 if( op->o_ssf < ssf->sss_ssf ) {
1080 rs->sr_text = op->o_ssf
1081 ? "stronger confidentiality required"
1082 : "confidentiality required";
1088 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1089 rs->sr_text = op->o_transport_ssf
1090 ? "stronger transport confidentiality required for update"
1091 : "transport confidentiality required for update";
1095 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1096 rs->sr_text = op->o_tls_ssf
1097 ? "stronger TLS confidentiality required for update"
1098 : "TLS confidentiality required for update";
1102 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1103 rs->sr_text = op->o_sasl_ssf
1104 ? "stronger SASL confidentiality required for update"
1105 : "SASL confidentiality required for update";
1109 if( op->o_ssf < ssf->sss_update_ssf ) {
1110 rs->sr_text = op->o_ssf
1111 ? "stronger confidentiality required for update"
1112 : "confidentiality required for update";
1116 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1117 BER_BVISEMPTY( &op->o_ndn ) )
1119 rs->sr_text = "modifications require authentication";
1120 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1124 #ifdef SLAP_X_LISTENER_MOD
1125 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1126 /* no "w" mode means readonly */
1127 rs->sr_text = "modifications not allowed on this listener";
1128 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1131 #endif /* SLAP_X_LISTENER_MOD */
1136 /* these checks don't apply to Bind, StartTLS, or Unbind */
1138 if( requires & SLAP_REQUIRE_STRONG ) {
1139 /* should check mechanism */
1140 if( ( op->o_transport_ssf < ssf->sss_transport
1141 && op->o_authtype == LDAP_AUTH_SIMPLE )
1142 || BER_BVISEMPTY( &op->o_dn ) )
1144 rs->sr_text = "strong(er) authentication required";
1145 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1150 if( requires & SLAP_REQUIRE_SASL ) {
1151 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1152 rs->sr_text = "SASL authentication required";
1153 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1158 if( requires & SLAP_REQUIRE_AUTHC ) {
1159 if( BER_BVISEMPTY( &op->o_dn ) ) {
1160 rs->sr_text = "authentication required";
1161 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1166 if( requires & SLAP_REQUIRE_BIND ) {
1168 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1169 version = op->o_conn->c_protocol;
1170 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1173 /* no bind has occurred */
1174 rs->sr_text = "BIND required";
1175 rs->sr_err = LDAP_OPERATIONS_ERROR;
1180 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1181 if( op->o_protocol < LDAP_VERSION3 ) {
1182 /* no bind has occurred */
1183 rs->sr_text = "operation restricted to LDAPv3 clients";
1184 rs->sr_err = LDAP_OPERATIONS_ERROR;
1189 #ifdef SLAP_X_LISTENER_MOD
1190 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1191 if ( op->o_conn->c_listener &&
1192 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1194 /* no "x" mode means bind required */
1195 rs->sr_text = "bind required on this listener";
1196 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1201 if ( !starttls && !updateop ) {
1202 if ( op->o_conn->c_listener &&
1203 !( op->o_conn->c_listener->sl_perms &
1204 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1206 /* no "r" mode means no read */
1207 rs->sr_text = "read not allowed on this listener";
1208 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1212 #endif /* SLAP_X_LISTENER_MOD */
1216 if( ( restrictops & opflag )
1217 || ( exopflag && ( restrictops & exopflag ) ) ) {
1218 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1219 rs->sr_text = "read operations restricted";
1220 } else if ( restrictops & exopflag ) {
1221 rs->sr_text = "extended operation restricted";
1223 rs->sr_text = "operation restricted";
1225 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1229 rs->sr_err = LDAP_SUCCESS;
1233 int backend_check_referrals( Operation *op, SlapReply *rs )
1235 rs->sr_err = LDAP_SUCCESS;
1237 if( op->o_bd->be_chk_referrals ) {
1238 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1240 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1241 send_ldap_result( op, rs );
1253 AttributeDescription *at,
1261 if (op->o_bd == NULL) {
1262 rc = LDAP_NO_SUCH_OBJECT;
1263 } else if ( op->o_bd->be_fetch ) {
1264 rc = ( op->o_bd->be_fetch )( op, ndn,
1267 rc = LDAP_UNWILLING_TO_PERFORM;
1276 struct berval *gr_ndn,
1277 struct berval *op_ndn,
1278 ObjectClass *group_oc,
1279 AttributeDescription *group_at )
1285 Backend *be = op->o_bd;
1287 if ( op->o_abandon ) return SLAPD_ABANDON;
1289 op->o_bd = select_backend( gr_ndn, 0, 0 );
1291 for ( g = op->o_groups; g; g = g->ga_next ) {
1292 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1293 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1297 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1307 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1311 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1315 if ( op->o_pb != NULL ) {
1316 init_group_pblock( op, target, e, op_ndn, group_at );
1318 rc = call_group_preop_plugins( op );
1319 if ( rc == LDAP_SUCCESS ) {
1323 #endif /* LDAP_SLAPI */
1325 a = attr_find( e->e_attrs, group_at );
1327 /* If the attribute is a subtype of labeledURI, treat this as
1328 * a dynamic group ala groupOfURLs
1330 if (is_at_subtype( group_at->ad_type,
1331 slap_schema.si_ad_labeledURI->ad_type ) )
1335 struct berval bv, nbase;
1338 Backend *b2 = op->o_bd;
1340 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1343 op->o_bd = select_backend( op_ndn, 0, 0 );
1344 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1349 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1350 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1355 BER_BVZERO( &nbase );
1356 /* host part must be empty */
1357 /* attrs and extensions parts must be empty */
1358 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1359 ludp->lud_attrs || ludp->lud_exts )
1363 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1364 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1365 op->o_tmpmemctx ) != LDAP_SUCCESS )
1369 switch ( ludp->lud_scope ) {
1370 case LDAP_SCOPE_BASE:
1371 if ( !dn_match( &nbase, op_ndn ) ) {
1375 case LDAP_SCOPE_ONELEVEL:
1376 dnParent( op_ndn, &bv );
1377 if ( !dn_match( &nbase, &bv ) ) {
1381 case LDAP_SCOPE_SUBTREE:
1382 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1386 #ifdef LDAP_SCOPE_SUBORDINATE
1387 case LDAP_SCOPE_SUBORDINATE:
1388 if ( dn_match( &nbase, op_ndn ) ||
1389 !dnIsSuffix( op_ndn, &nbase ) )
1395 filter = str2filter_x( op, ludp->lud_filter );
1397 if ( test_filter( NULL, user, filter ) ==
1402 filter_free_x( op, filter );
1405 ldap_free_urldesc( ludp );
1406 if ( !BER_BVISNULL( &nbase ) ) {
1407 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1409 if ( rc == 0 ) break;
1411 if ( user != target ) {
1412 be_entry_release_r( op, user );
1417 rc = value_find_ex( group_at,
1418 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1419 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1420 a->a_nvals, op_ndn, op->o_tmpmemctx );
1423 rc = LDAP_NO_SUCH_ATTRIBUTE;
1426 be_entry_release_r( op, e );
1429 rc = LDAP_NO_SUCH_OBJECT;
1433 if ( op->o_pb ) call_group_postop_plugins( op );
1434 #endif /* LDAP_SLAPI */
1436 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1437 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1439 g->ga_be = op->o_bd;
1440 g->ga_oc = group_oc;
1441 g->ga_at = group_at;
1443 g->ga_len = gr_ndn->bv_len;
1444 strcpy( g->ga_ndn, gr_ndn->bv_val );
1445 g->ga_next = op->o_groups;
1454 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1458 BerVarray *vals = (BerVarray *)c->cac_private;
1459 Operation *op = NULL;
1462 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1467 if ( op->o_conn && access_allowed( op,
1468 e, a->a_desc, NULL, ACL_AUTH,
1469 &c->cac_acl_state ) == 0 ) {
1473 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1475 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1477 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1478 if ( op->o_conn && access_allowed( op,
1481 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1485 &a->a_nvals[i], op->o_tmpmemctx );
1486 if ( !BER_BVISNULL( &v[j] ) ) {
1492 op->o_tmpfree( v, op->o_tmpmemctx );
1496 BER_BVZERO( &v[j] );
1503 #endif /* LDAP_SLAPI */
1510 AttributeDescription *entry_at,
1512 slap_access_t access )
1515 Attribute *a = NULL;
1516 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1517 AccessControlState acl_state = ACL_STATE_INIT;
1518 Backend *be = op->o_bd;
1520 op->o_bd = select_backend( edn, 0, 0 );
1522 if ( target && dn_match( &target->e_nname, edn ) ) {
1526 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1530 a = attr_find( e->e_attrs, entry_at );
1532 SlapReply rs = { 0 };
1533 AttributeName anlist[ 2 ];
1535 anlist[ 0 ].an_name = entry_at->ad_cname;
1536 anlist[ 0 ].an_desc = entry_at;
1537 BER_BVZERO( &anlist[ 1 ].an_name );
1538 rs.sr_attrs = anlist;
1540 /* NOTE: backend_operational() is also called
1541 * when returning results, so it's supposed
1542 * to do no harm to entries */
1544 rc = backend_operational( op, &rs );
1547 if ( rc == LDAP_SUCCESS ) {
1548 if ( rs.sr_operational_attrs ) {
1550 a = rs.sr_operational_attrs;
1553 rc = LDAP_NO_SUCH_ATTRIBUTE;
1561 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1562 e, entry_at, NULL, access,
1563 &acl_state ) == 0 ) {
1564 rc = LDAP_INSUFFICIENT_ACCESS;
1568 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1571 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1573 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1575 if ( op->o_conn && access > ACL_NONE &&
1576 access_allowed( op, e,
1584 ber_dupbv_x( &v[j], &a->a_nvals[i],
1586 if ( !BER_BVISNULL( &v[j] ) ) {
1591 op->o_tmpfree( v, op->o_tmpmemctx );
1593 rc = LDAP_INSUFFICIENT_ACCESS;
1596 BER_BVZERO( &v[j] );
1602 else if ( op->o_pb ) {
1603 /* try any computed attributes */
1604 computed_attr_context ctx;
1606 slapi_int_pblock_set_operation( op->o_pb, op );
1608 ctx.cac_pb = op->o_pb;
1609 ctx.cac_attrs = NULL;
1610 ctx.cac_userattrs = 0;
1611 ctx.cac_opattrs = 0;
1612 ctx.cac_acl_state = acl_state;
1613 ctx.cac_private = (void *)vals;
1615 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1617 rc = LDAP_INSUFFICIENT_ACCESS;
1623 #endif /* LDAP_SLAPI */
1624 freeit: if ( e != target ) {
1625 be_entry_release_r( op, e );
1637 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1639 struct berval *nval = (struct berval *)c->cac_private;
1640 Operation *op = NULL;
1642 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1647 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1649 #endif /* LDAP_SLAPI */
1656 AttributeDescription *entry_at,
1657 struct berval *nval,
1658 slap_access_t access,
1662 int rc = LDAP_INSUFFICIENT_ACCESS;
1663 Backend *be = op->o_bd;
1667 assert( op->o_conn );
1669 assert( access > ACL_NONE );
1671 op->o_bd = select_backend( edn, 0, 0 );
1673 if ( target && dn_match( &target->e_nname, edn ) ) {
1677 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1681 Attribute *a = NULL;
1684 if ( entry_at == NULL ) {
1685 entry_at = slap_schema.si_ad_entry;
1688 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1690 if ( access_allowed_mask( op, e, entry_at,
1691 NULL, access, NULL, mask ) == 0 )
1693 rc = LDAP_INSUFFICIENT_ACCESS;
1700 a = attr_find( e->e_attrs, entry_at );
1702 SlapReply rs = { 0 };
1703 AttributeName anlist[ 2 ];
1705 anlist[ 0 ].an_name = entry_at->ad_cname;
1706 anlist[ 0 ].an_desc = entry_at;
1707 BER_BVZERO( &anlist[ 1 ].an_name );
1708 rs.sr_attrs = anlist;
1710 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1712 /* NOTE: backend_operational() is also called
1713 * when returning results, so it's supposed
1714 * to do no harm to entries */
1716 rc = backend_operational( op, &rs );
1719 if ( rc == LDAP_SUCCESS ) {
1720 if ( rs.sr_operational_attrs ) {
1722 a = rs.sr_operational_attrs;
1725 rc = LDAP_NO_SUCH_OBJECT;
1731 if ( access_allowed_mask( op, e, entry_at,
1732 nval, access, NULL, mask ) == 0 )
1734 rc = LDAP_INSUFFICIENT_ACCESS;
1740 else if ( op->o_pb ) {
1741 /* try any computed attributes */
1742 computed_attr_context ctx;
1744 slapi_int_pblock_set_operation( op->o_pb, op );
1746 ctx.cac_pb = op->o_pb;
1747 ctx.cac_attrs = NULL;
1748 ctx.cac_userattrs = 0;
1749 ctx.cac_opattrs = 0;
1750 ctx.cac_private = (void *)nval;
1752 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1754 rc = LDAP_INSUFFICIENT_ACCESS;
1760 #endif /* LDAP_SLAPI */
1762 freeit: if ( e != target ) {
1763 be_entry_release_r( op, e );
1774 int backend_operational(
1782 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1783 /* just count them */ ;
1786 * If operational attributes (allegedly) are required,
1787 * and the backend supports specific operational attributes,
1788 * add them to the attribute list
1790 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1791 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1793 *ap = slap_operational_entryDN( rs->sr_entry );
1794 ap = &(*ap)->a_next;
1797 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1798 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1800 *ap = slap_operational_subschemaSubentry( op->o_bd );
1801 ap = &(*ap)->a_next;
1804 /* Let the overlays have a chance at this */
1806 if ( SLAP_ISOVERLAY( be_orig ))
1807 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1809 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1810 op->o_bd && op->o_bd->be_operational != NULL )
1812 rc = op->o_bd->be_operational( op, rs );
1820 static void init_group_pblock( Operation *op, Entry *target,
1821 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1823 slapi_int_pblock_set_operation( op->o_pb, op );
1825 slapi_pblock_set( op->o_pb,
1826 SLAPI_X_GROUP_ENTRY, (void *)e );
1827 slapi_pblock_set( op->o_pb,
1828 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1829 slapi_pblock_set( op->o_pb,
1830 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1831 slapi_pblock_set( op->o_pb,
1832 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1835 static int call_group_preop_plugins( Operation *op )
1839 rc = slapi_int_call_plugins( op->o_bd,
1840 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1842 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1843 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1845 rc = LDAP_NO_SUCH_ATTRIBUTE;
1854 static void call_group_postop_plugins( Operation *op )
1856 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1858 #endif /* LDAP_SLAPI */