pam_lastlog.c revision 1.14 1 /* $NetBSD: pam_lastlog.c,v 1.14 2012/01/03 19:02:55 christos 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.14 2012/01/03 19:02:55 christos 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 <errno.h>
64 #include <time.h>
65 #include <unistd.h>
66 #include <stdarg.h>
67 #ifdef LOGIN_CAP
68 #include <login_cap.h>
69 #endif
70
71 #define PAM_SM_SESSION
72
73 #include <security/pam_appl.h>
74 #include <security/pam_modules.h>
75 #include <security/pam_mod_misc.h>
76
77 #ifdef SUPPORT_UTMP
78 #include <utmp.h>
79 static void doutmp(const char *, const char *, const char *,
80 const struct timeval *);
81 static void dolastlog(pam_handle_t *, int, const struct passwd *, const char *,
82 const char *, const struct timeval *);
83 #endif
84
85 #ifdef SUPPORT_UTMPX
86 #include <utmpx.h>
87 static void doutmpx(const char *, const char *, const char *,
88 const struct sockaddr_storage *ss, const struct timeval *);
89 static void dolastlogx(pam_handle_t *, int, const struct passwd *, const char *,
90 const char *, const struct sockaddr_storage *ss, const struct timeval *);
91 #endif
92
93 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
94 static void domsg(pam_handle_t *, time_t, const char *, size_t, const char *,
95 size_t);
96 #endif
97
98 static void
99 logit(int level, const char *fmt, ...)
100 {
101 va_list ap;
102 struct syslog_data data = SYSLOG_DATA_INIT;
103
104 openlog_r("pam_lastlog", LOG_PID, LOG_AUTHPRIV, &data);
105 va_start(ap, fmt);
106 vsyslog_r(level, &data, fmt, ap);
107 va_end(ap);
108 closelog_r(&data);
109 }
110
111
112 PAM_EXTERN int
113 pam_sm_open_session(pam_handle_t *pamh, int flags,
114 int argc __unused, const char *argv[] __unused)
115 {
116 struct passwd *pwd, pwres;
117 struct timeval now;
118 const char *user, *rhost, *tty, *nuser;
119 const void *vrhost, *vtty, *vss, *vnuser;
120 const struct sockaddr_storage *ss;
121 int pam_err;
122 char pwbuf[1024];
123 #ifdef LOGIN_CAP
124 login_cap_t *lc;
125 #endif
126
127 pam_err = pam_get_user(pamh, &user, NULL);
128 if (pam_err != PAM_SUCCESS)
129 return pam_err;
130
131 if (user == NULL ||
132 getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
133 pwd == NULL)
134 return PAM_SERVICE_ERR;
135
136 PAM_LOG("Got user: %s", user);
137
138 pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
139 if (pam_err != PAM_SUCCESS)
140 goto err;
141 rhost = (const char *)vrhost;
142
143 pam_err = pam_get_item(pamh, PAM_SOCKADDR, &vss);
144 if (pam_err != PAM_SUCCESS)
145 goto err;
146 ss = (const struct sockaddr_storage *)vss;
147
148 pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
149 if (pam_err != PAM_SUCCESS)
150 goto err;
151 tty = (const char *)vtty;
152
153 if (tty == NULL) {
154 pam_err = PAM_SERVICE_ERR;
155 goto err;
156 }
157
158 if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS)
159 nuser = NULL;
160 else
161 nuser = (const char *)vnuser;
162
163 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
164 tty = tty + strlen(_PATH_DEV);
165
166 if (*tty == '\0') {
167 pam_err = PAM_SERVICE_ERR;
168 goto err;
169 }
170
171 (void)gettimeofday(&now, NULL);
172
173 if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) {
174 int quiet;
175 if ((flags & PAM_SILENT) != 0)
176 quiet = 1;
177 else {
178 #ifdef LOGIN_CAP
179 lc = login_getpwclass(pwd);
180 quiet = login_getcapbool(lc, "hushlogin", 0);
181 login_close(lc);
182 #else
183 quiet = 0;
184 #endif
185 }
186 #ifdef SUPPORT_UTMPX
187 doutmpx(user, rhost, tty, ss, &now);
188 dolastlogx(pamh, quiet, pwd, rhost, tty, ss, &now);
189 quiet = 1;
190 #endif
191 #ifdef SUPPORT_UTMP
192 doutmp(user, rhost, tty, &now);
193 dolastlog(pamh, quiet, pwd, rhost, tty, &now);
194 #endif
195 }
196 err:
197 if (openpam_get_option(pamh, "no_fail"))
198 return PAM_SUCCESS;
199 return pam_err;
200 }
201
202 PAM_EXTERN int
203 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
204 int argc __unused, const char *argv[] __unused)
205 {
206 const void *vtty, *vnuser;
207 const char *tty, *nuser;
208
209 if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS)
210 nuser = NULL;
211 else
212 nuser = (const char *)vnuser;
213
214 pam_get_item(pamh, PAM_TTY, &vtty);
215 if (vtty == NULL)
216 return PAM_SERVICE_ERR;
217 tty = (const char *)vtty;
218
219 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
220 tty = tty + strlen(_PATH_DEV);
221
222 if (*tty == '\0')
223 return PAM_SERVICE_ERR;
224
225 if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) {
226
227 #ifdef SUPPORT_UTMPX
228 if (logoutx(tty, 0, DEAD_PROCESS))
229 logwtmpx(tty, "", "", 0, DEAD_PROCESS);
230 else
231 logit(LOG_NOTICE, "%s(): no utmpx record for %s",
232 __func__, tty);
233 #endif
234
235 #ifdef SUPPORT_UTMP
236 if (logout(tty))
237 logwtmp(tty, "", "");
238 else
239 logit(LOG_NOTICE, "%s(): no utmp record for %s",
240 __func__, tty);
241 #endif
242 }
243 return PAM_SUCCESS;
244 }
245
246 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
247 static void
248 domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize,
249 const char *line, size_t lsize)
250 {
251 char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL;
252 int pam_err;
253
254 if (*host) {
255 (void)snprintf(buf, sizeof(buf), "from %.*s ",
256 (int)hsize, host);
257 host = buf;
258 }
259
260 pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp,
261 "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line);
262
263 if (pam_err == PAM_SUCCESS && promptresp)
264 free(promptresp);
265 }
266 #endif
267
268 #ifdef SUPPORT_UTMPX
269 static void
270 doutmpx(const char *username, const char *hostname, const char *tty,
271 const struct sockaddr_storage *ss, const struct timeval *now)
272 {
273 struct utmpx utmpx;
274 const char *t;
275
276 memset((void *)&utmpx, 0, sizeof(utmpx));
277 utmpx.ut_tv = *now;
278 (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
279 if (hostname) {
280 (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
281 if (ss)
282 utmpx.ut_ss = *ss;
283 }
284 (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
285 utmpx.ut_type = USER_PROCESS;
286 utmpx.ut_pid = getpid();
287 t = tty + strlen(tty);
288 if ((size_t)(t - tty) >= sizeof(utmpx.ut_id)) {
289 (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
290 sizeof(utmpx.ut_id));
291 } else {
292 (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
293 }
294 if (pututxline(&utmpx) == NULL)
295 logit(LOG_NOTICE, "Cannot update utmpx: %s", strerror(errno));
296 endutxent();
297 if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
298 logit(LOG_NOTICE, "Cannot update wtmpx: %s", strerror(errno));
299 }
300
301 static void
302 dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
303 const char *hostname, const char *tty, const struct sockaddr_storage *ss,
304 const struct timeval *now)
305 {
306 struct lastlogx ll;
307 if (!quiet) {
308 if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL)
309 domsg(pamh, (time_t)ll.ll_tv.tv_sec, ll.ll_host,
310 sizeof(ll.ll_host), ll.ll_line,
311 sizeof(ll.ll_line));
312 }
313 ll.ll_tv = *now;
314 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
315
316 if (hostname)
317 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
318 else
319 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
320
321 if (ss)
322 ll.ll_ss = *ss;
323 else
324 (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
325
326 if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
327 logit(LOG_NOTICE, "Cannot update lastlogx: %s", strerror(errno));
328 PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
329 }
330 #endif
331
332 #ifdef SUPPORT_UTMP
333 static void
334 doutmp(const char *username, const char *hostname, const char *tty,
335 const struct timeval *now)
336 {
337 struct utmp utmp;
338
339 (void)memset((void *)&utmp, 0, sizeof(utmp));
340 utmp.ut_time = now->tv_sec;
341 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
342 if (hostname)
343 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
344 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
345 login(&utmp);
346 }
347
348 static void
349 dolastlog(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
350 const char *hostname, const char *tty, const struct timeval *now)
351 {
352 struct lastlog ll;
353 int fd;
354
355 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1) {
356 logit(LOG_NOTICE, "Cannot open `%s': %s", _PATH_LASTLOG,
357 strerror(errno));
358 return;
359 }
360 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
361
362 if (!quiet) {
363 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
364 ll.ll_time != 0)
365 domsg(pamh, ll.ll_time, ll.ll_host,
366 sizeof(ll.ll_host), ll.ll_line,
367 sizeof(ll.ll_line));
368 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
369 }
370
371 ll.ll_time = now->tv_sec;
372 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
373
374 if (hostname)
375 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
376 else
377 (void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
378
379 (void)write(fd, &ll, sizeof(ll));
380 (void)close(fd);
381
382 PAM_LOG("Login recorded in %s", _PATH_LASTLOG);
383 }
384 #endif
385
386 PAM_MODULE_ENTRY("pam_lastlog");
387