Browse Source

Add the gpgutil module.

Add a module dedicated to helper functions to work with GpgME.
This module provides a small structure encapsulating both a
stdio FILE object and a GpgME data buffer.
master
Damien Goutte-Gattat 2 years ago
parent
commit
8f00369b4c
  1. 2
      src/Makefile.am
  2. 87
      src/gpgutil.c
  3. 46
      src/gpgutil.h
  4. 57
      src/yki.c

2
src/Makefile.am

@ -1,6 +1,6 @@
bin_PROGRAMS = yki
yki_SOURCES = yki.c yki-encrypt.ui resources.xml
yki_SOURCES = yki.c gpgutil.c gpgutil.h yki-encrypt.ui resources.xml
nodist_yki_SOURCES = resources.c

87
src/gpgutil.c

@ -0,0 +1,87 @@
/*
* Yorkie - A task-oriented GnuPG frontend
* Copyright (C) 2020 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gpgutil.h"
#include <stdio.h>
#include <errno.h>
#include <locale.h>
gpgme_error_t
gpgutil_initialize(gpgme_ctx_t *ctx)
{
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
return gpgme_new(ctx);
}
gpgme_error_t
gpgutil_open_file(const char *name, const char *mode, gpgutil_file_t *file)
{
gpgme_error_t gerr;
gerr = 0;
file->buffer = NULL;
file->file = NULL;
if ( ! (file->file = fopen(name, mode)) )
gerr = gpgme_error_from_errno(errno);
if ( ! gerr )
gerr = gpgme_data_new_from_stream(&file->buffer, file->file);
if ( ! gerr )
gerr = gpgme_data_set_file_name(file->buffer, name);
if ( gerr ) {
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
}
if ( file->file ) {
fclose(file->file);
file->file = NULL;
}
}
return gerr ? gerr : gpgme_error(GPG_ERR_NO_ERROR);
}
gpgme_error_t
gpgutil_close_file(gpgutil_file_t *file)
{
int ret = GPG_ERR_NO_ERROR;
if ( file->buffer ) {
gpgme_data_release(file->buffer);
file->buffer = NULL;
if ( fclose(file->file) )
return gpgme_error_from_errno(errno);
file->buffer = NULL;
}
return gpgme_error(GPG_ERR_NO_ERROR);
}

46
src/gpgutil.h

@ -0,0 +1,46 @@
/*
* Yorkie - A task-oriented GnuPG frontend
* Copyright (C) 2020 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20200502_GPGUTIL_H
#define ICP20200502_GPGUTIL_H
#include <gpgme.h>
typedef struct {
FILE *file;
gpgme_data_t buffer;
} gpgutil_file_t;
#ifdef __cpluscplus
extern "C" {
#endif
gpgme_error_t
gpgutil_initialize(gpgme_ctx_t *);
gpgme_error_t
gpgutil_open_file(const char *, const char *, gpgutil_file_t *);
gpgme_error_t
gpgutil_close_file(gpgutil_file_t *);
#ifdef __cplusplus
}
#endif
#endif /* !ICP20200502_GPGUTIL_H */

57
src/yki.c

@ -27,7 +27,7 @@
#include <glib.h>
#include <gtk/gtk.h>
#include <gpgme.h>
#include "gpgutil.h"
typedef struct {
gpgme_ctx_t gpgme;
@ -41,16 +41,6 @@ typedef struct {
} yorkie_ctx_t;
static gpgme_error_t
initialize_gpgme(gpgme_ctx_t *ctx)
{
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
return gpgme_new(ctx);
}
static gpgme_error_t
populate_public_key_list(gpgme_ctx_t ctx, GtkListStore *key_list)
{
@ -110,38 +100,27 @@ get_selected_keys(yorkie_ctx_t *yki)
}
static gpgme_error_t
perform_operation(yorkie_ctx_t *yki, FILE *in, FILE *out)
perform_operation(yorkie_ctx_t *yki, gpgme_data_t in, gpgme_data_t out)
{
gpgme_data_t gin, gout;
gpgme_error_t gerr;
if ( (gerr = gpgme_data_new_from_stream(&gin, in)) )
return gerr;
if ( (gerr = gpgme_data_new_from_stream(&gout, out)) ) {
gpgme_data_release(gin);
return gerr;
}
if ( yki->symmetric || yki->selected_keys ) {
gpgme_key_t *keys = get_selected_keys(yki);
gpgme_encrypt_flags_t flags = yki->symmetric ? GPGME_ENCRYPT_SYMMETRIC : 0;
if ( yki->sign )
gerr = gpgme_op_encrypt_sign(yki->gpgme, keys, flags, gin, gout);
gerr = gpgme_op_encrypt_sign(yki->gpgme, keys, flags, in, out);
else
gerr = gpgme_op_encrypt(yki->gpgme, keys, flags, gin, gout);
gerr = gpgme_op_encrypt(yki->gpgme, keys, flags, in, out);
g_free(keys);
}
else if ( yki->sign ) {
gpgme_sig_mode_t mode = yki->detached ? GPGME_SIG_MODE_DETACH : GPGME_SIG_MODE_NORMAL;
gerr = gpgme_op_sign(yki->gpgme, gin, gout, mode);
gerr = gpgme_op_sign(yki->gpgme, in, out, mode);
}
gpgme_data_release(gin);
gpgme_data_release(gout);
return gerr;
}
@ -222,25 +201,23 @@ create_encrypt_dialog(yorkie_ctx_t *yki)
static gpgme_error_t
perform_operation_on_file(yorkie_ctx_t *yki, const char *filename)
{
FILE *in, *out;
gchar *outname;
gpgutil_file_t in, out;
gpgme_error_t gerr;
gchar *outname;
if ( ! (in = fopen(filename, "r")) )
return gpgme_error_from_errno(errno);
gerr = gpgutil_open_file(filename, "r", &in);
outname = g_strdup_printf("%s.gpg", filename);
out = fopen(outname, "w");
g_free(outname);
if ( ! out ) {
fclose(in);
return gpgme_error_from_errno(errno);
if ( ! gerr ) {
outname = g_strdup_printf("%s.gpg", filename);
gerr = gpgutil_open_file(outname, "w", &out);
g_free(outname);
}
gerr = perform_operation(yki, in, out);
if ( ! gerr )
gerr = perform_operation(yki, in.buffer, out.buffer);
fclose(in);
fclose(out);
gpgutil_close_file(&in);
gpgutil_close_file(&out);
return gerr;
}
@ -262,7 +239,7 @@ main(int argc, char **argv)
dlg = create_encrypt_dialog(&yki);
if ( (gerr = initialize_gpgme(&yki.gpgme)) )
if ( (gerr = gpgutil_initialize(&yki.gpgme)) )
g_error("Cannot initialize GPGME: %s", gpgme_strerror(gerr));
if ( (gerr = populate_public_key_list(yki.gpgme, yki.public_keys)) )

Loading…
Cancel
Save