2009-03-23 17:17:49 +01:00
|
|
|
/* delete.c - Delete certificates from the keybox.
|
|
|
|
* Copyright (C) 2002, 2009 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2011-02-04 12:57:53 +01:00
|
|
|
#include <unistd.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
#include "keydb.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Delete a certificate or an secret key from a key database. */
|
|
|
|
static int
|
2006-09-06 18:35:52 +02:00
|
|
|
delete_one (ctrl_t ctrl, const char *username)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
|
|
|
KEYDB_HANDLE kh = NULL;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_cert_t cert = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
int duplicates = 0;
|
2009-03-23 17:17:49 +01:00
|
|
|
int is_ephem = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2011-04-25 23:56:47 +02:00
|
|
|
rc = classify_user_id (username, &desc, 0);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (rc)
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("certificate '%s' not found: %s\n"),
|
2003-08-05 19:11:04 +02:00
|
|
|
username, gpg_strerror (rc));
|
|
|
|
gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "1", NULL);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2020-09-02 17:27:30 +02:00
|
|
|
kh = keydb_new (ctrl);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!kh)
|
|
|
|
{
|
|
|
|
log_error ("keydb_new failed\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2009-03-23 17:17:49 +01:00
|
|
|
/* If the key is specified in a unique way, include ephemeral keys
|
|
|
|
in the search. */
|
|
|
|
if ( desc.mode == KEYDB_SEARCH_MODE_FPR
|
|
|
|
|| desc.mode == KEYDB_SEARCH_MODE_KEYGRIP )
|
|
|
|
{
|
|
|
|
is_ephem = 1;
|
|
|
|
keydb_set_ephemeral (kh, 1);
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:01:19 +01:00
|
|
|
rc = keydb_search (ctrl, kh, &desc, 1);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!rc)
|
|
|
|
rc = keydb_get_cert (kh, &cert);
|
2009-03-23 17:17:49 +01:00
|
|
|
if (!rc && !is_ephem)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2005-06-16 10:12:03 +02:00
|
|
|
unsigned char fpr[20];
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
gpgsm_get_fingerprint (cert, 0, fpr, NULL);
|
|
|
|
|
|
|
|
next_ambigious:
|
2016-11-10 17:01:19 +01:00
|
|
|
rc = keydb_search (ctrl, kh, &desc, 1);
|
2020-09-10 13:05:17 +02:00
|
|
|
if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
|
2003-08-05 19:11:04 +02:00
|
|
|
rc = 0;
|
|
|
|
else if (!rc)
|
|
|
|
{
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_cert_t cert2 = NULL;
|
2005-06-16 10:12:03 +02:00
|
|
|
unsigned char fpr2[20];
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
/* We ignore all duplicated certificates which might have
|
|
|
|
been inserted due to program bugs. */
|
|
|
|
if (!keydb_get_cert (kh, &cert2))
|
|
|
|
{
|
|
|
|
gpgsm_get_fingerprint (cert2, 0, fpr2, NULL);
|
|
|
|
ksba_cert_release (cert2);
|
|
|
|
if (!memcmp (fpr, fpr2, 20))
|
|
|
|
{
|
|
|
|
duplicates++;
|
|
|
|
goto next_ambigious;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
{
|
2020-09-10 13:05:17 +02:00
|
|
|
if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND)
|
2003-08-05 19:11:04 +02:00
|
|
|
rc = gpg_error (GPG_ERR_NO_PUBKEY);
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("certificate '%s' not found: %s\n"),
|
2003-08-05 19:11:04 +02:00
|
|
|
username, gpg_strerror (rc));
|
|
|
|
gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "3", NULL);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:48:53 +02:00
|
|
|
/* We need to search again to get back to the right position. Note
|
2019-05-14 13:36:08 +02:00
|
|
|
* that the lock is kept until the KH is released. */
|
2004-02-02 18:09:35 +01:00
|
|
|
rc = keydb_lock (kh);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error (_("error locking keybox: %s\n"), gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
|
|
do
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
keydb_search_reset (kh);
|
2016-11-10 17:01:19 +01:00
|
|
|
rc = keydb_search (ctrl, kh, &desc, 1);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("problem re-searching certificate: %s\n",
|
|
|
|
gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2019-05-14 13:36:08 +02:00
|
|
|
rc = keydb_delete (kh);
|
2011-02-04 12:57:53 +01:00
|
|
|
if (rc)
|
2003-08-05 19:11:04 +02:00
|
|
|
goto leave;
|
|
|
|
if (opt.verbose)
|
|
|
|
{
|
|
|
|
if (duplicates)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info (_("duplicated certificate '%s' deleted\n"), username);
|
2003-08-05 19:11:04 +02:00
|
|
|
else
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info (_("certificate '%s' deleted\n"), username);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (duplicates--);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
keydb_release (kh);
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Delete the certificates specified by NAMES. */
|
|
|
|
int
|
2006-10-02 13:54:35 +02:00
|
|
|
gpgsm_delete (ctrl_t ctrl, strlist_t names)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!names)
|
|
|
|
{
|
|
|
|
log_error ("nothing to delete\n");
|
|
|
|
return gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
for (; names; names=names->next )
|
|
|
|
{
|
|
|
|
rc = delete_one (ctrl, names->d);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error (_("deleting certificate \"%s\" failed: %s\n"),
|
|
|
|
names->d, gpg_strerror (rc) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|