gdm-session-record.c 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006 Ray Strode <rstrode@redhat.com>
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
18 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if defined(HAVE_UTMPX_H)
#include <utmpx.h>
#endif

#if defined(HAVE_UTMP_H)
#include <utmp.h>
#endif

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include "gdm-session-record.h"

#ifndef GDM_BAD_SESSION_RECORDS_FILE
#define GDM_BAD_SESSION_RECORDS_FILE "/var/log/btmp"
#endif

46 47 48 49 50 51 52 53 54 55 56 57 58 59
#if !defined(GDM_NEW_SESSION_RECORDS_FILE)
#    if defined(WTMPX_FILE)
#        define GDM_NEW_SESSION_RECORDS_FILE WTMPX_FILE
#    elif defined(_PATH_WTMPX)
#        define GDM_NEW_SESSION_RECORDS_FILE _PATH_WTMPX
#    elif defined(WTMPX_FILENAME)
#        define GDM_NEW_SESSION_RECORDS_FILE WTMPX_FILENAME
#    elif defined(WTMP_FILE)
#        define GDM_NEW_SESSION_RECORDS_FILE WTMP_FILE
#    elif defined(_PATH_WTMP) /* BSD systems */
#        define GDM_NEW_SESSION_RECORDS_FILE _PATH_WTMP
#    else
#        define GDM_NEW_SESSION_RECORDS_FILE "/var/log/wtmp"
#    endif
60 61
#endif

62 63
static void
record_set_username (UTMP       *u,
64
                     const char *username)
65
{
66 67 68 69 70
#if defined(HAVE_UT_UT_USER)
        strncpy (u->ut_user,
                 username,
                 sizeof (u->ut_user));
        g_debug ("using ut_user %.*s",
71
                 (int) sizeof (u->ut_user),
72 73 74
                 u->ut_user);
#elif defined(HAVE_UT_UT_NAME)
        strncpy (u->ut_name,
75
                 username,
76 77
                 sizeof (u->ut_name));
        g_debug ("using ut_name %.*s",
78
                 (int) sizeof (u->ut_name),
79
                 u->ut_name);
80
#endif
81
}
82

83 84 85
static void
record_set_timestamp (UTMP *u)
{
86
#if defined(HAVE_UT_UT_TV)
87 88
        GTimeVal    now = { 0 };

89 90
        /* Set time in TV format */
        g_get_current_time (&now);
91 92
        u->ut_tv.tv_sec  = now.tv_sec;
        u->ut_tv.tv_usec = now.tv_usec;
93
        g_debug ("using ut_tv time %ld",
94
                 (glong) u->ut_tv.tv_sec);
95 96
#elif defined(HAVE_UT_UT_TIME)
        /* Set time in time format */
97
        time (&u->ut_time);
98
        g_debug ("using ut_time %ld",
99
                 (glong) u->ut_time);
100
#endif
101
}
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static void
record_set_pid (UTMP *u,
                GPid  pid)
{
#if defined(HAVE_UT_UT_PID)
        /* Set pid */
        if (pid != 0) {
                u->ut_pid = pid;
        }
        g_debug ("using ut_pid %d", (int) u->ut_pid);
#endif
}

static void
record_set_host (UTMP       *u,
                 const char *x11_display_name,
                 const char *host_name)
{
        char *hostname;
122 123 124 125 126 127 128 129

#if defined(HAVE_UT_UT_HOST)
        hostname = NULL;

        /*
         * Set ut_host to hostname:$DISPLAY if remote, otherwise set
         * to $DISPLAY
         */
130 131 132
        if (host_name != NULL
            && x11_display_name != NULL
            && g_str_has_prefix (x11_display_name, ":")) {
133
                hostname = g_strdup_printf ("%s%s", host_name, x11_display_name);
134 135 136 137 138
        } else {
                hostname = g_strdup (x11_display_name);
        }

        if (hostname != NULL) {
139
                strncpy (u->ut_host, hostname, sizeof (u->ut_host));
140
                g_debug ("using ut_host %.*s", (int) sizeof (u->ut_host), u->ut_host);
141
#ifdef HAVE_UT_UT_SYSLEN
142
                u->ut_syslen = MIN (strlen (hostname), sizeof (u->ut_host));
143
#endif
144
                g_free (hostname);
145 146
        }
#endif
147
}
148

149 150 151 152 153
static void
record_set_line (UTMP       *u,
                 const char *display_device,
                 const char *x11_display_name)
{
154 155 156 157 158
        /*
         * Set ut_line to the device name associated with this display
         * but remove the "/dev/" prefix.  If no device, then use the
         * $DISPLAY value.
         */
159 160
        if (display_device != NULL
            && g_str_has_prefix (display_device, "/dev/")) {
161
                strncpy (u->ut_line,
162
                         display_device + strlen ("/dev/"),
163
                         sizeof (u->ut_line));
164
        } else if (x11_display_name != NULL) {
165
                strncpy (u->ut_line,
166
                         x11_display_name,
167
                         sizeof (u->ut_line));
168 169
        }

170
        g_debug ("using ut_line %.*s", (int) sizeof (u->ut_line), u->ut_line);
171 172 173 174 175 176 177 178 179 180
}

