~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty

« back to all changes in this revision

Viewing changes to agent/preset-passphrase.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2 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, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <stddef.h>
 
26
#include <stdarg.h>
 
27
#include <string.h>
 
28
#include <errno.h>
 
29
#include <assert.h>
 
30
#include <sys/stat.h>
 
31
#include <unistd.h>
 
32
#ifdef HAVE_LOCALE_H
 
33
#include <locale.h>
 
34
#endif
 
35
#ifdef HAVE_LANGINFO_CODESET
 
36
#include <langinfo.h>
 
37
#endif
 
38
#ifdef HAVE_DOSISH_SYSTEM
 
39
#include <fcntl.h> /* for setmode() */
 
40
#endif
 
41
#ifdef HAVE_W32_SYSTEM
 
42
#include <windows.h>  /* To initialize the sockets.  fixme */
 
43
#endif
 
44
 
 
45
#define JNLIB_NEED_LOG_LOGV
 
46
#include "agent.h"
 
47
#include "minip12.h"
 
48
#include "simple-pwquery.h"
 
49
#include "i18n.h"
 
50
#include "sysutils.h"
 
51
 
 
52
 
 
53
enum cmd_and_opt_values 
 
54
{ aNull = 0,
 
55
  oVerbose        = 'v',
 
56
  oPassphrase     = 'P',
 
57
 
 
58
  oPreset         = 'c',
 
59
  oForget         = 'f',
 
60
  
 
61
  oNoVerbose = 500,
 
62
 
 
63
  oHomedir,
 
64
 
 
65
aTest };
 
66
 
 
67
 
 
68
static const char *opt_homedir;
 
69
static const char *opt_passphrase;
 
70
 
 
71
static ARGPARSE_OPTS opts[] = {
 
72
  
 
73
  { 301, NULL, 0, N_("@Options:\n ") },
 
74
 
 
75
  { oVerbose, "verbose",   0, "verbose" },
 
76
  { oPassphrase, "passphrase", 2, "|STRING|use passphrase STRING" },
 
77
  { oPreset,  "preset",   256, "preset passphrase"},
 
78
  { oForget,  "forget",  256, "forget passphrase"},
 
79
 
 
80
  { oHomedir, "homedir", 2, "@" }, 
 
81
  {0}
 
82
};
 
83
 
 
84
 
 
85
static const char *
 
86
my_strusage (int level)
 
87
{
 
88
  const char *p;
 
89
  switch (level)
 
90
    {
 
91
    case 11: p = "gpg-preset-passphrase (GnuPG)";
 
92
      break;
 
93
    case 13: p = VERSION; break;
 
94
    case 17: p = PRINTABLE_OS_NAME; break;
 
95
    case 19: p = _("Please report bugs to <" PACKAGE_BUGREPORT ">.\n");
 
96
      break;
 
97
    case 1:
 
98
    case 40: 
 
99
      p =  _("Usage: gpg-preset-passphrase [options] KEYGRIP (-h for help)\n");
 
100
      break;
 
101
    case 41:
 
102
      p = _("Syntax: gpg-preset-passphrase [options] KEYGRIP\n"
 
103
                    "Password cache maintenance\n");
 
104
    break;
 
105
    
 
106
    default: p = NULL;
 
107
    }
 
108
  return p;
 
109
}
 
110
 
 
111
 
 
112
 
 
113
static void
 
114
i18n_init (void)
 
115
{
 
116
#ifdef USE_SIMPLE_GETTEXT
 
117
    set_gettext_file( PACKAGE_GT );
 
118
#else
 
119
#ifdef ENABLE_NLS
 
120
    setlocale (LC_ALL, "");
 
121
    bindtextdomain (PACKAGE_GT, LOCALEDIR);
 
122
    textdomain (PACKAGE_GT);
 
123
#endif
 
124
#endif
 
125
}
 
126
 
 
127
 
 
128
static gpg_error_t
 
129
map_spwq_error (int err)
 
130
{
 
131
  switch (err)
 
132
    {
 
133
    case 0:
 
134
      return 0;
 
135
    case SPWQ_OUT_OF_CORE:
 
136
      return gpg_error_from_errno (ENOMEM);
 
137
    case SPWQ_IO_ERROR:
 
138
      return gpg_error_from_errno (EIO);
 
139
    case SPWQ_PROTOCOL_ERROR:
 
140
      return gpg_error (GPG_ERR_PROTOCOL_VIOLATION);
 
141
    case SPWQ_ERR_RESPONSE:
 
142
      return gpg_error (GPG_ERR_INV_RESPONSE);
 
143
    case SPWQ_NO_AGENT:
 
144
      return gpg_error (GPG_ERR_NO_AGENT);
 
145
    case SPWQ_SYS_ERROR:
 
146
      return gpg_error_from_errno (errno);
 
147
    case SPWQ_GENERAL_ERROR:
 
148
    default:
 
149
      return gpg_error (GPG_ERR_GENERAL);
 
150
    }
 
151
}
 
