diff options
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | m4/Makefile.am | 2 | ||||
-rw-r--r-- | m4/gnupg-misc.m4 | 35 | ||||
-rw-r--r-- | src/gpg-error.def.in | 12 | ||||
-rw-r--r-- | src/gpg-error.h.in | 9 | ||||
-rw-r--r-- | src/gpg-error.vers | 3 | ||||
-rw-r--r-- | src/gpgrt-int.h | 7 | ||||
-rw-r--r-- | src/sysutils.c | 116 | ||||
-rw-r--r-- | src/visibility.c | 141 | ||||
-rw-r--r-- | src/visibility.h | 41 |
10 files changed, 288 insertions, 84 deletions
diff --git a/configure.ac b/configure.ac index 68dbc55..0d2f4b5 100644 --- a/configure.ac +++ b/configure.ac @@ -177,7 +177,7 @@ AC_MSG_WARN([[Without strerror_r, gpg_strerror_r might not be thread-safe]])) ;; esac -AC_CHECK_FUNCS([flockfile vasprintf mmap rand strlwr stpcpy setenv]) +AC_CHECK_FUNCS([flockfile vasprintf mmap rand strlwr stpcpy setenv stat]) # @@ -203,6 +203,9 @@ AC_CHECK_SIZEOF(time_t,,[[ #endif ]]) +GNUPG_FUNC_MKDIR_TAKES_ONE_ARG + + # Find a 64 bit integer type to be used instead of off_t. We prefer # the standard integer types over int64_t and finally try long long. if test "$ac_cv_sizeof_int" = "8"; then @@ -223,7 +226,6 @@ AC_DEFINE_UNQUOTED(REPLACEMENT_FOR_OFF_T, "$replacement_for_off_t", [Used by mkheader to insert the replacement type.]) - # # Setup gcc specific options # diff --git a/m4/Makefile.am b/m4/Makefile.am index f92d115..b7b1963 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -1,3 +1,3 @@ EXTRA_DIST = inttypes-h.m4 lock.m4 visibility.m4 glibc2.m4 intmax.m4 longdouble.m4 longlong.m4 printf-posix.m4 signed.m4 size_max.m4 wchar_t.m4 wint_t.m4 xsize.m4 ac_prog_cc_for_build.m4 nls.m4 po.m4 codeset.m4 gettext.m4 glibc21.m4 iconv.m4 intdiv0.m4 inttypes.m4 inttypes_h.m4 inttypes-pri.m4 isc-posix.m4 lcmessage.m4 lib-ld.m4 lib-link.m4 lib-prefix.m4 progtest.m4 stdint_h.m4 uintmax_t.m4 ulonglong.m4 -EXTRA_DIST += autobuild.m4 estream.m4 readline.m4 +EXTRA_DIST += autobuild.m4 estream.m4 readline.m4 gnupg-misc.m4 diff --git a/m4/gnupg-misc.m4 b/m4/gnupg-misc.m4 new file mode 100644 index 0000000..12f86d8 --- /dev/null +++ b/m4/gnupg-misc.m4 @@ -0,0 +1,35 @@ +dnl gnupg-misc.m4 - Autoconf macros originally from GnuPG +dnl Copyright (C) 2017 g10 Code GmbH +dnl +dnl This file is free software; as a special exception the author gives +dnl unlimited permission to copy and/or distribute it, with or without +dnl modifications, as long as this notice is preserved. +dnl +dnl This file is distributed in the hope that it will be useful, but +dnl WITHOUT ANY WARRANTY, to the extent permitted by law; without even the +dnl implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +dnl SPDX-License-Identifier: FSFULLR + + +dnl Define MKDIR_TAKES_ONE_ARG if mkdir accepts only one argument instead +dnl of the usual 2. +AC_DEFUN([GNUPG_FUNC_MKDIR_TAKES_ONE_ARG], +[AC_CHECK_HEADERS(sys/stat.h unistd.h direct.h) +AC_CACHE_CHECK([if mkdir takes one argument], gnupg_cv_mkdir_takes_one_arg, +[AC_TRY_COMPILE([ +#include <sys/types.h> +#ifdef HAVE_SYS_STAT_H +# include <sys/stat.h> +#endif +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif +#ifdef HAVE_DIRECT_H +# include <direct.h> +#endif], [mkdir ("foo", 0);], + gnupg_cv_mkdir_takes_one_arg=no, gnupg_cv_mkdir_takes_one_arg=yes)]) +if test $gnupg_cv_mkdir_takes_one_arg = yes ; then + AC_DEFINE(MKDIR_TAKES_ONE_ARG,1, + [Defined if mkdir() does not take permission flags]) +fi +]) diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in index 6c40571..64c75dc 100644 --- a/src/gpg-error.def.in +++ b/src/gpg-error.def.in @@ -189,11 +189,13 @@ EXPORTS gpgrt_strdup @144 gpgrt_strconcat @145 - gpgrt_getenv @146 - gpgrt_setenv @147 + gpgrt_w32_reg_query_string @146 + + gpgrt_getenv @147 + gpgrt_setenv @148 + gpgrt_mkdir @149 + gpgrt_chdir @150 + gpgrt_getcwd @151 -#ifdef HAVE_W32_SYSTEM - gpgrt_w32_reg_query_string @148 -#endif ;; end of file with public symbols for Windows. diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 3d9c914..dffde2e 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -456,6 +456,15 @@ char *gpgrt_getenv (const char *name); int gpgrt_setenv (const char *name, const char *value, int overwrite); #define gpgrt_unsetenv(n) gpgrt_setenv ((n), NULL, 1) +/* A wrapper around mkdir using a string for the mode. */ +int gpgrt_mkdir (const char *name, const char *modestr); + +/* A simple wrapper around chdir. */ +int gpgrt_chdir (const char *name); + +/* Return the current WD as a malloced string. */ +char *gpgrt_getcwd (void); + diff --git a/src/gpg-error.vers b/src/gpg-error.vers index 661222c..92184f3 100644 --- a/src/gpg-error.vers +++ b/src/gpg-error.vers @@ -165,6 +165,9 @@ GPG_ERROR_1.0 { gpgrt_getenv; gpgrt_setenv; + gpgrt_mkdir; + gpgrt_chdir; + gpgrt_getcwd; local: *; diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index c65f630..b2f87b9 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -566,7 +566,14 @@ char *_gpgrt_getenv (const char *name); * NULL and OVERRIDE to true. */ int _gpgrt_setenv (const char *name, const char *value, int overwrite); +/* A wrapper around mkdir using a string for the mode (permissions). */ +int _gpgrt_mkdir (const char *name, const char *modestr); +/* A simple wrapper around chdir. */ +int _gpgrt_chdir (const char *name); + +/* Return the current WD as a malloced string. */ +char *_gpgrt_getcwd (void); /* diff --git a/src/sysutils.c b/src/sysutils.c index 2dda4cd..70c2dc3 100644 --- a/src/sysutils.c +++ b/src/sysutils.c @@ -27,6 +27,11 @@ #ifdef HAVE_W32_SYSTEM # include <windows.h> #endif +#ifdef HAVE_STAT +# include <sys/stat.h> +#endif +#include <sys/types.h> +#include <fcntl.h> #include "gpgrt-int.h" @@ -212,3 +217,114 @@ _gpgrt_setenv (const char *name, const char *value, int overwrite) # endif /*!HAVE_SETENV*/ #endif /*!HAVE_W32_SYSTEM*/ } + + +#ifndef HAVE_W32_SYSTEM +static mode_t +modestr_to_mode (const char *modestr) +{ + mode_t mode = 0; + + if (modestr && *modestr) + { + modestr++; + if (*modestr && *modestr++ == 'r') + mode |= S_IRUSR; + if (*modestr && *modestr++ == 'w') + mode |= S_IWUSR; + if (*modestr && *modestr++ == 'x') + mode |= S_IXUSR; + if (*modestr && *modestr++ == 'r') + mode |= S_IRGRP; + if (*modestr && *modestr++ == 'w') + mode |= S_IWGRP; + if (*modestr && *modestr++ == 'x') + mode |= S_IXGRP; + if (*modestr && *modestr++ == 'r') + mode |= S_IROTH; + if (*modestr && *modestr++ == 'w') + mode |= S_IWOTH; + if (*modestr && *modestr++ == 'x') + mode |= S_IXOTH; + } + + return mode; +} +#endif + + +/* A wrapper around mkdir which takes a string for the mode argument. + * This makes it easier to handle the mode argument which is not + * defined on all systems. The format of the modestring is + * + * "-rwxrwxrwx" + * + * '-' is a don't care or not set. 'r', 'w', 'x' are read allowed, + * write allowed, execution allowed with the first group for the user, + * the second for the group and the third for all others. If the + * string is shorter than above the missing mode characters are meant + * to be not set. */ +int +_gpgrt_mkdir (const char *name, const char *modestr) +{ +#ifdef HAVE_W32CE_SYSTEM + wchar_t *wname; + (void)modestr; + + wname = utf8_to_wchar (name); + if (!wname) + return -1; + if (!CreateDirectoryW (wname, NULL)) + { + xfree (wname); + return -1; /* ERRNO is automagically provided by gpg-error.h. */ + } + xfree (wname); + return 0; +#elif MKDIR_TAKES_ONE_ARG + (void)modestr; + /* Note: In the case of W32 we better use CreateDirectory and try to + set appropriate permissions. However using mkdir is easier + because this sets ERRNO. */ + return mkdir (name); +#else + return mkdir (name, modestr_to_mode (modestr)); +#endif +} + + +/* A simple wrapper around chdir. NAME is expected to be utf8 + * encoded. */ +int +_gpgrt_chdir (const char *name) +{ + return chdir (name); +} + + +/* Return the current working directory as a malloced string. Return + * NULL and sets ERRNO on error. */ +char * +_gpgrt_getcwd (void) +{ + char *buffer; + size_t size = 100; + + for (;;) + { + buffer = xtrymalloc (size+1); + if (!buffer) + return NULL; +#ifdef HAVE_W32CE_SYSTEM + strcpy (buffer, "/"); /* Always "/". */ + return buffer; +#else + if (getcwd (buffer, size) == buffer) + return buffer; + xfree (buffer); + if (errno != ERANGE) + return NULL; + size *= 2; +#endif + } +} diff --git a/src/visibility.c b/src/visibility.c index 45ac644..c0cab3d 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -504,66 +504,6 @@ gpgrt_read_line (estream_t stream, max_length); } -void * -gpgrt_realloc (void *a, size_t n) -{ - return _gpgrt_realloc (a, n); -} - -void * -gpgrt_malloc (size_t n) -{ - return _gpgrt_malloc (n); -} - -void * -gpgrt_calloc (size_t n, size_t m) -{ - return _gpgrt_calloc (n, m); -} - -char * -gpgrt_strdup (const char *string) -{ - return _gpgrt_strdup (string); -} - -char * -gpgrt_strconcat (const char *s1, ...) -{ - va_list arg_ptr; - char *result; - - if (!s1) - result = _gpgrt_strdup (""); - else - { - va_start (arg_ptr, s1); - result = _gpgrt_strconcat_core (s1, arg_ptr); - va_end (arg_ptr); - } - return result; -} - -void -gpgrt_free (void *a) -{ - if (a) - _gpgrt_free (a); -} - -char * -gpgrt_getenv (const char *name) -{ - return _gpgrt_getenv (name); -} - -int -gpgrt_setenv (const char *name, const char *value, int overwrite) -{ - return _gpgrt_setenv (name, value, overwrite); -} - int gpgrt_vfprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, @@ -769,6 +709,85 @@ gpgrt_vsnprintf (char *buf, size_t bufsize, +void * +gpgrt_realloc (void *a, size_t n) +{ + return _gpgrt_realloc (a, n); +} + +void * +gpgrt_malloc (size_t n) +{ + return _gpgrt_malloc (n); +} + +void * +gpgrt_calloc (size_t n, size_t m) +{ + return _gpgrt_calloc (n, m); +} + +char * +gpgrt_strdup (const char *string) +{ + return _gpgrt_strdup (string); +} + +char * +gpgrt_strconcat (const char *s1, ...) +{ + va_list arg_ptr; + char *result; + + if (!s1) + result = _gpgrt_strdup (""); + else + { + va_start (arg_ptr, s1); + result = _gpgrt_strconcat_core (s1, arg_ptr); + va_end (arg_ptr); + } + return result; +} + +void +gpgrt_free (void *a) +{ + if (a) + _gpgrt_free (a); +} + +char * +gpgrt_getenv (const char *name) +{ + return _gpgrt_getenv (name); +} + +int +gpgrt_setenv (const char *name, const char *value, int overwrite) +{ + return _gpgrt_setenv (name, value, overwrite); +} + +int +gpgrt_mkdir (const char *name, const char *modestr) +{ + return _gpgrt_mkdir (name, modestr); +} + +int +gpgrt_chdir (const char *name) +{ + return _gpgrt_chdir (name); +} + +char *gpgrt_getcwd (void) +{ + return _gpgrt_getcwd (); +} + + + gpgrt_b64state_t gpgrt_b64dec_start (const char *title) { @@ -990,6 +1009,8 @@ _gpgrt_log_assert (const char *expr, const char *file, + + /* For consistency reasons we use function wrappers also for Windows * specific function despite that they are technically not needed. */ diff --git a/src/visibility.h b/src/visibility.h index 3c2d6be..3209c23 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -118,14 +118,6 @@ MARK_VISIBLE (gpgrt_fputs) MARK_VISIBLE (gpgrt_fputs_unlocked) MARK_VISIBLE (gpgrt_getline) MARK_VISIBLE (gpgrt_read_line) -MARK_VISIBLE (gpgrt_realloc) -MARK_VISIBLE (gpgrt_malloc) -MARK_VISIBLE (gpgrt_calloc) -MARK_VISIBLE (gpgrt_strdup) -MARK_VISIBLE (gpgrt_strconcat) -MARK_VISIBLE (gpgrt_free) -MARK_VISIBLE (gpgrt_getenv) -MARK_VISIBLE (gpgrt_setenv) MARK_VISIBLE (gpgrt_fprintf) MARK_VISIBLE (gpgrt_fprintf_unlocked) MARK_VISIBLE (gpgrt_printf) @@ -149,10 +141,23 @@ MARK_VISIBLE (gpgrt_bsprintf) MARK_VISIBLE (gpgrt_vbsprintf) MARK_VISIBLE (gpgrt_snprintf) MARK_VISIBLE (gpgrt_vsnprintf) + MARK_VISIBLE (gpgrt_set_syscall_clamp) MARK_VISIBLE (gpgrt_get_syscall_clamp) MARK_VISIBLE (gpgrt_set_alloc_func) +MARK_VISIBLE (gpgrt_realloc) +MARK_VISIBLE (gpgrt_malloc) +MARK_VISIBLE (gpgrt_calloc) +MARK_VISIBLE (gpgrt_strdup) +MARK_VISIBLE (gpgrt_strconcat) +MARK_VISIBLE (gpgrt_free) +MARK_VISIBLE (gpgrt_getenv) +MARK_VISIBLE (gpgrt_setenv) +MARK_VISIBLE (gpgrt_mkdir) +MARK_VISIBLE (gpgrt_chdir) +MARK_VISIBLE (gpgrt_getcwd) + MARK_VISIBLE (gpgrt_b64dec_start) MARK_VISIBLE (gpgrt_b64dec_proc) MARK_VISIBLE (gpgrt_b64dec_finish) @@ -264,14 +269,6 @@ MARK_VISIBLE (_gpgrt_log_assert) #define gpgrt_fputs_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_getline _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_read_line _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_realloc _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_malloc _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_strdup _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_strconcat _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_free _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_getenv _gpgrt_USE_UNDERSCORED_FUNCTION -#define gpgrt_setenv _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_printf _gpgrt_USE_UNDERSCORED_FUNCTION @@ -296,6 +293,18 @@ MARK_VISIBLE (_gpgrt_log_assert) #define gpgrt_snprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vsnprintf _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_realloc _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_malloc _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_strdup _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_strconcat _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_free _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_getenv _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_setenv _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_mkdir _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_chdir _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_getcwd _gpgrt_USE_UNDERSCORED_FUNCTION + #define gpgrt_set_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_alloc_func _gpgrt_USE_UNDERSCORED_FUNCTION |