libotutil: Establish a place for GPG utilities

Add ot-gpg-utils.[ch] and move _ostree_gpg_error_to_gio_error() here.
This commit is contained in:
Matthew Barnes 2015-04-26 18:08:20 -04:00
parent cd93780d97
commit ceacc57206
10 changed files with 112 additions and 64 deletions

View File

@ -112,9 +112,9 @@ libostree_1_la_SOURCES += \
endif endif
libostree_1_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/bsdiff -I$(srcdir)/libglnx -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree \ libostree_1_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/bsdiff -I$(srcdir)/libglnx -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree \
$(OT_INTERNAL_GIO_UNIX_CFLAGS) $(OT_DEP_LZMA_CFLAGS) $(OT_DEP_ZLIB_CFLAGS) $(OT_INTERNAL_GIO_UNIX_CFLAGS) $(OT_INTERNAL_GPGME_CFLAGS) $(OT_DEP_LZMA_CFLAGS) $(OT_DEP_ZLIB_CFLAGS)
libostree_1_la_LDFLAGS = -version-number 1:0:0 -Bsymbolic-functions -export-symbols-regex '^ostree_' libostree_1_la_LDFLAGS = -version-number 1:0:0 -Bsymbolic-functions -export-symbols-regex '^ostree_'
libostree_1_la_LIBADD = libotutil.la libbupsplit.la libglnx.la libbsdiff.la libostree-kernel-args.la $(OT_INTERNAL_GIO_UNIX_LIBS) $(OT_DEP_LZMA_LIBS) $(OT_DEP_ZLIB_LIBS) $(GPGME_LIBS) libostree_1_la_LIBADD = libotutil.la libbupsplit.la libglnx.la libbsdiff.la libostree-kernel-args.la $(OT_INTERNAL_GIO_UNIX_LIBS) $(OT_INTERNAL_GPGME_LIBS) $(OT_DEP_LZMA_LIBS) $(OT_DEP_ZLIB_LIBS)
if USE_LIBARCHIVE if USE_LIBARCHIVE
libostree_1_la_CFLAGS += $(OT_DEP_LIBARCHIVE_CFLAGS) libostree_1_la_CFLAGS += $(OT_DEP_LIBARCHIVE_CFLAGS)

View File

@ -36,10 +36,12 @@ libotutil_la_SOURCES = \
src/libotutil/ot-variant-utils.h \ src/libotutil/ot-variant-utils.h \
src/libotutil/ot-gio-utils.c \ src/libotutil/ot-gio-utils.c \
src/libotutil/ot-gio-utils.h \ src/libotutil/ot-gio-utils.h \
src/libotutil/ot-gpg-utils.c \
src/libotutil/ot-gpg-utils.h \
src/libotutil/otutil.c \ src/libotutil/otutil.c \
src/libotutil/otutil.h \ src/libotutil/otutil.h \
src/libotutil/ot-tool-util.c \ src/libotutil/ot-tool-util.c \
src/libotutil/ot-tool-util.h \ src/libotutil/ot-tool-util.h \
$(NULL) $(NULL)
libotutil_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libotutil -DLOCALEDIR=\"$(datadir)/locale\" $(OT_INTERNAL_GIO_UNIX_CFLAGS) libotutil_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libotutil -DLOCALEDIR=\"$(datadir)/locale\" $(OT_INTERNAL_GIO_UNIX_CFLAGS) $(OT_INTERNAL_GPGME_CFLAGS)
libotutil_la_LIBADD = $(OT_INTERNAL_GIO_UNIX_LIBS) libotutil_la_LIBADD = $(OT_INTERNAL_GIO_UNIX_LIBS) $(OT_INTERNAL_GPGME_LIBS)

View File

@ -39,6 +39,10 @@ OT_INTERNAL_GIO_UNIX_LIBS = $(OT_DEP_GIO_UNIX_LIBS)
OT_INTERNAL_SOUP_CFLAGS = $(OT_DEP_SOUP_CFLAGS) OT_INTERNAL_SOUP_CFLAGS = $(OT_DEP_SOUP_CFLAGS)
OT_INTERNAL_SOUP_LIBS = $(OT_DEP_SOUP_LIBS) OT_INTERNAL_SOUP_LIBS = $(OT_DEP_SOUP_LIBS)
# This canonicalizes the PKG_CHECK_MODULES or AM_PATH_GPGME results
OT_INTERNAL_GPGME_CFLAGS = $(OT_DEP_GPGME_CFLAGS) $(GPGME_CFLAGS)
OT_INTERNAL_GPGME_LIBS = $(OT_DEP_GPGME_LIBS) $(GPGME_LIBS)
if BUILDOPT_INTROSPECTION if BUILDOPT_INTROSPECTION
include $(INTROSPECTION_MAKEFILE) include $(INTROSPECTION_MAKEFILE)
GIRS = GIRS =

