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

Annotation of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 16842 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 twoaday 23 /* wptKeyEditCB.cpp - Key edit callback handling
2     * Copyright (C) 2005 Timo Schulz
3     *
4     * This file is part of WinPT.
5     *
6     * WinPT is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20     #include <stdio.h>
21     #include <string.h>
22     #include <stdlib.h>
23     #include <assert.h>
24     #include <io.h>
25     #include <windows.h>
26    
27     #include "w32gpgme.h"
28     #include "wptCommonCtl.h"
29     #include "wptContext.h"
30     #include "wptKeyEdit.h"
31    
32    
33     /* edit key callback for command 'keyserver' */
34     static const char *
35     cmd_keyserv_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
36     {
37     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
38     ctx->cmd_sent = 1;
39     return "keyserver";
40     }
41     if (!strcmp (key, "keyedit.add_keyserver"))
42     return ctx->url;
43     if ( !strcmp (key, "keyedit.confirm_keyserver"))
44     return "Y";
45     if (!strcmp (key, "passphrase.enter"))
46     return ctx->pass;
47     if (!strcmp (key, "keyedit.prompt")) {
48     ctx->cmd_sent = 0;
49     return "save";
50     }
51    
52     return NULL;
53     }
54    
55    
56     static const char*
57     cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
58     {
59     static char buf[32];
60    
61     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
62     ctx->cmd_sent = 1;
63     switch (ctx->type) {
64     case GPG_EDITKEY_SIGN: return "sign";
65     case GPG_EDITKEY_TSIGN: return "tsign";
66     case GPG_EDITKEY_LSIGN: return "lsign";
67     case GPG_EDITKEY_NRSIGN: return"nrsign";
68     case GPG_EDITKEY_NRLSIGN: return "nrlsign";
69     }
70     }
71     if( !strcmp( key, "sign_uid.class" ) ) {
72     sprintf( buf, "%d", ctx->sig_class );
73     return buf;
74     }
75     if (!strcmp (key, "sign_uid.expire"))
76     return "Y"; /* the sig expires when the key expires */
77     if (!strcmp (key, "siggen.valid"))
78     return ctx->exp_date;
79     if (!strcmp (key, "trustsig_prompt.trust_value")) {
80     sprintf (buf, "%d", ctx->trust_id);
81     return buf;
82     }
83     if (!strcmp (key, "trustsig_prompt.trust_depth"))
84     return ""; /* fixme */
85     if (!strcmp (key, "trustsig_prompt.trust_regexp"))
86     return ""; /* fixme */
87     if( !strcmp (key, "sign_uid.local_promote_okay" ) )
88     return "Y";
89     if( !strcmp (key, "sign_uid.okay" ) )
90     return "Y";
91     if( !strcmp (key, "keyedit.sign_all.okay" ) )
92     return "Y";
93     if( !strcmp ( key, "passphrase.enter" ) )
94     return ctx->pass;
95     if( !strcmp (key, "keyedit.prompt" ) ) {
96     ctx->cmd_sent = 0;
97     return "save";
98     }
99    
100     return NULL;
101     }
102    
103    
104     static const char*
105     cmd_trust_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
106     {
107     static char buf[4];
108    
109     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
110     ctx->cmd_sent = 1;
111     return "trust";
112     }
113     if (!strcmp (key, "edit_ownertrust.set_ultimate.okay"))
114     return "Y";
115     if (!strcmp (key, "edit_ownertrust.value" )) {
116     sprintf (buf, "%d", ctx->trust_id);
117     return buf;
118     }
119     if (!strcmp (key, "keyedit.prompt")) {
120     ctx->cmd_sent = 0;
121     return "save";
122     }
123    
124     return NULL;
125     }
126    
127    
128     static const char*
129     cmd_adduid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
130     {
131     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
132     ctx->cmd_sent = 1;
133     return "adduid";
134     }
135     if( !strcmp ( key, "keygen.name" ) )
136     return ctx->name;
137     if( !strcmp (key, "keygen.email" ) )
138     return ctx->email;
139     if( !strcmp ( key, "keygen.comment" ) ) {
140     if (ctx->cmt)
141     return ctx->cmt;
142     return "";
143     }
144     if( !strcmp (key, "passphrase.enter" ) )
145     return ctx->pass;
146     if( !strcmp (key, "keyedit.prompt" ) ) {
147     ctx->cmd_sent = 0;
148     return "save";
149     }
150    
151     return NULL;
152     }
153    
154    
155     static const char*
156     cmd_deluid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key,
157     int *r_step)
158     {
159     static char buf[64];
160     int step = *r_step;
161    
162     if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
163     sprintf (buf, "uid %d", ctx->uid_index);
164     *r_step = step = 1;
165     return buf;
166     }
167     if( step == 1 && !strcmp( key, "keyedit.prompt" ) ) {
168     *r_step = step = 2;
169     return "deluid";
170     }
171     if( step == 2 && !strcmp ( key, "keyedit.remove.uid.okay" ) ) {
172     *r_step = step = 3;
173     return "Y";
174     }
175     if( step == 3 && !strcmp ( key, "keyedit.prompt" ) ) {
176     *r_step = step = 0;
177     return "save";
178     }
179    
180     return NULL;
181     }
182    
183    
184     static const char *
185     cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key,
186     int * r_step)
187     {
188     static char buf[64];
189     int step = *r_step;
190    
191     if (step == 0 && !strcmp (key, "keyedit.prompt")) {
192     sprintf (buf, "uid %d", ctx->uid_index);
193     *r_step = step = 1;
194     return buf;
195     }
196     if (step == 1 && !strcmp (key, "keyedit.prompt")) {
197     *r_step = step = 2;
198     return "delsig";
199     }
200     if (!strcmp (key, "keyedit.delsig.unknown") ||
201     !strcmp (key, "keyedit.delsig.valid")) {
202     if (++ctx->cnt == ctx->sig_index)
203     return "Y";
204     else
205     return "N";
206     }
207     if (ctx->sig_index == 0 &&
208     !strcmp (key, "keyedit.delsig.selfsig"))
209     return "Y";
210     if (step == 2 && !strcmp (key, "keyedit.prompt")) {
211     *r_step = step = 0;
212     return "save";
213     }
214     return NULL;
215     }
216    
217    
218     static const char*
219     cmd_delkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
220     const char *key, int *r_step )
221     {
222     static char buf[64];
223     int step = *r_step;
224    
225     if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
226     sprintf( buf, "key %d", ctx->key_index);
227     *r_step = step = 1;
228     return buf;
229     }
230     if( step == 1 && !strcmp (key, "keyedit.prompt" ) ) {
231     *r_step = step = 2;
232     return "delkey";
233     }
234     if( step == 2 && !strcmp (key, "keyedit.remove.subkey.okay" ) ) {
235     *r_step = step = 3;
236     return "Y";
237     }
238     if( step == 3 && !strcmp (key, "keyedit.prompt" ) ) {
239     *r_step = step = 0;
240     return "save";
241     }
242    
243     return NULL;
244     }
245    
246    
247     static const char*
248     cmd_addkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
249     {
250     static char buf[64];
251    
252     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
253     ctx->cmd_sent = 1;
254     return "addkey";
255     }
256    
257     if( !strcmp (key, "passphrase.enter" ) )
258     return ctx->pass;
259     if( !strcmp (key, "keygen.algo" ) ) {
260     _snprintf( buf, sizeof buf-1, "%d", ctx->pubkey_algo);
261     return buf;
262     }
263     if( !strcmp (key, "keygen.size" ) ) {
264     _snprintf( buf, sizeof buf-1, "%d", ctx->pubkey_size);
265     return buf;
266     }
267     if( !strcmp (key, "keygen.valid" ) ) {
268     _snprintf( buf, sizeof buf-1, "%d", ctx->valid);
269     return buf;
270     }
271     if( !strcmp (key, "keyedit.prompt" ) ) {
272     ctx->cmd_sent = 0;
273     return "save";
274     }
275     return NULL;
276     }
277    
278    
279     static const char*
280     cmd_passwd_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
281     {
282     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
283     ctx->cmd_sent = 1;
284     return "passwd";
285     }
286     if( !strcmp (key, "passphrase.enter") && !ctx->cnt) {
287     ctx->cnt = 1;
288     return ctx->pass;
289     }
290     if( !strcmp (key, "passphrase.enter" ))
291     return ctx->new_pass;
292     if( !strcmp (key, "change_passwd.empty.okay" ))
293     return ctx->flags? "Y" : "N";
294     if( !strcmp ( key, "keyedit.prompt" ) ) {
295     ctx->cmd_sent = 0;
296     return "save";
297     }
298     return NULL;
299     }
300    
301    
302     static const char *
303     cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key)
304     {
305     static char buf[128];
306    
307     /* XXX: check the code. */
308     #if 0
309     if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
310     ctx->cmd_sent = 1;
311     return "";
312     }
313     if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 0) {
314     ctx->u.pref.id++;
315     _snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs);
316     return buf;
317     }
318     if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 1) {
319     ctx->u.pref.id++;
320     return "updpref";
321     }
322     if (!strcmp ( key, "keyedit.updpref.okay"))
323     return "Y";
324     if (!strcmp (key, "passphrase.enter"))
325     return ctx->u.pref.passwd;
326     if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) {
327     ctx->u.pref.id = 0;
328     return "save";
329     }
330     #endif
331     return NULL;
332     }
333    
334    
335     static const char*
336     cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
337     const char *key, int *r_step )
338     {
339     static char buf[64];
340     int step = *r_step;
341    
342     if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {
343     sprintf (buf, "uid %d", ctx->uid_index);
344     *r_step = step = 1;
345     return buf;
346     }
347     if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
348     *r_step = step = 2;
349     return "primary";
350     }
351     if( step == 2 && !strcmp ( key, "passphrase.enter" ) ) {
352     *r_step = step = 3;
353     return ctx->pass;
354     }
355     if( step == 3 && !strcmp (key, "keyedit.prompt" ) ) {
356     *r_step = step = 0;
357     return "save";
358     }
359     return NULL;
360     }
361    
362    
363     static const char*
364     cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
365     const char *key, int *r_step )
366     {
367     static char buf[64];
368     int step = *r_step;
369    
370     if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
371     sprintf(buf, "key %d", ctx->key_index);
372     *r_step = step = 1;
373     return buf;
374     }
375     if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
376     *r_step = step = 2;
377     return "expire";
378     }
379     if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {
380     *r_step = step = 3;
381     if (ctx->valid) {
382     sprintf (buf, "%d", ctx->valid);
383     return buf;
384     }
385     else
386     return ctx->exp_date;
387     }
388     if( step == 3 && !strcmp (key, "passphrase.enter" ) ) {
389     *r_step = step = 4;
390     return ctx->pass;
391     }
392     if( step == 4 && !strcmp ( key, "keyedit.prompt" ) ) {
393     *r_step = step = 0;
394     return "save";
395     }
396    
397     return NULL;
398     }
399    
400    
401     const char*
402     cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
403     const char *key, int *r_step)
404     {
405     static char buf[64];
406     int step = *r_step;
407    
408     if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
409     sprintf( buf, "uid %d", ctx->uid_index);
410     *r_step = step = 1;
411     return buf;
412     }
413     if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
414     *r_step = step = 2;
415     return "revsig";
416     }
417     if( step == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {
418     *r_step = step = 3;
419     return "Y";
420     }
421     if( step == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {
422     *r_step = step = 4;
423     return "Y";
424     }
425     if( step == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
426     *r_step = step = 5;
427     return "0";
428     }
429     if( step == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
430     *r_step = step = 6;
431     return "\n";
432     }
433     if( step == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
434     *r_step = step = 7;
435     return "Y";
436     }
437     if( step == 7 && !strcmp (key, "passphrase.enter" ) ) {
438     *r_step = step = 8;
439     return ctx->pass;
440     }
441     if( step == 8 && !strcmp ( key, "keyedit.prompt" ) ) {
442     *r_step = step = 0;
443     return "save";
444     }
445    
446     return NULL;
447     }
448    
449    
450     static const char *
451     cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
452     const char * key, int *r_step)
453     {
454     int step = *r_step;
455     static char buf[64];
456    
457     if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
458     sprintf( buf, "key %d", ctx->key_index);
459     *r_step = step = 1;
460     return buf;
461     }
462     if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
463     *r_step = step = 2;
464     return "revkey";
465     }
466     if( step == 2 && !strcmp (key, "keyedit.revoke.subkey.okay" ) ) {
467     *r_step = step = 3;
468     return "Y";
469     }
470     if( step == 3 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
471     sprintf( buf, "%d", ctx->reason);
472     *r_step = step = 4;
473     return buf;
474     }
475     if( step == 4 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
476     *r_step = step = 5;
477     return "";
478     }
479     if( step == 5 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
480     *r_step = step = 6;
481     return "Y";
482     }
483     if( step == 6 && !strcmp (key, "passphrase.enter" ) ) {
484     *r_step = step = 7;
485     return ctx->pass;
486     }
487     if( step == 7 && !strcmp ( key, "keyedit.prompt" ) ) {
488     *r_step = step = 0;
489     return "save";
490     }
491     return NULL;
492     }
493    
494    
495     static const char *
496     cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
497     const char * key, int * r_step)
498     {
499     int step = *r_step;
500    
501     if ((step == 0 /*|| c->result.editk->already_signed*/)
502     && !strcmp (key, "keyedit.add_revoker")) {
503     *r_step = step = 1;
504     /* XXX: handle already-signed. */
505     return ctx->name;
506     }
507     if( step == 1 && !strcmp (key, "keyedit.add_revoker.okay" ) ) {
508     *r_step = step = 2;
509     return "Y";
510     }
511     if( step == 2 && !strcmp (key, "passphrase.enter" ) ) {
512     *r_step = step = 3;
513     return ctx->pass;
514     }
515     if (step == 3 && !strcmp (key, "keyedit.prompt")) {
516     *r_step = step = 0;
517     return "save";
518     }
519     return NULL;
520     }
521    
522    
523     static const char*
524     cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key,
525     int *r_step )
526     {
527     int step = *r_step;
528    
529     if (!strcmp (key, "photoid.jpeg.add"))
530     return ctx->url;
531     if (!strcmp (key, "photoid.jpeg.size"))
532     return "Y";
533     if (!strcmp (key, "passphrase.enter"))
534     return ctx->pass;
535     if (!strcmp (key, "keyedit.prompt")) {
536     *r_step = step = 0;
537     return "save";
538     }
539     return NULL;
540     }
541    
542    
543     static const char *
544     cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
545     const char * key, int mode)
546     {
547     if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
548     ctx->cmd_sent = 1;
549     if (mode)
550     return "disable";
551     return "enable";
552     }
553     if (!strcmp (key, "keyedit.prompt")) {
554     ctx->cmd_sent = 0;
555     return "save";
556     }
557     return NULL;
558     }
559    
560    
561    
562    
563     static gpgme_error_t
564     editkey_command_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)
565     {
566     static int step = 0;
567     const char *out = NULL;
568     GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
569    
570     if (!code || !ke)
571     return gpg_error (GPG_ERR_INV_ARG);
572    
573     switch (ke->type) {
574     case GPG_EDITKEY_LSIGN:
575     case GPG_EDITKEY_SIGN:
576     case GPG_EDITKEY_NRSIGN:
577     case GPG_EDITKEY_TSIGN:
578     case GPG_EDITKEY_NRLSIGN:
579     out = cmd_sign_handler (ke, code, key);
580     break;
581    
582     case GPG_EDITKEY_TRUST:
583     out = cmd_trust_handler (ke, code, key);
584     break;
585    
586     case GPG_EDITKEY_ADDUID:
587     out = cmd_adduid_handler (ke, code, key);
588     break;
589    
590     case GPG_EDITKEY_DELUID:
591     out = cmd_deluid_handler (ke, code, key, &step);
592     break;
593    
594     case GPG_EDITKEY_DELSIG:
595     out = cmd_delsig_handler (ke, code, key, &step);
596     break;
597    
598     case GPG_EDITKEY_DELKEY:
599     out = cmd_delkey_handler(ke, code, key, &step );
600     break;
601    
602     case GPG_EDITKEY_ADDKEY:
603     out = cmd_addkey_handler (ke, code, key);
604     break;
605    
606     case GPG_EDITKEY_PASSWD:
607     out = cmd_passwd_handler (ke, code, key);
608     break;
609    
610     case GPG_EDITKEY_PRIMARY:
611     out = cmd_primary_handler (ke, code, key, &step);
612     break;
613    
614     case GPG_EDITKEY_EXPIRE:
615     out = cmd_expire_handler (ke, code, key, &step);
616     break;
617    
618     case GPG_EDITKEY_REVSIG:
619     out = cmd_revsig_handler (ke, code, key, &step);
620     break;
621    
622     case GPG_EDITKEY_REVKEY:
623     out = cmd_revkey_handler (ke, code, key, &step);
624     break;
625    
626     case GPG_EDITKEY_ADDREV:
627     out = cmd_addrev_handler (ke, code, key, &step);
628     break;
629    
630     case GPG_EDITKEY_ADDPHOTO:
631     out = cmd_addphoto_handler (ke, code, key, &step);
632     break;
633    
634     case GPG_EDITKEY_ENABLE:
635     case GPG_EDITKEY_DISABLE:
636     out = cmd_enable_disable_handler (ke, code, key,
637     ke->type == GPG_EDITKEY_DISABLE? 1: 0);
638     break;
639    
640     case GPG_EDITKEY_SETPREF:
641     out = cmd_setpref_handler (ke, code, key);
642     break;
643    
644     case GPG_EDITKEY_KEYSERV:
645     out = cmd_keyserv_handler (ke, code, key);
646     break;
647     }
648     if (out != NULL) {
649     write (fd, out, strlen (out));
650     write (fd, "\n", 1);
651     }
652     return 0;
653     }
654    
655    
656    
657     gpgme_error_t
658     gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
659     {
660     gpgme_error_t err;
661    
662     err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, NULL);
663     return err;
664     }
665    

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26