/[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 199 by twoaday, Mon Apr 10 07:38:06 2006 UTC revision 200 by twoaday, Mon Apr 17 09:12:50 2006 UTC
# Line 1  Line 1 
1  /* wptGPG.cpp - GnuPG configuration  /* wptGPG.cpp - GnuPG configuration
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 115  get_gnupg_cfgfile (void) Line 115  get_gnupg_cfgfile (void)
115      path = get_gnupg_path ();      path = get_gnupg_path ();
116      if (!path)      if (!path)
117          return NULL;          return NULL;
118      p = get_reg_entry_gpg ("OptFile");      nlen = strlen (path) + 64;
119      if (p) {      optfile = new char[nlen + 1];
120          nlen = strlen (p) + 4;      if (!optfile)
121          optfile = new char[nlen + 1];          BUG (NULL);    
122          if (!optfile)      _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);
123              BUG (NULL);  
         _snprintf (optfile, nlen, "%s", p);  
     }  
     else {  
         nlen = strlen (path) + 64;  
         optfile = new char[nlen + 1];  
         if( !optfile)  
             BUG (NULL);  
         _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);  
     }  
124      free_if_alloc (path);      free_if_alloc (path);
125      free_if_alloc (p);      free_if_alloc (p);
126      return optfile;      return optfile;
# Line 214  default_key_from_cache (int *ret_no_usea Line 205  default_key_from_cache (int *ret_no_usea
205      while (!gpg_keycache_next_key (sec, 1, &key)) {      while (!gpg_keycache_next_key (sec, 1, &key)) {
206          if (key_is_useable (key) && !get_pubkey (key->subkeys->keyid, &pk)) {          if (key_is_useable (key) && !get_pubkey (key->subkeys->keyid, &pk)) {
207              s = key->subkeys->keyid;              s = key->subkeys->keyid;
208              if (s)                  if (s)
209                  keyid = m_strdup (s+8);                  keyid = m_strdup (s+8);
210              break;              break;
211          }          }
# Line 234  gnupg_load_config (void) Line 225  gnupg_load_config (void)
225      int rc;      int rc;
226      gpg_optfile_t opt;      gpg_optfile_t opt;
227      gpg_option_t o;      gpg_option_t o;
228      char *conf = get_gnupg_cfgfile ();      char *conf;
229        
230        conf = get_gnupg_cfgfile ();
231      if (!conf)      if (!conf)
232          return -1;          return -1;
233      rc = parse_gpg_options (conf, &opt);      rc = parse_gpg_options (conf, &opt);
# Line 271  get_gnupg_default_key (void) Line 264  get_gnupg_default_key (void)
264          return default_key_from_cache (&no_usable);          return default_key_from_cache (&no_usable);
265      }      }
266      e = find_option (opt, "default-key");      e = find_option (opt, "default-key");
267        if (!e)
268            e = find_option (opt, "local-user");
269      if (e)      if (e)
270          keyid = m_strdup (e->val);          keyid = m_strdup (e->val);
     if (!e) {  
         e = find_option (opt, "local-user");  
         if (e)  
             keyid = m_strdup (e->val);  
     }  
     if (!e) {  
         e = find_option (opt, "encrypt-to");  
         if (e)  
             keyid = m_strdup (e->val);  
     }  
     free_if_alloc (optfile);  
     release_gpg_options (opt);      
271    
272        free_if_alloc (optfile);
273        release_gpg_options (opt);
274      if (!keyid)      if (!keyid)
275          keyid = default_key_from_cache (&no_usable);          keyid = default_key_from_cache (&no_usable);
276      return keyid;      return keyid;
# Line 332  parse_version_nr (const char *buf, int * Line 317  parse_version_nr (const char *buf, int *
317      while (buf && *buf != '.' && i < 8)      while (buf && *buf != '.' && i < 8)
318          tmp[i++] = *buf++;          tmp[i++] = *buf++;
319      tmp[i] = 0; buf++;      tmp[i] = 0; buf++;
320      *major = atol( tmp );      *major = atoi (tmp);
321      i=0;      i=0;
322      while (buf && *buf != '.' && i < 8)      while (buf && *buf != '.' && i < 8)
323          tmp[i++] = *buf++;          tmp[i++] = *buf++;
324      tmp[i] = 0; buf++;      tmp[i] = 0; buf++;
325      *minor = atol (tmp);      *minor = atoi (tmp);
326      i=0;      i=0;
327      while (buf && isdigit (*buf) && i < 8)      while (buf && isdigit (*buf) && i < 8)
328          tmp[i++] = *buf++;          tmp[i++] = *buf++;
329      tmp[i] = 0;      tmp[i] = 0;
330      *patch = atol (tmp);      *patch = atoi (tmp);
331      return 0;      return 0;
332  }  }
333    
# Line 378  check_gnupg_engine (const char *need_gpg Line 363  check_gnupg_engine (const char *need_gpg
363          return -1;          return -1;
364      if (strstr (eng, "IDEA"))      if (strstr (eng, "IDEA"))
365          idea_available = 1;          idea_available = 1;
366      free (eng);      safe_free (eng);
367      if (parse_version_nr (inf->version, &major, &minor, &patch)) {      if (parse_version_nr (inf->version, &major, &minor, &patch)) {
368          gpgme_release (ctx);          gpgme_release (ctx);
369          return 1;          return 1;
370      }      }
371        gpgme_release (ctx);
372    
373      if (major > need_major)      if (major > need_major)
374          rc = 0;          rc = 0;
# Line 405  check_gnupg_cfgfile (const char *fname, Line 391  check_gnupg_cfgfile (const char *fname,
391  {  {
392      gpg_optfile_t opt;          gpg_optfile_t opt;    
393      gpg_option_t e;      gpg_option_t e;
     int rc = 0;  
394    
395      *r_secrings = 0;      *r_secrings = 0;
396      *r_pubrings = 0;      *r_pubrings = 0;
397      rc = parse_gpg_options( fname, &opt );      if (parse_gpg_options (fname, &opt))
     if( rc )  
398          return WPTERR_FILE_OPEN;          return WPTERR_FILE_OPEN;
399    
400      for( e = opt->list; e; e = e->next ) {      for (e = opt->list; e; e = e->next) {
401          if( !strcmp( e->name, "secret-keyring" ) ) {          if (!strcmp( e->name, "secret-keyring")) {
402              if( !file_exist_check( e->val ) )              if (!file_exist_check (e->val))
403                  r_secrings[0]++;                  r_secrings[0]++;
404          }          }
405          else if( !strcmp( e->name, "keyring" ) ) {          else if (!strcmp (e->name, "keyring")) {
406              if( !file_exist_check( e->val ) )              if (!file_exist_check (e->val))
407                  r_pubrings[0]++;                  r_pubrings[0]++;
408          }          }
409      }      }
410      release_gpg_options( opt );      release_gpg_options (opt);
411      return 0;      return 0;
412  } /* check_gnupg_cfgfile */  }
413    
414    
415  /* Usually GPG creates the pubring.gpg, secring.gpg on  /* Usually GPG creates the pubring.gpg, secring.gpg on
# Line 435  static void Line 419  static void
419  create_empty_keyring (int _pub)  create_empty_keyring (int _pub)
420  {  {
421      char *name;      char *name;
422      FILE *f;      FILE *fp;
423    
424      name = get_gnupg_keyring (_pub, 0);      name = get_gnupg_keyring (_pub, 0);
425      if (file_exist_check (name) != 0) {      if (name && file_exist_check (name) != 0) {
426          f = fopen (name, "ab");          fp = fopen (name, "ab");
427          if (f != NULL)          if (fp != NULL)
428              fclose (f);              fclose (fp);
429      }      }
430      free_if_alloc (name);      free_if_alloc (name);
431  }  }
# Line 475  gnupg_access_files (void) Line 459  gnupg_access_files (void)
459          rc = file_exist_check (optfile);          rc = file_exist_check (optfile);
460          if (!rc && get_file_size (optfile) > 0) {          if (!rc && get_file_size (optfile) > 0) {
461              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);
462              if (!rc && secrings && pubrings) {              if (!rc && secrings > 0 && pubrings > 0) {
463                  free_if_alloc (optfile);                  free_if_alloc (optfile);
464                  return 0; /* found two keyrings in the option file */                  return 0; /* found two keyrings in the option file */
465              }              }
# Line 501  create_gpg_options (void) Line 485  create_gpg_options (void)
485      char *s, *optfile;      char *s, *optfile;
486    
487      s = get_gnupg_path ();      s = get_gnupg_path ();
488      if( s == NULL )      if (!s)
489          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
490      optfile = make_filename (s, GPG_CONF, NULL);      optfile = make_filename (s, GPG_CONF, NULL);
491      fp = fopen (optfile, "wb");      fp = fopen (optfile, "wb");
492      if (fp == NULL) {        if (!fp) {
493          return WPTERR_FILE_CREAT;          return WPTERR_FILE_CREAT;
494          goto fail;          goto fail;
495      }      }
# Line 516  fail: Line 500  fail:
500      free_if_alloc (s);      free_if_alloc (s);
501      free_if_alloc (optfile);      free_if_alloc (optfile);
502      return 0;      return 0;
503  } /* create_gpg_options */  }
504    
505    
506  /*  /* Return the contents of the options file as a char buf. */
507   * Return the contents of the options file as a char buf.  char*
  */  
 char *  
