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>
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 BackendInfo *backendInfo = NULL;
63 BackendDB *backendDB = NULL;
65 ldap_pvt_thread_pool_t syncrepl_pool;
66 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
68 int backend_init(void)
72 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
74 if((nBackendInfo != 0) || (backendInfo != NULL)) {
75 /* already initialized */
76 Debug( LDAP_DEBUG_ANY,
77 "backend_init: already initialized.\n", 0, 0, 0 );
82 slap_binfo[nBackendInfo].bi_type != NULL;
85 assert( slap_binfo[nBackendInfo].bi_init );
87 rc = slap_binfo[nBackendInfo].bi_init( &slap_binfo[nBackendInfo] );
90 Debug( LDAP_DEBUG_ANY,
91 "backend_init: initialized for type \"%s\"\n",
92 slap_binfo[nBackendInfo].bi_type, 0, 0 );
93 /* destroy those we've already inited */
98 if ( slap_binfo[nBackendInfo].bi_destroy ) {
99 slap_binfo[nBackendInfo].bi_destroy(
100 &slap_binfo[nBackendInfo] );
107 if ( nBackendInfo > 0) {
108 backendInfo = slap_binfo;
116 Debug( LDAP_DEBUG_ANY,
117 "backend_init: failed\n",
121 #endif /* SLAPD_MODULES */
124 int backend_add(BackendInfo *aBackendInfo)
128 if ( aBackendInfo->bi_init == NULL ) {
129 Debug( LDAP_DEBUG_ANY, "backend_add: "
130 "backend type \"%s\" does not have the (mandatory)init function\n",
131 aBackendInfo->bi_type, 0, 0 );
135 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
136 Debug( LDAP_DEBUG_ANY,
137 "backend_add: initialization for type \"%s\" failed\n",
138 aBackendInfo->bi_type, 0, 0 );
142 /* now add the backend type to the Backend Info List */
144 BackendInfo *newBackendInfo = 0;
146 /* if backendInfo == slap_binfo no deallocation of old backendInfo */
147 if (backendInfo == slap_binfo) {
148 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
149 AC_MEMCPY(newBackendInfo, backendInfo,
150 sizeof(BackendInfo) * nBackendInfo);
152 newBackendInfo = ch_realloc(backendInfo,
153 sizeof(BackendInfo) * (nBackendInfo + 1));
156 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
157 sizeof(BackendInfo));
158 backendInfo = newBackendInfo;
164 /* startup a specific backend database */
165 int backend_startup_one(Backend *be)
171 be->be_pending_csn_list = (struct be_pcl *)
172 ch_calloc( 1, sizeof( struct be_pcl ));
174 LDAP_TAILQ_INIT( be->be_pending_csn_list );
176 Debug( LDAP_DEBUG_TRACE,
177 "backend_startup: starting \"%s\"\n",
178 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
180 if ( be->bd_info->bi_db_open ) {
181 rc = be->bd_info->bi_db_open( be );
183 Debug( LDAP_DEBUG_ANY,
184 "backend_startup: bi_db_open failed! (%d)\n",
191 int backend_startup(Backend *be)
196 if( ! ( nBackendDB > 0 ) ) {
198 Debug( LDAP_DEBUG_ANY,
199 "backend_startup: %d databases to startup.\n",
205 if ( be->bd_info->bi_open ) {
206 rc = be->bd_info->bi_open( be->bd_info );
208 Debug( LDAP_DEBUG_ANY,
209 "backend_startup: bi_open failed!\n",
216 return backend_startup_one( be );
219 /* open frontend, if required */
220 if ( frontendDB->bd_info->bi_db_open ) {
221 rc = frontendDB->bd_info->bi_db_open( frontendDB );
223 Debug( LDAP_DEBUG_ANY,
224 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
230 /* open each backend type */
231 for( i = 0; i < nBackendInfo; i++ ) {
232 if( backendInfo[i].bi_nDB == 0) {
233 /* no database of this type, don't open */
237 if( backendInfo[i].bi_open ) {
238 rc = backendInfo[i].bi_open(
241 Debug( LDAP_DEBUG_ANY,
242 "backend_startup: bi_open %d failed!\n",
249 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
250 LDAP_STAILQ_INIT( &slapd_rq.task_list );
251 LDAP_STAILQ_INIT( &slapd_rq.run_list );
253 /* open each backend database */
254 for( i = 0; i < nBackendDB; i++ ) {
255 if ( backendDB[i].be_suffix == NULL ) {
256 Debug( LDAP_DEBUG_ANY,
257 "backend_startup: warning, database %d (%s) "
259 i, backendDB[i].bd_info->bi_type, 0 );
261 /* append global access controls */
262 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
264 rc = backend_startup_one( &backendDB[i] );
269 if ( backendDB[i].be_syncinfo ) {
272 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
273 backendDB[i].be_modify && backendDB[i].be_delete )) {
274 Debug( LDAP_DEBUG_ANY,
275 "backend_startup: database(%d) does not support "
276 "operations required for syncrepl", i, 0, 0 );
281 si = backendDB[i].be_syncinfo;
282 si->si_be = &backendDB[i];
284 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
285 ldap_pvt_runqueue_insert( &slapd_rq,
286 si->si_interval, do_syncrepl, (void *) si );
287 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
295 int backend_num( Backend *be )
299 if( be == NULL ) return -1;
301 for( i = 0; i < nBackendDB; i++ ) {
302 if( be == &backendDB[i] ) return i;
307 int backend_shutdown( Backend *be )
313 /* shutdown a specific backend database */
315 if ( be->bd_info->bi_nDB == 0 ) {
316 /* no database of this type, we never opened it */
320 if ( be->bd_info->bi_db_close ) {
321 be->bd_info->bi_db_close( be );
324 if( be->bd_info->bi_close ) {
325 be->bd_info->bi_close( be->bd_info );
331 /* close each backend database */
332 for( i = 0; i < nBackendDB; i++ ) {
333 if ( backendDB[i].bd_info->bi_db_close ) {
334 backendDB[i].bd_info->bi_db_close(
339 Debug( LDAP_DEBUG_ANY,
340 "backend_close: bi_db_close %s failed!\n",
341 backendDB[i].be_type, 0, 0 );
345 /* close each backend type */
346 for( i = 0; i < nBackendInfo; i++ ) {
347 if( backendInfo[i].bi_nDB == 0 ) {
348 /* no database of this type */
352 if( backendInfo[i].bi_close ) {
353 backendInfo[i].bi_close(
358 /* close frontend, if required */
359 if ( frontendDB->bd_info->bi_db_close ) {
360 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
362 Debug( LDAP_DEBUG_ANY,
363 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
371 int backend_destroy(void)
375 struct slap_csn_entry *csne;
377 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
379 /* destroy each backend database */
380 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
382 if ( bd->be_syncinfo ) {
383 syncinfo_free( bd->be_syncinfo );
386 if ( bd->be_pending_csn_list ) {
387 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
389 struct slap_csn_entry *tmp_csne = csne;
391 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
392 ch_free( csne->ce_csn.bv_val );
393 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
398 if ( bd->bd_info->bi_db_destroy ) {
399 bd->bd_info->bi_db_destroy( bd );
401 ber_bvarray_free( bd->be_suffix );
402 ber_bvarray_free( bd->be_nsuffix );
403 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
404 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
405 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
406 acl_destroy( bd->be_acl, frontendDB->be_acl );
410 /* destroy each backend type */
411 for( i = 0; i < nBackendInfo; i++ ) {
412 if( backendInfo[i].bi_destroy ) {
413 backendInfo[i].bi_destroy(
419 if (backendInfo != slap_binfo) {
422 #endif /* SLAPD_MODULES */
427 /* destroy frontend database */
430 if ( bd->bd_info->bi_db_destroy ) {
431 bd->bd_info->bi_db_destroy( bd );
433 ber_bvarray_free( bd->be_suffix );
434 ber_bvarray_free( bd->be_nsuffix );
435 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
436 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
437 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
438 acl_destroy( bd->be_acl, frontendDB->be_acl );
444 BackendInfo* backend_info(const char *type)
448 /* search for the backend type */
449 for( i = 0; i < nBackendInfo; i++ ) {
450 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
451 return &backendInfo[i];
464 BackendInfo *bi = backend_info(type);
468 fprintf( stderr, "Unrecognized database type (%s)\n", type );
474 backendDB = (BackendDB *) ch_realloc(
476 (nBackendDB + 1) * sizeof(Backend) );
478 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
480 /* did realloc move our table? if so, fix up dependent pointers */
481 if ( be != backendDB ) {
483 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
484 be->be_pcl_mutexp = &be->be_pcl_mutex;
488 be = &backends[nbackends++];
491 be->be_def_limit = frontendDB->be_def_limit;
492 be->be_dfltaccess = frontendDB->be_dfltaccess;
494 be->be_restrictops = frontendDB->be_restrictops;
495 be->be_requires = frontendDB->be_requires;
496 be->be_ssf_set = frontendDB->be_ssf_set;
498 be->be_pcl_mutexp = &be->be_pcl_mutex;
499 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
501 /* assign a default depth limit for alias deref */
502 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
505 rc = bi->bi_db_init( be );
509 fprintf( stderr, "database init failed (%s)\n", type );
523 for ( i = 0; i < nbackends; i++ ) {
524 if ( backends[i].bd_info->bi_db_close ) {
525 (*backends[i].bd_info->bi_db_close)( &backends[i] );
529 if ( frontendDB->bd_info->bi_db_close ) {
530 (*frontendDB->bd_info->bi_db_close)( frontendDB );
541 ber_len_t len, dnlen = dn->bv_len;
544 for ( i = 0; i < nbackends; i++ ) {
545 for ( j = 0; backends[i].be_nsuffix != NULL &&
546 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
548 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
554 len = backends[i].be_nsuffix[j].bv_len;
557 /* suffix is longer than DN */
562 * input DN is normalized, so the separator check
563 * need not look at escaping
565 if ( len && len < dnlen &&
566 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
571 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
572 &dn->bv_val[dnlen-len] ) == 0 )
577 if( manageDSAit && len == dnlen &&
578 !SLAP_GLUE_SUBORDINATE( be ) ) {
595 struct berval *bvsuffix )
600 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
603 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
612 be_isroot_dn( Backend *be, struct berval *ndn )
614 if ( !ndn->bv_len ) {
618 if ( !be->be_rootndn.bv_len ) {
622 return dn_match( &be->be_rootndn, ndn );
626 be_slurp_update( Operation *op )
628 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
629 be_isupdate_dn( op->o_bd, &op->o_ndn ));
633 be_shadow_update( Operation *op )
635 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
636 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn )));
640 be_isupdate_dn( Backend *be, struct berval *ndn )
642 if ( !ndn->bv_len ) return( 0 );
644 if ( !be->be_update_ndn.bv_len ) return( 0 );
646 return dn_match( &be->be_update_ndn, ndn );
650 be_root_dn( Backend *be )
652 return &be->be_rootdn;
656 be_isroot( Operation *op )
658 return be_isroot_dn( op->o_bd, &op->o_ndn );
662 be_isroot_pw( Operation *op )
666 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
670 if( op->o_bd->be_rootpw.bv_len == 0 ) {
674 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
675 ldap_pvt_thread_mutex_lock( &passwd_mutex );
677 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
681 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
683 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
685 lutil_passwd_sasl_conn = NULL;
687 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
699 if ( op->o_bd->be_release ) {
700 /* free and release entry from backend */
701 return op->o_bd->be_release( op, e, rw );
710 backend_unbind( Operation *op, SlapReply *rs )
714 for ( i = 0; i < nbackends; i++ ) {
715 #if defined( LDAP_SLAPI )
718 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
719 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
720 rc = slapi_int_call_plugins( &backends[i],
721 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
724 * A preoperation plugin failure will abort the
727 Debug(LDAP_DEBUG_TRACE,
728 "do_bind: Unbind preoperation plugin failed\n",
733 #endif /* defined( LDAP_SLAPI ) */
735 if ( backends[i].be_unbind ) {
736 op->o_bd = &backends[i];
737 (*backends[i].be_unbind)( op, rs );
740 #if defined( LDAP_SLAPI )
741 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
742 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
744 Debug(LDAP_DEBUG_TRACE,
745 "do_unbind: Unbind postoperation plugins failed\n",
748 #endif /* defined( LDAP_SLAPI ) */
755 backend_connection_init(
760 for ( i = 0; i < nbackends; i++ ) {
761 if ( backends[i].be_connection_init ) {
762 (*backends[i].be_connection_init)( &backends[i], conn);
770 backend_connection_destroy(
775 for ( i = 0; i < nbackends; i++ ) {
776 if ( backends[i].be_connection_destroy ) {
777 (*backends[i].be_connection_destroy)( &backends[i], conn);
785 backend_check_controls(
789 LDAPControl **ctrls = op->o_ctrls;
790 rs->sr_err = LDAP_SUCCESS;
793 for( ; *ctrls != NULL ; ctrls++ ) {
794 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
795 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
797 /* FIXME: standards compliance issue
799 * Per RFC 2251 (and LDAPBIS discussions), if the control
800 * is recognized and appropriate for the operation (which
801 * we've already verified), then the server should make
802 * use of the control when performing the operation
803 * (without regard to criticality). This code is incorrect
805 * 1) a service error (e.g., unwillingToPerform) should be
806 * returned where a particular backend cannot service the
808 * 2) this error should be returned irregardless of the
809 * criticality of the control.
811 rs->sr_text = "control unavailable in context";
812 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
822 backend_check_restrictions(
825 struct berval *opdata )
827 slap_mask_t restrictops;
828 slap_mask_t requires;
830 slap_mask_t exopflag = 0;
837 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
841 restrictops = op->o_bd->be_restrictops;
842 requires = op->o_bd->be_requires;
843 ssf = &op->o_bd->be_ssf_set;
846 restrictops = frontendDB->be_restrictops;
847 requires = frontendDB->be_requires;
848 ssf = &frontendDB->be_ssf_set;
851 switch( op->o_tag ) {
853 opflag = SLAP_RESTRICT_OP_ADD;
857 opflag = SLAP_RESTRICT_OP_BIND;
860 case LDAP_REQ_COMPARE:
861 opflag = SLAP_RESTRICT_OP_COMPARE;
863 case LDAP_REQ_DELETE:
865 opflag = SLAP_RESTRICT_OP_DELETE;
867 case LDAP_REQ_EXTENDED:
868 opflag = SLAP_RESTRICT_OP_EXTENDED;
871 /* treat unspecified as a modify */
872 opflag = SLAP_RESTRICT_OP_MODIFY;
877 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
880 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
884 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
885 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
889 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
890 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
894 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
895 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
900 /* treat everything else as a modify */
901 opflag = SLAP_RESTRICT_OP_MODIFY;
905 case LDAP_REQ_MODIFY:
907 opflag = SLAP_RESTRICT_OP_MODIFY;
909 case LDAP_REQ_RENAME:
911 opflag = SLAP_RESTRICT_OP_RENAME;
913 case LDAP_REQ_SEARCH:
914 opflag = SLAP_RESTRICT_OP_SEARCH;
916 case LDAP_REQ_UNBIND:
921 rs->sr_text = "restrict operations internal error";
922 rs->sr_err = LDAP_OTHER;
927 /* these checks don't apply to StartTLS */
929 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
930 if( op->o_transport_ssf < ssf->sss_transport ) {
931 rs->sr_text = op->o_transport_ssf
932 ? "stronger transport confidentiality required"
933 : "transport confidentiality required";
937 if( op->o_tls_ssf < ssf->sss_tls ) {
938 rs->sr_text = op->o_tls_ssf
939 ? "stronger TLS confidentiality required"
940 : "TLS confidentiality required";
945 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
946 /* simple bind specific check */
947 if( op->o_ssf < ssf->sss_simple_bind ) {
948 rs->sr_text = op->o_ssf
949 ? "stronger confidentiality required"
950 : "confidentiality required";
955 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
956 /* these checks don't apply to SASL bind */
958 if( op->o_sasl_ssf < ssf->sss_sasl ) {
959 rs->sr_text = op->o_sasl_ssf
960 ? "stronger SASL confidentiality required"
961 : "SASL confidentiality required";
965 if( op->o_ssf < ssf->sss_ssf ) {
966 rs->sr_text = op->o_ssf
967 ? "stronger confidentiality required"
968 : "confidentiality required";
974 if( op->o_transport_ssf < ssf->sss_update_transport ) {
975 rs->sr_text = op->o_transport_ssf
976 ? "stronger transport confidentiality required for update"
977 : "transport confidentiality required for update";
981 if( op->o_tls_ssf < ssf->sss_update_tls ) {
982 rs->sr_text = op->o_tls_ssf
983 ? "stronger TLS confidentiality required for update"
984 : "TLS confidentiality required for update";
988 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
989 rs->sr_text = op->o_sasl_ssf
990 ? "stronger SASL confidentiality required for update"
991 : "SASL confidentiality required for update";
995 if( op->o_ssf < ssf->sss_update_ssf ) {
996 rs->sr_text = op->o_ssf
997 ? "stronger confidentiality required for update"
998 : "confidentiality required for update";
1002 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1003 op->o_ndn.bv_len == 0 )
1005 rs->sr_text = "modifications require authentication";
1006 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1010 #ifdef SLAP_X_LISTENER_MOD
1011 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1012 /* no "w" mode means readonly */
1013 rs->sr_text = "modifications not allowed on this listener";
1014 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1017 #endif /* SLAP_X_LISTENER_MOD */
1022 /* these checks don't apply to Bind, StartTLS, or Unbind */
1024 if( requires & SLAP_REQUIRE_STRONG ) {
1025 /* should check mechanism */
1026 if( ( op->o_transport_ssf < ssf->sss_transport
1027 && op->o_authtype == LDAP_AUTH_SIMPLE )
1028 || op->o_dn.bv_len == 0 )
1030 rs->sr_text = "strong(er) authentication required";
1031 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1036 if( requires & SLAP_REQUIRE_SASL ) {
1037 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1038 rs->sr_text = "SASL authentication required";
1039 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1044 if( requires & SLAP_REQUIRE_AUTHC ) {
1045 if( op->o_dn.bv_len == 0 ) {
1046 rs->sr_text = "authentication required";
1047 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1052 if( requires & SLAP_REQUIRE_BIND ) {
1054 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1055 version = op->o_conn->c_protocol;
1056 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1059 /* no bind has occurred */
1060 rs->sr_text = "BIND required";
1061 rs->sr_err = LDAP_OPERATIONS_ERROR;
1066 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1067 if( op->o_protocol < LDAP_VERSION3 ) {
1068 /* no bind has occurred */
1069 rs->sr_text = "operation restricted to LDAPv3 clients";
1070 rs->sr_err = LDAP_OPERATIONS_ERROR;
1075 #ifdef SLAP_X_LISTENER_MOD
1076 if ( !starttls && op->o_dn.bv_len == 0 ) {
1077 if ( op->o_conn->c_listener &&
1078 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1080 /* no "x" mode means bind required */
1081 rs->sr_text = "bind required on this listener";
1082 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1087 if ( !starttls && !updateop ) {
1088 if ( op->o_conn->c_listener &&
1089 !( op->o_conn->c_listener->sl_perms &
1090 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1092 /* no "r" mode means no read */
1093 rs->sr_text = "read not allowed on this listener";
1094 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1098 #endif /* SLAP_X_LISTENER_MOD */
1102 if( ( restrictops & opflag )
1103 || ( exopflag && ( restrictops & exopflag ) ) ) {
1104 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1105 rs->sr_text = "read operations restricted";
1106 } else if ( restrictops & exopflag ) {
1107 rs->sr_text = "extended operation restricted";
1109 rs->sr_text = "operation restricted";
1111 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1115 rs->sr_err = LDAP_SUCCESS;
1119 int backend_check_referrals( Operation *op, SlapReply *rs )
1121 rs->sr_err = LDAP_SUCCESS;
1123 if( op->o_bd->be_chk_referrals ) {
1124 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1126 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1127 send_ldap_result( op, rs );
1139 AttributeDescription *at,
1147 if (op->o_bd == NULL) {
1148 rc = LDAP_NO_SUCH_OBJECT;
1149 } else if ( op->o_bd->be_fetch ) {
1150 rc = ( op->o_bd->be_fetch )( op, ndn,
1153 rc = LDAP_UNWILLING_TO_PERFORM;
1162 struct berval *gr_ndn,
1163 struct berval *op_ndn,
1164 ObjectClass *group_oc,
1165 AttributeDescription *group_at )
1171 Backend *be = op->o_bd;
1173 if ( op->o_abandon ) return SLAPD_ABANDON;
1175 op->o_bd = select_backend( gr_ndn, 0, 0 );
1177 for (g = op->o_groups; g; g=g->ga_next) {
1178 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1179 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1181 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1190 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1194 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1198 if ( op->o_pb != NULL ) {
1199 init_group_pblock( op, target, e, op_ndn, group_at );
1201 rc = call_group_preop_plugins( op );
1202 if ( rc == LDAP_SUCCESS ) {
1206 #endif /* LDAP_SLAPI */
1208 a = attr_find( e->e_attrs, group_at );
1210 /* If the attribute is a subtype of labeledURI, treat this as
1211 * a dynamic group ala groupOfURLs
1213 if (is_at_subtype( group_at->ad_type,
1214 slap_schema.si_ad_labeledURI->ad_type ) )
1218 struct berval bv, nbase;
1221 Backend *b2 = op->o_bd;
1223 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1226 op->o_bd = select_backend( op_ndn, 0, 0 );
1227 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1232 for (i=0; a->a_vals[i].bv_val; i++) {
1233 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1238 nbase.bv_val = NULL;
1239 /* host part must be empty */
1240 /* attrs and extensions parts must be empty */
1241 if (( ludp->lud_host && *ludp->lud_host ) ||
1242 ludp->lud_attrs || ludp->lud_exts )
1246 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1247 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1248 op->o_tmpmemctx ) != LDAP_SUCCESS )
1252 switch(ludp->lud_scope) {
1253 case LDAP_SCOPE_BASE:
1254 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1256 case LDAP_SCOPE_ONELEVEL:
1257 dnParent(op_ndn, &bv );
1258 if ( !dn_match( &nbase, &bv )) goto loopit;
1260 case LDAP_SCOPE_SUBTREE:
1261 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1263 #ifdef LDAP_SCOPE_SUBORDINATE
1264 case LDAP_SCOPE_SUBORDINATE:
1265 if ( dn_match( &nbase, op_ndn ) ||
1266 !dnIsSuffix( op_ndn, &nbase ) )
1272 filter = str2filter_x( op, ludp->lud_filter );
1274 if ( test_filter( NULL, user, filter ) ==
1279 filter_free_x( op, filter );
1282 ldap_free_urldesc( ludp );
1283 if ( nbase.bv_val ) {
1284 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1286 if ( rc == 0 ) break;
1288 if ( user != target ) {
1289 be_entry_release_r( op, user );
1294 rc = value_find_ex( group_at,
1295 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1296 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1297 a->a_nvals, op_ndn, op->o_tmpmemctx );
1300 rc = LDAP_NO_SUCH_ATTRIBUTE;
1303 be_entry_release_r( op, e );
1306 rc = LDAP_NO_SUCH_OBJECT;
1310 if ( op->o_pb ) call_group_postop_plugins( op );
1311 #endif /* LDAP_SLAPI */
1313 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1314 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1316 g->ga_be = op->o_bd;
1317 g->ga_oc = group_oc;
1318 g->ga_at = group_at;
1320 g->ga_len = gr_ndn->bv_len;
1321 strcpy(g->ga_ndn, gr_ndn->bv_val);
1322 g->ga_next = op->o_groups;
1331 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1335 BerVarray *vals = (BerVarray *)c->cac_private;
1336 Operation *op = NULL;
1339 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1344 if ( op->o_conn && access_allowed( op,
1345 e, a->a_desc, NULL, ACL_AUTH,
1346 &c->cac_acl_state ) == 0 ) {
1350 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1352 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1354 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1355 if ( op->o_conn && access_allowed( op,
1358 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1362 &a->a_nvals[i], op->o_tmpmemctx );
1363 if (v[j].bv_val ) j++;
1367 op->o_tmpfree( v, op->o_tmpmemctx );
1379 #endif /* LDAP_SLAPI */
1386 AttributeDescription *entry_at,
1388 slap_access_t access )
1391 Attribute *a = NULL;
1392 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1393 AccessControlState acl_state = ACL_STATE_INIT;
1394 Backend *be = op->o_bd;
1396 op->o_bd = select_backend( edn, 0, 0 );
1398 if ( target && dn_match( &target->e_nname, edn ) ) {
1402 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1406 a = attr_find( e->e_attrs, entry_at );
1408 SlapReply rs = { 0 };
1409 AttributeName anlist[ 2 ];
1411 anlist[ 0 ].an_name = entry_at->ad_cname;
1412 anlist[ 0 ].an_desc = entry_at;
1413 BER_BVZERO( &anlist[ 1 ].an_name );
1414 rs.sr_attrs = anlist;
1416 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1418 rc = backend_operational( op, &rs );
1420 if ( rc == LDAP_SUCCESS ) {
1421 a = rs.sr_operational_attrs;
1428 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1429 e, entry_at, NULL, access,
1430 &acl_state ) == 0 ) {
1431 rc = LDAP_INSUFFICIENT_ACCESS;
1435 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1438 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1440 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1442 if ( op->o_conn && access > ACL_NONE &&
1443 access_allowed( op, e,
1451 ber_dupbv_x( &v[j], &a->a_nvals[i],
1453 if ( !BER_BVISNULL( &v[j] ) ) {
1458 op->o_tmpfree( v, op->o_tmpmemctx );
1460 rc = LDAP_INSUFFICIENT_ACCESS;
1463 BER_BVZERO( &v[j] );
1469 else if ( op->o_pb ) {
1470 /* try any computed attributes */
1471 computed_attr_context ctx;
1473 slapi_int_pblock_set_operation( op->o_pb, op );
1475 ctx.cac_pb = op->o_pb;
1476 ctx.cac_attrs = NULL;
1477 ctx.cac_userattrs = 0;
1478 ctx.cac_opattrs = 0;
1479 ctx.cac_acl_state = acl_state;
1480 ctx.cac_private = (void *)vals;
1482 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1484 rc = LDAP_INSUFFICIENT_ACCESS;
1490 #endif /* LDAP_SLAPI */
1491 freeit: if ( e != target ) {
1492 be_entry_release_r( op, e );
1503 int backend_operational(
1511 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1512 /* just count them */ ;
1515 * If operational attributes (allegedly) are required,
1516 * and the backend supports specific operational attributes,
1517 * add them to the attribute list
1519 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1520 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1522 *ap = slap_operational_entryDN( rs->sr_entry );
1523 ap = &(*ap)->a_next;
1526 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1527 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1529 *ap = slap_operational_subschemaSubentry( op->o_bd );
1530 ap = &(*ap)->a_next;
1533 /* Let the overlays have a chance at this */
1535 if ( SLAP_ISOVERLAY( be_orig ))
1536 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1538 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1539 op->o_bd && op->o_bd->be_operational != NULL )
1543 a = rs->sr_operational_attrs;
1544 rs->sr_operational_attrs = NULL;
1545 rc = op->o_bd->be_operational( op, rs );
1546 *ap = rs->sr_operational_attrs;
1548 rs->sr_operational_attrs = a;
1551 for ( ; *ap; ap = &(*ap)->a_next )
1552 /* just count them */ ;
1560 static void init_group_pblock( Operation *op, Entry *target,
1561 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1563 slapi_int_pblock_set_operation( op->o_pb, op );
1565 slapi_pblock_set( op->o_pb,
1566 SLAPI_X_GROUP_ENTRY, (void *)e );
1567 slapi_pblock_set( op->o_pb,
1568 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1569 slapi_pblock_set( op->o_pb,
1570 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1571 slapi_pblock_set( op->o_pb,
1572 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1575 static int call_group_preop_plugins( Operation *op )
1579 rc = slapi_int_call_plugins( op->o_bd,
1580 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1582 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1583 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1585 rc = LDAP_NO_SUCH_ATTRIBUTE;
1594 static void call_group_postop_plugins( Operation *op )
1596 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1598 #endif /* LDAP_SLAPI */