1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

common: New functions gnupg_opendir et al.

* common/sysutils.h (struct gnupg_dirent_s): New.
* common/sysutils.c: Include dirent.h.
(struct gnupg_dir_s): New.
(gnupg_opendir, gnupg_readdir, gnupg_closedir): New.  Change all
callers of opendir, readdir, and closedir to use these functions.
--

GnuPG-bug-id: 5098
This commit is contained in:
Werner Koch 2020-10-21 16:59:38 +02:00
parent 9a0197b6fe
commit 7e22e08e2a
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
9 changed files with 217 additions and 43 deletions

View File

@ -1370,7 +1370,7 @@ cmd_keyinfo (assuan_context_t ctx, char *line)
ctrl_t ctrl = assuan_get_pointer (ctx); ctrl_t ctrl = assuan_get_pointer (ctx);
int err; int err;
unsigned char grip[20]; unsigned char grip[20];
DIR *dir = NULL; gnupg_dir_t dir = NULL;
int list_mode; int list_mode;
int opt_data, opt_ssh_fpr, opt_with_ssh; int opt_data, opt_ssh_fpr, opt_with_ssh;
ssh_control_file_t cf = NULL; ssh_control_file_t cf = NULL;
@ -1454,7 +1454,7 @@ cmd_keyinfo (assuan_context_t ctx, char *line)
else if (list_mode) else if (list_mode)
{ {
char *dirname; char *dirname;
struct dirent *dir_entry; gnupg_dirent_t dir_entry;
if (ctrl->restricted) if (ctrl->restricted)
return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN)); return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
@ -1466,7 +1466,7 @@ cmd_keyinfo (assuan_context_t ctx, char *line)
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
goto leave; goto leave;
} }
dir = opendir (dirname); dir = gnupg_opendir (dirname);
if (!dir) if (!dir)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
@ -1475,7 +1475,7 @@ cmd_keyinfo (assuan_context_t ctx, char *line)
} }
xfree (dirname); xfree (dirname);
while ( (dir_entry = readdir (dir)) ) while ( (dir_entry = gnupg_readdir (dir)) )
{ {
if (strlen (dir_entry->d_name) != 44 if (strlen (dir_entry->d_name) != 44
|| strcmp (dir_entry->d_name + 40, ".key")) || strcmp (dir_entry->d_name + 40, ".key"))
@ -1536,8 +1536,7 @@ cmd_keyinfo (assuan_context_t ctx, char *line)
leave: leave:
ssh_close_control_file (cf); ssh_close_control_file (cf);
if (dir) gnupg_closedir (dir);
closedir (dir);
if (err && gpg_err_code (err) != GPG_ERR_NOT_FOUND) if (err && gpg_err_code (err) != GPG_ERR_NOT_FOUND)
leave_cmd (ctx, err); leave_cmd (ctx, err);
return err; return err;

View File

@ -79,6 +79,7 @@
# include <npth.h> # include <npth.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h>
#include <assuan.h> #include <assuan.h>
@ -89,6 +90,22 @@
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A')) #define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
/* The object used with our opendir functions. We need to define our
* own so that we can properly handle Unicode on Windows. */
struct gnupg_dir_s
{
#ifdef HAVE_W32_SYSTEM
_WDIR *dir; /* The system's DIR pointer. */
#else
DIR *dir; /* The system's DIR pointer. */
#endif
struct gnupg_dirent_s dirent; /* The current dirent. */
size_t namesize; /* If not 0 the allocated size of dirent.d_name. */
char name[256]; /* Only used if NAMESIZE is 0. */
};
/* Flag to tell whether special file names are enabled. See gpg.c for /* Flag to tell whether special file names are enabled. See gpg.c for
* an explanation of these file names. */ * an explanation of these file names. */
static int allow_special_filenames; static int allow_special_filenames;
@ -1176,6 +1193,151 @@ gnupg_open (const char *name, int flags, unsigned int mode)
} }
/* A wrapper around opendir to handle Unicode file names under
* Windows. This assumes the mingw toolchain. */
gnupg_dir_t
gnupg_opendir (const char *name)
{
#ifdef HAVE_W32_SYSTEM
_WDIR *dir;
wchar_t *wname;
#else
DIR *dir;
#endif
gnupg_dir_t gdir;
#ifdef HAVE_W32_SYSTEM
/* Note: See gpgtar-create for an alternative implementation which
* could be used here to avoid a mingw dependency. */
wname = utf8_to_wchar (name);
if (!wname)
return NULL;
dir = _wopendir (wname);
xfree (wname);
#else
dir = opendir (name);
#endif
if (!dir)
return NULL;
gdir = xtrymalloc (sizeof *gdir);
if (!gdir)
{
int save_errno = errno;
#ifdef HAVE_W32_SYSTEM
_wclosedir (dir);
#else
closedir (dir);
#endif
gpg_err_set_errno (save_errno);
return NULL;
}
gdir->dir = dir;
gdir->namesize = 0;
gdir->dirent.d_name = gdir->name;
return gdir;
}
gnupg_dirent_t
gnupg_readdir (gnupg_dir_t gdir)
{
#ifdef HAVE_W32_SYSTEM
char *namebuffer = NULL;
struct _wdirent *de;
#else
struct dirent *de;
#endif
size_t n;
gnupg_dirent_t gde;
const char *name;
if (!gdir)
{
gpg_err_set_errno (EINVAL);
return 0;
}
#ifdef HAVE_W32_SYSTEM
de = _wreaddir (gdir->dir);
if (!de)
return NULL;
namebuffer = wchar_to_utf8 (de->d_name);
if (!namebuffer)
return NULL;
name = namebuffer;
#else
de = readdir (gdir->dir);
if (!de)
return NULL;
name = de->d_name;
#endif
gde = &gdir->dirent;
n = strlen (name);
if (gdir->namesize)
{
/* Use allocated buffer. */
if (n+1 >= gdir->namesize || !gde->d_name)
{
gdir->namesize = n + 256;
xfree (gde->d_name);
gde->d_name = xtrymalloc (gdir->namesize);
if (!gde->d_name)
return NULL; /* ERRNO is already set. */
}
strcpy (gde->d_name, name);
}
else if (n+1 >= sizeof (gdir->name))
{
/* Switch to allocated buffer. */
gdir->namesize = n + 256;
gde->d_name = xtrymalloc (gdir->namesize);
if (!gde->d_name)
return NULL; /* ERRNO is already set. */
strcpy (gde->d_name, name);
}
else
{
/* Use static buffer. */
gde->d_name = gdir->name;
strcpy (gde->d_name, name);
}
#ifdef HAVE_W32_SYSTEM
xfree (namebuffer);
#endif
return gde;
}
int
gnupg_closedir (gnupg_dir_t gdir)
{
#ifdef HAVE_W32_SYSTEM
_WDIR *dir;
#else
DIR *dir;
#endif
if (!gdir)
return 0;
dir = gdir->dir;
if (gdir->namesize)
xfree (gdir->dirent.d_name);
xfree (gdir);
#ifdef HAVE_W32_SYSTEM
return _wclosedir (dir);
#else
return closedir (dir);
#endif
}
/* Try to set an envvar. Print only a notice on error. */ /* Try to set an envvar. Print only a notice on error. */
#ifndef HAVE_W32_SYSTEM #ifndef HAVE_W32_SYSTEM
static void static void

