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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26