View File

@ -29,7 +29,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <gpgme.h>
typedef struct { typedef struct {
GObjectClass parent_class; GObjectClass parent_class;
@ -187,7 +186,7 @@ override_gpgme_home_dir (gpgme_ctx_t gpg_ctx,
home_dir); home_dir);
if (gpg_error != GPG_ERR_NO_ERROR) if (gpg_error != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (gpg_error, error); ot_gpgme_error_to_gio_error (gpg_error, error);
goto out; goto out;
} }
@ -278,7 +277,7 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
0 /* do not copy */); 0 /* do not copy */);
if (gpg_error != GPG_ERR_NO_ERROR) if (gpg_error != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (gpg_error, error); ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to read signed data: "); g_prefix_error (error, "Unable to read signed data: ");
goto out; goto out;
} }
@ -289,7 +288,7 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
0 /* do not copy */); 0 /* do not copy */);
if (gpg_error != GPG_ERR_NO_ERROR) if (gpg_error != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (gpg_error, error); ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to read signature: "); g_prefix_error (error, "Unable to read signature: ");
goto out; goto out;
} }
@ -297,7 +296,7 @@ _ostree_gpg_verifier_check_signature (OstreeGpgVerifier *self,
gpg_error = gpgme_op_verify (result->context, signature_buffer, data_buffer, NULL); gpg_error = gpgme_op_verify (result->context, signature_buffer, data_buffer, NULL);
if (gpg_error != GPG_ERR_NO_ERROR) if (gpg_error != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (gpg_error, error); ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to complete signature verification: "); g_prefix_error (error, "Unable to complete signature verification: ");
goto out; goto out;
} }

View File

@ -22,7 +22,7 @@
#include "ostree-gpg-verify-result.h" #include "ostree-gpg-verify-result.h"
#include <gpgme.h> #include "otutil.h"
/** /**
* OstreeGpgVerifyResult: * OstreeGpgVerifyResult:
@ -35,8 +35,3 @@ struct OstreeGpgVerifyResult {
gpgme_ctx_t context; gpgme_ctx_t context;
gpgme_verify_result_t details; gpgme_verify_result_t details;
}; };
/* XXX Split these into a separate "GPGME utilities" file? */
void _ostree_gpg_error_to_gio_error (gpgme_error_t gpg_error, GError **error);

View File

@ -27,8 +27,6 @@
#include "ostree-gpg-verify-result-private.h" #include "ostree-gpg-verify-result-private.h"
#include <gpgme.h>
/** /**
* SECTION: libostree-gpg-verify-result * SECTION: libostree-gpg-verify-result
* @title: GPG signature verification results * @title: GPG signature verification results
@ -136,7 +134,7 @@ ostree_gpg_verify_result_initable_init (GInitable *initable,
gpg_error = gpgme_new (&result->context); gpg_error = gpgme_new (&result->context);
if (gpg_error != GPG_ERR_NO_ERROR) if (gpg_error != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (gpg_error, error); ot_gpgme_error_to_gio_error (gpg_error, error);
g_prefix_error (error, "Unable to create context: "); g_prefix_error (error, "Unable to create context: ");
goto out; goto out;
} }
@ -594,37 +592,3 @@ ostree_gpg_verify_result_describe (OstreeGpgVerifyResult *result,
} }
} }
} }
void
_ostree_gpg_error_to_gio_error (gpgme_error_t gpg_error,
GError **error)
{
GIOErrorEnum errcode;
/* XXX This list is incomplete. Add cases as needed. */
switch (gpg_error)
{
/* special case - shouldn't be here */
case GPG_ERR_NO_ERROR:
g_return_if_reached ();
/* special case - abort on out-of-memory */
case GPG_ERR_ENOMEM:
g_error ("%s: %s",
gpgme_strsource (gpg_error),
gpgme_strerror (gpg_error));
case GPG_ERR_INV_VALUE:
errcode = G_IO_ERROR_INVALID_ARGUMENT;
break;
default:
errcode = G_IO_ERROR_FAILED;
break;
}
g_set_error (error, G_IO_ERROR, errcode, "%s: %s",
gpgme_strsource (gpg_error),
gpgme_strerror (gpg_error));
}

View File

