~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty

« back to all changes in this revision

Viewing changes to g10/trustdb.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
/* trustdb.c
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
 
3
 *                                             Free Software Foundation, Inc.
 
4
 *
 
5
 * This file is part of GnuPG.
 
6
 *
 
7
 * GnuPG is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * GnuPG is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <assert.h>
 
27
 
 
28
#ifndef DISABLE_REGEX
 
29
#include <sys/types.h>
 
30
#ifdef USE_GNU_REGEX
 
31
#include "_regex.h"
 
32
#else
 
33
#include <regex.h>
 
34
#endif
 
35
#endif /* !DISABLE_REGEX */
 
36
 
 
37
#include "gpg.h"
 
38
#include "errors.h"
 
39
#include "iobuf.h"
 
40
#include "keydb.h"
 
41
#include "memory.h"
 
42
#include "util.h"
 
43
#include "options.h"
 
44
#include "packet.h"
 
45
#include "main.h"
 
46
#include "i18n.h"
 
47
#include "tdbio.h"
 
48
#include "trustdb.h"
 
49
 
 
50
 
 
51
/*
 
52
 * A structure to store key identification as well as some stuff needed
 
53
 * for validation 
 
54
 */
 
55
struct key_item {
 
56
  struct key_item *next;
 
57
  unsigned int ownertrust,min_ownertrust;
 
58
  byte trust_depth;
 
59
  byte trust_value;
 
60
  char *trust_regexp;
 
61
  u32 kid[2];
 
62
};
 
63
 
 
64
 
 
65
typedef struct key_item **KeyHashTable; /* see new_key_hash_table() */
 
66
 
 
67
/*
 
68
 * Structure to keep track of keys, this is used as an array wherre
 
69
 * the item right after the last one has a keyblock set to NULL. 
 
70
 * Maybe we can drop this thing and replace it by key_item
 
71
 */
 
72
struct key_array {
 
73
  KBNODE keyblock;
 
74
};
 
75
 
 
76
 
 
77
/* control information for the trust DB */
 
78
static struct {
 
79
    int init;
 
80
    int level;
 
81
    char *dbname;
 
82
} trustdb_args;
 
83
 
 
84
/* some globals */
 
85
static struct key_item *user_utk_list; /* temp. used to store --trusted-keys */
 
86
static struct key_item *utk_list;      /* all ultimately trusted keys */
 
87
 
 
88
static int pending_check_trustdb;
 
89
 
 
90
static int validate_keys (int interactive);
 
91
 
 
92
 
 
93
/**********************************************
 
94
 ************* some helpers *******************
 
95
 **********************************************/
 
96
 
 
97
static struct key_item *
 
98
new_key_item (void)
 
99
{
 
100
  struct key_item *k;
 
101
  
 
102
  k = xcalloc (1,sizeof *k);
 
103
  return k;
 
104
}
 
105
 
 
106
static void
 
107
release_key_items (struct key_item *k)
 
108
{
 
109
  struct key_item *k2;
 
110
 
 
111
  for (; k; k = k2)
 
112
    {
 
113
      k2 = k->next;
 
114
      xfree (k->trust_regexp);
 
115
      xfree (k);
 
116
    }
 
117
}
 
118
 
 
119
/*
 
120
 * For fast keylook up we need a hash table.  Each byte of a KeyIDs
 
121
 * should be distributed equally over the 256 possible values (except
 
122
 * for v3 keyIDs but we consider them as not important here). So we
 
123
 * can just use 10 bits to index a table of 1024 key items. 
 
124
 * Possible optimization: Don not use key_items but other hash_table when the
 
125
 * duplicates lists gets too large. 
 
126
 */
 
127
static KeyHashTable 
 
128
new_key_hash_table (void)
 
129
{
 
130
  struct key_item **tbl;
 
131
 
 
132
  tbl = xcalloc (1,1024 * sizeof *tbl);
 
133
  return tbl;
 
134
}
 
135
 
 
136
static void
 
137
release_key_hash_table (KeyHashTable tbl)
 
138
{
 
139
  int i;
 
140
 
 
141
  if (!tbl)
 
142
    return;
 
143
  for (i=0; i < 1024; i++)
 
144
    release_key_items (tbl[i]);
 
145
  xfree (tbl);
 
146
}
 
147
 
 
148
/* 
 
149
 * Returns: True if the keyID is in the given hash table
 
150
 */
 
151
static int
 
152
test_key_hash_table (KeyHashTable tbl, u32 *kid)
 
153
{
 
154
  struct key_item *k;
 
155
 
 
156
  for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
 
157
    if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
 
158
      return 1;
 
159
  return 0;
 
160
}
 
161
 
 
162
/*
 
163
 * Add a new key to the hash table.  The key is identified by its key ID.
 
164
 */
 
165
static void
 
166
add_key_hash_table (KeyHashTable tbl, u32 *kid)
 
167
{
 
168
  struct key_item *k, *kk;
 
169
 
 
170
  for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
 
171
    if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
 
172
      return; /* already in table */
 
173
  
 
174
  kk = new_key_item ();
 
175
  kk->kid[0] = kid[0];
 
176
  kk->kid[1] = kid[1];
 
177
  kk->next = tbl[(kid[1] & 0x03ff)];
 
178
  tbl[(kid[1] & 0x03ff)] = kk;
 
179
}
 
180
 
 
181
/*
 
182
 * Release a key_array
 
183
 */
 
184
static void
 
185
release_key_array ( struct key_array *keys )
 
186
{
 
187
    struct key_array *k;
 
188
 
 
189
    if (keys) {
 
190
        for (k=keys; k->keyblock; k++)
 
191
            release_kbnode (k->keyblock);
 
192
        xfree (keys);
 
193
    }
 
194
}
 
195
 
 
196
 
 
197
/*********************************************
 
198
 **********  Initialization  *****************
 
199
 *********************************************/
 
200
 
 
201
 
 
202
 
 
203
/*
 
204
 * Used to register extra ultimately trusted keys - this has to be done
 
205
 * before initializing the validation module.
 
206
 * FIXME: Should be replaced by a function to add those keys to the trustdb.
 
207
 */
 
208
void
 
209
register_trusted_key( const char *string )
 
210
{
 
211
  KEYDB_SEARCH_DESC desc;
 
212
  struct key_item *k;
 
213
 
 
214
  if (classify_user_id (string, &desc) != KEYDB_SEARCH_MODE_LONG_KID ) {
 
215
    log_error(_("`%s' is not a valid long keyID\n"), string );
 
216
    return;
 
217
  }
 
218
 
 
219
  k = new_key_item ();
 
220
  k->kid[0] = desc.u.kid[0];
 
221
  k->kid[1] = desc.u.kid[1];
 
222
  k->next = user_utk_list;
 
223
  user_utk_list = k;
 
224
}
 
225
 
 
226
/*
 
227
 * Helper to add a key to the global list of ultimately trusted keys.
 
228
 * Retruns: true = inserted, false = already in in list.
 
229
 */
 
230
static int
 
231
add_utk (u32 *kid)
 
232
{
 
233
  struct key_item *k;
 
234
 
 
235
  for (k = utk_list; k; k = k->next) 
 
236
    {
 
237
      if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
 
238
        {
 
239
          return 0;
 
240
        }
 
241
    }
 
242
 
 
243
  k = new_key_item ();
 
244
  k->kid[0] = kid[0];
 
245
  k->kid[1] = kid[1];
 
246
  k->ownertrust = TRUST_ULTIMATE;
 
247
  k->next = utk_list;
 
248
  utk_list = k;
 
249
  if( opt.verbose > 1 )
 
250
    log_info(_("key %08lX: accepted as trusted key\n"), (ulong)kid[1]);
 
251
  return 1;
 
252
}
 
253
 
 
254
 
 
255
/****************
 
256
 * Verify that all our secret keys are usable and put them into the utk_list.
 
257
 */
 
258
static void
 
259
verify_own_keys(void)
 
260
{
 
261
  TRUSTREC rec;
 
262
  ulong recnum;
 
263
  int rc;
 
264
  struct key_item *k;
 
265
 
 
266
  if (utk_list)
 
267
    return;
 
268
 
 
269
  /* scan the trustdb to find all ultimately trusted keys */
 
270
  for (recnum=1; !tdbio_read_record (recnum, &rec, 0); recnum++ ) 
 
271
    {
 
272
      if ( rec.rectype == RECTYPE_TRUST 
 
273
           && (rec.r.trust.ownertrust & TRUST_MASK) == TRUST_ULTIMATE)
 
274
        {
 
275
            byte *fpr = rec.r.trust.fingerprint;
 
276
            int fprlen;
 
277
            u32 kid[2];
 
278
            
 
279
            /* Problem: We do only use fingerprints in the trustdb but
 
280
             * we need the keyID here to indetify the key; we can only
 
281
             * use that ugly hack to distinguish between 16 and 20
 
282
             * butes fpr - it does not work always so we better change
 
283
             * the whole validation code to only work with
 
284
             * fingerprints */
 
285
            fprlen = (!fpr[16] && !fpr[17] && !fpr[18] && !fpr[19])? 16:20;
 
286
            keyid_from_fingerprint (fpr, fprlen, kid);
 
287
            if (!add_utk (kid))
 
288
                log_info(_("key %08lX occurs more than once in the trustdb\n"),
 
289
                            (ulong)kid[1]);
 
290
        }
 
291
    }
 
292
 
 
293
  /* Put any --trusted-key keys into the trustdb */
 
294
  for (k = user_utk_list; k; k = k->next) 
 
295
    {
 
296
      if ( add_utk (k->kid) ) 
 
297
        { /* not yet in trustDB as ultimately trusted */
 
298
          PKT_public_key pk;
 
299
 
 
300
          memset (&pk, 0, sizeof pk);
 
301
          rc = get_pubkey (&pk, k->kid);
 
302
          if (rc) {
 
303
            log_info(_("key %08lX: no public key for trusted key - skipped\n"),
 
304
                     (ulong)k->kid[1] );
 
305
          }
 
306
          else {
 
307
            update_ownertrust (&pk,
 
308
                               ((get_ownertrust (&pk) & ~TRUST_MASK)
 
309
                                | TRUST_ULTIMATE ));
 
310
            release_public_key_parts (&pk);
 
311
          }
 
312
          log_info (_("key %08lX marked as ultimately trusted\n"),
 
313
                    (ulong)k->kid[1]);
 
314
        }
 
315
    }
 
316
 
 
317
 
 
318
  /* release the helper table table */
 
319
  release_key_items (user_utk_list);
 
320
  user_utk_list = NULL;
 
321
  return;
 
322
}
 
323
 
 
324
 
 
325
/*********************************************
 
326
 *********** TrustDB stuff *******************
 
327
 *********************************************/
 
328
 
 
329
/*
 
330
 * Read a record but die if it does not exist
 
331
 */
 
332
static void
 
333
read_record (ulong recno, TRUSTREC *rec, int rectype )
 
334
{
 
335
  int rc = tdbio_read_record (recno, rec, rectype);
 
336
  if (rc)
 
337
    {
 
338
      log_error(_("trust record %lu, req type %d: read failed: %s\n"),
 
339
                recno, rec->rectype, gpg_strerror (rc) );
 
340
      tdbio_invalid();
 
341
    }
 
342
  if (rectype != rec->rectype)
 
343
    {
 
344
      log_error(_("trust record %lu is not of requested type %d\n"),
 
345
                rec->recnum, rectype);
 
346
      tdbio_invalid();
 
347
    }
 
348
}
 
349
 
 
350
/*
 
351
 * Write a record and die on error
 
352
 */
 
353
static void
 
354
write_record (TRUSTREC *rec)
 
355
{
 
356
  int rc = tdbio_write_record (rec);
 
357
  if (rc)
 
358
    {
 
359
      log_error(_("trust record %lu, type %d: write failed: %s\n"),
 
360
                            rec->recnum, rec->rectype, gpg_strerror (rc) );
 
361
      tdbio_invalid();
 
362
    }
 
363
}
 
364
 
 
365
/*
 
366
 * sync the TrustDb and die on error
 
367
 */
 
368
static void
 
369
do_sync(void)
 
370
{
 
371
    int rc = tdbio_sync ();
 
372
    if(rc)
 
373
      {
 
374
        log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );
 
375
        g10_exit(2);
 
376
      }
 
377
}
 
378
 
 
379
static const char *
 
380
trust_model_string(void)
 
381
{
 
382
  switch(opt.trust_model)
 
383
    {
 
384
    case TM_PGP:     return "PGP";
 
385
    case TM_CLASSIC: return "classic";
 
386
    case TM_ALWAYS:  return "always";
 
387
    default:         return "unknown";
 
388
    }
 
389
}
 
390
 
 
391
/****************
 
392
 * Perform some checks over the trustdb
 
393
 *  level 0: only open the db
 
394
 *        1: used for initial program startup
 
395
 */
 
396
int
 
397
setup_trustdb( int level, const char *dbname )
 
398
{
 
399
    /* just store the args */
 
400
    if( trustdb_args.init )
 
401
        return 0;
 
402
    trustdb_args.level = level;
 
403
    trustdb_args.dbname = dbname? xstrdup (dbname): NULL;
 
404
    return 0;
 
405
}
 
406
 
 
407
void
 
408
init_trustdb()
 
409
{
 
410
  int rc=0;
 
411
  int level = trustdb_args.level;
 
412
  const char* dbname = trustdb_args.dbname;
 
413
 
 
414
  if( trustdb_args.init )
 
415
    return;
 
416
 
 
417
  trustdb_args.init = 1;
 
418
 
 
419
  if ( !level || level==1)
 
420
    {
 
421
      rc = tdbio_set_dbname( dbname, !!level );
 
422
      if( !rc )
 
423
        {
 
424
          if( !level )
 
425
            return;
 
426
          
 
427
          /* verify that our own keys are in the trustDB
 
428
           * or move them to the trustdb. */
 
429
          verify_own_keys();
 
430
          
 
431
          /* should we check whether there is no other ultimately trusted
 
432
           * key in the database? */
 
433
        }
 
434
    }
 
435
  else
 
436
    BUG();
 
437
  if( rc )
 
438
    log_fatal("can't init trustdb: %s\n", gpg_strerror (rc) );
 
439
 
 
440
  if(opt.trust_model==TM_AUTO)
 
441
    {
 
442
      /* Try and set the trust model off of whatever the trustdb says
 
443
         it is. */
 
444
      opt.trust_model=tdbio_read_model();
 
445
 
 
446
      /* Sanity check this ;) */
 
447
      if(opt.trust_model!=TM_PGP && opt.trust_model!=TM_CLASSIC)
 
448
        {
 
449
          log_info(_("unable to use unknown trust model (%d) - "
 
450
                     "assuming %s trust model\n"),opt.trust_model,"PGP");
 
451
          opt.trust_model=TM_PGP;
 
452
        }
 
453
 
 
454
      if(opt.verbose)
 
455
        log_info(_("using %s trust model\n"),trust_model_string());
 
456
    }
 
457
 
 
458
  if((opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
 
459
     && !tdbio_db_matches_options())
 
460
    pending_check_trustdb=1;
 
461
}
 
462
 
 
463
 
 
464
 
 
465
 
 
466
/***********************************************
 
467
 *************  Print helpers   ****************
 
468
 ***********************************************/
 
469
 
 
470
/****************
 
471
 * This function returns a letter for a trustvalue  Trust flags
 
472
 * are ignore.
 
473
 */
 
474
static int
 
475
trust_letter (unsigned int value)
 
476
{
 
477
  switch( (value & TRUST_MASK) ) 
 
478
    {
 
479
    case TRUST_UNKNOWN:   return '-';
 
480
    case TRUST_EXPIRED:   return 'e';
 
481
    case TRUST_UNDEFINED: return 'q';
 
482
    case TRUST_NEVER:     return 'n';
 
483
    case TRUST_MARGINAL:  return 'm';
 
484
    case TRUST_FULLY:     return 'f';
 
485
    case TRUST_ULTIMATE:  return 'u';
 
486
    default:              return '?';
 
487
    }
 
488
}
 
489
 
 
490
/* The strings here are similar to those in
 
491
   pkclist.c:do_edit_ownertrust() */
 
492
const char *
 
493
trust_value_to_string (unsigned int value)
 
494
{
 
495
  switch( (value & TRUST_MASK) ) 
 
496
    {
 
497
    case TRUST_UNKNOWN:   return _("unknown");
 
498
    case TRUST_EXPIRED:   return _("expired");
 
499
    case TRUST_UNDEFINED: return _("undefined");
 
500
    case TRUST_NEVER:     return _("never");
 
501
    case TRUST_MARGINAL:  return _("marginal");
 
502
    case TRUST_FULLY:     return _("full");
 
503
    case TRUST_ULTIMATE:  return _("ultimate");
 
504
    default:              return "err";
 
505
    }
 
506
}
 
507
 
 
508
int
 
509
string_to_trust_value (const char *str)
 
510
{
 
511
  if(ascii_strcasecmp(str,"undefined")==0)
 
512
    return TRUST_UNDEFINED;
 
513
  else if(ascii_strcasecmp(str,"never")==0)
 
514
    return TRUST_NEVER;
 
515
  else if(ascii_strcasecmp(str,"marginal")==0)
 
516
    return TRUST_MARGINAL;
 
517
  else if(ascii_strcasecmp(str,"full")==0)
 
518
    return TRUST_FULLY;
 
519
  else if(ascii_strcasecmp(str,"ultimate")==0)
 
520
    return TRUST_ULTIMATE;
 
521
  else
 
522
    return -1;
 
523
}
 
524
 
 
525
/****************
 
526
 * Recreate the WoT but do not ask for new ownertrusts.  Special
 
527
 * feature: In batch mode and without a forced yes, this is only done
 
528
 * when a check is due.  This can be used to run the check from a crontab
 
529
 */
 
530
void
 
531
check_trustdb ()
 
532
{
 
533
  init_trustdb();
 
534
  if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
 
535
    {
 
536
      if (opt.batch && !opt.answer_yes)
 
537
        {
 
538
          ulong scheduled;
 
539
 
 
540
          scheduled = tdbio_read_nextcheck ();
 
541
          if (!scheduled)
 
542
            {
 
543
              log_info (_("no need for a trustdb check\n"));
 
544
              return;
 
545
            }
 
546
 
 
547
          if (scheduled > make_timestamp ())
 
548
            {
 
549
              log_info (_("next trustdb check due at %s\n"),
 
550
                        strtimestamp (scheduled));
 
551
              return;
 
552
            }
 
553
        }
 
554
 
 
555
      validate_keys (0);
 
556
    }
 
557
  else
 
558
    log_info (_("no need for a trustdb check with \"%s\" trust model\n"),
 
559
              trust_model_string());
 
560
}
 
561
 
 
562
 
 
563
/*
 
564
 * Recreate the WoT. 
 
565
 */
 
566
void
 
567
update_trustdb()
 
568
{
 
569
  init_trustdb();
 
570
  if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
 
571
    validate_keys (1);
 
572
  else
 
573
    log_info (_("no need for a trustdb update with \"%s\" trust model\n"),
 
574
              trust_model_string());
 
575
}
 
576
 
 
577
void
 
578
revalidation_mark (void)
 
579
{
 
580
  init_trustdb();
 
581
  /* we simply set the time for the next check to 1 (far back in 1970)
 
582
   * so that a --update-trustdb will be scheduled */
 
583
  if (tdbio_write_nextcheck (1))
 
584
      do_sync ();
 
585
  pending_check_trustdb = 1;
 
586
}
 
587
 
 
588
int
 
589
trustdb_pending_check(void)
 
590
{
 
591
  return pending_check_trustdb;
 
592
}
 
593
 
 
594
void
 
595
read_trust_options(byte *trust_model,ulong *created,ulong *nextcheck,
 
596
                   byte *marginals,byte *completes,byte *cert_depth)
 
597
{
 
598
  TRUSTREC opts;
 
599
 
 
600
  init_trustdb();
 
601
 
 
602
  read_record(0,&opts,RECTYPE_VER);
 
603
 
 
604
  if(trust_model)
 
605
    *trust_model=opts.r.ver.trust_model;
 
606
  if(created)
 
607
    *created=opts.r.ver.created;
 
608
  if(nextcheck)
 
609
    *nextcheck=opts.r.ver.nextcheck;
 
610
  if(marginals)
 
611
    *marginals=opts.r.ver.marginals;
 
612
  if(completes)
 
613
    *completes=opts.r.ver.completes;
 
614
  if(cert_depth)
 
615
    *cert_depth=opts.r.ver.cert_depth;
 
616
}
 
617
 
 
618
 
 
619
 
 
620
/***********************************************
 
621
 ***********  Ownertrust et al. ****************
 
622
 ***********************************************/
 
623
 
 
624
static int 
 
625
read_trust_record (PKT_public_key *pk, TRUSTREC *rec)
 
626
{
 
627
  int rc;
 
628
  
 
629
  init_trustdb();
 
630
  rc = tdbio_search_trust_bypk (pk, rec);
 
631
  if (rc == -1)
 
632
    return -1; /* no record yet */
 
633
  if (rc) 
 
634
    {
 
635
      log_error ("trustdb: searching trust record failed: %s\n",
 
636
                 gpg_strerror (rc));
 
637
      return rc; 
 
638
    }
 
639
      
 
640
  if (rec->rectype != RECTYPE_TRUST)
 
641
    {
 
642
      log_error ("trustdb: record %lu is not a trust record\n",
 
643
                 rec->recnum);
 
644
      return GPG_ERR_TRUSTDB; 
 
645
    }      
 
646
  
 
647
  return 0;
 
648
}
 
649
 
 
650
/****************
 
651
 * Return the assigned ownertrust value for the given public key.
 
652
 * The key should be the primary key.
 
653
 */
 
654
unsigned int 
 
655
get_ownertrust ( PKT_public_key *pk)
 
656
{
 
657
  TRUSTREC rec;
 
658
  int rc;
 
659
  
 
660
  rc = read_trust_record (pk, &rec);
 
661
  if (rc == -1)
 
662
    return TRUST_UNKNOWN; /* no record yet */
 
663
  if (rc) 
 
664
    {
 
665
      tdbio_invalid ();
 
666
      return rc; /* actually never reached */
 
667
    }
 
668
 
 
669
  return rec.r.trust.ownertrust;
 
670
}
 
671
 
 
672
unsigned int 
 
673
get_min_ownertrust (PKT_public_key *pk)
 
674
{
 
675
  TRUSTREC rec;
 
676
  int rc;
 
677
  
 
678
  rc = read_trust_record (pk, &rec);
 
679
  if (rc == -1)
 
680
    return TRUST_UNKNOWN; /* no record yet */
 
681
  if (rc) 
 
682
    {
 
683
      tdbio_invalid ();
 
684
      return rc; /* actually never reached */
 
685
    }
 
686
 
 
687
  return rec.r.trust.min_ownertrust;
 
688
}
 
689
 
 
690
/*
 
691
 * Same as get_ownertrust but this takes the minimum ownertrust value
 
692
 * into into account, and will bump up the value as needed.
 
693
 */
 
694
static int
 
695
get_ownertrust_with_min (PKT_public_key *pk)
 
696
{
 
697
  unsigned int otrust,otrust_min;
 
698
 
 
699
  otrust = (get_ownertrust (pk) & TRUST_MASK);
 
700
  otrust_min = get_min_ownertrust (pk);
 
701
  if(otrust<otrust_min)
 
702
    {
 
703
      /* If the trust that the user has set is less than the trust
 
704
         that was calculated from a trust signature chain, use the
 
705
         higher of the two.  We do this here and not in
 
706
         get_ownertrust since the underlying ownertrust should not
 
707
         really be set - just the appearance of the ownertrust. */
 
708
 
 
709
      otrust=otrust_min;
 
710
    }
 
711
 
 
712
  return otrust;
 
713
}
 
714
 
 
715
/*
 
716
 * Same as get_ownertrust but return a trust letter instead of an
 
717
 * value.  This takes the minimum ownertrust value into account.
 
718
 */
 
719
int
 
720
get_ownertrust_info (PKT_public_key *pk)
 
721
{
 
722
  return trust_letter(get_ownertrust_with_min(pk));
 
723
}
 
724
 
 
725
/*
 
726
 * Same as get_ownertrust but return a trust string instead of an
 
727
 * value.  This takes the minimum ownertrust value into account.
 
728
 */
 
729
const char *
 
730
get_ownertrust_string (PKT_public_key *pk)
 
731
{
 
732
  return trust_value_to_string(get_ownertrust_with_min(pk));
 
733
}
 
734
 
 
735
/*
 
736
 * Set the trust value of the given public key to the new value.
 
737
 * The key should be a primary one.
 
738
 */
 
739
void
 
740
update_ownertrust (PKT_public_key *pk, unsigned int new_trust )
 
741
{
 
742
  TRUSTREC rec;
 
743
  int rc;
 
744
  
 
745
  rc = read_trust_record (pk, &rec);
 
746
  if (!rc)
 
747
    {
 
748
      if (DBG_TRUST)
 
749
        log_debug ("update ownertrust from %u to %u\n",
 
750
                   (unsigned int)rec.r.trust.ownertrust, new_trust );
 
751
      if (rec.r.trust.ownertrust != new_trust)
 
752
        {
 
753
          rec.r.trust.ownertrust = new_trust;
 
754
          write_record( &rec );
 
755
          revalidation_mark ();
 
756
          do_sync ();
 
757
        }
 
758
    }
 
759
  else if (rc == -1)
 
760
    { /* no record yet - create a new one */
 
761
      size_t dummy;
 
762
 
 
763
      if (DBG_TRUST)
 
764
        log_debug ("insert ownertrust %u\n", new_trust );
 
765
 
 
766
      memset (&rec, 0, sizeof rec);
 
767
      rec.recnum = tdbio_new_recnum ();
 
768
      rec.rectype = RECTYPE_TRUST;
 
769
      fingerprint_from_pk (pk, rec.r.trust.fingerprint, &dummy);
 
770
      rec.r.trust.ownertrust = new_trust;
 
771
      write_record (&rec);
 
772
      revalidation_mark ();
 
773
      do_sync ();
 
774
      rc = 0;
 
775
    }
 
776
  else 
 
777
    {
 
778
      tdbio_invalid ();
 
779
    }
 
780
}
 
781
 
 
782
static void
 
783
update_min_ownertrust (u32 *kid, unsigned int new_trust )
 
784
{
 
785
  PKT_public_key *pk;
 
786
  TRUSTREC rec;
 
787
  int rc;
 
788
 
 
789
  pk = xcalloc (1,sizeof *pk);
 
790
  rc = get_pubkey (pk, kid);
 
791
  if (rc)
 
792
    {
 
793
      log_error (_("public key %08lX not found: %s\n"),
 
794
                 (ulong)kid[1], gpg_strerror (rc) );
 
795
      return;
 
796
    }
 
797
 
 
798
  rc = read_trust_record (pk, &rec);
 
799
  if (!rc)
 
800
    {
 
801
      if (DBG_TRUST)
 
802
        log_debug ("key %08lX: update min_ownertrust from %u to %u\n",
 
803
                   (ulong)kid[1],(unsigned int)rec.r.trust.min_ownertrust,
 
804
                   new_trust );
 
805
      if (rec.r.trust.min_ownertrust != new_trust)
 
806
        {
 
807
          rec.r.trust.min_ownertrust = new_trust;
 
808
          write_record( &rec );
 
809
          revalidation_mark ();
 
810
          do_sync ();
 
811
        }
 
812
    }
 
813
  else if (rc == -1)
 
814
    { /* no record yet - create a new one */
 
815
      size_t dummy;
 
816
 
 
817
      if (DBG_TRUST)
 
818
        log_debug ("insert min_ownertrust %u\n", new_trust );
 
819
 
 
820
      memset (&rec, 0, sizeof rec);
 
821
      rec.recnum = tdbio_new_recnum ();
 
822
      rec.rectype = RECTYPE_TRUST;
 
823
      fingerprint_from_pk (pk, rec.r.trust.fingerprint, &dummy);
 
824
      rec.r.trust.min_ownertrust = new_trust;
 
825
      write_record (&rec);
 
826
      revalidation_mark ();
 
827
      do_sync ();
 
828
      rc = 0;
 
829
    }
 
830
  else 
 
831
    {
 
832
      tdbio_invalid ();
 
833
    }
 
834
}
 
835
 
 
836
/* Clear the ownertrust and min_ownertrust values.  Return true if a
 
837
   change actually happened. */
 
838
int
 
839
clear_ownertrusts (PKT_public_key *pk)
 
840
{
 
841
  TRUSTREC rec;
 
842
  int rc;
 
843
  
 
844
  rc = read_trust_record (pk, &rec);
 
845
  if (!rc)
 
846
    {
 
847
      if (DBG_TRUST)
 
848
        {
 
849
          log_debug ("clearing ownertrust (old value %u)\n",
 
850
                     (unsigned int)rec.r.trust.ownertrust);
 
851
          log_debug ("clearing min_ownertrust (old value %u)\n",
 
852
                     (unsigned int)rec.r.trust.min_ownertrust);
 
853
        }
 
854
      if (rec.r.trust.ownertrust || rec.r.trust.min_ownertrust)
 
855
        {
 
856
          rec.r.trust.ownertrust = 0;
 
857
          rec.r.trust.min_ownertrust = 0;
 
858
          write_record( &rec );
 
859
          revalidation_mark ();
 
860
          do_sync ();
 
861
          return 1;
 
862
        }
 
863
    }
 
864
  else if (rc != -1)
 
865
    {
 
866
      tdbio_invalid ();
 
867
    }
 
868
  return 0;
 
869
}
 
870
 
 
871
/* 
 
872
 * Note: Caller has to do a sync 
 
873
 */
 
874
static void
 
875
update_validity (PKT_public_key *pk, PKT_user_id *uid,
 
876
                 int depth, int validity)
 
877
{
 
878
  TRUSTREC trec, vrec;
 
879
  int rc;
 
880
  ulong recno;
 
881
 
 
882
  namehash_from_uid(uid);
 
883
 
 
884
  rc = read_trust_record (pk, &trec);
 
885
  if (rc && rc != -1)
 
886
    {
 
887
      tdbio_invalid ();
 
888
      return;
 
889
    }
 
890
  if (rc == -1) /* no record yet - create a new one */
 
891
    { 
 
892
      size_t dummy;
 
893
 
 
894
      rc = 0;
 
895
      memset (&trec, 0, sizeof trec);
 
896
      trec.recnum = tdbio_new_recnum ();
 
897
      trec.rectype = RECTYPE_TRUST;
 
898
      fingerprint_from_pk (pk, trec.r.trust.fingerprint, &dummy);
 
899
      trec.r.trust.ownertrust = 0;
 
900
      }
 
901
 
 
902
  /* locate an existing one */
 
903
  recno = trec.r.trust.validlist;
 
904
  while (recno)
 
905
    {
 
906
      read_record (recno, &vrec, RECTYPE_VALID);
 
907
      if ( !memcmp (vrec.r.valid.namehash, uid->namehash, 20) )
 
908
        break;
 
909
      recno = vrec.r.valid.next;
 
910
    }
 
911
 
 
912
  if (!recno) /* insert a new validity record */
 
913
    {
 
914
      memset (&vrec, 0, sizeof vrec);
 
915
      vrec.recnum = tdbio_new_recnum ();
 
916
      vrec.rectype = RECTYPE_VALID;
 
917
      memcpy (vrec.r.valid.namehash, uid->namehash, 20);
 
918
      vrec.r.valid.next = trec.r.trust.validlist;
 
919
      trec.r.trust.validlist = vrec.recnum;
 
920
    }
 
921
  vrec.r.valid.validity = validity;
 
922
  vrec.r.valid.full_count = uid->help_full_count;
 
923
  vrec.r.valid.marginal_count = uid->help_marginal_count;
 
924
  write_record (&vrec);
 
925
  trec.r.trust.depth = depth;
 
926
  write_record (&trec);
 
927
}
 
928
 
 
929
 
 
930
/* reset validity for all user IDs.  Caller must sync. */
 
931
static int
 
932
clear_validity (PKT_public_key *pk)
 
933
{
 
934
  TRUSTREC trec, vrec;
 
935
  int rc;
 
936
  ulong recno;
 
937
  int any = 0;
 
938
  
 
939
  rc = read_trust_record (pk, &trec);
 
940
  if (rc && rc != -1)
 
941
    {
 
942
      tdbio_invalid ();
 
943
      return 0;
 
944
    }
 
945
  if (rc == -1) /* no record yet - no need to clear it then ;-) */
 
946
    return 0;
 
947
 
 
948
  /* Clear minimum ownertrust, if any */
 
949
  if(trec.r.trust.min_ownertrust)
 
950
    {
 
951
      trec.r.trust.min_ownertrust=0;
 
952
      write_record(&trec);
 
953
    }
 
954
 
 
955
  recno = trec.r.trust.validlist;
 
956
  while (recno)
 
957
    {
 
958
      read_record (recno, &vrec, RECTYPE_VALID);
 
959
      if ((vrec.r.valid.validity & TRUST_MASK)
 
960
          || vrec.r.valid.marginal_count || vrec.r.valid.full_count)
 
961
        {
 
962
          vrec.r.valid.validity &= ~TRUST_MASK;
 
963
          vrec.r.valid.marginal_count = vrec.r.valid.full_count = 0;
 
964
          write_record (&vrec);
 
965
          any = 1;
 
966
        }
 
967
      recno = vrec.r.valid.next;
 
968
    }
 
969
 
 
970
  return any;
 
971
}
 
972
 
 
973
/***********************************************
 
974
 *********  Query trustdb values  **************
 
975
 ***********************************************/
 
976
 
 
977
/* Return true if key is disabled */
 
978
int
 
979
cache_disabled_value(PKT_public_key *pk)
 
980
{
 
981
  int rc;
 
982
  TRUSTREC trec;
 
983
  int disabled=0;
 
984
 
 
985
  if(pk->is_disabled)
 
986
    return (pk->is_disabled==2);
 
987
 
 
988
  init_trustdb();
 
989
 
 
990
  rc = read_trust_record (pk, &trec);
 
991
  if (rc && rc != -1)
 
992
    {
 
993
      tdbio_invalid ();
 
994
      goto leave;
 
995
    }
 
996
  if (rc == -1) /* no record found, so assume not disabled */
 
997
    goto leave;
 
998
 
 
999
  if(trec.r.trust.ownertrust & TRUST_FLAG_DISABLED)
 
1000
    disabled=1;
 
1001
 
 
1002
  /* Cache it for later so we don't need to look at the trustdb every
 
1003
     time */
 
1004
  if(disabled)
 
1005
    pk->is_disabled=2;
 
1006
  else
 
1007
    pk->is_disabled=1;
 
1008
 
 
1009
 leave:
 
1010
   return disabled;
 
1011
}
 
1012
 
 
1013
/*
 
1014
 * Return the validity information for PK.  If the namehash is not
 
1015
 * NULL, the validity of the corresponsing user ID is returned,
 
1016
 * otherwise, a reasonable value for the entire key is returned. 
 
1017
 */
 
1018
unsigned int
 
1019
get_validity (PKT_public_key *pk, PKT_user_id *uid)
 
1020
{
 
1021
  static int did_nextcheck;
 
1022
  TRUSTREC trec, vrec;
 
1023
  int rc;
 
1024
  ulong recno;
 
1025
  unsigned int validity;
 
1026
  u32 kid[2];
 
1027
  PKT_public_key *main_pk;
 
1028
 
 
1029
  if(uid)
 
1030
    namehash_from_uid(uid);
 
1031
 
 
1032
  init_trustdb ();
 
1033
  if (!did_nextcheck
 
1034
      && (opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC))
 
1035
    {
 
1036
      ulong scheduled;
 
1037
 
 
1038
      did_nextcheck = 1;
 
1039
      scheduled = tdbio_read_nextcheck ();
 
1040
      if (scheduled && scheduled <= make_timestamp ())
 
1041
        {
 
1042
          if (opt.no_auto_check_trustdb) 
 
1043
            {
 
1044
              pending_check_trustdb = 1;
 
1045
              log_info (_("please do a --check-trustdb\n"));
 
1046
            }
 
1047
          else
 
1048
            {
 
1049
              log_info (_("checking the trustdb\n"));
 
1050
              validate_keys (0);
 
1051
            }
 
1052
        }
 
1053
    }
 
1054
 
 
1055
  keyid_from_pk (pk, kid);
 
1056
  if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1])
 
1057
    { /* this is a subkey - get the mainkey */
 
1058
      main_pk = xcalloc (1,sizeof *main_pk);
 
1059
      rc = get_pubkey (main_pk, pk->main_keyid);
 
1060
      if (rc)
 
1061
        {
 
1062
          log_error ("error getting main key %08lX of subkey %08lX: %s\n",
 
1063
                     (ulong)pk->main_keyid[1], (ulong)kid[1], gpg_strerror (rc));
 
1064
          validity = TRUST_UNKNOWN; 
 
1065
          goto leave;
 
1066
        }
 
1067
    }
 
1068
  else
 
1069
    main_pk = pk;
 
1070
 
 
1071
  rc = read_trust_record (main_pk, &trec);
 
1072
  if (rc && rc != -1)
 
1073
    {
 
1074
      tdbio_invalid ();
 
1075
      return 0;
 
1076
    }
 
1077
  if (rc == -1) /* no record found */
 
1078
    {
 
1079
      validity = TRUST_UNKNOWN; 
 
1080
      goto leave;
 
1081
    }
 
1082
 
 
1083
  /* loop over all user IDs */
 
1084
  recno = trec.r.trust.validlist;
 
1085
  validity = 0;
 
1086
  while (recno)
 
1087
    {
 
1088
      read_record (recno, &vrec, RECTYPE_VALID);
 
1089
 
 
1090
      if(uid)
 
1091
        {
 
1092
          /* If a user ID is given we return the validity for that
 
1093
             user ID ONLY.  If the namehash is not found, then there
 
1094
             is no validity at all (i.e. the user ID wasn't
 
1095
             signed). */
 
1096
          if(memcmp(vrec.r.valid.namehash,uid->namehash,20)==0)
 
1097
            {
 
1098
              validity=(vrec.r.valid.validity & TRUST_MASK);
 
1099
              break;
 
1100
            }
 
1101
        }
 
1102
      else
 
1103
        {
 
1104
          /* If no namehash is given, we take the maximum validity
 
1105
             over all user IDs */
 
1106
          if ( validity < (vrec.r.valid.validity & TRUST_MASK) )
 
1107
            validity = (vrec.r.valid.validity & TRUST_MASK);
 
1108
        }
 
1109
 
 
1110
      recno = vrec.r.valid.next;
 
1111
    }
 
1112
  
 
1113
  if ( (trec.r.trust.ownertrust & TRUST_FLAG_DISABLED) )
 
1114
    {
 
1115
      validity |= TRUST_FLAG_DISABLED;
 
1116
      pk->is_disabled=2;
 
1117
    }
 
1118
  else
 
1119
    pk->is_disabled=1;
 
1120
 
 
1121
 leave:
 
1122
  /* set some flags direct from the key */
 
1123
  if (main_pk->is_revoked)
 
1124
    validity |= TRUST_FLAG_REVOKED;
 
1125
  if (main_pk != pk && pk->is_revoked)
 
1126
    validity |= TRUST_FLAG_SUB_REVOKED;
 
1127
  /* Note: expiration is a trust value and not a flag - don't know why
 
1128
   * I initially designed it that way */
 
1129
  if (main_pk->has_expired || pk->has_expired)
 
1130
    validity = (validity & ~TRUST_MASK) | TRUST_EXPIRED;
 
1131
  
 
1132
  if (pending_check_trustdb)
 
1133
    validity |= TRUST_FLAG_PENDING_CHECK;
 
1134
 
 
1135
  if (main_pk != pk)
 
1136
    free_public_key (main_pk);
 
1137
  return validity;
 
1138
}
 
1139
 
 
1140
int
 
1141
get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
 
1142
{
 
1143
    int trustlevel;
 
1144
 
 
1145
    trustlevel = get_validity (pk, uid);
 
1146
    if( trustlevel & TRUST_FLAG_REVOKED )
 
1147
        return 'r';
 
1148
    return trust_letter ( trustlevel );
 
1149
}
 
1150
 
 
1151
const char *
 
1152
get_validity_string (PKT_public_key *pk, PKT_user_id *uid)
 
1153
{
 
1154
  int trustlevel;
 
1155
 
 
1156
  trustlevel = get_validity (pk, uid);
 
1157
  if( trustlevel & TRUST_FLAG_REVOKED )
 
1158
    return _("revoked");
 
1159
  return trust_value_to_string(trustlevel);
 
1160
}
 
1161
 
 
1162
static void
 
1163
get_validity_counts (PKT_public_key *pk, PKT_user_id *uid)
 
1164
{
 
1165
  TRUSTREC trec, vrec;
 
1166
  ulong recno;
 
1167
 
 
1168
  if(pk==NULL || uid==NULL)
 
1169
    BUG();
 
1170
 
 
1171
  namehash_from_uid(uid);
 
1172
 
 
1173
  uid->help_marginal_count=uid->help_full_count=0;
 
1174
 
 
1175
  init_trustdb ();
 
1176
 
 
1177
  if(read_trust_record (pk, &trec)!=0)
 
1178
    return;
 
1179
 
 
1180
  /* loop over all user IDs */
 
1181
  recno = trec.r.trust.validlist;
 
1182
  while (recno)
 
1183
    {
 
1184
      read_record (recno, &vrec, RECTYPE_VALID);
 
1185
 
 
1186
      if(memcmp(vrec.r.valid.namehash,uid->namehash,20)==0)
 
1187
        {
 
1188
          uid->help_marginal_count=vrec.r.valid.marginal_count;
 
1189
          uid->help_full_count=vrec.r.valid.full_count;
 
1190
          /*  printf("Fetched marginal %d, full %d\n",uid->help_marginal_count,uid->help_full_count); */
 
1191
          break;
 
1192
        }
 
1193
 
 
1194
      recno = vrec.r.valid.next;
 
1195
    }
 
1196
}
 
1197
 
 
1198
void
 
1199
list_trust_path( const char *username )
 
1200
{
 
1201
}
 
1202
 
 
1203
/****************
 
1204
 * Enumerate all keys, which are needed to build all trust paths for
 
1205
 * the given key.  This function does not return the key itself or
 
1206
 * the ultimate key (the last point in cerificate chain).  Only
 
1207
 * certificate chains which ends up at an ultimately trusted key
 
1208
 * are listed.  If ownertrust or validity is not NULL, the corresponding
 
1209
 * value for the returned LID is also returned in these variable(s).
 
1210
 *
 
1211
 *  1) create a void pointer and initialize it to NULL
 
1212
 *  2) pass this void pointer by reference to this function.
 
1213
 *     Set lid to the key you want to enumerate and pass it by reference.
 
1214
 *  3) call this function as long as it does not return -1
 
1215
 *     to indicate EOF. LID does contain the next key used to build the web
 
1216
 *  4) Always call this function a last time with LID set to NULL,
 
1217
 *     so that it can free its context.
 
1218
 *
 
1219
 * Returns: -1 on EOF or the level of the returned LID
 
1220
 */
 
1221
int
 
1222
enum_cert_paths( void **context, ulong *lid,
 
1223
                 unsigned *ownertrust, unsigned *validity )
 
1224
{
 
1225
    return -1;
 
1226
}
 
1227
 
 
1228
 
 
1229
/****************
 
1230
 * Print the current path
 
1231
 */
 
1232
void
 
1233
enum_cert_paths_print( void **context, FILE *fp,
 
1234
                                       int refresh, ulong selected_lid )
 
1235
{
 
1236
    return;
 
1237
}
 
1238
 
 
1239
 
 
1240
 
 
1241
/****************************************
 
1242
 *********** NEW NEW NEW ****************
 
1243
 ****************************************/
 
1244
 
 
1245
static int
 
1246
ask_ownertrust (u32 *kid,int minimum)
 
1247
{
 
1248
  PKT_public_key *pk;
 
1249
  int rc;
 
1250
  int ot;
 
1251
 
 
1252
  pk = xcalloc (1,sizeof *pk);
 
1253
  rc = get_pubkey (pk, kid);
 
1254
  if (rc)
 
1255
    {
 
1256
      log_error (_("public key %08lX not found: %s\n"),
 
1257
                 (ulong)kid[1], gpg_strerror (rc) );
 
1258
      return TRUST_UNKNOWN;
 
1259
    }
 
1260
 
 
1261
  if(opt.force_ownertrust)
 
1262
    {
 
1263
      log_info("force trust for key %08lX%08lX to %s\n",
 
1264
               (ulong)kid[0],(ulong)kid[1],
 
1265
               trust_value_to_string(opt.force_ownertrust));
 
1266
      update_ownertrust(pk,opt.force_ownertrust);
 
1267
      ot=opt.force_ownertrust;
 
1268
    }
 
1269
  else
 
1270
    {
 
1271
      ot=edit_ownertrust(pk,0);
 
1272
      if(ot>0)
 
1273
        ot = get_ownertrust (pk);
 
1274
      else if(ot==0)
 
1275
        ot = minimum?minimum:TRUST_UNDEFINED;
 
1276
      else
 
1277
        ot = -1; /* quit */
 
1278
    }
 
1279
 
 
1280
  free_public_key( pk );
 
1281
 
 
1282
  return ot;
 
1283
}
 
1284
 
 
1285
 
 
1286
static void
 
1287
mark_keyblock_seen (KeyHashTable tbl, KBNODE node)
 
1288
{
 
1289
  for ( ;node; node = node->next )
 
1290
    if (node->pkt->pkttype == PKT_PUBLIC_KEY
 
1291
        || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
 
1292
      {
 
1293
        u32 aki[2];
 
1294
 
 
1295
        keyid_from_pk (node->pkt->pkt.public_key, aki);
 
1296
        add_key_hash_table (tbl, aki);
 
1297
      }
 
1298
}
 
1299
 
 
1300
 
 
1301
static void
 
1302
dump_key_array (int depth, struct key_array *keys)
 
1303
{
 
1304
  struct key_array *kar;
 
1305
 
 
1306
  for (kar=keys; kar->keyblock; kar++)
 
1307
    {
 
1308
      KBNODE node = kar->keyblock;
 
1309
      u32 kid[2];
 
1310
 
 
1311
      keyid_from_pk(node->pkt->pkt.public_key, kid);
 
1312
      printf ("%d:%08lX%08lX:K::%c::::\n",
 
1313
              depth, (ulong)kid[0], (ulong)kid[1], '?');
 
1314
 
 
1315
      for (; node; node = node->next)
 
1316
        {
 
1317
          if (node->pkt->pkttype == PKT_USER_ID)
 
1318
            {
 
1319
              int len = node->pkt->pkt.user_id->len;
 
1320
 
 
1321
              if (len > 30)
 
1322
                len = 30;
 
1323
              printf ("%d:%08lX%08lX:U:::%c:::",
 
1324
                      depth, (ulong)kid[0], (ulong)kid[1],
 
1325
                      (node->flag & 4)? 'f':
 
1326
                      (node->flag & 2)? 'm':
 
1327
                      (node->flag & 1)? 'q':'-');
 
1328
              print_string (stdout,  node->pkt->pkt.user_id->name, len, ':');
 
1329
              putchar (':');
 
1330
              putchar ('\n');
 
1331
            }
 
1332
        }
 
1333
    }
 
1334
}  
 
1335
 
 
1336
 
 
1337
static void
 
1338
store_validation_status (int depth, KBNODE keyblock, KeyHashTable stored)
 
1339
{
 
1340
  KBNODE node;
 
1341
  int status;
 
1342
  int any = 0;
 
1343
 
 
1344
  for (node=keyblock; node; node = node->next)
 
1345
    {
 
1346
      if (node->pkt->pkttype == PKT_USER_ID)
 
1347
        {
 
1348
          PKT_user_id *uid = node->pkt->pkt.user_id;
 
1349
          if (node->flag & 4)
 
1350
            status = TRUST_FULLY;
 
1351
          else if (node->flag & 2)
 
1352
            status = TRUST_MARGINAL;
 
1353
          else if (node->flag & 1)
 
1354
            status = TRUST_UNDEFINED;
 
1355
          else
 
1356
            status = 0;
 
1357
          
 
1358
          if (status)
 
1359
            {
 
1360
              update_validity (keyblock->pkt->pkt.public_key,
 
1361
                               uid, depth, status);
 
1362
 
 
1363
              mark_keyblock_seen(stored,keyblock);
 
1364
 
 
1365
              any = 1;
 
1366
            }
 
1367
        }
 
1368
    }
 
1369
 
 
1370
  if (any)
 
1371
    do_sync ();
 
1372
}  
 
1373
 
 
1374
/*
 
1375
 * check whether the signature sig is in the klist k
 
1376
 */
 
1377
static struct key_item *
 
1378
is_in_klist (struct key_item *k, PKT_signature *sig)
 
1379
{
 
1380
  for (; k; k = k->next)
 
1381
    {
 
1382
      if (k->kid[0] == sig->keyid[0] && k->kid[1] == sig->keyid[1])
 
1383
        return k;
 
1384
    }
 
1385
  return NULL;
 
1386
}
 
1387
 
 
1388
/*
 
1389
 * Mark the signature of the given UID which are used to certify it.
 
1390
 * To do this, we first revmove all signatures which are not valid and
 
1391
 * from the remain ones we look for the latest one.  If this is not a
 
1392
 * certification revocation signature we mark the signature by setting
 
1393
 * node flag bit 8.  Note that flag bits 9 and 10 are used for internal
 
1394
 * purposes.  
 
1395
 */
 
1396
static void
 
1397
mark_usable_uid_certs (KBNODE keyblock, KBNODE uidnode,
 
1398
                       u32 *main_kid, struct key_item *klist,
 
1399
                       u32 curtime, u32 *next_expire)
 
1400
{
 
1401
  KBNODE node;
 
1402
  PKT_signature *sig;
 
1403
  
 
1404
  /* first check all signatures */
 
1405
  for (node=uidnode->next; node; node = node->next)
 
1406
    {
 
1407
      node->flag &= ~(1<<8 | 1<<9 | 1<<10);
 
1408
      if (node->pkt->pkttype == PKT_USER_ID
 
1409
          || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
 
1410
        break; /* ready */
 
1411
      if (node->pkt->pkttype != PKT_SIGNATURE)
 
1412
        continue;
 
1413
      
 
1414
      sig = node->pkt->pkt.signature;
 
1415
      if (sig->keyid[0] == main_kid[0] && sig->keyid[1] == main_kid[1])
 
1416
        continue; /* ignore self-signatures */
 
1417
      if (!IS_UID_SIG(sig) && !IS_UID_REV(sig))
 
1418
        continue; /* we only look at these signature classes */
 
1419
      if (!is_in_klist (klist, sig))
 
1420
        continue;  /* no need to check it then */
 
1421
      if (check_key_signature (keyblock, node, NULL))
 
1422
        continue; /* ignore invalid signatures */
 
1423
      node->flag |= 1<<9;
 
1424
    }      
 
1425
  /* reset the remaining flags */
 
1426
  for (; node; node = node->next)
 
1427
      node->flag &= ~(1<<8 | 1<<9 | 1 << 10);
 
1428
 
 
1429
  /* kbnode flag usage: bit 9 is here set for signatures to consider,
 
1430
   * bit 10 will be set by the loop to keep track of keyIDs already
 
1431
   * processed, bit 8 will be set for the usable signatures */
 
1432
 
 
1433
  /* for each cert figure out the latest valid one */
 
1434
  for (node=uidnode->next; node; node = node->next)
 
1435
    {
 
1436
      KBNODE n, signode;
 
1437
      u32 kid[2];
 
1438
      u32 sigdate;
 
1439
      
 
1440
      if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
 
1441
        break;
 
1442
      if ( !(node->flag & (1<<9)) )
 
1443
        continue; /* not a node to look at */
 
1444
      if ( (node->flag & (1<<10)) )
 
1445
        continue; /* signature with a keyID already processed */
 
1446
      node->flag |= (1<<10); /* mark this node as processed */
 
1447
      sig = node->pkt->pkt.signature;
 
1448
      signode = node;
 
1449
      sigdate = sig->timestamp;
 
1450
      kid[0] = sig->keyid[0]; kid[1] = sig->keyid[1];
 
1451
      for (n=uidnode->next; n; n = n->next)
 
1452
        {
 
1453
          if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY)
 
1454
            break;
 
1455
          if ( !(n->flag & (1<<9)) )
 
1456
            continue;
 
1457
          if ( (n->flag & (1<<10)) )
 
1458
            continue; /* shortcut already processed signatures */
 
1459
          sig = n->pkt->pkt.signature;
 
1460
          if (kid[0] != sig->keyid[0] || kid[1] != sig->keyid[1])
 
1461
            continue;
 
1462
          n->flag |= (1<<10); /* mark this node as processed */
 
1463
 
 
1464
          /* If signode is nonrevocable and unexpired and n isn't,
 
1465
             then take signode (skip).  It doesn't matter which is
 
1466
             older: if signode was older then we don't want to take n
 
1467
             as signode is nonrevocable.  If n was older then we're
 
1468
             automatically fine. */
 
1469
          
 
1470
          if(((IS_UID_SIG(signode->pkt->pkt.signature) &&
 
1471
               !signode->pkt->pkt.signature->flags.revocable &&
 
1472
               (signode->pkt->pkt.signature->expiredate==0 ||
 
1473
                signode->pkt->pkt.signature->expiredate>curtime))) &&
 
1474
             (!(IS_UID_SIG(n->pkt->pkt.signature) &&
 
1475
                !n->pkt->pkt.signature->flags.revocable &&
 
1476
                (n->pkt->pkt.signature->expiredate==0 ||
 
1477
                 n->pkt->pkt.signature->expiredate>curtime))))
 
1478
            continue;
 
1479
 
 
1480
          /* If n is nonrevocable and unexpired and signode isn't,
 
1481
             then take n.  Again, it doesn't matter which is older: if
 
1482
             n was older then we don't want to take signode as n is
 
1483
             nonrevocable.  If signode was older then we're
 
1484
             automatically fine. */
 
1485
          
 
1486
          if((!(IS_UID_SIG(signode->pkt->pkt.signature) &&
 
1487
                !signode->pkt->pkt.signature->flags.revocable &&
 
1488
                (signode->pkt->pkt.signature->expiredate==0 ||
 
1489
                 signode->pkt->pkt.signature->expiredate>curtime))) &&
 
1490
             ((IS_UID_SIG(n->pkt->pkt.signature) &&
 
1491
               !n->pkt->pkt.signature->flags.revocable &&
 
1492
               (n->pkt->pkt.signature->expiredate==0 ||
 
1493
                n->pkt->pkt.signature->expiredate>curtime))))
 
1494
            {
 
1495
              signode = n;
 
1496
              sigdate = sig->timestamp;
 
1497
              continue;
 
1498
            }
 
1499
 
 
1500
          /* At this point, if it's newer, it goes in as the only
 
1501
             remaining possibilities are signode and n are both either
 
1502
             revocable or expired or both nonrevocable and unexpired.
 
1503
             If the timestamps are equal take the later ordered
 
1504
             packet, presuming that the key packets are hopefully in
 
1505
             their original order. */
 
1506
 
 
1507
          if (sig->timestamp >= sigdate)
 
1508
            {
 
1509
              signode = n;
 
1510
              sigdate = sig->timestamp;
 
1511
            }
 
1512
        }
 
1513
      sig = signode->pkt->pkt.signature;
 
1514
      if (IS_UID_SIG (sig))
 
1515
        { /* this seems to be a usable one which is not revoked. 
 
1516
           * Just need to check whether there is an expiration time,
 
1517
           * We do the expired certification after finding a suitable
 
1518
           * certification, the assumption is that a signator does not
 
1519
           * want that after the expiration of his certificate the
 
1520
           * system falls back to an older certification which has a
 
1521
           * different expiration time */
 
1522
          const byte *p;
 
1523
          u32 expire;
 
1524
                    
 
1525
          p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_EXPIRE, NULL );
 
1526
          expire = p? sig->timestamp + buffer_to_u32(p) : 0;
 
1527
 
 
1528
          if (expire==0 || expire > curtime )
 
1529
            {
 
1530
              signode->flag |= (1<<8); /* yeah, found a good cert */
 
1531
              if (expire && expire < *next_expire)
 
1532
                *next_expire = expire;
 
1533
            }
 
1534
        }
 
1535
    }
 
1536
}
 
1537
 
 
1538
/* Used by validate_one_keyblock to confirm a regexp within a trust
 
1539
   signature.  Returns 1 for match, and 0 for no match or regex
 
1540
   error. */
 
1541
static int
 
1542
check_regexp(const char *expr,const char *string)
 
1543
{
 
1544
#ifdef DISABLE_REGEX
 
1545
  /* When DISABLE_REGEX is defined, assume all regexps do not
 
1546
     match. */
 
1547
  return 0;
 
1548
#elif defined(__riscos__)
 
1549
  return riscos_check_regexp(expr, string, DBG_TRUST);
 
1550
#else
 
1551
  int ret;
 
1552
  regex_t pat;
 
1553
 
 
1554
  if(regcomp(&pat,expr,REG_ICASE|REG_NOSUB|REG_EXTENDED)!=0)
 
1555
    return 0;
 
1556
 
 
1557
  ret=regexec(&pat,string,0,NULL,0);
 
1558
 
 
1559
  regfree(&pat);
 
1560
 
 
1561
  if(DBG_TRUST)
 
1562
    log_debug("regexp \"%s\" on \"%s\": %s\n",expr,string,ret==0?"YES":"NO");
 
1563
 
 
1564
  return (ret==0);
 
1565
#endif
 
1566
}
 
1567
 
 
1568
/*
 
1569
 * Return true if the key is signed by one of the keys in the given
 
1570
 * key ID list.  User IDs with a valid signature are marked by node
 
1571
 * flags as follows:
 
1572
 *  flag bit 0: There is at least one signature
 
1573
 *           1: There is marginal confidence that this is a legitimate uid
 
1574
 *           2: There is full confidence that this is a legitimate uid.
 
1575
 *           8: Used for internal purposes.
 
1576
 *           9: Ditto (in mark_usable_uid_certs())
 
1577
 *          10: Ditto (ditto)
 
1578
 * This function assumes that all kbnode flags are cleared on entry.
 
1579
 */
 
1580
static int
 
1581
validate_one_keyblock (KBNODE kb, struct key_item *klist,
 
1582
                       u32 curtime, u32 *next_expire)
 
1583
{
 
1584
  struct key_item *kr;
 
1585
  KBNODE node, uidnode=NULL;
 
1586
  PKT_user_id *uid=NULL;
 
1587
  PKT_public_key *pk = kb->pkt->pkt.public_key;
 
1588
  u32 main_kid[2];
 
1589
  int issigned=0, any_signed = 0;
 
1590
 
 
1591
  keyid_from_pk(pk, main_kid);
 
1592
  for (node=kb; node; node = node->next)
 
1593
    {
 
1594
      /* A bit of discussion here: is it better for the web of trust
 
1595
         to be built among only self-signed uids?  On the one hand, a
 
1596
         self-signed uid is a statement that the key owner definitely
 
1597
         intended that uid to be there, but on the other hand, a
 
1598
         signed (but not self-signed) uid does carry trust, of a sort,
 
1599
         even if it is a statement being made by people other than the
 
1600
         key owner "through" the uids on the key owner's key.  I'm
 
1601
         going with the latter.  However, if the user ID was
 
1602
         explicitly revoked, or passively allowed to expire, that
 
1603
         should stop validity through the user ID until it is
 
1604
         resigned.  -dshaw */
 
1605
 
 
1606
      if (node->pkt->pkttype == PKT_USER_ID
 
1607
          && !node->pkt->pkt.user_id->is_revoked
 
1608
          && !node->pkt->pkt.user_id->is_expired)
 
1609
        {
 
1610
          if (uidnode && issigned)
 
1611
            {
 
1612
              if (uid->help_full_count >= opt.completes_needed
 
1613
                  || uid->help_marginal_count >= opt.marginals_needed )
 
1614
                uidnode->flag |= 4; 
 
1615
              else if (uid->help_full_count || uid->help_marginal_count)
 
1616
                uidnode->flag |= 2;
 
1617
              uidnode->flag |= 1;
 
1618
              any_signed = 1;
 
1619
            }
 
1620
          uidnode = node;
 
1621
          uid=uidnode->pkt->pkt.user_id;
 
1622
 
 
1623
          /* If the selfsig is going to expire... */
 
1624
          if(uid->expiredate && uid->expiredate<*next_expire)
 
1625
            *next_expire = uid->expiredate;
 
1626
 
 
1627
          issigned = 0;
 
1628
          get_validity_counts(pk,uid);
 
1629
          mark_usable_uid_certs (kb, uidnode, main_kid, klist, 
 
1630
                                 curtime, next_expire);
 
1631
        }
 
1632
      else if (node->pkt->pkttype == PKT_SIGNATURE
 
1633
               && (node->flag & (1<<8)) && uid)
 
1634
        {
 
1635
          /* Note that we are only seeing unrevoked sigs here */
 
1636
          PKT_signature *sig = node->pkt->pkt.signature;
 
1637
          
 
1638
          kr = is_in_klist (klist, sig);
 
1639
          /* If the trust_regexp does not match, it's as if the sig
 
1640
             did not exist.  This is safe for non-trust sigs as well
 
1641
             since we don't accept a regexp on the sig unless it's a
 
1642
             trust sig. */
 
1643
          if (kr && (kr->trust_regexp==NULL || opt.trust_model!=TM_PGP ||
 
1644
                     (uidnode && check_regexp(kr->trust_regexp,
 
1645
                                            uidnode->pkt->pkt.user_id->name))))
 
1646
            {
 
1647
              if(DBG_TRUST && opt.trust_model==TM_PGP && sig->trust_depth)
 
1648
                log_debug("trust sig on %s, sig depth is %d, kr depth is %d\n",
 
1649
                          uidnode->pkt->pkt.user_id->name,sig->trust_depth,
 
1650
                          kr->trust_depth);
 
1651
 
 
1652
              /* Are we part of a trust sig chain?  We always favor
 
1653
                 the latest trust sig, rather than the greater or
 
1654
                 lesser trust sig or value.  I could make a decent
 
1655
                 argument for any of these cases, but this seems to be
 
1656
                 what PGP does, and I'd like to be compatible. -dms */
 
1657
              if(opt.trust_model==TM_PGP && sig->trust_depth
 
1658
                 && pk->trust_timestamp<=sig->timestamp
 
1659
                 && (sig->trust_depth<=kr->trust_depth
 
1660
                     || kr->ownertrust==TRUST_ULTIMATE))
 
1661
                {
 
1662
                  /* If we got here, we know that:
 
1663
 
 
1664
                     this is a trust sig.
 
1665
 
 
1666
                     it's a newer trust sig than any previous trust
 
1667
                     sig on this key (not uid).
 
1668
 
 
1669
                     it is legal in that it was either generated by an
 
1670
                     ultimate key, or a key that was part of a trust
 
1671
                     chain, and the depth does not violate the
 
1672
                     original trust sig.
 
1673
 
 
1674
                     if there is a regexp attached, it matched
 
1675
                     successfully.
 
1676
                  */
 
1677
 
 
1678
                  if(DBG_TRUST)
 
1679
                    log_debug("replacing trust value %d with %d and "
 
1680
                              "depth %d with %d\n",
 
1681
                              pk->trust_value,sig->trust_value,
 
1682
                              pk->trust_depth,sig->trust_depth);
 
1683
 
 
1684
                  pk->trust_value=sig->trust_value;
 
1685
                  pk->trust_depth=sig->trust_depth-1;
 
1686
 
 
1687
                  /* If the trust sig contains a regexp, record it
 
1688
                     on the pk for the next round. */
 
1689
                  if(sig->trust_regexp)
 
1690
                    pk->trust_regexp=sig->trust_regexp;
 
1691
                }
 
1692
 
 
1693
              if (kr->ownertrust == TRUST_ULTIMATE)
 
1694
                uid->help_full_count = opt.completes_needed;
 
1695
              else if (kr->ownertrust == TRUST_FULLY)
 
1696
                uid->help_full_count++;
 
1697
              else if (kr->ownertrust == TRUST_MARGINAL)
 
1698
                uid->help_marginal_count++;
 
1699
              issigned = 1;
 
1700
            }
 
1701
        }
 
1702
    }
 
1703
 
 
1704
  if (uidnode && issigned)
 
1705
    {
 
1706
      if (uid->help_full_count >= opt.completes_needed
 
1707
          || uid->help_marginal_count >= opt.marginals_needed )
 
1708
        uidnode->flag |= 4; 
 
1709
      else if (uid->help_full_count || uid->help_marginal_count)
 
1710
        uidnode->flag |= 2;
 
1711
      uidnode->flag |= 1;
 
1712
      any_signed = 1;
 
1713
    }
 
1714
 
 
1715
  return any_signed;
 
1716
}
 
