Coverage Report

Created: 2026-01-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/kbx/keybox-update.c
Line
Count
Source
1
/* keybox-update.c - keybox update operations
2
 * Copyright (C) 2001, 2003, 2004, 2012 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 3 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, see <https://www.gnu.org/licenses/>.
18
 */
19
20
#include <config.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <errno.h>
25
#include <time.h>
26
#include <unistd.h>
27
#include <assert.h>
28
29
#include "keybox-defs.h"
30
#include "../common/sysutils.h"
31
#include "../common/host2net.h"
32
#include "../common/utilproto.h"
33
34
#define EXTSEP_S "."
35
36
14.7k
#define FILECOPY_INSERT 1
37
11.7k
#define FILECOPY_DELETE 2
38
11.6k
#define FILECOPY_UPDATE 3
39
40
41
#if !defined(HAVE_FSEEKO) && !defined(fseeko)
42
43
#ifdef HAVE_LIMITS_H
44
# include <limits.h>
45
#endif
46
#ifndef LONG_MAX
47
# define LONG_MAX ((long) ((unsigned long) -1 >> 1))
48
#endif
49
#ifndef LONG_MIN
50
# define LONG_MIN (-1 - LONG_MAX)
51
#endif
52
53
/****************
54
 * A substitute for fseeko, for hosts that don't have it.
55
 */
56
static int
57
fseeko (FILE * stream, off_t newpos, int whence)
58
{
59
  while (newpos != (long) newpos)
60
    {
61
      long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
62
      if (fseek (stream, pos, whence) != 0)
63
  return -1;
64
      newpos -= pos;
65
      whence = SEEK_CUR;
66
    }
67
  return fseek (stream, (long) newpos, whence);
68
}
69
#endif /* !defined(HAVE_FSEEKO) && !defined(fseeko) */
70
71
72
static int
73
create_tmp_file (const char *template,
74
                 char **r_bakfname, char **r_tmpfname, estream_t *r_fp)
75
2.93k
{
76
2.93k
  gpg_error_t err;
77
78
2.93k
  err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
79
2.93k
  if (!err)
80
2.93k
    {
81
2.93k
      err = _keybox_ll_open (r_fp, *r_tmpfname, KEYBOX_LL_OPEN_CREATE);
82
2.93k
      if (err)
83
0
        {
84
0
          xfree (*r_tmpfname);
85
0
          *r_tmpfname = NULL;
86
0
          xfree (*r_bakfname);
87
0
          *r_bakfname = NULL;
88
0
        }
89
2.93k
    }
90
91
2.93k
  return err;
92
2.93k
}
93
94
95
static int
96
rename_tmp_file (const char *bakfname, const char *tmpfname,
97
                 const char *fname, int secret )
98
2.93k
{
99
2.93k
  int rc=0;
100
2.93k
  int block = 0;
101
102
  /* restrict the permissions for secret keyboxs */
103
2.93k
#ifndef HAVE_DOSISH_SYSTEM
104
/*    if (secret && !opt.preserve_permissions) */
105
/*      { */
106
/*        if (chmod (tmpfname, S_IRUSR | S_IWUSR) )  */
107
/*          { */
108
/*            log_debug ("chmod of '%s' failed: %s\n", */
109
/*                       tmpfname, strerror(errno) ); */
110
/*            return KEYBOX_Write_File; */
111
/*    } */
112
/*      } */
113
2.93k
#endif
114
115
  /* fixme: invalidate close caches (not used with stdio)*/
116
/*    iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)tmpfname ); */
117
/*    iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)bakfname ); */
118
/*    iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname ); */
119
120
  /* First make a backup file except for secret keyboxes. */
121
2.93k
  if (!secret)
122
2.93k
    {
123
2.93k
      block = 1;
124
2.93k
      rc = gnupg_rename_file (fname, bakfname, &block);
125
2.93k
      if (rc)
126
0
        goto leave;
127
2.93k
    }
128
129
  /* Then rename the file. */
130
2.93k
  rc = gnupg_rename_file (tmpfname, fname, NULL);
131
2.93k
  if (block)
132
2.93k
    {
133
2.93k
      gnupg_unblock_all_signals ();
134
2.93k
      block = 0;
135
2.93k
    }
136
  /* if (rc) */
137
  /*   { */
138
  /*     if (secret) */
139
  /*       { */
140
  /*         log_info ("WARNING: 2 files with confidential" */
141
  /*                   " information exists.\n"); */
142
  /*         log_info ("%s is the unchanged one\n", fname ); */
143
  /*         log_info ("%s is the new one\n", tmpfname ); */
144
  /*         log_info ("Please fix this possible security flaw\n"); */
145
  /*       } */
146
  /*   } */
147
148
2.93k
 leave:
149
2.93k
  if (block)
150
0
    gnupg_unblock_all_signals ();
151
2.93k
  return rc;
152
2.93k
}
153
154
155
156
/* Perform insert/delete/update operation.  MODE is one of
157
   FILECOPY_INSERT, FILECOPY_DELETE, FILECOPY_UPDATE.  FOR_OPENPGP
158
   indicates that this is called due to an OpenPGP keyblock change.  */
