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

Annotation of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 200 - (hide annotations)
Mon Apr 17 09:12:50 2006 UTC (18 years, 10 months ago) by twoaday
File size: 20865 byte(s)
2006-04-16  Timo Schulz  <ts@g10code.de>
 
        * wptHTTP.cpp (getErrorCode): New.
        (connect): Store winsock error code.
        * wptGPGMEData.cpp (is_armor_header): New.
        * wptGPG.cpp (check_gnupg_engine): Free context.
        (gnupg_backup_keyrings): Do not use global vars.
        * wptGPGUtil.cpp (gpg_export_seckey): Export in ascii format.
         
2006-04-15  Timo Schulz  <ts@g10code.de>
 
        * wptKeyManager.cpp (km_get_key): New.
        (km_key_show_revoc_info): New.
        * wptKeyRevokeDlg.cpp (key_revoke_dlg): Cleanups.
        (on_init_dialog): New.
        * wptKeyManagerDlg.cpp (key_manager_dlg_proc): Factour
        out some common code and use km_get_key() instead.
        * wptKeyEditDlgs.cpp (do_init_keylist): Change second
        param type. Change all callers.
        * wptKeyEdit.cpp (addNotation): New.
        * wptKeyEditCB.cpp (editkey_command_handler): Remove 'step'
        param everywhere. Change all callers.


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     return ctx->pass;
345     }
346 twoaday 129 if (!strcmp (key, "passphrase.enter" ))
347 werner 36 return ctx->new_pass;
348 twoaday 129 if (!strcmp (key, "change_passwd.empty.okay" ))
349 werner 36 return ctx->flags? "Y" : "N";
350     if (!strcmp (key, "keyedit.prompt")) {
351     ctx->reset ();
352     return "save";
353     }
354 twoaday 129
355 werner 36 return NULL;
356     }
357    
358    
359     static const char*
360     cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key)
361     {
362 twoaday 129 static char buf[128];
363    
364 werner 36 /* XXX: check the code. */
365     #if 0
366     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
367     ctx->cmd_sent = 1;
368     return "";
369     }
370 twoaday 200 if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 0) {
371 werner 36 ctx->u.pref.id++;
372     _snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs);
373     return buf;
374     }
375 twoaday 200 if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 1) {
376 werner 36 ctx->u.pref.id++;
377     return "updpref";
378     }
379 twoaday 200 if (!strcmp (key, "keyedit.updpref.okay"))
380 werner 36 return "Y";
381     if (!strcmp (key, "passphrase.enter"))
382     return ctx->u.pref.passwd;
383     if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) {
384     ctx->u.pref.id = 0;
385     ctx->reset ();
386     return "save";
387     }
388     #endif
389     return NULL;
390     }
391    
392    
393     /* 'primary' command handler. */
394     static const char*
395 twoaday 200 cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
396     const char *key)
397 werner 36 {
398     static char buf[64];
399    
400 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
401 twoaday 170 sprintf (buf, "uid %d", ctx->getUseridIndex ());
402 twoaday 200 ctx->cnt = 1;
403 werner 36 return buf;
404     }
405 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt" )) {
406     ctx->cnt = 2;
407 werner 36 return "primary";
408     }
409 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "passphrase.enter")) {
410     ctx->cnt = 3;
411 werner 36 return ctx->pass;
412     }
413 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "keyedit.prompt")) {
414 werner 36 ctx->reset ();
415     return "save";
416     }
417     return NULL;
418     }
419    
420 twoaday 200
421 werner 36 /* 'expire' command handler. */
422     static const char*
423     cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
424 twoaday 200 const char *key)
425 werner 36 {
426     static char buf[64];
427    
428 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
429 twoaday 170 sprintf (buf, "key %d", ctx->getKeyIndex ());
430 twoaday 200 ctx->cnt = 1;
431 werner 36 return buf;
432     }
433 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
434     ctx->cnt = 2;
435 werner 36 return "expire";
436     }
437 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keygen.valid" )) {
438     ctx->cnt = 3;
439 twoaday 176 sprintf (buf, "%d", ctx->getValidDays ());
440 werner 36 return buf;
441     }
442 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) {
443     ctx->cnt = 4;
444 werner 36 return ctx->pass;
445     }
446 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) {
447 werner 36 ctx->reset ();
448     return "save";
449     }
450    
451     return NULL;
452     }
453    
454    
455     /* 'revuid' command handler. */
456     const char*
457     cmd_revuid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
458 twoaday 200 const char *key)
459 werner 36 {
460     static char buf[32];
461    
462 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
463     ctx->cnt = 1;
464 twoaday 170 sprintf (buf, "uid %d", ctx->getUseridIndex ());
465 werner 36 return buf;
466     }
467 twoaday 200 else if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
468     ctx->cnt = 2;
469 werner 36 return "revuid";
470     }
471 twoaday 200 else if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.uid.okay")) {
472     ctx->cnt = 3;
473 werner 36 return "Y";
474     }
475 twoaday 200 else if (ctx->cnt == 3 && !strcmp (key, "ask_revocation_reason.code")) {
476     ctx->cnt = 4;
477 werner 36 return "4";
478     }
479 twoaday 200 else if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) {
480     ctx->cnt = 5;
481 werner 36 return "";
482     }
483 twoaday 200 else if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) {
484     ctx->cnt = 6;
485 werner 36 return "Y";
486     }
487 twoaday 200 else if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) {
488     ctx->cnt = 7;
489 werner 36 return ctx->pass;
490     }
491 twoaday 200 else if (ctx->cnt == 7 && !strcmp (key, "keyedit.prompt")) {
492 werner 36 ctx->reset ();
493     return "save";
494     }
495     return NULL;
496     }
497    
498    
499     const char*
500     cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
501 twoaday 200 const char *key)
502 werner 36 {
503     static char buf[64];
504    
505 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt" )) {
506 twoaday 170 sprintf( buf, "uid %d", ctx->getUseridIndex ());
507 twoaday 200 ctx->cnt = 1;
508 werner 36 return buf;
509     }
510 twoaday 200 if (ctx->cnt == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
511     ctx->cnt = 2;
512 werner 36 return "revsig";
513     }
514 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {
515     ctx->cnt = 3;
516 werner 36 return "Y";
517     }
518 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {
519     ctx->cnt = 4;
520 werner 36 return "Y";
521     }
522 twoaday 200 if (ctx->cnt == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
523     ctx->cnt = 5;
524 werner 36 return "0";
525     }
526 twoaday 200 if (ctx->cnt == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
527     ctx->cnt = 6;
528 werner 36 return "";
529     }
530 twoaday 200 if (ctx->cnt == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
531     ctx->cnt = 7;
532 werner 36 return "Y";
533     }
534 twoaday 200 if (ctx->cnt == 7 && !strcmp (key, "passphrase.enter" ) ) {
535     ctx->cnt = 8;
536 werner 36 return ctx->pass;
537     }
538 twoaday 200 if (ctx->cnt == 8 && !strcmp ( key, "keyedit.prompt" ) ) {
539 werner 36 ctx->reset ();
540     return "save";
541     }
542    
543     return NULL;
544     }
545    
546    
547     /* 'revoke' command handler. */
548     static const char *
549     cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
550 twoaday 200 const char *key)
551 werner 36 {
552     static char buf[64];
553    
554 twoaday 200 if (ctx->cnt == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
555 twoaday 170 sprintf( buf, "key %d", ctx->getKeyIndex ());
556 twoaday 200 ctx->cnt = 1;
557 werner 36 return buf;
558     }
559 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.prompt")) {
560     ctx->cnt = 2;
561 werner 36 return "revkey";
562     }
563 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.revoke.subkey.okay")) {
564     ctx->cnt = 3;
565 werner 36 return "Y";
566     }
567 twoaday 200 if (ctx->cnt == 3 && !strcmp ( key, "ask_revocation_reason.code")) {
568 werner 36 sprintf( buf, "%d", ctx->reason);
569 twoaday 200 ctx->cnt = 4;
570 werner 36 return buf;
571     }
572 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "ask_revocation_reason.text")) {
573     ctx->cnt = 5;
574 werner 36 return "";
575     }
576 twoaday 200 if (ctx->cnt == 5 && !strcmp (key, "ask_revocation_reason.okay")) {
577     ctx->cnt = 6;
578 werner 36 return "Y";
579     }
580 twoaday 200 if (ctx->cnt == 6 && !strcmp (key, "passphrase.enter")) {
581     ctx->cnt = 7;
582 werner 36 return ctx->pass;
583     }
584 twoaday 200 if (ctx->cnt == 7 && !strcmp ( key, "keyedit.prompt")) {
585 werner 36 ctx->reset ();
586     return "save";
587     }
588     return NULL;
589     }
590    
591    
592     /* 'addrevoker' command handler. */
593     static const char *
594     cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
595 twoaday 200 const char *key)
596 werner 36 {
597    
598 twoaday 101 /* If the isuser already signed the key, send an empty
599 twoaday 119 string and jump to quit. */
600 twoaday 129 if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED
601 twoaday 200 && ctx->cnt != -1 && ctx->cnt != 4) {
602     ctx->cnt = -1;
603 twoaday 101 return "";
604     }
605 twoaday 200 if (ctx->cnt == -1) {
606     ctx->cnt = 4;
607 twoaday 101 return ""; /* empty value to abort. */
608 twoaday 119 }
609 twoaday 200 if (ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
610     ctx->cnt = 1;
611 werner 36 return "addrevoker";
612     }
613 twoaday 200 if (ctx->cnt == 1 && !strcmp (key, "keyedit.add_revoker")) {
614     ctx->cnt = 2;
615 werner 36 return ctx->name;
616     }
617 twoaday 200 if (ctx->cnt == 2 && !strcmp (key, "keyedit.add_revoker.okay")) {
618     ctx->cnt = 3;
619 werner 36 return "Y";
620     }
621 twoaday 200 if (ctx->cnt == 3 && !strcmp (key, "passphrase.enter")) {
622     ctx->cnt = 4;
623 werner 36 return ctx->pass;
624     }
625 twoaday 200 if (ctx->cnt == 4 && !strcmp (key, "keyedit.prompt")) {
626 werner 36 ctx->reset ();
627     return "save";
628     }
629    
630     return NULL;
631     }
632    
633    
634     /* 'addphoto' command handler. */
635     static const char*
636     cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
637     {
638     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
639     ctx->cmd_sent = 1;
640     return "addphoto";
641     }
642     if (!strcmp (key, "photoid.jpeg.add"))
643     return ctx->url;
644     if (!strcmp (key, "photoid.jpeg.size"))
645     return "Y";
646     if (!strcmp (key, "passphrase.enter"))
647     return ctx->pass;
648     if (!strcmp (key, "keyedit.prompt")) {
649     ctx->reset ();
650     return "save";
651     }
652     return NULL;
653     }
654    
655 twoaday 119
656     /* 'clean' command handler. */
657     static const char*
658     cmd_clean_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
659     {
660     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
661     ctx->cmd_sent = 1;
662     return "clean";
663     }
664     if (!strcmp (key, "keyedit.prompt")) {
665     ctx->reset ();
666     return "save";
667     }
668     return NULL;
669     }
670    
671    
672 werner 36 /* 'enable' and 'disable' command handler. */
673     static const char *
674     cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
675     const char * key, int mode)
676     {
677     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
678     ctx->cmd_sent = 1;
679 twoaday 200 return mode? "disable": "enable";
680 werner 36 }
681     if (!strcmp (key, "keyedit.prompt")) {
682     ctx->reset ();
683     return "save";
684     }
685     return NULL;
686     }
687    
688    
689     /* edit key dispatch handler. */
690     static gpgme_error_t
691     editkey_command_handler (void *opaque, gpgme_status_code_t code,
692     const char *key, int fd)
693     {
694     const char *out = NULL;
695     GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
696     HANDLE hd = (HANDLE)fd;
697     DWORD n;
698    
699     if (!ke)
700     return gpg_error (GPG_ERR_INV_ARG);
701    
702     /*log_debug ("key=%s code=%d\r\n", key, code);*/
703     switch (code) {
704     case GPGME_STATUS_ALREADY_SIGNED:
705 twoaday 129 ke->setResult (EDITKEY_ERR_ALREADY_SIGNED);
706 werner 36 break;
707 twoaday 119
708 werner 36 case GPGME_STATUS_BAD_PASSPHRASE:
709 twoaday 129 ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE);
710 werner 36 break;
711 twoaday 119
712 twoaday 68 default:
713     break;
714 werner 36 }
715    
716 twoaday 129 if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) {
717 werner 36 /* If the entered passphrase is bad, we supply empty
718     passphrase to abort and send 'quit' as soon as possible. */
719     if (!strcmp (key, "passphrase.enter"))
720     WriteFile (hd, "\n", 1, &n, NULL);
721     if (!strcmp (key, "keyedit.prompt"))
722     WriteFile (hd, "quit\n", 5, &n, NULL);
723 twoaday 129 ke->reset ();
724     return 0;
725 werner 36 }
726    
727     switch (ke->getType ()) {
728     case GPG_EDITKEY_LSIGN:
729     case GPG_EDITKEY_SIGN:
730     case GPG_EDITKEY_NRSIGN:
731     case GPG_EDITKEY_TSIGN:
732     case GPG_EDITKEY_NRLSIGN:
733     out = cmd_sign_handler (ke, code, key);
734     break;
735    
736     case GPG_EDITKEY_TRUST:
737     out = cmd_trust_handler (ke, code, key);
738     break;
739    
740     case GPG_EDITKEY_ADDUID:
741     out = cmd_adduid_handler (ke, code, key);
742     break;
743    
744     case GPG_EDITKEY_DELUID:
745 twoaday 200 out = cmd_deluid_handler (ke, code, key);
746 werner 36 break;
747    
748     case GPG_EDITKEY_DELSIG:
749 twoaday 200 out = cmd_delsig_handler (ke, code, key);
750 werner 36 break;
751    
752     case GPG_EDITKEY_DELKEY:
753 twoaday 200 out = cmd_delkey_handler(ke, code, key);
754 werner 36 break;
755    
756     case GPG_EDITKEY_ADDKEY:
757     out = cmd_addkey_handler (ke, code, key);
758     break;
759    
760     case GPG_EDITKEY_PASSWD:
761     out = cmd_passwd_handler (ke, code, key);
762     break;
763    
764     case GPG_EDITKEY_PRIMARY:
765 twoaday 200 out = cmd_primary_handler (ke, code, key);
766 werner 36 break;
767    
768     case GPG_EDITKEY_EXPIRE:
769 twoaday 200 out = cmd_expire_handler (ke, code, key);
770 werner 36 break;
771    
772     case GPG_EDITKEY_REVSIG:
773 twoaday 200 out = cmd_revsig_handler (ke, code, key);
774 werner 36 break;
775    
776     case GPG_EDITKEY_REVKEY:
777 twoaday 200 out = cmd_revkey_handler (ke, code, key);
778 werner 36 break;
779    
780     case GPG_EDITKEY_REVUID:
781 twoaday 200 out = cmd_revuid_handler (ke, code, key);
782 werner 36 break;
783    
784     case GPG_EDITKEY_ADDREV:
785 twoaday 200 out = cmd_addrev_handler (ke, code, key);
786 werner 36 break;
787    
788     case GPG_EDITKEY_ADDPHOTO:
789     out = cmd_addphoto_handler (ke, code, key);
790     break;
791    
792 twoaday 200 case GPG_EDITKEY_NOTATION:
793     out = cmd_notation_handler (ke, code, key);
794     break;
795    
796 twoaday 119 case GPG_EDITKEY_CLEAN:
797     out = cmd_clean_handler (ke, code, key);
798     break;
799    
800 werner 36 case GPG_EDITKEY_ENABLE:
801     case GPG_EDITKEY_DISABLE:
802 twoaday 200 n = ke->getType () == GPG_EDITKEY_DISABLE? 1: 0;
803     out = cmd_enable_disable_handler (ke, code, key, n);
804 werner 36 break;
805    
806     case GPG_EDITKEY_SETPREF:
807     out = cmd_setpref_handler (ke, code, key);
808     break;
809    
810     case GPG_EDITKEY_KEYSERV:
811     out = cmd_keyserv_handler (ke, code, key);
812     break;
813     }
814    
815     if (out != NULL) {
816     WriteFile (hd, out, strlen (out), &n, NULL);
817     WriteFile (hd, "\n", 1, &n, NULL);
818     }
819     return 0;
820     }
821    
822    
823     /* Check if a GPG status code occured which marks the
824     current operation as failed.
825     Return value: gpg error constant. */
826     static gpgme_error_t
827     map_result (GpgKeyEdit *ke)
828     {
829 twoaday 101 /* XXX Sometimes ALREADY_SIGNED indicates an failure. */
830 werner 36 if (!ke->getResult ())
831     return gpg_error (GPG_ERR_NO_ERROR);
832 twoaday 129 if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE)
833 werner 36 return gpg_error (GPG_ERR_BAD_PASSPHRASE);
834     return 0;
835     }
836    
837    
838     /* Wrapper around the gpgme edit interface.
839     @ctx context to use.
840     @key key on which the operation should be performed.
841     @ek key edit context.
842     Return value: 0 on success. */
843     gpgme_error_t
844     gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
845     {
846     gpgme_error_t err;
847     gpgme_data_t out;
848    
849     err = gpgme_data_new (&out);
850     if (err)
851     return err;
852     err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);
853     if (!err)
854     err = map_result (ek);
855    
856     gpgme_data_release (out);
857     return err;
858     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26