/[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 24 by twoaday, Sat Oct 8 10:43:08 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      gpgme_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      gpgme_keycache_rewind (sec);  }
196      while (!gpgme_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   */  }
252  int  
253  check_gnupg_prog( void )  
254  {  char*
255      char *pgm = NULL;  get_gnupg_default_key (void)
256      int rc = 0;  {    
257        gpg_optfile_t opt = NULL;
258      pgm = get_gnupg_prog( );      gpg_option_t e;
259      if( pgm == NULL )      char * keyid = NULL, * optfile = NULL;
260          rc = WPTERR_GPG_EXEFILE;      int no_usable=0, rc = 0;
261      if( file_exist_check( pgm ) )  
262          rc = WPTERR_GPG_EXEFILE;      optfile = get_gnupg_cfgfile ();
263      free_if_alloc( pgm );      if (!optfile)
264      return rc;          return default_key_from_cache (&no_usable);
265  } /* check_gpg_prog */      rc = parse_gpg_options (optfile, &opt);
266        if (rc) {
267            free_if_alloc( optfile );
268  static int          return default_key_from_cache( &no_usable );
269  parse_version_nr (const char * buf, int *major, int *minor, int *patch)      }
270  {      e = find_option( opt, "default-key" );
271      char tmp[8];      if ( e )
272      int i;          keyid = m_strdup( e->val );
273            if( !e ) {
274      i=0;          e = find_option( opt, "local-user" );
275      while( buf && *buf != '.' && i < 8 )          if( e )
276          tmp[i++] = *buf++;              keyid = m_strdup( e->val );
277      tmp[i] = 0; buf++;      }
278      *major = atol( tmp );      if( !e ) {
279      i=0;          e = find_option( opt, "encrypt-to" );
280      while( buf && *buf != '.' && i < 8 )          if( e )
281          tmp[i++] = *buf++;              keyid = m_strdup( e->val );
282      tmp[i] = 0; buf++;      }
283      *minor = atol( tmp );      free_if_alloc (optfile);
284      i=0;      release_gpg_options (opt);
285      while( buf && isdigit( *buf ) && i < 8 )  
286          tmp[i++] = *buf++;      if (!keyid)
287      tmp[i] = 0;          keyid = default_key_from_cache (&no_usable);
288      *patch = atol( tmp );      return keyid;
289      return 0;  } /* get_gnupg_default_key */
290  }  
291    
292    /* Check if the gpg application (exe file) is available. */
293  int  int
294  check_gnupg_engine (int * r_major, int * r_minor, int * r_patch)  check_gnupg_prog (void)
295  {  {
296      gpgme_ctx_t ctx;      char *pgm = NULL;
297      gpgme_engine_info_t inf;      int rc = 0;
298      char * eng = NULL;  
299      int major=0, minor=0, patch=0;      pgm = get_gnupg_prog ();
300      int rc;      if (!pgm)
301                    rc = WPTERR_GPG_EXEFILE;
302      gpgme_new (&ctx);      if (file_exist_check (pgm))
303      inf = gpgme_ctx_get_engine_info (ctx);          rc = WPTERR_GPG_EXEFILE;
304      if (!inf) {      free_if_alloc (pgm);
305          gpgme_release (ctx);      return rc;
306          return -1;  }
307      }  
308      /* We need to exec GPG again to find out if IDEA is available. */  
309      if (gpg_get_version (&eng))  static int
310          return -1;  parse_version_nr (const char * buf, int *major, int *minor, int *patch)
311      if (strstr (eng, "IDEA"))  {
312          idea_available = 1;      char tmp[8];
313      free (eng);      int i;
314      rc = parse_version_nr( inf->version, &major, &minor, &patch );      
315      if( rc ) {      i=0;
316          gpgme_release (ctx);      while (buf && *buf != '.' && i < 8)
317          return rc;          tmp[i++] = *buf++;
318      }      tmp[i] = 0; buf++;
319      if (major < *r_major || minor < *r_minor)      *major = atol( tmp );
320          rc = 1;      i=0;
321      else {      while (buf && *buf != '.' && i < 8)
322          if (patch < *r_patch)          tmp[i++] = *buf++;
323              rc = 1;      tmp[i] = 0; buf++;
324          rc = 0;      *minor = atol (tmp);
325      }      i=0;
326      *r_major = major;      while (buf && isdigit( *buf ) && i < 8)
327      *r_minor = minor;          tmp[i++] = *buf++;
328      *r_patch = patch;      tmp[i] = 0;
329      return rc;      *patch = atol (tmp);
330  } /* check_gnupg_engine */      return 0;
331    }
332    
333  int  
334  check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings)  /* Check if the gnupg engine fullfills the minimum requirement
335  {     version given in @r_major.@r_minor.@r_patch. On success these
336      gpg_optfile_t opt;         variables contain the GPG version which is installed. */
337      gpg_option_t e;  int
338      int rc = 0;  check_gnupg_engine (int *r_major, int *r_minor, int *r_patch)
339    {
340      if( r_secrings )      gpgme_ctx_t ctx;
341          *r_secrings = 0;      gpgme_engine_info_t inf;
342      if( r_pubrings )      char * eng = NULL;
343          *r_pubrings = 0;      int major=0, minor=0, patch=0;
344      rc = parse_gpg_options( fname, &opt );      int rc;
345      if( rc )          
346          return WPTERR_FILE_OPEN;      gpgme_new (&ctx);
347        inf = gpgme_ctx_get_engine_info (ctx);
348      for( e = opt->list; e; e = e->next ) {      if (!inf) {
349          if( !strcmp( e->name, "secret-keyring" ) ) {          gpgme_release (ctx);
350              if( !file_exist_check( e->val ) )          return -1;
351                  r_secrings[0]++;      }
352          }      /* We need to exec GPG again to find out if IDEA is available. */
353          else if( !strcmp( e->name, "keyring" ) ) {      if (gpg_get_version (&eng))
354              if( !file_exist_check( e->val ) )          return -1;
355                  r_pubrings[0]++;      if (strstr (eng, "IDEA"))
356          }          idea_available = 1;
357      }      free (eng);
358      release_gpg_options( opt );      rc = parse_version_nr( inf->version, &major, &minor, &patch );
359      return 0;      if( rc ) {
360  } /* check_gnupg_cfgfile */          gpgme_release (ctx);
361            return rc;
362  /*      }
363   * Check if both keyrings are located in the gnupg home directory.  
364   */      /* FIXME: This check is wrong! */
365  int      if (major < *r_major || minor < *r_minor)
366  gnupg_access_files (void)          rc = 1;
367  {      else {
368      int rc = 0;          if (patch < *r_patch)
369      int pubring_ok = 0, secring_ok = 0;              rc = 1;
370      int secrings = 0, pubrings = 0;          rc = 0;
371      char *optfile;      }
372        *r_major = major;
373      if (gnupg_access_keyring (1))      *r_minor = minor;
374          rc = WPTERR_GPG_KEYRINGS;      *r_patch = patch;
375      else      return rc;
376          pubring_ok = 1;  }
377    
378      if (gnupg_access_keyring (0))  
379          rc = WPTERR_GPG_KEYRINGS;  int
380      else  check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings)
381          secring_ok = 1;  {
382      if (!pubring_ok || !secring_ok) {      gpg_optfile_t opt;    
383          optfile = get_gnupg_cfgfile ();      gpg_option_t e;
384          if (!optfile)      int rc = 0;
385              return WPTERR_GPG_KEYRINGS;  
386          rc = file_exist_check (optfile);      *r_secrings = 0;
387          if (!rc && get_file_size(optfile) > 0) {      *r_pubrings = 0;
388              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);      rc = parse_gpg_options( fname, &opt );
389              if (!rc && secrings && pubrings) {      if( rc )
390                  free_if_alloc (optfile);          return WPTERR_FILE_OPEN;
391                  return 0; /* found two keyrings in the option file */  
392              }      for( e = opt->list; e; e = e->next ) {
393              else if ((!rc && pubrings && secring_ok)          if( !strcmp( e->name, "secret-keyring" ) ) {
394                    || (!rc && secrings && pubring_ok)) {              if( !file_exist_check( e->val ) )
395                  free_if_alloc (optfile);                  r_secrings[0]++;
396                  return 0; /* found one keyring and one entry in the options file */          }
397              }          else if( !strcmp( e->name, "keyring" ) ) {
398              else              if( !file_exist_check( e->val ) )
399                  return WPTERR_GPG_OPT_KEYRINGS;                  r_pubrings[0]++;
400          }          }
401          free_if_alloc (optfile);      }
402          rc = WPTERR_GPG_KEYRINGS;      release_gpg_options( opt );
403      }      return 0;
404      return rc;  } /* check_gnupg_cfgfile */
405  } /* gnupg_access_files */  
406    
407    /*
408  static int   * Check if both keyrings are located in the gnupg home directory.
409  create_gpg_options( void )   */
410  {  int
411      FILE *fp;  gnupg_access_files (void)
412      char *s, *optfile;  {
413        int rc = 0;
414      s = get_gnupg_path( );      int pubring_ok = 0, secring_ok = 0;
415      if( s == NULL )      int secrings = 0, pubrings = 0;
416          return WPTERR_FILE_CREAT;      char *optfile;
417      optfile = make_filename( s, GPG_CONF, NULL );  
418      fp = fopen( optfile, "wb" );      if (gnupg_access_keyring (1))
419      if( fp == NULL ) {            rc = WPTERR_GPG_KEYRINGS;
420          return WPTERR_FILE_CREAT;      else
421          goto fail;          pubring_ok = 1;
422      }  
423      fwrite( options_skel, 1, strlen( options_skel ), fp );      if (gnupg_access_keyring (0))
424      fclose( fp );          rc = WPTERR_GPG_KEYRINGS;
425        else
426  fail:          secring_ok = 1;
427      free_if_alloc( s );      if (!pubring_ok || !secring_ok) {
428      free_if_alloc( optfile );          optfile = get_gnupg_cfgfile ();
429      return 0;          if (!optfile)
430  } /* create_gpg_options */              return WPTERR_GPG_KEYRINGS;
431            rc = file_exist_check (optfile);
432            if (!rc && get_file_size(optfile) > 0) {
433  /*              rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings);
434   * Return the contents of the options file as a char buf.              if (!rc && secrings && pubrings) {
435   */                  free_if_alloc (optfile);
436  char *                  return 0; /* found two keyrings in the option file */
437  get_gnupg_config (void)              }
438  {              else if ((!rc && pubrings && secring_ok)
439      FILE * fp;                    || (!rc && secrings && pubring_ok)) {
440      char * p = NULL, * optfile = NULL;                  free_if_alloc (optfile);
441      int fsize, rc = 0;                  return 0; /* found one keyring and one entry in the options file */
442                        }
443      optfile = get_gnupg_cfgfile ();              else
444      if( optfile == NULL )                  return WPTERR_GPG_OPT_KEYRINGS;
445          return NULL;          }
446      fsize = get_file_size( optfile );          free_if_alloc (optfile);
447      if( !fsize ) {          rc = WPTERR_GPG_KEYRINGS;
448          rc = create_gpg_options( );      }
449          if ( rc )      return rc;
450              return NULL;  } /* gnupg_access_files */
451          fsize = get_file_size( optfile );  
452      }  
453      if( fsize > 100000 )  static int
454          goto leave; /* too large */  create_gpg_options (void)
455      p = new char[fsize+1];  {
456      if( p == NULL )      FILE *fp;
457          BUG( NULL );      char *s, *optfile;
458      fp = fopen( optfile, "rb" );  
459      if( fp == NULL ) {      s = get_gnupg_path( );
460          free_if_alloc( p );      if( s == NULL )
461          return NULL;          return WPTERR_FILE_CREAT;
462      }      optfile = make_filename( s, GPG_CONF, NULL );
463      fread( p, 1, fsize, fp );      fp = fopen( optfile, "wb" );
464      fclose( fp );      if( fp == NULL ) {  
465      p[fsize] = '\0';          return WPTERR_FILE_CREAT;
466      free_if_alloc( optfile );          goto fail;
467        }
468  leave:      fwrite( options_skel, 1, strlen( options_skel ), fp );
469      return p;      fclose( fp );
470  } /* get_gnupg_config */  
471    fail:
472        free_if_alloc( s );
473  int      free_if_alloc( optfile );
474  set_gnupg_default_key (const char * key)      return 0;
475  {  } /* create_gpg_options */
476      gpg_optfile_t opt;  
477      gpg_option_t e;  
478      char *optfile = NULL;  /*
479      int rc = 0;   * Return the contents of the options file as a char buf.
480     */
481      optfile = get_gnupg_cfgfile ();  char *
482      if (!optfile)  get_gnupg_config (void)
483          return -1;  {
484      rc = parse_gpg_options (optfile, &opt);      FILE * fp;
485      if( rc ) {      char * p = NULL, * optfile = NULL;
486          free_if_alloc (optfile);      int fsize, rc = 0;
487          return -1;          
488      }      optfile = get_gnupg_cfgfile ();
489      e = find_option (opt, "default-key");      if( optfile == NULL )
490      if (e) {          return NULL;
491          free_if_alloc (e->val);      fsize = get_file_size( optfile );
492          e->val = m_strdup (key);      if( !fsize ) {
493          e->used = 1;          rc = create_gpg_options( );
494      }          if ( rc )
495      else              return NULL;
496          add_entry (opt, ENTRY_MULTI, "default-key", key);          fsize = get_file_size( optfile );
497      rc = commit_gpg_options (optfile, opt);      }
498        if( fsize > 100000 )
499      free_if_alloc (optfile);          goto leave; /* too large */
500      release_gpg_options (opt);      p = new char[fsize+1];
501        if( p == NULL )
502      return rc;          BUG( NULL );
503  } /* set_gnupg_default_key */      fp = fopen( optfile, "rb" );
504        if( fp == NULL ) {
505            free_if_alloc( p );
506  /*          return NULL;
507   * Set the contents of the options file.      }
508   */      fread( p, 1, fsize, fp );
509  int      fclose( fp );
510  set_gnupg_options( const char *buf, size_t buflen )      p[fsize] = '\0';
511  {      free_if_alloc( optfile );
512      FILE *fp;    
513      char *optfile = NULL;  leave:
514        return p;
515      optfile = get_gnupg_cfgfile( );  } /* get_gnupg_config */
516      if( optfile == NULL )  
517          return WPTERR_FILE_CREAT;  
518    int
519      fp = fopen( optfile, "wb" );  set_gnupg_default_key (const char * key)
520      if( fp == NULL ) {  {
521          free_if_alloc( optfile );      gpg_optfile_t opt;
522          return WPTERR_FILE_CREAT;      gpg_option_t e;
523      }      char *optfile = NULL;
524      fwrite( buf, 1, buflen, fp );      int rc = 0;
525      fclose( fp );  
526      free_if_alloc( optfile );      optfile = get_gnupg_cfgfile ();
527      return 0;      if (!optfile)
528  } /* set_gnupg_options */          return -1;
529        rc = parse_gpg_options (optfile, &opt);
530  /*      if( rc ) {
531   * Check if the line contains a valid GPG argument.          free_if_alloc (optfile);
532   */          return -1;
533  static int      }
534  check_line( const char *buf )      e = find_option (opt, "default-key");
535  {      if (e) {
536      int j, len;          free_if_alloc (e->val);
537      int rc = 0;          e->val = m_strdup (key);
538            e->used = 1;
539      if( *buf == '#' || *buf == '\r' || *buf == '\n' )      }
540          return 1;      else
541      rc = 0;          add_entry (opt, ENTRY_MULTI, "default-key", key);
542      for ( j = 0; valid_gpg_args[j]; j++ ) {      rc = commit_gpg_options (optfile, opt);
543          len = strlen( valid_gpg_args[j] );  
544          if( !strncmp( valid_gpg_args[j], buf, len ) )      free_if_alloc (optfile);
545              rc = 1;          release_gpg_options (opt);
546      }  
547        return rc;
548      return rc;  } /* set_gnupg_default_key */
549  } /* check_line */  
550    
551    /*
552  int   * Set the contents of the options file.
553  check_gnupg_options( const char *buf )   */
554  {  int
555      char line[1024];  set_gnupg_options( const char *buf, size_t buflen )
556      int nbytes = 0;  {
557      unsigned j;      FILE *fp;  
558                char *optfile = NULL;
559      for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) {  
560          line[nbytes++] = buf[j];      optfile = get_gnupg_cfgfile( );
561          if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) {      if( optfile == NULL )
562              line[nbytes] = '\0';          return WPTERR_FILE_CREAT;
563              if( !check_line( line ) ) {  
564                  msg_box( NULL, line, "options", MB_OK );      fp = fopen( optfile, "wb" );
565                  return 1;            if( fp == NULL ) {
566              }          free_if_alloc( optfile );
567              nbytes = 0;          return WPTERR_FILE_CREAT;
568          }            }
569      }      fwrite( buf, 1, buflen, fp );
570        fclose( fp );
571      return 0;      free_if_alloc( optfile );
572  } /* check_gnupg_options */      return 0;
573    } /* set_gnupg_options */
574    
575  static int  /*
576  get_last_gnupg_access (gpg_watcher_s * ctx)   * Check if the line contains a valid GPG argument.
577  {   */
578      HANDLE fd;  static int
579      char *path = NULL, *file = NULL;  check_line( const char *buf )
580    {
581      path = get_gnupg_path ();      int j, len;
582      file =  make_filename (path, ctx->object, NULL);      int rc = 0;
583      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);  
584      if( fd == INVALID_HANDLE_VALUE ) {      if( *buf == '#' || *buf == '\r' || *buf == '\n' )
585          free_if_alloc (path);          return 1;
586          free_if_alloc (file);      rc = 0;
587          return WPTERR_FILE_OPEN;      for ( j = 0; valid_gpg_args[j]; j++ ) {
588      }          len = strlen( valid_gpg_args[j] );
589      GetFileTime (fd, NULL, NULL, &ctx->access);          if( !strncmp( valid_gpg_args[j], buf, len ) )
590      CloseHandle (fd);              rc = 1;    
591      free_if_alloc (path);            }
592      free_if_alloc (file);  
593      return 0;      return rc;
594  } /* get_last_gnupg_access */  } /* check_line */
595    
596    
597  static void  int
598  check_last_gnupg_access( gpg_watcher_s *ctx )  check_gnupg_options( const char *buf )
599  {                {
600      ctx->modified = 0;      char line[1024];
601        int nbytes = 0;
602      if( ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&      unsigned j;
603          ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime )            
604          ctx->modified = 1;      for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) {
605            line[nbytes++] = buf[j];
606      ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;          if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) {
607      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;              line[nbytes] = '\0';
608  } /* check_last_gnupg_access */              if( !check_line( line ) ) {
609                    msg_box( NULL, line, "options", MB_OK );
610                    return 1;      
611  void              }
612  init_gnupg_table (void)              nbytes = 0;
613  {                }      
614      int j;      }
615    
616      for (j = 0; j < gpg_table_count; j++) {      return 0;
617          gpg_table[j].object = m_strdup (gpg_objs[j]);  } /* check_gnupg_options */
618          memset (&gpg_table[j].access, 0, sizeof (FILETIME));  
619          memset (&gpg_table[j].last_access, 0, sizeof (FILETIME));  
620          gpg_table[j].modified = 0;  /* Store the last access of the file inside the watcher @ctx. */
621      }  static int
622  } /* init_gnupg_table */  get_last_gnupg_access (gpg_watcher_s *ctx)
623    {
624        HANDLE fd;
625  void      char *path;
626  free_gnupg_table (void)      char *file;
627  {  
628      int j;      path = get_gnupg_path ();
629        file =  make_filename (path, ctx->object, NULL);
630      for (j=0; j < gpg_table_count; j++)      fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
631          free_if_alloc (gpg_table[j].object);      if (fd == INVALID_HANDLE_VALUE) {
632  } /* free_gnupg_table */          free_if_alloc (path);
633            free_if_alloc (file);
634            return WPTERR_FILE_OPEN;
635  int      }
636  keyring_check_last_access (void)      GetFileTime (fd, NULL, NULL, &ctx->access);
637  {            CloseHandle (fd);
638      int rc, j;      free_if_alloc (path);
639        free_if_alloc (file);
640      rc = 0;      return 0;
641      for (j = 0; j < gpg_table_count; j++) {  }
642          get_last_gnupg_access( &gpg_table[j] );  
643          check_last_gnupg_access( &gpg_table[j] );  
644          if ( gpg_table[j].modified )  /* Check if the file inside watcher @ctx was modified. */
645              rc++;        static void
646      }  check_last_gnupg_access (gpg_watcher_s *ctx)
647    {              
648      return rc;      ctx->modified = 0;
649  } /* keyring_check_last_access */  
650        if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime &&
651            ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime)
652  const char *          ctx->modified = 1;
653  gnupg_check_file_ext (const char * fname)  
654  {                    ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime;
655      char file_ext[5];      ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime;
656    }
657      if (!strchr( fname, '.' ))  
658          return "UNKNOWN";  
659    /* Init GPG watcher table for all monitored files. */
660      strncpy (file_ext, fname + strlen (fname) - 4, 4);  void
661      file_ext[4] = '\0';  init_gnupg_table (void)
662      if (!stricmp (file_ext, ".asc"))  {      
663          return "ARMORED";      char *p;
664      else if (!stricmp (file_ext, ".sig"))      int j;
665          return "SIGNED";  
666      else if  (!stricmp (file_ext, ".gpg")      for (j = 0; j < gpg_table_count; j++) {
667          || !stricmp (file_ext, ".pgp"))          p = gpg_table[j].object = m_strdup (gpg_objs[j]);
668          return "ENCRYPTED";          if (!p)
669      else              BUG (NULL);
670          return "UNKNOWN";          memset (&gpg_table[j].access, 0, sizeof (FILETIME));
671                    memset (&gpg_table[j].last_access, 0, sizeof (FILETIME));
672      return "UNKNOWN";          gpg_table[j].modified = 0;
673  } /* gnupg_check_file_ext */      }
674    }
675    
676  char *  
677  get_gnupg_keyring_from_options (const char * fname, int pub)  void
678  {  free_gnupg_table (void)
679      gpg_optfile_t opt;  {
680      gpg_option_t e;      int j;
681      char * kring = NULL;  
682      int rc = 0;      for (j=0; j < gpg_table_count; j++)
683            free_if_alloc (gpg_table[j].object);
684      rc = parse_gpg_options (fname, &opt);  }
685      if (rc)  
686          return NULL;  
687      if (pub)  /* Return the amount of files modified since the last call. */
688          e = find_option (opt, "keyring");  int
689      else  keyring_check_last_access (void)
690          e = find_option (opt, "secret-keyring");  {      
691      if (e)      int rc, j;
692          kring = m_strdup (e->val);  
693      release_gpg_options (opt);      rc = 0;
694        for (j = 0; j < gpg_table_count; j++) {
695      return kring;          get_last_gnupg_access (&gpg_table[j]);
696  } /* get_gnupg_keyring_from_options */          check_last_gnupg_access (&gpg_table[j]);
697            if (gpg_table[j].modified)
698                rc++;      
699        }
700  /* XXX: does not work with write-protected floppies */  
701  static int      return rc;
702  my_access (const char * fname)  }
703  {  
704      HANDLE hd;  
705      hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  const char*
706      if (hd == INVALID_HANDLE_VALUE)  gnupg_check_file_ext (const char *fname, int *r_type)
707          return -1;  {              
708      CloseHandle (hd);      char file_ext[5];
709      return 0;  
710  } /* my_access */      if (r_type)
711            *r_type = PGP_NONE;
712        if (!strchr (fname, '.' ))
713  int          return "UNKNOWN";
714  gpg_check_permissions (int showmsg)  
715  {      strncpy (file_ext, fname + strlen (fname) - 4, 4);
716      char * p, * name = NULL;      file_ext[4] = '\0';
717      int failed = 0, ans=0, attrs=0;      if (!stricmp (file_ext, ".asc"))
718            return "ARMORED";
719      p = get_gnupg_path ();      else if (!stricmp (file_ext, ".sig")) {
720      check_keyring (&p);          if (r_type)
721      if (p) {              *r_type = PGP_SIG;
722          name = make_filename (p, "pubring", "gpg");          return "SIGNED";
723          free_if_alloc (p);      }
724          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {      else if  (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) {
725              ans = msg_box (NULL,          if (r_type)
726                             _("The selected keyring has the read-only file\n"              *r_type = PGP_MESSAGE;
727                               "attribute. In this state you do not have write\n"          return "ENCRYPTED";
728                               "access. Do you want to remove the attribute?"),      }
729                       _("GPG Information"), MB_YESNO);      return "UNKNOWN";
730              if (ans == IDYES) {  }
731                  attrs &= ~FILE_ATTRIBUTE_READONLY;  
732                  if (!SetFileAttributes (name, attrs)) {  
733                      msg_box (NULL, _("Could not reset read-only state."),  char*
734                               _("GPG Error"), MB_ERR);  get_gnupg_keyring_from_options (const char * fname, int pub)
735                      failed = 1;  {
736                  }      gpg_optfile_t opt;
737              }      gpg_option_t e;
738              else if (ans == IDNO) {      char * kring = NULL;
739                  /*      int rc = 0;
740                  msg_box (NULL, _("All commands with write access to the keyring\n"  
741                                   "will be disabled."), _("GPG Information"), MB_INFO);      rc = parse_gpg_options (fname, &opt);
742                  */      if (rc)
743                  failed = 1;          return NULL;
744              }      if (pub)
745          }          e = find_option (opt, "keyring");
746          if (my_access (name)) {      else
747              if (showmsg)          e = find_option (opt, "secret-keyring");
748                  msg_box (NULL,      if (e)
749                  _("You do not have file access to modify the contents of\n"          kring = m_strdup (e->val);
750                    "one or both of the selected keyrings.\n"      release_gpg_options (opt);
751                    "\n"  
752                    "The keyrings are in a read-only state which is propably\n"      return kring;
753                    "caused by another program which already opened the files.\n"),  }
754                     _("GPG Warning"), MB_WARN);  
755              failed = 2;  
756          }  
757      }  /* XXX: does not work with write-protected floppies */
758      free_if_alloc (name);  static int
759      return failed;  my_access (const char * fname)
760  } /* gpg_check_permissions */  {
761        HANDLE hd;
762        hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE,
763  static int                       NULL, OPEN_EXISTING, 0, NULL);
764  check_homedir (void)      if (hd == INVALID_HANDLE_VALUE)
765  {                return -1;
766      char * homedir = NULL;      CloseHandle (hd);
767      int yes = 0;      return 0;
768    }
769      homedir = get_reg_entry_gpg ("HomeDir");  
770      if (!homedir)  
771          homedir = multi_gnupg_path ();  int
772      if (!homedir)  gpg_check_permissions (int showmsg)
773          homedir = m_strdup ("c:\\gnupg");  {
774      if (homedir) {      char * p, * name = NULL;
775          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {      int failed = 0, ans=0, attrs=0;
776              yes = log_box (_("Preferences"), MB_YESNO,  
777                             _("%s does not exit.\n"      p = get_gnupg_path ();
778                               "Do you want to create this directory?"), homedir);      check_keyring (&p);
779              if (yes == IDYES) {      if (p) {
780                  if (CreateDirectory (homedir, NULL) == FALSE) {          name = make_filename (p, "pubring", "gpg");
781                      free_if_alloc (homedir);          free_if_alloc (p);
782                      return WPTERR_DIR_CREAT;          if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) {
783                  }              ans = msg_box (NULL,
784              }                             _("The selected keyring has the read-only file\n"
785              return WPTERR_DIR_OPEN;                               "attribute. In this state you do not have write\n"
786          }                               "access. Do you want to remove the attribute?"),
787          free_if_alloc (homedir);                       _("GPG Information"), MB_YESNO);
788      }              if (ans == IDYES) {
789      return 0;                  attrs &= ~FILE_ATTRIBUTE_READONLY;
790  } /* check_homedir */                  if (!SetFileAttributes (name, attrs)) {
791                        msg_box (NULL, _("Could not reset read-only state."),
792                                 _("GPG Error"), MB_ERR);
793  int                      failed = 1;
794  gnupg_check_homedir (void)                  }
795  {                    }
796      char *homedir = NULL, *prog = NULL;              else if (ans == IDNO) {
797      int rc = 0, ec = 0;                  /*
798                        msg_box (NULL, _("All commands with write access to the keyring\n"
799      rc = check_homedir ();                                   "will be disabled."), _("GPG Information"), MB_INFO);
800      if (rc)                  */
801          return rc;                  failed = 1;
802      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&              }
803          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {          }
804          prog = make_filename (homedir, "gpg", "exe");          if (my_access (name)) {
805          if (file_exist_check (prog) == 0) {              if (showmsg)
806              rc = set_reg_entry_gpg ("gpgProgram", prog);                  msg_box (NULL,
807              if (rc)                  _("You do not have file access to modify the contents of\n"
808                  goto fail;                    "one or both of the selected keyrings.\n"
809          }                    "\n"
810          free_if_alloc (homedir);                    "The keyrings are in a read-only state which is propably\n"
811          free_if_alloc (prog);                    "caused by another program which already opened the files.\n"),
812          return rc;                     _("GPG Warning"), MB_WARN);
813      }              failed = 2;
814      if ((prog = get_reg_entry_gpg ("gpgProgram"))          }
815          && file_exist_check (prog)) {      }
816          free_if_alloc (prog);      free_if_alloc (name);
817          homedir = get_reg_entry_gpg ("HomeDir");      return failed;
818          if (!homedir) {  } /* gpg_check_permissions */
819              rc = WPTERR_GENERAL;  
820              goto fail;  
821          }  /* Check the GPG home dir. If all methods failed, try to
822          prog = make_filename (homedir, "gpg", "exe");     create the default folder. */
823          if (file_exist_check (prog) == 0) {  static int
824              rc = set_reg_entry_gpg ("gpgProgram", prog);  check_homedir (void)
825              if (rc)  {      
826                  goto fail;      char *homedir = NULL;
827              free_if_alloc (prog);      int yes = 0;
828              return rc;  
829          }      homedir = get_reg_entry_gpg ("HomeDir");
830      }      if (!homedir)
831                homedir = multi_gnupg_path ();
832      /* Change the return code if homedir doesn't exist or if the program      if (!homedir)
833         doesn't exist. Note that exist_checks return 0 to suggest existance. */          homedir = m_strdup ("c:\\gnupg");
834      if ((!homedir || dir_exist_check (homedir)))      if (homedir) {
835          rc = WPTERR_GENERAL;          if (GetFileAttributes (homedir) == 0xFFFFFFFF) {
836                    yes = log_box (_("Preferences"), MB_YESNO,
837  fail:                             _("%s does not exit.\n"
838      free_if_alloc (homedir);                               "Do you want to create this directory?"), homedir);
839      free_if_alloc (prog);              if (yes == IDYES) {
840      return rc;                  BOOL ec = CreateDirectory (homedir, NULL);
841  } /* gnupg_check_homedir */                  free_if_alloc (homedir);
842                    if (ec == FALSE)
843                        return WPTERR_DIR_CREAT;
844  int                  return 0;
845  gnupg_copy_keyrings (void)              }
846  {              return WPTERR_DIR_OPEN;
847      const char * pring, * sring;          }
848      char * file = NULL, * path = NULL;          free_if_alloc (homedir);
849      int id = 0, rc = 0;      }
850      HWND hwnd;      return 0;
851        }
852      path = get_gnupg_path ();  
853      if (!path)  
854          return WPTERR_GENERAL;  int
855      hwnd = GetDesktopWindow ();  gnupg_check_homedir (void)
856    {      
857      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),      char *homedir = NULL;
858                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);      char *prog = NULL;
859      if (!pring) {      int rc = 0, ec = 0;
860          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);      
861          free_if_alloc (path);      rc = check_homedir ();
862          return WPTERR_GENERAL;      if (rc)
863      }          return rc;
864      file = make_filename (path, "pubring", "gpg");      if ((homedir = get_reg_entry_gpg ("HomeDir")) &&
865      if (file_exist_check (file) == 0) {          !(prog = get_reg_entry_gpg ("gpgProgram" ))) {
866          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);          prog = make_filename (homedir, "gpg", "exe");
867          if (id == IDNO)          if (file_exist_check (prog) == 0) {
868              goto fail;              rc = set_reg_entry_gpg ("gpgProgram", prog);
869      }              if (rc)
870      if (!CopyFile (pring, file, FALSE)) {                  goto fail;
871          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);          }
872          rc = WPTERR_FILE_READ;          free_if_alloc (homedir);
873          goto fail;          free_if_alloc (prog);
874      }          return rc;
875      free_if_alloc (file);      }
876        if ((prog = get_reg_entry_gpg ("gpgProgram"))
877      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),          && file_exist_check (prog)) {
878                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);          free_if_alloc (prog);
879      if (!sring) {          homedir = get_reg_entry_gpg ("HomeDir");
880          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );          if (!homedir) {
881          return WPTERR_GENERAL;              rc = WPTERR_GENERAL;
882      }              goto fail;
883      file = make_filename (path, "secring", "gpg");          }
884      if (file_exist_check (file) == 0) {          prog = make_filename (homedir, "gpg", "exe");
885          id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);          if (file_exist_check (prog) == 0) {
886          if( id == IDNO )              rc = set_reg_entry_gpg ("gpgProgram", prog);
887              goto fail;              if (rc)
888      }                  goto fail;
889      if (!CopyFile (sring, file, FALSE)) {              free_if_alloc (prog);
890          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);              return rc;
891          rc = WPTERR_FILE_READ;          }
892      }      }
893        
894  fail:      /* Change the return code if homedir doesn't exist or if the program
895      free_if_alloc (file);         doesn't exist. Note that exist_checks return 0 to suggest existance. */
896      free_if_alloc (path);      if ((!homedir || dir_exist_check (homedir)))
897      return rc;          rc = WPTERR_GENERAL;
898  } /* gnupg_import_keyrings */      
899    fail:
900        free_if_alloc (homedir);
901  void      free_if_alloc (prog);
902  gnupg_backup_options (void)      return rc;
903  {  } /* gnupg_check_homedir */
904      char *cfgfile = NULL;  
905      char bak[512];  
906    int
907      cfgfile = get_gnupg_cfgfile ();  gnupg_copy_keyrings (void)
908      if (cfgfile == NULL)  {
909          return;      const char * pring, * sring;
910      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);      char * file = NULL, * path = NULL;
911      CopyFile (cfgfile, bak, FALSE);      int id = 0, rc = 0;
912      free_if_alloc (cfgfile);      HWND hwnd;
913  } /* gnupg_backup_options */      
914        path = get_gnupg_path ();
915        if (!path)
916            return WPTERR_GENERAL;
917  static int      hwnd = GetDesktopWindow ();
918  backup_one_file (const char * srcpath, const char * srcn,  
919                   const char * dstpath, const char * dstn)      pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"),
920  {                                _("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL);
921      char * src, * dst;      if (!pring) {
922      BOOL rc;          msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR);
923            free_if_alloc (path);
924      src = make_filename (srcpath, srcn, "gpg");          return WPTERR_GENERAL;
925      if (!src)      }
926          BUG (NULL);      file = make_filename (path, "pubring", "gpg");
927      dst = make_filename (dstpath, dstn, "gpg");      if (file_exist_check (file) == 0) {
928      if (!dst)          id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO);
929          BUG (NULL);          if (id == IDNO)
930      rc = CopyFile (src, dst, FALSE);              goto fail;
931      free_if_alloc (src);      }
932      free_if_alloc (dst);      if (!CopyFile (pring, file, FALSE)) {
933      if (!rc)          msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR);
934      {          rc = WPTERR_FILE_READ;
935          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);          goto fail;
936          return WPTERR_GENERAL;      }
937      }      free_if_alloc (file);
938      return 0;  
939  } /* backup_one_file */      sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"),
940                                  _("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL);
941        if (!sring) {
942  static int          msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR );
943  check_keyring (char ** r_path)          return WPTERR_GENERAL;
944  {      }
945      char * p;      file = make_filename (path, "secring", "gpg");
946      char * opt, * name;      if (file_exist_check (file) == 0) {
947            id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO);
948      if (!*r_path)          if( id == IDNO )
949          return 0;              goto fail;
950      p = make_filename (*r_path, "pubring", "gpg");      }
951      if (!p || get_file_size (p) > 0)      if (!CopyFile (sring, file, FALSE)) {
952          return 0;          msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR);
953            rc = WPTERR_FILE_READ;
954      opt = get_gnupg_cfgfile ();      }
955      if (!opt)  
956          BUG (0);  fail:
957      name = get_gnupg_keyring_from_options (opt, 1);      free_if_alloc (file);
958      free_if_alloc (opt);      free_if_alloc (path);
959      free_if_alloc (p);      return rc;
960      if (!name)  } /* gnupg_import_keyrings */
961          return 0;  
962      p = strrchr (name, '\\');  
963      if (!p)  void
964      {  gnupg_backup_options (void)
965          free_if_alloc (name);  {
966          return 0;            char *cfgfile = NULL;
967      }      char bak[512];
968      free_if_alloc (*r_path);  
969      *r_path = new char [strlen (name)+1];      cfgfile = get_gnupg_cfgfile ();
970      memset (*r_path, 0, strlen (name));      if (cfgfile == NULL)
971      strncpy (*r_path, name, (p-name));          return;
972      free_if_alloc (name);      _snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile);
973      return 1;      CopyFile (cfgfile, bak, FALSE);
974  }      free_if_alloc (cfgfile);
975    } /* gnupg_backup_options */
976    
977  static char*  
978  get_backup_name (const char *templ)  
979  {  static int
980      struct tm *tm;  backup_one_file (const char *srcpath, const char *srcn,
981      char *p;                   const char *dstpath, const char *dstn)
982    {
983      time_t t = time (NULL);      char * src, * dst;
984      tm = localtime (&t);      BOOL rc;
985      p = new char [strlen (templ) + 8 + 1];  
986      if (!p)      src = make_filename (srcpath, srcn, "gpg");
987          BUG (0);      if (!src)
988      sprintf (p, "%s-%d", templ, tm->tm_wday % 3);          BUG (NULL);
989      return p;      dst = make_filename (dstpath, dstn, "gpg");
990  }      if (!dst)
991            BUG (NULL);
992        rc = CopyFile (src, dst, FALSE);
993  void      free_if_alloc (src);
994  gnupg_backup_keyrings (void)      free_if_alloc (dst);
995  {      if (!rc)
996      char *srcpath = NULL, *dstpath = NULL;      {
997      char *name=NULL;          log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn);
998      int rc, bakmode=0;          return WPTERR_GENERAL;
999        }
1000      if (!reg_prefs.auto_backup)      return 0;
1001          return;  } /* backup_one_file */
1002      bakmode = reg_prefs.backup.mode;  
1003      srcpath =  get_gnupg_path ();  
1004      check_keyring (&srcpath);  static int
1005      if (bakmode == 1) {  check_keyring (char ** r_path)
1006          dstpath = get_gnupg_path ();  {
1007          check_keyring (&dstpath);      char * p;
1008      }      char * opt, * name;
1009      else if (bakmode == 2) {  
1010          char * tmpfile;      if (!*r_path)
1011          FILE * fp;          return 0;
1012        p = make_filename (*r_path, "pubring", "gpg");
1013          dstpath = m_strdup (reg_prefs.backup.path);      if (!p || get_file_size (p) > 0)
1014          if (!dstpath)          return 0;
1015              BUG (0);  
1016          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");      opt = get_gnupg_cfgfile ();
1017          fp = fopen (tmpfile, "wb");      if (!opt)
1018          if (!fp)          BUG (0);
1019              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n"      name = get_gnupg_keyring_from_options (opt, 1);
1020                                                                   "Please insert/check the drive to continue."), dstpath);      free_if_alloc (opt);
1021          else {      free_if_alloc (p);
1022              rc = 0;      if (!name)
1023              fclose (fp);          return 0;
1024              unlink (tmpfile);      p = strrchr (name, '\\');
1025          }      if (!p)
1026          free_if_alloc (tmpfile);      {
1027          if (!fp || rc == IDCANCEL)          free_if_alloc (name);
1028              return;          return 0;      
1029      }      }
1030      else {      free_if_alloc (*r_path);
1031          log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode);      *r_path = new char [strlen (name)+1];
1032          return;      memset (*r_path, 0, strlen (name));
1033      }      strncpy (*r_path, name, (p-name));
1034      name = get_backup_name ("pubring-bak");      free_if_alloc (name);
1035      rc = backup_one_file (srcpath, "pubring", dstpath, name);      return 1;
1036      if (!rc)  }
1037          rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak");  
1038      free_if_alloc (name);  
1039      free_if_alloc (srcpath);  static char*
1040      free_if_alloc (dstpath);  get_backup_name (const char *templ)
1041  } /* gnupg_backup_keyrings */  {
1042        struct tm *tm;
1043        char *p;
1044  void  
1045  gnupg_display_error (void)      time_t t = time (NULL);
1046  {            tm = localtime (&t);
1047      char tmpath[512], * errstr;      p = new char [strlen (templ) + 8 + 1];
1048      size_t size = 0;      if (!p)
1049      FILE * fp;          BUG (0);
1050        sprintf (p, "%s-%d", templ, tm->tm_wday % 3);
1051      GetTempPath (sizeof tmpath - 32, (tmpath));      return p;
1052      strcat (tmpath, "gpg_stderr");  }
1053      size = get_file_size (tmpath);  
1054      if (file_exist_check (tmpath) || size <= 0)  
1055          return;  void
1056      fp = fopen( tmpath, "rb" );  gnupg_backup_keyrings (void)
1057      if (!fp) {  {
1058          msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO );      char *srcpath = NULL, *dstpath = NULL;
1059          return;      char *name=NULL;
1060      }      int rc, bakmode=0;
1061      errstr = new char[size+1];  
1062      if (!errstr)      if (!reg_prefs.auto_backup)
1063          BUG (0);          return;
1064      fread (errstr, 1, size, fp);      bakmode = reg_prefs.backup.mode;
1065      errstr[size] = '\0';      srcpath =  get_gnupg_path ();
1066      fclose (fp);      check_keyring (&srcpath);
1067      msg_box (NULL, errstr, _("GPG Error"), MB_INFO);      if (bakmode == 1) {
1068      free_if_alloc (errstr);          dstpath = get_gnupg_path ();
1069  } /* gnupg_display_error */          check_keyring (&dstpath);
1070        }
1071        else if (bakmode == 2) {
1072  int          char * tmpfile;
1073  gnupg_access_keyring (int _pub)          FILE * fp;
1074  {  
1075      int rc = 0;          dstpath = m_strdup (reg_prefs.backup.path);
1076      char * name = get_gnupg_keyring (_pub, 1);          if (!dstpath)
1077      if (file_exist_check (name))              BUG (0);
1078          rc = -1;          tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp");
1079      free_if_alloc (name);          fp = fopen (tmpfile, "wb");
1080      return rc;          if (!fp)
1081  }              rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL,
1082                              _("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.24  
changed lines
  Added in v.58

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26