/[winpt]/trunk/Src/wptGPG.cpp
ViewVC logotype

Diff of /trunk/Src/wptGPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26