2007-06-14 19:05:07 +02:00
|
|
|
/* init.c - Various initializations
|
|
|
|
* Copyright (C) 2007 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2007-06-14 19:05:07 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2007-06-14 19:05:07 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2007-06-14 19:05:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2023-01-11 10:04:08 +01:00
|
|
|
# if _WIN32_WINNT < 0x0600
|
|
|
|
# define _WIN32_WINNT 0x0600 /* Required for SetProcessDEPPolicy. */
|
|
|
|
# endif
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
2007-06-14 19:05:07 +02:00
|
|
|
#endif
|
|
|
|
|
2016-08-09 00:40:03 +02:00
|
|
|
#include <gcrypt.h>
|
2007-06-14 19:05:07 +02:00
|
|
|
#include "util.h"
|
2016-08-09 00:40:03 +02:00
|
|
|
#include "i18n.h"
|
2021-03-04 10:16:48 +01:00
|
|
|
#include "w32help.h"
|
2007-06-14 19:05:07 +02:00
|
|
|
|
2012-12-28 19:26:59 +01:00
|
|
|
/* This object is used to register memory cleanup functions.
|
|
|
|
Technically they are not needed but they can avoid frequent
|
|
|
|
questions about un-released memory. Note that we use the system
|
|
|
|
malloc and not any wrappers. */
|
|
|
|
struct mem_cleanup_item_s;
|
|
|
|
typedef struct mem_cleanup_item_s *mem_cleanup_item_t;
|
|
|
|
|
|
|
|
struct mem_cleanup_item_s
|
|
|
|
{
|
|
|
|
mem_cleanup_item_t next;
|
|
|
|
void (*func) (void);
|
|
|
|
};
|
|
|
|
|
|
|
|
static mem_cleanup_item_t mem_cleanup_list;
|
|
|
|
|
2010-04-14 16:39:16 +02:00
|
|
|
|
2012-02-06 20:50:47 +01:00
|
|
|
/* The default error source of the application. This is different
|
|
|
|
from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the
|
2014-11-19 10:34:32 +01:00
|
|
|
source file and thus is usable in code shared by applications.
|
|
|
|
Note that we need to initialize it because otherwise some linkers
|
|
|
|
(OS X at least) won't find the symbol when linking the t-*.c
|
|
|
|
files. */
|
|
|
|
gpg_err_source_t default_errsource = 0;
|
2012-02-06 20:50:47 +01:00
|
|
|
|
|
|
|
|
2010-03-22 13:46:05 +01:00
|
|
|
|
2021-03-04 10:16:48 +01:00
|
|
|
#if HAVE_W32_SYSTEM
|
|
|
|
static void prepare_w32_commandline (int *argcp, char ***argvp);
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
|
2012-12-28 19:26:59 +01:00
|
|
|
static void
|
|
|
|
run_mem_cleanup (void)
|
|
|
|
{
|
|
|
|
mem_cleanup_item_t next;
|
|
|
|
|
|
|
|
while (mem_cleanup_list)
|
|
|
|
{
|
|
|
|
next = mem_cleanup_list->next;
|
|
|
|
mem_cleanup_list->func ();
|
|
|
|
free (mem_cleanup_list);
|
|
|
|
mem_cleanup_list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
register_mem_cleanup_func (void (*func)(void))
|
|
|
|
{
|
|
|
|
mem_cleanup_item_t item;
|
|
|
|
|
2016-07-01 14:42:18 +02:00
|
|
|
for (item = mem_cleanup_list; item; item = item->next)
|
|
|
|
if (item->func == func)
|
|
|
|
return; /* Function has already been registered. */
|
|
|
|
|
2012-12-28 19:26:59 +01:00
|
|
|
item = malloc (sizeof *item);
|
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
item->func = func;
|
|
|
|
item->next = mem_cleanup_list;
|
|
|
|
mem_cleanup_list = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-30 14:06:38 +02:00
|
|
|
/* If STRING is not NULL write string to es_stdout or es_stderr. MODE
|
|
|
|
must be 1 or 2. If STRING is NULL flush the respective stream. */
|
|
|
|
static int
|
|
|
|
writestring_via_estream (int mode, const char *string)
|
|
|
|
{
|
|
|
|
if (mode == 1 || mode == 2)
|
|
|
|
{
|
|
|
|
if (string)
|
|
|
|
return es_fputs (string, mode == 1? es_stdout : es_stderr);
|
|
|
|
else
|
|
|
|
return es_fflush (mode == 1? es_stdout : es_stderr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-28 19:57:22 +01:00
|
|
|
/* This function should be the first called after main. */
|
|
|
|
void
|
|
|
|
early_system_init (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
/* This function is to be used early at program startup to make sure
|
2007-07-04 21:49:40 +02:00
|
|
|
that some subsystems are initialized. This is in particular
|
2007-06-14 19:05:07 +02:00
|
|
|
important for W32 to initialize the sockets so that our socket
|
|
|
|
emulation code used directly as well as in libassuan may be used.
|
|
|
|
It should best be called before any I/O is done so that setup
|
2010-03-22 13:46:05 +01:00
|
|
|
required for logging is ready. ARGCP and ARGVP are the addresses
|
|
|
|
of the parameters given to main. This function may modify them.
|
|
|
|
|
2012-02-06 20:50:47 +01:00
|
|
|
This function should be called only via the macro
|
|
|
|
init_common_subsystems.
|
|
|
|
|
2010-03-22 13:46:05 +01:00
|
|
|
CAUTION: This might be called while running suid(root). */
|
2007-06-14 19:05:07 +02:00
|
|
|
void
|
2012-02-06 20:50:47 +01:00
|
|
|
_init_common_subsystems (gpg_err_source_t errsource, int *argcp, char ***argvp)
|
2007-06-14 19:05:07 +02:00
|
|
|
{
|
2014-11-19 10:34:32 +01:00
|
|
|
/* Store the error source in a global variable. */
|
2012-02-06 20:50:47 +01:00
|
|
|
default_errsource = errsource;
|
|
|
|
|
2012-12-28 19:26:59 +01:00
|
|
|
atexit (run_mem_cleanup);
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
/* Try to auto set the character set. */
|
2011-02-04 12:57:53 +01:00
|
|
|
set_native_charset (NULL);
|
2007-06-14 19:05:07 +02:00
|
|
|
|
2020-07-28 09:57:52 +02:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
/* For W32 we need to initialize the socket layer. This is because
|
|
|
|
we use recv and send in libassuan as well as at some other
|
|
|
|
places. */
|
|
|
|
{
|
|
|
|
WSADATA wsadat;
|
|
|
|
|
|
|
|
WSAStartup (0x202, &wsadat);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-09 00:40:03 +02:00
|
|
|
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION))
|
|
|
|
{
|
|
|
|
log_fatal (_("%s is too old (need %s, have %s)\n"), "libgcrypt",
|
|
|
|
NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL));
|
|
|
|
}
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
/* Initialize the Estream library. */
|
2014-08-26 17:47:22 +02:00
|
|
|
gpgrt_init ();
|
|
|
|
gpgrt_set_alloc_func (gcry_realloc);
|
2010-03-22 13:46:05 +01:00
|
|
|
|
2010-05-30 14:06:38 +02:00
|
|
|
|
2021-03-05 15:33:40 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
/* We want gettext to always output UTF-8 and we put the console in
|
|
|
|
* utf-8 mode. */
|
|
|
|
gettext_use_utf8 (1);
|
|
|
|
if (!SetConsoleCP (CP_UTF8) || !SetConsoleOutputCP (CP_UTF8))
|
|
|
|
{
|
2021-10-17 14:26:28 +02:00
|
|
|
/* Don't show the error if the program does not have a console.
|
|
|
|
* This is for example the case for daemons. */
|
2021-10-27 12:55:03 +02:00
|
|
|
int rc = GetLastError ();
|
2021-10-17 14:26:28 +02:00
|
|
|
if (rc != ERROR_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
log_info ("SetConsoleCP failed: %s\n", w32_strerror (rc));
|
|
|
|
log_info ("Warning: Garbled console data possible\n");
|
|
|
|
}
|
2021-03-05 15:33:40 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-23 21:26:05 +02:00
|
|
|
/* Access the standard estreams as early as possible. If we don't
|
|
|
|
do this the original stdio streams may have been closed when
|
|
|
|
_es_get_std_stream is first use and in turn it would connect to
|
|
|
|
the bit bucket. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i < 3; i++)
|
2014-08-26 17:47:22 +02:00
|
|
|
(void)_gpgrt_get_std_stream (i);
|
2010-08-23 21:26:05 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 14:06:38 +02:00
|
|
|
/* --version et al shall use estream as well. */
|
2018-06-12 16:11:19 +02:00
|
|
|
gpgrt_set_usage_outfnc (writestring_via_estream);
|
|
|
|
|
|
|
|
/* Register our string mapper with gpgrt. */
|
|
|
|
gpgrt_set_fixed_string_mapper (map_static_macro_string);
|
2016-08-29 11:45:47 +02:00
|
|
|
|
|
|
|
/* Logging shall use the standard socket directory as fallback. */
|
|
|
|
log_set_socket_dir_cb (gnupg_socketdir);
|
2021-03-04 10:16:48 +01:00
|
|
|
|
|
|
|
#if HAVE_W32_SYSTEM
|
2023-01-11 10:04:08 +01:00
|
|
|
/* Make sure that Data Execution Prevention is enabled. */
|
|
|
|
if (GetSystemDEPPolicy () >= 2)
|
|
|
|
{
|
|
|
|
DWORD flags;
|
|
|
|
BOOL perm;
|
|
|
|
|
|
|
|
if (!GetProcessDEPPolicy (GetCurrentProcess (), &flags, &perm))
|
|
|
|
log_info ("error getting DEP policy: %s\n",
|
|
|
|
w32_strerror (GetLastError()));
|
|
|
|
else if (!(flags & PROCESS_DEP_ENABLE)
|
|
|
|
&& !SetProcessDEPPolicy (PROCESS_DEP_ENABLE))
|
|
|
|
log_info ("Warning: Enabling DEP failed: %s (%d,%d)\n",
|
|
|
|
w32_strerror (GetLastError ()), (int)flags, (int)perm);
|
|
|
|
}
|
|
|
|
/* On Windows we use our own parser for the command line
|
2021-03-04 10:16:48 +01:00
|
|
|
* so that we can return an array of utf-8 encoded strings. */
|
|
|
|
prepare_w32_commandline (argcp, argvp);
|
|
|
|
#else
|
|
|
|
(void)argcp;
|
|
|
|
(void)argvp;
|
|
|
|
#endif
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2010-03-22 13:46:05 +01:00
|
|
|
|
|
|
|
|
2021-03-04 10:16:48 +01:00
|
|
|
/* For Windows we need to parse the command line so that we can
|
|
|
|
* provide an UTF-8 encoded argv. If there is any Unicode character
|
|
|
|
* we return a new array but if there is no Unicode character we do
|
|
|
|
* nothing. */
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
static void
|
|
|
|
prepare_w32_commandline (int *r_argc, char ***r_argv)
|
|
|
|
{
|
|
|
|
const wchar_t *wcmdline, *ws;
|
|
|
|
char *cmdline;
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
const char *s;
|
2021-03-04 16:52:03 +01:00
|
|
|
int i, globing, itemsalloced;
|
2021-03-04 10:16:48 +01:00
|
|
|
|
|
|
|
s = gpgrt_strusage (95);
|
|
|
|
globing = (s && *s == '1');
|
|
|
|
|
|
|
|
wcmdline = GetCommandLineW ();
|
|
|
|
if (!wcmdline)
|
|
|
|
{
|
|
|
|
log_error ("GetCommandLineW failed\n");
|
|
|
|
return; /* Ooops. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!globing)
|
|
|
|
{
|
|
|
|
/* If globbing is not enabled we use our own parser only if
|
|
|
|
* there are any non-ASCII characters. */
|
|
|
|
for (ws=wcmdline; *ws; ws++)
|
|
|
|
if (!iswascii (*ws))
|
|
|
|
break;
|
|
|
|
if (!*ws)
|
|
|
|
return; /* No Unicode - return directly. */
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdline = wchar_to_utf8 (wcmdline);
|
|
|
|
if (!cmdline)
|
|
|
|
{
|
|
|
|
log_error ("parsing command line failed: %s\n", strerror (errno));
|
|
|
|
return; /* Ooops. */
|
|
|
|
}
|
|
|
|
gpgrt_annotate_leaked_object (cmdline);
|
|
|
|
|
2021-03-04 16:52:03 +01:00
|
|
|
argv = w32_parse_commandline (cmdline, globing, &argc, &itemsalloced);
|
2021-03-04 10:16:48 +01:00
|
|
|
if (!argv)
|
|
|
|
{
|
|
|
|
log_error ("parsing command line failed: %s\n", "internal error");
|
|
|
|
return; /* Ooops. */
|
|
|
|
}
|
|
|
|
gpgrt_annotate_leaked_object (argv);
|
2021-03-04 16:52:03 +01:00
|
|
|
if (itemsalloced)
|
|
|
|
{
|
|
|
|
for (i=0; i < argc; i++)
|
|
|
|
gpgrt_annotate_leaked_object (argv[i]);
|
|
|
|
}
|
2021-03-04 10:16:48 +01:00
|
|
|
*r_argv = argv;
|
|
|
|
*r_argc = argc;
|
|
|
|
}
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|