Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpam/modules/pam_lastlog Re-write to use both utmp and...



details:   https://anonhg.NetBSD.org/src/rev/57c36a336549
branches:  trunk
changeset: 573581:57c36a336549
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Feb 01 17:54:48 2005 +0000

description:
Re-write to use both utmp and utmpx properly.

diffstat:

 lib/libpam/modules/pam_lastlog/Makefile      |    4 +-
 lib/libpam/modules/pam_lastlog/pam_lastlog.8 |   13 +-
 lib/libpam/modules/pam_lastlog/pam_lastlog.c |  279 ++++++++++++++++++--------
 3 files changed, 204 insertions(+), 92 deletions(-)

diffs (truncated from 401 to 300 lines):

diff -r 29dc06108169 -r 57c36a336549 lib/libpam/modules/pam_lastlog/Makefile
--- a/lib/libpam/modules/pam_lastlog/Makefile   Tue Feb 01 17:53:07 2005 +0000
+++ b/lib/libpam/modules/pam_lastlog/Makefile   Tue Feb 01 17:54:48 2005 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.4 2004/12/29 15:04:06 lukem Exp $
+# $NetBSD: Makefile,v 1.5 2005/02/01 17:54:48 christos Exp $
 # Copyright 2001 Mark R V Murray
 # All rights reserved.
 #
@@ -29,6 +29,8 @@
 SRCS=  pam_lastlog.c
 MAN=   pam_lastlog.8
 
+CPPFLAGS+=-DSUPPORT_UTMP -DSUPPORT_UTMPX
+
 LIBDPLIBS=     util    ${LIB_ROOT_DIR}/libutil
 
 .include "${.CURDIR}/../mod.mk"
diff -r 29dc06108169 -r 57c36a336549 lib/libpam/modules/pam_lastlog/pam_lastlog.8
--- a/lib/libpam/modules/pam_lastlog/pam_lastlog.8      Tue Feb 01 17:53:07 2005 +0000
+++ b/lib/libpam/modules/pam_lastlog/pam_lastlog.8      Tue Feb 01 17:54:48 2005 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: pam_lastlog.8,v 1.2 2004/12/12 08:18:46 christos Exp $
+.\" $NetBSD: pam_lastlog.8,v 1.3 2005/02/01 17:54:48 christos Exp $
 .\" Copyright (c) 2001 Mark R V Murray
 .\" All rights reserved.
 .\" Copyright (c) 2001 Networks Associates Technology, Inc.
@@ -35,7 +35,7 @@
 .\"
 .\" $FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.8,v 1.6 2004/07/02 23:52:17 ru Exp $
 .\"
-.Dd January 24, 2002
+.Dd February 1, 2005
 .Dt PAM_LASTLOG 8
 .Os
 .Sh NAME
@@ -67,10 +67,10 @@
 The
 .Fn pam_sm_open_session
 function records the session in the
-.Xr utmp 5 ,
-.Xr wtmp 5
+.Xr utmp 5 , utmpx 5
+.Xr wtmp 5 , wtmpx 5
 and
-.Xr lastlog 5
+.Xr lastlog 5 , lastlogx 5
 databases.
 The
 .Fn pam_sm_close_session
@@ -94,8 +94,11 @@
 .Xr w 1 ,
 .Xr login 3 ,
 .Xr logout 3 ,
+.Xr loginx 3 ,
+.Xr logoutx 3 ,
 .Xr pam.conf 5 ,
 .Xr utmp 5 ,
+.Xr utmpx 5 ,
 .Xr pam 8
 .Sh AUTHORS
 The
