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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26