/[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 78 by twoaday, Tue Nov 15 08:54:44 2005 UTC revision 197 by twoaday, Mon Apr 10 07:38:06 2006 UTC
# Line 62  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 (int strict)  multi_gnupg_path (int strict)
67  {  {
68      static char buf[256+64];      static char buf[256+64];
# Line 84  multi_gnupg_path (int strict) Line 84  multi_gnupg_path (int strict)
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;      char *path;
93      char *path = NULL;  
94            path = get_reg_entry_gpg (GPG_REG_HOME);
95      p = get_reg_entry_gpg (GPG_REG_HOME);      if (path) {
96      if (p) {          if (dir_exist_check (path) == 0)
97          path = m_strdup (p);              return path;
98          free_if_alloc (p);          free_if_alloc (path);
         return path;  
99      }      }
100      else      path = multi_gnupg_path (1);
101          return multi_gnupg_path (1);      return path;
     return m_strdup ("c:\\gnupg");  
102  }  }
103    
104    
# Line 119  get_gnupg_cfgfile (void) Line 116  get_gnupg_cfgfile (void)
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 159  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 206  get_gnupg_prog (void) Line 200  get_gnupg_prog (void)
200     Return value: the keyid of the secret key. */     Return value: the keyid of the secret key. */
201  static char *  static char *
202  default_key_from_cache (int *ret_no_useable)  default_key_from_cache (int *ret_no_useable)
203  {  {    
204        gpgme_key_t key, pk;
205        gpg_keycache_t sec, pub;
206      const char *s;      const char *s;
207      char *keyid = NULL;      char *keyid = NULL;
     gpgme_key_t key;  
     gpg_keycache_t sec = keycache_get_ctx (0);  
208    
209        sec = keycache_get_ctx (0);
210      if (!sec)      if (!sec)
211          BUG (0);          BUG (0);
212        pub = keycache_get_ctx (1);
213      gpg_keycache_rewind (sec);      gpg_keycache_rewind (sec);
214      while (!gpg_keycache_next_key (sec, 1, &key)) {      while (!gpg_keycache_next_key (sec, 1, &key)) {
215          if (key_is_useable (key)) {          if (key_is_useable (key) && !get_pubkey (key->subkeys->keyid, &pk)) {
216              s = key->subkeys->keyid;              s = key->subkeys->keyid;
217              if (s)                  if (s)    
218                  keyid = m_strdup (s+8);                  keyid = m_strdup (s+8);
219              break;              break;
220          }          }
221      }      }
222      if (!keyid)      if (!keyid)
223          *ret_no_useable = 1;          *ret_no_useable = 1;    
224      return keyid;      return keyid;
225  }  }
226    
# Line 249  gnupg_load_config (void) Line 245  gnupg_load_config (void)
245      o = find_option (opt, "ask-cert-level");      o = find_option (opt, "ask-cert-level");
246      if (o)      if (o)
247          reg_prefs.gpg.ask_cert_level = 1;          reg_prefs.gpg.ask_cert_level = 1;
248        o = find_option (opt, "ask-cert-expire");
249        if (o)
250            reg_prefs.gpg.ask_cert_expire = 1;
251      release_gpg_options (opt);      release_gpg_options (opt);
252      free_if_alloc (conf);      free_if_alloc (conf);
253      return 0;      return 0;
# Line 260  get_gnupg_default_key (void) Line 259  get_gnupg_default_key (void)
259  {      {    
260      gpg_optfile_t opt = NULL;      gpg_optfile_t opt = NULL;
261      gpg_option_t e;      gpg_option_t e;
262      char * keyid = NULL, * optfile = NULL;      char *keyid = NULL, *optfile = NULL;
263      int no_usable=0, rc = 0;      int no_usable=0, rc = 0;
264    
265      optfile = get_gnupg_cfgfile ();      optfile = get_gnupg_cfgfile ();
# Line 271  get_gnupg_default_key (void) Line 270  get_gnupg_default_key (void)
270          free_if_alloc (optfile);          free_if_alloc (optfile);
271          return default_key_from_cache (&no_usable);          return default_key_from_cache (&no_usable);
272      }      }
273      e = find_option( opt, "default-key" );      e = find_option (opt, "default-key");
274      if ( e )      if (e)
275          keyid = m_strdup( e->val );          keyid = m_strdup (e->val);
276      if( !e ) {      if (!e) {
277          e = find_option( opt, "local-user" );          e = find_option (opt, "local-user");
278          if( e )          if (e)
279              keyid = m_strdup( e->val );              keyid = m_strdup (e->val);
280      }      }
281      if( !e ) {      if (!e) {
282          e = find_option( opt, "encrypt-to" );          e = find_option (opt, "encrypt-to");
283          if( e )          if (e)
284              keyid = m_strdup( e->val );              keyid = m_strdup (e->val);
285      }      }
286      free_if_alloc (optfile);      free_if_alloc (optfile);
287      release_gpg_options (opt);      release_gpg_options (opt);    
288    
289      if (!keyid)      if (!keyid)
290          keyid = default_key_from_cache (&no_usable);          keyid = default_key_from_cache (&no_usable);
291      return keyid;      return keyid;
292  } /* get_gnupg_default_key */  }
   
