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

Legend:
Removed from v.12  
changed lines
  Added in v.42

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26