159
static int
160
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
161
               int secret, int for_openpgp, off_t start_offset)
162
2.93k
{
163
2.93k
  gpg_err_code_t ec;
164
2.93k
  estream_t fp, newfp;
165
2.93k
  int rc = 0;
166
2.93k
  char *bakfname = NULL;
167
2.93k
  char *tmpfname = NULL;
168
2.93k
  char buffer[4096];  /* (Must be at least 32 bytes) */
169
2.93k
  int nread, nbytes;
170
171
  /* Open the source file. Because we do a rename, we have to check the
172
     permissions of the file */
173
2.93k
  if ((ec = gnupg_access (fname, W_OK)))
174
0
    return gpg_error (ec);
175
176
2.93k
  rc = _keybox_ll_open (&fp, fname, 0);
177
2.93k
  if (mode == FILECOPY_INSERT && gpg_err_code (rc) == GPG_ERR_ENOENT)
178
0
    {
179
      /* Insert mode but file does not exist:
180
       * Create a new keybox file. */
181
0
      rc = _keybox_ll_open (&newfp, fname, KEYBOX_LL_OPEN_CREATE);
182
0
      if (rc)
183
0
        return rc;
184
185
0
      rc = _keybox_write_header_blob (newfp, for_openpgp);
186
0
      if (rc)
187
0
        {
188
0
          _keybox_ll_close (newfp);
189
0
          return rc;
190
0
        }
191
192
0
      rc = _keybox_write_blob (blob, newfp, NULL);
193
0
      if (rc)
194
0
        {
195
0
          _keybox_ll_close (newfp);
196
0
          return rc;
197
0
        }
198
199
0
      rc = _keybox_ll_close (newfp);
200
0
      if (rc)
201
0
        return rc;
202
203
/*        if (chmod( fname, S_IRUSR | S_IWUSR )) */
204
/*          { */
205
/*            log_debug ("%s: chmod failed: %s\n", fname, strerror(errno) ); */
206
/*            return KEYBOX_File_Error; */
207
/*          } */
208
0
      return 0; /* Ready. */
209
0
    }
210
211
2.93k
  if (!fp)
212
0
    {
213
0
      rc = gpg_error_from_syserror ();
214
0
      goto leave;
215
0
    }
216
217
  /* Create the new file.  On success NEWFP is initialized.  */
218
2.93k
  rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
219
2.93k
  if (rc)
220
0
    {
221
0
      _keybox_ll_close (fp);
222
0
      goto leave;
223
0
    }
224
225
  /* prepare for insert */
226
2.93k
  if (mode == FILECOPY_INSERT)
227
42
    {
228
42
      int first_record = 1;
229
230
      /* Copy everything to the new file.  If this is for OpenPGP, we
231
         make sure that the openpgp flag is set in the header.  (We
232
         failsafe the blob type.) */
233
1.96k
      while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
234
1.92k
        {
235
1.92k
          if (first_record && for_openpgp
236
42
              && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
237
42
            {
238
42
              first_record = 0;
239
42
              buffer[7] |= 0x02; /* OpenPGP data may be available.  */
240
42
            }
241
242
1.92k
          if (es_fwrite (buffer, nread, 1, newfp) != 1)
243
0
            {
244
0
              rc = gpg_error_from_syserror ();
245
0
              _keybox_ll_close (fp);
246
0
              _keybox_ll_close (newfp);
247
0
              goto leave;
248
0
            }
249
1.92k
        }
250
42
      if (es_ferror (fp))
251
0
        {
252
0
          rc = gpg_error_from_syserror ();
253
0
          _keybox_ll_close (fp);
254
0
          _keybox_ll_close (newfp);
255
0
          goto leave;
256
0
        }
257
42
    }
258
259
  /* Prepare for delete or update. */
260
2.93k
  if ( mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE )
261
2.89k
    {
262
2.89k
      off_t current = 0;
263
264
      /* Copy first part to the new file. */
265
22.9k
      while ( current < start_offset )
266
20.0k
        {
267
20.0k
          nbytes = DIM(buffer);
268
20.0k
          if (current + nbytes > start_offset)
269
2.89k
              nbytes = start_offset - current;
270
20.0k
          nread = es_fread (buffer, 1, nbytes, fp);
271
20.0k
          if (!nread)
272
0
            break;
273
20.0k
          current += nread;
274
275
20.0k
          if (es_fwrite (buffer, nread, 1, newfp) != 1)
276
0
            {
277
0
              rc = gpg_error_from_syserror ();
278
0
              _keybox_ll_close (fp);
279
0
              _keybox_ll_close (newfp);
280
0
              goto leave;
281
0
            }
282
20.0k
        }
283
2.89k
      if (es_ferror (fp))
284
0
        {
285
0
          rc = gpg_error_from_syserror ();
286
0
          _keybox_ll_close (fp);
287
0
          _keybox_ll_close (newfp);
288
0
          goto leave;
289
0
        }
290
291
      /* Skip this blob. */
292
2.89k
      rc = _keybox_read_blob (NULL, fp, NULL);
293
2.89k
      if (rc)
294
0
        {
295
0
          _keybox_ll_close (fp);
296
0
          _keybox_ll_close (newfp);
297
0
          goto leave;
298
0
        }
299
2.89k
    }
300
301
  /* Do an insert or update. */
302
2.93k
  if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE )
303
2.93k
    {
304
2.93k
      rc = _keybox_write_blob (blob, newfp, NULL);
305
2.93k
      if (rc)
306
0
        {
307
0
          _keybox_ll_close (fp);
308
0
          _keybox_ll_close (newfp);
309
0
          goto leave;
310
0
        }
311
2.93k
    }
312
313
  /* Copy the rest of the packet for an delete or update. */
314
2.93k
  if (mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE)
315
2.89k
    {
316
48.1k
      while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
317
45.2k
        {
318
45.2k
          if (es_fwrite (buffer, nread, 1, newfp) != 1)
319
0
            {
320
0
              rc = gpg_error_from_syserror ();
321
0
              _keybox_ll_close (fp);
322
0
              _keybox_ll_close (newfp);
323
0
              goto leave;
324
0
            }
325
45.2k
        }
326
2.89k
      if (es_ferror (fp))
327
0
        {
328
0
          rc = gpg_error_from_syserror ();
329
0
          _keybox_ll_close (fp);
330
0
          _keybox_ll_close (newfp);
331
0
          goto leave;
332
0
        }
333
2.89k
    }
334
335
  /* Close both files. */
336
2.93k
  rc = _keybox_ll_close (fp);
337
2.93k
  if (rc)
338
0
    {
339
0
      _keybox_ll_close (newfp);
340
0
      goto leave;
341
0
    }
342
2.93k
  rc = _keybox_ll_close (newfp);
343
2.93k
  if (rc)
344
0
    goto leave;
345
346
2.93k
  rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
347
348
2.93k
 leave:
349
2.93k
  xfree(bakfname);
350
2.93k
  xfree(tmpfname);
351
2.93k
  return rc;
352
2.93k
}
353
354
355
/* Insert the OpenPGP keyblock {IMAGE,IMAGELEN} into HD. */
356
gpg_error_t
357
keybox_insert_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen)
358
42
{
359
42
  gpg_error_t err;
360
42
  const char *fname;
361
42
  KEYBOXBLOB blob;
362
42
  size_t nparsed;
363
42
  struct _keybox_openpgp_info info;
364
365
42
  if (!hd)
366
0
    return gpg_error (GPG_ERR_INV_HANDLE);
367
42
  if (!hd->kb)
368
0
    return gpg_error (GPG_ERR_INV_HANDLE);
369
42
  fname = hd->kb->fname;
370
42
  if (!fname)
371
0
    return gpg_error (GPG_ERR_INV_HANDLE);
372
373
374
  /* Close this one otherwise we will mess up the position for a next
375
     search.  Fixme: it would be better to adjust the position after
376
     the write operation.  */
377
42
  _keybox_close_file (hd);
378
379
42
  err = _keybox_parse_openpgp (image, imagelen, 0, &nparsed, &info);
380
42
  if (err)
381
0
    return err;
382
42
  assert (nparsed <= imagelen);
383
42
  err = _keybox_create_openpgp_blob (&blob, &info, image, imagelen,
384
42
                                      hd->ephemeral);
385
42
  _keybox_destroy_openpgp_info (&info);
386
42
  if (!err)
387
42
    {
388
42
      err = blob_filecopy (FILECOPY_INSERT, fname, blob, hd->secret, 1, 0);
389
42
      _keybox_release_blob (blob);
390
      /*    if (!rc && !hd->secret && kb_offtbl) */
391
      /*      { */
392
      /*        update_offset_hash_table_from_kb (kb_offtbl, kb, 0); */
393
      /*      } */
394
42
    }
395
42
  return err;
396
42
}
397
398
399
/* Update the current key at HD with the given OpenPGP keyblock in
400
   {IMAGE,IMAGELEN}.  */
