From 04c67a50392d4d597d2d4d5b39ca81410174eaa1 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Thu, 29 Jun 2023 17:06:28 +0100 Subject: [PATCH] Set ut_id field The utmp record is generally looked up by the ut_id field. Setting this field means we can use a blank username and host when the session exits --- sesman/sesexec/session.c | 7 ++---- sesman/sesexec/sessionrecord.c | 45 ++++++++++++++++++++-------------- sesman/sesexec/sessionrecord.h | 19 ++++++++++---- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/sesman/sesexec/session.c b/sesman/sesexec/session.c index 7eb5399b..f8f7af86 100644 --- a/sesman/sesexec/session.c +++ b/sesman/sesexec/session.c @@ -657,9 +657,7 @@ session_start_wrapped(struct login_info *login_info, } else { - utmp_login(window_manager_pid, - s->display, login_info->username, - login_info->ip_addr); + utmp_login(window_manager_pid, s->display, login_info); LOG(LOG_LEVEL_INFO, "Starting the xrdp channel server for display :%d", s->display); @@ -890,8 +888,7 @@ session_process_child_exit(struct session_data *sd, sd->win_mgr, sd->params.display, wm_wait_time); } - utmp_logout(sd->win_mgr, sd->params.display, g_login_info->username, - g_login_info->ip_addr); + utmp_logout(sd->win_mgr, sd->params.display); sd->win_mgr = -1; if (sd->x_server > 0) diff --git a/sesman/sesexec/sessionrecord.c b/sesman/sesexec/sessionrecord.c index dcc7aea1..d1aa6c9a 100644 --- a/sesman/sesexec/sessionrecord.c +++ b/sesman/sesexec/sessionrecord.c @@ -69,29 +69,42 @@ typedef struct utmp _utmp; #include "string_calls.h" #define XRDP_LINE_FORMAT "xrdp:%d" +// ut_id is a very small field on some platforms, so use the display +// number in hex +#define XRDP_ID_FORMAT ":%x" /* * Prepare the utmp struct and write it. * this can handle login and logout at once with the 'mode' parameter */ -void -add_xtmp_entry(int pid, const char *display_id, const char *user, const char *rhostname, enum add_xtmp_mode mode) +static void +add_xtmp_entry(int pid, int display, const struct login_info *login_info, + enum add_xtmp_mode mode) { #if USE_UTMP + char idbuff[16]; + char str_display[16]; + _utmp ut; struct timeval tv; g_memset(&ut, 0, sizeof(ut)); + g_snprintf(str_display, sizeof(str_display), XRDP_LINE_FORMAT, display); + g_snprintf(idbuff, sizeof(idbuff), XRDP_ID_FORMAT, display); + gettimeofday(&tv, NULL); ut.ut_type = (mode == MODE_LOGIN) ? USER_PROCESS : DEAD_PROCESS; ut.ut_pid = pid; - gettimeofday(&tv, NULL); ut.ut_tv.tv_sec = tv.tv_sec; ut.ut_tv.tv_usec = tv.tv_usec; - g_strncpy(ut.ut_line, display_id, sizeof(ut.ut_line)); - g_strncpy(ut.ut_user, user , sizeof(ut.ut_user)); - g_strncpy(ut.ut_host, rhostname, sizeof(ut.ut_host)); + g_strncpy(ut.ut_line, str_display, sizeof(ut.ut_line)); + g_strncpy(ut.ut_id, idbuff, sizeof(ut.ut_id)); + if (login_info != NULL) + { + g_strncpy(ut.ut_user, login_info->username , sizeof(ut.ut_user)); + g_strncpy(ut.ut_host, login_info->ip_addr, sizeof(ut.ut_host)); + } /* update the utmp file */ /* open utmp */ @@ -105,27 +118,21 @@ add_xtmp_entry(int pid, const char *display_id, const char *user, const char *rh } void -utmp_login(int pid, int display, const char *user, const char *rhostname) +utmp_login(int pid, int display, const struct login_info *login_info) { - char str_display[16]; - log_message(LOG_LEVEL_DEBUG, "adding login info for utmp: %d - %d - %s - %s", - pid, display, user, rhostname); - g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display); + pid, display, login_info->username, login_info->ip_addr); - add_xtmp_entry(pid, str_display, user, rhostname, MODE_LOGIN); + add_xtmp_entry(pid, display, login_info, MODE_LOGIN); } void -utmp_logout(int pid, int display, const char *user, const char *rhostname) +utmp_logout(int pid, int display) { - char str_display[16]; - log_message(LOG_LEVEL_DEBUG, - "adding logout info for utmp: %d - %d - %s - %s", - pid, display, user, rhostname); - g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display); + log_message(LOG_LEVEL_DEBUG, "adding logout info for utmp: %d - %d", + pid, display); - add_xtmp_entry(pid, str_display, user, rhostname, MODE_LOGOUT); + add_xtmp_entry(pid, display, NULL, MODE_LOGOUT); } diff --git a/sesman/sesexec/sessionrecord.h b/sesman/sesexec/sessionrecord.h index 3fa73bd2..28a26b49 100644 --- a/sesman/sesexec/sessionrecord.h +++ b/sesman/sesexec/sessionrecord.h @@ -19,21 +19,30 @@ /** * * @file sessionrecord.h - * @brief utmp/wtmp handling code + * @brief utmp handling code * */ #ifndef SESSIONRECORD_H #define SESSIONRECORD_H +struct login_info; /** - * @brief functions for adding utmp entries. one at login, one for logout + * @brief Record login in utmp * - * @param pid of the session, display, login, and hostname + * @param pid PID of window manager + * @param display Display number + * @param login_info Information about logged in user */ -void utmp_login(int pid, int display, const char *user, const char *rhostname); +void utmp_login(int pid, int display, const struct login_info *login_info); -void utmp_logout(int pid, int display, const char *user, const char *rhostname); +/** + * @brief Record logout in utmp + * + * @param pid PID of window manager + * @param display Display number + */ +void utmp_logout(int pid, int display); #endif