/[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 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
1  /* wptKeyEditCB.cpp - Key edit callback handling  /* wptKeyEditCB.cpp - Key edit callback handling
2   *      Copyright (C) 2005 Timo Schulz   *      Copyright (C) 2005 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      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) {      static char buf[128];
322          ctx->u.pref.id++;  
323          _snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs);      /* XXX: check the code. */
324          return buf;  #if 0
325      }      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
326      if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 1) {          ctx->cmd_sent = 1;
327          ctx->u.pref.id++;          return "";
328          return "updpref";      }
329      }      if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 0) {
330      if (!strcmp ( key, "keyedit.updpref.okay"))          ctx->u.pref.id++;
331          return "Y";          _snprintf (buf, sizeof buf-1, "setpref %s", ctx->u.pref.new_prefs);
332      if (!strcmp (key, "passphrase.enter"))          return buf;
333          return ctx->u.pref.passwd;      }
334      if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) {      if (!strcmp ( key, "keyedit.prompt") && ctx->u.pref.id == 1) {
335          ctx->u.pref.id = 0;          ctx->u.pref.id++;
336          ctx->reset ();          return "updpref";
337          return "save";      }
338      }      if (!strcmp ( key, "keyedit.updpref.okay"))
339  #endif          return "Y";
340      return NULL;      if (!strcmp (key, "passphrase.enter"))
341  }          return ctx->u.pref.passwd;
342        if (!strcmp (key, "keyedit.prompt") && ctx->u.pref.id == 2) {
343            ctx->u.pref.id = 0;
344  static const char*          ctx->reset ();
345  cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,          return "save";
346                       const char *key, int *r_step )      }
347  {  #endif
348      static char buf[64];      return NULL;
349      int step = *r_step;  }
350    
351      if( step == 0 && !strcmp (key, "keyedit.prompt" ) ) {  
352          sprintf (buf, "uid %d", ctx->uid_index);  /* 'primary' command handler. */
353          *r_step = step = 1;  static const char*
354          return buf;  cmd_primary_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
355      }                       const char *key, int *r_step )
356      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {  {
357          *r_step = step = 2;      static char buf[64];
358          return "primary";      int step = *r_step;
359      }  
360      if( step == 2 && !strcmp ( key, "passphrase.enter" ) ) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
361          *r_step = step = 3;          sprintf (buf, "uid %d", ctx->uid_index);
362          return ctx->pass;          *r_step = step = 1;
363      }          return buf;
364      if( step == 3 && !strcmp (key, "keyedit.prompt" ) ) {      }
365          *r_step = step = 0;      if (step == 1 && !strcmp (key, "keyedit.prompt" )) {
366          ctx->reset ();          *r_step = step = 2;
367          return "save";          return "primary";
368      }      }
369      return NULL;      if (step == 2 && !strcmp (key, "passphrase.enter")) {
370  }          *r_step = step = 3;
371            return ctx->pass;
372        }
373  static const char*      if (step == 3 && !strcmp (key, "keyedit.prompt")) {
374  cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,          *r_step = step = 0;
375                      const char *key, int *r_step )          ctx->reset ();
376  {          return "save";
377      static char buf[64];      }
378      int step = *r_step;      return NULL;
379    }
380      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {  
381          sprintf(buf, "key %d", ctx->key_index);  /* 'expire' command handler. */
382          *r_step = step = 1;  static const char*
383          return buf;  cmd_expire_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
384      }                      const char *key, int *r_step )
385      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {  {
386          *r_step = step = 2;      static char buf[64];
387          return "expire";      int step = *r_step;
388      }  
389      if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
390          *r_step = step = 3;          sprintf (buf, "key %d", ctx->key_index);
391          if (ctx->valid) {                  *r_step = step = 1;
392              sprintf (buf, "%d", ctx->valid);          return buf;
393              return buf;      }
394          }      if (step == 1 && !strcmp (key, "keyedit.prompt")) {
395          else          *r_step = step = 2;
396              return ctx->exp_date;          return "expire";
397      }      }
398      if( step == 3 && !strcmp (key, "passphrase.enter" ) ) {      if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {
399          *r_step = step = 4;          *r_step = step = 3;
400          return ctx->pass;          sprintf (buf, "%d", ctx->valid);
401      }          return buf;
402      if( step == 4 && !strcmp ( key, "keyedit.prompt" ) ) {      }
403          *r_step = step = 0;      if (step == 3 && !strcmp (key, "passphrase.enter")) {
404          ctx->reset ();          *r_step = step = 4;
405          return "save";          return ctx->pass;
406      }      }
407            if (step == 4 && !strcmp (key, "keyedit.prompt")) {
408      return NULL;          *r_step = step = 0;
409  }          ctx->reset ();
410            return "save";
411        }
412  const char*      
413  cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,      return NULL;
414                      const char *key, int *r_step)  }
415  {  
416      static char buf[64];  
417      int step = *r_step;  /* 'revuid' command handler. */
418        const char*
419      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {  cmd_revuid_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
420          sprintf( buf, "uid %d", ctx->uid_index);                      const char *key, int *r_step)
421          *r_step = step = 1;  {
422          return buf;      static char buf[32];
423      }      int step = *r_step;
424      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {      
425          *r_step = step = 2;      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
426          return "revsig";          *r_step = step = 1;
427      }          sprintf (buf, "uid %d", ctx->uid_index);
428      if( step == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {          return buf;
429          *r_step = step = 3;      }
430          return "Y";      else if (step == 1 && !strcmp (key, "keyedit.prompt")) {
431      }          *r_step = step = 2;
432      if( step == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {          return "revuid";
433          *r_step = step = 4;      }
434          return "Y";      else if (step == 2 && !strcmp (key, "keyedit.revoke.uid.okay")) {
435      }          *r_step = step = 3;
436      if( step == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {          return "Y";
437          *r_step = step = 5;      }
438          return "0";      else if (step == 3 && !strcmp (key, "ask_revocation_reason.code")) {
439      }          *r_step = step = 4;
440      if( step == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {          return "4";
441          *r_step = step = 6;      }
442          return "\n";      else if (step == 4 && !strcmp (key, "ask_revocation_reason.text")) {
443      }          *r_step = step = 5;
444      if( step == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {          return "";
445          *r_step = step = 7;      }
446          return "Y";      else if (step == 5 && !strcmp (key, "ask_revocation_reason.okay")) {
447      }          *r_step = step = 6;
448      if( step == 7 && !strcmp (key, "passphrase.enter" ) ) {          return "Y";
449          *r_step = step = 8;      }
450          return ctx->pass;      else if (step == 6 && !strcmp (key, "passphrase.enter")) {
451      }          *r_step = step = 7;
452      if( step == 8 && !strcmp ( key, "keyedit.prompt" ) ) {          return ctx->pass;
453          *r_step = step = 0;      }
454          ctx->reset ();      else if (step == 7 && !strcmp (key, "keyedit.prompt")) {
455          return "save";          *r_step = step = 0;
456      }          ctx->reset ();
457                return "save";
458      return NULL;      }
459  }      return NULL;
460    }
461    
462  static const char *  
463  cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,  const char*
464                      const char * key, int *r_step)  cmd_revsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
465  {                      const char *key, int *r_step)
466      int step = *r_step;  {
467      static char buf[64];      static char buf[64];
468        int step = *r_step;
469      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {      
470          sprintf( buf, "key %d", ctx->key_index);      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
471          *r_step = step = 1;          sprintf( buf, "uid %d", ctx->uid_index);
472          return buf;          *r_step = step = 1;
473      }          return buf;
474      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {      }
475          *r_step = step = 2;      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
476          return "revkey";          *r_step = step = 2;
477      }          return "revsig";
478      if( step == 2 && !strcmp (key, "keyedit.revoke.subkey.okay" ) ) {      }
479          *r_step = step = 3;      if( step == 2 && !strcmp (key, "ask_revoke_sig.one" ) ) {
480          return "Y";          *r_step = step = 3;
481      }          return "Y";
482      if( step == 3 &&  !strcmp ( key, "ask_revocation_reason.code" ) ) {      }
483          sprintf( buf, "%d", ctx->reason);      if( step == 3 && !strcmp (key, "ask_revoke_sig.okay" ) ) {
484          *r_step = step = 4;          *r_step = step = 4;
485          return buf;          return "Y";
486      }      }
487      if( step == 4 && !strcmp ( key, "ask_revocation_reason.text" ) ) {      if( step == 4 && !strcmp ( key, "ask_revocation_reason.code" ) ) {
488          *r_step = step = 5;          *r_step = step = 5;
489          return "";          return "0";
490      }      }
491      if( step == 5 && !strcmp (key, "ask_revocation_reason.okay" ) ) {      if( step == 5 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
492          *r_step = step = 6;          *r_step = step = 6;
493          return "Y";          return "";
494      }      }
495      if( step == 6 && !strcmp (key, "passphrase.enter" ) ) {      if( step == 6 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
496          *r_step = step = 7;          *r_step = step = 7;
497          return ctx->pass;          return "Y";
498      }      }
499      if( step == 7 && !strcmp ( key, "keyedit.prompt" ) ) {      if( step == 7 && !strcmp (key, "passphrase.enter" ) ) {
500          *r_step = step = 0;          *r_step = step = 8;
501          ctx->reset ();          return ctx->pass;
502          return "save";      }
503      }      if( step == 8 && !strcmp ( key, "keyedit.prompt" ) ) {
504      return NULL;          *r_step = step = 0;
505  }          ctx->reset ();
506            return "save";
507        }
508  static const char *      
509  cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,      return NULL;
510                      const char * key, int * r_step)  }
511  {  
512      int step = *r_step;  
513    /* 'revoke' command handler. */
514      if ((step == 0 /*|| c->result.editk->already_signed*/)  static const char *
515          && !strcmp (key, "keyedit.add_revoker")) {  cmd_revkey_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
516          *r_step = step = 1;                      const char * key, int *r_step)
517          /* XXX: handle already-signed. */  {
518          return ctx->name;      int step = *r_step;
519      }      static char buf[64];
520      if( step == 1 && !strcmp (key, "keyedit.add_revoker.okay" ) ) {  
521          *r_step = step = 2;      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
522          return "Y";          sprintf( buf, "key %d", ctx->key_index);
523      }          *r_step = step = 1;
524      if( step == 2 && !strcmp (key, "passphrase.enter" ) ) {          return buf;
525          *r_step = step = 3;      }
526          return ctx->pass;      if( step == 1 && !strcmp ( key, "keyedit.prompt" ) ) {
527      }          *r_step = step = 2;
528      if (step == 3 && !strcmp (key, "keyedit.prompt")) {          return "revkey";
529          *r_step = step = 0;      }
530          ctx->reset ();      if( step == 2 && !strcmp (key, "keyedit.revoke.subkey.okay" ) ) {
531          return "save";          *r_step = step = 3;
532      }          return "Y";
533      return NULL;      }
534  }      if( step == 3 &&  !strcmp ( key, "ask_revocation_reason.code" ) ) {
535            sprintf( buf, "%d", ctx->reason);
536            *r_step = step = 4;
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 == 4 && !strcmp ( key, "ask_revocation_reason.text" ) ) {
540  {          *r_step = step = 5;
541      int step = *r_step;          return "";
542        }
543      if (!strcmp (key, "photoid.jpeg.add"))      if( step == 5 && !strcmp (key, "ask_revocation_reason.okay" ) ) {
544          return ctx->url;          *r_step = step = 6;
545      if (!strcmp (key, "photoid.jpeg.size"))          return "Y";
546          return "Y";      }
547      if (!strcmp (key, "passphrase.enter"))      if( step == 6 && !strcmp (key, "passphrase.enter" ) ) {
548          return ctx->pass;          *r_step = step = 7;
549      if (!strcmp (key, "keyedit.prompt")) {          return ctx->pass;
550          *r_step = step = 0;      }
551          ctx->reset ();      if( step == 7 && !strcmp ( key, "keyedit.prompt" ) ) {
552          return "save";          *r_step = step = 0;
553      }          ctx->reset ();
554      return NULL;          return "save";
555  }      }
556        return NULL;
557    }
558  static const char *  
559  cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,  
560                              const char * key, int mode)  /* 'addrevoker' command handler. */
561  {  static const char *
562      if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {  cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
563          ctx->cmd_sent = 1;                      const char * key, int * r_step)
564          if (mode)  {
565              return "disable";      int step = *r_step;
566          return "enable";  
567      }      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
568      if (!strcmp (key, "keyedit.prompt")) {          *r_step = step = 1;
569          ctx->reset ();          return "addrevoker";
570          return "save";      }
571      }      if (step == 1 && !strcmp (key, "keyedit.add_revoker")) {
572      return NULL;          *r_step = step = 2;
573  }          /* XXX: handle already-signed. */
574            return ctx->name;
575        }
576        if (step == 2 && !strcmp (key, "keyedit.add_revoker.okay")) {
577            *r_step = step = 3;
578  static gpgme_error_t                              return "Y";
579  editkey_command_handler (void *opaque, gpgme_status_code_t code, const char *key, int fd)      }
580  {      if (step == 3 && !strcmp (key, "passphrase.enter")) {
581      static int step = 0;          *r_step = step = 4;
582      const char *out = NULL;          return ctx->pass;
583      GpgKeyEdit *ke = (GpgKeyEdit *)opaque;      }
584      HANDLE hd = (HANDLE)fd;      if (step == 4 && !strcmp (key, "keyedit.prompt")) {
585      DWORD n;          *r_step = step = 0;
586                ctx->reset ();
587      if (!ke)          return "save";
588          return gpg_error (GPG_ERR_INV_ARG);      }
589        
590      switch (ke->type) {      return NULL;
591      case GPG_EDITKEY_LSIGN:  }
592      case GPG_EDITKEY_SIGN:  
593      case GPG_EDITKEY_NRSIGN:  
594      case GPG_EDITKEY_TSIGN:  /* 'addphoto' command handler. */
595      case GPG_EDITKEY_NRLSIGN:  static const char*
596          out = cmd_sign_handler (ke, code, key);  cmd_addphoto_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
597          break;  {
598                if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
599      case GPG_EDITKEY_TRUST:          ctx->cmd_sent = 1;
600          out =  cmd_trust_handler (ke, code, key);          return "addphoto";
601          log_debug ("key=%s out=%s\n", key, out);      }
602          break;      if (!strcmp (key, "photoid.jpeg.add"))
603                    return ctx->url;
604      case GPG_EDITKEY_ADDUID:      if (!strcmp (key, "photoid.jpeg.size"))
605          out =  cmd_adduid_handler (ke, code, key);          return "Y";
606          break;      if (!strcmp (key, "passphrase.enter"))
607                    return ctx->pass;
608      case GPG_EDITKEY_DELUID:      if (!strcmp (key, "keyedit.prompt")) {      
609          out =  cmd_deluid_handler (ke, code, key, &step);          ctx->reset ();
610          break;          return "save";
611                }
612      case GPG_EDITKEY_DELSIG:      return NULL;
613          out =  cmd_delsig_handler (ke, code, key, &step);  }
614          break;  
615    /* 'enable' and 'disable' command handler. */
616      case GPG_EDITKEY_DELKEY:  static const char *
617          out =  cmd_delkey_handler(ke, code, key, &step );  cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
618          break;                              const char * key, int mode)
619            {
620      case GPG_EDITKEY_ADDKEY:      if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
621          out =  cmd_addkey_handler (ke, code, key);          ctx->cmd_sent = 1;
622          break;          if (mode)
623                        return "disable";
624      case GPG_EDITKEY_PASSWD:          return "enable";
625          out =  cmd_passwd_handler (ke, code, key);      }
626          break;      if (!strcmp (key, "keyedit.prompt")) {
627                    ctx->reset ();
628      case GPG_EDITKEY_PRIMARY:          return "save";
629          out =  cmd_primary_handler (ke, code, key, &step);      }
630          break;      return NULL;
631            }
632      case GPG_EDITKEY_EXPIRE:  
633          out =  cmd_expire_handler (ke, code, key, &step);  
634          break;  /* edit key dispatch handler. */
635    static gpgme_error_t                    
636      case GPG_EDITKEY_REVSIG:  editkey_command_handler (void *opaque, gpgme_status_code_t code,
637          out =  cmd_revsig_handler (ke, code, key, &step);                           const char *key, int fd)
638          break;  {
639        static int step = 0;
640      case GPG_EDITKEY_REVKEY:      const char *out = NULL;
641          out =  cmd_revkey_handler (ke, code, key, &step);      GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
642          break;      HANDLE hd = (HANDLE)fd;
643        DWORD n;
644      case GPG_EDITKEY_ADDREV:      
645          out =  cmd_addrev_handler (ke, code, key, &step);      if (!ke)
646          break;          return gpg_error (GPG_ERR_INV_ARG);
647    
648      case GPG_EDITKEY_ADDPHOTO:      /*log_debug ("key=%s code=%d\r\n", key, code);*/
649          out =  cmd_addphoto_handler (ke, code, key, &step);      switch (code) {
650          break;      case GPGME_STATUS_ALREADY_SIGNED:
651            ke->setResult (GPG_EDITRES_ALREADY_SIGNED);
652      case GPG_EDITKEY_ENABLE:          break;
653      case GPG_EDITKEY_DISABLE:      case GPGME_STATUS_BAD_PASSPHRASE:
654          out =  cmd_enable_disable_handler (ke, code, key,          ke->setResult (GPG_EDITRES_BAD_PASSPHRASE);
655                                             ke->type == GPG_EDITKEY_DISABLE? 1: 0);          break;
656          break;      }
657    
658      case GPG_EDITKEY_SETPREF:      if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) {
659          out =  cmd_setpref_handler (ke, code, key);          /* If the entered passphrase is bad, we supply empty
660          break;             passphrase to abort and send 'quit' as soon as possible. */
661            if (!strcmp (key, "passphrase.enter"))
662      case GPG_EDITKEY_KEYSERV:              WriteFile (hd, "\n", 1, &n, NULL);
663          out =  cmd_keyserv_handler (ke, code, key);          if (!strcmp (key, "keyedit.prompt"))
664          break;              WriteFile (hd, "quit\n", 5, &n, NULL);
665      }      }
666      if (out != NULL) {      
667          WriteFile (hd, out, strlen (out), &n, NULL);          switch (ke->getType ()) {
668          WriteFile (hd, "\n", 1, &n, NULL);      case GPG_EDITKEY_LSIGN:
669          /* XXX: check return codes. */      case GPG_EDITKEY_SIGN:
670      }      case GPG_EDITKEY_NRSIGN:
671      return 0;      case GPG_EDITKEY_TSIGN:
672  }      case GPG_EDITKEY_NRLSIGN:
673            out = cmd_sign_handler (ke, code, key);
674            break;
675            
676  gpgme_error_t      case GPG_EDITKEY_TRUST:
677  gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)          out =  cmd_trust_handler (ke, code, key);
678  {          break;
679      gpgme_error_t err;          
680      gpgme_data_t out;      case GPG_EDITKEY_ADDUID:
681              out =  cmd_adduid_handler (ke, code, key);
682      err = gpgme_data_new (&out);          break;
683      if (err)          
684          return err;      case GPG_EDITKEY_DELUID:
685      err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);              out =  cmd_deluid_handler (ke, code, key, &step);
686            break;
687      gpgme_data_release (out);          
688      return err;      case GPG_EDITKEY_DELSIG:
689  }          out =  cmd_delsig_handler (ke, code, key, &step);
690            break;
691    
692        case GPG_EDITKEY_DELKEY:
693            out =  cmd_delkey_handler(ke, code, key, &step );
694            break;
695            
696        case GPG_EDITKEY_ADDKEY:
697            out =  cmd_addkey_handler (ke, code, key);
698            break;
699            
700        case GPG_EDITKEY_PASSWD:
701            out =  cmd_passwd_handler (ke, code, key);
702            break;
703            
704        case GPG_EDITKEY_PRIMARY:
705            out =  cmd_primary_handler (ke, code, key, &step);
706            break;
707            
708        case GPG_EDITKEY_EXPIRE:
709            out =  cmd_expire_handler (ke, code, key, &step);
710            break;
711    
712        case GPG_EDITKEY_REVSIG:
713            out =  cmd_revsig_handler (ke, code, key, &step);
714            break;
715    
716        case GPG_EDITKEY_REVKEY:
717            out =  cmd_revkey_handler (ke, code, key, &step);
718            break;
719                
720        case GPG_EDITKEY_REVUID:
721            out = cmd_revuid_handler (ke, code, key, &step);
722            break;
723    
724        case GPG_EDITKEY_ADDREV:
725            out =  cmd_addrev_handler (ke, code, key, &step);
726            break;
727    
728        case GPG_EDITKEY_ADDPHOTO:
729            out =  cmd_addphoto_handler (ke, code, key);
730            break;
731    
732        case GPG_EDITKEY_ENABLE:
733        case GPG_EDITKEY_DISABLE:
734            out = cmd_enable_disable_handler (ke, code, key,
735                                              ke->getType () == GPG_EDITKEY_DISABLE? 1: 0);
736            break;
737    
738        case GPG_EDITKEY_SETPREF:
739            out =  cmd_setpref_handler (ke, code, key);
740            break;
741    
742        case GPG_EDITKEY_KEYSERV:
743            out =  cmd_keyserv_handler (ke, code, key);
744            break;
745        }
746    
747        if (out != NULL) {
748            /* XXX: check return codes. */
749            WriteFile (hd, out, strlen (out), &n, NULL);
750            WriteFile (hd, "\n", 1, &n, NULL);
751        }
752        return 0;
753    }
754    
755    
756    /* Check if a GPG status code occured which marks the
757       current operation as failed.
758       Return value: gpg error constant. */
759    static gpgme_error_t
760    map_result (GpgKeyEdit *ke)
761    {
762        if (!ke->getResult ())
763            return gpg_error (GPG_ERR_NO_ERROR);
764        if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE)
765            return gpg_error (GPG_ERR_BAD_PASSPHRASE);
766        return 0;
767    }
768    
769    
770    /* Wrapper around the gpgme edit interface.
771       @ctx context to use.
772       @key key on which the operation should be performed.
773       @ek  key edit context.
774       Return value: 0 on success. */
775    gpgme_error_t
776    gpg_editkey (gpgme_ctx_t ctx, gpgme_key_t key, GpgKeyEdit *ek)
777    {
778        gpgme_error_t err;
779        gpgme_data_t out;
780      
781        err = gpgme_data_new (&out);
782        if (err)
783            return err;
784        err = gpgme_op_edit (ctx, key, editkey_command_handler, ek, out);
785        if (!err)
786            err = map_result (ek);
787    
788        gpgme_data_release (out);
789        return err;
790    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26