2002-06-29 13:31:13 +00:00
|
|
|
/* gpgkeys_hkp.c - talk to an HKP keyserver
|
2009-05-11 03:52:34 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
|
|
|
* 2009 Free Software Foundation, Inc.
|
2002-06-29 13:31:13 +00:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2002-06-29 13:31:13 +00:00
|
|
|
* (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
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-09-22 11:39:45 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the Free Software Foundation
|
|
|
|
* gives permission to link the code of the keyserver helper tools:
|
|
|
|
* gpgkeys_ldap, gpgkeys_curl and gpgkeys_hkp with the OpenSSL
|
|
|
|
* project's "OpenSSL" library (or with modified versions of it that
|
|
|
|
* use the same license as the "OpenSSL" library), and distribute the
|
|
|
|
* linked executables. You must obey the GNU General Public License
|
|
|
|
* in all respects for all of the code used other than "OpenSSL". If
|
|
|
|
* you modify this file, you may extend this exception to your version
|
|
|
|
* of the file, but you are not obligated to do so. If you do not
|
|
|
|
* wish to do so, delete this exception statement from your version.
|
2002-06-29 13:31:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2002-08-27 19:11:36 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2003-05-31 03:52:02 +00:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
2006-04-26 21:48:29 +00:00
|
|
|
#ifdef HAVE_LIBCURL
|
2005-04-17 02:18:32 +00:00
|
|
|
#include <curl/curl.h>
|
2006-04-26 21:48:29 +00:00
|
|
|
#else
|
|
|
|
#include "curl-shim.h"
|
2005-04-17 02:18:32 +00:00
|
|
|
#endif
|
2009-08-26 10:58:13 +00:00
|
|
|
#include "util.h"
|
2009-05-28 04:25:25 +00:00
|
|
|
#ifdef USE_DNS_SRV
|
|
|
|
#include "srv.h"
|
|
|
|
#endif
|
2002-08-27 19:11:36 +00:00
|
|
|
#include "keyserver.h"
|
2004-10-13 18:30:29 +00:00
|
|
|
#include "ksutil.h"
|
2002-08-27 12:44:18 +00:00
|
|
|
|
2003-05-31 03:52:02 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
static FILE *input,*output,*console;
|
|
|
|
static CURL *curl;
|
|
|
|
static struct ks_options *opt;
|
|
|
|
static char errorbuffer[CURL_ERROR_SIZE];
|
2009-05-11 03:52:34 +00:00
|
|
|
static char *proto,*port;
|
2003-12-28 16:21:46 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
static size_t
|
|
|
|
curl_mrindex_writer(const void *ptr,size_t size,size_t nmemb,void *stream)
|
2002-08-27 12:44:18 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
static int checked=0,swallow=0;
|
|
|
|
|
|
|
|
if(!checked)
|
|
|
|
{
|
|
|
|
/* If the document begins with a '<', assume it's a HTML
|
|
|
|
response, which we don't support. Discard the whole message
|
|
|
|
body. GPG can handle it, but this is an optimization to deal
|
|
|
|
with it on this side of the pipe. */
|
|
|
|
const char *buf=ptr;
|
|
|
|
if(buf[0]=='<')
|
2006-11-06 04:03:44 +00:00
|
|
|
swallow=1;
|
2005-04-17 02:18:32 +00:00
|
|
|
|
|
|
|
checked=1;
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
2005-04-17 02:18:32 +00:00
|
|
|
|
|
|
|
if(swallow || fwrite(ptr,size,nmemb,stream)==nmemb)
|
|
|
|
return size*nmemb;
|
|
|
|
else
|
|
|
|
return 0;
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2005-06-21 04:24:10 +00:00
|
|
|
/* Append but avoid creating a double slash // in the path. */
|
|
|
|
static char *
|
|
|
|
append_path(char *dest,const char *src)
|
|
|
|
{
|
|
|
|
size_t n=strlen(dest);
|
|
|
|
|
|
|
|
if(src[0]=='/' && n>0 && dest[n-1]=='/')
|
|
|
|
dest[n-1]='\0';
|
|
|
|
|
|
|
|
return strcat(dest,src);
|
|
|
|
}
|
|
|
|
|
2009-08-26 10:58:13 +00:00
|
|
|
/* Return a pointer into STRING so that appending PATH to STRING will
|
|
|
|
not yield a duplicated slash. */
|
|
|
|
static const char *
|
|
|
|
appendable_path (const char *string, const char *path)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (path[0] == '/' && (n=strlen (string)) && string[n-1] == '/')
|
|
|
|
return path+1;
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-13 18:45:36 +00:00
|
|
|
int
|
2007-06-14 17:05:07 +00:00
|
|
|
send_key(int *r_eof)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
CURLcode res;
|
2005-06-04 23:09:27 +00:00
|
|
|
char request[MAX_URL+15];
|
2005-04-17 02:18:32 +00:00
|
|
|
int begin=0,end=0,ret=KEYSERVER_INTERNAL_ERROR;
|
2006-07-14 16:07:01 +00:00
|
|
|
char keyid[17],state[6];
|
2002-08-27 19:11:36 +00:00
|
|
|
char line[MAX_LINE];
|
2006-01-16 17:59:46 +00:00
|
|
|
char *key=NULL,*encoded_key=NULL;
|
|
|
|
size_t keylen=0,keymax=0;
|
2005-05-02 00:46:39 +00:00
|
|
|
|
2002-08-28 04:04:37 +00:00
|
|
|
/* Read and throw away input until we see the BEGIN */
|
2002-06-29 13:31:13 +00:00
|
|
|
|
|
|
|
while(fgets(line,MAX_LINE,input)!=NULL)
|
2006-07-17 04:11:30 +00:00
|
|
|
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%5s\n",keyid,state)==2
|
2006-07-14 16:07:01 +00:00
|
|
|
&& strcmp(state,"BEGIN")==0)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2002-11-04 13:59:08 +00:00
|
|
|
begin=1;
|
2002-06-29 13:31:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-11-04 13:59:08 +00:00
|
|
|
if(!begin)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2002-09-24 19:50:09 +00:00
|
|
|
/* i.e. eof before the KEY BEGIN was found. This isn't an
|
|
|
|
error. */
|
2007-06-14 17:05:07 +00:00
|
|
|
*r_eof=1;
|
2002-09-24 19:50:09 +00:00
|
|
|
ret=KEYSERVER_OK;
|
2002-06-29 13:31:13 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now slurp up everything until we see the END */
|
|
|
|
|
2002-08-27 19:11:36 +00:00
|
|
|
while(fgets(line,MAX_LINE,input))
|
2006-07-17 04:11:30 +00:00
|
|
|
if(sscanf(line,"KEY%*[ ]%16s%*[ ]%3s\n",keyid,state)==2
|
2006-07-14 16:07:01 +00:00
|
|
|
&& strcmp(state,"END")==0)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2002-11-04 13:59:08 +00:00
|
|
|
end=1;
|
2002-06-29 13:31:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2005-04-17 02:18:32 +00:00
|
|
|
{
|
|
|
|
if(strlen(line)+keylen>keymax)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
keymax+=200;
|
|
|
|
tmp=realloc(key,keymax+1);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
free(key);
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
key=tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(&key[keylen],line);
|
|
|
|
keylen+=strlen(line);
|
|
|
|
}
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2002-11-04 13:59:08 +00:00
|
|
|
if(!end)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: no KEY %s END found\n",keyid);
|
2007-06-14 17:05:07 +00:00
|
|
|
*r_eof=1;
|
2002-09-24 19:50:09 +00:00
|
|
|
ret=KEYSERVER_KEY_INCOMPLETE;
|
2002-06-29 13:31:13 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
encoded_key=curl_escape(key,keylen);
|
|
|
|
if(!encoded_key)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
2002-06-29 13:31:13 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2006-01-16 17:59:46 +00:00
|
|
|
free(key);
|
|
|
|
|
2009-08-26 10:58:13 +00:00
|
|
|
key = strconcat ("keytext=", encoded_key, NULL);
|
2006-01-16 17:59:46 +00:00
|
|
|
if(!key)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-05-11 03:52:34 +00:00
|
|
|
strcpy(request,proto);
|
2009-05-28 04:25:25 +00:00
|
|
|
strcat(request,"://");
|
2005-04-17 02:18:32 +00:00
|
|
|
strcat(request,opt->host);
|
|
|
|
strcat(request,":");
|
2009-05-11 03:52:34 +00:00
|
|
|
strcat(request,port);
|
2005-06-21 04:24:10 +00:00
|
|
|
strcat(request,opt->path);
|
2005-06-04 23:09:27 +00:00
|
|
|
/* request is MAX_URL+15 bytes long - MAX_URL covers the whole URL,
|
|
|
|
including any supplied path. The 15 covers /pks/add. */
|
2005-06-21 04:24:10 +00:00
|
|
|
append_path(request,"/pks/add");
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->verbose>2)
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: HTTP URL is '%s'\n",request);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_URL,request);
|
2008-04-14 19:41:40 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_POST,1L);
|
2006-01-16 17:59:46 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,key);
|
2008-04-14 19:41:40 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_FAILONERROR,1L);
|
2002-08-27 19:11:36 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
res=curl_easy_perform(curl);
|
|
|
|
if(res!=0)
|
2002-08-27 19:11:36 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"gpgkeys: HTTP post error %d: %s\n",res,errorbuffer);
|
|
|
|
ret=curl_err_to_gpg_err(res);
|
2006-07-14 16:07:01 +00:00
|
|
|
goto fail;
|
2002-08-27 19:11:36 +00:00
|
|
|
}
|
2005-04-17 02:18:32 +00:00
|
|
|
else
|
|
|
|
fprintf(output,"\nKEY %s SENT\n",keyid);
|
2002-09-24 19:50:09 +00:00
|
|
|
|
|
|
|
ret=KEYSERVER_OK;
|
2002-08-27 19:11:36 +00:00
|
|
|
|
|
|
|
fail:
|
2009-08-26 10:58:13 +00:00
|
|
|
xfree (key);
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_free(encoded_key);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2002-11-04 13:59:08 +00:00
|
|
|
if(ret!=0 && begin)
|
|
|
|
fprintf(output,"KEY %s FAILED %d\n",keyid,ret);
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
static int
|
2002-09-13 18:45:36 +00:00
|
|
|
get_key(char *getkey)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
CURLcode res;
|
2011-12-28 16:41:31 -05:00
|
|
|
char request[MAX_URL+92];
|
2005-04-17 02:18:32 +00:00
|
|
|
char *offset;
|
|
|
|
struct curl_writer_ctx ctx;
|
2011-12-28 16:41:31 -05:00
|
|
|
size_t keylen;
|
2005-04-17 02:18:32 +00:00
|
|
|
|
|
|
|
memset(&ctx,0,sizeof(ctx));
|
2002-06-29 13:31:13 +00:00
|
|
|
|
|
|
|
/* Build the search string. HKP only uses the short key IDs. */
|
|
|
|
|
|
|
|
if(strncmp(getkey,"0x",2)==0)
|
|
|
|
getkey+=2;
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(output,"KEY 0x%s BEGIN\n",getkey);
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
if(strlen(getkey)==32)
|
|
|
|
{
|
|
|
|
fprintf(console,
|
|
|
|
"gpgkeys: HKP keyservers do not support v3 fingerprints\n");
|
2002-09-24 19:50:09 +00:00
|
|
|
fprintf(output,"KEY 0x%s FAILED %d\n",getkey,KEYSERVER_NOT_SUPPORTED);
|
|
|
|
return KEYSERVER_NOT_SUPPORTED;
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 03:52:34 +00:00
|
|
|
strcpy(request,proto);
|
2009-05-28 04:25:25 +00:00
|
|
|
strcat(request,"://");
|
2005-04-17 02:18:32 +00:00
|
|
|
strcat(request,opt->host);
|
|
|
|
strcat(request,":");
|
2009-05-11 03:52:34 +00:00
|
|
|
strcat(request,port);
|
2005-06-21 04:24:10 +00:00
|
|
|
strcat(request,opt->path);
|
2005-06-04 23:09:27 +00:00
|
|
|
/* request is MAX_URL+55 bytes long - MAX_URL covers the whole URL,
|
2011-12-28 16:41:31 -05:00
|
|
|
including any supplied path. The 92 overcovers this /pks/... etc
|
|
|
|
string plus the 8, 16, or 40 bytes of key id/fingerprint */
|
2005-06-21 04:24:10 +00:00
|
|
|
append_path(request,"/pks/lookup?op=get&options=mr&search=0x");
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2011-12-28 16:41:31 -05:00
|
|
|
/* send only fingerprint, long key id, or short keyid. see:
|
|
|
|
https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-3.1.1.1 */
|
|
|
|
keylen = strlen(getkey);
|
|
|
|
if(keylen >= 40)
|
|
|
|
offset=&getkey[keylen-40];
|
|
|
|
else if(keylen >= 16)
|
|
|
|
offset=&getkey[keylen-16];
|
|
|
|
else if(keylen >= 8)
|
|
|
|
offset=&getkey[keylen-8];
|
2005-04-17 02:18:32 +00:00
|
|
|
else
|
|
|
|
offset=getkey;
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
strcat(request,offset);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->verbose>2)
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: HTTP URL is '%s'\n",request);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_URL,request);
|
|
|
|
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer);
|
|
|
|
ctx.stream=output;
|
|
|
|
curl_easy_setopt(curl,CURLOPT_FILE,&ctx);
|
|
|
|
|
|
|
|
res=curl_easy_perform(curl);
|
2005-12-19 19:39:32 +00:00
|
|
|
if(res!=CURLE_OK)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"gpgkeys: HTTP fetch error %d: %s\n",res,errorbuffer);
|
|
|
|
fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
2002-08-27 19:11:36 +00:00
|
|
|
else
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-12-19 19:39:32 +00:00
|
|
|
curl_writer_finalize(&ctx);
|
|
|
|
if(!ctx.flags.done)
|
2002-09-16 14:35:19 +00:00
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey);
|
2005-12-19 19:39:32 +00:00
|
|
|
fprintf(output,"\nKEY 0x%s FAILED %d\n",
|
2002-09-24 19:50:09 +00:00
|
|
|
getkey,KEYSERVER_KEY_NOT_FOUND);
|
2002-09-16 14:35:19 +00:00
|
|
|
}
|
2005-12-19 19:39:32 +00:00
|
|
|
else
|
|
|
|
fprintf(output,"\nKEY 0x%s END\n",getkey);
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:50:09 +00:00
|
|
|
return KEYSERVER_OK;
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 20:51:48 +00:00
|
|
|
static int
|
|
|
|
get_name(const char *getkey)
|
|
|
|
{
|
|
|
|
CURLcode res;
|
|
|
|
char *request=NULL;
|
|
|
|
char *searchkey_encoded;
|
|
|
|
int ret=KEYSERVER_INTERNAL_ERROR;
|
|
|
|
struct curl_writer_ctx ctx;
|
|
|
|
|
|
|
|
memset(&ctx,0,sizeof(ctx));
|
|
|
|
|
|
|
|
searchkey_encoded=curl_escape((char *)getkey,0);
|
|
|
|
if(!searchkey_encoded)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-08-26 10:58:13 +00:00
|
|
|
request = strconcat
|
|
|
|
(proto,
|
|
|
|
"://",
|
|
|
|
opt->host,
|
|
|
|
":",
|
|
|
|
port,
|
|
|
|
opt->path,
|
|
|
|
appendable_path (opt->path,"/pks/lookup?op=get&options=mr&search="),
|
|
|
|
searchkey_encoded,
|
2011-01-20 14:12:53 +01:00
|
|
|
"&exact=on",
|
2009-08-26 10:58:13 +00:00
|
|
|
NULL);
|
2005-12-23 20:51:48 +00:00
|
|
|
if(!request)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-18 14:09:47 +01:00
|
|
|
|
2005-12-23 20:51:48 +00:00
|
|
|
fprintf(output,"NAME %s BEGIN\n",getkey);
|
|
|
|
|
|
|
|
if(opt->verbose>2)
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: HTTP URL is '%s'\n",request);
|
2005-12-23 20:51:48 +00:00
|
|
|
|
|
|
|
curl_easy_setopt(curl,CURLOPT_URL,request);
|
|
|
|
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer);
|
|
|
|
ctx.stream=output;
|
|
|
|
curl_easy_setopt(curl,CURLOPT_FILE,&ctx);
|
|
|
|
|
|
|
|
res=curl_easy_perform(curl);
|
|
|
|
if(res!=CURLE_OK)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: HTTP fetch error %d: %s\n",res,errorbuffer);
|
|
|
|
ret=curl_err_to_gpg_err(res);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curl_writer_finalize(&ctx);
|
|
|
|
if(!ctx.flags.done)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey);
|
|
|
|
ret=KEYSERVER_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(output,"\nNAME %s END\n",getkey);
|
|
|
|
ret=KEYSERVER_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
curl_free(searchkey_encoded);
|
2009-08-26 10:58:13 +00:00
|
|
|
xfree (request);
|
2005-12-23 20:51:48 +00:00
|
|
|
|
|
|
|
if(ret!=KEYSERVER_OK)
|
|
|
|
fprintf(output,"\nNAME %s FAILED %d\n",getkey,ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-08-26 04:24:46 +00:00
|
|
|
static int
|
|
|
|
search_key(const char *searchkey)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
CURLcode res;
|
2005-08-18 04:17:20 +00:00
|
|
|
char *request=NULL;
|
2005-12-23 20:51:48 +00:00
|
|
|
char *searchkey_encoded;
|
2005-04-17 02:18:32 +00:00
|
|
|
int ret=KEYSERVER_INTERNAL_ERROR;
|
2005-08-26 04:24:46 +00:00
|
|
|
enum ks_search_type search_type;
|
2009-08-26 10:58:13 +00:00
|
|
|
const char *hexprefix;
|
2004-01-12 04:09:37 +00:00
|
|
|
|
2005-08-26 04:24:46 +00:00
|
|
|
search_type=classify_ks_search(&searchkey);
|
2005-08-18 04:17:20 +00:00
|
|
|
|
2005-08-26 04:24:46 +00:00
|
|
|
if(opt->debug)
|
|
|
|
fprintf(console,"gpgkeys: search type is %d, and key is \"%s\"\n",
|
|
|
|
search_type,searchkey);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-11-03 04:46:20 +00:00
|
|
|
searchkey_encoded=curl_escape((char *)searchkey,0);
|
2005-08-18 04:17:20 +00:00
|
|
|
if(!searchkey_encoded)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-08-26 10:58:13 +00:00
|
|
|
/* HKP keyservers like the 0x to be present when searching by
|
|
|
|
keyid. */
|
|
|
|
hexprefix = (search_type==KS_SEARCH_KEYID_SHORT
|
|
|
|
|| search_type==KS_SEARCH_KEYID_LONG)? "0x":"";
|
|
|
|
|
|
|
|
request = strconcat
|
|
|
|
(proto,
|
|
|
|
"://",
|
|
|
|
opt->host,
|
|
|
|
":",
|
|
|
|
port,
|
|
|
|
opt->path,
|
|
|
|
appendable_path (opt->path, "/pks/lookup?op=index&options=mr&search="),
|
|
|
|
hexprefix,
|
|
|
|
searchkey_encoded,
|
|
|
|
NULL);
|
2002-09-10 08:28:40 +00:00
|
|
|
if(!request)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory\n");
|
2002-09-24 19:50:09 +00:00
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
2002-09-10 08:28:40 +00:00
|
|
|
}
|
2002-08-27 19:11:36 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(output,"SEARCH %s BEGIN\n",searchkey);
|
|
|
|
|
|
|
|
if(opt->verbose>2)
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: HTTP URL is '%s'\n",request);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_URL,request);
|
|
|
|
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_mrindex_writer);
|
|
|
|
curl_easy_setopt(curl,CURLOPT_FILE,output);
|
|
|
|
|
|
|
|
res=curl_easy_perform(curl);
|
|
|
|
if(res!=0)
|
2002-08-27 19:11:36 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"gpgkeys: HTTP search error %d: %s\n",res,errorbuffer);
|
|
|
|
ret=curl_err_to_gpg_err(res);
|
2002-08-27 19:11:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(output,"\nSEARCH %s END\n",searchkey);
|
2002-09-24 19:50:09 +00:00
|
|
|
ret=KEYSERVER_OK;
|
2002-08-27 19:11:36 +00:00
|
|
|
}
|
|
|
|
|
2002-09-24 19:50:09 +00:00
|
|
|
fail:
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_free(searchkey_encoded);
|
2009-08-26 10:58:13 +00:00
|
|
|
xfree (request);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2002-09-24 19:50:09 +00:00
|
|
|
if(ret!=KEYSERVER_OK)
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(output,"\nSEARCH %s FAILED %d\n",searchkey,ret);
|
2002-09-24 19:50:09 +00:00
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-10-09 02:03:22 +00:00
|
|
|
void
|
2005-04-17 02:18:32 +00:00
|
|
|
fail_all(struct keylist *keylist,int err)
|
2002-10-09 02:03:22 +00:00
|
|
|
{
|
|
|
|
if(!keylist)
|
|
|
|
return;
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->action==KS_SEARCH)
|
2002-10-09 02:03:22 +00:00
|
|
|
{
|
|
|
|
fprintf(output,"SEARCH ");
|
|
|
|
while(keylist)
|
|
|
|
{
|
|
|
|
fprintf(output,"%s ",keylist->str);
|
|
|
|
keylist=keylist->next;
|
|
|
|
}
|
|
|
|
fprintf(output,"FAILED %d\n",err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while(keylist)
|
|
|
|
{
|
|
|
|
fprintf(output,"KEY %s FAILED %d\n",keylist->str,err);
|
|
|
|
keylist=keylist->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-28 04:25:25 +00:00
|
|
|
#ifdef HAVE_LIBCURL
|
|
|
|
/* If there is a SRV record, take the highest ranked possibility.
|
|
|
|
This is a hack, as we don't proceed downwards. */
|
|
|
|
static void
|
2009-07-08 04:01:13 +00:00
|
|
|
srv_replace(const char *srvtag)
|
2009-05-28 04:25:25 +00:00
|
|
|
{
|
|
|
|
#ifdef USE_DNS_SRV
|
|
|
|
struct srventry *srvlist=NULL;
|
|
|
|
int srvcount;
|
|
|
|
|
2009-07-08 04:01:13 +00:00
|
|
|
if(!srvtag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(1+strlen(srvtag)+6+strlen(opt->host)+1<=MAXDNAME)
|
2009-05-28 04:25:25 +00:00
|
|
|
{
|
|
|
|
char srvname[MAXDNAME];
|
|
|
|
|
|
|
|
strcpy(srvname,"_");
|
2009-07-08 04:01:13 +00:00
|
|
|
strcat(srvname,srvtag);
|
2009-05-28 04:25:25 +00:00
|
|
|
strcat(srvname,"._tcp.");
|
|
|
|
strcat(srvname,opt->host);
|
|
|
|
srvcount=getsrv(srvname,&srvlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(srvlist)
|
|
|
|
{
|
|
|
|
char *newname,*newport;
|
|
|
|
|
|
|
|
newname=strdup(srvlist->target);
|
|
|
|
newport=malloc(MAX_PORT);
|
|
|
|
if(newname && newport)
|
|
|
|
{
|
|
|
|
free(opt->host);
|
|
|
|
free(opt->port);
|
|
|
|
opt->host=newname;
|
|
|
|
snprintf(newport,MAX_PORT,"%u",srvlist->port);
|
|
|
|
opt->port=newport;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(newname);
|
|
|
|
free(newport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-18 14:09:47 +01:00
|
|
|
static void
|
2004-10-15 12:19:06 +00:00
|
|
|
show_help (FILE *fp)
|
|
|
|
{
|
2009-05-11 03:21:41 +00:00
|
|
|
fprintf (fp,"-h, --help\thelp\n");
|
|
|
|
fprintf (fp,"-V\t\tmachine readable version\n");
|
|
|
|
fprintf (fp,"--version\thuman readable version\n");
|
|
|
|
fprintf (fp,"-o\t\toutput to this file\n");
|
2004-10-15 12:19:06 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 18:45:36 +00:00
|
|
|
int
|
|
|
|
main(int argc,char *argv[])
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2009-05-28 04:25:25 +00:00
|
|
|
int arg,ret=KEYSERVER_INTERNAL_ERROR,try_srv=1;
|
2002-06-29 13:31:13 +00:00
|
|
|
char line[MAX_LINE];
|
2002-08-28 04:04:37 +00:00
|
|
|
int failed=0;
|
2002-06-29 13:31:13 +00:00
|
|
|
struct keylist *keylist=NULL,*keyptr=NULL;
|
2005-06-23 23:42:35 +00:00
|
|
|
char *proxy=NULL;
|
2009-05-28 16:20:49 +00:00
|
|
|
struct curl_slist *headers=NULL;
|
2002-06-29 13:31:13 +00:00
|
|
|
|
|
|
|
console=stderr;
|
|
|
|
|
2004-10-15 12:19:06 +00:00
|
|
|
/* Kludge to implement standard GNU options. */
|
|
|
|
if (argc > 1 && !strcmp (argv[1], "--version"))
|
|
|
|
{
|
2013-11-18 14:09:47 +01:00
|
|
|
printf ("gpgkeys_hkp (%s) %s\n", GNUPG_NAME, VERSION);
|
2009-05-11 03:21:41 +00:00
|
|
|
printf ("Uses: %s\n", curl_version());
|
2004-10-15 12:19:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (argc > 1 && !strcmp (argv[1], "--help"))
|
|
|
|
{
|
|
|
|
show_help (stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-24 22:33:22 +00:00
|
|
|
while((arg=getopt(argc,argv,"hVo:"))!=-1)
|
2002-06-29 13:31:13 +00:00
|
|
|
switch(arg)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 'h':
|
2004-10-15 12:19:06 +00:00
|
|
|
show_help (console);
|
2002-06-29 13:31:13 +00:00
|
|
|
return KEYSERVER_OK;
|
|
|
|
|
2002-10-24 22:33:22 +00:00
|
|
|
case 'V':
|
2002-11-18 00:43:33 +00:00
|
|
|
fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
|
2002-10-24 22:33:22 +00:00
|
|
|
return KEYSERVER_OK;
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
case 'o':
|
|
|
|
output=fopen(optarg,"w");
|
|
|
|
if(output==NULL)
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: Cannot open output file '%s': %s\n",
|
2002-06-29 13:31:13 +00:00
|
|
|
optarg,strerror(errno));
|
|
|
|
return KEYSERVER_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(argc>optind)
|
|
|
|
{
|
|
|
|
input=fopen(argv[optind],"r");
|
|
|
|
if(input==NULL)
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
fprintf(console,"gpgkeys: Cannot open input file '%s': %s\n",
|
2002-06-29 13:31:13 +00:00
|
|
|
argv[optind],strerror(errno));
|
|
|
|
return KEYSERVER_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input==NULL)
|
|
|
|
input=stdin;
|
|
|
|
|
|
|
|
if(output==NULL)
|
|
|
|
output=stdout;
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
opt=init_ks_options();
|
|
|
|
if(!opt)
|
|
|
|
return KEYSERVER_NO_MEMORY;
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
/* Get the command and info block */
|
|
|
|
|
|
|
|
while(fgets(line,MAX_LINE,input)!=NULL)
|
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
int err;
|
2005-01-13 23:22:10 +00:00
|
|
|
char option[MAX_OPTION+1];
|
2002-06-29 13:31:13 +00:00
|
|
|
|
|
|
|
if(line[0]=='\n')
|
|
|
|
break;
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
err=parse_ks_options(line,opt);
|
|
|
|
if(err>0)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
ret=err;
|
|
|
|
goto fail;
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
2005-04-17 02:18:32 +00:00
|
|
|
else if(err==0)
|
|
|
|
continue;
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-01-13 23:22:10 +00:00
|
|
|
if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
|
|
|
int no=0;
|
2005-01-13 23:22:10 +00:00
|
|
|
char *start=&option[0];
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-01-13 23:22:10 +00:00
|
|
|
option[MAX_OPTION]='\0';
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-01-13 23:22:10 +00:00
|
|
|
if(strncasecmp(option,"no-",3)==0)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
|
|
|
no=1;
|
2005-01-13 23:22:10 +00:00
|
|
|
start=&option[3];
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(strncasecmp(start,"http-proxy",10)==0)
|
2002-07-01 17:46:56 +00:00
|
|
|
{
|
2002-08-27 19:11:36 +00:00
|
|
|
if(no)
|
2003-12-28 16:21:46 +00:00
|
|
|
{
|
2005-06-23 23:42:35 +00:00
|
|
|
free(proxy);
|
|
|
|
proxy=strdup("");
|
2003-12-28 16:21:46 +00:00
|
|
|
}
|
2005-06-23 23:42:35 +00:00
|
|
|
else if(start[10]=='=')
|
2003-12-28 16:21:46 +00:00
|
|
|
{
|
2005-06-23 23:42:35 +00:00
|
|
|
if(strlen(&start[11])<MAX_PROXY)
|
2003-12-28 16:21:46 +00:00
|
|
|
{
|
2005-06-23 23:42:35 +00:00
|
|
|
free(proxy);
|
|
|
|
proxy=strdup(&start[11]);
|
2003-12-28 16:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-27 19:11:36 +00:00
|
|
|
}
|
2003-03-11 17:42:07 +00:00
|
|
|
else if(strcasecmp(start,"try-dns-srv")==0)
|
|
|
|
{
|
|
|
|
if(no)
|
2009-05-28 04:25:25 +00:00
|
|
|
try_srv=0;
|
2003-03-11 17:42:07 +00:00
|
|
|
else
|
2009-05-28 04:25:25 +00:00
|
|
|
try_srv=1;
|
2003-03-11 17:42:07 +00:00
|
|
|
}
|
2009-05-28 04:25:25 +00:00
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 03:52:34 +00:00
|
|
|
if(!opt->scheme)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: no scheme supplied!\n");
|
|
|
|
ret=KEYSERVER_SCHEME_NOT_FOUND;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-01-10 14:30:17 +01:00
|
|
|
if(ascii_strcasecmp(opt->scheme,"hkps")==0)
|
2009-05-11 03:52:34 +00:00
|
|
|
{
|
2009-05-28 04:25:25 +00:00
|
|
|
proto="https";
|
2009-05-11 03:52:34 +00:00
|
|
|
port="443";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-28 04:25:25 +00:00
|
|
|
proto="http";
|
2009-05-11 03:52:34 +00:00
|
|
|
port="11371";
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(!opt->host)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: no keyserver host provided\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(opt->timeout && register_timeout()==-1)
|
2004-10-13 18:30:29 +00:00
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: unable to register timeout handler\n");
|
|
|
|
return KEYSERVER_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
curl=curl_easy_init();
|
|
|
|
if(!curl)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: unable to initialize curl\n");
|
|
|
|
ret=KEYSERVER_INTERNAL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-05-28 04:25:25 +00:00
|
|
|
/* If the user gives a :port, then disable SRV. The semantics of a
|
|
|
|
specified port and SRV do not play well together. */
|
|
|
|
if(opt->port)
|
|
|
|
port=opt->port;
|
|
|
|
else if(try_srv)
|
|
|
|
{
|
2009-07-08 04:01:13 +00:00
|
|
|
char *srvtag;
|
|
|
|
|
|
|
|
if(ks_strcasecmp(opt->scheme,"hkp")==0)
|
|
|
|
srvtag="pgpkey-http";
|
|
|
|
else if(ks_strcasecmp(opt->scheme,"hkps")==0)
|
|
|
|
srvtag="pgpkey-https";
|
|
|
|
else
|
|
|
|
srvtag=NULL;
|
|
|
|
|
2009-05-28 04:25:25 +00:00
|
|
|
#ifdef HAVE_LIBCURL
|
|
|
|
/* We're using libcurl, so fake SRV support via our wrapper.
|
|
|
|
This isn't as good as true SRV support, as we do not try all
|
|
|
|
possible targets at one particular level and work our way
|
2013-11-18 14:09:47 +01:00
|
|
|
down the list, but it's better than nothing. */
|
2009-07-08 04:01:13 +00:00
|
|
|
srv_replace(srvtag);
|
2009-05-28 04:25:25 +00:00
|
|
|
#else
|
|
|
|
/* We're using our internal curl shim, so we can use its (true)
|
|
|
|
SRV support. Obviously, CURLOPT_SRVTAG_GPG_HACK isn't a real
|
|
|
|
libcurl option. It's specific to our shim. */
|
2009-07-08 04:01:13 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_SRVTAG_GPG_HACK,srvtag);
|
2009-05-28 04:25:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer);
|
|
|
|
|
2005-06-23 23:42:35 +00:00
|
|
|
if(opt->auth)
|
|
|
|
curl_easy_setopt(curl,CURLOPT_USERPWD,opt->auth);
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->debug)
|
|
|
|
{
|
2005-08-04 03:59:16 +00:00
|
|
|
fprintf(console,"gpgkeys: curl version = %s\n",curl_version());
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_STDERR,console);
|
2008-04-14 19:41:40 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
|
2005-04-17 02:18:32 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 03:52:34 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,(long)opt->flags.check_cert);
|
|
|
|
curl_easy_setopt(curl,CURLOPT_CAINFO,opt->ca_cert_file);
|
|
|
|
|
2009-05-28 16:20:49 +00:00
|
|
|
/* Avoid caches to get the most recent copy of the key. This is bug
|
|
|
|
#1061. In pre-curl versions of the code, we didn't do it. Then
|
|
|
|
we did do it (as a curl default) until curl changed the default.
|
|
|
|
Now we're doing it again, but in such a way that changing
|
|
|
|
defaults in the future won't impact us. We set both the Pragma
|
|
|
|
and Cache-Control versions of the header, so we're good with both
|
|
|
|
HTTP 1.0 and 1.1. */
|
|
|
|
headers=curl_slist_append(headers,"Pragma: no-cache");
|
|
|
|
if(headers)
|
|
|
|
headers=curl_slist_append(headers,"Cache-Control: no-cache");
|
|
|
|
|
|
|
|
if(!headers)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory when building HTTP headers\n");
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
|
|
|
|
|
2005-06-23 23:42:35 +00:00
|
|
|
if(proxy)
|
2005-04-17 02:18:32 +00:00
|
|
|
curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
/* If it's a GET or a SEARCH, the next thing to come in is the
|
|
|
|
keyids. If it's a SEND, then there are no keyids. */
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->action==KS_SEND)
|
2002-06-29 13:31:13 +00:00
|
|
|
while(fgets(line,MAX_LINE,input)!=NULL && line[0]!='\n');
|
2005-12-23 20:51:48 +00:00
|
|
|
else if(opt->action==KS_GET
|
|
|
|
|| opt->action==KS_GETNAME || opt->action==KS_SEARCH)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
struct keylist *work;
|
|
|
|
|
|
|
|
if(fgets(line,MAX_LINE,input)==NULL)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
2004-01-12 04:09:37 +00:00
|
|
|
if(line[0]=='\n' || line[0]=='\0')
|
2002-06-29 13:31:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
work=malloc(sizeof(struct keylist));
|
|
|
|
if(work==NULL)
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: out of memory while "
|
|
|
|
"building key list\n");
|
2002-10-09 02:03:22 +00:00
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
2002-06-29 13:31:13 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(work->str,line);
|
|
|
|
|
|
|
|
/* Trim the trailing \n */
|
|
|
|
work->str[strlen(line)-1]='\0';
|
|
|
|
|
|
|
|
work->next=NULL;
|
|
|
|
|
|
|
|
/* Always attach at the end to keep the list in proper
|
|
|
|
order for searching */
|
|
|
|
if(keylist==NULL)
|
|
|
|
keylist=work;
|
|
|
|
else
|
|
|
|
keyptr->next=work;
|
|
|
|
|
|
|
|
keyptr=work;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(console,"gpgkeys: no keyserver command specified\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the response */
|
|
|
|
|
2002-10-14 20:01:05 +00:00
|
|
|
fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
|
2002-06-29 13:31:13 +00:00
|
|
|
fprintf(output,"PROGRAM %s\n\n",VERSION);
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->verbose>1)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"Host:\t\t%s\n",opt->host);
|
|
|
|
if(opt->port)
|
|
|
|
fprintf(console,"Port:\t\t%s\n",opt->port);
|
2005-06-21 04:24:10 +00:00
|
|
|
if(strcmp(opt->path,"/")!=0)
|
2005-06-04 23:09:27 +00:00
|
|
|
fprintf(console,"Path:\t\t%s\n",opt->path);
|
2005-04-17 02:18:32 +00:00
|
|
|
fprintf(console,"Command:\t%s\n",ks_action_to_string(opt->action));
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(opt->action==KS_GET)
|
2002-06-29 13:31:13 +00:00
|
|
|
{
|
|
|
|
keyptr=keylist;
|
|
|
|
|
|
|
|
while(keyptr!=NULL)
|
|
|
|
{
|
2005-04-17 02:18:32 +00:00
|
|
|
set_timeout(opt->timeout);
|
2004-10-13 18:30:29 +00:00
|
|
|
|
2002-09-24 19:50:09 +00:00
|
|
|
if(get_key(keyptr->str)!=KEYSERVER_OK)
|
2002-06-29 13:31:13 +00:00
|
|
|
failed++;
|
|
|
|
|
2005-12-23 20:51:48 +00:00
|
|
|
keyptr=keyptr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(opt->action==KS_GETNAME)
|
|
|
|
{
|
|
|
|
keyptr=keylist;
|
|
|
|
|
|
|
|
while(keyptr!=NULL)
|
|
|
|
{
|
|
|
|
set_timeout(opt->timeout);
|
|
|
|
|
|
|
|
if(get_name(keyptr->str)!=KEYSERVER_OK)
|
|
|
|
failed++;
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
keyptr=keyptr->next;
|
|
|
|
}
|
2005-04-17 02:18:32 +00:00
|
|
|
}
|
|
|
|
else if(opt->action==KS_SEND)
|
|
|
|
{
|
2007-06-14 17:05:07 +00:00
|
|
|
int myeof=0;
|
2004-10-13 18:30:29 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
set_timeout(opt->timeout);
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2007-06-14 17:05:07 +00:00
|
|
|
if(send_key(&myeof)!=KEYSERVER_OK)
|
2005-04-17 02:18:32 +00:00
|
|
|
failed++;
|
|
|
|
}
|
2007-06-14 17:05:07 +00:00
|
|
|
while(!myeof);
|
2005-04-17 02:18:32 +00:00
|
|
|
}
|
|
|
|
else if(opt->action==KS_SEARCH)
|
|
|
|
{
|
|
|
|
char *searchkey=NULL;
|
|
|
|
int len=0;
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
set_timeout(opt->timeout);
|
2004-10-13 18:30:29 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
/* To search, we stick a space in between each key to search
|
|
|
|
for. */
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
keyptr=keylist;
|
|
|
|
while(keyptr!=NULL)
|
|
|
|
{
|
|
|
|
len+=strlen(keyptr->str)+1;
|
|
|
|
keyptr=keyptr->next;
|
|
|
|
}
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
searchkey=malloc(len+1);
|
|
|
|
if(searchkey==NULL)
|
|
|
|
{
|
|
|
|
ret=KEYSERVER_NO_MEMORY;
|
|
|
|
fail_all(keylist,KEYSERVER_NO_MEMORY);
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
searchkey[0]='\0';
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
keyptr=keylist;
|
|
|
|
while(keyptr!=NULL)
|
|
|
|
{
|
|
|
|
strcat(searchkey,keyptr->str);
|
|
|
|
strcat(searchkey," ");
|
|
|
|
keyptr=keyptr->next;
|
|
|
|
}
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
/* Nail that last space */
|
|
|
|
if(*searchkey)
|
|
|
|
searchkey[strlen(searchkey)-1]='\0';
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
if(search_key(searchkey)!=KEYSERVER_OK)
|
|
|
|
failed++;
|
2002-06-29 13:31:13 +00:00
|
|
|
|
2005-04-17 02:18:32 +00:00
|
|
|
free(searchkey);
|
2002-06-29 13:31:13 +00:00
|
|
|
}
|
2005-04-17 02:18:32 +00:00
|
|
|
else
|
|
|
|
abort();
|
2002-06-29 13:31:13 +00:00
|
|
|
|
|
|
|
if(!failed)
|
|
|
|
ret=KEYSERVER_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
while(keylist!=NULL)
|
|
|
|
{
|
|
|
|
struct keylist *current=keylist;
|
|
|
|
keylist=keylist->next;
|
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input!=stdin)
|
|
|
|
fclose(input);
|
|
|
|
|
|
|
|
if(output!=stdout)
|
|
|
|
fclose(output);
|
|
|
|
|
2005-06-23 23:42:35 +00:00
|
|
|
free_ks_options(opt);
|
|
|
|
|
2009-05-28 16:20:49 +00:00
|
|
|
curl_slist_free_all(headers);
|
|
|
|
|
2005-06-23 23:42:35 +00:00
|
|
|
if(curl)
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
free(proxy);
|
|
|
|
|
2002-06-29 13:31:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|