1997-12-12 12:03:58 +00:00
|
|
|
|
/* ringedit.c - Function for key ring editing
|
2000-07-14 17:34:53 +00:00
|
|
|
|
* Copyright (C) 1998, 2000 Free Software Foundation, Inc.
|
1997-12-12 12:03:58 +00:00
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
|
* This file is part of GnuPG.
|
1997-12-12 12:03:58 +00:00
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-12-12 12:03:58 +00:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-12-12 12:03:58 +00:00
|
|
|
|
* 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
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* This module supplies function for:
|
|
|
|
|
*
|
|
|
|
|
* - Search for a key block (pubkey and all other stuff) and return a
|
|
|
|
|
* handle for it.
|
|
|
|
|
*
|
|
|
|
|
* - Lock/Unlock a key block
|
|
|
|
|
*
|
|
|
|
|
* - Read a key block into a tree
|
|
|
|
|
*
|
|
|
|
|
* - Update a key block
|
|
|
|
|
*
|
|
|
|
|
* - Insert a new key block
|
|
|
|
|
*
|
|
|
|
|
* - Delete a key block
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
1998-02-11 03:25:44 +00:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
1998-10-16 16:00:17 +00:00
|
|
|
|
#include <unistd.h> /* for truncate */
|
1997-12-12 12:03:58 +00:00
|
|
|
|
#include <assert.h>
|
2000-07-14 17:34:53 +00:00
|
|
|
|
|
|
|
|
|
#include <gcrypt.h>
|
1997-12-12 12:03:58 +00:00
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "packet.h"
|
|
|
|
|
#include "iobuf.h"
|
|
|
|
|
#include "keydb.h"
|
1998-10-21 17:34:36 +00:00
|
|
|
|
#include "host2net.h"
|
1998-10-16 16:00:17 +00:00
|
|
|
|
#include "options.h"
|
1998-10-25 19:00:01 +00:00
|
|
|
|
#include "main.h"
|
1998-08-07 08:53:38 +00:00
|
|
|
|
#include "i18n.h"
|
2000-03-13 18:19:12 +00:00
|
|
|
|
#include "kbx.h"
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
1998-10-18 15:21:22 +00:00
|
|
|
|
|
1999-09-06 18:10:27 +00:00
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
struct resource_table_struct {
|
|
|
|
|
int used;
|
1998-01-06 21:01:36 +00:00
|
|
|
|
int secret; /* this is a secret keyring */
|
1997-12-12 12:03:58 +00:00
|
|
|
|
char *fname;
|
|
|
|
|
IOBUF iobuf;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
enum resource_type rt;
|
1999-02-10 16:22:40 +00:00
|
|
|
|
DOTLOCK lockhd;
|
|
|
|
|
int is_locked;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
};
|
1997-12-16 19:15:09 +00:00
|
|
|
|
typedef struct resource_table_struct RESTBL;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
|
|
|
|
|
struct keyblock_pos_struct {
|
|
|
|
|
int resno; /* resource number */
|
|
|
|
|
enum resource_type rt;
|
|
|
|
|
ulong offset; /* position information */
|
|
|
|
|
unsigned count; /* length of the keyblock in packets */
|
|
|
|
|
IOBUF fp; /* used by enum_keyblocks */
|
|
|
|
|
int secret; /* working on a secret keyring */
|
|
|
|
|
PACKET *pkt; /* ditto */
|
|
|
|
|
int valid;
|
|
|
|
|
ulong save_offset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
#define MAX_RESOURCES 10
|
1997-12-16 19:15:09 +00:00
|
|
|
|
static RESTBL resource_table[MAX_RESOURCES];
|
1999-02-26 16:59:48 +00:00
|
|
|
|
static int default_public_resource;
|
|
|
|
|
static int default_secret_resource;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
static int keyring_enum( KBPOS kbpos, KBNODE *ret_root, int skipsigs );
|
|
|
|
|
static int keyring_copy( KBPOS kbpos, int mode, KBNODE root );
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
static int do_kbxf_enum( KBPOS kbpos, KBNODE *ret_root, int skipsigs );
|
|
|
|
|
static int do_kbxf_copy( KBPOS kbpos, int mode, KBNODE root );
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
|
|
|
|
|
1997-12-16 19:15:09 +00:00
|
|
|
|
static RESTBL *
|
2000-10-06 12:28:44 +00:00
|
|
|
|
check_pos( KBPOS kbpos )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
|
|
|
|
if( kbpos->resno < 0 || kbpos->resno >= MAX_RESOURCES )
|
1997-12-16 19:15:09 +00:00
|
|
|
|
return NULL;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
if( !resource_table[kbpos->resno].used )
|
1997-12-16 19:15:09 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
return resource_table + kbpos->resno;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-10 16:22:40 +00:00
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Hmmm, how to avoid deadlock? They should not happen if everyone
|
|
|
|
|
* locks the key resources in the same order; but who knows.
|
|
|
|
|
* A solution is to use only one lock file in the gnupg homedir but
|
|
|
|
|
* what will happen with key resources which normally don't belong
|
|
|
|
|
* to the gpg homedir?
|
|
|
|
|
*/
|
1998-11-27 20:40:56 +00:00
|
|
|
|
static void
|
1999-02-10 16:22:40 +00:00
|
|
|
|
lock_rentry( RESTBL *rentry )
|
1998-11-27 20:40:56 +00:00
|
|
|
|
{
|
1999-02-10 16:22:40 +00:00
|
|
|
|
if( !rentry->lockhd ) {
|
|
|
|
|
rentry->lockhd = create_dotlock( rentry->fname );
|
|
|
|
|
if( !rentry->lockhd )
|
|
|
|
|
log_fatal("can't allocate lock for `%s'\n", rentry->fname );
|
|
|
|
|
rentry->is_locked = 0;
|
|
|
|
|
}
|
|
|
|
|
if( !rentry->is_locked ) {
|
|
|
|
|
if( make_dotlock( rentry->lockhd, -1 ) )
|
|
|
|
|
log_fatal("can't lock `%s'\n", rentry->fname );
|
|
|
|
|
rentry->is_locked = 1;
|
1998-11-27 20:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-02-10 16:22:40 +00:00
|
|
|
|
static void
|
|
|
|
|
unlock_rentry( RESTBL *rentry )
|
|
|
|
|
{
|
|
|
|
|
if( opt.lock_once )
|
|
|
|
|
return;
|
|
|
|
|
if( !release_dotlock( rentry->lockhd ) )
|
|
|
|
|
rentry->is_locked = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
/****************************************************************
|
|
|
|
|
****************** public functions ****************************
|
|
|
|
|
****************************************************************/
|
|
|
|
|
|
1998-10-16 16:00:17 +00:00
|
|
|
|
/****************
|
|
|
|
|
* Get the name of the keyrings, start with a sequence number pointing to a 0.
|
|
|
|
|
*/
|
|
|
|
|
const char *
|
|
|
|
|
enum_keyblock_resources( int *sequence, int secret )
|
|
|
|
|
{
|
|
|
|
|
int i = *sequence;
|
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
|
|
for(; i < MAX_RESOURCES; i++ )
|
|
|
|
|
if( resource_table[i].used && !resource_table[i].secret == !secret ) {
|
|
|
|
|
if( resource_table[i].fname ) {
|
|
|
|
|
name = resource_table[i].fname;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*sequence = ++i;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
return name;
|
1998-10-16 16:00:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
/****************
|
1998-04-14 17:51:16 +00:00
|
|
|
|
* Register a resource (which currently may only be a keyring file).
|
1998-10-16 16:00:17 +00:00
|
|
|
|
* The first keyring which is added by this function is
|
|
|
|
|
* created if it does not exist.
|
|
|
|
|
* Note: this function may be called before secure memory is
|
|
|
|
|
* available.
|
1997-12-12 12:03:58 +00:00
|
|
|
|
*/
|
|
|
|
|
int
|
1998-10-17 14:47:14 +00:00
|
|
|
|
add_keyblock_resource( const char *url, int force, int secret )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
1998-10-16 16:00:17 +00:00
|
|
|
|
static int any_secret, any_public;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
const char *resname = url;
|
1998-10-25 19:00:01 +00:00
|
|
|
|
IOBUF iobuf = NULL;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
int i;
|
|
|
|
|
char *filename = NULL;
|
1998-10-16 16:00:17 +00:00
|
|
|
|
int rc = 0;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
enum resource_type rt = rt_UNKNOWN;
|
|
|
|
|
|
1998-11-27 20:40:56 +00:00
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
/* Do we have an URL?
|
2000-03-13 18:19:12 +00:00
|
|
|
|
* gnupg-kbxf:filename := this is a KBX file resource
|
1998-10-17 14:47:14 +00:00
|
|
|
|
* gnupg-ring:filename := this is a plain keyring
|
|
|
|
|
* filename := See what is is, but create as plain keyring.
|
|
|
|
|
*/
|
|
|
|
|
if( strlen( resname ) > 11 ) {
|
|
|
|
|
if( !strncmp( resname, "gnupg-ring:", 11 ) ) {
|
|
|
|
|
rt = rt_RING;
|
|
|
|
|
resname += 11;
|
|
|
|
|
}
|
2000-03-13 18:19:12 +00:00
|
|
|
|
else if( !strncmp( resname, "gnupg-kbxf:", 11 ) ) {
|
|
|
|
|
rt = rt_KBXF;
|
|
|
|
|
resname += 11;
|
|
|
|
|
}
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#ifndef HAVE_DRIVE_LETTERS
|
1998-10-17 14:47:14 +00:00
|
|
|
|
else if( strchr( resname, ':' ) ) {
|
|
|
|
|
log_error("%s: invalid URL\n", url );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_GENERAL;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
1998-10-16 16:00:17 +00:00
|
|
|
|
|
|
|
|
|
if( *resname != '/' ) { /* do tilde expansion etc */
|
|
|
|
|
if( strchr(resname, '/') )
|
|
|
|
|
filename = make_filename(resname, NULL);
|
|
|
|
|
else
|
|
|
|
|
filename = make_filename(opt.homedir, resname, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
2000-01-24 11:55:49 +00:00
|
|
|
|
filename = gcry_xstrdup( resname );
|
1998-10-16 16:00:17 +00:00
|
|
|
|
|
|
|
|
|
if( !force )
|
|
|
|
|
force = secret? !any_secret : !any_public;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
|
|
|
|
for(i=0; i < MAX_RESOURCES; i++ )
|
|
|
|
|
if( !resource_table[i].used )
|
|
|
|
|
break;
|
1998-10-16 16:00:17 +00:00
|
|
|
|
if( i == MAX_RESOURCES ) {
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_RESOURCE_LIMIT;
|
1998-10-16 16:00:17 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
1998-10-21 17:34:36 +00:00
|
|
|
|
/* see whether we can determine the filetype */
|
|
|
|
|
if( rt == rt_UNKNOWN ) {
|
|
|
|
|
FILE *fp = fopen( filename, "rb" );
|
|
|
|
|
|
|
|
|
|
if( fp ) {
|
|
|
|
|
u32 magic;
|
|
|
|
|
|
|
|
|
|
if( fread( &magic, 4, 1, fp) == 1 ) {
|
2000-10-06 12:28:44 +00:00
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
|
|
rt = rt_RING;
|
|
|
|
|
if( fread( buf, 8, 1, fp) == 1 ) {
|
|
|
|
|
if( !memcmp( buf+4, "KBXf", 4 )
|
|
|
|
|
&& buf[0] == 1 && buf[1] == 1 ) {
|
|
|
|
|
rt = rt_KBXF;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-25 19:00:01 +00:00
|
|
|
|
else /* maybe empty: assume ring */
|
|
|
|
|
rt = rt_RING;
|
1998-10-21 17:34:36 +00:00
|
|
|
|
fclose( fp );
|
|
|
|
|
}
|
|
|
|
|
else /* no file yet: create ring */
|
|
|
|
|
rt = rt_RING;
|
|
|
|
|
}
|
1998-07-06 10:23:57 +00:00
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
switch( rt ) {
|
|
|
|
|
case rt_UNKNOWN:
|
1998-10-21 17:34:36 +00:00
|
|
|
|
log_error("%s: unknown resource type\n", url );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_GENERAL;
|
1998-10-21 17:34:36 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
case rt_RING:
|
2000-03-13 18:19:12 +00:00
|
|
|
|
case rt_KBXF:
|
1998-12-10 19:20:47 +00:00
|
|
|
|
iobuf = iobuf_open( filename );
|
1998-10-17 14:47:14 +00:00
|
|
|
|
if( !iobuf && !force ) {
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
1998-10-16 16:00:17 +00:00
|
|
|
|
goto leave;
|
1998-07-06 10:23:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
if( !iobuf ) {
|
1998-10-21 17:34:36 +00:00
|
|
|
|
char *last_slash_in_filename;
|
|
|
|
|
|
|
|
|
|
last_slash_in_filename = strrchr(filename, '/');
|
|
|
|
|
*last_slash_in_filename = 0;
|
|
|
|
|
|
|
|
|
|
if( access(filename, F_OK) ) {
|
2000-07-14 17:34:53 +00:00
|
|
|
|
/* on the first time we try to create the default homedir and
|
|
|
|
|
* in this case the process will be terminated, so that on the
|
|
|
|
|
* next invocation it can read the options file in on startup
|
|
|
|
|
*/
|
|
|
|
|
try_make_homedir( filename );
|
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
|
|
|
|
goto leave;
|
1998-10-21 17:34:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*last_slash_in_filename = '/';
|
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
iobuf = iobuf_create( filename );
|
|
|
|
|
if( !iobuf ) {
|
1998-11-10 12:59:59 +00:00
|
|
|
|
log_error(_("%s: can't create keyring: %s\n"),
|
|
|
|
|
filename, strerror(errno));
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1999-04-07 18:58:34 +00:00
|
|
|
|
else {
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
if( secret ) {
|
|
|
|
|
if( chmod( filename, S_IRUSR | S_IWUSR ) ) {
|
|
|
|
|
log_error("%s: chmod failed: %s\n",
|
|
|
|
|
filename, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_WRITE_FILE;
|
1999-04-07 18:58:34 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
1999-04-18 08:18:52 +00:00
|
|
|
|
if( !opt.quiet )
|
|
|
|
|
log_info(_("%s: keyring created\n"), filename );
|
1999-04-07 18:58:34 +00:00
|
|
|
|
}
|
1998-10-17 14:47:14 +00:00
|
|
|
|
}
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#if HAVE_DOSISH_SYSTEM || 1
|
1998-10-17 14:47:14 +00:00
|
|
|
|
iobuf_close( iobuf );
|
|
|
|
|
iobuf = NULL;
|
1999-02-10 16:22:40 +00:00
|
|
|
|
/* must close it again */
|
1998-10-17 14:47:14 +00:00
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
log_error("%s: unsupported resource type\n", url );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_GENERAL;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1998-10-06 12:10:02 +00:00
|
|
|
|
|
1999-04-07 18:58:34 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
#if 0 /* fixme: check directory permissions and print a warning */
|
|
|
|
|
if( secret ) {
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-02-26 16:59:48 +00:00
|
|
|
|
/* fixme: avoid duplicate resources */
|
1997-12-12 12:03:58 +00:00
|
|
|
|
resource_table[i].used = 1;
|
1998-01-06 21:01:36 +00:00
|
|
|
|
resource_table[i].secret = !!secret;
|
2000-01-24 11:55:49 +00:00
|
|
|
|
resource_table[i].fname = gcry_xstrdup(filename);
|
1997-12-12 12:03:58 +00:00
|
|
|
|
resource_table[i].iobuf = iobuf;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
resource_table[i].rt = rt;
|
1999-02-26 16:59:48 +00:00
|
|
|
|
if( secret )
|
|
|
|
|
default_secret_resource = i;
|
|
|
|
|
else
|
|
|
|
|
default_public_resource = i;
|
|
|
|
|
|
1998-10-16 16:00:17 +00:00
|
|
|
|
leave:
|
|
|
|
|
if( rc )
|
2000-01-27 16:50:45 +00:00
|
|
|
|
log_error("keyblock resource `%s': %s\n", filename, gpg_errstr(rc) );
|
1998-10-16 16:00:17 +00:00
|
|
|
|
else if( secret )
|
|
|
|
|
any_secret = 1;
|
|
|
|
|
else
|
|
|
|
|
any_public = 1;
|
2000-01-24 11:55:49 +00:00
|
|
|
|
gcry_free( filename );
|
1998-10-16 16:00:17 +00:00
|
|
|
|
return rc;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-02-16 20:05:02 +00:00
|
|
|
|
/****************
|
|
|
|
|
* Return the resource name of the keyblock associated with KBPOS.
|
|
|
|
|
*/
|
|
|
|
|
const char *
|
2000-10-06 12:28:44 +00:00
|
|
|
|
keyblock_resource_name( KBPOS kbpos )
|
1998-02-16 20:05:02 +00:00
|
|
|
|
{
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
|
|
|
|
|
if( !(rentry = check_pos( kbpos )) || !rentry->fname )
|
|
|
|
|
log_bug("no name for keyblock resource %d\n", kbpos->resno );
|
|
|
|
|
return rentry->fname;
|
|
|
|
|
}
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Get a keyblock handle KBPOS from a filename. This can be used
|
|
|
|
|
* to get a handle for insert_keyblock for a new keyblock.
|
1998-02-16 20:05:02 +00:00
|
|
|
|
* Using a filename of NULL returns the default resource
|
1997-12-12 12:03:58 +00:00
|
|
|
|
*/
|
|
|
|
|
int
|
2000-10-06 12:28:44 +00:00
|
|
|
|
get_keyblock_handle( const char *filename, int secret, KBPOS kbpos )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
1999-02-26 16:59:48 +00:00
|
|
|
|
int i = 0;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
1999-02-26 16:59:48 +00:00
|
|
|
|
if( !filename )
|
|
|
|
|
i = secret? default_secret_resource : default_public_resource;
|
|
|
|
|
|
|
|
|
|
for(; i < MAX_RESOURCES; i++ ) {
|
1998-01-06 21:01:36 +00:00
|
|
|
|
if( resource_table[i].used && !resource_table[i].secret == !secret ) {
|
1997-12-12 12:03:58 +00:00
|
|
|
|
/* fixme: dos needs case insensitive file compare */
|
1998-02-16 20:05:02 +00:00
|
|
|
|
if( !filename || !strcmp( resource_table[i].fname, filename ) ) {
|
1997-12-12 12:03:58 +00:00
|
|
|
|
memset( kbpos, 0, sizeof *kbpos );
|
|
|
|
|
kbpos->resno = i;
|
1998-10-18 15:21:22 +00:00
|
|
|
|
kbpos->rt = resource_table[i].rt;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-02-26 16:59:48 +00:00
|
|
|
|
}
|
1997-12-12 12:03:58 +00:00
|
|
|
|
return -1; /* not found */
|
|
|
|
|
}
|
|
|
|
|
|
1998-01-16 21:15:24 +00:00
|
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
|
/****************
|
|
|
|
|
* Return the filename of the firstkeyblock resource which is intended
|
|
|
|
|
* for write access. This will either be the default resource or in
|
|
|
|
|
* case this is not writable one of the others. If no writable is found,
|
|
|
|
|
* the default filename in the homedirectory will be returned.
|
|
|
|
|
* Caller must free, will never return NULL.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_writable_keyblock_file( int secret )
|
|
|
|
|
{
|
|
|
|
|
int i = secret? default_secret_resource : default_public_resource;
|
|
|
|
|
|
|
|
|
|
if( resource_table[i].used && !resource_table[i].secret == !secret ) {
|
|
|
|
|
if( !access( resource_table[i].fname, R_OK|W_OK ) ) {
|
|
|
|
|
return gcry_xstrdup( resource_table[i].fname );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(i=0; i < MAX_RESOURCES; i++ ) {
|
|
|
|
|
if( resource_table[i].used && !resource_table[i].secret == !secret ) {
|
|
|
|
|
if( !access( resource_table[i].fname, R_OK|W_OK ) ) {
|
|
|
|
|
return gcry_xstrdup( resource_table[i].fname );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Assume the home dir is always writable */
|
|
|
|
|
return make_filename(opt.homedir, secret? "secring.gpg"
|
|
|
|
|
: "pubring.gpg", NULL );
|
|
|
|
|
}
|
|
|
|
|
|
1998-01-16 21:15:24 +00:00
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
void
|
|
|
|
|
ringedit_copy_kbpos ( KBPOS d, KBPOS s )
|
1998-10-21 17:34:36 +00:00
|
|
|
|
{
|
2000-10-06 12:28:44 +00:00
|
|
|
|
*d = *s;
|
1998-10-21 17:34:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
/****************
|
|
|
|
|
* Lock the keyblock; wait until it's available
|
|
|
|
|
* This function may change the internal data in kbpos, in cases
|
1998-04-14 17:51:16 +00:00
|
|
|
|
* when the keyblock to be locked has been modified.
|
1998-01-16 21:15:24 +00:00
|
|
|
|
* fixme: remove this function and add an option to search()?
|
1997-12-12 12:03:58 +00:00
|
|
|
|
*/
|
2000-10-06 12:28:44 +00:00
|
|
|
|
static int
|
|
|
|
|
lock_keyblock( KBPOS kbpos )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
1997-12-16 19:15:09 +00:00
|
|
|
|
if( !check_pos(kbpos) )
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_GENERAL;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Release a lock on a keyblock
|
|
|
|
|
*/
|
2000-10-06 12:28:44 +00:00
|
|
|
|
static void
|
|
|
|
|
unlock_keyblock( KBPOS kbpos )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
1997-12-16 19:15:09 +00:00
|
|
|
|
if( !check_pos(kbpos) )
|
1998-01-16 21:15:24 +00:00
|
|
|
|
BUG();
|
1997-12-12 12:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
|
2000-10-10 12:58:43 +00:00
|
|
|
|
static int
|
|
|
|
|
enum_keyrings_open_helper( KBPOS kbpos, int where )
|
|
|
|
|
{
|
|
|
|
|
int i = where;
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
|
|
|
|
|
for(; i < MAX_RESOURCES; i++ )
|
|
|
|
|
if( resource_table[i].used
|
|
|
|
|
&& !resource_table[i].secret == !kbpos->secret )
|
|
|
|
|
break;
|
|
|
|
|
if( i == MAX_RESOURCES )
|
|
|
|
|
return -1; /* no resources */
|
|
|
|
|
kbpos->resno = i;
|
|
|
|
|
rentry = check_pos( kbpos );
|
|
|
|
|
kbpos->rt = resource_table[i].rt;
|
|
|
|
|
kbpos->valid = 0;
|
|
|
|
|
switch( kbpos->rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
case rt_KBXF:
|
|
|
|
|
kbpos->fp = iobuf_open( rentry->fname );
|
|
|
|
|
if ( !kbpos->fp ) {
|
|
|
|
|
log_error("can't open `%s'\n", rentry->fname );
|
|
|
|
|
return GPGERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: BUG();
|
|
|
|
|
}
|
|
|
|
|
kbpos->pkt = NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
/****************
|
2000-10-10 12:58:43 +00:00
|
|
|
|
* This set of functions is used to scan over all keyrings.
|
|
|
|
|
* The mode in enum_keyblocks_next() is used liek this:
|
|
|
|
|
* Mode is: 1 = read
|
1998-03-19 15:27:29 +00:00
|
|
|
|
* 11 = read but skip signature and comment packets.
|
1998-02-13 20:58:50 +00:00
|
|
|
|
*/
|
|
|
|
|
int
|
2000-10-10 12:58:43 +00:00
|
|
|
|
enum_keyblocks_begin( KBPOS *rkbpos, int use_secret )
|
1998-02-13 20:58:50 +00:00
|
|
|
|
{
|
2000-10-10 12:58:43 +00:00
|
|
|
|
int rc, i;
|
|
|
|
|
KBPOS kbpos;
|
|
|
|
|
|
|
|
|
|
*rkbpos = NULL;
|
|
|
|
|
|
|
|
|
|
kbpos = gcry_xcalloc( 1, sizeof *kbpos );
|
|
|
|
|
kbpos->fp = NULL;
|
|
|
|
|
kbpos->rt = rt_UNKNOWN;
|
|
|
|
|
if( !use_secret ) {
|
|
|
|
|
kbpos->secret = 0;
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
kbpos->secret = 1;
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = enum_keyrings_open_helper( kbpos, i );
|
|
|
|
|
if ( rc ) {
|
|
|
|
|
gcry_free( kbpos );
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
/* return the handle */
|
|
|
|
|
*rkbpos = kbpos;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
1998-02-13 20:58:50 +00:00
|
|
|
|
|
2000-10-10 12:58:43 +00:00
|
|
|
|
void
|
|
|
|
|
enum_keyblocks_end( KBPOS kbpos )
|
|
|
|
|
{
|
|
|
|
|
if ( !kbpos )
|
|
|
|
|
return;
|
|
|
|
|
switch( kbpos->rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
case rt_KBXF:
|
|
|
|
|
if( kbpos->fp ) {
|
|
|
|
|
iobuf_close( kbpos->fp );
|
|
|
|
|
kbpos->fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case rt_UNKNOWN:
|
|
|
|
|
/* this happens when we have no keyring at all */
|
|
|
|
|
gcry_free( kbpos );
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
BUG();
|
|
|
|
|
}
|
|
|
|
|
/* release pending packet */
|
|
|
|
|
free_packet( kbpos->pkt );
|
|
|
|
|
gcry_free( kbpos->pkt );
|
|
|
|
|
gcry_free( kbpos );
|
|
|
|
|
}
|
1999-10-26 12:14:37 +00:00
|
|
|
|
|
2000-10-10 12:58:43 +00:00
|
|
|
|
int
|
|
|
|
|
enum_keyblocks_next( KBPOS kbpos, int mode, KBNODE *ret_root )
|
|
|
|
|
{
|
|
|
|
|
int cont, rc = 0;
|
|
|
|
|
RESTBL *rentry;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
|
2000-10-10 12:58:43 +00:00
|
|
|
|
if( mode != 1 && mode != 11 )
|
|
|
|
|
return GPGERR_INV_ARG;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
cont = 0;
|
|
|
|
|
switch( kbpos->rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
if( !kbpos->fp )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
rc = keyring_enum( kbpos, ret_root, mode == 11 );
|
|
|
|
|
break;
|
|
|
|
|
case rt_KBXF:
|
|
|
|
|
if( !kbpos->fp )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
rc = do_kbxf_enum( kbpos, ret_root, mode == 11 );
|
|
|
|
|
break;
|
|
|
|
|
default: BUG();
|
|
|
|
|
}
|
1998-10-17 14:47:14 +00:00
|
|
|
|
|
2000-10-10 12:58:43 +00:00
|
|
|
|
if( rc == -1 ) {
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
assert( !kbpos->pkt );
|
|
|
|
|
rentry = check_pos( kbpos );
|
|
|
|
|
assert(rentry);
|
|
|
|
|
i = kbpos->resno+1;
|
|
|
|
|
/* first close */
|
|
|
|
|
if( kbpos->fp ) {
|
|
|
|
|
iobuf_close( kbpos->fp );
|
|
|
|
|
kbpos->fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
free_packet( kbpos->pkt );
|
|
|
|
|
gcry_free( kbpos->pkt );
|
|
|
|
|
kbpos->pkt = NULL;
|
|
|
|
|
/* and then open the next one */
|
|
|
|
|
rc = enum_keyrings_open_helper( kbpos, i );
|
|
|
|
|
if ( !rc )
|
|
|
|
|
cont = 1;
|
|
|
|
|
/* hmm, that is not really correct: if we got an error kbpos
|
|
|
|
|
* might be not well anymore */
|
|
|
|
|
}
|
|
|
|
|
} while(cont);
|
1999-07-22 18:11:55 +00:00
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
/****************
|
|
|
|
|
* Insert the keyblock described by ROOT into the keyring described
|
|
|
|
|
* by KBPOS. This actually appends the data to the keyfile.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2000-10-06 12:28:44 +00:00
|
|
|
|
insert_keyblock( KBNODE root )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
#if 0
|
1997-12-16 19:15:09 +00:00
|
|
|
|
if( !check_pos(kbpos) )
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_GENERAL;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
switch( kbpos->rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
rc = keyring_copy( kbpos, 1, root );
|
|
|
|
|
break;
|
2000-03-13 18:19:12 +00:00
|
|
|
|
case rt_KBXF:
|
|
|
|
|
rc = do_kbxf_copy( kbpos, 1, root );
|
|
|
|
|
break;
|
1998-10-17 14:47:14 +00:00
|
|
|
|
default: BUG();
|
|
|
|
|
}
|
2000-10-06 12:28:44 +00:00
|
|
|
|
#endif
|
1997-12-12 12:03:58 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Delete the keyblock described by KBPOS.
|
|
|
|
|
* The current code simply changes the keyblock in the keyring
|
1998-04-14 17:51:16 +00:00
|
|
|
|
* to packet of type 0 with the correct length. To help detect errors,
|
1997-12-12 12:03:58 +00:00
|
|
|
|
* zero bytes are written.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2000-10-06 12:28:44 +00:00
|
|
|
|
delete_keyblock( KBNODE keyblock )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
#if 0
|
|
|
|
|
if( !check_pos(kbpos) )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
|
|
|
|
|
switch( kbpos->rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
rc = keyring_copy( kbpos, 2, NULL );
|
|
|
|
|
break;
|
|
|
|
|
case rt_KBXF:
|
|
|
|
|
rc = do_kbxf_copy( kbpos, 2, NULL );
|
|
|
|
|
break;
|
|
|
|
|
default: BUG();
|
1997-12-12 12:03:58 +00:00
|
|
|
|
}
|
2000-10-06 12:28:44 +00:00
|
|
|
|
#endif
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
1997-12-12 12:03:58 +00:00
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Update the keyblock in the ring (or whatever resource) one in ROOT.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
update_keyblock( KBNODE root )
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
2000-10-10 12:58:43 +00:00
|
|
|
|
struct keyblock_pos_struct kbpos;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
|
|
|
|
|
/* We need to get the file position of original keyblock first */
|
|
|
|
|
if ( root->pkt->pkttype == PKT_PUBLIC_KEY )
|
|
|
|
|
rc = find_kblocation_bypk( &kbpos, root->pkt->pkt.public_key );
|
|
|
|
|
else if ( root->pkt->pkttype == PKT_SECRET_KEY )
|
|
|
|
|
rc = find_kblocation_bysk( &kbpos, root->pkt->pkt.secret_key );
|
1998-02-13 20:58:50 +00:00
|
|
|
|
else
|
2000-10-06 12:28:44 +00:00
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
|
|
if ( rc )
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if( !check_pos(&kbpos) )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
|
|
|
|
|
switch( kbpos.rt ) {
|
|
|
|
|
case rt_RING:
|
|
|
|
|
rc = keyring_copy( &kbpos, 3, root );
|
|
|
|
|
break;
|
|
|
|
|
case rt_KBXF:
|
|
|
|
|
rc = do_kbxf_copy( &kbpos, 3, root );
|
|
|
|
|
break;
|
|
|
|
|
default: BUG();
|
|
|
|
|
}
|
|
|
|
|
|
1997-12-12 12:03:58 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-10-06 12:28:44 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
|
********** Functions which operates on regular keyrings ********
|
|
|
|
|
****************************************************************/
|
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
static int
|
2000-10-10 12:58:43 +00:00
|
|
|
|
keyring_enum( KBPOS kbpos, KBNODE *ret_root, int skipsigs )
|
1998-02-13 20:58:50 +00:00
|
|
|
|
{
|
|
|
|
|
PACKET *pkt;
|
|
|
|
|
int rc;
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
KBNODE root = NULL;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
ulong offset, first_offset=0;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
if( !(rentry=check_pos(kbpos)) )
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_GENERAL;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
|
|
|
|
|
if( kbpos->pkt ) {
|
|
|
|
|
root = new_kbnode( kbpos->pkt );
|
2000-10-06 12:28:44 +00:00
|
|
|
|
first_offset = kbpos->save_offset;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
kbpos->pkt = NULL;
|
|
|
|
|
}
|
2000-10-06 12:28:44 +00:00
|
|
|
|
kbpos->valid = 0;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
|
2000-01-24 11:55:49 +00:00
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
1998-02-13 20:58:50 +00:00
|
|
|
|
init_packet(pkt);
|
2000-10-06 12:28:44 +00:00
|
|
|
|
while( (rc=parse_packet(kbpos->fp, pkt, &offset )) != -1 ) {
|
1998-02-13 20:58:50 +00:00
|
|
|
|
if( rc ) { /* ignore errors */
|
2000-01-27 16:50:45 +00:00
|
|
|
|
if( rc != GPGERR_UNKNOWN_PACKET ) {
|
2000-10-06 12:28:44 +00:00
|
|
|
|
log_error("keyring_enum: read error: %s\n", gpg_errstr(rc) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_INV_KEYRING;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
goto ready;
|
|
|
|
|
}
|
|
|
|
|
free_packet( pkt );
|
1998-02-16 20:05:02 +00:00
|
|
|
|
init_packet( pkt );
|
1998-02-13 20:58:50 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* make a linked list of all packets */
|
|
|
|
|
switch( pkt->pkttype ) {
|
1998-12-23 12:41:40 +00:00
|
|
|
|
case PKT_COMPRESSED:
|
|
|
|
|
log_error("skipped compressed packet in keyring\n" );
|
|
|
|
|
free_packet(pkt);
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
|
case PKT_PUBLIC_KEY:
|
|
|
|
|
case PKT_SECRET_KEY:
|
2000-10-06 12:28:44 +00:00
|
|
|
|
if( root ) { /* save this packet */
|
1998-02-13 20:58:50 +00:00
|
|
|
|
kbpos->pkt = pkt;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
kbpos->save_offset = offset;
|
1998-02-13 20:58:50 +00:00
|
|
|
|
pkt = NULL;
|
|
|
|
|
goto ready;
|
|
|
|
|
}
|
1998-03-19 15:27:29 +00:00
|
|
|
|
root = new_kbnode( pkt );
|
2000-10-06 12:28:44 +00:00
|
|
|
|
first_offset = offset;
|
2000-01-24 11:55:49 +00:00
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
1998-03-19 15:27:29 +00:00
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
default:
|
1998-04-14 17:51:16 +00:00
|
|
|
|
/* skip pakets at the beginning of a keyring, until we find
|
1998-03-19 15:27:29 +00:00
|
|
|
|
* a start packet; issue a warning if it is not a comment */
|
1998-05-03 19:35:33 +00:00
|
|
|
|
if( !root && pkt->pkttype != PKT_COMMENT
|
1998-10-12 20:16:38 +00:00
|
|
|
|
&& pkt->pkttype != PKT_OLD_COMMENT ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
1998-03-19 15:27:29 +00:00
|
|
|
|
if( !root || (skipsigs && ( pkt->pkttype == PKT_SIGNATURE
|
1998-05-03 19:35:33 +00:00
|
|
|
|
||pkt->pkttype == PKT_COMMENT
|
|
|
|
|
||pkt->pkttype == PKT_OLD_COMMENT )) ) {
|
1998-03-19 15:27:29 +00:00
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
add_kbnode( root, new_kbnode( pkt ) );
|
2000-01-24 11:55:49 +00:00
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
1998-02-13 20:58:50 +00:00
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ready:
|
|
|
|
|
if( rc == -1 && root )
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
|
|
if( rc )
|
|
|
|
|
release_kbnode( root );
|
2000-10-06 12:28:44 +00:00
|
|
|
|
else {
|
|
|
|
|
if ( root ) {
|
|
|
|
|
kbpos->offset = first_offset;
|
|
|
|
|
kbpos->valid = 1;
|
|
|
|
|
}
|
1998-02-13 20:58:50 +00:00
|
|
|
|
*ret_root = root;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
}
|
1998-02-13 20:58:50 +00:00
|
|
|
|
free_packet( pkt );
|
2000-01-24 11:55:49 +00:00
|
|
|
|
gcry_free( pkt );
|
1998-10-12 20:16:38 +00:00
|
|
|
|
|
1998-02-13 20:58:50 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-12-16 19:15:09 +00:00
|
|
|
|
/****************
|
1998-04-14 17:51:16 +00:00
|
|
|
|
* Perform insert/delete/update operation.
|
1998-02-11 03:25:44 +00:00
|
|
|
|
* mode 1 = insert
|
|
|
|
|
* 2 = delete
|
|
|
|
|
* 3 = update
|
1997-12-16 19:15:09 +00:00
|
|
|
|
*/
|
1997-12-12 12:03:58 +00:00
|
|
|
|
static int
|
2000-10-10 12:58:43 +00:00
|
|
|
|
keyring_copy( KBPOS kbpos, int mode, KBNODE root )
|
1997-12-12 12:03:58 +00:00
|
|
|
|
{
|
1997-12-16 19:15:09 +00:00
|
|
|
|
RESTBL *rentry;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
IOBUF fp, newfp;
|
1998-02-17 20:48:52 +00:00
|
|
|
|
int rc=0;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
char *bakfname = NULL;
|
|
|
|
|
char *tmpfname = NULL;
|
2000-10-06 12:28:44 +00:00
|
|
|
|
#warning We need to lock the keyring while we are editing it.
|
|
|
|
|
/* rethink this whole module */
|
1997-12-16 19:15:09 +00:00
|
|
|
|
|
|
|
|
|
if( !(rentry = check_pos( kbpos )) )
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_GENERAL;
|
1997-12-16 19:15:09 +00:00
|
|
|
|
|
1999-02-25 17:51:55 +00:00
|
|
|
|
if( opt.dry_run )
|
|
|
|
|
return 0;
|
|
|
|
|
|
1999-02-10 16:22:40 +00:00
|
|
|
|
lock_rentry( rentry );
|
1998-11-27 20:40:56 +00:00
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
/* open the source file */
|
2000-10-06 12:28:44 +00:00
|
|
|
|
if( kbpos->fp ) {
|
|
|
|
|
/* BUG(); not allowed with such a handle */
|
|
|
|
|
log_debug("keyring_copy: closing fp %p\n", kbpos->fp );
|
|
|
|
|
iobuf_close (kbpos->fp);
|
|
|
|
|
kbpos->fp = NULL;
|
|
|
|
|
kbpos->valid = 0;
|
|
|
|
|
}
|
1998-12-10 19:20:47 +00:00
|
|
|
|
fp = iobuf_open( rentry->fname );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
if( mode == 1 && !fp && errno == ENOENT ) { /* no file yet */
|
|
|
|
|
KBNODE kbctx, node;
|
1997-12-16 19:15:09 +00:00
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
/* insert: create a new file */
|
|
|
|
|
newfp = iobuf_create( rentry->fname );
|
|
|
|
|
if( !newfp ) {
|
1998-11-10 12:59:59 +00:00
|
|
|
|
log_error(_("%s: can't create: %s\n"), rentry->fname, strerror(errno));
|
1999-02-10 16:22:40 +00:00
|
|
|
|
unlock_rentry( rentry );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_OPEN_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
}
|
1999-04-18 08:18:52 +00:00
|
|
|
|
else if( !opt.quiet )
|
1998-11-10 12:59:59 +00:00
|
|
|
|
log_info(_("%s: keyring created\n"), rentry->fname );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
|
|
kbctx=NULL;
|
|
|
|
|
while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
|
|
|
|
|
if( (rc = build_packet( newfp, node->pkt )) ) {
|
|
|
|
|
log_error("build_packet(%d) failed: %s\n",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
node->pkt->pkttype, gpg_errstr(rc) );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_cancel(newfp);
|
1999-02-10 16:22:40 +00:00
|
|
|
|
unlock_rentry( rentry );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_WRITE_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", rentry->fname, strerror(errno));
|
1999-02-10 16:22:40 +00:00
|
|
|
|
unlock_rentry( rentry );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_CLOSE_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
if( chmod( rentry->fname, S_IRUSR | S_IWUSR ) ) {
|
|
|
|
|
log_error("%s: chmod failed: %s\n",
|
|
|
|
|
rentry->fname, strerror(errno) );
|
1999-02-10 16:22:40 +00:00
|
|
|
|
unlock_rentry( rentry );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
return GPGERR_WRITE_FILE;
|
1997-12-16 19:15:09 +00:00
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
return 0;
|
1997-12-16 19:15:09 +00:00
|
|
|
|
}
|
1997-12-19 11:41:47 +00:00
|
|
|
|
if( !fp ) {
|
1998-02-11 03:25:44 +00:00
|
|
|
|
log_error("%s: can't open: %s\n", rentry->fname, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
/* create the new file */
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#ifdef USE_ONLY_8DOT3
|
1998-10-06 12:10:02 +00:00
|
|
|
|
/* 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(rentry->fname) > 4
|
|
|
|
|
&& !strcmp(rentry->fname+strlen(rentry->fname)-4, ".gpg") ) {
|
2000-01-24 11:55:49 +00:00
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 1 );
|
1998-10-06 12:10:02 +00:00
|
|
|
|
strcpy(bakfname,rentry->fname);
|
|
|
|
|
strcpy(bakfname+strlen(rentry->fname)-4, ".bak");
|
2000-01-24 11:55:49 +00:00
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 1 );
|
1998-10-06 12:10:02 +00:00
|
|
|
|
strcpy(tmpfname,rentry->fname);
|
|
|
|
|
strcpy(tmpfname+strlen(rentry->fname)-4, ".tmp");
|
|
|
|
|
}
|
|
|
|
|
else { /* file does not end with gpg; hmmm */
|
2000-01-24 11:55:49 +00:00
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
1998-10-06 12:10:02 +00:00
|
|
|
|
strcpy(stpcpy(bakfname,rentry->fname),".bak");
|
2000-01-24 11:55:49 +00:00
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
1998-10-06 12:10:02 +00:00
|
|
|
|
strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
|
|
|
|
|
}
|
|
|
|
|
#else
|
2000-01-24 11:55:49 +00:00
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 2 );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
strcpy(stpcpy(bakfname,rentry->fname),"~");
|
2000-01-24 11:55:49 +00:00
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
|
1998-10-06 12:10:02 +00:00
|
|
|
|
#endif
|
1998-02-11 03:25:44 +00:00
|
|
|
|
newfp = iobuf_create( tmpfname );
|
|
|
|
|
if( !newfp ) {
|
|
|
|
|
log_error("%s: can't create: %s\n", tmpfname, strerror(errno) );
|
1997-12-19 11:41:47 +00:00
|
|
|
|
iobuf_close(fp);
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-02-11 03:25:44 +00: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",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
rc = 0;
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
|
|
if( mode == 2 || mode == 3 ) { /* delete or update */
|
|
|
|
|
/* copy first part to the new file */
|
|
|
|
|
rc = copy_some_packets( fp, newfp, kbpos->offset );
|
|
|
|
|
if( rc ) { /* should never get EOF here */
|
|
|
|
|
log_error("%s: copy to %s failed: %s\n",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* skip this keyblock */
|
|
|
|
|
assert( kbpos->count );
|
|
|
|
|
rc = skip_some_packets( fp, kbpos->count );
|
|
|
|
|
if( rc ) {
|
|
|
|
|
log_error("%s: skipping %u packets failed: %s\n",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rentry->fname, kbpos->count, gpg_errstr(rc));
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
|
|
if( mode == 1 || mode == 3 ) { /* insert or update */
|
|
|
|
|
KBNODE kbctx, node;
|
|
|
|
|
|
|
|
|
|
/* append the new data */
|
|
|
|
|
kbctx=NULL;
|
|
|
|
|
while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
|
|
|
|
|
if( (rc = build_packet( newfp, node->pkt )) ) {
|
|
|
|
|
log_error("build_packet(%d) failed: %s\n",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
node->pkt->pkttype, gpg_errstr(rc) );
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_WRITE_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-12-23 12:41:40 +00:00
|
|
|
|
kbpos->valid = 0;
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
|
|
|
|
|
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",
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
1997-12-19 11:41:47 +00:00
|
|
|
|
iobuf_close(fp);
|
1998-02-11 03:25:44 +00:00
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
1997-12-19 11:41:47 +00:00
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
1997-12-19 11:41:47 +00:00
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
/* close both files */
|
1998-02-27 17:51:28 +00:00
|
|
|
|
if( iobuf_close(fp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", rentry->fname, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_CLOSE_FILE;
|
1998-02-27 17:51:28 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_CLOSE_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* if the new file is a secring, restrict the permissions */
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
1998-02-11 03:25:44 +00:00
|
|
|
|
if( rentry->secret ) {
|
|
|
|
|
if( chmod( tmpfname, S_IRUSR | S_IWUSR ) ) {
|
|
|
|
|
log_error("%s: chmod failed: %s\n",
|
|
|
|
|
tmpfname, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_WRITE_FILE;
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-06 12:10:02 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
/* rename and make backup file */
|
1998-08-07 08:53:38 +00:00
|
|
|
|
if( !rentry->secret ) { /* but not for secret keyrings */
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
1998-08-07 08:53:38 +00:00
|
|
|
|
remove( bakfname );
|
|
|
|
|
#endif
|
|
|
|
|
if( rename( rentry->fname, bakfname ) ) {
|
|
|
|
|
log_error("%s: rename to %s failed: %s\n",
|
|
|
|
|
rentry->fname, bakfname, strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_RENAME_FILE;
|
1998-08-07 08:53:38 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
}
|
1999-02-10 16:22:40 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
1998-02-24 18:50:46 +00:00
|
|
|
|
remove( rentry->fname );
|
|
|
|
|
#endif
|
1998-02-11 03:25:44 +00:00
|
|
|
|
if( rename( tmpfname, rentry->fname ) ) {
|
|
|
|
|
log_error("%s: rename to %s failed: %s\n",
|
|
|
|
|
tmpfname, rentry->fname,strerror(errno) );
|
2000-01-27 16:50:45 +00:00
|
|
|
|
rc = GPGERR_RENAME_FILE;
|
1998-08-07 08:53:38 +00:00
|
|
|
|
if( rentry->secret ) {
|
|
|
|
|
log_info(_(
|
1998-11-10 12:59:59 +00:00
|
|
|
|
"WARNING: 2 files with confidential information exists.\n"));
|
1998-08-07 08:53:38 +00:00
|
|
|
|
log_info(_("%s is the unchanged one\n"), rentry->fname );
|
|
|
|
|
log_info(_("%s is the new one\n"), tmpfname );
|
|
|
|
|
log_info(_("Please fix this possible security flaw\n"));
|
|
|
|
|
}
|
1998-02-11 03:25:44 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
1997-12-19 11:41:47 +00:00
|
|
|
|
|
1998-02-11 03:25:44 +00:00
|
|
|
|
leave:
|
1999-02-10 16:22:40 +00:00
|
|
|
|
unlock_rentry( rentry );
|
2000-01-24 11:55:49 +00:00
|
|
|
|
gcry_free(bakfname);
|
|
|
|
|
gcry_free(tmpfname);
|
1998-02-11 03:25:44 +00:00
|
|
|
|
return rc;
|
1997-12-12 12:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-03-13 18:19:12 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
|
********** Functions which operate on KBX files ****************
|
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
|
|
static int
|
2000-10-10 12:58:43 +00:00
|
|
|
|
do_kbxf_enum( KBPOS kbpos, KBNODE *ret_root, int skipsigs )
|
2000-03-13 18:19:12 +00:00
|
|
|
|
{
|
|
|
|
|
PACKET *pkt;
|
|
|
|
|
int rc;
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
KBNODE root = NULL;
|
|
|
|
|
|
|
|
|
|
if( !(rentry=check_pos(kbpos)) )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
|
|
|
|
|
if( kbpos->pkt ) {
|
|
|
|
|
root = new_kbnode( kbpos->pkt );
|
|
|
|
|
kbpos->pkt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
|
|
|
|
init_packet(pkt);
|
2000-10-06 12:28:44 +00:00
|
|
|
|
while( (rc=parse_packet(kbpos->fp, pkt, NULL)) != -1 ) {
|
2000-03-13 18:19:12 +00:00
|
|
|
|
if( rc ) { /* ignore errors */
|
|
|
|
|
if( rc != GPGERR_UNKNOWN_PACKET ) {
|
2000-10-06 12:28:44 +00:00
|
|
|
|
log_error("do_kbxf_enum: read error: %s\n", gpg_errstr(rc) );
|
2000-03-13 18:19:12 +00:00
|
|
|
|
rc = GPGERR_INV_KEYRING;
|
|
|
|
|
goto ready;
|
|
|
|
|
}
|
|
|
|
|
free_packet( pkt );
|
|
|
|
|
init_packet( pkt );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* make a linked list of all packets */
|
|
|
|
|
switch( pkt->pkttype ) {
|
|
|
|
|
case PKT_COMPRESSED:
|
|
|
|
|
log_error("skipped compressed packet in keyring\n" );
|
|
|
|
|
free_packet(pkt);
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PKT_PUBLIC_KEY:
|
|
|
|
|
case PKT_SECRET_KEY:
|
|
|
|
|
if( root ) { /* store this packet */
|
|
|
|
|
kbpos->pkt = pkt;
|
|
|
|
|
pkt = NULL;
|
|
|
|
|
goto ready;
|
|
|
|
|
}
|
|
|
|
|
root = new_kbnode( pkt );
|
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* skip pakets at the beginning of a keyring, until we find
|
|
|
|
|
* a start packet; issue a warning if it is not a comment */
|
|
|
|
|
if( !root && pkt->pkttype != PKT_COMMENT
|
|
|
|
|
&& pkt->pkttype != PKT_OLD_COMMENT ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if( !root || (skipsigs && ( pkt->pkttype == PKT_SIGNATURE
|
|
|
|
|
||pkt->pkttype == PKT_COMMENT
|
|
|
|
|
||pkt->pkttype == PKT_OLD_COMMENT )) ) {
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
add_kbnode( root, new_kbnode( pkt ) );
|
|
|
|
|
pkt = gcry_xmalloc( sizeof *pkt );
|
|
|
|
|
init_packet(pkt);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ready:
|
|
|
|
|
if( rc == -1 && root )
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
|
|
if( rc )
|
|
|
|
|
release_kbnode( root );
|
|
|
|
|
else
|
|
|
|
|
*ret_root = root;
|
|
|
|
|
free_packet( pkt );
|
|
|
|
|
gcry_free( pkt );
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
|
* Perform insert/delete/update operation.
|
|
|
|
|
* mode 1 = insert
|
|
|
|
|
* 2 = delete
|
|
|
|
|
* 3 = update
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2000-10-10 12:58:43 +00:00
|
|
|
|
do_kbxf_copy( KBPOS kbpos, int mode, KBNODE root )
|
2000-03-13 18:19:12 +00:00
|
|
|
|
{
|
|
|
|
|
RESTBL *rentry;
|
|
|
|
|
IOBUF fp, newfp;
|
|
|
|
|
int rc=0;
|
|
|
|
|
char *bakfname = NULL;
|
|
|
|
|
char *tmpfname = NULL;
|
|
|
|
|
|
|
|
|
|
if( !(rentry = check_pos( kbpos )) )
|
|
|
|
|
return GPGERR_GENERAL;
|
|
|
|
|
if( kbpos->fp )
|
|
|
|
|
BUG(); /* not allowed with such a handle */
|
|
|
|
|
|
|
|
|
|
if( opt.dry_run )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
lock_rentry( rentry );
|
|
|
|
|
|
|
|
|
|
/* open the source file */
|
|
|
|
|
fp = iobuf_open( rentry->fname );
|
|
|
|
|
if( mode == 1 && !fp && errno == ENOENT ) { /* no file yet */
|
|
|
|
|
KBNODE kbctx, node;
|
|
|
|
|
|
|
|
|
|
/* insert: create a new file */
|
|
|
|
|
newfp = iobuf_create( rentry->fname );
|
|
|
|
|
if( !newfp ) {
|
|
|
|
|
log_error(_("%s: can't create: %s\n"), rentry->fname, strerror(errno));
|
|
|
|
|
unlock_rentry( rentry );
|
|
|
|
|
return GPGERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
else if( !opt.quiet )
|
|
|
|
|
log_info(_("%s: keyring created\n"), rentry->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, gpg_errstr(rc) );
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
unlock_rentry( rentry );
|
|
|
|
|
return GPGERR_WRITE_FILE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", rentry->fname, strerror(errno));
|
|
|
|
|
unlock_rentry( rentry );
|
|
|
|
|
return GPGERR_CLOSE_FILE;
|
|
|
|
|
}
|
|
|
|
|
if( chmod( rentry->fname, S_IRUSR | S_IWUSR ) ) {
|
|
|
|
|
log_error("%s: chmod failed: %s\n",
|
|
|
|
|
rentry->fname, strerror(errno) );
|
|
|
|
|
unlock_rentry( rentry );
|
|
|
|
|
return GPGERR_WRITE_FILE;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if( !fp ) {
|
|
|
|
|
log_error("%s: can't open: %s\n", rentry->fname, strerror(errno) );
|
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* create the new file */
|
|
|
|
|
#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(rentry->fname) > 4
|
|
|
|
|
&& !strcmp(rentry->fname+strlen(rentry->fname)-4, ".gpg") ) {
|
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 1 );
|
|
|
|
|
strcpy(bakfname,rentry->fname);
|
|
|
|
|
strcpy(bakfname+strlen(rentry->fname)-4, ".bak");
|
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 1 );
|
|
|
|
|
strcpy(tmpfname,rentry->fname);
|
|
|
|
|
strcpy(tmpfname+strlen(rentry->fname)-4, ".tmp");
|
|
|
|
|
}
|
|
|
|
|
else { /* file does not end with gpg; hmmm */
|
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
|
|
|
|
strcpy(stpcpy(bakfname,rentry->fname),".bak");
|
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
|
|
|
|
strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
bakfname = gcry_xmalloc( strlen( rentry->fname ) + 2 );
|
|
|
|
|
strcpy(stpcpy(bakfname,rentry->fname),"~");
|
|
|
|
|
tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 );
|
|
|
|
|
strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
|
|
|
|
|
#endif
|
|
|
|
|
newfp = iobuf_create( tmpfname );
|
|
|
|
|
if( !newfp ) {
|
|
|
|
|
log_error("%s: can't create: %s\n", tmpfname, strerror(errno) );
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
rc = GPGERR_OPEN_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
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, kbpos->offset );
|
|
|
|
|
if( rc ) { /* should never get EOF here */
|
|
|
|
|
log_error("%s: copy to %s failed: %s\n",
|
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* skip this keyblock */
|
|
|
|
|
assert( kbpos->count );
|
|
|
|
|
rc = skip_some_packets( fp, kbpos->count );
|
|
|
|
|
if( rc ) {
|
|
|
|
|
log_error("%s: skipping %u packets failed: %s\n",
|
|
|
|
|
rentry->fname, kbpos->count, gpg_errstr(rc));
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( mode == 1 || mode == 3 ) { /* insert or update */
|
|
|
|
|
KBNODE kbctx, node;
|
|
|
|
|
|
|
|
|
|
/* append the new data */
|
|
|
|
|
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, gpg_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
rc = GPGERR_WRITE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
kbpos->valid = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
rentry->fname, tmpfname, gpg_errstr(rc) );
|
|
|
|
|
iobuf_close(fp);
|
|
|
|
|
iobuf_cancel(newfp);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* close both files */
|
|
|
|
|
if( iobuf_close(fp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", rentry->fname, strerror(errno) );
|
|
|
|
|
rc = GPGERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if( iobuf_close(newfp) ) {
|
|
|
|
|
log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
|
|
|
|
|
rc = GPGERR_CLOSE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* if the new file is a secring, restrict the permissions */
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
if( rentry->secret ) {
|
|
|
|
|
if( chmod( tmpfname, S_IRUSR | S_IWUSR ) ) {
|
|
|
|
|
log_error("%s: chmod failed: %s\n",
|
|
|
|
|
tmpfname, strerror(errno) );
|
|
|
|
|
rc = GPGERR_WRITE_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* rename and make backup file */
|
|
|
|
|
if( !rentry->secret ) { /* but not for secret keyrings */
|
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
remove( bakfname );
|
|
|
|
|
#endif
|
|
|
|
|
if( rename( rentry->fname, bakfname ) ) {
|
|
|
|
|
log_error("%s: rename to %s failed: %s\n",
|
|
|
|
|
rentry->fname, bakfname, strerror(errno) );
|
|
|
|
|
rc = GPGERR_RENAME_FILE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
remove( rentry->fname );
|
|
|
|
|
#endif
|
|
|
|
|
if( rename( tmpfname, rentry->fname ) ) {
|
|
|
|
|
log_error("%s: rename to %s failed: %s\n",
|
|
|
|
|
tmpfname, rentry->fname,strerror(errno) );
|
|
|
|
|
rc = GPGERR_RENAME_FILE;
|
|
|
|
|
if( rentry->secret ) {
|
|
|
|
|
log_info(_(
|
|
|
|
|
"WARNING: 2 files with confidential information exists.\n"));
|
|
|
|
|
log_info(_("%s is the unchanged one\n"), rentry->fname );
|
|
|
|
|
log_info(_("%s is the new one\n"), tmpfname );
|
|
|
|
|
log_info(_("Please fix this possible security flaw\n"));
|
|
|
|
|
}
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
unlock_rentry( rentry );
|
|
|
|
|
gcry_free(bakfname);
|
|
|
|
|
gcry_free(tmpfname);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1998-10-17 14:47:14 +00:00
|
|
|
|
|
|
|
|
|
|