kbx: Use wrapper functions for es_fclose and es_fopen.

* kbx/keybox-defs.h (KEYBOX_LL_OPEN_READ)
(KEYBOX_LL_OPEN_UPDATE, KEYBOX_LL_OPEN_CREATE): New.
* kbx/keybox-init.c (_keybox_ll_open): New.  Replace all keybox use of
es_fopen by this function.
(_keybox_ll_close): New.  Replace all keybox use of es_fclose by this
function.
--

Note that this has not been done for the utilities and the backend-kbx
of keyboxd.
This commit is contained in:
Werner Koch 2023-05-05 11:37:44 +02:00
parent a7dbf11954
commit a6c4d6413a
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
4 changed files with 120 additions and 88 deletions

View File

@ -136,6 +136,14 @@ typedef struct _keybox_openpgp_info *keybox_openpgp_info_t;
/* } keybox_opt; */ /* } keybox_opt; */
/*-- keybox-init.c --*/ /*-- keybox-init.c --*/
#define KEYBOX_LL_OPEN_READ 0
#define KEYBOX_LL_OPEN_UPDATE 1
#define KEYBOX_LL_OPEN_CREATE 2
gpg_error_t _keybox_ll_open (estream_t *rfp, const char *fname,
unsigned int mode);
gpg_error_t _keybox_ll_close (estream_t fp);
void _keybox_close_file (KEYBOX_HANDLE hd); void _keybox_close_file (KEYBOX_HANDLE hd);

View File

@ -180,7 +180,7 @@ keybox_release (KEYBOX_HANDLE hd)
_keybox_release_blob (hd->saved_found.blob); _keybox_release_blob (hd->saved_found.blob);
if (hd->fp) if (hd->fp)
{ {
es_fclose (hd->fp); _keybox_ll_close (hd->fp);
hd->fp = NULL; hd->fp = NULL;
} }
xfree (hd->word_match.name); xfree (hd->word_match.name);
@ -236,6 +236,47 @@ keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes)
} }
/* Low-level open function to be used for keybox files. This function
* also manages custom buffering. On success 0 is returned and a new
* file pointer stored at RFP; on error an error code is returned and
* NULL is stored at RFP. MODE is one of
* KEYBOX_LL_OPEN_READ(0) := fopen mode is "rb"
* KEYBOX_LL_OPEN_UPDATE := fopen mode is "r+b"
* KEYBOX_LL_OPEN_CREATE := fopen mode is "wb"
*/
gpg_error_t
_keybox_ll_open (estream_t *rfp, const char *fname, unsigned int mode)
{
estream_t fp;
*rfp = NULL;
fp = es_fopen (fname,
mode == KEYBOX_LL_OPEN_CREATE
? "wb,sysopen,sequential" :
mode == KEYBOX_LL_OPEN_UPDATE
? "r+b,sysopen,sequential" :
"rb,sysopen,sequential");
if (!fp)
return gpg_error_from_syserror ();
*rfp = fp;
return 0;
}
/* Wrapper around es_fclose to be used for file opened with
* _keybox_ll_open. */
gpg_error_t
_keybox_ll_close (estream_t fp)
{
if (fp && es_fclose (fp))
return gpg_error_from_syserror ();
return 0;
}
/* Close the file of the resource identified by HD. For consistent /* Close the file of the resource identified by HD. For consistent
results this function closes the files of all handles pointing to results this function closes the files of all handles pointing to
the resource identified by HD. */ the resource identified by HD. */
@ -253,7 +294,7 @@ _keybox_close_file (KEYBOX_HANDLE hd)
{ {
if (roverhd->fp) if (roverhd->fp)
{ {
es_fclose (roverhd->fp); _keybox_ll_close (roverhd->fp);
roverhd->fp = NULL; roverhd->fp = NULL;
} }
} }

View File

