xref: /freebsd/lib/libpam/modules/pam_lastlog/pam_lastlog.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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  * Copyright (c) 2004 Joe R. Doupnik
9  * All rights reserved.
10  *
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.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
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
26  *    permission.
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.
30  *
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
41  * SUCH DAMAGE.
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #define _BSD_SOURCE
48 
49 #include <sys/time.h>
50 
51 #include <paths.h>
52 #include <pwd.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <utmpx.h>
58 
59 #define PAM_SM_SESSION
60 
61 #include <security/pam_appl.h>
62 #include <security/pam_modules.h>
63 #include <security/pam_mod_misc.h>
64 
65 #define	PAM_UTMPX_ID	"utmpx_id"
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 utmpx *utx, utl;
73 	time_t t;
74 	const char *user;
75 	const void *rhost, *tty;
76 	char *id;
77 	int 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, &rhost);
87 	if (pam_err != PAM_SUCCESS) {
88 		PAM_LOG("No PAM_RHOST");
89 		goto err;
90 	}
91 	pam_err = pam_get_item(pamh, PAM_TTY, &tty);
92 	if (pam_err != PAM_SUCCESS) {
93 		PAM_LOG("No PAM_TTY");
94 		goto err;
95 	}
96 	if (tty == NULL) {
97 		PAM_LOG("No PAM_TTY");
98 		pam_err = PAM_SERVICE_ERR;
99 		goto err;
100 	}
101 	/* Strip /dev/ component. */
102 	if (strncmp(tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
103 		tty = (const char *)tty + sizeof(_PATH_DEV) - 1;
104 
105 	if ((flags & PAM_SILENT) == 0) {
106 		if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) {
107 			PAM_LOG("Failed to open lastlogin database");
108 		} else {
109 			utx = getutxuser(user);
110 			if (utx != NULL && utx->ut_type == USER_PROCESS) {
111 				t = utx->ut_tv.tv_sec;
112 				if (*utx->ut_host != '\0')
113 					pam_info(pamh, "Last login: %.*s from %s",
114 					    24 - 5, ctime(&t), utx->ut_host);
115 				else
116 					pam_info(pamh, "Last login: %.*s on %s",
117 					    24 - 5, ctime(&t), utx->ut_line);
118 			}
119 			endutxent();
120 		}
121 	}
122 
123 	id = malloc(sizeof utl.ut_id);
124 	if (id == NULL) {
125 		pam_err = PAM_SERVICE_ERR;
126 		goto err;
127 	}
128 	arc4random_buf(id, sizeof utl.ut_id);
129 
130 	pam_err = pam_set_data(pamh, PAM_UTMPX_ID, id, openpam_free_data);
131 	if (pam_err != PAM_SUCCESS) {
132 		free(id);
133 		goto err;
134 	}
135 
136 	memset(&utl, 0, sizeof utl);
137 	utl.ut_type = USER_PROCESS;
138 	memcpy(utl.ut_id, id, sizeof utl.ut_id);
139 	strncpy(utl.ut_user, user, sizeof utl.ut_user);
140 	strncpy(utl.ut_line, tty, sizeof utl.ut_line);
141 	if (rhost != NULL)
142 		strncpy(utl.ut_host, rhost, sizeof utl.ut_host);
143 	utl.ut_pid = getpid();
144 	gettimeofday(&utl.ut_tv, NULL);
145 	pututxline(&utl);
146 
147 	return (PAM_SUCCESS);
148 
149 err:
150 	if (openpam_get_option(pamh, "no_fail"))
151 		return (PAM_SUCCESS);
152 	return (pam_err);
153 }
154 
155 PAM_EXTERN int
156 pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
157     int argc __unused, const char *argv[] __unused)
158 {
159 	struct utmpx utl;
160 	const void *id;
161 	int pam_err;
162 
163 	pam_err = pam_get_data(pamh, PAM_UTMPX_ID, (const void **)&id);
164 	if (pam_err != PAM_SUCCESS)
165 		goto err;
166 
167 	memset(&utl, 0, sizeof utl);
168 	utl.ut_type = DEAD_PROCESS;
169 	memcpy(utl.ut_id, id, sizeof utl.ut_id);
170 	utl.ut_pid = getpid();
171 	gettimeofday(&utl.ut_tv, NULL);
172 	pututxline(&utl);
173 
174 	return (PAM_SUCCESS);
175 
176  err:
177 	if (openpam_get_option(pamh, "no_fail"))
178 		return (PAM_SUCCESS);
179 	return (pam_err);
180 }
181 
182 PAM_MODULE_ENTRY("pam_lastlog");
183