/[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 101 by twoaday, Fri Nov 25 10:56:05 2005 UTC revision 181 by twoaday, Tue Mar 14 11:01:22 2006 UTC
# Line 18  Line 18 
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    
22  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
23  #include <config.h>  #include <config.h>
24  #endif  #endif
25    
26  #include <stdio.h>  #include <stdio.h>
 #include <stdio.h>  
27  #include <string.h>  #include <string.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <assert.h>  #include <assert.h>
# Line 35  Line 35 
35  #include "wptKeyEdit.h"  #include "wptKeyEdit.h"
36  #include "wptErrors.h"  #include "wptErrors.h"
37    
38    /* Possible errors for the edit key operation. */
39    enum editkey_error_t {
40        EDITKEY_ERR_ALREADY_SIGNED = 1,
41        EDITKEY_ERR_BAD_PASSPHRASE = 2
42    };
43    
44  /* 'keyserver' command handler. */  /* 'keyserver' command handler. */
45  static const char*  static const char*
# Line 43  cmd_keyserv_handler (GpgKeyEdit *ctx, gp Line 48  cmd_keyserv_handler (GpgKeyEdit *ctx, gp
48      static char buf[32];      static char buf[32];
49    
50      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
51            int uid = ctx->getUseridIndex ();
52          ctx->cmd_sent = 1;          ctx->cmd_sent = 1;
53          sprintf (buf, "uid %d", ctx->uid_index);          if (uid != -1) {
54          return buf;              sprintf (buf, "uid %d", ctx->getUseridIndex ());
55                return buf;
56            }
57      }      }
58      if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) {      if (!strcmp (key, "keyedit.prompt") && !ctx->cnt) {
59          ctx->cnt = 1;          ctx->cnt = 1;
# Line 53  cmd_keyserv_handler (GpgKeyEdit *ctx, gp Line 61  cmd_keyserv_handler (GpgKeyEdit *ctx, gp
61      }      }
62      if (!strcmp (key, "keyedit.add_keyserver"))      if (!strcmp (key, "keyedit.add_keyserver"))
63          return ctx->url;          return ctx->url;
64      if ( !strcmp (key, "keyedit.confirm_keyserver"))      if (!strcmp (key, "keyedit.confirm_keyserver"))
65          return "Y";          return "Y";
66      if (!strcmp (key, "passphrase.enter"))      if (!strcmp (key, "passphrase.enter"))
67          return ctx->pass;          return ctx->pass;
# Line 66  cmd_keyserv_handler (GpgKeyEdit *ctx, gp Line 74  cmd_keyserv_handler (GpgKeyEdit *ctx, gp
74  }  }
75    
76    
77    /* 'sign' command handler. */
78  static const char*  static const char*
79  cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)  cmd_sign_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
80  {  {
81      static char buf[32];      static char buf[32];
82    
83        if (ctx->getUseridIndex () != -1 &&
84            ctx->cnt == 0 && !strcmp (key, "keyedit.prompt")) {
85            ctx->cnt++;
86            sprintf (buf, "uid %d", ctx->getUseridIndex ());
87            return buf;
88        }
89    
90      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
91          ctx->cmd_sent = 1;          ctx->cmd_sent = 1;
92          switch (ctx->getType ()) {          switch (ctx->getType ()) {
# Line 171  cmd_deluid_handler (GpgKeyEdit *ctx, gpg Line 187  cmd_deluid_handler (GpgKeyEdit *ctx, gpg
187      int step = *r_step;      int step = *r_step;
188    
189      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
190          sprintf (buf, "uid %d", ctx->uid_index);          sprintf (buf, "uid %d", ctx->getUseridIndex ());
191          *r_step = step = 1;          *r_step = step = 1;
192          return buf;          return buf;
193      }      }
# Line 193  cmd_deluid_handler (GpgKeyEdit *ctx, gpg Line 209  cmd_deluid_handler (GpgKeyEdit *ctx, gpg
209  }  }
210    
211    
212  static const char *  static const char*
213  cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key,  cmd_delsig_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key,
214                      int * r_step)                      int *r_step)
215  {  {
216      static char buf[64];      static char buf[64];
217      int step = *r_step;      int step = *r_step;
218    
219      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
220          sprintf (buf, "uid %d", ctx->uid_index);          sprintf (buf, "uid %d", ctx->getUseridIndex ());
221          *r_step = step = 1;          *r_step = step = 1;
222          return buf;          return buf;
223      }      }
# Line 211  cmd_delsig_handler (GpgKeyEdit *ctx, gpg Line 227  cmd_delsig_handler (GpgKeyEdit *ctx, gpg
227      }      }
228      if (!strcmp (key, "keyedit.delsig.unknown") ||      if (!strcmp (key, "keyedit.delsig.unknown") ||
229          !strcmp (key, "keyedit.delsig.valid")) {          !strcmp (key, "keyedit.delsig.valid")) {
230          if (++ctx->cnt == ctx->sig_index)          if (++ctx->cnt == ctx->getSigIndex ())
231              return "Y";              return "Y";
232          else          else
233              return "N";              return "N";
234      }      }
235      if (ctx->sig_index == 0 &&      if (!strcmp (key, "keyedit.delsig.selfsig"))
         !strcmp (key, "keyedit.delsig.selfsig"))  
