~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to g10/keyserver.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
/* keyserver.c - generic keyserver code
2
 
 * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006,
 
3
 *               2007 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * 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
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
24
24
#include <string.h>
25
25
#include <stdlib.h>
26
26
#include <assert.h>
 
27
#include <errno.h>
27
28
 
28
29
#include "gpg.h"
 
30
#include "iobuf.h"
29
31
#include "filter.h"
30
32
#include "keydb.h"
31
33
#include "status.h"
32
34
#include "exec.h"
33
35
#include "main.h"
34
36
#include "i18n.h"
35
 
#include "iobuf.h"
36
 
#include "memory.h"
37
37
#include "ttyio.h"
38
38
#include "options.h"
39
39
#include "packet.h"
 
40
#include "trustdb.h"
40
41
#include "keyserver-internal.h"
41
42
#include "util.h"
 
43
#include "dns-cert.h"
 
44
#include "pka.h"
42
45
 
43
 
#define GET    0
44
 
#define SEND   1
45
 
#define SEARCH 2
 
46
#ifdef HAVE_W32_SYSTEM
 
47
/* It seems Vista doesn't grok X_OK and so fails access() tests.
 
48
   Previous versions interpreted X_OK as F_OK anyway, so we'll just
 
49
   use F_OK directly. */
 
50
#undef X_OK
 
51
#define X_OK F_OK
 
52
#endif /* HAVE_W32_SYSTEM */
46
53
 
47
54
struct keyrec
48
55
{
49
56
  KEYDB_SEARCH_DESC desc;
50
 
  time_t createtime,expiretime;
 
57
  u32 createtime,expiretime;
51
58
  int size,flags;
52
59
  byte type;
53
 
  iobuf_t uidbuf;
54
 
  int lines;
55
 
};
56
 
 
57
 
struct kopts
58
 
{
59
 
  char *name;
60
 
  int tell; /* tell remote process about this one */
61
 
  int *flag;
62
 
} keyserver_opts[]=
63
 
{
64
 
  {"include-revoked",1,&opt.keyserver_options.include_revoked},
65
 
  {"include-disabled",1,&opt.keyserver_options.include_disabled},
66
 
  {"include-subkeys",1,&opt.keyserver_options.include_subkeys},
67
 
  {"keep-temp-files",0,&opt.keyserver_options.keep_temp_files},
68
 
  {"honor-http-proxy",1,&opt.keyserver_options.honor_http_proxy},
69
 
  {"broken-http-proxy",1,&opt.keyserver_options.broken_http_proxy},
70
 
  {"refresh-add-fake-v3-keyids",0,&opt.keyserver_options.fake_v3_keyids},
71
 
  {"auto-key-retrieve",0,&opt.keyserver_options.auto_key_retrieve},
72
 
  {"try-dns-srv",1,&opt.keyserver_options.try_dns_srv},
73
 
  {NULL}
74
 
};
75
 
 
76
 
static int keyserver_work(int action,STRLIST list,
77
 
                          KEYDB_SEARCH_DESC *desc,int count);
78
 
 
79
 
void 
 
60
  IOBUF uidbuf;
 
61
  unsigned int lines;
 
62
};
 
63
 
 
64
enum ks_action {KS_UNKNOWN=0,KS_GET,KS_GETNAME,KS_SEND,KS_SEARCH};
 
65
 
 
66
static struct parse_options keyserver_opts[]=
 
67
  {
 
68
    /* some of these options are not real - just for the help
 
69
       message */
 
70
    {"max-cert-size",0,NULL,NULL},
 
71
    {"include-revoked",0,NULL,N_("include revoked keys in search results")},
 
72
    {"include-subkeys",0,NULL,N_("include subkeys when searching by key ID")},
 
73
    {"use-temp-files",0,NULL,
 
74
     N_("use temporary files to pass data to keyserver helpers")},
 
75
    {"keep-temp-files",KEYSERVER_KEEP_TEMP_FILES,NULL,
 
76
     N_("do not delete temporary files after using them")},
 
77
    {"refresh-add-fake-v3-keyids",KEYSERVER_ADD_FAKE_V3,NULL,
 
78
     NULL},
 
79
    {"auto-key-retrieve",KEYSERVER_AUTO_KEY_RETRIEVE,NULL,
 
80
     N_("automatically retrieve keys when verifying signatures")},
 
81
    {"honor-keyserver-url",KEYSERVER_HONOR_KEYSERVER_URL,NULL,
 
82
     N_("honor the preferred keyserver URL set on the key")},
 
83
    {"honor-pka-record",KEYSERVER_HONOR_PKA_RECORD,NULL,
 
84
     N_("honor the PKA record set on a key when retrieving keys")},
 
85
    {NULL,0,NULL,NULL}
 
86
  };
 
87
 
 
88
static int keyserver_work(enum ks_action action,strlist_t list,
 
89
                          KEYDB_SEARCH_DESC *desc,int count,
 
90
                          unsigned char **fpr,size_t *fpr_len,
 
91
                          struct keyserver_spec *keyserver);
 
92
 
 
93
/* Reasonable guess */
 
94
#define DEFAULT_MAX_CERT_SIZE 16384
 
95
 
 
96
static size_t max_cert_size=DEFAULT_MAX_CERT_SIZE;
 
97
 
 
98
static void
 
99
add_canonical_option(char *option,strlist_t *list)
 
100
{
 
101
  char *arg=argsplit(option);
 
102
 
 
103
  if(arg)
 
104
    {
 
105
      char *joined;
 
106
 
 
107
      joined=xmalloc(strlen(option)+1+strlen(arg)+1);
 
108
      /* Make a canonical name=value form with no spaces */
 
109
      strcpy(joined,option);
 
110
      strcat(joined,"=");
 
111
      strcat(joined,arg);
 
112
      append_to_strlist(list,joined);
 
113
      xfree(joined);
 
114
    }
 
115
  else
 
116
    append_to_strlist(list,option);
 
117
}
 
118
 
 
119
int
80
120
parse_keyserver_options(char *options)
81
121
{
 
122
  int ret=1;
82
123
  char *tok;
83
 
 
84
 
  while((tok=strsep(&options," ,")))
 
124
  char *max_cert=NULL;
 
125
 
 
126
  keyserver_opts[0].value=&max_cert;
 
127
 
 
128
  while((tok=optsep(&options)))
85
129
    {
86
 
      int i,hit=0;
87
 
 
88
130
      if(tok[0]=='\0')
89
131
        continue;
90
132
 
91
 
      for(i=0;keyserver_opts[i].name;i++)
92
 
        {
93
 
          if(ascii_strcasecmp(tok,keyserver_opts[i].name)==0)
94
 
            {
95
 
              *(keyserver_opts[i].flag)=1;
96
 
              hit=1;
97
 
              break;
98
 
            }
99
 
          else if(ascii_strncasecmp("no-",tok,3)==0 &&
100
 
                  ascii_strcasecmp(&tok[3],keyserver_opts[i].name)==0)
101
 
            {
102
 
              *(keyserver_opts[i].flag)=0;
103
 
              hit=1;
104
 
              break;
105
 
            }
106
 
        }
107
 
 
108
 
      /* These options need more than just a flag */
109
 
      if(!hit)
110
 
        {
111
 
          if(ascii_strcasecmp(tok,"verbose")==0)
112
 
            opt.keyserver_options.verbose++;
113
 
          else if(ascii_strcasecmp(tok,"no-verbose")==0)
114
 
            opt.keyserver_options.verbose--;
 
133
      /* For backwards compatibility.  1.2.x used honor-http-proxy and
 
134
         there are a good number of documents published that recommend
 
135
         it. */
 
136
      if(ascii_strcasecmp(tok,"honor-http-proxy")==0)
 
137
        tok="http-proxy";
 
138
      else if(ascii_strcasecmp(tok,"no-honor-http-proxy")==0)
 
139
        tok="no-http-proxy";
 
140
 
 
141
      /* We accept quite a few possible options here - some options to
 
142
         handle specially, the keyserver_options list, and import and
 
143
         export options that pertain to keyserver operations.  Note
 
144
         that you must use strncasecmp here as there might be an
 
145
         =argument attached which will foil the use of strcasecmp. */
 
146
 
115
147
#ifdef EXEC_TEMPFILE_ONLY
116
 
          else if(ascii_strcasecmp(tok,"use-temp-files")==0 ||
117
 
                  ascii_strcasecmp(tok,"no-use-temp-files")==0)
118
 
            log_info(_("WARNING: keyserver option \"%s\" is not used "
119
 
                       "on this platform\n"),tok);
 
148
      if(ascii_strncasecmp(tok,"use-temp-files",14)==0 ||
 
149
              ascii_strncasecmp(tok,"no-use-temp-files",17)==0)
 
150
        log_info(_("WARNING: keyserver option `%s' is not used"
 
151
                   " on this platform\n"),tok);
120
152
#else
121
 
          else if(ascii_strcasecmp(tok,"use-temp-files")==0)
122
 
            opt.keyserver_options.use_temp_files=1;
123
 
          else if(ascii_strcasecmp(tok,"no-use-temp-files")==0)
124
 
            opt.keyserver_options.use_temp_files=0;
 
153
      if(ascii_strncasecmp(tok,"use-temp-files",14)==0)
 
154
        opt.keyserver_options.options|=KEYSERVER_USE_TEMP_FILES;
 
155
      else if(ascii_strncasecmp(tok,"no-use-temp-files",17)==0)
 
156
        opt.keyserver_options.options&=~KEYSERVER_USE_TEMP_FILES;
125
157
#endif
126
 
          else
127
 
            if(!parse_import_options(tok,
128
 
                                     &opt.keyserver_options.import_options) &&
129
 
               !parse_export_options(tok,
130
 
                                     &opt.keyserver_options.export_options))
131
 
              add_to_strlist(&opt.keyserver_options.other,tok);
132
 
        }
133
 
    }
134
 
}
135
 
 
136
 
int 
137
 
parse_keyserver_uri(char *uri,const char *configname,unsigned int configlineno)
 
158
      else if(!parse_options(tok,&opt.keyserver_options.options,
 
159
                             keyserver_opts,0)
 
160
         && !parse_import_options(tok,
 
161
                                  &opt.keyserver_options.import_options,0)
 
162
         && !parse_export_options(tok,
 
163
                                  &opt.keyserver_options.export_options,0))
 
164
        {
 
165
          /* All of the standard options have failed, so the option is
 
166
             destined for a keyserver plugin. */
 
167
          add_canonical_option(tok,&opt.keyserver_options.other);
 
168
        }
 
169
    }
 
170
 
 
171
  if(max_cert)
 
