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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26