401
gpg_error_t
402
keybox_update_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen)
403
2.89k
{
404
2.89k
  gpg_error_t err;
405
2.89k
  const char *fname;
406
2.89k
  off_t off;
407
2.89k
  KEYBOXBLOB blob;
408
2.89k
  size_t nparsed;
409
2.89k
  struct _keybox_openpgp_info info;
410
411
2.89k
  if (!hd || !image || !imagelen)
412
0
    return gpg_error (GPG_ERR_INV_VALUE);
413
2.89k
  if (!hd->found.blob)
414
0
    return gpg_error (GPG_ERR_NOTHING_FOUND);
415
2.89k
  if (blob_get_type (hd->found.blob) != KEYBOX_BLOBTYPE_PGP)
416
0
    return gpg_error (GPG_ERR_WRONG_BLOB_TYPE);
417
2.89k
  fname = hd->kb->fname;
418
2.89k
  if (!fname)
419
0
    return gpg_error (GPG_ERR_INV_HANDLE);
420
421
2.89k
  off = _keybox_get_blob_fileoffset (hd->found.blob);
422
2.89k
  if (off == (off_t)-1)
423
0
    return gpg_error (GPG_ERR_GENERAL);
424
425
  /* Close the file so that we do no mess up the position for a
426
     next search.  */
427
2.89k
  _keybox_close_file (hd);
428
429
  /* Build a new blob.  */
430
2.89k
  err = _keybox_parse_openpgp (image, imagelen, 0, &nparsed, &info);
431
2.89k
  if (err)
432
0
    return err;
433
2.89k
  assert (nparsed <= imagelen);
434
2.89k
  err = _keybox_create_openpgp_blob (&blob, &info, image, imagelen,
435
2.89k
                                     hd->ephemeral);
436
2.89k
  _keybox_destroy_openpgp_info (&info);
437
438
  /* Update the keyblock.  */
439
2.89k
  if (!err)
440
2.89k
    {
441
2.89k
      err = blob_filecopy (FILECOPY_UPDATE, fname, blob, hd->secret, 1, off);
442
2.89k
      _keybox_release_blob (blob);
443
2.89k
    }
444
2.89k
  return err;
445
2.89k
}
446
447
448
449
#ifdef KEYBOX_WITH_X509
450
int
451
keybox_insert_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
452
                    unsigned char *sha1_digest)