172
    {
 
173
      max_cert_size=strtoul(max_cert,(char **)NULL,10);
 
174
 
 
175
      if(max_cert_size==0)
 
176
        max_cert_size=DEFAULT_MAX_CERT_SIZE;
 
177
    }
 
178
 
 
179
  return ret;
 
180
}
 
181
 
 
182
void
 
183
free_keyserver_spec(struct keyserver_spec *keyserver)
 
184
{
 
185
  xfree(keyserver->uri);
 
186
  xfree(keyserver->scheme);
 
187
  xfree(keyserver->auth);
 
188
  xfree(keyserver->host);
 
189
  xfree(keyserver->port);
 
190
  xfree(keyserver->path);
 
191
  xfree(keyserver->opaque);
 
192
  free_strlist(keyserver->options);
 
193
  xfree(keyserver);
 
194
}
 
195
 
 
196
/* Return 0 for match */
 
197
static int
 
198
cmp_keyserver_spec(struct keyserver_spec *one,struct keyserver_spec *two)
 
199
{
 
200
  if(ascii_strcasecmp(one->scheme,two->scheme)==0)
 
201
    {
 
202
      if(one->host && two->host && ascii_strcasecmp(one->host,two->host)==0)
 
203
        {
 
204
          if((one->port && two->port
 
205
              && ascii_strcasecmp(one->port,two->port)==0)
 
206
             || (!one->port && !two->port))
 
207
            return 0;
 
208
        }
 
209
      else if(one->opaque && two->opaque
 
210
              && ascii_strcasecmp(one->opaque,two->opaque)==0)
 
211
        return 0;
 
212
    }
 
213
 
 
214
  return 1;
 
215
}
 
216
 
 
217
/* Try and match one of our keyservers.  If we can, return that.  If
 
218
   we can't, return our input. */
 
219
struct keyserver_spec *
 
220
keyserver_match(struct keyserver_spec *spec)
 
221
{
 
222
  struct keyserver_spec *ks;
 
223
 
 
224
  for(ks=opt.keyserver;ks;ks=ks->next)
 
225
    if(cmp_keyserver_spec(spec,ks)==0)
 
226
      return ks;
 
227
 
 
228
  return spec;
 
229
}
 
230
 
 
231
/* TODO: once we cut over to an all-curl world, we don't need this
 
232
   parser any longer so it can be removed, or at least moved to
 
233
   keyserver/ksutil.c for limited use in gpgkeys_ldap or the like. */
 
234
 
 
235
struct keyserver_spec *
 
236
parse_keyserver_uri(const char *string,int require_scheme,
 
237
                    const char *configname,unsigned int configlineno)
138
238
{
139
239
  int assume_hkp=0;
140
 
 
141
 
  assert(uri!=NULL);
142
 
 
143
 
  opt.keyserver_host=NULL;
144
 
  opt.keyserver_port=NULL;
145
 
  opt.keyserver_opaque=NULL;
 
240
  struct keyserver_spec *keyserver;
 
241
  const char *idx;
 
242
  int count;
 
243
  char *uri,*options;
 
244
 
 
245
  assert(string!=NULL);
 
246
 
 
247
  keyserver=xmalloc_clear(sizeof(struct keyserver_spec));
 
248
 
 
249
  uri=xstrdup(string);
 
250
 
 
251
  options=strchr(uri,' ');
 
252
  if(options)
 
253
    {
 
254
      char *tok;
 
255
 
 
256
      *options='\0';
 
257
      options++;
 
258
 
 
259
      while((tok=optsep(&options)))
 
260
        add_canonical_option(tok,&keyserver->options);
 
261
    }
146
262
 
147
263
  /* Get the scheme */
148
264
 
149
 
  opt.keyserver_scheme=strsep(&uri,":");
150
 
  if(uri==NULL)
151
 
    {
 
265
  for(idx=uri,count=0;*idx && *idx!=':';idx++)
 
266
    {
 
267
      count++;
 
268
 
 
269
      /* Do we see the start of an RFC-2732 ipv6 address here?  If so,
 
270
         there clearly isn't a scheme so get out early. */
 
271
      if(*idx=='[')
 
272
        {
 
273
          /* Was the '[' the first thing in the string?  If not, we
 
274
             have a mangled scheme with a [ in it so fail. */
 
275
          if(count==1)
 
276
            break;
 
277
          else
 
278
            goto fail;
 
279
        }
 
280
    }
 
281
 
 
282
  if(count==0)
 
283
    goto fail;
 
284
 
 
285
  if(*idx=='\0' || *idx=='[')
 
286
    {
 
287
      if(require_scheme)
 
288
        return NULL;
 
289
 
152
290
      /* Assume HKP if there is no scheme */
153
291
      assume_hkp=1;
154
 
      uri=opt.keyserver_scheme;
155
 
      opt.keyserver_scheme="hkp";
 
292
      keyserver->scheme=xstrdup("hkp");
 
293
 
 
294
      keyserver->uri=xmalloc(strlen(keyserver->scheme)+3+strlen(uri)+1);
 
295
      strcpy(keyserver->uri,keyserver->scheme);
 
296
      strcat(keyserver->uri,"://");
 
297
      strcat(keyserver->uri,uri);
156
298
    }
157
299
  else
158
300
    {
 
301
      int i;
 
302
 
 
303
      keyserver->uri=xstrdup(uri);
 
304
 
 
305
      keyserver->scheme=xmalloc(count+1);
 
306
 
159
307
      /* Force to lowercase */
160
 
      char *i;
161
 
 
162
 
      for(i=opt.keyserver_scheme;*i!='\0';i++)
163
 
        *i=ascii_tolower(*i);
 
308
      for(i=0;i<count;i++)
 
309
        keyserver->scheme[i]=ascii_tolower(uri[i]);
 
310
 
 
311
      keyserver->scheme[i]='\0';
 
312
 
 
313
      /* Skip past the scheme and colon */
 
314
      uri+=count+1;
164
315
    }
165
316
 
166
 
  if(ascii_strcasecmp(opt.keyserver_scheme,"x-broken-hkp")==0)
 
317
  if(ascii_strcasecmp(keyserver->scheme,"x-broken-hkp")==0)
167
318
    {
168
319
      deprecated_warning(configname,configlineno,"x-broken-hkp",
169
320
                         "--keyserver-options ","broken-http-proxy");
170
 
      opt.keyserver_scheme="hkp";
171
 
      opt.keyserver_options.broken_http_proxy=1;
 
321
      xfree(keyserver->scheme);
 
322
      keyserver->scheme=xstrdup("hkp");
 
323
      append_to_strlist(&opt.keyserver_options.other,"broken-http-proxy");
172
324
    }
173
 
  else if(ascii_strcasecmp(opt.keyserver_scheme,"x-hkp")==0
174
 
          || ascii_strcasecmp(opt.keyserver_scheme,"http")==0)
 
325
  else if(ascii_strcasecmp(keyserver->scheme,"x-hkp")==0)
175
326
    {
176
327
      /* Canonicalize this to "hkp" so it works with both the internal
177
328
         and external keyserver interface. */
178
 
      opt.keyserver_scheme="hkp";
 
329
      xfree(keyserver->scheme);
 
330
      keyserver->scheme=xstrdup("hkp");
179
331
    }
180
332
 
181
333
  if(assume_hkp || (uri[0]=='/' && uri[1]=='/'))
186
338
      if(!assume_hkp)
187
339
        uri+=2;
188
340
 
189
 
      /* Get the host */
190
 
      opt.keyserver_host=strsep(&uri,":/");
191
 
      if(opt.keyserver_host[0]=='\0')
192
 
        return GPG_ERR_BAD_URI;
193
 
 
194
 
      if(uri==NULL || uri[0]=='\0')
195
 
        opt.keyserver_port=NULL;
 
341
      /* Do we have userinfo auth data present? */
 
342
      for(idx=uri,count=0;*idx && *idx!='@' && *idx!='/';idx++)
 
343
        count++;
 
344
 
 
345
      /* We found a @ before the slash, so that means everything
 
346
         before the @ is auth data. */
 
347
      if(*idx=='@')
 
348
        {
 
349
          if(count==0)
 
350
            goto fail;
 
351
 
 
352
          keyserver->auth=xmalloc(count+1);
 
353
          strncpy(keyserver->auth,uri,count);
 
354
          keyserver->auth[count]='\0';
 
355
          uri+=count+1;
 
356
        }
 
357
 
 
358
      /* Is it an RFC-2732 ipv6 [literal address] ? */
 
359
      if(*uri=='[')
 
360
        {
 
361
          for(idx=uri+1,count=1;*idx
 
362
                && ((isascii (*idx) && isxdigit(*idx))
 
363
                    || *idx==':' || *idx=='.');idx++)
 
364
            count++;
 
365
 
 
366
          /* Is the ipv6 literal address terminated? */
 
367
          if(*idx==']')
 
368
            count++;
 
369
          else
 
370
            goto fail;
 
371
        }
196
372
      else
 
373
        for(idx=uri,count=0;*idx && *idx!=':' && *idx!='/';idx++)
 
374
          count++;
 
375
 
 
376
      if(count==0)
 
377
        goto fail;
 
378
 
 
379
      keyserver->host=xmalloc(count+1);
 
380
      strncpy(keyserver->host,uri,count);
 
381
      keyserver->host[count]='\0';
 
382
 
 
383
      /* Skip past the host */
 
384
      uri+=count;
 
385
 
 
386
      if(*uri==':')
197
387
        {
198
 
          char *ch;
199
 
 
200
 
          /* Get the port */
201
 
          opt.keyserver_port=strsep(&uri,"/");
202
 
 
203
 
          /* Ports are digits only */
204
 
          ch=opt.keyserver_port;
205
 
          while(*ch!='\0')
206
 
            {
207
 
              if(!digitp(ch))
208
 
                return GPG_ERR_BAD_URI;
209
 
 
210
 
              ch++;
211
 
            }
212
 
 
213
388
          /* It would seem to be reasonable to limit the range of the
214
389
             ports to values between 1-65535, but RFC 1738 and 1808
215
390
             imply there is no limit.  Of course, the real world has
216
391
             limits. */
 
392
 
 
393
          for(idx=uri+1,count=0;*idx && *idx!='/';idx++)
 
394
            {
 
395
              count++;
 
396
 
 
397
              /* Ports are digits only */
 
398
              if(!digitp(idx))
 
399
                goto fail;
 
400
            }
 
401
 
 
402
          keyserver->port=xmalloc(count+1);
 
403
          strncpy(keyserver->port,uri+1,count);
 
404
          keyserver->port[count]='\0';
 
405
 
 
406
          /* Skip past the colon and port number */
 
407
          uri+=1+count;
217
408
        }
218
409
 
219
 
      /* (any path part of the URI is discarded for now as no keyserver
220
 
         uses it yet) */
 
410
      /* Everything else is the path */
 
411
      if(*uri)
 
412
        keyserver->path=xstrdup(uri);
 
413
      else
 
414
        keyserver->path=xstrdup("/");
 
415
 
 
416
      if(keyserver->path[1])
 
417
        keyserver->flags.direct_uri=1;
221
418
    }
222
419
  else if(uri[0]!='/')
223
420
    {
224
421
      /* No slash means opaque.  Just record the opaque blob and get
225
422
         out. */
226
 
      opt.keyserver_opaque=uri;
227
 
      return 0;
 
423
      keyserver->opaque=xstrdup(uri);
228
424
    }
229
425
  else
230
426
    {
231
427
      /* One slash means absolute path.  We don't need to support that
232
428
         yet. */
233
 
      return GPG_ERR_BAD_URI;
234
 
    }
235
 
 
236
 
  if(opt.keyserver_scheme[0]=='\0')
237
 
    return GPG_ERR_BAD_URI;
238
 
 
239
 
  return 0;
 
429
      goto fail;
 
430
    }
 
