2001-10-26 10:05:14 +08:00
|
|
|
/* referral.c - muck with referrals */
|
|
|
|
/* $OpenLDAP$ */
|
2003-11-27 09:17:14 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2010-04-14 06:17:29 +08:00
|
|
|
* Copyright 1998-2010 The OpenLDAP Foundation.
|
2003-11-27 09:17: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>.
|
2001-10-26 10:05:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine generates the DN appropriate to return in
|
|
|
|
* an LDAP referral.
|
|
|
|
*/
|
|
|
|
static char * referral_dn_muck(
|
|
|
|
const char * refDN,
|
2002-08-29 16:47:53 +08:00
|
|
|
struct berval * baseDN,
|
|
|
|
struct berval * targetDN )
|
2001-10-26 10:05:14 +08:00
|
|
|
{
|
2002-01-16 11:01:19 +08:00
|
|
|
int rc;
|
|
|
|
struct berval bvin;
|
2004-04-07 12:11:43 +08:00
|
|
|
struct berval nrefDN = BER_BVNULL;
|
|
|
|
struct berval nbaseDN = BER_BVNULL;
|
|
|
|
struct berval ntargetDN = BER_BVNULL;
|
2001-10-26 10:05:14 +08:00
|
|
|
|
|
|
|
if( !baseDN ) {
|
|
|
|
/* no base, return target */
|
2002-08-29 16:47:53 +08:00
|
|
|
return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( refDN ) {
|
2002-01-16 11:01:19 +08:00
|
|
|
bvin.bv_val = (char *)refDN;
|
|
|
|
bvin.bv_len = strlen( refDN );
|
|
|
|
|
2003-04-30 02:13:10 +08:00
|
|
|
rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
|
2002-01-16 11:01:19 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
/* Invalid refDN */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !targetDN ) {
|
|
|
|
/* continuation reference
|
|
|
|
* if refDN present return refDN
|
|
|
|
* else return baseDN
|
|
|
|
*/
|
2002-08-29 16:47:53 +08:00
|
|
|
return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2003-04-30 02:13:10 +08:00
|
|
|
rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
|
2002-01-16 11:01:19 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
|
|
|
/* Invalid targetDN */
|
|
|
|
ch_free( nrefDN.bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
if( nrefDN.bv_len ) {
|
2003-04-30 02:13:10 +08:00
|
|
|
rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
|
2002-01-16 11:01:19 +08:00
|
|
|
if( rc != LDAP_SUCCESS ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
/* Invalid baseDN */
|
2002-01-16 11:01:19 +08:00
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
ch_free( ntargetDN.bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-08-29 16:43:23 +08:00
|
|
|
if( dn_match( &nbaseDN, &nrefDN ) ) {
|
2002-01-16 11:01:19 +08:00
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
ch_free( nbaseDN.bv_val );
|
|
|
|
return ntargetDN.bv_val;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2002-01-16 11:01:19 +08:00
|
|
|
struct berval muck;
|
|
|
|
|
|
|
|
if( ntargetDN.bv_len < nbaseDN.bv_len ) {
|
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
ch_free( nbaseDN.bv_val );
|
|
|
|
return ntargetDN.bv_val;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
rc = strcasecmp(
|
|
|
|
&ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
|
|
|
|
nbaseDN.bv_val );
|
|
|
|
if( rc ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
/* target not subordinate to base */
|
2002-01-16 11:01:19 +08:00
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
ch_free( nbaseDN.bv_val );
|
|
|
|
return ntargetDN.bv_val;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
|
2006-04-07 05:44:05 +08:00
|
|
|
muck.bv_val = ch_malloc( muck.bv_len + 1 );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
strncpy( muck.bv_val, ntargetDN.bv_val,
|
|
|
|
ntargetDN.bv_len-nbaseDN.bv_len );
|
|
|
|
strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
|
|
|
|
nrefDN.bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
ch_free( nbaseDN.bv_val );
|
|
|
|
ch_free( ntargetDN.bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
return muck.bv_val;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-16 11:01:19 +08:00
|
|
|
ch_free( nrefDN.bv_val );
|
|
|
|
return ntargetDN.bv_val;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* validate URL for global referral use
|
|
|
|
* LDAP URLs must not have:
|
|
|
|
* DN, attrs, scope, nor filter
|
|
|
|
* Any non-LDAP URL is okay
|
|
|
|
*
|
|
|
|
* XXYYZ: should return an error string
|
|
|
|
*/
|
|
|
|
int validate_global_referral( const char *url )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
LDAPURLDesc *lurl;
|
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
rc = ldap_url_parse_ext( url, &lurl, LDAP_PVT_URL_PARSE_NONE );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
|
|
|
switch( rc ) {
|
|
|
|
case LDAP_URL_SUCCESS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_URL_ERR_BADSCHEME:
|
|
|
|
/* not LDAP hence valid */
|
2005-12-16 09:58:36 +08:00
|
|
|
Debug( LDAP_DEBUG_CONFIG, "referral \"%s\": not LDAP.\n", url, 0, 0 );
|
2001-10-26 10:05:14 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* other error, bail */
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"referral: invalid URL (%s): %s (%d)\n",
|
|
|
|
url, "" /* ldap_url_error2str(rc) */, rc );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
if( lurl->lud_dn && *lurl->lud_dn ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"referral: URL (%s): contains DN\n",
|
|
|
|
url, 0, 0 );
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
} else if( lurl->lud_attrs ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"referral: URL (%s): requests attributes\n",
|
|
|
|
url, 0, 0 );
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
} else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"referral: URL (%s): contains explicit scope\n",
|
|
|
|
url, 0, 0 );
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
} else if( lurl->lud_filter ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"referral: URL (%s): contains explicit filter\n",
|
|
|
|
url, 0, 0 );
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_free_urldesc( lurl );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray referral_rewrite(
|
|
|
|
BerVarray in,
|
2001-12-27 04:59:24 +08:00
|
|
|
struct berval *base,
|
|
|
|
struct berval *target,
|
2001-10-26 10:05:14 +08:00
|
|
|
int scope )
|
|
|
|
{
|
2005-01-08 17:11:53 +08:00
|
|
|
int i;
|
|
|
|
BerVarray refs;
|
|
|
|
struct berval *iv, *jv;
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( in == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
/* just count them */
|
|
|
|
}
|
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( i < 1 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2006-04-07 05:44:05 +08:00
|
|
|
refs = ch_malloc( ( i + 1 ) * sizeof( struct berval ) );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
|
|
|
|
LDAPURLDesc *url;
|
2005-01-08 17:51:29 +08:00
|
|
|
char *dn;
|
2005-01-08 17:11:53 +08:00
|
|
|
int rc;
|
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
rc = ldap_url_parse_ext( iv->bv_val, &url, LDAP_PVT_URL_PARSE_NONE );
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( rc == LDAP_URL_ERR_BADSCHEME ) {
|
2002-01-02 19:00:36 +08:00
|
|
|
ber_dupbv( jv++, iv );
|
2001-10-26 10:05:14 +08:00
|
|
|
continue;
|
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
} else if ( rc != LDAP_URL_SUCCESS ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
dn = url->lud_dn;
|
|
|
|
url->lud_dn = referral_dn_muck( ( dn && *dn ) ? dn : NULL,
|
2002-08-29 16:47:53 +08:00
|
|
|
base, target );
|
2005-01-08 17:11:53 +08:00
|
|
|
ldap_memfree( dn );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
url->lud_scope = scope;
|
|
|
|
}
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
jv->bv_val = ldap_url_desc2str( url );
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( jv->bv_val != NULL ) {
|
|
|
|
jv->bv_len = strlen( jv->bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
} else {
|
|
|
|
ber_dupbv( jv, iv );
|
|
|
|
}
|
2002-01-02 19:00:36 +08:00
|
|
|
jv++;
|
2005-01-08 17:11:53 +08:00
|
|
|
|
|
|
|
ldap_free_urldesc( url );
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2005-01-08 17:11:53 +08:00
|
|
|
if ( jv == refs ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
ch_free( refs );
|
|
|
|
refs = NULL;
|
|
|
|
|
|
|
|
} else {
|
2005-01-08 17:11:53 +08:00
|
|
|
BER_BVZERO( jv );
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray get_entry_referrals(
|
2001-10-26 10:05:14 +08:00
|
|
|
Operation *op,
|
2001-12-27 04:59:24 +08:00
|
|
|
Entry *e )
|
2001-10-26 10:05:14 +08:00
|
|
|
{
|
|
|
|
Attribute *attr;
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray refs;
|
2002-01-02 19:00:36 +08:00
|
|
|
unsigned i;
|
|
|
|
struct berval *iv, *jv;
|
2001-10-26 10:05:14 +08:00
|
|
|
|
|
|
|
AttributeDescription *ad_ref = slap_schema.si_ad_ref;
|
|
|
|
|
|
|
|
attr = attr_find( e->e_attrs, ad_ref );
|
|
|
|
|
|
|
|
if( attr == NULL ) return NULL;
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
/* count references */
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i < 1 ) return NULL;
|
|
|
|
|
2006-04-07 05:44:05 +08:00
|
|
|
refs = ch_malloc( (i + 1) * sizeof(struct berval));
|
2001-10-26 10:05:14 +08:00
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
|
2001-10-26 10:05:14 +08:00
|
|
|
unsigned k;
|
2002-01-02 19:00:36 +08:00
|
|
|
ber_dupbv( jv, iv );
|
2001-10-26 10:05:14 +08:00
|
|
|
|
|
|
|
/* trim the label */
|
2002-01-02 19:00:36 +08:00
|
|
|
for( k=0; k<jv->bv_len; k++ ) {
|
2002-04-16 04:42:42 +08:00
|
|
|
if( isspace( (unsigned char) jv->bv_val[k] ) ) {
|
2002-01-02 19:00:36 +08:00
|
|
|
jv->bv_val[k] = '\0';
|
|
|
|
jv->bv_len = k;
|
2001-10-26 10:05:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
if( jv->bv_len > 0 ) {
|
|
|
|
jv++;
|
2001-10-26 10:05:14 +08:00
|
|
|
} else {
|
2002-01-02 19:00:36 +08:00
|
|
|
free( jv->bv_val );
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
if( jv == refs ) {
|
|
|
|
free( refs );
|
2001-10-26 10:05:14 +08:00
|
|
|
refs = NULL;
|
|
|
|
|
|
|
|
} else {
|
2002-01-02 19:00:36 +08:00
|
|
|
jv->bv_val = NULL;
|
2001-10-26 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we should check that a referral value exists... */
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
2003-04-05 14:34:20 +08:00
|
|
|
|
|
|
|
int get_alias_dn(
|
|
|
|
Entry *e,
|
|
|
|
struct berval *ndn,
|
|
|
|
int *err,
|
|
|
|
const char **text )
|
|
|
|
{
|
|
|
|
Attribute *a;
|
|
|
|
AttributeDescription *aliasedObjectName
|
|
|
|
= slap_schema.si_ad_aliasedObjectName;
|
|
|
|
|
|
|
|
a = attr_find( e->e_attrs, aliasedObjectName );
|
|
|
|
|
|
|
|
if( a == NULL ) {
|
|
|
|
/*
|
|
|
|
* there was an aliasedobjectname defined but no data.
|
|
|
|
*/
|
|
|
|
*err = LDAP_ALIAS_PROBLEM;
|
|
|
|
*text = "alias missing aliasedObjectName attribute";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* aliasedObjectName should be SINGLE-VALUED with a single value.
|
|
|
|
*/
|
|
|
|
if ( a->a_vals[0].bv_val == NULL ) {
|
|
|
|
/*
|
|
|
|
* there was an aliasedobjectname defined but no data.
|
|
|
|
*/
|
|
|
|
*err = LDAP_ALIAS_PROBLEM;
|
|
|
|
*text = "alias missing aliasedObjectName value";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( a->a_nvals[1].bv_val != NULL ) {
|
|
|
|
*err = LDAP_ALIAS_PROBLEM;
|
|
|
|
*text = "alias has multivalued aliasedObjectName";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ndn = a->a_nvals[0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|