cfc9fa3b7668c920b2cde073bc093b97aa91d094
[platform/upstream/gpg2.git] / tools / gpgconf-comp.c
1 /* gpgconf-comp.c - Configuration utility for GnuPG.
2  * Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc.
3  * Copyright (C) 2016 Werner Koch
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GnuPG; if not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <time.h>
33 #include <stdarg.h>
34 #ifdef HAVE_SIGNAL_H
35 # include <signal.h>
36 #endif
37 #include <ctype.h>
38 #ifdef HAVE_W32_SYSTEM
39 # define WIN32_LEAN_AND_MEAN 1
40 # include <windows.h>
41 #else
42 # include <pwd.h>
43 # include <grp.h>
44 #endif
45
46 /* For log_logv(), asctimestamp(), gnupg_get_time ().  */
47 #include "../common/util.h"
48 #include "../common/i18n.h"
49 #include "../common/exechelp.h"
50 #include "../common/sysutils.h"
51
52 #include "../common/gc-opt-flags.h"
53 #include "gpgconf.h"
54
55 /* There is a problem with gpg 1.4 under Windows: --gpgconf-list
56    returns a plain filename without escaping.  As long as we have not
57    fixed that we need to use gpg2.  */
58 #if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
59 #define GPGNAME "gpg2"
60 #else
61 #define GPGNAME GPG_NAME
62 #endif
63
64 \f
65 /* TODO:
66    Components: Add more components and their options.
67    Robustness: Do more validation.  Call programs to do validation for us.
68    Add options to change backend binary path.
69    Extract binary path for some backends from gpgsm/gpg config.
70 */
71
72 \f
73 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
74 void gc_error (int status, int errnum, const char *fmt, ...) \
75   __attribute__ ((format (printf, 3, 4)));
76 #endif
77
78 /* Output a diagnostic message.  If ERRNUM is not 0, then the output
79    is followed by a colon, a white space, and the error string for the
80    error number ERRNUM.  In any case the output is finished by a
81    newline.  The message is prepended by the program name, a colon,
82    and a whitespace.  The output may be further formatted or
83    redirected by the jnlib logging facility.  */
84 void
85 gc_error (int status, int errnum, const char *fmt, ...)
86 {
87   va_list arg_ptr;
88
89   va_start (arg_ptr, fmt);
90   log_logv (GPGRT_LOG_ERROR, fmt, arg_ptr);
91   va_end (arg_ptr);
92
93   if (errnum)
94     log_printf (": %s\n", strerror (errnum));
95   else
96     log_printf ("\n");
97
98   if (status)
99     {
100       log_printf (NULL);
101       log_printf ("fatal error (exit status %i)\n", status);
102       exit (status);
103     }
104 }
105
106 \f
107 /* Forward declaration.  */
108 static void gpg_agent_runtime_change (int killflag);
109 static void scdaemon_runtime_change (int killflag);
110 static void dirmngr_runtime_change (int killflag);
111
112 /* Backend configuration.  Backends are used to decide how the default
113    and current value of an option can be determined, and how the
114    option can be changed.  To every option in every component belongs
115    exactly one backend that controls and determines the option.  Some
116    backends are programs from the GPG system.  Others might be
117    implemented by GPGConf itself.  If you change this enum, don't
118    forget to update GC_BACKEND below.  */
119 typedef enum
120   {
121     /* Any backend, used for find_option ().  */
122     GC_BACKEND_ANY,
123
124     /* The Gnu Privacy Guard.  */
125     GC_BACKEND_GPG,
126
127     /* The Gnu Privacy Guard for S/MIME.  */
128     GC_BACKEND_GPGSM,
129
130     /* The GPG Agent.  */
131     GC_BACKEND_GPG_AGENT,
132
133     /* The GnuPG SCDaemon.  */
134     GC_BACKEND_SCDAEMON,
135
136     /* The GnuPG directory manager.  */
137     GC_BACKEND_DIRMNGR,
138
139     /* The LDAP server list file for the director manager.  */
140     GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST,
141
142     /* The Pinentry (not a part of GnuPG, proper).  */
143     GC_BACKEND_PINENTRY,
144
145     /* The number of the above entries.  */
146     GC_BACKEND_NR
147   } gc_backend_t;
148
149
150 /* To be able to implement generic algorithms for the various
151    backends, we collect all information about them in this struct.  */
152 static const struct
153 {
154   /* The name of the backend.  */
155   const char *name;
156
157   /* The name of the program that acts as the backend.  Some backends
158      don't have an associated program, but are implemented directly by
159      GPGConf.  In this case, PROGRAM is NULL.  */
160   char *program;
161
162   /* The module name (GNUPG_MODULE_NAME_foo) as defined by
163      ../common/util.h.  This value is used to get the actual installed
164      path of the program.  0 is used if no backend program is
165      available. */
166   char module_name;
167
168   /* The runtime change callback.  If KILLFLAG is true the component
169      is killed and not just reloaded.  */
170   void (*runtime_change) (int killflag);
171
172   /* The option name for the configuration filename of this backend.
173      This must be an absolute filename.  It can be an option from a
174      different backend (but then ordering of the options might
175      matter).  Note: This must be unique among all components.  */
176   const char *option_config_filename;
177
178   /* If this is a file backend rather than a program backend, then
179      this is the name of the option associated with the file.  */
180   const char *option_name;
181 } gc_backend[GC_BACKEND_NR] =
182   {
183     { NULL },           /* GC_BACKEND_ANY dummy entry.  */
184     { GPG_DISP_NAME, GPGNAME, GNUPG_MODULE_NAME_GPG,
185       NULL, GPGCONF_NAME "-" GPG_NAME ".conf" },
186     { GPGSM_DISP_NAME, GPGSM_NAME, GNUPG_MODULE_NAME_GPGSM,
187       NULL, GPGCONF_NAME "-" GPGSM_NAME ".conf" },
188     { GPG_AGENT_DISP_NAME, GPG_AGENT_NAME, GNUPG_MODULE_NAME_AGENT,
189       gpg_agent_runtime_change, GPGCONF_NAME"-" GPG_AGENT_NAME ".conf" },
190     { SCDAEMON_DISP_NAME, SCDAEMON_NAME, GNUPG_MODULE_NAME_SCDAEMON,
191       scdaemon_runtime_change, GPGCONF_NAME"-" SCDAEMON_NAME ".conf" },
192     { DIRMNGR_DISP_NAME, DIRMNGR_NAME, GNUPG_MODULE_NAME_DIRMNGR,
193       dirmngr_runtime_change, GPGCONF_NAME "-" DIRMNGR_NAME ".conf" },
194     { DIRMNGR_DISP_NAME " LDAP Server List", NULL, 0,
195       NULL, "ldapserverlist-file", "LDAP Server" },
196     { "Pinentry", "pinentry", GNUPG_MODULE_NAME_PINENTRY,
197       NULL, GPGCONF_NAME "-pinentry.conf" },
198   };
199
200 \f
201 /* Option configuration.  */
202
203 /* An option might take an argument, or not.  Argument types can be
204    basic or complex.  Basic types are generic and easy to validate.
205    Complex types provide more specific information about the intended
206    use, but can be difficult to validate.  If you add to this enum,
207    don't forget to update GC_ARG_TYPE below.  YOU MUST NOT CHANGE THE
208    NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE EXTERNAL
209    INTERFACE.  */
210 typedef enum
211   {
212     /* Basic argument types.  */
213
214     /* No argument.  */
215     GC_ARG_TYPE_NONE = 0,
216
217     /* A String argument.  */
218     GC_ARG_TYPE_STRING = 1,
219
220     /* A signed integer argument.  */
221     GC_ARG_TYPE_INT32 = 2,
222
223     /* An unsigned integer argument.  */
224     GC_ARG_TYPE_UINT32 = 3,
225
226     /* ADD NEW BASIC TYPE ENTRIES HERE.  */
227
228     /* Complex argument types.  */
229
230     /* A complete filename.  */
231     GC_ARG_TYPE_FILENAME = 32,
232
233     /* An LDAP server in the format
234        HOSTNAME:PORT:USERNAME:PASSWORD:BASE_DN.  */
235     GC_ARG_TYPE_LDAP_SERVER = 33,
236
237     /* A 40 character fingerprint.  */
238     GC_ARG_TYPE_KEY_FPR = 34,
239
240     /* A user ID or key ID or fingerprint for a certificate.  */
241     GC_ARG_TYPE_PUB_KEY = 35,
242
243     /* A user ID or key ID or fingerprint for a certificate with a key.  */
244     GC_ARG_TYPE_SEC_KEY = 36,
245
246     /* A alias list made up of a key, an equal sign and a space
247        separated list of values.  */
248     GC_ARG_TYPE_ALIAS_LIST = 37,
249
250     /* ADD NEW COMPLEX TYPE ENTRIES HERE.  */
251
252     /* The number of the above entries.  */
253     GC_ARG_TYPE_NR
254   } gc_arg_type_t;
255
256
257 /* For every argument, we record some information about it in the
258    following struct.  */
259 static const struct
260 {
261   /* For every argument type exists a basic argument type that can be
262      used as a fallback for input and validation purposes.  */
263   gc_arg_type_t fallback;
264
265   /* Human-readable name of the type.  */
266   const char *name;
267 } gc_arg_type[GC_ARG_TYPE_NR] =
268   {
269     /* The basic argument types have their own types as fallback.  */
270     { GC_ARG_TYPE_NONE, "none" },
271     { GC_ARG_TYPE_STRING, "string" },
272     { GC_ARG_TYPE_INT32, "int32" },
273     { GC_ARG_TYPE_UINT32, "uint32" },
274
275     /* Reserved basic type entries for future extension.  */
276     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
277     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
278     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
279     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
280     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
281     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
282     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
283     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
284     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
285     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
286     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
287     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
288     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
289     { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
290
291     /* The complex argument types have a basic type as fallback.  */
292     { GC_ARG_TYPE_STRING, "filename" },
293     { GC_ARG_TYPE_STRING, "ldap server" },
294     { GC_ARG_TYPE_STRING, "key fpr" },
295     { GC_ARG_TYPE_STRING, "pub key" },
296     { GC_ARG_TYPE_STRING, "sec key" },
297     { GC_ARG_TYPE_STRING, "alias list" },
298   };
299
300
301 /* Every option has an associated expert level, than can be used to
302    hide advanced and expert options from beginners.  If you add to
303    this list, don't forget to update GC_LEVEL below.  YOU MUST NOT
304    CHANGE THE NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE
305    EXTERNAL INTERFACE.  */
306 typedef enum
307   {
308     /* The basic options should always be displayed.  */
309     GC_LEVEL_BASIC,
310
311     /* The advanced options may be hidden from beginners.  */
312     GC_LEVEL_ADVANCED,
313
314     /* The expert options should only be displayed to experts.  */
315     GC_LEVEL_EXPERT,
316
317     /* The invisible options should normally never be displayed.  */
318     GC_LEVEL_INVISIBLE,
319
320     /* The internal options are never exported, they mark options that
321        are recorded for internal use only.  */
322     GC_LEVEL_INTERNAL,
323
324     /* ADD NEW ENTRIES HERE.  */
325
326     /* The number of the above entries.  */
327     GC_LEVEL_NR
328   } gc_expert_level_t;
329
330 /* A description for each expert level.  */
331 static const struct
332 {
333   const char *name;
334 } gc_level[] =
335   {
336     { "basic" },
337     { "advanced" },
338     { "expert" },
339     { "invisible" },
340     { "internal" }
341   };
342
343
344 /* Option flags.  The flags which are used by the backends are defined
345    by gc-opt-flags.h, included above.
346
347    YOU MUST NOT CHANGE THE NUMBERS OF THE EXISTING FLAGS, AS THEY ARE
348    PART OF THE EXTERNAL INTERFACE.  */
349
350 /* Some entries in the option list are not options, but mark the
351    beginning of a new group of options.  These entries have the GROUP
352    flag set.  */
353 #define GC_OPT_FLAG_GROUP       (1UL << 0)
354 /* The ARG_OPT flag for an option indicates that the argument is
355    optional.  This is never set for GC_ARG_TYPE_NONE options.  */
356 #define GC_OPT_FLAG_ARG_OPT     (1UL << 1)
357 /* The LIST flag for an option indicates that the option can occur
358    several times.  A comma separated list of arguments is used as the
359    argument value.  */
360 #define GC_OPT_FLAG_LIST        (1UL << 2)
361
362
363 /* A human-readable description for each flag.  */
364 static const struct
365 {
366   const char *name;
367 } gc_flag[] =
368   {
369     { "group" },
370     { "optional arg" },
371     { "list" },
372     { "runtime" },
373     { "default" },
374     { "default desc" },
375     { "no arg desc" },
376     { "no change" }
377   };
378
379
380 /* To each option, or group marker, the information in the GC_OPTION
381    struct is provided.  If you change this, don't forget to update the
382    option list of each component.  */
383 struct gc_option
384 {
385   /* If this is NULL, then this is a terminator in an array of unknown
386      length.  Otherwise, if this entry is a group marker (see FLAGS),
387      then this is the name of the group described by this entry.
388      Otherwise it is the name of the option described by this
389      entry.  The name must not contain a colon.  */
390   const char *name;
391
392   /* The option flags.  If the GROUP flag is set, then this entry is a
393      group marker, not an option, and only the fields LEVEL,
394      DESC_DOMAIN and DESC are valid.  In all other cases, this entry
395      describes a new option and all fields are valid.  */
396   unsigned long flags;
397
398   /* The expert level.  This field is valid for options and groups.  A
399      group has the expert level of the lowest-level option in the
400      group.  */
401   gc_expert_level_t level;
402
403   /* A gettext domain in which the following description can be found.
404      If this is NULL, then DESC is not translated.  Valid for groups
405      and options.
406
407      Note that we try to keep the description of groups within the
408      gnupg domain.
409
410      IMPORTANT: If you add a new domain please make sure to add a code
411      set switching call to the function my_dgettext further below.  */
412   const char *desc_domain;
413
414   /* A gettext description for this group or option.  If it starts
415      with a '|', then the string up to the next '|' describes the
416      argument, and the description follows the second '|'.
417
418      In general enclosing these description in N_() is not required
419      because the description should be identical to the one in the
420      help menu of the respective program. */
421   const char *desc;
422
423   /* The following fields are only valid for options.  */
424
425   /* The type of the option argument.  */
426   gc_arg_type_t arg_type;
427
428   /* The backend that implements this option.  */
429   gc_backend_t backend;
430
431   /* The following fields are set to NULL at startup (because all
432      option's are declared as static variables).  They are at the end
433      of the list so that they can be omitted from the option
434      declarations.  */
435
436   /* This is true if the option is supported by this version of the
437      backend.  */
438   int active;
439
440   /* The default value for this option.  This is NULL if the option is
441      not present in the backend, the empty string if no default is
442      available, and otherwise a quoted string.  */
443   char *default_value;
444
445   /* The default argument is only valid if the "optional arg" flag is
446      set, and specifies the default argument (value) that is used if
447      the argument is omitted.  */
448   char *default_arg;
449
450   /* The current value of this option.  */
451   char *value;
452
453   /* The new flags for this option.  The only defined flag is actually
454      GC_OPT_FLAG_DEFAULT, and it means that the option should be
455      deleted.  In this case, NEW_VALUE is NULL.  */
456   unsigned long new_flags;
457
458   /* The new value of this option.  */
459   char *new_value;
460 };
461 typedef struct gc_option gc_option_t;
462
463 /* Use this macro to terminate an option list.  */
464 #define GC_OPTION_NULL { NULL }
465
466 \f
467 #ifndef BUILD_WITH_AGENT
468 #define gc_options_gpg_agent NULL
469 #else
470 /* The options of the GC_COMPONENT_GPG_AGENT component.  */
471 static gc_option_t gc_options_gpg_agent[] =
472  {
473    /* The configuration file to which we write the changes.  */
474    { GPGCONF_NAME"-" GPG_AGENT_NAME ".conf",
475      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
476      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
477
478    { "Monitor",
479      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
480      "gnupg", N_("Options controlling the diagnostic output") },
481    { "verbose", GC_OPT_FLAG_LIST|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
482      "gnupg", "verbose",
483      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
484    { "quiet", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
485      "gnupg", "be somewhat more quiet",
486      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
487    { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
488      NULL, NULL,
489      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
490
491    { "Configuration",
492      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
493      "gnupg", N_("Options controlling the configuration") },
494    { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
495      "gnupg", "|FILE|read options from FILE",
496      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
497    { "disable-scdaemon", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
498      "gnupg", "do not use the SCdaemon",
499      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
500    { "enable-ssh-support", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
501      "gnupg", "enable ssh support",
502      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
503    { "ssh-fingerprint-digest",
504      GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_EXPERT,
505      "gnupg", "|ALGO|use ALGO to show ssh fingerprints",
506      GC_ARG_TYPE_STRING, GC_BACKEND_GPG_AGENT },
507    { "enable-putty-support", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
508      "gnupg", "enable putty support",
509      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
510    { "enable-extended-key-format", GC_OPT_FLAG_RUNTIME, GC_LEVEL_INVISIBLE,
511      NULL, NULL,
512      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
513
514    { "Debug",
515      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
516      "gnupg", N_("Options useful for debugging") },
517    { "debug-level", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
518      "gnupg", "|LEVEL|set the debugging level to LEVEL",
519      GC_ARG_TYPE_STRING, GC_BACKEND_GPG_AGENT },
520    { "log-file", GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
521      "gnupg", N_("|FILE|write server mode logs to FILE"),
522      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
523    { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
524      NULL, NULL,
525      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
526
527    { "Security",
528      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
529      "gnupg", N_("Options controlling the security") },
530    { "default-cache-ttl", GC_OPT_FLAG_RUNTIME,
531      GC_LEVEL_BASIC, "gnupg",
532      "|N|expire cached PINs after N seconds",
533      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
534    { "default-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME,
535      GC_LEVEL_ADVANCED, "gnupg",
536      N_("|N|expire SSH keys after N seconds"),
537      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
538    { "max-cache-ttl", GC_OPT_FLAG_RUNTIME,
539      GC_LEVEL_EXPERT, "gnupg",
540      N_("|N|set maximum PIN cache lifetime to N seconds"),
541      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
542    { "max-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME,
543      GC_LEVEL_EXPERT, "gnupg",
544      N_("|N|set maximum SSH key lifetime to N seconds"),
545      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
546    { "ignore-cache-for-signing", GC_OPT_FLAG_RUNTIME,
547      GC_LEVEL_BASIC, "gnupg", "do not use the PIN cache when signing",
548      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
549    { "allow-emacs-pinentry", GC_OPT_FLAG_RUNTIME,
550      GC_LEVEL_ADVANCED,
551      "gnupg", "allow passphrase to be prompted through Emacs",
552      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
553    { "no-allow-external-cache", GC_OPT_FLAG_RUNTIME,
554      GC_LEVEL_BASIC, "gnupg", "disallow the use of an external password cache",
555      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
556    { "no-allow-mark-trusted", GC_OPT_FLAG_RUNTIME,
557      GC_LEVEL_ADVANCED, "gnupg", "disallow clients to mark keys as \"trusted\"",
558      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
559    { "no-allow-loopback-pinentry", GC_OPT_FLAG_RUNTIME,
560      GC_LEVEL_EXPERT, "gnupg", "disallow caller to override the pinentry",
561      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
562    { "no-grab", GC_OPT_FLAG_RUNTIME, GC_LEVEL_EXPERT,
563      "gnupg", "do not grab keyboard and mouse",
564      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
565
566    { "Passphrase policy",
567      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
568      "gnupg", N_("Options enforcing a passphrase policy") },
569    { "enforce-passphrase-constraints", GC_OPT_FLAG_RUNTIME,
570      GC_LEVEL_EXPERT, "gnupg",
571      N_("do not allow bypassing the passphrase policy"),
572      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
573    { "min-passphrase-len", GC_OPT_FLAG_RUNTIME,
574      GC_LEVEL_ADVANCED, "gnupg",
575      N_("|N|set minimal required length for new passphrases to N"),
576      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
577    { "min-passphrase-nonalpha", GC_OPT_FLAG_RUNTIME,
578      GC_LEVEL_EXPERT, "gnupg",
579      N_("|N|require at least N non-alpha characters for a new passphrase"),
580      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
581    { "check-passphrase-pattern", GC_OPT_FLAG_RUNTIME,
582      GC_LEVEL_EXPERT,
583      "gnupg", N_("|FILE|check new passphrases against pattern in FILE"),
584      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
585    { "max-passphrase-days", GC_OPT_FLAG_RUNTIME,
586      GC_LEVEL_EXPERT, "gnupg",
587      N_("|N|expire the passphrase after N days"),
588      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
589    { "enable-passphrase-history", GC_OPT_FLAG_RUNTIME,
590      GC_LEVEL_EXPERT, "gnupg",
591      N_("do not allow the reuse of old passphrases"),
592      GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
593    { "pinentry-timeout", GC_OPT_FLAG_RUNTIME,
594      GC_LEVEL_ADVANCED, "gnupg",
595      N_("|N|set the Pinentry timeout to N seconds"),
596      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
597
598    GC_OPTION_NULL
599  };
600 #endif /*BUILD_WITH_AGENT*/
601
602
603 #ifndef BUILD_WITH_SCDAEMON
604 #define gc_options_scdaemon NULL
605 #else
606 /* The options of the GC_COMPONENT_SCDAEMON component.  */
607 static gc_option_t gc_options_scdaemon[] =
608  {
609    /* The configuration file to which we write the changes.  */
610    { GPGCONF_NAME"-"SCDAEMON_NAME".conf",
611      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
612      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
613
614    { "Monitor",
615      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
616      "gnupg", N_("Options controlling the diagnostic output") },
617    { "verbose", GC_OPT_FLAG_LIST|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
618      "gnupg", "verbose",
619      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
620    { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
621      "gnupg", "be somewhat more quiet",
622      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
623    { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
624      NULL, NULL,
625      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
626
627    { "Configuration",
628      GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
629      "gnupg", N_("Options controlling the configuration") },
630    { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
631      "gnupg", "|FILE|read options from FILE",
632      GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
633    { "reader-port", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
634      "gnupg", "|N|connect to reader at port N",
635      GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
636    { "ctapi-driver", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
637      "gnupg", "|NAME|use NAME as ct-API driver",
638      GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
639    { "pcsc-driver", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
640      "gnupg", "|NAME|use NAME as PC/SC driver",
641      GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
642    { "disable-ccid", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_EXPERT,
643      "gnupg", "do not use the internal CCID driver",
644      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
645    { "disable-pinpad", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
646      "gnupg", "do not use a reader's pinpad",
647      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
648    { "enable-pinpad-varlen",
649      GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
650      "gnupg", "use variable length input for pinpad",
651      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
652    { "card-timeout", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
653      "gnupg", "|N|disconnect the card after N seconds of inactivity",
654      GC_ARG_TYPE_UINT32, GC_BACKEND_SCDAEMON },
655
656    { "Debug",
657      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
658      "gnupg", N_("Options useful for debugging") },
659    { "debug-level", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
660      "gnupg", "|LEVEL|set the debugging level to LEVEL",
661      GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
662    { "log-file", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
663      "gnupg", N_("|FILE|write a log to FILE"),
664      GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
665
666    { "Security",
667      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
668      "gnupg", N_("Options controlling the security") },
669    { "deny-admin", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
670      "gnupg", "deny the use of admin card commands",
671      GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
672
673
674    GC_OPTION_NULL
675  };
676 #endif /*BUILD_WITH_SCDAEMON*/
677
678 #ifndef BUILD_WITH_GPG
679 #define gc_options_gpg NULL
680 #else
681 /* The options of the GC_COMPONENT_GPG component.  */
682 static gc_option_t gc_options_gpg[] =
683  {
684    /* The configuration file to which we write the changes.  */
685    { GPGCONF_NAME"-"GPG_NAME".conf",
686      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
687      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
688
689    { "Monitor",
690      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
691      "gnupg", N_("Options controlling the diagnostic output") },
692    { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
693      "gnupg", "verbose",
694      GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
695    { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
696      "gnupg", "be somewhat more quiet",
697      GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
698    { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
699      NULL, NULL,
700      GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
701
702    { "Configuration",
703      GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
704      "gnupg", N_("Options controlling the configuration") },
705    { "default-key", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
706      "gnupg", N_("|NAME|use NAME as default secret key"),
707      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
708    { "encrypt-to", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
709      "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
710      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
711    { "group", GC_OPT_FLAG_LIST, GC_LEVEL_ADVANCED,
712      "gnupg", N_("|SPEC|set up email aliases"),
713      GC_ARG_TYPE_ALIAS_LIST, GC_BACKEND_GPG },
714    { "options", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
715      NULL, NULL,
716      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
717    { "compliance", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
718      NULL, NULL,
719      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
720    { "default-new-key-algo", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
721      NULL, NULL,
722      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
723    { "default_pubkey_algo",
724      (GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_NO_CHANGE), GC_LEVEL_INVISIBLE,
725      NULL, NULL,
726      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
727    { "trust-model",
728      GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
729      NULL, NULL,
730      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
731
732
733    { "Debug",
734      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
735      "gnupg", N_("Options useful for debugging") },
736    { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
737      "gnupg", "|LEVEL|set the debugging level to LEVEL",
738      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
739    { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
740      "gnupg", N_("|FILE|write server mode logs to FILE"),
741      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
742 /*    { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE, */
743 /*      NULL, NULL, */
744 /*      GC_ARG_TYPE_UINT32, GC_BACKEND_GPG }, */
745
746    { "Keyserver",
747      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
748      "gnupg", N_("Configuration for Keyservers") },
749    { "keyserver", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
750      "gnupg", N_("|URL|use keyserver at URL"), /* Deprecated - use dirmngr */
751      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
752    { "allow-pka-lookup", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
753      "gnupg", N_("allow PKA lookups (DNS requests)"),
754      GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
755    { "auto-key-locate", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
756      "gnupg", N_("|MECHANISMS|use MECHANISMS to locate keys by mail address"),
757      GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
758    { "auto-key-retrieve", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
759      NULL, NULL, GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
760
761
762    GC_OPTION_NULL
763  };
764 #endif /*BUILD_WITH_GPG*/
765
766
767 #ifndef BUILD_WITH_GPGSM
768 #define gc_options_gpgsm NULL
769 #else
770 /* The options of the GC_COMPONENT_GPGSM component.  */
771 static gc_option_t gc_options_gpgsm[] =
772  {
773    /* The configuration file to which we write the changes.  */
774    { GPGCONF_NAME"-"GPGSM_NAME".conf",
775      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
776      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
777
778    { "Monitor",
779      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
780      "gnupg", N_("Options controlling the diagnostic output") },
781    { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
782      "gnupg", "verbose",
783      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
784    { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
785      "gnupg", "be somewhat more quiet",
786      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
787    { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
788      NULL, NULL,
789      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
790
791    { "Configuration",
792      GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
793      "gnupg", N_("Options controlling the configuration") },
794    { "default-key", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
795      "gnupg", N_("|NAME|use NAME as default secret key"),
796      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
797    { "encrypt-to", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
798      "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
799      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
800    { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
801      "gnupg", "|FILE|read options from FILE",
802      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
803    { "prefer-system-dirmngr", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
804      "gnupg", "use system's dirmngr if available",
805      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
806    { "disable-dirmngr", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
807      "gnupg", N_("disable all access to the dirmngr"),
808      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
809    { "p12-charset", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
810      "gnupg", N_("|NAME|use encoding NAME for PKCS#12 passphrases"),
811      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
812    { "keyserver", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
813      "gnupg", N_("|SPEC|use this keyserver to lookup keys"),
814      GC_ARG_TYPE_LDAP_SERVER, GC_BACKEND_GPGSM },
815    { "default_pubkey_algo",
816      (GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_NO_CHANGE), GC_LEVEL_INVISIBLE,
817      NULL, NULL,
818      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
819
820    { "Debug",
821      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
822      "gnupg", N_("Options useful for debugging") },
823    { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
824      "gnupg", "|LEVEL|set the debugging level to LEVEL",
825      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
826    { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
827      "gnupg", N_("|FILE|write server mode logs to FILE"),
828      GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
829    { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
830      NULL, NULL,
831      GC_ARG_TYPE_UINT32, GC_BACKEND_GPGSM },
832
833    { "Security",
834      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
835      "gnupg", N_("Options controlling the security") },
836    { "disable-crl-checks", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
837      "gnupg", "never consult a CRL",
838      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
839    { "enable-crl-checks", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
840      NULL, NULL,
841      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
842    { "disable-trusted-cert-crl-check", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
843      "gnupg", N_("do not check CRLs for root certificates"),
844      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
845    { "enable-ocsp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
846      "gnupg", "check validity using OCSP",
847      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
848    { "include-certs", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
849      "gnupg", "|N|number of certificates to include",
850      GC_ARG_TYPE_INT32, GC_BACKEND_GPGSM },
851    { "disable-policy-checks", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
852      "gnupg", "do not check certificate policies",
853      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
854    { "auto-issuer-key-retrieve", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
855      "gnupg", "fetch missing issuer certificates",
856      GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
857    { "cipher-algo", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
858      "gnupg", "|NAME|use cipher algorithm NAME",
859      GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
860
861    GC_OPTION_NULL
862  };
863 #endif /*BUILD_WITH_GPGSM*/
864
865
866 #ifndef BUILD_WITH_DIRMNGR
867 #define gc_options_dirmngr NULL
868 #else
869 /* The options of the GC_COMPONENT_DIRMNGR component.  */
870 static gc_option_t gc_options_dirmngr[] =
871  {
872    /* The configuration file to which we write the changes.  */
873    { GPGCONF_NAME"-"DIRMNGR_NAME".conf",
874      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
875      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
876
877    { "Monitor",
878      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
879      "gnupg", N_("Options controlling the diagnostic output") },
880    { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
881      "dirmngr", "verbose",
882      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
883    { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
884      "dirmngr", "be somewhat more quiet",
885      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
886    { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
887      NULL, NULL,
888      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
889
890    { "Format",
891      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
892      "gnupg", N_("Options controlling the format of the output") },
893    { "sh", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
894      "dirmngr", "sh-style command output",
895      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
896    { "csh", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
897      "dirmngr", "csh-style command output",
898      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
899
900    { "Configuration",
901      GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
902      "gnupg", N_("Options controlling the configuration") },
903    { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
904      "dirmngr", "|FILE|read options from FILE",
905      GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
906    { "resolver-timeout", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
907      NULL, NULL,
908      GC_ARG_TYPE_INT32, GC_BACKEND_DIRMNGR },
909    { "nameserver", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
910      NULL, NULL,
911      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
912
913    { "Debug",
914      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
915      "gnupg", N_("Options useful for debugging") },
916    { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
917      "dirmngr", "|LEVEL|set the debugging level to LEVEL",
918      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
919    { "no-detach", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
920      "dirmngr", "do not detach from the console",
921      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
922    { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
923      "dirmngr", N_("|FILE|write server mode logs to FILE"),
924      GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
925    { "debug-wait", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
926      NULL, NULL,
927      GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
928    { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
929      NULL, NULL,
930      GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
931
932    { "Enforcement",
933      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
934      "gnupg", N_("Options controlling the interactivity and enforcement") },
935    { "batch", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
936      "dirmngr", "run without asking a user",
937      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
938    { "force", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
939      "dirmngr", "force loading of outdated CRLs",
940      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
941    { "allow-version-check", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
942      "dirmngr", "allow online software version check",
943      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
944
945    { "Tor",
946      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
947      "gnupg", N_("Options controlling the use of Tor") },
948    { "use-tor", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
949      "dirmngr", "route all network traffic via TOR",
950       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
951
952    { "Keyserver",
953      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
954      "gnupg", N_("Configuration for Keyservers") },
955    { "keyserver", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
956      "gnupg", N_("|URL|use keyserver at URL"),
957      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
958
959    { "HTTP",
960      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
961      "gnupg", N_("Configuration for HTTP servers") },
962    { "disable-http", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
963      "dirmngr", "inhibit the use of HTTP",
964       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
965    { "ignore-http-dp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
966      "dirmngr", "ignore HTTP CRL distribution points",
967       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
968    { "http-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
969      "dirmngr", "|URL|redirect all HTTP requests to URL",
970      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
971    { "honor-http-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
972      "gnupg", N_("use system's HTTP proxy setting"),
973      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
974
975    { "LDAP",
976      GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
977      "gnupg", N_("Configuration of LDAP servers to use") },
978    { "disable-ldap", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
979      "dirmngr", "inhibit the use of LDAP",
980       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
981    { "ignore-ldap-dp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
982      "dirmngr", "ignore LDAP CRL distribution points",
983       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
984    { "ldap-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
985      "dirmngr", "|HOST|use HOST for LDAP queries",
986      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
987    { "only-ldap-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
988      "dirmngr", "do not use fallback hosts with --ldap-proxy",
989       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
990    { "add-servers", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
991      "dirmngr", "add new servers discovered in CRL distribution points"
992      " to serverlist", GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
993    { "ldaptimeout", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
994      "dirmngr", "|N|set LDAP timeout to N seconds",
995      GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
996    /* The following entry must not be removed, as it is required for
997       the GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST.  */
998    { "ldapserverlist-file",
999      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
1000      "dirmngr", "|FILE|read LDAP server list from FILE",
1001      GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
1002    /* This entry must come after at least one entry for
1003       GC_BACKEND_DIRMNGR in this component, so that the entry for
1004       "ldapserverlist-file will be initialized before this one.  */
1005    { "LDAP Server", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
1006      "gnupg", N_("LDAP server list"),
1007      GC_ARG_TYPE_LDAP_SERVER, GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST },
1008    { "max-replies", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
1009      "dirmngr", "|N|do not return more than N items in one query",
1010      GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
1011
1012    { "OCSP",
1013      GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
1014      "gnupg", N_("Configuration for OCSP") },
1015    { "allow-ocsp", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
1016      "dirmngr", "allow sending OCSP requests",
1017      GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
1018    { "ignore-ocsp-service-url", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1019      "dirmngr", "ignore certificate contained OCSP service URLs",
1020       GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
1021    { "ocsp-responder", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1022      "dirmngr", "|URL|use OCSP responder at URL",
1023      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
1024    { "ocsp-signer", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1025      "dirmngr", "|FPR|OCSP response signed by FPR",
1026      GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
1027
1028
1029    GC_OPTION_NULL
1030  };
1031 #endif /*BUILD_WITH_DIRMNGR*/
1032
1033
1034 /* The options of the GC_COMPONENT_PINENTRY component.  */
1035 static gc_option_t gc_options_pinentry[] =
1036  {
1037    /* A dummy option to allow gc_component_list_components to find the
1038       pinentry backend.  Needs to be a conf file. */
1039    { GPGCONF_NAME"-pinentry.conf",
1040      GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
1041      NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_PINENTRY },
1042
1043    GC_OPTION_NULL
1044  };
1045
1046
1047 \f
1048 /* Component system.  Each component is a set of options that can be
1049    configured at the same time.  If you change this, don't forget to
1050    update GC_COMPONENT below.  */
1051 typedef enum
1052   {
1053     /* The classic GPG for OpenPGP.  */
1054     GC_COMPONENT_GPG,
1055
1056     /* The GPG Agent.  */
1057     GC_COMPONENT_GPG_AGENT,
1058
1059     /* The Smardcard Daemon.  */
1060     GC_COMPONENT_SCDAEMON,
1061
1062     /* GPG for S/MIME.  */
1063     GC_COMPONENT_GPGSM,
1064
1065     /* The LDAP Directory Manager for CRLs.  */
1066     GC_COMPONENT_DIRMNGR,
1067
1068     /* The external Pinentry.  */
1069     GC_COMPONENT_PINENTRY,
1070
1071     /* The number of components.  */
1072     GC_COMPONENT_NR
1073   } gc_component_t;
1074
1075
1076 /* The information associated with each component.  */
1077 static const struct
1078 {
1079   /* The name of this component.  Must not contain a colon (':')
1080      character.  */
1081   const char *name;
1082
1083   /* The gettext domain for the description DESC.  If this is NULL,
1084      then the description is not translated.  */
1085   const char *desc_domain;
1086
1087   /* The description for this domain.  */
1088   const char *desc;
1089
1090   /* The list of options for this component, terminated by
1091      GC_OPTION_NULL.  */
1092   gc_option_t *options;
1093 } gc_component[] =
1094   {
1095     { "gpg",      "gnupg", N_("OpenPGP"), gc_options_gpg },
1096     { "gpg-agent","gnupg", N_("Private Keys"), gc_options_gpg_agent },
1097     { "scdaemon", "gnupg", N_("Smartcards"), gc_options_scdaemon },
1098     { "gpgsm",    "gnupg", N_("S/MIME"), gc_options_gpgsm },
1099     { "dirmngr",  "gnupg", N_("Network"), gc_options_dirmngr },
1100     { "pinentry", "gnupg", N_("Passphrase Entry"), gc_options_pinentry }
1101   };
1102
1103
1104
1105 /* Structure used to collect error output of the backend programs.  */
1106 struct error_line_s;
1107 typedef struct error_line_s *error_line_t;
1108 struct error_line_s
1109 {
1110   error_line_t next;   /* Link to next item.  */
1111   const char *fname;   /* Name of the config file (points into BUFFER).  */
1112   unsigned int lineno; /* Line number of the config file.  */
1113   const char *errtext; /* Text of the error message (points into BUFFER).  */
1114   char buffer[1];  /* Helper buffer.  */
1115 };
1116
1117
1118 \f
1119
1120 /* Initialization and finalization.  */
1121
1122 static void
1123 gc_option_free (gc_option_t *o)
1124 {
1125   if (o == NULL || o->name == NULL)
1126     return;
1127
1128   xfree (o->value);
1129   gc_option_free (o + 1);
1130 }
1131
1132 static void
1133 gc_components_free (void)
1134 {
1135   int i;
1136   for (i = 0; i < DIM (gc_component); i++)
1137     gc_option_free (gc_component[i].options);
1138 }
1139
1140 void
1141 gc_components_init (void)
1142 {
1143   atexit (gc_components_free);
1144 }
1145
1146 \f
1147
1148 /* Engine specific support.  */
1149 static void
1150 gpg_agent_runtime_change (int killflag)
1151 {
1152   gpg_error_t err = 0;
1153   const char *pgmname;
1154   const char *argv[5];
1155   pid_t pid = (pid_t)(-1);
1156   char *abs_homedir = NULL;
1157   int i = 0;
1158
1159   pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1160   if (!gnupg_default_homedir_p ())
1161     {
1162       abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1163       if (!abs_homedir)
1164         err = gpg_error_from_syserror ();
1165
1166       argv[i++] = "--homedir";
1167       argv[i++] = abs_homedir;
1168     }
1169   argv[i++] = "--no-autostart";
1170   argv[i++] = killflag? "KILLAGENT" : "RELOADAGENT";
1171   argv[i++] = NULL;
1172
1173   if (!err)
1174     err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1175   if (!err)
1176     err = gnupg_wait_process (pgmname, pid, 1, NULL);
1177   if (err)
1178     gc_error (0, 0, "error running '%s %s': %s",
1179               pgmname, argv[1], gpg_strerror (err));
1180   gnupg_release_process (pid);
1181   xfree (abs_homedir);
1182 }
1183
1184
1185 static void
1186 scdaemon_runtime_change (int killflag)
1187 {
1188   gpg_error_t err = 0;
1189   const char *pgmname;
1190   const char *argv[9];
1191   pid_t pid = (pid_t)(-1);
1192   char *abs_homedir = NULL;
1193   int i = 0;
1194
1195   (void)killflag;  /* For scdaemon kill and reload are synonyms.  */
1196
1197   /* We use "GETINFO app_running" to see whether the agent is already
1198      running and kill it only in this case.  This avoids an explicit
1199      starting of the agent in case it is not yet running.  There is
1200      obviously a race condition but that should not harm too much.  */
1201
1202   pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1203   if (!gnupg_default_homedir_p ())
1204     {
1205       abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1206       if (!abs_homedir)
1207         err = gpg_error_from_syserror ();
1208
1209       argv[i++] = "--homedir";
1210       argv[i++] = abs_homedir;
1211     }
1212   argv[i++] = "-s";
1213   argv[i++] = "--no-autostart";
1214   argv[i++] = "GETINFO scd_running";
1215   argv[i++] = "/if ${! $?}";
1216   argv[i++] = "scd killscd";
1217   argv[i++] = "/end";
1218   argv[i++] = NULL;
1219
1220   if (!err)
1221     err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1222   if (!err)
1223     err = gnupg_wait_process (pgmname, pid, 1, NULL);
1224   if (err)
1225     gc_error (0, 0, "error running '%s %s': %s",
1226               pgmname, argv[4], gpg_strerror (err));
1227   gnupg_release_process (pid);
1228   xfree (abs_homedir);
1229 }
1230
1231
1232 static void
1233 dirmngr_runtime_change (int killflag)
1234 {
1235   gpg_error_t err = 0;
1236   const char *pgmname;
1237   const char *argv[6];
1238   pid_t pid = (pid_t)(-1);
1239   char *abs_homedir = NULL;
1240
1241   pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1242   argv[0] = "--no-autostart";
1243   argv[1] = "--dirmngr";
1244   argv[2] = killflag? "KILLDIRMNGR" : "RELOADDIRMNGR";
1245   if (gnupg_default_homedir_p ())
1246     argv[3] = NULL;
1247   else
1248     {
1249       abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1250       if (!abs_homedir)
1251         err = gpg_error_from_syserror ();
1252
1253       argv[3] = "--homedir";
1254       argv[4] = abs_homedir;
1255       argv[5] = NULL;
1256     }
1257
1258   if (!err)
1259     err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1260   if (!err)
1261     err = gnupg_wait_process (pgmname, pid, 1, NULL);
1262   if (err)
1263     gc_error (0, 0, "error running '%s %s': %s",
1264               pgmname, argv[2], gpg_strerror (err));
1265   gnupg_release_process (pid);
1266   xfree (abs_homedir);
1267 }
1268
1269
1270 /* Launch the gpg-agent or the dirmngr if not already running.  */
1271 gpg_error_t
1272 gc_component_launch (int component)
1273 {
1274   gpg_error_t err;
1275   const char *pgmname;
1276   const char *argv[3];
1277   int i;
1278   pid_t pid;
1279
1280   if (component < 0)
1281     {
1282       err = gc_component_launch (GC_COMPONENT_GPG_AGENT);
1283       if (!err)
1284         err = gc_component_launch (GC_COMPONENT_DIRMNGR);
1285       return err;
1286     }
1287
1288   if (!(component == GC_COMPONENT_GPG_AGENT
1289         || component == GC_COMPONENT_DIRMNGR))
1290     {
1291       es_fputs (_("Component not suitable for launching"), es_stderr);
1292       es_putc ('\n', es_stderr);
1293       exit (1);
1294     }
1295
1296   pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1297   i = 0;
1298   if (component == GC_COMPONENT_DIRMNGR)
1299     argv[i++] = "--dirmngr";
1300   argv[i++] = "NOP";
1301   argv[i] = NULL;
1302
1303   err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1304   if (!err)
1305     err = gnupg_wait_process (pgmname, pid, 1, NULL);
1306   if (err)
1307     gc_error (0, 0, "error running '%s%s%s': %s",
1308               pgmname,
1309               component == GC_COMPONENT_DIRMNGR? " --dirmngr":"",
1310               " NOP",
1311               gpg_strerror (err));
1312   gnupg_release_process (pid);
1313   return err;
1314 }
1315
1316
1317 /* Unconditionally restart COMPONENT.  */
1318 void
1319 gc_component_kill (int component)
1320 {
1321   int runtime[GC_BACKEND_NR];
1322   gc_option_t *option;
1323   gc_backend_t backend;
1324
1325   /* Set a flag for the backends to be reloaded.  */
1326   for (backend = 0; backend < GC_BACKEND_NR; backend++)
1327     runtime[backend] = 0;
1328
1329   if (component < 0)
1330     {
1331       for (component = 0; component < GC_COMPONENT_NR; component++)
1332         {
1333           option = gc_component[component].options;
1334           for (; option && option->name; option++)
1335             runtime[option->backend] = 1;
1336         }
1337     }
1338   else
1339     {
1340       assert (component < GC_COMPONENT_NR);
1341       option = gc_component[component].options;
1342       for (; option && option->name; option++)
1343         runtime[option->backend] = 1;
1344     }
1345
1346   /* Do the restart for the selected backends.  */
1347   for (backend = 0; backend < GC_BACKEND_NR; backend++)
1348     {
1349       if (runtime[backend] && gc_backend[backend].runtime_change)
1350         (*gc_backend[backend].runtime_change) (1);
1351     }
1352 }
1353
1354
1355 /* Unconditionally reload COMPONENT or all components if COMPONENT is -1.  */
1356 void
1357 gc_component_reload (int component)
1358 {
1359   int runtime[GC_BACKEND_NR];
1360   gc_option_t *option;
1361   gc_backend_t backend;
1362
1363   /* Set a flag for the backends to be reloaded.  */
1364   for (backend = 0; backend < GC_BACKEND_NR; backend++)
1365     runtime[backend] = 0;
1366
1367   if (component < 0)
1368     {
1369       for (component = 0; component < GC_COMPONENT_NR; component++)
1370         {
1371           option = gc_component[component].options;
1372           for (; option && option->name; option++)
1373             runtime[option->backend] = 1;
1374         }
1375     }
1376   else
1377     {
1378       assert (component < GC_COMPONENT_NR);
1379       option = gc_component[component].options;
1380       for (; option && option->name; option++)
1381         runtime[option->backend] = 1;
1382     }
1383
1384   /* Do the reload for all selected backends.  */
1385   for (backend = 0; backend < GC_BACKEND_NR; backend++)
1386     {
1387       if (runtime[backend] && gc_backend[backend].runtime_change)
1388         (*gc_backend[backend].runtime_change) (0);
1389     }
1390 }
1391
1392
1393 \f
1394 /* More or less Robust version of dgettext.  It has the side effect of
1395    switching the codeset to utf-8 because this is what we want to
1396    output.  In theory it is possible to keep the original code set and
1397    switch back for regular disgnostic output (redefine "_(" for that)
1398    but given the natur of this tool, being something invoked from
1399    other pograms, it does not make much sense.  */
1400 static const char *
1401 my_dgettext (const char *domain, const char *msgid)
1402 {
1403 #ifdef USE_SIMPLE_GETTEXT
1404   if (domain)
1405     {
1406       static int switched_codeset;
1407       char *text;
1408
1409       if (!switched_codeset)
1410         {
1411           switched_codeset = 1;
1412           gettext_use_utf8 (1);
1413         }
1414
1415       if (!strcmp (domain, "gnupg"))
1416         domain = PACKAGE_GT;
1417
1418       /* FIXME: we have no dgettext, thus we can't switch.  */
1419
1420       text = (char*)gettext (msgid);
1421       return text ? text : msgid;
1422     }
1423   else
1424     return msgid;
1425 #elif defined(ENABLE_NLS)
1426   if (domain)
1427     {
1428       static int switched_codeset;
1429       char *text;
1430
1431       if (!switched_codeset)
1432         {
1433           switched_codeset = 1;
1434           bind_textdomain_codeset (PACKAGE_GT, "utf-8");
1435
1436           bindtextdomain (DIRMNGR_NAME, LOCALEDIR);
1437           bind_textdomain_codeset (DIRMNGR_NAME, "utf-8");
1438
1439         }
1440
1441       /* Note: This is a hack to actually use the gnupg2 domain as
1442          long we are in a transition phase where gnupg 1.x and 1.9 may
1443          coexist. */
1444       if (!strcmp (domain, "gnupg"))
1445         domain = PACKAGE_GT;
1446
1447       text = dgettext (domain, msgid);
1448       return text ? text : msgid;
1449     }
1450   else
1451     return msgid;
1452 #else
1453   (void)domain;
1454   return msgid;
1455 #endif
1456 }
1457
1458
1459 /* Percent-Escape special characters.  The string is valid until the
1460    next invocation of the function.  */
1461 char *
1462 gc_percent_escape (const char *src)
1463 {
1464   static char *esc_str;
1465   static int esc_str_len;
1466   int new_len = 3 * strlen (src) + 1;
1467   char *dst;
1468
1469   if (esc_str_len < new_len)
1470     {
1471       char *new_esc_str = realloc (esc_str, new_len);
1472       if (!new_esc_str)
1473         gc_error (1, errno, "can not escape string");
1474       esc_str = new_esc_str;
1475       esc_str_len = new_len;
1476     }
1477
1478   dst = esc_str;
1479   while (*src)
1480     {
1481       if (*src == '%')
1482         {
1483           *(dst++) = '%';
1484           *(dst++) = '2';
1485           *(dst++) = '5';
1486         }
1487       else if (*src == ':')
1488         {
1489           /* The colon is used as field separator.  */
1490           *(dst++) = '%';
1491           *(dst++) = '3';
1492           *(dst++) = 'a';
1493         }
1494       else if (*src == ',')
1495         {
1496           /* The comma is used as list separator.  */
1497           *(dst++) = '%';
1498           *(dst++) = '2';
1499           *(dst++) = 'c';
1500         }
1501       else if (*src == '\n')
1502         {
1503           /* The newline is problematic in a line-based format.  */
1504           *(dst++) = '%';
1505           *(dst++) = '0';
1506           *(dst++) = 'a';
1507         }
1508       else
1509         *(dst++) = *(src);
1510       src++;
1511     }
1512   *dst = '\0';
1513   return esc_str;
1514 }
1515
1516
1517
1518 /* Percent-Deescape special characters.  The string is valid until the
1519    next invocation of the function.  */
1520 static char *
1521 percent_deescape (const char *src)
1522 {
1523   static char *str;
1524   static int str_len;
1525   int new_len = 3 * strlen (src) + 1;
1526   char *dst;
1527
1528   if (str_len < new_len)
1529     {
1530       char *new_str = realloc (str, new_len);
1531       if (!new_str)
1532         gc_error (1, errno, "can not deescape string");
1533       str = new_str;
1534       str_len = new_len;
1535     }
1536
1537   dst = str;
1538   while (*src)
1539     {
1540       if (*src == '%')
1541         {
1542           int val = hextobyte (src + 1);
1543
1544           if (val < 0)
1545             gc_error (1, 0, "malformed end of string %s", src);
1546
1547           *(dst++) = (char) val;
1548           src += 3;
1549         }
1550       else
1551         *(dst++) = *(src++);
1552     }
1553   *dst = '\0';
1554   return str;
1555 }
1556
1557 \f
1558 /* List all components that are available.  */
1559 void
1560 gc_component_list_components (estream_t out)
1561 {
1562   gc_component_t component;
1563   gc_option_t *option;
1564   gc_backend_t backend;
1565   int backend_seen[GC_BACKEND_NR];
1566   const char *desc;
1567   const char *pgmname;
1568
1569   for (component = 0; component < GC_COMPONENT_NR; component++)
1570     {
1571       option = gc_component[component].options;
1572       if (option)
1573         {
1574           for (backend = 0; backend < GC_BACKEND_NR; backend++)
1575             backend_seen[backend] = 0;
1576
1577           pgmname = "";
1578           for (; option && option->name; option++)
1579             {
1580               if ((option->flags & GC_OPT_FLAG_GROUP))
1581                 continue;
1582               backend = option->backend;
1583               if (backend_seen[backend])
1584                 continue;
1585               backend_seen[backend] = 1;
1586               assert (backend != GC_BACKEND_ANY);
1587               if (gc_backend[backend].program
1588                   && !gc_backend[backend].module_name)
1589                 continue;
1590               pgmname = gnupg_module_name (gc_backend[backend].module_name);
1591               break;
1592             }
1593
1594           desc = gc_component[component].desc;
1595           desc = my_dgettext (gc_component[component].desc_domain, desc);
1596           es_fprintf (out, "%s:%s:",
1597                       gc_component[component].name,  gc_percent_escape (desc));
1598           es_fprintf (out, "%s\n",  gc_percent_escape (pgmname));
1599         }
1600     }
1601 }
1602
1603
1604 \f
1605 static int
1606 all_digits_p (const char *p, size_t len)
1607 {
1608   if (!len)
1609     return 0; /* No. */
1610   for (; len; len--, p++)
1611     if (!isascii (*p) || !isdigit (*p))
1612       return 0; /* No.  */
1613   return 1; /* Yes.  */
1614 }
1615
1616
1617 /* Collect all error lines from stream FP. Only lines prefixed with
1618    TAG are considered.  Returns a list of error line items (which may
1619    be empty).  There is no error return.  */
1620 static error_line_t
1621 collect_error_output (estream_t fp, const char *tag)
1622 {
1623   char buffer[1024];
1624   char *p, *p2, *p3;
1625   int c, cont_line;
1626   unsigned int pos;
1627   error_line_t eitem, errlines, *errlines_tail;
1628   size_t taglen = strlen (tag);
1629
1630   errlines = NULL;
1631   errlines_tail = &errlines;
1632   pos = 0;
1633   cont_line = 0;
1634   while ((c=es_getc (fp)) != EOF)
1635     {
1636       buffer[pos++] = c;
1637       if (pos >= sizeof buffer - 5 || c == '\n')
1638         {
1639           buffer[pos - (c == '\n')] = 0;
1640           if (cont_line)
1641             ; /*Ignore continuations of previous line. */
1642           else if (!strncmp (buffer, tag, taglen) && buffer[taglen] == ':')
1643             {
1644               /* "gpgsm: foo:4: bla" */
1645               /* Yep, we are interested in this line.  */
1646               p = buffer + taglen + 1;
1647               while (*p == ' ' || *p == '\t')
1648                 p++;
1649               trim_trailing_spaces (p); /* Get rid of extra CRs.  */
1650               if (!*p)
1651                 ; /* Empty lines are ignored.  */
1652               else if ( (p2 = strchr (p, ':')) && (p3 = strchr (p2+1, ':'))
1653                         && all_digits_p (p2+1, p3 - (p2+1)))
1654                 {
1655                   /* Line in standard compiler format.  */
1656                   p3++;
1657                   while (*p3 == ' ' || *p3 == '\t')
1658                     p3++;
1659                   eitem = xmalloc (sizeof *eitem + strlen (p));
1660                   eitem->next = NULL;
1661                   strcpy (eitem->buffer, p);
1662                   eitem->fname = eitem->buffer;
1663                   eitem->buffer[p2-p] = 0;
1664                   eitem->errtext = eitem->buffer + (p3 - p);
1665                   /* (we already checked that there are only ascii
1666                      digits followed by a colon) */
1667                   eitem->lineno = 0;
1668                   for (p2++; isdigit (*p2); p2++)
1669                     eitem->lineno = eitem->lineno*10 + (*p2 - '0');
1670                   *errlines_tail = eitem;
1671                   errlines_tail = &eitem->next;
1672                 }
1673               else
1674                 {
1675                   /* Other error output.  */
1676                   eitem = xmalloc (sizeof *eitem + strlen (p));
1677                   eitem->next = NULL;
1678                   strcpy (eitem->buffer, p);
1679                   eitem->fname = NULL;
1680                   eitem->errtext = eitem->buffer;
1681                   eitem->lineno = 0;
1682                   *errlines_tail = eitem;
1683                   errlines_tail = &eitem->next;
1684                 }
1685             }
1686           pos = 0;
1687           /* If this was not a complete line mark that we are in a
1688              continuation.  */
1689           cont_line = (c != '\n');
1690         }
1691     }
1692
1693   /* We ignore error lines not terminated by a LF.  */
1694   return errlines;
1695 }
1696
1697
1698 /* Check the options of a single component.  Returns 0 if everything
1699    is OK.  */
1700 int
1701 gc_component_check_options (int component, estream_t out, const char *conf_file)
1702 {
1703   gpg_error_t err;
1704   unsigned int result;
1705   int backend_seen[GC_BACKEND_NR];
1706   gc_backend_t backend;
1707   gc_option_t *option;
1708   const char *pgmname;
1709   const char *argv[4];
1710   int i;
1711   pid_t pid;
1712   int exitcode;
1713   estream_t errfp;
1714   error_line_t errlines;
1715
1716   for (backend = 0; backend < GC_BACKEND_NR; backend++)
1717     backend_seen[backend] = 0;
1718
1719   option = gc_component[component].options;
1720   for (; option && option->name; option++)
1721     {
1722       if ((option->flags & GC_OPT_FLAG_GROUP))
1723         continue;
1724       backend = option->backend;
1725       if (backend_seen[backend])
1726         continue;
1727       backend_seen[backend] = 1;
1728       assert (backend != GC_BACKEND_ANY);
1729       if (!gc_backend[backend].program)
1730         continue;
1731       if (!gc_backend[backend].module_name)
1732         continue;
1733
1734       break;
1735     }
1736   if (! option || ! option->name)
1737     return 0;
1738
1739   pgmname = gnupg_module_name (gc_backend[backend].module_name);
1740   i = 0;
1741   if (conf_file)
1742     {
1743       argv[i++] = "--options";
1744       argv[i++] = conf_file;
1745     }
1746   if (component == GC_COMPONENT_PINENTRY)
1747     argv[i++] = "--version";
1748   else
1749     argv[i++] = "--gpgconf-test";
1750   argv[i++] = NULL;
1751
1752   result = 0;
1753   errlines = NULL;
1754   err = gnupg_spawn_process (pgmname, argv, NULL, NULL, 0,
1755                              NULL, NULL, &errfp, &pid);
1756   if (err)
1757     result |= 1; /* Program could not be run.  */
1758   else
1759     {
1760       errlines = collect_error_output (errfp,
1761                                        gc_component[component].name);
1762       if (gnupg_wait_process (pgmname, pid, 1, &exitcode))
1763         {
1764           if (exitcode == -1)
1765             result |= 1; /* Program could not be run or it
1766                             terminated abnormally.  */
1767           result |= 2; /* Program returned an error.  */
1768         }
1769       gnupg_release_process (pid);
1770       es_fclose (errfp);
1771     }
1772
1773   /* If the program could not be run, we can't tell whether
1774      the config file is good.  */
1775   if (result & 1)
1776     result |= 2;
1777
1778   if (out)
1779     {
1780       const char *desc;
1781       error_line_t errptr;
1782
1783       desc = gc_component[component].desc;
1784       desc = my_dgettext (gc_component[component].desc_domain, desc);
1785       es_fprintf (out, "%s:%s:",
1786                   gc_component[component].name, gc_percent_escape (desc));
1787       es_fputs (gc_percent_escape (pgmname), out);
1788       es_fprintf (out, ":%d:%d:", !(result & 1), !(result & 2));
1789       for (errptr = errlines; errptr; errptr = errptr->next)
1790         {
1791           if (errptr != errlines)
1792             es_fputs ("\n:::::", out); /* Continuation line.  */
1793           if (errptr->fname)
1794             es_fputs (gc_percent_escape (errptr->fname), out);
1795           es_putc (':', out);
1796           if (errptr->fname)
1797             es_fprintf (out, "%u", errptr->lineno);
1798           es_putc (':', out);
1799           es_fputs (gc_percent_escape (errptr->errtext), out);
1800           es_putc (':', out);
1801         }
1802       es_putc ('\n', out);
1803     }
1804
1805   while (errlines)
1806     {
1807       error_line_t tmp = errlines->next;
1808       xfree (errlines);
1809       errlines = tmp;
1810     }
1811
1812   return result;
1813 }
1814
1815
1816
1817 /* Check all components that are available.  */
1818 void
1819 gc_check_programs (estream_t out)
1820 {
1821   gc_component_t component;
1822
1823   for (component = 0; component < GC_COMPONENT_NR; component++)
1824     gc_component_check_options (component, out, NULL);
1825 }
1826
1827
1828 \f
1829 /* Find the component with the name NAME.  Returns -1 if not
1830    found.  */
1831 int
1832 gc_component_find (const char *name)
1833 {
1834   gc_component_t idx;
1835
1836   for (idx = 0; idx < GC_COMPONENT_NR; idx++)
1837     {
1838       if (gc_component[idx].options
1839           && !strcmp (name, gc_component[idx].name))
1840         return idx;
1841     }
1842   return -1;
1843 }
1844
1845 \f
1846 /* List the option OPTION.  */
1847 static void
1848 list_one_option (const gc_option_t *option, estream_t out)
1849 {
1850   const char *desc = NULL;
1851   char *arg_name = NULL;
1852
1853   if (option->desc)
1854     {
1855       desc = my_dgettext (option->desc_domain, option->desc);
1856
1857       if (*desc == '|')
1858         {
1859           const char *arg_tail = strchr (&desc[1], '|');
1860
1861           if (arg_tail)
1862             {
1863               int arg_len = arg_tail - &desc[1];
1864               arg_name = xmalloc (arg_len + 1);
1865               memcpy (arg_name, &desc[1], arg_len);
1866               arg_name[arg_len] = '\0';
1867               desc = arg_tail + 1;
1868             }
1869         }
1870     }
1871
1872
1873   /* YOU MUST NOT REORDER THE FIELDS IN THIS OUTPUT, AS THEIR ORDER IS
1874      PART OF THE EXTERNAL INTERFACE.  YOU MUST NOT REMOVE ANY
1875      FIELDS.  */
1876
1877   /* The name field.  */
1878   es_fprintf (out, "%s", option->name);
1879
1880   /* The flags field.  */
1881   es_fprintf (out, ":%lu", option->flags);
1882   if (opt.verbose)
1883     {
1884       es_putc (' ', out);
1885
1886       if (!option->flags)
1887         es_fprintf (out, "none");
1888       else
1889         {
1890           unsigned long flags = option->flags;
1891           unsigned long flag = 0;
1892           unsigned long first = 1;
1893
1894           while (flags)
1895             {
1896               if (flags & 1)
1897                 {
1898                   if (first)
1899                     first = 0;
1900                   else
1901                     es_putc (',', out);
1902                   es_fprintf (out, "%s", gc_flag[flag].name);
1903                 }
1904               flags >>= 1;
1905               flag++;
1906             }
1907         }
1908     }
1909
1910   /* The level field.  */
1911   es_fprintf (out, ":%u", option->level);
1912   if (opt.verbose)
1913     es_fprintf (out, " %s", gc_level[option->level].name);
1914
1915   /* The description field.  */
1916   es_fprintf (out, ":%s", desc ? gc_percent_escape (desc) : "");
1917
1918   /* The type field.  */
1919   es_fprintf (out, ":%u", option->arg_type);
1920   if (opt.verbose)
1921     es_fprintf (out, " %s", gc_arg_type[option->arg_type].name);
1922
1923   /* The alternate type field.  */
1924   es_fprintf (out, ":%u", gc_arg_type[option->arg_type].fallback);
1925   if (opt.verbose)
1926     es_fprintf (out, " %s",
1927                 gc_arg_type[gc_arg_type[option->arg_type].fallback].name);
1928
1929   /* The argument name field.  */
1930   es_fprintf (out, ":%s", arg_name ? gc_percent_escape (arg_name) : "");
1931   xfree (arg_name);
1932
1933   /* The default value field.  */
1934   es_fprintf (out, ":%s", option->default_value ? option->default_value : "");
1935
1936   /* The default argument field.  */
1937   es_fprintf (out, ":%s", option->default_arg ? option->default_arg : "");
1938
1939   /* The value field.  */
1940   if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE
1941       && (option->flags & GC_OPT_FLAG_LIST)
1942       && option->value)
1943     /* The special format "1,1,1,1,...,1" is converted to a number
1944        here.  */
1945     es_fprintf (out, ":%u", (unsigned int)((strlen (option->value) + 1) / 2));
1946   else
1947     es_fprintf (out, ":%s", option->value ? option->value : "");
1948
1949   /* ADD NEW FIELDS HERE.  */
1950
1951   es_putc ('\n', out);
1952 }
1953
1954
1955 /* List all options of the component COMPONENT.  */
1956 void
1957 gc_component_list_options (int component, estream_t out)
1958 {
1959   const gc_option_t *option = gc_component[component].options;
1960
1961   while (option && option->name)
1962     {
1963       /* Do not output unknown or internal options.  */
1964       if (!(option->flags & GC_OPT_FLAG_GROUP)
1965           && (!option->active || option->level == GC_LEVEL_INTERNAL))
1966         {
1967           option++;
1968           continue;
1969         }
1970
1971       if (option->flags & GC_OPT_FLAG_GROUP)
1972         {
1973           const gc_option_t *group_option = option + 1;
1974           gc_expert_level_t level = GC_LEVEL_NR;
1975
1976           /* The manual states that the group level is always the
1977              minimum of the levels of all contained options.  Due to
1978              different active options, and because it is hard to
1979              maintain manually, we calculate it here.  The value in
1980              the global static table is ignored.  */
1981
1982           while (group_option->name)
1983             {
1984               if (group_option->flags & GC_OPT_FLAG_GROUP)
1985                 break;
1986               if (group_option->level < level)
1987                 level = group_option->level;
1988               group_option++;
1989             }
1990
1991           /* Check if group is empty.  */
1992           if (level != GC_LEVEL_NR)
1993             {
1994               gc_option_t opt_copy;
1995
1996               /* Fix up the group level.  */
1997               memcpy (&opt_copy, option, sizeof (opt_copy));
1998               opt_copy.level = level;
1999               list_one_option (&opt_copy, out);
2000             }
2001         }
2002       else
2003         list_one_option (option, out);
2004
2005       option++;
2006     }
2007 }
2008
2009
2010 /* Find the option NAME in component COMPONENT, for the backend
2011    BACKEND.  If BACKEND is GC_BACKEND_ANY, any backend will match.  */
2012 static gc_option_t *
2013 find_option (gc_component_t component, const char *name,
2014              gc_backend_t backend)
2015 {
2016   gc_option_t *option = gc_component[component].options;
2017   while (option->name)
2018     {
2019       if (!(option->flags & GC_OPT_FLAG_GROUP)
2020           && !strcmp (option->name, name)
2021           && (backend == GC_BACKEND_ANY || option->backend == backend))
2022         break;
2023       option++;
2024     }
2025   return option->name ? option : NULL;
2026 }
2027
2028 \f
2029 /* Determine the configuration filename for the component COMPONENT
2030    and backend BACKEND.  */
2031 static char *
2032 get_config_filename (gc_component_t component, gc_backend_t backend)
2033 {
2034   char *filename = NULL;
2035   gc_option_t *option = find_option
2036     (component, gc_backend[backend].option_config_filename, GC_BACKEND_ANY);
2037   assert (option);
2038   assert (option->arg_type == GC_ARG_TYPE_FILENAME);
2039   assert (!(option->flags & GC_OPT_FLAG_LIST));
2040
2041   if (!option->active || !option->default_value)
2042     gc_error (1, 0, "Option %s, needed by backend %s, was not initialized",
2043               gc_backend[backend].option_config_filename,
2044               gc_backend[backend].name);
2045
2046   if (option->value && *option->value)
2047     filename = percent_deescape (&option->value[1]);
2048   else if (option->default_value && *option->default_value)
2049     filename = percent_deescape (&option->default_value[1]);
2050   else
2051     filename = "";
2052
2053 #if HAVE_W32CE_SYSTEM
2054   if (!(filename[0] == '/' || filename[0] == '\\'))
2055 #elif defined(HAVE_DOSISH_SYSTEM)
2056   if (!(filename[0]
2057         && filename[1] == ':'
2058         && (filename[2] == '/' || filename[2] == '\\')))
2059 #else
2060   if (filename[0] != '/')
2061 #endif
2062     gc_error (1, 0, "Option %s, needed by backend %s, is not absolute",
2063               gc_backend[backend].option_config_filename,
2064               gc_backend[backend].name);
2065
2066   return filename;
2067 }
2068
2069 \f
2070 /* Retrieve the options for the component COMPONENT from backend
2071    BACKEND, which we already know is a program-type backend.  */
2072 static void
2073 retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
2074 {
2075   gpg_error_t err;
2076   const char *pgmname;
2077   const char *argv[2];
2078   estream_t outfp;
2079   int exitcode;
2080   pid_t pid;
2081   char *line = NULL;
2082   size_t line_len = 0;
2083   ssize_t length;
2084   estream_t config;
2085   char *config_filename;
2086
2087   pgmname = (gc_backend[backend].module_name
2088              ? gnupg_module_name (gc_backend[backend].module_name)
2089              : gc_backend[backend].program );
2090   argv[0] = "--gpgconf-list";
2091   argv[1] = NULL;
2092
2093   err = gnupg_spawn_process (pgmname, argv, NULL, NULL, 0,
2094                              NULL, &outfp, NULL, &pid);
2095   if (err)
2096     {
2097       gc_error (1, 0, "could not gather active options from '%s': %s",
2098                 pgmname, gpg_strerror (err));
2099     }
2100
2101   while ((length = es_read_line (outfp, &line, &line_len, NULL)) > 0)
2102     {
2103       gc_option_t *option;
2104       char *linep;
2105       unsigned long flags = 0;
2106       char *default_value = NULL;
2107
2108       /* Strip newline and carriage return, if present.  */
2109       while (length > 0
2110              && (line[length - 1] == '\n' || line[length - 1] == '\r'))
2111         line[--length] = '\0';
2112
2113       linep = strchr (line, ':');
2114       if (linep)
2115         *(linep++) = '\0';
2116
2117       /* Extract additional flags.  Default to none.  */
2118       if (linep)
2119         {
2120           char *end;
2121           char *tail;
2122
2123           end = strchr (linep, ':');
2124           if (end)
2125             *(end++) = '\0';
2126
2127           gpg_err_set_errno (0);
2128           flags = strtoul (linep, &tail, 0);
2129           if (errno)
2130             gc_error (1, errno, "malformed flags in option %s from %s",
2131                       line, pgmname);
2132           if (!(*tail == '\0' || *tail == ':' || *tail == ' '))
2133             gc_error (1, 0, "garbage after flags in option %s from %s",
2134                       line, pgmname);
2135
2136           linep = end;
2137         }
2138
2139       /* Extract default value, if present.  Default to empty if
2140          not.  */
2141       if (linep)
2142         {
2143           char *end;
2144
2145           end = strchr (linep, ':');
2146           if (end)
2147             *(end++) = '\0';
2148
2149           if (flags & GC_OPT_FLAG_DEFAULT)
2150             default_value = linep;
2151
2152           linep = end;
2153         }
2154
2155       /* Look up the option in the component and install the
2156          configuration data.  */
2157       option = find_option (component, line, backend);
2158       if (option)
2159         {
2160           if (option->active)
2161             gc_error (1, errno, "option %s returned twice from %s",
2162                       line, pgmname);
2163           option->active = 1;
2164
2165           option->flags |= flags;
2166           if (default_value && *default_value)
2167             option->default_value = xstrdup (default_value);
2168         }
2169     }
2170   if (length < 0 || es_ferror (outfp))
2171     gc_error (1, errno, "error reading from %s", pgmname);
2172   if (es_fclose (outfp))
2173     gc_error (1, errno, "error closing %s", pgmname);
2174
2175   err = gnupg_wait_process (pgmname, pid, 1, &exitcode);
2176   if (err)
2177     gc_error (1, 0, "running %s failed (exitcode=%d): %s",
2178               pgmname, exitcode, gpg_strerror (err));
2179   gnupg_release_process (pid);
2180
2181
2182   /* At this point, we can parse the configuration file.  */
2183   config_filename = get_config_filename (component, backend);
2184
2185   config = es_fopen (config_filename, "r");
2186   if (!config)
2187     {
2188       if (errno != ENOENT)
2189         gc_error (0, errno, "warning: can not open config file %s",
2190                   config_filename);
2191     }
2192   else
2193     {
2194       while ((length = es_read_line (config, &line, &line_len, NULL)) > 0)
2195         {
2196           char *name;
2197           char *value;
2198           gc_option_t *option;
2199
2200           name = line;
2201           while (*name == ' ' || *name == '\t')
2202             name++;
2203           if (!*name || *name == '#' || *name == '\r' || *name == '\n')
2204             continue;
2205
2206           value = name;
2207           while (*value && *value != ' ' && *value != '\t'
2208                  && *value != '#' && *value != '\r' && *value != '\n')
2209             value++;
2210           if (*value == ' ' || *value == '\t')
2211             {
2212               char *end;
2213
2214               *(value++) = '\0';
2215               while (*value == ' ' || *value == '\t')
2216                 value++;
2217
2218               end = value;
2219               while (*end && *end != '#' && *end != '\r' && *end != '\n')
2220                 end++;
2221               while (end > value && (end[-1] == ' ' || end[-1] == '\t'))
2222                 end--;
2223               *end = '\0';
2224             }
2225           else
2226             *value = '\0';
2227
2228           /* Look up the option in the component and install the
2229              configuration data.  */
2230           option = find_option (component, line, backend);
2231           if (option)
2232             {
2233               char *opt_value;
2234
2235               if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE)
2236                 {
2237                   if (*value)
2238                     gc_error (0, 0,
2239                               "warning: ignoring argument %s for option %s",
2240                               value, name);
2241                   opt_value = xstrdup ("1");
2242                 }
2243               else if (gc_arg_type[option->arg_type].fallback
2244                        == GC_ARG_TYPE_STRING)
2245                 opt_value = xasprintf ("\"%s", gc_percent_escape (value));
2246               else
2247                 {
2248                   /* FIXME: Verify that the number is sane.  */
2249                   opt_value = xstrdup (value);
2250                 }
2251
2252               /* Now enter the option into the table.  */
2253               if (!(option->flags & GC_OPT_FLAG_LIST))
2254                 {
2255                   if (option->value)
2256                     xfree (option->value);
2257                   option->value = opt_value;
2258                 }
2259               else
2260                 {
2261                   if (!option->value)
2262                     option->value = opt_value;
2263                   else
2264                     {
2265                       char *old = option->value;
2266                       option->value = xasprintf ("%s,%s", old, opt_value);
2267                       xfree (old);
2268                       xfree (opt_value);
2269                     }
2270                 }
2271             }
2272         }
2273
2274       if (length < 0 || es_ferror (config))
2275         gc_error (1, errno, "error reading from %s", config_filename);
2276       if (es_fclose (config))
2277         gc_error (1, errno, "error closing %s", config_filename);
2278     }
2279
2280   xfree (line);
2281 }
2282
2283
2284 /* Retrieve the options for the component COMPONENT from backend
2285    BACKEND, which we already know is of type file list.  */
2286 static void
2287 retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
2288 {
2289   gc_option_t *list_option;
2290   gc_option_t *config_option;
2291   char *list_filename;
2292   gpgrt_stream_t list_file;
2293   char *line = NULL;
2294   size_t line_len = 0;
2295   ssize_t length;
2296   char *list = NULL;
2297
2298   list_option = find_option (component,
2299                              gc_backend[backend].option_name, GC_BACKEND_ANY);
2300   assert (list_option);
2301   assert (!list_option->active);
2302
2303   list_filename = get_config_filename (component, backend);
2304   list_file = gpgrt_fopen (list_filename, "r");
2305   if (!list_file)
2306     gc_error (0, errno, "warning: can not open list file %s", list_filename);
2307   else
2308     {
2309
2310       while ((length = gpgrt_read_line (list_file, &line, &line_len, NULL)) > 0)
2311         {
2312           char *start;
2313           char *end;
2314           char *new_list;
2315
2316           start = line;
2317           while (*start == ' ' || *start == '\t')
2318             start++;
2319           if (!*start || *start == '#' || *start == '\r' || *start == '\n')
2320             continue;
2321
2322           end = start;
2323           while (*end && *end != '#' && *end != '\r' && *end != '\n')
2324             end++;
2325           /* Walk back to skip trailing white spaces.  Looks evil, but
2326              works because of the conditions on START and END imposed
2327              at this point (END is at least START + 1, and START is
2328              not a whitespace character).  */
2329           while (*(end - 1) == ' ' || *(end - 1) == '\t')
2330             end--;
2331           *end = '\0';
2332           /* FIXME: Oh, no!  This is so lame!  Should use realloc and
2333              really append.  */
2334           if (list)
2335             {
2336               new_list = xasprintf ("%s,\"%s", list, gc_percent_escape (start));
2337               xfree (list);
2338               list = new_list;
2339             }
2340           else
2341             list = xasprintf ("\"%s", gc_percent_escape (start));
2342         }
2343       if (length < 0 || gpgrt_ferror (list_file))
2344         gc_error (1, errno, "can not read list file %s", list_filename);
2345     }
2346
2347   list_option->active = 1;
2348   list_option->value = list;
2349
2350   /* Fix up the read-only flag.  */
2351   config_option = find_option
2352     (component, gc_backend[backend].option_config_filename, GC_BACKEND_ANY);
2353   if (config_option->flags & GC_OPT_FLAG_NO_CHANGE)
2354     list_option->flags |= GC_OPT_FLAG_NO_CHANGE;
2355
2356   if (list_file && gpgrt_fclose (list_file))
2357     gc_error (1, errno, "error closing %s", list_filename);
2358   xfree (line);
2359 }
2360
2361
2362 /* Retrieve the currently active options and their defaults from all
2363    involved backends for this component.  Using -1 for component will
2364    retrieve all options from all components. */
2365 void
2366 gc_component_retrieve_options (int component)
2367 {
2368   int process_all = 0;
2369   int backend_seen[GC_BACKEND_NR];
2370   gc_backend_t backend;
2371   gc_option_t *option;
2372
2373   for (backend = 0; backend < GC_BACKEND_NR; backend++)
2374     backend_seen[backend] = 0;
2375
2376   if (component == -1)
2377     {
2378       process_all = 1;
2379       component = 0;
2380       assert (component < GC_COMPONENT_NR);
2381     }
2382
2383   do
2384     {
2385       if (component == GC_COMPONENT_PINENTRY)
2386         continue; /* Skip this dummy component.  */
2387
2388       option = gc_component[component].options;
2389
2390       while (option && option->name)
2391         {
2392           if (!(option->flags & GC_OPT_FLAG_GROUP))
2393             {
2394               backend = option->backend;
2395
2396               if (backend_seen[backend])
2397                 {
2398                   option++;
2399                   continue;
2400                 }
2401               backend_seen[backend] = 1;
2402
2403               assert (backend != GC_BACKEND_ANY);
2404
2405               if (gc_backend[backend].program)
2406                 retrieve_options_from_program (component, backend);
2407               else
2408                 retrieve_options_from_file (component, backend);
2409             }
2410           option++;
2411         }
2412     }
2413   while (process_all && ++component < GC_COMPONENT_NR);
2414
2415 }
2416
2417
2418 \f
2419 /* Perform a simple validity check based on the type.  Return in
2420  * NEW_VALUE_NR the value of the number in NEW_VALUE if OPTION is of
2421  * type GC_ARG_TYPE_NONE.  If VERBATIM is set the profile parsing mode
2422  * is used. */
2423 static void
2424 option_check_validity (gc_option_t *option, unsigned long flags,
2425                        char *new_value, unsigned long *new_value_nr,
2426                        int verbatim)
2427 {
2428   char *arg;
2429
2430   if (!option->active)
2431     gc_error (1, 0, "option %s not supported by backend %s",
2432               option->name, gc_backend[option->backend].name);
2433
2434   if (option->new_flags || option->new_value)
2435     gc_error (1, 0, "option %s already changed", option->name);
2436
2437   if (flags & GC_OPT_FLAG_DEFAULT)
2438     {
2439       if (*new_value)
2440         gc_error (1, 0, "argument %s provided for deleted option %s",
2441                   new_value, option->name);
2442
2443       return;
2444     }
2445
2446   /* GC_ARG_TYPE_NONE options have special list treatment.  */
2447   if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE)
2448     {
2449       char *tail;
2450
2451       gpg_err_set_errno (0);
2452       *new_value_nr = strtoul (new_value, &tail, 0);
2453
2454       if (errno)
2455         gc_error (1, errno, "invalid argument for option %s",
2456                   option->name);
2457       if (*tail)
2458         gc_error (1, 0, "garbage after argument for option %s",
2459                       option->name);
2460
2461       if (!(option->flags & GC_OPT_FLAG_LIST))
2462         {
2463           if (*new_value_nr != 1)
2464             gc_error (1, 0, "argument for non-list option %s of type 0 "
2465                       "(none) must be 1", option->name);
2466         }
2467       else
2468         {
2469           if (*new_value_nr == 0)
2470             gc_error (1, 0, "argument for option %s of type 0 (none) "
2471                       "must be positive", option->name);
2472         }
2473
2474       return;
2475     }
2476
2477   arg = new_value;
2478   do
2479     {
2480       if (*arg == '\0' || (*arg == ',' && !verbatim))
2481         {
2482           if (!(option->flags & GC_OPT_FLAG_ARG_OPT))
2483             gc_error (1, 0, "argument required for option %s", option->name);
2484
2485           if (*arg == ',' && !verbatim && !(option->flags & GC_OPT_FLAG_LIST))
2486             gc_error (1, 0, "list found for non-list option %s", option->name);
2487         }
2488       else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_STRING)
2489         {
2490           if (*arg != '"' && !verbatim)
2491             gc_error (1, 0, "string argument for option %s must begin "
2492                       "with a quote (\") character", option->name);
2493
2494           /* FIXME: We do not allow empty string arguments for now, as
2495              we do not quote arguments in configuration files, and
2496              thus no argument is indistinguishable from the empty
2497              string.  */
2498           if (arg[1] == '\0' || (arg[1] == ',' && !verbatim))
2499             gc_error (1, 0, "empty string argument for option %s is "
2500                       "currently not allowed.  Please report this!",
2501                       option->name);
2502         }
2503       else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_INT32)
2504         {
2505           long res;
2506
2507           gpg_err_set_errno (0);
2508           res = strtol (arg, &arg, 0);
2509           (void) res;
2510
2511           if (errno)
2512             gc_error (1, errno, "invalid argument for option %s",
2513                       option->name);
2514
2515           if (*arg != '\0' && (*arg != ',' || verbatim))
2516             gc_error (1, 0, "garbage after argument for option %s",
2517                       option->name);
2518         }
2519       else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_UINT32)
2520         {
2521           unsigned long res;
2522
2523           gpg_err_set_errno (0);
2524           res = strtoul (arg, &arg, 0);
2525           (void) res;
2526
2527           if (errno)
2528             gc_error (1, errno, "invalid argument for option %s",
2529                       option->name);
2530
2531           if (*arg != '\0' && (*arg != ',' || verbatim))
2532             gc_error (1, 0, "garbage after argument for option %s",
2533                       option->name);
2534         }
2535       arg = verbatim? strchr (arg, ',') : NULL;
2536       if (arg)
2537         arg++;
2538     }
2539   while (arg && *arg);
2540 }
2541
2542 #ifdef HAVE_W32_SYSTEM
2543 int
2544 copy_file (const char *src_name, const char *dst_name)
2545 {
2546 #define BUF_LEN 4096
2547   char buffer[BUF_LEN];
2548   int len;
2549   gpgrt_stream_t src;
2550   gpgrt_stream_t dst;
2551
2552   src = gpgrt_fopen (src_name, "r");
2553   if (src == NULL)
2554     return -1;
2555
2556   dst = gpgrt_fopen (dst_name, "w");
2557   if (dst == NULL)
2558     {
2559       int saved_err = errno;
2560       gpgrt_fclose (src);
2561       gpg_err_set_errno (saved_err);
2562       return -1;
2563     }
2564
2565   do
2566     {
2567       int written;
2568
2569       len = gpgrt_fread (buffer, 1, BUF_LEN, src);
2570       if (len == 0)
2571         break;
2572       written = gpgrt_fwrite (buffer, 1, len, dst);
2573       if (written != len)
2574         break;
2575     }
2576   while (! gpgrt_feof (src) && ! gpgrt_ferror (src) && ! gpgrt_ferror (dst));
2577
2578   if (gpgrt_ferror (src) || gpgrt_ferror (dst) || ! gpgrt_feof (src))
2579     {
2580       int saved_errno = errno;
2581       gpgrt_fclose (src);
2582       gpgrt_fclose (dst);
2583       unlink (dst_name);
2584       gpg_err_set_errno (saved_errno);
2585       return -1;
2586     }
2587
2588   if (gpgrt_fclose (dst))
2589     gc_error (1, errno, "error closing %s", dst_name);
2590   if (gpgrt_fclose (src))
2591     gc_error (1, errno, "error closing %s", src_name);
2592
2593   return 0;
2594 }
2595 #endif /* HAVE_W32_SYSTEM */
2596
2597
2598 /* Create and verify the new configuration file for the specified
2599  * backend and component.  Returns 0 on success and -1 on error.  This
2600  * function may store pointers to malloced strings in SRC_FILENAMEP,
2601  * DEST_FILENAMEP, and ORIG_FILENAMEP.  Those must be freed by the
2602  * caller.  The strings refer to three versions of the configuration
2603  * file:
2604  *
2605  * SRC_FILENAME:  The updated configuration is written to this file.
2606  * DEST_FILENAME: Name of the configuration file read by the
2607  *                component.
2608  * ORIG_FILENAME: A backup of the previous configuration file.
2609  *
2610  * To apply the configuration change, rename SRC_FILENAME to
2611  * DEST_FILENAME.  To revert to the previous configuration, rename
2612  * ORIG_FILENAME to DEST_FILENAME.  */
2613 static int
2614 change_options_file (gc_component_t component, gc_backend_t backend,
2615                      char **src_filenamep, char **dest_filenamep,
2616                      char **orig_filenamep)
2617 {
2618   static const char marker[] = "###+++--- " GPGCONF_DISP_NAME " ---+++###";
2619   /* True if we are within the marker in the config file.  */
2620   int in_marker = 0;
2621   gc_option_t *option;
2622   char *line = NULL;
2623   size_t line_len;
2624   ssize_t length;
2625   int res;
2626   int fd;
2627   gpgrt_stream_t src_file = NULL;
2628   gpgrt_stream_t dest_file = NULL;
2629   char *src_filename;
2630   char *dest_filename;
2631   char *orig_filename;
2632   char *arg;
2633   char *cur_arg = NULL;
2634
2635   option = find_option (component,
2636                         gc_backend[backend].option_name, GC_BACKEND_ANY);
2637   assert (option);
2638   assert (option->active);
2639   assert (gc_arg_type[option->arg_type].fallback != GC_ARG_TYPE_NONE);
2640
2641   /* FIXME.  Throughout the function, do better error reporting.  */
2642   /* Note that get_config_filename() calls percent_deescape(), so we
2643      call this before processing the arguments.  */
2644   dest_filename = xstrdup (get_config_filename (component, backend));
2645   src_filename = xasprintf ("%s.%s.%i.new",
2646                             dest_filename, GPGCONF_NAME, (int)getpid ());
2647   orig_filename = xasprintf ("%s.%s.%i.bak",
2648                              dest_filename, GPGCONF_NAME, (int)getpid ());
2649
2650   arg = option->new_value;
2651   if (arg && arg[0] == '\0')
2652     arg = NULL;
2653   else if (arg)
2654     {
2655       char *end;
2656
2657       arg++;
2658       end = strchr (arg, ',');
2659       if (end)
2660         *end = '\0';
2661
2662       cur_arg = percent_deescape (arg);
2663       if (end)
2664         {
2665           *end = ',';
2666           arg = end + 1;
2667         }
2668       else
2669         arg = NULL;
2670     }
2671
2672 #ifdef HAVE_W32_SYSTEM
2673   res = copy_file (dest_filename, orig_filename);
2674 #else
2675   res = link (dest_filename, orig_filename);
2676 #endif
2677   if (res < 0 && errno != ENOENT)
2678     {
2679       xfree (dest_filename);
2680       xfree (src_filename);
2681       xfree (orig_filename);
2682       return -1;
2683     }
2684   if (res < 0)
2685     {
2686       xfree (orig_filename);
2687       orig_filename = NULL;
2688     }
2689
2690   /* We now initialize the return strings, so the caller can do the
2691      cleanup for us.  */
2692   *src_filenamep = src_filename;
2693   *dest_filenamep = dest_filename;
2694   *orig_filenamep = orig_filename;
2695
2696   /* Use open() so that we can use O_EXCL.  */
2697   fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
2698   if (fd < 0)
2699     return -1;
2700   src_file = gpgrt_fdopen (fd, "w");
2701   res = errno;
2702   if (!src_file)
2703     {
2704       gpg_err_set_errno (res);
2705       return -1;
2706     }
2707
2708   /* Only if ORIG_FILENAME is not NULL did the configuration file
2709      exist already.  In this case, we will copy its content into the
2710      new configuration file, changing it to our liking in the
2711      process.  */
2712   if (orig_filename)
2713     {
2714       dest_file = gpgrt_fopen (dest_filename, "r");
2715       if (!dest_file)
2716         goto change_file_one_err;
2717
2718       while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
2719         {
2720           int disable = 0;
2721           char *start;
2722
2723           if (!strncmp (marker, line, sizeof (marker) - 1))
2724             {
2725               if (!in_marker)
2726                 in_marker = 1;
2727               else
2728                 break;
2729             }
2730
2731           start = line;
2732           while (*start == ' ' || *start == '\t')
2733             start++;
2734           if (*start && *start != '\r' && *start != '\n' && *start != '#')
2735             {
2736               char *end;
2737               char *endp;
2738               char saved_end;
2739
2740               endp = start;
2741               end = endp;
2742
2743               /* Search for the end of the line.  */
2744               while (*endp && *endp != '#' && *endp != '\r' && *endp != '\n')
2745                 {
2746                   endp++;
2747                   if (*endp && *endp != ' ' && *endp != '\t'
2748                       && *endp != '\r' && *endp != '\n' && *endp != '#')
2749                     end = endp + 1;
2750                 }
2751               saved_end = *end;
2752               *end = '\0';
2753
2754               if ((option->new_flags & GC_OPT_FLAG_DEFAULT)
2755                   || !cur_arg || strcmp (start, cur_arg))
2756                 disable = 1;
2757               else
2758                 {
2759                   /* Find next argument.  */
2760                   if (arg)
2761                     {
2762                       char *arg_end;
2763
2764                       arg++;
2765                       arg_end = strchr (arg, ',');
2766                       if (arg_end)
2767                         *arg_end = '\0';
2768
2769                       cur_arg = percent_deescape (arg);
2770                       if (arg_end)
2771                         {
2772                           *arg_end = ',';
2773                           arg = arg_end + 1;
2774                         }
2775                       else
2776                         arg = NULL;
2777                     }
2778                   else
2779                     cur_arg = NULL;
2780                 }
2781
2782               *end = saved_end;
2783             }
2784
2785           if (disable)
2786             {
2787               if (!in_marker)
2788                 {
2789                   gpgrt_fprintf (src_file,
2790                            "# %s disabled this option here at %s\n",
2791                            GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
2792                   if (gpgrt_ferror (src_file))
2793                     goto change_file_one_err;
2794                   gpgrt_fprintf (src_file, "# %s", line);
2795                   if (gpgrt_ferror (src_file))
2796                     goto change_file_one_err;
2797                 }
2798             }
2799           else
2800             {
2801               gpgrt_fprintf (src_file, "%s", line);
2802               if (gpgrt_ferror (src_file))
2803                 goto change_file_one_err;
2804             }
2805         }
2806       if (length < 0 || gpgrt_ferror (dest_file))
2807         goto change_file_one_err;
2808     }
2809
2810   if (!in_marker)
2811     {
2812       /* There was no marker.  This is the first time we edit the
2813          file.  We add our own marker at the end of the file and
2814          proceed.  Note that we first write a newline, this guards us
2815          against files which lack the newline at the end of the last
2816          line, while it doesn't hurt us in all other cases.  */
2817       gpgrt_fprintf (src_file, "\n%s\n", marker);
2818       if (gpgrt_ferror (src_file))
2819         goto change_file_one_err;
2820     }
2821
2822   /* At this point, we have copied everything up to the end marker
2823      into the new file, except for the arguments we are going to add.
2824      Now, dump the new arguments and write the end marker, possibly
2825      followed by the rest of the original file.  */
2826   while (cur_arg)
2827     {
2828       gpgrt_fprintf (src_file, "%s\n", cur_arg);
2829
2830       /* Find next argument.  */
2831       if (arg)
2832         {
2833           char *end;
2834
2835           arg++;
2836           end = strchr (arg, ',');
2837           if (end)
2838             *end = '\0';
2839
2840           cur_arg = percent_deescape (arg);
2841           if (end)
2842             {
2843               *end = ',';
2844               arg = end + 1;
2845             }
2846           else
2847             arg = NULL;
2848         }
2849       else
2850         cur_arg = NULL;
2851     }
2852
2853   gpgrt_fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
2854   if (gpgrt_ferror (src_file))
2855     goto change_file_one_err;
2856
2857   if (!in_marker)
2858     {
2859       gpgrt_fprintf (src_file, "# %s edited this configuration file.\n",
2860                GPGCONF_DISP_NAME);
2861       if (gpgrt_ferror (src_file))
2862         goto change_file_one_err;
2863       gpgrt_fprintf (src_file, "# It will disable options before this marked "
2864                "block, but it will\n");
2865       if (gpgrt_ferror (src_file))
2866         goto change_file_one_err;
2867       gpgrt_fprintf (src_file, "# never change anything below these lines.\n");
2868       if (gpgrt_ferror (src_file))
2869         goto change_file_one_err;
2870     }
2871   if (dest_file)
2872     {
2873       while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
2874         {
2875           gpgrt_fprintf (src_file, "%s", line);
2876           if (gpgrt_ferror (src_file))
2877             goto change_file_one_err;
2878         }
2879       if (length < 0 || gpgrt_ferror (dest_file))
2880         goto change_file_one_err;
2881     }
2882   xfree (line);
2883   line = NULL;
2884
2885   res = gpgrt_fclose (src_file);
2886   if (res)
2887     {
2888       res = errno;
2889       close (fd);
2890       if (dest_file)
2891         gpgrt_fclose (dest_file);
2892       gpg_err_set_errno (res);
2893       return -1;
2894     }
2895   close (fd);
2896   if (dest_file)
2897     {
2898       res = gpgrt_fclose (dest_file);
2899       if (res)
2900         return -1;
2901     }
2902   return 0;
2903
2904  change_file_one_err:
2905   xfree (line);
2906   res = errno;
2907   if (src_file)
2908     {
2909       gpgrt_fclose (src_file);
2910       close (fd);
2911     }
2912   if (dest_file)
2913     gpgrt_fclose (dest_file);
2914   gpg_err_set_errno (res);
2915   return -1;
2916 }
2917
2918
2919 /* Create and verify the new configuration file for the specified
2920  * backend and component.  Returns 0 on success and -1 on error.  If
2921  * VERBATIM is set the profile mode is used.  This function may store
2922  * pointers to malloced strings in SRC_FILENAMEP, DEST_FILENAMEP, and
2923  * ORIG_FILENAMEP.  Those must be freed by the caller.  The strings
2924  * refer to three versions of the configuration file:
2925  *
2926  * SRC_FILENAME:  The updated configuration is written to this file.
2927  * DEST_FILENAME: Name of the configuration file read by the
2928  *                component.
2929  * ORIG_FILENAME: A backup of the previous configuration file.
2930  *
2931  * To apply the configuration change, rename SRC_FILENAME to
2932  * DEST_FILENAME.  To revert to the previous configuration, rename
2933  * ORIG_FILENAME to DEST_FILENAME.  */
2934 static int
2935 change_options_program (gc_component_t component, gc_backend_t backend,
2936                         char **src_filenamep, char **dest_filenamep,
2937                         char **orig_filenamep,
2938                         int verbatim)
2939 {
2940   static const char marker[] = "###+++--- " GPGCONF_DISP_NAME " ---+++###";
2941   /* True if we are within the marker in the config file.  */
2942   int in_marker = 0;
2943   gc_option_t *option;
2944   char *line = NULL;
2945   size_t line_len;
2946   ssize_t length;
2947   int res;
2948   int fd;
2949   gpgrt_stream_t src_file = NULL;
2950   gpgrt_stream_t dest_file = NULL;
2951   char *src_filename;
2952   char *dest_filename;
2953   char *orig_filename;
2954   /* Special hack for gpg, see below.  */
2955   int utf8strings_seen = 0;
2956
2957   /* FIXME.  Throughout the function, do better error reporting.  */
2958   dest_filename = xstrdup (get_config_filename (component, backend));
2959   src_filename = xasprintf ("%s.%s.%i.new",
2960                             dest_filename, GPGCONF_NAME, (int)getpid ());
2961   orig_filename = xasprintf ("%s.%s.%i.bak",
2962                              dest_filename, GPGCONF_NAME, (int)getpid ());
2963
2964 #ifdef HAVE_W32_SYSTEM
2965   res = copy_file (dest_filename, orig_filename);
2966 #else
2967   res = link (dest_filename, orig_filename);
2968 #endif
2969   if (res < 0 && errno != ENOENT)
2970     {
2971       xfree (dest_filename);
2972       xfree (src_filename);
2973       xfree (orig_filename);
2974       return -1;
2975     }
2976   if (res < 0)
2977     {
2978       xfree (orig_filename);
2979       orig_filename = NULL;
2980     }
2981
2982   /* We now initialize the return strings, so the caller can do the
2983      cleanup for us.  */
2984   *src_filenamep = src_filename;
2985   *dest_filenamep = dest_filename;
2986   *orig_filenamep = orig_filename;
2987
2988   /* Use open() so that we can use O_EXCL.  */
2989   fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
2990   if (fd < 0)
2991     return -1;
2992   src_file = gpgrt_fdopen (fd, "w");
2993   res = errno;
2994   if (!src_file)
2995     {
2996       gpg_err_set_errno (res);
2997       return -1;
2998     }
2999
3000   /* Only if ORIG_FILENAME is not NULL did the configuration file
3001      exist already.  In this case, we will copy its content into the
3002      new configuration file, changing it to our liking in the
3003      process.  */
3004   if (orig_filename)
3005     {
3006       dest_file = gpgrt_fopen (dest_filename, "r");
3007       if (!dest_file)
3008         goto change_one_err;
3009
3010       while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
3011         {
3012           int disable = 0;
3013           char *start;
3014
3015           if (!strncmp (marker, line, sizeof (marker) - 1))
3016             {
3017               if (!in_marker)
3018                 in_marker = 1;
3019               else
3020                 break;
3021             }
3022           else if (backend == GC_BACKEND_GPG && in_marker
3023                    && ! strcmp ("utf8-strings\n", line))
3024             {
3025               /* Strip duplicated entries.  */
3026               if (utf8strings_seen)
3027                 disable = 1;
3028               else
3029                 utf8strings_seen = 1;
3030             }
3031
3032           start = line;
3033           while (*start == ' ' || *start == '\t')
3034             start++;
3035           if (*start && *start != '\r' && *start != '\n' && *start != '#')
3036             {
3037               char *end;
3038               char saved_end;
3039
3040               end = start;
3041               while (*end && *end != ' ' && *end != '\t'
3042                      && *end != '\r' && *end != '\n' && *end != '#')
3043                 end++;
3044               saved_end = *end;
3045               *end = '\0';
3046
3047               option = find_option (component, start, backend);
3048               *end = saved_end;
3049               if (option && ((option->new_flags & GC_OPT_FLAG_DEFAULT)
3050                              || option->new_value))
3051                 disable = 1;
3052             }
3053           if (disable)
3054             {
3055               if (!in_marker)
3056                 {
3057                   gpgrt_fprintf (src_file,
3058                            "# %s disabled this option here at %s\n",
3059                            GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
3060                   if (gpgrt_ferror (src_file))
3061                     goto change_one_err;
3062                   gpgrt_fprintf (src_file, "# %s", line);
3063                   if (gpgrt_ferror (src_file))
3064                     goto change_one_err;
3065                 }
3066             }
3067           else
3068             {
3069               gpgrt_fprintf (src_file, "%s", line);
3070               if (gpgrt_ferror (src_file))
3071                 goto change_one_err;
3072             }
3073         }
3074       if (length < 0 || gpgrt_ferror (dest_file))
3075         goto change_one_err;
3076     }
3077
3078   if (!in_marker)
3079     {
3080       /* There was no marker.  This is the first time we edit the
3081          file.  We add our own marker at the end of the file and
3082          proceed.  Note that we first write a newline, this guards us
3083          against files which lack the newline at the end of the last
3084          line, while it doesn't hurt us in all other cases.  */
3085       gpgrt_fprintf (src_file, "\n%s\n", marker);
3086       if (gpgrt_ferror (src_file))
3087         goto change_one_err;
3088     }
3089   /* At this point, we have copied everything up to the end marker
3090      into the new file, except for the options we are going to change.
3091      Now, dump the changed options (except for those we are going to
3092      revert to their default), and write the end marker, possibly
3093      followed by the rest of the original file.  */
3094
3095   /* We have to turn on UTF8 strings for GnuPG.  */
3096   if (backend == GC_BACKEND_GPG && ! utf8strings_seen)
3097     gpgrt_fprintf (src_file, "utf8-strings\n");
3098
3099   option = gc_component[component].options;
3100   while (option->name)
3101     {
3102       if (!(option->flags & GC_OPT_FLAG_GROUP)
3103           && option->backend == backend
3104           && option->new_value)
3105         {
3106           char *arg = option->new_value;
3107
3108           do
3109             {
3110               if (*arg == '\0' || *arg == ',')
3111                 {
3112                   gpgrt_fprintf (src_file, "%s\n", option->name);
3113                   if (gpgrt_ferror (src_file))
3114                     goto change_one_err;
3115                 }
3116               else if (gc_arg_type[option->arg_type].fallback
3117                        == GC_ARG_TYPE_NONE)
3118                 {
3119                   assert (*arg == '1');
3120                   gpgrt_fprintf (src_file, "%s\n", option->name);
3121                   if (gpgrt_ferror (src_file))
3122                     goto change_one_err;
3123
3124                   arg++;
3125                 }
3126               else if (gc_arg_type[option->arg_type].fallback
3127                        == GC_ARG_TYPE_STRING)
3128                 {
3129                   char *end;
3130
3131                   if (!verbatim)
3132                     {
3133                       log_assert (*arg == '"');
3134                       arg++;
3135
3136                       end = strchr (arg, ',');
3137                       if (end)
3138                         *end = '\0';
3139                     }
3140                   else
3141                     end = NULL;
3142
3143                   gpgrt_fprintf (src_file, "%s %s\n", option->name,
3144                            verbatim? arg : percent_deescape (arg));
3145                   if (gpgrt_ferror (src_file))
3146                     goto change_one_err;
3147
3148                   if (end)
3149                     *end = ',';
3150                   arg = end;
3151                 }
3152               else
3153                 {
3154                   char *end;
3155
3156                   end = strchr (arg, ',');
3157                   if (end)
3158                     *end = '\0';
3159
3160                   gpgrt_fprintf (src_file, "%s %s\n", option->name, arg);
3161                   if (gpgrt_ferror (src_file))
3162                     goto change_one_err;
3163
3164                   if (end)
3165                     *end = ',';
3166                   arg = end;
3167                 }
3168
3169               assert (arg == NULL || *arg == '\0' || *arg == ',');
3170               if (arg && *arg == ',')
3171                 arg++;
3172             }
3173           while (arg && *arg);
3174         }
3175       option++;
3176     }
3177
3178   gpgrt_fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
3179   if (gpgrt_ferror (src_file))
3180     goto change_one_err;
3181
3182   if (!in_marker)
3183     {
3184       gpgrt_fprintf (src_file, "# %s edited this configuration file.\n",
3185                GPGCONF_DISP_NAME);
3186       if (gpgrt_ferror (src_file))
3187         goto change_one_err;
3188       gpgrt_fprintf (src_file, "# It will disable options before this marked "
3189                "block, but it will\n");
3190       if (gpgrt_ferror (src_file))
3191         goto change_one_err;
3192       gpgrt_fprintf (src_file, "# never change anything below these lines.\n");
3193       if (gpgrt_ferror (src_file))
3194         goto change_one_err;
3195     }
3196   if (dest_file)
3197     {
3198       while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
3199         {
3200           gpgrt_fprintf (src_file, "%s", line);
3201           if (gpgrt_ferror (src_file))
3202             goto change_one_err;
3203         }
3204       if (length < 0 || gpgrt_ferror (dest_file))
3205         goto change_one_err;
3206     }
3207   xfree (line);
3208   line = NULL;
3209
3210   res = gpgrt_fclose (src_file);
3211   if (res)
3212     {
3213       res = errno;
3214       close (fd);
3215       if (dest_file)
3216         gpgrt_fclose (dest_file);
3217       gpg_err_set_errno (res);
3218       return -1;
3219     }
3220   close (fd);
3221   if (dest_file)
3222     {
3223       res = gpgrt_fclose (dest_file);
3224       if (res)
3225         return -1;
3226     }
3227   return 0;
3228
3229  change_one_err:
3230   xfree (line);
3231   res = errno;
3232   if (src_file)
3233     {
3234       gpgrt_fclose (src_file);
3235       close (fd);
3236     }
3237   if (dest_file)
3238     gpgrt_fclose (dest_file);
3239   gpg_err_set_errno (res);
3240   return -1;
3241 }
3242
3243
3244 /* Common code for gc_component_change_options and
3245  * gc_process_gpgconf_conf.  If VERBATIM is set the profile parsing
3246  * mode is used.  */
3247 static void
3248 change_one_value (gc_option_t *option, int *runtime,
3249                   unsigned long flags, char *new_value, int verbatim)
3250 {
3251   unsigned long new_value_nr = 0;
3252
3253   option_check_validity (option, flags, new_value, &new_value_nr, verbatim);
3254
3255   if (option->flags & GC_OPT_FLAG_RUNTIME)
3256     runtime[option->backend] = 1;
3257
3258   option->new_flags = flags;
3259   if (!(flags & GC_OPT_FLAG_DEFAULT))
3260     {
3261       if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE
3262           && (option->flags & GC_OPT_FLAG_LIST))
3263         {
3264           char *str;
3265
3266           /* We convert the number to a list of 1's for convenient
3267              list handling.  */
3268           assert (new_value_nr > 0);
3269           option->new_value = xmalloc ((2 * (new_value_nr - 1) + 1) + 1);
3270           str = option->new_value;
3271           *(str++) = '1';
3272           while (--new_value_nr > 0)
3273             {
3274               *(str++) = ',';
3275               *(str++) = '1';
3276             }
3277           *(str++) = '\0';
3278         }
3279       else
3280         option->new_value = xstrdup (new_value);
3281     }
3282 }
3283
3284
3285 /* Read the modifications from IN and apply them.  If IN is NULL the
3286    modifications are expected to already have been set to the global
3287    table.  If VERBATIM is set the profile mode is used.  */
3288 void
3289 gc_component_change_options (int component, estream_t in, estream_t out,
3290                              int verbatim)
3291 {
3292   int err = 0;
3293   int block = 0;
3294   int runtime[GC_BACKEND_NR];
3295   char *src_filename[GC_BACKEND_NR];
3296   char *dest_filename[GC_BACKEND_NR];
3297   char *orig_filename[GC_BACKEND_NR];
3298   gc_backend_t backend;
3299   gc_option_t *option;
3300   char *line = NULL;
3301   size_t line_len = 0;
3302   ssize_t length;
3303
3304   if (component == GC_COMPONENT_PINENTRY)
3305     return; /* Dummy component for now.  */
3306
3307   for (backend = 0; backend < GC_BACKEND_NR; backend++)
3308     {
3309       runtime[backend] = 0;
3310       src_filename[backend] = NULL;
3311       dest_filename[backend] = NULL;
3312       orig_filename[backend] = NULL;
3313     }
3314
3315   if (in)
3316     {
3317       /* Read options from the file IN.  */
3318       while ((length = es_read_line (in, &line, &line_len, NULL)) > 0)
3319         {
3320           char *linep;
3321           unsigned long flags = 0;
3322           char *new_value = "";
3323
3324           /* Strip newline and carriage return, if present.  */
3325           while (length > 0
3326                  && (line[length - 1] == '\n' || line[length - 1] == '\r'))
3327             line[--length] = '\0';
3328
3329           linep = strchr (line, ':');
3330           if (linep)
3331             *(linep++) = '\0';
3332
3333           /* Extract additional flags.  Default to none.  */
3334           if (linep)
3335             {
3336               char *end;
3337               char *tail;
3338
3339               end = strchr (linep, ':');
3340               if (end)
3341                 *(end++) = '\0';
3342
3343               gpg_err_set_errno (0);
3344               flags = strtoul (linep, &tail, 0);
3345               if (errno)
3346                 gc_error (1, errno, "malformed flags in option %s", line);
3347               if (!(*tail == '\0' || *tail == ':' || *tail == ' '))
3348                 gc_error (1, 0, "garbage after flags in option %s", line);
3349
3350               linep = end;
3351             }
3352
3353           /* Don't allow setting of the no change flag.  */
3354           flags &= ~GC_OPT_FLAG_NO_CHANGE;
3355
3356           /* Extract default value, if present.  Default to empty if not.  */
3357           if (linep)
3358             {
3359               char *end;
3360               end = strchr (linep, ':');
3361               if (end)
3362                 *(end++) = '\0';
3363               new_value = linep;
3364               linep = end;
3365             }
3366
3367           option = find_option (component, line, GC_BACKEND_ANY);
3368           if (!option)
3369             gc_error (1, 0, "unknown option %s", line);
3370
3371           if ((option->flags & GC_OPT_FLAG_NO_CHANGE))
3372             {
3373               gc_error (0, 0, "ignoring new value for option %s",
3374                         option->name);
3375               continue;
3376             }
3377
3378           change_one_value (option, runtime, flags, new_value, 0);
3379         }
3380       if (length < 0 || gpgrt_ferror (in))
3381         gc_error (1, errno, "error reading stream 'in'");
3382     }
3383
3384   /* Now that we have collected and locally verified the changes,
3385      write them out to new configuration files, verify them
3386      externally, and then commit them.  */
3387   option = gc_component[component].options;
3388   while (option && option->name)
3389     {
3390       /* Go on if we have already seen this backend, or if there is
3391          nothing to do.  */
3392       if (src_filename[option->backend]
3393           || !(option->new_flags || option->new_value))
3394         {
3395           option++;
3396           continue;
3397         }
3398
3399       if (gc_backend[option->backend].program)
3400         {
3401           err = change_options_program (component, option->backend,
3402                                         &src_filename[option->backend],
3403                                         &dest_filename[option->backend],
3404                                         &orig_filename[option->backend],
3405                                         verbatim);
3406           if (! err)
3407             {
3408               /* External verification.  */
3409               err = gc_component_check_options (component, out,
3410                                                 src_filename[option->backend]);
3411               if (err)
3412                 {
3413                   gc_error (0, 0,
3414                             _("External verification of component %s failed"),
3415                             gc_component[component].name);
3416                   gpg_err_set_errno (EINVAL);
3417                 }
3418             }
3419
3420         }
3421       else
3422         err = change_options_file (component, option->backend,
3423                                    &src_filename[option->backend],
3424                                    &dest_filename[option->backend],
3425                                    &orig_filename[option->backend]);
3426
3427       if (err)
3428         break;
3429
3430       option++;
3431     }
3432
3433   /* We are trying to atomically commit all changes.  Unfortunately,
3434      we cannot rely on gnupg_rename_file to manage the signals for us,
3435      doing so would require us to pass NULL as BLOCK to any subsequent
3436      call to it.  Instead, we just manage the signal handling
3437      manually.  */
3438   block = 1;
3439   gnupg_block_all_signals ();
3440
3441   if (! err && ! opt.dry_run)
3442     {
3443       int i;
3444
3445       for (i = 0; i < GC_BACKEND_NR; i++)
3446         {
3447           if (src_filename[i])
3448             {
3449               /* FIXME: Make a verification here.  */
3450
3451               assert (dest_filename[i]);
3452
3453               if (orig_filename[i])
3454                 err = gnupg_rename_file (src_filename[i], dest_filename[i], NULL);
3455               else
3456                 {
3457 #ifdef HAVE_W32_SYSTEM
3458                   /* We skip the unlink if we expect the file not to
3459                      be there.  */
3460                   err = gnupg_rename_file (src_filename[i], dest_filename[i], NULL);
3461 #else /* HAVE_W32_SYSTEM */
3462                   /* This is a bit safer than rename() because we
3463                      expect DEST_FILENAME not to be there.  If it
3464                      happens to be there, this will fail.  */
3465                   err = link (src_filename[i], dest_filename[i]);
3466                   if (!err)
3467                     err = unlink (src_filename[i]);
3468 #endif /* !HAVE_W32_SYSTEM */
3469                 }
3470               if (err)
3471                 break;
3472               xfree (src_filename[i]);
3473               src_filename[i] = NULL;
3474             }
3475         }
3476     }
3477
3478   if (err || opt.dry_run)
3479     {
3480       int i;
3481       int saved_errno = errno;
3482
3483       /* An error occurred or a dry-run is requested.  */
3484       for (i = 0; i < GC_BACKEND_NR; i++)
3485         {
3486           if (src_filename[i])
3487             {
3488               /* The change was not yet committed.  */
3489               unlink (src_filename[i]);
3490               if (orig_filename[i])
3491                 unlink (orig_filename[i]);
3492             }
3493           else
3494             {
3495               /* The changes were already committed.  FIXME: This is a
3496                  tad dangerous, as we don't know if we don't overwrite
3497                  a version of the file that is even newer than the one
3498                  we just installed.  */
3499               if (orig_filename[i])
3500                 gnupg_rename_file (orig_filename[i], dest_filename[i], NULL);
3501               else
3502                 unlink (dest_filename[i]);
3503             }
3504         }
3505       if (err)
3506         gc_error (1, saved_errno, "could not commit changes");
3507
3508       /* Fall-through for dry run.  */
3509       goto leave;
3510     }
3511
3512   /* If it all worked, notify the daemons of the changes.  */
3513   if (opt.runtime)
3514     for (backend = 0; backend < GC_BACKEND_NR; backend++)
3515       {
3516         if (runtime[backend] && gc_backend[backend].runtime_change)
3517           (*gc_backend[backend].runtime_change) (0);
3518       }
3519
3520   /* Move the per-process backup file into its place.  */
3521   for (backend = 0; backend < GC_BACKEND_NR; backend++)
3522     if (orig_filename[backend])
3523       {
3524         char *backup_filename;
3525
3526         assert (dest_filename[backend]);
3527
3528         backup_filename = xasprintf ("%s.%s.bak",
3529                                      dest_filename[backend], GPGCONF_NAME);
3530         gnupg_rename_file (orig_filename[backend], backup_filename, NULL);
3531         xfree (backup_filename);
3532       }
3533
3534  leave:
3535   if (block)
3536     gnupg_unblock_all_signals ();
3537   xfree (line);
3538   for (backend = 0; backend < GC_BACKEND_NR; backend++)
3539     {
3540       xfree (src_filename[backend]);
3541       xfree (dest_filename[backend]);
3542       xfree (orig_filename[backend]);
3543     }
3544 }
3545
3546
3547 /* Check whether USER matches the current user of one of its group.
3548    This function may change USER.  Returns true is there is a
3549    match.  */
3550 static int
3551 key_matches_user_or_group (char *user)
3552 {
3553   char *group;
3554
3555   if (*user == '*' && user[1] == 0)
3556     return 1; /* A single asterisk matches all users.  */
3557
3558   group = strchr (user, ':');
3559   if (group)
3560     *group++ = 0;
3561
3562 #ifdef HAVE_W32_SYSTEM
3563   /* Under Windows we don't support groups. */
3564   if (group && *group)
3565     gc_error (0, 0, _("Note that group specifications are ignored\n"));
3566 #ifndef HAVE_W32CE_SYSTEM
3567   if (*user)
3568     {
3569       static char *my_name;
3570
3571       if (!my_name)
3572         {
3573           char tmp[1];
3574           DWORD size = 1;
3575
3576           GetUserNameA (tmp, &size);
3577           my_name = xmalloc (size);
3578           if (!GetUserNameA (my_name, &size))
3579             gc_error (1,0, "error getting current user name: %s",
3580                       w32_strerror (-1));
3581         }
3582
3583       if (!strcmp (user, my_name))
3584         return 1; /* Found.  */
3585     }
3586 #endif /*HAVE_W32CE_SYSTEM*/
3587 #else /*!HAVE_W32_SYSTEM*/
3588   /* First check whether the user matches.  */
3589   if (*user)
3590     {
3591       static char *my_name;
3592
3593       if (!my_name)
3594         {
3595           struct passwd *pw = getpwuid ( getuid () );
3596           if (!pw)
3597             gc_error (1, errno, "getpwuid failed for current user");
3598           my_name = xstrdup (pw->pw_name);
3599         }
3600       if (!strcmp (user, my_name))
3601         return 1; /* Found.  */
3602     }
3603
3604   /* If that failed, check whether a group matches.  */
3605   if (group && *group)
3606     {
3607       static char *my_group;
3608       static char **my_supgroups;
3609       int n;
3610
3611       if (!my_group)
3612         {
3613           struct group *gr = getgrgid ( getgid () );
3614           if (!gr)
3615             gc_error (1, errno, "getgrgid failed for current user");
3616           my_group = xstrdup (gr->gr_name);
3617         }
3618       if (!strcmp (group, my_group))
3619         return 1; /* Found.  */
3620
3621       if (!my_supgroups)
3622         {
3623           int ngids;
3624           gid_t *gids;
3625
3626           ngids = getgroups (0, NULL);
3627           gids  = xcalloc (ngids+1, sizeof *gids);
3628           ngids = getgroups (ngids, gids);
3629           if (ngids < 0)
3630             gc_error (1, errno, "getgroups failed for current user");
3631           my_supgroups = xcalloc (ngids+1, sizeof *my_supgroups);
3632           for (n=0; n < ngids; n++)
3633             {
3634               struct group *gr = getgrgid ( gids[n] );
3635               if (!gr)
3636                 gc_error (1, errno, "getgrgid failed for supplementary group");
3637               my_supgroups[n] = xstrdup (gr->gr_name);
3638             }
3639           xfree (gids);
3640         }
3641
3642       for (n=0; my_supgroups[n]; n++)
3643         if (!strcmp (group, my_supgroups[n]))
3644           return 1; /* Found.  */
3645     }
3646 #endif /*!HAVE_W32_SYSTEM*/
3647   return 0; /* No match.  */
3648 }
3649
3650
3651
3652 /* Read and process the global configuration file for gpgconf.  This
3653    optional file is used to update our internal tables at runtime and
3654    may also be used to set new default values.  If FNAME is NULL the
3655    default name will be used.  With UPDATE set to true the internal
3656    tables are actually updated; if not set, only a syntax check is
3657    done.  If DEFAULTS is true the global options are written to the
3658    configuration files.  If LISTFP is set, no changes are done but the
3659    configuration file is printed to LISTFP in a colon separated format.
3660
3661    Returns 0 on success or if the config file is not present; -1 is
3662    returned on error. */
3663 int
3664 gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
3665                          estream_t listfp)
3666 {
3667   int result = 0;
3668   char *line = NULL;
3669   size_t line_len = 0;
3670   ssize_t length;
3671   gpgrt_stream_t config;
3672   int lineno = 0;
3673   int in_rule = 0;
3674   int got_match = 0;
3675   int runtime[GC_BACKEND_NR];
3676   int backend_id, component_id;
3677   char *fname;
3678
3679   if (fname_arg)
3680     fname = xstrdup (fname_arg);
3681   else
3682     fname = make_filename (gnupg_sysconfdir (), GPGCONF_NAME EXTSEP_S "conf",
3683                            NULL);
3684
3685   for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3686     runtime[backend_id] = 0;
3687
3688   config = gpgrt_fopen (fname, "r");
3689   if (!config)
3690     {
3691       /* Do not print an error if the file is not available, except
3692          when running in syntax check mode.  */
3693       if (errno != ENOENT || !update)
3694         {
3695           gc_error (0, errno, "can not open global config file '%s'", fname);
3696           result = -1;
3697         }
3698       xfree (fname);
3699       return result;
3700     }
3701
3702   while ((length = gpgrt_read_line (config, &line, &line_len, NULL)) > 0)
3703     {
3704       char *key, *component, *option, *flags, *value;
3705       char *empty;
3706       gc_option_t *option_info = NULL;
3707       char *p;
3708       int is_continuation;
3709
3710       lineno++;
3711       key = line;
3712       while (*key == ' ' || *key == '\t')
3713         key++;
3714       if (!*key || *key == '#' || *key == '\r' || *key == '\n')
3715         continue;
3716
3717       is_continuation = (key != line);
3718
3719       /* Parse the key field.  */
3720       if (!is_continuation && got_match)
3721         break;  /* Finish after the first match.  */
3722       else if (!is_continuation)
3723         {
3724           in_rule = 0;
3725           for (p=key+1; *p && !strchr (" \t\r\n", *p); p++)
3726             ;
3727           if (!*p)
3728             {
3729               gc_error (0, 0, "missing rule at '%s', line %d", fname, lineno);
3730               result = -1;
3731               continue;
3732             }
3733           *p++ = 0;
3734           component = p;
3735         }
3736       else if (!in_rule)
3737         {
3738           gc_error (0, 0, "continuation but no rule at '%s', line %d",
3739                     fname, lineno);
3740           result = -1;
3741           continue;
3742         }
3743       else
3744         {
3745           component = key;
3746           key = NULL;
3747         }
3748
3749       in_rule = 1;
3750
3751       /* Parse the component.  */
3752       while (*component == ' ' || *component == '\t')
3753         component++;
3754       for (p=component; *p && !strchr (" \t\r\n", *p); p++)
3755         ;
3756       if (p == component)
3757         {
3758           gc_error (0, 0, "missing component at '%s', line %d",
3759                     fname, lineno);
3760           result = -1;
3761           continue;
3762         }
3763       empty = p;
3764       *p++ = 0;
3765       option = p;
3766       component_id = gc_component_find (component);
3767       if (component_id < 0)
3768         {
3769           gc_error (0, 0, "unknown component at '%s', line %d",
3770                     fname, lineno);
3771           result = -1;
3772         }
3773
3774       /* Parse the option name.  */
3775       while (*option == ' ' || *option == '\t')
3776         option++;
3777       for (p=option; *p && !strchr (" \t\r\n", *p); p++)
3778         ;
3779       if (p == option)
3780         {
3781           gc_error (0, 0, "missing option at '%s', line %d",
3782                     fname, lineno);
3783           result = -1;
3784           continue;
3785         }
3786       *p++ = 0;
3787       flags = p;
3788       if ( component_id != -1)
3789         {
3790           option_info = find_option (component_id, option, GC_BACKEND_ANY);
3791           if (!option_info)
3792             {
3793               gc_error (0, 0, "unknown option at '%s', line %d",
3794                         fname, lineno);
3795               result = -1;
3796             }
3797         }
3798
3799
3800       /* Parse the optional flags.  */
3801       while (*flags == ' ' || *flags == '\t')
3802         flags++;
3803       if (*flags == '[')
3804         {
3805           flags++;
3806           p = strchr (flags, ']');
3807           if (!p)
3808             {
3809               gc_error (0, 0, "syntax error in rule at '%s', line %d",
3810                         fname, lineno);
3811               result = -1;
3812               continue;
3813             }
3814           *p++ = 0;
3815           value = p;
3816         }
3817       else  /* No flags given.  */
3818         {
3819           value = flags;
3820           flags = NULL;
3821         }
3822
3823       /* Parse the optional value.  */
3824       while (*value == ' ' || *value == '\t')
3825        value++;
3826       for (p=value; *p && !strchr ("\r\n", *p); p++)
3827         ;
3828       if (p == value)
3829         value = empty; /* No value given; let it point to an empty string.  */
3830       else
3831         {
3832           /* Strip trailing white space.  */
3833           *p = 0;
3834           for (p--; p > value && (*p == ' ' || *p == '\t'); p--)
3835             *p = 0;
3836         }
3837
3838       /* Check flag combinations.  */
3839       if (!flags)
3840         ;
3841       else if (!strcmp (flags, "default"))
3842         {
3843           if (*value)
3844             {
3845               gc_error (0, 0, "flag \"default\" may not be combined "
3846                         "with a value at '%s', line %d",
3847                         fname, lineno);
3848               result = -1;
3849             }
3850         }
3851       else if (!strcmp (flags, "change"))
3852         ;
3853       else if (!strcmp (flags, "no-change"))
3854         ;
3855       else
3856         {
3857           gc_error (0, 0, "unknown flag at '%s', line %d",
3858                     fname, lineno);
3859           result = -1;
3860         }
3861
3862       /* In list mode we print out all records.  */
3863       if (listfp && !result)
3864         {
3865           /* If this is a new ruleset, print a key record.  */
3866           if (!is_continuation)
3867             {
3868               char *group = strchr (key, ':');
3869               if (group)
3870                 {
3871                   *group++ = 0;
3872                   if ((p = strchr (group, ':')))
3873                     *p = 0; /* We better strip any extra stuff. */
3874                 }
3875
3876               es_fprintf (listfp, "k:%s:", gc_percent_escape (key));
3877               es_fprintf (listfp, "%s\n", group? gc_percent_escape (group):"");
3878             }
3879
3880           /* All other lines are rule records.  */
3881           es_fprintf (listfp, "r:::%s:%s:%s:",
3882                       gc_component[component_id].name,
3883                       option_info->name? option_info->name : "",
3884                       flags? flags : "");
3885           if (value != empty)
3886             es_fprintf (listfp, "\"%s", gc_percent_escape (value));
3887
3888           es_putc ('\n', listfp);
3889         }
3890
3891       /* Check whether the key matches but do this only if we are not
3892          running in syntax check mode. */
3893       if ( update
3894            && !result && !listfp
3895            && (got_match || (key && key_matches_user_or_group (key))) )
3896         {
3897           int newflags = 0;
3898
3899           got_match = 1;
3900
3901           /* Apply the flags from gpgconf.conf.  */
3902           if (!flags)
3903             ;
3904           else if (!strcmp (flags, "default"))
3905             newflags |= GC_OPT_FLAG_DEFAULT;
3906           else if (!strcmp (flags, "no-change"))
3907             option_info->flags |= GC_OPT_FLAG_NO_CHANGE;
3908           else if (!strcmp (flags, "change"))
3909             option_info->flags &= ~GC_OPT_FLAG_NO_CHANGE;
3910
3911           if (defaults)
3912             {
3913               /* Here we explicitly allow updating the value again.  */
3914               if (newflags)
3915                 {
3916                   option_info->new_flags = 0;
3917                 }
3918               if (*value)
3919                 {
3920                   xfree (option_info->new_value);
3921                   option_info->new_value = NULL;
3922                 }
3923               change_one_value (option_info, runtime, newflags, value, 0);
3924             }
3925         }
3926     }
3927
3928   if (length < 0 || gpgrt_ferror (config))
3929     {
3930       gc_error (0, errno, "error reading from '%s'", fname);
3931       result = -1;
3932     }
3933   if (gpgrt_fclose (config))
3934     gc_error (0, errno, "error closing '%s'", fname);
3935
3936   xfree (line);
3937
3938   /* If it all worked, process the options. */
3939   if (!result && update && defaults && !listfp)
3940     {
3941       /* We need to switch off the runtime update, so that we can do
3942          it later all at once. */
3943       int save_opt_runtime = opt.runtime;
3944       opt.runtime = 0;
3945
3946       for (component_id = 0; component_id < GC_COMPONENT_NR; component_id++)
3947         {
3948           gc_component_change_options (component_id, NULL, NULL, 0);
3949         }
3950       opt.runtime = save_opt_runtime;
3951
3952       if (opt.runtime)
3953         {
3954           for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3955             if (runtime[backend_id] && gc_backend[backend_id].runtime_change)
3956               (*gc_backend[backend_id].runtime_change) (0);
3957         }
3958     }
3959
3960   xfree (fname);
3961   return result;
3962 }
3963
3964
3965 /*
3966  * Apply the profile FNAME to all known configure files.
3967  */
3968 gpg_error_t
3969 gc_apply_profile (const char *fname)
3970 {
3971   gpg_error_t err;
3972   char *fname_buffer = NULL;
3973   char *line = NULL;
3974   size_t line_len = 0;
3975   ssize_t length;
3976   estream_t fp;
3977   int lineno = 0;
3978   int runtime[GC_BACKEND_NR];
3979   int backend_id;
3980   int component_id = -1;
3981   int skip_section = 0;
3982   int error_count = 0;
3983   int newflags;
3984
3985   if (!fname)
3986     fname = "-";
3987
3988   for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3989     runtime[backend_id] = 0;
3990
3991
3992   if (!(!strcmp (fname, "-")
3993         || strchr (fname, '/')
3994 #ifdef HAVE_W32_SYSTEM
3995         || strchr (fname, '\\')
3996 #endif
3997         || strchr (fname, '.')))
3998     {
3999       /* FNAME looks like a standard profile name.  Check whether one
4000        * is installed and use that instead of the given file name.  */
4001       fname_buffer = xstrconcat (gnupg_datadir (), DIRSEP_S,
4002                                  fname, ".prf", NULL);
4003       if (!access (fname_buffer, F_OK))
4004         fname = fname_buffer;
4005     }
4006
4007   fp = !strcmp (fname, "-")? es_stdin : es_fopen (fname, "r");
4008   if (!fp)
4009     {
4010       err = gpg_error_from_syserror ();
4011       log_error ("can't open '%s': %s\n", fname, gpg_strerror (err));
4012       return err;
4013     }
4014
4015   if (opt.verbose)
4016     log_info ("applying profile '%s'\n", fname);
4017
4018   err = 0;
4019   while ((length = es_read_line (fp, &line, &line_len, NULL)) > 0)
4020     {
4021       char *name, *flags, *value;
4022       gc_option_t *option_info = NULL;
4023       char *p;
4024
4025       lineno++;
4026       name = line;
4027       while (*name == ' ' || *name == '\t')
4028         name++;
4029       if (!*name || *name == '#' || *name == '\r' || *name == '\n')
4030         continue;
4031       trim_trailing_spaces (name);
4032
4033       /* Check whether this is a new section.  */
4034       if (*name == '[')
4035         {
4036           name++;
4037           skip_section = 0;
4038           /* New section: Get the name of the component.  */
4039           p = strchr (name, ']');
4040           if (!p)
4041             {
4042               error_count++;
4043               log_info ("%s:%d:%d: error: syntax error in section tag\n",
4044                         fname, lineno, (int)(name - line));
4045               skip_section = 1;
4046               continue;
4047             }
4048           *p++ = 0;
4049           if (*p)
4050             log_info ("%s:%d:%d: warning: garbage after section tag\n",
4051                       fname, lineno, (int)(p - line));
4052
4053           trim_spaces (name);
4054           component_id = gc_component_find (name);
4055           if (component_id < 0)
4056             {
4057               log_info ("%s:%d:%d: warning: skipping unknown section '%s'\n",
4058                         fname, lineno, (int)(name - line), name );
4059               skip_section = 1;
4060             }
4061           continue;
4062         }
4063
4064       if (skip_section)
4065         continue;
4066       if (component_id < 0)
4067         {
4068           error_count++;
4069           log_info ("%s:%d:%d: error: not in a valid section\n",
4070                     fname, lineno, (int)(name - line));
4071           skip_section = 1;
4072           continue;
4073         }
4074
4075       /* Parse the option name.  */
4076       for (p = name; *p && !spacep (p); p++)
4077         ;
4078       *p++ = 0;
4079       value = p;
4080
4081       option_info = find_option (component_id, name, GC_BACKEND_ANY);
4082       if (!option_info)
4083         {
4084           error_count++;
4085           log_info ("%s:%d:%d: error: unknown option '%s' in section '%s'\n",
4086                     fname, lineno, (int)(name - line),
4087                     name, gc_component[component_id].name);
4088           continue;
4089         }
4090
4091       /* Parse the optional flags. */
4092       trim_spaces (value);
4093       flags = value;
4094       if (*flags == '[')
4095         {
4096           flags++;
4097           p = strchr (flags, ']');
4098           if (!p)
4099             {
4100               log_info ("%s:%d:%d: warning: invalid flag specification\n",
4101                         fname, lineno, (int)(p - line));
4102               continue;
4103             }
4104           *p++ = 0;
4105           value = p;
4106           trim_spaces (value);
4107         }
4108       else /* No flags given.  */
4109         flags = NULL;
4110
4111       /* Set required defaults.  */
4112       if (gc_arg_type[option_info->arg_type].fallback == GC_ARG_TYPE_NONE
4113           && !*value)
4114         value = "1";
4115
4116       /* Check and save this option.  */
4117       newflags = 0;
4118       if (flags && !strcmp (flags, "default"))
4119         newflags |= GC_OPT_FLAG_DEFAULT;
4120
4121       if (newflags)
4122         option_info->new_flags = 0;
4123       if (*value)
4124         {
4125           xfree (option_info->new_value);
4126           option_info->new_value = NULL;
4127         }
4128       change_one_value (option_info, runtime, newflags, value, 1);
4129     }
4130
4131   if (length < 0 || es_ferror (fp))
4132     {
4133       err = gpg_error_from_syserror ();
4134       error_count++;
4135       log_error (_("%s:%u: read error: %s\n"),
4136                  fname, lineno, gpg_strerror (err));
4137     }
4138   if (es_fclose (fp))
4139     log_error (_("error closing '%s'\n"), fname);
4140   if (error_count)
4141     log_error (_("error parsing '%s'\n"), fname);
4142
4143   xfree (line);
4144
4145   /* If it all worked, process the options. */
4146   if (!err)
4147     {
4148       /* We need to switch off the runtime update, so that we can do
4149          it later all at once. */
4150       int save_opt_runtime = opt.runtime;
4151       opt.runtime = 0;
4152
4153       for (component_id = 0; component_id < GC_COMPONENT_NR; component_id++)
4154         {
4155           gc_component_change_options (component_id, NULL, NULL, 1);
4156         }
4157       opt.runtime = save_opt_runtime;
4158
4159       if (opt.runtime)
4160         {
4161           for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
4162             if (runtime[backend_id] && gc_backend[backend_id].runtime_change)
4163               (*gc_backend[backend_id].runtime_change) (0);
4164         }
4165     }
4166
4167   xfree (fname_buffer);
4168   return err;
4169 }