431
 
 
432
  return keyserver;
 
433
 
 
434
 fail:
 
435
  free_keyserver_spec(keyserver);
 
436
 
 
437
  return NULL;
 
438
}
 
439
 
 
440
struct keyserver_spec *
 
441
parse_preferred_keyserver(PKT_signature *sig)
 
442
{
 
443
  struct keyserver_spec *spec=NULL;
 
444
  const byte *p;
 
445
  size_t plen;
 
446
 
 
447
  p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_PREF_KS,&plen);
 
448
  if(p && plen)
 
449
    {
 
450
      byte *dupe=xmalloc(plen+1);
 
451
 
 
452
      memcpy(dupe,p,plen);
 
453
      dupe[plen]='\0';
 
454
      spec=parse_keyserver_uri(dupe,1,NULL,0);
 
455
      xfree(dupe);
 
456
    }
 
457
 
 
458
  return spec;
240
459
}
241
460
 
242
461
static void
263
482
 
264
483
  switch(keyrec->desc.mode)
265
484
    {
 
485
      /* If the keyserver helper gave us a short keyid, we have no
 
486
         choice but to use it.  Do check --keyid-format to add a 0x if
 
487
         needed. */
266
488
    case KEYDB_SEARCH_MODE_SHORT_KID:
267
 
      printf("key %08lX",(ulong)keyrec->desc.u.kid[1]);
 
489
      printf("key %s%08lX",
 
490
             (opt.keyid_format==KF_0xSHORT
 
491
              || opt.keyid_format==KF_0xLONG)?"0x":"",
 
492
             (ulong)keyrec->desc.u.kid[1]);
268
493
      break;
269
494
 
 
495
      /* However, if it gave us a long keyid, we can honor
 
496
         --keyid-format */
270
497
    case KEYDB_SEARCH_MODE_LONG_KID:
271
 
      printf("key %08lX%08lX",(ulong)keyrec->desc.u.kid[0],
272
 
             (ulong)keyrec->desc.u.kid[1]);
 
498
      printf("key %s",keystr(keyrec->desc.u.kid));
273
499
      break;
274
500
 
275
501
    case KEYDB_SEARCH_MODE_FPR16:
276
502
      printf("key ");
277
503
      for(i=0;i<16;i++)
278
 
        printf("%02X",(unsigned char)keyrec->desc.u.fpr[i]);
 
504
        printf("%02X",keyrec->desc.u.fpr[i]);
279
505
      break;
280
506
 
281
507
    case KEYDB_SEARCH_MODE_FPR20:
282
508
      printf("key ");
283
509
      for(i=0;i<20;i++)
284
 
        printf("%02X",(unsigned char)keyrec->desc.u.fpr[i]);
 
510
        printf("%02X",keyrec->desc.u.fpr[i]);
285
511
      break;
286
512
 
287
513
    default:
290
516
    }
291
517
 
292
518
  if(keyrec->createtime>0)
293
 
    printf(", created %s",strtimestamp(keyrec->createtime));
 
519
    {
 
520
      printf(", ");
 
521
      printf(_("created: %s"),strtimestamp(keyrec->createtime));
 
522
    }
294
523
 
295
524
  if(keyrec->expiretime>0)
296
 
    printf(", expires %s",strtimestamp(keyrec->expiretime));
 
525
    {
 
526
      printf(", ");
 
527
      printf(_("expires: %s"),strtimestamp(keyrec->expiretime));
 
528
    }
297
529
 
298
530
  if(keyrec->flags&1)
299
 
    printf(" (%s)",("revoked"));
 
531
    printf(" (%s)",_("revoked"));
300
532
  if(keyrec->flags&2)
301
 
    printf(" (%s)",("disabled"));
 
533
    printf(" (%s)",_("disabled"));
302
534
  if(keyrec->flags&4)
303
 
    printf(" (%s)",("expired"));
 
535
    printf(" (%s)",_("expired"));
304
536
 
305
537
  printf("\n");
306
538
}
322
554
        return NULL;
323
555
      else if(work->desc.mode==KEYDB_SEARCH_MODE_NONE)
324
556
        {
325
 
          xfree (work);
 
557
          xfree(work);
326
558
          return NULL;
327
559
        }
328
560
      else
335
567
 
336
568
  if(work==NULL)
337
569
    {
338
 
      work=xcalloc (1,sizeof(struct keyrec));
 
570
      work=xmalloc_clear(sizeof(struct keyrec));
339
571
      work->uidbuf=iobuf_temp();
340
572
    }
341
573
 
356
588
      if(work->desc.mode)
357
589
        {
358
590
          ret=work;
359
 
          work=xcalloc (1,sizeof(struct keyrec));
 
591
          work=xmalloc_clear(sizeof(struct keyrec));
360
592
          work->uidbuf=iobuf_temp();
361
593
        }
362
594
 
391
623
      if((tok=strsep(&keystring,":"))==NULL)
392
624
        return ret;
393
625
 
394
 
      work->createtime=atoi(tok);
 
626
      if(atoi(tok)<=0)
 
627
        work->createtime=0;
 
628
      else
 
629
        work->createtime=atoi(tok);
395
630
 
396
631
      if((tok=strsep(&keystring,":"))==NULL)
397
632
        return ret;
398
633
 
399
 
      work->expiretime=atoi(tok);
 
634
      if(atoi(tok)<=0)
 
635
        work->expiretime=0;
 
636
      else
 
637
        {
 
638
          work->expiretime=atoi(tok);
 
639
          /* Force the 'e' flag on if this key is expired. */
 
640
          if(work->expiretime<=make_timestamp())
 
641
            work->flags|=4;
 
642
        }
400
643
 
401
644
      if((tok=strsep(&keystring,":"))==NULL)
402
645
        return ret;
419
662
            work->flags|=4;
420
663
            break;
421
664
          }
422
 
 
423
 
      if(work->expiretime && work->expiretime<=make_timestamp())
424
 
        work->flags|=4;
425
665
    }
426
666
  else if(ascii_strcasecmp("uid",record)==0 && work->desc.mode)
427
667
    {
443
683
      while(*tok)
444
684
        if(tok[0]=='%' && tok[1] && tok[2])
445
685
          {
446
 
            if((userid[i]=hextobyte(&tok[1]))==-1)
447
 
              userid[i]='?';
 
686
            int c;
448
687
 
 
688
            userid[i] = (c=hextobyte(&tok[1])) == -1 ? '?' : c;
449
689
            i++;
450
690
            tok+=3;
451
691
          }
460
700
         does this for us. */
461
701
 
462
702
      decoded=utf8_to_native(userid,i,0);
 
703
      if(strlen(decoded)>opt.screen_columns-10)
 
704
        decoded[opt.screen_columns-10]='\0';
463
705
      iobuf_writestr(work->uidbuf,decoded);
464
 
      xfree (decoded);
 
706
      xfree(decoded);
465
707
      iobuf_writestr(work->uidbuf,"\n\t");
466
708
      work->lines++;
467
709
    }
468
 
  
 
710
 
469
711
  /* Ignore any records other than "pri" and "uid" for easy future
470
712
     growth. */
471
713
 
499
741
 
500
742
  if(answer[0]=='q' || answer[0]=='Q')
501
743
    {
502
 
      xfree (answer);
 
744
      xfree(answer);
503
745
      return 1;
504
746
    }
505
747
  else if(atoi(answer)>=1 && atoi(answer)<=numdesc)
508
750
 
509
751
      while((num=strsep(&split," ,"))!=NULL)
510
752
        if(atoi(num)>=1 && atoi(num)<=numdesc)
511
 
          keyserver_work(GET,NULL,&desc[atoi(num)-1],1);
 
753
          keyserver_work(KS_GET,NULL,&desc[atoi(num)-1],1,
 
754
                         NULL,NULL,opt.keyserver);
512
755
 
513
 
      xfree (answer);
 
756
      xfree(answer);
514
757
      return 1;
515
758
    }
516
759
 
519
762
 
520
763
/* Count and searchstr are just for cosmetics.  If the count is too
521
764
   small, it will grow safely.  If negative it disables the "Key x-y
522
 
   of z" messages. */
 
765
   of z" messages.  searchstr should be UTF-8 (rather than native). */
523
766
static void
524
 
keyserver_search_prompt(iobuf_t buffer,const char *searchstr)
 