236          return "Y";          return "Y";
237      if (step == 2 && !strcmp (key, "keyedit.prompt")) {      if (step == 2 && !strcmp (key, "keyedit.prompt")) {
238          *r_step = step = 0;          *r_step = step = 0;
# Line 237  cmd_delkey_handler (GpgKeyEdit *ctx, gpg Line 252  cmd_delkey_handler (GpgKeyEdit *ctx, gpg
252      int step = *r_step;      int step = *r_step;
253    
254      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
255          sprintf (buf, "key %d", ctx->key_index);          sprintf (buf, "key %d", ctx->getKeyIndex ());
256          *r_step = step = 1;          *r_step = step = 1;
257          return buf;          return buf;
258      }      }
# Line 273  cmd_addkey_handler (GpgKeyEdit *ctx, gpg Line 288  cmd_addkey_handler (GpgKeyEdit *ctx, gpg
288      if (!strcmp (key, "passphrase.enter"))      if (!strcmp (key, "passphrase.enter"))
289          return ctx->pass;          return ctx->pass;
290      if (!strcmp (key, "keygen.algo")) {      if (!strcmp (key, "keygen.algo")) {
291          _snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_algo);          _snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_algo);
292          return buf;          return buf;
293      }        }  
294      if (!strcmp (key, "keygen.size")) {      if (!strcmp (key, "keygen.size")) {
295          _snprintf (buf, sizeof buf-1, "%d", ctx->pubkey_size);          _snprintf (buf, sizeof (buf)-1, "%d", ctx->pubkey_size);
296          return buf;          return buf;
297      }      }
298      if (!strcmp (key, "keygen.valid")) {      if (!strcmp (key, "keygen.valid")) {
299          _snprintf (buf, sizeof buf-1, "%d", ctx->valid);          _snprintf (buf, sizeof (buf)-1, "%d", ctx->getValidDays ());
300          return buf;                  return buf;        
301      }      }
302      if (!strcmp (key, "keyedit.prompt")) {      if (!strcmp (key, "keyedit.prompt")) {
# Line 299  cmd_passwd_handler (GpgKeyEdit *ctx, gpg Line 314  cmd_passwd_handler (GpgKeyEdit *ctx, gpg
314          ctx->cmd_sent = 1;          ctx->cmd_sent = 1;
315          return "passwd";          return "passwd";
316      }      }
317      if( !strcmp (key, "passphrase.enter") && !ctx->cnt) {      if (!ctx->cnt && !strcmp (key, "passphrase.enter")) {
318          ctx->cnt = 1;          ctx->cnt = 1;
319          return ctx->pass;          return ctx->pass;
320      }      }
321      if( !strcmp (key, "passphrase.enter" ))      if (!strcmp (key, "passphrase.enter" ))
322          return ctx->new_pass;          return ctx->new_pass;
323      if( !strcmp (key, "change_passwd.empty.okay" ))      if (!strcmp (key, "change_passwd.empty.okay" ))
324          return ctx->flags?  "Y" : "N";          return ctx->flags?  "Y" : "N";
325      if (!strcmp (key, "keyedit.prompt")) {      if (!strcmp (key, "keyedit.prompt")) {
326          ctx->reset ();          ctx->reset ();
327          return "save";          return "save";
328      }      }
329    
330      return NULL;      return NULL;
331  }  }
332    
# Line 318  cmd_passwd_handler (GpgKeyEdit *ctx, gpg Line 334  cmd_passwd_handler (GpgKeyEdit *ctx, gpg
334  static const char*  static const char*
335  cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key)  cmd_setpref_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char * key)
336  {  {
337        static char buf[128];
338    
339      /* XXX: check the code. */      /* XXX: check the code. */
340  #if 0  #if 0
     static char buf[128];  
341      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {      if (!ctx->cmd_sent && !strcmp (key, "keyedit.prompt")) {
342          ctx->cmd_sent = 1;          ctx->cmd_sent = 1;
343          return "";          return "";
# Line 357  cmd_primary_handler (GpgKeyEdit *ctx, gp Line 374  cmd_primary_handler (GpgKeyEdit *ctx, gp
374      int step = *r_step;      int step = *r_step;
375    
376      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
377          sprintf (buf, "uid %d", ctx->uid_index);          sprintf (buf, "uid %d", ctx->getUseridIndex ());
378          *r_step = step = 1;          *r_step = step = 1;
379          return buf;          return buf;
380      }      }
# Line 386  cmd_expire_handler (GpgKeyEdit *ctx, gpg Line 403  cmd_expire_handler (GpgKeyEdit *ctx, gpg
403      int step = *r_step;      int step = *r_step;
404    
405      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
406          sprintf (buf, "key %d", ctx->key_index);          sprintf (buf, "key %d", ctx->getKeyIndex ());
407          *r_step = step = 1;          *r_step = step = 1;
408          return buf;          return buf;
409      }      }
# Line 396  cmd_expire_handler (GpgKeyEdit *ctx, gpg Line 413  cmd_expire_handler (GpgKeyEdit *ctx, gpg
413      }      }
414      if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {      if( step == 2 && !strcmp ( key, "keygen.valid" ) ) {
415          *r_step = step = 3;          *r_step = step = 3;
416          sprintf (buf, "%d", ctx->valid);          sprintf (buf, "%d", ctx->getValidDays ());
417          return buf;          return buf;
418      }      }
419      if (step == 3 && !strcmp (key, "passphrase.enter")) {      if (step == 3 && !strcmp (key, "passphrase.enter")) {
# Line 423  cmd_revuid_handler (GpgKeyEdit *ctx, gpg Line 440  cmd_revuid_handler (GpgKeyEdit *ctx, gpg
440            
441      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
442          *r_step = step = 1;          *r_step = step = 1;
443          sprintf (buf, "uid %d", ctx->uid_index);          sprintf (buf, "uid %d", ctx->getUseridIndex ());
444          return buf;          return buf;
445      }      }
446      else if (step == 1 && !strcmp (key, "keyedit.prompt")) {      else if (step == 1 && !strcmp (key, "keyedit.prompt")) {
# Line 467  cmd_revsig_handler (GpgKeyEdit *ctx, gpg Line 484  cmd_revsig_handler (GpgKeyEdit *ctx, gpg
484      int step = *r_step;      int step = *r_step;
485            
486      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
487          sprintf( buf, "uid %d", ctx->uid_index);          sprintf( buf, "uid %d", ctx->getUseridIndex ());
488          *r_step = step = 1;          *r_step = step = 1;
489          return buf;          return buf;
490      }      }
# Line 518  cmd_revkey_handler (GpgKeyEdit *ctx, gpg Line 535  cmd_revkey_handler (GpgKeyEdit *ctx, gpg
535      static char buf[64];      static char buf[64];
536    
537      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {      if( step == 0 && !strcmp ( key, "keyedit.prompt" ) ) {
538          sprintf( buf, "key %d", ctx->key_index);          sprintf( buf, "key %d", ctx->getKeyIndex ());
539          *r_step = step = 1;          *r_step = step = 1;
540          return buf;          return buf;
541      }      }
# Line 559  cmd_revkey_handler (GpgKeyEdit *ctx, gpg Line 576  cmd_revkey_handler (GpgKeyEdit *ctx, gpg
576  /* 'addrevoker' command handler. */  /* 'addrevoker' command handler. */
577  static const char *  static const char *
578  cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,  cmd_addrev_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
579                      const char * key, int * r_step)                      const char *key, int *r_step)
580  {  {
581      int step = *r_step;      int step = *r_step;
582    
583      /* If the isuser already signed the key, send an empty      /* If the isuser already signed the key, send an empty
584         string and jump to quit. */             string and jump to quit. */
585      if (ctx->getResult () & GPG_EDITRES_ALREADY_SIGNED      if (ctx->getResult () & EDITKEY_ERR_ALREADY_SIGNED
586          && *r_step != -1 && *r_step != 4) {          && *r_step != -1 && *r_step != 4) {
587          *r_step = step = -1;          *r_step = step = -1;
588          return "";          return "";
# Line 573  cmd_addrev_handler (GpgKeyEdit *ctx, gpg Line 590  cmd_addrev_handler (GpgKeyEdit *ctx, gpg
590      if (*r_step == -1) {      if (*r_step == -1) {
591          *r_step = step = 4;          *r_step = step = 4;
592          return ""; /* empty value to abort. */          return ""; /* empty value to abort. */
593      }      }
594      if (step == 0 && !strcmp (key, "keyedit.prompt")) {      if (step == 0 && !strcmp (key, "keyedit.prompt")) {
595          *r_step = step = 1;          *r_step = step = 1;
596          return "addrevoker";          return "addrevoker";
# Line 621  cmd_addphoto_handler (GpgKeyEdit *ctx, g Line 638  cmd_addphoto_handler (GpgKeyEdit *ctx, g
638      return NULL;      return NULL;
639  }  }
640    
641    
642    /* 'clean' command handler. */
643    static const char*
644    cmd_clean_handler (GpgKeyEdit *ctx, gpgme_status_code_t code, const char *key)
645    {
646        if (!strcmp (key, "keyedit.prompt") && !ctx->cmd_sent) {
647            ctx->cmd_sent = 1;
648            return "clean";
649        }
650        if (!strcmp (key, "keyedit.prompt")) {
651            ctx->reset ();
652            return "save";
653        }
654        return NULL;
655    }
656    
657    
658  /* 'enable' and 'disable' command handler. */  /* 'enable' and 'disable' command handler. */
659  static const char *  static const char *
660  cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,  cmd_enable_disable_handler (GpgKeyEdit *ctx, gpgme_status_code_t code,
# Line 645  static gpgme_error_t Line 679  static gpgme_error_t
679  editkey_command_handler (void *opaque, gpgme_status_code_t code,  editkey_command_handler (void *opaque, gpgme_status_code_t code,
680                           const char *key, int fd)                           const char *key, int fd)
681  {  {
682      static int step = 0;      static int step = 0; /* XXX use ke->cnt */
683      const char *out = NULL;      const char *out = NULL;
684      GpgKeyEdit *ke = (GpgKeyEdit *)opaque;      GpgKeyEdit *ke = (GpgKeyEdit *)opaque;
685      HANDLE hd = (HANDLE)fd;      HANDLE hd = (HANDLE)fd;
# Line 657  editkey_command_handler (void *opaque, g Line 691  editkey_command_handler (void *opaque, g
691      /*log_debug ("key=%s code=%d\r\n", key, code);*/      /*log_debug ("key=%s code=%d\r\n", key, code);*/
692      switch (code) {      switch (code) {
693      case GPGME_STATUS_ALREADY_SIGNED:      case GPGME_STATUS_ALREADY_SIGNED:
694          ke->setResult (GPG_EDITRES_ALREADY_SIGNED);          ke->setResult (EDITKEY_ERR_ALREADY_SIGNED);
695          break;          break;
696    
697      case GPGME_STATUS_BAD_PASSPHRASE:      case GPGME_STATUS_BAD_PASSPHRASE:
698          ke->setResult (GPG_EDITRES_BAD_PASSPHRASE);          ke->setResult (EDITKEY_ERR_BAD_PASSPHRASE);
699          break;          break;
700            
701      default:      default:
702          break;          break;
703      }      }
704    
705      if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE) {      if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE) {
706          /* If the entered passphrase is bad, we supply empty          /* If the entered passphrase is bad, we supply empty
707             passphrase to abort and send 'quit' as soon as possible. */             passphrase to abort and send 'quit' as soon as possible. */
708          if (!strcmp (key, "passphrase.enter"))          if (!strcmp (key, "passphrase.enter"))
709              WriteFile (hd, "\n", 1, &n, NULL);              WriteFile (hd, "\n", 1, &n, NULL);
710          if (!strcmp (key, "keyedit.prompt"))          if (!strcmp (key, "keyedit.prompt"))
711              WriteFile (hd, "quit\n", 5, &n, NULL);              WriteFile (hd, "quit\n", 5, &n, NULL);
712            ke->reset ();
713            step = 0;
714            return 0;
715      }      }
716            
717      switch (ke->getType ()) {      switch (ke->getType ()) {
# Line 741  editkey_command_handler (void *opaque, g Line 779  editkey_command_handler (void *opaque, g
779          out =  cmd_addphoto_handler (ke, code, key);          out =  cmd_addphoto_handler (ke, code, key);
780          break;          break;
781    
782        case GPG_EDITKEY_CLEAN:
783            out = cmd_clean_handler (ke, code, key);
784            break;
785    
786      case GPG_EDITKEY_ENABLE:      case GPG_EDITKEY_ENABLE:
787      case GPG_EDITKEY_DISABLE:      case GPG_EDITKEY_DISABLE:
788          out = cmd_enable_disable_handler (ke, code, key,          out = cmd_enable_disable_handler (ke, code, key,
# Line 774  map_result (GpgKeyEdit *ke) Line 816  map_result (GpgKeyEdit *ke)
816      /* XXX Sometimes ALREADY_SIGNED indicates an failure. */      /* XXX Sometimes ALREADY_SIGNED indicates an failure. */
817      if (!ke->getResult ())      if (!ke->getResult ())
818          return gpg_error (GPG_ERR_NO_ERROR);          return gpg_error (GPG_ERR_NO_ERROR);
819      if (ke->getResult () & GPG_EDITRES_BAD_PASSPHRASE)      if (ke->getResult () & EDITKEY_ERR_BAD_PASSPHRASE)
820          return gpg_error (GPG_ERR_BAD_PASSPHRASE);          return gpg_error (GPG_ERR_BAD_PASSPHRASE);
821      return 0;      return 0;
822  }  }

Legend:
Removed from v.101  
changed lines
  Added in v.181

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26