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 /* startup a specific backend database */
283 int backend_startup_one(Backend *be)
289 be->be_pending_csn_list = (struct be_pcl *)
290 ch_calloc( 1, sizeof( struct be_pcl ));
291 build_new_dn( &be->be_context_csn, be->be_nsuffix,
292 (struct berval *)&slap_ldapsync_cn_bv, NULL );
294 LDAP_TAILQ_INIT( be->be_pending_csn_list );
297 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
298 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
301 Debug( LDAP_DEBUG_TRACE,
302 "backend_startup: starting \"%s\"\n",
303 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
306 if ( be->bd_info->bi_db_open ) {
307 rc = be->bd_info->bi_db_open( be );
310 LDAP_LOG( BACKEND, CRIT,
311 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
313 Debug( LDAP_DEBUG_ANY,
314 "backend_startup: bi_db_open failed! (%d)\n",
322 int backend_startup(Backend *be)
327 if( ! ( nBackendDB > 0 ) ) {
330 LDAP_LOG( BACKEND, INFO,
331 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
333 Debug( LDAP_DEBUG_ANY,
334 "backend_startup: %d databases to startup.\n",
341 if ( be->bd_info->bi_open ) {
342 rc = be->bd_info->bi_open( be->bd_info );
345 LDAP_LOG( BACKEND, CRIT,
346 "backend_startup: bi_open failed!\n", 0, 0, 0 );
348 Debug( LDAP_DEBUG_ANY,
349 "backend_startup: bi_open failed!\n",
357 return backend_startup_one( be );
360 /* open frontend, if required */
361 if ( frontendDB->bd_info->bi_db_open ) {
362 rc = frontendDB->bd_info->bi_db_open( frontendDB );
365 LDAP_LOG( BACKEND, CRIT,
366 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
369 Debug( LDAP_DEBUG_ANY,
370 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
377 /* open each backend type */
378 for( i = 0; i < nBackendInfo; i++ ) {
379 if( backendInfo[i].bi_nDB == 0) {
380 /* no database of this type, don't open */
384 if( backendInfo[i].bi_open ) {
385 rc = backendInfo[i].bi_open(
389 LDAP_LOG( BACKEND, CRIT,
390 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
392 Debug( LDAP_DEBUG_ANY,
393 "backend_startup: bi_open %d failed!\n",
401 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
402 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
403 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
405 /* open each backend database */
406 for( i = 0; i < nBackendDB; i++ ) {
407 if ( backendDB[i].be_suffix == NULL ) {
409 LDAP_LOG( BACKEND, CRIT,
410 "backend_startup: warning, database %d (%s) "
412 i, backendDB[i].bd_info->bi_type, 0 );
414 Debug( LDAP_DEBUG_ANY,
415 "backend_startup: warning, database %d (%s) "
417 i, backendDB[i].bd_info->bi_type, 0 );
420 /* append global access controls */
421 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
423 rc = backend_startup_one( &backendDB[i] );
428 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
431 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
432 backendDB[i].be_modify && backendDB[i].be_delete )) {
434 LDAP_LOG( BACKEND, CRIT,
435 "backend_startup: database(%d) does not support "
436 "operations required for syncrepl", i, 0, 0 );
438 Debug( LDAP_DEBUG_ANY,
439 "backend_startup: database(%d) does not support "
440 "operations required for syncrepl", i, 0, 0 );
445 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
446 si->si_be = &backendDB[i];
448 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
449 ldap_pvt_runqueue_insert( &syncrepl_rq,
450 si->si_interval, do_syncrepl, (void *) si );
451 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
459 int backend_num( Backend *be )
463 if( be == NULL ) return -1;
465 for( i = 0; i < nBackendDB; i++ ) {
466 if( be == &backendDB[i] ) return i;
471 int backend_shutdown( Backend *be )
477 /* shutdown a specific backend database */
479 if ( be->bd_info->bi_nDB == 0 ) {
480 /* no database of this type, we never opened it */
484 if ( be->bd_info->bi_db_close ) {
485 be->bd_info->bi_db_close( be );
488 if( be->bd_info->bi_close ) {
489 be->bd_info->bi_close( be->bd_info );
495 /* close each backend database */
496 for( i = 0; i < nBackendDB; i++ ) {
497 if ( backendDB[i].bd_info->bi_db_close ) {
498 backendDB[i].bd_info->bi_db_close(
504 LDAP_LOG( BACKEND, NOTICE,
505 "backend_shutdown: bi_db_close %s failed!\n",
506 backendDB[i].be_type, 0, 0 );
508 Debug( LDAP_DEBUG_ANY,
509 "backend_close: bi_db_close %s failed!\n",
510 backendDB[i].be_type, 0, 0 );
515 /* close each backend type */
516 for( i = 0; i < nBackendInfo; i++ ) {
517 if( backendInfo[i].bi_nDB == 0 ) {
518 /* no database of this type */
522 if( backendInfo[i].bi_close ) {
523 backendInfo[i].bi_close(
528 /* close frontend, if required */
529 if ( frontendDB->bd_info->bi_db_close ) {
530 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
533 LDAP_LOG( BACKEND, CRIT,
534 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
537 Debug( LDAP_DEBUG_ANY,
538 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
547 int backend_destroy(void)
552 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
554 /* destroy each backend database */
555 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
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 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
565 acl_destroy( bd->be_acl, frontendDB->be_acl );
569 /* destroy each backend type */
570 for( i = 0; i < nBackendInfo; i++ ) {
571 if( backendInfo[i].bi_destroy ) {
572 backendInfo[i].bi_destroy(
578 if (backendInfo != binfo) {
581 #endif /* SLAPD_MODULES */
586 /* destroy frontend database */
588 if ( bd->bd_info->bi_db_destroy ) {
589 bd->bd_info->bi_db_destroy( bd );
591 ber_bvarray_free( bd->be_suffix );
592 ber_bvarray_free( bd->be_nsuffix );
593 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
594 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
595 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
596 acl_destroy( bd->be_acl, frontendDB->be_acl );
601 BackendInfo* backend_info(const char *type)
605 /* search for the backend type */
606 for( i = 0; i < nBackendInfo; i++ ) {
607 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
608 return &backendInfo[i];
621 BackendInfo *bi = backend_info(type);
625 fprintf( stderr, "Unrecognized database type (%s)\n", type );
629 backendDB = (BackendDB *) ch_realloc(
631 (nBackendDB + 1) * sizeof(Backend) );
633 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
635 be = &backends[nbackends++];
638 be->be_def_limit = frontendDB->be_def_limit;
639 be->be_dfltaccess = frontendDB->be_dfltaccess;
641 be->be_restrictops = frontendDB->be_restrictops;
642 be->be_requires = frontendDB->be_requires;
643 be->be_ssf_set = frontendDB->be_ssf_set;
645 be->be_context_csn.bv_len = 0;
646 be->be_context_csn.bv_val = NULL;
647 be->be_pcl_mutexp = &be->be_pcl_mutex;
648 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
649 ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
651 LDAP_STAILQ_INIT( &be->be_syncinfo );
653 /* assign a default depth limit for alias deref */
654 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
657 rc = bi->bi_db_init( be );
661 fprintf( stderr, "database init failed (%s)\n", type );
675 for ( i = 0; i < nbackends; i++ ) {
676 if ( backends[i].bd_info->bi_db_close ) {
677 (*backends[i].bd_info->bi_db_close)( &backends[i] );
681 if ( frontendDB->bd_info->bi_db_close ) {
682 (*frontendDB->bd_info->bi_db_close)( frontendDB );
693 ber_len_t len, dnlen = dn->bv_len;
696 for ( i = 0; i < nbackends; i++ ) {
697 for ( j = 0; backends[i].be_nsuffix != NULL &&
698 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
700 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
706 len = backends[i].be_nsuffix[j].bv_len;
709 /* suffix is longer than DN */
714 * input DN is normalized, so the separator check
715 * need not look at escaping
717 if ( len && len < dnlen &&
718 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
723 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
724 &dn->bv_val[dnlen-len] ) == 0 )
729 if( manageDSAit && len == dnlen &&
730 !SLAP_GLUE_SUBORDINATE( be ) ) {
747 struct berval *bvsuffix )
752 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
755 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
764 be_isroot_dn( Backend *be, struct berval *ndn )
766 if ( !ndn->bv_len ) {
770 if ( !be->be_rootndn.bv_len ) {
774 return dn_match( &be->be_rootndn, ndn );
778 be_sync_update( Operation *op )
780 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
784 be_slurp_update( Operation *op )
786 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
787 be_isupdate_dn( op->o_bd, &op->o_ndn ));
791 be_shadow_update( Operation *op )
793 return ( SLAP_SHADOW( op->o_bd ) &&
794 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
798 be_isupdate_dn( Backend *be, struct berval *ndn )
800 if ( !ndn->bv_len ) return( 0 );
802 if ( !be->be_update_ndn.bv_len ) return( 0 );
804 return dn_match( &be->be_update_ndn, ndn );
808 be_root_dn( Backend *be )
810 return &be->be_rootdn;
814 be_isroot( Operation *op )
816 return be_isroot_dn( op->o_bd, &op->o_ndn );
820 be_isroot_pw( Operation *op )
824 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
828 if( op->o_bd->be_rootpw.bv_len == 0 ) {
832 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
833 ldap_pvt_thread_mutex_lock( &passwd_mutex );
835 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
839 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
841 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
843 lutil_passwd_sasl_conn = NULL;
845 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
857 if ( op->o_bd->be_release ) {
858 /* free and release entry from backend */
859 return op->o_bd->be_release( op, e, rw );
868 backend_unbind( Operation *op, SlapReply *rs )
872 for ( i = 0; i < nbackends; i++ ) {
873 #if defined( LDAP_SLAPI )
876 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
877 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
878 rc = slapi_int_call_plugins( &backends[i],
879 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
882 * A preoperation plugin failure will abort the
886 LDAP_LOG( OPERATION, INFO,
887 "do_bind: Unbind preoperation plugin failed\n",
890 Debug(LDAP_DEBUG_TRACE,
891 "do_bind: Unbind preoperation plugin failed\n",
897 #endif /* defined( LDAP_SLAPI ) */
899 if ( backends[i].be_unbind ) {
900 op->o_bd = &backends[i];
901 (*backends[i].be_unbind)( op, rs );
904 #if defined( LDAP_SLAPI )
905 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
906 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
909 LDAP_LOG( OPERATION, INFO,
910 "do_unbind: Unbind postoperation plugins failed\n",
913 Debug(LDAP_DEBUG_TRACE,
914 "do_unbind: Unbind postoperation plugins failed\n",
918 #endif /* defined( LDAP_SLAPI ) */
925 backend_connection_init(
930 for ( i = 0; i < nbackends; i++ ) {
931 if ( backends[i].be_connection_init ) {
932 (*backends[i].be_connection_init)( &backends[i], conn);
940 backend_connection_destroy(
945 for ( i = 0; i < nbackends; i++ ) {
946 if ( backends[i].be_connection_destroy ) {
947 (*backends[i].be_connection_destroy)( &backends[i], conn);
955 backend_check_controls(
959 LDAPControl **ctrls = op->o_ctrls;
960 rs->sr_err = LDAP_SUCCESS;
963 for( ; *ctrls != NULL ; ctrls++ ) {
964 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
965 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
967 rs->sr_text = "control unavailable in context";
968 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
978 backend_check_restrictions(
981 struct berval *opdata )
983 slap_mask_t restrictops;
984 slap_mask_t requires;
986 slap_mask_t exopflag = 0;
993 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
997 restrictops = op->o_bd->be_restrictops;
998 requires = op->o_bd->be_requires;
999 ssf = &op->o_bd->be_ssf_set;
1002 restrictops = frontendDB->be_restrictops;
1003 requires = frontendDB->be_requires;
1004 ssf = &frontendDB->be_ssf_set;
1007 switch( op->o_tag ) {
1009 opflag = SLAP_RESTRICT_OP_ADD;
1013 opflag = SLAP_RESTRICT_OP_BIND;
1016 case LDAP_REQ_COMPARE:
1017 opflag = SLAP_RESTRICT_OP_COMPARE;
1019 case LDAP_REQ_DELETE:
1021 opflag = SLAP_RESTRICT_OP_DELETE;
1023 case LDAP_REQ_EXTENDED:
1024 opflag = SLAP_RESTRICT_OP_EXTENDED;
1027 /* treat unspecified as a modify */
1028 opflag = SLAP_RESTRICT_OP_MODIFY;
1033 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1036 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1040 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1041 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1045 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1046 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1050 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1051 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1056 /* treat everything else as a modify */
1057 opflag = SLAP_RESTRICT_OP_MODIFY;
1061 case LDAP_REQ_MODIFY:
1063 opflag = SLAP_RESTRICT_OP_MODIFY;
1065 case LDAP_REQ_RENAME:
1067 opflag = SLAP_RESTRICT_OP_RENAME;
1069 case LDAP_REQ_SEARCH:
1070 opflag = SLAP_RESTRICT_OP_SEARCH;
1072 case LDAP_REQ_UNBIND:
1077 rs->sr_text = "restrict operations internal error";
1078 rs->sr_err = LDAP_OTHER;
1083 /* these checks don't apply to StartTLS */
1085 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1086 if( op->o_transport_ssf < ssf->sss_transport ) {
1087 rs->sr_text = op->o_transport_ssf
1088 ? "stronger transport confidentiality required"
1089 : "transport confidentiality required";
1093 if( op->o_tls_ssf < ssf->sss_tls ) {
1094 rs->sr_text = op->o_tls_ssf
1095 ? "stronger TLS confidentiality required"
1096 : "TLS confidentiality required";
1101 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1102 /* simple bind specific check */
1103 if( op->o_ssf < ssf->sss_simple_bind ) {
1104 rs->sr_text = op->o_ssf
1105 ? "stronger confidentiality required"
1106 : "confidentiality required";
1111 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1112 /* these checks don't apply to SASL bind */
1114 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1115 rs->sr_text = op->o_sasl_ssf
1116 ? "stronger SASL confidentiality required"
1117 : "SASL confidentiality required";
1121 if( op->o_ssf < ssf->sss_ssf ) {
1122 rs->sr_text = op->o_ssf
1123 ? "stronger confidentiality required"
1124 : "confidentiality required";
1130 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1131 rs->sr_text = op->o_transport_ssf
1132 ? "stronger transport confidentiality required for update"
1133 : "transport confidentiality required for update";
1137 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1138 rs->sr_text = op->o_tls_ssf
1139 ? "stronger TLS confidentiality required for update"
1140 : "TLS confidentiality required for update";
1144 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1145 rs->sr_text = op->o_sasl_ssf
1146 ? "stronger SASL confidentiality required for update"
1147 : "SASL confidentiality required for update";
1151 if( op->o_ssf < ssf->sss_update_ssf ) {
1152 rs->sr_text = op->o_ssf
1153 ? "stronger confidentiality required for update"
1154 : "confidentiality required for update";
1158 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1159 op->o_ndn.bv_len == 0 )
1161 rs->sr_text = "modifications require authentication";
1162 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1166 #ifdef SLAP_X_LISTENER_MOD
1167 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1168 /* no "w" mode means readonly */
1169 rs->sr_text = "modifications not allowed on this listener";
1170 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1173 #endif /* SLAP_X_LISTENER_MOD */
1178 /* these checks don't apply to Bind, StartTLS, or Unbind */
1180 if( requires & SLAP_REQUIRE_STRONG ) {
1181 /* should check mechanism */
1182 if( ( op->o_transport_ssf < ssf->sss_transport
1183 && op->o_authtype == LDAP_AUTH_SIMPLE )
1184 || op->o_dn.bv_len == 0 )
1186 rs->sr_text = "strong(er) authentication required";
1187 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1192 if( requires & SLAP_REQUIRE_SASL ) {
1193 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1194 rs->sr_text = "SASL authentication required";
1195 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1200 if( requires & SLAP_REQUIRE_AUTHC ) {
1201 if( op->o_dn.bv_len == 0 ) {
1202 rs->sr_text = "authentication required";
1203 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1208 if( requires & SLAP_REQUIRE_BIND ) {
1210 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1211 version = op->o_conn->c_protocol;
1212 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1215 /* no bind has occurred */
1216 rs->sr_text = "BIND required";
1217 rs->sr_err = LDAP_OPERATIONS_ERROR;
1222 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1223 if( op->o_protocol < LDAP_VERSION3 ) {
1224 /* no bind has occurred */
1225 rs->sr_text = "operation restricted to LDAPv3 clients";
1226 rs->sr_err = LDAP_OPERATIONS_ERROR;
1231 #ifdef SLAP_X_LISTENER_MOD
1232 if ( !starttls && op->o_dn.bv_len == 0 ) {
1233 if ( op->o_conn->c_listener &&
1234 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1236 /* no "x" mode means bind required */
1237 rs->sr_text = "bind required on this listener";
1238 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1243 if ( !starttls && !updateop ) {
1244 if ( op->o_conn->c_listener &&
1245 !( op->o_conn->c_listener->sl_perms &
1246 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1248 /* no "r" mode means no read */
1249 rs->sr_text = "read not allowed on this listener";
1250 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1254 #endif /* SLAP_X_LISTENER_MOD */
1258 if( ( restrictops & opflag )
1259 || ( exopflag && ( restrictops & exopflag ) ) ) {
1260 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1261 rs->sr_text = "read operations restricted";
1262 } else if ( restrictops & exopflag ) {
1263 rs->sr_text = "extended operation restricted";
1265 rs->sr_text = "operation restricted";
1267 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1271 rs->sr_err = LDAP_SUCCESS;
1275 int backend_check_referrals( Operation *op, SlapReply *rs )
1277 rs->sr_err = LDAP_SUCCESS;
1279 if( op->o_bd->be_chk_referrals ) {
1280 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1282 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1283 send_ldap_result( op, rs );
1295 AttributeDescription *at,
1303 if (op->o_bd == NULL) {
1304 rc = LDAP_NO_SUCH_OBJECT;
1305 } else if ( op->o_bd->be_fetch ) {
1306 rc = ( op->o_bd->be_fetch )( op, ndn,
1309 rc = LDAP_UNWILLING_TO_PERFORM;
1318 struct berval *gr_ndn,
1319 struct berval *op_ndn,
1320 ObjectClass *group_oc,
1321 AttributeDescription *group_at )
1327 Backend *be = op->o_bd;
1329 if ( op->o_abandon ) return SLAPD_ABANDON;
1331 op->o_bd = select_backend( gr_ndn, 0, 0 );
1333 for (g = op->o_groups; g; g=g->ga_next) {
1334 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1335 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1337 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1346 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1350 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1353 a = attr_find( e->e_attrs, group_at );
1355 /* If the attribute is a subtype of labeledURI, treat this as
1356 * a dynamic group ala groupOfURLs
1358 if (is_at_subtype( group_at->ad_type,
1359 slap_schema.si_ad_labeledURI->ad_type ) )
1363 struct berval bv, nbase;
1366 Backend *b2 = op->o_bd;
1368 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1371 op->o_bd = select_backend( op_ndn, 0, 0 );
1372 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1377 for (i=0; a->a_vals[i].bv_val; i++) {
1378 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1383 nbase.bv_val = NULL;
1384 /* host part must be empty */
1385 /* attrs and extensions parts must be empty */
1386 if (( ludp->lud_host && *ludp->lud_host ) ||
1387 ludp->lud_attrs || ludp->lud_exts )
1391 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1392 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1393 op->o_tmpmemctx ) != LDAP_SUCCESS )
1397 switch(ludp->lud_scope) {
1398 case LDAP_SCOPE_BASE:
1399 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1401 case LDAP_SCOPE_ONELEVEL:
1402 dnParent(op_ndn, &bv );
1403 if ( !dn_match( &nbase, &bv )) goto loopit;
1405 case LDAP_SCOPE_SUBTREE:
1406 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1408 #ifdef LDAP_SCOPE_SUBORDINATE
1409 case LDAP_SCOPE_SUBORDINATE:
1410 if ( dn_match( &nbase, op_ndn ) &&
1411 !dnIsSuffix(op_ndn, &nbase ))
1417 filter = str2filter_x( op, ludp->lud_filter );
1419 if ( test_filter( NULL, user, filter ) ==
1424 filter_free_x( op, filter );
1427 ldap_free_urldesc( ludp );
1428 if ( nbase.bv_val ) {
1429 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1431 if ( rc == 0 ) break;
1433 if ( user != target ) {
1434 be_entry_release_r( op, user );
1439 rc = value_find_ex( group_at,
1440 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1441 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1442 a->a_nvals, op_ndn, op->o_tmpmemctx );
1445 rc = LDAP_NO_SUCH_ATTRIBUTE;
1448 be_entry_release_r( op, e );
1451 rc = LDAP_NO_SUCH_OBJECT;
1454 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1455 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1457 g->ga_be = op->o_bd;
1458 g->ga_oc = group_oc;
1459 g->ga_at = group_at;
1461 g->ga_len = gr_ndn->bv_len;
1462 strcpy(g->ga_ndn, gr_ndn->bv_val);
1463 g->ga_next = op->o_groups;
1476 AttributeDescription *entry_at,
1481 int i, j, rc = LDAP_SUCCESS;
1482 AccessControlState acl_state = ACL_STATE_INIT;
1483 Backend *be = op->o_bd;
1485 op->o_bd = select_backend( edn, 0, 0 );
1487 if ( target && dn_match( &target->e_nname, edn ) ) {
1490 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1494 a = attr_find( e->e_attrs, entry_at );
1498 if ( op->o_conn && access_allowed( op,
1499 e, entry_at, NULL, ACL_AUTH,
1500 &acl_state ) == 0 ) {
1501 rc = LDAP_INSUFFICIENT_ACCESS;
1505 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1507 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1509 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1510 if ( op->o_conn && access_allowed( op,
1513 ACL_AUTH, &acl_state ) == 0 ) {
1517 &a->a_nvals[i], op->o_tmpmemctx );
1518 if (v[j].bv_val ) j++;
1521 op->o_tmpfree( v, op->o_tmpmemctx );
1523 rc = LDAP_INSUFFICIENT_ACCESS;
1531 freeit: if (e != target ) {
1532 be_entry_release_r( op, e );
1540 int backend_operational(
1547 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1548 /* just count them */ ;
1551 * If operational attributes (allegedly) are required,
1552 * and the backend supports specific operational attributes,
1553 * add them to the attribute list
1555 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1556 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1557 *ap = slap_operational_subschemaSubentry( op->o_bd );
1559 ap = &(*ap)->a_next;
1562 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) && op->o_bd &&
1563 op->o_bd->be_operational != NULL )
1567 a = rs->sr_operational_attrs;
1568 rs->sr_operational_attrs = NULL;
1569 rc = op->o_bd->be_operational( op, rs );
1570 *ap = rs->sr_operational_attrs;
1572 rs->sr_operational_attrs = a;
1575 for ( ; *ap; ap = &(*ap)->a_next )
1576 /* just count them */ ;