2009-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / agent / preset-passphrase.c
blob77646d87ef0284ea5b6268ecaf63fb7f18e019ca
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/>.
20 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #ifdef HAVE_LOCALE_H
32 #include <locale.h>
33 #endif
34 #ifdef HAVE_LANGINFO_CODESET
35 #include <langinfo.h>
36 #endif
37 #ifdef HAVE_DOSISH_SYSTEM
38 #include <fcntl.h> /* for setmode() */
39 #endif
40 #ifdef HAVE_W32_SYSTEM
41 #include <windows.h> /* To initialize the sockets. fixme */
42 #endif
44 #define JNLIB_NEED_LOG_LOGV
45 #include "agent.h"
46 #include "minip12.h"
47 #include "simple-pwquery.h"
48 #include "i18n.h"
49 #include "sysutils.h"
52 enum cmd_and_opt_values
53 { aNull = 0,
54 oVerbose = 'v',
55 oPassphrase = 'P',
57 oPreset = 'c',
58 oForget = 'f',
60 oNoVerbose = 500,
62 oHomedir,
64 aTest };
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, "@" },
80 {0}
84 static const char *
85 my_strusage (int level)
87 const char *p;
88 switch (level)
90 case 11: p = "gpg-preset-passphrase (GnuPG)";
91 break;
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");
95 break;
96 case 1:
97 case 40:
98 p = _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
99 break;
100 case 41:
101 p = _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
102 "Password cache maintenance\n");
103 break;
105 default: p = NULL;
107 return p;
113 /* Include the implementation of map_spwq_error. */
114 MAP_SPWQ_ERROR_IMPL
117 static void
118 preset_passphrase (const char *keygrip)
120 int rc;
121 char *line;
122 /* FIXME: Use secure memory. */
123 char passphrase[500];
124 char *passphrase_esc;
126 if (!opt_passphrase)
128 rc = read (0, passphrase, sizeof (passphrase) - 1);
129 if (rc < 0)
131 log_error ("reading passphrase failed: %s\n",
132 gpg_strerror (gpg_error_from_syserror ()));
133 return;
135 passphrase[rc] = '\0';
136 line = strchr (passphrase, '\n');
137 if (line)
139 if (line > passphrase && line[-1] == '\r')
140 line--;
141 *line = '\0';
144 /* FIXME: How to handle empty passwords? */
148 const char *s = opt_passphrase ? opt_passphrase : passphrase;
149 passphrase_esc = bin2hex (s, strlen (s), NULL);
151 if (!passphrase_esc)
153 log_error ("can not escape string: %s\n",
154 gpg_strerror (gpg_error_from_syserror ()));
155 return;
158 rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
159 passphrase_esc);
160 wipememory (passphrase_esc, strlen (passphrase_esc));
161 xfree (passphrase_esc);
163 if (rc < 0)
165 log_error ("caching passphrase failed: %s\n",
166 gpg_strerror (gpg_error_from_syserror ()));
167 return;
169 if (!opt_passphrase)
170 wipememory (passphrase, sizeof (passphrase));
172 rc = map_spwq_error (simple_query (line));
173 if (rc)
175 log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
176 return;
179 wipememory (line, strlen (line));
180 xfree (line);
184 static void
185 forget_passphrase (const char *keygrip)
187 int rc;
188 char *line;
190 rc = asprintf (&line, "CLEAR_PASSPHRASE %s\n", keygrip);
191 if (rc < 0)
193 log_error ("clearing passphrase failed: %s\n",
194 gpg_strerror (gpg_error_from_syserror ()));
195 return;
197 xfree (line);
202 main (int argc, char **argv)
204 ARGPARSE_ARGS pargs;
205 int cmd = 0;
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. */
212 i18n_init ();
213 init_common_subsystems ();
215 opt_homedir = default_homedir ();
217 pargs.argc = &argc;
218 pargs.argv = &argv;
219 pargs.flags= 1; /* (do not remove the args) */
220 while (arg_parse (&pargs, opts) )
222 switch (pargs.r_opt)
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))
235 exit(2);
237 if (argc == 1)
238 keygrip = *argv;
239 else
240 usage (1);
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);
246 xfree (tmp);
249 if (cmd == oPreset)
250 preset_passphrase (keygrip);
251 else if (cmd == oForget)
252 forget_passphrase (keygrip);
253 else
254 log_error ("one of the options --preset or --forget must be given\n");
256 agent_exit (0);
257 return 8; /*NOTREACHED*/
261 void
262 agent_exit (int rc)
264 rc = rc? rc : log_get_errorcount(0)? 2 : 0;
265 exit (rc);