Home | History | Annotate | Line # | Download | only in slapd
referral.c revision 1.1
      1  1.1  lukem /* referral.c - muck with referrals */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/referral.c,v 1.28.2.5 2008/02/11 23:26:44 kurt Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * All rights reserved.
      7  1.1  lukem  *
      8  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      9  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     10  1.1  lukem  * Public License.
     11  1.1  lukem  *
     12  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     13  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     14  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     15  1.1  lukem  */
     16  1.1  lukem 
     17  1.1  lukem #include "portable.h"
     18  1.1  lukem 
     19  1.1  lukem #include <stdio.h>
     20  1.1  lukem 
     21  1.1  lukem #include <ac/socket.h>
     22  1.1  lukem #include <ac/errno.h>
     23  1.1  lukem #include <ac/string.h>
     24  1.1  lukem #include <ac/ctype.h>
     25  1.1  lukem #include <ac/time.h>
     26  1.1  lukem #include <ac/unistd.h>
     27  1.1  lukem 
     28  1.1  lukem #include "slap.h"
     29  1.1  lukem 
     30  1.1  lukem /*
     31  1.1  lukem  * This routine generates the DN appropriate to return in
     32  1.1  lukem  * an LDAP referral.
     33  1.1  lukem  */
     34  1.1  lukem static char * referral_dn_muck(
     35  1.1  lukem 	const char * refDN,
     36  1.1  lukem 	struct berval * baseDN,
     37  1.1  lukem 	struct berval * targetDN )
     38  1.1  lukem {
     39  1.1  lukem 	int rc;
     40  1.1  lukem 	struct berval bvin;
     41  1.1  lukem 	struct berval nrefDN = BER_BVNULL;
     42  1.1  lukem 	struct berval nbaseDN = BER_BVNULL;
     43  1.1  lukem 	struct berval ntargetDN = BER_BVNULL;
     44  1.1  lukem 
     45  1.1  lukem 	if( !baseDN ) {
     46  1.1  lukem 		/* no base, return target */
     47  1.1  lukem 		return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
     48  1.1  lukem 	}
     49  1.1  lukem 
     50  1.1  lukem 	if( refDN ) {
     51  1.1  lukem 		bvin.bv_val = (char *)refDN;
     52  1.1  lukem 		bvin.bv_len = strlen( refDN );
     53  1.1  lukem 
     54  1.1  lukem 		rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
     55  1.1  lukem 		if( rc != LDAP_SUCCESS ) {
     56  1.1  lukem 			/* Invalid refDN */
     57  1.1  lukem 			return NULL;
     58  1.1  lukem 		}
     59  1.1  lukem 	}
     60  1.1  lukem 
     61  1.1  lukem 	if( !targetDN ) {
     62  1.1  lukem 		/* continuation reference
     63  1.1  lukem 		 *	if refDN present return refDN
     64  1.1  lukem 		 *  else return baseDN
     65  1.1  lukem 		 */
     66  1.1  lukem 		return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
     67  1.1  lukem 	}
     68  1.1  lukem 
     69  1.1  lukem 	rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
     70  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
     71  1.1  lukem 		/* Invalid targetDN */
     72  1.1  lukem 		ch_free( nrefDN.bv_val );
     73  1.1  lukem 		return NULL;
     74  1.1  lukem 	}
     75  1.1  lukem 
     76  1.1  lukem 	if( nrefDN.bv_len ) {
     77  1.1  lukem 		rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
     78  1.1  lukem 		if( rc != LDAP_SUCCESS ) {
     79  1.1  lukem 			/* Invalid baseDN */
     80  1.1  lukem 			ch_free( nrefDN.bv_val );
     81  1.1  lukem 			ch_free( ntargetDN.bv_val );
     82  1.1  lukem 			return NULL;
     83  1.1  lukem 		}
     84  1.1  lukem 
     85  1.1  lukem 		if( dn_match( &nbaseDN, &nrefDN ) ) {
     86  1.1  lukem 			ch_free( nrefDN.bv_val );
     87  1.1  lukem 			ch_free( nbaseDN.bv_val );
     88  1.1  lukem 			return ntargetDN.bv_val;
     89  1.1  lukem 		}
     90  1.1  lukem 
     91  1.1  lukem 		{
     92  1.1  lukem 			struct berval muck;
     93  1.1  lukem 
     94  1.1  lukem 			if( ntargetDN.bv_len < nbaseDN.bv_len ) {
     95  1.1  lukem 				ch_free( nrefDN.bv_val );
     96  1.1  lukem 				ch_free( nbaseDN.bv_val );
     97  1.1  lukem 				return ntargetDN.bv_val;
     98  1.1  lukem 			}
     99  1.1  lukem 
    100  1.1  lukem 			rc = strcasecmp(
    101  1.1  lukem 				&ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
    102  1.1  lukem 				nbaseDN.bv_val );
    103  1.1  lukem 			if( rc ) {
    104  1.1  lukem 				/* target not subordinate to base */
    105  1.1  lukem 				ch_free( nrefDN.bv_val );
    106  1.1  lukem 				ch_free( nbaseDN.bv_val );
    107  1.1  lukem 				return ntargetDN.bv_val;
    108  1.1  lukem 			}
    109  1.1  lukem 
    110  1.1  lukem 			muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
    111  1.1  lukem 			muck.bv_val = ch_malloc( muck.bv_len + 1 );
    112  1.1  lukem 
    113  1.1  lukem 			strncpy( muck.bv_val, ntargetDN.bv_val,
    114  1.1  lukem 				ntargetDN.bv_len-nbaseDN.bv_len );
    115  1.1  lukem 			strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
    116  1.1  lukem 				nrefDN.bv_val );
    117  1.1  lukem 
    118  1.1  lukem 			ch_free( nrefDN.bv_val );
    119  1.1  lukem 			ch_free( nbaseDN.bv_val );
    120  1.1  lukem 			ch_free( ntargetDN.bv_val );
    121  1.1  lukem 
    122  1.1  lukem 			return muck.bv_val;
    123  1.1  lukem 		}
    124  1.1  lukem 	}
    125  1.1  lukem 
    126  1.1  lukem 	ch_free( nrefDN.bv_val );
    127  1.1  lukem 	return ntargetDN.bv_val;
    128  1.1  lukem }
    129  1.1  lukem 
    130  1.1  lukem 
    131  1.1  lukem /* validate URL for global referral use
    132  1.1  lukem  *   LDAP URLs must not have:
    133  1.1  lukem  *     DN, attrs, scope, nor filter
    134  1.1  lukem  *   Any non-LDAP URL is okay
    135  1.1  lukem  *
    136  1.1  lukem  *   XXYYZ: should return an error string
    137  1.1  lukem  */
    138  1.1  lukem int validate_global_referral( const char *url )
    139  1.1  lukem {
    140  1.1  lukem 	int rc;
    141  1.1  lukem 	LDAPURLDesc *lurl;
    142  1.1  lukem 
    143  1.1  lukem 	rc = ldap_url_parse_ext( url, &lurl, LDAP_PVT_URL_PARSE_NONE );
    144  1.1  lukem 
    145  1.1  lukem 	switch( rc ) {
    146  1.1  lukem 	case LDAP_URL_SUCCESS:
    147  1.1  lukem 		break;
    148  1.1  lukem 
    149  1.1  lukem 	case LDAP_URL_ERR_BADSCHEME:
    150  1.1  lukem 		/* not LDAP hence valid */
    151  1.1  lukem 		Debug( LDAP_DEBUG_CONFIG, "referral \"%s\": not LDAP.\n", url, 0, 0 );
    152  1.1  lukem 		return 0;
    153  1.1  lukem 
    154  1.1  lukem 	default:
    155  1.1  lukem 		/* other error, bail */
    156  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    157  1.1  lukem 			"referral: invalid URL (%s): %s (%d)\n",
    158  1.1  lukem 			url, "" /* ldap_url_error2str(rc) */, rc );
    159  1.1  lukem 		return 1;
    160  1.1  lukem 	}
    161  1.1  lukem 
    162  1.1  lukem 	rc = 0;
    163  1.1  lukem 
    164  1.1  lukem 	if( lurl->lud_dn && *lurl->lud_dn ) {
    165  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    166  1.1  lukem 			"referral: URL (%s): contains DN\n",
    167  1.1  lukem 			url, 0, 0 );
    168  1.1  lukem 		rc = 1;
    169  1.1  lukem 
    170  1.1  lukem 	} else if( lurl->lud_attrs ) {
    171  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    172  1.1  lukem 			"referral: URL (%s): requests attributes\n",
    173  1.1  lukem 			url, 0, 0 );
    174  1.1  lukem 		rc = 1;
    175  1.1  lukem 
    176  1.1  lukem 	} else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
    177  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    178  1.1  lukem 			"referral: URL (%s): contains explicit scope\n",
    179  1.1  lukem 			url, 0, 0 );
    180  1.1  lukem 		rc = 1;
    181  1.1  lukem 
    182  1.1  lukem 	} else if( lurl->lud_filter ) {
    183  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    184  1.1  lukem 			"referral: URL (%s): contains explicit filter\n",
    185  1.1  lukem 			url, 0, 0 );
    186  1.1  lukem 		rc = 1;
    187  1.1  lukem 	}
    188  1.1  lukem 
    189  1.1  lukem 	ldap_free_urldesc( lurl );
    190  1.1  lukem 	return rc;
    191  1.1  lukem }
    192  1.1  lukem 
    193  1.1  lukem BerVarray referral_rewrite(
    194  1.1  lukem 	BerVarray in,
    195  1.1  lukem 	struct berval *base,
    196  1.1  lukem 	struct berval *target,
    197  1.1  lukem 	int scope )
    198  1.1  lukem {
    199  1.1  lukem 	int		i;
    200  1.1  lukem 	BerVarray	refs;
    201  1.1  lukem 	struct berval	*iv, *jv;
    202  1.1  lukem 
    203  1.1  lukem 	if ( in == NULL ) {
    204  1.1  lukem 		return NULL;
    205  1.1  lukem 	}
    206  1.1  lukem 
    207  1.1  lukem 	for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
    208  1.1  lukem 		/* just count them */
    209  1.1  lukem 	}
    210  1.1  lukem 
    211  1.1  lukem 	if ( i < 1 ) {
    212  1.1  lukem 		return NULL;
    213  1.1  lukem 	}
    214  1.1  lukem 
    215  1.1  lukem 	refs = ch_malloc( ( i + 1 ) * sizeof( struct berval ) );
    216  1.1  lukem 
    217  1.1  lukem 	for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
    218  1.1  lukem 		LDAPURLDesc	*url;
    219  1.1  lukem 		char		*dn;
    220  1.1  lukem 		int		rc;
    221  1.1  lukem 
    222  1.1  lukem 		rc = ldap_url_parse_ext( iv->bv_val, &url, LDAP_PVT_URL_PARSE_NONE );
    223  1.1  lukem 		if ( rc == LDAP_URL_ERR_BADSCHEME ) {
    224  1.1  lukem 			ber_dupbv( jv++, iv );
    225  1.1  lukem 			continue;
    226  1.1  lukem 
    227  1.1  lukem 		} else if ( rc != LDAP_URL_SUCCESS ) {
    228  1.1  lukem 			continue;
    229  1.1  lukem 		}
    230  1.1  lukem 
    231  1.1  lukem 		dn = url->lud_dn;
    232  1.1  lukem 		url->lud_dn = referral_dn_muck( ( dn && *dn ) ? dn : NULL,
    233  1.1  lukem 				base, target );
    234  1.1  lukem 		ldap_memfree( dn );
    235  1.1  lukem 
    236  1.1  lukem 		if ( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
    237  1.1  lukem 			url->lud_scope = scope;
    238  1.1  lukem 		}
    239  1.1  lukem 
    240  1.1  lukem 		jv->bv_val = ldap_url_desc2str( url );
    241  1.1  lukem 		if ( jv->bv_val != NULL ) {
    242  1.1  lukem 			jv->bv_len = strlen( jv->bv_val );
    243  1.1  lukem 
    244  1.1  lukem 		} else {
    245  1.1  lukem 			ber_dupbv( jv, iv );
    246  1.1  lukem 		}
    247  1.1  lukem 		jv++;
    248  1.1  lukem 
    249  1.1  lukem 		ldap_free_urldesc( url );
    250  1.1  lukem 	}
    251  1.1  lukem 
    252  1.1  lukem 	if ( jv == refs ) {
    253  1.1  lukem 		ch_free( refs );
    254  1.1  lukem 		refs = NULL;
    255  1.1  lukem 
    256  1.1  lukem 	} else {
    257  1.1  lukem 		BER_BVZERO( jv );
    258  1.1  lukem 	}
    259  1.1  lukem 
    260  1.1  lukem 	return refs;
    261  1.1  lukem }
    262  1.1  lukem 
    263  1.1  lukem 
    264  1.1  lukem BerVarray get_entry_referrals(
    265  1.1  lukem 	Operation *op,
    266  1.1  lukem 	Entry *e )
    267  1.1  lukem {
    268  1.1  lukem 	Attribute *attr;
    269  1.1  lukem 	BerVarray refs;
    270  1.1  lukem 	unsigned i;
    271  1.1  lukem 	struct berval *iv, *jv;
    272  1.1  lukem 
    273  1.1  lukem 	AttributeDescription *ad_ref = slap_schema.si_ad_ref;
    274  1.1  lukem 
    275  1.1  lukem 	attr = attr_find( e->e_attrs, ad_ref );
    276  1.1  lukem 
    277  1.1  lukem 	if( attr == NULL ) return NULL;
    278  1.1  lukem 
    279  1.1  lukem 	for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
    280  1.1  lukem 		/* count references */
    281  1.1  lukem 	}
    282  1.1  lukem 
    283  1.1  lukem 	if( i < 1 ) return NULL;
    284  1.1  lukem 
    285  1.1  lukem 	refs = ch_malloc( (i + 1) * sizeof(struct berval));
    286  1.1  lukem 
    287  1.1  lukem 	for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
    288  1.1  lukem 		unsigned k;
    289  1.1  lukem 		ber_dupbv( jv, iv );
    290  1.1  lukem 
    291  1.1  lukem 		/* trim the label */
    292  1.1  lukem 		for( k=0; k<jv->bv_len; k++ ) {
    293  1.1  lukem 			if( isspace( (unsigned char) jv->bv_val[k] ) ) {
    294  1.1  lukem 				jv->bv_val[k] = '\0';
    295  1.1  lukem 				jv->bv_len = k;
    296  1.1  lukem 				break;
    297  1.1  lukem 			}
    298  1.1  lukem 		}
    299  1.1  lukem 
    300  1.1  lukem 		if(	jv->bv_len > 0 ) {
    301  1.1  lukem 			jv++;
    302  1.1  lukem 		} else {
    303  1.1  lukem 			free( jv->bv_val );
    304  1.1  lukem 		}
    305  1.1  lukem 	}
    306  1.1  lukem 
    307  1.1  lukem 	if( jv == refs ) {
    308  1.1  lukem 		free( refs );
    309  1.1  lukem 		refs = NULL;
    310  1.1  lukem 
    311  1.1  lukem 	} else {
    312  1.1  lukem 		jv->bv_val = NULL;
    313  1.1  lukem 	}
    314  1.1  lukem 
    315  1.1  lukem 	/* we should check that a referral value exists... */
    316  1.1  lukem 	return refs;
    317  1.1  lukem }
    318  1.1  lukem 
    319  1.1  lukem 
    320  1.1  lukem int get_alias_dn(
    321  1.1  lukem 	Entry *e,
    322  1.1  lukem 	struct berval *ndn,
    323  1.1  lukem 	int *err,
    324  1.1  lukem 	const char **text )
    325  1.1  lukem {
    326  1.1  lukem 	Attribute *a;
    327  1.1  lukem 	AttributeDescription *aliasedObjectName
    328  1.1  lukem 		= slap_schema.si_ad_aliasedObjectName;
    329  1.1  lukem 
    330  1.1  lukem 	a = attr_find( e->e_attrs, aliasedObjectName );
    331  1.1  lukem 
    332  1.1  lukem 	if( a == NULL ) {
    333  1.1  lukem 		/*
    334  1.1  lukem 		 * there was an aliasedobjectname defined but no data.
    335  1.1  lukem 		 */
    336  1.1  lukem 		*err = LDAP_ALIAS_PROBLEM;
    337  1.1  lukem 		*text = "alias missing aliasedObjectName attribute";
    338  1.1  lukem 		return -1;
    339  1.1  lukem 	}
    340  1.1  lukem 
    341  1.1  lukem 	/*
    342  1.1  lukem 	 * aliasedObjectName should be SINGLE-VALUED with a single value.
    343  1.1  lukem 	 */
    344  1.1  lukem 	if ( a->a_vals[0].bv_val == NULL ) {
    345  1.1  lukem 		/*
    346  1.1  lukem 		 * there was an aliasedobjectname defined but no data.
    347  1.1  lukem 		 */
    348  1.1  lukem 		*err = LDAP_ALIAS_PROBLEM;
    349  1.1  lukem 		*text = "alias missing aliasedObjectName value";
    350  1.1  lukem 		return -1;
    351  1.1  lukem 	}
    352  1.1  lukem 
    353  1.1  lukem 	if( a->a_nvals[1].bv_val != NULL ) {
    354  1.1  lukem 		*err = LDAP_ALIAS_PROBLEM;
    355  1.1  lukem 		*text = "alias has multivalued aliasedObjectName";
    356  1.1  lukem 		return -1;
    357  1.1  lukem 	}
    358  1.1  lukem 
    359  1.1  lukem 	*ndn = a->a_nvals[0];
    360  1.1  lukem 
    361  1.1  lukem 	return 0;
    362  1.1  lukem }
    363  1.1  lukem 
    364