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++ ) {
877 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
878 case 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 );
889 case LDAP_COMPARE_FALSE:
890 if ( !op->o_bd->be_ctrls[ cid ] )
892 /* Per RFC 2251 (and LDAPBIS discussions), if the control
893 * is recognized and appropriate for the operation (which
894 * we've already verified), then the server should make
895 * use of the control when performing the operation.
897 * Here we find that operation extended by the control
898 * is not unavailable in a particular context, hence the
899 * return of unwillingToPerform.
901 rs->sr_text = "control unavailable in context";
902 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
907 case LDAP_COMPARE_TRUE:
912 rs->sr_err = "unable to check control";
913 rs->sr_err = LDAP_OTHER;
924 backend_check_restrictions(
927 struct berval *opdata )
929 slap_mask_t restrictops;
930 slap_mask_t requires;
932 slap_mask_t exopflag = 0;
939 int rc = SLAP_CB_CONTINUE;
941 if ( op->o_bd->be_chk_controls ) {
942 rc = ( *op->o_bd->be_chk_controls )( op, rs );
945 if ( rc == SLAP_CB_CONTINUE ) {
946 rc = backend_check_controls( op, rs );
949 if ( rc != LDAP_SUCCESS ) {
953 restrictops = op->o_bd->be_restrictops;
954 requires = op->o_bd->be_requires;
955 ssf = &op->o_bd->be_ssf_set;
958 restrictops = frontendDB->be_restrictops;
959 requires = frontendDB->be_requires;
960 ssf = &frontendDB->be_ssf_set;
963 switch( op->o_tag ) {
965 opflag = SLAP_RESTRICT_OP_ADD;
969 opflag = SLAP_RESTRICT_OP_BIND;
972 case LDAP_REQ_COMPARE:
973 opflag = SLAP_RESTRICT_OP_COMPARE;
975 case LDAP_REQ_DELETE:
977 opflag = SLAP_RESTRICT_OP_DELETE;
979 case LDAP_REQ_EXTENDED:
980 opflag = SLAP_RESTRICT_OP_EXTENDED;
983 /* treat unspecified as a modify */
984 opflag = SLAP_RESTRICT_OP_MODIFY;
989 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
992 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
996 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
997 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1001 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1002 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1006 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1007 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1012 /* treat everything else as a modify */
1013 opflag = SLAP_RESTRICT_OP_MODIFY;
1017 case LDAP_REQ_MODIFY:
1019 opflag = SLAP_RESTRICT_OP_MODIFY;
1021 case LDAP_REQ_RENAME:
1023 opflag = SLAP_RESTRICT_OP_RENAME;
1025 case LDAP_REQ_SEARCH:
1026 opflag = SLAP_RESTRICT_OP_SEARCH;
1028 case LDAP_REQ_UNBIND:
1033 rs->sr_text = "restrict operations internal error";
1034 rs->sr_err = LDAP_OTHER;
1039 /* these checks don't apply to StartTLS */
1041 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1042 if( op->o_transport_ssf < ssf->sss_transport ) {
1043 rs->sr_text = op->o_transport_ssf
1044 ? "stronger transport confidentiality required"
1045 : "transport confidentiality required";
1049 if( op->o_tls_ssf < ssf->sss_tls ) {
1050 rs->sr_text = op->o_tls_ssf
1051 ? "stronger TLS confidentiality required"
1052 : "TLS confidentiality required";
1057 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1058 /* simple bind specific check */
1059 if( op->o_ssf < ssf->sss_simple_bind ) {
1060 rs->sr_text = op->o_ssf
1061 ? "stronger confidentiality required"
1062 : "confidentiality required";
1067 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1068 /* these checks don't apply to SASL bind */
1070 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1071 rs->sr_text = op->o_sasl_ssf
1072 ? "stronger SASL confidentiality required"
1073 : "SASL confidentiality required";
1077 if( op->o_ssf < ssf->sss_ssf ) {
1078 rs->sr_text = op->o_ssf
1079 ? "stronger confidentiality required"
1080 : "confidentiality required";
1086 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1087 rs->sr_text = op->o_transport_ssf
1088 ? "stronger transport confidentiality required for update"
1089 : "transport confidentiality required for update";
1093 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1094 rs->sr_text = op->o_tls_ssf
1095 ? "stronger TLS confidentiality required for update"
1096 : "TLS confidentiality required for update";
1100 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1101 rs->sr_text = op->o_sasl_ssf
1102 ? "stronger SASL confidentiality required for update"
1103 : "SASL confidentiality required for update";
1107 if( op->o_ssf < ssf->sss_update_ssf ) {
1108 rs->sr_text = op->o_ssf
1109 ? "stronger confidentiality required for update"
1110 : "confidentiality required for update";
1114 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1115 BER_BVISEMPTY( &op->o_ndn ) )
1117 rs->sr_text = "modifications require authentication";
1118 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1122 #ifdef SLAP_X_LISTENER_MOD
1123 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1124 /* no "w" mode means readonly */
1125 rs->sr_text = "modifications not allowed on this listener";
1126 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1129 #endif /* SLAP_X_LISTENER_MOD */
1134 /* these checks don't apply to Bind, StartTLS, or Unbind */
1136 if( requires & SLAP_REQUIRE_STRONG ) {
1137 /* should check mechanism */
1138 if( ( op->o_transport_ssf < ssf->sss_transport
1139 && op->o_authtype == LDAP_AUTH_SIMPLE )
1140 || BER_BVISEMPTY( &op->o_dn ) )
1142 rs->sr_text = "strong(er) authentication required";
1143 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1148 if( requires & SLAP_REQUIRE_SASL ) {
1149 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1150 rs->sr_text = "SASL authentication required";
1151 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1156 if( requires & SLAP_REQUIRE_AUTHC ) {
1157 if( BER_BVISEMPTY( &op->o_dn ) ) {
1158 rs->sr_text = "authentication required";
1159 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1164 if( requires & SLAP_REQUIRE_BIND ) {
1166 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1167 version = op->o_conn->c_protocol;
1168 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1171 /* no bind has occurred */
1172 rs->sr_text = "BIND required";
1173 rs->sr_err = LDAP_OPERATIONS_ERROR;
1178 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1179 if( op->o_protocol < LDAP_VERSION3 ) {
1180 /* no bind has occurred */
1181 rs->sr_text = "operation restricted to LDAPv3 clients";
1182 rs->sr_err = LDAP_OPERATIONS_ERROR;
1187 #ifdef SLAP_X_LISTENER_MOD
1188 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1189 if ( op->o_conn->c_listener &&
1190 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1192 /* no "x" mode means bind required */
1193 rs->sr_text = "bind required on this listener";
1194 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1199 if ( !starttls && !updateop ) {
1200 if ( op->o_conn->c_listener &&
1201 !( op->o_conn->c_listener->sl_perms &
1202 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1204 /* no "r" mode means no read */
1205 rs->sr_text = "read not allowed on this listener";
1206 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1210 #endif /* SLAP_X_LISTENER_MOD */
1214 if( ( restrictops & opflag )
1215 || ( exopflag && ( restrictops & exopflag ) ) ) {
1216 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1217 rs->sr_text = "read operations restricted";
1218 } else if ( restrictops & exopflag ) {
1219 rs->sr_text = "extended operation restricted";
1221 rs->sr_text = "operation restricted";
1223 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1227 rs->sr_err = LDAP_SUCCESS;
1231 int backend_check_referrals( Operation *op, SlapReply *rs )
1233 rs->sr_err = LDAP_SUCCESS;
1235 if( op->o_bd->be_chk_referrals ) {
1236 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1238 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1239 send_ldap_result( op, rs );
1251 AttributeDescription *at,
1259 if (op->o_bd == NULL) {
1260 rc = LDAP_NO_SUCH_OBJECT;
1261 } else if ( op->o_bd->be_fetch ) {
1262 rc = ( op->o_bd->be_fetch )( op, ndn,
1265 rc = LDAP_UNWILLING_TO_PERFORM;
1274 struct berval *gr_ndn,
1275 struct berval *op_ndn,
1276 ObjectClass *group_oc,
1277 AttributeDescription *group_at )
1283 Backend *be = op->o_bd;
1285 if ( op->o_abandon ) return SLAPD_ABANDON;
1287 op->o_bd = select_backend( gr_ndn, 0, 0 );
1289 for ( g = op->o_groups; g; g = g->ga_next ) {
1290 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1291 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1295 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1305 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1309 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1313 if ( op->o_pb != NULL ) {
1314 init_group_pblock( op, target, e, op_ndn, group_at );
1316 rc = call_group_preop_plugins( op );
1317 if ( rc == LDAP_SUCCESS ) {
1321 #endif /* LDAP_SLAPI */
1323 a = attr_find( e->e_attrs, group_at );
1325 /* If the attribute is a subtype of labeledURI, treat this as
1326 * a dynamic group ala groupOfURLs
1328 if (is_at_subtype( group_at->ad_type,
1329 slap_schema.si_ad_labeledURI->ad_type ) )
1333 struct berval bv, nbase;
1336 Backend *b2 = op->o_bd;
1338 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1341 op->o_bd = select_backend( op_ndn, 0, 0 );
1342 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1347 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1348 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1353 BER_BVZERO( &nbase );
1354 /* host part must be empty */
1355 /* attrs and extensions parts must be empty */
1356 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1357 ludp->lud_attrs || ludp->lud_exts )
1361 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1362 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1363 op->o_tmpmemctx ) != LDAP_SUCCESS )
1367 switch ( ludp->lud_scope ) {
1368 case LDAP_SCOPE_BASE:
1369 if ( !dn_match( &nbase, op_ndn ) ) {
1373 case LDAP_SCOPE_ONELEVEL:
1374 dnParent( op_ndn, &bv );
1375 if ( !dn_match( &nbase, &bv ) ) {
1379 case LDAP_SCOPE_SUBTREE:
1380 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1384 #ifdef LDAP_SCOPE_SUBORDINATE
1385 case LDAP_SCOPE_SUBORDINATE:
1386 if ( dn_match( &nbase, op_ndn ) ||
1387 !dnIsSuffix( op_ndn, &nbase ) )
1393 filter = str2filter_x( op, ludp->lud_filter );
1395 if ( test_filter( NULL, user, filter ) ==
1400 filter_free_x( op, filter );
1403 ldap_free_urldesc( ludp );
1404 if ( !BER_BVISNULL( &nbase ) ) {
1405 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1407 if ( rc == 0 ) break;
1409 if ( user != target ) {
1410 be_entry_release_r( op, user );
1415 rc = value_find_ex( group_at,
1416 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1417 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1418 a->a_nvals, op_ndn, op->o_tmpmemctx );
1421 rc = LDAP_NO_SUCH_ATTRIBUTE;
1424 be_entry_release_r( op, e );
1427 rc = LDAP_NO_SUCH_OBJECT;
1431 if ( op->o_pb ) call_group_postop_plugins( op );
1432 #endif /* LDAP_SLAPI */
1434 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1435 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1437 g->ga_be = op->o_bd;
1438 g->ga_oc = group_oc;
1439 g->ga_at = group_at;
1441 g->ga_len = gr_ndn->bv_len;
1442 strcpy( g->ga_ndn, gr_ndn->bv_val );
1443 g->ga_next = op->o_groups;
1452 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1456 BerVarray *vals = (BerVarray *)c->cac_private;
1457 Operation *op = NULL;
1460 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1465 if ( op->o_conn && access_allowed( op,
1466 e, a->a_desc, NULL, ACL_AUTH,
1467 &c->cac_acl_state ) == 0 ) {
1471 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1473 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1475 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1476 if ( op->o_conn && access_allowed( op,
1479 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1483 &a->a_nvals[i], op->o_tmpmemctx );
1484 if ( !BER_BVISNULL( &v[j] ) ) {
1490 op->o_tmpfree( v, op->o_tmpmemctx );
1494 BER_BVZERO( &v[j] );
1501 #endif /* LDAP_SLAPI */
1508 AttributeDescription *entry_at,
1510 slap_access_t access )
1513 Attribute *a = NULL;
1514 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1515 AccessControlState acl_state = ACL_STATE_INIT;
1516 Backend *be = op->o_bd;
1518 op->o_bd = select_backend( edn, 0, 0 );
1520 if ( target && dn_match( &target->e_nname, edn ) ) {
1524 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1528 a = attr_find( e->e_attrs, entry_at );
1530 SlapReply rs = { 0 };
1531 AttributeName anlist[ 2 ];
1533 anlist[ 0 ].an_name = entry_at->ad_cname;
1534 anlist[ 0 ].an_desc = entry_at;
1535 BER_BVZERO( &anlist[ 1 ].an_name );
1536 rs.sr_attrs = anlist;
1538 /* NOTE: backend_operational() is also called
1539 * when returning results, so it's supposed
1540 * to do no harm to entries */
1542 rc = backend_operational( op, &rs );
1545 if ( rc == LDAP_SUCCESS ) {
1546 if ( rs.sr_operational_attrs ) {
1548 a = rs.sr_operational_attrs;
1551 rc = LDAP_NO_SUCH_ATTRIBUTE;
1559 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1560 e, entry_at, NULL, access,
1561 &acl_state ) == 0 ) {
1562 rc = LDAP_INSUFFICIENT_ACCESS;
1566 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1569 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1571 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1573 if ( op->o_conn && access > ACL_NONE &&
1574 access_allowed( op, e,
1582 ber_dupbv_x( &v[j], &a->a_nvals[i],
1584 if ( !BER_BVISNULL( &v[j] ) ) {
1589 op->o_tmpfree( v, op->o_tmpmemctx );
1591 rc = LDAP_INSUFFICIENT_ACCESS;
1594 BER_BVZERO( &v[j] );
1600 else if ( op->o_pb ) {
1601 /* try any computed attributes */
1602 computed_attr_context ctx;
1604 slapi_int_pblock_set_operation( op->o_pb, op );
1606 ctx.cac_pb = op->o_pb;
1607 ctx.cac_attrs = NULL;
1608 ctx.cac_userattrs = 0;
1609 ctx.cac_opattrs = 0;
1610 ctx.cac_acl_state = acl_state;
1611 ctx.cac_private = (void *)vals;
1613 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1615 rc = LDAP_INSUFFICIENT_ACCESS;
1621 #endif /* LDAP_SLAPI */
1622 freeit: if ( e != target ) {
1623 be_entry_release_r( op, e );
1635 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1637 struct berval *nval = (struct berval *)c->cac_private;
1638 Operation *op = NULL;
1640 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1645 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1647 #endif /* LDAP_SLAPI */
1654 AttributeDescription *entry_at,
1655 struct berval *nval,
1656 slap_access_t access,
1660 int rc = LDAP_INSUFFICIENT_ACCESS;
1661 Backend *be = op->o_bd;
1665 assert( op->o_conn );
1667 assert( access > ACL_NONE );
1669 op->o_bd = select_backend( edn, 0, 0 );
1671 if ( target && dn_match( &target->e_nname, edn ) ) {
1675 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1679 Attribute *a = NULL;
1682 if ( entry_at == NULL ) {
1683 entry_at = slap_schema.si_ad_entry;
1686 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1688 if ( access_allowed_mask( op, e, entry_at,
1689 NULL, access, NULL, mask ) == 0 )
1691 rc = LDAP_INSUFFICIENT_ACCESS;
1698 a = attr_find( e->e_attrs, entry_at );
1700 SlapReply rs = { 0 };
1701 AttributeName anlist[ 2 ];
1703 anlist[ 0 ].an_name = entry_at->ad_cname;
1704 anlist[ 0 ].an_desc = entry_at;
1705 BER_BVZERO( &anlist[ 1 ].an_name );
1706 rs.sr_attrs = anlist;
1708 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1710 /* NOTE: backend_operational() is also called
1711 * when returning results, so it's supposed
1712 * to do no harm to entries */
1714 rc = backend_operational( op, &rs );
1717 if ( rc == LDAP_SUCCESS ) {
1718 if ( rs.sr_operational_attrs ) {
1720 a = rs.sr_operational_attrs;
1723 rc = LDAP_NO_SUCH_OBJECT;
1729 if ( access_allowed_mask( op, e, entry_at,
1730 nval, access, NULL, mask ) == 0 )
1732 rc = LDAP_INSUFFICIENT_ACCESS;
1738 else if ( op->o_pb ) {
1739 /* try any computed attributes */
1740 computed_attr_context ctx;
1742 slapi_int_pblock_set_operation( op->o_pb, op );
1744 ctx.cac_pb = op->o_pb;
1745 ctx.cac_attrs = NULL;
1746 ctx.cac_userattrs = 0;
1747 ctx.cac_opattrs = 0;
1748 ctx.cac_private = (void *)nval;
1750 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1752 rc = LDAP_INSUFFICIENT_ACCESS;
1758 #endif /* LDAP_SLAPI */
1760 freeit: if ( e != target ) {
1761 be_entry_release_r( op, e );
1772 int backend_operational(
1780 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1781 /* just count them */ ;
1784 * If operational attributes (allegedly) are required,
1785 * and the backend supports specific operational attributes,
1786 * add them to the attribute list
1788 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1789 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1791 *ap = slap_operational_entryDN( rs->sr_entry );
1792 ap = &(*ap)->a_next;
1795 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1796 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1798 *ap = slap_operational_subschemaSubentry( op->o_bd );
1799 ap = &(*ap)->a_next;
1802 /* Let the overlays have a chance at this */
1804 if ( SLAP_ISOVERLAY( be_orig ))
1805 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1807 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1808 op->o_bd && op->o_bd->be_operational != NULL )
1812 a = rs->sr_operational_attrs;
1813 rs->sr_operational_attrs = NULL;
1814 rc = op->o_bd->be_operational( op, rs );
1815 *ap = rs->sr_operational_attrs;
1817 rs->sr_operational_attrs = a;
1820 for ( ; *ap; ap = &(*ap)->a_next )
1821 /* just count them */ ;
1829 static void init_group_pblock( Operation *op, Entry *target,
1830 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1832 slapi_int_pblock_set_operation( op->o_pb, op );
1834 slapi_pblock_set( op->o_pb,
1835 SLAPI_X_GROUP_ENTRY, (void *)e );
1836 slapi_pblock_set( op->o_pb,
1837 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1838 slapi_pblock_set( op->o_pb,
1839 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1840 slapi_pblock_set( op->o_pb,
1841 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1844 static int call_group_preop_plugins( Operation *op )
1848 rc = slapi_int_call_plugins( op->o_bd,
1849 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1851 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1852 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1854 rc = LDAP_NO_SUCH_ATTRIBUTE;
1863 static void call_group_postop_plugins( Operation *op )
1865 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1867 #endif /* LDAP_SLAPI */