453
{
454
  int rc;
455
  const char *fname;
456
  KEYBOXBLOB blob;
457
458
  if (!hd)
459
    return gpg_error (GPG_ERR_INV_HANDLE);
460
  if (!hd->kb)
461
    return gpg_error (GPG_ERR_INV_HANDLE);
462
  fname = hd->kb->fname;
463
  if (!fname)
464
    return gpg_error (GPG_ERR_INV_HANDLE);
465
466
  /* Close this one otherwise we will mess up the position for a next
467
     search.  Fixme: it would be better to adjust the position after
468
     the write operation.  */
469
  _keybox_close_file (hd);
470
471
  rc = _keybox_create_x509_blob (&blob, cert, sha1_digest, hd->ephemeral);
472
  if (!rc)
473
    {
474
      rc = blob_filecopy (FILECOPY_INSERT, fname, blob, hd->secret, 0, 0);
475
      _keybox_release_blob (blob);
476
      /*    if (!rc && !hd->secret && kb_offtbl) */
477
      /*      { */
478
      /*        update_offset_hash_table_from_kb (kb_offtbl, kb, 0); */
479
      /*      } */
480
    }
481
  return rc;
482
}
483
484
int
485
keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
486
                    unsigned char *sha1_digest)
487
{
488
  (void)hd;
489
  (void)cert;
490
  (void)sha1_digest;
491
  return -1;
492
}
493
494
495
#endif /*KEYBOX_WITH_X509*/
496
497
/* Note: We assume that the keybox has been locked before the current
498
   search was executed.  This is needed so that we can depend on the
499
   offset information of the flags. */
