/[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 73 by twoaday, Tue Nov 8 07:15:13 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*
300  check_for_gpg4win (void)  check_for_gpg4win (void)
301  {  {
302      return get_reg_entry_gpg4win ("\\gpg.exe");      return get_reg_entry_gpg4win ("gpg.exe");
303  }  }
304    
305    
# Line 342  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 358  check_gnupg_engine (int *r_major, int *r Line 357  check_gnupg_engine (int *r_major, int *r
357  {  {
358      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
359      gpgme_engine_info_t inf;      gpgme_engine_info_t inf;
360      char * eng = NULL;      char *eng = NULL;
361      int major=0, minor=0, patch=0;      int major=0, minor=0, patch=0;
362      int rc;      int rc = 1;
363                    
364      gpgme_new (&ctx);      gpgme_new (&ctx);
365      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
# Line 368  check_gnupg_engine (int *r_major, int *r Line 367  check_gnupg_engine (int *r_major, int *r
367          gpgme_release (ctx);          gpgme_release (ctx);
368          return -1;          return -1;
369      }      }
370    
371      /* 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. */
372      if (gpg_get_version (&eng))      if (gpg_get_version (&eng))
373          return -1;          return -1;
374      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
375          idea_available = 1;          idea_available = 1;
376      free (eng);      free (eng);
377      rc = parse_version_nr( inf->version, &major, &minor, &patch );      rc = parse_version_nr (inf->version, &major, &minor, &patch);
378      if( rc ) {      if (rc) {
379          gpgme_release (ctx);          gpgme_release (ctx);
380          return rc;          return rc;
381      }      }
382      /* FIXME: This check is wrong! */  
383      if (major < *r_major || minor < *r_minor)      if (major > *r_major)
         rc = 1;  
     else {  
         if (patch < *r_patch)  
             rc = 1;  
384          rc = 0;          rc = 0;
385      }      else if (major == *r_major && minor > *r_minor)          
386            rc = 0;
387        else if (major == *r_major && minor == *r_minor &&
388                 patch >= *r_patch)
389            rc = 0;
390    
391      *r_major = major;      *r_major = major;
392      *r_minor = minor;      *r_minor = minor;
393      *r_patch = patch;      *r_patch = patch;
# Line 422  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 433  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 466  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 479  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 669  check_last_gnupg_access (gpg_watcher_s * Line 689  check_last_gnupg_access (gpg_watcher_s *
689      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
690          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
691          ctx->modified = 1;          ctx->modified = 1;
692        
693        /* XXX: find a better way. without it, winpt --keymanager loads
694                the key cache twice. */
695        if (ctx->last_access.dwLowDateTime == 0)
696            ctx->modified = 0;
697    
698      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
699      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
# Line 775  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 787  my_access (const char * fname) Line 812  my_access (const char * fname)
812  }  }
813    
814    
815    /* Check the file permissions of the public keyring.
816       If @showmsg is 1 output a message in case of errors.
817       Return value: 1 if read-only attribute
818                     2 if file is opened by another process exclusively. */
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 813  gpg_check_permissions (int showmsg) Line 841  gpg_check_permissions (int showmsg)
841                  }                  }
842              }              }
843              else if (ans == IDNO) {              else if (ans == IDNO) {
844                  /*                  /* 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);  
                 */  
845                  failed = 1;                  failed = 1;
846              }              }
847          }          }
# Line 832  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  } /* gpg_check_permissions */  }
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 874  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 938  gnupg_copy_keyrings (void) Line 906  gnupg_copy_keyrings (void)
906          return WPTERR_GENERAL;          return WPTERR_GENERAL;
907      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
908    
909      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),
910                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);
911      if (!pring) {      if (!pring) {
912          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 958  gnupg_copy_keyrings (void) Line 926  gnupg_copy_keyrings (void)
926      }      }
927      free_if_alloc (file);      free_if_alloc (file);
928    
929      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),
930                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);
931      if (!sring) {      if (!sring) {
932          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 982  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 989  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 1023  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 1044  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 1058  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 1074  get_backup_name (const char *templ) Line 1046  get_backup_name (const char *templ)
1046  }  }
1047    
1048    
1049    /* Make backups of all keyrings. The public key ring is
1050       rotated like this pubring-%d.gpg. */
1051  void  void
1052  gnupg_backup_keyrings (void)  gnupg_backup_keyrings (void)
1053  {  {
# Line 1084  gnupg_backup_keyrings (void) Line 1058  gnupg_backup_keyrings (void)
1058      if (!reg_prefs.auto_backup)      if (!reg_prefs.auto_backup)
1059          return;          return;
1060      bakmode = reg_prefs.backup.mode;      bakmode = reg_prefs.backup.mode;
1061      srcpath =  get_gnupg_path ();      srcpath = get_gnupg_path ();
1062      check_keyring (&srcpath);      check_keyring (&srcpath);
1063      if (bakmode == 1) {      if (bakmode == 1) {
1064          dstpath = get_gnupg_path ();          dstpath = multi_gnupg_path (1);
1065          check_keyring (&dstpath);          check_keyring (&dstpath);
1066      }      }
1067      else if (bakmode == 2) {      else if (bakmode == 2) {
1068          char * tmpfile;          char *tmpfile;
1069          FILE * fp;          FILE *fp;
1070    
1071          dstpath = m_strdup (reg_prefs.backup.path);          dstpath = m_strdup (reg_prefs.backup.path);
1072          if (!dstpath)          if (!dstpath)
# Line 1123  gnupg_backup_keyrings (void) Line 1097  gnupg_backup_keyrings (void)
1097      free_if_alloc (name);      free_if_alloc (name);
1098      free_if_alloc (srcpath);      free_if_alloc (srcpath);
1099      free_if_alloc (dstpath);      free_if_alloc (dstpath);
1100  } /* gnupg_backup_keyrings */  }
1101    
1102    
1103  /* Display GPG error from file if possible. */  /* Display GPG error from file if possible. */
# Line 1141  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.73  
changed lines
  Added in v.133

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26