2000-12-06 12:17:10 +00:00
|
|
|
|
/* version.c - version check
|
|
|
|
|
* Copyright (C) 2000 Werner Koch (dd9jn)
|
2001-04-02 08:40:32 +00:00
|
|
|
|
* Copyright (C) 2001 g10 Code GmbH
|
2000-12-06 12:17:10 +00:00
|
|
|
|
*
|
|
|
|
|
* 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>
|
2001-01-22 20:22:41 +00:00
|
|
|
|
#include <string.h>
|
2000-12-06 12:17:10 +00:00
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
#include "gpgme.h"
|
2001-01-22 20:22:41 +00:00
|
|
|
|
#include "context.h"
|
|
|
|
|
#include "rungpg.h"
|
2001-01-30 11:01:41 +00:00
|
|
|
|
#include "sema.h"
|
2001-01-22 20:22:41 +00:00
|
|
|
|
#include "util.h"
|
2001-05-28 17:35:10 +00:00
|
|
|
|
#include "key.h" /* for key_cache_init */
|
2001-11-20 04:02:38 +00:00
|
|
|
|
#include "io.h"
|
2001-01-22 20:22:41 +00:00
|
|
|
|
|
|
|
|
|
static const char *get_engine_info (void);
|
|
|
|
|
|
2000-12-06 12:17:10 +00:00
|
|
|
|
|
2001-01-30 11:01:41 +00:00
|
|
|
|
static void
|
|
|
|
|
do_subsystem_inits (void)
|
|
|
|
|
{
|
|
|
|
|
static int done = 0;
|
|
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
|
return;
|
|
|
|
|
_gpgme_sema_subsystem_init ();
|
2001-05-28 17:35:10 +00:00
|
|
|
|
_gpgme_key_cache_init ();
|
2001-01-30 11:01:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-12-06 12:17:10 +00:00
|
|
|
|
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 */
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-22 20:22:41 +00:00
|
|
|
|
static const char *
|
|
|
|
|
compare_versions ( const char *my_version, const char *req_version )
|
2000-12-06 12:17:10 +00:00
|
|
|
|
{
|
|
|
|
|
int my_major, my_minor, my_micro;
|
|
|
|
|
int rq_major, rq_minor, rq_micro;
|
|
|
|
|
const char *my_plvl, *rq_plvl;
|
|
|
|
|
|
|
|
|
|
if ( !req_version )
|
2001-01-22 20:22:41 +00:00
|
|
|
|
return my_version;
|
2000-12-06 12:17:10 +00:00
|
|
|
|
|
2001-01-22 20:22:41 +00:00
|
|
|
|
my_plvl = parse_version_string ( my_version,
|
|
|
|
|
&my_major, &my_minor, &my_micro );
|
2000-12-06 12:17:10 +00:00
|
|
|
|
if ( !my_plvl )
|
|
|
|
|
return NULL; /* very strange: our own version is bogus */
|
2001-01-22 20:22:41 +00:00
|
|
|
|
rq_plvl = parse_version_string( req_version,
|
|
|
|
|
&rq_major, &rq_minor, &rq_micro );
|
2000-12-06 12:17:10 +00:00
|
|
|
|
if ( !rq_plvl )
|
|
|
|
|
return NULL; /* req version string is invalid */
|
|
|
|
|
|
|
|
|
|
if ( my_major > rq_major
|
|
|
|
|
|| (my_major == rq_major && my_minor > rq_minor)
|
2001-05-28 17:35:10 +00:00
|
|
|
|
|| (my_major == rq_major && my_minor == rq_minor
|
2000-12-06 12:17:10 +00:00
|
|
|
|
&& my_micro > rq_micro)
|
|
|
|
|
|| (my_major == rq_major && my_minor == rq_minor
|
|
|
|
|
&& my_micro == rq_micro
|
|
|
|
|
&& strcmp( my_plvl, rq_plvl ) >= 0) ) {
|
2001-01-22 20:22:41 +00:00
|
|
|
|
return my_version;
|
2000-12-06 12:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-01-22 20:22:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
2001-01-30 11:01:41 +00:00
|
|
|
|
* the version string is simply returned. It is a pretty good idea to
|
2001-07-31 15:21:58 +00:00
|
|
|
|
* run this function as soon as possible, because it also intializes
|
2001-01-30 11:01:41 +00:00
|
|
|
|
* some subsystems. In a multithreaded environment if should be called
|
|
|
|
|
* before the first thread is created.
|
2001-01-22 20:22:41 +00:00
|
|
|
|
*
|
|
|
|
|
* Return value: The version string or NULL
|
|
|
|
|
**/
|
|
|
|
|
const char *
|
|
|
|
|
gpgme_check_version ( const char *req_version )
|
|
|
|
|
{
|
2001-01-30 11:01:41 +00:00
|
|
|
|
do_subsystem_inits ();
|
2001-01-22 20:22:41 +00:00
|
|
|
|
return compare_versions ( VERSION, req_version );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gpgme_get_engine_info:
|
|
|
|
|
*
|
|
|
|
|
* Return information about the underlying crypto engine. This is an
|
|
|
|
|
* XML string with various information. To get the version of the
|
|
|
|
|
* crypto engine it should be sufficient to grep for the first
|
|
|
|
|
* <literal>version</literal> tag and use it's content. A string is
|
|
|
|
|
* always returned even if the crypto engine 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 engine.
|
|
|
|
|
**/
|
|
|
|
|
const char *
|
|
|
|
|
gpgme_get_engine_info ()
|
|
|
|
|
{
|
2001-01-30 11:01:41 +00:00
|
|
|
|
do_subsystem_inits ();
|
2001-01-22 20:22:41 +00:00
|
|
|
|
return get_engine_info ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gpgme_check_engine:
|
|
|
|
|
*
|
|
|
|
|
* Check whether the installed crypto engine matches the requirement of
|
|
|
|
|
* GPGME.
|
|
|
|
|
*
|
|
|
|
|
* Return value: 0 or an error code.
|
|
|
|
|
**/
|
|
|
|
|
GpgmeError
|
|
|
|
|
gpgme_check_engine ()
|
|
|
|
|
{
|
|
|
|
|
const char *info = gpgme_get_engine_info ();
|
|
|
|
|
const char *s, *s2;
|
|
|
|
|
|
|
|
|
|
s = strstr (info, "<version>");
|
|
|
|
|
if (s) {
|
|
|
|
|
s += 9;
|
2001-05-28 17:35:10 +00:00
|
|
|
|
s2 = strchr (s, '<');
|
2001-01-22 20:22:41 +00:00
|
|
|
|
if (s2) {
|
|
|
|
|
char *ver = xtrymalloc (s2 - s + 1);
|
|
|
|
|
if (!ver)
|
|
|
|
|
return mk_error (Out_Of_Core);
|
|
|
|
|
memcpy (ver, s, s2-s);
|
|
|
|
|
ver[s2-s] = 0;
|
|
|
|
|
s = compare_versions ( ver, NEED_GPG_VERSION );
|
|
|
|
|
xfree (ver);
|
|
|
|
|
if (s)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mk_error (Invalid_Engine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-20 04:02:38 +00:00
|
|
|
|
#define LINELENGTH 80
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
_gpgme_get_program_version (const char *const path)
|
2001-01-22 20:22:41 +00:00
|
|
|
|
{
|
2001-11-20 04:02:38 +00:00
|
|
|
|
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[] = { {0, -1}, {-1, 1 /* STDOUT_FILENO */},
|
|
|
|
|
{-1, -1} };
|
|
|
|
|
int status, signal;
|
|
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
|
return NULL;
|
2001-01-22 20:22:41 +00:00
|
|
|
|
|
2001-11-20 04:02:38 +00:00
|
|
|
|
if (_gpgme_io_pipe (rp, 1) < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
pfd[0].fd = rp[1];
|
|
|
|
|
cfd[0].fd = rp[0];
|
|
|
|
|
cfd[1].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;
|
2001-01-22 20:22:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 04:02:38 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2001-01-22 20:22:41 +00:00
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
|
get_engine_info (void)
|
|
|
|
|
{
|
2001-01-31 12:39:53 +00:00
|
|
|
|
static const char *engine_info =NULL;
|
2001-01-22 20:22:41 +00:00
|
|
|
|
GpgmeError err = 0;
|
2001-01-30 17:36:03 +00:00
|
|
|
|
const char *path = NULL;
|
2001-11-20 04:02:38 +00:00
|
|
|
|
char *version;
|
2001-01-22 20:22:41 +00:00
|
|
|
|
|
|
|
|
|
/* FIXME: make sure that only one instance does run */
|
|
|
|
|
if (engine_info)
|
2001-01-31 12:39:53 +00:00
|
|
|
|
return engine_info;
|
2001-01-22 20:22:41 +00:00
|
|
|
|
|
2001-01-30 17:36:03 +00:00
|
|
|
|
path = _gpgme_get_gpg_path ();
|
2001-11-18 03:31:31 +00:00
|
|
|
|
if (!path)
|
|
|
|
|
{
|
|
|
|
|
engine_info = "<GnupgInfo>\n"
|
|
|
|
|
" <error>Not supported</error>\n"
|
|
|
|
|
"</GnupgInfo>\n";
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-11-20 04:02:38 +00:00
|
|
|
|
version = _gpgme_get_program_version (path);
|
|
|
|
|
|
|
|
|
|
if (version) {
|
2001-01-22 20:22:41 +00:00
|
|
|
|
const char *fmt;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
fmt = "<GnupgInfo>\n"
|
|
|
|
|
" <engine>\n"
|
|
|
|
|
" <version>%s</version>\n"
|
2001-01-30 17:36:03 +00:00
|
|
|
|
" <path>%s</path>\n"
|
2001-01-22 20:22:41 +00:00
|
|
|
|
" </engine>\n"
|
|
|
|
|
"</GnupgInfo>\n";
|
|
|
|
|
/*(yes, I know that we allocating 2 extra bytes)*/
|
2001-01-30 17:36:03 +00:00
|
|
|
|
p = xtrymalloc ( strlen(fmt) + strlen(path)
|
2001-11-20 04:02:38 +00:00
|
|
|
|
+ strlen (version) + 1);
|
2001-01-22 20:22:41 +00:00
|
|
|
|
if (!p) {
|
|
|
|
|
err = mk_error (Out_Of_Core);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-11-20 04:02:38 +00:00
|
|
|
|
sprintf (p, fmt, version, path);
|
2001-01-22 20:22:41 +00:00
|
|
|
|
engine_info = p;
|
2001-11-20 04:02:38 +00:00
|
|
|
|
xfree (version);
|
2001-01-22 20:22:41 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
err = mk_error (General_Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
2001-01-30 17:36:03 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
const char *fmt;
|
|
|
|
|
const char *errstr = gpgme_strerror (err);
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
fmt = "<GnupgInfo>\n"
|
|
|
|
|
" <engine>\n"
|
|
|
|
|
" <error>%s</error>\n"
|
|
|
|
|
" <path>%s</path>\n"
|
|
|
|
|
" </engine>\n"
|
|
|
|
|
"</GnupgInfo>\n";
|
|
|
|
|
|
|
|
|
|
p = xtrymalloc ( strlen(fmt) + strlen(errstr) + strlen(path) + 1);
|
|
|
|
|
if (p) {
|
|
|
|
|
sprintf (p, fmt, errstr, path);
|
|
|
|
|
engine_info = p;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
engine_info = "<GnupgInfo>\n"
|
|
|
|
|
" <error>Out of core</error>\n"
|
|
|
|
|
"</GnupgInfo>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-01-22 20:22:41 +00:00
|
|
|
|
return engine_info;
|
|
|
|
|
}
|