sm: On Windows close the kbx files at several places.

* kbx/keybox-search.c (keybox_search_reset) [W32]: Always close.

* kbx/keybox-init.c (keybox_close_all_files): New.
* sm/keydb.c (keydb_close_all_files): New.
* sm/call-dirmngr.c (gpgsm_dirmngr_isvalid): Call new function.
(gpgsm_dirmngr_lookup): Ditto.
(gpgsm_dirmngr_run_command): Ditto.
--

We need to make sure that there are no open files on Windows.  Thus we
close them at several strategic locations.

GnuPG-bug-id: 4505
This commit is contained in:
Werner Koch 2021-03-02 19:01:07 +01:00
parent c99f3599d8
commit 2b9ae79ad8
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
6 changed files with 56 additions and 1 deletions

View File

@ -261,6 +261,26 @@ _keybox_close_file (KEYBOX_HANDLE hd)
}
/* Close all the files associated with the resource identified by TOKEN. */
void
keybox_close_all_files (void *token)
{
KB_NAME resource = token;
KEYBOX_HANDLE roverhd;
int idx;
if (!resource)
return;
for (idx=0; idx < resource->handle_table_size; idx++)
if ((roverhd = resource->handle_table[idx]) && roverhd->fp)
{
es_fclose (roverhd->fp);
roverhd->fp = NULL;
}
}
/*
* Lock the keybox at handle HD, or unlock if YES is false.
* Lock the keybox at handle HD, or unlock if YES is false. TIMEOUT

View File

@ -814,6 +814,10 @@ keybox_search_reset (KEYBOX_HANDLE hd)
if (hd->fp)
{
#if HAVE_W32_SYSTEM
es_fclose (hd->fp);
hd->fp = NULL;
#else
if (es_fseeko (hd->fp, 0, SEEK_SET))
{
/* Ooops. Seek did not work. Close so that the search will
@ -821,6 +825,7 @@ keybox_search_reset (KEYBOX_HANDLE hd)
es_fclose (hd->fp);
hd->fp = NULL;
}
#endif
}
hd->error = 0;
hd->eof = 0;

View File

@ -70,6 +70,8 @@ int keybox_is_writable (void *token);
KEYBOX_HANDLE keybox_new_openpgp (void *token, int secret);
KEYBOX_HANDLE keybox_new_x509 (void *token, int secret);
void keybox_close_all_files (void *token);
void keybox_release (KEYBOX_HANDLE hd);
void keybox_push_found_state (KEYBOX_HANDLE hd);
void keybox_pop_found_state (KEYBOX_HANDLE hd);

View File

@ -505,6 +505,8 @@ gpgsm_dirmngr_isvalid (ctrl_t ctrl,
struct inq_certificate_parm_s parm;
struct isvalid_status_parm_s stparm;
keydb_close_all_files ();
rc = start_dirmngr (ctrl);
if (rc)
return rc;
@ -775,6 +777,8 @@ gpgsm_dirmngr_lookup (ctrl_t ctrl, strlist_t names, const char *uri,
if ((names && uri) || (!names && !uri))
return gpg_error (GPG_ERR_INV_ARG);
keydb_close_all_files ();
/* The lookup function can be invoked from the callback of a lookup
function, for example to walk the chain. */
if (!dirmngr_ctx_locked)
@ -1043,6 +1047,8 @@ gpgsm_dirmngr_run_command (ctrl_t ctrl, const char *command,
size_t len;
struct run_command_parm_s parm;
keydb_close_all_files ();
rc = start_dirmngr (ctrl);
if (rc)
return rc;

View File

@ -375,6 +375,23 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
}
/* This is a helper requyired under Windows to close all files so that
* a rename will work. */
void
keydb_close_all_files (void)
{
#ifdef HAVE_W32_SYSTEM
int i;
log_assert (used_resources <= MAX_KEYDB_RESOURCES);
for (i=0; i < used_resources; i++)
if (all_resources[i].type == KEYDB_RESOURCE_TYPE_KEYBOX)
keybox_close_all_files (all_resources[i].token);
#endif
}
KEYDB_HANDLE
keydb_new (void)
{
@ -1080,6 +1097,7 @@ keydb_store_cert (ctrl_t ctrl, ksba_cert_t cert, int ephemeral, int *existed)
records. */
keydb_set_ephemeral (kh, 1);
keydb_close_all_files ();
rc = lock_all (kh);
if (rc)
return rc;
@ -1165,6 +1183,7 @@ keydb_set_cert_flags (ctrl_t ctrl, ksba_cert_t cert, int ephemeral,
if (ephemeral)
keydb_set_ephemeral (kh, 1);
keydb_close_all_files ();
err = keydb_lock (kh);
if (err)
{
@ -1263,6 +1282,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names)
}
}
keydb_close_all_files ();
err = keydb_lock (hd);
if (err)
{
@ -1295,7 +1315,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names)
}
}
if (rc && rc != -1)
log_error ("keydb_search failed: %s\n", gpg_strerror (rc));
log_error ("%s failed: %s\n", __func__, gpg_strerror (rc));
leave:
xfree (desc);

View File

@ -33,6 +33,8 @@ typedef struct keydb_handle *KEYDB_HANDLE;
/*-- keydb.c --*/
gpg_error_t keydb_add_resource (ctrl_t ctrl, const char *url,
int force, int *auto_created);
void keydb_close_all_files (void);
KEYDB_HANDLE keydb_new (void);
void keydb_release (KEYDB_HANDLE hd);
int keydb_set_ephemeral (KEYDB_HANDLE hd, int yes);