508  get_gnupg_config (void)  get_gnupg_config (void)
509  {  {
510      FILE * fp;      FILE *fp;
511      char * p = NULL, * optfile = NULL;      char *p = NULL, *optfile = NULL;
512      int fsize, rc = 0;      int fsize, rc = 0;
513                    
514      optfile = get_gnupg_cfgfile ();      optfile = get_gnupg_cfgfile ();
515      if( optfile == NULL )      if (optfile == NULL)
516          return NULL;          return NULL;
517      fsize = get_file_size( optfile );      fsize = get_file_size (optfile);
518      if( !fsize ) {      if (!fsize) {
519          rc = create_gpg_options( );          rc = create_gpg_options ();
520          if ( rc )          if (rc)
521              return NULL;              return NULL;
522          fsize = get_file_size( optfile );          fsize = get_file_size (optfile);
523      }      }
524      if( fsize > 100000 )      if (fsize > 100000)
525          goto leave; /* too large */          goto leave; /* too large */
526      p = new char[fsize+1];      p = new char[fsize+1];
527      if( p == NULL )      if (!p)
528          BUG( NULL );          BUG (NULL);
529      fp = fopen( optfile, "rb" );      fp = fopen( optfile, "rb" );
530      if( fp == NULL ) {      if (!fp) {
531          free_if_alloc( p );          free_if_alloc (p);
532          return NULL;          return NULL;
533      }      }
534      fread( p, 1, fsize, fp );      fread (p, 1, fsize, fp);
535      fclose( fp );      fclose (fp);
536      p[fsize] = '\0';      p[fsize] = '\0';
537      free_if_alloc( optfile );      free_if_alloc (optfile);
538    
539  leave:  leave:
540      return p;      return p;
541  } /* get_gnupg_config */  }
542    
543    
544  /* Set the default key in the gpg.conf.  /* Set the default key in the gpg.conf.
# Line 596  set_gnupg_default_key (const char *key) Line 578  set_gnupg_default_key (const char *key)
578  }  }
579    
580    
581  /*  /* Set the contents of the options file. */
  * Set the contents of the options file.  
  */  
