pam_lastlog.c revision 89743
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 Technologies, Inc. 7 * All rights reserved. 8 * 9 * Portions of this software were developed for the FreeBSD Project by 10 * ThinkSec AS and NAI Labs, the Security Research Division of Network 11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 12 * ("CBOSS"), as part of the DARPA CHATS research program. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. The name of the author may not be used to endorse or promote 23 * products derived from this software without specific prior written 24 * permission. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_lastlog/pam_lastlog.c 89743 2002-01-24 16:14:56Z des $"); 44 45#define _BSD_SOURCE 46 47#include <sys/param.h> 48 49#include <fcntl.h> 50#include <libutil.h> 51#include <pwd.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <syslog.h> 56#include <time.h> 57#include <unistd.h> 58#include <utmp.h> 59 60#define PAM_SM_AUTH 61#define PAM_SM_ACCOUNT 62#define PAM_SM_SESSION 63#define PAM_SM_PASSWORD 64 65#include <security/pam_modules.h> 66#include <pam_mod_misc.h> 67 68extern int login_access(const char *, const char *); 69 70PAM_EXTERN int 71pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) 72{ 73 struct options options; 74 75 pam_std_option(&options, NULL, argc, argv); 76 77 PAM_LOG("Options processed"); 78 79 PAM_RETURN(PAM_IGNORE); 80} 81 82PAM_EXTERN int 83pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) 84{ 85 struct options options; 86 87 pam_std_option(&options, NULL, argc, argv); 88 89 PAM_LOG("Options processed"); 90 91 PAM_RETURN(PAM_IGNORE); 92} 93 94PAM_EXTERN int 95pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc ,const char **argv) 96{ 97 struct options options; 98 99 pam_std_option(&options, NULL, argc, argv); 100 101 PAM_LOG("Options processed"); 102 103 PAM_RETURN(PAM_IGNORE); 104} 105 106PAM_EXTERN int 107pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv) 108{ 109 struct options options; 110 111 pam_std_option(&options, NULL, argc, argv); 112 113 PAM_LOG("Options processed"); 114 115 PAM_RETURN(PAM_IGNORE); 116} 117 118PAM_EXTERN int 119pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) 120{ 121 struct options options; 122 struct passwd *pwd; 123 struct utmp utmp; 124 struct lastlog ll; 125 const char *rhost, *user, *tty; 126 char *buf; 127 off_t llpos; 128 int fd, pam_err; 129 130 pam_std_option(&options, NULL, argc, argv); 131 132 PAM_LOG("Options processed"); 133 134 pam_err = pam_get_item(pamh, PAM_USER, (const void **)&user); 135 if (pam_err != PAM_SUCCESS) 136 PAM_RETURN(pam_err); 137 if (user == NULL || (pwd = getpwnam(user)) == NULL) 138 PAM_RETURN(PAM_SERVICE_ERR); 139 PAM_LOG("Got user: %s", user); 140 141 pam_err = pam_get_item(pamh, PAM_RHOST, (const void **)&rhost); 142 if (pam_err != PAM_SUCCESS) 143 PAM_RETURN(pam_err); 144 145 pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty); 146 if (pam_err != PAM_SUCCESS) 147 PAM_RETURN(pam_err); 148 if (tty == NULL) 149 PAM_RETURN(PAM_SERVICE_ERR); 150 151 fd = open(_PATH_LASTLOG, O_RDWR, 0); 152 if (fd == -1) { 153 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LASTLOG); 154 PAM_RETURN(PAM_SERVICE_ERR); 155 } 156 157 /* 158 * Record session in utmp(5) and wtmp(5). 159 */ 160 bzero(&utmp, sizeof(utmp)); 161 time(&utmp.ut_time); 162 /* note: does not need to be NUL-terminated */ 163 strncpy(utmp.ut_name, user, sizeof(utmp.ut_name)); 164 if (rhost != NULL) 165 strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host)); 166 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 167 login(&utmp); 168 169 /* 170 * Record session in lastlog(5). 171 */ 172 llpos = (off_t)(pwd->pw_uid * sizeof(ll)); 173 if (lseek(fd, llpos, L_SET) != llpos) 174 goto file_err; 175 if ((flags & PAM_SILENT) == 0) { 176 if (read(fd, &ll, sizeof(ll)) == sizeof(ll) && 177 ll.ll_time != 0) { 178 asprintf(&buf, "Last login: %.*s ", 24 - 5, 179 ctime(&ll.ll_time)); 180 if (buf != NULL) { 181 pam_prompt(pamh, PAM_TEXT_INFO, buf, NULL); 182 free(buf); 183 } 184 if (*ll.ll_host != '\0') 185 asprintf(&buf, "from %.*s\n", 186 (int)sizeof(ll.ll_host), ll.ll_host); 187 else 188 asprintf(&buf, "on %.*s\n", 189 (int)sizeof(ll.ll_line), ll.ll_line); 190 if (buf != NULL) { 191 pam_prompt(pamh, PAM_TEXT_INFO, buf, NULL); 192 free(buf); 193 } 194 } 195 if (lseek(fd, llpos, L_SET) != llpos) 196 goto file_err; 197 } 198 199 bzero(&ll, sizeof(ll)); 200 time(&ll.ll_time); 201 202 /* note: does not need to be NUL-terminated */ 203 strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 204 if (rhost != NULL) 205 /* note: does not need to be NUL-terminated */ 206 strncpy(ll.ll_host, rhost, sizeof(ll.ll_host)); 207 208 if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0) 209 goto file_err; 210 211 PAM_LOG("Login recorded in %s", _PATH_LASTLOG); 212 PAM_RETURN(PAM_IGNORE); 213 214 file_err: 215 syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG); 216 close(fd); 217 PAM_RETURN(PAM_SERVICE_ERR); 218} 219 220PAM_EXTERN int 221pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) 222{ 223 struct options options; 224 const char *tty; 225 int pam_err; 226 227 pam_std_option(&options, NULL, argc, argv); 228 229 PAM_LOG("Options processed"); 230 231 pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty); 232 if (pam_err != PAM_SUCCESS) 233 PAM_RETURN(pam_err); 234 if (tty != NULL) 235 logout(tty); 236 237 PAM_RETURN(PAM_SUCCESS); 238} 239 240PAM_MODULE_ENTRY("pam_lastlog"); 241