1
/* gpgkeys_http.c - fetch a key via HTTP
2
* Copyright (C) 2004 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30
#define INCLUDED_BY_MAIN_MODULE 1
33
#include "keyserver.h"
43
static unsigned int http_flags=0;
44
static char auth[128]={'\0'},host[80]={'\0'},proxy[80]={'\0'},port[10]={'\0'},path[1024]={'\0'};
45
static FILE *input=NULL,*output=NULL,*console=NULL;
47
#define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
48
#define END "-----END PGP PUBLIC KEY BLOCK-----"
51
#define HTTP_PROXY_ENV "GnuPG$HttpProxy"
53
#define HTTP_PROXY_ENV "http_proxy"
61
struct http_context hd;
63
if(strncmp(getkey,"0x",2)==0)
66
fprintf(output,"KEY 0x%s BEGIN\n",getkey);
68
request=malloc(4+3+strlen(host)+1+strlen(port)+1+strlen(path)+50);
71
fprintf(console,"gpgkeys: out of memory\n");
72
return KEYSERVER_NO_MEMORY;
75
sprintf(request,"http://%s%s%s%s%s%s%s",auth[0]?auth:"",auth[0]?"@":"",
76
host,port[0]?":":"",port[0]?port:"",path[0]?"":"/",path);
78
rc=http_open_document(&hd,request,http_flags,proxy[0]?proxy:NULL);
81
fprintf(console,"gpgkeys: HTTP fetch error: %s\n",
82
rc==G10ERR_NETWORK?strerror(errno):g10_errstr(rc));
83
fprintf(output,"KEY 0x%s FAILED %d\n",getkey,
84
rc==G10ERR_NETWORK?KEYSERVER_UNREACHABLE:KEYSERVER_INTERNAL_ERROR);
88
unsigned int maxlen=1024,buflen,gotit=0;
91
while(iobuf_read_line(hd.fp_read,&line,&buflen,&maxlen))
98
if(strncmp(line,END,strlen(END))==0)
102
if(strncmp(line,BEGIN,strlen(BEGIN))==0)
110
fprintf(output,"KEY 0x%s END\n",getkey);
113
fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey);
114
fprintf(output,"KEY 0x%s FAILED %d\n",
115
getkey,KEYSERVER_KEY_NOT_FOUND);
129
fprintf (fp,"-h\thelp\n");
130
fprintf (fp,"-V\tversion\n");
131
fprintf (fp,"-o\toutput to this file\n");
135
main(int argc,char *argv[])
137
int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
140
unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
144
/* Kludge to implement standard GNU options. */
145
if (argc > 1 && !strcmp (argv[1], "--version"))
147
fputs ("gpgkeys_http (GnuPG) " VERSION"\n", stdout);
150
else if (argc > 1 && !strcmp (argv[1], "--help"))
156
while((arg=getopt(argc,argv,"hVo:"))!=-1)
165
fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
169
output=fopen(optarg,"w");
172
fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
173
optarg,strerror(errno));
174
return KEYSERVER_INTERNAL_ERROR;
182
input=fopen(argv[optind],"r");
185
fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
186
argv[optind],strerror(errno));
187
return KEYSERVER_INTERNAL_ERROR;
197
/* Get the command and info block */
199
while(fgets(line,MAX_LINE,input)!=NULL)
209
if(sscanf(line,"%c",&hash)==1 && hash=='#')
212
if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
216
if(strcasecmp(commandstr,"get")==0)
222
if(sscanf(line,"AUTH %127s\n",auth)==1)
228
if(sscanf(line,"HOST %79s\n",host)==1)
234
if(sscanf(line,"PORT %9s\n",port)==1)
240
if(sscanf(line,"PATH %1023s\n",path)==1)
246
if(sscanf(line,"VERSION %d\n",&version)==1)
248
if(version!=KEYSERVER_PROTO_VERSION)
250
ret=KEYSERVER_VERSION_ERROR;
257
if(sscanf(line,"OPTION %255s\n",optionstr)==1)
260
char *start=&optionstr[0];
264
if(strncasecmp(optionstr,"no-",3)==0)
270
if(strcasecmp(start,"verbose")==0)
277
else if(strncasecmp(start,"http-proxy",10)==0)
281
else if(start[10]=='=')
283
strncpy(proxy,&start[11],79);
286
else if(start[10]=='\0')
288
char *http_proxy=getenv(HTTP_PROXY_ENV);
291
strncpy(proxy,http_proxy,79);
296
else if(strcasecmp(start,"broken-http-proxy")==0)
299
http_flags&=~HTTP_FLAG_NO_SHUTDOWN;
301
http_flags|=HTTP_FLAG_NO_SHUTDOWN;
303
else if(strcasecmp(start,"try-dns-srv")==0)
306
http_flags&=~HTTP_FLAG_TRY_SRV;
308
http_flags|=HTTP_FLAG_TRY_SRV;
310
else if(strncasecmp(start,"timeout",7)==0)
314
else if(start[7]=='=')
315
timeout=atoi(&start[8]);
316
else if(start[7]=='\0')
317
timeout=DEFAULT_KEYSERVER_TIMEOUT;
324
if(timeout && register_timeout()==-1)
326
fprintf(console,"gpgkeys: unable to register timeout handler\n");
327
return KEYSERVER_INTERNAL_ERROR;
330
/* By suggested convention, if the user gives a :port, then disable
333
http_flags&=~HTTP_FLAG_TRY_SRV;
335
/* If it's a GET or a SEARCH, the next thing to come in is the
336
keyids. If it's a SEND, then there are no keyids. */
340
/* Eat the rest of the file */
343
if(fgets(line,MAX_LINE,input)==NULL)
347
if(line[0]=='\n' || line[0]=='\0')
355
fprintf(console,"gpgkeys: out of memory while "
356
"building key list\n");
357
ret=KEYSERVER_NO_MEMORY;
361
/* Trim the trailing \n */
362
thekey[strlen(line)-1]='\0';
370
"gpgkeys: this keyserver type only supports key retrieval\n");
374
if(!thekey || !host[0])
376
fprintf(console,"gpgkeys: invalid keyserver instructions\n");
380
/* Send the response */
382
fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
383
fprintf(output,"PROGRAM %s\n\n",VERSION);
387
fprintf(console,"Host:\t\t%s\n",host);
389
fprintf(console,"Port:\t\t%s\n",port);
391
fprintf(console,"Path:\t\t%s\n",path);
392
fprintf(console,"Command:\tGET\n");
395
set_timeout(timeout);