/[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 179 by twoaday, Fri Feb 24 13:12:26 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 293  get_gnupg_default_key (void) Line 287  get_gnupg_default_key (void)
287  } /* get_gnupg_default_key */  } /* get_gnupg_default_key */
288    
289    
 char* get_reg_entry_gpg4win (const char *path);  
   
290  /* Check if GPG4WIN is available and if so, use the  /* Check if GPG4WIN is available and if so, use the
291     install path to figure out where the gpg.exe is. */     install path to figure out where the gpg.exe is. */
292  char*  char*
# Line 326  check_gnupg_prog (void) Line 318  check_gnupg_prog (void)
318    
319    
320  static int  static int
321  parse_version_nr (const char * buf, int *major, int *minor, int *patch)  parse_version_nr (const char *buf, int *major, int *minor, int *patch)
322  {  {
323      char tmp[8];      char tmp[8];
324      int i;      int i;
# Line 354  parse_version_nr (const char * buf, int Line 346  parse_version_nr (const char * buf, int
346     version given in @r_major.@r_minor.@r_patch. On success these     version given in @r_major.@r_minor.@r_patch. On success these
347     variables contain the GPG version which is installed. */     variables contain the GPG version which is installed. */
348  int  int
349  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)  check_gnupg_engine (const char *need_gpg_ver,
350                        int *r_major, int *r_minor, int *r_patch)
351  {  {
352      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
353      gpgme_engine_info_t inf;      gpgme_engine_info_t inf;
354      char *eng = NULL;      char *eng = NULL;
355      int major=0, minor=0, patch=0;      int major=0, minor=0, patch=0;
356        int need_major = 0, need_minor = 0, need_patch = 0;
357      int rc = 1;      int rc = 1;
358            
359        /* Convert the needed GPG version to the integer format. */
360        if (parse_version_nr (need_gpg_ver,
361                              &need_major, &need_minor, &need_patch))
362            return 1;
363        
364      gpgme_new (&ctx);      gpgme_new (&ctx);
365      inf = gpgme_ctx_get_engine_info (ctx);      inf = gpgme_ctx_get_engine_info (ctx);
366      if (!inf) {      if (!inf) {
# Line 375  check_gnupg_engine (int *r_major, int *r Line 374  check_gnupg_engine (int *r_major, int *r
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);      if (parse_version_nr (inf->version, &major, &minor, &patch)) {
     if (rc) {  
378          gpgme_release (ctx);          gpgme_release (ctx);
379          return rc;          return 1;
380      }      }
381    
382      if (major > *r_major)      if (major > need_major)
383          rc = 0;          rc = 0;
384      else if (major == *r_major && minor > *r_minor)                else if (major == need_major && minor > need_minor)      
385          rc = 0;          rc = 0;
386      else if (major == *r_major && minor == *r_minor &&      else if (major == need_major && minor == need_minor &&
387               patch >= *r_patch)               patch >= need_patch)
388          rc = 0;          rc = 0;
389    
390        /* Return the current GPG version. */
391      *r_major = major;      *r_major = major;
392      *r_minor = minor;      *r_minor = minor;
393      *r_patch = patch;      *r_patch = patch;
# 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 536  leave: Line 554  leave:
554  } /* get_gnupg_config */  } /* get_gnupg_config */
555    
556    
557    /* Set the default key in the gpg.conf.
558       If @key is NULL, the entry will be deleted. */
559  int  int
560  set_gnupg_default_key (const char * key)  set_gnupg_default_key (const char *key)
561  {  {
562      gpg_optfile_t opt;      gpg_optfile_t opt;
563      gpg_option_t e;      gpg_option_t e;
# Line 546  set_gnupg_default_key (const char * key) Line 566  set_gnupg_default_key (const char * key)
566    
567      optfile = get_gnupg_cfgfile ();      optfile = get_gnupg_cfgfile ();
568      if (!optfile)      if (!optfile)
569          return -1;          return WPTERR_FILE_OPEN;
570      rc = parse_gpg_options (optfile, &opt);      rc = parse_gpg_options (optfile, &opt);
571      if( rc ) {      if (rc) {
572          free_if_alloc (optfile);          free_if_alloc (optfile);
573          return -1;          return WPTERR_GENERAL;
574      }      }
575      e = find_option (opt, "default-key");      e = find_option (opt, "default-key");
576      if (e) {      if (e && !key)
577            e->used = 0;
578        else if (e) {
579          free_if_alloc (e->val);          free_if_alloc (e->val);
580          e->val = m_strdup (key);          e->val = m_strdup (key);
581          e->used = 1;          e->used = 1;
# Line 566  set_gnupg_default_key (const char * key) Line 588  set_gnupg_default_key (const char * key)
588      release_gpg_options (opt);      release_gpg_options (opt);
589    
590      return rc;      return rc;
591  } /* set_gnupg_default_key */  }
592    
593    
594  /*  /*
# Line 782  get_gnupg_keyring_from_options (const ch Line 804  get_gnupg_keyring_from_options (const ch
804    
805  /* XXX: does not work with write-protected floppies */  /* XXX: does not work with write-protected floppies */
806  static int  static int
807  my_access (const char * fname)  my_access (const char *fname)
808  {  {
809      HANDLE hd;      HANDLE hd;
810      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
# Line 801  my_access (const char * fname) Line 823  my_access (const char * fname)
823  int  int
824  gpg_check_permissions (int showmsg)  gpg_check_permissions (int showmsg)
825  {  {
826      char * p, * name = NULL;      char *p = NULL;
827        char *name = NULL;
828      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
829    
830      p = get_gnupg_path ();      p = get_gnupg_path ();
831      check_keyring (&p);      if (check_keyring (&p) && p) {
     if (p) {  
832          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
         free_if_alloc (p);  
833          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
834              ans = msg_box (NULL,              ans = msg_box (NULL,
835                             _("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 861  gpg_check_permissions (int showmsg)
861              failed = 2;              failed = 2;
862          }          }
863      }      }
864        free_if_alloc (p);
865      free_if_alloc (name);      free_if_alloc (name);
866      return failed;      return failed;
867  }  }
868    
869    
870  /* Check the GPG home dir. If all methods failed, try to  /* Check the GPG home dir. First try to read the 'HomeDir' registry entry,
871     create the default folder. */     then check for $APPDATA\gnupg. Create the dir if it does not exists. */
872  static int  int
873  check_homedir (void)  gnupg_check_homedir (void)
874  {        {      
875      char *homedir = NULL;      char *homedir = NULL;
876      int yes = 0, set_reg=0;      int val = 0;
877      int rc = 0;      int rc = 0;
878    
879      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);  
     }  
880      if (!homedir)      if (!homedir)
881          homedir = m_strdup ("c:\\gnupg");          homedir = multi_gnupg_path (0);
882      if (homedir) {      if (homedir) {
883          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
884              yes = log_box (_("Preferences"), MB_YESNO,              val = log_box (_("Preferences"), MB_YESNO,
885                             _("%s does not exit.\n"                             _("%s does not exit.\n"
886                               "Do you want to create this directory?"), homedir);                               "Do you want to create this directory?"), homedir);
887              if (yes == IDYES) {              if (val == IDYES) {
888                  if (!CreateDirectory (homedir, NULL))                  if (!CreateDirectory (homedir, NULL))
889                      rc = WPTERR_DIR_CREAT;                      rc = WPTERR_DIR_CREAT;
890              }              }
891              else              else
892                  rc = WPTERR_DIR_OPEN;                  rc = WPTERR_DIR_OPEN;
893          }          }
         if (set_reg)  
             set_reg_entry_gpg (GPG_REG_HOME, homedir);  
894          free_if_alloc (homedir);          free_if_alloc (homedir);
895      }      }
896      return rc;      return rc;
# Line 882  check_homedir (void) Line 898  check_homedir (void)
898    
899    
900  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  
901  gnupg_copy_keyrings (void)  gnupg_copy_keyrings (void)
902  {  {
903      const char * pring, * sring;      const char * pring, * sring;
# Line 947  gnupg_copy_keyrings (void) Line 911  gnupg_copy_keyrings (void)
911      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
912    
913      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),
914                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL);
915      if (!pring) {      if (!pring) {
916          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
917          free_if_alloc (path);          free_if_alloc (path);
# Line 967  gnupg_copy_keyrings (void) Line 931  gnupg_copy_keyrings (void)
931      free_if_alloc (file);      free_if_alloc (file);
932    
933      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),
934                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL);
935      if (!sring) {      if (!sring) {
936          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
937          return WPTERR_GENERAL;          return WPTERR_GENERAL;
# Line 990  fail: Line 954  fail:
954  } /* gnupg_import_keyrings */  } /* gnupg_import_keyrings */
955    
956    
957    /* Backup the gpg.conf file. */
958  void  void
959  gnupg_backup_options (void)  gnupg_backup_options (void)
960  {  {
# Line 997  gnupg_backup_options (void) Line 962  gnupg_backup_options (void)
962      char bak[512];      char bak[512];
963    
964      cfgfile = get_gnupg_cfgfile ();      cfgfile = get_gnupg_cfgfile ();
965      if (cfgfile == NULL)      if (!cfgfile)
966          return;          return;
967      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
968      CopyFile (cfgfile, bak, FALSE);      CopyFile (cfgfile, bak, FALSE);
969      free_if_alloc (cfgfile);      free_if_alloc (cfgfile);
970  } /* gnupg_backup_options */  }
   
971    
972    
973  static int  static int
# Line 1031  backup_one_file (const char *srcpath, co Line 995  backup_one_file (const char *srcpath, co
995  } /* backup_one_file */  } /* backup_one_file */
996    
997    
998    /* Figure out first public keyring which is not empty.
999       Return value: 1 on success. */
1000  static int  static int
1001  check_keyring (char ** r_path)  check_keyring (char **r_path)
1002  {  {
1003      char * p;      char *p;
1004      char * opt, * name;      char *opt;
1005        char *name;
1006    
1007      if (!*r_path)      if (!*r_path)
1008          return 0;          return 0;
1009      p = make_filename (*r_path, "pubring", "gpg");      p = make_filename (*r_path, "pubring", "gpg");
1010      if (!p || get_file_size (p) > 0)      if (!p || get_file_size (p) <= 0)
1011          return 0;          return 0;
1012    
1013      opt = get_gnupg_cfgfile ();      opt = get_gnupg_cfgfile ();
# Line 1052  check_keyring (char ** r_path) Line 1019  check_keyring (char ** r_path)
1019      if (!name)      if (!name)
1020          return 0;          return 0;
1021      p = strrchr (name, '\\');      p = strrchr (name, '\\');
1022      if (!p)      if (!p) {
     {  
1023          free_if_alloc (name);          free_if_alloc (name);
1024          return 0;                return 0;      
1025      }      }
# Line 1066  check_keyring (char ** r_path) Line 1032  check_keyring (char ** r_path)
1032  }  }
1033    
1034    
1035    /* Return a temp name based on the day of the week. */
1036  static char*  static char*
1037  get_backup_name (const char *templ)  get_backup_name (const char *templ)
1038  {  {
1039      struct tm *tm;      struct tm *tm;
1040      char *p;      char *p;
1041        time_t t;
1042    
1043      time_t t = time (NULL);      t = time (NULL);
1044      tm = localtime (&t);      tm = localtime (&t);
1045      p = new char [strlen (templ) + 8 + 1];      p = new char [strlen (templ) + 8 + 1];
1046      if (!p)      if (!p)
# Line 1140  gnupg_backup_keyrings (void) Line 1108  gnupg_backup_keyrings (void)
1108  void  void
1109  gnupg_display_error (void)  gnupg_display_error (void)
1110  {        {      
1111      char tmpath[512], * errstr;      char tmpath[512], *errstr;
1112      size_t size = 0;      size_t size = 0;
1113      FILE * fp;      FILE *fp;
1114    
1115      GetTempPath (sizeof tmpath - 32, (tmpath));      get_temp_name (tmpath, sizeof (tmpath), "gpg_stderr");
     strcat (tmpath, "gpg_stderr");  
1116      size = get_file_size (tmpath);      size = get_file_size (tmpath);
1117      if (file_exist_check (tmpath) || size <= 0)      if (file_exist_check (tmpath) || size <= 0)
1118          return;          return;
1119      fp = fopen( tmpath, "rb" );      fp = fopen( tmpath, "rb" );
1120      if (!fp) {      if (!fp) {
1121          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );          msg_box (NULL, _("No GPG error description available."),
1122                     _("GPG Error"), MB_INFO);
1123          return;          return;
1124      }      }
1125      errstr = new char[size+1];      errstr = new char[size+1];

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26