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

Diff of /trunk/Src/wptGPG.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26