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( &SLAPD_GLOBAL(runqueue).rq_mutex );
348 LDAP_STAILQ_INIT( &SLAPD_GLOBAL(runqueue).task_list );
349 LDAP_STAILQ_INIT( &SLAPD_GLOBAL(runqueue).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( &SLAPD_GLOBAL(runqueue).rq_mutex );
382 ldap_pvt_runqueue_insert( &SLAPD_GLOBAL(runqueue),
383 si->si_interval, do_syncrepl, (void *) si );
384 ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(runqueue).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)
472 syncinfo_t *si_entry;
473 struct slap_csn_entry *csne;
475 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
477 /* destroy each backend database */
478 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
480 while ( !LDAP_STAILQ_EMPTY( &bd->be_syncinfo )) {
481 si_entry = LDAP_STAILQ_FIRST( &bd->be_syncinfo );
482 LDAP_STAILQ_REMOVE_HEAD( &bd->be_syncinfo, si_next );
483 syncinfo_free( si_entry );
486 if ( bd->be_pending_csn_list ) {
487 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
489 struct slap_csn_entry *tmp_csne = csne;
491 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
492 ch_free( csne->ce_csn->bv_val );
493 ch_free( csne->ce_csn );
494 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
499 if ( bd->bd_info->bi_db_destroy ) {
500 bd->bd_info->bi_db_destroy( bd );
502 ber_bvarray_free( bd->be_suffix );
503 ber_bvarray_free( bd->be_nsuffix );
504 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
505 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
506 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
507 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
508 acl_destroy( bd->be_acl, frontendDB->be_acl );
512 /* destroy each backend type */
513 for( i = 0; i < nBackendInfo; i++ ) {
514 if( backendInfo[i].bi_destroy ) {
515 backendInfo[i].bi_destroy(
521 if (backendInfo != binfo) {
524 #endif /* SLAPD_MODULES */
529 /* destroy frontend database */
531 if ( bd->bd_info->bi_db_destroy ) {
532 bd->bd_info->bi_db_destroy( bd );
534 ber_bvarray_free( bd->be_suffix );
535 ber_bvarray_free( bd->be_nsuffix );
536 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
537 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
538 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
539 acl_destroy( bd->be_acl, frontendDB->be_acl );
544 BackendInfo* backend_info(const char *type)
548 /* search for the backend type */
549 for( i = 0; i < nBackendInfo; i++ ) {
550 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
551 return &backendInfo[i];
564 BackendInfo *bi = backend_info(type);
568 fprintf( stderr, "Unrecognized database type (%s)\n", type );
574 backendDB = (BackendDB *) ch_realloc(
576 (nBackendDB + 1) * sizeof(Backend) );
578 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
580 /* did realloc move our table? if so, fix up dependent pointers */
581 if ( be != backendDB ) {
583 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
584 be->be_pcl_mutexp = &be->be_pcl_mutex;
588 be = &backends[nbackends++];
591 be->be_def_limit = frontendDB->be_def_limit;
592 be->be_dfltaccess = frontendDB->be_dfltaccess;
594 be->be_restrictops = frontendDB->be_restrictops;
595 be->be_requires = frontendDB->be_requires;
596 be->be_ssf_set = frontendDB->be_ssf_set;
598 be->be_context_csn.bv_len = 0;
599 be->be_context_csn.bv_val = NULL;
600 be->be_pcl_mutexp = &be->be_pcl_mutex;
601 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
603 LDAP_STAILQ_INIT( &be->be_syncinfo );
605 /* assign a default depth limit for alias deref */
606 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
609 rc = bi->bi_db_init( be );
613 fprintf( stderr, "database init failed (%s)\n", type );
627 for ( i = 0; i < nbackends; i++ ) {
628 if ( backends[i].bd_info->bi_db_close ) {
629 (*backends[i].bd_info->bi_db_close)( &backends[i] );
633 if ( frontendDB->bd_info->bi_db_close ) {
634 (*frontendDB->bd_info->bi_db_close)( frontendDB );
645 ber_len_t len, dnlen = dn->bv_len;
648 for ( i = 0; i < nbackends; i++ ) {
649 for ( j = 0; backends[i].be_nsuffix != NULL &&
650 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
652 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
658 len = backends[i].be_nsuffix[j].bv_len;
661 /* suffix is longer than DN */
666 * input DN is normalized, so the separator check
667 * need not look at escaping
669 if ( len && len < dnlen &&
670 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
675 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
676 &dn->bv_val[dnlen-len] ) == 0 )
681 if( manageDSAit && len == dnlen &&
682 !SLAP_GLUE_SUBORDINATE( be ) ) {
699 struct berval *bvsuffix )
704 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
707 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
716 be_isroot_dn( Backend *be, struct berval *ndn )
718 if ( !ndn->bv_len ) {
722 if ( !be->be_rootndn.bv_len ) {
726 return dn_match( &be->be_rootndn, ndn );
730 be_sync_update( Operation *op )
732 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
736 be_slurp_update( Operation *op )
738 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
739 be_isupdate_dn( op->o_bd, &op->o_ndn ));
743 be_shadow_update( Operation *op )
745 return ( SLAP_SHADOW( op->o_bd ) &&
746 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
750 be_isupdate_dn( Backend *be, struct berval *ndn )
752 if ( !ndn->bv_len ) return( 0 );
754 if ( !be->be_update_ndn.bv_len ) return( 0 );
756 return dn_match( &be->be_update_ndn, ndn );
760 be_root_dn( Backend *be )
762 return &be->be_rootdn;
766 be_isroot( Operation *op )
768 return be_isroot_dn( op->o_bd, &op->o_ndn );
772 be_isroot_pw( Operation *op )
776 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
780 if( op->o_bd->be_rootpw.bv_len == 0 ) {
784 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
785 ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(passwd_mutex) );
787 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
791 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
793 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
795 lutil_passwd_sasl_conn = NULL;
797 ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(passwd_mutex) );
809 if ( op->o_bd->be_release ) {
810 /* free and release entry from backend */
811 return op->o_bd->be_release( op, e, rw );
820 backend_unbind( Operation *op, SlapReply *rs )
824 for ( i = 0; i < nbackends; i++ ) {
825 #if defined( LDAP_SLAPI )
828 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
829 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
830 rc = slapi_int_call_plugins( &backends[i],
831 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
834 * A preoperation plugin failure will abort the
837 Debug(LDAP_DEBUG_TRACE,
838 "do_bind: Unbind preoperation plugin failed\n",
843 #endif /* defined( LDAP_SLAPI ) */
845 if ( backends[i].be_unbind ) {
846 op->o_bd = &backends[i];
847 (*backends[i].be_unbind)( op, rs );
850 #if defined( LDAP_SLAPI )
851 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
852 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
854 Debug(LDAP_DEBUG_TRACE,
855 "do_unbind: Unbind postoperation plugins failed\n",
858 #endif /* defined( LDAP_SLAPI ) */
865 backend_connection_init(
870 for ( i = 0; i < nbackends; i++ ) {
871 if ( backends[i].be_connection_init ) {
872 (*backends[i].be_connection_init)( &backends[i], conn);
880 backend_connection_destroy(
885 for ( i = 0; i < nbackends; i++ ) {
886 if ( backends[i].be_connection_destroy ) {
887 (*backends[i].be_connection_destroy)( &backends[i], conn);
895 backend_check_controls(
899 LDAPControl **ctrls = op->o_ctrls;
900 rs->sr_err = LDAP_SUCCESS;
903 for( ; *ctrls != NULL ; ctrls++ ) {
904 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
905 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
907 /* FIXME: standards compliance issue
909 * Per RFC 2251 (and LDAPBIS discussions), if the control
910 * is recognized and appropriate for the operation (which
911 * we've already verified), then the server should make
912 * use of the control when performing the operation
913 * (without regard to criticality). This code is incorrect
915 * 1) a service error (e.g., unwillingToPerform) should be
916 * returned where a particular backend cannot service the
918 * 2) this error should be returned irregardless of the
919 * criticality of the control.
921 rs->sr_text = "control unavailable in context";
922 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
932 backend_check_restrictions(
935 struct berval *opdata )
937 slap_mask_t restrictops;
938 slap_mask_t requires;
940 slap_mask_t exopflag = 0;
947 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
951 restrictops = op->o_bd->be_restrictops;
952 requires = op->o_bd->be_requires;
953 ssf = &op->o_bd->be_ssf_set;
956 restrictops = frontendDB->be_restrictops;
957 requires = frontendDB->be_requires;
958 ssf = &frontendDB->be_ssf_set;
961 switch( op->o_tag ) {
963 opflag = SLAP_RESTRICT_OP_ADD;
967 opflag = SLAP_RESTRICT_OP_BIND;
970 case LDAP_REQ_COMPARE:
971 opflag = SLAP_RESTRICT_OP_COMPARE;
973 case LDAP_REQ_DELETE:
975 opflag = SLAP_RESTRICT_OP_DELETE;
977 case LDAP_REQ_EXTENDED:
978 opflag = SLAP_RESTRICT_OP_EXTENDED;
981 /* treat unspecified as a modify */
982 opflag = SLAP_RESTRICT_OP_MODIFY;
987 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
990 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
994 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
995 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
999 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1000 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1004 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1005 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1010 /* treat everything else as a modify */
1011 opflag = SLAP_RESTRICT_OP_MODIFY;
1015 case LDAP_REQ_MODIFY:
1017 opflag = SLAP_RESTRICT_OP_MODIFY;
1019 case LDAP_REQ_RENAME:
1021 opflag = SLAP_RESTRICT_OP_RENAME;
1023 case LDAP_REQ_SEARCH:
1024 opflag = SLAP_RESTRICT_OP_SEARCH;
1026 case LDAP_REQ_UNBIND:
1031 rs->sr_text = "restrict operations internal error";
1032 rs->sr_err = LDAP_OTHER;
1037 /* these checks don't apply to StartTLS */
1039 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1040 if( op->o_transport_ssf < ssf->sss_transport ) {
1041 rs->sr_text = op->o_transport_ssf
1042 ? "stronger transport confidentiality required"
1043 : "transport confidentiality required";
1047 if( op->o_tls_ssf < ssf->sss_tls ) {
1048 rs->sr_text = op->o_tls_ssf
1049 ? "stronger TLS confidentiality required"
1050 : "TLS confidentiality required";
1055 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1056 /* simple bind specific check */
1057 if( op->o_ssf < ssf->sss_simple_bind ) {
1058 rs->sr_text = op->o_ssf
1059 ? "stronger confidentiality required"
1060 : "confidentiality required";
1065 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1066 /* these checks don't apply to SASL bind */
1068 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1069 rs->sr_text = op->o_sasl_ssf
1070 ? "stronger SASL confidentiality required"
1071 : "SASL confidentiality required";
1075 if( op->o_ssf < ssf->sss_ssf ) {
1076 rs->sr_text = op->o_ssf
1077 ? "stronger confidentiality required"
1078 : "confidentiality required";
1084 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1085 rs->sr_text = op->o_transport_ssf
1086 ? "stronger transport confidentiality required for update"
1087 : "transport confidentiality required for update";
1091 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1092 rs->sr_text = op->o_tls_ssf
1093 ? "stronger TLS confidentiality required for update"
1094 : "TLS confidentiality required for update";
1098 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1099 rs->sr_text = op->o_sasl_ssf
1100 ? "stronger SASL confidentiality required for update"
1101 : "SASL confidentiality required for update";
1105 if( op->o_ssf < ssf->sss_update_ssf ) {
1106 rs->sr_text = op->o_ssf
1107 ? "stronger confidentiality required for update"
1108 : "confidentiality required for update";
1112 if( !( SLAPD_GLOBAL(allows) & SLAP_ALLOW_UPDATE_ANON ) &&
1113 op->o_ndn.bv_len == 0 )
1115 rs->sr_text = "modifications require authentication";
1116 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1120 #ifdef SLAP_X_LISTENER_MOD
1121 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1122 /* no "w" mode means readonly */
1123 rs->sr_text = "modifications not allowed on this listener";
1124 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1127 #endif /* SLAP_X_LISTENER_MOD */
1132 /* these checks don't apply to Bind, StartTLS, or Unbind */
1134 if( requires & SLAP_REQUIRE_STRONG ) {
1135 /* should check mechanism */
1136 if( ( op->o_transport_ssf < ssf->sss_transport
1137 && op->o_authtype == LDAP_AUTH_SIMPLE )
1138 || op->o_dn.bv_len == 0 )
1140 rs->sr_text = "strong(er) authentication required";
1141 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1146 if( requires & SLAP_REQUIRE_SASL ) {
1147 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1148 rs->sr_text = "SASL authentication required";
1149 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1154 if( requires & SLAP_REQUIRE_AUTHC ) {
1155 if( op->o_dn.bv_len == 0 ) {
1156 rs->sr_text = "authentication required";
1157 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1162 if( requires & SLAP_REQUIRE_BIND ) {
1164 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1165 version = op->o_conn->c_protocol;
1166 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1169 /* no bind has occurred */
1170 rs->sr_text = "BIND required";
1171 rs->sr_err = LDAP_OPERATIONS_ERROR;
1176 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1177 if( op->o_protocol < LDAP_VERSION3 ) {
1178 /* no bind has occurred */
1179 rs->sr_text = "operation restricted to LDAPv3 clients";
1180 rs->sr_err = LDAP_OPERATIONS_ERROR;
1185 #ifdef SLAP_X_LISTENER_MOD
1186 if ( !starttls && op->o_dn.bv_len == 0 ) {
1187 if ( op->o_conn->c_listener &&
1188 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1190 /* no "x" mode means bind required */
1191 rs->sr_text = "bind required on this listener";
1192 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1197 if ( !starttls && !updateop ) {
1198 if ( op->o_conn->c_listener &&
1199 !( op->o_conn->c_listener->sl_perms &
1200 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1202 /* no "r" mode means no read */
1203 rs->sr_text = "read not allowed on this listener";
1204 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1208 #endif /* SLAP_X_LISTENER_MOD */
1212 if( ( restrictops & opflag )
1213 || ( exopflag && ( restrictops & exopflag ) ) ) {
1214 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1215 rs->sr_text = "read operations restricted";
1216 } else if ( restrictops & exopflag ) {
1217 rs->sr_text = "extended operation restricted";
1219 rs->sr_text = "operation restricted";
1221 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1225 rs->sr_err = LDAP_SUCCESS;
1229 int backend_check_referrals( Operation *op, SlapReply *rs )
1231 rs->sr_err = LDAP_SUCCESS;
1233 if( op->o_bd->be_chk_referrals ) {
1234 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1236 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1237 send_ldap_result( op, rs );
1249 AttributeDescription *at,
1257 if (op->o_bd == NULL) {
1258 rc = LDAP_NO_SUCH_OBJECT;
1259 } else if ( op->o_bd->be_fetch ) {
1260 rc = ( op->o_bd->be_fetch )( op, ndn,
1263 rc = LDAP_UNWILLING_TO_PERFORM;
1272 struct berval *gr_ndn,
1273 struct berval *op_ndn,
1274 ObjectClass *group_oc,
1275 AttributeDescription *group_at )
1281 Backend *be = op->o_bd;
1283 if ( op->o_abandon ) return SLAPD_ABANDON;
1285 op->o_bd = select_backend( gr_ndn, 0, 0 );
1287 for (g = op->o_groups; g; g=g->ga_next) {
1288 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1289 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1291 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1300 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1304 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1308 if ( op->o_pb != NULL ) {
1309 init_group_pblock( op, target, e, op_ndn, group_at );
1311 rc = call_group_preop_plugins( op );
1312 if ( rc == LDAP_SUCCESS ) {
1316 #endif /* LDAP_SLAPI */
1318 a = attr_find( e->e_attrs, group_at );
1320 /* If the attribute is a subtype of labeledURI, treat this as
1321 * a dynamic group ala groupOfURLs
1323 if (is_at_subtype( group_at->ad_type,
1324 slap_schema.si_ad_labeledURI->ad_type ) )
1328 struct berval bv, nbase;
1331 Backend *b2 = op->o_bd;
1333 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1336 op->o_bd = select_backend( op_ndn, 0, 0 );
1337 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1342 for (i=0; a->a_vals[i].bv_val; i++) {
1343 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1348 nbase.bv_val = NULL;
1349 /* host part must be empty */
1350 /* attrs and extensions parts must be empty */
1351 if (( ludp->lud_host && *ludp->lud_host ) ||
1352 ludp->lud_attrs || ludp->lud_exts )
1356 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1357 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1358 op->o_tmpmemctx ) != LDAP_SUCCESS )
1362 switch(ludp->lud_scope) {
1363 case LDAP_SCOPE_BASE:
1364 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1366 case LDAP_SCOPE_ONELEVEL:
1367 dnParent(op_ndn, &bv );
1368 if ( !dn_match( &nbase, &bv )) goto loopit;
1370 case LDAP_SCOPE_SUBTREE:
1371 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1373 #ifdef LDAP_SCOPE_SUBORDINATE
1374 case LDAP_SCOPE_SUBORDINATE:
1375 if ( dn_match( &nbase, op_ndn ) &&
1376 !dnIsSuffix(op_ndn, &nbase ))
1382 filter = str2filter_x( op, ludp->lud_filter );
1384 if ( test_filter( NULL, user, filter ) ==
1389 filter_free_x( op, filter );
1392 ldap_free_urldesc( ludp );
1393 if ( nbase.bv_val ) {
1394 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1396 if ( rc == 0 ) break;
1398 if ( user != target ) {
1399 be_entry_release_r( op, user );
1404 rc = value_find_ex( group_at,
1405 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1406 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1407 a->a_nvals, op_ndn, op->o_tmpmemctx );
1410 rc = LDAP_NO_SUCH_ATTRIBUTE;
1413 be_entry_release_r( op, e );
1416 rc = LDAP_NO_SUCH_OBJECT;
1420 if ( op->o_pb ) call_group_postop_plugins( op );
1421 #endif /* LDAP_SLAPI */
1423 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1424 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1426 g->ga_be = op->o_bd;
1427 g->ga_oc = group_oc;
1428 g->ga_at = group_at;
1430 g->ga_len = gr_ndn->bv_len;
1431 strcpy(g->ga_ndn, gr_ndn->bv_val);
1432 g->ga_next = op->o_groups;
1441 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1445 BerVarray *vals = (BerVarray *)c->cac_private;
1446 Operation *op = NULL;
1449 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1454 if ( op->o_conn && access_allowed( op,
1455 e, a->a_desc, NULL, ACL_AUTH,
1456 &c->cac_acl_state ) == 0 ) {
1460 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1462 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1464 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1465 if ( op->o_conn && access_allowed( op,
1468 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1472 &a->a_nvals[i], op->o_tmpmemctx );
1473 if (v[j].bv_val ) j++;
1477 op->o_tmpfree( v, op->o_tmpmemctx );
1489 #endif /* LDAP_SLAPI */
1496 AttributeDescription *entry_at,
1498 slap_access_t access )
1502 int i, j, rc = LDAP_SUCCESS;
1503 AccessControlState acl_state = ACL_STATE_INIT;
1504 Backend *be = op->o_bd;
1506 op->o_bd = select_backend( edn, 0, 0 );
1508 if ( target && dn_match( &target->e_nname, edn ) ) {
1511 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1515 a = attr_find( e->e_attrs, entry_at );
1519 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1520 e, entry_at, NULL, access,
1521 &acl_state ) == 0 ) {
1522 rc = LDAP_INSUFFICIENT_ACCESS;
1526 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1528 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1530 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1531 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1534 access, &acl_state ) == 0 ) {
1538 &a->a_nvals[i], op->o_tmpmemctx );
1539 if (v[j].bv_val ) j++;
1542 op->o_tmpfree( v, op->o_tmpmemctx );
1544 rc = LDAP_INSUFFICIENT_ACCESS;
1553 else if ( op->o_pb ) {
1554 /* try any computed attributes */
1555 computed_attr_context ctx;
1556 AttributeName aname;
1558 slapi_int_pblock_set_operation( op->o_pb, op );
1560 ctx.cac_pb = op->o_pb;
1561 ctx.cac_attrs = NULL;
1562 ctx.cac_userattrs = 0;
1563 ctx.cac_opattrs = 0;
1564 ctx.cac_acl_state = acl_state;
1565 ctx.cac_private = (void *)vals;
1567 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1568 rc = LDAP_INSUFFICIENT_ACCESS;
1572 #endif /* LDAP_SLAPI */
1573 freeit: if (e != target ) {
1574 be_entry_release_r( op, e );
1582 int backend_operational(
1589 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1590 /* just count them */ ;
1593 * If operational attributes (allegedly) are required,
1594 * and the backend supports specific operational attributes,
1595 * add them to the attribute list
1597 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1598 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1600 *ap = slap_operational_entryDN( rs->sr_entry );
1601 ap = &(*ap)->a_next;
1604 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1605 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1607 *ap = slap_operational_subschemaSubentry( op->o_bd );
1608 ap = &(*ap)->a_next;
1611 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1612 op->o_bd && op->o_bd->be_operational != NULL )
1616 a = rs->sr_operational_attrs;
1617 rs->sr_operational_attrs = NULL;
1618 rc = op->o_bd->be_operational( op, rs );
1619 *ap = rs->sr_operational_attrs;
1621 rs->sr_operational_attrs = a;
1624 for ( ; *ap; ap = &(*ap)->a_next )
1625 /* just count them */ ;
1632 static void init_group_pblock( Operation *op, Entry *target,
1633 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1635 slapi_int_pblock_set_operation( op->o_pb, op );
1637 slapi_pblock_set( op->o_pb,
1638 SLAPI_X_GROUP_ENTRY, (void *)e );
1639 slapi_pblock_set( op->o_pb,
1640 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1641 slapi_pblock_set( op->o_pb,
1642 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1643 slapi_pblock_set( op->o_pb,
1644 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1647 static int call_group_preop_plugins( Operation *op )
1651 rc = slapi_int_call_plugins( op->o_bd,
1652 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1654 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1655 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1657 rc = LDAP_NO_SUCH_ATTRIBUTE;
1666 static void call_group_postop_plugins( Operation *op )
1668 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1670 #endif /* LDAP_SLAPI */