767
keyserver_search_prompt(IOBUF buffer,const char *searchstr)
525
768
{
526
769
  int i=0,validcount=0,started=0,header=0,count=1;
527
 
  unsigned int maxlen,buflen;
 
770
  unsigned int maxlen,buflen,numlines=0;
528
771
  KEYDB_SEARCH_DESC *desc;
529
772
  byte *line=NULL;
530
 
  /* TODO: Something other than 23?  That's 24-1 (the prompt). */
531
 
  int maxlines=23,numlines=0;
532
 
 
533
 
  desc=xmalloc (count*sizeof(KEYDB_SEARCH_DESC));
 
773
  char *localstr=NULL;
 
774
 
 
775
  if(searchstr)
 
776
    localstr=utf8_to_native(searchstr,strlen(searchstr),0);
 
777
 
 
778
  desc=xmalloc(count*sizeof(KEYDB_SEARCH_DESC));
534
779
 
535
780
  for(;;)
536
781
    {
609
854
 
610
855
              for(;;)
611
856
                {
612
 
                  if(show_prompt(desc,i,validcount?count:0,searchstr))
 
857
                  if(show_prompt(desc,i,validcount?count:0,localstr))
613
858
                    break;
614
859
                  validcount=0;
615
860
                }
635
880
 
636
881
          if(!opt.with_colons)
637
882
            {
638
 
              if(numlines+keyrec->lines>maxlines)
 
883
              /* screen_lines - 1 for the prompt. */
 
884
              if(numlines+keyrec->lines>opt.screen_lines-1)
639
885
                {
640
 
                  if(show_prompt(desc,i,validcount?count:0,searchstr))
 
886
                  if(show_prompt(desc,i,validcount?count:0,localstr))
641
887
                    break;
642
888
                  else
643
889
                    numlines=0;
648
894
 
649
895
          numlines+=keyrec->lines;
650
896
          iobuf_close(keyrec->uidbuf);
651
 
          xfree (keyrec);
 
897
          xfree(keyrec);
652
898
 
653
899
          started=1;
654
900
          i++;
655
901
        }
656
902
    }
657
903
 
658
 
  xfree (desc);
659
 
  xfree (line);
660
 
 
661
904
 notfound:
 
905
  /* Leave this commented out or now, and perhaps for a very long
 
906
     time.  All HKPish servers return HTML error messages for
 
907
     no-key-found. */
 
908
  /* 
 
909
     if(!started)
 
910
     log_info(_("keyserver does not support searching\n"));
 
911
     else
 
912
  */
662
913
  if(count==0)
663
914
    {
664
 
      if(searchstr)
665
 
        log_info(_("key \"%s\" not found on keyserver\n"),searchstr);
 
915
      if(localstr)
 
916
        log_info(_("key \"%s\" not found on keyserver\n"),localstr);
666
917
      else
667
918
        log_info(_("key not found on keyserver\n"));
668
 
      return;
669
919
    }
670
 
}
671
 
 
 
920
 
 
921
  xfree(localstr);
 
922
  xfree(desc);
 
923
  xfree(line);
 
924
}
 
925
 
 
926
/* We sometimes want to use a different gpgkeys_xxx for a given
 
927
   protocol (for example, ldaps is handled by gpgkeys_ldap).  Map
 
928
   these here. */
 
929
static const char *
 
930
keyserver_typemap(const char *type)
 
931
{
 
932
  if(strcmp(type,"ldaps")==0)
 
933
    return "ldap";
 
934
  else
 
935
    return type;
 
936
}
 
937
 
 
938
/* The PGP LDAP and the curl fetch-a-LDAP-object methodologies are
 
939
   sufficiently different that we can't use curl to do LDAP. */
 
940
static int
 
941
direct_uri_map(const char *scheme,unsigned int is_direct)
 
942
{
 
943
  if(is_direct && strcmp(scheme,"ldap")==0)
 
944
    return 1;
 
945
 
 
946
  return 0;
 
947
}
 
948
 
 
949
#if GNUPG_MAJOR_VERSION == 2
 
950
#define GPGKEYS_PREFIX "gpg2keys_"
 
951
#else
 
952
#define GPGKEYS_PREFIX "gpgkeys_"
 
953
#endif
 
954
#define GPGKEYS_CURL GPGKEYS_PREFIX "curl" EXEEXT
 
955
#define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_CURL))
672
956
#define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
673
957
#define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
674
958
 
675
959
static int 
676
 
keyserver_spawn(int action,STRLIST list,
677
 
                KEYDB_SEARCH_DESC *desc,int count,int *prog)
 
960
keyserver_spawn(enum ks_action action,strlist_t list,KEYDB_SEARCH_DESC *desc,
 
961
                int count,int *prog,unsigned char **fpr,size_t *fpr_len,
 
962
                struct keyserver_spec *keyserver)
