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

Diff of /trunk/Src/wptKeyEditCB.cpp

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26