152
 
 
153
      
 
154
static void
 
155
preset_passphrase (const char *keygrip)
 
156
{
 
157
  int  rc;
 
158
  char *line;
 
159
  /* FIXME: Use secure memory.  */
 
160
  char passphrase[500];
 
161
 
 
162
  if (!opt_passphrase)
 
163
    {
 
164
      rc = read (0, passphrase, sizeof (passphrase) - 1);
 
165
      if (rc < 0)
 
166
        {
 
167
          log_error ("reading passphrase failed: %s\n",
 
168
                     gpg_strerror (gpg_error_from_errno (errno)));
 
169
          return;
 
170
        }
 
171
      passphrase[rc] = '\0';
 
172
      line = strchr (passphrase, '\n');
 
173
      if (line)
 
174
        {
 
175
          line--;
 
176
          if (line > passphrase && line[-1] == '\r')
 
177
            line--;
 
178
          *line = '\0';
 
179
        }
 
180
 
 
181
      /* FIXME: How to handle empty passwords?  */
 
182
    }
 
183
 
 
184
  rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
 
185
                 opt_passphrase? opt_passphrase : passphrase);
 
186
  if (rc < 0)
 
187
    {
 
188
      log_error ("caching passphrase failed: %s\n",
 
189
                 gpg_strerror (gpg_error_from_errno (errno)));
 
190
      return;
 
191
    }
 
192
  if (!opt_passphrase)
 
193
    wipememory (passphrase, sizeof (passphrase));
 
194
 
 
195
  rc = map_spwq_error (simple_query (line));
 
196
  if (rc)
 
197
    {
 
198
      log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
 
199
      return;
 
200
    }
 
201
 
 
202
  wipememory (line, strlen (line));
 
203
  free (line);
 
204
}
 
205
 
 
206
 
 
207
static void
 
208
forget_passphrase (const char *keygrip)
 
209
{
 
210
  int rc;
 
211
  char *line;
 
212
 
 
213
  rc = asprintf (&line, "CLEAR_PASSPHRASE %s\n", keygrip);
 
214
  if (rc < 0)
 
215
    {
 
216
      log_error ("clearing passphrase failed: %s\n",
 
217
                 gpg_strerror (gpg_error_from_errno (errno)));
 
218
      return;
 
219
    }
 
220
  free (line);
 
221
}
 
222
 
 
223
 
 
224
int
 
225
main (int argc, char **argv)
 
226
{
 
227
  ARGPARSE_ARGS pargs;
 
228
  int cmd = 0;
 
229
  const char *keygrip = NULL;
 
230
 
 
231
  set_strusage (my_strusage);
 
232
  log_set_prefix ("gpg-preset-passphrase", 1); 
 
233
 
 
234
  /* Try to auto set the character set.  */
 
235
  set_native_charset (NULL); 
 
236
 
 
237
#ifdef HAVE_W32_SYSTEM
 
238
  /* Fixme: Need to initialize the Windows sockets: This should be
 
239
     moved to another place and we should make sure that it won't get
 
240
     doen twice, like when Pth is used too. */
 
241
  {
 
242
    WSADATA wsadat;
 
243
    WSAStartup (0x202, &wsadat);
 
244
  }
 
245
#endif
 
246
 
 
247
  i18n_init ();
 
248
 
 
249
  opt_homedir = default_homedir ();
 
250
 
 
251
  pargs.argc = &argc;
 
252
  pargs.argv = &argv;
 
253
  pargs.flags=  1;  /* (do not remove the args) */
 
254
  while (arg_parse (&pargs, opts) )
 
255
    {
 
256
      switch (pargs.r_opt)
 
257
        {
 
258
        case oVerbose: opt.verbose++; break;
 
259
        case oHomedir: opt_homedir = pargs.r.ret_str; break;
 
260
 
 
261
        case oPreset: cmd = oPreset; break;
 
262
        case oForget: cmd = oForget; break;
 
263
        case oPassphrase: opt_passphrase = pargs.r.ret_str; break;
 
264
          
 
265
        default : pargs.err = 2; break;
 
266
        }
 
267
    }
 
268
  if (log_get_errorcount(0))
 
269
    exit(2);
 
270
 
 
271
  if (argc == 1)
 
272
    keygrip = *argv;
 
273
  else
 
274
    usage (1);
 
275
 
 
276
  if (cmd == oPreset)
 
277
    preset_passphrase (keygrip);
 
278
  else if (cmd == oForget)
 
279
    forget_passphrase (keygrip);
 
280
  else
 
281
    log_error ("one of the options --preset or --forget must be given\n");
 
282
 
 
283
  agent_exit (0);
 
284
  return 8; /*NOTREACHED*/
 
285
}
 
286
 
 
287
 
 
288
void
 
289
agent_exit (int rc)
 
290
{
 
291
  rc = rc? rc : log_get_errorcount(0)? 2 : 0;
 
292
  exit (rc);
 
293
}