summaryrefslogtreecommitdiffstats
path: root/tools/gpgconf.c
blob: 1b3f2bebaeccb096ab8a34c2ef4ef72f2eda145c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
/* gpgconf.c - Configuration utility for GnuPG
 * Copyright (C) 2003, 2007, 2009, 2011 Free Software Foundation, Inc.
 * Copyright (C) 2016, 2020 g10 Code GmbH.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * 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
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define INCLUDED_BY_MAIN_MODULE 1
#include "gpgconf.h"
#include "../common/i18n.h"
#include "../common/sysutils.h"
#include "../common/init.h"
#include "../common/status.h"
#include "../common/exechelp.h"


/* Constants to identify the commands and options. */
enum cmd_and_opt_values
  {
    aNull = 0,
    oDryRun	= 'n',
    oOutput	= 'o',
    oQuiet      = 'q',
    oVerbose	= 'v',
    oRuntime    = 'r',
    oComponent  = 'c',
    oNull       = '0',
    aListDirs   = 'L',
    aKill       = 'K',
    aReload     = 'R',
    aShowVersions = 'V',
    aShowConfigs  = 'X',

    oNoVerbose	= 500,
    oHomedir,
    oBuilddir,
    oStatusFD,
    oShowSocket,

    aListComponents,
    aCheckPrograms,
    aListOptions,
    aChangeOptions,
    aCheckOptions,
    aApplyDefaults,
    aListConfig,
    aCheckConfig,
    aQuerySWDB,
    aLaunch,
    aCreateSocketDir,
    aRemoveSocketDir,
    aApplyProfile
  };


/* The list of commands and options. */
static ARGPARSE_OPTS opts[] =
  {
    { 300, NULL, 0, N_("@Commands:\n ") },

    { aListComponents, "list-components", 256, N_("list all components") },
    { aCheckPrograms, "check-programs", 256, N_("check all programs") },
    { aListOptions, "list-options", 256, N_("|COMPONENT|list options") },
    { aChangeOptions, "change-options", 256, N_("|COMPONENT|change options") },
    { aCheckOptions, "check-options", 256, N_("|COMPONENT|check options") },
    { aApplyDefaults, "apply-defaults", 256,
      N_("apply global default values") },
    { aApplyProfile, "apply-profile", 256,
      N_("|FILE|update configuration files using FILE") },
    { aListDirs, "list-dirs", 256,
      N_("get the configuration directories for @GPGCONF@") },
    { aListConfig,   "list-config", 256,
      N_("list global configuration file") },
    { aCheckConfig,   "check-config", 256,
      N_("check global configuration file") },
    { aQuerySWDB,     "query-swdb", 256,
      N_("query the software version database") },
    { aReload,        "reload", 256, N_("reload all or a given component")},
    { aLaunch,        "launch", 256, N_("launch a given component")},
    { aKill,          "kill", 256,   N_("kill a given component")},
    { aCreateSocketDir, "create-socketdir", 256, "@"},
    { aRemoveSocketDir, "remove-socketdir", 256, "@"},
    ARGPARSE_c (aShowVersions, "show-versions", ""),
    ARGPARSE_c (aShowConfigs,  "show-configs", ""),

    { 301, NULL, 0, N_("@\nOptions:\n ") },

    { oOutput, "output",    2, N_("use as output file") },
    { oVerbose, "verbose",  0, N_("verbose") },
    { oQuiet, "quiet",      0, N_("quiet") },
    { oDryRun, "dry-run",   0, N_("do not make any changes") },
    { oRuntime, "runtime",  0, N_("activate changes at runtime, if possible") },
  ARGPARSE_s_i (oStatusFD, "status-fd", N_("|FD|write status info to this FD")),
    /* hidden options */
    { oHomedir, "homedir", 2, "@" },
    { oBuilddir, "build-prefix", 2, "@" },
    { oNull, "null", 0, "@" },
    { oNoVerbose, "no-verbose",  0, "@"},
    ARGPARSE_s_n (oShowSocket, "show-socket", "@"),

    ARGPARSE_end(),
  };



#define CUTLINE_FMT \
  "--8<---------------cut here---------------%s------------->8---\n"


/* The stream to output the status information.  Status Output is disabled if
 * this is NULL.  */
static estream_t statusfp;

static void show_versions (estream_t fp);
static void show_configs (estream_t fp);



/* Print usage information and provide strings for help. */
static const char *
my_strusage( int level )
{
  const char *p;

  switch (level)
    {
    case  9: p = "GPL-3.0-or-later"; break;
    case 11: p = "@GPGCONF@ (@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;

    case 1:
    case 40: p = _("Usage: @GPGCONF@ [options] (-h for help)");
      break;
    case 41:
      p = _("Syntax: @GPGCONF@ [options]\n"
            "Manage configuration options for tools of the @GNUPG@ system\n");
      break;

    default: p = NULL; break;
    }
  return p;
}


/* Return the fp for the output.  This is usually stdout unless
   --output has been used.  In the latter case this function opens
   that file.  */
static estream_t
get_outfp (estream_t *fp)
{
  if (!*fp)
    {
      if (opt.outfile)
        {
          *fp = es_fopen (opt.outfile, "w");
          if (!*fp)
            gc_error (1, errno, "can not open '%s'", opt.outfile);
        }
      else
        *fp = es_stdout;
    }
  return *fp;
}


/* Set the status FD.  */
static void
set_status_fd (int fd)
{
  static int last_fd = -1;

  if (fd != -1 && last_fd == fd)
    return;

  if (statusfp && statusfp != es_stdout && statusfp != es_stderr)
    es_fclose (statusfp);
  statusfp = NULL;
  if (fd == -1)
    return;

  if (fd == 1)
    statusfp = es_stdout;
  else if (fd == 2)
    statusfp = es_stderr;
  else
    statusfp = es_fdopen (fd, "w");
  if (!statusfp)
    {
      log_fatal ("can't open fd %d for status output: %s\n",
                 fd, gpg_strerror (gpg_error_from_syserror ()));
    }
  last_fd = fd;
}


