147
146
{ oTTYtype, "ttytype", 2, "@" },
148
147
{ oLCctype, "lc-ctype", 2, "@" },
149
148
{ oLCmessages, "lc-messages", 2, "@" },
150
{ oKeepTTY, "keep-tty", 0, N_("ignore requests to change the TTY")},
149
{ oXauthority, "xauthority", 2, "@" },
150
{ oKeepTTY, "keep-tty", 0, N_("ignore requests to change the TTY")},
151
151
{ oKeepDISPLAY, "keep-display",
152
152
0, N_("ignore requests to change the X display")},
207
207
/* Name of the communication socket used for ssh-agent-emulation. */
208
208
static char *socket_name_ssh;
210
/* We need to keep track of the server's nonces (these are dummies for
212
static assuan_sock_nonce_t socket_nonce;
213
static assuan_sock_nonce_t socket_nonce_ssh;
210
216
/* Default values for options passed to the pinentry. */
211
217
static char *default_display;
212
218
static char *default_ttyname;
213
219
static char *default_ttytype;
214
220
static char *default_lc_ctype;
215
221
static char *default_lc_messages;
222
static char *default_xauthority;
217
224
/* Name of a config file, which will be reread on a HUP if it is not NULL. */
218
225
static char *config_filename;
237
244
static char *create_socket_name (int use_standard_socket,
238
245
char *standard_name, char *template);
239
static int create_server_socket (int is_standard_name, char *name);
246
static gnupg_fd_t create_server_socket (int is_standard_name, char *name,
248
assuan_sock_nonce_t *nonce);
240
249
static void create_directories (void);
242
251
static void agent_init_default_ctrl (ctrl_t ctrl);
243
252
static void agent_deinit_default_ctrl (ctrl_t ctrl);
245
static void handle_connections (int listen_fd, int listen_fd_ssh);
254
static void handle_connections (gnupg_fd_t listen_fd,
255
gnupg_fd_t listen_fd_ssh);
246
256
static int check_for_running_agent (int silent, int mode);
248
258
/* Pth wrapper function definitions. */
553
563
opt.startup_lc_messages = getenv ("LC_MESSAGES");
554
564
if (opt.startup_lc_messages)
555
565
opt.startup_lc_messages = xstrdup (opt.startup_lc_messages);
566
opt.startup_xauthority = getenv ("XAUTHORITY");
567
if (opt.startup_xauthority)
568
opt.startup_xauthority = xstrdup (opt.startup_xauthority);
569
opt.startup_pinentry_user_data = getenv ("PINENTRY_USER_DATA");
570
if (opt.startup_pinentry_user_data)
571
opt.startup_pinentry_user_data = xstrdup (opt.startup_pinentry_user_data);
557
573
/* Check whether we have a config file on the commandline */
558
574
orig_argc = argc;
656
672
case oTTYtype: default_ttytype = xstrdup (pargs.r.ret_str); break;
657
673
case oLCctype: default_lc_ctype = xstrdup (pargs.r.ret_str); break;
658
674
case oLCmessages: default_lc_messages = xstrdup (pargs.r.ret_str);
675
case oXauthority: default_xauthority = xstrdup (pargs.r.ret_str);
661
case oUseStandardSocket: standard_socket = 1; break;
662
case oNoUseStandardSocket: standard_socket = 0; break;
678
case oUseStandardSocket: use_standard_socket = 1; break;
679
case oNoUseStandardSocket: use_standard_socket = 0; break;
664
681
case oFakedSystemTime:
872
889
/* Create the sockets. */
873
socket_name = create_socket_name (standard_socket,
890
socket_name = create_socket_name (use_standard_socket,
875
892
"/tmp/gpg-XXXXXX/S.gpg-agent");
876
893
if (opt.ssh_support)
877
socket_name_ssh = create_socket_name (standard_socket,
894
socket_name_ssh = create_socket_name (use_standard_socket,
878
895
"S.gpg-agent.ssh",
879
896
"/tmp/gpg-XXXXXX/S.gpg-agent.ssh");
881
fd = create_server_socket (standard_socket, socket_name);
898
fd = create_server_socket (use_standard_socket, socket_name, 0,
882
900
if (opt.ssh_support)
883
fd_ssh = create_server_socket (standard_socket, socket_name_ssh);
901
fd_ssh = create_server_socket (use_standard_socket, socket_name_ssh, 1,
904
fd_ssh = GNUPG_INVALID_FD;
887
906
/* If we are going to exec a program in the parent, we record
888
907
the PID, so that the child may check whether the program is
1250
/* Under W32, this function returns the handle of the scdaemon
1251
notification event. Calling it the first time creates that
1253
#ifdef HAVE_W32_SYSTEM
1255
get_agent_scd_notify_event (void)
1257
static HANDLE the_event;
1261
SECURITY_ATTRIBUTES sa = { sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
1263
the_event = CreateEvent ( &sa, FALSE, FALSE, NULL);
1265
log_error ("can't create scd notify event: %s\n", w32_strerror (-1) );
1269
#endif /*HAVE_W32_SYSTEM*/
1222
1273
/* Create a name for the socket. With USE_STANDARD_SOCKET given as
1223
1274
true using STANDARD_NAME in the home directory or if given as
1269
1320
/* Create a Unix domain socket with NAME. IS_STANDARD_NAME indicates
1270
whether a non-random socket is used. Returns the file descriptor or
1271
terminates the process in case of an error. */
1273
create_server_socket (int is_standard_name, char *name)
1321
whether a non-random socket is used. Returns the file descriptor
1322
or terminates the process in case of an error. Not that this
1323
function needs to be used for the regular socket first and only then
1324
for the ssh socket. */
1326
create_server_socket (int is_standard_name, char *name, int is_ssh,
1327
assuan_sock_nonce_t *nonce)
1275
1329
struct sockaddr_un *serv_addr;
1280
#ifdef HAVE_W32_SYSTEM
1281
fd = _w32_sock_new (AF_UNIX, SOCK_STREAM, 0);
1283
fd = socket (AF_UNIX, SOCK_STREAM, 0);
1334
fd = assuan_sock_new (AF_UNIX, SOCK_STREAM, 0);
1335
if (fd == ASSUAN_INVALID_FD)
1287
1337
log_error (_("can't create socket: %s\n"), strerror (errno));
1288
1338
agent_exit (2);
1291
1341
serv_addr = xmalloc (sizeof (*serv_addr));
1292
1342
memset (serv_addr, 0, sizeof *serv_addr);
1293
1343
serv_addr->sun_family = AF_UNIX;
1294
assert (strlen (name) + 1 < sizeof (serv_addr->sun_path));
1344
if (strlen (name) + 1 >= sizeof (serv_addr->sun_path))
1346
log_error (_("socket name `%s' is too long\n"), name);
1295
1349
strcpy (serv_addr->sun_path, name);
1296
1350
len = (offsetof (struct sockaddr_un, sun_path)
1297
1351
+ strlen (serv_addr->sun_path) + 1);
1299
#ifdef HAVE_W32_SYSTEM
1300
rc = _w32_sock_bind (fd, (struct sockaddr*) serv_addr, len);
1301
if (is_standard_name && rc == -1 && errno == WSAEADDRINUSE)
1303
if (!check_for_running_agent (1, 1))
1305
log_error (_("a gpg-agent is already running - "
1306
"not starting a new one\n"));
1307
*name = 0; /* Inhibit removal of the socket by cleanup(). */
1313
rc = _w32_sock_bind (fd, (struct sockaddr*) serv_addr, len);
1316
rc = bind (fd, (struct sockaddr*) serv_addr, len);
1353
rc = assuan_sock_bind (fd, (struct sockaddr*) serv_addr, len);
1317
1354
if (is_standard_name && rc == -1 && errno == EADDRINUSE)
1319
if (!check_for_running_agent (1, 1))
1356
/* Check whether a gpg-agent is already running on the standard
1357
socket. We do this test only if this is not the ssh socket.
1358
For ssh we assume that a test for gpg-agent has already been
1359
done and reuse the requested ssh socket. Testing the
1360
ssh-socket is not possible because at this point, though we
1361
know the new Assuan socket, the Assuan server and thus the
1362
ssh-agent server is not yet operational. This would lead to
1364
if (!is_ssh && !check_for_running_agent (1, 1))
1321
1366
log_error (_("a gpg-agent is already running - "
1322
"not starting a new one\n"));
1367
"not starting a new one\n"));
1323
1368
*name = 0; /* Inhibit removal of the socket by cleanup(). */
1369
assuan_sock_close (fd);
1325
1370
agent_exit (2);
1328
rc = bind (fd, (struct sockaddr*) serv_addr, len);
1373
rc = assuan_sock_bind (fd, (struct sockaddr*) serv_addr, len);
1376
&& (rc=assuan_sock_get_nonce ((struct sockaddr*)serv_addr, len, nonce)))
1377
log_error (_("error getting nonce for the socket\n"));
1333
1380
/* We use gpg_strerror here because it allows us to get strings
1475
/* A global fucntion which allows us to call the reload stuff from
1476
other palces too. This is only used when build for W32. */
1522
/* A global function which allows us to call the reload stuff from
1523
other places too. This is only used when build for W32. */
1478
1525
agent_sighup_action (void)
1527
log_info ("SIGHUP received - "
1528
"re-reading configuration and flushing cache\n");
1480
1529
agent_flush_cache ();
1481
1530
reread_configuration ();
1482
1531
agent_reload_trustlist ();
1536
agent_sigusr2_action (void)
1539
log_info ("SIGUSR2 received - checking smartcard status\n");
1540
/* Nothing to check right now. We only increment a counter. */
1541
bump_card_eventcounter ();
1487
1546
handle_signal (int signo)
1491
1550
#ifndef HAVE_W32_SYSTEM
1493
log_info ("SIGHUP received - "
1494
"re-reading configuration and flushing cache\n");
1495
1552
agent_sighup_action ();
1595
/* Check the nonce on a new connection. This is a NOP unless we we
1596
are using our Unix domain socket emulation under Windows. */
1598
check_nonce (ctrl_t ctrl, assuan_sock_nonce_t *nonce)
1600
if (assuan_sock_check_nonce (ctrl->thread_startup.fd, nonce))
1602
log_info (_("error reading nonce on fd %d: %s\n"),
1603
FD2INT(ctrl->thread_startup.fd), strerror (errno));
1604
assuan_sock_close (ctrl->thread_startup.fd);
1541
1613
/* This is the standard connection thread's main function. */
1543
1615
start_connection_thread (void *arg)
1545
1617
ctrl_t ctrl = arg;
1619
if (check_nonce (ctrl, &socket_nonce))
1547
1622
agent_init_default_ctrl (ctrl);
1548
1623
if (opt.verbose)
1549
1624
log_info (_("handler 0x%lx for fd %d started\n"),
1550
(long)pth_self (), ctrl->thread_startup.fd);
1625
(long)pth_self (), FD2INT(ctrl->thread_startup.fd));
1552
start_command_handler (ctrl, -1, ctrl->thread_startup.fd);
1627
start_command_handler (ctrl, GNUPG_INVALID_FD, ctrl->thread_startup.fd);
1553
1628
if (opt.verbose)
1554
1629
log_info (_("handler 0x%lx for fd %d terminated\n"),
1555
(long)pth_self (), ctrl->thread_startup.fd);
1630
(long)pth_self (), FD2INT(ctrl->thread_startup.fd));
1557
1632
agent_deinit_default_ctrl (ctrl);
1567
1642
ctrl_t ctrl = arg;
1644
if (check_nonce (ctrl, &socket_nonce_ssh))
1569
1647
agent_init_default_ctrl (ctrl);
1570
1648
if (opt.verbose)
1571
1649
log_info (_("ssh handler 0x%lx for fd %d started\n"),
1572
(long)pth_self (), ctrl->thread_startup.fd);
1650
(long)pth_self (), FD2INT(ctrl->thread_startup.fd));
1574
1652
start_command_handler_ssh (ctrl, ctrl->thread_startup.fd);
1575
1653
if (opt.verbose)
1576
1654
log_info (_("ssh handler 0x%lx for fd %d terminated\n"),
1577
(long)pth_self (), ctrl->thread_startup.fd);
1655
(long)pth_self (), FD2INT(ctrl->thread_startup.fd));
1579
1657
agent_deinit_default_ctrl (ctrl);
1614
1692
pth_sigmask (SIG_UNBLOCK, &sigs, NULL);
1615
1693
ev = pth_event (PTH_EVENT_SIGS, &sigs, &signo);
1695
# ifdef PTH_EVENT_HANDLE
1697
ev = pth_event (PTH_EVENT_HANDLE, get_agent_scd_notify_event ());
1700
/* Use a dummy event. */
1618
1702
ev = pth_event (PTH_EVENT_SIGS, &sigs, &signo);
1620
1705
time_ev = NULL;
1622
1707
FD_ZERO (&fdset);
1623
FD_SET (listen_fd, &fdset);
1625
if (listen_fd_ssh != -1)
1708
FD_SET (FD2INT (listen_fd), &fdset);
1709
nfd = FD2INT (listen_fd);
1710
if (listen_fd_ssh != GNUPG_INVALID_FD)
1627
FD_SET (listen_fd_ssh, &fdset);
1628
if (listen_fd_ssh > nfd)
1629
nfd = listen_fd_ssh;
1712
FD_SET ( FD2INT(listen_fd_ssh), &fdset);
1713
if (FD2INT (listen_fd_ssh) > nfd)
1714
nfd = FD2INT (listen_fd_ssh);
1701
1796
new thread. Thus we need to block those signals. */
1702
1797
pth_sigmask (SIG_BLOCK, &sigs, &oldsigs);
1704
if (FD_ISSET (listen_fd, &read_fdset))
1799
if (FD_ISSET (FD2INT (listen_fd), &read_fdset))
1708
1803
plen = sizeof paddr;
1709
fd = pth_accept (listen_fd, (struct sockaddr *)&paddr, &plen);
1804
fd = INT2FD (pth_accept (FD2INT(listen_fd),
1805
(struct sockaddr *)&paddr, &plen));
1806
if (fd == GNUPG_INVALID_FD)
1712
1808
log_error ("accept failed: %s\n", strerror (errno));
1716
1812
log_error ("error allocating connection control data: %s\n",
1717
1813
strerror (errno) );
1814
assuan_sock_close (fd);
1722
1818
char threadname[50];
1724
1820
snprintf (threadname, sizeof threadname-1,
1725
"conn fd=%d (gpg)", fd);
1821
"conn fd=%d (gpg)", FD2INT(fd));
1726
1822
threadname[sizeof threadname -1] = 0;
1727
1823
pth_attr_set (tattr, PTH_ATTR_NAME, threadname);
1728
1824
ctrl->thread_startup.fd = fd;
1731
1827
log_error ("error spawning connection handler: %s\n",
1732
1828
strerror (errno) );
1829
assuan_sock_close (fd);
1833
fd = GNUPG_INVALID_FD;
1740
if (listen_fd_ssh != -1 && FD_ISSET (listen_fd_ssh, &read_fdset))
1836
if (listen_fd_ssh != GNUPG_INVALID_FD
1837
&& FD_ISSET ( FD2INT (listen_fd_ssh), &read_fdset))
1744
1841
plen = sizeof paddr;
1745
fd = pth_accept (listen_fd_ssh, (struct sockaddr *)&paddr, &plen);
1842
fd = INT2FD(pth_accept (FD2INT(listen_fd_ssh),
1843
(struct sockaddr *)&paddr, &plen));
1844
if (fd == GNUPG_INVALID_FD)
1748
1846
log_error ("accept failed for ssh: %s\n", strerror (errno));
1760
1858
agent_init_default_ctrl (ctrl);
1761
1859
snprintf (threadname, sizeof threadname-1,
1762
"conn fd=%d (ssh)", fd);
1860
"conn fd=%d (ssh)", FD2INT(fd));
1763
1861
threadname[sizeof threadname -1] = 0;
1764
1862
pth_attr_set (tattr, PTH_ATTR_NAME, threadname);
1765
1863
ctrl->thread_startup.fd = fd;
1790
1888
/* Figure out whether an agent is available and running. Prints an
1791
error if not. If SILENT is true, no mesdsages are printed. Usually
1889
error if not. If SILENT is true, no messages are printed. Usually
1792
1890
started with MODE 0. Returns 0 if the agent is running. */
1794
1892
check_for_running_agent (int silent, int mode)