1 /* gpgconf-comp.c - Configuration utility for GnuPG.
2 * Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc.
3 * Copyright (C) 2016 Werner Koch
5 * This file is part of GnuPG.
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.
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.
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/>.
29 #include <sys/types.h>
38 #ifdef HAVE_W32_SYSTEM
39 # define WIN32_LEAN_AND_MEAN 1
46 /* For log_logv(), asctimestamp(), gnupg_get_time (). */
51 #include "gc-opt-flags.h"
54 /* There is a problem with gpg 1.4 under Windows: --gpgconf-list
55 returns a plain filename without escaping. As long as we have not
56 fixed that we need to use gpg2. */
57 #if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
58 #define GPGNAME "gpg2"
60 #define GPGNAME GPG_NAME
65 Components: Add more components and their options.
66 Robustness: Do more validation. Call programs to do validation for us.
67 Add options to change backend binary path.
68 Extract binary path for some backends from gpgsm/gpg config.
72 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
73 void gc_error (int status, int errnum, const char *fmt, ...) \
74 __attribute__ ((format (printf, 3, 4)));
77 /* Output a diagnostic message. If ERRNUM is not 0, then the output
78 is followed by a colon, a white space, and the error string for the
79 error number ERRNUM. In any case the output is finished by a
80 newline. The message is prepended by the program name, a colon,
81 and a whitespace. The output may be further formatted or
82 redirected by the jnlib logging facility. */
84 gc_error (int status, int errnum, const char *fmt, ...)
88 va_start (arg_ptr, fmt);
89 log_logv (GPGRT_LOG_ERROR, fmt, arg_ptr);
93 log_printf (": %s\n", strerror (errnum));
100 log_printf ("fatal error (exit status %i)\n", status);
106 /* Forward declaration. */
107 static void gpg_agent_runtime_change (int killflag);
108 static void scdaemon_runtime_change (int killflag);
109 static void dirmngr_runtime_change (int killflag);
111 /* Backend configuration. Backends are used to decide how the default
112 and current value of an option can be determined, and how the
113 option can be changed. To every option in every component belongs
114 exactly one backend that controls and determines the option. Some
115 backends are programs from the GPG system. Others might be
116 implemented by GPGConf itself. If you change this enum, don't
117 forget to update GC_BACKEND below. */
120 /* Any backend, used for find_option (). */
123 /* The Gnu Privacy Guard. */
126 /* The Gnu Privacy Guard for S/MIME. */
130 GC_BACKEND_GPG_AGENT,
132 /* The GnuPG SCDaemon. */
135 /* The GnuPG directory manager. */
138 /* The LDAP server list file for the director manager. */
139 GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST,
141 /* The Pinentry (not a part of GnuPG, proper). */
144 /* The number of the above entries. */
149 /* To be able to implement generic algorithms for the various
150 backends, we collect all information about them in this struct. */
153 /* The name of the backend. */
156 /* The name of the program that acts as the backend. Some backends
157 don't have an associated program, but are implemented directly by
158 GPGConf. In this case, PROGRAM is NULL. */
161 /* The module name (GNUPG_MODULE_NAME_foo) as defined by
162 ../common/util.h. This value is used to get the actual installed
163 path of the program. 0 is used if no backend program is
167 /* The runtime change callback. If KILLFLAG is true the component
168 is killed and not just reloaded. */
169 void (*runtime_change) (int killflag);
171 /* The option name for the configuration filename of this backend.
172 This must be an absolute filename. It can be an option from a
173 different backend (but then ordering of the options might
174 matter). Note: This must be unique among all components. */
175 const char *option_config_filename;
177 /* If this is a file backend rather than a program backend, then
178 this is the name of the option associated with the file. */
179 const char *option_name;
180 } gc_backend[GC_BACKEND_NR] =
182 { NULL }, /* GC_BACKEND_ANY dummy entry. */
183 { GPG_DISP_NAME, GPGNAME, GNUPG_MODULE_NAME_GPG,
184 NULL, GPGCONF_NAME "-" GPG_NAME ".conf" },
185 { GPGSM_DISP_NAME, GPGSM_NAME, GNUPG_MODULE_NAME_GPGSM,
186 NULL, GPGCONF_NAME "-" GPGSM_NAME ".conf" },
187 { GPG_AGENT_DISP_NAME, GPG_AGENT_NAME, GNUPG_MODULE_NAME_AGENT,
188 gpg_agent_runtime_change, GPGCONF_NAME"-" GPG_AGENT_NAME ".conf" },
189 { SCDAEMON_DISP_NAME, SCDAEMON_NAME, GNUPG_MODULE_NAME_SCDAEMON,
190 scdaemon_runtime_change, GPGCONF_NAME"-" SCDAEMON_NAME ".conf" },
191 { DIRMNGR_DISP_NAME, DIRMNGR_NAME, GNUPG_MODULE_NAME_DIRMNGR,
192 dirmngr_runtime_change, GPGCONF_NAME "-" DIRMNGR_NAME ".conf" },
193 { DIRMNGR_DISP_NAME " LDAP Server List", NULL, 0,
194 NULL, "ldapserverlist-file", "LDAP Server" },
195 { "Pinentry", "pinentry", GNUPG_MODULE_NAME_PINENTRY,
196 NULL, GPGCONF_NAME "-pinentry.conf" },
200 /* Option configuration. */
202 /* An option might take an argument, or not. Argument types can be
203 basic or complex. Basic types are generic and easy to validate.
204 Complex types provide more specific information about the intended
205 use, but can be difficult to validate. If you add to this enum,
206 don't forget to update GC_ARG_TYPE below. YOU MUST NOT CHANGE THE
207 NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE EXTERNAL
211 /* Basic argument types. */
214 GC_ARG_TYPE_NONE = 0,
216 /* A String argument. */
217 GC_ARG_TYPE_STRING = 1,
219 /* A signed integer argument. */
220 GC_ARG_TYPE_INT32 = 2,
222 /* An unsigned integer argument. */
223 GC_ARG_TYPE_UINT32 = 3,
225 /* ADD NEW BASIC TYPE ENTRIES HERE. */
227 /* Complex argument types. */
229 /* A complete filename. */
230 GC_ARG_TYPE_FILENAME = 32,
232 /* An LDAP server in the format
233 HOSTNAME:PORT:USERNAME:PASSWORD:BASE_DN. */
234 GC_ARG_TYPE_LDAP_SERVER = 33,
236 /* A 40 character fingerprint. */
237 GC_ARG_TYPE_KEY_FPR = 34,
239 /* A user ID or key ID or fingerprint for a certificate. */
240 GC_ARG_TYPE_PUB_KEY = 35,
242 /* A user ID or key ID or fingerprint for a certificate with a key. */
243 GC_ARG_TYPE_SEC_KEY = 36,
245 /* A alias list made up of a key, an equal sign and a space
246 separated list of values. */
247 GC_ARG_TYPE_ALIAS_LIST = 37,
249 /* ADD NEW COMPLEX TYPE ENTRIES HERE. */
251 /* The number of the above entries. */
256 /* For every argument, we record some information about it in the
260 /* For every argument type exists a basic argument type that can be
261 used as a fallback for input and validation purposes. */
262 gc_arg_type_t fallback;
264 /* Human-readable name of the type. */
266 } gc_arg_type[GC_ARG_TYPE_NR] =
268 /* The basic argument types have their own types as fallback. */
269 { GC_ARG_TYPE_NONE, "none" },
270 { GC_ARG_TYPE_STRING, "string" },
271 { GC_ARG_TYPE_INT32, "int32" },
272 { GC_ARG_TYPE_UINT32, "uint32" },
274 /* Reserved basic type entries for future extension. */
275 { GC_ARG_TYPE_NR, NULL }, { GC_ARG_TYPE_NR, NULL },
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 },
290 /* The complex argument types have a basic type as fallback. */
291 { GC_ARG_TYPE_STRING, "filename" },
292 { GC_ARG_TYPE_STRING, "ldap server" },
293 { GC_ARG_TYPE_STRING, "key fpr" },
294 { GC_ARG_TYPE_STRING, "pub key" },
295 { GC_ARG_TYPE_STRING, "sec key" },
296 { GC_ARG_TYPE_STRING, "alias list" },
300 /* Every option has an associated expert level, than can be used to
301 hide advanced and expert options from beginners. If you add to
302 this list, don't forget to update GC_LEVEL below. YOU MUST NOT
303 CHANGE THE NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE
304 EXTERNAL INTERFACE. */
307 /* The basic options should always be displayed. */
310 /* The advanced options may be hidden from beginners. */
313 /* The expert options should only be displayed to experts. */
316 /* The invisible options should normally never be displayed. */
319 /* The internal options are never exported, they mark options that
320 are recorded for internal use only. */
323 /* ADD NEW ENTRIES HERE. */
325 /* The number of the above entries. */
329 /* A description for each expert level. */
343 /* Option flags. The flags which are used by the backends are defined
344 by gc-opt-flags.h, included above.
346 YOU MUST NOT CHANGE THE NUMBERS OF THE EXISTING FLAGS, AS THEY ARE
347 PART OF THE EXTERNAL INTERFACE. */
349 /* Some entries in the option list are not options, but mark the
350 beginning of a new group of options. These entries have the GROUP
352 #define GC_OPT_FLAG_GROUP (1UL << 0)
353 /* The ARG_OPT flag for an option indicates that the argument is
354 optional. This is never set for GC_ARG_TYPE_NONE options. */
355 #define GC_OPT_FLAG_ARG_OPT (1UL << 1)
356 /* The LIST flag for an option indicates that the option can occur
357 several times. A comma separated list of arguments is used as the
359 #define GC_OPT_FLAG_LIST (1UL << 2)
362 /* A human-readable description for each flag. */
379 /* To each option, or group marker, the information in the GC_OPTION
380 struct is provided. If you change this, don't forget to update the
381 option list of each component. */
384 /* If this is NULL, then this is a terminator in an array of unknown
385 length. Otherwise, if this entry is a group marker (see FLAGS),
386 then this is the name of the group described by this entry.
387 Otherwise it is the name of the option described by this
388 entry. The name must not contain a colon. */
391 /* The option flags. If the GROUP flag is set, then this entry is a
392 group marker, not an option, and only the fields LEVEL,
393 DESC_DOMAIN and DESC are valid. In all other cases, this entry
394 describes a new option and all fields are valid. */
397 /* The expert level. This field is valid for options and groups. A
398 group has the expert level of the lowest-level option in the
400 gc_expert_level_t level;
402 /* A gettext domain in which the following description can be found.
403 If this is NULL, then DESC is not translated. Valid for groups
406 Note that we try to keep the description of groups within the
409 IMPORTANT: If you add a new domain please make sure to add a code
410 set switching call to the function my_dgettext further below. */
411 const char *desc_domain;
413 /* A gettext description for this group or option. If it starts
414 with a '|', then the string up to the next '|' describes the
415 argument, and the description follows the second '|'.
417 In general enclosing these description in N_() is not required
418 because the description should be identical to the one in the
419 help menu of the respective program. */
422 /* The following fields are only valid for options. */
424 /* The type of the option argument. */
425 gc_arg_type_t arg_type;
427 /* The backend that implements this option. */
428 gc_backend_t backend;
430 /* The following fields are set to NULL at startup (because all
431 option's are declared as static variables). They are at the end
432 of the list so that they can be omitted from the option
435 /* This is true if the option is supported by this version of the
439 /* The default value for this option. This is NULL if the option is
440 not present in the backend, the empty string if no default is
441 available, and otherwise a quoted string. */
444 /* The default argument is only valid if the "optional arg" flag is
445 set, and specifies the default argument (value) that is used if
446 the argument is omitted. */
449 /* The current value of this option. */
452 /* The new flags for this option. The only defined flag is actually
453 GC_OPT_FLAG_DEFAULT, and it means that the option should be
454 deleted. In this case, NEW_VALUE is NULL. */
455 unsigned long new_flags;
457 /* The new value of this option. */
460 typedef struct gc_option gc_option_t;
462 /* Use this macro to terminate an option list. */
463 #define GC_OPTION_NULL { NULL }
466 #ifndef BUILD_WITH_AGENT
467 #define gc_options_gpg_agent NULL
469 /* The options of the GC_COMPONENT_GPG_AGENT component. */
470 static gc_option_t gc_options_gpg_agent[] =
472 /* The configuration file to which we write the changes. */
473 { GPGCONF_NAME"-" GPG_AGENT_NAME ".conf",
474 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
475 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
478 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
479 "gnupg", N_("Options controlling the diagnostic output") },
480 { "verbose", GC_OPT_FLAG_LIST|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
482 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
483 { "quiet", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
484 "gnupg", "be somewhat more quiet",
485 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
486 { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
488 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
491 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
492 "gnupg", N_("Options controlling the configuration") },
493 { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
494 "gnupg", "|FILE|read options from FILE",
495 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
496 { "disable-scdaemon", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
497 "gnupg", "do not use the SCdaemon",
498 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
499 { "enable-ssh-support", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
500 "gnupg", "enable ssh support",
501 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
502 { "enable-putty-support", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
503 "gnupg", "enable putty support",
504 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
507 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
508 "gnupg", N_("Options useful for debugging") },
509 { "debug-level", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
510 "gnupg", "|LEVEL|set the debugging level to LEVEL",
511 GC_ARG_TYPE_STRING, GC_BACKEND_GPG_AGENT },
512 { "log-file", GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
513 "gnupg", N_("|FILE|write server mode logs to FILE"),
514 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
515 { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
517 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
520 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
521 "gnupg", N_("Options controlling the security") },
522 { "default-cache-ttl", GC_OPT_FLAG_RUNTIME,
523 GC_LEVEL_BASIC, "gnupg",
524 "|N|expire cached PINs after N seconds",
525 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
526 { "default-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME,
527 GC_LEVEL_ADVANCED, "gnupg",
528 N_("|N|expire SSH keys after N seconds"),
529 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
530 { "max-cache-ttl", GC_OPT_FLAG_RUNTIME,
531 GC_LEVEL_EXPERT, "gnupg",
532 N_("|N|set maximum PIN cache lifetime to N seconds"),
533 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
534 { "max-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME,
535 GC_LEVEL_EXPERT, "gnupg",
536 N_("|N|set maximum SSH key lifetime to N seconds"),
537 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
538 { "ignore-cache-for-signing", GC_OPT_FLAG_RUNTIME,
539 GC_LEVEL_BASIC, "gnupg", "do not use the PIN cache when signing",
540 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
541 { "allow-emacs-pinentry", GC_OPT_FLAG_RUNTIME,
543 "gnupg", "allow passphrase to be prompted through Emacs",
544 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
545 { "no-allow-external-cache", GC_OPT_FLAG_RUNTIME,
546 GC_LEVEL_BASIC, "gnupg", "disallow the use of an external password cache",
547 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
548 { "no-allow-mark-trusted", GC_OPT_FLAG_RUNTIME,
549 GC_LEVEL_ADVANCED, "gnupg", "disallow clients to mark keys as \"trusted\"",
550 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
551 { "no-allow-loopback-pinentry", GC_OPT_FLAG_RUNTIME,
552 GC_LEVEL_EXPERT, "gnupg", "disallow caller to override the pinentry",
553 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
554 { "no-grab", GC_OPT_FLAG_RUNTIME, GC_LEVEL_EXPERT,
555 "gnupg", "do not grab keyboard and mouse",
556 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
558 { "Passphrase policy",
559 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
560 "gnupg", N_("Options enforcing a passphrase policy") },
561 { "enforce-passphrase-constraints", GC_OPT_FLAG_RUNTIME,
562 GC_LEVEL_EXPERT, "gnupg",
563 N_("do not allow bypassing the passphrase policy"),
564 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
565 { "min-passphrase-len", GC_OPT_FLAG_RUNTIME,
566 GC_LEVEL_ADVANCED, "gnupg",
567 N_("|N|set minimal required length for new passphrases to N"),
568 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
569 { "min-passphrase-nonalpha", GC_OPT_FLAG_RUNTIME,
570 GC_LEVEL_EXPERT, "gnupg",
571 N_("|N|require at least N non-alpha characters for a new passphrase"),
572 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
573 { "check-passphrase-pattern", GC_OPT_FLAG_RUNTIME,
575 "gnupg", N_("|FILE|check new passphrases against pattern in FILE"),
576 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG_AGENT },
577 { "max-passphrase-days", GC_OPT_FLAG_RUNTIME,
578 GC_LEVEL_EXPERT, "gnupg",
579 N_("|N|expire the passphrase after N days"),
580 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
581 { "enable-passphrase-history", GC_OPT_FLAG_RUNTIME,
582 GC_LEVEL_EXPERT, "gnupg",
583 N_("do not allow the reuse of old passphrases"),
584 GC_ARG_TYPE_NONE, GC_BACKEND_GPG_AGENT },
585 { "pinentry-timeout", GC_OPT_FLAG_RUNTIME,
586 GC_LEVEL_ADVANCED, "gnupg",
587 N_("|N|set the Pinentry timeout to N seconds"),
588 GC_ARG_TYPE_UINT32, GC_BACKEND_GPG_AGENT },
592 #endif /*BUILD_WITH_AGENT*/
595 #ifndef BUILD_WITH_SCDAEMON
596 #define gc_options_scdaemon NULL
598 /* The options of the GC_COMPONENT_SCDAEMON component. */
599 static gc_option_t gc_options_scdaemon[] =
601 /* The configuration file to which we write the changes. */
602 { GPGCONF_NAME"-"SCDAEMON_NAME".conf",
603 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
604 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
607 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
608 "gnupg", N_("Options controlling the diagnostic output") },
609 { "verbose", GC_OPT_FLAG_LIST|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
611 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
612 { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
613 "gnupg", "be somewhat more quiet",
614 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
615 { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
617 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
620 GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
621 "gnupg", N_("Options controlling the configuration") },
622 { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
623 "gnupg", "|FILE|read options from FILE",
624 GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
625 { "reader-port", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
626 "gnupg", "|N|connect to reader at port N",
627 GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
628 { "ctapi-driver", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
629 "gnupg", "|NAME|use NAME as ct-API driver",
630 GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
631 { "pcsc-driver", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
632 "gnupg", "|NAME|use NAME as PC/SC driver",
633 GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
634 { "disable-ccid", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_EXPERT,
635 "gnupg", "do not use the internal CCID driver",
636 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
637 { "disable-pinpad", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
638 "gnupg", "do not use a reader's pinpad",
639 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
640 { "enable-pinpad-varlen",
641 GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
642 "gnupg", "use variable length input for pinpad",
643 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
644 { "card-timeout", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
645 "gnupg", "|N|disconnect the card after N seconds of inactivity",
646 GC_ARG_TYPE_UINT32, GC_BACKEND_SCDAEMON },
649 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
650 "gnupg", N_("Options useful for debugging") },
651 { "debug-level", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
652 "gnupg", "|LEVEL|set the debugging level to LEVEL",
653 GC_ARG_TYPE_STRING, GC_BACKEND_SCDAEMON },
654 { "log-file", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_ADVANCED,
655 "gnupg", N_("|FILE|write a log to FILE"),
656 GC_ARG_TYPE_FILENAME, GC_BACKEND_SCDAEMON },
659 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
660 "gnupg", N_("Options controlling the security") },
661 { "deny-admin", GC_OPT_FLAG_NONE|GC_OPT_FLAG_RUNTIME, GC_LEVEL_BASIC,
662 "gnupg", "deny the use of admin card commands",
663 GC_ARG_TYPE_NONE, GC_BACKEND_SCDAEMON },
668 #endif /*BUILD_WITH_SCDAEMON*/
670 #ifndef BUILD_WITH_GPG
671 #define gc_options_gpg NULL
673 /* The options of the GC_COMPONENT_GPG component. */
674 static gc_option_t gc_options_gpg[] =
676 /* The configuration file to which we write the changes. */
677 { GPGCONF_NAME"-"GPG_NAME".conf",
678 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
679 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
682 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
683 "gnupg", N_("Options controlling the diagnostic output") },
684 { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
686 GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
687 { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
688 "gnupg", "be somewhat more quiet",
689 GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
690 { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
692 GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
695 GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
696 "gnupg", N_("Options controlling the configuration") },
697 { "default-key", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
698 "gnupg", N_("|NAME|use NAME as default secret key"),
699 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
700 { "encrypt-to", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
701 "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
702 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
703 { "group", GC_OPT_FLAG_LIST, GC_LEVEL_ADVANCED,
704 "gnupg", N_("|SPEC|set up email aliases"),
705 GC_ARG_TYPE_ALIAS_LIST, GC_BACKEND_GPG },
706 { "options", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
708 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
709 { "compliance", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
711 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
712 { "default-new-key-algo", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
714 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
715 { "default_pubkey_algo",
716 (GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_NO_CHANGE), GC_LEVEL_INVISIBLE,
718 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
722 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
723 "gnupg", N_("Options useful for debugging") },
724 { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
725 "gnupg", "|LEVEL|set the debugging level to LEVEL",
726 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
727 { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
728 "gnupg", N_("|FILE|write server mode logs to FILE"),
729 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPG },
730 /* { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE, */
732 /* GC_ARG_TYPE_UINT32, GC_BACKEND_GPG }, */
735 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
736 "gnupg", N_("Configuration for Keyservers") },
737 { "keyserver", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
738 "gnupg", N_("|URL|use keyserver at URL"), /* Deprecated - use dirmngr */
739 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
740 { "allow-pka-lookup", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
741 "gnupg", N_("allow PKA lookups (DNS requests)"),
742 GC_ARG_TYPE_NONE, GC_BACKEND_GPG },
743 { "auto-key-locate", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
744 "gnupg", N_("|MECHANISMS|use MECHANISMS to locate keys by mail address"),
745 GC_ARG_TYPE_STRING, GC_BACKEND_GPG },
750 #endif /*BUILD_WITH_GPG*/
753 #ifndef BUILD_WITH_GPGSM
754 #define gc_options_gpgsm NULL
756 /* The options of the GC_COMPONENT_GPGSM component. */
757 static gc_option_t gc_options_gpgsm[] =
759 /* The configuration file to which we write the changes. */
760 { GPGCONF_NAME"-"GPGSM_NAME".conf",
761 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
762 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
765 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
766 "gnupg", N_("Options controlling the diagnostic output") },
767 { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
769 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
770 { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
771 "gnupg", "be somewhat more quiet",
772 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
773 { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
775 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
778 GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
779 "gnupg", N_("Options controlling the configuration") },
780 { "default-key", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
781 "gnupg", N_("|NAME|use NAME as default secret key"),
782 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
783 { "encrypt-to", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
784 "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
785 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
786 { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
787 "gnupg", "|FILE|read options from FILE",
788 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
789 { "prefer-system-dirmngr", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
790 "gnupg", "use system's dirmngr if available",
791 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
792 { "disable-dirmngr", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
793 "gnupg", N_("disable all access to the dirmngr"),
794 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
795 { "p12-charset", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
796 "gnupg", N_("|NAME|use encoding NAME for PKCS#12 passphrases"),
797 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
798 { "keyserver", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
799 "gnupg", N_("|SPEC|use this keyserver to lookup keys"),
800 GC_ARG_TYPE_LDAP_SERVER, GC_BACKEND_GPGSM },
801 { "default_pubkey_algo",
802 (GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_NO_CHANGE), GC_LEVEL_INVISIBLE,
804 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
807 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
808 "gnupg", N_("Options useful for debugging") },
809 { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
810 "gnupg", "|LEVEL|set the debugging level to LEVEL",
811 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
812 { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
813 "gnupg", N_("|FILE|write server mode logs to FILE"),
814 GC_ARG_TYPE_FILENAME, GC_BACKEND_GPGSM },
815 { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
817 GC_ARG_TYPE_UINT32, GC_BACKEND_GPGSM },
820 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
821 "gnupg", N_("Options controlling the security") },
822 { "disable-crl-checks", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
823 "gnupg", "never consult a CRL",
824 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
825 { "enable-crl-checks", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
827 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
828 { "disable-trusted-cert-crl-check", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
829 "gnupg", N_("do not check CRLs for root certificates"),
830 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
831 { "enable-ocsp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
832 "gnupg", "check validity using OCSP",
833 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
834 { "include-certs", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
835 "gnupg", "|N|number of certificates to include",
836 GC_ARG_TYPE_INT32, GC_BACKEND_GPGSM },
837 { "disable-policy-checks", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
838 "gnupg", "do not check certificate policies",
839 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
840 { "auto-issuer-key-retrieve", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
841 "gnupg", "fetch missing issuer certificates",
842 GC_ARG_TYPE_NONE, GC_BACKEND_GPGSM },
843 { "cipher-algo", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
844 "gnupg", "|NAME|use cipher algorithm NAME",
845 GC_ARG_TYPE_STRING, GC_BACKEND_GPGSM },
849 #endif /*BUILD_WITH_GPGSM*/
852 #ifndef BUILD_WITH_DIRMNGR
853 #define gc_options_dirmngr NULL
855 /* The options of the GC_COMPONENT_DIRMNGR component. */
856 static gc_option_t gc_options_dirmngr[] =
858 /* The configuration file to which we write the changes. */
859 { GPGCONF_NAME"-"DIRMNGR_NAME".conf",
860 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
861 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
864 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
865 "gnupg", N_("Options controlling the diagnostic output") },
866 { "verbose", GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
867 "dirmngr", "verbose",
868 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
869 { "quiet", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
870 "dirmngr", "be somewhat more quiet",
871 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
872 { "no-greeting", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
874 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
877 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
878 "gnupg", N_("Options controlling the format of the output") },
879 { "sh", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
880 "dirmngr", "sh-style command output",
881 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
882 { "csh", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
883 "dirmngr", "csh-style command output",
884 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
887 GC_OPT_FLAG_GROUP, GC_LEVEL_EXPERT,
888 "gnupg", N_("Options controlling the configuration") },
889 { "options", GC_OPT_FLAG_NONE, GC_LEVEL_EXPERT,
890 "dirmngr", "|FILE|read options from FILE",
891 GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
892 { "resolver-timeout", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
894 GC_ARG_TYPE_INT32, GC_BACKEND_DIRMNGR },
895 { "nameserver", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
897 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
900 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
901 "gnupg", N_("Options useful for debugging") },
902 { "debug-level", GC_OPT_FLAG_ARG_OPT, GC_LEVEL_ADVANCED,
903 "dirmngr", "|LEVEL|set the debugging level to LEVEL",
904 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
905 { "no-detach", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
906 "dirmngr", "do not detach from the console",
907 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
908 { "log-file", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
909 "dirmngr", N_("|FILE|write server mode logs to FILE"),
910 GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
911 { "debug-wait", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
913 GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
914 { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE,
916 GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
919 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
920 "gnupg", N_("Options controlling the interactivity and enforcement") },
921 { "batch", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
922 "dirmngr", "run without asking a user",
923 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
924 { "force", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
925 "dirmngr", "force loading of outdated CRLs",
926 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
927 { "allow-version-check", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
928 "dirmngr", "allow online software version check",
929 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
932 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
933 "gnupg", N_("Options controlling the use of Tor") },
934 { "use-tor", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
935 "dirmngr", "route all network traffic via TOR",
936 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
939 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
940 "gnupg", N_("Configuration for Keyservers") },
941 { "keyserver", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
942 "gnupg", N_("|URL|use keyserver at URL"),
943 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
946 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
947 "gnupg", N_("Configuration for HTTP servers") },
948 { "disable-http", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
949 "dirmngr", "inhibit the use of HTTP",
950 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
951 { "ignore-http-dp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
952 "dirmngr", "ignore HTTP CRL distribution points",
953 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
954 { "http-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
955 "dirmngr", "|URL|redirect all HTTP requests to URL",
956 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
957 { "honor-http-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
958 "gnupg", N_("use system's HTTP proxy setting"),
959 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
962 GC_OPT_FLAG_GROUP, GC_LEVEL_BASIC,
963 "gnupg", N_("Configuration of LDAP servers to use") },
964 { "disable-ldap", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
965 "dirmngr", "inhibit the use of LDAP",
966 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
967 { "ignore-ldap-dp", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
968 "dirmngr", "ignore LDAP CRL distribution points",
969 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
970 { "ldap-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
971 "dirmngr", "|HOST|use HOST for LDAP queries",
972 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
973 { "only-ldap-proxy", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
974 "dirmngr", "do not use fallback hosts with --ldap-proxy",
975 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
976 { "add-servers", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
977 "dirmngr", "add new servers discovered in CRL distribution points"
978 " to serverlist", GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
979 { "ldaptimeout", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
980 "dirmngr", "|N|set LDAP timeout to N seconds",
981 GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
982 /* The following entry must not be removed, as it is required for
983 the GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST. */
984 { "ldapserverlist-file",
985 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
986 "dirmngr", "|FILE|read LDAP server list from FILE",
987 GC_ARG_TYPE_FILENAME, GC_BACKEND_DIRMNGR },
988 /* This entry must come after at least one entry for
989 GC_BACKEND_DIRMNGR in this component, so that the entry for
990 "ldapserverlist-file will be initialized before this one. */
991 { "LDAP Server", GC_OPT_FLAG_ARG_OPT|GC_OPT_FLAG_LIST, GC_LEVEL_BASIC,
992 "gnupg", N_("LDAP server list"),
993 GC_ARG_TYPE_LDAP_SERVER, GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST },
994 { "max-replies", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
995 "dirmngr", "|N|do not return more than N items in one query",
996 GC_ARG_TYPE_UINT32, GC_BACKEND_DIRMNGR },
999 GC_OPT_FLAG_GROUP, GC_LEVEL_ADVANCED,
1000 "gnupg", N_("Configuration for OCSP") },
1001 { "allow-ocsp", GC_OPT_FLAG_NONE, GC_LEVEL_BASIC,
1002 "dirmngr", "allow sending OCSP requests",
1003 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
1004 { "ignore-ocsp-service-url", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1005 "dirmngr", "ignore certificate contained OCSP service URLs",
1006 GC_ARG_TYPE_NONE, GC_BACKEND_DIRMNGR },
1007 { "ocsp-responder", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1008 "dirmngr", "|URL|use OCSP responder at URL",
1009 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
1010 { "ocsp-signer", GC_OPT_FLAG_NONE, GC_LEVEL_ADVANCED,
1011 "dirmngr", "|FPR|OCSP response signed by FPR",
1012 GC_ARG_TYPE_STRING, GC_BACKEND_DIRMNGR },
1017 #endif /*BUILD_WITH_DIRMNGR*/
1020 /* The options of the GC_COMPONENT_PINENTRY component. */
1021 static gc_option_t gc_options_pinentry[] =
1023 /* A dummy option to allow gc_component_list_components to find the
1024 pinentry backend. Needs to be a conf file. */
1025 { GPGCONF_NAME"-pinentry.conf",
1026 GC_OPT_FLAG_NONE, GC_LEVEL_INTERNAL,
1027 NULL, NULL, GC_ARG_TYPE_FILENAME, GC_BACKEND_PINENTRY },
1034 /* Component system. Each component is a set of options that can be
1035 configured at the same time. If you change this, don't forget to
1036 update GC_COMPONENT below. */
1039 /* The classic GPG for OpenPGP. */
1042 /* The GPG Agent. */
1043 GC_COMPONENT_GPG_AGENT,
1045 /* The Smardcard Daemon. */
1046 GC_COMPONENT_SCDAEMON,
1048 /* GPG for S/MIME. */
1051 /* The LDAP Directory Manager for CRLs. */
1052 GC_COMPONENT_DIRMNGR,
1054 /* The external Pinentry. */
1055 GC_COMPONENT_PINENTRY,
1057 /* The number of components. */
1062 /* The information associated with each component. */
1065 /* The name of this component. Must not contain a colon (':')
1069 /* The gettext domain for the description DESC. If this is NULL,
1070 then the description is not translated. */
1071 const char *desc_domain;
1073 /* The description for this domain. */
1076 /* The list of options for this component, terminated by
1078 gc_option_t *options;
1081 { "gpg", "gnupg", N_("OpenPGP"), gc_options_gpg },
1082 { "gpg-agent","gnupg", N_("Private Keys"), gc_options_gpg_agent },
1083 { "scdaemon", "gnupg", N_("Smartcards"), gc_options_scdaemon },
1084 { "gpgsm", "gnupg", N_("S/MIME"), gc_options_gpgsm },
1085 { "dirmngr", "gnupg", N_("Network"), gc_options_dirmngr },
1086 { "pinentry", "gnupg", N_("Passphrase Entry"), gc_options_pinentry }
1091 /* Structure used to collect error output of the backend programs. */
1092 struct error_line_s;
1093 typedef struct error_line_s *error_line_t;
1096 error_line_t next; /* Link to next item. */
1097 const char *fname; /* Name of the config file (points into BUFFER). */
1098 unsigned int lineno; /* Line number of the config file. */
1099 const char *errtext; /* Text of the error message (points into BUFFER). */
1100 char buffer[1]; /* Helper buffer. */
1106 /* Initialization and finalization. */
1109 gc_option_free (gc_option_t *o)
1111 if (o == NULL || o->name == NULL)
1115 gc_option_free (o + 1);
1119 gc_components_free (void)
1122 for (i = 0; i < DIM (gc_component); i++)
1123 gc_option_free (gc_component[i].options);
1127 gc_components_init (void)
1129 atexit (gc_components_free);
1134 /* Engine specific support. */
1136 gpg_agent_runtime_change (int killflag)
1138 gpg_error_t err = 0;
1139 const char *pgmname;
1140 const char *argv[5];
1141 pid_t pid = (pid_t)(-1);
1142 char *abs_homedir = NULL;
1145 pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1146 if (!gnupg_default_homedir_p ())
1148 abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1150 err = gpg_error_from_syserror ();
1152 argv[i++] = "--homedir";
1153 argv[i++] = abs_homedir;
1155 argv[i++] = "--no-autostart";
1156 argv[i++] = killflag? "KILLAGENT" : "RELOADAGENT";
1160 err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1162 err = gnupg_wait_process (pgmname, pid, 1, NULL);
1164 gc_error (0, 0, "error running '%s %s': %s",
1165 pgmname, argv[1], gpg_strerror (err));
1166 gnupg_release_process (pid);
1167 xfree (abs_homedir);
1172 scdaemon_runtime_change (int killflag)
1174 gpg_error_t err = 0;
1175 const char *pgmname;
1176 const char *argv[9];
1177 pid_t pid = (pid_t)(-1);
1178 char *abs_homedir = NULL;
1181 (void)killflag; /* For scdaemon kill and reload are synonyms. */
1183 /* We use "GETINFO app_running" to see whether the agent is already
1184 running and kill it only in this case. This avoids an explicit
1185 starting of the agent in case it is not yet running. There is
1186 obviously a race condition but that should not harm too much. */
1188 pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1189 if (!gnupg_default_homedir_p ())
1191 abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1193 err = gpg_error_from_syserror ();
1195 argv[i++] = "--homedir";
1196 argv[i++] = abs_homedir;
1199 argv[i++] = "--no-autostart";
1200 argv[i++] = "GETINFO scd_running";
1201 argv[i++] = "/if ${! $?}";
1202 argv[i++] = "scd killscd";
1207 err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1209 err = gnupg_wait_process (pgmname, pid, 1, NULL);
1211 gc_error (0, 0, "error running '%s %s': %s",
1212 pgmname, argv[4], gpg_strerror (err));
1213 gnupg_release_process (pid);
1214 xfree (abs_homedir);
1219 dirmngr_runtime_change (int killflag)
1221 gpg_error_t err = 0;
1222 const char *pgmname;
1223 const char *argv[6];
1224 pid_t pid = (pid_t)(-1);
1225 char *abs_homedir = NULL;
1227 pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1228 argv[0] = "--no-autostart";
1229 argv[1] = "--dirmngr";
1230 argv[2] = killflag? "KILLDIRMNGR" : "RELOADDIRMNGR";
1231 if (gnupg_default_homedir_p ())
1235 abs_homedir = make_absfilename_try (gnupg_homedir (), NULL);
1237 err = gpg_error_from_syserror ();
1239 argv[3] = "--homedir";
1240 argv[4] = abs_homedir;
1245 err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1247 err = gnupg_wait_process (pgmname, pid, 1, NULL);
1249 gc_error (0, 0, "error running '%s %s': %s",
1250 pgmname, argv[2], gpg_strerror (err));
1251 gnupg_release_process (pid);
1252 xfree (abs_homedir);
1256 /* Launch the gpg-agent or the dirmngr if not already running. */
1258 gc_component_launch (int component)
1261 const char *pgmname;
1262 const char *argv[3];
1268 err = gc_component_launch (GC_COMPONENT_GPG_AGENT);
1270 err = gc_component_launch (GC_COMPONENT_DIRMNGR);
1274 if (!(component == GC_COMPONENT_GPG_AGENT
1275 || component == GC_COMPONENT_DIRMNGR))
1277 es_fputs (_("Component not suitable for launching"), es_stderr);
1278 es_putc ('\n', es_stderr);
1282 pgmname = gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT);
1284 if (component == GC_COMPONENT_DIRMNGR)
1285 argv[i++] = "--dirmngr";
1289 err = gnupg_spawn_process_fd (pgmname, argv, -1, -1, -1, &pid);
1291 err = gnupg_wait_process (pgmname, pid, 1, NULL);
1293 gc_error (0, 0, "error running '%s%s%s': %s",
1295 component == GC_COMPONENT_DIRMNGR? " --dirmngr":"",
1297 gpg_strerror (err));
1298 gnupg_release_process (pid);
1303 /* Unconditionally restart COMPONENT. */
1305 gc_component_kill (int component)
1307 int runtime[GC_BACKEND_NR];
1308 gc_option_t *option;
1309 gc_backend_t backend;
1311 /* Set a flag for the backends to be reloaded. */
1312 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1313 runtime[backend] = 0;
1317 for (component = 0; component < GC_COMPONENT_NR; component++)
1319 option = gc_component[component].options;
1320 for (; option && option->name; option++)
1321 runtime[option->backend] = 1;
1326 assert (component < GC_COMPONENT_NR);
1327 option = gc_component[component].options;
1328 for (; option && option->name; option++)
1329 runtime[option->backend] = 1;
1332 /* Do the restart for the selected backends. */
1333 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1335 if (runtime[backend] && gc_backend[backend].runtime_change)
1336 (*gc_backend[backend].runtime_change) (1);
1341 /* Unconditionally reload COMPONENT or all components if COMPONENT is -1. */
1343 gc_component_reload (int component)
1345 int runtime[GC_BACKEND_NR];
1346 gc_option_t *option;
1347 gc_backend_t backend;
1349 /* Set a flag for the backends to be reloaded. */
1350 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1351 runtime[backend] = 0;
1355 for (component = 0; component < GC_COMPONENT_NR; component++)
1357 option = gc_component[component].options;
1358 for (; option && option->name; option++)
1359 runtime[option->backend] = 1;
1364 assert (component < GC_COMPONENT_NR);
1365 option = gc_component[component].options;
1366 for (; option && option->name; option++)
1367 runtime[option->backend] = 1;
1370 /* Do the reload for all selected backends. */
1371 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1373 if (runtime[backend] && gc_backend[backend].runtime_change)
1374 (*gc_backend[backend].runtime_change) (0);
1380 /* More or less Robust version of dgettext. It has the side effect of
1381 switching the codeset to utf-8 because this is what we want to
1382 output. In theory it is posible to keep the original code set and
1383 switch back for regular disgnostic output (redefine "_(" for that)
1384 but given the natur of this tool, being something invoked from
1385 other pograms, it does not make much sense. */
1387 my_dgettext (const char *domain, const char *msgid)
1389 #ifdef USE_SIMPLE_GETTEXT
1392 static int switched_codeset;
1395 if (!switched_codeset)
1397 switched_codeset = 1;
1398 gettext_use_utf8 (1);
1401 if (!strcmp (domain, "gnupg"))
1402 domain = PACKAGE_GT;
1404 /* FIXME: we have no dgettext, thus we can't switch. */
1406 text = (char*)gettext (msgid);
1407 return text ? text : msgid;
1411 #elif defined(ENABLE_NLS)
1414 static int switched_codeset;
1417 if (!switched_codeset)
1419 switched_codeset = 1;
1420 bind_textdomain_codeset (PACKAGE_GT, "utf-8");
1422 bindtextdomain (DIRMNGR_NAME, LOCALEDIR);
1423 bind_textdomain_codeset (DIRMNGR_NAME, "utf-8");
1427 /* Note: This is a hack to actually use the gnupg2 domain as
1428 long we are in a transition phase where gnupg 1.x and 1.9 may
1430 if (!strcmp (domain, "gnupg"))
1431 domain = PACKAGE_GT;
1433 text = dgettext (domain, msgid);
1434 return text ? text : msgid;
1445 /* Percent-Escape special characters. The string is valid until the
1446 next invocation of the function. */
1448 gc_percent_escape (const char *src)
1450 static char *esc_str;
1451 static int esc_str_len;
1452 int new_len = 3 * strlen (src) + 1;
1455 if (esc_str_len < new_len)
1457 char *new_esc_str = realloc (esc_str, new_len);
1459 gc_error (1, errno, "can not escape string");
1460 esc_str = new_esc_str;
1461 esc_str_len = new_len;
1473 else if (*src == ':')
1475 /* The colon is used as field separator. */
1480 else if (*src == ',')
1482 /* The comma is used as list separator. */
1497 /* Percent-Deescape special characters. The string is valid until the
1498 next invocation of the function. */
1500 percent_deescape (const char *src)
1504 int new_len = 3 * strlen (src) + 1;
1507 if (str_len < new_len)
1509 char *new_str = realloc (str, new_len);
1511 gc_error (1, errno, "can not deescape string");
1521 int val = hextobyte (src + 1);
1524 gc_error (1, 0, "malformed end of string %s", src);
1526 *(dst++) = (char) val;
1530 *(dst++) = *(src++);
1537 /* List all components that are available. */
1539 gc_component_list_components (estream_t out)
1541 gc_component_t component;
1542 gc_option_t *option;
1543 gc_backend_t backend;
1544 int backend_seen[GC_BACKEND_NR];
1546 const char *pgmname;
1548 for (component = 0; component < GC_COMPONENT_NR; component++)
1550 option = gc_component[component].options;
1553 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1554 backend_seen[backend] = 0;
1557 for (; option && option->name; option++)
1559 if ((option->flags & GC_OPT_FLAG_GROUP))
1561 backend = option->backend;
1562 if (backend_seen[backend])
1564 backend_seen[backend] = 1;
1565 assert (backend != GC_BACKEND_ANY);
1566 if (gc_backend[backend].program
1567 && !gc_backend[backend].module_name)
1569 pgmname = gnupg_module_name (gc_backend[backend].module_name);
1573 desc = gc_component[component].desc;
1574 desc = my_dgettext (gc_component[component].desc_domain, desc);
1575 es_fprintf (out, "%s:%s:",
1576 gc_component[component].name, gc_percent_escape (desc));
1577 es_fprintf (out, "%s\n", gc_percent_escape (pgmname));
1585 all_digits_p (const char *p, size_t len)
1589 for (; len; len--, p++)
1590 if (!isascii (*p) || !isdigit (*p))
1592 return 1; /* Yes. */
1596 /* Collect all error lines from stream FP. Only lines prefixed with
1597 TAG are considered. Returns a list of error line items (which may
1598 be empty). There is no error return. */
1600 collect_error_output (estream_t fp, const char *tag)
1606 error_line_t eitem, errlines, *errlines_tail;
1607 size_t taglen = strlen (tag);
1610 errlines_tail = &errlines;
1613 while ((c=es_getc (fp)) != EOF)
1616 if (pos >= sizeof buffer - 5 || c == '\n')
1618 buffer[pos - (c == '\n')] = 0;
1620 ; /*Ignore continuations of previous line. */
1621 else if (!strncmp (buffer, tag, taglen) && buffer[taglen] == ':')
1623 /* "gpgsm: foo:4: bla" */
1624 /* Yep, we are interested in this line. */
1625 p = buffer + taglen + 1;
1626 while (*p == ' ' || *p == '\t')
1628 trim_trailing_spaces (p); /* Get rid of extra CRs. */
1630 ; /* Empty lines are ignored. */
1631 else if ( (p2 = strchr (p, ':')) && (p3 = strchr (p2+1, ':'))
1632 && all_digits_p (p2+1, p3 - (p2+1)))
1634 /* Line in standard compiler format. */
1636 while (*p3 == ' ' || *p3 == '\t')
1638 eitem = xmalloc (sizeof *eitem + strlen (p));
1640 strcpy (eitem->buffer, p);
1641 eitem->fname = eitem->buffer;
1642 eitem->buffer[p2-p] = 0;
1643 eitem->errtext = eitem->buffer + (p3 - p);
1644 /* (we already checked that there are only ascii
1645 digits followed by a colon) */
1647 for (p2++; isdigit (*p2); p2++)
1648 eitem->lineno = eitem->lineno*10 + (*p2 - '0');
1649 *errlines_tail = eitem;
1650 errlines_tail = &eitem->next;
1654 /* Other error output. */
1655 eitem = xmalloc (sizeof *eitem + strlen (p));
1657 strcpy (eitem->buffer, p);
1658 eitem->fname = NULL;
1659 eitem->errtext = eitem->buffer;
1661 *errlines_tail = eitem;
1662 errlines_tail = &eitem->next;
1666 /* If this was not a complete line mark that we are in a
1668 cont_line = (c != '\n');
1672 /* We ignore error lines not terminated by a LF. */
1677 /* Check the options of a single component. Returns 0 if everything
1680 gc_component_check_options (int component, estream_t out, const char *conf_file)
1683 unsigned int result;
1684 int backend_seen[GC_BACKEND_NR];
1685 gc_backend_t backend;
1686 gc_option_t *option;
1687 const char *pgmname;
1688 const char *argv[4];
1693 error_line_t errlines;
1695 for (backend = 0; backend < GC_BACKEND_NR; backend++)
1696 backend_seen[backend] = 0;
1698 option = gc_component[component].options;
1699 for (; option && option->name; option++)
1701 if ((option->flags & GC_OPT_FLAG_GROUP))
1703 backend = option->backend;
1704 if (backend_seen[backend])
1706 backend_seen[backend] = 1;
1707 assert (backend != GC_BACKEND_ANY);
1708 if (!gc_backend[backend].program)
1710 if (!gc_backend[backend].module_name)
1715 if (! option || ! option->name)
1718 pgmname = gnupg_module_name (gc_backend[backend].module_name);
1722 argv[i++] = "--options";
1723 argv[i++] = conf_file;
1725 if (component == GC_COMPONENT_PINENTRY)
1726 argv[i++] = "--version";
1728 argv[i++] = "--gpgconf-test";
1733 err = gnupg_spawn_process (pgmname, argv, NULL, NULL, 0,
1734 NULL, NULL, &errfp, &pid);
1736 result |= 1; /* Program could not be run. */
1739 errlines = collect_error_output (errfp,
1740 gc_component[component].name);
1741 if (gnupg_wait_process (pgmname, pid, 1, &exitcode))
1744 result |= 1; /* Program could not be run or it
1745 terminated abnormally. */
1746 result |= 2; /* Program returned an error. */
1748 gnupg_release_process (pid);
1752 /* If the program could not be run, we can't tell whether
1753 the config file is good. */
1760 error_line_t errptr;
1762 desc = gc_component[component].desc;
1763 desc = my_dgettext (gc_component[component].desc_domain, desc);
1764 es_fprintf (out, "%s:%s:",
1765 gc_component[component].name, gc_percent_escape (desc));
1766 es_fputs (gc_percent_escape (pgmname), out);
1767 es_fprintf (out, ":%d:%d:", !(result & 1), !(result & 2));
1768 for (errptr = errlines; errptr; errptr = errptr->next)
1770 if (errptr != errlines)
1771 es_fputs ("\n:::::", out); /* Continuation line. */
1773 es_fputs (gc_percent_escape (errptr->fname), out);
1776 es_fprintf (out, "%u", errptr->lineno);
1778 es_fputs (gc_percent_escape (errptr->errtext), out);
1781 es_putc ('\n', out);
1786 error_line_t tmp = errlines->next;
1796 /* Check all components that are available. */
1798 gc_check_programs (estream_t out)
1800 gc_component_t component;
1802 for (component = 0; component < GC_COMPONENT_NR; component++)
1803 gc_component_check_options (component, out, NULL);
1808 /* Find the component with the name NAME. Returns -1 if not
1811 gc_component_find (const char *name)
1815 for (idx = 0; idx < GC_COMPONENT_NR; idx++)
1817 if (gc_component[idx].options
1818 && !strcmp (name, gc_component[idx].name))
1825 /* List the option OPTION. */
1827 list_one_option (const gc_option_t *option, estream_t out)
1829 const char *desc = NULL;
1830 char *arg_name = NULL;
1834 desc = my_dgettext (option->desc_domain, option->desc);
1838 const char *arg_tail = strchr (&desc[1], '|');
1842 int arg_len = arg_tail - &desc[1];
1843 arg_name = xmalloc (arg_len + 1);
1844 memcpy (arg_name, &desc[1], arg_len);
1845 arg_name[arg_len] = '\0';
1846 desc = arg_tail + 1;
1852 /* YOU MUST NOT REORDER THE FIELDS IN THIS OUTPUT, AS THEIR ORDER IS
1853 PART OF THE EXTERNAL INTERFACE. YOU MUST NOT REMOVE ANY
1856 /* The name field. */
1857 es_fprintf (out, "%s", option->name);
1859 /* The flags field. */
1860 es_fprintf (out, ":%lu", option->flags);
1866 es_fprintf (out, "none");
1869 unsigned long flags = option->flags;
1870 unsigned long flag = 0;
1871 unsigned long first = 1;
1881 es_fprintf (out, "%s", gc_flag[flag].name);
1889 /* The level field. */
1890 es_fprintf (out, ":%u", option->level);
1892 es_fprintf (out, " %s", gc_level[option->level].name);
1894 /* The description field. */
1895 es_fprintf (out, ":%s", desc ? gc_percent_escape (desc) : "");
1897 /* The type field. */
1898 es_fprintf (out, ":%u", option->arg_type);
1900 es_fprintf (out, " %s", gc_arg_type[option->arg_type].name);
1902 /* The alternate type field. */
1903 es_fprintf (out, ":%u", gc_arg_type[option->arg_type].fallback);
1905 es_fprintf (out, " %s",
1906 gc_arg_type[gc_arg_type[option->arg_type].fallback].name);
1908 /* The argument name field. */
1909 es_fprintf (out, ":%s", arg_name ? gc_percent_escape (arg_name) : "");
1912 /* The default value field. */
1913 es_fprintf (out, ":%s", option->default_value ? option->default_value : "");
1915 /* The default argument field. */
1916 es_fprintf (out, ":%s", option->default_arg ? option->default_arg : "");
1918 /* The value field. */
1919 if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE
1920 && (option->flags & GC_OPT_FLAG_LIST)
1922 /* The special format "1,1,1,1,...,1" is converted to a number
1924 es_fprintf (out, ":%u", (unsigned int)((strlen (option->value) + 1) / 2));
1926 es_fprintf (out, ":%s", option->value ? option->value : "");
1928 /* ADD NEW FIELDS HERE. */
1930 es_putc ('\n', out);
1934 /* List all options of the component COMPONENT. */
1936 gc_component_list_options (int component, estream_t out)
1938 const gc_option_t *option = gc_component[component].options;
1940 while (option && option->name)
1942 /* Do not output unknown or internal options. */
1943 if (!(option->flags & GC_OPT_FLAG_GROUP)
1944 && (!option->active || option->level == GC_LEVEL_INTERNAL))
1950 if (option->flags & GC_OPT_FLAG_GROUP)
1952 const gc_option_t *group_option = option + 1;
1953 gc_expert_level_t level = GC_LEVEL_NR;
1955 /* The manual states that the group level is always the
1956 minimum of the levels of all contained options. Due to
1957 different active options, and because it is hard to
1958 maintain manually, we calculate it here. The value in
1959 the global static table is ignored. */
1961 while (group_option->name)
1963 if (group_option->flags & GC_OPT_FLAG_GROUP)
1965 if (group_option->level < level)
1966 level = group_option->level;
1970 /* Check if group is empty. */
1971 if (level != GC_LEVEL_NR)
1973 gc_option_t opt_copy;
1975 /* Fix up the group level. */
1976 memcpy (&opt_copy, option, sizeof (opt_copy));
1977 opt_copy.level = level;
1978 list_one_option (&opt_copy, out);
1982 list_one_option (option, out);
1989 /* Find the option NAME in component COMPONENT, for the backend
1990 BACKEND. If BACKEND is GC_BACKEND_ANY, any backend will match. */
1991 static gc_option_t *
1992 find_option (gc_component_t component, const char *name,
1993 gc_backend_t backend)
1995 gc_option_t *option = gc_component[component].options;
1996 while (option->name)
1998 if (!(option->flags & GC_OPT_FLAG_GROUP)
1999 && !strcmp (option->name, name)
2000 && (backend == GC_BACKEND_ANY || option->backend == backend))
2004 return option->name ? option : NULL;
2008 /* Determine the configuration filename for the component COMPONENT
2009 and backend BACKEND. */
2011 get_config_filename (gc_component_t component, gc_backend_t backend)
2013 char *filename = NULL;
2014 gc_option_t *option = find_option
2015 (component, gc_backend[backend].option_config_filename, GC_BACKEND_ANY);
2017 assert (option->arg_type == GC_ARG_TYPE_FILENAME);
2018 assert (!(option->flags & GC_OPT_FLAG_LIST));
2020 if (!option->active || !option->default_value)
2021 gc_error (1, 0, "Option %s, needed by backend %s, was not initialized",
2022 gc_backend[backend].option_config_filename,
2023 gc_backend[backend].name);
2025 if (option->value && *option->value)
2026 filename = percent_deescape (&option->value[1]);
2027 else if (option->default_value && *option->default_value)
2028 filename = percent_deescape (&option->default_value[1]);
2032 #if HAVE_W32CE_SYSTEM
2033 if (!(filename[0] == '/' || filename[0] == '\\'))
2034 #elif defined(HAVE_DOSISH_SYSTEM)
2036 && filename[1] == ':'
2037 && (filename[2] == '/' || filename[2] == '\\')))
2039 if (filename[0] != '/')
2041 gc_error (1, 0, "Option %s, needed by backend %s, is not absolute",
2042 gc_backend[backend].option_config_filename,
2043 gc_backend[backend].name);
2049 /* Retrieve the options for the component COMPONENT from backend
2050 BACKEND, which we already know is a program-type backend. */
2052 retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
2055 const char *pgmname;
2056 const char *argv[2];
2061 size_t line_len = 0;
2064 char *config_filename;
2066 pgmname = (gc_backend[backend].module_name
2067 ? gnupg_module_name (gc_backend[backend].module_name)
2068 : gc_backend[backend].program );
2069 argv[0] = "--gpgconf-list";
2072 err = gnupg_spawn_process (pgmname, argv, NULL, NULL, 0,
2073 NULL, &outfp, NULL, &pid);
2076 gc_error (1, 0, "could not gather active options from '%s': %s",
2077 pgmname, gpg_strerror (err));
2080 while ((length = es_read_line (outfp, &line, &line_len, NULL)) > 0)
2082 gc_option_t *option;
2084 unsigned long flags = 0;
2085 char *default_value = NULL;
2087 /* Strip newline and carriage return, if present. */
2089 && (line[length - 1] == '\n' || line[length - 1] == '\r'))
2090 line[--length] = '\0';
2092 linep = strchr (line, ':');
2096 /* Extract additional flags. Default to none. */
2102 end = strchr (linep, ':');
2106 gpg_err_set_errno (0);
2107 flags = strtoul (linep, &tail, 0);
2109 gc_error (1, errno, "malformed flags in option %s from %s",
2111 if (!(*tail == '\0' || *tail == ':' || *tail == ' '))
2112 gc_error (1, 0, "garbage after flags in option %s from %s",
2118 /* Extract default value, if present. Default to empty if
2124 end = strchr (linep, ':');
2128 if (flags & GC_OPT_FLAG_DEFAULT)
2129 default_value = linep;
2134 /* Look up the option in the component and install the
2135 configuration data. */
2136 option = find_option (component, line, backend);
2140 gc_error (1, errno, "option %s returned twice from %s",
2144 option->flags |= flags;
2145 if (default_value && *default_value)
2146 option->default_value = xstrdup (default_value);
2149 if (length < 0 || es_ferror (outfp))
2150 gc_error (1, errno, "error reading from %s", pgmname);
2151 if (es_fclose (outfp))
2152 gc_error (1, errno, "error closing %s", pgmname);
2154 err = gnupg_wait_process (pgmname, pid, 1, &exitcode);
2156 gc_error (1, 0, "running %s failed (exitcode=%d): %s",
2157 pgmname, exitcode, gpg_strerror (err));
2158 gnupg_release_process (pid);
2161 /* At this point, we can parse the configuration file. */
2162 config_filename = get_config_filename (component, backend);
2164 config = es_fopen (config_filename, "r");
2167 if (errno != ENOENT)
2168 gc_error (0, errno, "warning: can not open config file %s",
2173 while ((length = es_read_line (config, &line, &line_len, NULL)) > 0)
2177 gc_option_t *option;
2180 while (*name == ' ' || *name == '\t')
2182 if (!*name || *name == '#' || *name == '\r' || *name == '\n')
2186 while (*value && *value != ' ' && *value != '\t'
2187 && *value != '#' && *value != '\r' && *value != '\n')
2189 if (*value == ' ' || *value == '\t')
2194 while (*value == ' ' || *value == '\t')
2198 while (*end && *end != '#' && *end != '\r' && *end != '\n')
2200 while (end > value && (end[-1] == ' ' || end[-1] == '\t'))
2207 /* Look up the option in the component and install the
2208 configuration data. */
2209 option = find_option (component, line, backend);
2214 if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE)
2218 "warning: ignoring argument %s for option %s",
2220 opt_value = xstrdup ("1");
2222 else if (gc_arg_type[option->arg_type].fallback
2223 == GC_ARG_TYPE_STRING)
2224 opt_value = xasprintf ("\"%s", gc_percent_escape (value));
2227 /* FIXME: Verify that the number is sane. */
2228 opt_value = xstrdup (value);
2231 /* Now enter the option into the table. */
2232 if (!(option->flags & GC_OPT_FLAG_LIST))
2235 xfree (option->value);
2236 option->value = opt_value;
2241 option->value = opt_value;
2244 char *old = option->value;
2245 option->value = xasprintf ("%s,%s", old, opt_value);
2253 if (length < 0 || es_ferror (config))
2254 gc_error (1, errno, "error reading from %s", config_filename);
2255 if (es_fclose (config))
2256 gc_error (1, errno, "error closing %s", config_filename);
2263 /* Retrieve the options for the component COMPONENT from backend
2264 BACKEND, which we already know is of type file list. */
2266 retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
2268 gc_option_t *list_option;
2269 gc_option_t *config_option;
2270 char *list_filename;
2273 size_t line_len = 0;
2277 list_option = find_option (component,
2278 gc_backend[backend].option_name, GC_BACKEND_ANY);
2279 assert (list_option);
2280 assert (!list_option->active);
2282 list_filename = get_config_filename (component, backend);
2283 list_file = fopen (list_filename, "r");
2285 gc_error (0, errno, "warning: can not open list file %s", list_filename);
2289 while ((length = read_line (list_file, &line, &line_len, NULL)) > 0)
2296 while (*start == ' ' || *start == '\t')
2298 if (!*start || *start == '#' || *start == '\r' || *start == '\n')
2302 while (*end && *end != '#' && *end != '\r' && *end != '\n')
2304 /* Walk back to skip trailing white spaces. Looks evil, but
2305 works because of the conditions on START and END imposed
2306 at this point (END is at least START + 1, and START is
2307 not a whitespace character). */
2308 while (*(end - 1) == ' ' || *(end - 1) == '\t')
2311 /* FIXME: Oh, no! This is so lame! Should use realloc and
2315 new_list = xasprintf ("%s,\"%s", list, gc_percent_escape (start));
2320 list = xasprintf ("\"%s", gc_percent_escape (start));
2322 if (length < 0 || ferror (list_file))
2323 gc_error (1, errno, "can not read list file %s", list_filename);
2326 list_option->active = 1;
2327 list_option->value = list;
2329 /* Fix up the read-only flag. */
2330 config_option = find_option
2331 (component, gc_backend[backend].option_config_filename, GC_BACKEND_ANY);
2332 if (config_option->flags & GC_OPT_FLAG_NO_CHANGE)
2333 list_option->flags |= GC_OPT_FLAG_NO_CHANGE;
2335 if (list_file && fclose (list_file))
2336 gc_error (1, errno, "error closing %s", list_filename);
2341 /* Retrieve the currently active options and their defaults from all
2342 involved backends for this component. Using -1 for component will
2343 retrieve all options from all components. */
2345 gc_component_retrieve_options (int component)
2347 int process_all = 0;
2348 int backend_seen[GC_BACKEND_NR];
2349 gc_backend_t backend;
2350 gc_option_t *option;
2352 for (backend = 0; backend < GC_BACKEND_NR; backend++)
2353 backend_seen[backend] = 0;
2355 if (component == -1)
2359 assert (component < GC_COMPONENT_NR);
2364 if (component == GC_COMPONENT_PINENTRY)
2365 continue; /* Skip this dummy component. */
2367 option = gc_component[component].options;
2369 while (option && option->name)
2371 if (!(option->flags & GC_OPT_FLAG_GROUP))
2373 backend = option->backend;
2375 if (backend_seen[backend])
2380 backend_seen[backend] = 1;
2382 assert (backend != GC_BACKEND_ANY);
2384 if (gc_backend[backend].program)
2385 retrieve_options_from_program (component, backend);
2387 retrieve_options_from_file (component, backend);
2392 while (process_all && ++component < GC_COMPONENT_NR);
2398 /* Perform a simple validity check based on the type. Return in
2399 * NEW_VALUE_NR the value of the number in NEW_VALUE if OPTION is of
2400 * type GC_ARG_TYPE_NONE. If VERBATIM is set the profile parsing mode
2403 option_check_validity (gc_option_t *option, unsigned long flags,
2404 char *new_value, unsigned long *new_value_nr,
2409 if (!option->active)
2410 gc_error (1, 0, "option %s not supported by backend %s",
2411 option->name, gc_backend[option->backend].name);
2413 if (option->new_flags || option->new_value)
2414 gc_error (1, 0, "option %s already changed", option->name);
2416 if (flags & GC_OPT_FLAG_DEFAULT)
2419 gc_error (1, 0, "argument %s provided for deleted option %s",
2420 new_value, option->name);
2425 /* GC_ARG_TYPE_NONE options have special list treatment. */
2426 if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE)
2430 gpg_err_set_errno (0);
2431 *new_value_nr = strtoul (new_value, &tail, 0);
2434 gc_error (1, errno, "invalid argument for option %s",
2437 gc_error (1, 0, "garbage after argument for option %s",
2440 if (!(option->flags & GC_OPT_FLAG_LIST))
2442 if (*new_value_nr != 1)
2443 gc_error (1, 0, "argument for non-list option %s of type 0 "
2444 "(none) must be 1", option->name);
2448 if (*new_value_nr == 0)
2449 gc_error (1, 0, "argument for option %s of type 0 (none) "
2450 "must be positive", option->name);
2459 if (*arg == '\0' || (*arg == ',' && !verbatim))
2461 if (!(option->flags & GC_OPT_FLAG_ARG_OPT))
2462 gc_error (1, 0, "argument required for option %s", option->name);
2464 if (*arg == ',' && !verbatim && !(option->flags & GC_OPT_FLAG_LIST))
2465 gc_error (1, 0, "list found for non-list option %s", option->name);
2467 else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_STRING)
2469 if (*arg != '"' && !verbatim)
2470 gc_error (1, 0, "string argument for option %s must begin "
2471 "with a quote (\") character", option->name);
2473 /* FIXME: We do not allow empty string arguments for now, as
2474 we do not quote arguments in configuration files, and
2475 thus no argument is indistinguishable from the empty
2477 if (arg[1] == '\0' || (arg[1] == ',' && !verbatim))
2478 gc_error (1, 0, "empty string argument for option %s is "
2479 "currently not allowed. Please report this!",
2482 else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_INT32)
2486 gpg_err_set_errno (0);
2487 res = strtol (arg, &arg, 0);
2491 gc_error (1, errno, "invalid argument for option %s",
2494 if (*arg != '\0' && (*arg != ',' || verbatim))
2495 gc_error (1, 0, "garbage after argument for option %s",
2498 else if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_UINT32)
2502 gpg_err_set_errno (0);
2503 res = strtoul (arg, &arg, 0);
2507 gc_error (1, errno, "invalid argument for option %s",
2510 if (*arg != '\0' && (*arg != ',' || verbatim))
2511 gc_error (1, 0, "garbage after argument for option %s",
2514 arg = verbatim? strchr (arg, ',') : NULL;
2518 while (arg && *arg);
2522 #ifdef HAVE_W32_SYSTEM
2524 copy_file (const char *src_name, const char *dst_name)
2526 #define BUF_LEN 4096
2527 char buffer[BUF_LEN];
2532 src = fopen (src_name, "r");
2536 dst = fopen (dst_name, "w");
2539 int saved_err = errno;
2541 gpg_err_set_errno (saved_err);
2549 len = fread (buffer, 1, BUF_LEN, src);
2552 written = fwrite (buffer, 1, len, dst);
2556 while (!feof (src) && !ferror (src) && !ferror (dst));
2558 if (ferror (src) || ferror (dst) || !feof (src))
2560 int saved_errno = errno;
2564 gpg_err_set_errno (saved_errno);
2569 gc_error (1, errno, "error closing %s", dst_name);
2571 gc_error (1, errno, "error closing %s", src_name);
2575 #endif /* HAVE_W32_SYSTEM */
2578 /* Create and verify the new configuration file for the specified
2579 backend and component. Returns 0 on success and -1 on error. */
2581 change_options_file (gc_component_t component, gc_backend_t backend,
2582 char **src_filenamep, char **dest_filenamep,
2583 char **orig_filenamep)
2585 static const char marker[] = "###+++--- " GPGCONF_DISP_NAME " ---+++###";
2586 /* True if we are within the marker in the config file. */
2588 gc_option_t *option;
2594 FILE *src_file = NULL;
2595 FILE *dest_file = NULL;
2597 char *dest_filename;
2598 char *orig_filename;
2600 char *cur_arg = NULL;
2602 option = find_option (component,
2603 gc_backend[backend].option_name, GC_BACKEND_ANY);
2605 assert (option->active);
2606 assert (gc_arg_type[option->arg_type].fallback != GC_ARG_TYPE_NONE);
2608 /* FIXME. Throughout the function, do better error reporting. */
2609 /* Note that get_config_filename() calls percent_deescape(), so we
2610 call this before processing the arguments. */
2611 dest_filename = xstrdup (get_config_filename (component, backend));
2612 src_filename = xasprintf ("%s.%s.%i.new",
2613 dest_filename, GPGCONF_NAME, (int)getpid ());
2614 orig_filename = xasprintf ("%s.%s.%i.bak",
2615 dest_filename, GPGCONF_NAME, (int)getpid ());
2617 arg = option->new_value;
2618 if (arg && arg[0] == '\0')
2625 end = strchr (arg, ',');
2629 cur_arg = percent_deescape (arg);
2639 #ifdef HAVE_W32_SYSTEM
2640 res = copy_file (dest_filename, orig_filename);
2642 res = link (dest_filename, orig_filename);
2644 if (res < 0 && errno != ENOENT)
2646 xfree (dest_filename);
2647 xfree (src_filename);
2648 xfree (orig_filename);
2653 xfree (orig_filename);
2654 orig_filename = NULL;
2657 /* We now initialize the return strings, so the caller can do the
2659 *src_filenamep = src_filename;
2660 *dest_filenamep = dest_filename;
2661 *orig_filenamep = orig_filename;
2663 /* Use open() so that we can use O_EXCL. */
2664 fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
2667 src_file = fdopen (fd, "w");
2671 gpg_err_set_errno (res);
2675 /* Only if ORIG_FILENAME is not NULL did the configuration file
2676 exist already. In this case, we will copy its content into the
2677 new configuration file, changing it to our liking in the
2681 dest_file = fopen (dest_filename, "r");
2683 goto change_file_one_err;
2685 while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
2690 if (!strncmp (marker, line, sizeof (marker) - 1))
2699 while (*start == ' ' || *start == '\t')
2701 if (*start && *start != '\r' && *start != '\n' && *start != '#')
2710 /* Search for the end of the line. */
2711 while (*endp && *endp != '#' && *endp != '\r' && *endp != '\n')
2714 if (*endp && *endp != ' ' && *endp != '\t'
2715 && *endp != '\r' && *endp != '\n' && *endp != '#')
2721 if ((option->new_flags & GC_OPT_FLAG_DEFAULT)
2722 || !cur_arg || strcmp (start, cur_arg))
2726 /* Find next argument. */
2732 arg_end = strchr (arg, ',');
2736 cur_arg = percent_deescape (arg);
2757 "# %s disabled this option here at %s\n",
2758 GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
2759 if (ferror (src_file))
2760 goto change_file_one_err;
2761 fprintf (src_file, "# %s", line);
2762 if (ferror (src_file))
2763 goto change_file_one_err;
2768 fprintf (src_file, "%s", line);
2769 if (ferror (src_file))
2770 goto change_file_one_err;
2773 if (length < 0 || ferror (dest_file))
2774 goto change_file_one_err;
2779 /* There was no marker. This is the first time we edit the
2780 file. We add our own marker at the end of the file and
2781 proceed. Note that we first write a newline, this guards us
2782 against files which lack the newline at the end of the last
2783 line, while it doesn't hurt us in all other cases. */
2784 fprintf (src_file, "\n%s\n", marker);
2785 if (ferror (src_file))
2786 goto change_file_one_err;
2789 /* At this point, we have copied everything up to the end marker
2790 into the new file, except for the arguments we are going to add.
2791 Now, dump the new arguments and write the end marker, possibly
2792 followed by the rest of the original file. */
2795 fprintf (src_file, "%s\n", cur_arg);
2797 /* Find next argument. */
2803 end = strchr (arg, ',');
2807 cur_arg = percent_deescape (arg);
2820 fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
2821 if (ferror (src_file))
2822 goto change_file_one_err;
2826 fprintf (src_file, "# %s edited this configuration file.\n",
2828 if (ferror (src_file))
2829 goto change_file_one_err;
2830 fprintf (src_file, "# It will disable options before this marked "
2831 "block, but it will\n");
2832 if (ferror (src_file))
2833 goto change_file_one_err;
2834 fprintf (src_file, "# never change anything below these lines.\n");
2835 if (ferror (src_file))
2836 goto change_file_one_err;
2840 while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
2842 fprintf (src_file, "%s", line);
2843 if (ferror (src_file))
2844 goto change_file_one_err;
2846 if (length < 0 || ferror (dest_file))
2847 goto change_file_one_err;
2852 res = fclose (src_file);
2859 gpg_err_set_errno (res);
2865 res = fclose (dest_file);
2871 change_file_one_err:
2881 gpg_err_set_errno (res);
2886 /* Create and verify the new configuration file for the specified
2887 * backend and component. Returns 0 on success and -1 on error. If
2888 * VERBATIM is set the profile mode is used. */
2890 change_options_program (gc_component_t component, gc_backend_t backend,
2891 char **src_filenamep, char **dest_filenamep,
2892 char **orig_filenamep,
2895 static const char marker[] = "###+++--- " GPGCONF_DISP_NAME " ---+++###";
2896 /* True if we are within the marker in the config file. */
2898 gc_option_t *option;
2904 FILE *src_file = NULL;
2905 FILE *dest_file = NULL;
2907 char *dest_filename;
2908 char *orig_filename;
2909 /* Special hack for gpg, see below. */
2910 int utf8strings_seen = 0;
2912 /* FIXME. Throughout the function, do better error reporting. */
2913 dest_filename = xstrdup (get_config_filename (component, backend));
2914 src_filename = xasprintf ("%s.%s.%i.new",
2915 dest_filename, GPGCONF_NAME, (int)getpid ());
2916 orig_filename = xasprintf ("%s.%s.%i.bak",
2917 dest_filename, GPGCONF_NAME, (int)getpid ());
2919 #ifdef HAVE_W32_SYSTEM
2920 res = copy_file (dest_filename, orig_filename);
2922 res = link (dest_filename, orig_filename);
2924 if (res < 0 && errno != ENOENT)
2926 xfree (dest_filename);
2927 xfree (src_filename);
2928 xfree (orig_filename);
2933 xfree (orig_filename);
2934 orig_filename = NULL;
2937 /* We now initialize the return strings, so the caller can do the
2939 *src_filenamep = src_filename;
2940 *dest_filenamep = dest_filename;
2941 *orig_filenamep = orig_filename;
2943 /* Use open() so that we can use O_EXCL. */
2944 fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
2947 src_file = fdopen (fd, "w");
2951 gpg_err_set_errno (res);
2955 /* Only if ORIG_FILENAME is not NULL did the configuration file
2956 exist already. In this case, we will copy its content into the
2957 new configuration file, changing it to our liking in the
2961 dest_file = fopen (dest_filename, "r");
2963 goto change_one_err;
2965 while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
2970 if (!strncmp (marker, line, sizeof (marker) - 1))
2977 else if (backend == GC_BACKEND_GPG && in_marker
2978 && ! strcmp ("utf8-strings\n", line))
2980 /* Strip duplicated entries. */
2981 if (utf8strings_seen)
2984 utf8strings_seen = 1;
2988 while (*start == ' ' || *start == '\t')
2990 if (*start && *start != '\r' && *start != '\n' && *start != '#')
2996 while (*end && *end != ' ' && *end != '\t'
2997 && *end != '\r' && *end != '\n' && *end != '#')
3002 option = find_option (component, start, backend);
3004 if (option && ((option->new_flags & GC_OPT_FLAG_DEFAULT)
3005 || option->new_value))
3013 "# %s disabled this option here at %s\n",
3014 GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
3015 if (ferror (src_file))
3016 goto change_one_err;
3017 fprintf (src_file, "# %s", line);
3018 if (ferror (src_file))
3019 goto change_one_err;
3024 fprintf (src_file, "%s", line);
3025 if (ferror (src_file))
3026 goto change_one_err;
3029 if (length < 0 || ferror (dest_file))
3030 goto change_one_err;
3035 /* There was no marker. This is the first time we edit the
3036 file. We add our own marker at the end of the file and
3037 proceed. Note that we first write a newline, this guards us
3038 against files which lack the newline at the end of the last
3039 line, while it doesn't hurt us in all other cases. */
3040 fprintf (src_file, "\n%s\n", marker);
3041 if (ferror (src_file))
3042 goto change_one_err;
3044 /* At this point, we have copied everything up to the end marker
3045 into the new file, except for the options we are going to change.
3046 Now, dump the changed options (except for those we are going to
3047 revert to their default), and write the end marker, possibly
3048 followed by the rest of the original file. */
3050 /* We have to turn on UTF8 strings for GnuPG. */
3051 if (backend == GC_BACKEND_GPG && ! utf8strings_seen)
3052 fprintf (src_file, "utf8-strings\n");
3054 option = gc_component[component].options;
3055 while (option->name)
3057 if (!(option->flags & GC_OPT_FLAG_GROUP)
3058 && option->backend == backend
3059 && option->new_value)
3061 char *arg = option->new_value;
3065 if (*arg == '\0' || *arg == ',')
3067 fprintf (src_file, "%s\n", option->name);
3068 if (ferror (src_file))
3069 goto change_one_err;
3071 else if (gc_arg_type[option->arg_type].fallback
3072 == GC_ARG_TYPE_NONE)
3074 assert (*arg == '1');
3075 fprintf (src_file, "%s\n", option->name);
3076 if (ferror (src_file))
3077 goto change_one_err;
3081 else if (gc_arg_type[option->arg_type].fallback
3082 == GC_ARG_TYPE_STRING)
3088 log_assert (*arg == '"');
3091 end = strchr (arg, ',');
3098 fprintf (src_file, "%s %s\n", option->name,
3099 verbatim? arg : percent_deescape (arg));
3100 if (ferror (src_file))
3101 goto change_one_err;
3111 end = strchr (arg, ',');
3115 fprintf (src_file, "%s %s\n", option->name, arg);
3116 if (ferror (src_file))
3117 goto change_one_err;
3124 assert (arg == NULL || *arg == '\0' || *arg == ',');
3125 if (arg && *arg == ',')
3128 while (arg && *arg);
3133 fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
3134 if (ferror (src_file))
3135 goto change_one_err;
3139 fprintf (src_file, "# %s edited this configuration file.\n",
3141 if (ferror (src_file))
3142 goto change_one_err;
3143 fprintf (src_file, "# It will disable options before this marked "
3144 "block, but it will\n");
3145 if (ferror (src_file))
3146 goto change_one_err;
3147 fprintf (src_file, "# never change anything below these lines.\n");
3148 if (ferror (src_file))
3149 goto change_one_err;
3153 while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
3155 fprintf (src_file, "%s", line);
3156 if (ferror (src_file))
3157 goto change_one_err;
3159 if (length < 0 || ferror (dest_file))
3160 goto change_one_err;
3165 res = fclose (src_file);
3172 gpg_err_set_errno (res);
3178 res = fclose (dest_file);
3194 gpg_err_set_errno (res);
3199 /* Common code for gc_component_change_options and
3200 * gc_process_gpgconf_conf. If VERBATIM is set the profile parsing
3203 change_one_value (gc_option_t *option, int *runtime,
3204 unsigned long flags, char *new_value, int verbatim)
3206 unsigned long new_value_nr = 0;
3208 option_check_validity (option, flags, new_value, &new_value_nr, verbatim);
3210 if (option->flags & GC_OPT_FLAG_RUNTIME)
3211 runtime[option->backend] = 1;
3213 option->new_flags = flags;
3214 if (!(flags & GC_OPT_FLAG_DEFAULT))
3216 if (gc_arg_type[option->arg_type].fallback == GC_ARG_TYPE_NONE
3217 && (option->flags & GC_OPT_FLAG_LIST))
3221 /* We convert the number to a list of 1's for convenient
3223 assert (new_value_nr > 0);
3224 option->new_value = xmalloc ((2 * (new_value_nr - 1) + 1) + 1);
3225 str = option->new_value;
3227 while (--new_value_nr > 0)
3235 option->new_value = xstrdup (new_value);
3240 /* Read the modifications from IN and apply them. If IN is NULL the
3241 modifications are expected to already have been set to the global
3242 table. If VERBATIM is set the profile mode is used. */
3244 gc_component_change_options (int component, estream_t in, estream_t out,
3248 int runtime[GC_BACKEND_NR];
3249 char *src_filename[GC_BACKEND_NR];
3250 char *dest_filename[GC_BACKEND_NR];
3251 char *orig_filename[GC_BACKEND_NR];
3252 gc_backend_t backend;
3253 gc_option_t *option;
3255 size_t line_len = 0;
3258 if (component == GC_COMPONENT_PINENTRY)
3259 return; /* Dummy component for now. */
3261 for (backend = 0; backend < GC_BACKEND_NR; backend++)
3263 runtime[backend] = 0;
3264 src_filename[backend] = NULL;
3265 dest_filename[backend] = NULL;
3266 orig_filename[backend] = NULL;
3271 /* Read options from the file IN. */
3272 while ((length = es_read_line (in, &line, &line_len, NULL)) > 0)
3275 unsigned long flags = 0;
3276 char *new_value = "";
3278 /* Strip newline and carriage return, if present. */
3280 && (line[length - 1] == '\n' || line[length - 1] == '\r'))
3281 line[--length] = '\0';
3283 linep = strchr (line, ':');
3287 /* Extract additional flags. Default to none. */
3293 end = strchr (linep, ':');
3297 gpg_err_set_errno (0);
3298 flags = strtoul (linep, &tail, 0);
3300 gc_error (1, errno, "malformed flags in option %s", line);
3301 if (!(*tail == '\0' || *tail == ':' || *tail == ' '))
3302 gc_error (1, 0, "garbage after flags in option %s", line);
3307 /* Don't allow setting of the no change flag. */
3308 flags &= ~GC_OPT_FLAG_NO_CHANGE;
3310 /* Extract default value, if present. Default to empty if not. */
3314 end = strchr (linep, ':');
3321 option = find_option (component, line, GC_BACKEND_ANY);
3323 gc_error (1, 0, "unknown option %s", line);
3325 if ((option->flags & GC_OPT_FLAG_NO_CHANGE))
3327 gc_error (0, 0, "ignoring new value for option %s",
3332 change_one_value (option, runtime, flags, new_value, 0);
3334 if (length < 0 || gpgrt_ferror (in))
3335 gc_error (1, errno, "error reading stream 'in'");
3338 /* Now that we have collected and locally verified the changes,
3339 write them out to new configuration files, verify them
3340 externally, and then commit them. */
3341 option = gc_component[component].options;
3342 while (option && option->name)
3344 /* Go on if we have already seen this backend, or if there is
3346 if (src_filename[option->backend]
3347 || !(option->new_flags || option->new_value))
3353 if (gc_backend[option->backend].program)
3355 err = change_options_program (component, option->backend,
3356 &src_filename[option->backend],
3357 &dest_filename[option->backend],
3358 &orig_filename[option->backend],
3362 /* External verification. */
3363 err = gc_component_check_options (component, out,
3364 src_filename[option->backend]);
3368 _("External verification of component %s failed"),
3369 gc_component[component].name);
3370 gpg_err_set_errno (EINVAL);
3376 err = change_options_file (component, option->backend,
3377 &src_filename[option->backend],
3378 &dest_filename[option->backend],
3379 &orig_filename[option->backend]);
3387 if (! err && ! opt.dry_run)
3391 for (i = 0; i < GC_BACKEND_NR; i++)
3393 if (src_filename[i])
3395 /* FIXME: Make a verification here. */
3397 assert (dest_filename[i]);
3399 if (orig_filename[i])
3401 #ifdef HAVE_W32_SYSTEM
3402 /* There is no atomic update on W32. */
3403 err = unlink (dest_filename[i]);
3404 #endif /* HAVE_W32_SYSTEM */
3406 err = rename (src_filename[i], dest_filename[i]);
3410 #ifdef HAVE_W32_SYSTEM
3411 /* We skip the unlink if we expect the file not to
3413 err = rename (src_filename[i], dest_filename[i]);
3414 #else /* HAVE_W32_SYSTEM */
3415 /* This is a bit safer than rename() because we
3416 expect DEST_FILENAME not to be there. If it
3417 happens to be there, this will fail. */
3418 err = link (src_filename[i], dest_filename[i]);
3420 err = unlink (src_filename[i]);
3421 #endif /* !HAVE_W32_SYSTEM */
3425 xfree (src_filename[i]);
3426 src_filename[i] = NULL;
3431 if (err || opt.dry_run)
3434 int saved_errno = errno;
3436 /* An error occurred or a dry-run is requested. */
3437 for (i = 0; i < GC_BACKEND_NR; i++)
3439 if (src_filename[i])
3441 /* The change was not yet committed. */
3442 unlink (src_filename[i]);
3443 if (orig_filename[i])
3444 unlink (orig_filename[i]);
3448 /* The changes were already committed. FIXME: This is a
3449 tad dangerous, as we don't know if we don't overwrite
3450 a version of the file that is even newer than the one
3451 we just installed. */
3452 if (orig_filename[i])
3454 #ifdef HAVE_W32_SYSTEM
3455 /* There is no atomic update on W32. */
3456 unlink (dest_filename[i]);
3457 #endif /* HAVE_W32_SYSTEM */
3458 rename (orig_filename[i], dest_filename[i]);
3461 unlink (dest_filename[i]);
3465 gc_error (1, saved_errno, "could not commit changes");
3467 /* Fall-through for dry run. */
3471 /* If it all worked, notify the daemons of the changes. */
3473 for (backend = 0; backend < GC_BACKEND_NR; backend++)
3475 if (runtime[backend] && gc_backend[backend].runtime_change)
3476 (*gc_backend[backend].runtime_change) (0);
3479 /* Move the per-process backup file into its place. */
3480 for (backend = 0; backend < GC_BACKEND_NR; backend++)
3481 if (orig_filename[backend])
3483 char *backup_filename;
3485 assert (dest_filename[backend]);
3487 backup_filename = xasprintf ("%s.%s.bak",
3488 dest_filename[backend], GPGCONF_NAME);
3490 #ifdef HAVE_W32_SYSTEM
3491 /* There is no atomic update on W32. */
3492 unlink (backup_filename);
3493 #endif /* HAVE_W32_SYSTEM */
3494 rename (orig_filename[backend], backup_filename);
3495 xfree (backup_filename);
3500 for (backend = 0; backend < GC_BACKEND_NR; backend++)
3502 xfree (src_filename[backend]);
3503 xfree (dest_filename[backend]);
3504 xfree (orig_filename[backend]);
3509 /* Check whether USER matches the current user of one of its group.
3510 This function may change USER. Returns true is there is a
3513 key_matches_user_or_group (char *user)
3517 if (*user == '*' && user[1] == 0)
3518 return 1; /* A single asterisk matches all users. */
3520 group = strchr (user, ':');
3524 #ifdef HAVE_W32_SYSTEM
3525 /* Under Windows we don't support groups. */
3526 if (group && *group)
3527 gc_error (0, 0, _("Note that group specifications are ignored\n"));
3528 #ifndef HAVE_W32CE_SYSTEM
3531 static char *my_name;
3538 GetUserNameA (tmp, &size);
3539 my_name = xmalloc (size);
3540 if (!GetUserNameA (my_name, &size))
3541 gc_error (1,0, "error getting current user name: %s",
3545 if (!strcmp (user, my_name))
3546 return 1; /* Found. */
3548 #endif /*HAVE_W32CE_SYSTEM*/
3549 #else /*!HAVE_W32_SYSTEM*/
3550 /* First check whether the user matches. */
3553 static char *my_name;
3557 struct passwd *pw = getpwuid ( getuid () );
3559 gc_error (1, errno, "getpwuid failed for current user");
3560 my_name = xstrdup (pw->pw_name);
3562 if (!strcmp (user, my_name))
3563 return 1; /* Found. */
3566 /* If that failed, check whether a group matches. */
3567 if (group && *group)
3569 static char *my_group;
3570 static char **my_supgroups;
3575 struct group *gr = getgrgid ( getgid () );
3577 gc_error (1, errno, "getgrgid failed for current user");
3578 my_group = xstrdup (gr->gr_name);
3580 if (!strcmp (group, my_group))
3581 return 1; /* Found. */
3588 ngids = getgroups (0, NULL);
3589 gids = xcalloc (ngids+1, sizeof *gids);
3590 ngids = getgroups (ngids, gids);
3592 gc_error (1, errno, "getgroups failed for current user");
3593 my_supgroups = xcalloc (ngids+1, sizeof *my_supgroups);
3594 for (n=0; n < ngids; n++)
3596 struct group *gr = getgrgid ( gids[n] );
3598 gc_error (1, errno, "getgrgid failed for supplementary group");
3599 my_supgroups[n] = xstrdup (gr->gr_name);
3604 for (n=0; my_supgroups[n]; n++)
3605 if (!strcmp (group, my_supgroups[n]))
3606 return 1; /* Found. */
3608 #endif /*!HAVE_W32_SYSTEM*/
3609 return 0; /* No match. */
3614 /* Read and process the global configuration file for gpgconf. This
3615 optional file is used to update our internal tables at runtime and
3616 may also be used to set new default values. If FNAME is NULL the
3617 default name will be used. With UPDATE set to true the internal
3618 tables are actually updated; if not set, only a syntax check is
3619 done. If DEFAULTS is true the global options are written to the
3620 configuration files. If LISTFP is set, no changes are done but the
3621 configuration file is printed to LISTFP in a colon separated format.
3623 Returns 0 on success or if the config file is not present; -1 is
3624 returned on error. */
3626 gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
3631 size_t line_len = 0;
3637 int runtime[GC_BACKEND_NR];
3638 int backend_id, component_id;
3642 fname = xstrdup (fname_arg);
3644 fname = make_filename (gnupg_sysconfdir (), GPGCONF_NAME EXTSEP_S "conf",
3647 for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3648 runtime[backend_id] = 0;
3650 config = fopen (fname, "r");
3653 /* Do not print an error if the file is not available, except
3654 when running in syntax check mode. */
3655 if (errno != ENOENT || !update)
3657 gc_error (0, errno, "can not open global config file '%s'", fname);
3664 while ((length = read_line (config, &line, &line_len, NULL)) > 0)
3666 char *key, *component, *option, *flags, *value;
3668 gc_option_t *option_info = NULL;
3670 int is_continuation;
3674 while (*key == ' ' || *key == '\t')
3676 if (!*key || *key == '#' || *key == '\r' || *key == '\n')
3679 is_continuation = (key != line);
3681 /* Parse the key field. */
3682 if (!is_continuation && got_match)
3683 break; /* Finish after the first match. */
3684 else if (!is_continuation)
3687 for (p=key+1; *p && !strchr (" \t\r\n", *p); p++)
3691 gc_error (0, 0, "missing rule at '%s', line %d", fname, lineno);
3700 gc_error (0, 0, "continuation but no rule at '%s', line %d",
3713 /* Parse the component. */
3714 while (*component == ' ' || *component == '\t')
3716 for (p=component; *p && !strchr (" \t\r\n", *p); p++)
3720 gc_error (0, 0, "missing component at '%s', line %d",
3728 component_id = gc_component_find (component);
3729 if (component_id < 0)
3731 gc_error (0, 0, "unknown component at '%s', line %d",
3736 /* Parse the option name. */
3737 while (*option == ' ' || *option == '\t')
3739 for (p=option; *p && !strchr (" \t\r\n", *p); p++)
3743 gc_error (0, 0, "missing option at '%s', line %d",
3750 if ( component_id != -1)
3752 option_info = find_option (component_id, option, GC_BACKEND_ANY);
3755 gc_error (0, 0, "unknown option at '%s', line %d",
3762 /* Parse the optional flags. */
3763 while (*flags == ' ' || *flags == '\t')
3768 p = strchr (flags, ']');
3771 gc_error (0, 0, "syntax error in rule at '%s', line %d",
3779 else /* No flags given. */
3785 /* Parse the optional value. */
3786 while (*value == ' ' || *value == '\t')
3788 for (p=value; *p && !strchr ("\r\n", *p); p++)
3791 value = empty; /* No value given; let it point to an empty string. */
3794 /* Strip trailing white space. */
3796 for (p--; p > value && (*p == ' ' || *p == '\t'); p--)
3800 /* Check flag combinations. */
3803 else if (!strcmp (flags, "default"))
3807 gc_error (0, 0, "flag \"default\" may not be combined "
3808 "with a value at '%s', line %d",
3813 else if (!strcmp (flags, "change"))
3815 else if (!strcmp (flags, "no-change"))
3819 gc_error (0, 0, "unknown flag at '%s', line %d",
3824 /* In list mode we print out all records. */
3825 if (listfp && !result)
3827 /* If this is a new ruleset, print a key record. */
3828 if (!is_continuation)
3830 char *group = strchr (key, ':');
3834 if ((p = strchr (group, ':')))
3835 *p = 0; /* We better strip any extra stuff. */
3838 es_fprintf (listfp, "k:%s:", gc_percent_escape (key));
3839 es_fprintf (listfp, "%s\n", group? gc_percent_escape (group):"");
3842 /* All other lines are rule records. */
3843 es_fprintf (listfp, "r:::%s:%s:%s:",
3844 gc_component[component_id].name,
3845 option_info->name? option_info->name : "",
3848 es_fprintf (listfp, "\"%s", gc_percent_escape (value));
3850 es_putc ('\n', listfp);
3853 /* Check whether the key matches but do this only if we are not
3854 running in syntax check mode. */
3856 && !result && !listfp
3857 && (got_match || (key && key_matches_user_or_group (key))) )
3863 /* Apply the flags from gpgconf.conf. */
3866 else if (!strcmp (flags, "default"))
3867 newflags |= GC_OPT_FLAG_DEFAULT;
3868 else if (!strcmp (flags, "no-change"))
3869 option_info->flags |= GC_OPT_FLAG_NO_CHANGE;
3870 else if (!strcmp (flags, "change"))
3871 option_info->flags &= ~GC_OPT_FLAG_NO_CHANGE;
3875 /* Here we explicitly allow updating the value again. */
3878 option_info->new_flags = 0;
3882 xfree (option_info->new_value);
3883 option_info->new_value = NULL;
3885 change_one_value (option_info, runtime, newflags, value, 0);
3890 if (length < 0 || ferror (config))
3892 gc_error (0, errno, "error reading from '%s'", fname);
3895 if (fclose (config))
3896 gc_error (0, errno, "error closing '%s'", fname);
3900 /* If it all worked, process the options. */
3901 if (!result && update && defaults && !listfp)
3903 /* We need to switch off the runtime update, so that we can do
3904 it later all at once. */
3905 int save_opt_runtime = opt.runtime;
3908 for (component_id = 0; component_id < GC_COMPONENT_NR; component_id++)
3910 gc_component_change_options (component_id, NULL, NULL, 0);
3912 opt.runtime = save_opt_runtime;
3916 for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3917 if (runtime[backend_id] && gc_backend[backend_id].runtime_change)
3918 (*gc_backend[backend_id].runtime_change) (0);
3928 * Apply the profile FNAME to all known configure files.
3931 gc_apply_profile (const char *fname)
3934 char *fname_buffer = NULL;
3936 size_t line_len = 0;
3940 int runtime[GC_BACKEND_NR];
3942 int component_id = -1;
3943 int skip_section = 0;
3944 int error_count = 0;
3950 for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
3951 runtime[backend_id] = 0;
3954 if (!(!strcmp (fname, "-")
3955 || strchr (fname, '/')
3956 #ifdef HAVE_W32_SYSTEM
3957 || strchr (fname, '\\')
3959 || strchr (fname, '.')))
3961 /* FNAME looks like a standard profile name. Check whether one
3962 * is installed and use that instead of the given file name. */
3963 fname_buffer = xstrconcat (gnupg_datadir (), DIRSEP_S,
3964 fname, ".prf", NULL);
3965 if (!access (fname_buffer, F_OK))
3966 fname = fname_buffer;
3969 fp = !strcmp (fname, "-")? es_stdin : es_fopen (fname, "r");
3972 err = gpg_error_from_syserror ();
3973 log_error ("can't open '%s': %s\n", fname, gpg_strerror (err));
3978 log_info ("applying profile '%s'\n", fname);
3981 while ((length = es_read_line (fp, &line, &line_len, NULL)) > 0)
3983 char *name, *flags, *value;
3984 gc_option_t *option_info = NULL;
3989 while (*name == ' ' || *name == '\t')
3991 if (!*name || *name == '#' || *name == '\r' || *name == '\n')
3993 trim_trailing_spaces (name);
3995 /* Check whether this is a new section. */
4000 /* New section: Get the name of the component. */
4001 p = strchr (name, ']');
4005 log_info ("%s:%d:%d: error: syntax error in section tag\n",
4006 fname, lineno, (int)(name - line));
4012 log_info ("%s:%d:%d: warning: garbage after section tag\n",
4013 fname, lineno, (int)(p - line));
4016 component_id = gc_component_find (name);
4017 if (component_id < 0)
4019 log_info ("%s:%d:%d: warning: skipping unknown section '%s'\n",
4020 fname, lineno, (int)(name - line), name );
4028 if (component_id < 0)
4031 log_info ("%s:%d:%d: error: not in a valid section\n",
4032 fname, lineno, (int)(name - line));
4037 /* Parse the option name. */
4038 for (p = name; *p && !spacep (p); p++)
4043 option_info = find_option (component_id, name, GC_BACKEND_ANY);
4047 log_info ("%s:%d:%d: error: unknown option '%s' in section '%s'\n",
4048 fname, lineno, (int)(name - line),
4049 name, gc_component[component_id].name);
4053 /* Parse the optional flags. */
4054 trim_spaces (value);
4059 p = strchr (flags, ']');
4062 log_info ("%s:%d:%d: warning: invalid flag specification\n",
4063 fname, lineno, (int)(p - line));
4068 trim_spaces (value);
4070 else /* No flags given. */
4073 /* Set required defaults. */
4074 if (gc_arg_type[option_info->arg_type].fallback == GC_ARG_TYPE_NONE
4078 /* Check and save this option. */
4080 if (flags && !strcmp (flags, "default"))
4081 newflags |= GC_OPT_FLAG_DEFAULT;
4084 option_info->new_flags = 0;
4087 xfree (option_info->new_value);
4088 option_info->new_value = NULL;
4090 change_one_value (option_info, runtime, newflags, value, 1);
4093 if (length < 0 || es_ferror (fp))
4095 err = gpg_error_from_syserror ();
4097 log_error (_("%s:%u: read error: %s\n"),
4098 fname, lineno, gpg_strerror (err));
4101 log_error (_("error closing '%s'\n"), fname);
4103 log_error (_("error parsing '%s'\n"), fname);
4107 /* If it all worked, process the options. */
4110 /* We need to switch off the runtime update, so that we can do
4111 it later all at once. */
4112 int save_opt_runtime = opt.runtime;
4115 for (component_id = 0; component_id < GC_COMPONENT_NR; component_id++)
4117 gc_component_change_options (component_id, NULL, NULL, 1);
4119 opt.runtime = save_opt_runtime;
4123 for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
4124 if (runtime[backend_id] && gc_backend[backend_id].runtime_change)
4125 (*gc_backend[backend_id].runtime_change) (0);
4129 xfree (fname_buffer);