xref: /illumos-gate/usr/src/cmd/bnu/uuxqt.c (revision 2a8bcb4e)
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*462be471Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*462be471Sceastha /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*462be471Sceastha /*	  All Rights Reserved  	*/
29*462be471Sceastha 
307c478bd9Sstevel@tonic-gate #include "uucp.h"
317c478bd9Sstevel@tonic-gate #include "log.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * execute commands set up by a uux command,
357c478bd9Sstevel@tonic-gate  * usually from a remote machine - set by uucp.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifndef	V7
397c478bd9Sstevel@tonic-gate #define	LOGNAME	"LOGNAME=uucp"
407c478bd9Sstevel@tonic-gate #else
417c478bd9Sstevel@tonic-gate #define	LOGNAME	"USER=uucp"
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	C_COMMAND	1
457c478bd9Sstevel@tonic-gate #define	C_FILE		2
467c478bd9Sstevel@tonic-gate #define	BAD_COMMAND	1
477c478bd9Sstevel@tonic-gate #define	BAD_FILE	2
487c478bd9Sstevel@tonic-gate #define	USAGEPREFIX	"Usage:"
497c478bd9Sstevel@tonic-gate #define	USAGE		"[-x DEBUG] [-s SYSTEM]"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate char	_Xfile[MAXFULLNAME];
527c478bd9Sstevel@tonic-gate char	_Cmd[2 * BUFSIZ];	/* build up command buffer */
537c478bd9Sstevel@tonic-gate int	_CargType;		/* argument type of next C argument */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void retosndr(), uucpst();
567c478bd9Sstevel@tonic-gate static int chkFile();
577c478bd9Sstevel@tonic-gate static int doFileChk();
587c478bd9Sstevel@tonic-gate void cleanup(), xprocess();
597c478bd9Sstevel@tonic-gate 
60*462be471Sceastha int
main(argc,argv,envp)617c478bd9Sstevel@tonic-gate main(argc, argv, envp)
62*462be471Sceastha int argc;
637c478bd9Sstevel@tonic-gate char *argv[];
647c478bd9Sstevel@tonic-gate char *envp[];
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	DIR	 *fp1;
677c478bd9Sstevel@tonic-gate 	struct	limits limitval;
687c478bd9Sstevel@tonic-gate 	int	ret, maxnumb;
697c478bd9Sstevel@tonic-gate 	char	dirname[MAXFULLNAME], lockname[MAXFULLNAME];
707c478bd9Sstevel@tonic-gate 	void	onintr();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* Set locale environment variables local definitions */
737c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
747c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
757c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	(void) signal(SIGILL, onintr);
807c478bd9Sstevel@tonic-gate 	(void) signal(SIGTRAP, onintr);
817c478bd9Sstevel@tonic-gate 	(void) signal(SIGIOT, onintr);
827c478bd9Sstevel@tonic-gate 	(void) signal(SIGEMT, onintr);
837c478bd9Sstevel@tonic-gate 	(void) signal(SIGFPE, onintr);
847c478bd9Sstevel@tonic-gate 	(void) signal(SIGBUS, onintr);
857c478bd9Sstevel@tonic-gate 	(void) signal(SIGSEGV, onintr);
867c478bd9Sstevel@tonic-gate 	(void) signal(SIGSYS, onintr);
877c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, onintr);
887c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_IGN);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* choose LOGFILE */
917c478bd9Sstevel@tonic-gate 	(void) strcpy(Logfile, LOGUUXQT);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * get local system name
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	Env = envp;
977c478bd9Sstevel@tonic-gate 	Nstat.t_qtime = time((time_t *)0);
987c478bd9Sstevel@tonic-gate 	(void) strcpy(Progname, "uuxqt");
997c478bd9Sstevel@tonic-gate 	Pchar = 'Q';
1007c478bd9Sstevel@tonic-gate 	uucpname(Myname);
1017c478bd9Sstevel@tonic-gate 	Ofn = 1;
1027c478bd9Sstevel@tonic-gate 	Ifn = 0;
1037c478bd9Sstevel@tonic-gate 	dirname[0] = dirname[MAXFULLNAME-1] = NULLCHAR;
1047c478bd9Sstevel@tonic-gate 	while ((ret = getopt(argc, argv, "s:x:")) != EOF) {
1057c478bd9Sstevel@tonic-gate 		switch (ret) {
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		/*
1087c478bd9Sstevel@tonic-gate 		 * debugging level
1097c478bd9Sstevel@tonic-gate 		 */
1107c478bd9Sstevel@tonic-gate 		case 'x':
1117c478bd9Sstevel@tonic-gate 			Debug = atoi(optarg);
1127c478bd9Sstevel@tonic-gate 			if (Debug <= 0)
1137c478bd9Sstevel@tonic-gate 				Debug = 1;
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		case 's':
1177c478bd9Sstevel@tonic-gate 			/*
1187c478bd9Sstevel@tonic-gate 			 * fake out uuxqt and use the argument as if
1197c478bd9Sstevel@tonic-gate 			 * it were the spool directory for the purpose
1207c478bd9Sstevel@tonic-gate 			 * of determining what subdirectories to search
1217c478bd9Sstevel@tonic-gate 			 *  EX:	mkdir /tmp/foo; touch /tmp/foo/[baz, gorp]
1227c478bd9Sstevel@tonic-gate 			 *	uuxqt -s/tmp/foo
1237c478bd9Sstevel@tonic-gate 			 * this will cause uuxqt to only run on the sub
1247c478bd9Sstevel@tonic-gate 			 * baz and gorp in the Spool directory.  Trust me.
1257c478bd9Sstevel@tonic-gate 			 */
1267c478bd9Sstevel@tonic-gate 			(void) strlcpy(dirname, optarg,
1277c478bd9Sstevel@tonic-gate 			    (MAXFULLNAME - sizeof (SEQLOCK)));
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		default:
1317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s %s\n",
1327c478bd9Sstevel@tonic-gate 			    gettext(USAGEPREFIX), Progname, gettext(USAGE));
1337c478bd9Sstevel@tonic-gate 			exit(1);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	if (argc != optind) {
1377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s %s %s\n",
1387c478bd9Sstevel@tonic-gate 		    gettext(USAGEPREFIX), Progname, gettext(USAGE));
1397c478bd9Sstevel@tonic-gate 		exit(1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	DEBUG(4, "\n\n** START **\n%s", "");
1437c478bd9Sstevel@tonic-gate 	acInit("rexe");
1447c478bd9Sstevel@tonic-gate 	scInit("rexe");
1457c478bd9Sstevel@tonic-gate 	if (scanlimit("uuxqt", &limitval) == FAIL) {
1467c478bd9Sstevel@tonic-gate 	    DEBUG(1, "No limits for uuxqt in %s\n", LIMITS);
1477c478bd9Sstevel@tonic-gate 	} else {
1487c478bd9Sstevel@tonic-gate 	    maxnumb = limitval.totalmax;
1497c478bd9Sstevel@tonic-gate 	    if (maxnumb < 0) {
1507c478bd9Sstevel@tonic-gate 		DEBUG(4, "Non-positive limit for uuxqt in %s\n", LIMITS);
1517c478bd9Sstevel@tonic-gate 		DEBUG(1, "No limits for uuxqt\n%s", "");
1527c478bd9Sstevel@tonic-gate 	    } else {
1537c478bd9Sstevel@tonic-gate 		DEBUG(4, "Uuxqt limit %d -- ", maxnumb);
1547c478bd9Sstevel@tonic-gate 		ret = cuantos(X_LOCKPRE, X_LOCKDIR);
1557c478bd9Sstevel@tonic-gate 		DEBUG(4, "found %d -- ", ret);
1567c478bd9Sstevel@tonic-gate 		if (maxnumb >= 0 && ret >= maxnumb) {
1577c478bd9Sstevel@tonic-gate 		    DEBUG(4, "exiting.%s\n", "");
1587c478bd9Sstevel@tonic-gate 		    exit(0);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		DEBUG(4, "continuing.%s\n", "");
1617c478bd9Sstevel@tonic-gate 	    }
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * determine user who started uuxqt (in principle)
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	strcpy(User, "uucp");	/* in case all else fails (can't happen) */
1687c478bd9Sstevel@tonic-gate 	Uid = getuid();
1697c478bd9Sstevel@tonic-gate 	Euid = geteuid();	/* this should be UUCPUID */
1707c478bd9Sstevel@tonic-gate 	guinfo(Euid, User);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (Uid == 0)
1737c478bd9Sstevel@tonic-gate 		(void) setuid(UUCPUID);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	setuucp(User);
1767c478bd9Sstevel@tonic-gate 	DEBUG(4, "User - %s\n", User);
1777c478bd9Sstevel@tonic-gate 	guinfo(Uid, Loginuser);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	DEBUG(4, "process\n%s", "");
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	fp1 = opendir(Spool);
1847c478bd9Sstevel@tonic-gate 	ASSERT(fp1 != NULL, Ct_OPEN, Spool, errno);
1857c478bd9Sstevel@tonic-gate 	if (dirname[0] != NULLCHAR) {
1867c478bd9Sstevel@tonic-gate 		/* look for special characters in remote name */
1877c478bd9Sstevel@tonic-gate 		if (strpbrk(dirname, Shchar) != NULL) {
1887c478bd9Sstevel@tonic-gate 		    /* ignore naughty name */
1897c478bd9Sstevel@tonic-gate 		    DEBUG(4, "Bad remote name '%s'", dirname);
1907c478bd9Sstevel@tonic-gate 		    errent("BAD REMOTE NAME", dirname, 0, __FILE__, __LINE__);
1917c478bd9Sstevel@tonic-gate 		    closedir(fp1);
1927c478bd9Sstevel@tonic-gate 		    cleanup(101);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		(void) snprintf(lockname, sizeof (lockname), "%s.%s",
1977c478bd9Sstevel@tonic-gate 		    X_LOCK, dirname);
1987c478bd9Sstevel@tonic-gate 		if (mklock(lockname) == SUCCESS) {
1997c478bd9Sstevel@tonic-gate 			xprocess(dirname);
2007c478bd9Sstevel@tonic-gate 			rmlock(CNULL);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	} else {
2037c478bd9Sstevel@tonic-gate 		while (gdirf(fp1, dirname, Spool) == TRUE) {
2047c478bd9Sstevel@tonic-gate 			if (strpbrk(dirname, Shchar) != NULL) {
2057c478bd9Sstevel@tonic-gate 				/* skip naughty names */
2067c478bd9Sstevel@tonic-gate 				errent("BAD REMOTE NAME", dirname, 0,
2077c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__);
2087c478bd9Sstevel@tonic-gate 				continue;
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 			(void) snprintf(lockname, sizeof (lockname), "%s.%s",
2117c478bd9Sstevel@tonic-gate 			    X_LOCK, dirname);
2127c478bd9Sstevel@tonic-gate 			if (mklock(lockname) != SUCCESS)
2137c478bd9Sstevel@tonic-gate 				continue;
2147c478bd9Sstevel@tonic-gate 			xprocess(dirname);
2157c478bd9Sstevel@tonic-gate 			rmlock(CNULL);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	closedir(fp1);
2207c478bd9Sstevel@tonic-gate 	cleanup(0);
2217c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
222*462be471Sceastha 	return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate void
cleanup(code)2267c478bd9Sstevel@tonic-gate cleanup(code)
2277c478bd9Sstevel@tonic-gate int	code;
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	rmlock(CNULL);
2307c478bd9Sstevel@tonic-gate 	exit(code);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * catch signal then cleanup and exit
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate void
onintr(inter)2377c478bd9Sstevel@tonic-gate onintr(inter)
238*462be471Sceastha int	inter;
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	char	str[30];
2417c478bd9Sstevel@tonic-gate 	(void) signal(inter, SIG_IGN);
2427c478bd9Sstevel@tonic-gate 	(void) sprintf(str, "QSIGNAL %d", inter);
2437c478bd9Sstevel@tonic-gate 	logent(str, "QCAUGHT");
2447c478bd9Sstevel@tonic-gate 	acEndexe(cpucycle(), PARTIAL);	/* stop collecting accounting log */
2457c478bd9Sstevel@tonic-gate 	cleanup(-inter);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #define	XCACHESIZE (4096 / (MAXBASENAME + 1))
2497c478bd9Sstevel@tonic-gate static char	xcache[XCACHESIZE][MAXBASENAME + 1];	/* cache for X. files */
2507c478bd9Sstevel@tonic-gate static int	xcachesize = 0;			/* how many left? */
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * stash an X. file so we can process them sorted first by grade, then by
2547c478bd9Sstevel@tonic-gate  * sequence number
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate static void
xstash(file)2577c478bd9Sstevel@tonic-gate xstash(file)
2587c478bd9Sstevel@tonic-gate char	*file;
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	if (xcachesize < XCACHESIZE) {
2617c478bd9Sstevel@tonic-gate 		DEBUG(4, "stashing %s\n", file);
2627c478bd9Sstevel@tonic-gate 		(void) strlcpy(xcache[xcachesize++], file, (MAXBASENAME + 1));
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * xcompare
2687c478bd9Sstevel@tonic-gate  *	comparison routine for for qsort()
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate static int
xcompare(f1,f2)2717c478bd9Sstevel@tonic-gate xcompare(f1, f2)
272*462be471Sceastha const void	*f1, *f2;
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	/* assumes file name is X.siteG1234 */
2757c478bd9Sstevel@tonic-gate 	/* use -strcmp() so that xstash is sorted largest first */
2767c478bd9Sstevel@tonic-gate 	/* pull files out of the stash from largest index to smallest */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	return (-strcmp((char *)f1 + strlen((char *)f1) - 5,
2797c478bd9Sstevel@tonic-gate 	    (char *)f2 + strlen((char *)f2) - 5));
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * xsort
2847c478bd9Sstevel@tonic-gate  *	sort the cached X. files,
2857c478bd9Sstevel@tonic-gate  *	largest (last) to smallest (next to be processed)
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate static void
xsort()2887c478bd9Sstevel@tonic-gate xsort()
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	DEBUG(4, "xsort:  first was %s\n", xcache[0]);
2917c478bd9Sstevel@tonic-gate 	qsort(xcache, xcachesize, MAXBASENAME + 1, xcompare);
2927c478bd9Sstevel@tonic-gate 	DEBUG(4, "xsort:  first is %s\n", xcache[0]);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * xget
2977c478bd9Sstevel@tonic-gate  *	return smallest X. file in cache
2987c478bd9Sstevel@tonic-gate  *	(hint:  it's the last one in the array)
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate static int
xget(file)3017c478bd9Sstevel@tonic-gate xget(file)
3027c478bd9Sstevel@tonic-gate char	*file;
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if (xcachesize > 0) {
3057c478bd9Sstevel@tonic-gate 		strlcpy(file, xcache[--xcachesize], (MAXBASENAME + 1));
3067c478bd9Sstevel@tonic-gate 		DEBUG(4, "xget: returning %s\n", file);
3077c478bd9Sstevel@tonic-gate 		return (1);
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		/* avoid horror of xcachesize < 0 (impossible, you say?)! */
3107c478bd9Sstevel@tonic-gate 		xcachesize = 0;
3117c478bd9Sstevel@tonic-gate 		return (0);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * get a file to execute
3187c478bd9Sstevel@tonic-gate  *	file	-> a read to return filename in
3197c478bd9Sstevel@tonic-gate  * returns:
3207c478bd9Sstevel@tonic-gate  *	0	-> no file
3217c478bd9Sstevel@tonic-gate  *	1	-> file to execute
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate int
gt_Xfile(file,dir)3247c478bd9Sstevel@tonic-gate gt_Xfile(file, dir)
325*462be471Sceastha char	*file, *dir;
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	DIR *pdir;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (xcachesize == 0) {
3307c478bd9Sstevel@tonic-gate 		/* open spool directory */
3317c478bd9Sstevel@tonic-gate 		pdir = opendir(dir);
3327c478bd9Sstevel@tonic-gate 		/* this was an ASSERT, but it's not so bad as all that */
3337c478bd9Sstevel@tonic-gate 		if (pdir == NULL)
3347c478bd9Sstevel@tonic-gate 			return (0);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		/* scan spool directory looking for X. files to stash */
3377c478bd9Sstevel@tonic-gate 		while (gnamef(pdir, file) == TRUE) {
3387c478bd9Sstevel@tonic-gate 			DEBUG(4, "gt_Xfile got %s\n", file);
3397c478bd9Sstevel@tonic-gate 			/* look for x prefix */
3407c478bd9Sstevel@tonic-gate 			if (file[0] != XQTPRE)
3417c478bd9Sstevel@tonic-gate 				continue;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			/* check to see if required files have arrived */
3447c478bd9Sstevel@tonic-gate 			if (gotfiles(file))
3457c478bd9Sstevel@tonic-gate 				xstash(file);
3467c478bd9Sstevel@tonic-gate 			if (xcachesize >= XCACHESIZE)
3477c478bd9Sstevel@tonic-gate 				break;
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		closedir(pdir);
3507c478bd9Sstevel@tonic-gate 		xsort();
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	return (xget(file));
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * check for needed files
3587c478bd9Sstevel@tonic-gate  *	file	-> name of file to check
3597c478bd9Sstevel@tonic-gate  * return:
3607c478bd9Sstevel@tonic-gate  *	0	-> not ready
3617c478bd9Sstevel@tonic-gate  *	1	-> all files ready
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate int
gotfiles(file)3647c478bd9Sstevel@tonic-gate gotfiles(file)
365*462be471Sceastha char	*file;
3667c478bd9Sstevel@tonic-gate {
367*462be471Sceastha 	FILE *fp;
3687c478bd9Sstevel@tonic-gate 	struct stat stbuf;
3697c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ], rqfile[MAXNAMESIZE];
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	fp = fopen(file, "r");
3727c478bd9Sstevel@tonic-gate 	if (fp == NULL)
3737c478bd9Sstevel@tonic-gate 		return (FALSE);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
3767c478bd9Sstevel@tonic-gate 		DEBUG(4, "%s\n", buf);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * look at required files
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		if (buf[0] != X_RQDFILE)
3827c478bd9Sstevel@tonic-gate 			continue;
3837c478bd9Sstevel@tonic-gate 		(void) sscanf(&buf[1], "%63s", rqfile);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * expand file name
3877c478bd9Sstevel@tonic-gate 		 */
3887c478bd9Sstevel@tonic-gate 		expfile(rqfile);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		/*
3917c478bd9Sstevel@tonic-gate 		 * see if file exists
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if (stat(rqfile, &stbuf) == -1) {
3947c478bd9Sstevel@tonic-gate 			fclose(fp);
3957c478bd9Sstevel@tonic-gate 			return (FALSE);
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	fclose(fp);
4007c478bd9Sstevel@tonic-gate 	return (TRUE);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * remove execute files to x-directory
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * _Xfile is a global
4077c478bd9Sstevel@tonic-gate  * return:
4087c478bd9Sstevel@tonic-gate  *	none
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate void
rm_Xfiles()4117c478bd9Sstevel@tonic-gate rm_Xfiles()
4127c478bd9Sstevel@tonic-gate {
413*462be471Sceastha 	FILE *fp;
4147c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ], file[MAXNAMESIZE], tfile[MAXNAMESIZE];
4157c478bd9Sstevel@tonic-gate 	char	tfull[MAXFULLNAME];
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if ((fp = fopen(_Xfile, "r")) == NULL) {
4187c478bd9Sstevel@tonic-gate 		DEBUG(4, "rm_Xfiles: can't read %s\n", _Xfile);
4197c478bd9Sstevel@tonic-gate 		return;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * (void) unlink each file belonging to job
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
4267c478bd9Sstevel@tonic-gate 		if (buf[0] != X_RQDFILE)
4277c478bd9Sstevel@tonic-gate 			continue;
4287c478bd9Sstevel@tonic-gate 		if (sscanf(&buf[1], "%63s%63s", file, tfile) < 2)
4297c478bd9Sstevel@tonic-gate 			continue;
4307c478bd9Sstevel@tonic-gate 		(void) snprintf(tfull, sizeof (tfull), "%s/%s", XQTDIR, tfile);
4317c478bd9Sstevel@tonic-gate 		(void) unlink(tfull);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	fclose(fp);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * move execute files to x-directory
4387c478bd9Sstevel@tonic-gate  *	_Xfile is a global
4397c478bd9Sstevel@tonic-gate  * return:
4407c478bd9Sstevel@tonic-gate  *	none
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate void
mv_Xfiles()4437c478bd9Sstevel@tonic-gate mv_Xfiles()
4447c478bd9Sstevel@tonic-gate {
445*462be471Sceastha 	FILE *fp;
4467c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ], ffile[MAXFULLNAME], tfile[MAXNAMESIZE];
4477c478bd9Sstevel@tonic-gate 	char	tfull[MAXFULLNAME];
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if ((fp = fopen(_Xfile, "r")) == NULL) {
4507c478bd9Sstevel@tonic-gate 		DEBUG(4, "mv_Xfiles: can't read %s\n", _Xfile);
4517c478bd9Sstevel@tonic-gate 		return;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
4557c478bd9Sstevel@tonic-gate 		if (buf[0] != X_RQDFILE)
4567c478bd9Sstevel@tonic-gate 			continue;
4577c478bd9Sstevel@tonic-gate 		if (sscanf(&buf[1], "%63s%63s", ffile, tfile) < 2)
4587c478bd9Sstevel@tonic-gate 			continue;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * expand file names and move to
4627c478bd9Sstevel@tonic-gate 		 * execute directory
4637c478bd9Sstevel@tonic-gate 		 * Make files readable by anyone
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		expfile(ffile);
4667c478bd9Sstevel@tonic-gate 		(void) snprintf(tfull, sizeof (tfull), "%s/%s", XQTDIR, tfile);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		if (chkpth(ffile, CK_READ) == FAIL)
4697c478bd9Sstevel@tonic-gate 			continue;	/* execution will fail later */
4707c478bd9Sstevel@tonic-gate 		if (chkpth(tfull, CK_WRITE) == FAIL) {
4717c478bd9Sstevel@tonic-gate 			/*
4727c478bd9Sstevel@tonic-gate 			 * tfull will have been canonicalized. If
4737c478bd9Sstevel@tonic-gate 			 * it still points to XQTDIR, allow us to
4747c478bd9Sstevel@tonic-gate 			 * write there.
4757c478bd9Sstevel@tonic-gate 			 */
4767c478bd9Sstevel@tonic-gate 			if (!PREFIX(XQTDIR, tfull))
4777c478bd9Sstevel@tonic-gate 				continue;	/* execution will fail later */
4787c478bd9Sstevel@tonic-gate 			/* otherwise, keep going */
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		ASSERT(xmv(ffile, tfull) == 0, "XMV ERROR", tfull, errno);
4827c478bd9Sstevel@tonic-gate 		chmod(tfull, PUB_FILEMODE);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	fclose(fp);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * undo what mv_Xfiles did
4897c478bd9Sstevel@tonic-gate  *	_Xfile is a global
4907c478bd9Sstevel@tonic-gate  * return:
4917c478bd9Sstevel@tonic-gate  *	none
4927c478bd9Sstevel@tonic-gate  */
4937c478bd9Sstevel@tonic-gate void
unmv_Xfiles()4947c478bd9Sstevel@tonic-gate unmv_Xfiles()
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	FILE *fp;
4977c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ], ffile[MAXNAMESIZE], tfile[MAXNAMESIZE];
4987c478bd9Sstevel@tonic-gate 	char	tfull[MAXFULLNAME], ffull[MAXFULLNAME], xfull[MAXFULLNAME];
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	(void) snprintf(xfull, MAXFULLNAME, "%s/%s", RemSpool, _Xfile);
5017c478bd9Sstevel@tonic-gate 	if ((fp = fopen(xfull, "r")) == NULL) {
5027c478bd9Sstevel@tonic-gate 		DEBUG(4, "unmv_Xfiles: can't read %s\n", xfull);
5037c478bd9Sstevel@tonic-gate 		return;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
5077c478bd9Sstevel@tonic-gate 		if (buf[0] != X_RQDFILE)
5087c478bd9Sstevel@tonic-gate 			continue;
5097c478bd9Sstevel@tonic-gate 		if (sscanf(&buf[1], "%63s%63s", ffile, tfile) < 2)
5107c478bd9Sstevel@tonic-gate 			continue;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		/*
5137c478bd9Sstevel@tonic-gate 		 * expand file names and move back to
5147c478bd9Sstevel@tonic-gate 		 * spool directory
5157c478bd9Sstevel@tonic-gate 		 * Make files readable by uucp
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate 		(void) snprintf(ffull, MAXFULLNAME, "%s/%s", RemSpool, ffile);
5187c478bd9Sstevel@tonic-gate 		/* i know we're in .Xqtdir, but ... */
5197c478bd9Sstevel@tonic-gate 		(void) snprintf(tfull, MAXFULLNAME, "%s/%s", XQTDIR, tfile);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		if (chkpth(ffull, CK_WRITE) == FAIL ||
5227c478bd9Sstevel@tonic-gate 		    chkpth(tfull, CK_READ) == FAIL)
5237c478bd9Sstevel@tonic-gate 			continue;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		ASSERT(xmv(tfull, ffull) == 0, "XMV ERROR", ffull, errno);
5267c478bd9Sstevel@tonic-gate 		(void) chmod(ffull, (mode_t)0600);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	fclose(fp);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * chkpart - checks the string (ptr points to it) for illegal command or
5337c478bd9Sstevel@tonic-gate  *  file permission restriction - called recursively
5347c478bd9Sstevel@tonic-gate  *  to check lines that have `string` or (string) form.
5357c478bd9Sstevel@tonic-gate  *  _Cmd is the buffer where the command is built up.
5367c478bd9Sstevel@tonic-gate  *  _CargType is the type of the next C line argument
5377c478bd9Sstevel@tonic-gate  *
5387c478bd9Sstevel@tonic-gate  * Return:
5397c478bd9Sstevel@tonic-gate  *	BAD_FILE if a non permitted file is found
5407c478bd9Sstevel@tonic-gate  *	BAD_COMMAND if non permitted command is found
5417c478bd9Sstevel@tonic-gate  *	0 - ok
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate static int
chkpart(char * ptr)5457c478bd9Sstevel@tonic-gate chkpart(char *ptr)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	char	prm[BUFSIZ], whitesp[BUFSIZ], rqtcmd[BUFSIZ], xcmd[BUFSIZ];
5487c478bd9Sstevel@tonic-gate 	char	savechar[2]; /* one character string with NULL */
5497c478bd9Sstevel@tonic-gate 	int	ret;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	/* _CargType is the arg type for this iteration (cmd or file) */
5527c478bd9Sstevel@tonic-gate 	while ((ptr = getprm(ptr, whitesp, prm)) != NULL) {
5537c478bd9Sstevel@tonic-gate 	    DEBUG(4, "prm='%s'\n", prm);
5547c478bd9Sstevel@tonic-gate 	    switch (*prm) {
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	    /* End of command delimiter */
5577c478bd9Sstevel@tonic-gate 	    case ';':
5587c478bd9Sstevel@tonic-gate 	    case '^':
5597c478bd9Sstevel@tonic-gate 	    case '&':
5607c478bd9Sstevel@tonic-gate 	    case '|':
5617c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
5627c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, prm, sizeof (_Cmd));
5637c478bd9Sstevel@tonic-gate 		_CargType = C_COMMAND;
5647c478bd9Sstevel@tonic-gate 		continue;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	    /* Other delimiter */
5677c478bd9Sstevel@tonic-gate 	    case '>':
5687c478bd9Sstevel@tonic-gate 	    case '<':
5697c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
5707c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, prm, sizeof (_Cmd));
5717c478bd9Sstevel@tonic-gate 		continue;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	    case '`':	/* don't allow any ` commands */
5747c478bd9Sstevel@tonic-gate 	    case '\\':
5757c478bd9Sstevel@tonic-gate 		return (BAD_COMMAND);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	    /* Some allowable quoted string */
5787c478bd9Sstevel@tonic-gate 	    case '(':
5797c478bd9Sstevel@tonic-gate 	    case '"':
5807c478bd9Sstevel@tonic-gate 	    case '\'':
5817c478bd9Sstevel@tonic-gate 		/* must recurse */
5827c478bd9Sstevel@tonic-gate 		savechar[0] = *prm;
5837c478bd9Sstevel@tonic-gate 		savechar[1] = NULLCHAR;
5847c478bd9Sstevel@tonic-gate 		/* put leading white space & first char into command */
5857c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
5867c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, savechar, sizeof (_Cmd));
5877c478bd9Sstevel@tonic-gate 		savechar[0] = prm[strlen(prm)-1];
5887c478bd9Sstevel@tonic-gate 		prm[strlen(prm)-1] = NULLCHAR; /* delete last character */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		/* recurse */
5917c478bd9Sstevel@tonic-gate 		if (ret = chkpart(prm+1)) { /* failed */
5927c478bd9Sstevel@tonic-gate 			return (ret);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		/* put last char into command */
5957c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, savechar, sizeof (_Cmd));
5967c478bd9Sstevel@tonic-gate 		continue;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	    case '2':
5997c478bd9Sstevel@tonic-gate 		if (*(prm+1) == '>') {
6007c478bd9Sstevel@tonic-gate 		    (void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
6017c478bd9Sstevel@tonic-gate 		    (void) strlcat(_Cmd, prm, sizeof (_Cmd));
6027c478bd9Sstevel@tonic-gate 		    continue;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		/* fall through if not "2>" */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	    default:	/* check for command or file */
6077c478bd9Sstevel@tonic-gate 		break;
6087c478bd9Sstevel@tonic-gate 	    }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	    if (_CargType == C_COMMAND) {
6117c478bd9Sstevel@tonic-gate 		(void) strlcpy(rqtcmd, prm, sizeof (rqtcmd));
6127c478bd9Sstevel@tonic-gate 		if (*rqtcmd == '~')
6137c478bd9Sstevel@tonic-gate 		    expfile(rqtcmd);
6147c478bd9Sstevel@tonic-gate 		if ((cmdOK(rqtcmd, xcmd)) == FALSE)
6157c478bd9Sstevel@tonic-gate 			return (BAD_COMMAND);
6167c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
6177c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, xcmd, sizeof (_Cmd));
6187c478bd9Sstevel@tonic-gate 		_CargType = C_FILE;
6197c478bd9Sstevel@tonic-gate 		continue;
6207c478bd9Sstevel@tonic-gate 	    }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	    (void) strlcpy(rqtcmd, prm, sizeof (rqtcmd));
6237c478bd9Sstevel@tonic-gate 	    if (*rqtcmd == '~')
6247c478bd9Sstevel@tonic-gate 		expfile(rqtcmd);
6257c478bd9Sstevel@tonic-gate 	    if (chkFile(rqtcmd)) {
6267c478bd9Sstevel@tonic-gate 		return (BAD_FILE);
6277c478bd9Sstevel@tonic-gate 	    } else {
6287c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
6297c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, rqtcmd, sizeof (_Cmd));
6307c478bd9Sstevel@tonic-gate 	    }
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	if (whitesp[0] != '\0')
6337c478bd9Sstevel@tonic-gate 		/* restore any trailing white space */
6347c478bd9Sstevel@tonic-gate 		(void) strlcat(_Cmd, whitesp, sizeof (_Cmd));
6357c478bd9Sstevel@tonic-gate 	return (0);	/* all ok */
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * chkFile - try to find a path name in the prm.
6407c478bd9Sstevel@tonic-gate  * 	if found, check it for access permission.
6417c478bd9Sstevel@tonic-gate  *
6427c478bd9Sstevel@tonic-gate  * check file access permissions
6437c478bd9Sstevel@tonic-gate  * if ! in name assume that access on local machine is required
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * Return:
6467c478bd9Sstevel@tonic-gate  *	BAD_FILE - not permitted
6477c478bd9Sstevel@tonic-gate  *	0 - ok
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate static int
chkFile(char * prm)6517c478bd9Sstevel@tonic-gate chkFile(char *prm)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	char	*p, buf[BUFSIZ];
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	(void) strlcpy(buf, prm, sizeof (buf));
6567c478bd9Sstevel@tonic-gate 	switch (*prm) {
6577c478bd9Sstevel@tonic-gate 	case '~':
6587c478bd9Sstevel@tonic-gate 	case '/':
6597c478bd9Sstevel@tonic-gate 	    if (doFileChk(buf))
6607c478bd9Sstevel@tonic-gate 		return (BAD_FILE);
6617c478bd9Sstevel@tonic-gate 	    else
6627c478bd9Sstevel@tonic-gate 		return (0);
6637c478bd9Sstevel@tonic-gate 	    /*NOTREACHED*/
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	case '!':
6667c478bd9Sstevel@tonic-gate 	    return (chkFile(buf+1));
6677c478bd9Sstevel@tonic-gate 	    /*NOTREACHED*/
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	default:
6707c478bd9Sstevel@tonic-gate 	    break;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((p = strchr(buf, '!')) == NULL) {  /* no "!", look for "/" */
6747c478bd9Sstevel@tonic-gate 	    if ((p = strchr(buf, '/')) == NULL) {  /* ok */
6757c478bd9Sstevel@tonic-gate 		return (0);
6767c478bd9Sstevel@tonic-gate 	    }
6777c478bd9Sstevel@tonic-gate 	    if (doFileChk(p))
6787c478bd9Sstevel@tonic-gate 		return (BAD_FILE);
6797c478bd9Sstevel@tonic-gate 	    else
6807c478bd9Sstevel@tonic-gate 		return (0);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* there is at least one '!' - see if it refers to my system */
6847c478bd9Sstevel@tonic-gate 	if (PREFIX(Myname, buf))	/*  my system so far, check further */
6857c478bd9Sstevel@tonic-gate 	    return (chkFile(p+1));	/* recurse with thing after '!' */
6867c478bd9Sstevel@tonic-gate 	else				/* not my system - not my worry */
6877c478bd9Sstevel@tonic-gate 	    return (0);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * doFileChk - check file path permission
6927c478bd9Sstevel@tonic-gate  * NOTE: file is assumed to be a buffer that expfile an
6937c478bd9Sstevel@tonic-gate  *  write into.
6947c478bd9Sstevel@tonic-gate  * Return
6957c478bd9Sstevel@tonic-gate  *	BAD_FILE - not allowed
6967c478bd9Sstevel@tonic-gate  *	0 - ok
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate static int
doFileChk(char * file)7007c478bd9Sstevel@tonic-gate doFileChk(char *file)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	expfile(file);
7037c478bd9Sstevel@tonic-gate 	DEBUG(7, "fullname: %s\n", file);
7047c478bd9Sstevel@tonic-gate 	if (chkpth(file, CK_READ) == FAIL ||
7057c478bd9Sstevel@tonic-gate 	    chkpth(file, CK_WRITE) == FAIL)
7067c478bd9Sstevel@tonic-gate 		return (BAD_FILE);
7077c478bd9Sstevel@tonic-gate 	else
7087c478bd9Sstevel@tonic-gate 		return (0);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * return stuff to user
7147c478bd9Sstevel@tonic-gate  *	user	-> user to notify
7157c478bd9Sstevel@tonic-gate  *	rmt	-> system name where user resides
7167c478bd9Sstevel@tonic-gate  *	file	-> file to return (generally contains input)
7177c478bd9Sstevel@tonic-gate  *	cmd	-> command that was to be executed
7187c478bd9Sstevel@tonic-gate  *	buf	-> user friendly face saving uplifting edifying missive
7197c478bd9Sstevel@tonic-gate  *	errfile	-> stderr output from cmd xeqn
7207c478bd9Sstevel@tonic-gate  * return:
7217c478bd9Sstevel@tonic-gate  *	none
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate static void
retosndr(user,rmt,file,cmd,buf,errfile)7247c478bd9Sstevel@tonic-gate retosndr(user, rmt, file, cmd, buf, errfile)
7257c478bd9Sstevel@tonic-gate char	*user, *rmt, *file, *cmd, *buf, *errfile;
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	char	ruser[BUFSIZ], msg[BUFSIZ], subj[BUFSIZ];
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	(void) snprintf(msg, sizeof (msg), "%s\t[%s %s (%s)]\n\t%s\n%s\n",
7307c478bd9Sstevel@tonic-gate 	    gettext("remote execution"), gettext("uucp job"),
7317c478bd9Sstevel@tonic-gate 	    *Jobid ? Jobid : &_Xfile[2], timeStamp(), cmd, buf);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	DEBUG(5, "retosndr %s, ", msg);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (EQUALS(rmt, Myname))
7367c478bd9Sstevel@tonic-gate 		(void) strlcpy(ruser, user, sizeof (ruser));
7377c478bd9Sstevel@tonic-gate 	else
7387c478bd9Sstevel@tonic-gate 		(void) snprintf(ruser, sizeof (ruser), "%s!%s", rmt, user);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	(void) strlcpy(subj, gettext("remote execution status"), sizeof (subj));
7417c478bd9Sstevel@tonic-gate 	mailst(ruser, subj, msg, file, errfile);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * uucpst - send the status message back using a uucp command
7477c478bd9Sstevel@tonic-gate  * NOTE - this would be better if the file could be appended.
7487c478bd9Sstevel@tonic-gate  * - suggestion for the future - if rmail would take a file name
7497c478bd9Sstevel@tonic-gate  * instead of just person, then that facility would be correct,
7507c478bd9Sstevel@tonic-gate  * and this routine would not be needed.
7517c478bd9Sstevel@tonic-gate  */
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static void
uucpst(rmt,tofile,errfile,cmd,buf)7547c478bd9Sstevel@tonic-gate uucpst(rmt, tofile, errfile, cmd, buf)
7557c478bd9Sstevel@tonic-gate char	*rmt, *tofile, *errfile, *cmd, *buf;
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	char	arg[MAXFULLNAME], tmp[NAMESIZE], msg[BUFSIZ];
7587c478bd9Sstevel@tonic-gate 	pid_t pid, ret;
7597c478bd9Sstevel@tonic-gate 	int status;
7607c478bd9Sstevel@tonic-gate 	FILE *fp, *fi;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	(void) snprintf(msg, sizeof (msg), "%s %s (%s) %s\n\t%s\n%s\n",
7637c478bd9Sstevel@tonic-gate 	    gettext("uucp job"), *Jobid ? Jobid : &_Xfile[2], timeStamp(),
7647c478bd9Sstevel@tonic-gate 	    gettext("remote execution"), cmd, buf);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	(void) snprintf(tmp, sizeof (tmp), "%s.%ld", rmt, (long)getpid());
7677c478bd9Sstevel@tonic-gate 	if ((fp = fopen(tmp, "w")) == NULL)
7687c478bd9Sstevel@tonic-gate 		return;
7697c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s\n", msg);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/* copy back stderr */
7727c478bd9Sstevel@tonic-gate 	if (*errfile != '\0' && NOTEMPTY(errfile) &&
7737c478bd9Sstevel@tonic-gate 	    (fi = fopen(errfile, "r")) != NULL) {
7747c478bd9Sstevel@tonic-gate 		fputs("\n\t===== stderr was =====\n", fp);
7757c478bd9Sstevel@tonic-gate 		if (xfappend(fi, fp) != SUCCESS)
7767c478bd9Sstevel@tonic-gate 			fputs("\n\t===== well, i tried =====\n", fp);
7777c478bd9Sstevel@tonic-gate 		(void) fclose(fi);
7787c478bd9Sstevel@tonic-gate 		fputc('\n', fp);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7837c478bd9Sstevel@tonic-gate 	(void) snprintf(arg, sizeof (arg), "%s!%s", rmt, tofile);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	/* start uucp */
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
7887c478bd9Sstevel@tonic-gate 		(void) close(0);
7897c478bd9Sstevel@tonic-gate 		(void) close(1);
7907c478bd9Sstevel@tonic-gate 		(void) close(2);
7917c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", 2);
7927c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", 2);
7937c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", 2);
7947c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
7957c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
7967c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_IGN);
7977c478bd9Sstevel@tonic-gate 		ucloselog();
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		(void) execle("/usr/bin/uucp", "UUCP",
8007c478bd9Sstevel@tonic-gate 		    "-C", tmp, arg, (char *)0, Env);
8017c478bd9Sstevel@tonic-gate 		_exit(100);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (pid == -1)
8057c478bd9Sstevel@tonic-gate 	    return;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	while ((ret = wait(&status)) != pid)
8087c478bd9Sstevel@tonic-gate 	    if (ret == -1 && errno != EINTR)
8097c478bd9Sstevel@tonic-gate 		break;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	(void) unlink(tmp);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate void
xprocess(dirname)8157c478bd9Sstevel@tonic-gate xprocess(dirname)
8167c478bd9Sstevel@tonic-gate char *dirname;
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate     char 	fdgrade();	/* returns default service grade on system */
8197c478bd9Sstevel@tonic-gate     int		return_stdin;	/* return stdin for failed commands */
8207c478bd9Sstevel@tonic-gate     int		cmdok, ret, badfiles;
8217c478bd9Sstevel@tonic-gate     mode_t	mask;
8227c478bd9Sstevel@tonic-gate     int		send_zero;	/* return successful completion status */
8237c478bd9Sstevel@tonic-gate     int		send_nonzero;	/* return unsuccessful completion status */
8247c478bd9Sstevel@tonic-gate     int		send_nothing;	/* request for no exit status */
8257c478bd9Sstevel@tonic-gate     int		store_status;	/* store status of command in local file */
8267c478bd9Sstevel@tonic-gate     char	lbuf[BUFSIZ];
8277c478bd9Sstevel@tonic-gate     char	dqueue;		/* var to hold the default service grade */
8287c478bd9Sstevel@tonic-gate     char	*errname = "";	/* name of local stderr output file */
8297c478bd9Sstevel@tonic-gate     char	*p;
8307c478bd9Sstevel@tonic-gate     char	sendsys[MAXNAMESIZE];
8317c478bd9Sstevel@tonic-gate     char	dfile[MAXFULLNAME], cfile[MAXFULLNAME], incmd[BUFSIZ];
8327c478bd9Sstevel@tonic-gate     char	errDfile[BUFSIZ];
8337c478bd9Sstevel@tonic-gate     char	fin[MAXFULLNAME];
8347c478bd9Sstevel@tonic-gate     char	fout[MAXFULLNAME], sysout[NAMESIZE];
8357c478bd9Sstevel@tonic-gate     char	ferr[MAXFULLNAME], syserr[NAMESIZE];
8367c478bd9Sstevel@tonic-gate     char	file[MAXFULLNAME], tempname[NAMESIZE];
8377c478bd9Sstevel@tonic-gate     char	_Sfile[MAXFULLNAME];	/* name of local file for status */
8387c478bd9Sstevel@tonic-gate     FILE	*xfp, *fp;
8397c478bd9Sstevel@tonic-gate     struct	stat sb;
8407c478bd9Sstevel@tonic-gate     char	buf[BUFSIZ], user[BUFSIZ], retaddr[BUFSIZ], retuser[BUFSIZ],
8417c478bd9Sstevel@tonic-gate 		msgbuf[BUFSIZ];
8427c478bd9Sstevel@tonic-gate     char	origsys[MAXFULLNAME], origuser[MAXFULLNAME];
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate     (void) strlcpy(Rmtname, dirname, sizeof (Rmtname));
8457c478bd9Sstevel@tonic-gate     chremdir(Rmtname);
8467c478bd9Sstevel@tonic-gate     (void) mchFind(Rmtname);
8477c478bd9Sstevel@tonic-gate     while (gt_Xfile(_Xfile, RemSpool) > 0) {
8487c478bd9Sstevel@tonic-gate 	DEBUG(4, "_Xfile - %s\n", _Xfile);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if ((xfp = fopen(_Xfile, "r")) == NULL) {
8517c478bd9Sstevel@tonic-gate 	    toCorrupt(_Xfile);
8527c478bd9Sstevel@tonic-gate 	    continue;
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	ASSERT(xfp != NULL, Ct_OPEN, _Xfile, errno);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if (stat(_Xfile, &sb) != -1)
8577c478bd9Sstevel@tonic-gate 	    Nstat.t_qtime = sb.st_mtime;
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * initialize to defaults
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	(void) strlcpy(user, User, sizeof (user));
8627c478bd9Sstevel@tonic-gate 	(void) strcpy(fin, "/dev/null");
8637c478bd9Sstevel@tonic-gate 	(void) strcpy(fout, "/dev/null");
8647c478bd9Sstevel@tonic-gate 	(void) strcpy(ferr, "/dev/null");
8657c478bd9Sstevel@tonic-gate 	(void) sprintf(sysout, "%.*s", MAXBASENAME, Myname);
8667c478bd9Sstevel@tonic-gate 	(void) sprintf(syserr, "%.*s", MAXBASENAME, Myname);
8677c478bd9Sstevel@tonic-gate 	badfiles = 0;
8687c478bd9Sstevel@tonic-gate 	*incmd = *retaddr = *retuser = *Jobid = NULLCHAR;
8697c478bd9Sstevel@tonic-gate 	initSeq();
8707c478bd9Sstevel@tonic-gate 	send_zero = send_nonzero = send_nothing = 0;
8717c478bd9Sstevel@tonic-gate 	store_status = 0;
8727c478bd9Sstevel@tonic-gate 	return_stdin = 0;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, xfp) != NULL) {
8757c478bd9Sstevel@tonic-gate 	    /*
8767c478bd9Sstevel@tonic-gate 	     * interpret JCL card
8777c478bd9Sstevel@tonic-gate 	     */
8787c478bd9Sstevel@tonic-gate 	    switch (buf[0]) {
8797c478bd9Sstevel@tonic-gate 		case X_USER:
8807c478bd9Sstevel@tonic-gate 			/*
8817c478bd9Sstevel@tonic-gate 			 * user name
8827c478bd9Sstevel@tonic-gate 			 * (ignore Rmtname)
8837c478bd9Sstevel@tonic-gate 			 * The utmpx username field is 32 characters long;
8847c478bd9Sstevel@tonic-gate 			 * UUCP usage truncates system name to 14 bytes.
8857c478bd9Sstevel@tonic-gate 			 */
8867c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%32s%14s", user, origsys);
8877c478bd9Sstevel@tonic-gate 			(void) strlcpy(origuser, user, sizeof (origuser));
8887c478bd9Sstevel@tonic-gate 			break;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		case X_STDIN:
8917c478bd9Sstevel@tonic-gate 			/*
8927c478bd9Sstevel@tonic-gate 			 * standard input
8937c478bd9Sstevel@tonic-gate 			 */
8947c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%256s", fin);
8957c478bd9Sstevel@tonic-gate 			expfile(fin);
8967c478bd9Sstevel@tonic-gate 			if (chkpth(fin, CK_READ)) {
8977c478bd9Sstevel@tonic-gate 				DEBUG(4, "badfile - in: %s\n", fin);
8987c478bd9Sstevel@tonic-gate 				badfiles = 1;
8997c478bd9Sstevel@tonic-gate 			}
9007c478bd9Sstevel@tonic-gate 			break;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		case X_STDOUT:
9037c478bd9Sstevel@tonic-gate 			/*
9047c478bd9Sstevel@tonic-gate 			 * standard output
9057c478bd9Sstevel@tonic-gate 			 */
9067c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%256s%14s", fout, sysout);
9077c478bd9Sstevel@tonic-gate 			if ((p = strpbrk(sysout, "!/")) != NULL)
9087c478bd9Sstevel@tonic-gate 				*p = NULLCHAR;	/* these are dangerous */
9097c478bd9Sstevel@tonic-gate 			if (*sysout != NULLCHAR && !EQUALS(sysout, Myname))
9107c478bd9Sstevel@tonic-gate 				break;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			expfile(fout);
9137c478bd9Sstevel@tonic-gate 			if (chkpth(fout, CK_WRITE)) {
9147c478bd9Sstevel@tonic-gate 				badfiles = 1;
9157c478bd9Sstevel@tonic-gate 				DEBUG(4, "badfile - out: %s\n", fout);
9167c478bd9Sstevel@tonic-gate 			}
9177c478bd9Sstevel@tonic-gate 			break;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		case X_STDERR:	/* standard error */
9207c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%256s%14s", ferr, syserr);
9217c478bd9Sstevel@tonic-gate 			if ((p = strpbrk(syserr, "!/")) != NULL)
9227c478bd9Sstevel@tonic-gate 				*p = NULLCHAR;	/* these are dangerous */
9237c478bd9Sstevel@tonic-gate 			if (*syserr != NULLCHAR && !EQUALS(syserr, Myname))
9247c478bd9Sstevel@tonic-gate 				break;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 			expfile(ferr);
9277c478bd9Sstevel@tonic-gate 			if (chkpth(ferr, CK_WRITE)) {
9287c478bd9Sstevel@tonic-gate 				badfiles = 1;
9297c478bd9Sstevel@tonic-gate 				DEBUG(4, "badfile - error: %s\n", ferr);
9307c478bd9Sstevel@tonic-gate 			}
9317c478bd9Sstevel@tonic-gate 			break;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		case X_CMD:	/* command to execute */
9357c478bd9Sstevel@tonic-gate 			(void) strlcpy(incmd, &buf[2], sizeof (incmd));
9367c478bd9Sstevel@tonic-gate 			if (*(incmd + strlen(incmd) - 1) == '\n')
9377c478bd9Sstevel@tonic-gate 				*(incmd + strlen(incmd) - 1) = NULLCHAR;
9387c478bd9Sstevel@tonic-gate 			break;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		case X_MAILF:	/* put status in _Sfile */
9417c478bd9Sstevel@tonic-gate 			store_status = 1;
9427c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%256s", _Sfile);
9437c478bd9Sstevel@tonic-gate 			break;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		case X_SENDNOTHING:	/* no failure notification */
9467c478bd9Sstevel@tonic-gate 			send_nothing++;
9477c478bd9Sstevel@tonic-gate 			break;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		case X_SENDZERO:	/* success notification */
9507c478bd9Sstevel@tonic-gate 			send_zero++;
9517c478bd9Sstevel@tonic-gate 			break;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		case X_NONZERO:	/* failure notification */
9547c478bd9Sstevel@tonic-gate 			send_nonzero++;
9557c478bd9Sstevel@tonic-gate 			break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		case X_BRINGBACK:  /* return stdin on command failure */
9587c478bd9Sstevel@tonic-gate 			return_stdin = 1;
9597c478bd9Sstevel@tonic-gate 			break;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		case X_RETADDR:
9637c478bd9Sstevel@tonic-gate 			/*
9647c478bd9Sstevel@tonic-gate 			 * return address -- is user's name
9657c478bd9Sstevel@tonic-gate 			 * put "Rmtname!" in front of it so mail
9667c478bd9Sstevel@tonic-gate 			 * will always get back to remote system.
9677c478bd9Sstevel@tonic-gate 			 */
9687c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%s", retuser);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 			/*
9717c478bd9Sstevel@tonic-gate 			 * Creates string of Rmtname!Rmtname!user which
9727c478bd9Sstevel@tonic-gate 			 * confuses rmail.
9737c478bd9Sstevel@tonic-gate 			 * (void) strcat(strcat(strcpy(retaddr, Rmtname), "!"),
9747c478bd9Sstevel@tonic-gate 			 *	retuser);
9757c478bd9Sstevel@tonic-gate 			 */
9767c478bd9Sstevel@tonic-gate 			break;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 		case X_JOBID:
9797c478bd9Sstevel@tonic-gate 			/*
9807c478bd9Sstevel@tonic-gate 			 * job id for notification
9817c478bd9Sstevel@tonic-gate 			 * (should be MAXBASENAME, not 14, but no can do)
9827c478bd9Sstevel@tonic-gate 			 */
9837c478bd9Sstevel@tonic-gate 			(void) sscanf(&buf[1], "%14s", Jobid);
9847c478bd9Sstevel@tonic-gate 			break;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		default:
9877c478bd9Sstevel@tonic-gate 			break;
9887c478bd9Sstevel@tonic-gate 	    }
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	fclose(xfp);
9927c478bd9Sstevel@tonic-gate 	DEBUG(4, "fin - %s, ", fin);
9937c478bd9Sstevel@tonic-gate 	DEBUG(4, "fout - %s, ", fout);
9947c478bd9Sstevel@tonic-gate 	DEBUG(4, "ferr - %s, ", ferr);
9957c478bd9Sstevel@tonic-gate 	DEBUG(4, "sysout - %s, ", sysout);
9967c478bd9Sstevel@tonic-gate 	DEBUG(4, "syserr - %s, ", syserr);
9977c478bd9Sstevel@tonic-gate 	DEBUG(4, "user - %s\n", user);
9987c478bd9Sstevel@tonic-gate 	DEBUG(4, "incmd - %s\n", incmd);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	scRexe(origsys, origuser, Loginuser, incmd);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (retuser[0] != NULLCHAR)
10037c478bd9Sstevel@tonic-gate 	    (void) strlcpy(user, retuser, sizeof (user)); /* pick on this guy */
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/* get rid of stuff that can be dangerous */
10067c478bd9Sstevel@tonic-gate 	if ((p = strpbrk(user, Shchar)) != NULL) {
10077c478bd9Sstevel@tonic-gate 	    *p = NULLCHAR;
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (incmd[0] == NULLCHAR) {
10117c478bd9Sstevel@tonic-gate 	    /* this is a bad X. file - just get rid of it */
10127c478bd9Sstevel@tonic-gate 	    toCorrupt(_Xfile);
10137c478bd9Sstevel@tonic-gate 	    continue;
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * send_nothing must be explicitly requested to avert failure status
10187c478bd9Sstevel@tonic-gate 	 * send_zero must be explicitly requested for success notification
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	if (!send_nothing)
10217c478bd9Sstevel@tonic-gate 		send_nonzero++;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * command execution
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * generate a temporary file (if necessary)
10297c478bd9Sstevel@tonic-gate 	 * to hold output to be shipped back
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	if (EQUALS(fout, "/dev/null"))
10327c478bd9Sstevel@tonic-gate 	    (void) strcpy(dfile, "/dev/null");
10337c478bd9Sstevel@tonic-gate 	else {
10347c478bd9Sstevel@tonic-gate 	    gename(DATAPRE, sysout, 'O', tempname);
10357c478bd9Sstevel@tonic-gate 	    (void) snprintf(dfile, sizeof (dfile), "%s/%s", WORKSPACE,
10367c478bd9Sstevel@tonic-gate 		tempname);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * generate a temporary file (if necessary)
10417c478bd9Sstevel@tonic-gate 	 * to hold errors to be shipped back
10427c478bd9Sstevel@tonic-gate 	 */
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * This is what really should be done. However for compatibility
10457c478bd9Sstevel@tonic-gate  * for the interim at least, we will always create temp file
10467c478bd9Sstevel@tonic-gate  * so we can return error output. If this temp file IS conditionally
10477c478bd9Sstevel@tonic-gate  * created, we must remove the unlink() of errDfile at the end
10487c478bd9Sstevel@tonic-gate  * because it may REALLY be /dev/null.
10497c478bd9Sstevel@tonic-gate  *	if (EQUALS(ferr, "/dev/null"))
10507c478bd9Sstevel@tonic-gate  *	    (void) strcpy(errDfile, "/dev/null");
10517c478bd9Sstevel@tonic-gate  *	else {
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate 	    gename(DATAPRE, syserr, 'E', tempname);
10547c478bd9Sstevel@tonic-gate 	    (void) snprintf(errDfile, sizeof (errDfile), "%s/%s",
10557c478bd9Sstevel@tonic-gate 		WORKSPACE, tempname);
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  *	}
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* initialize command line */
10617c478bd9Sstevel@tonic-gate 	/* set up two environment variables, remote machine name */
10627c478bd9Sstevel@tonic-gate 	/* and remote user name if available from R line */
10637c478bd9Sstevel@tonic-gate 	/*
10647c478bd9Sstevel@tonic-gate 	 * xcu4 requires that uucp *does* expand wildcards and uux *does not*
10657c478bd9Sstevel@tonic-gate 	 * expand wild cards... Further restrictions are that uux must work
10667c478bd9Sstevel@tonic-gate 	 * with every other uucp / uux that initiated a request, so nothing
10677c478bd9Sstevel@tonic-gate 	 * strange can been done to communicate that it was uucp that sent
10687c478bd9Sstevel@tonic-gate 	 * the request and not uux,  What we settle on here is looking for
10697c478bd9Sstevel@tonic-gate 	 * the command name uucp and expanding wildcards in only that case.
10707c478bd9Sstevel@tonic-gate 	 * It is true that a user can spoof this using uux, but in reality
10717c478bd9Sstevel@tonic-gate 	 * this would be identical to using the uucp command to start with.
10727c478bd9Sstevel@tonic-gate 	 */
10737c478bd9Sstevel@tonic-gate 	if (strncmp(incmd, "uucp ", 5) == 0) {
10747c478bd9Sstevel@tonic-gate 		(void) snprintf(_Cmd, sizeof (_Cmd),
10757c478bd9Sstevel@tonic-gate 		    "%s %s UU_MACHINE=%s UU_USER=%s "
10767c478bd9Sstevel@tonic-gate 		    " export UU_MACHINE UU_USER PATH; ",
10777c478bd9Sstevel@tonic-gate 		    PATH, LOGNAME, Rmtname, user);
10787c478bd9Sstevel@tonic-gate 	} else {
10797c478bd9Sstevel@tonic-gate 		(void) snprintf(_Cmd, sizeof (_Cmd),
10807c478bd9Sstevel@tonic-gate 		    "%s %s UU_MACHINE=%s UU_USER=%s "
10817c478bd9Sstevel@tonic-gate 		    " export UU_MACHINE UU_USER PATH; set -f; ",
10827c478bd9Sstevel@tonic-gate 		    PATH, LOGNAME, Rmtname, user);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 * check to see if command can be executed
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	_CargType = C_COMMAND;	/* the first thing is a command */
10897c478bd9Sstevel@tonic-gate 	cmdok = chkpart(incmd);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	if (badfiles || (cmdok == BAD_COMMAND) || cmdok == BAD_FILE) {
10927c478bd9Sstevel@tonic-gate 	    if (cmdok == BAD_COMMAND) {
10937c478bd9Sstevel@tonic-gate 		(void) snprintf(lbuf, sizeof (lbuf), "%s!%s XQT DENIED",
10947c478bd9Sstevel@tonic-gate 		    Rmtname, user);
10957c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
10967c478bd9Sstevel@tonic-gate 		    "execution permission denied to %s!%s", Rmtname, user);
10977c478bd9Sstevel@tonic-gate 	    } else {
10987c478bd9Sstevel@tonic-gate 		(void) snprintf(lbuf, sizeof (lbuf),
10997c478bd9Sstevel@tonic-gate 		    "%s!%s XQT - STDIN/STDOUT/FILE ACCESS DENIED",
11007c478bd9Sstevel@tonic-gate 		    Rmtname, user);
11017c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11027c478bd9Sstevel@tonic-gate 		    "file access denied to %s!%s", Rmtname, user);
11037c478bd9Sstevel@tonic-gate 	    }
11047c478bd9Sstevel@tonic-gate 	    logent(incmd, lbuf);
11057c478bd9Sstevel@tonic-gate 	    DEBUG(4, "bad command %s\n", incmd);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	    scWlog(); /* log security vialotion */
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	    if (send_nonzero)
11107c478bd9Sstevel@tonic-gate 		retosndr(user, Rmtname, return_stdin ? fin : "",
11117c478bd9Sstevel@tonic-gate 		    incmd, msgbuf, "");
11127c478bd9Sstevel@tonic-gate 	    if (store_status)
11137c478bd9Sstevel@tonic-gate 		    uucpst(Rmtname, _Sfile, "", incmd, msgbuf);
11147c478bd9Sstevel@tonic-gate 	    goto rmfiles;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	(void) snprintf(lbuf, sizeof (lbuf), "%s!%s XQT", Rmtname, user);
11187c478bd9Sstevel@tonic-gate 	logent(_Cmd, lbuf);
11197c478bd9Sstevel@tonic-gate 	DEBUG(4, "cmd %s\n", _Cmd);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/* move files to execute directory and change to that directory */
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	mv_Xfiles();
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	ASSERT(chdir(XQTDIR) == 0, Ct_CHDIR, XQTDIR, errno);
11267c478bd9Sstevel@tonic-gate 	acRexe(&_Xfile[2], origsys, origuser, Myname, Loginuser, incmd);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/* invoke shell to execute command */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	mask = umask(0);
11317c478bd9Sstevel@tonic-gate 	DEBUG(7, "full cmd: %s\n", _Cmd);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	cpucycle();
11347c478bd9Sstevel@tonic-gate 	ret = shio(_Cmd, fin, dfile, errDfile);
11357c478bd9Sstevel@tonic-gate 	if (ret == 0)
11367c478bd9Sstevel@tonic-gate 		acEndexe(cpucycle(), COMPLETE);
11377c478bd9Sstevel@tonic-gate 	else
11387c478bd9Sstevel@tonic-gate 		acEndexe(cpucycle(), PARTIAL);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	umask(mask);
11417c478bd9Sstevel@tonic-gate 	if (ret == -1) {	/* -1 means the fork() failed */
11427c478bd9Sstevel@tonic-gate 		unmv_Xfiles();	/* put things back */
11437c478bd9Sstevel@tonic-gate 		errent(Ct_FORK, buf, errno, __FILE__, __LINE__);
11447c478bd9Sstevel@tonic-gate 		cleanup(1);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	if (ret == 0) {				/* exit == signal == 0 */
11487c478bd9Sstevel@tonic-gate 	    (void) strcpy(msgbuf, "exited normally");
11497c478bd9Sstevel@tonic-gate 	} else {				/* exit != 0 */
11507c478bd9Sstevel@tonic-gate 	    int exitcode = (ret >> 8) & 0377;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	    if (exitcode) {
11537c478bd9Sstevel@tonic-gate 		/* exit != 0 */
11547c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11557c478bd9Sstevel@tonic-gate 		    "exited with status %d", exitcode);
11567c478bd9Sstevel@tonic-gate 	    } else {
11577c478bd9Sstevel@tonic-gate 		/* signal != 0 */
11587c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11597c478bd9Sstevel@tonic-gate 		    "terminated by signal %d", ret & 0177);
11607c478bd9Sstevel@tonic-gate 	    }
11617c478bd9Sstevel@tonic-gate 	    DEBUG(5, "%s\n", msgbuf);
11627c478bd9Sstevel@tonic-gate 	    (void) snprintf(lbuf, sizeof (lbuf), "%s - %s", incmd, msgbuf);
11637c478bd9Sstevel@tonic-gate 	    logent(lbuf, "COMMAND FAIL");
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	/* change back to spool directory */
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	chremdir(Rmtname);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/* remove file */
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	rm_Xfiles();
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/*
11757c478bd9Sstevel@tonic-gate 	 * We used to append stderr to stdout. Since stderr can
11767c478bd9Sstevel@tonic-gate 	 * now be specified separately, never append it to stdout.
11777c478bd9Sstevel@tonic-gate 	 * It can still be gotten via -s status file option.
11787c478bd9Sstevel@tonic-gate 	 */
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	if (!EQUALS(fout, "/dev/null")) {
11817c478bd9Sstevel@tonic-gate 	    /*
11827c478bd9Sstevel@tonic-gate 	     * if output is on this machine copy output
11837c478bd9Sstevel@tonic-gate 	     * there, otherwise spawn job to send to send
11847c478bd9Sstevel@tonic-gate 	     * output elsewhere.
11857c478bd9Sstevel@tonic-gate 	     */
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	    if (EQUALS(sysout, Myname)) {
11887c478bd9Sstevel@tonic-gate 		if ((xmv(dfile, fout)) != 0) {
11897c478bd9Sstevel@tonic-gate 		    logent("FAILED", "COPY");
11907c478bd9Sstevel@tonic-gate 		    scWrite();
11917c478bd9Sstevel@tonic-gate 		    (void) snprintf(msgbuf + strlen(msgbuf),
11927c478bd9Sstevel@tonic-gate 			(sizeof (msgbuf) - strlen(msgbuf)),
11937c478bd9Sstevel@tonic-gate 			"\nCould not move stdout to %s,", fout);
11947c478bd9Sstevel@tonic-gate 		    if (putinpub(fout, dfile, origuser) == 0)
11957c478bd9Sstevel@tonic-gate 			(void) snprintf(msgbuf + strlen(msgbuf),
11967c478bd9Sstevel@tonic-gate 			    (sizeof (msgbuf) - strlen(msgbuf)),
11977c478bd9Sstevel@tonic-gate 			    "\n\tstdout left in %s.", fout);
11987c478bd9Sstevel@tonic-gate 		    else
11997c478bd9Sstevel@tonic-gate 			(void) strlcat(msgbuf, " stdout lost.",
12007c478bd9Sstevel@tonic-gate 			    sizeof (msgbuf));
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 	    } else {
1203*462be471Sceastha 		char *bname;
1204*462be471Sceastha 
12057c478bd9Sstevel@tonic-gate 		if (eaccess(GRADES, 04) != -1)
12067c478bd9Sstevel@tonic-gate 			dqueue = fdgrade();
12077c478bd9Sstevel@tonic-gate 		else
12087c478bd9Sstevel@tonic-gate 			dqueue = Grade;
12097c478bd9Sstevel@tonic-gate 		gename(CMDPRE, sysout, dqueue, tempname);
12107c478bd9Sstevel@tonic-gate 		(void) snprintf(cfile, sizeof (cfile), "%s/%s",
12117c478bd9Sstevel@tonic-gate 		    WORKSPACE, tempname);
12127c478bd9Sstevel@tonic-gate 		fp = fdopen(ret = creat(cfile, CFILEMODE), "w");
12137c478bd9Sstevel@tonic-gate 		ASSERT(ret >= 0 && fp != NULL, Ct_OPEN, cfile, errno);
1214*462be471Sceastha 		bname = BASENAME(dfile, '/');
12157c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "S %s %s %s -d %s 0666\n",
1216*462be471Sceastha 		    bname, fout, user, bname);
12177c478bd9Sstevel@tonic-gate 		fclose(fp);
12187c478bd9Sstevel@tonic-gate 		(void) snprintf(sendsys, sizeof (sendsys), "%s/%c", sysout,
12197c478bd9Sstevel@tonic-gate 		    dqueue);
12207c478bd9Sstevel@tonic-gate 		sendsys[MAXNAMESIZE-1] = '\0';
12217c478bd9Sstevel@tonic-gate 		wfcommit(dfile, BASENAME(dfile, '/'), sendsys);
12227c478bd9Sstevel@tonic-gate 		wfcommit(cfile, BASENAME(cfile, '/'), sendsys);
12237c478bd9Sstevel@tonic-gate 	    }
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	if (!EQUALS(ferr, "/dev/null")) {
12267c478bd9Sstevel@tonic-gate 	    /*
12277c478bd9Sstevel@tonic-gate 	     * if stderr is on this machine copy output
12287c478bd9Sstevel@tonic-gate 	     * there, otherwise spawn job to send to send
12297c478bd9Sstevel@tonic-gate 	     * it elsewhere.
12307c478bd9Sstevel@tonic-gate 	     */
12317c478bd9Sstevel@tonic-gate 	    if (EQUALS(syserr, Myname)) {
12327c478bd9Sstevel@tonic-gate 		errname = ferr;
12337c478bd9Sstevel@tonic-gate 		if ((xmv(errDfile, ferr)) != 0) {
12347c478bd9Sstevel@tonic-gate 		    logent("FAILED", "COPY");
12357c478bd9Sstevel@tonic-gate 		    scWrite();
12367c478bd9Sstevel@tonic-gate 		    (void) snprintf(msgbuf + strlen(msgbuf),
12377c478bd9Sstevel@tonic-gate 			(sizeof (msgbuf) - strlen(msgbuf)),
12387c478bd9Sstevel@tonic-gate 			"\nCould not move stderr to %s,", ferr);
12397c478bd9Sstevel@tonic-gate 		    if (putinpub(ferr, errDfile, origuser) == 0) {
12407c478bd9Sstevel@tonic-gate 			(void) snprintf(msgbuf+strlen(msgbuf),
12417c478bd9Sstevel@tonic-gate 			    (sizeof (msgbuf) - strlen(msgbuf)),
12427c478bd9Sstevel@tonic-gate 			    "\n\tstderr left in %s", ferr);
12437c478bd9Sstevel@tonic-gate 		    } else {
12447c478bd9Sstevel@tonic-gate 			errname = errDfile;
12457c478bd9Sstevel@tonic-gate 			(void) strlcat(msgbuf, " stderr lost.",
12467c478bd9Sstevel@tonic-gate 			    sizeof (msgbuf));
12477c478bd9Sstevel@tonic-gate 		    }
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 	    } else {
1250*462be471Sceastha 		char *bname;
1251*462be471Sceastha 
12527c478bd9Sstevel@tonic-gate 		if (eaccess(GRADES, 04) != -1)
12537c478bd9Sstevel@tonic-gate 			dqueue = fdgrade();
12547c478bd9Sstevel@tonic-gate 		else
12557c478bd9Sstevel@tonic-gate 			dqueue = Grade;
12567c478bd9Sstevel@tonic-gate 		gename(CMDPRE, syserr, dqueue, tempname);
12577c478bd9Sstevel@tonic-gate 		(void) snprintf(cfile, sizeof (cfile), "%s/%s",
12587c478bd9Sstevel@tonic-gate 		    WORKSPACE, tempname);
12597c478bd9Sstevel@tonic-gate 		fp = fdopen(ret = creat(cfile, CFILEMODE), "w");
12607c478bd9Sstevel@tonic-gate 		ASSERT(ret >= 0 && fp != NULL, Ct_OPEN, cfile, errno);
1261*462be471Sceastha 		bname = BASENAME(errDfile, '/');
12627c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "S %s %s %s -d %s 0666\n",
1263*462be471Sceastha 		    bname, ferr, user, bname);
12647c478bd9Sstevel@tonic-gate 		fclose(fp);
12657c478bd9Sstevel@tonic-gate 		(void) snprintf(sendsys, sizeof (sendsys), "%s/%c",
12667c478bd9Sstevel@tonic-gate 		    syserr, dqueue);
12677c478bd9Sstevel@tonic-gate 		sendsys[MAXNAMESIZE-1] = '\0';
12687c478bd9Sstevel@tonic-gate 		wfcommit(errDfile, BASENAME(errDfile, '/'), sendsys);
12697c478bd9Sstevel@tonic-gate 		wfcommit(cfile, BASENAME(cfile, '/'), sendsys);
12707c478bd9Sstevel@tonic-gate 	    }
12717c478bd9Sstevel@tonic-gate 	} else {
12727c478bd9Sstevel@tonic-gate /*
12737c478bd9Sstevel@tonic-gate  * If we conditionally create stderr tempfile, we must
12747c478bd9Sstevel@tonic-gate  * remove this unlink() since errDfile may REALLY be /dev/null
12757c478bd9Sstevel@tonic-gate  */
12767c478bd9Sstevel@tonic-gate 	    unlink(errDfile);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (ret == 0) {
12807c478bd9Sstevel@tonic-gate 	    if (send_zero)
12817c478bd9Sstevel@tonic-gate 		retosndr(user, Rmtname, "", incmd, msgbuf, "");
12827c478bd9Sstevel@tonic-gate 	    if (store_status)
12837c478bd9Sstevel@tonic-gate 		uucpst(Rmtname, _Sfile, "", incmd, msgbuf);
12847c478bd9Sstevel@tonic-gate 	} else {
12857c478bd9Sstevel@tonic-gate 	    if (send_nonzero)
12867c478bd9Sstevel@tonic-gate 		retosndr(user, Rmtname, return_stdin ? fin : "",
12877c478bd9Sstevel@tonic-gate 			incmd, msgbuf, errname);
12887c478bd9Sstevel@tonic-gate 	    if (store_status)
12897c478bd9Sstevel@tonic-gate 		uucpst(Rmtname, _Sfile, errname, incmd, msgbuf);
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate rmfiles:
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	/* delete job files in spool directory */
12957c478bd9Sstevel@tonic-gate 	xfp = fopen(_Xfile, "r");
12967c478bd9Sstevel@tonic-gate 	ASSERT(xfp != NULL, Ct_OPEN, _Xfile, errno);
12977c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, xfp) != NULL) {
12987c478bd9Sstevel@tonic-gate 		if (buf[0] != X_RQDFILE)
12997c478bd9Sstevel@tonic-gate 			continue;
13007c478bd9Sstevel@tonic-gate 		(void) sscanf(&buf[1], "%63s", file);
13017c478bd9Sstevel@tonic-gate 		expfile(file);
13027c478bd9Sstevel@tonic-gate 		if (chkpth(file, CK_WRITE) != FAIL)
13037c478bd9Sstevel@tonic-gate 			(void) unlink(file);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 	(void) unlink(_Xfile);
13067c478bd9Sstevel@tonic-gate 	fclose(xfp);
13077c478bd9Sstevel@tonic-gate     }
13087c478bd9Sstevel@tonic-gate }
1309