1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00

libsystemd-login: add sd_session_get_remote_{host, user}

This commit is contained in:
Mantas Mikulėnas 2013-12-22 02:48:46 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent bee7e92899
commit 5b04fe6000
6 changed files with 120 additions and 5 deletions

View File

@ -908,8 +908,11 @@ MANPAGES_ALIAS += \
man/sd_seat_can_multi_session.3 \ man/sd_seat_can_multi_session.3 \
man/sd_seat_can_tty.3 \ man/sd_seat_can_tty.3 \
man/sd_seat_get_sessions.3 \ man/sd_seat_get_sessions.3 \
man/sd_session_is_remote.3 \
man/sd_session_get_class.3 \ man/sd_session_get_class.3 \
man/sd_session_get_display.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_seat.3 \
man/sd_session_get_service.3 \ man/sd_session_get_service.3 \
man/sd_session_get_state.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_multi_session.3: man/sd_seat_get_active.3
man/sd_seat_can_tty.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_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_class.3: man/sd_session_is_active.3
man/sd_session_get_display.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_seat.3: man/sd_session_is_active.3
man/sd_session_get_service.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 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 man/sd_seat_get_sessions.html: man/sd_seat_get_active.html
$(html-alias) $(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 man/sd_session_get_class.html: man/sd_session_is_active.html
$(html-alias) $(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 man/sd_session_get_state.html: man/sd_session_is_active.html
$(html-alias) $(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 man/sd_session_get_tty.html: man/sd_session_is_active.html
$(html-alias) $(html-alias)

View File

@ -65,6 +65,11 @@
<paramdef>const char* <parameter>session</parameter></paramdef> <paramdef>const char* <parameter>session</parameter></paramdef>
</funcprototype> </funcprototype>
<funcprototype>
<funcdef>int <function>sd_session_is_remote</function></funcdef>
<paramdef>const char* <parameter>session</parameter></paramdef>
</funcprototype>
<funcprototype> <funcprototype>
<funcdef>int <function>sd_session_get_state</function></funcdef> <funcdef>int <function>sd_session_get_state</function></funcdef>
<paramdef>const char* <parameter>session</parameter></paramdef> <paramdef>const char* <parameter>session</parameter></paramdef>
@ -107,6 +112,18 @@
<paramdef>char** <parameter>display</parameter></paramdef> <paramdef>char** <parameter>display</parameter></paramdef>
</funcprototype> </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> <funcprototype>
<funcdef>int <function>sd_session_get_tty</function></funcdef> <funcdef>int <function>sd_session_get_tty</function></funcdef>
<paramdef>const char* <parameter>session</parameter></paramdef> <paramdef>const char* <parameter>session</parameter></paramdef>
@ -130,6 +147,11 @@
(i.e. currently in the foreground and available for (i.e. currently in the foreground and available for
user input) or not.</para> 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 <para><function>sd_session_get_state()</function> may
be used to determine the state of the session be used to determine the state of the session
identified by the specified session identifier. The identified by the specified session identifier. The
@ -201,6 +223,23 @@
<citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> <citerefentry><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para> 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> <para><function>sd_session_get_tty()</function>
may be used to determine the TTY device of the may be used to determine the TTY device of the
session identified by the specified session session identified by the specified session
@ -226,7 +265,8 @@
<title>Return Value</title> <title>Return Value</title>
<para>If the test succeeds, <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 positive integer, if it fails 0. On success
<function>sd_session_get_state()</function>, <function>sd_session_get_state()</function>,
<function>sd_session_get_uid()</function>, <function>sd_session_get_uid()</function>,
@ -234,7 +274,9 @@
<function>sd_session_get_service()</function>, <function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>, <function>sd_session_get_type()</function>,
<function>sd_session_get_class()</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 <function>sd_session_get_tty()</function> return 0 or
a positive integer. On failure, these calls return a a positive integer. On failure, these calls return a
negative errno-style error code.</para> negative errno-style error code.</para>
@ -250,7 +292,9 @@
<function>sd_session_get_service()</function>, <function>sd_session_get_service()</function>,
<function>sd_session_get_type()</function>, <function>sd_session_get_type()</function>,
<function>sd_session_get_class()</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> <function>sd_session_get_tty()</function>
interfaces are available as shared library, which can interfaces are available as shared library, which can
be compiled and linked to with the be compiled and linked to with the

View File

@ -85,3 +85,10 @@ LIBSYSTEMD_LOGIN_207 {
global: global:
sd_session_get_vt; sd_session_get_vt;
} LIBSYSTEMD_LOGIN_205; } LIBSYSTEMD_LOGIN_205;
LIBSYSTEMD_LOGIN_209 {
global:
sd_session_is_remote;
sd_session_get_remote_user;
sd_session_get_remote_host;
} LIBSYSTEMD_LOGIN_207;

View File

@ -254,6 +254,26 @@ _public_ int sd_session_is_active(const char *session) {
return r; 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) { _public_ int sd_session_get_state(const char *session, char **state) {
_cleanup_free_ char *p = NULL, *s = NULL; _cleanup_free_ char *p = NULL, *s = NULL;
int r; int r;
@ -364,6 +384,14 @@ _public_ int sd_session_get_display(const char *session, char **display) {
return session_get_string(session, "DISPLAY", 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) { static int file_of_seat(const char *seat, char **_p) {
char *p; char *p;
int r; int r;

View File

@ -30,7 +30,7 @@
static void test_login(void) { static void test_login(void) {
int r, k; int r, k;
uid_t u, u2; uid_t u, u2;
char *seat, *type, *class, *display; char *seat, *type, *class, *display, *remote_user, *remote_host;
char *session; char *session;
char *state; char *state;
char *session2; char *session2;
@ -71,6 +71,10 @@ static void test_login(void) {
assert_se(r >= 0); assert_se(r >= 0);
printf("active = %s\n", yes_no(r)); 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); r = sd_session_get_state(session, &state);
assert_se(r >= 0); assert_se(r >= 0);
printf("state = %s\n", state); printf("state = %s\n", state);
@ -92,6 +96,14 @@ static void test_login(void) {
printf("display = %s\n", display); printf("display = %s\n", display);
free(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); assert_se(sd_session_get_seat(session, &seat) >= 0);
printf("seat = %s\n", seat); printf("seat = %s\n", seat);

View File

@ -95,9 +95,12 @@ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions);
* just return number of seats.*/ * just return number of seats.*/
int sd_uid_get_seats(uid_t uid, int require_active, char ***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); 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 /* Get state from session. Possible states: online, active, closing
* (This function is a more generic version of * (This function is a more generic version of
* sd_session_is_active().) */ * 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. */ /* Determine the X11 display of this session. */
int sd_session_get_display(const char *session, char **display); 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. */ /* Determine the TTY of this session. */
int sd_session_get_tty(const char *session, char **display); int sd_session_get_tty(const char *session, char **display);