2006-09-06 16:35:52 +00:00
|
|
|
|
/* call-pinnetry.c - fork of the pinentry to query stuff from the user
|
2004-02-03 16:24:37 +00:00
|
|
|
|
* Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
|
2003-06-05 07:14:21 +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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (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
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-06-20 17:21:37 +00:00
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
|
* USA.
|
2003-06-05 07:14:21 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/stat.h>
|
2005-11-28 11:52:25 +00:00
|
|
|
|
#ifndef HAVE_W32_SYSTEM
|
|
|
|
|
#include <sys/wait.h>
|
2003-06-05 07:14:21 +00:00
|
|
|
|
#endif
|
2005-11-28 11:52:25 +00:00
|
|
|
|
#include <pth.h>
|
2006-09-06 16:35:52 +00:00
|
|
|
|
#include <assuan.h>
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
#include "agent.h"
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
#ifdef _POSIX_OPEN_MAX
|
|
|
|
|
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
|
|
|
|
|
#else
|
|
|
|
|
#define MAX_OPEN_FDS 20
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-08-20 13:03:32 +00:00
|
|
|
|
|
|
|
|
|
/* Because access to the pinentry must be serialized (it is and shall
|
|
|
|
|
be a global mutual dialog) we should better timeout further
|
|
|
|
|
requests after some time. 2 minutes seem to be a reasonable
|
|
|
|
|
time. */
|
|
|
|
|
#define LOCK_TIMEOUT (1*60)
|
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
/* The assuan context of the current pinentry. */
|
|
|
|
|
static assuan_context_t entry_ctx;
|
2004-08-20 13:03:32 +00:00
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
/* The control variable of the connection owning the current pinentry.
|
|
|
|
|
This is only valid if ENTRY_CTX is not NULL. Note, that we care
|
|
|
|
|
only about the value of the pointer and that it should never be
|
|
|
|
|
dereferenced. */
|
|
|
|
|
static ctrl_t entry_owner;
|
|
|
|
|
|
|
|
|
|
/* A mutex used to serialize access to the pinentry. */
|
2004-12-19 17:44:20 +00:00
|
|
|
|
static pth_mutex_t entry_lock;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
/* The thread ID of the popup working thread. */
|
|
|
|
|
static pth_t popup_tid;
|
|
|
|
|
|
|
|
|
|
/* A flag used in communication between the popup working thread and
|
|
|
|
|
its stop function. */
|
|
|
|
|
static int popup_finished;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Data to be passed to our callbacks, */
|
|
|
|
|
struct entry_parm_s
|
|
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
|
int lines;
|
|
|
|
|
size_t size;
|
2005-06-16 08:12:03 +00:00
|
|
|
|
unsigned char *buffer;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-19 17:44:20 +00:00
|
|
|
|
/* This function must be called once to initialize this module. This
|
|
|
|
|
has to be done before a second thread is spawned. We can't do the
|
|
|
|
|
static initialization because Pth emulation code might not be able
|
2005-11-28 11:52:25 +00:00
|
|
|
|
to do a static init; in particular, it is not possible for W32. */
|
2004-12-19 17:44:20 +00:00
|
|
|
|
void
|
|
|
|
|
initialize_module_query (void)
|
|
|
|
|
{
|
|
|
|
|
static int initialized;
|
|
|
|
|
|
|
|
|
|
if (!initialized)
|
2005-11-28 11:52:25 +00:00
|
|
|
|
{
|
|
|
|
|
if (pth_mutex_init (&entry_lock))
|
|
|
|
|
initialized = 1;
|
|
|
|
|
}
|
2004-12-19 17:44:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-06-07 19:09:18 +00:00
|
|
|
|
static void
|
|
|
|
|
dump_mutex_state (pth_mutex_t *m)
|
|
|
|
|
{
|
|
|
|
|
if (!(m->mx_state & PTH_MUTEX_INITIALIZED))
|
|
|
|
|
log_printf ("not_initialized");
|
|
|
|
|
else if (!(m->mx_state & PTH_MUTEX_LOCKED))
|
|
|
|
|
log_printf ("not_locked");
|
|
|
|
|
else
|
|
|
|
|
log_printf ("locked tid=0x%lx count=%lu", (long)m->mx_owner, m->mx_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function may be called to print infromation pertaining to the
|
|
|
|
|
current state of this module to the log. */
|
|
|
|
|
void
|
|
|
|
|
agent_query_dump_state (void)
|
|
|
|
|
{
|
|
|
|
|
log_info ("agent_query_dump_state: entry_lock=");
|
|
|
|
|
dump_mutex_state (&entry_lock);
|
|
|
|
|
log_printf ("\n");
|
2005-11-28 11:52:25 +00:00
|
|
|
|
log_info ("agent_query_dump_state: entry_ctx=%p pid=%ld popup_tid=%p\n",
|
|
|
|
|
entry_ctx, (long)assuan_get_pid (entry_ctx), popup_tid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called to make sure that a popup window owned by the current
|
|
|
|
|
connection gets closed. */
|
|
|
|
|
void
|
|
|
|
|
agent_reset_query (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
if (entry_ctx && popup_tid && entry_owner == ctrl)
|
|
|
|
|
{
|
|
|
|
|
agent_popup_message_stop (ctrl);
|
|
|
|
|
}
|
2005-06-07 19:09:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-12-19 17:44:20 +00:00
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
/* Unlock the pinentry so that another thread can start one and
|
|
|
|
|
disconnect that pinentry - we do this after the unlock so that a
|
|
|
|
|
stalled pinentry does not block other threads. Fixme: We should
|
2005-02-23 21:06:32 +00:00
|
|
|
|
have a timeout in Assuan for the disconnect operation. */
|
2003-06-05 07:14:21 +00:00
|
|
|
|
static int
|
|
|
|
|
unlock_pinentry (int rc)
|
|
|
|
|
{
|
2005-06-07 19:09:18 +00:00
|
|
|
|
assuan_context_t ctx = entry_ctx;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2005-06-07 19:09:18 +00:00
|
|
|
|
entry_ctx = NULL;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (!pth_mutex_release (&entry_lock))
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to release the entry lock\n");
|
|
|
|
|
if (!rc)
|
|
|
|
|
rc = gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
|
}
|
|
|
|
|
assuan_disconnect (ctx);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-24 20:50:49 +00:00
|
|
|
|
|
|
|
|
|
/* To make sure we leave no secrets in our image after forking of the
|
|
|
|
|
pinentry, we use this callback. */
|
|
|
|
|
static void
|
|
|
|
|
atfork_cb (void *opaque, int where)
|
|
|
|
|
{
|
|
|
|
|
if (!where)
|
|
|
|
|
gcry_control (GCRYCTL_TERM_SECMEM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
/* Fork off the pin entry if this has not already been done. Note,
|
|
|
|
|
that this function must always be used to aquire the lock for the
|
|
|
|
|
pinentry - we will serialize _all_ pinentry calls.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2005-11-28 11:52:25 +00:00
|
|
|
|
start_pinentry (ctrl_t ctrl)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
const char *pgmname;
|
2006-09-06 16:35:52 +00:00
|
|
|
|
assuan_context_t ctx;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
const char *argv[5];
|
|
|
|
|
int no_close_list[3];
|
|
|
|
|
int i;
|
2005-11-28 11:52:25 +00:00
|
|
|
|
pth_event_t evt;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
evt = pth_event (PTH_EVENT_TIME, pth_timeout (LOCK_TIMEOUT, 0));
|
|
|
|
|
if (!pth_mutex_acquire (&entry_lock, 0, evt))
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
2004-08-20 13:03:32 +00:00
|
|
|
|
if (pth_event_occurred (evt))
|
|
|
|
|
rc = gpg_error (GPG_ERR_TIMEOUT);
|
|
|
|
|
else
|
|
|
|
|
rc = gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
|
pth_event_free (evt, PTH_FREE_THIS);
|
|
|
|
|
log_error (_("failed to acquire the pinentry lock: %s\n"),
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2005-11-28 11:52:25 +00:00
|
|
|
|
pth_event_free (evt, PTH_FREE_THIS);
|
|
|
|
|
|
|
|
|
|
entry_owner = ctrl;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
if (entry_ctx)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("starting a new PIN Entry\n");
|
|
|
|
|
|
|
|
|
|
if (fflush (NULL))
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
|
|
|
|
|
log_error ("error flushing pending output: %s\n", strerror (errno));
|
|
|
|
|
return unlock_pinentry (tmperr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!opt.pinentry_program || !*opt.pinentry_program)
|
|
|
|
|
opt.pinentry_program = GNUPG_DEFAULT_PINENTRY;
|
|
|
|
|
if ( !(pgmname = strrchr (opt.pinentry_program, '/')))
|
|
|
|
|
pgmname = opt.pinentry_program;
|
|
|
|
|
else
|
|
|
|
|
pgmname++;
|
|
|
|
|
|
|
|
|
|
argv[0] = pgmname;
|
2004-12-17 20:41:50 +00:00
|
|
|
|
if (ctrl->display && !opt.keep_display)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
argv[1] = "--display";
|
|
|
|
|
argv[2] = ctrl->display;
|
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
argv[1] = NULL;
|
|
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
|
if (!opt.running_detached)
|
|
|
|
|
{
|
|
|
|
|
if (log_get_fd () != -1)
|
|
|
|
|
no_close_list[i++] = log_get_fd ();
|
|
|
|
|
no_close_list[i++] = fileno (stderr);
|
|
|
|
|
}
|
|
|
|
|
no_close_list[i] = -1;
|
|
|
|
|
|
2004-01-24 20:50:49 +00:00
|
|
|
|
/* Connect to the pinentry and perform initial handshaking */
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = assuan_pipe_connect_ext (&ctx, opt.pinentry_program, argv,
|
|
|
|
|
no_close_list, atfork_cb, NULL, 0);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't connect to the PIN entry module: %s\n",
|
2006-09-06 16:35:52 +00:00
|
|
|
|
gpg_strerror (rc));
|
2003-06-05 07:14:21 +00:00
|
|
|
|
return unlock_pinentry (gpg_error (GPG_ERR_NO_PIN_ENTRY));
|
|
|
|
|
}
|
|
|
|
|
entry_ctx = ctx;
|
|
|
|
|
|
|
|
|
|
if (DBG_ASSUAN)
|
|
|
|
|
log_debug ("connection to PIN entry established\n");
|
|
|
|
|
|
|
|
|
|
rc = assuan_transact (entry_ctx,
|
|
|
|
|
opt.no_grab? "OPTION no-grab":"OPTION grab",
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2004-12-17 20:41:50 +00:00
|
|
|
|
if (ctrl->ttyname)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
char *optstr;
|
|
|
|
|
if (asprintf (&optstr, "OPTION ttyname=%s", ctrl->ttyname) < 0 )
|
|
|
|
|
return unlock_pinentry (out_of_core ());
|
|
|
|
|
rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
free (optstr);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2004-12-17 20:41:50 +00:00
|
|
|
|
if (ctrl->ttytype)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
char *optstr;
|
|
|
|
|
if (asprintf (&optstr, "OPTION ttytype=%s", ctrl->ttytype) < 0 )
|
|
|
|
|
return unlock_pinentry (out_of_core ());
|
|
|
|
|
rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2004-12-17 20:41:50 +00:00
|
|
|
|
if (ctrl->lc_ctype)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
char *optstr;
|
|
|
|
|
if (asprintf (&optstr, "OPTION lc-ctype=%s", ctrl->lc_ctype) < 0 )
|
|
|
|
|
return unlock_pinentry (out_of_core ());
|
|
|
|
|
rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
2004-12-17 20:41:50 +00:00
|
|
|
|
if (ctrl->lc_messages)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
char *optstr;
|
|
|
|
|
if (asprintf (&optstr, "OPTION lc-messages=%s", ctrl->lc_messages) < 0 )
|
|
|
|
|
return unlock_pinentry (out_of_core ());
|
|
|
|
|
rc = assuan_transact (entry_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
|
NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-19 14:22:06 +00:00
|
|
|
|
/* Returns True is the pinentry is currently active. If WAITSECONDS is
|
|
|
|
|
greater than zero the function will wait for this many seconds
|
|
|
|
|
before returning. */
|
|
|
|
|
int
|
|
|
|
|
pinentry_active_p (ctrl_t ctrl, int waitseconds)
|
|
|
|
|
{
|
|
|
|
|
if (waitseconds > 0)
|
|
|
|
|
{
|
|
|
|
|
pth_event_t evt;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
evt = pth_event (PTH_EVENT_TIME, pth_timeout (waitseconds, 0));
|
|
|
|
|
if (!pth_mutex_acquire (&entry_lock, 0, evt))
|
|
|
|
|
{
|
|
|
|
|
if (pth_event_occurred (evt))
|
|
|
|
|
rc = gpg_error (GPG_ERR_TIMEOUT);
|
|
|
|
|
else
|
|
|
|
|
rc = gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
|
pth_event_free (evt, PTH_FREE_THIS);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
pth_event_free (evt, PTH_FREE_THIS);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!pth_mutex_acquire (&entry_lock, 1, NULL))
|
|
|
|
|
return gpg_error (GPG_ERR_LOCKED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pth_mutex_release (&entry_lock))
|
|
|
|
|
log_error ("failed to release the entry lock at %d\n", __LINE__);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
static int
|
2003-06-05 07:14:21 +00:00
|
|
|
|
getpin_cb (void *opaque, const void *buffer, size_t length)
|
|
|
|
|
{
|
|
|
|
|
struct entry_parm_s *parm = opaque;
|
|
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* we expect the pin to fit on one line */
|
|
|
|
|
if (parm->lines || length >= parm->size)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return gpg_error (GPG_ERR_ASS_TOO_MUCH_DATA);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
/* fixme: we should make sure that the assuan buffer is allocated in
|
|
|
|
|
secure memory or read the response byte by byte */
|
|
|
|
|
memcpy (parm->buffer, buffer, length);
|
|
|
|
|
parm->buffer[length] = 0;
|
|
|
|
|
parm->lines++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
all_digitsp( const char *s)
|
|
|
|
|
{
|
|
|
|
|
for (; *s && *s >= '0' && *s <= '9'; s++)
|
|
|
|
|
;
|
|
|
|
|
return !*s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Call the Entry and ask for the PIN. We do check for a valid PIN
|
|
|
|
|
number here and repeat it as long as we have invalid formed
|
|
|
|
|
numbers. */
|
|
|
|
|
int
|
2005-05-24 12:37:36 +00:00
|
|
|
|
agent_askpin (ctrl_t ctrl,
|
|
|
|
|
const char *desc_text, const char *prompt_text,
|
|
|
|
|
const char *initial_errtext,
|
2004-02-21 13:05:22 +00:00
|
|
|
|
struct pin_entry_info_s *pininfo)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char line[ASSUAN_LINELENGTH];
|
|
|
|
|
struct entry_parm_s parm;
|
|
|
|
|
const char *errtext = NULL;
|
|
|
|
|
int is_pin = 0;
|
|
|
|
|
|
|
|
|
|
if (opt.batch)
|
|
|
|
|
return 0; /* fixme: we should return BAD PIN */
|
|
|
|
|
|
|
|
|
|
if (!pininfo || pininfo->max_length < 1)
|
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
if (!desc_text && pininfo->min_digits)
|
|
|
|
|
desc_text = _("Please enter your PIN, so that the secret key "
|
|
|
|
|
"can be unlocked for this session");
|
|
|
|
|
else if (!desc_text)
|
|
|
|
|
desc_text = _("Please enter your passphrase, so that the secret key "
|
|
|
|
|
"can be unlocked for this session");
|
|
|
|
|
|
2005-05-24 12:37:36 +00:00
|
|
|
|
if (prompt_text)
|
|
|
|
|
is_pin = !!strstr (prompt_text, "PIN");
|
|
|
|
|
else
|
|
|
|
|
is_pin = desc_text && strstr (desc_text, "PIN");
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
rc = start_pinentry (ctrl);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETDESC %s", desc_text);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2005-05-24 12:37:36 +00:00
|
|
|
|
snprintf (line, DIM(line)-1, "SETPROMPT %s",
|
|
|
|
|
prompt_text? prompt_text : is_pin? "PIN:" : "Passphrase:");
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
2004-02-21 13:05:22 +00:00
|
|
|
|
|
|
|
|
|
if (initial_errtext)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETERROR %s", initial_errtext);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line,
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2004-02-21 13:05:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
for (;pininfo->failed_tries < pininfo->max_tries; pininfo->failed_tries++)
|
|
|
|
|
{
|
|
|
|
|
memset (&parm, 0, sizeof parm);
|
|
|
|
|
parm.size = pininfo->max_length;
|
2005-06-16 08:12:03 +00:00
|
|
|
|
parm.buffer = (unsigned char*)pininfo->pin;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
if (errtext)
|
|
|
|
|
{
|
|
|
|
|
/* fixme: should we show the try count? It must be translated */
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETERROR %s (try %d of %d)",
|
|
|
|
|
errtext, pininfo->failed_tries+1, pininfo->max_tries);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
2004-02-21 13:05:22 +00:00
|
|
|
|
rc = assuan_transact (entry_ctx, line,
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
errtext = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = assuan_transact (entry_ctx, "GETPIN", getpin_cb, &parm,
|
|
|
|
|
NULL, NULL, NULL, NULL);
|
2006-10-04 16:45:04 +00:00
|
|
|
|
/* Most pinentries out in the wild return the old Assuan error code
|
|
|
|
|
for canceled which gets translated to an assuan Cancel error 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);
|
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_ASS_TOO_MUCH_DATA)
|
2003-06-05 07:14:21 +00:00
|
|
|
|
errtext = is_pin? _("PIN too long")
|
|
|
|
|
: _("Passphrase too long");
|
|
|
|
|
else if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
if (!errtext && pininfo->min_digits)
|
|
|
|
|
{
|
|
|
|
|
/* do some basic checks on the entered PIN. */
|
|
|
|
|
if (!all_digitsp (pininfo->pin))
|
|
|
|
|
errtext = _("Invalid characters in PIN");
|
|
|
|
|
else if (pininfo->max_digits
|
|
|
|
|
&& strlen (pininfo->pin) > pininfo->max_digits)
|
|
|
|
|
errtext = _("PIN too long");
|
|
|
|
|
else if (strlen (pininfo->pin) < pininfo->min_digits)
|
|
|
|
|
errtext = _("PIN too short");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!errtext && pininfo->check_cb)
|
|
|
|
|
{
|
|
|
|
|
/* More checks by utilizing the optional callback. */
|
|
|
|
|
pininfo->cb_errtext = NULL;
|
|
|
|
|
rc = pininfo->check_cb (pininfo);
|
|
|
|
|
if (rc == -1 && pininfo->cb_errtext)
|
|
|
|
|
errtext = pininfo->cb_errtext;
|
|
|
|
|
else if (gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
|
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_BAD_PIN)
|
|
|
|
|
errtext = (is_pin? _("Bad PIN")
|
|
|
|
|
: _("Bad Passphrase"));
|
|
|
|
|
else if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!errtext)
|
|
|
|
|
return unlock_pinentry (0); /* okay, got a PIN or passphrase */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unlock_pinentry (gpg_error (pininfo->min_digits? GPG_ERR_BAD_PIN
|
|
|
|
|
: GPG_ERR_BAD_PASSPHRASE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-04 16:45:04 +00:00
|
|
|
|
/* Ask for the passphrase using the supplied arguments. The returned
|
|
|
|
|
passphrase needs to be freed by the caller. */
|
2003-06-05 07:14:21 +00:00
|
|
|
|
int
|
2005-11-28 11:52:25 +00:00
|
|
|
|
agent_get_passphrase (ctrl_t ctrl,
|
2003-06-05 07:14:21 +00:00
|
|
|
|
char **retpass, const char *desc, const char *prompt,
|
|
|
|
|
const char *errtext)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
char line[ASSUAN_LINELENGTH];
|
|
|
|
|
struct entry_parm_s parm;
|
|
|
|
|
|
|
|
|
|
*retpass = NULL;
|
|
|
|
|
if (opt.batch)
|
|
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
|
|
|
|
|
|
|
|
|
rc = start_pinentry (ctrl);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if (!prompt)
|
|
|
|
|
prompt = desc && strstr (desc, "PIN")? "PIN": _("Passphrase");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETDESC %s", desc);
|
|
|
|
|
else
|
|
|
|
|
snprintf (line, DIM(line)-1, "RESET");
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETPROMPT %s", prompt);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
if (errtext)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETERROR %s", errtext);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset (&parm, 0, sizeof parm);
|
|
|
|
|
parm.size = ASSUAN_LINELENGTH/2 - 5;
|
|
|
|
|
parm.buffer = gcry_malloc_secure (parm.size+10);
|
|
|
|
|
if (!parm.buffer)
|
|
|
|
|
return unlock_pinentry (out_of_core ());
|
|
|
|
|
|
|
|
|
|
assuan_begin_confidential (entry_ctx);
|
2006-10-04 16:45:04 +00:00
|
|
|
|
rc = assuan_transact (entry_ctx, "GETPIN", getpin_cb, &parm,
|
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
|
/* Most pinentries out in the wild return the old Assuan error code
|
|
|
|
|
for canceled which gets translated to an assuan Cancel error 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);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (rc)
|
2006-10-04 16:45:04 +00:00
|
|
|
|
xfree (parm.buffer);
|
|
|
|
|
else
|
|
|
|
|
*retpass = parm.buffer;
|
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Pop up the PIN-entry, display the text and the prompt and ask the
|
2005-11-28 11:52:25 +00:00
|
|
|
|
user to confirm this. We return 0 for success, ie. the user
|
2003-06-05 07:14:21 +00:00
|
|
|
|
confirmed it, GPG_ERR_NOT_CONFIRMED for what the text says or an
|
|
|
|
|
other error. */
|
|
|
|
|
int
|
2005-11-28 11:52:25 +00:00
|
|
|
|
agent_get_confirmation (ctrl_t ctrl,
|
2003-06-05 07:14:21 +00:00
|
|
|
|
const char *desc, const char *ok, const char *cancel)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char line[ASSUAN_LINELENGTH];
|
|
|
|
|
|
|
|
|
|
rc = start_pinentry (ctrl);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETDESC %s", desc);
|
|
|
|
|
else
|
|
|
|
|
snprintf (line, DIM(line)-1, "RESET");
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
2006-10-04 16:45:04 +00:00
|
|
|
|
/* Most pinentries out in the wild return the old Assuan error code
|
|
|
|
|
for canceled which gets translated to an assuan Cancel error 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);
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETOK %s", ok);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
if (cancel)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETCANCEL %s", cancel);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = assuan_transact (entry_ctx, "CONFIRM", NULL, NULL, NULL, NULL, NULL, NULL);
|
2006-10-16 17:36:44 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2003-06-05 07:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-11-28 11:52:25 +00:00
|
|
|
|
/* The thread running the popup message. */
|
|
|
|
|
static void *
|
|
|
|
|
popup_message_thread (void *arg)
|
|
|
|
|
{
|
|
|
|
|
assuan_transact (entry_ctx, "CONFIRM", NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
popup_finished = 1;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Pop up a message window similar to the confirm one but keep it open
|
|
|
|
|
until agent_popup_message_stop has been called. It is crucial for
|
|
|
|
|
the caller to make sure that the stop function gets called as soon
|
2006-11-20 16:49:41 +00:00
|
|
|
|
as the message is not anymore required because the message is
|
2005-11-28 11:52:25 +00:00
|
|
|
|
system modal and all other attempts to use the pinentry will fail
|
|
|
|
|
(after a timeout). */
|
|
|
|
|
int
|
|
|
|
|
agent_popup_message_start (ctrl_t ctrl, const char *desc,
|
|
|
|
|
const char *ok_btn, const char *cancel_btn)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char line[ASSUAN_LINELENGTH];
|
|
|
|
|
pth_attr_t tattr;
|
|
|
|
|
|
|
|
|
|
rc = start_pinentry (ctrl);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETDESC %s", desc);
|
|
|
|
|
else
|
|
|
|
|
snprintf (line, DIM(line)-1, "RESET");
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2005-11-28 11:52:25 +00:00
|
|
|
|
|
|
|
|
|
if (ok_btn)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETOK %s", ok_btn);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL,NULL,NULL,NULL,NULL,NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2005-11-28 11:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
if (cancel_btn)
|
|
|
|
|
{
|
|
|
|
|
snprintf (line, DIM(line)-1, "SETCANCEL %s", cancel_btn);
|
|
|
|
|
line[DIM(line)-1] = 0;
|
|
|
|
|
rc = assuan_transact (entry_ctx, line, NULL,NULL,NULL,NULL,NULL,NULL);
|
|
|
|
|
if (rc)
|
2006-09-06 16:35:52 +00:00
|
|
|
|
return unlock_pinentry (rc);
|
2005-11-28 11:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tattr = pth_attr_new();
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_JOINABLE, 1);
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 256*1024);
|
|
|
|
|
pth_attr_set (tattr, PTH_ATTR_NAME, "popup-message");
|
|
|
|
|
|
|
|
|
|
popup_finished = 0;
|
|
|
|
|
popup_tid = pth_spawn (tattr, popup_message_thread, NULL);
|
|
|
|
|
if (!popup_tid)
|
|
|
|
|
{
|
2006-09-14 16:50:33 +00:00
|
|
|
|
rc = gpg_error_from_syserror ();
|
2005-11-28 11:52:25 +00:00
|
|
|
|
log_error ("error spawning popup message handler: %s\n",
|
|
|
|
|
strerror (errno) );
|
|
|
|
|
pth_attr_destroy (tattr);
|
|
|
|
|
return unlock_pinentry (rc);
|
|
|
|
|
}
|
|
|
|
|
pth_attr_destroy (tattr);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Close a popup window. */
|
|
|
|
|
void
|
|
|
|
|
agent_popup_message_stop (ctrl_t ctrl)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
|
|
if (!popup_tid || !entry_ctx)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("agent_popup_message_stop called with no active popup\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pid = assuan_get_pid (entry_ctx);
|
|
|
|
|
if (pid == (pid_t)(-1))
|
|
|
|
|
; /* No pid available can't send a kill. */
|
|
|
|
|
else if (popup_finished)
|
|
|
|
|
; /* Already finished and ready for joining. */
|
|
|
|
|
else if (pid && ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
|
|
|
|
|
{ /* The daemon already died. No need to send a kill. However
|
|
|
|
|
because we already waited for the process, we need to tell
|
|
|
|
|
assuan that it should not wait again (done by
|
|
|
|
|
unlock_pinentry). */
|
|
|
|
|
if (rc == pid)
|
|
|
|
|
assuan_set_flag (entry_ctx, ASSUAN_NO_WAITPID, 1);
|
|
|
|
|
}
|
2006-11-20 16:49:41 +00:00
|
|
|
|
else if (pid > 0)
|
|
|
|
|
kill (pid, SIGKILL); /* Need to use SIGKILL due to bad
|
|
|
|
|
interaction of SIGINT with Pth. */
|
2005-11-28 11:52:25 +00:00
|
|
|
|
|
|
|
|
|
/* Now wait for the thread to terminate. */
|
|
|
|
|
rc = pth_join (popup_tid, NULL);
|
|
|
|
|
if (!rc)
|
|
|
|
|
log_debug ("agent_popup_message_stop: pth_join failed: %s\n",
|
|
|
|
|
strerror (errno));
|
|
|
|
|
popup_tid = NULL;
|
|
|
|
|
entry_owner = NULL;
|
|
|
|
|
|
|
|
|
|
/* Now we can close the connection. */
|
|
|
|
|
unlock_pinentry (0);
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|