2004-12-21 20:05:15 +01:00
|
|
|
|
/* preset-passphrase.c - A tool to preset a passphrase.
|
|
|
|
|
* Copyright (C) 2004 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* 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 21:49:40 +02:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2004-12-21 20:05:15 +01: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/>.
|
2004-12-21 20:05:15 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
#include <fcntl.h> /* for setmode() */
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
|
# include <winsock2.h>
|
|
|
|
|
# endif
|
|
|
|
|
# include <windows.h> /* To initialize the sockets. fixme */
|
2004-12-21 20:05:15 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "agent.h"
|
|
|
|
|
#include "simple-pwquery.h"
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
#include "sysutils.h"
|
2012-02-06 20:50:47 +01:00
|
|
|
|
#include "../common/init.h"
|
2004-12-21 20:05:15 +01:00
|
|
|
|
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
enum cmd_and_opt_values
|
2004-12-21 20:05:15 +01:00
|
|
|
|
{ aNull = 0,
|
|
|
|
|
oVerbose = 'v',
|
|
|
|
|
oPassphrase = 'P',
|
|
|
|
|
|
|
|
|
|
oPreset = 'c',
|
|
|
|
|
oForget = 'f',
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
oNoVerbose = 500,
|
|
|
|
|
|
|
|
|
|
oHomedir,
|
|
|
|
|
|
|
|
|
|
aTest };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *opt_passphrase;
|
|
|
|
|
|
|
|
|
|
static ARGPARSE_OPTS opts[] = {
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
{ 301, NULL, 0, N_("@Options:\n ") },
|
|
|
|
|
|
|
|
|
|
{ oVerbose, "verbose", 0, "verbose" },
|
|
|
|
|
{ oPassphrase, "passphrase", 2, "|STRING|use passphrase STRING" },
|
|
|
|
|
{ oPreset, "preset", 256, "preset passphrase"},
|
|
|
|
|
{ oForget, "forget", 256, "forget passphrase"},
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
{ oHomedir, "homedir", 2, "@" },
|
2004-12-21 20:05:15 +01:00
|
|
|
|
{0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
|
my_strusage (int level)
|
|
|
|
|
{
|
|
|
|
|
const char *p;
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2013-11-18 14:09:47 +01:00
|
|
|
|
case 11: p = "gpg-preset-passphrase (@GNUPG@)";
|
2004-12-21 20:05:15 +01:00
|
|
|
|
break;
|
|
|
|
|
case 13: p = VERSION; break;
|
|
|
|
|
case 17: p = PRINTABLE_OS_NAME; break;
|
2009-07-21 16:21:05 +02:00
|
|
|
|
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
|
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
case 1:
|
2011-02-04 12:57:53 +01:00
|
|
|
|
case 40:
|
2004-12-21 20:05:15 +01:00
|
|
|
|
p = _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
|
|
|
|
|
break;
|
|
|
|
|
case 41:
|
|
|
|
|
p = _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
|
|
|
|
|
"Password cache maintenance\n");
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
default: p = NULL;
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
preset_passphrase (const char *keygrip)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char *line;
|
|
|
|
|
/* FIXME: Use secure memory. */
|
|
|
|
|
char passphrase[500];
|
2006-07-29 18:40:54 +02:00
|
|
|
|
char *passphrase_esc;
|
2004-12-21 20:05:15 +01:00
|
|
|
|
|
|
|
|
|
if (!opt_passphrase)
|
|
|
|
|
{
|
|
|
|
|
rc = read (0, passphrase, sizeof (passphrase) - 1);
|
|
|
|
|
if (rc < 0)
|
|
|
|
|
{
|
|
|
|
|
log_error ("reading passphrase failed: %s\n",
|
2006-09-14 18:50:33 +02:00
|
|
|
|
gpg_strerror (gpg_error_from_syserror ()));
|
2004-12-21 20:05:15 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
passphrase[rc] = '\0';
|
|
|
|
|
line = strchr (passphrase, '\n');
|
|
|
|
|
if (line)
|
|
|
|
|
{
|
|
|
|
|
if (line > passphrase && line[-1] == '\r')
|
|
|
|
|
line--;
|
|
|
|
|
*line = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: How to handle empty passwords? */
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 11:37:32 +02:00
|
|
|
|
{
|
|
|
|
|
const char *s = opt_passphrase ? opt_passphrase : passphrase;
|
|
|
|
|
passphrase_esc = bin2hex (s, strlen (s), NULL);
|
|
|
|
|
}
|
2006-07-29 18:40:54 +02:00
|
|
|
|
if (!passphrase_esc)
|
|
|
|
|
{
|
2008-09-03 11:37:32 +02:00
|
|
|
|
log_error ("can not escape string: %s\n",
|
|
|
|
|
gpg_strerror (gpg_error_from_syserror ()));
|
2006-07-29 18:40:54 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
|
2006-07-29 18:40:54 +02:00
|
|
|
|
passphrase_esc);
|
|
|
|
|
wipememory (passphrase_esc, strlen (passphrase_esc));
|
2006-08-16 12:47:53 +02:00
|
|
|
|
xfree (passphrase_esc);
|
2006-07-29 18:40:54 +02:00
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
if (rc < 0)
|
|
|
|
|
{
|
|
|
|
|
log_error ("caching passphrase failed: %s\n",
|
2006-09-14 18:50:33 +02:00
|
|
|
|
gpg_strerror (gpg_error_from_syserror ()));
|
2004-12-21 20:05:15 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!opt_passphrase)
|
|
|
|
|
wipememory (passphrase, sizeof (passphrase));
|
|
|
|
|
|
2016-08-11 09:52:08 +02:00
|
|
|
|
rc = simple_query (line);
|
2004-12-21 20:05:15 +01:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wipememory (line, strlen (line));
|
2008-05-27 14:03:50 +02:00
|
|
|
|
xfree (line);
|
2004-12-21 20:05:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
forget_passphrase (const char *keygrip)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
|
|
rc = asprintf (&line, "CLEAR_PASSPHRASE %s\n", keygrip);
|
|
|
|
|
if (rc < 0)
|
2010-06-07 15:33:02 +02:00
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
|
else
|
2016-08-11 09:52:08 +02:00
|
|
|
|
rc = simple_query (line);
|
2010-06-07 15:33:02 +02:00
|
|
|
|
if (rc)
|
2004-12-21 20:05:15 +01:00
|
|
|
|
{
|
2010-06-07 15:33:02 +02:00
|
|
|
|
log_error ("clearing passphrase failed: %s\n", gpg_strerror (rc));
|
2004-12-21 20:05:15 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2010-06-07 15:33:02 +02:00
|
|
|
|
|
2008-05-27 14:03:50 +02:00
|
|
|
|
xfree (line);
|
2004-12-21 20:05:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
ARGPARSE_ARGS pargs;
|
|
|
|
|
int cmd = 0;
|
|
|
|
|
const char *keygrip = NULL;
|
|
|
|
|
|
2015-01-28 19:57:22 +01:00
|
|
|
|
early_system_init ();
|
2004-12-21 20:05:15 +01:00
|
|
|
|
set_strusage (my_strusage);
|
Call log_set_prefix() with human-readable labels.
* agent/preset-passphrase.c, agent/protect-tool.c, dirmngr/dirmngr.c
* dirmngr/t-http.c, g10/gpg.c, g10/gpgv.c, g13/g13-syshelp.c
* g13/g13.c, kbx/kbxutil.c, scd/scdaemon.c, sm/gpgsm.c
* tests/gpgscm/main.c, tools/gpg-check-pattern.c
* tools/gpg-connect-agent.c, tools/gpgconf.c, tools/gpgtar.c
* tools/symcryptrun.c: Invoke log_set_prefix() with
human-readable labels.
--
Some invocations of log_set_prefix() were done with raw numeric values
instead of values that humans can understand. Use symbolic
representations instead of numeric for better readability.
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2016-08-12 07:37:57 +02:00
|
|
|
|
log_set_prefix ("gpg-preset-passphrase", GPGRT_LOG_WITH_PREFIX);
|
2004-12-21 20:05:15 +01:00
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
|
/* Make sure that our subsystems are ready. */
|
2004-12-21 20:05:15 +01:00
|
|
|
|
i18n_init ();
|
2010-03-22 13:46:05 +01:00
|
|
|
|
init_common_subsystems (&argc, &argv);
|
2004-12-21 20:05:15 +01:00
|
|
|
|
|
|
|
|
|
pargs.argc = &argc;
|
|
|
|
|
pargs.argv = &argv;
|
|
|
|
|
pargs.flags= 1; /* (do not remove the args) */
|
|
|
|
|
while (arg_parse (&pargs, opts) )
|
|
|
|
|
{
|
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
|
{
|
|
|
|
|
case oVerbose: opt.verbose++; break;
|
2016-06-07 10:59:46 +02:00
|
|
|
|
case oHomedir: gnupg_set_homedir (pargs.r.ret_str); break;
|
2004-12-21 20:05:15 +01:00
|
|
|
|
|
|
|
|
|
case oPreset: cmd = oPreset; break;
|
|
|
|
|
case oForget: cmd = oForget; break;
|
|
|
|
|
case oPassphrase: opt_passphrase = pargs.r.ret_str; break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
default : pargs.err = 2; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (log_get_errorcount(0))
|
|
|
|
|
exit(2);
|
|
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
|
keygrip = *argv;
|
|
|
|
|
else
|
|
|
|
|
usage (1);
|
|
|
|
|
|
2017-02-20 22:19:50 +01:00
|
|
|
|
/* Tell simple-pwquery about the standard socket name. */
|
2007-06-14 19:05:07 +02:00
|
|
|
|
{
|
2016-06-07 13:48:46 +02:00
|
|
|
|
char *tmp = make_filename (gnupg_socketdir (), GPG_AGENT_SOCK_NAME, NULL);
|
2007-06-14 19:05:07 +02:00
|
|
|
|
simple_pw_set_socket (tmp);
|
|
|
|
|
xfree (tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-21 20:05:15 +01:00
|
|
|
|
if (cmd == oPreset)
|
|
|
|
|
preset_passphrase (keygrip);
|
|
|
|
|
else if (cmd == oForget)
|
|
|
|
|
forget_passphrase (keygrip);
|
|
|
|
|
else
|
|
|
|
|
log_error ("one of the options --preset or --forget must be given\n");
|
|
|
|
|
|
|
|
|
|
agent_exit (0);
|
|
|
|
|
return 8; /*NOTREACHED*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
agent_exit (int rc)
|
|
|
|
|
{
|
|
|
|
|
rc = rc? rc : log_get_errorcount(0)? 2 : 0;
|
|
|
|
|
exit (rc);
|
|
|
|
|
}
|