summaryrefslogtreecommitdiffstats
path: root/crypto/openssh
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2011-10-07 13:10:16 +0000
committerdes <des@FreeBSD.org>2011-10-07 13:10:16 +0000
commit0caa0a85dd00ad53818b9cc641579462e69fd198 (patch)
tree5cebfa85b7bf7d8091f2779f2f3f947a6ea76d2e /crypto/openssh
parentc5fa84a4783cf66dc551a47a6d2fce6d17b825a3 (diff)
downloadFreeBSD-src-0caa0a85dd00ad53818b9cc641579462e69fd198.zip
FreeBSD-src-0caa0a85dd00ad53818b9cc641579462e69fd198.tar.gz
Add a -x option that causes ssh-agent(1) to exit when all clients have
disconnected. MFC after: 1 week
Diffstat (limited to 'crypto/openssh')
-rw-r--r--crypto/openssh/ssh-agent.14
-rw-r--r--crypto/openssh/ssh-agent.c32
2 files changed, 32 insertions, 4 deletions
diff --git a/crypto/openssh/ssh-agent.1 b/crypto/openssh/ssh-agent.1
index 7f4942f..b144615 100644
--- a/crypto/openssh/ssh-agent.1
+++ b/crypto/openssh/ssh-agent.1
@@ -44,7 +44,7 @@
.Sh SYNOPSIS
.Nm ssh-agent
.Op Fl c | s
-.Op Fl d
+.Op Fl dx
.Op Fl a Ar bind_address
.Op Fl t Ar life
.Op Ar command Op Ar arg ...
@@ -103,6 +103,8 @@ A lifetime specified for an identity with
.Xr ssh-add 1
overrides this value.
Without this option the default maximum lifetime is forever.
+.It Fl x
+Exit after the last client has disconnected.
.El
.Pp
If a commandline is given, this is executed as a subprocess of the agent.
diff --git a/crypto/openssh/ssh-agent.c b/crypto/openssh/ssh-agent.c
index b75d689..9d4e0c9 100644
--- a/crypto/openssh/ssh-agent.c
+++ b/crypto/openssh/ssh-agent.c
@@ -138,15 +138,34 @@ extern char *__progname;
/* Default lifetime (0 == forever) */
static int lifetime = 0;
+/*
+ * Client connection count; incremented in new_socket() and decremented in
+ * close_socket(). When it reaches 0, ssh-agent will exit. Since it is
+ * normally initialized to 1, it will never reach 0. However, if the -x
+ * option is specified, it is initialized to 0 in main(); in that case,
+ * ssh-agent will exit as soon as it has had at least one client but no
+ * longer has any.
+ */
+static int xcount = 1;
+
static void
close_socket(SocketEntry *e)
{
+ int last = 0;
+
+ if (e->type == AUTH_CONNECTION) {
+ debug("xcount %d -> %d", xcount, xcount - 1);
+ if (--xcount == 0)
+ last = 1;
+ }
close(e->fd);
e->fd = -1;
e->type = AUTH_UNUSED;
buffer_free(&e->input);
buffer_free(&e->output);
buffer_free(&e->request);
+ if (last)
+ cleanup_exit(0);
}
static void
@@ -901,6 +920,10 @@ new_socket(sock_type type, int fd)
{
u_int i, old_alloc, new_alloc;
+ if (type == AUTH_CONNECTION) {
+ debug("xcount %d -> %d", xcount, xcount + 1);
+ ++xcount;
+ }
set_nonblock(fd);
if (fd > max_fd)
@@ -1121,6 +1144,7 @@ usage(void)
fprintf(stderr, " -d Debug mode.\n");
fprintf(stderr, " -a socket Bind agent socket to given name.\n");
fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
+ fprintf(stderr, " -x Exit when the last client disconnects.\n");
exit(1);
}
@@ -1162,7 +1186,7 @@ main(int ac, char **av)
__progname = ssh_get_progname(av[0]);
seed_rng();
- while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
+ while ((ch = getopt(ac, av, "cdksa:t:x")) != -1) {
switch (ch) {
case 'c':
if (s_flag)
@@ -1191,6 +1215,9 @@ main(int ac, char **av)
usage();
}
break;
+ case 'x':
+ xcount = 0;
+ break;
default:
usage();
}
@@ -1350,8 +1377,7 @@ skip:
if (ac > 0)
parent_alive_interval = 10;
idtab_init();
- if (!d_flag)
- signal(SIGINT, SIG_IGN);
+ signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, cleanup_handler);
signal(SIGTERM, cleanup_handler);
OpenPOWER on IntegriCloud