18 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
#include <config.h> |
24 |
#endif |
#endif |
25 |
|
|
26 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdio.h> |
|
27 |
#include <string.h> |
#include <string.h> |
28 |
#include <stdlib.h> |
#include <stdlib.h> |
29 |
#include <assert.h> |
#include <assert.h> |
35 |
#include "wptKeyEdit.h" |
#include "wptKeyEdit.h" |
36 |
#include "wptErrors.h" |
#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. */ |
/* 'keyserver' command handler. */ |
45 |
static const char* |
static const char* |
49 |
|
|
50 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
51 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
52 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
53 |
return buf; |
return buf; |
54 |
} |
} |
55 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) { |
if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) { |
71 |
} |
} |
72 |
|
|
73 |
|
|
74 |
|
/* 'sign' command handler. */ |
75 |
static const char* |
static const char* |
76 |
cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
77 |
{ |
{ |
78 |
static char buf[32]; |
static char buf[32]; |
79 |
|
|
80 |
|
if (ctx->getUseridIndex () != -1 && |
81 |
|
ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
82 |
|
ctx->cnt++; |
83 |
|
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
84 |
|
return buf; |
85 |
|
} |
86 |
|
|
87 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
88 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
89 |
switch (ctx->getType ()) { |
switch (ctx->getType ()) { |
184 |
int step = *r_step; |
int step = *r_step; |
185 |
|
|
186 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
187 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
188 |
*r_step = step = 1; |
*r_step = step = 1; |
189 |
return buf; |
return buf; |
190 |
} |
} |
206 |
} |
} |
207 |
|
|
208 |
|
|
209 |
static const char * |
static const char* |
210 |
cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key, |
cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key, |
211 |
int * r_step) |
int *r_step) |
212 |
{ |
{ |
213 |
static char buf[64]; |
static char buf[64]; |
214 |
int step = *r_step; |
int step = *r_step; |
215 |
|
|
216 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
217 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
218 |
*r_step = step = 1; |
*r_step = step = 1; |
219 |
return buf; |
return buf; |
220 |
} |
} |
224 |
} |
} |
225 |
if (!strcmp (key, "keyedit.delsig.unknown") || |
if (!strcmp (key, "keyedit.delsig.unknown") || |
226 |
!strcmp (key, "keyedit.delsig.valid")) { |
!strcmp (key, "keyedit.delsig.valid")) { |
227 |
if (++ctx->cnt == ctx->sig_index) |
if (++ctx->cnt == ctx->getSigIndex ()) |
228 |
return "Y"; |
return "Y"; |
229 |
else |
else |
230 |
return "N"; |
return "N"; |
231 |
} |
} |
232 |
if (ctx->sig_index == 0 && |
if (!strcmp (key, "keyedit.delsig.selfsig")) |
|
!strcmp (key, "keyedit.delsig.selfsig")) |
|
233 |
return "Y"; |
return "Y"; |
234 |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
235 |
*r_step = step = 0; |
*r_step = step = 0; |
249 |
int step = *r_step; |
int step = *r_step; |
250 |
|
|
251 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
252 |
sprintf (buf, "key %d", ctx->key_index); |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
253 |
*r_step = step = 1; |
*r_step = step = 1; |
254 |
return buf; |
return buf; |
255 |
} |
} |
285 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
286 |
return ctx->pass; |
return ctx->pass; |
287 |
if (!strcmp (key, "keygen.algo")) { |
if (!strcmp (key, "keygen.algo")) { |
288 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_algo); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo); |
289 |
return buf; |
return buf; |
290 |
} |
} |
291 |
if (!strcmp (key, "keygen.size")) { |
if (!strcmp (key, "keygen.size")) { |
292 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_size); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size); |
293 |
return buf; |
return buf; |
294 |
} |
} |
295 |
if (!strcmp (key, "keygen.valid")) { |
if (!strcmp (key, "keygen.valid")) { |
296 |
_snprintf (buf, sizeof buf-1, "%d", ctx->valid); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->valid); |
297 |
return buf; |
return buf; |
298 |
} |
} |
299 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
311 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
312 |
return "passwd"; |
return "passwd"; |
313 |
} |
} |
314 |
if( !strcmp (key, "passphrase.enter") && !ctx->cnt) { |
if (!ctx->cnt && !strcmp (key, "passphrase.enter")) { |
315 |
ctx->cnt = 1; |
ctx->cnt = 1; |
316 |
return ctx->pass; |
return ctx->pass; |
317 |
} |
} |
318 |
if( !strcmp (key, "passphrase.enter" )) |
if (!strcmp (key, "passphrase.enter" )) |
319 |
return ctx->new_pass; |
return ctx->new_pass; |
320 |
if( !strcmp (key, "change_passwd.empty.okay" )) |
if (!strcmp (key, "change_passwd.empty.okay" )) |
321 |
return ctx->flags? "Y" : "N"; |
return ctx->flags? "Y" : "N"; |
322 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
323 |
ctx->reset (); |
ctx->reset (); |
324 |
return "save"; |
return "save"; |
325 |
} |
} |
326 |
|
|
327 |
return NULL; |
return NULL; |
328 |
} |
} |
329 |
|
|
331 |
static const char* |
static const char* |
332 |
cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key) |
cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key) |
333 |
{ |
{ |
334 |
|
static char buf[128]; |
335 |
|
|
336 |
/* XXX: check the code. */ |
/* XXX: check the code. */ |
337 |
#if 0 |
#if 0 |
|
static char buf[128]; |
|
338 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
339 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
340 |
return ""; |
return ""; |
371 |
int step = *r_step; |
int step = *r_step; |
372 |
|
|
373 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
374 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
375 |
*r_step = step = 1; |
*r_step = step = 1; |
376 |
return buf; |
return buf; |
377 |
} |
} |
400 |
int step = *r_step; |
int step = *r_step; |
401 |
|
|
402 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
403 |
sprintf (buf, "key %d", ctx->key_index); |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
404 |
*r_step = step = 1; |
*r_step = step = 1; |
405 |
return buf; |
return buf; |
406 |
} |
} |
437 |
|
|
438 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
439 |
*r_step = step = 1; |
*r_step = step = 1; |
440 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
441 |
return buf; |
return buf; |
442 |
} |
} |
443 |
else if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
else if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
481 |
int step = *r_step; |
int step = *r_step; |
482 |
|
|
483 |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
484 |
sprintf( buf, "uid %d", ctx->uid_index); |
sprintf( buf, "uid %d", ctx->getUseridIndex ()); |
485 |
*r_step = step = 1; |
*r_step = step = 1; |
486 |
return buf; |
return buf; |
487 |
} |
} |
532 |
static char buf[64]; |
static char buf[64]; |
533 |
|
|
534 |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
535 |
sprintf( buf, "key %d", ctx->key_index); |
sprintf( buf, "key %d", ctx->getKeyIndex ()); |
536 |
*r_step = step = 1; |
*r_step = step = 1; |
537 |
return buf; |
return buf; |
538 |
} |
} |
573 |
/* 'addrevoker' command handler. */ |
/* 'addrevoker' command handler. */ |
574 |
static const char * |
static const char * |
575 |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
576 |
const char * key, int * r_step) |
const char *key, int *r_step) |
577 |
{ |
{ |
578 |
int step = *r_step; |
int step = *r_step; |
579 |
|
|
580 |
/* If the isuser already signed the key, send an empty |
/* If the isuser already signed the key, send an empty |
581 |
string and jump to quit. */ |
string and jump to quit. */ |
582 |
if (ctx->getResult () & GPG_EDITRES_ALREADY_SIGNED |
if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED |
583 |
&& *r_step != -1 && *r_step != 4) { |
&& *r_step != -1 && *r_step != 4) { |
584 |
*r_step = step = -1; |
*r_step = step = -1; |
585 |
return ""; |
return ""; |
587 |
if (*r_step == -1) { |
if (*r_step == -1) { |
588 |
*r_step = step = 4; |
*r_step = step = 4; |
589 |
return ""; /* empty value to abort. */ |
return ""; /* empty value to abort. */ |
590 |
} |
} |
591 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
592 |
*r_step = step = 1; |
*r_step = step = 1; |
593 |
return "addrevoker"; |
return "addrevoker"; |
635 |
return NULL; |
return NULL; |
636 |
} |
} |
637 |
|
|
638 |
|
|
639 |
|
/* 'clean' command handler. */ |
640 |
|
static const char* |
641 |
|
cmd_clean_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
642 |
|
{ |
643 |
|
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
644 |
|
ctx->cmd_sent = 1; |
645 |
|
return "clean"; |
646 |
|
} |
647 |
|
if (!strcmp (key, "keyedit.prompt")) { |
648 |
|
ctx->reset (); |
649 |
|
return "save"; |
650 |
|
} |
651 |
|
return NULL; |
652 |
|
} |
653 |
|
|
654 |
|
|
655 |
/* 'enable' and 'disable' command handler. */ |
/* 'enable' and 'disable' command handler. */ |
656 |
static const char * |
static const char * |
657 |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
676 |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
677 |
const char *key, int fd) |
const char *key, int fd) |
678 |
{ |
{ |
679 |
static int step = 0; |
static int step = 0; /* XXX use ke->cnt */ |
680 |
const char *out = NULL; |
const char *out = NULL; |
681 |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
682 |
HANDLE hd = (HANDLE)fd; |
HANDLE hd = (HANDLE)fd; |
688 |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
689 |
switch (code) { |
switch (code) { |
690 |
case GPGME_STATUS_ALREADY_SIGNED: |
case GPGME_STATUS_ALREADY_SIGNED: |
691 |
ke->setResult (GPG_EDITRES_ALREADY_SIGNED); |
ke->setResult (EDITKEY_ERR_ALREADY_SIGNED); |
692 |
break; |
break; |
693 |
|
|
694 |
case GPGME_STATUS_BAD_PASSPHRASE: |
case GPGME_STATUS_BAD_PASSPHRASE: |
695 |
ke->setResult (GPG_EDITRES_BAD_PASSPHRASE); |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
696 |
break; |
break; |
697 |
|
|
698 |
default: |
default: |
699 |
break; |
break; |
700 |
} |
} |
701 |
|
|
702 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) { |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) { |
703 |
/* If the entered passphrase is bad, we supply empty |
/* If the entered passphrase is bad, we supply empty |
704 |
passphrase to abort and send 'quit' as soon as possible. */ |
passphrase to abort and send 'quit' as soon as possible. */ |
705 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
706 |
WriteFile (hd, "\n", 1, &n, NULL); |
WriteFile (hd, "\n", 1, &n, NULL); |
707 |
if (!strcmp (key, "keyedit.prompt")) |
if (!strcmp (key, "keyedit.prompt")) |
708 |
WriteFile (hd, "quit\n", 5, &n, NULL); |
WriteFile (hd, "quit\n", 5, &n, NULL); |
709 |
|
ke->reset (); |
710 |
|
step = 0; |
711 |
|
return 0; |
712 |
} |
} |
713 |
|
|
714 |
switch (ke->getType ()) { |
switch (ke->getType ()) { |
776 |
out = cmd_addphoto_handler (ke, code, key); |
out = cmd_addphoto_handler (ke, code, key); |
777 |
break; |
break; |
778 |
|
|
779 |
|
case GPG_EDITKEY_CLEAN: |
780 |
|
out = cmd_clean_handler (ke, code, key); |
781 |
|
break; |
782 |
|
|
783 |
case GPG_EDITKEY_ENABLE: |
case GPG_EDITKEY_ENABLE: |
784 |
case GPG_EDITKEY_DISABLE: |
case GPG_EDITKEY_DISABLE: |
785 |
out = cmd_enable_disable_handler (ke, code, key, |
out = cmd_enable_disable_handler (ke, code, key, |
813 |
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
814 |
if (!ke->getResult ()) |
if (!ke->getResult ()) |
815 |
return gpg_error (GPG_ERR_NO_ERROR); |
return gpg_error (GPG_ERR_NO_ERROR); |
816 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) |
817 |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
818 |
return 0; |
return 0; |
819 |
} |
} |