~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to keyserver/gpgkeys_http.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gpgkeys_http.c - fetch a key via HTTP
 
2
 * Copyright (C) 2004 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
#include <stdlib.h>
 
25
#include <errno.h>
 
26
#include <unistd.h>
 
27
#ifdef HAVE_GETOPT_H
 
28
#include <getopt.h>
 
29
#endif
 
30
#define INCLUDED_BY_MAIN_MODULE 1
 
31
#include "util.h"
 
32
#include "http.h"
 
33
#include "keyserver.h"
 
34
#include "ksutil.h"
 
35
 
 
36
extern char *optarg;
 
37
extern int optind;
 
38
 
 
39
#define GET    0
 
40
#define MAX_LINE 80
 
41
 
 
42
static int verbose=0;
 
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;
 
46
 
 
47
#define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
 
48
#define END   "-----END PGP PUBLIC KEY BLOCK-----"
 
49
 
 
50
#ifdef __riscos__
 
51
#define HTTP_PROXY_ENV           "GnuPG$HttpProxy"
 
52
#else
 
53
#define HTTP_PROXY_ENV           "http_proxy"
 
54
#endif
 
55
 
 
56
static int
 
57
get_key(char *getkey)
 
58
{
 
59
  int rc;
 
60
  char *request;
 
61
  struct http_context hd;
 
62
 
 
63
  if(strncmp(getkey,"0x",2)==0)
 
64
    getkey+=2;
 
65
 
 
66
  fprintf(output,"KEY 0x%s BEGIN\n",getkey);
 
67
 
 
68
  request=malloc(4+3+strlen(host)+1+strlen(port)+1+strlen(path)+50);
 
69
  if(!request)
 
70
    {
 
71
      fprintf(console,"gpgkeys: out of memory\n");
 
72
      return KEYSERVER_NO_MEMORY;
 
73
    }
 
74
 
 
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);
 
77
 
 
78
  rc=http_open_document(&hd,request,http_flags,proxy[0]?proxy:NULL);
 
79
  if(rc!=0)
 
80
    {
 
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);
 
85
    }
 
86
  else
 
87
    {
 
88
      unsigned int maxlen=1024,buflen,gotit=0;
 
89
      byte *line=NULL;
 
90
 
 
91
      while(iobuf_read_line(hd.fp_read,&line,&buflen,&maxlen))
 
92
        {
 
93
          maxlen=1024;
 
94
 
 
95
          if(gotit)
 
96
            {
 
97
              fputs(line,output);
 
98
              if(strncmp(line,END,strlen(END))==0)
 
99
                break;
 
100
            }
 
101
          else
 
102
            if(strncmp(line,BEGIN,strlen(BEGIN))==0)
 
103
              {
 
104
                fputs(line,output);
 
105
                gotit=1;
 
106
              }
 
107
        }
 
108
 
 
109
      if(gotit)
 
110
        fprintf(output,"KEY 0x%s END\n",getkey);
 
111
      else
 
112
        {
 
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);
 
116
        }
 
117
 
 
118
      m_free(line);
 
119
    }
 
120
 
 
121
  free(request);
 
122
 
 
123
  return KEYSERVER_OK;
 
124
}
 
125
 
 
126
static void 
 
127
show_help (FILE *fp)
 
128
{
 
129
  fprintf (fp,"-h\thelp\n");
 
130
  fprintf (fp,"-V\tversion\n");
 
131
  fprintf (fp,"-o\toutput to this file\n");
 
132
}
 
133
 
 
134
int
 
135
main(int argc,char *argv[])
 
