xrdp/sesman/sessionrecord.c

132 lines
3.4 KiB
C
Raw Normal View History

2017-09-21 15:01:20 +08: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.
*/
/**
*
* @file sessionrecord.c
2017-09-21 15:01:20 +08:00
* @brief utmp/wtmp handling code
2017-11-12 00:22:26 +08:00
* Idea: Only implement actual utmp, i.e. utmpx for 99%.
* See http://80386.nl/unix/utmpx/
2017-09-21 15:01:20 +08:00
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
#include <paths.h>
2017-09-21 15:01:20 +08:00
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
2017-09-21 15:01:20 +08:00
#include "log.h"
#include "os_calls.h"
#include "sessionrecord.h"
2017-09-21 15:01:20 +08:00
/*
* Prepare the utmp/ struct and write it.
2017-09-21 15:01:20 +08:00
* this can handle login and logout at once with the 'state' parameter
*/
int
add_xtmp_entry(int pid, const char *line, const char *user, const char *rhostname, short state)
{
_utmp ut;
2017-09-21 15:01:20 +08:00
struct timeval tv;
char *hostname = 0;
/* The string rhostname containt too much data, only get the ip
* the format is
* "2001:123:12:1234:1234:1234:1234:1234:53194 - socket: 12"
* "::ffff:99.99.9.999:51165 - socket: 12"
* "99.99.9.999:51165 - socket: 12"
*
* So the IP is the string up the two last colons
*/
int i = g_strlen(rhostname) - 1;
while ((i>0) && (rhostname[i] != ':'))
{
i--;
}
i--;
while ((i>0) && (rhostname[i] != ':'))
{
i--;
}
2017-11-12 18:58:45 +08:00
hostname = g_strndup(rhostname, i);
2017-09-21 15:01:20 +08:00
g_memset(&ut, 0, sizeof(ut));
2017-09-21 15:01:20 +08:00
ut.ut_type=state;
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, line , sizeof(ut.ut_line));
g_strncpy(ut.ut_user, user , sizeof(ut.ut_user));
g_strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
2017-09-21 15:01:20 +08:00
/* utmp */
2017-09-22 13:43:10 +08:00
setutxent();
2017-09-21 15:01:20 +08:00
pututxline(&ut);
2017-11-12 00:22:26 +08:00
endutxent();
2017-09-21 15:01:20 +08:00
2017-11-12 00:22:26 +08:00
/* wtmp : update on linux, FreeBSD uses utx */
#ifdef HAVE_UTMPX_H
2017-11-12 00:22:26 +08:00
#if !defined(__FreeBSD__)
updwtmpx(_PATH_WTMP, &ut);
2017-11-12 00:22:26 +08:00
#endif
#elif defined(HAVE_UTMP_H)
2017-11-12 00:22:26 +08:00
/* Does such system still exist ? */
log_message(LOG_LEVEL_DEBUG, "HAVE_UTMP_H");
updwtmp("/var/log/wtmp", &ut);
#endif
2017-11-12 18:58:45 +08:00
g_free(hostname);
2017-10-06 09:55:37 +08:00
2017-09-21 15:01:20 +08:00
return 0;
}
2017-09-22 13:43:10 +08:00
int
2017-09-21 15:01:20 +08:00
utmp_login(int pid, int display, const char *user, const char *rhostname)
{
char str_display[16];
2017-09-22 13:43:10 +08:00
log_message(LOG_LEVEL_DEBUG,
2017-10-06 09:55:37 +08:00
"adding login info for utmp/wtmp: %d - %d - %s - %s",
pid, display, user, rhostname);
2017-09-21 15:01:20 +08:00
g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display);
2017-10-06 09:55:37 +08:00
2017-09-21 15:01:20 +08:00
return add_xtmp_entry(pid, str_display, user, rhostname, USER_PROCESS);
}
2017-09-22 13:43:10 +08:00
int
2017-09-21 15:01:20 +08:00
utmp_logout(int pid, int display, const char *user, const char *rhostname)
{
char str_display[16];
2017-09-22 13:43:10 +08:00
log_message(LOG_LEVEL_DEBUG,
2017-10-06 09:55:37 +08:00
"adding logout info for utmp/wtmp: %d - %d - %s - %s",
pid, display, user, rhostname);
2017-09-21 15:01:20 +08:00
g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display);
2017-10-06 09:55:37 +08:00
2017-09-21 15:01:20 +08:00
return add_xtmp_entry(pid, str_display, user, rhostname, DEAD_PROCESS);
}