pam_lastlog.c revision 202211
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: head/lib/libpam/modules/pam_lastlog/pam_lastlog.c 202211 2010-01-13 18:32:31Z ed $");
46
47#define _BSD_SOURCE
48
49#include <pwd.h>
50#include <time.h>
51#include <ulog.h>
52#include <utmpx.h>
53
54#define PAM_SM_SESSION
55
56#include <security/pam_appl.h>
57#include <security/pam_modules.h>
58#include <security/pam_mod_misc.h>
59
60PAM_EXTERN int
61pam_sm_open_session(pam_handle_t *pamh, int flags,
62    int argc __unused, const char *argv[] __unused)
63{
64	struct passwd *pwd;
65	struct utmpx *utx;
66	time_t t;
67	const char *user;
68	const void *rhost, *tty;
69	int pam_err;
70
71	pam_err = pam_get_user(pamh, &user, NULL);
72	if (pam_err != PAM_SUCCESS)
73		return (pam_err);
74	if (user == NULL || (pwd = getpwnam(user)) == NULL)
75		return (PAM_SERVICE_ERR);
76	PAM_LOG("Got user: %s", user);
77
78	pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
79	if (pam_err != PAM_SUCCESS) {
80		PAM_LOG("No PAM_RHOST");
81		goto err;
82	}
83	pam_err = pam_get_item(pamh, PAM_TTY, &tty);
84	if (pam_err != PAM_SUCCESS) {
85		PAM_LOG("No PAM_TTY");
86		goto err;
87	}
88	if (tty == NULL) {
89		PAM_LOG("No PAM_TTY");
90		pam_err = PAM_SERVICE_ERR;
91		goto err;
92	}
93
94	if ((flags & PAM_SILENT) == 0) {
95		if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) {
96			PAM_LOG("Failed to open lastlogin database");
97		} else {
98			utx = getutxuser(user);
99			if (utx != NULL && utx->ut_type == USER_PROCESS) {
100				t = utx->ut_tv.tv_sec;
101				if (*utx->ut_host != '\0')
102					pam_info(pamh, "Last login: %.*s from %s",
103					    24 - 5, ctime(&t), utx->ut_host);
104				else
105					pam_info(pamh, "Last login: %.*s on %s",
106					    24 - 5, ctime(&t), utx->ut_line);
107			}
108			endutxent();
109		}
110	}
111
112	ulog_login(tty, user, rhost);
113
114	return (PAM_SUCCESS);
115
116err:
117	if (openpam_get_option(pamh, "no_fail"))
118		return (PAM_SUCCESS);
119	return (pam_err);
120}
121
122PAM_EXTERN int
123pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
124    int argc __unused, const char *argv[] __unused)
125{
126	const void *tty;
127	int pam_err;
128
129	pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty);
130	if (pam_err != PAM_SUCCESS)
131		goto err;
132	if (tty == NULL) {
133		PAM_LOG("No PAM_TTY");
134		pam_err = PAM_SERVICE_ERR;
135		goto err;
136	}
137	ulog_logout(tty);
138	return (PAM_SUCCESS);
139
140 err:
141	if (openpam_get_option(pamh, "no_fail"))
142		return (PAM_SUCCESS);
143	return (pam_err);
144}
145
146PAM_MODULE_ENTRY("pam_lastlog");
147