xref: /dflybsd-src/lib/libpam/modules/pam_lastlog/pam_lastlog.c (revision a44784ff200e5a0d069df30d87167c350664c287)
1242be47eSzrj /*-
2242be47eSzrj  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3242be47eSzrj  *	The Regents of the University of California.  All rights reserved.
4242be47eSzrj  * Copyright (c) 2001 Mark R V Murray
5242be47eSzrj  * All rights reserved.
6242be47eSzrj  * Copyright (c) 2001 Networks Associates Technology, Inc.
7242be47eSzrj  * All rights reserved.
8242be47eSzrj  * Copyright (c) 2004 Joe R. Doupnik
9242be47eSzrj  * All rights reserved.
10242be47eSzrj  *
11242be47eSzrj  * Portions of this software were developed for the FreeBSD Project by
12242be47eSzrj  * ThinkSec AS and NAI Labs, the Security Research Division of Network
13242be47eSzrj  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
14242be47eSzrj  * ("CBOSS"), as part of the DARPA CHATS research program.
15242be47eSzrj  *
16242be47eSzrj  * Redistribution and use in source and binary forms, with or without
17242be47eSzrj  * modification, are permitted provided that the following conditions
18242be47eSzrj  * are met:
19242be47eSzrj  * 1. Redistributions of source code must retain the above copyright
20242be47eSzrj  *    notice, this list of conditions and the following disclaimer.
21242be47eSzrj  * 2. Redistributions in binary form must reproduce the above copyright
22242be47eSzrj  *    notice, this list of conditions and the following disclaimer in the
23242be47eSzrj  *    documentation and/or other materials provided with the distribution.
24242be47eSzrj  * 3. The name of the author may not be used to endorse or promote
25242be47eSzrj  *    products derived from this software without specific prior written
26242be47eSzrj  *    permission.
27242be47eSzrj  * 4. Neither the name of the University nor the names of its contributors
28242be47eSzrj  *    may be used to endorse or promote products derived from this software
29242be47eSzrj  *    without specific prior written permission.
30242be47eSzrj  *
31242be47eSzrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32242be47eSzrj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33242be47eSzrj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34242be47eSzrj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35242be47eSzrj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36242be47eSzrj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37242be47eSzrj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38242be47eSzrj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39242be47eSzrj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40242be47eSzrj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41242be47eSzrj  * SUCH DAMAGE.
42242be47eSzrj  *
43242be47eSzrj  * $FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.23 2007/07/22 15:17:29 des Exp $
44242be47eSzrj  */
45242be47eSzrj 
46242be47eSzrj #define _BSD_SOURCE
47242be47eSzrj 
48242be47eSzrj #include <sys/param.h>
49242be47eSzrj 
50242be47eSzrj #include <fcntl.h>
51242be47eSzrj #include <libutil.h>
52242be47eSzrj #include <paths.h>
53242be47eSzrj #include <pwd.h>
54242be47eSzrj #include <stdio.h>
55242be47eSzrj #include <stdlib.h>
56242be47eSzrj #include <string.h>
57242be47eSzrj #include <syslog.h>
58242be47eSzrj #include <time.h>
59242be47eSzrj #include <unistd.h>
60*a44784ffSSascha Wildner #include <utmpx.h>
61242be47eSzrj 
62242be47eSzrj #define PAM_SM_SESSION
63242be47eSzrj 
64242be47eSzrj #include <security/pam_appl.h>
65242be47eSzrj #include <security/pam_modules.h>
66242be47eSzrj #include <security/pam_mod_misc.h>
67242be47eSzrj 
68242be47eSzrj static void doutmpx(const char *, const char *, const char *,
69242be47eSzrj     const struct sockaddr_storage *ss, const struct timeval *);
70242be47eSzrj static void dolastlogx(pam_handle_t *, int, const struct passwd *, const char *,
71242be47eSzrj     const char *, const struct sockaddr_storage *ss, const struct timeval *);
72242be47eSzrj static void domsg(pam_handle_t *, time_t, const char *, size_t, const char *,
73242be47eSzrj     size_t);
74242be47eSzrj static void logit(int, const char *, ...) __printflike(2, 3);
75242be47eSzrj 
76242be47eSzrj static void
logit(int level,const char * fmt,...)77242be47eSzrj logit(int level, const char *fmt, ...)
78242be47eSzrj {
79242be47eSzrj 	va_list ap;
80242be47eSzrj 
81242be47eSzrj 	openlog("pam_lastlog", LOG_PID, LOG_AUTHPRIV);
82242be47eSzrj 	va_start(ap, fmt);
83242be47eSzrj 	vsyslog(level, fmt, ap);
84242be47eSzrj 	va_end(ap);
85242be47eSzrj 	closelog();
86242be47eSzrj }
87242be47eSzrj 
88242be47eSzrj 
89242be47eSzrj PAM_EXTERN int
pam_sm_open_session(pam_handle_t * pamh,int flags,int argc __unused,const char * argv[]__unused)90242be47eSzrj pam_sm_open_session(pam_handle_t *pamh, int flags,
91242be47eSzrj     int argc __unused, const char *argv[] __unused)
92242be47eSzrj {
93242be47eSzrj 	struct passwd *pwd, pwres;
94242be47eSzrj 	struct timeval now;
95242be47eSzrj 	const char *user, *rhost, *tty;
96242be47eSzrj 	const void *vrhost, *vtty;
97242be47eSzrj 	int pam_err;
98242be47eSzrj 	int quiet;
99242be47eSzrj 	char pwbuf[1024];
100242be47eSzrj #ifdef LOGIN_CAP
101242be47eSzrj 	login_cap_t *lc;
102242be47eSzrj #endif
103242be47eSzrj 
104242be47eSzrj 	pam_err = pam_get_user(pamh, &user, NULL);
105242be47eSzrj 	if (pam_err != PAM_SUCCESS)
106242be47eSzrj 		return pam_err;
107242be47eSzrj 
108242be47eSzrj 	if (user == NULL ||
109242be47eSzrj 	    getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
110242be47eSzrj 	    pwd == NULL)
111242be47eSzrj 		return PAM_SERVICE_ERR;
112242be47eSzrj 
113242be47eSzrj 	PAM_LOG("Got user: %s", user);
114242be47eSzrj 
115242be47eSzrj 	pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost);
116242be47eSzrj 	if (pam_err != PAM_SUCCESS)
117242be47eSzrj 		goto err;
118242be47eSzrj 	rhost = (const char *)vrhost;
119242be47eSzrj 
120242be47eSzrj 	pam_err = pam_get_item(pamh, PAM_TTY, &vtty);
121242be47eSzrj 	if (pam_err != PAM_SUCCESS)
122242be47eSzrj 		goto err;
123242be47eSzrj 	tty = (const char *)vtty;
124242be47eSzrj 
125242be47eSzrj 	if (tty == NULL) {
126c98db407SSascha Wildner 		PAM_LOG("No PAM_TTY");
127242be47eSzrj 		pam_err = PAM_SERVICE_ERR;
128242be47eSzrj 		goto err;
129242be47eSzrj 	}
130242be47eSzrj 
131242be47eSzrj 	if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
132242be47eSzrj 		tty = tty + strlen(_PATH_DEV);
133242be47eSzrj 
134242be47eSzrj 	if (*tty == '\0') {
135242be47eSzrj 		pam_err = PAM_SERVICE_ERR;
136242be47eSzrj 		goto err;
137242be47eSzrj 	}
138242be47eSzrj 
139242be47eSzrj 	(void)gettimeofday(&now, NULL);
140242be47eSzrj 
141242be47eSzrj 	if ((flags & PAM_SILENT) != 0)
142242be47eSzrj 		quiet = 1;
143242be47eSzrj 	else {
144242be47eSzrj #ifdef LOGIN_CAP
145242be47eSzrj 		lc = login_getpwclass(pwd);
146242be47eSzrj 		quiet = login_getcapbool(lc, "hushlogin", 0);
147242be47eSzrj 		login_close(lc);
148242be47eSzrj #else
149242be47eSzrj 		quiet = 0;
150242be47eSzrj #endif
151242be47eSzrj 	}
152242be47eSzrj 	dolastlogx(pamh, quiet, pwd, rhost, tty, NULL, &now);
153242be47eSzrj 	doutmpx(user, rhost, tty, NULL, &now);
154242be47eSzrj 	quiet = 1;
155242be47eSzrj err:
156242be47eSzrj 	if (openpam_get_option(pamh, "no_fail"))
157242be47eSzrj 		return PAM_SUCCESS;
158242be47eSzrj 	return pam_err;
159242be47eSzrj }
160242be47eSzrj 
161242be47eSzrj PAM_EXTERN int
pam_sm_close_session(pam_handle_t * pamh __unused,int flags __unused,int argc __unused,const char * argv[]__unused)162242be47eSzrj pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused,
163242be47eSzrj     int argc __unused, const char *argv[] __unused)
164242be47eSzrj {
165242be47eSzrj 	const void *vtty;
166242be47eSzrj 	const char *tty;
167242be47eSzrj 
168242be47eSzrj 	pam_get_item(pamh, PAM_TTY, &vtty);
169242be47eSzrj 	if (vtty == NULL)
170242be47eSzrj 		return PAM_SERVICE_ERR;
171242be47eSzrj 	tty = (const char *)vtty;
172242be47eSzrj 
173242be47eSzrj 	if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0)
174242be47eSzrj 		tty = tty + strlen(_PATH_DEV);
175242be47eSzrj 
176242be47eSzrj 	if (*tty == '\0')
177242be47eSzrj 		return PAM_SERVICE_ERR;
178242be47eSzrj 
179242be47eSzrj 	if (logoutx(tty, 0, DEAD_PROCESS))
180242be47eSzrj 		logwtmpx(tty, "", "", 0, DEAD_PROCESS);
181242be47eSzrj 	else
182242be47eSzrj 		logit(LOG_NOTICE, "%s(): no utmpx record for %s",
183242be47eSzrj 		    __func__, tty);
184242be47eSzrj 
185242be47eSzrj         return PAM_SUCCESS;
186242be47eSzrj }
187242be47eSzrj 
188242be47eSzrj static void
domsg(pam_handle_t * pamh,time_t t,const char * host,size_t hsize,const char * line,size_t lsize)189242be47eSzrj domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize,
190242be47eSzrj     const char *line, size_t lsize)
191242be47eSzrj {
192242be47eSzrj 	char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL;
193242be47eSzrj 	int pam_err;
194242be47eSzrj 
195242be47eSzrj 	if (*host) {
196242be47eSzrj 		(void)snprintf(buf, sizeof(buf), "from %.*s ",
197242be47eSzrj 		    (int)hsize, host);
198242be47eSzrj 		host = buf;
199242be47eSzrj 	}
200242be47eSzrj 
201242be47eSzrj 	pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp,
202242be47eSzrj 	    "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line);
203242be47eSzrj 
204242be47eSzrj 	if (pam_err == PAM_SUCCESS && promptresp)
205242be47eSzrj 		free(promptresp);
206242be47eSzrj }
207242be47eSzrj 
208242be47eSzrj static void
doutmpx(const char * username,const char * hostname,const char * tty,const struct sockaddr_storage * ss,const struct timeval * now)209242be47eSzrj doutmpx(const char *username, const char *hostname, const char *tty,
210242be47eSzrj     const struct sockaddr_storage *ss, const struct timeval *now)
211242be47eSzrj {
212242be47eSzrj 	struct utmpx utmpx;
213242be47eSzrj 	const char *t;
214242be47eSzrj 
215242be47eSzrj 	memset((void *)&utmpx, 0, sizeof(utmpx));
216242be47eSzrj 	utmpx.ut_tv = *now;
217242be47eSzrj 	(void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
218242be47eSzrj 	if (hostname) {
219242be47eSzrj 		(void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
220242be47eSzrj 		if (ss)
221242be47eSzrj 			utmpx.ut_ss = *ss;
222242be47eSzrj 	}
223242be47eSzrj 	(void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
224242be47eSzrj 	utmpx.ut_type = USER_PROCESS;
225242be47eSzrj 	utmpx.ut_pid = getpid();
226242be47eSzrj 	t = tty + strlen(tty);
227242be47eSzrj 	if ((size_t)(t - tty) >= sizeof(utmpx.ut_id)) {
228242be47eSzrj 		(void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
229242be47eSzrj 		    sizeof(utmpx.ut_id));
230242be47eSzrj 	} else {
231242be47eSzrj 		(void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
232242be47eSzrj 	}
233242be47eSzrj 	if (pututxline(&utmpx) == NULL)
234242be47eSzrj 		logit(LOG_NOTICE, "Cannot update utmpx %m");
235242be47eSzrj 	endutxent();
236242be47eSzrj 	if (_updwtmpx(_PATH_WTMPX, &utmpx) != 0)
237242be47eSzrj 		logit(LOG_NOTICE, "Cannot update wtmpx %m");
238242be47eSzrj }
239242be47eSzrj 
240242be47eSzrj static void
dolastlogx(pam_handle_t * pamh,int quiet,const struct passwd * pwd,const char * hostname __unused,const char * tty __unused,const struct sockaddr_storage * ss __unused,const struct timeval * now __unused)241242be47eSzrj dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd,
242242be47eSzrj     const char *hostname __unused, const char *tty __unused,
243242be47eSzrj     const struct sockaddr_storage *ss __unused,
244242be47eSzrj     const struct timeval *now __unused)
245242be47eSzrj {
246242be47eSzrj 	struct lastlogx ll;
247242be47eSzrj 	if (!quiet) {
248242be47eSzrj 	    if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL)
249242be47eSzrj 		    domsg(pamh, (time_t)ll.ll_tv.tv_sec, ll.ll_host,
250242be47eSzrj 			sizeof(ll.ll_host), ll.ll_line,
251242be47eSzrj 			sizeof(ll.ll_line));
252242be47eSzrj 	}
253242be47eSzrj #if 0
254242be47eSzrj 	ll.ll_tv = *now;
255242be47eSzrj 	(void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
256242be47eSzrj 
257242be47eSzrj 	if (hostname)
258242be47eSzrj 		(void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
259242be47eSzrj 	else
260242be47eSzrj 		(void)memset(ll.ll_host, 0, sizeof(ll.ll_host));
261242be47eSzrj 
262242be47eSzrj 	if (ss)
263242be47eSzrj 		ll.ll_ss = *ss;
264242be47eSzrj 	else
265242be47eSzrj 		(void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
266242be47eSzrj 
267242be47eSzrj 	if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
268242be47eSzrj 		logit(LOG_NOTICE, "Cannot update lastlogx %m");
269242be47eSzrj 	PAM_LOG("Login recorded in %s", _PATH_LASTLOGX);
270242be47eSzrj #endif
271242be47eSzrj }
272242be47eSzrj 
273242be47eSzrj PAM_MODULE_ENTRY("pam_lastlog");
274