/* Write a status line with code NO followed by the output of the
 * printf style FORMAT.  The caller needs to make sure that LFs and
 * CRs are not printed.  */
void
gpgconf_write_status (int no, const char *format, ...)
{
  va_list arg_ptr;

  if (!statusfp)
    return;  /* Not enabled.  */

  es_fputs ("[GNUPG:] ", statusfp);
  es_fputs (get_status_string (no), statusfp);
  if (format)
    {
      es_putc (' ', statusfp);
      va_start (arg_ptr, format);
      es_vfprintf (statusfp, format, arg_ptr);
      va_end (arg_ptr);
    }
  es_putc ('\n', statusfp);
}


static void
list_dirs (estream_t fp, char **names, int special)
{
  static struct {
    const char *name;
    const char *(*fnc)(void);
    const char *extra;
  } list[] = {
    { "sysconfdir",         gnupg_sysconfdir, NULL },
    { "bindir",             gnupg_bindir,     NULL },
    { "libexecdir",         gnupg_libexecdir, NULL },
    { "libdir",             gnupg_libdir,     NULL },
    { "datadir",            gnupg_datadir,    NULL },
    { "localedir",          gnupg_localedir,  NULL },
    { "socketdir",          gnupg_socketdir,  NULL },
    { "dirmngr-socket",     dirmngr_socket_name, NULL,},
    { "agent-ssh-socket",   gnupg_socketdir,  GPG_AGENT_SSH_SOCK_NAME },
    { "agent-extra-socket", gnupg_socketdir,  GPG_AGENT_EXTRA_SOCK_NAME },
    { "agent-browser-socket",gnupg_socketdir, GPG_AGENT_BROWSER_SOCK_NAME },
    { "agent-socket",       gnupg_socketdir,  GPG_AGENT_SOCK_NAME },
    { "homedir",            gnupg_homedir,    NULL }
  };
  int idx, j;
  char *tmp;
  const char *s;


  for (idx = 0; idx < DIM (list); idx++)
    {
      s = list[idx].fnc ();
      if (list[idx].extra)
        {
          tmp = make_filename (s, list[idx].extra, NULL);
          s = tmp;
        }
      else
        tmp = NULL;
      if (!names)
        es_fprintf (fp, "%s:%s\n", list[idx].name, gc_percent_escape (s));
      else
        {
          for (j=0; names[j]; j++)
            if (!strcmp (names[j], list[idx].name))
              {
                es_fputs (s, fp);
                es_putc (opt.null? '\0':'\n', fp);
              }
        }

      xfree (tmp);
    }


#ifdef HAVE_W32_SYSTEM
  tmp = read_w32_registry_string (NULL,
                                  GNUPG_REGISTRY_DIR,
                                  "HomeDir");
  if (tmp)
    {
      int hkcu = 0;
      int hklm = 0;

      xfree (tmp);
      if ((tmp = read_w32_registry_string ("HKEY_CURRENT_USER",
                                           GNUPG_REGISTRY_DIR,
                                           "HomeDir")))
        {
          xfree (tmp);
          hkcu = 1;
        }
      if ((tmp = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
                                           GNUPG_REGISTRY_DIR,
                                           "HomeDir")))
        {
          xfree (tmp);
          hklm = 1;
        }

      es_fflush (fp);
      if (special)
        es_fprintf (fp, "\n"
                    "### Note: homedir taken from registry key %s%s\\%s:%s\n"
                    "\n",
                    hkcu?" HKCU":"", hklm?" HKLM":"",
                    GNUPG_REGISTRY_DIR, "HomeDir");
      else
        log_info ("Warning: homedir taken from registry key (%s:%s) in%s%s\n",
                  GNUPG_REGISTRY_DIR, "HomeDir",
                  hkcu?" HKCU":"",
                  hklm?" HKLM":"");
    }
  else if ((tmp = read_w32_registry_string (NULL,
                                            GNUPG_REGISTRY_DIR,
                                            NULL)))
    {
      xfree (tmp);
      es_fflush (fp);
      if (special)
        es_fprintf (fp, "\n"
                    "### Note: registry key %s without value in HKCU or HKLM\n"
                    "\n", GNUPG_REGISTRY_DIR);
      else
        log_info ("Warning: registry key (%s) without value in HKCU or HKLM\n",
                  GNUPG_REGISTRY_DIR);
    }

#else /*!HAVE_W32_SYSTEM*/
  (void)special;
#endif /*!HAVE_W32_SYSTEM*/
}



/* Check whether NAME is valid argument for query_swdb().  Valid names
 * start with a letter and contain only alphanumeric characters or an
 * underscore.  */
static int
valid_swdb_name_p (const char *name)
{
  if (!name || !*name || !alphap (name))
    return 0;

  for (name++; *name; name++)
    if (!alnump (name) && *name != '_')
      return 0;

  return 1;
}


/* Query the SWDB file.  If necessary and possible this functions asks
 * the dirmngr to load an updated version of that file.  The caller
 * needs to provide the NAME to query (e.g. "gnupg", "libgcrypt") and
 * optional the currently installed version in CURRENT_VERSION.  The
 * output written to OUT is a colon delimited line with these fields:
 *
 * name   :: The name of the package
 * curvers:: The installed version if given.
 * status :: This value tells the status of the software package
 *           '-' :: No information available
 *                  (error or CURRENT_VERSION not given)
 *           '?' :: Unknown NAME
 *           'u' :: Update available
 *           'c' :: The version is Current
 *           'n' :: The current version is already Newer than the
 *                  available one.
 * urgency :: If the value is greater than zero an urgent update is required.
 * error   :: 0 on success or an gpg_err_code_t
 *            Common codes seen:
 *            GPG_ERR_TOO_OLD :: The SWDB file is to old to be used.
 *            GPG_ERR_ENOENT  :: The SWDB file is not available.
 *            GPG_ERR_BAD_SIGNATURE :: Currupted SWDB file.
 * filedate:: Date of the swdb file (yyyymmddThhmmss)
 * verified:: Date we checked the validity of the file (yyyyymmddThhmmss)
 * version :: The version string from the swdb.
 * reldate :: Release date of that version (yyyymmddThhmmss)
 * size    :: Size of the package in bytes.
 * hash    :: SHA-2 hash of the package.
 *
 */
