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

Legend:
Removed from v.32  
changed lines
  Added in v.58

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26