Line data Source code
1 : /* delete.c - Delete a key.
2 : Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
6 : GPGME is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : GPGME is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 : #include <stdlib.h>
25 : #include <errno.h>
26 :
27 : #include "debug.h"
28 : #include "gpgme.h"
29 : #include "context.h"
30 : #include "ops.h"
31 :
32 :
33 : static gpgme_error_t
34 0 : delete_status_handler (void *priv, gpgme_status_code_t code, char *args)
35 : {
36 : (void)priv;
37 :
38 0 : if (code == GPGME_STATUS_DELETE_PROBLEM)
39 : {
40 : enum delete_problem
41 : {
42 : DELETE_No_Problem = 0,
43 : DELETE_No_Such_Key = 1,
44 : DELETE_Must_Delete_Secret_Key = 2,
45 : DELETE_Ambiguous_Specification = 3
46 : };
47 : long problem;
48 : char *tail;
49 :
50 0 : gpg_err_set_errno (0);
51 0 : problem = strtol (args, &tail, 0);
52 0 : if (errno || (*tail && *tail != ' '))
53 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
54 :
55 0 : switch (problem)
56 : {
57 : case DELETE_No_Problem:
58 0 : break;
59 :
60 : case DELETE_No_Such_Key:
61 0 : return gpg_error (GPG_ERR_NO_PUBKEY);
62 :
63 : case DELETE_Must_Delete_Secret_Key:
64 0 : return gpg_error (GPG_ERR_CONFLICT);
65 :
66 : case DELETE_Ambiguous_Specification:
67 0 : return gpg_error (GPG_ERR_AMBIGUOUS_NAME);
68 :
69 : }
70 :
71 0 : return gpg_error (GPG_ERR_GENERAL);
72 : }
73 0 : else if (code == GPGME_STATUS_ERROR)
74 : {
75 : /* Some error stati are informational, so we don't return an
76 : error code if we are not ready to process this status. */
77 : gpgme_error_t err;
78 0 : char *where = strchr (args, ' ');
79 : char *which;
80 :
81 0 : if (where)
82 : {
83 0 : *where = '\0';
84 0 : which = where + 1;
85 :
86 0 : where = strchr (which, ' ');
87 0 : if (where)
88 0 : *where = '\0';
89 :
90 0 : where = args;
91 : }
92 : else
93 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
94 :
95 0 : err = atoi (which);
96 :
97 0 : if (!strcmp (where, "delete_key.secret")
98 0 : && (gpg_err_code (err) == GPG_ERR_CANCELED
99 0 : || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED))
100 : {
101 : /* This indicates a user cancellation on the confirmation dialog. */
102 0 : return gpg_error (gpg_err_code (err));
103 : }
104 : }
105 0 : return 0;
106 : }
107 :
108 :
109 : static gpgme_error_t
110 0 : delete_start (gpgme_ctx_t ctx, int synchronous, const gpgme_key_t key,
111 : int allow_secret)
112 : {
113 : gpgme_error_t err;
114 :
115 0 : err = _gpgme_op_reset (ctx, synchronous);
116 0 : if (err)
117 0 : return err;
118 :
119 0 : _gpgme_engine_set_status_handler (ctx->engine, delete_status_handler, ctx);
120 :
121 0 : return _gpgme_engine_op_delete (ctx->engine, key, allow_secret);
122 : }
123 :
124 :
125 : /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret
126 : keys are also deleted. */
127 : gpgme_error_t
128 0 : gpgme_op_delete_start (gpgme_ctx_t ctx, const gpgme_key_t key,
129 : int allow_secret)
130 : {
131 : gpgme_error_t err;
132 :
133 0 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_delete", ctx,
134 : "key=%p (%s), allow_secret=%i", key,
135 : (key->subkeys && key->subkeys->fpr) ?
136 : key->subkeys->fpr : "invalid", allow_secret);
137 :
138 0 : if (!ctx)
139 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
140 :
141 0 : err = delete_start (ctx, 0, key, allow_secret);
142 0 : return TRACE_ERR (err);
143 : }
144 :
145 :
146 : /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret
147 : keys are also deleted. */
148 : gpgme_error_t
149 0 : gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret)
150 : {
151 : gpgme_error_t err;
152 :
153 0 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_delete", ctx,
154 : "key=%p (%s), allow_secret=%i", key,
155 : (key->subkeys && key->subkeys->fpr) ?
156 : key->subkeys->fpr : "invalid", allow_secret);
157 :
158 0 : if (!ctx)
159 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
160 :
161 0 : err = delete_start (ctx, 1, key, allow_secret);
162 0 : if (!err)
163 0 : err = _gpgme_wait_one (ctx);
164 0 : return err;
165 : }
|