678
963
{
679
964
  int ret=0,i,gotversion=0,outofband=0;
680
 
  STRLIST temp;
 
965
  strlist_t temp;
681
966
  unsigned int maxlen,buflen;
682
 
  char *command=NULL,*searchstr=NULL;
 
967
  char *command,*end,*searchstr=NULL;
683
968
  byte *line=NULL;
684
 
  struct kopts *kopts;
685
969
  struct exec_info *spawn;
 
970
  const char *scheme;
 
971
  const char *libexecdir = gnupg_libexecdir ();
 
972
 
 
973
  assert(keyserver);
686
974
 
687
975
#ifdef EXEC_TEMPFILE_ONLY
688
 
  opt.keyserver_options.use_temp_files=1;
 
976
  opt.keyserver_options.options|=KEYSERVER_USE_TEMP_FILES;
689
977
#endif
690
978
 
691
 
  /* Push the libexecdir into path.  If DISABLE_KEYSERVER_PATH is set,
692
 
     use the 0 arg to replace the path. */
 
979
  /* Build the filename for the helper to execute */
 
980
  scheme=keyserver_typemap(keyserver->scheme);
 
981
 
693
982
#ifdef DISABLE_KEYSERVER_PATH
694
 
  set_exec_path(GNUPG_LIBEXECDIR,0);
 
983
  /* Destroy any path we might have.  This is a little tricky,
 
984
     portability-wise.  It's not correct to delete the PATH
 
985
     environment variable, as that may fall back to a system built-in
 
986
     PATH.  Similarly, it is not correct to set PATH to the null
 
987
     string (PATH="") since this actually deletes the PATH environment
 
988
     variable under MinGW.  The safest thing to do here is to force
 
989
     PATH to be GNUPG_LIBEXECDIR.  All this is not that meaningful on
 
990
     Unix-like systems (since we're going to give a full path to
 
991
     gpgkeys_foo), but on W32 it prevents loading any DLLs from
 
992
     directories in %PATH%.
 
993
 
 
994
     After some more thinking about this we came to the conclusion
 
995
     that it is better to load the helpers from the directory where
 
996
     the program of this process lives.  Fortunately Windows provides
 
997
     a way to retrieve this and our gnupg_libexecdir function has been
 
998
     modified to return just this.  Setting the exec-path is not
 
999
     anymore required.  
 
1000
       set_exec_path(libexecdir);
 
1001
 */
695
1002
#else
696
 
  set_exec_path(GNUPG_LIBEXECDIR,opt.exec_path_set);
 
1003
  if(opt.exec_path_set)
 
1004
    {
 
1005
      /* If exec-path was set, and DISABLE_KEYSERVER_PATH is
 
1006
         undefined, then don't specify a full path to gpgkeys_foo, so
 
1007
         that the PATH can work. */
 
1008
      command=xmalloc(GPGKEYS_PREFIX_LEN+strlen(scheme)+3+strlen(EXEEXT)+1);
 
1009
      command[0]='\0';
 
1010
    }
 
1011
  else
697
1012
#endif
698
 
 
699
 
  /* Build the filename for the helper to execute */
700
 
  command=xmalloc (strlen("gpgkeys_")+strlen(opt.keyserver_scheme)+1);
701
 
  strcpy(command,"gpgkeys_");
702
 
  strcat(command,opt.keyserver_scheme);
703
 
 
704
 
  if(opt.keyserver_options.use_temp_files)
705
 
    {
706
 
      if(opt.keyserver_options.keep_temp_files)
 
1013
    {
 
1014
      /* Specify a full path to gpgkeys_foo. */
 
1015
      command=xmalloc(strlen(libexecdir)+strlen(DIRSEP_S)+
 
1016
                      GPGKEYS_PREFIX_LEN+strlen(scheme)+3+strlen(EXEEXT)+1);
 
1017
      strcpy(command,libexecdir);
 
1018
      strcat(command,DIRSEP_S);
 
1019
    }
 
1020
 
 
1021
  end=command+strlen(command);
 
1022
 
 
1023
  /* Build a path for the keyserver helper.  If it is direct_uri
 
1024
     (i.e. an object fetch and not a keyserver), then add "_uri" to
 
1025
     the end to distinguish the keyserver helper from an object
 
1026
     fetcher that can speak that protocol (this is a problem for
 
1027
     LDAP). */
 
1028
 
 
1029
  strcat(command,GPGKEYS_PREFIX); 
 
1030
  strcat(command,scheme);
 
1031
 
 
1032
  /* This "_uri" thing is in case we need to call a direct handler
 
1033
     instead of the keyserver handler.  This lets us use gpgkeys_curl
 
1034
     or gpgkeys_ldap_uri (we don't provide it, but a user might)
 
1035
     instead of gpgkeys_ldap to fetch things like
 
1036
     ldap://keyserver.pgp.com/o=PGP%20keys?pgpkey?sub?pgpkeyid=99242560 */
 
1037
 
 
1038
  if(direct_uri_map(scheme,keyserver->flags.direct_uri))
 
1039
    strcat(command,"_uri");
 
1040
 
 
1041
  strcat(command,EXEEXT);
 
1042
 
 
1043
  /* Can we execute it?  If not, try curl as our catchall. */
 
1044
  if(path_access(command,X_OK)!=0)
 
1045
    strcpy(end,GPGKEYS_CURL);
 
1046
 
 
1047
  if(opt.keyserver_options.options&KEYSERVER_USE_TEMP_FILES)
 
1048
    {
 
1049
      if(opt.keyserver_options.options&KEYSERVER_KEEP_TEMP_FILES)
707
1050
        {
708
1051
          command=xrealloc(command,strlen(command)+
709
1052
                            strlen(KEYSERVER_ARGS_KEEP)+1);
721
1064
  else
722
1065
    ret=exec_write(&spawn,command,NULL,NULL,0,0);
723
1066
 
 
1067
  xfree(command);
 
1068
 
724
1069
  if(ret)
725
1070
    return ret;
726
1071
 
727
 
  fprintf(spawn->tochild,"# This is a gpg keyserver communications file\n");
 
1072
  fprintf(spawn->tochild,
 
1073
          "# This is a GnuPG %s keyserver communications file\n",VERSION);
728
1074
  fprintf(spawn->tochild,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
729
1075
  fprintf(spawn->tochild,"PROGRAM %s\n",VERSION);
 
1076
  fprintf(spawn->tochild,"SCHEME %s\n",keyserver->scheme);
730
1077
 
731
 
  if(opt.keyserver_opaque)
732
 
    fprintf(spawn->tochild,"OPAQUE %s\n",opt.keyserver_opaque);
 
1078
  if(keyserver->opaque)
 
1079
    fprintf(spawn->tochild,"OPAQUE %s\n",keyserver->opaque);
733
1080
  else
734
1081
    {
735
 
      if(opt.keyserver_host)
736
 
        fprintf(spawn->tochild,"HOST %s\n",opt.keyserver_host);
737
 
 
738
 
      if(opt.keyserver_port)
739
 
        fprintf(spawn->tochild,"PORT %s\n",opt.keyserver_port);
 
1082
      if(keyserver->auth)
 
1083
        fprintf(spawn->tochild,"AUTH %s\n",keyserver->auth);
 
1084
 
 
1085
      if(keyserver->host)
 
1086
        fprintf(spawn->tochild,"HOST %s\n",keyserver->host);
 
1087
 
 
1088
      if(keyserver->port)
 
1089
        fprintf(spawn->tochild,"PORT %s\n",keyserver->port);
 
1090
 
 
1091
      if(keyserver->path)
 
1092
        fprintf(spawn->tochild,"PATH %s\n",keyserver->path);
740
1093
    }
741
1094
 
742
 
  /* Write options */
743
 
 
744
 
  for(i=0,kopts=keyserver_opts;kopts[i].name;i++)
745
 
    if(*(kopts[i].flag) && kopts[i].tell)
746
 
      fprintf(spawn->tochild,"OPTION %s\n",kopts[i].name);
747
 
 
748
 
  for(i=0;i<opt.keyserver_options.verbose;i++)
749
 
    fprintf(spawn->tochild,"OPTION verbose\n");
750
 
 
751
 
  temp=opt.keyserver_options.other;
752
 
 
753
 
  for(;temp;temp=temp->next)
 
1095
  /* Write global options */
 
1096
 
 
1097
  for(temp=opt.keyserver_options.other;temp;temp=temp->next)
 
1098
    fprintf(spawn->tochild,"OPTION %s\n",temp->d);
 
1099
 
 
1100
  /* Write per-keyserver options */
 
1101
 
 
1102
  for(temp=keyserver->options;temp;temp=temp->next)
754
1103
    fprintf(spawn->tochild,"OPTION %s\n",temp->d);
755
1104
 
756
1105
  switch(action)
757
1106
    {
758
 
    case GET:
 
1107
    case KS_GET:
759
1108
      {
760
1109
        fprintf(spawn->tochild,"COMMAND GET\n\n");
761
1110
 
763
1112
 
764
1113
        for(i=0;i<count;i++)
765
1114
          {
 
1115
            int quiet=0;
 
1116
 
766
1117
            if(desc[i].mode==KEYDB_SEARCH_MODE_FPR20)
767
1118
              {
768
1119
                int f;
770
1121
                fprintf(spawn->tochild,"0x");
771
1122
 
772
1123
                for(f=0;f<MAX_FINGERPRINT_LEN;f++)
773
 
                  fprintf(spawn->tochild,"%02X",(byte)desc[i].u.fpr[f]);
 
1124
                  fprintf(spawn->tochild,"%02X",desc[i].u.fpr[f]);
774
1125
 
775
1126
                fprintf(spawn->tochild,"\n");
776
1127
              }
781
1132
                fprintf(spawn->tochild,"0x");
782
1133
 
783
1134
                for(f=0;f<16;f++)
784
 
                  fprintf(spawn->tochild,"%02X",(byte)desc[i].u.fpr[f]);
 
1135
                  fprintf(spawn->tochild,"%02X",desc[i].u.fpr[f]);
785
1136
 
786
1137
                fprintf(spawn->tochild,"\n");
787
1138
              }
789
1140
              fprintf(spawn->tochild,"0x%08lX%08lX\n",
790
1141
                      (ulong)desc[i].u.kid[0],
791
1142
                      (ulong)desc[i].u.kid[1]);
792
 
            else
 
1143
            else if(desc[i].mode==KEYDB_SEARCH_MODE_SHORT_KID)
793
1144
              fprintf(spawn->tochild,"0x%08lX\n",
794
1145
                      (ulong)desc[i].u.kid[1]);
 
1146
            else if(desc[i].mode==KEYDB_SEARCH_MODE_EXACT)
 
1147
              {
 
1148
                fprintf(spawn->tochild,"0x0000000000000000\n");
 
1149
                quiet=1;
 
1150
              }
 
1151
            else if(desc[i].mode==KEYDB_SEARCH_MODE_NONE)
 
1152
              continue;
 
1153
            else
 
1154
              BUG();
 
1155
 
 
1156
            if(!quiet)
 
1157
              {
 
1158
                if(keyserver->host)
 
1159
                  log_info(_("requesting key %s from %s server %s\n"),
 
1160
                           keystr_from_desc(&desc[i]),
 
1161
                           keyserver->scheme,keyserver->host);
 
1162
                else
 
1163
                  log_info(_("requesting key %s from %s\n"),
 
1164
                           keystr_from_desc(&desc[i]),keyserver->uri);
 
1165
              }
795
1166
          }
796
1167
 
797
1168
        fprintf(spawn->tochild,"\n");
799
1170
        break;
800
1171
      }
801
1172
 
802
 
    case SEND:
803
 
      {
804
 
        STRLIST key;
 
1173
    case KS_GETNAME:
 
1174
      {
 
1175
        strlist_t key;
 
1176
 
 
1177
        fprintf(spawn->tochild,"COMMAND GETNAME\n\n");
 
1178
 
 
1179
        /* Which names do we want? */
 
1180
 
 
1181
        for(key=list;key!=NULL;key=key->next)
 
1182
          fprintf(spawn->tochild,"%s\n",key->d);
 
1183
 
 
1184
        fprintf(spawn->tochild,"\n");
 
1185
 
 
1186
        if(keyserver->host)
 
1187
          log_info(_("searching for names from %s server %s\n"),
 
1188
                   keyserver->scheme,keyserver->host);
 
1189
        else
 
1190
          log_info(_("searching for names from %s\n"),keyserver->uri);
 
1191
 
 
1192
        break;
 
1193
      }
 
1194
 
 
1195
    case KS_SEND:
 
1196
      {
 
1197
        strlist_t key;
805
1198
 
806
1199
        /* Note the extra \n here to send an empty keylist block */
807
1200
        fprintf(spawn->tochild,"COMMAND SEND\n\n\n");
808
1201
 
809
1202
        for(key=list;key!=NULL;key=key->next)
810
1203
          {
811
 
            armor_filter_context_t afx;
812
 
            iobuf_t buffer=iobuf_temp();
 
1204
            armor_filter_context_t *afx;
 
1205
            IOBUF buffer = iobuf_temp ();
813
1206
            KBNODE block;
814
1207
 
815
1208
            temp=NULL;
816
1209
            add_to_strlist(&temp,key->d);
817
1210
 
818
 
            memset(&afx,0,sizeof(afx));
819
 
            afx.what=1;
820
 
            iobuf_push_filter(buffer,armor_filter,&afx);
 
1211
            afx = new_armor_context ();
 
1212
            afx->what = 1;
 
1213
            /* Tell the armor filter to use Unix-style \n line
 
1214
               endings, since we're going to fprintf this to a file
 
1215
               that (on Win32) is open in text mode.  The win32 stdio
 
1216
               will transform the \n to \r\n and we'll end up with the
 
1217
               proper line endings on win32.  This is a no-op on
 
1218
               Unix. */
 
1219
            afx->eol[0] = '\n';
 
1220
            push_armor_filter (afx, buffer);
 
1221
            release_armor_context (afx);
821
1222
 
822
 
            /* TODO: Don't use the keyblock hack here - instead,
823
 
               output each key as a different ascii armored blob with
824
 
               its own INFO section. */
 
1223
            /* TODO: Remove Comment: lines from keys exported this
 
1224
               way? */
825
1225
 
826
1226
            if(export_pubkeys_stream(buffer,temp,&block,
827
1227
                                     opt.keyserver_options.export_options)==-1)
834
1234
 
835
1235
                merge_keys_and_selfsig(block);
836
1236
 
837
 
                fprintf(spawn->tochild,"INFO %s BEGIN\n",key->d);
 
1237
                fprintf(spawn->tochild,"INFO %08lX%08lX BEGIN\n",
 
1238
                        (ulong)block->pkt->pkt.public_key->keyid[0],
 
1239
                        (ulong)block->pkt->pkt.public_key->keyid[1]);
838
1240
 
839
1241
                for(node=block;node;node=node->next)
840
1242
                  {
864
1266
                            fprintf(spawn->tochild,"e");
865
1267
 
866
1268
                          fprintf(spawn->tochild,"\n");
867
 
 
868
 
                          break;
869
1269
                        }
 
1270
                        break;
870
1271
 
871
1272
                      case PKT_USER_ID:
872
1273
                        {
884
1285
                            {
885
1286
                              if(uid->name[r]==':' || uid->name[r]=='%'
886
1287
                                 || uid->name[r]&0x80)
887
 
                                fprintf(spawn->tochild,"%%%02X",uid->name[r]);
 
1288
                                fprintf(spawn->tochild,"%%%02X",
 
1289
                                        (byte)uid->name[r]);
888
1290
                              else
889
1291
                                fprintf(spawn->tochild,"%c",uid->name[r]);
890
1292
                            }
899
1301
 
900
1302
                          fprintf(spawn->tochild,"\n");
901
1303
                        }
 
1304
                        break;
 
1305
 
 
1306
                        /* This bit is really for the benefit of
 
1307
                           people who store their keys in LDAP
 
1308
                           servers.  It makes it easy to do queries
 
1309
                           for things like "all keys signed by
 
1310
                           Isabella". */
 
1311
                      case PKT_SIGNATURE:
 
1312
                        {
 
1313
                          PKT_signature *sig=node->pkt->pkt.signature;
 
1314
 
 
1315
                          if(!IS_UID_SIG(sig))
 
1316
                            continue;
 
1317
 
 
1318
                          fprintf(spawn->tochild,"sig:%08lX%08lX:%X:%u:%u\n",
 
1319
                                  (ulong)sig->keyid[0],(ulong)sig->keyid[1],
 
1320
                                  sig->sig_class,sig->timestamp,
 
1321
                                  sig->expiredate);
 
1322
                        }
 
1323
                        break;
902
1324
                      }
903
1325
                  }
904
1326
 
905
 
                fprintf(spawn->tochild,"INFO %s END\n",key->d);
 
1327
                fprintf(spawn->tochild,"INFO %08lX%08lX END\n",
 
1328
                        (ulong)block->pkt->pkt.public_key->keyid[0],
 
1329
                        (ulong)block->pkt->pkt.public_key->keyid[1]);
906
1330
 
907
 
                fprintf(spawn->tochild,"KEY %s BEGIN\n",key->d);
 
1331
                fprintf(spawn->tochild,"KEY %08lX%08lX BEGIN\n",
 
1332
                        (ulong)block->pkt->pkt.public_key->keyid[0],
 
1333
                        (ulong)block->pkt->pkt.public_key->keyid[1]);
908
1334
                fwrite(iobuf_get_temp_buffer(buffer),
909
1335
                       iobuf_get_temp_length(buffer),1,spawn->tochild);
910
 
                fprintf(spawn->tochild,"KEY %s END\n",key->d);
 
1336
                fprintf(spawn->tochild,"KEY %08lX%08lX END\n",
 
1337
                        (ulong)block->pkt->pkt.public_key->keyid[0],
 
1338
                        (ulong)block->pkt->pkt.public_key->keyid[1]);
911
1339
 
912
1340
                iobuf_close(buffer);
 
1341
 
 
1342
                if(keyserver->host)
 
1343
                  log_info(_("sending key %s to %s server %s\n"),
 
1344
                           keystr(block->pkt->pkt.public_key->keyid),
 
1345
                           keyserver->scheme,keyserver->host);
 
1346
                else
 
1347
                  log_info(_("sending key %s to %s\n"),
 
1348
                           keystr(block->pkt->pkt.public_key->keyid),
 
1349
                           keyserver->uri);
 
1350
 
913
1351
                release_kbnode(block);
914
1352
              }
915
1353
 
919
1357
        break;
920
1358
      }
921
1359
 
922
 
    case SEARCH:
 
1360
    case KS_SEARCH:
923
1361
      {
924
 
        STRLIST key;
 
1362
        strlist_t key;
925
1363
 
926
1364
        fprintf(spawn->tochild,"COMMAND SEARCH\n\n");
927
1365
 
939
1377
              }
940
1378
            else
941
1379
              {
942
 
                searchstr=xmalloc (strlen(key->d)+1);
 
1380
                searchstr=xmalloc(strlen(key->d)+1);
943
1381
                searchstr[0]='\0';
944
1382
              }
945
1383
 
948
1386
 
949
1387
        fprintf(spawn->tochild,"\n");
950
1388
 
 
1389
        if(keyserver->host)
 
1390
          log_info(_("searching for \"%s\" from %s server %s\n"),
 
1391
                   searchstr,keyserver->scheme,keyserver->host);
 
1392
        else
 
1393
          log_info(_("searching for \"%s\" from %s\n"),
 
1394
                   searchstr,keyserver->uri);
 
1395
 
951
1396
        break;
952
1397
      }
953
1398
 
971
1416
      maxlen=1024;
972
1417
      if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0)
973
1418
        {
974
 
          ret = iobuf_error (spawn->fromchild);
 
1419
          ret = gpg_error_from_syserror ();
975
1420
          goto fail; /* i.e. EOF */
976
1421
        }
977
1422
 
1000
1445
      else if(ascii_strncasecmp(ptr,"PROGRAM ",8)==0)
1001
1446
        {
1002
1447
          if(ascii_strncasecmp(&ptr[8],VERSION,strlen(VERSION))!=0)
1003
 
            log_info(_("WARNING: keyserver handler from a different "
1004
 
                       "version of GnuPG (%s)\n"),&ptr[8]);
 
1448
            log_info(_("WARNING: keyserver handler from a different"
 
1449
                       " version of GnuPG (%s)\n"),&ptr[8]);
1005
1450
        }
1006
1451
      else if(ascii_strncasecmp(ptr,"OPTION OUTOFBAND",16)==0)
1007
1452
        outofband=1; /* Currently the only OPTION */
1016
1461
  if(!outofband)
1017
1462
    switch(action)
1018
1463
      {
1019
 
      case GET:
 
1464
      case KS_GET:
 
1465
      case KS_GETNAME:
1020
1466
        {
1021
1467
          void *stats_handle;
1022
1468
 
1029
1475
             way to do this could be to continue parsing this
1030
1476
             line-by-line and make a temp iobuf for each key. */
1031
1477
 
1032
 
          import_keys_stream(spawn->fromchild,stats_handle,
 
1478
          import_keys_stream(spawn->fromchild,stats_handle,fpr,fpr_len,
1033
1479
                             opt.keyserver_options.import_options);
1034
1480
 
1035
1481
          import_print_stats(stats_handle);
1039
1485
        }
1040
1486
 
1041
1487
        /* Nothing to do here */
1042
 
      case SEND:
1043
 
        break;
1044
 
 
1045
 
      case SEARCH:
1046
 
        {
1047
 
          keyserver_search_prompt(spawn->fromchild,searchstr);
1048
 
 
1049
 
          break;
1050
 
        }
 
1488
      case KS_SEND:
 
1489
        break;
 
1490
 
 
1491
      case KS_SEARCH:
 
1492
        keyserver_search_prompt(spawn->fromchild,searchstr);
 
1493
        break;
1051
1494
 
1052
1495
      default:
1053
1496
        log_fatal(_("no keyserver action!\n"));
1055
1498
      }
1056
1499
 
1057
1500
 fail:
1058
 
  xfree (line);
 
1501
  xfree(line);
 
1502
  xfree(searchstr);
 
1503
 
1059
1504
 
1060
1505
  *prog=exec_finish(spawn);
1061
1506
 
1063
1508
}
1064
1509
 
1065
1510
static int 
1066
 
keyserver_work(int action,STRLIST list,KEYDB_SEARCH_DESC *desc,int count)
 
1511
keyserver_work(enum ks_action action,strlist_t list,KEYDB_SEARCH_DESC *desc,
 
1512
               int count,unsigned char **fpr,size_t *fpr_len,
 
1513
               struct keyserver_spec *keyserver)
1067
1514
{
1068
1515
  int rc=0,ret=0;
1069
1516
 
1070
 
  if(opt.keyserver_scheme==NULL)
 
1517
  if(!keyserver)
1071
1518
    {
1072
1519
      log_error(_("no keyserver known (use option --keyserver)\n"));
1073
 
      return GPG_ERR_BAD_URI;
 
1520
      return G10ERR_BAD_URI;
1074
1521
    }
1075
1522
 
1076
1523
#ifdef DISABLE_KEYSERVER_HELPERS
1077
1524
 
1078
1525
  log_error(_("external keyserver calls are not supported in this build\n"));
1079
 
  return GPG_ERR_KEYSERVER;
 
1526
  return G10ERR_KEYSERVER;
1080
1527
 
1081
1528
#else
1082
1529
  /* Spawn a handler */
1083
1530
 
1084
 
  rc=keyserver_spawn(action,list,desc,count,&ret);
 
1531
  rc=keyserver_spawn(action,list,desc,count,&ret,fpr,fpr_len,keyserver);
1085
1532
  if(ret)
1086
1533
    {
1087
1534
      switch(ret)
1088
1535
        {
1089
1536
        case KEYSERVER_SCHEME_NOT_FOUND:
1090
 
          log_error(_("no handler for keyserver scheme \"%s\"\n"),
1091
 
                    opt.keyserver_scheme);
 
1537
          log_error(_("no handler for keyserver scheme `%s'\n"),
 
1538
                    keyserver->scheme);
1092
1539
          break;
1093
1540
 
1094
1541
        case KEYSERVER_NOT_SUPPORTED:
1095
 
          log_error(_("action \"%s\" not supported with keyserver "
1096
 
                      "scheme \"%s\"\n"),
1097
 
                    action==GET?"get":action==SEND?"send":
1098
 
                    action==SEARCH?"search":"unknown",
1099
 
                    opt.keyserver_scheme);
 
1542
          log_error(_("action `%s' not supported with keyserver "
 
1543
                      "scheme `%s'\n"),
 
1544
                    action==KS_GET?"get":action==KS_SEND?"send":
 
1545
                    action==KS_SEARCH?"search":"unknown",
 
1546
                    keyserver->scheme);
1100
1547
          break;
1101
1548
 
1102
1549
        case KEYSERVER_VERSION_ERROR:
1103
 
          log_error(_("gpgkeys_%s does not support handler version %d\n"),
1104
 
                    opt.keyserver_scheme,KEYSERVER_PROTO_VERSION);
 
1550
          log_error(_(GPGKEYS_PREFIX "%s does not support"
 
1551
                      " handler version %d\n"),
 
1552
                    keyserver_typemap(keyserver->scheme),
 
1553
                    KEYSERVER_PROTO_VERSION);
 
1554
          break;
 
1555
 
 
1556
        case KEYSERVER_TIMEOUT:
 
1557
          log_error(_("keyserver timed out\n"));
1105
1558
          break;
1106
1559
 
1107
1560
        case KEYSERVER_INTERNAL_ERROR:
1110
1563
          break;
1111
1564
        }
1112
1565
 
1113
 
      return GPG_ERR_KEYSERVER;
 
1566
      return G10ERR_KEYSERVER;
1114
1567
    }
1115
1568
 
1116
1569
  if(rc)
1117
1570
    {
1118
 
      log_error(_("keyserver communications error: %s\n"),gpg_strerror (rc));
 
1571
      log_error(_("keyserver communications error: %s\n"),g10_errstr(rc));
1119
1572
 
1120
1573
      return rc;
1121
1574
    }
1125
1578
}
1126
1579
 
1127
1580
int 
1128
 
keyserver_export(STRLIST users)
 
1581
keyserver_export(strlist_t users)
1129
1582
{
1130
 
  /* We better ask for confirmation when the user entered --send-keys
1131
 
     without arguments.  Sending all keys might not be the thing he
1132
 
     intended to do */
1133
 
  if (users || opt.batch || opt.answer_yes)
1134
 
    ;
1135
 
  else if ( !cpr_get_answer_is_yes
1136
 
            ("keyserver_export.send_all",
1137
 
             _("Do you really want to send all your "
1138
 
               "public keys to the keyserver? (y/N) ")))
1139
 
    return -1;
1140
 
 
1141
 
  return keyserver_work(SEND,users,NULL,0);
 
1583
  strlist_t sl=NULL;
 
1584
  KEYDB_SEARCH_DESC desc;
 
1585
  int rc=0;
 
1586
 
 
1587
  /* Weed out descriptors that we don't support sending */
 
1588
  for(;users;users=users->next)
 
1589
    {
 
1590
      classify_user_id (users->d, &desc);
 
1591
      if(desc.mode!=KEYDB_SEARCH_MODE_SHORT_KID &&
 
1592
         desc.mode!=KEYDB_SEARCH_MODE_LONG_KID &&
 
1593
         desc.mode!=KEYDB_SEARCH_MODE_FPR16 &&
 
1594
         desc.mode!=KEYDB_SEARCH_MODE_FPR20)
 
1595
        {
 
1596
          log_error(_("\"%s\" not a key ID: skipping\n"),users->d);
 
1597
          continue;
 
1598
        }
 
1599
      else
 
1600
        append_to_strlist(&sl,users->d);
 
1601
    }
 
1602
 
 
1603
  if(sl)
 
1604
    {
 
1605
      rc=keyserver_work(KS_SEND,sl,NULL,0,NULL,NULL,opt.keyserver);
 
1606
      free_strlist(sl);
 
1607
    }
 
1608
 
 
1609
  return rc;
1142
1610
}
1143
1611
 
1144
1612
int 
1145
 
keyserver_import(STRLIST users)
 
1613
keyserver_import(strlist_t users)
1146
1614
{
1147
1615
  KEYDB_SEARCH_DESC *desc;
1148
1616
  int num=100,count=0;
1149
1617
  int rc=0;
1150
1618
 
1151
1619
  /* Build a list of key ids */
1152
 
  desc=xmalloc (sizeof(KEYDB_SEARCH_DESC)*num);
 
1620
  desc=xmalloc(sizeof(KEYDB_SEARCH_DESC)*num);
1153
1621
 
1154
1622
  for(;users;users=users->next)
1155
1623
    {
1159
1627
         desc[count].mode!=KEYDB_SEARCH_MODE_FPR16 &&
1160
1628
         desc[count].mode!=KEYDB_SEARCH_MODE_FPR20)
1161
1629
        {
1162
 
          log_error(_("skipping invalid key ID \"%s\"\n"),users->d);
 
1630
          log_error(_("\"%s\" not a key ID: skipping\n"),users->d);
1163
1631
          continue;
1164
1632
        }
1165
1633
 
1172
1640
    }
1173
1641
 
1174
1642
  if(count>0)
1175
 
    rc=keyserver_work(GET,NULL,desc,count);
 
1643
    rc=keyserver_work(KS_GET,NULL,desc,count,NULL,NULL,opt.keyserver);
1176
1644
 
1177
 
  xfree (desc);
 
1645
  xfree(desc);
1178
1646
 
1179
1647
  return rc;
1180
1648
}
1181
1649
 
1182
1650
int
1183
 
keyserver_import_fprint(const byte *fprint,size_t fprint_len)
 
1651
keyserver_import_fprint(const byte *fprint,size_t fprint_len,
 
1652
                        struct keyserver_spec *keyserver)
1184
1653
{
1185
1654
  KEYDB_SEARCH_DESC desc;
1186
1655
 
1195
1664
 
1196
1665
  memcpy(desc.u.fpr,fprint,fprint_len);
1197
1666
 
1198
 
  return keyserver_work(GET,NULL,&desc,1);
 
1667
  /* TODO: Warn here if the fingerprint we got doesn't match the one
 
1668
     we asked for? */
 
1669
  return keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,keyserver);
1199
1670
}
1200
1671
 
