1 /* gpgconf-comp.c - Configuration utility for GnuPG.
2 * Copyright (C) 2004, 2007, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GnuPG; if not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
35 #ifdef HAVE_W32_SYSTEM
36 # define WIN32_LEAN_AND_MEAN 1
43 /* For log_logv(), asctimestamp(), gnupg_get_time (). */
44 #define JNLIB_NEED_LOG_LOGV
49 #include "gc-opt-flags.h"
53 /* There is a problem with gpg 1.4 under Windows: --gpgconf-list
54 returns a plain filename without escaping. As long as we have not
55 fixed that we need to use gpg2 - it might actually be better to use
57 #ifdef HAVE_W32_SYSTEM
58 #define GPGNAME "gpg2"
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 (JNLIB_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 void gpg_agent_runtime_change (void);
109 /* Backend configuration. Backends are used to decide how the default
110 and current value of an option can be determined, and how the
111 option can be changed. To every option in every component belongs
112 exactly one backend that controls and determines the option. Some
113 backends are programs from the GPG system. Others might be
114 implemented by GPGConf itself. If you change this enum, don't
115 forget to update GC_BACKEND below. */
118 /* Any backend, used for find_option (). */
121 /* The Gnu Privacy Guard. */
124 /* The Gnu Privacy Guard for S/MIME. */
128 GC_BACKEND_GPG_AGENT
,
130 /* The GnuPG SCDaemon. */
133 /* The Aegypten directory manager. */
136 /* The LDAP server list file for the Aegypten director manager. */
137 GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST
,
139 /* The number of the above entries. */
144 /* To be able to implement generic algorithms for the various
145 backends, we collect all information about them in this struct. */
148 /* The name of the backend. */
151 /* The name of the program that acts as the backend. Some backends
152 don't have an associated program, but are implemented directly by
153 GPGConf. In this case, PROGRAM is NULL. */
156 /* The module name (GNUPG_MODULE_NAME_foo) as defined by
157 ../common/util.h. This value is used to get the actual installed
158 path of the program. 0 is used if no backedn program is
162 /* The runtime change callback. */
163 void (*runtime_change
) (void);
165 /* The option name for the configuration filename of this backend.
166 This must be an absolute filename. It can be an option from a
167 different backend (but then ordering of the options might
168 matter). Note: This must be unique among all components. */
169 const char *option_config_filename
;
171 /* If this is a file backend rather than a program backend, then
172 this is the name of the option associated with the file. */
173 const char *option_name
;
174 } gc_backend
[GC_BACKEND_NR
] =
176 { NULL
}, /* GC_BACKEND_ANY dummy entry. */
177 { "GnuPG", GPGNAME
, GNUPG_MODULE_NAME_GPG
,
178 NULL
, "gpgconf-gpg.conf" },
179 { "GPGSM", "gpgsm", GNUPG_MODULE_NAME_GPGSM
,
180 NULL
, "gpgconf-gpgsm.conf" },
181 { "GPG Agent", "gpg-agent", GNUPG_MODULE_NAME_AGENT
,
182 gpg_agent_runtime_change
, "gpgconf-gpg-agent.conf" },
183 { "SCDaemon", "scdaemon", GNUPG_MODULE_NAME_SCDAEMON
,
184 NULL
, "gpgconf-scdaemon.conf" },
185 { "DirMngr", "dirmngr", GNUPG_MODULE_NAME_DIRMNGR
,
186 NULL
, "gpgconf-dirmngr.conf" },
187 { "DirMngr LDAP Server List", NULL
, 0,
188 NULL
, "ldapserverlist-file", "LDAP Server" },
192 /* Option configuration. */
194 /* An option might take an argument, or not. Argument types can be
195 basic or complex. Basic types are generic and easy to validate.
196 Complex types provide more specific information about the intended
197 use, but can be difficult to validate. If you add to this enum,
198 don't forget to update GC_ARG_TYPE below. YOU MUST NOT CHANGE THE
199 NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE EXTERNAL
203 /* Basic argument types. */
206 GC_ARG_TYPE_NONE
= 0,
208 /* A String argument. */
209 GC_ARG_TYPE_STRING
= 1,
211 /* A signed integer argument. */
212 GC_ARG_TYPE_INT32
= 2,
214 /* An unsigned integer argument. */
215 GC_ARG_TYPE_UINT32
= 3,
217 /* ADD NEW BASIC TYPE ENTRIES HERE. */
219 /* Complex argument types. */
221 /* A complete filename. */
222 GC_ARG_TYPE_FILENAME
= 32,
224 /* An LDAP server in the format
225 HOSTNAME:PORT:USERNAME:PASSWORD:BASE_DN. */
226 GC_ARG_TYPE_LDAP_SERVER
= 33,
228 /* A 40 character fingerprint. */
229 GC_ARG_TYPE_KEY_FPR
= 34,
231 /* A user ID or key ID or fingerprint for a certificate. */
232 GC_ARG_TYPE_PUB_KEY
= 35,
234 /* A user ID or key ID or fingerprint for a certificate with a key. */
235 GC_ARG_TYPE_SEC_KEY
= 36,
237 /* A alias list made up of a key, an equal sign and a space
238 separated list of values. */
239 GC_ARG_TYPE_ALIAS_LIST
= 37,
241 /* ADD NEW COMPLEX TYPE ENTRIES HERE. */
243 /* The number of the above entries. */
248 /* For every argument, we record some information about it in the
252 /* For every argument type exists a basic argument type that can be
253 used as a fallback for input and validation purposes. */
254 gc_arg_type_t fallback
;
256 /* Human-readable name of the type. */
258 } gc_arg_type
[GC_ARG_TYPE_NR
] =
260 /* The basic argument types have their own types as fallback. */
261 { GC_ARG_TYPE_NONE
, "none" },
262 { GC_ARG_TYPE_STRING
, "string" },
263 { GC_ARG_TYPE_INT32
, "int32" },
264 { GC_ARG_TYPE_UINT32
, "uint32" },
266 /* Reserved basic type entries for future extension. */
267 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
268 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
269 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
270 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
271 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
272 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
273 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
274 { GC_ARG_TYPE_NR
, NULL
}, { GC_ARG_TYPE_NR
, NULL
},
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
},
282 /* The complex argument types have a basic type as fallback. */
283 { GC_ARG_TYPE_STRING
, "filename" },
284 { GC_ARG_TYPE_STRING
, "ldap server" },
285 { GC_ARG_TYPE_STRING
, "key fpr" },
286 { GC_ARG_TYPE_STRING
, "pub key" },
287 { GC_ARG_TYPE_STRING
, "sec key" },
288 { GC_ARG_TYPE_STRING
, "alias list" },
292 /* Every option has an associated expert level, than can be used to
293 hide advanced and expert options from beginners. If you add to
294 this list, don't forget to update GC_LEVEL below. YOU MUST NOT
295 CHANGE THE NUMBERS OF THE EXISTING ENTRIES, AS THEY ARE PART OF THE
296 EXTERNAL INTERFACE. */
299 /* The basic options should always be displayed. */
302 /* The advanced options may be hidden from beginners. */
305 /* The expert options should only be displayed to experts. */
308 /* The invisible options should normally never be displayed. */
311 /* The internal options are never exported, they mark options that
312 are recorded for internal use only. */
315 /* ADD NEW ENTRIES HERE. */
317 /* The number of the above entries. */
321 /* A description for each expert level. */
335 /* Option flags. The flags which are used by the backends are defined
336 by gc-opt-flags.h, included above.
338 YOU MUST NOT CHANGE THE NUMBERS OF THE EXISTING FLAGS, AS THEY ARE
339 PART OF THE EXTERNAL INTERFACE. */
341 /* Some entries in the option list are not options, but mark the
342 beginning of a new group of options. These entries have the GROUP
344 #define GC_OPT_FLAG_GROUP (1UL << 0)
345 /* The ARG_OPT flag for an option indicates that the argument is
346 optional. This is never set for GC_ARG_TYPE_NONE options. */
347 #define GC_OPT_FLAG_ARG_OPT (1UL << 1)
348 /* The LIST flag for an option indicates that the option can occur
349 several times. A comma separated list of arguments is used as the
351 #define GC_OPT_FLAG_LIST (1UL << 2)
352 /* The NO_CHANGE flag for an option indicates that the user should not
353 be allowed to change this option using the standard gpgconf method.
354 Frontends using gpgconf should grey out such options, so that only
355 the current value is displayed. */
356 #define GC_OPT_FLAG_NO_CHANGE (1UL <<7)
359 /* A human-readable description for each flag. */
376 /* To each option, or group marker, the information in the GC_OPTION
377 struct is provided. If you change this, don't forget to update the
378 option list of each component. */
381 /* If this is NULL, then this is a terminator in an array of unknown
382 length. Otherwise, if this entry is a group marker (see FLAGS),
383 then this is the name of the group described by this entry.
384 Otherwise it is the name of the option described by this
385 entry. The name must not contain a colon. */
388 /* The option flags. If the GROUP flag is set, then this entry is a
389 group marker, not an option, and only the fields LEVEL,
390 DESC_DOMAIN and DESC are valid. In all other cases, this entry
391 describes a new option and all fields are valid. */
394 /* The expert level. This field is valid for options and groups. A
395 group has the expert level of the lowest-level option in the
397 gc_expert_level_t level
;
399 /* A gettext domain in which the following description can be found.
400 If this is NULL, then DESC is not translated. Valid for groups
403 Note that we try to keep the description of groups within the
406 IMPORTANT: If you add a new domain please make sure to add a code
407 set switching call to the function my_dgettext further below. */
408 const char *desc_domain
;
410 /* A gettext description for this group or option. If it starts
411 with a '|', then the string up to the next '|' describes the
412 argument, and the description follows the second '|'.
414 In general enclosing these description in N_() is not required
415 because the description should be identical to the one in the
416 help menu of the respective program. */
419 /* The following fields are only valid for options. */
421 /* The type of the option argument. */
422 gc_arg_type_t arg_type
;
424 /* The backend that implements this option. */
425 gc_backend_t backend
;
427 /* The following fields are set to NULL at startup (because all
428 option's are declared as static variables). They are at the end
429 of the list so that they can be omitted from the option
432 /* This is true if the option is supported by this version of the
436 /* The default value for this option. This is NULL if the option is
437 not present in the backend, the empty string if no default is
438 available, and otherwise a quoted string. */
441 /* The default argument is only valid if the "optional arg" flag is
442 set, and specifies the default argument (value) that is used if
443 the argument is omitted. */
446 /* The current value of this option. */
449 /* The new flags for this option. The only defined flag is actually
450 GC_OPT_FLAG_DEFAULT, and it means that the option should be
451 deleted. In this case, NEW_VALUE is NULL. */
452 unsigned long new_flags
;
454 /* The new value of this option. */
457 typedef struct gc_option gc_option_t
;
459 /* Use this macro to terminate an option list. */
460 #define GC_OPTION_NULL { NULL }
463 /* The options of the GC_COMPONENT_GPG_AGENT component. */
464 static gc_option_t gc_options_gpg_agent
[] =
466 /* The configuration file to which we write the changes. */
467 { "gpgconf-gpg-agent.conf", GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
468 NULL
, NULL
, GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG_AGENT
},
471 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
472 "gnupg", N_("Options controlling the diagnostic output") },
473 { "verbose", GC_OPT_FLAG_LIST
|GC_OPT_FLAG_RUNTIME
, GC_LEVEL_BASIC
,
475 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
476 { "quiet", GC_OPT_FLAG_NONE
|GC_OPT_FLAG_RUNTIME
, GC_LEVEL_BASIC
,
477 "gnupg", "be somewhat more quiet",
478 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
479 { "no-greeting", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
481 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
484 GC_OPT_FLAG_GROUP
, GC_LEVEL_EXPERT
,
485 "gnupg", N_("Options controlling the configuration") },
486 { "options", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
487 "gnupg", "|FILE|read options from FILE",
488 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG_AGENT
},
489 { "disable-scdaemon", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
490 "gnupg", "do not use the SCdaemon",
491 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
494 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
495 "gnupg", N_("Options useful for debugging") },
496 { "debug-level", GC_OPT_FLAG_ARG_OPT
|GC_OPT_FLAG_RUNTIME
, GC_LEVEL_ADVANCED
,
497 "gnupg", "|LEVEL|set the debugging level to LEVEL",
498 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG_AGENT
},
499 { "log-file", GC_OPT_FLAG_RUNTIME
, GC_LEVEL_ADVANCED
,
500 "gnupg", N_("|FILE|write server mode logs to FILE"),
501 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG_AGENT
},
502 { "faked-system-time", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
504 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
507 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
508 "gnupg", N_("Options controlling the security") },
509 { "default-cache-ttl", GC_OPT_FLAG_RUNTIME
,
510 GC_LEVEL_BASIC
, "gnupg",
511 "|N|expire cached PINs after N seconds",
512 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
513 { "default-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME
,
514 GC_LEVEL_ADVANCED
, "gnupg",
515 N_("|N|expire SSH keys after N seconds"),
516 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
517 { "max-cache-ttl", GC_OPT_FLAG_RUNTIME
,
518 GC_LEVEL_EXPERT
, "gnupg",
519 N_("|N|set maximum PIN cache lifetime to N seconds"),
520 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
521 { "max-cache-ttl-ssh", GC_OPT_FLAG_RUNTIME
,
522 GC_LEVEL_EXPERT
, "gnupg",
523 N_("|N|set maximum SSH key lifetime to N seconds"),
524 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
525 { "ignore-cache-for-signing", GC_OPT_FLAG_RUNTIME
,
526 GC_LEVEL_BASIC
, "gnupg", "do not use the PIN cache when signing",
527 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
528 { "allow-mark-trusted", GC_OPT_FLAG_RUNTIME
,
529 GC_LEVEL_ADVANCED
, "gnupg", "allow clients to mark keys as \"trusted\"",
530 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
531 { "no-grab", GC_OPT_FLAG_RUNTIME
, GC_LEVEL_EXPERT
,
532 "gnupg", "do not grab keyboard and mouse",
533 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
535 { "Passphrase policy",
536 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
537 "gnupg", N_("Options enforcing a passphrase policy") },
538 { "enforce-passphrase-constraints", GC_OPT_FLAG_RUNTIME
,
539 GC_LEVEL_EXPERT
, "gnupg",
540 N_("do not allow to bypass the passphrase policy"),
541 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
542 { "min-passphrase-len", GC_OPT_FLAG_RUNTIME
,
543 GC_LEVEL_ADVANCED
, "gnupg",
544 N_("|N|set minimal required length for new passphrases to N"),
545 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
546 { "min-passphrase-nonalpha", GC_OPT_FLAG_RUNTIME
,
547 GC_LEVEL_EXPERT
, "gnupg",
548 N_("|N|require at least N non-alpha characters for a new passphrase"),
549 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
550 { "check-passphrase-pattern", GC_OPT_FLAG_RUNTIME
,
552 "gnupg", N_("|FILE|check new passphrases against pattern in FILE"),
553 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG_AGENT
},
554 { "max-passphrase-days", GC_OPT_FLAG_RUNTIME
,
555 GC_LEVEL_EXPERT
, "gnupg",
556 N_("|N|expire the passphrase after N days"),
557 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPG_AGENT
},
558 { "enable-passphrase-history", GC_OPT_FLAG_RUNTIME
,
559 GC_LEVEL_EXPERT
, "gnupg",
560 N_("do not allow the reuse of old passphrases"),
561 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG_AGENT
},
567 /* The options of the GC_COMPONENT_SCDAEMON component. */
568 static gc_option_t gc_options_scdaemon
[] =
570 /* The configuration file to which we write the changes. */
571 { "gpgconf-scdaemon.conf", GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
572 NULL
, NULL
, GC_ARG_TYPE_FILENAME
, GC_BACKEND_SCDAEMON
},
575 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
576 "gnupg", N_("Options controlling the diagnostic output") },
577 { "verbose", GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
579 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
580 { "quiet", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
581 "gnupg", "be somewhat more quiet",
582 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
583 { "no-greeting", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
585 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
588 GC_OPT_FLAG_GROUP
, GC_LEVEL_EXPERT
,
589 "gnupg", N_("Options controlling the configuration") },
590 { "options", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
591 "gnupg", "|FILE|read options from FILE",
592 GC_ARG_TYPE_FILENAME
, GC_BACKEND_SCDAEMON
},
593 { "reader-port", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
594 "gnupg", "|N|connect to reader at port N",
595 GC_ARG_TYPE_STRING
, GC_BACKEND_SCDAEMON
},
596 { "ctapi-driver", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
597 "gnupg", "|NAME|use NAME as ct-API driver",
598 GC_ARG_TYPE_STRING
, GC_BACKEND_SCDAEMON
},
599 { "pcsc-driver", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
600 "gnupg", "|NAME|use NAME as PC/SC driver",
601 GC_ARG_TYPE_STRING
, GC_BACKEND_SCDAEMON
},
602 { "disable-opensc", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
603 "gnupg", "do not use the OpenSC layer",
604 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
605 { "disable-ccid", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
606 "gnupg", "do not use the internal CCID driver",
607 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
608 { "disable-keypad", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
609 "gnupg", "do not use a reader's keypad",
610 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
613 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
614 "gnupg", N_("Options useful for debugging") },
615 { "debug-level", GC_OPT_FLAG_ARG_OPT
, GC_LEVEL_ADVANCED
,
616 "gnupg", "|LEVEL|set the debugging level to LEVEL",
617 GC_ARG_TYPE_STRING
, GC_BACKEND_SCDAEMON
},
618 { "log-file", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
619 "gnupg", N_("|FILE|write server mode logs to FILE"),
620 GC_ARG_TYPE_FILENAME
, GC_BACKEND_SCDAEMON
},
623 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
624 "gnupg", N_("Options controlling the security") },
625 { "allow-admin", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
626 "gnupg", "allow the use of admin card commands",
627 GC_ARG_TYPE_NONE
, GC_BACKEND_SCDAEMON
},
634 /* The options of the GC_COMPONENT_GPG component. */
635 static gc_option_t gc_options_gpg
[] =
637 /* The configuration file to which we write the changes. */
638 { "gpgconf-gpg.conf", GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
639 NULL
, NULL
, GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG
},
642 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
643 "gnupg", N_("Options controlling the diagnostic output") },
644 { "verbose", GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
646 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG
},
647 { "quiet", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
648 "gnupg", "be somewhat more quiet",
649 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG
},
650 { "no-greeting", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
652 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG
},
655 GC_OPT_FLAG_GROUP
, GC_LEVEL_EXPERT
,
656 "gnupg", N_("Options controlling the configuration") },
657 { "default-key", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
658 "gnupg", N_("|NAME|use NAME as default secret key"),
659 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG
},
660 { "encrypt-to", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
661 "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
662 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG
},
663 { "group", GC_OPT_FLAG_LIST
, GC_LEVEL_ADVANCED
,
664 "gnupg", N_("|SPEC|set up email aliases"),
665 GC_ARG_TYPE_ALIAS_LIST
, GC_BACKEND_GPG
},
666 { "options", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
667 "gnupg", "|FILE|read options from FILE",
668 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG
},
671 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
672 "gnupg", N_("Options useful for debugging") },
673 { "debug-level", GC_OPT_FLAG_ARG_OPT
, GC_LEVEL_ADVANCED
,
674 "gnupg", "|LEVEL|set the debugging level to LEVEL",
675 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG
},
676 { "log-file", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
677 "gnupg", N_("|FILE|write server mode logs to FILE"),
678 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPG
},
679 /* { "faked-system-time", GC_OPT_FLAG_NONE, GC_LEVEL_INVISIBLE, */
681 /* GC_ARG_TYPE_UINT32, GC_BACKEND_GPG }, */
684 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
685 "gnupg", N_("Configuration for Keyservers") },
686 { "keyserver", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
687 "gnupg", N_("|URL|use keyserver at URL"),
688 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG
},
689 { "allow-pka-lookup", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
690 "gnupg", N_("allow PKA lookups (DNS requests)"),
691 GC_ARG_TYPE_NONE
, GC_BACKEND_GPG
},
692 { "auto-key-locate", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
693 "gnupg", N_("|MECHANISMS|use MECHANISMS to locate keys by mail address"),
694 GC_ARG_TYPE_STRING
, GC_BACKEND_GPG
},
702 /* The options of the GC_COMPONENT_GPGSM component. */
703 static gc_option_t gc_options_gpgsm
[] =
705 /* The configuration file to which we write the changes. */
706 { "gpgconf-gpgsm.conf", GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
707 NULL
, NULL
, GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPGSM
},
710 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
711 "gnupg", N_("Options controlling the diagnostic output") },
712 { "verbose", GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
714 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
715 { "quiet", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
716 "gnupg", "be somewhat more quiet",
717 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
718 { "no-greeting", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
720 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
723 GC_OPT_FLAG_GROUP
, GC_LEVEL_EXPERT
,
724 "gnupg", N_("Options controlling the configuration") },
725 { "default-key", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
726 "gnupg", N_("|NAME|use NAME as default secret key"),
727 GC_ARG_TYPE_STRING
, GC_BACKEND_GPGSM
},
728 { "encrypt-to", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
729 "gnupg", N_("|NAME|encrypt to user ID NAME as well"),
730 GC_ARG_TYPE_STRING
, GC_BACKEND_GPGSM
},
731 { "options", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
732 "gnupg", "|FILE|read options from FILE",
733 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPGSM
},
734 { "prefer-system-dirmngr", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
735 "gnupg", "use system's dirmngr if available",
736 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
737 { "disable-dirmngr", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
738 "gnupg", N_("disable all access to the dirmngr"),
739 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
740 { "p12-charset", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
741 "gnupg", N_("|NAME|use encoding NAME for PKCS#12 passphrases"),
742 GC_ARG_TYPE_STRING
, GC_BACKEND_GPGSM
},
743 { "keyserver", GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
744 "gnupg", N_("|SPEC|use this keyserver to lookup keys"),
745 GC_ARG_TYPE_LDAP_SERVER
, GC_BACKEND_GPGSM
},
748 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
749 "gnupg", N_("Options useful for debugging") },
750 { "debug-level", GC_OPT_FLAG_ARG_OPT
, GC_LEVEL_ADVANCED
,
751 "gnupg", "|LEVEL|set the debugging level to LEVEL",
752 GC_ARG_TYPE_STRING
, GC_BACKEND_GPGSM
},
753 { "log-file", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
754 "gnupg", N_("|FILE|write server mode logs to FILE"),
755 GC_ARG_TYPE_FILENAME
, GC_BACKEND_GPGSM
},
756 { "faked-system-time", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
758 GC_ARG_TYPE_UINT32
, GC_BACKEND_GPGSM
},
761 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
762 "gnupg", N_("Options controlling the security") },
763 { "disable-crl-checks", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
764 "gnupg", "never consult a CRL",
765 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
766 { "disable-trusted-cert-crl-check", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
767 "gnupg", N_("do not check CRLs for root certificates"),
768 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
769 { "enable-ocsp", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
770 "gnupg", "check validity using OCSP",
771 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
772 { "include-certs", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
773 "gnupg", "|N|number of certificates to include",
774 GC_ARG_TYPE_INT32
, GC_BACKEND_GPGSM
},
775 { "disable-policy-checks", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
776 "gnupg", "do not check certificate policies",
777 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
778 { "auto-issuer-key-retrieve", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
779 "gnupg", "fetch missing issuer certificates",
780 GC_ARG_TYPE_NONE
, GC_BACKEND_GPGSM
},
781 { "cipher-algo", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
782 "gnupg", "|NAME|use cipher algorithm NAME",
783 GC_ARG_TYPE_STRING
, GC_BACKEND_GPGSM
},
789 /* The options of the GC_COMPONENT_DIRMNGR component. */
790 static gc_option_t gc_options_dirmngr
[] =
792 /* The configuration file to which we write the changes. */
793 { "gpgconf-dirmngr.conf", GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
794 NULL
, NULL
, GC_ARG_TYPE_FILENAME
, GC_BACKEND_DIRMNGR
},
797 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
798 "gnupg", N_("Options controlling the diagnostic output") },
799 { "verbose", GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
800 "dirmngr", "verbose",
801 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
802 { "quiet", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
803 "dirmngr", "be somewhat more quiet",
804 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
805 { "no-greeting", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
807 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
810 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
811 "gnupg", N_("Options controlling the format of the output") },
812 { "sh", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
813 "dirmngr", "sh-style command output",
814 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
815 { "csh", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
816 "dirmngr", "csh-style command output",
817 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
820 GC_OPT_FLAG_GROUP
, GC_LEVEL_EXPERT
,
821 "gnupg", N_("Options controlling the configuration") },
822 { "options", GC_OPT_FLAG_NONE
, GC_LEVEL_EXPERT
,
823 "dirmngr", "|FILE|read options from FILE",
824 GC_ARG_TYPE_FILENAME
, GC_BACKEND_DIRMNGR
},
827 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
828 "gnupg", N_("Options useful for debugging") },
829 { "debug-level", GC_OPT_FLAG_ARG_OPT
, GC_LEVEL_ADVANCED
,
830 "dirmngr", "|LEVEL|set the debugging level to LEVEL",
831 GC_ARG_TYPE_STRING
, GC_BACKEND_DIRMNGR
},
832 { "no-detach", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
833 "dirmngr", "do not detach from the console",
834 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
835 { "log-file", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
836 "dirmngr", N_("|FILE|write server mode logs to FILE"),
837 GC_ARG_TYPE_FILENAME
, GC_BACKEND_DIRMNGR
},
838 { "debug-wait", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
840 GC_ARG_TYPE_UINT32
, GC_BACKEND_DIRMNGR
},
841 { "faked-system-time", GC_OPT_FLAG_NONE
, GC_LEVEL_INVISIBLE
,
843 GC_ARG_TYPE_UINT32
, GC_BACKEND_DIRMNGR
},
846 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
847 "gnupg", N_("Options controlling the interactivity and enforcement") },
848 { "batch", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
849 "dirmngr", "run without asking a user",
850 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
851 { "force", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
852 "dirmngr", "force loading of outdated CRLs",
853 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
856 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
857 "gnupg", N_("Configuration for HTTP servers") },
858 { "disable-http", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
859 "dirmngr", "inhibit the use of HTTP",
860 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
861 { "ignore-http-dp", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
862 "dirmngr", "ignore HTTP CRL distribution points",
863 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
864 { "http-proxy", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
865 "dirmngr", "|URL|redirect all HTTP requests to URL",
866 GC_ARG_TYPE_STRING
, GC_BACKEND_DIRMNGR
},
867 { "honor-http-proxy", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
868 "gnupg", N_("use system's HTTP proxy setting"),
869 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
872 GC_OPT_FLAG_GROUP
, GC_LEVEL_BASIC
,
873 "gnupg", N_("Configuration of LDAP servers to use") },
874 { "disable-ldap", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
875 "dirmngr", "inhibit the use of LDAP",
876 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
877 { "ignore-ldap-dp", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
878 "dirmngr", "ignore LDAP CRL distribution points",
879 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
880 { "ldap-proxy", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
881 "dirmngr", "|HOST|use HOST for LDAP queries",
882 GC_ARG_TYPE_STRING
, GC_BACKEND_DIRMNGR
},
883 { "only-ldap-proxy", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
884 "dirmngr", "do not use fallback hosts with --ldap-proxy",
885 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
886 { "add-servers", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
887 "dirmngr", "add new servers discovered in CRL distribution points"
888 " to serverlist", GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
889 { "ldaptimeout", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
890 "dirmngr", "|N|set LDAP timeout to N seconds",
891 GC_ARG_TYPE_UINT32
, GC_BACKEND_DIRMNGR
},
892 /* The following entry must not be removed, as it is required for
893 the GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST. */
894 { "ldapserverlist-file",
895 GC_OPT_FLAG_NONE
, GC_LEVEL_INTERNAL
,
896 "dirmngr", "|FILE|read LDAP server list from FILE",
897 GC_ARG_TYPE_FILENAME
, GC_BACKEND_DIRMNGR
},
898 /* This entry must come after at least one entry for
899 GC_BACKEND_DIRMNGR in this component, so that the entry for
900 "ldapserverlist-file will be initialized before this one. */
901 { "LDAP Server", GC_OPT_FLAG_ARG_OPT
|GC_OPT_FLAG_LIST
, GC_LEVEL_BASIC
,
902 "gnupg", N_("LDAP server list"),
903 GC_ARG_TYPE_LDAP_SERVER
, GC_BACKEND_DIRMNGR_LDAP_SERVER_LIST
},
904 { "max-replies", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
905 "dirmngr", "|N|do not return more than N items in one query",
906 GC_ARG_TYPE_UINT32
, GC_BACKEND_DIRMNGR
},
909 GC_OPT_FLAG_GROUP
, GC_LEVEL_ADVANCED
,
910 "gnupg", N_("Configuration for OCSP") },
911 { "allow-ocsp", GC_OPT_FLAG_NONE
, GC_LEVEL_BASIC
,
912 "dirmngr", "allow sending OCSP requests",
913 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
914 { "ignore-ocsp-service-url", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
915 "dirmngr", "ignore certificate contained OCSP service URLs",
916 GC_ARG_TYPE_NONE
, GC_BACKEND_DIRMNGR
},
917 { "ocsp-responder", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
918 "dirmngr", "|URL|use OCSP responder at URL",
919 GC_ARG_TYPE_STRING
, GC_BACKEND_DIRMNGR
},
920 { "ocsp-signer", GC_OPT_FLAG_NONE
, GC_LEVEL_ADVANCED
,
921 "dirmngr", "|FPR|OCSP response signed by FPR",
922 GC_ARG_TYPE_STRING
, GC_BACKEND_DIRMNGR
},
929 /* Component system. Each component is a set of options that can be
930 configured at the same time. If you change this, don't forget to
931 update GC_COMPONENT below. */
934 /* The classic GPG for OpenPGP. */
938 GC_COMPONENT_GPG_AGENT
,
940 /* The Smardcard Daemon. */
941 GC_COMPONENT_SCDAEMON
,
943 /* GPG for S/MIME. */
946 /* The LDAP Directory Manager for CRLs. */
947 GC_COMPONENT_DIRMNGR
,
949 /* The number of components. */
954 /* The information associated with each component. */
957 /* The name of this component. Must not contain a colon (':')
961 /* The gettext domain for the description DESC. If this is NULL,
962 then the description is not translated. */
963 const char *desc_domain
;
965 /* The description for this domain. */
968 /* The list of options for this component, terminated by
970 gc_option_t
*options
;
973 { "gpg", NULL
, "GPG for OpenPGP", gc_options_gpg
},
974 { "gpg-agent", NULL
, "GPG Agent", gc_options_gpg_agent
},
975 { "scdaemon", NULL
, "Smartcard Daemon", gc_options_scdaemon
},
976 { "gpgsm", NULL
, "GPG for S/MIME", gc_options_gpgsm
},
977 { "dirmngr", NULL
, "Directory Manager", gc_options_dirmngr
}
982 /* Structure used to collect error output of the backend programs. */
984 typedef struct error_line_s
*error_line_t
;
987 error_line_t next
; /* Link to next item. */
988 const char *fname
; /* Name of the config file (points into BUFFER). */
989 unsigned int lineno
; /* Line number of the config file. */
990 const char *errtext
; /* Text of the error message (points into BUFFER). */
991 char buffer
[1]; /* Helper buffer. */
996 /* Engine specific support. */
998 gpg_agent_runtime_change (void)
1000 #ifndef HAVE_W32_SYSTEM
1001 char *agent
= getenv ("GPG_AGENT_INFO");
1003 unsigned long pid_long
;
1010 pid_str
= strchr (agent
, ':');
1016 pid_long
= strtoul (pid_str
, &tail
, 0);
1017 if (errno
|| (*tail
!= ':' && *tail
!= '\0'))
1020 pid
= (pid_t
) pid_long
;
1022 /* Check for overflow. */
1023 if (pid_long
!= (unsigned long) pid
)
1026 /* Ignore any errors here. */
1030 const char *pgmname
;
1031 const char *argv
[2];
1034 pgmname
= gnupg_module_name (GNUPG_MODULE_NAME_CONNECT_AGENT
);
1035 argv
[0] = "reloadagent";
1038 err
= gnupg_spawn_process_fd (pgmname
, argv
, -1, -1, -1, &pid
);
1040 err
= gnupg_wait_process (pgmname
, pid
, NULL
);
1042 gc_error (0, 0, "error running `%s%s': %s",
1043 pgmname
, " reloadagent", gpg_strerror (err
));
1044 #endif /*!HAVE_W32_SYSTEM*/
1049 /* More or less Robust version of dgettext. It has the side effect of
1050 switching the codeset to utf-8 because this is what we want to
1051 output. In theory it is posible to keep the orginal code set and
1052 switch back for regular disgnostic output (redefine "_(" for that)
1053 but given the natur of this tool, being something invoked from
1054 other pograms, it does not make much sense. */
1056 my_dgettext (const char *domain
, const char *msgid
)
1058 #ifdef USE_SIMPLE_GETTEXT
1061 static int switched_codeset
;
1064 if (!switched_codeset
)
1066 switched_codeset
= 1;
1067 gettext_select_utf8 (1);
1070 if (!strcmp (domain
, "gnupg"))
1071 domain
= PACKAGE_GT
;
1073 /* FIXME: we have no dgettext, thus we can't switch. */
1075 text
= gettext (msgid
);
1076 return text
? text
: msgid
;
1078 #elif defined(ENABLE_NLS)
1081 static int switched_codeset
;
1084 if (!switched_codeset
)
1086 switched_codeset
= 1;
1087 bind_textdomain_codeset (PACKAGE_GT
, "utf-8");
1089 bindtextdomain ("dirmngr", LOCALEDIR
);
1090 bind_textdomain_codeset ("dirmngr", "utf-8");
1094 /* Note: This is a hack to actually use the gnupg2 domain as
1095 long we are in a transition phase where gnupg 1.x and 1.9 may
1097 if (!strcmp (domain
, "gnupg"))
1098 domain
= PACKAGE_GT
;
1100 text
= dgettext (domain
, msgid
);
1101 return text
? text
: msgid
;
1109 /* Percent-Escape special characters. The string is valid until the
1110 next invocation of the function. */
1112 gc_percent_escape (const char *src
)
1114 static char *esc_str
;
1115 static int esc_str_len
;
1116 int new_len
= 3 * strlen (src
) + 1;
1119 if (esc_str_len
< new_len
)
1121 char *new_esc_str
= realloc (esc_str
, new_len
);
1123 gc_error (1, errno
, "can not escape string");
1124 esc_str
= new_esc_str
;
1125 esc_str_len
= new_len
;
1137 else if (*src
== ':')
1139 /* The colon is used as field separator. */
1144 else if (*src
== ',')
1146 /* The comma is used as list separator. */
1161 /* Percent-Deescape special characters. The string is valid until the
1162 next invocation of the function. */
1164 percent_deescape (const char *src
)
1168 int new_len
= 3 * strlen (src
) + 1;
1171 if (str_len
< new_len
)
1173 char *new_str
= realloc (str
, new_len
);
1175 gc_error (1, errno
, "can not deescape string");
1185 int val
= hextobyte (src
+ 1);
1188 gc_error (1, 0, "malformed end of string %s", src
);
1190 *(dst
++) = (char) val
;
1194 *(dst
++) = *(src
++);
1201 /* List all components that are available. */
1203 gc_component_list_components (FILE *out
)
1205 gc_component_t component
;
1206 gc_option_t
*option
;
1207 gc_backend_t backend
;
1208 int backend_seen
[GC_BACKEND_NR
];
1210 const char *pgmname
;
1212 for (component
= 0; component
< GC_COMPONENT_NR
; component
++)
1214 option
= gc_component
[component
].options
;
1217 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
1218 backend_seen
[backend
] = 0;
1221 for (; option
&& option
->name
; option
++)
1223 if ((option
->flags
& GC_OPT_FLAG_GROUP
))
1225 backend
= option
->backend
;
1226 if (backend_seen
[backend
])
1228 backend_seen
[backend
] = 1;
1229 assert (backend
!= GC_BACKEND_ANY
);
1230 if (gc_backend
[backend
].program
1231 && !gc_backend
[backend
].module_name
)
1233 pgmname
= gnupg_module_name (gc_backend
[backend
].module_name
);
1237 desc
= gc_component
[component
].desc
;
1238 desc
= my_dgettext (gc_component
[component
].desc_domain
, desc
);
1239 fprintf (out
, "%s:%s:",
1240 gc_component
[component
].name
, gc_percent_escape (desc
));
1241 fprintf (out
, "%s\n", gc_percent_escape (pgmname
));
1249 all_digits_p (const char *p
, size_t len
)
1253 for (; len
; len
--, p
++)
1254 if (!isascii (*p
) || !isdigit (*p
))
1256 return 1; /* Yes. */
1260 /* Collect all error lines from file descriptor FD. Only lines
1261 prefixed with TAG are considered. Close that file descriptor
1262 then. Returns a list of error line items (which may be empty).
1263 There is no error return. */
1265 collect_error_output (int fd
, const char *tag
)
1272 error_line_t eitem
, errlines
, *errlines_tail
;
1273 size_t taglen
= strlen (tag
);
1275 fp
= fdopen (fd
, "r");
1277 gc_error (1, errno
, "can't fdopen pipe for reading");
1280 errlines_tail
= &errlines
;
1283 while ((c
=getc (fp
)) != EOF
)
1286 if (pos
>= sizeof buffer
- 5 || c
== '\n')
1288 buffer
[pos
- (c
== '\n')] = 0;
1290 ; /*Ignore continuations of previous line. */
1291 else if (!strncmp (buffer
, tag
, taglen
) && buffer
[taglen
] == ':')
1293 /* "gpgsm: foo:4: bla" */
1294 /* Yep, we are interested in this line. */
1295 p
= buffer
+ taglen
+ 1;
1296 while (*p
== ' ' || *p
== '\t')
1299 ; /* Empty lines are ignored. */
1300 else if ( (p2
= strchr (p
, ':')) && (p3
= strchr (p2
+1, ':'))
1301 && all_digits_p (p2
+1, p3
- (p2
+1)))
1303 /* Line in standard compiler format. */
1305 while (*p3
== ' ' || *p3
== '\t')
1307 eitem
= xmalloc (sizeof *eitem
+ strlen (p
));
1309 strcpy (eitem
->buffer
, p
);
1310 eitem
->fname
= eitem
->buffer
;
1311 eitem
->buffer
[p2
-p
] = 0;
1312 eitem
->errtext
= eitem
->buffer
+ (p3
- p
);
1313 /* (we already checked that there are only ascii
1314 digits followed by a colon) */
1316 for (p2
++; isdigit (*p2
); p2
++)
1317 eitem
->lineno
= eitem
->lineno
*10 + (*p2
- '0');
1318 *errlines_tail
= eitem
;
1319 errlines_tail
= &eitem
->next
;
1323 /* Other error output. */
1324 eitem
= xmalloc (sizeof *eitem
+ strlen (p
));
1326 strcpy (eitem
->buffer
, p
);
1327 eitem
->fname
= NULL
;
1328 eitem
->errtext
= eitem
->buffer
;
1330 *errlines_tail
= eitem
;
1331 errlines_tail
= &eitem
->next
;
1335 /* If this was not a complete line mark that we are in a
1337 cont_line
= (c
!= '\n');
1341 /* We ignore error lines not terminated by a LF. */
1348 /* Check the options of a single component. Returns 0 if everything
1351 gc_component_check_options (int component
, FILE *out
, const char *conf_file
)
1354 unsigned int result
;
1355 int backend_seen
[GC_BACKEND_NR
];
1356 gc_backend_t backend
;
1357 gc_option_t
*option
;
1358 const char *pgmname
;
1359 const char *argv
[4];
1364 error_line_t errlines
;
1366 /* We use a temporary file to collect the error output. It would be
1367 better to use a pipe here but as of now we have no suitable
1368 fucntion to create a portable pipe outside of exechelp. Thus it
1369 is easier to use the tempfile approach. */
1371 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
1372 backend_seen
[backend
] = 0;
1374 option
= gc_component
[component
].options
;
1375 for (; option
&& option
->name
; option
++)
1377 if ((option
->flags
& GC_OPT_FLAG_GROUP
))
1379 backend
= option
->backend
;
1380 if (backend_seen
[backend
])
1382 backend_seen
[backend
] = 1;
1383 assert (backend
!= GC_BACKEND_ANY
);
1384 if (!gc_backend
[backend
].program
)
1386 if (!gc_backend
[backend
].module_name
)
1391 if (! option
|| ! option
->name
)
1394 pgmname
= gnupg_module_name (gc_backend
[backend
].module_name
);
1398 argv
[i
++] = "--options";
1399 argv
[i
++] = conf_file
;
1401 argv
[i
++] = "--gpgconf-test";
1404 err
= gnupg_create_inbound_pipe (filedes
);
1406 gc_error (1, 0, _("error creating a pipe: %s\n"),
1407 gpg_strerror (err
));
1411 if (gnupg_spawn_process_fd (pgmname
, argv
, -1, -1, filedes
[1], &pid
))
1415 result
|= 1; /* Program could not be run. */
1420 errlines
= collect_error_output (filedes
[0],
1421 gc_component
[component
].name
);
1422 if (gnupg_wait_process (pgmname
, pid
, &exitcode
))
1425 result
|= 1; /* Program could not be run or it
1426 terminated abnormally. */
1427 result
|= 2; /* Program returned an error. */
1431 /* If the program could not be run, we can't tell whether
1432 the config file is good. */
1439 error_line_t errptr
;
1441 desc
= gc_component
[component
].desc
;
1442 desc
= my_dgettext (gc_component
[component
].desc_domain
, desc
);
1443 fprintf (out
, "%s:%s:",
1444 gc_component
[component
].name
, gc_percent_escape (desc
));
1445 fputs (gc_percent_escape (pgmname
), out
);
1446 fprintf (out
, ":%d:%d:", !(result
& 1), !(result
& 2));
1447 for (errptr
= errlines
; errptr
; errptr
= errptr
->next
)
1449 if (errptr
!= errlines
)
1450 fputs ("\n:::::", out
); /* Continuation line. */
1452 fputs (gc_percent_escape (errptr
->fname
), out
);
1455 fprintf (out
, "%u", errptr
->lineno
);
1457 fputs (gc_percent_escape (errptr
->errtext
), out
);
1465 error_line_t tmp
= errlines
->next
;
1474 /* Check all components that are available. */
1476 gc_check_programs (FILE *out
)
1478 gc_component_t component
;
1480 for (component
= 0; component
< GC_COMPONENT_NR
; component
++)
1481 gc_component_check_options (component
, out
, NULL
);
1486 /* Find the component with the name NAME. Returns -1 if not
1489 gc_component_find (const char *name
)
1493 for (idx
= 0; idx
< GC_COMPONENT_NR
; idx
++)
1495 if (gc_component
[idx
].options
1496 && !strcmp (name
, gc_component
[idx
].name
))
1503 /* List the option OPTION. */
1505 list_one_option (const gc_option_t
*option
, FILE *out
)
1507 const char *desc
= NULL
;
1508 char *arg_name
= NULL
;
1512 desc
= my_dgettext (option
->desc_domain
, option
->desc
);
1516 const char *arg_tail
= strchr (&desc
[1], '|');
1520 int arg_len
= arg_tail
- &desc
[1];
1521 arg_name
= xmalloc (arg_len
+ 1);
1522 memcpy (arg_name
, &desc
[1], arg_len
);
1523 arg_name
[arg_len
] = '\0';
1524 desc
= arg_tail
+ 1;
1530 /* YOU MUST NOT REORDER THE FIELDS IN THIS OUTPUT, AS THEIR ORDER IS
1531 PART OF THE EXTERNAL INTERFACE. YOU MUST NOT REMOVE ANY
1534 /* The name field. */
1535 fprintf (out
, "%s", option
->name
);
1537 /* The flags field. */
1538 fprintf (out
, ":%lu", option
->flags
);
1544 fprintf (out
, "none");
1547 unsigned long flags
= option
->flags
;
1548 unsigned long flag
= 0;
1549 unsigned long first
= 1;
1559 fprintf (out
, "%s", gc_flag
[flag
].name
);
1567 /* The level field. */
1568 fprintf (out
, ":%u", option
->level
);
1570 fprintf (out
, " %s", gc_level
[option
->level
].name
);
1572 /* The description field. */
1573 fprintf (out
, ":%s", desc
? gc_percent_escape (desc
) : "");
1575 /* The type field. */
1576 fprintf (out
, ":%u", option
->arg_type
);
1578 fprintf (out
, " %s", gc_arg_type
[option
->arg_type
].name
);
1580 /* The alternate type field. */
1581 fprintf (out
, ":%u", gc_arg_type
[option
->arg_type
].fallback
);
1583 fprintf (out
, " %s",
1584 gc_arg_type
[gc_arg_type
[option
->arg_type
].fallback
].name
);
1586 /* The argument name field. */
1587 fprintf (out
, ":%s", arg_name
? gc_percent_escape (arg_name
) : "");
1591 /* The default value field. */
1592 fprintf (out
, ":%s", option
->default_value
? option
->default_value
: "");
1594 /* The default argument field. */
1595 fprintf (out
, ":%s", option
->default_arg
? option
->default_arg
: "");
1597 /* The value field. */
1598 if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_NONE
1599 && (option
->flags
& GC_OPT_FLAG_LIST
)
1601 /* The special format "1,1,1,1,...,1" is converted to a number
1603 fprintf (out
, ":%u", (unsigned int)((strlen (option
->value
) + 1) / 2));
1605 fprintf (out
, ":%s", option
->value
? option
->value
: "");
1607 /* ADD NEW FIELDS HERE. */
1613 /* List all options of the component COMPONENT. */
1615 gc_component_list_options (int component
, FILE *out
)
1617 const gc_option_t
*option
= gc_component
[component
].options
;
1619 while (option
&& option
->name
)
1621 /* Do not output unknown or internal options. */
1622 if (!(option
->flags
& GC_OPT_FLAG_GROUP
)
1623 && (!option
->active
|| option
->level
== GC_LEVEL_INTERNAL
))
1629 if (option
->flags
& GC_OPT_FLAG_GROUP
)
1631 const gc_option_t
*group_option
= option
+ 1;
1632 gc_expert_level_t level
= GC_LEVEL_NR
;
1634 /* The manual states that the group level is always the
1635 minimum of the levels of all contained options. Due to
1636 different active options, and because it is hard to
1637 maintain manually, we calculate it here. The value in
1638 the global static table is ignored. */
1640 while (group_option
->name
)
1642 if (group_option
->flags
& GC_OPT_FLAG_GROUP
)
1644 if (group_option
->level
< level
)
1645 level
= group_option
->level
;
1649 /* Check if group is empty. */
1650 if (level
!= GC_LEVEL_NR
)
1652 gc_option_t opt_copy
;
1654 /* Fix up the group level. */
1655 memcpy (&opt_copy
, option
, sizeof (opt_copy
));
1656 opt_copy
.level
= level
;
1657 list_one_option (&opt_copy
, out
);
1661 list_one_option (option
, out
);
1668 /* Find the option NAME in component COMPONENT, for the backend
1669 BACKEND. If BACKEND is GC_BACKEND_ANY, any backend will match. */
1670 static gc_option_t
*
1671 find_option (gc_component_t component
, const char *name
,
1672 gc_backend_t backend
)
1674 gc_option_t
*option
= gc_component
[component
].options
;
1675 while (option
->name
)
1677 if (!(option
->flags
& GC_OPT_FLAG_GROUP
)
1678 && !strcmp (option
->name
, name
)
1679 && (backend
== GC_BACKEND_ANY
|| option
->backend
== backend
))
1683 return option
->name
? option
: NULL
;
1687 /* Determine the configuration filename for the component COMPONENT
1688 and backend BACKEND. */
1690 get_config_filename (gc_component_t component
, gc_backend_t backend
)
1692 char *filename
= NULL
;
1693 gc_option_t
*option
= find_option
1694 (component
, gc_backend
[backend
].option_config_filename
, GC_BACKEND_ANY
);
1696 assert (option
->arg_type
== GC_ARG_TYPE_FILENAME
);
1697 assert (!(option
->flags
& GC_OPT_FLAG_LIST
));
1699 if (!option
->active
|| !option
->default_value
)
1700 gc_error (1, 0, "Option %s, needed by backend %s, was not initialized",
1701 gc_backend
[backend
].option_config_filename
,
1702 gc_backend
[backend
].name
);
1704 if (option
->value
&& *option
->value
)
1705 filename
= percent_deescape (&option
->value
[1]);
1706 else if (option
->default_value
&& *option
->default_value
)
1707 filename
= percent_deescape (&option
->default_value
[1]);
1711 #ifdef HAVE_DOSISH_SYSTEM
1713 && filename
[1] == ':'
1714 && (filename
[2] == '/' || filename
[2] == '\\')))
1716 if (filename
[0] != '/')
1718 gc_error (1, 0, "Option %s, needed by backend %s, is not absolute",
1719 gc_backend
[backend
].option_config_filename
,
1720 gc_backend
[backend
].name
);
1726 /* Retrieve the options for the component COMPONENT from backend
1727 BACKEND, which we already know is a program-type backend. */
1729 retrieve_options_from_program (gc_component_t component
, gc_backend_t backend
)
1733 const char *pgmname
;
1734 const char *argv
[2];
1738 size_t line_len
= 0;
1741 char *config_filename
;
1743 err
= gnupg_create_inbound_pipe (filedes
);
1745 gc_error (1, 0, _("error creating a pipe: %s\n"), gpg_strerror (err
));
1747 pgmname
= (gc_backend
[backend
].module_name
1748 ? gnupg_module_name (gc_backend
[backend
].module_name
)
1749 : gc_backend
[backend
].program
);
1750 argv
[0] = "--gpgconf-list";
1753 err
= gnupg_spawn_process_fd (pgmname
, argv
, -1, filedes
[1], -1, &pid
);
1758 gc_error (1, 0, "could not gather active options from `%s': %s",
1759 pgmname
, gpg_strerror (err
));
1762 config
= fdopen (filedes
[0], "r");
1764 gc_error (1, errno
, "can't fdopen pipe for reading");
1766 while ((length
= read_line (config
, &line
, &line_len
, NULL
)) > 0)
1768 gc_option_t
*option
;
1770 unsigned long flags
= 0;
1771 char *default_value
= NULL
;
1773 /* Strip newline and carriage return, if present. */
1775 && (line
[length
- 1] == '\n' || line
[length
- 1] == '\r'))
1776 line
[--length
] = '\0';
1778 linep
= strchr (line
, ':');
1782 /* Extract additional flags. Default to none. */
1788 end
= strchr (linep
, ':');
1793 flags
= strtoul (linep
, &tail
, 0);
1795 gc_error (1, errno
, "malformed flags in option %s from %s",
1797 if (!(*tail
== '\0' || *tail
== ':' || *tail
== ' '))
1798 gc_error (1, 0, "garbage after flags in option %s from %s",
1804 /* Extract default value, if present. Default to empty if
1810 end
= strchr (linep
, ':');
1814 if (flags
& GC_OPT_FLAG_DEFAULT
)
1815 default_value
= linep
;
1820 /* Look up the option in the component and install the
1821 configuration data. */
1822 option
= find_option (component
, line
, backend
);
1826 gc_error (1, errno
, "option %s returned twice from %s",
1830 option
->flags
|= flags
;
1831 if (default_value
&& *default_value
)
1832 option
->default_value
= xstrdup (default_value
);
1835 if (length
< 0 || ferror (config
))
1836 gc_error (1, errno
, "error reading from %s",pgmname
);
1837 if (fclose (config
) && ferror (config
))
1838 gc_error (1, errno
, "error closing %s", pgmname
);
1840 err
= gnupg_wait_process (pgmname
, pid
, &exitcode
);
1842 gc_error (1, 0, "running %s failed (exitcode=%d): %s",
1843 pgmname
, exitcode
, gpg_strerror (err
));
1846 /* At this point, we can parse the configuration file. */
1847 config_filename
= get_config_filename (component
, backend
);
1849 config
= fopen (config_filename
, "r");
1851 gc_error (0, errno
, "warning: can not open config file %s",
1855 while ((length
= read_line (config
, &line
, &line_len
, NULL
)) > 0)
1859 gc_option_t
*option
;
1862 while (*name
== ' ' || *name
== '\t')
1864 if (!*name
|| *name
== '#' || *name
== '\r' || *name
== '\n')
1868 while (*value
&& *value
!= ' ' && *value
!= '\t'
1869 && *value
!= '#' && *value
!= '\r' && *value
!= '\n')
1871 if (*value
== ' ' || *value
== '\t')
1876 while (*value
== ' ' || *value
== '\t')
1880 while (*end
&& *end
!= '#' && *end
!= '\r' && *end
!= '\n')
1882 while (end
> value
&& (end
[-1] == ' ' || end
[-1] == '\t'))
1889 /* Look up the option in the component and install the
1890 configuration data. */
1891 option
= find_option (component
, line
, backend
);
1896 if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_NONE
)
1900 "warning: ignoring argument %s for option %s",
1902 opt_value
= xstrdup ("1");
1904 else if (gc_arg_type
[option
->arg_type
].fallback
1905 == GC_ARG_TYPE_STRING
)
1906 opt_value
= xasprintf ("\"%s", gc_percent_escape (value
));
1909 /* FIXME: Verify that the number is sane. */
1910 opt_value
= xstrdup (value
);
1913 /* Now enter the option into the table. */
1914 if (!(option
->flags
& GC_OPT_FLAG_LIST
))
1917 free (option
->value
);
1918 option
->value
= opt_value
;
1923 option
->value
= opt_value
;
1926 char *opt_val
= opt_value
;
1928 option
->value
= xasprintf ("%s,%s", option
->value
,
1936 if (length
< 0 || ferror (config
))
1937 gc_error (1, errno
, "error reading from %s", config_filename
);
1938 if (fclose (config
) && ferror (config
))
1939 gc_error (1, errno
, "error closing %s", config_filename
);
1946 /* Retrieve the options for the component COMPONENT from backend
1947 BACKEND, which we already know is of type file list. */
1949 retrieve_options_from_file (gc_component_t component
, gc_backend_t backend
)
1951 gc_option_t
*list_option
;
1952 gc_option_t
*config_option
;
1953 char *list_filename
;
1956 size_t line_len
= 0;
1960 list_option
= find_option (component
,
1961 gc_backend
[backend
].option_name
, GC_BACKEND_ANY
);
1962 assert (list_option
);
1963 assert (!list_option
->active
);
1965 list_filename
= get_config_filename (component
, backend
);
1966 list_file
= fopen (list_filename
, "r");
1968 gc_error (0, errno
, "warning: can not open list file %s", list_filename
);
1972 while ((length
= read_line (list_file
, &line
, &line_len
, NULL
)) > 0)
1979 while (*start
== ' ' || *start
== '\t')
1981 if (!*start
|| *start
== '#' || *start
== '\r' || *start
== '\n')
1985 while (*end
&& *end
!= '#' && *end
!= '\r' && *end
!= '\n')
1987 /* Walk back to skip trailing white spaces. Looks evil, but
1988 works because of the conditions on START and END imposed
1989 at this point (END is at least START + 1, and START is
1990 not a whitespace character). */
1991 while (*(end
- 1) == ' ' || *(end
- 1) == '\t')
1994 /* FIXME: Oh, no! This is so lame! Should use realloc and
1998 new_list
= xasprintf ("%s,\"%s", list
, gc_percent_escape (start
));
2003 list
= xasprintf ("\"%s", gc_percent_escape (start
));
2005 if (length
< 0 || ferror (list_file
))
2006 gc_error (1, errno
, "can not read list file %s", list_filename
);
2009 list_option
->active
= 1;
2010 list_option
->value
= list
;
2012 /* Fix up the read-only flag. */
2013 config_option
= find_option
2014 (component
, gc_backend
[backend
].option_config_filename
, GC_BACKEND_ANY
);
2015 if (config_option
->flags
& GC_OPT_FLAG_NO_CHANGE
)
2016 list_option
->flags
|= GC_OPT_FLAG_NO_CHANGE
;
2018 if (list_file
&& fclose (list_file
) && ferror (list_file
))
2019 gc_error (1, errno
, "error closing %s", list_filename
);
2024 /* Retrieve the currently active options and their defaults from all
2025 involved backends for this component. Using -1 for component will
2026 retrieve all options from all components. */
2028 gc_component_retrieve_options (int component
)
2030 int process_all
= 0;
2031 int backend_seen
[GC_BACKEND_NR
];
2032 gc_backend_t backend
;
2033 gc_option_t
*option
;
2035 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
2036 backend_seen
[backend
] = 0;
2038 if (component
== -1)
2042 assert (component
< GC_COMPONENT_NR
);
2047 option
= gc_component
[component
].options
;
2049 while (option
&& option
->name
)
2051 if (!(option
->flags
& GC_OPT_FLAG_GROUP
))
2053 backend
= option
->backend
;
2055 if (backend_seen
[backend
])
2060 backend_seen
[backend
] = 1;
2062 assert (backend
!= GC_BACKEND_ANY
);
2064 if (gc_backend
[backend
].program
)
2065 retrieve_options_from_program (component
, backend
);
2067 retrieve_options_from_file (component
, backend
);
2072 while (process_all
&& ++component
< GC_COMPONENT_NR
);
2078 /* Perform a simple validity check based on the type. Return in
2079 NEW_VALUE_NR the value of the number in NEW_VALUE if OPTION is of
2080 type GC_ARG_TYPE_NONE. */
2082 option_check_validity (gc_option_t
*option
, unsigned long flags
,
2083 char *new_value
, unsigned long *new_value_nr
)
2087 if (!option
->active
)
2088 gc_error (1, 0, "option %s not supported by backend %s",
2089 option
->name
, gc_backend
[option
->backend
].name
);
2091 if (option
->new_flags
|| option
->new_value
)
2092 gc_error (1, 0, "option %s already changed", option
->name
);
2094 if (flags
& GC_OPT_FLAG_DEFAULT
)
2097 gc_error (1, 0, "argument %s provided for deleted option %s",
2098 new_value
, option
->name
);
2103 /* GC_ARG_TYPE_NONE options have special list treatment. */
2104 if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_NONE
)
2109 *new_value_nr
= strtoul (new_value
, &tail
, 0);
2112 gc_error (1, errno
, "invalid argument for option %s",
2115 gc_error (1, 0, "garbage after argument for option %s",
2118 if (!(option
->flags
& GC_OPT_FLAG_LIST
))
2120 if (*new_value_nr
!= 1)
2121 gc_error (1, 0, "argument for non-list option %s of type 0 "
2122 "(none) must be 1", option
->name
);
2126 if (*new_value_nr
== 0)
2127 gc_error (1, 0, "argument for option %s of type 0 (none) "
2128 "must be positive", option
->name
);
2137 if (*arg
== '\0' || *arg
== ',')
2139 if (!(option
->flags
& GC_OPT_FLAG_ARG_OPT
))
2140 gc_error (1, 0, "argument required for option %s", option
->name
);
2142 if (*arg
== ',' && !(option
->flags
& GC_OPT_FLAG_LIST
))
2143 gc_error (1, 0, "list found for non-list option %s", option
->name
);
2145 else if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_STRING
)
2148 gc_error (1, 0, "string argument for option %s must begin "
2149 "with a quote (\") character", option
->name
);
2151 /* FIXME: We do not allow empty string arguments for now, as
2152 we do not quote arguments in configuration files, and
2153 thus no argument is indistinguishable from the empty
2155 if (arg
[1] == '\0' || arg
[1] == ',')
2156 gc_error (1, 0, "empty string argument for option %s is "
2157 "currently not allowed. Please report this!",
2160 else if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_INT32
)
2163 (void) strtol (arg
, &arg
, 0);
2166 gc_error (1, errno
, "invalid argument for option %s",
2169 if (*arg
!= '\0' && *arg
!= ',')
2170 gc_error (1, 0, "garbage after argument for option %s",
2173 else if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_INT32
)
2176 (void) strtoul (arg
, &arg
, 0);
2179 gc_error (1, errno
, "invalid argument for option %s",
2182 if (*arg
!= '\0' && *arg
!= ',')
2183 gc_error (1, 0, "garbage after argument for option %s",
2186 arg
= strchr (arg
, ',');
2190 while (arg
&& *arg
);
2193 #ifdef HAVE_W32_SYSTEM
2195 copy_file (const char *src_name
, const char *dst_name
)
2197 #define BUF_LEN 4096
2198 char buffer
[BUF_LEN
];
2203 src
= fopen (src_name
, "r");
2207 dst
= fopen (dst_name
, "w");
2210 int saved_err
= errno
;
2220 len
= fread (buffer
, 1, BUF_LEN
, src
);
2223 written
= fwrite (buffer
, 1, len
, dst
);
2227 while (!feof (src
) && !ferror (src
) && !ferror (dst
));
2229 if (ferror (src
) || ferror (dst
) || !feof (src
))
2231 int saved_errno
= errno
;
2235 errno
= saved_errno
;
2239 if (fclose (dst
) && ferror (dst
))
2240 gc_error (1, errno
, "error closing %s", dst_name
);
2241 if (fclose (src
) && ferror (src
))
2242 gc_error (1, errno
, "error closing %s", src_name
);
2246 #endif /* HAVE_W32_SYSTEM */
2249 /* Create and verify the new configuration file for the specified
2250 backend and component. Returns 0 on success and -1 on error. */
2252 change_options_file (gc_component_t component
, gc_backend_t backend
,
2253 char **src_filenamep
, char **dest_filenamep
,
2254 char **orig_filenamep
)
2256 static const char marker
[] = "###+++--- GPGConf ---+++###";
2257 /* True if we are within the marker in the config file. */
2259 gc_option_t
*option
;
2265 FILE *src_file
= NULL
;
2266 FILE *dest_file
= NULL
;
2268 char *dest_filename
;
2269 char *orig_filename
;
2271 char *cur_arg
= NULL
;
2273 option
= find_option (component
,
2274 gc_backend
[backend
].option_name
, GC_BACKEND_ANY
);
2276 assert (option
->active
);
2277 assert (gc_arg_type
[option
->arg_type
].fallback
!= GC_ARG_TYPE_NONE
);
2279 /* FIXME. Throughout the function, do better error reporting. */
2280 /* Note that get_config_filename() calls percent_deescape(), so we
2281 call this before processing the arguments. */
2282 dest_filename
= xstrdup (get_config_filename (component
, backend
));
2283 src_filename
= xasprintf ("%s.gpgconf.%i.new", dest_filename
, getpid ());
2284 orig_filename
= xasprintf ("%s.gpgconf.%i.bak", dest_filename
, getpid ());
2286 arg
= option
->new_value
;
2287 if (arg
&& arg
[0] == '\0')
2294 end
= strchr (arg
, ',');
2298 cur_arg
= percent_deescape (arg
);
2308 #ifdef HAVE_W32_SYSTEM
2309 res
= copy_file (dest_filename
, orig_filename
);
2311 res
= link (dest_filename
, orig_filename
);
2313 if (res
< 0 && errno
!= ENOENT
)
2317 xfree (orig_filename
);
2318 orig_filename
= NULL
;
2321 /* We now initialize the return strings, so the caller can do the
2323 *src_filenamep
= src_filename
;
2324 *dest_filenamep
= dest_filename
;
2325 *orig_filenamep
= orig_filename
;
2327 /* Use open() so that we can use O_EXCL. */
2328 fd
= open (src_filename
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
2331 src_file
= fdopen (fd
, "w");
2339 /* Only if ORIG_FILENAME is not NULL did the configuration file
2340 exist already. In this case, we will copy its content into the
2341 new configuration file, changing it to our liking in the
2345 dest_file
= fopen (dest_filename
, "r");
2347 goto change_file_one_err
;
2349 while ((length
= read_line (dest_file
, &line
, &line_len
, NULL
)) > 0)
2354 if (!strncmp (marker
, line
, sizeof (marker
) - 1))
2363 while (*start
== ' ' || *start
== '\t')
2365 if (*start
&& *start
!= '\r' && *start
!= '\n' && *start
!= '#')
2374 /* Search for the end of the line. */
2375 while (*endp
&& *endp
!= '#' && *endp
!= '\r' && *endp
!= '\n')
2378 if (*endp
&& *endp
!= ' ' && *endp
!= '\t'
2379 && *endp
!= '\r' && *endp
!= '\n' && *endp
!= '#')
2385 if ((option
->new_flags
& GC_OPT_FLAG_DEFAULT
)
2386 || !cur_arg
|| strcmp (start
, cur_arg
))
2390 /* Find next argument. */
2396 arg_end
= strchr (arg
, ',');
2400 cur_arg
= percent_deescape (arg
);
2421 "# GPGConf disabled this option here at %s\n",
2422 asctimestamp (gnupg_get_time ()));
2423 if (ferror (src_file
))
2424 goto change_file_one_err
;
2425 fprintf (src_file
, "# %s", line
);
2426 if (ferror (src_file
))
2427 goto change_file_one_err
;
2432 fprintf (src_file
, "%s", line
);
2433 if (ferror (src_file
))
2434 goto change_file_one_err
;
2437 if (length
< 0 || ferror (dest_file
))
2438 goto change_file_one_err
;
2443 /* There was no marker. This is the first time we edit the
2444 file. We add our own marker at the end of the file and
2445 proceed. Note that we first write a newline, this guards us
2446 against files which lack the newline at the end of the last
2447 line, while it doesn't hurt us in all other cases. */
2448 fprintf (src_file
, "\n%s\n", marker
);
2449 if (ferror (src_file
))
2450 goto change_file_one_err
;
2453 /* At this point, we have copied everything up to the end marker
2454 into the new file, except for the arguments we are going to add.
2455 Now, dump the new arguments and write the end marker, possibly
2456 followed by the rest of the original file. */
2459 fprintf (src_file
, "%s\n", cur_arg
);
2461 /* Find next argument. */
2467 end
= strchr (arg
, ',');
2471 cur_arg
= percent_deescape (arg
);
2484 fprintf (src_file
, "%s %s\n", marker
, asctimestamp (gnupg_get_time ()));
2485 if (ferror (src_file
))
2486 goto change_file_one_err
;
2490 fprintf (src_file
, "# GPGConf edited this configuration file.\n");
2491 if (ferror (src_file
))
2492 goto change_file_one_err
;
2493 fprintf (src_file
, "# It will disable options before this marked "
2494 "block, but it will\n");
2495 if (ferror (src_file
))
2496 goto change_file_one_err
;
2497 fprintf (src_file
, "# never change anything below these lines.\n");
2498 if (ferror (src_file
))
2499 goto change_file_one_err
;
2503 while ((length
= read_line (dest_file
, &line
, &line_len
, NULL
)) > 0)
2505 fprintf (src_file
, "%s", line
);
2506 if (ferror (src_file
))
2507 goto change_file_one_err
;
2509 if (length
< 0 || ferror (dest_file
))
2510 goto change_file_one_err
;
2515 res
= fclose (src_file
);
2528 res
= fclose (dest_file
);
2534 change_file_one_err
:
2549 /* Create and verify the new configuration file for the specified
2550 backend and component. Returns 0 on success and -1 on error. */
2552 change_options_program (gc_component_t component
, gc_backend_t backend
,
2553 char **src_filenamep
, char **dest_filenamep
,
2554 char **orig_filenamep
)
2556 static const char marker
[] = "###+++--- GPGConf ---+++###";
2557 /* True if we are within the marker in the config file. */
2559 gc_option_t
*option
;
2565 FILE *src_file
= NULL
;
2566 FILE *dest_file
= NULL
;
2568 char *dest_filename
;
2569 char *orig_filename
;
2570 /* Special hack for gpg, see below. */
2571 int utf8strings_seen
= 0;
2573 /* FIXME. Throughout the function, do better error reporting. */
2574 dest_filename
= xstrdup (get_config_filename (component
, backend
));
2575 src_filename
= xasprintf ("%s.gpgconf.%i.new", dest_filename
, getpid ());
2576 orig_filename
= xasprintf ("%s.gpgconf.%i.bak", dest_filename
, getpid ());
2578 #ifdef HAVE_W32_SYSTEM
2579 res
= copy_file (dest_filename
, orig_filename
);
2581 res
= link (dest_filename
, orig_filename
);
2583 if (res
< 0 && errno
!= ENOENT
)
2587 xfree (orig_filename
);
2588 orig_filename
= NULL
;
2591 /* We now initialize the return strings, so the caller can do the
2593 *src_filenamep
= src_filename
;
2594 *dest_filenamep
= dest_filename
;
2595 *orig_filenamep
= orig_filename
;
2597 /* Use open() so that we can use O_EXCL. */
2598 fd
= open (src_filename
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
2601 src_file
= fdopen (fd
, "w");
2609 /* Only if ORIG_FILENAME is not NULL did the configuration file
2610 exist already. In this case, we will copy its content into the
2611 new configuration file, changing it to our liking in the
2615 dest_file
= fopen (dest_filename
, "r");
2617 goto change_one_err
;
2619 while ((length
= read_line (dest_file
, &line
, &line_len
, NULL
)) > 0)
2624 if (!strncmp (marker
, line
, sizeof (marker
) - 1))
2631 else if (backend
== GC_BACKEND_GPG
&& in_marker
2632 && ! strcmp ("utf8-strings\n", line
))
2634 /* Strip duplicated entries. */
2635 if (utf8strings_seen
)
2638 utf8strings_seen
= 1;
2642 while (*start
== ' ' || *start
== '\t')
2644 if (*start
&& *start
!= '\r' && *start
!= '\n' && *start
!= '#')
2650 while (*end
&& *end
!= ' ' && *end
!= '\t'
2651 && *end
!= '\r' && *end
!= '\n' && *end
!= '#')
2656 option
= find_option (component
, start
, backend
);
2658 if (option
&& ((option
->new_flags
& GC_OPT_FLAG_DEFAULT
)
2659 || option
->new_value
))
2667 "# GPGConf disabled this option here at %s\n",
2668 asctimestamp (gnupg_get_time ()));
2669 if (ferror (src_file
))
2670 goto change_one_err
;
2671 fprintf (src_file
, "# %s", line
);
2672 if (ferror (src_file
))
2673 goto change_one_err
;
2678 fprintf (src_file
, "%s", line
);
2679 if (ferror (src_file
))
2680 goto change_one_err
;
2683 if (length
< 0 || ferror (dest_file
))
2684 goto change_one_err
;
2689 /* There was no marker. This is the first time we edit the
2690 file. We add our own marker at the end of the file and
2691 proceed. Note that we first write a newline, this guards us
2692 against files which lack the newline at the end of the last
2693 line, while it doesn't hurt us in all other cases. */
2694 fprintf (src_file
, "\n%s\n", marker
);
2695 if (ferror (src_file
))
2696 goto change_one_err
;
2698 /* At this point, we have copied everything up to the end marker
2699 into the new file, except for the options we are going to change.
2700 Now, dump the changed options (except for those we are going to
2701 revert to their default), and write the end marker, possibly
2702 followed by the rest of the original file. */
2704 /* We have to turn on UTF8 strings for GnuPG. */
2705 if (backend
== GC_BACKEND_GPG
&& ! utf8strings_seen
)
2706 fprintf (src_file
, "utf8-strings\n");
2708 option
= gc_component
[component
].options
;
2709 while (option
->name
)
2711 if (!(option
->flags
& GC_OPT_FLAG_GROUP
)
2712 && option
->backend
== backend
2713 && option
->new_value
)
2715 char *arg
= option
->new_value
;
2719 if (*arg
== '\0' || *arg
== ',')
2721 fprintf (src_file
, "%s\n", option
->name
);
2722 if (ferror (src_file
))
2723 goto change_one_err
;
2725 else if (gc_arg_type
[option
->arg_type
].fallback
2726 == GC_ARG_TYPE_NONE
)
2728 assert (*arg
== '1');
2729 fprintf (src_file
, "%s\n", option
->name
);
2730 if (ferror (src_file
))
2731 goto change_one_err
;
2735 else if (gc_arg_type
[option
->arg_type
].fallback
2736 == GC_ARG_TYPE_STRING
)
2740 assert (*arg
== '"');
2743 end
= strchr (arg
, ',');
2747 fprintf (src_file
, "%s %s\n", option
->name
,
2748 percent_deescape (arg
));
2749 if (ferror (src_file
))
2750 goto change_one_err
;
2760 end
= strchr (arg
, ',');
2764 fprintf (src_file
, "%s %s\n", option
->name
, arg
);
2765 if (ferror (src_file
))
2766 goto change_one_err
;
2773 assert (arg
== NULL
|| *arg
== '\0' || *arg
== ',');
2774 if (arg
&& *arg
== ',')
2777 while (arg
&& *arg
);
2782 fprintf (src_file
, "%s %s\n", marker
, asctimestamp (gnupg_get_time ()));
2783 if (ferror (src_file
))
2784 goto change_one_err
;
2788 fprintf (src_file
, "# GPGConf edited this configuration file.\n");
2789 if (ferror (src_file
))
2790 goto change_one_err
;
2791 fprintf (src_file
, "# It will disable options before this marked "
2792 "block, but it will\n");
2793 if (ferror (src_file
))
2794 goto change_one_err
;
2795 fprintf (src_file
, "# never change anything below these lines.\n");
2796 if (ferror (src_file
))
2797 goto change_one_err
;
2801 while ((length
= read_line (dest_file
, &line
, &line_len
, NULL
)) > 0)
2803 fprintf (src_file
, "%s", line
);
2804 if (ferror (src_file
))
2805 goto change_one_err
;
2807 if (length
< 0 || ferror (dest_file
))
2808 goto change_one_err
;
2813 res
= fclose (src_file
);
2826 res
= fclose (dest_file
);
2847 /* Common code for gc_component_change_options and
2848 gc_process_gpgconf_conf. */
2850 change_one_value (gc_option_t
*option
, int *runtime
,
2851 unsigned long flags
, char *new_value
)
2853 unsigned long new_value_nr
= 0;
2855 option_check_validity (option
, flags
, new_value
, &new_value_nr
);
2857 if (option
->flags
& GC_OPT_FLAG_RUNTIME
)
2858 runtime
[option
->backend
] = 1;
2860 option
->new_flags
= flags
;
2861 if (!(flags
& GC_OPT_FLAG_DEFAULT
))
2863 if (gc_arg_type
[option
->arg_type
].fallback
== GC_ARG_TYPE_NONE
2864 && (option
->flags
& GC_OPT_FLAG_LIST
))
2868 /* We convert the number to a list of 1's for convenient
2870 assert (new_value_nr
> 0);
2871 option
->new_value
= xmalloc ((2 * (new_value_nr
- 1) + 1) + 1);
2872 str
= option
->new_value
;
2874 while (--new_value_nr
> 0)
2882 option
->new_value
= xstrdup (new_value
);
2887 /* Read the modifications from IN and apply them. If IN is NULL the
2888 modifications are expected to already have been set to the global
2891 gc_component_change_options (int component
, FILE *in
, FILE *out
)
2894 int runtime
[GC_BACKEND_NR
];
2895 char *src_filename
[GC_BACKEND_NR
];
2896 char *dest_filename
[GC_BACKEND_NR
];
2897 char *orig_filename
[GC_BACKEND_NR
];
2898 gc_backend_t backend
;
2899 gc_option_t
*option
;
2901 size_t line_len
= 0;
2904 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
2906 runtime
[backend
] = 0;
2907 src_filename
[backend
] = NULL
;
2908 dest_filename
[backend
] = NULL
;
2909 orig_filename
[backend
] = NULL
;
2914 /* Read options from the file IN. */
2915 while ((length
= read_line (in
, &line
, &line_len
, NULL
)) > 0)
2918 unsigned long flags
= 0;
2919 char *new_value
= "";
2921 /* Strip newline and carriage return, if present. */
2923 && (line
[length
- 1] == '\n' || line
[length
- 1] == '\r'))
2924 line
[--length
] = '\0';
2926 linep
= strchr (line
, ':');
2930 /* Extract additional flags. Default to none. */
2936 end
= strchr (linep
, ':');
2941 flags
= strtoul (linep
, &tail
, 0);
2943 gc_error (1, errno
, "malformed flags in option %s", line
);
2944 if (!(*tail
== '\0' || *tail
== ':' || *tail
== ' '))
2945 gc_error (1, 0, "garbage after flags in option %s", line
);
2950 /* Don't allow setting of the no change flag. */
2951 flags
&= ~GC_OPT_FLAG_NO_CHANGE
;
2953 /* Extract default value, if present. Default to empty if not. */
2957 end
= strchr (linep
, ':');
2964 option
= find_option (component
, line
, GC_BACKEND_ANY
);
2966 gc_error (1, 0, "unknown option %s", line
);
2968 if ((option
->flags
& GC_OPT_FLAG_NO_CHANGE
))
2970 gc_error (0, 0, "ignoring new value for option %s",
2975 change_one_value (option
, runtime
, flags
, new_value
);
2979 /* Now that we have collected and locally verified the changes,
2980 write them out to new configuration files, verify them
2981 externally, and then commit them. */
2982 option
= gc_component
[component
].options
;
2983 while (option
&& option
->name
)
2985 /* Go on if we have already seen this backend, or if there is
2987 if (src_filename
[option
->backend
]
2988 || !(option
->new_flags
|| option
->new_value
))
2994 if (gc_backend
[option
->backend
].program
)
2996 err
= change_options_program (component
, option
->backend
,
2997 &src_filename
[option
->backend
],
2998 &dest_filename
[option
->backend
],
2999 &orig_filename
[option
->backend
]);
3002 /* External verification. */
3003 err
= gc_component_check_options (component
, out
,
3004 src_filename
[option
->backend
]);
3008 _("External verification of component %s failed"),
3009 gc_component
[component
].name
);
3016 err
= change_options_file (component
, option
->backend
,
3017 &src_filename
[option
->backend
],
3018 &dest_filename
[option
->backend
],
3019 &orig_filename
[option
->backend
]);
3027 if (! err
&& ! opt
.dry_run
)
3031 for (i
= 0; i
< GC_BACKEND_NR
; i
++)
3033 if (src_filename
[i
])
3035 /* FIXME: Make a verification here. */
3037 assert (dest_filename
[i
]);
3039 if (orig_filename
[i
])
3041 #ifdef HAVE_W32_SYSTEM
3042 /* There is no atomic update on W32. */
3043 err
= unlink (dest_filename
[i
]);
3044 #endif /* HAVE_W32_SYSTEM */
3046 err
= rename (src_filename
[i
], dest_filename
[i
]);
3050 #ifdef HAVE_W32_SYSTEM
3051 /* We skip the unlink if we expect the file not to
3053 err
= rename (src_filename
[i
], dest_filename
[i
]);
3054 #else /* HAVE_W32_SYSTEM */
3055 /* This is a bit safer than rename() because we
3056 expect DEST_FILENAME not to be there. If it
3057 happens to be there, this will fail. */
3058 err
= link (src_filename
[i
], dest_filename
[i
]);
3060 err
= unlink (src_filename
[i
]);
3061 #endif /* !HAVE_W32_SYSTEM */
3065 src_filename
[i
] = NULL
;
3070 if (err
|| opt
.dry_run
)
3073 int saved_errno
= errno
;
3075 /* An error occured or a dry-run is requested. */
3076 for (i
= 0; i
< GC_BACKEND_NR
; i
++)
3078 if (src_filename
[i
])
3080 /* The change was not yet committed. */
3081 unlink (src_filename
[i
]);
3082 if (orig_filename
[i
])
3083 unlink (orig_filename
[i
]);
3087 /* The changes were already committed. FIXME: This is a
3088 tad dangerous, as we don't know if we don't overwrite
3089 a version of the file that is even newer than the one
3090 we just installed. */
3091 if (orig_filename
[i
])
3093 #ifdef HAVE_W32_SYSTEM
3094 /* There is no atomic update on W32. */
3095 unlink (dest_filename
[i
]);
3096 #endif /* HAVE_W32_SYSTEM */
3097 rename (orig_filename
[i
], dest_filename
[i
]);
3100 unlink (dest_filename
[i
]);
3104 gc_error (1, saved_errno
, "could not commit changes");
3106 /* Fall-through for dry run. */
3110 /* If it all worked, notify the daemons of the changes. */
3112 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
3114 if (runtime
[backend
] && gc_backend
[backend
].runtime_change
)
3115 (*gc_backend
[backend
].runtime_change
) ();
3118 /* Move the per-process backup file into its place. */
3119 for (backend
= 0; backend
< GC_BACKEND_NR
; backend
++)
3120 if (orig_filename
[backend
])
3122 char *backup_filename
;
3124 assert (dest_filename
[backend
]);
3126 backup_filename
= xasprintf ("%s.gpgconf.bak", dest_filename
[backend
]);
3128 #ifdef HAVE_W32_SYSTEM
3129 /* There is no atomic update on W32. */
3130 unlink (backup_filename
);
3131 #endif /* HAVE_W32_SYSTEM */
3132 rename (orig_filename
[backend
], backup_filename
);
3140 /* Check whether USER matches the current user of one of its group.
3141 This function may change USER. Returns true is there is a
3144 key_matches_user_or_group (char *user
)
3148 if (*user
== '*' && user
[1] == 0)
3149 return 1; /* A single asterisk matches all users. */
3151 group
= strchr (user
, ':');
3155 #ifdef HAVE_W32_SYSTEM
3156 /* Under Windows we don't support groups. */
3157 if (group
&& *group
)
3158 gc_error (0, 0, _("Note that group specifications are ignored\n"));
3161 static char *my_name
;
3168 GetUserNameA (tmp
, &size
);
3169 my_name
= xmalloc (size
);
3170 if (!GetUserNameA (my_name
, &size
))
3171 gc_error (1,0, "error getting current user name: %s",
3175 if (!strcmp (user
, my_name
))
3176 return 1; /* Found. */
3178 #else /*!HAVE_W32_SYSTEM*/
3179 /* First check whether the user matches. */
3182 static char *my_name
;
3186 struct passwd
*pw
= getpwuid ( getuid () );
3188 gc_error (1, errno
, "getpwuid failed for current user");
3189 my_name
= xstrdup (pw
->pw_name
);
3191 if (!strcmp (user
, my_name
))
3192 return 1; /* Found. */
3195 /* If that failed, check whether a group matches. */
3196 if (group
&& *group
)
3198 static char *my_group
;
3199 static char **my_supgroups
;
3204 struct group
*gr
= getgrgid ( getgid () );
3206 gc_error (1, errno
, "getgrgid failed for current user");
3207 my_group
= xstrdup (gr
->gr_name
);
3209 if (!strcmp (group
, my_group
))
3210 return 1; /* Found. */
3217 ngids
= getgroups (0, NULL
);
3218 gids
= xcalloc (ngids
+1, sizeof *gids
);
3219 ngids
= getgroups (ngids
, gids
);
3221 gc_error (1, errno
, "getgroups failed for current user");
3222 my_supgroups
= xcalloc (ngids
+1, sizeof *my_supgroups
);
3223 for (n
=0; n
< ngids
; n
++)
3225 struct group
*gr
= getgrgid ( gids
[n
] );
3227 gc_error (1, errno
, "getgrgid failed for supplementary group");
3228 my_supgroups
[n
] = xstrdup (gr
->gr_name
);
3233 for (n
=0; my_supgroups
[n
]; n
++)
3234 if (!strcmp (group
, my_supgroups
[n
]))
3235 return 1; /* Found. */
3237 #endif /*!HAVE_W32_SYSTEM*/
3238 return 0; /* No match. */
3243 /* Read and process the global configuration file for gpgconf. This
3244 optional file is used to update our internal tables at runtime and
3245 may also be used to set new default values. If FNAME is NULL the
3246 default name will be used. With UPDATE set to true the internal
3247 tables are actually updated; if not set, only a syntax check is
3248 done. If DEFAULTS is true the global options are written to the
3249 configuration files. If LISTFP is set, no changes are done but the
3250 configuration file is printed to LISTFP in a colon separated format.
3252 Returns 0 on success or if the config file is not present; -1 is
3253 returned on error. */
3255 gc_process_gpgconf_conf (const char *fname_arg
, int update
, int defaults
,
3260 size_t line_len
= 0;
3266 int runtime
[GC_BACKEND_NR
];
3267 int used_components
[GC_COMPONENT_NR
];
3268 int backend_id
, component_id
;
3272 fname
= xstrdup (fname_arg
);
3274 fname
= make_filename (gnupg_sysconfdir (), "gpgconf.conf", NULL
);
3276 for (backend_id
= 0; backend_id
< GC_BACKEND_NR
; backend_id
++)
3277 runtime
[backend_id
] = 0;
3278 for (component_id
= 0; component_id
< GC_COMPONENT_NR
; component_id
++)
3279 used_components
[component_id
] = 0;
3281 config
= fopen (fname
, "r");
3284 /* Do not print an error if the file is not available, except
3285 when running in syntax check mode. */
3286 if (errno
!= ENOENT
|| !update
)
3288 gc_error (0, errno
, "can not open global config file `%s'", fname
);
3295 while ((length
= read_line (config
, &line
, &line_len
, NULL
)) > 0)
3297 char *key
, *component
, *option
, *flags
, *value
;
3299 gc_option_t
*option_info
= NULL
;
3301 int is_continuation
;
3305 while (*key
== ' ' || *key
== '\t')
3307 if (!*key
|| *key
== '#' || *key
== '\r' || *key
== '\n')
3310 is_continuation
= (key
!= line
);
3312 /* Parse the key field. */
3313 if (!is_continuation
&& got_match
)
3314 break; /* Finish after the first match. */
3315 else if (!is_continuation
)
3318 for (p
=key
+1; *p
&& !strchr (" \t\r\n", *p
); p
++)
3322 gc_error (0, 0, "missing rule at `%s', line %d", fname
, lineno
);
3331 gc_error (0, 0, "continuation but no rule at `%s', line %d",
3344 /* Parse the component. */
3345 while (*component
== ' ' || *component
== '\t')
3347 for (p
=component
; *p
&& !strchr (" \t\r\n", *p
); p
++)
3351 gc_error (0, 0, "missing component at `%s', line %d",
3359 component_id
= gc_component_find (component
);
3360 if (component_id
< 0)
3362 gc_error (0, 0, "unknown component at `%s', line %d",
3367 /* Parse the option name. */
3368 while (*option
== ' ' || *option
== '\t')
3370 for (p
=option
; *p
&& !strchr (" \t\r\n", *p
); p
++)
3374 gc_error (0, 0, "missing option at `%s', line %d",
3381 if ( component_id
!= -1)
3383 option_info
= find_option (component_id
, option
, GC_BACKEND_ANY
);
3386 gc_error (0, 0, "unknown option at `%s', line %d",
3393 /* Parse the optional flags. */
3394 while (*flags
== ' ' || *flags
== '\t')
3399 p
= strchr (flags
, ']');
3402 gc_error (0, 0, "syntax error in rule at `%s', line %d",
3410 else /* No flags given. */
3416 /* Parse the optional value. */
3417 while (*value
== ' ' || *value
== '\t')
3419 for (p
=value
; *p
&& !strchr ("\r\n", *p
); p
++)
3422 value
= empty
; /* No value given; let it point to an empty string. */
3425 /* Strip trailing white space. */
3427 for (p
--; p
> value
&& (*p
== ' ' || *p
== '\t'); p
--)
3431 /* Check flag combinations. */
3434 else if (!strcmp (flags
, "default"))
3438 gc_error (0, 0, "flag \"default\" may not be combined "
3439 "with a value at `%s', line %d",
3444 else if (!strcmp (flags
, "change"))
3446 else if (!strcmp (flags
, "no-change"))
3450 gc_error (0, 0, "unknown flag at `%s', line %d",
3455 /* In list mode we print out all records. */
3456 if (listfp
&& !result
)
3458 /* If this is a new ruleset, print a key record. */
3459 if (!is_continuation
)
3461 char *group
= strchr (key
, ':');
3465 if ((p
= strchr (group
, ':')))
3466 *p
= 0; /* We better strip any extra stuff. */
3469 fprintf (listfp
, "k:%s:", gc_percent_escape (key
));
3470 fprintf (listfp
, "%s\n", group
? gc_percent_escape (group
):"");
3473 /* All other lines are rule records. */
3474 fprintf (listfp
, "r:::%s:%s:%s:",
3475 gc_component
[component_id
].name
,
3476 option_info
->name
? option_info
->name
: "",
3479 fprintf (listfp
, "\"%s", gc_percent_escape (value
));
3481 putc ('\n', listfp
);
3484 /* Check whether the key matches but do this only if we are not
3485 running in syntax check mode. */
3487 && !result
&& !listfp
3488 && (got_match
|| (key
&& key_matches_user_or_group (key
))) )
3494 /* Apply the flags from gpgconf.conf. */
3497 else if (!strcmp (flags
, "default"))
3498 newflags
|= GC_OPT_FLAG_DEFAULT
;
3499 else if (!strcmp (flags
, "no-change"))
3500 option_info
->flags
|= GC_OPT_FLAG_NO_CHANGE
;
3501 else if (!strcmp (flags
, "change"))
3502 option_info
->flags
&= ~GC_OPT_FLAG_NO_CHANGE
;
3506 assert (component_id
>= 0 && component_id
< GC_COMPONENT_NR
);
3507 used_components
[component_id
] = 1;
3509 /* Here we explicitly allow to update the value again. */
3512 option_info
->new_flags
= 0;
3516 xfree (option_info
->new_value
);
3517 option_info
->new_value
= NULL
;
3519 change_one_value (option_info
, runtime
, newflags
, value
);
3524 if (length
< 0 || ferror (config
))
3526 gc_error (0, errno
, "error reading from `%s'", fname
);
3529 if (fclose (config
) && ferror (config
))
3530 gc_error (0, errno
, "error closing `%s'", fname
);
3534 /* If it all worked, process the options. */
3535 if (!result
&& update
&& defaults
&& !listfp
)
3537 /* We need to switch off the runtime update, so that we can do
3538 it later all at once. */
3539 int save_opt_runtime
= opt
.runtime
;
3542 for (component_id
= 0; component_id
< GC_COMPONENT_NR
; component_id
++)
3544 gc_component_change_options (component_id
, NULL
, NULL
);
3546 opt
.runtime
= save_opt_runtime
;
3550 for (backend_id
= 0; backend_id
< GC_BACKEND_NR
; backend_id
++)
3551 if (runtime
[backend_id
] && gc_backend
[backend_id
].runtime_change
)
3552 (*gc_backend
[backend_id
].runtime_change
) ();