136
{
 
137
  int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
 
138
  char line[MAX_LINE];
 
139
  char *thekey=NULL;
 
140
  unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
 
141
 
 
142
  console=stderr;
 
143
 
 
144
  /* Kludge to implement standard GNU options.  */
 
145
  if (argc > 1 && !strcmp (argv[1], "--version"))
 
146
    {
 
147
      fputs ("gpgkeys_http (GnuPG) " VERSION"\n", stdout);
 
148
      return 0;
 
149
    }
 
150
  else if (argc > 1 && !strcmp (argv[1], "--help"))
 
151
    {
 
152
      show_help (stdout);
 
153
      return 0;
 
154
    }
 
155
 
 
156
  while((arg=getopt(argc,argv,"hVo:"))!=-1)
 
157
    switch(arg)
 
158
      {
 
159
      default:
 
160
      case 'h':
 
161
        show_help (console);
 
162
        return KEYSERVER_OK;
 
163
 
 
164
      case 'V':
 
165
        fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
 
166
        return KEYSERVER_OK;
 
167
 
 
168
      case 'o':
 
169
        output=fopen(optarg,"w");
 
170
        if(output==NULL)
 
171
          {
 
172
            fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
 
173
                    optarg,strerror(errno));
 
174
            return KEYSERVER_INTERNAL_ERROR;
 
175
          }
 
176
 
 
177
        break;
 
178
      }
 
179
 
 
180
  if(argc>optind)
 
181
    {
 
182
      input=fopen(argv[optind],"r");
 
183
      if(input==NULL)
 
184
        {
 
185
          fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
 
186
                  argv[optind],strerror(errno));
 
187
          return KEYSERVER_INTERNAL_ERROR;
 
188
        }
 
189
    }
 
190
 
 
191
  if(input==NULL)
 
192
    input=stdin;
 
193
 
 
194
  if(output==NULL)
 
195
    output=stdout;
 
196
 
 
197
  /* Get the command and info block */
 
198
 
 
199
  while(fgets(line,MAX_LINE,input)!=NULL)
 
200
    {
 
201
      int version;
 
202
      char commandstr[7];
 
203
      char optionstr[256];
 
204
      char hash;
 
205
 
 
206
      if(line[0]=='\n')
 
207
        break;
 
208
 
 
209
      if(sscanf(line,"%c",&hash)==1 && hash=='#')
 
210
        continue;
 
211
 
 
212
      if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
 
213
        {
 
214
          commandstr[6]='\0';
 
215
 
 
216
          if(strcasecmp(commandstr,"get")==0)
 
217
            action=GET;
 
218
 
 
219
          continue;
 
220
        }
 
221
 
 
222
      if(sscanf(line,"AUTH %127s\n",auth)==1)
 
223
        {
 
224
          auth[127]='\0';
 
225
          continue;
 
226
        }
 
227
 
 
228
      if(sscanf(line,"HOST %79s\n",host)==1)
 
229
        {
 
230
          host[79]='\0';
 
231
          continue;
 
232
        }
 
233
 
 
234
      if(sscanf(line,"PORT %9s\n",port)==1)
 
235
        {
 
236
          port[9]='\0';
 
237
          continue;
 
238
        }
 
239
 
 
240
      if(sscanf(line,"PATH %1023s\n",path)==1)
 
241
        {
 
242
          path[1023]='\0';
 
243
          continue;
 
244
        }
 
245
 
 
246
      if(sscanf(line,"VERSION %d\n",&version)==1)
 
247
        {
 
248
          if(version!=KEYSERVER_PROTO_VERSION)
 
249
            {
 
250
              ret=KEYSERVER_VERSION_ERROR;
 
251
              goto fail;
 
252
            }
 
253
 
 
254
          continue;
 
255
        }
 
256
 
 
257
      if(sscanf(line,"OPTION %255s\n",optionstr)==1)
 
258
        {
 
259
          int no=0;
 
260
          char *start=&optionstr[0];
 
261
 
 
262
          optionstr[255]='\0';
 
263
 
 
264
          if(strncasecmp(optionstr,"no-",3)==0)
 
265
            {
 
266
              no=1;
 
267
              start=&optionstr[3];
 
268
            }
 
269
 
 
270
          if(strcasecmp(start,"verbose")==0)
 
271
            {
 
272
              if(no)
 
273
                verbose--;
 
274
              else
 
275
                verbose++;
 
276
            }
 
277
          else if(strncasecmp(start,"http-proxy",10)==0)
 
278
            {
 
279
              if(no)
 
280
                proxy[0]='\0';
 
281
              else if(start[10]=='=')
 
282
                {
 
283
                  strncpy(proxy,&start[11],79);
 
284
                  proxy[79]='\0';
 
285
                }
 
286
              else if(start[10]=='\0')
 
287
                {
 
288
                  char *http_proxy=getenv(HTTP_PROXY_ENV);
 
289
                  if(http_proxy)
 
290
                    {
 
291
                      strncpy(proxy,http_proxy,79);
 
292
                      proxy[79]='\0';
 
293
                    }
 
294
                }
 
295
            }
 
296
          else if(strcasecmp(start,"broken-http-proxy")==0)
 
297
            {
 
298
              if(no)
 
299
                http_flags&=~HTTP_FLAG_NO_SHUTDOWN;
 
300
              else
 
301
                http_flags|=HTTP_FLAG_NO_SHUTDOWN;
 
302
            }
 
303
          else if(strcasecmp(start,"try-dns-srv")==0)
 
304
            {
 
305
              if(no)
 
306
                http_flags&=~HTTP_FLAG_TRY_SRV;
 
307
              else
 
308
                http_flags|=HTTP_FLAG_TRY_SRV;
 
309
            }
 
310
          else if(strncasecmp(start,"timeout",7)==0)
 
311
            {
 
312
              if(no)
 
313
                timeout=0;
 
314
              else if(start[7]=='=')
 
315
                timeout=atoi(&start[8]);
 
316
              else if(start[7]=='\0')
 
317
                timeout=DEFAULT_KEYSERVER_TIMEOUT;
 
318
            }
 
319
 
 
320
          continue;
 
321
        }
 
322
    }
 
