Add server option with-ephemeral-keys.
[gnupg.git] / sm / delete.c
blob00f60282707c5d47a298a17ed15d232a26364405
1 /* delete.c
2 * Copyright (C) 2002 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;
47 rc = keydb_classify_name (username, &desc);
48 if (rc)
50 log_error (_("certificate `%s' not found: %s\n"),
51 username, gpg_strerror (rc));
52 gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "1", NULL);
53 goto leave;
56 kh = keydb_new (0);
57 if (!kh)
59 log_error ("keydb_new failed\n");
60 goto leave;
64 rc = keydb_search (kh, &desc, 1);
65 if (!rc)
66 rc = keydb_get_cert (kh, &cert);
67 if (!rc)
69 unsigned char fpr[20];
71 gpgsm_get_fingerprint (cert, 0, fpr, NULL);
73 next_ambigious:
74 rc = keydb_search (kh, &desc, 1);
75 if (rc == -1)
76 rc = 0;
77 else if (!rc)
79 ksba_cert_t cert2 = NULL;
80 unsigned char fpr2[20];
82 /* We ignore all duplicated certificates which might have
83 been inserted due to program bugs. */
84 if (!keydb_get_cert (kh, &cert2))
86 gpgsm_get_fingerprint (cert2, 0, fpr2, NULL);
87 ksba_cert_release (cert2);
88 if (!memcmp (fpr, fpr2, 20))
90 duplicates++;
91 goto next_ambigious;
94 rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
97 if (rc)
99 if (rc == -1)
100 rc = gpg_error (GPG_ERR_NO_PUBKEY);
101 log_error (_("certificate `%s' not found: %s\n"),
102 username, gpg_strerror (rc));
103 gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "3", NULL);
104 goto leave;
107 /* We need to search again to get back to the right position. */
108 rc = keydb_lock (kh);
109 if (rc)
111 log_error (_("error locking keybox: %s\n"), gpg_strerror (rc));
112 goto leave;
117 keydb_search_reset (kh);
118 rc = keydb_search (kh, &desc, 1);
119 if (rc)
121 log_error ("problem re-searching certificate: %s\n",
122 gpg_strerror (rc));
123 goto leave;
126 rc = keydb_delete (kh, duplicates ? 0 : 1);
127 if (rc)
128 goto leave;
129 if (opt.verbose)
131 if (duplicates)
132 log_info (_("duplicated certificate `%s' deleted\n"), username);
133 else
134 log_info (_("certificate `%s' deleted\n"), username);
137 while (duplicates--);
139 leave:
140 keydb_release (kh);
141 ksba_cert_release (cert);
142 return rc;
147 /* Delete the certificates specified by NAMES. */
149 gpgsm_delete (ctrl_t ctrl, strlist_t names)
151 int rc;
153 if (!names)
155 log_error ("nothing to delete\n");
156 return gpg_error (GPG_ERR_NO_DATA);
159 for (; names; names=names->next )
161 rc = delete_one (ctrl, names->d);
162 if (rc)
164 log_error (_("deleting certificate \"%s\" failed: %s\n"),
165 names->d, gpg_strerror (rc) );
166 return rc;
170 return 0;