@ -35,11 +35,7 @@
#include "ostree-repo-file-enumerator.h" #include "ostree-repo-file-enumerator.h"
#include "ostree-gpg-verifier.h" #include "ostree-gpg-verifier.h"
/* XXX Only for _ostree_gpg_error_to_gio_error(). Move it elsewhere? */
#include "ostree-gpg-verify-result-private.h"
#include <locale.h> #include <locale.h>
#include <gpgme.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
/** /**
@ -3065,7 +3061,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR) if ((err = gpgme_new (&context)) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to create gpg context: "); g_prefix_error (error, "Unable to create gpg context: ");
goto out; goto out;
} }
@ -3075,7 +3071,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_set_protocol (context, GPGME_PROTOCOL_OpenPGP)) != if ((err = gpgme_set_protocol (context, GPGME_PROTOCOL_OpenPGP)) !=
GPG_ERR_NO_ERROR) GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to set gpg protocol: "); g_prefix_error (error, "Unable to set gpg protocol: ");
goto out; goto out;
} }
@ -3085,7 +3081,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir)) if ((err = gpgme_ctx_set_engine_info (context, info->protocol, NULL, homedir))
!= GPG_ERR_NO_ERROR) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to set gpg homedir to '%s': ", g_prefix_error (error, "Unable to set gpg homedir to '%s': ",
homedir); homedir);
goto out; goto out;
@ -3103,7 +3099,7 @@ sign_data (OstreeRepo *self,
} }
else if (err != GPG_ERR_NO_ERROR) else if (err != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Unable to lookup key ID %s: ", key_id); g_prefix_error (error, "Unable to lookup key ID %s: ", key_id);
goto out; goto out;
} }
@ -3111,7 +3107,7 @@ sign_data (OstreeRepo *self,
/* Add the key to the context as a signer */ /* Add the key to the context as a signer */
if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR) if ((err = gpgme_signers_add (context, key)) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Error signing commit: "); g_prefix_error (error, "Error signing commit: ");
goto out; goto out;
} }
@ -3121,7 +3117,7 @@ sign_data (OstreeRepo *self,
const char *buf = g_bytes_get_data (input_data, &len); const char *buf = g_bytes_get_data (input_data, &len);
if ((err = gpgme_data_new_from_mem (&commit_buffer, buf, len, FALSE)) != GPG_ERR_NO_ERROR) if ((err = gpgme_data_new_from_mem (&commit_buffer, buf, len, FALSE)) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Failed to create buffer from commit file: "); g_prefix_error (error, "Failed to create buffer from commit file: ");
goto out; goto out;
} }
@ -3137,7 +3133,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_data_new_from_fd (&signature_buffer, signature_fd)) != GPG_ERR_NO_ERROR) if ((err = gpgme_data_new_from_fd (&signature_buffer, signature_fd)) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Failed to create buffer for signature file: "); g_prefix_error (error, "Failed to create buffer for signature file: ");
goto out; goto out;
} }
@ -3145,7 +3141,7 @@ sign_data (OstreeRepo *self,
if ((err = gpgme_op_sign (context, commit_buffer, signature_buffer, GPGME_SIG_MODE_DETACH)) if ((err = gpgme_op_sign (context, commit_buffer, signature_buffer, GPGME_SIG_MODE_DETACH))
!= GPG_ERR_NO_ERROR) != GPG_ERR_NO_ERROR)
{ {
_ostree_gpg_error_to_gio_error (err, error); ot_gpgme_error_to_gio_error (err, error);
g_prefix_error (error, "Failure signing commit file: "); g_prefix_error (error, "Failure signing commit file: ");
goto out; goto out;
} }

View File

@ -0,0 +1,57 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "ot-gpg-utils.h"
void
ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error,
GError **error)
{
GIOErrorEnum errcode;
/* XXX This list is incomplete. Add cases as needed. */
switch (gpg_error)
{
/* special case - shouldn't be here */
case GPG_ERR_NO_ERROR:
g_return_if_reached ();
/* special case - abort on out-of-memory */
case GPG_ERR_ENOMEM:
g_error ("%s: %s",
gpgme_strsource (gpg_error),
gpgme_strerror (gpg_error));
case GPG_ERR_INV_VALUE:
errcode = G_IO_ERROR_INVALID_ARGUMENT;
break;
default:
errcode = G_IO_ERROR_FAILED;
break;
}
g_set_error (error, G_IO_ERROR, errcode, "%s: %s",
gpgme_strsource (gpg_error),
gpgme_strerror (gpg_error));
}

View File

@ -0,0 +1,30 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include <gpgme.h>
#include <gio/gio.h>
G_BEGIN_DECLS
void ot_gpgme_error_to_gio_error (gpgme_error_t gpg_error, GError **error);
G_END_DECLS

View File

@ -46,5 +46,6 @@
#include <ot-variant-utils.h> #include <ot-variant-utils.h>
#include <ot-spawn-utils.h> #include <ot-spawn-utils.h>
#include <ot-checksum-utils.h> #include <ot-checksum-utils.h>
#include <ot-gpg-utils.h>
void ot_ptrarray_add_many (GPtrArray *a, ...) G_GNUC_NULL_TERMINATED; void ot_ptrarray_add_many (GPtrArray *a, ...) G_GNUC_NULL_TERMINATED;