OpenVAS Libraries  9.0.3
gpgme_util.c File Reference

GPGME utilities. More...

#include <assert.h>
#include <ctype.h>
#include <glib.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include "gpgme_util.h"
Include dependency graph for gpgme_util.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "base gpgme"
 GLib log domain. More...
 

Functions

gpgme_ctx_t openvas_init_gpgme_ctx_from_dir (const gchar *dir)
 Returns a new gpgme context. More...
 
gpgme_ctx_t openvas_init_gpgme_ctx (const gchar *subdir)
 Returns a new gpgme context. More...
 
void set_gpghome (const char *path)
 
gpgme_ctx_t openvas_init_gpgme_sysconf_ctx (void)
 Returns a new gpgme context using the sycconf directory. More...
 

Detailed Description

GPGME utilities.

Definition in file gpgme_util.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "base gpgme"

GLib log domain.

Definition at line 47 of file gpgme_util.c.

Function Documentation

◆ openvas_init_gpgme_ctx()

gpgme_ctx_t openvas_init_gpgme_ctx ( const gchar *  subdir)

Returns a new gpgme context.

Inits a gpgme context with the custom gpghome directory, protocol version etc. Returns the context or NULL if an error occurred. This function also does an gpgme initialization the first time it is called. It is advisable to call this function as early as possible to notice a bad installation (e.g. an too old gpg version).

Parameters
subdirDirectory to use in OPENVAS_STATE_DIR for gpghome, if environment OPENVAS_GPGHOME is not set.
Returns
The gpgme_ctx_t to the context or NULL if an error occurred.

Definition at line 224 of file gpgme_util.c.

225 {
226  char *path;
227  gpgme_ctx_t ctx;
228 
229  path = determine_gpghome (subdir);
230  ctx = openvas_init_gpgme_ctx_from_dir (path);
231  g_free (path);
232  return ctx;
233 }
gpgme_ctx_t openvas_init_gpgme_ctx_from_dir(const gchar *dir)
Returns a new gpgme context.
Definition: gpgme_util.c:102

◆ openvas_init_gpgme_ctx_from_dir()

gpgme_ctx_t openvas_init_gpgme_ctx_from_dir ( const gchar *  dir)

Returns a new gpgme context.

Inits a gpgme context with the custom gpg directory, protocol version etc. Returns the context or NULL if an error occurred. This function also does an gpgme initialization the first time it is called.

Parameters
dirDirectory to use for gpg
Returns
The gpgme_ctx_t to the context or NULL if an error occurred.

Definition at line 102 of file gpgme_util.c.

103 {
104  static int initialized;
105  gpgme_error_t err;
106  gpgme_ctx_t ctx;
107 
108  /* Initialize GPGME the first time we are called. This is a
109  failsafe mode; it would be better to initialize GPGME early at
110  process startup instead of this on-the-fly method; however in
111  this non-threaded system; this is an easier way for a library.
112  We allow to initialize until a valid gpgme or a gpg backend has
113  been found. */
114  if (!initialized)
115  {
116  gpgme_engine_info_t info;
117 
118  if (!gpgme_check_version (NULL))
119  {
120  g_critical ("gpgme library could not be initialized.");
121  return NULL;
122  }
123  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
124 # ifdef LC_MESSAGES
125  gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
126 # endif
127 
128 #ifndef NDEBUG
129  g_message ("Setting GnuPG dir to '%s'", dir);
130 #endif
131  err = 0;
132  if (access (dir, F_OK))
133  {
134  err = gpg_error_from_syserror ();
135 
136  if (errno == ENOENT)
137  /* directory does not exists. try to create it */
138  if (mkdir (dir, 0700) == 0)
139  {
140 #ifndef NDEBUG
141  g_message ("Created GnuPG dir '%s'", dir);
142 #endif
143  err = 0;
144  }
145  }
146 
147  if (!err)
148  err = gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, NULL, dir);
149 
150  if (err)
151  {
152  log_gpgme (G_LOG_LEVEL_WARNING, err, "Setting GnuPG dir failed");
153  return NULL;
154  }
155 
156  /* Show the OpenPGP engine version. */
157  if (!gpgme_get_engine_info (&info))
158  {
159  while (info && info->protocol != GPGME_PROTOCOL_OpenPGP)
160  info = info->next;
161  }
162  else
163  info = NULL;
164 #ifndef NDEBUG
165  g_message ("Using OpenPGP engine version '%s'",
166  info && info->version? info->version: "[?]");
167 #endif
168 
169  /* Everything is fine. */
170  initialized = 1;
171  }
172 
173  /* Allocate the context. */
174  ctx = NULL;
175  err = gpgme_new (&ctx);
176  if (err)
177  log_gpgme (G_LOG_LEVEL_WARNING, err, "Creating GPGME context failed");
178 
179  return ctx;
180 }
#define err(x)

◆ openvas_init_gpgme_sysconf_ctx()

gpgme_ctx_t openvas_init_gpgme_sysconf_ctx ( void  )

Returns a new gpgme context using the sycconf directory.

Inits a gpgme context with the systeconf gpghome directory, protocol version etc. Returns the context or NULL if an error occurred. This function also does an gpgme initialization the first time it is called. It is advisable to call this function (or openvas_init_gpgme_ctx) as early as possible to notice a bad installation (e.g. an too old gpg version).

Returns
The gpgme_ctx_t to the context or NULL if an error occurred.

Definition at line 277 of file gpgme_util.c.

278 {
279  gpgme_ctx_t ctx;
280  char *path;
281 
282  path = get_sysconf_gpghome ();
283  ctx = openvas_init_gpgme_ctx_from_dir (path);
284  g_free (path);
285  return ctx;
286 }
gpgme_ctx_t openvas_init_gpgme_ctx_from_dir(const gchar *dir)
Returns a new gpgme context.
Definition: gpgme_util.c:102

Referenced by nasl_verify_signature().

Here is the caller graph for this function:

◆ set_gpghome()

void set_gpghome ( const char *  path)

Definition at line 236 of file gpgme_util.c.

237 {
238  gpghome = g_strdup (path);
239 }