2003-08-05 17:11:04 +00:00
|
|
|
|
/* command.c - gpg-agent command handler
|
2007-01-25 08:30:47 +00:00
|
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005,
|
2008-02-14 19:50:10 +00:00
|
|
|
|
* 2006, 2008 Free Software Foundation, Inc.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 17:11:04 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG 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
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* FIXME: we should not use the default assuan buffering but setup
|
|
|
|
|
some buffering in secure mempory to protect session keys etc. */
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2005-02-23 21:06:32 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <unistd.h>
|
2005-02-23 21:06:32 +00:00
|
|
|
|
#include <assert.h>
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
#include <assuan.h>
|
|
|
|
|
|
|
|
|
|
#include "agent.h"
|
|
|
|
|
|
|
|
|
|
/* maximum allowed size of the inquired ciphertext */
|
|
|
|
|
#define MAXLEN_CIPHERTEXT 4096
|
|
|
|
|
/* maximum allowed size of the key parameters */
|
|
|
|
|
#define MAXLEN_KEYPARAM 1024
|
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
#define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if MAX_DIGEST_LEN < 20
|
|
|
|
|
#error MAX_DIGEST_LEN shorter than keygrip
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Data used to associate an Assuan context with local server data */
|
2006-09-04 08:07:12 +00:00
|
|
|
|
struct server_local_s
|
|
|
|
|
{
|
|
|
|
|
assuan_context_t assuan_ctx;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int message_fd;
|
|
|
|
|
int use_cache_for_signing;
|
2004-12-20 08:32:56 +00:00
|
|
|
|
char *keydesc; /* Allocated description for the next key
|
2004-02-13 17:06:34 +00:00
|
|
|
|
operation. */
|
2006-11-14 16:40:44 +00:00
|
|
|
|
int pause_io_logging; /* Used to suppress I/O logging during a command */
|
2007-06-18 10:33:12 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
int stopme; /* If set to true the agent will be terminated after
|
|
|
|
|
the end of this session. */
|
|
|
|
|
#endif
|
2008-02-14 19:50:10 +00:00
|
|
|
|
int allow_pinentry_notify; /* Set if pinentry notifications should
|
|
|
|
|
be done. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 08:07:12 +00:00
|
|
|
|
/* An entry for the getval/putval commands. */
|
|
|
|
|
struct putval_item_s
|
|
|
|
|
{
|
|
|
|
|
struct putval_item_s *next;
|
|
|
|
|
size_t off; /* Offset to the value into DATA. */
|
|
|
|
|
size_t len; /* Length of the value. */
|
|
|
|
|
char d[1]; /* Key | Nul | value. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* A list of key value pairs fpr the getval/putval commands. */
|
|
|
|
|
static struct putval_item_s *putval_list;
|
|
|
|
|
|
|
|
|
|
|
2006-11-14 14:53:42 +00:00
|
|
|
|
|
|
|
|
|
/* To help polling clients, we keep tarck of the number of certain
|
|
|
|
|
events. This structure keeps those counters. The counters are
|
|
|
|
|
integers and there should be no problem if they are overflowing as
|
|
|
|
|
callers need to check only whether a counter changed. The actual
|
|
|
|
|
values are not meaningful. */
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
/* Incremented if any of the other counters below changed. */
|
|
|
|
|
unsigned int any;
|
|
|
|
|
|
|
|
|
|
/* Incremented if a key is added or removed from the internal privat
|
|
|
|
|
key database. */
|
|
|
|
|
unsigned int key;
|
|
|
|
|
|
|
|
|
|
/* Incremented if a change of the card readers stati has been
|
|
|
|
|
detected. */
|
|
|
|
|
unsigned int card;
|
|
|
|
|
|
|
|
|
|
} eventcounter;
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-20 08:32:56 +00:00
|
|
|
|
/* Release the memory buffer MB but first wipe out the used memory. */
|
|
|
|
|
static void
|
|
|
|
|
clear_outbuf (membuf_t *mb)
|
|
|
|
|
{
|
|
|
|
|
void *p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
p = get_membuf (mb, &n);
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
memset (p, 0, n);
|
|
|
|
|
xfree (p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Write the content of memory buffer MB as assuan data to CTX and
|
|
|
|
|
wipe the buffer out afterwards. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
write_and_clear_outbuf (assuan_context_t ctx, membuf_t *mb)
|
|
|
|
|
{
|
|
|
|
|
assuan_error_t ae;
|
|
|
|
|
void *p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
p = get_membuf (mb, &n);
|
|
|
|
|
if (!p)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2004-12-20 08:32:56 +00:00
|
|
|
|
ae = assuan_send_data (ctx, p, n);
|
|
|
|
|
memset (p, 0, n);
|
|
|
|
|
xfree (p);
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return ae;
|
2004-12-20 08:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
static void
|
2006-09-06 16:35:52 +00:00
|
|
|
|
reset_notify (assuan_context_t ctx)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
memset (ctrl->keygrip, 0, 20);
|
|
|
|
|
ctrl->have_keygrip = 0;
|
|
|
|
|
ctrl->digest.valuelen = 0;
|
2004-02-13 17:06:34 +00:00
|
|
|
|
|
|
|
|
|
xfree (ctrl->server_local->keydesc);
|
|
|
|
|
ctrl->server_local->keydesc = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Check whether the option NAME appears in LINE */
|
|
|
|
|
static int
|
|
|
|
|
has_option (const char *line, const char *name)
|
|
|
|
|
{
|
|
|
|
|
const char *s;
|
|
|
|
|
int n = strlen (name);
|
|
|
|
|
|
|
|
|
|
s = strstr (line, name);
|
|
|
|
|
return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-06 10:58:18 +00:00
|
|
|
|
/* Same as has_option but does only test for the name of the option
|
|
|
|
|
and ignores an argument, i.e. with NAME being "--hash" it would
|
|
|
|
|
return true for "--hash" as well as for "--hash=foo". */
|
|
|
|
|
static int
|
|
|
|
|
has_option_name (const char *line, const char *name)
|
|
|
|
|
{
|
|
|
|
|
const char *s;
|
|
|
|
|
int n = strlen (name);
|
|
|
|
|
|
|
|
|
|
s = strstr (line, name);
|
|
|
|
|
return (s && (s == line || spacep (s-1))
|
|
|
|
|
&& (!s[n] || spacep (s+n) || s[n] == '='));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-04 16:45:04 +00:00
|
|
|
|
/* Skip over options. It is assumed that leading spaces have been
|
|
|
|
|
removed (this is the case for lines passed to a handler from
|
2006-11-14 10:23:21 +00:00
|
|
|
|
assuan). Blanks after the options are also removed. */
|
2006-10-04 16:45:04 +00:00
|
|
|
|
static char *
|
|
|
|
|
skip_options (char *line)
|
|
|
|
|
{
|
|
|
|
|
while ( *line == '-' && line[1] == '-' )
|
|
|
|
|
{
|
|
|
|
|
while (*line && !spacep (line))
|
|
|
|
|
line++;
|
|
|
|
|
while (spacep (line))
|
|
|
|
|
line++;
|
|
|
|
|
}
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-13 17:06:34 +00:00
|
|
|
|
/* Replace all '+' by a blank. */
|
|
|
|
|
static void
|
|
|
|
|
plus_to_blank (char *s)
|
|
|
|
|
{
|
|
|
|
|
for (; *s; s++)
|
|
|
|
|
{
|
|
|
|
|
if (*s == '+')
|
|
|
|
|
*s = ' ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 08:07:12 +00:00
|
|
|
|
/* Do the percent and plus/space unescaping in place and return the
|
|
|
|
|
length of the valid buffer. */
|
|
|
|
|
static size_t
|
|
|
|
|
percent_plus_unescape (char *string)
|
|
|
|
|
{
|
2006-09-15 18:53:37 +00:00
|
|
|
|
unsigned char *p = (unsigned char *)string;
|
2006-09-04 08:07:12 +00:00
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
|
|
while (*string)
|
|
|
|
|
{
|
|
|
|
|
if (*string == '%' && string[1] && string[2])
|
|
|
|
|
{
|
|
|
|
|
string++;
|
|
|
|
|
*p++ = xtoi_2 (string);
|
|
|
|
|
n++;
|
|
|
|
|
string+= 2;
|
|
|
|
|
}
|
|
|
|
|
else if (*string == '+')
|
|
|
|
|
{
|
|
|
|
|
*p++ = ' ';
|
|
|
|
|
n++;
|
|
|
|
|
string++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*p++ = *string++;
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
/* Parse a hex string. Return an Assuan error code or 0 on success and the
|
|
|
|
|
length of the parsed string in LEN. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
parse_hexstring (assuan_context_t ctx, const char *string, size_t *len)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
const char *p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
/* parse the hash value */
|
|
|
|
|
for (p=string, n=0; hexdigitp (p); p++, n++)
|
|
|
|
|
;
|
2004-12-21 19:05:15 +00:00
|
|
|
|
if (*p != ' ' && *p != '\t' && *p)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if ((n&1))
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "odd number of digits");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*len = n;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Parse the keygrip in STRING into the provided buffer BUF. BUF must
|
2006-10-11 17:52:15 +00:00
|
|
|
|
provide space for 20 bytes. BUF is not changed if the function
|
2003-08-05 17:11:04 +00:00
|
|
|
|
returns an error. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
parse_keygrip (assuan_context_t ctx, const char *string, unsigned char *buf)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
rc = parse_hexstring (ctx, string, &n);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
n /= 2;
|
|
|
|
|
if (n != 20)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of keygrip");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2008-09-03 09:37:32 +00:00
|
|
|
|
if (hex2bin (string, buf, 20) < 0)
|
|
|
|
|
return set_error (GPG_ERR_BUG, "hex2bin");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-25 18:29:20 +00:00
|
|
|
|
/* Write an assuan status line. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
agent_write_status (ctrl_t ctrl, const char *keyword, ...)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
const char *text;
|
|
|
|
|
assuan_context_t ctx = ctrl->server_local->assuan_ctx;
|
|
|
|
|
char buf[950], *p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
va_start (arg_ptr, keyword);
|
|
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
|
n = 0;
|
|
|
|
|
while ( (text = va_arg (arg_ptr, const char *)) )
|
|
|
|
|
{
|
|
|
|
|
if (n)
|
|
|
|
|
{
|
|
|
|
|
*p++ = ' ';
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
for ( ; *text && n < DIM (buf)-2; n++)
|
|
|
|
|
*p++ = *text++;
|
|
|
|
|
}
|
|
|
|
|
*p = 0;
|
|
|
|
|
err = assuan_write_status (ctx, keyword, buf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2006-09-25 18:29:20 +00:00
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2006-09-25 18:29:20 +00:00
|
|
|
|
|
2008-03-20 15:31:43 +00:00
|
|
|
|
/* Helper to notify the client about a launched Pinentry. Because
|
|
|
|
|
that might disturb some older clients, this is only done if enabled
|
|
|
|
|
via an option. Returns an gpg error code. */
|
2008-02-14 19:50:10 +00:00
|
|
|
|
gpg_error_t
|
|
|
|
|
agent_inq_pinentry_launched (ctrl_t ctrl, unsigned long pid)
|
|
|
|
|
{
|
|
|
|
|
char line[100];
|
|
|
|
|
|
|
|
|
|
if (!ctrl || !ctrl->server_local
|
|
|
|
|
|| !ctrl->server_local->allow_pinentry_notify)
|
|
|
|
|
return 0;
|
|
|
|
|
snprintf (line, DIM(line)-1, "PINENTRY_LAUNCHED %lu", pid);
|
|
|
|
|
return assuan_inquire (ctrl->server_local->assuan_ctx, line, NULL, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-14 14:53:42 +00:00
|
|
|
|
|
|
|
|
|
/* GETEVENTCOUNTER
|
|
|
|
|
|
|
|
|
|
Return a a status line named EVENTCOUNTER with the current values
|
|
|
|
|
of all event counters. The values are decimal numbers in the range
|
|
|
|
|
0 to UINT_MAX and wrapping around to 0. The actual values should
|
|
|
|
|
not be relied upon, they shall only be used to detect a change.
|
|
|
|
|
|
|
|
|
|
The currently defined counters are:
|
|
|
|
|
|
|
|
|
|
ANY - Incremented with any change of any of the other counters.
|
|
|
|
|
KEY - Incremented for added or removed private keys.
|
|
|
|
|
CARD - Incremented for changes of the card readers stati.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
cmd_geteventcounter (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
char any_counter[25];
|
|
|
|
|
char key_counter[25];
|
|
|
|
|
char card_counter[25];
|
|
|
|
|
|
|
|
|
|
snprintf (any_counter, sizeof any_counter, "%u", eventcounter.any);
|
|
|
|
|
snprintf (key_counter, sizeof key_counter, "%u", eventcounter.key);
|
|
|
|
|
snprintf (card_counter, sizeof card_counter, "%u", eventcounter.card);
|
|
|
|
|
|
|
|
|
|
return agent_write_status (ctrl, "EVENTCOUNTER",
|
|
|
|
|
any_counter,
|
|
|
|
|
key_counter,
|
|
|
|
|
card_counter,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function should be called once for all key removals or
|
2007-11-27 08:01:19 +00:00
|
|
|
|
additions. This function is assured not to do any context
|
2006-11-14 14:53:42 +00:00
|
|
|
|
switches. */
|
|
|
|
|
void
|
|
|
|
|
bump_key_eventcounter (void)
|
|
|
|
|
{
|
|
|
|
|
eventcounter.key++;
|
|
|
|
|
eventcounter.any++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function should be called for all card reader status
|
2007-11-27 08:01:19 +00:00
|
|
|
|
changes. This function is assured not to do any context
|
2006-11-14 14:53:42 +00:00
|
|
|
|
switches. */
|
|
|
|
|
void
|
|
|
|
|
bump_card_eventcounter (void)
|
|
|
|
|
{
|
|
|
|
|
eventcounter.card++;
|
|
|
|
|
eventcounter.any++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-25 18:29:20 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
/* ISTRUSTED <hexstring_with_fingerprint>
|
|
|
|
|
|
|
|
|
|
Return OK when we have an entry with this fingerprint in our
|
|
|
|
|
trustlist */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_istrusted (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2006-09-25 18:29:20 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc, n, i;
|
|
|
|
|
char *p;
|
|
|
|
|
char fpr[41];
|
|
|
|
|
|
2006-09-15 18:53:37 +00:00
|
|
|
|
/* Parse the fingerprint value. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
for (p=line,n=0; hexdigitp (p); p++, n++)
|
|
|
|
|
;
|
|
|
|
|
if (*p || !(n == 40 || n == 32))
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid fingerprint");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
if (n==32)
|
|
|
|
|
{
|
|
|
|
|
strcpy (fpr, "00000000");
|
|
|
|
|
i += 8;
|
|
|
|
|
}
|
|
|
|
|
for (p=line; i < 40; p++, i++)
|
|
|
|
|
fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
|
|
|
|
|
fpr[i] = 0;
|
2006-09-25 18:29:20 +00:00
|
|
|
|
rc = agent_istrusted (ctrl, fpr);
|
2006-09-06 16:35:52 +00:00
|
|
|
|
if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
|
|
|
|
|
return rc;
|
|
|
|
|
else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF )
|
|
|
|
|
return gpg_error (GPG_ERR_NOT_TRUSTED);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error ("command is_trusted failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* LISTTRUSTED
|
|
|
|
|
|
|
|
|
|
List all entries from the trustlist */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_listtrusted (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc = agent_listtrusted (ctx);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command listtrusted failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>
|
|
|
|
|
|
|
|
|
|
Store a new key in into the trustlist*/
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_marktrusted (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc, n, i;
|
|
|
|
|
char *p;
|
|
|
|
|
char fpr[41];
|
|
|
|
|
int flag;
|
|
|
|
|
|
|
|
|
|
/* parse the fingerprint value */
|
|
|
|
|
for (p=line,n=0; hexdigitp (p); p++, n++)
|
|
|
|
|
;
|
|
|
|
|
if (!spacep (p) || !(n == 40 || n == 32))
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid fingerprint");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
if (n==32)
|
|
|
|
|
{
|
|
|
|
|
strcpy (fpr, "00000000");
|
|
|
|
|
i += 8;
|
|
|
|
|
}
|
|
|
|
|
for (p=line; i < 40; p++, i++)
|
|
|
|
|
fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
|
|
|
|
|
fpr[i] = 0;
|
|
|
|
|
|
|
|
|
|
while (spacep (p))
|
|
|
|
|
p++;
|
|
|
|
|
flag = *p++;
|
|
|
|
|
if ( (flag != 'S' && flag != 'P') || !spacep (p) )
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid flag - must be P or S");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
while (spacep (p))
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
rc = agent_marktrusted (ctrl, p, fpr, flag);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command marktrusted failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* HAVEKEY <hexstring_with_keygrip>
|
|
|
|
|
|
|
|
|
|
Return success when the secret key is available */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_havekey (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
unsigned char buf[20];
|
|
|
|
|
|
|
|
|
|
rc = parse_keygrip (ctx, line, buf);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if (agent_key_available (buf))
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NO_SECKEY);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SIGKEY <hexstring_with_keygrip>
|
|
|
|
|
SETKEY <hexstring_with_keygrip>
|
|
|
|
|
|
|
|
|
|
Set the key used for a sign or decrypt operation */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_sigkey (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
rc = parse_keygrip (ctx, line, ctrl->keygrip);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
ctrl->have_keygrip = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 08:07:12 +00:00
|
|
|
|
/* SETKEYDESC plus_percent_escaped_string
|
2004-02-13 17:06:34 +00:00
|
|
|
|
|
|
|
|
|
Set a description to be used for the next PKSIGN or PKDECRYPT
|
|
|
|
|
operation if this operation requires the entry of a passphrase. If
|
|
|
|
|
this command is not used a default text will be used. Note, that
|
|
|
|
|
this description implictly selects the label used for the entry
|
|
|
|
|
box; if the string contains the string PIN (which in general will
|
2005-11-28 11:52:25 +00:00
|
|
|
|
not be translated), "PIN" is used, otherwise the translation of
|
2006-09-04 08:07:12 +00:00
|
|
|
|
"passphrase" is used. The description string should not contain
|
2004-02-13 17:06:34 +00:00
|
|
|
|
blanks unless they are percent or '+' escaped.
|
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
The description is only valid for the next PKSIGN or PKDECRYPT
|
2004-02-13 17:06:34 +00:00
|
|
|
|
operation.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
cmd_setkeydesc (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
char *desc, *p;
|
|
|
|
|
|
|
|
|
|
for (p=line; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
desc = p;
|
|
|
|
|
p = strchr (desc, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = 0; /* We ignore any garbage; we might late use it for other args. */
|
|
|
|
|
|
|
|
|
|
if (!desc || !*desc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
|
2004-02-13 17:06:34 +00:00
|
|
|
|
|
|
|
|
|
/* Note, that we only need to replace the + characters and should
|
|
|
|
|
leave the other escaping in place because the escaped string is
|
|
|
|
|
send verbatim to the pinentry which does the unescaping (but not
|
|
|
|
|
the + replacing) */
|
|
|
|
|
plus_to_blank (desc);
|
|
|
|
|
|
|
|
|
|
xfree (ctrl->server_local->keydesc);
|
|
|
|
|
ctrl->server_local->keydesc = xtrystrdup (desc);
|
|
|
|
|
if (!ctrl->server_local->keydesc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2004-02-13 17:06:34 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-06 10:58:18 +00:00
|
|
|
|
/* SETHASH --hash=<name>|<algonumber> <hexstring>
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
The client can use this command to tell the server about the data
|
|
|
|
|
(which usually is a hash) to be signed. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_sethash (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
size_t n;
|
|
|
|
|
char *p;
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
unsigned char *buf;
|
|
|
|
|
char *endp;
|
|
|
|
|
int algo;
|
|
|
|
|
|
2006-10-06 10:58:18 +00:00
|
|
|
|
/* Parse the alternative hash options which may be used instead of
|
|
|
|
|
the algo number. */
|
|
|
|
|
if (has_option_name (line, "--hash"))
|
|
|
|
|
{
|
|
|
|
|
if (has_option (line, "--hash=sha1"))
|
|
|
|
|
algo = GCRY_MD_SHA1;
|
|
|
|
|
else if (has_option (line, "--hash=sha256"))
|
|
|
|
|
algo = GCRY_MD_SHA256;
|
|
|
|
|
else if (has_option (line, "--hash=rmd160"))
|
|
|
|
|
algo = GCRY_MD_RMD160;
|
|
|
|
|
else if (has_option (line, "--hash=md5"))
|
|
|
|
|
algo = GCRY_MD_MD5;
|
|
|
|
|
else if (has_option (line, "--hash=tls-md5sha1"))
|
|
|
|
|
algo = GCRY_MD_USER_TLS_MD5SHA1;
|
|
|
|
|
else
|
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid hash algorithm");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
algo = 0;
|
|
|
|
|
|
|
|
|
|
line = skip_options (line);
|
|
|
|
|
|
|
|
|
|
if (!algo)
|
|
|
|
|
{
|
|
|
|
|
/* No hash option has been given: require an algo number instead */
|
|
|
|
|
algo = (int)strtoul (line, &endp, 10);
|
|
|
|
|
for (line = endp; *line == ' ' || *line == '\t'; line++)
|
|
|
|
|
;
|
|
|
|
|
if (!algo || gcry_md_test_algo (algo))
|
|
|
|
|
return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM, NULL);
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
ctrl->digest.algo = algo;
|
|
|
|
|
|
2006-10-05 11:06:42 +00:00
|
|
|
|
/* Parse the hash value. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
rc = parse_hexstring (ctx, line, &n);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
n /= 2;
|
2006-10-06 10:58:18 +00:00
|
|
|
|
if (algo == GCRY_MD_USER_TLS_MD5SHA1 && n == 36)
|
|
|
|
|
;
|
|
|
|
|
else if (n != 16 && n != 20 && n != 24 && n != 32)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "unsupported length of hash");
|
2006-10-06 10:58:18 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (n > MAX_DIGEST_LEN)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "hash value to long");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
buf = ctrl->digest.value;
|
|
|
|
|
ctrl->digest.valuelen = n;
|
|
|
|
|
for (p=line, n=0; n < ctrl->digest.valuelen; p += 2, n++)
|
|
|
|
|
buf[n] = xtoi_2 (p);
|
|
|
|
|
for (; n < ctrl->digest.valuelen; n++)
|
|
|
|
|
buf[n] = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* PKSIGN <options>
|
|
|
|
|
|
|
|
|
|
Perform the actual sign operation. Neither input nor output are
|
2005-11-28 11:52:25 +00:00
|
|
|
|
sensitive to eavesdropping. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_pksign (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2005-06-07 19:09:18 +00:00
|
|
|
|
cache_mode_t cache_mode = CACHE_MODE_NORMAL;
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2004-12-20 08:32:56 +00:00
|
|
|
|
membuf_t outbuf;
|
2005-06-07 19:09:18 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (opt.ignore_cache_for_signing)
|
2005-06-07 19:09:18 +00:00
|
|
|
|
cache_mode = CACHE_MODE_IGNORE;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else if (!ctrl->server_local->use_cache_for_signing)
|
2005-06-07 19:09:18 +00:00
|
|
|
|
cache_mode = CACHE_MODE_IGNORE;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-12-20 08:32:56 +00:00
|
|
|
|
init_membuf (&outbuf, 512);
|
|
|
|
|
|
2004-02-13 17:06:34 +00:00
|
|
|
|
rc = agent_pksign (ctrl, ctrl->server_local->keydesc,
|
2005-06-07 19:09:18 +00:00
|
|
|
|
&outbuf, cache_mode);
|
2004-12-20 08:32:56 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
clear_outbuf (&outbuf);
|
|
|
|
|
else
|
|
|
|
|
rc = write_and_clear_outbuf (ctx, &outbuf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command pksign failed: %s\n", gpg_strerror (rc));
|
2004-02-13 17:06:34 +00:00
|
|
|
|
xfree (ctrl->server_local->keydesc);
|
|
|
|
|
ctrl->server_local->keydesc = NULL;
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* PKDECRYPT <options>
|
|
|
|
|
|
|
|
|
|
Perform the actual decrypt operation. Input is not
|
|
|
|
|
sensitive to eavesdropping */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_pkdecrypt (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-11-12 15:17:44 +00:00
|
|
|
|
unsigned char *value;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
size_t valuelen;
|
2004-12-20 08:32:56 +00:00
|
|
|
|
membuf_t outbuf;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
/* First inquire the data to decrypt */
|
|
|
|
|
rc = assuan_inquire (ctx, "CIPHERTEXT",
|
|
|
|
|
&value, &valuelen, MAXLEN_CIPHERTEXT);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
2004-12-20 08:32:56 +00:00
|
|
|
|
init_membuf (&outbuf, 512);
|
|
|
|
|
|
2004-02-13 17:06:34 +00:00
|
|
|
|
rc = agent_pkdecrypt (ctrl, ctrl->server_local->keydesc,
|
2004-12-20 08:32:56 +00:00
|
|
|
|
value, valuelen, &outbuf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
xfree (value);
|
2004-12-20 08:32:56 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
clear_outbuf (&outbuf);
|
|
|
|
|
else
|
|
|
|
|
rc = write_and_clear_outbuf (ctx, &outbuf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command pkdecrypt failed: %s\n", gpg_strerror (rc));
|
2004-02-13 17:06:34 +00:00
|
|
|
|
xfree (ctrl->server_local->keydesc);
|
|
|
|
|
ctrl->server_local->keydesc = NULL;
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GENKEY
|
|
|
|
|
|
|
|
|
|
Generate a new key, store the secret part and return the public
|
|
|
|
|
part. Here is an example transaction:
|
|
|
|
|
|
|
|
|
|
C: GENKEY
|
2008-02-14 19:50:10 +00:00
|
|
|
|
S: INQUIRE KEYPARAM
|
2003-08-05 17:11:04 +00:00
|
|
|
|
C: D (genkey (rsa (nbits 1024)))
|
|
|
|
|
C: END
|
|
|
|
|
S: D (public-key
|
|
|
|
|
S: D (rsa (n 326487324683264) (e 10001)))
|
|
|
|
|
S OK key created
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_genkey (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc;
|
2003-11-12 15:17:44 +00:00
|
|
|
|
unsigned char *value;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
size_t valuelen;
|
2004-12-20 08:32:56 +00:00
|
|
|
|
membuf_t outbuf;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
/* First inquire the parameters */
|
|
|
|
|
rc = assuan_inquire (ctx, "KEYPARAM", &value, &valuelen, MAXLEN_KEYPARAM);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
2004-12-20 08:32:56 +00:00
|
|
|
|
init_membuf (&outbuf, 512);
|
|
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
|
rc = agent_genkey (ctrl, (char*)value, valuelen, &outbuf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
xfree (value);
|
2004-12-20 08:32:56 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
clear_outbuf (&outbuf);
|
|
|
|
|
else
|
|
|
|
|
rc = write_and_clear_outbuf (ctx, &outbuf);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command genkey failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-23 21:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* READKEY <hexstring_with_keygrip>
|
|
|
|
|
|
|
|
|
|
Return the public key for the given keygrip. */
|
|
|
|
|
static int
|
|
|
|
|
cmd_readkey (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
int rc;
|
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
gcry_sexp_t s_pkey = NULL;
|
|
|
|
|
|
|
|
|
|
rc = parse_keygrip (ctx, line, grip);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc; /* Return immediately as this is already an Assuan error code.*/
|
|
|
|
|
|
|
|
|
|
rc = agent_public_key_from_file (ctrl, grip, &s_pkey);
|
|
|
|
|
if (!rc)
|
|
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
|
|
len = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, NULL, 0);
|
|
|
|
|
assert (len);
|
|
|
|
|
buf = xtrymalloc (len);
|
|
|
|
|
if (!buf)
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = gpg_error_from_syserror ();
|
2005-02-23 21:06:32 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
len = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, buf, len);
|
|
|
|
|
assert (len);
|
|
|
|
|
rc = assuan_send_data (ctx, buf, len);
|
|
|
|
|
xfree (buf);
|
|
|
|
|
}
|
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command readkey failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2005-02-23 21:06:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-04 16:45:04 +00:00
|
|
|
|
static int
|
|
|
|
|
send_back_passphrase (assuan_context_t ctx, int via_data, const char *pw)
|
|
|
|
|
{
|
|
|
|
|
size_t n;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
assuan_begin_confidential (ctx);
|
|
|
|
|
n = strlen (pw);
|
|
|
|
|
if (via_data)
|
|
|
|
|
rc = assuan_send_data (ctx, pw, n);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char *p = xtrymalloc_secure (n*2+1);
|
|
|
|
|
if (!p)
|
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bin2hex (pw, n, p);
|
|
|
|
|
rc = assuan_set_okay_line (ctx, p);
|
|
|
|
|
xfree (p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-25 08:30:47 +00:00
|
|
|
|
/* GET_PASSPHRASE [--data] [--check] <cache_id>
|
|
|
|
|
[<error_message> <prompt> <description>]
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
This function is usually used to ask for a passphrase to be used
|
|
|
|
|
for conventional encryption, but may also be used by programs which
|
|
|
|
|
need specal handling of passphrases. This command uses a syntax
|
|
|
|
|
which helps clients to use the agent with minimum effort. The
|
|
|
|
|
agent either returns with an error or with a OK followed by the hex
|
|
|
|
|
encoded passphrase. Note that the length of the strings is
|
|
|
|
|
implicitly limited by the maximum length of a command.
|
2006-10-04 16:45:04 +00:00
|
|
|
|
|
|
|
|
|
If the option "--data" is used the passphrase is returned by usual
|
|
|
|
|
data lines and not on the okay line.
|
2007-01-25 08:30:47 +00:00
|
|
|
|
|
|
|
|
|
If the option "--check" is used the passphrase constraints checks as
|
|
|
|
|
implemented by gpg-agent are applied. A check is not done if the
|
|
|
|
|
passphrase has been found in the cache.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_get_passphrase (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc;
|
|
|
|
|
const char *pw;
|
|
|
|
|
char *response;
|
|
|
|
|
char *cacheid = NULL, *desc = NULL, *prompt = NULL, *errtext = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
void *cache_marker;
|
2007-01-25 08:30:47 +00:00
|
|
|
|
int opt_data, opt_check;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2006-10-04 16:45:04 +00:00
|
|
|
|
opt_data = has_option (line, "--data");
|
2007-01-25 08:30:47 +00:00
|
|
|
|
opt_check = has_option (line, "--check");
|
2006-10-04 16:45:04 +00:00
|
|
|
|
line = skip_options (line);
|
|
|
|
|
|
|
|
|
|
cacheid = line;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
p = strchr (cacheid, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
while (*p == ' ')
|
|
|
|
|
p++;
|
|
|
|
|
errtext = p;
|
|
|
|
|
p = strchr (errtext, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
while (*p == ' ')
|
|
|
|
|
p++;
|
|
|
|
|
prompt = p;
|
|
|
|
|
p = strchr (prompt, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
while (*p == ' ')
|
|
|
|
|
p++;
|
|
|
|
|
desc = p;
|
|
|
|
|
p = strchr (desc, ' ');
|
|
|
|
|
if (p)
|
2007-01-25 08:30:47 +00:00
|
|
|
|
*p = 0; /* Ignore trailing garbage. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!cacheid || !*cacheid || strlen (cacheid) > 50)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of cacheID");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!desc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (!strcmp (cacheid, "X"))
|
|
|
|
|
cacheid = NULL;
|
|
|
|
|
if (!strcmp (errtext, "X"))
|
|
|
|
|
errtext = NULL;
|
|
|
|
|
if (!strcmp (prompt, "X"))
|
|
|
|
|
prompt = NULL;
|
|
|
|
|
if (!strcmp (desc, "X"))
|
|
|
|
|
desc = NULL;
|
|
|
|
|
|
2005-06-07 19:09:18 +00:00
|
|
|
|
pw = cacheid ? agent_get_cache (cacheid, CACHE_MODE_NORMAL, &cache_marker)
|
|
|
|
|
: NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (pw)
|
|
|
|
|
{
|
2006-10-04 16:45:04 +00:00
|
|
|
|
rc = send_back_passphrase (ctx, opt_data, pw);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
agent_unlock_cache_entry (&cache_marker);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Note, that we only need to replace the + characters and
|
|
|
|
|
should leave the other escaping in place because the escaped
|
|
|
|
|
string is send verbatim to the pinentry which does the
|
|
|
|
|
unescaping (but not the + replacing) */
|
|
|
|
|
if (errtext)
|
|
|
|
|
plus_to_blank (errtext);
|
|
|
|
|
if (prompt)
|
|
|
|
|
plus_to_blank (prompt);
|
|
|
|
|
if (desc)
|
|
|
|
|
plus_to_blank (desc);
|
|
|
|
|
|
2007-01-25 08:30:47 +00:00
|
|
|
|
response = NULL;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
xfree (response);
|
|
|
|
|
rc = agent_get_passphrase (ctrl, &response, desc, prompt, errtext);
|
|
|
|
|
}
|
|
|
|
|
while (!rc
|
|
|
|
|
&& opt_check
|
2007-09-18 11:40:09 +00:00
|
|
|
|
&& check_passphrase_constraints (ctrl, response, 0));
|
2007-01-25 08:30:47 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!rc)
|
|
|
|
|
{
|
|
|
|
|
if (cacheid)
|
2005-06-07 19:09:18 +00:00
|
|
|
|
agent_put_cache (cacheid, CACHE_MODE_USER, response, 0);
|
2006-10-04 16:45:04 +00:00
|
|
|
|
rc = send_back_passphrase (ctx, opt_data, response);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
xfree (response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command get_passphrase failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CLEAR_PASSPHRASE <cache_id>
|
|
|
|
|
|
|
|
|
|
may be used to invalidate the cache entry for a passphrase. The
|
|
|
|
|
function returns with OK even when there is no cached passphrase.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_clear_passphrase (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
char *cacheid = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
/* parse the stuff */
|
|
|
|
|
for (p=line; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
cacheid = p;
|
|
|
|
|
p = strchr (cacheid, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = 0; /* ignore garbage */
|
|
|
|
|
if (!cacheid || !*cacheid || strlen (cacheid) > 50)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of cacheID");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2005-06-07 19:09:18 +00:00
|
|
|
|
agent_put_cache (cacheid, CACHE_MODE_USER, NULL, 0);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-31 12:11:48 +00:00
|
|
|
|
|
|
|
|
|
/* GET_CONFIRMATION <description>
|
|
|
|
|
|
|
|
|
|
This command may be used to ask for a simple confirmation.
|
|
|
|
|
DESCRIPTION is displayed along with a Okay and Cancel button. This
|
|
|
|
|
command uses a syntax which helps clients to use the agent with
|
|
|
|
|
minimum effort. The agent either returns with an error or with a
|
|
|
|
|
OK. Note, that the length of DESCRIPTION is implicitly limited by
|
2004-02-13 17:06:34 +00:00
|
|
|
|
the maximum length of a command. DESCRIPTION should not contain
|
|
|
|
|
any spaces, those must be encoded either percent escaped or simply
|
2003-10-31 12:11:48 +00:00
|
|
|
|
as '+'.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_get_confirmation (assuan_context_t ctx, char *line)
|
2003-10-31 12:11:48 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-10-31 12:11:48 +00:00
|
|
|
|
int rc;
|
|
|
|
|
char *desc = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
/* parse the stuff */
|
|
|
|
|
for (p=line; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
desc = p;
|
|
|
|
|
p = strchr (desc, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = 0; /* We ignore any garbage -may be later used for other args. */
|
|
|
|
|
|
|
|
|
|
if (!desc || !*desc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
|
2003-10-31 12:11:48 +00:00
|
|
|
|
|
|
|
|
|
if (!strcmp (desc, "X"))
|
|
|
|
|
desc = NULL;
|
|
|
|
|
|
|
|
|
|
/* Note, that we only need to replace the + characters and should
|
|
|
|
|
leave the other escaping in place because the escaped string is
|
|
|
|
|
send verbatim to the pinentry which does the unescaping (but not
|
|
|
|
|
the + replacing) */
|
|
|
|
|
if (desc)
|
|
|
|
|
plus_to_blank (desc);
|
|
|
|
|
|
|
|
|
|
rc = agent_get_confirmation (ctrl, desc, NULL, NULL);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command get_confirmation failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-10-31 12:11:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
/* LEARN [--send]
|
|
|
|
|
|
|
|
|
|
Learn something about the currently inserted smartcard. With
|
|
|
|
|
--send the new certificates are send back. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_learn (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-01-29 20:17:27 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc;
|
|
|
|
|
|
2004-01-29 20:17:27 +00:00
|
|
|
|
rc = agent_handle_learn (ctrl, has_option (line, "--send")? ctx : NULL);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command learn failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* PASSWD <hexstring_with_keygrip>
|
|
|
|
|
|
|
|
|
|
Change the passphrase/PID for the key identified by keygrip in LINE. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_passwd (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc;
|
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
gcry_sexp_t s_skey = NULL;
|
|
|
|
|
unsigned char *shadow_info = NULL;
|
|
|
|
|
|
|
|
|
|
rc = parse_keygrip (ctx, line, grip);
|
|
|
|
|
if (rc)
|
2007-01-25 08:30:47 +00:00
|
|
|
|
goto leave;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2007-08-28 17:48:13 +00:00
|
|
|
|
ctrl->in_passwd++;
|
2004-02-21 13:05:22 +00:00
|
|
|
|
rc = agent_key_from_file (ctrl, ctrl->server_local->keydesc,
|
2005-06-07 19:09:18 +00:00
|
|
|
|
grip, &shadow_info, CACHE_MODE_IGNORE, &s_skey);
|
2004-01-16 17:39:58 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else if (!s_skey)
|
|
|
|
|
{
|
|
|
|
|
log_error ("changing a smartcard PIN is not yet supported\n");
|
|
|
|
|
rc = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
rc = agent_protect_and_store (ctrl, s_skey);
|
2007-08-28 17:48:13 +00:00
|
|
|
|
ctrl->in_passwd--;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-02-21 13:05:22 +00:00
|
|
|
|
xfree (ctrl->server_local->keydesc);
|
|
|
|
|
ctrl->server_local->keydesc = NULL;
|
2007-01-25 08:30:47 +00:00
|
|
|
|
|
|
|
|
|
leave:
|
2003-08-05 17:11:04 +00:00
|
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
|
xfree (shadow_info);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command passwd failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-29 16:40:54 +00:00
|
|
|
|
/* PRESET_PASSPHRASE <hexstring_with_keygrip> <timeout> <hexstring>
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
Set the cached passphrase/PIN for the key identified by the keygrip
|
|
|
|
|
to passwd for the given time, where -1 means infinite and 0 means
|
|
|
|
|
the default (currently only a timeout of -1 is allowed, which means
|
|
|
|
|
to never expire it). If passwd is not provided, ask for it via the
|
|
|
|
|
pinentry module. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_preset_passphrase (assuan_context_t ctx, char *line)
|
2004-12-21 19:05:15 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
char *grip_clear = NULL;
|
|
|
|
|
char *passphrase = NULL;
|
|
|
|
|
int ttl;
|
2006-07-29 16:40:54 +00:00
|
|
|
|
size_t len;
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
if (!opt.allow_preset_passphrase)
|
2008-09-03 09:37:32 +00:00
|
|
|
|
return set_error (GPG_ERR_NOT_SUPPORTED, "no --allow-preset-passphrase");
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
rc = parse_keygrip (ctx, line, grip);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
/* FIXME: parse_keygrip should return a tail pointer. */
|
|
|
|
|
grip_clear = line;
|
|
|
|
|
while (*line && (*line != ' ' && *line != '\t'))
|
|
|
|
|
line++;
|
|
|
|
|
if (!*line)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return gpg_error (GPG_ERR_MISSING_VALUE);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
*line = '\0';
|
|
|
|
|
line++;
|
|
|
|
|
while (*line && (*line == ' ' || *line == '\t'))
|
|
|
|
|
line++;
|
|
|
|
|
|
|
|
|
|
/* Currently, only infinite timeouts are allowed. */
|
|
|
|
|
ttl = -1;
|
|
|
|
|
if (line[0] != '-' || line[1] != '1')
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
line++;
|
|
|
|
|
line++;
|
|
|
|
|
while (!(*line != ' ' && *line != '\t'))
|
|
|
|
|
line++;
|
|
|
|
|
|
2006-07-29 16:40:54 +00:00
|
|
|
|
/* Syntax check the hexstring. */
|
|
|
|
|
rc = parse_hexstring (ctx, line, &len);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
2004-12-21 19:05:15 +00:00
|
|
|
|
/* If there is a passphrase, use it. Currently, a passphrase is
|
|
|
|
|
required. */
|
|
|
|
|
if (*line)
|
2008-09-03 09:37:32 +00:00
|
|
|
|
{
|
|
|
|
|
/* Do in-place conversion. */
|
|
|
|
|
passphrase = line;
|
|
|
|
|
if (!hex2str (passphrase, passphrase, strlen (passphrase)+1, NULL))
|
|
|
|
|
rc = set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
|
|
|
|
|
}
|
2004-12-21 19:05:15 +00:00
|
|
|
|
else
|
2008-09-03 09:37:32 +00:00
|
|
|
|
rc = set_error (GPG_ERR_NOT_IMPLEMENTED, "passphrase is required");
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
2008-09-03 09:37:32 +00:00
|
|
|
|
if (!rc)
|
|
|
|
|
rc = agent_put_cache (grip_clear, CACHE_MODE_ANY, passphrase, ttl);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command preset_passwd failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2004-12-21 19:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
/* SCD <commands to pass to the scdaemon>
|
|
|
|
|
|
|
|
|
|
This is a general quote command to redirect everything to the
|
|
|
|
|
SCDAEMON. */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
cmd_scd (assuan_context_t ctx, char *line)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
rc = divert_generic_cmd (ctrl, line, ctx);
|
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 08:07:12 +00:00
|
|
|
|
|
|
|
|
|
/* GETVAL <key>
|
|
|
|
|
|
|
|
|
|
Return the value for KEY from the special environment as created by
|
|
|
|
|
PUTVAL.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
cmd_getval (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
int rc = 0;
|
|
|
|
|
char *key = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
struct putval_item_s *vl;
|
|
|
|
|
|
|
|
|
|
for (p=line; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
key = p;
|
|
|
|
|
p = strchr (key, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
for (; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
if (*p)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "too many arguments");
|
2006-09-04 08:07:12 +00:00
|
|
|
|
}
|
|
|
|
|
if (!key || !*key)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
|
2006-09-04 08:07:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (vl=putval_list; vl; vl = vl->next)
|
|
|
|
|
if ( !strcmp (vl->d, key) )
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (vl) /* Got an entry. */
|
2006-09-06 16:35:52 +00:00
|
|
|
|
rc = assuan_send_data (ctx, vl->d+vl->off, vl->len);
|
2006-09-04 08:07:12 +00:00
|
|
|
|
else
|
|
|
|
|
return gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command getval failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2006-09-04 08:07:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* PUTVAL <key> [<percent_escaped_value>]
|
|
|
|
|
|
|
|
|
|
The gpg-agent maintains a kind of environment which may be used to
|
|
|
|
|
store key/value pairs in it, so that they can be retrieved later.
|
|
|
|
|
This may be used by helper daemons to daemonize themself on
|
|
|
|
|
invocation and register them with gpg-agent. Callers of the
|
|
|
|
|
daemon's service may now first try connect to get the information
|
|
|
|
|
for that service from gpg-agent through the GETVAL command and then
|
|
|
|
|
try to connect to that daemon. Only if that fails they may start
|
|
|
|
|
an own instance of the service daemon.
|
|
|
|
|
|
|
|
|
|
KEY is an an arbitrary symbol with the same syntax rules as keys
|
|
|
|
|
for shell environment variables. PERCENT_ESCAPED_VALUE is the
|
|
|
|
|
corresponsing value; they should be similar to the values of
|
|
|
|
|
envronment variables but gpg-agent does not enforce any
|
|
|
|
|
restrictions. If that value is not given any value under that KEY
|
|
|
|
|
is removed from this special environment.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
cmd_putval (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
int rc = 0;
|
|
|
|
|
char *key = NULL;
|
|
|
|
|
char *value = NULL;
|
|
|
|
|
size_t valuelen = 0;
|
|
|
|
|
char *p;
|
|
|
|
|
struct putval_item_s *vl, *vlprev;
|
|
|
|
|
|
|
|
|
|
for (p=line; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
key = p;
|
|
|
|
|
p = strchr (key, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
for (; *p == ' '; p++)
|
|
|
|
|
;
|
|
|
|
|
if (*p)
|
|
|
|
|
{
|
|
|
|
|
value = p;
|
|
|
|
|
p = strchr (value, ' ');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = 0;
|
|
|
|
|
valuelen = percent_plus_unescape (value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!key || !*key)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
|
2006-09-04 08:07:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (vl=putval_list,vlprev=NULL; vl; vlprev=vl, vl = vl->next)
|
|
|
|
|
if ( !strcmp (vl->d, key) )
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (vl) /* Delete old entry. */
|
|
|
|
|
{
|
|
|
|
|
if (vlprev)
|
|
|
|
|
vlprev->next = vl->next;
|
|
|
|
|
else
|
|
|
|
|
putval_list = vl->next;
|
|
|
|
|
xfree (vl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (valuelen) /* Add entry. */
|
|
|
|
|
{
|
|
|
|
|
vl = xtrymalloc (sizeof *vl + strlen (key) + valuelen);
|
|
|
|
|
if (!vl)
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = gpg_error_from_syserror ();
|
2006-09-04 08:07:12 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
vl->len = valuelen;
|
|
|
|
|
vl->off = strlen (key) + 1;
|
|
|
|
|
strcpy (vl->d, key);
|
|
|
|
|
memcpy (vl->d + vl->off, value, valuelen);
|
|
|
|
|
vl->next = putval_list;
|
|
|
|
|
putval_list = vl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("command putval failed: %s\n", gpg_strerror (rc));
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return rc;
|
2006-09-04 08:07:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-06-03 13:57:24 +00:00
|
|
|
|
|
|
|
|
|
/* UPDATESTARTUPTTY
|
|
|
|
|
|
|
|
|
|
Set startup TTY and X DISPLAY variables to the values of this
|
|
|
|
|
session. This command is useful to pull future pinentries to
|
|
|
|
|
another screen. It is only required because there is no way in the
|
|
|
|
|
ssh-agent protocol to convey this information. */
|
|
|
|
|
static int
|
|
|
|
|
cmd_updatestartuptty (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
|
|
|
|
|
xfree (opt.startup_display); opt.startup_display = NULL;
|
|
|
|
|
xfree (opt.startup_ttyname); opt.startup_ttyname = NULL;
|
|
|
|
|
xfree (opt.startup_ttytype); opt.startup_ttytype = NULL;
|
|
|
|
|
xfree (opt.startup_lc_ctype); opt.startup_lc_ctype = NULL;
|
|
|
|
|
xfree (opt.startup_lc_messages); opt.startup_lc_messages = NULL;
|
2007-11-19 16:03:50 +00:00
|
|
|
|
xfree (opt.startup_xauthority); opt.startup_xauthority = NULL;
|
2005-06-03 13:57:24 +00:00
|
|
|
|
|
|
|
|
|
if (ctrl->display)
|
|
|
|
|
opt.startup_display = xtrystrdup (ctrl->display);
|
|
|
|
|
if (ctrl->ttyname)
|
|
|
|
|
opt.startup_ttyname = xtrystrdup (ctrl->ttyname);
|
|
|
|
|
if (ctrl->ttytype)
|
|
|
|
|
opt.startup_ttytype = xtrystrdup (ctrl->ttytype);
|
|
|
|
|
if (ctrl->lc_ctype)
|
|
|
|
|
opt.startup_lc_ctype = xtrystrdup (ctrl->lc_ctype);
|
|
|
|
|
if (ctrl->lc_messages)
|
|
|
|
|
opt.startup_lc_messages = xtrystrdup (ctrl->lc_messages);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (ctrl->xauthority)
|
|
|
|
|
opt.startup_xauthority = xtrystrdup (ctrl->xauthority);
|
|
|
|
|
if (ctrl->pinentry_user_data)
|
|
|
|
|
opt.startup_pinentry_user_data = xtrystrdup (ctrl->pinentry_user_data);
|
2005-06-03 13:57:24 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-06-18 10:33:12 +00:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
/* KILLAGENT
|
|
|
|
|
|
|
|
|
|
Under Windows we start the agent on the fly. Thus it also make
|
|
|
|
|
sense to allow a client to stop the agent. */
|
|
|
|
|
static int
|
|
|
|
|
cmd_killagent (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
ctrl->server_local->stopme = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-07-04 09:34:28 +00:00
|
|
|
|
|
|
|
|
|
/* RELOADAGENT
|
|
|
|
|
|
|
|
|
|
As signals are inconvenient under Windows, we provide this command
|
|
|
|
|
to allow reloading of the configuration. */
|
|
|
|
|
static int
|
|
|
|
|
cmd_reloadagent (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
agent_sighup_action ();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-06-18 10:33:12 +00:00
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GETINFO <what>
|
|
|
|
|
|
|
|
|
|
Multipurpose function to return a variety of information.
|
|
|
|
|
Supported values for WHAT are:
|
|
|
|
|
|
|
|
|
|
version - Return the version of the program.
|
2007-10-02 16:30:58 +00:00
|
|
|
|
pid - Return the process id of the server.
|
2007-06-18 10:33:12 +00:00
|
|
|
|
socket_name - Return the name of the socket.
|
|
|
|
|
ssh_socket_name - Return the name of the ssh socket.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
cmd_getinfo (assuan_context_t ctx, char *line)
|
|
|
|
|
{
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
if (!strcmp (line, "version"))
|
|
|
|
|
{
|
|
|
|
|
const char *s = VERSION;
|
|
|
|
|
rc = assuan_send_data (ctx, s, strlen (s));
|
|
|
|
|
}
|
2007-10-02 16:30:58 +00:00
|
|
|
|
else if (!strcmp (line, "pid"))
|
|
|
|
|
{
|
|
|
|
|
char numbuf[50];
|
|
|
|
|
|
|
|
|
|
snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
|
|
|
|
|
rc = assuan_send_data (ctx, numbuf, strlen (numbuf));
|
|
|
|
|
}
|
2007-06-18 10:33:12 +00:00
|
|
|
|
else if (!strcmp (line, "socket_name"))
|
|
|
|
|
{
|
|
|
|
|
const char *s = get_agent_socket_name ();
|
|
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
|
rc = assuan_send_data (ctx, s, strlen (s));
|
|
|
|
|
else
|
|
|
|
|
rc = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (line, "ssh_socket_name"))
|
|
|
|
|
{
|
|
|
|
|
const char *s = get_agent_ssh_socket_name ();
|
|
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
|
rc = assuan_send_data (ctx, s, strlen (s));
|
|
|
|
|
else
|
|
|
|
|
rc = gpg_error (GPG_ERR_NO_DATA);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
option_handler (assuan_context_t ctx, const char *key, const char *value)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-02-13 17:06:34 +00:00
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (!strcmp (key, "display"))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->display)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->display);
|
|
|
|
|
ctrl->display = xtrystrdup (value);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!ctrl->display)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (key, "ttyname"))
|
|
|
|
|
{
|
|
|
|
|
if (!opt.keep_tty)
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->ttyname)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->ttyname);
|
|
|
|
|
ctrl->ttyname = xtrystrdup (value);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!ctrl->ttyname)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (key, "ttytype"))
|
|
|
|
|
{
|
|
|
|
|
if (!opt.keep_tty)
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->ttytype)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->ttytype);
|
|
|
|
|
ctrl->ttytype = xtrystrdup (value);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!ctrl->ttytype)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (key, "lc-ctype"))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->lc_ctype)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->lc_ctype);
|
|
|
|
|
ctrl->lc_ctype = xtrystrdup (value);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!ctrl->lc_ctype)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (key, "lc-messages"))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->lc_messages)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->lc_messages);
|
|
|
|
|
ctrl->lc_messages = xtrystrdup (value);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (!ctrl->lc_messages)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return out_of_core ();
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
2007-11-19 16:03:50 +00:00
|
|
|
|
else if (!strcmp (key, "xauthority"))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->xauthority)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->xauthority);
|
|
|
|
|
ctrl->xauthority = xtrystrdup (value);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (!ctrl->xauthority)
|
|
|
|
|
return out_of_core ();
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (key, "pinentry-user-data"))
|
|
|
|
|
{
|
|
|
|
|
if (ctrl->pinentry_user_data)
|
2008-05-27 12:03:50 +00:00
|
|
|
|
xfree (ctrl->pinentry_user_data);
|
|
|
|
|
ctrl->pinentry_user_data = xtrystrdup (value);
|
2007-11-19 16:03:50 +00:00
|
|
|
|
if (!ctrl->pinentry_user_data)
|
|
|
|
|
return out_of_core ();
|
|
|
|
|
}
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else if (!strcmp (key, "use-cache-for-signing"))
|
|
|
|
|
ctrl->server_local->use_cache_for_signing = *value? atoi (value) : 0;
|
2008-02-14 19:50:10 +00:00
|
|
|
|
else if (!strcmp (key, "allow-pinentry-notify"))
|
|
|
|
|
ctrl->server_local->allow_pinentry_notify = 1;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
else
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_OPTION);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-26 17:32:28 +00:00
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2006-11-14 16:40:44 +00:00
|
|
|
|
/* Called by libassuan after all commands. ERR is the error from the
|
|
|
|
|
last assuan operation and not the one returned from the command. */
|
|
|
|
|
static void
|
|
|
|
|
post_cmd_notify (assuan_context_t ctx, int err)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
|
|
|
|
|
|
|
|
|
/* Switch off any I/O monitor controlled logging pausing. */
|
|
|
|
|
ctrl->server_local->pause_io_logging = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function is called by libassuan for all I/O. We use it here
|
|
|
|
|
to disable logging for the GETEVENTCOUNTER commands. This is so
|
|
|
|
|
that the debug output won't get cluttered by this primitive
|
|
|
|
|
command. */
|
|
|
|
|
static unsigned int
|
|
|
|
|
io_monitor (assuan_context_t ctx, int direction,
|
|
|
|
|
const char *line, size_t linelen)
|
|
|
|
|
{
|
|
|
|
|
ctrl_t ctrl = assuan_get_pointer (ctx);
|
2008-05-27 12:03:50 +00:00
|
|
|
|
|
2006-11-14 16:40:44 +00:00
|
|
|
|
/* Note that we only check for the uppercase name. This allows to
|
|
|
|
|
see the logging for debugging if using a non-upercase command
|
|
|
|
|
name. */
|
|
|
|
|
if (ctx && !direction
|
|
|
|
|
&& linelen >= 15
|
|
|
|
|
&& !strncmp (line, "GETEVENTCOUNTER", 15)
|
|
|
|
|
&& (linelen == 15 || spacep (line+15)))
|
|
|
|
|
{
|
|
|
|
|
ctrl->server_local->pause_io_logging = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctrl->server_local->pause_io_logging? 1:0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
/* Tell the assuan library about our commands */
|
|
|
|
|
static int
|
2006-09-06 16:35:52 +00:00
|
|
|
|
register_commands (assuan_context_t ctx)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
static struct {
|
|
|
|
|
const char *name;
|
2006-09-06 16:35:52 +00:00
|
|
|
|
int (*handler)(assuan_context_t, char *line);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
} table[] = {
|
2006-11-14 14:53:42 +00:00
|
|
|
|
{ "GETEVENTCOUNTER",cmd_geteventcounter },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ "ISTRUSTED", cmd_istrusted },
|
|
|
|
|
{ "HAVEKEY", cmd_havekey },
|
|
|
|
|
{ "SIGKEY", cmd_sigkey },
|
|
|
|
|
{ "SETKEY", cmd_sigkey },
|
2004-02-13 17:06:34 +00:00
|
|
|
|
{ "SETKEYDESC", cmd_setkeydesc },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ "SETHASH", cmd_sethash },
|
|
|
|
|
{ "PKSIGN", cmd_pksign },
|
|
|
|
|
{ "PKDECRYPT", cmd_pkdecrypt },
|
|
|
|
|
{ "GENKEY", cmd_genkey },
|
2005-02-23 21:06:32 +00:00
|
|
|
|
{ "READKEY", cmd_readkey },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ "GET_PASSPHRASE", cmd_get_passphrase },
|
2004-12-21 19:05:15 +00:00
|
|
|
|
{ "PRESET_PASSPHRASE", cmd_preset_passphrase },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ "CLEAR_PASSPHRASE", cmd_clear_passphrase },
|
2003-10-31 12:11:48 +00:00
|
|
|
|
{ "GET_CONFIRMATION", cmd_get_confirmation },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ "LISTTRUSTED", cmd_listtrusted },
|
|
|
|
|
{ "MARKTRUSTED", cmd_marktrusted },
|
|
|
|
|
{ "LEARN", cmd_learn },
|
|
|
|
|
{ "PASSWD", cmd_passwd },
|
|
|
|
|
{ "INPUT", NULL },
|
|
|
|
|
{ "OUTPUT", NULL },
|
|
|
|
|
{ "SCD", cmd_scd },
|
2006-09-04 08:07:12 +00:00
|
|
|
|
{ "GETVAL", cmd_getval },
|
|
|
|
|
{ "PUTVAL", cmd_putval },
|
2005-06-03 13:57:24 +00:00
|
|
|
|
{ "UPDATESTARTUPTTY", cmd_updatestartuptty },
|
2007-06-18 10:33:12 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
{ "KILLAGENT", cmd_killagent },
|
2007-07-04 09:34:28 +00:00
|
|
|
|
{ "RELOADAGENT", cmd_reloadagent },
|
2007-06-18 10:33:12 +00:00
|
|
|
|
#endif
|
|
|
|
|
{ "GETINFO", cmd_getinfo },
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
int i, rc;
|
|
|
|
|
|
|
|
|
|
for (i=0; table[i].name; i++)
|
|
|
|
|
{
|
|
|
|
|
rc = assuan_register_command (ctx, table[i].name, table[i].handler);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2006-11-14 16:40:44 +00:00
|
|
|
|
#ifdef HAVE_ASSUAN_SET_IO_MONITOR
|
|
|
|
|
assuan_register_post_cmd_notify (ctx, post_cmd_notify);
|
|
|
|
|
#endif
|
2003-08-05 17:11:04 +00:00
|
|
|
|
assuan_register_reset_notify (ctx, reset_notify);
|
|
|
|
|
assuan_register_option_handler (ctx, option_handler);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-20 16:49:41 +00:00
|
|
|
|
/* Startup the server. If LISTEN_FD and FD is given as -1, this is a
|
|
|
|
|
simple piper server, otherwise it is a regular server. CTRL is the
|
|
|
|
|
control structure for this connection; it has only the basic
|
|
|
|
|
intialization. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
void
|
2007-10-01 14:48:39 +00:00
|
|
|
|
start_command_handler (ctrl_t ctrl, gnupg_fd_t listen_fd, gnupg_fd_t fd)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2006-09-06 16:35:52 +00:00
|
|
|
|
assuan_context_t ctx;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2007-10-01 14:48:39 +00:00
|
|
|
|
if (listen_fd == GNUPG_INVALID_FD && fd == GNUPG_INVALID_FD)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int filedes[2];
|
|
|
|
|
|
|
|
|
|
filedes[0] = 0;
|
|
|
|
|
filedes[1] = 1;
|
|
|
|
|
rc = assuan_init_pipe_server (&ctx, filedes);
|
|
|
|
|
}
|
2007-10-01 14:48:39 +00:00
|
|
|
|
else if (listen_fd != GNUPG_INVALID_FD)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = assuan_init_socket_server_ext (&ctx, listen_fd, 0);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = assuan_init_socket_server_ext (&ctx, fd, 2);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to initialize the server: %s\n",
|
2006-09-06 16:35:52 +00:00
|
|
|
|
gpg_strerror(rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
agent_exit (2);
|
|
|
|
|
}
|
|
|
|
|
rc = register_commands (ctx);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to register commands with Assuan: %s\n",
|
2006-09-06 16:35:52 +00:00
|
|
|
|
gpg_strerror(rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
agent_exit (2);
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-20 16:49:41 +00:00
|
|
|
|
assuan_set_pointer (ctx, ctrl);
|
|
|
|
|
ctrl->server_local = xcalloc (1, sizeof *ctrl->server_local);
|
|
|
|
|
ctrl->server_local->assuan_ctx = ctx;
|
|
|
|
|
ctrl->server_local->message_fd = -1;
|
|
|
|
|
ctrl->server_local->use_cache_for_signing = 1;
|
|
|
|
|
ctrl->digest.raw_value = 0;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
if (DBG_ASSUAN)
|
|
|
|
|
assuan_set_log_stream (ctx, log_get_stream ());
|
|
|
|
|
|
2006-11-14 16:40:44 +00:00
|
|
|
|
#ifdef HAVE_ASSUAN_SET_IO_MONITOR
|
|
|
|
|
assuan_set_io_monitor (ctx, io_monitor);
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
rc = assuan_accept (ctx);
|
|
|
|
|
if (rc == -1)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (rc)
|
|
|
|
|
{
|
2006-09-06 16:35:52 +00:00
|
|
|
|
log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = assuan_process (ctx);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2006-09-06 16:35:52 +00:00
|
|
|
|
log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-29 20:17:27 +00:00
|
|
|
|
/* Reset the SCD if needed. */
|
2006-11-20 16:49:41 +00:00
|
|
|
|
agent_reset_scd (ctrl);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
/* Reset the pinentry (in case of popup messages). */
|
2006-11-20 16:49:41 +00:00
|
|
|
|
agent_reset_query (ctrl);
|
2005-11-28 11:52:25 +00:00
|
|
|
|
|
2006-11-20 16:49:41 +00:00
|
|
|
|
/* Cleanup. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
assuan_deinit_server (ctx);
|
2007-06-18 10:33:12 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
|
if (ctrl->server_local->stopme)
|
|
|
|
|
agent_exit (0);
|
|
|
|
|
#endif
|
2006-11-20 16:49:41 +00:00
|
|
|
|
xfree (ctrl->server_local);
|
|
|
|
|
ctrl->server_local = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|