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 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
63 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
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)
96 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
97 /* already initialized */
98 Debug( LDAP_DEBUG_ANY,
99 "backend_init: already initialized\n", 0, 0, 0 );
103 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ )
105 assert( bi->bi_init );
107 rc = bi->bi_init( bi );
110 Debug( LDAP_DEBUG_ANY,
111 "backend_init: initialized for type \"%s\"\n",
113 /* destroy those we've already inited */
118 if ( slap_binfo[nBackendInfo].bi_destroy ) {
119 slap_binfo[nBackendInfo].bi_destroy(
120 &slap_binfo[nBackendInfo] );
126 #ifdef SLAP_OVERLAY_ACCESS
127 if ( bi->bi_access_allowed == NULL ) {
128 bi->bi_access_allowed = slap_access_allowed;
130 #endif /* SLAP_OVERLAY_ACCESS */
132 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
135 if ( nBackendInfo > 0) {
143 Debug( LDAP_DEBUG_ANY,
144 "backend_init: failed\n",
148 #endif /* SLAPD_MODULES */
151 int backend_add(BackendInfo *aBackendInfo)
155 if ( aBackendInfo->bi_init == NULL ) {
156 Debug( LDAP_DEBUG_ANY, "backend_add: "
157 "backend type \"%s\" does not have the (mandatory)init function\n",
158 aBackendInfo->bi_type, 0, 0 );
162 rc = aBackendInfo->bi_init(aBackendInfo);
164 Debug( LDAP_DEBUG_ANY,
165 "backend_add: initialization for type \"%s\" failed\n",
166 aBackendInfo->bi_type, 0, 0 );
170 #ifdef SLAP_OVERLAY_ACCESS
171 if ( aBackendInfo->bi_access_allowed == NULL ) {
172 aBackendInfo->bi_access_allowed = slap_access_allowed;
174 #endif /* SLAP_OVERLAY_ACCESS */
176 (void)backend_init_controls( aBackendInfo );
178 /* now add the backend type to the Backend Info List */
179 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
185 backend_set_controls( BackendDB *be )
187 BackendInfo *bi = be->bd_info;
189 /* back-relay takes care of itself; so may do other */
190 if ( overlay_is_over( be ) ) {
191 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
194 if ( bi->bi_controls ) {
195 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
196 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
197 sizeof( be->be_ctrls ) );
198 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
203 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
204 if ( bi->bi_ctrls[ i ] ) {
205 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
215 /* startup a specific backend database */
216 int backend_startup_one(Backend *be)
222 be->be_pending_csn_list = (struct be_pcl *)
223 ch_calloc( 1, sizeof( struct be_pcl ));
225 LDAP_TAILQ_INIT( be->be_pending_csn_list );
227 Debug( LDAP_DEBUG_TRACE,
228 "backend_startup_one: starting \"%s\"\n",
229 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
232 /* set database controls */
233 (void)backend_set_controls( be );
235 if ( be->bd_info->bi_db_open ) {
236 rc = be->bd_info->bi_db_open( be );
238 (void)backend_set_controls( be );
241 Debug( LDAP_DEBUG_ANY,
242 "backend_startup_one: bi_db_open failed! (%d)\n",
250 int backend_startup(Backend *be)
256 if( ! ( nBackendDB > 0 ) ) {
258 Debug( LDAP_DEBUG_ANY,
259 "backend_startup: %d databases to startup.\n",
265 if ( be->bd_info->bi_open ) {
266 rc = be->bd_info->bi_open( be->bd_info );
268 Debug( LDAP_DEBUG_ANY,
269 "backend_startup: bi_open failed!\n",
276 return backend_startup_one( be );
279 /* open frontend, if required */
280 if ( frontendDB->bd_info->bi_db_open ) {
281 rc = frontendDB->bd_info->bi_db_open( frontendDB );
283 Debug( LDAP_DEBUG_ANY,
284 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
290 /* open each backend type */
292 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
294 if( bi->bi_nDB == 0) {
295 /* no database of this type, don't open */
300 rc = bi->bi_open( bi );
302 Debug( LDAP_DEBUG_ANY,
303 "backend_startup: bi_open %d (%s) failed!\n",
309 (void)backend_init_controls( bi );
312 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
313 LDAP_STAILQ_INIT( &slapd_rq.task_list );
314 LDAP_STAILQ_INIT( &slapd_rq.run_list );
316 /* open each backend database */
318 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
320 if ( be->be_suffix == NULL ) {
321 Debug( LDAP_DEBUG_ANY,
322 "backend_startup: warning, database %d (%s) "
324 i, be->bd_info->bi_type, 0 );
326 /* append global access controls */
327 acl_append( &be->be_acl, frontendDB->be_acl );
329 rc = backend_startup_one( be );
334 if ( be->be_syncinfo ) {
337 if ( !( be->be_search && be->be_add &&
338 be->be_modify && be->be_delete )) {
339 Debug( LDAP_DEBUG_ANY,
340 "backend_startup: database(%d) does not support "
341 "operations required for syncrepl", i, 0, 0 );
346 si = be->be_syncinfo;
349 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
350 ldap_pvt_runqueue_insert( &slapd_rq,
351 si->si_interval, do_syncrepl, (void *) si );
352 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
360 int backend_num( Backend *be )
365 if( be == NULL ) return -1;
367 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
368 if( be == b2 ) return i;
374 int backend_shutdown( Backend *be )
381 /* shutdown a specific backend database */
383 if ( be->bd_info->bi_nDB == 0 ) {
384 /* no database of this type, we never opened it */
388 if ( be->bd_info->bi_db_close ) {
389 be->bd_info->bi_db_close( be );
392 if( be->bd_info->bi_close ) {
393 be->bd_info->bi_close( be->bd_info );
399 /* close each backend database */
400 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
401 if ( be->bd_info->bi_db_close ) {
402 be->bd_info->bi_db_close( be );
406 Debug( LDAP_DEBUG_ANY,
407 "backend_close: bi_db_close %s failed!\n",
412 /* close each backend type */
413 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
414 if( bi->bi_nDB == 0 ) {
415 /* no database of this type */
424 /* close frontend, if required */
425 if ( frontendDB->bd_info->bi_db_close ) {
426 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
428 Debug( LDAP_DEBUG_ANY,
429 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
437 int backend_destroy(void)
442 struct slap_csn_entry *csne;
444 /* destroy each backend database */
445 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
446 LDAP_STAILQ_REMOVE_HEAD(&backendDB, be_next);
448 if ( bd->be_syncinfo ) {
449 syncinfo_free( bd->be_syncinfo );
452 if ( bd->be_pending_csn_list ) {
453 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
455 struct slap_csn_entry *tmp_csne = csne;
457 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
458 ch_free( csne->ce_csn.bv_val );
459 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
464 if ( bd->bd_info->bi_db_destroy ) {
465 bd->bd_info->bi_db_destroy( bd );
467 ber_bvarray_free( bd->be_suffix );
468 ber_bvarray_free( bd->be_nsuffix );
469 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
470 free( bd->be_rootdn.bv_val );
472 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
473 free( bd->be_rootndn.bv_val );
475 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
476 free( bd->be_rootpw.bv_val );
478 acl_destroy( bd->be_acl, frontendDB->be_acl );
482 /* destroy each backend type */
483 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
484 if( bi->bi_destroy ) {
485 bi->bi_destroy( bi );
490 LDAP_STAILQ_INIT(&backendInfo);
492 /* destroy frontend database */
495 if ( bd->bd_info->bi_db_destroy ) {
496 bd->bd_info->bi_db_destroy( bd );
498 ber_bvarray_free( bd->be_suffix );
499 ber_bvarray_free( bd->be_nsuffix );
500 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
501 free( bd->be_rootdn.bv_val );
503 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
504 free( bd->be_rootndn.bv_val );
506 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
507 free( bd->be_rootpw.bv_val );
509 acl_destroy( bd->be_acl, frontendDB->be_acl );
515 BackendInfo* backend_info(const char *type)
519 /* search for the backend type */
520 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
521 if( strcasecmp(bi->bi_type, type) == 0 ) {
535 BackendInfo *bi = backend_info(type);
539 fprintf( stderr, "Unrecognized database type (%s)\n", type );
543 be = ch_calloc( 1, sizeof(Backend) );
545 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
549 be->be_def_limit = frontendDB->be_def_limit;
550 be->be_dfltaccess = frontendDB->be_dfltaccess;
552 be->be_restrictops = frontendDB->be_restrictops;
553 be->be_requires = frontendDB->be_requires;
554 be->be_ssf_set = frontendDB->be_ssf_set;
556 be->be_pcl_mutexp = &be->be_pcl_mutex;
557 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
559 /* assign a default depth limit for alias deref */
560 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
562 if ( bi->bi_db_init ) {
563 rc = bi->bi_db_init( be );
567 fprintf( stderr, "database init failed (%s)\n", type );
582 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
583 if ( be->bd_info->bi_db_close ) {
584 be->bd_info->bi_db_close( be );
588 if ( frontendDB->bd_info->bi_db_close ) {
589 (*frontendDB->bd_info->bi_db_close)( frontendDB );
601 ber_len_t len, dnlen = dn->bv_len;
602 Backend *be, *b2 = NULL;
604 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
605 if ( be->be_nsuffix == NULL ) {
609 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
611 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
616 len = be->be_nsuffix[j].bv_len;
619 /* suffix is longer than DN */
624 * input DN is normalized, so the separator check
625 * need not look at escaping
627 if ( len && len < dnlen &&
628 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
633 if ( strcmp( be->be_nsuffix[j].bv_val,
634 &dn->bv_val[dnlen-len] ) == 0 )
639 if( manageDSAit && len == dnlen &&
640 !SLAP_GLUE_SUBORDINATE( be ) ) {
657 struct berval *bvsuffix )
661 if ( be->be_nsuffix == NULL ) {
665 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
666 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
675 be_isroot_dn( Backend *be, struct berval *ndn )
677 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
681 return dn_match( &be->be_rootndn, ndn );
685 be_slurp_update( Operation *op )
687 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
688 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
692 be_shadow_update( Operation *op )
694 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
695 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
699 be_isupdate_dn( Backend *be, struct berval *ndn )
701 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
705 return dn_match( &be->be_update_ndn, ndn );
709 be_root_dn( Backend *be )
711 return &be->be_rootdn;
715 be_isroot( Operation *op )
717 return be_isroot_dn( op->o_bd, &op->o_ndn );
721 be_isroot_pw( Operation *op )
725 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
729 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
734 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
735 op->o_conn->c_sasl_authctx, NULL );
738 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
741 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
753 if ( op->o_bd->be_release ) {
754 /* free and release entry from backend */
755 return op->o_bd->be_release( op, e, rw );
764 backend_unbind( Operation *op, SlapReply *rs )
769 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
770 #if defined( LDAP_SLAPI )
773 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
774 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
775 rc = slapi_int_call_plugins( be,
776 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
779 * A preoperation plugin failure will abort the
782 Debug(LDAP_DEBUG_TRACE,
783 "do_bind: Unbind preoperation plugin failed\n",
788 #endif /* defined( LDAP_SLAPI ) */
790 if ( be->be_unbind ) {
792 be->be_unbind( op, rs );
795 #if defined( LDAP_SLAPI )
796 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
797 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
799 Debug(LDAP_DEBUG_TRACE,
800 "do_unbind: Unbind postoperation plugins failed\n",
803 #endif /* defined( LDAP_SLAPI ) */
811 backend_connection_init(
816 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
817 if ( be->be_connection_init ) {
818 be->be_connection_init( be, conn);
826 backend_connection_destroy(
831 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
832 if ( be->be_connection_destroy ) {
833 be->be_connection_destroy( be, conn);
841 backend_check_controls(
845 LDAPControl **ctrls = op->o_ctrls;
846 rs->sr_err = LDAP_SUCCESS;
849 for( ; *ctrls != NULL ; ctrls++ ) {
852 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
853 case LDAP_CONTROL_NOT_FOUND:
854 /* unrecognized control */
855 if ( (*ctrls)->ldctl_iscritical ) {
856 /* should not be reachable */
857 Debug( LDAP_DEBUG_ANY,
858 "backend_check_controls: unrecognized control: %s\n",
859 (*ctrls)->ldctl_oid, 0, 0 );
864 case LDAP_COMPARE_FALSE:
865 if ( !op->o_bd->be_ctrls[ cid ] )
867 /* Per RFC 2251 (and LDAPBIS discussions), if the control
868 * is recognized and appropriate for the operation (which
869 * we've already verified), then the server should make
870 * use of the control when performing the operation.
872 * Here we find that operation extended by the control
873 * is not unavailable in a particular context, hence the
874 * return of unwillingToPerform.
876 rs->sr_text = "control unavailable in context";
877 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
882 case LDAP_COMPARE_TRUE:
887 rs->sr_text = "unable to check control";
888 rs->sr_err = LDAP_OTHER;
899 backend_check_restrictions(
902 struct berval *opdata )
904 slap_mask_t restrictops;
905 slap_mask_t requires;
907 slap_mask_t exopflag = 0;
914 int rc = SLAP_CB_CONTINUE;
916 if ( op->o_bd->be_chk_controls ) {
917 rc = ( *op->o_bd->be_chk_controls )( op, rs );
920 if ( rc == SLAP_CB_CONTINUE ) {
921 rc = backend_check_controls( op, rs );
924 if ( rc != LDAP_SUCCESS ) {
928 restrictops = op->o_bd->be_restrictops;
929 requires = op->o_bd->be_requires;
930 ssf = &op->o_bd->be_ssf_set;
933 restrictops = frontendDB->be_restrictops;
934 requires = frontendDB->be_requires;
935 ssf = &frontendDB->be_ssf_set;
938 switch( op->o_tag ) {
940 opflag = SLAP_RESTRICT_OP_ADD;
944 opflag = SLAP_RESTRICT_OP_BIND;
947 case LDAP_REQ_COMPARE:
948 opflag = SLAP_RESTRICT_OP_COMPARE;
950 case LDAP_REQ_DELETE:
952 opflag = SLAP_RESTRICT_OP_DELETE;
954 case LDAP_REQ_EXTENDED:
955 opflag = SLAP_RESTRICT_OP_EXTENDED;
958 /* treat unspecified as a modify */
959 opflag = SLAP_RESTRICT_OP_MODIFY;
964 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
967 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
971 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
972 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
976 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
977 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
981 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
982 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
987 /* treat everything else as a modify */
988 opflag = SLAP_RESTRICT_OP_MODIFY;
992 case LDAP_REQ_MODIFY:
994 opflag = SLAP_RESTRICT_OP_MODIFY;
996 case LDAP_REQ_RENAME:
998 opflag = SLAP_RESTRICT_OP_RENAME;
1000 case LDAP_REQ_SEARCH:
1001 opflag = SLAP_RESTRICT_OP_SEARCH;
1003 case LDAP_REQ_UNBIND:
1008 rs->sr_text = "restrict operations internal error";
1009 rs->sr_err = LDAP_OTHER;
1014 /* these checks don't apply to StartTLS */
1016 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1017 if( op->o_transport_ssf < ssf->sss_transport ) {
1018 rs->sr_text = op->o_transport_ssf
1019 ? "stronger transport confidentiality required"
1020 : "transport confidentiality required";
1024 if( op->o_tls_ssf < ssf->sss_tls ) {
1025 rs->sr_text = op->o_tls_ssf
1026 ? "stronger TLS confidentiality required"
1027 : "TLS confidentiality required";
1032 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1033 /* simple bind specific check */
1034 if( op->o_ssf < ssf->sss_simple_bind ) {
1035 rs->sr_text = op->o_ssf
1036 ? "stronger confidentiality required"
1037 : "confidentiality required";
1042 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1043 /* these checks don't apply to SASL bind */
1045 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1046 rs->sr_text = op->o_sasl_ssf
1047 ? "stronger SASL confidentiality required"
1048 : "SASL confidentiality required";
1052 if( op->o_ssf < ssf->sss_ssf ) {
1053 rs->sr_text = op->o_ssf
1054 ? "stronger confidentiality required"
1055 : "confidentiality required";
1061 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1062 rs->sr_text = op->o_transport_ssf
1063 ? "stronger transport confidentiality required for update"
1064 : "transport confidentiality required for update";
1068 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1069 rs->sr_text = op->o_tls_ssf
1070 ? "stronger TLS confidentiality required for update"
1071 : "TLS confidentiality required for update";
1075 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1076 rs->sr_text = op->o_sasl_ssf
1077 ? "stronger SASL confidentiality required for update"
1078 : "SASL confidentiality required for update";
1082 if( op->o_ssf < ssf->sss_update_ssf ) {
1083 rs->sr_text = op->o_ssf
1084 ? "stronger confidentiality required for update"
1085 : "confidentiality required for update";
1089 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1090 BER_BVISEMPTY( &op->o_ndn ) )
1092 rs->sr_text = "modifications require authentication";
1093 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1097 #ifdef SLAP_X_LISTENER_MOD
1098 if ( op->o_conn->c_listener &&
1099 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1100 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1102 /* no "w" mode means readonly */
1103 rs->sr_text = "modifications not allowed on this listener";
1104 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1107 #endif /* SLAP_X_LISTENER_MOD */
1112 /* these checks don't apply to Bind, StartTLS, or Unbind */
1114 if( requires & SLAP_REQUIRE_STRONG ) {
1115 /* should check mechanism */
1116 if( ( op->o_transport_ssf < ssf->sss_transport
1117 && op->o_authtype == LDAP_AUTH_SIMPLE )
1118 || BER_BVISEMPTY( &op->o_dn ) )
1120 rs->sr_text = "strong(er) authentication required";
1121 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1126 if( requires & SLAP_REQUIRE_SASL ) {
1127 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1128 rs->sr_text = "SASL authentication required";
1129 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1134 if( requires & SLAP_REQUIRE_AUTHC ) {
1135 if( BER_BVISEMPTY( &op->o_dn ) ) {
1136 rs->sr_text = "authentication required";
1137 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1142 if( requires & SLAP_REQUIRE_BIND ) {
1144 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1145 version = op->o_conn->c_protocol;
1146 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1149 /* no bind has occurred */
1150 rs->sr_text = "BIND required";
1151 rs->sr_err = LDAP_OPERATIONS_ERROR;
1156 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1157 if( op->o_protocol < LDAP_VERSION3 ) {
1158 /* no bind has occurred */
1159 rs->sr_text = "operation restricted to LDAPv3 clients";
1160 rs->sr_err = LDAP_OPERATIONS_ERROR;
1165 #ifdef SLAP_X_LISTENER_MOD
1166 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1167 if ( op->o_conn->c_listener &&
1168 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1170 /* no "x" mode means bind required */
1171 rs->sr_text = "bind required on this listener";
1172 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1177 if ( !starttls && !updateop ) {
1178 if ( op->o_conn->c_listener &&
1179 !( op->o_conn->c_listener->sl_perms &
1180 ( !BER_BVISEMPTY( &op->o_dn )
1181 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1183 /* no "r" mode means no read */
1184 rs->sr_text = "read not allowed on this listener";
1185 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1189 #endif /* SLAP_X_LISTENER_MOD */
1193 if( ( restrictops & opflag )
1194 || ( exopflag && ( restrictops & exopflag ) ) ) {
1195 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1196 rs->sr_text = "read operations restricted";
1197 } else if ( restrictops & exopflag ) {
1198 rs->sr_text = "extended operation restricted";
1200 rs->sr_text = "operation restricted";
1202 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1206 rs->sr_err = LDAP_SUCCESS;
1210 int backend_check_referrals( Operation *op, SlapReply *rs )
1212 rs->sr_err = LDAP_SUCCESS;
1214 if( op->o_bd->be_chk_referrals ) {
1215 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1217 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1218 send_ldap_result( op, rs );
1230 AttributeDescription *at,
1238 if (op->o_bd == NULL) {
1239 rc = LDAP_NO_SUCH_OBJECT;
1240 } else if ( op->o_bd->be_fetch ) {
1241 rc = ( op->o_bd->be_fetch )( op, ndn,
1244 rc = LDAP_UNWILLING_TO_PERFORM;
1253 struct berval *gr_ndn,
1254 struct berval *op_ndn,
1255 ObjectClass *group_oc,
1256 AttributeDescription *group_at )
1262 Backend *be = op->o_bd;
1264 if ( op->o_abandon ) return SLAPD_ABANDON;
1266 op->o_bd = select_backend( gr_ndn, 0, 0 );
1268 for ( g = op->o_groups; g; g = g->ga_next ) {
1269 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1270 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1274 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1284 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1288 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1292 if ( op->o_pb != NULL ) {
1293 init_group_pblock( op, target, e, op_ndn, group_at );
1295 rc = call_group_preop_plugins( op );
1296 if ( rc == LDAP_SUCCESS ) {
1300 #endif /* LDAP_SLAPI */
1302 a = attr_find( e->e_attrs, group_at );
1304 /* If the attribute is a subtype of labeledURI, treat this as
1305 * a dynamic group ala groupOfURLs
1307 if (is_at_subtype( group_at->ad_type,
1308 slap_schema.si_ad_labeledURI->ad_type ) )
1312 struct berval bv, nbase;
1315 Backend *b2 = op->o_bd;
1317 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1320 op->o_bd = select_backend( op_ndn, 0, 0 );
1321 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1326 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1327 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1332 BER_BVZERO( &nbase );
1333 /* host part must be empty */
1334 /* attrs and extensions parts must be empty */
1335 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1336 ludp->lud_attrs || ludp->lud_exts )
1340 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1341 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1342 op->o_tmpmemctx ) != LDAP_SUCCESS )
1346 switch ( ludp->lud_scope ) {
1347 case LDAP_SCOPE_BASE:
1348 if ( !dn_match( &nbase, op_ndn ) ) {
1352 case LDAP_SCOPE_ONELEVEL:
1353 dnParent( op_ndn, &bv );
1354 if ( !dn_match( &nbase, &bv ) ) {
1358 case LDAP_SCOPE_SUBTREE:
1359 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1363 #ifdef LDAP_SCOPE_SUBORDINATE
1364 case LDAP_SCOPE_SUBORDINATE:
1365 if ( dn_match( &nbase, op_ndn ) ||
1366 !dnIsSuffix( op_ndn, &nbase ) )
1372 filter = str2filter_x( op, ludp->lud_filter );
1374 if ( test_filter( NULL, user, filter ) ==
1379 filter_free_x( op, filter );
1382 ldap_free_urldesc( ludp );
1383 if ( !BER_BVISNULL( &nbase ) ) {
1384 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1386 if ( rc == 0 ) break;
1388 if ( user != target ) {
1389 be_entry_release_r( op, user );
1394 rc = value_find_ex( group_at,
1395 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1396 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1397 a->a_nvals, op_ndn, op->o_tmpmemctx );
1400 rc = LDAP_NO_SUCH_ATTRIBUTE;
1403 be_entry_release_r( op, e );
1406 rc = LDAP_NO_SUCH_OBJECT;
1410 if ( op->o_pb ) call_group_postop_plugins( op );
1411 #endif /* LDAP_SLAPI */
1413 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1414 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1416 g->ga_be = op->o_bd;
1417 g->ga_oc = group_oc;
1418 g->ga_at = group_at;
1420 g->ga_len = gr_ndn->bv_len;
1421 strcpy( g->ga_ndn, gr_ndn->bv_val );
1422 g->ga_next = op->o_groups;
1431 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1435 BerVarray *vals = (BerVarray *)c->cac_private;
1436 Operation *op = NULL;
1439 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1444 if ( op->o_conn && access_allowed( op,
1445 e, a->a_desc, NULL, ACL_AUTH,
1446 &c->cac_acl_state ) == 0 ) {
1450 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1452 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1454 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1455 if ( op->o_conn && access_allowed( op,
1458 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1462 &a->a_nvals[i], op->o_tmpmemctx );
1463 if ( !BER_BVISNULL( &v[j] ) ) {
1469 op->o_tmpfree( v, op->o_tmpmemctx );
1473 BER_BVZERO( &v[j] );
1480 #endif /* LDAP_SLAPI */
1487 AttributeDescription *entry_at,
1489 slap_access_t access )
1492 Attribute *a = NULL;
1493 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1494 AccessControlState acl_state = ACL_STATE_INIT;
1495 Backend *be = op->o_bd;
1497 op->o_bd = select_backend( edn, 0, 0 );
1499 if ( target && dn_match( &target->e_nname, edn ) ) {
1503 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1507 a = attr_find( e->e_attrs, entry_at );
1509 SlapReply rs = { 0 };
1510 AttributeName anlist[ 2 ];
1512 anlist[ 0 ].an_name = entry_at->ad_cname;
1513 anlist[ 0 ].an_desc = entry_at;
1514 BER_BVZERO( &anlist[ 1 ].an_name );
1515 rs.sr_attrs = anlist;
1517 /* NOTE: backend_operational() is also called
1518 * when returning results, so it's supposed
1519 * to do no harm to entries */
1521 rc = backend_operational( op, &rs );
1524 if ( rc == LDAP_SUCCESS ) {
1525 if ( rs.sr_operational_attrs ) {
1527 a = rs.sr_operational_attrs;
1530 rc = LDAP_NO_SUCH_ATTRIBUTE;
1538 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1539 e, entry_at, NULL, access,
1540 &acl_state ) == 0 ) {
1541 rc = LDAP_INSUFFICIENT_ACCESS;
1545 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1548 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1550 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1552 if ( op->o_conn && access > ACL_NONE &&
1553 access_allowed( op, e,
1561 ber_dupbv_x( &v[j], &a->a_nvals[i],
1563 if ( !BER_BVISNULL( &v[j] ) ) {
1568 op->o_tmpfree( v, op->o_tmpmemctx );
1570 rc = LDAP_INSUFFICIENT_ACCESS;
1573 BER_BVZERO( &v[j] );
1579 else if ( op->o_pb ) {
1580 /* try any computed attributes */
1581 computed_attr_context ctx;
1583 slapi_int_pblock_set_operation( op->o_pb, op );
1585 ctx.cac_pb = op->o_pb;
1586 ctx.cac_attrs = NULL;
1587 ctx.cac_userattrs = 0;
1588 ctx.cac_opattrs = 0;
1589 ctx.cac_acl_state = acl_state;
1590 ctx.cac_private = (void *)vals;
1592 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1594 rc = LDAP_INSUFFICIENT_ACCESS;
1600 #endif /* LDAP_SLAPI */
1601 freeit: if ( e != target ) {
1602 be_entry_release_r( op, e );
1614 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1616 struct berval *nval = (struct berval *)c->cac_private;
1617 Operation *op = NULL;
1619 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1624 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1626 #endif /* LDAP_SLAPI */
1633 AttributeDescription *entry_at,
1634 struct berval *nval,
1635 slap_access_t access,
1639 int rc = LDAP_INSUFFICIENT_ACCESS;
1640 Backend *be = op->o_bd;
1644 assert( op->o_conn );
1646 assert( access > ACL_NONE );
1648 op->o_bd = select_backend( edn, 0, 0 );
1650 if ( target && dn_match( &target->e_nname, edn ) ) {
1654 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1658 Attribute *a = NULL;
1661 if ( entry_at == NULL ) {
1662 entry_at = slap_schema.si_ad_entry;
1665 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1667 if ( access_allowed_mask( op, e, entry_at,
1668 NULL, access, NULL, mask ) == 0 )
1670 rc = LDAP_INSUFFICIENT_ACCESS;
1677 a = attr_find( e->e_attrs, entry_at );
1679 SlapReply rs = { 0 };
1680 AttributeName anlist[ 2 ];
1682 anlist[ 0 ].an_name = entry_at->ad_cname;
1683 anlist[ 0 ].an_desc = entry_at;
1684 BER_BVZERO( &anlist[ 1 ].an_name );
1685 rs.sr_attrs = anlist;
1687 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1689 /* NOTE: backend_operational() is also called
1690 * when returning results, so it's supposed
1691 * to do no harm to entries */
1693 rc = backend_operational( op, &rs );
1696 if ( rc == LDAP_SUCCESS ) {
1697 if ( rs.sr_operational_attrs ) {
1699 a = rs.sr_operational_attrs;
1702 rc = LDAP_NO_SUCH_OBJECT;
1708 if ( access_allowed_mask( op, e, entry_at,
1709 nval, access, NULL, mask ) == 0 )
1711 rc = LDAP_INSUFFICIENT_ACCESS;
1717 else if ( op->o_pb ) {
1718 /* try any computed attributes */
1719 computed_attr_context ctx;
1721 slapi_int_pblock_set_operation( op->o_pb, op );
1723 ctx.cac_pb = op->o_pb;
1724 ctx.cac_attrs = NULL;
1725 ctx.cac_userattrs = 0;
1726 ctx.cac_opattrs = 0;
1727 ctx.cac_private = (void *)nval;
1729 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1731 rc = LDAP_INSUFFICIENT_ACCESS;
1737 #endif /* LDAP_SLAPI */
1739 freeit: if ( e != target ) {
1740 be_entry_release_r( op, e );
1751 int backend_operational(
1759 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1760 /* just count them */ ;
1763 * If operational attributes (allegedly) are required,
1764 * and the backend supports specific operational attributes,
1765 * add them to the attribute list
1767 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1768 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1770 *ap = slap_operational_entryDN( rs->sr_entry );
1771 ap = &(*ap)->a_next;
1774 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1775 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1777 *ap = slap_operational_subschemaSubentry( op->o_bd );
1778 ap = &(*ap)->a_next;
1781 /* Let the overlays have a chance at this */
1783 if ( SLAP_ISOVERLAY( be_orig ) )
1784 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1786 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1787 op->o_bd && op->o_bd->be_operational != NULL )
1789 rc = op->o_bd->be_operational( op, rs );
1797 static void init_group_pblock( Operation *op, Entry *target,
1798 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1800 slapi_int_pblock_set_operation( op->o_pb, op );
1802 slapi_pblock_set( op->o_pb,
1803 SLAPI_X_GROUP_ENTRY, (void *)e );
1804 slapi_pblock_set( op->o_pb,
1805 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1806 slapi_pblock_set( op->o_pb,
1807 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1808 slapi_pblock_set( op->o_pb,
1809 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1812 static int call_group_preop_plugins( Operation *op )
1816 rc = slapi_int_call_plugins( op->o_bd,
1817 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1819 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1820 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1822 rc = LDAP_NO_SUCH_ATTRIBUTE;
1831 static void call_group_postop_plugins( Operation *op )
1833 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1835 #endif /* LDAP_SLAPI */