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

Diff of /trunk/Src/wptGPG.cpp

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

revision 66 by twoaday, Thu Nov 3 17:55:18 2005 UTC revision 175 by twoaday, Tue Feb 7 08:58:04 2006 UTC
# Line 22  Line 22 
22  #endif  #endif
23    
24  #include <windows.h>  #include <windows.h>
 #include <shlobj.h>  
25  #include <string.h>  #include <string.h>
26  #include <stdio.h>  #include <stdio.h>
27  #include <shlobj.h>  #include <shlobj.h>
# Line 40  Line 39 
39  #include "wptW32API.h"  #include "wptW32API.h"
40  #include "wptCrypto.h"  #include "wptCrypto.h"
41    
42  #define GPG_CONF "gpg.conf"  #define GPG_CONF        "gpg.conf"
43    #define GPG_REG_EXE     "gpgProgram"    /* registry name for the binary. */
44    #define GPG_REG_HOME    "HomeDir"       /* registry name of the home dir. */
45    
46  struct gpg_watcher_s {  struct gpg_watcher_s {
47      FILETIME    last_access;      FILETIME    last_access;
# Line 61  static int check_keyring (char ** r_path Line 62  static int check_keyring (char ** r_path
62    
63    
64  /* Return the application data folder of the current user. */  /* Return the application data folder of the current user. */
65  static char*  char*
66  multi_gnupg_path (void)  multi_gnupg_path (int strict)
67  {  {
68      static char buf[256+64];      static char buf[256+64];
69      BOOL ec;      BOOL ec;
# Line 71  multi_gnupg_path (void) Line 72  multi_gnupg_path (void)
72      memset (buf, 0, sizeof (buf));      memset (buf, 0, sizeof (buf));
73      /* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */      /* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */
74      ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE);      ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE);
75      if (ec != 1)      if (ec != 1) {
76            log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed\r\n",
77                       (int)GetLastError ());
78          return NULL;          return NULL;
79        }
80      strcat (buf, "\\gnupg");      strcat (buf, "\\gnupg");
81      if (access (buf, 00))      if (strict && access (buf, 00))
82          return NULL;          return NULL;
83      return m_strdup (buf);      return m_strdup (buf);
84  }  }
85    
86    
87  /* Return the full path of the GnuPG application. First the registry is scanned  /* Return the full path to the GPG home directory. First the 'HomeDir' entry
88     for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used.     from the registry is used. Then the default $APPDATA\gnupg path. */
 */  
