13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
*/ |
*/ |
|
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
#include <config.h> |
19 |
#endif |
#endif |
29 |
#include "wptContext.h" |
#include "wptContext.h" |
30 |
#include "wptKeyEdit.h" |
#include "wptKeyEdit.h" |
31 |
#include "wptErrors.h" |
#include "wptErrors.h" |
32 |
|
#include "wptTypes.h" |
33 |
|
|
34 |
|
|
35 |
/* Possible errors for the edit key operation. */ |
/* Possible errors for the edit key operation. */ |
36 |
enum editkey_error_t { |
enum editkey_error_t { |
40 |
|
|
41 |
typedef gpgme_status_code_t status_code_t; |
typedef gpgme_status_code_t status_code_t; |
42 |
|
|
43 |
|
|
44 |
/* 'notation' command handler. */ |
/* 'notation' command handler. */ |
45 |
static const char* |
static const char* |
46 |
cmd_notation_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
cmd_notation_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
51 |
int uid = ctx->getUseridIndex (); |
int uid = ctx->getUseridIndex (); |
52 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
53 |
if (uid != -1) { |
if (uid != -1) { |
54 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
55 |
return buf; |
return buf; |
56 |
} |
} |
57 |
} |
} |
78 |
int uid = ctx->getUseridIndex (); |
int uid = ctx->getUseridIndex (); |
79 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
80 |
if (uid != -1) { |
if (uid != -1) { |
81 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
82 |
return buf; |
return buf; |
83 |
} |
} |
84 |
} |
} |
110 |
if (ctx->getUseridIndex () != -1 && |
if (ctx->getUseridIndex () != -1 && |
111 |
ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
112 |
ctx->cnt++; |
ctx->cnt++; |
113 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
114 |
return buf; |
return buf; |
115 |
} |
} |
116 |
|
|
117 |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
118 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
119 |
switch (ctx->getType ()) { |
switch (ctx->getType ()) { |
120 |
case GPG_EDITKEY_SIGN: return "sign"; |
case GPG_EDITKEY_SIGN: return "sign"; |
121 |
case GPG_EDITKEY_TSIGN: return "tsign"; |
case GPG_EDITKEY_TSIGN: return "tsign"; |
122 |
case GPG_EDITKEY_LSIGN: return "lsign"; |
case GPG_EDITKEY_LSIGN: return "lsign"; |
123 |
case GPG_EDITKEY_NRSIGN: return"nrsign"; |
case GPG_EDITKEY_NRSIGN: return "nrsign"; |
124 |
case GPG_EDITKEY_NRLSIGN: return "nrlsign"; |
case GPG_EDITKEY_NRLSIGN: return "nrlsign"; |
125 |
} |
} |
126 |
} |
} |
127 |
if (!strcmp (key, "sign_uid.class")) { |
if (!strcmp (key, "sign_uid.class")) { |
131 |
if (!strcmp (key, "sign_uid.expire")) |
if (!strcmp (key, "sign_uid.expire")) |
132 |
return "Y"; /* the sig expires when the key expires */ |
return "Y"; /* the sig expires when the key expires */ |
133 |
if (!strcmp (key, "siggen.valid")) |
if (!strcmp (key, "siggen.valid")) |
134 |
return ctx->exp_date; |
return ctx->exp_date? ctx->exp_date : "0"; |
135 |
if (!strcmp (key, "trustsig_prompt.trust_value")) { |
if (!strcmp (key, "trustsig_prompt.trust_value")) { |
136 |
sprintf (buf, "%d", ctx->trust_id); |
sprintf (buf, "%d", ctx->trust_id); |
137 |
return buf; |
return buf; |
146 |
return "Y"; |
return "Y"; |
147 |
if (!strcmp (key, "keyedit.sign_all.okay")) |
if (!strcmp (key, "keyedit.sign_all.okay")) |
148 |
return "Y"; |
return "Y"; |
149 |
if (!strcmp ( key, "passphrase.enter")) |
if (!ctx->usePassphraseCallback() && !strcmp (key, "passphrase.enter")) |
150 |
return ctx->pass; |
return ctx->pass; |
151 |
if (!strcmp (key, "keyedit.prompt")) { |
if (!strcmp (key, "keyedit.prompt")) { |
152 |
ctx->reset (); |
ctx->reset (); |
153 |
return "save"; |
return "save"; |
213 |
static char buf[64]; |
static char buf[64]; |
214 |
|
|
215 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
216 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
217 |
ctx->cnt = 1; |
ctx->cnt = 1; |
218 |
return buf; |
return buf; |
219 |
} |
} |
241 |
static int sig_cnt = 0; |
static int sig_cnt = 0; |
242 |
|
|
243 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
244 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
245 |
ctx->cnt = 1; |
ctx->cnt = 1; |
246 |
return buf; |
return buf; |
247 |
} |
} |
275 |
static char buf[64]; |
static char buf[64]; |
276 |
|
|
277 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
278 |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
_snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ()); |
279 |
ctx->cnt = 1; |
ctx->cnt = 1; |
280 |
return buf; |
return buf; |
281 |
} |
} |
296 |
} |
} |
297 |
|
|
298 |
|
|
299 |
|
/* 'addcardkey' command handler. */ |
300 |
|
static const char* |
301 |
|
cmd_addcardkey_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
302 |
|
{ |
303 |
|
static char buf[32]; |
304 |
|
|
305 |
|
/* XXX: actually we mix up things here, it would be better to |
306 |
|
implement this in CardEditCB but because the edit-key |
307 |
|
interface is used, we have to implement it here. */ |
308 |
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
309 |
|
ctx->cmd_sent = 1; |
310 |
|
return "addcardkey"; |
311 |
|
} |
312 |
|
if (!strcmp (key, "cardedit.genkeys.subkeytype")) { |
313 |
|
sprintf (buf, "%d", ctx->getKeyIndex ()); |
314 |
|
return buf; |
315 |
|
} |
316 |
|
if (!strcmp (key, "cardedit.genkeys.replace_key")) |
317 |
|
return "Y"; /* better issue an extra warning? */ |
318 |
|
if (!strcmp (key, "passphrase.adminpin.ask")) |
319 |
|
return ctx->pass; |
320 |
|
if (!strcmp (key, "passphrase.pin.ask")) |
321 |
|
return ctx->new_pass; |
322 |
|
if (!strcmp (key, "keygen.valid")) { |
323 |
|
sprintf (buf, "%d", ctx->getValidDays ()); |
324 |
|
return buf; |
325 |
|
} |
326 |
|
if (!strcmp (key, "keyedit.prompt")) { |
327 |
|
ctx->reset (); |
328 |
|
return "save"; |
329 |
|
} |
330 |
|
return NULL; |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
/* 'addkey' command handler. */ |
/* 'addkey' command handler. */ |
335 |
static const char* |
static const char* |
336 |
cmd_addkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
cmd_addkey_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
337 |
{ |
{ |
338 |
static char buf[64]; |
static char buf[64]; |
339 |
|
|
374 |
} |
} |
375 |
if (ctx->cnt == 0 && !strcmp (key, "passphrase.enter")) { |
if (ctx->cnt == 0 && !strcmp (key, "passphrase.enter")) { |
376 |
ctx->cnt = 1; |
ctx->cnt = 1; |
377 |
|
/* it is possible that there is no old passphrase. */ |
378 |
|
if (!ctx->pass && ctx->flags) |
379 |
|
return ""; |
380 |
return ctx->pass; |
return ctx->pass; |
381 |
} |
} |
382 |
if (!strcmp (key, "passphrase.enter" )) |
if (!strcmp (key, "passphrase.enter" )) |
392 |
} |
} |
393 |
|
|
394 |
|
|
395 |
|
/* 'setpref' command handler. */ |
396 |
static const char* |
static const char* |
397 |
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) |
398 |
{ |
{ |
399 |
static char buf[128]; |
static char buf[128] = {0}; |
400 |
|
|
401 |
/* XXX: check the code. */ |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
402 |
#if 0 |
_snprintf (buf, sizeof (buf)-1, "uid %d", ctx->getUseridIndex ()); |
|
if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) { |
|
403 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
404 |
return ""; |
ctx->cnt = 1; |
|
} |
|
|
if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 0) { |
|
|
ctx->u.pref.id++; |
|
|
_snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs); |
|
405 |
return buf; |
return buf; |
406 |
} |
} |
407 |
if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 1) { |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) { |
408 |
ctx->u.pref.id++; |
_snprintf (buf, sizeof buf-1, "setpref %s", ctx->new_prefs); |
409 |
return "updpref"; |
ctx->cnt = 2; |
410 |
|
return buf; |
411 |
} |
} |
412 |
if (!strcmp (key, "keyedit.updpref.okay")) |
if (!strcmp (key, "keyedit.setpref.okay")) |
413 |
return "Y"; |
return "Y"; |
414 |
if (!strcmp (key, "passphrase.enter")) |
if (!strcmp (key, "passphrase.enter")) |
415 |
return ctx->u.pref.passwd; |
return ctx->pass; |
416 |
if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) { |
if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 2) { |
|
ctx->u.pref.id = 0; |
|
417 |
ctx->reset (); |
ctx->reset (); |
418 |
return "save"; |
return "save"; |
419 |
} |
} |
420 |
#endif |
return buf; |
|
return NULL; |
|
421 |
} |
} |
422 |
|
|
423 |
|
|
427 |
const char *key) |
const char *key) |
428 |
{ |
{ |
429 |
static char buf[64]; |
static char buf[64]; |
430 |
|
|
431 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
432 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
433 |
ctx->cnt = 1; |
ctx->cnt = 1; |
434 |
return buf; |
return buf; |
435 |
} |
} |
457 |
static char buf[64]; |
static char buf[64]; |
458 |
|
|
459 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
460 |
sprintf (buf, "key %d", ctx->getKeyIndex ()); |
_snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ()); |
461 |
ctx->cnt = 1; |
ctx->cnt = 1; |
462 |
return buf; |
return buf; |
463 |
} |
} |
492 |
|
|
493 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) { |
494 |
ctx->cnt = 1; |
ctx->cnt = 1; |
495 |
sprintf (buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
496 |
return buf; |
return buf; |
497 |
} |
} |
498 |
else if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
else if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
534 |
static char buf[64]; |
static char buf[64]; |
535 |
|
|
536 |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt" )) { |
if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt" )) { |
537 |
sprintf( buf, "uid %d", ctx->getUseridIndex ()); |
_snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ()); |
538 |
ctx->cnt = 1; |
ctx->cnt = 1; |
539 |
return buf; |
return buf; |
540 |
} |
} |
541 |
if (ctx->cnt == 1 && !strcmp ( key, "keyedit.prompt" ) ) { |
if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) { |
542 |
ctx->cnt = 2; |
ctx->cnt = 2; |
543 |
return "revsig"; |
return "revsig"; |
544 |
} |
} |
545 |
if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) { |
if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one")) { |
546 |
ctx->cnt = 3; |
ctx->cnt = 3; |
547 |
return "Y"; |
return "Y"; |
548 |
} |
} |
549 |
if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) { |
if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay")) { |
550 |
ctx->cnt = 4; |
ctx->cnt = 4; |
551 |
return "Y"; |
return "Y"; |
552 |
} |
} |
553 |
if (ctx->cnt == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) { |
if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.code")) { |
554 |
ctx->cnt = 5; |
ctx->cnt = 5; |
555 |
return "0"; |
return "0"; |
556 |
} |
} |
557 |
if (ctx->cnt == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) { |
if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.text" )) { |
558 |
ctx->cnt = 6; |
ctx->cnt = 6; |
559 |
return ""; |
return ""; |
560 |
} |
} |
566 |
ctx->cnt = 8; |
ctx->cnt = 8; |
567 |
return ctx->pass; |
return ctx->pass; |
568 |
} |
} |
569 |
if (ctx->cnt == 8 && !strcmp ( key, "keyedit.prompt" ) ) { |
if (ctx->cnt == 8 && !strcmp (key, "keyedit.prompt")) { |
570 |
ctx->reset (); |
ctx->reset (); |
571 |
return "save"; |
return "save"; |
572 |
} |
} |
583 |
static char buf[64]; |
static char buf[64]; |
584 |
|
|
585 |
if (ctx->cnt == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
if (ctx->cnt == 0 && !strcmp ( key, "keyedit.prompt" ) ) { |
586 |
sprintf( buf, "key %d", ctx->getKeyIndex ()); |
_snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ()); |
587 |
ctx->cnt = 1; |
ctx->cnt = 1; |
588 |
return buf; |
return buf; |
589 |
} |
} |
684 |
} |
} |
685 |
|
|
686 |
|
|
687 |
|
static const char* |
688 |
|
cmd_minimize_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
689 |
|
{ |
690 |
|
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
691 |
|
ctx->cmd_sent = 1; |
692 |
|
return "minimize"; |
693 |
|
} |
694 |
|
if (!strcmp (key, "keyedit.prompt")) { |
695 |
|
ctx->reset (); |
696 |
|
return "save"; |
697 |
|
} |
698 |
|
|
699 |
|
return NULL; |
700 |
|
} |
701 |
|
|
702 |
/* 'clean' command handler. */ |
/* 'clean' command handler. */ |
703 |
static const char* |
static const char* |
704 |
cmd_clean_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key) |
cmd_clean_handler (GpgKeyEdit *ctx, status_code_t code, const char *key) |
705 |
{ |
{ |
706 |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) { |
707 |
ctx->cmd_sent = 1; |
ctx->cmd_sent = 1; |
752 |
break; |
break; |
753 |
|
|
754 |
case GPGME_STATUS_BAD_PASSPHRASE: |
case GPGME_STATUS_BAD_PASSPHRASE: |
755 |
|
log_debug ("editkey_command_handler: bad passphrase\n"); |
756 |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE); |
757 |
break; |
break; |
758 |
|
|
793 |
break; |
break; |
794 |
|
|
795 |
case GPG_EDITKEY_DELSIG: |
case GPG_EDITKEY_DELSIG: |
796 |
out = cmd_delsig_handler (ke, code, key); |
out = cmd_delsig_handler (ke, code, key); |
797 |
break; |
break; |
798 |
|
|
799 |
case GPG_EDITKEY_DELKEY: |
case GPG_EDITKEY_DELKEY: |
801 |
break; |
break; |
802 |
|
|
803 |
case GPG_EDITKEY_ADDKEY: |
case GPG_EDITKEY_ADDKEY: |
804 |
out = cmd_addkey_handler (ke, code, key); |
out = cmd_addkey_handler (ke, code, key); |
805 |
|
break; |
806 |
|
|
807 |
|
case GPG_EDITKEY_ADDCARDKEY: |
808 |
|
out = cmd_addcardkey_handler (ke, code, key); |
809 |
break; |
break; |
810 |
|
|
811 |
case GPG_EDITKEY_PASSWD: |
case GPG_EDITKEY_PASSWD: |
844 |
out = cmd_notation_handler (ke, code, key); |
out = cmd_notation_handler (ke, code, key); |
845 |
break; |
break; |
846 |
|
|
847 |
|
case GPG_EDITKEY_MINIMIZE: |
848 |
|
out = cmd_minimize_handler (ke, code, key); |
849 |
|
break; |
850 |
|
|
851 |
case GPG_EDITKEY_CLEAN: |
case GPG_EDITKEY_CLEAN: |
852 |
out = cmd_clean_handler (ke, code, key); |
out = cmd_clean_handler (ke, code, key); |
853 |
break; |
break; |