* gpg.texi (OpenPGP Options): Clarify that personal-foo-preferences
[gnupg.git] / sm / delete.c
blobfd49ebeab2ed1ea6cc1251abceff98957eab6150
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 <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <assert.h>
29 #include "gpgsm.h"
30 #include <gcrypt.h>
31 #include <ksba.h>
33 #include "keydb.h"
34 #include "i18n.h"
37 /* Delete a certificate or an secret key from a key database. */
38 static int
39 delete_one (ctrl_t ctrl, const char *username)
41 int rc = 0;
42 KEYDB_SEARCH_DESC desc;
43 KEYDB_HANDLE kh = NULL;
44 ksba_cert_t cert = NULL;
45 int duplicates = 0;
46 int is_ephem = 0;
48 rc = keydb_classify_name (username, &desc);
49 if (rc)
51 log_error (_("certificate `%s' not found: %s\n"),
52 username, gpg_strerror (rc));
53 gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "1", NULL);
54 goto leave;
57 kh = keydb_new (0);
58 if (!kh)
60 log_error ("keydb_new failed\n");
61 goto leave;
64 /* If the key is specified in a unique way, include ephemeral keys
65 in the search. */
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 )
71 is_ephem = 1;
72 keydb_set_ephemeral (kh, 1);
75 rc = keydb_search (kh, &desc, 1);
76 if (!rc)
77 rc = keydb_get_cert (kh, &cert);
78 if (!rc && !is_ephem)
80 unsigned char fpr[20];
82 gpgsm_get_fingerprint (cert, 0, fpr, NULL);
84 next_ambigious:
85 rc = keydb_search (kh, &desc, 1);
86 if (rc == -1)
87 rc = 0;
88 else if (!rc)
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))
101 duplicates++;
102 goto next_ambigious;
105 rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
108 if (rc)
110 if (rc == -1)
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);
115 goto leave;
118 /* We need to search again to get back to the right position. */
119 rc = keydb_lock (kh);
120 if (rc)
122 log_error (_("error locking keybox: %s\n"), gpg_strerror (rc));
123 goto leave;
128 keydb_search_reset (kh);
129 rc = keydb_search (kh, &desc, 1);
130 if (rc)
132 log_error ("problem re-searching certificate: %s\n",
133 gpg_strerror (rc));
134 goto leave;
137 rc = keydb_delete (kh, duplicates ? 0 : 1);
138 if (rc)
139 goto leave;
140 if (opt.verbose)
142 if (duplicates)
143 log_info (_("duplicated certificate `%s' deleted\n"), username);
144 else
145 log_info (_("certificate `%s' deleted\n"), username);
148 while (duplicates--);
150 leave:
151 keydb_release (kh);
152 ksba_cert_release (cert);
153 return rc;
158 /* Delete the certificates specified by NAMES. */
160 gpgsm_delete (ctrl_t ctrl, strlist_t names)
162 int rc;
164 if (!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);
173 if (rc)
175 log_error (_("deleting certificate \"%s\" failed: %s\n"),
176 names->d, gpg_strerror (rc) );
177 return rc;
181 return 0;