2008-11-20 16:26:40 +00:00
|
|
|
/* keybox-file.c - File operations
|
2003-06-05 07:14:21 +00:00
|
|
|
* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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 "keybox-defs.h"
|
|
|
|
|
* 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
|
|
|
|
2014-10-09 20:19:05 +02:00
|
|
|
#define IMAGELEN_LIMIT (2*1024*1024)
|
|
|
|
|
|
|
|
|
* 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_FTELLO) && !defined(ftello)
|
|
|
|
static off_t
|
|
|
|
ftello (FILE *stream)
|
|
|
|
{
|
|
|
|
long int off;
|
|
|
|
|
|
|
|
off = ftell (stream);
|
|
|
|
if (off == -1)
|
|
|
|
return (off_t)-1;
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
#endif /* !defined(HAVE_FTELLO) && !defined(ftello) */
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
/* Read a block at the current postion and return it in r_blob.
|
2013-11-15 15:54:31 +01:00
|
|
|
r_blob may be NULL to simply skip the current block. */
|
2003-06-05 07:14:21 +00:00
|
|
|
int
|
2004-04-26 08:09:25 +00:00
|
|
|
_keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
|
2003-06-05 07:14:21 +00:00
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
unsigned char *image;
|
2003-06-05 07:14:21 +00:00
|
|
|
size_t imagelen = 0;
|
|
|
|
int c1, c2, c3, c4, type;
|
|
|
|
int rc;
|
|
|
|
off_t off;
|
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
*skipped_deleted = 0;
|
2003-06-05 07:14:21 +00:00
|
|
|
again:
|
2013-11-15 15:54:31 +01:00
|
|
|
if (r_blob)
|
|
|
|
*r_blob = NULL;
|
2003-06-05 07:14:21 +00:00
|
|
|
off = ftello (fp);
|
|
|
|
if (off == (off_t)-1)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
if ((c1 = getc (fp)) == EOF
|
|
|
|
|| (c2 = getc (fp)) == EOF
|
|
|
|
|| (c3 = getc (fp)) == EOF
|
|
|
|
|| (c4 = getc (fp)) == EOF
|
|
|
|
|| (type = getc (fp)) == EOF)
|
|
|
|
{
|
|
|
|
if ( c1 == EOF && !ferror (fp) )
|
|
|
|
return -1; /* eof */
|
2008-05-06 14:03:36 +00:00
|
|
|
if (!ferror (fp))
|
|
|
|
return gpg_error (GPG_ERR_TOO_SHORT);
|
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
|
2011-02-04 12:57:53 +01:00
|
|
|
if (imagelen < 5)
|
2003-06-05 07:14:21 +00:00
|
|
|
return gpg_error (GPG_ERR_TOO_SHORT);
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
{
|
2004-04-26 08:09:25 +00:00
|
|
|
/* Special treatment for empty blobs. */
|
2003-06-05 07:14:21 +00:00
|
|
|
if (fseek (fp, imagelen-5, SEEK_CUR))
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
*skipped_deleted = 1;
|
2003-06-05 07:14:21 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2014-10-09 20:19:05 +02:00
|
|
|
if (imagelen > IMAGELEN_LIMIT) /* Sanity check. */
|
|
|
|
{
|
|
|
|
/* Seek forward so that the caller may choose to ignore this
|
|
|
|
record. */
|
|
|
|
if (fseek (fp, imagelen-5, SEEK_CUR))
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
return gpg_error (GPG_ERR_TOO_LARGE);
|
|
|
|
}
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
image = xtrymalloc (imagelen);
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!image)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
|
|
|
|
if (fread (image+5, imagelen-5, 1, fp) != 1)
|
|
|
|
{
|
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 (image);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
|
|
|
|
if (rc || !r_blob)
|
2008-05-06 14:03:36 +00:00
|
|
|
xfree (image);
|
2003-06-05 07:14:21 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
int
|
|
|
|
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
|
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
return _keybox_read_blob2 (r_blob, fp, &dummy);
|
|
|
|
}
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
/* Write the block to the current file position */
|
|
|
|
int
|
|
|
|
_keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const unsigned char *image;
|
2003-06-05 07:14:21 +00:00
|
|
|
size_t length;
|
|
|
|
|
|
|
|
image = _keybox_get_blob_image (blob, &length);
|
2014-10-09 20:19:05 +02:00
|
|
|
|
|
|
|
if (length > IMAGELEN_LIMIT)
|
|
|
|
return gpg_error (GPG_ERR_TOO_LARGE);
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
if (fwrite (image, length, 1, fp) != 1)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2003-06-05 07:14:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-04-26 08:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Write a fresh header type blob. */
|
|
|
|
int
|
2014-10-09 19:10:32 +02:00
|
|
|
_keybox_write_header_blob (FILE *fp, int for_openpgp)
|
2004-04-26 08:09:25 +00:00
|
|
|
{
|
|
|
|
unsigned char image[32];
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
memset (image, 0, sizeof image);
|
|
|
|
/* Length of this blob. */
|
|
|
|
image[3] = 32;
|
|
|
|
|
2014-10-31 12:15:34 +01:00
|
|
|
image[4] = KEYBOX_BLOBTYPE_HEADER;
|
2004-04-26 08:09:25 +00:00
|
|
|
image[5] = 1; /* Version */
|
2014-10-09 19:10:32 +02:00
|
|
|
if (for_openpgp)
|
|
|
|
image[7] = 0x02; /* OpenPGP data may be available. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-04-26 08:09:25 +00:00
|
|
|
memcpy (image+8, "KBXf", 4);
|
|
|
|
val = time (NULL);
|
|
|
|
/* created_at and last maintenance run. */
|
|
|
|
image[16] = (val >> 24);
|
|
|
|
image[16+1] = (val >> 16);
|
|
|
|
image[16+2] = (val >> 8);
|
|
|
|
image[16+3] = (val );
|
|
|
|
image[20] = (val >> 24);
|
|
|
|
image[20+1] = (val >> 16);
|
|
|
|
image[20+2] = (val >> 8);
|
|
|
|
image[20+3] = (val );
|
|
|
|
|
|
|
|
if (fwrite (image, 32, 1, fp) != 1)
|
2008-05-06 14:03:36 +00:00
|
|
|
return gpg_error_from_syserror ();
|
2004-04-26 08:09:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|