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