xref: /freebsd/lib/libpam/modules/pam_lastlog/pam_lastlog.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
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
5  * All rights reserved.
6  * Copyright (c) 2001 Networks Associates Technology, Inc.
7  * All rights reserved.
8  *
9  * Portions of this software were developed for the FreeBSD Project by
10  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12  * ("CBOSS"), as part of the DARPA CHATS research program.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. The name of the author may not be used to endorse or promote
23  *    products derived from this software without specific prior written
24  *    permission.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #define _BSD_SOURCE
46 
47 #include <sys/param.h>
48 
49 #include <fcntl.h>
50 #include <libutil.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <syslog.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <utmp.h>
60 
61 #define PAM_SM_SESSION
62 
63 #include <security/pam_appl.h>
64 #include <security/pam_modules.h>
65 #include <security/pam_mod_misc.h>
66 
67 PAM_EXTERN int
68 pam_sm_open_session(pam_handle_t *pamh, int flags,
69     int argc __unused, const char *argv[] __unused)
70 {
71 	struct passwd *pwd;
72 	struct utmp utmp;
73 	struct lastlog ll;
74 	time_t t;
75 	const char *rhost, *user, *tty;
76 	off_t llpos;
77 	int fd, pam_err;
78 
79 	pam_err = pam_get_user(pamh, &user, NULL);
80 	if (pam_err != PAM_SUCCESS)
81 		return (pam_err);
82 	if (user == NULL || (pwd = getpwnam(user)) == NULL)
83 		return (PAM_SERVICE_ERR);
84 	PAM_LOG("Got user: %s", user);
85 
86 	pam_err = pam_get_item(pamh, PAM_RHOST, (const void **)&rhost);
87 	if (pam_err != PAM_SUCCESS)
88 		goto err;
89 	pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty);
90 	if (pam_err != PAM_SUCCESS)
91 		goto err;
92 	if (tty == NULL) {
93 		pam_err = PAM_SERVICE_ERR;
94 		goto err;
95 	}
96 	if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
97 		tty += strlen(_PATH_DEV);
98 	if (*tty == '\0')
99 		return (PAM_SERVICE_ERR);
100 
101 	fd = open(_PATH_LASTLOG, O_RDWR|O_CREAT, 0644);
102 	if (fd == -1)
103 		goto file_err;
104 
105 	/*
106 	 * Record session in lastlog(5).
107 	 */
108 	llpos = (off_t)(pwd->pw_uid * sizeof(ll));
109 	if (lseek(fd, llpos, L_SET) != llpos)
110 		goto file_err;
111 	if ((flags & PAM_SILENT) == 0) {
112 		if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) {
113 			t = ll.ll_time;
114 			if (*ll.ll_host != '\0')
115 				pam_info(pamh, "Last login: %.*s from %.*s",
116 				    24 - 5, ctime(&t),
117 				    (int)sizeof(ll.ll_host), ll.ll_host);
118 			else
119 				pam_info(pamh, "Last login: %.*s on %.*s",
120 				    24 - 5, ctime(&t),
121 				    (int)sizeof(ll.ll_line), ll.ll_line);
122 		}
123 		if (lseek(fd, llpos, L_SET) != llpos)
124 			goto file_err;
125 	}
126 
127 	bzero(&ll, sizeof(ll));
128 	ll.ll_time = time(NULL);
129 
130 	/* note: does not need to be NUL-terminated */
131 	strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
132 	if (rhost != NULL && *rhost != '\0')
133 		/* note: does not need to be NUL-terminated */
134 		strncpy(ll.ll_host, rhost, sizeof(ll.ll_host));
135 
136 	if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0)
137 		goto file_err;
138 
139 	PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
140 
141 	/*
142 	 * Record session in utmp(5) and wtmp(5).
143 	 */
144 	bzero(&utmp, sizeof(utmp));
145 	utmp.ut_time = time(NULL);
146 	/* note: does not need to be NUL-terminated */
147 	strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
148 	if (rhost != NULL && *rhost != '\0')
149 		strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host));
150 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
151 	login(&utmp);
152 
153 	return (PAM_SUCCESS);
154 
155 file_err:
156 	syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG);
157 	if (fd != -1)
158 		close(fd);
159 	pam_err = PAM_SYSTEM_ERR;
160 err:
161 	if (openpam_get_option(pamh, "no_fail"))
162 		return (PAM_SUCCESS);
163 	return (pam_err);
164 }
165 
166 PAM_EXTERN int
167 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
168     int argc __unused, const char *argv[] __unused)
169 {
170 
171 	return (PAM_SUCCESS);
172 }
173 
174 PAM_MODULE_ENTRY("pam_lastlog");
175