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++ )
103 assert( bi->bi_init );
105 rc = bi->bi_init( bi );
108 Debug( LDAP_DEBUG_ANY,
109 "backend_init: initialized for type \"%s\"\n",
111 /* destroy those we've already inited */
116 if ( slap_binfo[nBackendInfo].bi_destroy ) {
117 slap_binfo[nBackendInfo].bi_destroy(
118 &slap_binfo[nBackendInfo] );
124 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
127 if ( nBackendInfo > 0) {
135 Debug( LDAP_DEBUG_ANY,
136 "backend_init: failed\n",
140 #endif /* SLAPD_MODULES */
143 int backend_add(BackendInfo *aBackendInfo)
147 if ( aBackendInfo->bi_init == NULL ) {
148 Debug( LDAP_DEBUG_ANY, "backend_add: "
149 "backend type \"%s\" does not have the (mandatory)init function\n",
150 aBackendInfo->bi_type, 0, 0 );
154 rc = aBackendInfo->bi_init(aBackendInfo);
156 Debug( LDAP_DEBUG_ANY,
157 "backend_add: initialization for type \"%s\" failed\n",
158 aBackendInfo->bi_type, 0, 0 );
162 (void)backend_init_controls( aBackendInfo );
164 /* now add the backend type to the Backend Info List */
165 LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
171 backend_set_controls( BackendDB *be )
173 BackendInfo *bi = be->bd_info;
175 /* back-relay takes care of itself; so may do other */
176 if ( overlay_is_over( be ) ) {
177 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
180 if ( bi->bi_controls ) {
181 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
182 AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
183 sizeof( be->be_ctrls ) );
184 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
189 for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
190 if ( bi->bi_ctrls[ i ] ) {
191 be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
201 /* startup a specific backend database */
202 int backend_startup_one(Backend *be)
208 be->be_pending_csn_list = (struct be_pcl *)
209 ch_calloc( 1, sizeof( struct be_pcl ));
211 LDAP_TAILQ_INIT( be->be_pending_csn_list );
213 Debug( LDAP_DEBUG_TRACE,
214 "backend_startup_one: starting \"%s\"\n",
215 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
218 /* set database controls */
219 (void)backend_set_controls( be );
221 if ( be->bd_info->bi_db_open ) {
222 rc = be->bd_info->bi_db_open( be );
224 (void)backend_set_controls( be );
227 Debug( LDAP_DEBUG_ANY,
228 "backend_startup_one: bi_db_open failed! (%d)\n",
236 int backend_startup(Backend *be)
242 if( ! ( nBackendDB > 0 ) ) {
244 Debug( LDAP_DEBUG_ANY,
245 "backend_startup: %d databases to startup.\n",
251 if ( be->bd_info->bi_open ) {
252 rc = be->bd_info->bi_open( be->bd_info );
254 Debug( LDAP_DEBUG_ANY,
255 "backend_startup: bi_open failed!\n",
262 return backend_startup_one( be );
265 /* open frontend, if required */
266 if ( frontendDB->bd_info->bi_db_open ) {
267 rc = frontendDB->bd_info->bi_db_open( frontendDB );
269 Debug( LDAP_DEBUG_ANY,
270 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
276 /* open each backend type */
278 LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
280 if( bi->bi_nDB == 0) {
281 /* no database of this type, don't open */
286 rc = bi->bi_open( bi );
288 Debug( LDAP_DEBUG_ANY,
289 "backend_startup: bi_open %d (%s) failed!\n",
295 (void)backend_init_controls( bi );
298 /* open each backend database */
300 LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
302 if ( be->be_suffix == NULL ) {
303 Debug( LDAP_DEBUG_ANY,
304 "backend_startup: warning, database %d (%s) "
306 i, be->bd_info->bi_type, 0 );
308 /* append global access controls */
309 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
311 rc = backend_startup_one( be );
319 int backend_num( Backend *be )
324 if( be == NULL ) return -1;
326 LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
327 if( be == b2 ) return i;
333 int backend_shutdown( Backend *be )
340 /* shutdown a specific backend database */
342 if ( be->bd_info->bi_nDB == 0 ) {
343 /* no database of this type, we never opened it */
347 if ( be->bd_info->bi_db_close ) {
348 be->bd_info->bi_db_close( be );
351 if( be->bd_info->bi_close ) {
352 be->bd_info->bi_close( be->bd_info );
358 /* close each backend database */
359 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
360 if ( be->bd_info->bi_db_close ) {
361 be->bd_info->bi_db_close( be );
365 Debug( LDAP_DEBUG_ANY,
366 "backend_close: bi_db_close %s failed!\n",
371 /* close each backend type */
372 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
373 if( bi->bi_nDB == 0 ) {
374 /* no database of this type */
383 /* close frontend, if required */
384 if ( frontendDB->bd_info->bi_db_close ) {
385 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
387 Debug( LDAP_DEBUG_ANY,
388 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
396 int backend_destroy(void)
401 struct slap_csn_entry *csne;
403 /* destroy each backend database */
404 while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
405 LDAP_STAILQ_REMOVE_HEAD(&backendDB, be_next);
407 if ( bd->be_syncinfo ) {
408 syncinfo_free( bd->be_syncinfo );
411 if ( bd->be_pending_csn_list ) {
412 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
414 struct slap_csn_entry *tmp_csne = csne;
416 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
417 ch_free( csne->ce_csn.bv_val );
418 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
423 if ( bd->bd_info->bi_db_destroy ) {
424 bd->bd_info->bi_db_destroy( bd );
426 ber_bvarray_free( bd->be_suffix );
427 ber_bvarray_free( bd->be_nsuffix );
428 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
429 free( bd->be_rootdn.bv_val );
431 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
432 free( bd->be_rootndn.bv_val );
434 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
435 free( bd->be_rootpw.bv_val );
437 acl_destroy( bd->be_acl, frontendDB->be_acl );
441 /* destroy each backend type */
442 LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
443 if( bi->bi_destroy ) {
444 bi->bi_destroy( bi );
449 LDAP_STAILQ_INIT(&backendInfo);
451 /* destroy frontend database */
454 if ( bd->bd_info->bi_db_destroy ) {
455 bd->bd_info->bi_db_destroy( bd );
457 ber_bvarray_free( bd->be_suffix );
458 ber_bvarray_free( bd->be_nsuffix );
459 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
460 free( bd->be_rootdn.bv_val );
462 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
463 free( bd->be_rootndn.bv_val );
465 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
466 free( bd->be_rootpw.bv_val );
468 acl_destroy( bd->be_acl, frontendDB->be_acl );
474 BackendInfo* backend_info(const char *type)
478 /* search for the backend type */
479 LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
480 if( strcasecmp(bi->bi_type, type) == 0 ) {
494 BackendInfo *bi = backend_info(type);
498 fprintf( stderr, "Unrecognized database type (%s)\n", type );
502 be = ch_calloc( 1, sizeof(Backend) );
504 LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
508 be->be_def_limit = frontendDB->be_def_limit;
509 be->be_dfltaccess = frontendDB->be_dfltaccess;
511 be->be_restrictops = frontendDB->be_restrictops;
512 be->be_requires = frontendDB->be_requires;
513 be->be_ssf_set = frontendDB->be_ssf_set;
515 be->be_pcl_mutexp = &be->be_pcl_mutex;
516 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
518 /* assign a default depth limit for alias deref */
519 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
521 if ( bi->bi_db_init ) {
522 rc = bi->bi_db_init( be );
526 fprintf( stderr, "database init failed (%s)\n", type );
541 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
542 if ( be->bd_info->bi_db_close ) {
543 be->bd_info->bi_db_close( be );
547 if ( frontendDB->bd_info->bi_db_close ) {
548 (*frontendDB->bd_info->bi_db_close)( frontendDB );
560 ber_len_t len, dnlen = dn->bv_len;
561 Backend *be, *b2 = NULL;
563 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
564 if ( be->be_nsuffix == NULL ) {
568 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
570 if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
575 len = be->be_nsuffix[j].bv_len;
578 /* suffix is longer than DN */
583 * input DN is normalized, so the separator check
584 * need not look at escaping
586 if ( len && len < dnlen &&
587 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
592 if ( strcmp( be->be_nsuffix[j].bv_val,
593 &dn->bv_val[dnlen-len] ) == 0 )
598 if( manageDSAit && len == dnlen &&
599 !SLAP_GLUE_SUBORDINATE( be ) ) {
616 struct berval *bvsuffix )
620 if ( be->be_nsuffix == NULL ) {
624 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
625 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
634 be_isroot_dn( Backend *be, struct berval *ndn )
636 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
640 return dn_match( &be->be_rootndn, ndn );
644 be_slurp_update( Operation *op )
646 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
647 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
651 be_shadow_update( Operation *op )
653 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
654 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
658 be_isupdate_dn( Backend *be, struct berval *ndn )
660 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
664 return dn_match( &be->be_update_ndn, ndn );
668 be_root_dn( Backend *be )
670 return &be->be_rootdn;
674 be_isroot( Operation *op )
676 return be_isroot_dn( op->o_bd, &op->o_ndn );
680 be_isroot_pw( Operation *op )
684 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
688 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
693 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
694 op->o_conn->c_sasl_authctx, NULL );
697 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
700 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
712 if ( op->o_bd->be_release ) {
713 /* free and release entry from backend */
714 return op->o_bd->be_release( op, e, rw );
723 backend_unbind( Operation *op, SlapReply *rs )
728 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
729 #if defined( LDAP_SLAPI )
732 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
733 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)be );
734 rc = slapi_int_call_plugins( be,
735 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
738 * A preoperation plugin failure will abort the
741 Debug(LDAP_DEBUG_TRACE,
742 "do_bind: Unbind preoperation plugin failed\n",
747 #endif /* defined( LDAP_SLAPI ) */
749 if ( be->be_unbind ) {
751 be->be_unbind( op, rs );
754 #if defined( LDAP_SLAPI )
755 if ( op->o_pb != NULL && slapi_int_call_plugins( be,
756 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
758 Debug(LDAP_DEBUG_TRACE,
759 "do_unbind: Unbind postoperation plugins failed\n",
762 #endif /* defined( LDAP_SLAPI ) */
770 backend_connection_init(
775 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
776 if ( be->be_connection_init ) {
777 be->be_connection_init( be, conn);
785 backend_connection_destroy(
790 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
791 if ( be->be_connection_destroy ) {
792 be->be_connection_destroy( be, conn);
800 backend_check_controls(
804 LDAPControl **ctrls = op->o_ctrls;
805 rs->sr_err = LDAP_SUCCESS;
808 for( ; *ctrls != NULL ; ctrls++ ) {
811 switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
812 case LDAP_CONTROL_NOT_FOUND:
813 /* unrecognized control */
814 if ( (*ctrls)->ldctl_iscritical ) {
815 /* should not be reachable */
816 Debug( LDAP_DEBUG_ANY,
817 "backend_check_controls: unrecognized control: %s\n",
818 (*ctrls)->ldctl_oid, 0, 0 );
823 case LDAP_COMPARE_FALSE:
824 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
825 /* Per RFC 2251 (and LDAPBIS discussions), if the control
826 * is recognized and appropriate for the operation (which
827 * we've already verified), then the server should make
828 * use of the control when performing the operation.
830 * Here we find that operation extended by the control
831 * is unavailable in a particular context, and the control
832 * is marked Critical, hence the return of
833 * unwillingToPerform.
835 rs->sr_text = "critical control unavailable in context";
836 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
841 case LDAP_COMPARE_TRUE:
846 Debug( LDAP_DEBUG_ANY,
847 "backend_check_controls: unable to check control: %s\n",
848 (*ctrls)->ldctl_oid, 0, 0 );
851 rs->sr_text = "unable to check control";
852 rs->sr_err = LDAP_OTHER;
863 backend_check_restrictions(
866 struct berval *opdata )
868 slap_mask_t restrictops;
869 slap_mask_t requires;
871 slap_mask_t exopflag = 0;
878 int rc = SLAP_CB_CONTINUE;
880 if ( op->o_bd->be_chk_controls ) {
881 rc = ( *op->o_bd->be_chk_controls )( op, rs );
884 if ( rc == SLAP_CB_CONTINUE ) {
885 rc = backend_check_controls( op, rs );
888 if ( rc != LDAP_SUCCESS ) {
892 restrictops = op->o_bd->be_restrictops;
893 requires = op->o_bd->be_requires;
894 ssf = &op->o_bd->be_ssf_set;
897 restrictops = frontendDB->be_restrictops;
898 requires = frontendDB->be_requires;
899 ssf = &frontendDB->be_ssf_set;
902 switch( op->o_tag ) {
904 opflag = SLAP_RESTRICT_OP_ADD;
908 opflag = SLAP_RESTRICT_OP_BIND;
911 case LDAP_REQ_COMPARE:
912 opflag = SLAP_RESTRICT_OP_COMPARE;
914 case LDAP_REQ_DELETE:
916 opflag = SLAP_RESTRICT_OP_DELETE;
918 case LDAP_REQ_EXTENDED:
919 opflag = SLAP_RESTRICT_OP_EXTENDED;
922 /* treat unspecified as a modify */
923 opflag = SLAP_RESTRICT_OP_MODIFY;
928 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
931 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
935 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
936 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
940 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
941 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
945 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
946 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
951 /* treat everything else as a modify */
952 opflag = SLAP_RESTRICT_OP_MODIFY;
956 case LDAP_REQ_MODIFY:
958 opflag = SLAP_RESTRICT_OP_MODIFY;
960 case LDAP_REQ_RENAME:
962 opflag = SLAP_RESTRICT_OP_RENAME;
964 case LDAP_REQ_SEARCH:
965 opflag = SLAP_RESTRICT_OP_SEARCH;
967 case LDAP_REQ_UNBIND:
972 rs->sr_text = "restrict operations internal error";
973 rs->sr_err = LDAP_OTHER;
978 /* these checks don't apply to StartTLS */
980 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
981 if( op->o_transport_ssf < ssf->sss_transport ) {
982 rs->sr_text = op->o_transport_ssf
983 ? "stronger transport confidentiality required"
984 : "transport confidentiality required";
988 if( op->o_tls_ssf < ssf->sss_tls ) {
989 rs->sr_text = op->o_tls_ssf
990 ? "stronger TLS confidentiality required"
991 : "TLS confidentiality required";
996 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
997 /* simple bind specific check */
998 if( op->o_ssf < ssf->sss_simple_bind ) {
999 rs->sr_text = op->o_ssf
1000 ? "stronger confidentiality required"
1001 : "confidentiality required";
1006 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1007 /* these checks don't apply to SASL bind */
1009 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1010 rs->sr_text = op->o_sasl_ssf
1011 ? "stronger SASL confidentiality required"
1012 : "SASL confidentiality required";
1016 if( op->o_ssf < ssf->sss_ssf ) {
1017 rs->sr_text = op->o_ssf
1018 ? "stronger confidentiality required"
1019 : "confidentiality required";
1025 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1026 rs->sr_text = op->o_transport_ssf
1027 ? "stronger transport confidentiality required for update"
1028 : "transport confidentiality required for update";
1032 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1033 rs->sr_text = op->o_tls_ssf
1034 ? "stronger TLS confidentiality required for update"
1035 : "TLS confidentiality required for update";
1039 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1040 rs->sr_text = op->o_sasl_ssf
1041 ? "stronger SASL confidentiality required for update"
1042 : "SASL confidentiality required for update";
1046 if( op->o_ssf < ssf->sss_update_ssf ) {
1047 rs->sr_text = op->o_ssf
1048 ? "stronger confidentiality required for update"
1049 : "confidentiality required for update";
1053 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1054 BER_BVISEMPTY( &op->o_ndn ) )
1056 rs->sr_text = "modifications require authentication";
1057 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1061 #ifdef SLAP_X_LISTENER_MOD
1062 if ( op->o_conn->c_listener &&
1063 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1064 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1066 /* no "w" mode means readonly */
1067 rs->sr_text = "modifications not allowed on this listener";
1068 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1071 #endif /* SLAP_X_LISTENER_MOD */
1076 /* these checks don't apply to Bind, StartTLS, or Unbind */
1078 if( requires & SLAP_REQUIRE_STRONG ) {
1079 /* should check mechanism */
1080 if( ( op->o_transport_ssf < ssf->sss_transport
1081 && op->o_authtype == LDAP_AUTH_SIMPLE )
1082 || BER_BVISEMPTY( &op->o_dn ) )
1084 rs->sr_text = "strong(er) authentication required";
1085 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1090 if( requires & SLAP_REQUIRE_SASL ) {
1091 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1092 rs->sr_text = "SASL authentication required";
1093 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1098 if( requires & SLAP_REQUIRE_AUTHC ) {
1099 if( BER_BVISEMPTY( &op->o_dn ) ) {
1100 rs->sr_text = "authentication required";
1101 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1106 if( requires & SLAP_REQUIRE_BIND ) {
1108 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1109 version = op->o_conn->c_protocol;
1110 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1113 /* no bind has occurred */
1114 rs->sr_text = "BIND required";
1115 rs->sr_err = LDAP_OPERATIONS_ERROR;
1120 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1121 if( op->o_protocol < LDAP_VERSION3 ) {
1122 /* no bind has occurred */
1123 rs->sr_text = "operation restricted to LDAPv3 clients";
1124 rs->sr_err = LDAP_OPERATIONS_ERROR;
1129 #ifdef SLAP_X_LISTENER_MOD
1130 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1131 if ( op->o_conn->c_listener &&
1132 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1134 /* no "x" mode means bind required */
1135 rs->sr_text = "bind required on this listener";
1136 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1141 if ( !starttls && !updateop ) {
1142 if ( op->o_conn->c_listener &&
1143 !( op->o_conn->c_listener->sl_perms &
1144 ( !BER_BVISEMPTY( &op->o_dn )
1145 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1147 /* no "r" mode means no read */
1148 rs->sr_text = "read not allowed on this listener";
1149 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1153 #endif /* SLAP_X_LISTENER_MOD */
1157 if( ( restrictops & opflag )
1158 || ( exopflag && ( restrictops & exopflag ) ) ) {
1159 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1160 rs->sr_text = "read operations restricted";
1161 } else if ( restrictops & exopflag ) {
1162 rs->sr_text = "extended operation restricted";
1164 rs->sr_text = "operation restricted";
1166 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1170 rs->sr_err = LDAP_SUCCESS;
1174 int backend_check_referrals( Operation *op, SlapReply *rs )
1176 rs->sr_err = LDAP_SUCCESS;
1178 if( op->o_bd->be_chk_referrals ) {
1179 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1181 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1182 send_ldap_result( op, rs );
1194 AttributeDescription *at,
1202 if (op->o_bd == NULL) {
1203 rc = LDAP_NO_SUCH_OBJECT;
1204 } else if ( op->o_bd->be_fetch ) {
1205 rc = ( op->o_bd->be_fetch )( op, ndn,
1208 rc = LDAP_UNWILLING_TO_PERFORM;
1217 struct berval *gr_ndn,
1218 struct berval *op_ndn,
1219 ObjectClass *group_oc,
1220 AttributeDescription *group_at )
1226 Backend *be = op->o_bd;
1228 if ( op->o_abandon ) return SLAPD_ABANDON;
1230 op->o_bd = select_backend( gr_ndn, 0, 0 );
1232 for ( g = op->o_groups; g; g = g->ga_next ) {
1233 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1234 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1238 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1248 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1252 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1256 if ( op->o_pb != NULL ) {
1257 init_group_pblock( op, target, e, op_ndn, group_at );
1259 rc = call_group_preop_plugins( op );
1260 if ( rc == LDAP_SUCCESS ) {
1264 #endif /* LDAP_SLAPI */
1266 a = attr_find( e->e_attrs, group_at );
1268 /* If the attribute is a subtype of labeledURI, treat this as
1269 * a dynamic group ala groupOfURLs
1271 if (is_at_subtype( group_at->ad_type,
1272 slap_schema.si_ad_labeledURI->ad_type ) )
1276 struct berval bv, nbase;
1279 Backend *b2 = op->o_bd;
1281 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1284 op->o_bd = select_backend( op_ndn, 0, 0 );
1285 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1289 rc = LDAP_COMPARE_FALSE;
1290 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1291 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1296 BER_BVZERO( &nbase );
1297 /* host part must be empty */
1298 /* attrs and extensions parts must be empty */
1299 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1300 ludp->lud_attrs || ludp->lud_exts )
1304 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1305 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1306 op->o_tmpmemctx ) != LDAP_SUCCESS )
1310 switch ( ludp->lud_scope ) {
1311 case LDAP_SCOPE_BASE:
1312 if ( !dn_match( &nbase, op_ndn ) ) {
1316 case LDAP_SCOPE_ONELEVEL:
1317 dnParent( op_ndn, &bv );
1318 if ( !dn_match( &nbase, &bv ) ) {
1322 case LDAP_SCOPE_SUBTREE:
1323 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1327 #ifdef LDAP_SCOPE_SUBORDINATE
1328 case LDAP_SCOPE_SUBORDINATE:
1329 if ( dn_match( &nbase, op_ndn ) ||
1330 !dnIsSuffix( op_ndn, &nbase ) )
1336 filter = str2filter_x( op, ludp->lud_filter );
1338 if ( test_filter( NULL, user, filter ) ==
1343 filter_free_x( op, filter );
1346 ldap_free_urldesc( ludp );
1347 if ( !BER_BVISNULL( &nbase ) ) {
1348 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1350 if ( rc == 0 ) break;
1352 if ( user != target ) {
1353 be_entry_release_r( op, user );
1358 rc = value_find_ex( group_at,
1359 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1360 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1361 a->a_nvals, op_ndn, op->o_tmpmemctx );
1362 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1363 rc = LDAP_COMPARE_FALSE;
1366 rc = LDAP_NO_SUCH_ATTRIBUTE;
1369 be_entry_release_r( op, e );
1372 rc = LDAP_NO_SUCH_OBJECT;
1376 if ( op->o_pb ) call_group_postop_plugins( op );
1377 #endif /* LDAP_SLAPI */
1379 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1380 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1382 g->ga_be = op->o_bd;
1383 g->ga_oc = group_oc;
1384 g->ga_at = group_at;
1386 g->ga_len = gr_ndn->bv_len;
1387 strcpy( g->ga_ndn, gr_ndn->bv_val );
1388 g->ga_next = op->o_groups;
1397 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1401 BerVarray *vals = (BerVarray *)c->cac_private;
1402 Operation *op = NULL;
1405 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1410 if ( op->o_conn && access_allowed( op,
1411 e, a->a_desc, NULL, ACL_AUTH,
1412 &c->cac_acl_state ) == 0 ) {
1416 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1418 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1420 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1421 if ( op->o_conn && access_allowed( op,
1424 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1428 &a->a_nvals[i], op->o_tmpmemctx );
1429 if ( !BER_BVISNULL( &v[j] ) ) {
1435 op->o_tmpfree( v, op->o_tmpmemctx );
1439 BER_BVZERO( &v[j] );
1446 #endif /* LDAP_SLAPI */
1453 AttributeDescription *entry_at,
1455 slap_access_t access )
1458 Attribute *a = NULL;
1459 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1460 AccessControlState acl_state = ACL_STATE_INIT;
1461 Backend *be = op->o_bd;
1463 op->o_bd = select_backend( edn, 0, 0 );
1465 if ( target && dn_match( &target->e_nname, edn ) ) {
1469 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1473 a = attr_find( e->e_attrs, entry_at );
1475 SlapReply rs = { 0 };
1476 AttributeName anlist[ 2 ];
1478 anlist[ 0 ].an_name = entry_at->ad_cname;
1479 anlist[ 0 ].an_desc = entry_at;
1480 BER_BVZERO( &anlist[ 1 ].an_name );
1481 rs.sr_attrs = anlist;
1483 /* NOTE: backend_operational() is also called
1484 * when returning results, so it's supposed
1485 * to do no harm to entries */
1487 rc = backend_operational( op, &rs );
1490 if ( rc == LDAP_SUCCESS ) {
1491 if ( rs.sr_operational_attrs ) {
1493 a = rs.sr_operational_attrs;
1496 rc = LDAP_NO_SUCH_ATTRIBUTE;
1504 if ( op->o_conn && access > ACL_NONE &&
1505 access_allowed( op, e, entry_at, NULL,
1506 access, &acl_state ) == 0 )
1508 rc = LDAP_INSUFFICIENT_ACCESS;
1512 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1515 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1517 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1519 if ( op->o_conn && access > ACL_NONE &&
1520 access_allowed( op, e, entry_at,
1527 ber_dupbv_x( &v[j], &a->a_nvals[i],
1529 if ( !BER_BVISNULL( &v[j] ) ) {
1534 op->o_tmpfree( v, op->o_tmpmemctx );
1536 rc = LDAP_INSUFFICIENT_ACCESS;
1539 BER_BVZERO( &v[j] );
1545 else if ( op->o_pb ) {
1546 /* try any computed attributes */
1547 computed_attr_context ctx;
1549 slapi_int_pblock_set_operation( op->o_pb, op );
1551 ctx.cac_pb = op->o_pb;
1552 ctx.cac_attrs = NULL;
1553 ctx.cac_userattrs = 0;
1554 ctx.cac_opattrs = 0;
1555 ctx.cac_acl_state = acl_state;
1556 ctx.cac_private = (void *)vals;
1558 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1560 rc = LDAP_INSUFFICIENT_ACCESS;
1566 #endif /* LDAP_SLAPI */
1567 freeit: if ( e != target ) {
1568 be_entry_release_r( op, e );
1580 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1582 struct berval *nval = (struct berval *)c->cac_private;
1583 Operation *op = NULL;
1585 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1590 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1592 #endif /* LDAP_SLAPI */
1599 AttributeDescription *entry_at,
1600 struct berval *nval,
1601 slap_access_t access,
1605 int rc = LDAP_INSUFFICIENT_ACCESS;
1606 Backend *be = op->o_bd;
1610 assert( op->o_conn );
1612 assert( access > ACL_NONE );
1614 op->o_bd = select_backend( edn, 0, 0 );
1616 if ( target && dn_match( &target->e_nname, edn ) ) {
1620 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1624 Attribute *a = NULL;
1627 if ( entry_at == NULL ) {
1628 entry_at = slap_schema.si_ad_entry;
1631 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1633 if ( access_allowed_mask( op, e, entry_at,
1634 NULL, access, NULL, mask ) == 0 )
1636 rc = LDAP_INSUFFICIENT_ACCESS;
1643 a = attr_find( e->e_attrs, entry_at );
1645 SlapReply rs = { 0 };
1646 AttributeName anlist[ 2 ];
1648 anlist[ 0 ].an_name = entry_at->ad_cname;
1649 anlist[ 0 ].an_desc = entry_at;
1650 BER_BVZERO( &anlist[ 1 ].an_name );
1651 rs.sr_attrs = anlist;
1653 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1655 /* NOTE: backend_operational() is also called
1656 * when returning results, so it's supposed
1657 * to do no harm to entries */
1659 rc = backend_operational( op, &rs );
1662 if ( rc == LDAP_SUCCESS ) {
1663 if ( rs.sr_operational_attrs ) {
1665 a = rs.sr_operational_attrs;
1668 rc = LDAP_NO_SUCH_OBJECT;
1674 if ( access_allowed_mask( op, e, entry_at,
1675 nval, access, NULL, mask ) == 0 )
1677 rc = LDAP_INSUFFICIENT_ACCESS;
1683 else if ( op->o_pb ) {
1684 /* try any computed attributes */
1685 computed_attr_context ctx;
1687 slapi_int_pblock_set_operation( op->o_pb, op );
1689 ctx.cac_pb = op->o_pb;
1690 ctx.cac_attrs = NULL;
1691 ctx.cac_userattrs = 0;
1692 ctx.cac_opattrs = 0;
1693 ctx.cac_private = (void *)nval;
1695 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1697 rc = LDAP_INSUFFICIENT_ACCESS;
1703 #endif /* LDAP_SLAPI */
1705 freeit: if ( e != target ) {
1706 be_entry_release_r( op, e );
1717 int backend_operational(
1725 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1726 /* just count them */ ;
1729 * If operational attributes (allegedly) are required,
1730 * and the backend supports specific operational attributes,
1731 * add them to the attribute list
1733 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1734 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1736 *ap = slap_operational_entryDN( rs->sr_entry );
1737 ap = &(*ap)->a_next;
1740 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1741 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1743 *ap = slap_operational_subschemaSubentry( op->o_bd );
1744 ap = &(*ap)->a_next;
1747 /* Let the overlays have a chance at this */
1749 if ( SLAP_ISOVERLAY( be_orig ) )
1750 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1752 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1753 op->o_bd && op->o_bd->be_operational != NULL )
1755 rc = op->o_bd->be_operational( op, rs );
1763 static void init_group_pblock( Operation *op, Entry *target,
1764 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1766 slapi_int_pblock_set_operation( op->o_pb, op );
1768 slapi_pblock_set( op->o_pb,
1769 SLAPI_X_GROUP_ENTRY, (void *)e );
1770 slapi_pblock_set( op->o_pb,
1771 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1772 slapi_pblock_set( op->o_pb,
1773 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1774 slapi_pblock_set( op->o_pb,
1775 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1778 static int call_group_preop_plugins( Operation *op )
1782 rc = slapi_int_call_plugins( op->o_bd,
1783 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1785 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1786 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1788 rc = LDAP_NO_SUCH_ATTRIBUTE;
1797 static void call_group_postop_plugins( Operation *op )
1799 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1801 #endif /* LDAP_SLAPI */