116e058b5SDag-Erling Smørgrav /*- 216e058b5SDag-Erling Smørgrav * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994 316e058b5SDag-Erling Smørgrav * The Regents of the University of California. All rights reserved. 416e058b5SDag-Erling Smørgrav * Copyright (c) 2001 Mark R V Murray 516e058b5SDag-Erling Smørgrav * All rights reserved. 616e058b5SDag-Erling Smørgrav * Copyright (c) 2001 Networks Associates Technologies, Inc. 716e058b5SDag-Erling Smørgrav * All rights reserved. 816e058b5SDag-Erling Smørgrav * 916e058b5SDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by 1016e058b5SDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network 1116e058b5SDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 1216e058b5SDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program. 1316e058b5SDag-Erling Smørgrav * 1416e058b5SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 1516e058b5SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 1616e058b5SDag-Erling Smørgrav * are met: 1716e058b5SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 1816e058b5SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 1916e058b5SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 2016e058b5SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 2116e058b5SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 2216e058b5SDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote 2316e058b5SDag-Erling Smørgrav * products derived from this software without specific prior written 2416e058b5SDag-Erling Smørgrav * permission. 2516e058b5SDag-Erling Smørgrav * 4. Neither the name of the University nor the names of its contributors 2616e058b5SDag-Erling Smørgrav * may be used to endorse or promote products derived from this software 2716e058b5SDag-Erling Smørgrav * without specific prior written permission. 2816e058b5SDag-Erling Smørgrav * 2916e058b5SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 3016e058b5SDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3116e058b5SDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3216e058b5SDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 3316e058b5SDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3416e058b5SDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3516e058b5SDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3616e058b5SDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3716e058b5SDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3816e058b5SDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3916e058b5SDag-Erling Smørgrav * SUCH DAMAGE. 4016e058b5SDag-Erling Smørgrav */ 4116e058b5SDag-Erling Smørgrav 4216e058b5SDag-Erling Smørgrav #include <sys/cdefs.h> 4316e058b5SDag-Erling Smørgrav __FBSDID("$FreeBSD$"); 4416e058b5SDag-Erling Smørgrav 4516e058b5SDag-Erling Smørgrav #define _BSD_SOURCE 4616e058b5SDag-Erling Smørgrav 4716e058b5SDag-Erling Smørgrav #include <sys/param.h> 4816e058b5SDag-Erling Smørgrav 4916e058b5SDag-Erling Smørgrav #include <fcntl.h> 5016e058b5SDag-Erling Smørgrav #include <libutil.h> 5116e058b5SDag-Erling Smørgrav #include <pwd.h> 5216e058b5SDag-Erling Smørgrav #include <stdio.h> 5316e058b5SDag-Erling Smørgrav #include <stdlib.h> 5416e058b5SDag-Erling Smørgrav #include <string.h> 5516e058b5SDag-Erling Smørgrav #include <syslog.h> 5616e058b5SDag-Erling Smørgrav #include <time.h> 5716e058b5SDag-Erling Smørgrav #include <unistd.h> 5816e058b5SDag-Erling Smørgrav #include <utmp.h> 5916e058b5SDag-Erling Smørgrav 6016e058b5SDag-Erling Smørgrav #define PAM_SM_AUTH 6116e058b5SDag-Erling Smørgrav #define PAM_SM_ACCOUNT 6216e058b5SDag-Erling Smørgrav #define PAM_SM_SESSION 6316e058b5SDag-Erling Smørgrav #define PAM_SM_PASSWORD 6416e058b5SDag-Erling Smørgrav 6516e058b5SDag-Erling Smørgrav #include <security/pam_modules.h> 6616e058b5SDag-Erling Smørgrav #include <pam_mod_misc.h> 6716e058b5SDag-Erling Smørgrav 6816e058b5SDag-Erling Smørgrav extern int login_access(const char *, const char *); 6916e058b5SDag-Erling Smørgrav 7016e058b5SDag-Erling Smørgrav PAM_EXTERN int 7116e058b5SDag-Erling Smørgrav pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) 7216e058b5SDag-Erling Smørgrav { 7316e058b5SDag-Erling Smørgrav struct options options; 7416e058b5SDag-Erling Smørgrav 7516e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 7616e058b5SDag-Erling Smørgrav 7716e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 7816e058b5SDag-Erling Smørgrav 7916e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_IGNORE); 8016e058b5SDag-Erling Smørgrav } 8116e058b5SDag-Erling Smørgrav 8216e058b5SDag-Erling Smørgrav PAM_EXTERN int 8316e058b5SDag-Erling Smørgrav pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) 8416e058b5SDag-Erling Smørgrav { 8516e058b5SDag-Erling Smørgrav struct options options; 8616e058b5SDag-Erling Smørgrav 8716e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 8816e058b5SDag-Erling Smørgrav 8916e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 9016e058b5SDag-Erling Smørgrav 9116e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_IGNORE); 9216e058b5SDag-Erling Smørgrav } 9316e058b5SDag-Erling Smørgrav 9416e058b5SDag-Erling Smørgrav PAM_EXTERN int 9516e058b5SDag-Erling Smørgrav pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc ,const char **argv) 9616e058b5SDag-Erling Smørgrav { 9716e058b5SDag-Erling Smørgrav struct options options; 9816e058b5SDag-Erling Smørgrav 9916e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 10016e058b5SDag-Erling Smørgrav 10116e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 10216e058b5SDag-Erling Smørgrav 10316e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_IGNORE); 10416e058b5SDag-Erling Smørgrav } 10516e058b5SDag-Erling Smørgrav 10616e058b5SDag-Erling Smørgrav PAM_EXTERN int 10716e058b5SDag-Erling Smørgrav pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv) 10816e058b5SDag-Erling Smørgrav { 10916e058b5SDag-Erling Smørgrav struct options options; 11016e058b5SDag-Erling Smørgrav 11116e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 11216e058b5SDag-Erling Smørgrav 11316e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 11416e058b5SDag-Erling Smørgrav 11516e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_IGNORE); 11616e058b5SDag-Erling Smørgrav } 11716e058b5SDag-Erling Smørgrav 11816e058b5SDag-Erling Smørgrav PAM_EXTERN int 11916e058b5SDag-Erling Smørgrav pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) 12016e058b5SDag-Erling Smørgrav { 12116e058b5SDag-Erling Smørgrav struct options options; 12216e058b5SDag-Erling Smørgrav struct passwd *pwd; 12316e058b5SDag-Erling Smørgrav struct utmp utmp; 12416e058b5SDag-Erling Smørgrav struct lastlog ll; 12516e058b5SDag-Erling Smørgrav const char *rhost, *user, *tty; 12616e058b5SDag-Erling Smørgrav char *buf; 12716e058b5SDag-Erling Smørgrav off_t llpos; 12816e058b5SDag-Erling Smørgrav int fd, pam_err; 12916e058b5SDag-Erling Smørgrav 13016e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 13116e058b5SDag-Erling Smørgrav 13216e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 13316e058b5SDag-Erling Smørgrav 13416e058b5SDag-Erling Smørgrav pam_err = pam_get_item(pamh, PAM_USER, (const void **)&user); 13516e058b5SDag-Erling Smørgrav if (pam_err != PAM_SUCCESS) 13616e058b5SDag-Erling Smørgrav PAM_RETURN(pam_err); 13716e058b5SDag-Erling Smørgrav if (user == NULL || (pwd = getpwnam(user)) == NULL) 13816e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_SERVICE_ERR); 13916e058b5SDag-Erling Smørgrav PAM_LOG("Got user: %s", user); 14016e058b5SDag-Erling Smørgrav 14116e058b5SDag-Erling Smørgrav pam_err = pam_get_item(pamh, PAM_RHOST, (const void **)&rhost); 14216e058b5SDag-Erling Smørgrav if (pam_err != PAM_SUCCESS) 14316e058b5SDag-Erling Smørgrav PAM_RETURN(pam_err); 14416e058b5SDag-Erling Smørgrav 14516e058b5SDag-Erling Smørgrav pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty); 14616e058b5SDag-Erling Smørgrav if (pam_err != PAM_SUCCESS) 14716e058b5SDag-Erling Smørgrav PAM_RETURN(pam_err); 14816e058b5SDag-Erling Smørgrav if (tty == NULL) 14916e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_SERVICE_ERR); 15016e058b5SDag-Erling Smørgrav 15116e058b5SDag-Erling Smørgrav fd = open(_PATH_LASTLOG, O_RDWR, 0); 15216e058b5SDag-Erling Smørgrav if (fd == -1) { 15316e058b5SDag-Erling Smørgrav syslog(LOG_ERR, "cannot open %s: %m", _PATH_LASTLOG); 15416e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_SERVICE_ERR); 15516e058b5SDag-Erling Smørgrav } 15616e058b5SDag-Erling Smørgrav 15716e058b5SDag-Erling Smørgrav /* 15816e058b5SDag-Erling Smørgrav * Record session in utmp(5) and wtmp(5). 15916e058b5SDag-Erling Smørgrav */ 16016e058b5SDag-Erling Smørgrav bzero(&utmp, sizeof(utmp)); 16116e058b5SDag-Erling Smørgrav time(&utmp.ut_time); 16216e058b5SDag-Erling Smørgrav /* note: does not need to be NUL-terminated */ 16316e058b5SDag-Erling Smørgrav strncpy(utmp.ut_name, user, sizeof(utmp.ut_name)); 16416e058b5SDag-Erling Smørgrav if (rhost) 16516e058b5SDag-Erling Smørgrav strncpy(utmp.ut_host, rhost, sizeof(utmp.ut_host)); 16616e058b5SDag-Erling Smørgrav (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 16716e058b5SDag-Erling Smørgrav login(&utmp); 16816e058b5SDag-Erling Smørgrav 16916e058b5SDag-Erling Smørgrav /* 17016e058b5SDag-Erling Smørgrav * Record session in lastlog(5). 17116e058b5SDag-Erling Smørgrav */ 17216e058b5SDag-Erling Smørgrav llpos = (off_t)(pwd->pw_uid * sizeof(ll)); 17316e058b5SDag-Erling Smørgrav if (lseek(fd, llpos, L_SET) != llpos) 17416e058b5SDag-Erling Smørgrav goto file_err; 17516e058b5SDag-Erling Smørgrav if ((flags & PAM_SILENT) == 0) { 17616e058b5SDag-Erling Smørgrav if (read(fd, &ll, sizeof(ll)) == sizeof(ll) && 17716e058b5SDag-Erling Smørgrav ll.ll_time != 0) { 17816e058b5SDag-Erling Smørgrav asprintf(&buf, "Last login: %.*s ", 24 - 5, 17916e058b5SDag-Erling Smørgrav ctime(&ll.ll_time)); 18016e058b5SDag-Erling Smørgrav if (buf != NULL) { 18116e058b5SDag-Erling Smørgrav pam_prompt(pamh, PAM_TEXT_INFO, buf, NULL); 18216e058b5SDag-Erling Smørgrav free(buf); 18316e058b5SDag-Erling Smørgrav } 18416e058b5SDag-Erling Smørgrav if (*ll.ll_host != '\0') 18516e058b5SDag-Erling Smørgrav asprintf(&buf, "from %.*s\n", 18616e058b5SDag-Erling Smørgrav (int)sizeof(ll.ll_host), ll.ll_host); 18716e058b5SDag-Erling Smørgrav else 18816e058b5SDag-Erling Smørgrav asprintf(&buf, "on %.*s\n", 18916e058b5SDag-Erling Smørgrav (int)sizeof(ll.ll_line), ll.ll_line); 19016e058b5SDag-Erling Smørgrav if (buf != NULL) { 19116e058b5SDag-Erling Smørgrav pam_prompt(pamh, PAM_TEXT_INFO, buf, NULL); 19216e058b5SDag-Erling Smørgrav free(buf); 19316e058b5SDag-Erling Smørgrav } 19416e058b5SDag-Erling Smørgrav } 19516e058b5SDag-Erling Smørgrav if (lseek(fd, llpos, L_SET) != llpos) 19616e058b5SDag-Erling Smørgrav goto file_err; 19716e058b5SDag-Erling Smørgrav } 19816e058b5SDag-Erling Smørgrav 19916e058b5SDag-Erling Smørgrav bzero(&ll, sizeof(ll)); 20016e058b5SDag-Erling Smørgrav time(&ll.ll_time); 20116e058b5SDag-Erling Smørgrav 20216e058b5SDag-Erling Smørgrav /* note: does not need to be NUL-terminated */ 20316e058b5SDag-Erling Smørgrav strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 20416e058b5SDag-Erling Smørgrav if (rhost) 20516e058b5SDag-Erling Smørgrav /* note: does not need to be NUL-terminated */ 20616e058b5SDag-Erling Smørgrav strncpy(ll.ll_host, rhost, sizeof(ll.ll_host)); 20716e058b5SDag-Erling Smørgrav 20816e058b5SDag-Erling Smørgrav if (write(fd, (char *)&ll, sizeof(ll)) != sizeof(ll) || close(fd) != 0) 20916e058b5SDag-Erling Smørgrav goto file_err; 21016e058b5SDag-Erling Smørgrav 21116e058b5SDag-Erling Smørgrav PAM_LOG("Login recorded in %s", _PATH_LASTLOG); 21216e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_IGNORE); 21316e058b5SDag-Erling Smørgrav 21416e058b5SDag-Erling Smørgrav file_err: 21516e058b5SDag-Erling Smørgrav syslog(LOG_ERR, "%s: %m", _PATH_LASTLOG); 21616e058b5SDag-Erling Smørgrav close(fd); 21716e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_SERVICE_ERR); 21816e058b5SDag-Erling Smørgrav } 21916e058b5SDag-Erling Smørgrav 22016e058b5SDag-Erling Smørgrav PAM_EXTERN int 22116e058b5SDag-Erling Smørgrav pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) 22216e058b5SDag-Erling Smørgrav { 22316e058b5SDag-Erling Smørgrav struct options options; 22416e058b5SDag-Erling Smørgrav const char *tty; 22516e058b5SDag-Erling Smørgrav int pam_err; 22616e058b5SDag-Erling Smørgrav 22716e058b5SDag-Erling Smørgrav pam_std_option(&options, NULL, argc, argv); 22816e058b5SDag-Erling Smørgrav 22916e058b5SDag-Erling Smørgrav PAM_LOG("Options processed"); 23016e058b5SDag-Erling Smørgrav 23116e058b5SDag-Erling Smørgrav pam_err = pam_get_item(pamh, PAM_TTY, (const void **)&tty); 23216e058b5SDag-Erling Smørgrav if (pam_err != PAM_SUCCESS) 23316e058b5SDag-Erling Smørgrav PAM_RETURN(pam_err); 23416e058b5SDag-Erling Smørgrav if (tty != NULL) 23516e058b5SDag-Erling Smørgrav logout(tty); 23616e058b5SDag-Erling Smørgrav 23716e058b5SDag-Erling Smørgrav PAM_RETURN(PAM_SUCCESS); 23816e058b5SDag-Erling Smørgrav } 23916e058b5SDag-Erling Smørgrav 24016e058b5SDag-Erling Smørgrav PAM_MODULE_ENTRY("pam_lastlog"); 241