/[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 117 by twoaday, Thu Dec 8 09:26:32 2005 UTC revision 133 by twoaday, Mon Jan 9 09:15:29 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 159  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);
161            return keyring;
162        }
163        else if (!strict) {
164          free_if_alloc (path);          free_if_alloc (path);
165          return keyring;          return keyring;
166      }      }
# Line 293  get_gnupg_default_key (void) Line 294  get_gnupg_default_key (void)
294  } /* get_gnupg_default_key */  } /* get_gnupg_default_key */
295    
296    
 char* get_reg_entry_gpg4win (const char *path);  
   
297  /* Check if GPG4WIN is available and if so, use the  /* Check if GPG4WIN is available and if so, use the
298     install path to figure out where the gpg.exe is. */     install path to figure out where the gpg.exe is. */
299  char*  char*
# Line 424  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 435  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
# Line 468  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 481  create_gpg_options (void) Line 499  create_gpg_options (void)
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 782  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 801  my_access (const char * fname) Line 819  my_access (const char * fname)
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 840  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  }  }
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, set_reg=0;      int val = 0;
873      int rc = 0;      int rc = 0;
874    
875      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);  
     }  
876      if (!homedir)      if (!homedir)
877          homedir = m_strdup ("c:\\gnupg");          homedir = multi_gnupg_path (0);
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                  if (!CreateDirectory (homedir, NULL))                  if (!CreateDirectory (homedir, NULL))
885                      rc = WPTERR_DIR_CREAT;                      rc = WPTERR_DIR_CREAT;
886              }              }
887              else              else
888                  rc = WPTERR_DIR_OPEN;                  rc = WPTERR_DIR_OPEN;
889          }          }
         if (set_reg)  
             set_reg_entry_gpg (GPG_REG_HOME, homedir);  
890          free_if_alloc (homedir);          free_if_alloc (homedir);
891      }      }
892      return rc;      return rc;
# Line 882  check_homedir (void) Line 894  check_homedir (void)
894    
895    
896  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  
897  gnupg_copy_keyrings (void)  gnupg_copy_keyrings (void)
898  {  {
899      const char * pring, * sring;      const char * pring, * sring;
# Line 990  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 997  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 1031  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 1052  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 1066  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 1151  gnupg_display_error (void) Line 1115  gnupg_display_error (void)
1115          return;          return;
1116      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1117      if (!fp) {      if (!fp) {
1118          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1119                     _("GPG Error"), MB_INFO);
1120          return;          return;
1121      }      }
1122      errstr = new char[size+1];      errstr = new char[size+1];

Legend:
Removed from v.117  
changed lines
  Added in v.133

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26