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 * 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 ));
187 LDAP_TAILQ_INIT( be->be_pending_csn_list );
189 Debug( LDAP_DEBUG_TRACE,
190 "backend_startup: starting \"%s\"\n",
191 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
193 if ( be->bd_info->bi_db_open ) {
194 rc = be->bd_info->bi_db_open( be );
196 Debug( LDAP_DEBUG_ANY,
197 "backend_startup: bi_db_open failed! (%d)\n",
204 int backend_startup(Backend *be)
209 if( ! ( nBackendDB > 0 ) ) {
211 Debug( LDAP_DEBUG_ANY,
212 "backend_startup: %d databases to startup.\n",
218 if ( be->bd_info->bi_open ) {
219 rc = be->bd_info->bi_open( be->bd_info );
221 Debug( LDAP_DEBUG_ANY,
222 "backend_startup: bi_open failed!\n",
229 return backend_startup_one( be );
232 /* open frontend, if required */
233 if ( frontendDB->bd_info->bi_db_open ) {
234 rc = frontendDB->bd_info->bi_db_open( frontendDB );
236 Debug( LDAP_DEBUG_ANY,
237 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
243 /* open each backend type */
244 for( i = 0; i < nBackendInfo; i++ ) {
245 if( backendInfo[i].bi_nDB == 0) {
246 /* no database of this type, don't open */
250 if( backendInfo[i].bi_open ) {
251 rc = backendInfo[i].bi_open(
254 Debug( LDAP_DEBUG_ANY,
255 "backend_startup: bi_open %d failed!\n",
262 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
263 LDAP_STAILQ_INIT( &slapd_rq.task_list );
264 LDAP_STAILQ_INIT( &slapd_rq.run_list );
266 /* open each backend database */
267 for( i = 0; i < nBackendDB; i++ ) {
268 if ( backendDB[i].be_suffix == NULL ) {
269 Debug( LDAP_DEBUG_ANY,
270 "backend_startup: warning, database %d (%s) "
272 i, backendDB[i].bd_info->bi_type, 0 );
274 /* append global access controls */
275 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
277 rc = backend_startup_one( &backendDB[i] );
282 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
285 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
286 backendDB[i].be_modify && backendDB[i].be_delete )) {
287 Debug( LDAP_DEBUG_ANY,
288 "backend_startup: database(%d) does not support "
289 "operations required for syncrepl", i, 0, 0 );
293 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
294 si->si_be = &backendDB[i];
296 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
297 ldap_pvt_runqueue_insert( &slapd_rq,
298 si->si_interval, do_syncrepl, (void *) si );
299 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
307 int backend_num( Backend *be )
311 if( be == NULL ) return -1;
313 for( i = 0; i < nBackendDB; i++ ) {
314 if( be == &backendDB[i] ) return i;
319 int backend_shutdown( Backend *be )
325 /* shutdown a specific backend database */
327 if ( be->bd_info->bi_nDB == 0 ) {
328 /* no database of this type, we never opened it */
332 if ( be->bd_info->bi_db_close ) {
333 be->bd_info->bi_db_close( be );
336 if( be->bd_info->bi_close ) {
337 be->bd_info->bi_close( be->bd_info );
343 /* close each backend database */
344 for( i = 0; i < nBackendDB; i++ ) {
345 if ( backendDB[i].bd_info->bi_db_close ) {
346 backendDB[i].bd_info->bi_db_close(
351 Debug( LDAP_DEBUG_ANY,
352 "backend_close: bi_db_close %s failed!\n",
353 backendDB[i].be_type, 0, 0 );
357 /* close each backend type */
358 for( i = 0; i < nBackendInfo; i++ ) {
359 if( backendInfo[i].bi_nDB == 0 ) {
360 /* no database of this type */
364 if( backendInfo[i].bi_close ) {
365 backendInfo[i].bi_close(
370 /* close frontend, if required */
371 if ( frontendDB->bd_info->bi_db_close ) {
372 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
374 Debug( LDAP_DEBUG_ANY,
375 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
383 int backend_destroy(void)
387 syncinfo_t *si_entry;
388 struct slap_csn_entry *csne;
390 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
392 /* destroy each backend database */
393 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
395 while ( !LDAP_STAILQ_EMPTY( &bd->be_syncinfo )) {
396 si_entry = LDAP_STAILQ_FIRST( &bd->be_syncinfo );
397 LDAP_STAILQ_REMOVE_HEAD( &bd->be_syncinfo, si_next );
398 syncinfo_free( si_entry );
401 if ( bd->be_pending_csn_list ) {
402 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
404 struct slap_csn_entry *tmp_csne = csne;
406 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
407 ch_free( csne->ce_csn.bv_val );
408 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
413 if ( bd->bd_info->bi_db_destroy ) {
414 bd->bd_info->bi_db_destroy( bd );
416 ber_bvarray_free( bd->be_suffix );
417 ber_bvarray_free( bd->be_nsuffix );
418 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
419 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
420 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
421 acl_destroy( bd->be_acl, frontendDB->be_acl );
425 /* destroy each backend type */
426 for( i = 0; i < nBackendInfo; i++ ) {
427 if( backendInfo[i].bi_destroy ) {
428 backendInfo[i].bi_destroy(
434 if (backendInfo != binfo) {
437 #endif /* SLAPD_MODULES */
442 /* destroy frontend database */
445 if ( bd->bd_info->bi_db_destroy ) {
446 bd->bd_info->bi_db_destroy( bd );
448 ber_bvarray_free( bd->be_suffix );
449 ber_bvarray_free( bd->be_nsuffix );
450 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
451 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
452 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
453 acl_destroy( bd->be_acl, frontendDB->be_acl );
459 BackendInfo* backend_info(const char *type)
463 /* search for the backend type */
464 for( i = 0; i < nBackendInfo; i++ ) {
465 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
466 return &backendInfo[i];
479 BackendInfo *bi = backend_info(type);
483 fprintf( stderr, "Unrecognized database type (%s)\n", type );
489 backendDB = (BackendDB *) ch_realloc(
491 (nBackendDB + 1) * sizeof(Backend) );
493 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
495 /* did realloc move our table? if so, fix up dependent pointers */
496 if ( be != backendDB ) {
498 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
499 be->be_pcl_mutexp = &be->be_pcl_mutex;
503 be = &backends[nbackends++];
506 be->be_def_limit = frontendDB->be_def_limit;
507 be->be_dfltaccess = frontendDB->be_dfltaccess;
509 be->be_restrictops = frontendDB->be_restrictops;
510 be->be_requires = frontendDB->be_requires;
511 be->be_ssf_set = frontendDB->be_ssf_set;
513 be->be_pcl_mutexp = &be->be_pcl_mutex;
514 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
516 LDAP_STAILQ_INIT( &be->be_syncinfo );
518 /* assign a default depth limit for alias deref */
519 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
522 rc = bi->bi_db_init( be );
526 fprintf( stderr, "database init failed (%s)\n", type );
540 for ( i = 0; i < nbackends; i++ ) {
541 if ( backends[i].bd_info->bi_db_close ) {
542 (*backends[i].bd_info->bi_db_close)( &backends[i] );
546 if ( frontendDB->bd_info->bi_db_close ) {
547 (*frontendDB->bd_info->bi_db_close)( frontendDB );
558 ber_len_t len, dnlen = dn->bv_len;
561 for ( i = 0; i < nbackends; i++ ) {
562 for ( j = 0; backends[i].be_nsuffix != NULL &&
563 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
565 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
571 len = backends[i].be_nsuffix[j].bv_len;
574 /* suffix is longer than DN */
579 * input DN is normalized, so the separator check
580 * need not look at escaping
582 if ( len && len < dnlen &&
583 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
588 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
589 &dn->bv_val[dnlen-len] ) == 0 )
594 if( manageDSAit && len == dnlen &&
595 !SLAP_GLUE_SUBORDINATE( be ) ) {
612 struct berval *bvsuffix )
617 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
620 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
629 be_isroot_dn( Backend *be, struct berval *ndn )
631 if ( !ndn->bv_len ) {
635 if ( !be->be_rootndn.bv_len ) {
639 return dn_match( &be->be_rootndn, ndn );
643 be_sync_update( Operation *op )
645 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
649 be_slurp_update( Operation *op )
651 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
652 be_isupdate_dn( op->o_bd, &op->o_ndn ));
656 be_shadow_update( Operation *op )
658 return ( SLAP_SHADOW( op->o_bd ) &&
659 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
663 be_isupdate_dn( Backend *be, struct berval *ndn )
665 if ( !ndn->bv_len ) return( 0 );
667 if ( !be->be_update_ndn.bv_len ) return( 0 );
669 return dn_match( &be->be_update_ndn, ndn );
673 be_root_dn( Backend *be )
675 return &be->be_rootdn;
679 be_isroot( Operation *op )
681 return be_isroot_dn( op->o_bd, &op->o_ndn );
685 be_isroot_pw( Operation *op )
689 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
693 if( op->o_bd->be_rootpw.bv_len == 0 ) {
697 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
698 ldap_pvt_thread_mutex_lock( &passwd_mutex );
700 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
704 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
706 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
708 lutil_passwd_sasl_conn = NULL;
710 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
722 if ( op->o_bd->be_release ) {
723 /* free and release entry from backend */
724 return op->o_bd->be_release( op, e, rw );
733 backend_unbind( Operation *op, SlapReply *rs )
737 for ( i = 0; i < nbackends; i++ ) {
738 #if defined( LDAP_SLAPI )
741 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
742 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
743 rc = slapi_int_call_plugins( &backends[i],
744 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
747 * A preoperation plugin failure will abort the
750 Debug(LDAP_DEBUG_TRACE,
751 "do_bind: Unbind preoperation plugin failed\n",
756 #endif /* defined( LDAP_SLAPI ) */
758 if ( backends[i].be_unbind ) {
759 op->o_bd = &backends[i];
760 (*backends[i].be_unbind)( op, rs );
763 #if defined( LDAP_SLAPI )
764 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
765 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
767 Debug(LDAP_DEBUG_TRACE,
768 "do_unbind: Unbind postoperation plugins failed\n",
771 #endif /* defined( LDAP_SLAPI ) */
778 backend_connection_init(
783 for ( i = 0; i < nbackends; i++ ) {
784 if ( backends[i].be_connection_init ) {
785 (*backends[i].be_connection_init)( &backends[i], conn);
793 backend_connection_destroy(
798 for ( i = 0; i < nbackends; i++ ) {
799 if ( backends[i].be_connection_destroy ) {
800 (*backends[i].be_connection_destroy)( &backends[i], conn);
808 backend_check_controls(
812 LDAPControl **ctrls = op->o_ctrls;
813 rs->sr_err = LDAP_SUCCESS;
816 for( ; *ctrls != NULL ; ctrls++ ) {
817 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
818 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
820 /* FIXME: standards compliance issue
822 * Per RFC 2251 (and LDAPBIS discussions), if the control
823 * is recognized and appropriate for the operation (which
824 * we've already verified), then the server should make
825 * use of the control when performing the operation
826 * (without regard to criticality). This code is incorrect
828 * 1) a service error (e.g., unwillingToPerform) should be
829 * returned where a particular backend cannot service the
831 * 2) this error should be returned irregardless of the
832 * criticality of the control.
834 rs->sr_text = "control unavailable in context";
835 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
845 backend_check_restrictions(
848 struct berval *opdata )
850 slap_mask_t restrictops;
851 slap_mask_t requires;
853 slap_mask_t exopflag = 0;
860 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
864 restrictops = op->o_bd->be_restrictops;
865 requires = op->o_bd->be_requires;
866 ssf = &op->o_bd->be_ssf_set;
869 restrictops = frontendDB->be_restrictops;
870 requires = frontendDB->be_requires;
871 ssf = &frontendDB->be_ssf_set;
874 switch( op->o_tag ) {
876 opflag = SLAP_RESTRICT_OP_ADD;
880 opflag = SLAP_RESTRICT_OP_BIND;
883 case LDAP_REQ_COMPARE:
884 opflag = SLAP_RESTRICT_OP_COMPARE;
886 case LDAP_REQ_DELETE:
888 opflag = SLAP_RESTRICT_OP_DELETE;
890 case LDAP_REQ_EXTENDED:
891 opflag = SLAP_RESTRICT_OP_EXTENDED;
894 /* treat unspecified as a modify */
895 opflag = SLAP_RESTRICT_OP_MODIFY;
900 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
903 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
907 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
908 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
912 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
913 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
917 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
918 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
923 /* treat everything else as a modify */
924 opflag = SLAP_RESTRICT_OP_MODIFY;
928 case LDAP_REQ_MODIFY:
930 opflag = SLAP_RESTRICT_OP_MODIFY;
932 case LDAP_REQ_RENAME:
934 opflag = SLAP_RESTRICT_OP_RENAME;
936 case LDAP_REQ_SEARCH:
937 opflag = SLAP_RESTRICT_OP_SEARCH;
939 case LDAP_REQ_UNBIND:
944 rs->sr_text = "restrict operations internal error";
945 rs->sr_err = LDAP_OTHER;
950 /* these checks don't apply to StartTLS */
952 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
953 if( op->o_transport_ssf < ssf->sss_transport ) {
954 rs->sr_text = op->o_transport_ssf
955 ? "stronger transport confidentiality required"
956 : "transport confidentiality required";
960 if( op->o_tls_ssf < ssf->sss_tls ) {
961 rs->sr_text = op->o_tls_ssf
962 ? "stronger TLS confidentiality required"
963 : "TLS confidentiality required";
968 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
969 /* simple bind specific check */
970 if( op->o_ssf < ssf->sss_simple_bind ) {
971 rs->sr_text = op->o_ssf
972 ? "stronger confidentiality required"
973 : "confidentiality required";
978 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
979 /* these checks don't apply to SASL bind */
981 if( op->o_sasl_ssf < ssf->sss_sasl ) {
982 rs->sr_text = op->o_sasl_ssf
983 ? "stronger SASL confidentiality required"
984 : "SASL confidentiality required";
988 if( op->o_ssf < ssf->sss_ssf ) {
989 rs->sr_text = op->o_ssf
990 ? "stronger confidentiality required"
991 : "confidentiality required";
997 if( op->o_transport_ssf < ssf->sss_update_transport ) {
998 rs->sr_text = op->o_transport_ssf
999 ? "stronger transport confidentiality required for update"
1000 : "transport confidentiality required for update";
1004 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1005 rs->sr_text = op->o_tls_ssf
1006 ? "stronger TLS confidentiality required for update"
1007 : "TLS confidentiality required for update";
1011 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1012 rs->sr_text = op->o_sasl_ssf
1013 ? "stronger SASL confidentiality required for update"
1014 : "SASL confidentiality required for update";
1018 if( op->o_ssf < ssf->sss_update_ssf ) {
1019 rs->sr_text = op->o_ssf
1020 ? "stronger confidentiality required for update"
1021 : "confidentiality required for update";
1025 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1026 op->o_ndn.bv_len == 0 )
1028 rs->sr_text = "modifications require authentication";
1029 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1033 #ifdef SLAP_X_LISTENER_MOD
1034 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1035 /* no "w" mode means readonly */
1036 rs->sr_text = "modifications not allowed on this listener";
1037 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1040 #endif /* SLAP_X_LISTENER_MOD */
1045 /* these checks don't apply to Bind, StartTLS, or Unbind */
1047 if( requires & SLAP_REQUIRE_STRONG ) {
1048 /* should check mechanism */
1049 if( ( op->o_transport_ssf < ssf->sss_transport
1050 && op->o_authtype == LDAP_AUTH_SIMPLE )
1051 || op->o_dn.bv_len == 0 )
1053 rs->sr_text = "strong(er) authentication required";
1054 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1059 if( requires & SLAP_REQUIRE_SASL ) {
1060 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1061 rs->sr_text = "SASL authentication required";
1062 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1067 if( requires & SLAP_REQUIRE_AUTHC ) {
1068 if( op->o_dn.bv_len == 0 ) {
1069 rs->sr_text = "authentication required";
1070 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1075 if( requires & SLAP_REQUIRE_BIND ) {
1077 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1078 version = op->o_conn->c_protocol;
1079 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1082 /* no bind has occurred */
1083 rs->sr_text = "BIND required";
1084 rs->sr_err = LDAP_OPERATIONS_ERROR;
1089 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1090 if( op->o_protocol < LDAP_VERSION3 ) {
1091 /* no bind has occurred */
1092 rs->sr_text = "operation restricted to LDAPv3 clients";
1093 rs->sr_err = LDAP_OPERATIONS_ERROR;
1098 #ifdef SLAP_X_LISTENER_MOD
1099 if ( !starttls && op->o_dn.bv_len == 0 ) {
1100 if ( op->o_conn->c_listener &&
1101 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1103 /* no "x" mode means bind required */
1104 rs->sr_text = "bind required on this listener";
1105 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1110 if ( !starttls && !updateop ) {
1111 if ( op->o_conn->c_listener &&
1112 !( op->o_conn->c_listener->sl_perms &
1113 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1115 /* no "r" mode means no read */
1116 rs->sr_text = "read not allowed on this listener";
1117 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1121 #endif /* SLAP_X_LISTENER_MOD */
1125 if( ( restrictops & opflag )
1126 || ( exopflag && ( restrictops & exopflag ) ) ) {
1127 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1128 rs->sr_text = "read operations restricted";
1129 } else if ( restrictops & exopflag ) {
1130 rs->sr_text = "extended operation restricted";
1132 rs->sr_text = "operation restricted";
1134 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1138 rs->sr_err = LDAP_SUCCESS;
1142 int backend_check_referrals( Operation *op, SlapReply *rs )
1144 rs->sr_err = LDAP_SUCCESS;
1146 if( op->o_bd->be_chk_referrals ) {
1147 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1149 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1150 send_ldap_result( op, rs );
1162 AttributeDescription *at,
1170 if (op->o_bd == NULL) {
1171 rc = LDAP_NO_SUCH_OBJECT;
1172 } else if ( op->o_bd->be_fetch ) {
1173 rc = ( op->o_bd->be_fetch )( op, ndn,
1176 rc = LDAP_UNWILLING_TO_PERFORM;
1185 struct berval *gr_ndn,
1186 struct berval *op_ndn,
1187 ObjectClass *group_oc,
1188 AttributeDescription *group_at )
1194 Backend *be = op->o_bd;
1196 if ( op->o_abandon ) return SLAPD_ABANDON;
1198 op->o_bd = select_backend( gr_ndn, 0, 0 );
1200 for (g = op->o_groups; g; g=g->ga_next) {
1201 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1202 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1204 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1213 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1217 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1221 if ( op->o_pb != NULL ) {
1222 init_group_pblock( op, target, e, op_ndn, group_at );
1224 rc = call_group_preop_plugins( op );
1225 if ( rc == LDAP_SUCCESS ) {
1229 #endif /* LDAP_SLAPI */
1231 a = attr_find( e->e_attrs, group_at );
1233 /* If the attribute is a subtype of labeledURI, treat this as
1234 * a dynamic group ala groupOfURLs
1236 if (is_at_subtype( group_at->ad_type,
1237 slap_schema.si_ad_labeledURI->ad_type ) )
1241 struct berval bv, nbase;
1244 Backend *b2 = op->o_bd;
1246 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1249 op->o_bd = select_backend( op_ndn, 0, 0 );
1250 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1255 for (i=0; a->a_vals[i].bv_val; i++) {
1256 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1261 nbase.bv_val = NULL;
1262 /* host part must be empty */
1263 /* attrs and extensions parts must be empty */
1264 if (( ludp->lud_host && *ludp->lud_host ) ||
1265 ludp->lud_attrs || ludp->lud_exts )
1269 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1270 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1271 op->o_tmpmemctx ) != LDAP_SUCCESS )
1275 switch(ludp->lud_scope) {
1276 case LDAP_SCOPE_BASE:
1277 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1279 case LDAP_SCOPE_ONELEVEL:
1280 dnParent(op_ndn, &bv );
1281 if ( !dn_match( &nbase, &bv )) goto loopit;
1283 case LDAP_SCOPE_SUBTREE:
1284 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1286 #ifdef LDAP_SCOPE_SUBORDINATE
1287 case LDAP_SCOPE_SUBORDINATE:
1288 if ( dn_match( &nbase, op_ndn ) ||
1289 !dnIsSuffix( op_ndn, &nbase ) )
1295 filter = str2filter_x( op, ludp->lud_filter );
1297 if ( test_filter( NULL, user, filter ) ==
1302 filter_free_x( op, filter );
1305 ldap_free_urldesc( ludp );
1306 if ( nbase.bv_val ) {
1307 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1309 if ( rc == 0 ) break;
1311 if ( user != target ) {
1312 be_entry_release_r( op, user );
1317 rc = value_find_ex( group_at,
1318 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1319 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1320 a->a_nvals, op_ndn, op->o_tmpmemctx );
1323 rc = LDAP_NO_SUCH_ATTRIBUTE;
1326 be_entry_release_r( op, e );
1329 rc = LDAP_NO_SUCH_OBJECT;
1333 if ( op->o_pb ) call_group_postop_plugins( op );
1334 #endif /* LDAP_SLAPI */
1336 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1337 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1339 g->ga_be = op->o_bd;
1340 g->ga_oc = group_oc;
1341 g->ga_at = group_at;
1343 g->ga_len = gr_ndn->bv_len;
1344 strcpy(g->ga_ndn, gr_ndn->bv_val);
1345 g->ga_next = op->o_groups;
1354 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1358 BerVarray *vals = (BerVarray *)c->cac_private;
1359 Operation *op = NULL;
1362 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1367 if ( op->o_conn && access_allowed( op,
1368 e, a->a_desc, NULL, ACL_AUTH,
1369 &c->cac_acl_state ) == 0 ) {
1373 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1375 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1377 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1378 if ( op->o_conn && access_allowed( op,
1381 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1385 &a->a_nvals[i], op->o_tmpmemctx );
1386 if (v[j].bv_val ) j++;
1390 op->o_tmpfree( v, op->o_tmpmemctx );
1402 #endif /* LDAP_SLAPI */
1409 AttributeDescription *entry_at,
1411 slap_access_t access )
1415 int i, j, rc = LDAP_SUCCESS;
1416 AccessControlState acl_state = ACL_STATE_INIT;
1417 Backend *be = op->o_bd;
1419 op->o_bd = select_backend( edn, 0, 0 );
1421 if ( target && dn_match( &target->e_nname, edn ) ) {
1424 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1428 a = attr_find( e->e_attrs, entry_at );
1432 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1433 e, entry_at, NULL, access,
1434 &acl_state ) == 0 ) {
1435 rc = LDAP_INSUFFICIENT_ACCESS;
1439 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1441 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1443 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1444 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1447 access, &acl_state ) == 0 ) {
1451 &a->a_nvals[i], op->o_tmpmemctx );
1452 if (v[j].bv_val ) j++;
1455 op->o_tmpfree( v, op->o_tmpmemctx );
1457 rc = LDAP_INSUFFICIENT_ACCESS;
1466 else if ( op->o_pb ) {
1467 /* try any computed attributes */
1468 computed_attr_context ctx;
1469 AttributeName aname;
1471 slapi_int_pblock_set_operation( op->o_pb, op );
1473 ctx.cac_pb = op->o_pb;
1474 ctx.cac_attrs = NULL;
1475 ctx.cac_userattrs = 0;
1476 ctx.cac_opattrs = 0;
1477 ctx.cac_acl_state = acl_state;
1478 ctx.cac_private = (void *)vals;
1480 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1481 rc = LDAP_INSUFFICIENT_ACCESS;
1485 #endif /* LDAP_SLAPI */
1486 freeit: if (e != target ) {
1487 be_entry_release_r( op, e );
1495 int backend_operational(
1503 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1504 /* just count them */ ;
1507 * If operational attributes (allegedly) are required,
1508 * and the backend supports specific operational attributes,
1509 * add them to the attribute list
1511 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1512 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1514 *ap = slap_operational_entryDN( rs->sr_entry );
1515 ap = &(*ap)->a_next;
1518 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1519 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1521 *ap = slap_operational_subschemaSubentry( op->o_bd );
1522 ap = &(*ap)->a_next;
1525 /* Let the overlays have a chance at this */
1527 if ( SLAP_ISOVERLAY( be_orig ))
1528 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1530 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1531 op->o_bd && op->o_bd->be_operational != NULL )
1535 a = rs->sr_operational_attrs;
1536 rs->sr_operational_attrs = NULL;
1537 rc = op->o_bd->be_operational( op, rs );
1538 *ap = rs->sr_operational_attrs;
1540 rs->sr_operational_attrs = a;
1543 for ( ; *ap; ap = &(*ap)->a_next )
1544 /* just count them */ ;
1552 static void init_group_pblock( Operation *op, Entry *target,
1553 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1555 slapi_int_pblock_set_operation( op->o_pb, op );
1557 slapi_pblock_set( op->o_pb,
1558 SLAPI_X_GROUP_ENTRY, (void *)e );
1559 slapi_pblock_set( op->o_pb,
1560 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1561 slapi_pblock_set( op->o_pb,
1562 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1563 slapi_pblock_set( op->o_pb,
1564 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1567 static int call_group_preop_plugins( Operation *op )
1571 rc = slapi_int_call_plugins( op->o_bd,
1572 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1574 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1575 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1577 rc = LDAP_NO_SUCH_ATTRIBUTE;
1586 static void call_group_postop_plugins( Operation *op )
1588 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1590 #endif /* LDAP_SLAPI */