~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-proposed

« back to all changes in this revision

Viewing changes to agent/gpg-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Deslauriers
  • Date: 2011-05-25 14:27:35 UTC
  • mfrom: (1.1.15 upstream) (7.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20110525142735-jccyw0fopnyv728q
Tags: 2.0.17-2ubuntu1
* Merge from debian unstable. Remaining changes:
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
    . debian/gnupg2.dev: udev rules to set ACLs on SCM smartcard readers.
    . debian/rules: Call dh_installudev.
  - debian/control: Rename Vcs-* to XS-Debian-Vcs-*.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* gpg-agent.c  -  The GnuPG Agent
2
2
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
3
 
 *               2006, 2007, 2009 Free Software Foundation, Inc.
 
3
 *               2006, 2007, 2009, 2010 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
50
50
#include "gc-opt-flags.h"
51
51
#include "exechelp.h"
52
52
 
53
 
 
54
53
enum cmd_and_opt_values 
55
54
{ aNull = 0,
56
55
  oCsh            = 'c',
61
60
  oNoVerbose = 500,
62
61
  aGPGConfList,
63
62
  aGPGConfTest,
 
63
  aUseStandardSocketP,
64
64
  oOptions,
65
65
  oDebug,
66
66
  oDebugAll,
115
115
 
116
116
  { aGPGConfList, "gpgconf-list", 256, "@" },
117
117
  { aGPGConfTest, "gpgconf-test", 256, "@" },
 
118
  { aUseStandardSocketP, "use-standard-socket-p", 256, "@" }, 
118
119
  
119
120
  { 301, NULL, 0, N_("@Options:\n ") },
120
121
 
216
217
/* Counter for the currently running own socket checks.  */
217
218
static int check_own_socket_running;
218
219
 
219
 
/* True if we are listening on the standard socket.  */
220
 
static int use_standard_socket;
221
 
 
222
220
/* It is possible that we are currently running under setuid permissions */
223
221
static int maybe_setuid = 1;
224
222
 
276
274
static int check_for_running_agent (int silent, int mode);
277
275
 
278
276
/* Pth wrapper function definitions. */
 
277
ASSUAN_SYSTEM_PTH_IMPL;
 
278
 
279
279
GCRY_THREAD_OPTION_PTH_IMPL;
280
280
static int fixed_gcry_pth_init (void)
281
281
{
493
493
          || strcmp (current_logfile, pargs->r.ret_str))
494
494
        {
495
495
          log_set_file (pargs->r.ret_str);
496
 
          assuan_set_assuan_log_stream (log_get_stream ());
 
496
          if (DBG_ASSUAN)
 
497
            assuan_set_assuan_log_stream (log_get_stream ());
497
498
          xfree (current_logfile);
498
499
          current_logfile = xtrystrdup (pargs->r.ret_str);
499
500
        }
567
568
  int gpgconf_list = 0;
568
569
  gpg_error_t err;
569
570
  const char *env_file_name = NULL;
570
 
 
 
571
  struct assuan_malloc_hooks malloc_hooks;
571
572
 
572
573
  /* Before we do anything else we save the list of currently open
573
574
     file descriptors and the signal mask.  This info is required to
610
611
                 NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
611
612
    }
612
613
 
613
 
  assuan_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free);
614
 
  assuan_set_assuan_log_stream (log_get_stream ());
 
614
  malloc_hooks.malloc = gcry_malloc;
 
615
  malloc_hooks.realloc = gcry_realloc;
 
616
  malloc_hooks.free = gcry_free;
 
617
  assuan_set_malloc_hooks (&malloc_hooks);
615
618
  assuan_set_assuan_log_prefix (log_get_prefix (NULL));
616
 
  assuan_set_assuan_err_source (GPG_ERR_SOURCE_DEFAULT);
 
619
  assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT);
 
620
  assuan_set_system_hooks (ASSUAN_SYSTEM_PTH);
 
621
  assuan_sock_init ();
617
622
 
618
623
  setup_libgcrypt_logging ();
619
624
  gcry_control (GCRYCTL_USE_SECURE_RNDPOOL);
622
627
 
623
628
  /* Set default options.  */
624
629
  parse_rereadable_options (NULL, 0); /* Reset them to default values. */
625
 
#ifdef HAVE_W32_SYSTEM
626
 
  use_standard_socket = 1;  /* Under Windows we always use a standard
627
 
                               socket.  */
 
630
#ifdef USE_STANDARD_SOCKET
 
631
  opt.use_standard_socket = 1;  /* Under Windows we always use a standard
 
632
                                   socket.  */
628
633
#endif
629
634
  
630
635
  shell = getenv ("SHELL");
719
724
              if( parse_debug )
720
725
                log_info (_("NOTE: no default option file `%s'\n"),
721
726
                          configname );
 
727
              /* Save the default conf file name so that
 
728
                 reread_configuration is able to test whether the
 
729
                 config file has been created in the meantime.  */
 
730
              xfree (config_filename);
 
731
              config_filename = configname;
 
732
              configname = NULL;
722
733
            }
723
734
          else
724
735
            {
742
753
        {
743
754
        case aGPGConfList: gpgconf_list = 1; break;
744
755
        case aGPGConfTest: gpgconf_list = 2; break;
 
756
        case aUseStandardSocketP: gpgconf_list = 3; break;
745
757
        case oBatch: opt.batch=1; break;
746
758
 
747
759
        case oDebugWait: debug_wait = pargs.r.ret_int; break;
774
786
        case oXauthority: default_xauthority = xstrdup (pargs.r.ret_str);
775
787
          break;
776
788
 
777
 
        case oUseStandardSocket: use_standard_socket = 1; break;
778
 
        case oNoUseStandardSocket: use_standard_socket = 0; break;
 
789
        case oUseStandardSocket:   opt.use_standard_socket = 1; break;
 
790
        case oNoUseStandardSocket: opt.use_standard_socket = 0; break;
779
791
 
780
792
        case oFakedSystemTime:
781
793
          {
805
817
      fclose( configfp );
806
818
      configfp = NULL;
807
819
      /* Keep a copy of the name so that it can be read on SIGHUP. */
808
 
      config_filename = configname;
 
820
      if (config_filename != configname)
 
821
        {
 
822
          xfree (config_filename);
 
823
          config_filename = configname;
 
824
        }
809
825
      configname = NULL;
810
826
      goto next_pass;
811
827
    }
 
828
    
812
829
  xfree (configname);
813
830
  configname = NULL;
814
831
  if (log_get_errorcount(0))
852
869
      log_debug ("... okay\n");
853
870
    }
854
871
  
 
872
  if (gpgconf_list == 3)
 
873
    agent_exit (!opt.use_standard_socket);
855
874
  if (gpgconf_list == 2)
856
875
    agent_exit (0);
857
876
  if (gpgconf_list)
939
958
                             |JNLIB_LOG_WITH_TIME
940
959
                             |JNLIB_LOG_WITH_PID));
