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

Diff of /trunk/Src/wptGPG.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26