/[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 138 by twoaday, Mon Jan 9 14:20:00 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 326  check_gnupg_prog (void) Line 325  check_gnupg_prog (void)
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 354  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 need_major = 0, need_minor = 0, need_patch = 0;
364      int rc = 1;      int rc = 1;
365            
366        /* Convert the needed GPG version to the integer format. */
367        if (parse_version_nr (need_gpg_ver,
368                              &need_major, &need_minor, &need_patch))
369            return 1;
370        
371      gpgme_new (&ctx);      gpgme_new (&ctx);
372      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
373      if (!inf) {      if (!inf) {
# Line 375  check_gnupg_engine (int *r_major, int *r Line 381  check_gnupg_engine (int *r_major, int *r
381      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
382          idea_available = 1;          idea_available = 1;
383      free (eng);      free (eng);
384      rc = parse_version_nr (inf->version, &major, &minor, &patch);      if (parse_version_nr (inf->version, &major, &minor, &patch)) {
     if (rc) {  
385          gpgme_release (ctx);          gpgme_release (ctx);
386          return rc;          return 1;
387      }      }
388    
389      if (major > *r_major)      if (major > need_major)
390          rc = 0;          rc = 0;
391      else if (major == *r_major && minor > *r_minor)                else if (major == need_major && minor > need_minor)      
392          rc = 0;          rc = 0;
393      else if (major == *r_major && minor == *r_minor &&      else if (major == need_major && minor == need_minor &&
394               patch >= *r_patch)               patch >= need_patch)
395          rc = 0;          rc = 0;
396    
397        /* Return the current GPG version. */
398      *r_major = major;      *r_major = major;
399      *r_minor = minor;      *r_minor = minor;
400      *r_patch = patch;      *r_patch = patch;
# Line 424  check_gnupg_cfgfile (const char *fname, Line 430  check_gnupg_cfgfile (const char *fname,
430  } /* check_gnupg_cfgfile */  } /* check_gnupg_cfgfile */
431    
432    
433  /*  /* Usually GPG creates the pubring.gpg, secring.gpg on
434   * Check if both keyrings are located in the gnupg home directory.     the first start, but to make sure they always exist
435   */     create them empty if needed. */
436    static void
437    create_empty_keyring (int _pub)
438    {
439        char *name;
440        FILE *f;
441    
442        name = get_gnupg_keyring (_pub, 0);
443        if (file_exist_check (name) != 0) {
444            f = fopen (name, "ab");
445            if (f != NULL)
446                fclose (f);
447        }
448        free_if_alloc (name);
449    }
450    
451    
452    /* Check if both keyrings are located in the gnupg home directory. */
453  int  int
454  gnupg_access_files (void)  gnupg_access_files (void)
455  {  {
# Line 435  gnupg_access_files (void) Line 458  gnupg_access_files (void)
458      int secrings = 0, pubrings = 0;      int secrings = 0, pubrings = 0;
459      char *optfile;      char *optfile;
460    
461        create_empty_keyring (1);
462      if (gnupg_access_keyring (1))      if (gnupg_access_keyring (1))
463          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
464      else      else
465          pubring_ok = 1;          pubring_ok = 1;
466    
467        create_empty_keyring (0);
468      if (gnupg_access_keyring (0))      if (gnupg_access_keyring (0))
469          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
470      else      else
# Line 468  gnupg_access_files (void) Line 493  gnupg_access_files (void)
493          rc = WPTERR_GPG_KEYRINGS;          rc = WPTERR_GPG_KEYRINGS;
494      }      }
495      return rc;      return rc;
496  } /* gnupg_access_files */  }
497    
498    
499  static int  static int
# Line 481  create_gpg_options (void) Line 506  create_gpg_options (void)
506      if( s == NULL )      if( s == NULL )
507          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
508      optfile = make_filename (s, GPG_CONF, NULL);      optfile = make_filename (s, GPG_CONF, NULL);
509      fp = fopen( optfile, "wb" );      fp = fopen (optfile, "wb");
510      if( fp == NULL ) {        if (fp == NULL) {  
511          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
512          goto fail;          goto fail;
513      }      }
514      fwrite( options_skel, 1, strlen( options_skel ), fp );      fwrite (options_skel, 1, strlen (options_skel), fp);
515      fclose( fp );      fclose (fp);
516    
517  fail:  fail:
518      free_if_alloc( s );      free_if_alloc (s);
519      free_if_alloc( optfile );      free_if_alloc (optfile);
520      return 0;      return 0;
521  } /* create_gpg_options */  } /* create_gpg_options */
522    
# Line 782  get_gnupg_keyring_from_options (const ch Line 807  get_gnupg_keyring_from_options (const ch
807    
808  /* XXX: does not work with write-protected floppies */  /* XXX: does not work with write-protected floppies */
809  static int  static int
810  my_access (const char * fname)  my_access (const char *fname)
811  {  {
812      HANDLE hd;      HANDLE hd;
813      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
# Line 801  my_access (const char * fname) Line 826  my_access (const char * fname)
826  int  int
827  gpg_check_permissions (int showmsg)  gpg_check_permissions (int showmsg)
828  {  {
829      char * p, * name = NULL;      char *p = NULL;
830        char *name = NULL;
831      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
832    
833      p = get_gnupg_path ();      p = get_gnupg_path ();
834      check_keyring (&p);      if (check_keyring (&p) && p) {
     if (p) {  
835          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
         free_if_alloc (p);  
836          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
837              ans = msg_box (NULL,              ans = msg_box (NULL,
838                             _("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 864  gpg_check_permissions (int showmsg)
864              failed = 2;              failed = 2;
865          }          }
866      }      }
867        free_if_alloc (p);
868      free_if_alloc (name);      free_if_alloc (name);
869      return failed;      return failed;
870  }  }
871    
872    
873  /* Check the GPG home dir. If all methods failed, try to  /* Check the GPG home dir. First try to read the 'HomeDir' registry entry,
874     create the default folder. */     then check for $APPDATA\gnupg. Create the dir if it does not exists. */
875  static int  int
876  check_homedir (void)  gnupg_check_homedir (void)
877  {        {      
878      char *homedir = NULL;      char *homedir = NULL;
879      int yes = 0, set_reg=0;      int val = 0;
880      int rc = 0;      int rc = 0;
881    
882      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);  
     }  
883      if (!homedir)      if (!homedir)
884          homedir = m_strdup ("c:\\gnupg");          homedir = multi_gnupg_path (0);
885      if (homedir) {      if (homedir) {
886          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
887              yes = log_box (_("Preferences"), MB_YESNO,              val = log_box (_("Preferences"), MB_YESNO,
888                             _("%s does not exit.\n"                             _("%s does not exit.\n"
889                               "Do you want to create this directory?"), homedir);                               "Do you want to create this directory?"), homedir);
890              if (yes == IDYES) {              if (val == IDYES) {
891                  if (!CreateDirectory (homedir, NULL))                  if (!CreateDirectory (homedir, NULL))
892                      rc = WPTERR_DIR_CREAT;                      rc = WPTERR_DIR_CREAT;
893              }              }
894              else              else
895                  rc = WPTERR_DIR_OPEN;                  rc = WPTERR_DIR_OPEN;
896          }          }
         if (set_reg)  
             set_reg_entry_gpg (GPG_REG_HOME, homedir);  
897          free_if_alloc (homedir);          free_if_alloc (homedir);
898      }      }
899      return rc;      return rc;
# Line 882  check_homedir (void) Line 901  check_homedir (void)
901    
902    
903  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  
904  gnupg_copy_keyrings (void)  gnupg_copy_keyrings (void)
905  {  {
906      const char * pring, * sring;      const char * pring, * sring;
# Line 990  fail: Line 957  fail:
957  } /* gnupg_import_keyrings */  } /* gnupg_import_keyrings */
958    
959    
960    /* Backup the gpg.conf file. */
961  void  void
962  gnupg_backup_options (void)  gnupg_backup_options (void)
963  {  {
# Line 997  gnupg_backup_options (void) Line 965  gnupg_backup_options (void)
965      char bak[512];      char bak[512];
966    
967      cfgfile = get_gnupg_cfgfile ();      cfgfile = get_gnupg_cfgfile ();
968      if (cfgfile == NULL)      if (!cfgfile)
969          return;          return;
970      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
971      CopyFile (cfgfile, bak, FALSE);      CopyFile (cfgfile, bak, FALSE);
972      free_if_alloc (cfgfile);      free_if_alloc (cfgfile);
973  } /* gnupg_backup_options */  }
   
974    
975    
976  static int  static int
# Line 1031  backup_one_file (const char *srcpath, co Line 998  backup_one_file (const char *srcpath, co
998  } /* backup_one_file */  } /* backup_one_file */
999    
1000    
1001    /* Figure out first public keyring which is not empty.
1002       Return value: 1 on success. */
1003  static int  static int
1004  check_keyring (char ** r_path)  check_keyring (char **r_path)
1005  {  {
1006      char * p;      char *p;
1007      char * opt, * name;      char *opt;
1008        char *name;
1009    
1010      if (!*r_path)      if (!*r_path)
1011          return 0;          return 0;
1012      p = make_filename (*r_path, "pubring", "gpg");      p = make_filename (*r_path, "pubring", "gpg");
1013      if (!p || get_file_size (p) > 0)      if (!p || get_file_size (p) <= 0)
1014          return 0;          return 0;
1015    
1016      opt = get_gnupg_cfgfile ();      opt = get_gnupg_cfgfile ();
# Line 1052  check_keyring (char ** r_path) Line 1022  check_keyring (char ** r_path)
1022      if (!name)      if (!name)
1023          return 0;          return 0;
1024      p = strrchr (name, '\\');      p = strrchr (name, '\\');
1025      if (!p)      if (!p) {
     {  
1026          free_if_alloc (name);          free_if_alloc (name);
1027          return 0;                return 0;      
1028      }      }
# Line 1066  check_keyring (char ** r_path) Line 1035  check_keyring (char ** r_path)
1035  }  }
1036    
1037    
1038    /* Return a temp name based on the day of the week. */
1039  static char*  static char*
1040  get_backup_name (const char *templ)  get_backup_name (const char *templ)
1041  {  {
1042      struct tm *tm;      struct tm *tm;
1043      char *p;      char *p;
1044        time_t t;
1045    
1046      time_t t = time (NULL);      t = time (NULL);
1047      tm = localtime (&t);      tm = localtime (&t);
1048      p = new char [strlen (templ) + 8 + 1];      p = new char [strlen (templ) + 8 + 1];
1049      if (!p)      if (!p)
# Line 1151  gnupg_display_error (void) Line 1122  gnupg_display_error (void)
1122          return;          return;
1123      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1124      if (!fp) {      if (!fp) {
1125          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1126                     _("GPG Error"), MB_INFO);
1127          return;          return;
1128      }      }
1129      errstr = new char[size+1];      errstr = new char[size+1];

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26