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 ( !BER_BVISNULL( &bd->be_rootdn ) ) {
404 free( bd->be_rootdn.bv_val );
406 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
407 free( bd->be_rootndn.bv_val );
409 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
410 free( bd->be_rootpw.bv_val );
412 acl_destroy( bd->be_acl, frontendDB->be_acl );
416 /* destroy each backend type */
417 for( i = 0; i < nBackendInfo; i++ ) {
418 if( backendInfo[i].bi_destroy ) {
419 backendInfo[i].bi_destroy(
425 if (backendInfo != slap_binfo) {
428 #endif /* SLAPD_MODULES */
433 /* destroy frontend database */
436 if ( bd->bd_info->bi_db_destroy ) {
437 bd->bd_info->bi_db_destroy( bd );
439 ber_bvarray_free( bd->be_suffix );
440 ber_bvarray_free( bd->be_nsuffix );
441 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
442 free( bd->be_rootdn.bv_val );
444 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
445 free( bd->be_rootndn.bv_val );
447 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
448 free( bd->be_rootpw.bv_val );
450 acl_destroy( bd->be_acl, frontendDB->be_acl );
456 BackendInfo* backend_info(const char *type)
460 /* search for the backend type */
461 for( i = 0; i < nBackendInfo; i++ ) {
462 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
463 return &backendInfo[i];
476 BackendInfo *bi = backend_info(type);
480 fprintf( stderr, "Unrecognized database type (%s)\n", type );
486 backendDB = (BackendDB *) ch_realloc(
488 (nBackendDB + 1) * sizeof(Backend) );
490 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
492 /* did realloc move our table? if so, fix up dependent pointers */
493 if ( be != backendDB ) {
495 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
496 be->be_pcl_mutexp = &be->be_pcl_mutex;
500 be = &backends[nbackends++];
503 be->be_def_limit = frontendDB->be_def_limit;
504 be->be_dfltaccess = frontendDB->be_dfltaccess;
506 be->be_restrictops = frontendDB->be_restrictops;
507 be->be_requires = frontendDB->be_requires;
508 be->be_ssf_set = frontendDB->be_ssf_set;
510 be->be_pcl_mutexp = &be->be_pcl_mutex;
511 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
513 /* assign a default depth limit for alias deref */
514 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
517 rc = bi->bi_db_init( be );
521 fprintf( stderr, "database init failed (%s)\n", type );
535 for ( i = 0; i < nbackends; i++ ) {
536 if ( backends[i].bd_info->bi_db_close ) {
537 (*backends[i].bd_info->bi_db_close)( &backends[i] );
541 if ( frontendDB->bd_info->bi_db_close ) {
542 (*frontendDB->bd_info->bi_db_close)( frontendDB );
553 ber_len_t len, dnlen = dn->bv_len;
556 for ( i = 0; i < nbackends; i++ ) {
557 if ( backends[i].be_nsuffix == NULL ) {
561 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
563 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
569 len = backends[i].be_nsuffix[j].bv_len;
572 /* suffix is longer than DN */
577 * input DN is normalized, so the separator check
578 * need not look at escaping
580 if ( len && len < dnlen &&
581 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
586 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
587 &dn->bv_val[dnlen-len] ) == 0 )
592 if( manageDSAit && len == dnlen &&
593 !SLAP_GLUE_SUBORDINATE( be ) ) {
610 struct berval *bvsuffix )
614 if ( be->be_nsuffix == NULL ) {
618 for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
619 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
628 be_isroot_dn( Backend *be, struct berval *ndn )
630 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
634 return dn_match( &be->be_rootndn, ndn );
638 be_slurp_update( Operation *op )
640 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
641 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
645 be_shadow_update( Operation *op )
647 return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
648 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
652 be_isupdate_dn( Backend *be, struct berval *ndn )
654 if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
658 return dn_match( &be->be_update_ndn, ndn );
662 be_root_dn( Backend *be )
664 return &be->be_rootdn;
668 be_isroot( Operation *op )
670 return be_isroot_dn( op->o_bd, &op->o_ndn );
674 be_isroot_pw( Operation *op )
678 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
682 if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
686 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
687 ldap_pvt_thread_mutex_lock( &passwd_mutex );
689 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
693 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
695 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
697 lutil_passwd_sasl_conn = NULL;
699 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
711 if ( op->o_bd->be_release ) {
712 /* free and release entry from backend */
713 return op->o_bd->be_release( op, e, rw );
722 backend_unbind( Operation *op, SlapReply *rs )
726 for ( i = 0; i < nbackends; i++ ) {
727 #if defined( LDAP_SLAPI )
730 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
731 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
732 rc = slapi_int_call_plugins( &backends[i],
733 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
736 * A preoperation plugin failure will abort the
739 Debug(LDAP_DEBUG_TRACE,
740 "do_bind: Unbind preoperation plugin failed\n",
745 #endif /* defined( LDAP_SLAPI ) */
747 if ( backends[i].be_unbind ) {
748 op->o_bd = &backends[i];
749 (*backends[i].be_unbind)( op, rs );
752 #if defined( LDAP_SLAPI )
753 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
754 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
756 Debug(LDAP_DEBUG_TRACE,
757 "do_unbind: Unbind postoperation plugins failed\n",
760 #endif /* defined( LDAP_SLAPI ) */
767 backend_connection_init(
772 for ( i = 0; i < nbackends; i++ ) {
773 if ( backends[i].be_connection_init ) {
774 (*backends[i].be_connection_init)( &backends[i], conn);
782 backend_connection_destroy(
787 for ( i = 0; i < nbackends; i++ ) {
788 if ( backends[i].be_connection_destroy ) {
789 (*backends[i].be_connection_destroy)( &backends[i], conn);
797 backend_check_controls(
801 LDAPControl **ctrls = op->o_ctrls;
802 rs->sr_err = LDAP_SUCCESS;
805 for( ; *ctrls != NULL ; ctrls++ ) {
806 if( !ldap_charray_inlist( op->o_bd->be_controls,
807 (*ctrls)->ldctl_oid ) )
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).
815 * Here we find that operation extended by the control
816 * is not unavailable in a particular context, hence the
817 * return of unwillingToPerform.
819 * Note that criticality of the control is irrelevant to
822 rs->sr_text = "control unavailable in context";
823 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
833 backend_check_restrictions(
836 struct berval *opdata )
838 slap_mask_t restrictops;
839 slap_mask_t requires;
841 slap_mask_t exopflag = 0;
848 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
852 restrictops = op->o_bd->be_restrictops;
853 requires = op->o_bd->be_requires;
854 ssf = &op->o_bd->be_ssf_set;
857 restrictops = frontendDB->be_restrictops;
858 requires = frontendDB->be_requires;
859 ssf = &frontendDB->be_ssf_set;
862 switch( op->o_tag ) {
864 opflag = SLAP_RESTRICT_OP_ADD;
868 opflag = SLAP_RESTRICT_OP_BIND;
871 case LDAP_REQ_COMPARE:
872 opflag = SLAP_RESTRICT_OP_COMPARE;
874 case LDAP_REQ_DELETE:
876 opflag = SLAP_RESTRICT_OP_DELETE;
878 case LDAP_REQ_EXTENDED:
879 opflag = SLAP_RESTRICT_OP_EXTENDED;
882 /* treat unspecified as a modify */
883 opflag = SLAP_RESTRICT_OP_MODIFY;
888 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
891 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
895 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
896 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
900 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
901 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
905 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
906 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
911 /* treat everything else as a modify */
912 opflag = SLAP_RESTRICT_OP_MODIFY;
916 case LDAP_REQ_MODIFY:
918 opflag = SLAP_RESTRICT_OP_MODIFY;
920 case LDAP_REQ_RENAME:
922 opflag = SLAP_RESTRICT_OP_RENAME;
924 case LDAP_REQ_SEARCH:
925 opflag = SLAP_RESTRICT_OP_SEARCH;
927 case LDAP_REQ_UNBIND:
932 rs->sr_text = "restrict operations internal error";
933 rs->sr_err = LDAP_OTHER;
938 /* these checks don't apply to StartTLS */
940 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
941 if( op->o_transport_ssf < ssf->sss_transport ) {
942 rs->sr_text = op->o_transport_ssf
943 ? "stronger transport confidentiality required"
944 : "transport confidentiality required";
948 if( op->o_tls_ssf < ssf->sss_tls ) {
949 rs->sr_text = op->o_tls_ssf
950 ? "stronger TLS confidentiality required"
951 : "TLS confidentiality required";
956 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
957 /* simple bind specific check */
958 if( op->o_ssf < ssf->sss_simple_bind ) {
959 rs->sr_text = op->o_ssf
960 ? "stronger confidentiality required"
961 : "confidentiality required";
966 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
967 /* these checks don't apply to SASL bind */
969 if( op->o_sasl_ssf < ssf->sss_sasl ) {
970 rs->sr_text = op->o_sasl_ssf
971 ? "stronger SASL confidentiality required"
972 : "SASL confidentiality required";
976 if( op->o_ssf < ssf->sss_ssf ) {
977 rs->sr_text = op->o_ssf
978 ? "stronger confidentiality required"
979 : "confidentiality required";
985 if( op->o_transport_ssf < ssf->sss_update_transport ) {
986 rs->sr_text = op->o_transport_ssf
987 ? "stronger transport confidentiality required for update"
988 : "transport confidentiality required for update";
992 if( op->o_tls_ssf < ssf->sss_update_tls ) {
993 rs->sr_text = op->o_tls_ssf
994 ? "stronger TLS confidentiality required for update"
995 : "TLS confidentiality required for update";
999 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1000 rs->sr_text = op->o_sasl_ssf
1001 ? "stronger SASL confidentiality required for update"
1002 : "SASL confidentiality required for update";
1006 if( op->o_ssf < ssf->sss_update_ssf ) {
1007 rs->sr_text = op->o_ssf
1008 ? "stronger confidentiality required for update"
1009 : "confidentiality required for update";
1013 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1014 BER_BVISEMPTY( &op->o_ndn ) )
1016 rs->sr_text = "modifications require authentication";
1017 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1021 #ifdef SLAP_X_LISTENER_MOD
1022 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn ) ? S_IWUSR : S_IWOTH ) ) ) {
1023 /* no "w" mode means readonly */
1024 rs->sr_text = "modifications not allowed on this listener";
1025 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1028 #endif /* SLAP_X_LISTENER_MOD */
1033 /* these checks don't apply to Bind, StartTLS, or Unbind */
1035 if( requires & SLAP_REQUIRE_STRONG ) {
1036 /* should check mechanism */
1037 if( ( op->o_transport_ssf < ssf->sss_transport
1038 && op->o_authtype == LDAP_AUTH_SIMPLE )
1039 || BER_BVISEMPTY( &op->o_dn ) )
1041 rs->sr_text = "strong(er) authentication required";
1042 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1047 if( requires & SLAP_REQUIRE_SASL ) {
1048 if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1049 rs->sr_text = "SASL authentication required";
1050 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1055 if( requires & SLAP_REQUIRE_AUTHC ) {
1056 if( BER_BVISEMPTY( &op->o_dn ) ) {
1057 rs->sr_text = "authentication required";
1058 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1063 if( requires & SLAP_REQUIRE_BIND ) {
1065 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1066 version = op->o_conn->c_protocol;
1067 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1070 /* no bind has occurred */
1071 rs->sr_text = "BIND required";
1072 rs->sr_err = LDAP_OPERATIONS_ERROR;
1077 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1078 if( op->o_protocol < LDAP_VERSION3 ) {
1079 /* no bind has occurred */
1080 rs->sr_text = "operation restricted to LDAPv3 clients";
1081 rs->sr_err = LDAP_OPERATIONS_ERROR;
1086 #ifdef SLAP_X_LISTENER_MOD
1087 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1088 if ( op->o_conn->c_listener &&
1089 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1091 /* no "x" mode means bind required */
1092 rs->sr_text = "bind required on this listener";
1093 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1098 if ( !starttls && !updateop ) {
1099 if ( op->o_conn->c_listener &&
1100 !( op->o_conn->c_listener->sl_perms &
1101 ( !BER_BVISEMPTY( &op->o_dn ) ? S_IRUSR : S_IROTH )))
1103 /* no "r" mode means no read */
1104 rs->sr_text = "read not allowed on this listener";
1105 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1109 #endif /* SLAP_X_LISTENER_MOD */
1113 if( ( restrictops & opflag )
1114 || ( exopflag && ( restrictops & exopflag ) ) ) {
1115 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1116 rs->sr_text = "read operations restricted";
1117 } else if ( restrictops & exopflag ) {
1118 rs->sr_text = "extended operation restricted";
1120 rs->sr_text = "operation restricted";
1122 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126 rs->sr_err = LDAP_SUCCESS;
1130 int backend_check_referrals( Operation *op, SlapReply *rs )
1132 rs->sr_err = LDAP_SUCCESS;
1134 if( op->o_bd->be_chk_referrals ) {
1135 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1137 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1138 send_ldap_result( op, rs );
1150 AttributeDescription *at,
1158 if (op->o_bd == NULL) {
1159 rc = LDAP_NO_SUCH_OBJECT;
1160 } else if ( op->o_bd->be_fetch ) {
1161 rc = ( op->o_bd->be_fetch )( op, ndn,
1164 rc = LDAP_UNWILLING_TO_PERFORM;
1173 struct berval *gr_ndn,
1174 struct berval *op_ndn,
1175 ObjectClass *group_oc,
1176 AttributeDescription *group_at )
1182 Backend *be = op->o_bd;
1184 if ( op->o_abandon ) return SLAPD_ABANDON;
1186 op->o_bd = select_backend( gr_ndn, 0, 0 );
1188 for ( g = op->o_groups; g; g = g->ga_next ) {
1189 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1190 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1194 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1204 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1208 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1212 if ( op->o_pb != NULL ) {
1213 init_group_pblock( op, target, e, op_ndn, group_at );
1215 rc = call_group_preop_plugins( op );
1216 if ( rc == LDAP_SUCCESS ) {
1220 #endif /* LDAP_SLAPI */
1222 a = attr_find( e->e_attrs, group_at );
1224 /* If the attribute is a subtype of labeledURI, treat this as
1225 * a dynamic group ala groupOfURLs
1227 if (is_at_subtype( group_at->ad_type,
1228 slap_schema.si_ad_labeledURI->ad_type ) )
1232 struct berval bv, nbase;
1235 Backend *b2 = op->o_bd;
1237 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1240 op->o_bd = select_backend( op_ndn, 0, 0 );
1241 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1246 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1247 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1252 BER_BVZERO( &nbase );
1253 /* host part must be empty */
1254 /* attrs and extensions parts must be empty */
1255 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1256 ludp->lud_attrs || ludp->lud_exts )
1260 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1261 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1262 op->o_tmpmemctx ) != LDAP_SUCCESS )
1266 switch ( ludp->lud_scope ) {
1267 case LDAP_SCOPE_BASE:
1268 if ( !dn_match( &nbase, op_ndn ) ) {
1272 case LDAP_SCOPE_ONELEVEL:
1273 dnParent( op_ndn, &bv );
1274 if ( !dn_match( &nbase, &bv ) ) {
1278 case LDAP_SCOPE_SUBTREE:
1279 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1283 #ifdef LDAP_SCOPE_SUBORDINATE
1284 case LDAP_SCOPE_SUBORDINATE:
1285 if ( dn_match( &nbase, op_ndn ) ||
1286 !dnIsSuffix( op_ndn, &nbase ) )
1292 filter = str2filter_x( op, ludp->lud_filter );
1294 if ( test_filter( NULL, user, filter ) ==
1299 filter_free_x( op, filter );
1302 ldap_free_urldesc( ludp );
1303 if ( !BER_BVISNULL( &nbase ) ) {
1304 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1306 if ( rc == 0 ) break;
1308 if ( user != target ) {
1309 be_entry_release_r( op, user );
1314 rc = value_find_ex( group_at,
1315 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1316 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1317 a->a_nvals, op_ndn, op->o_tmpmemctx );
1320 rc = LDAP_NO_SUCH_ATTRIBUTE;
1323 be_entry_release_r( op, e );
1326 rc = LDAP_NO_SUCH_OBJECT;
1330 if ( op->o_pb ) call_group_postop_plugins( op );
1331 #endif /* LDAP_SLAPI */
1333 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1334 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1336 g->ga_be = op->o_bd;
1337 g->ga_oc = group_oc;
1338 g->ga_at = group_at;
1340 g->ga_len = gr_ndn->bv_len;
1341 strcpy( g->ga_ndn, gr_ndn->bv_val );
1342 g->ga_next = op->o_groups;
1351 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1355 BerVarray *vals = (BerVarray *)c->cac_private;
1356 Operation *op = NULL;
1359 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1364 if ( op->o_conn && access_allowed( op,
1365 e, a->a_desc, NULL, ACL_AUTH,
1366 &c->cac_acl_state ) == 0 ) {
1370 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1372 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1374 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1375 if ( op->o_conn && access_allowed( op,
1378 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1382 &a->a_nvals[i], op->o_tmpmemctx );
1383 if ( !BER_BVISNULL( &v[j] ) ) {
1389 op->o_tmpfree( v, op->o_tmpmemctx );
1393 BER_BVZERO( &v[j] );
1400 #endif /* LDAP_SLAPI */
1407 AttributeDescription *entry_at,
1409 slap_access_t access )
1412 Attribute *a = NULL;
1413 int freeattr = 0, i, j, rc = LDAP_SUCCESS;
1414 AccessControlState acl_state = ACL_STATE_INIT;
1415 Backend *be = op->o_bd;
1417 op->o_bd = select_backend( edn, 0, 0 );
1419 if ( target && dn_match( &target->e_nname, edn ) ) {
1423 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1427 a = attr_find( e->e_attrs, entry_at );
1429 SlapReply rs = { 0 };
1430 AttributeName anlist[ 2 ];
1432 anlist[ 0 ].an_name = entry_at->ad_cname;
1433 anlist[ 0 ].an_desc = entry_at;
1434 BER_BVZERO( &anlist[ 1 ].an_name );
1435 rs.sr_attrs = anlist;
1437 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1439 rc = backend_operational( op, &rs );
1441 if ( rc == LDAP_SUCCESS && rs.sr_operational_attrs ) {
1443 a = rs.sr_operational_attrs;
1450 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1451 e, entry_at, NULL, access,
1452 &acl_state ) == 0 ) {
1453 rc = LDAP_INSUFFICIENT_ACCESS;
1457 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1460 v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1462 for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1464 if ( op->o_conn && access > ACL_NONE &&
1465 access_allowed( op, e,
1473 ber_dupbv_x( &v[j], &a->a_nvals[i],
1475 if ( !BER_BVISNULL( &v[j] ) ) {
1480 op->o_tmpfree( v, op->o_tmpmemctx );
1482 rc = LDAP_INSUFFICIENT_ACCESS;
1485 BER_BVZERO( &v[j] );
1491 else if ( op->o_pb ) {
1492 /* try any computed attributes */
1493 computed_attr_context ctx;
1495 slapi_int_pblock_set_operation( op->o_pb, op );
1497 ctx.cac_pb = op->o_pb;
1498 ctx.cac_attrs = NULL;
1499 ctx.cac_userattrs = 0;
1500 ctx.cac_opattrs = 0;
1501 ctx.cac_acl_state = acl_state;
1502 ctx.cac_private = (void *)vals;
1504 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1506 rc = LDAP_INSUFFICIENT_ACCESS;
1512 #endif /* LDAP_SLAPI */
1513 freeit: if ( e != target ) {
1514 be_entry_release_r( op, e );
1525 int backend_operational(
1533 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1534 /* just count them */ ;
1537 * If operational attributes (allegedly) are required,
1538 * and the backend supports specific operational attributes,
1539 * add them to the attribute list
1541 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1542 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1544 *ap = slap_operational_entryDN( rs->sr_entry );
1545 ap = &(*ap)->a_next;
1548 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1549 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1551 *ap = slap_operational_subschemaSubentry( op->o_bd );
1552 ap = &(*ap)->a_next;
1555 /* Let the overlays have a chance at this */
1557 if ( SLAP_ISOVERLAY( be_orig ))
1558 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1560 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1561 op->o_bd && op->o_bd->be_operational != NULL )
1565 a = rs->sr_operational_attrs;
1566 rs->sr_operational_attrs = NULL;
1567 rc = op->o_bd->be_operational( op, rs );
1568 *ap = rs->sr_operational_attrs;
1570 rs->sr_operational_attrs = a;
1573 for ( ; *ap; ap = &(*ap)->a_next )
1574 /* just count them */ ;
1582 static void init_group_pblock( Operation *op, Entry *target,
1583 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1585 slapi_int_pblock_set_operation( op->o_pb, op );
1587 slapi_pblock_set( op->o_pb,
1588 SLAPI_X_GROUP_ENTRY, (void *)e );
1589 slapi_pblock_set( op->o_pb,
1590 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1591 slapi_pblock_set( op->o_pb,
1592 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1593 slapi_pblock_set( op->o_pb,
1594 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1597 static int call_group_preop_plugins( Operation *op )
1601 rc = slapi_int_call_plugins( op->o_bd,
1602 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1604 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1605 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1607 rc = LDAP_NO_SUCH_ATTRIBUTE;
1616 static void call_group_postop_plugins( Operation *op )
1618 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1620 #endif /* LDAP_SLAPI */