static void
query_swdb (estream_t out, const char *name, const char *current_version)
{
  gpg_error_t err;
  const char *search_name;
  char *fname = NULL;
  estream_t fp = NULL;
  char *line = NULL;
  char *self_version = NULL;
  size_t length_of_line = 0;
  size_t  maxlen;
  ssize_t len;
  char *fields[2];
  char *p;
  gnupg_isotime_t filedate = {0};
  gnupg_isotime_t verified = {0};
  char *value_ver = NULL;
  gnupg_isotime_t value_date = {0};
  char *value_size = NULL;
  char *value_sha2 = NULL;
  unsigned long value_size_ul = 0;
  int status, i;


  if (!valid_swdb_name_p (name))
    {
      log_error ("error in package name '%s': %s\n",
                 name, gpg_strerror (GPG_ERR_INV_NAME));
      goto leave;
    }
  if (!strcmp (name, "gnupg"))
    search_name = GNUPG_SWDB_TAG;
  else if (!strcmp (name, "gnupg1"))
    search_name = "gnupg1";
  else
    search_name = name;

  if (!current_version && !strcmp (name, "gnupg"))
    {
      /* Use our own version but string a possible beta string.  */
      self_version = xstrdup (PACKAGE_VERSION);
      p = strchr (self_version, '-');
      if (p)
        *p = 0;
      current_version = self_version;
    }

  if (current_version && (strchr (current_version, ':')
                          || compare_version_strings (current_version, NULL)))
    {
      log_error ("error in version string '%s': %s\n",
                 current_version, gpg_strerror (GPG_ERR_INV_ARG));
      goto leave;
    }

  fname = make_filename (gnupg_homedir (), "swdb.lst", NULL);
  fp = es_fopen (fname, "r");
  if (!fp)
    {
      err = gpg_error_from_syserror ();
      es_fprintf (out, "%s:%s:-::%u:::::::\n",
                  name,
                  current_version? current_version : "",
                  gpg_err_code (err));
      if (gpg_err_code (err) != GPG_ERR_ENOENT)
        log_error (_("error opening '%s': %s\n"), fname, gpg_strerror (err));
      goto leave;
    }

  /* Note that the parser uses the first occurrence of a matching
   * values and ignores possible duplicated values.  */

  maxlen = 2048; /* Set limit.  */
  while ((len = es_read_line (fp, &line, &length_of_line, &maxlen)) > 0)
    {
      if (!maxlen)
        {
          err = gpg_error (GPG_ERR_LINE_TOO_LONG);
          log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err));
          goto leave;
        }
      /* Strip newline and carriage return, if present.  */
      while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
	line[--len] = '\0';

      if (split_fields (line, fields, DIM (fields)) < DIM(fields))
        continue; /* Skip empty lines and names w/o a value.  */
      if (*fields[0] == '#')
        continue; /* Skip comments.  */

      /* Record the meta data.  */
      if (!*filedate && !strcmp (fields[0], ".filedate"))
        {
          string2isotime (filedate, fields[1]);
          continue;
        }
      if (!*verified && !strcmp (fields[0], ".verified"))
        {
          string2isotime (verified, fields[1]);
          continue;
        }

      /* Tokenize the name.  */
      p = strrchr (fields[0], '_');
      if (!p)
        continue; /* Name w/o an underscore.  */
      *p++ = 0;

      /* Wait for the requested name.  */
      if (!strcmp (fields[0], search_name))
        {
          if (!strcmp (p, "ver") && !value_ver)
            value_ver = xstrdup (fields[1]);
          else if (!strcmp (p, "date") && !*value_date)
            string2isotime (value_date, fields[1]);
          else if (!strcmp (p, "size") && !value_size)
            value_size = xstrdup (fields[1]);
          else if (!strcmp (p, "sha2") && !value_sha2)
            value_sha2 = xstrdup (fields[1]);
        }
    }
  if (len < 0 || es_ferror (fp))
    {
      err = gpg_error_from_syserror ();
      log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err));
      goto leave;
    }

  if (!*filedate || !*verified)
    {
      err = gpg_error (GPG_ERR_INV_TIME);
      es_fprintf (out, "%s:%s:-::%u:::::::\n",
                  name,
                  current_version? current_version : "",
                  gpg_err_code (err));
      goto leave;
    }

  if (!value_ver)
    {
      es_fprintf (out, "%s:%s:?:::::::::\n",
                  name,
                  current_version? current_version : "");
      goto leave;
    }

  if (value_size)
    {
      gpg_err_set_errno (0);
      value_size_ul = strtoul (value_size, &p, 10);
      if (errno)
        value_size_ul = 0;
      else if (*p == 'k')
        value_size_ul *= 1024;
    }

  err = 0;
  status = '-';
  if (compare_version_strings (value_ver, NULL))
    err = gpg_error (GPG_ERR_INV_VALUE);
  else if (!current_version)
    ;
  else if (!(i = compare_version_strings (value_ver, current_version)))
    status = 'c';
  else if (i > 0)
    status = 'u';
  else
    status = 'n';

  es_fprintf (out, "%s:%s:%c::%d:%s:%s:%s:%s:%lu:%s:\n",
              name,
              current_version? current_version : "",
              status,
              err,
              filedate,
              verified,
              value_ver,
              value_date,
              value_size_ul,
              value_sha2? value_sha2 : "");

 leave:
  xfree (value_ver);
  xfree (value_size);
  xfree (value_sha2);
  xfree (line);
  es_fclose (fp);
  xfree (fname);
  xfree (self_version);
}


