xref: /csrg-svn/usr.bin/uucp/uuxqt/uuxqt.c (revision 25968)
113697Ssam #ifndef lint
2*25968Sbloom static char sccsid[] = "@(#)uuxqt.c	5.8 (Berkeley) 01/24/86";
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 
2113697Ssam /*
2213697Ssam  *	uuxqt will execute commands set up by a uux command,
2313697Ssam  *	usually from a remote machine - set by uucp.
2413697Ssam  */
2513697Ssam 
2613697Ssam #define	NCMDS	50
2717846Sralph char *Cmds[NCMDS+1];
2817846Sralph int Notify[NCMDS+1];
2917846Sralph #define	NT_YES	0	/* if should notify on execution */
3017846Sralph #define	NT_ERR	1	/* if should notify if non-zero exit status (-z equivalent) */
3117846Sralph #define	NT_NO	2	/* if should not notify ever (-n equivalent) */
3213697Ssam 
3317846Sralph extern int Nfiles;
3417846Sralph 
3518628Sralph int TransferSucceeded = 1;
3613697Ssam int notiok = 1;
3713697Ssam int nonzero = 0;
3813697Ssam 
3925147Sbloom struct timeb Now;
4025147Sbloom 
4118628Sralph char PATH[MAXFULLNAME] = "PATH=/bin:/usr/bin:/usr/ucb";
4218628Sralph char Shell[MAXFULLNAME];
4318628Sralph char HOME[MAXFULLNAME];
4417846Sralph 
4518628Sralph extern char **environ;
4618628Sralph char *nenv[] = {
4718628Sralph 	PATH,
4818628Sralph 	Shell,
4918628Sralph 	HOME,
5018628Sralph 	0
5118628Sralph };
5217846Sralph 
5313697Ssam /*  to remove restrictions from uuxqt
5413697Ssam  *  define ALLOK 1
5513697Ssam  *
5613697Ssam  *  to add allowable commands, add to the file CMDFILE
5713697Ssam  *  A line of form "PATH=..." changes the search path
5813697Ssam  */
5913697Ssam main(argc, argv)
6013697Ssam char *argv[];
6113697Ssam {
6213697Ssam 	char xcmd[MAXFULLNAME];
6313697Ssam 	int argnok;
6417846Sralph 	int notiflg;
6517846Sralph 	char xfile[MAXFULLNAME], user[MAXFULLNAME], buf[BUFSIZ];
6617846Sralph 	char lbuf[MAXFULLNAME];
6713697Ssam 	char cfile[NAMESIZE], dfile[MAXFULLNAME];
6813697Ssam 	char file[NAMESIZE];
6913697Ssam 	char fin[MAXFULLNAME], sysout[NAMESIZE], fout[MAXFULLNAME];
7013697Ssam 	register FILE *xfp, *fp;
7113697Ssam 	FILE *dfp;
7213697Ssam 	char path[MAXFULLNAME];
7313697Ssam 	char cmd[BUFSIZ];
7413697Ssam 	char *cmdp, prm[1000], *ptr;
7513697Ssam 	char *getprm(), *lastpart();
7617846Sralph 	int uid, ret, ret2, badfiles;
7713697Ssam 	register int i;
7813697Ssam 	int stcico = 0;
7918628Sralph 	time_t xstart, xnow;
8013697Ssam 	char retstat[30];
8118628Sralph 	char **ep;
8213697Ssam 
8313697Ssam 	strcpy(Progname, "uuxqt");
8413697Ssam 	uucpname(Myname);
8513697Ssam 
8613697Ssam 	umask(WFMASK);
8713697Ssam 	Ofn = 1;
8813697Ssam 	Ifn = 0;
8913697Ssam 	while (argc>1 && argv[1][0] == '-') {
9013697Ssam 		switch(argv[1][1]){
9113697Ssam 		case 'x':
9217846Sralph 			chkdebug();
9313697Ssam 			Debug = atoi(&argv[1][2]);
9413697Ssam 			if (Debug <= 0)
9513697Ssam 				Debug = 1;
9613697Ssam 			break;
9713697Ssam 		default:
9813697Ssam 			fprintf(stderr, "unknown flag %s\n", argv[1]);
9913697Ssam 				break;
10013697Ssam 		}
10113697Ssam 		--argc;  argv++;
10213697Ssam 	}
10313697Ssam 
10418628Sralph 	DEBUG(4, "\n\n** START **\n", CNULL);
10517846Sralph 	ret = subchdir(Spool);
10617846Sralph 	ASSERT(ret >= 0, "CHDIR FAILED", Spool, ret);
10713697Ssam 	strcpy(Wrkdir, Spool);
10813697Ssam 	uid = getuid();
10913697Ssam 	guinfo(uid, User, path);
11017846Sralph 	setgid(getegid());
11117846Sralph 	setuid(geteuid());
11217846Sralph 
11313697Ssam 	DEBUG(4, "User - %s\n", User);
11423692Sbloom 	if (ulockf(X_LOCK, X_LOCKTIME) != 0)
11513697Ssam 		exit(0);
11613697Ssam 
11713697Ssam 	fp = fopen(CMDFILE, "r");
11813697Ssam 	if (fp == NULL) {
11917846Sralph 		logent(CANTOPEN, CMDFILE);
12013697Ssam 		Cmds[0] = "rmail";
12113697Ssam 		Cmds[1] = "rnews";
12213697Ssam 		Cmds[2] = "ruusend";
12313697Ssam 		Cmds[3] = NULL;
12413697Ssam 		goto doprocess;
12513697Ssam 	}
12613697Ssam 	DEBUG(5, "%s opened\n", CMDFILE);
12717846Sralph 	for (i=0; i<NCMDS && cfgets(xcmd, sizeof(xcmd), fp) != NULL; i++) {
12817846Sralph 		int j;
12917846Sralph 		/* strip trailing whitespace */
13017846Sralph 		for (j = strlen(xcmd)-1; j >= 0; --j)
13117846Sralph 			if (xcmd[j] == '\n' || xcmd[j] == ' ' || xcmd[j] == '\t')
13217846Sralph 				xcmd[j] = '\0';
13317846Sralph 			else
13417846Sralph 				break;
13517846Sralph 		/* look for imbedded whitespace */
13617846Sralph 		for (; j >= 0; --j)
13717846Sralph 			if (xcmd[j] == '\n' || xcmd[j] == ' ' || xcmd[j] == '\t')
13817846Sralph 				break;
13917846Sralph 		/* skip this entry if it has embedded whitespace */
14017846Sralph 		/* This defends against a bad PATH=, for example */
14117846Sralph 		if (j >= 0) {
14217846Sralph 			logent(xcmd, "BAD WHITESPACE");
14317846Sralph 			continue;
14417846Sralph 		}
14513697Ssam 		if (strncmp(xcmd, "PATH=", 5) == 0) {
14613697Ssam 			strcpy(PATH, xcmd);
14717846Sralph 			i--;	/*kludge */
14813697Ssam 			continue;
14913697Ssam 		}
15013697Ssam 		DEBUG(5, "xcmd = %s\n", xcmd);
15117846Sralph 
15217846Sralph 		if ((ptr = index(xcmd, ',')) != NULL) {
15317846Sralph 			*ptr++ = '\0';
15417846Sralph 			if (strncmp(ptr, "Err", 3) == SAME)
15517846Sralph 				Notify[i] = NT_ERR;
15617846Sralph 			else if (strcmp(ptr, "No") == SAME)
15717846Sralph 				Notify[i] = NT_NO;
15817846Sralph 			else
15917846Sralph 				Notify[i] = NT_YES;
16017846Sralph 		} else
16117846Sralph 			Notify[i] = NT_YES;
16217846Sralph 		if ((Cmds[i] = malloc((unsigned)(strlen(xcmd)+1))) == NULL) {
16317846Sralph 			DEBUG(1, "MALLOC FAILED", CNULL);
16417846Sralph 			break;
16517846Sralph 		}
16613697Ssam 		strcpy(Cmds[i], xcmd);
16713697Ssam 	}
16817846Sralph 	Cmds[i] = CNULL;
16913697Ssam 	fclose(fp);
17013697Ssam 
17113697Ssam doprocess:
17218628Sralph 
17318628Sralph 	(void) sprintf(HOME, "HOME=%s", Spool);
17418628Sralph 	(void) sprintf(Shell, "SHELL=%s", SHELL);
17518628Sralph 	environ = nenv; /* force use if our environment */
17618628Sralph 
17718628Sralph 	DEBUG(11,"path = %s\n", getenv("PATH"));
17818628Sralph 
17917846Sralph 	DEBUG(4, "process %s\n", CNULL);
18018628Sralph 	time(&xstart);
18113697Ssam 	while (gtxfile(xfile) > 0) {
18225147Sbloom 		/* if /etc/nologin exists, exit cleanly */
18325147Sbloom #if defined(BSD4_2) || defined(USG)
18425147Sbloom 		if (access(NOLOGIN) == 0) {
18525147Sbloom #else !BSD4_2 && ! USG
18617846Sralph 		ultouch();
18717846Sralph 		if (nologinflag) {
18825147Sbloom #endif !BSD4_2 && !USG
18917846Sralph 			logent(NOLOGIN, "UUXQT SHUTDOWN");
19017846Sralph 			if (Debug)
19117846Sralph 				logent("debugging", "continuing anyway");
19217846Sralph 			else
19317846Sralph 				break;
19417846Sralph 		}
19513697Ssam 		DEBUG(4, "xfile - %s\n", xfile);
19613697Ssam 
19713697Ssam 		xfp = fopen(subfile(xfile), "r");
19817846Sralph 		ASSERT(xfp != NULL, CANTOPEN, xfile, 0);
19913697Ssam 
20013697Ssam 		/*  initialize to default  */
20113697Ssam 		strcpy(user, User);
20217846Sralph 		strcpy(fin, DEVNULL);
20317846Sralph 		strcpy(fout, DEVNULL);
20423692Sbloom 		strcpy(sysout, Myname);
20517846Sralph 		badfiles = 0;
20613697Ssam 		while (fgets(buf, BUFSIZ, xfp) != NULL) {
20713697Ssam 			switch (buf[0]) {
20813697Ssam 			case X_USER:
20917846Sralph 				sscanf(&buf[1], "%s %s", user, Rmtname);
21013697Ssam 				break;
21117846Sralph 			case X_RETURNTO:
21217846Sralph 				sscanf(&buf[1], "%s", user);
21317846Sralph 				break;
21413697Ssam 			case X_STDIN:
21513697Ssam 				sscanf(&buf[1], "%s", fin);
21613697Ssam 				i = expfile(fin);
21713697Ssam 				/* rti!trt: do not check permissions of
21813697Ssam 				 * vanilla spool file */
21913697Ssam 				if (i != 0
22013697Ssam 				 && (chkpth("", "", fin) || anyread(fin) != 0))
22113697Ssam 					badfiles = 1;
22213697Ssam 				break;
22313697Ssam 			case X_STDOUT:
22413697Ssam 				sscanf(&buf[1], "%s%s", fout, sysout);
22523692Sbloom 				sysout[MAXBASENAME] = '\0';
22613697Ssam 				/* rti!trt: do not check permissions of
22713697Ssam 				 * vanilla spool file.  DO check permissions
22813697Ssam 				 * of writing on a non-vanilla file */
22913697Ssam 				i = 1;
23013697Ssam 				if (fout[0] != '~' || prefix(sysout, Myname))
23113697Ssam 					i = expfile(fout);
23213697Ssam 				if (i != 0
23313697Ssam 				 && (chkpth("", "", fout)
23413697Ssam 					|| chkperm(fout, (char *)1)))
23513697Ssam 					badfiles = 1;
23613697Ssam 				break;
23713697Ssam 			case X_CMD:
23813697Ssam 				strcpy(cmd, &buf[2]);
23913697Ssam 				if (*(cmd + strlen(cmd) - 1) == '\n')
24013697Ssam 					*(cmd + strlen(cmd) - 1) = '\0';
24113697Ssam 				break;
24213697Ssam 			case X_NONOTI:
24313697Ssam 				notiok = 0;
24413697Ssam 				break;
24513697Ssam 			case X_NONZERO:
24613697Ssam 				nonzero = 1;
24713697Ssam 				break;
24813697Ssam 			default:
24913697Ssam 				break;
25013697Ssam 			}
25113697Ssam 		}
25213697Ssam 
25313697Ssam 		fclose(xfp);
25413697Ssam 		DEBUG(4, "fin - %s, ", fin);
25513697Ssam 		DEBUG(4, "fout - %s, ", fout);
25613697Ssam 		DEBUG(4, "sysout - %s, ", sysout);
25713697Ssam 		DEBUG(4, "user - %s\n", user);
25813697Ssam 		DEBUG(4, "cmd - %s\n", cmd);
25913697Ssam 
26013697Ssam 		/*  command execution  */
26117846Sralph 		if (strcmp(fout, DEVNULL) == SAME)
26217846Sralph 			strcpy(dfile,DEVNULL);
26313697Ssam 		else
26413697Ssam 			gename(DATAPRE, sysout, 'O', dfile);
26513697Ssam 
26613697Ssam 		/* expand file names where necessary */
26713697Ssam 		expfile(dfile);
26818628Sralph 		cmdp = buf;
26913697Ssam 		ptr = cmd;
27013697Ssam 		xcmd[0] = '\0';
27113697Ssam 		argnok = 0;
27213697Ssam 		while ((ptr = getprm(ptr, prm)) != NULL) {
27313697Ssam 			if (prm[0] == ';' || prm[0] == '^'
27413697Ssam 			  || prm[0] == '&'  || prm[0] == '|') {
27513697Ssam 				xcmd[0] = '\0';
27613697Ssam 				APPCMD(prm);
27713697Ssam 				continue;
27813697Ssam 			}
27913697Ssam 
28017846Sralph 			if ((argnok = argok(xcmd, prm)) != SUCCESS)
28113697Ssam 				/*  command not valid  */
28213697Ssam 				break;
28313697Ssam 
28413697Ssam 			if (prm[0] == '~')
28513697Ssam 				expfile(prm);
28613697Ssam 			APPCMD(prm);
28713697Ssam 		}
28817846Sralph 		/*
28917846Sralph 		 * clean up trailing ' ' in command.
29017846Sralph 		 */
29117846Sralph 		if (cmdp > buf && cmdp[0] == '\0' && cmdp[-1] == ' ')
29217846Sralph 			*--cmdp = '\0';
29313697Ssam 		if (argnok || badfiles) {
29413697Ssam 			sprintf(lbuf, "%s XQT DENIED", user);
29513697Ssam 			logent(cmd, lbuf);
29613697Ssam 			DEBUG(4, "bad command %s\n", prm);
29713697Ssam 			notify(user, Rmtname, cmd, "DENIED");
29813697Ssam 			goto rmfiles;
29913697Ssam 		}
30013697Ssam 		sprintf(lbuf, "%s XQT", user);
30113697Ssam 		logent(buf, lbuf);
30213697Ssam 		DEBUG(4, "cmd %s\n", buf);
30313697Ssam 
30413697Ssam 		mvxfiles(xfile);
30517846Sralph 		ret = subchdir(XQTDIR);
30617846Sralph 		ASSERT(ret >= 0, "CHDIR FAILED", XQTDIR, ret);
30718628Sralph 		ret = shio(buf, fin, dfile);
30813697Ssam 		sprintf(retstat, "signal %d, exit %d", ret & 0377,
30913697Ssam 		  (ret>>8) & 0377);
31013697Ssam 		if (strcmp(xcmd, "rmail") == SAME)
31113697Ssam 			notiok = 0;
31213697Ssam 		if (strcmp(xcmd, "rnews") == SAME)
31313697Ssam 			nonzero = 1;
31417846Sralph 		notiflg = chknotify(xcmd);
31517846Sralph 		if (notiok && notiflg != NT_NO &&
31617846Sralph 		   (ret != 0 || (!nonzero && notiflg == NT_YES)))
31713697Ssam 			notify(user, Rmtname, cmd, retstat);
31813697Ssam 		else if (ret != 0 && strcmp(xcmd, "rmail") == SAME) {
31913697Ssam 			/* mail failed - return letter to sender  */
32017846Sralph #ifdef	DANGEROUS
32117846Sralph 			/* NOT GUARANTEED SAFE!!! */
32217846Sralph 			if (!nonzero)
32317846Sralph 				retosndr(user, Rmtname, fin);
32417846Sralph #else
32517846Sralph 			notify(user, Rmtname, cmd, retstat);
32617846Sralph #endif
32717846Sralph 			sprintf(buf, "%s (%s) from %s!%s", buf, retstat, Rmtname, user);
32813697Ssam 			logent("MAIL FAIL", buf);
32913697Ssam 		}
33013697Ssam 		DEBUG(4, "exit cmd - %d\n", ret);
33117846Sralph 		ret2 = subchdir(Spool);
33217846Sralph 		ASSERT(ret2 >= 0, "CHDIR FAILED", Spool, ret);
33313697Ssam 		rmxfiles(xfile);
33413697Ssam 		if (ret != 0) {
33513697Ssam 			/*  exit status not zero */
33613697Ssam 			dfp = fopen(subfile(dfile), "a");
33717846Sralph 			ASSERT(dfp != NULL, CANTOPEN, dfile, 0);
33813697Ssam 			fprintf(dfp, "exit status %d", ret);
33913697Ssam 			fclose(dfp);
34013697Ssam 		}
34117846Sralph 		if (strcmp(fout, DEVNULL) != SAME) {
34213697Ssam 			if (prefix(sysout, Myname)) {
34313697Ssam 				xmv(dfile, fout);
34413697Ssam 				chmod(fout, BASEMODE);
34518628Sralph 			} else {
34617846Sralph 				char *cp = rindex(user, '!');
34713697Ssam 				gename(CMDPRE, sysout, 'O', cfile);
34813697Ssam 				fp = fopen(subfile(cfile), "w");
34913697Ssam 				ASSERT(fp != NULL, "OPEN", cfile, 0);
35017846Sralph 				fprintf(fp, "S %s %s %s - %s 0666\n", dfile,
35117846Sralph 					fout, cp ? cp : user, lastpart(dfile));
35213697Ssam 				fclose(fp);
35313697Ssam 			}
35413697Ssam 		}
35513697Ssam 	rmfiles:
35613697Ssam 		xfp = fopen(subfile(xfile), "r");
35717846Sralph 		ASSERT(xfp != NULL, CANTOPEN, xfile, 0);
35813697Ssam 		while (fgets(buf, BUFSIZ, xfp) != NULL) {
35913697Ssam 			if (buf[0] != X_RQDFILE)
36013697Ssam 				continue;
36113697Ssam 			sscanf(&buf[1], "%s", file);
36213697Ssam 			unlink(subfile(file));
36313697Ssam 		}
36413697Ssam 		unlink(subfile(xfile));
36513697Ssam 		fclose(xfp);
36618628Sralph 
36718628Sralph 		/* rescan X. for new work every RECHECKTIME seconds */
36818628Sralph 		time(&xnow);
36918628Sralph 		if (xnow > (xstart + RECHECKTIME)) {
37018628Sralph 			extern int Nfiles;
37118628Sralph 			Nfiles = 0; 	/*force rescan for new work */
37218628Sralph 		}
37318628Sralph 		xstart = xnow;
37413697Ssam 	}
37513697Ssam 
37613697Ssam 	if (stcico)
37713697Ssam 		xuucico("");
37813697Ssam 	cleanup(0);
37913697Ssam }
38013697Ssam 
38113697Ssam 
38213697Ssam cleanup(code)
38313697Ssam int code;
38413697Ssam {
38513697Ssam 	logcls();
38613697Ssam 	rmlock(CNULL);
38717846Sralph #ifdef	VMS
38817846Sralph 	/*
38917846Sralph 	 *	Since we run as a BATCH job we must wait for all processes to
39017846Sralph 	 *	to finish
39117846Sralph 	 */
39218628Sralph 	while(wait(0) != -1)
39318628Sralph 		;
39417846Sralph #endif VMS
39513697Ssam 	exit(code);
39613697Ssam }
39713697Ssam 
39813697Ssam 
39918628Sralph /*
40018628Sralph  *	get a file to execute
40113697Ssam  *
40213697Ssam  *	return codes:  0 - no file  |  1 - file to execute
40313697Ssam  */
40413697Ssam 
40513697Ssam gtxfile(file)
40613697Ssam register char *file;
40713697Ssam {
40813697Ssam 	char pre[3];
40917846Sralph 	int rechecked;
41017846Sralph 	time_t ystrdy;		/* yesterday */
41117846Sralph 	struct stat stbuf;	/* for X file age */
41213697Ssam 
41313697Ssam 	pre[0] = XQTPRE;
41413697Ssam 	pre[1] = '.';
41513697Ssam 	pre[2] = '\0';
41613697Ssam 	rechecked = 0;
41713697Ssam retry:
41813697Ssam 	if (!gtwrkf(Spool, file)) {
41913697Ssam 		if (rechecked)
42017846Sralph 			return 0;
42113697Ssam 		rechecked = 1;
42217846Sralph 		DEBUG(4, "iswrk\n", CNULL);
42313697Ssam 		if (!iswrk(file, "get", Spool, pre))
42417846Sralph 			return 0;
42513697Ssam 	}
42613697Ssam 	DEBUG(4, "file - %s\n", file);
42713697Ssam 	/* skip spurious subdirectories */
42813697Ssam 	if (strcmp(pre, file) == SAME)
42913697Ssam 		goto retry;
43013697Ssam 	if (gotfiles(file))
43117846Sralph 		return 1;
43217846Sralph 	/* check for old X. file with no work files and remove them. */
43317846Sralph 	if (Nfiles > LLEN/2) {
43417846Sralph 	    time(&ystrdy);
43517846Sralph 	    ystrdy -= (4 * 3600L);		/* 4 hours ago */
43617846Sralph 	    DEBUG(4, "gtxfile: Nfiles > LLEN/2\n", CNULL);
43717846Sralph 	    while (gtwrkf(Spool, file) && !gotfiles(file)) {
43817846Sralph 		if (stat(subfile(file), &stbuf) == 0)
43917846Sralph 		    if (stbuf.st_mtime <= ystrdy) {
44017846Sralph 			char *bnp, cfilename[NAMESIZE];
44117846Sralph 			DEBUG(4, "gtxfile: move %s to CORRUPT \n", file);
44217846Sralph 			unlink(subfile(file));
44317846Sralph 			bnp = rindex(subfile(file), '/');
44417846Sralph 			sprintf(cfilename, "%s/%s", CORRUPT,
44517846Sralph 				bnp ? bnp + 1 : subfile(file));
44617846Sralph 			xmv(subfile(file), cfilename);
44717846Sralph 			logent(file, "X. FILE CORRUPTED");
44817846Sralph 		    }
44917846Sralph 	    }
45017846Sralph 	    DEBUG(4, "iswrk\n", CNULL);
45117846Sralph 	    if (!iswrk(file, "get", Spool, pre))
45217846Sralph 		return 0;
45317846Sralph 	}
45413697Ssam 	goto retry;
45513697Ssam }
45613697Ssam 
45718628Sralph /*
45818628Sralph  *	check for needed files
45913697Ssam  *
46013697Ssam  *	return codes:  0 - not ready  |  1 - all files ready
46113697Ssam  */
46213697Ssam 
46313697Ssam gotfiles(file)
46413697Ssam register char *file;
46513697Ssam {
46613697Ssam 	struct stat stbuf;
46713697Ssam 	register FILE *fp;
46813697Ssam 	char buf[BUFSIZ], rqfile[MAXFULLNAME];
46913697Ssam 
47013697Ssam 	fp = fopen(subfile(file), "r");
47113697Ssam 	if (fp == NULL)
47217846Sralph 		return 0;
47313697Ssam 
47413697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
47513697Ssam 		DEBUG(4, "%s\n", buf);
47613697Ssam 		if (buf[0] != X_RQDFILE)
47713697Ssam 			continue;
47813697Ssam 		sscanf(&buf[1], "%s", rqfile);
47913697Ssam 		expfile(rqfile);
48013697Ssam 		if (stat(subfile(rqfile), &stbuf) == -1) {
48113697Ssam 			fclose(fp);
48217846Sralph 			return 0;
48313697Ssam 		}
48413697Ssam 	}
48513697Ssam 
48613697Ssam 	fclose(fp);
48717846Sralph 	return 1;
48813697Ssam }
48913697Ssam 
49013697Ssam 
49118628Sralph /*
49218628Sralph  *	remove execute files to x-directory
49313697Ssam  */
49413697Ssam 
49513697Ssam rmxfiles(xfile)
49613697Ssam register char *xfile;
49713697Ssam {
49813697Ssam 	register FILE *fp;
49913697Ssam 	char buf[BUFSIZ], file[NAMESIZE], tfile[NAMESIZE];
50013697Ssam 	char tfull[MAXFULLNAME];
50113697Ssam 
50213697Ssam 	if((fp = fopen(subfile(xfile), "r")) == NULL)
50313697Ssam 		return;
50413697Ssam 
50513697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
50613697Ssam 		if (buf[0] != X_RQDFILE)
50713697Ssam 			continue;
50813697Ssam 		if (sscanf(&buf[1], "%s%s", file, tfile) < 2)
50913697Ssam 			continue;
51013697Ssam 		sprintf(tfull, "%s/%s", XQTDIR, tfile);
51113697Ssam 		unlink(subfile(tfull));
51213697Ssam 	}
51313697Ssam 	fclose(fp);
51413697Ssam 	return;
51513697Ssam }
51613697Ssam 
51713697Ssam 
51818628Sralph /*
51918628Sralph  *	move execute files to x-directory
52013697Ssam  */
52113697Ssam 
52213697Ssam mvxfiles(xfile)
52313697Ssam char *xfile;
52413697Ssam {
52513697Ssam 	register FILE *fp;
52613697Ssam 	char buf[BUFSIZ], ffile[MAXFULLNAME], tfile[NAMESIZE];
52713697Ssam 	char tfull[MAXFULLNAME];
52813697Ssam 	int ret;
52913697Ssam 
53013697Ssam 	if((fp = fopen(subfile(xfile), "r")) == NULL)
53113697Ssam 		return;
53213697Ssam 
53313697Ssam 	while (fgets(buf, BUFSIZ, fp) != NULL) {
53413697Ssam 		if (buf[0] != X_RQDFILE)
53513697Ssam 			continue;
53613697Ssam 		if (sscanf(&buf[1], "%s%s", ffile, tfile) < 2)
53713697Ssam 			continue;
53813697Ssam 		expfile(ffile);
53913697Ssam 		sprintf(tfull, "%s/%s", XQTDIR, tfile);
54013697Ssam 		unlink(subfile(tfull));
54113697Ssam 		ret = xmv(ffile, tfull);
54217846Sralph 		ASSERT(ret == 0, "XQTDIR ERROR", CNULL, ret);
54313697Ssam 	}
54413697Ssam 	fclose(fp);
54513697Ssam }
54613697Ssam 
54718628Sralph /*
54818628Sralph  *	check for valid command/argument
54918628Sralph  *	*NOTE - side effect is to set xc to the	command to be executed.
55013697Ssam  *
55113697Ssam  *	return 0 - ok | 1 nok
55213697Ssam  */
55313697Ssam 
55413697Ssam argok(xc, cmd)
55513697Ssam register char *xc, *cmd;
55613697Ssam {
55713697Ssam 	register char **ptr;
55813697Ssam 
55913697Ssam #ifndef ALLOK
56018628Sralph 	if (strpbrk(cmd, BADCHARS) != NULL) {
56117846Sralph 		DEBUG(1,"MAGIC CHARACTER FOUND\n", CNULL);
56218628Sralph 		logent(cmd, "NASTY MAGIC CHARACTER FOUND");
56317846Sralph 		return FAIL;
56417846Sralph 	}
56517846Sralph #endif !ALLOK
56613697Ssam 
56713697Ssam 	if (xc[0] != '\0')
56817846Sralph 		return SUCCESS;
56913697Ssam 
57013697Ssam #ifndef ALLOK
57113697Ssam 	ptr = Cmds;
57217846Sralph 	DEBUG(9, "Compare %s and\n", cmd);
57313697Ssam 	while(*ptr != NULL) {
57417846Sralph 		DEBUG(9, "\t%s\n", *ptr);
57513697Ssam 		if (strcmp(cmd, *ptr) == SAME)
57613697Ssam 			break;
57717846Sralph 		ptr++;
57813697Ssam 	}
57917846Sralph 	if (*ptr == NULL) {
58017846Sralph 		DEBUG(1,"COMMAND NOT FOUND\n", CNULL);
58117846Sralph 		return FAIL;
58217846Sralph 	}
58313697Ssam #endif
58413697Ssam 	strcpy(xc, cmd);
58517846Sralph 	DEBUG(9, "MATCHED %s\n", xc);
58617846Sralph 	return SUCCESS;
58713697Ssam }
58813697Ssam 
58913697Ssam 
59018628Sralph /*
59118628Sralph  *	if notification should be sent for successful execution of cmd
59217846Sralph  *
59317846Sralph  *	return NT_YES - do notification
59417846Sralph  *	       NT_ERR - do notification if exit status != 0
59517846Sralph  *	       NT_NO  - don't do notification ever
59617846Sralph  */
59717846Sralph 
59817846Sralph chknotify(cmd)
59917846Sralph char *cmd;
60017846Sralph {
60117846Sralph 	register char **ptr;
60217846Sralph 	register int *nptr;
60317846Sralph 
60417846Sralph 	ptr = Cmds;
60517846Sralph 	nptr = Notify;
60617846Sralph 	while (*ptr != NULL) {
60717846Sralph 		if (strcmp(cmd, *ptr) == SAME)
60817846Sralph 			return *nptr;
60917846Sralph 		ptr++;
61017846Sralph 		nptr++;
61117846Sralph 	}
61217846Sralph 	return NT_YES;		/* "shouldn't happen" */
61317846Sralph }
61417846Sralph 
61517846Sralph 
61617846Sralph 
61718628Sralph /*
61818628Sralph  *	send mail to user giving execution results
61913697Ssam  */
62013697Ssam 
62113697Ssam notify(user, rmt, cmd, str)
62213697Ssam char *user, *rmt, *cmd, *str;
62313697Ssam {
62413697Ssam 	char text[MAXFULLNAME];
62513697Ssam 	char ruser[MAXFULLNAME];
62613697Ssam 
627*25968Sbloom 	if (strpbrk(user, BADCHARS) != NULL) {
628*25968Sbloom 		char lbuf[MAXFULLNAME];
629*25968Sbloom 		sprintf(lbuf, "%s INVALID CHARACTER IN USERNAME", user);
630*25968Sbloom 		logent(cmd, lbuf);
631*25968Sbloom 		strcpy(user, "postmaster");
632*25968Sbloom 	}
63317846Sralph 	sprintf(text, "uuxqt cmd (%s) status (%s)", cmd, str);
63413697Ssam 	if (prefix(rmt, Myname))
63513697Ssam 		strcpy(ruser, user);
63613697Ssam 	else
63713697Ssam 		sprintf(ruser, "%s!%s", rmt, user);
63817846Sralph 	mailst(ruser, text, CNULL);
63913697Ssam }
64013697Ssam 
64118628Sralph /*
64218628Sralph  *	return mail to sender
64313697Ssam  *
64413697Ssam  */
64513697Ssam retosndr(user, rmt, file)
64613697Ssam char *user, *rmt, *file;
64713697Ssam {
64817846Sralph 	char ruser[MAXFULLNAME];
64913697Ssam 
650*25968Sbloom 	if (strpbrk(user, BADCHARS) != NULL) {
651*25968Sbloom 		char lbuf[MAXFULLNAME];
652*25968Sbloom 		sprintf(lbuf, "%s INVALID CHARACTER IN USERNAME", user);
653*25968Sbloom 		logent(file, lbuf);
654*25968Sbloom 		strcpy(user, "postmaster");
655*25968Sbloom 	}
65613697Ssam 	if (strcmp(rmt, Myname) == SAME)
65713697Ssam 		strcpy(ruser, user);
65813697Ssam 	else
65913697Ssam 		sprintf(ruser, "%s!%s", rmt, user);
66013697Ssam 
66113697Ssam 	if (anyread(file) == 0)
66213697Ssam 		mailst(ruser, "Mail failed.  Letter returned to sender.\n", file);
66313697Ssam 	else
66417846Sralph 		mailst(ruser, "Mail failed.  Letter returned to sender.\n", CNULL);
66513697Ssam 	return;
66613697Ssam }
66717870Sralph 
66817870Sralph /*
66918628Sralph  *	execute shell of command with fi and fo as standard input/output
67018628Sralph  */
67118628Sralph 
67218628Sralph shio(cmd, fi, fo)
67318628Sralph char *cmd, *fi, *fo;
67418628Sralph {
67518628Sralph 	int status, f;
67618628Sralph 	int uid, pid, ret;
67718628Sralph 	char path[MAXFULLNAME];
67818628Sralph 	char *args[20];
67918628Sralph 	extern int errno;
68018628Sralph 
68118628Sralph 	if (fi == NULL)
68218628Sralph 		fi = DEVNULL;
68318628Sralph 	if (fo == NULL)
68418628Sralph 		fo = DEVNULL;
68518628Sralph 
68618628Sralph 	getargs(cmd, args, 20);
68718628Sralph 	DEBUG(3, "shio - %s\n", cmd);
68818628Sralph #ifdef SIGCHLD
68918628Sralph 	signal(SIGCHLD, SIG_IGN);
69018628Sralph #endif SIGCHLD
69118628Sralph 	if ((pid = fork()) == 0) {
69218628Sralph 		signal(SIGINT, SIG_IGN);
69318628Sralph 		signal(SIGHUP, SIG_IGN);
69418628Sralph 		signal(SIGQUIT, SIG_IGN);
69518628Sralph 		close(Ifn);
69618628Sralph 		close(Ofn);
69718628Sralph 		close(0);
69818628Sralph 		setuid(getuid());
69918628Sralph 		f = open(subfile(fi), 0);
70018628Sralph 		if (f != 0) {
70118628Sralph 			logent(fi, "CAN'T READ");
70218628Sralph 			exit(-errno);
70318628Sralph 		}
70418628Sralph 		close(1);
70518628Sralph 		f = creat(subfile(fo), 0666);
70618628Sralph 		if (f != 1) {
70718628Sralph 			logent(fo, "CAN'T WRITE");
70818628Sralph 			exit(-errno);
70918628Sralph 		}
71018628Sralph 		execvp(args[0], args);
71118628Sralph 		exit(100+errno);
71218628Sralph 	}
71318628Sralph 	while ((ret = wait(&status)) != pid && ret != -1)
71418628Sralph 		;
71518628Sralph 	DEBUG(3, "status %d\n", status);
71618628Sralph 	return status;
71718628Sralph }
718