2002-06-29 15:46:34 +02:00
|
|
|
|
/* keyring.c - keyring file handling
|
2009-03-20 21:55:47 +01:00
|
|
|
|
* Copyright (C) 2001, 2004, 2007, 2009 Free Software Foundation, Inc.
|
2002-06-29 15:46:34 +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-10-23 12:48:09 +02:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2002-06-29 15:46:34 +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-10-23 12:48:09 +02:00
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2002-06-29 15:46:34 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "keyring.h"
|
|
|
|
|
#include "packet.h"
|
2012-01-10 15:16:44 +01:00
|
|
|
|
#include "keydb.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
|
#include "options.h"
|
|
|
|
|
#include "main.h" /*for check_key_signature()*/
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
/* off_item is a funny named for an object used to keep track of known
|
|
|
|
|
* keys. The idea was to use the offset to seek to the known keyblock, but
|
|
|
|
|
* this is not possible if more than one process is using the keyring.
|
|
|
|
|
*/
|
|
|
|
|
struct off_item {
|
|
|
|
|
struct off_item *next;
|
|
|
|
|
u32 kid[2];
|
|
|
|
|
/*off_t off;*/
|
|
|
|
|
};
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
typedef struct off_item **OffsetHashTable;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct keyring_name *KR_NAME;
|
|
|
|
|
struct keyring_name {
|
|
|
|
|
struct keyring_name *next;
|
|
|
|
|
int secret;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
dotlock_t lockhd;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
int is_locked;
|
|
|
|
|
int did_full_scan;
|
|
|
|
|
char fname[1];
|
|
|
|
|
};
|
|
|
|
|
typedef struct keyring_name const * CONST_KR_NAME;
|
|
|
|
|
|
|
|
|
|
static KR_NAME kr_names;
|
|
|
|
|
static int active_handles;
|
|
|
|
|
|
|
|
|
|
static OffsetHashTable kr_offtbl;
|
|
|
|
|
static int kr_offtbl_ready;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct keyring_handle {
|
|
|
|
|
CONST_KR_NAME resource;
|
|
|
|
|
int secret; /* this is for a secret keyring */
|
|
|
|
|
struct {
|
|
|
|
|
CONST_KR_NAME kr;
|
|
|
|
|
IOBUF iobuf;
|
|
|
|
|
int eof;
|
|
|
|
|
int error;
|
|
|
|
|
} current;
|
|
|
|
|
struct {
|
2012-01-10 15:16:44 +01:00
|
|
|
|
CONST_KR_NAME kr;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
off_t offset;
|
|
|
|
|
size_t pk_no;
|
|
|
|
|
size_t uid_no;
|
|
|
|
|
unsigned int n_packets; /*used for delete and update*/
|
|
|
|
|
} found;
|
|
|
|
|
struct {
|
|
|
|
|
char *name;
|
|
|
|
|
char *pattern;
|
|
|
|
|
} word_match;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int do_copy (int mode, const char *fname, KBNODE root, int secret,
|
|
|
|
|
off_t start_offset, unsigned int n_packets );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct off_item *
|
|
|
|
|
new_offset_item (void)
|
|
|
|
|
{
|
|
|
|
|
struct off_item *k;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
k = xmalloc_clear (sizeof *k);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
static void
|
|
|
|
|
release_offset_items (struct off_item *k)
|
|
|
|
|
{
|
|
|
|
|
struct off_item *k2;
|
|
|
|
|
|
|
|
|
|
for (; k; k = k2)
|
|
|
|
|
{
|
|
|
|
|
k2 = k->next;
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (k);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
static OffsetHashTable
|
2002-06-29 15:46:34 +02:00
|
|
|
|
new_offset_hash_table (void)
|
|
|
|
|
{
|
|
|
|
|
struct off_item **tbl;
|
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
tbl = xmalloc_clear (2048 * sizeof *tbl);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return tbl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
static void
|
|
|
|
|
release_offset_hash_table (OffsetHashTable tbl)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!tbl)
|
|
|
|
|
return;
|
|
|
|
|
for (i=0; i < 2048; i++)
|
|
|
|
|
release_offset_items (tbl[i]);
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (tbl);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static struct off_item *
|
|
|
|
|
lookup_offset_hash_table (OffsetHashTable tbl, u32 *kid)
|
|
|
|
|
{
|
|
|
|
|
struct off_item *k;
|
|
|
|
|
|
|
|
|
|
for (k = tbl[(kid[1] & 0x07ff)]; k; k = k->next)
|
|
|
|
|
if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
|
|
|
|
|
return k;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
update_offset_hash_table (OffsetHashTable tbl, u32 *kid, off_t off)
|
|
|
|
|
{
|
|
|
|
|
struct off_item *k;
|
|
|
|
|
|
|
|
|
|
for (k = tbl[(kid[1] & 0x07ff)]; k; k = k->next)
|
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
/*k->off = off;*/
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
k = new_offset_item ();
|
|
|
|
|
k->kid[0] = kid[0];
|
|
|
|
|
k->kid[1] = kid[1];
|
|
|
|
|
/*k->off = off;*/
|
|
|
|
|
k->next = tbl[(kid[1] & 0x07ff)];
|
|
|
|
|
tbl[(kid[1] & 0x07ff)] = k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
update_offset_hash_table_from_kb (OffsetHashTable tbl, KBNODE node, off_t off)
|
|
|
|
|
{
|
|
|
|
|
for (; node; node = node->next)
|
|
|
|
|
{
|
|
|
|
|
if (node->pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|
|| node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
|
|
|
|
|
{
|
|
|
|
|
u32 aki[2];
|
|
|
|
|
keyid_from_pk (node->pkt->pkt.public_key, aki);
|
|
|
|
|
update_offset_hash_table (tbl, aki, off);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/*
|
2002-11-08 04:31:21 +01:00
|
|
|
|
* Register a filename for plain keyring files. ptr is set to a
|
|
|
|
|
* pointer to be used to create a handles etc, or the already-issued
|
|
|
|
|
* pointer if it has already been registered. The function returns 1
|
|
|
|
|
* if a new keyring was registered.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
keyring_register_filename (const char *fname, int secret, void **ptr)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
KR_NAME kr;
|
|
|
|
|
|
|
|
|
|
if (active_handles)
|
|
|
|
|
BUG (); /* We don't allow that */
|
|
|
|
|
|
2002-11-08 04:31:21 +01:00
|
|
|
|
for (kr=kr_names; kr; kr = kr->next)
|
|
|
|
|
{
|
2007-08-24 11:51:58 +02:00
|
|
|
|
if (same_file_p (kr->fname, fname) )
|
2002-11-08 04:31:21 +01:00
|
|
|
|
{
|
|
|
|
|
*ptr=kr;
|
|
|
|
|
return 0; /* already registered */
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
register_secured_file (fname);
|
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
kr = xmalloc (sizeof *kr + strlen (fname));
|
2002-06-29 15:46:34 +02:00
|
|
|
|
strcpy (kr->fname, fname);
|
|
|
|
|
kr->secret = !!secret;
|
|
|
|
|
kr->lockhd = NULL;
|
|
|
|
|
kr->is_locked = 0;
|
|
|
|
|
kr->did_full_scan = 0;
|
|
|
|
|
/* keep a list of all issued pointers */
|
|
|
|
|
kr->next = kr_names;
|
|
|
|
|
kr_names = kr;
|
|
|
|
|
|
|
|
|
|
/* create the offset table the first time a function here is used */
|
|
|
|
|
if (!kr_offtbl)
|
|
|
|
|
kr_offtbl = new_offset_hash_table ();
|
|
|
|
|
|
2002-11-08 04:31:21 +01:00
|
|
|
|
*ptr=kr;
|
|
|
|
|
|
|
|
|
|
return 1;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
keyring_is_writable (void *token)
|
|
|
|
|
{
|
|
|
|
|
KR_NAME r = token;
|
|
|
|
|
|
|
|
|
|
return r? !access (r->fname, W_OK) : 0;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a new handle for the resource associated with TOKEN. SECRET
|
|
|
|
|
is just just as a cross-check.
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
The returned handle must be released using keyring_release (). */
|
|
|
|
|
KEYRING_HANDLE
|
|
|
|
|
keyring_new (void *token, int secret)
|
|
|
|
|
{
|
|
|
|
|
KEYRING_HANDLE hd;
|
|
|
|
|
KR_NAME resource = token;
|
|
|
|
|
|
|
|
|
|
assert (resource && !resource->secret == !secret);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
hd = xmalloc_clear (sizeof *hd);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
hd->resource = resource;
|
|
|
|
|
hd->secret = !!secret;
|
|
|
|
|
active_handles++;
|
|
|
|
|
return hd;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
void
|
2002-06-29 15:46:34 +02:00
|
|
|
|
keyring_release (KEYRING_HANDLE hd)
|
|
|
|
|
{
|
|
|
|
|
if (!hd)
|
|
|
|
|
return;
|
|
|
|
|
assert (active_handles > 0);
|
|
|
|
|
active_handles--;
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (hd->word_match.name);
|
|
|
|
|
xfree (hd->word_match.pattern);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_close (hd->current.iobuf);
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (hd);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
keyring_get_resource_name (KEYRING_HANDLE hd)
|
|
|
|
|
{
|
|
|
|
|
if (!hd || !hd->resource)
|
|
|
|
|
return NULL;
|
|
|
|
|
return hd->resource->fname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Lock the keyring with the given handle, or unlok if yes is false.
|
|
|
|
|
* We ignore the handle and lock all registered files.
|
|
|
|
|
*/
|
2012-01-10 15:16:44 +01:00
|
|
|
|
int
|
2002-06-29 15:46:34 +02:00
|
|
|
|
keyring_lock (KEYRING_HANDLE hd, int yes)
|
|
|
|
|
{
|
|
|
|
|
KR_NAME kr;
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
if (yes) {
|
|
|
|
|
/* first make sure the lock handles are created */
|
|
|
|
|
for (kr=kr_names; kr; kr = kr->next) {
|
|
|
|
|
if (!keyring_is_writable(kr))
|
|
|
|
|
continue;
|
|
|
|
|
if (!kr->lockhd) {
|
2012-01-10 15:16:44 +01:00
|
|
|
|
kr->lockhd = dotlock_create (kr->fname, 0);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (!kr->lockhd) {
|
|
|
|
|
log_info ("can't allocate lock for `%s'\n", kr->fname );
|
|
|
|
|
rc = G10ERR_GENERAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
/* and now set the locks */
|
|
|
|
|
for (kr=kr_names; kr; kr = kr->next) {
|
|
|
|
|
if (!keyring_is_writable(kr))
|
|
|
|
|
continue;
|
|
|
|
|
if (kr->is_locked)
|
|
|
|
|
;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else if (dotlock_take (kr->lockhd, -1) ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
|
log_info ("can't lock `%s'\n", kr->fname );
|
|
|
|
|
rc = G10ERR_GENERAL;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
|
kr->is_locked = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc || !yes) {
|
|
|
|
|
for (kr=kr_names; kr; kr = kr->next) {
|
|
|
|
|
if (!keyring_is_writable(kr))
|
|
|
|
|
continue;
|
|
|
|
|
if (!kr->is_locked)
|
|
|
|
|
;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else if (dotlock_release (kr->lockhd))
|
2002-06-29 15:46:34 +02:00
|
|
|
|
log_info ("can't unlock `%s'\n", kr->fname );
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
|
kr->is_locked = 0;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return the last found keyring. Caller must free it.
|
|
|
|
|
* The returned keyblock has the kbode flag bit 0 set for the node with
|
2012-01-10 15:16:44 +01:00
|
|
|
|
* the public key used to locate the keyblock or flag bit 1 set for
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* the user ID node.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
|
|
|
|
|
{
|
|
|
|
|
PACKET *pkt;
|
|
|
|
|
int rc;
|
|
|
|
|
KBNODE keyblock = NULL, node, lastnode;
|
|
|
|
|
IOBUF a;
|
|
|
|
|
int in_cert = 0;
|
|
|
|
|
int pk_no = 0;
|
|
|
|
|
int uid_no = 0;
|
|
|
|
|
int save_mode;
|
|
|
|
|
|
|
|
|
|
if (ret_kb)
|
|
|
|
|
*ret_kb = NULL;
|
|
|
|
|
|
|
|
|
|
if (!hd->found.kr)
|
|
|
|
|
return -1; /* no successful search */
|
|
|
|
|
|
|
|
|
|
a = iobuf_open (hd->found.kr->fname);
|
2004-10-28 04:14:06 +02:00
|
|
|
|
if (!a)
|
|
|
|
|
{
|
|
|
|
|
log_error(_("can't open `%s'\n"), hd->found.kr->fname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return G10ERR_KEYRING_OPEN;
|
2004-10-28 04:14:06 +02:00
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
if (iobuf_seek (a, hd->found.offset) ) {
|
|
|
|
|
log_error ("can't seek `%s'\n", hd->found.kr->fname);
|
|
|
|
|
iobuf_close(a);
|
|
|
|
|
return G10ERR_KEYRING_OPEN;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
pkt = xmalloc (sizeof *pkt);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
init_packet (pkt);
|
|
|
|
|
hd->found.n_packets = 0;;
|
|
|
|
|
lastnode = NULL;
|
|
|
|
|
save_mode = set_packet_list_mode(0);
|
|
|
|
|
while ((rc=parse_packet (a, pkt)) != -1) {
|
|
|
|
|
hd->found.n_packets++;
|
|
|
|
|
if (rc == G10ERR_UNKNOWN_PACKET) {
|
|
|
|
|
free_packet (pkt);
|
|
|
|
|
init_packet (pkt);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (rc) {
|
2002-06-29 15:46:34 +02:00
|
|
|
|
log_error ("keyring_get_keyblock: read error: %s\n",
|
|
|
|
|
g10_errstr(rc) );
|
|
|
|
|
rc = G10ERR_INV_KEYRING;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-02-22 05:10:30 +01:00
|
|
|
|
|
|
|
|
|
/* Filter allowed packets. */
|
|
|
|
|
switch (pkt->pkttype){
|
|
|
|
|
case PKT_PUBLIC_KEY:
|
|
|
|
|
case PKT_PUBLIC_SUBKEY:
|
|
|
|
|
case PKT_SECRET_KEY:
|
|
|
|
|
case PKT_SECRET_SUBKEY:
|
|
|
|
|
case PKT_USER_ID:
|
|
|
|
|
case PKT_ATTRIBUTE:
|
|
|
|
|
case PKT_SIGNATURE:
|
|
|
|
|
break; /* Allowed per RFC. */
|
|
|
|
|
case PKT_RING_TRUST:
|
|
|
|
|
case PKT_OLD_COMMENT:
|
|
|
|
|
case PKT_COMMENT:
|
|
|
|
|
case PKT_GPG_CONTROL:
|
|
|
|
|
break; /* Allowed by us. */
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
log_error ("skipped packet of type %d in keyring\n",
|
|
|
|
|
(int)pkt->pkttype);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
free_packet(pkt);
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_cert && (pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|
|| pkt->pkttype == PKT_SECRET_KEY)) {
|
|
|
|
|
hd->found.n_packets--; /* fix counter */
|
|
|
|
|
break; /* ready */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
in_cert = 1;
|
|
|
|
|
if (pkt->pkttype == PKT_RING_TRUST) {
|
|
|
|
|
/*(this code is duplicated after the loop)*/
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if ( lastnode
|
2002-06-29 15:46:34 +02:00
|
|
|
|
&& lastnode->pkt->pkttype == PKT_SIGNATURE
|
|
|
|
|
&& (pkt->pkt.ring_trust->sigcache & 1) ) {
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/* This is a ring trust packet with a checked signature
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* status cache following directly a signature paket.
|
2009-05-06 11:31:15 +02:00
|
|
|
|
* Set the cache status into that signature packet. */
|
2002-06-29 15:46:34 +02:00
|
|
|
|
PKT_signature *sig = lastnode->pkt->pkt.signature;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
sig->flags.checked = 1;
|
|
|
|
|
sig->flags.valid = !!(pkt->pkt.ring_trust->sigcache & 2);
|
|
|
|
|
}
|
2009-05-06 11:31:15 +02:00
|
|
|
|
/* Reset LASTNODE, so that we set the cache status only
|
|
|
|
|
* from the ring trust packets immediately following
|
|
|
|
|
* signature packets. */
|
2002-06-29 15:46:34 +02:00
|
|
|
|
lastnode = NULL;
|
2009-05-06 11:31:15 +02:00
|
|
|
|
free_packet(pkt);
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
continue;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-05-06 11:31:15 +02:00
|
|
|
|
node = lastnode = new_kbnode (pkt);
|
|
|
|
|
if (!keyblock)
|
|
|
|
|
keyblock = node;
|
|
|
|
|
else
|
|
|
|
|
add_kbnode (keyblock, node);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2009-05-06 11:31:15 +02:00
|
|
|
|
if ( pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|
|| pkt->pkttype == PKT_PUBLIC_SUBKEY
|
|
|
|
|
|| pkt->pkttype == PKT_SECRET_KEY
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|| pkt->pkttype == PKT_SECRET_SUBKEY)
|
2009-05-06 11:31:15 +02:00
|
|
|
|
{
|
|
|
|
|
if (++pk_no == hd->found.pk_no)
|
|
|
|
|
node->flag |= 1;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else if ( pkt->pkttype == PKT_USER_ID)
|
2009-05-06 11:31:15 +02:00
|
|
|
|
{
|
|
|
|
|
if (++uid_no == hd->found.uid_no)
|
|
|
|
|
node->flag |= 2;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
pkt = xmalloc (sizeof *pkt);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
init_packet(pkt);
|
|
|
|
|
}
|
|
|
|
|
set_packet_list_mode(save_mode);
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (rc == -1 && keyblock)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = 0; /* got the entire keyblock */
|
|
|
|
|
|
|
|
|
|
if (rc || !ret_kb)
|
|
|
|
|
release_kbnode (keyblock);
|
|
|
|
|
else {
|
2015-02-22 05:10:30 +01:00
|
|
|
|
/*(duplicated from the loop body)*/
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if ( pkt && pkt->pkttype == PKT_RING_TRUST
|
2012-01-10 15:16:44 +01:00
|
|
|
|
&& lastnode
|
2002-06-29 15:46:34 +02:00
|
|
|
|
&& lastnode->pkt->pkttype == PKT_SIGNATURE
|
|
|
|
|
&& (pkt->pkt.ring_trust->sigcache & 1) ) {
|
|
|
|
|
PKT_signature *sig = lastnode->pkt->pkt.signature;
|
|
|
|
|
sig->flags.checked = 1;
|
|
|
|
|
sig->flags.valid = !!(pkt->pkt.ring_trust->sigcache & 2);
|
|
|
|
|
}
|
|
|
|
|
*ret_kb = keyblock;
|
|
|
|
|
}
|
|
|
|
|
free_packet (pkt);
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (pkt);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_close(a);
|
|
|
|
|
|
|
|
|
|
/* Make sure that future search operations fail immediately when
|
2012-01-10 15:16:44 +01:00
|
|
|
|
* we know that we are working on a invalid keyring
|
2002-06-29 15:46:34 +02:00
|
|
|
|
*/
|
|
|
|
|
if (rc == G10ERR_INV_KEYRING)
|
|
|
|
|
hd->current.error = rc;
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
keyring_update_keyblock (KEYRING_HANDLE hd, KBNODE kb)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (!hd->found.kr)
|
|
|
|
|
return -1; /* no successful prior search */
|
|
|
|
|
|
|
|
|
|
if (!hd->found.n_packets) {
|
|
|
|
|
/* need to know the number of packets - do a dummy get_keyblock*/
|
|
|
|
|
rc = keyring_get_keyblock (hd, NULL);
|
|
|
|
|
if (rc) {
|
|
|
|
|
log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc));
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
if (!hd->found.n_packets)
|
|
|
|
|
BUG ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The open iobuf isn't needed anymore and in fact is a problem when
|
|
|
|
|
it comes to renaming the keyring files on some operating systems,
|
|
|
|
|
so close it here */
|
|
|
|
|
iobuf_close(hd->current.iobuf);
|
|
|
|
|
hd->current.iobuf = NULL;
|
|
|
|
|
|
|
|
|
|
/* do the update */
|
|
|
|
|
rc = do_copy (3, hd->found.kr->fname, kb, hd->secret,
|
|
|
|
|
hd->found.offset, hd->found.n_packets );
|
|
|
|
|
if (!rc) {
|
|
|
|
|
if (!hd->secret && kr_offtbl)
|
|
|
|
|
{
|
|
|
|
|
update_offset_hash_table_from_kb (kr_offtbl, kb, 0);
|
|
|
|
|
}
|
|
|
|
|
/* better reset the found info */
|
|
|
|
|
hd->found.kr = NULL;
|
|
|
|
|
hd->found.offset = 0;
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
keyring_insert_keyblock (KEYRING_HANDLE hd, KBNODE kb)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
const char *fname;
|
|
|
|
|
|
|
|
|
|
if (!hd)
|
|
|
|
|
fname = NULL;
|
|
|
|
|
else if (hd->found.kr)
|
|
|
|
|
fname = hd->found.kr->fname;
|
|
|
|
|
else if (hd->current.kr)
|
|
|
|
|
fname = hd->current.kr->fname;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
|
fname = hd->resource? hd->resource->fname:NULL;
|
|
|
|
|
|
|
|
|
|
if (!fname)
|
2012-01-10 15:16:44 +01:00
|
|
|
|
return G10ERR_GENERAL;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
/* close this one otherwise we will lose the position for
|
|
|
|
|
* a next search. Fixme: it would be better to adjust the position
|
|
|
|
|
* after the write opertions.
|
|
|
|
|
*/
|
|
|
|
|
iobuf_close (hd->current.iobuf);
|
|
|
|
|
hd->current.iobuf = NULL;
|
|
|
|
|
|
|
|
|
|
/* do the insert */
|
|
|
|
|
rc = do_copy (1, fname, kb, hd->secret, 0, 0 );
|
|
|
|
|
if (!rc && !hd->secret && kr_offtbl)
|
|
|
|
|
{
|
|
|
|
|
update_offset_hash_table_from_kb (kr_offtbl, kb, 0);
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
keyring_delete_keyblock (KEYRING_HANDLE hd)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (!hd->found.kr)
|
|
|
|
|
return -1; /* no successful prior search */
|
|
|
|
|
|
|
|
|
|
if (!hd->found.n_packets) {
|
|
|
|
|
/* need to know the number of packets - do a dummy get_keyblock*/
|
|
|
|
|
rc = keyring_get_keyblock (hd, NULL);
|
|
|
|
|
if (rc) {
|
|
|
|
|
log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc));
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
if (!hd->found.n_packets)
|
|
|
|
|
BUG ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* close this one otherwise we will lose the position for
|
|
|
|
|
* a next search. Fixme: it would be better to adjust the position
|
|
|
|
|
* after the write opertions.
|
|
|
|
|
*/
|
|
|
|
|
iobuf_close (hd->current.iobuf);
|
|
|
|
|
hd->current.iobuf = NULL;
|
|
|
|
|
|
|
|
|
|
/* do the delete */
|
|
|
|
|
rc = do_copy (2, hd->found.kr->fname, NULL, hd->secret,
|
|
|
|
|
hd->found.offset, hd->found.n_packets );
|
|
|
|
|
if (!rc) {
|
|
|
|
|
/* better reset the found info */
|
|
|
|
|
hd->found.kr = NULL;
|
|
|
|
|
hd->found.offset = 0;
|
|
|
|
|
/* Delete is a rare operations, so we don't remove the keys
|
|
|
|
|
* from the offset table */
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/*
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* Start the next search on this handle right at the beginning
|
|
|
|
|
*/
|
2012-01-10 15:16:44 +01:00
|
|
|
|
int
|
2002-06-29 15:46:34 +02:00
|
|
|
|
keyring_search_reset (KEYRING_HANDLE hd)
|
|
|
|
|
{
|
|
|
|
|
assert (hd);
|
|
|
|
|
|
|
|
|
|
hd->current.kr = NULL;
|
|
|
|
|
iobuf_close (hd->current.iobuf);
|
|
|
|
|
hd->current.iobuf = NULL;
|
|
|
|
|
hd->current.eof = 0;
|
|
|
|
|
hd->current.error = 0;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
hd->found.kr = NULL;
|
|
|
|
|
hd->found.offset = 0;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
return 0;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
prepare_search (KEYRING_HANDLE hd)
|
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (hd->current.error)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return hd->current.error; /* still in error state */
|
|
|
|
|
|
|
|
|
|
if (hd->current.kr && !hd->current.eof) {
|
|
|
|
|
if ( !hd->current.iobuf )
|
|
|
|
|
return G10ERR_GENERAL; /* position invalid after a modify */
|
|
|
|
|
return 0; /* okay */
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (!hd->current.kr && hd->current.eof)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return -1; /* still EOF */
|
|
|
|
|
|
|
|
|
|
if (!hd->current.kr) { /* start search with first keyring */
|
|
|
|
|
hd->current.kr = hd->resource;
|
|
|
|
|
if (!hd->current.kr) {
|
|
|
|
|
hd->current.eof = 1;
|
|
|
|
|
return -1; /* keyring not available */
|
|
|
|
|
}
|
|
|
|
|
assert (!hd->current.iobuf);
|
|
|
|
|
}
|
|
|
|
|
else { /* EOF */
|
2012-01-10 15:16:44 +01:00
|
|
|
|
iobuf_close (hd->current.iobuf);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
hd->current.iobuf = NULL;
|
|
|
|
|
hd->current.kr = NULL;
|
|
|
|
|
hd->current.eof = 1;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hd->current.eof = 0;
|
|
|
|
|
hd->current.iobuf = iobuf_open (hd->current.kr->fname);
|
2004-10-28 04:14:06 +02:00
|
|
|
|
if (!hd->current.iobuf)
|
|
|
|
|
{
|
|
|
|
|
log_error(_("can't open `%s'\n"), hd->current.kr->fname );
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return (hd->current.error = G10ERR_OPEN_FILE);
|
2004-10-28 04:14:06 +02:00
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* A map of the all characters valid used for word_match()
|
|
|
|
|
* Valid characters are in in this table converted to uppercase.
|
|
|
|
|
* because the upper 128 bytes have special meaning, we assume
|
|
|
|
|
* that they are all valid.
|
|
|
|
|
* Note: We must use numerical values here in case that this program
|
|
|
|
|
* will be converted to those little blue HAL9000s with their strange
|
|
|
|
|
* EBCDIC character set (user ids are UTF-8).
|
|
|
|
|
* wk 2000-04-13: Hmmm, does this really make sense, given the fact that
|
|
|
|
|
* we can run gpg now on a S/390 running GNU/Linux, where the code
|
|
|
|
|
* translation is done by the device drivers?
|
|
|
|
|
*/
|
|
|
|
|
static const byte word_match_chars[256] = {
|
|
|
|
|
/* 00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
|
|
|
|
|
/* 38 */ 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 40 */ 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
|
|
/* 48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
|
|
|
|
|
/* 50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
|
|
|
|
|
/* 58 */ 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 60 */ 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
|
|
/* 68 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
|
|
|
|
|
/* 70 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
|
|
|
|
|
/* 78 */ 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
/* 80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
|
|
|
|
/* 88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
|
|
/* 90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
|
|
|
|
|
/* 98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
|
|
|
|
|
/* a0 */ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
|
|
|
|
|
/* a8 */ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
|
|
|
|
|
/* b0 */ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
|
|
|
|
|
/* b8 */ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
|
|
|
|
|
/* c0 */ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
|
|
|
|
/* c8 */ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
|
|
|
|
|
/* d0 */ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
|
|
|
|
|
/* d8 */ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
|
|
|
|
|
/* e0 */ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
|
|
|
|
|
/* e8 */ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
|
|
|
|
|
/* f0 */ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
|
|
|
|
/* f8 */ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Do a word match (original user id starts with a '+').
|
|
|
|
|
* The pattern is already tokenized to a more suitable format:
|
|
|
|
|
* There are only the real words in it delimited by one space
|
|
|
|
|
* and all converted to uppercase.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 if all words match.
|
|
|
|
|
*
|
|
|
|
|
* Note: This algorithm is a straightforward one and not very
|
|
|
|
|
* fast. It works for UTF-8 strings. The uidlen should
|
|
|
|
|
* be removed but due to the fact that old versions of
|
|
|
|
|
* pgp don't use UTF-8 we still use the length; this should
|
|
|
|
|
* be fixed in parse-packet (and replace \0 by some special
|
|
|
|
|
* UTF-8 encoding)
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
word_match( const byte *uid, size_t uidlen, const byte *pattern )
|
|
|
|
|
{
|
|
|
|
|
size_t wlen, n;
|
|
|
|
|
const byte *p;
|
|
|
|
|
const byte *s;
|
|
|
|
|
|
|
|
|
|
for( s=pattern; *s; ) {
|
|
|
|
|
do {
|
|
|
|
|
/* skip leading delimiters */
|
|
|
|
|
while( uidlen && !word_match_chars[*uid] )
|
|
|
|
|
uid++, uidlen--;
|
|
|
|
|
/* get length of the word */
|
|
|
|
|
n = uidlen; p = uid;
|
|
|
|
|
while( n && word_match_chars[*p] )
|
|
|
|
|
p++, n--;
|
|
|
|
|
wlen = p - uid;
|
|
|
|
|
/* and compare against the current word from pattern */
|
|
|
|
|
for(n=0, p=uid; n < wlen && s[n] != ' ' && s[n] ; n++, p++ ) {
|
|
|
|
|
if( word_match_chars[*p] != s[n] )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if( n == wlen && (s[n] == ' ' || !s[n]) )
|
|
|
|
|
break; /* found */
|
|
|
|
|
uid += wlen;
|
|
|
|
|
uidlen -= wlen;
|
|
|
|
|
} while( uidlen );
|
|
|
|
|
if( !uidlen )
|
|
|
|
|
return -1; /* not found */
|
|
|
|
|
|
|
|
|
|
/* advance to next word in pattern */
|
|
|
|
|
for(; *s != ' ' && *s ; s++ )
|
|
|
|
|
;
|
|
|
|
|
if( *s )
|
|
|
|
|
s++ ;
|
|
|
|
|
}
|
|
|
|
|
return 0; /* found */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* prepare word word_match; that is parse the name and
|
|
|
|
|
* build the pattern.
|
|
|
|
|
* caller has to free the returned pattern
|
|
|
|
|
*/
|
|
|
|
|
static char*
|
|
|
|
|
prepare_word_match (const byte *name)
|
|
|
|
|
{
|
|
|
|
|
byte *pattern, *p;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
/* the original length is always enough for the pattern */
|
2005-07-27 20:10:56 +02:00
|
|
|
|
p = pattern = xmalloc(strlen(name)+1);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
do {
|
|
|
|
|
/* skip leading delimiters */
|
|
|
|
|
while( *name && !word_match_chars[*name] )
|
|
|
|
|
name++;
|
|
|
|
|
/* copy as long as we don't have a delimiter and convert
|
|
|
|
|
* to uppercase.
|
|
|
|
|
* fixme: how can we handle utf8 uppercasing */
|
|
|
|
|
for( ; *name && (c=word_match_chars[*name]); name++ )
|
|
|
|
|
*p++ = c;
|
|
|
|
|
*p++ = ' '; /* append pattern delimiter */
|
|
|
|
|
} while( *name );
|
|
|
|
|
p[-1] = 0; /* replace last pattern delimiter by EOS */
|
|
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
compare_name (int mode, const char *name, const char *uid, size_t uidlen)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
const char *s, *se;
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (mode == KEYDB_SEARCH_MODE_EXACT) {
|
2002-06-29 15:46:34 +02:00
|
|
|
|
for (i=0; name[i] && uidlen; i++, uidlen--)
|
|
|
|
|
if (uid[i] != name[i])
|
|
|
|
|
break;
|
|
|
|
|
if (!uidlen && !name[i])
|
|
|
|
|
return 0; /* found */
|
|
|
|
|
}
|
|
|
|
|
else if (mode == KEYDB_SEARCH_MODE_SUBSTR) {
|
|
|
|
|
if (ascii_memistr( uid, uidlen, name ))
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else if ( mode == KEYDB_SEARCH_MODE_MAIL
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|| mode == KEYDB_SEARCH_MODE_MAILSUB
|
|
|
|
|
|| mode == KEYDB_SEARCH_MODE_MAILEND) {
|
|
|
|
|
for (i=0, s= uid; i < uidlen && *s != '<'; s++, i++)
|
|
|
|
|
;
|
|
|
|
|
if (i < uidlen) {
|
|
|
|
|
/* skip opening delim and one char and look for the closing one*/
|
|
|
|
|
s++; i++;
|
|
|
|
|
for (se=s+1, i++; i < uidlen && *se != '>'; se++, i++)
|
|
|
|
|
;
|
|
|
|
|
if (i < uidlen) {
|
|
|
|
|
i = se - s;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (mode == KEYDB_SEARCH_MODE_MAIL) {
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if( strlen(name)-2 == i
|
|
|
|
|
&& !ascii_memcasecmp( s, name+1, i) )
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (mode == KEYDB_SEARCH_MODE_MAILSUB) {
|
|
|
|
|
if( ascii_memistr( s, i, name ) )
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else { /* email from end */
|
|
|
|
|
/* nyi */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (mode == KEYDB_SEARCH_MODE_WORDS)
|
|
|
|
|
return word_match (uid, uidlen, name);
|
|
|
|
|
else
|
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
|
|
return -1; /* not found */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/*
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* Search through the keyring(s), starting at the current position,
|
|
|
|
|
* for a keyblock which contains one of the keys described in the DESC array.
|
|
|
|
|
*/
|
2012-01-10 15:16:44 +01:00
|
|
|
|
int
|
2003-02-12 19:43:44 +01:00
|
|
|
|
keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
|
|
|
|
|
size_t ndesc, size_t *descindex)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
PACKET pkt;
|
|
|
|
|
int save_mode;
|
|
|
|
|
off_t offset, main_offset;
|
|
|
|
|
size_t n;
|
|
|
|
|
int need_uid, need_words, need_keyid, need_fpr, any_skip;
|
|
|
|
|
int pk_no, uid_no;
|
|
|
|
|
int initial_skip;
|
|
|
|
|
int use_offtbl;
|
|
|
|
|
PKT_user_id *uid = NULL;
|
|
|
|
|
PKT_public_key *pk = NULL;
|
|
|
|
|
PKT_secret_key *sk = NULL;
|
2003-01-03 22:41:53 +01:00
|
|
|
|
u32 aki[2];
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
/* figure out what information we need */
|
|
|
|
|
need_uid = need_words = need_keyid = need_fpr = any_skip = 0;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
for (n=0; n < ndesc; n++)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
switch (desc[n].mode)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_EXACT:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
case KEYDB_SEARCH_MODE_SUBSTR:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAIL:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILSUB:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILEND:
|
|
|
|
|
need_uid = 1;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_WORDS:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
need_uid = 1;
|
|
|
|
|
need_words = 1;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
|
|
|
|
need_keyid = 1;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_FPR16:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
case KEYDB_SEARCH_MODE_FPR20:
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_FPR:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
need_fpr = 1;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_FIRST:
|
|
|
|
|
/* always restart the search in this mode */
|
|
|
|
|
keyring_search_reset (hd);
|
|
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (desc[n].skipfnc)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
any_skip = 1;
|
|
|
|
|
need_keyid = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = prepare_search (hd);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
use_offtbl = !hd->secret && kr_offtbl;
|
|
|
|
|
if (!use_offtbl)
|
|
|
|
|
;
|
|
|
|
|
else if (!kr_offtbl_ready)
|
|
|
|
|
need_keyid = 1;
|
|
|
|
|
else if (ndesc == 1 && desc[0].mode == KEYDB_SEARCH_MODE_LONG_KID)
|
|
|
|
|
{
|
|
|
|
|
struct off_item *oi;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
oi = lookup_offset_hash_table (kr_offtbl, desc[0].u.kid);
|
|
|
|
|
if (!oi)
|
|
|
|
|
{ /* We know that we don't have this key */
|
|
|
|
|
hd->found.kr = NULL;
|
|
|
|
|
hd->current.eof = 1;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
/* We could now create a positive search status and return.
|
2012-01-10 15:16:44 +01:00
|
|
|
|
* However the problem is that another instance of gpg may
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* have changed the keyring so that the offsets are not valid
|
2012-01-10 15:16:44 +01:00
|
|
|
|
* anymore - therefore we don't do it
|
2002-06-29 15:46:34 +02:00
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (need_words)
|
|
|
|
|
{
|
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
|
|
log_debug ("word search mode does not yet work\n");
|
|
|
|
|
/* FIXME: here is a long standing bug in our function and in addition we
|
|
|
|
|
just use the first search description */
|
2012-01-10 15:16:44 +01:00
|
|
|
|
for (n=0; n < ndesc && !name; n++)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (desc[n].mode == KEYDB_SEARCH_MODE_WORDS)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
name = desc[n].u.name;
|
|
|
|
|
}
|
|
|
|
|
assert (name);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if ( !hd->word_match.name || strcmp (hd->word_match.name, name) )
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
/* name changed */
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (hd->word_match.name);
|
|
|
|
|
xfree (hd->word_match.pattern);
|
|
|
|
|
hd->word_match.name = xstrdup (name);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
hd->word_match.pattern = prepare_word_match (name);
|
|
|
|
|
}
|
|
|
|
|
name = hd->word_match.pattern;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init_packet(&pkt);
|
|
|
|
|
save_mode = set_packet_list_mode(0);
|
|
|
|
|
|
|
|
|
|
hd->found.kr = NULL;
|
|
|
|
|
main_offset = 0;
|
|
|
|
|
pk_no = uid_no = 0;
|
|
|
|
|
initial_skip = 1; /* skip until we see the start of a keyblock */
|
2012-01-10 15:16:44 +01:00
|
|
|
|
while (!(rc=search_packet (hd->current.iobuf, &pkt, &offset, need_uid)))
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
byte afp[MAX_FINGERPRINT_LEN];
|
|
|
|
|
size_t an;
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (pkt.pkttype == PKT_PUBLIC_KEY || pkt.pkttype == PKT_SECRET_KEY)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
main_offset = offset;
|
|
|
|
|
pk_no = uid_no = 0;
|
|
|
|
|
initial_skip = 0;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (initial_skip)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
free_packet (&pkt);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
pk = NULL;
|
|
|
|
|
sk = NULL;
|
|
|
|
|
uid = NULL;
|
|
|
|
|
if ( pkt.pkttype == PKT_PUBLIC_KEY
|
|
|
|
|
|| pkt.pkttype == PKT_PUBLIC_SUBKEY)
|
|
|
|
|
{
|
|
|
|
|
pk = pkt.pkt.public_key;
|
|
|
|
|
++pk_no;
|
|
|
|
|
|
|
|
|
|
if (need_fpr) {
|
|
|
|
|
fingerprint_from_pk (pk, afp, &an);
|
|
|
|
|
while (an < 20) /* fill up to 20 bytes */
|
|
|
|
|
afp[an++] = 0;
|
|
|
|
|
}
|
|
|
|
|
if (need_keyid)
|
|
|
|
|
keyid_from_pk (pk, aki);
|
|
|
|
|
|
|
|
|
|
if (use_offtbl && !kr_offtbl_ready)
|
|
|
|
|
update_offset_hash_table (kr_offtbl, aki, main_offset);
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else if (pkt.pkttype == PKT_USER_ID)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
uid = pkt.pkt.user_id;
|
|
|
|
|
++uid_no;
|
|
|
|
|
}
|
|
|
|
|
else if ( pkt.pkttype == PKT_SECRET_KEY
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|| pkt.pkttype == PKT_SECRET_SUBKEY)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
sk = pkt.pkt.secret_key;
|
|
|
|
|
++pk_no;
|
|
|
|
|
|
|
|
|
|
if (need_fpr) {
|
|
|
|
|
fingerprint_from_sk (sk, afp, &an);
|
|
|
|
|
while (an < 20) /* fill up to 20 bytes */
|
|
|
|
|
afp[an++] = 0;
|
|
|
|
|
}
|
|
|
|
|
if (need_keyid)
|
|
|
|
|
keyid_from_sk (sk, aki);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
for (n=0; n < ndesc; n++)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
switch (desc[n].mode) {
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_NONE:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
BUG ();
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_EXACT:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
case KEYDB_SEARCH_MODE_SUBSTR:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAIL:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILSUB:
|
|
|
|
|
case KEYDB_SEARCH_MODE_MAILEND:
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_WORDS:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if ( uid && !compare_name (desc[n].mode,
|
|
|
|
|
desc[n].u.name,
|
2012-01-10 15:16:44 +01:00
|
|
|
|
uid->name, uid->len))
|
2002-06-29 15:46:34 +02:00
|
|
|
|
goto found;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
|
|
|
|
case KEYDB_SEARCH_MODE_SHORT_KID:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if ((pk||sk) && desc[n].u.kid[1] == aki[1])
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_LONG_KID:
|
|
|
|
|
if ((pk||sk) && desc[n].u.kid[0] == aki[0]
|
|
|
|
|
&& desc[n].u.kid[1] == aki[1])
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR16:
|
|
|
|
|
if ((pk||sk) && !memcmp (desc[n].u.fpr, afp, 16))
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
|
|
|
|
case KEYDB_SEARCH_MODE_FPR20:
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_FPR:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if ((pk||sk) && !memcmp (desc[n].u.fpr, afp, 20))
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_FIRST:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (pk||sk)
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
case KEYDB_SEARCH_MODE_NEXT:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (pk||sk)
|
|
|
|
|
goto found;
|
|
|
|
|
break;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
default:
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = G10ERR_INV_ARG;
|
|
|
|
|
goto found;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free_packet (&pkt);
|
|
|
|
|
continue;
|
2003-02-12 19:43:44 +01:00
|
|
|
|
found:
|
|
|
|
|
/* Record which desc we matched on. Note this value is only
|
|
|
|
|
meaningful if this function returns with no errors. */
|
|
|
|
|
if(descindex)
|
|
|
|
|
*descindex=n;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
for (n=any_skip?0:ndesc; n < ndesc; n++)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
if (desc[n].skipfnc
|
2003-09-24 01:14:03 +02:00
|
|
|
|
&& desc[n].skipfnc (desc[n].skipfncvalue, aki, uid))
|
2002-06-29 15:46:34 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (n == ndesc)
|
|
|
|
|
goto real_found;
|
|
|
|
|
free_packet (&pkt);
|
|
|
|
|
}
|
|
|
|
|
real_found:
|
|
|
|
|
if (!rc)
|
|
|
|
|
{
|
|
|
|
|
hd->found.offset = main_offset;
|
|
|
|
|
hd->found.kr = hd->current.kr;
|
|
|
|
|
hd->found.pk_no = (pk||sk)? pk_no : 0;
|
|
|
|
|
hd->found.uid_no = uid? uid_no : 0;
|
|
|
|
|
}
|
|
|
|
|
else if (rc == -1)
|
|
|
|
|
{
|
|
|
|
|
hd->current.eof = 1;
|
|
|
|
|
/* if we scanned all keyrings, we are sure that
|
|
|
|
|
* all known key IDs are in our offtbl, mark that. */
|
|
|
|
|
if (use_offtbl && !kr_offtbl_ready)
|
|
|
|
|
{
|
|
|
|
|
KR_NAME kr;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
/* First set the did_full_scan flag for this keyring (ignore
|
|
|
|
|
secret keyrings) */
|
|
|
|
|
for (kr=kr_names; kr; kr = kr->next)
|
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (!kr->secret && hd->resource == kr)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
kr->did_full_scan = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Then check whether all flags are set and if so, mark the
|
|
|
|
|
offtbl ready */
|
|
|
|
|
for (kr=kr_names; kr; kr = kr->next)
|
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (!kr->secret && !kr->did_full_scan)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!kr)
|
|
|
|
|
kr_offtbl_ready = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
|
hd->current.error = rc;
|
|
|
|
|
|
|
|
|
|
free_packet(&pkt);
|
|
|
|
|
set_packet_list_mode(save_mode);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
create_tmp_file (const char *template,
|
|
|
|
|
char **r_bakfname, char **r_tmpfname, IOBUF *r_fp)
|
2012-01-10 15:16:44 +01:00
|
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
|
char *bakfname, *tmpfname;
|
2002-08-09 04:23:42 +02:00
|
|
|
|
mode_t oldmask;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
*r_bakfname = NULL;
|
|
|
|
|
*r_tmpfname = NULL;
|
|
|
|
|
|
|
|
|
|
# ifdef USE_ONLY_8DOT3
|
|
|
|
|
/* Here is another Windoze bug?:
|
|
|
|
|
* you cant rename("pubring.gpg.tmp", "pubring.gpg");
|
|
|
|
|
* but rename("pubring.gpg.tmp", "pubring.aaa");
|
|
|
|
|
* works. So we replace .gpg by .bak or .tmp
|
|
|
|
|
*/
|
|
|
|
|
if (strlen (template) > 4
|
|
|
|
|
&& !strcmp (template+strlen(template)-4, EXTSEP_S "gpg") )
|
|
|
|
|
{
|
2005-07-27 20:10:56 +02:00
|
|
|
|
bakfname = xmalloc (strlen (template) + 1);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
strcpy (bakfname, template);
|
|
|
|
|
strcpy (bakfname+strlen(template)-4, EXTSEP_S "bak");
|
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
tmpfname = xmalloc (strlen( template ) + 1 );
|
2002-06-29 15:46:34 +02:00
|
|
|
|
strcpy (tmpfname,template);
|
|
|
|
|
strcpy (tmpfname+strlen(template)-4, EXTSEP_S "tmp");
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{ /* file does not end with gpg; hmmm */
|
2005-07-27 20:10:56 +02:00
|
|
|
|
bakfname = xmalloc (strlen( template ) + 5);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
strcpy (stpcpy(bakfname, template), EXTSEP_S "bak");
|
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
|
tmpfname = xmalloc (strlen( template ) + 5);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
strcpy (stpcpy(tmpfname, template), EXTSEP_S "tmp");
|
|
|
|
|
}
|
|
|
|
|
# else /* Posix file names */
|
2010-10-10 11:53:02 +02:00
|
|
|
|
bakfname = xmalloc (strlen (template) + sizeof (GNUPG_BAK_SFX) + 1);
|
|
|
|
|
strcpy (stpcpy (bakfname, template), GNUPG_BAK_SFX);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
2010-10-10 11:53:02 +02:00
|
|
|
|
tmpfname = xmalloc (strlen (template) + sizeof (GNUPG_TMP_SFX) + 1 );
|
|
|
|
|
strcpy (stpcpy(tmpfname, template), GNUPG_TMP_SFX);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
# endif /* Posix filename */
|
|
|
|
|
|
2002-08-09 04:23:42 +02:00
|
|
|
|
/* Create the temp file with limited access */
|
|
|
|
|
oldmask=umask(077);
|
2004-10-14 09:11:57 +02:00
|
|
|
|
if (is_secured_filename (tmpfname))
|
|
|
|
|
{
|
|
|
|
|
*r_fp = NULL;
|
|
|
|
|
errno = EPERM;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*r_fp = iobuf_create (tmpfname);
|
2002-08-09 04:23:42 +02:00
|
|
|
|
umask(oldmask);
|
2004-10-28 04:14:06 +02:00
|
|
|
|
if (!*r_fp)
|
|
|
|
|
{
|
|
|
|
|
log_error(_("can't create `%s': %s\n"), tmpfname, strerror(errno) );
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (tmpfname);
|
|
|
|
|
xfree (bakfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return G10ERR_OPEN_FILE;
|
2004-10-28 04:14:06 +02:00
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +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 )
|
|
|
|
|
{
|
2009-03-20 21:55:47 +01:00
|
|
|
|
int rc=G10ERR_GENERAL;
|
|
|
|
|
|
|
|
|
|
/* It's a secret keyring, so let's force a fsync just to be safe on
|
|
|
|
|
filesystems that may not sync data and metadata together
|
|
|
|
|
(ext4). */
|
|
|
|
|
if(secret && iobuf_ioctl(NULL,4,0,(char*)tmpfname)!=0)
|
|
|
|
|
goto fail;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
/* invalidate close caches*/
|
2009-03-20 21:55:47 +01:00
|
|
|
|
if(iobuf_ioctl (NULL, 2, 0, (char*)tmpfname )!=0)
|
|
|
|
|
goto fail;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_ioctl (NULL, 2, 0, (char*)bakfname );
|
|
|
|
|
iobuf_ioctl (NULL, 2, 0, (char*)fname );
|
|
|
|
|
|
|
|
|
|
/* first make a backup file except for secret keyrings */
|
|
|
|
|
if (!secret)
|
2012-01-10 15:16:44 +01:00
|
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
|
|
|
|
remove (bakfname);
|
|
|
|
|
#endif
|
|
|
|
|
if (rename (fname, bakfname) )
|
|
|
|
|
{
|
|
|
|
|
log_error ("renaming `%s' to `%s' failed: %s\n",
|
|
|
|
|
fname, bakfname, strerror(errno) );
|
|
|
|
|
return G10ERR_RENAME_FILE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
/* then rename the file */
|
|
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
|
|
|
|
remove( fname );
|
|
|
|
|
#endif
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (fname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (rename (tmpfname, fname) )
|
|
|
|
|
{
|
2004-10-13 20:10:06 +02:00
|
|
|
|
log_error (_("renaming `%s' to `%s' failed: %s\n"),
|
2002-06-29 15:46:34 +02:00
|
|
|
|
tmpfname, fname, strerror(errno) );
|
2004-10-13 20:10:06 +02:00
|
|
|
|
register_secured_file (fname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = G10ERR_RENAME_FILE;
|
2009-03-20 21:55:47 +01:00
|
|
|
|
goto fail;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-09 04:23:42 +02:00
|
|
|
|
/* Now make sure the file has the same permissions as the original */
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
{
|
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
|
|
statbuf.st_mode=S_IRUSR | S_IWUSR;
|
|
|
|
|
|
|
|
|
|
if(((secret && !opt.preserve_permissions) ||
|
|
|
|
|
(stat(bakfname,&statbuf)==0)) &&
|
|
|
|
|
(chmod(fname,statbuf.st_mode)==0))
|
|
|
|
|
;
|
|
|
|
|
else
|
|
|
|
|
log_error("WARNING: unable to restore permissions to `%s': %s",
|
|
|
|
|
fname,strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return 0;
|
2009-03-20 21:55:47 +01:00
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
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;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
write_keyblock (IOBUF fp, KBNODE keyblock)
|
|
|
|
|
{
|
|
|
|
|
KBNODE kbctx = NULL, node;
|
|
|
|
|
int rc;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
|
|
|
|
while ( (node = walk_kbnode (keyblock, &kbctx, 0)) )
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (node->pkt->pkttype == PKT_RING_TRUST)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
continue; /* we write it later on our own */
|
|
|
|
|
|
|
|
|
|
if ( (rc = build_packet (fp, node->pkt) ))
|
|
|
|
|
{
|
|
|
|
|
log_error ("build_packet(%d) failed: %s\n",
|
|
|
|
|
node->pkt->pkttype, g10_errstr(rc) );
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (node->pkt->pkttype == PKT_SIGNATURE)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{ /* always write a signature cache packet */
|
|
|
|
|
PKT_signature *sig = node->pkt->pkt.signature;
|
|
|
|
|
unsigned int cacheval = 0;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
|
|
|
|
if (sig->flags.checked)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
cacheval |= 1;
|
|
|
|
|
if (sig->flags.valid)
|
|
|
|
|
cacheval |= 2;
|
|
|
|
|
}
|
|
|
|
|
iobuf_put (fp, 0xb0); /* old style packet 12, 1 byte len*/
|
|
|
|
|
iobuf_put (fp, 2); /* 2 bytes */
|
|
|
|
|
iobuf_put (fp, 0); /* unused */
|
|
|
|
|
if (iobuf_put (fp, cacheval)) {
|
|
|
|
|
log_error ("writing sigcache packet failed\n");
|
|
|
|
|
return G10ERR_WRITE_FILE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/*
|
2002-06-29 15:46:34 +02:00
|
|
|
|
* Walk over all public keyrings, check the signatures and replace the
|
|
|
|
|
* keyring with a new one where the signature cache is then updated.
|
|
|
|
|
* This is only done for the public keyrings.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2004-01-20 17:09:38 +01:00
|
|
|
|
keyring_rebuild_cache (void *token,int noisy)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
KEYRING_HANDLE hd;
|
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
|
|
|
|
KBNODE keyblock = NULL, node;
|
|
|
|
|
const char *lastresname = NULL, *resname;
|
|
|
|
|
IOBUF tmpfp = NULL;
|
|
|
|
|
char *tmpfilename = NULL;
|
|
|
|
|
char *bakfilename = NULL;
|
|
|
|
|
int rc;
|
|
|
|
|
ulong count = 0, sigcount = 0;
|
|
|
|
|
|
|
|
|
|
hd = keyring_new (token, 0);
|
|
|
|
|
memset (&desc, 0, sizeof desc);
|
|
|
|
|
desc.mode = KEYDB_SEARCH_MODE_FIRST;
|
|
|
|
|
|
2003-03-04 16:24:12 +01:00
|
|
|
|
rc=keyring_lock (hd, 1);
|
|
|
|
|
if(rc)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2003-02-12 19:43:44 +01:00
|
|
|
|
while ( !(rc = keyring_search (hd, &desc, 1, NULL)) )
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
desc.mode = KEYDB_SEARCH_MODE_NEXT;
|
|
|
|
|
resname = keyring_get_resource_name (hd);
|
|
|
|
|
if (lastresname != resname )
|
|
|
|
|
{ /* we have switched to a new keyring - commit changes */
|
|
|
|
|
if (tmpfp)
|
|
|
|
|
{
|
|
|
|
|
if (iobuf_close (tmpfp))
|
|
|
|
|
{
|
|
|
|
|
log_error ("error closing `%s': %s\n",
|
|
|
|
|
tmpfilename, strerror (errno));
|
|
|
|
|
rc = G10ERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* because we have switched resources, we can be sure that
|
|
|
|
|
* the original file is closed */
|
|
|
|
|
tmpfp = NULL;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
|
2002-06-29 15:46:34 +02:00
|
|
|
|
lastresname, 0) : 0;
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (tmpfilename); tmpfilename = NULL;
|
|
|
|
|
xfree (bakfilename); bakfilename = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
goto leave;
|
|
|
|
|
lastresname = resname;
|
2004-01-20 17:09:38 +01:00
|
|
|
|
if (noisy && !opt.quiet)
|
|
|
|
|
log_info (_("caching keyring `%s'\n"), resname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = create_tmp_file (resname, &bakfilename, &tmpfilename, &tmpfp);
|
|
|
|
|
if (rc)
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
release_kbnode (keyblock);
|
|
|
|
|
rc = keyring_get_keyblock (hd, &keyblock);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (rc)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
log_error ("keyring_get_keyblock failed: %s\n", g10_errstr(rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2009-07-17 13:24:51 +02:00
|
|
|
|
if ( keyblock->pkt->pkttype != PKT_PUBLIC_KEY)
|
|
|
|
|
{
|
|
|
|
|
/* We had a few reports about corrupted keyrings; if we have
|
|
|
|
|
been called directly from the command line we delete such
|
|
|
|
|
a keyblock instead of bailing out. */
|
|
|
|
|
log_error ("unexpected keyblock found (pkttype=%d)%s\n",
|
|
|
|
|
keyblock->pkt->pkttype, noisy? " - deleted":"");
|
|
|
|
|
if (noisy)
|
|
|
|
|
continue;
|
|
|
|
|
log_info ("Hint: backup your keys and try running `%s'\n",
|
|
|
|
|
"gpg --rebuild-keydb-caches");
|
|
|
|
|
rc = G10ERR_INV_KEYRING;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
/* check all signature to set the signature's cache flags */
|
|
|
|
|
for (node=keyblock; node; node=node->next)
|
|
|
|
|
{
|
2004-01-19 23:46:55 +01:00
|
|
|
|
/* Note that this doesn't cache the result of a revocation
|
|
|
|
|
issued by a designated revoker. This is because the pk
|
|
|
|
|
in question does not carry the revkeys as we haven't
|
|
|
|
|
merged the key and selfsigs. It is questionable whether
|
|
|
|
|
this matters very much since there are very very few
|
|
|
|
|
designated revoker revocation packets out there. */
|
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (node->pkt->pkttype == PKT_SIGNATURE)
|
|
|
|
|
{
|
2004-01-19 23:46:55 +01:00
|
|
|
|
PKT_signature *sig=node->pkt->pkt.signature;
|
|
|
|
|
|
|
|
|
|
if(!opt.no_sig_cache && sig->flags.checked && sig->flags.valid
|
|
|
|
|
&& (check_digest_algo(sig->digest_algo)
|
|
|
|
|
|| check_pubkey_algo(sig->pubkey_algo)))
|
|
|
|
|
sig->flags.checked=sig->flags.valid=0;
|
|
|
|
|
else
|
|
|
|
|
check_key_signature (keyblock, node, NULL);
|
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
sigcount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-10 15:16:44 +01:00
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
/* write the keyblock to the temporary file */
|
|
|
|
|
rc = write_keyblock (tmpfp, keyblock);
|
|
|
|
|
if (rc)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
2004-01-20 17:09:38 +01:00
|
|
|
|
if ( !(++count % 50) && noisy && !opt.quiet)
|
|
|
|
|
log_info(_("%lu keys cached so far (%lu signatures)\n"),
|
2002-06-29 15:46:34 +02:00
|
|
|
|
count, sigcount );
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
} /* end main loop */
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (rc == -1)
|
|
|
|
|
rc = 0;
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (rc)
|
2002-06-29 15:46:34 +02:00
|
|
|
|
{
|
|
|
|
|
log_error ("keyring_search failed: %s\n", g10_errstr(rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2004-01-20 17:09:38 +01:00
|
|
|
|
if(noisy || opt.verbose)
|
|
|
|
|
log_info(_("%lu keys cached (%lu signatures)\n"), count, sigcount );
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if (tmpfp)
|
|
|
|
|
{
|
|
|
|
|
if (iobuf_close (tmpfp))
|
|
|
|
|
{
|
|
|
|
|
log_error ("error closing `%s': %s\n",
|
|
|
|
|
tmpfilename, strerror (errno));
|
|
|
|
|
rc = G10ERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* because we have switched resources, we can be sure that
|
|
|
|
|
* the original file is closed */
|
|
|
|
|
tmpfp = NULL;
|
|
|
|
|
}
|
|
|
|
|
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
|
|
|
|
|
lastresname, 0) : 0;
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree (tmpfilename); tmpfilename = NULL;
|
|
|
|
|
xfree (bakfilename); bakfilename = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
if (tmpfp)
|
|
|
|
|
iobuf_cancel (tmpfp);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
xfree (tmpfilename);
|
|
|
|
|
xfree (bakfilename);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
release_kbnode (keyblock);
|
2003-03-04 16:24:12 +01:00
|
|
|
|
keyring_lock (hd, 0);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
keyring_release (hd);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Perform insert/delete/update operation.
|
|
|
|
|
* mode 1 = insert
|
|
|
|
|
* 2 = delete
|
|
|
|
|
* 3 = update
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
do_copy (int mode, const char *fname, KBNODE root, int secret,
|
|
|
|
|
off_t start_offset, unsigned int n_packets )
|
|
|
|
|
{
|
|
|
|
|
IOBUF fp, newfp;
|
|
|
|
|
int rc=0;
|
|
|
|
|
char *bakfname = NULL;
|
|
|
|
|
char *tmpfname = NULL;
|
|
|
|
|
|
2012-01-10 15:16:44 +01:00
|
|
|
|
/* Open the source file. Because we do a rename, we have to check the
|
2002-06-29 15:46:34 +02:00
|
|
|
|
permissions of the file */
|
|
|
|
|
if (access (fname, W_OK))
|
|
|
|
|
return G10ERR_WRITE_FILE;
|
|
|
|
|
|
|
|
|
|
fp = iobuf_open (fname);
|
2012-01-10 15:16:44 +01:00
|
|
|
|
if (mode == 1 && !fp && errno == ENOENT) {
|
2002-06-29 15:46:34 +02:00
|
|
|
|
/* insert mode but file does not exist: create a new file */
|
|
|
|
|
KBNODE kbctx, node;
|
2002-08-09 04:23:42 +02:00
|
|
|
|
mode_t oldmask;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
2002-08-09 04:23:42 +02:00
|
|
|
|
oldmask=umask(077);
|
2004-10-14 09:11:57 +02:00
|
|
|
|
if (!secret && is_secured_filename (fname)) {
|
|
|
|
|
newfp = NULL;
|
|
|
|
|
errno = EPERM;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
newfp = iobuf_create (fname);
|
2002-08-09 04:23:42 +02:00
|
|
|
|
umask(oldmask);
|
2004-10-28 04:14:06 +02:00
|
|
|
|
if( !newfp )
|
|
|
|
|
{
|
|
|
|
|
log_error (_("can't create `%s': %s\n"), fname, strerror(errno));
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return G10ERR_OPEN_FILE;
|
2004-10-28 04:14:06 +02:00
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if( !opt.quiet )
|
|
|
|
|
log_info(_("%s: keyring created\n"), fname );
|
|
|
|
|
|
|
|
|
|
kbctx=NULL;
|
|
|
|
|
while ( (node = walk_kbnode( root, &kbctx, 0 )) ) {
|
|
|
|
|
if( (rc = build_packet( newfp, node->pkt )) ) {
|
|
|
|
|
log_error("build_packet(%d) failed: %s\n",
|
|
|
|
|
node->pkt->pkttype, g10_errstr(rc) );
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
return G10ERR_WRITE_FILE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error ("%s: close failed: %s\n", fname, strerror(errno));
|
|
|
|
|
return G10ERR_CLOSE_FILE;
|
|
|
|
|
}
|
|
|
|
|
return 0; /* ready */
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-28 04:14:06 +02:00
|
|
|
|
if( !fp )
|
|
|
|
|
{
|
|
|
|
|
log_error(_("can't open `%s': %s\n"), fname, strerror(errno) );
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = G10ERR_OPEN_FILE;
|
|
|
|
|
goto leave;
|
2004-10-28 04:14:06 +02:00
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
2004-10-13 20:10:06 +02:00
|
|
|
|
/* Create the new file. */
|
2002-06-29 15:46:34 +02:00
|
|
|
|
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
|
|
|
|
|
if (rc) {
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
register_secured_file (tmpfname);
|
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
if( mode == 1 ) { /* insert */
|
|
|
|
|
/* copy everything to the new file */
|
|
|
|
|
rc = copy_all_packets (fp, newfp);
|
|
|
|
|
if( rc != -1 ) {
|
|
|
|
|
log_error("%s: copy to `%s' failed: %s\n",
|
|
|
|
|
fname, tmpfname, g10_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( mode == 2 || mode == 3 ) { /* delete or update */
|
|
|
|
|
/* copy first part to the new file */
|
|
|
|
|
rc = copy_some_packets( fp, newfp, start_offset );
|
|
|
|
|
if( rc ) { /* should never get EOF here */
|
|
|
|
|
log_error ("%s: copy to `%s' failed: %s\n",
|
|
|
|
|
fname, tmpfname, g10_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* skip this keyblock */
|
|
|
|
|
assert( n_packets );
|
|
|
|
|
rc = skip_some_packets( fp, n_packets );
|
|
|
|
|
if( rc ) {
|
|
|
|
|
log_error("%s: skipping %u packets failed: %s\n",
|
|
|
|
|
fname, n_packets, g10_errstr(rc));
|
|
|
|
|
iobuf_close(fp);
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( mode == 1 || mode == 3 ) { /* insert or update */
|
|
|
|
|
rc = write_keyblock (newfp, root);
|
|
|
|
|
if (rc) {
|
|
|
|
|
iobuf_close(fp);
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( mode == 2 || mode == 3 ) { /* delete or update */
|
|
|
|
|
/* copy the rest */
|
|
|
|
|
rc = copy_all_packets( fp, newfp );
|
|
|
|
|
if( rc != -1 ) {
|
|
|
|
|
log_error("%s: copy to `%s' failed: %s\n",
|
|
|
|
|
fname, tmpfname, g10_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
2004-10-13 20:10:06 +02:00
|
|
|
|
if (secret)
|
|
|
|
|
unregister_secured_file (tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* close both files */
|
|
|
|
|
if( iobuf_close(fp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", fname, strerror(errno) );
|
|
|
|
|
rc = G10ERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
|
|
|
|
|
rc = G10ERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
|
|
|
|
|
|
|
|
|
|
leave:
|
2005-07-27 20:10:56 +02:00
|
|
|
|
xfree(bakfname);
|
|
|
|
|
xfree(tmpfname);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
return rc;
|
|
|
|
|
}
|