gpgme/gpgme/version.c
Marcus Brinkmann 1f5180f4b0 2002-02-02 Marcus Brinkmann <marcus@g10code.de>
This patch has gotten a bit large... mmh.  The main thing that
	happens here is that error values are now not determined in the
	operation function after gpgme_wait completed, but in the status
	handler when EOF is received.  It should always be the case that
	either an error is flagged or EOF is received, so that after a
	gpgme_wait you should never have the situation that no error is
	flagged and EOF is not received.  One problem is that the engine
	status handlers don't have access to the context, a horrible
	kludge works around this for now.  All errors that happen during a
	pending operation should be catched and reported in ctx->error,
	including out-of-core and cancellation.  This rounds up neatly a
	couple of loose ends, and makes it possible to pass up any errors
	in the communication with the backend as well.  As a bonus, there
	will be a function to access gpgme->wait, so that the operations
	can truly be implemented with their _start function.

	* engine-gpgsm.c (gpgsm_status_handler): Horrible kludge to report
	error back to the context.
	* rungpg.c (gpg_status_handler): Same horrible kludge applied here.

	* engine-gpgsm.c (gpgsm_assuan_simple_command): Add error checking.

	* wait.c (_gpgme_wait_on_condition): If canceled, set CTX->error
	to a value indication that.

	* verify.c (add_notation): Set error, not out_of_core.
	(finish_sig): Likewise.
	(gpgme_op_verify_start): Don't clear out_of_core.
	(_gpgme_verify_status_handler): At EOF, clean up the notation data.
	(gpgme_op_verify): And don't do it here.

	* trustlist.c (trustlist_status_handler): Check error, not out_of_core.
	(gpgme_op_trustlist_start): Don't clear out_of_core.
	(gpgme_op_trustlist_next): Check error, not out_of_core.
	(gpgme_op_trustlist_end): Likewise.

	* ops.h (test_and_allocate_result): New macro.
	(_gpgme_passphrase_result): Remove prototype.
	* delete.c (gpgme_op_delete): Return error from context.
	(delete_status_handler): Use macro test_and_allocate_result.
	Perform error checking at EOF.
	(gpgme_op_delete_start): Release result.
	* passphrase.c (_gpgme_passphrase_status_handler): Use macro
	test_and_allocate_result, and perform error checking here.
	(_gpgme_passphrase_result): Function removed.
	* sign.c (gpgme_op_sign_start): Do not set out_of_core to zero.
	(gpgme_op_sign): Just return the error value from the context.
	(sign_status_handler): Only progress if no error is set yet.  If
	we process an EOF, set the resulting error value (if any).
	* decrypt.c (_gpgme_decrypt_result): Function removed.
	(create_result_struct): Function removed.
	(_gpgme_decrypt_status_handler): Use macro test_and_allocate_result,
	caclulate error on EOF, do not progress with errors.
	(_gpgme_decrypt_start): Do not set out_of_core to zero.
	(gpgme_op_decrypt): Just return the error value from the context.
	* encrypt.c (encrypt_status_handler): Perform the error checking
	here.
	(gpgme_op_encrypt_start): Do not clear out_of_core.
	* export.c (export_status_handler): Return if error is set in context.
	(gpgme_op_export_start): Release result.
	(gpgme_op_export): Return error from context.
	* decrypt-verify.c (gpgme_op_decrypt_verify): Return the error in
	the context.
	* genkey.c (genkey_status_handler): Use macro
	test_and_allocate_result.  Perform error checking at EOF.
	(gpgme_op_genkey): Just return the error from context.
	* import.c (gpgme_op_import): Return the error from context.
	(import_status_handler): Use macro test_and_allocate_result.
	* keylist.c (gpgme_op_keylist_start): Do not clear out_of_core.
	(gpgme_op_keylist_next): Return error of context.
	(keylist_colon_handler): Set error instead out_of_code.
	(finish_key): Likewise.

	* context.h: Remove member out_of_core, add member error.
	* gpgme.c (_gpgme_release_result): Clear error flag.

	* engine.h (_gpgme_engine_get_error): New prototype.
	* engine.c (_gpgme_engine_get_error): New function.
	* engine-gpgsm.c (_gpgme_gpgsm_get_error): New function.

	* engine-gpgsm.c (map_assuan_error): New function.
	(gpgsm_assuan_simple_command): Change return type to GpgmeError,
	use the new function to map error values.
	(gpgsm_set_fd): Change return type tp GpgmeError.
	(_gpgme_gpgsm_op_decrypt): Change type of ERR to GpgmeError.
	(gpgsm_set_recipients): Likewise.  Change type of return value
	equivalently.  Adjust error values.
	(_gpgme_gpgsm_op_import): Likewise.
	(_gpgme_gpgsm_op_sign): Likewise.
	(struct gpgsm_object_s): New member error.
	(gpgsm_status_handler): Set error if error occurs.  Determine
	error number from ERR line received.  If assuan_read_line fails,
	terminate the connection.
2002-02-02 03:52:59 +00:00

