openldap/servers/slapd/overlays/chain.c

495 lines
12 KiB
C
Raw Normal View History

/* chain.c - chain LDAP operations */
/* $OpenLDAP$ */
2003-11-27 14:35:14 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2005-01-02 04:49:32 +08:00
* Copyright 2003-2005 The OpenLDAP Foundation.
2003-12-09 01:41:40 +08:00
* Portions Copyright 2003 Howard Chu.
2003-11-27 14:35:14 +08:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by the Howard Chu for inclusion
* in OpenLDAP Software.
*/
#include "portable.h"
2004-03-19 02:59:46 +08:00
#if defined(SLAPD_LDAP)
#ifdef SLAPD_OVER_CHAIN
#include <stdio.h>
#include <ac/string.h>
#include <ac/socket.h>
#include "slap.h"
2004-03-19 02:59:46 +08:00
#include "../back-ldap/back-ldap.h"
static BackendInfo *lback;
2004-04-06 01:31:27 +08:00
static int
ldap_chain_chk_referrals( Operation *op, SlapReply *rs )
{
return LDAP_SUCCESS;
}
static int
ldap_chain_operational( Operation *op, SlapReply *rs )
{
/* trap entries generated by back-ldap.
* FIXME: we need a better way to recognize them; a cleaner
* solution would be to be able to intercept the response
* of be_operational(), so that we can divert only those
* calls that fail because operational attributes were
* requested for entries that do not belong to the underlying
* database. This fix is likely to intercept also entries
* generated by back-perl and so. */
if ( rs->sr_entry->e_private == NULL ) {
return 0;
}
return SLAP_CB_CONTINUE;
}
static int
ldap_chain_cb_response( Operation *op, SlapReply *rs )
{
assert( op->o_tag == LDAP_REQ_SEARCH );
if ( rs->sr_type == REP_SEARCH ) {
Attribute **ap = &rs->sr_entry->e_attrs;
for ( ; *ap != NULL; ap = &(*ap)->a_next ) {
/* will be generated later by frontend
* (a cleaner solution would be that
* the frontend checks if it already exists */
if ( ad_cmp( (*ap)->a_desc, slap_schema.si_ad_entryDN ) == 0 )
{
Attribute *a = *ap;
*ap = (*ap)->a_next;
attr_free( a );
/* there SHOULD be one only! */
break;
}
}
return SLAP_CB_CONTINUE;
}
return 0;
}
static int
ldap_chain_response( Operation *op, SlapReply *rs )
{
2004-04-06 01:31:27 +08:00
slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
void *private = op->o_bd->be_private;
slap_callback *sc = op->o_callback;
LDAPControl **prev = op->o_ctrls;
LDAPControl **ctrls = NULL, authz;
int i, nctrls, rc = 0;
int cache = op->o_do_not_cache;
char *authzid = NULL;
BerVarray ref;
struct berval ndn = op->o_ndn;
struct ldapinfo li, *lip = (struct ldapinfo *)on->on_bi.bi_private;
2004-03-21 00:35:55 +08:00
if ( rs->sr_err != LDAP_REFERRAL && rs->sr_type != REP_SEARCHREF )
return SLAP_CB_CONTINUE;
2003-06-11 14:32:24 +08:00
ref = rs->sr_ref;
2004-04-06 01:31:27 +08:00
rs->sr_ref = NULL;
2003-06-11 14:32:24 +08:00
op->o_callback = NULL;
2004-04-06 01:31:27 +08:00
if ( lip->url == NULL ) {
2004-12-12 02:36:31 +08:00
/* if we parse the URI then by no means
* we can cache stuff or reuse connections,
* because in back-ldap there's no caching
* based on the URI value, which is supposed
* to be set once for all (correct?) */
op->o_do_not_cache = 1;
/* FIXME: we're setting the URI of the first referral;
* what if there are more? Is this something we should
* worry about? */
2004-04-06 01:31:27 +08:00
li = *lip;
op->o_bd->be_private = &li;
if ( rs->sr_type != REP_SEARCHREF ) {
LDAPURLDesc *srv;
char *save_dn;
/* parse reference and use
* proto://[host][:port]/ only */
2004-04-06 01:31:27 +08:00
rc = ldap_url_parse_ext( ref[0].bv_val, &srv );
if ( rc != LDAP_URL_SUCCESS) {
2004-04-06 01:31:27 +08:00
/* error */
return 1;
}
/* remove DN essentially because later on
* ldap_initialize() will parse the URL
* as a comma-separated URL list */
save_dn = srv->lud_dn;
srv->lud_dn = "";
srv->lud_scope = LDAP_SCOPE_DEFAULT;
2004-04-06 01:31:27 +08:00
li.url = ldap_url_desc2str( srv );
srv->lud_dn = save_dn;
ldap_free_urldesc( srv );
2004-04-06 01:31:27 +08:00
if ( li.url == NULL ) {
/* error */
return 1;
}
}
} else {
op->o_bd->be_private = on->on_bi.bi_private;
}
2003-06-12 06:35:31 +08:00
/* Chaining is performed by a privileged user on behalf
* of a normal user, using the ProxyAuthz control. However,
* Binds are done separately, on an anonymous session.
*/
2003-06-11 14:32:24 +08:00
if ( op->o_tag != LDAP_REQ_BIND ) {
for ( i = 0; prev && prev[i]; i++ )
/* count and set prev to the last one */ ;
2003-06-11 14:32:24 +08:00
nctrls = i;
/* Add an extra NULL slot */
if ( !prev ) {
i++;
}
2003-06-11 14:32:24 +08:00
ctrls = op->o_tmpalloc((i + 1)*sizeof(LDAPControl *),
2003-06-11 14:32:24 +08:00
op->o_tmpmemctx);
for ( i = 0; i < nctrls; i++ ) {
2003-06-11 14:32:24 +08:00
ctrls[i] = prev[i];
}
2003-06-11 14:32:24 +08:00
ctrls[nctrls] = &authz;
ctrls[nctrls + 1] = NULL;
2003-06-11 14:32:24 +08:00
authz.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
authz.ldctl_iscritical = 1;
authz.ldctl_value = op->o_dn;
if ( !BER_BVISEMPTY( &op->o_dn ) ) {
authzid = op->o_tmpalloc( op->o_dn.bv_len + STRLENOF("dn:"),
2003-06-11 14:32:24 +08:00
op->o_tmpmemctx );
2004-04-06 01:31:27 +08:00
strcpy(authzid, "dn:");
strcpy(authzid + STRLENOF("dn:"), op->o_dn.bv_val);
authz.ldctl_value.bv_len = op->o_dn.bv_len + STRLENOF("dn:");
2003-06-11 14:32:24 +08:00
authz.ldctl_value.bv_val = authzid;
}
op->o_ctrls = ctrls;
2003-06-12 06:35:31 +08:00
op->o_ndn = op->o_bd->be_rootndn;
2003-06-11 14:32:24 +08:00
}
switch ( op->o_tag ) {
2003-06-12 06:35:31 +08:00
case LDAP_REQ_BIND: {
2004-04-06 01:31:27 +08:00
struct berval rndn = op->o_req_ndn;
Connection *conn = op->o_conn;
2003-06-12 06:35:31 +08:00
op->o_req_ndn = slap_empty_bv;
2004-04-06 01:31:27 +08:00
2003-06-12 06:35:31 +08:00
op->o_conn = NULL;
rc = lback->bi_op_bind( op, rs );
2003-06-12 06:35:31 +08:00
op->o_req_ndn = rndn;
op->o_conn = conn;
}
break;
case LDAP_REQ_ADD:
{
int cleanup_attrs = 0;
if ( op->ora_e->e_attrs == NULL ) {
char textbuf[ SLAP_TEXT_BUFLEN ];
size_t textlen = sizeof( textbuf );
/* global overlay; create entry */
/* NOTE: this is a hack to use the chain overlay
* as global. I expect to be able to remove this
* soon by using slap_mods2entry() earlier in
* do_add(), adding the operational attrs later
* if required. */
rs->sr_err = slap_mods2entry( op->ora_modlist,
&op->ora_e, 0, 1,
&rs->sr_text, textbuf, textlen );
if ( rs->sr_err != LDAP_SUCCESS ) {
send_ldap_result( op, rs );
rc = 1;
break;
}
}
rc = lback->bi_op_add( op, rs );
if ( cleanup_attrs ) {
attrs_free( op->ora_e->e_attrs );
op->ora_e->e_attrs = NULL;
}
break;
}
case LDAP_REQ_DELETE:
rc = lback->bi_op_delete( op, rs );
break;
case LDAP_REQ_MODRDN:
rc = lback->bi_op_modrdn( op, rs );
break;
case LDAP_REQ_MODIFY:
rc = lback->bi_op_modify( op, rs );
break;
case LDAP_REQ_COMPARE:
rc = lback->bi_op_compare( op, rs );
break;
case LDAP_REQ_SEARCH:
2004-03-21 00:35:55 +08:00
if ( rs->sr_type == REP_SEARCHREF ) {
2004-04-06 01:31:27 +08:00
struct berval *curr = ref,
2004-03-21 00:35:55 +08:00
odn = op->o_req_dn,
ondn = op->o_req_ndn;
slap_callback sc2 = { 0 };
int tmprc = 0;
ber_len_t refcnt = 0;
BerVarray newref = NULL;
sc2.sc_response = ldap_chain_cb_response;
op->o_callback = &sc2;
2004-03-21 00:35:55 +08:00
rs->sr_type = REP_SEARCH;
/* copy the private info because we need to modify it */
for ( ; !BER_BVISNULL( &curr[0] ); curr++ ) {
2004-03-21 00:35:55 +08:00
LDAPURLDesc *srv;
char *save_dn;
2004-03-21 00:35:55 +08:00
/* parse reference and use
* proto://[host][:port]/ only */
tmprc = ldap_url_parse_ext( curr[0].bv_val, &srv );
if ( tmprc != LDAP_URL_SUCCESS ) {
2004-03-21 00:35:55 +08:00
/* error */
2004-04-06 01:31:27 +08:00
rc = 1;
goto end_of_searchref;
2004-03-21 00:35:55 +08:00
}
/* remove DN essentially because later on
* ldap_initialize() will parse the URL
* as a comma-separated URL list */
save_dn = srv->lud_dn;
2004-03-21 00:35:55 +08:00
srv->lud_dn = "";
srv->lud_scope = LDAP_SCOPE_DEFAULT;
2004-03-21 00:35:55 +08:00
li.url = ldap_url_desc2str( srv );
if ( li.url != NULL ) {
ber_str2bv_x( save_dn, 0, 1, &op->o_req_dn,
op->o_tmpmemctx );
ber_dupbv_x( &op->o_req_ndn, &op->o_req_dn,
op->o_tmpmemctx );
}
srv->lud_dn = save_dn;
ldap_free_urldesc( srv );
2004-03-21 00:35:55 +08:00
if ( li.url == NULL ) {
/* error */
2004-04-06 01:31:27 +08:00
rc = 1;
goto end_of_searchref;
2004-03-21 00:35:55 +08:00
}
2004-03-21 00:35:55 +08:00
/* FIXME: should we also copy filter and scope?
* according to RFC3296, no */
tmprc = lback->bi_op_search( op, rs );
2004-03-21 00:35:55 +08:00
2004-04-06 01:31:27 +08:00
ldap_memfree( li.url );
li.url = NULL;
op->o_tmpfree( op->o_req_dn.bv_val,
op->o_tmpmemctx );
op->o_tmpfree( op->o_req_ndn.bv_val,
op->o_tmpmemctx );
2004-03-21 00:35:55 +08:00
if ( tmprc ) {
2004-03-21 00:35:55 +08:00
/* error */
2004-04-06 01:31:27 +08:00
rc = 1;
goto end_of_searchref;
2004-03-21 00:35:55 +08:00
}
if ( rs->sr_err != LDAP_SUCCESS ) {
/* if search was not successful,
* at least return the referral! */
/* FIXME: assumes referrals
* are always created via
* referral_rewrite() and freed via
* ber_bvarray_free( rs->sr_ref ) */
newref = ch_realloc( newref, sizeof( struct berval ) * (refcnt + 2) );
ber_dupbv( &newref[ refcnt ], &curr[ 0 ] );
refcnt++;
BER_BVZERO( &newref[ refcnt ] );
}
2004-03-21 00:35:55 +08:00
}
2004-04-06 01:31:27 +08:00
end_of_searchref:;
2004-03-21 00:35:55 +08:00
op->o_req_dn = odn;
op->o_req_ndn = ondn;
rs->sr_type = REP_SEARCHREF;
rs->sr_entry = NULL;
/* if the error was bad, it was already returned
* by back-ldap; destroy the referrals left;
* otherwise, let the frontend return them. */
if ( newref ) {
if ( rc == 0 ) {
rc = SLAP_CB_CONTINUE;
if ( ref != default_referral ) {
ber_bvarray_free( ref );
}
ref = newref;
} else {
ber_bvarray_free( newref );
}
}
2004-03-21 00:35:55 +08:00
} else {
rc = lback->bi_op_search( op, rs );
2004-03-21 00:35:55 +08:00
}
break;
case LDAP_REQ_EXTENDED:
rc = lback->bi_extended( op, rs );
break;
default:
rc = SLAP_CB_CONTINUE;
break;
}
op->o_do_not_cache = cache;
op->o_ctrls = prev;
op->o_bd->be_private = private;
2003-06-11 14:32:24 +08:00
op->o_callback = sc;
2003-06-12 06:35:31 +08:00
op->o_ndn = ndn;
if ( ctrls ) {
op->o_tmpfree( ctrls, op->o_tmpmemctx );
}
if ( authzid ) {
op->o_tmpfree( authzid, op->o_tmpmemctx );
}
2003-06-11 14:32:24 +08:00
rs->sr_ref = ref;
if ( lip->url == NULL && li.url != NULL ) {
2004-04-06 01:31:27 +08:00
ldap_memfree( li.url );
}
return rc;
}
static int
ldap_chain_config(
BackendDB *be,
const char *fname,
int lineno,
int argc,
char **argv
)
{
2004-03-19 02:59:46 +08:00
slap_overinst *on = (slap_overinst *) be->bd_info;
void *private = be->be_private;
char *argv0 = NULL;
int rc;
be->be_private = on->on_bi.bi_private;
2004-03-19 02:59:46 +08:00
if ( strncasecmp( argv[ 0 ], "chain-", sizeof( "chain-" ) - 1 ) == 0 ) {
argv0 = argv[ 0 ];
argv[ 0 ] = &argv[ 0 ][ sizeof( "chain-" ) - 1 ];
}
rc = lback->bi_db_config( be, fname, lineno, argc, argv );
2004-03-19 02:59:46 +08:00
if ( argv0 ) {
argv[ 0 ] = argv0;
}
be->be_private = private;
return rc;
}
static int
ldap_chain_init(
BackendDB *be
)
{
slap_overinst *on = (slap_overinst *) be->bd_info;
void *private = be->be_private;
int rc;
be->be_private = NULL;
rc = lback->bi_db_init( be );
on->on_bi.bi_private = be->be_private;
be->be_private = private;
2004-04-06 01:31:27 +08:00
return rc;
}
static int
ldap_chain_destroy(
BackendDB *be
)
{
slap_overinst *on = (slap_overinst *) be->bd_info;
void *private = be->be_private;
int rc;
be->be_private = on->on_bi.bi_private;
rc = lback->bi_db_destroy( be );
on->on_bi.bi_private = be->be_private;
be->be_private = private;
return rc;
}
static slap_overinst ldapchain;
int
chain_init()
{
lback = backend_info( "ldap" );
if ( !lback ) {
return -1;
}
ldapchain.on_bi.bi_type = "chain";
ldapchain.on_bi.bi_db_init = ldap_chain_init;
ldapchain.on_bi.bi_db_config = ldap_chain_config;
ldapchain.on_bi.bi_db_destroy = ldap_chain_destroy;
/* ... otherwise the underlying backend's function would be called,
* likely passing an invalid entry; on the contrary, the requested
* operational attributes should have been returned while chasing
* the referrals. This all in all is a bit messy, because part
* of the operational attributes are generated by they backend;
* part by the frontend; back-ldap should receive all the available
* ones from the remote server, but then, on it own, it strips those
* it assumes will be (re)generated by the frontend (e.g.
* subschemaSubentry.) */
ldapchain.on_bi.bi_operational = ldap_chain_operational;
ldapchain.on_response = ldap_chain_response;
2004-04-06 01:31:27 +08:00
ldapchain.on_bi.bi_chk_referrals = ldap_chain_chk_referrals;
return overlay_register( &ldapchain );
}
2004-03-19 02:59:46 +08:00
#if SLAPD_OVER_CHAIN == SLAPD_MOD_DYNAMIC
int init_module(int argc, char *argv[]) {
return chain_init();
}
#endif /* SLAPD_OVER_CHAIN == SLAPD_MOD_DYNAMIC */
#endif /* SLAPD_OVER_CHAIN */
#endif /* ! defined(SLAPD_LDAP) */