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

Diff of /trunk/Src/wptGPG.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26