Imported Upstream version 2.3.8
[platform/upstream/gpg2.git] / common / comopt.c
1 /* comopt.c - Common options for GnUPG (common.conf)
2  * Copyright (C) 2021 g10 Code GmbH
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  * SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
29  */
30
31 #include <config.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "util.h"
37 #include "i18n.h"
38 #include "comopt.h"
39
40
41 enum opt_values
42   {
43     aNull = 0,
44
45     oLogFile = 500,
46     oUseKeyboxd,
47     oKeyboxdProgram,
48     oNoAutostart,
49
50     oNoop
51   };
52
53 static gpgrt_opt_t opts[] = {
54   ARGPARSE_s_s (oLogFile,        "log-file", "@"),
55   ARGPARSE_s_n (oUseKeyboxd,     "use-keyboxd", "@"),
56   ARGPARSE_s_n (oNoAutostart,    "no-autostart", "@"),
57   ARGPARSE_s_s (oKeyboxdProgram, "keyboxd-program", "@"),
58
59   ARGPARSE_end ()
60 };
61
62
63
64 /* Parse the common options in the homedir and etc.  This needs to be
65  * called after the gpgrt config directories are set.  MODULE_ID is one of
66  * the GNUPG_MODULE_NAME_ constants.  If verbose is true info about
67  * the parsing is printed.  Note that this function is not
68  * thread-safe. */
69 gpg_error_t
70 parse_comopt (int module_id, int verbose)
71 {
72   gpg_error_t err = 0;
73   gpgrt_argparse_t pargs;
74   int argc = 0;
75   char **argv = NULL;
76
77   /* Reset all options in case we are called a second time.  */
78   xfree (comopt.logfile);
79   xfree (comopt.keyboxd_program);
80   memset (&comopt, 0, sizeof comopt);
81
82   /* Start the parser.  */
83   pargs.argc = &argc;
84   pargs.argv = &argv;
85   pargs.flags = (ARGPARSE_FLAG_NOVERSION
86                  | ARGPARSE_FLAG_SYS
87                  | ARGPARSE_FLAG_USER
88                  );
89   while (gpgrt_argparser (&pargs, opts, "common" EXTSEP_S "conf" ))
90     {
91       switch (pargs.r_opt)
92         {
93         case ARGPARSE_CONFFILE:
94           if (verbose)
95             log_info (_("reading options from '%s'\n"),
96                       pargs.r_type? pargs.r.ret_str: "[cmdline]");
97           break;
98
99         case oLogFile:
100           comopt.logfile = pargs.r.ret_str;
101           break;
102
103         case oUseKeyboxd:
104           comopt.use_keyboxd = 1;
105           break;
106
107         case oNoAutostart:
108           comopt.no_autostart = 1;
109           break;
110
111         case oKeyboxdProgram:
112           comopt.keyboxd_program = pargs.r.ret_str;
113           break;
114
115         default:
116           pargs.err = ARGPARSE_PRINT_WARNING;
117           err = gpg_error (GPG_ERR_GENERAL);
118           break;
119         }
120     }
121
122   gpgrt_argparse (NULL, &pargs, NULL);  /* Release internal state.  */
123
124   if (comopt.logfile && !(!strncmp (comopt.logfile, "socket:", 7)
125                           || !strncmp (comopt.logfile, "tcp:", 4)) )
126     {
127       /* Letting all modules write to the same log file is not a good
128        * idea.  Append the module name.  */
129       char *p;
130
131       p = xstrconcat (comopt.logfile, "-", gnupg_module_name (module_id), NULL);
132       xfree (comopt.logfile);
133       comopt.logfile = p;
134     }
135
136   return err;
137 }