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