2003-06-05 09:14:21 +02:00
|
|
|
/* keybox-update.c - keybox update operations
|
2012-12-28 14:03:16 +01:00
|
|
|
* Copyright (C) 2001, 2003, 2004, 2012 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2004-04-26 10:09:25 +02:00
|
|
|
#include <time.h>
|
2003-06-05 09:14:21 +02:00
|
|
|
#include <unistd.h>
|
2012-12-28 14:03:16 +01:00
|
|
|
#include <assert.h>
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
#include "keybox-defs.h"
|
2010-03-24 13:15:30 +01:00
|
|
|
#include "../common/sysutils.h"
|
2015-02-11 10:27:57 +01:00
|
|
|
#include "../common/host2net.h"
|
2016-08-03 16:58:32 +02:00
|
|
|
#include "../common/utilproto.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
#define EXTSEP_S "."
|
|
|
|
|
2013-08-29 17:39:35 +02:00
|
|
|
#define FILECOPY_INSERT 1
|
|
|
|
#define FILECOPY_DELETE 2
|
|
|
|
#define FILECOPY_UPDATE 3
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
* configure.ac (gl_INIT): Add gnulib stuff.
(fseeko, ftello, ttyname, isascii): Replaced the AC_REPLACE_FUNCS
by a simple check.
(putc_unlocked): Removed check. Not used.
(strsep, mkdtemp, asprintf): Replaced checks by gnulib checks.
(xsize): Added will probably come handy soon.
(CFLAGS): Use -Wformat-security instead of
-Wformat-nonliteral. Add --Wno-format-y2k.
* gl/, gl/m4/: New.
* gpg-agent.c: Include setenv.h.
* Makefile.am (AM_CPPFLAGS): Added.
* util.h: Add some includes for gnulib.
(ttyname, isascii): Define them inline.
* fseeko.c, ftello.c: Removed.
* strsep.c, mkdtemp.c: Removed.
* ttyname.c, isascii.c: Removed.
* mkdtemp.c: Removed.
* exec.c: Include mkdtemp.h
* keybox-file.c (ftello) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/ftello.c.
* keybox-update.c (fseeko) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/iobuf.c.
* scdaemon.c: Include mkdtemp.h.
* misc.c: Include setenv.h.
* symcryptrun.c: Include mkdtemp.h.
2005-06-01 17:46:01 +02:00
|
|
|
#if !defined(HAVE_FSEEKO) && !defined(fseeko)
|
|
|
|
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
# include <limits.h>
|
|
|
|
#endif
|
|
|
|
#ifndef LONG_MAX
|
|
|
|
# define LONG_MAX ((long) ((unsigned long) -1 >> 1))
|
|
|
|
#endif
|
|
|
|
#ifndef LONG_MIN
|
|
|
|
# define LONG_MIN (-1 - LONG_MAX)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* A substitute for fseeko, for hosts that don't have it.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fseeko (FILE * stream, off_t newpos, int whence)
|
|
|
|
{
|
|
|
|
while (newpos != (long) newpos)
|
|
|
|
{
|
|
|
|
long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
|
|
|
|
if (fseek (stream, pos, whence) != 0)
|
|
|
|
return -1;
|
|
|
|
newpos -= pos;
|
|
|
|
whence = SEEK_CUR;
|
|
|
|
}
|
|
|
|
return fseek (stream, (long) newpos, whence);
|
|
|
|
}
|
|
|
|
#endif /* !defined(HAVE_FSEEKO) && !defined(fseeko) */
|
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
static int
|
|
|
|
create_tmp_file (const char *template,
|
2020-10-20 11:52:16 +02:00
|
|
|
char **r_bakfname, char **r_tmpfname, estream_t *r_fp)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2016-01-14 16:29:45 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
|
|
|
|
if (!err)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
*r_fp = es_fopen (*r_tmpfname, "wb");
|
2016-01-14 16:29:45 +01:00
|
|
|
if (!*r_fp)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2016-01-14 16:29:45 +01:00
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
xfree (*r_tmpfname);
|
|
|
|
*r_tmpfname = NULL;
|
|
|
|
xfree (*r_bakfname);
|
|
|
|
*r_bakfname = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-01 17:08:57 +02:00
|
|
|
|
2016-01-14 16:29:45 +01:00
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
rename_tmp_file (const char *bakfname, const char *tmpfname,
|
|
|
|
const char *fname, int secret )
|
|
|
|
{
|
|
|
|
int rc=0;
|
2016-08-03 15:31:27 +02:00
|
|
|
int block = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
/* restrict the permissions for secret keyboxs */
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
/* if (secret && !opt.preserve_permissions) */
|
|
|
|
/* { */
|
|
|
|
/* if (chmod (tmpfname, S_IRUSR | S_IWUSR) ) */
|
|
|
|
/* { */
|
2012-06-05 19:29:22 +02:00
|
|
|
/* log_debug ("chmod of '%s' failed: %s\n", */
|
2003-06-05 09:14:21 +02:00
|
|
|
/* tmpfname, strerror(errno) ); */
|
|
|
|
/* return KEYBOX_Write_File; */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fixme: invalidate close caches (not used with stdio)*/
|
2010-03-08 18:05:37 +01:00
|
|
|
/* iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)tmpfname ); */
|
|
|
|
/* iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)bakfname ); */
|
|
|
|
/* iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname ); */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
/* First make a backup file except for secret keyboxes. */
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!secret)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2016-08-03 15:31:27 +02:00
|
|
|
block = 1;
|
2016-11-16 17:43:59 +01:00
|
|
|
rc = gnupg_rename_file (fname, bakfname, &block);
|
2016-01-14 16:50:15 +01:00
|
|
|
if (rc)
|
2016-08-03 15:31:27 +02:00
|
|
|
goto leave;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
/* Then rename the file. */
|
2016-11-16 17:43:59 +01:00
|
|
|
rc = gnupg_rename_file (tmpfname, fname, NULL);
|
2016-08-03 15:31:27 +02:00
|
|
|
if (block)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2016-08-03 15:31:27 +02:00
|
|
|
gnupg_unblock_all_signals ();
|
|
|
|
block = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2016-08-03 15:31:27 +02:00
|
|
|
/* if (rc) */
|
|
|
|
/* { */
|
|
|
|
/* if (secret) */
|
|
|
|
/* { */
|
|
|
|
/* log_info ("WARNING: 2 files with confidential" */
|
|
|
|
/* " information exists.\n"); */
|
|
|
|
/* log_info ("%s is the unchanged one\n", fname ); */
|
|
|
|
/* log_info ("%s is the new one\n", tmpfname ); */
|
|
|
|
/* log_info ("Please fix this possible security flaw\n"); */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if (block)
|
|
|
|
gnupg_unblock_all_signals ();
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-09 19:10:32 +02:00
|
|
|
/* Perform insert/delete/update operation. MODE is one of
|
|
|
|
FILECOPY_INSERT, FILECOPY_DELETE, FILECOPY_UPDATE. FOR_OPENPGP
|
|
|
|
indicates that this is called due to an OpenPGP keyblock change. */
|
2003-06-05 09:14:21 +02:00
|
|
|
static int
|
2011-02-04 12:57:53 +01:00
|
|
|
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
|
2014-10-09 19:10:32 +02:00
|
|
|
int secret, int for_openpgp, off_t start_offset)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2020-10-20 10:43:55 +02:00
|
|
|
gpg_err_code_t ec;
|
2020-10-20 11:52:16 +02:00
|
|
|
estream_t fp, newfp;
|
|
|
|
int rc = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
char *bakfname = NULL;
|
|
|
|
char *tmpfname = NULL;
|
2014-10-09 19:10:32 +02:00
|
|
|
char buffer[4096]; /* (Must be at least 32 bytes) */
|
2003-06-05 09:14:21 +02:00
|
|
|
int nread, nbytes;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
/* Open the source file. Because we do a rename, we have to check the
|
2003-06-05 09:14:21 +02:00
|
|
|
permissions of the file */
|
2020-10-20 10:43:55 +02:00
|
|
|
if ((ec = gnupg_access (fname, W_OK)))
|
|
|
|
return gpg_error (ec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
fp = es_fopen (fname, "rb");
|
2013-08-29 17:39:35 +02:00
|
|
|
if (mode == FILECOPY_INSERT && !fp && errno == ENOENT)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Insert mode but file does not exist:
|
|
|
|
Create a new keybox file. */
|
2020-10-20 11:52:16 +02:00
|
|
|
newfp = es_fopen (fname, "wb");
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!newfp )
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_header_blob (newfp, for_openpgp);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (newfp);
|
2015-01-30 03:42:52 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_blob (blob, newfp, NULL);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (newfp);
|
2015-01-30 03:42:52 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if ( es_fclose (newfp) )
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
/* if (chmod( fname, S_IRUSR | S_IWUSR )) */
|
|
|
|
/* { */
|
|
|
|
/* log_debug ("%s: chmod failed: %s\n", fname, strerror(errno) ); */
|
|
|
|
/* return KEYBOX_File_Error; */
|
|
|
|
/* } */
|
2004-04-26 10:09:25 +02:00
|
|
|
return 0; /* Ready. */
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:39:08 +01:00
|
|
|
/* Create the new file. On success NEWFP is initialized. */
|
2003-06-05 09:14:21 +02:00
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
/* prepare for insert */
|
2013-08-29 17:39:35 +02:00
|
|
|
if (mode == FILECOPY_INSERT)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2014-10-09 19:10:32 +02:00
|
|
|
int first_record = 1;
|
|
|
|
|
|
|
|
/* Copy everything to the new file. If this is for OpenPGP, we
|
|
|
|
make sure that the openpgp flag is set in the header. (We
|
|
|
|
failsafe the blob type.) */
|
2020-10-20 11:52:16 +02:00
|
|
|
while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2014-10-31 12:15:34 +01:00
|
|
|
if (first_record && for_openpgp
|
|
|
|
&& buffer[4] == KEYBOX_BLOBTYPE_HEADER)
|
2014-10-09 19:10:32 +02:00
|
|
|
{
|
|
|
|
first_record = 0;
|
|
|
|
buffer[7] |= 0x02; /* OpenPGP data may be available. */
|
|
|
|
}
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fwrite (buffer, nread, 1, newfp) != 1)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_ferror (fp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Prepare for delete or update. */
|
2013-08-29 17:39:35 +02:00
|
|
|
if ( mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE )
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
off_t current = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Copy first part to the new file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
while ( current < start_offset )
|
|
|
|
{
|
|
|
|
nbytes = DIM(buffer);
|
|
|
|
if (current + nbytes > start_offset)
|
|
|
|
nbytes = start_offset - current;
|
2020-10-20 11:52:16 +02:00
|
|
|
nread = es_fread (buffer, 1, nbytes, fp);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (!nread)
|
2003-06-05 09:14:21 +02:00
|
|
|
break;
|
|
|
|
current += nread;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fwrite (buffer, nread, 1, newfp) != 1)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_ferror (fp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Skip this blob. */
|
2017-04-01 11:10:47 +02:00
|
|
|
rc = _keybox_read_blob (NULL, fp, NULL);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2021-04-13 07:35:04 +02:00
|
|
|
goto leave;
|
2015-01-30 03:42:52 +01:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Do an insert or update. */
|
2013-08-29 17:39:35 +02:00
|
|
|
if ( mode == FILECOPY_INSERT || mode == FILECOPY_UPDATE )
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_blob (blob, newfp, NULL);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2021-04-13 07:35:04 +02:00
|
|
|
goto leave;
|
2015-01-30 03:42:52 +01:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Copy the rest of the packet for an delete or update. */
|
2013-08-29 17:39:35 +02:00
|
|
|
if (mode == FILECOPY_DELETE || mode == FILECOPY_UPDATE)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
while ( (nread = es_fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fwrite (buffer, nread, 1, newfp) != 1)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_ferror (fp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Close both files. */
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose(fp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose(newfp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
xfree(bakfname);
|
|
|
|
xfree(tmpfname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-30 15:18:45 +02:00
|
|
|
/* Insert the OpenPGP keyblock {IMAGE,IMAGELEN} into HD. */
|
2012-12-28 14:03:16 +01:00
|
|
|
gpg_error_t
|
2017-03-30 15:18:45 +02:00
|
|
|
keybox_insert_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen)
|
2012-12-28 14:03:16 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
const char *fname;
|
|
|
|
KEYBOXBLOB blob;
|
|
|
|
size_t nparsed;
|
|
|
|
struct _keybox_openpgp_info info;
|
|
|
|
|
|
|
|
if (!hd)
|
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
|
|
|
if (!hd->kb)
|
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
|
|
|
|
|
|
|
|
|
|
|
/* Close this one otherwise we will mess up the position for a next
|
|
|
|
search. Fixme: it would be better to adjust the position after
|
|
|
|
the write operation. */
|
|
|
|
_keybox_close_file (hd);
|
|
|
|
|
|
|
|
err = _keybox_parse_openpgp (image, imagelen, &nparsed, &info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
assert (nparsed <= imagelen);
|
|
|
|
err = _keybox_create_openpgp_blob (&blob, &info, image, imagelen,
|
2017-03-30 15:18:45 +02:00
|
|
|
hd->ephemeral);
|
2012-12-28 14:03:16 +01:00
|
|
|
_keybox_destroy_openpgp_info (&info);
|
|
|
|
if (!err)
|
|
|
|
{
|
2014-10-09 19:10:32 +02:00
|
|
|
err = blob_filecopy (FILECOPY_INSERT, fname, blob, hd->secret, 1, 0);
|
2012-12-28 14:03:16 +01:00
|
|
|
_keybox_release_blob (blob);
|
|
|
|
/* if (!rc && !hd->secret && kb_offtbl) */
|
|
|
|
/* { */
|
|
|
|
/* update_offset_hash_table_from_kb (kb_offtbl, kb, 0); */
|
|
|
|
/* } */
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Update the current key at HD with the given OpenPGP keyblock in
|
|
|
|
{IMAGE,IMAGELEN}. */
|
|
|
|
gpg_error_t
|
|
|
|
keybox_update_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen)
|
|
|
|
{
|
2013-11-15 15:54:31 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
const char *fname;
|
|
|
|
off_t off;
|
|
|
|
KEYBOXBLOB blob;
|
|
|
|
size_t nparsed;
|
|
|
|
struct _keybox_openpgp_info info;
|
|
|
|
|
|
|
|
if (!hd || !image || !imagelen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
if (!hd->found.blob)
|
|
|
|
return gpg_error (GPG_ERR_NOTHING_FOUND);
|
2014-10-31 12:15:34 +01:00
|
|
|
if (blob_get_type (hd->found.blob) != KEYBOX_BLOBTYPE_PGP)
|
2013-11-15 15:54:31 +01:00
|
|
|
return gpg_error (GPG_ERR_WRONG_BLOB_TYPE);
|
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
|
|
|
|
|
|
|
off = _keybox_get_blob_fileoffset (hd->found.blob);
|
|
|
|
if (off == (off_t)-1)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
2019-05-14 19:05:58 +02:00
|
|
|
/* Close the file so that we do no mess up the position for a
|
2013-11-15 15:54:31 +01:00
|
|
|
next search. */
|
|
|
|
_keybox_close_file (hd);
|
|
|
|
|
|
|
|
/* Build a new blob. */
|
|
|
|
err = _keybox_parse_openpgp (image, imagelen, &nparsed, &info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
assert (nparsed <= imagelen);
|
|
|
|
err = _keybox_create_openpgp_blob (&blob, &info, image, imagelen,
|
2017-03-30 15:18:45 +02:00
|
|
|
hd->ephemeral);
|
2013-11-15 15:54:31 +01:00
|
|
|
_keybox_destroy_openpgp_info (&info);
|
|
|
|
|
|
|
|
/* Update the keyblock. */
|
|
|
|
if (!err)
|
|
|
|
{
|
2014-10-09 19:10:32 +02:00
|
|
|
err = blob_filecopy (FILECOPY_UPDATE, fname, blob, hd->secret, 1, off);
|
2013-11-15 15:54:31 +01:00
|
|
|
_keybox_release_blob (blob);
|
|
|
|
}
|
|
|
|
return err;
|
2012-12-28 14:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
#ifdef KEYBOX_WITH_X509
|
2003-06-05 09:14:21 +02:00
|
|
|
int
|
2003-12-17 13:27:21 +01:00
|
|
|
keybox_insert_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
|
2003-06-05 09:14:21 +02:00
|
|
|
unsigned char *sha1_digest)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
const char *fname;
|
|
|
|
KEYBOXBLOB blob;
|
|
|
|
|
|
|
|
if (!hd)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Close this one otherwise we will mess up the position for a next
|
2003-06-05 09:14:21 +02:00
|
|
|
search. Fixme: it would be better to adjust the position after
|
2008-04-01 17:08:57 +02:00
|
|
|
the write operation. */
|
|
|
|
_keybox_close_file (hd);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
rc = _keybox_create_x509_blob (&blob, cert, sha1_digest, hd->ephemeral);
|
|
|
|
if (!rc)
|
|
|
|
{
|
2014-10-09 19:10:32 +02:00
|
|
|
rc = blob_filecopy (FILECOPY_INSERT, fname, blob, hd->secret, 0, 0);
|
2003-06-05 09:14:21 +02:00
|
|
|
_keybox_release_blob (blob);
|
|
|
|
/* if (!rc && !hd->secret && kb_offtbl) */
|
|
|
|
/* { */
|
|
|
|
/* update_offset_hash_table_from_kb (kb_offtbl, kb, 0); */
|
|
|
|
/* } */
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-12-17 13:27:21 +01:00
|
|
|
keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
|
2003-06-05 09:14:21 +02:00
|
|
|
unsigned char *sha1_digest)
|
|
|
|
{
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)hd;
|
|
|
|
(void)cert;
|
|
|
|
(void)sha1_digest;
|
2003-06-05 09:14:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /*KEYBOX_WITH_X509*/
|
|
|
|
|
2004-02-02 18:09:35 +01:00
|
|
|
/* Note: We assume that the keybox has been locked before the current
|
|
|
|
search was executed. This is needed so that we can depend on the
|
|
|
|
offset information of the flags. */
|
|
|
|
int
|
|
|
|
keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
|
|
|
|
{
|
|
|
|
off_t off;
|
|
|
|
const char *fname;
|
2020-10-20 11:52:16 +02:00
|
|
|
estream_t fp;
|
2004-02-02 18:09:35 +01:00
|
|
|
gpg_err_code_t ec;
|
|
|
|
size_t flag_pos, flag_size;
|
|
|
|
const unsigned char *buffer;
|
|
|
|
size_t length;
|
|
|
|
|
2008-10-20 15:53:23 +02:00
|
|
|
(void)idx; /* Not yet used. */
|
|
|
|
|
2004-02-02 18:09:35 +01:00
|
|
|
if (!hd)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
if (!hd->found.blob)
|
|
|
|
return gpg_error (GPG_ERR_NOTHING_FOUND);
|
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-02-02 18:09:35 +01:00
|
|
|
if (!hd->found.blob)
|
|
|
|
return gpg_error (GPG_ERR_NOTHING_FOUND);
|
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-02-02 18:09:35 +01:00
|
|
|
|
|
|
|
off = _keybox_get_blob_fileoffset (hd->found.blob);
|
|
|
|
if (off == (off_t)-1)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
|
|
|
|
buffer = _keybox_get_blob_image (hd->found.blob, &length);
|
|
|
|
ec = _keybox_get_flag_location (buffer, length, what, &flag_pos, &flag_size);
|
|
|
|
if (ec)
|
|
|
|
return gpg_error (ec);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-02-02 18:09:35 +01:00
|
|
|
off += flag_pos;
|
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
_keybox_close_file (hd);
|
2020-10-20 11:52:16 +02:00
|
|
|
fp = es_fopen (hd->kb->fname, "r+b");
|
2004-02-02 18:09:35 +01:00
|
|
|
if (!fp)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-02-02 18:09:35 +01:00
|
|
|
|
|
|
|
ec = 0;
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fseeko (fp, off, SEEK_SET))
|
2015-10-14 11:57:26 +02:00
|
|
|
ec = gpg_err_code_from_syserror ();
|
2004-02-02 18:09:35 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned char tmp[4];
|
|
|
|
|
|
|
|
tmp[0] = value >> 24;
|
|
|
|
tmp[1] = value >> 16;
|
|
|
|
tmp[2] = value >> 8;
|
|
|
|
tmp[3] = value;
|
|
|
|
|
|
|
|
switch (flag_size)
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
case 1:
|
2004-02-02 18:09:35 +01:00
|
|
|
case 2:
|
|
|
|
case 4:
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
|
2008-05-06 16:03:36 +02:00
|
|
|
ec = gpg_err_code_from_syserror ();
|
2004-02-02 18:09:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ec = GPG_ERR_BUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose (fp))
|
2004-02-02 18:09:35 +01:00
|
|
|
{
|
|
|
|
if (!ec)
|
2008-05-06 16:03:36 +02:00
|
|
|
ec = gpg_err_code_from_syserror ();
|
2004-02-02 18:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return gpg_error (ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
keybox_delete (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
off_t off;
|
|
|
|
const char *fname;
|
2020-10-20 11:52:16 +02:00
|
|
|
estream_t fp;
|
2003-06-05 09:14:21 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!hd)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
if (!hd->found.blob)
|
|
|
|
return gpg_error (GPG_ERR_NOTHING_FOUND);
|
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
off = _keybox_get_blob_fileoffset (hd->found.blob);
|
|
|
|
if (off == (off_t)-1)
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
|
|
|
off += 4;
|
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
_keybox_close_file (hd);
|
2020-10-20 11:52:16 +02:00
|
|
|
fp = es_fopen (hd->kb->fname, "r+b");
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!fp)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fseeko (fp, off, SEEK_SET))
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
else if (es_fputc (0, fp) == EOF)
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
else
|
|
|
|
rc = 0;
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose (fp))
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
if (!rc)
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Compress the keybox file. This should be run with the file
|
|
|
|
locked. */
|
|
|
|
int
|
|
|
|
keybox_compress (KEYBOX_HANDLE hd)
|
|
|
|
{
|
2020-10-20 10:43:55 +02:00
|
|
|
gpg_err_code_t ec;
|
2004-04-26 10:09:25 +02:00
|
|
|
int read_rc, rc;
|
|
|
|
const char *fname;
|
2020-10-20 11:52:16 +02:00
|
|
|
estream_t fp, newfp;
|
2004-04-26 10:09:25 +02:00
|
|
|
char *bakfname = NULL;
|
|
|
|
char *tmpfname = NULL;
|
|
|
|
int first_blob;
|
|
|
|
KEYBOXBLOB blob = NULL;
|
|
|
|
u32 cut_time;
|
|
|
|
int any_changes = 0;
|
|
|
|
int skipped_deleted;
|
|
|
|
|
|
|
|
if (!hd)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (hd->secret)
|
|
|
|
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
_keybox_close_file (hd);
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
/* Open the source file. Because we do a rename, we have to check the
|
2004-04-26 10:09:25 +02:00
|
|
|
permissions of the file */
|
2020-10-20 10:43:55 +02:00
|
|
|
if ((ec = gnupg_access (fname, W_OK)))
|
|
|
|
return gpg_error (ec);
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
fp = es_fopen (fname, "rb");
|
2004-04-26 10:09:25 +02:00
|
|
|
if (!fp && errno == ENOENT)
|
|
|
|
return 0; /* Ready. File has been deleted right after the access above. */
|
|
|
|
if (!fp)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A quick test to see if we need to compress the file at all. We
|
|
|
|
schedule a compress run after 3 hours. */
|
2017-04-01 11:10:47 +02:00
|
|
|
if ( !_keybox_read_blob (&blob, fp, NULL) )
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
|
|
|
const unsigned char *buffer;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
buffer = _keybox_get_blob_image (blob, &length);
|
2014-10-31 12:15:34 +01:00
|
|
|
if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
2015-02-11 10:27:57 +01:00
|
|
|
u32 last_maint = buf32_to_u32 (buffer+20);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2019-08-23 15:12:13 +02:00
|
|
|
if ( (last_maint + 3*3600) > make_timestamp () )
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
2004-04-26 10:09:25 +02:00
|
|
|
_keybox_release_blob (blob);
|
|
|
|
return 0; /* Compress run not yet needed. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_keybox_release_blob (blob);
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fseek (fp, 0, SEEK_SET);
|
|
|
|
es_clearerr (fp);
|
2004-04-26 10:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the new file. */
|
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
2004-04-26 10:09:25 +02:00
|
|
|
return rc;;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Processing loop. By reading using _keybox_read_blob we
|
2007-03-20 17:57:40 +01:00
|
|
|
automagically skip any blobs flagged as deleted. Thus what we
|
2004-04-26 10:09:25 +02:00
|
|
|
only have to do is to check all ephemeral flagged blocks whether
|
|
|
|
their time has come and write out all other blobs. */
|
2019-08-23 15:12:13 +02:00
|
|
|
cut_time = make_timestamp () - 86400;
|
2004-04-26 10:09:25 +02:00
|
|
|
first_blob = 1;
|
|
|
|
skipped_deleted = 0;
|
2017-04-01 11:10:47 +02:00
|
|
|
for (rc=0; !(read_rc = _keybox_read_blob (&blob, fp, &skipped_deleted));
|
2004-04-26 10:09:25 +02:00
|
|
|
_keybox_release_blob (blob), blob = NULL )
|
|
|
|
{
|
|
|
|
unsigned int blobflags;
|
|
|
|
const unsigned char *buffer;
|
|
|
|
size_t length, pos, size;
|
|
|
|
u32 created_at;
|
|
|
|
|
|
|
|
if (skipped_deleted)
|
|
|
|
any_changes = 1;
|
|
|
|
buffer = _keybox_get_blob_image (blob, &length);
|
|
|
|
if (first_blob)
|
|
|
|
{
|
|
|
|
first_blob = 0;
|
2014-10-31 12:15:34 +01:00
|
|
|
if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
2014-10-09 19:10:32 +02:00
|
|
|
/* Write out the blob with an updated maintenance time
|
|
|
|
stamp and if needed (ie. used by gpg) set the openpgp
|
|
|
|
flag. */
|
|
|
|
_keybox_update_header_blob (blob, hd->for_openpgp);
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_blob (blob, newfp, NULL);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The header blob is missing. Insert it. */
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_header_blob (newfp, hd->for_openpgp);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
any_changes = 1;
|
|
|
|
}
|
2014-10-31 12:15:34 +01:00
|
|
|
else if (length > 4 && buffer[4] == KEYBOX_BLOBTYPE_HEADER)
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
|
|
|
/* Oops: There is another header record - remove it. */
|
|
|
|
any_changes = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
if (_keybox_get_flag_location (buffer, length,
|
2004-04-26 10:09:25 +02:00
|
|
|
KEYBOX_FLAG_BLOB, &pos, &size)
|
|
|
|
|| size != 2)
|
|
|
|
{
|
|
|
|
rc = gpg_error (GPG_ERR_BUG);
|
|
|
|
break;
|
|
|
|
}
|
2015-02-11 10:27:57 +01:00
|
|
|
blobflags = buf16_to_uint (buffer+pos);
|
2007-03-20 17:57:40 +01:00
|
|
|
if ((blobflags & KEYBOX_FLAG_BLOB_EPHEMERAL))
|
2004-04-26 10:09:25 +02:00
|
|
|
{
|
|
|
|
/* This is an ephemeral blob. */
|
2011-02-04 12:57:53 +01:00
|
|
|
if (_keybox_get_flag_location (buffer, length,
|
2004-04-26 10:09:25 +02:00
|
|
|
KEYBOX_FLAG_CREATED_AT, &pos, &size)
|
|
|
|
|| size != 4)
|
|
|
|
created_at = 0; /* oops. */
|
|
|
|
else
|
2015-02-11 10:27:57 +01:00
|
|
|
created_at = buf32_to_u32 (buffer+pos);
|
2004-04-26 10:09:25 +02:00
|
|
|
|
|
|
|
if (created_at && created_at < cut_time)
|
|
|
|
{
|
|
|
|
any_changes = 1;
|
|
|
|
continue; /* Skip this blob. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
rc = _keybox_write_blob (blob, newfp, NULL);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (skipped_deleted)
|
|
|
|
any_changes = 1;
|
|
|
|
_keybox_release_blob (blob); blob = NULL;
|
|
|
|
if (!rc && read_rc == -1)
|
|
|
|
rc = 0;
|
|
|
|
else if (!rc)
|
|
|
|
rc = read_rc;
|
|
|
|
|
|
|
|
/* Close both files. */
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose(fp) && !rc)
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2020-10-20 11:52:16 +02:00
|
|
|
if (es_fclose(newfp) && !rc)
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02:00
|
|
|
|
|
|
|
/* Rename or remove the temporary file. */
|
|
|
|
if (rc || !any_changes)
|
2010-03-24 13:15:30 +01:00
|
|
|
gnupg_remove (tmpfname);
|
2004-04-26 10:09:25 +02:00
|
|
|
else
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, hd->secret);
|
|
|
|
|
|
|
|
xfree(bakfname);
|
|
|
|
xfree(tmpfname);
|
|
|
|
return rc;
|
|
|
|
}
|