#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
-#include <glib/gstdio.h>
-#include <utmp.h>
#include <paths.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <sys/socket.h>
#include <sys/inotify.h>
-#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <glib/gstdio.h>
#include "tlm-config.h"
#include "tlm-config-general.h"
-#define HOST_NAME_SIZE 256
-
void
g_clear_string (gchar **str)
{
return TRUE;
}
-static gchar *
-_get_tty_id (
- const gchar *tty_name)
-{
- gchar *id = NULL;
- const gchar *tmp = tty_name;
-
- while (tmp) {
- if (isdigit (*tmp)) {
- id = g_strdup (tmp);
- break;
- }
- tmp++;
- }
- return id;
-}
-
-static size_t
-_get_host_address (
- const gchar *hostname, gchar** hostaddress)
-{
- struct addrinfo hints, *info = NULL;
- size_t sz_hostaddress = 0;
-
- if (!hostname) return 0;
-
- memset (&hints, 0, sizeof (hints));
- hints.ai_flags = AI_ADDRCONFIG;
-
- if (getaddrinfo (hostname, NULL, &hints, &info) == 0) {
- if (info) {
- if (info->ai_family == AF_INET) {
- struct sockaddr_in *sa = (struct sockaddr_in *) info->ai_addr;
- sz_hostaddress = sizeof(struct in_addr);
- *hostaddress = g_malloc0 (sz_hostaddress);
- if (!*hostaddress) {
- CRITICAL("g_malloc0 memory allocation failure");
- freeaddrinfo (info);
- return 0;
- }
- memcpy (*hostaddress, &(sa->sin_addr), sz_hostaddress);
- } else if (info->ai_family == AF_INET6) {
- struct sockaddr_in6 *sa = (struct sockaddr_in6 *) info->ai_addr;
- sz_hostaddress = sizeof(struct in6_addr);
- *hostaddress = g_malloc0 (sz_hostaddress);
- if (!*hostaddress) {
- CRITICAL("g_malloc0 memory allocation failure");
- freeaddrinfo (info);
- return 0;
- }
- memcpy (*hostaddress, &(sa->sin6_addr), sz_hostaddress);
- }
- freeaddrinfo (info);
- }
- }
- return sz_hostaddress;
-}
-
-static gboolean
-_is_tty_same (
- const gchar *tty1_name,
- const gchar *tty2_name)
-{
- gchar *tty1 = NULL, *tty2 = NULL;
- gboolean res = FALSE;
-
- if (tty1_name == tty2_name) return TRUE;
- if (!tty1_name || !tty2_name) return FALSE;
-
- if (*tty1_name == '/') tty1 = g_strdup (tty1_name);
- else tty1 = g_strdup_printf ("/dev/%s", tty1_name);
- if (*tty2_name == '/') tty2 = g_strdup (tty2_name);
- else tty2 = g_strdup_printf ("/dev/%s", tty2_name);
-
- res = (g_strcmp0 (tty1_name, tty2_name) == 0);
-
- g_free (tty1);
- g_free (tty2);
- return res;
-}
-
-static gchar *
-_get_host_name ()
-{
- gchar *name = g_malloc0 (HOST_NAME_SIZE);
- if (!name) {
- CRITICAL("g_malloc0 memory allocation failure");
- return NULL;
- }
- if (gethostname (name, HOST_NAME_SIZE) != 0) {
- g_free (name);
- return NULL;
- }
- return name;
-}
-
-void
-tlm_utils_log_utmp_entry (const gchar *username)
-{
- struct timeval tv;
- pid_t pid;
- struct utmp ut_ent;
- struct utmp *ut_tmp = NULL;
- gchar *hostname = NULL, *hostaddress = NULL;
- size_t sz_hostaddress;
- const gchar *tty_name = NULL;
- gchar *tty_no_dev_name = NULL, *tty_id = NULL;
- gchar tty_name_buf[TTY_NAME_MAX+1] = {0,};
-
- DBG ("Log session entry to utmp/wtmp");
-
- hostname = _get_host_name ();
- sz_hostaddress = _get_host_address (hostname, &hostaddress);
-
- if (0 == ttyname_r(0, tty_name_buf, TTY_NAME_MAX+1))
- tty_name = tty_name_buf;
-
- if (tty_name) {
- tty_no_dev_name = g_strdup (strncmp(tty_name, "/dev/", 5) == 0 ?
- tty_name + 5 : tty_name);
- }
- tty_id = _get_tty_id (tty_no_dev_name);
- pid = getpid ();
- utmpname (_PATH_UTMP);
-
- setutent ();
- while ((ut_tmp = getutent())) {
- if ( (ut_tmp->ut_pid == pid) &&
- (ut_tmp->ut_id[0] != '\0') &&
- (ut_tmp->ut_type == LOGIN_PROCESS ||
- ut_tmp->ut_type == USER_PROCESS) &&
- (_is_tty_same (ut_tmp->ut_line, tty_name))) {
- break;
- }
- }
-
- if (ut_tmp) memcpy (&ut_ent, ut_tmp, sizeof (ut_ent));
- else memset (&ut_ent, 0, sizeof (ut_ent));
-
- ut_ent.ut_type = USER_PROCESS;
- ut_ent.ut_pid = pid;
- if (tty_id)
- strncpy (ut_ent.ut_id, tty_id, sizeof (ut_ent.ut_id)-1);
- if (username)
- strncpy (ut_ent.ut_user, username, sizeof (ut_ent.ut_user)-1);
- if (tty_no_dev_name)
- strncpy (ut_ent.ut_line, tty_no_dev_name, sizeof (ut_ent.ut_line)-1);
- if (hostname)
- strncpy (ut_ent.ut_host, hostname, sizeof (ut_ent.ut_host)-1);
- if (hostaddress)
- memcpy (&ut_ent.ut_addr_v6, hostaddress, sz_hostaddress);
-
- ut_ent.ut_session = getsid (0);
- gettimeofday (&tv, NULL);
-#ifdef _HAVE_UT_TV
- ut_ent.ut_tv.tv_sec = tv.tv_sec;
- ut_ent.ut_tv.tv_usec = tv.tv_usec;
-#else
- ut_ent.ut_time = tv.tv_sec;
-#endif
-
- pututline (&ut_ent);
- endutent ();
-
- updwtmp (_PATH_WTMP, &ut_ent);
-
- g_free (hostaddress);
- g_free (hostname);
- g_free (tty_no_dev_name);
- g_free (tty_id);
-}
-
static gchar **
_split_command_line_with_regex(const char *command, GRegex *regex) {
gchar **temp_strv = NULL;