From 5b04fe60004e7c5cd5a43648ede3e6a965e70b8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mantas=20Mikul=C4=97nas?= Date: Sun, 22 Dec 2013 02:48:46 +0200 Subject: [PATCH] libsystemd-login: add sd_session_get_remote_{host, user} --- 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 d25d64227..7ca8b4f07 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 293ce7164..d94ae196a 100644 --- a/man/sd_session_is_active.xml +++ b/man/sd_session_is_active.xml @@ -65,6 +65,11 @@ const char* session + + int sd_session_is_remote + const char* session + + int sd_session_get_state const char* session @@ -107,6 +112,18 @@ char** display + + int sd_session_get_remote_host + const char* session + char** remote_host + + + + int sd_session_get_remote_user + const char* session + char** remote_user + + int sd_session_get_tty const char* session @@ -130,6 +147,11 @@ (i.e. currently in the foreground and available for user input) or not. + sd_session_is_remote() 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. + sd_session_get_state() may be used to determine the state of the session identified by the specified session identifier. The @@ -201,6 +223,23 @@ free3 call after use. + sd_session_get_remote_host() + 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 + free3 + call after use. + + sd_session_get_remote_user() + 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 + free3 + call after use. Note that this value is rarely known + to the system, and even then should not be relied on. + sd_session_get_tty() may be used to determine the TTY device of the session identified by the specified session @@ -226,7 +265,8 @@ Return Value If the test succeeds, - sd_session_is_active() returns a + sd_session_is_active() and + sd_session_is_remote() return a positive integer, if it fails 0. On success sd_session_get_state(), sd_session_get_uid(), @@ -234,7 +274,9 @@ sd_session_get_service(), sd_session_get_type(), sd_session_get_class(), - sd_session_get_display() and + sd_session_get_display(), + sd_session_get_remote_user(), + sd_session_get_remote_host() and sd_session_get_tty() return 0 or a positive integer. On failure, these calls return a negative errno-style error code. @@ -250,7 +292,9 @@ sd_session_get_service(), sd_session_get_type(), sd_session_get_class(), - sd_session_get_display() and + sd_session_get_display(), + sd_session_get_remote_host(), + sd_session_get_remote_user() and sd_session_get_tty() 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 54aa91c60..1d3398253 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 2930b8751..c79576562 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 228ddb293..ce88af80f 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 a230aff4c..6de6932a7 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); -- 2.30.2