582  int  int
583  set_gnupg_options( const char *buf, size_t buflen )  set_gnupg_options( const char *buf, size_t buflen )
584  {  {
# Line 675  get_last_gnupg_access (gpg_watcher_s *ct Line 655  get_last_gnupg_access (gpg_watcher_s *ct
655    
656      path = get_gnupg_path ();      path = get_gnupg_path ();
657      file =  make_filename (path, ctx->object, NULL);      file =  make_filename (path, ctx->object, NULL);
658      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL,
659                         OPEN_ALWAYS, 0, NULL);
660      if (fd == INVALID_HANDLE_VALUE) {      if (fd == INVALID_HANDLE_VALUE) {
661          free_if_alloc (path);          free_if_alloc (path);
662          free_if_alloc (file);          free_if_alloc (file);
# Line 833  gpg_check_permissions (int showmsg) Line 814  gpg_check_permissions (int showmsg)
814      int failed = 0, ans=0, attrs=0;      int failed = 0, ans=0, attrs=0;
815    
816      p = get_gnupg_path ();      p = get_gnupg_path ();
817      if (check_keyring (&p) && p) {      if (p && check_keyring (&p)) {
818          name = make_filename (p, "pubring", "gpg");          name = make_filename (p, "pubring", "gpg");
819          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
820              ans = msg_box (NULL,              ans = msg_box (NULL,
# Line 915  gnupg_copy_keyrings (void) Line 896  gnupg_copy_keyrings (void)
896          return WPTERR_GENERAL;          return WPTERR_GENERAL;
897      hwnd = GetDesktopWindow ();      hwnd = GetDesktopWindow ();
898    
899      pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"),      pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"),
900                                "GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL);
901      if (!pring) {      if (!pring) {
902          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);          msg_box (hwnd, _("No keyring was chosen. Exit."),
903                     _("WinPT Error"), MB_ERR);
904          free_if_alloc (path);          free_if_alloc (path);
905          return WPTERR_GENERAL;          return WPTERR_GENERAL;
906      }      }
907      file = make_filename (path, "pubring", "gpg");      file = make_filename (path, "pubring", "gpg");
908      if (file_exist_check (file) == 0) {      if (file_exist_check (file) == 0) {
909          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);          id = msg_box (hwnd, _("Overwrite old public keyring?"),
910                          "WinPT", MB_INFO|MB_YESNO);
911          if (id == IDNO)          if (id == IDNO)
912              goto fail;              goto fail;
913      }      }
# Line 935  gnupg_copy_keyrings (void) Line 918  gnupg_copy_keyrings (void)
918      }      }
919      free_if_alloc (file);      free_if_alloc (file);
920    
921      sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"),      sring = get_fileopen_dlg (hwnd, _("Please choose your Secret Keyring"),
922                                "GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL);                                "GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL);
923      if (!sring) {      if (!sring) {
924          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );          msg_box (NULL, _("No keyring was chosen. Exit."),
925                     _("WinPT Error"), MB_ERR);
926          return WPTERR_GENERAL;          return WPTERR_GENERAL;
927      }      }
928      file = make_filename (path, "secring", "gpg");      file = make_filename (path, "secring", "gpg");
929      if (file_exist_check (file) == 0) {      if (file_exist_check (file) == 0) {
930          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);          id = msg_box (hwnd, _("Overwrite old secret keyring?"),
931          if( id == IDNO )                        "WinPT", MB_INFO|MB_YESNO);
932            if (id == IDNO)
933              goto fail;              goto fail;
934      }      }
935      if (!CopyFile (sring, file, FALSE)) {      if (!CopyFile (sring, file, FALSE)) {
936          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);          msg_box (NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);
937          rc = WPTERR_FILE_READ;          rc = WPTERR_FILE_READ;
938      }      }
939    
# Line 956  fail: Line 941  fail:
941      free_if_alloc (file);      free_if_alloc (file);
942      free_if_alloc (path);      free_if_alloc (path);
943      return rc;      return rc;
944  } /* gnupg_import_keyrings */  }
945    
946    
947  /* Backup the gpg.conf file. */  /* Backup the gpg.conf file. */
# Line 979  static int Line 964  static int
964  backup_one_file (const char *srcpath, const char *srcn,  backup_one_file (const char *srcpath, const char *srcn,
965                   const char *dstpath, const char *dstn)                   const char *dstpath, const char *dstn)
966  {  {
967      char * src, * dst;      char *src, *dst;
968      BOOL rc;      BOOL rc;
969    
970      src = make_filename (srcpath, srcn, "gpg");      src = make_filename (srcpath, srcn, "gpg");
# Line 991  backup_one_file (const char *srcpath, co Line 976  backup_one_file (const char *srcpath, co
976      rc = CopyFile (src, dst, FALSE);      rc = CopyFile (src, dst, FALSE);
977      free_if_alloc (src);      free_if_alloc (src);
978      free_if_alloc (dst);      free_if_alloc (dst);
979      if (!rc)      if (!rc) {
     {  
980          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);
981          return WPTERR_GENERAL;          return WPTERR_GENERAL;
982      }      }
983      return 0;      return 0;
984  } /* backup_one_file */  }
985    
986    
987  /* Figure out first public keyring which is not empty.  /* Figure out first public keyring which is not empty.
# Line 1058  get_backup_name (const char *templ) Line 1042  get_backup_name (const char *templ)
1042  /* Make backups of all keyrings. The public key ring is  /* Make backups of all keyrings. The public key ring is
1043     rotated like this pubring-%d.gpg. */     rotated like this pubring-%d.gpg. */
1044  void  void
1045  gnupg_backup_keyrings (void)  gnupg_backup_keyrings (int auto_backup, int backup_mode)
1046  {  {
1047      char *srcpath = NULL, *dstpath = NULL;      char *srcpath = NULL, *dstpath = NULL;
1048      char *name=NULL;      char *name=NULL;
1049      int rc, bakmode=0;      int rc;
1050    
1051      if (!reg_prefs.auto_backup)      if (!auto_backup)
1052          return;          return;
     bakmode = reg_prefs.backup.mode;  
1053      srcpath = get_gnupg_path ();      srcpath = get_gnupg_path ();
1054      check_keyring (&srcpath);      check_keyring (&srcpath);
1055      if (bakmode == 1) {      if (backup_mode == 1) {
1056          dstpath = multi_gnupg_path (1);          dstpath = multi_gnupg_path (1);
1057          check_keyring (&dstpath);          check_keyring (&dstpath);
1058      }      }
1059      else if (bakmode == 2) {      else if (backup_mode == 2) {
1060          char *tmpfile;          char *tmpfile;
1061          FILE *fp;          FILE *fp;
1062    
# Line 1096  gnupg_backup_keyrings (void) Line 1079  gnupg_backup_keyrings (void)
1079              return;              return;
1080      }      }
1081      else {      else {
1082          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode);          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode);
1083          return;          return;
1084      }      }
1085      name = get_backup_name ("pubring-bak");      name = get_backup_name ("pubring-bak");

Legend:
Removed from v.199  
changed lines
  Added in v.200

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26