2 * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2001 Mark R V Murray
6 * Copyright (c) 2001 Networks Associates Technology, Inc.
8 * Copyright (c) 2004 Joe R. Doupnik
11 * Portions of this software were developed for the FreeBSD Project by
12 * ThinkSec AS and NAI Labs, the Security Research Division of Network
13 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
14 * ("CBOSS"), as part of the DARPA CHATS research program.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. The name of the author may not be used to endorse or promote
25 * products derived from this software without specific prior written
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
63 #define PAM_SM_SESSION
65 #include <security/pam_appl.h>
66 #include <security/pam_modules.h>
67 #include <security/pam_mod_misc.h>
70 pam_sm_open_session(pam_handle_t *pamh, int flags,
71 int argc __unused, const char *argv[] __unused)
78 const void *rhost, *tty;
82 pam_err = pam_get_user(pamh, &user, NULL);
83 if (pam_err != PAM_SUCCESS)
85 if (user == NULL || (pwd = getpwnam(user)) == NULL)
86 return (PAM_SERVICE_ERR);
87 PAM_LOG("Got user: %s", user);
89 pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
90 if (pam_err != PAM_SUCCESS) {
91 PAM_LOG("No PAM_RHOST");
94 pam_err = pam_get_item(pamh, PAM_TTY, &tty);
95 if (pam_err != PAM_SUCCESS) {
96 PAM_LOG("No PAM_TTY");
100 PAM_LOG("No PAM_TTY");
101 pam_err = PAM_SERVICE_ERR;
104 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
105 tty = (const char *)tty + strlen(_PATH_DEV);
106 if (*(const char *)tty == '\0')
107 return (PAM_SERVICE_ERR);
109 fd = open(_PATH_LASTLOG, O_RDWR|O_CREAT, 0644);
111 PAM_LOG("Failed to open %s", _PATH_LASTLOG);
116 * Record session in lastlog(5).
118 llpos = (off_t)(pwd->pw_uid * sizeof(ll));
119 if (lseek(fd, llpos, L_SET) != llpos)
121 if ((flags & PAM_SILENT) == 0) {
122 if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
124 if (*ll.ll_host != '\0')
125 pam_info(pamh, "Last login: %.*s from %.*s",
127 (int)sizeof(ll.ll_host), ll.ll_host);
129 pam_info(pamh, "Last login: %.*s on %.*s",
131 (int)sizeof(ll.ll_line), ll.ll_line);
133 if (lseek(fd, llpos, L_SET) != llpos)
137 bzero(&ll, sizeof(ll));
138 ll.ll_time = time(NULL);
140 /* note: does not need to be NUL-terminated */
141 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
142 if (rhost != NULL && *(const char *)rhost != '\0')
143 /* note: does not need to be NUL-terminated */
144 strncpy(ll.ll_host, rhost, sizeof(ll.ll_host));
146 if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0)
149 PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
152 * Record session in utmp(5) and wtmp(5).
154 bzero(&utmp, sizeof(utmp));
155 utmp.ut_time = time(NULL);
156 /* note: does not need to be NUL-terminated */
157 strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
158 if (rhost != NULL && *(const char *)rhost != '\0')
159 strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host));
160 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
163 return (PAM_SUCCESS);
166 syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG);
169 pam_err = PAM_SYSTEM_ERR;
171 if (openpam_get_option(pamh, "no_fail"))
172 return (PAM_SUCCESS);
177 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
178 int argc __unused, const char *argv[] __unused)
183 pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty);
184 if (pam_err != PAM_SUCCESS)
186 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
187 tty = (const char *)tty + strlen(_PATH_DEV);
188 if (*(const char *)tty == '\0')
189 return (PAM_SERVICE_ERR);
190 if (logout(tty) != 1)
191 syslog(LOG_ERR, "%s(): no utmp record for %s",
192 __func__, (const char *)tty);
193 logwtmp(tty, "", "");
194 return (PAM_SUCCESS);
197 if (openpam_get_option(pamh, "no_fail"))
198 return (PAM_SUCCESS);
202 PAM_MODULE_ENTRY("pam_lastlog");