From: Iker Pedrosa Date: Tue, 18 Jul 2023 13:56:46 +0000 (+0200) Subject: libmisc: implement `get_session_host()` X-Git-Tag: 4.14.0-rc1~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f40bdfa66a3a284a15f7b6f15aa379010d466212;p=thirdparty%2Fshadow.git libmisc: implement `get_session_host()` Implement `get_session_host()` in `utmp.c` and `logind.c`. Signed-off-by: Iker Pedrosa --- diff --git a/lib/prototypes.h b/lib/prototypes.h index 350893e0e..066c3d169 100644 --- a/lib/prototypes.h +++ b/lib/prototypes.h @@ -472,7 +472,19 @@ extern int set_filesize_limit (int blocks); /* user_busy.c */ extern int user_busy (const char *name, uid_t uid); -/* utmp.c */ +/* + * Session management: utmp.c or logind.c + */ + +/** + * @brief Get host for the current session + * + * @param[out] out Host name + * + * @return 0 or a positive integer if the host was obtained properly, + * another value on error. + */ +extern int get_session_host (char **out); extern /*@null@*/struct utmp *get_current_utmp (void); extern struct utmp *prepare_utmp (const char *name, const char *line, diff --git a/libmisc/Makefile.am b/libmisc/Makefile.am index cc24901e4..6723a7c7c 100644 --- a/libmisc/Makefile.am +++ b/libmisc/Makefile.am @@ -94,4 +94,4 @@ endif if ENABLE_LASTLOG libmisc_la_SOURCES += log.c -endif +endif \ No newline at end of file diff --git a/libmisc/logind.c b/libmisc/logind.c new file mode 100644 index 000000000..ac1b3a141 --- /dev/null +++ b/libmisc/logind.c @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2023, Iker Pedrosa + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#ident "$Id$" + +#include "defines.h" +#include "prototypes.h" + +#include + +int get_session_host (char **out) +{ + char *host = NULL; + char *session = NULL; + int ret; + + ret = sd_pid_get_session (getpid(), &session); + if (ret < 0) { + return ret; + } + ret = sd_session_get_remote_host (session, &host); + if (ret < 0) { + goto done; + } + + *out = host; + +done: + free (session); + return ret; +} diff --git a/libmisc/utmp.c b/libmisc/utmp.c index 20b6c9bfc..c1bc19725 100644 --- a/libmisc/utmp.c +++ b/libmisc/utmp.c @@ -101,6 +101,32 @@ static bool is_my_tty (const char tty[UT_LINESIZE]) return ret; } +int get_session_host (char **out) +{ + char *hostname = NULL; + struct utmp *ut = NULL; + int ret = 0; + + ut = get_current_utmp(); + +#ifdef HAVE_STRUCT_UTMP_UT_HOST + if ((ut != NULL) && (ut->ut_host[0] != '\0')) { + hostname = XMALLOC(sizeof(ut->ut_host) + 1, char); + strncpy (hostname, ut->ut_host, sizeof (ut->ut_host)); + hostname[sizeof (ut->ut_host)] = '\0'; + *out = hostname; + free (ut); + } else { + *out = NULL; + ret = -2; + } +#else + *out = NULL; + ret = -2; +#endif /* HAVE_STRUCT_UTMP_UT_HOST */ + + return ret; +} #ifndef USE_PAM /*