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] );
125 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
128 if ( nBackendInfo > 0) {
136 Debug( LDAP_DEBUG_ANY,
137 "backend_init: failed\n",
141 #endif /* SLAPD_MODULES */
144 int backend_add(BackendInfo *aBackendInfo)
148 if ( aBackendInfo->bi_init == NULL ) {
149 Debug( LDAP_DEBUG_ANY, "backend_add: "
150 "backend type \"%s\" does not have the (mandatory)init function\n",
151 aBackendInfo->bi_type, 0, 0 );
155 rc = aBackendInfo->bi_init(aBackendInfo);
157 Debug( LDAP_DEBUG_ANY,
158 "backend_add: initialization for type \"%s\" failed\n",
159 aBackendInfo->bi_type, 0, 0 );
163 (void)backend_init_controls( aBackendInfo );
165 /* now add the backend type to the Backend Info List */
166 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
172 backend_set_controls( BackendDB *be )
174 BackendInfo *bi = be->bd_info;
176 /* back-relay takes care of itself; so may do other */
177 if ( overlay_is_over( be ) ) {
178 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
181 if ( bi->bi_controls ) {
182 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
183 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
184 sizeof( be->be_ctrls ) );
185 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
190 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
191 if ( bi->bi_ctrls[ i ] ) {
192 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
202 /* startup a specific backend database */
203 int backend_startup_one(Backend *be)
209 be->be_pending_csn_list = (struct be_pcl *)
210 ch_calloc( 1, sizeof( struct be_pcl ));
212 LDAP_TAILQ_INIT( be->be_pending_csn_list );
214 Debug( LDAP_DEBUG_TRACE,
215 "backend_startup_one: starting \"%s\"\n",
216 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
219 /* set database controls */
220 (void)backend_set_controls( be );
222 if ( be->bd_info->bi_db_open ) {
223 rc = be->bd_info->bi_db_open( be );
225 (void)backend_set_controls( be );
228 Debug( LDAP_DEBUG_ANY,
229 "backend_startup_one: bi_db_open failed! (%d)\n",
237 int backend_startup(Backend *be)
243 if( ! ( nBackendDB > 0 ) ) {
245 Debug( LDAP_DEBUG_ANY,
246 "backend_startup: %d databases to startup.\n",
252 if ( be->bd_info->bi_open ) {
253 rc = be->bd_info->bi_open( be->bd_info );
255 Debug( LDAP_DEBUG_ANY,
256 "backend_startup: bi_open failed!\n",
263 return backend_startup_one( be );
266 /* open frontend, if required */
267 if ( frontendDB->bd_info->bi_db_open ) {
268 rc = frontendDB->bd_info->bi_db_open( frontendDB );
270 Debug( LDAP_DEBUG_ANY,
271 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
277 /* open each backend type */
279 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
281 if( bi->bi_nDB == 0) {
282 /* no database of this type, don't open */
287 rc = bi->bi_open( bi );
289 Debug( LDAP_DEBUG_ANY,
290 "backend_startup: bi_open %d (%s) failed!\n",
296 (void)backend_init_controls( bi );
299 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
300 LDAP_STAILQ_INIT( &slapd_rq.task_list );
301 LDAP_STAILQ_INIT( &slapd_rq.run_list );
303 /* open each backend database */
305 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
307 if ( be->be_suffix == NULL ) {
308 Debug( LDAP_DEBUG_ANY,
309 "backend_startup: warning, database %d (%s) "
311 i, be->bd_info->bi_type, 0 );
313 /* append global access controls */
314 acl_append( &be->be_acl, frontendDB->be_acl );
316 rc = backend_startup_one( be );
321 if ( be->be_syncinfo ) {
324 if ( !( be->be_search && be->be_add &&
325 be->be_modify && be->be_delete )) {
326 Debug( LDAP_DEBUG_ANY,
327 "backend_startup: database(%d) does not support "
328 "operations required for syncrepl", i, 0, 0 );
333 si = be->be_syncinfo;
336 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
337 ldap_pvt_runqueue_insert( &slapd_rq,
338 si->si_interval, do_syncrepl, (void *) si );
339 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
347 int backend_num( Backend *be )
352 if( be == NULL ) return -1;
354 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
355 if( be == b2 ) return i;
361 int backend_shutdown( Backend *be )
368 /* shutdown a specific backend database */
370 if ( be->bd_info->bi_nDB == 0 ) {
371 /* no database of this type, we never opened it */
375 if ( be->bd_info->bi_db_close ) {
376 be->bd_info->bi_db_close( be );
379 if( be->bd_info->bi_close ) {
380 be->bd_info->bi_close( be->bd_info );
386 /* close each backend database */
387 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
388 if ( be->bd_info->bi_db_close ) {
389 be->bd_info->bi_db_close( be );
393 Debug( LDAP_DEBUG_ANY,
394 "backend_close: bi_db_close %s failed!\n",
399 /* close each backend type */
400 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
401 if( bi->bi_nDB == 0 ) {
402 /* no database of this type */
411 /* close frontend, if required */
412 if ( frontendDB->bd_info->bi_db_close ) {
413 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
415 Debug( LDAP_DEBUG_ANY,
416 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
424 int backend_destroy(void)
429 struct slap_csn_entry *csne;
431 /* destroy each backend database */
432 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
433 LDAP_STAILQ_REMOVE_HEAD(&backendDB, be_next);
435 if ( bd->be_syncinfo ) {
436 syncinfo_free( bd->be_syncinfo );
439 if ( bd->be_pending_csn_list ) {
440 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
442 struct slap_csn_entry *tmp_csne = csne;
444 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
445 ch_free( csne->ce_csn.bv_val );
446 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
451 if ( bd->bd_info->bi_db_destroy ) {
452 bd->bd_info->bi_db_destroy( bd );
454 ber_bvarray_free( bd->be_suffix );
455 ber_bvarray_free( bd->be_nsuffix );
456 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
457 free( bd->be_rootdn.bv_val );
459 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
460 free( bd->be_rootndn.bv_val );
462 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
463 free( bd->be_rootpw.bv_val );
465 acl_destroy( bd->be_acl, frontendDB->be_acl );
469 /* destroy each backend type */
470 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
471 if( bi->bi_destroy ) {
472 bi->bi_destroy( bi );
477 LDAP_STAILQ_INIT(&backendInfo);
479 /* destroy frontend database */
482 if ( bd->bd_info->bi_db_destroy ) {
483 bd->bd_info->bi_db_destroy( bd );
485 ber_bvarray_free( bd->be_suffix );
486 ber_bvarray_free( bd->be_nsuffix );
487 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
488 free( bd->be_rootdn.bv_val );
490 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
491 free( bd->be_rootndn.bv_val );
493 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
494 free( bd->be_rootpw.bv_val );
496 acl_destroy( bd->be_acl, frontendDB->be_acl );
502 BackendInfo* backend_info(const char *type)
506 /* search for the backend type */
507 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
508 if( strcasecmp(bi->bi_type, type) == 0 ) {
522 BackendInfo *bi = backend_info(type);
526 fprintf( stderr, "Unrecognized database type (%s)\n", type );
530 be = ch_calloc( 1, sizeof(Backend) );
532 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
536 be->be_def_limit = frontendDB->be_def_limit;
537 be->be_dfltaccess = frontendDB->be_dfltaccess;
539 be->be_restrictops = frontendDB->be_restrictops;
540 be->be_requires = frontendDB->be_requires;
541 be->be_ssf_set = frontendDB->be_ssf_set;
543 be->be_pcl_mutexp = &be->be_pcl_mutex;
544 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
546 /* assign a default depth limit for alias deref */
547 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
549 if ( bi->bi_db_init ) {
550 rc = bi->bi_db_init( be );
554 fprintf( stderr, "database init failed (%s)\n", type );
569 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
570 if ( be->bd_info->bi_db_close ) {
571 be->bd_info->bi_db_close( be );
575 if ( frontendDB->bd_info->bi_db_close ) {
576 (*frontendDB->bd_info->bi_db_close)( frontendDB );
588 ber_len_t len, dnlen = dn->bv_len;
589 Backend *be, *b2 = NULL;
591 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
592 if ( be->be_nsuffix == NULL ) {
596 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
598 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
603 len = be->be_nsuffix[j].bv_len;
606 /* suffix is longer than DN */
611 * input DN is normalized, so the separator check
612 * need not look at escaping
614 if ( len && len < dnlen &&
615 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
620 if ( strcmp( be->be_nsuffix[j].bv_val,
621 &dn->bv_val[dnlen-len] ) == 0 )
626 if( manageDSAit && len == dnlen &&
627 !SLAP_GLUE_SUBORDINATE( be ) ) {
644 struct berval *bvsuffix )
648 if ( be->be_nsuffix == NULL ) {
652 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
653 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
662 be_isroot_dn( Backend *be, struct berval *ndn )
664 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
668 return dn_match( &be->be_rootndn, ndn );
672 be_slurp_update( Operation *op )
674 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
675 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
679 be_shadow_update( Operation *op )
681 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
682 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
686 be_isupdate_dn( Backend *be, struct berval *ndn )
688 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
692 return dn_match( &be->be_update_ndn, ndn );
696 be_root_dn( Backend *be )
698 return &be->be_rootdn;
702 be_isroot( Operation *op )
704 return be_isroot_dn( op->o_bd, &op->o_ndn );
708 be_isroot_pw( Operation *op )
712 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
716 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
721 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
722 op->o_conn->c_sasl_authctx, NULL );
725 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
728 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
740 if ( op->o_bd->be_release ) {
741 /* free and release entry from backend */
742 return op->o_bd->be_release( op, e, rw );
751 backend_unbind( Operation *op, SlapReply *rs )
756 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
757 #if defined( LDAP_SLAPI )
760 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
761 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
762 rc = slapi_int_call_plugins( be,
763 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
766 * A preoperation plugin failure will abort the
769 Debug(LDAP_DEBUG_TRACE,
770 "do_bind: Unbind preoperation plugin failed\n",
775 #endif /* defined( LDAP_SLAPI ) */
777 if ( be->be_unbind ) {
779 be->be_unbind( op, rs );
782 #if defined( LDAP_SLAPI )
783 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
784 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
786 Debug(LDAP_DEBUG_TRACE,
787 "do_unbind: Unbind postoperation plugins failed\n",
790 #endif /* defined( LDAP_SLAPI ) */
798 backend_connection_init(
803 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
804 if ( be->be_connection_init ) {
805 be->be_connection_init( be, conn);
813 backend_connection_destroy(
818 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
819 if ( be->be_connection_destroy ) {
820 be->be_connection_destroy( be, conn);
828 backend_check_controls(
832 LDAPControl **ctrls = op->o_ctrls;
833 rs->sr_err = LDAP_SUCCESS;
836 for( ; *ctrls != NULL ; ctrls++ ) {
839 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
840 case LDAP_CONTROL_NOT_FOUND:
841 /* unrecognized control */
842 if ( (*ctrls)->ldctl_iscritical ) {
843 /* should not be reachable */
844 Debug( LDAP_DEBUG_ANY,
845 "backend_check_controls: unrecognized control: %s\n",
846 (*ctrls)->ldctl_oid, 0, 0 );
851 case LDAP_COMPARE_FALSE:
852 if ( !op->o_bd->be_ctrls[ cid ] )
854 /* Per RFC 2251 (and LDAPBIS discussions), if the control
855 * is recognized and appropriate for the operation (which
856 * we've already verified), then the server should make
857 * use of the control when performing the operation.
859 * Here we find that operation extended by the control
860 * is not unavailable in a particular context, hence the
861 * return of unwillingToPerform.
863 rs->sr_text = "control unavailable in context";
864 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
869 case LDAP_COMPARE_TRUE:
874 rs->sr_text = "unable to check control";
875 rs->sr_err = LDAP_OTHER;
886 backend_check_restrictions(
889 struct berval *opdata )
891 slap_mask_t restrictops;
892 slap_mask_t requires;
894 slap_mask_t exopflag = 0;
901 int rc = SLAP_CB_CONTINUE;
903 if ( op->o_bd->be_chk_controls ) {
904 rc = ( *op->o_bd->be_chk_controls )( op, rs );
907 if ( rc == SLAP_CB_CONTINUE ) {
908 rc = backend_check_controls( op, rs );
911 if ( rc != LDAP_SUCCESS ) {
915 restrictops = op->o_bd->be_restrictops;
916 requires = op->o_bd->be_requires;
917 ssf = &op->o_bd->be_ssf_set;
920 restrictops = frontendDB->be_restrictops;
921 requires = frontendDB->be_requires;
922 ssf = &frontendDB->be_ssf_set;
925 switch( op->o_tag ) {
927 opflag = SLAP_RESTRICT_OP_ADD;
931 opflag = SLAP_RESTRICT_OP_BIND;
934 case LDAP_REQ_COMPARE:
935 opflag = SLAP_RESTRICT_OP_COMPARE;
937 case LDAP_REQ_DELETE:
939 opflag = SLAP_RESTRICT_OP_DELETE;
941 case LDAP_REQ_EXTENDED:
942 opflag = SLAP_RESTRICT_OP_EXTENDED;
945 /* treat unspecified as a modify */
946 opflag = SLAP_RESTRICT_OP_MODIFY;
951 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
954 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
958 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
959 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
963 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
964 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
968 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
969 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
974 /* treat everything else as a modify */
975 opflag = SLAP_RESTRICT_OP_MODIFY;
979 case LDAP_REQ_MODIFY:
981 opflag = SLAP_RESTRICT_OP_MODIFY;
983 case LDAP_REQ_RENAME:
985 opflag = SLAP_RESTRICT_OP_RENAME;
987 case LDAP_REQ_SEARCH:
988 opflag = SLAP_RESTRICT_OP_SEARCH;
990 case LDAP_REQ_UNBIND:
995 rs->sr_text = "restrict operations internal error";
996 rs->sr_err = LDAP_OTHER;
1001 /* these checks don't apply to StartTLS */
1003 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1004 if( op->o_transport_ssf < ssf->sss_transport ) {
1005 rs->sr_text = op->o_transport_ssf
1006 ? "stronger transport confidentiality required"
1007 : "transport confidentiality required";
1011 if( op->o_tls_ssf < ssf->sss_tls ) {
1012 rs->sr_text = op->o_tls_ssf
1013 ? "stronger TLS confidentiality required"
1014 : "TLS confidentiality required";
1019 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1020 /* simple bind specific check */
1021 if( op->o_ssf < ssf->sss_simple_bind ) {
1022 rs->sr_text = op->o_ssf
1023 ? "stronger confidentiality required"
1024 : "confidentiality required";
1029 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1030 /* these checks don't apply to SASL bind */
1032 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1033 rs->sr_text = op->o_sasl_ssf
1034 ? "stronger SASL confidentiality required"
1035 : "SASL confidentiality required";
1039 if( op->o_ssf < ssf->sss_ssf ) {
1040 rs->sr_text = op->o_ssf
1041 ? "stronger confidentiality required"
1042 : "confidentiality required";
1048 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1049 rs->sr_text = op->o_transport_ssf
1050 ? "stronger transport confidentiality required for update"
1051 : "transport confidentiality required for update";
1055 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1056 rs->sr_text = op->o_tls_ssf
1057 ? "stronger TLS confidentiality required for update"
1058 : "TLS confidentiality required for update";
1062 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1063 rs->sr_text = op->o_sasl_ssf
1064 ? "stronger SASL confidentiality required for update"
1065 : "SASL confidentiality required for update";
1069 if( op->o_ssf < ssf->sss_update_ssf ) {
1070 rs->sr_text = op->o_ssf
1071 ? "stronger confidentiality required for update"
1072 : "confidentiality required for update";
1076 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1077 BER_BVISEMPTY( &op->o_ndn ) )
1079 rs->sr_text = "modifications require authentication";
1080 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1084 #ifdef SLAP_X_LISTENER_MOD
1085 if ( op->o_conn->c_listener &&
1086 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1087 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1089 /* no "w" mode means readonly */
1090 rs->sr_text = "modifications not allowed on this listener";
1091 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1094 #endif /* SLAP_X_LISTENER_MOD */
1099 /* these checks don't apply to Bind, StartTLS, or Unbind */
1101 if( requires & SLAP_REQUIRE_STRONG ) {
1102 /* should check mechanism */
1103 if( ( op->o_transport_ssf < ssf->sss_transport
1104 && op->o_authtype == LDAP_AUTH_SIMPLE )
1105 || BER_BVISEMPTY( &op->o_dn ) )
1107 rs->sr_text = "strong(er) authentication required";
1108 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1113 if( requires & SLAP_REQUIRE_SASL ) {
1114 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1115 rs->sr_text = "SASL authentication required";
1116 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1121 if( requires & SLAP_REQUIRE_AUTHC ) {
1122 if( BER_BVISEMPTY( &op->o_dn ) ) {
1123 rs->sr_text = "authentication required";
1124 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1129 if( requires & SLAP_REQUIRE_BIND ) {
1131 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1132 version = op->o_conn->c_protocol;
1133 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1136 /* no bind has occurred */
1137 rs->sr_text = "BIND required";
1138 rs->sr_err = LDAP_OPERATIONS_ERROR;
1143 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1144 if( op->o_protocol < LDAP_VERSION3 ) {
1145 /* no bind has occurred */
1146 rs->sr_text = "operation restricted to LDAPv3 clients";
1147 rs->sr_err = LDAP_OPERATIONS_ERROR;
1152 #ifdef SLAP_X_LISTENER_MOD
1153 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1154 if ( op->o_conn->c_listener &&
1155 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1157 /* no "x" mode means bind required */
1158 rs->sr_text = "bind required on this listener";
1159 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1164 if ( !starttls && !updateop ) {
1165 if ( op->o_conn->c_listener &&
1166 !( op->o_conn->c_listener->sl_perms &
1167 ( !BER_BVISEMPTY( &op->o_dn )
1168 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1170 /* no "r" mode means no read */
1171 rs->sr_text = "read not allowed on this listener";
1172 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1176 #endif /* SLAP_X_LISTENER_MOD */
1180 if( ( restrictops & opflag )
1181 || ( exopflag && ( restrictops & exopflag ) ) ) {
1182 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1183 rs->sr_text = "read operations restricted";
1184 } else if ( restrictops & exopflag ) {
1185 rs->sr_text = "extended operation restricted";
1187 rs->sr_text = "operation restricted";
1189 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1193 rs->sr_err = LDAP_SUCCESS;
1197 int backend_check_referrals( Operation *op, SlapReply *rs )
1199 rs->sr_err = LDAP_SUCCESS;
1201 if( op->o_bd->be_chk_referrals ) {
1202 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1204 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1205 send_ldap_result( op, rs );
1217 AttributeDescription *at,
1225 if (op->o_bd == NULL) {
1226 rc = LDAP_NO_SUCH_OBJECT;
1227 } else if ( op->o_bd->be_fetch ) {
1228 rc = ( op->o_bd->be_fetch )( op, ndn,
1231 rc = LDAP_UNWILLING_TO_PERFORM;
1240 struct berval *gr_ndn,
1241 struct berval *op_ndn,
1242 ObjectClass *group_oc,
1243 AttributeDescription *group_at )
1249 Backend *be = op->o_bd;
1251 if ( op->o_abandon ) return SLAPD_ABANDON;
1253 op->o_bd = select_backend( gr_ndn, 0, 0 );
1255 for ( g = op->o_groups; g; g = g->ga_next ) {
1256 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1257 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1261 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1271 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1275 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1279 if ( op->o_pb != NULL ) {
1280 init_group_pblock( op, target, e, op_ndn, group_at );
1282 rc = call_group_preop_plugins( op );
1283 if ( rc == LDAP_SUCCESS ) {
1287 #endif /* LDAP_SLAPI */
1289 a = attr_find( e->e_attrs, group_at );
1291 /* If the attribute is a subtype of labeledURI, treat this as
1292 * a dynamic group ala groupOfURLs
1294 if (is_at_subtype( group_at->ad_type,
1295 slap_schema.si_ad_labeledURI->ad_type ) )
1299 struct berval bv, nbase;
1302 Backend *b2 = op->o_bd;
1304 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1307 op->o_bd = select_backend( op_ndn, 0, 0 );
1308 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1313 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1314 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1319 BER_BVZERO( &nbase );
1320 /* host part must be empty */
1321 /* attrs and extensions parts must be empty */
1322 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1323 ludp->lud_attrs || ludp->lud_exts )
1327 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1328 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1329 op->o_tmpmemctx ) != LDAP_SUCCESS )
1333 switch ( ludp->lud_scope ) {
1334 case LDAP_SCOPE_BASE:
1335 if ( !dn_match( &nbase, op_ndn ) ) {
1339 case LDAP_SCOPE_ONELEVEL:
1340 dnParent( op_ndn, &bv );
1341 if ( !dn_match( &nbase, &bv ) ) {
1345 case LDAP_SCOPE_SUBTREE:
1346 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1350 #ifdef LDAP_SCOPE_SUBORDINATE
1351 case LDAP_SCOPE_SUBORDINATE:
1352 if ( dn_match( &nbase, op_ndn ) ||
1353 !dnIsSuffix( op_ndn, &nbase ) )
1359 filter = str2filter_x( op, ludp->lud_filter );
1361 if ( test_filter( NULL, user, filter ) ==
1366 filter_free_x( op, filter );
1369 ldap_free_urldesc( ludp );
1370 if ( !BER_BVISNULL( &nbase ) ) {
1371 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1373 if ( rc == 0 ) break;
1375 if ( user != target ) {
1376 be_entry_release_r( op, user );
1381 rc = value_find_ex( group_at,
1382 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1383 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1384 a->a_nvals, op_ndn, op->o_tmpmemctx );
1387 rc = LDAP_NO_SUCH_ATTRIBUTE;
1390 be_entry_release_r( op, e );
1393 rc = LDAP_NO_SUCH_OBJECT;
1397 if ( op->o_pb ) call_group_postop_plugins( op );
1398 #endif /* LDAP_SLAPI */
1400 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1401 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1403 g->ga_be = op->o_bd;
1404 g->ga_oc = group_oc;
1405 g->ga_at = group_at;
1407 g->ga_len = gr_ndn->bv_len;
1408 strcpy( g->ga_ndn, gr_ndn->bv_val );
1409 g->ga_next = op->o_groups;
1418 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1422 BerVarray *vals = (BerVarray *)c->cac_private;
1423 Operation *op = NULL;
1426 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1431 if ( op->o_conn && access_allowed( op,
1432 e, a->a_desc, NULL, ACL_AUTH,
1433 &c->cac_acl_state ) == 0 ) {
1437 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1439 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1441 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1442 if ( op->o_conn && access_allowed( op,
1445 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1449 &a->a_nvals[i], op->o_tmpmemctx );
1450 if ( !BER_BVISNULL( &v[j] ) ) {
1456 op->o_tmpfree( v, op->o_tmpmemctx );
1460 BER_BVZERO( &v[j] );
1467 #endif /* LDAP_SLAPI */
1474 AttributeDescription *entry_at,
1476 slap_access_t access )
1479 Attribute *a = NULL;
1480 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1481 AccessControlState acl_state = ACL_STATE_INIT;
1482 Backend *be = op->o_bd;
1484 op->o_bd = select_backend( edn, 0, 0 );
1486 if ( target && dn_match( &target->e_nname, edn ) ) {
1490 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1494 a = attr_find( e->e_attrs, entry_at );
1496 SlapReply rs = { 0 };
1497 AttributeName anlist[ 2 ];
1499 anlist[ 0 ].an_name = entry_at->ad_cname;
1500 anlist[ 0 ].an_desc = entry_at;
1501 BER_BVZERO( &anlist[ 1 ].an_name );
1502 rs.sr_attrs = anlist;
1504 /* NOTE: backend_operational() is also called
1505 * when returning results, so it's supposed
1506 * to do no harm to entries */
1508 rc = backend_operational( op, &rs );
1511 if ( rc == LDAP_SUCCESS ) {
1512 if ( rs.sr_operational_attrs ) {
1514 a = rs.sr_operational_attrs;
1517 rc = LDAP_NO_SUCH_ATTRIBUTE;
1525 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1526 e, entry_at, NULL, access,
1527 &acl_state ) == 0 ) {
1528 rc = LDAP_INSUFFICIENT_ACCESS;
1532 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1535 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1537 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1539 if ( op->o_conn && access > ACL_NONE &&
1540 access_allowed( op, e,
1548 ber_dupbv_x( &v[j], &a->a_nvals[i],
1550 if ( !BER_BVISNULL( &v[j] ) ) {
1555 op->o_tmpfree( v, op->o_tmpmemctx );
1557 rc = LDAP_INSUFFICIENT_ACCESS;
1560 BER_BVZERO( &v[j] );
1566 else if ( op->o_pb ) {
1567 /* try any computed attributes */
1568 computed_attr_context ctx;
1570 slapi_int_pblock_set_operation( op->o_pb, op );
1572 ctx.cac_pb = op->o_pb;
1573 ctx.cac_attrs = NULL;
1574 ctx.cac_userattrs = 0;
1575 ctx.cac_opattrs = 0;
1576 ctx.cac_acl_state = acl_state;
1577 ctx.cac_private = (void *)vals;
1579 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1581 rc = LDAP_INSUFFICIENT_ACCESS;
1587 #endif /* LDAP_SLAPI */
1588 freeit: if ( e != target ) {
1589 be_entry_release_r( op, e );
1601 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1603 struct berval *nval = (struct berval *)c->cac_private;
1604 Operation *op = NULL;
1606 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1611 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1613 #endif /* LDAP_SLAPI */
1620 AttributeDescription *entry_at,
1621 struct berval *nval,
1622 slap_access_t access,
1626 int rc = LDAP_INSUFFICIENT_ACCESS;
1627 Backend *be = op->o_bd;
1631 assert( op->o_conn );
1633 assert( access > ACL_NONE );
1635 op->o_bd = select_backend( edn, 0, 0 );
1637 if ( target && dn_match( &target->e_nname, edn ) ) {
1641 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1645 Attribute *a = NULL;
1648 if ( entry_at == NULL ) {
1649 entry_at = slap_schema.si_ad_entry;
1652 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1654 if ( access_allowed_mask( op, e, entry_at,
1655 NULL, access, NULL, mask ) == 0 )
1657 rc = LDAP_INSUFFICIENT_ACCESS;
1664 a = attr_find( e->e_attrs, entry_at );
1666 SlapReply rs = { 0 };
1667 AttributeName anlist[ 2 ];
1669 anlist[ 0 ].an_name = entry_at->ad_cname;
1670 anlist[ 0 ].an_desc = entry_at;
1671 BER_BVZERO( &anlist[ 1 ].an_name );
1672 rs.sr_attrs = anlist;
1674 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1676 /* NOTE: backend_operational() is also called
1677 * when returning results, so it's supposed
1678 * to do no harm to entries */
1680 rc = backend_operational( op, &rs );
1683 if ( rc == LDAP_SUCCESS ) {
1684 if ( rs.sr_operational_attrs ) {
1686 a = rs.sr_operational_attrs;
1689 rc = LDAP_NO_SUCH_OBJECT;
1695 if ( access_allowed_mask( op, e, entry_at,
1696 nval, access, NULL, mask ) == 0 )
1698 rc = LDAP_INSUFFICIENT_ACCESS;
1704 else if ( op->o_pb ) {
1705 /* try any computed attributes */
1706 computed_attr_context ctx;
1708 slapi_int_pblock_set_operation( op->o_pb, op );
1710 ctx.cac_pb = op->o_pb;
1711 ctx.cac_attrs = NULL;
1712 ctx.cac_userattrs = 0;
1713 ctx.cac_opattrs = 0;
1714 ctx.cac_private = (void *)nval;
1716 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1718 rc = LDAP_INSUFFICIENT_ACCESS;
1724 #endif /* LDAP_SLAPI */
1726 freeit: if ( e != target ) {
1727 be_entry_release_r( op, e );
1738 int backend_operational(
1746 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1747 /* just count them */ ;
1750 * If operational attributes (allegedly) are required,
1751 * and the backend supports specific operational attributes,
1752 * add them to the attribute list
1754 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1755 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1757 *ap = slap_operational_entryDN( rs->sr_entry );
1758 ap = &(*ap)->a_next;
1761 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1762 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1764 *ap = slap_operational_subschemaSubentry( op->o_bd );
1765 ap = &(*ap)->a_next;
1768 /* Let the overlays have a chance at this */
1770 if ( SLAP_ISOVERLAY( be_orig ))
1771 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1773 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1774 op->o_bd && op->o_bd->be_operational != NULL )
1776 rc = op->o_bd->be_operational( op, rs );
1784 static void init_group_pblock( Operation *op, Entry *target,
1785 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1787 slapi_int_pblock_set_operation( op->o_pb, op );
1789 slapi_pblock_set( op->o_pb,
1790 SLAPI_X_GROUP_ENTRY, (void *)e );
1791 slapi_pblock_set( op->o_pb,
1792 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1793 slapi_pblock_set( op->o_pb,
1794 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1795 slapi_pblock_set( op->o_pb,
1796 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1799 static int call_group_preop_plugins( Operation *op )
1803 rc = slapi_int_call_plugins( op->o_bd,
1804 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1806 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1807 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1809 rc = LDAP_NO_SUCH_ATTRIBUTE;
1818 static void call_group_postop_plugins( Operation *op )
1820 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1822 #endif /* LDAP_SLAPI */