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

Legend:
Removed from v.22  
changed lines
  Added in v.128

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26