diff -r 29dc06108169 -r 57c36a336549 lib/libpam/modules/pam_lastlog/pam_lastlog.c
--- a/lib/libpam/modules/pam_lastlog/pam_lastlog.c      Tue Feb 01 17:53:07 2005 +0000
+++ b/lib/libpam/modules/pam_lastlog/pam_lastlog.c      Tue Feb 01 17:54:48 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pam_lastlog.c,v 1.2 2004/12/12 08:18:46 christos Exp $ */
+/*     $NetBSD: pam_lastlog.c,v 1.3 2005/02/01 17:54:48 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
@@ -47,11 +47,9 @@
 #ifdef __FreeBSD__
 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.20 2004/01/26 19:28:37 des Exp $");
 #else
-__RCSID("$NetBSD: pam_lastlog.c,v 1.2 2004/12/12 08:18:46 christos Exp $");
+__RCSID("$NetBSD: pam_lastlog.c,v 1.3 2005/02/01 17:54:48 christos Exp $");
 #endif
 
-#define _BSD_SOURCE
-
 #include <sys/param.h>
 
 #include <fcntl.h>
@@ -64,7 +62,22 @@
 #include <syslog.h>
 #include <time.h>
 #include <unistd.h>
+
+#ifdef SUPPORT_UTMP
 #include <utmp.h>
+static void doutmp(const char *, const char *, const char *,
+    const struct timeval *);
+static void dolastlog(int, const struct passwd *, const char *, const char *,
+    const struct timeval *);
+#endif
+
+#ifdef SUPPORT_UTMPX
+#include <utmpx.h>
+static void doutmpx(const char *, const char *, const char *,
+    const struct sockaddr_storage *ss, const struct timeval *);
+static void dolastlogx(int, const struct passwd *, const char *, const char *,
+    const struct sockaddr_storage *ss, const struct timeval *);
+#endif
 
 #define PAM_SM_SESSION
 
@@ -77,117 +90,211 @@
     int argc __unused, const char *argv[] __unused)
 {
        struct passwd *pwd;
-       struct utmp utmp;
-       struct lastlog ll;
-       time_t t;
-       const char *user;
-       const void *rhost, *tty;
-       off_t llpos;
-       int fd, pam_err;
+       struct timeval now;
+       const char *user, *rhost, *tty;
+       const struct sockaddr_storage *ss;
+       int pam_err;
 
        pam_err = pam_get_user(pamh, &user, NULL);
        if (pam_err != PAM_SUCCESS)
-               return (pam_err);
+               return pam_err;
+
        if (user == NULL || (pwd = getpwnam(user)) == NULL)
-               return (PAM_SERVICE_ERR);
+               return PAM_SERVICE_ERR;
+
        PAM_LOG("Got user: %s", user);
 
-       pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
+       pam_err = pam_get_item(pamh, PAM_RHOST,
+           (const void **)(void *)&rhost);
        if (pam_err != PAM_SUCCESS)
                goto err;
-       pam_err = pam_get_item(pamh, PAM_TTY, &tty);
+
+       pam_err = pam_get_item(pamh, PAM_SOCKADDR,
+           (const void **)(void *)&ss);
        if (pam_err != PAM_SUCCESS)
                goto err;
+
+       pam_err = pam_get_item(pamh, PAM_TTY,
+           (const void **)(void *)&tty);
+       if (pam_err != PAM_SUCCESS)
+               goto err;
+
        if (tty == NULL) {
                pam_err = PAM_SERVICE_ERR;
                goto err;
        }
+
        if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
-               tty = (const char *)tty + strlen(_PATH_DEV);
-       if (*(const char *)tty == '\0')
-               return (PAM_SERVICE_ERR);
-
-       fd = open(_PATH_LASTLOG, O_RDWR|O_CREAT, 0644);
-       if (fd == -1)
-               goto file_err;
+               tty = tty + strlen(_PATH_DEV);
 
-       /*
-        * Record session in lastlog(5).
-        */
-       llpos = (off_t)(pwd->pw_uid * sizeof(ll));
-       if (lseek(fd, llpos, L_SET) != llpos)
-               goto file_err;
-       if ((flags & PAM_SILENT) == 0) {
-               if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
-                       t = ll.ll_time;
-                       if (*ll.ll_host != '\0')
-                               pam_info(pamh, "Last login: %.*s from %.*s",
-                                   24 - 5, ctime(&t),
-                                   (int)sizeof(ll.ll_host), ll.ll_host);
-                       else
-                               pam_info(pamh, "Last login: %.*s on %.*s",
-                                   24 - 5, ctime(&t),
-                                   (int)sizeof(ll.ll_line), ll.ll_line);
-               }
-               if (lseek(fd, llpos, L_SET) != llpos)
-                       goto file_err;
+       if (*tty == '\0') {
+               pam_err = PAM_SERVICE_ERR;
+               goto err;
        }
 
