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