1 /* backend.c - routines for dealing with back-end databases */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/string.h>
33 #include <ac/socket.h>
43 #include "slapi/slapi.h"
45 static void init_group_pblock( Operation *op, Entry *target,
46 Entry *e, struct berval *op_ndn, AttributeDescription *group_at );
47 static int call_group_preop_plugins( Operation *op );
48 static void call_group_postop_plugins( Operation *op );
49 #endif /* LDAP_SLAPI */
52 * If a module is configured as dynamic, its header should not
53 * get included into slapd. While this is a general rule and does
54 * not have much of an effect in UNIX, this rule should be adhered
55 * to for Windows, where dynamic object code should not be implicitly
56 * imported into slapd without appropriate __declspec(dllimport) directives.
59 #if SLAPD_BDB == SLAPD_MOD_STATIC
60 #include "back-bdb/external.h"
62 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
63 #include "back-dnssrv/external.h"
65 #if SLAPD_HDB == SLAPD_MOD_STATIC
66 #include "back-hdb/external.h"
68 #if SLAPD_LDAP == SLAPD_MOD_STATIC
69 #include "back-ldap/external.h"
71 #if SLAPD_LDBM == SLAPD_MOD_STATIC
72 #include "back-ldbm/external.h"
74 #if SLAPD_META == SLAPD_MOD_STATIC
75 #include "back-meta/external.h"
77 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
78 #include "back-monitor/external.h"
80 #if SLAPD_NULL == SLAPD_MOD_STATIC
81 #include "back-null/external.h"
83 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
84 #include "back-passwd/external.h"
86 #if SLAPD_PERL == SLAPD_MOD_STATIC
87 #include "back-perl/external.h"
89 #if SLAPD_RELAY == SLAPD_MOD_STATIC
90 #include "back-relay/external.h"
92 #if SLAPD_SHELL == SLAPD_MOD_STATIC
93 #include "back-shell/external.h"
95 #if SLAPD_TCL == SLAPD_MOD_STATIC
96 #include "back-tcl/external.h"
98 #if SLAPD_SQL == SLAPD_MOD_STATIC
99 #include "back-sql/external.h"
101 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
102 #include "private/external.h"
105 static BackendInfo binfo[] = {
106 #if SLAPD_BDB == SLAPD_MOD_STATIC
107 {"bdb", bdb_initialize},
109 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
110 {"dnssrv", dnssrv_back_initialize},
112 #if SLAPD_HDB == SLAPD_MOD_STATIC
113 {"hdb", hdb_initialize},
115 #if SLAPD_LDAP == SLAPD_MOD_STATIC
116 {"ldap", ldap_back_initialize},
118 #if SLAPD_LDBM == SLAPD_MOD_STATIC
119 {"ldbm", ldbm_back_initialize},
121 #if SLAPD_META == SLAPD_MOD_STATIC
122 {"meta", meta_back_initialize},
124 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
125 {"monitor", monitor_back_initialize},
127 #if SLAPD_NULL == SLAPD_MOD_STATIC
128 {"null", null_back_initialize},
130 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
131 {"passwd", passwd_back_initialize},
133 #if SLAPD_PERL == SLAPD_MOD_STATIC
134 {"perl", perl_back_initialize},
136 #if SLAPD_RELAY == SLAPD_MOD_STATIC
137 {"relay", relay_back_initialize},
139 #if SLAPD_SHELL == SLAPD_MOD_STATIC
140 {"shell", shell_back_initialize},
142 #if SLAPD_TCL == SLAPD_MOD_STATIC
143 {"tcl", tcl_back_initialize},
145 #if SLAPD_SQL == SLAPD_MOD_STATIC
146 {"sql", sql_back_initialize},
148 /* for any private backend */
149 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
150 {"private", private_back_initialize},
155 int nBackendInfo = 0;
156 BackendInfo *backendInfo = NULL;
159 BackendDB *backendDB = NULL;
161 ldap_pvt_thread_pool_t syncrepl_pool;
162 int syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
164 int backend_init(void)
168 ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
170 if((nBackendInfo != 0) || (backendInfo != NULL)) {
171 /* already initialized */
173 LDAP_LOG( BACKEND, ERR,
174 "backend_init: backend already initialized\n", 0, 0, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "backend_init: already initialized.\n", 0, 0, 0 );
183 binfo[nBackendInfo].bi_type != NULL;
186 assert( binfo[nBackendInfo].bi_init );
188 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
192 LDAP_LOG( BACKEND, INFO,
193 "backend_init: initialized for type \"%s\"\n",
194 binfo[nBackendInfo].bi_type, 0, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "backend_init: initialized for type \"%s\"\n",
198 binfo[nBackendInfo].bi_type, 0, 0 );
200 /* destroy those we've already inited */
205 if ( binfo[nBackendInfo].bi_destroy ) {
206 binfo[nBackendInfo].bi_destroy(
207 &binfo[nBackendInfo] );
214 if ( nBackendInfo > 0) {
224 LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "backend_init: failed\n",
232 #endif /* SLAPD_MODULES */
235 int backend_add(BackendInfo *aBackendInfo)
239 if ( aBackendInfo->bi_init == NULL ) {
241 LDAP_LOG( BACKEND, ERR, "backend_add: "
242 "backend type \"%s\" does not have the (mandatory)init function\n",
243 aBackendInfo->bi_type, 0, 0 );
245 Debug( LDAP_DEBUG_ANY, "backend_add: "
246 "backend type \"%s\" does not have the (mandatory)init function\n",
247 aBackendInfo->bi_type, 0, 0 );
252 if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
254 LDAP_LOG( BACKEND, ERR,
255 "backend_add: initialization for type \"%s\" failed\n",
256 aBackendInfo->bi_type, 0, 0 );
258 Debug( LDAP_DEBUG_ANY,
259 "backend_add: initialization for type \"%s\" failed\n",
260 aBackendInfo->bi_type, 0, 0 );
265 /* now add the backend type to the Backend Info List */
267 BackendInfo *newBackendInfo = 0;
269 /* if backendInfo == binfo no deallocation of old backendInfo */
270 if (backendInfo == binfo) {
271 newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
272 AC_MEMCPY(newBackendInfo, backendInfo,
273 sizeof(BackendInfo) * nBackendInfo);
275 newBackendInfo = ch_realloc(backendInfo,
276 sizeof(BackendInfo) * (nBackendInfo + 1));
279 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
280 sizeof(BackendInfo));
281 backendInfo = newBackendInfo;
287 /* startup a specific backend database */
288 int backend_startup_one(Backend *be)
294 be->be_pending_csn_list = (struct be_pcl *)
295 ch_calloc( 1, sizeof( struct be_pcl ));
296 build_new_dn( &be->be_context_csn, be->be_nsuffix,
297 (struct berval *)&slap_ldapsync_cn_bv, NULL );
299 LDAP_TAILQ_INIT( be->be_pending_csn_list );
302 LDAP_LOG( BACKEND, DETAIL1, "backend_startup: starting \"%s\"\n",
303 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
306 Debug( LDAP_DEBUG_TRACE,
307 "backend_startup: starting \"%s\"\n",
308 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
311 if ( be->bd_info->bi_db_open ) {
312 rc = be->bd_info->bi_db_open( be );
315 LDAP_LOG( BACKEND, CRIT,
316 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
318 Debug( LDAP_DEBUG_ANY,
319 "backend_startup: bi_db_open failed! (%d)\n",
327 int backend_startup(Backend *be)
332 if( ! ( nBackendDB > 0 ) ) {
335 LDAP_LOG( BACKEND, INFO,
336 "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
338 Debug( LDAP_DEBUG_ANY,
339 "backend_startup: %d databases to startup.\n",
346 if ( be->bd_info->bi_open ) {
347 rc = be->bd_info->bi_open( be->bd_info );
350 LDAP_LOG( BACKEND, CRIT,
351 "backend_startup: bi_open failed!\n", 0, 0, 0 );
353 Debug( LDAP_DEBUG_ANY,
354 "backend_startup: bi_open failed!\n",
362 return backend_startup_one( be );
365 /* open frontend, if required */
366 if ( frontendDB->bd_info->bi_db_open ) {
367 rc = frontendDB->bd_info->bi_db_open( frontendDB );
370 LDAP_LOG( BACKEND, CRIT,
371 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
374 Debug( LDAP_DEBUG_ANY,
375 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
382 /* open each backend type */
383 for( i = 0; i < nBackendInfo; i++ ) {
384 if( backendInfo[i].bi_nDB == 0) {
385 /* no database of this type, don't open */
389 if( backendInfo[i].bi_open ) {
390 rc = backendInfo[i].bi_open(
394 LDAP_LOG( BACKEND, CRIT,
395 "backend_startup: bi_open %d failed!\n", i, 0, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "backend_startup: bi_open %d failed!\n",
406 ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
407 LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
408 LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
410 /* open each backend database */
411 for( i = 0; i < nBackendDB; i++ ) {
412 if ( backendDB[i].be_suffix == NULL ) {
414 LDAP_LOG( BACKEND, CRIT,
415 "backend_startup: warning, database %d (%s) "
417 i, backendDB[i].bd_info->bi_type, 0 );
419 Debug( LDAP_DEBUG_ANY,
420 "backend_startup: warning, database %d (%s) "
422 i, backendDB[i].bd_info->bi_type, 0 );
425 /* append global access controls */
426 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
428 rc = backend_startup_one( &backendDB[i] );
433 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
436 if ( !( backendDB[i].be_search && backendDB[i].be_add &&
437 backendDB[i].be_modify && backendDB[i].be_delete )) {
439 LDAP_LOG( BACKEND, CRIT,
440 "backend_startup: database(%d) does not support "
441 "operations required for syncrepl", i, 0, 0 );
443 Debug( LDAP_DEBUG_ANY,
444 "backend_startup: database(%d) does not support "
445 "operations required for syncrepl", i, 0, 0 );
450 LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
451 si->si_be = &backendDB[i];
453 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
454 ldap_pvt_runqueue_insert( &syncrepl_rq,
455 si->si_interval, do_syncrepl, (void *) si );
456 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
464 int backend_num( Backend *be )
468 if( be == NULL ) return -1;
470 for( i = 0; i < nBackendDB; i++ ) {
471 if( be == &backendDB[i] ) return i;
476 int backend_shutdown( Backend *be )
482 /* shutdown a specific backend database */
484 if ( be->bd_info->bi_nDB == 0 ) {
485 /* no database of this type, we never opened it */
489 if ( be->bd_info->bi_db_close ) {
490 be->bd_info->bi_db_close( be );
493 if( be->bd_info->bi_close ) {
494 be->bd_info->bi_close( be->bd_info );
500 /* close each backend database */
501 for( i = 0; i < nBackendDB; i++ ) {
502 if ( backendDB[i].bd_info->bi_db_close ) {
503 backendDB[i].bd_info->bi_db_close(
509 LDAP_LOG( BACKEND, NOTICE,
510 "backend_shutdown: bi_db_close %s failed!\n",
511 backendDB[i].be_type, 0, 0 );
513 Debug( LDAP_DEBUG_ANY,
514 "backend_close: bi_db_close %s failed!\n",
515 backendDB[i].be_type, 0, 0 );
520 /* close each backend type */
521 for( i = 0; i < nBackendInfo; i++ ) {
522 if( backendInfo[i].bi_nDB == 0 ) {
523 /* no database of this type */
527 if( backendInfo[i].bi_close ) {
528 backendInfo[i].bi_close(
533 /* close frontend, if required */
534 if ( frontendDB->bd_info->bi_db_close ) {
535 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
538 LDAP_LOG( BACKEND, CRIT,
539 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
542 Debug( LDAP_DEBUG_ANY,
543 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
552 int backend_destroy(void)
557 ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
559 /* destroy each backend database */
560 for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
561 if ( bd->bd_info->bi_db_destroy ) {
562 bd->bd_info->bi_db_destroy( bd );
564 ber_bvarray_free( bd->be_suffix );
565 ber_bvarray_free( bd->be_nsuffix );
566 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
567 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
568 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
569 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
570 acl_destroy( bd->be_acl, frontendDB->be_acl );
574 /* destroy each backend type */
575 for( i = 0; i < nBackendInfo; i++ ) {
576 if( backendInfo[i].bi_destroy ) {
577 backendInfo[i].bi_destroy(
583 if (backendInfo != binfo) {
586 #endif /* SLAPD_MODULES */
591 /* destroy frontend database */
593 if ( bd->bd_info->bi_db_destroy ) {
594 bd->bd_info->bi_db_destroy( bd );
596 ber_bvarray_free( bd->be_suffix );
597 ber_bvarray_free( bd->be_nsuffix );
598 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
599 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
600 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
601 acl_destroy( bd->be_acl, frontendDB->be_acl );
606 BackendInfo* backend_info(const char *type)
610 /* search for the backend type */
611 for( i = 0; i < nBackendInfo; i++ ) {
612 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
613 return &backendInfo[i];
626 BackendInfo *bi = backend_info(type);
630 fprintf( stderr, "Unrecognized database type (%s)\n", type );
636 backendDB = (BackendDB *) ch_realloc(
638 (nBackendDB + 1) * sizeof(Backend) );
640 memset( &backendDB[nbackends], '\0', sizeof(Backend) );
642 /* did realloc move our table? if so, fix up dependent pointers */
643 if ( be != backendDB ) {
645 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
646 be->be_pcl_mutexp = &be->be_pcl_mutex;
650 be = &backends[nbackends++];
653 be->be_def_limit = frontendDB->be_def_limit;
654 be->be_dfltaccess = frontendDB->be_dfltaccess;
656 be->be_restrictops = frontendDB->be_restrictops;
657 be->be_requires = frontendDB->be_requires;
658 be->be_ssf_set = frontendDB->be_ssf_set;
660 be->be_context_csn.bv_len = 0;
661 be->be_context_csn.bv_val = NULL;
662 be->be_pcl_mutexp = &be->be_pcl_mutex;
663 ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
665 LDAP_STAILQ_INIT( &be->be_syncinfo );
667 /* assign a default depth limit for alias deref */
668 be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH;
671 rc = bi->bi_db_init( be );
675 fprintf( stderr, "database init failed (%s)\n", type );
689 for ( i = 0; i < nbackends; i++ ) {
690 if ( backends[i].bd_info->bi_db_close ) {
691 (*backends[i].bd_info->bi_db_close)( &backends[i] );
695 if ( frontendDB->bd_info->bi_db_close ) {
696 (*frontendDB->bd_info->bi_db_close)( frontendDB );
707 ber_len_t len, dnlen = dn->bv_len;
710 for ( i = 0; i < nbackends; i++ ) {
711 for ( j = 0; backends[i].be_nsuffix != NULL &&
712 backends[i].be_nsuffix[j].bv_val != NULL; j++ )
714 if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
720 len = backends[i].be_nsuffix[j].bv_len;
723 /* suffix is longer than DN */
728 * input DN is normalized, so the separator check
729 * need not look at escaping
731 if ( len && len < dnlen &&
732 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
737 if ( strcmp( backends[i].be_nsuffix[j].bv_val,
738 &dn->bv_val[dnlen-len] ) == 0 )
743 if( manageDSAit && len == dnlen &&
744 !SLAP_GLUE_SUBORDINATE( be ) ) {
761 struct berval *bvsuffix )
766 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
769 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
778 be_isroot_dn( Backend *be, struct berval *ndn )
780 if ( !ndn->bv_len ) {
784 if ( !be->be_rootndn.bv_len ) {
788 return dn_match( &be->be_rootndn, ndn );
792 be_sync_update( Operation *op )
794 return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
798 be_slurp_update( Operation *op )
800 return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
801 be_isupdate_dn( op->o_bd, &op->o_ndn ));
805 be_shadow_update( Operation *op )
807 return ( SLAP_SHADOW( op->o_bd ) &&
808 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
812 be_isupdate_dn( Backend *be, struct berval *ndn )
814 if ( !ndn->bv_len ) return( 0 );
816 if ( !be->be_update_ndn.bv_len ) return( 0 );
818 return dn_match( &be->be_update_ndn, ndn );
822 be_root_dn( Backend *be )
824 return &be->be_rootdn;
828 be_isroot( Operation *op )
830 return be_isroot_dn( op->o_bd, &op->o_ndn );
834 be_isroot_pw( Operation *op )
838 if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
842 if( op->o_bd->be_rootpw.bv_len == 0 ) {
846 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
847 ldap_pvt_thread_mutex_lock( &passwd_mutex );
849 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
853 result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
855 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
857 lutil_passwd_sasl_conn = NULL;
859 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
871 if ( op->o_bd->be_release ) {
872 /* free and release entry from backend */
873 return op->o_bd->be_release( op, e, rw );
882 backend_unbind( Operation *op, SlapReply *rs )
886 for ( i = 0; i < nbackends; i++ ) {
887 #if defined( LDAP_SLAPI )
890 if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
891 slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
892 rc = slapi_int_call_plugins( &backends[i],
893 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
896 * A preoperation plugin failure will abort the
900 LDAP_LOG( OPERATION, INFO,
901 "do_bind: Unbind preoperation plugin failed\n",
904 Debug(LDAP_DEBUG_TRACE,
905 "do_bind: Unbind preoperation plugin failed\n",
911 #endif /* defined( LDAP_SLAPI ) */
913 if ( backends[i].be_unbind ) {
914 op->o_bd = &backends[i];
915 (*backends[i].be_unbind)( op, rs );
918 #if defined( LDAP_SLAPI )
919 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
920 SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
923 LDAP_LOG( OPERATION, INFO,
924 "do_unbind: Unbind postoperation plugins failed\n",
927 Debug(LDAP_DEBUG_TRACE,
928 "do_unbind: Unbind postoperation plugins failed\n",
932 #endif /* defined( LDAP_SLAPI ) */
939 backend_connection_init(
944 for ( i = 0; i < nbackends; i++ ) {
945 if ( backends[i].be_connection_init ) {
946 (*backends[i].be_connection_init)( &backends[i], conn);
954 backend_connection_destroy(
959 for ( i = 0; i < nbackends; i++ ) {
960 if ( backends[i].be_connection_destroy ) {
961 (*backends[i].be_connection_destroy)( &backends[i], conn);
969 backend_check_controls(
973 LDAPControl **ctrls = op->o_ctrls;
974 rs->sr_err = LDAP_SUCCESS;
977 for( ; *ctrls != NULL ; ctrls++ ) {
978 if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
979 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
981 rs->sr_text = "control unavailable in context";
982 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
992 backend_check_restrictions(
995 struct berval *opdata )
997 slap_mask_t restrictops;
998 slap_mask_t requires;
1000 slap_mask_t exopflag = 0;
1001 slap_ssf_set_t *ssf;
1007 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
1011 restrictops = op->o_bd->be_restrictops;
1012 requires = op->o_bd->be_requires;
1013 ssf = &op->o_bd->be_ssf_set;
1016 restrictops = frontendDB->be_restrictops;
1017 requires = frontendDB->be_requires;
1018 ssf = &frontendDB->be_ssf_set;
1021 switch( op->o_tag ) {
1023 opflag = SLAP_RESTRICT_OP_ADD;
1027 opflag = SLAP_RESTRICT_OP_BIND;
1030 case LDAP_REQ_COMPARE:
1031 opflag = SLAP_RESTRICT_OP_COMPARE;
1033 case LDAP_REQ_DELETE:
1035 opflag = SLAP_RESTRICT_OP_DELETE;
1037 case LDAP_REQ_EXTENDED:
1038 opflag = SLAP_RESTRICT_OP_EXTENDED;
1041 /* treat unspecified as a modify */
1042 opflag = SLAP_RESTRICT_OP_MODIFY;
1047 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1050 exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1054 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1055 exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1059 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1060 exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1064 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1065 exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1070 /* treat everything else as a modify */
1071 opflag = SLAP_RESTRICT_OP_MODIFY;
1075 case LDAP_REQ_MODIFY:
1077 opflag = SLAP_RESTRICT_OP_MODIFY;
1079 case LDAP_REQ_RENAME:
1081 opflag = SLAP_RESTRICT_OP_RENAME;
1083 case LDAP_REQ_SEARCH:
1084 opflag = SLAP_RESTRICT_OP_SEARCH;
1086 case LDAP_REQ_UNBIND:
1091 rs->sr_text = "restrict operations internal error";
1092 rs->sr_err = LDAP_OTHER;
1097 /* these checks don't apply to StartTLS */
1099 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1100 if( op->o_transport_ssf < ssf->sss_transport ) {
1101 rs->sr_text = op->o_transport_ssf
1102 ? "stronger transport confidentiality required"
1103 : "transport confidentiality required";
1107 if( op->o_tls_ssf < ssf->sss_tls ) {
1108 rs->sr_text = op->o_tls_ssf
1109 ? "stronger TLS confidentiality required"
1110 : "TLS confidentiality required";
1115 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1116 /* simple bind specific check */
1117 if( op->o_ssf < ssf->sss_simple_bind ) {
1118 rs->sr_text = op->o_ssf
1119 ? "stronger confidentiality required"
1120 : "confidentiality required";
1125 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1126 /* these checks don't apply to SASL bind */
1128 if( op->o_sasl_ssf < ssf->sss_sasl ) {
1129 rs->sr_text = op->o_sasl_ssf
1130 ? "stronger SASL confidentiality required"
1131 : "SASL confidentiality required";
1135 if( op->o_ssf < ssf->sss_ssf ) {
1136 rs->sr_text = op->o_ssf
1137 ? "stronger confidentiality required"
1138 : "confidentiality required";
1144 if( op->o_transport_ssf < ssf->sss_update_transport ) {
1145 rs->sr_text = op->o_transport_ssf
1146 ? "stronger transport confidentiality required for update"
1147 : "transport confidentiality required for update";
1151 if( op->o_tls_ssf < ssf->sss_update_tls ) {
1152 rs->sr_text = op->o_tls_ssf
1153 ? "stronger TLS confidentiality required for update"
1154 : "TLS confidentiality required for update";
1158 if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1159 rs->sr_text = op->o_sasl_ssf
1160 ? "stronger SASL confidentiality required for update"
1161 : "SASL confidentiality required for update";
1165 if( op->o_ssf < ssf->sss_update_ssf ) {
1166 rs->sr_text = op->o_ssf
1167 ? "stronger confidentiality required for update"
1168 : "confidentiality required for update";
1172 if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1173 op->o_ndn.bv_len == 0 )
1175 rs->sr_text = "modifications require authentication";
1176 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1180 #ifdef SLAP_X_LISTENER_MOD
1181 if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1182 /* no "w" mode means readonly */
1183 rs->sr_text = "modifications not allowed on this listener";
1184 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1187 #endif /* SLAP_X_LISTENER_MOD */
1192 /* these checks don't apply to Bind, StartTLS, or Unbind */
1194 if( requires & SLAP_REQUIRE_STRONG ) {
1195 /* should check mechanism */
1196 if( ( op->o_transport_ssf < ssf->sss_transport
1197 && op->o_authtype == LDAP_AUTH_SIMPLE )
1198 || op->o_dn.bv_len == 0 )
1200 rs->sr_text = "strong(er) authentication required";
1201 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1206 if( requires & SLAP_REQUIRE_SASL ) {
1207 if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1208 rs->sr_text = "SASL authentication required";
1209 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1214 if( requires & SLAP_REQUIRE_AUTHC ) {
1215 if( op->o_dn.bv_len == 0 ) {
1216 rs->sr_text = "authentication required";
1217 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1222 if( requires & SLAP_REQUIRE_BIND ) {
1224 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1225 version = op->o_conn->c_protocol;
1226 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1229 /* no bind has occurred */
1230 rs->sr_text = "BIND required";
1231 rs->sr_err = LDAP_OPERATIONS_ERROR;
1236 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1237 if( op->o_protocol < LDAP_VERSION3 ) {
1238 /* no bind has occurred */
1239 rs->sr_text = "operation restricted to LDAPv3 clients";
1240 rs->sr_err = LDAP_OPERATIONS_ERROR;
1245 #ifdef SLAP_X_LISTENER_MOD
1246 if ( !starttls && op->o_dn.bv_len == 0 ) {
1247 if ( op->o_conn->c_listener &&
1248 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1250 /* no "x" mode means bind required */
1251 rs->sr_text = "bind required on this listener";
1252 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1257 if ( !starttls && !updateop ) {
1258 if ( op->o_conn->c_listener &&
1259 !( op->o_conn->c_listener->sl_perms &
1260 ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1262 /* no "r" mode means no read */
1263 rs->sr_text = "read not allowed on this listener";
1264 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1268 #endif /* SLAP_X_LISTENER_MOD */
1272 if( ( restrictops & opflag )
1273 || ( exopflag && ( restrictops & exopflag ) ) ) {
1274 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1275 rs->sr_text = "read operations restricted";
1276 } else if ( restrictops & exopflag ) {
1277 rs->sr_text = "extended operation restricted";
1279 rs->sr_text = "operation restricted";
1281 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1285 rs->sr_err = LDAP_SUCCESS;
1289 int backend_check_referrals( Operation *op, SlapReply *rs )
1291 rs->sr_err = LDAP_SUCCESS;
1293 if( op->o_bd->be_chk_referrals ) {
1294 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1296 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1297 send_ldap_result( op, rs );
1309 AttributeDescription *at,
1317 if (op->o_bd == NULL) {
1318 rc = LDAP_NO_SUCH_OBJECT;
1319 } else if ( op->o_bd->be_fetch ) {
1320 rc = ( op->o_bd->be_fetch )( op, ndn,
1323 rc = LDAP_UNWILLING_TO_PERFORM;
1332 struct berval *gr_ndn,
1333 struct berval *op_ndn,
1334 ObjectClass *group_oc,
1335 AttributeDescription *group_at )
1341 Backend *be = op->o_bd;
1343 if ( op->o_abandon ) return SLAPD_ABANDON;
1345 op->o_bd = select_backend( gr_ndn, 0, 0 );
1347 for (g = op->o_groups; g; g=g->ga_next) {
1348 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1349 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1351 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1360 if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1364 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1368 if ( op->o_pb != NULL ) {
1369 init_group_pblock( op, target, e, op_ndn, group_at );
1371 rc = call_group_preop_plugins( op );
1372 if ( rc == LDAP_SUCCESS ) {
1376 #endif /* LDAP_SLAPI */
1378 a = attr_find( e->e_attrs, group_at );
1380 /* If the attribute is a subtype of labeledURI, treat this as
1381 * a dynamic group ala groupOfURLs
1383 if (is_at_subtype( group_at->ad_type,
1384 slap_schema.si_ad_labeledURI->ad_type ) )
1388 struct berval bv, nbase;
1391 Backend *b2 = op->o_bd;
1393 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1396 op->o_bd = select_backend( op_ndn, 0, 0 );
1397 rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1402 for (i=0; a->a_vals[i].bv_val; i++) {
1403 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1408 nbase.bv_val = NULL;
1409 /* host part must be empty */
1410 /* attrs and extensions parts must be empty */
1411 if (( ludp->lud_host && *ludp->lud_host ) ||
1412 ludp->lud_attrs || ludp->lud_exts )
1416 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1417 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1418 op->o_tmpmemctx ) != LDAP_SUCCESS )
1422 switch(ludp->lud_scope) {
1423 case LDAP_SCOPE_BASE:
1424 if ( !dn_match( &nbase, op_ndn )) goto loopit;
1426 case LDAP_SCOPE_ONELEVEL:
1427 dnParent(op_ndn, &bv );
1428 if ( !dn_match( &nbase, &bv )) goto loopit;
1430 case LDAP_SCOPE_SUBTREE:
1431 if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1433 #ifdef LDAP_SCOPE_SUBORDINATE
1434 case LDAP_SCOPE_SUBORDINATE:
1435 if ( dn_match( &nbase, op_ndn ) &&
1436 !dnIsSuffix(op_ndn, &nbase ))
1442 filter = str2filter_x( op, ludp->lud_filter );
1444 if ( test_filter( NULL, user, filter ) ==
1449 filter_free_x( op, filter );
1452 ldap_free_urldesc( ludp );
1453 if ( nbase.bv_val ) {
1454 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1456 if ( rc == 0 ) break;
1458 if ( user != target ) {
1459 be_entry_release_r( op, user );
1464 rc = value_find_ex( group_at,
1465 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1466 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1467 a->a_nvals, op_ndn, op->o_tmpmemctx );
1470 rc = LDAP_NO_SUCH_ATTRIBUTE;
1473 be_entry_release_r( op, e );
1476 rc = LDAP_NO_SUCH_OBJECT;
1480 if ( op->o_pb ) call_group_postop_plugins( op );
1481 #endif /* LDAP_SLAPI */
1483 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1484 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1486 g->ga_be = op->o_bd;
1487 g->ga_oc = group_oc;
1488 g->ga_at = group_at;
1490 g->ga_len = gr_ndn->bv_len;
1491 strcpy(g->ga_ndn, gr_ndn->bv_val);
1492 g->ga_next = op->o_groups;
1501 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1505 BerVarray *vals = (BerVarray *)c->cac_private;
1506 Operation *op = NULL;
1509 slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1514 if ( op->o_conn && access_allowed( op,
1515 e, a->a_desc, NULL, ACL_AUTH,
1516 &c->cac_acl_state ) == 0 ) {
1520 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1522 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1524 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1525 if ( op->o_conn && access_allowed( op,
1528 ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1532 &a->a_nvals[i], op->o_tmpmemctx );
1533 if (v[j].bv_val ) j++;
1537 op->o_tmpfree( v, op->o_tmpmemctx );
1549 #endif /* LDAP_SLAPI */
1556 AttributeDescription *entry_at,
1558 slap_access_t access )
1562 int i, j, rc = LDAP_SUCCESS;
1563 AccessControlState acl_state = ACL_STATE_INIT;
1564 Backend *be = op->o_bd;
1566 op->o_bd = select_backend( edn, 0, 0 );
1568 if ( target && dn_match( &target->e_nname, edn ) ) {
1571 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1575 a = attr_find( e->e_attrs, entry_at );
1579 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1580 e, entry_at, NULL, access,
1581 &acl_state ) == 0 ) {
1582 rc = LDAP_INSUFFICIENT_ACCESS;
1586 for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1588 v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1590 for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1591 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1594 access, &acl_state ) == 0 ) {
1598 &a->a_nvals[i], op->o_tmpmemctx );
1599 if (v[j].bv_val ) j++;
1602 op->o_tmpfree( v, op->o_tmpmemctx );
1604 rc = LDAP_INSUFFICIENT_ACCESS;
1613 else if ( op->o_pb ) {
1614 /* try any computed attributes */
1615 computed_attr_context ctx;
1616 AttributeName aname;
1618 slapi_int_pblock_set_operation( op->o_pb, op );
1620 ctx.cac_pb = op->o_pb;
1621 ctx.cac_attrs = NULL;
1622 ctx.cac_userattrs = 0;
1623 ctx.cac_opattrs = 0;
1624 ctx.cac_acl_state = acl_state;
1625 ctx.cac_private = (void *)vals;
1627 if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1628 rc = LDAP_INSUFFICIENT_ACCESS;
1632 #endif /* LDAP_SLAPI */
1633 freeit: if (e != target ) {
1634 be_entry_release_r( op, e );
1642 int backend_operational(
1649 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1650 /* just count them */ ;
1653 * If operational attributes (allegedly) are required,
1654 * and the backend supports specific operational attributes,
1655 * add them to the attribute list
1657 if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1658 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1659 *ap = slap_operational_subschemaSubentry( op->o_bd );
1661 ap = &(*ap)->a_next;
1664 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) && op->o_bd &&
1665 op->o_bd->be_operational != NULL )
1669 a = rs->sr_operational_attrs;
1670 rs->sr_operational_attrs = NULL;
1671 rc = op->o_bd->be_operational( op, rs );
1672 *ap = rs->sr_operational_attrs;
1674 rs->sr_operational_attrs = a;
1677 for ( ; *ap; ap = &(*ap)->a_next )
1678 /* just count them */ ;
1685 static void init_group_pblock( Operation *op, Entry *target,
1686 Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1688 slapi_int_pblock_set_operation( op->o_pb, op );
1689 slapi_pblock_set( op->o_pb, SLAPI_X_GROUP_ENTRY, (void *)e );
1690 slapi_pblock_set( op->o_pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1691 slapi_pblock_set( op->o_pb, SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1692 slapi_pblock_set( op->o_pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1695 static int call_group_preop_plugins( Operation *op )
1699 rc = slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1701 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1702 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1704 rc = LDAP_NO_SUCH_ATTRIBUTE;
1713 static void call_group_postop_plugins( Operation *op )
1715 (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1717 #endif /* LDAP_SLAPI */