2003-06-05 07:14:21 +00: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 07:14:21 +00: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 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00: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
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2004-04-26 08:09:25 +00:00
|
|
|
#include <time.h>
|
2003-06-05 07:14:21 +00:00
|
|
|
#include <unistd.h>
|
2012-12-28 14:03:16 +01:00
|
|
|
#include <assert.h>
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
#include "keybox-defs.h"
|
2010-03-24 12:15:30 +00:00
|
|
|
#include "../common/sysutils.h"
|
2015-02-11 10:27:57 +01:00
|
|
|
#include "../common/host2net.h"
|
2003-06-05 07:14:21 +00: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 07:14:21 +00: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 15:46:01 +00: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 07:14:21 +00:00
|
|
|
static int
|
|
|
|
create_tmp_file (const char *template,
|
|
|
|
char **r_bakfname, char **r_tmpfname, FILE **r_fp)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
char *bakfname, *tmpfname;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
*r_bakfname = NULL;
|
|
|
|
*r_tmpfname = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
# ifdef USE_ONLY_8DOT3
|
|
|
|
/* Here is another Windoze bug?:
|
|
|
|
* you cant rename("pubring.kbx.tmp", "pubring.kbx");
|
|
|
|
* but rename("pubring.kbx.tmp", "pubring.aaa");
|
2008-11-20 16:26:40 +00:00
|
|
|
* works. So we replace ".kbx" by ".kb_" or ".k__". Note that we
|
|
|
|
* can't use ".bak" and ".tmp", because these suffixes are used by
|
|
|
|
* gpg and would lead to a sharing violation or data corruption.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
if (strlen (template) > 4
|
|
|
|
&& !strcmp (template+strlen(template)-4, EXTSEP_S "kbx") )
|
|
|
|
{
|
|
|
|
bakfname = xtrymalloc (strlen (template) + 1);
|
|
|
|
if (!bakfname)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
strcpy (bakfname, template);
|
2008-11-20 16:26:40 +00:00
|
|
|
strcpy (bakfname+strlen(template)-4, EXTSEP_S "kb_");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
tmpfname = xtrymalloc (strlen (template) + 1);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
|
|
|
strcpy (tmpfname,template);
|
2008-11-20 16:26:40 +00:00
|
|
|
strcpy (tmpfname + strlen (template)-4, EXTSEP_S "k__");
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
else
|
2008-11-20 16:26:40 +00:00
|
|
|
{ /* File does not end with kbx, thus we hope we are working on a
|
|
|
|
modern file system and appending a suffix works. */
|
2003-06-05 07:14:21 +00:00
|
|
|
bakfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!bakfname)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2008-11-20 16:26:40 +00:00
|
|
|
strcpy (stpcpy (bakfname, template), EXTSEP_S "kb_");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
tmpfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2008-11-20 16:26:40 +00:00
|
|
|
strcpy (stpcpy (tmpfname, template), EXTSEP_S "k__");
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
# else /* Posix file names */
|
|
|
|
bakfname = xtrymalloc (strlen (template) + 2);
|
|
|
|
if (!bakfname)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
strcpy (stpcpy (bakfname,template),"~");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
tmpfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
|
|
|
strcpy (stpcpy (tmpfname,template), EXTSEP_S "tmp");
|
|
|
|
# endif /* Posix filename */
|
|
|
|
|
|
|
|
*r_fp = fopen (tmpfname, "wb");
|
|
|
|
if (!*r_fp)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
xfree (tmpfname);
|
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2008-04-01 15:08:57 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
*r_bakfname = bakfname;
|
|
|
|
*r_tmpfname = tmpfname;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
rename_tmp_file (const char *bakfname, const char *tmpfname,
|
|
|
|
const char *fname, int secret )
|
|
|
|
{
|
|
|
|
int rc=0;
|
|
|
|
|
|
|
|
/* 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 07:14:21 +00:00
|
|
|
/* tmpfname, strerror(errno) ); */
|
|
|
|
/* return KEYBOX_Write_File; */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fixme: invalidate close caches (not used with stdio)*/
|
2010-03-08 17:05:37 +00: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 07:14:21 +00:00
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
/* First make a backup file except for secret keyboxes. */
|
2003-06-05 07:14:21 +00:00
|
|
|
if (!secret)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
2010-03-24 12:15:30 +00:00
|
|
|
gnupg_remove (bakfname);
|
2003-06-05 07:14:21 +00:00
|
|
|
#endif
|
|
|
|
if (rename (fname, bakfname) )
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
/* Then rename the file. */
|
2003-06-05 07:14:21 +00:00
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
2010-03-24 12:15:30 +00:00
|
|
|
gnupg_remove (fname);
|
2003-06-05 07:14:21 +00:00
|
|
|
#endif
|
|
|
|
if (rename (tmpfname, fname) )
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
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"); */
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 07:14:21 +00: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 07:14:21 +00:00
|
|
|
{
|
|
|
|
FILE *fp, *newfp;
|
|
|
|
int rc=0;
|
|
|
|
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 07:14:21 +00: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 07:14:21 +00:00
|
|
|
permissions of the file */
|
|
|
|
if (access (fname, W_OK))
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
fp = 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 08:09:25 +00:00
|
|
|
/* Insert mode but file does not exist:
|
|
|
|
Create a new keybox file. */
|
2003-06-05 07:14:21 +00:00
|
|
|
newfp = fopen (fname, "wb");
|
|
|
|
if (!newfp )
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
|
2014-10-09 19:10:32 +02:00
|
|
|
rc = _keybox_write_header_blob (newfp, for_openpgp);
|
2004-04-26 08:09:25 +00:00
|
|
|
if (rc)
|
2015-01-30 11:42:52 +09:00
|
|
|
{
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
2015-01-30 11:42:52 +09:00
|
|
|
{
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2004-04-26 08:09:25 +00:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
if ( fclose (newfp) )
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00: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 08:09:25 +00:00
|
|
|
return 0; /* Ready. */
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Create the new file. */
|
2003-06-05 07:14:21 +00:00
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00: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.) */
|
2003-06-05 07:14:21 +00:00
|
|
|
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
|
|
|
{
|
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. */
|
|
|
|
}
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00: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 07:14:21 +00:00
|
|
|
off_t current = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Copy first part to the new file. */
|
2003-06-05 07:14:21 +00:00
|
|
|
while ( current < start_offset )
|
|
|
|
{
|
|
|
|
nbytes = DIM(buffer);
|
|
|
|
if (current + nbytes > start_offset)
|
|
|
|
nbytes = start_offset - current;
|
|
|
|
nread = fread (buffer, 1, nbytes, fp);
|
2004-04-26 08:09:25 +00:00
|
|
|
if (!nread)
|
2003-06-05 07:14:21 +00:00
|
|
|
break;
|
|
|
|
current += nread;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Skip this blob. */
|
2003-06-05 07:14:21 +00:00
|
|
|
rc = _keybox_read_blob (NULL, fp);
|
|
|
|
if (rc)
|
2015-01-30 11:42:52 +09:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00: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
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
2015-01-30 11:42:52 +09:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
return rc;
|
2015-01-30 11:42:52 +09:00
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00: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
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
|
|
|
{
|
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Close both files. */
|
2003-06-05 07:14:21 +00:00
|
|
|
if (fclose(fp))
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
fclose (newfp);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if (fclose(newfp))
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
xfree(bakfname);
|
|
|
|
xfree(tmpfname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-28 17:17:56 +01:00
|
|
|
/* Insert the OpenPGP keyblock {IMAGE,IMAGELEN} into HD. SIGSTATUS is
|
|
|
|
a vector describing the status of the signatures; its first element
|
|
|
|
gives the number of following elements. */
|
2012-12-28 14:03:16 +01:00
|
|
|
gpg_error_t
|
2012-12-28 17:17:56 +01:00
|
|
|
keybox_insert_keyblock (KEYBOX_HANDLE hd, const void *image, size_t imagelen,
|
|
|
|
u32 *sigstatus)
|
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,
|
2012-12-28 17:17:56 +01:00
|
|
|
sigstatus, 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);
|
|
|
|
|
|
|
|
/* Close this the file so that we do no mess up the position for a
|
|
|
|
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,
|
|
|
|
NULL, hd->ephemeral);
|
|
|
|
_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 07:14:21 +00:00
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
#ifdef KEYBOX_WITH_X509
|
2003-06-05 07:14:21 +00:00
|
|
|
int
|
2003-12-17 12:27:21 +00:00
|
|
|
keybox_insert_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
|
2003-06-05 07:14:21 +00: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 07:14:21 +00:00
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 07:14:21 +00: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 07:14:21 +00:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Close this one otherwise we will mess up the position for a next
|
2003-06-05 07:14:21 +00:00
|
|
|
search. Fixme: it would be better to adjust the position after
|
2008-04-01 15:08:57 +00:00
|
|
|
the write operation. */
|
|
|
|
_keybox_close_file (hd);
|
2003-06-05 07:14:21 +00: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 07:14:21 +00: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 12:27:21 +00:00
|
|
|
keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
|
2003-06-05 07:14:21 +00:00
|
|
|
unsigned char *sha1_digest)
|
|
|
|
{
|
2008-10-20 13:53:23 +00:00
|
|
|
(void)hd;
|
|
|
|
(void)cert;
|
|
|
|
(void)sha1_digest;
|
2003-06-05 07:14:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /*KEYBOX_WITH_X509*/
|
|
|
|
|
2004-02-02 17:09:35 +00: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;
|
|
|
|
FILE *fp;
|
|
|
|
gpg_err_code_t ec;
|
|
|
|
size_t flag_pos, flag_size;
|
|
|
|
const unsigned char *buffer;
|
|
|
|
size_t length;
|
|
|
|
|
2008-10-20 13:53:23 +00:00
|
|
|
(void)idx; /* Not yet used. */
|
|
|
|
|
2004-02-02 17:09:35 +00: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 17:09:35 +00: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 17:09:35 +00: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 17:09:35 +00:00
|
|
|
off += flag_pos;
|
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
_keybox_close_file (hd);
|
2004-02-02 17:09:35 +00:00
|
|
|
fp = fopen (hd->kb->fname, "r+b");
|
|
|
|
if (!fp)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2004-02-02 17:09:35 +00:00
|
|
|
|
|
|
|
ec = 0;
|
|
|
|
if (fseeko (fp, off, SEEK_SET))
|
2008-05-06 14:03:36 +00:00
|
|
|
ec = gpg_error_from_syserror ();
|
2004-02-02 17:09:35 +00: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 17:09:35 +00:00
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
if (fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
|
2008-05-06 14:03:36 +00:00
|
|
|
ec = gpg_err_code_from_syserror ();
|
2004-02-02 17:09:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ec = GPG_ERR_BUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose (fp))
|
|
|
|
{
|
|
|
|
if (!ec)
|
2008-05-06 14:03:36 +00:00
|
|
|
ec = gpg_err_code_from_syserror ();
|
2004-02-02 17:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gpg_error (ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
keybox_delete (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
off_t off;
|
|
|
|
const char *fname;
|
|
|
|
FILE *fp;
|
|
|
|
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 07:14:21 +00: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 07:14:21 +00: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 15:08:57 +00:00
|
|
|
_keybox_close_file (hd);
|
2003-06-05 07:14:21 +00:00
|
|
|
fp = fopen (hd->kb->fname, "r+b");
|
|
|
|
if (!fp)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
if (fseeko (fp, off, SEEK_SET))
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
else if (putc (0, fp) == EOF)
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
else
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
if (fclose (fp))
|
|
|
|
{
|
|
|
|
if (!rc)
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Compress the keybox file. This should be run with the file
|
|
|
|
locked. */
|
|
|
|
int
|
|
|
|
keybox_compress (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
int read_rc, rc;
|
|
|
|
const char *fname;
|
|
|
|
FILE *fp, *newfp;
|
|
|
|
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 08:09:25 +00:00
|
|
|
if (!hd->kb)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-04-26 08:09:25 +00: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 08:09:25 +00:00
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
_keybox_close_file (hd);
|
2004-04-26 08:09:25 +00: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 08:09:25 +00:00
|
|
|
permissions of the file */
|
|
|
|
if (access (fname, W_OK))
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
|
|
|
|
fp = fopen (fname, "rb");
|
|
|
|
if (!fp && errno == ENOENT)
|
|
|
|
return 0; /* Ready. File has been deleted right after the access above. */
|
|
|
|
if (!fp)
|
|
|
|
{
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00: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. */
|
|
|
|
if ( !_keybox_read_blob (&blob, fp) )
|
|
|
|
{
|
|
|
|
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 08:09:25 +00: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
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
if ( (last_maint + 3*3600) > time (NULL) )
|
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
_keybox_release_blob (blob);
|
|
|
|
return 0; /* Compress run not yet needed. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_keybox_release_blob (blob);
|
2010-03-24 12:15:30 +00:00
|
|
|
fseek (fp, 0, SEEK_SET);
|
|
|
|
clearerr (fp);
|
2004-04-26 08:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the new file. */
|
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2015-01-30 11:42:52 +09:00
|
|
|
fclose (fp);
|
2004-04-26 08:09:25 +00:00
|
|
|
return rc;;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Processing loop. By reading using _keybox_read_blob we
|
2007-03-20 16:57:40 +00:00
|
|
|
automagically skip any blobs flagged as deleted. Thus what we
|
2004-04-26 08:09:25 +00:00
|
|
|
only have to do is to check all ephemeral flagged blocks whether
|
|
|
|
their time has come and write out all other blobs. */
|
|
|
|
cut_time = time(NULL) - 86400;
|
|
|
|
first_blob = 1;
|
|
|
|
skipped_deleted = 0;
|
|
|
|
for (rc=0; !(read_rc = _keybox_read_blob2 (&blob, fp, &skipped_deleted));
|
|
|
|
_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 08:09:25 +00: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);
|
2004-04-26 08:09:25 +00:00
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The header blob is missing. Insert it. */
|
2014-10-09 19:10:32 +02:00
|
|
|
rc = _keybox_write_header_blob (newfp, hd->for_openpgp);
|
2004-04-26 08:09:25 +00: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 08:09:25 +00: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 08:09:25 +00: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 16:57:40 +00:00
|
|
|
if ((blobflags & KEYBOX_FLAG_BLOB_EPHEMERAL))
|
2004-04-26 08:09:25 +00:00
|
|
|
{
|
|
|
|
/* This is an ephemeral blob. */
|
2011-02-04 12:57:53 +01:00
|
|
|
if (_keybox_get_flag_location (buffer, length,
|
2004-04-26 08:09:25 +00: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 08:09:25 +00:00
|
|
|
|
|
|
|
if (created_at && created_at < cut_time)
|
|
|
|
{
|
|
|
|
any_changes = 1;
|
|
|
|
continue; /* Skip this blob. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
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. */
|
|
|
|
if (fclose(fp) && !rc)
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
if (fclose(newfp) && !rc)
|
2008-05-06 14:03:36 +00:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
|
|
|
|
/* Rename or remove the temporary file. */
|
|
|
|
if (rc || !any_changes)
|
2010-03-24 12:15:30 +00:00
|
|
|
gnupg_remove (tmpfname);
|
2004-04-26 08:09:25 +00:00
|
|
|
else
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, hd->secret);
|
|
|
|
|
|
|
|
xfree(bakfname);
|
|
|
|
xfree(tmpfname);
|
|
|
|
return rc;
|
|
|
|
}
|