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"
47 * If a module is configured as dynamic, its header should not
48 * get included into slapd. While this is a general rule and does
49 * not have much of an effect in UNIX, this rule should be adhered
50 * to for Windows, where dynamic object code should not be implicitly
51 * imported into slapd without appropriate __declspec(dllimport) directives.
54 #if SLAPD_BDB == SLAPD_MOD_STATIC
55 #include "back-bdb/external.h"
57 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
58 #include "back-dnssrv/external.h"
60 #if SLAPD_HDB == SLAPD_MOD_STATIC
61 #include "back-hdb/external.h"
63 #if SLAPD_LDAP == SLAPD_MOD_STATIC
64 #include "back-ldap/external.h"
66 #if SLAPD_LDBM == SLAPD_MOD_STATIC
67 #include "back-ldbm/external.h"
69 #if SLAPD_META == SLAPD_MOD_STATIC
70 #include "back-meta/external.h"
72 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
73 #include "back-monitor/external.h"
75 #if SLAPD_NULL == SLAPD_MOD_STATIC
76 #include "back-null/external.h"
78 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
79 #include "back-passwd/external.h"
81 #if SLAPD_PERL == SLAPD_MOD_STATIC
82 #include "back-perl/external.h"
84 #if SLAPD_RELAY == SLAPD_MOD_STATIC
85 #include "back-relay/external.h"
87 #if SLAPD_SHELL == SLAPD_MOD_STATIC
88 #include "back-shell/external.h"
90 #if SLAPD_TCL == SLAPD_MOD_STATIC
91 #include "back-tcl/external.h"
93 #if SLAPD_SQL == SLAPD_MOD_STATIC
94 #include "back-sql/external.h"
96 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
97 #include "private/external.h"
100 static BackendInfo binfo[] = {
101 #if SLAPD_BDB == SLAPD_MOD_STATIC
102 {"bdb", bdb_initialize},
104 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
105 {"dnssrv", dnssrv_back_initialize},
107 #if SLAPD_HDB == SLAPD_MOD_STATIC
108 {"hdb", hdb_initialize},
110 #if SLAPD_LDAP == SLAPD_MOD_STATIC
111 {"ldap", ldap_back_initialize},
113 #if SLAPD_LDBM == SLAPD_MOD_STATIC
114 {"ldbm", ldbm_back_initialize},
116 #if SLAPD_META == SLAPD_MOD_STATIC
117 {"meta", meta_back_initialize},
119 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
120 {"monitor", monitor_back_initialize},
122 #if SLAPD_NULL == SLAPD_MOD_STATIC
123 {"null", null_back_initialize},
125 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
126 {"passwd", passwd_back_initialize},
128 #if SLAPD_PERL == SLAPD_MOD_STATIC
129 {"perl", perl_back_initialize},
131 #if SLAPD_RELAY == SLAPD_MOD_STATIC
132 {"relay", relay_back_initialize},
134 #if SLAPD_SHELL == SLAPD_MOD_STATIC
135 {"shell", shell_back_initialize},
137 #if SLAPD_TCL == SLAPD_MOD_STATIC
138 {"tcl", tcl_back_initialize},
140 #if SLAPD_SQL == SLAPD_MOD_STATIC
141 {"sql", sql_back_initialize},
143 /* for any private backend */
144 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
145 {"private", private_back_initialize},
150 int nBackendInfo = 0;
151 BackendInfo *backendInfo = NULL;
154 BackendDB *backendDB = NULL;
156 ldap_pvt_thread_pool_t syncrepl_pool;
157 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
159 int backend_init(void)
163 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
165 if((nBackendInfo != 0) || (backendInfo != NULL)) {
166 /* already initialized */
168 LDAP_LOG( BACKEND, ERR,
169 "backend_init: backend already initialized\n", 0, 0, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "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] );
187 LDAP_LOG( BACKEND, INFO,
188 "backend_init: initialized for type \"%s\"\n",
189 binfo[nBackendInfo].bi_type, 0, 0 );
191 Debug( LDAP_DEBUG_ANY,
192 "backend_init: initialized for type \"%s\"\n",
193 binfo[nBackendInfo].bi_type, 0, 0 );
195 /* destroy those we've already inited */
200 if ( binfo[nBackendInfo].bi_destroy ) {
201 binfo[nBackendInfo].bi_destroy(
202 &binfo[nBackendInfo] );
209 if ( nBackendInfo > 0) {
219 LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
221 Debug( LDAP_DEBUG_ANY,
222 "backend_init: failed\n",
227 #endif /* SLAPD_MODULES */
230 int backend_add(BackendInfo *aBackendInfo)
234 if ( aBackendInfo->bi_init == NULL ) {
236 LDAP_LOG( BACKEND, ERR, "backend_add: "
237 "backend type \"%s\" does not have the (mandatory)init function\n",
238 aBackendInfo->bi_type, 0, 0 );
240 Debug( LDAP_DEBUG_ANY, "backend_add: "
241 "backend type \"%s\" does not have the (mandatory)init function\n",
242 aBackendInfo->bi_type, 0, 0 );
247 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
249 LDAP_LOG( BACKEND, ERR,
250 "backend_add: initialization for type \"%s\" failed\n",
251 aBackendInfo->bi_type, 0, 0 );
253 Debug( LDAP_DEBUG_ANY,
254 "backend_add: initialization for type \"%s\" failed\n",
255 aBackendInfo->bi_type, 0, 0 );
260 /* now add the backend type to the Backend Info List */
262 BackendInfo *newBackendInfo = 0;
264 /* if backendInfo == binfo no deallocation of old backendInfo */
265 if (backendInfo == binfo) {
266 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
267 AC_MEMCPY(newBackendInfo, backendInfo,
268 sizeof(BackendInfo) * nBackendInfo);
270 newBackendInfo = ch_realloc(backendInfo,
271 sizeof(BackendInfo) * (nBackendInfo + 1));
274 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
275 sizeof(BackendInfo));
276 backendInfo = newBackendInfo;
282 int backend_startup(Backend *be)
287 if( ! ( nBackendDB > 0 ) ) {
290 LDAP_LOG( BACKEND, INFO,
291 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
293 Debug( LDAP_DEBUG_ANY,
294 "backend_startup: %d databases to startup.\n",
301 /* startup a specific backend database */
302 be->be_pending_csn_list = (struct be_pcl *)
303 ch_calloc( 1, sizeof( struct be_pcl ));
304 build_new_dn( &be->be_context_csn, be->be_nsuffix,
305 &slap_ldapsync_cn_bv, NULL );
307 LDAP_TAILQ_INIT( be->be_pending_csn_list );
310 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
311 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
314 Debug( LDAP_DEBUG_TRACE,
315 "backend_startup: starting \"%s\"\n",
316 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
320 if ( be->bd_info->bi_open ) {
321 rc = be->bd_info->bi_open( be->bd_info );
324 LDAP_LOG( BACKEND, CRIT,
325 "backend_startup: bi_open failed!\n", 0, 0, 0 );
327 Debug( LDAP_DEBUG_ANY,
328 "backend_startup: bi_open failed!\n",
336 if ( be->bd_info->bi_db_open ) {
337 rc = be->bd_info->bi_db_open( be );
340 LDAP_LOG( BACKEND, CRIT,
341 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
343 Debug( LDAP_DEBUG_ANY,
344 "backend_startup: bi_db_open failed! (%d)\n",
354 /* open each backend type */
355 for( i = 0; i < nBackendInfo; i++ ) {
356 if( backendInfo[i].bi_nDB == 0) {
357 /* no database of this type, don't open */
361 if( backendInfo[i].bi_open ) {
362 rc = backendInfo[i].bi_open(
366 LDAP_LOG( BACKEND, CRIT,
367 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
369 Debug( LDAP_DEBUG_ANY,
370 "backend_startup: bi_open %d failed!\n",
378 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
379 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
380 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
382 /* open each backend database */
383 for( i = 0; i < nBackendDB; i++ ) {
384 /* append global access controls */
385 acl_append( &backendDB[i].be_acl, global_acl );
387 backendDB[i].be_pending_csn_list = (struct be_pcl *)
388 ch_calloc( 1, sizeof( struct be_pcl ));
389 LDAP_TAILQ_INIT( backendDB[i].be_pending_csn_list );
391 if ( backendDB[i].be_suffix == NULL ) {
393 LDAP_LOG( BACKEND, CRIT,
394 "backend_startup: warning, database %d (%s) "
396 i, backendDB[i].bd_info->bi_type, 0 );
398 Debug( LDAP_DEBUG_ANY,
399 "backend_startup: warning, database %d (%s) "
401 i, backendDB[i].bd_info->bi_type, 0 );
404 build_new_dn( &be->be_context_csn, be->be_nsuffix,
405 &slap_ldapsync_cn_bv, NULL );
407 if ( backendDB[i].bd_info->bi_db_open ) {
408 rc = backendDB[i].bd_info->bi_db_open(
412 LDAP_LOG( BACKEND, CRIT,
413 "backend_startup: bi_db_open(%d) failed! (%d)\n",
416 Debug( LDAP_DEBUG_ANY,
417 "backend_startup: bi_db_open(%d) failed! (%d)\n",
424 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
427 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
428 backendDB[i].be_modify && backendDB[i].be_delete )) {
430 LDAP_LOG( BACKEND, CRIT,
431 "backend_startup: database(%d) does not support "
432 "operations required for syncrepl", i, 0, 0 );
434 Debug( LDAP_DEBUG_ANY,
435 "backend_startup: database(%d) does not support "
436 "operations required for syncrepl", i, 0, 0 );
441 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
442 si->si_be = &backendDB[i];
444 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
445 ldap_pvt_runqueue_insert( &syncrepl_rq,
446 si->si_interval, do_syncrepl, (void *) si );
447 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
455 int backend_num( Backend *be )
459 if( be == NULL ) return -1;
461 for( i = 0; i < nBackendDB; i++ ) {
462 if( be == &backendDB[i] ) return i;
467 int backend_shutdown( Backend *be )
473 /* shutdown a specific backend database */
475 if ( be->bd_info->bi_nDB == 0 ) {
476 /* no database of this type, we never opened it */
480 if ( be->bd_info->bi_db_close ) {
481 be->bd_info->bi_db_close( be );
484 if( be->bd_info->bi_close ) {
485 be->bd_info->bi_close( be->bd_info );
491 /* close each backend database */
492 for( i = 0; i < nBackendDB; i++ ) {
493 if ( backendDB[i].bd_info->bi_db_close ) {
494 backendDB[i].bd_info->bi_db_close(
500 LDAP_LOG( BACKEND, NOTICE,
501 "backend_shutdown: bi_close %s failed!\n",
502 backendDB[i].be_type, 0, 0 );
504 Debug( LDAP_DEBUG_ANY,
505 "backend_close: bi_close %s failed!\n",
506 backendDB[i].be_type, 0, 0 );
511 /* close each backend type */
512 for( i = 0; i < nBackendInfo; i++ ) {
513 if( backendInfo[i].bi_nDB == 0 ) {
514 /* no database of this type */
518 if( backendInfo[i].bi_close ) {
519 backendInfo[i].bi_close(
527 int backend_destroy(void)
532 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
534 /* destroy each backend database */
535 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
536 if ( bd->bd_info->bi_db_destroy ) {
537 bd->bd_info->bi_db_destroy( bd );
539 ber_bvarray_free( bd->be_suffix );
540 ber_bvarray_free( bd->be_nsuffix );
541 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
542 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
543 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
544 acl_destroy( bd->be_acl, global_acl );
548 /* destroy each backend type */
549 for( i = 0; i < nBackendInfo; i++ ) {
550 if( backendInfo[i].bi_destroy ) {
551 backendInfo[i].bi_destroy(
557 if (backendInfo != binfo) {
560 #endif /* SLAPD_MODULES */
568 BackendInfo* backend_info(const char *type)
572 /* search for the backend type */
573 for( i = 0; i < nBackendInfo; i++ ) {
574 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
575 return &backendInfo[i];
588 BackendInfo *bi = backend_info(type);
592 fprintf( stderr, "Unrecognized database type (%s)\n", type );
596 backendDB = (BackendDB *) ch_realloc(
598 (nBackendDB + 1) * sizeof(Backend) );
600 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
602 be = &backends[nbackends++];
605 be->be_def_limit = deflimit;
606 be->be_dfltaccess = global_default_access;
608 be->be_restrictops = global_restrictops;
609 be->be_requires = global_requires;
610 be->be_ssf_set = global_ssf_set;
612 be->be_context_csn.bv_len = 0;
613 be->be_context_csn.bv_val = NULL;
614 ldap_pvt_thread_mutex_init( &be->be_pcl_mutex );
615 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
617 LDAP_STAILQ_INIT( &be->be_syncinfo );
619 /* assign a default depth limit for alias deref */
620 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
623 rc = bi->bi_db_init( be );
627 fprintf( stderr, "database init failed (%s)\n", type );
641 for ( i = 0; i < nbackends; i++ ) {
642 if ( backends[i].bd_info->bi_db_close ) {
643 (*backends[i].bd_info->bi_db_close)( &backends[i] );
655 ber_len_t len, dnlen = dn->bv_len;
658 for ( i = 0; i < nbackends; i++ ) {
659 for ( j = 0; backends[i].be_nsuffix != NULL &&
660 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
662 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
668 len = backends[i].be_nsuffix[j].bv_len;
671 /* suffix is longer than DN */
676 * input DN is normalized, so the separator check
677 * need not look at escaping
679 if ( len && len < dnlen &&
680 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
685 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
686 &dn->bv_val[dnlen-len] ) == 0 )
691 if( manageDSAit && len == dnlen &&
692 !SLAP_GLUE_SUBORDINATE( be ) ) {
709 struct berval *bvsuffix )
714 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
717 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
726 be_isroot_dn( Backend *be, struct berval *ndn )
728 if ( !ndn->bv_len ) {
732 if ( !be->be_rootndn.bv_len ) {
736 return dn_match( &be->be_rootndn, ndn );
740 be_sync_update( Operation *op )
742 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
746 be_slurp_update( Operation *op )
748 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
749 be_isupdate_dn( op->o_bd, &op->o_ndn ));
753 be_shadow_update( Operation *op )
755 return ( SLAP_SHADOW( op->o_bd ) &&
756 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
760 be_isupdate_dn( Backend *be, struct berval *ndn )
762 if ( !ndn->bv_len ) return( 0 );
764 if ( !be->be_update_ndn.bv_len ) return( 0 );
766 return dn_match( &be->be_update_ndn, ndn );
770 be_root_dn( Backend *be )
772 return &be->be_rootdn;
776 be_isroot( Operation *op )
778 return be_isroot_dn( op->o_bd, &op->o_ndn );
782 be_isroot_pw( Operation *op )
787 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
791 if( op->o_bd->be_rootpw.bv_len == 0 ) {
795 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
796 ldap_pvt_thread_mutex_lock( &passwd_mutex );
798 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
802 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
804 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
806 lutil_passwd_sasl_conn = NULL;
808 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
820 if ( op->o_bd->be_release ) {
821 /* free and release entry from backend */
822 return op->o_bd->be_release( op, e, rw );
831 backend_unbind( Operation *op, SlapReply *rs )
835 for ( i = 0; i < nbackends; i++ ) {
836 #if defined( LDAP_SLAPI )
839 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
840 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
841 rc = slapi_int_call_plugins( &backends[i],
842 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
845 * A preoperation plugin failure will abort the
849 LDAP_LOG( OPERATION, INFO,
850 "do_bind: Unbind preoperation plugin failed\n",
853 Debug(LDAP_DEBUG_TRACE,
854 "do_bind: Unbind preoperation plugin failed\n",
860 #endif /* defined( LDAP_SLAPI ) */
862 if ( backends[i].be_unbind ) {
863 op->o_bd = &backends[i];
864 (*backends[i].be_unbind)( op, rs );
867 #if defined( LDAP_SLAPI )
868 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
869 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
872 LDAP_LOG( OPERATION, INFO,
873 "do_unbind: Unbind postoperation plugins failed\n",
876 Debug(LDAP_DEBUG_TRACE,
877 "do_unbind: Unbind postoperation plugins failed\n",
881 #endif /* defined( LDAP_SLAPI ) */
888 backend_connection_init(
893 for ( i = 0; i < nbackends; i++ ) {
894 if ( backends[i].be_connection_init ) {
895 (*backends[i].be_connection_init)( &backends[i], conn);
903 backend_connection_destroy(
908 for ( i = 0; i < nbackends; i++ ) {
909 if ( backends[i].be_connection_destroy ) {
910 (*backends[i].be_connection_destroy)( &backends[i], conn);
918 backend_check_controls(
922 LDAPControl **ctrls = op->o_ctrls;
923 rs->sr_err = LDAP_SUCCESS;
926 for( ; *ctrls != NULL ; ctrls++ ) {
927 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
928 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
930 rs->sr_text = "control unavailable in context";
931 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
941 backend_check_restrictions(
944 struct berval *opdata )
946 slap_mask_t restrictops;
947 slap_mask_t requires;
949 slap_mask_t exopflag = 0;
956 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
960 restrictops = op->o_bd->be_restrictops;
961 requires = op->o_bd->be_requires;
962 ssf = &op->o_bd->be_ssf_set;
965 restrictops = global_restrictops;
966 requires = global_requires;
967 ssf = &global_ssf_set;
970 switch( op->o_tag ) {
972 opflag = SLAP_RESTRICT_OP_ADD;
976 opflag = SLAP_RESTRICT_OP_BIND;
979 case LDAP_REQ_COMPARE:
980 opflag = SLAP_RESTRICT_OP_COMPARE;
982 case LDAP_REQ_DELETE:
984 opflag = SLAP_RESTRICT_OP_DELETE;
986 case LDAP_REQ_EXTENDED:
987 opflag = SLAP_RESTRICT_OP_EXTENDED;
990 /* treat unspecified as a modify */
991 opflag = SLAP_RESTRICT_OP_MODIFY;
996 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
999 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1003 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1004 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1008 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1009 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1013 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1014 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1019 /* treat everything else as a modify */
1020 opflag = SLAP_RESTRICT_OP_MODIFY;
1024 case LDAP_REQ_MODIFY:
1026 opflag = SLAP_RESTRICT_OP_MODIFY;
1028 case LDAP_REQ_RENAME:
1030 opflag = SLAP_RESTRICT_OP_RENAME;
1032 case LDAP_REQ_SEARCH:
1033 opflag = SLAP_RESTRICT_OP_SEARCH;
1035 case LDAP_REQ_UNBIND:
1040 rs->sr_text = "restrict operations internal error";
1041 rs->sr_err = LDAP_OTHER;
1046 /* these checks don't apply to StartTLS */
1048 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1049 if( op->o_transport_ssf < ssf->sss_transport ) {
1050 rs->sr_text = op->o_transport_ssf
1051 ? "stronger transport confidentiality required"
1052 : "transport confidentiality required";
1056 if( op->o_tls_ssf < ssf->sss_tls ) {
1057 rs->sr_text = op->o_tls_ssf
1058 ? "stronger TLS confidentiality required"
1059 : "TLS confidentiality required";
1064 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1065 /* simple bind specific check */
1066 if( op->o_ssf < ssf->sss_simple_bind ) {
1067 rs->sr_text = op->o_ssf
1068 ? "stronger confidentiality required"
1069 : "confidentiality required";
1074 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1075 /* these checks don't apply to SASL bind */
1077 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1078 rs->sr_text = op->o_sasl_ssf
1079 ? "stronger SASL confidentiality required"
1080 : "SASL confidentiality required";
1084 if( op->o_ssf < ssf->sss_ssf ) {
1085 rs->sr_text = op->o_ssf
1086 ? "stronger confidentiality required"
1087 : "confidentiality required";
1093 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1094 rs->sr_text = op->o_transport_ssf
1095 ? "stronger transport confidentiality required for update"
1096 : "transport confidentiality required for update";
1100 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1101 rs->sr_text = op->o_tls_ssf
1102 ? "stronger TLS confidentiality required for update"
1103 : "TLS confidentiality required for update";
1107 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1108 rs->sr_text = op->o_sasl_ssf
1109 ? "stronger SASL confidentiality required for update"
1110 : "SASL confidentiality required for update";
1114 if( op->o_ssf < ssf->sss_update_ssf ) {
1115 rs->sr_text = op->o_ssf
1116 ? "stronger confidentiality required for update"
1117 : "confidentiality required for update";
1121 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1122 op->o_ndn.bv_len == 0 )
1124 rs->sr_text = "modifications require authentication";
1125 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1129 #ifdef SLAP_X_LISTENER_MOD
1130 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1131 /* no "w" mode means readonly */
1132 rs->sr_text = "modifications not allowed on this listener";
1133 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1136 #endif /* SLAP_X_LISTENER_MOD */
1141 /* these checks don't apply to Bind, StartTLS, or Unbind */
1143 if( requires & SLAP_REQUIRE_STRONG ) {
1144 /* should check mechanism */
1145 if( ( op->o_transport_ssf < ssf->sss_transport
1146 && op->o_authtype == LDAP_AUTH_SIMPLE )
1147 || op->o_dn.bv_len == 0 )
1149 rs->sr_text = "strong(er) authentication required";
1150 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1155 if( requires & SLAP_REQUIRE_SASL ) {
1156 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1157 rs->sr_text = "SASL authentication required";
1158 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1163 if( requires & SLAP_REQUIRE_AUTHC ) {
1164 if( op->o_dn.bv_len == 0 ) {
1165 rs->sr_text = "authentication required";
1166 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1171 if( requires & SLAP_REQUIRE_BIND ) {
1173 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1174 version = op->o_conn->c_protocol;
1175 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1178 /* no bind has occurred */
1179 rs->sr_text = "BIND required";
1180 rs->sr_err = LDAP_OPERATIONS_ERROR;
1185 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1186 if( op->o_protocol < LDAP_VERSION3 ) {
1187 /* no bind has occurred */
1188 rs->sr_text = "operation restricted to LDAPv3 clients";
1189 rs->sr_err = LDAP_OPERATIONS_ERROR;
1194 #ifdef SLAP_X_LISTENER_MOD
1195 if ( !starttls && op->o_dn.bv_len == 0 ) {
1196 if ( op->o_conn->c_listener &&
1197 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1199 /* no "x" mode means bind required */
1200 rs->sr_text = "bind required on this listener";
1201 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1206 if ( !starttls && !updateop ) {
1207 if ( op->o_conn->c_listener &&
1208 !( op->o_conn->c_listener->sl_perms &
1209 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1211 /* no "r" mode means no read */
1212 rs->sr_text = "read not allowed on this listener";
1213 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1217 #endif /* SLAP_X_LISTENER_MOD */
1221 if( ( restrictops & opflag )
1222 || ( exopflag && ( restrictops & exopflag ) ) ) {
1223 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1224 rs->sr_text = "read operations restricted";
1225 } else if ( restrictops & exopflag ) {
1226 rs->sr_text = "extended operation restricted";
1228 rs->sr_text = "operation restricted";
1230 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1234 rs->sr_err = LDAP_SUCCESS;
1238 int backend_check_referrals( Operation *op, SlapReply *rs )
1240 rs->sr_err = LDAP_SUCCESS;
1242 if( op->o_bd->be_chk_referrals ) {
1243 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1245 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1246 send_ldap_result( op, rs );
1258 AttributeDescription *at,
1266 if (op->o_bd == NULL) {
1267 rc = LDAP_NO_SUCH_OBJECT;
1268 } else if ( op->o_bd->be_fetch ) {
1269 rc = ( op->o_bd->be_fetch )( op, ndn,
1272 rc = LDAP_UNWILLING_TO_PERFORM;
1281 struct berval *gr_ndn,
1282 struct berval *op_ndn,
1283 ObjectClass *group_oc,
1284 AttributeDescription *group_at )
1290 Backend *be = op->o_bd;
1292 if ( op->o_abandon ) return SLAPD_ABANDON;
1294 op->o_bd = select_backend( gr_ndn, 0, 0 );
1296 for (g = op->o_groups; g; g=g->ga_next) {
1297 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1298 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1300 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1309 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1313 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1316 a = attr_find( e->e_attrs, group_at );
1318 /* If the attribute is a subtype of labeledURI, treat this as
1319 * a dynamic group ala groupOfURLs
1321 if (is_at_subtype( group_at->ad_type,
1322 slap_schema.si_ad_labeledURI->ad_type ) )
1326 struct berval bv, nbase;
1329 Backend *b2 = op->o_bd;
1331 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1334 op->o_bd = select_backend( op_ndn, 0, 0 );
1335 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1340 for (i=0; a->a_vals[i].bv_val; i++) {
1341 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1346 nbase.bv_val = NULL;
1347 /* host part must be empty */
1348 /* attrs and extensions parts must be empty */
1349 if (( ludp->lud_host && *ludp->lud_host ) ||
1350 ludp->lud_attrs || ludp->lud_exts )
1354 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1355 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1356 op->o_tmpmemctx ) != LDAP_SUCCESS )
1360 switch(ludp->lud_scope) {
1361 case LDAP_SCOPE_BASE:
1362 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1364 case LDAP_SCOPE_ONELEVEL:
1365 dnParent(op_ndn, &bv );
1366 if ( !dn_match( &nbase, &bv )) goto loopit;
1368 case LDAP_SCOPE_SUBTREE:
1369 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1371 #ifdef LDAP_SCOPE_SUBORDINATE
1372 case LDAP_SCOPE_SUBORDINATE:
1373 if ( dn_match( &nbase, op_ndn ) &&
1374 !dnIsSuffix(op_ndn, &nbase ))
1380 filter = str2filter_x( op, ludp->lud_filter );
1382 if ( test_filter( NULL, user, filter ) ==
1387 filter_free_x( op, filter );
1390 ldap_free_urldesc( ludp );
1391 if ( nbase.bv_val ) {
1392 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1394 if ( rc == 0 ) break;
1396 if ( user != target ) {
1397 be_entry_release_r( op, user );
1402 rc = value_find_ex( group_at,
1403 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1404 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1405 a->a_nvals, op_ndn, op->o_tmpmemctx );
1408 rc = LDAP_NO_SUCH_ATTRIBUTE;
1411 be_entry_release_r( op, e );
1414 rc = LDAP_NO_SUCH_OBJECT;
1417 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1418 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1420 g->ga_be = op->o_bd;
1421 g->ga_oc = group_oc;
1422 g->ga_at = group_at;
1424 g->ga_len = gr_ndn->bv_len;
1425 strcpy(g->ga_ndn, gr_ndn->bv_val);
1426 g->ga_next = op->o_groups;
1439 AttributeDescription *entry_at,
1444 int i, j, rc = LDAP_SUCCESS;
1445 AccessControlState acl_state = ACL_STATE_INIT;
1446 Backend *be = op->o_bd;
1448 op->o_bd = select_backend( edn, 0, 0 );
1450 if ( target && dn_match( &target->e_nname, edn ) ) {
1453 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1457 a = attr_find( e->e_attrs, entry_at );
1461 if ( op->o_conn && access_allowed( op,
1462 e, entry_at, NULL, ACL_AUTH,
1463 &acl_state ) == 0 ) {
1464 rc = LDAP_INSUFFICIENT_ACCESS;
1468 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1470 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1472 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1473 if ( op->o_conn && access_allowed( op,
1476 ACL_AUTH, &acl_state ) == 0 ) {
1480 &a->a_nvals[i], op->o_tmpmemctx );
1481 if (v[j].bv_val ) j++;
1484 op->o_tmpfree( v, op->o_tmpmemctx );
1486 rc = LDAP_INSUFFICIENT_ACCESS;
1494 freeit: if (e != target ) {
1495 be_entry_release_r( op, e );
1503 Attribute *backend_operational(
1508 Attribute *a = NULL, **ap = &a;
1511 * If operational attributes (allegedly) are required,
1512 * and the backend supports specific operational attributes,
1513 * add them to the attribute list
1515 if ( opattrs || ( op->ors_attrs &&
1516 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1517 *ap = slap_operational_subschemaSubentry( op->o_bd );
1518 ap = &(*ap)->a_next;
1521 if ( ( opattrs || op->ors_attrs ) && op->o_bd &&
1522 op->o_bd->be_operational != NULL )
1524 ( void )op->o_bd->be_operational( op, rs, opattrs, ap );