~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to keyserver/gpgkeys_finger.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* gpgkeys_finger.c - fetch a key via finger
2
 
 * Copyright (C) 2004 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
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
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
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
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
45
44
#include "util.h"
46
45
#include "keyserver.h"
47
46
#include "ksutil.h"
 
47
#include "iobuf.h"
48
48
 
49
49
#ifdef _WIN32
50
50
#define sock_close(a)  closesocket(a)
55
55
extern char *optarg;
56
56
extern int optind;
57
57
 
58
 
#define GET    0
59
 
#define MAX_LINE 80
60
 
 
61
 
static int verbose=0;
62
 
static char path[1024];
63
 
static FILE *input, *output, *console;
64
 
 
65
 
#define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
66
 
#define END   "-----END PGP PUBLIC KEY BLOCK-----"
 
58
static FILE *input,*output,*console;
 
59
static struct ks_options *opt;
67
60
 
68
61
#ifdef _WIN32
69
62
static void
120
113
 
121
114
  /* Win32 gethostbyname doesn't handle IP addresses internally, so we
122
115
     try inet_addr first on that platform only. */
123
 
  if ((l = inet_addr (server)) != SOCKET_ERROR) 
 
116
  if ((l = inet_addr (server)) != INADDR_NONE) 
124
117
    memcpy (&addr.sin_addr, &l, sizeof l);
125
118
  else if ((hp = gethostbyname (server))) 
