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