/[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 42 by werner, Fri Oct 28 08:25:30 2005 UTC revision 137 by twoaday, Mon Jan 9 14:01:51 2006 UTC
# Line 22  Line 22 
22  #endif  #endif
23    
24  #include <windows.h>  #include <windows.h>
 #include <windows.h>  
25  #include <string.h>  #include <string.h>
26  #include <stdio.h>  #include <stdio.h>
27  #include <shlobj.h>  #include <shlobj.h>
# Line 31  Line 30 
30  #include <time.h>  #include <time.h>
31    
32  #include "wptGPG.h"  #include "wptGPG.h"
33  #include "wptGPGCmds.h"  #include "wptGpgCmds.h"
34  #include "wptGPGOptSkel.h"  #include "wptGPGOptSkel.h"
35  #include "wptTypes.h"  #include "wptTypes.h"
36  #include "wptNLS.h"  #include "wptNLS.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 ();
# Line 152  get_gnupg_keyring (int pub, int strict) Line 156  get_gnupg_keyring (int pub, int strict)
156      if (!path)      if (!path)
157          return NULL;          return NULL;
158      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");
159      if (!strict && !file_exist_check (keyring)) {      if (strict && !file_exist_check (keyring)) {
160          free_if_alloc (path);          free_if_alloc (path);
161          return keyring;          return keyring;
162      }      }
163      if (file_exist_check (keyring) || get_file_size (keyring) == 0) {      else if (!strict) {
164            free_if_alloc (path);
165            return keyring;
166        }
167        if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) {
168          free_if_alloc (keyring);          free_if_alloc (keyring);
169          optfile = make_filename (path, GPG_CONF, NULL);          optfile = make_filename (path, GPG_CONF, NULL);
170          keyring = get_gnupg_keyring_from_options (optfile, pub);          keyring = get_gnupg_keyring_from_options (optfile, pub);
# Line 170  get_gnupg_keyring (int pub, int strict) Line 178  get_gnupg_keyring (int pub, int strict)
178  /* Return the full path (with the gpg exe name). First the registry is scanned  /* Return the full path (with the gpg exe name). First the registry is scanned
179     for the entry 'gpgProgram'. If it wasn't set, the default path is the     for the entry 'gpgProgram'. If it wasn't set, the default path is the
180     appended string 'gpg.exe' is used. */     appended string 'gpg.exe' is used. */
181    
182    /* FIXME:  Use gpgme's engine info here. */
183  char*  char*
184  get_gnupg_prog (void)  get_gnupg_prog (void)
185  {      {    
186      char *p;      char *p;
187      char *pgm = NULL;      char *pgm = NULL;
     size_t nlen = 0;  
188    
189      p = get_reg_entry_gpg ("gpgProgram");      p = get_reg_entry_gpg (GPG_REG_EXE);
190      if (!p) {      if (!p) {
191          char *path = get_gnupg_path ();          char *path = get_gnupg_path ();
192          if (!path)          if (!path)
# Line 199  get_gnupg_prog (void) Line 208  get_gnupg_prog (void)
208  static char *  static char *
209  default_key_from_cache (int *ret_no_useable)  default_key_from_cache (int *ret_no_useable)
210  {  {
211      const char * s;      const char *s;
212      char * keyid = NULL;      char *keyid = NULL;
213      gpgme_key_t key;      gpgme_key_t key;
214      gpg_keycache_t sec = keycache_get_ctx (0);      gpg_keycache_t sec = keycache_get_ctx (0);
215    
# Line 215  default_key_from_cache (int *ret_no_usea Line 224  default_key_from_cache (int *ret_no_usea
224              break;              break;
225          }          }
226      }      }
227      if (!keyid) {      if (!keyid)
228          *ret_no_useable = 1;          *ret_no_useable = 1;
         msg_box (NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR);  
     }  
229      return keyid;      return keyid;
230  }  }
231    
# Line 262  get_gnupg_default_key (void) Line 269  get_gnupg_default_key (void)
269          return default_key_from_cache (&no_usable);          return default_key_from_cache (&no_usable);
270      rc = parse_gpg_options (optfile, &opt);      rc = parse_gpg_options (optfile, &opt);
271      if (rc) {      if (rc) {
272          free_if_alloc( optfile );          free_if_alloc (optfile);
273          return default_key_from_cache( &no_usable );          return default_key_from_cache (&no_usable);
274      }      }
275      e = find_option( opt, "default-key" );      e = find_option( opt, "default-key" );
276      if ( e )      if ( e )
# Line 287  get_gnupg_default_key (void) Line 294  get_gnupg_default_key (void)
294  } /* get_gnupg_default_key */  } /* get_gnupg_default_key */
295    
296    
297    /* Check if GPG4WIN is available and if so, use the
298       install path to figure out where the gpg.exe is. */
299    char*
300    check_for_gpg4win (void)
301    {
302        return get_reg_entry_gpg4win ("gpg.exe");
303    }
304    
305    
306  /* Check if the gpg application (exe file) is available. */  /* Check if the gpg application (exe file) is available. */
307  int  int
308  check_gnupg_prog (void)  check_gnupg_prog (void)
309  {  {
310      char *pgm = NULL;      char *gpgexe = NULL;
311      int rc = 0;      int rc = 0;
312    
313      pgm = get_gnupg_prog ();      gpgexe = get_gnupg_prog ();
314      if (!pgm)      if (!gpgexe || file_exist_check (gpgexe)) {
315          rc = WPTERR_GPG_EXEFILE;          free_if_alloc (gpgexe);
316      if (file_exist_check (pgm))          gpgexe = check_for_gpg4win ();
317          rc = WPTERR_GPG_EXEFILE;          if (!gpgexe || file_exist_check (gpgexe))
318      free_if_alloc (pgm);              rc = WPTERR_GPG_EXEFILE;
319            else
320                set_reg_entry_gpg (GPG_REG_EXE, gpgexe);
321        }
322        free_if_alloc (gpgexe);
323      return rc;      return rc;
324  }  }
325    
326    
327  static int  static int
328  parse_version_nr (const char * buf, int *major, int *minor, int *patch)  parse_version_nr (const char *buf, int *major, int *minor, int *patch)
329  {  {
330      char tmp[8];      char tmp[8];
331      int i;      int i;
# Line 321  parse_version_nr (const char * buf, int Line 341  parse_version_nr (const char * buf, int
341      tmp[i] = 0; buf++;      tmp[i] = 0; buf++;
342      *minor = atol (tmp);      *minor = atol (tmp);
343      i=0;      i=0;
344      while (buf && isdigit( *buf ) && i < 8)      while (buf && isdigit (*buf) && i < 8)
345          tmp[i++] = *buf++;          tmp[i++] = *buf++;
346      tmp[i] = 0;      tmp[i] = 0;
347      *patch = atol (tmp);      *patch = atol (tmp);
# Line 333  parse_version_nr (const char * buf, int Line 353  parse_version_nr (const char * buf, int
353     version given in @r_major.@r_minor.@r_patch. On success these     version given in @r_major.@r_minor.@r_patch. On success these
354     variables contain the GPG version which is installed. */     variables contain the GPG version which is installed. */
355  int  int
356  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)  check_gnupg_engine (const char *need_gpg_ver,
357                        int *r_major, int *r_minor, int *r_patch)
358  {  {
359      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
360      gpgme_engine_info_t inf;      gpgme_engine_info_t inf;
361      char * eng = NULL;      char *eng = NULL;
362      int major=0, minor=0, patch=0;      int major=0, minor=0, patch=0;
363      int rc;      int need_major = 0, need_minor = 0, need_patch = 0;
364                int rc = 1;
365    
366        /* Convert the needed GPG version to the integer format. */
367        rc = parse_version_nr (need_gpg_ver,
368                               &need_major, &need_minor, &need_patch);
369        if (rc)
370            return rc;
371        
372      gpgme_new (&ctx);      gpgme_new (&ctx);
373      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
374      if (!inf) {      if (!inf) {
375          gpgme_release (ctx);          gpgme_release (ctx);
376          return -1;          return -1;
377      }      }
378    
379      /* 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. */
380      if (gpg_get_version (&eng))      if (gpg_get_version (&eng))
381          return -1;          return -1;
382      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
383          idea_available = 1;          idea_available = 1;
384      free (eng);      free (eng);
385      rc = parse_version_nr( inf->version, &major, &minor, &patch );      rc = parse_version_nr (inf->version, &major, &minor, &patch);
386      if( rc ) {      if (rc) {
387          gpgme_release (ctx);          gpgme_release (ctx);
388          return rc;          return rc;
389      }      }
390      if (major < *r_major || minor < *r_minor)  
391          rc = 1;      if (major > need_major)
     else {  
         if (patch < *r_patch)  
             rc = 1;  
392          rc = 0;          rc = 0;
393      }      else if (major == need_major && minor > need_minor)      
394            rc = 0;
395        else if (major == need_major && minor == need_minor &&
396                 patch >= need_patch)
397            rc = 0;
398    
399        /* Return the current GPG version. */
400      *r_major = major;      *r_major = major;
401      *r_minor = minor;      *r_minor = minor;
402      *r_patch = patch;      *r_patch = patch;
# Line 400  check_gnupg_cfgfile (const char *fname, Line 432  check_gnupg_cfgfile (const char *fname,
432  } /* check_gnupg_cfgfile */  } /* check_gnupg_cfgfile */
433    
434    
435  /*  /* Usually GPG creates the pubring.gpg, secring.gpg on
436   * Check if both keyrings are located in the gnupg home directory.     the first start, but to make sure they always exist
437   */     create them empty if needed. */
438    static void
439    create_empty_keyring (int _pub)
440    {
441        char *name;
442        FILE *f;
443    
444        name = get_gnupg_keyring (_pub, 0);
445        if (file_exist_check (name) != 0) {
446            f = fopen (name, "ab");
447            if (f != NULL)
448                fclose (f);
449        }
450        free_if_alloc (name);
451    }
452    
453    
454    /* Check if both keyrings are located in the gnupg home directory. */
455  int  int
456  gnupg_access_files (void)  gnupg_access_files (void)
457  {  {
# Line 411  gnupg_access_files (void) Line 460  gnupg_access_files (void)
460      int secrings = 0, pubrings = 0;      int secrings = 0, pubrings = 0;
461      char *optfile;      char *optfile;
462    
463        create_empty_keyring (1);
464      if (gnupg_access_keyring (1))      if (gnupg_access_keyring (1))
465          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
466      else      else
467          pubring_ok = 1;          pubring_ok = 1;
468    
469        create_empty_keyring (0);
470      if (gnupg_access_keyring (0))      if (gnupg_access_keyring (0))
471          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
472      else      else
473          secring_ok = 1;          secring_ok = 1;
474    
475      if (!pubring_ok || !secring_ok) {      if (!pubring_ok || !secring_ok) {
476          optfile = get_gnupg_cfgfile ();          optfile = get_gnupg_cfgfile ();
477          if (!optfile)          if (!optfile)
478              return WPTERR_GPG_KEYRINGS;              return WPTERR_GPG_KEYRINGS;
479          rc = file_exist_check (optfile);          rc = file_exist_check (optfile);
480          if (!rc && get_file_size(optfile) > 0) {          if (!rc && get_file_size (optfile) > 0) {
481              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);
482              if (!rc && secrings && pubrings) {              if (!rc && secrings && pubrings) {
483                  free_if_alloc (optfile);                  free_if_alloc (optfile);
# Line 443  gnupg_access_files (void) Line 495  gnupg_access_files (void)
495          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
496      }      }
497      return rc;      return rc;
498  } /* gnupg_access_files */  }
499    
500    
501  static int  static int
# Line 452  create_gpg_options (void) Line 504  create_gpg_options (void)
504      FILE *fp;      FILE *fp;
505      char *s, *optfile;      char *s, *optfile;
506    
507      s = get_gnupg_path( );      s = get_gnupg_path ();
508      if( s == NULL )      if( s == NULL )
509          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
510      optfile = make_filename( s, GPG_CONF, NULL );      optfile = make_filename (s, GPG_CONF, NULL);
511      fp = fopen( optfile, "wb" );      fp = fopen (optfile, "wb");
512      if( fp == NULL ) {        if (fp == NULL) {  
513          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
514          goto fail;          goto fail;
515      }      }
516      fwrite( options_skel, 1, strlen( options_skel ), fp );      fwrite (options_skel, 1, strlen (options_skel), fp);
517      fclose( fp );      fclose (fp);
518    
519  fail:  fail:
520      free_if_alloc( s );      free_if_alloc (s);
521      free_if_alloc( optfile );      free_if_alloc (optfile);
522      return 0;      return 0;
523  } /* create_gpg_options */  } /* create_gpg_options */
524    
# Line 646  check_last_gnupg_access (gpg_watcher_s * Line 698  check_last_gnupg_access (gpg_watcher_s *
698      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
699          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
700          ctx->modified = 1;          ctx->modified = 1;
701        
702        /* XXX: find a better way. without it, winpt --keymanager loads
703                the key cache twice. */
704        if (ctx->last_access.dwLowDateTime == 0)
705            ctx->modified = 0;
706    
707      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
708      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
# Line 752  get_gnupg_keyring_from_options (const ch Line 809  get_gnupg_keyring_from_options (const ch
809    
810  /* XXX: does not work with write-protected floppies */  /* XXX: does not work with write-protected floppies */
811  static int  static int
812  my_access (const char * fname)  my_access (const char *fname)
813  {  {
814      HANDLE hd;      HANDLE hd;
815      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
# Line 764  my_access (const char * fname) Line 821  my_access (const char * fname)
821  }  }
822    
823    
824    /* Check the file permissions of the public keyring.
825       If @showmsg is 1 output a message in case of errors.
826       Return value: 1 if read-only attribute
827                     2 if file is opened by another process exclusively. */
828  int  int
829  gpg_check_permissions (int showmsg)  gpg_check_permissions (int showmsg)
830  {  {
831      char * p, * name = NULL;      char *p = NULL;
832        char *name = NULL;
833      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
834    
835      p = get_gnupg_path ();      p = get_gnupg_path ();
836      check_keyring (&p);      if (check_keyring (&p) && p) {
     if (p) {  
837          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
         free_if_alloc (p);  
838          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
839              ans = msg_box (NULL,              ans = msg_box (NULL,
840                             _("The selected keyring has the read-only file\n"                             _("The selected keyring has the read-only file\n"
# Line 790  gpg_check_permissions (int showmsg) Line 850  gpg_check_permissions (int showmsg)
850                  }                  }
851              }              }
852              else if (ans == IDNO) {              else if (ans == IDNO) {
853                  /*                  /* 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);  
                 */  
854                  failed = 1;                  failed = 1;
855              }              }
856          }          }
# Line 809  gpg_check_permissions (int showmsg) Line 866  gpg_check_permissions (int showmsg)
866              failed = 2;              failed = 2;
867          }          }
868      }      }
869        free_if_alloc (p);
870      free_if_alloc (name);      free_if_alloc (name);
871      return failed;      return failed;
872  } /* gpg_check_permissions */  }
873    
874    
875  /* Check the GPG home dir. If all methods failed, try to  /* Check the GPG home dir. First try to read the 'HomeDir' registry entry,
876     create the default folder. */     then check for $APPDATA\gnupg. Create the dir if it does not exists. */
877  static int  int
878  check_homedir (void)  gnupg_check_homedir (void)
879  {        {      
880      char *homedir = NULL;      char *homedir = NULL;
881      int yes = 0;      int val = 0;
882        int rc = 0;
883    
884      homedir = get_reg_entry_gpg ("HomeDir");      homedir = get_reg_entry_gpg (GPG_REG_HOME);
     if (!homedir)  
         homedir = multi_gnupg_path ();  
885      if (!homedir)      if (!homedir)
886          homedir = m_strdup ("c:\\gnupg");          homedir = multi_gnupg_path (0);
887      if (homedir) {      if (homedir) {
888          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
889              yes = log_box (_("Preferences"), MB_YESNO,              val = log_box (_("Preferences"), MB_YESNO,
890                             _("%s does not exit.\n"                             _("%s does not exit.\n"
891                               "Do you want to create this directory?"), homedir);                               "Do you want to create this directory?"), homedir);
892              if (yes == IDYES) {              if (val == IDYES) {
893                  BOOL ec = CreateDirectory (homedir, NULL);                  if (!CreateDirectory (homedir, NULL))
894                  free_if_alloc (homedir);                      rc = WPTERR_DIR_CREAT;
                 if (ec == FALSE)  
                     return WPTERR_DIR_CREAT;  
                 return 0;  
895              }              }
896              return WPTERR_DIR_OPEN;              else
897          }                  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;  
898          }          }
899          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;  
         }  
