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>
41 #include "slapi/slapi.h"
43 static void init_group_pblock( Operation *op, Entry *target,
44 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
45 static int call_group_preop_plugins( Operation *op );
46 static void call_group_postop_plugins( Operation *op );
47 #endif /* LDAP_SLAPI */
50 * If a module is configured as dynamic, its header should not
51 * get included into slapd. While this is a general rule and does
52 * not have much of an effect in UNIX, this rule should be adhered
53 * to for Windows, where dynamic object code should not be implicitly
54 * imported into slapd without appropriate __declspec(dllimport) directives.
58 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
61 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
64 backend_init_controls( BackendInfo *bi )
66 if ( bi->bi_controls ) {
69 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
72 if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
73 == LDAP_CONTROL_NOT_FOUND )
75 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
82 bi->bi_ctrls[ cid ] = 1;
89 int backend_init(void)
94 if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
95 /* already initialized */
96 Debug( LDAP_DEBUG_ANY,
97 "backend_init: already initialized\n", 0, 0, 0 );
101 for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
102 assert( bi->bi_init != 0 );
104 rc = bi->bi_init( bi );
107 Debug( LDAP_DEBUG_ANY,
108 "backend_init: initialized for type \"%s\"\n",
110 /* destroy those we've already inited */
115 if ( slap_binfo[nBackendInfo].bi_destroy ) {
116 slap_binfo[nBackendInfo].bi_destroy(
117 &slap_binfo[nBackendInfo] );
123 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
126 if ( nBackendInfo > 0) {
134 Debug( LDAP_DEBUG_ANY,
135 "backend_init: failed\n",
139 #endif /* SLAPD_MODULES */
142 int backend_add(BackendInfo *aBackendInfo)
146 if ( aBackendInfo->bi_init == NULL ) {
147 Debug( LDAP_DEBUG_ANY, "backend_add: "
148 "backend type \"%s\" does not have the (mandatory)init function\n",
149 aBackendInfo->bi_type, 0, 0 );
153 rc = aBackendInfo->bi_init(aBackendInfo);
155 Debug( LDAP_DEBUG_ANY,
156 "backend_add: initialization for type \"%s\" failed\n",
157 aBackendInfo->bi_type, 0, 0 );
161 (void)backend_init_controls( aBackendInfo );
163 /* now add the backend type to the Backend Info List */
164 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
170 backend_set_controls( BackendDB *be )
172 BackendInfo *bi = be->bd_info;
174 /* back-relay takes care of itself; so may do other */
175 if ( overlay_is_over( be ) ) {
176 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
179 if ( bi->bi_controls ) {
180 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
181 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
182 sizeof( be->be_ctrls ) );
183 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
188 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
189 if ( bi->bi_ctrls[ i ] ) {
190 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
200 /* startup a specific backend database */
201 int backend_startup_one(Backend *be)
205 assert( be != NULL );
207 be->be_pending_csn_list = (struct be_pcl *)
208 ch_calloc( 1, sizeof( struct be_pcl ));
210 LDAP_TAILQ_INIT( be->be_pending_csn_list );
212 Debug( LDAP_DEBUG_TRACE,
213 "backend_startup_one: starting \"%s\"\n",
214 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
217 /* set database controls */
218 (void)backend_set_controls( be );
220 if ( be->bd_info->bi_db_open ) {
221 rc = be->bd_info->bi_db_open( be );
223 (void)backend_set_controls( be );
226 Debug( LDAP_DEBUG_ANY,
227 "backend_startup_one: bi_db_open failed! (%d)\n",
235 int backend_startup(Backend *be)
241 if( ! ( nBackendDB > 0 ) ) {
243 Debug( LDAP_DEBUG_ANY,
244 "backend_startup: %d databases to startup.\n",
250 if ( be->bd_info->bi_open ) {
251 rc = be->bd_info->bi_open( be->bd_info );
253 Debug( LDAP_DEBUG_ANY,
254 "backend_startup: bi_open failed!\n",
261 return backend_startup_one( be );
264 /* open frontend, if required */
265 if ( frontendDB->bd_info->bi_db_open ) {
266 rc = frontendDB->bd_info->bi_db_open( frontendDB );
268 Debug( LDAP_DEBUG_ANY,
269 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
275 /* open each backend type */
277 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
279 if( bi->bi_nDB == 0) {
280 /* no database of this type, don't open */
285 rc = bi->bi_open( bi );
287 Debug( LDAP_DEBUG_ANY,
288 "backend_startup: bi_open %d (%s) failed!\n",
294 (void)backend_init_controls( bi );
297 /* open each backend database */
299 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
301 if ( be->be_suffix == NULL ) {
302 Debug( LDAP_DEBUG_ANY,
303 "backend_startup: warning, database %d (%s) "
305 i, be->bd_info->bi_type, 0 );
307 /* append global access controls */
308 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
310 rc = backend_startup_one( be );
318 int backend_num( Backend *be )
323 if( be == NULL ) return -1;
325 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
326 if( be == b2 ) return i;
332 int backend_shutdown( Backend *be )
338 /* shutdown a specific backend database */
340 if ( be->bd_info->bi_nDB == 0 ) {
341 /* no database of this type, we never opened it */
345 if ( be->bd_info->bi_db_close ) {
346 be->bd_info->bi_db_close( be );
349 if( be->bd_info->bi_close ) {
350 be->bd_info->bi_close( be->bd_info );
356 /* close each backend database */
357 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
358 if ( be->bd_info->bi_db_close ) {
359 be->bd_info->bi_db_close( be );
363 Debug( LDAP_DEBUG_ANY,
364 "backend_close: bi_db_close %s failed!\n",
369 /* close each backend type */
370 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
371 if( bi->bi_nDB == 0 ) {
372 /* no database of this type */
381 /* close frontend, if required */
382 if ( frontendDB->bd_info->bi_db_close ) {
383 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
385 Debug( LDAP_DEBUG_ANY,
386 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
394 void backend_destroy_one( BackendDB *bd )
396 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
398 if ( bd->be_syncinfo ) {
399 syncinfo_free( bd->be_syncinfo );
402 if ( bd->be_pending_csn_list ) {
403 struct slap_csn_entry *csne;
404 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
406 struct slap_csn_entry *tmp_csne = csne;
408 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
409 ch_free( csne->ce_csn.bv_val );
410 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
415 if ( bd->bd_info->bi_db_destroy ) {
416 bd->bd_info->bi_db_destroy( bd );
418 ber_bvarray_free( bd->be_suffix );
419 ber_bvarray_free( bd->be_nsuffix );
420 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
421 free( bd->be_rootdn.bv_val );
423 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
424 free( bd->be_rootndn.bv_val );
426 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
427 free( bd->be_rootpw.bv_val );
429 acl_destroy( bd->be_acl, frontendDB->be_acl );
433 int backend_destroy(void)
438 /* destroy each backend database */
439 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
440 backend_destroy_one( bd );
443 /* destroy each backend type */
444 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
445 if( bi->bi_destroy ) {
446 bi->bi_destroy( bi );
451 LDAP_STAILQ_INIT(&backendInfo);
453 /* destroy frontend database */
456 if ( bd->bd_info->bi_db_destroy ) {
457 bd->bd_info->bi_db_destroy( bd );
459 ber_bvarray_free( bd->be_suffix );
460 ber_bvarray_free( bd->be_nsuffix );
461 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
462 free( bd->be_rootdn.bv_val );
464 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
465 free( bd->be_rootndn.bv_val );
467 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
468 free( bd->be_rootpw.bv_val );
470 acl_destroy( bd->be_acl, frontendDB->be_acl );
476 BackendInfo* backend_info(const char *type)
480 /* search for the backend type */
481 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
482 if( strcasecmp(bi->bi_type, type) == 0 ) {
496 BackendInfo *bi = backend_info(type);
500 fprintf( stderr, "Unrecognized database type (%s)\n", type );
504 be = ch_calloc( 1, sizeof(Backend) );
506 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
510 be->be_def_limit = frontendDB->be_def_limit;
511 be->be_dfltaccess = frontendDB->be_dfltaccess;
513 be->be_restrictops = frontendDB->be_restrictops;
514 be->be_requires = frontendDB->be_requires;
515 be->be_ssf_set = frontendDB->be_ssf_set;
517 be->be_pcl_mutexp = &be->be_pcl_mutex;
518 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
520 /* assign a default depth limit for alias deref */
521 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
523 if ( bi->bi_db_init ) {
524 rc = bi->bi_db_init( be );
528 fprintf( stderr, "database init failed (%s)\n", type );
542 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
543 if ( be->bd_info->bi_db_close ) {
544 be->bd_info->bi_db_close( be );
548 if ( frontendDB->bd_info->bi_db_close ) {
549 (*frontendDB->bd_info->bi_db_close)( frontendDB );
561 ber_len_t len, dnlen = dn->bv_len;
562 Backend *be, *b2 = NULL;
564 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
565 if ( be->be_nsuffix == NULL ) {
569 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
571 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
576 len = be->be_nsuffix[j].bv_len;
579 /* suffix is longer than DN */
584 * input DN is normalized, so the separator check
585 * need not look at escaping
587 if ( len && len < dnlen &&
588 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
593 if ( strcmp( be->be_nsuffix[j].bv_val,
594 &dn->bv_val[dnlen-len] ) == 0 )
599 if( manageDSAit && len == dnlen &&
600 !SLAP_GLUE_SUBORDINATE( be ) ) {
617 struct berval *bvsuffix )
621 if ( be->be_nsuffix == NULL ) {
625 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
626 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
635 be_isroot_dn( Backend *be, struct berval *ndn )
637 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
641 return dn_match( &be->be_rootndn, ndn );
645 be_slurp_update( Operation *op )
647 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
648 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
652 be_shadow_update( Operation *op )
654 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
655 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
659 be_isupdate_dn( Backend *be, struct berval *ndn )
661 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
665 return dn_match( &be->be_update_ndn, ndn );
669 be_root_dn( Backend *be )
671 return &be->be_rootdn;
675 be_isroot( Operation *op )
677 return be_isroot_dn( op->o_bd, &op->o_ndn );
681 be_isroot_pw( Operation *op )
685 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
689 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
694 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
695 op->o_conn->c_sasl_authctx, NULL );
698 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
701 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
713 if ( op->o_bd->be_release ) {
714 /* free and release entry from backend */
715 return op->o_bd->be_release( op, e, rw );
724 backend_unbind( Operation *op, SlapReply *rs )
729 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
730 #if defined( LDAP_SLAPI )
733 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
734 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
735 rc = slapi_int_call_plugins( be,
736 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
739 * A preoperation plugin failure will abort the
742 Debug(LDAP_DEBUG_TRACE,
743 "do_bind: Unbind preoperation plugin failed\n",
748 #endif /* defined( LDAP_SLAPI ) */
750 if ( be->be_unbind ) {
752 be->be_unbind( op, rs );
755 #if defined( LDAP_SLAPI )
756 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
757 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
759 Debug(LDAP_DEBUG_TRACE,
760 "do_unbind: Unbind postoperation plugins failed\n",
763 #endif /* defined( LDAP_SLAPI ) */
771 backend_connection_init(
776 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
777 if ( be->be_connection_init ) {
778 be->be_connection_init( be, conn );
786 backend_connection_destroy(
791 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
792 if ( be->be_connection_destroy ) {
793 be->be_connection_destroy( be, conn);
801 backend_check_controls(
805 LDAPControl **ctrls = op->o_ctrls;
806 rs->sr_err = LDAP_SUCCESS;
809 for( ; *ctrls != NULL ; ctrls++ ) {
812 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
813 case LDAP_CONTROL_NOT_FOUND:
814 /* unrecognized control */
815 if ( (*ctrls)->ldctl_iscritical ) {
816 /* should not be reachable */
817 Debug( LDAP_DEBUG_ANY,
818 "backend_check_controls: unrecognized control: %s\n",
819 (*ctrls)->ldctl_oid, 0, 0 );
824 case LDAP_COMPARE_FALSE:
825 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
826 /* Per RFC 2251 (and LDAPBIS discussions), if the control
827 * is recognized and appropriate for the operation (which
828 * we've already verified), then the server should make
829 * use of the control when performing the operation.
831 * Here we find that operation extended by the control
832 * is unavailable in a particular context, and the control
833 * is marked Critical, hence the return of
834 * unwillingToPerform.
836 rs->sr_text = "critical control unavailable in context";
837 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
842 case LDAP_COMPARE_TRUE:
847 Debug( LDAP_DEBUG_ANY,
848 "backend_check_controls: unable to check control: %s\n",
849 (*ctrls)->ldctl_oid, 0, 0 );
852 rs->sr_text = "unable to check control";
853 rs->sr_err = LDAP_OTHER;
859 /* check should be generalized */
860 if( get_manageDIT(op) && !be_isroot(op)) {
861 rs->sr_text = "requires manager authorization";
862 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
870 backend_check_restrictions(
873 struct berval *opdata )
875 slap_mask_t restrictops;
876 slap_mask_t requires;
878 slap_mask_t exopflag = 0;
885 int rc = SLAP_CB_CONTINUE;
887 if ( op->o_bd->be_chk_controls ) {
888 rc = ( *op->o_bd->be_chk_controls )( op, rs );
891 if ( rc == SLAP_CB_CONTINUE ) {
892 rc = backend_check_controls( op, rs );
895 if ( rc != LDAP_SUCCESS ) {
899 restrictops = op->o_bd->be_restrictops;
900 requires = op->o_bd->be_requires;
901 ssf = &op->o_bd->be_ssf_set;
904 restrictops = frontendDB->be_restrictops;
905 requires = frontendDB->be_requires;
906 ssf = &frontendDB->be_ssf_set;
909 switch( op->o_tag ) {
911 opflag = SLAP_RESTRICT_OP_ADD;
915 opflag = SLAP_RESTRICT_OP_BIND;
918 case LDAP_REQ_COMPARE:
919 opflag = SLAP_RESTRICT_OP_COMPARE;
921 case LDAP_REQ_DELETE:
923 opflag = SLAP_RESTRICT_OP_DELETE;
925 case LDAP_REQ_EXTENDED:
926 opflag = SLAP_RESTRICT_OP_EXTENDED;
929 /* treat unspecified as a modify */
930 opflag = SLAP_RESTRICT_OP_MODIFY;
935 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
938 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
942 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
943 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
947 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
948 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
952 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
953 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
958 /* treat everything else as a modify */
959 opflag = SLAP_RESTRICT_OP_MODIFY;
963 case LDAP_REQ_MODIFY:
965 opflag = SLAP_RESTRICT_OP_MODIFY;
967 case LDAP_REQ_RENAME:
969 opflag = SLAP_RESTRICT_OP_RENAME;
971 case LDAP_REQ_SEARCH:
972 opflag = SLAP_RESTRICT_OP_SEARCH;
974 case LDAP_REQ_UNBIND:
979 rs->sr_text = "restrict operations internal error";
980 rs->sr_err = LDAP_OTHER;
985 /* these checks don't apply to StartTLS */
987 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
988 if( op->o_transport_ssf < ssf->sss_transport ) {
989 rs->sr_text = op->o_transport_ssf
990 ? "stronger transport confidentiality required"
991 : "transport confidentiality required";
995 if( op->o_tls_ssf < ssf->sss_tls ) {
996 rs->sr_text = op->o_tls_ssf
997 ? "stronger TLS confidentiality required"
998 : "TLS confidentiality required";
1003 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1004 /* simple bind specific check */
1005 if( op->o_ssf < ssf->sss_simple_bind ) {
1006 rs->sr_text = op->o_ssf
1007 ? "stronger confidentiality required"
1008 : "confidentiality required";
1013 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1014 /* these checks don't apply to SASL bind */
1016 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1017 rs->sr_text = op->o_sasl_ssf
1018 ? "stronger SASL confidentiality required"
1019 : "SASL confidentiality required";
1023 if( op->o_ssf < ssf->sss_ssf ) {
1024 rs->sr_text = op->o_ssf
1025 ? "stronger confidentiality required"
1026 : "confidentiality required";
1032 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1033 rs->sr_text = op->o_transport_ssf
1034 ? "stronger transport confidentiality required for update"
1035 : "transport confidentiality required for update";
1039 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1040 rs->sr_text = op->o_tls_ssf
1041 ? "stronger TLS confidentiality required for update"
1042 : "TLS confidentiality required for update";
1046 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1047 rs->sr_text = op->o_sasl_ssf
1048 ? "stronger SASL confidentiality required for update"
1049 : "SASL confidentiality required for update";
1053 if( op->o_ssf < ssf->sss_update_ssf ) {
1054 rs->sr_text = op->o_ssf
1055 ? "stronger confidentiality required for update"
1056 : "confidentiality required for update";
1060 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1061 BER_BVISEMPTY( &op->o_ndn ) )
1063 rs->sr_text = "modifications require authentication";
1064 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1068 #ifdef SLAP_X_LISTENER_MOD
1069 if ( op->o_conn->c_listener &&
1070 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1071 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1073 /* no "w" mode means readonly */
1074 rs->sr_text = "modifications not allowed on this listener";
1075 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1078 #endif /* SLAP_X_LISTENER_MOD */
1083 /* these checks don't apply to Bind, StartTLS, or Unbind */
1085 if( requires & SLAP_REQUIRE_STRONG ) {
1086 /* should check mechanism */
1087 if( ( op->o_transport_ssf < ssf->sss_transport
1088 && op->o_authtype == LDAP_AUTH_SIMPLE )
1089 || BER_BVISEMPTY( &op->o_dn ) )
1091 rs->sr_text = "strong(er) authentication required";
1092 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1097 if( requires & SLAP_REQUIRE_SASL ) {
1098 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1099 rs->sr_text = "SASL authentication required";
1100 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1105 if( requires & SLAP_REQUIRE_AUTHC ) {
1106 if( BER_BVISEMPTY( &op->o_dn ) ) {
1107 rs->sr_text = "authentication required";
1108 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1113 if( requires & SLAP_REQUIRE_BIND ) {
1115 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1116 version = op->o_conn->c_protocol;
1117 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1120 /* no bind has occurred */
1121 rs->sr_text = "BIND required";
1122 rs->sr_err = LDAP_OPERATIONS_ERROR;
1127 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1128 if( op->o_protocol < LDAP_VERSION3 ) {
1129 /* no bind has occurred */
1130 rs->sr_text = "operation restricted to LDAPv3 clients";
1131 rs->sr_err = LDAP_OPERATIONS_ERROR;
1136 #ifdef SLAP_X_LISTENER_MOD
1137 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1138 if ( op->o_conn->c_listener &&
1139 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1141 /* no "x" mode means bind required */
1142 rs->sr_text = "bind required on this listener";
1143 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1148 if ( !starttls && !updateop ) {
1149 if ( op->o_conn->c_listener &&
1150 !( op->o_conn->c_listener->sl_perms &
1151 ( !BER_BVISEMPTY( &op->o_dn )
1152 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1154 /* no "r" mode means no read */
1155 rs->sr_text = "read not allowed on this listener";
1156 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1160 #endif /* SLAP_X_LISTENER_MOD */
1164 if( ( restrictops & opflag )
1165 || ( exopflag && ( restrictops & exopflag ) ) ) {
1166 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1167 rs->sr_text = "read operations restricted";
1168 } else if ( restrictops & exopflag ) {
1169 rs->sr_text = "extended operation restricted";
1171 rs->sr_text = "operation restricted";
1173 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1177 rs->sr_err = LDAP_SUCCESS;
1181 int backend_check_referrals( Operation *op, SlapReply *rs )
1183 rs->sr_err = LDAP_SUCCESS;
1185 if( op->o_bd->be_chk_referrals ) {
1186 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1188 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1189 send_ldap_result( op, rs );
1201 AttributeDescription *at,
1209 if (op->o_bd == NULL) {
1210 rc = LDAP_NO_SUCH_OBJECT;
1211 } else if ( op->o_bd->be_fetch ) {
1212 rc = ( op->o_bd->be_fetch )( op, ndn,
1215 rc = LDAP_UNWILLING_TO_PERFORM;
1224 struct berval *gr_ndn,
1225 struct berval *op_ndn,
1226 ObjectClass *group_oc,
1227 AttributeDescription *group_at )
1233 Backend *be = op->o_bd;
1235 if ( op->o_abandon ) return SLAPD_ABANDON;
1237 op->o_bd = select_backend( gr_ndn, 0, 0 );
1239 for ( g = op->o_groups; g; g = g->ga_next ) {
1240 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1241 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1245 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1255 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1259 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1263 if ( op->o_pb != NULL ) {
1264 init_group_pblock( op, target, e, op_ndn, group_at );
1266 rc = call_group_preop_plugins( op );
1267 if ( rc == LDAP_SUCCESS ) {
1271 #endif /* LDAP_SLAPI */
1273 a = attr_find( e->e_attrs, group_at );
1275 /* If the attribute is a subtype of labeledURI, treat this as
1276 * a dynamic group ala groupOfURLs
1278 if (is_at_subtype( group_at->ad_type,
1279 slap_schema.si_ad_labeledURI->ad_type ) )
1283 struct berval bv, nbase;
1286 Backend *b2 = op->o_bd;
1288 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1291 op->o_bd = select_backend( op_ndn, 0, 0 );
1292 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1296 rc = LDAP_COMPARE_FALSE;
1297 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1298 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1303 BER_BVZERO( &nbase );
1304 /* host part must be empty */
1305 /* attrs and extensions parts must be empty */
1306 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1307 ludp->lud_attrs || ludp->lud_exts )
1311 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1312 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1313 op->o_tmpmemctx ) != LDAP_SUCCESS )
1317 switch ( ludp->lud_scope ) {
1318 case LDAP_SCOPE_BASE:
1319 if ( !dn_match( &nbase, op_ndn ) ) {
1323 case LDAP_SCOPE_ONELEVEL:
1324 dnParent( op_ndn, &bv );
1325 if ( !dn_match( &nbase, &bv ) ) {
1329 case LDAP_SCOPE_SUBTREE:
1330 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1334 #ifdef LDAP_SCOPE_SUBORDINATE
1335 case LDAP_SCOPE_SUBORDINATE:
1336 if ( dn_match( &nbase, op_ndn ) ||
1337 !dnIsSuffix( op_ndn, &nbase ) )
1343 filter = str2filter_x( op, ludp->lud_filter );
1345 if ( test_filter( NULL, user, filter ) ==
1350 filter_free_x( op, filter );
1353 ldap_free_urldesc( ludp );
1354 if ( !BER_BVISNULL( &nbase ) ) {
1355 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1357 if ( rc == 0 ) break;
1359 if ( user != target ) {
1360 be_entry_release_r( op, user );
1365 rc = value_find_ex( group_at,
1366 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1367 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1368 a->a_nvals, op_ndn, op->o_tmpmemctx );
1369 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1370 rc = LDAP_COMPARE_FALSE;
1373 rc = LDAP_NO_SUCH_ATTRIBUTE;
1376 be_entry_release_r( op, e );
1379 rc = LDAP_NO_SUCH_OBJECT;
1383 if ( op->o_pb ) call_group_postop_plugins( op );
1384 #endif /* LDAP_SLAPI */
1386 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1387 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1389 g->ga_be = op->o_bd;
1390 g->ga_oc = group_oc;
1391 g->ga_at = group_at;
1393 g->ga_len = gr_ndn->bv_len;
1394 strcpy( g->ga_ndn, gr_ndn->bv_val );
1395 g->ga_next = op->o_groups;
1404 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1408 BerVarray *vals = (BerVarray *)c->cac_private;
1409 Operation *op = NULL;
1412 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1417 if ( op->o_conn && access_allowed( op,
1418 e, a->a_desc, NULL, ACL_AUTH,
1419 &c->cac_acl_state ) == 0 ) {
1423 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1425 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1427 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1428 if ( op->o_conn && access_allowed( op,
1431 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1435 &a->a_nvals[i], op->o_tmpmemctx );
1436 if ( !BER_BVISNULL( &v[j] ) ) {
1442 op->o_tmpfree( v, op->o_tmpmemctx );
1446 BER_BVZERO( &v[j] );
1453 #endif /* LDAP_SLAPI */
1460 AttributeDescription *entry_at,
1462 slap_access_t access )
1465 Attribute *a = NULL;
1466 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1467 AccessControlState acl_state = ACL_STATE_INIT;
1468 Backend *be = op->o_bd;
1470 op->o_bd = select_backend( edn, 0, 0 );
1472 if ( target && dn_match( &target->e_nname, edn ) ) {
1476 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1480 a = attr_find( e->e_attrs, entry_at );
1482 SlapReply rs = { 0 };
1483 AttributeName anlist[ 2 ];
1485 anlist[ 0 ].an_name = entry_at->ad_cname;
1486 anlist[ 0 ].an_desc = entry_at;
1487 BER_BVZERO( &anlist[ 1 ].an_name );
1488 rs.sr_attrs = anlist;
1490 /* NOTE: backend_operational() is also called
1491 * when returning results, so it's supposed
1492 * to do no harm to entries */
1494 rc = backend_operational( op, &rs );
1497 if ( rc == LDAP_SUCCESS ) {
1498 if ( rs.sr_operational_attrs ) {
1500 a = rs.sr_operational_attrs;
1503 rc = LDAP_NO_SUCH_ATTRIBUTE;
1511 if ( op->o_conn && access > ACL_NONE &&
1512 access_allowed( op, e, entry_at, NULL,
1513 access, &acl_state ) == 0 )
1515 rc = LDAP_INSUFFICIENT_ACCESS;
1519 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1522 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1524 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1526 if ( op->o_conn && access > ACL_NONE &&
1527 access_allowed( op, e, entry_at,
1534 ber_dupbv_x( &v[j], &a->a_nvals[i],
1536 if ( !BER_BVISNULL( &v[j] ) ) {
1541 op->o_tmpfree( v, op->o_tmpmemctx );
1543 rc = LDAP_INSUFFICIENT_ACCESS;
1546 BER_BVZERO( &v[j] );
1552 else if ( op->o_pb ) {
1553 /* try any computed attributes */
1554 computed_attr_context ctx;
1556 slapi_int_pblock_set_operation( op->o_pb, op );
1558 ctx.cac_pb = op->o_pb;
1559 ctx.cac_attrs = NULL;
1560 ctx.cac_userattrs = 0;
1561 ctx.cac_opattrs = 0;
1562 ctx.cac_acl_state = acl_state;
1563 ctx.cac_private = (void *)vals;
1565 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1567 rc = LDAP_INSUFFICIENT_ACCESS;
1573 #endif /* LDAP_SLAPI */
1574 freeit: if ( e != target ) {
1575 be_entry_release_r( op, e );
1587 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1589 struct berval *nval = (struct berval *)c->cac_private;
1590 Operation *op = NULL;
1592 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1597 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1599 #endif /* LDAP_SLAPI */
1606 AttributeDescription *entry_at,
1607 struct berval *nval,
1608 slap_access_t access,
1612 int rc = LDAP_INSUFFICIENT_ACCESS;
1613 Backend *be = op->o_bd;
1616 assert( op != NULL );
1617 assert( op->o_conn != NULL );
1618 assert( edn != NULL );
1619 assert( access > ACL_NONE );
1621 op->o_bd = select_backend( edn, 0, 0 );
1623 if ( target && dn_match( &target->e_nname, edn ) ) {
1627 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1631 Attribute *a = NULL;
1634 if ( entry_at == NULL ) {
1635 entry_at = slap_schema.si_ad_entry;
1638 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1640 if ( access_allowed_mask( op, e, entry_at,
1641 NULL, access, NULL, mask ) == 0 )
1643 rc = LDAP_INSUFFICIENT_ACCESS;
1650 a = attr_find( e->e_attrs, entry_at );
1652 SlapReply rs = { 0 };
1653 AttributeName anlist[ 2 ];
1655 anlist[ 0 ].an_name = entry_at->ad_cname;
1656 anlist[ 0 ].an_desc = entry_at;
1657 BER_BVZERO( &anlist[ 1 ].an_name );
1658 rs.sr_attrs = anlist;
1660 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1662 /* NOTE: backend_operational() is also called
1663 * when returning results, so it's supposed
1664 * to do no harm to entries */
1666 rc = backend_operational( op, &rs );
1669 if ( rc == LDAP_SUCCESS ) {
1670 if ( rs.sr_operational_attrs ) {
1672 a = rs.sr_operational_attrs;
1675 rc = LDAP_NO_SUCH_OBJECT;
1681 if ( access_allowed_mask( op, e, entry_at,
1682 nval, access, NULL, mask ) == 0 )
1684 rc = LDAP_INSUFFICIENT_ACCESS;
1690 else if ( op->o_pb ) {
1691 /* try any computed attributes */
1692 computed_attr_context ctx;
1694 slapi_int_pblock_set_operation( op->o_pb, op );
1696 ctx.cac_pb = op->o_pb;
1697 ctx.cac_attrs = NULL;
1698 ctx.cac_userattrs = 0;
1699 ctx.cac_opattrs = 0;
1700 ctx.cac_private = (void *)nval;
1702 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1704 rc = LDAP_INSUFFICIENT_ACCESS;
1710 #endif /* LDAP_SLAPI */
1712 freeit: if ( e != target ) {
1713 be_entry_release_r( op, e );
1724 int backend_operational(
1732 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1733 /* just count them */ ;
1736 * If operational attributes (allegedly) are required,
1737 * and the backend supports specific operational attributes,
1738 * add them to the attribute list
1740 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1741 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1743 *ap = slap_operational_entryDN( rs->sr_entry );
1744 ap = &(*ap)->a_next;
1747 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1748 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1750 *ap = slap_operational_subschemaSubentry( op->o_bd );
1751 ap = &(*ap)->a_next;
1754 if ( op->o_bd != NULL )
1756 /* Let the overlays have a chance at this */
1758 if ( SLAP_ISOVERLAY( be_orig ) )
1759 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1761 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1762 op->o_bd && op->o_bd->be_operational != NULL )
1764 rc = op->o_bd->be_operational( op, rs );
1773 static void init_group_pblock( Operation *op, Entry *target,
1774 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1776 slapi_pblock_set( op->o_pb,
1777 SLAPI_X_GROUP_ENTRY, (void *)e );
1778 slapi_pblock_set( op->o_pb,
1779 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1780 slapi_pblock_set( op->o_pb,
1781 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1782 slapi_pblock_set( op->o_pb,
1783 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1786 static int call_group_preop_plugins( Operation *op )
1790 rc = slapi_int_call_plugins( op->o_bd,
1791 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1793 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1794 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1796 rc = LDAP_NO_SUCH_ATTRIBUTE;
1805 static void call_group_postop_plugins( Operation *op )
1807 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1809 #endif /* LDAP_SLAPI */