293    
 char* get_reg_entry_gpg4win (const char *path);  
294    
295  /* Check if GPG4WIN is available and if so, use the  /* Check if GPG4WIN is available and if so, use the
296     install path to figure out where the gpg.exe is. */     install path to figure out where the gpg.exe is. */
# Line 326  check_gnupg_prog (void) Line 323  check_gnupg_prog (void)
323    
324    
325  static int  static int
326  parse_version_nr (const char * buf, int *major, int *minor, int *patch)  parse_version_nr (const char *buf, int *major, int *minor, int *patch)
327  {  {
328      char tmp[8];      char tmp[8];
329      int i;      int i;
# Line 354  parse_version_nr (const char * buf, int Line 351  parse_version_nr (const char * buf, int
351     version given in @r_major.@r_minor.@r_patch. On success these     version given in @r_major.@r_minor.@r_patch. On success these
352     variables contain the GPG version which is installed. */     variables contain the GPG version which is installed. */
353  int  int
354  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)  check_gnupg_engine (const char *need_gpg_ver,
355                        int *r_major, int *r_minor, int *r_patch)
356  {  {
357      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
358      gpgme_engine_info_t inf;      gpgme_engine_info_t inf;
359      char *eng = NULL;      char *eng = NULL;
360      int major=0, minor=0, patch=0;      int major=0, minor=0, patch=0;
361        int need_major = 0, need_minor = 0, need_patch = 0;
362      int rc = 1;      int rc = 1;
363            
364        /* Convert the needed GPG version to the integer format. */
365        if (parse_version_nr (need_gpg_ver,
366                              &need_major, &need_minor, &need_patch))
367            return 1;
368        
369      gpgme_new (&ctx);      gpgme_new (&ctx);
370      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
371      if (!inf) {      if (!inf) {
# Line 375  check_gnupg_engine (int *r_major, int *r Line 379  check_gnupg_engine (int *r_major, int *r
379      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
380          idea_available = 1;          idea_available = 1;
381      free (eng);      free (eng);
382      rc = parse_version_nr (inf->version, &major, &minor, &patch);      if (parse_version_nr (inf->version, &major, &minor, &patch)) {
     if (rc) {  
383          gpgme_release (ctx);          gpgme_release (ctx);
384          return rc;          return 1;
385      }      }
386    
387      if (major > *r_major)      if (major > need_major)
388          rc = 0;          rc = 0;
389      else if (major == *r_major && minor > *r_minor)                else if (major == need_major && minor > need_minor)      
390          rc = 0;          rc = 0;
391      else if (major == *r_major && minor == *r_minor &&      else if (major == need_major && minor == need_minor &&
392               patch >= *r_patch)               patch >= need_patch)
393          rc = 0;          rc = 0;
394    
395        /* Return the current GPG version. */
396      *r_major = major;      *r_major = major;
397      *r_minor = minor;      *r_minor = minor;
398      *r_patch = patch;      *r_patch = patch;
# Line 424  check_gnupg_cfgfile (const char *fname, Line 428  check_gnupg_cfgfile (const char *fname,
428  } /* check_gnupg_cfgfile */  } /* check_gnupg_cfgfile */
429    
430    
431  /*  /* Usually GPG creates the pubring.gpg, secring.gpg on
432   * Check if both keyrings are located in the gnupg home directory.     the first start, but to make sure they always exist
433   */     create them empty if needed. */
434    static void
435    create_empty_keyring (int _pub)
436    {
437        char *name;
438        FILE *f;
439    
440        name = get_gnupg_keyring (_pub, 0);
441        if (file_exist_check (name) != 0) {
442            f = fopen (name, "ab");
443            if (f != NULL)
444                fclose (f);
445        }
446        free_if_alloc (name);
447    }
448    
449    
450    /* Check if both keyrings are located in the gnupg home directory. */
451  int  int
452  gnupg_access_files (void)  gnupg_access_files (void)
453  {  {
# Line 435  gnupg_access_files (void) Line 456  gnupg_access_files (void)
456      int secrings = 0, pubrings = 0;      int secrings = 0, pubrings = 0;
457      char *optfile;      char *optfile;
458    
459        create_empty_keyring (1);
460      if (gnupg_access_keyring (1))      if (gnupg_access_keyring (1))
461          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
462      else      else
463          pubring_ok = 1;          pubring_ok = 1;
464    
465        create_empty_keyring (0);
466      if (gnupg_access_keyring (0))      if (gnupg_access_keyring (0))
467          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
468      else      else
# Line 468  gnupg_access_files (void) Line 491  gnupg_access_files (void)
491          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
492      }      }
493      return rc;      return rc;
494  } /* gnupg_access_files */  }
495    
496    
497  static int  static int
# Line 481  create_gpg_options (void) Line 504  create_gpg_options (void)
504      if( s == NULL )      if( s == NULL )
505          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
506      optfile = make_filename (s, GPG_CONF, NULL);      optfile = make_filename (s, GPG_CONF, NULL);
507      fp = fopen( optfile, "wb" );      fp = fopen (optfile, "wb");
508      if( fp == NULL ) {        if (fp == NULL) {  
509          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
510          goto fail;          goto fail;
511      }      }
512      fwrite( options_skel, 1, strlen( options_skel ), fp );      fwrite (options_skel, 1, strlen (options_skel), fp);
513      fclose( fp );      fclose (fp);
514    
515  fail:  fail:
516      free_if_alloc( s );      free_if_alloc (s);
517      free_if_alloc( optfile );      free_if_alloc (optfile);
518      return 0;      return 0;
519  } /* create_gpg_options */  } /* create_gpg_options */
520    
# Line 536  leave: Line 559  leave:
559  } /* get_gnupg_config */  } /* get_gnupg_config */
560    
561    
562    /* Set the default key in the gpg.conf.
563       If @key is NULL, the entry will be deleted. */
564  int  int
565  set_gnupg_default_key (const char * key)  set_gnupg_default_key (const char *key)
566  {  {
567      gpg_optfile_t opt;      gpg_optfile_t opt;
568      gpg_option_t e;      gpg_option_t e;
# Line 546  set_gnupg_default_key (const char * key) Line 571  set_gnupg_default_key (const char * key)
571    
572      optfile = get_gnupg_cfgfile ();      optfile = get_gnupg_cfgfile ();
573      if (!optfile)      if (!optfile)
574          return -1;          return WPTERR_FILE_OPEN;
575      rc = parse_gpg_options (optfile, &opt);      rc = parse_gpg_options (optfile, &opt);
576      if( rc ) {      if (rc) {
577          free_if_alloc (optfile);          free_if_alloc (optfile);
578          return -1;          return WPTERR_GENERAL;
579      }      }
580      e = find_option (opt, "default-key");      e = find_option (opt, "default-key");
581      if (e) {      if (e && !key)
582            e->used = 0;
583        else if (e) {
584          free_if_alloc (e->val);          free_if_alloc (e->val);
585          e->val = m_strdup (key);          e->val = m_strdup (key);
586          e->used = 1;          e->used = 1;
587      }      }
588      else      else if (key)
589          add_entry (opt, ENTRY_MULTI, "default-key", key);          add_entry (opt, ENTRY_MULTI, "default-key", key);
590      rc = commit_gpg_options (optfile, opt);      rc = commit_gpg_options (optfile, opt);
591    
# Line 566  set_gnupg_default_key (const char * key) Line 593  set_gnupg_default_key (const char * key)
593      release_gpg_options (opt);      release_gpg_options (opt);
594    
595      return rc;      return rc;
596  } /* set_gnupg_default_key */  }
597    
598    
599  /*  /*
# Line 671  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 777  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 796  my_access (const char * fname) Line 828  my_access (const char * fname)
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 835  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  }  }
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, set_reg=0;      int val = 0;
882      int rc = 0;      int rc = 0;
883    
884      homedir = get_reg_entry_gpg (GPG_REG_HOME);      homedir = get_reg_entry_gpg (GPG_REG_HOME);
     if (!homedir) {  
         set_reg = 1;  
         homedir = multi_gnupg_path (0);  
     }  
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                  if (!CreateDirectory (homedir, NULL))                  if (!CreateDirectory (homedir, NULL))
894                      rc = WPTERR_DIR_CREAT;                      rc = WPTERR_DIR_CREAT;
895              }              }
896              else              else
897                  rc = WPTERR_DIR_OPEN;                  rc = WPTERR_DIR_OPEN;
898          }          }
         if (set_reg)  
             set_reg_entry_gpg (GPG_REG_HOME, homedir);  
899          free_if_alloc (homedir);          free_if_alloc (homedir);
900      }      }
901      return rc;      return rc;
# Line 877  check_homedir (void) Line 903  check_homedir (void)
903    
904    
905  int  int
 gnupg_check_homedir (void)  
 {        
     char *homedir = NULL;  
     char *prog = NULL;  
     int rc = 0;  
       
     rc = check_homedir ();  
     if (rc)  
         return rc;  
     if ((homedir = get_reg_entry_gpg (GPG_REG_HOME)) &&  
         !(prog = get_reg_entry_gpg (GPG_REG_EXE ))) {  
         prog = make_filename (homedir, "gpg", "exe");  
         if (file_exist_check (prog) == 0) {  
             rc = set_reg_entry_gpg (GPG_REG_EXE, prog);  
             if (rc)  
                 goto fail;  
         }  
         free_if_alloc (homedir);  
         free_if_alloc (prog);  
         return rc;  
     }  
     if ((prog = get_reg_entry_gpg (GPG_REG_EXE))  
         && file_exist_check (prog)) {  
         free_if_alloc (prog);  
         homedir = get_reg_entry_gpg (GPG_REG_HOME);  
         if (!homedir) {  
             rc = WPTERR_GENERAL;  
             goto fail;  
         }  
         prog = make_filename (homedir, "gpg", "exe");  
         if (file_exist_check (prog) == 0) {  
             rc = set_reg_entry_gpg (GPG_REG_EXE, prog);  
             if (rc)  
                 goto fail;  
             free_if_alloc (prog);  
             return rc;  
         }  
     }  
       
     /* 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);  
     return rc;  
 } /* gnupg_check_homedir */  
   
   
 int  
