1 /* $NetBSD: pam_lastlog.c,v 1.2 2004/12/12 08:18:46 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.2 2004/12/12 08:18:46 christos Exp $"); 51 #endif 52 53 #define _BSD_SOURCE 54 55 #include <sys/param.h> 56 57 #include <fcntl.h> 58 #include <util.h> 59 #include <paths.h> 60 #include <pwd.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <syslog.h> 65 #include <time.h> 66 #include <unistd.h> 67 #include <utmp.h> 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 PAM_EXTERN int 76 pam_sm_open_session(pam_handle_t *pamh, int flags, 77 int argc __unused, const char *argv[] __unused) 78 { 79 struct passwd *pwd; 80 struct utmp utmp; 81 struct lastlog ll; 82 time_t t; 83 const char *user; 84 const void *rhost, *tty; 85 off_t llpos; 86 int fd, pam_err; 87 88 pam_err = pam_get_user(pamh, &user, NULL); 89 if (pam_err != PAM_SUCCESS) 90 return (pam_err); 91 if (user == NULL || (pwd = getpwnam(user)) == NULL) 92 return (PAM_SERVICE_ERR); 93 PAM_LOG("Got user: %s", user); 94 95 pam_err = pam_get_item(pamh, PAM_RHOST, &rhost); 96 if (pam_err != PAM_SUCCESS) 97 goto err; 98 pam_err = pam_get_item(pamh, PAM_TTY, &tty); 99 if (pam_err != PAM_SUCCESS) 100 goto err; 101 if (tty == NULL) { 102 pam_err = PAM_SERVICE_ERR; 103 goto err; 104 } 105 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0) 106 tty = (const char *)tty + strlen(_PATH_DEV); 107 if (*(const char *)tty == '\0') 108 return (PAM_SERVICE_ERR); 109 110 fd = open(_PATH_LASTLOG, O_RDWR|O_CREAT, 0644); 111 if (fd == -1) 112 goto file_err; 113 114 /* 115 * Record session in lastlog(5). 116 */ 117 llpos = (off_t)(pwd->pw_uid * sizeof(ll)); 118 if (lseek(fd, llpos, L_SET) != llpos) 119 goto file_err; 120 if ((flags & PAM_SILENT) == 0) { 121 if (read(fd, &ll, sizeof ll) == sizeof ll && ll.ll_time != 0) { 122 t = ll.ll_time; 123 if (*ll.ll_host != '\0') 124 pam_info(pamh, "Last login: %.*s from %.*s", 125 24 - 5, ctime(&t), 126 (int)sizeof(ll.ll_host), ll.ll_host); 127 else 128 pam_info(pamh, "Last login: %.*s on %.*s", 129 24 - 5, ctime(&t), 130 (int)sizeof(ll.ll_line), ll.ll_line); 131 } 132 if (lseek(fd, llpos, L_SET) != llpos) 133 goto file_err; 134 } 135 136 bzero(&ll, sizeof(ll)); 137 ll.ll_time = time(NULL); 138 139 /* note: does not need to be NUL-terminated */ 140 strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 141 if (rhost != NULL && *(const char *)rhost != '\0') 142 /* note: does not need to be NUL-terminated */ 143 strncpy(ll.ll_host, rhost, sizeof(ll.ll_host)); 144 145 if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0) 146 goto file_err; 147 148 PAM_LOG("Login recorded in %s", _PATH_LASTLOG); 149 150 /* 151 * Record session in utmp(5) and wtmp(5). 152 */ 153 bzero(&utmp, sizeof(utmp)); 154 utmp.ut_time = time(NULL); 155 /* note: does not need to be NUL-terminated */ 156 strncpy(utmp.ut_name, user, sizeof(utmp.ut_name)); 157 if (rhost != NULL && *(const char *)rhost != '\0') 158 strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host)); 159 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 160 login(&utmp); 161 162 return (PAM_SUCCESS); 163 164 file_err: 165 syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG); 166 if (fd != -1) 167 close(fd); 168 pam_err = PAM_SYSTEM_ERR; 169 err: 170 if (openpam_get_option(pamh, "no_fail")) 171 return (PAM_SUCCESS); 172 return (pam_err); 173 } 174 175 PAM_EXTERN int 176 pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, 177 int argc __unused, const char *argv[] __unused) 178 { 179 const void *tty; 180 181 pam_get_item(pamh, PAM_TTY, (const void **)&tty); 182 if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0) 183 tty = (const char *)tty + strlen(_PATH_DEV); 184 if (*(const char *)tty == '\0') 185 return (PAM_SERVICE_ERR); 186 if (logout(tty) != 1) 187 syslog(LOG_ERR, "%s(): no utmp record for %s", 188 __func__, (const char *)tty); 189 logwtmp(tty, "", ""); 190 return (PAM_SUCCESS); 191 } 192 193 PAM_MODULE_ENTRY("pam_lastlog"); 194