900      }      }
       
     /* 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);  
901      return rc;      return rc;
902  } /* gnupg_check_homedir */  }
903    
904    
905  int  int
# Line 912  gnupg_copy_keyrings (void) Line 915  gnupg_copy_keyrings (void)
915          return WPTERR_GENERAL;          return WPTERR_GENERAL;
916      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
917    
918      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),
919                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);
920      if (!pring) {      if (!pring) {
921          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
# Line 932  gnupg_copy_keyrings (void) Line 935  gnupg_copy_keyrings (void)
935      }      }
936      free_if_alloc (file);      free_if_alloc (file);
937    
938      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),
939                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);
940      if (!sring) {      if (!sring) {
941          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
# Line 956  fail: Line 959  fail:
959  } /* gnupg_import_keyrings */  } /* gnupg_import_keyrings */
960    
961    
962    /* Backup the gpg.conf file. */
963  void  void
964  gnupg_backup_options (void)  gnupg_backup_options (void)
965  {  {
# Line 963  gnupg_backup_options (void) Line 967  gnupg_backup_options (void)
967      char bak[512];      char bak[512];
968    
969      cfgfile = get_gnupg_cfgfile ();      cfgfile = get_gnupg_cfgfile ();
970      if (cfgfile == NULL)      if (!cfgfile)
971          return;          return;
972      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
973      CopyFile (cfgfile, bak, FALSE);      CopyFile (cfgfile, bak, FALSE);
974      free_if_alloc (cfgfile);      free_if_alloc (cfgfile);
975  } /* gnupg_backup_options */  }
   
