xref: /csrg-svn/usr.bin/uucp/uuxqt/uuxqt.c (revision 34176)
113697Ssam #ifndef lint
2*34176Srick static char sccsid[] = "@(#)uuxqt.c	5.11	(Berkeley) 05/04/88";
313697Ssam #endif
413697Ssam 
513697Ssam #include "uucp.h"
613697Ssam #include <sys/stat.h>
713697Ssam #ifdef	NDIR
813697Ssam #include "ndir.h"
913697Ssam #else
1013702Ssam #include <sys/dir.h>
1113697Ssam #endif
1217846Sralph #include <signal.h>
1313697Ssam 
1418628Sralph #define BADCHARS	"&^|(`\\<>;\"{}\n'"
1518628Sralph #define RECHECKTIME	60*10	/* 10 minutes */
1617870Sralph 
1713697Ssam #define APPCMD(d) {\
1813697Ssam char *p;\
1917846Sralph for (p = d; *p != '\0';) *cmdp++ = *p++; *cmdp++ = ' '; *cmdp = '\0';}
2013697Ssam 
2133579Srick extern char Filent[LLEN][NAMESIZE];
2233579Srick 
2313697Ssam /*
2413697Ssam  *	uuxqt will execute commands set up by a uux command,
2513697Ssam  *	usually from a remote machine - set by uucp.
2613697Ssam  */
2713697Ssam 
2813697Ssam #define	NCMDS	50
2917846Sralph char *Cmds[NCMDS+1];
3017846Sralph int Notify[NCMDS+1];
3117846Sralph #define	NT_YES	0	/* if should notify on execution */
3217846Sralph #define	NT_ERR	1	/* if should notify if non-zero exit status (-z equivalent) */
3317846Sralph #define	NT_NO	2	/* if should not notify ever (-n equivalent) */
3413697Ssam 
3517846Sralph extern int Nfiles;
3617846Sralph 
3718628Sralph int TransferSucceeded = 1;
3813697Ssam int notiok = 1;
3913697Ssam int nonzero = 0;
4013697Ssam 
4125147Sbloom struct timeb Now;
4225147Sbloom 
4318628Sralph char PATH[MAXFULLNAME] = "PATH=/bin:/usr/bin:/usr/ucb";
4433966Srick char UU_MACHINE[MAXFULLNAME];
4518628Sralph char Shell[MAXFULLNAME];
4618628Sralph char HOME[MAXFULLNAME];
4717846Sralph 
4818628Sralph extern char **environ;
4918628Sralph char *nenv[] = {
5018628Sralph 	PATH,
5118628Sralph 	Shell,
5218628Sralph 	HOME,
5333966Srick 	UU_MACHINE,
5418628Sralph 	0
5518628Sralph };
5617846Sralph 
5713697Ssam /*  to remove restrictions from uuxqt
5813697Ssam  *  define ALLOK 1
5913697Ssam  *
6013697Ssam  *  to add allowable commands, add to the file CMDFILE
6113697Ssam  *  A line of form "PATH=..." changes the search path
6213697Ssam  */
6313697Ssam main(argc, argv)
6413697Ssam char *argv[];
6513697Ssam {
6633579Srick 	char xcmd[BUFSIZ*2];
6713697Ssam 	int argnok;
6817846Sralph 	int notiflg;
6933579Srick 	char xfile[MAXFULLNAME], user[MAXFULLNAME], buf[BUFSIZ*2];
7017846Sralph 	char lbuf[MAXFULLNAME];
7113697Ssam 	char cfile[NAMESIZE], dfile[MAXFULLNAME];
7213697Ssam 	char file[NAMESIZE];
7313697Ssam 	char fin[MAXFULLNAME], sysout[NAMESIZE], fout[MAXFULLNAME];
7413697Ssam 	register FILE *xfp, *fp;
7513697Ssam 	FILE *dfp;
7613697Ssam 	char path[MAXFULLNAME];
7733579Srick 	char cmd[BUFSIZ*2];
7813697Ssam 	char *cmdp, prm[1000], *ptr;
7913697Ssam 	char *getprm(), *lastpart();
8033966Srick 	int uid, ret, badfiles;
8113697Ssam 	register int i;
8213697Ssam 	int stcico = 0;
8318628Sralph 	time_t xstart, xnow;
8413697Ssam 	char retstat[30];
8533966Srick 	extern char *optarg;
8633966Srick 	extern int optind;
8713697Ssam 
8813697Ssam 	strcpy(Progname, "uuxqt");
8913697Ssam 	uucpname(Myname);
90*34176Srick 	strcpy(Rmtname, Myname);
9113697Ssam 
9213697Ssam 	umask(WFMASK);
9313697Ssam 	Ofn = 1;
9413697Ssam 	Ifn = 0;
9533966Srick 	while ((i = getopt(argc, argv, "x:S:")) != EOF)
9633966Srick 		switch(i) {
9713697Ssam 		case 'x':
9817846Sralph 			chkdebug();
9933966Srick 			Debug = atoi(optarg);
10013697Ssam 			if (Debug <= 0)
10113697Ssam 				Debug = 1;
10213697Ssam 			break;
10333579Srick 		case 'S':
10433966Srick 			Spool = optarg;
10533579Srick 			DEBUG(1, "Spool set to %s", Spool);
10633579Srick 			break;
10733966Srick 		case '?':
10813697Ssam 		default:
10933966Srick 			fprintf(stderr, "unknown flag %s\n", argv[optind-1]);
11013697Ssam 				break;
11113697Ssam 		}
11213697Ssam 
11318628Sralph 	DEBUG(4, "\n\n** START **\n", CNULL);
11433966Srick 	if (subchdir(Spool) < 0) {
11533966Srick 		syslog(LOG_WARNING, "chdir(%s) failed: %m", Spool);
11633966Srick 		cleanup(1);
11733966Srick 	}
11813697Ssam 	strcpy(Wrkdir, Spool);
11913697Ssam 	uid = getuid();
12033579Srick 	if (guinfo(uid, User, path) != SUCCESS) {
12133966Srick 		syslog(LOG_WARNING, "Can't find username for uid %d", uid);
12233579Srick 		DEBUG(1, "Using username", "uucp");
12333579Srick 		strcpy(User, "uucp");
12433579Srick 	}
12517846Sralph 	setgid(getegid());
12617846Sralph 	setuid(geteuid());
12717846Sralph 
12813697Ssam 	DEBUG(4, "User - %s\n", User);
12923692Sbloom 	if (ulockf(X_LOCK, X_LOCKTIME) != 0)
13013697Ssam 		exit(0);
13113697Ssam 
13213697Ssam 	fp = fopen(CMDFILE, "r");
13313697Ssam 	if (fp == NULL) {
13417846Sralph 		logent(CANTOPEN, CMDFILE);
13513697Ssam 		Cmds[0] = "rmail";
13613697Ssam 		Cmds[1] = "rnews";
13713697Ssam 		Cmds[2] = "ruusend";
13813697Ssam 		Cmds[3] = NULL;
13913697Ssam 		goto doprocess;
14013697Ssam 	}
14113697Ssam 	DEBUG(5, "%s opened\n", CMDFILE);
14217846Sralph 	for (i=0; i<NCMDS && cfgets(xcmd, sizeof(xcmd), fp) != NULL; i++) {
14317846Sralph 		int j;
14417846Sralph 		/* strip trailing whitespace */
14517846Sralph 		for (j = strlen(xcmd)-1; j >= 0; --j)
14617846Sralph 			if (xcmd[j] == '\n' || xcmd[j] == ' ' || xcmd[j] == '\t')
14717846Sralph 				xcmd[j] = '\0';
14817846Sralph 			else
14917846Sralph 				break;
15017846Sralph 		/* look for imbedded whitespace */
15117846Sralph 		for (; j >= 0; --j)
15217846Sralph 			if (xcmd[j] == '\n' || xcmd[j] == ' ' || xcmd[j] == '\t')
15317846Sralph 				break;
15417846Sralph 		/* skip this entry if it has embedded whitespace */
15517846Sralph 		/* This defends against a bad PATH=, for example */
15617846Sralph 		if (j >= 0) {
15717846Sralph 			logent(xcmd, "BAD WHITESPACE");
15817846Sralph 			continue;
15917846Sralph 		}
16013697Ssam 		if (strncmp(xcmd, "PATH=", 5) == 0) {
16113697Ssam 			strcpy(PATH, xcmd);
16217846Sralph 			i--;	/*kludge */
16313697Ssam 			continue;
16413697Ssam 		}
16513697Ssam 		DEBUG(5, "xcmd = %s\n", xcmd);
16617846Sralph 
16717846Sralph 		if ((ptr = index(xcmd, ',')) != NULL) {
16817846Sralph 			*ptr++ = '\0';
16917846Sralph 			if (strncmp(ptr, "Err", 3) == SAME)
17017846Sralph 				Notify[i] = NT_ERR;
17117846Sralph 			else if (strcmp(ptr, "No") == SAME)
17217846Sralph 				Notify[i] = NT_NO;
17317846Sralph 			else
17417846Sralph 				Notify[i] = NT_YES;
17517846Sralph 		} else
17617846Sralph 			Notify[i] = NT_YES;
17717846Sralph 		if ((Cmds[i] = malloc((unsigned)(strlen(xcmd)+1))) == NULL) {
17817846Sralph 			DEBUG(1, "MALLOC FAILED", CNULL);
17917846Sralph 			break;
18017846Sralph 		}
18113697Ssam 		strcpy(Cmds[i], xcmd);
18213697Ssam 	}
18317846Sralph 	Cmds[i] = CNULL;
18413697Ssam 	fclose(fp);
18513697Ssam 
18613697Ssam doprocess:
18718628Sralph 
18818628Sralph 	(void) sprintf(HOME, "HOME=%s", Spool);
18918628Sralph 	(void) sprintf(Shell, "SHELL=%s", SHELL);
19018628Sralph 	environ = nenv; /* force use if our environment */
19118628Sralph 
19218628Sralph 	DEBUG(11,"path = %s\n", getenv("PATH"));
19318628Sralph 
19417846Sralph 	DEBUG(4, "process %s\n", CNULL);
19533579Srick 
19618628Sralph 	time(&xstart);
19713697Ssam 	while (gtxfile(xfile) > 0) {
19825147Sbloom 		/* if /etc/nologin exists, exit cleanly */
19925147Sbloom #if defined(BSD4_2) || defined(USG)
20025147Sbloom 		if (access(NOLOGIN) == 0) {
20125147Sbloom #else !BSD4_2 && ! USG
20217846Sralph 		ultouch();
20317846Sralph 		if (nologinflag) {
20425147Sbloom #endif !BSD4_2 && !USG
20517846Sralph 			logent(NOLOGIN, "UUXQT SHUTDOWN");
20617846Sralph 			if (Debug)
20717846Sralph 				logent("debugging", "continuing anyway");
20817846Sralph 			else
20917846Sralph 				break;
21017846Sralph 		}
21113697Ssam 		DEBUG(4, "xfile - %s\n", xfile);
21213697Ssam 
21313697Ssam 		xfp = fopen(subfile(xfile), "r");
21433966Srick 		if (xfp == NULL) {
21533966Srick 			syslog(LOG_ERR, "fopen(%s) failed: %m", subfile(xfile));
21633966Srick 			cleanup(1);
21733966Srick 		}
21813697Ssam 
21913697Ssam 		/*  initialize to default  */
22013697Ssam 		strcpy(user, User);
22117846Sralph 		strcpy(fin, DEVNULL);
22217846Sralph 		strcpy(fout, DEVNULL);
22323692Sbloom 		strcpy(sysout, Myname);
22417846Sralph 		badfiles = 0;
22513697Ssam 		while (fgets(buf, BUFSIZ, xfp) != NULL) {
22633579Srick 	if(buf[0] != '\0' && buf[0] != '#' &&
22733579Srick 		buf[1] != ' ' && buf[1] != '\0' && buf[1] != '\n') {
22833579Srick 		char *bnp, cfilename[BUFSIZ];
22933579Srick 		DEBUG(4, "uuxqt: buf = %s\n", buf);
23033579Srick 		bnp = rindex(xfile, '/');
23133579Srick 		sprintf(cfilename, "%s/%s", CORRUPT,
23233579Srick 			bnp ? bnp + 1 : xfile);
23333579Srick 		DEBUG(4, "uuxqt: move %s to ", xfile);
23433579Srick 		DEBUG(4, "%s\n", cfilename);
23533579Srick 		xmv(xfile, cfilename);
23633966Srick 		syslog(LOG_WARNING, "%s: X. FILE CORRUPTED", xfile);
23733579Srick 		fclose(xfp);
23833579Srick 		goto doprocess;
23933579Srick 
24033579Srick 	}
24113697Ssam 			switch (buf[0]) {
24213697Ssam 			case X_USER:
24317846Sralph 				sscanf(&buf[1], "%s %s", user, Rmtname);
24433966Srick 				sprintf(UU_MACHINE, "UU_MACHINE=%s", Rmtname);
24513697Ssam 				break;
24617846Sralph 			case X_RETURNTO:
24717846Sralph 				sscanf(&buf[1], "%s", user);
24817846Sralph 				break;
24913697Ssam 			case X_STDIN:
25013697Ssam 				sscanf(&buf[1], "%s", fin);
25113697Ssam 				i = expfile(fin);
25213697Ssam 				/* rti!trt: do not check permissions of
25313697Ssam 				 * vanilla spool file */
25413697Ssam 				if (i != 0
25513697Ssam 				 && (chkpth("", "", fin) || anyread(fin) != 0))
25613697Ssam 					badfiles = 1;
25713697Ssam 				break;
25813697Ssam 			case X_STDOUT:
25913697Ssam 				sscanf(&buf[1], "%s%s", fout, sysout);
26023692Sbloom 				sysout[MAXBASENAME] = '\0';
26113697Ssam 				/* rti!trt: do not check permissions of
26213697Ssam 				 * vanilla spool file.  DO check permissions
26313697Ssam 				 * of writing on a non-vanilla file */
26413697Ssam 				i = 1;
26513697Ssam 				if (fout[0] != '~' || prefix(sysout, Myname))
26613697Ssam 					i = expfile(fout);
26713697Ssam 				if (i != 0
26813697Ssam 				 && (chkpth("", "", fout)
26913697Ssam 					|| chkperm(fout, (char *)1)))
27013697Ssam 					badfiles = 1;
27113697Ssam 				break;
27213697Ssam 			case X_CMD:
27313697Ssam 				strcpy(cmd, &buf[2]);
27413697Ssam 				if (*(cmd + strlen(cmd) - 1) == '\n')
27513697Ssam 					*(cmd + strlen(cmd) - 1) = '\0';
27613697Ssam 				break;
27713697Ssam 			case X_NONOTI:
27813697Ssam 				notiok = 0;
27913697Ssam 				break;
28013697Ssam 			case X_NONZERO:
28113697Ssam 				nonzero = 1;
28213697Ssam 				break;
28313697Ssam 			default:
28413697Ssam 				break;
28513697Ssam 			}
28613697Ssam 		}
28713697Ssam 
28813697Ssam 		fclose(xfp);
28913697Ssam 		DEBUG(4, "fin - %s, ", fin);
29013697Ssam 		DEBUG(4, "fout - %s, ", fout);
29113697Ssam 		DEBUG(4, "sysout - %s, ", sysout);
29213697Ssam 		DEBUG(4, "user - %s\n", user);
29313697Ssam 		DEBUG(4, "cmd - %s\n", cmd);
29413697Ssam 
29513697Ssam 		/*  command execution  */
29617846Sralph 		if (strcmp(fout, DEVNULL) == SAME)
29717846Sralph 			strcpy(dfile,DEVNULL);
29813697Ssam 		else
29913697Ssam 			gename(DATAPRE, sysout, 'O', dfile);
30013697Ssam 
30113697Ssam 		/* expand file names where necessary */
30213697Ssam 		expfile(dfile);
30318628Sralph 		cmdp = buf;
30413697Ssam 		ptr = cmd;
30513697Ssam 		xcmd[0] = '\0';
30613697Ssam 		argnok = 0;
30713697Ssam 		while ((ptr = getprm(ptr, prm)) != NULL) {
30813697Ssam 			if (prm[0] == ';' || prm[0] == '^'
30913697Ssam 			  || prm[0] == '&'  || prm[0] == '|') {
31013697Ssam 				xcmd[0] = '\0';
31113697Ssam 				APPCMD(prm);
31213697Ssam 				continue;
31313697Ssam 			}
31413697Ssam 
31517846Sralph 			if ((argnok = argok(xcmd, prm)) != SUCCESS)
31613697Ssam 				/*  command not valid  */
31713697Ssam 				break;
31813697Ssam 
31913697Ssam 			if (prm[0] == '~')
32013697Ssam 				expfile(prm);
32113697Ssam 			APPCMD(prm);
32213697Ssam 		}
32317846Sralph 		/*
32417846Sralph 		 * clean up trailing ' ' in command.
32517846Sralph 		 */
32617846Sralph 		if (cmdp > buf && cmdp[0] == '\0' && cmdp[-1] == ' ')
32717846Sralph 			*--cmdp = '\0';
32813697Ssam 		if (argnok || badfiles) {
32913697Ssam 			sprintf(lbuf, "%s XQT DENIED", user);
33013697Ssam 			logent(cmd, lbuf);
33113697Ssam 			DEBUG(4, "bad command %s\n", prm);
33213697Ssam 			notify(user, Rmtname, cmd, "DENIED");
33313697Ssam 			goto rmfiles;
33413697Ssam 		}
33513697Ssam 		sprintf(lbuf, "%s XQT", user);
33613697Ssam 		logent(buf, lbuf);
33713697Ssam 		DEBUG(4, "cmd %s\n", buf);
33813697Ssam 
33913697Ssam 		mvxfiles(xfile);
34033966Srick 		if (subchdir(XQTDIR) < 0) {
34133966Srick 			syslog(LOG_ERR, "chdir(%s) failed: %m", XQTDIR);
34233966Srick 			cleanup(1);
34333966Srick 		}
34418628Sralph 		ret = shio(buf, fin, dfile);
34513697Ssam 		sprintf(retstat, "signal %d, exit %d", ret & 0377,
34613697Ssam 		  (ret>>8) & 0377);
34713697Ssam 		if (strcmp(xcmd, "rmail") == SAME)
34813697Ssam 			notiok = 0;
34913697Ssam 		if (strcmp(xcmd, "rnews") == SAME)
35013697Ssam 			nonzero = 1;
35117846Sralph 		notiflg = chknotify(xcmd);
35217846Sralph 		if (notiok && notiflg != NT_NO &&
35317846Sralph 		   (ret != 0 || (!nonzero && notiflg == NT_YES)))
35413697Ssam 			notify(user, Rmtname, cmd, retstat);
35513697Ssam 		else if (ret != 0 && strcmp(xcmd, "rmail") == SAME) {
35613697Ssam 			/* mail failed - return letter to sender  */
35717846Sralph #ifdef	DANGEROUS
35817846Sralph 			/* NOT GUARANTEED SAFE!!! */
35917846Sralph 			if (!nonzero)
36017846Sralph 				retosndr(user, Rmtname, fin);
36117846Sralph #else
36217846Sralph 			notify(user, Rmtname, cmd, retstat);
36317846Sralph #endif
36417846Sralph 			sprintf(buf, "%s (%s) from %s!%s", buf, retstat, Rmtname, user);
36513697Ssam 			logent("MAIL FAIL", buf);
36613697Ssam 		}
36713697Ssam 		DEBUG(4, "exit cmd - %d\n", ret);
36833966Srick 		if (subchdir(Spool) < 0) {
36933966Srick 			syslog(LOG_ERR, "chdir(%s) failed: %m", Spool);
37033966Srick 			cleanup(1);
37133966Srick 		}
37213697Ssam 		rmxfiles(xfile);
37313697Ssam 		if (ret != 0) {
37413697Ssam 			/*  exit status not zero */
37513697Ssam 			dfp = fopen(subfile(dfile), "a");
37633966Srick 			if (dfp == NULL) {
37733966Srick 				syslog(LOG_ERR, "fopen(%s) failed: %m",
37833966Srick 					subfile(dfile));
37933966Srick 				cleanup(1);
38033966Srick 			}
38113697Ssam 			fprintf(dfp, "exit status %d", ret);
38213697Ssam 			fclose(dfp);
38313697Ssam 		}
38417846Sralph 		if (strcmp(fout, DEVNULL) != SAME) {
38513697Ssam 			if (prefix(sysout, Myname)) {
38613697Ssam 				xmv(dfile, fout);
38713697Ssam 				chmod(fout, BASEMODE);
38818628Sralph 			} else {
38917846Sralph 				char *cp = rindex(user, '!');
39013697Ssam 				gename(CMDPRE, sysout, 'O', cfile);
39113697Ssam 				fp = fopen(subfile(cfile), "w");
39233966Srick 				if (fp == NULL) {
39333966Srick 					syslog(LOG_ERR, "fopen(%s) failed: %m",
39433966Srick 						subfile(cfile));
39533966Srick 					cleanup(1);
39633966Srick 				}
39717846Sralph 				fprintf(fp, "S %s %s %s - %s 0666\n", dfile,
39817846Sralph 					fout, cp ? cp : user, lastpart(dfile));
39913697Ssam 				fclose(fp);
40013697Ssam 			}
40113697Ssam 		}
40213697Ssam 	rmfiles:
40313697Ssam 		xfp = fopen(subfile(xfile), "r");
40433966Srick 		if (xfp == NULL) {
40533966Srick 			syslog(LOG_ERR, "fopen(%s) failed: %m",
40633966Srick 				subfile(xfile));
40733966Srick 			cleanup(1);
40833966Srick 		}
40913697Ssam 		while (fgets(buf, BUFSIZ, xfp) != NULL) {
41013697Ssam 			if (buf[0] != X_RQDFILE)
41113697Ssam 				continue;
41213697Ssam 			sscanf(&buf[1], "%s", file);
41313697Ssam 			unlink(subfile(file));
41413697Ssam 		}
41513697Ssam 		unlink(subfile(xfile));
41613697Ssam 		fclose(xfp);
41718628Sralph 
41818628Sralph 		/* rescan X. for new work every RECHECKTIME seconds */
41918628Sralph 		time(&xnow);
42018628Sralph 		if (xnow > (xstart + RECHECKTIME)) {
42118628Sralph 			extern int Nfiles;
42218628Sralph 			Nfiles = 0; 	/*force rescan for new work */
42318628Sralph 		}
42418628Sralph 		xstart = xnow;
42513697Ssam 	}
42613697Ssam 
42713697Ssam 	if (stcico)
42813697Ssam 		xuucico("");
42913697Ssam 	cleanup(0);
43013697Ssam }
43113697Ssam 
43213697Ssam 
43313697Ssam cleanup(code)
43413697Ssam int code;
43513697Ssam {
43613697Ssam 	logcls();
43713697Ssam 	rmlock(CNULL);
43817846Sralph #ifdef	VMS
43917846Sralph 	/*
44017846Sralph 	 *	Since we run as a BATCH job we must wait for all processes to
44117846Sralph 	 *	to finish
44217846Sralph 	 */
44318628Sralph 	while(wait(0) != -1)
44418628Sralph 		;
44517846Sralph #endif VMS
44613697Ssam 	exit(code);
44713697Ssam }
44813697Ssam 
44913697Ssam 
45018628Sralph /*
45118628Sralph  *	get a file to execute
45213697Ssam  *
45313697Ssam  *	return codes:  0 - no file  |  1 - file to execute
45413697Ssam  */
45513697Ssam 
45613697Ssam gtxfile(file)
45713697Ssam register char *file;
45813697Ssam {
45913697Ssam 	char pre[3];
46033579Srick 	register int rechecked, i;
46117846Sralph 	time_t ystrdy;		/* yesterday */
46217846Sralph 	struct stat stbuf;	/* for X file age */
46313697Ssam 
46413697Ssam 	pre[0] = XQTPRE;
46513697Ssam 	pre[1] = '.';
46613697Ssam 	pre[2] = '\0';
46713697Ssam 	rechecked = 0;
46813697Ssam retry:
46933579Srick 	if (Nfiles-- <= 0) {
47033579Srick 		Nfiles = 0;
47113697Ssam 		if (rechecked)
47217846Sralph 			return 0;
47313697Ssam 		rechecked = 1;
47417846Sralph 		DEBUG(4, "iswrk\n", CNULL);
47533579Srick 		return iswrk(file, "get", Spool, pre);
47613697Ssam 	}
47733579Srick 	sprintf(file, "%s/%s", Spool, Filent[0]);
47833579Srick 	for (i=0; i<Nfiles;i++)
47933579Srick 		strcpy(Filent[i], Filent[i+1]);
48033579Srick 
48113697Ssam 	DEBUG(4, "file - %s\n", file);
48213697Ssam 	/* skip spurious subdirectories */
48313697Ssam 	if (strcmp(pre, file) == SAME)
48413697Ssam 		goto retry;
48513697Ssam 	if (gotfiles(file))
48617846Sralph 		return 1;
48717846Sralph 	/* check for old X. file with no work files and remove them. */
48817846Sralph 	if (Nfiles > LLEN/2) {
48917846Sralph 	    time(&ystrdy);
49017846Sralph 	    ystrdy -= (4 * 3600L);		/* 4 hours ago */
49117846Sralph 	    DEBUG(4, "gtxfile: Nfiles > LLEN/2\n", CNULL);
49233579Srick 	    while (Nfiles-- > 0) {
49333579Srick 		sprintf(file, "%s/%s", Spool, Filent[0]);
49433579Srick 		for (i=0; i<Nfiles; i++)
49533579Srick 			strcpy(Filent[i], Filent[i+1]);
49633579Srick 
49733579Srick 		if (gotfiles(file))
49833579Srick 			return 1;
49917846Sralph 		if (stat(subfile(file), &stbuf) == 0)
50017846Sralph 		    if (stbuf.st_mtime <= ystrdy) {
50117846Sralph 			char *bnp, cfilename[NAMESIZE];
50217846Sralph 			DEBUG(4, "gtxfile: move %s to CORRUPT \n", file);
50333579Srick 			bnp = rindex(file, '/');
50417846Sralph 			sprintf(cfilename, "%s/%s", CORRUPT,
50533579Srick 				bnp ? bnp + 1 : file);
50633579Srick 			xmv(file, cfilename);
50733966Srick 			syslog(LOG_WARNING, "%s: X. FILE MISSING FILES", file);
50817846Sralph 		    }
50917846Sralph 	    }
51033579Srick  	    Nfiles = 0;
51117846Sralph 	    DEBUG(4, "iswrk\n", CNULL);
51217846Sralph 	    if (!iswrk(file, "get", Spool, pre))
51317846Sralph 		return 0;
51417846Sralph 	}
51513697Ssam 	goto retry;
51613697Ssam }
51713697Ssam 
51818628Sralph /*
51918628Sralph  *	check for needed files
52013697Ssam  *
52113697Ssam  *	return codes:  0 - not ready  |  1 - all files ready
52213697Ssam  */
52313697Ssam 
52413697Ssam gotfiles(file)
52513697Ssam register char *file;
52613697Ssam {
52713697Ssam 	struct stat stbuf;
52813697Ssam 	register FILE *fp;
52913697Ssam 	char buf[BUFSIZ], rqfile[MAXFULLNAME];
53013697Ssam 
53113697Ssam 	fp = fopen(subfile(file), "r");
53213697Ssam 	if (fp == NULL)
53317846Sralph 		return 0;
53413697Ssam 
53513697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
53613697Ssam 		DEBUG(4, "%s\n", buf);
53713697Ssam 		if (buf[0] != X_RQDFILE)
53813697Ssam 			continue;
53913697Ssam 		sscanf(&buf[1], "%s", rqfile);
54013697Ssam 		expfile(rqfile);
54113697Ssam 		if (stat(subfile(rqfile), &stbuf) == -1) {
54213697Ssam 			fclose(fp);
54317846Sralph 			return 0;
54413697Ssam 		}
54513697Ssam 	}
54613697Ssam 
54713697Ssam 	fclose(fp);
54817846Sralph 	return 1;
54913697Ssam }
55013697Ssam 
55113697Ssam 
55218628Sralph /*
55318628Sralph  *	remove execute files to x-directory
55413697Ssam  */
55513697Ssam 
55613697Ssam rmxfiles(xfile)
55713697Ssam register char *xfile;
55813697Ssam {
55913697Ssam 	register FILE *fp;
56013697Ssam 	char buf[BUFSIZ], file[NAMESIZE], tfile[NAMESIZE];
56113697Ssam 	char tfull[MAXFULLNAME];
56213697Ssam 
56313697Ssam 	if((fp = fopen(subfile(xfile), "r")) == NULL)
56413697Ssam 		return;
56513697Ssam 
56613697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
56713697Ssam 		if (buf[0] != X_RQDFILE)
56813697Ssam 			continue;
56913697Ssam 		if (sscanf(&buf[1], "%s%s", file, tfile) < 2)
57013697Ssam 			continue;
57113697Ssam 		sprintf(tfull, "%s/%s", XQTDIR, tfile);
57213697Ssam 		unlink(subfile(tfull));
57313697Ssam 	}
57413697Ssam 	fclose(fp);
57513697Ssam 	return;
57613697Ssam }
57713697Ssam 
57813697Ssam 
57918628Sralph /*
58018628Sralph  *	move execute files to x-directory
58113697Ssam  */
58213697Ssam 
58313697Ssam mvxfiles(xfile)
58413697Ssam char *xfile;
58513697Ssam {
58613697Ssam 	register FILE *fp;
58713697Ssam 	char buf[BUFSIZ], ffile[MAXFULLNAME], tfile[NAMESIZE];
58813697Ssam 	char tfull[MAXFULLNAME];
58913697Ssam 
59013697Ssam 	if((fp = fopen(subfile(xfile), "r")) == NULL)
59113697Ssam 		return;
59213697Ssam 
59313697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
59413697Ssam 		if (buf[0] != X_RQDFILE)
59513697Ssam 			continue;
59613697Ssam 		if (sscanf(&buf[1], "%s%s", ffile, tfile) < 2)
59713697Ssam 			continue;
59813697Ssam 		expfile(ffile);
59913697Ssam 		sprintf(tfull, "%s/%s", XQTDIR, tfile);
60013697Ssam 		unlink(subfile(tfull));
60133966Srick 		if (xmv(ffile, tfull) != 0) {
60233966Srick 			syslog(LOG_WARNING, "xmv(%s,%s) failed: %m",
60333966Srick 				ffile, tfull);
60433966Srick 			cleanup(1);
60533966Srick 		}
60613697Ssam 	}
60713697Ssam 	fclose(fp);
60813697Ssam }
60913697Ssam 
61018628Sralph /*
61118628Sralph  *	check for valid command/argument
61218628Sralph  *	*NOTE - side effect is to set xc to the	command to be executed.
61313697Ssam  *
61413697Ssam  *	return 0 - ok | 1 nok
61513697Ssam  */
61613697Ssam 
61713697Ssam argok(xc, cmd)
61813697Ssam register char *xc, *cmd;
61913697Ssam {
62013697Ssam 	register char **ptr;
62113697Ssam 
62213697Ssam #ifndef ALLOK
62318628Sralph 	if (strpbrk(cmd, BADCHARS) != NULL) {
62417846Sralph 		DEBUG(1,"MAGIC CHARACTER FOUND\n", CNULL);
62518628Sralph 		logent(cmd, "NASTY MAGIC CHARACTER FOUND");
62617846Sralph 		return FAIL;
62717846Sralph 	}
62817846Sralph #endif !ALLOK
62913697Ssam 
63013697Ssam 	if (xc[0] != '\0')
63117846Sralph 		return SUCCESS;
63213697Ssam 
63313697Ssam #ifndef ALLOK
63413697Ssam 	ptr = Cmds;
63517846Sralph 	DEBUG(9, "Compare %s and\n", cmd);
63613697Ssam 	while(*ptr != NULL) {
63717846Sralph 		DEBUG(9, "\t%s\n", *ptr);
63813697Ssam 		if (strcmp(cmd, *ptr) == SAME)
63913697Ssam 			break;
64017846Sralph 		ptr++;
64113697Ssam 	}
64217846Sralph 	if (*ptr == NULL) {
64317846Sralph 		DEBUG(1,"COMMAND NOT FOUND\n", CNULL);
64417846Sralph 		return FAIL;
64517846Sralph 	}
64613697Ssam #endif
64713697Ssam 	strcpy(xc, cmd);
64817846Sralph 	DEBUG(9, "MATCHED %s\n", xc);
64917846Sralph 	return SUCCESS;
65013697Ssam }
65113697Ssam 
65213697Ssam 
65318628Sralph /*
65418628Sralph  *	if notification should be sent for successful execution of cmd
65517846Sralph  *
65617846Sralph  *	return NT_YES - do notification
65717846Sralph  *	       NT_ERR - do notification if exit status != 0
65817846Sralph  *	       NT_NO  - don't do notification ever
65917846Sralph  */
66017846Sralph 
66117846Sralph chknotify(cmd)
66217846Sralph char *cmd;
66317846Sralph {
66417846Sralph 	register char **ptr;
66517846Sralph 	register int *nptr;
66617846Sralph 
66717846Sralph 	ptr = Cmds;
66817846Sralph 	nptr = Notify;
66917846Sralph 	while (*ptr != NULL) {
67017846Sralph 		if (strcmp(cmd, *ptr) == SAME)
67117846Sralph 			return *nptr;
67217846Sralph 		ptr++;
67317846Sralph 		nptr++;
67417846Sralph 	}
67517846Sralph 	return NT_YES;		/* "shouldn't happen" */
67617846Sralph }
67717846Sralph 
67817846Sralph 
67917846Sralph 
68018628Sralph /*
68118628Sralph  *	send mail to user giving execution results
68213697Ssam  */
68313697Ssam 
68413697Ssam notify(user, rmt, cmd, str)
68513697Ssam char *user, *rmt, *cmd, *str;
68613697Ssam {
68733579Srick 	char text[BUFSIZ*2];
68813697Ssam 	char ruser[MAXFULLNAME];
68913697Ssam 
69025968Sbloom 	if (strpbrk(user, BADCHARS) != NULL) {
69125968Sbloom 		char lbuf[MAXFULLNAME];
69225968Sbloom 		sprintf(lbuf, "%s INVALID CHARACTER IN USERNAME", user);
69325968Sbloom 		logent(cmd, lbuf);
69425968Sbloom 		strcpy(user, "postmaster");
69525968Sbloom 	}
69617846Sralph 	sprintf(text, "uuxqt cmd (%s) status (%s)", cmd, str);
69713697Ssam 	if (prefix(rmt, Myname))
69813697Ssam 		strcpy(ruser, user);
69913697Ssam 	else
70013697Ssam 		sprintf(ruser, "%s!%s", rmt, user);
70117846Sralph 	mailst(ruser, text, CNULL);
70213697Ssam }
70313697Ssam 
70418628Sralph /*
70518628Sralph  *	return mail to sender
70613697Ssam  *
70713697Ssam  */
70813697Ssam retosndr(user, rmt, file)
70913697Ssam char *user, *rmt, *file;
71013697Ssam {
71117846Sralph 	char ruser[MAXFULLNAME];
71213697Ssam 
71325968Sbloom 	if (strpbrk(user, BADCHARS) != NULL) {
71425968Sbloom 		char lbuf[MAXFULLNAME];
71525968Sbloom 		sprintf(lbuf, "%s INVALID CHARACTER IN USERNAME", user);
71625968Sbloom 		logent(file, lbuf);
71725968Sbloom 		strcpy(user, "postmaster");
71825968Sbloom 	}
71913697Ssam 	if (strcmp(rmt, Myname) == SAME)
72013697Ssam 		strcpy(ruser, user);
72113697Ssam 	else
72213697Ssam 		sprintf(ruser, "%s!%s", rmt, user);
72313697Ssam 
72413697Ssam 	if (anyread(file) == 0)
72513697Ssam 		mailst(ruser, "Mail failed.  Letter returned to sender.\n", file);
72613697Ssam 	else
72717846Sralph 		mailst(ruser, "Mail failed.  Letter returned to sender.\n", CNULL);
72813697Ssam 	return;
72913697Ssam }
73017870Sralph 
73117870Sralph /*
73218628Sralph  *	execute shell of command with fi and fo as standard input/output
73318628Sralph  */
73418628Sralph 
73518628Sralph shio(cmd, fi, fo)
73618628Sralph char *cmd, *fi, *fo;
73718628Sralph {
73818628Sralph 	int status, f;
73933966Srick 	int pid, ret;
74033579Srick 	char *args[256];
74118628Sralph 	extern int errno;
74218628Sralph 
74318628Sralph 	if (fi == NULL)
74418628Sralph 		fi = DEVNULL;
74518628Sralph 	if (fo == NULL)
74618628Sralph 		fo = DEVNULL;
74718628Sralph 
74833579Srick 	getargs(cmd, args, 256);
74918628Sralph 	DEBUG(3, "shio - %s\n", cmd);
75018628Sralph #ifdef SIGCHLD
75118628Sralph 	signal(SIGCHLD, SIG_IGN);
75218628Sralph #endif SIGCHLD
75318628Sralph 	if ((pid = fork()) == 0) {
75418628Sralph 		signal(SIGINT, SIG_IGN);
75518628Sralph 		signal(SIGHUP, SIG_IGN);
75618628Sralph 		signal(SIGQUIT, SIG_IGN);
75718628Sralph 		close(Ifn);
75818628Sralph 		close(Ofn);
75918628Sralph 		close(0);
76018628Sralph 		setuid(getuid());
76118628Sralph 		f = open(subfile(fi), 0);
76218628Sralph 		if (f != 0) {
76318628Sralph 			logent(fi, "CAN'T READ");
76418628Sralph 			exit(-errno);
76518628Sralph 		}
76618628Sralph 		close(1);
76718628Sralph 		f = creat(subfile(fo), 0666);
76818628Sralph 		if (f != 1) {
76918628Sralph 			logent(fo, "CAN'T WRITE");
77018628Sralph 			exit(-errno);
77118628Sralph 		}
77218628Sralph 		execvp(args[0], args);
77318628Sralph 		exit(100+errno);
77418628Sralph 	}
77518628Sralph 	while ((ret = wait(&status)) != pid && ret != -1)
77618628Sralph 		;
77718628Sralph 	DEBUG(3, "status %d\n", status);
77818628Sralph 	return status;
77918628Sralph }
780