Convert pam_lastlog(8) to libulog.

The information used by the "Last login:"-line is obtained by using
ulog_setutxfile(3) to switch to the lastlog database. Login and logout
are performed using the utility functions ulog_login(3) and
ulog_logout(3).

This also means we must build libulog during bootstrap.

Approved by:	des
This commit is contained in:
Ed Schouten 2009-12-11 14:15:55 +00:00
parent 9cd8bb54fa
commit 17c79ad08c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=200413
4 changed files with 29 additions and 90 deletions

View File

@ -1103,8 +1103,8 @@ _prebuild_libs= ${_kerberos5_lib_libasn1} ${_kerberos5_lib_libkrb5} \
lib/libkiconv lib/libkvm lib/libmd \
lib/ncurses/ncurses lib/ncurses/ncursesw \
lib/libopie lib/libpam ${_lib_libthr} \
lib/libradius lib/libsbuf lib/libtacplus lib/libutil \
${_lib_libypclnt} lib/libz lib/msun \
lib/libradius lib/libsbuf lib/libtacplus lib/libulog \
lib/libutil ${_lib_libypclnt} lib/libz lib/msun \
${_secure_lib_libcrypto} ${_secure_lib_libssh} \
${_secure_lib_libssl} lib/libdwarf lib/libproc

View File

@ -21,6 +21,7 @@
# librpcsvc must be built before libpam.
# libsbuf must be built before libcam.
# libtacplus must be built before libpam.
# libulog must be built before libpam.
# libutil must be built before libpam.
# libypclnt must be built before libpam.
# libgssapi must be built before librpcsec_gss
@ -30,8 +31,8 @@
SUBDIR= ${_csu} libc libbsm libauditd libcom_err libcrypt libelf libkvm msun \
libmd \
ncurses ${_libnetgraph} libradius librpcsvc libsbuf \
libtacplus libutil ${_libypclnt} libalias libarchive ${_libatm} \
libbegemot ${_libbluetooth} ${_libbsnmp} libbz2 \
libtacplus libulog libutil ${_libypclnt} libalias libarchive \
${_libatm} libbegemot ${_libbluetooth} ${_libbsnmp} libbz2 \
libcalendar libcam libcompat libdevinfo libdevstat libdisk \
libdwarf libedit libexpat libfetch libftpio libgeom ${_libgpib} \
${_libgssapi} ${_librpcsec_gss} libipsec \
@ -40,8 +41,8 @@ SUBDIR= ${_csu} libc libbsm libauditd libcom_err libcrypt libelf libkvm msun \
${_libpmc} libproc librt ${_libsdp} ${_libsm} ${_libsmb} \
${_libsmdb} \
${_libsmutil} libstand ${_libtelnet} ${_libthr} libthread_db libufs \
libugidfw libulog ${_libusbhid} ${_libusb} ${_libvgl} libwrap \
liby libz ${_bind}
libugidfw ${_libusbhid} ${_libusb} ${_libvgl} libwrap liby libz \
${_bind}
.if exists(${.CURDIR}/csu/${MACHINE_ARCH}-elf)
_csu=csu/${MACHINE_ARCH}-elf

View File

@ -28,7 +28,7 @@ LIB= pam_lastlog
SRCS= pam_lastlog.c
MAN= pam_lastlog.8
DPADD= ${LIBUTIL}
LDADD= -lutil
DPADD= ${LIBULOG}
LDADD= -lulog
.include <bsd.lib.mk>

View File

@ -46,19 +46,9 @@ __FBSDID("$FreeBSD$");
#define _BSD_SOURCE
#include <sys/param.h>
#include <fcntl.h>
#include <libutil.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <utmp.h>
#include <ulog.h>
#define PAM_SM_SESSION
@ -71,13 +61,11 @@ pam_sm_open_session(pam_handle_t *pamh, int flags,
int argc __unused, const char *argv[] __unused)
{
struct passwd *pwd;
struct utmp utmp;
struct lastlog ll;
struct ulog_utmpx *utx;
time_t t;
const char *user;
const void *rhost, *tty;
off_t llpos;
int fd, pam_err;
int pam_err;
pam_err = pam_get_user(pamh, &user, NULL);
if (pam_err != PAM_SUCCESS)
@ -101,72 +89,29 @@ pam_sm_open_session(pam_handle_t *pamh, int flags,
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) {
PAM_LOG("Failed to open %s", _PATH_LASTLOG);
goto file_err;
}
/*
* 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 (ulog_setutxfile(UTXF_LASTLOG, NULL) != 0) {
PAM_LOG("Failed to open lastlog database");
} else {
utx = ulog_getutxuser(user);
if (utx != NULL && utx->ut_type == USER_PROCESS) {
t = utx->ut_tv.tv_sec;
if (*utx->ut_host != '\0')
pam_info(pamh, "Last login: %.*s from %s",
24 - 5, ctime(&t), utx->ut_host);
else
pam_info(pamh, "Last login: %.*s on %s",
24 - 5, ctime(&t), utx->ut_line);
}
ulog_endutxent();
}
if (lseek(fd, llpos, L_SET) != llpos)
goto file_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);
/*
* 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);
ulog_login(tty, user, rhost);
return (PAM_SUCCESS);
file_err:
syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG);
if (fd != -1)
close(fd);
pam_err = PAM_SYSTEM_ERR;
err:
if (openpam_get_option(pamh, "no_fail"))
return (PAM_SUCCESS);
@ -174,7 +119,7 @@ err:
}
PAM_EXTERN int
pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
int argc __unused, const char *argv[] __unused)
{
const void *tty;
@ -188,14 +133,7 @@ pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
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);
if (logout(tty) != 1)
syslog(LOG_ERR, "%s(): no utmp record for %s",
__func__, (const char *)tty);
logwtmp(tty, "", "");
ulog_logout(tty);
return (PAM_SUCCESS);
err: