--- trunk/Src/wptGPG.cpp 2009/09/25 15:54:57 327 +++ trunk/Src/wptGPG.cpp 2009/09/25 16:07:38 328 @@ -1,5 +1,5 @@ /* wptGPG.cpp - GnuPG configuration - * Copyright (C) 2001-2007 Timo Schulz + * Copyright (C) 2001-2009 Timo Schulz * * This file is part of WinPT. * @@ -27,7 +27,6 @@ #include "wptGPG.h" #include "wptGpgCmds.h" -#include "wptGPGOptSkel.h" #include "wptTypes.h" #include "wptNLS.h" #include "wptRegistry.h" @@ -160,7 +159,7 @@ return keyring; } if (file_exist_check (keyring) || - pub && get_file_size (keyring) == 0) { + (pub && get_file_size (keyring) == 0)) { free_if_alloc (keyring); optfile = make_filename (path, GPG_CONF, NULL); keyring = get_keyring_from_conf (optfile, pub); @@ -333,25 +332,21 @@ static int parse_version_nr (const char *buf, int *major, int *minor, int *patch) -{ - char tmp[8]; - int i; +{ + char *p; + char *tmp = m_strdup(buf); - i=0; - while (buf && *buf != '.' && i < 8) - tmp[i++] = *buf++; - tmp[i] = 0; buf++; - *major = atoi (tmp); - i=0; - while (buf && *buf != '.' && i < 8) - tmp[i++] = *buf++; - tmp[i] = 0; buf++; - *minor = atoi (tmp); - i=0; - while (buf && isdigit (*buf) && i < 8) - tmp[i++] = *buf++; - tmp[i] = 0; - *patch = atoi (tmp); + int pos=0; + while ((p = strsep(&tmp, ".")) != NULL) { + switch (++pos) { + case 1: *major = atoi (p); break; + case 2: *minor = atoi (p); break; + case 3: *patch = atoi (p); break; + } + } + delete[] tmp; + if (pos != 3) + return -1; return 0; } @@ -363,7 +358,6 @@ check_gnupg_engine (const char *need_gpg_ver, int *r_major, int *r_minor, int *r_patch) { - gpgme_ctx_t ctx; gpgme_engine_info_t inf; char *eng = NULL; int major=0, minor=0, patch=0; @@ -375,26 +369,18 @@ &need_major, &need_minor, &need_patch)) return 1; - gpgme_new (&ctx); - inf = gpgme_ctx_get_engine_info (ctx); - if (!inf) { - gpgme_release (ctx); - return -1; - } + if (gpgme_get_engine_info (&inf)) + return -1; /* We need to exec GPG again to find out if IDEA is available. */ - if (gpg_get_version (&eng)) { - gpgme_release (ctx); - return -1; - } + if (gpg_get_version (&eng)) + return -1; if (strstr (eng, "IDEA")) idea_available = 1; safe_free (eng); - if (parse_version_nr (inf->version, &major, &minor, &patch)) { - gpgme_release (ctx); + + if (parse_version_nr (inf->version, &major, &minor, &patch)) return 1; - } - gpgme_release (ctx); if (major > need_major) rc = 0; @@ -506,64 +492,34 @@ } -static int -create_gpg_conf (void) -{ - FILE *fp; - char *s, *optfile; - - s = get_gnupg_path (); - if (!s) - return WPTERR_FILE_CREAT; - optfile = make_filename (s, GPG_CONF, NULL); - fp = fopen (optfile, "wb"); - if (!fp) { - return WPTERR_FILE_CREAT; - goto fail; - } - fwrite (options_skel, 1, strlen (options_skel), fp); - fclose (fp); - -fail: - free_if_alloc (s); - free_if_alloc (optfile); - return 0; -} - /* Return the contents of the options file as a char buf. */ char* get_gnupg_config (void) { FILE *fp; - char *p = NULL, *optfile = NULL; + char *p = NULL, *optfile; int fsize; optfile = get_gnupg_cfgfile (); if (!optfile) return NULL; fsize = get_file_size (optfile); - if (!fsize) { - if (create_gpg_conf ()) - return NULL; - fsize = get_file_size (optfile); - } - if (fsize > 100000) - goto leave; /* too large */ + if (fsize < 1 || fsize > 100000) + goto leave; /* too large or does not exist */ + + fp = fopen (optfile, "rb"); + if (!fp) + goto leave; p = new char[fsize+1]; if (!p) BUG (NULL); - fp = fopen( optfile, "rb" ); - if (!fp) { - free_if_alloc (p); - return NULL; - } fread (p, 1, fsize, fp); fclose (fp); p[fsize] = '\0'; - free_if_alloc (optfile); - + leave: + free_if_alloc (optfile); return p; } @@ -747,9 +703,7 @@ void free_gnupg_table (void) { - int j; - - for (j=0; j < gpg_table_count; j++) { + for (int j=0; j < gpg_table_count; j++) { free_if_alloc (gpg_table[j].object); free_if_alloc (gpg_table[j].fpath_object); } @@ -761,10 +715,9 @@ keyring_check_last_access (void) { int nfiles; - int pos; nfiles = 0; - for (pos = 0; pos < gpg_table_count; pos++) { + for (int pos = 0; pos < gpg_table_count; pos++) { get_last_gnupg_access (&gpg_table[pos]); check_last_gnupg_access (&gpg_table[pos]); if (gpg_table[pos].modified) @@ -900,10 +853,6 @@ int val; int rc = 0; -#ifdef WINPT_MOBILE - return 0; -#endif - homedir = get_reg_entry_gpg (GPG_REG_HOME); if (!homedir) homedir = multi_gnupg_path (0); @@ -929,8 +878,8 @@ gnupg_copy_keyrings (void) { const char *pring, *sring; - char *file = NULL, *path = NULL; - int id = 0, rc = 0; + char *file = NULL, *path; + int id, rc = 0; HWND hwnd; path = get_gnupg_path (); @@ -939,22 +888,23 @@ hwnd = GetDesktopWindow (); pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), - "GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL); + "GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL); if (!pring) { - msg_box (hwnd, _("No keyring was chosen. Exit."), + msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); free_if_alloc (path); return WPTERR_GENERAL; } file = make_filename (path, "pubring", "gpg"); if (file_exist_check (file) == 0) { - id = msg_box (hwnd, _("Overwrite old public keyring?"), + id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO); if (id == IDNO) goto fail; } if (!CopyFile (pring, file, FALSE)) { - msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR); + msg_box (hwnd, _("Could not copy public keyring."), + _("WinPT Error"), MB_ERR); rc = WPTERR_FILE_READ; goto fail; } @@ -975,7 +925,7 @@ goto fail; } if (!CopyFile (sring, file, FALSE)) { - msg_box (NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); + msg_box (NULL, _("Could not copy secret keyring."), _("WinPT Error"), MB_ERR); rc = WPTERR_FILE_READ; }