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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26