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 );
631 backendDB = (BackendDB *) ch_realloc(
633 (nBackendDB + 1) * sizeof(Backend) );
635 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
637 /* did realloc move our table? if so, fix up dependent pointers */
638 if ( be != backendDB ) {
640 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
641 be->be_pcl_mutexp = &be->be_pcl_mutex;
645 be = &backends[nbackends++];
648 be->be_def_limit = frontendDB->be_def_limit;
649 be->be_dfltaccess = frontendDB->be_dfltaccess;
651 be->be_restrictops = frontendDB->be_restrictops;
652 be->be_requires = frontendDB->be_requires;
653 be->be_ssf_set = frontendDB->be_ssf_set;
655 be->be_context_csn.bv_len = 0;
656 be->be_context_csn.bv_val = NULL;
657 be->be_pcl_mutexp = &be->be_pcl_mutex;
658 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
660 LDAP_STAILQ_INIT( &be->be_syncinfo );
662 /* assign a default depth limit for alias deref */
663 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
666 rc = bi->bi_db_init( be );
670 fprintf( stderr, "database init failed (%s)\n", type );
684 for ( i = 0; i < nbackends; i++ ) {
685 if ( backends[i].bd_info->bi_db_close ) {
686 (*backends[i].bd_info->bi_db_close)( &backends[i] );
690 if ( frontendDB->bd_info->bi_db_close ) {
691 (*frontendDB->bd_info->bi_db_close)( frontendDB );
702 ber_len_t len, dnlen = dn->bv_len;
705 for ( i = 0; i < nbackends; i++ ) {
706 for ( j = 0; backends[i].be_nsuffix != NULL &&
707 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
709 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
715 len = backends[i].be_nsuffix[j].bv_len;
718 /* suffix is longer than DN */
723 * input DN is normalized, so the separator check
724 * need not look at escaping
726 if ( len && len < dnlen &&
727 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
732 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
733 &dn->bv_val[dnlen-len] ) == 0 )
738 if( manageDSAit && len == dnlen &&
739 !SLAP_GLUE_SUBORDINATE( be ) ) {
756 struct berval *bvsuffix )
761 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
764 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
773 be_isroot_dn( Backend *be, struct berval *ndn )
775 if ( !ndn->bv_len ) {
779 if ( !be->be_rootndn.bv_len ) {
783 return dn_match( &be->be_rootndn, ndn );
787 be_sync_update( Operation *op )
789 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
793 be_slurp_update( Operation *op )
795 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
796 be_isupdate_dn( op->o_bd, &op->o_ndn ));
800 be_shadow_update( Operation *op )
802 return ( SLAP_SHADOW( op->o_bd ) &&
803 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
807 be_isupdate_dn( Backend *be, struct berval *ndn )
809 if ( !ndn->bv_len ) return( 0 );
811 if ( !be->be_update_ndn.bv_len ) return( 0 );
813 return dn_match( &be->be_update_ndn, ndn );
817 be_root_dn( Backend *be )
819 return &be->be_rootdn;
823 be_isroot( Operation *op )
825 return be_isroot_dn( op->o_bd, &op->o_ndn );
829 be_isroot_pw( Operation *op )
833 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
837 if( op->o_bd->be_rootpw.bv_len == 0 ) {
841 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
842 ldap_pvt_thread_mutex_lock( &passwd_mutex );
844 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
848 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
850 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
852 lutil_passwd_sasl_conn = NULL;
854 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
866 if ( op->o_bd->be_release ) {
867 /* free and release entry from backend */
868 return op->o_bd->be_release( op, e, rw );
877 backend_unbind( Operation *op, SlapReply *rs )
881 for ( i = 0; i < nbackends; i++ ) {
882 #if defined( LDAP_SLAPI )
885 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
886 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
887 rc = slapi_int_call_plugins( &backends[i],
888 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
891 * A preoperation plugin failure will abort the
895 LDAP_LOG( OPERATION, INFO,
896 "do_bind: Unbind preoperation plugin failed\n",
899 Debug(LDAP_DEBUG_TRACE,
900 "do_bind: Unbind preoperation plugin failed\n",
906 #endif /* defined( LDAP_SLAPI ) */
908 if ( backends[i].be_unbind ) {
909 op->o_bd = &backends[i];
910 (*backends[i].be_unbind)( op, rs );
913 #if defined( LDAP_SLAPI )
914 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
915 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
918 LDAP_LOG( OPERATION, INFO,
919 "do_unbind: Unbind postoperation plugins failed\n",
922 Debug(LDAP_DEBUG_TRACE,
923 "do_unbind: Unbind postoperation plugins failed\n",
927 #endif /* defined( LDAP_SLAPI ) */
934 backend_connection_init(
939 for ( i = 0; i < nbackends; i++ ) {
940 if ( backends[i].be_connection_init ) {
941 (*backends[i].be_connection_init)( &backends[i], conn);
949 backend_connection_destroy(
954 for ( i = 0; i < nbackends; i++ ) {
955 if ( backends[i].be_connection_destroy ) {
956 (*backends[i].be_connection_destroy)( &backends[i], conn);
964 backend_check_controls(
968 LDAPControl **ctrls = op->o_ctrls;
969 rs->sr_err = LDAP_SUCCESS;
972 for( ; *ctrls != NULL ; ctrls++ ) {
973 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
974 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
976 rs->sr_text = "control unavailable in context";
977 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
987 backend_check_restrictions(
990 struct berval *opdata )
992 slap_mask_t restrictops;
993 slap_mask_t requires;
995 slap_mask_t exopflag = 0;
1002 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
1006 restrictops = op->o_bd->be_restrictops;
1007 requires = op->o_bd->be_requires;
1008 ssf = &op->o_bd->be_ssf_set;
1011 restrictops = frontendDB->be_restrictops;
1012 requires = frontendDB->be_requires;
1013 ssf = &frontendDB->be_ssf_set;
1016 switch( op->o_tag ) {
1018 opflag = SLAP_RESTRICT_OP_ADD;
1022 opflag = SLAP_RESTRICT_OP_BIND;
1025 case LDAP_REQ_COMPARE:
1026 opflag = SLAP_RESTRICT_OP_COMPARE;
1028 case LDAP_REQ_DELETE:
1030 opflag = SLAP_RESTRICT_OP_DELETE;
1032 case LDAP_REQ_EXTENDED:
1033 opflag = SLAP_RESTRICT_OP_EXTENDED;
1036 /* treat unspecified as a modify */
1037 opflag = SLAP_RESTRICT_OP_MODIFY;
1042 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1045 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1049 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1050 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1054 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1055 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1059 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1060 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1065 /* treat everything else as a modify */
1066 opflag = SLAP_RESTRICT_OP_MODIFY;
1070 case LDAP_REQ_MODIFY:
1072 opflag = SLAP_RESTRICT_OP_MODIFY;
1074 case LDAP_REQ_RENAME:
1076 opflag = SLAP_RESTRICT_OP_RENAME;
1078 case LDAP_REQ_SEARCH:
1079 opflag = SLAP_RESTRICT_OP_SEARCH;
1081 case LDAP_REQ_UNBIND:
1086 rs->sr_text = "restrict operations internal error";
1087 rs->sr_err = LDAP_OTHER;
1092 /* these checks don't apply to StartTLS */
1094 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1095 if( op->o_transport_ssf < ssf->sss_transport ) {
1096 rs->sr_text = op->o_transport_ssf
1097 ? "stronger transport confidentiality required"
1098 : "transport confidentiality required";
1102 if( op->o_tls_ssf < ssf->sss_tls ) {
1103 rs->sr_text = op->o_tls_ssf
1104 ? "stronger TLS confidentiality required"
1105 : "TLS confidentiality required";
1110 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1111 /* simple bind specific check */
1112 if( op->o_ssf < ssf->sss_simple_bind ) {
1113 rs->sr_text = op->o_ssf
1114 ? "stronger confidentiality required"
1115 : "confidentiality required";
1120 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1121 /* these checks don't apply to SASL bind */
1123 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1124 rs->sr_text = op->o_sasl_ssf
1125 ? "stronger SASL confidentiality required"
1126 : "SASL confidentiality required";
1130 if( op->o_ssf < ssf->sss_ssf ) {
1131 rs->sr_text = op->o_ssf
1132 ? "stronger confidentiality required"
1133 : "confidentiality required";
1139 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1140 rs->sr_text = op->o_transport_ssf
1141 ? "stronger transport confidentiality required for update"
1142 : "transport confidentiality required for update";
1146 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1147 rs->sr_text = op->o_tls_ssf
1148 ? "stronger TLS confidentiality required for update"
1149 : "TLS confidentiality required for update";
1153 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1154 rs->sr_text = op->o_sasl_ssf
1155 ? "stronger SASL confidentiality required for update"
1156 : "SASL confidentiality required for update";
1160 if( op->o_ssf < ssf->sss_update_ssf ) {
1161 rs->sr_text = op->o_ssf
1162 ? "stronger confidentiality required for update"
1163 : "confidentiality required for update";
1167 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1168 op->o_ndn.bv_len == 0 )
1170 rs->sr_text = "modifications require authentication";
1171 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1175 #ifdef SLAP_X_LISTENER_MOD
1176 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1177 /* no "w" mode means readonly */
1178 rs->sr_text = "modifications not allowed on this listener";
1179 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1182 #endif /* SLAP_X_LISTENER_MOD */
1187 /* these checks don't apply to Bind, StartTLS, or Unbind */
1189 if( requires & SLAP_REQUIRE_STRONG ) {
1190 /* should check mechanism */
1191 if( ( op->o_transport_ssf < ssf->sss_transport
1192 && op->o_authtype == LDAP_AUTH_SIMPLE )
1193 || op->o_dn.bv_len == 0 )
1195 rs->sr_text = "strong(er) authentication required";
1196 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1201 if( requires & SLAP_REQUIRE_SASL ) {
1202 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1203 rs->sr_text = "SASL authentication required";
1204 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1209 if( requires & SLAP_REQUIRE_AUTHC ) {
1210 if( op->o_dn.bv_len == 0 ) {
1211 rs->sr_text = "authentication required";
1212 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1217 if( requires & SLAP_REQUIRE_BIND ) {
1219 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1220 version = op->o_conn->c_protocol;
1221 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1224 /* no bind has occurred */
1225 rs->sr_text = "BIND required";
1226 rs->sr_err = LDAP_OPERATIONS_ERROR;
1231 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1232 if( op->o_protocol < LDAP_VERSION3 ) {
1233 /* no bind has occurred */
1234 rs->sr_text = "operation restricted to LDAPv3 clients";
1235 rs->sr_err = LDAP_OPERATIONS_ERROR;
1240 #ifdef SLAP_X_LISTENER_MOD
1241 if ( !starttls && op->o_dn.bv_len == 0 ) {
1242 if ( op->o_conn->c_listener &&
1243 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1245 /* no "x" mode means bind required */
1246 rs->sr_text = "bind required on this listener";
1247 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1252 if ( !starttls && !updateop ) {
1253 if ( op->o_conn->c_listener &&
1254 !( op->o_conn->c_listener->sl_perms &
1255 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1257 /* no "r" mode means no read */
1258 rs->sr_text = "read not allowed on this listener";
1259 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1263 #endif /* SLAP_X_LISTENER_MOD */
1267 if( ( restrictops & opflag )
1268 || ( exopflag && ( restrictops & exopflag ) ) ) {
1269 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1270 rs->sr_text = "read operations restricted";
1271 } else if ( restrictops & exopflag ) {
1272 rs->sr_text = "extended operation restricted";
1274 rs->sr_text = "operation restricted";
1276 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1280 rs->sr_err = LDAP_SUCCESS;
1284 int backend_check_referrals( Operation *op, SlapReply *rs )
1286 rs->sr_err = LDAP_SUCCESS;
1288 if( op->o_bd->be_chk_referrals ) {
1289 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1291 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1292 send_ldap_result( op, rs );
1304 AttributeDescription *at,
1312 if (op->o_bd == NULL) {
1313 rc = LDAP_NO_SUCH_OBJECT;
1314 } else if ( op->o_bd->be_fetch ) {
1315 rc = ( op->o_bd->be_fetch )( op, ndn,
1318 rc = LDAP_UNWILLING_TO_PERFORM;
1327 struct berval *gr_ndn,
1328 struct berval *op_ndn,
1329 ObjectClass *group_oc,
1330 AttributeDescription *group_at )
1336 Backend *be = op->o_bd;
1338 if ( op->o_abandon ) return SLAPD_ABANDON;
1340 op->o_bd = select_backend( gr_ndn, 0, 0 );
1342 for (g = op->o_groups; g; g=g->ga_next) {
1343 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1344 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1346 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1355 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1359 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1362 a = attr_find( e->e_attrs, group_at );
1364 /* If the attribute is a subtype of labeledURI, treat this as
1365 * a dynamic group ala groupOfURLs
1367 if (is_at_subtype( group_at->ad_type,
1368 slap_schema.si_ad_labeledURI->ad_type ) )
1372 struct berval bv, nbase;
1375 Backend *b2 = op->o_bd;
1377 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1380 op->o_bd = select_backend( op_ndn, 0, 0 );
1381 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1386 for (i=0; a->a_vals[i].bv_val; i++) {
1387 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1392 nbase.bv_val = NULL;
1393 /* host part must be empty */
1394 /* attrs and extensions parts must be empty */
1395 if (( ludp->lud_host && *ludp->lud_host ) ||
1396 ludp->lud_attrs || ludp->lud_exts )
1400 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1401 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1402 op->o_tmpmemctx ) != LDAP_SUCCESS )
1406 switch(ludp->lud_scope) {
1407 case LDAP_SCOPE_BASE:
1408 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1410 case LDAP_SCOPE_ONELEVEL:
1411 dnParent(op_ndn, &bv );
1412 if ( !dn_match( &nbase, &bv )) goto loopit;
1414 case LDAP_SCOPE_SUBTREE:
1415 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1417 #ifdef LDAP_SCOPE_SUBORDINATE
1418 case LDAP_SCOPE_SUBORDINATE:
1419 if ( dn_match( &nbase, op_ndn ) &&
1420 !dnIsSuffix(op_ndn, &nbase ))
1426 filter = str2filter_x( op, ludp->lud_filter );
1428 if ( test_filter( NULL, user, filter ) ==
1433 filter_free_x( op, filter );
1436 ldap_free_urldesc( ludp );
1437 if ( nbase.bv_val ) {
1438 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1440 if ( rc == 0 ) break;
1442 if ( user != target ) {
1443 be_entry_release_r( op, user );
1448 rc = value_find_ex( group_at,
1449 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1450 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1451 a->a_nvals, op_ndn, op->o_tmpmemctx );
1454 rc = LDAP_NO_SUCH_ATTRIBUTE;
1457 be_entry_release_r( op, e );
1460 rc = LDAP_NO_SUCH_OBJECT;
1463 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1464 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1466 g->ga_be = op->o_bd;
1467 g->ga_oc = group_oc;
1468 g->ga_at = group_at;
1470 g->ga_len = gr_ndn->bv_len;
1471 strcpy(g->ga_ndn, gr_ndn->bv_val);
1472 g->ga_next = op->o_groups;
1485 AttributeDescription *entry_at,
1490 int i, j, rc = LDAP_SUCCESS;
1491 AccessControlState acl_state = ACL_STATE_INIT;
1492 Backend *be = op->o_bd;
1494 op->o_bd = select_backend( edn, 0, 0 );
1496 if ( target && dn_match( &target->e_nname, edn ) ) {
1499 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1503 a = attr_find( e->e_attrs, entry_at );
1507 if ( op->o_conn && access_allowed( op,
1508 e, entry_at, NULL, ACL_AUTH,
1509 &acl_state ) == 0 ) {
1510 rc = LDAP_INSUFFICIENT_ACCESS;
1514 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1516 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1518 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1519 if ( op->o_conn && access_allowed( op,
1522 ACL_AUTH, &acl_state ) == 0 ) {
1526 &a->a_nvals[i], op->o_tmpmemctx );
1527 if (v[j].bv_val ) j++;
1530 op->o_tmpfree( v, op->o_tmpmemctx );
1532 rc = LDAP_INSUFFICIENT_ACCESS;
1540 freeit: if (e != target ) {
1541 be_entry_release_r( op, e );
1549 int backend_operational(
1556 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1557 /* just count them */ ;
1560 * If operational attributes (allegedly) are required,
1561 * and the backend supports specific operational attributes,
1562 * add them to the attribute list
1564 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1565 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1566 *ap = slap_operational_subschemaSubentry( op->o_bd );
1568 ap = &(*ap)->a_next;
1571 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) && op->o_bd &&
1572 op->o_bd->be_operational != NULL )
1576 a = rs->sr_operational_attrs;
1577 rs->sr_operational_attrs = NULL;
1578 rc = op->o_bd->be_operational( op, rs );
1579 *ap = rs->sr_operational_attrs;
1581 rs->sr_operational_attrs = a;
1584 for ( ; *ap; ap = &(*ap)->a_next )
1585 /* just count them */ ;