906  gnupg_copy_keyrings (void)  gnupg_copy_keyrings (void)
907  {  {
908      const char * pring, * sring;      const char * pring, * sring;
# Line 942  gnupg_copy_keyrings (void) Line 916  gnupg_copy_keyrings (void)
916      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
917    
918      pring = get_fileopen_dlg (hwnd, _("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);
922          free_if_alloc (path);          free_if_alloc (path);
# Line 962  gnupg_copy_keyrings (void) Line 936  gnupg_copy_keyrings (void)
936      free_if_alloc (file);      free_if_alloc (file);
937    
938      sring = get_fileopen_dlg (hwnd, _("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 );
942          return WPTERR_GENERAL;          return WPTERR_GENERAL;
# Line 985  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 992  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 1026  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 1047  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 1061  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 1077  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 1087  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 1126  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. */
1113  void  void
1114  gnupg_display_error (void)  gnupg_display_error (void)
1115  {        {      
1116      char tmpath[512], * errstr;      char tmpath[512], *errstr;
1117      size_t size = 0;      size_t size = 0;
1118      FILE * fp;      FILE *fp;
1119    
1120      GetTempPath (sizeof tmpath - 32, (tmpath));      get_temp_name (tmpath, sizeof (tmpath), "gpg_stderr");
     strcat (tmpath, "gpg_stderr");  
1121      size = get_file_size (tmpath);      size = get_file_size (tmpath);
1122      if (file_exist_check (tmpath) || size <= 0)      if (file_exist_check (tmpath) || size <= 0)
1123          return;          return;
1124      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1125      if (!fp) {      if (!fp) {
1126          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1127                     _("GPG Error"), MB_INFO);
1128          return;          return;
1129      }      }
1130      errstr = new char[size+1];      errstr = new char[size+1];

Legend:
Removed from v.78  
changed lines
  Added in v.197

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26