1201
1672
int 
1202
 
keyserver_import_keyid(u32 *keyid)
 
1673
keyserver_import_keyid(u32 *keyid,struct keyserver_spec *keyserver)
1203
1674
{
1204
1675
  KEYDB_SEARCH_DESC desc;
1205
1676
 
1209
1680
  desc.u.kid[0]=keyid[0];
1210
1681
  desc.u.kid[1]=keyid[1];
1211
1682
 
1212
 
  return keyserver_work(GET,NULL,&desc,1);
 
1683
  return keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,keyserver);
1213
1684
}
1214
1685
 
1215
1686
/* code mostly stolen from do_export_stream */
1216
1687
static int 
1217
 
keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
 
1688
keyidlist(strlist_t users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
1218
1689
{
1219
1690
  int rc=0,ndesc,num=100;
1220
1691
  KBNODE keyblock=NULL,node;
1221
1692
  KEYDB_HANDLE kdbhd;
1222
1693
  KEYDB_SEARCH_DESC *desc;
1223
 
  STRLIST sl;
 
1694
  strlist_t sl;
1224
1695
 
1225
1696
  *count=0;
1226
1697
 
1227
 
  *klist=xmalloc (sizeof(KEYDB_SEARCH_DESC)*num);
 
1698
  *klist=xmalloc(sizeof(KEYDB_SEARCH_DESC)*num);
1228
1699
 
1229
1700
  kdbhd=keydb_new(0);
1230
1701
 
1231
1702
  if(!users)
1232
1703
    {
1233
1704
      ndesc = 1;
1234
 
      desc = xcalloc (1, ndesc * sizeof *desc);
 
1705
      desc = xmalloc_clear ( ndesc * sizeof *desc);
1235
1706
      desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
1236
1707
    }
1237
1708
  else
1245
1716
          if(classify_user_id (sl->d, desc+ndesc))
1246
1717
            ndesc++;
1247
1718
          else
1248
 
            log_error (_("key `%s' not found: %s\n"),
1249
 
                       sl->d, gpg_strerror (GPG_ERR_INV_USER_ID));
 
1719
            log_error (_("key \"%s\" not found: %s\n"),
 
1720
                       sl->d, g10_errstr (G10ERR_INV_USER_ID));
1250
1721
        }
1251
1722
    }