/* gpgconf main. */
int
main (int argc, char **argv)
{
  gpg_error_t err;
  ARGPARSE_ARGS pargs;
  const char *fname;
  int no_more_options = 0;
  enum cmd_and_opt_values cmd = 0;
  estream_t outfp = NULL;
  int show_socket = 0;

  early_system_init ();
  gnupg_reopen_std (GPGCONF_NAME);
  set_strusage (my_strusage);
  log_set_prefix (GPGCONF_NAME, GPGRT_LOG_WITH_PREFIX|GPGRT_LOG_NO_REGISTRY);

  /* Make sure that our subsystems are ready.  */
  i18n_init();
  init_common_subsystems (&argc, &argv);
  gc_components_init ();

  /* Parse the command line. */
  pargs.argc  = &argc;
  pargs.argv  = &argv;
  pargs.flags = ARGPARSE_FLAG_KEEP;
  while (!no_more_options && gnupg_argparse (NULL, &pargs, opts))
    {
      switch (pargs.r_opt)
        {
        case oOutput:    opt.outfile = pargs.r.ret_str; break;
	case oQuiet:     opt.quiet = 1; break;
        case oDryRun:    opt.dry_run = 1; break;
        case oRuntime:   opt.runtime = 1; break;
        case oVerbose:   opt.verbose++; break;
        case oNoVerbose: opt.verbose = 0; break;
        case oHomedir:   gnupg_set_homedir (pargs.r.ret_str); break;
        case oBuilddir:  gnupg_set_builddir (pargs.r.ret_str); break;
        case oNull:      opt.null = 1; break;
        case oStatusFD:
          set_status_fd (translate_sys2libc_fd_int (pargs.r.ret_int, 1));
          break;
        case oShowSocket: show_socket = 1; break;

	case aListDirs:
        case aListComponents:
        case aCheckPrograms:
        case aListOptions:
        case aChangeOptions:
        case aCheckOptions:
        case aApplyDefaults:
        case aApplyProfile:
        case aListConfig:
        case aCheckConfig:
        case aQuerySWDB:
        case aReload:
        case aLaunch:
        case aKill:
        case aCreateSocketDir:
        case aRemoveSocketDir:
        case aShowVersions:
        case aShowConfigs:
	  cmd = pargs.r_opt;
	  break;

        default: pargs.err = ARGPARSE_PRINT_ERROR; break;
	}
    }
  gnupg_argparse (NULL, &pargs, NULL);  /* Release internal state.  */

  if (log_get_errorcount (0))
    gpgconf_failure (GPG_ERR_USER_2);

  /* Print a warning if an argument looks like an option.  */
  if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN))
    {
      int i;

      for (i=0; i < argc; i++)
        if (argv[i][0] == '-' && argv[i][1] == '-')
          log_info (_("Note: '%s' is not considered an option\n"), argv[i]);
    }

  fname = argc ? *argv : NULL;

  /* Set the configuraton directories for use by gpgrt_argparser.  We
   * don't have a configuration file for this program but we have code
   * which reads the component's config files.  */
  gnupg_set_confdir (GNUPG_CONFDIR_SYS, gnupg_sysconfdir ());
  gnupg_set_confdir (GNUPG_CONFDIR_USER, gnupg_homedir ());

  switch (cmd)
    {
    case aListComponents:
    default:
      /* List all components. */
      gc_component_list_components (get_outfp (&outfp));
      break;

    case aCheckPrograms:
      /* Check all programs. */
      gc_check_programs (get_outfp (&outfp));
      break;

    case aListOptions:
    case aChangeOptions:
    case aCheckOptions:
      if (!fname)
	{
	  es_fprintf (es_stderr, _("usage: %s [options] "), GPGCONF_NAME);
	  es_putc ('\n', es_stderr);
	  es_fputs (_("Need one component argument"), es_stderr);
	  es_putc ('\n', es_stderr);
	  gpgconf_failure (GPG_ERR_USER_2);
	}
      else
	{
	  int idx = gc_component_find (fname);
	  if (idx < 0)
	    {
	      es_fputs (_("Component not found"), es_stderr);
	      es_putc ('\n', es_stderr);
	      gpgconf_failure (0);
	    }
          if (cmd == aCheckOptions)
	    gc_component_check_options (idx, get_outfp (&outfp), NULL);
          else
            {
              gc_component_retrieve_options (idx);
              if (gc_process_gpgconf_conf (NULL, 1, 0, NULL))
                gpgconf_failure (0);
              if (cmd == aListOptions)
                gc_component_list_options (idx, get_outfp (&outfp));
              else if (cmd == aChangeOptions)
                gc_component_change_options (idx, es_stdin,
                                             get_outfp (&outfp), 0);
            }
	}
      break;

    case aLaunch:
    case aKill:
      if (!fname)
	{
	  es_fprintf (es_stderr, _("usage: %s [options] "), GPGCONF_NAME);
	  es_putc ('\n', es_stderr);
	  es_fputs (_("Need one component argument"), es_stderr);
	  es_putc ('\n', es_stderr);
	  gpgconf_failure (GPG_ERR_USER_2);
	}
      else if (!strcmp (fname, "all"))
        {
          if (cmd == aLaunch)
            {
              if (gc_component_launch (-1))
                gpgconf_failure (0);
            }
          else
            {
              gc_component_kill (-1);
            }
        }
      else
        {
          /* Launch/Kill a given component.  */
          int idx;

          idx = gc_component_find (fname);
          if (idx < 0)
            {
              es_fputs (_("Component not found"), es_stderr);
              es_putc ('\n', es_stderr);
              gpgconf_failure (0);
            }
          else if (cmd == aLaunch)
            {
              err = gc_component_launch (idx);
              if (show_socket)
                {
                  char *names[2];

                  if (idx == GC_COMPONENT_GPG_AGENT)
                    names[0] = "agent-socket";
                  else if (idx == GC_COMPONENT_DIRMNGR)
                    names[0] = "dirmngr-socket";
                  else
                    names[0] = NULL;
                  names[1] = NULL;
                  get_outfp (&outfp);
                  list_dirs (outfp, names, 0);
                }
              if (err)
                gpgconf_failure (0);
            }
          else
            {
              /* We don't error out if the kill failed because this
                 command should do nothing if the component is not
                 running.  */
              gc_component_kill (idx);
            }
        }
      break;

    case aReload:
      if (!fname || !strcmp (fname, "all"))
	{
          /* Reload all.  */
          gc_component_reload (-1);
	}
      else
        {
          /* Reload given component.  */
          int idx;

          idx = gc_component_find (fname);
          if (idx < 0)
            {
              es_fputs (_("Component not found"), es_stderr);
              es_putc ('\n', es_stderr);
              gpgconf_failure (0);
            }
          else
            {
              gc_component_reload (idx);
            }
        }
      break;

    case aListConfig:
      if (gc_process_gpgconf_conf (fname, 0, 0, get_outfp (&outfp)))
        gpgconf_failure (0);
      break;

    case aCheckConfig:
      if (gc_process_gpgconf_conf (fname, 0, 0, NULL))
        gpgconf_failure (0);
      break;

    case aApplyDefaults:
      if (fname)
	{
	  es_fprintf (es_stderr, _("usage: %s [options] "), GPGCONF_NAME);
	  es_putc ('\n', es_stderr);
	  es_fputs (_("No argument allowed"), es_stderr);
	  es_putc ('\n', es_stderr);
	  gpgconf_failure (GPG_ERR_USER_2);
	}
      if (!opt.dry_run && gnupg_access (gnupg_homedir (), F_OK))
        gnupg_maybe_make_homedir (gnupg_homedir (), opt.quiet);
      gc_component_retrieve_options (-1);
      if (gc_process_gpgconf_conf (NULL, 1, 1, NULL))
        gpgconf_failure (0);
      break;

    case aApplyProfile:
      if (!opt.dry_run && gnupg_access (gnupg_homedir (), F_OK))
        gnupg_maybe_make_homedir (gnupg_homedir (), opt.quiet);
      gc_component_retrieve_options (-1);
      if (gc_apply_profile (fname))
        gpgconf_failure (0);
      break;

    case aListDirs:
      /* Show the system configuration directories for gpgconf.  */
      get_outfp (&outfp);
      list_dirs (outfp, argc? argv : NULL, 0);
      break;

    case aQuerySWDB:
      /* Query the software version database.  */
      if (!fname || argc > 2)
	{
	  es_fprintf (es_stderr, "usage: %s --query-swdb NAME [VERSION]\n",
                      GPGCONF_NAME);
	  gpgconf_failure (GPG_ERR_USER_2);
	}
      get_outfp (&outfp);
      query_swdb (outfp, fname, argc > 1? argv[1] : NULL);
      break;

    case aCreateSocketDir:
      {
        char *socketdir;
        unsigned int flags;

        /* Make sure that the top /run/user/UID/gnupg dir has been
         * created.  */
        gnupg_socketdir ();

        /* Check the /var/run dir.  */
        socketdir = _gnupg_socketdir_internal (1, &flags);
        if ((flags & 64) && !opt.dry_run)
          {
            /* No sub dir - create it. */
            if (gnupg_mkdir (socketdir, "-rwx"))
              gc_error (1, errno, "error creating '%s'", socketdir);
            /* Try again.  */
            xfree (socketdir);
            socketdir = _gnupg_socketdir_internal (1, &flags);
          }

        /* Give some info.  */
        if ( (flags & ~32) || opt.verbose || opt.dry_run)
          {
            log_info ("socketdir is '%s'\n", socketdir);
            if ((flags &   1)) log_info ("\tgeneral error\n");
            if ((flags &   2)) log_info ("\tno /run/user dir\n");
            if ((flags &   4)) log_info ("\tbad permissions\n");
            if ((flags &   8)) log_info ("\tbad permissions (subdir)\n");
            if ((flags &  16)) log_info ("\tmkdir failed\n");
            if ((flags &  32)) log_info ("\tnon-default homedir\n");
            if ((flags &  64)) log_info ("\tno such subdir\n");
            if ((flags & 128)) log_info ("\tusing homedir as fallback\n");
          }

        if ((flags & ~32) && !opt.dry_run)
          gc_error (1, 0, "error creating socket directory");

        xfree (socketdir);
      }
      break;

    case aRemoveSocketDir:
      {
        char *socketdir;
        unsigned int flags;

        /* Check the /var/run dir.  */
        socketdir = _gnupg_socketdir_internal (1, &flags);
        if ((flags & 128))
          log_info ("ignoring request to remove non /run/user socket dir\n");
        else if (opt.dry_run)
          ;
        else if (gnupg_rmdir (socketdir))
          {
            /* If the director is not empty we first try to delet
             * socket files.  */
            err = gpg_error_from_syserror ();
            if (gpg_err_code (err) == GPG_ERR_ENOTEMPTY
                || gpg_err_code (err) == GPG_ERR_EEXIST)
              {
                static const char * const names[] = {
                  GPG_AGENT_SOCK_NAME,
                  GPG_AGENT_EXTRA_SOCK_NAME,
                  GPG_AGENT_BROWSER_SOCK_NAME,
                  GPG_AGENT_SSH_SOCK_NAME,
                  SCDAEMON_SOCK_NAME,
                  DIRMNGR_SOCK_NAME
                };
                int i;
                char *p;

                for (i=0; i < DIM(names); i++)
                  {
                    p = strconcat (socketdir , "/", names[i], NULL);
                    if (p)
                      gnupg_remove (p);
                    xfree (p);
                  }
                if (gnupg_rmdir (socketdir))
                  gc_error (1, 0, "error removing '%s': %s",
                            socketdir, gpg_strerror (err));
              }
            else if (gpg_err_code (err) == GPG_ERR_ENOENT)
              gc_error (0, 0, "warning: removing '%s' failed: %s",
                        socketdir, gpg_strerror (err));
            else
              gc_error (1, 0, "error removing '%s': %s",
                        socketdir, gpg_strerror (err));
          }

        xfree (socketdir);
      }
      break;

    case aShowVersions:
      {
        get_outfp (&outfp);
        show_versions (outfp);
      }
      break;

    case aShowConfigs:
      {
        get_outfp (&outfp);
        show_configs (outfp);
      }
      break;

    }

  if (outfp != es_stdout)
    if (es_fclose (outfp))
      gc_error (1, errno, "error closing '%s'", opt.outfile);


  if (log_get_errorcount (0))
    gpgconf_failure (0);
  else
    gpgconf_write_status (STATUS_SUCCESS, NULL);
  return 0;
}


