Back to index

openldap  2.4.31
referral.c
Go to the documentation of this file.
00001 /* referral.c - muck with referrals */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 
00021 #include <ac/socket.h>
00022 #include <ac/errno.h>
00023 #include <ac/string.h>
00024 #include <ac/ctype.h>
00025 #include <ac/time.h>
00026 #include <ac/unistd.h>
00027 
00028 #include "slap.h"
00029 
00030 /*
00031  * This routine generates the DN appropriate to return in
00032  * an LDAP referral.
00033  */
00034 static char * referral_dn_muck(
00035        const char * refDN,
00036        struct berval * baseDN,
00037        struct berval * targetDN )
00038 {
00039        int rc;
00040        struct berval bvin;
00041        struct berval nrefDN = BER_BVNULL;
00042        struct berval nbaseDN = BER_BVNULL;
00043        struct berval ntargetDN = BER_BVNULL;
00044 
00045        if( !baseDN ) {
00046               /* no base, return target */
00047               return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
00048        }
00049 
00050        if( refDN ) {
00051               bvin.bv_val = (char *)refDN;
00052               bvin.bv_len = strlen( refDN );
00053 
00054               rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
00055               if( rc != LDAP_SUCCESS ) {
00056                      /* Invalid refDN */
00057                      return NULL;
00058               }
00059        }
00060 
00061        if( !targetDN ) {
00062               /* continuation reference
00063                *     if refDN present return refDN
00064                *  else return baseDN
00065                */
00066               return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
00067        }
00068 
00069        rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
00070        if( rc != LDAP_SUCCESS ) {
00071               /* Invalid targetDN */
00072               ch_free( nrefDN.bv_val );
00073               return NULL;
00074        }
00075 
00076        if( nrefDN.bv_len ) {
00077               rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
00078               if( rc != LDAP_SUCCESS ) {
00079                      /* Invalid baseDN */
00080                      ch_free( nrefDN.bv_val );
00081                      ch_free( ntargetDN.bv_val );
00082                      return NULL;
00083               }
00084 
00085               if( dn_match( &nbaseDN, &nrefDN ) ) {
00086                      ch_free( nrefDN.bv_val );
00087                      ch_free( nbaseDN.bv_val );
00088                      return ntargetDN.bv_val;
00089               }
00090 
00091               {
00092                      struct berval muck;
00093 
00094                      if( ntargetDN.bv_len < nbaseDN.bv_len ) {
00095                             ch_free( nrefDN.bv_val );
00096                             ch_free( nbaseDN.bv_val );
00097                             return ntargetDN.bv_val;
00098                      }
00099 
00100                      rc = strcasecmp(
00101                             &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
00102                             nbaseDN.bv_val );
00103                      if( rc ) {
00104                             /* target not subordinate to base */
00105                             ch_free( nrefDN.bv_val );
00106                             ch_free( nbaseDN.bv_val );
00107                             return ntargetDN.bv_val;
00108                      }
00109 
00110                      muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
00111                      muck.bv_val = ch_malloc( muck.bv_len + 1 );
00112 
00113                      strncpy( muck.bv_val, ntargetDN.bv_val,
00114                             ntargetDN.bv_len-nbaseDN.bv_len );
00115                      strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
00116                             nrefDN.bv_val );
00117 
00118                      ch_free( nrefDN.bv_val );
00119                      ch_free( nbaseDN.bv_val );
00120                      ch_free( ntargetDN.bv_val );
00121 
00122                      return muck.bv_val;
00123               }
00124        }
00125 
00126        ch_free( nrefDN.bv_val );
00127        return ntargetDN.bv_val;
00128 }
00129 
00130 
00131 /* validate URL for global referral use
00132  *   LDAP URLs must not have:
00133  *     DN, attrs, scope, nor filter
00134  *   Any non-LDAP URL is okay
00135  *
00136  *   XXYYZ: should return an error string
00137  */
00138 int validate_global_referral( const char *url )
00139 {
00140        int rc;
00141        LDAPURLDesc *lurl;
00142 
00143        rc = ldap_url_parse_ext( url, &lurl, LDAP_PVT_URL_PARSE_NONE );
00144 
00145        switch( rc ) {
00146        case LDAP_URL_SUCCESS:
00147               break;
00148 
00149        case LDAP_URL_ERR_BADSCHEME:
00150               /* not LDAP hence valid */
00151               Debug( LDAP_DEBUG_CONFIG, "referral \"%s\": not LDAP.\n", url, 0, 0 );
00152               return 0;
00153 
00154        default:
00155               /* other error, bail */
00156               Debug( LDAP_DEBUG_ANY,
00157                      "referral: invalid URL (%s): %s (%d)\n",
00158                      url, "" /* ldap_url_error2str(rc) */, rc );
00159               return 1;
00160        }
00161 
00162        rc = 0;
00163 
00164        if( lurl->lud_dn && *lurl->lud_dn ) {
00165               Debug( LDAP_DEBUG_ANY,
00166                      "referral: URL (%s): contains DN\n",
00167                      url, 0, 0 );
00168               rc = 1;
00169 
00170        } else if( lurl->lud_attrs ) {
00171               Debug( LDAP_DEBUG_ANY,
00172                      "referral: URL (%s): requests attributes\n",
00173                      url, 0, 0 );
00174               rc = 1;
00175 
00176        } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
00177               Debug( LDAP_DEBUG_ANY,
00178                      "referral: URL (%s): contains explicit scope\n",
00179                      url, 0, 0 );
00180               rc = 1;
00181 
00182        } else if( lurl->lud_filter ) {
00183               Debug( LDAP_DEBUG_ANY,
00184                      "referral: URL (%s): contains explicit filter\n",
00185                      url, 0, 0 );
00186               rc = 1;
00187        }
00188 
00189        ldap_free_urldesc( lurl );
00190        return rc;
00191 }
00192 
00193 BerVarray referral_rewrite(
00194        BerVarray in,
00195        struct berval *base,
00196        struct berval *target,
00197        int scope )
00198 {
00199        int           i;
00200        BerVarray     refs;
00201        struct berval *iv, *jv;
00202 
00203        if ( in == NULL ) {
00204               return NULL;
00205        }
00206 
00207        for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
00208               /* just count them */
00209        }
00210 
00211        if ( i < 1 ) {
00212               return NULL;
00213        }
00214 
00215        refs = ch_malloc( ( i + 1 ) * sizeof( struct berval ) );
00216 
00217        for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
00218               LDAPURLDesc   *url;
00219               char          *dn;
00220               int           rc;
00221               
00222               rc = ldap_url_parse_ext( iv->bv_val, &url, LDAP_PVT_URL_PARSE_NONE );
00223               if ( rc == LDAP_URL_ERR_BADSCHEME ) {
00224                      ber_dupbv( jv++, iv );
00225                      continue;
00226 
00227               } else if ( rc != LDAP_URL_SUCCESS ) {
00228                      continue;
00229               }
00230 
00231               dn = url->lud_dn;
00232               url->lud_dn = referral_dn_muck( ( dn && *dn ) ? dn : NULL,
00233                             base, target );
00234               ldap_memfree( dn );
00235 
00236               if ( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
00237                      url->lud_scope = scope;
00238               }
00239 
00240               jv->bv_val = ldap_url_desc2str( url );
00241               if ( jv->bv_val != NULL ) {
00242                      jv->bv_len = strlen( jv->bv_val );
00243 
00244               } else {
00245                      ber_dupbv( jv, iv );
00246               }
00247               jv++;
00248 
00249               ldap_free_urldesc( url );
00250        }
00251 
00252        if ( jv == refs ) {
00253               ch_free( refs );
00254               refs = NULL;
00255 
00256        } else {
00257               BER_BVZERO( jv );
00258        }
00259 
00260        return refs;
00261 }
00262 
00263 
00264 BerVarray get_entry_referrals(
00265        Operation *op,
00266        Entry *e )
00267 {
00268        Attribute *attr;
00269        BerVarray refs;
00270        unsigned i;
00271        struct berval *iv, *jv;
00272 
00273        AttributeDescription *ad_ref = slap_schema.si_ad_ref;
00274 
00275        attr = attr_find( e->e_attrs, ad_ref );
00276 
00277        if( attr == NULL ) return NULL;
00278 
00279        for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
00280               /* count references */
00281        }
00282 
00283        if( i < 1 ) return NULL;
00284 
00285        refs = ch_malloc( (i + 1) * sizeof(struct berval));
00286 
00287        for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
00288               unsigned k;
00289               ber_dupbv( jv, iv );
00290 
00291               /* trim the label */
00292               for( k=0; k<jv->bv_len; k++ ) {
00293                      if( isspace( (unsigned char) jv->bv_val[k] ) ) {
00294                             jv->bv_val[k] = '\0';
00295                             jv->bv_len = k;
00296                             break;
00297                      }
00298               }
00299 
00300               if(    jv->bv_len > 0 ) {
00301                      jv++;
00302               } else {
00303                      free( jv->bv_val );
00304               }
00305        }
00306 
00307        if( jv == refs ) {
00308               free( refs );
00309               refs = NULL;
00310 
00311        } else {
00312               jv->bv_val = NULL;
00313        }
00314 
00315        /* we should check that a referral value exists... */
00316        return refs;
00317 }
00318 
00319 
00320 int get_alias_dn(
00321        Entry *e,
00322        struct berval *ndn,
00323        int *err,
00324        const char **text )
00325 {      
00326        Attribute *a;
00327        AttributeDescription *aliasedObjectName
00328               = slap_schema.si_ad_aliasedObjectName;
00329 
00330        a = attr_find( e->e_attrs, aliasedObjectName );
00331 
00332        if( a == NULL ) {
00333               /*
00334                * there was an aliasedobjectname defined but no data.
00335                */
00336               *err = LDAP_ALIAS_PROBLEM;
00337               *text = "alias missing aliasedObjectName attribute";
00338               return -1;
00339        }
00340 
00341        /* 
00342         * aliasedObjectName should be SINGLE-VALUED with a single value. 
00343         */                  
00344        if ( a->a_vals[0].bv_val == NULL ) {
00345               /*
00346                * there was an aliasedobjectname defined but no data.
00347                */
00348               *err = LDAP_ALIAS_PROBLEM;
00349               *text = "alias missing aliasedObjectName value";
00350               return -1;
00351        }
00352 
00353        if( a->a_nvals[1].bv_val != NULL ) {
00354               *err = LDAP_ALIAS_PROBLEM;
00355               *text = "alias has multivalued aliasedObjectName";
00356               return -1;
00357        }
00358 
00359        *ndn = a->a_nvals[0];
00360 
00361        return 0;
00362 }
00363