xref: /onnv-gate/usr/src/cmd/login/login.c (revision 5331:3047ad28a67b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52515Sas145665  * Common Development and Distribution License (the "License").
62515Sas145665  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
410Sstevel@tonic-gate /*	  All Rights Reserved	*/
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * For a complete reference to login(1), see the manual page.  However,
490Sstevel@tonic-gate  * login has accreted some intentionally undocumented options, which are
500Sstevel@tonic-gate  * explained here:
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * -a: This legacy flag appears to be unused.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * -f <username>: This flag was introduced by PSARC 1995/039 in support
550Sstevel@tonic-gate  *    of Kerberos.  But it's not used by Sun's Kerberos implementation.
560Sstevel@tonic-gate  *    It is however employed by zlogin(1), since it allows one to tell
570Sstevel@tonic-gate  *    login: "This user is authenticated."  In the case of zlogin that's
580Sstevel@tonic-gate  *    true because the zone always trusts the global zone.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * -z <zonename>: This flag is passed to login when zlogin(1) executes a
610Sstevel@tonic-gate  *    zone login.  This tells login(1) to skip it's normal CONSOLE check
620Sstevel@tonic-gate  *    (i.e. that the root login must be on /dev/console) and tells us the
63*5331Samw  *    name of the zone from which the login is occurring.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/types.h>
670Sstevel@tonic-gate #include <sys/param.h>
680Sstevel@tonic-gate #include <unistd.h>	/* For logfile locking */
690Sstevel@tonic-gate #include <signal.h>
700Sstevel@tonic-gate #include <stdio.h>
710Sstevel@tonic-gate #include <sys/stat.h>
720Sstevel@tonic-gate #include <string.h>
730Sstevel@tonic-gate #include <deflt.h>
740Sstevel@tonic-gate #include <grp.h>
750Sstevel@tonic-gate #include <fcntl.h>
760Sstevel@tonic-gate #include <lastlog.h>
770Sstevel@tonic-gate #include <termio.h>
780Sstevel@tonic-gate #include <utmpx.h>
790Sstevel@tonic-gate #include <stdlib.h>
800Sstevel@tonic-gate #include <wait.h>
810Sstevel@tonic-gate #include <errno.h>
820Sstevel@tonic-gate #include <ctype.h>
830Sstevel@tonic-gate #include <syslog.h>
840Sstevel@tonic-gate #include <ulimit.h>
850Sstevel@tonic-gate #include <libgen.h>
860Sstevel@tonic-gate #include <pwd.h>
870Sstevel@tonic-gate #include <security/pam_appl.h>
880Sstevel@tonic-gate #include <strings.h>
890Sstevel@tonic-gate #include <libdevinfo.h>
900Sstevel@tonic-gate #include <zone.h>
910Sstevel@tonic-gate #include "login_audit.h"
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #include <krb5_repository.h>
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *	    *** Defines, Macros, and String Constants  ***
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	ISSUEFILE "/etc/issue"	/* file to print before prompt */
1020Sstevel@tonic-gate #define	NOLOGIN	"/etc/nologin"	/* file to lock users out during shutdown */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * These need to be defined for UTMPX management.
1060Sstevel@tonic-gate  * If we add in the utility functions later, we
1070Sstevel@tonic-gate  * can remove them.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate #define	__UPDATE_ENTRY	1
1100Sstevel@tonic-gate #define	__LOGIN		2
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Intervals to sleep after failed login
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate #ifndef	SLEEPTIME
1160Sstevel@tonic-gate #define	SLEEPTIME 4	/* sleeptime before login incorrect msg */
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate static int	Sleeptime = SLEEPTIME;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * seconds login disabled after allowable number of unsuccessful attempts
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate #ifndef	DISABLETIME
1240Sstevel@tonic-gate #define	DISABLETIME	20
1250Sstevel@tonic-gate #endif
1260Sstevel@tonic-gate static int	Disabletime = DISABLETIME;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #define	MAXTRYS		5
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static int	retry = MAXTRYS;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Login logging support
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate #define	LOGINLOG	"/var/adm/loginlog"	/* login log file */
1360Sstevel@tonic-gate #define	LNAME_SIZE	20	/* size of logged logname */
1370Sstevel@tonic-gate #define	TTYN_SIZE	15	/* size of logged tty name */
1380Sstevel@tonic-gate #define	TIME_SIZE	30	/* size of logged time string */
1390Sstevel@tonic-gate #define	ENT_SIZE	(LNAME_SIZE + TTYN_SIZE + TIME_SIZE + 3)
1400Sstevel@tonic-gate #define	L_WAITTIME	5	/* waittime for log file to unlock */
1410Sstevel@tonic-gate #define	LOGTRYS		10	/* depth of 'try' logging */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * String manipulation macros: SCPYN, SCPYL, EQN and ENVSTRNCAT
1450Sstevel@tonic-gate  * SCPYL is the safer version of SCPYN
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate #define	SCPYL(a, b)	(void) strlcpy(a, b, sizeof (a))
1480Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, sizeof (a))
1490Sstevel@tonic-gate #define	EQN(a, b)	(strncmp(a, b, sizeof (a)-1) == 0)
1500Sstevel@tonic-gate #define	ENVSTRNCAT(to, from) {int deflen; deflen = strlen(to); \
1510Sstevel@tonic-gate 	(void) strncpy((to)+ deflen, (from), sizeof (to) - (1 + deflen)); }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * Other macros
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate #define	NMAX	sizeof (((struct utmpx *)0)->ut_name)
1570Sstevel@tonic-gate #define	HMAX	sizeof (((struct utmpx *)0)->ut_host)
1580Sstevel@tonic-gate #define	min(a, b)	(((a) < (b)) ? (a) : (b))
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Various useful files and string constants
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate #define	SHELL		"/usr/bin/sh"
1640Sstevel@tonic-gate #define	SHELL2		"/sbin/sh"
1650Sstevel@tonic-gate #define	SUBLOGIN	"<!sublogin>"
1660Sstevel@tonic-gate #define	LASTLOG		"/var/adm/lastlog"
1670Sstevel@tonic-gate #define	PROG_NAME	"login"
1680Sstevel@tonic-gate #define	HUSHLOGIN	".hushlogin"
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Array and Buffer sizes
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate #define	PBUFSIZE 8	/* max significant characters in a password */
1750Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1760Sstevel@tonic-gate #define	MAXARGS 63	/* change value below if changing this */
1770Sstevel@tonic-gate #define	MAXARGSWIDTH 2	/* log10(MAXARGS) */
1780Sstevel@tonic-gate #define	MAXENV 1024
1790Sstevel@tonic-gate #define	MAXLINE 2048
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Miscellaneous constants
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate #define	ROOTUID		0
1850Sstevel@tonic-gate #define	ERROR		1
1860Sstevel@tonic-gate #define	OK		0
1870Sstevel@tonic-gate #define	LOG_ERROR	1
1880Sstevel@tonic-gate #define	DONT_LOG_ERROR	0
1890Sstevel@tonic-gate #define	TRUE		1
1900Sstevel@tonic-gate #define	FALSE		0
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * Counters for counting the number of failed login attempts
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate static int trys = 0;
1960Sstevel@tonic-gate static int count = 1;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * error value for login_exit() audit output (0 == no audit record)
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate static int	audit_error = 0;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * Externs a plenty
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2070Sstevel@tonic-gate extern	int	getsecretkey();
2080Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * The current user name
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static	char	user_name[NMAX];
2140Sstevel@tonic-gate static	char	minusnam[16] = "-";
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * locale environments to be passed to shells.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static char *localeenv[] = {
2200Sstevel@tonic-gate 	"LANG",
2210Sstevel@tonic-gate 	"LC_CTYPE", "LC_NUMERIC", "LC_TIME", "LC_COLLATE",
2220Sstevel@tonic-gate 	"LC_MONETARY", "LC_MESSAGES", "LC_ALL", 0};
2230Sstevel@tonic-gate static int locale_envmatch(char *, char *);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Environment variable support
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate static	char	shell[256] = { "SHELL=" };
2290Sstevel@tonic-gate static	char	home[MAXPATHLEN] = { "HOME=" };
2300Sstevel@tonic-gate static	char	term[64] = { "TERM=" };
2310Sstevel@tonic-gate static	char	logname[30] = { "LOGNAME=" };
2320Sstevel@tonic-gate static	char	timez[100] = { "TZ=" };
2330Sstevel@tonic-gate static	char	hertz[10] = { "HZ=" };
2340Sstevel@tonic-gate static	char	path[MAXPATHLEN] = { "PATH=" };
2350Sstevel@tonic-gate static	char	*newenv[10+MAXARGS] =
2360Sstevel@tonic-gate 	{home, path, logname, hertz, term, 0, 0};
2370Sstevel@tonic-gate static	char	**envinit = newenv;
2380Sstevel@tonic-gate static	int	basicenv;
2390Sstevel@tonic-gate static	char	*zero = (char *)0;
2400Sstevel@tonic-gate static	char 	**envp;
2410Sstevel@tonic-gate #ifndef	NO_MAIL
2420Sstevel@tonic-gate static	char	mail[30] = { "MAIL=/var/mail/" };
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate extern char **environ;
2450Sstevel@tonic-gate static	char inputline[MAXLINE];
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #define	MAX_ID_LEN 256
2480Sstevel@tonic-gate #define	MAX_REPOSITORY_LEN 256
2490Sstevel@tonic-gate #define	MAX_PAMSERVICE_LEN 256
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static char identity[MAX_ID_LEN];
2520Sstevel@tonic-gate static char repository[MAX_REPOSITORY_LEN];
2530Sstevel@tonic-gate static char progname[MAX_PAMSERVICE_LEN];
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Strings used to prompt the user.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate static	char	loginmsg[] = "login: ";
2600Sstevel@tonic-gate static	char	passwdmsg[] = "Password:";
2610Sstevel@tonic-gate static	char	incorrectmsg[] = "Login incorrect\n";
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * Password file support
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate static	struct	passwd *pwd = NULL;
2680Sstevel@tonic-gate static	char	remote_host[HMAX];
2690Sstevel@tonic-gate static	char	zone_name[ZONENAME_MAX];
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * Illegal passwd entries.
2730Sstevel@tonic-gate  */
2744321Scasper static	struct	passwd nouser = { "", "no:password", (uid_t)-1 };
2750Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * Log file support
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate static	char	*log_entry[LOGTRYS];
2810Sstevel@tonic-gate static	int	writelog = 0;
2820Sstevel@tonic-gate static	int	lastlogok = 0;
2830Sstevel@tonic-gate static	struct lastlog ll;
2840Sstevel@tonic-gate static	int	dosyslog = 0;
2850Sstevel@tonic-gate static	int	flogin = MAXTRYS;	/* flag for SYSLOG_FAILED_LOGINS */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Default file toggles
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static	char	*Pndefault	= "/etc/default/login";
2910Sstevel@tonic-gate static	char	*Altshell	= NULL;
2920Sstevel@tonic-gate static	char	*Console	= NULL;
2930Sstevel@tonic-gate static	int	Passreqflag	= 0;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate #define	DEFUMASK	022
2960Sstevel@tonic-gate static	mode_t	Umask		= DEFUMASK;
2970Sstevel@tonic-gate static	char 	*Def_tz		= NULL;
2980Sstevel@tonic-gate static	char 	*tmp_tz		= NULL;
2990Sstevel@tonic-gate static	char 	*Def_hertz	= NULL;
3000Sstevel@tonic-gate #define	SET_FSIZ	2			/* ulimit() command arg */
3010Sstevel@tonic-gate static	long	Def_ulimit	= 0;
3020Sstevel@tonic-gate #define	MAX_TIMEOUT	(15 * 60)
3030Sstevel@tonic-gate #define	DEF_TIMEOUT	(5 * 60)
3040Sstevel@tonic-gate static	unsigned Def_timeout	= DEF_TIMEOUT;
3050Sstevel@tonic-gate static	char	*Def_path	= NULL;
3060Sstevel@tonic-gate static	char	*Def_supath	= NULL;
3070Sstevel@tonic-gate #define	DEF_PATH	"/usr/bin:" 	/* same as PATH */
3080Sstevel@tonic-gate #define	DEF_SUPATH	"/usr/sbin:/usr/bin" /* same as ROOTPATH */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Defaults for updating expired passwords
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate #define	DEF_ATTEMPTS	3
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * ttyprompt will point to the environment variable TTYPROMPT.
3170Sstevel@tonic-gate  * TTYPROMPT is set by ttymon if ttymon already wrote out the prompt.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static	char	*ttyprompt = NULL;
3200Sstevel@tonic-gate static	char 	*ttyn = NULL;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * Pass inherited environment.  Used by telnetd in support of the telnet
3240Sstevel@tonic-gate  * ENVIRON option.
3250Sstevel@tonic-gate  */
3260Sstevel@tonic-gate static	boolean_t pflag = B_FALSE;
3270Sstevel@tonic-gate static  boolean_t uflag = B_FALSE;
3280Sstevel@tonic-gate static  boolean_t Rflag = B_FALSE;
3290Sstevel@tonic-gate static  boolean_t sflag = B_FALSE;
3300Sstevel@tonic-gate static  boolean_t Uflag = B_FALSE;
3310Sstevel@tonic-gate static  boolean_t tflag = B_FALSE;
3320Sstevel@tonic-gate static	boolean_t hflag = B_FALSE;
3330Sstevel@tonic-gate static  boolean_t rflag = B_FALSE;
3340Sstevel@tonic-gate static  boolean_t zflag = B_FALSE;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * Remote login support
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate static	char	rusername[NMAX+1], lusername[NMAX+1];
3400Sstevel@tonic-gate static	char	terminal[MAXPATHLEN];
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * Pre-authentication flag support
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate static	int	fflag;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate static char ** getargs(char *);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate static int login_conv(int, struct pam_message **,
3510Sstevel@tonic-gate     struct pam_response **, void *);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static struct pam_conv pam_conv = {login_conv, NULL};
3540Sstevel@tonic-gate static pam_handle_t *pamh;	/* Authentication handle */
3550Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Function declarations
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate static	void	turn_on_logging(void);
3610Sstevel@tonic-gate static	void	defaults(void);
3620Sstevel@tonic-gate static	void	usage(void);
3630Sstevel@tonic-gate static	void	process_rlogin(void);
3640Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
3650Sstevel@tonic-gate static	void	login_authenticate();
3660Sstevel@tonic-gate static	void	setup_credentials(void);
3670Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
3680Sstevel@tonic-gate static	void	adjust_nice(void);
3690Sstevel@tonic-gate static	void	update_utmpx_entry(int);
3700Sstevel@tonic-gate static	void	establish_user_environment(char **);
3710Sstevel@tonic-gate static	void	print_banner(void);
3720Sstevel@tonic-gate static	void	display_last_login_time(void);
3730Sstevel@tonic-gate static	void	exec_the_shell(void);
3740Sstevel@tonic-gate static	int	process_chroot_logins(void);
3750Sstevel@tonic-gate static 	void	chdir_to_dir_user(void);
3760Sstevel@tonic-gate static	void	check_log(void);
3770Sstevel@tonic-gate static	void	validate_account(void);
3780Sstevel@tonic-gate static	void	doremoteterm(char *);
3790Sstevel@tonic-gate static	int	get_options(int, char **);
3800Sstevel@tonic-gate static	void	getstr(char *, int, char *);
3810Sstevel@tonic-gate static 	int	legalenvvar(char *);
3820Sstevel@tonic-gate static	void	check_for_console(void);
3830Sstevel@tonic-gate static	void	check_for_dueling_unix(char *);
3840Sstevel@tonic-gate static	void	get_user_name(void);
3850Sstevel@tonic-gate static	uint_t	get_audit_id(void);
386523Sbasabi static	void	login_exit(int)__NORETURN;
3870Sstevel@tonic-gate static	int	logins_disabled(char *);
3880Sstevel@tonic-gate static	void	log_bad_attempts(void);
3890Sstevel@tonic-gate static	int	is_number(char *);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  *			*** main ***
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  *	The primary flow of control is directed in this routine.
3960Sstevel@tonic-gate  *	Control moves in line from top to bottom calling subfunctions
3970Sstevel@tonic-gate  *	which perform the bulk of the work.  Many of these calls exit
3980Sstevel@tonic-gate  *	when a fatal error is encountered and do not return to main.
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  *
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate 
403523Sbasabi int
4040Sstevel@tonic-gate main(int argc, char *argv[], char **renvp)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
4070Sstevel@tonic-gate 	int sublogin;
4080Sstevel@tonic-gate 	int pam_rc;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Set up Defaults and flags
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	defaults();
4140Sstevel@tonic-gate 	SCPYL(progname, PROG_NAME);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Set up default umask
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	if (Umask > ((mode_t)0777))
4200Sstevel@tonic-gate 		Umask = DEFUMASK;
4210Sstevel@tonic-gate 	(void) umask(Umask);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * Set up default timeouts and delays
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	if (Def_timeout > MAX_TIMEOUT)
4270Sstevel@tonic-gate 		Def_timeout = MAX_TIMEOUT;
4280Sstevel@tonic-gate 	if (Sleeptime < 0 || Sleeptime > 5)
4290Sstevel@tonic-gate 		Sleeptime = SLEEPTIME;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	(void) alarm(Def_timeout);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/*
4340Sstevel@tonic-gate 	 * Ignore SIGQUIT and SIGINT and set nice to 0
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
4370Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
4380Sstevel@tonic-gate 	(void) nice(0);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * Set flag to disable the pid check if you find that you are
4420Sstevel@tonic-gate 	 * a subsystem login.
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	sublogin = 0;
4450Sstevel@tonic-gate 	if (*renvp && strcmp(*renvp, SUBLOGIN) == 0)
4460Sstevel@tonic-gate 		sublogin = 1;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Parse Arguments
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if (get_options(argc, argv) == -1) {
4520Sstevel@tonic-gate 		usage();
4530Sstevel@tonic-gate 		audit_error = ADT_FAIL_VALUE_BAD_CMD;
4540Sstevel@tonic-gate 		login_exit(1);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * if devicename is not passed as argument, call ttyname(0)
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	if (ttyn == NULL) {
4610Sstevel@tonic-gate 		ttyn = ttyname(0);
4620Sstevel@tonic-gate 		if (ttyn == NULL)
4630Sstevel@tonic-gate 			ttyn = "/dev/???";
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * Call pam_start to initiate a PAM authentication operation
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if ((pam_rc = pam_start(progname, user_name, &pam_conv, &pamh))
4720Sstevel@tonic-gate 	    != PAM_SUCCESS) {
4730Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + pam_rc;
4740Sstevel@tonic-gate 		login_exit(1);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 	if ((pam_rc = pam_set_item(pamh, PAM_TTY, ttyn)) != PAM_SUCCESS) {
4770Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + pam_rc;
4780Sstevel@tonic-gate 		login_exit(1);
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 	if ((pam_rc = pam_set_item(pamh, PAM_RHOST, remote_host)) !=
4810Sstevel@tonic-gate 	    PAM_SUCCESS) {
4820Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + pam_rc;
4830Sstevel@tonic-gate 		login_exit(1);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * We currently only support special handling of the KRB5 PAM repository
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	if ((Rflag && strlen(repository)) &&
4900Sstevel@tonic-gate 	    strcmp(repository, KRB5_REPOSITORY_NAME) == 0 &&
4910Sstevel@tonic-gate 	    (uflag && strlen(identity))) {
4920Sstevel@tonic-gate 		krb5_repository_data_t krb5_data;
4930Sstevel@tonic-gate 		pam_repository_t pam_rep_data;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		krb5_data.principal = identity;
4960Sstevel@tonic-gate 		krb5_data.flags = SUNW_PAM_KRB5_ALREADY_AUTHENTICATED;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		pam_rep_data.type = repository;
4990Sstevel@tonic-gate 		pam_rep_data.scope = (void *)&krb5_data;
5000Sstevel@tonic-gate 		pam_rep_data.scope_len = sizeof (krb5_data);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_REPOSITORY,
5030Sstevel@tonic-gate 		    (void *)&pam_rep_data);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Open the log file which contains a record of successful and failed
5090Sstevel@tonic-gate 	 * login attempts
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	turn_on_logging();
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * say "hi" to syslogd ..
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	openlog("login", 0, LOG_AUTH);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Do special processing for -r (rlogin) flag
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	if (rflag)
5220Sstevel@tonic-gate 		process_rlogin();
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
5250Sstevel@tonic-gate 	/*
5260Sstevel@tonic-gate 	 * validate user
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	/* we are already authenticated. fill in what we must, then continue */
5290Sstevel@tonic-gate 	if (fflag) {
5300Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
5310Sstevel@tonic-gate 		if ((pwd = getpwnam(user_name)) == NULL) {
5320Sstevel@tonic-gate 			audit_error = ADT_FAIL_VALUE_USERNAME;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 			log_bad_attempts();
5350Sstevel@tonic-gate 			(void) printf("Login failed: unknown user '%s'.\n",
5360Sstevel@tonic-gate 			    user_name);
5370Sstevel@tonic-gate 			login_exit(1);
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
5400Sstevel@tonic-gate 	} else {
5410Sstevel@tonic-gate 		/*
5420Sstevel@tonic-gate 		 * Perform the primary login authentication activity.
5430Sstevel@tonic-gate 		 */
5440Sstevel@tonic-gate 		login_authenticate();
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/* change root login, then we exec another login and try again */
5490Sstevel@tonic-gate 	if (process_chroot_logins() != OK)
5500Sstevel@tonic-gate 		login_exit(1);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * If root login and not on system console then call exit(2)
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	check_for_console();
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * Check to see if a shutdown is in progress, if it is and
5590Sstevel@tonic-gate 	 * we are not root then throw the user off the system
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	if (logins_disabled(user_name) == TRUE) {
5620Sstevel@tonic-gate 		audit_error = ADT_FAIL_VALUE_LOGIN_DISABLED;
5630Sstevel@tonic-gate 		login_exit(1);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (pwd->pw_uid == 0) {
5670Sstevel@tonic-gate 		if (Def_supath != NULL)
5680Sstevel@tonic-gate 			Def_path = Def_supath;
5690Sstevel@tonic-gate 		else
5700Sstevel@tonic-gate 			Def_path = DEF_SUPATH;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * Check account expiration and passwd aging
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	validate_account();
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * We only get here if we've been authenticated.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * NOTE: telnetd and rlogind rely upon this updating of utmpx
5830Sstevel@tonic-gate 	 * to indicate that the authentication completed  successfully,
5840Sstevel@tonic-gate 	 * pam_open_session was called and therefore they are required to
5850Sstevel@tonic-gate 	 * call pam_close_session.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	update_utmpx_entry(sublogin);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * Now we set up the environment for the new user, which includes
5910Sstevel@tonic-gate 	 * the users ulimit, nice value, ownership of this tty, uid, gid,
5920Sstevel@tonic-gate 	 * and environment variables.
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	if (Def_ulimit > 0L && ulimit(SET_FSIZ, Def_ulimit) < 0L)
5950Sstevel@tonic-gate 		(void) printf("Could not set ULIMIT to %ld\n", Def_ulimit);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/* di_devperm_login() sends detailed errors to syslog */
5980Sstevel@tonic-gate 	if (di_devperm_login((const char *)ttyn, pwd->pw_uid, pwd->pw_gid,
5990Sstevel@tonic-gate 	    NULL) == -1) {
6000Sstevel@tonic-gate 		(void) fprintf(stderr, "error processing /etc/logindevperm,"
6010Sstevel@tonic-gate 		    " see syslog for more details\n");
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	adjust_nice();		/* passwd file can specify nice value */
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
6070Sstevel@tonic-gate 	setup_credentials();	/* Set uid/gid - exits on failure */
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * Set up the basic environment for the exec.  This includes
6110Sstevel@tonic-gate 	 * HOME, PATH, LOGNAME, SHELL, TERM, TZ, HZ, and MAIL.
6120Sstevel@tonic-gate 	 */
6130Sstevel@tonic-gate 	chdir_to_dir_user();
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	establish_user_environment(renvp);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);	/* Done using PAM */
6180Sstevel@tonic-gate 	pamh = NULL;
6190Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (pwd->pw_uid == 0) {
6220Sstevel@tonic-gate 		if (dosyslog) {
6230Sstevel@tonic-gate 			if (remote_host[0]) {
6240Sstevel@tonic-gate 			    syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s",
6250Sstevel@tonic-gate 			    ttyn, HMAX, remote_host);
6260Sstevel@tonic-gate 			} else
6270Sstevel@tonic-gate 				syslog(LOG_NOTICE, "ROOT LOGIN %s", ttyn);
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	closelog();
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_DFL);
6330Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_DFL);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/*
6360Sstevel@tonic-gate 	 * Display some useful information to the new user like the banner
6370Sstevel@tonic-gate 	 * and last login time if not a quiet login.
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (access(HUSHLOGIN, F_OK) != 0) {
6410Sstevel@tonic-gate 		print_banner();
6420Sstevel@tonic-gate 		display_last_login_time();
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * Set SIGXCPU and SIGXFSZ to default disposition.
6470Sstevel@tonic-gate 	 * Shells inherit signal disposition from parent.
6480Sstevel@tonic-gate 	 * And the shells should have default dispositions
6490Sstevel@tonic-gate 	 * for the two below signals.
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	(void) signal(SIGXCPU, SIG_DFL);
6520Sstevel@tonic-gate 	(void) signal(SIGXFSZ, SIG_DFL);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * Now fire off the shell of choice
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	exec_the_shell();
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * All done
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	login_exit(1);
663523Sbasabi 	return (0);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  *			*** Utility functions ***
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * donothing & catch	- Signal catching functions
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*ARGSUSED*/
6790Sstevel@tonic-gate static void
6800Sstevel@tonic-gate donothing(int sig)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	if (pamh)
6830Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate #ifdef notdef
6880Sstevel@tonic-gate static	int	intrupt;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate /*ARGSUSED*/
6910Sstevel@tonic-gate static void
6920Sstevel@tonic-gate catch(int sig)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	++intrupt;
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate #endif
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  *			*** Bad login logging support ***
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * badlogin() 		- log to the log file 'trys'
7040Sstevel@tonic-gate  *			  unsuccessful attempts
7050Sstevel@tonic-gate  */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate static void
7080Sstevel@tonic-gate badlogin(void)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	int retval, count1, fildes;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Tries to open the log file. If succeed, lock it and write
7140Sstevel@tonic-gate 	 * in the failed attempts
7150Sstevel@tonic-gate 	 */
7160Sstevel@tonic-gate 	if ((fildes = open(LOGINLOG, O_APPEND|O_WRONLY)) != -1) {
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		(void) sigset(SIGALRM, donothing);
7190Sstevel@tonic-gate 		(void) alarm(L_WAITTIME);
7200Sstevel@tonic-gate 		retval = lockf(fildes, F_LOCK, 0L);
7210Sstevel@tonic-gate 		(void) alarm(0);
7220Sstevel@tonic-gate 		(void) sigset(SIGALRM, SIG_DFL);
7230Sstevel@tonic-gate 		if (retval == 0) {
7240Sstevel@tonic-gate 			for (count1 = 0; count1 < trys; count1++)
7250Sstevel@tonic-gate 				(void) write(fildes, log_entry[count1],
7260Sstevel@tonic-gate 				    (unsigned)strlen(log_entry[count1]));
7270Sstevel@tonic-gate 			(void) lockf(fildes, F_ULOCK, 0L);
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 		(void) close(fildes);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate  * log_bad_attempts 	- log each bad login attempt - called from
7360Sstevel@tonic-gate  *			  login_authenticate.  Exits when the maximum attempt
7370Sstevel@tonic-gate  *			  count is exceeded.
7380Sstevel@tonic-gate  */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate static void
7410Sstevel@tonic-gate log_bad_attempts(void)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	time_t timenow;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (trys >= LOGTRYS)
7460Sstevel@tonic-gate 		return;
7470Sstevel@tonic-gate 	if (writelog) {
7480Sstevel@tonic-gate 		(void) time(&timenow);
7490Sstevel@tonic-gate 		(void) strncat(log_entry[trys], user_name, LNAME_SIZE);
7500Sstevel@tonic-gate 		(void) strncat(log_entry[trys], ":", (size_t)1);
7510Sstevel@tonic-gate 		(void) strncat(log_entry[trys], ttyn, TTYN_SIZE);
7520Sstevel@tonic-gate 		(void) strncat(log_entry[trys], ":", (size_t)1);
7530Sstevel@tonic-gate 		(void) strncat(log_entry[trys], ctime(&timenow),
7540Sstevel@tonic-gate 				TIME_SIZE);
7550Sstevel@tonic-gate 		trys++;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	if (count > flogin) {
7580Sstevel@tonic-gate 		if ((pwd = getpwnam(user_name)) != NULL) {
7590Sstevel@tonic-gate 			if (remote_host[0]) {
7600Sstevel@tonic-gate 				syslog(LOG_NOTICE,
7610Sstevel@tonic-gate 				    "Login failure on %s from %.*s, "
7620Sstevel@tonic-gate 				    "%.*s", ttyn, HMAX, remote_host,
7630Sstevel@tonic-gate 				    NMAX, user_name);
7640Sstevel@tonic-gate 			} else {
7650Sstevel@tonic-gate 				syslog(LOG_NOTICE,
7660Sstevel@tonic-gate 				    "Login failure on %s, %.*s",
7670Sstevel@tonic-gate 				    ttyn, NMAX, user_name);
7680Sstevel@tonic-gate 			}
7690Sstevel@tonic-gate 		} else 	{
7700Sstevel@tonic-gate 			if (remote_host[0]) {
7710Sstevel@tonic-gate 				syslog(LOG_NOTICE,
7720Sstevel@tonic-gate 				    "Login failure on %s from %.*s",
7730Sstevel@tonic-gate 				    ttyn, HMAX, remote_host);
7740Sstevel@tonic-gate 			} else {
7750Sstevel@tonic-gate 				syslog(LOG_NOTICE,
7760Sstevel@tonic-gate 				    "Login failure on %s", ttyn);
7770Sstevel@tonic-gate 			}
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate  * turn_on_logging 	- if the logfile exist, turn on attempt logging and
7850Sstevel@tonic-gate  *			  initialize the string storage area
7860Sstevel@tonic-gate  */
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate static void
7890Sstevel@tonic-gate turn_on_logging(void)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate 	struct stat dbuf;
7920Sstevel@tonic-gate 	int i;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	if (stat(LOGINLOG, &dbuf) == 0) {
7950Sstevel@tonic-gate 		writelog = 1;
7960Sstevel@tonic-gate 		for (i = 0; i < LOGTRYS; i++) {
7970Sstevel@tonic-gate 			if (!(log_entry[i] = malloc((size_t)ENT_SIZE))) {
7980Sstevel@tonic-gate 				writelog = 0;
7990Sstevel@tonic-gate 				break;
8000Sstevel@tonic-gate 			}
8010Sstevel@tonic-gate 			*log_entry[i] = '\0';
8020Sstevel@tonic-gate 		}
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
8080Sstevel@tonic-gate /*
8090Sstevel@tonic-gate  * login_conv():
8100Sstevel@tonic-gate  *	This is the conv (conversation) function called from
8110Sstevel@tonic-gate  *	a PAM authentication module to print error messages
8120Sstevel@tonic-gate  *	or garner information from the user.
8130Sstevel@tonic-gate  */
8140Sstevel@tonic-gate /*ARGSUSED*/
8150Sstevel@tonic-gate static int
8160Sstevel@tonic-gate login_conv(int num_msg, struct pam_message **msg,
8170Sstevel@tonic-gate     struct pam_response **response, void *appdata_ptr)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	struct pam_message	*m;
8200Sstevel@tonic-gate 	struct pam_response	*r;
8210Sstevel@tonic-gate 	char 			*temp;
8220Sstevel@tonic-gate 	int			k, i;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (num_msg <= 0)
8250Sstevel@tonic-gate 		return (PAM_CONV_ERR);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	*response = calloc(num_msg, sizeof (struct pam_response));
8280Sstevel@tonic-gate 	if (*response == NULL)
8290Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	k = num_msg;
8320Sstevel@tonic-gate 	m = *msg;
8330Sstevel@tonic-gate 	r = *response;
8340Sstevel@tonic-gate 	while (k--) {
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		switch (m->msg_style) {
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
8392515Sas145665 			errno = 0;
8400Sstevel@tonic-gate 			temp = getpassphrase(m->msg);
8410Sstevel@tonic-gate 			if (temp != NULL) {
8422515Sas145665 				if (errno == EINTR)
8432515Sas145665 					return (PAM_CONV_ERR);
8442515Sas145665 
8450Sstevel@tonic-gate 				r->resp = strdup(temp);
8460Sstevel@tonic-gate 				if (r->resp == NULL) {
8470Sstevel@tonic-gate 					/* free responses */
8480Sstevel@tonic-gate 					r = *response;
8490Sstevel@tonic-gate 					for (i = 0; i < num_msg; i++, r++) {
8500Sstevel@tonic-gate 						if (r->resp)
8510Sstevel@tonic-gate 							free(r->resp);
8520Sstevel@tonic-gate 					}
8530Sstevel@tonic-gate 					free(*response);
8540Sstevel@tonic-gate 					*response = NULL;
8550Sstevel@tonic-gate 					return (PAM_BUF_ERR);
8560Sstevel@tonic-gate 				}
8570Sstevel@tonic-gate 			}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 			m++;
8600Sstevel@tonic-gate 			r++;
8610Sstevel@tonic-gate 			break;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
8640Sstevel@tonic-gate 			if (m->msg != NULL)
8650Sstevel@tonic-gate 				(void) fputs(m->msg, stdout);
8660Sstevel@tonic-gate 			r->resp = calloc(1, PAM_MAX_RESP_SIZE);
8670Sstevel@tonic-gate 			if (r->resp == NULL) {
8680Sstevel@tonic-gate 				/* free responses */
8690Sstevel@tonic-gate 				r = *response;
8700Sstevel@tonic-gate 				for (i = 0; i < num_msg; i++, r++) {
8710Sstevel@tonic-gate 					if (r->resp)
8720Sstevel@tonic-gate 						free(r->resp);
8730Sstevel@tonic-gate 				}
8740Sstevel@tonic-gate 				free(*response);
8750Sstevel@tonic-gate 				*response = NULL;
8760Sstevel@tonic-gate 				return (PAM_BUF_ERR);
8770Sstevel@tonic-gate 			}
8780Sstevel@tonic-gate 			/*
8790Sstevel@tonic-gate 			 * The response might include environment variables
8800Sstevel@tonic-gate 			 * information. We should store that information in
8810Sstevel@tonic-gate 			 * envp if there is any; otherwise, envp is set to
8820Sstevel@tonic-gate 			 * NULL.
8830Sstevel@tonic-gate 			 */
8840Sstevel@tonic-gate 			bzero((void *)inputline, MAXLINE);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			envp = getargs(inputline);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			/* If we read in any input, process it. */
8890Sstevel@tonic-gate 			if (inputline[0] != '\0') {
8900Sstevel@tonic-gate 				int len;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 				if (envp != (char **)NULL)
8930Sstevel@tonic-gate 					/*
8940Sstevel@tonic-gate 					 * If getargs() did not return NULL,
8950Sstevel@tonic-gate 					 * *envp is the first string in
8960Sstevel@tonic-gate 					 * inputline. envp++ makes envp point
8970Sstevel@tonic-gate 					 * to environment variables information
8980Sstevel@tonic-gate 					 *  or be NULL.
8990Sstevel@tonic-gate 					 */
9000Sstevel@tonic-gate 					envp++;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 				(void) strncpy(r->resp, inputline,
9030Sstevel@tonic-gate 					PAM_MAX_RESP_SIZE-1);
9040Sstevel@tonic-gate 				r->resp[PAM_MAX_RESP_SIZE-1] = NULL;
9050Sstevel@tonic-gate 				len = strlen(r->resp);
9060Sstevel@tonic-gate 				if (r->resp[len-1] == '\n')
9070Sstevel@tonic-gate 					r->resp[len-1] = '\0';
9080Sstevel@tonic-gate 			} else {
9090Sstevel@tonic-gate 				login_exit(1);
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 			m++;
9120Sstevel@tonic-gate 			r++;
9130Sstevel@tonic-gate 			break;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		case PAM_ERROR_MSG:
9160Sstevel@tonic-gate 			if (m->msg != NULL) {
9170Sstevel@tonic-gate 				(void) fputs(m->msg, stderr);
9180Sstevel@tonic-gate 				(void) fputs("\n", stderr);
9190Sstevel@tonic-gate 			}
9200Sstevel@tonic-gate 			m++;
9210Sstevel@tonic-gate 			r++;
9220Sstevel@tonic-gate 			break;
9230Sstevel@tonic-gate 		case PAM_TEXT_INFO:
9240Sstevel@tonic-gate 			if (m->msg != NULL) {
9250Sstevel@tonic-gate 				(void) fputs(m->msg, stdout);
9260Sstevel@tonic-gate 				(void) fputs("\n", stdout);
9270Sstevel@tonic-gate 			}
9280Sstevel@tonic-gate 			m++;
9290Sstevel@tonic-gate 			r++;
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		default:
9330Sstevel@tonic-gate 			break;
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	return (PAM_SUCCESS);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * verify_passwd - Authenticates the user.
9410Sstevel@tonic-gate  *	Returns: PAM_SUCCESS if authentication successful,
9420Sstevel@tonic-gate  *		 PAM error code if authentication fails.
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate static int
946523Sbasabi verify_passwd(void)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	int error;
9490Sstevel@tonic-gate 	char *user;
9500Sstevel@tonic-gate 	int flag = 0;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * PAM authenticates the user for us.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	if ((error = pam_authenticate(pamh, flag)) != PAM_SUCCESS) {
9560Sstevel@tonic-gate 		return (error);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	/* get the user_name from the pam handle */
9600Sstevel@tonic-gate 	(void) pam_get_item(pamh, PAM_USER, (void**)&user);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	if (user == NULL || *user == '\0')
9630Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	SCPYL(user_name, user);
9660Sstevel@tonic-gate 	check_for_dueling_unix(user_name);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if ((pwd = getpwnam(user_name)) == NULL) {
9690Sstevel@tonic-gate 		return (PAM_SYSTEM_ERR);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	return (error);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate /*
9770Sstevel@tonic-gate  * quotec		- Called by getargs
9780Sstevel@tonic-gate  */
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate static int
9810Sstevel@tonic-gate quotec(void)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	int c, i, num;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	switch (c = getc(stdin)) {
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		case 'n':
9880Sstevel@tonic-gate 			c = '\n';
9890Sstevel@tonic-gate 			break;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		case 'r':
9920Sstevel@tonic-gate 			c = '\r';
9930Sstevel@tonic-gate 			break;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		case 'v':
9960Sstevel@tonic-gate 			c = '\013';
9970Sstevel@tonic-gate 			break;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 		case 'b':
10000Sstevel@tonic-gate 			c = '\b';
10010Sstevel@tonic-gate 			break;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		case 't':
10040Sstevel@tonic-gate 			c = '\t';
10050Sstevel@tonic-gate 			break;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		case 'f':
10080Sstevel@tonic-gate 			c = '\f';
10090Sstevel@tonic-gate 			break;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 		case '0':
10120Sstevel@tonic-gate 		case '1':
10130Sstevel@tonic-gate 		case '2':
10140Sstevel@tonic-gate 		case '3':
10150Sstevel@tonic-gate 		case '4':
10160Sstevel@tonic-gate 		case '5':
10170Sstevel@tonic-gate 		case '6':
10180Sstevel@tonic-gate 		case '7':
10190Sstevel@tonic-gate 			for (num = 0, i = 0; i < 3; i++) {
10200Sstevel@tonic-gate 				num = num * 8 + (c - '0');
10210Sstevel@tonic-gate 				if ((c = getc(stdin)) < '0' || c > '7')
10220Sstevel@tonic-gate 					break;
10230Sstevel@tonic-gate 			}
10240Sstevel@tonic-gate 			(void) ungetc(c, stdin);
10250Sstevel@tonic-gate 			c = num & 0377;
10260Sstevel@tonic-gate 			break;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		default:
10290Sstevel@tonic-gate 			break;
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 	return (c);
10320Sstevel@tonic-gate }
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate /*
10350Sstevel@tonic-gate  * getargs		- returns an input line.  Exits if EOF encountered.
10360Sstevel@tonic-gate  */
10370Sstevel@tonic-gate #define	WHITESPACE	0
10380Sstevel@tonic-gate #define	ARGUMENT	1
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate static char **
10410Sstevel@tonic-gate getargs(char *input_line)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate 	static char envbuf[MAXLINE];
10440Sstevel@tonic-gate 	static char *args[MAXARGS];
10450Sstevel@tonic-gate 	char *ptr, **answer;
10460Sstevel@tonic-gate 	int c;
10470Sstevel@tonic-gate 	int state;
10480Sstevel@tonic-gate 	char *p = input_line;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	ptr = envbuf;
10510Sstevel@tonic-gate 	answer = &args[0];
10520Sstevel@tonic-gate 	state = WHITESPACE;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	while ((c = getc(stdin)) != EOF && answer < &args[MAXARGS-1]) {
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		*(input_line++) = c;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		switch (c) {
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		case '\n':
10610Sstevel@tonic-gate 			if (ptr == &envbuf[0])
10620Sstevel@tonic-gate 				return ((char **)NULL);
10630Sstevel@tonic-gate 			*input_line = *ptr = '\0';
10640Sstevel@tonic-gate 			*answer = NULL;
10650Sstevel@tonic-gate 			return (&args[0]);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		case ' ':
10680Sstevel@tonic-gate 		case '\t':
10690Sstevel@tonic-gate 			if (state == ARGUMENT) {
10700Sstevel@tonic-gate 				*ptr++ = '\0';
10710Sstevel@tonic-gate 				state = WHITESPACE;
10720Sstevel@tonic-gate 			}
10730Sstevel@tonic-gate 			break;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		case '\\':
10760Sstevel@tonic-gate 			c = quotec();
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		default:
10790Sstevel@tonic-gate 			if (state == WHITESPACE) {
10800Sstevel@tonic-gate 				*answer++ = ptr;
10810Sstevel@tonic-gate 				state = ARGUMENT;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			*ptr++ = c;
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		/* Attempt at overflow, exit */
10870Sstevel@tonic-gate 		if (input_line - p >= MAXLINE - 1 ||
10880Sstevel@tonic-gate 		    ptr >= &envbuf[sizeof (envbuf) - 1]) {
10890Sstevel@tonic-gate 			audit_error = ADT_FAIL_VALUE_INPUT_OVERFLOW;
10900Sstevel@tonic-gate 			login_exit(1);
10910Sstevel@tonic-gate 		}
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * If we left loop because an EOF was received or we've overflown
10960Sstevel@tonic-gate 	 * args[], exit immediately.
10970Sstevel@tonic-gate 	 */
10980Sstevel@tonic-gate 	login_exit(0);
10990Sstevel@tonic-gate 	/* NOTREACHED */
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * get_user_name	- Gets the user name either passed in, or from the
11040Sstevel@tonic-gate  *			  login: prompt.
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate static void
1108523Sbasabi get_user_name(void)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	FILE	*fp;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	if ((fp = fopen(ISSUEFILE, "r")) != NULL) {
11130Sstevel@tonic-gate 		char    *ptr, buffer[BUFSIZ];
11140Sstevel@tonic-gate 		while ((ptr = fgets(buffer, sizeof (buffer),
11150Sstevel@tonic-gate 					fp)) != NULL) {
11160Sstevel@tonic-gate 			(void) fputs(ptr, stdout);
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 		(void) fclose(fp);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	/*
11220Sstevel@tonic-gate 	 * if TTYPROMPT is not set, use our own prompt
11230Sstevel@tonic-gate 	 * otherwise, use ttyprompt. We just set PAM_USER_PROMPT
11240Sstevel@tonic-gate 	 * and let the module do the prompting.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if ((ttyprompt == NULL) || (*ttyprompt == '\0'))
11280Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_USER_PROMPT, (void *)loginmsg);
11290Sstevel@tonic-gate 	else
11300Sstevel@tonic-gate 		(void) pam_set_item(pamh, PAM_USER_PROMPT, (void *)ttyprompt);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	envp = &zero; /* XXX: is this right? */
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate  * Check_for_dueling_unix   -	Check to see if the another login is talking
11380Sstevel@tonic-gate  *				to the line we've got open as a login port
11390Sstevel@tonic-gate  *				Exits if we're talking to another unix system
11400Sstevel@tonic-gate  */
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate static void
11430Sstevel@tonic-gate check_for_dueling_unix(char *inputline)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	if (EQN(loginmsg, inputline) || EQN(passwdmsg, inputline) ||
11460Sstevel@tonic-gate 	    EQN(incorrectmsg, inputline)) {
11470Sstevel@tonic-gate 		(void) printf("Looking at a login line.\n");
11480Sstevel@tonic-gate 		login_exit(8);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * logins_disabled - 	if the file /etc/nologin exists and the user is not
11540Sstevel@tonic-gate  *			root then do not permit them to login
11550Sstevel@tonic-gate  */
11560Sstevel@tonic-gate static int
11570Sstevel@tonic-gate logins_disabled(char *user_name)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	FILE	*nlfd;
11600Sstevel@tonic-gate 	int	c;
11610Sstevel@tonic-gate 	if (!EQN("root", user_name) &&
11620Sstevel@tonic-gate 			((nlfd = fopen(NOLOGIN, "r")) != (FILE *)NULL)) {
11630Sstevel@tonic-gate 		while ((c = getc(nlfd)) != EOF)
11640Sstevel@tonic-gate 			(void) putchar(c);
11650Sstevel@tonic-gate 		(void) fflush(stdout);
11660Sstevel@tonic-gate 		(void) sleep(5);
11670Sstevel@tonic-gate 		return (TRUE);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	return (FALSE);
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate /*
11730Sstevel@tonic-gate  * check_for_console -  Checks if we're getting a root login on the
11740Sstevel@tonic-gate  *			console, or a login from the global zone.  Exits if not.
11750Sstevel@tonic-gate  *
11760Sstevel@tonic-gate  */
11770Sstevel@tonic-gate static void
11780Sstevel@tonic-gate check_for_console(void)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	if (pwd != NULL && pwd->pw_uid == 0 && zflag == B_FALSE) {
11810Sstevel@tonic-gate 		if ((Console != NULL) && (strcmp(ttyn, Console) != 0)) {
11820Sstevel@tonic-gate 			(void) printf("Not on system console\n");
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 			audit_error = ADT_FAIL_VALUE_CONSOLE;
11850Sstevel@tonic-gate 			login_exit(10);
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate /*
11910Sstevel@tonic-gate  * List of environment variables or environment variable prefixes that should
11920Sstevel@tonic-gate  * not be propagated across logins, such as when the login -p option is used.
11930Sstevel@tonic-gate  */
11940Sstevel@tonic-gate static const char *const illegal[] = {
11950Sstevel@tonic-gate 	"SHELL=",
11960Sstevel@tonic-gate 	"HOME=",
11970Sstevel@tonic-gate 	"LOGNAME=",
11980Sstevel@tonic-gate #ifndef	NO_MAIL
11990Sstevel@tonic-gate 	"MAIL=",
12000Sstevel@tonic-gate #endif
12010Sstevel@tonic-gate 	"CDPATH=",
12020Sstevel@tonic-gate 	"IFS=",
12030Sstevel@tonic-gate 	"PATH=",
12040Sstevel@tonic-gate 	"LD_",
12050Sstevel@tonic-gate 	"SMF_",
12060Sstevel@tonic-gate 	NULL
12070Sstevel@tonic-gate };
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate /*
12100Sstevel@tonic-gate  * legalenvvar		- Is it legal to insert this environmental variable?
12110Sstevel@tonic-gate  */
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate static int
12140Sstevel@tonic-gate legalenvvar(char *s)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	const char *const *p;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	for (p = &illegal[0]; *p; p++) {
12190Sstevel@tonic-gate 		if (strncmp(s, *p, strlen(*p)) == 0)
12200Sstevel@tonic-gate 			return (0);
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	return (1);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * getstr		- Get a string from standard input
12290Sstevel@tonic-gate  *			  Calls exit if read(2) fails.
12300Sstevel@tonic-gate  */
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate static void
12330Sstevel@tonic-gate getstr(char *buf, int cnt, char *err)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	char c;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	do {
12380Sstevel@tonic-gate 		if (read(0, &c, 1) != 1)
12390Sstevel@tonic-gate 			login_exit(1);
12400Sstevel@tonic-gate 		*buf++ = c;
12410Sstevel@tonic-gate 	} while (--cnt > 1 && c != 0);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	*buf = 0;
12440Sstevel@tonic-gate 	err = err; 	/* For lint */
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * defaults 		- read defaults
12500Sstevel@tonic-gate  */
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate static void
12530Sstevel@tonic-gate defaults(void)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	int  flags;
12560Sstevel@tonic-gate 	char *ptr;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (defopen(Pndefault) == 0) {
12590Sstevel@tonic-gate 		/*
12600Sstevel@tonic-gate 		 * ignore case
12610Sstevel@tonic-gate 		 */
12620Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
12630Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
12640Sstevel@tonic-gate 		(void) defcntl(DC_SETFLAGS, flags);
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 		if ((Console = defread("CONSOLE=")) != NULL)
12670Sstevel@tonic-gate 			Console = strdup(Console);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 		if ((Altshell = defread("ALTSHELL=")) != NULL)
12700Sstevel@tonic-gate 			Altshell = strdup(Altshell);
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 		if ((ptr = defread("PASSREQ=")) != NULL &&
12730Sstevel@tonic-gate 		    strcasecmp("YES", ptr) == 0)
12740Sstevel@tonic-gate 				Passreqflag = 1;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		if ((Def_tz = defread("TIMEZONE=")) != NULL)
12770Sstevel@tonic-gate 			Def_tz = strdup(Def_tz);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		if ((Def_hertz = defread("HZ=")) != NULL)
12800Sstevel@tonic-gate 			Def_hertz = strdup(Def_hertz);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		if ((Def_path   = defread("PATH=")) != NULL)
12830Sstevel@tonic-gate 			Def_path = strdup(Def_path);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		if ((Def_supath = defread("SUPATH=")) != NULL)
12860Sstevel@tonic-gate 			Def_supath = strdup(Def_supath);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		if ((ptr = defread("ULIMIT=")) != NULL)
12890Sstevel@tonic-gate 			Def_ulimit = atol(ptr);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 		if ((ptr = defread("TIMEOUT=")) != NULL)
12920Sstevel@tonic-gate 			Def_timeout = (unsigned)atoi(ptr);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		if ((ptr = defread("UMASK=")) != NULL)
12950Sstevel@tonic-gate 			if (sscanf(ptr, "%lo", &Umask) != 1)
12960Sstevel@tonic-gate 				Umask = DEFUMASK;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		if ((ptr = defread("SLEEPTIME=")) != NULL) {
12990Sstevel@tonic-gate 			if (is_number(ptr))
13000Sstevel@tonic-gate 				Sleeptime = atoi(ptr);
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		if ((ptr = defread("DISABLETIME=")) != NULL) {
13040Sstevel@tonic-gate 			if (is_number(ptr))
13050Sstevel@tonic-gate 				Disabletime = atoi(ptr);
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 		if ((ptr = defread("SYSLOG=")) != NULL)
13090Sstevel@tonic-gate 			dosyslog = strcmp(ptr, "YES") == 0;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 		if ((ptr = defread("RETRIES=")) != NULL) {
13120Sstevel@tonic-gate 			if (is_number(ptr))
13130Sstevel@tonic-gate 				retry = atoi(ptr);
13140Sstevel@tonic-gate 		}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 		if ((ptr = defread("SYSLOG_FAILED_LOGINS=")) != NULL) {
13170Sstevel@tonic-gate 			if (is_number(ptr))
13180Sstevel@tonic-gate 				flogin = atoi(ptr);
13190Sstevel@tonic-gate 			else
13200Sstevel@tonic-gate 				flogin = retry;
13210Sstevel@tonic-gate 		} else
13220Sstevel@tonic-gate 			flogin = retry;
13230Sstevel@tonic-gate 		(void) defopen((char *)NULL);
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate }
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate /*
13290Sstevel@tonic-gate  * get_options(argc, argv)
13300Sstevel@tonic-gate  * 			- parse the cmd line.
13310Sstevel@tonic-gate  *			- return 0 if successful, -1 if failed.
13320Sstevel@tonic-gate  *			Calls login_exit() on misuse of -r, -h, and -z flags
13330Sstevel@tonic-gate  */
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate static	int
13360Sstevel@tonic-gate get_options(int argc, char *argv[])
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	int	c;
13390Sstevel@tonic-gate 	int	errflg = 0;
13400Sstevel@tonic-gate 	char    sflagname[NMAX+1];
13410Sstevel@tonic-gate 	const 	char *flags_message = "Only one of -r, -h and -z allowed\n";
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "u:s:R:f:h:r:pad:t:U:z:")) != -1) {
13440Sstevel@tonic-gate 		switch (c) {
13450Sstevel@tonic-gate 		case 'a':
13460Sstevel@tonic-gate 			break;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 		case 'd':
13490Sstevel@tonic-gate 			/*
13500Sstevel@tonic-gate 			 * Must be root to pass in device name
13510Sstevel@tonic-gate 			 * otherwise we exit() as punishment for trying.
13520Sstevel@tonic-gate 			 */
13530Sstevel@tonic-gate 			if (getuid() != 0 || geteuid() != 0) {
13540Sstevel@tonic-gate 				audit_error = ADT_FAIL_VALUE_DEVICE_PERM;
13550Sstevel@tonic-gate 				login_exit(1);	/* sigh */
13560Sstevel@tonic-gate 				/*NOTREACHED*/
13570Sstevel@tonic-gate 			}
13580Sstevel@tonic-gate 			ttyn = optarg;
13590Sstevel@tonic-gate 			break;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		case 'h':
13620Sstevel@tonic-gate 			if (hflag || rflag || zflag) {
13630Sstevel@tonic-gate 				(void) fprintf(stderr, flags_message);
13640Sstevel@tonic-gate 				login_exit(1);
13650Sstevel@tonic-gate 			}
13660Sstevel@tonic-gate 			hflag = B_TRUE;
13670Sstevel@tonic-gate 			SCPYL(remote_host, optarg);
13680Sstevel@tonic-gate 			if (argv[optind]) {
13690Sstevel@tonic-gate 				if (argv[optind][0] != '-') {
13700Sstevel@tonic-gate 					SCPYL(terminal, argv[optind]);
13710Sstevel@tonic-gate 					optind++;
13720Sstevel@tonic-gate 				} else {
13730Sstevel@tonic-gate 					/*
13740Sstevel@tonic-gate 					 * Allow "login -h hostname -" to
13750Sstevel@tonic-gate 					 * skip setting up an username as "-".
13760Sstevel@tonic-gate 					 */
13770Sstevel@tonic-gate 					if (argv[optind][1] == '\0')
13780Sstevel@tonic-gate 						optind++;
13790Sstevel@tonic-gate 				}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 			}
13820Sstevel@tonic-gate 			SCPYL(progname, "telnet");
13830Sstevel@tonic-gate 			break;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 		case 'r':
13860Sstevel@tonic-gate 			if (hflag || rflag || zflag) {
13870Sstevel@tonic-gate 				(void) fprintf(stderr, flags_message);
13880Sstevel@tonic-gate 				login_exit(1);
13890Sstevel@tonic-gate 			}
13900Sstevel@tonic-gate 			rflag = B_TRUE;
13910Sstevel@tonic-gate 			SCPYL(remote_host, optarg);
13920Sstevel@tonic-gate 			SCPYL(progname, "rlogin");
13930Sstevel@tonic-gate 			break;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		case 'p':
13960Sstevel@tonic-gate 			pflag = B_TRUE;
13970Sstevel@tonic-gate 			break;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		case 'f':
14000Sstevel@tonic-gate 			/*
14010Sstevel@tonic-gate 			 * Must be root to bypass authentication
14020Sstevel@tonic-gate 			 * otherwise we exit() as punishment for trying.
14030Sstevel@tonic-gate 			 */
14040Sstevel@tonic-gate 			if (getuid() != 0 || geteuid() != 0) {
14050Sstevel@tonic-gate 				audit_error = ADT_FAIL_VALUE_AUTH_BYPASS;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 				login_exit(1);	/* sigh */
14080Sstevel@tonic-gate 				/*NOTREACHED*/
14090Sstevel@tonic-gate 			}
14100Sstevel@tonic-gate 			/* save fflag user name for future use */
14110Sstevel@tonic-gate 			SCPYL(user_name, optarg);
14120Sstevel@tonic-gate 			fflag = B_TRUE;
14130Sstevel@tonic-gate 			break;
14140Sstevel@tonic-gate 		case 'u':
14150Sstevel@tonic-gate 			if (!strlen(optarg)) {
14160Sstevel@tonic-gate 				(void) fprintf(stderr,
14170Sstevel@tonic-gate 					"Empty string supplied with -u\n");
14180Sstevel@tonic-gate 				login_exit(1);
14190Sstevel@tonic-gate 			}
14200Sstevel@tonic-gate 			SCPYL(identity, optarg);
14210Sstevel@tonic-gate 			uflag = B_TRUE;
14220Sstevel@tonic-gate 			break;
14230Sstevel@tonic-gate 		case 's':
14240Sstevel@tonic-gate 			if (!strlen(optarg)) {
14250Sstevel@tonic-gate 				(void) fprintf(stderr,
14260Sstevel@tonic-gate 					"Empty string supplied with -s\n");
14270Sstevel@tonic-gate 				login_exit(1);
14280Sstevel@tonic-gate 			}
14290Sstevel@tonic-gate 			SCPYL(sflagname, optarg);
14300Sstevel@tonic-gate 			sflag = B_TRUE;
14310Sstevel@tonic-gate 			break;
14320Sstevel@tonic-gate 		case 'R':
14330Sstevel@tonic-gate 			if (!strlen(optarg)) {
14340Sstevel@tonic-gate 				(void) fprintf(stderr,
14350Sstevel@tonic-gate 					"Empty string supplied with -R\n");
14360Sstevel@tonic-gate 				login_exit(1);
14370Sstevel@tonic-gate 			}
14380Sstevel@tonic-gate 			SCPYL(repository, optarg);
14390Sstevel@tonic-gate 			Rflag =	B_TRUE;
14400Sstevel@tonic-gate 			break;
14410Sstevel@tonic-gate 		case 't':
14420Sstevel@tonic-gate 			if (!strlen(optarg)) {
14430Sstevel@tonic-gate 				(void) fprintf(stderr,
14440Sstevel@tonic-gate 					"Empty string supplied with -t\n");
14450Sstevel@tonic-gate 				login_exit(1);
14460Sstevel@tonic-gate 			}
14470Sstevel@tonic-gate 			SCPYL(terminal, optarg);
14480Sstevel@tonic-gate 			tflag = B_TRUE;
14490Sstevel@tonic-gate 			break;
14500Sstevel@tonic-gate 		case 'U':
14510Sstevel@tonic-gate 			/*
14520Sstevel@tonic-gate 			 * Kerberized rlogind may fork us with
14530Sstevel@tonic-gate 			 * -U "" if the rlogin client used the "-a"
14540Sstevel@tonic-gate 			 * option to send a NULL username.  This is done
14550Sstevel@tonic-gate 			 * to force login to prompt for a user/password.
14560Sstevel@tonic-gate 			 * However, if Kerberos auth was used, we dont need
14570Sstevel@tonic-gate 			 * to prompt, so we will accept the option and
14580Sstevel@tonic-gate 			 * handle the situation later.
14590Sstevel@tonic-gate 			 */
14600Sstevel@tonic-gate 			SCPYL(rusername, optarg);
14610Sstevel@tonic-gate 			Uflag = B_TRUE;
14620Sstevel@tonic-gate 			break;
14630Sstevel@tonic-gate 		case 'z':
14640Sstevel@tonic-gate 			if (hflag || rflag || zflag) {
14650Sstevel@tonic-gate 				(void) fprintf(stderr, flags_message);
14660Sstevel@tonic-gate 				login_exit(1);
14670Sstevel@tonic-gate 			}
14680Sstevel@tonic-gate 			(void) snprintf(zone_name, sizeof (zone_name),
14690Sstevel@tonic-gate 			    "zone:%s", optarg);
14700Sstevel@tonic-gate 			SCPYL(progname, "zlogin");
14710Sstevel@tonic-gate 			zflag = B_TRUE;
14720Sstevel@tonic-gate 			break;
14730Sstevel@tonic-gate 		default:
14740Sstevel@tonic-gate 			errflg++;
14750Sstevel@tonic-gate 			break;
14760Sstevel@tonic-gate 		} 	/* end switch */
14770Sstevel@tonic-gate 	} 		/* end while */
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	/*
14800Sstevel@tonic-gate 	 * If the 's svcname' flag was used, override the progname
14810Sstevel@tonic-gate 	 * value that is to be used in the pam_start call.
14820Sstevel@tonic-gate 	 */
14830Sstevel@tonic-gate 	if (sflag)
14840Sstevel@tonic-gate 		SCPYL(progname, sflagname);
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	/*
14870Sstevel@tonic-gate 	 * get the prompt set by ttymon
14880Sstevel@tonic-gate 	 */
14890Sstevel@tonic-gate 	ttyprompt = getenv("TTYPROMPT");
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if ((ttyprompt != NULL) && (*ttyprompt != '\0')) {
14920Sstevel@tonic-gate 		/*
14930Sstevel@tonic-gate 		 * if ttyprompt is set, there should be data on
14940Sstevel@tonic-gate 		 * the stream already.
14950Sstevel@tonic-gate 		 */
14960Sstevel@tonic-gate 		if ((envp = getargs(inputline)) != (char **)NULL) {
14970Sstevel@tonic-gate 			/*
14980Sstevel@tonic-gate 			 * don't get name if name passed as argument.
14990Sstevel@tonic-gate 			 */
15000Sstevel@tonic-gate 			SCPYL(user_name, *envp++);
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 	} else if (optind < argc) {
15030Sstevel@tonic-gate 		SCPYL(user_name, argv[optind]);
15040Sstevel@tonic-gate 		(void) SCPYL(inputline, user_name);
15050Sstevel@tonic-gate 		(void) strlcat(inputline, "   \n", sizeof (inputline));
15060Sstevel@tonic-gate 		envp = &argv[optind+1];
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		if (!fflag)
15090Sstevel@tonic-gate 			SCPYL(lusername, user_name);
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (errflg)
15130Sstevel@tonic-gate 		return (-1);
15140Sstevel@tonic-gate 	return (0);
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate /*
15180Sstevel@tonic-gate  * usage		- Print usage message
15190Sstevel@tonic-gate  *
15200Sstevel@tonic-gate  */
15210Sstevel@tonic-gate static void
15220Sstevel@tonic-gate usage(void)
15230Sstevel@tonic-gate {
15240Sstevel@tonic-gate 	(void) fprintf(stderr,
15250Sstevel@tonic-gate 	    "usage:\n"
15260Sstevel@tonic-gate 	    "    login [-p] [-d device] [-R repository] [-s service]\n"
15270Sstevel@tonic-gate 	    "\t[-t terminal]  [-u identity] [-U ruser]\n"
15280Sstevel@tonic-gate 	    "\t[-h hostname [terminal] | -r hostname] [name [environ]...]\n");
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * doremoteterm		- Sets the appropriate ioctls for a remote terminal
15340Sstevel@tonic-gate  */
15350Sstevel@tonic-gate static char	*speeds[] = {
15360Sstevel@tonic-gate 	"0", "50", "75", "110", "134", "150", "200", "300",
15370Sstevel@tonic-gate 	"600", "1200", "1800", "2400", "4800", "9600", "19200", "38400",
15380Sstevel@tonic-gate 	"57600", "76800", "115200", "153600", "230400", "307200", "460800"
15390Sstevel@tonic-gate };
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate #define	NSPEEDS	(sizeof (speeds) / sizeof (speeds[0]))
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate static void
15450Sstevel@tonic-gate doremoteterm(char *term)
15460Sstevel@tonic-gate {
15470Sstevel@tonic-gate 	struct termios tp;
15480Sstevel@tonic-gate 	char *cp = strchr(term, '/'), **cpp;
15490Sstevel@tonic-gate 	char *speed;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	(void) ioctl(0, TCGETS, &tp);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if (cp) {
15540Sstevel@tonic-gate 		*cp++ = '\0';
15550Sstevel@tonic-gate 		speed = cp;
15560Sstevel@tonic-gate 		cp = strchr(speed, '/');
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 		if (cp)
15590Sstevel@tonic-gate 			*cp++ = '\0';
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
15620Sstevel@tonic-gate 			if (strcmp(*cpp, speed) == 0) {
15630Sstevel@tonic-gate 				(void) cfsetospeed(&tp, cpp-speeds);
15640Sstevel@tonic-gate 				break;
15650Sstevel@tonic-gate 			}
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	tp.c_lflag |= ECHO|ICANON;
15690Sstevel@tonic-gate 	tp.c_iflag |= IGNPAR|ICRNL;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	(void) ioctl(0, TCSETS, &tp);
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Process_rlogin		- Does the work that rlogin and telnet
15770Sstevel@tonic-gate  *				  need done
15780Sstevel@tonic-gate  */
15790Sstevel@tonic-gate static void
15800Sstevel@tonic-gate process_rlogin(void)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	/*
15830Sstevel@tonic-gate 	 * If a Kerberized rlogin was initiated, then these fields
15840Sstevel@tonic-gate 	 * must be read by rlogin daemon itself and passed down via
15850Sstevel@tonic-gate 	 * cmd line args.
15860Sstevel@tonic-gate 	 */
15870Sstevel@tonic-gate 	if (!Uflag && !strlen(rusername))
15880Sstevel@tonic-gate 		getstr(rusername, sizeof (rusername), "remuser");
15890Sstevel@tonic-gate 	if (!strlen(lusername))
15900Sstevel@tonic-gate 		getstr(lusername, sizeof (lusername), "locuser");
15910Sstevel@tonic-gate 	if (!tflag && !strlen(terminal))
15920Sstevel@tonic-gate 		getstr(terminal, sizeof (terminal), "Terminal type");
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	if (strlen(terminal))
15950Sstevel@tonic-gate 		doremoteterm(terminal);
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/* fflag has precedence over stuff passed by rlogind */
15980Sstevel@tonic-gate 	if (fflag || getuid()) {
15990Sstevel@tonic-gate 		pwd = &nouser;
16000Sstevel@tonic-gate 		return;
16010Sstevel@tonic-gate 	} else {
16020Sstevel@tonic-gate 		if (pam_set_item(pamh, PAM_USER, lusername) != PAM_SUCCESS)
16030Sstevel@tonic-gate 			login_exit(1);
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 		pwd = getpwnam(lusername);
16060Sstevel@tonic-gate 		if (pwd == NULL) {
16070Sstevel@tonic-gate 			pwd = &nouser;
16080Sstevel@tonic-gate 			return;
16090Sstevel@tonic-gate 		}
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/*
16130Sstevel@tonic-gate 	 * Update PAM on the user name
16140Sstevel@tonic-gate 	 */
16150Sstevel@tonic-gate 	if (strlen(lusername) &&
16160Sstevel@tonic-gate 	    pam_set_item(pamh, PAM_USER, lusername) != PAM_SUCCESS)
16170Sstevel@tonic-gate 		login_exit(1);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	if (strlen(rusername) &&
16200Sstevel@tonic-gate 	    pam_set_item(pamh, PAM_RUSER, rusername) != PAM_SUCCESS)
16210Sstevel@tonic-gate 		login_exit(1);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	SCPYL(user_name, lusername);
16240Sstevel@tonic-gate 	envp = &zero;
16250Sstevel@tonic-gate 	lusername[0] = '\0';
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate /*
16290Sstevel@tonic-gate  *		*** Account validation routines ***
16300Sstevel@tonic-gate  *
16310Sstevel@tonic-gate  */
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate /*
16340Sstevel@tonic-gate  * validate_account		- This is the PAM version of validate.
16350Sstevel@tonic-gate  */
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate static void
16380Sstevel@tonic-gate validate_account(void)
16390Sstevel@tonic-gate {
16400Sstevel@tonic-gate 	int 	error;
16410Sstevel@tonic-gate 	int	flag;
16420Sstevel@tonic-gate 	int	tries;		/* new password retries */
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	(void) alarm(0);	/* give user time to come up with password */
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	check_log();
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	if (Passreqflag)
16490Sstevel@tonic-gate 		flag = PAM_DISALLOW_NULL_AUTHTOK;
16500Sstevel@tonic-gate 	else
16510Sstevel@tonic-gate 		flag = 0;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	if ((error = pam_acct_mgmt(pamh, flag)) != PAM_SUCCESS) {
16540Sstevel@tonic-gate 		if (error == PAM_NEW_AUTHTOK_REQD) {
16550Sstevel@tonic-gate 			tries = 1;
16560Sstevel@tonic-gate 			error = PAM_AUTHTOK_ERR;
16570Sstevel@tonic-gate 			while (error == PAM_AUTHTOK_ERR &&
16580Sstevel@tonic-gate 					tries <= DEF_ATTEMPTS) {
16590Sstevel@tonic-gate 				if (tries > 1)
16600Sstevel@tonic-gate 					(void) printf("Try again\n\n");
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 				(void) printf("Choose a new password.\n");
16630Sstevel@tonic-gate 
16641419Sdarrenm 				error = pam_chauthtok(pamh,
16651419Sdarrenm 				    PAM_CHANGE_EXPIRED_AUTHTOK);
16660Sstevel@tonic-gate 				if (error == PAM_TRY_AGAIN) {
16670Sstevel@tonic-gate 					(void) sleep(1);
16681419Sdarrenm 					error = pam_chauthtok(pamh,
16691419Sdarrenm 					    PAM_CHANGE_EXPIRED_AUTHTOK);
16700Sstevel@tonic-gate 				}
16710Sstevel@tonic-gate 				tries++;
16720Sstevel@tonic-gate 			}
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 			if (error != PAM_SUCCESS) {
16750Sstevel@tonic-gate 				if (dosyslog)
16760Sstevel@tonic-gate 					syslog(LOG_CRIT,
16770Sstevel@tonic-gate 						"change password failure: %s",
16780Sstevel@tonic-gate 						pam_strerror(pamh, error));
16790Sstevel@tonic-gate 				audit_error = ADT_FAIL_PAM + error;
16800Sstevel@tonic-gate 				login_exit(1);
16810Sstevel@tonic-gate 			} else {
16820Sstevel@tonic-gate 				audit_success(ADT_passwd, pwd, zone_name);
16830Sstevel@tonic-gate 			}
16840Sstevel@tonic-gate 		} else {
16850Sstevel@tonic-gate 			(void) printf(incorrectmsg);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 			if (dosyslog)
16880Sstevel@tonic-gate 				syslog(LOG_CRIT,
16890Sstevel@tonic-gate 					"login account failure: %s",
16900Sstevel@tonic-gate 						pam_strerror(pamh, error));
16910Sstevel@tonic-gate 			audit_error = ADT_FAIL_PAM + error;
16920Sstevel@tonic-gate 			login_exit(1);
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate /*
16980Sstevel@tonic-gate  * Check_log	- This is really a hack because PAM checks the log, but login
16990Sstevel@tonic-gate  *		  wants to know if the log is okay and PAM doesn't have
17000Sstevel@tonic-gate  *		  a module independent way of handing this info back.
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate static void
17040Sstevel@tonic-gate check_log(void)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	int fdl;
17070Sstevel@tonic-gate 	long long offset;
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	offset = (long long) pwd->pw_uid * (long long) sizeof (struct lastlog);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
17120Sstevel@tonic-gate 		if (llseek(fdl, offset, SEEK_SET) == offset &&
17130Sstevel@tonic-gate 		    read(fdl, (char *)&ll, sizeof (ll)) == sizeof (ll) &&
17140Sstevel@tonic-gate 		    ll.ll_time != 0)
17150Sstevel@tonic-gate 			lastlogok = 1;
17160Sstevel@tonic-gate 		(void) close(fdl);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate /*
17210Sstevel@tonic-gate  * chdir_to_dir_user	- Now chdir after setuid/setgid have happened to
17220Sstevel@tonic-gate  *			  place us in the user's home directory just in
17230Sstevel@tonic-gate  *			  case it was protected and the first chdir failed.
17240Sstevel@tonic-gate  *			  No chdir errors should happen at this point because
17250Sstevel@tonic-gate  *			  all failures should have happened on the first
17260Sstevel@tonic-gate  *			  time around.
17270Sstevel@tonic-gate  */
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate static void
17300Sstevel@tonic-gate chdir_to_dir_user(void)
17310Sstevel@tonic-gate {
17320Sstevel@tonic-gate 	if (chdir(pwd->pw_dir) < 0) {
17330Sstevel@tonic-gate 		if (chdir("/") < 0) {
17340Sstevel@tonic-gate 			(void) printf("No directory!\n");
17350Sstevel@tonic-gate 			/*
17360Sstevel@tonic-gate 			 * This probably won't work since we can't get to /.
17370Sstevel@tonic-gate 			 */
17380Sstevel@tonic-gate 			if (dosyslog) {
17390Sstevel@tonic-gate 				if (remote_host[0]) {
17400Sstevel@tonic-gate 					syslog(LOG_CRIT,
17410Sstevel@tonic-gate 					    "LOGIN FAILURES ON %s FROM %.*s ",
17420Sstevel@tonic-gate 					    " %.*s", ttyn, HMAX,
17430Sstevel@tonic-gate 					    remote_host, NMAX, pwd->pw_name);
17440Sstevel@tonic-gate 				} else {
17450Sstevel@tonic-gate 					syslog(LOG_CRIT,
17460Sstevel@tonic-gate 					    "LOGIN FAILURES ON %s, %.*s",
17470Sstevel@tonic-gate 					    ttyn, NMAX, pwd->pw_name);
17480Sstevel@tonic-gate 				}
17490Sstevel@tonic-gate 			}
17500Sstevel@tonic-gate 			closelog();
17510Sstevel@tonic-gate 			(void) sleep(Disabletime);
17520Sstevel@tonic-gate 			exit(1);
17530Sstevel@tonic-gate 		} else {
17540Sstevel@tonic-gate 			(void) printf("No directory! Logging in with home=/\n");
17550Sstevel@tonic-gate 			pwd->pw_dir = "/";
17560Sstevel@tonic-gate 		}
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
17620Sstevel@tonic-gate /*
17630Sstevel@tonic-gate  * login_authenticate	- Performs the main authentication work
17640Sstevel@tonic-gate  *			  1. Prints the login prompt
17650Sstevel@tonic-gate  *			  2. Requests and verifys the password
17660Sstevel@tonic-gate  *			  3. Checks the port password
17670Sstevel@tonic-gate  */
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate static void
1770523Sbasabi login_authenticate(void)
17710Sstevel@tonic-gate {
17720Sstevel@tonic-gate 	char *user;
17730Sstevel@tonic-gate 	int err;
17740Sstevel@tonic-gate 	int login_successful = 0;
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	do {
17770Sstevel@tonic-gate 		/* if scheme broken, then nothing to do but quit */
17780Sstevel@tonic-gate 		if (pam_get_item(pamh, PAM_USER, (void **)&user)
17790Sstevel@tonic-gate 							!= PAM_SUCCESS)
17800Sstevel@tonic-gate 			exit(1);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 		/*
17830Sstevel@tonic-gate 		 * only get name from utility if it is not already
17840Sstevel@tonic-gate 		 * supplied by pam_start or a pam_set_item.
17850Sstevel@tonic-gate 		 */
17860Sstevel@tonic-gate 		if (!user || !user[0]) {
17870Sstevel@tonic-gate 			/* use call back to get user name */
17880Sstevel@tonic-gate 			get_user_name();
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		err = verify_passwd();
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		/*
17940Sstevel@tonic-gate 		 * If root login and not on system console then call exit(2)
17950Sstevel@tonic-gate 		 */
17960Sstevel@tonic-gate 		check_for_console();
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		switch (err) {
17990Sstevel@tonic-gate 		case PAM_SUCCESS:
18000Sstevel@tonic-gate 		case PAM_NEW_AUTHTOK_REQD:
18010Sstevel@tonic-gate 			/*
18020Sstevel@tonic-gate 			 * Officially, pam_authenticate() shouldn't return this
18030Sstevel@tonic-gate 			 * but it's probably the right thing to return if
18040Sstevel@tonic-gate 			 * PAM_DISALLOW_NULL_AUTHTOK is set so the user will
18050Sstevel@tonic-gate 			 * be forced to change password later in this code.
18060Sstevel@tonic-gate 			 */
18070Sstevel@tonic-gate 			count = 0;
18080Sstevel@tonic-gate 			login_successful = 1;
18090Sstevel@tonic-gate 			break;
18100Sstevel@tonic-gate 		case PAM_MAXTRIES:
18110Sstevel@tonic-gate 			count = retry;
18120Sstevel@tonic-gate 			/*FALLTHROUGH*/
18130Sstevel@tonic-gate 		case PAM_AUTH_ERR:
18140Sstevel@tonic-gate 		case PAM_AUTHINFO_UNAVAIL:
18150Sstevel@tonic-gate 		case PAM_USER_UNKNOWN:
18160Sstevel@tonic-gate 			audit_failure(get_audit_id(), ADT_FAIL_PAM + err, pwd,
18170Sstevel@tonic-gate 			    remote_host, ttyn, zone_name);
18180Sstevel@tonic-gate 			log_bad_attempts();
18190Sstevel@tonic-gate 			break;
18200Sstevel@tonic-gate 		case PAM_ABORT:
18210Sstevel@tonic-gate 			log_bad_attempts();
18220Sstevel@tonic-gate 			(void) sleep(Disabletime);
18230Sstevel@tonic-gate 			(void) printf(incorrectmsg);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 			audit_error = ADT_FAIL_PAM + err;
18260Sstevel@tonic-gate 			login_exit(1);
18270Sstevel@tonic-gate 			/*NOTREACHED*/
18280Sstevel@tonic-gate 		default:	/* Some other PAM error */
18290Sstevel@tonic-gate 			audit_error = ADT_FAIL_PAM + err;
18300Sstevel@tonic-gate 			login_exit(1);
18310Sstevel@tonic-gate 			/*NOTREACHED*/
18320Sstevel@tonic-gate 		}
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 		if (login_successful)
18350Sstevel@tonic-gate 			break;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		/* sleep after bad passwd */
18380Sstevel@tonic-gate 		if (count)
18390Sstevel@tonic-gate 			(void) sleep(Sleeptime);
18400Sstevel@tonic-gate 		(void) printf(incorrectmsg);
18410Sstevel@tonic-gate 		/* force name to be null in this case */
18420Sstevel@tonic-gate 		if (pam_set_item(pamh, PAM_USER, NULL) != PAM_SUCCESS)
18430Sstevel@tonic-gate 			login_exit(1);
18440Sstevel@tonic-gate 		if (pam_set_item(pamh, PAM_RUSER, NULL) != PAM_SUCCESS)
18450Sstevel@tonic-gate 			login_exit(1);
18460Sstevel@tonic-gate 	} while (count++ < retry);
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	if (count >= retry) {
18490Sstevel@tonic-gate 		audit_failure(get_audit_id(), ADT_FAIL_VALUE_MAX_TRIES, pwd,
18500Sstevel@tonic-gate 		    remote_host, ttyn, zone_name);
18510Sstevel@tonic-gate 		/*
18520Sstevel@tonic-gate 		 * If logging is turned on, output the
18530Sstevel@tonic-gate 		 * string storage area to the log file,
18540Sstevel@tonic-gate 		 * and sleep for Disabletime
18550Sstevel@tonic-gate 		 * seconds before exiting.
18560Sstevel@tonic-gate 		 */
18570Sstevel@tonic-gate 		if (writelog)
18580Sstevel@tonic-gate 			badlogin();
18590Sstevel@tonic-gate 		if (dosyslog) {
18600Sstevel@tonic-gate 			if ((pwd = getpwnam(user_name)) != NULL) {
18610Sstevel@tonic-gate 				if (remote_host[0]) {
18620Sstevel@tonic-gate 					syslog(LOG_CRIT,
18630Sstevel@tonic-gate 					"REPEATED LOGIN FAILURES ON %s FROM "
18640Sstevel@tonic-gate 						"%.*s, %.*s",
18650Sstevel@tonic-gate 					ttyn, HMAX, remote_host, NMAX,
18660Sstevel@tonic-gate 					user_name);
18670Sstevel@tonic-gate 				} else {
18680Sstevel@tonic-gate 					syslog(LOG_CRIT,
18690Sstevel@tonic-gate 					"REPEATED LOGIN FAILURES ON %s, %.*s",
18700Sstevel@tonic-gate 					ttyn, NMAX, user_name);
18710Sstevel@tonic-gate 				}
18720Sstevel@tonic-gate 			} else {
18730Sstevel@tonic-gate 				if (remote_host[0]) {
18740Sstevel@tonic-gate 					syslog(LOG_CRIT,
18750Sstevel@tonic-gate 					"REPEATED LOGIN FAILURES ON %s FROM "
18760Sstevel@tonic-gate 					"%.*s",
18770Sstevel@tonic-gate 					ttyn, HMAX, remote_host);
18780Sstevel@tonic-gate 				} else {
18790Sstevel@tonic-gate 					syslog(LOG_CRIT,
18800Sstevel@tonic-gate 					"REPEATED LOGIN FAILURES ON %s", ttyn);
18810Sstevel@tonic-gate 				}
18820Sstevel@tonic-gate 			}
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 		(void) sleep(Disabletime);
18850Sstevel@tonic-gate 		exit(1);
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate /*
18910Sstevel@tonic-gate  * 			*** Credential Related routines ***
18920Sstevel@tonic-gate  *
18930Sstevel@tonic-gate  */
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate /*
18960Sstevel@tonic-gate  * setup_credentials		- sets the group ID, initializes the groups
18970Sstevel@tonic-gate  *				  and sets up the secretkey.
18980Sstevel@tonic-gate  *				  Exits if a failure occurrs.
18990Sstevel@tonic-gate  */
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate  * setup_credentials		- PAM does all the work for us on this one.
19040Sstevel@tonic-gate  */
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate static void
19070Sstevel@tonic-gate setup_credentials(void)
19080Sstevel@tonic-gate {
19090Sstevel@tonic-gate 	int 	error = 0;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	/* set the real (and effective) GID */
19120Sstevel@tonic-gate 	if (setgid(pwd->pw_gid) == -1) {
19130Sstevel@tonic-gate 		login_exit(1);
19140Sstevel@tonic-gate 	}
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	/*
19170Sstevel@tonic-gate 	 * Initialize the supplementary group access list.
19180Sstevel@tonic-gate 	 */
19190Sstevel@tonic-gate 	if ((user_name[0] == '\0') ||
19200Sstevel@tonic-gate 	    (initgroups(user_name, pwd->pw_gid) == -1)) {
19210Sstevel@tonic-gate 		audit_error = ADT_FAIL_VALUE_PROGRAM;
19220Sstevel@tonic-gate 		login_exit(1);
19230Sstevel@tonic-gate 	}
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	if ((error = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
19260Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + error;
19270Sstevel@tonic-gate 		login_exit(error);
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	/*
19310Sstevel@tonic-gate 	 * Record successful login and fork process that records logout.
19320Sstevel@tonic-gate 	 * We have to do this after setting credentials because pam_setcred()
19330Sstevel@tonic-gate 	 * loads key audit info into the cred, but before setuid() so audit
19340Sstevel@tonic-gate 	 * system calls will work.
19350Sstevel@tonic-gate 	 */
19360Sstevel@tonic-gate 	audit_success(get_audit_id(), pwd, zone_name);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	/* set the real (and effective) UID */
19390Sstevel@tonic-gate 	if (setuid(pwd->pw_uid) == -1) {
19400Sstevel@tonic-gate 		login_exit(1);
19410Sstevel@tonic-gate 	}
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate static uint_t
1947523Sbasabi get_audit_id(void)
1948523Sbasabi {
19490Sstevel@tonic-gate 	if (rflag)
19500Sstevel@tonic-gate 		return (ADT_rlogin);
19510Sstevel@tonic-gate 	else if (hflag)
19520Sstevel@tonic-gate 		return (ADT_telnet);
19530Sstevel@tonic-gate 	else if (zflag)
19540Sstevel@tonic-gate 		return (ADT_zlogin);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	return (ADT_login);
19570Sstevel@tonic-gate }
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate /*
19600Sstevel@tonic-gate  *
19610Sstevel@tonic-gate  *		*** Routines to get a new user set up and running ***
19620Sstevel@tonic-gate  *
19630Sstevel@tonic-gate  *			Things to do when starting up a new user:
19640Sstevel@tonic-gate  *				adjust_nice
19650Sstevel@tonic-gate  *				update_utmpx_entry
19660Sstevel@tonic-gate  *				establish_user_environment
19670Sstevel@tonic-gate  *				print_banner
19680Sstevel@tonic-gate  *				display_last_login_time
19690Sstevel@tonic-gate  *				exec_the_shell
19700Sstevel@tonic-gate  *
19710Sstevel@tonic-gate  */
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate  * adjust_nice		- Set the nice (process priority) value if the
19760Sstevel@tonic-gate  *			  gecos value contains an appropriate value.
19770Sstevel@tonic-gate  */
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate static void
19800Sstevel@tonic-gate adjust_nice(void)
19810Sstevel@tonic-gate {
19820Sstevel@tonic-gate 	int pri, mflg, i;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	if (strncmp("pri=", pwd->pw_gecos, 4) == 0) {
19850Sstevel@tonic-gate 		pri = 0;
19860Sstevel@tonic-gate 		mflg = 0;
19870Sstevel@tonic-gate 		i = 4;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 		if (pwd->pw_gecos[i] == '-') {
19900Sstevel@tonic-gate 			mflg++;
19910Sstevel@tonic-gate 			i++;
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		while (pwd->pw_gecos[i] >= '0' && pwd->pw_gecos[i] <= '9')
19950Sstevel@tonic-gate 			pri = (pri * 10) + pwd->pw_gecos[i++] - '0';
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 		if (mflg)
19980Sstevel@tonic-gate 			pri = -pri;
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		(void) nice(pri);
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
20050Sstevel@tonic-gate /*
20060Sstevel@tonic-gate  * update_utmpx_entry	- Searchs for the correct utmpx entry, making an
20070Sstevel@tonic-gate  *			  entry there if it finds one, otherwise exits.
20080Sstevel@tonic-gate  */
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate static void
20110Sstevel@tonic-gate update_utmpx_entry(int sublogin)
20120Sstevel@tonic-gate {
20130Sstevel@tonic-gate 	int	err;
20140Sstevel@tonic-gate 	char	*user;
20150Sstevel@tonic-gate 	static char	*errmsg	= "No utmpx entry. "
20160Sstevel@tonic-gate 		"You must exec \"login\" from the lowest level \"shell\".";
20170Sstevel@tonic-gate 	int	   tmplen;
20180Sstevel@tonic-gate 	struct utmpx  *u = (struct utmpx *)0;
20190Sstevel@tonic-gate 	struct utmpx  utmpx;
20200Sstevel@tonic-gate 	char	  *ttyntail;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	/*
20230Sstevel@tonic-gate 	 * If we're not a sublogin then
20240Sstevel@tonic-gate 	 * we'll get an error back if our PID doesn't match the PID of the
20250Sstevel@tonic-gate 	 * entry we are updating, otherwise if its a sublogin the flags
20260Sstevel@tonic-gate 	 * field is set to 0, which means we just write a matching entry
20270Sstevel@tonic-gate 	 * (without checking the pid), or a new entry if an entry doesn't
20280Sstevel@tonic-gate 	 * exist.
20290Sstevel@tonic-gate 	 */
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	if ((err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
20320Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + err;
20330Sstevel@tonic-gate 		login_exit(1);
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if ((err = pam_get_item(pamh, PAM_USER, (void **) &user)) !=
20370Sstevel@tonic-gate 	    PAM_SUCCESS) {
20380Sstevel@tonic-gate 		audit_error = ADT_FAIL_PAM + err;
20390Sstevel@tonic-gate 		login_exit(1);
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	(void) memset((void *)&utmpx, 0, sizeof (utmpx));
20440Sstevel@tonic-gate 	(void) time(&utmpx.ut_tv.tv_sec);
20450Sstevel@tonic-gate 	utmpx.ut_pid = getpid();
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	if (rflag || hflag) {
20480Sstevel@tonic-gate 		SCPYN(utmpx.ut_host, remote_host);
20490Sstevel@tonic-gate 		tmplen = strlen(remote_host) + 1;
20500Sstevel@tonic-gate 		if (tmplen < sizeof (utmpx.ut_host))
20510Sstevel@tonic-gate 			utmpx.ut_syslen = tmplen;
20520Sstevel@tonic-gate 		else
20530Sstevel@tonic-gate 			utmpx.ut_syslen = sizeof (utmpx.ut_host);
20540Sstevel@tonic-gate 	} else if (zflag) {
20550Sstevel@tonic-gate 		/*
20560Sstevel@tonic-gate 		 * If this is a login from another zone, put the
20570Sstevel@tonic-gate 		 * zone:<zonename> string in the utmpx entry.
20580Sstevel@tonic-gate 		 */
20590Sstevel@tonic-gate 		SCPYN(utmpx.ut_host, zone_name);
20600Sstevel@tonic-gate 		tmplen = strlen(zone_name) + 1;
20610Sstevel@tonic-gate 		if (tmplen < sizeof (utmpx.ut_host))
20620Sstevel@tonic-gate 			utmpx.ut_syslen = tmplen;
20630Sstevel@tonic-gate 		else
20640Sstevel@tonic-gate 			utmpx.ut_syslen = sizeof (utmpx.ut_host);
20650Sstevel@tonic-gate 	} else {
20660Sstevel@tonic-gate 		utmpx.ut_syslen = 0;
20670Sstevel@tonic-gate 	}
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	SCPYN(utmpx.ut_user, user);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/* skip over "/dev/" */
20720Sstevel@tonic-gate 	ttyntail = basename(ttyn);
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
20750Sstevel@tonic-gate 		if ((u->ut_type == INIT_PROCESS ||
20760Sstevel@tonic-gate 			u->ut_type == LOGIN_PROCESS ||
20770Sstevel@tonic-gate 			u->ut_type == USER_PROCESS) &&
20780Sstevel@tonic-gate 			((sublogin && strncmp(u->ut_line, ttyntail,
20790Sstevel@tonic-gate 			sizeof (u->ut_line)) == 0) ||
20800Sstevel@tonic-gate 			u->ut_pid == utmpx.ut_pid)) {
20810Sstevel@tonic-gate 			SCPYN(utmpx.ut_line, (ttyn+sizeof ("/dev/")-1));
20820Sstevel@tonic-gate 			(void) memcpy(utmpx.ut_id, u->ut_id,
20830Sstevel@tonic-gate 			    sizeof (utmpx.ut_id));
20840Sstevel@tonic-gate 			utmpx.ut_exit.e_exit = u->ut_exit.e_exit;
20850Sstevel@tonic-gate 			utmpx.ut_type = USER_PROCESS;
20860Sstevel@tonic-gate 			(void) pututxline(&utmpx);
20870Sstevel@tonic-gate 			break;
20880Sstevel@tonic-gate 		}
20890Sstevel@tonic-gate 	}
20900Sstevel@tonic-gate 	endutxent();
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	if (u == (struct utmpx *)NULL) {
20930Sstevel@tonic-gate 		if (!sublogin) {
20940Sstevel@tonic-gate 			/*
20950Sstevel@tonic-gate 			 * no utmpx entry already setup
20960Sstevel@tonic-gate 			 * (init or rlogind/telnetd)
20970Sstevel@tonic-gate 			 */
20980Sstevel@tonic-gate 			(void) puts(errmsg);
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 			audit_error = ADT_FAIL_VALUE_PROGRAM;
21010Sstevel@tonic-gate 			login_exit(1);
21020Sstevel@tonic-gate 		}
21030Sstevel@tonic-gate 	} else {
21040Sstevel@tonic-gate 		/* Now attempt to write out this entry to the wtmp file if */
21050Sstevel@tonic-gate 		/* we were successful in getting it from the utmpx file and */
21060Sstevel@tonic-gate 		/* the wtmp file exists.				   */
21070Sstevel@tonic-gate 		updwtmpx(WTMPX_FILE, &utmpx);
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
21100Sstevel@tonic-gate }
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate /*
21150Sstevel@tonic-gate  * process_chroot_logins 	- Chroots to the specified subdirectory and
21160Sstevel@tonic-gate  *				  re executes login.
21170Sstevel@tonic-gate  */
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate static int
21200Sstevel@tonic-gate process_chroot_logins(void)
21210Sstevel@tonic-gate {
21220Sstevel@tonic-gate 	/*
21230Sstevel@tonic-gate 	 * If the shell field starts with a '*', do a chroot to the home
21240Sstevel@tonic-gate 	 * directory and perform a new login.
21250Sstevel@tonic-gate 	 */
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	if (*pwd->pw_shell == '*') {
21280Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_SUCCESS);	/* Done using PAM */
21290Sstevel@tonic-gate 		pamh = NULL;				/* really done */
21300Sstevel@tonic-gate 		if (chroot(pwd->pw_dir) < 0) {
21310Sstevel@tonic-gate 			(void) printf("No Root Directory\n");
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 			audit_failure(get_audit_id(),
21340Sstevel@tonic-gate 			    ADT_FAIL_VALUE_CHDIR_FAILED,
21350Sstevel@tonic-gate 			    pwd, remote_host, ttyn, zone_name);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 			return (ERROR);
21380Sstevel@tonic-gate 		}
21390Sstevel@tonic-gate 		/*
21400Sstevel@tonic-gate 		 * Set the environment flag <!sublogin> so that the next login
21410Sstevel@tonic-gate 		 * knows that it is a sublogin.
21420Sstevel@tonic-gate 		 */
21430Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
21440Sstevel@tonic-gate 		envinit[0] = SUBLOGIN;
21450Sstevel@tonic-gate 		envinit[1] = (char *)NULL;
21460Sstevel@tonic-gate 		(void) printf("Subsystem root: %s\n", pwd->pw_dir);
21470Sstevel@tonic-gate 		(void) execle("/usr/bin/login", "login", (char *)0,
21480Sstevel@tonic-gate 			&envinit[0]);
21490Sstevel@tonic-gate 		(void) execle("/etc/login", "login", (char *)0, &envinit[0]);
21500Sstevel@tonic-gate 		(void) printf("No /usr/bin/login or /etc/login on root\n");
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		audit_error = ADT_FAIL_VALUE_PROGRAM;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 		login_exit(1);
21550Sstevel@tonic-gate 	}
21560Sstevel@tonic-gate 	return (OK);
21570Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
21580Sstevel@tonic-gate }
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate /*
21610Sstevel@tonic-gate  * establish_user_environment	- Set up the new users enviornment
21620Sstevel@tonic-gate  */
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate static void
21650Sstevel@tonic-gate establish_user_environment(char **renvp)
21660Sstevel@tonic-gate {
21670Sstevel@tonic-gate 	int i, j, k, l_index, length, idx = 0;
21680Sstevel@tonic-gate 	char *endptr;
21690Sstevel@tonic-gate 	char **lenvp;
21700Sstevel@tonic-gate 	char **pam_env;
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 	lenvp = environ;
21730Sstevel@tonic-gate 	while (*lenvp++)
21740Sstevel@tonic-gate 		;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	/* count the number of PAM environment variables set by modules */
21770Sstevel@tonic-gate 	if ((pam_env = pam_getenvlist(pamh)) != 0) {
21780Sstevel@tonic-gate 		for (idx = 0; pam_env[idx] != 0; idx++)
21790Sstevel@tonic-gate 				;
21800Sstevel@tonic-gate 	}
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	envinit = (char **)calloc(lenvp - environ + 10
21830Sstevel@tonic-gate 		+ MAXARGS + idx, sizeof (char *));
21840Sstevel@tonic-gate 	if (envinit == NULL) {
21850Sstevel@tonic-gate 		(void) printf("Calloc failed - out of swap space.\n");
21860Sstevel@tonic-gate 		login_exit(8);
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 	/*
21900Sstevel@tonic-gate 	 * add PAM environment variables first so they
21910Sstevel@tonic-gate 	 * can be overwritten at login's discretion.
21920Sstevel@tonic-gate 	 * check for illegal environment variables.
21930Sstevel@tonic-gate 	 */
21940Sstevel@tonic-gate 	idx = 0;	basicenv = 0;
21950Sstevel@tonic-gate 	if (pam_env != 0) {
21960Sstevel@tonic-gate 		while (pam_env[idx] != 0) {
21970Sstevel@tonic-gate 			if (legalenvvar(pam_env[idx])) {
21980Sstevel@tonic-gate 				envinit[basicenv] = pam_env[idx];
21990Sstevel@tonic-gate 				basicenv++;
22000Sstevel@tonic-gate 			}
22010Sstevel@tonic-gate 			idx++;
22020Sstevel@tonic-gate 		}
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 	(void) memcpy(&envinit[basicenv], newenv, sizeof (newenv));
22050Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	/* Set up environment */
22080Sstevel@tonic-gate 	if (rflag) {
22090Sstevel@tonic-gate 		ENVSTRNCAT(term, terminal);
22100Sstevel@tonic-gate 	} else if (hflag) {
22110Sstevel@tonic-gate 		if (strlen(terminal)) {
22120Sstevel@tonic-gate 			ENVSTRNCAT(term, terminal);
22130Sstevel@tonic-gate 		}
22140Sstevel@tonic-gate 	} else {
22150Sstevel@tonic-gate 		char *tp = getenv("TERM");
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		if ((tp != NULL) && (*tp != '\0'))
22180Sstevel@tonic-gate 			ENVSTRNCAT(term, tp);
22190Sstevel@tonic-gate 	}
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	ENVSTRNCAT(logname, pwd->pw_name);
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	/*
22240Sstevel@tonic-gate 	 * There are three places to get timezone info.  init.c sets
22250Sstevel@tonic-gate 	 * TZ if the file /etc/TIMEZONE contains a value for TZ.
22260Sstevel@tonic-gate 	 * login.c looks in the file /etc/default/login for a
22270Sstevel@tonic-gate 	 * variable called TIMEZONE being set.  If TIMEZONE has a
22280Sstevel@tonic-gate 	 *  value, TZ is set to that value; no environment variable
22290Sstevel@tonic-gate 	 * TIMEZONE is set, only TZ.  If neither of these methods
22300Sstevel@tonic-gate 	 * work to set TZ, then the library routines  will default
22310Sstevel@tonic-gate 	 * to using the file /usr/lib/locale/TZ/localtime.
22320Sstevel@tonic-gate 	 *
22330Sstevel@tonic-gate 	 * There is a priority set up here.  If /etc/TIMEZONE has
22340Sstevel@tonic-gate 	 * a value for TZ, that value remains top priority.  If the
22350Sstevel@tonic-gate 	 * file /etc/default/login has TIMEZONE set, that has second
22360Sstevel@tonic-gate 	 * highest priority not overriding the value of TZ in
22370Sstevel@tonic-gate 	 * /etc/TIMEZONE.  The reason for this priority is that the
22380Sstevel@tonic-gate 	 * file /etc/TIMEZONE is supposed to be sourced by
22390Sstevel@tonic-gate 	 * /etc/profile.  We are doing the "sourcing" prematurely in
22400Sstevel@tonic-gate 	 * init.c.  Additionally, a login C shell doesn't source the
22410Sstevel@tonic-gate 	 * file /etc/profile thus not sourcing /etc/TIMEZONE thus not
22420Sstevel@tonic-gate 	 * allowing an adminstrator to globally set TZ for all users
22430Sstevel@tonic-gate 	 */
22440Sstevel@tonic-gate 	if (Def_tz != NULL)	/* Is there a TZ from defaults/login? */
22450Sstevel@tonic-gate 		tmp_tz = Def_tz;
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	if ((Def_tz = getenv("TZ")) != NULL) {
22480Sstevel@tonic-gate 		ENVSTRNCAT(timez, Def_tz);
22490Sstevel@tonic-gate 	} else if (tmp_tz != NULL) {
22500Sstevel@tonic-gate 		Def_tz = tmp_tz;
22510Sstevel@tonic-gate 		ENVSTRNCAT(timez, Def_tz);
22520Sstevel@tonic-gate 	}
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	if (Def_hertz == NULL)
22550Sstevel@tonic-gate 		(void) sprintf(hertz + strlen(hertz), "%lu", HZ);
22560Sstevel@tonic-gate 	else
22570Sstevel@tonic-gate 		ENVSTRNCAT(hertz, Def_hertz);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	if (Def_path == NULL)
22600Sstevel@tonic-gate 		(void) strlcat(path, DEF_PATH, sizeof (path));
22610Sstevel@tonic-gate 	else
22620Sstevel@tonic-gate 		ENVSTRNCAT(path, Def_path);
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	ENVSTRNCAT(home, pwd->pw_dir);
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	/*
22670Sstevel@tonic-gate 	 * Find the end of the basic environment
22680Sstevel@tonic-gate 	 */
22690Sstevel@tonic-gate 	for (basicenv = 0; envinit[basicenv] != NULL; basicenv++)
22700Sstevel@tonic-gate 		;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	/*
22730Sstevel@tonic-gate 	 * If TZ has a value, add it.
22740Sstevel@tonic-gate 	 */
22750Sstevel@tonic-gate 	if (strcmp(timez, "TZ=") != 0)
22760Sstevel@tonic-gate 		envinit[basicenv++] = timez;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	if (*pwd->pw_shell == '\0') {
22790Sstevel@tonic-gate 		/*
22800Sstevel@tonic-gate 		 * If possible, use the primary default shell,
22810Sstevel@tonic-gate 		 * otherwise, use the secondary one.
22820Sstevel@tonic-gate 		 */
22830Sstevel@tonic-gate 		if (access(SHELL, X_OK) == 0)
22840Sstevel@tonic-gate 			pwd->pw_shell = SHELL;
22850Sstevel@tonic-gate 		else
22860Sstevel@tonic-gate 			pwd->pw_shell = SHELL2;
22870Sstevel@tonic-gate 	} else if (Altshell != NULL && strcmp(Altshell, "YES") == 0) {
22880Sstevel@tonic-gate 		envinit[basicenv++] = shell;
22890Sstevel@tonic-gate 		ENVSTRNCAT(shell, pwd->pw_shell);
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate #ifndef	NO_MAIL
22930Sstevel@tonic-gate 	envinit[basicenv++] = mail;
22940Sstevel@tonic-gate 	(void) strlcat(mail, pwd->pw_name, sizeof (mail));
22950Sstevel@tonic-gate #endif
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	/*
22980Sstevel@tonic-gate 	 * Pick up locale environment variables, if any.
22990Sstevel@tonic-gate 	 */
23000Sstevel@tonic-gate 	lenvp = renvp;
23010Sstevel@tonic-gate 	while (*lenvp != NULL) {
23020Sstevel@tonic-gate 		j = 0;
23030Sstevel@tonic-gate 		while (localeenv[j] != 0) {
23040Sstevel@tonic-gate 			/*
23050Sstevel@tonic-gate 			 * locale_envmatch() returns 1 if
23060Sstevel@tonic-gate 			 * *lenvp is localenev[j] and valid.
23070Sstevel@tonic-gate 			 */
23080Sstevel@tonic-gate 			if (locale_envmatch(localeenv[j], *lenvp) == 1) {
23090Sstevel@tonic-gate 				envinit[basicenv++] = *lenvp;
23100Sstevel@tonic-gate 				break;
23110Sstevel@tonic-gate 			}
23120Sstevel@tonic-gate 			j++;
23130Sstevel@tonic-gate 		}
23140Sstevel@tonic-gate 		lenvp++;
23150Sstevel@tonic-gate 	}
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 	/*
23180Sstevel@tonic-gate 	 * If '-p' flag, then try to pass on allowable environment
23190Sstevel@tonic-gate 	 * variables.  Note that by processing this first, what is
23200Sstevel@tonic-gate 	 * passed on the final "login:" line may over-ride the invocation
23210Sstevel@tonic-gate 	 * values.  XXX is this correct?
23220Sstevel@tonic-gate 	 */
23230Sstevel@tonic-gate 	if (pflag) {
23240Sstevel@tonic-gate 		for (lenvp = renvp; *lenvp; lenvp++) {
23250Sstevel@tonic-gate 			if (!legalenvvar(*lenvp)) {
23260Sstevel@tonic-gate 				continue;
23270Sstevel@tonic-gate 			}
23280Sstevel@tonic-gate 			/*
23290Sstevel@tonic-gate 			 * If this isn't 'xxx=yyy', skip it.  XXX
23300Sstevel@tonic-gate 			 */
23310Sstevel@tonic-gate 			if ((endptr = strchr(*lenvp, '=')) == NULL) {
23320Sstevel@tonic-gate 				continue;
23330Sstevel@tonic-gate 			}
23340Sstevel@tonic-gate 			length = endptr + 1 - *lenvp;
23350Sstevel@tonic-gate 			for (j = 0; j < basicenv; j++) {
23360Sstevel@tonic-gate 				if (strncmp(envinit[j], *lenvp, length) == 0) {
23370Sstevel@tonic-gate 					/*
23380Sstevel@tonic-gate 					 * Replace previously established value
23390Sstevel@tonic-gate 					 */
23400Sstevel@tonic-gate 					envinit[j] = *lenvp;
23410Sstevel@tonic-gate 					break;
23420Sstevel@tonic-gate 				}
23430Sstevel@tonic-gate 			}
23440Sstevel@tonic-gate 			if (j == basicenv) {
23450Sstevel@tonic-gate 				/*
23460Sstevel@tonic-gate 				 * It's a new definition, so add it at the end.
23470Sstevel@tonic-gate 				 */
23480Sstevel@tonic-gate 				envinit[basicenv++] = *lenvp;
23490Sstevel@tonic-gate 			}
23500Sstevel@tonic-gate 		}
23510Sstevel@tonic-gate 	}
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	/*
23540Sstevel@tonic-gate 	 * Add in all the environment variables picked up from the
23550Sstevel@tonic-gate 	 * argument list to "login" or from the user response to the
23560Sstevel@tonic-gate 	 * "login" request, if any.
23570Sstevel@tonic-gate 	 */
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	if (envp == NULL)
23600Sstevel@tonic-gate 		goto switch_env;	/* done */
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 	for (j = 0, k = 0, l_index = 0;
23630Sstevel@tonic-gate 		*envp != NULL && j < (MAXARGS-1);
23640Sstevel@tonic-gate 		j++, envp++) {
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 		/*
23670Sstevel@tonic-gate 		 * Scan each string provided.  If it doesn't have the
23680Sstevel@tonic-gate 		 * format xxx=yyy, then add the string "Ln=" to the beginning.
23690Sstevel@tonic-gate 		 */
23700Sstevel@tonic-gate 		if ((endptr = strchr(*envp, '=')) == NULL) {
23710Sstevel@tonic-gate 			/*
23720Sstevel@tonic-gate 			 * This much to be malloc'd:
23730Sstevel@tonic-gate 			 *   strlen(*envp) + 1 char for 'L' +
23740Sstevel@tonic-gate 			 *   MAXARGSWIDTH + 1 char for '=' + 1 for null char;
23750Sstevel@tonic-gate 			 *
23760Sstevel@tonic-gate 			 * total = strlen(*envp) + MAXARGSWIDTH + 3
23770Sstevel@tonic-gate 			 */
23780Sstevel@tonic-gate 			int total = strlen(*envp) + MAXARGSWIDTH + 3;
23790Sstevel@tonic-gate 			envinit[basicenv+k] = malloc(total);
23800Sstevel@tonic-gate 			if (envinit[basicenv+k] == NULL) {
23810Sstevel@tonic-gate 				(void) printf("%s: malloc failed\n", PROG_NAME);
23820Sstevel@tonic-gate 				login_exit(1);
23830Sstevel@tonic-gate 			}
23840Sstevel@tonic-gate 			(void) snprintf(envinit[basicenv+k], total, "L%d=%s",
23850Sstevel@tonic-gate 				l_index, *envp);
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 			k++;
23880Sstevel@tonic-gate 			l_index++;
23890Sstevel@tonic-gate 		} else  {
23900Sstevel@tonic-gate 			if (!legalenvvar(*envp)) { /* this env var permited? */
23910Sstevel@tonic-gate 				continue;
23920Sstevel@tonic-gate 			} else {
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 				/*
23950Sstevel@tonic-gate 				 * Check to see whether this string replaces
23960Sstevel@tonic-gate 				 * any previously defined string
23970Sstevel@tonic-gate 				 */
23980Sstevel@tonic-gate 				for (i = 0, length = endptr + 1 - *envp;
23990Sstevel@tonic-gate 					i < basicenv + k; i++) {
24000Sstevel@tonic-gate 				    if (strncmp(*envp, envinit[i], length)
24010Sstevel@tonic-gate 						== 0) {
24020Sstevel@tonic-gate 					envinit[i] = *envp;
24030Sstevel@tonic-gate 					break;
24040Sstevel@tonic-gate 				    }
24050Sstevel@tonic-gate 				}
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 				/*
24080Sstevel@tonic-gate 				 * If it doesn't, place it at the end of
24090Sstevel@tonic-gate 				 * environment array.
24100Sstevel@tonic-gate 				 */
24110Sstevel@tonic-gate 				if (i == basicenv+k) {
24120Sstevel@tonic-gate 					envinit[basicenv+k] = *envp;
24130Sstevel@tonic-gate 					k++;
24140Sstevel@tonic-gate 				}
24150Sstevel@tonic-gate 			}
24160Sstevel@tonic-gate 		}
24170Sstevel@tonic-gate 	}		/* for (j = 0 ... ) */
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate switch_env:
24200Sstevel@tonic-gate 	/*
24210Sstevel@tonic-gate 	 * Switch to the new environment.
24220Sstevel@tonic-gate 	 */
24230Sstevel@tonic-gate 	environ = envinit;
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate /*
24270Sstevel@tonic-gate  * print_banner		- Print the banner at start up
24280Sstevel@tonic-gate  *			   Do not turn on DOBANNER ifdef.  This is not
24290Sstevel@tonic-gate  *			   relevant to SunOS.
24300Sstevel@tonic-gate  */
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate static void
24330Sstevel@tonic-gate print_banner(void)
24340Sstevel@tonic-gate {
24350Sstevel@tonic-gate #ifdef DOBANNER
24360Sstevel@tonic-gate 	uname(&un);
24370Sstevel@tonic-gate #if i386
24380Sstevel@tonic-gate 	(void) printf("UNIX System V/386 Release %s\n%s\n"
24390Sstevel@tonic-gate 	    "Copyright (C) 1984, 1986, 1987, 1988 AT&T\n"
24400Sstevel@tonic-gate 	    "Copyright (C) 1987, 1988 Microsoft Corp.\nAll Rights Reserved\n",
24410Sstevel@tonic-gate 		un.release, un.nodename);
24420Sstevel@tonic-gate #elif sun
24430Sstevel@tonic-gate 	(void) printf("SunOS Release %s Sun Microsystems %s\n%s\n"
24440Sstevel@tonic-gate 	    "Copyright (c) 1984, 1986, 1987, 1988 AT&T\n"
24450Sstevel@tonic-gate 	    "Copyright (c) 1988, 1989, 1990, 1991 Sun Microsystems\n"
24460Sstevel@tonic-gate 	    "All Rights Reserved\n",
24470Sstevel@tonic-gate 		un.release, un.machine, un.nodename);
24480Sstevel@tonic-gate #else
24490Sstevel@tonic-gate 	(void) printf("UNIX System V Release %s AT&T %s\n%s\n"
24500Sstevel@tonic-gate 	    "Copyright (c) 1984, 1986, 1987, 1988 AT&T\nAll Rights Reserved\n",
24510Sstevel@tonic-gate 		un.release, un.machine, un.nodename);
24520Sstevel@tonic-gate #endif /* i386 */
24530Sstevel@tonic-gate #endif /* DOBANNER */
24540Sstevel@tonic-gate }
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate /*
24570Sstevel@tonic-gate  * display_last_login_time	- Advise the user the time and date
24580Sstevel@tonic-gate  *				  that this login-id was last used.
24590Sstevel@tonic-gate  */
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate static void
24620Sstevel@tonic-gate display_last_login_time(void)
24630Sstevel@tonic-gate {
24640Sstevel@tonic-gate 	if (lastlogok) {
24650Sstevel@tonic-gate 		(void) printf("Last login: %.*s ", 24-5, ctime(&ll.ll_time));
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 		if (*ll.ll_host != '\0')
24680Sstevel@tonic-gate 			(void) printf("from %.*s\n", sizeof (ll.ll_host),
24690Sstevel@tonic-gate 					    ll.ll_host);
24700Sstevel@tonic-gate 		else
24710Sstevel@tonic-gate 			(void) printf("on %.*s\n", sizeof (ll.ll_line),
24720Sstevel@tonic-gate 					    ll.ll_line);
24730Sstevel@tonic-gate 	}
24740Sstevel@tonic-gate }
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate /*
24770Sstevel@tonic-gate  * exec_the_shell	- invoke the specified shell or start up program
24780Sstevel@tonic-gate  */
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate static void
24810Sstevel@tonic-gate exec_the_shell(void)
24820Sstevel@tonic-gate {
24830Sstevel@tonic-gate 	char *endptr;
24840Sstevel@tonic-gate 	int i;
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	(void) strlcat(minusnam, basename(pwd->pw_shell),
24870Sstevel@tonic-gate 		sizeof (minusnam));
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	/*
24900Sstevel@tonic-gate 	 * Exec the shell
24910Sstevel@tonic-gate 	 */
24920Sstevel@tonic-gate 	(void) execl(pwd->pw_shell, minusnam, (char *)0);
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	/*
24950Sstevel@tonic-gate 	 * pwd->pw_shell was not an executable object file, maybe it
24960Sstevel@tonic-gate 	 * is a shell proceedure or a command line with arguments.
24970Sstevel@tonic-gate 	 * If so, turn off the SHELL= environment variable.
24980Sstevel@tonic-gate 	 */
24990Sstevel@tonic-gate 	for (i = 0; envinit[i] != NULL; ++i) {
25000Sstevel@tonic-gate 		if ((envinit[i] == shell) &&
25010Sstevel@tonic-gate 		    ((endptr = strchr(shell, '=')) != NULL))
25020Sstevel@tonic-gate 			(*++endptr) = '\0';
25030Sstevel@tonic-gate 		}
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	if (access(pwd->pw_shell, R_OK|X_OK) == 0) {
25060Sstevel@tonic-gate 		(void) execl(SHELL, "sh", pwd->pw_shell, (char *)0);
25070Sstevel@tonic-gate 		(void) execl(SHELL2, "sh", pwd->pw_shell, (char *)0);
25080Sstevel@tonic-gate 	}
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	(void) printf("No shell\n");
25110Sstevel@tonic-gate }
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate /*
25140Sstevel@tonic-gate  * login_exit		- Call exit()  and terminate.
25150Sstevel@tonic-gate  *			  This function is here for PAM so cleanup can
25160Sstevel@tonic-gate  *			  be done before the process exits.
25170Sstevel@tonic-gate  */
25180Sstevel@tonic-gate static void
25190Sstevel@tonic-gate login_exit(int exit_code)
25200Sstevel@tonic-gate {
25210Sstevel@tonic-gate 	if (pamh)
25220Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	if (audit_error)
25250Sstevel@tonic-gate 		audit_failure(get_audit_id(), audit_error,
25260Sstevel@tonic-gate 		    pwd, remote_host, ttyn, zone_name);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	exit(exit_code);
25290Sstevel@tonic-gate 	/*NOTREACHED*/
25300Sstevel@tonic-gate }
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate /*
25330Sstevel@tonic-gate  * Check if lenv and penv matches or not.
25340Sstevel@tonic-gate  */
25350Sstevel@tonic-gate static int
25360Sstevel@tonic-gate locale_envmatch(char *lenv, char *penv)
25370Sstevel@tonic-gate {
25380Sstevel@tonic-gate 	while ((*lenv == *penv) && *lenv && *penv != '=') {
25390Sstevel@tonic-gate 		lenv++;
25400Sstevel@tonic-gate 		penv++;
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate 	/*
25440Sstevel@tonic-gate 	 * '/' is eliminated for security reason.
25450Sstevel@tonic-gate 	 */
25460Sstevel@tonic-gate 	if (*lenv == '\0' && *penv == '=' && *(penv + 1) != '/')
25470Sstevel@tonic-gate 		return (1);
25480Sstevel@tonic-gate 	return (0);
25490Sstevel@tonic-gate }
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate static int
25520Sstevel@tonic-gate is_number(char *ptr)
25530Sstevel@tonic-gate {
25540Sstevel@tonic-gate 	while (*ptr != '\0') {
25550Sstevel@tonic-gate 		if (!isdigit(*ptr))
25560Sstevel@tonic-gate 			return (0);
25570Sstevel@tonic-gate 		ptr++;
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 	return (1);
25600Sstevel@tonic-gate }
2561