xref: /netbsd-src/lib/libpam/modules/pam_lastlog/pam_lastlog.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: pam_lastlog.c,v 1.4 2005/02/04 15:11:35 he Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2001 Mark R V Murray
7  * All rights reserved.
8  * Copyright (c) 2001 Networks Associates Technology, Inc.
9  * All rights reserved.
10  * Copyright (c) 2004 Joe R. Doupnik
11  * All rights reserved.
12  *
13  * Portions of this software were developed for the FreeBSD Project by
14  * ThinkSec AS and NAI Labs, the Security Research Division of Network
15  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
16  * ("CBOSS"), as part of the DARPA CHATS research program.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. The name of the author may not be used to endorse or promote
27  *    products derived from this software without specific prior written
28  *    permission.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  */
45 
46 #include <sys/cdefs.h>
47 #ifdef __FreeBSD__
48 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.20 2004/01/26 19:28:37 des Exp $");
49 #else
50 __RCSID("$NetBSD: pam_lastlog.c,v 1.4 2005/02/04 15:11:35 he Exp $");
51 #endif
52 
53 #include <sys/param.h>
54 
55 #include <fcntl.h>
56 #include <util.h>
57 #include <paths.h>
58 #include <pwd.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include <time.h>
64 #include <unistd.h>
65 
66 #ifdef SUPPORT_UTMP
67 #include <utmp.h>
68 static void doutmp(const char *, const char *, const char *,
69     const struct timeval *);
70 static void dolastlog(int, const struct passwd *, const char *, const char *,
71     const struct timeval *);
72 #endif
73 
74 #ifdef SUPPORT_UTMPX
75 #include <utmpx.h>
76 static void doutmpx(const char *, const char *, const char *,
77     const struct sockaddr_storage *ss, const struct timeval *);
78 static void dolastlogx(int, const struct passwd *, const char *, const char *,
79     const struct sockaddr_storage *ss, const struct timeval *);
80 #endif
81 
82 #define PAM_SM_SESSION
83 
84 #include <security/pam_appl.h>
85 #include <security/pam_modules.h>
86 #include <security/pam_mod_misc.h>
87 
88 PAM_EXTERN int
89 pam_sm_open_session(pam_handle_t *pamh, int flags,
90     int argc __unused, const char *argv[] __unused)
91 {
92 	struct passwd *pwd;
93 	struct timeval now;
94 	const char *user, *rhost, *tty;
95 	const void *vrhost, *vtty, *vss;
96 	const struct sockaddr_storage *ss;
97 	int pam_err;
98 
99 	pam_err = pam_get_user(pamh, &user, NULL);
100 	if (pam_err != PAM_SUCCESS)
101 		return pam_err;
102 
103 	if (user == NULL || (pwd = getpwnam(user)) == NULL)
104 		return PAM_SERVICE_ERR;
105 
106 	PAM_LOG("Got user: %s", user);
107 
108 	pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
109 	if (pam_err != PAM_SUCCESS)
110 		goto err;
111 	rhost = (const char *)vrhost;
112 
113 	pam_err = pam_get_item(pamh, PAM_SOCKADDR, &vss);
114 	if (pam_err != PAM_SUCCESS)
115 		goto err;
116 	ss = (const struct sockaddr_storage *)vss;
117 
118 	pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
119 	if (pam_err != PAM_SUCCESS)
120 		goto err;
121 	tty = (const char *)vtty;
122 
123 	if (tty == NULL) {
124 		pam_err = PAM_SERVICE_ERR;
125 		goto err;
126 	}
127 
128 	if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
129 		tty = tty + strlen(_PATH_DEV);
130 
131 	if (*tty == '\0') {
132 		pam_err = PAM_SERVICE_ERR;
133 		goto err;
134 	}
135 
136 	(void)gettimeofday(&now, NULL);
137 
138 #ifdef SUPPORT_UTMPX
139 	doutmpx(user, rhost, tty, ss, &now);
140 	dolastlogx(1, pwd, rhost, tty, ss, &now);
141 #endif
142 #ifdef SUPPORT_UTMP
143 	doutmp(user, rhost, tty, &now);
144 	dolastlog(1, pwd, rhost, tty, &now);
145 #endif
146 err:
147 	if (openpam_get_option(pamh, "no_fail"))
148 		return PAM_SUCCESS;
149 	return pam_err;
150 }
151 
152 PAM_EXTERN int
153 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
154     int argc __unused, const char *argv[] __unused)
155 {
156 	const void *vtty;
157 	const char *tty;
158 
159 	pam_get_item(pamh, PAM_TTY, &vtty);
160 	if (vtty == NULL)
161 		return PAM_SERVICE_ERR;
162 	tty = (const char *)vtty;
163 
164 	if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
165 		tty = tty + strlen(_PATH_DEV);
166 
167 	if (*tty == '\0')
168 		return PAM_SERVICE_ERR;
169 
170 #ifdef SUPPORT_UTMPX
171 	if (logoutx(tty, 0, DEAD_PROCESS))
172 		logwtmpx(tty, "", "", 0, DEAD_PROCESS);
173 	else
174                 syslog(LOG_NOTICE, "%s(): no utmpx record for %s",
175 		    __func__, tty);
176 #endif
177 
178 #ifdef SUPPORT_UTMP
179 	if (logout(tty))
180 		logwtmp(tty, "", "");
181 	else
182                 syslog(LOG_NOTICE, "%s(): no utmp record for %s",
183 		    __func__, tty);
184 #endif
185         return PAM_SUCCESS;
186 }
187 
188 #ifdef SUPPORT_UTMPX
189 static void
190 doutmpx(const char *username, const char *hostname, const char *tty,
191     const struct sockaddr_storage *ss, const struct timeval *now)
192 {
193 	struct utmpx utmpx;
194 	const char *t;
195 
196 	memset((void *)&utmpx, 0, sizeof(utmpx));
197 	utmpx.ut_tv = *now;
198 	(void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
199 	if (hostname) {
200 		(void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
201 		if (ss)
202 			utmpx.ut_ss = *ss;
203 	}
204 	(void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
205 	utmpx.ut_type = USER_PROCESS;
206 	utmpx.ut_pid = getpid();
207 	t = tty + strlen(tty);
208 	if (t - tty >= sizeof(utmpx.ut_id)) {
209 		(void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
210 		    sizeof(utmpx.ut_id));
211 	} else {
212 		(void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
213 	}
214 	if (pututxline(&utmpx) == NULL)
215 		syslog(LOG_NOTICE, "Cannot update utmpx %m");
216 	endutxent();
217 	if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
218 		syslog(LOG_NOTICE, "Cannot update wtmpx %m");
219 }
220 
221 static void
222 dolastlogx(int quiet, const struct passwd *pwd, const char *hostname,
223     const char *tty, const struct sockaddr_storage *ss,
224     const struct timeval *now)
225 {
226 	struct lastlogx ll;
227 	if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL) {
228 		time_t t = (time_t)ll.ll_tv.tv_sec;
229 		if (!quiet) {
230 			(void)printf("Last login: %.24s ", ctime(&t));
231 			if (*ll.ll_host != '\0')
232 				(void)printf("from %.*s ",
233 				    (int)sizeof(ll.ll_host), ll.ll_host);
234 			(void)printf("on %.*s\n", (int)sizeof(ll.ll_line),
235 			    ll.ll_line);
236 		}
237 	}
238 	ll.ll_tv = *now;
239 	(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
240 	if (hostname) {
241 		(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
242 		if (ss)
243 			ll.ll_ss = *ss;
244 	}
245 	if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
246 		syslog(LOG_NOTICE, "Cannot update lastlogx %m");
247 	PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
248 }
249 #endif
250 
251 #ifdef SUPPORT_UTMP
252 static void
253 doutmp(const char *username, const char *hostname, const char *tty,
254     const struct timeval *now)
255 {
256 	struct utmp utmp;
257 
258 	(void)memset((void *)&utmp, 0, sizeof(utmp));
259 	utmp.ut_time = now->tv_sec;
260 	(void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
261 	if (hostname)
262 		(void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
263 	(void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
264 	login(&utmp);
265 }
266 
267 static void
268 dolastlog(int quiet, const struct passwd *pwd, const char *hostname,
269     const char *tty, const struct timeval *now)
270 {
271 	struct lastlog ll;
272 	int fd;
273 
274 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
275 		(void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
276 		if (!quiet) {
277 			if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
278 			    ll.ll_time != 0) {
279 				(void)printf("Last login: %.24s ",
280 				    ctime(&ll.ll_time));
281 				if (*ll.ll_host != '\0')
282 					(void)printf("from %.*s ",
283 					    (int)sizeof(ll.ll_host),
284 					    ll.ll_host);
285 				(void)printf("on %.*s\n",
286 				    (int)sizeof(ll.ll_line), ll.ll_line);
287 			}
288 			(void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)),
289 			    SEEK_SET);
290 		}
291 		(void)memset((void *)&ll, 0, sizeof(ll));
292 		ll.ll_time = now->tv_sec;
293 		(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
294 		if (hostname)
295 			(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
296 		(void)write(fd, (char *)&ll, sizeof(ll));
297 		(void)close(fd);
298 	}
299 	PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
300 }
301 #endif
302 
303 PAM_MODULE_ENTRY("pam_lastlog");
304