Make gpgv error message about a missing keyring more useful. This fixes
[gnupg.git] / tools / gpgconf.c
blob6f1fcbb54d0628d796ca6c4e56bb698fe9e8a550
1 /* gpgconf.c - Configuration utility for GnuPG
2 * Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it 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,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "gpgconf.h"
27 #include "i18n.h"
29 /* Constants to identify the commands and options. */
30 enum cmd_and_opt_values
32 aNull = 0,
33 oDryRun = 'n',
34 oOutput = 'o',
35 oQuiet = 'q',
36 oVerbose = 'v',
37 oRuntime = 'r',
38 oComponent = 'c',
39 oNoVerbose = 500,
40 oHomedir,
42 aListComponents,
43 aCheckPrograms,
44 aListOptions,
45 aChangeOptions,
46 aCheckOptions,
47 aApplyDefaults,
48 aListConfig,
49 aCheckConfig,
50 aListDirs,
51 aReload
56 /* The list of commands and options. */
57 static ARGPARSE_OPTS opts[] =
59 { 300, NULL, 0, N_("@Commands:\n ") },
61 { aListComponents, "list-components", 256, N_("list all components") },
62 { aCheckPrograms, "check-programs", 256, N_("check all programs") },
63 { aListOptions, "list-options", 256, N_("|COMPONENT|list options") },
64 { aChangeOptions, "change-options", 256, N_("|COMPONENT|change options") },
65 { aCheckOptions, "check-options", 256, N_("|COMPONENT|check options") },
66 { aApplyDefaults, "apply-defaults", 256,
67 N_("apply global default values") },
68 { aListDirs, "list-dirs", 256,
69 N_("get the configuration directories for gpgconf") },
70 { aListConfig, "list-config", 256,
71 N_("list global configuration file") },
72 { aCheckConfig, "check-config", 256,
73 N_("check global configuration file") },
74 { aReload, "reload", 256, "@" },
76 { 301, NULL, 0, N_("@\nOptions:\n ") },
78 { oOutput, "output", 2, N_("use as output file") },
79 { oVerbose, "verbose", 0, N_("verbose") },
80 { oQuiet, "quiet", 0, N_("quiet") },
81 { oDryRun, "dry-run", 0, N_("do not make any changes") },
82 { oRuntime, "runtime", 0, N_("activate changes at runtime, if possible") },
83 /* hidden options */
84 { oNoVerbose, "no-verbose", 0, "@"},
85 {0}
89 /* Print usage information and and provide strings for help. */
90 static const char *
91 my_strusage( int level )
93 const char *p;
95 switch (level)
97 case 11: p = "gpgconf (GnuPG)";
98 break;
99 case 13: p = VERSION; break;
100 case 17: p = PRINTABLE_OS_NAME; break;
101 case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
102 break;
103 case 1:
104 case 40: p = _("Usage: gpgconf [options] (-h for help)");
105 break;
106 case 41:
107 p = _("Syntax: gpgconf [options]\n"
108 "Manage configuration options for tools of the GnuPG system\n");
109 break;
111 default: p = NULL; break;
113 return p;
117 /* Return the fp for the output. This is usually stdout unless
118 --output has been used. In the latter case this function opens
119 that file. */
120 static FILE *
121 get_outfp (FILE **fp)
123 if (!*fp)
125 if (opt.outfile)
127 *fp = fopen (opt.outfile, "w");
128 if (!*fp)
129 gc_error (1, errno, "can not open `%s'", opt.outfile);
131 else
132 *fp = stdout;
134 return *fp;
138 /* gpgconf main. */
140 main (int argc, char **argv)
142 ARGPARSE_ARGS pargs;
143 const char *fname;
144 int no_more_options = 0;
145 enum cmd_and_opt_values cmd = 0;
146 FILE *outfp = NULL;
148 set_strusage (my_strusage);
149 log_set_prefix ("gpgconf", 1);
151 /* Make sure that our subsystems are ready. */
152 i18n_init();
153 init_common_subsystems ();
155 /* Parse the command line. */
156 pargs.argc = &argc;
157 pargs.argv = &argv;
158 pargs.flags = 1; /* Do not remove the args. */
159 while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts))
161 switch (pargs.r_opt)
163 case oOutput: opt.outfile = pargs.r.ret_str; break;
164 case oQuiet: opt.quiet = 1; break;
165 case oDryRun: opt.dry_run = 1; break;
166 case oRuntime:
167 opt.runtime = 1;
168 break;
169 case oVerbose: opt.verbose++; break;
170 case oNoVerbose: opt.verbose = 0; break;
172 case aListDirs:
173 case aListComponents:
174 case aCheckPrograms:
175 case aListOptions:
176 case aChangeOptions:
177 case aCheckOptions:
178 case aApplyDefaults:
179 case aListConfig:
180 case aCheckConfig:
181 case aReload:
182 cmd = pargs.r_opt;
183 break;
185 default: pargs.err = 2; break;
189 if (log_get_errorcount (0))
190 exit (2);
192 fname = argc ? *argv : NULL;
194 switch (cmd)
196 case aListComponents:
197 default:
198 /* List all components. */
199 gc_component_list_components (get_outfp (&outfp));
200 break;
202 case aCheckPrograms:
203 /* Check all programs. */
204 gc_check_programs (get_outfp (&outfp));
205 break;
207 case aListOptions:
208 case aChangeOptions:
209 case aCheckOptions:
210 if (!fname)
212 fputs (_("usage: gpgconf [options] "), stderr);
213 putc ('\n',stderr);
214 fputs (_("Need one component argument"), stderr);
215 putc ('\n',stderr);
216 exit (2);
218 else
220 int idx = gc_component_find (fname);
221 if (idx < 0)
223 fputs (_("Component not found"), stderr);
224 putc ('\n', stderr);
225 exit (1);
227 gc_component_retrieve_options (idx);
228 if (gc_process_gpgconf_conf (NULL, 1, 0, NULL))
229 exit (1);
230 if (cmd == aListOptions)
231 gc_component_list_options (idx, get_outfp (&outfp));
232 else if (cmd == aChangeOptions)
233 gc_component_change_options (idx, stdin, get_outfp (&outfp));
234 else
235 gc_component_check_options (idx, get_outfp (&outfp), NULL);
237 break;
239 case aReload:
240 if (!fname)
242 /* Reload all. */
243 gc_component_reload (-1);
245 else
247 /* Reload given component. */
248 int idx;
250 idx = gc_component_find (fname);
251 if (idx < 0)
253 fputs (_("Component not found"), stderr);
254 putc ('\n', stderr);
255 exit (1);
257 else
259 gc_component_reload (idx);
262 break;
264 case aListConfig:
265 if (gc_process_gpgconf_conf (fname, 0, 0, get_outfp (&outfp)))
266 exit (1);
267 break;
269 case aCheckConfig:
270 if (gc_process_gpgconf_conf (fname, 0, 0, NULL))
271 exit (1);
272 break;
274 case aApplyDefaults:
275 if (fname)
277 fputs (_("usage: gpgconf [options] "), stderr);
278 putc ('\n',stderr);
279 fputs (_("No argument allowed"), stderr);
280 putc ('\n',stderr);
281 exit (2);
283 gc_component_retrieve_options (-1);
284 if (gc_process_gpgconf_conf (NULL, 1, 1, NULL))
285 exit (1);
286 break;
288 case aListDirs:
289 /* Show the system configuration directories for gpgconf. */
290 get_outfp (&outfp);
291 fprintf (outfp, "sysconfdir:%s\n",
292 gc_percent_escape (gnupg_sysconfdir ()));
293 fprintf (outfp, "bindir:%s\n",
294 gc_percent_escape (gnupg_bindir ()));
295 fprintf (outfp, "libexecdir:%s\n",
296 gc_percent_escape (gnupg_libexecdir ()));
297 fprintf (outfp, "libdir:%s\n",
298 gc_percent_escape (gnupg_libdir ()));
299 fprintf (outfp, "datadir:%s\n",
300 gc_percent_escape (gnupg_datadir ()));
301 fprintf (outfp, "localedir:%s\n",
302 gc_percent_escape (gnupg_localedir ()));
303 fprintf (outfp, "dirmngr-socket:%s\n",
304 gc_percent_escape (dirmngr_socket_name ()));
306 char *infostr = getenv ("GPG_AGENT_INFO");
308 if (!infostr || !*infostr)
309 infostr = make_filename (default_homedir (), "S.gpg-agent", NULL);
310 else
312 char *tmp;
314 infostr = xstrdup (infostr);
315 tmp = strchr (infostr, PATHSEP_C);
316 if (!tmp || tmp == infostr)
318 xfree (infostr);
319 infostr = NULL;
321 else
322 *tmp = 0;
324 fprintf (outfp, "agent-socket:%s\n",
325 infostr? gc_percent_escape (infostr) : "");
326 xfree (infostr);
329 /* We need to use make_filename to expand a possible "~/". */
330 char *tmp = make_filename (default_homedir (), NULL);
331 fprintf (outfp, "homedir:%s\n", gc_percent_escape (tmp));
332 xfree (tmp);
334 break;
337 if (outfp && outfp != stdout)
338 if (fclose (outfp))
339 gc_error (1, errno, "error closing `%s'", opt.outfile);
341 return 0;