1 /* preset-passphrase.c - A tool to preset a passphrase.
2 * Copyright (C) 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
34 #ifdef HAVE_LANGINFO_CODESET
37 #ifdef HAVE_DOSISH_SYSTEM
38 #include <fcntl.h> /* for setmode() */
40 #ifdef HAVE_W32_SYSTEM
41 # ifdef HAVE_WINSOCK2_H
42 # include <winsock2.h>
44 # include <windows.h> /* To initialize the sockets. fixme */
48 #include "simple-pwquery.h"
51 #include "../common/init.h"
54 enum cmd_and_opt_values
69 static const char *opt_homedir;
70 static const char *opt_passphrase;
72 static ARGPARSE_OPTS opts[] = {
74 { 301, NULL, 0, N_("@Options:\n ") },
76 { oVerbose, "verbose", 0, "verbose" },
77 { oPassphrase, "passphrase", 2, "|STRING|use passphrase STRING" },
78 { oPreset, "preset", 256, "preset passphrase"},
79 { oForget, "forget", 256, "forget passphrase"},
81 { oHomedir, "homedir", 2, "@" },
87 my_strusage (int level)
92 case 11: p = "gpg-preset-passphrase (@GNUPG@)";
94 case 13: p = VERSION; break;
95 case 17: p = PRINTABLE_OS_NAME; break;
96 case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
100 p = _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
103 p = _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
104 "Password cache maintenance\n");
115 /* Include the implementation of map_spwq_error. */
120 preset_passphrase (const char *keygrip)
124 /* FIXME: Use secure memory. */
125 char passphrase[500];
126 char *passphrase_esc;
130 rc = read (0, passphrase, sizeof (passphrase) - 1);
133 log_error ("reading passphrase failed: %s\n",
134 gpg_strerror (gpg_error_from_syserror ()));
137 passphrase[rc] = '\0';
138 line = strchr (passphrase, '\n');
141 if (line > passphrase && line[-1] == '\r')
146 /* FIXME: How to handle empty passwords? */
150 const char *s = opt_passphrase ? opt_passphrase : passphrase;
151 passphrase_esc = bin2hex (s, strlen (s), NULL);
155 log_error ("can not escape string: %s\n",
156 gpg_strerror (gpg_error_from_syserror ()));
160 rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
162 wipememory (passphrase_esc, strlen (passphrase_esc));
163 xfree (passphrase_esc);
167 log_error ("caching passphrase failed: %s\n",
168 gpg_strerror (gpg_error_from_syserror ()));
172 wipememory (passphrase, sizeof (passphrase));
174 rc = map_spwq_error (simple_query (line));
177 log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
181 wipememory (line, strlen (line));
187 forget_passphrase (const char *keygrip)
192 rc = asprintf (&line, "CLEAR_PASSPHRASE %s\n", keygrip);
194 rc = gpg_error_from_syserror ();
196 rc = map_spwq_error (simple_query (line));
199 log_error ("clearing passphrase failed: %s\n", gpg_strerror (rc));
208 main (int argc, char **argv)
212 const char *keygrip = NULL;
214 early_system_init ();
215 set_strusage (my_strusage);
216 log_set_prefix ("gpg-preset-passphrase", 1);
218 /* Make sure that our subsystems are ready. */
220 init_common_subsystems (&argc, &argv);
222 opt_homedir = default_homedir ();
226 pargs.flags= 1; /* (do not remove the args) */
227 while (arg_parse (&pargs, opts) )
231 case oVerbose: opt.verbose++; break;
232 case oHomedir: opt_homedir = pargs.r.ret_str; break;
234 case oPreset: cmd = oPreset; break;
235 case oForget: cmd = oForget; break;
236 case oPassphrase: opt_passphrase = pargs.r.ret_str; break;
238 default : pargs.err = 2; break;
241 if (log_get_errorcount(0))
249 /* Tell simple-pwquery about the the standard socket name. */
251 char *tmp = make_filename (opt_homedir, GPG_AGENT_SOCK_NAME, NULL);
252 simple_pw_set_socket (tmp);
257 preset_passphrase (keygrip);
258 else if (cmd == oForget)
259 forget_passphrase (keygrip);
261 log_error ("one of the options --preset or --forget must be given\n");
264 return 8; /*NOTREACHED*/
271 rc = rc? rc : log_get_errorcount(0)? 2 : 0;