void
gpgconf_failure (gpg_error_t err)
{
  log_flush ();
  if (!err)
    err = gpg_error (GPG_ERR_GENERAL);
  gpgconf_write_status
    (STATUS_FAILURE, "- %u",
     gpg_err_code (err) == GPG_ERR_USER_2? GPG_ERR_EINVAL : err);
  exit (gpg_err_code (err) == GPG_ERR_USER_2? 2 : 1);
}



/* Parse the revision part from the extended version blurb.  */
static const char *
get_revision_from_blurb (const char *blurb, int *r_len)
{
  const char *s = blurb? blurb : "";
  int n;

  for (; *s; s++)
    if (*s == '\n' && s[1] == '(')
      break;
  if (s)
    {
      s += 2;
      for (n=0; s[n] && s[n] != ' '; n++)
        ;
    }
  else
    {
      s = "?";
      n = 1;
    }
  *r_len = n;
  return s;
}


static void
show_version_gnupg (estream_t fp, const char *prefix)
{
  char *fname, *p;
  size_t n;
  estream_t verfp;
  char line[100];

  es_fprintf (fp, "%s%sGnuPG %s (%s)\n%s%s\n", prefix, *prefix?"":"* ",
              strusage (13), BUILD_REVISION, prefix, strusage (17));

  /* Show the GnuPG VS-Desktop version in --show-configs mode  */
  if (prefix && *prefix == '#')
    {
      fname = make_filename (gnupg_bindir (), NULL);
      n = strlen (fname);
      if (n > 10 && (!ascii_strcasecmp (fname + n - 10, "/GnuPG/bin")
                     || !ascii_strcasecmp (fname + n - 10, "\\GnuPG\\bin")))
        {
          /* Append VERSION to the ../../ direcory.  Note that VERSION
           * is only 7 bytes and thus fits.  */
          strcpy (fname + n - 9, "VERSION");
          verfp = es_fopen (fname, "r");
          if (!verfp)
            es_fprintf (fp, "%s[VERSION file not found]\n", prefix);
          else if (!es_fgets (line, sizeof line, verfp))
            es_fprintf (fp, "%s[VERSION file is empty]\n", prefix);
          else
            {
              trim_spaces (line);
              for (p=line; *p; p++)
                if (*p < ' ' || *p > '~' || *p == '[')
                  *p = '?';
              es_fprintf (fp, "%s%s\n", prefix, line);
            }
          es_fclose (verfp);
        }
      xfree (fname);
    }

#ifdef HAVE_W32_SYSTEM
  {
    OSVERSIONINFO osvi = { sizeof (osvi) };

    GetVersionEx (&osvi);
    es_fprintf (fp, "%sWindows %lu.%lu build %lu%s%s%s\n",
                prefix,
                (unsigned long)osvi.dwMajorVersion,
                (unsigned long)osvi.dwMinorVersion,
                (unsigned long)osvi.dwBuildNumber,
                *osvi.szCSDVersion? " (":"",
                osvi.szCSDVersion,
                *osvi.szCSDVersion? ")":""
                );
  }
#endif /*HAVE_W32_SYSTEM*/
}


