2003-06-05 09:14:21 +02:00
|
|
|
/* keybox-update.c - keybox update operations
|
2004-04-26 10:09:25 +02:00
|
|
|
* Copyright (C) 2001, 2003, 2004 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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://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>
|
|
|
|
|
|
|
|
#include "keybox-defs.h"
|
2015-02-12 20:34:44 +01:00
|
|
|
#include "../include/host2net.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
#define EXTSEP_S "."
|
|
|
|
|
|
|
|
|
* 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,
|
|
|
|
char **r_bakfname, char **r_tmpfname, FILE **r_fp)
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
char *bakfname, *tmpfname;
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
*r_bakfname = NULL;
|
|
|
|
*r_tmpfname = NULL;
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02: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 17:26:40 +01: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 09:14:21 +02:00
|
|
|
*/
|
|
|
|
if (strlen (template) > 4
|
|
|
|
&& !strcmp (template+strlen(template)-4, EXTSEP_S "kbx") )
|
|
|
|
{
|
|
|
|
bakfname = xtrymalloc (strlen (template) + 1);
|
|
|
|
if (!bakfname)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
strcpy (bakfname, template);
|
2008-11-20 17:26:40 +01:00
|
|
|
strcpy (bakfname+strlen(template)-4, EXTSEP_S "kb_");
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
tmpfname = xtrymalloc (strlen (template) + 1);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
|
|
|
strcpy (tmpfname,template);
|
2008-11-20 17:26:40 +01:00
|
|
|
strcpy (tmpfname + strlen (template)-4, EXTSEP_S "k__");
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
else
|
2008-11-20 17:26:40 +01: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 09:14:21 +02:00
|
|
|
bakfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!bakfname)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2008-11-20 17:26:40 +01:00
|
|
|
strcpy (stpcpy (bakfname, template), EXTSEP_S "kb_");
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
tmpfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2008-11-20 17:26:40 +01:00
|
|
|
strcpy (stpcpy (tmpfname, template), EXTSEP_S "k__");
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
# else /* Posix file names */
|
|
|
|
bakfname = xtrymalloc (strlen (template) + 2);
|
|
|
|
if (!bakfname)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
strcpy (stpcpy (bakfname,template),"~");
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
tmpfname = xtrymalloc ( strlen (template) + 5);
|
|
|
|
if (!tmpfname)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02: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 16:03:36 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
xfree (tmpfname);
|
|
|
|
xfree (bakfname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2008-04-01 17:08:57 +02:00
|
|
|
|
2003-06-05 09:14:21 +02: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) ) */
|
|
|
|
/* { */
|
|
|
|
/* log_debug ("chmod of `%s' failed: %s\n", */
|
|
|
|
/* tmpfname, strerror(errno) ); */
|
|
|
|
/* return KEYBOX_Write_File; */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fixme: invalidate close caches (not used with stdio)*/
|
|
|
|
/* iobuf_ioctl (NULL, 2, 0, (char*)tmpfname ); */
|
|
|
|
/* iobuf_ioctl (NULL, 2, 0, (char*)bakfname ); */
|
|
|
|
/* iobuf_ioctl (NULL, 2, 0, (char*)fname ); */
|
|
|
|
|
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)
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
|
|
|
remove (bakfname);
|
|
|
|
#endif
|
|
|
|
if (rename (fname, bakfname) )
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2008-04-01 17:08:57 +02:00
|
|
|
/* Then rename the file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
|
|
|
remove (fname);
|
|
|
|
#endif
|
|
|
|
if (rename (tmpfname, fname) )
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02: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;
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Perform insert/delete/update operation.
|
|
|
|
mode 1 = insert
|
|
|
|
2 = delete
|
|
|
|
3 = update
|
|
|
|
*/
|
|
|
|
static int
|
2015-02-12 20:34:44 +01:00
|
|
|
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
|
2008-10-20 15:53:23 +02:00
|
|
|
int secret, off_t start_offset)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
FILE *fp, *newfp;
|
|
|
|
int rc=0;
|
|
|
|
char *bakfname = NULL;
|
|
|
|
char *tmpfname = NULL;
|
|
|
|
char buffer[4096];
|
|
|
|
int nread, nbytes;
|
|
|
|
|
2015-02-12 20:34:44 +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 */
|
|
|
|
if (access (fname, W_OK))
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
fp = fopen (fname, "rb");
|
|
|
|
if (mode == 1 && !fp && errno == ENOENT)
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Insert mode but file does not exist:
|
|
|
|
Create a new keybox file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
newfp = fopen (fname, "wb");
|
|
|
|
if (!newfp )
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02:00
|
|
|
|
|
|
|
rc = _keybox_write_header_blob (newfp);
|
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2004-04-26 10:09:25 +02:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if ( 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;
|
|
|
|
}
|
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Create the new file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
/* prepare for insert */
|
|
|
|
if (mode == 1)
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Copy everything to the new file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
|
|
|
{
|
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Prepare for delete or update. */
|
2015-02-12 20:34:44 +01:00
|
|
|
if ( mode == 2 || mode == 3 )
|
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
off_t current = 0;
|
2015-02-12 20:34:44 +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;
|
|
|
|
nread = 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;
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Skip this blob. */
|
2003-06-05 09:14:21 +02:00
|
|
|
rc = _keybox_read_blob (NULL, fp);
|
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
|
|
|
return rc;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Do an insert or update. */
|
2003-06-05 09:14:21 +02:00
|
|
|
if ( mode == 1 || mode == 3 )
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
2015-01-30 03:42:52 +01:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
2015-01-30 03:42:52 +01:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Copy the rest of the packet for an delete or update. */
|
2003-06-05 09:14:21 +02:00
|
|
|
if (mode == 2 || mode == 3)
|
2015-02-12 20:34:44 +01:00
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
while ( (nread = fread (buffer, 1, DIM(buffer), fp)) > 0 )
|
|
|
|
{
|
|
|
|
if (fwrite (buffer, nread, 1, newfp) != 1)
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror (fp))
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
|
|
|
fclose (newfp);
|
2003-06-05 09:14:21 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 20:34:44 +01:00
|
|
|
|
2004-04-26 10:09:25 +02:00
|
|
|
/* Close both files. */
|
2003-06-05 09:14:21 +02:00
|
|
|
if (fclose(fp))
|
|
|
|
{
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
fclose (newfp);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if (fclose(newfp))
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-12 20:34:44 +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)
|
2015-02-12 20:34:44 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!hd->kb)
|
2015-02-12 20:34:44 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2015-02-12 20:34:44 +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)
|
|
|
|
{
|
2008-10-20 15:53:23 +02:00
|
|
|
rc = blob_filecopy (1, fname, blob, hd->secret, 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;
|
|
|
|
FILE *fp;
|
|
|
|
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)
|
2015-02-12 20:34:44 +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)
|
2015-02-12 20:34:44 +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);
|
2015-02-12 20:34:44 +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);
|
2004-02-02 18:09:35 +01:00
|
|
|
fp = fopen (hd->kb->fname, "r+b");
|
|
|
|
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;
|
|
|
|
if (fseeko (fp, off, SEEK_SET))
|
2008-05-06 16:03:36 +02:00
|
|
|
ec = gpg_error_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)
|
|
|
|
{
|
2015-02-12 20:34:44 +01:00
|
|
|
case 1:
|
2004-02-02 18:09:35 +01:00
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose (fp))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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)
|
2015-02-12 20:34:44 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 09:14:21 +02:00
|
|
|
fname = hd->kb->fname;
|
|
|
|
if (!fname)
|
2015-02-12 20:34:44 +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);
|
2003-06-05 09:14:21 +02:00
|
|
|
fp = fopen (hd->kb->fname, "r+b");
|
|
|
|
if (!fp)
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (fseeko (fp, off, SEEK_SET))
|
2008-05-06 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (putc (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;
|
|
|
|
|
|
|
|
if (fclose (fp))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
2015-02-12 20:34:44 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2004-04-26 10:09:25 +02:00
|
|
|
if (!hd->kb)
|
2015-02-12 20:34:44 +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)
|
2015-02-12 20:34:44 +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
|
|
|
|
2015-02-12 20:34:44 +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 */
|
|
|
|
if (access (fname, W_OK))
|
2008-05-06 16:03:36 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02: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 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. */
|
|
|
|
if ( !_keybox_read_blob (&blob, fp) )
|
|
|
|
{
|
|
|
|
const unsigned char *buffer;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
buffer = _keybox_get_blob_image (blob, &length);
|
|
|
|
if (length > 4 && buffer[4] == BLOBTYPE_HEADER)
|
|
|
|
{
|
2015-02-12 20:34:44 +01:00
|
|
|
u32 last_maint = buf32_to_u32 (buffer+20);
|
|
|
|
|
2004-04-26 10:09:25 +02: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);
|
|
|
|
rewind (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the new file. */
|
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
if (rc)
|
|
|
|
{
|
2015-01-30 03:42:52 +01:00
|
|
|
fclose (fp);
|
2004-04-26 10:09:25 +02:00
|
|
|
return rc;;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:34:44 +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. */
|
|
|
|
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;
|
|
|
|
if (length > 4 && buffer[4] == BLOBTYPE_HEADER)
|
|
|
|
{
|
|
|
|
/* Write out the blob with an updated maintenance time stamp. */
|
|
|
|
_keybox_update_header_blob (blob);
|
|
|
|
rc = _keybox_write_blob (blob, newfp);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The header blob is missing. Insert it. */
|
|
|
|
rc = _keybox_write_header_blob (newfp);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
any_changes = 1;
|
|
|
|
}
|
|
|
|
else if (length > 4 && buffer[4] == BLOBTYPE_HEADER)
|
|
|
|
{
|
|
|
|
/* Oops: There is another header record - remove it. */
|
|
|
|
any_changes = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:34:44 +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-12 20:34:44 +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. */
|
2015-02-12 20:34:44 +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-12 20:34:44 +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. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:03:36 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2004-04-26 10:09:25 +02:00
|
|
|
if (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)
|
|
|
|
remove (tmpfname);
|
|
|
|
else
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, hd->secret);
|
|
|
|
|
|
|
|
xfree(bakfname);
|
|
|
|
xfree(tmpfname);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|