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 ] )
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 not unavailable in a particular context, hence the
833 * return of unwillingToPerform.
835 rs->sr_text = "control unavailable in context";
836 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
841 case LDAP_COMPARE_TRUE:
846 rs->sr_text = "unable to check control";
847 rs->sr_err = LDAP_OTHER;
858 backend_check_restrictions(
861 struct berval *opdata )
863 slap_mask_t restrictops;
864 slap_mask_t requires;
866 slap_mask_t exopflag = 0;
873 int rc = SLAP_CB_CONTINUE;
875 if ( op->o_bd->be_chk_controls ) {
876 rc = ( *op->o_bd->be_chk_controls )( op, rs );
879 if ( rc == SLAP_CB_CONTINUE ) {
880 rc = backend_check_controls( op, rs );
883 if ( rc != LDAP_SUCCESS ) {
887 restrictops = op->o_bd->be_restrictops;
888 requires = op->o_bd->be_requires;
889 ssf = &op->o_bd->be_ssf_set;
892 restrictops = frontendDB->be_restrictops;
893 requires = frontendDB->be_requires;
894 ssf = &frontendDB->be_ssf_set;
897 switch( op->o_tag ) {
899 opflag = SLAP_RESTRICT_OP_ADD;
903 opflag = SLAP_RESTRICT_OP_BIND;
906 case LDAP_REQ_COMPARE:
907 opflag = SLAP_RESTRICT_OP_COMPARE;
909 case LDAP_REQ_DELETE:
911 opflag = SLAP_RESTRICT_OP_DELETE;
913 case LDAP_REQ_EXTENDED:
914 opflag = SLAP_RESTRICT_OP_EXTENDED;
917 /* treat unspecified as a modify */
918 opflag = SLAP_RESTRICT_OP_MODIFY;
923 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
926 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
930 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
931 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
935 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
936 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
940 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
941 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
946 /* treat everything else as a modify */
947 opflag = SLAP_RESTRICT_OP_MODIFY;
951 case LDAP_REQ_MODIFY:
953 opflag = SLAP_RESTRICT_OP_MODIFY;
955 case LDAP_REQ_RENAME:
957 opflag = SLAP_RESTRICT_OP_RENAME;
959 case LDAP_REQ_SEARCH:
960 opflag = SLAP_RESTRICT_OP_SEARCH;
962 case LDAP_REQ_UNBIND:
967 rs->sr_text = "restrict operations internal error";
968 rs->sr_err = LDAP_OTHER;
973 /* these checks don't apply to StartTLS */
975 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
976 if( op->o_transport_ssf < ssf->sss_transport ) {
977 rs->sr_text = op->o_transport_ssf
978 ? "stronger transport confidentiality required"
979 : "transport confidentiality required";
983 if( op->o_tls_ssf < ssf->sss_tls ) {
984 rs->sr_text = op->o_tls_ssf
985 ? "stronger TLS confidentiality required"
986 : "TLS confidentiality required";
991 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
992 /* simple bind specific check */
993 if( op->o_ssf < ssf->sss_simple_bind ) {
994 rs->sr_text = op->o_ssf
995 ? "stronger confidentiality required"
996 : "confidentiality required";
1001 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1002 /* these checks don't apply to SASL bind */
1004 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1005 rs->sr_text = op->o_sasl_ssf
1006 ? "stronger SASL confidentiality required"
1007 : "SASL confidentiality required";
1011 if( op->o_ssf < ssf->sss_ssf ) {
1012 rs->sr_text = op->o_ssf
1013 ? "stronger confidentiality required"
1014 : "confidentiality required";
1020 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1021 rs->sr_text = op->o_transport_ssf
1022 ? "stronger transport confidentiality required for update"
1023 : "transport confidentiality required for update";
1027 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1028 rs->sr_text = op->o_tls_ssf
1029 ? "stronger TLS confidentiality required for update"
1030 : "TLS confidentiality required for update";
1034 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1035 rs->sr_text = op->o_sasl_ssf
1036 ? "stronger SASL confidentiality required for update"
1037 : "SASL confidentiality required for update";
1041 if( op->o_ssf < ssf->sss_update_ssf ) {
1042 rs->sr_text = op->o_ssf
1043 ? "stronger confidentiality required for update"
1044 : "confidentiality required for update";
1048 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1049 BER_BVISEMPTY( &op->o_ndn ) )
1051 rs->sr_text = "modifications require authentication";
1052 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1056 #ifdef SLAP_X_LISTENER_MOD
1057 if ( op->o_conn->c_listener &&
1058 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1059 ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1061 /* no "w" mode means readonly */
1062 rs->sr_text = "modifications not allowed on this listener";
1063 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1066 #endif /* SLAP_X_LISTENER_MOD */
1071 /* these checks don't apply to Bind, StartTLS, or Unbind */
1073 if( requires & SLAP_REQUIRE_STRONG ) {
1074 /* should check mechanism */
1075 if( ( op->o_transport_ssf < ssf->sss_transport
1076 && op->o_authtype == LDAP_AUTH_SIMPLE )
1077 || BER_BVISEMPTY( &op->o_dn ) )
1079 rs->sr_text = "strong(er) authentication required";
1080 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1085 if( requires & SLAP_REQUIRE_SASL ) {
1086 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1087 rs->sr_text = "SASL authentication required";
1088 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1093 if( requires & SLAP_REQUIRE_AUTHC ) {
1094 if( BER_BVISEMPTY( &op->o_dn ) ) {
1095 rs->sr_text = "authentication required";
1096 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1101 if( requires & SLAP_REQUIRE_BIND ) {
1103 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1104 version = op->o_conn->c_protocol;
1105 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1108 /* no bind has occurred */
1109 rs->sr_text = "BIND required";
1110 rs->sr_err = LDAP_OPERATIONS_ERROR;
1115 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1116 if( op->o_protocol < LDAP_VERSION3 ) {
1117 /* no bind has occurred */
1118 rs->sr_text = "operation restricted to LDAPv3 clients";
1119 rs->sr_err = LDAP_OPERATIONS_ERROR;
1124 #ifdef SLAP_X_LISTENER_MOD
1125 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1126 if ( op->o_conn->c_listener &&
1127 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1129 /* no "x" mode means bind required */
1130 rs->sr_text = "bind required on this listener";
1131 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1136 if ( !starttls && !updateop ) {
1137 if ( op->o_conn->c_listener &&
1138 !( op->o_conn->c_listener->sl_perms &
1139 ( !BER_BVISEMPTY( &op->o_dn )
1140 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1142 /* no "r" mode means no read */
1143 rs->sr_text = "read not allowed on this listener";
1144 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1148 #endif /* SLAP_X_LISTENER_MOD */
1152 if( ( restrictops & opflag )
1153 || ( exopflag && ( restrictops & exopflag ) ) ) {
1154 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1155 rs->sr_text = "read operations restricted";
1156 } else if ( restrictops & exopflag ) {
1157 rs->sr_text = "extended operation restricted";
1159 rs->sr_text = "operation restricted";
1161 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1165 rs->sr_err = LDAP_SUCCESS;
1169 int backend_check_referrals( Operation *op, SlapReply *rs )
1171 rs->sr_err = LDAP_SUCCESS;
1173 if( op->o_bd->be_chk_referrals ) {
1174 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1176 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1177 send_ldap_result( op, rs );
1189 AttributeDescription *at,
1197 if (op->o_bd == NULL) {
1198 rc = LDAP_NO_SUCH_OBJECT;
1199 } else if ( op->o_bd->be_fetch ) {
1200 rc = ( op->o_bd->be_fetch )( op, ndn,
1203 rc = LDAP_UNWILLING_TO_PERFORM;
1212 struct berval *gr_ndn,
1213 struct berval *op_ndn,
1214 ObjectClass *group_oc,
1215 AttributeDescription *group_at )
1221 Backend *be = op->o_bd;
1223 if ( op->o_abandon ) return SLAPD_ABANDON;
1225 op->o_bd = select_backend( gr_ndn, 0, 0 );
1227 for ( g = op->o_groups; g; g = g->ga_next ) {
1228 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1229 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1233 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1243 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1247 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1251 if ( op->o_pb != NULL ) {
1252 init_group_pblock( op, target, e, op_ndn, group_at );
1254 rc = call_group_preop_plugins( op );
1255 if ( rc == LDAP_SUCCESS ) {
1259 #endif /* LDAP_SLAPI */
1261 a = attr_find( e->e_attrs, group_at );
1263 /* If the attribute is a subtype of labeledURI, treat this as
1264 * a dynamic group ala groupOfURLs
1266 if (is_at_subtype( group_at->ad_type,
1267 slap_schema.si_ad_labeledURI->ad_type ) )
1271 struct berval bv, nbase;
1274 Backend *b2 = op->o_bd;
1276 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1279 op->o_bd = select_backend( op_ndn, 0, 0 );
1280 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1284 rc = LDAP_COMPARE_FALSE;
1285 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1286 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1291 BER_BVZERO( &nbase );
1292 /* host part must be empty */
1293 /* attrs and extensions parts must be empty */
1294 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1295 ludp->lud_attrs || ludp->lud_exts )
1299 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1300 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1301 op->o_tmpmemctx ) != LDAP_SUCCESS )
1305 switch ( ludp->lud_scope ) {
1306 case LDAP_SCOPE_BASE:
1307 if ( !dn_match( &nbase, op_ndn ) ) {
1311 case LDAP_SCOPE_ONELEVEL:
1312 dnParent( op_ndn, &bv );
1313 if ( !dn_match( &nbase, &bv ) ) {
1317 case LDAP_SCOPE_SUBTREE:
1318 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1322 #ifdef LDAP_SCOPE_SUBORDINATE
1323 case LDAP_SCOPE_SUBORDINATE:
1324 if ( dn_match( &nbase, op_ndn ) ||
1325 !dnIsSuffix( op_ndn, &nbase ) )
1331 filter = str2filter_x( op, ludp->lud_filter );
1333 if ( test_filter( NULL, user, filter ) ==
1338 filter_free_x( op, filter );
1341 ldap_free_urldesc( ludp );
1342 if ( !BER_BVISNULL( &nbase ) ) {
1343 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1345 if ( rc == 0 ) break;
1347 if ( user != target ) {
1348 be_entry_release_r( op, user );
1353 rc = value_find_ex( group_at,
1354 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1355 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1356 a->a_nvals, op_ndn, op->o_tmpmemctx );
1357 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1358 rc = LDAP_COMPARE_FALSE;
1361 rc = LDAP_NO_SUCH_ATTRIBUTE;
1364 be_entry_release_r( op, e );
1367 rc = LDAP_NO_SUCH_OBJECT;
1371 if ( op->o_pb ) call_group_postop_plugins( op );
1372 #endif /* LDAP_SLAPI */
1374 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1375 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1377 g->ga_be = op->o_bd;
1378 g->ga_oc = group_oc;
1379 g->ga_at = group_at;
1381 g->ga_len = gr_ndn->bv_len;
1382 strcpy( g->ga_ndn, gr_ndn->bv_val );
1383 g->ga_next = op->o_groups;
1392 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1396 BerVarray *vals = (BerVarray *)c->cac_private;
1397 Operation *op = NULL;
1400 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1405 if ( op->o_conn && access_allowed( op,
1406 e, a->a_desc, NULL, ACL_AUTH,
1407 &c->cac_acl_state ) == 0 ) {
1411 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1413 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1415 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1416 if ( op->o_conn && access_allowed( op,
1419 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1423 &a->a_nvals[i], op->o_tmpmemctx );
1424 if ( !BER_BVISNULL( &v[j] ) ) {
1430 op->o_tmpfree( v, op->o_tmpmemctx );
1434 BER_BVZERO( &v[j] );
1441 #endif /* LDAP_SLAPI */
1448 AttributeDescription *entry_at,
1450 slap_access_t access )
1453 Attribute *a = NULL;
1454 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1455 AccessControlState acl_state = ACL_STATE_INIT;
1456 Backend *be = op->o_bd;
1458 op->o_bd = select_backend( edn, 0, 0 );
1460 if ( target && dn_match( &target->e_nname, edn ) ) {
1464 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1468 a = attr_find( e->e_attrs, entry_at );
1470 SlapReply rs = { 0 };
1471 AttributeName anlist[ 2 ];
1473 anlist[ 0 ].an_name = entry_at->ad_cname;
1474 anlist[ 0 ].an_desc = entry_at;
1475 BER_BVZERO( &anlist[ 1 ].an_name );
1476 rs.sr_attrs = anlist;
1478 /* NOTE: backend_operational() is also called
1479 * when returning results, so it's supposed
1480 * to do no harm to entries */
1482 rc = backend_operational( op, &rs );
1485 if ( rc == LDAP_SUCCESS ) {
1486 if ( rs.sr_operational_attrs ) {
1488 a = rs.sr_operational_attrs;
1491 rc = LDAP_NO_SUCH_ATTRIBUTE;
1499 if ( op->o_conn && access > ACL_NONE &&
1500 access_allowed( op, e, entry_at, NULL,
1501 access, &acl_state ) == 0 )
1503 rc = LDAP_INSUFFICIENT_ACCESS;
1507 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1510 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1512 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1514 if ( op->o_conn && access > ACL_NONE &&
1515 access_allowed( op, e, entry_at,
1522 ber_dupbv_x( &v[j], &a->a_nvals[i],
1524 if ( !BER_BVISNULL( &v[j] ) ) {
1529 op->o_tmpfree( v, op->o_tmpmemctx );
1531 rc = LDAP_INSUFFICIENT_ACCESS;
1534 BER_BVZERO( &v[j] );
1540 else if ( op->o_pb ) {
1541 /* try any computed attributes */
1542 computed_attr_context ctx;
1544 slapi_int_pblock_set_operation( op->o_pb, op );
1546 ctx.cac_pb = op->o_pb;
1547 ctx.cac_attrs = NULL;
1548 ctx.cac_userattrs = 0;
1549 ctx.cac_opattrs = 0;
1550 ctx.cac_acl_state = acl_state;
1551 ctx.cac_private = (void *)vals;
1553 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1555 rc = LDAP_INSUFFICIENT_ACCESS;
1561 #endif /* LDAP_SLAPI */
1562 freeit: if ( e != target ) {
1563 be_entry_release_r( op, e );
1575 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1577 struct berval *nval = (struct berval *)c->cac_private;
1578 Operation *op = NULL;
1580 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1585 return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1587 #endif /* LDAP_SLAPI */
1594 AttributeDescription *entry_at,
1595 struct berval *nval,
1596 slap_access_t access,
1600 int rc = LDAP_INSUFFICIENT_ACCESS;
1601 Backend *be = op->o_bd;
1605 assert( op->o_conn );
1607 assert( access > ACL_NONE );
1609 op->o_bd = select_backend( edn, 0, 0 );
1611 if ( target && dn_match( &target->e_nname, edn ) ) {
1615 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1619 Attribute *a = NULL;
1622 if ( entry_at == NULL ) {
1623 entry_at = slap_schema.si_ad_entry;
1626 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1628 if ( access_allowed_mask( op, e, entry_at,
1629 NULL, access, NULL, mask ) == 0 )
1631 rc = LDAP_INSUFFICIENT_ACCESS;
1638 a = attr_find( e->e_attrs, entry_at );
1640 SlapReply rs = { 0 };
1641 AttributeName anlist[ 2 ];
1643 anlist[ 0 ].an_name = entry_at->ad_cname;
1644 anlist[ 0 ].an_desc = entry_at;
1645 BER_BVZERO( &anlist[ 1 ].an_name );
1646 rs.sr_attrs = anlist;
1648 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1650 /* NOTE: backend_operational() is also called
1651 * when returning results, so it's supposed
1652 * to do no harm to entries */
1654 rc = backend_operational( op, &rs );
1657 if ( rc == LDAP_SUCCESS ) {
1658 if ( rs.sr_operational_attrs ) {
1660 a = rs.sr_operational_attrs;
1663 rc = LDAP_NO_SUCH_OBJECT;
1669 if ( access_allowed_mask( op, e, entry_at,
1670 nval, access, NULL, mask ) == 0 )
1672 rc = LDAP_INSUFFICIENT_ACCESS;
1678 else if ( op->o_pb ) {
1679 /* try any computed attributes */
1680 computed_attr_context ctx;
1682 slapi_int_pblock_set_operation( op->o_pb, op );
1684 ctx.cac_pb = op->o_pb;
1685 ctx.cac_attrs = NULL;
1686 ctx.cac_userattrs = 0;
1687 ctx.cac_opattrs = 0;
1688 ctx.cac_private = (void *)nval;
1690 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1692 rc = LDAP_INSUFFICIENT_ACCESS;
1698 #endif /* LDAP_SLAPI */
1700 freeit: if ( e != target ) {
1701 be_entry_release_r( op, e );
1712 int backend_operational(
1720 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1721 /* just count them */ ;
1724 * If operational attributes (allegedly) are required,
1725 * and the backend supports specific operational attributes,
1726 * add them to the attribute list
1728 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1729 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1731 *ap = slap_operational_entryDN( rs->sr_entry );
1732 ap = &(*ap)->a_next;
1735 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1736 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1738 *ap = slap_operational_subschemaSubentry( op->o_bd );
1739 ap = &(*ap)->a_next;
1742 /* Let the overlays have a chance at this */
1744 if ( SLAP_ISOVERLAY( be_orig ) )
1745 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1747 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1748 op->o_bd && op->o_bd->be_operational != NULL )
1750 rc = op->o_bd->be_operational( op, rs );
1758 static void init_group_pblock( Operation *op, Entry *target,
1759 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1761 slapi_int_pblock_set_operation( op->o_pb, op );
1763 slapi_pblock_set( op->o_pb,
1764 SLAPI_X_GROUP_ENTRY, (void *)e );
1765 slapi_pblock_set( op->o_pb,
1766 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1767 slapi_pblock_set( op->o_pb,
1768 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1769 slapi_pblock_set( op->o_pb,
1770 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1773 static int call_group_preop_plugins( Operation *op )
1777 rc = slapi_int_call_plugins( op->o_bd,
1778 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1780 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1781 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1783 rc = LDAP_NO_SUCH_ATTRIBUTE;
1792 static void call_group_postop_plugins( Operation *op )
1794 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1796 #endif /* LDAP_SLAPI */