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* |
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 |
{ |
{ |
222 |
else |
else |
223 |
return "N"; |
return "N"; |
224 |
} |
} |
225 |
if (ctx->sig_index == 0 && |
if (!strcmp (key, "keyedit.delsig.selfsig")) |
|
!strcmp (key, "keyedit.delsig.selfsig")) |
|
226 |
return "Y"; |
return "Y"; |
227 |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
228 |
*r_step = step = 0; |
*r_step = step = 0; |
278 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
279 |
return ctx->pass; |
return ctx->pass; |
280 |
if (!strcmp (key, "keygen.algo")) { |
if (!strcmp (key, "keygen.algo")) { |
281 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_algo); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo); |
282 |
return buf; |
return buf; |
283 |
} |
} |
284 |
if (!strcmp (key, "keygen.size")) { |
if (!strcmp (key, "keygen.size")) { |
285 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_size); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size); |
286 |
return buf; |
return buf; |
287 |
} |
} |
288 |
if (!strcmp (key, "keygen.valid")) { |
if (!strcmp (key, "keygen.valid")) { |
289 |
_snprintf (buf, sizeof buf-1, "%d", ctx->valid); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->valid); |
290 |
return buf; |
return buf; |
291 |
} |
} |
292 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
304 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
305 |
return "passwd"; |
return "passwd"; |
306 |
} |
} |
307 |
if( !strcmp (key, "passphrase.enter") && !ctx->cnt) { |
if (!ctx->cnt && !strcmp (key, "passphrase.enter")) { |
308 |
ctx->cnt = 1; |
ctx->cnt = 1; |
309 |
return ctx->pass; |
return ctx->pass; |
310 |
} |
} |
311 |
if( !strcmp (key, "passphrase.enter" )) |
if (!strcmp (key, "passphrase.enter" )) |
312 |
return ctx->new_pass; |
return ctx->new_pass; |
313 |
if( !strcmp (key, "change_passwd.empty.okay" )) |
if (!strcmp (key, "change_passwd.empty.okay" )) |
314 |
return ctx->flags? "Y" : "N"; |
return ctx->flags? "Y" : "N"; |
315 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
316 |
ctx->reset (); |
ctx->reset (); |
317 |
return "save"; |
return "save"; |
318 |
} |
} |
319 |
|
|
320 |
return NULL; |
return NULL; |
321 |
} |
} |
322 |
|
|
566 |
/* 'addrevoker' command handler. */ |
/* 'addrevoker' command handler. */ |
567 |
static const char * |
static const char * |
568 |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
569 |
const char * key, int * r_step) |
const char *key, int *r_step) |
570 |
{ |
{ |
571 |
int step = *r_step; |
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")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
585 |
*r_step = step = 1; |
*r_step = step = 1; |
586 |
return "addrevoker"; |
return "addrevoker"; |
587 |
} |
} |
588 |
if (step == 1 && !strcmp (key, "keyedit.add_revoker")) { |
if (step == 1 && !strcmp (key, "keyedit.add_revoker")) { |
589 |
*r_step = step = 2; |
*r_step = step = 2; |
|
/* XXX: handle already-signed. */ |
|
590 |
return ctx->name; |
return ctx->name; |
591 |
} |
} |
592 |
if (step == 2 && !strcmp (key, "keyedit.add_revoker.okay")) { |
if (step == 2 && !strcmp (key, "keyedit.add_revoker.okay")) { |
628 |
return NULL; |
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. */ |
/* 'enable' and 'disable' command handler. */ |
649 |
static const char * |
static const char * |
650 |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
669 |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
670 |
const char *key, int fd) |
const char *key, int fd) |
671 |
{ |
{ |
672 |
static int step = 0; |
static int step = 0; /* XXX use ke->cnt */ |
673 |
const char *out = NULL; |
const char *out = NULL; |
674 |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
675 |
HANDLE hd = (HANDLE)fd; |
HANDLE hd = (HANDLE)fd; |
681 |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
682 |
switch (code) { |
switch (code) { |
683 |
case GPGME_STATUS_ALREADY_SIGNED: |
case GPGME_STATUS_ALREADY_SIGNED: |
684 |
ke->setResult (GPG_EDITRES_ALREADY_SIGNED); |
ke->setResult (EDITKEY_ERR_ALREADY_SIGNED); |
685 |
break; |
break; |
686 |
|
|
687 |
case GPGME_STATUS_BAD_PASSPHRASE: |
case GPGME_STATUS_BAD_PASSPHRASE: |
688 |
ke->setResult (GPG_EDITRES_BAD_PASSPHRASE); |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
689 |
|
break; |
690 |
|
|
691 |
|
default: |
692 |
break; |
break; |
693 |
} |
} |
694 |
|
|
695 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) { |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) { |
696 |
/* If the entered passphrase is bad, we supply empty |
/* If the entered passphrase is bad, we supply empty |
697 |
passphrase to abort and send 'quit' as soon as possible. */ |
passphrase to abort and send 'quit' as soon as possible. */ |
698 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
699 |
WriteFile (hd, "\n", 1, &n, NULL); |
WriteFile (hd, "\n", 1, &n, NULL); |
700 |
if (!strcmp (key, "keyedit.prompt")) |
if (!strcmp (key, "keyedit.prompt")) |
701 |
WriteFile (hd, "quit\n", 5, &n, NULL); |
WriteFile (hd, "quit\n", 5, &n, NULL); |
702 |
|
ke->reset (); |
703 |
|
step = 0; |
704 |
|
return 0; |
705 |
} |
} |
706 |
|
|
707 |
switch (ke->getType ()) { |
switch (ke->getType ()) { |
769 |
out = cmd_addphoto_handler (ke, code, key); |
out = cmd_addphoto_handler (ke, code, key); |
770 |
break; |
break; |
771 |
|
|
772 |
|
case GPG_EDITKEY_CLEAN: |
773 |
|
out = cmd_clean_handler (ke, code, key); |
774 |
|
break; |
775 |
|
|
776 |
case GPG_EDITKEY_ENABLE: |
case GPG_EDITKEY_ENABLE: |
777 |
case GPG_EDITKEY_DISABLE: |
case GPG_EDITKEY_DISABLE: |
778 |
out = cmd_enable_disable_handler (ke, code, key, |
out = cmd_enable_disable_handler (ke, code, key, |
803 |
static gpgme_error_t |
static gpgme_error_t |
804 |
map_result (GpgKeyEdit *ke) |
map_result (GpgKeyEdit *ke) |
805 |
{ |
{ |
806 |
|
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
807 |
if (!ke->getResult ()) |
if (!ke->getResult ()) |
808 |
return gpg_error (GPG_ERR_NO_ERROR); |
return gpg_error (GPG_ERR_NO_ERROR); |
809 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) |
810 |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
811 |
return 0; |
return 0; |
812 |
} |
} |