View File

@ -50,6 +50,19 @@ typedef int gnupg_fd_t;
# include <sys/stat.h> # include <sys/stat.h>
#endif #endif
struct gnupg_dir_s;
typedef struct gnupg_dir_s *gnupg_dir_t;
struct gnupg_dirent_s
{
/* We don't have a d_ino because that can't be used on Windows
* anyway. D_NAME is a pointer into the gnupg_dir_s which has a
* static buffer or allocates sufficient space as needed. This is
* only valid after gnupg_readdir. */
char *d_name;
};
typedef struct gnupg_dirent_s *gnupg_dirent_t;
void trap_unaligned (void); void trap_unaligned (void);
int disable_core_dumps (void); int disable_core_dumps (void);
int enable_core_dumps (void); int enable_core_dumps (void);
@ -81,6 +94,11 @@ gpg_err_code_t gnupg_access (const char *name, int mode);
int gnupg_stat (const char *name, struct stat *statbuf); int gnupg_stat (const char *name, struct stat *statbuf);
#endif /*HAVE_STAT*/ #endif /*HAVE_STAT*/
int gnupg_open (const char *name, int flags, unsigned int mode); int gnupg_open (const char *name, int flags, unsigned int mode);
gnupg_dir_t gnupg_opendir (const char *name);
gnupg_dirent_t gnupg_readdir (gnupg_dir_t gdir);
int gnupg_closedir (gnupg_dir_t gdir);
gpg_error_t gnupg_chuid (const char *user, int silent); gpg_error_t gnupg_chuid (const char *user, int silent);
char *gnupg_get_socket_name (int fd); char *gnupg_get_socket_name (int fd);
int gnupg_fd_valid (int fd); int gnupg_fd_valid (int fd);

View File

