/[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 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 41 by twoaday, Fri Oct 28 07:15:26 2005 UTC
# Line 1  Line 1 
1  /* wptGPG.cpp - GnuPG configuration  /* wptGPG.cpp - GnuPG configuration
2   *      Copyright (C) 2001-2004 Timo Schulz   *      Copyright (C) 2001-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or   * WinPT is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License   * modify it under the terms of the GNU General Public License
8   * as published by the Free Software Foundation; either version 2   * as published by the Free Software Foundation; either version 2
9   * of the License, or (at your option) any later version.   * of the License, or (at your option) any later version.
10   *     *  
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * General Public License for more details.   * General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   */   */
20    #include <windows.h>
21  #include <string.h>  #include <string.h>
22  #include <stdio.h>  #include <stdio.h>
23  #include <windows.h>  #include <shlobj.h>
24  #include <shlobj.h>  #include <ctype.h>
25  #include <ctype.h>  #include <io.h>
26  #include <io.h>  #include <time.h>
27    
28  #include "wptGPG.h"  #include "wptGPG.h"
29  #include "wptGPGCmds.h"  #include "wptGPGCmds.h"
30  #include "wptGPGOptSkel.h"  #include "wptGPGOptSkel.h"
31  #include "wptTypes.h"  #include "wptTypes.h"
32  #include "wptNLS.h"  #include "wptNLS.h"
33  #include "wptRegistry.h"  #include "wptRegistry.h"
34  #include "wptErrors.h"  #include "wptErrors.h"
35  #include "wptW32API.h"  #include "wptW32API.h"
36    #include "wptCrypto.h"
37  #define GPG_CONF "gpg.conf"  
38    #define GPG_CONF "gpg.conf"
39  struct gpg_watcher_s {  
40      FILETIME last_access;  struct gpg_watcher_s {
41      FILETIME access;      FILETIME    last_access;
42      char * object;      FILETIME    access;
43      int modified;      char        *object;
44  };      int         modified;
45    };
46  /* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */  
47  static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"};  
48  static gpg_watcher_s gpg_table[3];  /* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */
49  static int gpg_table_count = DIM (gpg_table);  static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"};
50    static gpg_watcher_s gpg_table[3];
51  int idea_available = 0;  static int gpg_table_count = DIM (gpg_table);
52    
53  static int check_keyring (char ** r_path);  int idea_available = 0;
54    
55    static int check_keyring (char ** r_path);
56  static char*  
57  multi_gnupg_path (void)  
58  {  /* Return the application data folder of the current user. */
59      static char buf[256+64];  static char*
60      BOOL ec;  multi_gnupg_path (void)
61    {
62      /* MSDN: buf must be at least MAX_PATH=256 bytes */      static char buf[256+64];
63      memset (buf, 0, sizeof (buf));      BOOL ec;
64      ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE);  
65      if (ec != 1)      /* MSDN: buf must be at least MAX_PATH=256 bytes */
66          return NULL;      memset (buf, 0, sizeof (buf));
67      strcat (buf, "\\gnupg");      /* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */
68      if (access (buf, 00))      ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE);
69          return NULL;      if (ec != 1)
70      return m_strdup (buf);          return NULL;
71  }      strcat (buf, "\\gnupg");
72        if (access (buf, 00))
73  /*          return NULL;
74   * Return the full path of the GnuPG application. First the registry is scanned      return m_strdup (buf);
75   * for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used.  }
76   */  
77  char*  
78  get_gnupg_path (void)  /* Return the full path of the GnuPG application. First the registry is scanned
79  {     for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used.
80      char *p = NULL, *path = NULL;  */
81        char*
82      p = get_reg_entry_gpg ("HomeDir");  get_gnupg_path (void)
83      if (p) {  {
84          path = m_strdup (p);      char *p = NULL, *path = NULL;
85          free_if_alloc (p);      
86          return path;      p = get_reg_entry_gpg ("HomeDir");
87      }      if (p) {
88      else          path = m_strdup (p);
89          return multi_gnupg_path ();          free_if_alloc (p);
90      return m_strdup ("c:\\gnupg");          return path;
91  } /* get_gnupg_path */      }
92        else
93            return multi_gnupg_path ();
94  char*      return m_strdup ("c:\\gnupg");
95  get_gnupg_cfgfile (void)  }
96  {      
97      char *p = NULL, *optfile = NULL, *path = NULL;  
98      size_t nlen = 0;  /* Return the full path of the gpg config file.
99       A value of NULL indicates an error. */
100      path = get_gnupg_path ();  char*
101      if (path == NULL)  get_gnupg_cfgfile (void)
102          return NULL;  {    
103      p = get_reg_entry_gpg ("OptFile");      char *p = NULL, *optfile = NULL, *path = NULL;
104      if (p && !strcmp (p, ""))      size_t nlen = 0;
105      {  
106          nlen = strlen (path) + 64;      path = get_gnupg_path ();
107          optfile = new char[nlen + 1];      if (!path)
108          if (optfile == NULL)          return NULL;
109              BUG (0);      p = get_reg_entry_gpg ("OptFile");
110          _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);      if (p && !strcmp (p, "")) {
111      }          nlen = strlen (path) + 64;
112      else if( p ) {          optfile = new char[nlen + 1];
113          nlen = strlen( p ) + 4;          if (!optfile)
114          optfile = new char[nlen + 1];              BUG (0);
115          if( optfile == NULL )          _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);
116              BUG( NULL );      }
117          _snprintf( optfile, nlen, "%s", p );      else if (p) {
118      }          nlen = strlen( p ) + 4;
119      else {          optfile = new char[nlen + 1];
120          nlen = strlen( path ) + 64;          if (!optfile)
121          optfile = new char[nlen + 1];              BUG (NULL);
122          if( optfile == NULL )          _snprintf (optfile, nlen, "%s", p);
123              BUG( NULL );      }
124          _snprintf( optfile, nlen, "%s\\"GPG_CONF, path );      else {
125      }          nlen = strlen (path) + 64;
126      free_if_alloc (path);          optfile = new char[nlen + 1];
127      free_if_alloc (p);          if( !optfile)
128                BUG (NULL);
129      return optfile;          _snprintf (optfile, nlen, "%s\\"GPG_CONF, path);
130  } /* get_gnupg_cfgfile */      }
131        free_if_alloc (path);
132        free_if_alloc (p);
133  char *      return optfile;
134  get_gnupg_keyring (int pub, int strict)  }
135  {      
136      char * optfile = NULL, * path = NULL;  
137      char * keyring = NULL;  /* Return the full path of the keyring. If @pub is 1, the public
138       keyring is return, otherwise the secret keyring. */
139      path = get_gnupg_path ();  char*
140      if (!path)  get_gnupg_keyring (int pub, int strict)
141          return NULL;  {    
142      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");      char *optfile = NULL;
143      if (!strict && !file_exist_check (keyring)) {      char *path = NULL;
144          free_if_alloc (path);      char *keyring = NULL;
145          return keyring;  
146      }      path = get_gnupg_path ();
147      if (file_exist_check (keyring) || get_file_size (keyring) == 0) {      if (!path)
148          free_if_alloc (keyring);          return NULL;
149          optfile = make_filename (path, GPG_CONF, NULL);      keyring = make_filename (path, pub? "pubring" : "secring", "gpg");
150          keyring = get_gnupg_keyring_from_options (optfile, pub);      if (!strict && !file_exist_check (keyring)) {
151      }          free_if_alloc (path);
152      free_if_alloc (path);          return keyring;
153      free_if_alloc (optfile);      }
154      return keyring;      if (file_exist_check (keyring) || get_file_size (keyring) == 0) {
155  } /* get_gnupg_keyring */          free_if_alloc (keyring);
156            optfile = make_filename (path, GPG_CONF, NULL);
157            keyring = get_gnupg_keyring_from_options (optfile, pub);
158  /*      }
159   * Return the full path (with the gpg exe name). First the registry is scanned      free_if_alloc (path);
160   * for the entry 'gpgProgram'. If it wasn't set, the default path is the      free_if_alloc (optfile);
161   * appended string 'gpg.exe' is used.      return keyring;
162   */  }
163  char*  
164  get_gnupg_prog( void )  
165  {      /* Return the full path (with the gpg exe name). First the registry is scanned
166      char *p, *path, *pgm = NULL;     for the entry 'gpgProgram'. If it wasn't set, the default path is the
167      size_t nlen = 0;     appended string 'gpg.exe' is used. */
168            char*
169      path = get_gnupg_path ();  get_gnupg_prog (void)
170      if (path == NULL)  {    
171          return NULL;      char *p;
172        char *pgm = NULL;
173      p = get_reg_entry_gpg ("gpgProgram");      size_t nlen = 0;
174      if (p == NULL)  
175          pgm = make_filename (path, "gpg", "exe");      p = get_reg_entry_gpg ("gpgProgram");
176      else {      if (!p) {
177          pgm = m_strdup (p);          char *path = get_gnupg_path ();
178          free_if_alloc (p);          if (!path)
179      }              return NULL;
180      free_if_alloc (path);          pgm = make_filename (path, "gpg", "exe");
181      return pgm;          free_if_alloc (path);
182  } /* get_gnupg_prog */      }
183        else {
184            pgm = m_strdup (p);
185  static char *          free_if_alloc (p);
186  default_key_from_cache (int * ret_no_useable)      }
187  {      return pgm;
188      const char * s;  }
189      char * keyid = NULL;  
190      gpgme_key_t key;  
191      gpgme_keycache_t sec = keycache_get_ctx (0);  /* Retrieve the first usable secret key from cache.
192       If no usable was found, @ret_no_useable is 1.
193      if (!sec)     Return value: the keyid of the secret key. */
194          BUG (0);  static char *
195      gpgme_keycache_rewind (sec);  default_key_from_cache (int *ret_no_useable)
196      while (!gpgme_keycache_next_key (sec, 1, &key))  {
197      {      const char * s;
198          if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, 0))      char * keyid = NULL;
199          {      gpgme_key_t key;
200              s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0);      gpg_keycache_t sec = keycache_get_ctx (0);
201              if (s)      
202                  keyid = m_strdup (s+8);      if (!sec)
203              break;          BUG (0);
204          }      gpg_keycache_rewind (sec);
205      }      while (!gpg_keycache_next_key (sec, 1, &key)) {
206      if (!keyid)          if (key_is_useable (key)) {
207      {              s = key->subkeys->keyid;
208          *ret_no_useable = 1;              if (s)    
209          msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR);                  keyid = m_strdup (s+8);
210      }              break;
211      return keyid;          }
212  } /* default_key_from_cache */      }
213        if (!keyid) {
214            *ret_no_useable = 1;
215  char *          msg_box (NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR);
216  get_gnupg_default_key (void)      }
217  {          return keyid;
218      gpg_optfile_t opt = NULL;  }
219      gpg_option_t e;  
220      char * keyid = NULL, * optfile = NULL;  
221      int no_usable=0, rc = 0;  /* Load the gpg.conf and search for some options
222       and store the result in the global preference context.
223      optfile = get_gnupg_cfgfile ();     Return value: 0 on success. */
224      if (!optfile)  int
225          return default_key_from_cache( &no_usable );  gnupg_load_config (void)
226      rc = parse_gpg_options( optfile, &opt );  {
227      if( rc ) {      int rc;
228          free_if_alloc( optfile );      gpg_optfile_t opt;
229          return default_key_from_cache( &no_usable );      gpg_option_t o;
230      }      char *conf = get_gnupg_cfgfile ();
231      e = find_option( opt, "default-key" );      if (!conf)
232      if ( e )          return -1;
233          keyid = m_strdup( e->val );      rc = parse_gpg_options (conf, &opt);
234      if( !e ) {      if (rc) {
235          e = find_option( opt, "local-user" );          free_if_alloc (conf);
236          if( e )          return -1;
237              keyid = m_strdup( e->val );      }
238      }      o = find_option (opt, "ask-cert-level");
239      if( !e ) {      if (o)
240          e = find_option( opt, "encrypt-to" );          reg_prefs.gpg.ask_cert_level = 1;
241          if( e )      release_gpg_options (opt);
242              keyid = m_strdup( e->val );      free_if_alloc (conf);
243      }      return 0;
244      free_if_alloc( optfile );  }
245      release_gpg_options( opt );      
246    
247      if( !keyid )  char*
248          keyid = default_key_from_cache( &no_usable );  get_gnupg_default_key (void)
249      return keyid;  {    
250  } /* get_gnupg_default_key */      gpg_optfile_t opt = NULL;
251        gpg_option_t e;
252  /*      char * keyid = NULL, * optfile = NULL;
253   * Check if the gpg application (exe file) is available.      int no_usable=0, rc = 0;
254   */  
255  int      optfile = get_gnupg_cfgfile ();
256  check_gnupg_prog( void )      if (!optfile)
257  {          return default_key_from_cache (&no_usable);
258      char *pgm = NULL;      rc = parse_gpg_options (optfile, &opt);
259      int rc = 0;      if (rc) {
260            free_if_alloc( optfile );
261      pgm = get_gnupg_prog( );          return default_key_from_cache( &no_usable );
262      if( pgm == NULL )      }
263          rc = WPTERR_GPG_EXEFILE;      e = find_option( opt, "default-key" );
264      if( file_exist_check( pgm ) )      if ( e )
265          rc = WPTERR_GPG_EXEFILE;          keyid = m_strdup( e->val );
266      free_if_alloc( pgm );      if( !e ) {
267      return rc;          e = find_option( opt, "local-user" );
268  } /* check_gpg_prog */          if( e )
269                keyid = m_strdup( e->val );
270        }
271  static int      if( !e ) {
272  parse_version_nr( const char * buf, int *major, int *minor, int *patch )          e = find_option( opt, "encrypt-to" );
273  {          if( e )
274      char tmp[8];              keyid = m_strdup( e->val );
275      int i;      }
276            free_if_alloc (optfile);
277      if( strncmp( buf, "gpg ", 4 ) )      release_gpg_options (opt);
278          return -1;      
279      buf += 4;      if (!keyid)
280      if( strncmp( buf, "(GnuPG) ", 8 ) )          keyid = default_key_from_cache (&no_usable);
281          return -1;          return keyid;
282      buf += 8;  } /* get_gnupg_default_key */
283      i=0;  
284      while( buf && *buf != '.' && i < 8 )  
285          tmp[i++] = *buf++;  /* Check if the gpg application (exe file) is available. */
286      tmp[i] = 0; buf++;  int
287      *major = atol( tmp );  check_gnupg_prog (void)
288      i=0;  {
289      while( buf && *buf != '.' && i < 8 )      char *pgm = NULL;
290          tmp[i++] = *buf++;      int rc = 0;
291      tmp[i] = 0; buf++;  
292      *minor = atol( tmp );      pgm = get_gnupg_prog ();
293      i=0;      if (!pgm)
294      while( buf && isdigit( *buf ) && i < 8 )          rc = WPTERR_GPG_EXEFILE;
295          tmp[i++] = *buf++;      if (file_exist_check (pgm))
296      tmp[i] = 0;          rc = WPTERR_GPG_EXEFILE;
297      *patch = atol( tmp );      free_if_alloc (pgm);
298      return 0;      return rc;
299  }  }
300    
301    
302  int  static int
303  check_gnupg_engine (int * r_major, int * r_minor, int * r_patch)  parse_version_nr (const char * buf, int *major, int *minor, int *patch)
304  {  {
305      gpgme_error_t err;      char tmp[8];
306      char * eng = NULL;      int i;
307      int major=0, minor=0, patch=0;      
308      int rc;      i=0;
309                while (buf && *buf != '.' && i < 8)
310      err = gpgme_op_version( &eng );          tmp[i++] = *buf++;
311      if( err )      tmp[i] = 0; buf++;
312          return -1;      *major = atol( tmp );
313      if( strstr( eng, "IDEA" ) )      i=0;
314          idea_available = 1;      while (buf && *buf != '.' && i < 8)
315      rc = parse_version_nr( eng, &major, &minor, &patch );          tmp[i++] = *buf++;
316      free( eng ); eng = NULL;      tmp[i] = 0; buf++;
317      if( rc )      *minor = atol (tmp);
318          return rc;      i=0;
319      if( major < *r_major      while (buf && isdigit( *buf ) && i < 8)
320       || minor < *r_minor)          tmp[i++] = *buf++;
321          rc = 1;      tmp[i] = 0;
322      else {      *patch = atol (tmp);
323          if (patch < *r_patch )      return 0;
324              rc = 1;  }
325          rc = 0;  
326      }  
327      *r_major = major;  /* Check if the gnupg engine fullfills the minimum requirement
328      *r_minor = minor;     version given in @r_major.@r_minor.@r_patch. On success these
329      *r_patch = patch;     variables contain the GPG version which is installed. */
330      return rc;  int
331  } /* check_gnupg_engine */  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)
332    {
333        gpgme_ctx_t ctx;
334  int      gpgme_engine_info_t inf;
335  check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings)      char * eng = NULL;
336  {      int major=0, minor=0, patch=0;
337      gpg_optfile_t opt;          int rc;
338      gpg_option_t e;          
339      int rc = 0;      gpgme_new (&ctx);
340        inf = gpgme_ctx_get_engine_info (ctx);
341      if( r_secrings )      if (!inf) {
342          *r_secrings = 0;          gpgme_release (ctx);
343      if( r_pubrings )          return -1;
344          *r_pubrings = 0;      }
345      rc = parse_gpg_options( fname, &opt );      /* We need to exec GPG again to find out if IDEA is available. */
346      if( rc )      if (gpg_get_version (&eng))
347          return WPTERR_FILE_OPEN;          return -1;
348        if (strstr (eng, "IDEA"))
349      for( e = opt->list; e; e = e->next ) {          idea_available = 1;
350          if( !strcmp( e->name, "secret-keyring" ) ) {      free (eng);
351              if( !file_exist_check( e->val ) )      rc = parse_version_nr( inf->version, &major, &minor, &patch );
352                  r_secrings[0]++;      if( rc ) {
353          }          gpgme_release (ctx);
354          else if( !strcmp( e->name, "keyring" ) ) {          return rc;
355              if( !file_exist_check( e->val ) )      }
356                  r_pubrings[0]++;      if (major < *r_major || minor < *r_minor)
357          }          rc = 1;
358      }      else {
359      release_gpg_options( opt );          if (patch < *r_patch)
360      return 0;              rc = 1;
361  } /* check_gnupg_cfgfile */          rc = 0;
362        }
363  /*      *r_major = major;
364   * Check if both keyrings are located in the gnupg home directory.      *r_minor = minor;
365   */      *r_patch = patch;
366  int      return rc;
367  gnupg_access_files (void)  }
368  {  
369      int rc = 0;  
370      int pubring_ok = 0, secring_ok = 0;  int
371      int secrings = 0, pubrings = 0;  check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings)
372      char *optfile;  {
373        gpg_optfile_t opt;    
374      if (gnupg_access_keyring (1))      gpg_option_t e;
375          rc = WPTERR_GPG_KEYRINGS;      int rc = 0;
376      else  
377          pubring_ok = 1;      *r_secrings = 0;
378        *r_pubrings = 0;
379      if (gnupg_access_keyring (0))      rc = parse_gpg_options( fname, &opt );
380          rc = WPTERR_GPG_KEYRINGS;      if( rc )
381      else          return WPTERR_FILE_OPEN;
382          secring_ok = 1;  
383      if (!pubring_ok || !secring_ok) {      for( e = opt->list; e; e = e->next ) {
384          optfile = get_gnupg_cfgfile ();          if( !strcmp( e->name, "secret-keyring" ) ) {
385          if (!optfile)              if( !file_exist_check( e->val ) )
386              return WPTERR_GPG_KEYRINGS;                  r_secrings[0]++;
387          rc = file_exist_check (optfile);          }
388          if (!rc && get_file_size(optfile) > 0) {          else if( !strcmp( e->name, "keyring" ) ) {
389              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);              if( !file_exist_check( e->val ) )
390              if (!rc && secrings && pubrings) {                  r_pubrings[0]++;
391                  free_if_alloc (optfile);          }
392                  return 0; /* found two keyrings in the option file */      }
393              }      release_gpg_options( opt );
394              else if ((!rc && pubrings && secring_ok)      return 0;
395                    || (!rc && secrings && pubring_ok)) {  } /* check_gnupg_cfgfile */
396                  free_if_alloc (optfile);  
397                  return 0; /* found one keyring and one entry in the options file */  
398              }  /*
399              else   * Check if both keyrings are located in the gnupg home directory.
400                  return WPTERR_GPG_OPT_KEYRINGS;   */
401          }  int
402          free_if_alloc (optfile);  gnupg_access_files (void)
403          rc = WPTERR_GPG_KEYRINGS;  {
404      }      int rc = 0;
405      return rc;      int pubring_ok = 0, secring_ok = 0;
406  } /* gnupg_access_files */      int secrings = 0, pubrings = 0;
407        char *optfile;
408    
409  static int      if (gnupg_access_keyring (1))
410  create_gpg_options( void )          rc = WPTERR_GPG_KEYRINGS;
411  {      else
412      FILE *fp;          pubring_ok = 1;
413      char *s, *optfile;  
414        if (gnupg_access_keyring (0))
415      s = get_gnupg_path( );          rc = WPTERR_GPG_KEYRINGS;
416      if( s == NULL )      else
417          return WPTERR_FILE_CREAT;          secring_ok = 1;
418      optfile = make_filename( s, GPG_CONF, NULL );      if (!pubring_ok || !secring_ok) {
419      fp = fopen( optfile, "wb" );          optfile = get_gnupg_cfgfile ();
420      if( fp == NULL ) {            if (!optfile)
421          return WPTERR_FILE_CREAT;              return WPTERR_GPG_KEYRINGS;
422          goto fail;          rc = file_exist_check (optfile);
423      }          if (!rc && get_file_size(optfile) > 0) {
424      fwrite( options_skel, 1, strlen( options_skel ), fp );              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);
425      fclose( fp );              if (!rc && secrings && pubrings) {
426                    free_if_alloc (optfile);
427  fail:                  return 0; /* found two keyrings in the option file */
428      free_if_alloc( s );              }
429      free_if_alloc( optfile );              else if ((!rc && pubrings && secring_ok)
430      return 0;                    || (!rc && secrings && pubring_ok)) {
431  } /* create_gpg_options */                  free_if_alloc (optfile);
432                    return 0; /* found one keyring and one entry in the options file */
433                }
434  /*              else
435   * Return the contents of the options file as a char buf.                  return WPTERR_GPG_OPT_KEYRINGS;
436   */          }
437  char *          free_if_alloc (optfile);
438  get_gnupg_config (void)          rc = WPTERR_GPG_KEYRINGS;
439  {      }
440      FILE * fp;      return rc;
441      char * p = NULL, * optfile = NULL;  } /* gnupg_access_files */
442      int fsize, rc = 0;  
443            
444      optfile = get_gnupg_cfgfile ();  static int
445      if( optfile == NULL )  create_gpg_options (void)
446          return NULL;  {
447      fsize = get_file_size( optfile );      FILE *fp;
448      if( !fsize ) {      char *s, *optfile;
449          rc = create_gpg_options( );  
450          if ( rc )      s = get_gnupg_path( );
451              return NULL;      if( s == NULL )
452          fsize = get_file_size( optfile );          return WPTERR_FILE_CREAT;
453      }      optfile = make_filename( s, GPG_CONF, NULL );
454      if( fsize > 100000 )      fp = fopen( optfile, "wb" );
455          goto leave; /* too large */      if( fp == NULL ) {  
456      p = new char[fsize+1];          return WPTERR_FILE_CREAT;
457      if( p == NULL )          goto fail;
458          BUG( NULL );      }
459      fp = fopen( optfile, "rb" );      fwrite( options_skel, 1, strlen( options_skel ), fp );
460      if( fp == NULL ) {      fclose( fp );
461          free_if_alloc( p );  
462          return NULL;  fail:
463      }      free_if_alloc( s );
464      fread( p, 1, fsize, fp );      free_if_alloc( optfile );
465      fclose( fp );      return 0;
466      p[fsize] = '\0';  } /* create_gpg_options */
467      free_if_alloc( optfile );  
468    
469  leave:  /*
470      return p;   * Return the contents of the options file as a char buf.
471  } /* get_gnupg_config */   */
472    char *
473    get_gnupg_config (void)
474  int  {
475  set_gnupg_default_key (const char * key)      FILE * fp;
476  {      char * p = NULL, * optfile = NULL;
477      gpg_optfile_t opt;      int fsize, rc = 0;
478      gpg_option_t e;          
479      char *optfile = NULL;      optfile = get_gnupg_cfgfile ();
480      int rc = 0;      if( optfile == NULL )
481            return NULL;
482      optfile = get_gnupg_cfgfile ();      fsize = get_file_size( optfile );
483      if (!optfile)      if( !fsize ) {
484          return -1;          rc = create_gpg_options( );
485      rc = parse_gpg_options (optfile, &opt);          if ( rc )
486      if( rc ) {              return NULL;
487          free_if_alloc (optfile);          fsize = get_file_size( optfile );
488          return -1;      }
489      }      if( fsize > 100000 )
490      e = find_option (opt, "default-key");          goto leave; /* too large */
491      if (e) {      p = new char[fsize+1];
492          free_if_alloc (e->val);      if( p == NULL )
493          e->val = m_strdup (key);          BUG( NULL );
494          e->used = 1;      fp = fopen( optfile, "rb" );
495      }      if( fp == NULL ) {
496      else          free_if_alloc( p );
497          add_entry (opt, ENTRY_MULTI, "default-key", key);          return NULL;
498      rc = commit_gpg_options (optfile, opt);      }
499        fread( p, 1, fsize, fp );
500      free_if_alloc (optfile);      fclose( fp );
501      release_gpg_options (opt);      p[fsize] = '\0';
502        free_if_alloc( optfile );
503      return rc;  
504  } /* set_gnupg_default_key */  leave:
505        return p;
506    } /* get_gnupg_config */
507  /*  
508   * Set the contents of the options file.  
509   */  int
510  int  set_gnupg_default_key (const char * key)
511  set_gnupg_options( const char *buf, size_t buflen )  {
512  {      gpg_optfile_t opt;
513      FILE *fp;        gpg_option_t e;
514      char *optfile = NULL;      char *optfile = NULL;
515        int rc = 0;
516      optfile = get_gnupg_cfgfile( );  
517      if( optfile == NULL )      optfile = get_gnupg_cfgfile ();
518          return WPTERR_FILE_CREAT;      if (!optfile)
519            return -1;
520      fp = fopen( optfile, "wb" );      rc = parse_gpg_options (optfile, &opt);
521      if( fp == NULL ) {      if( rc ) {
522          free_if_alloc( optfile );          free_if_alloc (optfile);
523          return WPTERR_FILE_CREAT;          return -1;
524      }      }
525      fwrite( buf, 1, buflen, fp );      e = find_option (opt, "default-key");
526      fclose( fp );      if (e) {
527      free_if_alloc( optfile );          free_if_alloc (e->val);
528      return 0;          e->val = m_strdup (key);
529  } /* set_gnupg_options */          e->used = 1;
530        }
531  /*      else
532   * Check if the line contains a valid GPG argument.          add_entry (opt, ENTRY_MULTI, "default-key", key);
533   */      rc = commit_gpg_options (optfile, opt);
534  static int  
535  check_line( const char *buf )      free_if_alloc (optfile);
536  {      release_gpg_options (opt);
537      int j, len;  
538      int rc = 0;      return rc;
539    } /* set_gnupg_default_key */
540      if( *buf == '#' || *buf == '\r' || *buf == '\n' )  
541          return 1;  
542      rc = 0;  /*
543      for ( j = 0; valid_gpg_args[j]; j++ ) {   * Set the contents of the options file.
544          len = strlen( valid_gpg_args[j] );   */
545          if( !strncmp( valid_gpg_args[j], buf, len ) )  int
546              rc = 1;      set_gnupg_options( const char *buf, size_t buflen )
547      }  {
548        FILE *fp;  
549      return rc;      char *optfile = NULL;
550  } /* check_line */  
551        optfile = get_gnupg_cfgfile( );
552        if( optfile == NULL )
553  int          return WPTERR_FILE_CREAT;
554  check_gnupg_options( const char *buf )  
555  {      fp = fopen( optfile, "wb" );
556      char line[1024];      if( fp == NULL ) {
557      int nbytes = 0;          free_if_alloc( optfile );
558      unsigned j;          return WPTERR_FILE_CREAT;
559                }
560      for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) {      fwrite( buf, 1, buflen, fp );
561          line[nbytes++] = buf[j];      fclose( fp );
562          if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) {      free_if_alloc( optfile );
563              line[nbytes] = '\0';      return 0;
564              if( !check_line( line ) ) {  } /* set_gnupg_options */
565                  msg_box( NULL, line, "options", MB_OK );  
566                  return 1;        /*
567              }   * Check if the line contains a valid GPG argument.
568              nbytes = 0;   */
569          }        static int
570      }  check_line( const char *buf )
571    {
572      return 0;      int j, len;
573  } /* check_gnupg_options */      int rc = 0;
574    
575        if( *buf == '#' || *buf == '\r' || *buf == '\n' )
576  static int          return 1;
577  get_last_gnupg_access (gpg_watcher_s * ctx)      rc = 0;
578  {      for ( j = 0; valid_gpg_args[j]; j++ ) {
579      HANDLE fd;          len = strlen( valid_gpg_args[j] );
580      char *path = NULL, *file = NULL;          if( !strncmp( valid_gpg_args[j], buf, len ) )
581                rc = 1;    
582      path = get_gnupg_path ();      }
583      file =  make_filename (path, ctx->object, NULL);  
584      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);      return rc;
585      if( fd == INVALID_HANDLE_VALUE ) {  } /* check_line */
586          free_if_alloc (path);  
587          free_if_alloc (file);  
588          return WPTERR_FILE_OPEN;  int
589      }  check_gnupg_options( const char *buf )
590      GetFileTime (fd, NULL, NULL, &ctx->access);  {
591      CloseHandle (fd);      char line[1024];
592      free_if_alloc (path);            int nbytes = 0;
593      free_if_alloc (file);      unsigned j;
594      return 0;          
595  } /* get_last_gnupg_access */      for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) {
596            line[nbytes++] = buf[j];
597            if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) {
598  static void              line[nbytes] = '\0';
599  check_last_gnupg_access( gpg_watcher_s *ctx )              if( !check_line( line ) ) {
600  {                                msg_box( NULL, line, "options", MB_OK );
601      ctx->modified = 0;                  return 1;      
602                }
603      if( ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&              nbytes = 0;
604          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime )            }      
605          ctx->modified = 1;      }
606    
607      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;      return 0;
608      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;  } /* check_gnupg_options */
609  } /* check_last_gnupg_access */  
610    
611    /* Store the last access of the file inside the watcher @ctx. */
612  void  static int
613  init_gnupg_table (void)  get_last_gnupg_access (gpg_watcher_s *ctx)
614  {        {
615      int j;      HANDLE fd;
616        char *path;
617      for (j = 0; j < gpg_table_count; j++) {      char *file;
618          gpg_table[j].object = m_strdup (gpg_objs[j]);  
619          memset (&gpg_table[j].access, 0, sizeof (FILETIME));      path = get_gnupg_path ();
620          memset (&gpg_table[j].last_access, 0, sizeof (FILETIME));      file =  make_filename (path, ctx->object, NULL);
621          gpg_table[j].modified = 0;      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
622      }      if (fd == INVALID_HANDLE_VALUE) {
623  } /* init_gnupg_table */          free_if_alloc (path);
624            free_if_alloc (file);
625            return WPTERR_FILE_OPEN;
626  void      }
627  free_gnupg_table (void)      GetFileTime (fd, NULL, NULL, &ctx->access);
628  {      CloseHandle (fd);
629      int j;      free_if_alloc (path);
630        free_if_alloc (file);
631      for (j=0; j < gpg_table_count; j++)      return 0;
632          free_if_alloc (gpg_table[j].object);  }
633  } /* free_gnupg_table */  
634    
635    /* Check if the file inside watcher @ctx was modified. */
636  int  static void
637  keyring_check_last_access (void)  check_last_gnupg_access (gpg_watcher_s *ctx)
638  {        {              
639      int rc, j;      ctx->modified = 0;
640    
641      rc = 0;      if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
642      for (j = 0; j < gpg_table_count; j++) {          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
643          get_last_gnupg_access( &gpg_table[j] );          ctx->modified = 1;
644          check_last_gnupg_access( &gpg_table[j] );  
645          if ( gpg_table[j].modified )      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
646              rc++;            ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
647      }  }
648    
649      return rc;  
650  } /* keyring_check_last_access */  /* Init GPG watcher table for all monitored files. */
651    void
652    init_gnupg_table (void)
653  const char *  {      
654  gnupg_check_file_ext (const char * fname)      char *p;
655  {                    int j;
656      char file_ext[5];  
657        for (j = 0; j < gpg_table_count; j++) {
658      if (!strchr( fname, '.' ))          p = gpg_table[j].object = m_strdup (gpg_objs[j]);
659          return "UNKNOWN";          if (!p)
660                BUG (NULL);
661      strncpy (file_ext, fname + strlen (fname) - 4, 4);          memset (&gpg_table[j].access, 0, sizeof (FILETIME));
662      file_ext[4] = '\0';          memset (&gpg_table[j].last_access, 0, sizeof (FILETIME));
663      if (!stricmp (file_ext, ".asc"))          gpg_table[j].modified = 0;
664          return "ARMORED";      }
665      else if (!stricmp (file_ext, ".sig"))  }
666          return "SIGNED";  
667      else if  (!stricmp (file_ext, ".gpg")  
668          || !stricmp (file_ext, ".pgp"))  void
669          return "ENCRYPTED";  free_gnupg_table (void)
670      else  {
671          return "UNKNOWN";      int j;
672            
673      return "UNKNOWN";      for (j=0; j < gpg_table_count; j++)
674  } /* gnupg_check_file_ext */          free_if_alloc (gpg_table[j].object);
675    }
676    
677  char *  
678  get_gnupg_keyring_from_options (const char * fname, int pub)  /* Return the amount of files modified since the last call. */
679  {  int
680      gpg_optfile_t opt;  keyring_check_last_access (void)
681      gpg_option_t e;  {      
682      char * kring = NULL;      int rc, j;
683      int rc = 0;  
684        rc = 0;
685      rc = parse_gpg_options (fname, &opt);      for (j = 0; j < gpg_table_count; j++) {
686      if (rc)          get_last_gnupg_access (&gpg_table[j]);
687          return NULL;          check_last_gnupg_access (&gpg_table[j]);
688      if (pub)          if (gpg_table[j].modified)
689          e = find_option (opt, "keyring");              rc++;      
690      else      }
691          e = find_option (opt, "secret-keyring");  
692      if (e)      return rc;
693          kring = m_strdup (e->val);  }
694      release_gpg_options (opt);  
695    
696      return kring;  const char*
697  } /* get_gnupg_keyring_from_options */  gnupg_check_file_ext (const char *fname, int *r_type)
698    {              
699        char file_ext[5];
700    
701  /* XXX: does not work with write-protected floppies */      if (r_type)
702  static int          *r_type = PGP_NONE;
703  my_access (const char * fname)      if (!strchr (fname, '.' ))
704  {          return "UNKNOWN";
705      HANDLE hd;  
706      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);      strncpy (file_ext, fname + strlen (fname) - 4, 4);
707      if (hd == INVALID_HANDLE_VALUE)      file_ext[4] = '\0';
708          return -1;      if (!stricmp (file_ext, ".asc"))
709      CloseHandle (hd);          return "ARMORED";
710      return 0;      else if (!stricmp (file_ext, ".sig")) {
711  } /* my_access */          if (r_type)
712                *r_type = PGP_SIG;
713            return "SIGNED";
714  int      }
715  gpg_check_permissions (int showmsg)      else if  (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) {
716  {          if (r_type)
717      char * p, * name = NULL;              *r_type = PGP_MESSAGE;
718      int failed = 0, ans=0, attrs=0;          return "ENCRYPTED";
719        }
720      p = get_gnupg_path ();      return "UNKNOWN";
721      check_keyring (&p);  }
722      if (p) {  
723          name = make_filename (p, "pubring", "gpg");  
724          free_if_alloc (p);  char*
725          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {  get_gnupg_keyring_from_options (const char * fname, int pub)
726              ans = msg_box (NULL,  {
727                             _("The selected keyring has the read-only file\n"      gpg_optfile_t opt;
728                               "attribute. In this state you do not have write\n"      gpg_option_t e;
729                               "access. Do you want to remove the attribute?"),      char * kring = NULL;
730                       _("GPG Information"), MB_YESNO);      int rc = 0;
731              if (ans == IDYES) {  
732                  attrs &= ~FILE_ATTRIBUTE_READONLY;      rc = parse_gpg_options (fname, &opt);
733                  if (!SetFileAttributes (name, attrs)) {      if (rc)
734                      msg_box (NULL, _("Could not reset read-only state."),          return NULL;
735                               _("GPG Error"), MB_ERR);      if (pub)
736                      failed = 1;          e = find_option (opt, "keyring");
737                  }      else
738              }          e = find_option (opt, "secret-keyring");
739              else if (ans == IDNO) {      if (e)
740                  /*          kring = m_strdup (e->val);
741                  msg_box (NULL, _("All commands with write access to the keyring\n"      release_gpg_options (opt);
742                                   "will be disabled."), _("GPG Information"), MB_INFO);  
743                  */      return kring;
744                  failed = 1;  }
745              }  
746          }  
747          if (my_access (name)) {  
748              if (showmsg)  /* XXX: does not work with write-protected floppies */
749                  msg_box (NULL,  static int
750                  _("You do not have file access to modify the contents of\n"  my_access (const char * fname)
751                    "one or both of the selected keyrings.\n"  {
752                    "\n"      HANDLE hd;
753                    "The keyrings are in a read-only state which is propably\n"      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
754                    "caused by another program which already opened the files.\n"),                       NULL, OPEN_EXISTING, 0, NULL);
755                     _("GPG Warning"), MB_WARN);      if (hd == INVALID_HANDLE_VALUE)
756              failed = 2;          return -1;
757          }      CloseHandle (hd);
758      }      return 0;
759      free_if_alloc (name);  }
760      return failed;  
761  } /* gpg_check_permissions */  
762    int
763    gpg_check_permissions (int showmsg)
764  static int  {
765  check_homedir (void)      char * p, * name = NULL;
766  {            int failed = 0, ans=0, attrs=0;
767      char * homedir = NULL;  
768      int yes = 0;      p = get_gnupg_path ();
769        check_keyring (&p);
770      homedir = get_reg_entry_gpg ("HomeDir");      if (p) {
771      if (!homedir)          name = make_filename (p, "pubring", "gpg");
772          homedir = multi_gnupg_path ();          free_if_alloc (p);
773      if (!homedir)          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
774          homedir = m_strdup ("c:\\gnupg");              ans = msg_box (NULL,
775      if (homedir) {                             _("The selected keyring has the read-only file\n"
776          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {                               "attribute. In this state you do not have write\n"
777              yes = log_box (_("Preferences"), MB_YESNO,                               "access. Do you want to remove the attribute?"),
778                             _("%s does not exit.\n"                       _("GPG Information"), MB_YESNO);
779                               "Do you want to create this directory?"), homedir);              if (ans == IDYES) {
780              if (yes == IDYES) {                  attrs &= ~FILE_ATTRIBUTE_READONLY;
781                  if (CreateDirectory (homedir, NULL) == FALSE) {                  if (!SetFileAttributes (name, attrs)) {
782                      free_if_alloc (homedir);                      msg_box (NULL, _("Could not reset read-only state."),
783                      return WPTERR_DIR_CREAT;                               _("GPG Error"), MB_ERR);
784                  }                      failed = 1;
785              }                  }
786              return WPTERR_DIR_OPEN;              }
787          }              else if (ans == IDNO) {
788          free_if_alloc (homedir);                  /*
789      }                  msg_box (NULL, _("All commands with write access to the keyring\n"
790      return 0;                                   "will be disabled."), _("GPG Information"), MB_INFO);
791  } /* check_homedir */                  */
792                    failed = 1;
793                }
794  int          }
795  gnupg_check_homedir (void)          if (my_access (name)) {
796  {                    if (showmsg)
797      char *homedir = NULL, *prog = NULL;                  msg_box (NULL,
798      int rc = 0, ec = 0;                  _("You do not have file access to modify the contents of\n"
799                          "one or both of the selected keyrings.\n"
800      rc = check_homedir ();                    "\n"
801      if (rc)                    "The keyrings are in a read-only state which is propably\n"
802          return rc;                    "caused by another program which already opened the files.\n"),
803      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&                     _("GPG Warning"), MB_WARN);
804          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {              failed = 2;
805          prog = make_filename (homedir, "gpg", "exe");          }
806          if (file_exist_check (prog) == 0) {      }
807              rc = set_reg_entry_gpg ("gpgProgram", prog);      free_if_alloc (name);
808              if (rc)      return failed;
809                  goto fail;  } /* gpg_check_permissions */
810          }  
811          free_if_alloc (homedir);  
812          free_if_alloc (prog);  /* Check the GPG home dir. If all methods failed, try to
813          return rc;     create the default folder. */
814      }  static int
815      if ((prog = get_reg_entry_gpg ("gpgProgram"))  check_homedir (void)
816          && file_exist_check (prog)) {  {      
817          free_if_alloc (prog);      char *homedir = NULL;
818          homedir = get_reg_entry_gpg ("HomeDir");      int yes = 0;
819          if (!homedir) {  
820              rc = WPTERR_GENERAL;      homedir = get_reg_entry_gpg ("HomeDir");
821              goto fail;      if (!homedir)
822          }          homedir = multi_gnupg_path ();
823          prog = make_filename (homedir, "gpg", "exe");      if (!homedir)
824          if (file_exist_check (prog) == 0) {          homedir = m_strdup ("c:\\gnupg");
825              rc = set_reg_entry_gpg ("gpgProgram", prog);      if (homedir) {
826              if (rc)          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
827                  goto fail;              yes = log_box (_("Preferences"), MB_YESNO,
828              free_if_alloc (prog);                             _("%s does not exit.\n"
829              return rc;                               "Do you want to create this directory?"), homedir);
830          }              if (yes == IDYES) {
831      }                  BOOL ec = CreateDirectory (homedir, NULL);
832                        free_if_alloc (homedir);
833      /* Change the return code if homedir doesn't exist or if the program                  if (ec == FALSE)
834         doesn't exist. Note that exist_checks return 0 to suggest existance. */                      return WPTERR_DIR_CREAT;
835      if ((!homedir || dir_exist_check (homedir)))                  return 0;
836          rc = WPTERR_GENERAL;              }
837                    return WPTERR_DIR_OPEN;
838  fail:          }
839      free_if_alloc (homedir);          free_if_alloc (homedir);
840      free_if_alloc (prog);      }
841      return rc;      return 0;
842  } /* gnupg_check_homedir */  }
843    
844    
845  int  int
846  gnupg_copy_keyrings (void)  gnupg_check_homedir (void)
847  {  {      
848      const char * pring, * sring;      char *homedir = NULL;
849      char * file = NULL, * path = NULL;      char *prog = NULL;
850      int id = 0, rc = 0;      int rc = 0, ec = 0;
851      HWND hwnd;      
852            rc = check_homedir ();
853      path = get_gnupg_path ();      if (rc)
854      if (!path)          return rc;
855          return WPTERR_GENERAL;      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&
856      hwnd = GetDesktopWindow ();          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {
857            prog = make_filename (homedir, "gpg", "exe");
858      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),          if (file_exist_check (prog) == 0) {
859                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);              rc = set_reg_entry_gpg ("gpgProgram", prog);
860      if (!pring) {              if (rc)
861          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);                  goto fail;
862          free_if_alloc (path);          }
863          return WPTERR_GENERAL;          free_if_alloc (homedir);
864      }          free_if_alloc (prog);
865      file = make_filename (path, "pubring", "gpg");          return rc;
866      if (file_exist_check (file) == 0) {      }
867          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);      if ((prog = get_reg_entry_gpg ("gpgProgram"))
868          if (id == IDNO)          && file_exist_check (prog)) {
869              goto fail;          free_if_alloc (prog);
870      }          homedir = get_reg_entry_gpg ("HomeDir");
871      if (!CopyFile (pring, file, FALSE)) {          if (!homedir) {
872          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);              rc = WPTERR_GENERAL;
873          rc = WPTERR_FILE_READ;              goto fail;
874          goto fail;          }
875      }          prog = make_filename (homedir, "gpg", "exe");
876      free_if_alloc (file);          if (file_exist_check (prog) == 0) {
877                rc = set_reg_entry_gpg ("gpgProgram", prog);
878      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),              if (rc)
879                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);                  goto fail;
880      if (!sring) {              free_if_alloc (prog);
881          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );              return rc;
882          return WPTERR_GENERAL;          }
883      }      }
884      file = make_filename (path, "secring", "gpg");      
885      if (file_exist_check (file) == 0) {      /* Change the return code if homedir doesn't exist or if the program
886          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);         doesn't exist. Note that exist_checks return 0 to suggest existance. */
887          if( id == IDNO )      if ((!homedir || dir_exist_check (homedir)))
888              goto fail;          rc = WPTERR_GENERAL;
889      }      
890      if (!CopyFile (sring, file, FALSE)) {  fail:
891          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);      free_if_alloc (homedir);
892          rc = WPTERR_FILE_READ;      free_if_alloc (prog);
893      }      return rc;
894    } /* gnupg_check_homedir */
895  fail:  
896      free_if_alloc (file);  
897      free_if_alloc (path);  int
898      return rc;  gnupg_copy_keyrings (void)
899  } /* gnupg_import_keyrings */  {
900        const char * pring, * sring;
901        char * file = NULL, * path = NULL;
902  void      int id = 0, rc = 0;
903  gnupg_backup_options (int keep)      HWND hwnd;
904  {      
905      char *optfile = NULL;      path = get_gnupg_path ();
906      char bak[1024];      if (!path)
907            return WPTERR_GENERAL;
908      optfile = get_gnupg_cfgfile ();      hwnd = GetDesktopWindow ();
909      if (optfile == NULL)  
910          return;      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),
911      if (keep)                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);
912          _snprintf (bak, DIM (bak)-1, "%s.old", optfile);      if (!pring) {
913      else          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
914          _snprintf (bak, DIM (bak)-1, "%s.O", optfile);          free_if_alloc (path);
915      CopyFile (optfile, bak, keep);          return WPTERR_GENERAL;
916      free_if_alloc (optfile);      }
917  } /* gnupg_backup_options */      file = make_filename (path, "pubring", "gpg");
918        if (file_exist_check (file) == 0) {
919            id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);
920            if (id == IDNO)
921  static int              goto fail;
922  backup_one_file (const char * srcpath, const char * srcn,      }
923                   const char * dstpath, const char * dstn)      if (!CopyFile (pring, file, FALSE)) {
924  {          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);
925      char * src, * dst;          rc = WPTERR_FILE_READ;
926      BOOL rc;          goto fail;
927        }
928      src = make_filename (srcpath, srcn, "gpg");      free_if_alloc (file);
929      if (!src)  
930          BUG (NULL);      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),
931      dst = make_filename (dstpath, dstn, "gpg");                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);
932      if (!dst)      if (!sring) {
933          BUG (NULL);          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
934      rc = CopyFile (src, dst, FALSE);          return WPTERR_GENERAL;
935      free_if_alloc (src);      }
936      free_if_alloc (dst);      file = make_filename (path, "secring", "gpg");
937      if (!rc)      if (file_exist_check (file) == 0) {
938      {          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);
939          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);          if( id == IDNO )
940          return WPTERR_GENERAL;              goto fail;
941      }      }
942      return 0;      if (!CopyFile (sring, file, FALSE)) {
943  } /* backup_one_file */          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);
944            rc = WPTERR_FILE_READ;
945        }
946  static int  
947  check_keyring (char ** r_path)  fail:
948  {      free_if_alloc (file);
949      char * p;      free_if_alloc (path);
950      char * opt, * name;      return rc;
951    } /* gnupg_import_keyrings */
952      if (!*r_path)  
953          return 0;  
954      p = make_filename (*r_path, "pubring", "gpg");  void
955      if (!p || get_file_size (p) > 0)  gnupg_backup_options (void)
956          return 0;  {
957        char *cfgfile = NULL;
958      opt = get_gnupg_cfgfile ();      char bak[512];
959      if (!opt)  
960          BUG (0);      cfgfile = get_gnupg_cfgfile ();
961      name = get_gnupg_keyring_from_options (opt, 1);      if (cfgfile == NULL)
962      free_if_alloc (opt);          return;
963      free_if_alloc (p);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
964      if (!name)      CopyFile (cfgfile, bak, FALSE);
965          return 0;      free_if_alloc (cfgfile);
966      p = strrchr (name, '\\');  } /* gnupg_backup_options */
967      if (!p)  
968      {  
969          free_if_alloc (name);  
970          return 0;        static int
971      }  backup_one_file (const char *srcpath, const char *srcn,
972      free_if_alloc (*r_path);                   const char *dstpath, const char *dstn)
973      *r_path = new char [strlen (name)+1];  {
974      memset (*r_path, 0, strlen (name));      char * src, * dst;
975      strncpy (*r_path, name, (p-name));      BOOL rc;
976      free_if_alloc (name);  
977      return 1;      src = make_filename (srcpath, srcn, "gpg");
978  }      if (!src)
979            BUG (NULL);
980        dst = make_filename (dstpath, dstn, "gpg");
981  static char*      if (!dst)
982  get_backup_name (const char *templ)          BUG (NULL);
983  {      rc = CopyFile (src, dst, FALSE);
984      struct tm *tm;      free_if_alloc (src);
985      char *p;      free_if_alloc (dst);
986        if (!rc)
987      time_t t = time (NULL);      {
988      tm = localtime (&t);          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);
989      p = new char [strlen (templ) + 8 + 1];          return WPTERR_GENERAL;
990      if (!p)      }
991          BUG (0);      return 0;
992      sprintf (p, "%s-%d", templ, tm->tm_wday % 3);  } /* backup_one_file */
993      return p;  
994  }  
995    static int
996    check_keyring (char ** r_path)
997  void  {
998  gnupg_backup_keyrings (void)      char * p;
999  {      char * opt, * name;
1000      char *srcpath = NULL, *dstpath = NULL;  
1001      char *name=NULL;      if (!*r_path)
1002      int rc, bakmode=0;          return 0;
1003        p = make_filename (*r_path, "pubring", "gpg");
1004      if (!reg_prefs.auto_backup)      if (!p || get_file_size (p) > 0)
1005          return;          return 0;
1006      bakmode = reg_prefs.backup.mode;  
1007      srcpath =  get_gnupg_path ();      opt = get_gnupg_cfgfile ();
1008      check_keyring (&srcpath);      if (!opt)
1009      if (bakmode == 1) {          BUG (0);
1010          dstpath = get_gnupg_path ();      name = get_gnupg_keyring_from_options (opt, 1);
1011          check_keyring (&dstpath);      free_if_alloc (opt);
1012      }      free_if_alloc (p);
1013      else if (bakmode == 2) {      if (!name)
1014          char * tmpfile;          return 0;
1015          FILE * fp;      p = strrchr (name, '\\');
1016        if (!p)
1017          dstpath = m_strdup (reg_prefs.backup.path);      {
1018          if (!dstpath)          free_if_alloc (name);
1019              BUG (0);          return 0;      
1020          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");      }
1021          fp = fopen (tmpfile, "wb");      free_if_alloc (*r_path);
1022          if (!fp)      *r_path = new char [strlen (name)+1];
1023              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n"      memset (*r_path, 0, strlen (name));
1024                                                                   "Please insert/check the drive to continue."), dstpath);      strncpy (*r_path, name, (p-name));
1025          else {      free_if_alloc (name);
1026              rc = 0;      return 1;
1027              fclose (fp);  }
1028              unlink (tmpfile);  
1029          }  
1030          free_if_alloc (tmpfile);  static char*
1031          if (!fp || rc == IDCANCEL)  get_backup_name (const char *templ)
1032              return;  {
1033      }      struct tm *tm;
1034      else {      char *p;
1035          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode);  
1036          return;      time_t t = time (NULL);
1037      }      tm = localtime (&t);
1038      name = get_backup_name ("pubring-bak");      p = new char [strlen (templ) + 8 + 1];
1039      rc = backup_one_file (srcpath, "pubring", dstpath, name);      if (!p)
1040      if (!rc)          BUG (0);
1041          rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak");      sprintf (p, "%s-%d", templ, tm->tm_wday % 3);
1042      free_if_alloc (name);      return p;
1043      free_if_alloc (srcpath);  }
1044      free_if_alloc (dstpath);  
1045  } /* gnupg_backup_keyrings */  
1046    void
1047    gnupg_backup_keyrings (void)
1048  void  {
1049  gnupg_display_error (void)      char *srcpath = NULL, *dstpath = NULL;
1050  {            char *name=NULL;
1051      char tmpath[512], * errstr;      int rc, bakmode=0;
1052      size_t size = 0;  
1053      FILE * fp;      if (!reg_prefs.auto_backup)
1054            return;
1055      GetTempPath (sizeof tmpath - 32, (tmpath));      bakmode = reg_prefs.backup.mode;
1056      strcat (tmpath, "gpg_stderr");      srcpath =  get_gnupg_path ();
1057      size = get_file_size (tmpath);      check_keyring (&srcpath);
1058      if (file_exist_check (tmpath) || size <= 0)      if (bakmode == 1) {
1059          return;          dstpath = get_gnupg_path ();
1060      fp = fopen( tmpath, "rb" );          check_keyring (&dstpath);
1061      if (!fp) {      }
1062          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );      else if (bakmode == 2) {
1063          return;          char * tmpfile;
1064      }          FILE * fp;
1065      errstr = new char[size+1];  
1066      if (!errstr)          dstpath = m_strdup (reg_prefs.backup.path);
1067          BUG (0);          if (!dstpath)
1068      fread (errstr, 1, size, fp);              BUG (0);
1069      errstr[size] = '\0';          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");
1070      fclose (fp);          fp = fopen (tmpfile, "wb");
1071      msg_box (NULL, errstr, _("GPG Error"), MB_INFO);          if (!fp)
1072      free_if_alloc (errstr);              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL,
1073  } /* gnupg_display_error */                            _("The backup drive '%s' does not seems to accessable.\n"
1074                                "Please insert/check the drive to continue."), dstpath);
1075            else {
1076  int              rc = 0;
1077  gnupg_access_keyring (int _pub)              fclose (fp);
1078  {              unlink (tmpfile);
1079      int rc = 0;          }
1080      char * name = get_gnupg_keyring (_pub, 1);          free_if_alloc (tmpfile);
1081      if (file_exist_check (name))          if (!fp || rc == IDCANCEL)
1082          rc = -1;              return;
1083      free_if_alloc (name);      }
1084      return rc;      else {
1085  }          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode);
1086            return;
1087        }
1088        name = get_backup_name ("pubring-bak");
1089        rc = backup_one_file (srcpath, "pubring", dstpath, name);
1090        if (!rc)
1091            rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak");
1092        free_if_alloc (name);
1093        free_if_alloc (srcpath);
1094        free_if_alloc (dstpath);
1095    } /* gnupg_backup_keyrings */
1096    
1097    
1098    /* Display GPG error from file if possible. */
1099    void
1100    gnupg_display_error (void)
1101    {      
1102        char tmpath[512], * errstr;
1103        size_t size = 0;
1104        FILE * fp;
1105    
1106        GetTempPath (sizeof tmpath - 32, (tmpath));
1107        strcat (tmpath, "gpg_stderr");
1108        size = get_file_size (tmpath);
1109        if (file_exist_check (tmpath) || size <= 0)
1110            return;
1111        fp = fopen( tmpath, "rb" );
1112        if (!fp) {
1113            msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );
1114            return;
1115        }
1116        errstr = new char[size+1];
1117        if (!errstr)
1118            BUG (0);
1119        fread (errstr, 1, size, fp);
1120        errstr[size] = '\0';
1121        fclose (fp);
1122        msg_box (NULL, errstr, _("GPG Error"), MB_INFO);
1123        free_if_alloc (errstr);
1124    }
1125    
1126    
1127    
1128    /* check that the requested GPG keyring exist and.
1129       Return value: 0 for success. */
1130    int
1131    gnupg_access_keyring (int _pub)
1132    {
1133        int rc = 0;
1134        char *name = get_gnupg_keyring (_pub, 1);
1135        if (!name || file_exist_check (name))
1136            rc = -1;
1137        free_if_alloc (name);
1138        return rc;
1139    }

Legend:
Removed from v.22  
changed lines
  Added in v.41

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26