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 ( !LDAP_STAILQ_EMPTY( &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 );
280 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
281 si->si_be = &backendDB[i];
283 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
284 ldap_pvt_runqueue_insert( &slapd_rq,
285 si->si_interval, do_syncrepl, (void *) si );
286 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
294 int backend_num( Backend *be )
298 if( be == NULL ) return -1;
300 for( i = 0; i < nBackendDB; i++ ) {
301 if( be == &backendDB[i] ) return i;
306 int backend_shutdown( Backend *be )
312 /* shutdown a specific backend database */
314 if ( be->bd_info->bi_nDB == 0 ) {
315 /* no database of this type, we never opened it */
319 if ( be->bd_info->bi_db_close ) {
320 be->bd_info->bi_db_close( be );
323 if( be->bd_info->bi_close ) {
324 be->bd_info->bi_close( be->bd_info );
330 /* close each backend database */
331 for( i = 0; i < nBackendDB; i++ ) {
332 if ( backendDB[i].bd_info->bi_db_close ) {
333 backendDB[i].bd_info->bi_db_close(
338 Debug( LDAP_DEBUG_ANY,
339 "backend_close: bi_db_close %s failed!\n",
340 backendDB[i].be_type, 0, 0 );
344 /* close each backend type */
345 for( i = 0; i < nBackendInfo; i++ ) {
346 if( backendInfo[i].bi_nDB == 0 ) {
347 /* no database of this type */
351 if( backendInfo[i].bi_close ) {
352 backendInfo[i].bi_close(
357 /* close frontend, if required */
358 if ( frontendDB->bd_info->bi_db_close ) {
359 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
361 Debug( LDAP_DEBUG_ANY,
362 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
370 int backend_destroy(void)
374 syncinfo_t *si_entry;
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 while ( !LDAP_STAILQ_EMPTY( &bd->be_syncinfo )) {
383 si_entry = LDAP_STAILQ_FIRST( &bd->be_syncinfo );
384 LDAP_STAILQ_REMOVE_HEAD( &bd->be_syncinfo, si_next );
385 syncinfo_free( si_entry );
388 if ( bd->be_pending_csn_list ) {
389 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
391 struct slap_csn_entry *tmp_csne = csne;
393 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
394 ch_free( csne->ce_csn.bv_val );
395 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
400 if ( bd->bd_info->bi_db_destroy ) {
401 bd->bd_info->bi_db_destroy( bd );
403 ber_bvarray_free( bd->be_suffix );
404 ber_bvarray_free( bd->be_nsuffix );
405 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
406 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
407 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
408 acl_destroy( bd->be_acl, frontendDB->be_acl );
412 /* destroy each backend type */
413 for( i = 0; i < nBackendInfo; i++ ) {
414 if( backendInfo[i].bi_destroy ) {
415 backendInfo[i].bi_destroy(
421 if (backendInfo != slap_binfo) {
424 #endif /* SLAPD_MODULES */
429 /* destroy frontend database */
432 if ( bd->bd_info->bi_db_destroy ) {
433 bd->bd_info->bi_db_destroy( bd );
435 ber_bvarray_free( bd->be_suffix );
436 ber_bvarray_free( bd->be_nsuffix );
437 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
438 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
439 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
440 acl_destroy( bd->be_acl, frontendDB->be_acl );
446 BackendInfo* backend_info(const char *type)
450 /* search for the backend type */
451 for( i = 0; i < nBackendInfo; i++ ) {
452 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
453 return &backendInfo[i];
466 BackendInfo *bi = backend_info(type);
470 fprintf( stderr, "Unrecognized database type (%s)\n", type );
476 backendDB = (BackendDB *) ch_realloc(
478 (nBackendDB + 1) * sizeof(Backend) );
480 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
482 /* did realloc move our table? if so, fix up dependent pointers */
483 if ( be != backendDB ) {
485 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
486 be->be_pcl_mutexp = &be->be_pcl_mutex;
490 be = &backends[nbackends++];
493 be->be_def_limit = frontendDB->be_def_limit;
494 be->be_dfltaccess = frontendDB->be_dfltaccess;
496 be->be_restrictops = frontendDB->be_restrictops;
497 be->be_requires = frontendDB->be_requires;
498 be->be_ssf_set = frontendDB->be_ssf_set;
500 be->be_pcl_mutexp = &be->be_pcl_mutex;
501 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
503 LDAP_STAILQ_INIT( &be->be_syncinfo );
505 /* assign a default depth limit for alias deref */
506 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
509 rc = bi->bi_db_init( be );
513 fprintf( stderr, "database init failed (%s)\n", type );
527 for ( i = 0; i < nbackends; i++ ) {
528 if ( backends[i].bd_info->bi_db_close ) {
529 (*backends[i].bd_info->bi_db_close)( &backends[i] );
533 if ( frontendDB->bd_info->bi_db_close ) {
534 (*frontendDB->bd_info->bi_db_close)( frontendDB );
545 ber_len_t len, dnlen = dn->bv_len;
548 for ( i = 0; i < nbackends; i++ ) {
549 for ( j = 0; backends[i].be_nsuffix != NULL &&
550 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
552 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
558 len = backends[i].be_nsuffix[j].bv_len;
561 /* suffix is longer than DN */
566 * input DN is normalized, so the separator check
567 * need not look at escaping
569 if ( len && len < dnlen &&
570 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
575 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
576 &dn->bv_val[dnlen-len] ) == 0 )
581 if( manageDSAit && len == dnlen &&
582 !SLAP_GLUE_SUBORDINATE( be ) ) {
599 struct berval *bvsuffix )
604 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
607 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
616 be_isroot_dn( Backend *be, struct berval *ndn )
618 if ( !ndn->bv_len ) {
622 if ( !be->be_rootndn.bv_len ) {
626 return dn_match( &be->be_rootndn, ndn );
630 be_sync_update( Operation *op )
632 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
636 be_slurp_update( Operation *op )
638 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
639 be_isupdate_dn( op->o_bd, &op->o_ndn ));
643 be_shadow_update( Operation *op )
645 return ( SLAP_SHADOW( op->o_bd ) &&
646 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
650 be_isupdate_dn( Backend *be, struct berval *ndn )
652 if ( !ndn->bv_len ) return( 0 );
654 if ( !be->be_update_ndn.bv_len ) return( 0 );
656 return dn_match( &be->be_update_ndn, ndn );
660 be_root_dn( Backend *be )
662 return &be->be_rootdn;
666 be_isroot( Operation *op )
668 return be_isroot_dn( op->o_bd, &op->o_ndn );
672 be_isroot_pw( Operation *op )
676 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
680 if( op->o_bd->be_rootpw.bv_len == 0 ) {
684 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
685 ldap_pvt_thread_mutex_lock( &passwd_mutex );
687 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
691 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
693 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
695 lutil_passwd_sasl_conn = NULL;
697 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
709 if ( op->o_bd->be_release ) {
710 /* free and release entry from backend */
711 return op->o_bd->be_release( op, e, rw );
720 backend_unbind( Operation *op, SlapReply *rs )
724 for ( i = 0; i < nbackends; i++ ) {
725 #if defined( LDAP_SLAPI )
728 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
729 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
730 rc = slapi_int_call_plugins( &backends[i],
731 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
734 * A preoperation plugin failure will abort the
737 Debug(LDAP_DEBUG_TRACE,
738 "do_bind: Unbind preoperation plugin failed\n",
743 #endif /* defined( LDAP_SLAPI ) */
745 if ( backends[i].be_unbind ) {
746 op->o_bd = &backends[i];
747 (*backends[i].be_unbind)( op, rs );
750 #if defined( LDAP_SLAPI )
751 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
752 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
754 Debug(LDAP_DEBUG_TRACE,
755 "do_unbind: Unbind postoperation plugins failed\n",
758 #endif /* defined( LDAP_SLAPI ) */
765 backend_connection_init(
770 for ( i = 0; i < nbackends; i++ ) {
771 if ( backends[i].be_connection_init ) {
772 (*backends[i].be_connection_init)( &backends[i], conn);
780 backend_connection_destroy(
785 for ( i = 0; i < nbackends; i++ ) {
786 if ( backends[i].be_connection_destroy ) {
787 (*backends[i].be_connection_destroy)( &backends[i], conn);
795 backend_check_controls(
799 LDAPControl **ctrls = op->o_ctrls;
800 rs->sr_err = LDAP_SUCCESS;
803 for( ; *ctrls != NULL ; ctrls++ ) {
804 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
805 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
807 /* FIXME: standards compliance issue
809 * Per RFC 2251 (and LDAPBIS discussions), if the control
810 * is recognized and appropriate for the operation (which
811 * we've already verified), then the server should make
812 * use of the control when performing the operation
813 * (without regard to criticality). This code is incorrect
815 * 1) a service error (e.g., unwillingToPerform) should be
816 * returned where a particular backend cannot service the
818 * 2) this error should be returned irregardless of the
819 * criticality of the control.
821 rs->sr_text = "control unavailable in context";
822 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
832 backend_check_restrictions(
835 struct berval *opdata )
837 slap_mask_t restrictops;
838 slap_mask_t requires;
840 slap_mask_t exopflag = 0;
847 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
851 restrictops = op->o_bd->be_restrictops;
852 requires = op->o_bd->be_requires;
853 ssf = &op->o_bd->be_ssf_set;
856 restrictops = frontendDB->be_restrictops;
857 requires = frontendDB->be_requires;
858 ssf = &frontendDB->be_ssf_set;
861 switch( op->o_tag ) {
863 opflag = SLAP_RESTRICT_OP_ADD;
867 opflag = SLAP_RESTRICT_OP_BIND;
870 case LDAP_REQ_COMPARE:
871 opflag = SLAP_RESTRICT_OP_COMPARE;
873 case LDAP_REQ_DELETE:
875 opflag = SLAP_RESTRICT_OP_DELETE;
877 case LDAP_REQ_EXTENDED:
878 opflag = SLAP_RESTRICT_OP_EXTENDED;
881 /* treat unspecified as a modify */
882 opflag = SLAP_RESTRICT_OP_MODIFY;
887 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
890 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
894 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
895 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
899 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
900 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
904 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
905 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
910 /* treat everything else as a modify */
911 opflag = SLAP_RESTRICT_OP_MODIFY;
915 case LDAP_REQ_MODIFY:
917 opflag = SLAP_RESTRICT_OP_MODIFY;
919 case LDAP_REQ_RENAME:
921 opflag = SLAP_RESTRICT_OP_RENAME;
923 case LDAP_REQ_SEARCH:
924 opflag = SLAP_RESTRICT_OP_SEARCH;
926 case LDAP_REQ_UNBIND:
931 rs->sr_text = "restrict operations internal error";
932 rs->sr_err = LDAP_OTHER;
937 /* these checks don't apply to StartTLS */
939 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
940 if( op->o_transport_ssf < ssf->sss_transport ) {
941 rs->sr_text = op->o_transport_ssf
942 ? "stronger transport confidentiality required"
943 : "transport confidentiality required";
947 if( op->o_tls_ssf < ssf->sss_tls ) {
948 rs->sr_text = op->o_tls_ssf
949 ? "stronger TLS confidentiality required"
950 : "TLS confidentiality required";
955 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
956 /* simple bind specific check */
957 if( op->o_ssf < ssf->sss_simple_bind ) {
958 rs->sr_text = op->o_ssf
959 ? "stronger confidentiality required"
960 : "confidentiality required";
965 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
966 /* these checks don't apply to SASL bind */
968 if( op->o_sasl_ssf < ssf->sss_sasl ) {
969 rs->sr_text = op->o_sasl_ssf
970 ? "stronger SASL confidentiality required"
971 : "SASL confidentiality required";
975 if( op->o_ssf < ssf->sss_ssf ) {
976 rs->sr_text = op->o_ssf
977 ? "stronger confidentiality required"
978 : "confidentiality required";
984 if( op->o_transport_ssf < ssf->sss_update_transport ) {
985 rs->sr_text = op->o_transport_ssf
986 ? "stronger transport confidentiality required for update"
987 : "transport confidentiality required for update";
991 if( op->o_tls_ssf < ssf->sss_update_tls ) {
992 rs->sr_text = op->o_tls_ssf
993 ? "stronger TLS confidentiality required for update"
994 : "TLS confidentiality required for update";
998 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
999 rs->sr_text = op->o_sasl_ssf
1000 ? "stronger SASL confidentiality required for update"
1001 : "SASL confidentiality required for update";
1005 if( op->o_ssf < ssf->sss_update_ssf ) {
1006 rs->sr_text = op->o_ssf
1007 ? "stronger confidentiality required for update"
1008 : "confidentiality required for update";
1012 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1013 op->o_ndn.bv_len == 0 )
1015 rs->sr_text = "modifications require authentication";
1016 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1020 #ifdef SLAP_X_LISTENER_MOD
1021 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1022 /* no "w" mode means readonly */
1023 rs->sr_text = "modifications not allowed on this listener";
1024 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1027 #endif /* SLAP_X_LISTENER_MOD */
1032 /* these checks don't apply to Bind, StartTLS, or Unbind */
1034 if( requires & SLAP_REQUIRE_STRONG ) {
1035 /* should check mechanism */
1036 if( ( op->o_transport_ssf < ssf->sss_transport
1037 && op->o_authtype == LDAP_AUTH_SIMPLE )
1038 || op->o_dn.bv_len == 0 )
1040 rs->sr_text = "strong(er) authentication required";
1041 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1046 if( requires & SLAP_REQUIRE_SASL ) {
1047 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1048 rs->sr_text = "SASL authentication required";
1049 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1054 if( requires & SLAP_REQUIRE_AUTHC ) {
1055 if( op->o_dn.bv_len == 0 ) {
1056 rs->sr_text = "authentication required";
1057 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1062 if( requires & SLAP_REQUIRE_BIND ) {
1064 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1065 version = op->o_conn->c_protocol;
1066 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1069 /* no bind has occurred */
1070 rs->sr_text = "BIND required";
1071 rs->sr_err = LDAP_OPERATIONS_ERROR;
1076 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1077 if( op->o_protocol < LDAP_VERSION3 ) {
1078 /* no bind has occurred */
1079 rs->sr_text = "operation restricted to LDAPv3 clients";
1080 rs->sr_err = LDAP_OPERATIONS_ERROR;
1085 #ifdef SLAP_X_LISTENER_MOD
1086 if ( !starttls && op->o_dn.bv_len == 0 ) {
1087 if ( op->o_conn->c_listener &&
1088 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1090 /* no "x" mode means bind required */
1091 rs->sr_text = "bind required on this listener";
1092 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1097 if ( !starttls && !updateop ) {
1098 if ( op->o_conn->c_listener &&
1099 !( op->o_conn->c_listener->sl_perms &
1100 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1102 /* no "r" mode means no read */
1103 rs->sr_text = "read not allowed on this listener";
1104 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1108 #endif /* SLAP_X_LISTENER_MOD */
1112 if( ( restrictops & opflag )
1113 || ( exopflag && ( restrictops & exopflag ) ) ) {
1114 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1115 rs->sr_text = "read operations restricted";
1116 } else if ( restrictops & exopflag ) {
1117 rs->sr_text = "extended operation restricted";
1119 rs->sr_text = "operation restricted";
1121 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1125 rs->sr_err = LDAP_SUCCESS;
1129 int backend_check_referrals( Operation *op, SlapReply *rs )
1131 rs->sr_err = LDAP_SUCCESS;
1133 if( op->o_bd->be_chk_referrals ) {
1134 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1136 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1137 send_ldap_result( op, rs );
1149 AttributeDescription *at,
1157 if (op->o_bd == NULL) {
1158 rc = LDAP_NO_SUCH_OBJECT;
1159 } else if ( op->o_bd->be_fetch ) {
1160 rc = ( op->o_bd->be_fetch )( op, ndn,
1163 rc = LDAP_UNWILLING_TO_PERFORM;
1172 struct berval *gr_ndn,
1173 struct berval *op_ndn,
1174 ObjectClass *group_oc,
1175 AttributeDescription *group_at )
1181 Backend *be = op->o_bd;
1183 if ( op->o_abandon ) return SLAPD_ABANDON;
1185 op->o_bd = select_backend( gr_ndn, 0, 0 );
1187 for (g = op->o_groups; g; g=g->ga_next) {
1188 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1189 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1191 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1200 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1204 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1208 if ( op->o_pb != NULL ) {
1209 init_group_pblock( op, target, e, op_ndn, group_at );
1211 rc = call_group_preop_plugins( op );
1212 if ( rc == LDAP_SUCCESS ) {
1216 #endif /* LDAP_SLAPI */
1218 a = attr_find( e->e_attrs, group_at );
1220 /* If the attribute is a subtype of labeledURI, treat this as
1221 * a dynamic group ala groupOfURLs
1223 if (is_at_subtype( group_at->ad_type,
1224 slap_schema.si_ad_labeledURI->ad_type ) )
1228 struct berval bv, nbase;
1231 Backend *b2 = op->o_bd;
1233 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1236 op->o_bd = select_backend( op_ndn, 0, 0 );
1237 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1242 for (i=0; a->a_vals[i].bv_val; i++) {
1243 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1248 nbase.bv_val = NULL;
1249 /* host part must be empty */
1250 /* attrs and extensions parts must be empty */
1251 if (( ludp->lud_host && *ludp->lud_host ) ||
1252 ludp->lud_attrs || ludp->lud_exts )
1256 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1257 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1258 op->o_tmpmemctx ) != LDAP_SUCCESS )
1262 switch(ludp->lud_scope) {
1263 case LDAP_SCOPE_BASE:
1264 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1266 case LDAP_SCOPE_ONELEVEL:
1267 dnParent(op_ndn, &bv );
1268 if ( !dn_match( &nbase, &bv )) goto loopit;
1270 case LDAP_SCOPE_SUBTREE:
1271 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1273 #ifdef LDAP_SCOPE_SUBORDINATE
1274 case LDAP_SCOPE_SUBORDINATE:
1275 if ( dn_match( &nbase, op_ndn ) ||
1276 !dnIsSuffix( op_ndn, &nbase ) )
1282 filter = str2filter_x( op, ludp->lud_filter );
1284 if ( test_filter( NULL, user, filter ) ==
1289 filter_free_x( op, filter );
1292 ldap_free_urldesc( ludp );
1293 if ( nbase.bv_val ) {
1294 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1296 if ( rc == 0 ) break;
1298 if ( user != target ) {
1299 be_entry_release_r( op, user );
1304 rc = value_find_ex( group_at,
1305 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1306 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1307 a->a_nvals, op_ndn, op->o_tmpmemctx );
1310 rc = LDAP_NO_SUCH_ATTRIBUTE;
1313 be_entry_release_r( op, e );
1316 rc = LDAP_NO_SUCH_OBJECT;
1320 if ( op->o_pb ) call_group_postop_plugins( op );
1321 #endif /* LDAP_SLAPI */
1323 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1324 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1326 g->ga_be = op->o_bd;
1327 g->ga_oc = group_oc;
1328 g->ga_at = group_at;
1330 g->ga_len = gr_ndn->bv_len;
1331 strcpy(g->ga_ndn, gr_ndn->bv_val);
1332 g->ga_next = op->o_groups;
1341 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1345 BerVarray *vals = (BerVarray *)c->cac_private;
1346 Operation *op = NULL;
1349 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1354 if ( op->o_conn && access_allowed( op,
1355 e, a->a_desc, NULL, ACL_AUTH,
1356 &c->cac_acl_state ) == 0 ) {
1360 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1362 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1364 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1365 if ( op->o_conn && access_allowed( op,
1368 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1372 &a->a_nvals[i], op->o_tmpmemctx );
1373 if (v[j].bv_val ) j++;
1377 op->o_tmpfree( v, op->o_tmpmemctx );
1389 #endif /* LDAP_SLAPI */
1396 AttributeDescription *entry_at,
1398 slap_access_t access )
1402 int i, j, rc = LDAP_SUCCESS;
1403 AccessControlState acl_state = ACL_STATE_INIT;
1404 Backend *be = op->o_bd;
1406 op->o_bd = select_backend( edn, 0, 0 );
1408 if ( target && dn_match( &target->e_nname, edn ) ) {
1411 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1415 a = attr_find( e->e_attrs, entry_at );
1419 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1420 e, entry_at, NULL, access,
1421 &acl_state ) == 0 ) {
1422 rc = LDAP_INSUFFICIENT_ACCESS;
1426 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1428 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1430 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1431 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1434 access, &acl_state ) == 0 ) {
1438 &a->a_nvals[i], op->o_tmpmemctx );
1439 if (v[j].bv_val ) j++;
1442 op->o_tmpfree( v, op->o_tmpmemctx );
1444 rc = LDAP_INSUFFICIENT_ACCESS;
1453 else if ( op->o_pb ) {
1454 /* try any computed attributes */
1455 computed_attr_context ctx;
1456 AttributeName aname;
1458 slapi_int_pblock_set_operation( op->o_pb, op );
1460 ctx.cac_pb = op->o_pb;
1461 ctx.cac_attrs = NULL;
1462 ctx.cac_userattrs = 0;
1463 ctx.cac_opattrs = 0;
1464 ctx.cac_acl_state = acl_state;
1465 ctx.cac_private = (void *)vals;
1467 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1468 rc = LDAP_INSUFFICIENT_ACCESS;
1472 #endif /* LDAP_SLAPI */
1473 freeit: if (e != target ) {
1474 be_entry_release_r( op, e );
1482 int backend_operational(
1490 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1491 /* just count them */ ;
1494 * If operational attributes (allegedly) are required,
1495 * and the backend supports specific operational attributes,
1496 * add them to the attribute list
1498 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1499 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1501 *ap = slap_operational_entryDN( rs->sr_entry );
1502 ap = &(*ap)->a_next;
1505 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1506 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1508 *ap = slap_operational_subschemaSubentry( op->o_bd );
1509 ap = &(*ap)->a_next;
1512 /* Let the overlays have a chance at this */
1514 if ( SLAP_ISOVERLAY( be_orig ))
1515 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1517 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1518 op->o_bd && op->o_bd->be_operational != NULL )
1522 a = rs->sr_operational_attrs;
1523 rs->sr_operational_attrs = NULL;
1524 rc = op->o_bd->be_operational( op, rs );
1525 *ap = rs->sr_operational_attrs;
1527 rs->sr_operational_attrs = a;
1530 for ( ; *ap; ap = &(*ap)->a_next )
1531 /* just count them */ ;
1539 static void init_group_pblock( Operation *op, Entry *target,
1540 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1542 slapi_int_pblock_set_operation( op->o_pb, op );
1544 slapi_pblock_set( op->o_pb,
1545 SLAPI_X_GROUP_ENTRY, (void *)e );
1546 slapi_pblock_set( op->o_pb,
1547 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1548 slapi_pblock_set( op->o_pb,
1549 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1550 slapi_pblock_set( op->o_pb,
1551 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1554 static int call_group_preop_plugins( Operation *op )
1558 rc = slapi_int_call_plugins( op->o_bd,
1559 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1561 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1562 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1564 rc = LDAP_NO_SUCH_ATTRIBUTE;
1573 static void call_group_postop_plugins( Operation *op )
1575 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1577 #endif /* LDAP_SLAPI */