@ -364,8 +364,8 @@ static gpg_error_t
load_certs_from_dir (const char *dirname, unsigned int trustclass) load_certs_from_dir (const char *dirname, unsigned int trustclass)
{ {
gpg_error_t err; gpg_error_t err;
DIR *dir; gnupg_dir_t dir;
struct dirent *ep; gnupg_dirent_t ep;
char *p; char *p;
size_t n; size_t n;
estream_t fp; estream_t fp;
@ -373,13 +373,13 @@ load_certs_from_dir (const char *dirname, unsigned int trustclass)
ksba_cert_t cert; ksba_cert_t cert;
char *fname = NULL; char *fname = NULL;
dir = opendir (dirname); dir = gnupg_opendir (dirname);
if (!dir) if (!dir)
{ {
return 0; /* We do not consider this a severe error. */ return 0; /* We do not consider this a severe error. */
} }
while ( (ep=readdir (dir)) ) while ( (ep = gnupg_readdir (dir)) )
{ {
p = ep->d_name; p = ep->d_name;
if (*p == '.' || !*p) if (*p == '.' || !*p)
@ -447,7 +447,7 @@ load_certs_from_dir (const char *dirname, unsigned int trustclass)
} }
xfree (fname); xfree (fname);
closedir (dir); gnupg_closedir (dir);
return 0; return 0;
} }

View File

@ -101,10 +101,6 @@
#ifndef HAVE_W32_SYSTEM #ifndef HAVE_W32_SYSTEM
#include <sys/utsname.h> #include <sys/utsname.h>
#endif #endif
#ifdef MKDIR_TAKES_ONE_ARG
#undef mkdir
#define mkdir(a,b) mkdir(a)
#endif
#include "dirmngr.h" #include "dirmngr.h"
#include "validate.h" #include "validate.h"
@ -206,15 +202,15 @@ get_current_cache (void)
static int static int
create_directory_if_needed (const char *name) create_directory_if_needed (const char *name)
{ {
DIR *dir; gnupg_dir_t dir;
char *fname; char *fname;
fname = make_filename (opt.homedir_cache, name, NULL); fname = make_filename (opt.homedir_cache, name, NULL);
dir = opendir (fname); dir = gnupg_opendir (fname);
if (!dir) if (!dir)
{ {
log_info (_("creating directory '%s'\n"), fname); log_info (_("creating directory '%s'\n"), fname);
if (mkdir (fname, S_IRUSR|S_IWUSR|S_IXUSR) ) if (gnupg_mkdir (fname, "-rwx"))
{ {
int save_errno = errno; int save_errno = errno;
log_error (_("error creating directory '%s': %s\n"), log_error (_("error creating directory '%s': %s\n"),
@ -225,7 +221,7 @@ create_directory_if_needed (const char *name)
} }
} }
else else
closedir (dir); gnupg_closedir (dir);
xfree (fname); xfree (fname);
return 0; return 0;
} }
@ -237,8 +233,8 @@ static int
cleanup_cache_dir (int force) cleanup_cache_dir (int force)
{ {
char *dname = make_filename (opt.homedir_cache, DBDIR_D, NULL); char *dname = make_filename (opt.homedir_cache, DBDIR_D, NULL);
DIR *dir; gnupg_dir_t dir;
struct dirent *de; gnupg_dirent_t de;
int problem = 0; int problem = 0;
if (!force) if (!force)
@ -251,7 +247,7 @@ cleanup_cache_dir (int force)
} }
} }
dir = opendir (dname); dir = gnupg_opendir (dname);
if (!dir) if (!dir)
{ {
log_error (_("error reading directory '%s': %s\n"), log_error (_("error reading directory '%s': %s\n"),
@ -260,7 +256,7 @@ cleanup_cache_dir (int force)
return -1; return -1;
} }
while ((de = readdir (dir))) while ((de = gnupg_readdir (dir)))
{ {
if (strcmp (de->d_name, "." ) && strcmp (de->d_name, "..")) if (strcmp (de->d_name, "." ) && strcmp (de->d_name, ".."))
{ {
@ -289,7 +285,7 @@ cleanup_cache_dir (int force)
} }
} }
xfree (dname); xfree (dname);
closedir (dir); gnupg_closedir (dir);
return problem; return problem;
} }

View File

@ -1305,7 +1305,7 @@ main (int argc, char **argv)
{ {
log_error (_("error binding socket to '%s': %s\n"), log_error (_("error binding socket to '%s': %s\n"),
serv_addr.sun_path, serv_addr.sun_path,
gpg_strerror (gpg_error_from_errno (errno))); gpg_strerror (gpg_error_from_syserror ()));
assuan_sock_close (fd); assuan_sock_close (fd);
dirmngr_exit (1); dirmngr_exit (1);
} }

View File

