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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26