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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26