@ -1701,8 +1701,8 @@ expire_old_states (void)
{ {
gpg_error_t err = 0; gpg_error_t err = 0;
const char *dirname; const char *dirname;
DIR *dir = NULL; gnupg_dir_t dir = NULL;
struct dirent *dir_entry; gnupg_dirent_t dir_entry;
char *fname = NULL; char *fname = NULL;
estream_t fp = NULL; estream_t fp = NULL;
nvc_t nvc = NULL; nvc_t nvc = NULL;
@ -1712,14 +1712,14 @@ expire_old_states (void)
unsigned long now = gnupg_get_time (); unsigned long now = gnupg_get_time ();
dirname = get_pairing_statedir (); dirname = get_pairing_statedir ();
dir = opendir (dirname); dir = gnupg_opendir (dirname);
if (!dir) if (!dir)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
goto leave; goto leave;
} }
while ((dir_entry = readdir (dir))) while ((dir_entry = gnupg_readdir (dir)))
{ {
if (strlen (dir_entry->d_name) != 16+4 if (strlen (dir_entry->d_name) != 16+4
|| strcmp (dir_entry->d_name + 16, ".pa1")) || strcmp (dir_entry->d_name + 16, ".pa1"))
@ -1781,8 +1781,7 @@ expire_old_states (void)
if (err) if (err)
log_error ("expiring old states in '%s' failed: %s\n", log_error ("expiring old states in '%s' failed: %s\n",
dirname, gpg_strerror (err)); dirname, gpg_strerror (err));
if (dir) gnupg_closedir (dir);
closedir (dir);
es_fclose (fp); es_fclose (fp);
xfree (fname); xfree (fname);
} }

View File

@ -1613,22 +1613,22 @@ static gpg_error_t
get_domain_list (strlist_t *r_list) get_domain_list (strlist_t *r_list)
{ {
gpg_error_t err; gpg_error_t err;
DIR *dir = NULL; gnupg_dir_t dir = NULL;
char *fname = NULL; char *fname = NULL;
struct dirent *dentry; gnupg_dirent_t dentry;
struct stat sb; struct stat sb;
strlist_t list = NULL; strlist_t list = NULL;
*r_list = NULL; *r_list = NULL;
dir = opendir (opt.directory); dir = gnupg_opendir (opt.directory);
if (!dir) if (!dir)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
goto leave; goto leave;
} }
while ((dentry = readdir (dir))) while ((dentry = gnupg_readdir (dir)))
{ {
if (*dentry->d_name == '.') if (*dentry->d_name == '.')
continue; continue;
@ -1668,8 +1668,7 @@ get_domain_list (strlist_t *r_list)
leave: leave:
free_strlist (list); free_strlist (list);
if (dir) gnupg_closedir (dir);
closedir (dir);
xfree (fname); xfree (fname);
return err; return err;
} }
@ -1682,8 +1681,8 @@ expire_one_domain (const char *top_dirname, const char *domain)
gpg_error_t err; gpg_error_t err;
char *dirname; char *dirname;
char *fname = NULL; char *fname = NULL;
DIR *dir = NULL; gnupg_dir_t dir = NULL;
struct dirent *dentry; gnupg_dirent_t dentry;
struct stat sb; struct stat sb;
time_t now = gnupg_get_time (); time_t now = gnupg_get_time ();
@ -1696,7 +1695,7 @@ expire_one_domain (const char *top_dirname, const char *domain)
goto leave; goto leave;
} }
dir = opendir (dirname); dir = gnupg_opendir (dirname);
if (!dir) if (!dir)
{ {
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
@ -1705,7 +1704,7 @@ expire_one_domain (const char *top_dirname, const char *domain)
goto leave; goto leave;
} }
while ((dentry = readdir (dir))) while ((dentry = gnupg_readdir (dir)))
{ {
if (*dentry->d_name == '.') if (*dentry->d_name == '.')
continue; continue;
@ -1754,8 +1753,7 @@ expire_one_domain (const char *top_dirname, const char *domain)
err = 0; err = 0;
leave: leave:
if (dir) gnupg_closedir (dir);
closedir (dir);
xfree (dirname); xfree (dirname);
xfree (fname); xfree (fname);
return err; return err;

View File

@ -301,6 +301,8 @@ scan_directory (const char *dname, scanctrl_t scanctrl)
gpg_error_t err = 0; gpg_error_t err = 0;
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM
/* Note that we introduced gnupg_opendir only after we had deployed
* this code and thus we don't change it for now. */
WIN32_FIND_DATAW fi; WIN32_FIND_DATAW fi;
HANDLE hd = INVALID_HANDLE_VALUE; HANDLE hd = INVALID_HANDLE_VALUE;
char *p; char *p;