diff options
Diffstat (limited to 'scd/scdaemon.c')
-rw-r--r-- | scd/scdaemon.c | 144 |
1 files changed, 61 insertions, 83 deletions
diff --git a/scd/scdaemon.c b/scd/scdaemon.c index c1a3f438b..5c519f8a0 100644 --- a/scd/scdaemon.c +++ b/scd/scdaemon.c @@ -16,6 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: GPL-3.0-or-later */ #include <config.h> @@ -121,7 +122,7 @@ static ARGPARSE_OPTS opts[] = { ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_n (oSh, "sh", N_("sh-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_n (oDebugAll, "debug-all", "@"), ARGPARSE_s_s (oDebugLevel, "debug-level" , @@ -161,6 +162,7 @@ static ARGPARSE_OPTS opts[] = { N_("use variable length input for pinpad")), ARGPARSE_s_s (oHomedir, "homedir", "@"), ARGPARSE_s_i (oListenBacklog, "listen-backlog", "@"), + ARGPARSE_noconffile (oNoOptions, "no-options", "@"), /* Stubs for options which are implemented by 2.3 or later. */ ARGPARSE_s_s (oNoop, "application-priority", "@"), @@ -287,9 +289,11 @@ my_strusage (int level) switch (level) { + case 9: p = "GPL-3.0-or-later"; break; case 11: p = "@SCDAEMON@ (@GNUPG@)"; break; case 13: p = VERSION; break; + case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; @@ -421,13 +425,11 @@ main (int argc, char **argv ) ARGPARSE_ARGS pargs; int orig_argc; char **orig_argv; - FILE *configfp = NULL; - char *configname = NULL; + char *last_configname = NULL; + const char *configname = NULL; const char *shell; - unsigned int configlineno; - int parse_debug = 0; + int debug_argparser = 0; const char *debug_level = NULL; - int default_config =1; int greeting = 0; int nogreeting = 0; int multi_server = 0; @@ -437,7 +439,7 @@ main (int argc, char **argv ) char *logfile = NULL; int debug_wait = 0; int gpgconf_list = 0; - const char *config_filename = NULL; + char *config_filename = NULL; int allow_coredump = 0; struct assuan_malloc_hooks malloc_hooks; int res; @@ -484,73 +486,61 @@ main (int argc, char **argv ) orig_argv = argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1|(1<<6); /* do not remove the args, ignore version */ - while (arg_parse( &pargs, opts)) + pargs.flags= (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION); + while (gnupg_argparse (NULL, &pargs, opts)) { - if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll) - 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) + switch (pargs.r_opt) + { + case oDebug: + case oDebugAll: + debug_argparser++; + break; + case oHomedir: 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); maybe_setuid = 0; /* - Now we are working under our real uid - */ - - - if (default_config) - configname = make_filename (gnupg_homedir (), SCDAEMON_NAME EXTSEP_S "conf", - NULL ); + * Now we are working under our real uid + */ + /* The configuraton directories for use by gpgrt_argparser. */ + gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ()); + gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ()); argc = orig_argc; argv = orig_argv; pargs.argc = &argc; pargs.argv = &argv; - pargs.flags= 1; /* do not remove the args */ - next_pass: - if (configname) + pargs.flags |= (ARGPARSE_FLAG_RESET + | ARGPARSE_FLAG_KEEP + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER); + while (gnupg_argparser (&pargs, opts, SCDAEMON_NAME EXTSEP_S "conf")) { - configlineno = 0; - configfp = gnupg_fopen (configname, "r"); - if (!configfp) + switch (pargs.r_opt) { - if (default_config) + 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) { - if( parse_debug ) - log_info (_("Note: no default option file '%s'\n"), - configname ); + xfree (last_configname); + last_configname = xstrdup (pargs.r.ret_str); + configname = last_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; - } + configname = NULL; + break; - while (optfile_parse( configfp, configname, &configlineno, &pargs, opts) ) - { - switch (pargs.r_opt) - { case aGPGConfList: gpgconf_list = 1; break; case aGPGConfTest: gpgconf_list = 2; break; case oQuiet: opt.quiet = 1; break; @@ -583,18 +573,8 @@ main (int argc, char **argv ) set_libassuan_log_cats (pargs.r.ret_ulong); 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 oNoVerbose: opt.verbose = 0; break; - case oNoOptions: break; /* no-options */ case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break; case oNoDetach: nodetach = 1; break; case oLogFile: logfile = pargs.r.ret_str; break; @@ -631,21 +611,25 @@ main (int argc, char **argv ) case oNoop: break; default: - pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR; + if (configname) + pargs.err = ARGPARSE_PRINT_WARNING; + else + pargs.err = ARGPARSE_PRINT_ERROR; break; } } - if (configfp) + gnupg_argparse (NULL, &pargs, NULL); /* Release internal state. */ + + if (!last_configname) + config_filename = make_filename (gnupg_homedir (), + SCDAEMON_NAME EXTSEP_S "conf", + NULL); + else { - fclose( configfp ); - configfp = NULL; - /* Keep a copy of the config name for use by --gpgconf-list. */ - config_filename = configname; - configname = NULL; - goto next_pass; + config_filename = last_configname; + last_configname = NULL; } - xfree (configname); - configname = NULL; + if (log_get_errorcount(0)) exit(2); if (nogreeting ) @@ -692,21 +676,13 @@ main (int argc, char **argv ) if (gpgconf_list) { /* List options and default values in the GPG Conf format. */ - char *filename = NULL; char *filename_esc; - if (config_filename) - filename = xstrdup (config_filename); - else - filename = make_filename (gnupg_homedir (), - SCDAEMON_NAME EXTSEP_S "conf", NULL); - filename_esc = percent_escape (filename, NULL); - + filename_esc = percent_escape (config_filename, NULL); es_printf ("%s-%s.conf:%lu:\"%s\n", GPGCONF_NAME, SCDAEMON_NAME, GC_OPT_FLAG_DEFAULT, filename_esc); xfree (filename_esc); - xfree (filename); es_printf ("verbose:%lu:\n" "quiet:%lu:\n" @@ -960,9 +936,11 @@ main (int argc, char **argv ) close (fd); } + xfree (config_filename); return 0; } + void scd_exit (int rc) { |