1 /* delkey.c - delete keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004,
3 * 2005, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
47 * Delete a public or secret key from a keyring.
48 * r_sec_avail will be set if a secret key is available and the public
49 * key can't be deleted for that reason.
52 do_delete_key( const char *username
, int secret
, int force
, int *r_sec_avail
)
55 KBNODE keyblock
= NULL
;
57 KEYDB_HANDLE hd
= keydb_new (secret
);
58 PKT_public_key
*pk
= NULL
;
59 PKT_secret_key
*sk
= NULL
;
63 KEYDB_SEARCH_DESC desc
;
68 /* search the userid */
69 classify_user_id (username
, &desc
);
70 exactmatch
= (desc
.mode
== KEYDB_SEARCH_MODE_FPR
71 || desc
.mode
== KEYDB_SEARCH_MODE_FPR16
72 || desc
.mode
== KEYDB_SEARCH_MODE_FPR20
);
73 rc
= desc
.mode
? keydb_search (hd
, &desc
, 1):G10ERR_INV_USER_ID
;
75 log_error (_("key \"%s\" not found: %s\n"), username
, g10_errstr (rc
));
76 write_status_text( STATUS_DELETE_PROBLEM
, "1" );
80 /* read the keyblock */
81 rc
= keydb_get_keyblock (hd
, &keyblock
);
83 log_error (_("error reading keyblock: %s\n"), g10_errstr(rc
) );
87 /* get the keyid from the keyblock */
88 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
:PKT_PUBLIC_KEY
);
90 log_error("Oops; key not found anymore!\n");
97 sk
= node
->pkt
->pkt
.secret_key
;
98 keyid_from_sk( sk
, keyid
);
103 pk
= node
->pkt
->pkt
.public_key
;
104 keyid_from_pk( pk
, keyid
);
108 rc
= seckey_available( keyid
);
115 else if( rc
!= G10ERR_NO_SECKEY
)
116 log_error("%s: get secret key: %s\n", username
, g10_errstr(rc
) );
124 else if (opt
.batch
&& exactmatch
)
126 else if( opt
.batch
&& secret
)
128 log_error(_("can't do this in batch mode\n"));
129 log_info (_("(unless you specify the key by fingerprint)\n"));
131 else if( opt
.batch
&& opt
.answer_yes
)
135 log_error(_("can't do this in batch mode without \"--yes\"\n"));
136 log_info (_("(unless you specify the key by fingerprint)\n"));
140 print_seckey_info( sk
);
142 print_pubkey_info(NULL
, pk
);
145 yes
= cpr_get_answer_is_yes( secret
? "delete_key.secret.okay"
147 _("Delete this key from the keyring? (y/N) "));
148 if( !cpr_enabled() && secret
&& yes
) {
149 /* I think it is not required to check a passphrase; if
150 * the user is so stupid as to let others access his secret keyring
151 * (and has no backup) - it is up him to read some very
152 * basic texts about security.
154 yes
= cpr_get_answer_is_yes("delete_key.secret.okay",
155 _("This is a secret key! - really delete? (y/N) "));
163 rc
= keydb_delete_keyblock (hd
);
165 log_error (_("deleting keyblock failed: %s\n"), g10_errstr(rc
) );
169 /* Note that the ownertrust being cleared will trigger a
170 revalidation_mark(). This makes sense - only deleting keys
171 that have ownertrust set should trigger this. */
173 if (!secret
&& pk
&& clear_ownertrusts (pk
)) {
175 log_info (_("ownertrust information cleared\n"));
181 release_kbnode (keyblock
);
186 * Delete a public or secret key from a keyring.
189 delete_keys( STRLIST names
, int secret
, int allow_both
)
191 int rc
, avail
, force
=(!allow_both
&& !secret
&& opt
.expert
);
193 /* Force allows us to delete a public key even if a secret key
196 for(;names
;names
=names
->next
) {
197 rc
= do_delete_key (names
->d
, secret
, force
, &avail
);
200 rc
= do_delete_key (names
->d
, 1, 0, &avail
);
202 rc
= do_delete_key (names
->d
, 0, 0, &avail
);
206 "there is a secret key for public key \"%s\"!\n"),names
->d
);
208 "use option \"--delete-secret-keys\" to delete it first.\n"));
209 write_status_text( STATUS_DELETE_PROBLEM
, "2" );
215 log_error("%s: delete key failed: %s\n", names
->d
, g10_errstr(rc
) );