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

Legend:
Removed from v.24  
changed lines
  Added in v.160

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26