aboutsummaryrefslogtreecommitdiffstats
path: root/g10/hkp.c
blob: 3d43dbfcfc5816e41814a6acfa9faebb3b76412f (plain)
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
/* hkp.c  -  Horrowitz Keyserver Protocol
 *	Copyright (C) 1999 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG 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.
 *
 * GnuPG 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "errors.h"
#include "util.h"
#include "ttyio.h"
#include "i18n.h"
#include "options.h"
#include "http.h"
#include "main.h"


/****************
 * Try to import the key with KEYID from a keyserver but ask the user
 * before doing so.
 * Returns: 0 the key was successfully imported
 *	    -1 key not found on server or user does not want to
 *	       import the key
 *	    or other error codes.
 */
int
hkp_ask_import( u32 *keyid )
{
    struct http_context hd;
    char *request;
    int rc;

    if( !opt.keyserver_name )
	return -1;
    log_info("requesting key %08lX from %s ...\n", (ulong)keyid[1],
						   opt.keyserver_name );
    request = m_alloc( strlen( opt.keyserver_name ) + 100 );
    sprintf( request, "x-hkp://%s:11371/pks/lookup?op=get&search=0x%08lX%08lX",
			opt.keyserver_name, (ulong)keyid[0], (ulong)keyid[1] );
    rc = open_http_document( &hd, request, 0 );
    if( rc ) {
	log_info("can't get key from keyserver: %s\n", g10_errstr(rc) );
	goto leave;
    }
    rc = import_keys_stream( hd.fp_read , 0 );
    close_http_document( &hd );

  leave:
    m_free( request );
    return rc;
}