270 lines
6.7 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* version.c - version check
* Copyright (C) 2000 Werner Koch (dd9jn)
* Copyright (C) 2001 g10 Code GmbH
*
* This file is part of GPGME.
*
* GPGME 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 2 of the License, or
* (at your option) any later version.
*
* GPGME 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gpgme.h"
#include "context.h"
#include "rungpg.h"
#include "sema.h"
#include "util.h"
#include "key.h" /* for key_cache_init */
#include "io.h"
static void
do_subsystem_inits (void)
{
static int done = 0;
if (done)
return;
_gpgme_sema_subsystem_init ();
_gpgme_key_cache_init ();
}
static const char*
parse_version_number (const char *s, int *number)
{
int val = 0;
if (*s == '0' && isdigit(s[1]))
return NULL; /* Leading zeros are not allowed. */
for (; isdigit(*s); s++)
{
val *= 10;
val += *s - '0';
}
*number = val;
return val < 0 ? NULL : s;
}
static const char *
parse_version_string (const char *s, int *major, int *minor, int *micro)
{
s = parse_version_number (s, major);
if (!s || *s != '.')
return NULL;
s++;
s = parse_version_number (s, minor);
if (!s || *s != '.')
return NULL;
s++;
s = parse_version_number (s, micro);
if (!s)
return NULL;
return s; /* Patchlevel. */
}
const char *
_gpgme_compare_versions (const char *my_version,
const char *req_version)
{
int my_major, my_minor, my_micro;
int rq_major, rq_minor, rq_micro;
const char *my_plvl, *rq_plvl;
if (!req_version)
return my_version;
if (!my_version)
return NULL;
my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
if (!my_plvl)
return NULL; /* Very strange: our own version is bogus. */
rq_plvl = parse_version_string(req_version,
&rq_major, &rq_minor, &rq_micro);
if (!rq_plvl)
return NULL; /* Requested version string is invalid. */
if (my_major > rq_major
|| (my_major == rq_major && my_minor > rq_minor)
|| (my_major == rq_major && my_minor == rq_minor
&& my_micro > rq_micro)
|| (my_major == rq_major && my_minor == rq_minor
&& my_micro == rq_micro
&& strcmp( my_plvl, rq_plvl ) >= 0))
{
return my_version;
}
return NULL;
}
/**
* gpgme_check_version:
* @req_version: A string with a version
*
* Check that the the version of the library is at minimum the requested one
* and return the version string; return NULL if the condition is not
* met. If a NULL is passed to this function, no check is done and
* the version string is simply returned. It is a pretty good idea to
* run this function as soon as possible, because it also intializes
* some subsystems. In a multithreaded environment if should be called
* before the first thread is created.
*
* Return value: The version string or NULL
**/
const char *
gpgme_check_version (const char *req_version)
{
do_subsystem_inits ();
return _gpgme_compare_versions (VERSION, req_version);
}
/**
* gpgme_get_engine_info:
*
* Return information about the underlying crypto engines. This is an
* XML string with various information. A string is always returned
* even if the crypto engines is not installed; in this case a XML
* string with some error information is returned.
*
* Return value: A XML string with information about the crypto
* engines.
**/
const char *
gpgme_get_engine_info ()
{
static const char *engine_info;
const char *openpgp_info = _gpgme_engine_get_info (GPGME_PROTOCOL_OpenPGP);
const char *cms_info = _gpgme_engine_get_info (GPGME_PROTOCOL_CMS);
char *info;
/* FIXME: Make sure that only one instance does run. */
if (engine_info)
return engine_info;
if (!openpgp_info && !cms_info)
info = "<EngineInfo>\n</EngineInfo>\n";
else if (!openpgp_info || !cms_info)
{
const char *fmt = "<EngineInfo>\n"
"%s"
"</EngineInfo>\n";
info = xtrymalloc (strlen(fmt) + strlen(openpgp_info
? openpgp_info : cms_info) + 1);
if (info)
sprintf (info, fmt, openpgp_info ? openpgp_info : cms_info);
}
else
{
const char *fmt = "<EngineInfo>\n"
"%s%s"
"</EngineInfo>\n";
info = xtrymalloc (strlen(fmt) + strlen(openpgp_info)
+ strlen (cms_info) + 1);
if (info)
sprintf (info, fmt, openpgp_info, cms_info);
}
if (!info)
info = "<EngineInfo>\n"
" <error>Out of core</error>\n"
"</EngineInfo>\n";
engine_info = info;
return engine_info;
}
/**
* gpgme_check_engine:
*
* Check whether the installed crypto engine for the OpenPGP protocol
* matches the requirement of GPGME. This function is deprecated,
* instead use gpgme_engine_get_info() with the specific protocol you
* need.
*
* Return value: 0 or an error code.
**/
GpgmeError
gpgme_check_engine ()
{
return gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
}
#define LINELENGTH 80
char *
_gpgme_get_program_version (const char *const path)
{
char line[LINELENGTH] = "";
int linelen = 0;
char *mark = NULL;
int rp[2];
pid_t pid;
int nread;
char *argv[] = {(char *) path, "--version", 0};
struct spawn_fd_item_s pfd[] = { {0, -1}, {-1, -1} };
struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */}, {-1, -1} };
int status, signal;
if (!path)
return NULL;
if (_gpgme_io_pipe (rp, 1) < 0)
return NULL;
pfd[0].fd = rp[1];
cfd[0].fd = rp[1];
pid = _gpgme_io_spawn (path, argv, cfd, pfd);
if (pid < 0)
{
_gpgme_io_close (rp[0]);
_gpgme_io_close (rp[1]);
return NULL;
}
do
{
nread = _gpgme_io_read (rp[0], &line[linelen], LINELENGTH - linelen - 1);
if (nread > 0)
{
line[linelen + nread] = '\0';
mark = strchr (&line[linelen], '\n');
if (mark)
{
*mark = '\0';
break;
}
linelen += nread;
}
}
while (nread > 0 && linelen < LINELENGTH - 1);
_gpgme_io_close (rp[0]);
_gpgme_io_waitpid (pid, 1, &status, &signal);
if (mark)
{
mark = strrchr (line, ' ');
if (!mark)
return NULL;
return xtrystrdup (mark + 1);
}
return NULL;
}