1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* -*- Mode: C; c-file-style: "bsd" -*-
* keyserver.c - Keyserver support
* Copyright (C) 2002 Timo Schulz
*
* This file is part of OpenCDK.
*
* OpenCDK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* OpenCDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenCDK; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_NETDB_H
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
# define closesocket close
#else
# include <windows.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "opencdk.h"
#include "main.h"
static int initialized = 0;
static void
init_sockets( void )
{
#ifdef __MINGW32__
WSADATA wsdata;
if( initialized )
return;
if( WSAStartup( 0x0101, &wsdata ) )
_cdk_log_debug( "winsock init failed.\n" );
#endif
initialized = 1;
}
static int
keyserver_hkp( const char * host, int port, u32 keyid, cdk_kbnode_t * ret_key )
{
const char * fmt;
struct hostent * hp;
struct sockaddr_in saddr;
cdk_stream_t a;
char * buf, buffer[256];
int nwritten, nread, state = 0;
int rc = 0, fd;
_cdk_log_debug( "connect to `%s'\n", host );
hp = gethostbyname( host );
if( !hp )
return CDK_General_Error;
memset( &saddr, 0, sizeof saddr );
memcpy( &saddr.sin_addr, hp->h_addr, hp->h_length );
saddr.sin_family = hp->h_addrtype;
saddr.sin_port = htons( port );
fd = socket( AF_INET, SOCK_STREAM, 0 );
if( fd == -1 )
return CDK_General_Error;
setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,( char *)1, 1 );
if( connect( fd,( struct sockaddr *) &saddr, sizeof saddr ) == -1 ) {
closesocket( fd );
return CDK_General_Error;
}
fmt = "GET /pks/lookup?op=get&search=0x%08lX HTTP/1.0\r\n"
"Host: %s:%d\r\n"
"\r\n";
buf = cdk_calloc( 1, 64 + strlen( host ) + strlen( fmt ) );
if( !buf ) {
closesocket( fd );
return CDK_Out_Of_Core;
}
sprintf( buf, fmt, keyid, host, port );
_cdk_log_debug( "%s\n", buf );
nwritten = send( fd, buf, strlen( buf ), 0 );
if( nwritten == -1 ) {
cdk_free( buf );
closesocket( fd );
return CDK_File_Error;
}
a = cdk_stream_tmp( );
if( !a ) {
cdk_free( buf );
closesocket( fd );
return CDK_Out_Of_Core;
}
while( (nread = recv( fd, buffer, 255, 0 )) > 0 ) {
buffer[nread] = '\0';
/*_cdk_log_debug( "%s", buffer );*/
cdk_stream_write( a, buffer, nread );
if( strstr( buffer, "<pre>") || strstr( buffer, "</pre>" ) )
state++;
}
cdk_free( buf );
if( state != 2 )
rc = CDK_Error_No_Key;
if( !rc ) {
cdk_stream_tmp_set_mode( a, 0 );
cdk_stream_set_armor_flag( a, 0 );
cdk_stream_seek( a, 0 );
cdk_stream_read( a, NULL, 0 );
rc = cdk_keydb_get_keyblock( a, ret_key );
}
if( rc == CDK_EOF && *ret_key )
rc = 0;
cdk_stream_close( a );
closesocket( fd );
return rc;
}
/**
* cdk_keyserver_recv_key:
* @host: URL or hostname of the keyserver
* @port: The port to use for the connection
* @keyid: KeyID of the key to retrieve
* @kid_type: KeyID type (long, short, fingerprint)
* @r_knode: The key that was found wrapped in a KBNODE struct
*
* Receive a key from a keyserver.
**/
cdk_error_t
cdk_keyserver_recv_key( const char * host, int port,
const byte * keyid, int kid_type,
cdk_kbnode_t * ret_key )
{
u32 kid = 0;
if( !host || !keyid || !ret_key )
return CDK_Inv_Value;
if( !initialized )
init_sockets( );
if( !port )
port = 11371;
if( !strncmp( host, "http://", 7 ) )
host += 7;
else if( !strncmp( host, "hkp://", 6 ) )
host += 6;
else if( !strncmp( host, "x-hkp://", 8 ) )
host += 8;
switch( kid_type ) {
case CDK_DBSEARCH_SHORT_KEYID: kid = _cdk_buftou32( keyid ); break;
case CDK_DBSEARCH_KEYID : kid = _cdk_buftou32( keyid + 4 ); break;
case CDK_DBSEARCH_FPR : kid = _cdk_buftou32( keyid + 16 ); break;
default : return CDK_Inv_Mode;
}
return keyserver_hkp( host, port, kid, ret_key );
}
|