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 );
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)
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 )
339 /* shutdown a specific backend database */
341 if ( be->bd_info->bi_nDB == 0 ) {
342 /* no database of this type, we never opened it */
346 if ( be->bd_info->bi_db_close ) {
347 be->bd_info->bi_db_close( be );
350 if( be->bd_info->bi_close ) {
351 be->bd_info->bi_close( be->bd_info );
357 /* close each backend database */
358 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
359 if ( be->bd_info->bi_db_close ) {
360 be->bd_info->bi_db_close( be );
364 Debug( LDAP_DEBUG_ANY,
365 "backend_close: bi_db_close %s failed!\n",
370 /* close each backend type */
371 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
372 if( bi->bi_nDB == 0 ) {
373 /* no database of this type */
382 /* close frontend, if required */
383 if ( frontendDB->bd_info->bi_db_close ) {
384 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
386 Debug( LDAP_DEBUG_ANY,
387 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
395 int backend_destroy(void)
400 struct slap_csn_entry *csne;
402 /* destroy each backend database */
403 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
404 LDAP_STAILQ_REMOVE_HEAD(&backendDB, be_next);
406 if ( bd->be_syncinfo ) {
407 syncinfo_free( bd->be_syncinfo );
410 if ( bd->be_pending_csn_list ) {
411 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
413 struct slap_csn_entry *tmp_csne = csne;
415 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
416 ch_free( csne->ce_csn.bv_val );
417 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
422 if ( bd->bd_info->bi_db_destroy ) {
423 bd->bd_info->bi_db_destroy( bd );
425 ber_bvarray_free( bd->be_suffix );
426 ber_bvarray_free( bd->be_nsuffix );
427 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
428 free( bd->be_rootdn.bv_val );
430 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
431 free( bd->be_rootndn.bv_val );
433 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
434 free( bd->be_rootpw.bv_val );
436 acl_destroy( bd->be_acl, frontendDB->be_acl );
440 /* destroy each backend type */
441 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
442 if( bi->bi_destroy ) {
443 bi->bi_destroy( bi );
448 LDAP_STAILQ_INIT(&backendInfo);
450 /* destroy frontend database */
453 if ( bd->bd_info->bi_db_destroy ) {
454 bd->bd_info->bi_db_destroy( bd );
456 ber_bvarray_free( bd->be_suffix );
457 ber_bvarray_free( bd->be_nsuffix );
458 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
459 free( bd->be_rootdn.bv_val );
461 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
462 free( bd->be_rootndn.bv_val );
464 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
465 free( bd->be_rootpw.bv_val );
467 acl_destroy( bd->be_acl, frontendDB->be_acl );
473 BackendInfo* backend_info(const char *type)
477 /* search for the backend type */
478 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
479 if( strcasecmp(bi->bi_type, type) == 0 ) {
493 BackendInfo *bi = backend_info(type);
497 fprintf( stderr, "Unrecognized database type (%s)\n", type );
501 be = ch_calloc( 1, sizeof(Backend) );
503 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
507 be->be_def_limit = frontendDB->be_def_limit;
508 be->be_dfltaccess = frontendDB->be_dfltaccess;
510 be->be_restrictops = frontendDB->be_restrictops;
511 be->be_requires = frontendDB->be_requires;
512 be->be_ssf_set = frontendDB->be_ssf_set;
514 be->be_pcl_mutexp = &be->be_pcl_mutex;
515 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
517 /* assign a default depth limit for alias deref */
518 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
520 if ( bi->bi_db_init ) {
521 rc = bi->bi_db_init( be );
525 fprintf( stderr, "database init failed (%s)\n", type );
540 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
541 if ( be->bd_info->bi_db_close ) {
542 be->bd_info->bi_db_close( be );
546 if ( frontendDB->bd_info->bi_db_close ) {
547 (*frontendDB->bd_info->bi_db_close)( frontendDB );
559 ber_len_t len, dnlen = dn->bv_len;
560 Backend *be, *b2 = NULL;
562 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
563 if ( be->be_nsuffix == NULL ) {
567 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
569 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
574 len = be->be_nsuffix[j].bv_len;
577 /* suffix is longer than DN */
582 * input DN is normalized, so the separator check
583 * need not look at escaping
585 if ( len && len < dnlen &&
586 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
591 if ( strcmp( be->be_nsuffix[j].bv_val,
592 &dn->bv_val[dnlen-len] ) == 0 )
597 if( manageDSAit && len == dnlen &&
598 !SLAP_GLUE_SUBORDINATE( be ) ) {
615 struct berval *bvsuffix )
619 if ( be->be_nsuffix == NULL ) {
623 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
624 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
633 be_isroot_dn( Backend *be, struct berval *ndn )
635 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
639 return dn_match( &be->be_rootndn, ndn );
643 be_slurp_update( Operation *op )
645 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
646 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
650 be_shadow_update( Operation *op )
652 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
653 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
657 be_isupdate_dn( Backend *be, struct berval *ndn )
659 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
663 return dn_match( &be->be_update_ndn, ndn );
667 be_root_dn( Backend *be )
669 return &be->be_rootdn;
673 be_isroot( Operation *op )
675 return be_isroot_dn( op->o_bd, &op->o_ndn );
679 be_isroot_pw( Operation *op )
683 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
687 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
692 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
693 op->o_conn->c_sasl_authctx, NULL );
696 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
699 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
711 if ( op->o_bd->be_release ) {
712 /* free and release entry from backend */
713 return op->o_bd->be_release( op, e, rw );
722 backend_unbind( Operation *op, SlapReply *rs )
727 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
728 #if defined( LDAP_SLAPI )
731 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
732 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
733 rc = slapi_int_call_plugins( be,
734 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
737 * A preoperation plugin failure will abort the
740 Debug(LDAP_DEBUG_TRACE,
741 "do_bind: Unbind preoperation plugin failed\n",
746 #endif /* defined( LDAP_SLAPI ) */
748 if ( be->be_unbind ) {
750 be->be_unbind( op, rs );
753 #if defined( LDAP_SLAPI )
754 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
755 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
757 Debug(LDAP_DEBUG_TRACE,
758 "do_unbind: Unbind postoperation plugins failed\n",
761 #endif /* defined( LDAP_SLAPI ) */
769 backend_connection_init(
774 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
775 if ( be->be_connection_init ) {
776 be->be_connection_init( be, conn);
784 backend_connection_destroy(
789 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
790 if ( be->be_connection_destroy ) {
791 be->be_connection_destroy( be, conn);
799 backend_check_controls(
803 LDAPControl **ctrls = op->o_ctrls;
804 rs->sr_err = LDAP_SUCCESS;
807 for( ; *ctrls != NULL ; ctrls++ ) {
810 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
811 case LDAP_CONTROL_NOT_FOUND:
812 /* unrecognized control */
813 if ( (*ctrls)->ldctl_iscritical ) {
814 /* should not be reachable */
815 Debug( LDAP_DEBUG_ANY,
816 "backend_check_controls: unrecognized control: %s\n",
817 (*ctrls)->ldctl_oid, 0, 0 );
822 case LDAP_COMPARE_FALSE:
823 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
824 /* Per RFC 2251 (and LDAPBIS discussions), if the control
825 * is recognized and appropriate for the operation (which
826 * we've already verified), then the server should make
827 * use of the control when performing the operation.
829 * Here we find that operation extended by the control
830 * is unavailable in a particular context, and the control
831 * is marked Critical, hence the return of
832 * unwillingToPerform.
834 rs->sr_text = "critical control unavailable in context";
835 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
840 case LDAP_COMPARE_TRUE:
845 Debug( LDAP_DEBUG_ANY,
846 "backend_check_controls: unable to check control: %s\n",
847 (*ctrls)->ldctl_oid, 0, 0 );
850 rs->sr_text = "unable to check control";
851 rs->sr_err = LDAP_OTHER;
857 /* check should be generalized */
858 if( get_manageDIT(op) && !be_isroot(op)) {
859 rs->sr_text = "requires manager authorization";
860 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
868 backend_check_restrictions(
871 struct berval *opdata )
873 slap_mask_t restrictops;
874 slap_mask_t requires;
876 slap_mask_t exopflag = 0;
883 int rc = SLAP_CB_CONTINUE;
885 if ( op->o_bd->be_chk_controls ) {
886 rc = ( *op->o_bd->be_chk_controls )( op, rs );
889 if ( rc == SLAP_CB_CONTINUE ) {
890 rc = backend_check_controls( op, rs );
893 if ( rc != LDAP_SUCCESS ) {
897 restrictops = op->o_bd->be_restrictops;
898 requires = op->o_bd->be_requires;
899 ssf = &op->o_bd->be_ssf_set;
902 restrictops = frontendDB->be_restrictops;
903 requires = frontendDB->be_requires;
904 ssf = &frontendDB->be_ssf_set;
907 switch( op->o_tag ) {
909 opflag = SLAP_RESTRICT_OP_ADD;
913 opflag = SLAP_RESTRICT_OP_BIND;
916 case LDAP_REQ_COMPARE:
917 opflag = SLAP_RESTRICT_OP_COMPARE;
919 case LDAP_REQ_DELETE:
921 opflag = SLAP_RESTRICT_OP_DELETE;
923 case LDAP_REQ_EXTENDED:
924 opflag = SLAP_RESTRICT_OP_EXTENDED;
927 /* treat unspecified as a modify */
928 opflag = SLAP_RESTRICT_OP_MODIFY;
933 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
936 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
940 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
941 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
945 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
946 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
950 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
951 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
956 /* treat everything else as a modify */
957 opflag = SLAP_RESTRICT_OP_MODIFY;
961 case LDAP_REQ_MODIFY:
963 opflag = SLAP_RESTRICT_OP_MODIFY;
965 case LDAP_REQ_RENAME:
967 opflag = SLAP_RESTRICT_OP_RENAME;
969 case LDAP_REQ_SEARCH:
970 opflag = SLAP_RESTRICT_OP_SEARCH;
972 case LDAP_REQ_UNBIND:
977 rs->sr_text = "restrict operations internal error";
978 rs->sr_err = LDAP_OTHER;
983 /* these checks don't apply to StartTLS */
985 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
986 if( op->o_transport_ssf < ssf->sss_transport ) {
987 rs->sr_text = op->o_transport_ssf
988 ? "stronger transport confidentiality required"
989 : "transport confidentiality required";
993 if( op->o_tls_ssf < ssf->sss_tls ) {
994 rs->sr_text = op->o_tls_ssf
995 ? "stronger TLS confidentiality required"
996 : "TLS confidentiality required";
1001 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1002 /* simple bind specific check */
1003 if( op->o_ssf < ssf->sss_simple_bind ) {
1004 rs->sr_text = op->o_ssf
1005 ? "stronger confidentiality required"
1006 : "confidentiality required";
1011 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1012 /* these checks don't apply to SASL bind */
1014 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1015 rs->sr_text = op->o_sasl_ssf
1016 ? "stronger SASL confidentiality required"
1017 : "SASL confidentiality required";
1021 if( op->o_ssf < ssf->sss_ssf ) {
1022 rs->sr_text = op->o_ssf
1023 ? "stronger confidentiality required"
1024 : "confidentiality required";
1030 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1031 rs->sr_text = op->o_transport_ssf
1032 ? "stronger transport confidentiality required for update"
1033 : "transport confidentiality required for update";
1037 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1038 rs->sr_text = op->o_tls_ssf
1039 ? "stronger TLS confidentiality required for update"
1040 : "TLS confidentiality required for update";
1044 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1045 rs->sr_text = op->o_sasl_ssf
1046 ? "stronger SASL confidentiality required for update"
1047 : "SASL confidentiality required for update";
1051 if( op->o_ssf < ssf->sss_update_ssf ) {
1052 rs->sr_text = op->o_ssf
1053 ? "stronger confidentiality required for update"
1054 : "confidentiality required for update";
1058 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1059 BER_BVISEMPTY( &op->o_ndn ) )
1061 rs->sr_text = "modifications require authentication";
1062 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1066 #ifdef SLAP_X_LISTENER_MOD
1067 if ( op->o_conn->c_listener &&
1068 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1069 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1071 /* no "w" mode means readonly */
1072 rs->sr_text = "modifications not allowed on this listener";
1073 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1076 #endif /* SLAP_X_LISTENER_MOD */
1081 /* these checks don't apply to Bind, StartTLS, or Unbind */
1083 if( requires & SLAP_REQUIRE_STRONG ) {
1084 /* should check mechanism */
1085 if( ( op->o_transport_ssf < ssf->sss_transport
1086 && op->o_authtype == LDAP_AUTH_SIMPLE )
1087 || BER_BVISEMPTY( &op->o_dn ) )
1089 rs->sr_text = "strong(er) authentication required";
1090 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1095 if( requires & SLAP_REQUIRE_SASL ) {
1096 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1097 rs->sr_text = "SASL authentication required";
1098 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1103 if( requires & SLAP_REQUIRE_AUTHC ) {
1104 if( BER_BVISEMPTY( &op->o_dn ) ) {
1105 rs->sr_text = "authentication required";
1106 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1111 if( requires & SLAP_REQUIRE_BIND ) {
1113 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1114 version = op->o_conn->c_protocol;
1115 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1118 /* no bind has occurred */
1119 rs->sr_text = "BIND required";
1120 rs->sr_err = LDAP_OPERATIONS_ERROR;
1125 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1126 if( op->o_protocol < LDAP_VERSION3 ) {
1127 /* no bind has occurred */
1128 rs->sr_text = "operation restricted to LDAPv3 clients";
1129 rs->sr_err = LDAP_OPERATIONS_ERROR;
1134 #ifdef SLAP_X_LISTENER_MOD
1135 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1136 if ( op->o_conn->c_listener &&
1137 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1139 /* no "x" mode means bind required */
1140 rs->sr_text = "bind required on this listener";
1141 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1146 if ( !starttls && !updateop ) {
1147 if ( op->o_conn->c_listener &&
1148 !( op->o_conn->c_listener->sl_perms &
1149 ( !BER_BVISEMPTY( &op->o_dn )
1150 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1152 /* no "r" mode means no read */
1153 rs->sr_text = "read not allowed on this listener";
1154 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1158 #endif /* SLAP_X_LISTENER_MOD */
1162 if( ( restrictops & opflag )
1163 || ( exopflag && ( restrictops & exopflag ) ) ) {
1164 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1165 rs->sr_text = "read operations restricted";
1166 } else if ( restrictops & exopflag ) {
1167 rs->sr_text = "extended operation restricted";
1169 rs->sr_text = "operation restricted";
1171 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1175 rs->sr_err = LDAP_SUCCESS;
1179 int backend_check_referrals( Operation *op, SlapReply *rs )
1181 rs->sr_err = LDAP_SUCCESS;
1183 if( op->o_bd->be_chk_referrals ) {
1184 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1186 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1187 send_ldap_result( op, rs );
1199 AttributeDescription *at,
1207 if (op->o_bd == NULL) {
1208 rc = LDAP_NO_SUCH_OBJECT;
1209 } else if ( op->o_bd->be_fetch ) {
1210 rc = ( op->o_bd->be_fetch )( op, ndn,
1213 rc = LDAP_UNWILLING_TO_PERFORM;
1222 struct berval *gr_ndn,
1223 struct berval *op_ndn,
1224 ObjectClass *group_oc,
1225 AttributeDescription *group_at )
1231 Backend *be = op->o_bd;
1233 if ( op->o_abandon ) return SLAPD_ABANDON;
1235 op->o_bd = select_backend( gr_ndn, 0, 0 );
1237 for ( g = op->o_groups; g; g = g->ga_next ) {
1238 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1239 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1243 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1253 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1257 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1261 if ( op->o_pb != NULL ) {
1262 init_group_pblock( op, target, e, op_ndn, group_at );
1264 rc = call_group_preop_plugins( op );
1265 if ( rc == LDAP_SUCCESS ) {
1269 #endif /* LDAP_SLAPI */
1271 a = attr_find( e->e_attrs, group_at );
1273 /* If the attribute is a subtype of labeledURI, treat this as
1274 * a dynamic group ala groupOfURLs
1276 if (is_at_subtype( group_at->ad_type,
1277 slap_schema.si_ad_labeledURI->ad_type ) )
1281 struct berval bv, nbase;
1284 Backend *b2 = op->o_bd;
1286 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1289 op->o_bd = select_backend( op_ndn, 0, 0 );
1290 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1294 rc = LDAP_COMPARE_FALSE;
1295 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1296 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1301 BER_BVZERO( &nbase );
1302 /* host part must be empty */
1303 /* attrs and extensions parts must be empty */
1304 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1305 ludp->lud_attrs || ludp->lud_exts )
1309 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1310 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1311 op->o_tmpmemctx ) != LDAP_SUCCESS )
1315 switch ( ludp->lud_scope ) {
1316 case LDAP_SCOPE_BASE:
1317 if ( !dn_match( &nbase, op_ndn ) ) {
1321 case LDAP_SCOPE_ONELEVEL:
1322 dnParent( op_ndn, &bv );
1323 if ( !dn_match( &nbase, &bv ) ) {
1327 case LDAP_SCOPE_SUBTREE:
1328 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1332 #ifdef LDAP_SCOPE_SUBORDINATE
1333 case LDAP_SCOPE_SUBORDINATE:
1334 if ( dn_match( &nbase, op_ndn ) ||
1335 !dnIsSuffix( op_ndn, &nbase ) )
1341 filter = str2filter_x( op, ludp->lud_filter );
1343 if ( test_filter( NULL, user, filter ) ==
1348 filter_free_x( op, filter );
1351 ldap_free_urldesc( ludp );
1352 if ( !BER_BVISNULL( &nbase ) ) {
1353 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1355 if ( rc == 0 ) break;
1357 if ( user != target ) {
1358 be_entry_release_r( op, user );
1363 rc = value_find_ex( group_at,
1364 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1365 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1366 a->a_nvals, op_ndn, op->o_tmpmemctx );
1367 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1368 rc = LDAP_COMPARE_FALSE;
1371 rc = LDAP_NO_SUCH_ATTRIBUTE;
1374 be_entry_release_r( op, e );
1377 rc = LDAP_NO_SUCH_OBJECT;
1381 if ( op->o_pb ) call_group_postop_plugins( op );
1382 #endif /* LDAP_SLAPI */
1384 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1385 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1387 g->ga_be = op->o_bd;
1388 g->ga_oc = group_oc;
1389 g->ga_at = group_at;
1391 g->ga_len = gr_ndn->bv_len;
1392 strcpy( g->ga_ndn, gr_ndn->bv_val );
1393 g->ga_next = op->o_groups;
1402 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1406 BerVarray *vals = (BerVarray *)c->cac_private;
1407 Operation *op = NULL;
1410 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1415 if ( op->o_conn && access_allowed( op,
1416 e, a->a_desc, NULL, ACL_AUTH,
1417 &c->cac_acl_state ) == 0 ) {
1421 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1423 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1425 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1426 if ( op->o_conn && access_allowed( op,
1429 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1433 &a->a_nvals[i], op->o_tmpmemctx );
1434 if ( !BER_BVISNULL( &v[j] ) ) {
1440 op->o_tmpfree( v, op->o_tmpmemctx );
1444 BER_BVZERO( &v[j] );
1451 #endif /* LDAP_SLAPI */
1458 AttributeDescription *entry_at,
1460 slap_access_t access )
1463 Attribute *a = NULL;
1464 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1465 AccessControlState acl_state = ACL_STATE_INIT;
1466 Backend *be = op->o_bd;
1468 op->o_bd = select_backend( edn, 0, 0 );
1470 if ( target && dn_match( &target->e_nname, edn ) ) {
1474 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1478 a = attr_find( e->e_attrs, entry_at );
1480 SlapReply rs = { 0 };
1481 AttributeName anlist[ 2 ];
1483 anlist[ 0 ].an_name = entry_at->ad_cname;
1484 anlist[ 0 ].an_desc = entry_at;
1485 BER_BVZERO( &anlist[ 1 ].an_name );
1486 rs.sr_attrs = anlist;
1488 /* NOTE: backend_operational() is also called
1489 * when returning results, so it's supposed
1490 * to do no harm to entries */
1492 rc = backend_operational( op, &rs );
1495 if ( rc == LDAP_SUCCESS ) {
1496 if ( rs.sr_operational_attrs ) {
1498 a = rs.sr_operational_attrs;
1501 rc = LDAP_NO_SUCH_ATTRIBUTE;
1509 if ( op->o_conn && access > ACL_NONE &&
1510 access_allowed( op, e, entry_at, NULL,
1511 access, &acl_state ) == 0 )
1513 rc = LDAP_INSUFFICIENT_ACCESS;
1517 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1520 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1522 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1524 if ( op->o_conn && access > ACL_NONE &&
1525 access_allowed( op, e, entry_at,
1532 ber_dupbv_x( &v[j], &a->a_nvals[i],
1534 if ( !BER_BVISNULL( &v[j] ) ) {
1539 op->o_tmpfree( v, op->o_tmpmemctx );
1541 rc = LDAP_INSUFFICIENT_ACCESS;
1544 BER_BVZERO( &v[j] );
1550 else if ( op->o_pb ) {
1551 /* try any computed attributes */
1552 computed_attr_context ctx;
1554 slapi_int_pblock_set_operation( op->o_pb, op );
1556 ctx.cac_pb = op->o_pb;
1557 ctx.cac_attrs = NULL;
1558 ctx.cac_userattrs = 0;
1559 ctx.cac_opattrs = 0;
1560 ctx.cac_acl_state = acl_state;
1561 ctx.cac_private = (void *)vals;
1563 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1565 rc = LDAP_INSUFFICIENT_ACCESS;
1571 #endif /* LDAP_SLAPI */
1572 freeit: if ( e != target ) {
1573 be_entry_release_r( op, e );
1585 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1587 struct berval *nval = (struct berval *)c->cac_private;
1588 Operation *op = NULL;
1590 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1595 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1597 #endif /* LDAP_SLAPI */
1604 AttributeDescription *entry_at,
1605 struct berval *nval,
1606 slap_access_t access,
1610 int rc = LDAP_INSUFFICIENT_ACCESS;
1611 Backend *be = op->o_bd;
1615 assert( op->o_conn );
1617 assert( access > ACL_NONE );
1619 op->o_bd = select_backend( edn, 0, 0 );
1621 if ( target && dn_match( &target->e_nname, edn ) ) {
1625 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1629 Attribute *a = NULL;
1632 if ( entry_at == NULL ) {
1633 entry_at = slap_schema.si_ad_entry;
1636 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1638 if ( access_allowed_mask( op, e, entry_at,
1639 NULL, access, NULL, mask ) == 0 )
1641 rc = LDAP_INSUFFICIENT_ACCESS;
1648 a = attr_find( e->e_attrs, entry_at );
1650 SlapReply rs = { 0 };
1651 AttributeName anlist[ 2 ];
1653 anlist[ 0 ].an_name = entry_at->ad_cname;
1654 anlist[ 0 ].an_desc = entry_at;
1655 BER_BVZERO( &anlist[ 1 ].an_name );
1656 rs.sr_attrs = anlist;
1658 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1660 /* NOTE: backend_operational() is also called
1661 * when returning results, so it's supposed
1662 * to do no harm to entries */
1664 rc = backend_operational( op, &rs );
1667 if ( rc == LDAP_SUCCESS ) {
1668 if ( rs.sr_operational_attrs ) {
1670 a = rs.sr_operational_attrs;
1673 rc = LDAP_NO_SUCH_OBJECT;
1679 if ( access_allowed_mask( op, e, entry_at,
1680 nval, access, NULL, mask ) == 0 )
1682 rc = LDAP_INSUFFICIENT_ACCESS;
1688 else if ( op->o_pb ) {
1689 /* try any computed attributes */
1690 computed_attr_context ctx;
1692 slapi_int_pblock_set_operation( op->o_pb, op );
1694 ctx.cac_pb = op->o_pb;
1695 ctx.cac_attrs = NULL;
1696 ctx.cac_userattrs = 0;
1697 ctx.cac_opattrs = 0;
1698 ctx.cac_private = (void *)nval;
1700 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1702 rc = LDAP_INSUFFICIENT_ACCESS;
1708 #endif /* LDAP_SLAPI */
1710 freeit: if ( e != target ) {
1711 be_entry_release_r( op, e );
1722 int backend_operational(
1730 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1731 /* just count them */ ;
1734 * If operational attributes (allegedly) are required,
1735 * and the backend supports specific operational attributes,
1736 * add them to the attribute list
1738 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1739 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1741 *ap = slap_operational_entryDN( rs->sr_entry );
1742 ap = &(*ap)->a_next;
1745 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1746 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1748 *ap = slap_operational_subschemaSubentry( op->o_bd );
1749 ap = &(*ap)->a_next;
1752 /* Let the overlays have a chance at this */
1754 if ( SLAP_ISOVERLAY( be_orig ) )
1755 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1757 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1758 op->o_bd && op->o_bd->be_operational != NULL )
1760 rc = op->o_bd->be_operational( op, rs );
1768 static void init_group_pblock( Operation *op, Entry *target,
1769 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1771 slapi_int_pblock_set_operation( op->o_pb, op );
1773 slapi_pblock_set( op->o_pb,
1774 SLAPI_X_GROUP_ENTRY, (void *)e );
1775 slapi_pblock_set( op->o_pb,
1776 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1777 slapi_pblock_set( op->o_pb,
1778 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1779 slapi_pblock_set( op->o_pb,
1780 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1783 static int call_group_preop_plugins( Operation *op )
1787 rc = slapi_int_call_plugins( op->o_bd,
1788 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1790 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1791 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1793 rc = LDAP_NO_SUCH_ATTRIBUTE;
1802 static void call_group_postop_plugins( Operation *op )
1804 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1806 #endif /* LDAP_SLAPI */