1 |
twoaday |
2 |
/* delete.c - delete a key |
2 |
|
|
* Copyright (C) 2001 Werner Koch (dd9jn), g10 Code GmbH |
3 |
twoaday |
22 |
* Copyright (C) 2001-2005 Timo Schulz |
4 |
twoaday |
2 |
* |
5 |
|
|
* This file is part of MyGPGME. |
6 |
|
|
* |
7 |
|
|
* MyGPGME is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* MyGPGME is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include <stdio.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <time.h> |
26 |
|
|
#include <assert.h> |
27 |
|
|
|
28 |
|
|
#include "util.h" |
29 |
|
|
#include "context.h" |
30 |
|
|
#include "ops.h" |
31 |
|
|
#include "key.h" |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
enum delete_problem { |
35 |
|
|
No_Such_Key = 1, |
36 |
|
|
Delete_Seckey_First = 2, |
37 |
|
|
Ambigiuous_Specs = 3 |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
static void |
42 |
|
|
delete_status_handler (gpgme_ctx_t ctx, gpg_status_code_t code, char * args) |
43 |
|
|
{ |
44 |
|
|
if (ctx->out_of_core) |
45 |
|
|
return; |
46 |
|
|
|
47 |
|
|
switch (code) { |
48 |
|
|
case STATUS_EOF: |
49 |
|
|
break; |
50 |
|
|
|
51 |
|
|
case STATUS_DELETE_PROBLEM: |
52 |
|
|
ctx->problem = atol (args); |
53 |
|
|
break; |
54 |
|
|
|
55 |
|
|
default: |
56 |
|
|
break; /* ignore all other codes */ |
57 |
|
|
} |
58 |
|
|
} /* delete_status_handler */ |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
static const char * |
62 |
twoaday |
22 |
delete_command_handler (void * opaque, gpg_status_code_t code, const char * key) |
63 |
twoaday |
2 |
{ |
64 |
|
|
gpgme_ctx_t ctx = opaque; |
65 |
|
|
|
66 |
|
|
if( !code ) |
67 |
|
|
return NULL; |
68 |
|
|
|
69 |
twoaday |
22 |
if (code == STATUS_GET_BOOL && !strcmp ("delete_key.secret.okay", key) |
70 |
|
|
|| code == STATUS_GET_BOOL && !strcmp ("delete_key.okay", key)) |
71 |
twoaday |
2 |
return "Y"; |
72 |
|
|
|
73 |
|
|
return NULL; |
74 |
|
|
} /* delete_command_handler */ |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
static gpgme_error_t |
78 |
twoaday |
22 |
delete_start (gpgme_ctx_t ctx, const gpgme_key_t key, |
79 |
|
|
const char *pattern, int allow_secret) |
80 |
twoaday |
2 |
{ |
81 |
|
|
gpgme_error_t rc = 0; |
82 |
twoaday |
22 |
const char *s; |
83 |
twoaday |
2 |
|
84 |
twoaday |
22 |
if (!key && !pattern) { |
85 |
|
|
rc = mk_error (Invalid_Value); |
86 |
twoaday |
2 |
goto leave; |
87 |
|
|
} |
88 |
|
|
|
89 |
twoaday |
22 |
fail_on_pending_request (ctx); |
90 |
twoaday |
2 |
ctx->pending = 1; |
91 |
|
|
|
92 |
twoaday |
22 |
_gpgme_gpg_release (&ctx->gpg); |
93 |
|
|
rc = _gpgme_gpg_new (&ctx->gpg); |
94 |
twoaday |
2 |
if( rc ) |
95 |
|
|
goto leave; |
96 |
|
|
|
97 |
|
|
_gpgme_gpg_set_status_handler ( ctx->gpg, delete_status_handler, ctx ); |
98 |
|
|
_gpgme_gpg_set_command_handler( ctx->gpg, delete_command_handler, ctx ); |
99 |
|
|
if( ctx->use_logging ) |
100 |
|
|
_gpgme_gpg_set_logging_handler( ctx->gpg, ctx ); |
101 |
|
|
|
102 |
|
|
/* build the commandline */ |
103 |
|
|
_gpgme_gpg_add_arg ( ctx->gpg, allow_secret? |
104 |
|
|
"--delete-secret-and-public-key" : "--delete-key" ); |
105 |
|
|
|
106 |
|
|
_gpgme_gpg_add_arg ( ctx->gpg, "--" ); |
107 |
twoaday |
22 |
if (key != NULL) { |
108 |
|
|
s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0); |
109 |
|
|
if(!s) { |
110 |
|
|
rc = mk_error (Invalid_Key); |
111 |
|
|
goto leave; |
112 |
|
|
} |
113 |
twoaday |
2 |
} |
114 |
twoaday |
22 |
else |
115 |
|
|
s = pattern; |
116 |
|
|
_gpgme_gpg_add_arg (ctx->gpg, s); |
117 |
twoaday |
2 |
rc = _gpgme_gpg_spawn( ctx->gpg, ctx ); /* do it */ |
118 |
|
|
|
119 |
|
|
leave: |
120 |
|
|
if( rc ) { |
121 |
|
|
ctx->pending = 0; |
122 |
|
|
_gpgme_gpg_release( &ctx->gpg ); |
123 |
|
|
} |
124 |
|
|
return rc; |
125 |
|
|
} /* delete_start */ |
126 |
|
|
|
127 |
|
|
|
128 |
twoaday |
22 |
static gpgme_error_t |
129 |
|
|
get_delete_result (gpgme_ctx_t ctx) |
130 |
|
|
{ |
131 |
|
|
gpgme_error_t err = 0; |
132 |
|
|
|
133 |
|
|
if (ctx->problem == No_Such_Key) |
134 |
|
|
err = mk_error (Invalid_Key); |
135 |
|
|
else if (ctx->problem == Delete_Seckey_First) |
136 |
|
|
err = mk_error( Conflict ); |
137 |
|
|
else if (gpgme_get_process_rc (ctx)) |
138 |
|
|
err = mk_error (General_Error); |
139 |
|
|
return err; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
twoaday |
2 |
gpgme_error_t |
144 |
twoaday |
22 |
gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret) |
145 |
twoaday |
2 |
{ |
146 |
|
|
gpgme_error_t err; |
147 |
|
|
|
148 |
twoaday |
22 |
err = delete_start (ctx, key, NULL, allow_secret); |
149 |
|
|
if (!err) { |
150 |
|
|
gpgme_wait (ctx, 1); |
151 |
twoaday |
2 |
ctx->pending = 0; |
152 |
twoaday |
22 |
err = get_delete_result (ctx); |
153 |
twoaday |
2 |
} |
154 |
|
|
return err; |
155 |
|
|
} /* gpgme_op_delete */ |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
/* interface */ |
159 |
|
|
gpgme_error_t |
160 |
twoaday |
22 |
gpgme_op_delete_key (const char *keyid, int allow_secret) |
161 |
twoaday |
2 |
{ |
162 |
|
|
gpgme_error_t err = 0; |
163 |
|
|
gpgme_key_t key = NULL; |
164 |
|
|
gpgme_ctx_t ctx; |
165 |
|
|
|
166 |
twoaday |
22 |
if (!keyid) |
167 |
|
|
return mk_error (Invalid_Value); |
168 |
twoaday |
2 |
|
169 |
twoaday |
22 |
err = gpgme_new(&ctx); |
170 |
|
|
if (!err) |
171 |
|
|
key = _gpgme_key_new_fromkeyid (keyid); |
172 |
|
|
if (!err && key) |
173 |
|
|
err = gpgme_op_delete (ctx, key, allow_secret); |
174 |
twoaday |
2 |
|
175 |
twoaday |
22 |
gpgme_release (ctx); |
176 |
|
|
gpgme_key_release (key); |
177 |
twoaday |
2 |
return err; |
178 |
|
|
} /* gpgme_op_key_delete */ |
179 |
|
|
|
180 |
|
|
|
181 |
|
|
gpgme_error_t |
182 |
|
|
gpgme_op_delete_keys (gpgme_recipients_t recp, int allow_secret) |
183 |
|
|
{ |
184 |
|
|
gpgme_error_t err; |
185 |
twoaday |
22 |
gpgme_ctx_t ctx; |
186 |
twoaday |
2 |
struct user_id_s * u; |
187 |
twoaday |
22 |
char *list; |
188 |
|
|
size_t n=0; |
189 |
twoaday |
2 |
|
190 |
|
|
if (!recp) |
191 |
|
|
return mk_error (Invalid_Value); |
192 |
|
|
|
193 |
twoaday |
22 |
for (u=recp->list; u; u=u->next) |
194 |
|
|
n += strlen (u->name) + 1 + 2; |
195 |
|
|
list = calloc (1, n+1); |
196 |
|
|
if (!list) |
197 |
|
|
mk_error (Out_Of_Core); |
198 |
|
|
|
199 |
twoaday |
2 |
for (u = recp->list; u; u = u->next) { |
200 |
twoaday |
22 |
strcat (list, u->name); |
201 |
|
|
strcat (list, " "); |
202 |
twoaday |
2 |
} |
203 |
twoaday |
22 |
|
204 |
|
|
/*DEBUG1 ("delete_keys: '%s'\r\n", list);*/ |
205 |
|
|
err = gpgme_new (&ctx); |
206 |
|
|
if (err) |
207 |
|
|
return err; |
208 |
|
|
err = delete_start (ctx, NULL, list, allow_secret); |
209 |
|
|
if (!err) { |
210 |
|
|
gpgme_wait (ctx, 1); |
211 |
|
|
ctx->pending = 0; |
212 |
|
|
err = get_delete_result (ctx); |
213 |
|
|
} |
214 |
twoaday |
2 |
|
215 |
twoaday |
22 |
gpgme_release (ctx); |
216 |
|
|
safe_free (list); |
217 |
twoaday |
2 |
return err; |
218 |
|
|
} /* gpgme_op_delete_keys */ |