static void
show_version_libgcrypt (estream_t fp)
{
  const char *s;
  int n;

  s = get_revision_from_blurb (gcry_check_version ("\x01\x01"), &n);
  es_fprintf (fp, "* Libgcrypt %s (%.*s)\n",
              gcry_check_version (NULL), n, s);
  s = gcry_get_config (0, NULL);
  if (s)
    es_fputs (s, fp);
}


static void
show_version_gpgrt (estream_t fp)
{
  const char *s;
  int n;

  s = get_revision_from_blurb (gpg_error_check_version ("\x01\x01"), &n);
  es_fprintf (fp, "* GpgRT %s (%.*s)\n",
              gpg_error_check_version (NULL), n, s);
}


/* Printing version information for other libraries is problematic
 * because we don't want to link gpgconf to all these libraries.  The
 * best solution is delegating this to dirmngr which uses libassuan,
 * libksba, libnpth and ntbtls anyway.  */
static void
show_versions_via_dirmngr (estream_t fp)
{
  gpg_error_t err;
  const char *pgmname;
  const char *argv[2];
  estream_t outfp;
  pid_t pid;
  char *line = NULL;
  size_t line_len = 0;
  ssize_t length;
  int exitcode;

  pgmname = gnupg_module_name (GNUPG_MODULE_NAME_DIRMNGR);
  argv[0] = "--gpgconf-versions";
  argv[1] = NULL;
  err = gnupg_spawn_process (pgmname, argv, NULL, NULL, 0,
                             NULL, &outfp, NULL, &pid);
  if (err)
    {
      log_error ("error spawning %s: %s", pgmname, gpg_strerror (err));
      es_fprintf (fp, "[error: can't get further info]\n");
      return;
    }

  while ((length = es_read_line (outfp, &line, &line_len, NULL)) > 0)
    {
      /* Strip newline and carriage return, if present.  */
      while (length > 0
	     && (line[length - 1] == '\n' || line[length - 1] == '\r'))
	line[--length] = '\0';
      es_fprintf (fp, "%s\n", line);
    }
  if (length < 0 || es_ferror (outfp))
    {
      err = gpg_error_from_syserror ();
      log_error ("error reading from %s: %s\n", pgmname, gpg_strerror (err));
    }
  if (es_fclose (outfp))
    {
      err = gpg_error_from_syserror ();
      log_error ("error closing output stream of %s: %s\n",
                 pgmname, gpg_strerror (err));
    }

  err = gnupg_wait_process (pgmname, pid, 1, &exitcode);
  if (err)
    {
      log_error ("running %s failed (exitcode=%d): %s\n",
                 pgmname, exitcode, gpg_strerror (err));
      es_fprintf (fp, "[error: can't get further info]\n");
    }
  gnupg_release_process (pid);
  xfree (line);
}


