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

Diff of /trunk/Src/wptKeyEditCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.23  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26