2009-12-29 22:26:13 +03:00
|
|
|
/* $NetBSD: login_pam.c,v 1.20 2009/12/29 19:26:13 christos Exp $ */
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
2005-01-23 12:47:43 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
|
|
|
|
#endif
|
2009-12-29 22:26:13 +03:00
|
|
|
__RCSID("$NetBSD: login_pam.c,v 1.20 2009/12/29 19:26:13 christos Exp $");
|
2005-01-23 12:47:43 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* login [ name ]
|
|
|
|
* login -h hostname (for telnetd, etc.)
|
|
|
|
* login -f name (for pre-authenticated login: datakit, xterm, etc.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ttyent.h>
|
|
|
|
#include <tzfile.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <login_cap.h>
|
|
|
|
#include <vis.h>
|
|
|
|
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/openpam.h>
|
|
|
|
|
|
|
|
#include "pathnames.h"
|
2009-12-29 22:26:13 +03:00
|
|
|
#include "common.h"
|
2005-01-23 12:47:43 +03:00
|
|
|
|
2009-12-29 22:26:13 +03:00
|
|
|
#if 0
|
|
|
|
static int rootterm(char *);
|
|
|
|
#endif
|
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
static struct pam_conv pamc = { openpam_ttyconv, NULL };
|
|
|
|
|
|
|
|
#define TTYGRPNAME "tty" /* name of group to own ttys */
|
|
|
|
|
|
|
|
#define DEFAULT_BACKOFF 3
|
|
|
|
#define DEFAULT_RETRIES 10
|
|
|
|
|
2009-12-29 22:26:13 +03:00
|
|
|
static struct passwd pwres;
|
|
|
|
static char pwbuf[1024];
|
|
|
|
static struct group grs, *grp;
|
|
|
|
static char grbuf[1024];
|
|
|
|
extern char **environ;
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct stat st;
|
2005-11-01 18:47:43 +03:00
|
|
|
int ask, ch, cnt, fflag, pflag, quietlog, rootlogin;
|
2005-01-23 12:47:43 +03:00
|
|
|
int auth_passed;
|
|
|
|
uid_t uid, saved_uid;
|
|
|
|
gid_t saved_gid, saved_gids[NGROUPS_MAX];
|
|
|
|
int nsaved_gids;
|
|
|
|
char *domain, *p, *ttyn, *pwprompt;
|
|
|
|
char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
|
|
|
|
char localhost[MAXHOSTNAMELEN + 1];
|
|
|
|
int need_chpass, require_chpass;
|
|
|
|
int login_retries = DEFAULT_RETRIES,
|
|
|
|
login_backoff = DEFAULT_BACKOFF;
|
|
|
|
char *shell = NULL;
|
|
|
|
login_cap_t *lc = NULL;
|
|
|
|
pam_handle_t *pamh = NULL;
|
|
|
|
int pam_err;
|
2006-04-17 20:29:44 +04:00
|
|
|
sig_t oint, oabrt, oquit, oalrm;
|
2005-01-23 12:47:43 +03:00
|
|
|
const void *newuser;
|
|
|
|
int pam_silent = PAM_SILENT;
|
|
|
|
pid_t xpid, pid;
|
|
|
|
int status;
|
|
|
|
char *saved_term;
|
|
|
|
char **pamenv;
|
|
|
|
|
|
|
|
tbuf[0] = '\0';
|
|
|
|
pwprompt = NULL;
|
|
|
|
nested = NULL;
|
|
|
|
need_chpass = require_chpass = 0;
|
|
|
|
|
2006-04-17 20:29:44 +04:00
|
|
|
oabrt = signal(SIGABRT, SIG_IGN);
|
|
|
|
oalrm = signal(SIGALRM, timedout);
|
|
|
|
oint = signal(SIGINT, SIG_IGN);
|
|
|
|
oquit = signal(SIGQUIT, SIG_IGN);
|
|
|
|
|
2005-01-23 12:47:43 +03:00
|
|
|
(void)alarm(timeout);
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, 0);
|
|
|
|
|
|
|
|
openlog("login", 0, LOG_AUTH);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* -p is used by getty to tell login not to destroy the environment
|
|
|
|
* -f is used to skip a second login authentication
|
|
|
|
* -h is used by other servers to pass the name of the remote host to
|
|
|
|
* login so that it may be placed in utmp/utmpx and wtmp/wtmpx
|
|
|
|
* -a in addition to -h, a server my supply -a to pass the actual
|
|
|
|
* server address.
|
|
|
|
*/
|
|
|
|
domain = NULL;
|
|
|
|
if (gethostname(localhost, sizeof(localhost)) < 0)
|
|
|
|
syslog(LOG_ERR, "couldn't get local hostname: %m");
|
|
|
|
else
|
|
|
|
domain = strchr(localhost, '.');
|
|
|
|
localhost[sizeof(localhost) - 1] = '\0';
|
|
|
|
|
2005-11-01 18:47:43 +03:00
|
|
|
fflag = pflag = 0;
|
2005-01-23 12:47:43 +03:00
|
|
|
have_ss = 0;
|
|
|
|
uid = getuid();
|
2005-11-01 18:47:43 +03:00
|
|
|
while ((ch = getopt(argc, argv, "a:fh:p")) != -1)
|
2005-01-23 12:47:43 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
2005-03-30 05:30:21 +04:00
|
|
|
if (uid) {
|
|
|
|
errno = EPERM;
|
|
|
|
err(EXIT_FAILURE, "-a option");
|
|
|
|
}
|
2005-01-23 12:47:43 +03:00
|
|
|
decode_ss(optarg);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2005-03-30 05:30:21 +04:00
|
|
|
if (uid) {
|
|
|
|
errno = EPERM;
|
|
|
|
err(EXIT_FAILURE, "-h option");
|
|
|
|
}
|
2005-01-23 12:47:43 +03:00
|
|
|
if (domain && (p = strchr(optarg, '.')) != NULL &&
|
|
|
|
strcasecmp(p, domain) == 0)
|
|
|
|
*p = '\0';
|
|
|
|
hostname = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
2005-02-28 19:11:36 +03:00
|
|
|
|
|
|
|
setproctitle(NULL);
|
2005-01-23 12:47:43 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
username = *argv;
|
|
|
|
ask = 0;
|
|
|
|
} else
|
|
|
|
ask = 1;
|
|
|
|
|
2005-02-28 19:11:36 +03:00
|
|
|
#ifdef F_CLOSEM
|
|
|
|
(void)fcntl(3, F_CLOSEM, 0);
|
|
|
|
#else
|
2005-01-23 12:47:43 +03:00
|
|
|
for (cnt = getdtablesize(); cnt > 2; cnt--)
|
|
|
|
(void)close(cnt);
|
2005-02-28 19:11:36 +03:00
|
|
|
#endif
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
ttyn = ttyname(STDIN_FILENO);
|
|
|
|
if (ttyn == NULL || *ttyn == '\0') {
|
|
|
|
(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
|
|
|
|
ttyn = tname;
|
|
|
|
}
|
2005-02-28 19:11:36 +03:00
|
|
|
if ((tty = strstr(ttyn, "/pts/")) != NULL)
|
|
|
|
++tty;
|
|
|
|
else if ((tty = strrchr(ttyn, '/')) != NULL)
|
2005-01-23 12:47:43 +03:00
|
|
|
++tty;
|
|
|
|
else
|
|
|
|
tty = ttyn;
|
|
|
|
|
|
|
|
if (issetugid()) {
|
|
|
|
nested = strdup(user_from_uid(getuid(), 0));
|
|
|
|
if (nested == NULL) {
|
|
|
|
syslog(LOG_ERR, "strdup: %m");
|
2005-03-30 05:30:21 +04:00
|
|
|
sleepexit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get "login-retries" and "login-backoff" from default class */
|
|
|
|
if ((lc = login_getclass(NULL)) != NULL) {
|
|
|
|
login_retries = (int)login_getcapnum(lc, "login-retries",
|
|
|
|
DEFAULT_RETRIES, DEFAULT_RETRIES);
|
|
|
|
login_backoff = (int)login_getcapnum(lc, "login-backoff",
|
|
|
|
DEFAULT_BACKOFF, DEFAULT_BACKOFF);
|
|
|
|
login_close(lc);
|
|
|
|
lc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (cnt = 0;; ask = 1) {
|
|
|
|
if (ask) {
|
|
|
|
fflag = 0;
|
|
|
|
getloginname();
|
|
|
|
}
|
|
|
|
rootlogin = 0;
|
|
|
|
auth_passed = 0;
|
|
|
|
if (strlen(username) > MAXLOGNAME)
|
|
|
|
username[MAXLOGNAME] = '\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).
|
|
|
|
*/
|
|
|
|
if (failures && strcmp(tbuf, username)) {
|
|
|
|
if (failures > (pwd ? 0 : 1))
|
|
|
|
badlogin(tbuf);
|
|
|
|
failures = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PAM_END(msg) do { \
|
|
|
|
syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err)); \
|
|
|
|
warnx("%s: %s", msg, pam_strerror(pamh, pam_err)); \
|
|
|
|
pam_end(pamh, pam_err); \
|
2005-03-30 05:30:21 +04:00
|
|
|
sleepexit(EXIT_FAILURE); \
|
2005-01-23 12:47:43 +03:00
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
pam_err = pam_start("login", username, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
if (pamh != NULL)
|
|
|
|
PAM_END("pam_start");
|
|
|
|
/* Things went really bad... */
|
|
|
|
syslog(LOG_ERR, "pam_start failed: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
2005-03-30 05:30:21 +04:00
|
|
|
errx(EXIT_FAILURE, "pam_start failed");
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PAM_SET_ITEM(item, var) do { \
|
|
|
|
pam_err = pam_set_item(pamh, (item), (var)); \
|
|
|
|
if (pam_err != PAM_SUCCESS) \
|
|
|
|
PAM_END("pam_set_item(" # item ")"); \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
/*
|
2005-03-03 05:06:16 +03:00
|
|
|
* Fill hostname tty, and nested user
|
2005-01-23 12:47:43 +03:00
|
|
|
*/
|
|
|
|
PAM_SET_ITEM(PAM_RHOST, hostname);
|
|
|
|
PAM_SET_ITEM(PAM_TTY, tty);
|
2005-03-03 05:06:16 +03:00
|
|
|
if (nested)
|
|
|
|
PAM_SET_ITEM(PAM_NUSER, nested);
|
2005-02-01 20:57:16 +03:00
|
|
|
if (have_ss)
|
|
|
|
PAM_SET_ITEM(PAM_SOCKADDR, &ss);
|
2005-01-23 12:47:43 +03:00
|
|
|
|
2005-04-03 10:56:39 +04:00
|
|
|
/*
|
|
|
|
* Don't check for errors, because we don't want to give
|
|
|
|
* out any information.
|
|
|
|
*/
|
2005-04-19 07:40:58 +04:00
|
|
|
pwd = NULL;
|
2005-04-03 10:56:39 +04:00
|
|
|
(void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf), &pwd);
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Establish the class now, before we might goto
|
|
|
|
* within the next block. pwd can be NULL since it
|
|
|
|
* falls back to the "default" class if it is.
|
|
|
|
*/
|
|
|
|
lc = login_getclass(pwd ? pwd->pw_class : NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we have a valid account name, and it doesn't have a
|
|
|
|
* password, or the -f option was specified and the caller
|
|
|
|
* is root or the caller isn't changing their uid, don't
|
|
|
|
* authenticate.
|
|
|
|
*/
|
|
|
|
if (pwd) {
|
|
|
|
if (pwd->pw_uid == 0)
|
|
|
|
rootlogin = 1;
|
|
|
|
|
|
|
|
if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
|
|
|
|
/* already authenticated */
|
|
|
|
auth_passed = 1;
|
|
|
|
goto skip_auth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, -4);
|
|
|
|
|
|
|
|
switch(pam_err = pam_authenticate(pamh, pam_silent)) {
|
|
|
|
case PAM_SUCCESS:
|
|
|
|
/*
|
|
|
|
* PAM can change the user, refresh
|
|
|
|
* username, pwd, and lc.
|
|
|
|
*/
|
|
|
|
pam_err = pam_get_item(pamh, PAM_USER, &newuser);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
PAM_END("pam_get_item(PAM_USER)");
|
|
|
|
|
|
|
|
username = (char *)newuser;
|
2005-04-03 10:56:39 +04:00
|
|
|
/*
|
|
|
|
* Don't check for errors, because we don't want to give
|
|
|
|
* out any information.
|
|
|
|
*/
|
2005-04-19 07:40:58 +04:00
|
|
|
pwd = NULL;
|
2005-04-03 10:56:39 +04:00
|
|
|
(void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf),
|
|
|
|
&pwd);
|
2005-01-23 12:47:43 +03:00
|
|
|
lc = login_getpwclass(pwd);
|
|
|
|
auth_passed = 1;
|
|
|
|
|
|
|
|
switch (pam_err = pam_acct_mgmt(pamh, pam_silent)) {
|
|
|
|
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_END("pam_chauthtok");
|
|
|
|
break;
|
|
|
|
|
2006-03-07 02:06:18 +03:00
|
|
|
case PAM_AUTH_ERR:
|
|
|
|
case PAM_USER_UNKNOWN:
|
|
|
|
case PAM_MAXTRIES:
|
|
|
|
auth_passed = 0;
|
|
|
|
break;
|
|
|
|
|
2005-01-23 12:47:43 +03:00
|
|
|
default:
|
|
|
|
PAM_END("pam_acct_mgmt");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAM_AUTH_ERR:
|
|
|
|
case PAM_USER_UNKNOWN:
|
|
|
|
case PAM_MAXTRIES:
|
|
|
|
auth_passed = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PAM_END("pam_authenticate");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, 0);
|
|
|
|
|
|
|
|
skip_auth:
|
|
|
|
/*
|
|
|
|
* If the user exists and authentication passed,
|
|
|
|
* get out of the loop and login the user.
|
|
|
|
*/
|
|
|
|
if (pwd && auth_passed)
|
|
|
|
break;
|
|
|
|
|
2006-03-07 02:06:18 +03:00
|
|
|
(void)printf("Login incorrect or refused on this terminal.\n");
|
2005-01-23 12:47:43 +03:00
|
|
|
failures++;
|
|
|
|
cnt++;
|
2006-03-08 06:09:04 +03:00
|
|
|
/*
|
|
|
|
* We allow login_retries tries, but after login_backoff
|
|
|
|
* we start backing off. These default to 10 and 3
|
|
|
|
* respectively.
|
|
|
|
*/
|
2005-01-23 12:47:43 +03:00
|
|
|
if (cnt > login_backoff) {
|
|
|
|
if (cnt >= login_retries) {
|
|
|
|
badlogin(username);
|
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2005-03-30 05:30:21 +04:00
|
|
|
sleepexit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
2006-03-08 06:09:04 +03:00
|
|
|
sleep((u_int)((cnt - login_backoff) * 5));
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* committed to login -- turn off timeout */
|
|
|
|
(void)alarm((u_int)0);
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
quietlog = login_getcapbool(lc, "hushlogin", 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Temporarily give up special privileges so we can change
|
|
|
|
* into NFS-mounted homes that are exported for non-root
|
|
|
|
* access and have mode 7x0
|
|
|
|
*/
|
|
|
|
saved_uid = geteuid();
|
|
|
|
saved_gid = getegid();
|
|
|
|
nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
|
|
|
|
|
|
|
|
(void)setegid(pwd->pw_gid);
|
|
|
|
initgroups(username, pwd->pw_gid);
|
|
|
|
(void)seteuid(pwd->pw_uid);
|
|
|
|
|
|
|
|
if (chdir(pwd->pw_dir) != 0) {
|
|
|
|
if (login_getcapbool(lc, "requirehome", 0)) {
|
|
|
|
(void)printf("Home directory %s required\n",
|
|
|
|
pwd->pw_dir);
|
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)printf("No home directory %s!\n", pwd->pw_dir);
|
2007-05-09 05:56:25 +04:00
|
|
|
if (chdir("/") == -1) {
|
2005-01-23 12:47:43 +03:00
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2007-05-09 05:56:25 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
pwd->pw_dir = "/";
|
|
|
|
(void)printf("Logging in with home = \"/\".\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!quietlog) {
|
|
|
|
quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
|
2006-02-19 03:12:36 +03:00
|
|
|
pam_silent = quietlog ? PAM_SILENT : 0;
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* regain special privileges */
|
|
|
|
setegid(saved_gid);
|
|
|
|
setgroups(nsaved_gids, saved_gids);
|
|
|
|
seteuid(saved_uid);
|
|
|
|
|
2005-09-21 16:24:11 +04:00
|
|
|
(void)getgrnam_r(TTYGRPNAME, &grs, grbuf, sizeof(grbuf), &grp);
|
2005-02-28 19:11:36 +03:00
|
|
|
(void)chown(ttyn, pwd->pw_uid,
|
2005-09-21 16:24:11 +04:00
|
|
|
(grp != NULL) ? grp->gr_gid : pwd->pw_gid);
|
2005-02-28 19:11:36 +03:00
|
|
|
|
2005-03-03 05:06:16 +03:00
|
|
|
if (ttyaction(ttyn, "login", pwd->pw_name))
|
|
|
|
(void)printf("Warning: ttyaction failed.\n");
|
|
|
|
|
2005-01-23 12:47:43 +03:00
|
|
|
/* Nothing else left to fail -- really log in. */
|
2009-12-29 22:26:13 +03:00
|
|
|
update_db(quietlog, rootlogin, fflag);
|
2005-01-23 12:47:43 +03:00
|
|
|
|
|
|
|
if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
|
|
|
|
LOGIN_SETLOGIN) != 0) {
|
|
|
|
syslog(LOG_ERR, "setusercontext failed");
|
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tty[sizeof("tty")-1] == 'd')
|
|
|
|
syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Establish groups
|
|
|
|
*/
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
|
|
|
|
syslog(LOG_ERR, "setusercontext failed");
|
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
PAM_END("pam_setcred");
|
|
|
|
|
|
|
|
pam_err = pam_open_session(pamh, pam_silent);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
PAM_END("pam_open_session");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fork because we need to call pam_closesession as root.
|
|
|
|
* Make sure signals cannot kill the parent.
|
2006-04-17 20:29:44 +04:00
|
|
|
* This has been handled in the begining of main.
|
2005-01-23 12:47:43 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
switch(pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
pam_err = pam_close_session(pamh, 0);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
warnx("pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "fork failed: %m");
|
|
|
|
warn("fork failed");
|
|
|
|
pam_end(pamh, pam_err);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0: /* Child */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Parent: wait for the child to terminate
|
|
|
|
* and call pam_close_session.
|
|
|
|
*/
|
2005-02-28 19:11:36 +03:00
|
|
|
if ((xpid = waitpid(pid, &status, 0)) != pid) {
|
2005-01-23 12:47:43 +03:00
|
|
|
pam_err = pam_close_session(pamh, 0);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
warnx("pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
}
|
|
|
|
pam_end(pamh, pam_err);
|
2005-02-28 19:11:36 +03:00
|
|
|
if (xpid != -1)
|
|
|
|
warnx("wrong PID: %d != %d", pid, xpid);
|
|
|
|
else
|
|
|
|
warn("wait for pid %d failed", pid);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)signal(SIGABRT, oabrt);
|
2006-04-17 20:29:44 +04:00
|
|
|
(void)signal(SIGALRM, oalrm);
|
|
|
|
(void)signal(SIGINT, oint);
|
|
|
|
(void)signal(SIGQUIT, oquit);
|
2005-01-23 12:47:43 +03:00
|
|
|
if ((pam_err = pam_close_session(pamh, 0)) != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
warnx("pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
}
|
|
|
|
pam_end(pamh, PAM_SUCCESS);
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_SUCCESS);
|
2005-01-23 12:47:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The child: starting here, we don't have to care about
|
|
|
|
* handling PAM issues if we exit, the parent will do the
|
|
|
|
* job when we exit.
|
|
|
|
*
|
|
|
|
* Destroy environment unless user has requested its preservation.
|
|
|
|
* Try to preserve TERM anyway.
|
|
|
|
*/
|
|
|
|
saved_term = getenv("TERM");
|
|
|
|
if (!pflag) {
|
|
|
|
environ = envinit;
|
|
|
|
if (saved_term)
|
|
|
|
setenv("TERM", saved_term, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pwd->pw_shell == '\0')
|
|
|
|
pwd->pw_shell = _PATH_BSHELL;
|
|
|
|
|
|
|
|
shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
|
|
|
|
if (*shell == '\0')
|
|
|
|
shell = pwd->pw_shell;
|
|
|
|
|
|
|
|
if ((pwd->pw_shell = strdup(shell)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "Cannot alloc mem");
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
(void)setenv("SHELL", pwd->pw_shell, 1);
|
|
|
|
if (term[0] == '\0') {
|
|
|
|
char *tt = (char *)stypeof(tty);
|
|
|
|
|
|
|
|
if (tt == NULL)
|
|
|
|
tt = login_getcapstr(lc, "term", NULL, NULL);
|
|
|
|
|
|
|
|
/* unknown term -> "su" */
|
|
|
|
(void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
|
|
|
|
}
|
|
|
|
(void)setenv("TERM", term, 0);
|
|
|
|
(void)setenv("LOGNAME", pwd->pw_name, 1);
|
|
|
|
(void)setenv("USER", pwd->pw_name, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add PAM environement
|
|
|
|
*/
|
|
|
|
if ((pamenv = pam_getenvlist(pamh)) != NULL) {
|
|
|
|
char **envitem;
|
|
|
|
|
|
|
|
for (envitem = pamenv; *envitem; envitem++) {
|
|
|
|
putenv(*envitem);
|
|
|
|
free(*envitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pamenv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This drops root privs */
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid,
|
|
|
|
(LOGIN_SETALL & ~LOGIN_SETLOGIN)) != 0) {
|
|
|
|
syslog(LOG_ERR, "setusercontext failed");
|
2005-03-30 05:30:21 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!quietlog) {
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
fname = login_getcapstr(lc, "copyright", NULL, NULL);
|
|
|
|
if (fname != NULL && access(fname, F_OK) == 0)
|
|
|
|
motd(fname);
|
|
|
|
else
|
|
|
|
(void)printf("%s", copyrightstr);
|
|
|
|
|
|
|
|
fname = login_getcapstr(lc, "welcome", NULL, NULL);
|
|
|
|
if (fname == NULL || access(fname, F_OK) != 0)
|
|
|
|
fname = _PATH_MOTDFILE;
|
|
|
|
motd(fname);
|
|
|
|
|
|
|
|
(void)snprintf(tbuf,
|
|
|
|
sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
|
|
|
|
if (stat(tbuf, &st) == 0 && st.st_size != 0)
|
|
|
|
(void)printf("You have %smail.\n",
|
|
|
|
(st.st_mtime > st.st_atime) ? "new " : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
login_close(lc);
|
|
|
|
|
|
|
|
|
|
|
|
tbuf[0] = '-';
|
|
|
|
(void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
|
|
|
|
p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
|
|
|
|
|
2006-03-22 18:36:49 +03:00
|
|
|
(void)signal(SIGABRT, oabrt);
|
2006-04-17 20:29:44 +04:00
|
|
|
(void)signal(SIGALRM, oalrm);
|
|
|
|
(void)signal(SIGINT, oint);
|
|
|
|
(void)signal(SIGQUIT, oquit);
|
|
|
|
(void)signal(SIGTSTP, SIG_IGN);
|
|
|
|
|
2005-04-25 05:33:03 +04:00
|
|
|
execlp(pwd->pw_shell, tbuf, NULL);
|
2005-03-30 05:30:21 +04:00
|
|
|
err(EXIT_FAILURE, "%s", pwd->pw_shell);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
2009-12-29 22:26:13 +03:00
|
|
|
static void
|
|
|
|
usage(void)
|
2005-01-23 12:47:43 +03:00
|
|
|
{
|
2009-12-29 22:26:13 +03:00
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Usage: %s [-fp] [-a address] [-h hostname] [username]\n",
|
|
|
|
getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
2005-01-23 12:47:43 +03:00
|
|
|
}
|
|
|
|
|
2009-12-29 22:26:13 +03:00
|
|
|
#if 0
|
|
|
|
static int
|
2005-01-23 12:47:43 +03:00
|
|
|
rootterm(char *ttyn)
|
|
|
|
{
|
|
|
|
struct ttyent *t;
|
|
|
|
|
|
|
|
return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
|
|
|
|
}
|
2009-12-29 22:26:13 +03:00
|
|
|
#endif
|