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)
229 be->be_pending_csn_list = (struct be_pcl *)
230 ch_calloc( 1, sizeof( struct be_pcl ));
232 LDAP_TAILQ_INIT( be->be_pending_csn_list );
234 Debug( LDAP_DEBUG_TRACE,
235 "backend_startup_one: starting \"%s\"\n",
236 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
238 (void)backend_set_controls( be );
239 if ( be->bd_info->bi_db_open ) {
240 rc = be->bd_info->bi_db_open( be );
242 (void)backend_set_controls( be );
245 Debug( LDAP_DEBUG_ANY,
246 "backend_startup_one: bi_db_open failed! (%d)\n",
254 int backend_startup(Backend *be)
259 if( ! ( nBackendDB > 0 ) ) {
261 Debug( LDAP_DEBUG_ANY,
262 "backend_startup: %d databases to startup.\n",
268 if ( be->bd_info->bi_open ) {
269 rc = be->bd_info->bi_open( be->bd_info );
271 Debug( LDAP_DEBUG_ANY,
272 "backend_startup: bi_open failed!\n",
279 return backend_startup_one( be );
282 /* open frontend, if required */
283 if ( frontendDB->bd_info->bi_db_open ) {
284 rc = frontendDB->bd_info->bi_db_open( frontendDB );
286 Debug( LDAP_DEBUG_ANY,
287 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
293 /* open each backend type */
294 for( i = 0; i < nBackendInfo; i++ ) {
295 if( backendInfo[i].bi_nDB == 0) {
296 /* no database of this type, don't open */
300 if( backendInfo[i].bi_open ) {
301 rc = backendInfo[i].bi_open( &backendInfo[i] );
303 Debug( LDAP_DEBUG_ANY,
304 "backend_startup: bi_open %d failed!\n",
310 (void)backend_init_controls( &backendInfo[i] );
313 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
314 LDAP_STAILQ_INIT( &slapd_rq.task_list );
315 LDAP_STAILQ_INIT( &slapd_rq.run_list );
317 /* open each backend database */
318 for( i = 0; i < nBackendDB; i++ ) {
319 if ( backendDB[i].be_suffix == NULL ) {
320 Debug( LDAP_DEBUG_ANY,
321 "backend_startup: warning, database %d (%s) "
323 i, backendDB[i].bd_info->bi_type, 0 );
325 /* append global access controls */
326 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
328 rc = backend_startup_one( &backendDB[i] );
333 if ( backendDB[i].be_syncinfo ) {
336 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
337 backendDB[i].be_modify && backendDB[i].be_delete )) {
338 Debug( LDAP_DEBUG_ANY,
339 "backend_startup: database(%d) does not support "
340 "operations required for syncrepl", i, 0, 0 );
345 si = backendDB[i].be_syncinfo;
346 si->si_be = &backendDB[i];
348 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
349 ldap_pvt_runqueue_insert( &slapd_rq,
350 si->si_interval, do_syncrepl, (void *) si );
351 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
359 int backend_num( Backend *be )
363 if( be == NULL ) return -1;
365 for( i = 0; i < nBackendDB; i++ ) {
366 if( be == &backendDB[i] ) return i;
371 int backend_shutdown( Backend *be )
377 /* shutdown a specific backend database */
379 if ( be->bd_info->bi_nDB == 0 ) {
380 /* no database of this type, we never opened it */
384 if ( be->bd_info->bi_db_close ) {
385 be->bd_info->bi_db_close( be );
388 if( be->bd_info->bi_close ) {
389 be->bd_info->bi_close( be->bd_info );
395 /* close each backend database */
396 for( i = 0; i < nBackendDB; i++ ) {
397 if ( backendDB[i].bd_info->bi_db_close ) {
398 backendDB[i].bd_info->bi_db_close(
403 Debug( LDAP_DEBUG_ANY,
404 "backend_close: bi_db_close %s failed!\n",
405 backendDB[i].be_type, 0, 0 );
409 /* close each backend type */
410 for( i = 0; i < nBackendInfo; i++ ) {
411 if( backendInfo[i].bi_nDB == 0 ) {
412 /* no database of this type */
416 if( backendInfo[i].bi_close ) {
417 backendInfo[i].bi_close(
422 /* close frontend, if required */
423 if ( frontendDB->bd_info->bi_db_close ) {
424 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
426 Debug( LDAP_DEBUG_ANY,
427 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
435 int backend_destroy(void)
439 struct slap_csn_entry *csne;
441 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
443 /* destroy each backend database */
444 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
446 if ( bd->be_syncinfo ) {
447 syncinfo_free( bd->be_syncinfo );
450 if ( bd->be_pending_csn_list ) {
451 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
453 struct slap_csn_entry *tmp_csne = csne;
455 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
456 ch_free( csne->ce_csn.bv_val );
457 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
462 if ( bd->bd_info->bi_db_destroy ) {
463 bd->bd_info->bi_db_destroy( bd );
465 ber_bvarray_free( bd->be_suffix );
466 ber_bvarray_free( bd->be_nsuffix );
467 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
468 free( bd->be_rootdn.bv_val );
470 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
471 free( bd->be_rootndn.bv_val );
473 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
474 free( bd->be_rootpw.bv_val );
476 acl_destroy( bd->be_acl, frontendDB->be_acl );
480 /* destroy each backend type */
481 for( i = 0; i < nBackendInfo; i++ ) {
482 if( backendInfo[i].bi_destroy ) {
483 backendInfo[i].bi_destroy(
489 if (backendInfo != slap_binfo) {
492 #endif /* SLAPD_MODULES */
497 /* destroy frontend database */
500 if ( bd->bd_info->bi_db_destroy ) {
501 bd->bd_info->bi_db_destroy( bd );
503 ber_bvarray_free( bd->be_suffix );
504 ber_bvarray_free( bd->be_nsuffix );
505 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
506 free( bd->be_rootdn.bv_val );
508 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
509 free( bd->be_rootndn.bv_val );
511 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
512 free( bd->be_rootpw.bv_val );
514 acl_destroy( bd->be_acl, frontendDB->be_acl );
520 BackendInfo* backend_info(const char *type)
524 /* search for the backend type */
525 for( i = 0; i < nBackendInfo; i++ ) {
526 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
527 return &backendInfo[i];
540 BackendInfo *bi = backend_info(type);
544 fprintf( stderr, "Unrecognized database type (%s)\n", type );
550 backendDB = (BackendDB *) ch_realloc(
552 (nBackendDB + 1) * sizeof(Backend) );
554 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
556 /* did realloc move our table? if so, fix up dependent pointers */
557 if ( be != backendDB ) {
559 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
560 be->be_pcl_mutexp = &be->be_pcl_mutex;
564 be = &backends[nbackends++];
568 be->be_def_limit = frontendDB->be_def_limit;
569 be->be_dfltaccess = frontendDB->be_dfltaccess;
571 be->be_restrictops = frontendDB->be_restrictops;
572 be->be_requires = frontendDB->be_requires;
573 be->be_ssf_set = frontendDB->be_ssf_set;
575 be->be_pcl_mutexp = &be->be_pcl_mutex;
576 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
578 /* assign a default depth limit for alias deref */
579 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
581 if ( bi->bi_db_init ) {
582 rc = bi->bi_db_init( be );
586 fprintf( stderr, "database init failed (%s)\n", type );
600 for ( i = 0; i < nbackends; i++ ) {
601 if ( backends[i].bd_info->bi_db_close ) {
602 (*backends[i].bd_info->bi_db_close)( &backends[i] );
606 if ( frontendDB->bd_info->bi_db_close ) {
607 (*frontendDB->bd_info->bi_db_close)( frontendDB );
619 ber_len_t len, dnlen = dn->bv_len;
622 for ( i = 0; i < nbackends; i++ ) {
623 if ( backends[i].be_nsuffix == NULL ) {
627 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
629 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
635 len = backends[i].be_nsuffix[j].bv_len;
638 /* suffix is longer than DN */
643 * input DN is normalized, so the separator check
644 * need not look at escaping
646 if ( len && len < dnlen &&
647 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
652 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
653 &dn->bv_val[dnlen-len] ) == 0 )
658 if( manageDSAit && len == dnlen &&
659 !SLAP_GLUE_SUBORDINATE( be ) ) {
676 struct berval *bvsuffix )
680 if ( be->be_nsuffix == NULL ) {
684 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
685 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
694 be_isroot_dn( Backend *be, struct berval *ndn )
696 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
700 return dn_match( &be->be_rootndn, ndn );
704 be_slurp_update( Operation *op )
706 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
707 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
711 be_shadow_update( Operation *op )
713 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
714 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
718 be_isupdate_dn( Backend *be, struct berval *ndn )
720 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
724 return dn_match( &be->be_update_ndn, ndn );
728 be_root_dn( Backend *be )
730 return &be->be_rootdn;
734 be_isroot( Operation *op )
736 return be_isroot_dn( op->o_bd, &op->o_ndn );
740 be_isroot_pw( Operation *op )
744 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
748 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
752 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
753 ldap_pvt_thread_mutex_lock( &passwd_mutex );
755 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
759 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
761 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
763 lutil_passwd_sasl_conn = NULL;
765 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
777 if ( op->o_bd->be_release ) {
778 /* free and release entry from backend */
779 return op->o_bd->be_release( op, e, rw );
788 backend_unbind( Operation *op, SlapReply *rs )
792 for ( i = 0; i < nbackends; i++ ) {
793 #if defined( LDAP_SLAPI )
796 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
797 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
798 rc = slapi_int_call_plugins( &backends[i],
799 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
802 * A preoperation plugin failure will abort the
805 Debug(LDAP_DEBUG_TRACE,
806 "do_bind: Unbind preoperation plugin failed\n",
811 #endif /* defined( LDAP_SLAPI ) */
813 if ( backends[i].be_unbind ) {
814 op->o_bd = &backends[i];
815 (*backends[i].be_unbind)( op, rs );
818 #if defined( LDAP_SLAPI )
819 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
820 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
822 Debug(LDAP_DEBUG_TRACE,
823 "do_unbind: Unbind postoperation plugins failed\n",
826 #endif /* defined( LDAP_SLAPI ) */
833 backend_connection_init(
838 for ( i = 0; i < nbackends; i++ ) {
839 if ( backends[i].be_connection_init ) {
840 (*backends[i].be_connection_init)( &backends[i], conn);
848 backend_connection_destroy(
853 for ( i = 0; i < nbackends; i++ ) {
854 if ( backends[i].be_connection_destroy ) {
855 (*backends[i].be_connection_destroy)( &backends[i], conn);
863 backend_check_controls(
867 LDAPControl **ctrls = op->o_ctrls;
868 rs->sr_err = LDAP_SUCCESS;
871 for( ; *ctrls != NULL ; ctrls++ ) {
874 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
875 case LDAP_CONTROL_NOT_FOUND:
876 /* unrecognized control */
877 if ( (*ctrls)->ldctl_iscritical ) {
878 /* should not be reachable */
879 Debug( LDAP_DEBUG_ANY,
880 "backend_check_controls: unrecognized control: %s\n",
881 (*ctrls)->ldctl_oid, 0, 0 );
886 case LDAP_COMPARE_FALSE:
887 if ( !op->o_bd->be_ctrls[ cid ] )
889 /* Per RFC 2251 (and LDAPBIS discussions), if the control
890 * is recognized and appropriate for the operation (which
891 * we've already verified), then the server should make
892 * use of the control when performing the operation.
894 * Here we find that operation extended by the control
895 * is not unavailable in a particular context, hence the
896 * return of unwillingToPerform.
898 rs->sr_text = "control unavailable in context";
899 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
904 case LDAP_COMPARE_TRUE:
909 rs->sr_err = "unable to check control";
910 rs->sr_err = LDAP_OTHER;
921 backend_check_restrictions(
924 struct berval *opdata )
926 slap_mask_t restrictops;
927 slap_mask_t requires;
929 slap_mask_t exopflag = 0;
936 int rc = SLAP_CB_CONTINUE;
938 if ( op->o_bd->be_chk_controls ) {
939 rc = ( *op->o_bd->be_chk_controls )( op, rs );
942 if ( rc == SLAP_CB_CONTINUE ) {
943 rc = backend_check_controls( op, rs );
946 if ( rc != LDAP_SUCCESS ) {
950 restrictops = op->o_bd->be_restrictops;
951 requires = op->o_bd->be_requires;
952 ssf = &op->o_bd->be_ssf_set;
955 restrictops = frontendDB->be_restrictops;
956 requires = frontendDB->be_requires;
957 ssf = &frontendDB->be_ssf_set;
960 switch( op->o_tag ) {
962 opflag = SLAP_RESTRICT_OP_ADD;
966 opflag = SLAP_RESTRICT_OP_BIND;
969 case LDAP_REQ_COMPARE:
970 opflag = SLAP_RESTRICT_OP_COMPARE;
972 case LDAP_REQ_DELETE:
974 opflag = SLAP_RESTRICT_OP_DELETE;
976 case LDAP_REQ_EXTENDED:
977 opflag = SLAP_RESTRICT_OP_EXTENDED;
980 /* treat unspecified as a modify */
981 opflag = SLAP_RESTRICT_OP_MODIFY;
986 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
989 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
993 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
994 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
998 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
999 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1003 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1004 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1009 /* treat everything else as a modify */
1010 opflag = SLAP_RESTRICT_OP_MODIFY;
1014 case LDAP_REQ_MODIFY:
1016 opflag = SLAP_RESTRICT_OP_MODIFY;
1018 case LDAP_REQ_RENAME:
1020 opflag = SLAP_RESTRICT_OP_RENAME;
1022 case LDAP_REQ_SEARCH:
1023 opflag = SLAP_RESTRICT_OP_SEARCH;
1025 case LDAP_REQ_UNBIND:
1030 rs->sr_text = "restrict operations internal error";
1031 rs->sr_err = LDAP_OTHER;
1036 /* these checks don't apply to StartTLS */
1038 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1039 if( op->o_transport_ssf < ssf->sss_transport ) {
1040 rs->sr_text = op->o_transport_ssf
1041 ? "stronger transport confidentiality required"
1042 : "transport confidentiality required";
1046 if( op->o_tls_ssf < ssf->sss_tls ) {
1047 rs->sr_text = op->o_tls_ssf
1048 ? "stronger TLS confidentiality required"
1049 : "TLS confidentiality required";
1054 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1055 /* simple bind specific check */
1056 if( op->o_ssf < ssf->sss_simple_bind ) {
1057 rs->sr_text = op->o_ssf
1058 ? "stronger confidentiality required"
1059 : "confidentiality required";
1064 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1065 /* these checks don't apply to SASL bind */
1067 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1068 rs->sr_text = op->o_sasl_ssf
1069 ? "stronger SASL confidentiality required"
1070 : "SASL confidentiality required";
1074 if( op->o_ssf < ssf->sss_ssf ) {
1075 rs->sr_text = op->o_ssf
1076 ? "stronger confidentiality required"
1077 : "confidentiality required";
1083 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1084 rs->sr_text = op->o_transport_ssf
1085 ? "stronger transport confidentiality required for update"
1086 : "transport confidentiality required for update";
1090 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1091 rs->sr_text = op->o_tls_ssf
1092 ? "stronger TLS confidentiality required for update"
1093 : "TLS confidentiality required for update";
1097 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1098 rs->sr_text = op->o_sasl_ssf
1099 ? "stronger SASL confidentiality required for update"
1100 : "SASL confidentiality required for update";
1104 if( op->o_ssf < ssf->sss_update_ssf ) {
1105 rs->sr_text = op->o_ssf
1106 ? "stronger confidentiality required for update"
1107 : "confidentiality required for update";
1111 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1112 BER_BVISEMPTY( &op->o_ndn ) )
1114 rs->sr_text = "modifications require authentication";
1115 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1119 #ifdef SLAP_X_LISTENER_MOD
1120 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1121 /* no "w" mode means readonly */
1122 rs->sr_text = "modifications not allowed on this listener";
1123 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126 #endif /* SLAP_X_LISTENER_MOD */
1131 /* these checks don't apply to Bind, StartTLS, or Unbind */
1133 if( requires & SLAP_REQUIRE_STRONG ) {
1134 /* should check mechanism */
1135 if( ( op->o_transport_ssf < ssf->sss_transport
1136 && op->o_authtype == LDAP_AUTH_SIMPLE )
1137 || BER_BVISEMPTY( &op->o_dn ) )
1139 rs->sr_text = "strong(er) authentication required";
1140 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1145 if( requires & SLAP_REQUIRE_SASL ) {
1146 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1147 rs->sr_text = "SASL authentication required";
1148 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1153 if( requires & SLAP_REQUIRE_AUTHC ) {
1154 if( BER_BVISEMPTY( &op->o_dn ) ) {
1155 rs->sr_text = "authentication required";
1156 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1161 if( requires & SLAP_REQUIRE_BIND ) {
1163 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1164 version = op->o_conn->c_protocol;
1165 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1168 /* no bind has occurred */
1169 rs->sr_text = "BIND required";
1170 rs->sr_err = LDAP_OPERATIONS_ERROR;
1175 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1176 if( op->o_protocol < LDAP_VERSION3 ) {
1177 /* no bind has occurred */
1178 rs->sr_text = "operation restricted to LDAPv3 clients";
1179 rs->sr_err = LDAP_OPERATIONS_ERROR;
1184 #ifdef SLAP_X_LISTENER_MOD
1185 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1186 if ( op->o_conn->c_listener &&
1187 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1189 /* no "x" mode means bind required */
1190 rs->sr_text = "bind required on this listener";
1191 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1196 if ( !starttls && !updateop ) {
1197 if ( op->o_conn->c_listener &&
1198 !( op->o_conn->c_listener->sl_perms &
1199 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1201 /* no "r" mode means no read */
1202 rs->sr_text = "read not allowed on this listener";
1203 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1207 #endif /* SLAP_X_LISTENER_MOD */
1211 if( ( restrictops & opflag )
1212 || ( exopflag && ( restrictops & exopflag ) ) ) {
1213 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1214 rs->sr_text = "read operations restricted";
1215 } else if ( restrictops & exopflag ) {
1216 rs->sr_text = "extended operation restricted";
1218 rs->sr_text = "operation restricted";
1220 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1224 rs->sr_err = LDAP_SUCCESS;
1228 int backend_check_referrals( Operation *op, SlapReply *rs )
1230 rs->sr_err = LDAP_SUCCESS;
1232 if( op->o_bd->be_chk_referrals ) {
1233 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1235 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1236 send_ldap_result( op, rs );
1248 AttributeDescription *at,
1256 if (op->o_bd == NULL) {
1257 rc = LDAP_NO_SUCH_OBJECT;
1258 } else if ( op->o_bd->be_fetch ) {
1259 rc = ( op->o_bd->be_fetch )( op, ndn,
1262 rc = LDAP_UNWILLING_TO_PERFORM;
1271 struct berval *gr_ndn,
1272 struct berval *op_ndn,
1273 ObjectClass *group_oc,
1274 AttributeDescription *group_at )
1280 Backend *be = op->o_bd;
1282 if ( op->o_abandon ) return SLAPD_ABANDON;
1284 op->o_bd = select_backend( gr_ndn, 0, 0 );
1286 for ( g = op->o_groups; g; g = g->ga_next ) {
1287 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1288 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1292 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1302 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1306 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1310 if ( op->o_pb != NULL ) {
1311 init_group_pblock( op, target, e, op_ndn, group_at );
1313 rc = call_group_preop_plugins( op );
1314 if ( rc == LDAP_SUCCESS ) {
1318 #endif /* LDAP_SLAPI */
1320 a = attr_find( e->e_attrs, group_at );
1322 /* If the attribute is a subtype of labeledURI, treat this as
1323 * a dynamic group ala groupOfURLs
1325 if (is_at_subtype( group_at->ad_type,
1326 slap_schema.si_ad_labeledURI->ad_type ) )
1330 struct berval bv, nbase;
1333 Backend *b2 = op->o_bd;
1335 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1338 op->o_bd = select_backend( op_ndn, 0, 0 );
1339 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1344 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1345 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1350 BER_BVZERO( &nbase );
1351 /* host part must be empty */
1352 /* attrs and extensions parts must be empty */
1353 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1354 ludp->lud_attrs || ludp->lud_exts )
1358 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1359 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1360 op->o_tmpmemctx ) != LDAP_SUCCESS )
1364 switch ( ludp->lud_scope ) {
1365 case LDAP_SCOPE_BASE:
1366 if ( !dn_match( &nbase, op_ndn ) ) {
1370 case LDAP_SCOPE_ONELEVEL:
1371 dnParent( op_ndn, &bv );
1372 if ( !dn_match( &nbase, &bv ) ) {
1376 case LDAP_SCOPE_SUBTREE:
1377 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1381 #ifdef LDAP_SCOPE_SUBORDINATE
1382 case LDAP_SCOPE_SUBORDINATE:
1383 if ( dn_match( &nbase, op_ndn ) ||
1384 !dnIsSuffix( op_ndn, &nbase ) )
1390 filter = str2filter_x( op, ludp->lud_filter );
1392 if ( test_filter( NULL, user, filter ) ==
1397 filter_free_x( op, filter );
1400 ldap_free_urldesc( ludp );
1401 if ( !BER_BVISNULL( &nbase ) ) {
1402 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1404 if ( rc == 0 ) break;
1406 if ( user != target ) {
1407 be_entry_release_r( op, user );
1412 rc = value_find_ex( group_at,
1413 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1414 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1415 a->a_nvals, op_ndn, op->o_tmpmemctx );
1418 rc = LDAP_NO_SUCH_ATTRIBUTE;
1421 be_entry_release_r( op, e );
1424 rc = LDAP_NO_SUCH_OBJECT;
1428 if ( op->o_pb ) call_group_postop_plugins( op );
1429 #endif /* LDAP_SLAPI */
1431 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1432 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1434 g->ga_be = op->o_bd;
1435 g->ga_oc = group_oc;
1436 g->ga_at = group_at;
1438 g->ga_len = gr_ndn->bv_len;
1439 strcpy( g->ga_ndn, gr_ndn->bv_val );
1440 g->ga_next = op->o_groups;
1449 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1453 BerVarray *vals = (BerVarray *)c->cac_private;
1454 Operation *op = NULL;
1457 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1462 if ( op->o_conn && access_allowed( op,
1463 e, a->a_desc, NULL, ACL_AUTH,
1464 &c->cac_acl_state ) == 0 ) {
1468 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1470 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1472 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1473 if ( op->o_conn && access_allowed( op,
1476 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1480 &a->a_nvals[i], op->o_tmpmemctx );
1481 if ( !BER_BVISNULL( &v[j] ) ) {
1487 op->o_tmpfree( v, op->o_tmpmemctx );
1491 BER_BVZERO( &v[j] );
1498 #endif /* LDAP_SLAPI */
1505 AttributeDescription *entry_at,
1507 slap_access_t access )
1510 Attribute *a = NULL;
1511 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1512 AccessControlState acl_state = ACL_STATE_INIT;
1513 Backend *be = op->o_bd;
1515 op->o_bd = select_backend( edn, 0, 0 );
1517 if ( target && dn_match( &target->e_nname, edn ) ) {
1521 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1525 a = attr_find( e->e_attrs, entry_at );
1527 SlapReply rs = { 0 };
1528 AttributeName anlist[ 2 ];
1530 anlist[ 0 ].an_name = entry_at->ad_cname;
1531 anlist[ 0 ].an_desc = entry_at;
1532 BER_BVZERO( &anlist[ 1 ].an_name );
1533 rs.sr_attrs = anlist;
1535 /* NOTE: backend_operational() is also called
1536 * when returning results, so it's supposed
1537 * to do no harm to entries */
1539 rc = backend_operational( op, &rs );
1542 if ( rc == LDAP_SUCCESS ) {
1543 if ( rs.sr_operational_attrs ) {
1545 a = rs.sr_operational_attrs;
1548 rc = LDAP_NO_SUCH_ATTRIBUTE;
1556 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1557 e, entry_at, NULL, access,
1558 &acl_state ) == 0 ) {
1559 rc = LDAP_INSUFFICIENT_ACCESS;
1563 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1566 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1568 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1570 if ( op->o_conn && access > ACL_NONE &&
1571 access_allowed( op, e,
1579 ber_dupbv_x( &v[j], &a->a_nvals[i],
1581 if ( !BER_BVISNULL( &v[j] ) ) {
1586 op->o_tmpfree( v, op->o_tmpmemctx );
1588 rc = LDAP_INSUFFICIENT_ACCESS;
1591 BER_BVZERO( &v[j] );
1597 else if ( op->o_pb ) {
1598 /* try any computed attributes */
1599 computed_attr_context ctx;
1601 slapi_int_pblock_set_operation( op->o_pb, op );
1603 ctx.cac_pb = op->o_pb;
1604 ctx.cac_attrs = NULL;
1605 ctx.cac_userattrs = 0;
1606 ctx.cac_opattrs = 0;
1607 ctx.cac_acl_state = acl_state;
1608 ctx.cac_private = (void *)vals;
1610 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1612 rc = LDAP_INSUFFICIENT_ACCESS;
1618 #endif /* LDAP_SLAPI */
1619 freeit: if ( e != target ) {
1620 be_entry_release_r( op, e );
1632 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1634 struct berval *nval = (struct berval *)c->cac_private;
1635 Operation *op = NULL;
1637 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1642 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1644 #endif /* LDAP_SLAPI */
1651 AttributeDescription *entry_at,
1652 struct berval *nval,
1653 slap_access_t access,
1657 int rc = LDAP_INSUFFICIENT_ACCESS;
1658 Backend *be = op->o_bd;
1662 assert( op->o_conn );
1664 assert( access > ACL_NONE );
1666 op->o_bd = select_backend( edn, 0, 0 );
1668 if ( target && dn_match( &target->e_nname, edn ) ) {
1672 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1676 Attribute *a = NULL;
1679 if ( entry_at == NULL ) {
1680 entry_at = slap_schema.si_ad_entry;
1683 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1685 if ( access_allowed_mask( op, e, entry_at,
1686 NULL, access, NULL, mask ) == 0 )
1688 rc = LDAP_INSUFFICIENT_ACCESS;
1695 a = attr_find( e->e_attrs, entry_at );
1697 SlapReply rs = { 0 };
1698 AttributeName anlist[ 2 ];
1700 anlist[ 0 ].an_name = entry_at->ad_cname;
1701 anlist[ 0 ].an_desc = entry_at;
1702 BER_BVZERO( &anlist[ 1 ].an_name );
1703 rs.sr_attrs = anlist;
1705 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1707 /* NOTE: backend_operational() is also called
1708 * when returning results, so it's supposed
1709 * to do no harm to entries */
1711 rc = backend_operational( op, &rs );
1714 if ( rc == LDAP_SUCCESS ) {
1715 if ( rs.sr_operational_attrs ) {
1717 a = rs.sr_operational_attrs;
1720 rc = LDAP_NO_SUCH_OBJECT;
1726 if ( access_allowed_mask( op, e, entry_at,
1727 nval, access, NULL, mask ) == 0 )
1729 rc = LDAP_INSUFFICIENT_ACCESS;
1735 else if ( op->o_pb ) {
1736 /* try any computed attributes */
1737 computed_attr_context ctx;
1739 slapi_int_pblock_set_operation( op->o_pb, op );
1741 ctx.cac_pb = op->o_pb;
1742 ctx.cac_attrs = NULL;
1743 ctx.cac_userattrs = 0;
1744 ctx.cac_opattrs = 0;
1745 ctx.cac_private = (void *)nval;
1747 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1749 rc = LDAP_INSUFFICIENT_ACCESS;
1755 #endif /* LDAP_SLAPI */
1757 freeit: if ( e != target ) {
1758 be_entry_release_r( op, e );
1769 int backend_operational(
1777 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1778 /* just count them */ ;
1781 * If operational attributes (allegedly) are required,
1782 * and the backend supports specific operational attributes,
1783 * add them to the attribute list
1785 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1786 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1788 *ap = slap_operational_entryDN( rs->sr_entry );
1789 ap = &(*ap)->a_next;
1792 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1793 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1795 *ap = slap_operational_subschemaSubentry( op->o_bd );
1796 ap = &(*ap)->a_next;
1799 /* Let the overlays have a chance at this */
1801 if ( SLAP_ISOVERLAY( be_orig ))
1802 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1804 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1805 op->o_bd && op->o_bd->be_operational != NULL )
1809 a = rs->sr_operational_attrs;
1810 rs->sr_operational_attrs = NULL;
1811 rc = op->o_bd->be_operational( op, rs );
1812 *ap = rs->sr_operational_attrs;
1814 rs->sr_operational_attrs = a;
1817 for ( ; *ap; ap = &(*ap)->a_next )
1818 /* just count them */ ;
1826 static void init_group_pblock( Operation *op, Entry *target,
1827 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1829 slapi_int_pblock_set_operation( op->o_pb, op );
1831 slapi_pblock_set( op->o_pb,
1832 SLAPI_X_GROUP_ENTRY, (void *)e );
1833 slapi_pblock_set( op->o_pb,
1834 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1835 slapi_pblock_set( op->o_pb,
1836 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1837 slapi_pblock_set( op->o_pb,
1838 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1841 static int call_group_preop_plugins( Operation *op )
1845 rc = slapi_int_call_plugins( op->o_bd,
1846 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1848 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1849 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1851 rc = LDAP_NO_SUCH_ATTRIBUTE;
1860 static void call_group_postop_plugins( Operation *op )
1862 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1864 #endif /* LDAP_SLAPI */