~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to common/simple-pwquery.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:
1
1
/* simple-pwquery.c - A simple password query client for gpg-agent
2
 
 *      Copyright (C) 2002, 2004 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2002, 2004, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
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
/* This module is intended as a standalone client implementation to
67
66
#endif
68
67
 
69
68
 
 
69
/* Name of the socket to be used if GPG_AGENT_INFO has not been
 
70
   set. No default socket is used if this is NULL.  */
 
71
static char *default_gpg_agent_info;
 
72
 
 
73
 
70
74
 
71
75
 
72
76
 
153
157
        ;
154
158
      if (n)
155
159
        {
156
 
          break; /* at least one full line available - that's enough.
 
160
          break; /* At least one full line available - that's enough.
157
161
                    This function is just a simple implementation, so
158
 
                    it is okay to forget about pending bytes */
 
162
                    it is okay to forget about pending bytes.  */
159
163
        }
160
164
    }
161
165
 
203
207
  char *dft_display = NULL;
204
208
  char *dft_ttyname = NULL;
205
209
  char *dft_ttytype = NULL;
 
210
  char *dft_xauthority = NULL;
 
211
  char *dft_pinentry_user_data = NULL;
206
212
  int rc = 0;
207
213
 
208
214
  dft_display = getenv ("DISPLAY");
281
287
  }
282
288
#endif /*HAVE_SETLOCALE*/
283
289
 
 
290
  /* Send the XAUTHORITY variable.  */
 
291
  dft_xauthority = getenv ("XAUTHORITY");
 
292
  if (dft_xauthority)
 
293
    {
 
294
      /* We ignore errors here because older gpg-agents don't support
 
295
         this option.  */
 
296
      agent_send_option (fd, "xauthority", dft_xauthority);
 
297
    }
 
298
 
 
299
  /* Send the PINENTRY_USER_DATA variable.  */
 
300
  dft_pinentry_user_data = getenv ("PINENTRY_USER_DATA");
 
301
  if (dft_pinentry_user_data)
 
302
    {
 
303
      /* We ignore errors here because older gpg-agents don't support
 
304
         this option.  */
 
305
      agent_send_option (fd, "pinentry-user-data", dft_pinentry_user_data);
 
306
    }
 
307
 
284
308
  return 0;
285
309
}
286
310
 
304
328
  *rfd = -1;
305
329
  infostr = getenv ( "GPG_AGENT_INFO" );
306
330
  if ( !infostr || !*infostr ) 
 
331
    infostr = default_gpg_agent_info;
 
332
  if ( !infostr || !*infostr ) 
307
333
    {
308
334
#ifdef SPWQ_USE_LOGGING
309
335
      log_error (_("gpg-agent is not available in this session\n"));
424
450
}
425
451
 
426
452
 
 
453
/* Set the name of the default socket to NAME.  */
 
454
int 
 
455
simple_pw_set_socket (const char *name)
 
456
{
 
457
  spwq_free (default_gpg_agent_info);
 
458
  if (name)
 
459
    {
 
460
      default_gpg_agent_info = spwq_malloc (strlen (name) + 4 + 1);
 
461
      if (!default_gpg_agent_info)
 
462
        return SPWQ_OUT_OF_CORE;
 
463
      /* We don't know the PID thus we use 0.  */
 
464
      strcpy (stpcpy (default_gpg_agent_info, name),
 
465
              PATHSEP_S "0" PATHSEP_S "1");
 
466
    }
 
467
  else
 
468
    default_gpg_agent_info = NULL;
 
469
 
 
470
  return 0;
 
471
}
 
472
 
 
473
 
427
474
/* Ask the gpg-agent for a passphrase and present the user with a
428
 
   DESCRIPTION, a PROMPT and optiaonlly with a TRYAGAIN extra text.
 
475
   DESCRIPTION, a PROMPT and optionally with a TRYAGAIN extra text.
429
476
   If a CACHEID is not NULL it is used to locate the passphrase in in
430
 
   the cache and store it under this ID.  If ERRORCODE is not NULL it
431
 
   should point a variable receiving an errorcode; thsi errocode might
432
 
   be 0 if the user canceled the operation.  The function returns NULL
433
 
   to indicate an error. */
 
477
   the cache and store it under this ID.  If OPT_CHECK is true
 
478
   gpg-agent is asked to apply some checks on the passphrase security.
 
479
   If ERRORCODE is not NULL it should point a variable receiving an
 
480
   errorcode; this error code might be 0 if the user canceled the
 
481
   operation.  The function returns NULL to indicate an error.  */
434
482
char *
435
483
simple_pwquery (const char *cacheid, 
436
484
                const char *tryagain,
437
485
                const char *prompt,
438
486
                const char *description,
 
487
                int opt_check,
439
488
                int *errorcode)
440
489
{
441
490
  int fd = -1;
462
511
    char *line;
463
512
    /* We allocate 3 times the needed space so that there is enough
464
513
       space for escaping. */
465
 
    line = spwq_malloc (15
 
514
    line = spwq_malloc (15 + 10
466
515
                        + 3*strlen (cacheid) + 1
467
516
                        + 3*strlen (tryagain) + 1
468
517
                        + 3*strlen (prompt) + 1
475
524
      }
476
525
    strcpy (line, "GET_PASSPHRASE ");
477
526
    p = line+15;
 
527
    if (opt_check)
 
528
      p = stpcpy (p, "--check ");
478
529
    p = copy_and_escape (p, cacheid);
479
530
    *p++ = ' ';
480
531
    p = copy_and_escape (p, tryagain);
525
576
#ifdef SPWQ_USE_LOGGING
526
577
      log_info (_("canceled by user\n") );
527
578
#endif
528
 
      *errorcode = 0; /* canceled */
 
579
      *errorcode = 0; /* Special error code to indicate Cancel. */
 
580
    }
 
581
  else if (nread > 4 && !memcmp (pw, "ERR ", 4))
 
582
    {
 
583
      switch ( (strtoul (pw+4, NULL, 0) & 0xffff) )
 
584
        {
 
585
        case 85: rc = SPWQ_NO_PIN_ENTRY;  break;
 
586
        default: rc = SPWQ_GENERAL_ERROR; break;
 
587
        }
529
588
    }
530
589
  else 
531
590
    {