xref: /titanic_52/usr/src/cmd/sh/main.c (revision 965005c81e0f731867d47892b9fb677030b102df)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*965005c8Schin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * UNIX shell
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include	"defs.h"
397c478bd9Sstevel@tonic-gate #include	"sym.h"
407c478bd9Sstevel@tonic-gate #include	"timeout.h"
41*965005c8Schin #include	<stdio.h>
427c478bd9Sstevel@tonic-gate #include	<sys/types.h>
437c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
447c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
457c478bd9Sstevel@tonic-gate #include	"dup.h"
467c478bd9Sstevel@tonic-gate #include	"sh_policy.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifdef RES
497c478bd9Sstevel@tonic-gate #include	<sgtty.h>
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate pid_t mypid, mypgid, mysid;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static BOOL	beenhere = FALSE;
55*965005c8Schin unsigned char	tmpout[TMPOUTSZ];
567c478bd9Sstevel@tonic-gate struct fileblk	stdfile;
577c478bd9Sstevel@tonic-gate struct fileblk *standin = &stdfile;
587c478bd9Sstevel@tonic-gate int mailchk = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static unsigned char	*mailp;
617c478bd9Sstevel@tonic-gate static long	*mod_time = 0;
627c478bd9Sstevel@tonic-gate static BOOL login_shell = FALSE;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #if vax
657c478bd9Sstevel@tonic-gate char **execargs = (char **)(0x7ffffffc);
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #if pdp11
697c478bd9Sstevel@tonic-gate char **execargs = (char **)(-2);
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
73*965005c8Schin static void	exfile();
747c478bd9Sstevel@tonic-gate extern unsigned char 	*simple();
75*965005c8Schin static void Ldup(int, int);
76*965005c8Schin void settmp(void);
77*965005c8Schin void chkmail(void);
78*965005c8Schin void setmail(unsigned char *);
797c478bd9Sstevel@tonic-gate 
80*965005c8Schin int
81*965005c8Schin main(int c, char *v[], char *e[])
827c478bd9Sstevel@tonic-gate {
83*965005c8Schin 	int		rflag = ttyflg;
847c478bd9Sstevel@tonic-gate 	int		rsflag = 1;	/* local restricted flag */
85*965005c8Schin 	unsigned char	*flagc = flagadr;
867c478bd9Sstevel@tonic-gate 	struct namnod	*n;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	mypid = getpid();
897c478bd9Sstevel@tonic-gate 	mypgid = getpgid(mypid);
907c478bd9Sstevel@tonic-gate 	mysid = getsid(mypid);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	localedir_exists = (access(localedir, F_OK) == 0);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * initialize storage allocation
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (stakbot == 0) {
1027c478bd9Sstevel@tonic-gate 	addblok((unsigned)0);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * If the first character of the last path element of v[0] is "-"
1077c478bd9Sstevel@tonic-gate 	 * (ex. -sh, or /bin/-sh), this is a login shell
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 	if (*simple(v[0]) == '-') {
1107c478bd9Sstevel@tonic-gate 		signal(SIGXCPU, SIG_DFL);
1117c478bd9Sstevel@tonic-gate 		signal(SIGXFSZ, SIG_DFL);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		/*
1147c478bd9Sstevel@tonic-gate 		 * As the previous comment states, this is a login shell.
1157c478bd9Sstevel@tonic-gate 		 * Therefore, we set the login_shell flag to explicitly
1167c478bd9Sstevel@tonic-gate 		 * indicate this condition.
1177c478bd9Sstevel@tonic-gate 		 */
1187c478bd9Sstevel@tonic-gate 		login_shell = TRUE;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	stdsigs();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * set names from userenv
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	setup_env();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * LC_MESSAGES is set here so that early error messages will
1317c478bd9Sstevel@tonic-gate 	 * come out in the right style.
1327c478bd9Sstevel@tonic-gate 	 * Note that LC_CTYPE is done later on and is *not*
1337c478bd9Sstevel@tonic-gate 	 * taken from the previous environ
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	if (localedir_exists)
1407c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
1417c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1427c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1437c478bd9Sstevel@tonic-gate #endif
1447c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * This is a profile shell if the simple name of argv[0] is
1487c478bd9Sstevel@tonic-gate 	 * pfsh or -pfsh
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("pfsh", simple(*v)) || eq("-pfsh", simple(*v)))) {
1517c478bd9Sstevel@tonic-gate 		flags |= pfshflg;
1527c478bd9Sstevel@tonic-gate 		secpolicy_init();
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * 'rsflag' is zero if SHELL variable is
1577c478bd9Sstevel@tonic-gate 	 *  set in environment and
1587c478bd9Sstevel@tonic-gate 	 *  the simple file part of the value.
1597c478bd9Sstevel@tonic-gate 	 *  is rsh
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (n = findnam("SHELL"))
1627c478bd9Sstevel@tonic-gate 	{
1637c478bd9Sstevel@tonic-gate 		if (eq("rsh", simple(n->namval)))
1647c478bd9Sstevel@tonic-gate 			rsflag = 0;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * a shell is also restricted if the simple name of argv(0) is
1697c478bd9Sstevel@tonic-gate 	 * rsh or -rsh in its simple name
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #ifndef RES
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("rsh", simple(*v)) || eq("-rsh", simple(*v))))
1757c478bd9Sstevel@tonic-gate 		rflag = 0;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (eq("jsh", simple(*v)) || eq("-jsh", simple(*v)))
1807c478bd9Sstevel@tonic-gate 		flags |= monitorflg;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	hcreate();
1837c478bd9Sstevel@tonic-gate 	set_dotpath();
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * look for options
1887c478bd9Sstevel@tonic-gate 	 * dolc is $#
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	dolc = options(c, v);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (dolc < 2)
1937c478bd9Sstevel@tonic-gate 	{
1947c478bd9Sstevel@tonic-gate 		flags |= stdflg;
1957c478bd9Sstevel@tonic-gate 		{
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 			while (*flagc)
1987c478bd9Sstevel@tonic-gate 				flagc++;
1997c478bd9Sstevel@tonic-gate 			*flagc++ = STDFLG;
2007c478bd9Sstevel@tonic-gate 			*flagc = 0;
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	if ((flags & stdflg) == 0)
2047c478bd9Sstevel@tonic-gate 		dolc--;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if ((flags & privflg) == 0) {
207*965005c8Schin 		uid_t euid;
208*965005c8Schin 		gid_t egid;
209*965005c8Schin 		uid_t ruid;
210*965005c8Schin 		gid_t rgid;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * Determine all of the user's id #'s for this process and
2147c478bd9Sstevel@tonic-gate 		 * then decide if this shell is being entered as a result
2157c478bd9Sstevel@tonic-gate 		 * of a fork/exec.
2167c478bd9Sstevel@tonic-gate 		 * If the effective uid/gid do NOT match and the euid/egid
2177c478bd9Sstevel@tonic-gate 		 * is < 100 and the egid is NOT 1, reset the uid and gid to
2187c478bd9Sstevel@tonic-gate 		 * the user originally calling this process.
2197c478bd9Sstevel@tonic-gate 		 */
2207c478bd9Sstevel@tonic-gate 		euid = geteuid();
2217c478bd9Sstevel@tonic-gate 		ruid = getuid();
2227c478bd9Sstevel@tonic-gate 		egid = getegid();
2237c478bd9Sstevel@tonic-gate 		rgid = getgid();
2247c478bd9Sstevel@tonic-gate 		if ((euid != ruid) && (euid < 100))
2257c478bd9Sstevel@tonic-gate 			setuid(ruid);   /* reset the uid to the orig user */
2267c478bd9Sstevel@tonic-gate 		if ((egid != rgid) && ((egid < 100) && (egid != 1)))
2277c478bd9Sstevel@tonic-gate 			setgid(rgid);   /* reset the gid to the orig user */
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	dolv = (unsigned char **)v + c - dolc;
2317c478bd9Sstevel@tonic-gate 	dolc--;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * return here for shell file execution
2357c478bd9Sstevel@tonic-gate 	 * but not for parenthesis subshells
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (setjmp(subshell)) {
2387c478bd9Sstevel@tonic-gate 		freejobs();
2397c478bd9Sstevel@tonic-gate 		flags |= subsh;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * number of positional parameters
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	replace(&cmdadr, dolv[0]);	/* cmdadr is $0 */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * set pidname '$$'
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	assnum(&pidadr, (long)mypid);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * set up temp file names
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	settmp();
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * default internal field separators
2597c478bd9Sstevel@tonic-gate 	 * Do not allow importing of IFS from parent shell.
2607c478bd9Sstevel@tonic-gate 	 * setup_env() may have set anything from parent shell to IFS.
2617c478bd9Sstevel@tonic-gate 	 * Always set the default ifs to IFS.
2627c478bd9Sstevel@tonic-gate 	 */
263*965005c8Schin 	assign(&ifsnod, (unsigned char *)sptbnl);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	dfault(&mchknod, MAILCHECK);
2667c478bd9Sstevel@tonic-gate 	mailchk = stoi(mchknod.namval);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/* initialize OPTIND for getopt */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	n = lookup("OPTIND");
271*965005c8Schin 	assign(n, (unsigned char *)"1");
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * make sure that option parsing starts
2747c478bd9Sstevel@tonic-gate 	 * at first character
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	_sp = 1;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* initialize multibyte information */
2797c478bd9Sstevel@tonic-gate 	setwidth();
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if ((beenhere++) == FALSE)	/* ? profile */
2827c478bd9Sstevel@tonic-gate 	{
2837c478bd9Sstevel@tonic-gate 		if ((login_shell == TRUE) && (flags & privflg) == 0) {
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			/* system profile */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate #ifndef RES
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			if ((input = pathopen(nullstr, sysprofile)) >= 0)
2907c478bd9Sstevel@tonic-gate 				exfile(rflag);		/* file exists */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate #endif
2937c478bd9Sstevel@tonic-gate 			/* user profile */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			if ((input = pathopen(homenod.namval, profile)) >= 0)
2967c478bd9Sstevel@tonic-gate 			{
2977c478bd9Sstevel@tonic-gate 				exfile(rflag);
2987c478bd9Sstevel@tonic-gate 				flags &= ~ttyflg;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 		if (rsflag == 0 || rflag == 0) {
3027c478bd9Sstevel@tonic-gate 			if ((flags & rshflg) == 0) {
3037c478bd9Sstevel@tonic-gate 				while (*flagc)
3047c478bd9Sstevel@tonic-gate 					flagc++;
3057c478bd9Sstevel@tonic-gate 				*flagc++ = 'r';
3067c478bd9Sstevel@tonic-gate 				*flagc = '\0';
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			flags |= rshflg;
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		/*
3127c478bd9Sstevel@tonic-gate 		 * open input file if specified
3137c478bd9Sstevel@tonic-gate 		 */
3147c478bd9Sstevel@tonic-gate 		if (comdiv)
3157c478bd9Sstevel@tonic-gate 		{
3167c478bd9Sstevel@tonic-gate 			estabf(comdiv);
3177c478bd9Sstevel@tonic-gate 			input = -1;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 		{
3217c478bd9Sstevel@tonic-gate 			if (flags & stdflg) {
3227c478bd9Sstevel@tonic-gate 				input = 0;
3237c478bd9Sstevel@tonic-gate 			} else {
3247c478bd9Sstevel@tonic-gate 			/*
3257c478bd9Sstevel@tonic-gate 			 * If the command file specified by 'cmdadr'
3267c478bd9Sstevel@tonic-gate 			 * doesn't exist, chkopen() will fail calling
3277c478bd9Sstevel@tonic-gate 			 * exitsh(). If this is a login shell and
3287c478bd9Sstevel@tonic-gate 			 * the $HOME/.profile file does not exist, the
3297c478bd9Sstevel@tonic-gate 			 * above statement "flags &= ~ttyflg" does not
3307c478bd9Sstevel@tonic-gate 			 * get executed and this makes exitsh() call
3317c478bd9Sstevel@tonic-gate 			 * longjmp() instead of exiting. longjmp() will
3327c478bd9Sstevel@tonic-gate 			 * return to the location specified by the last
3337c478bd9Sstevel@tonic-gate 			 * active jmpbuffer, which is the one set up in
3347c478bd9Sstevel@tonic-gate 			 * the function exfile() called after the system
3357c478bd9Sstevel@tonic-gate 			 * profile file is executed (see lines above).
3367c478bd9Sstevel@tonic-gate 			 * This would cause an infinite loop, because
3377c478bd9Sstevel@tonic-gate 			 * chkopen() will continue to fail and exitsh()
3387c478bd9Sstevel@tonic-gate 			 * to call longjmp(). To make exitsh() exit instead
3397c478bd9Sstevel@tonic-gate 			 * of calling longjmp(), we then set the flag forcexit
3407c478bd9Sstevel@tonic-gate 			 * at this stage.
3417c478bd9Sstevel@tonic-gate 			 */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 				flags |= forcexit;
3447c478bd9Sstevel@tonic-gate 				input = chkopen(cmdadr, 0);
3457c478bd9Sstevel@tonic-gate 				flags &= ~forcexit;
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #ifdef ACCT
3497c478bd9Sstevel@tonic-gate 			if (input != 0)
3507c478bd9Sstevel@tonic-gate 				preacct(cmdadr);
3517c478bd9Sstevel@tonic-gate #endif
3527c478bd9Sstevel@tonic-gate 			comdiv--;
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate #ifdef pdp11
3567c478bd9Sstevel@tonic-gate 	else
3577c478bd9Sstevel@tonic-gate 		*execargs = (char *)dolv;	/* for `ps' cmd */
3587c478bd9Sstevel@tonic-gate #endif
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	exfile(0);
3627c478bd9Sstevel@tonic-gate 	done(0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
365*965005c8Schin static void
366*965005c8Schin exfile(int prof)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	time_t	mailtime = 0;	/* Must not be a register variable */
3697c478bd9Sstevel@tonic-gate 	time_t 	curtime = 0;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * move input
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if (input > 0)
3757c478bd9Sstevel@tonic-gate 	{
3767c478bd9Sstevel@tonic-gate 		Ldup(input, INIO);
3777c478bd9Sstevel@tonic-gate 		input = INIO;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	setmode(prof);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	if (setjmp(errshell) && prof)
3847c478bd9Sstevel@tonic-gate 	{
3857c478bd9Sstevel@tonic-gate 		close(input);
3867c478bd9Sstevel@tonic-gate 		(void) endjobs(0);
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * error return here
3917c478bd9Sstevel@tonic-gate 	 */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	loopcnt = peekc = peekn = 0;
3947c478bd9Sstevel@tonic-gate 	fndef = 0;
3957c478bd9Sstevel@tonic-gate 	nohash = 0;
3967c478bd9Sstevel@tonic-gate 	iopend = 0;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (input >= 0)
3997c478bd9Sstevel@tonic-gate 		initf(input);
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * command loop
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	for (;;)
4047c478bd9Sstevel@tonic-gate 	{
4057c478bd9Sstevel@tonic-gate 		tdystak(0);
4067c478bd9Sstevel@tonic-gate 		stakchk();	/* may reduce sbrk */
4077c478bd9Sstevel@tonic-gate 		exitset();
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		if ((flags & prompt) && standin->fstak == 0 && !eof)
4107c478bd9Sstevel@tonic-gate 		{
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			if (mailp)
4137c478bd9Sstevel@tonic-gate 			{
4147c478bd9Sstevel@tonic-gate 				time(&curtime);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 				if ((curtime - mailtime) >= mailchk)
4177c478bd9Sstevel@tonic-gate 				{
4187c478bd9Sstevel@tonic-gate 					chkmail();
4197c478bd9Sstevel@tonic-gate 					mailtime = curtime;
4207c478bd9Sstevel@tonic-gate 				}
4217c478bd9Sstevel@tonic-gate 			}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 			/* necessary to print jobs in a timely manner */
4247c478bd9Sstevel@tonic-gate 			if (trapnote & TRAPSET)
4257c478bd9Sstevel@tonic-gate 				chktrap();
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 			prs(ps1nod.namval);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4307c478bd9Sstevel@tonic-gate 			alarm(TIMEOUT);
4317c478bd9Sstevel@tonic-gate #endif
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		trapnote = 0;
4367c478bd9Sstevel@tonic-gate 		peekc = readwc();
4377c478bd9Sstevel@tonic-gate 		if (eof) {
4387c478bd9Sstevel@tonic-gate 			if (endjobs(JOB_STOPPED))
4397c478bd9Sstevel@tonic-gate 				return;
4407c478bd9Sstevel@tonic-gate 			eof = 0;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4447c478bd9Sstevel@tonic-gate 		alarm(0);
4457c478bd9Sstevel@tonic-gate #endif
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		{
448*965005c8Schin 			struct trenod *t;
4497c478bd9Sstevel@tonic-gate 			t = cmd(NL, MTFLG);
4507c478bd9Sstevel@tonic-gate 			if (t == NULL && flags & ttyflg)
4517c478bd9Sstevel@tonic-gate 				freejobs();
4527c478bd9Sstevel@tonic-gate 			else
4537c478bd9Sstevel@tonic-gate 				execute(t, 0, eflag);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		eof |= (flags & oneflg);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
461*965005c8Schin void
462*965005c8Schin chkpr(void)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	if ((flags & prompt) && standin->fstak == 0)
4657c478bd9Sstevel@tonic-gate 		prs(ps2nod.namval);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
468*965005c8Schin void
469*965005c8Schin settmp(void)
4707c478bd9Sstevel@tonic-gate {
471*965005c8Schin 	int len;
4727c478bd9Sstevel@tonic-gate 	serial = 0;
473*965005c8Schin 	if ((len = snprintf((char *)tmpout, TMPOUTSZ, "/tmp/sh%u", mypid)) >=
474*965005c8Schin 	    TMPOUTSZ) {
475*965005c8Schin 		/*
476*965005c8Schin 		 * TMPOUTSZ should be big enough, but if it isn't,
477*965005c8Schin 		 * we'll at least try to create tmp files with
478*965005c8Schin 		 * a truncated tmpfile name at tmpout.
479*965005c8Schin 		 */
480*965005c8Schin 		tmpout_offset = TMPOUTSZ - 1;
481*965005c8Schin 	} else {
482*965005c8Schin 		tmpout_offset = len;
483*965005c8Schin 	}
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
486*965005c8Schin static void
487*965005c8Schin Ldup(int fa, int fb)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate #ifdef RES
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	dup(fa | DUPFLG, fb);
4927c478bd9Sstevel@tonic-gate 	close(fa);
4937c478bd9Sstevel@tonic-gate 	ioctl(fb, FIOCLEX, 0);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate #else
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (fa >= 0) {
4987c478bd9Sstevel@tonic-gate 		if (fa != fb)
4997c478bd9Sstevel@tonic-gate 		{
5007c478bd9Sstevel@tonic-gate 			close(fb);
5017c478bd9Sstevel@tonic-gate 			fcntl(fa, 0, fb); /* normal dup */
5027c478bd9Sstevel@tonic-gate 			close(fa);
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		fcntl(fb, 2, 1);	/* autoclose for fb */
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
510*965005c8Schin void
511*965005c8Schin chkmail(void)
5127c478bd9Sstevel@tonic-gate {
513*965005c8Schin 	unsigned char 	*s = mailp;
514*965005c8Schin 	unsigned char	*save;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	long	*ptr = mod_time;
5177c478bd9Sstevel@tonic-gate 	unsigned char	*start;
5187c478bd9Sstevel@tonic-gate 	BOOL	flg;
5197c478bd9Sstevel@tonic-gate 	struct stat	statb;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	while (*s) {
5227c478bd9Sstevel@tonic-gate 		start = s;
5237c478bd9Sstevel@tonic-gate 		save = 0;
5247c478bd9Sstevel@tonic-gate 		flg = 0;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		while (*s) {
5277c478bd9Sstevel@tonic-gate 			if (*s != COLON) {
5287c478bd9Sstevel@tonic-gate 				if (*s == '%' && save == 0)
5297c478bd9Sstevel@tonic-gate 					save = s;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 				s++;
5327c478bd9Sstevel@tonic-gate 			} else {
5337c478bd9Sstevel@tonic-gate 				flg = 1;
5347c478bd9Sstevel@tonic-gate 				*s = 0;
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if (save)
5397c478bd9Sstevel@tonic-gate 			*save = 0;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		if (*start && stat((const char *)start, &statb) >= 0) {
5427c478bd9Sstevel@tonic-gate 			if (statb.st_size && *ptr &&
5437c478bd9Sstevel@tonic-gate 			    statb.st_mtime != *ptr) {
5447c478bd9Sstevel@tonic-gate 				if (save) {
5457c478bd9Sstevel@tonic-gate 					prs(save+1);
5467c478bd9Sstevel@tonic-gate 					newline();
5477c478bd9Sstevel@tonic-gate 				}
5487c478bd9Sstevel@tonic-gate 				else
5497c478bd9Sstevel@tonic-gate 					prs(mailmsg);
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 			*ptr = statb.st_mtime;
5527c478bd9Sstevel@tonic-gate 		} else if (*ptr == 0)
5537c478bd9Sstevel@tonic-gate 			*ptr = 1;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (save)
5567c478bd9Sstevel@tonic-gate 			*save = '%';
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		if (flg)
5597c478bd9Sstevel@tonic-gate 			*s++ = COLON;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		ptr++;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
565*965005c8Schin void
566*965005c8Schin setmail(unsigned char *mailpath)
5677c478bd9Sstevel@tonic-gate {
568*965005c8Schin 	unsigned char	*s = mailpath;
569*965005c8Schin 	int 		cnt = 1;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	long	*ptr;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	free(mod_time);
5747c478bd9Sstevel@tonic-gate 	if (mailp = mailpath)
5757c478bd9Sstevel@tonic-gate 	{
5767c478bd9Sstevel@tonic-gate 		while (*s)
5777c478bd9Sstevel@tonic-gate 		{
5787c478bd9Sstevel@tonic-gate 			if (*s == COLON)
5797c478bd9Sstevel@tonic-gate 				cnt += 1;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 			s++;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		ptr = mod_time = (long *)alloc(sizeof (long) * cnt);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		while (cnt)
5877c478bd9Sstevel@tonic-gate 		{
5887c478bd9Sstevel@tonic-gate 			*ptr = 0;
5897c478bd9Sstevel@tonic-gate 			ptr++;
5907c478bd9Sstevel@tonic-gate 			cnt--;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate void
5967c478bd9Sstevel@tonic-gate setwidth()
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	unsigned char *name = lookup("LC_CTYPE")->namval;
5997c478bd9Sstevel@tonic-gate 	if (!name || !*name)
6007c478bd9Sstevel@tonic-gate 		name = lookup("LANG")->namval;
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (localedir_exists) {
6057c478bd9Sstevel@tonic-gate 		if (!name || !*name)
6067c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_CTYPE, "C");
6077c478bd9Sstevel@tonic-gate 		else
6087c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_CTYPE, (const char *)name);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
612*965005c8Schin void
613*965005c8Schin setmode(int prof)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * decide whether interactive
6177c478bd9Sstevel@tonic-gate 	 */
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if ((flags & intflg) ||
6207c478bd9Sstevel@tonic-gate 	    ((flags&oneflg) == 0 &&
6217c478bd9Sstevel@tonic-gate 	    isatty(output) &&
6227c478bd9Sstevel@tonic-gate 	    isatty(input)))
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	{
6257c478bd9Sstevel@tonic-gate 		dfault(&ps1nod, (geteuid() ? stdprompt : supprompt));
6267c478bd9Sstevel@tonic-gate 		dfault(&ps2nod, readmsg);
6277c478bd9Sstevel@tonic-gate 		flags |= ttyflg | prompt;
6287c478bd9Sstevel@tonic-gate 		if (mailpnod.namflg != N_DEFAULT)
6297c478bd9Sstevel@tonic-gate 			setmail(mailpnod.namval);
6307c478bd9Sstevel@tonic-gate 		else
6317c478bd9Sstevel@tonic-gate 			setmail(mailnod.namval);
6327c478bd9Sstevel@tonic-gate 		startjobs();
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	else
6357c478bd9Sstevel@tonic-gate 	{
6367c478bd9Sstevel@tonic-gate 		flags |= prof;
6377c478bd9Sstevel@tonic-gate 		flags &= ~prompt;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  * A generic call back routine to output error messages from the
6437c478bd9Sstevel@tonic-gate  * policy backing functions called by pfsh.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * msg must contain '\n' if a new line is to be printed.
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate void
6487c478bd9Sstevel@tonic-gate secpolicy_print(int level, const char *msg)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	switch (level) {
6517c478bd9Sstevel@tonic-gate 	case SECPOLICY_WARN:
6527c478bd9Sstevel@tonic-gate 	default:
6537c478bd9Sstevel@tonic-gate 		prs(msg);	/* prs() does gettext() */
6547c478bd9Sstevel@tonic-gate 		return;
6557c478bd9Sstevel@tonic-gate 	case SECPOLICY_ERROR:
6567c478bd9Sstevel@tonic-gate 		error(msg);
6577c478bd9Sstevel@tonic-gate 		break;
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate }
660