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 #include <windows.h> /* To initialize the sockets. fixme */
44 #define JNLIB_NEED_LOG_LOGV
47 #include "simple-pwquery.h"
52 enum cmd_and_opt_values
67 static const char *opt_homedir
;
68 static const char *opt_passphrase
;
70 static ARGPARSE_OPTS opts
[] = {
72 { 301, NULL
, 0, N_("@Options:\n ") },
74 { oVerbose
, "verbose", 0, "verbose" },
75 { oPassphrase
, "passphrase", 2, "|STRING|use passphrase STRING" },
76 { oPreset
, "preset", 256, "preset passphrase"},
77 { oForget
, "forget", 256, "forget passphrase"},
79 { oHomedir
, "homedir", 2, "@" },
85 my_strusage (int level
)
90 case 11: p
= "gpg-preset-passphrase (GnuPG)";
92 case 13: p
= VERSION
; break;
93 case 17: p
= PRINTABLE_OS_NAME
; break;
94 case 19: p
= _("Please report bugs to <" PACKAGE_BUGREPORT
">.\n");
98 p
= _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
101 p
= _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
102 "Password cache maintenance\n");
113 /* Include the implementation of map_spwq_error. */
118 preset_passphrase (const char *keygrip
)
122 /* FIXME: Use secure memory. */
123 char passphrase
[500];
124 char *passphrase_esc
;
128 rc
= read (0, passphrase
, sizeof (passphrase
) - 1);
131 log_error ("reading passphrase failed: %s\n",
132 gpg_strerror (gpg_error_from_syserror ()));
135 passphrase
[rc
] = '\0';
136 line
= strchr (passphrase
, '\n');
139 if (line
> passphrase
&& line
[-1] == '\r')
144 /* FIXME: How to handle empty passwords? */
148 const char *s
= opt_passphrase
? opt_passphrase
: passphrase
;
149 passphrase_esc
= bin2hex (s
, strlen (s
), NULL
);
153 log_error ("can not escape string: %s\n",
154 gpg_strerror (gpg_error_from_syserror ()));
158 rc
= asprintf (&line
, "PRESET_PASSPHRASE %s -1 %s\n", keygrip
,
160 wipememory (passphrase_esc
, strlen (passphrase_esc
));
161 xfree (passphrase_esc
);
165 log_error ("caching passphrase failed: %s\n",
166 gpg_strerror (gpg_error_from_syserror ()));
170 wipememory (passphrase
, sizeof (passphrase
));
172 rc
= map_spwq_error (simple_query (line
));
175 log_error ("caching passphrase failed: %s\n", gpg_strerror (rc
));
179 wipememory (line
, strlen (line
));
185 forget_passphrase (const char *keygrip
)
190 rc
= asprintf (&line
, "CLEAR_PASSPHRASE %s\n", keygrip
);
193 log_error ("clearing passphrase failed: %s\n",
194 gpg_strerror (gpg_error_from_syserror ()));
202 main (int argc
, char **argv
)
206 const char *keygrip
= NULL
;
208 set_strusage (my_strusage
);
209 log_set_prefix ("gpg-preset-passphrase", 1);
211 /* Make sure that our subsystems are ready. */
213 init_common_subsystems ();
215 opt_homedir
= default_homedir ();
219 pargs
.flags
= 1; /* (do not remove the args) */
220 while (arg_parse (&pargs
, opts
) )
224 case oVerbose
: opt
.verbose
++; break;
225 case oHomedir
: opt_homedir
= pargs
.r
.ret_str
; break;
227 case oPreset
: cmd
= oPreset
; break;
228 case oForget
: cmd
= oForget
; break;
229 case oPassphrase
: opt_passphrase
= pargs
.r
.ret_str
; break;
231 default : pargs
.err
= 2; break;
234 if (log_get_errorcount(0))
242 /* Tell simple-pwquery about the the standard socket name. */
244 char *tmp
= make_filename (opt_homedir
, "S.gpg-agent", NULL
);
245 simple_pw_set_socket (tmp
);
250 preset_passphrase (keygrip
);
251 else if (cmd
== oForget
)
252 forget_passphrase (keygrip
);
254 log_error ("one of the options --preset or --forget must be given\n");
257 return 8; /*NOTREACHED*/
264 rc
= rc
? rc
: log_get_errorcount(0)? 2 : 0;