void
gdm_session_record_login (GPid                  session_pid,
                          const char           *user_name,
                          const char           *host_name,
                          const char           *x11_display_name,
                          const char           *display_device)
{
        UTMP        session_record = { 0 };
181

182 183 184
        if (x11_display_name == NULL)
                x11_display_name = display_device;

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        record_set_username (&session_record, user_name);

        g_debug ("Writing login record");

#if defined(HAVE_UT_UT_TYPE)
        session_record.ut_type = USER_PROCESS;
        g_debug ("using ut_type USER_PROCESS");
#endif

        record_set_timestamp (&session_record);
        record_set_pid (&session_record, session_pid);
        record_set_host (&session_record, x11_display_name, host_name);
        record_set_line (&session_record, display_device, x11_display_name);

        /* Handle wtmp */
        g_debug ("Writing wtmp session record to " GDM_NEW_SESSION_RECORDS_FILE);
201
#if defined(HAVE_UPDWTMPX)
202
        updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record);
203
#elif defined(HAVE_UPDWTMP)
204
        updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record);
205
#elif defined(HAVE_LOGWTMP) && defined(HAVE_UT_UT_HOST)
206
#if defined(HAVE_UT_UT_USER)
207
        logwtmp (session_record.ut_line, session_record.ut_user, session_record.ut_host);
208
#elif defined(HAVE_UT_UT_NAME)
209
        logwtmp (session_record.ut_line, session_record.ut_name, session_record.ut_host);
210 211 212
#endif
#endif

213
        /* Handle utmp */
214
#if defined(HAVE_GETUTXENT)
215
        g_debug ("Adding or updating utmp record for login");
216
        setutxent();
217
        pututxline (&session_record);
218
        endutxent();
219 220
#elif defined(HAVE_LOGIN)
	login (&session_record);
221
#endif
222 223 224 225 226 227 228 229 230 231 232
}

void
gdm_session_record_logout (GPid                  session_pid,
                           const char           *user_name,
                           const char           *host_name,
                           const char           *x11_display_name,
                           const char           *display_device)
{
        UTMP        session_record = { 0 };

233 234 235
        if (x11_display_name == NULL)
                x11_display_name = display_device;

236 237 238 239 240
        g_debug ("Writing logout record");

#if defined(HAVE_UT_UT_TYPE)
        session_record.ut_type = DEAD_PROCESS;
        g_debug ("using ut_type DEAD_PROCESS");
241 242
#endif

243 244 245 246
        record_set_timestamp (&session_record);
        record_set_pid (&session_record, session_pid);
        record_set_host (&session_record, x11_display_name, host_name);
        record_set_line (&session_record, display_device, x11_display_name);
247

248 249
        /* Handle wtmp */
        g_debug ("Writing wtmp logout record to " GDM_NEW_SESSION_RECORDS_FILE);
250
#if defined(HAVE_UPDWTMPX)
251
        updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record);
252
#elif defined (HAVE_UPDWTMP)
253
        updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record);
254
#elif defined(HAVE_LOGWTMP)
255
        logwtmp (session_record.ut_line, "", "");
256 257
#endif

258
        /* Handle utmp */
259
#if defined(HAVE_GETUTXENT)
260
        g_debug ("Adding or updating utmp record for logout");
261
        setutxent();
262
        pututxline (&session_record);
263
        endutxent();
264
#elif defined(HAVE_LOGOUT)
265
        logout (session_record.ut_line);
266
#endif
267
}
268

269 270 271 272 273 274 275 276 277
void
gdm_session_record_failed (GPid                  session_pid,
                           const char           *user_name,
                           const char           *host_name,
                           const char           *x11_display_name,
                           const char           *display_device)
{
        UTMP        session_record = { 0 };

278 279 280
        if (x11_display_name == NULL)
                x11_display_name = display_device;

281 282 283 284 285 286 287 288 289 290 291 292 293 294
        record_set_username (&session_record, user_name);

        g_debug ("Writing failed session attempt record");

#if defined(HAVE_UT_UT_TYPE)
        session_record.ut_type = USER_PROCESS;
        g_debug ("using ut_type USER_PROCESS");
#endif

        record_set_timestamp (&session_record);
        record_set_pid (&session_record, session_pid);
        record_set_host (&session_record, x11_display_name, host_name);
        record_set_line (&session_record, display_device, x11_display_name);

295
#if defined(HAVE_UPDWTMPX) || defined(HAVE_UPDWTMP)
296 297 298
        /* Handle btmp */
        g_debug ("Writing btmp failed session attempt record to "
                 GDM_BAD_SESSION_RECORDS_FILE);
299
#endif
300 301

#if defined(HAVE_UPDWTMPX)
302
        updwtmpx (GDM_BAD_SESSION_RECORDS_FILE, &session_record);
303
#elif defined(HAVE_UPDWTMP)
304
        updwtmp(GDM_BAD_SESSION_RECORDS_FILE, &session_record);
305
#endif
306

307
}