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

Annotation of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 278 - (hide annotations)
Mon Jan 15 22:02:04 2007 UTC (18 years, 1 month ago) by twoaday
File size: 22414 byte(s)
See ChangeLog.


1 werner 36 /* wptKeyEditCB.cpp - Key edit callback handling
2 twoaday 200 * Copyright (C) 2005, 2006 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     if (!strcmp ( key, "passphrase.enter"))
150     return ctx->pass;
151     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 129 _snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo);
349 werner 36 return buf;
350     }
351     if (!strcmp (key, "keygen.size")) {
352 twoaday 129 _snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size);
353 werner 36 return buf;
354     }
355     if (!strcmp (key, "keygen.valid")) {
356 twoaday 176 _snprintf (buf, sizeof (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     _snprintf (buf, sizeof (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     _snprintf (buf, sizeof buf-1, "setpref %s", ctx->new_prefs);
409     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 200 if (ctx->cnt == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
542     ctx->cnt = 2;
543 werner 36 return "revsig";
544     }
545 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {
546     ctx->cnt = 3;
547 werner 36 return "Y";
548     }
549 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {
550     ctx->cnt = 4;
551 werner 36 return "Y";
552     }
553 twoaday 200 if (ctx->cnt == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
554     ctx->cnt = 5;
555 werner 36 return "0";
556     }
557 twoaday 200 if (ctx->cnt == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
558     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 200 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 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.add_revoker")) {
645     ctx->cnt = 2;
646 werner 36 return ctx->name;
647     }
648 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.add_revoker.okay")) {
649     ctx->cnt = 3;
650 werner 36 return "Y";
651     }
652 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) {
653     ctx->cnt = 4;
654 werner 36 return ctx->pass;
655     }
656 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) {
657 werner 36 ctx->reset ();
658     return "save";
659     }
660    
661     return NULL;
662     }
663    
664    
665     /* 'addphoto' command handler. */
666     static const char*
667     cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
668     {
669     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
670     ctx->cmd_sent = 1;
671     return "addphoto";
672     }
673     if (!strcmp (key, "photoid.jpeg.add"))
674     return ctx->url;
675     if (!strcmp (key, "photoid.jpeg.size"))
676     return "Y";
677     if (!strcmp (key, "passphrase.enter"))
678     return ctx->pass;
679     if (!strcmp (key, "keyedit.prompt")) {
680     ctx->reset ();
681     return "save";
682     }
683     return NULL;
684     }
685    
686 twoaday 119
687 twoaday 207 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 twoaday 119 /* 'clean' command handler. */
703     static const char*
704 twoaday 207 cmd_clean_handler (GpgKeyEdit *ctx, status_code_t code, const char *key)
705 twoaday 119 {
706     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
707     ctx->cmd_sent = 1;
708     return "clean";
709     }
710     if (!strcmp (key, "keyedit.prompt")) {
711     ctx->reset ();
712     return "save";
713     }
714     return NULL;
715     }
716    
717    
718 werner 36 /* 'enable' and 'disable' command handler. */
719     static const char *
720     cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
721     const char * key, int mode)
722     {
723     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
724     ctx->cmd_sent = 1;
725 twoaday 200 return mode? "disable": "enable";
726 werner 36 }
727     if (!strcmp (key, "keyedit.prompt")) {
728     ctx->reset ();
729     return "save";
730     }
731     return NULL;
732     }
733    
734    
735     /* edit key dispatch handler. */
736     static gpgme_error_t
737     editkey_command_handler (void *opaque, gpgme_status_code_t code,
738     const char *key, int fd)
739     {
740     const char *out = NULL;
741     GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
742     HANDLE hd = (HANDLE)fd;
743     DWORD n;
744    
745     if (!ke)
746     return gpg_error (GPG_ERR_INV_ARG);
747    
748     /*log_debug ("key=%s code=%d\r\n", key, code);*/
749     switch (code) {
750     case GPGME_STATUS_ALREADY_SIGNED:
751 twoaday 129 ke->setResult (EDITKEY_ERR_ALREADY_SIGNED);
752 werner 36 break;
753 twoaday 119
754 werner 36 case GPGME_STATUS_BAD_PASSPHRASE:
755 twoaday 220 log_debug ("editkey_command_handler: bad passphrase\n");
756 twoaday 129 ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE);
757 werner 36 break;
758 twoaday 119
759 twoaday 68 default:
760     break;
761 werner 36 }
762    
763 twoaday 129 if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) {
764 werner 36 /* If the entered passphrase is bad, we supply empty
765     passphrase to abort and send 'quit' as soon as possible. */
766     if (!strcmp (key, "passphrase.enter"))
767     WriteFile (hd, "\n", 1, &n, NULL);
768     if (!strcmp (key, "keyedit.prompt"))
769     WriteFile (hd, "quit\n", 5, &n, NULL);
770 twoaday 129 ke->reset ();
771     return 0;
772 werner 36 }
773    
774     switch (ke->getType ()) {
775     case GPG_EDITKEY_LSIGN:
776     case GPG_EDITKEY_SIGN:
777     case GPG_EDITKEY_NRSIGN:
778     case GPG_EDITKEY_TSIGN:
779     case GPG_EDITKEY_NRLSIGN:
780     out = cmd_sign_handler (ke, code, key);
781     break;
782    
783     case GPG_EDITKEY_TRUST:
784     out = cmd_trust_handler (ke, code, key);
785     break;
786    
787     case GPG_EDITKEY_ADDUID:
788     out = cmd_adduid_handler (ke, code, key);
789     break;
790    
791     case GPG_EDITKEY_DELUID:
792 twoaday 200 out = cmd_deluid_handler (ke, code, key);
793 werner 36 break;
794    
795     case GPG_EDITKEY_DELSIG:
796 twoaday 260 out = cmd_delsig_handler (ke, code, key);
797 werner 36 break;
798    
799     case GPG_EDITKEY_DELKEY:
800 twoaday 200 out = cmd_delkey_handler(ke, code, key);
801 werner 36 break;
802    
803     case GPG_EDITKEY_ADDKEY:
804 twoaday 260 out = cmd_addkey_handler (ke, code, key);
805 werner 36 break;
806 twoaday 260
807     case GPG_EDITKEY_ADDCARDKEY:
808     out = cmd_addcardkey_handler (ke, code, key);
809     break;
810 werner 36
811     case GPG_EDITKEY_PASSWD:
812     out = cmd_passwd_handler (ke, code, key);
813     break;
814    
815     case GPG_EDITKEY_PRIMARY:
816 twoaday 200 out = cmd_primary_handler (ke, code, key);
817 werner 36 break;
818    
819     case GPG_EDITKEY_EXPIRE:
820 twoaday 200 out = cmd_expire_handler (ke, code, key);
821 werner 36 break;
822    
823     case GPG_EDITKEY_REVSIG:
824 twoaday 200 out = cmd_revsig_handler (ke, code, key);
825 werner 36 break;
826    
827     case GPG_EDITKEY_REVKEY:
828 twoaday 200 out = cmd_revkey_handler (ke, code, key);
829 werner 36 break;
830    
831     case GPG_EDITKEY_REVUID:
832 twoaday 200 out = cmd_revuid_handler (ke, code, key);
833 werner 36 break;
834    
835     case GPG_EDITKEY_ADDREV:
836 twoaday 200 out = cmd_addrev_handler (ke, code, key);
837 werner 36 break;
838    
839     case GPG_EDITKEY_ADDPHOTO:
840     out = cmd_addphoto_handler (ke, code, key);
841     break;
842    
843 twoaday 200 case GPG_EDITKEY_NOTATION:
844     out = cmd_notation_handler (ke, code, key);
845     break;
846    
847 twoaday 207 case GPG_EDITKEY_MINIMIZE:
848     out = cmd_minimize_handler (ke, code, key);
849     break;
850    
851 twoaday 119 case GPG_EDITKEY_CLEAN:
852     out = cmd_clean_handler (ke, code, key);
853     break;
854    
855 werner 36 case GPG_EDITKEY_ENABLE:
856     case GPG_EDITKEY_DISABLE:
857 twoaday 200 n = ke->getType () == GPG_EDITKEY_DISABLE? 1: 0;
858     out = cmd_enable_disable_handler (ke, code, key, n);
859 werner 36 break;
860    
861     case GPG_EDITKEY_SETPREF:
862     out = cmd_setpref_handler (ke, code, key);
863     break;
864    
865     case GPG_EDITKEY_KEYSERV:
866     out = cmd_keyserv_handler (ke, code, key);
867     break;
868     }
869    
870     if (out != NULL) {
871     WriteFile (hd, out, strlen (out), &n, NULL);
872     WriteFile (hd, "\n", 1, &n, NULL);
873     }
874     return 0;
875     }
876    
877    
878     /* Check if a GPG status code occured which marks the
879     current operation as failed.
880     Return value: gpg error constant. */
881     static gpgme_error_t
882     map_result (GpgKeyEdit *ke)
883     {
884 twoaday 101 /* XXX Sometimes ALREADY_SIGNED indicates an failure. */
885 werner 36 if (!ke->getResult ())
886     return gpg_error (GPG_ERR_NO_ERROR);
887 twoaday 129 if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE)
888 werner 36 return gpg_error (GPG_ERR_BAD_PASSPHRASE);
889     return 0;
890     }
891    
892    
893     /* Wrapper around the gpgme edit interface.
894     @ctx context to use.
895     @key key on which the operation should be performed.
896     @ek key edit context.
897     Return value: 0 on success. */
898     gpgme_error_t
899     gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
900     {
901     gpgme_error_t err;
902     gpgme_data_t out;
903    
904     err = gpgme_data_new (&out);
905     if (err)
906     return err;
907     err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);
908     if (!err)
909     err = map_result (ek);
910    
911     gpgme_data_release (out);
912     return err;
913     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26