1 |
twoaday |
23 |
/* wptKeyEditCB.cpp - Key edit callback handling |
2 |
|
|
* Copyright (C) 2005 Timo Schulz |
3 |
twoaday |
24 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
twoaday |
23 |
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT 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 GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
#include <stdio.h> |
22 |
|
|
#include <string.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <assert.h> |
25 |
|
|
#include <windows.h> |
26 |
|
|
|
27 |
twoaday |
25 |
#include "gpgme.h" |
28 |
twoaday |
23 |
#include "wptCommonCtl.h" |
29 |
|
|
#include "wptContext.h" |
30 |
|
|
#include "wptKeyEdit.h" |
31 |
twoaday |
24 |
#include "wptErrors.h" |
32 |
twoaday |
23 |
|
33 |
|
|
|
34 |
twoaday |
25 |
/* 'keyserver' command handler. */ |
35 |
|
|
static const char* |
36 |
twoaday |
23 |
cmd_keyserv_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
37 |
|
|
{ |
38 |
twoaday |
25 |
static char buf[32]; |
39 |
|
|
|
40 |
twoaday |
23 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
41 |
|
|
ctx->cmd_sent = 1; |
42 |
twoaday |
25 |
sprintf (buf, "uid %d", ctx->uid_index); |
43 |
|
|
return buf; |
44 |
|
|
} |
45 |
|
|
if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) { |
46 |
|
|
ctx->cnt = 1; |
47 |
twoaday |
23 |
return "keyserver"; |
48 |
|
|
} |
49 |
|
|
if (!strcmp (key, "keyedit.add_keyserver")) |
50 |
|
|
return ctx->url; |
51 |
|
|
if ( !strcmp (key, "keyedit.confirm_keyserver")) |
52 |
|
|
return "Y"; |
53 |
|
|
if (!strcmp (key, "passphrase.enter")) |
54 |
|
|
return ctx->pass; |
55 |
twoaday |
25 |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) { |
56 |
twoaday |
24 |
ctx->reset (); |
57 |
twoaday |
23 |
return "save"; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
return NULL; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
static const char* |
65 |
|
|
cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
66 |
|
|
{ |
67 |
|
|
static char buf[32]; |
68 |
|
|
|
69 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
70 |
|
|
ctx->cmd_sent = 1; |
71 |
|
|
switch (ctx->type) { |
72 |
|
|
case GPG_EDITKEY_SIGN: return "sign"; |
73 |
|
|
case GPG_EDITKEY_TSIGN: return "tsign"; |
74 |
|
|
case GPG_EDITKEY_LSIGN: return "lsign"; |
75 |
|
|
case GPG_EDITKEY_NRSIGN: return"nrsign"; |
76 |
|
|
case GPG_EDITKEY_NRLSIGN: return "nrlsign"; |
77 |
|
|
} |
78 |
|
|
} |
79 |
twoaday |
25 |
if (!strcmp (key, "sign_uid.class")) { |
80 |
|
|
sprintf (buf, "%d", ctx->sig_class); |
81 |
twoaday |
23 |
return buf; |
82 |
|
|
} |
83 |
|
|
if (!strcmp (key, "sign_uid.expire")) |
84 |
|
|
return "Y"; /* the sig expires when the key expires */ |
85 |
|
|
if (!strcmp (key, "siggen.valid")) |
86 |
|
|
return ctx->exp_date; |
87 |
|
|
if (!strcmp (key, "trustsig_prompt.trust_value")) { |
88 |
|
|
sprintf (buf, "%d", ctx->trust_id); |
89 |
|
|
return buf; |
90 |
|
|
} |
91 |
|
|
if (!strcmp (key, "trustsig_prompt.trust_depth")) |
92 |
|
|
return ""; /* fixme */ |
93 |
|
|
if (!strcmp (key, "trustsig_prompt.trust_regexp")) |
94 |
|
|
return ""; /* fixme */ |
95 |
twoaday |
25 |
if (!strcmp (key, "sign_uid.local_promote_okay" ) ) |
96 |
twoaday |
23 |
return "Y"; |
97 |
twoaday |
25 |
if (!strcmp (key, "sign_uid.okay" ) ) |
98 |
twoaday |
23 |
return "Y"; |
99 |
twoaday |
25 |
if (!strcmp (key, "keyedit.sign_all.okay")) |
100 |
twoaday |
23 |
return "Y"; |
101 |
twoaday |
25 |
if (!strcmp ( key, "passphrase.enter")) |
102 |
twoaday |
23 |
return ctx->pass; |
103 |
twoaday |
25 |
if (!strcmp (key, "keyedit.prompt")) { |
104 |
twoaday |
24 |
ctx->reset (); |
105 |
twoaday |
23 |
return "save"; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
return NULL; |
109 |
|
|
} |
110 |
|
|
|
111 |
twoaday |
25 |
/* 'trust' command handler. */ |
112 |
twoaday |
23 |
static const char* |
113 |
|
|
cmd_trust_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
114 |
|
|
{ |
115 |
|
|
static char buf[4]; |
116 |
|
|
|
117 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
118 |
|
|
ctx->cmd_sent = 1; |
119 |
|
|
return "trust"; |
120 |
|
|
} |
121 |
|
|
if (!strcmp (key, "edit_ownertrust.set_ultimate.okay")) |
122 |
|
|
return "Y"; |
123 |
|
|
if (!strcmp (key, "edit_ownertrust.value" )) { |
124 |
|
|
sprintf (buf, "%d", ctx->trust_id); |
125 |
|
|
return buf; |
126 |
|
|
} |
127 |
|
|
if (!strcmp (key, "keyedit.prompt")) { |
128 |
twoaday |
24 |
ctx->reset (); |
129 |
twoaday |
23 |
return "save"; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
return NULL; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
|
136 |
twoaday |
25 |
/* 'adduid' command handler. */ |
137 |
twoaday |
23 |
static const char* |
138 |
|
|
cmd_adduid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
139 |
|
|
{ |
140 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
141 |
|
|
ctx->cmd_sent = 1; |
142 |
|
|
return "adduid"; |
143 |
|
|
} |
144 |
twoaday |
25 |
if (!strcmp (key, "keygen.name")) |
145 |
twoaday |
23 |
return ctx->name; |
146 |
twoaday |
25 |
if (!strcmp (key, "keygen.email")) |
147 |
twoaday |
23 |
return ctx->email; |
148 |
twoaday |
25 |
if (!strcmp (key, "keygen.comment")) |
149 |
|
|
return ctx->cmt? ctx->cmt : ""; |
150 |
|
|
if (!strcmp (key, "passphrase.enter")) |
151 |
twoaday |
23 |
return ctx->pass; |
152 |
twoaday |
25 |
if( !strcmp (key, "keyedit.prompt")) { |
153 |
twoaday |
24 |
ctx->reset (); |
154 |
twoaday |
23 |
return "save"; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
return NULL; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
|
161 |
|
|
static const char* |
162 |
|
|
cmd_deluid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key, |
163 |
|
|
int *r_step) |
164 |
|
|
{ |
165 |
|
|
static char buf[64]; |
166 |
|
|
int step = *r_step; |
167 |
|
|
|
168 |
twoaday |
25 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
169 |
twoaday |
23 |
sprintf (buf, "uid %d", ctx->uid_index); |
170 |
|
|
*r_step = step = 1; |
171 |
|
|
return buf; |
172 |
|
|
} |
173 |
twoaday |
25 |
if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
174 |
twoaday |
23 |
*r_step = step = 2; |
175 |
|
|
return "deluid"; |
176 |
|
|
} |
177 |
twoaday |
25 |
if (step == 2 && !strcmp (key, "keyedit.remove.uid.okay")) { |
178 |
twoaday |
23 |
*r_step = step = 3; |
179 |
|
|
return "Y"; |
180 |
|
|
} |
181 |
twoaday |
25 |
if (step == 3 && !strcmp (key, "keyedit.prompt" )) { |
182 |
twoaday |
23 |
*r_step = step = 0; |
183 |
twoaday |
24 |
ctx->reset (); |
184 |
twoaday |
23 |
return "save"; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
return NULL; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
|
191 |
|
|
static const char * |
192 |
|
|
cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key, |
193 |
|
|
int * r_step) |
194 |
|
|
{ |
195 |
|
|
static char buf[64]; |
196 |
|
|
int step = *r_step; |
197 |
|
|
|
198 |
|
|
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
199 |
|
|
sprintf (buf, "uid %d", ctx->uid_index); |
200 |
|
|
*r_step = step = 1; |
201 |
|
|
return buf; |
202 |
|
|
} |
203 |
|
|
if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
204 |
|
|
*r_step = step = 2; |
205 |
|
|
return "delsig"; |
206 |
|
|
} |
207 |
|
|
if (!strcmp (key, "keyedit.delsig.unknown") || |
208 |
|
|
!strcmp (key, "keyedit.delsig.valid")) { |
209 |
|
|
if (++ctx->cnt == ctx->sig_index) |
210 |
|
|
return "Y"; |
211 |
|
|
else |
212 |
|
|
return "N"; |
213 |
|
|
} |
214 |
|
|
if (ctx->sig_index == 0 && |
215 |
|
|
!strcmp (key, "keyedit.delsig.selfsig")) |
216 |
|
|
return "Y"; |
217 |
|
|
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
218 |
|
|
*r_step = step = 0; |
219 |
twoaday |
24 |
ctx->reset (); |
220 |
twoaday |
23 |
return "save"; |
221 |
|
|
} |
222 |
|
|
return NULL; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
|
226 |
twoaday |
25 |
/* 'delkey' command handler. */ |
227 |
twoaday |
23 |
static const char* |
228 |
|
|
cmd_delkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
229 |
|
|
const char *key, int *r_step ) |
230 |
|
|
{ |
231 |
|
|
static char buf[64]; |
232 |
|
|
int step = *r_step; |
233 |
|
|
|
234 |
twoaday |
25 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
235 |
|
|
sprintf (buf, "key %d", ctx->key_index); |
236 |
twoaday |
23 |
*r_step = step = 1; |
237 |
|
|
return buf; |
238 |
|
|
} |
239 |
twoaday |
25 |
if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
240 |
twoaday |
23 |
*r_step = step = 2; |
241 |
|
|
return "delkey"; |
242 |
|
|
} |
243 |
twoaday |
25 |
if (step == 2 && !strcmp (key, "keyedit.remove.subkey.okay")) { |
244 |
twoaday |
23 |
*r_step = step = 3; |
245 |
|
|
return "Y"; |
246 |
|
|
} |
247 |
twoaday |
25 |
if (step == 3 && !strcmp (key, "keyedit.prompt")) { |
248 |
twoaday |
23 |
*r_step = step = 0; |
249 |
twoaday |
24 |
ctx->reset (); |
250 |
twoaday |
23 |
return "save"; |
251 |
|
|
} |
252 |
twoaday |
25 |
|
253 |
twoaday |
23 |
return NULL; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
|
257 |
twoaday |
25 |
/* 'addkey' command handler. */ |
258 |
twoaday |
23 |
static const char* |
259 |
|
|
cmd_addkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
260 |
|
|
{ |
261 |
|
|
static char buf[64]; |
262 |
|
|
|
263 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
264 |
|
|
ctx->cmd_sent = 1; |
265 |
|
|
return "addkey"; |
266 |
|
|
} |
267 |
|
|
|
268 |
twoaday |
25 |
if (!strcmp (key, "passphrase.enter")) |
269 |
twoaday |
23 |
return ctx->pass; |
270 |
twoaday |
25 |
if (!strcmp (key, "keygen.algo")) { |
271 |
|
|
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_algo); |
272 |
twoaday |
23 |
return buf; |
273 |
|
|
} |
274 |
twoaday |
25 |
if (!strcmp (key, "keygen.size")) { |
275 |
|
|
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_size); |
276 |
twoaday |
23 |
return buf; |
277 |
|
|
} |
278 |
twoaday |
25 |
if (!strcmp (key, "keygen.valid")) { |
279 |
|
|
_snprintf (buf, sizeof buf-1, "%d", ctx->valid); |
280 |
twoaday |
23 |
return buf; |
281 |
|
|
} |
282 |
twoaday |
25 |
if (!strcmp (key, "keyedit.prompt")) { |
283 |
twoaday |
24 |
ctx->reset (); |
284 |
twoaday |
23 |
return "save"; |
285 |
|
|
} |
286 |
|
|
return NULL; |
287 |
|
|
} |
288 |
|
|
|
289 |
twoaday |
25 |
/* 'passwd' command handler. */ |
290 |
twoaday |
23 |
static const char* |
291 |
|
|
cmd_passwd_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
292 |
|
|
{ |
293 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
294 |
|
|
ctx->cmd_sent = 1; |
295 |
|
|
return "passwd"; |
296 |
|
|
} |
297 |
|
|
if( !strcmp (key, "passphrase.enter") && !ctx->cnt) { |
298 |
|
|
ctx->cnt = 1; |
299 |
|
|
return ctx->pass; |
300 |
|
|
} |
301 |
|
|
if( !strcmp (key, "passphrase.enter" )) |
302 |
|
|
return ctx->new_pass; |
303 |
|
|
if( !strcmp (key, "change_passwd.empty.okay" )) |
304 |
|
|
return ctx->flags? "Y" : "N"; |
305 |
twoaday |
25 |
if (!strcmp (key, "keyedit.prompt")) { |
306 |
twoaday |
24 |
ctx->reset (); |
307 |
twoaday |
23 |
return "save"; |
308 |
|
|
} |
309 |
|
|
return NULL; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
|
313 |
twoaday |
25 |
static const char* |
314 |
twoaday |
23 |
cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key) |
315 |
|
|
{ |
316 |
|
|
static char buf[128]; |
317 |
|
|
|
318 |
|
|
/* XXX: check the code. */ |
319 |
|
|
#if 0 |
320 |
|
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
321 |
|
|
ctx->cmd_sent = 1; |
322 |
|
|
return ""; |
323 |
|
|
} |
324 |
|
|
if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 0) { |
325 |
|
|
ctx->u.pref.id++; |
326 |
|
|
_snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs); |
327 |
|
|
return buf; |
328 |
|
|
} |
329 |
|
|
if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 1) { |
330 |
|
|
ctx->u.pref.id++; |
331 |
|
|
return "updpref"; |
332 |
|
|
} |
333 |
|
|
if (!strcmp ( key, "keyedit.updpref.okay")) |
334 |
|
|
return "Y"; |
335 |
|
|
if (!strcmp (key, "passphrase.enter")) |
336 |
|
|
return ctx->u.pref.passwd; |
337 |
|
|
if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) { |
338 |
|
|
ctx->u.pref.id = 0; |
339 |
twoaday |
24 |
ctx->reset (); |
340 |
twoaday |
23 |
return "save"; |
341 |
|
|
} |
342 |
|
|
#endif |
343 |
|
|
return NULL; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
|
347 |
twoaday |
25 |
/* 'primary' command handler. */ |
348 |
twoaday |
23 |
static const char* |
349 |
|
|
cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
350 |
|
|
const char *key, int *r_step ) |
351 |
|
|
{ |
352 |
|
|
static char buf[64]; |
353 |
|
|
int step = *r_step; |
354 |
|
|
|
355 |
twoaday |
25 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
356 |
twoaday |
23 |
sprintf (buf, "uid %d", ctx->uid_index); |
357 |
|
|
*r_step = step = 1; |
358 |
|
|
return buf; |
359 |
|
|
} |
360 |
twoaday |
25 |
if (step == 1 && !strcmp (key, "keyedit.prompt" )) { |
361 |
twoaday |
23 |
*r_step = step = 2; |
362 |
|
|
return "primary"; |
363 |
|
|
} |
364 |
twoaday |
25 |
if (step == 2 && !strcmp (key, "passphrase.enter")) { |
365 |
twoaday |
23 |
*r_step = step = 3; |
366 |
|
|
return ctx->pass; |
367 |
|
|
} |
368 |
twoaday |
25 |
if (step == 3 && !strcmp (key, "keyedit.prompt")) { |
369 |
twoaday |
23 |
*r_step = step = 0; |
370 |
twoaday |
24 |
ctx->reset (); |
371 |
twoaday |
23 |
return "save"; |
372 |
|
|
} |
373 |
|
|
return NULL; |
374 |
|
|
} |
375 |
|
|
|
376 |
twoaday |
25 |
/* 'expire' command handler. */ |
377 |
twoaday |
23 |
static const char* |
378 |
|
|
cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
379 |
|
|
const char *key, int *r_step ) |
380 |
|
|
{ |
381 |
|
|
static char buf[64]; |
382 |
|
|
int step = *r_step; |
383 |
|
|
|
384 |
twoaday |
25 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
385 |
|
|
sprintf (buf, "key %d", ctx->key_index); |
386 |
twoaday |
23 |
*r_step = step = 1; |
387 |
|
|
return buf; |
388 |
|
|
} |
389 |
twoaday |
25 |
if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
390 |
twoaday |
23 |
*r_step = step = 2; |
391 |
|
|
return "expire"; |
392 |
|
|
} |
393 |
|
|
if( step == 2 && !strcmp ( key, "keygen.valid" ) ) { |
394 |
|
|
*r_step = step = 3; |
395 |
twoaday |
25 |
sprintf (buf, "%d", ctx->valid); |
396 |
|
|
return buf; |
397 |
twoaday |
23 |
} |
398 |
twoaday |
25 |
if (step == 3 && !strcmp (key, "passphrase.enter")) { |
399 |
twoaday |
23 |
*r_step = step = 4; |
400 |
|
|
return ctx->pass; |
401 |
|
|
} |
402 |
twoaday |
25 |
if (step == 4 && !strcmp (key, "keyedit.prompt")) { |
403 |
twoaday |
23 |
*r_step = step = 0; |
404 |
twoaday |
24 |
ctx->reset (); |
405 |
twoaday |
23 |
return "save"; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
return NULL; |
409 |
|
|
} |
410 |
|
|
|
411 |
|
|
|
412 |
twoaday |
25 |
/* 'revuid' command handler. */ |
413 |
twoaday |
23 |
const char* |
414 |
twoaday |
25 |
cmd_revuid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
415 |
|
|
const char *key, int *r_step) |
416 |
|
|
{ |
417 |
|
|
static char buf[32]; |
418 |
|
|
int step = *r_step; |
419 |
|
|
|
420 |
|
|
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
421 |
|
|
*r_step = step = 1; |
422 |
|
|
sprintf (buf, "uid %d", ctx->uid_index); |
423 |
|
|
return buf; |
424 |
|
|
} |
425 |
|
|
else if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
426 |
|
|
*r_step = step = 2; |
427 |
|
|
return "revuid"; |
428 |
|
|
} |
429 |
|
|
else if (step == 2 && !strcmp (key, "keyedit.revoke.uid.okay")) { |
430 |
|
|
*r_step = step = 3; |
431 |
|
|
return "Y"; |
432 |
|
|
} |
433 |
|
|
else if (step == 3 && !strcmp (key, "ask_revocation_reason.code")) { |
434 |
|
|
*r_step = step = 4; |
435 |
|
|
return "4"; |
436 |
|
|
} |
437 |
|
|
else if (step == 4 && !strcmp (key, "ask_revocation_reason.text")) { |
438 |
|
|
*r_step = step = 5; |
439 |
|
|
return ""; |
440 |
|
|
} |
441 |
|
|
else if (step == 5 && !strcmp (key, "ask_revocation_reason.okay")) { |
442 |
|
|
*r_step = step = 6; |
443 |
|
|
return "Y"; |
444 |
|
|
} |
445 |
|
|
else if (step == 6 && !strcmp (key, "passphrase.enter")) { |
446 |
|
|
*r_step = step = 7; |
447 |
|
|
return ctx->pass; |
448 |
|
|
} |
449 |
|
|
else if (step == 7 && !strcmp (key, "keyedit.prompt")) { |
450 |
|
|
*r_step = step = 0; |
451 |
|
|
ctx->reset (); |
452 |
|
|
return "save"; |
453 |
|
|
} |
454 |
|
|
return NULL; |
455 |
|
|
} |
456 |
|
|
|
457 |
|
|
|
458 |
|
|
const char* |
459 |
twoaday |
23 |
cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
460 |
|
|
const char *key, int *r_step) |
461 |
|
|
{ |
462 |
|
|
static char buf[64]; |
463 |
|
|
int step = *r_step; |
464 |
|
|
|
465 |
|
|
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
466 |
|
|
sprintf( buf, "uid %d", ctx->uid_index); |
467 |
|
|
*r_step = step = 1; |
468 |
|
|
return buf; |
469 |
|
|
} |
470 |
|
|
if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) { |
471 |
|
|
*r_step = step = 2; |
472 |
|
|
return "revsig"; |
473 |
|
|
} |
474 |
|
|
if( step == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) { |
475 |
|
|
*r_step = step = 3; |
476 |
|
|
return "Y"; |
477 |
|
|
} |
478 |
|
|
if( step == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) { |
479 |
|
|
*r_step = step = 4; |
480 |
|
|
return "Y"; |
481 |
|
|
} |
482 |
|
|
if( step == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) { |
483 |
|
|
*r_step = step = 5; |
484 |
|
|
return "0"; |
485 |
|
|
} |
486 |
|
|
if( step == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) { |
487 |
|
|
*r_step = step = 6; |
488 |
twoaday |
25 |
return ""; |
489 |
twoaday |
23 |
} |
490 |
|
|
if( step == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) { |
491 |
|
|
*r_step = step = 7; |
492 |
|
|
return "Y"; |
493 |
|
|
} |
494 |
|
|
if( step == 7 && !strcmp (key, "passphrase.enter" ) ) { |
495 |
|
|
*r_step = step = 8; |
496 |
|
|
return ctx->pass; |
497 |
|
|
} |
498 |
|
|
if( step == 8 && !strcmp ( key, "keyedit.prompt" ) ) { |
499 |
|
|
*r_step = step = 0; |
500 |
twoaday |
24 |
ctx->reset (); |
501 |
twoaday |
23 |
return "save"; |
502 |
|
|
} |
503 |
|
|
|
504 |
|
|
return NULL; |
505 |
|
|
} |
506 |
|
|
|
507 |
|
|
|
508 |
twoaday |
25 |
/* 'revoke' command handler. */ |
509 |
twoaday |
23 |
static const char * |
510 |
|
|
cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
511 |
|
|
const char * key, int *r_step) |
512 |
|
|
{ |
513 |
|
|
int step = *r_step; |
514 |
|
|
static char buf[64]; |
515 |
|
|
|
516 |
|
|
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
517 |
|
|
sprintf( buf, "key %d", ctx->key_index); |
518 |
|
|
*r_step = step = 1; |
519 |
|
|
return buf; |
520 |
|
|
} |
521 |
|
|
if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) { |
522 |
|
|
*r_step = step = 2; |
523 |
|
|
return "revkey"; |
524 |
|
|
} |
525 |
|
|
if( step == 2 && !strcmp (key, "keyedit.revoke.subkey.okay" ) ) { |
526 |
|
|
*r_step = step = 3; |
527 |
|
|
return "Y"; |
528 |
|
|
} |
529 |
|
|
if( step == 3 && !strcmp ( key, "ask_revocation_reason.code" ) ) { |
530 |
|
|
sprintf( buf, "%d", ctx->reason); |
531 |
|
|
*r_step = step = 4; |
532 |
|
|
return buf; |
533 |
|
|
} |
534 |
|
|
if( step == 4 && !strcmp ( key, "ask_revocation_reason.text" ) ) { |
535 |
|
|
*r_step = step = 5; |
536 |
|
|
return ""; |
537 |
|
|
} |
538 |
|
|
if( step == 5 && !strcmp (key, "ask_revocation_reason.okay" ) ) { |
539 |
|
|
*r_step = step = 6; |
540 |
|
|
return "Y"; |
541 |
|
|
} |
542 |
|
|
if( step == 6 && !strcmp (key, "passphrase.enter" ) ) { |
543 |
|
|
*r_step = step = 7; |
544 |
|
|
return ctx->pass; |
545 |
|
|
} |
546 |
|
|
if( step == 7 && !strcmp ( key, "keyedit.prompt" ) ) { |
547 |
|
|
*r_step = step = 0; |
548 |
twoaday |
24 |
ctx->reset (); |
549 |
twoaday |
23 |
return "save"; |
550 |
|
|
} |
551 |
|
|
return NULL; |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
|
555 |
twoaday |
25 |
/* 'addrevoker' command handler. */ |
556 |
twoaday |
23 |
static const char * |
557 |
|
|
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
558 |
|
|
const char * key, int * r_step) |
559 |
|
|
{ |
560 |
|
|
int step = *r_step; |
561 |
|
|
|
562 |
twoaday |
25 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
563 |
twoaday |
23 |
*r_step = step = 1; |
564 |
twoaday |
25 |
return "addrevoker"; |
565 |
|
|
} |
566 |
|
|
if (step == 1 && !strcmp (key, "keyedit.add_revoker")) { |
567 |
|
|
*r_step = step = 2; |
568 |
twoaday |
23 |
/* XXX: handle already-signed. */ |
569 |
|
|
return ctx->name; |
570 |
|
|
} |
571 |
twoaday |
25 |
if (step == 2 && !strcmp (key, "keyedit.add_revoker.okay")) { |
572 |
|
|
*r_step = step = 3; |
573 |
twoaday |
23 |
return "Y"; |
574 |
|
|
} |
575 |
twoaday |
25 |
if (step == 3 && !strcmp (key, "passphrase.enter")) { |
576 |
|
|
*r_step = step = 4; |
577 |
twoaday |
23 |
return ctx->pass; |
578 |
|
|
} |
579 |
twoaday |
25 |
if (step == 4 && !strcmp (key, "keyedit.prompt")) { |
580 |
twoaday |
23 |
*r_step = step = 0; |
581 |
twoaday |
24 |
ctx->reset (); |
582 |
twoaday |
23 |
return "save"; |
583 |
|
|
} |
584 |
twoaday |
25 |
|
585 |
twoaday |
23 |
return NULL; |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
|
589 |
twoaday |
25 |
/* 'addphoto' command handler. */ |
590 |
twoaday |
23 |
static const char* |
591 |
twoaday |
25 |
cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
592 |
twoaday |
23 |
{ |
593 |
twoaday |
25 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
594 |
|
|
ctx->cmd_sent = 1; |
595 |
|
|
return "addphoto"; |
596 |
|
|
} |
597 |
twoaday |
23 |
if (!strcmp (key, "photoid.jpeg.add")) |
598 |
|
|
return ctx->url; |
599 |
|
|
if (!strcmp (key, "photoid.jpeg.size")) |
600 |
|
|
return "Y"; |
601 |
|
|
if (!strcmp (key, "passphrase.enter")) |
602 |
|
|
return ctx->pass; |
603 |
twoaday |
25 |
if (!strcmp (key, "keyedit.prompt")) { |
604 |
twoaday |
24 |
ctx->reset (); |
605 |
twoaday |
23 |
return "save"; |
606 |
|
|
} |
607 |
|
|
return NULL; |
608 |
|
|
} |
609 |
|
|
|
610 |
twoaday |
25 |
/* 'enable' and 'disable' command handler. */ |
611 |
twoaday |
23 |
static const char * |
612 |
|
|
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
613 |
|
|
const char * key, int mode) |
614 |
|
|
{ |
615 |
|
|
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
616 |
|
|
ctx->cmd_sent = 1; |
617 |
|
|
if (mode) |
618 |
|
|
return "disable"; |
619 |
|
|
return "enable"; |
620 |
|
|
} |
621 |
|
|
if (!strcmp (key, "keyedit.prompt")) { |
622 |
twoaday |
24 |
ctx->reset (); |
623 |
twoaday |
23 |
return "save"; |
624 |
|
|
} |
625 |
|
|
return NULL; |
626 |
|
|
} |
627 |
|
|
|
628 |
|
|
|
629 |
twoaday |
25 |
/* edit key dispatch handler. */ |
630 |
twoaday |
23 |
static gpgme_error_t |
631 |
twoaday |
25 |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
632 |
|
|
const char *key, int fd) |
633 |
twoaday |
23 |
{ |
634 |
|
|
static int step = 0; |
635 |
|
|
const char *out = NULL; |
636 |
|
|
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
637 |
twoaday |
24 |
HANDLE hd = (HANDLE)fd; |
638 |
|
|
DWORD n; |
639 |
twoaday |
23 |
|
640 |
twoaday |
24 |
if (!ke) |
641 |
twoaday |
23 |
return gpg_error (GPG_ERR_INV_ARG); |
642 |
twoaday |
25 |
|
643 |
|
|
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
644 |
|
|
switch (code) { |
645 |
|
|
case GPGME_STATUS_ALREADY_SIGNED: |
646 |
twoaday |
26 |
ke->setResult (GPG_EDITRES_ALREADY_SIGNED); |
647 |
twoaday |
25 |
break; |
648 |
|
|
case GPGME_STATUS_BAD_PASSPHRASE: |
649 |
twoaday |
26 |
ke->setResult (GPG_EDITRES_BAD_PASSPHRASE); |
650 |
twoaday |
25 |
break; |
651 |
|
|
} |
652 |
|
|
|
653 |
twoaday |
26 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) { |
654 |
twoaday |
25 |
/* If the entered passphrase is bad, we supply empty |
655 |
|
|
passphrase to abort and send 'quit' as soon as possible. */ |
656 |
|
|
if (!strcmp (key, "passphrase.enter")) |
657 |
|
|
WriteFile (hd, "\n", 1, &n, NULL); |
658 |
|
|
if (!strcmp (key, "keyedit.prompt")) |
659 |
|
|
WriteFile (hd, "quit\n", 5, &n, NULL); |
660 |
|
|
} |
661 |
twoaday |
23 |
|
662 |
|
|
switch (ke->type) { |
663 |
|
|
case GPG_EDITKEY_LSIGN: |
664 |
|
|
case GPG_EDITKEY_SIGN: |
665 |
|
|
case GPG_EDITKEY_NRSIGN: |
666 |
|
|
case GPG_EDITKEY_TSIGN: |
667 |
|
|
case GPG_EDITKEY_NRLSIGN: |
668 |
|
|
out = cmd_sign_handler (ke, code, key); |
669 |
|
|
break; |
670 |
|
|
|
671 |
|
|
case GPG_EDITKEY_TRUST: |
672 |
|
|
out = cmd_trust_handler (ke, code, key); |
673 |
|
|
break; |
674 |
|
|
|
675 |
|
|
case GPG_EDITKEY_ADDUID: |
676 |
|
|
out = cmd_adduid_handler (ke, code, key); |
677 |
|
|
break; |
678 |
|
|
|
679 |
|
|
case GPG_EDITKEY_DELUID: |
680 |
|
|
out = cmd_deluid_handler (ke, code, key, &step); |
681 |
|
|
break; |
682 |
|
|
|
683 |
|
|
case GPG_EDITKEY_DELSIG: |
684 |
|
|
out = cmd_delsig_handler (ke, code, key, &step); |
685 |
|
|
break; |
686 |
|
|
|
687 |
|
|
case GPG_EDITKEY_DELKEY: |
688 |
|
|
out = cmd_delkey_handler(ke, code, key, &step ); |
689 |
|
|
break; |
690 |
|
|
|
691 |
|
|
case GPG_EDITKEY_ADDKEY: |
692 |
|
|
out = cmd_addkey_handler (ke, code, key); |
693 |
|
|
break; |
694 |
|
|
|
695 |
|
|
case GPG_EDITKEY_PASSWD: |
696 |
|
|
out = cmd_passwd_handler (ke, code, key); |
697 |
|
|
break; |
698 |
|
|
|
699 |
|
|
case GPG_EDITKEY_PRIMARY: |
700 |
|
|
out = cmd_primary_handler (ke, code, key, &step); |
701 |
|
|
break; |
702 |
|
|
|
703 |
|
|
case GPG_EDITKEY_EXPIRE: |
704 |
|
|
out = cmd_expire_handler (ke, code, key, &step); |
705 |
|
|
break; |
706 |
|
|
|
707 |
|
|
case GPG_EDITKEY_REVSIG: |
708 |
|
|
out = cmd_revsig_handler (ke, code, key, &step); |
709 |
|
|
break; |
710 |
|
|
|
711 |
|
|
case GPG_EDITKEY_REVKEY: |
712 |
|
|
out = cmd_revkey_handler (ke, code, key, &step); |
713 |
|
|
break; |
714 |
twoaday |
25 |
|
715 |
|
|
case GPG_EDITKEY_REVUID: |
716 |
|
|
out = cmd_revuid_handler (ke, code, key, &step); |
717 |
|
|
break; |
718 |
twoaday |
23 |
|
719 |
|
|
case GPG_EDITKEY_ADDREV: |
720 |
|
|
out = cmd_addrev_handler (ke, code, key, &step); |
721 |
|
|
break; |
722 |
|
|
|
723 |
|
|
case GPG_EDITKEY_ADDPHOTO: |
724 |
twoaday |
25 |
out = cmd_addphoto_handler (ke, code, key); |
725 |
twoaday |
23 |
break; |
726 |
|
|
|
727 |
|
|
case GPG_EDITKEY_ENABLE: |
728 |
|
|
case GPG_EDITKEY_DISABLE: |
729 |
twoaday |
25 |
out = cmd_enable_disable_handler (ke, code, key, |
730 |
|
|
ke->type == GPG_EDITKEY_DISABLE? 1: 0); |
731 |
twoaday |
23 |
break; |
732 |
|
|
|
733 |
|
|
case GPG_EDITKEY_SETPREF: |
734 |
|
|
out = cmd_setpref_handler (ke, code, key); |
735 |
|
|
break; |
736 |
|
|
|
737 |
|
|
case GPG_EDITKEY_KEYSERV: |
738 |
|
|
out = cmd_keyserv_handler (ke, code, key); |
739 |
|
|
break; |
740 |
|
|
} |
741 |
twoaday |
25 |
|
742 |
twoaday |
23 |
if (out != NULL) { |
743 |
twoaday |
25 |
/* XXX: check return codes. */ |
744 |
|
|
WriteFile (hd, out, strlen (out), &n, NULL); |
745 |
twoaday |
24 |
WriteFile (hd, "\n", 1, &n, NULL); |
746 |
twoaday |
23 |
} |
747 |
|
|
return 0; |
748 |
|
|
} |
749 |
|
|
|
750 |
|
|
|
751 |
twoaday |
25 |
/* Check if a GPG status code occured which marks the |
752 |
|
|
current operation as failed. |
753 |
|
|
Return value: gpg error constant. */ |
754 |
|
|
static gpgme_error_t |
755 |
|
|
map_result (GpgKeyEdit *ke) |
756 |
|
|
{ |
757 |
twoaday |
26 |
if (!ke->getResult ()) |
758 |
twoaday |
25 |
return gpg_error (GPG_ERR_NO_ERROR); |
759 |
twoaday |
26 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) |
760 |
twoaday |
25 |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
761 |
|
|
return 0; |
762 |
|
|
} |
763 |
twoaday |
23 |
|
764 |
twoaday |
25 |
|
765 |
|
|
/* Wrapper around the gpgme edit interface. |
766 |
|
|
@ctx context to use. |
767 |
|
|
@key key on which the operation should be performed. |
768 |
|
|
@ek key edit context. |
769 |
|
|
Return value: 0 on success. */ |
770 |
twoaday |
23 |
gpgme_error_t |
771 |
|
|
gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek) |
772 |
|
|
{ |
773 |
|
|
gpgme_error_t err; |
774 |
twoaday |
24 |
gpgme_data_t out; |
775 |
twoaday |
23 |
|
776 |
twoaday |
24 |
err = gpgme_data_new (&out); |
777 |
|
|
if (err) |
778 |
|
|
return err; |
779 |
twoaday |
25 |
err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out); |
780 |
|
|
if (!err) |
781 |
|
|
err = map_result (ek); |
782 |
twoaday |
24 |
|
783 |
|
|
gpgme_data_release (out); |
784 |
twoaday |
23 |
return err; |
785 |
|
|
} |