@ -873,28 +873,12 @@ release_sn_array (struct sn_array_s *array, size_t size)
} }
/* Helper to open the file. */
static gpg_error_t
open_file (KEYBOX_HANDLE hd)
{
hd->fp = es_fopen (hd->kb->fname, "rb,sysopen,sequential");
if (!hd->fp)
{
hd->error = gpg_error_from_syserror ();
return hd->error;
}
return 0;
}
/* /*
*
The search API * The search API
*
*/ */
gpg_error_t gpg_error_t
keybox_search_reset (KEYBOX_HANDLE hd) keybox_search_reset (KEYBOX_HANDLE hd)
@ -914,7 +898,7 @@ keybox_search_reset (KEYBOX_HANDLE hd)
{ {
/* Ooops. Seek did not work. Close so that the search will /* Ooops. Seek did not work. Close so that the search will
* open the file again. */ * open the file again. */
es_fclose (hd->fp); _keybox_ll_close (hd->fp);
hd->fp = NULL; hd->fp = NULL;
} }
} }
@ -992,7 +976,7 @@ keybox_search (KEYBOX_HANDLE hd, KEYBOX_SEARCH_DESC *desc, size_t ndesc,
if (!hd->fp) if (!hd->fp)
{ {
rc = open_file (hd); rc = _keybox_ll_open (&hd->fp, hd->kb->fname, 0);
if (rc) if (rc)
{ {
xfree (sn_array); xfree (sn_array);
@ -1480,7 +1464,7 @@ keybox_seek (KEYBOX_HANDLE hd, off_t offset)
return 0; return 0;
} }
err = open_file (hd); err = _keybox_ll_open (&hd->fp, hd->kb->fname, 0);
if (err) if (err)
return err; return err;
} }

View File

@ -78,10 +78,9 @@ create_tmp_file (const char *template,
err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname); err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
if (!err) if (!err)
{ {
*r_fp = es_fopen (*r_tmpfname, "wb,sysopen,sequential"); err = _keybox_ll_open (r_fp, *r_tmpfname, KEYBOX_LL_OPEN_CREATE);
if (!*r_fp) if (err)
{ {
err = gpg_error_from_syserror ();
xfree (*r_tmpfname); xfree (*r_tmpfname);
*r_tmpfname = NULL; *r_tmpfname = NULL;
xfree (*r_bakfname); xfree (*r_bakfname);
@ -174,31 +173,32 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if ((ec = gnupg_access (fname, W_OK))) if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec); return gpg_error (ec);
fp = es_fopen (fname, "rb,sysopen,sequential"); rc = _keybox_ll_open (&fp, fname, 0);
if (mode == FILECOPY_INSERT && !fp && errno == ENOENT) if (mode == FILECOPY_INSERT && gpg_err_code (rc) == GPG_ERR_ENOENT)
{ {
/* Insert mode but file does not exist: /* Insert mode but file does not exist:
Create a new keybox file. */ * Create a new keybox file. */
newfp = es_fopen (fname, "wb,sysopen,sequential"); rc = _keybox_ll_open (&newfp, fname, KEYBOX_LL_OPEN_CREATE);
if (!newfp ) if (rc)
return gpg_error_from_syserror (); return rc;
rc = _keybox_write_header_blob (newfp, for_openpgp); rc = _keybox_write_header_blob (newfp, for_openpgp);
if (rc) if (rc)
{ {
es_fclose (newfp); _keybox_ll_close (newfp);
return rc; return rc;
} }
rc = _keybox_write_blob (blob, newfp, NULL); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
{ {
es_fclose (newfp); _keybox_ll_close (newfp);
return rc; return rc;
} }
if ( es_fclose (newfp) ) rc = _keybox_ll_close (newfp);
return gpg_error_from_syserror (); if (rc)
return rc;
/* if (chmod( fname, S_IRUSR | S_IWUSR )) */ /* if (chmod( fname, S_IRUSR | S_IWUSR )) */
/* { */ /* { */
@ -218,7 +218,7 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc) if (rc)
{ {
es_fclose (fp); _keybox_ll_close (fp);
goto leave; goto leave;
} }
@ -242,16 +242,16 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if (es_fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
if (es_ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
@ -275,16 +275,16 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if (es_fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
if (es_ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
@ -292,8 +292,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = _keybox_read_blob (NULL, fp, NULL); rc = _keybox_read_blob (NULL, fp, NULL);
if (rc) if (rc)
{ {
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
@ -304,8 +304,8 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
rc = _keybox_write_blob (blob, newfp, NULL); rc = _keybox_write_blob (blob, newfp, NULL);
if (rc) if (rc)
{ {
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
@ -318,32 +318,30 @@ blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob,
if (es_fwrite (buffer, nread, 1, newfp) != 1) if (es_fwrite (buffer, nread, 1, newfp) != 1)
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
if (es_ferror (fp)) if (es_ferror (fp))
{ {
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
es_fclose (fp); _keybox_ll_close (fp);
es_fclose (newfp); _keybox_ll_close (newfp);
goto leave; goto leave;
} }
} }
/* Close both files. */ /* Close both files. */
if (es_fclose(fp)) rc = _keybox_ll_close (fp);
if (rc)
{ {
rc = gpg_error_from_syserror (); _keybox_ll_close (newfp);
es_fclose (newfp);
goto leave;
}
if (es_fclose(newfp))
{
rc = gpg_error_from_syserror ();
goto leave; goto leave;
} }
rc = _keybox_ll_close (newfp);
if (rc)
goto leave;
rc = rename_tmp_file (bakfname, tmpfname, fname, secret); rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
@ -502,6 +500,7 @@ keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
int int
keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value) keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
{ {
gpg_error_t err;
off_t off; off_t off;
const char *fname; const char *fname;
estream_t fp; estream_t fp;
@ -536,9 +535,10 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
off += flag_pos; off += flag_pos;
_keybox_close_file (hd); _keybox_close_file (hd);
fp = es_fopen (hd->kb->fname, "r+b,sysopen,sequential");
if (!fp) err = _keybox_ll_open (&fp, fname, KEYBOX_LL_OPEN_UPDATE);
return gpg_error_from_syserror (); if (err)
return err;
ec = 0; ec = 0;
if (es_fseeko (fp, off, SEEK_SET)) if (es_fseeko (fp, off, SEEK_SET))
@ -566,10 +566,11 @@ keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value)
} }
} }
if (es_fclose (fp)) err = _keybox_ll_close (fp);
if (err)
{ {
if (!ec) if (!ec)
ec = gpg_err_code_from_syserror (); ec = gpg_err_code (err);
} }
return gpg_error (ec); return gpg_error (ec);
@ -583,7 +584,7 @@ keybox_delete (KEYBOX_HANDLE hd)
off_t off; off_t off;
const char *fname; const char *fname;
estream_t fp; estream_t fp;
int rc; int rc, rc2;
if (!hd) if (!hd)
return gpg_error (GPG_ERR_INV_VALUE); return gpg_error (GPG_ERR_INV_VALUE);
@ -601,9 +602,9 @@ keybox_delete (KEYBOX_HANDLE hd)
off += 4; off += 4;
_keybox_close_file (hd); _keybox_close_file (hd);
fp = es_fopen (hd->kb->fname, "r+b,sysopen,sequential"); rc = _keybox_ll_open (&fp, hd->kb->fname, KEYBOX_LL_OPEN_UPDATE);
if (!fp) if (rc)
return gpg_error_from_syserror (); return rc;
if (es_fseeko (fp, off, SEEK_SET)) if (es_fseeko (fp, off, SEEK_SET))
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
@ -612,10 +613,11 @@ keybox_delete (KEYBOX_HANDLE hd)
else else
rc = 0; rc = 0;
if (es_fclose (fp)) rc2 = _keybox_ll_close (fp);
if (rc2)
{ {
if (!rc) if (!rc)
rc = gpg_error_from_syserror (); rc = rc2;
} }
return rc; return rc;
@ -628,7 +630,7 @@ int
keybox_compress (KEYBOX_HANDLE hd) keybox_compress (KEYBOX_HANDLE hd)
{ {
gpg_err_code_t ec; gpg_err_code_t ec;
int read_rc, rc; int read_rc, rc, rc2;
const char *fname; const char *fname;
estream_t fp, newfp; estream_t fp, newfp;
char *bakfname = NULL; char *bakfname = NULL;
@ -656,14 +658,11 @@ keybox_compress (KEYBOX_HANDLE hd)
if ((ec = gnupg_access (fname, W_OK))) if ((ec = gnupg_access (fname, W_OK)))
return gpg_error (ec); return gpg_error (ec);
fp = es_fopen (fname, "rb,sysopen,sequential"); rc = _keybox_ll_open (&fp, fname, 0);
if (!fp && errno == ENOENT) if (gpg_err_code (rc) == GPG_ERR_ENOENT)
return 0; /* Ready. File has been deleted right after the access above. */ return 0; /* Ready. File has been deleted right after the access above. */
if (!fp) if (rc)
{ return rc;
rc = gpg_error_from_syserror ();
return rc;
}
/* A quick test to see if we need to compress the file at all. We /* A quick test to see if we need to compress the file at all. We
schedule a compress run after 3 hours. */ schedule a compress run after 3 hours. */
@ -679,7 +678,7 @@ keybox_compress (KEYBOX_HANDLE hd)
if ( (last_maint + 3*3600) > make_timestamp () ) if ( (last_maint + 3*3600) > make_timestamp () )
{ {
es_fclose (fp); _keybox_ll_close (fp);
_keybox_release_blob (blob); _keybox_release_blob (blob);
return 0; /* Compress run not yet needed. */ return 0; /* Compress run not yet needed. */
} }
@ -693,7 +692,7 @@ keybox_compress (KEYBOX_HANDLE hd)
rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp); rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
if (rc) if (rc)
{ {
es_fclose (fp); _keybox_ll_close (fp);
return rc;; return rc;;
} }
@ -782,10 +781,10 @@ keybox_compress (KEYBOX_HANDLE hd)
rc = read_rc; rc = read_rc;
/* Close both files. */ /* Close both files. */
if (es_fclose(fp) && !rc) if ((rc2 = _keybox_ll_close (fp)) && !rc)
rc = gpg_error_from_syserror (); rc = rc2;
if (es_fclose(newfp) && !rc) if ((rc2 = _keybox_ll_close (newfp)) && !rc)
rc = gpg_error_from_syserror (); rc = rc2;
/* Rename or remove the temporary file. */ /* Rename or remove the temporary file. */
if (rc || !any_changes) if (rc || !any_changes)