500
int
501
keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
502
0
{
503
0
  gpg_error_t err;
504
0
  off_t off;
505
0
  const char *fname;
506
0
  estream_t fp;
507
0
  gpg_err_code_t ec;
508
0
  size_t flag_pos, flag_size;
509
0
  const unsigned char *buffer;
510
0
  size_t length;
511
512
0
  (void)idx;  /* Not yet used.  */
513
514
0
  if (!hd)
515
0
    return gpg_error (GPG_ERR_INV_VALUE);
516
0
  if (!hd->found.blob)
517
0
    return gpg_error (GPG_ERR_NOTHING_FOUND);
518
0
  if (!hd->kb)
519
0
    return gpg_error (GPG_ERR_INV_HANDLE);
520
0
  if (!hd->found.blob)
521
0
    return gpg_error (GPG_ERR_NOTHING_FOUND);
522
0
  fname = hd->kb->fname;
523
0
  if (!fname)
524
0
    return gpg_error (GPG_ERR_INV_HANDLE);
525
526
0
  off = _keybox_get_blob_fileoffset (hd->found.blob);
527
0
  if (off == (off_t)-1)
528
0
    return gpg_error (GPG_ERR_GENERAL);
529
530
0
  buffer = _keybox_get_blob_image (hd->found.blob, &length);
531
0
  ec = _keybox_get_flag_location (buffer, length, what, &flag_pos, &flag_size);
532
0
  if (ec)
533
0
    return gpg_error (ec);
534
535
0
  off += flag_pos;
536
537
0
  _keybox_close_file (hd);
538
539
0
  err = _keybox_ll_open (&fp, fname, KEYBOX_LL_OPEN_UPDATE);
540
0
  if (err)
541
0
    return err;
542
543
0
  ec = 0;
544
0
  if (es_fseeko (fp, off, SEEK_SET))
545
0
    ec = gpg_err_code_from_syserror ();
546
0
  else
547
0
    {
548
0
      unsigned char tmp[4];
549
550
0
      tmp[0] = value >> 24;
551
0
      tmp[1] = value >> 16;
552
0
      tmp[2] = value >>  8;
553
0
      tmp[3] = value;
554
555
0
      switch (flag_size)
556
0
        {
557
0
        case 1:
558
0
        case 2:
559
0
        case 4:
560
0
          if (es_fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
561
0
            ec = gpg_err_code_from_syserror ();
562
0
          break;
563
0
        default:
564
0
          ec = GPG_ERR_BUG;
565
0
          break;
566
0
        }
567
0
    }
568
569
0
  err = _keybox_ll_close (fp);
570
0
  if (err)
571
0
    {
572
0
      if (!ec)
573
0
        ec = gpg_err_code (err);
574
0
    }
575
576
0
  return gpg_error (ec);
577
0
}
578
579
580
581
int
582
keybox_delete (KEYBOX_HANDLE hd)
583
0
{
584
0
  off_t off;
585
0
  const char *fname;
586
0
  estream_t fp;
587
0
  int rc, rc2;
588
589
0
  if (!hd)
590
0
    return gpg_error (GPG_ERR_INV_VALUE);
591
0
  if (!hd->found.blob)
592
0
    return gpg_error (GPG_ERR_NOTHING_FOUND);
593
0
  if (!hd->kb)
594
0
    return gpg_error (GPG_ERR_INV_HANDLE);
595
0
  fname = hd->kb->fname;
596
0
  if (!fname)
597
0
    return gpg_error (GPG_ERR_INV_HANDLE);
598
599
0
  off = _keybox_get_blob_fileoffset (hd->found.blob);
600
0
  if (off == (off_t)-1)
601
0
    return gpg_error (GPG_ERR_GENERAL);
602
0
  off += 4;
603
604
0
  _keybox_close_file (hd);
605
0
  rc = _keybox_ll_open (&fp, hd->kb->fname, KEYBOX_LL_OPEN_UPDATE);
606
0
  if (rc)
607
0
    return rc;
608
609
0
  if (es_fseeko (fp, off, SEEK_SET))
610
0
    rc = gpg_error_from_syserror ();
611
0
  else if (es_fputc (0, fp) == EOF)
612
0
    rc = gpg_error_from_syserror ();
613
0
  else
614
0
    rc = 0;
615
616
0
  rc2 = _keybox_ll_close (fp);
617
0
  if (rc2)
618
0
    {
619
0
      if (!rc)
620
0
        rc = rc2;
621
0
    }
622
623
0
  return rc;
624
0
}
625
626
627
/* Compress the keybox file, if needed and not used by other
628
 * process.  */
629
void
630
keybox_compress_when_no_other_users (void *token, int for_openpgp)
631
1
{
632
1
  KEYBOX_HANDLE hd;
633
1
  gpg_error_t err;
634
1
  int read_rc, rc, rc2;
635
1
  const char *fname;
636
1
  estream_t fp, newfp;
637
1
  char *bakfname = NULL;
638
1
  char *tmpfname = NULL;
639
1
  int first_blob;
640
1
  KEYBOXBLOB blob = NULL;
641
1
  u32 cut_time;
642
1
  int any_changes = 0;
643
1
  int skipped_deleted;
644
645
1
  if (for_openpgp)
646
1
    hd = keybox_new_openpgp (token, 0);
647
0
  else
648
0
    hd = keybox_new_x509 (token, 0);
649
1
  if (!hd || !hd->kb)
650
0
    return;
651
652
1
  if (hd->secret)
653
0
    return;
654
1
  fname = hd->kb->fname;
655
1
  if (!fname)
656
0
    {
657
0
      keybox_fp_close (hd);
658
0
      keybox_release (hd);
659
0
      return;
660
0
    }
661
662
1
  if (keybox_lock (hd, 1, 0))
663
0
    {
664
0
      keybox_fp_close (hd);
665
0
      keybox_release (hd);
666
0
      return;
667
0
    }
668
669
1
  _keybox_close_file (hd);
670
671
  /* Open the source file. Because we do a rename, we have to check the
672
     permissions of the file */
673
1
  if ((rc = gnupg_access (fname, W_OK)))
674
0
    {
675
0
      err = gpg_error (rc);
676
0
      goto leave;
677
0
    }
678
679
1
  rc = _keybox_ll_open (&fp, fname, 0);
680
1
  if (gpg_err_code (rc) == GPG_ERR_ENOENT)
681
0
    {
682
0
      err = 0; /* Ready. File has been deleted right after the access above. */
683
0
      goto leave;
684
0
    }
685
1
  if (rc)
686
0
    {
687
0
      err = gpg_error (rc);
688
0
      goto leave;
689
0
    }
690
691
  /* A quick test to see if we need to compress the file at all.  We
692
     schedule a compress run after 3 hours. */
693
1
  if ( !_keybox_read_blob (&blob, fp, NULL) )
694
1
    {
695
1
      const unsigned char *buffer;
696
1
      size_t length;
697
698
1
      buffer = _keybox_get_blob_image (blob, &length);
699
1
      if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
700
1
        {
701
1
          u32 last_maint = buf32_to_u32 (buffer+20);
702
703
1
          if ( (last_maint + 3*3600) > make_timestamp () )
704
1
            {
705
1
              _keybox_ll_close (fp);
706
1
              _keybox_release_blob (blob);
707
1
              err = 0;
708
1
              goto leave; /* Compress run not yet needed. */
709
1
            }
710
1
        }
711
0
      _keybox_release_blob (blob);
712
0
      es_fseek (fp, 0, SEEK_SET);
713
0
      es_clearerr (fp);
714
0
    }
715
716
  /* Create the new file. */
717
0
  rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
718
0
  if (rc)
719
0
    {
720
0
      _keybox_ll_close (fp);
721
0
      err = gpg_error (rc);
722
0
      goto leave;
723
0
    }
724
725
726
  /* Processing loop.  By reading using _keybox_read_blob we
727
     automagically skip any blobs flagged as deleted.  Thus what we
728
     only have to do is to check all ephemeral flagged blocks whether
729
     their time has come and write out all other blobs. */
730
0
  cut_time = make_timestamp () - 86400;
731
0
  first_blob = 1;
732
0
  skipped_deleted = 0;
733
0
  for (rc=0; !(read_rc = _keybox_read_blob (&blob, fp, &skipped_deleted));
734
0
       _keybox_release_blob (blob), blob = NULL )
735
0
    {
736
0
      unsigned int blobflags;
737
0
      const unsigned char *buffer;
738
0
      size_t length, pos, size;
739
0
      u32 created_at;
740
741
0
      if (skipped_deleted)
742
0
        any_changes = 1;
743
0
      buffer = _keybox_get_blob_image (blob, &length);
744
0
      if (first_blob)
745
0
        {
746
0
          first_blob = 0;
747
0
          if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
748
0
            {
749
              /* Write out the blob with an updated maintenance time
750
                 stamp and if needed (ie. used by gpg) set the openpgp
751
                 flag.  */
752
0
              _keybox_update_header_blob (blob, hd->for_openpgp);
753
0
              rc = _keybox_write_blob (blob, newfp, NULL);
754
0
              if (rc)
755
0
                break;
756
0
              continue;
757
0
            }
758
759
          /* The header blob is missing.  Insert it.  */
760
0
          rc = _keybox_write_header_blob (newfp, hd->for_openpgp);
761
0
          if (rc)
762
0
            break;
763
0
          any_changes = 1;
764
0
        }
765
0
      else if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
766
0
        {
767
          /* Oops: There is another header record - remove it. */
768
0
          any_changes = 1;
769
0
          continue;
770
0
        }
771
772
0
      if (_keybox_get_flag_location (buffer, length,
773
0
                                     KEYBOX_FLAG_BLOB, &pos, &size)
774
0
          || size != 2)
775
0
        {
776
0
          rc = GPG_ERR_BUG;
777
0
          break;
778
0
        }
779
0
      blobflags = buf16_to_uint (buffer+pos);
780
0
      if ((blobflags & KEYBOX_FLAG_BLOB_EPHEMERAL))
781
0
        {
782
          /* This is an ephemeral blob. */
783
0
          if (_keybox_get_flag_location (buffer, length,
784
0
                                         KEYBOX_FLAG_CREATED_AT, &pos, &size)
785
0
              || size != 4)
786
0
            created_at = 0; /* oops. */
787
0
          else
788
0
            created_at = buf32_to_u32 (buffer+pos);
789
790
0
          if (created_at && created_at < cut_time)
791
0
            {
792
0
              any_changes = 1;
793
0
              continue; /* Skip this blob. */
794
0
            }
795
0
        }
796
797
0
      rc = _keybox_write_blob (blob, newfp, NULL);
798
0
      if (rc)
799
0
        break;
800
0
    }
801
0
  if (skipped_deleted)
802
0
    any_changes = 1;
803
0
  _keybox_release_blob (blob); blob = NULL;
804
0
  if (!rc && read_rc == -1)
805
0
    rc = 0;
806
0
  else if (!rc)
807
0
    rc = read_rc;
808
809
  /* Close both files. */
810
0
  if ((rc2 = _keybox_ll_close (fp)) && !rc)
811
0
    rc = rc2;
812
0
  if ((rc2 = _keybox_ll_close (newfp)) && !rc)
813
0
    rc = rc2;
814
815
  /* Rename or remove the temporary file. */
816
0
  if (rc || !any_changes)
817
0
    gnupg_remove (tmpfname);
818
0
  else
819
0
    rc = rename_tmp_file (bakfname, tmpfname, fname, hd->secret);
820
821
0
  xfree(bakfname);
822
0
  xfree(tmpfname);
823
0
  if (rc)
824
0
    err = gpg_error (rc);
825
0
  else
826
0
    err = 0;
827
828
0
  if (err)
829
0
    log_error ("keybox: error compressing keybox '%s': %s\n",
830
0
               fname, gpg_strerror (err));
831
832
1
 leave:
833
1
  keybox_fp_close (hd);
834
1
  keybox_lock (hd, 0, 0);
835
1
  keybox_release (hd);
836
1
  return;
837
0
}