1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2002-01-21 16:19:38 +00:00
|
|
|
* Copyright (c) 2002 Networks Associates Technologies, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed for the FreeBSD Project by
|
|
|
|
* ThinkSec AS and NAI Labs, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
|
|
|
|
* ("CBOSS"), as part of the DARPA CHATS research program.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2001-12-10 21:13:08 +00:00
|
|
|
static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
|
1997-07-22 07:39:43 +00:00
|
|
|
#endif
|
2001-12-10 21:13:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* login [ name ]
|
|
|
|
* login -h hostname (for telnetd, etc.)
|
|
|
|
* login -f name (for pre-authenticated login: datakit, xterm, etc.)
|
|
|
|
*/
|
|
|
|
|
2001-12-02 00:06:22 +00:00
|
|
|
#include <sys/copyright.h>
|
2001-12-02 20:54:57 +00:00
|
|
|
#include <sys/param.h>
|
2001-12-02 00:06:22 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2001-12-02 00:06:22 +00:00
|
|
|
#include <sys/wait.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
1998-10-09 06:38:33 +00:00
|
|
|
#include <libutil.h>
|
1998-11-11 05:47:45 +00:00
|
|
|
#include <login_cap.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <ttyent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1998-11-21 02:22:14 +00:00
|
|
|
#include <security/pam_appl.h>
|
2002-03-05 21:56:25 +00:00
|
|
|
#include <security/openpam.h>
|
1994-10-19 00:03:45 +00:00
|
|
|
|
2001-12-01 17:46:46 +00:00
|
|
|
#include "login.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
static int auth_pam(void);
|
|
|
|
static void bail(int, int);
|
|
|
|
static int export(const char *);
|
|
|
|
static void export_pam_environment(void);
|
|
|
|
static int motd(const char *);
|
|
|
|
static void badlogin(char *);
|
|
|
|
static char *getloginname(void);
|
|
|
|
static void pam_syslog(const char *);
|
|
|
|
static void pam_cleanup(void);
|
|
|
|
static void refused(const char *, const char *, int);
|
|
|
|
static const char *stypeof(char *);
|
|
|
|
static void sigint(int);
|
|
|
|
static void timedout(int);
|
|
|
|
static void usage(void);
|
2001-08-30 11:27:36 +00:00
|
|
|
|
2001-12-01 17:46:46 +00:00
|
|
|
#define TTYGRPNAME "tty" /* group to own ttys */
|
|
|
|
#define DEFAULT_BACKOFF 3
|
|
|
|
#define DEFAULT_RETRIES 10
|
2001-05-18 04:55:16 +00:00
|
|
|
#define DEFAULT_PROMPT "login: "
|
|
|
|
#define DEFAULT_PASSWD_PROMPT "Password:"
|
2002-01-30 19:10:21 +00:00
|
|
|
#define TERM_UNKNOWN "su"
|
2001-12-01 17:46:46 +00:00
|
|
|
#define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
|
2002-01-30 19:10:21 +00:00
|
|
|
#define NO_SLEEP_EXIT 0
|
|
|
|
#define SLEEP_EXIT 5
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This bounds the time given to login. Not a define so it can
|
|
|
|
* be patched on machines where it's too small.
|
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
static u_int timeout = 300;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1999-01-03 23:39:33 +00:00
|
|
|
/* Buffer for signal handling of timeout */
|
2002-01-30 19:10:21 +00:00
|
|
|
static jmp_buf timeout_buf;
|
|
|
|
|
|
|
|
struct passwd *pwd;
|
|
|
|
static int failures;
|
|
|
|
|
|
|
|
static char *envinit[1]; /* empty environment list */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command line flags and arguments
|
|
|
|
*/
|
|
|
|
static int fflag; /* -f: do not perform authentication */
|
|
|
|
static int hflag; /* -h: login from remote host */
|
|
|
|
static char *hostname; /* hostname from command line */
|
|
|
|
static int pflag; /* -p: preserve environment */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User name
|
|
|
|
*/
|
|
|
|
static char *username; /* user name */
|
|
|
|
static char *olduser; /* previous user name */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompts
|
|
|
|
*/
|
|
|
|
static char default_prompt[] = DEFAULT_PROMPT;
|
2002-04-08 11:07:51 +00:00
|
|
|
static const char *prompt;
|
2002-01-30 19:10:21 +00:00
|
|
|
static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT;
|
2002-04-08 11:07:51 +00:00
|
|
|
static const char *passwd_prompt;
|
2002-01-30 19:10:21 +00:00
|
|
|
|
|
|
|
static char *tty;
|
1999-01-03 23:39:33 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* PAM data
|
|
|
|
*/
|
|
|
|
static pam_handle_t *pamh = NULL;
|
2002-03-05 21:56:25 +00:00
|
|
|
static struct pam_conv pamc = { openpam_ttyconv, NULL };
|
2002-01-30 19:10:21 +00:00
|
|
|
static int pam_err;
|
|
|
|
static int pam_silent = PAM_SILENT;
|
|
|
|
static int pam_cred_established;
|
|
|
|
static int pam_session_established;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-01-30 19:10:21 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
struct stat st;
|
2002-01-30 19:10:21 +00:00
|
|
|
int retries, backoff;
|
|
|
|
int ask, ch, cnt, quietlog, rootlogin, rval;
|
1998-04-30 16:50:07 +00:00
|
|
|
uid_t uid, euid;
|
1999-04-24 17:26:32 +00:00
|
|
|
gid_t egid;
|
2002-01-30 19:10:21 +00:00
|
|
|
char *term;
|
1999-04-07 14:05:03 +00:00
|
|
|
char *p, *ttyn;
|
1999-01-03 23:39:33 +00:00
|
|
|
char tname[sizeof(_PATH_TTY) + 10];
|
2002-04-08 11:07:51 +00:00
|
|
|
char *arg0;
|
2002-06-28 04:59:39 +00:00
|
|
|
const char *tp;
|
2002-04-08 11:07:51 +00:00
|
|
|
const char *shell = NULL;
|
1997-01-11 07:16:35 +00:00
|
|
|
login_cap_t *lc = NULL;
|
2005-06-01 12:23:06 +00:00
|
|
|
login_cap_t *lc_user = NULL;
|
2001-03-27 19:40:51 +00:00
|
|
|
pid_t pid;
|
2006-02-04 20:20:02 +00:00
|
|
|
char auditsuccess = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
(void)signal(SIGQUIT, SIG_IGN);
|
|
|
|
(void)signal(SIGINT, SIG_IGN);
|
2001-05-21 20:19:59 +00:00
|
|
|
(void)signal(SIGHUP, SIG_IGN);
|
1999-01-03 23:39:33 +00:00
|
|
|
if (setjmp(timeout_buf)) {
|
|
|
|
if (failures)
|
2002-01-30 19:10:21 +00:00
|
|
|
badlogin(username);
|
2001-05-18 05:33:42 +00:00
|
|
|
(void)fprintf(stderr, "Login timed out after %d seconds\n",
|
|
|
|
timeout);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 0);
|
1999-01-03 23:39:33 +00:00
|
|
|
}
|
|
|
|
(void)signal(SIGALRM, timedout);
|
|
|
|
(void)alarm(timeout);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)setpriority(PRIO_PROCESS, 0, 0);
|
|
|
|
|
|
|
|
openlog("login", LOG_ODELAY, LOG_AUTH);
|
|
|
|
|
|
|
|
uid = getuid();
|
1998-04-30 16:48:20 +00:00
|
|
|
euid = geteuid();
|
1999-04-24 17:26:32 +00:00
|
|
|
egid = getegid();
|
2002-01-30 19:10:21 +00:00
|
|
|
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "fh:p")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2002-01-30 19:10:21 +00:00
|
|
|
if (uid != 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
errx(1, "-h option: %s", strerror(EPERM));
|
2002-01-30 19:10:21 +00:00
|
|
|
if (strlen(optarg) >= MAXHOSTNAMELEN)
|
2001-08-12 17:54:49 +00:00
|
|
|
errx(1, "-h option: %s: exceeds maximum "
|
|
|
|
"hostname size", optarg);
|
2002-01-30 19:10:21 +00:00
|
|
|
hflag = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
hostname = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2002-01-30 19:10:21 +00:00
|
|
|
if (uid == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
syslog(LOG_ERR, "invalid flag %c", ch);
|
1997-07-22 07:39:43 +00:00
|
|
|
usage();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
if (argc > 0) {
|
|
|
|
username = strdup(*argv);
|
|
|
|
if (username == NULL)
|
|
|
|
err(1, "strdup()");
|
1994-05-27 12:33:43 +00:00
|
|
|
ask = 0;
|
2002-01-30 19:10:21 +00:00
|
|
|
} else {
|
1994-05-27 12:33:43 +00:00
|
|
|
ask = 1;
|
2002-01-30 19:10:21 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-12-04 15:00:10 +00:00
|
|
|
setproctitle("-%s", getprogname());
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
for (cnt = getdtablesize(); cnt > 2; cnt--)
|
|
|
|
(void)close(cnt);
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Get current TTY
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
ttyn = ttyname(STDIN_FILENO);
|
|
|
|
if (ttyn == NULL || *ttyn == '\0') {
|
|
|
|
(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
|
|
|
|
ttyn = tname;
|
|
|
|
}
|
1997-03-18 16:27:12 +00:00
|
|
|
if ((tty = strrchr(ttyn, '/')) != NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
++tty;
|
|
|
|
else
|
|
|
|
tty = ttyn;
|
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
|
|
|
* Get "login-retries" & "login-backoff" from default class
|
|
|
|
*/
|
|
|
|
lc = login_getclass(NULL);
|
2003-04-25 11:57:20 +00:00
|
|
|
prompt = login_getcapstr(lc, "login_prompt",
|
2002-01-30 19:10:21 +00:00
|
|
|
default_prompt, default_prompt);
|
2001-05-18 04:55:16 +00:00
|
|
|
passwd_prompt = login_getcapstr(lc, "passwd_prompt",
|
2001-12-01 17:46:46 +00:00
|
|
|
default_passwd_prompt, default_passwd_prompt);
|
2002-01-30 19:10:21 +00:00
|
|
|
retries = login_getcapnum(lc, "login-retries",
|
|
|
|
DEFAULT_RETRIES, DEFAULT_RETRIES);
|
|
|
|
backoff = login_getcapnum(lc, "login-backoff",
|
|
|
|
DEFAULT_BACKOFF, DEFAULT_BACKOFF);
|
1997-03-18 16:27:12 +00:00
|
|
|
login_close(lc);
|
|
|
|
lc = NULL;
|
1997-01-11 07:16:35 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Try to authenticate the user until we succeed or time out.
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
for (cnt = 0;; ask = 1) {
|
|
|
|
if (ask) {
|
|
|
|
fflag = 0;
|
2002-01-30 19:10:21 +00:00
|
|
|
if (olduser != NULL)
|
|
|
|
free(olduser);
|
|
|
|
olduser = username;
|
|
|
|
username = getloginname();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
rootlogin = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note if trying multiple user names; log failures for
|
|
|
|
* previous user name, but don't bother logging one failure
|
|
|
|
* for nonexistent name (mistyped username).
|
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
if (failures && strcmp(olduser, username) != 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (failures > (pwd ? 0 : 1))
|
2002-01-30 19:10:21 +00:00
|
|
|
badlogin(olduser);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Load the PAM policy and set some variables
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_start("login", username, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_start()");
|
2006-02-04 20:20:02 +00:00
|
|
|
au_login_fail("PAM Error", 1);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
1994-09-29 18:59:15 +00:00
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_set_item(pamh, PAM_TTY, tty);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_set_item(PAM_TTY)");
|
2006-02-04 20:20:02 +00:00
|
|
|
au_login_fail("PAM Error", 1);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
|
|
|
}
|
2003-02-15 23:20:04 +00:00
|
|
|
pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
|
2002-01-30 19:10:21 +00:00
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_set_item(PAM_RHOST)");
|
2006-02-04 20:20:02 +00:00
|
|
|
au_login_fail("PAM Error", 1);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
|
|
|
}
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
pwd = getpwnam(username);
|
1998-11-21 02:22:14 +00:00
|
|
|
if (pwd != NULL && pwd->pw_uid == 0)
|
|
|
|
rootlogin = 1;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* If the -f option was specified and the caller is
|
|
|
|
* root or the caller isn't changing their uid, don't
|
|
|
|
* authenticate.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
if (pwd != NULL && fflag &&
|
|
|
|
(uid == (uid_t)0 || uid == (uid_t)pwd->pw_uid)) {
|
|
|
|
/* already authenticated */
|
|
|
|
rval = 0;
|
2006-02-04 20:20:02 +00:00
|
|
|
auditsuccess = 0; /* opened a terminal window only */
|
2002-01-30 19:10:21 +00:00
|
|
|
} else {
|
|
|
|
fflag = 0;
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, -4);
|
|
|
|
rval = auth_pam();
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
if (pwd && rval == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pam_cleanup();
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2006-02-04 20:20:02 +00:00
|
|
|
/*
|
|
|
|
* We are not exiting here, but this corresponds to a failed
|
|
|
|
* login event, so set exitstatus to 1.
|
|
|
|
*/
|
|
|
|
au_login_fail("Login incorrect", 1);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)printf("Login incorrect\n");
|
|
|
|
failures++;
|
1997-03-18 16:27:12 +00:00
|
|
|
|
2006-03-28 15:30:42 +00:00
|
|
|
pwd = NULL;
|
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Allow up to 'retry' (10) attempts, but start
|
|
|
|
* backing off after 'backoff' (3) attempts.
|
1997-03-18 16:27:12 +00:00
|
|
|
*/
|
|
|
|
if (++cnt > backoff) {
|
|
|
|
if (cnt >= retries) {
|
1994-05-27 12:33:43 +00:00
|
|
|
badlogin(username);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(SLEEP_EXIT, 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1998-08-17 03:25:07 +00:00
|
|
|
sleep((u_int)((cnt - backoff) * 5));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* committed to login -- turn off timeout */
|
|
|
|
(void)alarm((u_int)0);
|
2001-05-21 20:19:59 +00:00
|
|
|
(void)signal(SIGHUP, SIG_DFL);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
2006-02-04 20:20:02 +00:00
|
|
|
/* Audit successful login. */
|
|
|
|
if (auditsuccess)
|
|
|
|
au_login_success();
|
|
|
|
|
1998-11-21 02:22:14 +00:00
|
|
|
/*
|
|
|
|
* Establish the login class.
|
|
|
|
*/
|
|
|
|
lc = login_getpwclass(pwd);
|
2005-06-01 12:23:06 +00:00
|
|
|
lc_user = login_getuserclass(pwd);
|
1998-11-21 02:22:14 +00:00
|
|
|
|
2005-06-01 12:23:06 +00:00
|
|
|
if (!(quietlog = login_getcapbool(lc_user, "hushlogin", 0)))
|
|
|
|
quietlog = login_getcapbool(lc, "hushlogin", 0);
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2001-09-15 17:09:39 +00:00
|
|
|
/*
|
|
|
|
* Switching needed for NFS with root access disabled.
|
|
|
|
*
|
|
|
|
* XXX: This change fails to modify the additional groups for the
|
|
|
|
* process, and as such, may restrict rights normally granted
|
|
|
|
* through those groups.
|
|
|
|
*/
|
1999-04-24 17:26:32 +00:00
|
|
|
(void)setegid(pwd->pw_gid);
|
1998-04-30 16:48:20 +00:00
|
|
|
(void)seteuid(rootlogin ? 0 : pwd->pw_uid);
|
1997-01-11 07:16:35 +00:00
|
|
|
if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
|
1997-05-23 00:14:27 +00:00
|
|
|
if (login_getcapbool(lc, "requirehome", 0))
|
1997-03-18 16:27:12 +00:00
|
|
|
refused("Home directory not available", "HOMEDIR", 1);
|
2002-05-28 06:46:37 +00:00
|
|
|
if (chdir("/") < 0)
|
1997-03-18 16:27:12 +00:00
|
|
|
refused("Cannot find root directory", "ROOTDIR", 1);
|
|
|
|
if (!quietlog || *pwd->pw_dir)
|
|
|
|
printf("No home directory.\nLogging in with home = \"/\".\n");
|
2001-12-01 17:46:46 +00:00
|
|
|
pwd->pw_dir = strdup("/");
|
|
|
|
if (pwd->pw_dir == NULL) {
|
|
|
|
syslog(LOG_NOTICE, "strdup(): %m");
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(SLEEP_EXIT, 1);
|
2001-12-01 17:46:46 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1998-04-30 16:48:20 +00:00
|
|
|
(void)seteuid(euid);
|
1999-04-24 17:26:32 +00:00
|
|
|
(void)setegid(egid);
|
2004-01-26 20:04:47 +00:00
|
|
|
if (!quietlog) {
|
1997-01-11 07:16:35 +00:00
|
|
|
quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
|
2004-01-26 20:04:47 +00:00
|
|
|
if (!quietlog)
|
|
|
|
pam_silent = 0;
|
|
|
|
}
|
2002-05-28 06:46:37 +00:00
|
|
|
|
|
|
|
shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
|
1997-03-18 16:27:12 +00:00
|
|
|
if (*pwd->pw_shell == '\0')
|
2001-12-01 17:46:46 +00:00
|
|
|
pwd->pw_shell = strdup(_PATH_BSHELL);
|
|
|
|
if (pwd->pw_shell == NULL) {
|
|
|
|
syslog(LOG_NOTICE, "strdup(): %m");
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(SLEEP_EXIT, 1);
|
2001-12-01 17:46:46 +00:00
|
|
|
}
|
1997-03-18 16:27:12 +00:00
|
|
|
if (*shell == '\0') /* Not overridden */
|
|
|
|
shell = pwd->pw_shell;
|
|
|
|
if ((shell = strdup(shell)) == NULL) {
|
2001-08-12 17:54:49 +00:00
|
|
|
syslog(LOG_NOTICE, "strdup(): %m");
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(SLEEP_EXIT, 1);
|
1997-01-11 07:16:35 +00:00
|
|
|
}
|
1997-03-18 16:27:12 +00:00
|
|
|
|
1994-08-22 19:56:14 +00:00
|
|
|
/*
|
|
|
|
* Set device protections, depending on what terminal the
|
|
|
|
* user is logged in. This feature is used on Suns to give
|
|
|
|
* console users better privacy.
|
|
|
|
*/
|
|
|
|
login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
|
|
|
|
|
1999-08-21 18:19:49 +00:00
|
|
|
/*
|
|
|
|
* Clear flags of the tty. None should be set, and when the
|
|
|
|
* user sets them otherwise, this can cause the chown to fail.
|
|
|
|
* Since it isn't clear that flags are useful on character
|
|
|
|
* devices, we just clear them.
|
2002-08-19 20:54:00 +00:00
|
|
|
*
|
|
|
|
* We don't log in the case of EOPNOTSUPP because dev might be
|
|
|
|
* on NFS, which doesn't support chflags.
|
|
|
|
*
|
|
|
|
* We don't log in the EROFS because that means that /dev is on
|
|
|
|
* a read only file system and we assume that the permissions there
|
|
|
|
* are sane.
|
1999-08-21 18:19:49 +00:00
|
|
|
*/
|
2002-08-19 20:54:00 +00:00
|
|
|
if (ttyn != tname && chflags(ttyn, 0))
|
|
|
|
if (errno != EOPNOTSUPP && errno != EROFS)
|
|
|
|
syslog(LOG_ERR, "chflags(%s): %m", ttyn);
|
2002-01-30 19:10:21 +00:00
|
|
|
if (ttyn != tname && chown(ttyn, pwd->pw_uid,
|
1999-08-21 18:19:49 +00:00
|
|
|
(gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
|
2002-08-19 20:54:00 +00:00
|
|
|
if (errno != EROFS)
|
2003-04-26 02:51:03 +00:00
|
|
|
syslog(LOG_ERR, "chown(%s): %m", ttyn);
|
1999-08-21 18:19:49 +00:00
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
|
|
|
* Exclude cons/vt/ptys only, assume dialup otherwise
|
|
|
|
* TODO: Make dialup tty determination a library call
|
|
|
|
* for consistency (finger etc.)
|
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
if (hflag && isdialuptty(tty))
|
1994-05-27 12:33:43 +00:00
|
|
|
syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
|
|
|
|
|
1994-09-29 18:59:15 +00:00
|
|
|
#ifdef LOGALL
|
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Syslog each successful login, so we don't have to watch
|
|
|
|
* hundreds of wtmp or lastlogin files.
|
1994-09-29 18:59:15 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
if (hflag)
|
1997-03-18 16:27:12 +00:00
|
|
|
syslog(LOG_INFO, "login from %s on %s as %s",
|
2002-01-30 19:10:21 +00:00
|
|
|
hostname, tty, pwd->pw_name);
|
1997-03-18 16:27:12 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "login on %s as %s",
|
|
|
|
tty, pwd->pw_name);
|
1997-01-11 07:16:35 +00:00
|
|
|
#endif
|
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* If fflag is on, assume caller/authenticator has logged root
|
|
|
|
* login.
|
1997-03-18 16:27:12 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
if (rootlogin && fflag == 0) {
|
|
|
|
if (hflag)
|
1997-03-18 16:27:12 +00:00
|
|
|
syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
|
2002-01-30 19:10:21 +00:00
|
|
|
username, tty, hostname);
|
1997-03-18 16:27:12 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
|
2001-05-18 05:33:42 +00:00
|
|
|
username, tty);
|
1997-03-18 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Destroy environment unless user has requested its
|
|
|
|
* preservation - but preserve TERM in all cases
|
1997-03-18 16:27:12 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
term = getenv("TERM");
|
1997-01-11 07:16:35 +00:00
|
|
|
if (!pflag)
|
|
|
|
environ = envinit;
|
2002-01-30 19:10:21 +00:00
|
|
|
if (term != NULL)
|
|
|
|
setenv("TERM", term, 0);
|
1997-01-11 07:16:35 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
/*
|
|
|
|
* PAM modules might add supplementary groups during pam_setcred().
|
|
|
|
*/
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
|
2002-05-28 06:46:37 +00:00
|
|
|
syslog(LOG_ERR, "setusercontext() failed - exiting");
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_setcred()");
|
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
|
|
|
}
|
|
|
|
pam_cred_established = 1;
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_open_session(pamh, pam_silent);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_open_session()");
|
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
|
|
|
}
|
|
|
|
pam_session_established = 1;
|
2001-07-28 19:53:10 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* We must fork() before setuid() because we need to call
|
|
|
|
* pam_close_session() as root.
|
|
|
|
*/
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
err(1, "fork");
|
|
|
|
} else if (pid != 0) {
|
2001-03-27 19:40:51 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Parent: wait for child to finish, then clean up
|
|
|
|
* session.
|
2001-03-27 19:40:51 +00:00
|
|
|
*/
|
2003-02-08 16:11:20 +00:00
|
|
|
int status;
|
2002-12-04 15:00:10 +00:00
|
|
|
setproctitle("-%s [pam]", getprogname());
|
2003-02-08 16:11:20 +00:00
|
|
|
waitpid(pid, &status, 0);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 0);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2001-02-09 13:21:50 +00:00
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* NOTICE: We are now in the child process!
|
|
|
|
*/
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Add any environment variables the PAM modules may have set.
|
|
|
|
*/
|
|
|
|
export_pam_environment();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're done with PAM now; our parent will deal with the rest.
|
|
|
|
*/
|
2002-03-05 21:56:25 +00:00
|
|
|
pam_end(pamh, 0);
|
2002-01-30 19:10:21 +00:00
|
|
|
pamh = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need to be root anymore, so set the login name and
|
|
|
|
* the UID.
|
1997-01-11 07:16:35 +00:00
|
|
|
*/
|
1998-11-21 02:22:14 +00:00
|
|
|
if (setlogin(username) != 0) {
|
2002-05-28 06:46:37 +00:00
|
|
|
syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 1);
|
1998-11-21 02:22:14 +00:00
|
|
|
}
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid,
|
2001-03-27 19:40:51 +00:00
|
|
|
LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
|
2002-05-28 06:46:37 +00:00
|
|
|
syslog(LOG_ERR, "setusercontext() failed - exiting");
|
1997-01-11 07:16:35 +00:00
|
|
|
exit(1);
|
1994-09-29 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
1997-02-27 05:09:58 +00:00
|
|
|
(void)setenv("SHELL", pwd->pw_shell, 1);
|
1997-01-11 07:16:35 +00:00
|
|
|
(void)setenv("HOME", pwd->pw_dir, 1);
|
2002-06-28 04:59:39 +00:00
|
|
|
/* Overwrite "term" from login.conf(5) for any known TERM */
|
2002-06-28 22:56:31 +00:00
|
|
|
if (term == NULL && (tp = stypeof(tty)) != NULL)
|
2002-06-28 04:59:39 +00:00
|
|
|
(void)setenv("TERM", tp, 1);
|
|
|
|
else
|
|
|
|
(void)setenv("TERM", TERM_UNKNOWN, 0);
|
1998-11-21 02:22:14 +00:00
|
|
|
(void)setenv("LOGNAME", username, 1);
|
|
|
|
(void)setenv("USER", username, 1);
|
1997-01-11 07:16:35 +00:00
|
|
|
(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
if (!quietlog) {
|
2002-04-08 11:07:51 +00:00
|
|
|
const char *cw;
|
1997-03-18 16:27:12 +00:00
|
|
|
|
|
|
|
cw = login_getcapstr(lc, "copyright", NULL, NULL);
|
2002-01-30 19:10:21 +00:00
|
|
|
if (cw == NULL || motd(cw) == -1)
|
|
|
|
(void)printf("%s", copyright);
|
1997-03-18 16:27:12 +00:00
|
|
|
|
|
|
|
(void)printf("\n");
|
|
|
|
|
1997-01-11 07:16:35 +00:00
|
|
|
cw = login_getcapstr(lc, "welcome", NULL, NULL);
|
2002-01-30 19:10:21 +00:00
|
|
|
if (cw != NULL && access(cw, F_OK) == 0)
|
|
|
|
motd(cw);
|
|
|
|
else
|
|
|
|
motd(_PATH_MOTDFILE);
|
1997-03-18 16:27:12 +00:00
|
|
|
|
2005-06-01 12:23:06 +00:00
|
|
|
if (login_getcapbool(lc_user, "nocheckmail", 0) == 0 &&
|
|
|
|
login_getcapbool(lc, "nocheckmail", 0) == 0) {
|
2002-07-28 16:17:38 +00:00
|
|
|
char *cx;
|
|
|
|
|
2001-11-16 04:39:16 +00:00
|
|
|
/* $MAIL may have been set by class. */
|
2002-07-28 16:17:38 +00:00
|
|
|
cx = getenv("MAIL");
|
|
|
|
if (cx == NULL) {
|
|
|
|
asprintf(&cx, "%s/%s",
|
2001-11-16 04:39:16 +00:00
|
|
|
_PATH_MAILDIR, pwd->pw_name);
|
2002-01-30 19:10:21 +00:00
|
|
|
}
|
2002-07-28 16:17:38 +00:00
|
|
|
if (cx && stat(cx, &st) == 0 && st.st_size != 0)
|
2001-11-16 04:39:16 +00:00
|
|
|
(void)printf("You have %smail.\n",
|
|
|
|
(st.st_mtime > st.st_atime) ? "new " : "");
|
2002-01-30 19:10:21 +00:00
|
|
|
if (getenv("MAIL") == NULL)
|
2002-07-28 16:17:38 +00:00
|
|
|
free(cx);
|
2001-11-16 04:39:16 +00:00
|
|
|
}
|
1994-09-29 18:59:15 +00:00
|
|
|
}
|
1997-01-11 07:16:35 +00:00
|
|
|
|
2005-06-01 12:23:06 +00:00
|
|
|
login_close(lc_user);
|
1997-01-11 07:16:35 +00:00
|
|
|
login_close(lc);
|
1994-09-29 18:59:15 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)signal(SIGALRM, SIG_DFL);
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
(void)signal(SIGINT, SIG_DFL);
|
|
|
|
(void)signal(SIGTSTP, SIG_IGN);
|
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
|
|
|
* Login shells have a leading '-' in front of argv[0]
|
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
p = strrchr(pwd->pw_shell, '/');
|
|
|
|
if (asprintf(&arg0, "-%s", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) {
|
2001-08-12 17:54:49 +00:00
|
|
|
syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
|
|
|
|
username);
|
|
|
|
errx(1, "shell exceeds maximum pathname size");
|
2002-01-30 19:10:21 +00:00
|
|
|
} else if (arg0 == NULL) {
|
|
|
|
err(1, "asprintf()");
|
2001-08-12 17:54:49 +00:00
|
|
|
}
|
1997-03-18 16:27:12 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
execlp(shell, arg0, (char *)0);
|
1997-01-11 07:16:35 +00:00
|
|
|
err(1, "%s", shell);
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* That's it, folks!
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1998-11-21 02:22:14 +00:00
|
|
|
/*
|
|
|
|
* Attempt to authenticate the user using PAM. Returns 0 if the user is
|
|
|
|
* authenticated, or 1 if not authenticated. If some sort of PAM system
|
|
|
|
* error occurs (e.g., the "/etc/pam.conf" file is missing) then this
|
|
|
|
* function returns -1. This can be used as an indication that we should
|
|
|
|
* fall back to a different authentication mechanism.
|
|
|
|
*/
|
|
|
|
static int
|
2002-01-30 19:10:21 +00:00
|
|
|
auth_pam(void)
|
1998-11-21 02:22:14 +00:00
|
|
|
{
|
|
|
|
const char *tmpl_user;
|
|
|
|
const void *item;
|
|
|
|
int rval;
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_authenticate(pamh, pam_silent);
|
|
|
|
switch (pam_err) {
|
1998-11-21 02:22:14 +00:00
|
|
|
|
|
|
|
case PAM_SUCCESS:
|
|
|
|
/*
|
|
|
|
* With PAM we support the concept of a "template"
|
|
|
|
* user. The user enters a login name which is
|
|
|
|
* authenticated by PAM, usually via a remote service
|
|
|
|
* such as RADIUS or TACACS+. If authentication
|
|
|
|
* succeeds, a different but related "template" name
|
|
|
|
* is used for setting the credentials, shell, and
|
|
|
|
* home directory. The name the user enters need only
|
|
|
|
* exist on the remote authentication server, but the
|
|
|
|
* template name must be present in the local password
|
|
|
|
* database.
|
|
|
|
*
|
|
|
|
* This is supported by two various mechanisms in the
|
|
|
|
* individual modules. However, from the application's
|
|
|
|
* point of view, the template user is always passed
|
|
|
|
* back as a changed value of the PAM_USER item.
|
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_get_item(pamh, PAM_USER, &item);
|
|
|
|
if (pam_err == PAM_SUCCESS) {
|
|
|
|
tmpl_user = (const char *)item;
|
1998-11-21 02:22:14 +00:00
|
|
|
if (strcmp(username, tmpl_user) != 0)
|
|
|
|
pwd = getpwnam(tmpl_user);
|
2002-01-30 19:10:21 +00:00
|
|
|
} else {
|
|
|
|
pam_syslog("pam_get_item(PAM_USER)");
|
|
|
|
}
|
1998-11-21 02:22:14 +00:00
|
|
|
rval = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAM_AUTH_ERR:
|
|
|
|
case PAM_USER_UNKNOWN:
|
|
|
|
case PAM_MAXTRIES:
|
|
|
|
rval = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_syslog("pam_authenticate()");
|
1998-11-21 02:22:14 +00:00
|
|
|
rval = -1;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-27 19:40:51 +00:00
|
|
|
|
|
|
|
if (rval == 0) {
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_err = pam_acct_mgmt(pamh, pam_silent);
|
|
|
|
switch (pam_err) {
|
|
|
|
case PAM_SUCCESS:
|
|
|
|
break;
|
|
|
|
case PAM_NEW_AUTHTOK_REQD:
|
|
|
|
pam_err = pam_chauthtok(pamh,
|
|
|
|
pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
pam_syslog("pam_chauthtok()");
|
2001-03-27 19:40:51 +00:00
|
|
|
rval = 1;
|
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pam_syslog("pam_acct_mgmt()");
|
2001-03-27 19:40:51 +00:00
|
|
|
rval = 1;
|
2002-01-30 19:10:21 +00:00
|
|
|
break;
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rval != 0) {
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_end(pamh, pam_err);
|
2001-03-27 19:40:51 +00:00
|
|
|
pamh = NULL;
|
1998-11-21 02:22:14 +00:00
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
return (rval);
|
1998-11-21 02:22:14 +00:00
|
|
|
}
|
2001-02-09 13:21:50 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Export any environment variables PAM modules may have set
|
|
|
|
*/
|
|
|
|
static void
|
2001-12-01 21:12:04 +00:00
|
|
|
export_pam_environment()
|
2001-02-09 13:21:50 +00:00
|
|
|
{
|
2002-01-30 19:10:21 +00:00
|
|
|
char **pam_env;
|
|
|
|
char **pp;
|
|
|
|
|
|
|
|
pam_env = pam_getenvlist(pamh);
|
|
|
|
if (pam_env != NULL) {
|
|
|
|
for (pp = pam_env; *pp != NULL; pp++) {
|
|
|
|
(void)export(*pp);
|
|
|
|
free(*pp);
|
|
|
|
}
|
2001-02-09 13:21:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Perform sanity checks on an environment variable:
|
2001-02-09 13:21:50 +00:00
|
|
|
* - Make sure there is an '=' in the string.
|
|
|
|
* - Make sure the string doesn't run on too long.
|
|
|
|
* - Do not export certain variables. This list was taken from the
|
|
|
|
* Solaris pam_putenv(3) man page.
|
2002-01-30 19:10:21 +00:00
|
|
|
* Then export it.
|
2001-02-09 13:21:50 +00:00
|
|
|
*/
|
|
|
|
static int
|
2002-01-30 19:10:21 +00:00
|
|
|
export(const char *s)
|
2001-02-09 13:21:50 +00:00
|
|
|
{
|
|
|
|
static const char *noexport[] = {
|
|
|
|
"SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
|
|
|
|
"IFS", "PATH", NULL
|
|
|
|
};
|
|
|
|
const char **pp;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (strlen(s) > 1024 || strchr(s, '=') == NULL)
|
2002-01-30 19:10:21 +00:00
|
|
|
return (0);
|
2001-02-09 13:21:50 +00:00
|
|
|
if (strncmp(s, "LD_", 3) == 0)
|
2002-01-30 19:10:21 +00:00
|
|
|
return (0);
|
2001-02-09 13:21:50 +00:00
|
|
|
for (pp = noexport; *pp != NULL; pp++) {
|
|
|
|
n = strlen(*pp);
|
|
|
|
if (s[n] == '=' && strncmp(s, *pp, n) == 0)
|
2002-01-30 19:10:21 +00:00
|
|
|
return (0);
|
2001-02-09 13:21:50 +00:00
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
(void)putenv(s);
|
|
|
|
return (1);
|
2001-02-09 13:21:50 +00:00
|
|
|
}
|
1998-11-21 02:22:14 +00:00
|
|
|
|
1997-07-22 07:39:43 +00:00
|
|
|
static void
|
2001-12-01 21:12:04 +00:00
|
|
|
usage()
|
1997-07-22 07:39:43 +00:00
|
|
|
{
|
2001-05-18 05:46:40 +00:00
|
|
|
|
1997-07-22 07:39:43 +00:00
|
|
|
(void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-01-11 07:16:35 +00:00
|
|
|
|
1997-03-18 16:27:12 +00:00
|
|
|
/*
|
2002-01-30 19:10:21 +00:00
|
|
|
* Prompt user and read login name from stdin.
|
2001-03-27 19:40:51 +00:00
|
|
|
*/
|
2002-01-30 19:10:21 +00:00
|
|
|
static char *
|
2001-12-01 21:12:04 +00:00
|
|
|
getloginname()
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-01-30 19:10:21 +00:00
|
|
|
char *nbuf, *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
int ch;
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
nbuf = malloc(MAXLOGNAME);
|
|
|
|
if (nbuf == NULL)
|
|
|
|
err(1, "malloc()");
|
|
|
|
do {
|
2001-08-12 17:54:49 +00:00
|
|
|
(void)printf("%s", prompt);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (p = nbuf; (ch = getchar()) != '\n'; ) {
|
|
|
|
if (ch == EOF) {
|
|
|
|
badlogin(username);
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(NO_SLEEP_EXIT, 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
if (p < nbuf + MAXLOGNAME - 1)
|
1994-05-27 12:33:43 +00:00
|
|
|
*p++ = ch;
|
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
} while (p == nbuf);
|
2002-05-28 06:46:37 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
*p = '\0';
|
|
|
|
if (nbuf[0] == '-') {
|
|
|
|
pam_silent = 0;
|
|
|
|
memmove(nbuf, nbuf + 1, strlen(nbuf));
|
|
|
|
} else {
|
|
|
|
pam_silent = PAM_SILENT;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-01-30 19:10:21 +00:00
|
|
|
return nbuf;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* SIGINT handler for motd().
|
|
|
|
*/
|
|
|
|
static volatile int motdinterrupt;
|
|
|
|
static void
|
|
|
|
sigint(int signo __unused)
|
1997-01-11 07:16:35 +00:00
|
|
|
{
|
|
|
|
motdinterrupt = 1;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Display the contents of a file (such as /etc/motd).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
motd(const char *motdfile)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
sig_t oldint;
|
2002-01-30 19:10:21 +00:00
|
|
|
FILE *f;
|
|
|
|
int ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
if ((f = fopen(motdfile, "r")) == NULL)
|
|
|
|
return (-1);
|
1997-01-11 07:16:35 +00:00
|
|
|
motdinterrupt = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
oldint = signal(SIGINT, sigint);
|
2002-01-30 19:10:21 +00:00
|
|
|
while ((ch = fgetc(f)) != EOF && !motdinterrupt)
|
|
|
|
putchar(ch);
|
|
|
|
signal(SIGINT, oldint);
|
|
|
|
if (ch != EOF || ferror(f)) {
|
|
|
|
fclose(f);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* SIGALRM handler, to enforce login prompt timeout.
|
|
|
|
*
|
|
|
|
* XXX This can potentially confuse the hell out of PAM. We should
|
|
|
|
* XXX instead implement a conversation function that returns
|
|
|
|
* XXX PAM_CONV_ERR when interrupted by a signal, and have the signal
|
|
|
|
* XXX handler just set a flag.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
timedout(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-18 05:33:42 +00:00
|
|
|
|
1999-01-03 23:39:33 +00:00
|
|
|
longjmp(timeout_buf, signo);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:24:31 +00:00
|
|
|
static void
|
2002-01-30 19:10:21 +00:00
|
|
|
badlogin(char *name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (failures == 0)
|
|
|
|
return;
|
2002-01-30 19:10:21 +00:00
|
|
|
if (hflag) {
|
1994-05-27 12:33:43 +00:00
|
|
|
syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
|
2002-01-30 19:10:21 +00:00
|
|
|
failures, failures > 1 ? "S" : "", hostname);
|
1994-05-27 12:33:43 +00:00
|
|
|
syslog(LOG_AUTHPRIV|LOG_NOTICE,
|
|
|
|
"%d LOGIN FAILURE%s FROM %s, %s",
|
2002-01-30 19:10:21 +00:00
|
|
|
failures, failures > 1 ? "S" : "", hostname, name);
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
|
|
|
syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
|
|
|
|
failures, failures > 1 ? "S" : "", tty);
|
|
|
|
syslog(LOG_AUTHPRIV|LOG_NOTICE,
|
|
|
|
"%d LOGIN FAILURE%s ON %s, %s",
|
|
|
|
failures, failures > 1 ? "S" : "", tty, name);
|
|
|
|
}
|
1999-01-03 23:39:33 +00:00
|
|
|
failures = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2001-12-01 17:46:46 +00:00
|
|
|
const char *
|
2002-01-30 19:10:21 +00:00
|
|
|
stypeof(char *ttyid)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct ttyent *t;
|
1997-03-18 16:27:12 +00:00
|
|
|
|
2001-08-12 17:54:49 +00:00
|
|
|
if (ttyid != NULL && *ttyid != '\0') {
|
|
|
|
t = getttynam(ttyid);
|
|
|
|
if (t != NULL && t->ty_type != NULL)
|
|
|
|
return (t->ty_type);
|
|
|
|
}
|
2002-06-28 04:59:39 +00:00
|
|
|
return (NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-10-15 18:24:31 +00:00
|
|
|
static void
|
2002-01-30 19:10:21 +00:00
|
|
|
refused(const char *msg, const char *rtype, int lout)
|
1997-03-18 16:27:12 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (msg != NULL)
|
|
|
|
printf("%s.\n", msg);
|
2002-01-30 19:10:21 +00:00
|
|
|
if (hflag)
|
1997-03-18 16:27:12 +00:00
|
|
|
syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
|
2002-01-30 19:10:21 +00:00
|
|
|
pwd->pw_name, rtype, hostname, tty);
|
1997-03-18 16:27:12 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
|
2001-05-18 05:33:42 +00:00
|
|
|
pwd->pw_name, rtype, tty);
|
1997-03-18 16:27:12 +00:00
|
|
|
if (lout)
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(SLEEP_EXIT, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log a PAM error
|
|
|
|
*/
|
2002-10-15 18:24:31 +00:00
|
|
|
static void
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_syslog(const char *msg)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err));
|
1997-03-18 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
/*
|
|
|
|
* Shut down PAM
|
|
|
|
*/
|
2002-10-15 18:24:31 +00:00
|
|
|
static void
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_cleanup()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (pamh != NULL) {
|
|
|
|
if (pam_session_established) {
|
|
|
|
pam_err = pam_close_session(pamh, 0);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
pam_syslog("pam_close_session()");
|
|
|
|
}
|
|
|
|
pam_session_established = 0;
|
|
|
|
if (pam_cred_established) {
|
|
|
|
pam_err = pam_setcred(pamh, pam_silent|PAM_DELETE_CRED);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
pam_syslog("pam_setcred()");
|
|
|
|
}
|
|
|
|
pam_cred_established = 0;
|
|
|
|
pam_end(pamh, pam_err);
|
|
|
|
pamh = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit, optionally after sleeping a few seconds
|
|
|
|
*/
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-01-30 19:10:21 +00:00
|
|
|
bail(int sec, int eval)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-03-18 16:27:12 +00:00
|
|
|
|
2002-01-30 19:10:21 +00:00
|
|
|
pam_cleanup();
|
2006-03-28 15:30:42 +00:00
|
|
|
if (pwd != NULL)
|
|
|
|
audit_logout();
|
2002-01-30 19:10:21 +00:00
|
|
|
(void)sleep(sec);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(eval);
|
|
|
|
}
|