89  char*  char*
90  get_gnupg_path (void)  get_gnupg_path (void)
91  {  {
92      char *p = NULL, *path = NULL;      char *path;
93        
94      p = get_reg_entry_gpg ("HomeDir");      path = get_reg_entry_gpg (GPG_REG_HOME);
95      if (p) {      if (path) {
96          path = m_strdup (p);          if (dir_exist_check (path) == 0)
97          free_if_alloc (p);              return path;
98          return path;          free_if_alloc (path);
99      }      }
100      else      path = multi_gnupg_path (1);
101          return multi_gnupg_path ();      return path;
     return m_strdup ("c:\\gnupg");  
102  }  }
103    
104    
# Line 105  get_gnupg_path (void) Line 107  get_gnupg_path (void)
107  char*  char*
108  get_gnupg_cfgfile (void)  get_gnupg_cfgfile (void)
109  {      {    
110      char *p = NULL, *optfile = NULL, *path = NULL;      char *p = NULL;
111        char *optfile = NULL;
112        char *path = NULL;
113      size_t nlen = 0;      size_t nlen = 0;
114    
115      path = get_gnupg_path ();      path = get_gnupg_path ();
116      if (!path)      if (!path)
117          return NULL;          return NULL;
118      p = get_reg_entry_gpg ("OptFile");      p = get_reg_entry_gpg ("OptFile");
119      if (p && !strcmp (p, "")) {      if (p) {
120          nlen = strlen (path) + 64;          nlen = strlen (p) + 4;
         optfile = new char[nlen + 1];  
         if (!optfile)  
             BUG (0);  
         _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);  
     }  
     else if (p) {  
         nlen = strlen( p ) + 4;  
121          optfile = new char[nlen + 1];          optfile = new char[nlen + 1];
122          if (!optfile)          if (!optfile)
123              BUG (NULL);              BUG (NULL);
# Line 152  get_gnupg_keyring (int pub, int strict) Line 149  get_gnupg_keyring (int pub, int strict)
149      if (!path)      if (!path)
150          return NULL;          return NULL;
151      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");
152      if (!strict && !file_exist_check (keyring)) {      if (strict && !file_exist_check (keyring)) {
153            free_if_alloc (path);
154            return keyring;
155        }
156        else if (!strict) {
157          free_if_alloc (path);          free_if_alloc (path);
158          return keyring;          return keyring;
159      }      }
# Line 177  get_gnupg_prog (void) Line 178  get_gnupg_prog (void)
178  {      {    
179      char *p;      char *p;
180      char *pgm = NULL;      char *pgm = NULL;
     size_t nlen = 0;  
181    
182      p = get_reg_entry_gpg ("gpgProgram");      p = get_reg_entry_gpg (GPG_REG_EXE);
183      if (!p) {      if (!p) {
184          char *path = get_gnupg_path ();          char *path = get_gnupg_path ();
185          if (!path)          if (!path)
# Line 201  get_gnupg_prog (void) Line 201  get_gnupg_prog (void)
201  static char *  static char *
202  default_key_from_cache (int *ret_no_useable)  default_key_from_cache (int *ret_no_useable)
203  {  {
204      const char * s;      const char *s;
205      char * keyid = NULL;      char *keyid = NULL;
206      gpgme_key_t key;      gpgme_key_t key;
207      gpg_keycache_t sec = keycache_get_ctx (0);      gpg_keycache_t sec = keycache_get_ctx (0);
208    
# Line 262  get_gnupg_default_key (void) Line 262  get_gnupg_default_key (void)
262          return default_key_from_cache (&no_usable);          return default_key_from_cache (&no_usable);
263      rc = parse_gpg_options (optfile, &opt);      rc = parse_gpg_options (optfile, &opt);
264      if (rc) {      if (rc) {
265          free_if_alloc( optfile );          free_if_alloc (optfile);
266          return default_key_from_cache( &no_usable );          return default_key_from_cache (&no_usable);
267      }      }
268      e = find_option( opt, "default-key" );      e = find_option( opt, "default-key" );
269      if ( e )      if ( e )
# Line 287  get_gnupg_default_key (void) Line 287  get_gnupg_default_key (void)
287  } /* get_gnupg_default_key */  } /* get_gnupg_default_key */
288    
289    
290    /* Check if GPG4WIN is available and if so, use the
291       install path to figure out where the gpg.exe is. */
292    char*
293    check_for_gpg4win (void)
294    {
295        return get_reg_entry_gpg4win ("gpg.exe");
296    }
297    
298    
299  /* Check if the gpg application (exe file) is available. */  /* Check if the gpg application (exe file) is available. */
300  int  int
301  check_gnupg_prog (void)  check_gnupg_prog (void)
302  {  {
303      char *pgm = NULL;      char *gpgexe = NULL;
304      int rc = 0;      int rc = 0;
305    
306      pgm = get_gnupg_prog ();      gpgexe = get_gnupg_prog ();
307      if (!pgm)      if (!gpgexe || file_exist_check (gpgexe)) {
308          rc = WPTERR_GPG_EXEFILE;          free_if_alloc (gpgexe);
309      if (file_exist_check (pgm))          gpgexe = check_for_gpg4win ();
310          rc = WPTERR_GPG_EXEFILE;          if (!gpgexe || file_exist_check (gpgexe))
311      free_if_alloc (pgm);              rc = WPTERR_GPG_EXEFILE;
312            else
313                set_reg_entry_gpg (GPG_REG_EXE, gpgexe);
314        }
315        free_if_alloc (gpgexe);
316      return rc;      return rc;
317  }  }
318    
319    
320  static int  static int
321  parse_version_nr (const char * buf, int *major, int *minor, int *patch)  parse_version_nr (const char *buf, int *major, int *minor, int *patch)
322  {  {
323      char tmp[8];      char tmp[8];
324      int i;      int i;
# Line 321  parse_version_nr (const char * buf, int Line 334  parse_version_nr (const char * buf, int
334      tmp[i] = 0; buf++;      tmp[i] = 0; buf++;
335      *minor = atol (tmp);      *minor = atol (tmp);
336      i=0;      i=0;
337      while (buf && isdigit( *buf ) && i < 8)      while (buf && isdigit (*buf) && i < 8)
338          tmp[i++] = *buf++;          tmp[i++] = *buf++;
339      tmp[i] = 0;      tmp[i] = 0;
340      *patch = atol (tmp);      *patch = atol (tmp);
# Line 333  parse_version_nr (const char * buf, int Line 346  parse_version_nr (const char * buf, int
346     version given in @r_major.@r_minor.@r_patch. On success these     version given in @r_major.@r_minor.@r_patch. On success these
347     variables contain the GPG version which is installed. */     variables contain the GPG version which is installed. */
348  int  int
349  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)  check_gnupg_engine (const char *need_gpg_ver,
350                        int *r_major, int *r_minor, int *r_patch)
351  {  {
352      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
353      gpgme_engine_info_t inf;      gpgme_engine_info_t inf;
354      char * eng = NULL;      char *eng = NULL;
355      int major=0, minor=0, patch=0;      int major=0, minor=0, patch=0;
356      int rc;      int need_major = 0, need_minor = 0, need_patch = 0;
357                int rc = 1;
358    
359        /* Convert the needed GPG version to the integer format. */
360        if (parse_version_nr (need_gpg_ver,
361                              &need_major, &need_minor, &need_patch))
362            return 1;
363        
364      gpgme_new (&ctx);      gpgme_new (&ctx);
365      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
366      if (!inf) {      if (!inf) {
367          gpgme_release (ctx);          gpgme_release (ctx);
368          return -1;          return -1;
369      }      }
370    
371      /* We need to exec GPG again to find out if IDEA is available. */      /* We need to exec GPG again to find out if IDEA is available. */
372      if (gpg_get_version (&eng))      if (gpg_get_version (&eng))
373          return -1;          return -1;
374      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
375          idea_available = 1;          idea_available = 1;
376      free (eng);      free (eng);
377      rc = parse_version_nr( inf->version, &major, &minor, &patch );      if (parse_version_nr (inf->version, &major, &minor, &patch)) {
     if( rc ) {  
378          gpgme_release (ctx);          gpgme_release (ctx);
379          return rc;          return 1;
380      }      }
381    
382      /* FIXME: This check is wrong! */      if (major > need_major)
     if (major < *r_major || minor < *r_minor)  
         rc = 1;  
     else {  
         if (patch < *r_patch)  
             rc = 1;  
383          rc = 0;          rc = 0;
384      }      else if (major == need_major && minor > need_minor)      
385            rc = 0;
386        else if (major == need_major && minor == need_minor &&
387                 patch >= need_patch)
388            rc = 0;
389    
390        /* Return the current GPG version. */
391      *r_major = major;      *r_major = major;
392      *r_minor = minor;      *r_minor = minor;
393      *r_patch = patch;      *r_patch = patch;
# Line 402  check_gnupg_cfgfile (const char *fname, Line 423  check_gnupg_cfgfile (const char *fname,
423  } /* check_gnupg_cfgfile */  } /* check_gnupg_cfgfile */
424    
425    
426  /*  /* Usually GPG creates the pubring.gpg, secring.gpg on
427   * Check if both keyrings are located in the gnupg home directory.     the first start, but to make sure they always exist
428   */     create them empty if needed. */
429    static void
430    create_empty_keyring (int _pub)
431    {
432        char *name;
433        FILE *f;
434    
435        name = get_gnupg_keyring (_pub, 0);
436        if (file_exist_check (name) != 0) {
437            f = fopen (name, "ab");
438            if (f != NULL)
439                fclose (f);
440        }
441        free_if_alloc (name);
442    }
443    
444    
445    /* Check if both keyrings are located in the gnupg home directory. */
446  int  int
447  gnupg_access_files (void)  gnupg_access_files (void)
448  {  {
# Line 413  gnupg_access_files (void) Line 451  gnupg_access_files (void)
451      int secrings = 0, pubrings = 0;      int secrings = 0, pubrings = 0;
452      char *optfile;      char *optfile;
453    
454        create_empty_keyring (1);
455      if (gnupg_access_keyring (1))      if (gnupg_access_keyring (1))
456          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
457      else      else
458          pubring_ok = 1;          pubring_ok = 1;
459    
460        create_empty_keyring (0);
461      if (gnupg_access_keyring (0))      if (gnupg_access_keyring (0))
462          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
463      else      else
464          secring_ok = 1;          secring_ok = 1;
465    
466      if (!pubring_ok || !secring_ok) {      if (!pubring_ok || !secring_ok) {
467          optfile = get_gnupg_cfgfile ();          optfile = get_gnupg_cfgfile ();
468          if (!optfile)          if (!optfile)
469              return WPTERR_GPG_KEYRINGS;              return WPTERR_GPG_KEYRINGS;
470          rc = file_exist_check (optfile);          rc = file_exist_check (optfile);
471          if (!rc && get_file_size(optfile) > 0) {          if (!rc && get_file_size (optfile) > 0) {
472              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);
473              if (!rc && secrings && pubrings) {              if (!rc && secrings && pubrings) {
474                  free_if_alloc (optfile);                  free_if_alloc (optfile);
# Line 445  gnupg_access_files (void) Line 486  gnupg_access_files (void)
486          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
487      }      }
488      return rc;      return rc;
489  } /* gnupg_access_files */  }
490    
491    
492  static int  static int
# Line 454  create_gpg_options (void) Line 495  create_gpg_options (void)
495      FILE *fp;      FILE *fp;
496      char *s, *optfile;      char *s, *optfile;
497    
498      s = get_gnupg_path( );      s = get_gnupg_path ();
499      if( s == NULL )      if( s == NULL )
500          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
501      optfile = make_filename( s, GPG_CONF, NULL );      optfile = make_filename (s, GPG_CONF, NULL);
502      fp = fopen( optfile, "wb" );      fp = fopen (optfile, "wb");
503      if( fp == NULL ) {        if (fp == NULL) {  
504          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
505          goto fail;          goto fail;
506      }      }
507      fwrite( options_skel, 1, strlen( options_skel ), fp );      fwrite (options_skel, 1, strlen (options_skel), fp);
508      fclose( fp );      fclose (fp);
509    
510  fail:  fail:
511      free_if_alloc( s );      free_if_alloc (s);
512      free_if_alloc( optfile );      free_if_alloc (optfile);
513      return 0;      return 0;
514  } /* create_gpg_options */  } /* create_gpg_options */
515    
# Line 648  check_last_gnupg_access (gpg_watcher_s * Line 689  check_last_gnupg_access (gpg_watcher_s *
689      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
690          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
691          ctx->modified = 1;          ctx->modified = 1;
692        
693        /* XXX: find a better way. without it, winpt --keymanager loads
694                the key cache twice. */
695        if (ctx->last_access.dwLowDateTime == 0)
696            ctx->modified = 0;
697    
698      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
699      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
# Line 754  get_gnupg_keyring_from_options (const ch Line 800  get_gnupg_keyring_from_options (const ch
800    
801  /* XXX: does not work with write-protected floppies */  /* XXX: does not work with write-protected floppies */
802  static int  static int
803  my_access (const char * fname)  my_access (const char *fname)
804  {  {
805      HANDLE hd;      HANDLE hd;
806      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
# Line 766  my_access (const char * fname) Line 812  my_access (const char * fname)
812  }  }
813    
814    
815    /* Check the file permissions of the public keyring.
816       If @showmsg is 1 output a message in case of errors.
817       Return value: 1 if read-only attribute
818                     2 if file is opened by another process exclusively. */
819  int  int
820  gpg_check_permissions (int showmsg)  gpg_check_permissions (int showmsg)
821  {  {
822      char * p, * name = NULL;      char *p = NULL;
823        char *name = NULL;
824      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
825    
826      p = get_gnupg_path ();      p = get_gnupg_path ();
827      check_keyring (&p);      if (check_keyring (&p) && p) {
     if (p) {  
828          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
         free_if_alloc (p);  
829          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
830              ans = msg_box (NULL,              ans = msg_box (NULL,
831                             _("The selected keyring has the read-only file\n"                             _("The selected keyring has the read-only file\n"
# Line 792  gpg_check_permissions (int showmsg) Line 841  gpg_check_permissions (int showmsg)
841                  }                  }
842              }              }
843              else if (ans == IDNO) {              else if (ans == IDNO) {
844                  /*                  /* All commands with write access will be disabled. */
                 msg_box (NULL, _("All commands with write access to the keyring\n"  
                                  "will be disabled."), _("GPG Information"), MB_INFO);  
                 */  
845                  failed = 1;                  failed = 1;
846              }              }
847          }          }
# Line 811  gpg_check_permissions (int showmsg) Line 857  gpg_check_permissions (int showmsg)
857              failed = 2;              failed = 2;
858          }          }
859      }      }
860        free_if_alloc (p);
861      free_if_alloc (name);      free_if_alloc (name);
862      return failed;      return failed;
863  } /* gpg_check_permissions */  }
864    
865    
866  /* Check the GPG home dir. If all methods failed, try to  /* Check the GPG home dir. First try to read the 'HomeDir' registry entry,
867     create the default folder. */     then check for $APPDATA\gnupg. Create the dir if it does not exists. */
868  static int  int
869  check_homedir (void)  gnupg_check_homedir (void)
870  {        {      
871      char *homedir = NULL;      char *homedir = NULL;
872      int yes = 0;      int val = 0;
873        int rc = 0;
874    
875      homedir = get_reg_entry_gpg ("HomeDir");      homedir = get_reg_entry_gpg (GPG_REG_HOME);
876      if (!homedir)      if (!homedir)
877          homedir = multi_gnupg_path ();          homedir = multi_gnupg_path (0);
     if (!homedir)  
         homedir = m_strdup ("c:\\gnupg");  
878      if (homedir) {      if (homedir) {
879          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
880              yes = log_box (_("Preferences"), MB_YESNO,              val = log_box (_("Preferences"), MB_YESNO,
881                             _("%s does not exit.\n"                             _("%s does not exit.\n"
882                               "Do you want to create this directory?"), homedir);                               "Do you want to create this directory?"), homedir);
883              if (yes == IDYES) {              if (val == IDYES) {
884                  BOOL ec = CreateDirectory (homedir, NULL);                  if (!CreateDirectory (homedir, NULL))
885                  free_if_alloc (homedir);                      rc = WPTERR_DIR_CREAT;
                 if (ec == FALSE)  
                     return WPTERR_DIR_CREAT;  
                 return 0;  
886              }              }
887              return WPTERR_DIR_OPEN;              else
888          }                  rc = WPTERR_DIR_OPEN;
         free_if_alloc (homedir);  
     }  
     return 0;  
 }  
   
   
 int  
 gnupg_check_homedir (void)  
 {        
     char *homedir = NULL;  
     char *prog = NULL;  
     int rc = 0, ec = 0;  
       
     rc = check_homedir ();  
     if (rc)  
         return rc;  
     if ((homedir = get_reg_entry_gpg ("HomeDir")) &&  
         !(prog = get_reg_entry_gpg ("gpgProgram" ))) {  
         prog = make_filename (homedir, "gpg", "exe");  
         if (file_exist_check (prog) == 0) {  
             rc = set_reg_entry_gpg ("gpgProgram", prog);  
             if (rc)  
                 goto fail;  
889          }          }
890          free_if_alloc (homedir);          free_if_alloc (homedir);
         free_if_alloc (prog);  
         return rc;  
     }  
     if ((prog = get_reg_entry_gpg ("gpgProgram"))  
         && file_exist_check (prog)) {  
         free_if_alloc (prog);  
         homedir = get_reg_entry_gpg ("HomeDir");  
         if (!homedir) {  
             rc = WPTERR_GENERAL;  
             goto fail;  
         }  
         prog = make_filename (homedir, "gpg", "exe");  
         if (file_exist_check (prog) == 0) {  
             rc = set_reg_entry_gpg ("gpgProgram", prog);  
             if (rc)  
                 goto fail;  
             free_if_alloc (prog);  
             return rc;  
         }  
891      }      }
       
     /* Change the return code if homedir doesn't exist or if the program  
        doesn't exist. Note that exist_checks return 0 to suggest existance. */  
     if ((!homedir || dir_exist_check (homedir)))  
         rc = WPTERR_GENERAL;  
       
 fail:  
     free_if_alloc (homedir);  
     free_if_alloc (prog);  
892      return rc;      return rc;
893  } /* gnupg_check_homedir */  }
894    
895    
896  int  int
# Line 914  gnupg_copy_keyrings (void) Line 906  gnupg_copy_keyrings (void)
906          return WPTERR_GENERAL;          return WPTERR_GENERAL;
907      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
908    
909      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),
910                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL);
911      if (!pring) {      if (!pring) {
912          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
913          free_if_alloc (path);          free_if_alloc (path);
# Line 934  gnupg_copy_keyrings (void) Line 926  gnupg_copy_keyrings (void)
926      }      }
927      free_if_alloc (file);      free_if_alloc (file);
928    
929      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),
930                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL);
931      if (!sring) {      if (!sring) {
932          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
933          return WPTERR_GENERAL;          return WPTERR_GENERAL;
# Line 958  fail: Line 950  fail:
950  } /* gnupg_import_keyrings */  } /* gnupg_import_keyrings */
951    
952    
953    /* Backup the gpg.conf file. */
954  void  void
955  gnupg_backup_options (void)  gnupg_backup_options (void)
956  {  {
# Line 965  gnupg_backup_options (void) Line 958  gnupg_backup_options (void)
958      char bak[512];      char bak[512];
959    
960      cfgfile = get_gnupg_cfgfile ();      cfgfile = get_gnupg_cfgfile ();
961      if (cfgfile == NULL)      if (!cfgfile)
962          return;          return;
963      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
964      CopyFile (cfgfile, bak, FALSE);      CopyFile (cfgfile, bak, FALSE);
965      free_if_alloc (cfgfile);      free_if_alloc (cfgfile);
966  } /* gnupg_backup_options */  }
   
