Imported Upstream version 2.1.11
[platform/upstream/gpg2.git] / agent / preset-passphrase.c
1 /* preset-passphrase.c - A tool to preset a passphrase.
2  *      Copyright (C) 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21
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 # ifdef HAVE_WINSOCK2_H
42 #  include <winsock2.h>
43 # endif
44 # include <windows.h>  /* To initialize the sockets.  fixme */
45 #endif
46
47 #include "agent.h"
48 #include "simple-pwquery.h"
49 #include "i18n.h"
50 #include "sysutils.h"
51 #include "../common/init.h"
52
53
54 enum cmd_and_opt_values
55 { aNull = 0,
56   oVerbose        = 'v',
57   oPassphrase     = 'P',
58
59   oPreset         = 'c',
60   oForget         = 'f',
61
62   oNoVerbose = 500,
63
64   oHomedir,
65
66 aTest };
67
68
69 static const char *opt_homedir;
70 static const char *opt_passphrase;
71
72 static ARGPARSE_OPTS opts[] = {
73
74   { 301, NULL, 0, N_("@Options:\n ") },
75
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"},
80
81   { oHomedir, "homedir", 2, "@" },
82   {0}
83 };
84
85
86 static const char *
87 my_strusage (int level)
88 {
89   const char *p;
90   switch (level)
91     {
92     case 11: p = "gpg-preset-passphrase (@GNUPG@)";
93       break;
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;
97
98     case 1:
99     case 40:
100       p =  _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
101       break;
102     case 41:
103       p = _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
104                     "Password cache maintenance\n");
105     break;
106
107     default: p = NULL;
108     }
109   return p;
110 }
111
112
113 \f
114
115 /* Include the implementation of map_spwq_error.  */
116 MAP_SPWQ_ERROR_IMPL
117
118
119 static void
120 preset_passphrase (const char *keygrip)
121 {
122   int  rc;
123   char *line;
124   /* FIXME: Use secure memory.  */
125   char passphrase[500];
126   char *passphrase_esc;
127
128   if (!opt_passphrase)
129     {
130       rc = read (0, passphrase, sizeof (passphrase) - 1);
131       if (rc < 0)
132         {
133           log_error ("reading passphrase failed: %s\n",
134                      gpg_strerror (gpg_error_from_syserror ()));
135           return;
136         }
137       passphrase[rc] = '\0';
138       line = strchr (passphrase, '\n');
139       if (line)
140         {
141           if (line > passphrase && line[-1] == '\r')
142             line--;
143           *line = '\0';
144         }
145
146       /* FIXME: How to handle empty passwords?  */
147     }
148
149   {
150     const char *s = opt_passphrase ? opt_passphrase : passphrase;
151     passphrase_esc = bin2hex (s, strlen (s), NULL);
152   }
153   if (!passphrase_esc)
154     {
155       log_error ("can not escape string: %s\n",
156                  gpg_strerror (gpg_error_from_syserror ()));
157       return;
158     }
159
160   rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
161                  passphrase_esc);
162   wipememory (passphrase_esc, strlen (passphrase_esc));
163   xfree (passphrase_esc);
164
165   if (rc < 0)
166     {
167       log_error ("caching passphrase failed: %s\n",
168                  gpg_strerror (gpg_error_from_syserror ()));
169       return;
170     }
171   if (!opt_passphrase)
172     wipememory (passphrase, sizeof (passphrase));
173
174   rc = map_spwq_error (simple_query (line));
175   if (rc)
176     {
177       log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
178       return;
179     }
180
181   wipememory (line, strlen (line));
182   xfree (line);
183 }
184
185
186 static void
187 forget_passphrase (const char *keygrip)
188 {
189   int rc;
190   char *line;
191
192   rc = asprintf (&line, "CLEAR_PASSPHRASE %s\n", keygrip);
193   if (rc < 0)
194     rc = gpg_error_from_syserror ();
195   else
196     rc = map_spwq_error (simple_query (line));
197   if (rc)
198     {
199       log_error ("clearing passphrase failed: %s\n", gpg_strerror (rc));
200       return;
201     }
202
203   xfree (line);
204 }
205
206 \f
207 int
208 main (int argc, char **argv)
209 {
210   ARGPARSE_ARGS pargs;
211   int cmd = 0;
212   const char *keygrip = NULL;
213
214   early_system_init ();
215   set_strusage (my_strusage);
216   log_set_prefix ("gpg-preset-passphrase", 1);
217
218   /* Make sure that our subsystems are ready.  */
219   i18n_init ();
220   init_common_subsystems (&argc, &argv);
221
222   opt_homedir = default_homedir ();
223
224   pargs.argc = &argc;
225   pargs.argv = &argv;
226   pargs.flags=  1;  /* (do not remove the args) */
227   while (arg_parse (&pargs, opts) )
228     {
229       switch (pargs.r_opt)
230         {
231         case oVerbose: opt.verbose++; break;
232         case oHomedir: opt_homedir = pargs.r.ret_str; break;
233
234         case oPreset: cmd = oPreset; break;
235         case oForget: cmd = oForget; break;
236         case oPassphrase: opt_passphrase = pargs.r.ret_str; break;
237
238         default : pargs.err = 2; break;
239         }
240     }
241   if (log_get_errorcount(0))
242     exit(2);
243
244   if (argc == 1)
245     keygrip = *argv;
246   else
247     usage (1);
248
249   /* Tell simple-pwquery about the the standard socket name.  */
250   {
251     char *tmp = make_filename (opt_homedir, GPG_AGENT_SOCK_NAME, NULL);
252     simple_pw_set_socket (tmp);
253     xfree (tmp);
254   }
255
256   if (cmd == oPreset)
257     preset_passphrase (keygrip);
258   else if (cmd == oForget)
259     forget_passphrase (keygrip);
260   else
261     log_error ("one of the options --preset or --forget must be given\n");
262
263   agent_exit (0);
264   return 8; /*NOTREACHED*/
265 }
266
267
268 void
269 agent_exit (int rc)
270 {
271   rc = rc? rc : log_get_errorcount(0)? 2 : 0;
272   exit (rc);
273 }