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",
189 /* back-relay takes care of itself; so may do other */
190 if ( be->be_controls == NULL ) {
191 BackendInfo *bi = be->bd_info;
193 if ( overlay_is_over( be ) ) {
194 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
197 if ( bi->bi_controls ) {
198 be->be_controls = ldap_charray_dup( bi->bi_controls );
205 int backend_startup(Backend *be)
210 if( ! ( nBackendDB > 0 ) ) {
212 Debug( LDAP_DEBUG_ANY,
213 "backend_startup: %d databases to startup.\n",
219 if ( be->bd_info->bi_open ) {
220 rc = be->bd_info->bi_open( be->bd_info );
222 Debug( LDAP_DEBUG_ANY,
223 "backend_startup: bi_open failed!\n",
230 return backend_startup_one( be );
233 /* open frontend, if required */
234 if ( frontendDB->bd_info->bi_db_open ) {
235 rc = frontendDB->bd_info->bi_db_open( frontendDB );
237 Debug( LDAP_DEBUG_ANY,
238 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
244 /* open each backend type */
245 for( i = 0; i < nBackendInfo; i++ ) {
246 if( backendInfo[i].bi_nDB == 0) {
247 /* no database of this type, don't open */
251 if( backendInfo[i].bi_open ) {
252 rc = backendInfo[i].bi_open(
255 Debug( LDAP_DEBUG_ANY,
256 "backend_startup: bi_open %d failed!\n",
263 ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
264 LDAP_STAILQ_INIT( &slapd_rq.task_list );
265 LDAP_STAILQ_INIT( &slapd_rq.run_list );
267 /* open each backend database */
268 for( i = 0; i < nBackendDB; i++ ) {
269 if ( backendDB[i].be_suffix == NULL ) {
270 Debug( LDAP_DEBUG_ANY,
271 "backend_startup: warning, database %d (%s) "
273 i, backendDB[i].bd_info->bi_type, 0 );
275 /* append global access controls */
276 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
278 rc = backend_startup_one( &backendDB[i] );
283 if ( backendDB[i].be_syncinfo ) {
286 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
287 backendDB[i].be_modify && backendDB[i].be_delete )) {
288 Debug( LDAP_DEBUG_ANY,
289 "backend_startup: database(%d) does not support "
290 "operations required for syncrepl", i, 0, 0 );
295 si = backendDB[i].be_syncinfo;
296 si->si_be = &backendDB[i];
298 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
299 ldap_pvt_runqueue_insert( &slapd_rq,
300 si->si_interval, do_syncrepl, (void *) si );
301 ldap_pvt_thread_mutex_unlock( &slapd_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 struct slap_csn_entry *csne;
391 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
393 /* destroy each backend database */
394 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
396 if ( bd->be_syncinfo ) {
397 syncinfo_free( bd->be_syncinfo );
400 if ( bd->be_pending_csn_list ) {
401 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
403 struct slap_csn_entry *tmp_csne = csne;
405 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
406 ch_free( csne->ce_csn.bv_val );
407 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
412 if ( bd->bd_info->bi_db_destroy ) {
413 bd->bd_info->bi_db_destroy( bd );
415 ber_bvarray_free( bd->be_suffix );
416 ber_bvarray_free( bd->be_nsuffix );
417 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
418 free( bd->be_rootdn.bv_val );
420 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
421 free( bd->be_rootndn.bv_val );
423 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
424 free( bd->be_rootpw.bv_val );
426 acl_destroy( bd->be_acl, frontendDB->be_acl );
427 if ( bd->be_controls ) {
428 ldap_charray_free( bd->be_controls );
433 /* destroy each backend type */
434 for( i = 0; i < nBackendInfo; i++ ) {
435 if( backendInfo[i].bi_destroy ) {
436 backendInfo[i].bi_destroy(
442 if (backendInfo != slap_binfo) {
445 #endif /* SLAPD_MODULES */
450 /* destroy frontend database */
453 if ( bd->bd_info->bi_db_destroy ) {
454 bd->bd_info->bi_db_destroy( bd );
456 ber_bvarray_free( bd->be_suffix );
457 ber_bvarray_free( bd->be_nsuffix );
458 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
459 free( bd->be_rootdn.bv_val );
461 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
462 free( bd->be_rootndn.bv_val );
464 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
465 free( bd->be_rootpw.bv_val );
467 acl_destroy( bd->be_acl, frontendDB->be_acl );
473 BackendInfo* backend_info(const char *type)
477 /* search for the backend type */
478 for( i = 0; i < nBackendInfo; i++ ) {
479 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
480 return &backendInfo[i];
493 BackendInfo *bi = backend_info(type);
497 fprintf( stderr, "Unrecognized database type (%s)\n", type );
503 backendDB = (BackendDB *) ch_realloc(
505 (nBackendDB + 1) * sizeof(Backend) );
507 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
509 /* did realloc move our table? if so, fix up dependent pointers */
510 if ( be != backendDB ) {
512 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
513 be->be_pcl_mutexp = &be->be_pcl_mutex;
517 be = &backends[nbackends++];
521 be->be_def_limit = frontendDB->be_def_limit;
522 be->be_dfltaccess = frontendDB->be_dfltaccess;
524 be->be_restrictops = frontendDB->be_restrictops;
525 be->be_requires = frontendDB->be_requires;
526 be->be_ssf_set = frontendDB->be_ssf_set;
528 be->be_pcl_mutexp = &be->be_pcl_mutex;
529 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
531 /* assign a default depth limit for alias deref */
532 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
535 rc = bi->bi_db_init( be );
539 fprintf( stderr, "database init failed (%s)\n", type );
553 for ( i = 0; i < nbackends; i++ ) {
554 if ( backends[i].bd_info->bi_db_close ) {
555 (*backends[i].bd_info->bi_db_close)( &backends[i] );
559 if ( frontendDB->bd_info->bi_db_close ) {
560 (*frontendDB->bd_info->bi_db_close)( frontendDB );
571 ber_len_t len, dnlen = dn->bv_len;
574 for ( i = 0; i < nbackends; i++ ) {
575 if ( backends[i].be_nsuffix == NULL ) {
579 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
581 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
587 len = backends[i].be_nsuffix[j].bv_len;
590 /* suffix is longer than DN */
595 * input DN is normalized, so the separator check
596 * need not look at escaping
598 if ( len && len < dnlen &&
599 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
604 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
605 &dn->bv_val[dnlen-len] ) == 0 )
610 if( manageDSAit && len == dnlen &&
611 !SLAP_GLUE_SUBORDINATE( be ) ) {
628 struct berval *bvsuffix )
632 if ( be->be_nsuffix == NULL ) {
636 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
637 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
646 be_isroot_dn( Backend *be, struct berval *ndn )
648 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
652 return dn_match( &be->be_rootndn, ndn );
656 be_slurp_update( Operation *op )
658 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
659 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
663 be_shadow_update( Operation *op )
665 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
666 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
670 be_isupdate_dn( Backend *be, struct berval *ndn )
672 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
676 return dn_match( &be->be_update_ndn, ndn );
680 be_root_dn( Backend *be )
682 return &be->be_rootdn;
686 be_isroot( Operation *op )
688 return be_isroot_dn( op->o_bd, &op->o_ndn );
692 be_isroot_pw( Operation *op )
696 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
700 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
704 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
705 ldap_pvt_thread_mutex_lock( &passwd_mutex );
707 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
711 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
713 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
715 lutil_passwd_sasl_conn = NULL;
717 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
729 if ( op->o_bd->be_release ) {
730 /* free and release entry from backend */
731 return op->o_bd->be_release( op, e, rw );
740 backend_unbind( Operation *op, SlapReply *rs )
744 for ( i = 0; i < nbackends; i++ ) {
745 #if defined( LDAP_SLAPI )
748 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
749 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
750 rc = slapi_int_call_plugins( &backends[i],
751 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
754 * A preoperation plugin failure will abort the
757 Debug(LDAP_DEBUG_TRACE,
758 "do_bind: Unbind preoperation plugin failed\n",
763 #endif /* defined( LDAP_SLAPI ) */
765 if ( backends[i].be_unbind ) {
766 op->o_bd = &backends[i];
767 (*backends[i].be_unbind)( op, rs );
770 #if defined( LDAP_SLAPI )
771 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
772 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
774 Debug(LDAP_DEBUG_TRACE,
775 "do_unbind: Unbind postoperation plugins failed\n",
778 #endif /* defined( LDAP_SLAPI ) */
785 backend_connection_init(
790 for ( i = 0; i < nbackends; i++ ) {
791 if ( backends[i].be_connection_init ) {
792 (*backends[i].be_connection_init)( &backends[i], conn);
800 backend_connection_destroy(
805 for ( i = 0; i < nbackends; i++ ) {
806 if ( backends[i].be_connection_destroy ) {
807 (*backends[i].be_connection_destroy)( &backends[i], conn);
815 backend_check_controls(
819 LDAPControl **ctrls = op->o_ctrls;
820 rs->sr_err = LDAP_SUCCESS;
823 for( ; *ctrls != NULL ; ctrls++ ) {
824 /* KLUDGE: ldctl_iscritical munged by controls.c:get_ctrls()
825 * to ensure this check is enabled/disabled appropriately.
827 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
828 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
830 /* Per RFC 2251 (and LDAPBIS discussions), if the control
831 * is recognized and appropriate for the operation (which
832 * we've already verified), then the server should make
833 * use of the control when performing the operation.
835 * Here we find that operation extended by the control
836 * is not unavailable in a particular context, hence the
837 * return of unwillingToPerform.
839 rs->sr_text = "control unavailable in context";
840 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
850 backend_check_restrictions(
853 struct berval *opdata )
855 slap_mask_t restrictops;
856 slap_mask_t requires;
858 slap_mask_t exopflag = 0;
865 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
869 restrictops = op->o_bd->be_restrictops;
870 requires = op->o_bd->be_requires;
871 ssf = &op->o_bd->be_ssf_set;
874 restrictops = frontendDB->be_restrictops;
875 requires = frontendDB->be_requires;
876 ssf = &frontendDB->be_ssf_set;
879 switch( op->o_tag ) {
881 opflag = SLAP_RESTRICT_OP_ADD;
885 opflag = SLAP_RESTRICT_OP_BIND;
888 case LDAP_REQ_COMPARE:
889 opflag = SLAP_RESTRICT_OP_COMPARE;
891 case LDAP_REQ_DELETE:
893 opflag = SLAP_RESTRICT_OP_DELETE;
895 case LDAP_REQ_EXTENDED:
896 opflag = SLAP_RESTRICT_OP_EXTENDED;
899 /* treat unspecified as a modify */
900 opflag = SLAP_RESTRICT_OP_MODIFY;
905 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
908 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
912 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
913 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
917 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
918 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
922 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
923 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
928 /* treat everything else as a modify */
929 opflag = SLAP_RESTRICT_OP_MODIFY;
933 case LDAP_REQ_MODIFY:
935 opflag = SLAP_RESTRICT_OP_MODIFY;
937 case LDAP_REQ_RENAME:
939 opflag = SLAP_RESTRICT_OP_RENAME;
941 case LDAP_REQ_SEARCH:
942 opflag = SLAP_RESTRICT_OP_SEARCH;
944 case LDAP_REQ_UNBIND:
949 rs->sr_text = "restrict operations internal error";
950 rs->sr_err = LDAP_OTHER;
955 /* these checks don't apply to StartTLS */
957 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
958 if( op->o_transport_ssf < ssf->sss_transport ) {
959 rs->sr_text = op->o_transport_ssf
960 ? "stronger transport confidentiality required"
961 : "transport confidentiality required";
965 if( op->o_tls_ssf < ssf->sss_tls ) {
966 rs->sr_text = op->o_tls_ssf
967 ? "stronger TLS confidentiality required"
968 : "TLS confidentiality required";
973 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
974 /* simple bind specific check */
975 if( op->o_ssf < ssf->sss_simple_bind ) {
976 rs->sr_text = op->o_ssf
977 ? "stronger confidentiality required"
978 : "confidentiality required";
983 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
984 /* these checks don't apply to SASL bind */
986 if( op->o_sasl_ssf < ssf->sss_sasl ) {
987 rs->sr_text = op->o_sasl_ssf
988 ? "stronger SASL confidentiality required"
989 : "SASL confidentiality required";
993 if( op->o_ssf < ssf->sss_ssf ) {
994 rs->sr_text = op->o_ssf
995 ? "stronger confidentiality required"
996 : "confidentiality required";
1002 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1003 rs->sr_text = op->o_transport_ssf
1004 ? "stronger transport confidentiality required for update"
1005 : "transport confidentiality required for update";
1009 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1010 rs->sr_text = op->o_tls_ssf
1011 ? "stronger TLS confidentiality required for update"
1012 : "TLS confidentiality required for update";
1016 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1017 rs->sr_text = op->o_sasl_ssf
1018 ? "stronger SASL confidentiality required for update"
1019 : "SASL confidentiality required for update";
1023 if( op->o_ssf < ssf->sss_update_ssf ) {
1024 rs->sr_text = op->o_ssf
1025 ? "stronger confidentiality required for update"
1026 : "confidentiality required for update";
1030 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1031 BER_BVISEMPTY( &op->o_ndn ) )
1033 rs->sr_text = "modifications require authentication";
1034 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1038 #ifdef SLAP_X_LISTENER_MOD
1039 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1040 /* no "w" mode means readonly */
1041 rs->sr_text = "modifications not allowed on this listener";
1042 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1045 #endif /* SLAP_X_LISTENER_MOD */
1050 /* these checks don't apply to Bind, StartTLS, or Unbind */
1052 if( requires & SLAP_REQUIRE_STRONG ) {
1053 /* should check mechanism */
1054 if( ( op->o_transport_ssf < ssf->sss_transport
1055 && op->o_authtype == LDAP_AUTH_SIMPLE )
1056 || BER_BVISEMPTY( &op->o_dn ) )
1058 rs->sr_text = "strong(er) authentication required";
1059 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1064 if( requires & SLAP_REQUIRE_SASL ) {
1065 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1066 rs->sr_text = "SASL authentication required";
1067 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1072 if( requires & SLAP_REQUIRE_AUTHC ) {
1073 if( BER_BVISEMPTY( &op->o_dn ) ) {
1074 rs->sr_text = "authentication required";
1075 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1080 if( requires & SLAP_REQUIRE_BIND ) {
1082 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1083 version = op->o_conn->c_protocol;
1084 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1087 /* no bind has occurred */
1088 rs->sr_text = "BIND required";
1089 rs->sr_err = LDAP_OPERATIONS_ERROR;
1094 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1095 if( op->o_protocol < LDAP_VERSION3 ) {
1096 /* no bind has occurred */
1097 rs->sr_text = "operation restricted to LDAPv3 clients";
1098 rs->sr_err = LDAP_OPERATIONS_ERROR;
1103 #ifdef SLAP_X_LISTENER_MOD
1104 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1105 if ( op->o_conn->c_listener &&
1106 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1108 /* no "x" mode means bind required */
1109 rs->sr_text = "bind required on this listener";
1110 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1115 if ( !starttls && !updateop ) {
1116 if ( op->o_conn->c_listener &&
1117 !( op->o_conn->c_listener->sl_perms &
1118 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1120 /* no "r" mode means no read */
1121 rs->sr_text = "read not allowed on this listener";
1122 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126 #endif /* SLAP_X_LISTENER_MOD */
1130 if( ( restrictops & opflag )
1131 || ( exopflag && ( restrictops & exopflag ) ) ) {
1132 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1133 rs->sr_text = "read operations restricted";
1134 } else if ( restrictops & exopflag ) {
1135 rs->sr_text = "extended operation restricted";
1137 rs->sr_text = "operation restricted";
1139 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1143 rs->sr_err = LDAP_SUCCESS;
1147 int backend_check_referrals( Operation *op, SlapReply *rs )
1149 rs->sr_err = LDAP_SUCCESS;
1151 if( op->o_bd->be_chk_referrals ) {
1152 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1154 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1155 send_ldap_result( op, rs );
1167 AttributeDescription *at,
1175 if (op->o_bd == NULL) {
1176 rc = LDAP_NO_SUCH_OBJECT;
1177 } else if ( op->o_bd->be_fetch ) {
1178 rc = ( op->o_bd->be_fetch )( op, ndn,
1181 rc = LDAP_UNWILLING_TO_PERFORM;
1190 struct berval *gr_ndn,
1191 struct berval *op_ndn,
1192 ObjectClass *group_oc,
1193 AttributeDescription *group_at )
1199 Backend *be = op->o_bd;
1201 if ( op->o_abandon ) return SLAPD_ABANDON;
1203 op->o_bd = select_backend( gr_ndn, 0, 0 );
1205 for ( g = op->o_groups; g; g = g->ga_next ) {
1206 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1207 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1211 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1221 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1225 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1229 if ( op->o_pb != NULL ) {
1230 init_group_pblock( op, target, e, op_ndn, group_at );
1232 rc = call_group_preop_plugins( op );
1233 if ( rc == LDAP_SUCCESS ) {
1237 #endif /* LDAP_SLAPI */
1239 a = attr_find( e->e_attrs, group_at );
1241 /* If the attribute is a subtype of labeledURI, treat this as
1242 * a dynamic group ala groupOfURLs
1244 if (is_at_subtype( group_at->ad_type,
1245 slap_schema.si_ad_labeledURI->ad_type ) )
1249 struct berval bv, nbase;
1252 Backend *b2 = op->o_bd;
1254 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1257 op->o_bd = select_backend( op_ndn, 0, 0 );
1258 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1263 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1264 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1269 BER_BVZERO( &nbase );
1270 /* host part must be empty */
1271 /* attrs and extensions parts must be empty */
1272 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1273 ludp->lud_attrs || ludp->lud_exts )
1277 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1278 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1279 op->o_tmpmemctx ) != LDAP_SUCCESS )
1283 switch ( ludp->lud_scope ) {
1284 case LDAP_SCOPE_BASE:
1285 if ( !dn_match( &nbase, op_ndn ) ) {
1289 case LDAP_SCOPE_ONELEVEL:
1290 dnParent( op_ndn, &bv );
1291 if ( !dn_match( &nbase, &bv ) ) {
1295 case LDAP_SCOPE_SUBTREE:
1296 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1300 #ifdef LDAP_SCOPE_SUBORDINATE
1301 case LDAP_SCOPE_SUBORDINATE:
1302 if ( dn_match( &nbase, op_ndn ) ||
1303 !dnIsSuffix( op_ndn, &nbase ) )
1309 filter = str2filter_x( op, ludp->lud_filter );
1311 if ( test_filter( NULL, user, filter ) ==
1316 filter_free_x( op, filter );
1319 ldap_free_urldesc( ludp );
1320 if ( !BER_BVISNULL( &nbase ) ) {
1321 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1323 if ( rc == 0 ) break;
1325 if ( user != target ) {
1326 be_entry_release_r( op, user );
1331 rc = value_find_ex( group_at,
1332 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1333 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1334 a->a_nvals, op_ndn, op->o_tmpmemctx );
1337 rc = LDAP_NO_SUCH_ATTRIBUTE;
1340 be_entry_release_r( op, e );
1343 rc = LDAP_NO_SUCH_OBJECT;
1347 if ( op->o_pb ) call_group_postop_plugins( op );
1348 #endif /* LDAP_SLAPI */
1350 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1351 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1353 g->ga_be = op->o_bd;
1354 g->ga_oc = group_oc;
1355 g->ga_at = group_at;
1357 g->ga_len = gr_ndn->bv_len;
1358 strcpy( g->ga_ndn, gr_ndn->bv_val );
1359 g->ga_next = op->o_groups;
1368 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1372 BerVarray *vals = (BerVarray *)c->cac_private;
1373 Operation *op = NULL;
1376 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1381 if ( op->o_conn && access_allowed( op,
1382 e, a->a_desc, NULL, ACL_AUTH,
1383 &c->cac_acl_state ) == 0 ) {
1387 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1389 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1391 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1392 if ( op->o_conn && access_allowed( op,
1395 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1399 &a->a_nvals[i], op->o_tmpmemctx );
1400 if ( !BER_BVISNULL( &v[j] ) ) {
1406 op->o_tmpfree( v, op->o_tmpmemctx );
1410 BER_BVZERO( &v[j] );
1417 #endif /* LDAP_SLAPI */
1424 AttributeDescription *entry_at,
1426 slap_access_t access )
1429 Attribute *a = NULL;
1430 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1431 AccessControlState acl_state = ACL_STATE_INIT;
1432 Backend *be = op->o_bd;
1434 op->o_bd = select_backend( edn, 0, 0 );
1436 if ( target && dn_match( &target->e_nname, edn ) ) {
1440 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1444 a = attr_find( e->e_attrs, entry_at );
1446 SlapReply rs = { 0 };
1447 AttributeName anlist[ 2 ];
1449 anlist[ 0 ].an_name = entry_at->ad_cname;
1450 anlist[ 0 ].an_desc = entry_at;
1451 BER_BVZERO( &anlist[ 1 ].an_name );
1452 rs.sr_attrs = anlist;
1454 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1456 rc = backend_operational( op, &rs );
1458 if ( rc == LDAP_SUCCESS && rs.sr_operational_attrs ) {
1460 a = rs.sr_operational_attrs;
1467 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1468 e, entry_at, NULL, access,
1469 &acl_state ) == 0 ) {
1470 rc = LDAP_INSUFFICIENT_ACCESS;
1474 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1477 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1479 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1481 if ( op->o_conn && access > ACL_NONE &&
1482 access_allowed( op, e,
1490 ber_dupbv_x( &v[j], &a->a_nvals[i],
1492 if ( !BER_BVISNULL( &v[j] ) ) {
1497 op->o_tmpfree( v, op->o_tmpmemctx );
1499 rc = LDAP_INSUFFICIENT_ACCESS;
1502 BER_BVZERO( &v[j] );
1508 else if ( op->o_pb ) {
1509 /* try any computed attributes */
1510 computed_attr_context ctx;
1512 slapi_int_pblock_set_operation( op->o_pb, op );
1514 ctx.cac_pb = op->o_pb;
1515 ctx.cac_attrs = NULL;
1516 ctx.cac_userattrs = 0;
1517 ctx.cac_opattrs = 0;
1518 ctx.cac_acl_state = acl_state;
1519 ctx.cac_private = (void *)vals;
1521 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1523 rc = LDAP_INSUFFICIENT_ACCESS;
1529 #endif /* LDAP_SLAPI */
1530 freeit: if ( e != target ) {
1531 be_entry_release_r( op, e );
1542 int backend_operational(
1550 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1551 /* just count them */ ;
1554 * If operational attributes (allegedly) are required,
1555 * and the backend supports specific operational attributes,
1556 * add them to the attribute list
1558 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1559 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1561 *ap = slap_operational_entryDN( rs->sr_entry );
1562 ap = &(*ap)->a_next;
1565 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1566 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1568 *ap = slap_operational_subschemaSubentry( op->o_bd );
1569 ap = &(*ap)->a_next;
1572 /* Let the overlays have a chance at this */
1574 if ( SLAP_ISOVERLAY( be_orig ))
1575 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1577 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1578 op->o_bd && op->o_bd->be_operational != NULL )
1582 a = rs->sr_operational_attrs;
1583 rs->sr_operational_attrs = NULL;
1584 rc = op->o_bd->be_operational( op, rs );
1585 *ap = rs->sr_operational_attrs;
1587 rs->sr_operational_attrs = a;
1590 for ( ; *ap; ap = &(*ap)->a_next )
1591 /* just count them */ ;
1599 static void init_group_pblock( Operation *op, Entry *target,
1600 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1602 slapi_int_pblock_set_operation( op->o_pb, op );
1604 slapi_pblock_set( op->o_pb,
1605 SLAPI_X_GROUP_ENTRY, (void *)e );
1606 slapi_pblock_set( op->o_pb,
1607 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1608 slapi_pblock_set( op->o_pb,
1609 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1610 slapi_pblock_set( op->o_pb,
1611 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1614 static int call_group_preop_plugins( Operation *op )
1618 rc = slapi_int_call_plugins( op->o_bd,
1619 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1621 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1622 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1624 rc = LDAP_NO_SUCH_ATTRIBUTE;
1633 static void call_group_postop_plugins( Operation *op )
1635 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1637 #endif /* LDAP_SLAPI */