diff options
author | BLINDAUER Emmanuel <e.blindauer@gmail.com> | 2017-09-21 10:01:20 +0300 |
---|---|---|
committer | Koichiro IWAO <meta@vmeta.jp> | 2018-08-02 03:02:03 +0300 |
commit | 9d6537ae2a70956a97860e0ac2094fb165cb1309 (patch) | |
tree | 7f9b3e09c032194325141b4ddcf84a861ea0e961 | |
parent | a9e2dcc99ff97f0590d6723a48c28b90e467d5f2 (diff) |
Initial support for utmp/wtmp on linux
-rw-r--r-- | sesman/session.c | 3 | ||||
-rw-r--r-- | sesman/utmp.c | 95 | ||||
-rw-r--r-- | sesman/utmp.h | 44 |
3 files changed, 141 insertions, 1 deletions
diff --git a/sesman/session.c b/sesman/session.c index 0d9fdc70..aa3abd12 100644 --- a/sesman/session.c +++ b/sesman/session.c @@ -520,6 +520,7 @@ session_start_fork(tbus data, tui8 type, struct SCP_CONNECTION *c, */ } #endif + utmp_login(g_getpid(), display, s->username, s->client_ip); window_manager_pid = g_fork(); /* parent becomes X, child forks wm, and waits, todo */ if (window_manager_pid == -1) @@ -942,8 +943,8 @@ session_kill(int pid) { /* deleting the session */ log_message(LOG_LEVEL_INFO, "++ terminated session: username %s, display :%d.0, session_pid %d, ip %s", tmp->item->name, tmp->item->display, tmp->item->pid, tmp->item->client_ip); + utmp_logout(tmp->item->pid, tmp->item->display, tmp->item->name, tmp->item->client_ip); g_free(tmp->item); - if (prev == 0) { /* prev does no exist, so it's the first element - so we set diff --git a/sesman/utmp.c b/sesman/utmp.c new file mode 100644 index 00000000..7a6aa351 --- /dev/null +++ b/sesman/utmp.c @@ -0,0 +1,95 @@ +/** + * 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 utmp.c + * @brief utmp/wtmp handling code + * + */ + +#if defined(HAVE_CONFIG_H) +#include <config_ac.h> +#endif + +#include <string.h> +#include <stdlib.h> +#include <pwd.h> +#include <unistd.h> +#include <stdio.h> +#include "log.h" + +#include <utmp.h> +#include <utmpx.h> + +/* + * Prepare the utmpx struct and write it. + * 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) +{ + struct utmpx ut; + struct timeval tv; + + memset (&ut, 0, sizeof (ut)); + + 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; + strncpy(ut.ut_line, line , sizeof (ut.ut_line)); + strncpy(ut.ut_user, user , sizeof (ut.ut_user)); + strncpy(ut.ut_host, rhostname, sizeof (ut.ut_host)); + + /* utmp */ + setutxent(); + pututxline(&ut); + endutxent (); + + /* wtmp XXX hardcoded! */ + updwtmpx("/var/log/wtmp", &ut); + + return 0; +} + +int +utmp_login(int pid, int display, const char *user, const char *rhostname) +{ + char str_display[16]; + + log_message(LOG_LEVEL_DEBUG, + "adding login info for utmp/wtmp: %d - %d - %s - %s", + pid, display, user, rhostname); + g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display); + return add_xtmp_entry(pid, str_display, user, rhostname, USER_PROCESS); +} + +int +utmp_logout(int pid, int display, const char *user, const char *rhostname) +{ + char str_display[16]; + + log_message(LOG_LEVEL_DEBUG, + "adding logout info for utmp/wtmp: %d - %d - %s - %s", + pid, display, user, rhostname); + g_snprintf(str_display, 15, XRDP_LINE_FORMAT, display); + return add_xtmp_entry(pid, str_display, user, rhostname, DEAD_PROCESS); +} diff --git a/sesman/utmp.h b/sesman/utmp.h new file mode 100644 index 00000000..636450f3 --- /dev/null +++ b/sesman/utmp.h @@ -0,0 +1,44 @@ +/** + * 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 utmp.h + * @brief utmp/wtmp handling code + * + */ + +#ifndef UTMP_H +#define UTMP_H + +#define XRDP_LINE_FORMAT "xrdp:%d" +/** + * + * @brief + * + * @param pid + * @return 0 + */ + +int add_xtmp_entry(int pid, const char *line, const char *user, const char *rhostname, short state); + +int utmp_login(int pid, int display, const char *user, const char *rhostname); + +int utmp_logout(int pid, int display, const char *user, const char *rhostname); + +#endif |