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