/* Show all kind of version information.  */
static void
show_versions (estream_t fp)
{
  show_version_gnupg (fp, "");
  es_fputc ('\n', fp);
  show_version_libgcrypt (fp);
  es_fputc ('\n', fp);
  show_version_gpgrt (fp);
  es_fputc ('\n', fp);
  show_versions_via_dirmngr (fp);
}



/* Copy data from file SRC to DST.  Returns 0 on success or an error
 * 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
my_copy_file (estream_t src, estream_t dst, strlist_t *listp)
{
  gpg_error_t err;
  char *line = NULL;
  size_t line_len = 0;
  ssize_t length;
  int written;

  while ((length = es_read_line (src, &line, &line_len, NULL)) > 0)
    {
      /* Strip newline and carriage return, if present.  */
      written = gpgrt_fwrite (line, 1, length, dst);
      if (written != length)
	return gpg_error_from_syserror ();
      trim_spaces (line);
      if (*line == '[' && listp)
        {
          char **tokens;
          char *p;

          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 ();

  if (gpgrt_fflush (dst))
    return gpg_error_from_syserror ();

  return 0;
}


/* Helper for show_configs  */
static void
show_configs_one_file (const char *fname, int global, estream_t outfp,
                       strlist_t *listp)
{
  gpg_error_t err;
  estream_t fp;

  fp = es_fopen (fname, "r");
  if (!fp)
    {
      err = gpg_error_from_syserror ();
      es_fprintf (outfp, "###\n### %s config \"%s\": %s\n###\n",
                  global? "global":"local", fname,
                  (gpg_err_code (err) == GPG_ERR_ENOENT)?
                  "not installed" : gpg_strerror (err));
    }
  else
    {
      es_fprintf (outfp, "###\n### %s config \"%s\"\n###\n",
                  global? "global":"local", fname);
      es_fprintf (outfp, CUTLINE_FMT, "start");
      err = my_copy_file (fp, outfp, listp);
      if (err)
        log_error ("error copying file \"%s\": %s\n",
                   fname, gpg_strerror (err));
      es_fprintf (outfp, CUTLINE_FMT, "end--");
      es_fclose (fp);
    }
}


#ifdef HAVE_W32_SYSTEM
/* Print registry entries relevant to the GnuPG system and related
 * software.  */
static void
show_other_registry_entries (estream_t outfp)
{
  static struct {
    int group;
    const char *name;
  } names[] =
  {
    { 1, "HKLM\\Software\\Gpg4win:Install Directory" },
    { 1, "HKLM\\Software\\Gpg4win:Desktop-Version" },
    { 1, "HKLM\\Software\\Gpg4win:VS-Desktop-Version" },
    { 1, "\\" GNUPG_REGISTRY_DIR ":HomeDir" },
    { 1, "\\" GNUPG_REGISTRY_DIR ":DefaultLogFile" },
    { 2, "\\Software\\Microsoft\\Office\\Outlook\\Addins\\GNU.GpgOL"
      ":LoadBehavior" },
    { 2, "HKCU\\Software\\Microsoft\\Office\\16.0\\Outlook\\Options\\Mail:"
      "ReadAsPlain" },
    { 2, "HKCU\\Software\\Policies\\Microsoft\\Office\\16.0\\Outlook\\"
      "Options\\Mail:ReadAsPlain" },
    { 3, "logFile" },
    { 3, "enableDebug" },
    { 3, "searchSmimeServers" },
    { 3, "smimeInsecureReplyAllowed" },
    { 3, "enableSmime" },
    { 3, "preferSmime" },
    { 3, "encryptDefault" },
    { 3, "signDefault" },
    { 3, "inlinePGP" },
    { 3, "replyCrypt" },
    { 3, "autoresolve" },
    { 3, "autoretrieve" },
    { 3, "automation" },
    { 3, "autosecure" },
    { 3, "autotrust" },
    { 3, "autoencryptUntrusted" },
    { 3, "autoimport" },
    { 3, "splitBCCMails" },
    { 3, "combinedOpsEnabled" },
    { 3, "encryptSubject" },
    { 0, NULL }
  };
  int idx;
  int group = 0;
  char *namebuf = NULL;
  const char *name;
  int from_hklm;

  for (idx=0; (name = names[idx].name); idx++)
    {
      char *value;

      if (names[idx].group == 3)
        {
          xfree (namebuf);
          namebuf = xstrconcat ("\\Software\\GNU\\GpgOL", ":",
                                names[idx].name, NULL);
          name = namebuf;
        }

      value = read_w32_reg_string (name, &from_hklm);
      if (!value)
        continue;

      if (names[idx].group != group)
        {
          group = names[idx].group;
          es_fprintf (outfp, "###\n### %s related:\n",
                      group == 1 ? "GnuPG Desktop" :
                      group == 2 ? "Outlook" :
                      group == 3 ? "\\Software\\GNU\\GpgOL"
                      : "System" );
        }

      if (group == 3)
        es_fprintf (outfp, "### %s=%s%s\n", names[idx].name, value,
                    from_hklm? " [hklm]":"");
      else
        es_fprintf (outfp, "### %s\n###   ->%s<-%s\n", name, value,
                    from_hklm? " [hklm]":"");

      xfree (value);
    }

  es_fprintf (outfp, "###\n");
  xfree (namebuf);
}


