Coverage Report

Created: 2025-12-14 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/comopt.c
Line
Count
Source
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
struct gnupg_comopt_s comopt = {NULL};
64
65
66
/* Parse the common options in the homedir and etc.  This needs to be
67
 * called after the gpgrt config directories are set.  MODULE_ID is one of
68
 * the GNUPG_MODULE_NAME_ constants.  If verbose is true info about
69
 * the parsing is printed.  Note that this function is not
70
 * thread-safe. */
71
gpg_error_t
72
parse_comopt (int module_id, int verbose)
73
0
{
74
0
  gpg_error_t err = 0;
75
0
  gpgrt_argparse_t pargs;
76
0
  int argc = 0;
77
0
  char **argv = NULL;
78
79
  /* Reset all options in case we are called a second time.  */
80
0
  xfree (comopt.logfile);
81
0
  xfree (comopt.keyboxd_program);
82
0
  memset (&comopt, 0, sizeof comopt);
83
84
  /* Start the parser.  */
85
0
  pargs.argc = &argc;
86
0
  pargs.argv = &argv;
87
0
  pargs.flags = (ARGPARSE_FLAG_NOVERSION
88
0
                 | ARGPARSE_FLAG_SYS
89
0
                 | ARGPARSE_FLAG_USER
90
0
                 );
91
0
  while (gpgrt_argparser (&pargs, opts, "common" EXTSEP_S "conf" ))
92
0
    {
93
0
      switch (pargs.r_opt)
94
0
        {
95
0
        case ARGPARSE_CONFFILE:
96
0
          if (verbose)
97
0
            log_info (_("reading options from '%s'\n"),
98
0
                      pargs.r_type? pargs.r.ret_str: "[cmdline]");
99
0
          break;
100
101
0
        case oLogFile:
102
0
          comopt.logfile = pargs.r.ret_str;
103
0
          break;
104
105
0
        case oUseKeyboxd:
106
0
          comopt.use_keyboxd = 1;
107
0
          break;
108
109
0
        case oNoAutostart:
110
0
          comopt.no_autostart = 1;
111
0
          break;
112
113
0
        case oKeyboxdProgram:
114
0
          comopt.keyboxd_program = pargs.r.ret_str;
115
0
          break;
116
117
0
        default:
118
0
          pargs.err = ARGPARSE_PRINT_WARNING;
119
0
          err = gpg_error (GPG_ERR_GENERAL);
120
0
          break;
121
0
        }
122
0
    }
123
124
0
  gpgrt_argparse (NULL, &pargs, NULL);  /* Release internal state.  */
125
126
0
  if (comopt.logfile && !(!strncmp (comopt.logfile, "socket:", 7)
127
0
                          || !strncmp (comopt.logfile, "tcp:", 4)) )
128
0
    {
129
      /* Letting all modules write to the same log file is not a good
130
       * idea.  Append the module name.  */
131
0
      char *p;
132
133
0
      p = xstrconcat (comopt.logfile, "-", gnupg_module_name (module_id), NULL);
134
0
      xfree (comopt.logfile);
135
0
      comopt.logfile = p;
136
0
    }
137
138
0
  return err;
139
0
}