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

Legend:
Removed from v.22  
changed lines
  Added in v.78

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26