2007-06-14 17:05:07 +00: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 17:05:07 +00: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 17:05:07 +00: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
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2007-06-14 17:05:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
Port to npth.
* configure.ac: Don't check for PTH but for NPTH.
(AH_BOTTOM): Remove PTH_SYSCALL_SOFT.
(have_pth): Rename to ...
(have_npth): ... this.
(USE_GNU_NPTH): Rename to ...
(USE_GNU_PTH): ... this.
* m4/npth.m4: New file.
* agent/Makefile.am, agent/cache.c, agent/call-pinentry.c,
agent/call-scd.c, agent/findkey.c, agent/gpg-agent.c,
agent/trustlist.c, common/Makefile.am, common/estream.c,
common/exechelp-posix.c, common/exechelp-w32.c,
common/exechelp-w32ce.c, common/http.c, common/init.c,
common/sysutils.c, dirmngr/Makefile.am, dirmngr/crlfetch.c,
dirmngr/dirmngr.c, dirmngr/dirmngr_ldap.c, dirmngr/ldap-wrapper-ce.c,
dirmngr/ldap-wrapper.c, dirmngr/ldap.c, g13/Makefile.am,
g13/call-gpg.c, g13/g13.c, g13/runner.c, scd/Makefile.am,
scd/apdu.c, scd/app.c, scd/ccid-driver.c, scd/command.c,
scd/scdaemon.c, tools/Makefile.am: Port to npth.
2012-01-03 22:12:37 +01:00
|
|
|
#ifdef WITHOUT_NPTH /* Give the Makefile a chance to build without Pth. */
|
|
|
|
#undef HAVE_NPTH
|
|
|
|
#undef USE_NPTH
|
2007-06-14 17:05:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
2007-06-14 17:05:07 +00:00
|
|
|
#endif
|
Port to npth.
* configure.ac: Don't check for PTH but for NPTH.
(AH_BOTTOM): Remove PTH_SYSCALL_SOFT.
(have_pth): Rename to ...
(have_npth): ... this.
(USE_GNU_NPTH): Rename to ...
(USE_GNU_PTH): ... this.
* m4/npth.m4: New file.
* agent/Makefile.am, agent/cache.c, agent/call-pinentry.c,
agent/call-scd.c, agent/findkey.c, agent/gpg-agent.c,
agent/trustlist.c, common/Makefile.am, common/estream.c,
common/exechelp-posix.c, common/exechelp-w32.c,
common/exechelp-w32ce.c, common/http.c, common/init.c,
common/sysutils.c, dirmngr/Makefile.am, dirmngr/crlfetch.c,
dirmngr/dirmngr.c, dirmngr/dirmngr_ldap.c, dirmngr/ldap-wrapper-ce.c,
dirmngr/ldap-wrapper.c, dirmngr/ldap.c, g13/Makefile.am,
g13/call-gpg.c, g13/g13.c, g13/runner.c, scd/Makefile.am,
scd/apdu.c, scd/app.c, scd/ccid-driver.c, scd/command.c,
scd/scdaemon.c, tools/Makefile.am: Port to npth.
2012-01-03 22:12:37 +01:00
|
|
|
#ifdef HAVE_NPTH
|
2014-03-07 09:46:44 +01:00
|
|
|
# include <npth.h>
|
2007-06-14 17:05:07 +00:00
|
|
|
#endif
|
2010-04-14 14:39:16 +00:00
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
# include <assuan.h> /* For _assuan_w32ce_finish_pipe. */
|
|
|
|
#endif
|
2007-06-14 17:05:07 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
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 14:39:16 +00: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
|
|
|
|
source file and thus is usable in code shared by applications. */
|
|
|
|
gpg_err_source_t default_errsource;
|
|
|
|
|
|
|
|
|
2010-03-22 12:46:05 +00:00
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
static void parse_std_file_handles (int *argcp, char ***argvp);
|
2010-03-29 12:57:11 +00:00
|
|
|
static void
|
|
|
|
sleep_on_exit (void)
|
|
|
|
{
|
|
|
|
/* The sshd on CE swallows some of the command output. Sleeping a
|
|
|
|
while usually helps. */
|
|
|
|
Sleep (400);
|
|
|
|
}
|
2010-03-22 12:46:05 +00:00
|
|
|
#endif /*HAVE_W32CE_SYSTEM*/
|
|
|
|
|
2007-06-14 17:05:07 +00: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;
|
|
|
|
|
|
|
|
item = malloc (sizeof *item);
|
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
item->func = func;
|
|
|
|
item->next = mem_cleanup_list;
|
|
|
|
mem_cleanup_list = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-30 12:06:38 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-14 17:05:07 +00:00
|
|
|
/* This function is to be used early at program startup to make sure
|
2007-07-04 19:49:40 +00:00
|
|
|
that some subsystems are initialized. This is in particular
|
2007-06-14 17:05:07 +00: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 12:46:05 +00: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 12:46:05 +00:00
|
|
|
CAUTION: This might be called while running suid(root). */
|
2007-06-14 17:05:07 +00: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 17:05:07 +00:00
|
|
|
{
|
2012-02-06 20:50:47 +01:00
|
|
|
/* Store the error source in a gloabl variable. */
|
|
|
|
default_errsource = errsource;
|
|
|
|
|
2012-12-28 19:26:59 +01:00
|
|
|
atexit (run_mem_cleanup);
|
|
|
|
|
2007-06-14 17:05:07 +00:00
|
|
|
/* Try to auto set the character set. */
|
2011-02-04 12:57:53 +01:00
|
|
|
set_native_charset (NULL);
|
2007-06-14 17:05:07 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2007-07-04 19:49:40 +00:00
|
|
|
/* For W32 we need to initialize the socket layer. This is because
|
2007-06-14 17:05:07 +00:00
|
|
|
we use recv and send in libassuan as well as at some other
|
2014-05-02 08:06:10 +02:00
|
|
|
places. */
|
2010-04-26 11:53:14 +00:00
|
|
|
{
|
|
|
|
WSADATA wsadat;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-26 11:53:14 +00:00
|
|
|
WSAStartup (0x202, &wsadat);
|
|
|
|
}
|
2007-06-14 17:05:07 +00:00
|
|
|
#endif
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-26 11:53:14 +00:00
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
/* Register the sleep exit function before the estream init so that
|
|
|
|
the sleep will be called after the estream registered atexit
|
|
|
|
function which flushes the left open estream streams and in
|
|
|
|
particular es_stdout. */
|
|
|
|
atexit (sleep_on_exit);
|
|
|
|
#endif
|
2007-06-14 17:05:07 +00:00
|
|
|
|
|
|
|
/* Initialize the Estream library. */
|
2014-08-26 17:47:22 +02:00
|
|
|
gpgrt_init ();
|
|
|
|
gpgrt_set_alloc_func (gcry_realloc);
|
|
|
|
#ifdef USE_NPTH
|
|
|
|
gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
|
|
|
|
#endif
|
2010-03-22 12:46:05 +00:00
|
|
|
|
|
|
|
/* Special hack for Windows CE: We extract some options from arg
|
|
|
|
to setup the standard handles. */
|
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
parse_std_file_handles (argcp, argvp);
|
|
|
|
#else
|
|
|
|
(void)argcp;
|
|
|
|
(void)argvp;
|
|
|
|
#endif
|
2010-05-30 12:06:38 +00:00
|
|
|
|
2010-08-23 19:26:05 +00: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 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-30 12:06:38 +00:00
|
|
|
/* --version et al shall use estream as well. */
|
|
|
|
argparse_register_outfnc (writestring_via_estream);
|
2007-06-14 17:05:07 +00:00
|
|
|
}
|
|
|
|
|
2010-03-22 12:46:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* WindowsCE uses a very strange way of handling the standard streams.
|
|
|
|
There is a function SetStdioPath to associate a standard stream
|
|
|
|
with a file or a device but what we really want is to use pipes as
|
|
|
|
standard streams. Despite that we implement pipes using a device,
|
|
|
|
we would have some limitations on the number of open pipes due to
|
|
|
|
the 3 character limit of device file name. Thus we don't take this
|
|
|
|
path. Another option would be to install a file system driver with
|
|
|
|
support for pipes; this would allow us to get rid of the device
|
|
|
|
name length limitation. However, with GnuPG we can get away be
|
|
|
|
redefining the standard streams and passing the handles to be used
|
|
|
|
on the command line. This has also the advantage that it makes
|
|
|
|
creating a process much easier and does not require the
|
|
|
|
SetStdioPath set and restore game. The caller needs to pass the
|
|
|
|
rendezvous ids using up to three options:
|
|
|
|
|
2010-03-24 12:15:30 +00:00
|
|
|
-&S0=<rvid> -&S1=<rvid> -&S2=<rvid>
|
2010-03-22 12:46:05 +00:00
|
|
|
|
|
|
|
They are all optional but they must be the first arguments on the
|
|
|
|
command line. Parsing stops as soon as an invalid option is found.
|
|
|
|
These rendezvous ids are then used to finish the pipe creation.*/
|
|
|
|
#ifdef HAVE_W32CE_SYSTEM
|
|
|
|
static void
|
|
|
|
parse_std_file_handles (int *argcp, char ***argvp)
|
|
|
|
{
|
|
|
|
int argc = *argcp;
|
|
|
|
char **argv = *argvp;
|
|
|
|
const char *s;
|
|
|
|
assuan_fd_t fd;
|
|
|
|
int i;
|
|
|
|
int fixup = 0;
|
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (argc--, argv++; argc; argc--, argv++)
|
|
|
|
{
|
|
|
|
s = *argv;
|
|
|
|
if (*s == '-' && s[1] == '&' && s[2] == 'S'
|
|
|
|
&& (s[3] == '0' || s[3] == '1' || s[3] == '2')
|
2011-02-04 12:57:53 +01:00
|
|
|
&& s[4] == '='
|
2010-03-22 12:46:05 +00:00
|
|
|
&& (strchr ("-01234567890", s[5]) || !strcmp (s+5, "null")))
|
|
|
|
{
|
|
|
|
if (s[5] == 'n')
|
|
|
|
fd = ASSUAN_INVALID_FD;
|
|
|
|
else
|
|
|
|
fd = _assuan_w32ce_finish_pipe (atoi (s+5), s[3] != '0');
|
|
|
|
_es_set_std_fd (s[3] - '0', (int)fd);
|
|
|
|
fixup++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixup)
|
|
|
|
{
|
|
|
|
argc = *argcp;
|
|
|
|
argc -= fixup;
|
|
|
|
*argcp = argc;
|
|
|
|
|
|
|
|
argv = *argvp;
|
|
|
|
for (i=1; i < argc; i++)
|
|
|
|
argv[i] = argv[i + fixup];
|
|
|
|
for (; i < argc + fixup; i++)
|
|
|
|
argv[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-03-22 12:46:05 +00:00
|
|
|
}
|
|
|
|
#endif /*HAVE_W32CE_SYSTEM*/
|