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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26