From 365092a43a92b57723297f5bdaad3375aa1efdde Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Fri, 19 Oct 2007 02:04:52 +0000 Subject: Try to reduce the complexity of the utmp/wtmp recording code. 2007-10-18 William Jon McCann * daemon/gdm-session-record.c: (record_set_username), (record_set_timestamp), (record_set_pid), (record_set_id), (record_set_host), (record_set_line), (gdm_session_record_login), (gdm_session_record_logout), (gdm_session_record_failed): * daemon/gdm-session-record.h: * daemon/gdm-session.c: (gdm_session_user_verification_error_handler), (gdm_session_started_handler), (gdm_session_startup_error_handler), (gdm_session_exited_handler), (gdm_session_close): Try to reduce the complexity of the utmp/wtmp recording code. svn path=/trunk/; revision=5385 --- daemon/gdm-session-record.c | 387 ++++++++++++++++++++++++++------------------ 1 file changed, 227 insertions(+), 160 deletions(-) (limited to 'daemon/gdm-session-record.c') diff --git a/daemon/gdm-session-record.c b/daemon/gdm-session-record.c index 2c77f0b1..be9787cf 100644 --- a/daemon/gdm-session-record.c +++ b/daemon/gdm-session-record.c @@ -43,105 +43,105 @@ #define GDM_BAD_SESSION_RECORDS_FILE "/var/log/btmp" #endif -#ifndef GDM_NEW_SESSION_RECORDS_FILE -#define GDM_NEW_SESSION_RECORDS_FILE "/var/log/wtmp" +#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 #endif -void -gdm_session_record_write (GdmSessionRecordType record_type, - GPid session_pid, - const char *user_name, - const char *host_name, - const char *x11_display_name, - const char *display_device) +static void +record_set_username (UTMP *u, + const char *user_name) { - UTMP session_record = { 0 }; - UTMP *u; - GTimeVal now = { 0 }; - char *hostname; const char *username; - u = NULL; - - g_debug ("Writing %s utmp/wtmp record", - record_type == GDM_SESSION_RECORD_TYPE_LOGIN ? "session" : - record_type == GDM_SESSION_RECORD_TYPE_LOGOUT ? "logout" : - "failed session attempt"); - - if (record_type != GDM_SESSION_RECORD_TYPE_LOGOUT) { - /* - * It is possible that PAM failed before it mapped the user - * input into a valid username, so we fallback to try using - * "(unknown)" - */ - if (user_name != NULL) { - username = user_name; - } else { - username = "(unknown)"; - } - -#if defined(HAVE_UT_UT_USER) - strncpy (session_record.ut_user, - username, - sizeof (session_record.ut_user)); - g_debug ("using ut_user %.*s", - sizeof (session_record.ut_user), - session_record.ut_user); -#elif defined(HAVE_UT_UT_NAME) - strncpy (session_record.ut_name, - username - sizeof (session_record.ut_name)); - g_debug ("using ut_name %.*s", - sizeof (session_record.ut_name), - session_record.ut_name); -#endif - } - -#if defined(HAVE_UT_UT_TYPE) /* - * Set type to DEAD_PROCESS when logging out, otherwise - * set to USER_PROCESS. + * It is possible that PAM failed before it mapped the user + * input into a valid username, so we fallback to try using + * "(unknown)" */ - if (record_type == GDM_SESSION_RECORD_TYPE_LOGOUT) { - session_record.ut_type = DEAD_PROCESS; - g_debug ("using ut_type DEAD_PROCESS"); + if (user_name != NULL) { + username = user_name; } else { - session_record.ut_type = USER_PROCESS; - g_debug ("using ut_type USER_PROCESS"); + username = "(unknown)"; } -#endif -#if defined(HAVE_UT_UT_PID) - /* Set pid */ - if (session_pid != 0) { - session_record.ut_pid = session_pid; - } - g_debug ("using ut_pid %d", (int) session_record.ut_pid); +#if defined(HAVE_UT_UT_USER) + strncpy (u->ut_user, + username, + sizeof (u->ut_user)); + g_debug ("using ut_user %.*s", + sizeof (u->ut_user), + u->ut_user); +#elif defined(HAVE_UT_UT_NAME) + strncpy (u->ut_name, + username + sizeof (u->ut_name)); + g_debug ("using ut_name %.*s", + sizeof (u->ut_name), + u->ut_name); #endif +} +static void +record_set_timestamp (UTMP *u) +{ #if defined(HAVE_UT_UT_TV) + GTimeVal now = { 0 }; + /* Set time in TV format */ g_get_current_time (&now); - session_record.ut_tv.tv_sec = now.tv_sec; - session_record.ut_tv.tv_usec = now.tv_usec; + u->ut_tv.tv_sec = now.tv_sec; + u->ut_tv.tv_usec = now.tv_usec; g_debug ("using ut_tv time %ld", - (glong) session_record.ut_tv.tv_sec); + (glong) u->ut_tv.tv_sec); #elif defined(HAVE_UT_UT_TIME) /* Set time in time format */ - time (&session_record.ut_time); + time (&u->ut_time); g_debug ("using ut_time %ld", - (glong) session_record.ut_time); + (glong) u->ut_time); #endif +} +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_id (UTMP *u, + const char *id) +{ #if defined(HAVE_UT_UT_ID) - /* Set ut_id to the $DISPLAY value */ - strncpy (session_record.ut_id, - x11_display_name, - sizeof (session_record.ut_id)); - g_debug ("using ut_id %.*s", - sizeof (session_record.ut_id), - session_record.ut_id); + strncpy (u->ut_id, id, sizeof (u->ut_id)); + g_debug ("using ut_id %.*s", sizeof (u->ut_id), u->ut_id); #endif +} + +static void +record_set_host (UTMP *u, + const char *x11_display_name, + const char *host_name) +{ + char *hostname; #if defined(HAVE_UT_UT_HOST) hostname = NULL; @@ -151,148 +151,215 @@ gdm_session_record_write (GdmSessionRecordType record_type, * to $DISPLAY */ if ((host_name != NULL) && g_str_has_prefix (x11_display_name, ":")) { - hostname = g_strdup_printf ("%s%s", - host_name, - x11_display_name); + hostname = g_strdup_printf ("%s%s", host_name, x11_display_name); } else { hostname = g_strdup (x11_display_name); } if (hostname != NULL) { - strncpy (session_record.ut_host, - hostname, sizeof (session_record.ut_host)); - g_debug ("using ut_host %.*s", - sizeof (session_record.ut_host), - session_record.ut_host); + strncpy (u->ut_host, hostname, sizeof (u->ut_host)); + g_debug ("using ut_host %.*s", sizeof (u->ut_host), u->ut_host); g_free (hostname); #ifdef HAVE_UT_UT_SYSLEN - session_record.ut_syslen = MIN (strlen (hostname), - sizeof (session_record.ut_host)); + u->ut_syslen = MIN (strlen (hostname), sizeof (u->ut_host)); #endif } #endif +} +static void +record_set_line (UTMP *u, + const char *display_device, + const char *x11_display_name) +{ /* * Set ut_line to the device name associated with this display * but remove the "/dev/" prefix. If no device, then use the * $DISPLAY value. */ if (g_str_has_prefix (display_device, "/dev/")) { - strncpy (session_record.ut_line, + strncpy (u->ut_line, display_device + strlen ("/dev/"), - sizeof (session_record.ut_line)); + sizeof (u->ut_line)); } else if (g_str_has_prefix (x11_display_name, ":")) { - strncpy (session_record.ut_line, + strncpy (u->ut_line, x11_display_name, - sizeof (session_record.ut_line)); + sizeof (u->ut_line)); } - g_debug ("using ut_line %.*s", - sizeof (session_record.ut_line), - session_record.ut_line); - switch (record_type) { - case GDM_SESSION_RECORD_TYPE_LOGIN: + g_debug ("using ut_line %.*s", sizeof (u->ut_line), u->ut_line); +} + +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 }; + UTMP *u; - /* Handle wtmp */ - g_debug ("Writing wtmp session record to " GDM_NEW_SESSION_RECORDS_FILE); + 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); + + /* Set ut_id to the $DISPLAY value */ + record_set_id (&session_record, x11_display_name); + 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); #if defined(HAVE_UPDWTMPX) - updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record); + updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record); #elif defined(HAVE_UPDWTMP) - updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record); + updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record); #elif defined(HAVE_LOGWTMP) && defined(HAVE_UT_UT_HOST) && !defined(HAVE_LOGIN) #if defined(HAVE_UT_UT_USER) - logwtmp (record.ut_line, record.ut_user, record.ut_host); + logwtmp (session_record.ut_line, session_record.ut_user, session_record.ut_host); #elif defined(HAVE_UT_UT_NAME) - logwtmp (record.ut_line, record.ut_name, record.ut_host); + logwtmp (session_record.ut_line, session_record.ut_name, session_record.ut_host); #endif #endif -#if defined(HAVE_GETUTXENT) - /* - * Handle utmp - * Update if entry already exists - */ - while ((u = getutxent ()) != NULL) { - if (u->ut_type == USER_PROCESS && - (session_record.ut_line != NULL && - (strncmp (u->ut_line, session_record.ut_line, - sizeof (u->ut_line)) == 0 || - u->ut_pid == session_record.ut_pid))) { - g_debug ("Updating existing utmp record"); - pututxline (&session_record); - break; - } - } - endutxent (); - - /* Add new entry if update did not work */ - if (u == NULL) { - g_debug ("Adding new utmp record"); + /* + * Handle utmp + * Update if entry already exists + */ + setutxent (); + + while ((u = getutxent ()) != NULL) { + if (u->ut_type == USER_PROCESS && + (session_record.ut_line != NULL && + (strncmp (u->ut_line, session_record.ut_line, + sizeof (u->ut_line)) == 0 || + u->ut_pid == session_record.ut_pid))) { + g_debug ("Updating existing utmp record"); pututxline (&session_record); + break; } -#elif defined(HAVE_LOGIN) - login (&session_record); + } + endutxent (); + + /* Add new entry if update did not work */ + if (u == NULL) { + g_debug ("Adding new utmp record"); + pututxline (&session_record); + } +} + +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 }; + UTMP *u; + + g_debug ("Writing logout record"); + +#if defined(HAVE_UT_UT_TYPE) + session_record.ut_type = DEAD_PROCESS; + g_debug ("using ut_type DEAD_PROCESS"); #endif - break; + record_set_timestamp (&session_record); + record_set_pid (&session_record, session_pid); + /* Set ut_id to the $DISPLAY value */ + record_set_id (&session_record, x11_display_name); + record_set_host (&session_record, x11_display_name, host_name); + record_set_line (&session_record, display_device, x11_display_name); - case GDM_SESSION_RECORD_TYPE_LOGOUT: - /* Handle wtmp */ - g_debug ("Writing wtmp logout record to " GDM_NEW_SESSION_RECORDS_FILE); + /* Handle wtmp */ + g_debug ("Writing wtmp logout record to " GDM_NEW_SESSION_RECORDS_FILE); #if defined(HAVE_UPDWTMPX) - updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record); + updwtmpx (GDM_NEW_SESSION_RECORDS_FILE, &session_record); #elif defined (HAVE_UPDWTMP) - updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record); + updwtmp (GDM_NEW_SESSION_RECORDS_FILE, &session_record); #elif defined(HAVE_LOGWTMP) - logwtmp (record.ut_line, "", ""); + logwtmp (record.ut_line, "", ""); #endif - /* Hande utmp */ + /* Handle utmp */ #if defined(HAVE_GETUTXENT) - setutxent (); + setutxent (); - while ((u = getutxent ()) != NULL && - (u = getutxid (&session_record)) != NULL) { + while ((u = getutxent ()) != NULL && + (u = getutxid (&session_record)) != NULL) { - g_debug ("Removing utmp record"); - if (u->ut_pid == session_pid && - u->ut_type == DEAD_PROCESS) { - /* Already done */ - break; - } + g_debug ("Removing utmp record"); + if (u->ut_pid == session_pid && + u->ut_type == DEAD_PROCESS) { + /* Already done */ + break; + } - u->ut_type = DEAD_PROCESS; + u->ut_type = DEAD_PROCESS; #if defined(HAVE_UT_UT_TV) - u->ut_tv.tv_sec = session_record.ut_tv.tv_sec; + u->ut_tv.tv_sec = session_record.ut_tv.tv_sec; #elif defined(HAVE_UT_UT_TIME) - u->ut_time = session_record.ut_time; + u->ut_time = session_record.ut_time; #endif - u->ut_exit.e_termination = 0; - u->ut_exit.e_exit = 0; + u->ut_exit.e_termination = 0; + u->ut_exit.e_exit = 0; - pututxline (u); + pututxline (u); - break; - } + break; + } - endutxent (); + endutxent (); #elif defined(HAVE_LOGOUT) - logout (session_record.ut_line); + logout (session_record.ut_line); #endif +} - break; +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 }; + + 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); + /* Set ut_id to the $DISPLAY value */ + record_set_id (&session_record, x11_display_name); + record_set_host (&session_record, x11_display_name, host_name); + record_set_line (&session_record, display_device, x11_display_name); + + /* Handle btmp */ + g_debug ("Writing btmp failed session attempt record to " + GDM_BAD_SESSION_RECORDS_FILE); - case GDM_SESSION_RECORD_TYPE_FAILED_ATTEMPT: - /* Handle btmp */ - g_debug ("Writing btmp failed session attempt record to " - GDM_BAD_SESSION_RECORDS_FILE); #if defined(HAVE_UPDWTMPX) - updwtmpx (GDM_BAD_SESSION_RECORDS_FILE, &session_record); + updwtmpx (GDM_BAD_SESSION_RECORDS_FILE, &session_record); #elif defined(HAVE_UPDWTMP) - updwtmp (GDM_BAD_SESSION_RECORDS_FILE, &session_record); + updwtmp(GDM_BAD_SESSION_RECORDS_FILE, &session_record); #endif - break; - } + } -- cgit v1.2.1