[systemd-devel] [PATCH] libsystemd-login: add sd_session_get_remote_{host, user}
Mantas Mikulėnas
grawity at gmail.com
Sat Dec 21 16:48:46 PST 2013
---
Makefile-man.am | 15 +++++++++++++
man/sd_session_is_active.xml | 50 +++++++++++++++++++++++++++++++++++++++---
src/login/libsystemd-login.sym | 7 ++++++
src/login/sd-login.c | 28 +++++++++++++++++++++++
src/login/test-login.c | 14 +++++++++++-
src/systemd/sd-login.h | 11 +++++++++-
6 files changed, 120 insertions(+), 5 deletions(-)
diff --git a/Makefile-man.am b/Makefile-man.am
index d25d642..7ca8b4f 100644
--- a/Makefile-man.am
+++ b/Makefile-man.am
@@ -908,8 +908,11 @@ MANPAGES_ALIAS += \
man/sd_seat_can_multi_session.3 \
man/sd_seat_can_tty.3 \
man/sd_seat_get_sessions.3 \
+ man/sd_session_is_remote.3 \
man/sd_session_get_class.3 \
man/sd_session_get_display.3 \
+ man/sd_session_get_remote_host.3 \
+ man/sd_session_get_remote_user.3 \
man/sd_session_get_seat.3 \
man/sd_session_get_service.3 \
man/sd_session_get_state.3 \
@@ -939,8 +942,11 @@ man/sd_seat_can_graphical.3: man/sd_seat_get_active.3
man/sd_seat_can_multi_session.3: man/sd_seat_get_active.3
man/sd_seat_can_tty.3: man/sd_seat_get_active.3
man/sd_seat_get_sessions.3: man/sd_seat_get_active.3
+man/sd_session_is_remote.3: man/sd_session_is_active.3
man/sd_session_get_class.3: man/sd_session_is_active.3
man/sd_session_get_display.3: man/sd_session_is_active.3
+man/sd_session_get_remote_host.3: man/sd_session_is_active.3
+man/sd_session_get_remote_user.3: man/sd_session_is_active.3
man/sd_session_get_seat.3: man/sd_session_is_active.3
man/sd_session_get_service.3: man/sd_session_is_active.3
man/sd_session_get_state.3: man/sd_session_is_active.3
@@ -1006,6 +1012,9 @@ man/sd_seat_can_tty.html: man/sd_seat_get_active.html
man/sd_seat_get_sessions.html: man/sd_seat_get_active.html
$(html-alias)
+man/sd_session_is_remote.html: man/sd_session_is_active.html
+ $(html-alias)
+
man/sd_session_get_class.html: man/sd_session_is_active.html
$(html-alias)
@@ -1021,6 +1030,12 @@ man/sd_session_get_service.html: man/sd_session_is_active.html
man/sd_session_get_state.html: man/sd_session_is_active.html
$(html-alias)
+man/sd_session_get_remote_host.html: man/sd_session_is_active.html
+ $(html-alias)
+
+man/sd_session_get_remote_user.html: man/sd_session_is_active.html
+ $(html-alias)
+
man/sd_session_get_tty.html: man/sd_session_is_active.html
$(html-alias)
diff --git a/man/sd_session_is_active.xml b/man/sd_session_is_active.xml
index 293ce71..d94ae19 100644
--- a/man/sd_session_is_active.xml
+++ b/man/sd_session_is_active.xml
@@ -66,6 +66,11 @@
</funcprototype>
<funcprototype>
+ <funcdef>int <function>sd_session_is_remote</function></funcdef>
+ <paramdef>const char* <parameter>session</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
<funcdef>int <function>sd_session_get_state</function></funcdef>
<paramdef>const char* <parameter>session</parameter></paramdef>
<paramdef>char** <parameter>state</parameter></paramdef>
@@ -108,6 +113,18 @@
</funcprototype>
<funcprototype>
+ <funcdef>int <function>sd_session_get_remote_host</function></funcdef>
+ <paramdef>const char* <parameter>session</parameter></paramdef>
+ <paramdef>char** <parameter>remote_host</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
+ <funcdef>int <function>sd_session_get_remote_user</function></funcdef>
+ <paramdef>const char* <parameter>session</parameter></paramdef>
+ <paramdef>char** <parameter>remote_user</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
<funcdef>int <function>sd_session_get_tty</function></funcdef>
<paramdef>const char* <parameter>session</parameter></paramdef>
<paramdef>char** <parameter>tty</parameter></paramdef>
@@ -130,6 +147,11 @@
(i.e. currently in the foreground and available for
user input) or not.</para>
+ <para><function>sd_session_is_remote()</function> may
+ be used to determine whether the session identified by
+ the specified session identifier is a remote session
+ (i.e. its remote host is known) or not.</para>
+
<para><function>sd_session_get_state()</function> may
be used to determine the state of the session
identified by the specified session identifier. The
@@ -201,6 +223,23 @@
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para>
+ <para><function>sd_session_get_remote_host()</function>
+ may be used to determine the remote hostname of the
+ session identified by the specified session
+ identifier. The returned string needs to be
+ freed with the libc
+ <citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ call after use.</para>
+
+ <para><function>sd_session_get_remote_user()</function>
+ may be used to determine the remote username of the
+ session identified by the specified session
+ identifier. The returned string needs to be
+ freed with the libc
+ <citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ call after use. Note that this value is rarely known
+ to the system, and even then should not be relied on.</para>
+
<para><function>sd_session_get_tty()</function>
may be used to determine the TTY device of the
session identified by the specified session
@@ -226,7 +265,8 @@
<title>Return Value</title>
<para>If the test succeeds,
- <function>sd_session_is_active()</function> returns a
+ <function>sd_session_is_active()</function> and
+ <function>sd_session_is_remote()</function> return a
positive integer, if it fails 0. On success
<function>sd_session_get_state()</function>,
<function>sd_session_get_uid()</function>,
@@ -234,7 +274,9 @@
<function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>,
<function>sd_session_get_class()</function>,
- <function>sd_session_get_display()</function> and
+ <function>sd_session_get_display()</function>,
+ <function>sd_session_get_remote_user()</function>,
+ <function>sd_session_get_remote_host()</function> and
<function>sd_session_get_tty()</function> return 0 or
a positive integer. On failure, these calls return a
negative errno-style error code.</para>
@@ -250,7 +292,9 @@
<function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>,
<function>sd_session_get_class()</function>,
- <function>sd_session_get_display()</function> and
+ <function>sd_session_get_display()</function>,
+ <function>sd_session_get_remote_host()</function>,
+ <function>sd_session_get_remote_user()</function> and
<function>sd_session_get_tty()</function>
interfaces are available as shared library, which can
be compiled and linked to with the
diff --git a/src/login/libsystemd-login.sym b/src/login/libsystemd-login.sym
index 54aa91c..1d33982 100644
--- a/src/login/libsystemd-login.sym
+++ b/src/login/libsystemd-login.sym
@@ -85,3 +85,10 @@ LIBSYSTEMD_LOGIN_207 {
global:
sd_session_get_vt;
} LIBSYSTEMD_LOGIN_205;
+
+LIBSYSTEMD_LOGIN_209 {
+global:
+ sd_session_is_remote;
+ sd_session_get_remote_user;
+ sd_session_get_remote_host;
+} LIBSYSTEMD_LOGIN_207;
diff --git a/src/login/sd-login.c b/src/login/sd-login.c
index 2930b87..c795765 100644
--- a/src/login/sd-login.c
+++ b/src/login/sd-login.c
@@ -254,6 +254,26 @@ _public_ int sd_session_is_active(const char *session) {
return r;
}
+_public_ int sd_session_is_remote(const char *session) {
+ int r;
+ _cleanup_free_ char *p = NULL, *s = NULL;
+
+ r = file_of_session(session, &p);
+ if (r < 0)
+ return r;
+
+ r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+ if (r < 0)
+ return r;
+
+ if (!s)
+ return -EIO;
+
+ r = parse_boolean(s);
+
+ return r;
+}
+
_public_ int sd_session_get_state(const char *session, char **state) {
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
@@ -364,6 +384,14 @@ _public_ int sd_session_get_display(const char *session, char **display) {
return session_get_string(session, "DISPLAY", display);
}
+_public_ int sd_session_get_remote_user(const char *session, char **remote_user) {
+ return session_get_string(session, "REMOTE_USER", remote_user);
+}
+
+_public_ int sd_session_get_remote_host(const char *session, char **remote_host) {
+ return session_get_string(session, "REMOTE_HOST", remote_host);
+}
+
static int file_of_seat(const char *seat, char **_p) {
char *p;
int r;
diff --git a/src/login/test-login.c b/src/login/test-login.c
index 228ddb2..ce88af8 100644
--- a/src/login/test-login.c
+++ b/src/login/test-login.c
@@ -30,7 +30,7 @@
static void test_login(void) {
int r, k;
uid_t u, u2;
- char *seat, *type, *class, *display;
+ char *seat, *type, *class, *display, *remote_user, *remote_host;
char *session;
char *state;
char *session2;
@@ -71,6 +71,10 @@ static void test_login(void) {
assert_se(r >= 0);
printf("active = %s\n", yes_no(r));
+ r = sd_session_is_remote(session);
+ assert_se(r >= 0);
+ printf("remote = %s\n", yes_no(r));
+
r = sd_session_get_state(session, &state);
assert_se(r >= 0);
printf("state = %s\n", state);
@@ -92,6 +96,14 @@ static void test_login(void) {
printf("display = %s\n", display);
free(display);
+ assert_se(sd_session_get_remote_user(session, &remote_user) >= 0);
+ printf("remote_user = %s\n", remote_user);
+ free(remote_user);
+
+ assert_se(sd_session_get_remote_host(session, &remote_host) >= 0);
+ printf("remote_host = %s\n", remote_host);
+ free(remote_host);
+
assert_se(sd_session_get_seat(session, &seat) >= 0);
printf("seat = %s\n", seat);
diff --git a/src/systemd/sd-login.h b/src/systemd/sd-login.h
index a230aff..6de6932 100644
--- a/src/systemd/sd-login.h
+++ b/src/systemd/sd-login.h
@@ -95,9 +95,12 @@ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions);
* just return number of seats.*/
int sd_uid_get_seats(uid_t uid, int require_active, char ***seats);
-/* Return 1 if the session is a active. */
+/* Return 1 if the session is active. */
int sd_session_is_active(const char *session);
+/* Return 1 if the session is remote. */
+int sd_session_is_remote(const char *session);
+
/* Get state from session. Possible states: online, active, closing
* (This function is a more generic version of
* sd_session_is_active().) */
@@ -121,6 +124,12 @@ int sd_session_get_class(const char *session, char **clazz);
/* Determine the X11 display of this session. */
int sd_session_get_display(const char *session, char **display);
+/* Determine the remote host of this session. */
+int sd_session_get_remote_host(const char *session, char **remote_host);
+
+/* Determine the remote user of this session (if provided by PAM). */
+int sd_session_get_remote_user(const char *session, char **remote_user);
+
/* Determine the TTY of this session. */
int sd_session_get_tty(const char *session, char **display);
--
1.8.5.1.277.g0e0d235
More information about the systemd-devel
mailing list