/[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 25 by twoaday, Wed Oct 12 10:04:26 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 "wptTypes.h"
37  #define GPG_CONF "gpg.conf"  #include "wptNLS.h"
38    #include "wptRegistry.h"
39  struct gpg_watcher_s {  #include "wptErrors.h"
40      FILETIME last_access;  #include "wptW32API.h"
41      FILETIME access;  #include "wptCrypto.h"
42      char * object;  
43      int modified;  #define GPG_CONF "gpg.conf"
44  };  
45    struct gpg_watcher_s {
46  /* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */      FILETIME    last_access;
47  static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"};      FILETIME    access;
48  static gpg_watcher_s gpg_table[3];      char        *object;
49  static int gpg_table_count = DIM (gpg_table);      int         modified;
50    };
51  int idea_available = 0;  
52    
53  static int check_keyring (char ** r_path);  /* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */
54    static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"};
55    static gpg_watcher_s gpg_table[3];
56  static char*  static int gpg_table_count = DIM (gpg_table);
57  multi_gnupg_path (void)  
58  {  int idea_available = 0;
59      static char buf[256+64];  
60      BOOL ec;  static int check_keyring (char ** r_path);
61    
62      /* MSDN: buf must be at least MAX_PATH=256 bytes */  
63      memset (buf, 0, sizeof (buf));  /* Return the application data folder of the current user. */
64      ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE);  static char*
65      if (ec != 1)  multi_gnupg_path (void)
66          return NULL;  {
67      strcat (buf, "\\gnupg");      static char buf[256+64];
68      if (access (buf, 00))      BOOL ec;
69          return NULL;  
70      return m_strdup (buf);      /* MSDN: buf must be at least MAX_PATH=256 bytes */
71  }      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  } /* get_gnupg_path */      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)      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
656  {                }
657      char file_ext[5];  
658    
659      if (!strchr( fname, '.' ))  /* Init GPG watcher table for all monitored files. */
660          return "UNKNOWN";  void
661    init_gnupg_table (void)
662      strncpy (file_ext, fname + strlen (fname) - 4, 4);  {      
663      file_ext[4] = '\0';      char *p;
664      if (!stricmp (file_ext, ".asc"))      int j;
665          return "ARMORED";  
666      else if (!stricmp (file_ext, ".sig"))      for (j = 0; j < gpg_table_count; j++) {
667          return "SIGNED";          p = gpg_table[j].object = m_strdup (gpg_objs[j]);
668      else if  (!stricmp (file_ext, ".gpg")          if (!p)
669          || !stricmp (file_ext, ".pgp"))              BUG (NULL);
670          return "ENCRYPTED";          memset (&gpg_table[j].access, 0, sizeof (FILETIME));
671      else          memset (&gpg_table[j].last_access, 0, sizeof (FILETIME));
672          return "UNKNOWN";          gpg_table[j].modified = 0;
673                }
674      return "UNKNOWN";  }
675  } /* gnupg_check_file_ext */  
676    
677    void
678  char *  free_gnupg_table (void)
679  get_gnupg_keyring_from_options (const char * fname, int pub)  {
680  {      int j;
681      gpg_optfile_t opt;  
682      gpg_option_t e;      for (j=0; j < gpg_table_count; j++)
683      char * kring = NULL;          free_if_alloc (gpg_table[j].object);
684      int rc = 0;  }
685    
686      rc = parse_gpg_options (fname, &opt);  
687      if (rc)  /* Return the amount of files modified since the last call. */
688          return NULL;  int
689      if (pub)  keyring_check_last_access (void)
690          e = find_option (opt, "keyring");  {      
691      else      int rc, j;
692          e = find_option (opt, "secret-keyring");  
693      if (e)      rc = 0;
694          kring = m_strdup (e->val);      for (j = 0; j < gpg_table_count; j++) {
695      release_gpg_options (opt);          get_last_gnupg_access (&gpg_table[j]);
696            check_last_gnupg_access (&gpg_table[j]);
697      return kring;          if (gpg_table[j].modified)
698  } /* get_gnupg_keyring_from_options */              rc++;      
699        }
700    
701        return rc;
702  /* XXX: does not work with write-protected floppies */  }
703  static int  
704  my_access (const char * fname)  
705  {  const char*
706      HANDLE hd;  gnupg_check_file_ext (const char *fname, int *r_type)
707      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  {              
708      if (hd == INVALID_HANDLE_VALUE)      char file_ext[5];
709          return -1;  
710      CloseHandle (hd);      if (r_type)
711      return 0;          *r_type = PGP_NONE;
712  } /* my_access */      if (!strchr (fname, '.' ))
713            return "UNKNOWN";
714    
715  int      strncpy (file_ext, fname + strlen (fname) - 4, 4);
716  gpg_check_permissions (int showmsg)      file_ext[4] = '\0';
717  {      if (!stricmp (file_ext, ".asc"))
718      char * p, * name = NULL;          return "ARMORED";
719      int failed = 0, ans=0, attrs=0;      else if (!stricmp (file_ext, ".sig")) {
720            if (r_type)
721      p = get_gnupg_path ();              *r_type = PGP_SIG;
722      check_keyring (&p);          return "SIGNED";
723      if (p) {      }
724          name = make_filename (p, "pubring", "gpg");      else if  (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) {
725          free_if_alloc (p);          if (r_type)
726          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {              *r_type = PGP_MESSAGE;
727              ans = msg_box (NULL,          return "ENCRYPTED";
728                             _("The selected keyring has the read-only file\n"      }
729                               "attribute. In this state you do not have write\n"      return "UNKNOWN";
730                               "access. Do you want to remove the attribute?"),  }
731                       _("GPG Information"), MB_YESNO);  
732              if (ans == IDYES) {  
733                  attrs &= ~FILE_ATTRIBUTE_READONLY;  char*
734                  if (!SetFileAttributes (name, attrs)) {  get_gnupg_keyring_from_options (const char * fname, int pub)
735                      msg_box (NULL, _("Could not reset read-only state."),  {
736                               _("GPG Error"), MB_ERR);      gpg_optfile_t opt;
737                      failed = 1;      gpg_option_t e;
738                  }      char * kring = NULL;
739              }      int rc = 0;
740              else if (ans == IDNO) {  
741                  /*      rc = parse_gpg_options (fname, &opt);
742                  msg_box (NULL, _("All commands with write access to the keyring\n"      if (rc)
743                                   "will be disabled."), _("GPG Information"), MB_INFO);          return NULL;
744                  */      if (pub)
745                  failed = 1;          e = find_option (opt, "keyring");
746              }      else
747          }          e = find_option (opt, "secret-keyring");
748          if (my_access (name)) {      if (e)
749              if (showmsg)          kring = m_strdup (e->val);
750                  msg_box (NULL,      release_gpg_options (opt);
751                  _("You do not have file access to modify the contents of\n"  
752                    "one or both of the selected keyrings.\n"      return kring;
753                    "\n"  }
754                    "The keyrings are in a read-only state which is propably\n"  
755                    "caused by another program which already opened the files.\n"),  
756                     _("GPG Warning"), MB_WARN);  
757              failed = 2;  /* XXX: does not work with write-protected floppies */
758          }  static int
759      }  my_access (const char * fname)
760      free_if_alloc (name);  {
761      return failed;      HANDLE hd;
762  } /* gpg_check_permissions */      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
763                         NULL, OPEN_EXISTING, 0, NULL);
764        if (hd == INVALID_HANDLE_VALUE)
765  static int          return -1;
766  check_homedir (void)      CloseHandle (hd);
767  {            return 0;
768      char * homedir = NULL;  }
769      int yes = 0;  
770    
771      homedir = get_reg_entry_gpg ("HomeDir");  int
772      if (!homedir)  gpg_check_permissions (int showmsg)
773          homedir = multi_gnupg_path ();  {
774      if (!homedir)      char * p, * name = NULL;
775          homedir = m_strdup ("c:\\gnupg");      int failed = 0, ans=0, attrs=0;
776      if (homedir) {  
777          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {      p = get_gnupg_path ();
778              yes = log_box (_("Preferences"), MB_YESNO,      check_keyring (&p);
779                             _("%s does not exit.\n"      if (p) {
780                               "Do you want to create this directory?"), homedir);          name = make_filename (p, "pubring", "gpg");
781              if (yes == IDYES) {          free_if_alloc (p);
782                  if (CreateDirectory (homedir, NULL) == FALSE) {          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
783                      free_if_alloc (homedir);              ans = msg_box (NULL,
784                      return WPTERR_DIR_CREAT;                             _("The selected keyring has the read-only file\n"
785                  }                               "attribute. In this state you do not have write\n"
786              }                               "access. Do you want to remove the attribute?"),
787              return WPTERR_DIR_OPEN;                       _("GPG Information"), MB_YESNO);
788          }              if (ans == IDYES) {
789          free_if_alloc (homedir);                  attrs &= ~FILE_ATTRIBUTE_READONLY;
790      }                  if (!SetFileAttributes (name, attrs)) {
791      return 0;                      msg_box (NULL, _("Could not reset read-only state."),
792  } /* check_homedir */                               _("GPG Error"), MB_ERR);
793                        failed = 1;
794                    }
795  int              }
796  gnupg_check_homedir (void)              else if (ans == IDNO) {
797  {                        /*
798      char *homedir = NULL, *prog = NULL;                  msg_box (NULL, _("All commands with write access to the keyring\n"
799      int rc = 0, ec = 0;                                   "will be disabled."), _("GPG Information"), MB_INFO);
800                        */
801      rc = check_homedir ();                  failed = 1;
802      if (rc)              }
803          return rc;          }
804      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&          if (my_access (name)) {
805          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {              if (showmsg)
806          prog = make_filename (homedir, "gpg", "exe");                  msg_box (NULL,
807          if (file_exist_check (prog) == 0) {                  _("You do not have file access to modify the contents of\n"
808              rc = set_reg_entry_gpg ("gpgProgram", prog);                    "one or both of the selected keyrings.\n"
809              if (rc)                    "\n"
810                  goto fail;                    "The keyrings are in a read-only state which is propably\n"
811          }                    "caused by another program which already opened the files.\n"),
812          free_if_alloc (homedir);                     _("GPG Warning"), MB_WARN);
813          free_if_alloc (prog);              failed = 2;
814          return rc;          }
815      }      }
816      if ((prog = get_reg_entry_gpg ("gpgProgram"))      free_if_alloc (name);
817          && file_exist_check (prog)) {      return failed;
818          free_if_alloc (prog);  } /* gpg_check_permissions */
819          homedir = get_reg_entry_gpg ("HomeDir");  
820          if (!homedir) {  
821              rc = WPTERR_GENERAL;  /* Check the GPG home dir. If all methods failed, try to
822              goto fail;     create the default folder. */
823          }  static int
824          prog = make_filename (homedir, "gpg", "exe");  check_homedir (void)
825          if (file_exist_check (prog) == 0) {  {      
826              rc = set_reg_entry_gpg ("gpgProgram", prog);      char *homedir = NULL;
827              if (rc)      int yes = 0;
828                  goto fail;  
829              free_if_alloc (prog);      homedir = get_reg_entry_gpg ("HomeDir");
830              return rc;      if (!homedir)
831          }          homedir = multi_gnupg_path ();
832      }      if (!homedir)
833                homedir = m_strdup ("c:\\gnupg");
834      /* Change the return code if homedir doesn't exist or if the program      if (homedir) {
835         doesn't exist. Note that exist_checks return 0 to suggest existance. */          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
836      if ((!homedir || dir_exist_check (homedir)))              yes = log_box (_("Preferences"), MB_YESNO,
837          rc = WPTERR_GENERAL;                             _("%s does not exit.\n"
838                                     "Do you want to create this directory?"), homedir);
839  fail:              if (yes == IDYES) {
840      free_if_alloc (homedir);                  BOOL ec = CreateDirectory (homedir, NULL);
841      free_if_alloc (prog);                  free_if_alloc (homedir);
842      return rc;                  if (ec == FALSE)
843  } /* gnupg_check_homedir */                      return WPTERR_DIR_CREAT;
844                    return 0;
845                }
846  int              return WPTERR_DIR_OPEN;
847  gnupg_copy_keyrings (void)          }
848  {          free_if_alloc (homedir);
849      const char * pring, * sring;      }
850      char * file = NULL, * path = NULL;      return 0;
851      int id = 0, rc = 0;  }
852      HWND hwnd;  
853        
854      path = get_gnupg_path ();  int
855      if (!path)  gnupg_check_homedir (void)
856          return WPTERR_GENERAL;  {      
857      hwnd = GetDesktopWindow ();      char *homedir = NULL;
858        char *prog = NULL;
859      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),      int rc = 0, ec = 0;
860                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);      
861      if (!pring) {      rc = check_homedir ();
862          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);      if (rc)
863          free_if_alloc (path);          return rc;
864          return WPTERR_GENERAL;      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&
865      }          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {
866      file = make_filename (path, "pubring", "gpg");          prog = make_filename (homedir, "gpg", "exe");
867      if (file_exist_check (file) == 0) {          if (file_exist_check (prog) == 0) {
868          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);              rc = set_reg_entry_gpg ("gpgProgram", prog);
869          if (id == IDNO)              if (rc)
870              goto fail;                  goto fail;
871      }          }
872      if (!CopyFile (pring, file, FALSE)) {          free_if_alloc (homedir);
873          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);          free_if_alloc (prog);
874          rc = WPTERR_FILE_READ;          return rc;
875          goto fail;      }
876      }      if ((prog = get_reg_entry_gpg ("gpgProgram"))
877      free_if_alloc (file);          && file_exist_check (prog)) {
878            free_if_alloc (prog);
879      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),          homedir = get_reg_entry_gpg ("HomeDir");
880                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);          if (!homedir) {
881      if (!sring) {              rc = WPTERR_GENERAL;
882          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );              goto fail;
883          return WPTERR_GENERAL;          }
884      }          prog = make_filename (homedir, "gpg", "exe");
885      file = make_filename (path, "secring", "gpg");          if (file_exist_check (prog) == 0) {
886      if (file_exist_check (file) == 0) {              rc = set_reg_entry_gpg ("gpgProgram", prog);
887          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);              if (rc)
888          if( id == IDNO )                  goto fail;
889              goto fail;              free_if_alloc (prog);
890      }              return rc;
891      if (!CopyFile (sring, file, FALSE)) {          }
892          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);      }
893          rc = WPTERR_FILE_READ;      
894      }      /* Change the return code if homedir doesn't exist or if the program
895           doesn't exist. Note that exist_checks return 0 to suggest existance. */
896  fail:      if ((!homedir || dir_exist_check (homedir)))
897      free_if_alloc (file);          rc = WPTERR_GENERAL;
898      free_if_alloc (path);      
899      return rc;  fail:
900  } /* gnupg_import_keyrings */      free_if_alloc (homedir);
901        free_if_alloc (prog);
902        return rc;
903  void  } /* gnupg_check_homedir */
904  gnupg_backup_options (void)  
905  {  
906      char *cfgfile = NULL;  int
907      char bak[512];  gnupg_copy_keyrings (void)
908    {
909      cfgfile = get_gnupg_cfgfile ();      const char * pring, * sring;
910      if (cfgfile == NULL)      char * file = NULL, * path = NULL;
911          return;      int id = 0, rc = 0;
912      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      HWND hwnd;
913      CopyFile (cfgfile, bak, FALSE);      
914      free_if_alloc (cfgfile);      path = get_gnupg_path ();
915  } /* gnupg_backup_options */      if (!path)
916            return WPTERR_GENERAL;
917        hwnd = GetDesktopWindow ();
918    
919  static int      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),
920  backup_one_file (const char * srcpath, const char * srcn,                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);
921                   const char * dstpath, const char * dstn)      if (!pring) {
922  {          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
923      char * src, * dst;          free_if_alloc (path);
924      BOOL rc;          return WPTERR_GENERAL;
925        }
926      src = make_filename (srcpath, srcn, "gpg");      file = make_filename (path, "pubring", "gpg");
927      if (!src)      if (file_exist_check (file) == 0) {
928          BUG (NULL);          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);
929      dst = make_filename (dstpath, dstn, "gpg");          if (id == IDNO)
930      if (!dst)              goto fail;
931          BUG (NULL);      }
932      rc = CopyFile (src, dst, FALSE);      if (!CopyFile (pring, file, FALSE)) {
933      free_if_alloc (src);          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);
934      free_if_alloc (dst);          rc = WPTERR_FILE_READ;
935      if (!rc)          goto fail;
936      {      }
937          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);      free_if_alloc (file);
938          return WPTERR_GENERAL;  
939      }      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),
940      return 0;                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);
941  } /* backup_one_file */      if (!sring) {
942            msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
943            return WPTERR_GENERAL;
944  static int      }
945  check_keyring (char ** r_path)      file = make_filename (path, "secring", "gpg");
946  {      if (file_exist_check (file) == 0) {
947      char * p;          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);
948      char * opt, * name;          if( id == IDNO )
949                goto fail;
950      if (!*r_path)      }
951          return 0;      if (!CopyFile (sring, file, FALSE)) {
952      p = make_filename (*r_path, "pubring", "gpg");          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);
953      if (!p || get_file_size (p) > 0)          rc = WPTERR_FILE_READ;
954          return 0;      }
955    
956      opt = get_gnupg_cfgfile ();  fail:
957      if (!opt)      free_if_alloc (file);
958          BUG (0);      free_if_alloc (path);
959      name = get_gnupg_keyring_from_options (opt, 1);      return rc;
960      free_if_alloc (opt);  } /* gnupg_import_keyrings */
961      free_if_alloc (p);  
962      if (!name)  
963          return 0;  void
964      p = strrchr (name, '\\');  gnupg_backup_options (void)
965      if (!p)  {
966      {      char *cfgfile = NULL;
967          free_if_alloc (name);      char bak[512];
968          return 0;        
969      }      cfgfile = get_gnupg_cfgfile ();
970      free_if_alloc (*r_path);      if (cfgfile == NULL)
971      *r_path = new char [strlen (name)+1];          return;
972      memset (*r_path, 0, strlen (name));      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
973      strncpy (*r_path, name, (p-name));      CopyFile (cfgfile, bak, FALSE);
974      free_if_alloc (name);      free_if_alloc (cfgfile);
975      return 1;  } /* gnupg_backup_options */
976  }  
977    
978    
979  static char*  static int
980  get_backup_name (const char *templ)  backup_one_file (const char *srcpath, const char *srcn,
981  {                   const char *dstpath, const char *dstn)
982      struct tm *tm;  {
983      char *p;      char * src, * dst;
984        BOOL rc;
985      time_t t = time (NULL);  
986      tm = localtime (&t);      src = make_filename (srcpath, srcn, "gpg");
987      p = new char [strlen (templ) + 8 + 1];      if (!src)
988      if (!p)          BUG (NULL);
989          BUG (0);      dst = make_filename (dstpath, dstn, "gpg");
990      sprintf (p, "%s-%d", templ, tm->tm_wday % 3);      if (!dst)
991      return p;          BUG (NULL);
992  }      rc = CopyFile (src, dst, FALSE);
993        free_if_alloc (src);
994        free_if_alloc (dst);
995  void      if (!rc)
996  gnupg_backup_keyrings (void)      {
997  {          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);
998      char *srcpath = NULL, *dstpath = NULL;          return WPTERR_GENERAL;
999      char *name=NULL;      }
1000      int rc, bakmode=0;      return 0;
1001    } /* backup_one_file */
1002      if (!reg_prefs.auto_backup)  
1003          return;  
1004      bakmode = reg_prefs.backup.mode;  static int
1005      srcpath =  get_gnupg_path ();  check_keyring (char ** r_path)
1006      check_keyring (&srcpath);  {
1007      if (bakmode == 1) {      char * p;
1008          dstpath = get_gnupg_path ();      char * opt, * name;
1009          check_keyring (&dstpath);  
1010      }      if (!*r_path)
1011      else if (bakmode == 2) {          return 0;
1012          char * tmpfile;      p = make_filename (*r_path, "pubring", "gpg");
1013          FILE * fp;      if (!p || get_file_size (p) > 0)
1014            return 0;
1015          dstpath = m_strdup (reg_prefs.backup.path);  
1016          if (!dstpath)      opt = get_gnupg_cfgfile ();
1017              BUG (0);      if (!opt)
1018          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");          BUG (0);
1019          fp = fopen (tmpfile, "wb");      name = get_gnupg_keyring_from_options (opt, 1);
1020          if (!fp)      free_if_alloc (opt);
1021              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n"      free_if_alloc (p);
1022                                                                   "Please insert/check the drive to continue."), dstpath);      if (!name)
1023          else {          return 0;
1024              rc = 0;      p = strrchr (name, '\\');
1025              fclose (fp);      if (!p)
1026              unlink (tmpfile);      {
1027          }          free_if_alloc (name);
1028          free_if_alloc (tmpfile);          return 0;      
1029          if (!fp || rc == IDCANCEL)      }
1030              return;      free_if_alloc (*r_path);
1031      }      *r_path = new char [strlen (name)+1];
1032      else {      memset (*r_path, 0, strlen (name));
1033          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode);      strncpy (*r_path, name, (p-name));
1034          return;      free_if_alloc (name);
1035      }      return 1;
1036      name = get_backup_name ("pubring-bak");  }
1037      rc = backup_one_file (srcpath, "pubring", dstpath, name);  
1038      if (!rc)  
1039          rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak");  static char*
1040      free_if_alloc (name);  get_backup_name (const char *templ)
1041      free_if_alloc (srcpath);  {
1042      free_if_alloc (dstpath);      struct tm *tm;
1043  } /* gnupg_backup_keyrings */      char *p;
1044    
1045        time_t t = time (NULL);
1046  void      tm = localtime (&t);
1047  gnupg_display_error (void)      p = new char [strlen (templ) + 8 + 1];
1048  {            if (!p)
1049      char tmpath[512], * errstr;          BUG (0);
1050      size_t size = 0;      sprintf (p, "%s-%d", templ, tm->tm_wday % 3);
1051      FILE * fp;      return p;
1052    }
1053      GetTempPath (sizeof tmpath - 32, (tmpath));  
1054      strcat (tmpath, "gpg_stderr");  
1055      size = get_file_size (tmpath);  void
1056      if (file_exist_check (tmpath) || size <= 0)  gnupg_backup_keyrings (void)
1057          return;  {
1058      fp = fopen( tmpath, "rb" );      char *srcpath = NULL, *dstpath = NULL;
1059      if (!fp) {      char *name=NULL;
1060          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );      int rc, bakmode=0;
1061          return;  
1062      }      if (!reg_prefs.auto_backup)
1063      errstr = new char[size+1];          return;
1064      if (!errstr)      bakmode = reg_prefs.backup.mode;
1065          BUG (0);      srcpath =  get_gnupg_path ();
1066      fread (errstr, 1, size, fp);      check_keyring (&srcpath);
1067      errstr[size] = '\0';      if (bakmode == 1) {
1068      fclose (fp);          dstpath = get_gnupg_path ();
1069      msg_box (NULL, errstr, _("GPG Error"), MB_INFO);          check_keyring (&dstpath);
1070      free_if_alloc (errstr);      }
1071  } /* gnupg_display_error */      else if (bakmode == 2) {
1072            char * tmpfile;
1073            FILE * fp;
1074  int  
1075  gnupg_access_keyring (int _pub)          dstpath = m_strdup (reg_prefs.backup.path);
1076  {          if (!dstpath)
1077      int rc = 0;              BUG (0);
1078      char * name = get_gnupg_keyring (_pub, 1);          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");
1079      if (file_exist_check (name))          fp = fopen (tmpfile, "wb");
1080          rc = -1;          if (!fp)
1081      free_if_alloc (name);              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL,
1082      return rc;                            _("The backup drive '%s' does not seems to accessable.\n"
1083  }                              "Please insert/check the drive to continue."), dstpath);
1084            else {
1085                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.25  
changed lines
  Added in v.58

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26