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 */
54 * If a module is configured as dynamic, its header should not
55 * get included into slapd. While this is a general rule and does
56 * not have much of an effect in UNIX, this rule should be adhered
57 * to for Windows, where dynamic object code should not be implicitly
58 * imported into slapd without appropriate __declspec(dllimport) directives.
61 #if SLAPD_BDB == SLAPD_MOD_STATIC
62 #include "back-bdb/external.h"
64 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
65 #include "back-dnssrv/external.h"
67 #if SLAPD_HDB == SLAPD_MOD_STATIC
68 #include "back-hdb/external.h"
70 #if SLAPD_LDAP == SLAPD_MOD_STATIC
71 #include "back-ldap/external.h"
73 #if SLAPD_LDBM == SLAPD_MOD_STATIC
74 #include "back-ldbm/external.h"
76 #if SLAPD_META == SLAPD_MOD_STATIC
77 #include "back-meta/external.h"
79 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
80 #include "back-monitor/external.h"
82 #if SLAPD_NULL == SLAPD_MOD_STATIC
83 #include "back-null/external.h"
85 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
86 #include "back-passwd/external.h"
88 #if SLAPD_PERL == SLAPD_MOD_STATIC
89 #include "back-perl/external.h"
91 #if SLAPD_RELAY == SLAPD_MOD_STATIC
92 #include "back-relay/external.h"
94 #if SLAPD_SHELL == SLAPD_MOD_STATIC
95 #include "back-shell/external.h"
97 #if SLAPD_TCL == SLAPD_MOD_STATIC
98 #include "back-tcl/external.h"
100 #if SLAPD_SQL == SLAPD_MOD_STATIC
101 #include "back-sql/external.h"
103 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
104 #include "private/external.h"
107 static BackendInfo binfo[] = {
108 #if SLAPD_BDB == SLAPD_MOD_STATIC
109 {"bdb", bdb_initialize},
111 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
112 {"dnssrv", dnssrv_back_initialize},
114 #if SLAPD_HDB == SLAPD_MOD_STATIC
115 {"hdb", hdb_initialize},
117 #if SLAPD_LDAP == SLAPD_MOD_STATIC
118 {"ldap", ldap_back_initialize},
120 #if SLAPD_LDBM == SLAPD_MOD_STATIC
121 {"ldbm", ldbm_back_initialize},
123 #if SLAPD_META == SLAPD_MOD_STATIC
124 {"meta", meta_back_initialize},
126 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
127 {"monitor", monitor_back_initialize},
129 #if SLAPD_NULL == SLAPD_MOD_STATIC
130 {"null", null_back_initialize},
132 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
133 {"passwd", passwd_back_initialize},
135 #if SLAPD_PERL == SLAPD_MOD_STATIC
136 {"perl", perl_back_initialize},
138 #if SLAPD_RELAY == SLAPD_MOD_STATIC
139 {"relay", relay_back_initialize},
141 #if SLAPD_SHELL == SLAPD_MOD_STATIC
142 {"shell", shell_back_initialize},
144 #if SLAPD_TCL == SLAPD_MOD_STATIC
145 {"tcl", tcl_back_initialize},
147 #if SLAPD_SQL == SLAPD_MOD_STATIC
148 {"sql", backsql_initialize},
150 /* for any private backend */
151 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
152 {"private", private_back_initialize},
160 * If a module is configured as dynamic, its header should not
161 * get included into slapd. While this is a general rule and does
162 * not have much of an effect in UNIX, this rule should be adhered
163 * to for Windows, where dynamic object code should not be implicitly
164 * imported into slapd without appropriate __declspec(dllimport) directives.
168 * This file is automatically generated by configure; it defines
169 * the BackendInfo binfo[] structure with the configured static
170 * backend info. It assumes that every backend of type <name>
171 * provides an initialization function
173 * int name_back_initialize( BackendInfo *bi )
175 * that populates the rest of the structure.
180 int nBackendInfo = 0;
181 BackendInfo *backendInfo = NULL;
184 BackendDB *backendDB = NULL;
186 ldap_pvt_thread_pool_t syncrepl_pool;
187 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
189 int backend_init(void)
193 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
195 if((nBackendInfo != 0) || (backendInfo != NULL)) {
196 /* already initialized */
197 Debug( LDAP_DEBUG_ANY,
198 "backend_init: already initialized.\n", 0, 0, 0 );
203 binfo[nBackendInfo].bi_type != NULL;
206 assert( binfo[nBackendInfo].bi_init );
208 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
211 Debug( LDAP_DEBUG_ANY,
212 "backend_init: initialized for type \"%s\"\n",
213 binfo[nBackendInfo].bi_type, 0, 0 );
214 /* destroy those we've already inited */
219 if ( binfo[nBackendInfo].bi_destroy ) {
220 binfo[nBackendInfo].bi_destroy(
221 &binfo[nBackendInfo] );
228 if ( nBackendInfo > 0) {
237 Debug( LDAP_DEBUG_ANY,
238 "backend_init: failed\n",
242 #endif /* SLAPD_MODULES */
245 int backend_add(BackendInfo *aBackendInfo)
249 if ( aBackendInfo->bi_init == NULL ) {
250 Debug( LDAP_DEBUG_ANY, "backend_add: "
251 "backend type \"%s\" does not have the (mandatory)init function\n",
252 aBackendInfo->bi_type, 0, 0 );
256 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
257 Debug( LDAP_DEBUG_ANY,
258 "backend_add: initialization for type \"%s\" failed\n",
259 aBackendInfo->bi_type, 0, 0 );
263 /* now add the backend type to the Backend Info List */
265 BackendInfo *newBackendInfo = 0;
267 /* if backendInfo == binfo no deallocation of old backendInfo */
268 if (backendInfo == binfo) {
269 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
270 AC_MEMCPY(newBackendInfo, backendInfo,
271 sizeof(BackendInfo) * nBackendInfo);
273 newBackendInfo = ch_realloc(backendInfo,
274 sizeof(BackendInfo) * (nBackendInfo + 1));
277 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
278 sizeof(BackendInfo));
279 backendInfo = newBackendInfo;
285 /* startup a specific backend database */
286 int backend_startup_one(Backend *be)
292 be->be_pending_csn_list = (struct be_pcl *)
293 ch_calloc( 1, sizeof( struct be_pcl ));
294 build_new_dn( &be->be_context_csn, be->be_nsuffix,
295 (struct berval *)&slap_ldapsync_cn_bv, NULL );
297 LDAP_TAILQ_INIT( be->be_pending_csn_list );
299 Debug( LDAP_DEBUG_TRACE,
300 "backend_startup: starting \"%s\"\n",
301 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
303 if ( be->bd_info->bi_db_open ) {
304 rc = be->bd_info->bi_db_open( be );
306 Debug( LDAP_DEBUG_ANY,
307 "backend_startup: bi_db_open failed! (%d)\n",
314 int backend_startup(Backend *be)
319 if( ! ( nBackendDB > 0 ) ) {
321 Debug( LDAP_DEBUG_ANY,
322 "backend_startup: %d databases to startup.\n",
328 if ( be->bd_info->bi_open ) {
329 rc = be->bd_info->bi_open( be->bd_info );
331 Debug( LDAP_DEBUG_ANY,
332 "backend_startup: bi_open failed!\n",
339 return backend_startup_one( be );
342 /* open frontend, if required */
343 if ( frontendDB->bd_info->bi_db_open ) {
344 rc = frontendDB->bd_info->bi_db_open( frontendDB );
346 Debug( LDAP_DEBUG_ANY,
347 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
353 /* open each backend type */
354 for( i = 0; i < nBackendInfo; i++ ) {
355 if( backendInfo[i].bi_nDB == 0) {
356 /* no database of this type, don't open */
360 if( backendInfo[i].bi_open ) {
361 rc = backendInfo[i].bi_open(
364 Debug( LDAP_DEBUG_ANY,
365 "backend_startup: bi_open %d failed!\n",
372 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
373 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
374 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
376 /* open each backend database */
377 for( i = 0; i < nBackendDB; i++ ) {
378 if ( backendDB[i].be_suffix == NULL ) {
379 Debug( LDAP_DEBUG_ANY,
380 "backend_startup: warning, database %d (%s) "
382 i, backendDB[i].bd_info->bi_type, 0 );
384 /* append global access controls */
385 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
387 rc = backend_startup_one( &backendDB[i] );
392 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
395 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
396 backendDB[i].be_modify && backendDB[i].be_delete )) {
397 Debug( LDAP_DEBUG_ANY,
398 "backend_startup: database(%d) does not support "
399 "operations required for syncrepl", i, 0, 0 );
403 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
404 si->si_be = &backendDB[i];
406 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
407 ldap_pvt_runqueue_insert( &syncrepl_rq,
408 si->si_interval, do_syncrepl, (void *) si );
409 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
417 int backend_num( Backend *be )
421 if( be == NULL ) return -1;
423 for( i = 0; i < nBackendDB; i++ ) {
424 if( be == &backendDB[i] ) return i;
429 int backend_shutdown( Backend *be )
435 /* shutdown a specific backend database */
437 if ( be->bd_info->bi_nDB == 0 ) {
438 /* no database of this type, we never opened it */
442 if ( be->bd_info->bi_db_close ) {
443 be->bd_info->bi_db_close( be );
446 if( be->bd_info->bi_close ) {
447 be->bd_info->bi_close( be->bd_info );
453 /* close each backend database */
454 for( i = 0; i < nBackendDB; i++ ) {
455 if ( backendDB[i].bd_info->bi_db_close ) {
456 backendDB[i].bd_info->bi_db_close(
461 Debug( LDAP_DEBUG_ANY,
462 "backend_close: bi_db_close %s failed!\n",
463 backendDB[i].be_type, 0, 0 );
467 /* close each backend type */
468 for( i = 0; i < nBackendInfo; i++ ) {
469 if( backendInfo[i].bi_nDB == 0 ) {
470 /* no database of this type */
474 if( backendInfo[i].bi_close ) {
475 backendInfo[i].bi_close(
480 /* close frontend, if required */
481 if ( frontendDB->bd_info->bi_db_close ) {
482 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
484 Debug( LDAP_DEBUG_ANY,
485 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
493 int backend_destroy(void)
497 syncinfo_t *si_entry;
498 struct slap_csn_entry *csne;
500 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
502 /* destroy each backend database */
503 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
505 while ( !LDAP_STAILQ_EMPTY( &bd->be_syncinfo )) {
506 si_entry = LDAP_STAILQ_FIRST( &bd->be_syncinfo );
507 LDAP_STAILQ_REMOVE_HEAD( &bd->be_syncinfo, si_next );
508 syncinfo_free( si_entry );
511 if ( bd->be_pending_csn_list ) {
512 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
514 struct slap_csn_entry *tmp_csne = csne;
516 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
517 ch_free( csne->ce_csn->bv_val );
518 ch_free( csne->ce_csn );
519 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
524 if ( bd->bd_info->bi_db_destroy ) {
525 bd->bd_info->bi_db_destroy( bd );
527 ber_bvarray_free( bd->be_suffix );
528 ber_bvarray_free( bd->be_nsuffix );
529 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
530 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
531 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
532 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
533 acl_destroy( bd->be_acl, frontendDB->be_acl );
537 /* destroy each backend type */
538 for( i = 0; i < nBackendInfo; i++ ) {
539 if( backendInfo[i].bi_destroy ) {
540 backendInfo[i].bi_destroy(
546 if (backendInfo != binfo) {
549 #endif /* SLAPD_MODULES */
554 /* destroy frontend database */
556 if ( bd->bd_info->bi_db_destroy ) {
557 bd->bd_info->bi_db_destroy( bd );
559 ber_bvarray_free( bd->be_suffix );
560 ber_bvarray_free( bd->be_nsuffix );
561 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
562 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
563 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
564 acl_destroy( bd->be_acl, frontendDB->be_acl );
569 BackendInfo* backend_info(const char *type)
573 /* search for the backend type */
574 for( i = 0; i < nBackendInfo; i++ ) {
575 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
576 return &backendInfo[i];
589 BackendInfo *bi = backend_info(type);
593 fprintf( stderr, "Unrecognized database type (%s)\n", type );
599 backendDB = (BackendDB *) ch_realloc(
601 (nBackendDB + 1) * sizeof(Backend) );
603 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
605 /* did realloc move our table? if so, fix up dependent pointers */
606 if ( be != backendDB ) {
608 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
609 be->be_pcl_mutexp = &be->be_pcl_mutex;
613 be = &backends[nbackends++];
616 be->be_def_limit = frontendDB->be_def_limit;
617 be->be_dfltaccess = frontendDB->be_dfltaccess;
619 be->be_restrictops = frontendDB->be_restrictops;
620 be->be_requires = frontendDB->be_requires;
621 be->be_ssf_set = frontendDB->be_ssf_set;
623 be->be_context_csn.bv_len = 0;
624 be->be_context_csn.bv_val = NULL;
625 be->be_pcl_mutexp = &be->be_pcl_mutex;
626 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
628 LDAP_STAILQ_INIT( &be->be_syncinfo );
630 /* assign a default depth limit for alias deref */
631 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
634 rc = bi->bi_db_init( be );
638 fprintf( stderr, "database init failed (%s)\n", type );
652 for ( i = 0; i < nbackends; i++ ) {
653 if ( backends[i].bd_info->bi_db_close ) {
654 (*backends[i].bd_info->bi_db_close)( &backends[i] );
658 if ( frontendDB->bd_info->bi_db_close ) {
659 (*frontendDB->bd_info->bi_db_close)( frontendDB );
670 ber_len_t len, dnlen = dn->bv_len;
673 for ( i = 0; i < nbackends; i++ ) {
674 for ( j = 0; backends[i].be_nsuffix != NULL &&
675 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
677 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
683 len = backends[i].be_nsuffix[j].bv_len;
686 /* suffix is longer than DN */
691 * input DN is normalized, so the separator check
692 * need not look at escaping
694 if ( len && len < dnlen &&
695 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
700 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
701 &dn->bv_val[dnlen-len] ) == 0 )
706 if( manageDSAit && len == dnlen &&
707 !SLAP_GLUE_SUBORDINATE( be ) ) {
724 struct berval *bvsuffix )
729 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
732 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
741 be_isroot_dn( Backend *be, struct berval *ndn )
743 if ( !ndn->bv_len ) {
747 if ( !be->be_rootndn.bv_len ) {
751 return dn_match( &be->be_rootndn, ndn );
755 be_sync_update( Operation *op )
757 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
761 be_slurp_update( Operation *op )
763 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
764 be_isupdate_dn( op->o_bd, &op->o_ndn ));
768 be_shadow_update( Operation *op )
770 return ( SLAP_SHADOW( op->o_bd ) &&
771 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
775 be_isupdate_dn( Backend *be, struct berval *ndn )
777 if ( !ndn->bv_len ) return( 0 );
779 if ( !be->be_update_ndn.bv_len ) return( 0 );
781 return dn_match( &be->be_update_ndn, ndn );
785 be_root_dn( Backend *be )
787 return &be->be_rootdn;
791 be_isroot( Operation *op )
793 return be_isroot_dn( op->o_bd, &op->o_ndn );
797 be_isroot_pw( Operation *op )
801 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
805 if( op->o_bd->be_rootpw.bv_len == 0 ) {
809 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
810 ldap_pvt_thread_mutex_lock( &passwd_mutex );
812 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
816 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
818 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
820 lutil_passwd_sasl_conn = NULL;
822 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
834 if ( op->o_bd->be_release ) {
835 /* free and release entry from backend */
836 return op->o_bd->be_release( op, e, rw );
845 backend_unbind( Operation *op, SlapReply *rs )
849 for ( i = 0; i < nbackends; i++ ) {
850 #if defined( LDAP_SLAPI )
853 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
854 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
855 rc = slapi_int_call_plugins( &backends[i],
856 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
859 * A preoperation plugin failure will abort the
862 Debug(LDAP_DEBUG_TRACE,
863 "do_bind: Unbind preoperation plugin failed\n",
868 #endif /* defined( LDAP_SLAPI ) */
870 if ( backends[i].be_unbind ) {
871 op->o_bd = &backends[i];
872 (*backends[i].be_unbind)( op, rs );
875 #if defined( LDAP_SLAPI )
876 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
877 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
879 Debug(LDAP_DEBUG_TRACE,
880 "do_unbind: Unbind postoperation plugins failed\n",
883 #endif /* defined( LDAP_SLAPI ) */
890 backend_connection_init(
895 for ( i = 0; i < nbackends; i++ ) {
896 if ( backends[i].be_connection_init ) {
897 (*backends[i].be_connection_init)( &backends[i], conn);
905 backend_connection_destroy(
910 for ( i = 0; i < nbackends; i++ ) {
911 if ( backends[i].be_connection_destroy ) {
912 (*backends[i].be_connection_destroy)( &backends[i], conn);
920 backend_check_controls(
924 LDAPControl **ctrls = op->o_ctrls;
925 rs->sr_err = LDAP_SUCCESS;
928 for( ; *ctrls != NULL ; ctrls++ ) {
929 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
930 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
932 /* FIXME: standards compliance issue
934 * Per RFC 2251 (and LDAPBIS discussions), if the control
935 * is recognized and appropriate for the operation (which
936 * we've already verified), then the server should make
937 * use of the control when performing the operation
938 * (without regard to criticality). This code is incorrect
940 * 1) a service error (e.g., unwillingToPerform) should be
941 * returned where a particular backend cannot service the
943 * 2) this error should be returned irregardless of the
944 * criticality of the control.
946 rs->sr_text = "control unavailable in context";
947 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
957 backend_check_restrictions(
960 struct berval *opdata )
962 slap_mask_t restrictops;
963 slap_mask_t requires;
965 slap_mask_t exopflag = 0;
972 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
976 restrictops = op->o_bd->be_restrictops;
977 requires = op->o_bd->be_requires;
978 ssf = &op->o_bd->be_ssf_set;
981 restrictops = frontendDB->be_restrictops;
982 requires = frontendDB->be_requires;
983 ssf = &frontendDB->be_ssf_set;
986 switch( op->o_tag ) {
988 opflag = SLAP_RESTRICT_OP_ADD;
992 opflag = SLAP_RESTRICT_OP_BIND;
995 case LDAP_REQ_COMPARE:
996 opflag = SLAP_RESTRICT_OP_COMPARE;
998 case LDAP_REQ_DELETE:
1000 opflag = SLAP_RESTRICT_OP_DELETE;
1002 case LDAP_REQ_EXTENDED:
1003 opflag = SLAP_RESTRICT_OP_EXTENDED;
1006 /* treat unspecified as a modify */
1007 opflag = SLAP_RESTRICT_OP_MODIFY;
1012 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1015 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1019 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1020 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1024 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1025 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1029 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1030 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1035 /* treat everything else as a modify */
1036 opflag = SLAP_RESTRICT_OP_MODIFY;
1040 case LDAP_REQ_MODIFY:
1042 opflag = SLAP_RESTRICT_OP_MODIFY;
1044 case LDAP_REQ_RENAME:
1046 opflag = SLAP_RESTRICT_OP_RENAME;
1048 case LDAP_REQ_SEARCH:
1049 opflag = SLAP_RESTRICT_OP_SEARCH;
1051 case LDAP_REQ_UNBIND:
1056 rs->sr_text = "restrict operations internal error";
1057 rs->sr_err = LDAP_OTHER;
1062 /* these checks don't apply to StartTLS */
1064 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1065 if( op->o_transport_ssf < ssf->sss_transport ) {
1066 rs->sr_text = op->o_transport_ssf
1067 ? "stronger transport confidentiality required"
1068 : "transport confidentiality required";
1072 if( op->o_tls_ssf < ssf->sss_tls ) {
1073 rs->sr_text = op->o_tls_ssf
1074 ? "stronger TLS confidentiality required"
1075 : "TLS confidentiality required";
1080 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1081 /* simple bind specific check */
1082 if( op->o_ssf < ssf->sss_simple_bind ) {
1083 rs->sr_text = op->o_ssf
1084 ? "stronger confidentiality required"
1085 : "confidentiality required";
1090 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1091 /* these checks don't apply to SASL bind */
1093 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1094 rs->sr_text = op->o_sasl_ssf
1095 ? "stronger SASL confidentiality required"
1096 : "SASL confidentiality required";
1100 if( op->o_ssf < ssf->sss_ssf ) {
1101 rs->sr_text = op->o_ssf
1102 ? "stronger confidentiality required"
1103 : "confidentiality required";
1109 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1110 rs->sr_text = op->o_transport_ssf
1111 ? "stronger transport confidentiality required for update"
1112 : "transport confidentiality required for update";
1116 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1117 rs->sr_text = op->o_tls_ssf
1118 ? "stronger TLS confidentiality required for update"
1119 : "TLS confidentiality required for update";
1123 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1124 rs->sr_text = op->o_sasl_ssf
1125 ? "stronger SASL confidentiality required for update"
1126 : "SASL confidentiality required for update";
1130 if( op->o_ssf < ssf->sss_update_ssf ) {
1131 rs->sr_text = op->o_ssf
1132 ? "stronger confidentiality required for update"
1133 : "confidentiality required for update";
1137 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1138 op->o_ndn.bv_len == 0 )
1140 rs->sr_text = "modifications require authentication";
1141 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1145 #ifdef SLAP_X_LISTENER_MOD
1146 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1147 /* no "w" mode means readonly */
1148 rs->sr_text = "modifications not allowed on this listener";
1149 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1152 #endif /* SLAP_X_LISTENER_MOD */
1157 /* these checks don't apply to Bind, StartTLS, or Unbind */
1159 if( requires & SLAP_REQUIRE_STRONG ) {
1160 /* should check mechanism */
1161 if( ( op->o_transport_ssf < ssf->sss_transport
1162 && op->o_authtype == LDAP_AUTH_SIMPLE )
1163 || op->o_dn.bv_len == 0 )
1165 rs->sr_text = "strong(er) authentication required";
1166 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1171 if( requires & SLAP_REQUIRE_SASL ) {
1172 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1173 rs->sr_text = "SASL authentication required";
1174 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1179 if( requires & SLAP_REQUIRE_AUTHC ) {
1180 if( op->o_dn.bv_len == 0 ) {
1181 rs->sr_text = "authentication required";
1182 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1187 if( requires & SLAP_REQUIRE_BIND ) {
1189 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1190 version = op->o_conn->c_protocol;
1191 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1194 /* no bind has occurred */
1195 rs->sr_text = "BIND required";
1196 rs->sr_err = LDAP_OPERATIONS_ERROR;
1201 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1202 if( op->o_protocol < LDAP_VERSION3 ) {
1203 /* no bind has occurred */
1204 rs->sr_text = "operation restricted to LDAPv3 clients";
1205 rs->sr_err = LDAP_OPERATIONS_ERROR;
1210 #ifdef SLAP_X_LISTENER_MOD
1211 if ( !starttls && op->o_dn.bv_len == 0 ) {
1212 if ( op->o_conn->c_listener &&
1213 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1215 /* no "x" mode means bind required */
1216 rs->sr_text = "bind required on this listener";
1217 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1222 if ( !starttls && !updateop ) {
1223 if ( op->o_conn->c_listener &&
1224 !( op->o_conn->c_listener->sl_perms &
1225 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1227 /* no "r" mode means no read */
1228 rs->sr_text = "read not allowed on this listener";
1229 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1233 #endif /* SLAP_X_LISTENER_MOD */
1237 if( ( restrictops & opflag )
1238 || ( exopflag && ( restrictops & exopflag ) ) ) {
1239 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1240 rs->sr_text = "read operations restricted";
1241 } else if ( restrictops & exopflag ) {
1242 rs->sr_text = "extended operation restricted";
1244 rs->sr_text = "operation restricted";
1246 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1250 rs->sr_err = LDAP_SUCCESS;
1254 int backend_check_referrals( Operation *op, SlapReply *rs )
1256 rs->sr_err = LDAP_SUCCESS;
1258 if( op->o_bd->be_chk_referrals ) {
1259 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1261 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1262 send_ldap_result( op, rs );
1274 AttributeDescription *at,
1282 if (op->o_bd == NULL) {
1283 rc = LDAP_NO_SUCH_OBJECT;
1284 } else if ( op->o_bd->be_fetch ) {
1285 rc = ( op->o_bd->be_fetch )( op, ndn,
1288 rc = LDAP_UNWILLING_TO_PERFORM;
1297 struct berval *gr_ndn,
1298 struct berval *op_ndn,
1299 ObjectClass *group_oc,
1300 AttributeDescription *group_at )
1306 Backend *be = op->o_bd;
1308 if ( op->o_abandon ) return SLAPD_ABANDON;
1310 op->o_bd = select_backend( gr_ndn, 0, 0 );
1312 for (g = op->o_groups; g; g=g->ga_next) {
1313 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1314 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1316 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1325 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1329 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1333 if ( op->o_pb != NULL ) {
1334 init_group_pblock( op, target, e, op_ndn, group_at );
1336 rc = call_group_preop_plugins( op );
1337 if ( rc == LDAP_SUCCESS ) {
1341 #endif /* LDAP_SLAPI */
1343 a = attr_find( e->e_attrs, group_at );
1345 /* If the attribute is a subtype of labeledURI, treat this as
1346 * a dynamic group ala groupOfURLs
1348 if (is_at_subtype( group_at->ad_type,
1349 slap_schema.si_ad_labeledURI->ad_type ) )
1353 struct berval bv, nbase;
1356 Backend *b2 = op->o_bd;
1357 void *o_private = op->o_private;
1359 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1362 /* back-bdb stored lockinfo here, we saved it
1363 * above. Clear it out so that a new lock can be used.
1365 op->o_private = NULL;
1366 op->o_bd = select_backend( op_ndn, 0, 0 );
1367 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1372 for (i=0; a->a_vals[i].bv_val; i++) {
1373 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1378 nbase.bv_val = NULL;
1379 /* host part must be empty */
1380 /* attrs and extensions parts must be empty */
1381 if (( ludp->lud_host && *ludp->lud_host ) ||
1382 ludp->lud_attrs || ludp->lud_exts )
1386 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1387 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1388 op->o_tmpmemctx ) != LDAP_SUCCESS )
1392 switch(ludp->lud_scope) {
1393 case LDAP_SCOPE_BASE:
1394 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1396 case LDAP_SCOPE_ONELEVEL:
1397 dnParent(op_ndn, &bv );
1398 if ( !dn_match( &nbase, &bv )) goto loopit;
1400 case LDAP_SCOPE_SUBTREE:
1401 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1403 #ifdef LDAP_SCOPE_SUBORDINATE
1404 case LDAP_SCOPE_SUBORDINATE:
1405 if ( dn_match( &nbase, op_ndn ) ||
1406 !dnIsSuffix( op_ndn, &nbase ) )
1412 filter = str2filter_x( op, ludp->lud_filter );
1414 if ( test_filter( NULL, user, filter ) ==
1419 filter_free_x( op, filter );
1422 ldap_free_urldesc( ludp );
1423 if ( nbase.bv_val ) {
1424 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1426 if ( rc == 0 ) break;
1428 if ( user != target ) {
1429 be_entry_release_r( op, user );
1430 /* restore previous lockinfo, if any */
1431 op->o_private = o_private;
1436 rc = value_find_ex( group_at,
1437 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1438 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1439 a->a_nvals, op_ndn, op->o_tmpmemctx );
1442 rc = LDAP_NO_SUCH_ATTRIBUTE;
1445 be_entry_release_r( op, e );
1448 rc = LDAP_NO_SUCH_OBJECT;
1452 if ( op->o_pb ) call_group_postop_plugins( op );
1453 #endif /* LDAP_SLAPI */
1455 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1456 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1458 g->ga_be = op->o_bd;
1459 g->ga_oc = group_oc;
1460 g->ga_at = group_at;
1462 g->ga_len = gr_ndn->bv_len;
1463 strcpy(g->ga_ndn, gr_ndn->bv_val);
1464 g->ga_next = op->o_groups;
1473 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1477 BerVarray *vals = (BerVarray *)c->cac_private;
1478 Operation *op = NULL;
1481 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1486 if ( op->o_conn && access_allowed( op,
1487 e, a->a_desc, NULL, ACL_AUTH,
1488 &c->cac_acl_state ) == 0 ) {
1492 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1494 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1496 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1497 if ( op->o_conn && access_allowed( op,
1500 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1504 &a->a_nvals[i], op->o_tmpmemctx );
1505 if (v[j].bv_val ) j++;
1509 op->o_tmpfree( v, op->o_tmpmemctx );
1521 #endif /* LDAP_SLAPI */
1528 AttributeDescription *entry_at,
1530 slap_access_t access )
1534 int i, j, rc = LDAP_SUCCESS;
1535 AccessControlState acl_state = ACL_STATE_INIT;
1536 Backend *be = op->o_bd;
1538 op->o_bd = select_backend( edn, 0, 0 );
1540 if ( target && dn_match( &target->e_nname, edn ) ) {
1543 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1547 a = attr_find( e->e_attrs, entry_at );
1551 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1552 e, entry_at, NULL, access,
1553 &acl_state ) == 0 ) {
1554 rc = LDAP_INSUFFICIENT_ACCESS;
1558 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1560 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1562 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1563 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1566 access, &acl_state ) == 0 ) {
1570 &a->a_nvals[i], op->o_tmpmemctx );
1571 if (v[j].bv_val ) j++;
1574 op->o_tmpfree( v, op->o_tmpmemctx );
1576 rc = LDAP_INSUFFICIENT_ACCESS;
1585 else if ( op->o_pb ) {
1586 /* try any computed attributes */
1587 computed_attr_context ctx;
1588 AttributeName aname;
1590 slapi_int_pblock_set_operation( op->o_pb, op );
1592 ctx.cac_pb = op->o_pb;
1593 ctx.cac_attrs = NULL;
1594 ctx.cac_userattrs = 0;
1595 ctx.cac_opattrs = 0;
1596 ctx.cac_acl_state = acl_state;
1597 ctx.cac_private = (void *)vals;
1599 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1600 rc = LDAP_INSUFFICIENT_ACCESS;
1604 #endif /* LDAP_SLAPI */
1605 freeit: if (e != target ) {
1606 be_entry_release_r( op, e );
1614 int backend_operational(
1621 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1622 /* just count them */ ;
1625 * If operational attributes (allegedly) are required,
1626 * and the backend supports specific operational attributes,
1627 * add them to the attribute list
1629 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1630 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1632 *ap = slap_operational_entryDN( rs->sr_entry );
1633 ap = &(*ap)->a_next;
1636 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1637 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1639 *ap = slap_operational_subschemaSubentry( op->o_bd );
1640 ap = &(*ap)->a_next;
1643 if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1644 op->o_bd && op->o_bd->be_operational != NULL )
1648 a = rs->sr_operational_attrs;
1649 rs->sr_operational_attrs = NULL;
1650 rc = op->o_bd->be_operational( op, rs );
1651 *ap = rs->sr_operational_attrs;
1653 rs->sr_operational_attrs = a;
1656 for ( ; *ap; ap = &(*ap)->a_next )
1657 /* just count them */ ;
1664 static void init_group_pblock( Operation *op, Entry *target,
1665 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1667 slapi_int_pblock_set_operation( op->o_pb, op );
1669 slapi_pblock_set( op->o_pb,
1670 SLAPI_X_GROUP_ENTRY, (void *)e );
1671 slapi_pblock_set( op->o_pb,
1672 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1673 slapi_pblock_set( op->o_pb,
1674 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1675 slapi_pblock_set( op->o_pb,
1676 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1679 static int call_group_preop_plugins( Operation *op )
1683 rc = slapi_int_call_plugins( op->o_bd,
1684 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1686 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1687 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1689 rc = LDAP_NO_SUCH_ATTRIBUTE;
1698 static void call_group_postop_plugins( Operation *op )
1700 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1702 #endif /* LDAP_SLAPI */