mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
Fix issue 1446: honor ports given in SRV responses.
* common/http.c (send_request, connect_server, http_open): Use a
struct srv instead of a single srvtag so we can pass the chosen host
and port back to the caller.
(connect_server): Use the proper port in the HAVE_GETADDRINFO case.
* keyserver/curl-shim.c (curl_easy_perform): Use struct srv and log
chosen host and port.
* keyserver/gpgkeys_hkp.c (main): Properly take the port given by SRV.
Backported from ba9e974f1f
This commit is contained in:
parent
3a51d501b9
commit
f2f12f41ef
4 changed files with 74 additions and 35 deletions
54
util/http.c
54
util/http.c
|
@ -1,6 +1,6 @@
|
|||
/* http.c - HTTP protocol handler
|
||||
* Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
|
||||
* 2009 Free Software Foundation, Inc.
|
||||
* 2009, 2012 Free Software Foundation, Inc.
|
||||
*
|
||||
* This file is part of GnuPG.
|
||||
*
|
||||
|
@ -69,12 +69,12 @@ static int insert_escapes( byte *buffer, const byte *string,
|
|||
const byte *special );
|
||||
static URI_TUPLE parse_tuple( byte *string );
|
||||
static int send_request( HTTP_HD hd, const char *auth, const char *proxy,
|
||||
const char *srvtag, STRLIST headers);
|
||||
struct http_srv *srv, STRLIST headers);
|
||||
static byte *build_rel_path( PARSED_URI uri );
|
||||
static int parse_response( HTTP_HD hd );
|
||||
|
||||
static int connect_server( const char *server, ushort port, unsigned int flags,
|
||||
const char *srvtag );
|
||||
struct http_srv *srv );
|
||||
static int write_server( int sock, const char *data, size_t length );
|
||||
|
||||
#ifdef _WIN32
|
||||
|
@ -150,7 +150,7 @@ make_radix64_string( const byte *data, size_t len )
|
|||
int
|
||||
http_open( HTTP_HD hd, HTTP_REQ_TYPE reqtype, const char *url,
|
||||
char *auth, unsigned int flags, const char *proxy,
|
||||
const char *srvtag, STRLIST headers )
|
||||
struct http_srv *srv, STRLIST headers )
|
||||
{
|
||||
int rc;
|
||||
|
||||
|
@ -166,7 +166,7 @@ http_open( HTTP_HD hd, HTTP_REQ_TYPE reqtype, const char *url,
|
|||
|
||||
rc = parse_uri( &hd->uri, url );
|
||||
if( !rc ) {
|
||||
rc = send_request( hd, auth, proxy, srvtag, headers );
|
||||
rc = send_request( hd, auth, proxy, srv, headers );
|
||||
if( !rc ) {
|
||||
hd->fp_write = iobuf_sockopen( hd->sock , "w" );
|
||||
if( hd->fp_write )
|
||||
|
@ -234,12 +234,12 @@ http_wait_response( HTTP_HD hd, unsigned int *ret_status )
|
|||
|
||||
int
|
||||
http_open_document( HTTP_HD hd, const char *document, char *auth,
|
||||
unsigned int flags, const char *proxy, const char *srvtag,
|
||||
unsigned int flags, const char *proxy, struct http_srv *srv,
|
||||
STRLIST headers )
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = http_open(hd, HTTP_REQ_GET, document, auth, flags, proxy, srvtag,
|
||||
rc = http_open(hd, HTTP_REQ_GET, document, auth, flags, proxy, srv,
|
||||
headers );
|
||||
if( rc )
|
||||
return rc;
|
||||
|
@ -523,7 +523,7 @@ parse_tuple( byte *string )
|
|||
*/
|
||||
static int
|
||||
send_request( HTTP_HD hd, const char *auth, const char *proxy,
|
||||
const char *srvtag, STRLIST headers )
|
||||
struct http_srv *srv, STRLIST headers )
|
||||
{
|
||||
const byte *server;
|
||||
byte *request, *p;
|
||||
|
@ -546,7 +546,7 @@ send_request( HTTP_HD hd, const char *auth, const char *proxy,
|
|||
return G10ERR_NETWORK;
|
||||
}
|
||||
hd->sock = connect_server( *uri->host? uri->host : "localhost",
|
||||
uri->port? uri->port : 80, 0, NULL );
|
||||
uri->port? uri->port : 80, 0, srv );
|
||||
if(uri->auth)
|
||||
{
|
||||
char *x;
|
||||
|
@ -560,7 +560,7 @@ send_request( HTTP_HD hd, const char *auth, const char *proxy,
|
|||
release_parsed_uri( uri );
|
||||
}
|
||||
else
|
||||
hd->sock = connect_server( server, port, hd->flags, srvtag );
|
||||
hd->sock = connect_server( server, port, hd->flags, srv );
|
||||
|
||||
if(auth || hd->uri->auth)
|
||||
{
|
||||
|
@ -815,9 +815,9 @@ start_server(void)
|
|||
|
||||
static int
|
||||
connect_server( const char *server, ushort port, unsigned int flags,
|
||||
const char *srvtag )
|
||||
struct http_srv *srv )
|
||||
{
|
||||
int sock=-1,srv,srvcount=0,connected=0,hostfound=0;
|
||||
int sock=-1, srvindex, srvcount=0, connected=0, hostfound=0, chosen=-1;
|
||||
struct srventry *srvlist=NULL;
|
||||
|
||||
#ifdef _WIN32
|
||||
|
@ -854,15 +854,15 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
|
||||
#ifdef USE_DNS_SRV
|
||||
/* Do the SRV thing */
|
||||
if(srvtag)
|
||||
if(srv && srv->srvtag)
|
||||
{
|
||||
/* We're using SRV, so append the tags */
|
||||
if(1+strlen(srvtag)+6+strlen(server)+1<=MAXDNAME)
|
||||
if(1+strlen(srv->srvtag)+6+strlen(server)+1<=MAXDNAME)
|
||||
{
|
||||
char srvname[MAXDNAME];
|
||||
|
||||
strcpy(srvname,"_");
|
||||
strcat(srvname,srvtag);
|
||||
strcat(srvname,srv->srvtag);
|
||||
strcat(srvname,"._tcp.");
|
||||
strcat(srvname,server);
|
||||
srvcount=getsrv(srvname,&srvlist);
|
||||
|
@ -885,15 +885,15 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
|
||||
#ifdef HAVE_GETADDRINFO
|
||||
|
||||
for(srv=0;srv<srvcount;srv++)
|
||||
for(srvindex=0;srvindex<srvcount;srvindex++)
|
||||
{
|
||||
struct addrinfo hints,*res,*ai;
|
||||
char portstr[6];
|
||||
|
||||
sprintf(portstr,"%u",srvlist[srv].port);
|
||||
sprintf(portstr,"%u",srvlist[srvindex].port);
|
||||
memset(&hints,0,sizeof(hints));
|
||||
hints.ai_socktype=SOCK_STREAM;
|
||||
if(getaddrinfo(srvlist[srv].target,portstr,&hints,&res)==0)
|
||||
if(getaddrinfo(srvlist[srvindex].target,portstr,&hints,&res)==0)
|
||||
hostfound=1;
|
||||
else
|
||||
continue;
|
||||
|
@ -910,6 +910,7 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
if(connect(sock,ai->ai_addr,ai->ai_addrlen)==0)
|
||||
{
|
||||
connected=1;
|
||||
chosen = srvindex;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -924,7 +925,7 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
|
||||
#else /* !HAVE_GETADDRINFO */
|
||||
|
||||
for(srv=0;srv<srvcount;srv++)
|
||||
for(srvindex=0; srvindex < srvcount; srv++)
|
||||
{
|
||||
int i=0;
|
||||
struct hostent *host=NULL;
|
||||
|
@ -932,7 +933,7 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
|
||||
memset(&addr,0,sizeof(addr));
|
||||
|
||||
if((host=gethostbyname(srvlist[srv].target))==NULL)
|
||||
if((host=gethostbyname(srvlist[srvindex].target))==NULL)
|
||||
continue;
|
||||
|
||||
hostfound=1;
|
||||
|
@ -946,18 +947,18 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
addr.sin_family=host->h_addrtype;
|
||||
if(addr.sin_family!=AF_INET)
|
||||
{
|
||||
log_error("%s: unknown address family\n",srvlist[srv].target);
|
||||
log_error("%s: unknown address family\n",srvlist[srvindex].target);
|
||||
return -1;
|
||||
}
|
||||
|
||||
addr.sin_port=htons(srvlist[srv].port);
|
||||
addr.sin_port=htons(srvlist[srvindex].port);
|
||||
|
||||
/* Try all A records until one responds. */
|
||||
while(host->h_addr_list[i])
|
||||
{
|
||||
if(host->h_length!=4)
|
||||
{
|
||||
log_error("%s: illegal address length\n",srvlist[srv].target);
|
||||
log_error("%s: illegal address length\n",srvlist[srvindex].target);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -966,6 +967,7 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))==0)
|
||||
{
|
||||
connected=1;
|
||||
chosen = srvindex;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -979,6 +981,12 @@ connect_server( const char *server, ushort port, unsigned int flags,
|
|||
}
|
||||
#endif /* !HAVE_GETADDRINFO */
|
||||
|
||||
if(chosen>-1 && srv)
|
||||
{
|
||||
srv->used_server = strdup (srvlist[chosen].target);
|
||||
srv->used_port = srvlist[chosen].port;
|
||||
}
|
||||
|
||||
free(srvlist);
|
||||
|
||||
if(!connected)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue