~ubuntu-branches/ubuntu/dapper/gnupg2/dapper

« back to all changes in this revision

Viewing changes to g10/tdbio.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
/* tdbio.c
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <assert.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <fcntl.h>
 
30
#include <unistd.h>
 
31
 
 
32
#include "gpg.h"
 
33
#include "errors.h"
 
34
#include "iobuf.h"
 
35
#include "memory.h"
 
36
#include "util.h"
 
37
#include "options.h"
 
38
#include "main.h"
 
39
#include "i18n.h"
 
40
#include "trustdb.h"
 
41
#include "tdbio.h"
 
42
 
 
43
#if defined(HAVE_DOSISH_SYSTEM)
 
44
#define ftruncate chsize
 
45
#endif
 
46
 
 
47
#if defined(HAVE_DOSISH_SYSTEM) || defined(__CYGWIN__)
 
48
#define MY_O_BINARY  O_BINARY
 
49
#else
 
50
#define MY_O_BINARY  0
 
51
#endif
 
52
 
 
53
 
 
54
/****************
 
55
 * Yes, this is a very simple implementation. We should really
 
56
 * use a page aligned buffer and read complete pages.
 
57
 * To implement a simple trannsaction system, this is sufficient.
 
58
 */
 
59
typedef struct cache_ctrl_struct *CACHE_CTRL;
 
60
struct cache_ctrl_struct {
 
61
    CACHE_CTRL next;
 
62
    struct {
 
63
        unsigned used:1;
 
64
        unsigned dirty:1;
 
65
    } flags;
 
66
    ulong recno;
 
67
    char data[TRUST_RECORD_LEN];
 
68
};
 
69
 
 
70
#define MAX_CACHE_ENTRIES_SOFT  200    /* may be increased while in a */
 
71
#define MAX_CACHE_ENTRIES_HARD  10000  /* transaction to this one */
 
72
static CACHE_CTRL cache_list;
 
73
static int cache_entries;
 
74
static int cache_is_dirty;
 
75
 
 
76
/* a type used to pass infomation to cmp_krec_fpr */
 
77
struct cmp_krec_fpr_struct {
 
78
    int pubkey_algo;
 
79
    const char *fpr;
 
80
    int fprlen;
 
81
};
 
82
 
 
83
/* a type used to pass infomation to cmp_[s]dir */
 
84
struct cmp_xdir_struct {
 
85
    int pubkey_algo;
 
86
    u32 keyid[2];
 
87
};
 
88
 
 
89
 
 
90
static char *db_name;
 
91
static DOTLOCK lockhandle;
 
92
static int is_locked;
 
93
static int  db_fd = -1;
 
94
static int in_transaction;
 
95
 
 
96
static void open_db(void);
 
97
static void migrate_from_v2 (void);
 
98
 
 
99
 
 
100
 
 
101
/*************************************
 
102
 ************* record cache **********
 
103
 *************************************/
 
104
 
 
105
/****************
 
106
 * Get the data from therecord cache and return a
 
107
 * pointer into that cache.  Caller should copy
 
108
 * the return data.  NULL is returned on a cache miss.
 
109
 */
 
110
static const char *
 
111
get_record_from_cache( ulong recno )
 
112
{
 
113
    CACHE_CTRL r;
 
114
 
 
115
    for( r = cache_list; r; r = r->next ) {
 
116
        if( r->flags.used && r->recno == recno )
 
117
            return r->data;
 
118
    }
 
119
    return NULL;
 
120
}
 
121
 
 
122
 
 
123
static int
 
124
write_cache_item( CACHE_CTRL r )
 
125
{
 
126
    int n;
 
127
    gpg_error_t rc;
 
128
 
 
129
    if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
 
130
        rc = gpg_error_from_errno (errno);
 
131
        log_error(_("trustdb rec %lu: lseek failed: %s\n"),
 
132
                                            r->recno, strerror(errno) );
 
133
        return rc;
 
134
    }
 
135
    n = write( db_fd, r->data, TRUST_RECORD_LEN);
 
136
    if( n != TRUST_RECORD_LEN ) {
 
137
        rc = gpg_error_from_errno (errno);
 
138
        log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
 
139
                                            r->recno, n, strerror(errno) );
 
140
        return rc;
 
141
    }
 
142
    r->flags.dirty = 0;
 
143
    return 0;
 
144
}
 
145
 
 
146
/****************
 
147
 * Put data into the cache.  This function may flush the
 
148
 * some cache entries if there is not enough space available.
 
149
 */
 
150
int
 
151
put_record_into_cache( ulong recno, const char *data )
 
152
{
 
153
    CACHE_CTRL r, unused;
 
154
    int dirty_count = 0;
 
155
    int clean_count = 0;
 
156
 
 
157
    /* see whether we already cached this one */
 
158
    for( unused = NULL, r = cache_list; r; r = r->next ) {
 
159
        if( !r->flags.used ) {
 
160
            if( !unused )
 
161
                unused = r;
 
162
        }
 
163
        else if( r->recno == recno ) {
 
164
            if( !r->flags.dirty ) {
 
165
                /* Hmmm: should we use a a copy and compare? */
 
166
                if( memcmp(r->data, data, TRUST_RECORD_LEN ) ) {
 
167
                    r->flags.dirty = 1;
 
168
                    cache_is_dirty = 1;
 
169
                }
 
170
            }
 
171
            memcpy( r->data, data, TRUST_RECORD_LEN );
 
172
            return 0;
 
173
        }
 
174
        if( r->flags.used ) {
 
175
            if( r->flags.dirty )
 
176
                dirty_count++;
 
177
            else
 
178
                clean_count++;
 
179
        }
 
180
    }
 
181
    /* not in the cache: add a new entry */
 
182
    if( unused ) { /* reuse this entry */
 
183
        r = unused;
 
184
        r->flags.used = 1;
 
185
        r->recno = recno;
 
186
        memcpy( r->data, data, TRUST_RECORD_LEN );
 
187
        r->flags.dirty = 1;
 
188
        cache_is_dirty = 1;
 
189
        cache_entries++;
 
190
        return 0;
 
191
    }
 
192
    /* see whether we reached the limit */
 
193
    if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */
 
194
        r = xmalloc ( sizeof *r );
 
195
        r->flags.used = 1;
 
196
        r->recno = recno;
 
197
        memcpy( r->data, data, TRUST_RECORD_LEN );
 
198
        r->flags.dirty = 1;
 
199
        r->next = cache_list;
 
200
        cache_list = r;
 
201
        cache_is_dirty = 1;
 
202
        cache_entries++;
 
203
        return 0;
 
204
    }
 
205
    /* cache is full: discard some clean entries */
 
206
    if( clean_count ) {
 
207
        int n = clean_count / 3; /* discard a third of the clean entries */
 
208
        if( !n )
 
209
            n = 1;
 
210
        for( unused = NULL, r = cache_list; r; r = r->next ) {
 
211
            if( r->flags.used && !r->flags.dirty ) {
 
212
                if( !unused )
 
213
                    unused = r;
 
214
                r->flags.used = 0;
 
215
                cache_entries--;
 
216
                if( !--n )
 
217
                    break;
 
218
            }
 
219
        }
 
220
        assert( unused );
 
221
        r = unused;
 
222
        r->flags.used = 1;
 
223
        r->recno = recno;
 
224
        memcpy( r->data, data, TRUST_RECORD_LEN );
 
225
        r->flags.dirty = 1;
 
226
        cache_is_dirty = 1;
 
227
        cache_entries++;
 
228
        return 0;
 
229
    }
 
230
    /* no clean entries: have to flush some dirty entries */
 
231
    if( in_transaction ) {
 
232
        /* but we can't do this while in a transaction
 
233
         * we increase the cache size instead */
 
234
        if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */
 
235
            if( opt.debug && !(cache_entries % 100) )
 
236
                log_debug("increasing tdbio cache size\n");
 
237
            r = xmalloc ( sizeof *r );
 
238
            r->flags.used = 1;
 
239
            r->recno = recno;
 
240
            memcpy( r->data, data, TRUST_RECORD_LEN );
 
241
            r->flags.dirty = 1;
 
242
            r->next = cache_list;
 
243
            cache_list = r;
 
244
            cache_is_dirty = 1;
 
245
            cache_entries++;
 
246
            return 0;
 
247
        }
 
248
        log_info(_("trustdb transaction too large\n"));
 
249
        return GPG_ERR_RESOURCE_LIMIT;
 
250
    }
 
251
    if( dirty_count ) {
 
252
        int n = dirty_count / 5; /* discard some dirty entries */
 
253
        if( !n )
 
254
            n = 1;
 
255
        if( !is_locked ) {
 
256
            if( make_dotlock( lockhandle, -1 ) )
 
257
                log_fatal("can't acquire lock - giving up\n");
 
258
            else
 
259
                is_locked = 1;
 
260
        }
 
261
        for( unused = NULL, r = cache_list; r; r = r->next ) {
 
262
            if( r->flags.used && r->flags.dirty ) {
 
263
                int rc = write_cache_item( r );
 
264
                if( rc )
 
265
                    return rc;
 
266
                if( !unused )
 
267
                    unused = r;
 
268
                r->flags.used = 0;
 
269
                cache_entries--;
 
270
                if( !--n )
 
271
                    break;
 
272
            }
 
273
        }
 
274
        if( !opt.lock_once ) {
 
275
            if( !release_dotlock( lockhandle ) )
 
276
                is_locked = 0;
 
277
        }
 
278
        assert( unused );
 
279
        r = unused;
 
280
        r->flags.used = 1;
 
281
        r->recno = recno;
 
282
        memcpy( r->data, data, TRUST_RECORD_LEN );
 
283
        r->flags.dirty = 1;
 
284
        cache_is_dirty = 1;
 
285
        cache_entries++;
 
286
        return 0;
 
287
    }
 
288
    BUG();
 
289
}
 
290
 
 
291
 
 
292
int
 
293
tdbio_is_dirty()
 
294
{
 
295
    return cache_is_dirty;
 
296
}
 
297
 
 
298
 
 
299
/****************
 
300
 * Flush the cache.  This cannot be used while in a transaction.
 
301
 */
 
302
int
 
303
tdbio_sync()
 
304
{
 
305
    CACHE_CTRL r;
 
306
    int did_lock = 0;
 
307
 
 
308
    if( db_fd == -1 )
 
309
        open_db();
 
310
    if( in_transaction )
 
311
        log_bug("tdbio: syncing while in transaction\n");
 
312
 
 
313
    if( !cache_is_dirty )
 
314
        return 0;
 
315
 
 
316
    if( !is_locked ) {
 
317
        if( make_dotlock( lockhandle, -1 ) )
 
318
            log_fatal("can't acquire lock - giving up\n");
 
319
        else
 
320
            is_locked = 1;
 
321
        did_lock = 1;
 
322
    }
 
323
    for( r = cache_list; r; r = r->next ) {
 
324
        if( r->flags.used && r->flags.dirty ) {
 
325
            int rc = write_cache_item( r );
 
326
            if( rc )
 
327
                return rc;
 
328
        }
 
329
    }
 
330
    cache_is_dirty = 0;
 
331
    if( did_lock && !opt.lock_once ) {
 
332
        if( !release_dotlock( lockhandle ) )
 
333
            is_locked = 0;
 
334
    }
 
335
 
 
336
    return 0;
 
337
}
 
338
 
 
339
 
 
340
#if 0
 
341
/* The transaction code is disabled in the 1.2.x branch, as it is not
 
342
   yet used.  It will be enabled in 1.3.x. */
 
343
 
 
344
/****************
 
345
 * Simple transactions system:
 
346
 * Everything between begin_transaction and end/cancel_transaction
 
347
 * is not immediatly written but at the time of end_transaction.
 
348
 *
 
349
 */
 
350
int
 
351
tdbio_begin_transaction()
 
352
{
 
353
    int rc;
 
354
 
 
355
    if( in_transaction )
 
356
        log_bug("tdbio: nested transactions\n");
 
357
    /* flush everything out */
 
358
    rc = tdbio_sync();
 
359
    if( rc )
 
360
        return rc;
 
361
    in_transaction = 1;
 
362
    return 0;
 
363
}
 
364
 
 
365
int
 
366
tdbio_end_transaction()
 
367
{
 
368
    int rc;
 
369
 
 
370
    if( !in_transaction )
 
371
        log_bug("tdbio: no active transaction\n");
 
372
    if( !is_locked ) {
 
373
        if( make_dotlock( lockhandle, -1 ) )
 
374
            log_fatal("can't acquire lock - giving up\n");
 
375
        else
 
376
            is_locked = 1;
 
377
    }
 
378
#warning block_all_signals is not yet available in ../common/signals.c
 
379
    /*    block_all_signals(); */
 
380
    in_transaction = 0;
 
381
    rc = tdbio_sync();
 
382
/*      unblock_all_signals(); */
 
383
    if( !opt.lock_once ) {
 
384
        if( !release_dotlock( lockhandle ) )
 
385
            is_locked = 0;
 
386
    }
 
387
    return rc;
 
388
}
 
389
 
 
390
int
 
391
tdbio_cancel_transaction()
 
392
{
 
393
    CACHE_CTRL r;
 
394
 
 
395
    if( !in_transaction )
 
396
        log_bug("tdbio: no active transaction\n");
 
397
 
 
398
    /* remove all dirty marked entries, so that the original ones
 
399
     * are read back the next time */
 
400
    if( cache_is_dirty ) {
 
401
        for( r = cache_list; r; r = r->next ) {
 
402
            if( r->flags.used && r->flags.dirty ) {
 
403
                r->flags.used = 0;
 
404
                cache_entries--;
 
405
            }
 
406
        }
 
407
        cache_is_dirty = 0;
 
408
    }
 
409
 
 
410
    in_transaction = 0;
 
411
    return 0;
 
412
}
 
413
 
 
414
#endif /* transaction code */
 
415
 
 
416
 
 
417
 
 
418
/********************************************************
 
419
 **************** cached I/O functions ******************
 
420
 ********************************************************/
 
421
 
 
422
static void
 
423
cleanup(void)
 
424
{
 
425
    if( is_locked ) {
 
426
        if( !release_dotlock(lockhandle) )
 
427
            is_locked = 0;
 
428
    }
 
429
}
 
430
 
 
431
/* Caller must sync */
 
432
int
 
433
tdbio_update_version_record (void)
 
434
{
 
435
  TRUSTREC rec;
 
436
  int rc;
 
437
 
 
438
  memset( &rec, 0, sizeof rec );
 
439
 
 
440
  rc=tdbio_read_record( 0, &rec, RECTYPE_VER);
 
441
  if(rc==0)
 
442
    {
 
443
      rec.r.ver.created     = make_timestamp();
 
444
      rec.r.ver.marginals   = opt.marginals_needed;
 
445
      rec.r.ver.completes   = opt.completes_needed;
 
446
      rec.r.ver.cert_depth  = opt.max_cert_depth;
 
447
      rec.r.ver.trust_model = opt.trust_model;
 
448
      rc=tdbio_write_record(&rec);
 
449
    }
 
450
 
 
451
  return rc;
 
452
}
 
453
 
 
454
static int
 
455
create_version_record (void)
 
456
{
 
457
  TRUSTREC rec;
 
458
  int rc;
 
459
  
 
460
  memset( &rec, 0, sizeof rec );
 
461
  rec.r.ver.version     = 3;
 
462
  rec.r.ver.created     = make_timestamp();
 
463
  rec.r.ver.marginals   = opt.marginals_needed;
 
464
  rec.r.ver.completes   = opt.completes_needed;
 
465
  rec.r.ver.cert_depth  = opt.max_cert_depth;
 
466
  if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
 
467
    rec.r.ver.trust_model = opt.trust_model;
 
468
  else
 
469
    rec.r.ver.trust_model = TM_PGP;
 
470
  rec.rectype = RECTYPE_VER;
 
471
  rec.recnum = 0;
 
472
  rc = tdbio_write_record( &rec );
 
473
  if( !rc )
 
474
    tdbio_sync();
 
475
  return rc;
 
476
}
 
477
 
 
478
 
 
479
 
 
480
int
 
481
tdbio_set_dbname( const char *new_dbname, int create )
 
482
{
 
483
    char *fname;
 
484
    static int initialized = 0;
 
485
 
 
486
    if( !initialized ) {
 
487
        atexit( cleanup );
 
488
        initialized = 1;
 
489
    }
 
490
 
 
491
    if(new_dbname==NULL)
 
492
      fname=make_filename(opt.homedir,"trustdb" EXTSEP_S "gpg", NULL);
 
493
    else if (*new_dbname != DIRSEP_C )
 
494
      {
 
495
        if (strchr(new_dbname, DIRSEP_C) )
 
496
          fname = make_filename (new_dbname, NULL);
 
497
        else
 
498
          fname = make_filename (opt.homedir, new_dbname, NULL);
 
499
      }
 
500
    else
 
501
      fname = xstrdup (new_dbname);
 
502
 
 
503
    if( access( fname, R_OK ) ) {
 
504
        if( errno != ENOENT ) {
 
505
            log_error( _("%s: can't access: %s\n"), fname, strerror(errno) );
 
506
            xfree (fname);
 
507
            return GPG_ERR_TRUSTDB;
 
508
        }
 
509
        if( create ) {
 
510
            FILE *fp;
 
511
            TRUSTREC rec;
 
512
            int rc;
 
513
            char *p = strrchr( fname, DIRSEP_C );
 
514
            mode_t oldmask;
 
515
 
 
516
            assert(p);
 
517
            *p = 0;
 
518
            if( access( fname, F_OK ) ) {
 
519
                try_make_homedir( fname );
 
520
                log_fatal( _("%s: directory does not exist!\n"), fname );
 
521
            }
 
522
            *p = DIRSEP_C;
 
523
 
 
524
            xfree (db_name);
 
525
            db_name = fname;
 
526
#ifdef __riscos__
 
527
            if( !lockhandle )
 
528
                lockhandle = create_dotlock( db_name );
 
529
            if( !lockhandle )
 
530
                log_fatal( _("%s: can't create lock\n"), db_name );
 
531
            if( make_dotlock( lockhandle, -1 ) )
 
532
                log_fatal( _("%s: can't make lock\n"), db_name );
 
533
#endif /* __riscos__ */
 
534
            oldmask=umask(077);
 
535
            fp =fopen( fname, "wb" );
 
536
            umask(oldmask);
 
537
            if( !fp )
 
538
                log_fatal( _("%s: can't create: %s\n"), fname, strerror(errno) );
 
539
            fclose(fp);
 
540
            db_fd = open( db_name, O_RDWR | MY_O_BINARY );
 
541
            if( db_fd == -1 )
 
542
                log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
 
543
 
 
544
#ifndef __riscos__
 
545
            if( !lockhandle )
 
546
                lockhandle = create_dotlock( db_name );
 
547
            if( !lockhandle )
 
548
                log_fatal( _("%s: can't create lock\n"), db_name );
 
549
#endif /* !__riscos__ */
 
550
 
 
551
            rc = create_version_record ();
 
552
            if( rc )
 
553
                log_fatal( _("%s: failed to create version record: %s"),
 
554
                                                   fname, gpg_strerror (rc));
 
555
            /* and read again to check that we are okay */
 
556
            if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
 
557
                log_fatal( _("%s: invalid trustdb created\n"), db_name );
 
558
 
 
559
            if( !opt.quiet )
 
560
                log_info(_("%s: trustdb created\n"), db_name);
 
561
 
 
562
            return 0;
 
563
        }
 
564
    }
 
565
    xfree (db_name);
 
566
    db_name = fname;
 
567
    return 0;
 
568
}
 
569
 
 
570
 
 
571
const char *
 
572
tdbio_get_dbname()
 
573
{
 
574
    return db_name;
 
575
}
 
576
 
 
577
 
 
578
 
 
579
static void
 
580
open_db()
 
581
{
 
582
  byte buf[10];
 
583
  int n;
 
584
  TRUSTREC rec;
 
585
 
 
586
  assert( db_fd == -1 );
 
587
 
 
588
  if (!lockhandle )
 
589
    lockhandle = create_dotlock( db_name );
 
590
  if (!lockhandle )
 
591
    log_fatal( _("%s: can't create lock\n"), db_name );
 
592
#ifdef __riscos__
 
593
  if (make_dotlock( lockhandle, -1 ) )
 
594
    log_fatal( _("%s: can't make lock\n"), db_name );
 
595
#endif /* __riscos__ */
 
596
  db_fd = open (db_name, O_RDWR | MY_O_BINARY );
 
597
  if (db_fd == -1 && errno == EACCES) {
 
598
      db_fd = open (db_name, O_RDONLY | MY_O_BINARY );
 
599
      if (db_fd != -1)
 
600
          log_info (_("NOTE: trustdb not writable\n"));
 
601
  }
 
602
  if ( db_fd == -1 )
 
603
    log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
 
604
 
 
605
  /* check whether we need to do a version migration */
 
606
  do
 
607
    n = read (db_fd, buf, 5);
 
608
  while (n==-1 && errno == EINTR);
 
609
  if (n == 5 && !memcmp (buf, "\x01gpg\x02", 5))
 
610
    {
 
611
      migrate_from_v2 ();
 
612
    }
 
613
  
 
614
  /* read the version record */
 
615
  if (tdbio_read_record (0, &rec, RECTYPE_VER ) )
 
616
    log_fatal( _("%s: invalid trustdb\n"), db_name );
 
617
}
 
618
 
 
619
 
 
620
/****************
 
621
 * Make a hashtable: type 0 = trust hash
 
622
 */
 
623
static void
 
624
create_hashtable( TRUSTREC *vr, int type )
 
625
{
 
626
    TRUSTREC rec;
 
627
    off_t offset;
 
628
    ulong recnum;
 
629
    int i, n, rc;
 
630
 
 
631
    offset = lseek( db_fd, 0, SEEK_END );
 
632
    if( offset == -1 )
 
633
        log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
 
634
    recnum = offset / TRUST_RECORD_LEN;
 
635
    assert(recnum); /* this is will never be the first record */
 
636
 
 
637
    if( !type )
 
638
        vr->r.ver.trusthashtbl = recnum;
 
639
 
 
640
    /* Now write the records */
 
641
    n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
 
642
    for(i=0; i < n; i++, recnum++ ) {
 
643
         memset( &rec, 0, sizeof rec );
 
644
         rec.rectype = RECTYPE_HTBL;
 
645
         rec.recnum = recnum;
 
646
         rc = tdbio_write_record( &rec );
 
647
         if( rc )
 
648
             log_fatal( _("%s: failed to create hashtable: %s\n"),
 
649
                                        db_name, gpg_strerror (rc));
 
650
    }
 
651
    /* update the version record */
 
652
    rc = tdbio_write_record( vr );
 
653
    if( !rc )
 
654
        rc = tdbio_sync();
 
655
    if( rc )
 
656
        log_fatal( _("%s: error updating version record: %s\n"),
 
657
                                                  db_name, gpg_strerror (rc));
 
658
}
 
659
 
 
660
 
 
661
int
 
662
tdbio_db_matches_options()
 
663
{
 
664
  static int yes_no = -1;
 
665
 
 
666
  if( yes_no == -1 )
 
667
    {
 
668
      TRUSTREC vr;
 
669
      int rc;
 
670
 
 
671
      rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
672
      if( rc )
 
673
        log_fatal( _("%s: error reading version record: %s\n"),
 
674
                   db_name, gpg_strerror (rc) );
 
675
 
 
676
      yes_no = vr.r.ver.marginals == opt.marginals_needed
 
677
        && vr.r.ver.completes == opt.completes_needed
 
678
        && vr.r.ver.cert_depth == opt.max_cert_depth
 
679
        && vr.r.ver.trust_model == opt.trust_model;
 
680
    }
 
681
 
 
682
  return yes_no;
 
683
}
 
684
 
 
685
byte
 
686
tdbio_read_model(void)
 
687
{
 
688
  TRUSTREC vr;
 
689
  int rc;
 
690
 
 
691
  rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
692
  if( rc )
 
693
    log_fatal( _("%s: error reading version record: %s\n"),
 
694
               db_name, gpg_strerror (rc) );
 
695
  return vr.r.ver.trust_model;
 
696
}
 
697
 
 
698
/****************
 
699
 * Return the nextstamp value.
 
700
 */
 
701
ulong
 
702
tdbio_read_nextcheck ()
 
703
{
 
704
    TRUSTREC vr;
 
705
    int rc;
 
706
 
 
707
    rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
708
    if( rc )
 
709
        log_fatal( _("%s: error reading version record: %s\n"),
 
710
                                                    db_name, gpg_strerror (rc) );
 
711
    return vr.r.ver.nextcheck;
 
712
}
 
713
 
 
714
/* Return true when the stamp was actually changed. */
 
715
int
 
716
tdbio_write_nextcheck (ulong stamp)
 
717
{
 
718
    TRUSTREC vr;
 
719
    int rc;
 
720
 
 
721
    rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
722
    if( rc )
 
723
        log_fatal( _("%s: error reading version record: %s\n"),
 
724
                                       db_name, gpg_strerror (rc) );
 
725
 
 
726
    if (vr.r.ver.nextcheck == stamp)
 
727
      return 0;
 
728
 
 
729
    vr.r.ver.nextcheck = stamp;
 
730
    rc = tdbio_write_record( &vr );
 
731
    if( rc )
 
732
        log_fatal( _("%s: error writing version record: %s\n"),
 
733
                                       db_name, gpg_strerror (rc) );
 
734
    return 1;
 
735
}
 
736
 
 
737
 
 
738
 
 
739
/****************
 
740
 * Return the record number of the trusthash tbl or create a new one.
 
741
 */
 
742
static ulong
 
743
get_trusthashrec(void)
 
744
{
 
745
    static ulong trusthashtbl; /* record number of the trust hashtable */
 
746
 
 
747
    if( !trusthashtbl ) {
 
748
        TRUSTREC vr;
 
749
        int rc;
 
750
 
 
751
        rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
752
        if( rc )
 
753
            log_fatal( _("%s: error reading version record: %s\n"),
 
754
                                            db_name, gpg_strerror (rc) );
 
755
        if( !vr.r.ver.trusthashtbl )
 
756
            create_hashtable( &vr, 0 );
 
757
 
 
758
        trusthashtbl = vr.r.ver.trusthashtbl;
 
759
    }
 
760
    return trusthashtbl;
 
761
}
 
762
 
 
763
 
 
764
 
 
765
/****************
 
766
 * Update a hashtable.
 
767
 * table gives the start of the table, key and keylen is the key,
 
768
 * newrecnum is the record number to insert.
 
769
 */
 
770
static int
 
771
upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
 
772
{
 
773
    TRUSTREC lastrec, rec;
 
774
    ulong hashrec, item;
 
775
    int msb;
 
776
    int level=0;
 
777
    int rc, i;
 
778
 
 
779
    hashrec = table;
 
780
  next_level:
 
781
    msb = key[level];
 
782
    hashrec += msb / ITEMS_PER_HTBL_RECORD;
 
783
    rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
 
784
    if( rc ) {
 
785
        log_error ("upd_hashtable in `%s': read failed: %s\n", db_name,
 
786
                   gpg_strerror (rc) );
 
787
        return rc;
 
788
    }
 
789
 
 
790
    item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
 
791
    if( !item ) { /* insert a new item into the hash table */
 
792
        rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
 
793
        rc = tdbio_write_record( &rec );
 
794
        if( rc ) {
 
795
            log_error ("upd_hashtable in `%s': write htbl failed: %s\n",
 
796
                       db_name, gpg_strerror (rc) );
 
797
            return rc;
 
798
        }
 
799
    }
 
800
    else if( item != newrecnum ) {  /* must do an update */
 
801
        lastrec = rec;
 
802
        rc = tdbio_read_record( item, &rec, 0 );
 
803
        if( rc ) {
 
804
            log_error( "upd_hashtable: read item failed: %s\n",
 
805
                                                            gpg_strerror (rc) );
 
806
            return rc;
 
807
        }
 
808
 
 
809
        if( rec.rectype == RECTYPE_HTBL ) {
 
810
            hashrec = item;
 
811
            level++;
 
812
            if( level >= keylen ) {
 
813
                log_error( "hashtable has invalid indirections.\n");
 
814
                return GPG_ERR_TRUSTDB;
 
815
            }
 
816
            goto next_level;
 
817
        }
 
818
        else if( rec.rectype == RECTYPE_HLST ) { /* extend list */
 
819
            /* see whether the key is already in this list */
 
820
            for(;;) {
 
821
                for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
822
                    if( rec.r.hlst.rnum[i] == newrecnum ) {
 
823
                        return 0; /* okay, already in the list */
 
824
                    }
 
825
                }
 
826
                if( rec.r.hlst.next ) {
 
827
                    rc = tdbio_read_record( rec.r.hlst.next,
 
828
                                                       &rec, RECTYPE_HLST);
 
829
                    if( rc ) {
 
830
                        log_error( "upd_hashtable: read hlst failed: %s\n",
 
831
                                                             gpg_strerror (rc) );
 
832
                        return rc;
 
833
                    }
 
834
                }
 
835
                else
 
836
                    break; /* not there */
 
837
            }
 
838
            /* find the next free entry and put it in */
 
839
            for(;;) {
 
840
                for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
841
                    if( !rec.r.hlst.rnum[i] ) {
 
842
                        rec.r.hlst.rnum[i] = newrecnum;
 
843
                        rc = tdbio_write_record( &rec );
 
844
                        if( rc )
 
845
                            log_error( "upd_hashtable: write hlst failed: %s\n",
 
846
                                                              gpg_strerror (rc) );
 
847
                        return rc; /* done */
 
848
                    }
 
849
                }
 
850
                if( rec.r.hlst.next ) {
 
851
                    rc = tdbio_read_record( rec.r.hlst.next,
 
852
                                                      &rec, RECTYPE_HLST );
 
853
                    if( rc ) {
 
854
                        log_error( "upd_hashtable: read hlst failed: %s\n",
 
855
                                                             gpg_strerror (rc) );
 
856
                        return rc;
 
857
                    }
 
858
                }
 
859
                else { /* add a new list record */
 
860
                    rec.r.hlst.next = item = tdbio_new_recnum();
 
861
                    rc = tdbio_write_record( &rec );
 
862
                    if( rc ) {
 
863
                        log_error( "upd_hashtable: write hlst failed: %s\n",
 
864
                                                          gpg_strerror (rc) );
 
865
                        return rc;
 
866
                    }
 
867
                    memset( &rec, 0, sizeof rec );
 
868
                    rec.rectype = RECTYPE_HLST;
 
869
                    rec.recnum = item;
 
870
                    rec.r.hlst.rnum[0] = newrecnum;
 
871
                    rc = tdbio_write_record( &rec );
 
872
                    if( rc )
 
873
                        log_error( "upd_hashtable: write ext hlst failed: %s\n",
 
874
                                                          gpg_strerror (rc) );
 
875
                    return rc; /* done */
 
876
                }
 
877
            } /* end loop over hlst slots */
 
878
        }
 
879
        else if( rec.rectype == RECTYPE_TRUST ) { /* insert a list record */
 
880
            if( rec.recnum == newrecnum ) {
 
881
                return 0;
 
882
            }
 
883
            item = rec.recnum; /* save number of key record */
 
884
            memset( &rec, 0, sizeof rec );
 
885
            rec.rectype = RECTYPE_HLST;
 
886
            rec.recnum = tdbio_new_recnum();
 
887
            rec.r.hlst.rnum[0] = item;       /* old keyrecord */
 
888
            rec.r.hlst.rnum[1] = newrecnum; /* and new one */
 
889
            rc = tdbio_write_record( &rec );
 
890
            if( rc ) {
 
891
                log_error( "upd_hashtable: write new hlst failed: %s\n",
 
892
                                                  gpg_strerror (rc) );
 
893
                return rc;
 
894
            }
 
895
            /* update the hashtable record */
 
896
            lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
 
897
            rc = tdbio_write_record( &lastrec );
 
898
            if( rc )
 
899
                log_error( "upd_hashtable: update htbl failed: %s\n",
 
900
                                                             gpg_strerror (rc) );
 
901
            return rc; /* ready */
 
902
        }
 
903
        else {
 
904
            log_error( "hashtbl %lu: %lu/%d points to an invalid record %lu\n",
 
905
                       table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
 
906
            list_trustdb(NULL);
 
907
            return GPG_ERR_TRUSTDB;
 
908
        }
 
909
    }
 
910
 
 
911
    return 0;
 
912
}
 
913
 
 
914
 
 
915
/****************
 
916
 * Drop an entry from a hashtable
 
917
 * table gives the start of the table, key and keylen is the key,
 
918
 */
 
919
static int
 
920
drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
 
921
{
 
922
    TRUSTREC rec;
 
923
    ulong hashrec, item;
 
924
    int msb;
 
925
    int level=0;
 
926
    int rc, i;
 
927
 
 
928
    hashrec = table;
 
929
  next_level:
 
930
    msb = key[level];
 
931
    hashrec += msb / ITEMS_PER_HTBL_RECORD;
 
932
    rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
 
933
    if( rc ) {
 
934
        log_error ("drop_from_hashtable `%s': read failed: %s\n",
 
935
                   db_name, gpg_strerror (rc) );
 
936
        return rc;
 
937
    }
 
938
 
 
939
    item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
 
940
    if( !item )  /* not found - forget about it  */
 
941
        return 0;
 
942
 
 
943
    if( item == recnum ) {  /* tables points direct to the record */
 
944
        rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
 
945
        rc = tdbio_write_record( &rec );
 
946
        if( rc )
 
947
            log_error ("drop_from_hashtable `%s': write htbl failed: %s\n",
 
948
                       db_name, gpg_strerror (rc) );
 
949
        return rc;
 
950
    }
 
951
 
 
952
    rc = tdbio_read_record( item, &rec, 0 );
 
953
    if( rc ) {
 
954
        log_error( "drop_from_hashtable: read item failed: %s\n",
 
955
                                                        gpg_strerror (rc) );
 
956
        return rc;
 
957
    }
 
958
 
 
959
    if( rec.rectype == RECTYPE_HTBL ) {
 
960
        hashrec = item;
 
961
        level++;
 
962
        if( level >= keylen ) {
 
963
            log_error( "hashtable has invalid indirections.\n");
 
964
            return GPG_ERR_TRUSTDB;
 
965
        }
 
966
        goto next_level;
 
967
    }
 
968
 
 
969
    if( rec.rectype == RECTYPE_HLST ) {
 
970
        for(;;) {
 
971
            for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
972
                if( rec.r.hlst.rnum[i] == recnum ) {
 
973
                    rec.r.hlst.rnum[i] = 0; /* drop */
 
974
                    rc = tdbio_write_record( &rec );
 
975
                    if( rc )
 
976
                        log_error ("drop_from_hashtable `%s': "
 
977
                                   "write htbl failed: %s\n",
 
978
                                   db_name, gpg_strerror (rc) );
 
979
                    return rc;
 
980
                }
 
981
            }
 
982
            if( rec.r.hlst.next ) {
 
983
                rc = tdbio_read_record( rec.r.hlst.next,
 
984
                                                   &rec, RECTYPE_HLST);
 
985
                if( rc ) {
 
986
                    log_error( "drop_from_hashtable: read hlst failed: %s\n",
 
987
                                                         gpg_strerror (rc) );
 
988
                    return rc;
 
989
                }
 
990
            }
 
991
            else
 
992
                return 0; /* key not in table */
 
993
        }
 
994
    }
 
995
 
 
996
    log_error( "hashtbl %lu: %lu/%d points to wrong record %lu\n",
 
997
                    table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
 
998
    return GPG_ERR_TRUSTDB;
 
999
}
 
1000
 
 
1001
 
 
1002
 
 
1003
/****************
 
1004
 * Lookup a record via the hashtable tablewith key/keylen and return the
 
1005
 * result in rec.  cmp() should return if the record is the desired one.
 
1006
 * Returns -1 if not found, 0 if found or another errocode
 
1007
 */
 
1008
static int
 
1009
lookup_hashtable( ulong table, const byte *key, size_t keylen,
 
1010
                  int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
 
1011
                                                TRUSTREC *rec )
 
1012
{
 
1013
    int rc;
 
1014
    ulong hashrec, item;
 
1015
    int msb;
 
1016
    int level=0;
 
1017
 
 
1018
    hashrec = table;
 
1019
  next_level:
 
1020
    msb = key[level];
 
1021
    hashrec += msb / ITEMS_PER_HTBL_RECORD;
 
1022
    rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
 
1023
    if( rc ) {
 
1024
        log_error ("lookup_hashtable in `%s' failed: %s\n",
 
1025
                   db_name, gpg_strerror (rc) );
 
1026
        return rc;
 
1027
    }
 
1028
 
 
1029
    item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
 
1030
    if( !item )
 
1031
        return -1; /* not found */
 
1032
 
 
1033
    rc = tdbio_read_record( item, rec, 0 );
 
1034
    if( rc ) {
 
1035
        log_error ("hashtable `%s' read failed: %s\n",
 
1036
                   db_name, gpg_strerror (rc) );
 
1037
        return rc;
 
1038
    }
 
1039
    if( rec->rectype == RECTYPE_HTBL ) {
 
1040
        hashrec = item;
 
1041
        level++;
 
1042
        if( level >= keylen ) {
 
1043
            log_error ("hashtable `%s' has invalid indirections\n", db_name);
 
1044
            return GPG_ERR_TRUSTDB;
 
1045
        }
 
1046
        goto next_level;
 
1047
    }
 
1048
    else if( rec->rectype == RECTYPE_HLST ) {
 
1049
        for(;;) {
 
1050
            int i;
 
1051
 
 
1052
            for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
1053
                if( rec->r.hlst.rnum[i] ) {
 
1054
                    TRUSTREC tmp;
 
1055
 
 
1056
                    rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
 
1057
                    if( rc ) {
 
1058
                        log_error( "lookup_hashtable: read item failed: %s\n",
 
1059
                                                              gpg_strerror (rc) );
 
1060
                        return rc;
 
1061
                    }
 
1062
                    if( (*cmpfnc)( cmpdata, &tmp ) ) {
 
1063
                        *rec = tmp;
 
1064
                        return 0;
 
1065
                    }
 
1066
                }
 
1067
            }
 
1068
            if( rec->r.hlst.next ) {
 
1069
                rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
 
1070
                if( rc ) {
 
1071
                    log_error( "lookup_hashtable: read hlst failed: %s\n",
 
1072
                                                         gpg_strerror (rc) );
 
1073
                    return rc;
 
1074
                }
 
1075
            }
 
1076
            else
 
1077
                return -1; /* not found */
 
1078
        }
 
1079
    }
 
1080
 
 
1081
 
 
1082
    if( (*cmpfnc)( cmpdata, rec ) )
 
1083
        return 0; /* really found */
 
1084
 
 
1085
    return -1; /* no: not found */
 
1086
}
 
1087
 
 
1088
 
 
1089
/****************
 
1090
 * Update the trust hashtbl or create the table if it does not exist
 
1091
 */
 
1092
static int
 
1093
update_trusthashtbl( TRUSTREC *tr )
 
1094
{
 
1095
    return upd_hashtable( get_trusthashrec(),
 
1096
                          tr->r.trust.fingerprint, 20, tr->recnum );
 
1097
}
 
1098
 
 
1099
 
 
1100
 
 
1101
void
 
1102
tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
 
1103
{
 
1104
    int i;
 
1105
    ulong rnum = rec->recnum;
 
1106
 
 
1107
    fprintf(fp, "rec %5lu, ", rnum );
 
1108
 
 
1109
    switch( rec->rectype ) {
 
1110
      case 0: fprintf(fp, "blank\n");
 
1111
        break;
 
1112
      case RECTYPE_VER: fprintf(fp,
 
1113
            "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d nc=%lu (%s)\n",
 
1114
                                   rec->r.ver.trusthashtbl,
 
1115
                                   rec->r.ver.firstfree,
 
1116
                                   rec->r.ver.marginals,
 
1117
                                   rec->r.ver.completes,
 
1118
                                   rec->r.ver.cert_depth,
 
1119
                                   rec->r.ver.trust_model,
 
1120
                                   rec->r.ver.nextcheck,
 
1121
                                   strtimestamp(rec->r.ver.nextcheck)
 
1122
                                 );
 
1123
        break;
 
1124
      case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
 
1125
        break;
 
1126
      case RECTYPE_HTBL:
 
1127
        fprintf(fp, "htbl,");
 
1128
        for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
 
1129
            fprintf(fp, " %lu", rec->r.htbl.item[i] );
 
1130
        putc('\n', fp);
 
1131
        break;
 
1132
      case RECTYPE_HLST:
 
1133
        fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
 
1134
        for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
 
1135
            fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
 
1136
        putc('\n', fp);
 
1137
        break;
 
1138
      case RECTYPE_TRUST:
 
1139
        fprintf(fp, "trust ");
 
1140
        for(i=0; i < 20; i++ )
 
1141
            fprintf(fp, "%02X", rec->r.trust.fingerprint[i] );
 
1142
        fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust,
 
1143
                 rec->r.trust.depth, rec->r.trust.validlist);
 
1144
        break;
 
1145
      case RECTYPE_VALID:
 
1146
        fprintf(fp, "valid ");
 
1147
        for(i=0; i < 20; i++ )
 
1148
            fprintf(fp, "%02X", rec->r.valid.namehash[i] );
 
1149
        fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity,
 
1150
                 rec->r.valid.next);
 
1151
        break;
 
1152
      default:
 
1153
        fprintf(fp, "unknown type %d\n", rec->rectype );
 
1154
        break;
 
1155
    }
 
1156
}
 
1157
 
 
1158
/****************
 
1159
 * read the record with number recnum
 
1160
 * returns: -1 on error, 0 on success
 
1161
 */
 
1162
int
 
1163
tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
 
1164
{
 
1165
    byte readbuf[TRUST_RECORD_LEN];
 
1166
    const byte *buf, *p;
 
1167
    int rc = 0;
 
1168
    int n, i;
 
1169
 
 
1170
    if( db_fd == -1 )
 
1171
        open_db();
 
1172
    buf = get_record_from_cache( recnum );
 
1173
    if( !buf ) {
 
1174
        if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
 
1175
            rc = gpg_error_from_errno (errno);
 
1176
            log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
 
1177
            return rc;
 
1178
        }
 
1179
        n = read( db_fd, readbuf, TRUST_RECORD_LEN);
 
1180
        if( !n ) {
 
1181
            return -1; /* eof */
 
1182
        }
 
1183
        else if( n != TRUST_RECORD_LEN ) {
 
1184
            rc = gpg_error_from_errno (errno);
 
1185
            log_error(_("trustdb: read failed (n=%d): %s\n"), n,
 
1186
                                                        strerror(errno) );
 
1187
            return rc;
 
1188
        }
 
1189
        buf = readbuf;
 
1190
    }
 
1191
    rec->recnum = recnum;
 
1192
    rec->dirty = 0;
 
1193
    p = buf;
 
1194
    rec->rectype = *p++;
 
1195
    if( expected && rec->rectype != expected ) {
 
1196
        log_error("%lu: read expected rec type %d, got %d\n",
 
1197
                    recnum, expected, rec->rectype );
 
1198
        return GPG_ERR_TRUSTDB;
 
1199
    }
 
1200
    p++;    /* skip reserved byte */
 
1201
    switch( rec->rectype ) {
 
1202
      case 0:  /* unused (free) record */
 
1203
        break;
 
1204
      case RECTYPE_VER: /* version record */
 
1205
        if( memcmp(buf+1, "gpg", 3 ) ) {
 
1206
            log_error( _("%s: not a trustdb file\n"), db_name );
 
1207
            rc = GPG_ERR_TRUSTDB;
 
1208
        }
 
1209
        p += 2; /* skip "gpg" */
 
1210
        rec->r.ver.version  = *p++;
 
1211
        rec->r.ver.marginals = *p++;
 
1212
        rec->r.ver.completes = *p++;
 
1213
        rec->r.ver.cert_depth = *p++;
 
1214
        rec->r.ver.trust_model = *p++;
 
1215
        p += 3;
 
1216
        rec->r.ver.created  = buftoulong(p); p += 4;
 
1217
        rec->r.ver.nextcheck = buftoulong(p); p += 4;
 
1218
        p += 4;
 
1219
        p += 4;
 
1220
        rec->r.ver.firstfree =buftoulong(p); p += 4;
 
1221
        p += 4;
 
1222
        rec->r.ver.trusthashtbl =buftoulong(p); p += 4;
 
1223
        if( recnum ) {
 
1224
            log_error( _("%s: version record with recnum %lu\n"), db_name,
 
1225
                                                             (ulong)recnum );
 
1226
            rc = GPG_ERR_TRUSTDB;
 
1227
        }
 
1228
        else if( rec->r.ver.version != 3 ) {
 
1229
            log_error( _("%s: invalid file version %d\n"), db_name,
 
1230
                                                        rec->r.ver.version );
 
1231
            rc = GPG_ERR_TRUSTDB;
 
1232
        }
 
1233
        break;
 
1234
      case RECTYPE_FREE:
 
1235
        rec->r.free.next  = buftoulong(p); p += 4;
 
1236
        break;
 
1237
      case RECTYPE_HTBL:
 
1238
        for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
 
1239
            rec->r.htbl.item[i] = buftoulong(p); p += 4;
 
1240
        }
 
1241
        break;
 
1242
      case RECTYPE_HLST:
 
1243
        rec->r.hlst.next = buftoulong(p); p += 4;
 
1244
        for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
1245
            rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
 
1246
        }
 
1247
        break;
 
1248
      case RECTYPE_TRUST:
 
1249
        memcpy( rec->r.trust.fingerprint, p, 20); p+=20;
 
1250
        rec->r.trust.ownertrust = *p++;
 
1251
        rec->r.trust.depth = *p++;
 
1252
        rec->r.trust.min_ownertrust = *p++;
 
1253
        p++;
 
1254
        rec->r.trust.validlist = buftoulong(p); p += 4;
 
1255
        break;
 
1256
      case RECTYPE_VALID:
 
1257
        memcpy( rec->r.valid.namehash, p, 20); p+=20;
 
1258
        rec->r.valid.validity = *p++;
 
1259
        rec->r.valid.next = buftoulong(p); p += 4;
 
1260
        rec->r.valid.full_count = *p++;
 
1261
        rec->r.valid.marginal_count = *p++;
 
1262
        break;
 
1263
      default:
 
1264
        log_error( "%s: invalid record type %d at recnum %lu\n",
 
1265
                                   db_name, rec->rectype, (ulong)recnum );
 
1266
        rc = GPG_ERR_TRUSTDB;
 
1267
        break;
 
1268
    }
 
1269
 
 
1270
    return rc;
 
1271
}
 
1272
 
 
1273
/****************
 
1274
 * Write the record at RECNUM
 
1275
 */
 
1276
int
 
1277
tdbio_write_record( TRUSTREC *rec )
 
1278
{
 
1279
    byte buf[TRUST_RECORD_LEN], *p;
 
1280
    int rc = 0;
 
1281
    int i;
 
1282
    ulong recnum = rec->recnum;
 
1283
 
 
1284
    if( db_fd == -1 )
 
1285
        open_db();
 
1286
 
 
1287
    memset(buf, 0, TRUST_RECORD_LEN);
 
1288
    p = buf;
 
1289
    *p++ = rec->rectype; p++;
 
1290
    switch( rec->rectype ) {
 
1291
      case 0:  /* unused record */
 
1292
        break;
 
1293
      case RECTYPE_VER: /* version record */
 
1294
        if( recnum )
 
1295
            BUG();
 
1296
        memcpy(p-1, "gpg", 3 ); p += 2;
 
1297
        *p++ = rec->r.ver.version;
 
1298
        *p++ = rec->r.ver.marginals;
 
1299
        *p++ = rec->r.ver.completes;
 
1300
        *p++ = rec->r.ver.cert_depth;
 
1301
        *p++ = rec->r.ver.trust_model;
 
1302
        p += 3;
 
1303
        ulongtobuf(p, rec->r.ver.created); p += 4;
 
1304
        ulongtobuf(p, rec->r.ver.nextcheck); p += 4;
 
1305
        p += 4;
 
1306
        p += 4;
 
1307
        ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
 
1308
        p += 4;
 
1309
        ulongtobuf(p, rec->r.ver.trusthashtbl ); p += 4;
 
1310
        break;
 
1311
 
 
1312
      case RECTYPE_FREE:
 
1313
        ulongtobuf(p, rec->r.free.next); p += 4;
 
1314
        break;
 
1315
 
 
1316
 
 
1317
      case RECTYPE_HTBL:
 
1318
        for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
 
1319
            ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
 
1320
        }
 
1321
        break;
 
1322
 
 
1323
      case RECTYPE_HLST:
 
1324
        ulongtobuf( p, rec->r.hlst.next); p += 4;
 
1325
        for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
 
1326
            ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
 
1327
        }
 
1328
        break;
 
1329
 
 
1330
      case RECTYPE_TRUST:
 
1331
        memcpy( p, rec->r.trust.fingerprint, 20); p += 20;
 
1332
        *p++ = rec->r.trust.ownertrust;
 
1333
        *p++ = rec->r.trust.depth;
 
1334
        *p++ = rec->r.trust.min_ownertrust;
 
1335
        p++;
 
1336
        ulongtobuf( p, rec->r.trust.validlist); p += 4;
 
1337
        break;
 
1338
 
 
1339
      case RECTYPE_VALID:
 
1340
        memcpy( p, rec->r.valid.namehash, 20); p += 20;
 
1341
        *p++ = rec->r.valid.validity;
 
1342
        ulongtobuf( p, rec->r.valid.next); p += 4;
 
1343
        *p++ = rec->r.valid.full_count;
 
1344
        *p++ = rec->r.valid.marginal_count;
 
1345
        break;
 
1346
 
 
1347
      default:
 
1348
        BUG();
 
1349
    }
 
1350
 
 
1351
    rc = put_record_into_cache( recnum, buf );
 
1352
    if( rc )
 
1353
        ;
 
1354
    else if( rec->rectype == RECTYPE_TRUST )
 
1355
        rc = update_trusthashtbl( rec );
 
1356
 
 
1357
    return rc;
 
1358
}
 
1359
 
 
1360
int
 
1361
tdbio_delete_record( ulong recnum )
 
1362
{
 
1363
    TRUSTREC vr, rec;
 
1364
    int rc;
 
1365
 
 
1366
    /* Must read the record fist, so we can drop it from the hash tables */
 
1367
    rc = tdbio_read_record( recnum, &rec, 0 );
 
1368
    if( rc )
 
1369
        ;
 
1370
    else if( rec.rectype == RECTYPE_TRUST ) {
 
1371
         rc = drop_from_hashtable( get_trusthashrec(),
 
1372
                                   rec.r.trust.fingerprint, 20, rec.recnum );
 
1373
    }
 
1374
 
 
1375
    if( rc )
 
1376
        return rc;
 
1377
 
 
1378
    /* now we can chnage it to a free record */
 
1379
    rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
1380
    if( rc )
 
1381
        log_fatal( _("%s: error reading version record: %s\n"),
 
1382
                                       db_name, gpg_strerror (rc) );
 
1383
 
 
1384
    rec.recnum = recnum;
 
1385
    rec.rectype = RECTYPE_FREE;
 
1386
    rec.r.free.next = vr.r.ver.firstfree;
 
1387
    vr.r.ver.firstfree = recnum;
 
1388
    rc = tdbio_write_record( &rec );
 
1389
    if( !rc )
 
1390
        rc = tdbio_write_record( &vr );
 
1391
    return rc;
 
1392
}
 
1393
 
 
1394
/****************
 
1395
 * create a new record and return its record number
 
1396
 */
 
1397
ulong
 
1398
tdbio_new_recnum()
 
1399
{
 
1400
    off_t offset;
 
1401
    ulong recnum;
 
1402
    TRUSTREC vr, rec;
 
1403
    int rc;
 
1404
 
 
1405
    /* look for unused records */
 
1406
    rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
 
1407
    if( rc )
 
1408
        log_fatal( _("%s: error reading version record: %s\n"),
 
1409
                                             db_name, gpg_strerror (rc) );
 
1410
    if( vr.r.ver.firstfree ) {
 
1411
        recnum = vr.r.ver.firstfree;
 
1412
        rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
 
1413
        if( rc ) {
 
1414
            log_error( _("%s: error reading free record: %s\n"),
 
1415
                                                  db_name,  gpg_strerror (rc) );
 
1416
            return rc;
 
1417
        }
 
1418
        /* update dir record */
 
1419
        vr.r.ver.firstfree = rec.r.free.next;
 
1420
        rc = tdbio_write_record( &vr );
 
1421
        if( rc ) {
 
1422
            log_error( _("%s: error writing dir record: %s\n"),
 
1423
                                                     db_name, gpg_strerror (rc) );
 
1424
            return rc;
 
1425
        }
 
1426
        /*zero out the new record */
 
1427
        memset( &rec, 0, sizeof rec );
 
1428
        rec.rectype = 0; /* unused record */
 
1429
        rec.recnum = recnum;
 
1430
        rc = tdbio_write_record( &rec );
 
1431
        if( rc )
 
1432
            log_fatal(_("%s: failed to zero a record: %s\n"),
 
1433
                                       db_name, gpg_strerror (rc));
 
1434
    }
 
1435
    else { /* not found, append a new record */
 
1436
        offset = lseek( db_fd, 0, SEEK_END );
 
1437
        if( offset == -1 )
 
1438
            log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
 
1439
        recnum = offset / TRUST_RECORD_LEN;
 
1440
        assert(recnum); /* this is will never be the first record */
 
1441
        /* we must write a record, so that the next call to this function
 
1442
         * returns another recnum */
 
1443
        memset( &rec, 0, sizeof rec );
 
1444
        rec.rectype = 0; /* unused record */
 
1445
        rec.recnum = recnum;
 
1446
        rc = 0;
 
1447
        if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
 
1448
            rc = gpg_error_from_errno (errno);
 
1449
            log_error(_("trustdb rec %lu: lseek failed: %s\n"),
 
1450
                                                recnum, strerror(errno) );
 
1451
        }
 
1452
        else {
 
1453
            int n = write( db_fd, &rec, TRUST_RECORD_LEN);
 
1454
            if( n != TRUST_RECORD_LEN ) {
 
1455
                rc = gpg_error_from_errno (errno);
 
1456
                log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
 
1457
                                                 recnum, n, strerror(errno) );
 
1458
            }
 
1459
        }
 
1460
 
 
1461
        if( rc )
 
1462
            log_fatal(_("%s: failed to append a record: %s\n"),
 
1463
                                    db_name,    gpg_strerror (rc));
 
1464
    }
 
1465
    return recnum ;
 
1466
}
 
1467
 
 
1468
 
 
1469
 
 
1470
static int
 
1471
cmp_trec_fpr ( void *fpr, const TRUSTREC *rec )
 
1472
{
 
1473
    return rec->rectype == RECTYPE_TRUST
 
1474
           && !memcmp( rec->r.trust.fingerprint, fpr, 20);
 
1475
}
 
1476
 
 
1477
 
 
1478
int
 
1479
tdbio_search_trust_byfpr( const byte *fingerprint, TRUSTREC *rec )
 
1480
{
 
1481
    int rc;
 
1482
 
 
1483
    /* locate the trust record using the hash table */
 
1484
    rc = lookup_hashtable( get_trusthashrec(), fingerprint, 20,
 
1485
                           cmp_trec_fpr, (void*)fingerprint, rec );
 
1486
    return rc;
 
1487
}
 
1488
 
 
1489
int
 
1490
tdbio_search_trust_bypk (PKT_public_key *pk, TRUSTREC *rec)
 
1491
{
 
1492
    byte fingerprint[MAX_FINGERPRINT_LEN];
 
1493
    size_t fingerlen;
 
1494
 
 
1495
    fingerprint_from_pk( pk, fingerprint, &fingerlen );
 
1496
    for (; fingerlen < 20; fingerlen++ )
 
1497
      fingerprint[fingerlen] = 0;
 
1498
    return tdbio_search_trust_byfpr (fingerprint, rec);
 
1499
}
 
1500
 
 
1501
 
 
1502
 
 
1503
void
 
1504
tdbio_invalid(void)
 
1505
{
 
1506
    log_error(_(
 
1507
        "the trustdb is corrupted; please run \"gpg --fix-trustdb\".\n") );
 
1508
    g10_exit(2);
 
1509
}
 
1510
 
 
1511
/*
 
1512
 * Migrate the trustdb as just up to gpg 1.0.6 (trustdb version 2)
 
1513
 * to the 2.1 version as used with 1.0.6b - This is pretty trivial as needs
 
1514
 * only to scan the tdb and insert new the new trust records.  The old ones are
 
1515
 * obsolte from now on
 
1516
 */
 
1517
static void
 
1518
migrate_from_v2 ()
 
1519
{
 
1520
  TRUSTREC rec;
 
1521
  int i, n;
 
1522
  struct {
 
1523
    ulong keyrecno;
 
1524
    byte  ot;
 
1525
    byte okay;
 
1526
    byte  fpr[20];
 
1527
  } *ottable;
 
1528
  int ottable_size, ottable_used;
 
1529
  byte oldbuf[40];
 
1530
  ulong recno;
 
1531
  int rc, count;
 
1532
 
 
1533
  ottable_size = 5;
 
1534
  ottable = xmalloc (ottable_size * sizeof *ottable);
 
1535
  ottable_used = 0;
 
1536
 
 
1537
  /* We have some restrictions here.  We can't use the version record
 
1538
   * and we can't use any of the old hashtables because we dropped the
 
1539
   * code.  So we first collect all ownertrusts and then use a second
 
1540
   * pass fo find the associated keys.  We have to do this all without using 
 
1541
   * the regular record read functions.
 
1542
   */
 
1543
 
 
1544
  /* get all the ownertrusts */
 
1545
  if (lseek (db_fd, 0, SEEK_SET ) == -1 ) 
 
1546
      log_fatal ("migrate_from_v2: lseek failed: %s\n", strerror (errno));
 
1547
  for (recno=0;;recno++)
 
1548
    {
 
1549
      do
 
1550
        n = read (db_fd, oldbuf, 40);
 
1551
      while (n==-1 && errno == EINTR);
 
1552
      if (!n)
 
1553
        break; /* eof */
 
1554
      if (n != 40)
 
1555
        log_fatal ("migrate_vfrom_v2: read error or short read\n");
 
1556
 
 
1557
      if (*oldbuf != 2)
 
1558
        continue;
 
1559
      
 
1560
      /* v2 dir record */
 
1561
      if (ottable_used == ottable_size)
 
1562
        {
 
1563
          ottable_size += 1000;
 
1564
          ottable = xrealloc (ottable, ottable_size * sizeof *ottable);
 
1565
        }
 
1566
      ottable[ottable_used].keyrecno = buftoulong (oldbuf+6);
 
1567
      ottable[ottable_used].ot = oldbuf[18];
 
1568
      ottable[ottable_used].okay = 0;
 
1569
      memset (ottable[ottable_used].fpr,0, 20);
 
1570
      if (ottable[ottable_used].keyrecno && ottable[ottable_used].ot)
 
1571
        ottable_used++;
 
1572
    }
 
1573
  log_info ("found %d ownertrust records\n", ottable_used);
 
1574
 
 
1575
  /* Read again and find the fingerprints */
 
1576
  if (lseek (db_fd, 0, SEEK_SET ) == -1 ) 
 
1577
      log_fatal ("migrate_from_v2: lseek failed: %s\n", strerror (errno));
 
1578
  for (recno=0;;recno++)
 
1579
    {
 
1580
      do
 
1581
        n = read (db_fd, oldbuf, 40);
 
1582
      while (n==-1 && errno == EINTR);
 
1583
      if (!n)
 
1584
        break; /* eof */
 
1585
      if (n != 40)
 
1586
        log_fatal ("migrate_from_v2: read error or short read\n");
 
1587
 
 
1588
      if (*oldbuf != 3) 
 
1589
        continue;
 
1590
 
 
1591
      /* v2 key record */
 
1592
      for (i=0; i < ottable_used; i++)
 
1593
        {
 
1594
          if (ottable[i].keyrecno == recno)
 
1595
            {
 
1596
              memcpy (ottable[i].fpr, oldbuf+20, 20);
 
1597
              ottable[i].okay = 1;
 
1598
              break;
 
1599
            }
 
1600
        }
 
1601
    }
 
1602
 
 
1603
  /* got everything - create the v3 trustdb */
 
1604
  if (ftruncate (db_fd, 0))
 
1605
    log_fatal ("can't truncate `%s': %s\n", db_name, strerror (errno) );
 
1606
  if (create_version_record ())
 
1607
    log_fatal ("failed to recreate version record of `%s'\n", db_name);
 
1608
 
 
1609
  /* access the hash table, so it is store just after the version record, 
 
1610
   * this is not needed put a dump is more pretty */
 
1611
  get_trusthashrec ();
 
1612
 
 
1613
  /* And insert the old ownertrust values */
 
1614
  count = 0;
 
1615
  for (i=0; i < ottable_used; i++)
 
1616
    {
 
1617
      if (!ottable[i].okay)
 
1618
        continue;
 
1619
      
 
1620
      memset (&rec, 0, sizeof rec);
 
1621
      rec.recnum = tdbio_new_recnum ();
 
1622
      rec.rectype = RECTYPE_TRUST;
 
1623
      memcpy(rec.r.trust.fingerprint, ottable[i].fpr, 20);
 
1624
      rec.r.trust.ownertrust = ottable[i].ot;
 
1625
      if (tdbio_write_record (&rec))
 
1626
        log_fatal ("failed to write trust record of `%s'\n", db_name);
 
1627
      count++;
 
1628
    }
 
1629
 
 
1630
  revalidation_mark ();
 
1631
  rc = tdbio_sync ();
 
1632
  if (rc)
 
1633
    log_fatal ("failed to sync `%s'\n", db_name);
 
1634
  log_info ("migrated %d version 2 ownertrusts\n", count);
 
1635
  xfree (ottable);
 
1636
}