summaryrefslogtreecommitdiffstats
path: root/lib/libpam
diff options
context:
space:
mode:
authored <ed@FreeBSD.org>2009-12-11 14:15:55 +0000
committered <ed@FreeBSD.org>2009-12-11 14:15:55 +0000
commitcaa83cf2554dc1a3ec427400b1adf64486f4afb9 (patch)
tree5952c7825f6457babba4605c312f4371dd484e74 /lib/libpam
parentc5db4e0d33d83745ec06536afa3f6ebef01b81c6 (diff)
downloadFreeBSD-src-caa83cf2554dc1a3ec427400b1adf64486f4afb9.zip
FreeBSD-src-caa83cf2554dc1a3ec427400b1adf64486f4afb9.tar.gz
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
Diffstat (limited to 'lib/libpam')
-rw-r--r--lib/libpam/modules/pam_lastlog/Makefile4
-rw-r--r--lib/libpam/modules/pam_lastlog/pam_lastlog.c102
2 files changed, 22 insertions, 84 deletions
diff --git a/lib/libpam/modules/pam_lastlog/Makefile b/lib/libpam/modules/pam_lastlog/Makefile
index f0b96c4..616c113 100644
--- a/lib/libpam/modules/pam_lastlog/Makefile
+++ b/lib/libpam/modules/pam_lastlog/Makefile
@@ -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>
diff --git a/lib/libpam/modules/pam_lastlog/pam_lastlog.c b/lib/libpam/modules/pam_lastlog/pam_lastlog.c
index 9899f8d..a51432b 100644
--- a/lib/libpam/modules/pam_lastlog/pam_lastlog.c
+++ b/lib/libpam/modules/pam_lastlog/pam_lastlog.c
@@ -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:
OpenPOWER on IntegriCloud