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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26