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-2004 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.
59 #if SLAPD_BDB == SLAPD_MOD_STATIC
60 #include "back-bdb/external.h"
62 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
63 #include "back-dnssrv/external.h"
65 #if SLAPD_HDB == SLAPD_MOD_STATIC
66 #include "back-hdb/external.h"
68 #if SLAPD_LDAP == SLAPD_MOD_STATIC
69 #include "back-ldap/external.h"
71 #if SLAPD_LDBM == SLAPD_MOD_STATIC
72 #include "back-ldbm/external.h"
74 #if SLAPD_META == SLAPD_MOD_STATIC
75 #include "back-meta/external.h"
77 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
78 #include "back-monitor/external.h"
80 #if SLAPD_NULL == SLAPD_MOD_STATIC
81 #include "back-null/external.h"
83 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
84 #include "back-passwd/external.h"
86 #if SLAPD_PERL == SLAPD_MOD_STATIC
87 #include "back-perl/external.h"
89 #if SLAPD_RELAY == SLAPD_MOD_STATIC
90 #include "back-relay/external.h"
92 #if SLAPD_SHELL == SLAPD_MOD_STATIC
93 #include "back-shell/external.h"
95 #if SLAPD_TCL == SLAPD_MOD_STATIC
96 #include "back-tcl/external.h"
98 #if SLAPD_SQL == SLAPD_MOD_STATIC
99 #include "back-sql/external.h"
101 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
102 #include "private/external.h"
105 static BackendInfo binfo[] = {
106 #if SLAPD_BDB == SLAPD_MOD_STATIC
107 {"bdb", bdb_initialize},
109 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
110 {"dnssrv", dnssrv_back_initialize},
112 #if SLAPD_HDB == SLAPD_MOD_STATIC
113 {"hdb", hdb_initialize},
115 #if SLAPD_LDAP == SLAPD_MOD_STATIC
116 {"ldap", ldap_back_initialize},
118 #if SLAPD_LDBM == SLAPD_MOD_STATIC
119 {"ldbm", ldbm_back_initialize},
121 #if SLAPD_META == SLAPD_MOD_STATIC
122 {"meta", meta_back_initialize},
124 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
125 {"monitor", monitor_back_initialize},
127 #if SLAPD_NULL == SLAPD_MOD_STATIC
128 {"null", null_back_initialize},
130 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
131 {"passwd", passwd_back_initialize},
133 #if SLAPD_PERL == SLAPD_MOD_STATIC
134 {"perl", perl_back_initialize},
136 #if SLAPD_RELAY == SLAPD_MOD_STATIC
137 {"relay", relay_back_initialize},
139 #if SLAPD_SHELL == SLAPD_MOD_STATIC
140 {"shell", shell_back_initialize},
142 #if SLAPD_TCL == SLAPD_MOD_STATIC
143 {"tcl", tcl_back_initialize},
145 #if SLAPD_SQL == SLAPD_MOD_STATIC
146 {"sql", backsql_initialize},
148 /* for any private backend */
149 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
150 {"private", private_back_initialize},
155 int nBackendInfo = 0;
156 BackendInfo *backendInfo = NULL;
159 BackendDB *backendDB = NULL;
161 ldap_pvt_thread_pool_t syncrepl_pool;
162 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
164 int backend_init(void)
168 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
170 if((nBackendInfo != 0) || (backendInfo != NULL)) {
171 /* already initialized */
172 Debug( LDAP_DEBUG_ANY,
173 "backend_init: already initialized.\n", 0, 0, 0 );
178 binfo[nBackendInfo].bi_type != NULL;
181 assert( binfo[nBackendInfo].bi_init );
183 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
186 Debug( LDAP_DEBUG_ANY,
187 "backend_init: initialized for type \"%s\"\n",
188 binfo[nBackendInfo].bi_type, 0, 0 );
189 /* destroy those we've already inited */
194 if ( binfo[nBackendInfo].bi_destroy ) {
195 binfo[nBackendInfo].bi_destroy(
196 &binfo[nBackendInfo] );
203 if ( nBackendInfo > 0) {
212 Debug( LDAP_DEBUG_ANY,
213 "backend_init: failed\n",
217 #endif /* SLAPD_MODULES */
220 int backend_add(BackendInfo *aBackendInfo)
224 if ( aBackendInfo->bi_init == NULL ) {
225 Debug( LDAP_DEBUG_ANY, "backend_add: "
226 "backend type \"%s\" does not have the (mandatory)init function\n",
227 aBackendInfo->bi_type, 0, 0 );
231 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
232 Debug( LDAP_DEBUG_ANY,
233 "backend_add: initialization for type \"%s\" failed\n",
234 aBackendInfo->bi_type, 0, 0 );
238 /* now add the backend type to the Backend Info List */
240 BackendInfo *newBackendInfo = 0;
242 /* if backendInfo == binfo no deallocation of old backendInfo */
243 if (backendInfo == binfo) {
244 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
245 AC_MEMCPY(newBackendInfo, backendInfo,
246 sizeof(BackendInfo) * nBackendInfo);
248 newBackendInfo = ch_realloc(backendInfo,
249 sizeof(BackendInfo) * (nBackendInfo + 1));
252 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
253 sizeof(BackendInfo));
254 backendInfo = newBackendInfo;
260 /* startup a specific backend database */
261 int backend_startup_one(Backend *be)
267 be->be_pending_csn_list = (struct be_pcl *)
268 ch_calloc( 1, sizeof( struct be_pcl ));
269 build_new_dn( &be->be_context_csn, be->be_nsuffix,
270 (struct berval *)&slap_ldapsync_cn_bv, NULL );
272 LDAP_TAILQ_INIT( be->be_pending_csn_list );
274 Debug( LDAP_DEBUG_TRACE,
275 "backend_startup: starting \"%s\"\n",
276 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
278 if ( be->bd_info->bi_db_open ) {
279 rc = be->bd_info->bi_db_open( be );
281 Debug( LDAP_DEBUG_ANY,
282 "backend_startup: bi_db_open failed! (%d)\n",
289 int backend_startup(Backend *be)
294 if( ! ( nBackendDB > 0 ) ) {
296 Debug( LDAP_DEBUG_ANY,
297 "backend_startup: %d databases to startup.\n",
303 if ( be->bd_info->bi_open ) {
304 rc = be->bd_info->bi_open( be->bd_info );
306 Debug( LDAP_DEBUG_ANY,
307 "backend_startup: bi_open failed!\n",
314 return backend_startup_one( be );
317 /* open frontend, if required */
318 if ( frontendDB->bd_info->bi_db_open ) {
319 rc = frontendDB->bd_info->bi_db_open( frontendDB );
321 Debug( LDAP_DEBUG_ANY,
322 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
328 /* open each backend type */
329 for( i = 0; i < nBackendInfo; i++ ) {
330 if( backendInfo[i].bi_nDB == 0) {
331 /* no database of this type, don't open */
335 if( backendInfo[i].bi_open ) {
336 rc = backendInfo[i].bi_open(
339 Debug( LDAP_DEBUG_ANY,
340 "backend_startup: bi_open %d failed!\n",
347 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
348 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
349 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
351 /* open each backend database */
352 for( i = 0; i < nBackendDB; i++ ) {
353 if ( backendDB[i].be_suffix == NULL ) {
354 Debug( LDAP_DEBUG_ANY,
355 "backend_startup: warning, database %d (%s) "
357 i, backendDB[i].bd_info->bi_type, 0 );
359 /* append global access controls */
360 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
362 rc = backend_startup_one( &backendDB[i] );
367 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
370 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
371 backendDB[i].be_modify && backendDB[i].be_delete )) {
372 Debug( LDAP_DEBUG_ANY,
373 "backend_startup: database(%d) does not support "
374 "operations required for syncrepl", i, 0, 0 );
378 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
379 si->si_be = &backendDB[i];
381 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
382 ldap_pvt_runqueue_insert( &syncrepl_rq,
383 si->si_interval, do_syncrepl, (void *) si );
384 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
392 int backend_num( Backend *be )
396 if( be == NULL ) return -1;
398 for( i = 0; i < nBackendDB; i++ ) {
399 if( be == &backendDB[i] ) return i;
404 int backend_shutdown( Backend *be )
410 /* shutdown a specific backend database */
412 if ( be->bd_info->bi_nDB == 0 ) {
413 /* no database of this type, we never opened it */
417 if ( be->bd_info->bi_db_close ) {
418 be->bd_info->bi_db_close( be );
421 if( be->bd_info->bi_close ) {
422 be->bd_info->bi_close( be->bd_info );
428 /* close each backend database */
429 for( i = 0; i < nBackendDB; i++ ) {
430 if ( backendDB[i].bd_info->bi_db_close ) {
431 backendDB[i].bd_info->bi_db_close(
436 Debug( LDAP_DEBUG_ANY,
437 "backend_close: bi_db_close %s failed!\n",
438 backendDB[i].be_type, 0, 0 );
442 /* close each backend type */
443 for( i = 0; i < nBackendInfo; i++ ) {
444 if( backendInfo[i].bi_nDB == 0 ) {
445 /* no database of this type */
449 if( backendInfo[i].bi_close ) {
450 backendInfo[i].bi_close(
455 /* close frontend, if required */
456 if ( frontendDB->bd_info->bi_db_close ) {
457 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
459 Debug( LDAP_DEBUG_ANY,
460 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
468 int backend_destroy(void)
473 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
475 /* destroy each backend database */
476 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
477 if ( bd->bd_info->bi_db_destroy ) {
478 bd->bd_info->bi_db_destroy( bd );
480 ber_bvarray_free( bd->be_suffix );
481 ber_bvarray_free( bd->be_nsuffix );
482 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
483 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
484 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
485 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
486 acl_destroy( bd->be_acl, frontendDB->be_acl );
490 /* destroy each backend type */
491 for( i = 0; i < nBackendInfo; i++ ) {
492 if( backendInfo[i].bi_destroy ) {
493 backendInfo[i].bi_destroy(
499 if (backendInfo != binfo) {
502 #endif /* SLAPD_MODULES */
507 /* destroy frontend database */
509 if ( bd->bd_info->bi_db_destroy ) {
510 bd->bd_info->bi_db_destroy( bd );
512 ber_bvarray_free( bd->be_suffix );
513 ber_bvarray_free( bd->be_nsuffix );
514 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
515 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
516 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
517 acl_destroy( bd->be_acl, frontendDB->be_acl );
522 BackendInfo* backend_info(const char *type)
526 /* search for the backend type */
527 for( i = 0; i < nBackendInfo; i++ ) {
528 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
529 return &backendInfo[i];
542 BackendInfo *bi = backend_info(type);
546 fprintf( stderr, "Unrecognized database type (%s)\n", type );
552 backendDB = (BackendDB *) ch_realloc(
554 (nBackendDB + 1) * sizeof(Backend) );
556 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
558 /* did realloc move our table? if so, fix up dependent pointers */
559 if ( be != backendDB ) {
561 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
562 be->be_pcl_mutexp = &be->be_pcl_mutex;
566 be = &backends[nbackends++];
569 be->be_def_limit = frontendDB->be_def_limit;
570 be->be_dfltaccess = frontendDB->be_dfltaccess;
572 be->be_restrictops = frontendDB->be_restrictops;
573 be->be_requires = frontendDB->be_requires;
574 be->be_ssf_set = frontendDB->be_ssf_set;
576 be->be_context_csn.bv_len = 0;
577 be->be_context_csn.bv_val = NULL;
578 be->be_pcl_mutexp = &be->be_pcl_mutex;
579 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
581 LDAP_STAILQ_INIT( &be->be_syncinfo );
583 /* assign a default depth limit for alias deref */
584 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
587 rc = bi->bi_db_init( be );
591 fprintf( stderr, "database init failed (%s)\n", type );
605 for ( i = 0; i < nbackends; i++ ) {
606 if ( backends[i].bd_info->bi_db_close ) {
607 (*backends[i].bd_info->bi_db_close)( &backends[i] );
611 if ( frontendDB->bd_info->bi_db_close ) {
612 (*frontendDB->bd_info->bi_db_close)( frontendDB );
623 ber_len_t len, dnlen = dn->bv_len;
626 for ( i = 0; i < nbackends; i++ ) {
627 for ( j = 0; backends[i].be_nsuffix != NULL &&
628 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
630 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
636 len = backends[i].be_nsuffix[j].bv_len;
639 /* suffix is longer than DN */
644 * input DN is normalized, so the separator check
645 * need not look at escaping
647 if ( len && len < dnlen &&
648 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
653 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
654 &dn->bv_val[dnlen-len] ) == 0 )
659 if( manageDSAit && len == dnlen &&
660 !SLAP_GLUE_SUBORDINATE( be ) ) {
677 struct berval *bvsuffix )
682 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
685 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
694 be_isroot_dn( Backend *be, struct berval *ndn )
696 if ( !ndn->bv_len ) {
700 if ( !be->be_rootndn.bv_len ) {
704 return dn_match( &be->be_rootndn, ndn );
708 be_sync_update( Operation *op )
710 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
714 be_slurp_update( Operation *op )
716 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
717 be_isupdate_dn( op->o_bd, &op->o_ndn ));
721 be_shadow_update( Operation *op )
723 return ( SLAP_SHADOW( op->o_bd ) &&
724 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
728 be_isupdate_dn( Backend *be, struct berval *ndn )
730 if ( !ndn->bv_len ) return( 0 );
732 if ( !be->be_update_ndn.bv_len ) return( 0 );
734 return dn_match( &be->be_update_ndn, ndn );
738 be_root_dn( Backend *be )
740 return &be->be_rootdn;
744 be_isroot( Operation *op )
746 return be_isroot_dn( op->o_bd, &op->o_ndn );
750 be_isroot_pw( Operation *op )
754 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
758 if( op->o_bd->be_rootpw.bv_len == 0 ) {
762 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
763 ldap_pvt_thread_mutex_lock( &passwd_mutex );
765 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
769 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
771 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
773 lutil_passwd_sasl_conn = NULL;
775 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
787 if ( op->o_bd->be_release ) {
788 /* free and release entry from backend */
789 return op->o_bd->be_release( op, e, rw );
798 backend_unbind( Operation *op, SlapReply *rs )
802 for ( i = 0; i < nbackends; i++ ) {
803 #if defined( LDAP_SLAPI )
806 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
807 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
808 rc = slapi_int_call_plugins( &backends[i],
809 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
812 * A preoperation plugin failure will abort the
815 Debug(LDAP_DEBUG_TRACE,
816 "do_bind: Unbind preoperation plugin failed\n",
821 #endif /* defined( LDAP_SLAPI ) */
823 if ( backends[i].be_unbind ) {
824 op->o_bd = &backends[i];
825 (*backends[i].be_unbind)( op, rs );
828 #if defined( LDAP_SLAPI )
829 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
830 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
832 Debug(LDAP_DEBUG_TRACE,
833 "do_unbind: Unbind postoperation plugins failed\n",
836 #endif /* defined( LDAP_SLAPI ) */
843 backend_connection_init(
848 for ( i = 0; i < nbackends; i++ ) {
849 if ( backends[i].be_connection_init ) {
850 (*backends[i].be_connection_init)( &backends[i], conn);
858 backend_connection_destroy(
863 for ( i = 0; i < nbackends; i++ ) {
864 if ( backends[i].be_connection_destroy ) {
865 (*backends[i].be_connection_destroy)( &backends[i], conn);
873 backend_check_controls(
877 LDAPControl **ctrls = op->o_ctrls;
878 rs->sr_err = LDAP_SUCCESS;
881 for( ; *ctrls != NULL ; ctrls++ ) {
882 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
883 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
885 /* FIXME: standards compliance issue
887 * Per RFC 2251 (and LDAPBIS discussions), if the control
888 * is recognized and appropriate for the operation (which
889 * we've already verified), then the server should make
890 * use of the control when performing the operation
891 * (without regard to criticality). This code is incorrect
893 * 1) a service error (e.g., unwillingToPerform) should be
894 * returned where a particular backend cannot service the
896 * 2) this error should be returned irregardless of the
897 * criticality of the control.
899 rs->sr_text = "control unavailable in context";
900 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
910 backend_check_restrictions(
913 struct berval *opdata )
915 slap_mask_t restrictops;
916 slap_mask_t requires;
918 slap_mask_t exopflag = 0;
925 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
929 restrictops = op->o_bd->be_restrictops;
930 requires = op->o_bd->be_requires;
931 ssf = &op->o_bd->be_ssf_set;
934 restrictops = frontendDB->be_restrictops;
935 requires = frontendDB->be_requires;
936 ssf = &frontendDB->be_ssf_set;
939 switch( op->o_tag ) {
941 opflag = SLAP_RESTRICT_OP_ADD;
945 opflag = SLAP_RESTRICT_OP_BIND;
948 case LDAP_REQ_COMPARE:
949 opflag = SLAP_RESTRICT_OP_COMPARE;
951 case LDAP_REQ_DELETE:
953 opflag = SLAP_RESTRICT_OP_DELETE;
955 case LDAP_REQ_EXTENDED:
956 opflag = SLAP_RESTRICT_OP_EXTENDED;
959 /* treat unspecified as a modify */
960 opflag = SLAP_RESTRICT_OP_MODIFY;
965 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
968 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
972 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
973 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
977 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
978 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
982 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
983 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
988 /* treat everything else as a modify */
989 opflag = SLAP_RESTRICT_OP_MODIFY;
993 case LDAP_REQ_MODIFY:
995 opflag = SLAP_RESTRICT_OP_MODIFY;
997 case LDAP_REQ_RENAME:
999 opflag = SLAP_RESTRICT_OP_RENAME;
1001 case LDAP_REQ_SEARCH:
1002 opflag = SLAP_RESTRICT_OP_SEARCH;
1004 case LDAP_REQ_UNBIND:
1009 rs->sr_text = "restrict operations internal error";
1010 rs->sr_err = LDAP_OTHER;
1015 /* these checks don't apply to StartTLS */
1017 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1018 if( op->o_transport_ssf < ssf->sss_transport ) {
1019 rs->sr_text = op->o_transport_ssf
1020 ? "stronger transport confidentiality required"
1021 : "transport confidentiality required";
1025 if( op->o_tls_ssf < ssf->sss_tls ) {
1026 rs->sr_text = op->o_tls_ssf
1027 ? "stronger TLS confidentiality required"
1028 : "TLS confidentiality required";
1033 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1034 /* simple bind specific check */
1035 if( op->o_ssf < ssf->sss_simple_bind ) {
1036 rs->sr_text = op->o_ssf
1037 ? "stronger confidentiality required"
1038 : "confidentiality required";
1043 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1044 /* these checks don't apply to SASL bind */
1046 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1047 rs->sr_text = op->o_sasl_ssf
1048 ? "stronger SASL confidentiality required"
1049 : "SASL confidentiality required";
1053 if( op->o_ssf < ssf->sss_ssf ) {
1054 rs->sr_text = op->o_ssf
1055 ? "stronger confidentiality required"
1056 : "confidentiality required";
1062 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1063 rs->sr_text = op->o_transport_ssf
1064 ? "stronger transport confidentiality required for update"
1065 : "transport confidentiality required for update";
1069 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1070 rs->sr_text = op->o_tls_ssf
1071 ? "stronger TLS confidentiality required for update"
1072 : "TLS confidentiality required for update";
1076 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1077 rs->sr_text = op->o_sasl_ssf
1078 ? "stronger SASL confidentiality required for update"
1079 : "SASL confidentiality required for update";
1083 if( op->o_ssf < ssf->sss_update_ssf ) {
1084 rs->sr_text = op->o_ssf
1085 ? "stronger confidentiality required for update"
1086 : "confidentiality required for update";
1090 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1091 op->o_ndn.bv_len == 0 )
1093 rs->sr_text = "modifications require authentication";
1094 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1098 #ifdef SLAP_X_LISTENER_MOD
1099 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1100 /* no "w" mode means readonly */
1101 rs->sr_text = "modifications not allowed on this listener";
1102 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1105 #endif /* SLAP_X_LISTENER_MOD */
1110 /* these checks don't apply to Bind, StartTLS, or Unbind */
1112 if( requires & SLAP_REQUIRE_STRONG ) {
1113 /* should check mechanism */
1114 if( ( op->o_transport_ssf < ssf->sss_transport
1115 && op->o_authtype == LDAP_AUTH_SIMPLE )
1116 || op->o_dn.bv_len == 0 )
1118 rs->sr_text = "strong(er) authentication required";
1119 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1124 if( requires & SLAP_REQUIRE_SASL ) {
1125 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1126 rs->sr_text = "SASL authentication required";
1127 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1132 if( requires & SLAP_REQUIRE_AUTHC ) {
1133 if( op->o_dn.bv_len == 0 ) {
1134 rs->sr_text = "authentication required";
1135 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1140 if( requires & SLAP_REQUIRE_BIND ) {
1142 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1143 version = op->o_conn->c_protocol;
1144 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1147 /* no bind has occurred */
1148 rs->sr_text = "BIND required";
1149 rs->sr_err = LDAP_OPERATIONS_ERROR;
1154 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1155 if( op->o_protocol < LDAP_VERSION3 ) {
1156 /* no bind has occurred */
1157 rs->sr_text = "operation restricted to LDAPv3 clients";
1158 rs->sr_err = LDAP_OPERATIONS_ERROR;
1163 #ifdef SLAP_X_LISTENER_MOD
1164 if ( !starttls && op->o_dn.bv_len == 0 ) {
1165 if ( op->o_conn->c_listener &&
1166 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1168 /* no "x" mode means bind required */
1169 rs->sr_text = "bind required on this listener";
1170 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1175 if ( !starttls && !updateop ) {
1176 if ( op->o_conn->c_listener &&
1177 !( op->o_conn->c_listener->sl_perms &
1178 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1180 /* no "r" mode means no read */
1181 rs->sr_text = "read not allowed on this listener";
1182 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1186 #endif /* SLAP_X_LISTENER_MOD */
1190 if( ( restrictops & opflag )
1191 || ( exopflag && ( restrictops & exopflag ) ) ) {
1192 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1193 rs->sr_text = "read operations restricted";
1194 } else if ( restrictops & exopflag ) {
1195 rs->sr_text = "extended operation restricted";
1197 rs->sr_text = "operation restricted";
1199 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1203 rs->sr_err = LDAP_SUCCESS;
1207 int backend_check_referrals( Operation *op, SlapReply *rs )
1209 rs->sr_err = LDAP_SUCCESS;
1211 if( op->o_bd->be_chk_referrals ) {
1212 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1214 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1215 send_ldap_result( op, rs );
1227 AttributeDescription *at,
1235 if (op->o_bd == NULL) {
1236 rc = LDAP_NO_SUCH_OBJECT;
1237 } else if ( op->o_bd->be_fetch ) {
1238 rc = ( op->o_bd->be_fetch )( op, ndn,
1241 rc = LDAP_UNWILLING_TO_PERFORM;
1250 struct berval *gr_ndn,
1251 struct berval *op_ndn,
1252 ObjectClass *group_oc,
1253 AttributeDescription *group_at )
1259 Backend *be = op->o_bd;
1261 if ( op->o_abandon ) return SLAPD_ABANDON;
1263 op->o_bd = select_backend( gr_ndn, 0, 0 );
1265 for (g = op->o_groups; g; g=g->ga_next) {
1266 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1267 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1269 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1278 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1282 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1286 if ( op->o_pb != NULL ) {
1287 init_group_pblock( op, target, e, op_ndn, group_at );
1289 rc = call_group_preop_plugins( op );
1290 if ( rc == LDAP_SUCCESS ) {
1294 #endif /* LDAP_SLAPI */
1296 a = attr_find( e->e_attrs, group_at );
1298 /* If the attribute is a subtype of labeledURI, treat this as
1299 * a dynamic group ala groupOfURLs
1301 if (is_at_subtype( group_at->ad_type,
1302 slap_schema.si_ad_labeledURI->ad_type ) )
1306 struct berval bv, nbase;
1309 Backend *b2 = op->o_bd;
1311 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1314 op->o_bd = select_backend( op_ndn, 0, 0 );
1315 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1320 for (i=0; a->a_vals[i].bv_val; i++) {
1321 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1326 nbase.bv_val = NULL;
1327 /* host part must be empty */
1328 /* attrs and extensions parts must be empty */
1329 if (( ludp->lud_host && *ludp->lud_host ) ||
1330 ludp->lud_attrs || ludp->lud_exts )
1334 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1335 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1336 op->o_tmpmemctx ) != LDAP_SUCCESS )
1340 switch(ludp->lud_scope) {
1341 case LDAP_SCOPE_BASE:
1342 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1344 case LDAP_SCOPE_ONELEVEL:
1345 dnParent(op_ndn, &bv );
1346 if ( !dn_match( &nbase, &bv )) goto loopit;
1348 case LDAP_SCOPE_SUBTREE:
1349 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1351 #ifdef LDAP_SCOPE_SUBORDINATE
1352 case LDAP_SCOPE_SUBORDINATE:
1353 if ( dn_match( &nbase, op_ndn ) &&
1354 !dnIsSuffix(op_ndn, &nbase ))
1360 filter = str2filter_x( op, ludp->lud_filter );
1362 if ( test_filter( NULL, user, filter ) ==
1367 filter_free_x( op, filter );
1370 ldap_free_urldesc( ludp );
1371 if ( nbase.bv_val ) {
1372 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1374 if ( rc == 0 ) break;
1376 if ( user != target ) {
1377 be_entry_release_r( op, user );
1382 rc = value_find_ex( group_at,
1383 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1384 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1385 a->a_nvals, op_ndn, op->o_tmpmemctx );
1388 rc = LDAP_NO_SUCH_ATTRIBUTE;
1391 be_entry_release_r( op, e );
1394 rc = LDAP_NO_SUCH_OBJECT;
1398 if ( op->o_pb ) call_group_postop_plugins( op );
1399 #endif /* LDAP_SLAPI */
1401 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1402 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1404 g->ga_be = op->o_bd;
1405 g->ga_oc = group_oc;
1406 g->ga_at = group_at;
1408 g->ga_len = gr_ndn->bv_len;
1409 strcpy(g->ga_ndn, gr_ndn->bv_val);
1410 g->ga_next = op->o_groups;
1419 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1423 BerVarray *vals = (BerVarray *)c->cac_private;
1424 Operation *op = NULL;
1427 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1432 if ( op->o_conn && access_allowed( op,
1433 e, a->a_desc, NULL, ACL_AUTH,
1434 &c->cac_acl_state ) == 0 ) {
1438 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1440 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1442 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1443 if ( op->o_conn && access_allowed( op,
1446 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1450 &a->a_nvals[i], op->o_tmpmemctx );
1451 if (v[j].bv_val ) j++;
1455 op->o_tmpfree( v, op->o_tmpmemctx );
1467 #endif /* LDAP_SLAPI */
1474 AttributeDescription *entry_at,
1476 slap_access_t access )
1480 int i, j, rc = LDAP_SUCCESS;
1481 AccessControlState acl_state = ACL_STATE_INIT;
1482 Backend *be = op->o_bd;
1484 op->o_bd = select_backend( edn, 0, 0 );
1486 if ( target && dn_match( &target->e_nname, edn ) ) {
1489 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1493 a = attr_find( e->e_attrs, entry_at );
1497 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1498 e, entry_at, NULL, access,
1499 &acl_state ) == 0 ) {
1500 rc = LDAP_INSUFFICIENT_ACCESS;
1504 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1506 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1508 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1509 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1512 access, &acl_state ) == 0 ) {
1516 &a->a_nvals[i], op->o_tmpmemctx );
1517 if (v[j].bv_val ) j++;
1520 op->o_tmpfree( v, op->o_tmpmemctx );
1522 rc = LDAP_INSUFFICIENT_ACCESS;
1531 else if ( op->o_pb ) {
1532 /* try any computed attributes */
1533 computed_attr_context ctx;
1534 AttributeName aname;
1536 slapi_int_pblock_set_operation( op->o_pb, op );
1538 ctx.cac_pb = op->o_pb;
1539 ctx.cac_attrs = NULL;
1540 ctx.cac_userattrs = 0;
1541 ctx.cac_opattrs = 0;
1542 ctx.cac_acl_state = acl_state;
1543 ctx.cac_private = (void *)vals;
1545 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1546 rc = LDAP_INSUFFICIENT_ACCESS;
1550 #endif /* LDAP_SLAPI */
1551 freeit: if (e != target ) {
1552 be_entry_release_r( op, e );
1560 int backend_operational(
1567 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1568 /* just count them */ ;
1571 * If operational attributes (allegedly) are required,
1572 * and the backend supports specific operational attributes,
1573 * add them to the attribute list
1575 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1576 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1578 *ap = slap_operational_entryDN( rs->sr_entry );
1579 ap = &(*ap)->a_next;
1582 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1583 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1585 *ap = slap_operational_subschemaSubentry( op->o_bd );
1586 ap = &(*ap)->a_next;
1589 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1590 op->o_bd && op->o_bd->be_operational != NULL )
1594 a = rs->sr_operational_attrs;
1595 rs->sr_operational_attrs = NULL;
1596 rc = op->o_bd->be_operational( op, rs );
1597 *ap = rs->sr_operational_attrs;
1599 rs->sr_operational_attrs = a;
1602 for ( ; *ap; ap = &(*ap)->a_next )
1603 /* just count them */ ;
1610 static void init_group_pblock( Operation *op, Entry *target,
1611 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1613 slapi_int_pblock_set_operation( op->o_pb, op );
1615 slapi_pblock_set( op->o_pb,
1616 SLAPI_X_GROUP_ENTRY, (void *)e );
1617 slapi_pblock_set( op->o_pb,
1618 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1619 slapi_pblock_set( op->o_pb,
1620 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1621 slapi_pblock_set( op->o_pb,
1622 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1625 static int call_group_preop_plugins( Operation *op )
1629 rc = slapi_int_call_plugins( op->o_bd,
1630 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1632 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1633 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1635 rc = LDAP_NO_SUCH_ATTRIBUTE;
1644 static void call_group_postop_plugins( Operation *op )
1646 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1648 #endif /* LDAP_SLAPI */