941
960
      current_logfile = xstrdup (logfile);
942
 
      assuan_set_assuan_log_stream (log_get_stream ());
943
961
    }
 
962
  if (DBG_ASSUAN)
 
963
    assuan_set_assuan_log_stream (log_get_stream ());
944
964
 
945
965
  /* Make sure that we have a default ttyname. */
946
966
  if (!default_ttyname && ttyname (1))
1323
1343
  fp = fopen (config_filename, "r");
1324
1344
  if (!fp)
1325
1345
    {
1326
 
      log_error (_("option file `%s': %s\n"),
1327
 
                 config_filename, strerror(errno) );
 
1346
      log_info (_("option file `%s': %s\n"),
 
1347
                config_filename, strerror(errno) );
1328
1348
      return;
1329
1349
    }
1330
1350
 
1425
1445
{
1426
1446
  char *name, *p;
1427
1447
 
1428
 
  if (use_standard_socket)
 
1448
  if (opt.use_standard_socket)
1429
1449
    name = make_filename (opt.homedir, standard_name, NULL);
1430
1450
  else
1431
1451
    {
1488
1508
  strcpy (serv_addr->sun_path, name);
1489
1509
  len = SUN_LEN (serv_addr);
1490
1510
  rc = assuan_sock_bind (fd, (struct sockaddr*) serv_addr, len);
1491
 
  if (use_standard_socket && rc == -1 && errno == EADDRINUSE)
 
1511
  if (opt.use_standard_socket && rc == -1 && errno == EADDRINUSE)
1492
1512
    {
1493
1513
      /* Check whether a gpg-agent is already running on the standard
1494
1514
         socket.  We do this test only if this is not the ssh socket.
1521
1541
                 gpg_strerror (gpg_error_from_errno (errno)));
1522
1542
      
1523
1543
      assuan_sock_close (fd);
1524
 
      if (use_standard_socket)
 
1544
      if (opt.use_standard_socket)
1525
1545
        *name = 0; /* Inhibit removal of the socket by cleanup(). */
1526
1546
      agent_exit (2);
1527
1547
    }
1651
1671
      if (kill (parent_pid, 0))
1652
1672
        {
1653
1673
          shutdown_pending = 2;
1654
 
          log_info ("parent process died - shutting down\n");
1655
 
          log_info ("%s %s stopped\n", strusage(11), strusage(13) );
 
1674
          if (!opt.quiet)
 
1675
            {
 
1676
              log_info ("parent process died - shutting down\n");
 
1677
              log_info ("%s %s stopped\n", strusage(11), strusage(13) );
 
1678
            }
1656
1679
          cleanup ();
1657
1680
          agent_exit (0);
1658
1681
        }
1869
1892
#endif
1870
1893
  time_ev = NULL;
1871
1894
 
 
1895
  /* Set a flag to tell call-scd.c that it may enable event
 
1896
     notifications.  */
 
1897
  opt.sigusr2_enabled = 1;
 
1898
 
1872
1899
  FD_ZERO (&fdset);
1873
1900
  FD_SET (FD2INT (listen_fd), &fdset);
1874
1901
  nfd = FD2INT (listen_fd);
2070
2097
 
2071
2098
 
2072
2099
/* Helper for check_own_socket.  */
2073
 
static int
 
2100
static gpg_error_t
2074
2101
check_own_socket_pid_cb (void *opaque, const void *buffer, size_t length)
2075
2102
{
2076
2103
  membuf_t *mb = opaque;
2087
2114
{
2088
2115
  int rc;
2089
2116
  char *sockname = arg;
2090
 
  assuan_context_t ctx;
 
2117
  assuan_context_t ctx = NULL;
2091
2118
  membuf_t mb;
2092
2119
  char *buffer;
2093
2120
 
2094
2121
  check_own_socket_running++;
2095
2122
 
2096
 
  rc = assuan_socket_connect (&ctx, sockname, (pid_t)(-1));
2097
 
  xfree (sockname);
 
2123
  rc = assuan_new (&ctx);
 
2124
  if (rc)
 
2125
    {
 
2126
      log_error ("can't allocate assuan context: %s\n", gpg_strerror (rc));
 
2127
      goto leave;
 
2128
    }
 
2129
 
 
2130
  rc = assuan_socket_connect (ctx, sockname, (pid_t)(-1), 0);
2098
2131
  if (rc)
2099
2132
    {
2100
2133
      log_error ("can't connect my own socket: %s\n", gpg_strerror (rc));
2121
2154
    log_error ("socket is still served by this server\n");
2122
2155
    
2123
2156
  xfree (buffer);
2124
 
  assuan_disconnect (ctx);
2125
2157
 
2126
2158
 leave:
 
2159
  xfree (sockname);
 
2160
  if (ctx)
 
2161
    assuan_release (ctx);
2127
2162
  if (rc)
2128
2163
    {
2129
2164
      /* We may not remove the socket as it is now in use by another
2150
2185
  char *sockname;
2151
2186
  pth_attr_t tattr;
2152
2187
 
2153
 
  if (!use_standard_socket)
 
2188
  if (!opt.use_standard_socket)
2154
2189
    return; /* This check makes only sense in standard socket mode.  */
2155
2190
 
2156
2191
  if (check_own_socket_running || shutdown_pending)
2181
2216
{
2182
2217
  int rc;
2183
2218
  char *infostr, *p;
2184
 
  assuan_context_t ctx;
 
2219
  assuan_context_t ctx = NULL;
2185
2220
  int prot, pid;
2186
2221
 
2187
2222
  if (!mode)
2229
2264
      pid = (pid_t)(-1);
2230
2265
    }
2231
2266
 
2232
 
 
2233
 
  rc = assuan_socket_connect (&ctx, infostr, pid);
 
2267
  rc = assuan_new (&ctx);
 
2268
  if (! rc)
 
2269
    rc = assuan_socket_connect (ctx, infostr, pid, 0);
2234
2270
  xfree (infostr);
2235
2271
  if (rc)
2236
2272
    {
2239
2275
 
2240
2276
      if (!mode && !silent)
2241
2277
        log_error ("can't connect to the agent: %s\n", gpg_strerror (rc));
 
2278
 
 
2279
      if (ctx)
 
2280
        assuan_release (ctx);
2242
2281
      return -1;
2243
2282
    }
2244
2283
 
2245
2284
  if (!opt.quiet && !silent)
2246
2285
    log_info ("gpg-agent running and available\n");
2247
2286
 
2248
 
  assuan_disconnect (ctx);
 
2287
  assuan_release (ctx);
2249
2288
  return 0;
2250
2289
}