1 |
twoaday |
2 |
/* delete.c - delete a key |
2 |
|
|
* Copyright (C) 2001 Werner Koch (dd9jn), g10 Code GmbH |
3 |
|
|
* Copyright (C) 2001-2004 Timo Schulz |
4 |
|
|
* |
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 |
|
|
delete_command_handler( void * opaque, gpg_status_code_t code, const char * key ) |
63 |
|
|
{ |
64 |
|
|
gpgme_ctx_t ctx = opaque; |
65 |
|
|
|
66 |
|
|
if( !code ) |
67 |
|
|
return NULL; |
68 |
|
|
|
69 |
|
|
if( code == STATUS_GET_BOOL && !strcmp( "delete_key.secret.okay", key ) |
70 |
|
|
|| code == STATUS_GET_BOOL && !strcmp( "delete_key.okay", key ) ) |
71 |
|
|
return "Y"; |
72 |
|
|
|
73 |
|
|
return NULL; |
74 |
|
|
} /* delete_command_handler */ |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
static gpgme_error_t |
78 |
|
|
delete_start( gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret ) |
79 |
|
|
{ |
80 |
|
|
gpgme_error_t rc = 0; |
81 |
|
|
const char * s; |
82 |
|
|
|
83 |
|
|
if( !key ) { |
84 |
|
|
rc = mk_error( Invalid_Value ); |
85 |
|
|
goto leave; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
fail_on_pending_request( ctx ); |
89 |
|
|
ctx->pending = 1; |
90 |
|
|
|
91 |
|
|
_gpgme_gpg_release( &ctx->gpg ); |
92 |
|
|
rc = _gpgme_gpg_new( &ctx->gpg ); |
93 |
|
|
if( rc ) |
94 |
|
|
goto leave; |
95 |
|
|
|
96 |
|
|
_gpgme_gpg_set_status_handler ( ctx->gpg, delete_status_handler, ctx ); |
97 |
|
|
_gpgme_gpg_set_command_handler( ctx->gpg, delete_command_handler, ctx ); |
98 |
|
|
if( ctx->use_logging ) |
99 |
|
|
_gpgme_gpg_set_logging_handler( ctx->gpg, ctx ); |
100 |
|
|
|
101 |
|
|
/* build the commandline */ |
102 |
|
|
_gpgme_gpg_add_arg ( ctx->gpg, allow_secret? |
103 |
|
|
"--delete-secret-and-public-key" : "--delete-key" ); |
104 |
|
|
|
105 |
|
|
_gpgme_gpg_add_arg ( ctx->gpg, "--" ); |
106 |
|
|
s = gpgme_key_get_string_attr ( key, GPGME_ATTR_KEYID, NULL, 0 ); |
107 |
|
|
if( !s ) { |
108 |
|
|
rc = mk_error( Invalid_Key ); |
109 |
|
|
goto leave; |
110 |
|
|
} |
111 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, s ); |
112 |
|
|
rc = _gpgme_gpg_spawn( ctx->gpg, ctx ); /* do it */ |
113 |
|
|
|
114 |
|
|
leave: |
115 |
|
|
if( rc ) { |
116 |
|
|
ctx->pending = 0; |
117 |
|
|
_gpgme_gpg_release( &ctx->gpg ); |
118 |
|
|
} |
119 |
|
|
return rc; |
120 |
|
|
} /* delete_start */ |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
gpgme_error_t |
124 |
|
|
gpgme_op_delete( gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret ) |
125 |
|
|
{ |
126 |
|
|
gpgme_error_t err; |
127 |
|
|
|
128 |
|
|
err = delete_start( ctx, key, allow_secret ); |
129 |
|
|
if( !err ) { |
130 |
|
|
gpgme_wait( ctx, 1 ); |
131 |
|
|
ctx->pending = 0; |
132 |
|
|
if( ctx->problem == No_Such_Key ) |
133 |
|
|
err = mk_error( Invalid_Key ); |
134 |
|
|
else if( ctx->problem == Delete_Seckey_First ) |
135 |
|
|
err = mk_error( Conflict ); |
136 |
|
|
else if( gpgme_get_process_rc( ctx ) ) |
137 |
|
|
err = mk_error( General_Error ); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
return err; |
141 |
|
|
} /* gpgme_op_delete */ |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/* interface */ |
145 |
|
|
gpgme_error_t |
146 |
|
|
gpgme_op_delete_key( const char * keyid, int allow_secret ) |
147 |
|
|
{ |
148 |
|
|
gpgme_error_t err = 0; |
149 |
|
|
gpgme_key_t key = NULL; |
150 |
|
|
gpgme_ctx_t ctx; |
151 |
|
|
|
152 |
|
|
if( !keyid ) |
153 |
|
|
return mk_error( Invalid_Value ); |
154 |
|
|
|
155 |
|
|
err = gpgme_new( &ctx ); |
156 |
|
|
if( !err ) |
157 |
|
|
key = _gpgme_key_new_fromkeyid( keyid ); |
158 |
|
|
if( !err && key ) |
159 |
|
|
err = gpgme_op_delete( ctx, key, allow_secret ); |
160 |
|
|
|
161 |
|
|
gpgme_release( ctx ); |
162 |
|
|
gpgme_key_release( key ); |
163 |
|
|
return err; |
164 |
|
|
} /* gpgme_op_key_delete */ |
165 |
|
|
|
166 |
|
|
|
167 |
|
|
gpgme_error_t |
168 |
|
|
gpgme_op_delete_keys (gpgme_recipients_t recp, int allow_secret) |
169 |
|
|
{ |
170 |
|
|
gpgme_error_t err; |
171 |
|
|
struct user_id_s * u; |
172 |
|
|
|
173 |
|
|
if (!recp) |
174 |
|
|
return mk_error (Invalid_Value); |
175 |
|
|
|
176 |
|
|
for (u = recp->list; u; u = u->next) { |
177 |
|
|
err = gpgme_op_delete_key (u->name, allow_secret); |
178 |
|
|
if (err) |
179 |
|
|
break; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
return err; |
183 |
|
|
} /* gpgme_op_delete_keys */ |