Imported Upstream version 2.3.1
[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
49     oNoop
50   };
51
52 static gpgrt_opt_t opts[] = {
53   ARGPARSE_s_s (oLogFile,        "log-file", "@"),
54   ARGPARSE_s_n (oUseKeyboxd,     "use-keyboxd", "@"),
55   ARGPARSE_s_s (oKeyboxdProgram, "keyboxd-program", "@"),
56
57   ARGPARSE_end ()
58 };
59
60
61
62 /* Parse the common options in the homedir and etc.  This needs to be
63  * called after the gpgrt config directories are.  MODULE_ID is one of
64  * the GNUPG_MODULE_NAME_ constants.  If verbose is true info about
65  * the parsing is printed.  Note that this function is not
66  * thread-safe. */
67 gpg_error_t
68 parse_comopt (int module_id, int verbose)
69 {
70   gpg_error_t err = 0;
71   gpgrt_argparse_t pargs;
72   int argc = 0;
73   char **argv = NULL;
74
75   /* Reset all options in case we are called a second time.  */
76   xfree (comopt.logfile);
77   xfree (comopt.keyboxd_program);
78   memset (&comopt, 0, sizeof comopt);
79
80   /* Start the parser.  */
81   pargs.argc = &argc;
82   pargs.argv = &argv;
83   pargs.flags = (ARGPARSE_FLAG_NOVERSION
84                  | ARGPARSE_FLAG_SYS
85                  | ARGPARSE_FLAG_USER
86                  );
87   while (gpgrt_argparser (&pargs, opts, "common" EXTSEP_S "conf" ))
88     {
89       switch (pargs.r_opt)
90         {
91         case ARGPARSE_CONFFILE:
92           if (verbose)
93             log_info (_("reading options from '%s'\n"),
94                       pargs.r_type? pargs.r.ret_str: "[cmdline]");
95           break;
96
97         case oLogFile:
98           comopt.logfile = pargs.r.ret_str;
99           break;
100
101         case oUseKeyboxd:
102           comopt.use_keyboxd = 1;
103           break;
104
105         case oKeyboxdProgram:
106           comopt.keyboxd_program = pargs.r.ret_str;
107           break;
108
109         default:
110           pargs.err = ARGPARSE_PRINT_WARNING;
111           err = gpg_error (GPG_ERR_GENERAL);
112           break;
113         }
114     }
115
116   gpgrt_argparse (NULL, &pargs, NULL);  /* Release internal state.  */
117
118   if (comopt.logfile && !(!strncmp (comopt.logfile, "socket:", 7)
119                           || !strncmp (comopt.logfile, "tcp:", 4)) )
120     {
121       /* Letting all modules write to the same log file is not a good
122        * idea.  Append the module name.  */
123       char *p;
124
125       p = xstrconcat (comopt.logfile, "-", gnupg_module_name (module_id), NULL);
126       xfree (comopt.logfile);
127       comopt.logfile = p;
128     }
129
130   return err;
131 }