/[winpt]/trunk/Src/wptGPG.cpp
ViewVC logotype

Diff of /trunk/Src/wptGPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2  
changed lines
  Added in v.117

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26