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

Annotation of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 333 - (hide annotations)
Tue Oct 13 10:51:21 2009 UTC (15 years, 4 months ago) by twoaday
File size: 22493 byte(s)


1 werner 36 /* wptKeyEditCB.cpp - Key edit callback handling
2 twoaday 333 * Copyright (C) 2005, 2006, 2009 Timo Schulz
3 werner 36 * 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     #ifdef HAVE_CONFIG_H
18     #include <config.h>
19     #endif
20    
21     #include <stdio.h>
22     #include <string.h>
23     #include <stdlib.h>
24     #include <assert.h>
25     #include <windows.h>
26    
27     #include "gpgme.h"
28     #include "wptCommonCtl.h"
29     #include "wptContext.h"
30     #include "wptKeyEdit.h"
31     #include "wptErrors.h"
32 twoaday 278 #include "wptTypes.h"
33 werner 36
34 twoaday 278
35 twoaday 129 /* Possible errors for the edit key operation. */
36     enum editkey_error_t {
37     EDITKEY_ERR_ALREADY_SIGNED = 1,
38     EDITKEY_ERR_BAD_PASSPHRASE = 2
39     };
40 werner 36
41 twoaday 200 typedef gpgme_status_code_t status_code_t;
42    
43 twoaday 260
44 twoaday 200 /* 'notation' command handler. */
45     static const char*
46     cmd_notation_handler (GpgKeyEdit *ctx, status_code_t code, const char *key)
47     {
48     static char buf[32];
49    
50     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
51     int uid = ctx->getUseridIndex ();
52     ctx->cmd_sent = 1;
53     if (uid != -1) {
54 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
55 twoaday 200 return buf;
56     }
57     }
58     if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 0) {
59     ctx->cnt = 1;
60     return "notation";
61     }
62     if (!strcmp (key, "keyedit.add_notation"))
63     return ctx->notation;
64     if (!strcmp (key, "passphrase.enter"))
65     return ctx->pass;
66    
67     return NULL;
68     }
69    
70    
71 werner 36 /* 'keyserver' command handler. */
72     static const char*
73     cmd_keyserv_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
74     {
75     static char buf[32];
76    
77     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
78 twoaday 181 int uid = ctx->getUseridIndex ();
79 werner 36 ctx->cmd_sent = 1;
80 twoaday 181 if (uid != -1) {
81 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
82 twoaday 181 return buf;
83     }
84 werner 36 }
85 twoaday 200 if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 0) {
86 werner 36 ctx->cnt = 1;
87     return "keyserver";
88     }
89     if (!strcmp (key, "keyedit.add_keyserver"))
90     return ctx->url;
91 twoaday 181 if (!strcmp (key, "keyedit.confirm_keyserver"))
92 werner 36 return "Y";
93     if (!strcmp (key, "passphrase.enter"))
94     return ctx->pass;
95     if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) {
96     ctx->reset ();
97     return "save";
98     }
99    
100     return NULL;
101     }
102    
103    
104 twoaday 119 /* 'sign' command handler. */
105 werner 36 static const char*
106     cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
107     {
108     static char buf[32];
109    
110 twoaday 170 if (ctx->getUseridIndex () != -1 &&
111     ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
112     ctx->cnt++;
113 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
114 twoaday 170 return buf;
115     }
116    
117 werner 36 if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
118     ctx->cmd_sent = 1;
119     switch (ctx->getType ()) {
120 twoaday 225 case GPG_EDITKEY_SIGN: return "sign";
121     case GPG_EDITKEY_TSIGN: return "tsign";
122     case GPG_EDITKEY_LSIGN: return "lsign";
123     case GPG_EDITKEY_NRSIGN: return "nrsign";
124     case GPG_EDITKEY_NRLSIGN: return "nrlsign";
125 werner 36 }
126     }
127     if (!strcmp (key, "sign_uid.class")) {
128     sprintf (buf, "%d", ctx->sig_class);
129     return buf;
130     }
131     if (!strcmp (key, "sign_uid.expire"))
132     return "Y"; /* the sig expires when the key expires */
133     if (!strcmp (key, "siggen.valid"))
134 twoaday 225 return ctx->exp_date? ctx->exp_date : "0";
135 werner 36 if (!strcmp (key, "trustsig_prompt.trust_value")) {
136     sprintf (buf, "%d", ctx->trust_id);
137     return buf;
138     }
139     if (!strcmp (key, "trustsig_prompt.trust_depth"))
140     return ""; /* fixme */
141     if (!strcmp (key, "trustsig_prompt.trust_regexp"))
142     return ""; /* fixme */
143     if (!strcmp (key, "sign_uid.local_promote_okay" ) )
144     return "Y";
145     if (!strcmp (key, "sign_uid.okay" ) )
146     return "Y";
147     if (!strcmp (key, "keyedit.sign_all.okay"))
148     return "Y";
149 twoaday 328 if (!ctx->usePassphraseCallback() && !strcmp (key, "passphrase.enter"))
150     return ctx->pass;
151 werner 36 if (!strcmp (key, "keyedit.prompt")) {
152     ctx->reset ();
153     return "save";
154     }
155    
156     return NULL;
157     }
158    
159     /* 'trust' command handler. */
160     static const char*
161     cmd_trust_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
162     {
163     static char buf[4];
164    
165     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
166     ctx->cmd_sent = 1;
167     return "trust";
168     }
169     if (!strcmp (key, "edit_ownertrust.set_ultimate.okay"))
170     return "Y";
171     if (!strcmp (key, "edit_ownertrust.value" )) {
172     sprintf (buf, "%d", ctx->trust_id);
173     return buf;
174     }
175     if (!strcmp (key, "keyedit.prompt")) {
176     ctx->reset ();
177     return "save";
178     }
179    
180     return NULL;
181     }
182    
183    
184     /* 'adduid' command handler. */
185     static const char*
186     cmd_adduid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
187     {
188     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
189     ctx->cmd_sent = 1;
190     return "adduid";
191     }
192     if (!strcmp (key, "keygen.name"))
193     return ctx->name;
194     if (!strcmp (key, "keygen.email"))
195     return ctx->email;
196     if (!strcmp (key, "keygen.comment"))
197     return ctx->cmt? ctx->cmt : "";
198     if (!strcmp (key, "passphrase.enter"))
199     return ctx->pass;
200     if( !strcmp (key, "keyedit.prompt")) {
201     ctx->reset ();
202     return "save";
203     }
204    
205     return NULL;
206     }
207    
208    
209     static const char*
210 twoaday 200 cmd_deluid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
211     const char *key)
212 werner 36 {
213     static char buf[64];
214    
215 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
216 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
217 twoaday 200 ctx->cnt = 1;
218 werner 36 return buf;
219     }
220 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
221     ctx->cnt = 2;
222 werner 36 return "deluid";
223     }
224 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.remove.uid.okay")) {
225     ctx->cnt = 3;
226 werner 36 return "Y";
227     }
228 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt" )) {
229 werner 36 ctx->reset ();
230     return "save";
231     }
232    
233     return NULL;
234     }
235    
236    
237 twoaday 170 static const char*
238 twoaday 200 cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
239 werner 36 {
240     static char buf[64];
241 twoaday 200 static int sig_cnt = 0;
242 werner 36
243 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
244 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
245 twoaday 200 ctx->cnt = 1;
246 werner 36 return buf;
247     }
248 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
249     ctx->cnt = 2;
250 werner 36 return "delsig";
251     }
252     if (!strcmp (key, "keyedit.delsig.unknown") ||
253     !strcmp (key, "keyedit.delsig.valid")) {
254 twoaday 200 if (++sig_cnt == ctx->getSigIndex ())
255 werner 36 return "Y";
256     else
257     return "N";
258     }
259 twoaday 160 if (!strcmp (key, "keyedit.delsig.selfsig"))
260 werner 36 return "Y";
261 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.prompt")) {
262     sig_cnt = 0;
263 werner 36 ctx->reset ();
264     return "save";
265     }
266     return NULL;
267     }
268    
269    
270     /* 'delkey' command handler. */
271     static const char*
272     cmd_delkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
273 twoaday 200 const char *key)
274 werner 36 {
275     static char buf[64];
276    
277 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
278 twoaday 278 _snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ());
279 twoaday 200 ctx->cnt = 1;
280 werner 36 return buf;
281     }
282 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
283     ctx->cnt = 2;
284 werner 36 return "delkey";
285     }
286 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.remove.subkey.okay")) {
287     ctx->cnt = 3;
288 werner 36 return "Y";
289     }
290 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt")) {
291 werner 36 ctx->reset ();
292     return "save";
293     }
294    
295     return NULL;
296     }
297    
298    
299 twoaday 260 /* '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 werner 36 /* 'addkey' command handler. */
335     static const char*
336 twoaday 260 cmd_addkey_handler (GpgKeyEdit *ctx, status_code_t code, const char *key)
337 werner 36 {
338     static char buf[64];
339    
340     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
341     ctx->cmd_sent = 1;
342     return "addkey";
343     }
344    
345     if (!strcmp (key, "passphrase.enter"))
346     return ctx->pass;
347     if (!strcmp (key, "keygen.algo")) {
348 twoaday 333 _snprintf (buf, DIM (buf)-1, "%d", ctx->pubkey_algo);
349 werner 36 return buf;
350     }
351     if (!strcmp (key, "keygen.size")) {
352 twoaday 333 _snprintf (buf, DIM (buf)-1, "%d", ctx->pubkey_size);
353 werner 36 return buf;
354     }
355     if (!strcmp (key, "keygen.valid")) {
356 twoaday 333 _snprintf (buf, DIM (buf)-1, "%d", ctx->getValidDays ());
357 werner 36 return buf;
358     }
359     if (!strcmp (key, "keyedit.prompt")) {
360     ctx->reset ();
361     return "save";
362     }
363     return NULL;
364     }
365    
366 twoaday 200
367 werner 36 /* 'passwd' command handler. */
368     static const char*
369     cmd_passwd_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
370     {
371     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
372     ctx->cmd_sent = 1;
373     return "passwd";
374     }
375 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "passphrase.enter")) {
376 werner 36 ctx->cnt = 1;
377 twoaday 220 /* it is possible that there is no old passphrase. */
378     if (!ctx->pass && ctx->flags)
379     return "";
380 werner 36 return ctx->pass;
381     }
382 twoaday 129 if (!strcmp (key, "passphrase.enter" ))
383 werner 36 return ctx->new_pass;
384 twoaday 129 if (!strcmp (key, "change_passwd.empty.okay" ))
385 werner 36 return ctx->flags? "Y" : "N";
386     if (!strcmp (key, "keyedit.prompt")) {
387     ctx->reset ();
388     return "save";
389     }
390 twoaday 129
391 werner 36 return NULL;
392     }
393    
394    
395 twoaday 273 /* 'setpref' command handler. */
396 werner 36 static const char*
397 twoaday 260 cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
398 werner 36 {
399 twoaday 220 static char buf[128] = {0};
400 twoaday 129
401 twoaday 260 if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
402 twoaday 333 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
403 werner 36 ctx->cmd_sent = 1;
404 twoaday 260 ctx->cnt = 1;
405     return buf;
406 werner 36 }
407 twoaday 260 if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 1) {
408 twoaday 333 _snprintf (buf, DIM (buf)-1, "setpref %s", ctx->new_prefs);
409 twoaday 260 ctx->cnt = 2;
410 werner 36 return buf;
411     }
412 twoaday 260 if (!strcmp (key, "keyedit.setpref.okay"))
413 werner 36 return "Y";
414     if (!strcmp (key, "passphrase.enter"))
415 twoaday 260 return ctx->pass;
416     if (!strcmp (key, "keyedit.prompt") && ctx->cnt == 2) {
417 werner 36 ctx->reset ();
418     return "save";
419     }
420 twoaday 220 return buf;
421 werner 36 }
422    
423    
424     /* 'primary' command handler. */
425     static const char*
426 twoaday 200 cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
427     const char *key)
428 werner 36 {
429     static char buf[64];
430 twoaday 260
431 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
432 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
433 twoaday 200 ctx->cnt = 1;
434 werner 36 return buf;
435     }
436 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt" )) {
437     ctx->cnt = 2;
438 werner 36 return "primary";
439     }
440 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "passphrase.enter")) {
441     ctx->cnt = 3;
442 werner 36 return ctx->pass;
443     }
444 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt")) {
445 werner 36 ctx->reset ();
446     return "save";
447     }
448     return NULL;
449     }
450    
451 twoaday 200
452 werner 36 /* 'expire' command handler. */
453     static const char*
454     cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
455 twoaday 200 const char *key)
456 werner 36 {
457     static char buf[64];
458    
459 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
460 twoaday 278 _snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ());
461 twoaday 200 ctx->cnt = 1;
462 werner 36 return buf;
463     }
464 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
465     ctx->cnt = 2;
466 werner 36 return "expire";
467     }
468 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keygen.valid" )) {
469     ctx->cnt = 3;
470 twoaday 176 sprintf (buf, "%d", ctx->getValidDays ());
471 werner 36 return buf;
472     }
473 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) {
474     ctx->cnt = 4;
475 werner 36 return ctx->pass;
476     }
477 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) {
478 werner 36 ctx->reset ();
479     return "save";
480     }
481    
482     return NULL;
483     }
484    
485    
486     /* 'revuid' command handler. */
487     const char*
488     cmd_revuid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
489 twoaday 200 const char *key)
490 werner 36 {
491     static char buf[32];
492    
493 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
494     ctx->cnt = 1;
495 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
496 werner 36 return buf;
497     }
498 twoaday 200 else if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
499     ctx->cnt = 2;
500 werner 36 return "revuid";
501     }
502 twoaday 200 else if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.uid.okay")) {
503     ctx->cnt = 3;
504 werner 36 return "Y";
505     }
506 twoaday 200 else if (ctx->cnt == 3 && !strcmp (key, "ask_revocation_reason.code")) {
507     ctx->cnt = 4;
508 werner 36 return "4";
509     }
510 twoaday 200 else if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) {
511     ctx->cnt = 5;
512 werner 36 return "";
513     }
514 twoaday 200 else if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) {
515     ctx->cnt = 6;
516 werner 36 return "Y";
517     }
518 twoaday 200 else if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) {
519     ctx->cnt = 7;
520 werner 36 return ctx->pass;
521     }
522 twoaday 200 else if (ctx->cnt == 7 && !strcmp (key, "keyedit.prompt")) {
523 werner 36 ctx->reset ();
524     return "save";
525     }
526     return NULL;
527     }
528    
529    
530     const char*
531     cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
532 twoaday 200 const char *key)
533 werner 36 {
534     static char buf[64];
535    
536 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt" )) {
537 twoaday 278 _snprintf (buf, DIM (buf)-1, "uid %d", ctx->getUseridIndex ());
538 twoaday 200 ctx->cnt = 1;
539 werner 36 return buf;
540     }
541 twoaday 328 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
542 twoaday 200 ctx->cnt = 2;
543 werner 36 return "revsig";
544     }
545 twoaday 328 if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one")) {
546 twoaday 200 ctx->cnt = 3;
547 werner 36 return "Y";
548     }
549 twoaday 328 if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay")) {
550 twoaday 200 ctx->cnt = 4;
551 werner 36 return "Y";
552     }
553 twoaday 328 if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.code")) {
554 twoaday 200 ctx->cnt = 5;
555 werner 36 return "0";
556     }
557 twoaday 328 if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.text" )) {
558 twoaday 200 ctx->cnt = 6;
559 werner 36 return "";
560     }
561 twoaday 200 if (ctx->cnt == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
562     ctx->cnt = 7;
563 werner 36 return "Y";
564     }
565 twoaday 200 if (ctx->cnt == 7 && !strcmp (key, "passphrase.enter" ) ) {
566     ctx->cnt = 8;
567 werner 36 return ctx->pass;
568     }
569 twoaday 328 if (ctx->cnt == 8 && !strcmp (key, "keyedit.prompt")) {
570 werner 36 ctx->reset ();
571     return "save";
572     }
573    
574     return NULL;
575     }
576    
577    
578     /* 'revoke' command handler. */
579     static const char *
580     cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
581 twoaday 200 const char *key)
582 werner 36 {
583     static char buf[64];
584    
585 twoaday 200 if (ctx->cnt == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
586 twoaday 278 _snprintf (buf, DIM (buf)-1, "key %d", ctx->getKeyIndex ());
587 twoaday 200 ctx->cnt = 1;
588 werner 36 return buf;
589     }
590 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
591     ctx->cnt = 2;
592 werner 36 return "revkey";
593     }
594 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.subkey.okay")) {
595     ctx->cnt = 3;
596 werner 36 return "Y";
597     }
598 twoaday 200 if (ctx->cnt == 3 && !strcmp ( key, "ask_revocation_reason.code")) {
599 werner 36 sprintf( buf, "%d", ctx->reason);
600 twoaday 200 ctx->cnt = 4;
601 werner 36 return buf;
602     }
603 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) {
604     ctx->cnt = 5;
605 werner 36 return "";
606     }
607 twoaday 200 if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) {
608     ctx->cnt = 6;
609 werner 36 return "Y";
610     }
611 twoaday 200 if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) {
612     ctx->cnt = 7;
613 werner 36 return ctx->pass;
614     }
615 twoaday 200 if (ctx->cnt == 7 && !strcmp ( key, "keyedit.prompt")) {
616 werner 36 ctx->reset ();
617     return "save";
618     }
619     return NULL;
620     }
621    
622    
623     /* 'addrevoker' command handler. */
624     static const char *
625     cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
626 twoaday 200 const char *key)
627 werner 36 {
628    
629 twoaday 101 /* If the isuser already signed the key, send an empty
630 twoaday 119 string and jump to quit. */
631 twoaday 129 if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED
632 twoaday 200 && ctx->cnt != -1 && ctx->cnt != 4) {
633     ctx->cnt = -1;
634 twoaday 101 return "";
635     }
636 twoaday 200 if (ctx->cnt == -1) {
637     ctx->cnt = 4;
638 twoaday 101 return ""; /* empty value to abort. */
639 twoaday 119 }
640 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
641     ctx->cnt = 1;
642 werner 36 return "addrevoker";
643     }
644 twoaday 333 if (!strcmp (key, "keyedit.add_revoker"))
645 werner 36 return ctx->name;
646 twoaday 333
647     if (!strcmp (key, "keyedit.add_revoker.okay"))
648 werner 36 return "Y";
649 twoaday 333
650     if (!strcmp (key, "passphrase.enter"))
651 werner 36 return ctx->pass;
652 twoaday 333
653     if (ctx->cnt > 0 && !strcmp (key, "keyedit.prompt")) {
654 werner 36 ctx->reset ();
655     return "save";
656     }
657    
658     return NULL;
659     }
660    
661    
662     /* 'addphoto' command handler. */
663     static const char*
664     cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
665     {
666     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
667     ctx->cmd_sent = 1;
668     return "addphoto";
669     }
670     if (!strcmp (key, "photoid.jpeg.add"))
671     return ctx->url;
672     if (!strcmp (key, "photoid.jpeg.size"))
673     return "Y";
674     if (!strcmp (key, "passphrase.enter"))
675     return ctx->pass;
676     if (!strcmp (key, "keyedit.prompt")) {
677     ctx->reset ();
678     return "save";
679     }
680     return NULL;
681     }
682    
683 twoaday 119
684 twoaday 207 static const char*
685     cmd_minimize_handler (GpgKeyEdit *ctx, status_code_t code, const char *key)
686     {
687     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
688     ctx->cmd_sent = 1;
689     return "minimize";
690     }
691     if (!strcmp (key, "keyedit.prompt")) {
692     ctx->reset ();
693     return "save";
694     }
695    
696     return NULL;
697     }
698    
699 twoaday 119 /* 'clean' command handler. */
700     static const char*
701 twoaday 207 cmd_clean_handler (GpgKeyEdit *ctx, status_code_t code, const char *key)
702 twoaday 119 {
703     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
704     ctx->cmd_sent = 1;
705     return "clean";
706     }
707     if (!strcmp (key, "keyedit.prompt")) {
708     ctx->reset ();
709     return "save";
710     }
711     return NULL;
712     }
713    
714    
715 werner 36 /* 'enable' and 'disable' command handler. */
716     static const char *
717     cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
718     const char * key, int mode)
719     {
720     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
721     ctx->cmd_sent = 1;
722 twoaday 200 return mode? "disable": "enable";
723 werner 36 }
724     if (!strcmp (key, "keyedit.prompt")) {
725     ctx->reset ();
726     return "save";
727     }
728     return NULL;
729     }
730    
731    
732     /* edit key dispatch handler. */
733     static gpgme_error_t
734     editkey_command_handler (void *opaque, gpgme_status_code_t code,
735     const char *key, int fd)
736     {
737     const char *out = NULL;
738     GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
739     HANDLE hd = (HANDLE)fd;
740     DWORD n;
741    
742     if (!ke)
743     return gpg_error (GPG_ERR_INV_ARG);
744    
745     /*log_debug ("key=%s code=%d\r\n", key, code);*/
746     switch (code) {
747     case GPGME_STATUS_ALREADY_SIGNED:
748 twoaday 129 ke->setResult (EDITKEY_ERR_ALREADY_SIGNED);
749 werner 36 break;
750 twoaday 119
751 twoaday 333 //case GPGME_STATUS_GOOD_PASSPHRASE:
752     // ke->setResult (0);
753     // break;
754    
755 werner 36 case GPGME_STATUS_BAD_PASSPHRASE:
756 twoaday 333 if (!ke->usePassphraseCallback ()) {
757     log_debug ("editkey_command_handler: bad passphrase\n");
758     ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE);
759     }
760 werner 36 break;
761 twoaday 119
762 twoaday 68 default:
763     break;
764 werner 36 }
765    
766 twoaday 333 /* If the entered passphrase is bad, we supply empty
767     passphrase to abort and send 'quit' as soon as possible.
768     But this semantic is not used when we use the callback mode. */
769     if (!ke->usePassphraseCallback() &&
770     (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE)) {
771    
772 werner 36 if (!strcmp (key, "passphrase.enter"))
773     WriteFile (hd, "\n", 1, &n, NULL);
774     if (!strcmp (key, "keyedit.prompt"))
775     WriteFile (hd, "quit\n", 5, &n, NULL);
776 twoaday 129 ke->reset ();
777     return 0;
778 werner 36 }
779    
780     switch (ke->getType ()) {
781     case GPG_EDITKEY_LSIGN:
782     case GPG_EDITKEY_SIGN:
783     case GPG_EDITKEY_NRSIGN:
784     case GPG_EDITKEY_TSIGN:
785     case GPG_EDITKEY_NRLSIGN:
786     out = cmd_sign_handler (ke, code, key);
787     break;
788    
789     case GPG_EDITKEY_TRUST:
790     out = cmd_trust_handler (ke, code, key);
791     break;
792    
793     case GPG_EDITKEY_ADDUID:
794     out = cmd_adduid_handler (ke, code, key);
795     break;
796    
797     case GPG_EDITKEY_DELUID:
798 twoaday 200 out = cmd_deluid_handler (ke, code, key);
799 werner 36 break;
800    
801     case GPG_EDITKEY_DELSIG:
802 twoaday 260 out = cmd_delsig_handler (ke, code, key);
803 werner 36 break;
804    
805     case GPG_EDITKEY_DELKEY:
806 twoaday 200 out = cmd_delkey_handler(ke, code, key);
807 werner 36 break;
808    
809     case GPG_EDITKEY_ADDKEY:
810 twoaday 260 out = cmd_addkey_handler (ke, code, key);
811 werner 36 break;
812 twoaday 260
813     case GPG_EDITKEY_ADDCARDKEY:
814     out = cmd_addcardkey_handler (ke, code, key);
815     break;
816 werner 36
817     case GPG_EDITKEY_PASSWD:
818     out = cmd_passwd_handler (ke, code, key);
819     break;
820    
821     case GPG_EDITKEY_PRIMARY:
822 twoaday 200 out = cmd_primary_handler (ke, code, key);
823 werner 36 break;
824    
825     case GPG_EDITKEY_EXPIRE:
826 twoaday 200 out = cmd_expire_handler (ke, code, key);
827 werner 36 break;
828    
829     case GPG_EDITKEY_REVSIG:
830 twoaday 200 out = cmd_revsig_handler (ke, code, key);
831 werner 36 break;
832    
833     case GPG_EDITKEY_REVKEY:
834 twoaday 200 out = cmd_revkey_handler (ke, code, key);
835 werner 36 break;
836    
837     case GPG_EDITKEY_REVUID:
838 twoaday 200 out = cmd_revuid_handler (ke, code, key);
839 werner 36 break;
840    
841     case GPG_EDITKEY_ADDREV:
842 twoaday 200 out = cmd_addrev_handler (ke, code, key);
843 werner 36 break;
844    
845     case GPG_EDITKEY_ADDPHOTO:
846     out = cmd_addphoto_handler (ke, code, key);
847     break;
848    
849 twoaday 200 case GPG_EDITKEY_NOTATION:
850     out = cmd_notation_handler (ke, code, key);
851     break;
852    
853 twoaday 207 case GPG_EDITKEY_MINIMIZE:
854     out = cmd_minimize_handler (ke, code, key);
855     break;
856    
857 twoaday 119 case GPG_EDITKEY_CLEAN:
858     out = cmd_clean_handler (ke, code, key);
859     break;
860    
861 werner 36 case GPG_EDITKEY_ENABLE:
862     case GPG_EDITKEY_DISABLE:
863 twoaday 200 n = ke->getType () == GPG_EDITKEY_DISABLE? 1: 0;
864     out = cmd_enable_disable_handler (ke, code, key, n);
865 werner 36 break;
866    
867     case GPG_EDITKEY_SETPREF:
868     out = cmd_setpref_handler (ke, code, key);
869     break;
870    
871     case GPG_EDITKEY_KEYSERV:
872     out = cmd_keyserv_handler (ke, code, key);
873     break;
874     }
875    
876     if (out != NULL) {
877     WriteFile (hd, out, strlen (out), &n, NULL);
878     WriteFile (hd, "\n", 1, &n, NULL);
879     }
880     return 0;
881     }
882    
883    
884     /* Check if a GPG status code occured which marks the
885     current operation as failed.
886     Return value: gpg error constant. */
887     static gpgme_error_t
888     map_result (GpgKeyEdit *ke)
889 twoaday 333 {
890 werner 36 if (!ke->getResult ())
891     return gpg_error (GPG_ERR_NO_ERROR);
892 twoaday 129 if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE)
893 werner 36 return gpg_error (GPG_ERR_BAD_PASSPHRASE);
894     return 0;
895     }
896    
897    
898     /* Wrapper around the gpgme edit interface.
899     @ctx context to use.
900     @key key on which the operation should be performed.
901     @ek key edit context.
902     Return value: 0 on success. */
903     gpgme_error_t
904     gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
905     {
906     gpgme_error_t err;
907     gpgme_data_t out;
908    
909     err = gpgme_data_new (&out);
910     if (err)
911     return err;
912     err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);
913     if (!err)
914     err = map_result (ek);
915    
916     gpgme_data_release (out);
917     return err;
918     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26