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

Diff of /trunk/Src/wptGPG.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26