xref: /titanic_51/usr/src/cmd/sh/main.c (revision 39e7390a771e8a17884d4f1558a2a41422b38104)
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
5*39e7390aSna195498  * Common Development and Distribution License (the "License").
6*39e7390aSna195498  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*39e7390aSna195498 
227c478bd9Sstevel@tonic-gate /*
23*39e7390aSna195498  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * UNIX shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate #include	"sym.h"
377c478bd9Sstevel@tonic-gate #include	"timeout.h"
38965005c8Schin #include	<stdio.h>
397c478bd9Sstevel@tonic-gate #include	<sys/types.h>
407c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
417c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
427c478bd9Sstevel@tonic-gate #include	"dup.h"
437c478bd9Sstevel@tonic-gate #include	"sh_policy.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef RES
467c478bd9Sstevel@tonic-gate #include	<sgtty.h>
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate pid_t mypid, mypgid, mysid;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static BOOL	beenhere = FALSE;
52965005c8Schin unsigned char	tmpout[TMPOUTSZ];
537c478bd9Sstevel@tonic-gate struct fileblk	stdfile;
547c478bd9Sstevel@tonic-gate struct fileblk *standin = &stdfile;
557c478bd9Sstevel@tonic-gate int mailchk = 0;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static unsigned char	*mailp;
587c478bd9Sstevel@tonic-gate static long	*mod_time = 0;
597c478bd9Sstevel@tonic-gate static BOOL login_shell = FALSE;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #if vax
627c478bd9Sstevel@tonic-gate char **execargs = (char **)(0x7ffffffc);
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #if pdp11
667c478bd9Sstevel@tonic-gate char **execargs = (char **)(-2);
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
70965005c8Schin static void	exfile();
717c478bd9Sstevel@tonic-gate extern unsigned char 	*simple();
72965005c8Schin static void Ldup(int, int);
73965005c8Schin void settmp(void);
74965005c8Schin void chkmail(void);
75965005c8Schin void setmail(unsigned char *);
767c478bd9Sstevel@tonic-gate 
77965005c8Schin int
78965005c8Schin main(int c, char *v[], char *e[])
797c478bd9Sstevel@tonic-gate {
80965005c8Schin 	int		rflag = ttyflg;
817c478bd9Sstevel@tonic-gate 	int		rsflag = 1;	/* local restricted flag */
82965005c8Schin 	unsigned char	*flagc = flagadr;
837c478bd9Sstevel@tonic-gate 	struct namnod	*n;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	mypid = getpid();
867c478bd9Sstevel@tonic-gate 	mypgid = getpgid(mypid);
877c478bd9Sstevel@tonic-gate 	mysid = getsid(mypid);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	localedir_exists = (access(localedir, F_OK) == 0);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * initialize storage allocation
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (stakbot == 0) {
997c478bd9Sstevel@tonic-gate 	addblok((unsigned)0);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * If the first character of the last path element of v[0] is "-"
1047c478bd9Sstevel@tonic-gate 	 * (ex. -sh, or /bin/-sh), this is a login shell
1057c478bd9Sstevel@tonic-gate 	 */
1067c478bd9Sstevel@tonic-gate 	if (*simple(v[0]) == '-') {
1077c478bd9Sstevel@tonic-gate 		signal(SIGXCPU, SIG_DFL);
1087c478bd9Sstevel@tonic-gate 		signal(SIGXFSZ, SIG_DFL);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * As the previous comment states, this is a login shell.
1127c478bd9Sstevel@tonic-gate 		 * Therefore, we set the login_shell flag to explicitly
1137c478bd9Sstevel@tonic-gate 		 * indicate this condition.
1147c478bd9Sstevel@tonic-gate 		 */
1157c478bd9Sstevel@tonic-gate 		login_shell = TRUE;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	stdsigs();
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * set names from userenv
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	setup_env();
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * LC_MESSAGES is set here so that early error messages will
1287c478bd9Sstevel@tonic-gate 	 * come out in the right style.
1297c478bd9Sstevel@tonic-gate 	 * Note that LC_CTYPE is done later on and is *not*
1307c478bd9Sstevel@tonic-gate 	 * taken from the previous environ
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Do locale processing only if /usr is mounted.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	if (localedir_exists)
1377c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
1387c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1397c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * This is a profile shell if the simple name of argv[0] is
1457c478bd9Sstevel@tonic-gate 	 * pfsh or -pfsh
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("pfsh", simple(*v)) || eq("-pfsh", simple(*v)))) {
1487c478bd9Sstevel@tonic-gate 		flags |= pfshflg;
1497c478bd9Sstevel@tonic-gate 		secpolicy_init();
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * 'rsflag' is zero if SHELL variable is
1547c478bd9Sstevel@tonic-gate 	 *  set in environment and
1557c478bd9Sstevel@tonic-gate 	 *  the simple file part of the value.
1567c478bd9Sstevel@tonic-gate 	 *  is rsh
1577c478bd9Sstevel@tonic-gate 	 */
158db397771Schin 	if (n = findnam("SHELL")) {
1597c478bd9Sstevel@tonic-gate 		if (eq("rsh", simple(n->namval)))
1607c478bd9Sstevel@tonic-gate 			rsflag = 0;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * a shell is also restricted if the simple name of argv(0) is
1657c478bd9Sstevel@tonic-gate 	 * rsh or -rsh in its simple name
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #ifndef RES
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (c > 0 && (eq("rsh", simple(*v)) || eq("-rsh", simple(*v))))
1717c478bd9Sstevel@tonic-gate 		rflag = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (eq("jsh", simple(*v)) || eq("-jsh", simple(*v)))
1767c478bd9Sstevel@tonic-gate 		flags |= monitorflg;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	hcreate();
1797c478bd9Sstevel@tonic-gate 	set_dotpath();
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * look for options
1847c478bd9Sstevel@tonic-gate 	 * dolc is $#
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	dolc = options(c, v);
1877c478bd9Sstevel@tonic-gate 
188db397771Schin 	if (dolc < 2) {
1897c478bd9Sstevel@tonic-gate 		flags |= stdflg;
1907c478bd9Sstevel@tonic-gate 		{
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			while (*flagc)
1937c478bd9Sstevel@tonic-gate 				flagc++;
1947c478bd9Sstevel@tonic-gate 			*flagc++ = STDFLG;
1957c478bd9Sstevel@tonic-gate 			*flagc = 0;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	if ((flags & stdflg) == 0)
1997c478bd9Sstevel@tonic-gate 		dolc--;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if ((flags & privflg) == 0) {
202965005c8Schin 		uid_t euid;
203965005c8Schin 		gid_t egid;
204965005c8Schin 		uid_t ruid;
205965005c8Schin 		gid_t rgid;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * Determine all of the user's id #'s for this process and
2097c478bd9Sstevel@tonic-gate 		 * then decide if this shell is being entered as a result
2107c478bd9Sstevel@tonic-gate 		 * of a fork/exec.
2117c478bd9Sstevel@tonic-gate 		 * If the effective uid/gid do NOT match and the euid/egid
2127c478bd9Sstevel@tonic-gate 		 * is < 100 and the egid is NOT 1, reset the uid and gid to
2137c478bd9Sstevel@tonic-gate 		 * the user originally calling this process.
2147c478bd9Sstevel@tonic-gate 		 */
2157c478bd9Sstevel@tonic-gate 		euid = geteuid();
2167c478bd9Sstevel@tonic-gate 		ruid = getuid();
2177c478bd9Sstevel@tonic-gate 		egid = getegid();
2187c478bd9Sstevel@tonic-gate 		rgid = getgid();
2197c478bd9Sstevel@tonic-gate 		if ((euid != ruid) && (euid < 100))
2207c478bd9Sstevel@tonic-gate 			setuid(ruid);   /* reset the uid to the orig user */
2217c478bd9Sstevel@tonic-gate 		if ((egid != rgid) && ((egid < 100) && (egid != 1)))
2227c478bd9Sstevel@tonic-gate 			setgid(rgid);   /* reset the gid to the orig user */
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	dolv = (unsigned char **)v + c - dolc;
2267c478bd9Sstevel@tonic-gate 	dolc--;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * return here for shell file execution
2307c478bd9Sstevel@tonic-gate 	 * but not for parenthesis subshells
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	if (setjmp(subshell)) {
2337c478bd9Sstevel@tonic-gate 		freejobs();
2347c478bd9Sstevel@tonic-gate 		flags |= subsh;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * number of positional parameters
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	replace(&cmdadr, dolv[0]);	/* cmdadr is $0 */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * set pidname '$$'
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	assnum(&pidadr, (long)mypid);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * set up temp file names
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	settmp();
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * default internal field separators
2547c478bd9Sstevel@tonic-gate 	 * Do not allow importing of IFS from parent shell.
2557c478bd9Sstevel@tonic-gate 	 * setup_env() may have set anything from parent shell to IFS.
2567c478bd9Sstevel@tonic-gate 	 * Always set the default ifs to IFS.
2577c478bd9Sstevel@tonic-gate 	 */
258965005c8Schin 	assign(&ifsnod, (unsigned char *)sptbnl);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	dfault(&mchknod, MAILCHECK);
2617c478bd9Sstevel@tonic-gate 	mailchk = stoi(mchknod.namval);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* initialize OPTIND for getopt */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	n = lookup("OPTIND");
266965005c8Schin 	assign(n, (unsigned char *)"1");
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * make sure that option parsing starts
2697c478bd9Sstevel@tonic-gate 	 * at first character
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	_sp = 1;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if ((beenhere++) == FALSE)	/* ? profile */
2747c478bd9Sstevel@tonic-gate 	{
2757c478bd9Sstevel@tonic-gate 		if ((login_shell == TRUE) && (flags & privflg) == 0) {
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			/* system profile */
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate #ifndef RES
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 			if ((input = pathopen(nullstr, sysprofile)) >= 0)
2827c478bd9Sstevel@tonic-gate 				exfile(rflag);		/* file exists */
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate #endif
2857c478bd9Sstevel@tonic-gate 			/* user profile */
2867c478bd9Sstevel@tonic-gate 
287db397771Schin 			if ((input = pathopen(homenod.namval, profile)) >= 0) {
2887c478bd9Sstevel@tonic-gate 				exfile(rflag);
2897c478bd9Sstevel@tonic-gate 				flags &= ~ttyflg;
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		if (rsflag == 0 || rflag == 0) {
2937c478bd9Sstevel@tonic-gate 			if ((flags & rshflg) == 0) {
2947c478bd9Sstevel@tonic-gate 				while (*flagc)
2957c478bd9Sstevel@tonic-gate 					flagc++;
2967c478bd9Sstevel@tonic-gate 				*flagc++ = 'r';
2977c478bd9Sstevel@tonic-gate 				*flagc = '\0';
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 			flags |= rshflg;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		/*
3037c478bd9Sstevel@tonic-gate 		 * open input file if specified
3047c478bd9Sstevel@tonic-gate 		 */
305db397771Schin 		if (comdiv) {
3067c478bd9Sstevel@tonic-gate 			estabf(comdiv);
3077c478bd9Sstevel@tonic-gate 			input = -1;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		else
3107c478bd9Sstevel@tonic-gate 		{
3117c478bd9Sstevel@tonic-gate 			if (flags & stdflg) {
3127c478bd9Sstevel@tonic-gate 				input = 0;
3137c478bd9Sstevel@tonic-gate 			} else {
3147c478bd9Sstevel@tonic-gate 			/*
3157c478bd9Sstevel@tonic-gate 			 * If the command file specified by 'cmdadr'
3167c478bd9Sstevel@tonic-gate 			 * doesn't exist, chkopen() will fail calling
3177c478bd9Sstevel@tonic-gate 			 * exitsh(). If this is a login shell and
3187c478bd9Sstevel@tonic-gate 			 * the $HOME/.profile file does not exist, the
3197c478bd9Sstevel@tonic-gate 			 * above statement "flags &= ~ttyflg" does not
3207c478bd9Sstevel@tonic-gate 			 * get executed and this makes exitsh() call
3217c478bd9Sstevel@tonic-gate 			 * longjmp() instead of exiting. longjmp() will
3227c478bd9Sstevel@tonic-gate 			 * return to the location specified by the last
3237c478bd9Sstevel@tonic-gate 			 * active jmpbuffer, which is the one set up in
3247c478bd9Sstevel@tonic-gate 			 * the function exfile() called after the system
3257c478bd9Sstevel@tonic-gate 			 * profile file is executed (see lines above).
3267c478bd9Sstevel@tonic-gate 			 * This would cause an infinite loop, because
3277c478bd9Sstevel@tonic-gate 			 * chkopen() will continue to fail and exitsh()
3287c478bd9Sstevel@tonic-gate 			 * to call longjmp(). To make exitsh() exit instead
3297c478bd9Sstevel@tonic-gate 			 * of calling longjmp(), we then set the flag forcexit
3307c478bd9Sstevel@tonic-gate 			 * at this stage.
3317c478bd9Sstevel@tonic-gate 			 */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 				flags |= forcexit;
3347c478bd9Sstevel@tonic-gate 				input = chkopen(cmdadr, 0);
3357c478bd9Sstevel@tonic-gate 				flags &= ~forcexit;
3367c478bd9Sstevel@tonic-gate 			}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate #ifdef ACCT
3397c478bd9Sstevel@tonic-gate 			if (input != 0)
3407c478bd9Sstevel@tonic-gate 				preacct(cmdadr);
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate 			comdiv--;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate #ifdef pdp11
3467c478bd9Sstevel@tonic-gate 	else
3477c478bd9Sstevel@tonic-gate 		*execargs = (char *)dolv;	/* for `ps' cmd */
3487c478bd9Sstevel@tonic-gate #endif
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	exfile(0);
3527c478bd9Sstevel@tonic-gate 	done(0);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
355965005c8Schin static void
356965005c8Schin exfile(int prof)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	time_t	mailtime = 0;	/* Must not be a register variable */
3597c478bd9Sstevel@tonic-gate 	time_t 	curtime = 0;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * move input
3637c478bd9Sstevel@tonic-gate 	 */
364db397771Schin 	if (input > 0) {
3657c478bd9Sstevel@tonic-gate 		Ldup(input, INIO);
3667c478bd9Sstevel@tonic-gate 		input = INIO;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	setmode(prof);
3717c478bd9Sstevel@tonic-gate 
372db397771Schin 	if (setjmp(errshell) && prof) {
3737c478bd9Sstevel@tonic-gate 		close(input);
3747c478bd9Sstevel@tonic-gate 		(void) endjobs(0);
3757c478bd9Sstevel@tonic-gate 		return;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * error return here
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	loopcnt = peekc = peekn = 0;
3827c478bd9Sstevel@tonic-gate 	fndef = 0;
3837c478bd9Sstevel@tonic-gate 	nohash = 0;
3847c478bd9Sstevel@tonic-gate 	iopend = 0;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (input >= 0)
3877c478bd9Sstevel@tonic-gate 		initf(input);
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * command loop
3907c478bd9Sstevel@tonic-gate 	 */
391db397771Schin 	for (;;) {
3927c478bd9Sstevel@tonic-gate 		tdystak(0);
3937c478bd9Sstevel@tonic-gate 		stakchk();	/* may reduce sbrk */
3947c478bd9Sstevel@tonic-gate 		exitset();
3957c478bd9Sstevel@tonic-gate 
396db397771Schin 		if ((flags & prompt) && standin->fstak == 0 && !eof) {
3977c478bd9Sstevel@tonic-gate 
398db397771Schin 			if (mailp) {
3997c478bd9Sstevel@tonic-gate 				time(&curtime);
4007c478bd9Sstevel@tonic-gate 
401db397771Schin 				if ((curtime - mailtime) >= mailchk) {
4027c478bd9Sstevel@tonic-gate 					chkmail();
4037c478bd9Sstevel@tonic-gate 					mailtime = curtime;
4047c478bd9Sstevel@tonic-gate 				}
4057c478bd9Sstevel@tonic-gate 			}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			/* necessary to print jobs in a timely manner */
4087c478bd9Sstevel@tonic-gate 			if (trapnote & TRAPSET)
4097c478bd9Sstevel@tonic-gate 				chktrap();
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 			prs(ps1nod.namval);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4147c478bd9Sstevel@tonic-gate 			alarm(TIMEOUT);
4157c478bd9Sstevel@tonic-gate #endif
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		trapnote = 0;
4207c478bd9Sstevel@tonic-gate 		peekc = readwc();
4217c478bd9Sstevel@tonic-gate 		if (eof) {
4227c478bd9Sstevel@tonic-gate 			if (endjobs(JOB_STOPPED))
4237c478bd9Sstevel@tonic-gate 				return;
4247c478bd9Sstevel@tonic-gate 			eof = 0;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate #ifdef TIME_OUT
4287c478bd9Sstevel@tonic-gate 		alarm(0);
4297c478bd9Sstevel@tonic-gate #endif
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		{
432965005c8Schin 			struct trenod *t;
4337c478bd9Sstevel@tonic-gate 			t = cmd(NL, MTFLG);
4347c478bd9Sstevel@tonic-gate 			if (t == NULL && flags & ttyflg)
4357c478bd9Sstevel@tonic-gate 				freejobs();
4367c478bd9Sstevel@tonic-gate 			else
4377c478bd9Sstevel@tonic-gate 				execute(t, 0, eflag);
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		eof |= (flags & oneflg);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
445965005c8Schin void
446965005c8Schin chkpr(void)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	if ((flags & prompt) && standin->fstak == 0)
4497c478bd9Sstevel@tonic-gate 		prs(ps2nod.namval);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
452965005c8Schin void
453965005c8Schin settmp(void)
4547c478bd9Sstevel@tonic-gate {
455965005c8Schin 	int len;
4567c478bd9Sstevel@tonic-gate 	serial = 0;
457965005c8Schin 	if ((len = snprintf((char *)tmpout, TMPOUTSZ, "/tmp/sh%u", mypid)) >=
458965005c8Schin 	    TMPOUTSZ) {
459965005c8Schin 		/*
460965005c8Schin 		 * TMPOUTSZ should be big enough, but if it isn't,
461965005c8Schin 		 * we'll at least try to create tmp files with
462965005c8Schin 		 * a truncated tmpfile name at tmpout.
463965005c8Schin 		 */
464965005c8Schin 		tmpout_offset = TMPOUTSZ - 1;
465965005c8Schin 	} else {
466965005c8Schin 		tmpout_offset = len;
467965005c8Schin 	}
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
470965005c8Schin static void
471965005c8Schin Ldup(int fa, int fb)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate #ifdef RES
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	dup(fa | DUPFLG, fb);
4767c478bd9Sstevel@tonic-gate 	close(fa);
4777c478bd9Sstevel@tonic-gate 	ioctl(fb, FIOCLEX, 0);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate #else
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (fa >= 0) {
482db397771Schin 		if (fa != fb) {
4837c478bd9Sstevel@tonic-gate 			close(fb);
4847c478bd9Sstevel@tonic-gate 			fcntl(fa, 0, fb); /* normal dup */
4857c478bd9Sstevel@tonic-gate 			close(fa);
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		fcntl(fb, 2, 1);	/* autoclose for fb */
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
493965005c8Schin void
494965005c8Schin chkmail(void)
4957c478bd9Sstevel@tonic-gate {
496965005c8Schin 	unsigned char 	*s = mailp;
497965005c8Schin 	unsigned char	*save;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	long	*ptr = mod_time;
5007c478bd9Sstevel@tonic-gate 	unsigned char	*start;
5017c478bd9Sstevel@tonic-gate 	BOOL	flg;
5027c478bd9Sstevel@tonic-gate 	struct stat	statb;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	while (*s) {
5057c478bd9Sstevel@tonic-gate 		start = s;
5067c478bd9Sstevel@tonic-gate 		save = 0;
5077c478bd9Sstevel@tonic-gate 		flg = 0;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		while (*s) {
5107c478bd9Sstevel@tonic-gate 			if (*s != COLON) {
5117c478bd9Sstevel@tonic-gate 				if (*s == '%' && save == 0)
5127c478bd9Sstevel@tonic-gate 					save = s;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 				s++;
5157c478bd9Sstevel@tonic-gate 			} else {
5167c478bd9Sstevel@tonic-gate 				flg = 1;
5177c478bd9Sstevel@tonic-gate 				*s = 0;
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		if (save)
5227c478bd9Sstevel@tonic-gate 			*save = 0;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		if (*start && stat((const char *)start, &statb) >= 0) {
5257c478bd9Sstevel@tonic-gate 			if (statb.st_size && *ptr &&
5267c478bd9Sstevel@tonic-gate 			    statb.st_mtime != *ptr) {
5277c478bd9Sstevel@tonic-gate 				if (save) {
5287c478bd9Sstevel@tonic-gate 					prs(save+1);
5297c478bd9Sstevel@tonic-gate 					newline();
5307c478bd9Sstevel@tonic-gate 				}
5317c478bd9Sstevel@tonic-gate 				else
532*39e7390aSna195498 					prs(_gettext(mailmsg));
5337c478bd9Sstevel@tonic-gate 			}
5347c478bd9Sstevel@tonic-gate 			*ptr = statb.st_mtime;
5357c478bd9Sstevel@tonic-gate 		} else if (*ptr == 0)
5367c478bd9Sstevel@tonic-gate 			*ptr = 1;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if (save)
5397c478bd9Sstevel@tonic-gate 			*save = '%';
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		if (flg)
5427c478bd9Sstevel@tonic-gate 			*s++ = COLON;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		ptr++;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
548965005c8Schin void
549965005c8Schin setmail(unsigned char *mailpath)
5507c478bd9Sstevel@tonic-gate {
551965005c8Schin 	unsigned char	*s = mailpath;
552965005c8Schin 	int 		cnt = 1;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	long	*ptr;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	free(mod_time);
557db397771Schin 	if (mailp = mailpath) {
558db397771Schin 		while (*s) {
5597c478bd9Sstevel@tonic-gate 			if (*s == COLON)
5607c478bd9Sstevel@tonic-gate 				cnt += 1;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			s++;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		ptr = mod_time = (long *)alloc(sizeof (long) * cnt);
5667c478bd9Sstevel@tonic-gate 
567db397771Schin 		while (cnt) {
5687c478bd9Sstevel@tonic-gate 			*ptr = 0;
5697c478bd9Sstevel@tonic-gate 			ptr++;
5707c478bd9Sstevel@tonic-gate 			cnt--;
5717c478bd9Sstevel@tonic-gate 		}
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate void
576965005c8Schin setmode(int prof)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * decide whether interactive
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if ((flags & intflg) ||
5837c478bd9Sstevel@tonic-gate 	    ((flags&oneflg) == 0 &&
5847c478bd9Sstevel@tonic-gate 	    isatty(output) &&
5857c478bd9Sstevel@tonic-gate 	    isatty(input)))
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	{
5887c478bd9Sstevel@tonic-gate 		dfault(&ps1nod, (geteuid() ? stdprompt : supprompt));
5897c478bd9Sstevel@tonic-gate 		dfault(&ps2nod, readmsg);
5907c478bd9Sstevel@tonic-gate 		flags |= ttyflg | prompt;
5917c478bd9Sstevel@tonic-gate 		if (mailpnod.namflg != N_DEFAULT)
5927c478bd9Sstevel@tonic-gate 			setmail(mailpnod.namval);
5937c478bd9Sstevel@tonic-gate 		else
5947c478bd9Sstevel@tonic-gate 			setmail(mailnod.namval);
5957c478bd9Sstevel@tonic-gate 		startjobs();
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	else
5987c478bd9Sstevel@tonic-gate 	{
5997c478bd9Sstevel@tonic-gate 		flags |= prof;
6007c478bd9Sstevel@tonic-gate 		flags &= ~prompt;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * A generic call back routine to output error messages from the
6067c478bd9Sstevel@tonic-gate  * policy backing functions called by pfsh.
6077c478bd9Sstevel@tonic-gate  *
6087c478bd9Sstevel@tonic-gate  * msg must contain '\n' if a new line is to be printed.
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate void
6117c478bd9Sstevel@tonic-gate secpolicy_print(int level, const char *msg)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	switch (level) {
6147c478bd9Sstevel@tonic-gate 	case SECPOLICY_WARN:
6157c478bd9Sstevel@tonic-gate 	default:
616*39e7390aSna195498 		prs(_gettext(msg));
6177c478bd9Sstevel@tonic-gate 		return;
6187c478bd9Sstevel@tonic-gate 	case SECPOLICY_ERROR:
6197c478bd9Sstevel@tonic-gate 		error(msg);
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate }
623