976    
977    
978  static int  static int
# Line 997  backup_one_file (const char *srcpath, co Line 1000  backup_one_file (const char *srcpath, co
1000  } /* backup_one_file */  } /* backup_one_file */
1001    
1002    
1003    /* Figure out first public keyring which is not empty.
1004       Return value: 1 on success. */
1005  static int  static int
1006  check_keyring (char ** r_path)  check_keyring (char **r_path)
1007  {  {
1008      char * p;      char *p;
1009      char * opt, * name;      char *opt;
1010        char *name;
1011    
1012      if (!*r_path)      if (!*r_path)
1013          return 0;          return 0;
1014      p = make_filename (*r_path, "pubring", "gpg");      p = make_filename (*r_path, "pubring", "gpg");
1015      if (!p || get_file_size (p) > 0)      if (!p || get_file_size (p) <= 0)
1016          return 0;          return 0;
1017    
1018      opt = get_gnupg_cfgfile ();      opt = get_gnupg_cfgfile ();
# Line 1018  check_keyring (char ** r_path) Line 1024  check_keyring (char ** r_path)
1024      if (!name)      if (!name)
1025          return 0;          return 0;
1026      p = strrchr (name, '\\');      p = strrchr (name, '\\');
1027      if (!p)      if (!p) {
     {  
1028          free_if_alloc (name);          free_if_alloc (name);
1029          return 0;                return 0;      
1030      }      }
# Line 1032  check_keyring (char ** r_path) Line 1037  check_keyring (char ** r_path)
1037  }  }
1038    
1039    
1040    /* Return a temp name based on the day of the week. */
1041  static char*  static char*
1042  get_backup_name (const char *templ)  get_backup_name (const char *templ)
1043  {  {
1044      struct tm *tm;      struct tm *tm;
1045      char *p;      char *p;
1046        time_t t;
1047    
1048      time_t t = time (NULL);      t = time (NULL);
1049      tm = localtime (&t);      tm = localtime (&t);
1050      p = new char [strlen (templ) + 8 + 1];      p = new char [strlen (templ) + 8 + 1];
1051      if (!p)      if (!p)
# Line 1048  get_backup_name (const char *templ) Line 1055  get_backup_name (const char *templ)
1055  }  }
1056    
1057    
1058    /* Make backups of all keyrings. The public key ring is
1059       rotated like this pubring-%d.gpg. */
1060  void  void
1061  gnupg_backup_keyrings (void)  gnupg_backup_keyrings (void)
1062  {  {
# Line 1058  gnupg_backup_keyrings (void) Line 1067  gnupg_backup_keyrings (void)
1067      if (!reg_prefs.auto_backup)      if (!reg_prefs.auto_backup)
1068          return;          return;
1069      bakmode = reg_prefs.backup.mode;      bakmode = reg_prefs.backup.mode;
1070      srcpath =  get_gnupg_path ();      srcpath = get_gnupg_path ();
1071      check_keyring (&srcpath);      check_keyring (&srcpath);
1072      if (bakmode == 1) {      if (bakmode == 1) {
1073          dstpath = get_gnupg_path ();          dstpath = multi_gnupg_path (1);
1074          check_keyring (&dstpath);          check_keyring (&dstpath);
1075      }      }
1076      else if (bakmode == 2) {      else if (bakmode == 2) {
1077          char * tmpfile;          char *tmpfile;
1078          FILE * fp;          FILE *fp;
1079    
1080          dstpath = m_strdup (reg_prefs.backup.path);          dstpath = m_strdup (reg_prefs.backup.path);
1081          if (!dstpath)          if (!dstpath)
# Line 1080  gnupg_backup_keyrings (void) Line 1089  gnupg_backup_keyrings (void)
1089          else {          else {
1090              rc = 0;              rc = 0;
1091              fclose (fp);              fclose (fp);
1092              unlink (tmpfile);              remove (tmpfile);
1093          }          }
1094          free_if_alloc (tmpfile);          free_if_alloc (tmpfile);
1095          if (!fp || rc == IDCANCEL)          if (!fp || rc == IDCANCEL)
# Line 1097  gnupg_backup_keyrings (void) Line 1106  gnupg_backup_keyrings (void)
1106      free_if_alloc (name);      free_if_alloc (name);
1107      free_if_alloc (srcpath);      free_if_alloc (srcpath);
1108      free_if_alloc (dstpath);      free_if_alloc (dstpath);
1109  } /* gnupg_backup_keyrings */  }
1110    
1111    
1112  /* Display GPG error from file if possible. */  /* Display GPG error from file if possible. */
# Line 1115  gnupg_display_error (void) Line 1124  gnupg_display_error (void)
1124          return;          return;
1125      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1126      if (!fp) {      if (!fp) {
1127          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1128                     _("GPG Error"), MB_INFO);
1129          return;          return;
1130      }      }
1131      errstr = new char[size+1];      errstr = new char[size+1];

Legend:
Removed from v.42  
changed lines
  Added in v.137

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26