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