967    
968    
969  static int  static int
# Line 999  backup_one_file (const char *srcpath, co Line 991  backup_one_file (const char *srcpath, co
991  } /* backup_one_file */  } /* backup_one_file */
992    
993    
994    /* Figure out first public keyring which is not empty.
995       Return value: 1 on success. */
996  static int  static int
997  check_keyring (char ** r_path)  check_keyring (char **r_path)
998  {  {
999      char * p;      char *p;
1000      char * opt, * name;      char *opt;
1001        char *name;
1002    
1003      if (!*r_path)      if (!*r_path)
1004          return 0;          return 0;
1005      p = make_filename (*r_path, "pubring", "gpg");      p = make_filename (*r_path, "pubring", "gpg");
1006      if (!p || get_file_size (p) > 0)      if (!p || get_file_size (p) <= 0)
1007          return 0;          return 0;
1008    
1009      opt = get_gnupg_cfgfile ();      opt = get_gnupg_cfgfile ();
# Line 1020  check_keyring (char ** r_path) Line 1015  check_keyring (char ** r_path)
1015      if (!name)      if (!name)
1016          return 0;          return 0;
1017      p = strrchr (name, '\\');      p = strrchr (name, '\\');
1018      if (!p)      if (!p) {
     {  
1019          free_if_alloc (name);          free_if_alloc (name);
1020          return 0;                return 0;      
1021      }      }
# Line 1034  check_keyring (char ** r_path) Line 1028  check_keyring (char ** r_path)
1028  }  }
1029    
1030    
1031    /* Return a temp name based on the day of the week. */
1032  static char*  static char*
1033  get_backup_name (const char *templ)  get_backup_name (const char *templ)
1034  {  {
1035      struct tm *tm;      struct tm *tm;
1036      char *p;      char *p;
1037        time_t t;
1038    
1039      time_t t = time (NULL);      t = time (NULL);
1040      tm = localtime (&t);      tm = localtime (&t);
1041      p = new char [strlen (templ) + 8 + 1];      p = new char [strlen (templ) + 8 + 1];
1042      if (!p)      if (!p)
# Line 1050  get_backup_name (const char *templ) Line 1046  get_backup_name (const char *templ)
1046  }  }
1047    
1048    
1049    /* Make backups of all keyrings. The public key ring is
1050       rotated like this pubring-%d.gpg. */
1051  void  void
1052  gnupg_backup_keyrings (void)  gnupg_backup_keyrings (void)
1053  {  {
# Line 1060  gnupg_backup_keyrings (void) Line 1058  gnupg_backup_keyrings (void)
1058      if (!reg_prefs.auto_backup)      if (!reg_prefs.auto_backup)
1059          return;          return;
1060      bakmode = reg_prefs.backup.mode;      bakmode = reg_prefs.backup.mode;
1061      srcpath =  get_gnupg_path ();      srcpath = get_gnupg_path ();
1062      check_keyring (&srcpath);      check_keyring (&srcpath);
1063      if (bakmode == 1) {      if (bakmode == 1) {
1064          dstpath = get_gnupg_path ();          dstpath = multi_gnupg_path (1);
1065          check_keyring (&dstpath);          check_keyring (&dstpath);
1066      }      }
1067      else if (bakmode == 2) {      else if (bakmode == 2) {
1068          char * tmpfile;          char *tmpfile;
1069          FILE * fp;          FILE *fp;
1070    
1071          dstpath = m_strdup (reg_prefs.backup.path);          dstpath = m_strdup (reg_prefs.backup.path);
1072          if (!dstpath)          if (!dstpath)
# Line 1082  gnupg_backup_keyrings (void) Line 1080  gnupg_backup_keyrings (void)
1080          else {          else {
1081              rc = 0;              rc = 0;
1082              fclose (fp);              fclose (fp);
1083              unlink (tmpfile);              remove (tmpfile);
1084          }          }
1085          free_if_alloc (tmpfile);          free_if_alloc (tmpfile);
1086          if (!fp || rc == IDCANCEL)          if (!fp || rc == IDCANCEL)
# Line 1099  gnupg_backup_keyrings (void) Line 1097  gnupg_backup_keyrings (void)
1097      free_if_alloc (name);      free_if_alloc (name);
1098      free_if_alloc (srcpath);      free_if_alloc (srcpath);
1099      free_if_alloc (dstpath);      free_if_alloc (dstpath);
1100  } /* gnupg_backup_keyrings */  }
1101    
1102    
1103  /* Display GPG error from file if possible. */  /* Display GPG error from file if possible. */
1104  void  void
1105  gnupg_display_error (void)  gnupg_display_error (void)
1106  {        {      
1107      char tmpath[512], * errstr;      char tmpath[512], *errstr;
1108      size_t size = 0;      size_t size = 0;
1109      FILE * fp;      FILE *fp;
1110    
1111      GetTempPath (sizeof tmpath - 32, (tmpath));      get_temp_name (tmpath, sizeof (tmpath), "gpg_stderr");
     strcat (tmpath, "gpg_stderr");  
1112      size = get_file_size (tmpath);      size = get_file_size (tmpath);
1113      if (file_exist_check (tmpath) || size <= 0)      if (file_exist_check (tmpath) || size <= 0)
1114          return;          return;
1115      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1116      if (!fp) {      if (!fp) {
1117          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1118                     _("GPG Error"), MB_INFO);
1119          return;          return;
1120      }      }
1121      errstr = new char[size+1];      errstr = new char[size+1];

Legend:
Removed from v.66  
changed lines
  Added in v.175

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26