From 4ae64383cd288f223fec810328f305b46046b2b6 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 23 Jan 2001 13:56:30 +0000 Subject: [PATCH] Enhanced delkey command and other stuff for gpgme --- doc/gpg.sgml | 6 ++++++ g10/ChangeLog | 6 ++++++ g10/delkey.c | 41 +++++++++++++++++++++++++++++++++-------- g10/g10.c | 16 +++++++++++++++- g10/main.h | 2 +- 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/doc/gpg.sgml b/doc/gpg.sgml index 8cbd326ca..48647fcd3 100644 --- a/doc/gpg.sgml +++ b/doc/gpg.sgml @@ -413,6 +413,12 @@ Remove key from the public keyring Remove key from the secret and public keyring + +--delete-secret-and-public-key &ParmName; + +Same as --delete-key, but if a secret key exists, it will be removed first. + + --gen-revoke diff --git a/g10/ChangeLog b/g10/ChangeLog index a060710bc..a92083747 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,9 @@ +2001-01-22 Werner Koch + + * g10.c: New command --delete-secret-and-public-key. + * delkey.c (delete_key): Add new arg allow_both. + (do_delete_key): Move most stuff from above to this new function. + 2001-01-12 Werner Koch * passphrase.c (passphrase_to_dek): Use MD5 when IDEA is installed diff --git a/g10/delkey.c b/g10/delkey.c index 31d7caaa4..a5560473e 100644 --- a/g10/delkey.c +++ b/g10/delkey.c @@ -43,9 +43,11 @@ /**************** * Delete a public or secret key from a keyring. + * r_sec_avail will be set if a secret key is available and the public + * key can't be deleted for that reason. */ -int -delete_key( const char *username, int secret ) +static int +do_delete_key( const char *username, int secret, int *r_sec_avail ) { int rc = 0; KBNODE keyblock = NULL; @@ -57,6 +59,7 @@ delete_key( const char *username, int secret ) int okay=0; int yes; + *r_sec_avail = 0; /* search the userid */ rc = secret? find_secret_keyblock_byname( &kbpos, username ) : find_keyblock_byname( &kbpos, username ); @@ -90,12 +93,8 @@ delete_key( const char *username, int secret ) keyid_from_pk( pk, keyid ); rc = seckey_available( keyid ); if( !rc ) { - log_error(_( - "there is a secret key for this public key!\n")); - log_info(_( - "use option \"--delete-secret-key\" to delete it first.\n")); - write_status_text( STATUS_DELETE_PROBLEM, "2" ); - rc = -1; + *r_sec_avail = 1; + rc = -1; } else if( rc != G10ERR_NO_SECKEY ) { log_error("%s: get secret key: %s\n", username, g10_errstr(rc) ); @@ -161,3 +160,29 @@ delete_key( const char *username, int secret ) return rc; } +/**************** + * Delete a public or secret key from a keyring. + */ +int +delete_key( const char *username, int secret, int allow_both ) +{ + int rc, avail; + + rc = do_delete_key (username, secret, &avail ); + if ( rc && avail ) { + assert (secret); + if ( allow_both ) { + rc = do_delete_key (username, 1, &avail ); + if ( !rc ) + rc = do_delete_key (username, 0, &avail ); + } + else { + log_error(_( + "there is a secret key for this public key!\n")); + log_info(_( + "use option \"--delete-secret-key\" to delete it first.\n")); + write_status_text( STATUS_DELETE_PROBLEM, "2" ); + } + } + return rc; +} diff --git a/g10/g10.c b/g10/g10.c index a45f4f584..bebaaf5d1 100644 --- a/g10/g10.c +++ b/g10/g10.c @@ -76,6 +76,7 @@ enum cmd_and_opt_values { aNull = 0, aEditKey, aDeleteKey, aDeleteSecretKey, + aDeleteSecretAndPublicKey, aKMode, aKModeC, aImport, @@ -395,6 +396,7 @@ static ARGPARSE_OPTS opts[] = { { oTryAllSecrets, "try-all-secrets", 0, "@" }, { oEnableSpecialFilenames, "enable-special-filenames", 0, "@" }, { oNoExpensiveTrustChecks, "no-expensive-trust-checks", 0, "@" }, + { aDeleteSecretAndPublicKey, "delete-secret-and-public-key",256, "@" }, { oEmu3DESS2KBug, "emulate-3des-s2k-bug", 0, "@"}, { oEmuMDEncodeBug, "emulate-md-encode-bug", 0, "@"}, {0} }; @@ -754,6 +756,10 @@ main( int argc, char **argv ) case aExportSecretSub: set_cmd( &cmd, aExportSecretSub); break; case aDeleteSecretKey: set_cmd( &cmd, aDeleteSecretKey); greeting=1; break; + case aDeleteSecretAndPublicKey: + set_cmd( &cmd, aDeleteSecretAndPublicKey); + greeting=1; + break; case aDeleteKey: set_cmd( &cmd, aDeleteKey); greeting=1; break; case aDetachedSign: detached_sig = 1; set_cmd( &cmd, aSign ); break; @@ -1280,10 +1286,18 @@ main( int argc, char **argv ) if( argc != 1 ) wrong_args(_("--delete-key user-id")); username = make_username( fname ); - if( (rc = delete_key(username, cmd==aDeleteSecretKey)) ) + if( (rc = delete_key(username, cmd==aDeleteSecretKey, 0)) ) log_error("%s: delete key failed: %s\n", username, g10_errstr(rc) ); m_free(username); break; + case aDeleteSecretAndPublicKey: + if( argc != 1 ) + wrong_args(_("--delete-secret-and-public-key user-id")); + username = make_username( fname ); + if( (rc = delete_key(username, 0, 1)) ) + log_error("%s: delete key failed: %s\n", username, g10_errstr(rc)); + m_free(username); + break; case aCheckKeys: diff --git a/g10/main.h b/g10/main.h index f6657a2c2..150c42805 100644 --- a/g10/main.h +++ b/g10/main.h @@ -85,7 +85,7 @@ int check_key_signature2( KBNODE root, KBNODE node, int *is_selfsig, u32 *r_expiredate, int *r_expired ); /*-- delkey.c --*/ -int delete_key( const char *username, int secure ); +int delete_key( const char *username, int secret, int allow_both ); /*-- keyedit.c --*/ void keyedit_menu( const char *username, STRLIST locusr, STRLIST cmds,