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;
66 backend_init_controls( BackendInfo *bi )
68 if ( bi->bi_controls ) {
71 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
74 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
75 == LDAP_CONTROL_NOT_FOUND )
77 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
84 bi->bi_ctrls[ cid ] = 1;
91 int backend_init(void)
95 if((nBackendInfo != 0) || (backendInfo != NULL)) {
96 /* already initialized */
97 Debug( LDAP_DEBUG_ANY,
98 "backend_init: already initialized\n", 0, 0, 0 );
103 slap_binfo[nBackendInfo].bi_type != NULL;
106 assert( slap_binfo[nBackendInfo].bi_init );
108 rc = slap_binfo[nBackendInfo].bi_init( &slap_binfo[nBackendInfo] );
111 Debug( LDAP_DEBUG_ANY,
112 "backend_init: initialized for type \"%s\"\n",
113 slap_binfo[nBackendInfo].bi_type, 0, 0 );
114 /* destroy those we've already inited */
119 if ( slap_binfo[nBackendInfo].bi_destroy ) {
120 slap_binfo[nBackendInfo].bi_destroy(
121 &slap_binfo[nBackendInfo] );
128 if ( nBackendInfo > 0) {
129 backendInfo = slap_binfo;
137 Debug( LDAP_DEBUG_ANY,
138 "backend_init: failed\n",
142 #endif /* SLAPD_MODULES */
145 int backend_add(BackendInfo *aBackendInfo)
149 if ( aBackendInfo->bi_init == NULL ) {
150 Debug( LDAP_DEBUG_ANY, "backend_add: "
151 "backend type \"%s\" does not have the (mandatory)init function\n",
152 aBackendInfo->bi_type, 0, 0 );
156 rc = aBackendInfo->bi_init(aBackendInfo);
158 Debug( LDAP_DEBUG_ANY,
159 "backend_add: initialization for type \"%s\" failed\n",
160 aBackendInfo->bi_type, 0, 0 );
164 (void)backend_init_controls( aBackendInfo );
166 /* now add the backend type to the Backend Info List */
168 BackendInfo *newBackendInfo = 0;
170 /* if backendInfo == slap_binfo no deallocation of old backendInfo */
171 if (backendInfo == slap_binfo) {
172 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
173 AC_MEMCPY(newBackendInfo, backendInfo,
174 sizeof(BackendInfo) * nBackendInfo);
176 newBackendInfo = ch_realloc(backendInfo,
177 sizeof(BackendInfo) * (nBackendInfo + 1));
180 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
181 sizeof(BackendInfo));
182 backendInfo = newBackendInfo;
189 backend_set_controls( BackendDB *be )
191 BackendInfo *bi = be->bd_info;
193 /* back-relay takes care of itself; so may do other */
194 if ( overlay_is_over( be ) ) {
195 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
198 if ( bi->bi_controls ) {
199 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
200 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
201 sizeof( be->be_ctrls ) );
202 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
207 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
208 if ( bi->bi_ctrls[ i ] ) {
209 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
219 /* startup a specific backend database */
220 int backend_startup_one(Backend *be)
226 be->be_pending_csn_list = (struct be_pcl *)
227 ch_calloc( 1, sizeof( struct be_pcl ));
229 LDAP_TAILQ_INIT( be->be_pending_csn_list );
231 Debug( LDAP_DEBUG_TRACE,
232 "backend_startup_one: starting \"%s\"\n",
233 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
236 /* set database controls */
237 (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 /* destroy each backend database */
442 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
444 if ( bd->be_syncinfo ) {
445 syncinfo_free( bd->be_syncinfo );
448 if ( bd->be_pending_csn_list ) {
449 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
451 struct slap_csn_entry *tmp_csne = csne;
453 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
454 ch_free( csne->ce_csn.bv_val );
455 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
460 if ( bd->bd_info->bi_db_destroy ) {
461 bd->bd_info->bi_db_destroy( bd );
463 ber_bvarray_free( bd->be_suffix );
464 ber_bvarray_free( bd->be_nsuffix );
465 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
466 free( bd->be_rootdn.bv_val );
468 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
469 free( bd->be_rootndn.bv_val );
471 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
472 free( bd->be_rootpw.bv_val );
474 acl_destroy( bd->be_acl, frontendDB->be_acl );
478 /* destroy each backend type */
479 for( i = 0; i < nBackendInfo; i++ ) {
480 if( backendInfo[i].bi_destroy ) {
481 backendInfo[i].bi_destroy(
487 if (backendInfo != slap_binfo) {
490 #endif /* SLAPD_MODULES */
495 /* destroy frontend database */
498 if ( bd->bd_info->bi_db_destroy ) {
499 bd->bd_info->bi_db_destroy( bd );
501 ber_bvarray_free( bd->be_suffix );
502 ber_bvarray_free( bd->be_nsuffix );
503 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
504 free( bd->be_rootdn.bv_val );
506 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
507 free( bd->be_rootndn.bv_val );
509 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
510 free( bd->be_rootpw.bv_val );
512 acl_destroy( bd->be_acl, frontendDB->be_acl );
518 BackendInfo* backend_info(const char *type)
522 /* search for the backend type */
523 for( i = 0; i < nBackendInfo; i++ ) {
524 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
525 return &backendInfo[i];
538 BackendInfo *bi = backend_info(type);
542 fprintf( stderr, "Unrecognized database type (%s)\n", type );
548 backendDB = (BackendDB *) ch_realloc(
550 (nBackendDB + 1) * sizeof(Backend) );
552 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
554 /* did realloc move our table? if so, fix up dependent pointers */
555 if ( be != backendDB ) {
557 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
558 be->be_pcl_mutexp = &be->be_pcl_mutex;
562 be = &backends[nbackends++];
566 be->be_def_limit = frontendDB->be_def_limit;
567 be->be_dfltaccess = frontendDB->be_dfltaccess;
569 be->be_restrictops = frontendDB->be_restrictops;
570 be->be_requires = frontendDB->be_requires;
571 be->be_ssf_set = frontendDB->be_ssf_set;
573 be->be_pcl_mutexp = &be->be_pcl_mutex;
574 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
576 /* assign a default depth limit for alias deref */
577 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
579 if ( bi->bi_db_init ) {
580 rc = bi->bi_db_init( be );
584 fprintf( stderr, "database init failed (%s)\n", type );
598 for ( i = 0; i < nbackends; i++ ) {
599 if ( backends[i].bd_info->bi_db_close ) {
600 (*backends[i].bd_info->bi_db_close)( &backends[i] );
604 if ( frontendDB->bd_info->bi_db_close ) {
605 (*frontendDB->bd_info->bi_db_close)( frontendDB );
617 ber_len_t len, dnlen = dn->bv_len;
620 for ( i = 0; i < nbackends; i++ ) {
621 if ( backends[i].be_nsuffix == NULL ) {
625 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
627 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
633 len = backends[i].be_nsuffix[j].bv_len;
636 /* suffix is longer than DN */
641 * input DN is normalized, so the separator check
642 * need not look at escaping
644 if ( len && len < dnlen &&
645 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
650 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
651 &dn->bv_val[dnlen-len] ) == 0 )
656 if( manageDSAit && len == dnlen &&
657 !SLAP_GLUE_SUBORDINATE( be ) ) {
674 struct berval *bvsuffix )
678 if ( be->be_nsuffix == NULL ) {
682 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
683 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
692 be_isroot_dn( Backend *be, struct berval *ndn )
694 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
698 return dn_match( &be->be_rootndn, ndn );
702 be_slurp_update( Operation *op )
704 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
705 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
709 be_shadow_update( Operation *op )
711 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
712 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
716 be_isupdate_dn( Backend *be, struct berval *ndn )
718 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
722 return dn_match( &be->be_update_ndn, ndn );
726 be_root_dn( Backend *be )
728 return &be->be_rootdn;
732 be_isroot( Operation *op )
734 return be_isroot_dn( op->o_bd, &op->o_ndn );
738 be_isroot_pw( Operation *op )
742 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
746 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
750 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
751 ldap_pvt_thread_mutex_lock( &passwd_mutex );
753 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
757 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
759 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
761 lutil_passwd_sasl_conn = NULL;
763 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
775 if ( op->o_bd->be_release ) {
776 /* free and release entry from backend */
777 return op->o_bd->be_release( op, e, rw );
786 backend_unbind( Operation *op, SlapReply *rs )
790 for ( i = 0; i < nbackends; i++ ) {
791 #if defined( LDAP_SLAPI )
794 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
795 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
796 rc = slapi_int_call_plugins( &backends[i],
797 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
800 * A preoperation plugin failure will abort the
803 Debug(LDAP_DEBUG_TRACE,
804 "do_bind: Unbind preoperation plugin failed\n",
809 #endif /* defined( LDAP_SLAPI ) */
811 if ( backends[i].be_unbind ) {
812 op->o_bd = &backends[i];
813 (*backends[i].be_unbind)( op, rs );
816 #if defined( LDAP_SLAPI )
817 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
818 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
820 Debug(LDAP_DEBUG_TRACE,
821 "do_unbind: Unbind postoperation plugins failed\n",
824 #endif /* defined( LDAP_SLAPI ) */
831 backend_connection_init(
836 for ( i = 0; i < nbackends; i++ ) {
837 if ( backends[i].be_connection_init ) {
838 (*backends[i].be_connection_init)( &backends[i], conn);
846 backend_connection_destroy(
851 for ( i = 0; i < nbackends; i++ ) {
852 if ( backends[i].be_connection_destroy ) {
853 (*backends[i].be_connection_destroy)( &backends[i], conn);
861 backend_check_controls(
865 LDAPControl **ctrls = op->o_ctrls;
866 rs->sr_err = LDAP_SUCCESS;
869 for( ; *ctrls != NULL ; ctrls++ ) {
872 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
873 case LDAP_CONTROL_NOT_FOUND:
874 /* unrecognized control */
875 if ( (*ctrls)->ldctl_iscritical ) {
876 /* should not be reachable */
877 Debug( LDAP_DEBUG_ANY,
878 "backend_check_controls: unrecognized control: %s\n",
879 (*ctrls)->ldctl_oid, 0, 0 );
884 case LDAP_COMPARE_FALSE:
885 if ( !op->o_bd->be_ctrls[ cid ] )
887 /* Per RFC 2251 (and LDAPBIS discussions), if the control
888 * is recognized and appropriate for the operation (which
889 * we've already verified), then the server should make
890 * use of the control when performing the operation.
892 * Here we find that operation extended by the control
893 * is not unavailable in a particular context, hence the
894 * return of unwillingToPerform.
896 rs->sr_text = "control unavailable in context";
897 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
902 case LDAP_COMPARE_TRUE:
907 rs->sr_text = "unable to check control";
908 rs->sr_err = LDAP_OTHER;
919 backend_check_restrictions(
922 struct berval *opdata )
924 slap_mask_t restrictops;
925 slap_mask_t requires;
927 slap_mask_t exopflag = 0;
934 int rc = SLAP_CB_CONTINUE;
936 if ( op->o_bd->be_chk_controls ) {
937 rc = ( *op->o_bd->be_chk_controls )( op, rs );
940 if ( rc == SLAP_CB_CONTINUE ) {
941 rc = backend_check_controls( op, rs );
944 if ( rc != LDAP_SUCCESS ) {
948 restrictops = op->o_bd->be_restrictops;
949 requires = op->o_bd->be_requires;
950 ssf = &op->o_bd->be_ssf_set;
953 restrictops = frontendDB->be_restrictops;
954 requires = frontendDB->be_requires;
955 ssf = &frontendDB->be_ssf_set;
958 switch( op->o_tag ) {
960 opflag = SLAP_RESTRICT_OP_ADD;
964 opflag = SLAP_RESTRICT_OP_BIND;
967 case LDAP_REQ_COMPARE:
968 opflag = SLAP_RESTRICT_OP_COMPARE;
970 case LDAP_REQ_DELETE:
972 opflag = SLAP_RESTRICT_OP_DELETE;
974 case LDAP_REQ_EXTENDED:
975 opflag = SLAP_RESTRICT_OP_EXTENDED;
978 /* treat unspecified as a modify */
979 opflag = SLAP_RESTRICT_OP_MODIFY;
984 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
987 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
991 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
992 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
996 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
997 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1001 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1002 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1007 /* treat everything else as a modify */
1008 opflag = SLAP_RESTRICT_OP_MODIFY;
1012 case LDAP_REQ_MODIFY:
1014 opflag = SLAP_RESTRICT_OP_MODIFY;
1016 case LDAP_REQ_RENAME:
1018 opflag = SLAP_RESTRICT_OP_RENAME;
1020 case LDAP_REQ_SEARCH:
1021 opflag = SLAP_RESTRICT_OP_SEARCH;
1023 case LDAP_REQ_UNBIND:
1028 rs->sr_text = "restrict operations internal error";
1029 rs->sr_err = LDAP_OTHER;
1034 /* these checks don't apply to StartTLS */
1036 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1037 if( op->o_transport_ssf < ssf->sss_transport ) {
1038 rs->sr_text = op->o_transport_ssf
1039 ? "stronger transport confidentiality required"
1040 : "transport confidentiality required";
1044 if( op->o_tls_ssf < ssf->sss_tls ) {
1045 rs->sr_text = op->o_tls_ssf
1046 ? "stronger TLS confidentiality required"
1047 : "TLS confidentiality required";
1052 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1053 /* simple bind specific check */
1054 if( op->o_ssf < ssf->sss_simple_bind ) {
1055 rs->sr_text = op->o_ssf
1056 ? "stronger confidentiality required"
1057 : "confidentiality required";
1062 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1063 /* these checks don't apply to SASL bind */
1065 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1066 rs->sr_text = op->o_sasl_ssf
1067 ? "stronger SASL confidentiality required"
1068 : "SASL confidentiality required";
1072 if( op->o_ssf < ssf->sss_ssf ) {
1073 rs->sr_text = op->o_ssf
1074 ? "stronger confidentiality required"
1075 : "confidentiality required";
1081 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1082 rs->sr_text = op->o_transport_ssf
1083 ? "stronger transport confidentiality required for update"
1084 : "transport confidentiality required for update";
1088 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1089 rs->sr_text = op->o_tls_ssf
1090 ? "stronger TLS confidentiality required for update"
1091 : "TLS confidentiality required for update";
1095 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1096 rs->sr_text = op->o_sasl_ssf
1097 ? "stronger SASL confidentiality required for update"
1098 : "SASL confidentiality required for update";
1102 if( op->o_ssf < ssf->sss_update_ssf ) {
1103 rs->sr_text = op->o_ssf
1104 ? "stronger confidentiality required for update"
1105 : "confidentiality required for update";
1109 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1110 BER_BVISEMPTY( &op->o_ndn ) )
1112 rs->sr_text = "modifications require authentication";
1113 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1117 #ifdef SLAP_X_LISTENER_MOD
1118 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1119 /* no "w" mode means readonly */
1120 rs->sr_text = "modifications not allowed on this listener";
1121 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1124 #endif /* SLAP_X_LISTENER_MOD */
1129 /* these checks don't apply to Bind, StartTLS, or Unbind */
1131 if( requires & SLAP_REQUIRE_STRONG ) {
1132 /* should check mechanism */
1133 if( ( op->o_transport_ssf < ssf->sss_transport
1134 && op->o_authtype == LDAP_AUTH_SIMPLE )
1135 || BER_BVISEMPTY( &op->o_dn ) )
1137 rs->sr_text = "strong(er) authentication required";
1138 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1143 if( requires & SLAP_REQUIRE_SASL ) {
1144 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1145 rs->sr_text = "SASL authentication required";
1146 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1151 if( requires & SLAP_REQUIRE_AUTHC ) {
1152 if( BER_BVISEMPTY( &op->o_dn ) ) {
1153 rs->sr_text = "authentication required";
1154 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1159 if( requires & SLAP_REQUIRE_BIND ) {
1161 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1162 version = op->o_conn->c_protocol;
1163 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1166 /* no bind has occurred */
1167 rs->sr_text = "BIND required";
1168 rs->sr_err = LDAP_OPERATIONS_ERROR;
1173 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1174 if( op->o_protocol < LDAP_VERSION3 ) {
1175 /* no bind has occurred */
1176 rs->sr_text = "operation restricted to LDAPv3 clients";
1177 rs->sr_err = LDAP_OPERATIONS_ERROR;
1182 #ifdef SLAP_X_LISTENER_MOD
1183 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1184 if ( op->o_conn->c_listener &&
1185 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1187 /* no "x" mode means bind required */
1188 rs->sr_text = "bind required on this listener";
1189 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1194 if ( !starttls && !updateop ) {
1195 if ( op->o_conn->c_listener &&
1196 !( op->o_conn->c_listener->sl_perms &
1197 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1199 /* no "r" mode means no read */
1200 rs->sr_text = "read not allowed on this listener";
1201 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1205 #endif /* SLAP_X_LISTENER_MOD */
1209 if( ( restrictops & opflag )
1210 || ( exopflag && ( restrictops & exopflag ) ) ) {
1211 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1212 rs->sr_text = "read operations restricted";
1213 } else if ( restrictops & exopflag ) {
1214 rs->sr_text = "extended operation restricted";
1216 rs->sr_text = "operation restricted";
1218 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1222 rs->sr_err = LDAP_SUCCESS;
1226 int backend_check_referrals( Operation *op, SlapReply *rs )
1228 rs->sr_err = LDAP_SUCCESS;
1230 if( op->o_bd->be_chk_referrals ) {
1231 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1233 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1234 send_ldap_result( op, rs );
1246 AttributeDescription *at,
1254 if (op->o_bd == NULL) {
1255 rc = LDAP_NO_SUCH_OBJECT;
1256 } else if ( op->o_bd->be_fetch ) {
1257 rc = ( op->o_bd->be_fetch )( op, ndn,
1260 rc = LDAP_UNWILLING_TO_PERFORM;
1269 struct berval *gr_ndn,
1270 struct berval *op_ndn,
1271 ObjectClass *group_oc,
1272 AttributeDescription *group_at )
1278 Backend *be = op->o_bd;
1280 if ( op->o_abandon ) return SLAPD_ABANDON;
1282 op->o_bd = select_backend( gr_ndn, 0, 0 );
1284 for ( g = op->o_groups; g; g = g->ga_next ) {
1285 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1286 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1290 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1300 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1304 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1308 if ( op->o_pb != NULL ) {
1309 init_group_pblock( op, target, e, op_ndn, group_at );
1311 rc = call_group_preop_plugins( op );
1312 if ( rc == LDAP_SUCCESS ) {
1316 #endif /* LDAP_SLAPI */
1318 a = attr_find( e->e_attrs, group_at );
1320 /* If the attribute is a subtype of labeledURI, treat this as
1321 * a dynamic group ala groupOfURLs
1323 if (is_at_subtype( group_at->ad_type,
1324 slap_schema.si_ad_labeledURI->ad_type ) )
1328 struct berval bv, nbase;
1331 Backend *b2 = op->o_bd;
1333 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1336 op->o_bd = select_backend( op_ndn, 0, 0 );
1337 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1342 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1343 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1348 BER_BVZERO( &nbase );
1349 /* host part must be empty */
1350 /* attrs and extensions parts must be empty */
1351 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1352 ludp->lud_attrs || ludp->lud_exts )
1356 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1357 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1358 op->o_tmpmemctx ) != LDAP_SUCCESS )
1362 switch ( ludp->lud_scope ) {
1363 case LDAP_SCOPE_BASE:
1364 if ( !dn_match( &nbase, op_ndn ) ) {
1368 case LDAP_SCOPE_ONELEVEL:
1369 dnParent( op_ndn, &bv );
1370 if ( !dn_match( &nbase, &bv ) ) {
1374 case LDAP_SCOPE_SUBTREE:
1375 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1379 #ifdef LDAP_SCOPE_SUBORDINATE
1380 case LDAP_SCOPE_SUBORDINATE:
1381 if ( dn_match( &nbase, op_ndn ) ||
1382 !dnIsSuffix( op_ndn, &nbase ) )
1388 filter = str2filter_x( op, ludp->lud_filter );
1390 if ( test_filter( NULL, user, filter ) ==
1395 filter_free_x( op, filter );
1398 ldap_free_urldesc( ludp );
1399 if ( !BER_BVISNULL( &nbase ) ) {
1400 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1402 if ( rc == 0 ) break;
1404 if ( user != target ) {
1405 be_entry_release_r( op, user );
1410 rc = value_find_ex( group_at,
1411 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1412 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1413 a->a_nvals, op_ndn, op->o_tmpmemctx );
1416 rc = LDAP_NO_SUCH_ATTRIBUTE;
1419 be_entry_release_r( op, e );
1422 rc = LDAP_NO_SUCH_OBJECT;
1426 if ( op->o_pb ) call_group_postop_plugins( op );
1427 #endif /* LDAP_SLAPI */
1429 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1430 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1432 g->ga_be = op->o_bd;
1433 g->ga_oc = group_oc;
1434 g->ga_at = group_at;
1436 g->ga_len = gr_ndn->bv_len;
1437 strcpy( g->ga_ndn, gr_ndn->bv_val );
1438 g->ga_next = op->o_groups;
1447 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1451 BerVarray *vals = (BerVarray *)c->cac_private;
1452 Operation *op = NULL;
1455 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1460 if ( op->o_conn && access_allowed( op,
1461 e, a->a_desc, NULL, ACL_AUTH,
1462 &c->cac_acl_state ) == 0 ) {
1466 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1468 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1470 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1471 if ( op->o_conn && access_allowed( op,
1474 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1478 &a->a_nvals[i], op->o_tmpmemctx );
1479 if ( !BER_BVISNULL( &v[j] ) ) {
1485 op->o_tmpfree( v, op->o_tmpmemctx );
1489 BER_BVZERO( &v[j] );
1496 #endif /* LDAP_SLAPI */
1503 AttributeDescription *entry_at,
1505 slap_access_t access )
1508 Attribute *a = NULL;
1509 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1510 AccessControlState acl_state = ACL_STATE_INIT;
1511 Backend *be = op->o_bd;
1513 op->o_bd = select_backend( edn, 0, 0 );
1515 if ( target && dn_match( &target->e_nname, edn ) ) {
1519 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1523 a = attr_find( e->e_attrs, entry_at );
1525 SlapReply rs = { 0 };
1526 AttributeName anlist[ 2 ];
1528 anlist[ 0 ].an_name = entry_at->ad_cname;
1529 anlist[ 0 ].an_desc = entry_at;
1530 BER_BVZERO( &anlist[ 1 ].an_name );
1531 rs.sr_attrs = anlist;
1533 /* NOTE: backend_operational() is also called
1534 * when returning results, so it's supposed
1535 * to do no harm to entries */
1537 rc = backend_operational( op, &rs );
1540 if ( rc == LDAP_SUCCESS ) {
1541 if ( rs.sr_operational_attrs ) {
1543 a = rs.sr_operational_attrs;
1546 rc = LDAP_NO_SUCH_ATTRIBUTE;
1554 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1555 e, entry_at, NULL, access,
1556 &acl_state ) == 0 ) {
1557 rc = LDAP_INSUFFICIENT_ACCESS;
1561 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1564 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1566 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1568 if ( op->o_conn && access > ACL_NONE &&
1569 access_allowed( op, e,
1577 ber_dupbv_x( &v[j], &a->a_nvals[i],
1579 if ( !BER_BVISNULL( &v[j] ) ) {
1584 op->o_tmpfree( v, op->o_tmpmemctx );
1586 rc = LDAP_INSUFFICIENT_ACCESS;
1589 BER_BVZERO( &v[j] );
1595 else if ( op->o_pb ) {
1596 /* try any computed attributes */
1597 computed_attr_context ctx;
1599 slapi_int_pblock_set_operation( op->o_pb, op );
1601 ctx.cac_pb = op->o_pb;
1602 ctx.cac_attrs = NULL;
1603 ctx.cac_userattrs = 0;
1604 ctx.cac_opattrs = 0;
1605 ctx.cac_acl_state = acl_state;
1606 ctx.cac_private = (void *)vals;
1608 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1610 rc = LDAP_INSUFFICIENT_ACCESS;
1616 #endif /* LDAP_SLAPI */
1617 freeit: if ( e != target ) {
1618 be_entry_release_r( op, e );
1630 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1632 struct berval *nval = (struct berval *)c->cac_private;
1633 Operation *op = NULL;
1635 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1640 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1642 #endif /* LDAP_SLAPI */
1649 AttributeDescription *entry_at,
1650 struct berval *nval,
1651 slap_access_t access,
1655 int rc = LDAP_INSUFFICIENT_ACCESS;
1656 Backend *be = op->o_bd;
1660 assert( op->o_conn );
1662 assert( access > ACL_NONE );
1664 op->o_bd = select_backend( edn, 0, 0 );
1666 if ( target && dn_match( &target->e_nname, edn ) ) {
1670 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1674 Attribute *a = NULL;
1677 if ( entry_at == NULL ) {
1678 entry_at = slap_schema.si_ad_entry;
1681 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1683 if ( access_allowed_mask( op, e, entry_at,
1684 NULL, access, NULL, mask ) == 0 )
1686 rc = LDAP_INSUFFICIENT_ACCESS;
1693 a = attr_find( e->e_attrs, entry_at );
1695 SlapReply rs = { 0 };
1696 AttributeName anlist[ 2 ];
1698 anlist[ 0 ].an_name = entry_at->ad_cname;
1699 anlist[ 0 ].an_desc = entry_at;
1700 BER_BVZERO( &anlist[ 1 ].an_name );
1701 rs.sr_attrs = anlist;
1703 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1705 /* NOTE: backend_operational() is also called
1706 * when returning results, so it's supposed
1707 * to do no harm to entries */
1709 rc = backend_operational( op, &rs );
1712 if ( rc == LDAP_SUCCESS ) {
1713 if ( rs.sr_operational_attrs ) {
1715 a = rs.sr_operational_attrs;
1718 rc = LDAP_NO_SUCH_OBJECT;
1724 if ( access_allowed_mask( op, e, entry_at,
1725 nval, access, NULL, mask ) == 0 )
1727 rc = LDAP_INSUFFICIENT_ACCESS;
1733 else if ( op->o_pb ) {
1734 /* try any computed attributes */
1735 computed_attr_context ctx;
1737 slapi_int_pblock_set_operation( op->o_pb, op );
1739 ctx.cac_pb = op->o_pb;
1740 ctx.cac_attrs = NULL;
1741 ctx.cac_userattrs = 0;
1742 ctx.cac_opattrs = 0;
1743 ctx.cac_private = (void *)nval;
1745 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1747 rc = LDAP_INSUFFICIENT_ACCESS;
1753 #endif /* LDAP_SLAPI */
1755 freeit: if ( e != target ) {
1756 be_entry_release_r( op, e );
1767 int backend_operational(
1775 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1776 /* just count them */ ;
1779 * If operational attributes (allegedly) are required,
1780 * and the backend supports specific operational attributes,
1781 * add them to the attribute list
1783 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1784 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1786 *ap = slap_operational_entryDN( rs->sr_entry );
1787 ap = &(*ap)->a_next;
1790 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1791 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1793 *ap = slap_operational_subschemaSubentry( op->o_bd );
1794 ap = &(*ap)->a_next;
1797 /* Let the overlays have a chance at this */
1799 if ( SLAP_ISOVERLAY( be_orig ))
1800 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1802 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1803 op->o_bd && op->o_bd->be_operational != NULL )
1805 rc = op->o_bd->be_operational( op, rs );
1813 static void init_group_pblock( Operation *op, Entry *target,
1814 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1816 slapi_int_pblock_set_operation( op->o_pb, op );
1818 slapi_pblock_set( op->o_pb,
1819 SLAPI_X_GROUP_ENTRY, (void *)e );
1820 slapi_pblock_set( op->o_pb,
1821 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1822 slapi_pblock_set( op->o_pb,
1823 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1824 slapi_pblock_set( op->o_pb,
1825 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1828 static int call_group_preop_plugins( Operation *op )
1832 rc = slapi_int_call_plugins( op->o_bd,
1833 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1835 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1836 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1838 rc = LDAP_NO_SUCH_ATTRIBUTE;
1847 static void call_group_postop_plugins( Operation *op )
1849 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1851 #endif /* LDAP_SLAPI */