1 1.15 joerg /* $NetBSD: pam_lastlog.c,v 1.15 2014/01/07 02:07:43 joerg Exp $ */ 2 1.2 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994 5 1.1 christos * The Regents of the University of California. All rights reserved. 6 1.1 christos * Copyright (c) 2001 Mark R V Murray 7 1.1 christos * All rights reserved. 8 1.1 christos * Copyright (c) 2001 Networks Associates Technology, Inc. 9 1.1 christos * All rights reserved. 10 1.1 christos * Copyright (c) 2004 Joe R. Doupnik 11 1.1 christos * All rights reserved. 12 1.1 christos * 13 1.1 christos * Portions of this software were developed for the FreeBSD Project by 14 1.1 christos * ThinkSec AS and NAI Labs, the Security Research Division of Network 15 1.1 christos * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 16 1.1 christos * ("CBOSS"), as part of the DARPA CHATS research program. 17 1.1 christos * 18 1.1 christos * Redistribution and use in source and binary forms, with or without 19 1.1 christos * modification, are permitted provided that the following conditions 20 1.1 christos * are met: 21 1.1 christos * 1. Redistributions of source code must retain the above copyright 22 1.1 christos * notice, this list of conditions and the following disclaimer. 23 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 24 1.1 christos * notice, this list of conditions and the following disclaimer in the 25 1.1 christos * documentation and/or other materials provided with the distribution. 26 1.1 christos * 3. The name of the author may not be used to endorse or promote 27 1.1 christos * products derived from this software without specific prior written 28 1.1 christos * permission. 29 1.1 christos * 4. Neither the name of the University nor the names of its contributors 30 1.1 christos * may be used to endorse or promote products derived from this software 31 1.1 christos * without specific prior written permission. 32 1.1 christos * 33 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 34 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 37 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 1.1 christos * SUCH DAMAGE. 44 1.1 christos */ 45 1.1 christos 46 1.1 christos #include <sys/cdefs.h> 47 1.2 christos #ifdef __FreeBSD__ 48 1.1 christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_lastlog/pam_lastlog.c,v 1.20 2004/01/26 19:28:37 des Exp $"); 49 1.2 christos #else 50 1.15 joerg __RCSID("$NetBSD: pam_lastlog.c,v 1.15 2014/01/07 02:07:43 joerg Exp $"); 51 1.2 christos #endif 52 1.1 christos 53 1.1 christos #include <sys/param.h> 54 1.1 christos 55 1.1 christos #include <fcntl.h> 56 1.2 christos #include <util.h> 57 1.1 christos #include <paths.h> 58 1.1 christos #include <pwd.h> 59 1.1 christos #include <stdio.h> 60 1.1 christos #include <stdlib.h> 61 1.1 christos #include <string.h> 62 1.1 christos #include <syslog.h> 63 1.14 christos #include <errno.h> 64 1.1 christos #include <time.h> 65 1.1 christos #include <unistd.h> 66 1.11 christos #include <stdarg.h> 67 1.5 christos #ifdef LOGIN_CAP 68 1.5 christos #include <login_cap.h> 69 1.5 christos #endif 70 1.3 christos 71 1.6 christos #define PAM_SM_SESSION 72 1.6 christos 73 1.6 christos #include <security/pam_appl.h> 74 1.6 christos #include <security/pam_modules.h> 75 1.6 christos #include <security/pam_mod_misc.h> 76 1.6 christos 77 1.3 christos #ifdef SUPPORT_UTMP 78 1.1 christos #include <utmp.h> 79 1.3 christos static void doutmp(const char *, const char *, const char *, 80 1.3 christos const struct timeval *); 81 1.6 christos static void dolastlog(pam_handle_t *, int, const struct passwd *, const char *, 82 1.6 christos const char *, const struct timeval *); 83 1.3 christos #endif 84 1.3 christos 85 1.3 christos #ifdef SUPPORT_UTMPX 86 1.3 christos #include <utmpx.h> 87 1.3 christos static void doutmpx(const char *, const char *, const char *, 88 1.3 christos const struct sockaddr_storage *ss, const struct timeval *); 89 1.6 christos static void dolastlogx(pam_handle_t *, int, const struct passwd *, const char *, 90 1.6 christos const char *, const struct sockaddr_storage *ss, const struct timeval *); 91 1.3 christos #endif 92 1.1 christos 93 1.6 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP) 94 1.6 christos static void domsg(pam_handle_t *, time_t, const char *, size_t, const char *, 95 1.6 christos size_t); 96 1.6 christos #endif 97 1.1 christos 98 1.15 joerg __printflike(2, 3) 99 1.11 christos static void 100 1.11 christos logit(int level, const char *fmt, ...) 101 1.11 christos { 102 1.11 christos va_list ap; 103 1.12 christos struct syslog_data data = SYSLOG_DATA_INIT; 104 1.11 christos 105 1.11 christos openlog_r("pam_lastlog", LOG_PID, LOG_AUTHPRIV, &data); 106 1.11 christos va_start(ap, fmt); 107 1.11 christos vsyslog_r(level, &data, fmt, ap); 108 1.11 christos va_end(ap); 109 1.11 christos closelog_r(&data); 110 1.11 christos } 111 1.11 christos 112 1.11 christos 113 1.1 christos PAM_EXTERN int 114 1.1 christos pam_sm_open_session(pam_handle_t *pamh, int flags, 115 1.1 christos int argc __unused, const char *argv[] __unused) 116 1.1 christos { 117 1.7 thorpej struct passwd *pwd, pwres; 118 1.3 christos struct timeval now; 119 1.5 christos const char *user, *rhost, *tty, *nuser; 120 1.5 christos const void *vrhost, *vtty, *vss, *vnuser; 121 1.3 christos const struct sockaddr_storage *ss; 122 1.3 christos int pam_err; 123 1.7 thorpej char pwbuf[1024]; 124 1.10 jnemeth #ifdef LOGIN_CAP 125 1.10 jnemeth login_cap_t *lc; 126 1.10 jnemeth #endif 127 1.1 christos 128 1.1 christos pam_err = pam_get_user(pamh, &user, NULL); 129 1.1 christos if (pam_err != PAM_SUCCESS) 130 1.3 christos return pam_err; 131 1.3 christos 132 1.7 thorpej if (user == NULL || 133 1.8 christos getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 || 134 1.8 christos pwd == NULL) 135 1.3 christos return PAM_SERVICE_ERR; 136 1.3 christos 137 1.1 christos PAM_LOG("Got user: %s", user); 138 1.1 christos 139 1.4 he pam_err = pam_get_item(pamh, PAM_RHOST, &vrhost); 140 1.1 christos if (pam_err != PAM_SUCCESS) 141 1.1 christos goto err; 142 1.4 he rhost = (const char *)vrhost; 143 1.3 christos 144 1.4 he pam_err = pam_get_item(pamh, PAM_SOCKADDR, &vss); 145 1.1 christos if (pam_err != PAM_SUCCESS) 146 1.1 christos goto err; 147 1.4 he ss = (const struct sockaddr_storage *)vss; 148 1.3 christos 149 1.4 he pam_err = pam_get_item(pamh, PAM_TTY, &vtty); 150 1.3 christos if (pam_err != PAM_SUCCESS) 151 1.3 christos goto err; 152 1.4 he tty = (const char *)vtty; 153 1.3 christos 154 1.1 christos if (tty == NULL) { 155 1.1 christos pam_err = PAM_SERVICE_ERR; 156 1.1 christos goto err; 157 1.1 christos } 158 1.3 christos 159 1.5 christos if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS) 160 1.5 christos nuser = NULL; 161 1.5 christos else 162 1.5 christos nuser = (const char *)vnuser; 163 1.5 christos 164 1.1 christos if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0) 165 1.3 christos tty = tty + strlen(_PATH_DEV); 166 1.3 christos 167 1.3 christos if (*tty == '\0') { 168 1.3 christos pam_err = PAM_SERVICE_ERR; 169 1.3 christos goto err; 170 1.1 christos } 171 1.1 christos 172 1.3 christos (void)gettimeofday(&now, NULL); 173 1.1 christos 174 1.5 christos if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) { 175 1.5 christos int quiet; 176 1.9 christos if ((flags & PAM_SILENT) != 0) 177 1.9 christos quiet = 1; 178 1.9 christos else { 179 1.5 christos #ifdef LOGIN_CAP 180 1.10 jnemeth lc = login_getpwclass(pwd); 181 1.10 jnemeth quiet = login_getcapbool(lc, "hushlogin", 0); 182 1.10 jnemeth login_close(lc); 183 1.5 christos #else 184 1.9 christos quiet = 0; 185 1.5 christos #endif 186 1.9 christos } 187 1.3 christos #ifdef SUPPORT_UTMPX 188 1.5 christos doutmpx(user, rhost, tty, ss, &now); 189 1.6 christos dolastlogx(pamh, quiet, pwd, rhost, tty, ss, &now); 190 1.5 christos quiet = 1; 191 1.3 christos #endif 192 1.3 christos #ifdef SUPPORT_UTMP 193 1.5 christos doutmp(user, rhost, tty, &now); 194 1.6 christos dolastlog(pamh, quiet, pwd, rhost, tty, &now); 195 1.3 christos #endif 196 1.5 christos } 197 1.1 christos err: 198 1.1 christos if (openpam_get_option(pamh, "no_fail")) 199 1.3 christos return PAM_SUCCESS; 200 1.3 christos return pam_err; 201 1.1 christos } 202 1.1 christos 203 1.1 christos PAM_EXTERN int 204 1.1 christos pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, 205 1.1 christos int argc __unused, const char *argv[] __unused) 206 1.1 christos { 207 1.5 christos const void *vtty, *vnuser; 208 1.5 christos const char *tty, *nuser; 209 1.5 christos 210 1.5 christos if (pam_get_item(pamh, PAM_NUSER, &vnuser) != PAM_SUCCESS) 211 1.5 christos nuser = NULL; 212 1.5 christos else 213 1.5 christos nuser = (const char *)vnuser; 214 1.3 christos 215 1.4 he pam_get_item(pamh, PAM_TTY, &vtty); 216 1.4 he if (vtty == NULL) 217 1.3 christos return PAM_SERVICE_ERR; 218 1.4 he tty = (const char *)vtty; 219 1.1 christos 220 1.1 christos if (strncmp(tty, _PATH_DEV, strlen(_PATH_DEV)) == 0) 221 1.3 christos tty = tty + strlen(_PATH_DEV); 222 1.3 christos 223 1.3 christos if (*tty == '\0') 224 1.3 christos return PAM_SERVICE_ERR; 225 1.3 christos 226 1.5 christos if (openpam_get_option(pamh, "no_nested") == NULL || nuser == NULL) { 227 1.5 christos 228 1.3 christos #ifdef SUPPORT_UTMPX 229 1.5 christos if (logoutx(tty, 0, DEAD_PROCESS)) 230 1.5 christos logwtmpx(tty, "", "", 0, DEAD_PROCESS); 231 1.5 christos else 232 1.11 christos logit(LOG_NOTICE, "%s(): no utmpx record for %s", 233 1.5 christos __func__, tty); 234 1.3 christos #endif 235 1.3 christos 236 1.3 christos #ifdef SUPPORT_UTMP 237 1.5 christos if (logout(tty)) 238 1.5 christos logwtmp(tty, "", ""); 239 1.5 christos else 240 1.11 christos logit(LOG_NOTICE, "%s(): no utmp record for %s", 241 1.11 christos __func__, tty); 242 1.3 christos #endif 243 1.5 christos } 244 1.3 christos return PAM_SUCCESS; 245 1.3 christos } 246 1.3 christos 247 1.6 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP) 248 1.6 christos static void 249 1.6 christos domsg(pam_handle_t *pamh, time_t t, const char *host, size_t hsize, 250 1.6 christos const char *line, size_t lsize) 251 1.6 christos { 252 1.6 christos char buf[MAXHOSTNAMELEN + 32], *promptresp = NULL; 253 1.6 christos int pam_err; 254 1.6 christos 255 1.6 christos if (*host) { 256 1.6 christos (void)snprintf(buf, sizeof(buf), "from %.*s ", 257 1.6 christos (int)hsize, host); 258 1.6 christos host = buf; 259 1.6 christos } 260 1.6 christos 261 1.6 christos pam_err = pam_prompt(pamh, PAM_TEXT_INFO, &promptresp, 262 1.6 christos "Last login: %.24s %son %.*s\n", ctime(&t), host, (int)lsize, line); 263 1.6 christos 264 1.6 christos if (pam_err == PAM_SUCCESS && promptresp) 265 1.6 christos free(promptresp); 266 1.6 christos } 267 1.6 christos #endif 268 1.6 christos 269 1.3 christos #ifdef SUPPORT_UTMPX 270 1.3 christos static void 271 1.3 christos doutmpx(const char *username, const char *hostname, const char *tty, 272 1.3 christos const struct sockaddr_storage *ss, const struct timeval *now) 273 1.3 christos { 274 1.3 christos struct utmpx utmpx; 275 1.3 christos const char *t; 276 1.3 christos 277 1.3 christos memset((void *)&utmpx, 0, sizeof(utmpx)); 278 1.3 christos utmpx.ut_tv = *now; 279 1.3 christos (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name)); 280 1.3 christos if (hostname) { 281 1.3 christos (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host)); 282 1.3 christos if (ss) 283 1.3 christos utmpx.ut_ss = *ss; 284 1.3 christos } 285 1.3 christos (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line)); 286 1.3 christos utmpx.ut_type = USER_PROCESS; 287 1.3 christos utmpx.ut_pid = getpid(); 288 1.3 christos t = tty + strlen(tty); 289 1.13 lukem if ((size_t)(t - tty) >= sizeof(utmpx.ut_id)) { 290 1.3 christos (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id), 291 1.3 christos sizeof(utmpx.ut_id)); 292 1.3 christos } else { 293 1.3 christos (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id)); 294 1.3 christos } 295 1.3 christos if (pututxline(&utmpx) == NULL) 296 1.14 christos logit(LOG_NOTICE, "Cannot update utmpx: %s", strerror(errno)); 297 1.3 christos endutxent(); 298 1.3 christos if (updwtmpx(_PATH_WTMPX, &utmpx) != 0) 299 1.14 christos logit(LOG_NOTICE, "Cannot update wtmpx: %s", strerror(errno)); 300 1.1 christos } 301 1.1 christos 302 1.3 christos static void 303 1.6 christos dolastlogx(pam_handle_t *pamh, int quiet, const struct passwd *pwd, 304 1.6 christos const char *hostname, const char *tty, const struct sockaddr_storage *ss, 305 1.3 christos const struct timeval *now) 306 1.3 christos { 307 1.3 christos struct lastlogx ll; 308 1.6 christos if (!quiet) { 309 1.6 christos if (getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL) 310 1.6 christos domsg(pamh, (time_t)ll.ll_tv.tv_sec, ll.ll_host, 311 1.6 christos sizeof(ll.ll_host), ll.ll_line, 312 1.6 christos sizeof(ll.ll_line)); 313 1.3 christos } 314 1.3 christos ll.ll_tv = *now; 315 1.3 christos (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 316 1.6 christos 317 1.6 christos if (hostname) 318 1.3 christos (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 319 1.6 christos else 320 1.6 christos (void)memset(ll.ll_host, 0, sizeof(ll.ll_host)); 321 1.6 christos 322 1.6 christos if (ss) 323 1.6 christos ll.ll_ss = *ss; 324 1.6 christos else 325 1.6 christos (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss)); 326 1.6 christos 327 1.3 christos if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0) 328 1.14 christos logit(LOG_NOTICE, "Cannot update lastlogx: %s", strerror(errno)); 329 1.3 christos PAM_LOG("Login recorded in %s", _PATH_LASTLOGX); 330 1.3 christos } 331 1.3 christos #endif 332 1.3 christos 333 1.3 christos #ifdef SUPPORT_UTMP 334 1.3 christos static void 335 1.3 christos doutmp(const char *username, const char *hostname, const char *tty, 336 1.3 christos const struct timeval *now) 337 1.3 christos { 338 1.3 christos struct utmp utmp; 339 1.3 christos 340 1.3 christos (void)memset((void *)&utmp, 0, sizeof(utmp)); 341 1.3 christos utmp.ut_time = now->tv_sec; 342 1.3 christos (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 343 1.3 christos if (hostname) 344 1.3 christos (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 345 1.3 christos (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 346 1.3 christos login(&utmp); 347 1.3 christos } 348 1.3 christos 349 1.3 christos static void 350 1.6 christos dolastlog(pam_handle_t *pamh, int quiet, const struct passwd *pwd, 351 1.6 christos const char *hostname, const char *tty, const struct timeval *now) 352 1.3 christos { 353 1.3 christos struct lastlog ll; 354 1.3 christos int fd; 355 1.3 christos 356 1.6 christos if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) == -1) { 357 1.14 christos logit(LOG_NOTICE, "Cannot open `%s': %s", _PATH_LASTLOG, 358 1.14 christos strerror(errno)); 359 1.6 christos return; 360 1.6 christos } 361 1.6 christos (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET); 362 1.6 christos 363 1.6 christos if (!quiet) { 364 1.6 christos if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 365 1.6 christos ll.ll_time != 0) 366 1.6 christos domsg(pamh, ll.ll_time, ll.ll_host, 367 1.6 christos sizeof(ll.ll_host), ll.ll_line, 368 1.6 christos sizeof(ll.ll_line)); 369 1.3 christos (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET); 370 1.3 christos } 371 1.6 christos 372 1.6 christos ll.ll_time = now->tv_sec; 373 1.6 christos (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 374 1.6 christos 375 1.6 christos if (hostname) 376 1.6 christos (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 377 1.6 christos else 378 1.6 christos (void)memset(ll.ll_host, 0, sizeof(ll.ll_host)); 379 1.6 christos 380 1.6 christos (void)write(fd, &ll, sizeof(ll)); 381 1.6 christos (void)close(fd); 382 1.6 christos 383 1.3 christos PAM_LOG("Login recorded in %s", _PATH_LASTLOG); 384 1.3 christos } 385 1.3 christos #endif 386 1.3 christos 387 1.1 christos PAM_MODULE_ENTRY("pam_lastlog"); 388