1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-05-31 22:18:03 +02:00
gnupg/g10/export.c

207 lines
5.1 KiB
C
Raw Normal View History

1998-01-26 23:09:01 +01:00
/* export.c
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1998-01-26 23:09:01 +01:00
*
* This file is part of GnuPG.
1998-01-26 23:09:01 +01:00
*
* GnuPG is free software; you can redistribute it and/or modify
1998-01-26 23:09:01 +01: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.
*
* GnuPG is distributed in the hope that it will be useful,
1998-01-26 23:09:01 +01: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
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "options.h"
#include "packet.h"
#include "errors.h"
#include "keydb.h"
#include <gcrypt.h>
1998-01-26 23:09:01 +01:00
#include "util.h"
1998-02-13 21:58:50 +01:00
#include "main.h"
1998-11-10 13:59:59 +01:00
#include "i18n.h"
1998-01-26 23:09:01 +01:00
static int do_export( STRLIST users, int secret, int onlyrfc );
static int do_export_stream( IOBUF out, STRLIST users,
int secret, int onlyrfc, int *any );
1998-01-26 23:09:01 +01:00
/****************
1998-04-14 19:51:16 +02:00
* Export the public keys (to standard out or --output).
1998-02-13 21:58:50 +01:00
* Depending on opt.armor the output is armored.
* If onlyrfc is True only RFC24404 compatible keys are exported.
1998-04-14 19:51:16 +02:00
* If USERS is NULL, the complete ring will be exported.
1998-01-26 23:09:01 +01:00
*/
int
export_pubkeys( STRLIST users, int onlyrfc )
1998-06-29 14:30:57 +02:00
{
return do_export( users, 0, onlyrfc );
1998-06-29 14:30:57 +02:00
}
/****************
* Export to an already opened stream; return -1 if no keys have
* been exported
*/
int
export_pubkeys_stream( IOBUF out, STRLIST users, int onlyrfc )
{
int any, rc;
rc = do_export_stream( out, users, 0, onlyrfc, &any );
if( !rc && !any )
rc = -1;
return rc;
}
1998-06-29 14:30:57 +02:00
int
export_seckeys( STRLIST users )
{
return do_export( users, 1, 0 );
1998-06-29 14:30:57 +02:00
}
static int
do_export( STRLIST users, int secret, int onlyrfc )
1998-01-26 23:09:01 +01:00
{
IOBUF out = NULL;
int any, rc;
1998-02-13 21:58:50 +01:00
armor_filter_context_t afx;
memset( &afx, 0, sizeof afx);
rc = open_outfile( NULL, 0, &out );
if( rc )
return rc;
if( opt.armor ) {
afx.what = secret?5:1;
iobuf_push_filter( out, armor_filter, &afx );
}
rc = do_export_stream( out, users, secret, onlyrfc, &any );
if( rc || !any )
iobuf_cancel(out);
else
iobuf_close(out);
return rc;
}
static int
do_export_stream( IOBUF out, STRLIST users, int secret, int onlyrfc, int *any )
{
int rc = 0;
1998-02-13 21:58:50 +01:00
compress_filter_context_t zfx;
PACKET pkt;
KBNODE keyblock = NULL;
KBNODE kbctx, node;
KBPOS kbpos;
STRLIST sl;
int all = !users;
*any = 0;
1998-02-13 21:58:50 +01:00
memset( &zfx, 0, sizeof zfx);
init_packet( &pkt );
1998-05-29 13:53:54 +02:00
if( opt.compress_keys && opt.compress )
1998-02-13 21:58:50 +01:00
iobuf_push_filter( out, compress_filter, &zfx );
if( all ) {
1998-06-29 14:30:57 +02:00
rc = enum_keyblocks( secret?5:0, &kbpos, &keyblock );
1998-02-13 21:58:50 +01:00
if( rc ) {
if( rc != -1 )
log_error("enum_keyblocks(open) failed: %s\n", gpg_errstr(rc) );
1998-02-13 21:58:50 +01:00
goto leave;
}
all = 2;
}
1998-04-14 19:51:16 +02:00
/* use the correct sequence. strlist_last,prev do work correctly with
1998-02-13 21:58:50 +01:00
* NULL pointers :-) */
for( sl=strlist_last(users); sl || all ; sl=strlist_prev( users, sl )) {
if( all ) { /* get the next user */
rc = enum_keyblocks( 1, &kbpos, &keyblock );
if( rc == -1 ) /* EOF */
break;
if( rc ) {
log_error("enum_keyblocks(read) failed: %s\n", gpg_errstr(rc));
1998-02-13 21:58:50 +01:00
break;
}
}
else {
/* search the userid */
1998-06-29 14:30:57 +02:00
rc = secret? find_secret_keyblock_byname( &kbpos, sl->d )
: find_keyblock_byname( &kbpos, sl->d );
1998-02-13 21:58:50 +01:00
if( rc ) {
log_error(_("%s: user not found: %s\n"), sl->d, gpg_errstr(rc));
1998-02-13 21:58:50 +01:00
rc = 0;
continue;
}
/* read the keyblock */
rc = read_keyblock( &kbpos, &keyblock );
}
if( rc ) {
log_error(_("certificate read problem: %s\n"), gpg_errstr(rc));
1998-02-13 21:58:50 +01:00
goto leave;
}
/* do not export keys which are incompatible with rfc2440 */
if( onlyrfc && (node = find_kbnode( keyblock, PKT_PUBLIC_KEY )) ) {
PKT_public_key *pk = node->pkt->pkt.public_key;
if( pk->version == 3 && pk->pubkey_algo > 3 ) {
log_info(_("key %08lX: not a rfc2440 key - skipped\n"),
(ulong)keyid_from_pk( pk, NULL) );
continue;
}
}
1998-02-13 21:58:50 +01:00
/* and write it */
for( kbctx=NULL; (node = walk_kbnode( keyblock, &kbctx, 0 )); ) {
1998-10-16 18:00:17 +02:00
/* don't export any comment packets but those in the
* secret keyring */
if( !secret && node->pkt->pkttype == PKT_COMMENT )
continue;
1998-11-03 20:38:58 +01:00
/* do not export packets which are marked as not exportable */
if( node->pkt->pkttype == PKT_SIGNATURE ) {
const char *p;
p = parse_sig_subpkt2( node->pkt->pkt.signature,
SIGSUBPKT_EXPORTABLE, NULL );
if( p && !*p )
continue; /* not exportable */
}
1998-02-13 21:58:50 +01:00
if( (rc = build_packet( out, node->pkt )) ) {
log_error("build_packet(%d) failed: %s\n",
node->pkt->pkttype, gpg_errstr(rc) );
rc = GPGERR_WRITE_FILE;
1998-02-13 21:58:50 +01:00
goto leave;
}
}
++*any;
1998-02-13 21:58:50 +01:00
}
if( rc == -1 )
rc = 0;
leave:
if( all == 2 )
enum_keyblocks( 2, &kbpos, &keyblock ); /* close */
release_kbnode( keyblock );
if( !*any )
1998-11-10 13:59:59 +01:00
log_info(_("WARNING: nothing exported\n"));
1998-02-13 21:58:50 +01:00
return rc;
1998-01-26 23:09:01 +01:00
}