323
 
 
324
  if(timeout && register_timeout()==-1)
 
325
    {
 
326
      fprintf(console,"gpgkeys: unable to register timeout handler\n");
 
327
      return KEYSERVER_INTERNAL_ERROR;
 
328
    }
 
329
 
 
330
  /* By suggested convention, if the user gives a :port, then disable
 
331
     SRV. */
 
332
  if(port[0])
 
333
    http_flags&=~HTTP_FLAG_TRY_SRV;
 
334
 
 
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. */
 
337
 
 
338
  if(action==GET)
 
339
    {
 
340
      /* Eat the rest of the file */
 
341
      for(;;)
 
342
        {
 
343
          if(fgets(line,MAX_LINE,input)==NULL)
 
344
            break;
 
345
          else
 
346
            {
 
347
              if(line[0]=='\n' || line[0]=='\0')
 
348
                break;
 
349
 
 
350
              if(!thekey)
 
351
                {
 
352
                  thekey=strdup(line);
 
353
                  if(!thekey)
 
354
                    {
 
355
                      fprintf(console,"gpgkeys: out of memory while "
 
356
                              "building key list\n");
 
357
                      ret=KEYSERVER_NO_MEMORY;
 
358
                      goto fail;
 
359
                    }
 
360
 
 
361
                  /* Trim the trailing \n */
 
362
                  thekey[strlen(line)-1]='\0';
 
363
                }
 
364
            }
 
365
        }
 
366
    }
 
367
  else
 
368
    {
 
369
      fprintf(console,
 
370
              "gpgkeys: this keyserver type only supports key retrieval\n");
 
371
      goto fail;
 
372
    }
 
373
 
 
374
  if(!thekey || !host[0])
 
375
    {
 
376
      fprintf(console,"gpgkeys: invalid keyserver instructions\n");
 
377
      goto fail;
 
378
    }
 
379
 
 
380
  /* Send the response */
 
381
 
 
382
  fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
 
383
  fprintf(output,"PROGRAM %s\n\n",VERSION);
 
384
 
 
385
  if(verbose>1)
 
386
    {
 
387
      fprintf(console,"Host:\t\t%s\n",host);
 
388
      if(port[0])
 
389
        fprintf(console,"Port:\t\t%s\n",port);
 
390
      if(path[0])
 
391
        fprintf(console,"Path:\t\t%s\n",path);
 
392
      fprintf(console,"Command:\tGET\n");
 
393
    }
 
394
 
 
395
  set_timeout(timeout);
 
396
 
 
397
  ret=get_key(thekey);
 
398
 
 
399
 fail:
 
400
 
 
401
  free(thekey);
 
402
 
 
403
  if(input!=stdin)
 
404
    fclose(input);
 
405
 
 
406
  if(output!=stdout)
 
407
    fclose(output);
 
408
 
 
409
  return ret;
 
410
}