1 /* delete.c - Delete certificates from the keybox.
2 * Copyright (C) 2002, 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
37 /* Delete a certificate or an secret key from a key database. */
39 delete_one (ctrl_t ctrl, const char *username)
42 KEYDB_SEARCH_DESC desc;
43 KEYDB_HANDLE kh = NULL;
44 ksba_cert_t cert = NULL;
48 rc = classify_user_id (username, &desc, 0);
51 log_error (_("certificate '%s' not found: %s\n"),
52 username, gpg_strerror (rc));
53 gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "1", NULL);
60 log_error ("keydb_new failed\n");
64 /* If the key is specified in a unique way, include ephemeral keys
66 if ( desc.mode == KEYDB_SEARCH_MODE_FPR
67 || desc.mode == KEYDB_SEARCH_MODE_FPR20
68 || desc.mode == KEYDB_SEARCH_MODE_FPR16
69 || desc.mode == KEYDB_SEARCH_MODE_KEYGRIP )
72 keydb_set_ephemeral (kh, 1);
75 rc = keydb_search (ctrl, kh, &desc, 1);
77 rc = keydb_get_cert (kh, &cert);
80 unsigned char fpr[20];
82 gpgsm_get_fingerprint (cert, 0, fpr, NULL);
85 rc = keydb_search (ctrl, kh, &desc, 1);
90 ksba_cert_t cert2 = NULL;
91 unsigned char fpr2[20];
93 /* We ignore all duplicated certificates which might have
94 been inserted due to program bugs. */
95 if (!keydb_get_cert (kh, &cert2))
97 gpgsm_get_fingerprint (cert2, 0, fpr2, NULL);
98 ksba_cert_release (cert2);
99 if (!memcmp (fpr, fpr2, 20))
105 rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
111 rc = gpg_error (GPG_ERR_NO_PUBKEY);
112 log_error (_("certificate '%s' not found: %s\n"),
113 username, gpg_strerror (rc));
114 gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "3", NULL);
118 /* We need to search again to get back to the right position. */
119 rc = keydb_lock (kh);
122 log_error (_("error locking keybox: %s\n"), gpg_strerror (rc));
128 keydb_search_reset (kh);
129 rc = keydb_search (ctrl, kh, &desc, 1);
132 log_error ("problem re-searching certificate: %s\n",
137 rc = keydb_delete (kh, duplicates ? 0 : 1);
143 log_info (_("duplicated certificate '%s' deleted\n"), username);
145 log_info (_("certificate '%s' deleted\n"), username);
148 while (duplicates--);
152 ksba_cert_release (cert);
158 /* Delete the certificates specified by NAMES. */
160 gpgsm_delete (ctrl_t ctrl, strlist_t names)
166 log_error ("nothing to delete\n");
167 return gpg_error (GPG_ERR_NO_DATA);
170 for (; names; names=names->next )
172 rc = delete_one (ctrl, names->d);
175 log_error (_("deleting certificate \"%s\" failed: %s\n"),
176 names->d, gpg_strerror (rc) );