1252
1723
 
1259
1730
      rc = keydb_get_keyblock (kdbhd, &keyblock );
1260
1731
      if( rc )
1261
1732
        {
1262
 
          log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
 
1733
          log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
1263
1734
          goto leave;
1264
1735
        }
1265
1736
 
1288
1759
            }
1289
1760
 
1290
1761
          /* v4 keys get full fingerprints.  v3 keys get long keyids.
1291
 
             This is because it's easy to calculate any sort of key id
 
1762
             This is because it's easy to calculate any sort of keyid
1292
1763
             from a v4 fingerprint, but not a v3 fingerprint. */
1293
1764
 
1294
1765
          if(node->pkt->pkt.public_key->version<4)
1306
1777
                                  (*klist)[*count].u.fpr,&dummy);
1307
1778
            }
1308
1779
 
 
1780
          /* This is a little hackish, using the skipfncvalue as a
 
1781
             void* pointer to the keyserver spec, but we don't need
 
1782
             the skipfnc here, and it saves having an additional field
 
1783
             for this (which would be wasted space most of the
 
1784
             time). */
 
1785
 
 
1786
          (*klist)[*count].skipfncvalue=NULL;
 
1787
 
 
1788
          /* Are we honoring preferred keyservers? */
 
1789
          if(opt.keyserver_options.options&KEYSERVER_HONOR_KEYSERVER_URL)
 
1790
            {
 
1791
              PKT_user_id *uid=NULL;
 
1792
              PKT_signature *sig=NULL;
 
1793
 
 
1794
              merge_keys_and_selfsig(keyblock);
 
1795
 
 
1796
              for(node=node->next;node;node=node->next)
 
1797
                {
 
1798
                  if(node->pkt->pkttype==PKT_USER_ID
 
1799
                     && node->pkt->pkt.user_id->is_primary)
 
1800
                    uid=node->pkt->pkt.user_id;
 
1801
                  else if(node->pkt->pkttype==PKT_SIGNATURE
 
1802
                          && node->pkt->pkt.signature->
 
1803
                          flags.chosen_selfsig && uid)
 
1804
                    {
 
1805
                      sig=node->pkt->pkt.signature;
 
1806
                      break;
 
1807
                    }
 
1808
                }
 
1809
 
 
1810
              /* Try and parse the keyserver URL.  If it doesn't work,
 
1811
                 then we end up writing NULL which indicates we are
 
1812
                 the same as any other key. */
 
1813
              if(sig)
 
1814
                (*klist)[*count].skipfncvalue=parse_preferred_keyserver(sig);
 
1815
            }
 
1816
 
1309
1817
          (*count)++;
1310
1818
 
1311
1819
          if(*count==num)
1320
1828
    rc=0;
1321
1829
  
1322
1830
 leave:
1323
 
  xfree (desc);
 
1831
  if(rc)
 
1832
    xfree(*klist);
 
1833
  xfree(desc);
1324
1834
  keydb_release(kdbhd);
1325
1835
  release_kbnode(keyblock);
1326
1836
 
1330
1840
/* Note this is different than the original HKP refresh.  It allows
1331
1841
   usernames to refresh only part of the keyring. */
1332
1842
 
1333
 
int 
1334
 
keyserver_refresh(STRLIST users)
 
1843
int
 
1844
keyserver_refresh(strlist_t users)
1335
1845
{
1336
 
  int rc,count,fakev3=0;
 
1846
  int rc,count,numdesc,fakev3=0;
1337
1847
  KEYDB_SEARCH_DESC *desc;
1338
 
 
1339
 
  /* We switch merge_only on during a refresh, as 'refresh' should
1340
 
     never import new keys, even if their keyids match.  Is it worth
1341
 
     preserving the old merge_only value here? */
1342
 
  opt.merge_only=1;
 
1848
  unsigned int options=opt.keyserver_options.import_options;
 
1849
 
 
1850
  /* We switch merge-only on during a refresh, as 'refresh' should
 
1851
     never import new keys, even if their keyids match. */
 
1852
  opt.keyserver_options.import_options|=IMPORT_MERGE_ONLY;
 
1853
 
 
1854
  /* Similarly, we switch on fast-import, since refresh may make
 
1855
     multiple import sets (due to preferred keyserver URLs).  We don't
 
1856
     want each set to rebuild the trustdb.  Instead we do it once at
 
1857
     the end here. */
 
1858
  opt.keyserver_options.import_options|=IMPORT_FAST;
1343
1859
 
1344
1860
  /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1345
1861
     scheme, then enable fake v3 keyid generation. */
1346
 
  if(opt.keyserver_options.fake_v3_keyids && opt.keyserver_scheme &&
1347
 
     (ascii_strcasecmp(opt.keyserver_scheme,"hkp")==0 ||
1348
 
      ascii_strcasecmp(opt.keyserver_scheme,"mailto")==0))
 
1862
  if((opt.keyserver_options.options&KEYSERVER_ADD_FAKE_V3) && opt.keyserver
 
1863
     && (ascii_strcasecmp(opt.keyserver->scheme,"hkp")==0 ||
 
1864
         ascii_strcasecmp(opt.keyserver->scheme,"mailto")==0))
1349
1865
    fakev3=1;
1350
1866
 
1351
 
  rc=keyidlist(users,&desc,&count,fakev3);
 
1867
  rc=keyidlist(users,&desc,&numdesc,fakev3);
1352
1868
  if(rc)
1353
1869
    return rc;
1354
1870
 
1355
 
  if(count>0)
1356
 
    {
1357
 
      if(opt.keyserver_uri)
 
1871
  count=numdesc;
 
1872
  if(count>0)
 
1873
    {
 
1874
      int i;
 
1875
 
 
1876
      /* Try to handle preferred keyserver keys first */
 
1877
      for(i=0;i<numdesc;i++)
 
1878
        {
 
1879
          if(desc[i].skipfncvalue)
 
1880
            {
 
1881
              struct keyserver_spec *keyserver=desc[i].skipfncvalue;
 
1882
 
 
1883
              /* We use the keyserver structure we parsed out before.
 
1884
                 Note that a preferred keyserver without a scheme://
 
1885
                 will be interpreted as hkp:// */
 
1886
 
 
1887
              rc=keyserver_work(KS_GET,NULL,&desc[i],1,NULL,NULL,keyserver);
 
1888
              if(rc)
 
1889
                log_info(_("WARNING: unable to refresh key %s"
 
1890
                           " via %s: %s\n"),keystr_from_desc(&desc[i]),
 
1891
                         keyserver->uri,g10_errstr(rc));
 
1892
              else
 
1893
                {
 
1894
                  /* We got it, so mark it as NONE so we don't try and
 
1895
                     get it again from the regular keyserver. */
 
1896
 
 
1897
                  desc[i].mode=KEYDB_SEARCH_MODE_NONE;
 
1898
                  count--;
 
1899
                }
 
1900
 
 
1901
              free_keyserver_spec(keyserver);
 
1902
            }
 
1903
        }
 
1904
    }
 
1905
 
 
1906
  if(count>0)
 
1907
    {
 
1908
      if(opt.keyserver)
1358
1909
        {
1359
1910
          if(count==1)
1360
 
            log_info(_("refreshing 1 key from %s\n"),opt.keyserver_uri);
 
1911
            log_info(_("refreshing 1 key from %s\n"),opt.keyserver->uri);
1361
1912
          else
1362
1913
            log_info(_("refreshing %d keys from %s\n"),
1363
 
                     count,opt.keyserver_uri);
 
1914
                     count,opt.keyserver->uri);
1364
1915
        }
1365
1916
 
1366
 
      rc=keyserver_work(GET,NULL,desc,count);
 
1917
      rc=keyserver_work(KS_GET,NULL,desc,numdesc,NULL,NULL,opt.keyserver);
1367
1918
    }
1368
1919
 
1369
 
  xfree (desc);
 
1920
  xfree(desc);
 
1921
 
 
1922
  opt.keyserver_options.import_options=options;
 
1923
 
 
1924
  /* If the original options didn't have fast import, and the trustdb
 
1925
     is dirty, rebuild. */
 
1926
  if(!(opt.keyserver_options.import_options&IMPORT_FAST))
 
1927
    trustdb_check_or_update();
1370
1928
 
1371
1929
  return rc;
1372
1930
}
1373
1931
 
