/[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 128 by twoaday, Mon Dec 19 13:05:59 2005 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 293  get_gnupg_default_key (void) Line 290  get_gnupg_default_key (void)
290  } /* get_gnupg_default_key */  } /* get_gnupg_default_key */
291    
292    
 char* get_reg_entry_gpg4win (const char *path);  
   
293  /* Check if GPG4WIN is available and if so, use the  /* Check if GPG4WIN is available and if so, use the
294     install path to figure out where the gpg.exe is. */     install path to figure out where the gpg.exe is. */
295  char*  char*
# Line 671  check_last_gnupg_access (gpg_watcher_s * Line 666  check_last_gnupg_access (gpg_watcher_s *
666      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
667          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
668          ctx->modified = 1;          ctx->modified = 1;
669        
670        /* XXX: find a better way. without it, winpt --keymanager loads
671                the key cache twice. */
672        if (ctx->last_access.dwLowDateTime == 0)
673            ctx->modified = 0;
674    
675      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
676      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
# Line 777  get_gnupg_keyring_from_options (const ch Line 777  get_gnupg_keyring_from_options (const ch
777    
778  /* XXX: does not work with write-protected floppies */  /* XXX: does not work with write-protected floppies */
779  static int  static int
780  my_access (const char * fname)  my_access (const char *fname)
781  {  {
782      HANDLE hd;      HANDLE hd;
783      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
# Line 796  my_access (const char * fname) Line 796  my_access (const char * fname)
796  int  int
797  gpg_check_permissions (int showmsg)  gpg_check_permissions (int showmsg)
798  {  {
799      char * p, * name = NULL;      char *p = NULL;
800        char *name = NULL;
801      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
802    
803      p = get_gnupg_path ();      p = get_gnupg_path ();
804      check_keyring (&p);      if (check_keyring (&p) && p) {
     if (p) {  
805          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
         free_if_alloc (p);  
806          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
807              ans = msg_box (NULL,              ans = msg_box (NULL,
808                             _("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 834  gpg_check_permissions (int showmsg)
834              failed = 2;              failed = 2;
835          }          }
836      }      }
837        free_if_alloc (p);
838      free_if_alloc (name);      free_if_alloc (name);
839      return failed;      return failed;
840  }  }
841    
842    
843  /* Check the GPG home dir. If all methods failed, try to  /* Check the GPG home dir. First try to read the 'HomeDir' registry entry,
844     create the default folder. */     then check for $APPDATA\gnupg. Create the dir if it does not exists. */
845  static int  int
846  check_homedir (void)  gnupg_check_homedir (void)
847  {        {      
848      char *homedir = NULL;      char *homedir = NULL;
849      int yes = 0, set_reg=0;      int val = 0;
850      int rc = 0;      int rc = 0;
851    
852      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);  
     }  
853      if (!homedir)      if (!homedir)
854          homedir = m_strdup ("c:\\gnupg");          homedir = multi_gnupg_path (0);
855      if (homedir) {      if (homedir) {
856          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
857              yes = log_box (_("Preferences"), MB_YESNO,              val = log_box (_("Preferences"), MB_YESNO,
858                             _("%s does not exit.\n"                             _("%s does not exit.\n"
859                               "Do you want to create this directory?"), homedir);                               "Do you want to create this directory?"), homedir);
860              if (yes == IDYES) {              if (val == IDYES) {
861                  if (!CreateDirectory (homedir, NULL))                  if (!CreateDirectory (homedir, NULL))
862                      rc = WPTERR_DIR_CREAT;                      rc = WPTERR_DIR_CREAT;
863              }              }
864              else              else
865                  rc = WPTERR_DIR_OPEN;                  rc = WPTERR_DIR_OPEN;
866          }          }
         if (set_reg)  
             set_reg_entry_gpg (GPG_REG_HOME, homedir);  
867          free_if_alloc (homedir);          free_if_alloc (homedir);
868      }      }
869      return rc;      return rc;
# Line 877  check_homedir (void) Line 871  check_homedir (void)
871    
872    
873  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  
874  gnupg_copy_keyrings (void)  gnupg_copy_keyrings (void)
875  {  {
876      const char * pring, * sring;      const char * pring, * sring;
# Line 985  fail: Line 927  fail:
927  } /* gnupg_import_keyrings */  } /* gnupg_import_keyrings */
928    
929    
930    /* Backup the gpg.conf file. */
931  void  void
932  gnupg_backup_options (void)  gnupg_backup_options (void)
933  {  {
# Line 992  gnupg_backup_options (void) Line 935  gnupg_backup_options (void)
935      char bak[512];      char bak[512];
936    
937      cfgfile = get_gnupg_cfgfile ();      cfgfile = get_gnupg_cfgfile ();
938      if (cfgfile == NULL)      if (!cfgfile)
939          return;          return;
940      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
941      CopyFile (cfgfile, bak, FALSE);      CopyFile (cfgfile, bak, FALSE);
942      free_if_alloc (cfgfile);      free_if_alloc (cfgfile);
943  } /* gnupg_backup_options */  }
   
944    
945    
946  static int  static int
# Line 1026  backup_one_file (const char *srcpath, co Line 968  backup_one_file (const char *srcpath, co
968  } /* backup_one_file */  } /* backup_one_file */
969    
970    
971    /* Figure out first public keyring which is not empty.
972       Return value: 1 on success. */
973  static int  static int
974  check_keyring (char ** r_path)  check_keyring (char **r_path)
975  {  {
976      char * p;      char *p;
977      char * opt, * name;      char *opt;
978        char *name;
979    
980      if (!*r_path)      if (!*r_path)
981          return 0;          return 0;
982      p = make_filename (*r_path, "pubring", "gpg");      p = make_filename (*r_path, "pubring", "gpg");
983      if (!p || get_file_size (p) > 0)      if (!p || get_file_size (p) <= 0)
984          return 0;          return 0;
985    
986      opt = get_gnupg_cfgfile ();      opt = get_gnupg_cfgfile ();
# Line 1047  check_keyring (char ** r_path) Line 992  check_keyring (char ** r_path)
992      if (!name)      if (!name)
993          return 0;          return 0;
994      p = strrchr (name, '\\');      p = strrchr (name, '\\');
995      if (!p)      if (!p) {
     {  
996          free_if_alloc (name);          free_if_alloc (name);
997          return 0;                return 0;      
998      }      }
# Line 1061  check_keyring (char ** r_path) Line 1005  check_keyring (char ** r_path)
1005  }  }
1006    
1007    
1008    /* Return a temp name based on the day of the week. */
1009  static char*  static char*
1010  get_backup_name (const char *templ)  get_backup_name (const char *templ)
1011  {  {
1012      struct tm *tm;      struct tm *tm;
1013      char *p;      char *p;
1014        time_t t;
1015    
1016      time_t t = time (NULL);      t = time (NULL);
1017      tm = localtime (&t);      tm = localtime (&t);
1018      p = new char [strlen (templ) + 8 + 1];      p = new char [strlen (templ) + 8 + 1];
1019      if (!p)      if (!p)
# Line 1077  get_backup_name (const char *templ) Line 1023  get_backup_name (const char *templ)
1023  }  }
1024    
1025    
1026    /* Make backups of all keyrings. The public key ring is
1027       rotated like this pubring-%d.gpg. */
1028  void  void
1029  gnupg_backup_keyrings (void)  gnupg_backup_keyrings (void)
1030  {  {
# Line 1087  gnupg_backup_keyrings (void) Line 1035  gnupg_backup_keyrings (void)
1035      if (!reg_prefs.auto_backup)      if (!reg_prefs.auto_backup)
1036          return;          return;
1037      bakmode = reg_prefs.backup.mode;      bakmode = reg_prefs.backup.mode;
1038      srcpath =  get_gnupg_path ();      srcpath = get_gnupg_path ();
1039      check_keyring (&srcpath);      check_keyring (&srcpath);
1040      if (bakmode == 1) {      if (bakmode == 1) {
1041          dstpath = get_gnupg_path ();          dstpath = multi_gnupg_path (1);
1042          check_keyring (&dstpath);          check_keyring (&dstpath);
1043      }      }
1044      else if (bakmode == 2) {      else if (bakmode == 2) {
1045          char * tmpfile;          char *tmpfile;
1046          FILE * fp;          FILE *fp;
1047    
1048          dstpath = m_strdup (reg_prefs.backup.path);          dstpath = m_strdup (reg_prefs.backup.path);
1049          if (!dstpath)          if (!dstpath)
# Line 1126  gnupg_backup_keyrings (void) Line 1074  gnupg_backup_keyrings (void)
1074      free_if_alloc (name);      free_if_alloc (name);
1075      free_if_alloc (srcpath);      free_if_alloc (srcpath);
1076      free_if_alloc (dstpath);      free_if_alloc (dstpath);
1077  } /* gnupg_backup_keyrings */  }
1078    
1079    
1080  /* Display GPG error from file if possible. */  /* Display GPG error from file if possible. */
# Line 1144  gnupg_display_error (void) Line 1092  gnupg_display_error (void)
1092          return;          return;
1093      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1094      if (!fp) {      if (!fp) {
1095          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1096                     _("GPG Error"), MB_INFO);
1097          return;          return;
1098      }      }
1099      errstr = new char[size+1];      errstr = new char[size+1];

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26