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

Legend:
Removed from v.12  
changed lines
  Added in v.78

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26