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