Use gpgrt's new option parser for the remaining daemons.

* scd/scdaemon.c: Switch to the new option parser and enable a global
conf file.
* dirmngr/dirmngr.c: Ditto.
* g13/g13.c: Ditto.
* g13/g13-syshelp.c: Ditto.  Do not force verbose mode.
* dirmngr/dirmngr_ldap.c: Switch to the new option parser.
* dirmngr/dirmngr-client.c: Switch to the new option parser.
--

This finalizes the switch to the new option parser.  What's left is to
remove the old argparser code from common.

g13-syshelp does not anymore default to --verbose because that can now
be enabled in /etc/gnupg/g13-syshelp.conf.

GnuPG-bug-id: 4788
Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2020-02-21 21:45:13 +01:00
parent 3bc004decd
commit ba463128ce
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
6 changed files with 313 additions and 382 deletions

View File

@ -16,12 +16,10 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,7 +33,6 @@
#include <assuan.h> #include <assuan.h>
#include "../common/logging.h" #include "../common/logging.h"
#include "../common/argparse.h"
#include "../common/stringhelp.h" #include "../common/stringhelp.h"
#include "../common/mischelp.h" #include "../common/mischelp.h"
#include "../common/strlist.h" #include "../common/strlist.h"
@ -44,7 +41,6 @@
#include "../common/i18n.h" #include "../common/i18n.h"
#include "../common/util.h" #include "../common/util.h"
#include "../common/init.h" #include "../common/init.h"
#include "../common/argparse.h" /* temporary hack. */
/* Constants for the options. */ /* Constants for the options. */
@ -69,7 +65,7 @@ enum
/* The list of options as used by the argparse.c code. */ /* The list of options as used by the argparse.c code. */
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
{ oVerbose, "verbose", 0, N_("verbose") }, { oVerbose, "verbose", 0, N_("verbose") },
{ oQuiet, "quiet", 0, N_("be somewhat more quiet") }, { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
{ oOCSP, "ocsp", 0, N_("use OCSP instead of CRLs") }, { oOCSP, "ocsp", 0, N_("use OCSP instead of CRLs") },
@ -162,9 +158,11 @@ my_strusage (int level)
switch(level) switch(level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "dirmngr-client (@GNUPG@)"; case 11: p = "dirmngr-client (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
case 49: p = PACKAGE_BUGREPORT; break; case 49: p = PACKAGE_BUGREPORT; break;
@ -190,7 +188,7 @@ my_strusage (int level)
int int
main (int argc, char **argv ) main (int argc, char **argv )
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
assuan_context_t ctx; assuan_context_t ctx;
gpg_error_t err; gpg_error_t err;
unsigned char *certbuf; unsigned char *certbuf;
@ -203,9 +201,12 @@ main (int argc, char **argv )
int cmd_squid_mode = 0; int cmd_squid_mode = 0;
early_system_init (); early_system_init ();
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
log_set_prefix ("dirmngr-client", log_set_prefix ("dirmngr-client",
GPGRT_LOG_WITH_PREFIX); GPGRT_LOG_WITH_PREFIX);
/* Register our string mapper with gpgrt. Usually done in
* init_common_subsystems, but we don't need that here. */
gpgrt_set_fixed_string_mapper (map_static_macro_string);
/* For W32 we need to initialize the socket subsystem. Because we /* For W32 we need to initialize the socket subsystem. Because we
don't use Pth we need to do this explicit. */ don't use Pth we need to do this explicit. */
@ -227,8 +228,8 @@ main (int argc, char **argv )
/* Parse the command line. */ /* Parse the command line. */
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* Do not remove the args. */ pargs.flags= ARGPARSE_FLAG_KEEP;
while (arg_parse (&pargs, opts) ) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
@ -251,9 +252,11 @@ main (int argc, char **argv )
break; break;
case oForceDefaultResponder: opt.force_default_responder = 1; break; case oForceDefaultResponder: opt.force_default_responder = 1; break;
default : pargs.err = 2; break; default : pargs.err = ARGPARSE_PRINT_ERROR; break;
} }
} }
gpgrt_argparse (NULL, &pargs, NULL);
if (log_get_errorcount (0)) if (log_get_errorcount (0))
exit (2); exit (2);
@ -262,14 +265,14 @@ main (int argc, char **argv )
else if (cmd_lookup || cmd_loadcrl) else if (cmd_lookup || cmd_loadcrl)
{ {
if (!argc) if (!argc)
usage (1); gpgrt_usage (1);
err = 0; err = 0;
} }
else if (cmd_squid_mode) else if (cmd_squid_mode)
{ {
err = 0; err = 0;
if (argc) if (argc)
usage (1); gpgrt_usage (1);
} }
else if (!argc) else if (!argc)
{ {
@ -288,7 +291,7 @@ main (int argc, char **argv )
else else
{ {
err = 0; err = 0;
usage (1); gpgrt_usage (1);
} }
if (log_get_errorcount (0)) if (log_get_errorcount (0))

View File

@ -17,14 +17,10 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* * SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-License-Identifier: GPL-3.0+
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -79,7 +75,6 @@
#include "../common/gc-opt-flags.h" #include "../common/gc-opt-flags.h"
#include "dns-stuff.h" #include "dns-stuff.h"
#include "http-common.h" #include "http-common.h"
#include "../common/argparse.h" /* temporary hack. */
#ifndef ENAMETOOLONG #ifndef ENAMETOOLONG
# define ENAMETOOLONG EINVAL # define ENAMETOOLONG EINVAL
@ -162,7 +157,7 @@ enum cmd_and_opt_values {
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
ARGPARSE_group (300, N_("@Commands:\n ")), ARGPARSE_group (300, N_("@Commands:\n ")),
@ -185,7 +180,7 @@ static ARGPARSE_OPTS opts[] = {
ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")),
ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")), ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")),
ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")), ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")),
ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")),
ARGPARSE_s_s (oDebugLevel, "debug-level", ARGPARSE_s_s (oDebugLevel, "debug-level",
N_("|LEVEL|set the debugging level to LEVEL")), N_("|LEVEL|set the debugging level to LEVEL")),
ARGPARSE_s_n (oNoDetach, "no-detach", N_("do not detach from the console")), ARGPARSE_s_n (oNoDetach, "no-detach", N_("do not detach from the console")),
@ -263,6 +258,7 @@ static ARGPARSE_OPTS opts[] = {
ARGPARSE_s_i (oConnectTimeout, "connect-timeout", "@"), ARGPARSE_s_i (oConnectTimeout, "connect-timeout", "@"),
ARGPARSE_s_i (oConnectQuickTimeout, "connect-quick-timeout", "@"), ARGPARSE_s_i (oConnectQuickTimeout, "connect-quick-timeout", "@"),
ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"),
ARGPARSE_noconffile (oNoOptions, "no-options", "@"),
ARGPARSE_group (302,N_("@\n(See the \"info\" manual for a complete listing " ARGPARSE_group (302,N_("@\n(See the \"info\" manual for a complete listing "
"of all commands and options)\n")), "of all commands and options)\n")),
@ -392,9 +388,11 @@ my_strusage( int level )
const char *p; const char *p;
switch ( level ) switch ( level )
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "@DIRMNGR@ (@GNUPG@)"; case 11: p = "@DIRMNGR@ (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
/* TRANSLATORS: @EMAIL@ will get replaced by the actual bug /* TRANSLATORS: @EMAIL@ will get replaced by the actual bug
reporting address. This is so that we can change the reporting address. This is so that we can change the
@ -593,7 +591,7 @@ shutdown_reaper (void)
PARGS, resets the options to the default. REREAD should be set PARGS, resets the options to the default. REREAD should be set
true if it is not the initial option parsing. */ true if it is not the initial option parsing. */
static int static int
parse_rereadable_options (ARGPARSE_ARGS *pargs, int reread) parse_rereadable_options (gpgrt_argparse_t *pargs, int reread)
{ {
if (!pargs) if (!pargs)
{ /* Reset mode. */ { /* Reset mode. */
@ -824,15 +822,13 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
enum cmd_and_opt_values cmd = 0; enum cmd_and_opt_values cmd = 0;
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int orig_argc; int orig_argc;
char **orig_argv; char **orig_argv;
FILE *configfp = NULL; char *last_configname = NULL;
char *configname = NULL; const char *configname = NULL;
const char *shell; const char *shell;
unsigned configlineno; int debug_argparser = 0;
int parse_debug = 0;
int default_config =1;
int greeting = 0; int greeting = 0;
int nogreeting = 0; int nogreeting = 0;
int nodetach = 0; int nodetach = 0;
@ -846,7 +842,7 @@ main (int argc, char **argv)
struct assuan_malloc_hooks malloc_hooks; struct assuan_malloc_hooks malloc_hooks;
early_system_init (); early_system_init ();
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
log_set_prefix (DIRMNGR_NAME, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_PID); log_set_prefix (DIRMNGR_NAME, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_PID);
/* Make sure that our subsystems are ready. */ /* Make sure that our subsystems are ready. */
@ -914,63 +910,56 @@ main (int argc, char **argv)
orig_argv = argv; orig_argv = argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
while (arg_parse( &pargs, opts)) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) switch (pargs.r_opt)
parse_debug++;
else if (pargs.r_opt == oOptions)
{ /* Yes there is one, so we do not try the default one, but
read the option file when it is encountered at the
commandline */
default_config = 0;
}
else if (pargs.r_opt == oNoOptions)
default_config = 0; /* --no-options */
else if (pargs.r_opt == oHomedir)
{ {
case oDebug:
case oDebugAll:
debug_argparser++;
break;
case oHomedir:
gnupg_set_homedir (pargs.r.ret_str); gnupg_set_homedir (pargs.r.ret_str);
break;
} }
} }
/* Reset the flags. */
pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
socket_name = dirmngr_socket_name (); socket_name = dirmngr_socket_name ();
if (default_config)
configname = make_filename (gnupg_homedir (), DIRMNGR_NAME".conf", NULL );
/* The configuraton directories for use by gpgrt_argparser. */
gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ());
gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ());
/* We are re-using the struct, thus the reset flag. We OR the
* flags so that the internal intialized flag won't be cleared. */
argc = orig_argc; argc = orig_argc;
argv = orig_argv; argv = orig_argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* do not remove the args */ pargs.flags |= (ARGPARSE_FLAG_RESET
next_pass: | ARGPARSE_FLAG_KEEP
if (configname) | ARGPARSE_FLAG_SYS
| ARGPARSE_FLAG_USER);
while (gpgrt_argparser (&pargs, opts, DIRMNGR_NAME EXTSEP_S "conf"))
{ {
configlineno = 0; if (pargs.r_opt == ARGPARSE_CONFFILE)
configfp = fopen (configname, "r");
if (!configfp)
{ {
if (default_config) if (debug_argparser)
log_info (_("reading options from '%s'\n"),
pargs.r_type? pargs.r.ret_str: "[cmdline]");
if (pargs.r_type)
{ {
if( parse_debug ) xfree (last_configname);
log_info (_("Note: no default option file '%s'\n"), last_configname = xstrdup (pargs.r.ret_str);
configname ); configname = last_configname;
} }
else else
{ configname = NULL;
log_error (_("option file '%s': %s\n"), continue;
configname, strerror(errno) ); }
exit(2);
}
xfree (configname);
configname = NULL;
}
if (parse_debug && configname )
log_info (_("reading options from '%s'\n"), configname );
default_config = 0;
}
while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) )
{
if (parse_rereadable_options (&pargs, 0)) if (parse_rereadable_options (&pargs, 0))
continue; /* Already handled */ continue; /* Already handled */
switch (pargs.r_opt) switch (pargs.r_opt)
@ -994,18 +983,8 @@ main (int argc, char **argv)
case oDebugWait: debug_wait = pargs.r.ret_int; break; case oDebugWait: debug_wait = pargs.r.ret_int; break;
case oOptions:
/* Config files may not be nested (silently ignore them) */
if (!configfp)
{
xfree(configname);
configname = xstrdup(pargs.r.ret_str);
goto next_pass;
}
break;
case oNoGreeting: nogreeting = 1; break; case oNoGreeting: nogreeting = 1; break;
case oNoVerbose: opt.verbose = 0; break; case oNoVerbose: opt.verbose = 0; break;
case oNoOptions: break; /* no-options */
case oHomedir: /* Ignore this option here. */; break; case oHomedir: /* Ignore this option here. */; break;
case oNoDetach: nodetach = 1; break; case oNoDetach: nodetach = 1; break;
case oLogFile: logfile = pargs.r.ret_str; break; case oLogFile: logfile = pargs.r.ret_str; break;
@ -1033,20 +1012,26 @@ main (int argc, char **argv)
listen_backlog = pargs.r.ret_int; listen_backlog = pargs.r.ret_int;
break; break;
default : pargs.err = configfp? 1:2; break; default:
if (configname)
pargs.err = ARGPARSE_PRINT_WARNING;
else
pargs.err = ARGPARSE_PRINT_ERROR;
break;
} }
} }
if (configfp) gpgrt_argparse (NULL, &pargs, NULL); /* Release internal state. */
if (!last_configname)
opt.config_filename = gpgrt_fnameconcat (gnupg_homedir (),
DIRMNGR_NAME EXTSEP_S "conf",
NULL);
else
{ {
fclose (configfp); opt.config_filename = last_configname;
configfp = NULL; last_configname = NULL;
/* Keep a copy of the name so that it can be read on SIGHUP. */
opt.config_filename = configname;
configname = NULL;
goto next_pass;
} }
xfree (configname);
configname = NULL;
if (log_get_errorcount(0)) if (log_get_errorcount(0))
exit(2); exit(2);
if (nogreeting ) if (nogreeting )
@ -1058,8 +1043,8 @@ main (int argc, char **argv)
if (greeting) if (greeting)
{ {
es_fprintf (es_stderr, "%s %s; %s\n", es_fprintf (es_stderr, "%s %s; %s\n",
strusage(11), strusage(13), strusage(14) ); gpgrt_strusage(11), gpgrt_strusage(13), gpgrt_strusage(14));
es_fprintf (es_stderr, "%s\n", strusage(15) ); es_fprintf (es_stderr, "%s\n", gpgrt_strusage(15));
} }
#ifdef IS_DEVELOPMENT_VERSION #ifdef IS_DEVELOPMENT_VERSION
@ -1489,10 +1474,6 @@ main (int argc, char **argv)
/* First the configuration file. This is not an option, but it /* First the configuration file. This is not an option, but it
is vital information for GPG Conf. */ is vital information for GPG Conf. */
if (!opt.config_filename)
opt.config_filename = make_filename (gnupg_homedir (),
"dirmngr.conf", NULL );
filename = percent_escape (opt.config_filename, NULL); filename = percent_escape (opt.config_filename, NULL);
es_printf ("gpgconf-dirmngr.conf:%lu:\"%s\n", es_printf ("gpgconf-dirmngr.conf:%lu:\"%s\n",
GC_OPT_FLAG_DEFAULT, filename); GC_OPT_FLAG_DEFAULT, filename);
@ -1830,36 +1811,33 @@ parse_ocsp_signer (const char *string)
static void static void
reread_configuration (void) reread_configuration (void)
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
FILE *fp;
unsigned int configlineno = 0;
int dummy; int dummy;
if (!opt.config_filename) if (!opt.config_filename)
return; /* No config file. */ return; /* No config file. */
fp = fopen (opt.config_filename, "r");
if (!fp)
{
log_error (_("option file '%s': %s\n"),
opt.config_filename, strerror(errno) );
return;
}
parse_rereadable_options (NULL, 1); /* Start from the default values. */ parse_rereadable_options (NULL, 1); /* Start from the default values. */
memset (&pargs, 0, sizeof pargs); memset (&pargs, 0, sizeof pargs);
dummy = 0; dummy = 0;
pargs.argc = &dummy; pargs.argc = &dummy;
pargs.flags = 1; /* do not remove the args */ pargs.flags = (ARGPARSE_FLAG_KEEP
while (optfile_parse (fp, opt.config_filename, &configlineno, &pargs, opts) ) |ARGPARSE_FLAG_SYS
|ARGPARSE_FLAG_USER);
while (gpgrt_argparser (&pargs, opts, DIRMNGR_NAME EXTSEP_S "conf") )
{ {
if (pargs.r_opt < -1) if (pargs.r_opt == ARGPARSE_CONFFILE)
pargs.err = 1; /* Print a warning. */ {
log_info (_("reading options from '%s'\n"),
pargs.r_type? pargs.r.ret_str: "[cmdline]");
}
else if (pargs.r_opt < -1)
pargs.err = ARGPARSE_PRINT_WARNING;
else /* Try to parse this option - ignore unchangeable ones. */ else /* Try to parse this option - ignore unchangeable ones. */
parse_rereadable_options (&pargs, 1); parse_rereadable_options (&pargs, 1);
} }
fclose (fp); gpgrt_argparse (NULL, &pargs, NULL); /* Release internal state. */
post_option_parsing (); post_option_parsing ();
} }
@ -1924,7 +1902,7 @@ handle_signal (int signo)
if (shutdown_pending > 2) if (shutdown_pending > 2)
{ {
log_info (_("shutdown forced\n")); log_info (_("shutdown forced\n"));
log_info ("%s %s stopped\n", strusage(11), strusage(13) ); log_info ("%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
cleanup (); cleanup ();
dirmngr_exit (0); dirmngr_exit (0);
} }
@ -1932,7 +1910,7 @@ handle_signal (int signo)
case SIGINT: case SIGINT:
log_info (_("SIGINT received - immediate shutdown\n")); log_info (_("SIGINT received - immediate shutdown\n"));
log_info( "%s %s stopped\n", strusage(11), strusage(13)); log_info( "%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
cleanup (); cleanup ();
dirmngr_exit (0); dirmngr_exit (0);
break; break;
@ -2348,7 +2326,7 @@ handle_connections (assuan_fd_t listen_fd)
if (listen_fd != GNUPG_INVALID_FD) if (listen_fd != GNUPG_INVALID_FD)
assuan_sock_close (listen_fd); assuan_sock_close (listen_fd);
cleanup (); cleanup ();
log_info ("%s %s stopped\n", strusage(11), strusage(13)); log_info ("%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
} }
const char* const char*

View File

@ -16,12 +16,11 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -50,7 +49,6 @@
#include <gpg-error.h> #include <gpg-error.h>
#include "../common/logging.h" #include "../common/logging.h"
#include "../common/argparse.h"
#include "../common/stringhelp.h" #include "../common/stringhelp.h"
#include "../common/mischelp.h" #include "../common/mischelp.h"
#include "../common/strlist.h" #include "../common/strlist.h"
@ -58,7 +56,6 @@
#include "../common/i18n.h" #include "../common/i18n.h"
#include "../common/util.h" #include "../common/util.h"
#include "../common/init.h" #include "../common/init.h"
#include "../common/argparse.h" /* temporary hack. */
/* There is no need for the npth_unprotect and leave functions here; /* There is no need for the npth_unprotect and leave functions here;
* thus we redefine them to nops. We keep them in the code just for * thus we redefine them to nops. We keep them in the code just for
@ -101,7 +98,7 @@ enum
/* The list of options as used by the argparse.c code. */ /* The list of options as used by the argparse.c code. */
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
{ oVerbose, "verbose", 0, N_("verbose") }, { oVerbose, "verbose", 0, N_("verbose") },
{ oQuiet, "quiet", 0, N_("be somewhat more quiet") }, { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
{ oTimeout, "timeout", 1, N_("|N|set LDAP timeout to N seconds")}, { oTimeout, "timeout", 1, N_("|N|set LDAP timeout to N seconds")},
@ -167,11 +164,13 @@ my_strusage (int level)
{ {
const char *p; const char *p;
switch(level) switch (level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "dirmngr_ldap (@GNUPG@)"; case 11: p = "dirmngr_ldap (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
case 49: p = PACKAGE_BUGREPORT; break; case 49: p = PACKAGE_BUGREPORT; break;
@ -194,7 +193,7 @@ my_strusage (int level)
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int any_err = 0; int any_err = 0;
char *p; char *p;
int only_search_timeout = 0; int only_search_timeout = 0;
@ -206,7 +205,7 @@ main (int argc, char **argv)
early_system_init (); early_system_init ();
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
log_set_prefix ("dirmngr_ldap", GPGRT_LOG_WITH_PREFIX); log_set_prefix ("dirmngr_ldap", GPGRT_LOG_WITH_PREFIX);
/* Setup I18N and common subsystems. */ /* Setup I18N and common subsystems. */
@ -225,8 +224,8 @@ main (int argc, char **argv)
/* Parse the command line. */ /* Parse the command line. */
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* Do not remove the args. */ pargs.flags= ARGPARSE_FLAG_KEEP;
while (arg_parse (&pargs, opts) ) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
@ -264,6 +263,7 @@ main (int argc, char **argv)
break; break;
} }
} }
gpgrt_argparse (NULL, &pargs, NULL);
if (only_search_timeout) if (only_search_timeout)
myopt->alarm_timeout = 0; myopt->alarm_timeout = 0;
@ -293,7 +293,7 @@ main (int argc, char **argv)
if (log_get_errorcount (0)) if (log_get_errorcount (0))
exit (2); exit (2);
if (argc < 1) if (argc < 1)
usage (1); gpgrt_usage (1);
if (myopt->alarm_timeout) if (myopt->alarm_timeout)
{ {

View File

@ -15,12 +15,11 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -45,7 +44,6 @@
#include "../common/asshelp.h" #include "../common/asshelp.h"
#include "../common/init.h" #include "../common/init.h"
#include "keyblob.h" #include "keyblob.h"
#include "../common/argparse.h" /* temporary hack. */
enum cmd_and_opt_values { enum cmd_and_opt_values {
@ -93,7 +91,7 @@ enum cmd_and_opt_values {
}; };
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")), ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")),
@ -149,9 +147,11 @@ my_strusage( int level )
switch (level) switch (level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "@G13@-syshelp (@GNUPG@)"; case 11: p = "@G13@-syshelp (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n"); case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
break; break;
@ -221,20 +221,18 @@ set_debug (void)
int int
main ( int argc, char **argv) main (int argc, char **argv)
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int orig_argc; int orig_argc;
char **orig_argv; char **orig_argv;
gpg_error_t err = 0; gpg_error_t err = 0;
/* const char *fname; */ /* const char *fname; */
int may_coredump; int may_coredump;
FILE *configfp = NULL; char *last_configname = NULL;
char *configname = NULL; const char *configname = NULL;
unsigned configlineno; int debug_argparser = 0;
int parse_debug = 0;
int no_more_options = 0; int no_more_options = 0;
int default_config =1;
char *logfile = NULL; char *logfile = NULL;
/* int debug_wait = 0; */ /* int debug_wait = 0; */
int use_random_seed = 1; int use_random_seed = 1;
@ -246,7 +244,7 @@ main ( int argc, char **argv)
early_system_init (); early_system_init ();
gnupg_reopen_std (G13_NAME "-syshelp"); gnupg_reopen_std (G13_NAME "-syshelp");
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
log_set_prefix (G13_NAME "-syshelp", GPGRT_LOG_WITH_PREFIX); log_set_prefix (G13_NAME "-syshelp", GPGRT_LOG_WITH_PREFIX);
@ -269,22 +267,24 @@ main ( int argc, char **argv)
log_fatal ("error allocating session environment block: %s\n", log_fatal ("error allocating session environment block: %s\n",
strerror (errno)); strerror (errno));
/* Fixme: We enable verbose mode here because there is currently no
way to do this when starting g13-syshelp. To fix that we should
add a g13-syshelp.conf file in /etc/gnupg. */
opt.verbose = 1;
/* First check whether we have a debug option on the commandline. */ /* First check whether we have a debug option on the commandline. */
orig_argc = argc; orig_argc = argc;
orig_argv = argv; orig_argv = argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
while (arg_parse( &pargs, opts)) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) switch (pargs.r_opt)
parse_debug++; {
case oDebug:
case oDebugAll:
debug_argparser++;
break;
}
} }
/* Reset the flags. */
pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
/* Initialize the secure memory. */ /* Initialize the secure memory. */
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
@ -315,47 +315,40 @@ main ( int argc, char **argv)
ctrl.no_server = 1; ctrl.no_server = 1;
ctrl.status_fd = -1; /* No status output. */ ctrl.status_fd = -1; /* No status output. */
if (default_config ) /* The configuraton directories for use by gpgrt_argparser. */
configname = make_filename (gnupg_sysconfdir (), gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ());
G13_NAME"-syshelp.conf", NULL); gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ());
argc = orig_argc; argc = orig_argc;
argv = orig_argv; argv = orig_argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags = 1; /* Do not remove the args. */ pargs.flags |= (ARGPARSE_FLAG_RESET
| ARGPARSE_FLAG_KEEP
next_pass: | ARGPARSE_FLAG_SYS
if (configname) | ARGPARSE_FLAG_USER);
{
configlineno = 0;
configfp = fopen (configname, "r");
if (!configfp)
{
if (default_config)
{
if (parse_debug)
log_info (_("NOTE: no default option file '%s'\n"), configname);
}
else
{
log_error (_("option file '%s': %s\n"),
configname, strerror(errno));
g13_exit(2);
}
xfree (configname);
configname = NULL;
}
if (parse_debug && configname)
log_info (_("reading options from '%s'\n"), configname);
default_config = 0;
}
while (!no_more_options while (!no_more_options
&& optfile_parse (configfp, configname, &configlineno, &pargs, opts)) && gpgrt_argparser (&pargs, opts, G13_NAME"-syshelp" EXTSEP_S "conf"))
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
case ARGPARSE_CONFFILE:
{
if (debug_argparser)
log_info (_("reading options from '%s'\n"),
pargs.r_type? pargs.r.ret_str: "[cmdline]");
if (pargs.r_type)
{
xfree (last_configname);
last_configname = xstrdup (pargs.r.ret_str);
configname = last_configname;
}
else
configname = NULL;
}
break;
case oQuiet: opt.quiet = 1; break; case oQuiet: opt.quiet = 1; break;
case oDryRun: opt.dry_run = 1; break; case oDryRun: opt.dry_run = 1; break;
@ -408,26 +401,22 @@ main ( int argc, char **argv)
case oNoRandomSeedFile: use_random_seed = 0; break; case oNoRandomSeedFile: use_random_seed = 0; break;
default: default:
pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; pargs.err = configname? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR;
break; break;
} }
} }
gpgrt_argparse (NULL, &pargs, NULL);
if (configfp)
if (!last_configname)
opt.config_filename = gpgrt_fnameconcat (gnupg_homedir (),
G13_NAME"-syshelp" EXTSEP_S "conf",
NULL);
else
{ {
fclose (configfp); opt.config_filename = last_configname;
configfp = NULL; last_configname = NULL;
/* Keep a copy of the config filename. */
opt.config_filename = configname;
configname = NULL;
goto next_pass;
} }
xfree (configname);
configname = NULL;
if (!opt.config_filename)
opt.config_filename = make_filename (gnupg_homedir (),
G13_NAME".conf", NULL);
if (log_get_errorcount(0)) if (log_get_errorcount(0))
g13_exit(2); g13_exit(2);