1374
1932
int
1375
 
keyserver_search(STRLIST tokens)
 
1933
keyserver_search(strlist_t tokens)
1376
1934
{
1377
1935
  if(tokens)
1378
 
    return keyserver_work(SEARCH,tokens,NULL,0);
 
1936
    return keyserver_work(KS_SEARCH,tokens,NULL,0,NULL,NULL,opt.keyserver);
1379
1937
  else
1380
1938
    return 0;
1381
1939
}
 
1940
 
 
1941
int
 
1942
keyserver_fetch(strlist_t urilist)
 
1943
{
 
1944
  KEYDB_SEARCH_DESC desc;
 
1945
  strlist_t sl;
 
1946
  unsigned int options=opt.keyserver_options.import_options;
 
1947
 
 
1948
  /* Switch on fast-import, since fetch can handle more than one
 
1949
     import and we don't want each set to rebuild the trustdb.
 
1950
     Instead we do it once at the end. */
 
1951
  opt.keyserver_options.import_options|=IMPORT_FAST;
 
1952
 
 
1953
  /* A dummy desc since we're not actually fetching a particular key
 
1954
     ID */
 
1955
  memset(&desc,0,sizeof(desc));
 
1956
  desc.mode=KEYDB_SEARCH_MODE_EXACT;
 
1957
 
 
1958
  for(sl=urilist;sl;sl=sl->next)
 
1959
    {
 
1960
      struct keyserver_spec *spec;
 
1961
 
 
1962
      spec=parse_keyserver_uri(sl->d,1,NULL,0);
 
1963
      if(spec)
 
1964
        {
 
1965
          int rc;
 
1966
 
 
1967
          rc=keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,spec);
 
1968
          if(rc)
 
1969
            log_info (_("WARNING: unable to fetch URI %s: %s\n"),
 
1970
                     sl->d,g10_errstr(rc));
 
1971
 
 
1972
          free_keyserver_spec(spec);
 
1973
        }
 
1974
      else
 
1975
        log_info (_("WARNING: unable to parse URI %s\n"),sl->d);
 
1976
    }
 
1977
 
 
1978
  opt.keyserver_options.import_options=options;
 
1979
 
 
1980
  /* If the original options didn't have fast import, and the trustdb
 
1981
     is dirty, rebuild. */
 
1982
  if(!(opt.keyserver_options.import_options&IMPORT_FAST))
 
1983
    trustdb_check_or_update();
 
1984
 
 
1985
  return 0;
 
1986
}
 
1987
 
 
1988
/* Import key in a CERT or pointed to by a CERT */
 
1989
int
 
1990
keyserver_import_cert(const char *name,unsigned char **fpr,size_t *fpr_len)
 
1991
{
 
1992
  char *domain,*look,*url;
 
1993
  IOBUF key;
 
1994
  int type,rc=G10ERR_GENERAL;
 
1995
 
 
1996
  look=xstrdup(name);
 
1997
 
 
1998
  domain=strrchr(look,'@');
 
1999
  if(domain)
 
2000
    *domain='.';
 
2001
 
 
2002
  type=get_dns_cert(look,max_cert_size,&key,fpr,fpr_len,&url);
 
2003
  if(type==1)
 
2004
    {
 
2005
      int armor_status=opt.no_armor;
 
2006
 
 
2007
      /* CERTs are always in binary format */
 
2008
      opt.no_armor=1;
 
2009
 
 
2010
      rc=import_keys_stream(key,NULL,fpr,fpr_len,
 
2011
                            opt.keyserver_options.import_options);
 
2012
 
 
2013
      opt.no_armor=armor_status;
 
2014
 
 
2015
      iobuf_close(key);
 
2016
    }
 
2017
  else if(type==2 && *fpr)
 
2018
    {
 
2019
      /* We only consider the IPGP type if a fingerprint was provided.
 
2020
         This lets us select the right key regardless of what a URL
 
2021
         points to, or get the key from a keyserver. */
 
2022
      if(url)
 
2023
        {
 
2024
          struct keyserver_spec *spec;
 
2025
 
 
2026
          spec=parse_keyserver_uri(url,1,NULL,0);
 
2027
          if(spec)
 
2028
            {
 
2029
              strlist_t list=NULL;
 
2030
 
 
2031
              add_to_strlist(&list,url);
 
2032
 
 
2033
              rc=keyserver_fetch(list);
 
2034
 
 
2035
              free_strlist(list);
 
2036
              free_keyserver_spec(spec);
 
2037
            }
 
2038
        }
 
2039
      else if(opt.keyserver)
 
2040
        {
 
2041
          /* If only a fingerprint is provided, try and fetch it from
 
2042
             our --keyserver */
 
2043
 
 
2044
          rc=keyserver_import_fprint(*fpr,*fpr_len,opt.keyserver);
 
2045
        }
 
2046
      else
 
2047
        log_info(_("no keyserver known (use option --keyserver)\n"));
 
2048
 
 
2049
      /* Give a better string here? "CERT fingerprint for \"%s\"
 
2050
         found, but no keyserver" " known (use option
 
2051
         --keyserver)\n" ? */
 
2052
 
 
2053
      xfree(url);
 
2054
    }
 
2055
 
 
2056
  xfree(look);
 
2057
 
 
2058
  return rc;
 
2059
}
 
2060
 
 
2061
/* Import key pointed to by a PKA record. Return the requested
 
2062
   fingerprint in fpr. */
 
2063
int
 
2064
keyserver_import_pka(const char *name,unsigned char **fpr,size_t *fpr_len)
 
2065
{
 
2066
  char *uri;
 
2067
  int rc=-1;
 
2068
 
 
2069
  *fpr=xmalloc(20);
 
2070
  *fpr_len=20;
 
2071
 
 
2072
  uri = get_pka_info (name, *fpr);
 
2073
  if (uri)
 
2074
    {
 
2075
      struct keyserver_spec *spec;
 
2076
      spec = parse_keyserver_uri (uri, 1, NULL, 0);
 
2077
      if (spec)
 
2078
        {
 
2079
          rc=keyserver_import_fprint (*fpr, 20, spec);
 
2080
          free_keyserver_spec (spec);
 
2081
        }
 
2082
      xfree (uri);
 
2083
    }
 
2084
 
 
2085
  if(rc!=0)
 
2086
    xfree(*fpr);
 
2087
 
 
2088
  return rc;
 
2089
}
 
2090
 
 
2091
/* Import all keys that match name */
 
2092
int
 
2093
keyserver_import_name(const char *name,unsigned char **fpr,size_t *fpr_len,
 
2094
                      struct keyserver_spec *keyserver)
 
2095
{
 
2096
  strlist_t list=NULL;
 
2097
  int rc;
 
2098
 
 
2099
  append_to_strlist(&list,name);
 
2100
 
 
2101
  rc=keyserver_work(KS_GETNAME,list,NULL,0,fpr,fpr_len,keyserver);
 
2102
 
 
2103
  free_strlist(list);
 
2104
 
 
2105
  return rc;
 
2106
}
 
2107
 
 
2108
/* Use the PGP Universal trick of asking ldap://keys.(maildomain) for
 
2109
   the key. */
 
2110
int
 
2111
keyserver_import_ldap(const char *name,unsigned char **fpr,size_t *fpr_len)
 
2112
{
 
2113
  char *domain;
 
2114
  struct keyserver_spec *keyserver;
 
2115
  strlist_t list=NULL;
 
2116
  int rc;
 
2117
 
 
2118
  append_to_strlist(&list,name);
 
2119
 
 
2120
  /* Parse out the domain */
 
2121
  domain=strrchr(name,'@');
 
2122
  if(!domain)
 
2123
    return G10ERR_GENERAL;
 
2124
 
 
2125
  domain++;
 
2126
 
 
2127
  keyserver=xmalloc_clear(sizeof(struct keyserver_spec));
 
2128
 
 
2129
  keyserver->scheme=xstrdup("ldap");
 
2130
  keyserver->host=xmalloc(5+strlen(domain)+1);
 
2131
  strcpy(keyserver->host,"keys.");
 
2132
  strcat(keyserver->host,domain);
 
2133
  keyserver->uri=xmalloc(strlen(keyserver->scheme)+
 
2134
                         3+strlen(keyserver->host)+1);
 
2135
  strcpy(keyserver->uri,keyserver->scheme);
 
2136
  strcat(keyserver->uri,"://");
 
2137
  strcat(keyserver->uri,keyserver->host);
 
2138
    
 
2139
  rc=keyserver_work(KS_GETNAME,list,NULL,0,fpr,fpr_len,keyserver);
 
2140
 
 
2141
  free_strlist(list);
 
2142
 
 
2143
  free_keyserver_spec(keyserver);
 
2144
 
 
2145
  return rc;
 
2146
}