Skip to content

Commit 18a79b3

Browse files
committed
Share the TTY device with systemd-logind
1 parent f980317 commit 18a79b3

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

Makefile.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ GSSLIBS=@GSSLIBS@
5555
SSHDLIBS=@SSHDLIBS@
5656
LIBEDIT=@LIBEDIT@
5757
LIBFIDO2=@LIBFIDO2@
58+
LIBSYSTEMD=@LIBSYSTEMD@
5859
AR=@AR@
5960
AWK=@AWK@
6061
RANLIB=@RANLIB@
@@ -207,7 +208,7 @@ ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
207208
$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) $(GSSLIBS) $(CHANNELLIBS)
208209

209210
sshd$(EXEEXT): libssh.a $(LIBCOMPAT) $(SSHDOBJS)
210-
$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS) $(CHANNELLIBS)
211+
$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS) $(CHANNELLIBS) $(LIBSYSTEMD)
211212

212213
scp$(EXEEXT): $(LIBCOMPAT) libssh.a $(SCP_OBJS)
213214
$(LD) -o $@ $(SCP_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)

configure.ac

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,47 @@ AC_ARG_WITH([libedit],
17461746
fi ]
17471747
)
17481748

1749+
# Check whether user wants logind/set tty support
1750+
AC_ARG_WITH([logind],
1751+
[ --with-logind[[=PATH]] Enable logind support for sshd],
1752+
[ if test "x$withval" != "xno" ; then
1753+
if test "x$withval" = "xyes" ; then
1754+
AC_PATH_TOOL([PKGCONFIG], [pkg-config], [no])
1755+
if test "x$PKGCONFIG" != "xno"; then
1756+
AC_MSG_CHECKING([if $PKGCONFIG knows about libsystemd])
1757+
if "$PKGCONFIG" libsystemd; then
1758+
AC_MSG_RESULT([yes])
1759+
use_pkgconfig_for_libsystemd=yes
1760+
else
1761+
AC_MSG_RESULT([no])
1762+
fi
1763+
fi
1764+
else
1765+
CPPFLAGS="$CPPFLAGS -I${withval}/include"
1766+
if test -n "${rpath_opt}"; then
1767+
LDFLAGS="-L${withval}/lib ${rpath_opt}${withval}/lib ${LDFLAGS}"
1768+
else
1769+
LDFLAGS="-L${withval}/lib ${LDFLAGS}"
1770+
fi
1771+
fi
1772+
if test "x$use_pkgconfig_for_libsystemd" = "xyes"; then
1773+
LIBSYSTEMD=`$PKGCONFIG --libs libsystemd`
1774+
CPPFLAGS="$CPPFLAGS `$PKGCONFIG --cflags libsystemd`"
1775+
else
1776+
LIBSYSTEMD="-lsystemd"
1777+
fi
1778+
OTHERLIBS=`echo $LIBSYSTEMD | sed 's/-lsystemd//'`
1779+
AC_CHECK_LIB([systemd], [sd_bus_open_system],
1780+
[ AC_DEFINE([USE_LOGIND], [1], [Use systemd-logind])
1781+
AC_SUBST([LIBSYSTEMD])
1782+
],
1783+
[ AC_MSG_ERROR([libsystemd not found]) ],
1784+
[ $OTHERLIBS ]
1785+
)
1786+
fi ]
1787+
)
1788+
1789+
17491790
AUDIT_MODULE=none
17501791
AC_ARG_WITH([audit],
17511792
[ --with-audit=module Enable audit support (modules=debug,bsm,linux)],

loginrec.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@
187187
# include <util.h>
188188
#endif
189189

190+
#ifdef USE_LOGIND
191+
# include <systemd/sd-bus.h>
192+
#endif
193+
190194
/**
191195
** prototypes for helper functions in this file
192196
**/
@@ -200,6 +204,9 @@ void construct_utmp(struct logininfo *li, struct utmp *ut);
200204
void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
201205
void construct_utmpx(struct logininfo *li, struct utmpx *ut);
202206
#endif
207+
#ifdef USE_LOGIND
208+
int logind_set_tty(struct logininfo *li);
209+
#endif
203210

204211
int utmp_write_entry(struct logininfo *li);
205212
int utmpx_write_entry(struct logininfo *li);
@@ -467,6 +474,9 @@ login_write(struct logininfo *li)
467474
#ifdef USE_WTMPX
468475
wtmpx_write_entry(li);
469476
#endif
477+
#ifdef USE_LOGIND
478+
logind_set_tty(li);
479+
#endif
470480
#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
471481
if (li->type == LTYPE_LOGIN &&
472482
!sys_auth_record_login(li->username,li->hostname,li->line,
@@ -1409,6 +1419,88 @@ wtmpx_get_entry(struct logininfo *li)
14091419
}
14101420
#endif /* USE_WTMPX */
14111421

1422+
#ifdef USE_LOGIND
1423+
#define DBUS_DESTINATION "org.freedesktop.login1"
1424+
#define DBUS_PATH_ID "/org/freedesktop/login1/session/auto"
1425+
#define DBUS_INTERFACE "org.freedesktop.login1.Session"
1426+
#define DBUS_PATH "/org/freedesktop/login1/session/%s"
1427+
1428+
static int
1429+
logind_perform_login(struct logininfo *li)
1430+
{
1431+
sd_bus *bus = NULL;
1432+
sd_bus_error error = SD_BUS_ERROR_NULL;
1433+
char *session_id = NULL;
1434+
char *dbus_path;
1435+
const char *tty;
1436+
char buf[PATH_MAX];
1437+
int r;
1438+
int fd;
1439+
1440+
if (sd_bus_open_system(&bus) < 0)
1441+
{
1442+
logit("logind: canot open dbus");
1443+
return (0);
1444+
}
1445+
1446+
if (sd_bus_get_property_string(bus, DBUS_DESTINATION,
1447+
DBUS_PATH_ID, DBUS_INTERFACE,
1448+
"Id", &error, &session_id) < 0)
1449+
{
1450+
logit("logind: cannot get session ID");
1451+
return (0);
1452+
}
1453+
1454+
if (strncmp(li->line, "/dev/", 5) != 0)
1455+
snprintf (buf, sizeof(buf), "/dev/%s", li->line);
1456+
else
1457+
tty = li->line;
1458+
1459+
fd = open(tty, O_RDWR|O_CLOEXEC|O_NOCTTY);
1460+
1461+
if (asprintf (&dbus_path, DBUS_PATH, session_id) < 0)
1462+
return (0);
1463+
1464+
if (sd_bus_call_method(bus, DBUS_DESTINATION, dbus_path,
1465+
DBUS_INTERFACE, "TakeControl", &error, NULL,
1466+
"b", 1) < 0) {
1467+
logit("logind: cannot take control");
1468+
free(dbus_path);
1469+
return (0);
1470+
}
1471+
1472+
if ((r = sd_bus_call_method(bus, DBUS_DESTINATION, dbus_path,
1473+
DBUS_INTERFACE, "SetTTY", &error, NULL,
1474+
"h", fd)) < 0) {
1475+
if (r != -EBADR) /* logind does not support "SetTTY" */
1476+
logit("logind: cannot set TTY(%s, %s): %s", session_id, tty, strerror(-r));
1477+
free(dbus_path);
1478+
return (0);
1479+
}
1480+
1481+
free(dbus_path);
1482+
1483+
if (sd_bus_flush(bus) < 0)
1484+
return (0);
1485+
1486+
return (1);
1487+
}
1488+
1489+
int
1490+
logind_set_tty(struct logininfo *li)
1491+
{
1492+
switch(li->type) {
1493+
case LTYPE_LOGIN:
1494+
return (logind_perform_login(li));
1495+
case LTYPE_LOGOUT:
1496+
return (1);
1497+
default:
1498+
logit("%s: invalid type field", __func__);
1499+
return (0);
1500+
}
1501+
}
1502+
#endif
1503+
14121504
/**
14131505
** Low-level libutil login() functions
14141506
**/

0 commit comments

Comments
 (0)