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

Legend:
Removed from v.23  
changed lines
  Added in v.41

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26