2017-09-21 10:01:20 +03:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
|
|
|
* Copyright (C) Emmanuel Blindauer 2017
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2023-06-30 12:09:49 +03:00
|
|
|
*
|
|
|
|
* str2memcpy() is taken from util-linux/include/strutils.h v2.39 which
|
|
|
|
* has the following header:-
|
|
|
|
*
|
|
|
|
* No copyright is claimed. This code is in the public domain; do with
|
|
|
|
* it what you wish.
|
2017-09-21 10:01:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-09-26 22:06:44 +03:00
|
|
|
* @file sessionrecord.c
|
2023-06-29 18:09:43 +03:00
|
|
|
* @brief utmp handling code
|
|
|
|
*
|
|
|
|
* wtmp/lastlog/btmp is handled by PAM or (on FreeBSD) UTX
|
|
|
|
*
|
2017-11-11 19:22:26 +03:00
|
|
|
* Idea: Only implement actual utmp, i.e. utmpx for 99%.
|
|
|
|
* See http://80386.nl/unix/utmpx/
|
2017-09-21 10:01:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config_ac.h>
|
|
|
|
#endif
|
|
|
|
|
2023-06-30 12:45:14 +03:00
|
|
|
#include "sessionrecord.h"
|
|
|
|
#include "login_info.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
// Operational mode of add_xtmp_entry()
|
|
|
|
//
|
|
|
|
// We can't use USER_PROCESS/DEAD_PROCESS directly, as they
|
|
|
|
// won't be available for platforms without USE_UTMP
|
|
|
|
enum add_xtmp_mode
|
|
|
|
{
|
|
|
|
MODE_LOGIN,
|
|
|
|
MODE_LOGOUT
|
|
|
|
};
|
|
|
|
|
2023-06-29 18:45:50 +03:00
|
|
|
#ifdef USE_UTMP
|
|
|
|
|
2017-10-06 04:50:54 +03:00
|
|
|
#include <sys/time.h>
|
2023-06-30 12:09:49 +03:00
|
|
|
#include <string.h>
|
2017-09-26 22:06:44 +03:00
|
|
|
#include <unistd.h>
|
2017-09-22 09:21:14 +03:00
|
|
|
|
2018-03-29 20:26:20 +03:00
|
|
|
#ifdef HAVE_UTMPX_H
|
|
|
|
#include <utmpx.h>
|
|
|
|
typedef struct utmpx _utmp;
|
|
|
|
#else
|
2023-06-29 18:45:50 +03:00
|
|
|
#include <utmp.h>
|
2018-03-29 20:26:20 +03:00
|
|
|
typedef struct utmp _utmp;
|
|
|
|
#endif
|
|
|
|
|
2023-06-29 18:45:50 +03:00
|
|
|
#include "os_calls.h"
|
|
|
|
#include "string_calls.h"
|
2018-03-29 20:26:20 +03:00
|
|
|
|
|
|
|
#define XRDP_LINE_FORMAT "xrdp:%d"
|
2023-06-29 19:06:28 +03:00
|
|
|
// ut_id is a very small field on some platforms, so use the display
|
|
|
|
// number in hex
|
|
|
|
#define XRDP_ID_FORMAT ":%x"
|
2018-03-29 20:26:20 +03:00
|
|
|
|
2023-06-30 12:09:49 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* utmp-specific strncpy() replacement
|
|
|
|
*
|
|
|
|
* @param dest Destination pointer
|
|
|
|
* @param src Source pointer
|
|
|
|
* @param n bytes to copy
|
|
|
|
*
|
|
|
|
* This is like strncpy(), but based on memcpy(), so compilers and static
|
|
|
|
* analyzers do not complain when sizeof(destination) is the same as 'n' and
|
|
|
|
* result is not terminated by zero.
|
|
|
|
*
|
|
|
|
* ONLY use this function to copy string to logs with fixed sizes
|
|
|
|
* (wtmp/utmp. ...) where string terminator is optional.
|
|
|
|
*/
|
|
|
|
static inline void *__attribute__((nonnull (1)))
|
|
|
|
str2memcpy(void *dest, const char *src, size_t n)
|
|
|
|
{
|
|
|
|
size_t bytes = strlen(src) + 1;
|
|
|
|
|
|
|
|
if (bytes > n)
|
|
|
|
{
|
|
|
|
bytes = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dest, src, bytes);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
2018-03-29 20:26:20 +03:00
|
|
|
* Prepare the utmp struct and write it.
|
2023-06-30 12:09:49 +03:00
|
|
|
*
|
|
|
|
* @param pid PID of session manager
|
|
|
|
* @param display Display number of session
|
|
|
|
* @param login_info Login info (NULL for MODE_LOGOUT)
|
|
|
|
* @param mode see enum add_xtmp_mode
|
|
|
|
* @param e Exit status (NULL unless MODE_LOGOUT)
|
2017-09-21 10:01:20 +03:00
|
|
|
*/
|
|
|
|
|
2023-06-29 19:06:28 +03:00
|
|
|
static void
|
|
|
|
add_xtmp_entry(int pid, int display, const struct login_info *login_info,
|
2023-06-29 19:27:06 +03:00
|
|
|
enum add_xtmp_mode mode, const struct proc_exit_status *e)
|
2017-09-21 10:01:20 +03:00
|
|
|
{
|
2023-06-29 19:06:28 +03:00
|
|
|
char idbuff[16];
|
|
|
|
char str_display[16];
|
|
|
|
|
2017-09-26 22:06:44 +03:00
|
|
|
_utmp ut;
|
2017-09-21 10:01:20 +03:00
|
|
|
struct timeval tv;
|
|
|
|
|
2017-11-12 19:18:15 +03:00
|
|
|
g_memset(&ut, 0, sizeof(ut));
|
2023-06-29 19:06:28 +03:00
|
|
|
g_snprintf(str_display, sizeof(str_display), XRDP_LINE_FORMAT, display);
|
|
|
|
g_snprintf(idbuff, sizeof(idbuff), XRDP_ID_FORMAT, display);
|
|
|
|
gettimeofday(&tv, NULL);
|
2017-09-21 10:01:20 +03:00
|
|
|
|
2023-06-30 12:45:14 +03:00
|
|
|
ut.ut_type = (mode == MODE_LOGIN) ? USER_PROCESS : DEAD_PROCESS;
|
2017-09-21 10:01:20 +03:00
|
|
|
ut.ut_pid = pid;
|
2023-06-30 12:09:49 +03:00
|
|
|
str2memcpy(ut.ut_id, idbuff, sizeof(ut.ut_id));
|
|
|
|
|
|
|
|
// Linux utmp(5) suggests ut_line, ut_time, ut_user, and ut_host
|
|
|
|
// are not set for a DEAD_PROCESS
|
|
|
|
if (ut.ut_type != DEAD_PROCESS)
|
2023-06-29 19:06:28 +03:00
|
|
|
{
|
2023-06-30 12:09:49 +03:00
|
|
|
ut.ut_tv.tv_sec = tv.tv_sec;
|
|
|
|
ut.ut_tv.tv_usec = tv.tv_usec;
|
|
|
|
str2memcpy(ut.ut_line, str_display, sizeof(ut.ut_line));
|
|
|
|
if (login_info != NULL)
|
|
|
|
{
|
|
|
|
str2memcpy(ut.ut_user, login_info->username, sizeof(ut.ut_user));
|
2023-06-29 19:27:06 +03:00
|
|
|
#ifdef HAVE_UTMPX_UT_HOST
|
2023-06-30 12:09:49 +03:00
|
|
|
str2memcpy(ut.ut_host, login_info->ip_addr, sizeof(ut.ut_host));
|
2023-06-29 19:27:06 +03:00
|
|
|
#endif
|
2023-06-30 12:09:49 +03:00
|
|
|
}
|
2023-06-29 19:27:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_UTMPX_UT_EXIT
|
|
|
|
if (e != NULL && e->reason == E_PXR_STATUS_CODE)
|
|
|
|
{
|
|
|
|
ut.ut_exit.e_exit = e->val;
|
2023-06-29 19:06:28 +03:00
|
|
|
}
|
2023-06-29 19:27:06 +03:00
|
|
|
else if (e != NULL && e->reason == E_PXR_SIGNAL)
|
|
|
|
{
|
|
|
|
ut.ut_exit.e_termination = e->val;
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-21 10:01:20 +03:00
|
|
|
|
2018-03-29 20:26:20 +03:00
|
|
|
/* update the utmp file */
|
|
|
|
/* open utmp */
|
2017-09-22 08:43:10 +03:00
|
|
|
setutxent();
|
2018-03-29 20:26:20 +03:00
|
|
|
/* add the computed entry */
|
2017-09-21 10:01:20 +03:00
|
|
|
pututxline(&ut);
|
2018-03-29 20:26:20 +03:00
|
|
|
/* closes utmp */
|
2017-11-11 19:22:26 +03:00
|
|
|
endutxent();
|
2023-06-29 18:45:50 +03:00
|
|
|
|
2017-09-21 10:01:20 +03:00
|
|
|
}
|
2023-06-30 12:09:49 +03:00
|
|
|
#else // USE_UTMP
|
|
|
|
static void
|
|
|
|
add_xtmp_entry(int pid, int display, const struct login_info *login_info,
|
|
|
|
short state, const struct proc_exit_status *e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-21 10:01:20 +03:00
|
|
|
|
2023-06-30 12:09:49 +03:00
|
|
|
/******************************************************************************/
|
2018-03-29 20:26:20 +03:00
|
|
|
void
|
2023-06-29 19:06:28 +03:00
|
|
|
utmp_login(int pid, int display, const struct login_info *login_info)
|
2017-09-21 10:01:20 +03:00
|
|
|
{
|
2017-09-22 08:43:10 +03:00
|
|
|
log_message(LOG_LEVEL_DEBUG,
|
2023-06-29 18:09:43 +03:00
|
|
|
"adding login info for utmp: %d - %d - %s - %s",
|
2023-06-29 19:06:28 +03:00
|
|
|
pid, display, login_info->username, login_info->ip_addr);
|
2017-10-06 04:55:37 +03:00
|
|
|
|
2023-06-29 19:27:06 +03:00
|
|
|
add_xtmp_entry(pid, display, login_info, MODE_LOGIN, NULL);
|
2017-09-21 10:01:20 +03:00
|
|
|
}
|
|
|
|
|
2023-06-30 12:09:49 +03:00
|
|
|
/******************************************************************************/
|
2018-03-29 20:26:20 +03:00
|
|
|
void
|
2023-06-29 19:27:06 +03:00
|
|
|
utmp_logout(int pid, int display, const struct proc_exit_status *exit_status)
|
2017-09-21 10:01:20 +03:00
|
|
|
{
|
|
|
|
|
2023-06-29 19:06:28 +03:00
|
|
|
log_message(LOG_LEVEL_DEBUG, "adding logout info for utmp: %d - %d",
|
|
|
|
pid, display);
|
2017-10-06 04:55:37 +03:00
|
|
|
|
2023-06-29 19:27:06 +03:00
|
|
|
add_xtmp_entry(pid, display, NULL, MODE_LOGOUT, exit_status);
|
2017-09-21 10:01:20 +03:00
|
|
|
}
|