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-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
43 #include "slapi/slapi.h"
45 static void init_group_pblock( Operation *op, Entry *target,
46 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
47 static int call_group_preop_plugins( Operation *op );
48 static void call_group_postop_plugins( Operation *op );
49 #endif /* LDAP_SLAPI */
52 * If a module is configured as dynamic, its header should not
53 * get included into slapd. While this is a general rule and does
54 * not have much of an effect in UNIX, this rule should be adhered
55 * to for Windows, where dynamic object code should not be implicitly
56 * imported into slapd without appropriate __declspec(dllimport) directives.
60 * This file is automatically generated by configure; it defines
61 * the BackendInfo binfo[] structure with the configured static
62 * backend info. It assumes that every backend of type <name>
63 * provides an initialization function
65 * int name_back_initialize( BackendInfo *bi )
67 * that populates the rest of the structure.
73 BackendInfo *backendInfo = NULL;
76 BackendDB *backendDB = NULL;
78 ldap_pvt_thread_pool_t syncrepl_pool;
79 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
81 int backend_init(void)
85 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
87 if((nBackendInfo != 0) || (backendInfo != NULL)) {
88 /* already initialized */
89 Debug( LDAP_DEBUG_ANY,
90 "backend_init: already initialized.\n", 0, 0, 0 );
95 binfo[nBackendInfo].bi_type != NULL;
98 assert( binfo[nBackendInfo].bi_init );
100 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
103 Debug( LDAP_DEBUG_ANY,
104 "backend_init: initialized for type \"%s\"\n",
105 binfo[nBackendInfo].bi_type, 0, 0 );
106 /* destroy those we've already inited */
111 if ( binfo[nBackendInfo].bi_destroy ) {
112 binfo[nBackendInfo].bi_destroy(
113 &binfo[nBackendInfo] );
120 if ( nBackendInfo > 0) {
129 Debug( LDAP_DEBUG_ANY,
130 "backend_init: failed\n",
134 #endif /* SLAPD_MODULES */
137 int backend_add(BackendInfo *aBackendInfo)
141 if ( aBackendInfo->bi_init == NULL ) {
142 Debug( LDAP_DEBUG_ANY, "backend_add: "
143 "backend type \"%s\" does not have the (mandatory)init function\n",
144 aBackendInfo->bi_type, 0, 0 );
148 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
149 Debug( LDAP_DEBUG_ANY,
150 "backend_add: initialization for type \"%s\" failed\n",
151 aBackendInfo->bi_type, 0, 0 );
155 /* now add the backend type to the Backend Info List */
157 BackendInfo *newBackendInfo = 0;
159 /* if backendInfo == binfo no deallocation of old backendInfo */
160 if (backendInfo == binfo) {
161 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
162 AC_MEMCPY(newBackendInfo, backendInfo,
163 sizeof(BackendInfo) * nBackendInfo);
165 newBackendInfo = ch_realloc(backendInfo,
166 sizeof(BackendInfo) * (nBackendInfo + 1));
169 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
170 sizeof(BackendInfo));
171 backendInfo = newBackendInfo;
177 /* startup a specific backend database */
178 int backend_startup_one(Backend *be)
184 be->be_pending_csn_list = (struct be_pcl *)
185 ch_calloc( 1, sizeof( struct be_pcl ));
186 build_new_dn( &be->be_context_csn, be->be_nsuffix,
187 (struct berval *)&slap_ldapsync_cn_bv, NULL );
189 LDAP_TAILQ_INIT( be->be_pending_csn_list );
191 Debug( LDAP_DEBUG_TRACE,
192 "backend_startup: starting \"%s\"\n",
193 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
195 if ( be->bd_info->bi_db_open ) {
196 rc = be->bd_info->bi_db_open( be );
198 Debug( LDAP_DEBUG_ANY,
199 "backend_startup: bi_db_open failed! (%d)\n",
206 int backend_startup(Backend *be)
211 if( ! ( nBackendDB > 0 ) ) {
213 Debug( LDAP_DEBUG_ANY,
214 "backend_startup: %d databases to startup.\n",
220 if ( be->bd_info->bi_open ) {
221 rc = be->bd_info->bi_open( be->bd_info );
223 Debug( LDAP_DEBUG_ANY,
224 "backend_startup: bi_open failed!\n",
231 return backend_startup_one( be );
234 /* open frontend, if required */
235 if ( frontendDB->bd_info->bi_db_open ) {
236 rc = frontendDB->bd_info->bi_db_open( frontendDB );
238 Debug( LDAP_DEBUG_ANY,
239 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
245 /* open each backend type */
246 for( i = 0; i < nBackendInfo; i++ ) {
247 if( backendInfo[i].bi_nDB == 0) {
248 /* no database of this type, don't open */
252 if( backendInfo[i].bi_open ) {
253 rc = backendInfo[i].bi_open(
256 Debug( LDAP_DEBUG_ANY,
257 "backend_startup: bi_open %d failed!\n",
264 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
265 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
266 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
268 /* open each backend database */
269 for( i = 0; i < nBackendDB; i++ ) {
270 if ( backendDB[i].be_suffix == NULL ) {
271 Debug( LDAP_DEBUG_ANY,
272 "backend_startup: warning, database %d (%s) "
274 i, backendDB[i].bd_info->bi_type, 0 );
276 /* append global access controls */
277 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
279 rc = backend_startup_one( &backendDB[i] );
284 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
287 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
288 backendDB[i].be_modify && backendDB[i].be_delete )) {
289 Debug( LDAP_DEBUG_ANY,
290 "backend_startup: database(%d) does not support "
291 "operations required for syncrepl", i, 0, 0 );
295 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
296 si->si_be = &backendDB[i];
298 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
299 ldap_pvt_runqueue_insert( &syncrepl_rq,
300 si->si_interval, do_syncrepl, (void *) si );
301 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
309 int backend_num( Backend *be )
313 if( be == NULL ) return -1;
315 for( i = 0; i < nBackendDB; i++ ) {
316 if( be == &backendDB[i] ) return i;
321 int backend_shutdown( Backend *be )
327 /* shutdown a specific backend database */
329 if ( be->bd_info->bi_nDB == 0 ) {
330 /* no database of this type, we never opened it */
334 if ( be->bd_info->bi_db_close ) {
335 be->bd_info->bi_db_close( be );
338 if( be->bd_info->bi_close ) {
339 be->bd_info->bi_close( be->bd_info );
345 /* close each backend database */
346 for( i = 0; i < nBackendDB; i++ ) {
347 if ( backendDB[i].bd_info->bi_db_close ) {
348 backendDB[i].bd_info->bi_db_close(
353 Debug( LDAP_DEBUG_ANY,
354 "backend_close: bi_db_close %s failed!\n",
355 backendDB[i].be_type, 0, 0 );
359 /* close each backend type */
360 for( i = 0; i < nBackendInfo; i++ ) {
361 if( backendInfo[i].bi_nDB == 0 ) {
362 /* no database of this type */
366 if( backendInfo[i].bi_close ) {
367 backendInfo[i].bi_close(
372 /* close frontend, if required */
373 if ( frontendDB->bd_info->bi_db_close ) {
374 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
376 Debug( LDAP_DEBUG_ANY,
377 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
385 int backend_destroy(void)
389 syncinfo_t *si_entry;
390 struct slap_csn_entry *csne;
392 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
394 /* destroy each backend database */
395 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
397 while ( !LDAP_STAILQ_EMPTY( &bd->be_syncinfo )) {
398 si_entry = LDAP_STAILQ_FIRST( &bd->be_syncinfo );
399 LDAP_STAILQ_REMOVE_HEAD( &bd->be_syncinfo, si_next );
400 syncinfo_free( si_entry );
403 if ( bd->be_pending_csn_list ) {
404 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
406 struct slap_csn_entry *tmp_csne = csne;
408 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
409 ch_free( csne->ce_csn->bv_val );
410 ch_free( csne->ce_csn );
411 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
416 if ( bd->bd_info->bi_db_destroy ) {
417 bd->bd_info->bi_db_destroy( bd );
419 ber_bvarray_free( bd->be_suffix );
420 ber_bvarray_free( bd->be_nsuffix );
421 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
422 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
423 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
424 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
425 acl_destroy( bd->be_acl, frontendDB->be_acl );
429 /* destroy each backend type */
430 for( i = 0; i < nBackendInfo; i++ ) {
431 if( backendInfo[i].bi_destroy ) {
432 backendInfo[i].bi_destroy(
438 if (backendInfo != binfo) {
441 #endif /* SLAPD_MODULES */
446 /* destroy frontend database */
448 if ( bd->bd_info->bi_db_destroy ) {
449 bd->bd_info->bi_db_destroy( bd );
451 ber_bvarray_free( bd->be_suffix );
452 ber_bvarray_free( bd->be_nsuffix );
453 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
454 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
455 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
456 acl_destroy( bd->be_acl, frontendDB->be_acl );
461 BackendInfo* backend_info(const char *type)
465 /* search for the backend type */
466 for( i = 0; i < nBackendInfo; i++ ) {
467 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
468 return &backendInfo[i];
481 BackendInfo *bi = backend_info(type);
485 fprintf( stderr, "Unrecognized database type (%s)\n", type );
491 backendDB = (BackendDB *) ch_realloc(
493 (nBackendDB + 1) * sizeof(Backend) );
495 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
497 /* did realloc move our table? if so, fix up dependent pointers */
498 if ( be != backendDB ) {
500 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
501 be->be_pcl_mutexp = &be->be_pcl_mutex;
505 be = &backends[nbackends++];
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_context_csn.bv_len = 0;
516 be->be_context_csn.bv_val = NULL;
517 be->be_pcl_mutexp = &be->be_pcl_mutex;
518 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
520 LDAP_STAILQ_INIT( &be->be_syncinfo );
522 /* assign a default depth limit for alias deref */
523 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
526 rc = bi->bi_db_init( be );
530 fprintf( stderr, "database init failed (%s)\n", type );
544 for ( i = 0; i < nbackends; i++ ) {
545 if ( backends[i].bd_info->bi_db_close ) {
546 (*backends[i].bd_info->bi_db_close)( &backends[i] );
550 if ( frontendDB->bd_info->bi_db_close ) {
551 (*frontendDB->bd_info->bi_db_close)( frontendDB );
562 ber_len_t len, dnlen = dn->bv_len;
565 for ( i = 0; i < nbackends; i++ ) {
566 for ( j = 0; backends[i].be_nsuffix != NULL &&
567 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
569 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
575 len = backends[i].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( backends[i].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 )
621 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
624 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
633 be_isroot_dn( Backend *be, struct berval *ndn )
635 if ( !ndn->bv_len ) {
639 if ( !be->be_rootndn.bv_len ) {
643 return dn_match( &be->be_rootndn, ndn );
647 be_sync_update( Operation *op )
649 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
653 be_slurp_update( Operation *op )
655 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
656 be_isupdate_dn( op->o_bd, &op->o_ndn ));
660 be_shadow_update( Operation *op )
662 return ( SLAP_SHADOW( op->o_bd ) &&
663 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
667 be_isupdate_dn( Backend *be, struct berval *ndn )
669 if ( !ndn->bv_len ) return( 0 );
671 if ( !be->be_update_ndn.bv_len ) return( 0 );
673 return dn_match( &be->be_update_ndn, ndn );
677 be_root_dn( Backend *be )
679 return &be->be_rootdn;
683 be_isroot( Operation *op )
685 return be_isroot_dn( op->o_bd, &op->o_ndn );
689 be_isroot_pw( Operation *op )
693 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
697 if( op->o_bd->be_rootpw.bv_len == 0 ) {
701 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
702 ldap_pvt_thread_mutex_lock( &passwd_mutex );
704 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
708 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
710 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
712 lutil_passwd_sasl_conn = NULL;
714 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
726 if ( op->o_bd->be_release ) {
727 /* free and release entry from backend */
728 return op->o_bd->be_release( op, e, rw );
737 backend_unbind( Operation *op, SlapReply *rs )
741 for ( i = 0; i < nbackends; i++ ) {
742 #if defined( LDAP_SLAPI )
745 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
746 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
747 rc = slapi_int_call_plugins( &backends[i],
748 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
751 * A preoperation plugin failure will abort the
754 Debug(LDAP_DEBUG_TRACE,
755 "do_bind: Unbind preoperation plugin failed\n",
760 #endif /* defined( LDAP_SLAPI ) */
762 if ( backends[i].be_unbind ) {
763 op->o_bd = &backends[i];
764 (*backends[i].be_unbind)( op, rs );
767 #if defined( LDAP_SLAPI )
768 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
769 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
771 Debug(LDAP_DEBUG_TRACE,
772 "do_unbind: Unbind postoperation plugins failed\n",
775 #endif /* defined( LDAP_SLAPI ) */
782 backend_connection_init(
787 for ( i = 0; i < nbackends; i++ ) {
788 if ( backends[i].be_connection_init ) {
789 (*backends[i].be_connection_init)( &backends[i], conn);
797 backend_connection_destroy(
802 for ( i = 0; i < nbackends; i++ ) {
803 if ( backends[i].be_connection_destroy ) {
804 (*backends[i].be_connection_destroy)( &backends[i], conn);
812 backend_check_controls(
816 LDAPControl **ctrls = op->o_ctrls;
817 rs->sr_err = LDAP_SUCCESS;
820 for( ; *ctrls != NULL ; ctrls++ ) {
821 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
822 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
824 /* FIXME: standards compliance issue
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
830 * (without regard to criticality). This code is incorrect
832 * 1) a service error (e.g., unwillingToPerform) should be
833 * returned where a particular backend cannot service the
835 * 2) this error should be returned irregardless of the
836 * criticality of the control.
838 rs->sr_text = "control unavailable in context";
839 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
849 backend_check_restrictions(
852 struct berval *opdata )
854 slap_mask_t restrictops;
855 slap_mask_t requires;
857 slap_mask_t exopflag = 0;
864 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
868 restrictops = op->o_bd->be_restrictops;
869 requires = op->o_bd->be_requires;
870 ssf = &op->o_bd->be_ssf_set;
873 restrictops = frontendDB->be_restrictops;
874 requires = frontendDB->be_requires;
875 ssf = &frontendDB->be_ssf_set;
878 switch( op->o_tag ) {
880 opflag = SLAP_RESTRICT_OP_ADD;
884 opflag = SLAP_RESTRICT_OP_BIND;
887 case LDAP_REQ_COMPARE:
888 opflag = SLAP_RESTRICT_OP_COMPARE;
890 case LDAP_REQ_DELETE:
892 opflag = SLAP_RESTRICT_OP_DELETE;
894 case LDAP_REQ_EXTENDED:
895 opflag = SLAP_RESTRICT_OP_EXTENDED;
898 /* treat unspecified as a modify */
899 opflag = SLAP_RESTRICT_OP_MODIFY;
904 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
907 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
911 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
912 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
916 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
917 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
921 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
922 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
927 /* treat everything else as a modify */
928 opflag = SLAP_RESTRICT_OP_MODIFY;
932 case LDAP_REQ_MODIFY:
934 opflag = SLAP_RESTRICT_OP_MODIFY;
936 case LDAP_REQ_RENAME:
938 opflag = SLAP_RESTRICT_OP_RENAME;
940 case LDAP_REQ_SEARCH:
941 opflag = SLAP_RESTRICT_OP_SEARCH;
943 case LDAP_REQ_UNBIND:
948 rs->sr_text = "restrict operations internal error";
949 rs->sr_err = LDAP_OTHER;
954 /* these checks don't apply to StartTLS */
956 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
957 if( op->o_transport_ssf < ssf->sss_transport ) {
958 rs->sr_text = op->o_transport_ssf
959 ? "stronger transport confidentiality required"
960 : "transport confidentiality required";
964 if( op->o_tls_ssf < ssf->sss_tls ) {
965 rs->sr_text = op->o_tls_ssf
966 ? "stronger TLS confidentiality required"
967 : "TLS confidentiality required";
972 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
973 /* simple bind specific check */
974 if( op->o_ssf < ssf->sss_simple_bind ) {
975 rs->sr_text = op->o_ssf
976 ? "stronger confidentiality required"
977 : "confidentiality required";
982 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
983 /* these checks don't apply to SASL bind */
985 if( op->o_sasl_ssf < ssf->sss_sasl ) {
986 rs->sr_text = op->o_sasl_ssf
987 ? "stronger SASL confidentiality required"
988 : "SASL confidentiality required";
992 if( op->o_ssf < ssf->sss_ssf ) {
993 rs->sr_text = op->o_ssf
994 ? "stronger confidentiality required"
995 : "confidentiality required";
1001 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1002 rs->sr_text = op->o_transport_ssf
1003 ? "stronger transport confidentiality required for update"
1004 : "transport confidentiality required for update";
1008 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1009 rs->sr_text = op->o_tls_ssf
1010 ? "stronger TLS confidentiality required for update"
1011 : "TLS confidentiality required for update";
1015 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1016 rs->sr_text = op->o_sasl_ssf
1017 ? "stronger SASL confidentiality required for update"
1018 : "SASL confidentiality required for update";
1022 if( op->o_ssf < ssf->sss_update_ssf ) {
1023 rs->sr_text = op->o_ssf
1024 ? "stronger confidentiality required for update"
1025 : "confidentiality required for update";
1029 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1030 op->o_ndn.bv_len == 0 )
1032 rs->sr_text = "modifications require authentication";
1033 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1037 #ifdef SLAP_X_LISTENER_MOD
1038 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1039 /* no "w" mode means readonly */
1040 rs->sr_text = "modifications not allowed on this listener";
1041 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1044 #endif /* SLAP_X_LISTENER_MOD */
1049 /* these checks don't apply to Bind, StartTLS, or Unbind */
1051 if( requires & SLAP_REQUIRE_STRONG ) {
1052 /* should check mechanism */
1053 if( ( op->o_transport_ssf < ssf->sss_transport
1054 && op->o_authtype == LDAP_AUTH_SIMPLE )
1055 || op->o_dn.bv_len == 0 )
1057 rs->sr_text = "strong(er) authentication required";
1058 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1063 if( requires & SLAP_REQUIRE_SASL ) {
1064 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1065 rs->sr_text = "SASL authentication required";
1066 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1071 if( requires & SLAP_REQUIRE_AUTHC ) {
1072 if( op->o_dn.bv_len == 0 ) {
1073 rs->sr_text = "authentication required";
1074 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1079 if( requires & SLAP_REQUIRE_BIND ) {
1081 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1082 version = op->o_conn->c_protocol;
1083 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1086 /* no bind has occurred */
1087 rs->sr_text = "BIND required";
1088 rs->sr_err = LDAP_OPERATIONS_ERROR;
1093 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1094 if( op->o_protocol < LDAP_VERSION3 ) {
1095 /* no bind has occurred */
1096 rs->sr_text = "operation restricted to LDAPv3 clients";
1097 rs->sr_err = LDAP_OPERATIONS_ERROR;
1102 #ifdef SLAP_X_LISTENER_MOD
1103 if ( !starttls && op->o_dn.bv_len == 0 ) {
1104 if ( op->o_conn->c_listener &&
1105 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1107 /* no "x" mode means bind required */
1108 rs->sr_text = "bind required on this listener";
1109 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1114 if ( !starttls && !updateop ) {
1115 if ( op->o_conn->c_listener &&
1116 !( op->o_conn->c_listener->sl_perms &
1117 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1119 /* no "r" mode means no read */
1120 rs->sr_text = "read not allowed on this listener";
1121 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1125 #endif /* SLAP_X_LISTENER_MOD */
1129 if( ( restrictops & opflag )
1130 || ( exopflag && ( restrictops & exopflag ) ) ) {
1131 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1132 rs->sr_text = "read operations restricted";
1133 } else if ( restrictops & exopflag ) {
1134 rs->sr_text = "extended operation restricted";
1136 rs->sr_text = "operation restricted";
1138 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1142 rs->sr_err = LDAP_SUCCESS;
1146 int backend_check_referrals( Operation *op, SlapReply *rs )
1148 rs->sr_err = LDAP_SUCCESS;
1150 if( op->o_bd->be_chk_referrals ) {
1151 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1153 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1154 send_ldap_result( op, rs );
1166 AttributeDescription *at,
1174 if (op->o_bd == NULL) {
1175 rc = LDAP_NO_SUCH_OBJECT;
1176 } else if ( op->o_bd->be_fetch ) {
1177 rc = ( op->o_bd->be_fetch )( op, ndn,
1180 rc = LDAP_UNWILLING_TO_PERFORM;
1189 struct berval *gr_ndn,
1190 struct berval *op_ndn,
1191 ObjectClass *group_oc,
1192 AttributeDescription *group_at )
1198 Backend *be = op->o_bd;
1200 if ( op->o_abandon ) return SLAPD_ABANDON;
1202 op->o_bd = select_backend( gr_ndn, 0, 0 );
1204 for (g = op->o_groups; g; g=g->ga_next) {
1205 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1206 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1208 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1217 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1221 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1225 if ( op->o_pb != NULL ) {
1226 init_group_pblock( op, target, e, op_ndn, group_at );
1228 rc = call_group_preop_plugins( op );
1229 if ( rc == LDAP_SUCCESS ) {
1233 #endif /* LDAP_SLAPI */
1235 a = attr_find( e->e_attrs, group_at );
1237 /* If the attribute is a subtype of labeledURI, treat this as
1238 * a dynamic group ala groupOfURLs
1240 if (is_at_subtype( group_at->ad_type,
1241 slap_schema.si_ad_labeledURI->ad_type ) )
1245 struct berval bv, nbase;
1248 Backend *b2 = op->o_bd;
1249 void *o_private = op->o_private;
1251 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1254 /* back-bdb stored lockinfo here, we saved it
1255 * above. Clear it out so that a new lock can be used.
1257 op->o_private = NULL;
1258 op->o_bd = select_backend( op_ndn, 0, 0 );
1259 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1264 for (i=0; a->a_vals[i].bv_val; i++) {
1265 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1270 nbase.bv_val = NULL;
1271 /* host part must be empty */
1272 /* attrs and extensions parts must be empty */
1273 if (( ludp->lud_host && *ludp->lud_host ) ||
1274 ludp->lud_attrs || ludp->lud_exts )
1278 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1279 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1280 op->o_tmpmemctx ) != LDAP_SUCCESS )
1284 switch(ludp->lud_scope) {
1285 case LDAP_SCOPE_BASE:
1286 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1288 case LDAP_SCOPE_ONELEVEL:
1289 dnParent(op_ndn, &bv );
1290 if ( !dn_match( &nbase, &bv )) goto loopit;
1292 case LDAP_SCOPE_SUBTREE:
1293 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1295 #ifdef LDAP_SCOPE_SUBORDINATE
1296 case LDAP_SCOPE_SUBORDINATE:
1297 if ( dn_match( &nbase, op_ndn ) ||
1298 !dnIsSuffix( op_ndn, &nbase ) )
1304 filter = str2filter_x( op, ludp->lud_filter );
1306 if ( test_filter( NULL, user, filter ) ==
1311 filter_free_x( op, filter );
1314 ldap_free_urldesc( ludp );
1315 if ( nbase.bv_val ) {
1316 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1318 if ( rc == 0 ) break;
1320 if ( user != target ) {
1321 be_entry_release_r( op, user );
1322 /* restore previous lockinfo, if any */
1323 op->o_private = o_private;
1328 rc = value_find_ex( group_at,
1329 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1330 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1331 a->a_nvals, op_ndn, op->o_tmpmemctx );
1334 rc = LDAP_NO_SUCH_ATTRIBUTE;
1337 be_entry_release_r( op, e );
1340 rc = LDAP_NO_SUCH_OBJECT;
1344 if ( op->o_pb ) call_group_postop_plugins( op );
1345 #endif /* LDAP_SLAPI */
1347 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1348 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1350 g->ga_be = op->o_bd;
1351 g->ga_oc = group_oc;
1352 g->ga_at = group_at;
1354 g->ga_len = gr_ndn->bv_len;
1355 strcpy(g->ga_ndn, gr_ndn->bv_val);
1356 g->ga_next = op->o_groups;
1365 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1369 BerVarray *vals = (BerVarray *)c->cac_private;
1370 Operation *op = NULL;
1373 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1378 if ( op->o_conn && access_allowed( op,
1379 e, a->a_desc, NULL, ACL_AUTH,
1380 &c->cac_acl_state ) == 0 ) {
1384 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1386 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1388 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1389 if ( op->o_conn && access_allowed( op,
1392 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1396 &a->a_nvals[i], op->o_tmpmemctx );
1397 if (v[j].bv_val ) j++;
1401 op->o_tmpfree( v, op->o_tmpmemctx );
1413 #endif /* LDAP_SLAPI */
1420 AttributeDescription *entry_at,
1422 slap_access_t access )
1426 int i, j, rc = LDAP_SUCCESS;
1427 AccessControlState acl_state = ACL_STATE_INIT;
1428 Backend *be = op->o_bd;
1430 op->o_bd = select_backend( edn, 0, 0 );
1432 if ( target && dn_match( &target->e_nname, edn ) ) {
1435 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1439 a = attr_find( e->e_attrs, entry_at );
1443 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1444 e, entry_at, NULL, access,
1445 &acl_state ) == 0 ) {
1446 rc = LDAP_INSUFFICIENT_ACCESS;
1450 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1452 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1454 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1455 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1458 access, &acl_state ) == 0 ) {
1462 &a->a_nvals[i], op->o_tmpmemctx );
1463 if (v[j].bv_val ) j++;
1466 op->o_tmpfree( v, op->o_tmpmemctx );
1468 rc = LDAP_INSUFFICIENT_ACCESS;
1477 else if ( op->o_pb ) {
1478 /* try any computed attributes */
1479 computed_attr_context ctx;
1480 AttributeName aname;
1482 slapi_int_pblock_set_operation( op->o_pb, op );
1484 ctx.cac_pb = op->o_pb;
1485 ctx.cac_attrs = NULL;
1486 ctx.cac_userattrs = 0;
1487 ctx.cac_opattrs = 0;
1488 ctx.cac_acl_state = acl_state;
1489 ctx.cac_private = (void *)vals;
1491 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1492 rc = LDAP_INSUFFICIENT_ACCESS;
1496 #endif /* LDAP_SLAPI */
1497 freeit: if (e != target ) {
1498 be_entry_release_r( op, e );
1506 int backend_operational(
1513 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1514 /* just count them */ ;
1517 * If operational attributes (allegedly) are required,
1518 * and the backend supports specific operational attributes,
1519 * add them to the attribute list
1521 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1522 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1524 *ap = slap_operational_entryDN( rs->sr_entry );
1525 ap = &(*ap)->a_next;
1528 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1529 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1531 *ap = slap_operational_subschemaSubentry( op->o_bd );
1532 ap = &(*ap)->a_next;
1535 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1536 op->o_bd && op->o_bd->be_operational != NULL )
1540 a = rs->sr_operational_attrs;
1541 rs->sr_operational_attrs = NULL;
1542 rc = op->o_bd->be_operational( op, rs );
1543 *ap = rs->sr_operational_attrs;
1545 rs->sr_operational_attrs = a;
1548 for ( ; *ap; ap = &(*ap)->a_next )
1549 /* just count them */ ;
1556 static void init_group_pblock( Operation *op, Entry *target,
1557 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1559 slapi_int_pblock_set_operation( op->o_pb, op );
1561 slapi_pblock_set( op->o_pb,
1562 SLAPI_X_GROUP_ENTRY, (void *)e );
1563 slapi_pblock_set( op->o_pb,
1564 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1565 slapi_pblock_set( op->o_pb,
1566 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1567 slapi_pblock_set( op->o_pb,
1568 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1571 static int call_group_preop_plugins( Operation *op )
1575 rc = slapi_int_call_plugins( op->o_bd,
1576 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1578 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1579 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1581 rc = LDAP_NO_SUCH_ATTRIBUTE;
1590 static void call_group_postop_plugins( Operation *op )
1592 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1594 #endif /* LDAP_SLAPI */