~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to sm/delete.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* delete.c
 
2
 * Copyright (C) 2002 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
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 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
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.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <unistd.h> 
 
27
#include <time.h>
 
28
#include <assert.h>
 
29
 
 
30
#include "gpgsm.h"
 
31
#include <gcrypt.h>
 
32
#include <ksba.h>
 
33
 
 
34
#include "keydb.h"
 
35
#include "i18n.h"
 
36
 
 
37
 
 
38
/* Delete a certificate or an secret key from a key database. */
 
39
static int
 
40
delete_one (CTRL ctrl, const char *username)
 
41
{
 
42
  int rc = 0;
 
43
  KEYDB_SEARCH_DESC desc;
 
44
  KEYDB_HANDLE kh = NULL;
 
45
  ksba_cert_t cert = NULL;
 
46
  int duplicates = 0;
 
47
  
 
48
  rc = keydb_classify_name (username, &desc);
 
49
  if (rc)
 
50
    {
 
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;
 
55
    }
 
56
 
 
57
  kh = keydb_new (0);
 
58
  if (!kh)
 
59
    {
 
60
      log_error ("keydb_new failed\n");
 
61
      goto leave;
 
62
    }
 
63
 
 
64
      
 
65
  rc = keydb_search (kh, &desc, 1);
 
66
  if (!rc)
 
67
    rc = keydb_get_cert (kh, &cert);
 
68
  if (!rc)
 
69
    {
 
70
      char fpr[20];
 
71
 
 
72
      gpgsm_get_fingerprint (cert, 0, fpr, NULL);
 
73
 
 
74
    next_ambigious:
 
75
      rc = keydb_search (kh, &desc, 1);
 
76
      if (rc == -1)
 
77
        rc = 0;
 
78
      else if (!rc)
 
79
        {
 
80
          ksba_cert_t cert2 = NULL;
 
81
          char fpr2[20];
 
82
 
 
83
          /* We ignore all duplicated certificates which might have
 
84
             been inserted due to program bugs. */
 
85
          if (!keydb_get_cert (kh, &cert2))
 
86
            {
 
87
              gpgsm_get_fingerprint (cert2, 0, fpr2, NULL);
 
88
              ksba_cert_release (cert2);
 
89
              if (!memcmp (fpr, fpr2, 20))
 
90
                {
 
91
                  duplicates++;
 
92
                  goto next_ambigious;
 
93
                }
 
94
            }
 
95
          rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
96
        }
 
97
    }
 
98
  if (rc)
 
99
    {
 
100
      if (rc == -1)
 
101
        rc = gpg_error (GPG_ERR_NO_PUBKEY);
 
102
      log_error (_("certificate `%s' not found: %s\n"),
 
103
                 username, gpg_strerror (rc));
 
104
      gpgsm_status2 (ctrl, STATUS_DELETE_PROBLEM, "3", NULL);
 
105
      goto leave;
 
106
    }
 
107
 
 
108
  /* We need to search again to get back to the right position. */
 
109
  rc = keydb_lock (kh);
 
110
  if (rc)
 
111
    {
 
112
      log_error (_("error locking keybox: %s\n"), gpg_strerror (rc));
 
113
      goto leave;
 
114
    }
 
115
                   
 
116
  do 
 
117
    {
 
118
      keydb_search_reset (kh);
 
119
      rc = keydb_search (kh, &desc, 1);
 
120
      if (rc)
 
121
        {
 
122
          log_error ("problem re-searching certificate: %s\n",
 
123
                     gpg_strerror (rc));
 
124
          goto leave;
 
125
        }
 
126
      
 
127
      rc = keydb_delete (kh);
 
128
      if (rc) 
 
129
        goto leave;
 
130
      if (opt.verbose)
 
131
        {
 
132
          if (duplicates)
 
133
            log_info (_("duplicated certificate `%s' deleted\n"), username);
 
134
          else
 
135
            log_info (_("certificate `%s' deleted\n"), username);
 
136
        }
 
137
    }
 
138
  while (duplicates--);
 
139
 
 
140
 leave:
 
141
  keydb_release (kh);
 
142
  ksba_cert_release (cert);
 
143
  return rc;
 
144
}
 
145
 
 
146
 
 
147
 
 
148
/* Delete the certificates specified by NAMES. */
 
149
int
 
150
gpgsm_delete (CTRL ctrl, STRLIST names)
 
151
{
 
152
  int rc;
 
153
 
 
154
  if (!names)
 
155
    {
 
156
      log_error ("nothing to delete\n");
 
157
      return gpg_error (GPG_ERR_NO_DATA);
 
158
    }
 
159
  
 
160
  for (; names; names=names->next )
 
161
    {
 
162
      rc = delete_one (ctrl, names->d);
 
163
      if (rc)
 
164
        {
 
165
          log_error (_("deleting certificate \"%s\" failed: %s\n"),
 
166
                     names->d, gpg_strerror (rc) );
 
167
          return rc;
 
168
        }
 
169
    }
 
170
  
 
171
  return 0;
 
172
}