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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26