-       bzero(&ll, sizeof(ll));
-       ll.ll_time = time(NULL);
-
-       /* note: does not need to be NUL-terminated */
-       strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
-       if (rhost != NULL && *(const char *)rhost != '\0')
-               /* note: does not need to be NUL-terminated */
-               strncpy(ll.ll_host, rhost, sizeof(ll.ll_host));
-
-       if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0)
-               goto file_err;
-
-       PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
+       (void)gettimeofday(&now, NULL);
 
-       /*
-        * Record session in utmp(5) and wtmp(5).
-        */
-       bzero(&utmp, sizeof(utmp));
-       utmp.ut_time = time(NULL);
-       /* note: does not need to be NUL-terminated */
-       strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
-       if (rhost != NULL && *(const char *)rhost != '\0')
-               strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host));
-       (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
-       login(&utmp);
-
-       return (PAM_SUCCESS);
-
-file_err:
-       syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG);
-       if (fd != -1)
-               close(fd);
-       pam_err = PAM_SYSTEM_ERR;
+#ifdef SUPPORT_UTMPX
+       doutmpx(user, rhost, tty, ss, &now);
+       dolastlogx(1, pwd, rhost, tty, ss, &now);
+#endif
+#ifdef SUPPORT_UTMP
+       doutmp(user, rhost, tty, &now);
+       dolastlog(1, pwd, rhost, tty, &now);
+#endif
 err:
        if (openpam_get_option(pamh, "no_fail"))
-               return (PAM_SUCCESS);
-       return (pam_err);
+               return PAM_SUCCESS;
+       return pam_err;
 }
 
 PAM_EXTERN int
 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
     int argc __unused, const char *argv[] __unused)
 {
-        const void *tty;
+        const char *tty;
 
-        pam_get_item(pamh, PAM_TTY, (const void **)&tty);
+        pam_get_item(pamh, PAM_TTY, (const void **)(void *)&tty);
+       if (tty == NULL)
+               return PAM_SERVICE_ERR;
+
        if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
-               tty = (const char *)tty + strlen(_PATH_DEV);
-       if (*(const char *)tty == '\0')
-               return (PAM_SERVICE_ERR);
-        if (logout(tty) != 1)
-                syslog(LOG_ERR, "%s(): no utmp record for %s",
-                   __func__, (const char *)tty);
-        logwtmp(tty, "", "");
-        return (PAM_SUCCESS);
+               tty = tty + strlen(_PATH_DEV);
+
+       if (*tty == '\0')
+               return PAM_SERVICE_ERR;
+
+#ifdef SUPPORT_UTMPX
+       if (logoutx(tty, 0, DEAD_PROCESS))
+               logwtmpx(tty, "", "", 0, DEAD_PROCESS);
+       else
+                syslog(LOG_NOTICE, "%s(): no utmpx record for %s",
+                   __func__, tty);
+#endif
+
+#ifdef SUPPORT_UTMP
+       if (logout(tty))
+               logwtmp(tty, "", "");
+       else
+                syslog(LOG_NOTICE, "%s(): no utmp record for %s",
+                   __func__, tty);
+#endif
+        return PAM_SUCCESS;
+}
+
+#ifdef SUPPORT_UTMPX
+static void
+doutmpx(const char *username, const char *hostname, const char *tty,
+    const struct sockaddr_storage *ss, const struct timeval *now)
+{
+       struct utmpx utmpx;
+       const char *t;
+
+       memset((void *)&utmpx, 0, sizeof(utmpx));
+       utmpx.ut_tv = *now;
+       (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
+       if (hostname) {
+               (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
+               if (ss)
+                       utmpx.ut_ss = *ss;



Home | Main Index | Thread Index | Old Index