126
119
    {
295
288
{
296
289
  int rc;
297
290
  int sock;
298
 
  IOBUF fp_read;
 
291
  iobuf_t fp_read;
299
292
  unsigned int maxlen, buflen, gotit=0;
300
293
  byte *line = NULL;
301
294
 
306
299
     indicated the requested key anyway. */
307
300
  fprintf(output,"KEY 0x%s BEGIN\n",getkey);
308
301
 
309
 
  rc = send_request (path, &sock);
 
302
  rc=send_request(opt->opaque,&sock);
310
303
  if(rc)
311
304
    {
312
305
      fprintf(output,"KEY 0x%s FAILED %d\n",getkey, rc);
330
323
      
331
324
      if(gotit)
332
325
        {
333
 
          fputs (line, output);
334
 
          if (!strncmp(line,END,strlen(END)))
 
326
          print_nocr(output, (const char*)line);
 
327
          if (!strncmp((char*)line,END,strlen(END)))
335
328
            break;
336
329
        }
337
 
      else if(!strncmp(line,BEGIN,strlen(BEGIN)))
 
330
      else if(!strncmp((char*)line,BEGIN,strlen(BEGIN)))
338
331
        {
339
 
          fputs (line,output);
 
332
          print_nocr(output, (const char*)line);
340
333
          gotit=1;
341
334
        }
342
335
    }
345
338
    fprintf (output,"KEY 0x%s END\n", getkey);
346
339
  else
347
340
    {
348
 
      fprintf (console,"gpgkeys: key %s not found on keyserver\n",getkey);
349
 
      fprintf (output,"KEY 0x%s FAILED %d\n",
350
 
               getkey,KEYSERVER_KEY_NOT_FOUND);
 
341
      fprintf(console,"gpgkeys: no key data found for finger:%s\n",
 
342
              opt->opaque);
 
343
      fprintf(output,"KEY 0x%s FAILED %d\n",getkey,KEYSERVER_KEY_NOT_FOUND);
351
344
    }
352
345
 
353
 
  m_free(line);
 
346
  xfree(line);
354
347
  iobuf_close (fp_read);
355
348
 
356
349
  return KEYSERVER_OK;
368
361
int
369
362
main(int argc,char *argv[])
370
363
{
371
 
  int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
 
364
  int arg,ret=KEYSERVER_INTERNAL_ERROR;
372
365
  char line[MAX_LINE];
373
366
  char *thekey=NULL;
374
 
  unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
375
367
 
376
368
  console=stderr;
377
369
 
378
370
  /* Kludge to implement standard GNU options.  */
379
371
  if (argc > 1 && !strcmp (argv[1], "--version"))
380
372
    {
381
 
      fputs ("gpgkeys_ldap (GnuPG) " VERSION"\n", stdout);
 
373
      fputs ("gpgkeys_finger (GnuPG) " VERSION"\n", stdout);
382
374
      return 0;
383
375
    }
384
376
  else if (argc > 1 && !strcmp (argv[1], "--help"))
428
420
  if(output==NULL)
429
421
    output=stdout;
430
422
 
 
423
  opt=init_ks_options();
 
424
  if(!opt)
 
425
    return KEYSERVER_NO_MEMORY;
 
426
 
431
427
  /* Get the command and info block */
432
428
 
433
429
  while(fgets(line,MAX_LINE,input)!=NULL)
434
430
    {
435
 
      int version;
436
 
      char commandstr[7];
437
 
      char optionstr[256];
438
 
      char hash;
 
431
      int err;
439
432
 
440
433
      if(line[0]=='\n')
441
434
        break;
442
435
 
443
 
      if(sscanf(line,"%c",&hash)==1 && hash=='#')
 
436
      err=parse_ks_options(line,opt);
 
437
      if(err>0)
 
438
        {
 
439
          ret=err;
 
440
          goto fail;
 
441
        }
 
442
      else if(err==0)
444
443
        continue;
445
 
 
446
 
      if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
447
 
        {
448
 
          commandstr[6]='\0';
449
 
 
450
 
          if(strcasecmp(commandstr,"get")==0)
451
 
            action=GET;
452
 
 
453
 
          continue;
454
 
        }
455
 
 
456
 
      if(strncmp(line,"HOST ",5)==0)
457
 
        {
458
 
          fprintf(console,"gpgkeys: finger://relay/user syntax is not"
459
 
                  " supported.  Use finger:user instead.\n");
460
 
          ret=KEYSERVER_NOT_SUPPORTED;
461
 
          goto fail;
462
 
        }
463
 
 
464
 
      if(sscanf(line,"OPAQUE %1023s\n",path)==1)
465
 
        {
466
 
          path[1023]='\0';
467
 
          continue;
468
 
        }
469
 
 
470
 
      if(sscanf(line,"VERSION %d\n",&version)==1)
471
 
        {
472
 
          if(version!=KEYSERVER_PROTO_VERSION)
473
 
            {
474
 
              ret=KEYSERVER_VERSION_ERROR;
475
 
              goto fail;
476
 
            }
477
 
 
478
 
          continue;
479
 
        }
480
 
 
481
 
      if(sscanf(line,"OPTION %255s\n",optionstr)==1)
482
 
        {
483
 
          int no=0;
484
 
          char *start=&optionstr[0];
485
 
 
486
 
          optionstr[255]='\0';
487
 
 
488
 
          if(strncasecmp(optionstr,"no-",3)==0)
489
 
            {
490
 
              no=1;
491
 
              start=&optionstr[3];
492
 
            }
493
 
 
494
 
          if(strcasecmp(start,"verbose")==0)
495
 
            {
496
 
              if(no)
497
 
                verbose--;
498
 
              else
499
 
                verbose++;
500
 
            }
501
 
          else if(strncasecmp(start,"timeout",7)==0)
502
 
            {
503
 
              if(no)
504
 
                timeout=0;
505
 
              else if(start[7]=='=')
506
 
                timeout=atoi(&start[8]);
507
 
              else if(start[7]=='\0')
508
 
                timeout=DEFAULT_KEYSERVER_TIMEOUT;
509
 
            }
510
 
 
511
 
          continue;
512
 
        }
513
 
    }
514
 
 
515
 
  if(timeout && register_timeout()==-1)
 
444
    }
 
445
 
 
446
  if(opt->host)
 
447
    {
 
448
      fprintf(console,"gpgkeys: finger://relay/user syntax is not"
 
449
              " supported.  Use finger:user instead.\n");
 
450
      ret=KEYSERVER_NOT_SUPPORTED;
 
451
      goto fail;
 
452
    }
 
453
 
 
454
  if(opt->timeout && register_timeout()==-1)
516
455
    {
517
456
      fprintf(console,"gpgkeys: unable to register timeout handler\n");
518
457
      return KEYSERVER_INTERNAL_ERROR;
521
460
  /* If it's a GET or a SEARCH, the next thing to come in is the
522
461
     keyids.  If it's a SEND, then there are no keyids. */
523
462
 
524
 
  if(action==GET)
 
463
  if(opt->action==KS_GET)
525
464
    {
526
465
      /* Eat the rest of the file */
527
466
      for(;;)
557
496
      goto fail;
558
497
    }
559
498
 
560
 
  if(!thekey || !*path)
 
499
  if(!thekey || !opt->opaque)
561
500
    {
562
501
      fprintf(console,"gpgkeys: invalid keyserver instructions\n");
563
502
      goto fail;
568
507
  fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
569
508
  fprintf(output,"PROGRAM %s\n\n",VERSION);
570
509
 
571
 
  if (verbose>1)
 
510
  if(opt->verbose>1)
572
511
    {
573
 
      if(path[0])
574
 
        fprintf(console,"Path:\t\t%s\n",path);
 
512
      fprintf(console,"User:\t\t%s\n",opt->opaque);
575
513
      fprintf(console,"Command:\tGET\n");
576
514
    }
577
515
 
578
 
  set_timeout(timeout);
 
516
  set_timeout(opt->timeout);
579
517
 
580
 
  ret = get_key(thekey);
 
518
  ret=get_key(thekey);
581
519
 
582
520
 fail:
583
521
 
589
527
  if(output!=stdout)
590
528
    fclose(output);
591
529
 
 
530
  free_ks_options(opt);
 
531
 
592
532
  return ret;
593
533
}