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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26