172
g13/g13.c
View File

@ -15,12 +15,11 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -50,7 +49,6 @@
#include "mountinfo.h" #include "mountinfo.h"
#include "backend.h" #include "backend.h"
#include "call-syshelp.h" #include "call-syshelp.h"
#include "../common/argparse.h" /* temporary hack. */
enum cmd_and_opt_values { enum cmd_and_opt_values {
@ -111,7 +109,7 @@ enum cmd_and_opt_values {
}; };
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
ARGPARSE_group (300, N_("@Commands:\n ")), ARGPARSE_group (300, N_("@Commands:\n ")),
@ -142,7 +140,7 @@ static ARGPARSE_OPTS opts[] = {
ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")), ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")),
ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")),
ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebug, "debug", "@"),
ARGPARSE_s_s (oDebugLevel, "debug-level", ARGPARSE_s_s (oDebugLevel, "debug-level",
@ -167,7 +165,7 @@ static ARGPARSE_OPTS opts[] = {
ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"), ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"),
ARGPARSE_s_n (oNoSecmemWarn, "no-secmem-warning", "@"), ARGPARSE_s_n (oNoSecmemWarn, "no-secmem-warning", "@"),
ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"), ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"),
ARGPARSE_s_n (oNoOptions, "no-options", "@"), ARGPARSE_noconffile (oNoOptions, "no-options", "@"),
ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"),
ARGPARSE_s_s (oAgentProgram, "agent-program", "@"), ARGPARSE_s_s (oAgentProgram, "agent-program", "@"),
ARGPARSE_s_s (oGpgProgram, "gpg-program", "@"), ARGPARSE_s_s (oGpgProgram, "gpg-program", "@"),
@ -239,9 +237,11 @@ my_strusage( int level )
switch (level) switch (level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "@G13@ (@GNUPG@)"; case 11: p = "@G13@ (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n"); case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
break; break;
@ -339,20 +339,18 @@ set_cmd (enum cmd_and_opt_values *ret_cmd, enum cmd_and_opt_values new_cmd)
int int
main ( int argc, char **argv) main (int argc, char **argv)
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int orig_argc; int orig_argc;
char **orig_argv; char **orig_argv;
gpg_error_t err = 0; gpg_error_t err = 0;
/* const char *fname; */ /* const char *fname; */
int may_coredump; int may_coredump;
FILE *configfp = NULL; char *last_configname = NULL;
char *configname = NULL; const char *configname = NULL;
unsigned configlineno; int debug_argparser = 0;
int parse_debug = 0;
int no_more_options = 0; int no_more_options = 0;
int default_config =1;
char *logfile = NULL; char *logfile = NULL;
int greeting = 0; int greeting = 0;
int nogreeting = 0; int nogreeting = 0;
@ -368,7 +366,7 @@ main ( int argc, char **argv)
early_system_init (); early_system_init ();
gnupg_reopen_std (G13_NAME); gnupg_reopen_std (G13_NAME);
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
log_set_prefix (G13_NAME, GPGRT_LOG_WITH_PREFIX); log_set_prefix (G13_NAME, GPGRT_LOG_WITH_PREFIX);
@ -398,30 +396,31 @@ main ( int argc, char **argv)
orig_argv = argv; orig_argv = argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1|(1<<6); /* Do not remove the args, ignore version. */ pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
while (arg_parse( &pargs, opts)) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) switch (pargs.r_opt)
parse_debug++; {
else if (pargs.r_opt == oOptions) case oDebug:
{ /* Yes, there is one, so we do not try the default one but case oDebugAll:
read the config file when it is encountered at the debug_argparser++;
commandline. */ break;
default_config = 0;
} case oHomedir:
else if (pargs.r_opt == oNoOptions) gnupg_set_homedir (pargs.r.ret_str);
default_config = 0; /* --no-options */ break;
else if (pargs.r_opt == oHomedir) }
gnupg_set_homedir (pargs.r.ret_str);
} }
/* Reset the flags. */
pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
/* Initialize the secure memory. */ /* Initialize the secure memory. */
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
maybe_setuid = 0; maybe_setuid = 0;
/* /*
Now we are now working under our real uid * Now we are now working under our real uid
*/ */
/* Setup malloc hooks. */ /* Setup malloc hooks. */
{ {
@ -444,47 +443,42 @@ main ( int argc, char **argv)
ctrl.no_server = 1; ctrl.no_server = 1;
ctrl.status_fd = -1; /* No status output. */ ctrl.status_fd = -1; /* No status output. */
/* Set the default option file */ /* The configuraton directories for use by gpgrt_argparser. */
if (default_config ) gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ());
configname = make_filename (gnupg_homedir (), G13_NAME".conf", NULL); gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ());
/* We are re-using the struct, thus the reset flag. We OR the
* flags so that the internal intialized flag won't be cleared. */
argc = orig_argc; argc = orig_argc;
argv = orig_argv; argv = orig_argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags = 1; /* Do not remove the args. */ pargs.flags |= (ARGPARSE_FLAG_RESET
| ARGPARSE_FLAG_KEEP
next_pass: | ARGPARSE_FLAG_SYS
if (configname) | ARGPARSE_FLAG_USER);
{
configlineno = 0;
configfp = fopen (configname, "r");
if (!configfp)
{
if (default_config)
{
if (parse_debug)
log_info (_("Note: no default option file '%s'\n"), configname);
}
else
{
log_error (_("option file '%s': %s\n"),
configname, strerror(errno));
g13_exit(2);
}
xfree (configname);
configname = NULL;
}
if (parse_debug && configname)
log_info (_("reading options from '%s'\n"), configname);
default_config = 0;
}
while (!no_more_options while (!no_more_options
&& optfile_parse (configfp, configname, &configlineno, &pargs, opts)) && gpgrt_argparser (&pargs, opts, G13_NAME EXTSEP_S "conf"))
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
case ARGPARSE_CONFFILE:
{
if (debug_argparser)
log_info (_("reading options from '%s'\n"),
pargs.r_type? pargs.r.ret_str: "[cmdline]");
if (pargs.r_type)
{
xfree (last_configname);
last_configname = xstrdup (pargs.r.ret_str);
configname = last_configname;
}
else
configname = NULL;
}
break;
case aGPGConfList: case aGPGConfList:
case aGPGConfTest: case aGPGConfTest:
set_cmd (&cmd, pargs.r_opt); set_cmd (&cmd, pargs.r_opt);
@ -542,17 +536,6 @@ main ( int argc, char **argv)
case oStatusFD: ctrl.status_fd = pargs.r.ret_int; break; case oStatusFD: ctrl.status_fd = pargs.r.ret_int; break;
case oLoggerFD: log_set_fd (pargs.r.ret_int ); break; case oLoggerFD: log_set_fd (pargs.r.ret_int ); break;
case oNoOptions: break; /* no-options */
case oOptions:
/* Config files may not be nested (silently ignore them). */
if (!configfp)
{
xfree(configname);
configname = xstrdup (pargs.r.ret_str);
goto next_pass;
}
break;
case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
case oAgentProgram: opt.agent_program = pargs.r.ret_str; break; case oAgentProgram: opt.agent_program = pargs.r.ret_str; break;
@ -596,12 +579,17 @@ main ( int argc, char **argv)
break; break;
default: default:
pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; if (configname)
pargs.err = ARGPARSE_PRINT_WARNING;
else
pargs.err = ARGPARSE_PRINT_ERROR;
break; break;
} }
} }
/* XXX Construct GPG arguments. */ gpgrt_argparse (NULL, &pargs, NULL);
/* Construct GPG arguments. */
{ {
strlist_t last; strlist_t last;
last = append_to_strlist (&opt.gpg_arguments, "-z"); last = append_to_strlist (&opt.gpg_arguments, "-z");
@ -611,21 +599,15 @@ main ( int argc, char **argv)
(void) last; (void) last;
} }
if (configfp) if (!last_configname)
opt.config_filename = gpgrt_fnameconcat (gnupg_homedir (),
G13_NAME EXTSEP_S "conf",
NULL);
else
{ {
fclose (configfp); opt.config_filename = last_configname;
configfp = NULL; last_configname = NULL;
/* Keep a copy of the config filename. */
opt.config_filename = configname;
configname = NULL;
goto next_pass;
} }
xfree (configname);
configname = NULL;
if (!opt.config_filename)
opt.config_filename = make_filename (gnupg_homedir (),
G13_NAME".conf", NULL);
if (log_get_errorcount(0)) if (log_get_errorcount(0))
g13_exit(2); g13_exit(2);
@ -642,8 +624,8 @@ main ( int argc, char **argv)
if (greeting) if (greeting)
{ {
fprintf (stderr, "%s %s; %s\n", fprintf (stderr, "%s %s; %s\n",
strusage(11), strusage(13), strusage(14) ); gpgrt_strusage(11), gpgrt_strusage(13), gpgrt_strusage(14) );
fprintf (stderr, "%s\n", strusage(15) ); fprintf (stderr, "%s\n", gpgrt_strusage(15));
} }
if (may_coredump && !opt.quiet) if (may_coredump && !opt.quiet)
@ -663,7 +645,9 @@ main ( int argc, char **argv)
if (logfile) if (logfile)
{ {
log_set_file (logfile); log_set_file (logfile);
log_set_prefix (NULL, GPGRT_LOG_WITH_PREFIX | GPGRT_LOG_WITH_TIME | GPGRT_LOG_WITH_PID); log_set_prefix (NULL, (GPGRT_LOG_WITH_PREFIX
| GPGRT_LOG_WITH_TIME
| GPGRT_LOG_WITH_PID));
} }
if (gnupg_faked_time_p ()) if (gnupg_faked_time_p ())
@ -907,14 +891,14 @@ handle_signal (int signo)
if (shutdown_pending > 2) if (shutdown_pending > 2)
{ {
log_info ("shutdown forced\n"); log_info ("shutdown forced\n");
log_info ("%s %s stopped\n", strusage(11), strusage(13) ); log_info ("%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13) );
g13_exit (0); g13_exit (0);
} }
break; break;
case SIGINT: case SIGINT:
log_info ("SIGINT received - immediate shutdown\n"); log_info ("SIGINT received - immediate shutdown\n");
log_info( "%s %s stopped\n", strusage(11), strusage(13)); log_info( "%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
g13_exit (0); g13_exit (0);
break; break;
#endif /*!HAVE_W32_SYSTEM*/ #endif /*!HAVE_W32_SYSTEM*/
@ -1012,7 +996,7 @@ idle_task (void *dummy_arg)
/* Here one would add processing of file descriptors. */ /* Here one would add processing of file descriptors. */
} }
log_info (_("%s %s stopped\n"), strusage(11), strusage(13)); log_info (_("%s %s stopped\n"), gpgrt_strusage(11), gpgrt_strusage(13));
return NULL; return NULL;
} }

