xref: /freebsd/lib/libpam/modules/pam_lastlog/pam_lastlog.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #define _BSD_SOURCE
48 
49 #include <sys/time.h>
50 
51 #include <paths.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56 #include <utmpx.h>
57 
58 #define PAM_SM_SESSION
59 
60 #include <security/pam_appl.h>
61 #include <security/pam_modules.h>
62 #include <security/pam_mod_misc.h>
63 
64 #define	PAM_UTMPX_ID	"utmpx_id"
65 
66 PAM_EXTERN int
67 pam_sm_open_session(pam_handle_t *pamh, int flags,
68     int argc __unused, const char *argv[] __unused)
69 {
70 	struct utmpx *utx, utl;
71 	time_t t;
72 	const char *user;
73 	const void *rhost, *tty;
74 	char *id;
75 	int pam_err;
76 
77 	pam_err = pam_get_user(pamh, &user, NULL);
78 	if (pam_err != PAM_SUCCESS)
79 		return (pam_err);
80 	if (user == NULL)
81 		return (PAM_SERVICE_ERR);
82 	PAM_LOG("Got user: %s", user);
83 
84 	pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
85 	if (pam_err != PAM_SUCCESS) {
86 		PAM_LOG("No PAM_RHOST");
87 		goto err;
88 	}
89 	pam_err = pam_get_item(pamh, PAM_TTY, &tty);
90 	if (pam_err != PAM_SUCCESS) {
91 		PAM_LOG("No PAM_TTY");
92 		goto err;
93 	}
94 	if (tty == NULL) {
95 		PAM_LOG("No PAM_TTY");
96 		pam_err = PAM_SERVICE_ERR;
97 		goto err;
98 	}
99 	/* Strip /dev/ component. */
100 	if (strncmp(tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
101 		tty = (const char *)tty + sizeof(_PATH_DEV) - 1;
102 
103 	if ((flags & PAM_SILENT) == 0) {
104 		if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) {
105 			PAM_LOG("Failed to open lastlogin database");
106 		} else {
107 			utx = getutxuser(user);
108 			if (utx != NULL && utx->ut_type == USER_PROCESS) {
109 				t = utx->ut_tv.tv_sec;
110 				if (*utx->ut_host != '\0')
111 					pam_info(pamh, "Last login: %.*s from %s",
112 					    24 - 5, ctime(&t), utx->ut_host);
113 				else
114 					pam_info(pamh, "Last login: %.*s on %s",
115 					    24 - 5, ctime(&t), utx->ut_line);
116 			}
117 			endutxent();
118 		}
119 	}
120 
121 	id = malloc(sizeof utl.ut_id);
122 	if (id == NULL) {
123 		pam_err = PAM_SERVICE_ERR;
124 		goto err;
125 	}
126 	arc4random_buf(id, sizeof utl.ut_id);
127 
128 	pam_err = pam_set_data(pamh, PAM_UTMPX_ID, id, openpam_free_data);
129 	if (pam_err != PAM_SUCCESS) {
130 		free(id);
131 		goto err;
132 	}
133 
134 	memset(&utl, 0, sizeof utl);
135 	utl.ut_type = USER_PROCESS;
136 	memcpy(utl.ut_id, id, sizeof utl.ut_id);
137 	strncpy(utl.ut_user, user, sizeof utl.ut_user);
138 	strncpy(utl.ut_line, tty, sizeof utl.ut_line);
139 	if (rhost != NULL)
140 		strncpy(utl.ut_host, rhost, sizeof utl.ut_host);
141 	utl.ut_pid = getpid();
142 	gettimeofday(&utl.ut_tv, NULL);
143 	pututxline(&utl);
144 
145 	return (PAM_SUCCESS);
146 
147 err:
148 	if (openpam_get_option(pamh, "no_fail"))
149 		return (PAM_SUCCESS);
150 	return (pam_err);
151 }
152 
153 PAM_EXTERN int
154 pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
155     int argc __unused, const char *argv[] __unused)
156 {
157 	struct utmpx utl;
158 	const void *id;
159 	int pam_err;
160 
161 	pam_err = pam_get_data(pamh, PAM_UTMPX_ID, (const void **)&id);
162 	if (pam_err != PAM_SUCCESS)
163 		goto err;
164 
165 	memset(&utl, 0, sizeof utl);
166 	utl.ut_type = DEAD_PROCESS;
167 	memcpy(utl.ut_id, id, sizeof utl.ut_id);
168 	utl.ut_pid = getpid();
169 	gettimeofday(&utl.ut_tv, NULL);
170 	pututxline(&utl);
171 
172 	return (PAM_SUCCESS);
173 
174  err:
175 	if (openpam_get_option(pamh, "no_fail"))
176 		return (PAM_SUCCESS);
177 	return (pam_err);
178 }
179 
180 PAM_MODULE_ENTRY("pam_lastlog");
181