2005-02-24 17:36:11 +00:00
|
|
|
|
/* simple-pwquery.c - A simple password query client for gpg-agent
|
2007-06-14 17:05:07 +00:00
|
|
|
|
* Copyright (C) 2002, 2004, 2007 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
|
2016-11-05 12:02:19 +01:00
|
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
/* This module is intended as a simple client implementation to
|
|
|
|
|
gpg-agent's GET_PASSPHRASE command. It can only cope with an
|
|
|
|
|
already running gpg-agent. Some stuff is configurable in the
|
|
|
|
|
header file. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
2016-08-11 12:26:09 +02:00
|
|
|
|
#include <assuan.h>
|
2004-12-15 14:15:54 +00:00
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2004-12-02 07:48:09 +00:00
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#else
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
2004-12-02 07:48:09 +00:00
|
|
|
|
#endif
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
#endif
|
2009-05-19 22:39:45 +00:00
|
|
|
|
|
2015-04-24 16:10:15 +02:00
|
|
|
|
#define GNUPG_COMMON_NEED_AFLOCAL
|
2010-03-10 12:24:58 +00:00
|
|
|
|
#include "../common/mischelp.h"
|
2016-08-11 12:26:09 +02:00
|
|
|
|
#include "sysutils.h"
|
|
|
|
|
#include "membuf.h"
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
|
|
#define SIMPLE_PWQUERY_IMPLEMENTATION 1
|
|
|
|
|
#include "simple-pwquery.h"
|
|
|
|
|
|
2016-08-11 09:52:08 +02:00
|
|
|
|
#define SPWQ_OUT_OF_CORE gpg_error_from_errno (ENOMEM)
|
|
|
|
|
#define SPWQ_IO_ERROR gpg_error_from_errno (EIO)
|
|
|
|
|
#define SPWQ_PROTOCOL_ERROR gpg_error (GPG_ERR_PROTOCOL_VIOLATION)
|
|
|
|
|
#define SPWQ_ERR_RESPONSE gpg_error (GPG_ERR_INV_RESPONSE)
|
|
|
|
|
#define SPWQ_NO_AGENT gpg_error (GPG_ERR_NO_AGENT)
|
|
|
|
|
#define SPWQ_SYS_ERROR gpg_error_from_syserror ()
|
|
|
|
|
#define SPWQ_GENERAL_ERROR gpg_error (GPG_ERR_GENERAL)
|
|
|
|
|
#define SPWQ_NO_PIN_ENTRY gpg_error (GPG_ERR_NO_PIN_ENTRY)
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#ifndef _
|
|
|
|
|
#define _(a) (a)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined (hexdigitp) && !defined (xtoi_2)
|
|
|
|
|
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
|
|
|
|
|
#define hexdigitp(a) (digitp (a) \
|
|
|
|
|
|| (*(a) >= 'A' && *(a) <= 'F') \
|
|
|
|
|
|| (*(a) >= 'a' && *(a) <= 'f'))
|
|
|
|
|
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
|
|
|
|
|
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
|
|
|
|
|
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2014-10-03 11:58:58 +02:00
|
|
|
|
/* Name of the socket to be used. This is a kludge to keep on using
|
2018-10-24 15:56:18 -04:00
|
|
|
|
the existing code despite that we only support a standard socket. */
|
2007-06-14 17:05:07 +00:00
|
|
|
|
static char *default_gpg_agent_info;
|
|
|
|
|
|
|
|
|
|
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_STPCPY
|
|
|
|
|
static char *
|
|
|
|
|
my_stpcpy(char *a,const char *b)
|
|
|
|
|
{
|
|
|
|
|
while( *b )
|
|
|
|
|
*a++ = *b++;
|
|
|
|
|
*a = 0;
|
|
|
|
|
|
|
|
|
|
return (char*)a;
|
|
|
|
|
}
|
|
|
|
|
#define stpcpy(a,b) my_stpcpy((a), (b))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
/* Send an option to the agent */
|
|
|
|
|
static int
|
2016-08-11 12:26:09 +02:00
|
|
|
|
agent_send_option (assuan_context_t ctx, const char *name, const char *value)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
int err;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *line;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
line = spwq_malloc (7 + strlen (name) + 1 + strlen (value) + 2);
|
|
|
|
|
if (!line)
|
|
|
|
|
return SPWQ_OUT_OF_CORE;
|
|
|
|
|
strcpy (stpcpy (stpcpy (stpcpy (
|
|
|
|
|
stpcpy (line, "OPTION "), name), "="), value), "\n");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
err = assuan_transact (ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
spwq_free (line);
|
|
|
|
|
return err;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Send all available options to the agent. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static int
|
2016-08-11 12:26:09 +02:00
|
|
|
|
agent_send_all_options (assuan_context_t ctx)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
char *dft_display = NULL;
|
|
|
|
|
char *dft_ttyname = NULL;
|
|
|
|
|
char *dft_ttytype = NULL;
|
2008-03-06 18:28:47 +00:00
|
|
|
|
char *dft_xauthority = NULL;
|
|
|
|
|
char *dft_pinentry_user_data = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
dft_display = getenv ("DISPLAY");
|
|
|
|
|
if (dft_display)
|
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
if ((rc = agent_send_option (ctx, "display", dft_display)))
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dft_ttyname = getenv ("GPG_TTY");
|
2012-11-20 19:01:13 +01:00
|
|
|
|
#if !defined(HAVE_W32_SYSTEM) && !defined(HAVE_BROKEN_TTYNAME)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if ((!dft_ttyname || !*dft_ttyname) && ttyname (0))
|
|
|
|
|
dft_ttyname = ttyname (0);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
#endif
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (dft_ttyname && *dft_ttyname)
|
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
if ((rc=agent_send_option (ctx, "ttyname", dft_ttyname)))
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dft_ttytype = getenv ("TERM");
|
|
|
|
|
if (dft_ttyname && dft_ttytype)
|
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
if ((rc = agent_send_option (ctx, "ttytype", dft_ttytype)))
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
#if defined(HAVE_SETLOCALE)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
char *old_lc = NULL;
|
|
|
|
|
char *dft_lc = NULL;
|
|
|
|
|
|
|
|
|
|
#if defined(LC_CTYPE)
|
|
|
|
|
old_lc = setlocale (LC_CTYPE, NULL);
|
|
|
|
|
if (old_lc)
|
|
|
|
|
{
|
|
|
|
|
char *p = spwq_malloc (strlen (old_lc)+1);
|
|
|
|
|
if (!p)
|
|
|
|
|
return SPWQ_OUT_OF_CORE;
|
|
|
|
|
strcpy (p, old_lc);
|
|
|
|
|
old_lc = p;
|
|
|
|
|
}
|
|
|
|
|
dft_lc = setlocale (LC_CTYPE, "");
|
|
|
|
|
if (dft_ttyname && dft_lc)
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = agent_send_option (ctx, "lc-ctype", dft_lc);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (old_lc)
|
|
|
|
|
{
|
|
|
|
|
setlocale (LC_CTYPE, old_lc);
|
|
|
|
|
spwq_free (old_lc);
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(LC_MESSAGES)
|
|
|
|
|
old_lc = setlocale (LC_MESSAGES, NULL);
|
|
|
|
|
if (old_lc)
|
|
|
|
|
{
|
|
|
|
|
char *p = spwq_malloc (strlen (old_lc)+1);
|
|
|
|
|
if (!p)
|
|
|
|
|
return SPWQ_OUT_OF_CORE;
|
|
|
|
|
strcpy (p, old_lc);
|
|
|
|
|
old_lc = p;
|
|
|
|
|
}
|
|
|
|
|
dft_lc = setlocale (LC_MESSAGES, "");
|
|
|
|
|
if (dft_ttyname && dft_lc)
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = agent_send_option (ctx, "lc-messages", dft_lc);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (old_lc)
|
|
|
|
|
{
|
|
|
|
|
setlocale (LC_MESSAGES, old_lc);
|
|
|
|
|
spwq_free (old_lc);
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /*HAVE_SETLOCALE*/
|
|
|
|
|
|
2008-03-06 18:28:47 +00:00
|
|
|
|
/* Send the XAUTHORITY variable. */
|
|
|
|
|
dft_xauthority = getenv ("XAUTHORITY");
|
|
|
|
|
if (dft_xauthority)
|
|
|
|
|
{
|
|
|
|
|
/* We ignore errors here because older gpg-agents don't support
|
|
|
|
|
this option. */
|
2016-08-11 12:26:09 +02:00
|
|
|
|
agent_send_option (ctx, "xauthority", dft_xauthority);
|
2008-03-06 18:28:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Send the PINENTRY_USER_DATA variable. */
|
|
|
|
|
dft_pinentry_user_data = getenv ("PINENTRY_USER_DATA");
|
|
|
|
|
if (dft_pinentry_user_data)
|
|
|
|
|
{
|
|
|
|
|
/* We ignore errors here because older gpg-agents don't support
|
|
|
|
|
this option. */
|
2016-08-11 12:26:09 +02:00
|
|
|
|
agent_send_option (ctx, "pinentry-user-data", dft_pinentry_user_data);
|
2008-03-06 18:28:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
/* Tell the agent that we support Pinentry notifications. No
|
|
|
|
|
error checking so that it will work with older agents. */
|
|
|
|
|
assuan_transact (ctx, "OPTION allow-pinentry-notify",
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Try to open a connection to the agent, send all options and return
|
|
|
|
|
the file descriptor for the connection. Return -1 in case of
|
|
|
|
|
error. */
|
|
|
|
|
static int
|
2016-08-11 12:26:09 +02:00
|
|
|
|
agent_open (assuan_context_t *ctx)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2016-08-22 11:09:42 +02:00
|
|
|
|
char *infostr;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2014-10-03 11:58:58 +02:00
|
|
|
|
infostr = default_gpg_agent_info;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if ( !infostr || !*infostr )
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef SPWQ_USE_LOGGING
|
2014-11-19 10:24:56 +01:00
|
|
|
|
log_error (_("no gpg-agent running in this session\n"));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#endif
|
2018-10-24 20:22:17 +02:00
|
|
|
|
*ctx = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return SPWQ_NO_AGENT;
|
|
|
|
|
}
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = assuan_new (ctx);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = assuan_socket_connect (*ctx, infostr, 0, 0);
|
|
|
|
|
if (rc)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef SPWQ_USE_LOGGING
|
2016-06-30 17:23:48 +02:00
|
|
|
|
log_error (_("can't connect to '%s': %s\n"),
|
2016-08-11 12:26:09 +02:00
|
|
|
|
infostr, gpg_strerror (rc));
|
2003-08-05 17:11:04 +00:00
|
|
|
|
#endif
|
2016-08-11 12:26:09 +02:00
|
|
|
|
goto errout;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = agent_send_all_options (*ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
#ifdef SPWQ_USE_LOGGING
|
|
|
|
|
log_error (_("problem setting the gpg-agent options\n"));
|
|
|
|
|
#endif
|
2016-08-11 12:26:09 +02:00
|
|
|
|
goto errout;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2016-08-11 12:26:09 +02:00
|
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
assuan_release (*ctx);
|
|
|
|
|
*ctx = NULL;
|
2016-08-22 11:09:42 +02:00
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 16:26:32 +00:00
|
|
|
|
/* Copy text to BUFFER and escape as required. Return a pointer to
|
2004-12-21 19:05:15 +00:00
|
|
|
|
the end of the new buffer. Note that BUFFER must be large enough
|
2003-08-05 17:11:04 +00:00
|
|
|
|
to keep the entire text; allocataing it 3 times the size of TEXT
|
|
|
|
|
is sufficient. */
|
|
|
|
|
static char *
|
|
|
|
|
copy_and_escape (char *buffer, const char *text)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2005-06-16 08:12:03 +00:00
|
|
|
|
const unsigned char *s = (unsigned char *)text;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *p = buffer;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2004-04-13 09:45:23 +00:00
|
|
|
|
for (i=0; s[i]; i++)
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-04-13 09:45:23 +00:00
|
|
|
|
if (s[i] < ' ' || s[i] == '+')
|
2003-08-05 17:11:04 +00:00
|
|
|
|
{
|
2004-04-13 09:45:23 +00:00
|
|
|
|
sprintf (p, "%%%02X", s[i]);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
p += 3;
|
|
|
|
|
}
|
2004-04-13 09:45:23 +00:00
|
|
|
|
else if (s[i] == ' ')
|
2003-08-05 17:11:04 +00:00
|
|
|
|
*p++ = '+';
|
|
|
|
|
else
|
2004-04-13 09:45:23 +00:00
|
|
|
|
*p++ = s[i];
|
2003-08-05 17:11:04 +00:00
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-06-14 17:05:07 +00:00
|
|
|
|
/* Set the name of the default socket to NAME. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
int
|
2007-06-14 17:05:07 +00:00
|
|
|
|
simple_pw_set_socket (const char *name)
|
|
|
|
|
{
|
|
|
|
|
spwq_free (default_gpg_agent_info);
|
2016-08-11 12:26:09 +02:00
|
|
|
|
default_gpg_agent_info = NULL;
|
2007-06-14 17:05:07 +00:00
|
|
|
|
if (name)
|
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
default_gpg_agent_info = spwq_malloc (strlen (name) + 1);
|
2007-06-14 17:05:07 +00:00
|
|
|
|
if (!default_gpg_agent_info)
|
|
|
|
|
return SPWQ_OUT_OF_CORE;
|
2016-08-11 12:26:09 +02:00
|
|
|
|
strcpy (default_gpg_agent_info, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the default inquiry callback. It merely handles the
|
|
|
|
|
Pinentry notification. */
|
|
|
|
|
static gpg_error_t
|
|
|
|
|
default_inq_cb (void *opaque, const char *line)
|
|
|
|
|
{
|
|
|
|
|
(void)opaque;
|
|
|
|
|
|
|
|
|
|
if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17]))
|
|
|
|
|
{
|
|
|
|
|
gnupg_allow_set_foregound_window ((pid_t)strtoul (line+17, NULL, 10));
|
|
|
|
|
/* We do not return errors to avoid breaking other code. */
|
2007-06-14 17:05:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2016-08-11 12:26:09 +02:00
|
|
|
|
{
|
|
|
|
|
#ifdef SPWQ_USE_LOGGING
|
|
|
|
|
log_debug ("ignoring gpg-agent inquiry '%s'\n", line);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2007-06-14 17:05:07 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
/* Ask the gpg-agent for a passphrase and present the user with a
|
2007-01-25 09:15:10 +00:00
|
|
|
|
DESCRIPTION, a PROMPT and optionally with a TRYAGAIN extra text.
|
2017-02-20 16:19:50 -05:00
|
|
|
|
If a CACHEID is not NULL it is used to locate the passphrase in
|
2007-01-25 09:15:10 +00:00
|
|
|
|
the cache and store it under this ID. If OPT_CHECK is true
|
|
|
|
|
gpg-agent is asked to apply some checks on the passphrase security.
|
|
|
|
|
If ERRORCODE is not NULL it should point a variable receiving an
|
2007-06-14 17:05:07 +00:00
|
|
|
|
errorcode; this error code might be 0 if the user canceled the
|
2007-01-25 09:15:10 +00:00
|
|
|
|
operation. The function returns NULL to indicate an error. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *
|
2011-02-04 12:57:53 +01:00
|
|
|
|
simple_pwquery (const char *cacheid,
|
2003-08-05 17:11:04 +00:00
|
|
|
|
const char *tryagain,
|
|
|
|
|
const char *prompt,
|
|
|
|
|
const char *description,
|
2007-01-25 09:15:10 +00:00
|
|
|
|
int opt_check,
|
2003-08-05 17:11:04 +00:00
|
|
|
|
int *errorcode)
|
|
|
|
|
{
|
2016-08-22 11:09:42 +02:00
|
|
|
|
int rc;
|
2016-08-11 12:26:09 +02:00
|
|
|
|
assuan_context_t ctx;
|
|
|
|
|
membuf_t data;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
char *result = NULL;
|
|
|
|
|
char *pw = NULL;
|
|
|
|
|
char *p;
|
2016-08-22 11:09:42 +02:00
|
|
|
|
size_t n;
|
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = agent_open (&ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
|
|
if (!cacheid)
|
|
|
|
|
cacheid = "X";
|
|
|
|
|
if (!tryagain)
|
|
|
|
|
tryagain = "X";
|
|
|
|
|
if (!prompt)
|
|
|
|
|
prompt = "X";
|
|
|
|
|
if (!description)
|
|
|
|
|
description = "X";
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
char *line;
|
|
|
|
|
/* We allocate 3 times the needed space so that there is enough
|
|
|
|
|
space for escaping. */
|
2007-01-25 09:15:10 +00:00
|
|
|
|
line = spwq_malloc (15 + 10
|
2003-08-05 17:11:04 +00:00
|
|
|
|
+ 3*strlen (cacheid) + 1
|
|
|
|
|
+ 3*strlen (tryagain) + 1
|
|
|
|
|
+ 3*strlen (prompt) + 1
|
|
|
|
|
+ 3*strlen (description) + 1
|
|
|
|
|
+ 2);
|
|
|
|
|
if (!line)
|
|
|
|
|
{
|
|
|
|
|
rc = SPWQ_OUT_OF_CORE;
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
strcpy (line, "GET_PASSPHRASE ");
|
|
|
|
|
p = line+15;
|
2007-01-25 09:15:10 +00:00
|
|
|
|
if (opt_check)
|
|
|
|
|
p = stpcpy (p, "--check ");
|
2003-08-05 17:11:04 +00:00
|
|
|
|
p = copy_and_escape (p, cacheid);
|
|
|
|
|
*p++ = ' ';
|
|
|
|
|
p = copy_and_escape (p, tryagain);
|
|
|
|
|
*p++ = ' ';
|
|
|
|
|
p = copy_and_escape (p, prompt);
|
|
|
|
|
*p++ = ' ';
|
|
|
|
|
p = copy_and_escape (p, description);
|
|
|
|
|
*p++ = '\n';
|
2016-08-11 12:26:09 +02:00
|
|
|
|
|
|
|
|
|
init_membuf_secure (&data, 64);
|
|
|
|
|
|
|
|
|
|
rc = assuan_transact (ctx, line, put_membuf_cb, &data,
|
|
|
|
|
default_inq_cb, NULL, NULL, NULL);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
spwq_free (line);
|
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
/* Older Pinentries return the old assuan error code for canceled
|
|
|
|
|
which gets translated by libassuan to GPG_ERR_ASS_CANCELED and
|
|
|
|
|
not to the code for a user cancel. Fix this here. */
|
|
|
|
|
if (rc && gpg_err_source (rc)
|
|
|
|
|
&& gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
|
|
|
|
|
rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_CANCELED);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
p = get_membuf (&data, &n);
|
|
|
|
|
if (p)
|
|
|
|
|
wipememory (p, n);
|
|
|
|
|
spwq_free (p);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
put_membuf (&data, "", 1);
|
|
|
|
|
result = get_membuf (&data, NULL);
|
|
|
|
|
if (pw == NULL)
|
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
|
leave:
|
|
|
|
|
if (errorcode)
|
|
|
|
|
*errorcode = rc;
|
2016-08-11 12:26:09 +02:00
|
|
|
|
assuan_release (ctx);
|
2003-08-05 17:11:04 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2004-12-21 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
2005-07-04 04:55:48 +00:00
|
|
|
|
/* Ask the gpg-agent to clear the passphrase for the cache ID CACHEID. */
|
|
|
|
|
int
|
|
|
|
|
simple_pwclear (const char *cacheid)
|
|
|
|
|
{
|
|
|
|
|
char line[500];
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
/* We need not more than 50 characters for the command and the
|
|
|
|
|
terminating nul. */
|
|
|
|
|
if (strlen (cacheid) * 3 > sizeof (line) - 50)
|
|
|
|
|
return SPWQ_PROTOCOL_ERROR;
|
|
|
|
|
|
|
|
|
|
strcpy (line, "CLEAR_PASSPHRASE ");
|
|
|
|
|
p = line + 17;
|
|
|
|
|
p = copy_and_escape (p, cacheid);
|
|
|
|
|
*p++ = '\n';
|
|
|
|
|
*p++ = '\0';
|
|
|
|
|
|
|
|
|
|
return simple_query (line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-12-21 19:05:15 +00:00
|
|
|
|
/* Perform the simple query QUERY (which must be new-line and 0
|
|
|
|
|
terminated) and return the error code. */
|
|
|
|
|
int
|
|
|
|
|
simple_query (const char *query)
|
|
|
|
|
{
|
2016-08-11 12:26:09 +02:00
|
|
|
|
assuan_context_t ctx;
|
2004-12-21 19:05:15 +00:00
|
|
|
|
int rc;
|
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = agent_open (&ctx);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
if (rc)
|
2016-08-11 12:26:09 +02:00
|
|
|
|
return rc;
|
2016-02-12 22:12:21 +01:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
rc = assuan_transact (ctx, query, NULL, NULL, NULL, NULL, NULL, NULL);
|
2016-02-12 22:12:21 +01:00
|
|
|
|
|
2016-08-11 12:26:09 +02:00
|
|
|
|
assuan_release (ctx);
|
2004-12-21 19:05:15 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|