/* Print registry entries take from a configuration file.  */
static void
show_registry_entries_from_file (estream_t outfp)
{
  gpg_error_t err;
  char *fname;
  estream_t fp;
  char *line = NULL;
  size_t length_of_line = 0;
  size_t  maxlen;
  ssize_t len;
  char *value = NULL;
  int from_hklm;
  int any = 0;

  fname = make_filename (gnupg_datadir (), "gpgconf.rnames", NULL);
  fp = es_fopen (fname, "r");
  if (!fp)
    {
      err = gpg_error_from_syserror ();
      if (gpg_err_code (err) != GPG_ERR_ENOENT)
        log_error ("error opening '%s': %s\n", fname, gpg_strerror (err));
      goto leave;
    }

  maxlen = 2048; /* Set limit.  */
  while ((len = es_read_line (fp, &line, &length_of_line, &maxlen)) > 0)
    {
      if (!maxlen)
        {
          err = gpg_error (GPG_ERR_LINE_TOO_LONG);
          log_error ("error reading '%s': %s\n", fname, gpg_strerror (err));
          goto leave;
        }
      trim_spaces (line);
      if (*line == '#')
        continue;

      xfree (value);
      value = read_w32_reg_string (line, &from_hklm);
      if (!value)
        continue;

      if (!any)
        {
          any = 1;
          es_fprintf (outfp, "### Taken from gpgconf.rnames:\n");
        }

      es_fprintf (outfp, "### %s\n###   ->%s<-%s\n", line, value,
                  from_hklm? " [hklm]":"");

    }
  if (len < 0 || es_ferror (fp))
    {
      err = gpg_error_from_syserror ();
      log_error ("error reading '%s': %s\n", fname, gpg_strerror (err));
    }

 leave:
  if (any)
    es_fprintf (outfp, "###\n");
  xfree (value);
  xfree (line);
  es_fclose (fp);
  xfree (fname);
}
#endif /*HAVE_W32_SYSTEM*/


/* Show all config files.  */
static void
show_configs (estream_t outfp)
{
  static const char *names[] = { "common.conf", "gpg-agent.conf",
                                 "scdaemon.conf", "dirmngr.conf",
                                 "gpg.conf", "gpgsm.conf" };
  gpg_error_t err;
  int idx;
  char *fname;
  gnupg_dir_t dir;
  gnupg_dirent_t dir_entry;
  size_t n;
  int any;
  strlist_t list = NULL;
  strlist_t sl;
  const char *s;

  es_fprintf (outfp, "### Dump of all standard config files\n");
  show_version_gnupg (outfp, "### ");
  es_fprintf (outfp, "### Libgcrypt %s\n", gcry_check_version (NULL));
  es_fprintf (outfp, "### GpgRT %s\n", gpg_error_check_version (NULL));
#ifdef HAVE_W32_SYSTEM
  es_fprintf (outfp, "### Codepages:");
  if (GetConsoleCP () != GetConsoleOutputCP ())
    es_fprintf (outfp, " %u/%u", GetConsoleCP (), GetConsoleOutputCP ());
  else
    es_fprintf (outfp, " %u", GetConsoleCP ());
  es_fprintf (outfp, " %u", GetACP ());
  es_fprintf (outfp, " %u\n", GetOEMCP ());
#endif
  es_fprintf (outfp, "###\n\n");

  list_dirs (outfp, NULL, 1);
  es_fprintf (outfp, "\n");

  for (idx = 0; idx < DIM (names); idx++)
    {
      fname = make_filename (gnupg_sysconfdir (), names[idx], NULL);
      show_configs_one_file (fname, 1, outfp, &list);
      xfree (fname);
      fname = make_filename (gnupg_homedir (), names[idx], NULL);
      show_configs_one_file (fname, 0, outfp, &list);
      xfree (fname);
      es_fprintf (outfp, "\n");
    }

  /* Print the encountered registry values and envvars.  */
  if (list)
    {
      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
  es_fprintf (outfp, "###\n### Registry entries:\n");
  any = 0;
  if (list)
    {
      for (sl = list; sl; sl = sl->next)
        if ((sl->flags & 1))
          {
            char *p;
            int from_hklm;

            if (!any)
              {
                any = 1;
                es_fprintf (outfp, "###\n### Encountered in config files:\n");
              }
            if ((p = read_w32_reg_string (sl->d, &from_hklm)))
              es_fprintf (outfp, "### %s ->%s<-%s\n", sl->d, p,
                          from_hklm? " [hklm]":"");
            else
              es_fprintf (outfp, "### %s [not set]\n", sl->d);
            xfree (p);
          }
    }
  if (!any)
    es_fprintf (outfp, "###\n");
  show_other_registry_entries (outfp);
  show_registry_entries_from_file (outfp);
#endif /*HAVE_W32_SYSTEM*/

  free_strlist (list);

  /* Check for uncommon files in the home directory.  */
  dir = gnupg_opendir (gnupg_homedir ());
  if (!dir)
    {
      err = gpg_error_from_syserror ();
      log_error ("error reading directory \"%s\": %s\n",
                 gnupg_homedir (), gpg_strerror (err));
      return;
    }

  any = 0;
  while ((dir_entry = gnupg_readdir (dir)))
    {
      for (idx = 0; idx < DIM (names); idx++)
        {
          n = strlen (names[idx]);
          if (!ascii_strncasecmp (dir_entry->d_name, names[idx], n)
              && dir_entry->d_name[n] == '-'
              && ascii_strncasecmp (dir_entry->d_name, "gpg.conf-1", 10))
            {
              if (!any)
                {
                  any = 1;
                  es_fprintf (outfp,
                              "###\n"
                              "### Warning: suspicious files in \"%s\":\n",
                              gnupg_homedir ());
                }
              es_fprintf (outfp, "### %s\n", dir_entry->d_name);
            }
        }
    }
  if (any)
    es_fprintf (outfp, "###\n");
  gnupg_closedir (dir);
}