1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

gpgconf: Extend --show-config to show envvars.

* tools/gpgconf.c (my_copy_file): Add arg LISTP and record certain
things.
(show_configs_one_file): New arg LISTP to be passed thru.
(show_configs): Show envars and regisiry values.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2021-11-17 09:53:05 +01:00
parent 5053939480
commit 9172fbc084
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -1190,27 +1190,67 @@ show_versions (estream_t fp)
/* Copy data from file SRC to DST. Returns 0 on success or an error /* Copy data from file SRC to DST. Returns 0 on success or an error
* code on failure. */ * code on failure. If LISTP is not NULL, that strlist is updated
* with the variabale or registry key names detected. Flag bit 0
* indicates a registry entry. */
static gpg_error_t static gpg_error_t
my_copy_file (estream_t src, estream_t dst) my_copy_file (estream_t src, estream_t dst, strlist_t *listp)
{ {
#define BUF_LEN 4096 gpg_error_t err;
char buffer[BUF_LEN]; char *line = NULL;
int len; size_t line_len = 0;
ssize_t length;
int written; int written;
do while ((length = es_read_line (src, &line, &line_len, NULL)) > 0)
{ {
len = gpgrt_fread (buffer, 1, BUF_LEN, src); /* Strip newline and carriage return, if present. */
if (!len) written = gpgrt_fwrite (line, 1, length, dst);
break; if (written != length)
written = gpgrt_fwrite (buffer, 1, len, dst); return gpg_error_from_syserror ();
if (written != len) trim_spaces (line);
break; if (*line == '[' && listp)
} {
while (!gpgrt_feof (src) && !gpgrt_ferror (src) && !gpgrt_ferror (dst)); char **tokens;
char *p;
if (gpgrt_ferror (src) || gpgrt_ferror (dst) || !gpgrt_feof (src)) for (p=line+1; *p; p++)
if (*p != ' ' && *p != '\t')
break;
if (*p && p[strlen (p)-1] == ']')
p[strlen (p)-1] = 0;
tokens = strtokenize (p, " \t");
if (!tokens)
{
err = gpg_error_from_syserror ();
log_error ("strtokenize failed: %s\n", gpg_strerror (err));
return err;
}
/* Check whether we have a getreg or getenv statement and
* store the third token to later retrieval. */
if (tokens[0] && tokens[1] && tokens[2]
&& (!strcmp (tokens[0], "getreg")
|| !strcmp (tokens[0], "getenv")))
{
int isreg = (tokens[0][3] == 'r');
strlist_t sl = *listp;
for (sl = *listp; sl; sl = sl->next)
if (!strcmp (sl->d, tokens[2]) && (sl->flags & 1) == isreg)
break;
if (!sl) /* Not yet in the respective list. */
{
sl = add_to_strlist (listp, tokens[2]);
if (isreg)
sl->flags = 1;
}
}
xfree (tokens);
}
}
if (length < 0 || es_ferror (src))
return gpg_error_from_syserror (); return gpg_error_from_syserror ();
if (gpgrt_fflush (dst)) if (gpgrt_fflush (dst))
@ -1222,7 +1262,8 @@ my_copy_file (estream_t src, estream_t dst)
/* Helper for show_configs */ /* Helper for show_configs */
static void static void
show_configs_one_file (const char *fname, int global, estream_t outfp) show_configs_one_file (const char *fname, int global, estream_t outfp,
strlist_t *listp)
{ {
gpg_error_t err; gpg_error_t err;
estream_t fp; estream_t fp;
@ -1241,7 +1282,7 @@ show_configs_one_file (const char *fname, int global, estream_t outfp)
es_fprintf (outfp, "###\n### %s config \"%s\"\n###\n", es_fprintf (outfp, "###\n### %s config \"%s\"\n###\n",
global? "global":"local", fname); global? "global":"local", fname);
es_fprintf (outfp, CUTLINE_FMT, "start"); es_fprintf (outfp, CUTLINE_FMT, "start");
err = my_copy_file (fp, outfp); err = my_copy_file (fp, outfp, listp);
if (err) if (err)
log_error ("error copying file \"%s\": %s\n", log_error ("error copying file \"%s\": %s\n",
fname, gpg_strerror (err)); fname, gpg_strerror (err));
@ -1265,6 +1306,7 @@ show_configs (estream_t outfp)
gnupg_dirent_t dir_entry; gnupg_dirent_t dir_entry;
size_t n; size_t n;
int any; int any;
strlist_t list = NULL;
es_fprintf (outfp, "### Dump of all standard config files\n"); es_fprintf (outfp, "### Dump of all standard config files\n");
show_version_gnupg (outfp, "### "); show_version_gnupg (outfp, "### ");
@ -1275,14 +1317,65 @@ show_configs (estream_t outfp)
for (idx = 0; idx < DIM (names); idx++) for (idx = 0; idx < DIM (names); idx++)
{ {
fname = make_filename (gnupg_sysconfdir (), names[idx], NULL); fname = make_filename (gnupg_sysconfdir (), names[idx], NULL);
show_configs_one_file (fname, 1, outfp); show_configs_one_file (fname, 1, outfp, &list);
xfree (fname); xfree (fname);
fname = make_filename (gnupg_homedir (), names[idx], NULL); fname = make_filename (gnupg_homedir (), names[idx], NULL);
show_configs_one_file (fname, 0, outfp); show_configs_one_file (fname, 0, outfp, &list);
xfree (fname); xfree (fname);
es_fprintf (outfp, "\n"); es_fprintf (outfp, "\n");
} }
/* Print the encountered registry values and envvars. */
if (list)
{
strlist_t sl;
const char *s;
any = 0;
for (sl = list; sl; sl = sl->next)
if (!(sl->flags & 1))
{
if (!any)
{
any = 1;
es_fprintf (outfp,
"###\n"
"### List of encountered environment variables:\n");
}
if ((s = getenv (sl->d)))
es_fprintf (outfp, "### %-12s ->%s<-\n", sl->d, s);
else
es_fprintf (outfp, "### %-12s [not set]\n", sl->d);
}
if (any)
es_fprintf (outfp, "###\n");
#ifdef HAVE_W32_SYSTEM
any = 0;
for (sl = list; sl; sl = sl->next)
if ((sl->flags & 1))
{
char *p;
if (!any)
{
any = 1;
es_fprintf (outfp,
"###\n"
"### List of encountered registry entries:\n");
}
if ((p = read_w32_reg_string (sl->d)))
es_fprintf (outfp, "### %s ->%s<-\n", sl->d, p);
else
es_fprintf (outfp, "### %s [not set]\n", sl->d);
xfree (p);
}
if (any)
es_fprintf (outfp, "###\n");
#endif
free_strlist (list);
}
/* Check for uncommon files in the home directory. */ /* Check for uncommon files in the home directory. */
dir = gnupg_opendir (gnupg_homedir ()); dir = gnupg_opendir (gnupg_homedir ());
if (!dir) if (!dir)