/[winpt]/trunk/Src/wptKeyEditCB.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 17402 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

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 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <io.h>
26 #include <windows.h>
27
28 #include "w32gpgme.h"
29 #include "wptCommonCtl.h"
30 #include "wptContext.h"
31 #include "wptKeyEdit.h"
32 #include "wptErrors.h"
33
34
35 /* edit key callback for command 'keyserver' */
36 static const char *
37 cmd_keyserv_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
38 {
39 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
40 ctx->cmd_sent = 1;
41 return "keyserver";
42 }
43 if (!strcmp (key, "keyedit.add_keyserver"))
44 return ctx->url;
45 if ( !strcmp (key, "keyedit.confirm_keyserver"))
46 return "Y";
47 if (!strcmp (key, "passphrase.enter"))
48 return ctx->pass;
49 if (!strcmp (key, "keyedit.prompt")) {
50 ctx->reset ();
51 return "save";
52 }
53
54 return NULL;
55 }
56
57
58 static const char*
59 cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
60 {
61 static char buf[32];
62
63 if (code == GPGME_STATUS_ALREADY_SIGNED)
64 ctx->setResultValue (GPG_EDITRES_ALREADY_SIGNED);
65
66 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
67 ctx->cmd_sent = 1;
68 switch (ctx->type) {
69 case GPG_EDITKEY_SIGN: return "sign";
70 case GPG_EDITKEY_TSIGN: return "tsign";
71 case GPG_EDITKEY_LSIGN: return "lsign";
72 case GPG_EDITKEY_NRSIGN: return"nrsign";
73 case GPG_EDITKEY_NRLSIGN: return "nrlsign";
74 }
75 }
76 if( !strcmp( key, "sign_uid.class" ) ) {
77 sprintf( buf, "%d", ctx->sig_class );
78 return buf;
79 }
80 if (!strcmp (key, "sign_uid.expire"))
81 return "Y"; /* the sig expires when the key expires */
82 if (!strcmp (key, "siggen.valid"))
83 return ctx->exp_date;
84 if (!strcmp (key, "trustsig_prompt.trust_value")) {
85 sprintf (buf, "%d", ctx->trust_id);
86 return buf;
87 }
88 if (!strcmp (key, "trustsig_prompt.trust_depth"))
89 return ""; /* fixme */
90 if (!strcmp (key, "trustsig_prompt.trust_regexp"))
91 return ""; /* fixme */
92 if( !strcmp (key, "sign_uid.local_promote_okay" ) )
93 return "Y";
94 if( !strcmp (key, "sign_uid.okay" ) )
95 return "Y";
96 if( !strcmp (key, "keyedit.sign_all.okay" ) )
97 return "Y";
98 if( !strcmp ( key, "passphrase.enter" ) )
99 return ctx->pass;
100 if( !strcmp (key, "keyedit.prompt" ) ) {
101 ctx->reset ();
102 return "save";
103 }
104
105 return NULL;
106 }
107
108
109 static const char*
110 cmd_trust_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
111 {
112 static char buf[4];
113
114 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
115 ctx->cmd_sent = 1;
116 return "trust";
117 }
118 if (!strcmp (key, "edit_ownertrust.set_ultimate.okay"))
119 return "Y";
120 if (!strcmp (key, "edit_ownertrust.value" )) {
121 sprintf (buf, "%d", ctx->trust_id);
122 return buf;
123 }
124 if (!strcmp (key, "keyedit.prompt")) {
125 ctx->reset ();
126 return "save";
127 }
128
129 return NULL;
130 }
131
132
133 static const char*
134 cmd_adduid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
135 {
136 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
137 ctx->cmd_sent = 1;
138 return "adduid";
139 }
140 if( !strcmp ( key, "keygen.name" ) )
141 return ctx->name;
142 if( !strcmp (key, "keygen.email" ) )
143 return ctx->email;
144 if( !strcmp ( key, "keygen.comment" ) ) {
145 if (ctx->cmt)
146 return ctx->cmt;
147 return "";
148 }
149 if( !strcmp (key, "passphrase.enter" ) )
150 return ctx->pass;
151 if( !strcmp (key, "keyedit.prompt" ) ) {
152 ctx->reset ();
153 return "save";
154 }
155
156 return NULL;
157 }
158
159
160 static const char*
161 cmd_deluid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key,
162 int *r_step)
163 {
164 static char buf[64];
165 int step = *r_step;
166
167 if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
168 sprintf (buf, "uid %d", ctx->uid_index);
169 *r_step = step = 1;
170 return buf;
171 }
172 if( step == 1 && !strcmp( key, "keyedit.prompt" ) ) {
173 *r_step = step = 2;
174 return "deluid";
175 }
176 if( step == 2 && !strcmp ( key, "keyedit.remove.uid.okay" ) ) {
177 *r_step = step = 3;
178 return "Y";
179 }
180 if( step == 3 && !strcmp ( key, "keyedit.prompt" ) ) {
181 *r_step = step = 0;
182 ctx->reset ();
183 return "save";
184 }
185
186 return NULL;
187 }
188
189
190 static const char *
191 cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key,
192 int * r_step)
193 {
194 static char buf[64];
195 int step = *r_step;
196
197 if (step == 0 && !strcmp (key, "keyedit.prompt")) {
198 sprintf (buf, "uid %d", ctx->uid_index);
199 *r_step = step = 1;
200 return buf;
201 }
202 if (step == 1 && !strcmp (key, "keyedit.prompt")) {
203 *r_step = step = 2;
204 return "delsig";
205 }
206 if (!strcmp (key, "keyedit.delsig.unknown") ||
207 !strcmp (key, "keyedit.delsig.valid")) {
208 if (++ctx->cnt == ctx->sig_index)
209 return "Y";
210 else
211 return "N";
212 }
213 if (ctx->sig_index == 0 &&
214 !strcmp (key, "keyedit.delsig.selfsig"))
215 return "Y";
216 if (step == 2 && !strcmp (key, "keyedit.prompt")) {
217 *r_step = step = 0;
218 ctx->reset ();
219 return "save";
220 }
221 return NULL;
222 }
223
224
225 static const char*
226 cmd_delkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
227 const char *key, int *r_step )
228 {
229 static char buf[64];
230 int step = *r_step;
231
232 if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
233 sprintf( buf, "key %d", ctx->key_index);
234 *r_step = step = 1;
235 return buf;
236 }
237 if( step == 1 && !strcmp (key, "keyedit.prompt" ) ) {
238 *r_step = step = 2;
239 return "delkey";
240 }
241 if( step == 2 && !strcmp (key, "keyedit.remove.subkey.okay" ) ) {
242 *r_step = step = 3;
243 return "Y";
244 }
245 if( step == 3 && !strcmp (key, "keyedit.prompt" ) ) {
246 *r_step = step = 0;
247 ctx->reset ();
248 return "save";
249 }
250
251 return NULL;
252 }
253
254
255 static const char*
256 cmd_addkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
257 {
258 static char buf[64];
259
260 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
261 ctx->cmd_sent = 1;
262 return "addkey";
263 }
264
265 if( !strcmp (key, "passphrase.enter" ) )
266 return ctx->pass;
267 if( !strcmp (key, "keygen.algo" ) ) {
268 _snprintf( buf, sizeof buf-1, "%d", ctx->pubkey_algo);
269 return buf;
270 }
271 if( !strcmp (key, "keygen.size" ) ) {
272 _snprintf( buf, sizeof buf-1, "%d", ctx->pubkey_size);
273 return buf;
274 }
275 if( !strcmp (key, "keygen.valid" ) ) {
276 _snprintf( buf, sizeof buf-1, "%d", ctx->valid);
277 return buf;
278 }
279 if( !strcmp (key, "keyedit.prompt" ) ) {
280 ctx->reset ();
281 return "save";
282 }
283 return NULL;
284 }
285
286
287 static const char*
288 cmd_passwd_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
289 {
290 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
291 ctx->cmd_sent = 1;
292 return "passwd";
293 }
294 if( !strcmp (key, "passphrase.enter") && !ctx->cnt) {
295 ctx->cnt = 1;
296 return ctx->pass;
297 }
298 if( !strcmp (key, "passphrase.enter" ))
299 return ctx->new_pass;
300 if( !strcmp (key, "change_passwd.empty.okay" ))
301 return ctx->flags? "Y" : "N";
302 if( !strcmp ( key, "keyedit.prompt" ) ) {
303 ctx->reset ();
304 return "save";
305 }
306 return NULL;
307 }
308
309
310 static const char *
311 cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key)
312 {
313 static char buf[128];
314
315 /* XXX: check the code. */
316 #if 0
317 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
318 ctx->cmd_sent = 1;
319 return "";
320 }
321 if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 0) {
322 ctx->u.pref.id++;
323 _snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs);
324 return buf;
325 }
326 if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 1) {
327 ctx->u.pref.id++;
328 return "updpref";
329 }
330 if (!strcmp ( key, "keyedit.updpref.okay"))
331 return "Y";
332 if (!strcmp (key, "passphrase.enter"))
333 return ctx->u.pref.passwd;
334 if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) {
335 ctx->u.pref.id = 0;
336 ctx->reset ();
337 return "save";
338 }
339 #endif
340 return NULL;
341 }
342
343
344 static const char*
345 cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
346 const char *key, int *r_step )
347 {
348 static char buf[64];
349 int step = *r_step;
350
351 if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
352 sprintf (buf, "uid %d", ctx->uid_index);
353 *r_step = step = 1;
354 return buf;
355 }
356 if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
357 *r_step = step = 2;
358 return "primary";
359 }
360 if( step == 2 && !strcmp ( key, "passphrase.enter" ) ) {
361 *r_step = step = 3;
362 return ctx->pass;
363 }
364 if( step == 3 && !strcmp (key, "keyedit.prompt" ) ) {
365 *r_step = step = 0;
366 ctx->reset ();
367 return "save";
368 }
369 return NULL;
370 }
371
372
373 static const char*
374 cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
375 const char *key, int *r_step )
376 {
377 static char buf[64];
378 int step = *r_step;
379
380 if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
381 sprintf(buf, "key %d", ctx->key_index);
382 *r_step = step = 1;
383 return buf;
384 }
385 if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
386 *r_step = step = 2;
387 return "expire";
388 }
389 if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {
390 *r_step = step = 3;
391 if (ctx->valid) {
392 sprintf (buf, "%d", ctx->valid);
393 return buf;
394 }
395 else
396 return ctx->exp_date;
397 }
398 if( step == 3 && !strcmp (key, "passphrase.enter" ) ) {
399 *r_step = step = 4;
400 return ctx->pass;
401 }
402 if( step == 4 && !strcmp ( key, "keyedit.prompt" ) ) {
403 *r_step = step = 0;
404 ctx->reset ();
405 return "save";
406 }
407
408 return NULL;
409 }
410
411
412 const char*
413 cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
414 const char *key, int *r_step)
415 {
416 static char buf[64];
417 int step = *r_step;
418
419 if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
420 sprintf( buf, "uid %d", ctx->uid_index);
421 *r_step = step = 1;
422 return buf;
423 }
424 if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
425 *r_step = step = 2;
426 return "revsig";
427 }
428 if( step == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {
429 *r_step = step = 3;
430 return "Y";
431 }
432 if( step == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {
433 *r_step = step = 4;
434 return "Y";
435 }
436 if( step == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
437 *r_step = step = 5;
438 return "0";
439 }
440 if( step == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
441 *r_step = step = 6;
442 return "\n";
443 }
444 if( step == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
445 *r_step = step = 7;
446 return "Y";
447 }
448 if( step == 7 && !strcmp (key, "passphrase.enter" ) ) {
449 *r_step = step = 8;
450 return ctx->pass;
451 }
452 if( step == 8 && !strcmp ( key, "keyedit.prompt" ) ) {
453 *r_step = step = 0;
454 ctx->reset ();
455 return "save";
456 }
457
458 return NULL;
459 }
460
461
462 static const char *
463 cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
464 const char * key, int *r_step)
465 {
466 int step = *r_step;
467 static char buf[64];
468
469 if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
470 sprintf( buf, "key %d", ctx->key_index);
471 *r_step = step = 1;
472 return buf;
473 }
474 if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
475 *r_step = step = 2;
476 return "revkey";
477 }
478 if( step == 2 && !strcmp (key, "keyedit.revoke.subkey.okay" ) ) {
479 *r_step = step = 3;
480 return "Y";
481 }
482 if( step == 3 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
483 sprintf( buf, "%d", ctx->reason);
484 *r_step = step = 4;
485 return buf;
486 }
487 if( step == 4 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
488 *r_step = step = 5;
489 return "";
490 }
491 if( step == 5 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
492 *r_step = step = 6;
493 return "Y";
494 }
495 if( step == 6 && !strcmp (key, "passphrase.enter" ) ) {
496 *r_step = step = 7;
497 return ctx->pass;
498 }
499 if( step == 7 && !strcmp ( key, "keyedit.prompt" ) ) {
500 *r_step = step = 0;
501 ctx->reset ();
502 return "save";
503 }
504 return NULL;
505 }
506
507
508 static const char *
509 cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
510 const char * key, int * r_step)
511 {
512 int step = *r_step;
513
514 if ((step == 0 /*|| c->result.editk->already_signed*/)
515 && !strcmp (key, "keyedit.add_revoker")) {
516 *r_step = step = 1;
517 /* XXX: handle already-signed. */
518 return ctx->name;
519 }
520 if( step == 1 && !strcmp (key, "keyedit.add_revoker.okay" ) ) {
521 *r_step = step = 2;
522 return "Y";
523 }
524 if( step == 2 && !strcmp (key, "passphrase.enter" ) ) {
525 *r_step = step = 3;
526 return ctx->pass;
527 }
528 if (step == 3 && !strcmp (key, "keyedit.prompt")) {
529 *r_step = step = 0;
530 ctx->reset ();
531 return "save";
532 }
533 return NULL;
534 }
535
536
537 static const char*
538 cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key,
539 int *r_step )
540 {
541 int step = *r_step;
542
543 if (!strcmp (key, "photoid.jpeg.add"))
544 return ctx->url;
545 if (!strcmp (key, "photoid.jpeg.size"))
546 return "Y";
547 if (!strcmp (key, "passphrase.enter"))
548 return ctx->pass;
549 if (!strcmp (key, "keyedit.prompt")) {
550 *r_step = step = 0;
551 ctx->reset ();
552 return "save";
553 }
554 return NULL;
555 }
556
557
558 static const char *
559 cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
560 const char * key, int mode)
561 {
562 if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
563 ctx->cmd_sent = 1;
564 if (mode)
565 return "disable";
566 return "enable";
567 }
568 if (!strcmp (key, "keyedit.prompt")) {
569 ctx->reset ();
570 return "save";
571 }
572 return NULL;
573 }
574
575
576
577
578 static gpgme_error_t
579 editkey_command_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)
580 {
581 static int step = 0;
582 const char *out = NULL;
583 GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
584 HANDLE hd = (HANDLE)fd;
585 DWORD n;
586
587 if (!ke)
588 return gpg_error (GPG_ERR_INV_ARG);
589
590 switch (ke->type) {
591 case GPG_EDITKEY_LSIGN:
592 case GPG_EDITKEY_SIGN:
593 case GPG_EDITKEY_NRSIGN:
594 case GPG_EDITKEY_TSIGN:
595 case GPG_EDITKEY_NRLSIGN:
596 out = cmd_sign_handler (ke, code, key);
597 break;
598
599 case GPG_EDITKEY_TRUST:
600 out = cmd_trust_handler (ke, code, key);
601 log_debug ("key=%s out=%s\n", key, out);
602 break;
603
604 case GPG_EDITKEY_ADDUID:
605 out = cmd_adduid_handler (ke, code, key);
606 break;
607
608 case GPG_EDITKEY_DELUID:
609 out = cmd_deluid_handler (ke, code, key, &step);
610 break;
611
612 case GPG_EDITKEY_DELSIG:
613 out = cmd_delsig_handler (ke, code, key, &step);
614 break;
615
616 case GPG_EDITKEY_DELKEY:
617 out = cmd_delkey_handler(ke, code, key, &step );
618 break;
619
620 case GPG_EDITKEY_ADDKEY:
621 out = cmd_addkey_handler (ke, code, key);
622 break;
623
624 case GPG_EDITKEY_PASSWD:
625 out = cmd_passwd_handler (ke, code, key);
626 break;
627
628 case GPG_EDITKEY_PRIMARY:
629 out = cmd_primary_handler (ke, code, key, &step);
630 break;
631
632 case GPG_EDITKEY_EXPIRE:
633 out = cmd_expire_handler (ke, code, key, &step);
634 break;
635
636 case GPG_EDITKEY_REVSIG:
637 out = cmd_revsig_handler (ke, code, key, &step);
638 break;
639
640 case GPG_EDITKEY_REVKEY:
641 out = cmd_revkey_handler (ke, code, key, &step);
642 break;
643
644 case GPG_EDITKEY_ADDREV:
645 out = cmd_addrev_handler (ke, code, key, &step);
646 break;
647
648 case GPG_EDITKEY_ADDPHOTO:
649 out = cmd_addphoto_handler (ke, code, key, &step);
650 break;
651
652 case GPG_EDITKEY_ENABLE:
653 case GPG_EDITKEY_DISABLE:
654 out = cmd_enable_disable_handler (ke, code, key,
655 ke->type == GPG_EDITKEY_DISABLE? 1: 0);
656 break;
657
658 case GPG_EDITKEY_SETPREF:
659 out = cmd_setpref_handler (ke, code, key);
660 break;
661
662 case GPG_EDITKEY_KEYSERV:
663 out = cmd_keyserv_handler (ke, code, key);
664 break;
665 }
666 if (out != NULL) {
667 WriteFile (hd, out, strlen (out), &n, NULL);
668 WriteFile (hd, "\n", 1, &n, NULL);
669 /* XXX: check return codes. */
670 }
671 return 0;
672 }
673
674
675
676 gpgme_error_t
677 gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
678 {
679 gpgme_error_t err;
680 gpgme_data_t out;
681
682 err = gpgme_data_new (&out);
683 if (err)
684 return err;
685 err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);
686
687 gpgme_data_release (out);
688 return err;
689 }
690

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26