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

Annotation of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (hide annotations)
Tue May 30 15:31:49 2006 UTC (18 years, 9 months ago) by twoaday
File size: 21415 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26