Back to index

openldap  2.4.31
referral.c
Go to the documentation of this file.
00001 /* referral.c - DNS SRV backend referral handler */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2000-2003 Kurt D. Zeilenga.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was originally developed by Kurt D. Zeilenga for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <stdio.h>
00025 
00026 #include <ac/string.h>
00027 #include <ac/socket.h>
00028 
00029 #include "slap.h"
00030 #include "proto-dnssrv.h"
00031 
00032 int
00033 dnssrv_back_referrals(
00034     Operation *op,
00035     SlapReply *rs )
00036 {
00037        int i;
00038        int rc = LDAP_OTHER;
00039        char *domain = NULL;
00040        char *hostlist = NULL;
00041        char **hosts = NULL;
00042        BerVarray urls = NULL;
00043 
00044        if ( BER_BVISEMPTY( &op->o_req_dn ) ) {
00045               /* FIXME: need some means to determine whether the database
00046                * is a glue instance */
00047               if ( SLAP_GLUE_INSTANCE( op->o_bd ) ) {
00048                      return LDAP_SUCCESS;
00049               }
00050 
00051               rs->sr_text = "DNS SRV operation upon null (empty) DN disallowed";
00052               return LDAP_UNWILLING_TO_PERFORM;
00053        }
00054 
00055        if( get_manageDSAit( op ) ) {
00056               if( op->o_tag == LDAP_REQ_SEARCH ) {
00057                      return LDAP_SUCCESS;
00058               }
00059 
00060               rs->sr_text = "DNS SRV problem processing manageDSAit control";
00061               return LDAP_OTHER;
00062        } 
00063 
00064        if( ldap_dn2domain( op->o_req_dn.bv_val, &domain ) || domain == NULL ) {
00065               rs->sr_err = LDAP_REFERRAL;
00066               rs->sr_ref = default_referral;
00067               send_ldap_result( op, rs );
00068               rs->sr_ref = NULL;
00069               return LDAP_REFERRAL;
00070        }
00071 
00072        Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> domain=\"%s\"\n",
00073               op->o_req_dn.bv_val, domain, 0 );
00074 
00075        i = ldap_domain2hostlist( domain, &hostlist );
00076        if ( i ) {
00077               Debug( LDAP_DEBUG_TRACE,
00078                      "DNSSRV: domain2hostlist(%s) returned %d\n",
00079                      domain, i, 0 );
00080               rs->sr_text = "no DNS SRV RR available for DN";
00081               rc = LDAP_NO_SUCH_OBJECT;
00082               goto done;
00083        }
00084 
00085        hosts = ldap_str2charray( hostlist, " " );
00086 
00087        if( hosts == NULL ) {
00088               Debug( LDAP_DEBUG_TRACE, "DNSSRV: str2charrary error\n", 0, 0, 0 );
00089               rs->sr_text = "problem processing DNS SRV records for DN";
00090               goto done;
00091        }
00092 
00093        for( i=0; hosts[i] != NULL; i++) {
00094               struct berval url;
00095 
00096               url.bv_len = STRLENOF( "ldap://" ) + strlen( hosts[i] );
00097               url.bv_val = ch_malloc( url.bv_len + 1 );
00098 
00099               strcpy( url.bv_val, "ldap://" );
00100               strcpy( &url.bv_val[STRLENOF( "ldap://" )], hosts[i] );
00101 
00102               if ( ber_bvarray_add( &urls, &url ) < 0 ) {
00103                      free( url.bv_val );
00104                      rs->sr_text = "problem processing DNS SRV records for DN";
00105                      goto done;
00106               }
00107        }
00108 
00109        Statslog( LDAP_DEBUG_STATS,
00110            "%s DNSSRV p=%d dn=\"%s\" url=\"%s\"\n",
00111            op->o_log_prefix, op->o_protocol,
00112               op->o_req_dn.bv_val, urls[0].bv_val, 0 );
00113 
00114        Debug( LDAP_DEBUG_TRACE, "DNSSRV: dn=\"%s\" -> url=\"%s\"\n",
00115               op->o_req_dn.bv_val, urls[0].bv_val, 0 );
00116 
00117        rs->sr_ref = urls;
00118        send_ldap_error( op, rs, LDAP_REFERRAL,
00119               "DNS SRV generated referrals" );
00120        rs->sr_ref = NULL;
00121        rc = LDAP_REFERRAL;
00122 
00123 done:
00124        if( domain != NULL ) ch_free( domain );
00125        if( hostlist != NULL ) ch_free( hostlist );
00126        if( hosts != NULL ) ldap_charray_free( hosts );
00127        ber_bvarray_free( urls );
00128        return rc;
00129 }