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

« back to all changes in this revision

Viewing changes to agent/preset-passphrase.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
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
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
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
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
109
108
}
110
109
 
111
110
 
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
111
 
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
112
 
 
113
/* Include the implementation of map_spwq_error.  */
 
114
MAP_SPWQ_ERROR_IMPL
153
115
      
 
116
/* Convert the string SRC into HEX encoding.  Caller needs to xfree
 
117
   the returned string.  */
 
118
static char *
 
119
make_hexstring (const char *src)
 
120
{
 
121
  int len = 2 * strlen (src) + 1;
 
122
  char *dst;
 
123
  char *res;
 
124
 
 
125
  res = dst = xtrymalloc (len);
 
126
  if (!dst)
 
127
    {
 
128
      log_error ("can not escape string: %s\n",
 
129
                 gpg_strerror (gpg_error_from_syserror ()));
 
130
      return NULL;
 
131
    }
 
132
 
 
133
#define _tohex(nr)      ((nr) < 10 ? ((nr) + '0') : (((nr) - 10) + 'A'))
 
134
#define tohex1(p)  _tohex (*((unsigned char *) p) & 15)
 
135
#define tohex2(p)  _tohex ((*((unsigned char *) p) >> 4) & 15)
 
136
 
 
137
  while (*src)
 
138
    {
 
139
      *(dst++) = tohex2 (src);
 
140
      *(dst++) = tohex1 (src);
 
141
      src++;
 
142
    }
 
143
  *dst = '\0';
 
144
  return res;
 
145
}
 
146
 
 
147
 
154
148
static void
155
149
preset_passphrase (const char *keygrip)
156
150
{
158
152
  char *line;
159
153
  /* FIXME: Use secure memory.  */
160
154
  char passphrase[500];
 
155
  char *passphrase_esc;
161
156
 
162
157
  if (!opt_passphrase)
163
158
    {
165
160
      if (rc < 0)
166
161
        {
167
162
          log_error ("reading passphrase failed: %s\n",
168
 
                     gpg_strerror (gpg_error_from_errno (errno)));
 
163
                     gpg_strerror (gpg_error_from_syserror ()));
169
164
          return;
170
165
        }
171
166
      passphrase[rc] = '\0';
172
167
      line = strchr (passphrase, '\n');
173
168
      if (line)
174
169
        {
175
 
          line--;
176
170
          if (line > passphrase && line[-1] == '\r')
177
171
            line--;
178
172
          *line = '\0';
181
175
      /* FIXME: How to handle empty passwords?  */
182
176
    }
183
177
 
 
178
  passphrase_esc = make_hexstring (opt_passphrase
 
179
                                   ? opt_passphrase : passphrase);
 
180
  if (!passphrase_esc)
 
181
    {
 
182
      /* Error message printed by callee.  */
 
183
      return;
 
184
    }
 
185
 
184
186
  rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip,
185
 
                 opt_passphrase? opt_passphrase : passphrase);
 
187
                 passphrase_esc);
 
188
  wipememory (passphrase_esc, strlen (passphrase_esc));
 
189
  xfree (passphrase_esc);
 
190
 
186
191
  if (rc < 0)
187
192
    {
188
193
      log_error ("caching passphrase failed: %s\n",
189
 
                 gpg_strerror (gpg_error_from_errno (errno)));
 
194
                 gpg_strerror (gpg_error_from_syserror ()));
190
195
      return;
191
196
    }
192
197
  if (!opt_passphrase)
214
219
  if (rc < 0)
215
220
    {
216
221
      log_error ("clearing passphrase failed: %s\n",
217
 
                 gpg_strerror (gpg_error_from_errno (errno)));
 
222
                 gpg_strerror (gpg_error_from_syserror ()));
218
223
      return;
219
224
    }
220
225
  free (line);
231
236
  set_strusage (my_strusage);
232
237
  log_set_prefix ("gpg-preset-passphrase", 1); 
233
238
 
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
 
239
  /* Make sure that our subsystems are ready.  */
 
240
  init_common_subsystems ();
246
241
 
247
242
  i18n_init ();
248
243
 
273
268
  else
274
269
    usage (1);
275
270
 
 
271
  /* Tell simple-pwquery about the the standard socket name.  */
 
272
  {
 
273
    char *tmp = make_filename (opt_homedir, "S.gpg-agent", NULL);
 
274
    simple_pw_set_socket (tmp);
 
275
    xfree (tmp);
 
276
  }
 
277
 
276
278
  if (cmd == oPreset)
277
279
    preset_passphrase (keygrip);
278
280
  else if (cmd == oForget)