1717
 
 
1718
 
 
1719
static int
 
1720
search_skipfnc (void *opaque, u32 *kid)
 
1721
{
 
1722
  return test_key_hash_table ((KeyHashTable)opaque, kid);
 
1723
}
 
1724
 
 
1725
 
 
1726
/*
 
1727
 * Scan all keys and return a key_array of all suitable keys from
 
1728
 * kllist.  The caller has to pass keydb handle so that we don't use
 
1729
 * to create our own.  Returns either a key_array or NULL in case of
 
1730
 * an error.  No results found are indicated by an empty array.
 
1731
 * Caller hast to release the returned array.  
 
1732
 */
 
1733
static struct key_array *
 
1734
validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
 
1735
                   struct key_item *klist, u32 curtime, u32 *next_expire)
 
1736
{
 
1737
  KBNODE keyblock = NULL;
 
1738
  struct key_array *keys = NULL;
 
1739
  size_t nkeys, maxkeys;
 
1740
  int rc;
 
1741
  KEYDB_SEARCH_DESC desc;
 
1742
  
 
1743
  maxkeys = 1000;
 
1744
  keys = xmalloc ((maxkeys+1) * sizeof *keys);
 
1745
  nkeys = 0;
 
1746
  
 
1747
  rc = keydb_search_reset (hd);
 
1748
  if (rc)
 
1749
    {
 
1750
      log_error ("keydb_search_reset failed: %s\n", gpg_strerror (rc));
 
1751
      xfree (keys);
 
1752
      return NULL;
 
1753
    }
 
1754
 
 
1755
  memset (&desc, 0, sizeof desc);
 
1756
  desc.mode = KEYDB_SEARCH_MODE_FIRST;
 
1757
  desc.skipfnc = search_skipfnc;
 
1758
  desc.skipfncvalue = full_trust;
 
1759
  rc = keydb_search (hd, &desc, 1);
 
1760
  if (rc == -1)
 
1761
    {
 
1762
      keys[nkeys].keyblock = NULL;
 
1763
      return keys;
 
1764
    }
 
1765
  if (rc)
 
1766
    {
 
1767
      log_error ("keydb_search_first failed: %s\n", gpg_strerror (rc));
 
1768
      xfree (keys);
 
1769
      return NULL;
 
1770
    }
 
1771
  
 
1772
  desc.mode = KEYDB_SEARCH_MODE_NEXT; /* change mode */
 
1773
  do
 
1774
    {
 
1775
      PKT_public_key *pk;
 
1776
        
 
1777
      rc = keydb_get_keyblock (hd, &keyblock);
 
1778
      if (rc) 
 
1779
        {
 
1780
          log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
 
1781
          xfree (keys);
 
1782
          return NULL;
 
1783
        }
 
1784
      
 
1785
      if ( keyblock->pkt->pkttype != PKT_PUBLIC_KEY) 
 
1786
        {
 
1787
          log_debug ("ooops: invalid pkttype %d encountered\n",
 
1788
                     keyblock->pkt->pkttype);
 
1789
          dump_kbnode (keyblock);
 
1790
          release_kbnode(keyblock);
 
1791
          continue;
 
1792
        }
 
1793
 
 
1794
      /* prepare the keyblock for further processing */
 
1795
      merge_keys_and_selfsig (keyblock); 
 
1796
      clear_kbnode_flags (keyblock);
 
1797
      pk = keyblock->pkt->pkt.public_key;
 
1798
      if (pk->has_expired || pk->is_revoked)
 
1799
        {
 
1800
          /* it does not make sense to look further at those keys */
 
1801
          mark_keyblock_seen (full_trust, keyblock);
 
1802
        }
 
1803
      else if (validate_one_keyblock (keyblock, klist, curtime, next_expire))
 
1804
        {
 
1805
          KBNODE node;
 
1806
 
 
1807
          if (pk->expiredate && pk->expiredate >= curtime
 
1808
              && pk->expiredate < *next_expire)
 
1809
            *next_expire = pk->expiredate;
 
1810
 
 
1811
          if (nkeys == maxkeys) {
 
1812
            maxkeys += 1000;
 
1813
            keys = xrealloc (keys, (maxkeys+1) * sizeof *keys);
 
1814
          }
 
1815
          keys[nkeys++].keyblock = keyblock;
 
1816
 
 
1817
          /* Optimization - if all uids are fully trusted, then we
 
1818
             never need to consider this key as a candidate again. */
 
1819
 
 
1820
          for (node=keyblock; node; node = node->next)
 
1821
            if (node->pkt->pkttype == PKT_USER_ID && !(node->flag & 4))
 
1822
              break;
 
1823
 
 
1824
          if(node==NULL)
 
1825
            mark_keyblock_seen (full_trust, keyblock);
 
1826
 
 
1827
          keyblock = NULL;
 
1828
        }
 
1829
 
 
1830
      release_kbnode (keyblock);
 
1831
      keyblock = NULL;
 
1832
    } 
 
1833
  while ( !(rc = keydb_search (hd, &desc, 1)) );
 
1834
  if (rc && rc != -1) 
 
1835
    {
 
1836
      log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
 
1837
      xfree (keys);
 
1838
      return NULL;
 
1839
    }
 
1840
 
 
1841
  keys[nkeys].keyblock = NULL;
 
1842
  return keys;
 
1843
 
1844
 
 
1845
/* Caller must sync */
 
1846
static void
 
1847
reset_trust_records (KEYDB_HANDLE hd, KeyHashTable exclude)
 
1848
{
 
1849
  int rc;
 
1850
  KBNODE keyblock = NULL;
 
1851
  KEYDB_SEARCH_DESC desc;
 
1852
  int count = 0, nreset = 0;
 
1853
  
 
1854
  rc = keydb_search_reset (hd);
 
1855
  if (rc)
 
1856
    {
 
1857
      log_error ("keydb_search_reset failed: %s\n", gpg_strerror (rc));
 
1858
      return;
 
1859
    }
 
1860
 
 
1861
  memset (&desc, 0, sizeof desc);
 
1862
  desc.mode = KEYDB_SEARCH_MODE_FIRST;
 
1863
  if(exclude)
 
1864
    {
 
1865
      desc.skipfnc = search_skipfnc;
 
1866
      desc.skipfncvalue = exclude;
 
1867
    }
 
1868
  rc = keydb_search (hd, &desc, 1);
 
1869
  if (rc && rc != -1 )
 
1870
    log_error ("keydb_search_first failed: %s\n", gpg_strerror (rc));
 
1871
  else if (!rc)
 
1872
    {
 
1873
      desc.mode = KEYDB_SEARCH_MODE_NEXT; /* change mode */
 
1874
      do
 
1875
        {
 
1876
          rc = keydb_get_keyblock (hd, &keyblock);
 
1877
          if (rc) 
 
1878
            {
 
1879
              log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
 
1880
              break;
 
1881
            }
 
1882
          count++;
 
1883
 
 
1884
          if (keyblock->pkt->pkttype == PKT_PUBLIC_KEY) /* paranoid assertion*/
 
1885
            {
 
1886
              nreset += clear_validity (keyblock->pkt->pkt.public_key);
 
1887
              release_kbnode (keyblock);
 
1888
            } 
 
1889
        }
 
1890
      while ( !(rc = keydb_search (hd, &desc, 1)) );
 
1891
      if (rc && rc != -1) 
 
1892
        log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
 
1893
    }
 
1894
  if (opt.verbose)
 
1895
    log_info (_("%d keys processed (%d validity counts cleared)\n"),
 
1896
              count, nreset);
 
1897
}
 
1898
 
 
1899
/*
 
1900
 * Run the key validation procedure.
 
1901
 *
 
1902
 * This works this way:
 
1903
 * Step 1: Find all ultimately trusted keys (UTK).
 
1904
 *         mark them all as seen and put them into klist.
 
1905
 * Step 2: loop max_cert_times
 
1906
 * Step 3:   if OWNERTRUST of any key in klist is undefined
 
1907
 *             ask user to assign ownertrust
 
1908
 * Step 4:   Loop over all keys in the keyDB which are not marked seen 
 
1909
 * Step 5:     if key is revoked or expired
 
1910
 *                mark key as seen
 
1911
 *                continue loop at Step 4
 
1912
 * Step 6:     For each user ID of that key signed by a key in klist
 
1913
 *                Calculate validity by counting trusted signatures.
 
1914
 *                Set validity of user ID
 
1915
 * Step 7:     If any signed user ID was found
 
1916
 *                mark key as seen
 
1917
 *             End Loop
 
1918
 * Step 8:   Build a new klist from all fully trusted keys from step 6
 
1919
 *           End Loop
 
1920
 *         Ready  
 
1921
 *
 
1922
 */
 
1923
static int
 
1924
validate_keys (int interactive)
 
1925
{
 
1926
  int rc = 0;
 
1927
  int quit=0;
 
1928
  struct key_item *klist = NULL;
 
1929
  struct key_item *k;
 
1930
  struct key_array *keys = NULL;
 
1931
  struct key_array *kar;
 
1932
  KEYDB_HANDLE kdb = NULL;
 
1933
  KBNODE node;
 
1934
  int depth;
 
1935
  int key_count;
 
1936
  int ot_unknown, ot_undefined, ot_never, ot_marginal, ot_full, ot_ultimate;
 
1937
  KeyHashTable stored,used,full_trust;
 
1938
  u32 start_time, next_expire;
 
1939
 
 
1940
  start_time = make_timestamp ();
 
1941
  next_expire = 0xffffffff; /* set next expire to the year 2106 */
 
1942
  stored = new_key_hash_table ();
 
1943
  used = new_key_hash_table ();
 
1944
  full_trust = new_key_hash_table ();
 
1945
  /* Fixme: Instead of always building a UTK list, we could just build it
 
1946
   * here when needed */
 
1947
  if (!utk_list)
 
1948
    {
 
1949
      log_info (_("no ultimately trusted keys found\n"));
 
1950
      goto leave;
 
1951
    }
 
1952
 
 
1953
  kdb = keydb_new (0);
 
1954
 
 
1955
  reset_trust_records (kdb,NULL);
 
1956
 
 
1957
  /* mark all UTKs as used and fully_trusted and set validity to
 
1958
     ultimate */
 
1959
  for (k=utk_list; k; k = k->next)
 
1960
    {
 
1961
      KBNODE keyblock;
 
1962
      PKT_public_key *pk;
 
1963
 
 
1964
      keyblock = get_pubkeyblock (k->kid);
 
1965
      if (!keyblock)
 
1966
        {
 
1967
          log_error (_("public key of ultimately"
 
1968
                       " trusted key %08lX not found\n"), (ulong)k->kid[1]);
 
1969
          continue;
 
1970
        }
 
1971
      mark_keyblock_seen (used, keyblock);
 
1972
      mark_keyblock_seen (stored, keyblock);
 
1973
      mark_keyblock_seen (full_trust, keyblock);
 
1974
      pk = keyblock->pkt->pkt.public_key;
 
1975
      for (node=keyblock; node; node = node->next)
 
1976
        {
 
1977
          if (node->pkt->pkttype == PKT_USER_ID)
 
1978
            update_validity (pk, node->pkt->pkt.user_id, 0, TRUST_ULTIMATE);
 
1979
        }
 
1980
      if ( pk->expiredate && pk->expiredate >= start_time
 
1981
           && pk->expiredate < next_expire)
 
1982
        next_expire = pk->expiredate;
 
1983
      
 
1984
      release_kbnode (keyblock);
 
1985
      do_sync ();
 
1986
    }
 
1987
 
 
1988
  klist = utk_list;
 
1989
 
 
1990
  log_info(_("%d marginal(s) needed, %d complete(s) needed, %s trust model\n"),
 
1991
           opt.marginals_needed,opt.completes_needed,trust_model_string());
 
1992
 
 
1993
  for (depth=0; depth < opt.max_cert_depth; depth++)
 
1994
    {
 
1995
      /* See whether we should assign ownertrust values to the keys in
 
1996
         utk_list.  */
 
1997
      ot_unknown = ot_undefined = ot_never = 0;
 
1998
      ot_marginal = ot_full = ot_ultimate = 0;
 
1999
      for (k=klist; k; k = k->next)
 
2000
        {
 
2001
          int min=0;
 
2002
 
 
2003
          /* 120 and 60 are as per RFC2440 */
 
2004
          if(k->trust_value>=120)
 
2005
            min=TRUST_FULLY;
 
2006
          else if(k->trust_value>=60)
 
2007
            min=TRUST_MARGINAL;
 
2008
 
 
2009
          if(min!=k->min_ownertrust)
 
2010
            update_min_ownertrust(k->kid,min);
 
2011
 
 
2012
          if (interactive && k->ownertrust == TRUST_UNKNOWN)
 
2013
            {
 
2014
              k->ownertrust = ask_ownertrust (k->kid,min);
 
2015
 
 
2016
              if (k->ownertrust == -1)
 
2017
                {
 
2018
                  quit=1;
 
2019
                  goto leave;
 
2020
                }
 
2021
            }
 
2022
 
 
2023
          /* This can happen during transition from an old trustdb
 
2024
             before trust sigs.  It can also happen if a user uses two
 
2025
             different versions of GnuPG or changes the --trust-model
 
2026
             setting. */
 
2027
          if(k->ownertrust<min)
 
2028
            {
 
2029
              if(DBG_TRUST)
 
2030
                log_debug("key %08lX: "
 
2031
                          "overriding ownertrust \"%s\" with \"%s\"\n",
 
2032
                          (ulong)k->kid[1],
 
2033
                          trust_value_to_string(k->ownertrust),
 
2034
                          trust_value_to_string(min));
 
2035
 
 
2036
              k->ownertrust=min;
 
2037
            }
 
2038
 
 
2039
          if (k->ownertrust == TRUST_UNKNOWN)
 
2040
            ot_unknown++;
 
2041
          else if (k->ownertrust == TRUST_UNDEFINED)
 
2042
            ot_undefined++;
 
2043
          else if (k->ownertrust == TRUST_NEVER)
 
2044
            ot_never++;
 
2045
          else if (k->ownertrust == TRUST_MARGINAL)
 
2046
            ot_marginal++;
 
2047
          else if (k->ownertrust == TRUST_FULLY)
 
2048
            ot_full++;
 
2049
          else if (k->ownertrust == TRUST_ULTIMATE)
 
2050
            ot_ultimate++;
 
2051
        }
 
2052
 
 
2053
      /* Find all keys which are signed by a key in kdlist */
 
2054
      keys = validate_key_list (kdb, full_trust, klist,
 
2055
                                start_time, &next_expire);
 
2056
      if (!keys) 
 
2057
        {
 
2058
          log_error ("validate_key_list failed\n");
 
2059
          rc = GPG_ERR_GENERAL;
 
2060
          goto leave;
 
2061
        }
 
2062
 
 
2063
      for (key_count=0, kar=keys; kar->keyblock; kar++, key_count++)
 
2064
        ;
 
2065
 
 
2066
      /* Store the calculated valididation status somewhere */
 
2067
      if (opt.verbose > 1)
 
2068
        dump_key_array (depth, keys);
 
2069
 
 
2070
      for (kar=keys; kar->keyblock; kar++)
 
2071
          store_validation_status (depth, kar->keyblock, stored);
 
2072
 
 
2073
      log_info (_("checking at depth %d valid=%d"
 
2074
                  " ot(-/q/n/m/f/u)=%d/%d/%d/%d/%d/%d\n"), 
 
2075
                depth, key_count, ot_unknown, ot_undefined,
 
2076
                ot_never, ot_marginal, ot_full, ot_ultimate ); 
 
2077
 
 
2078
      /* Build a new kdlist from all fully valid keys in KEYS */
 
2079
      if (klist != utk_list)
 
2080
        release_key_items (klist);
 
2081
      klist = NULL;
 
2082
      for (kar=keys; kar->keyblock; kar++)
 
2083
        {
 
2084
          for (node=kar->keyblock; node; node = node->next)
 
2085
            {
 
2086
              if (node->pkt->pkttype == PKT_USER_ID && (node->flag & 4))
 
2087
                {
 
2088
                  u32 kid[2];
 
2089
 
 
2090
                  /* have we used this key already? */
 
2091
                  keyid_from_pk (kar->keyblock->pkt->pkt.public_key, kid);
 
2092
                  if(test_key_hash_table(used,kid)==0)
 
2093
                    {
 
2094
                      /* Normally we add both the primary and subkey
 
2095
                         ids to the hash via mark_keyblock_seen, but
 
2096
                         since we aren't using this hash as a skipfnc,
 
2097
                         that doesn't matter here. */
 
2098
                      add_key_hash_table (used,kid);
 
2099
                      k = new_key_item ();
 
2100
                      k->kid[0]=kid[0];
 
2101
                      k->kid[1]=kid[1];
 
2102
                      k->ownertrust =
 
2103
                        (get_ownertrust (kar->keyblock->pkt->pkt.public_key)
 
2104
                         & TRUST_MASK);
 
2105
                      k->min_ownertrust =
 
2106
                        get_min_ownertrust(kar->keyblock->pkt->pkt.public_key);
 
2107
                      k->trust_depth=
 
2108
                        kar->keyblock->pkt->pkt.public_key->trust_depth;
 
2109
                      k->trust_value=
 
2110
                        kar->keyblock->pkt->pkt.public_key->trust_value;
 
2111
                      if(kar->keyblock->pkt->pkt.public_key->trust_regexp)
 
2112
                        k->trust_regexp=
 
2113
                          xstrdup (kar->keyblock->pkt->
 
2114
                                   pkt.public_key->trust_regexp);
 
2115
                      k->next = klist;
 
2116
                      klist = k;
 
2117
                      break;
 
2118
                    }
 
2119
                }
 
2120
            }
 
2121
        }
 
2122
      release_key_array (keys);
 
2123
      keys = NULL;
 
2124
      if (!klist)
 
2125
        break; /* no need to dive in deeper */
 
2126
    }
 
2127
 
 
2128
 leave:
 
2129
  keydb_release (kdb);
 
2130
  release_key_array (keys);
 
2131
  release_key_items (klist);
 
2132
  release_key_hash_table (full_trust);
 
2133
  release_key_hash_table (used);
 
2134
  release_key_hash_table (stored);
 
2135
  if (!rc && !quit) /* mark trustDB as checked */
 
2136
    {
 
2137
      if (next_expire == 0xffffffff || next_expire < start_time )
 
2138
        tdbio_write_nextcheck (0); 
 
2139
      else
 
2140
        {
 
2141
          tdbio_write_nextcheck (next_expire); 
 
2142
          log_info (_("next trustdb check due at %s\n"),
 
2143
                    strtimestamp (next_expire));
 
2144
        }
 
2145
 
 
2146
      if(tdbio_update_version_record()!=0)
 
2147
        {
 
2148
          log_error(_("unable to update trustdb version record: "
 
2149
                      "write failed: %s\n"), gpg_strerror (rc));
 
2150
          tdbio_invalid();
 
2151
        }
 
2152
 
 
2153
      do_sync ();
 
2154
      pending_check_trustdb = 0;
 
2155
    }
 
2156
 
 
2157
  return rc;
 
2158
}