View File

@ -16,12 +16,10 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
/* We don't want to have the macros from gpgrt here until we have
* completely replaced this module by the one from gpgrt. */
#undef GPGRT_ENABLE_ARGPARSE_MACROS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -48,7 +46,6 @@
#include <assuan.h> /* malloc hooks */ #include <assuan.h> /* malloc hooks */
#include "../common/argparse.h" /* temporary hack. */
#include "../common/i18n.h" #include "../common/i18n.h"
#include "../common/sysutils.h" #include "../common/sysutils.h"
#include "iso7816.h" #include "iso7816.h"
@ -109,7 +106,7 @@ enum cmd_and_opt_values
static ARGPARSE_OPTS opts[] = { static gpgrt_opt_t opts[] = {
ARGPARSE_c (aGPGConfList, "gpgconf-list", "@"), ARGPARSE_c (aGPGConfList, "gpgconf-list", "@"),
ARGPARSE_c (aGPGConfTest, "gpgconf-test", "@"), ARGPARSE_c (aGPGConfTest, "gpgconf-test", "@"),
@ -123,7 +120,7 @@ static ARGPARSE_OPTS opts[] = {
ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")),
ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")), ARGPARSE_s_n (oSh, "sh", N_("sh-style command output")),
ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")), ARGPARSE_s_n (oCsh, "csh", N_("csh-style command output")),
ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")), ARGPARSE_conffile (oOptions, "options", N_("|FILE|read options from FILE")),
ARGPARSE_s_s (oDebug, "debug", "@"), ARGPARSE_s_s (oDebug, "debug", "@"),
ARGPARSE_s_n (oDebugAll, "debug-all", "@"), ARGPARSE_s_n (oDebugAll, "debug-all", "@"),
ARGPARSE_s_s (oDebugLevel, "debug-level" , ARGPARSE_s_s (oDebugLevel, "debug-level" ,
@ -165,6 +162,7 @@ static ARGPARSE_OPTS opts[] = {
N_("use variable length input for pinpad")), N_("use variable length input for pinpad")),
ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_s (oHomedir, "homedir", "@"),
ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"),
ARGPARSE_noconffile (oNoOptions, "no-options", "@"),
ARGPARSE_end () ARGPARSE_end ()
}; };
@ -289,9 +287,11 @@ my_strusage (int level)
switch (level) switch (level)
{ {
case 9: p = "GPL-3.0-or-later"; break;
case 11: p = "@SCDAEMON@ (@GNUPG@)"; case 11: p = "@SCDAEMON@ (@GNUPG@)";
break; break;
case 13: p = VERSION; break; case 13: p = VERSION; break;
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
case 17: p = PRINTABLE_OS_NAME; break; case 17: p = PRINTABLE_OS_NAME; break;
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
@ -420,16 +420,14 @@ setup_signal_mask (void)
int int
main (int argc, char **argv ) main (int argc, char **argv )
{ {
ARGPARSE_ARGS pargs; gpgrt_argparse_t pargs;
int orig_argc; int orig_argc;
char **orig_argv; char **orig_argv;
FILE *configfp = NULL; char *last_configname = NULL;
char *configname = NULL; const char *configname = NULL;
const char *shell; const char *shell;
unsigned int configlineno; int debug_argparser = 0;
int parse_debug = 0;
const char *debug_level = NULL; const char *debug_level = NULL;
int default_config =1;
int greeting = 0; int greeting = 0;
int nogreeting = 0; int nogreeting = 0;
int multi_server = 0; int multi_server = 0;
@ -439,7 +437,7 @@ main (int argc, char **argv )
char *logfile = NULL; char *logfile = NULL;
int debug_wait = 0; int debug_wait = 0;
int gpgconf_list = 0; int gpgconf_list = 0;
const char *config_filename = NULL; char *config_filename = NULL;
int allow_coredump = 0; int allow_coredump = 0;
struct assuan_malloc_hooks malloc_hooks; struct assuan_malloc_hooks malloc_hooks;
int res; int res;
@ -447,7 +445,7 @@ main (int argc, char **argv )
const char *application_priority = NULL; const char *application_priority = NULL;
early_system_init (); early_system_init ();
set_strusage (my_strusage); gpgrt_set_strusage (my_strusage);
gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
/* Please note that we may running SUID(ROOT), so be very CAREFUL /* Please note that we may running SUID(ROOT), so be very CAREFUL
when adding any stuff between here and the call to INIT_SECMEM() when adding any stuff between here and the call to INIT_SECMEM()
@ -487,22 +485,22 @@ main (int argc, char **argv )
orig_argv = argv; orig_argv = argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
while (arg_parse( &pargs, opts)) while (gpgrt_argparse (NULL, &pargs, opts))
{ {
if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) switch (pargs.r_opt)
parse_debug++; {
else if (pargs.r_opt == oOptions) case oDebug:
{ /* yes there is one, so we do not try the default one, but case oDebugAll:
read the option file when it is encountered at the debug_argparser++;
commandline */ break;
default_config = 0; case oHomedir:
}
else if (pargs.r_opt == oNoOptions)
default_config = 0; /* --no-options */
else if (pargs.r_opt == oHomedir)
gnupg_set_homedir (pargs.r.ret_str); gnupg_set_homedir (pargs.r.ret_str);
break;
}
} }
/* Reset the flags. */
pargs.flags &= ~(ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION);
/* initialize the secure memory. */ /* initialize the secure memory. */
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
@ -512,48 +510,38 @@ main (int argc, char **argv )
Now we are working under our real uid Now we are working under our real uid
*/ */
/* The configuraton directories for use by gpgrt_argparser. */
gpgrt_set_confdir (GPGRT_CONFDIR_SYS, gnupg_sysconfdir ());
gpgrt_set_confdir (GPGRT_CONFDIR_USER, gnupg_homedir ());
if (default_config) /* We are re-using the struct, thus the reset flag. We OR the
configname = make_filename (gnupg_homedir (), SCDAEMON_NAME EXTSEP_S "conf", * flags so that the internal intialized flag won't be cleared. */
NULL );
argc = orig_argc; argc = orig_argc;
argv = orig_argv; argv = orig_argv;
pargs.argc = &argc; pargs.argc = &argc;
pargs.argv = &argv; pargs.argv = &argv;
pargs.flags= 1; /* do not remove the args */ pargs.flags |= (ARGPARSE_FLAG_RESET
next_pass: | ARGPARSE_FLAG_KEEP
if (configname) | ARGPARSE_FLAG_SYS
{ | ARGPARSE_FLAG_USER);
configlineno = 0; while (gpgrt_argparser (&pargs, opts, SCDAEMON_NAME EXTSEP_S "conf"))
configfp = fopen (configname, "r");
if (!configfp)
{
if (default_config)
{
if( parse_debug )
log_info (_("Note: no default option file '%s'\n"),
configname );
}
else
{
log_error (_("option file '%s': %s\n"),
configname, strerror(errno) );
exit(2);
}
xfree (configname);
configname = NULL;
}
if (parse_debug && configname )
log_info (_("reading options from '%s'\n"), configname );
default_config = 0;
}
while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) )
{ {
switch (pargs.r_opt) switch (pargs.r_opt)
{ {
case ARGPARSE_CONFFILE:
if (debug_argparser)
log_info (_("reading options from '%s'\n"),
pargs.r_type? pargs.r.ret_str: "[cmdline]");
if (pargs.r_type)
{
xfree (last_configname);
last_configname = xstrdup (pargs.r.ret_str);
configname = last_configname;
}
else
configname = NULL;
break;
case aGPGConfList: gpgconf_list = 1; break; case aGPGConfList: gpgconf_list = 1; break;
case aGPGConfTest: gpgconf_list = 2; break; case aGPGConfTest: gpgconf_list = 2; break;
case oQuiet: opt.quiet = 1; break; case oQuiet: opt.quiet = 1; break;
@ -586,18 +574,8 @@ main (int argc, char **argv )
set_libassuan_log_cats (pargs.r.ret_ulong); set_libassuan_log_cats (pargs.r.ret_ulong);
break; break;
case oOptions:
/* config files may not be nested (silently ignore them) */
if (!configfp)
{
xfree(configname);
configname = xstrdup(pargs.r.ret_str);
goto next_pass;
}
break;
case oNoGreeting: nogreeting = 1; break; case oNoGreeting: nogreeting = 1; break;
case oNoVerbose: opt.verbose = 0; break; case oNoVerbose: opt.verbose = 0; break;
case oNoOptions: break; /* no-options */
case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
case oNoDetach: nodetach = 1; break; case oNoDetach: nodetach = 1; break;
case oLogFile: logfile = pargs.r.ret_str; break; case oLogFile: logfile = pargs.r.ret_str; break;
@ -636,21 +614,26 @@ main (int argc, char **argv )
break; break;
default: default:
pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; if (configname)
pargs.err = ARGPARSE_PRINT_WARNING;
else
pargs.err = ARGPARSE_PRINT_ERROR;
break; break;
} }
} }
if (configfp)
gpgrt_argparse (NULL, &pargs, NULL); /* Release internal state. */
if (!last_configname)
config_filename = gpgrt_fnameconcat (gnupg_homedir (),
SCDAEMON_NAME EXTSEP_S "conf",
NULL);
else
{ {
fclose( configfp ); config_filename = last_configname;
configfp = NULL; last_configname = NULL;
/* Keep a copy of the config name for use by --gpgconf-list. */
config_filename = configname;
configname = NULL;
goto next_pass;
} }
xfree (configname);
configname = NULL;
if (log_get_errorcount(0)) if (log_get_errorcount(0))
exit(2); exit(2);
if (nogreeting ) if (nogreeting )
@ -659,8 +642,8 @@ main (int argc, char **argv )
if (greeting) if (greeting)
{ {
es_fprintf (es_stderr, "%s %s; %s\n", es_fprintf (es_stderr, "%s %s; %s\n",
strusage(11), strusage(13), strusage(14) ); gpgrt_strusage (11),gpgrt_strusage (13),gpgrt_strusage (14));
es_fprintf (es_stderr, "%s\n", strusage(15) ); es_fprintf (es_stderr, "%s\n", gpgrt_strusage (15));
} }
#ifdef IS_DEVELOPMENT_VERSION #ifdef IS_DEVELOPMENT_VERSION
log_info ("NOTE: this is a development version!\n"); log_info ("NOTE: this is a development version!\n");
@ -697,21 +680,14 @@ main (int argc, char **argv )
if (gpgconf_list) if (gpgconf_list)
{ {
/* List options and default values in the GPG Conf format. */ /* List options and default values in the GPG Conf format. */
char *filename = NULL;
char *filename_esc; char *filename_esc;
if (config_filename) filename_esc = percent_escape (config_filename, NULL);
filename = xstrdup (config_filename);
else
filename = make_filename (gnupg_homedir (),
SCDAEMON_NAME EXTSEP_S "conf", NULL);
filename_esc = percent_escape (filename, NULL);
es_printf ("%s-%s.conf:%lu:\"%s\n", es_printf ("%s-%s.conf:%lu:\"%s\n",
GPGCONF_NAME, SCDAEMON_NAME, GPGCONF_NAME, SCDAEMON_NAME,
GC_OPT_FLAG_DEFAULT, filename_esc); GC_OPT_FLAG_DEFAULT, filename_esc);
xfree (filename_esc); xfree (filename_esc);
xfree (filename);
es_printf ("verbose:%lu:\n" es_printf ("verbose:%lu:\n"
"quiet:%lu:\n" "quiet:%lu:\n"
@ -969,6 +945,7 @@ main (int argc, char **argv )
close (fd); close (fd);
} }
xfree (config_filename);
return 0; return 0;
} }
@ -1063,7 +1040,7 @@ handle_signal (int signo)
if (shutdown_pending > 2) if (shutdown_pending > 2)
{ {
log_info ("shutdown forced\n"); log_info ("shutdown forced\n");
log_info ("%s %s stopped\n", strusage(11), strusage(13) ); log_info ("%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
cleanup (); cleanup ();
scd_exit (0); scd_exit (0);
} }
@ -1071,7 +1048,7 @@ handle_signal (int signo)
case SIGINT: case SIGINT:
log_info ("SIGINT received - immediate shutdown\n"); log_info ("SIGINT received - immediate shutdown\n");
log_info( "%s %s stopped\n", strusage(11), strusage(13)); log_info( "%s %s stopped\n", gpgrt_strusage(11), gpgrt_strusage(13));
cleanup (); cleanup ();
scd_exit (0); scd_exit (0);
break; break;
@ -1455,7 +1432,7 @@ handle_connections (int listen_fd)
close (pipe_fd[1]); close (pipe_fd[1]);
#endif #endif
cleanup (); cleanup ();
log_info (_("%s %s stopped\n"), strusage(11), strusage(13)); log_info (_("%s %s stopped\n"), gpgrt_strusage(11), gpgrt_strusage(13));
npth_attr_destroy (&tattr); npth_attr_destroy (&tattr);
} }