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 <sys/time.h> 50 #include <pwd.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <time.h> 54 #include <unistd.h> 55 #include <utmpx.h> 56 57 #define PAM_SM_SESSION 58 59 #include <security/pam_appl.h> 60 #include <security/pam_modules.h> 61 #include <security/pam_mod_misc.h> 62 63 #define PAM_UTMPX_ID "utmpx_id" 64 65 PAM_EXTERN int 66 pam_sm_open_session(pam_handle_t *pamh, int flags, 67 int argc __unused, const char *argv[] __unused) 68 { 69 struct passwd *pwd; 70 struct utmpx *utx, utl; 71 time_t t; 72 const char *user; 73 const void *rhost, *tty; 74 char *id; 75 int pam_err; 76 77 pam_err = pam_get_user(pamh, &user, NULL); 78 if (pam_err != PAM_SUCCESS) 79 return (pam_err); 80 if (user == NULL || (pwd = getpwnam(user)) == NULL) 81 return (PAM_SERVICE_ERR); 82 PAM_LOG("Got user: %s", user); 83 84 pam_err = pam_get_item(pamh, PAM_RHOST, &rhost); 85 if (pam_err != PAM_SUCCESS) { 86 PAM_LOG("No PAM_RHOST"); 87 goto err; 88 } 89 pam_err = pam_get_item(pamh, PAM_TTY, &tty); 90 if (pam_err != PAM_SUCCESS) { 91 PAM_LOG("No PAM_TTY"); 92 goto err; 93 } 94 if (tty == NULL) { 95 PAM_LOG("No PAM_TTY"); 96 pam_err = PAM_SERVICE_ERR; 97 goto err; 98 } 99 100 if ((flags & PAM_SILENT) == 0) { 101 if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) { 102 PAM_LOG("Failed to open lastlogin database"); 103 } else { 104 utx = getutxuser(user); 105 if (utx != NULL && utx->ut_type == USER_PROCESS) { 106 t = utx->ut_tv.tv_sec; 107 if (*utx->ut_host != '\0') 108 pam_info(pamh, "Last login: %.*s from %s", 109 24 - 5, ctime(&t), utx->ut_host); 110 else 111 pam_info(pamh, "Last login: %.*s on %s", 112 24 - 5, ctime(&t), utx->ut_line); 113 } 114 endutxent(); 115 } 116 } 117 118 id = malloc(sizeof utl.ut_id); 119 if (id == NULL) { 120 pam_err = PAM_SERVICE_ERR; 121 goto err; 122 } 123 arc4random_buf(id, sizeof utl.ut_id); 124 125 pam_err = pam_set_data(pamh, PAM_UTMPX_ID, id, openpam_free_data); 126 if (pam_err != PAM_SUCCESS) { 127 free(id); 128 goto err; 129 } 130 131 memset(&utl, 0, sizeof utl); 132 utl.ut_type = USER_PROCESS; 133 memcpy(utl.ut_id, id, sizeof utl.ut_id); 134 strncpy(utl.ut_user, user, sizeof utl.ut_user); 135 strncpy(utl.ut_line, tty, sizeof utl.ut_line); 136 if (rhost != NULL) 137 strncpy(utl.ut_host, rhost, sizeof utl.ut_host); 138 utl.ut_pid = getpid(); 139 gettimeofday(&utl.ut_tv, NULL); 140 pututxline(&utl); 141 142 return (PAM_SUCCESS); 143 144 err: 145 if (openpam_get_option(pamh, "no_fail")) 146 return (PAM_SUCCESS); 147 return (pam_err); 148 } 149 150 PAM_EXTERN int 151 pam_sm_close_session(pam_handle_t *pamh, int flags __unused, 152 int argc __unused, const char *argv[] __unused) 153 { 154 struct utmpx utl; 155 const void *id; 156 int pam_err; 157 158 pam_err = pam_get_data(pamh, PAM_UTMPX_ID, (const void **)&id); 159 if (pam_err != PAM_SUCCESS) 160 goto err; 161 162 memset(&utl, 0, sizeof utl); 163 utl.ut_type = DEAD_PROCESS; 164 memcpy(utl.ut_id, id, sizeof utl.ut_id); 165 utl.ut_pid = getpid(); 166 gettimeofday(&utl.ut_tv, NULL); 167 pututxline(&utl); 168 169 return (PAM_SUCCESS); 170 171 err: 172 if (openpam_get_option(pamh, "no_fail")) 173 return (PAM_SUCCESS); 174 return (pam_err); 175 } 176 177 PAM_MODULE_ENTRY("pam_lastlog"); 178