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* |
48 |
static char buf[32]; |
static char buf[32]; |
49 |
|
|
50 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
51 |
|
int uid = ctx->getUseridIndex (); |
52 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
53 |
sprintf (buf, "uid %d", ctx->uid_index); |
if (uid != -1) { |
54 |
return buf; |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
55 |
|
return buf; |
56 |
|
} |
57 |
} |
} |
58 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) { |
if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) { |
59 |
ctx->cnt = 1; |
ctx->cnt = 1; |
61 |
} |
} |
62 |
if (!strcmp (key, "keyedit.add_keyserver")) |
if (!strcmp (key, "keyedit.add_keyserver")) |
63 |
return ctx->url; |
return ctx->url; |
64 |
if ( !strcmp (key, "keyedit.confirm_keyserver")) |
if (!strcmp (key, "keyedit.confirm_keyserver")) |
65 |
return "Y"; |
return "Y"; |
66 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
67 |
return ctx->pass; |
return ctx->pass; |
74 |
} |
} |
75 |
|
|
76 |
|
|
77 |
|
/* 'sign' command handler. */ |
78 |
static const char* |
static const char* |
79 |
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) |
80 |
{ |
{ |
81 |
static char buf[32]; |
static char buf[32]; |
82 |
|
|
83 |
|
if (ctx->getUseridIndex () != -1 && |
84 |
|
ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
85 |
|
ctx->cnt++; |
86 |
|
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
87 |
|
return buf; |
88 |
|
} |
89 |
|
|
90 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
91 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
92 |
switch (ctx->getType ()) { |
switch (ctx->getType ()) { |
187 |
int step = *r_step; |
int step = *r_step; |
188 |
|
|
189 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
190 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
191 |
*r_step = step = 1; |
*r_step = step = 1; |
192 |
return buf; |
return buf; |
193 |
} |
} |
209 |
} |
} |
210 |
|
|
211 |
|
|
212 |
static const char * |
static const char* |
213 |
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, |
214 |
int * r_step) |
int *r_step) |
215 |
{ |
{ |
216 |
static char buf[64]; |
static char buf[64]; |
217 |
int step = *r_step; |
int step = *r_step; |
218 |
|
|
219 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
220 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
221 |
*r_step = step = 1; |
*r_step = step = 1; |
222 |
return buf; |
return buf; |
223 |
} |
} |
227 |
} |
} |
228 |
if (!strcmp (key, "keyedit.delsig.unknown") || |
if (!strcmp (key, "keyedit.delsig.unknown") || |
229 |
!strcmp (key, "keyedit.delsig.valid")) { |
!strcmp (key, "keyedit.delsig.valid")) { |
230 |
if (++ctx->cnt == ctx->sig_index) |
if (++ctx->cnt == ctx->getSigIndex ()) |
231 |
return "Y"; |
return "Y"; |
232 |
else |
else |
233 |
return "N"; |
return "N"; |
234 |
} |
} |
235 |
if (ctx->sig_index == 0 && |
if (!strcmp (key, "keyedit.delsig.selfsig")) |
|
!strcmp (key, "keyedit.delsig.selfsig")) |
|
236 |
return "Y"; |
return "Y"; |
237 |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
if (step == 2 && !strcmp (key, "keyedit.prompt")) { |
238 |
*r_step = step = 0; |
*r_step = step = 0; |
252 |
int step = *r_step; |
int step = *r_step; |
253 |
|
|
254 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
255 |
sprintf (buf, "key %d", ctx->key_index); |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
256 |
*r_step = step = 1; |
*r_step = step = 1; |
257 |
return buf; |
return buf; |
258 |
} |
} |
288 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
289 |
return ctx->pass; |
return ctx->pass; |
290 |
if (!strcmp (key, "keygen.algo")) { |
if (!strcmp (key, "keygen.algo")) { |
291 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_algo); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo); |
292 |
return buf; |
return buf; |
293 |
} |
} |
294 |
if (!strcmp (key, "keygen.size")) { |
if (!strcmp (key, "keygen.size")) { |
295 |
_snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_size); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size); |
296 |
return buf; |
return buf; |
297 |
} |
} |
298 |
if (!strcmp (key, "keygen.valid")) { |
if (!strcmp (key, "keygen.valid")) { |
299 |
_snprintf (buf, sizeof buf-1, "%d", ctx->valid); |
_snprintf (buf, sizeof (buf)-1, "%d", ctx->getValidDays ()); |
300 |
return buf; |
return buf; |
301 |
} |
} |
302 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
314 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
315 |
return "passwd"; |
return "passwd"; |
316 |
} |
} |
317 |
if( !strcmp (key, "passphrase.enter") && !ctx->cnt) { |
if (!ctx->cnt && !strcmp (key, "passphrase.enter")) { |
318 |
ctx->cnt = 1; |
ctx->cnt = 1; |
319 |
return ctx->pass; |
return ctx->pass; |
320 |
} |
} |
321 |
if( !strcmp (key, "passphrase.enter" )) |
if (!strcmp (key, "passphrase.enter" )) |
322 |
return ctx->new_pass; |
return ctx->new_pass; |
323 |
if( !strcmp (key, "change_passwd.empty.okay" )) |
if (!strcmp (key, "change_passwd.empty.okay" )) |
324 |
return ctx->flags? "Y" : "N"; |
return ctx->flags? "Y" : "N"; |
325 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
326 |
ctx->reset (); |
ctx->reset (); |
327 |
return "save"; |
return "save"; |
328 |
} |
} |
329 |
|
|
330 |
return NULL; |
return NULL; |
331 |
} |
} |
332 |
|
|
334 |
static const char* |
static const char* |
335 |
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) |
336 |
{ |
{ |
337 |
|
static char buf[128]; |
338 |
|
|
339 |
/* XXX: check the code. */ |
/* XXX: check the code. */ |
340 |
#if 0 |
#if 0 |
|
static char buf[128]; |
|
341 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
342 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
343 |
return ""; |
return ""; |
374 |
int step = *r_step; |
int step = *r_step; |
375 |
|
|
376 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
377 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
378 |
*r_step = step = 1; |
*r_step = step = 1; |
379 |
return buf; |
return buf; |
380 |
} |
} |
403 |
int step = *r_step; |
int step = *r_step; |
404 |
|
|
405 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
406 |
sprintf (buf, "key %d", ctx->key_index); |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
407 |
*r_step = step = 1; |
*r_step = step = 1; |
408 |
return buf; |
return buf; |
409 |
} |
} |
413 |
} |
} |
414 |
if( step == 2 && !strcmp ( key, "keygen.valid" ) ) { |
if( step == 2 && !strcmp ( key, "keygen.valid" ) ) { |
415 |
*r_step = step = 3; |
*r_step = step = 3; |
416 |
sprintf (buf, "%d", ctx->valid); |
sprintf (buf, "%d", ctx->getValidDays ()); |
417 |
return buf; |
return buf; |
418 |
} |
} |
419 |
if (step == 3 && !strcmp (key, "passphrase.enter")) { |
if (step == 3 && !strcmp (key, "passphrase.enter")) { |
440 |
|
|
441 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
442 |
*r_step = step = 1; |
*r_step = step = 1; |
443 |
sprintf (buf, "uid %d", ctx->uid_index); |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
444 |
return buf; |
return buf; |
445 |
} |
} |
446 |
else if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
else if (step == 1 && !strcmp (key, "keyedit.prompt")) { |
484 |
int step = *r_step; |
int step = *r_step; |
485 |
|
|
486 |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
487 |
sprintf( buf, "uid %d", ctx->uid_index); |
sprintf( buf, "uid %d", ctx->getUseridIndex ()); |
488 |
*r_step = step = 1; |
*r_step = step = 1; |
489 |
return buf; |
return buf; |
490 |
} |
} |
535 |
static char buf[64]; |
static char buf[64]; |
536 |
|
|
537 |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
538 |
sprintf( buf, "key %d", ctx->key_index); |
sprintf( buf, "key %d", ctx->getKeyIndex ()); |
539 |
*r_step = step = 1; |
*r_step = step = 1; |
540 |
return buf; |
return buf; |
541 |
} |
} |
576 |
/* 'addrevoker' command handler. */ |
/* 'addrevoker' command handler. */ |
577 |
static const char * |
static const char * |
578 |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
579 |
const char * key, int * r_step) |
const char *key, int *r_step) |
580 |
{ |
{ |
581 |
int step = *r_step; |
int step = *r_step; |
582 |
|
|
583 |
/* If the isuser already signed the key, send an empty |
/* If the isuser already signed the key, send an empty |
584 |
string and jump to quit. */ |
string and jump to quit. */ |
585 |
if (ctx->getResult () & GPG_EDITRES_ALREADY_SIGNED |
if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED |
586 |
&& *r_step != -1 && *r_step != 4) { |
&& *r_step != -1 && *r_step != 4) { |
587 |
*r_step = step = -1; |
*r_step = step = -1; |
588 |
return ""; |
return ""; |
590 |
if (*r_step == -1) { |
if (*r_step == -1) { |
591 |
*r_step = step = 4; |
*r_step = step = 4; |
592 |
return ""; /* empty value to abort. */ |
return ""; /* empty value to abort. */ |
593 |
} |
} |
594 |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
if (step == 0 && !strcmp (key, "keyedit.prompt")) { |
595 |
*r_step = step = 1; |
*r_step = step = 1; |
596 |
return "addrevoker"; |
return "addrevoker"; |
638 |
return NULL; |
return NULL; |
639 |
} |
} |
640 |
|
|
641 |
|
|
642 |
|
/* 'clean' command handler. */ |
643 |
|
static const char* |
644 |
|
cmd_clean_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
645 |
|
{ |
646 |
|
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
647 |
|
ctx->cmd_sent = 1; |
648 |
|
return "clean"; |
649 |
|
} |
650 |
|
if (!strcmp (key, "keyedit.prompt")) { |
651 |
|
ctx->reset (); |
652 |
|
return "save"; |
653 |
|
} |
654 |
|
return NULL; |
655 |
|
} |
656 |
|
|
657 |
|
|
658 |
/* 'enable' and 'disable' command handler. */ |
/* 'enable' and 'disable' command handler. */ |
659 |
static const char * |
static const char * |
660 |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, |
679 |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
editkey_command_handler (void *opaque, gpgme_status_code_t code, |
680 |
const char *key, int fd) |
const char *key, int fd) |
681 |
{ |
{ |
682 |
static int step = 0; |
static int step = 0; /* XXX use ke->cnt */ |
683 |
const char *out = NULL; |
const char *out = NULL; |
684 |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
GpgKeyEdit *ke = (GpgKeyEdit *)opaque; |
685 |
HANDLE hd = (HANDLE)fd; |
HANDLE hd = (HANDLE)fd; |
691 |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
/*log_debug ("key=%s code=%d\r\n", key, code);*/ |
692 |
switch (code) { |
switch (code) { |
693 |
case GPGME_STATUS_ALREADY_SIGNED: |
case GPGME_STATUS_ALREADY_SIGNED: |
694 |
ke->setResult (GPG_EDITRES_ALREADY_SIGNED); |
ke->setResult (EDITKEY_ERR_ALREADY_SIGNED); |
695 |
break; |
break; |
696 |
|
|
697 |
case GPGME_STATUS_BAD_PASSPHRASE: |
case GPGME_STATUS_BAD_PASSPHRASE: |
698 |
ke->setResult (GPG_EDITRES_BAD_PASSPHRASE); |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
699 |
break; |
break; |
700 |
|
|
701 |
default: |
default: |
702 |
break; |
break; |
703 |
} |
} |
704 |
|
|
705 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) { |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) { |
706 |
/* If the entered passphrase is bad, we supply empty |
/* If the entered passphrase is bad, we supply empty |
707 |
passphrase to abort and send 'quit' as soon as possible. */ |
passphrase to abort and send 'quit' as soon as possible. */ |
708 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
709 |
WriteFile (hd, "\n", 1, &n, NULL); |
WriteFile (hd, "\n", 1, &n, NULL); |
710 |
if (!strcmp (key, "keyedit.prompt")) |
if (!strcmp (key, "keyedit.prompt")) |
711 |
WriteFile (hd, "quit\n", 5, &n, NULL); |
WriteFile (hd, "quit\n", 5, &n, NULL); |
712 |
|
ke->reset (); |
713 |
|
step = 0; |
714 |
|
return 0; |
715 |
} |
} |
716 |
|
|
717 |
switch (ke->getType ()) { |
switch (ke->getType ()) { |
779 |
out = cmd_addphoto_handler (ke, code, key); |
out = cmd_addphoto_handler (ke, code, key); |
780 |
break; |
break; |
781 |
|
|
782 |
|
case GPG_EDITKEY_CLEAN: |
783 |
|
out = cmd_clean_handler (ke, code, key); |
784 |
|
break; |
785 |
|
|
786 |
case GPG_EDITKEY_ENABLE: |
case GPG_EDITKEY_ENABLE: |
787 |
case GPG_EDITKEY_DISABLE: |
case GPG_EDITKEY_DISABLE: |
788 |
out = cmd_enable_disable_handler (ke, code, key, |
out = cmd_enable_disable_handler (ke, code, key, |
816 |
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
/* XXX Sometimes ALREADY_SIGNED indicates an failure. */ |
817 |
if (!ke->getResult ()) |
if (!ke->getResult ()) |
818 |
return gpg_error (GPG_ERR_NO_ERROR); |
return gpg_error (GPG_ERR_NO_ERROR); |
819 |
if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) |
if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) |
820 |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
return gpg_error (GPG_ERR_BAD_PASSPHRASE); |
821 |
return 0; |
return 0; |
822 |
} |
} |