xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 53182)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*53182Seric static char sccsid[] = "@(#)queue.c	5.38 (Berkeley) 04/16/92 (with queueing)";
1433731Sbostic #else
15*53182Seric static char sccsid[] = "@(#)queue.c	5.38 (Berkeley) 04/16/92 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <sys/stat.h>
2013707Ssam # include <sys/dir.h>
2140934Srick # include <sys/file.h>
224634Seric # include <signal.h>
234632Seric # include <errno.h>
2440973Sbostic # include <pwd.h>
2551937Seric # ifdef LOCKF
2651937Seric # include <fcntl.h>
2751937Seric # endif
284632Seric 
2933731Sbostic # ifdef QUEUE
304632Seric 
314632Seric /*
329377Seric **  Work queue.
339377Seric */
349377Seric 
359377Seric struct work
369377Seric {
379377Seric 	char		*w_name;	/* name of control file */
389377Seric 	long		w_pri;		/* priority of message, see below */
3925013Seric 	time_t		w_ctime;	/* creation time of message */
409377Seric 	struct work	*w_next;	/* next in queue */
419377Seric };
429377Seric 
439377Seric typedef struct work	WORK;
449377Seric 
459377Seric WORK	*WorkQ;			/* queue of things to be done */
469377Seric /*
474632Seric **  QUEUEUP -- queue a message up for future transmission.
484632Seric **
494632Seric **	Parameters:
506980Seric **		e -- the envelope to queue up.
516999Seric **		queueall -- if TRUE, queue all addresses, rather than
526999Seric **			just those with the QQUEUEUP flag set.
539377Seric **		announce -- if TRUE, tell when you are queueing up.
544632Seric **
554632Seric **	Returns:
5651920Seric **		none.
574632Seric **
584632Seric **	Side Effects:
599377Seric **		The current request are saved in a control file.
6051920Seric **		The queue file is left locked.
614632Seric */
624632Seric 
639377Seric queueup(e, queueall, announce)
646980Seric 	register ENVELOPE *e;
656999Seric 	bool queueall;
669377Seric 	bool announce;
674632Seric {
687812Seric 	char *qf;
697812Seric 	register FILE *tfp;
704632Seric 	register HDR *h;
715007Seric 	register ADDRESS *q;
7251920Seric 	int fd;
7351920Seric 	int i;
7451920Seric 	bool newid;
7510173Seric 	MAILER nullmailer;
7651920Seric 	char buf[MAXLINE], tf[MAXLINE];
774632Seric 
785037Seric 	/*
7917477Seric 	**  Create control file.
805037Seric 	*/
814632Seric 
8251920Seric 	newid = (e->e_id == NULL);
8351920Seric 	strcpy(tf, queuename(e, 't'));
8451920Seric 	tfp = e->e_lockfp;
8551920Seric 	if (tfp == NULL)
8651920Seric 		newid = FALSE;
8751920Seric 	if (newid)
8851835Seric 	{
8951920Seric 		tfp = e->e_lockfp;
9051920Seric 	}
9151920Seric 	else
9251920Seric 	{
9351920Seric 		/* get a locked tf file */
9451920Seric 		for (i = 100; --i >= 0; )
9551835Seric 		{
9651937Seric # ifdef LOCKF
9751937Seric 			struct flock lfd;
9851937Seric # endif
9951937Seric 
10051920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10151920Seric 			if (fd < 0)
10251835Seric 			{
10351920Seric 				if (errno == EEXIST)
10451920Seric 					continue;
10551920Seric 				syserr("queueup: cannot create temp file %s", tf);
10651920Seric 				return;
10741636Srick 			}
10851835Seric # ifdef LOCKF
10951937Seric 			lfd.l_type = F_WRLCK;
11051937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
11151937Seric 			if (fcntl(fd, F_SETLK, &lfd) >= 0)
11251920Seric 				break;
11351920Seric 			if (errno != EACCES && errno != EAGAIN)
11451920Seric 				syserr("cannot lockf(%s)", tf);
11551835Seric # else
11651920Seric 			if (flock(fd, LOCK_EX|LOCK_NB) >= 0)
11751920Seric 				break;
11851920Seric 			if (errno != EWOULDBLOCK)
11951920Seric 				syserr("cannot flock(%s)", tf);
12051835Seric # endif
12151920Seric 			close(fd);
12241636Srick 		}
12341636Srick 
12451920Seric 		tfp = fdopen(fd, "w");
12551920Seric 	}
1264632Seric 
1277677Seric 	if (tTd(40, 1))
12817468Seric 		printf("queueing %s\n", e->e_id);
1294632Seric 
1304632Seric 	/*
1316980Seric 	**  If there is no data file yet, create one.
1326980Seric 	*/
1336980Seric 
1346980Seric 	if (e->e_df == NULL)
1356980Seric 	{
1366980Seric 		register FILE *dfp;
1379389Seric 		extern putbody();
1386980Seric 
1397812Seric 		e->e_df = newstr(queuename(e, 'd'));
14040934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14140934Srick 		if (fd < 0)
1426980Seric 		{
1436980Seric 			syserr("queueup: cannot create %s", e->e_df);
14451920Seric 			if (!newid)
14551920Seric 				(void) fclose(tfp);
14651920Seric 			return;
1476980Seric 		}
14840934Srick 		dfp = fdopen(fd, "w");
14910173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1507009Seric 		(void) fclose(dfp);
1519389Seric 		e->e_putbody = putbody;
1526980Seric 	}
1536980Seric 
1546980Seric 	/*
1554632Seric 	**  Output future work requests.
15625687Seric 	**	Priority and creation time should be first, since
15725687Seric 	**	they are required by orderq.
1584632Seric 	*/
1594632Seric 
1609377Seric 	/* output message priority */
1619377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1629377Seric 
1639630Seric 	/* output creation time */
1649630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1659630Seric 
1664632Seric 	/* output name of data file */
1677812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1684632Seric 
16910108Seric 	/* message from envelope, if it exists */
17010108Seric 	if (e->e_message != NULL)
17110108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17210108Seric 
1734632Seric 	/* output name of sender */
1747812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1754632Seric 
1764632Seric 	/* output list of recipient addresses */
1776980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1784632Seric 	{
17947284Seric 		if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) :
1807763Seric 			       bitset(QQUEUEUP, q->q_flags))
1818245Seric 		{
18240973Sbostic 			char *ctluser, *getctluser();
18340973Sbostic 
18440973Sbostic 			if ((ctluser = getctluser(q)) != NULL)
18540973Sbostic 				fprintf(tfp, "C%s\n", ctluser);
1867812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1879377Seric 			if (announce)
1889377Seric 			{
1899377Seric 				e->e_to = q->q_paddr;
1909377Seric 				message(Arpa_Info, "queued");
1919377Seric 				if (LogLevel > 4)
1929377Seric 					logdelivery("queued");
1939377Seric 				e->e_to = NULL;
1949377Seric 			}
1959387Seric 			if (tTd(40, 1))
1969387Seric 			{
1979387Seric 				printf("queueing ");
1989387Seric 				printaddr(q, FALSE);
1999387Seric 			}
2008245Seric 		}
2014632Seric 	}
2024632Seric 
20325687Seric 	/* output list of error recipients */
20425687Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
20525687Seric 	{
20626504Seric 		if (!bitset(QDONTSEND, q->q_flags))
20740973Sbostic 		{
20840973Sbostic 			char *ctluser, *getctluser();
20940973Sbostic 
21040973Sbostic 			if ((ctluser = getctluser(q)) != NULL)
21140973Sbostic 				fprintf(tfp, "C%s\n", ctluser);
21226504Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
21340973Sbostic 		}
21425687Seric 	}
21525687Seric 
2169377Seric 	/*
2179377Seric 	**  Output headers for this message.
2189377Seric 	**	Expand macros completely here.  Queue run will deal with
2199377Seric 	**	everything as absolute headers.
2209377Seric 	**		All headers that must be relative to the recipient
2219377Seric 	**		can be cracked later.
22210173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
22310173Seric 	**	no effect on the addresses as they are output.
2249377Seric 	*/
2259377Seric 
22610686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
22710173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
22810349Seric 	nullmailer.m_eol = "\n";
22910173Seric 
23016147Seric 	define('g', "\001f", e);
2316980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2324632Seric 	{
23310686Seric 		extern bool bitzerop();
23410686Seric 
23512015Seric 		/* don't output null headers */
2364632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2374632Seric 			continue;
23812015Seric 
23912015Seric 		/* don't output resent headers on non-resent messages */
24012015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
24112015Seric 			continue;
24212015Seric 
24312015Seric 		/* output this header */
2447812Seric 		fprintf(tfp, "H");
24512015Seric 
24612015Seric 		/* if conditional, output the set of conditions */
24710686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
24810686Seric 		{
24910686Seric 			int j;
25010686Seric 
25123098Seric 			(void) putc('?', tfp);
25210686Seric 			for (j = '\0'; j <= '\177'; j++)
25310686Seric 				if (bitnset(j, h->h_mflags))
25423098Seric 					(void) putc(j, tfp);
25523098Seric 			(void) putc('?', tfp);
25610686Seric 		}
25712015Seric 
25812015Seric 		/* output the header: expand macros, convert addresses */
2597763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2607763Seric 		{
2617763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2628236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2637763Seric 		}
2648245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2659348Seric 		{
2669348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
26710173Seric 				 &nullmailer);
2689348Seric 		}
2697763Seric 		else
2708245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2714632Seric 	}
2724632Seric 
2734632Seric 	/*
2744632Seric 	**  Clean up.
2754632Seric 	*/
2764632Seric 
27751920Seric 	if (!newid)
27851920Seric 	{
27951920Seric 		qf = queuename(e, 'q');
28051920Seric 		if (rename(tf, qf) < 0)
28151920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
28251920Seric 		if (e->e_lockfp != NULL)
28351920Seric 			(void) fclose(e->e_lockfp);
28451920Seric 		e->e_lockfp = tfp;
28551920Seric 	}
28651920Seric 	else
28751920Seric 		qf = tf;
28841636Srick 	errno = 0;
2897391Seric 
2907677Seric # ifdef LOG
2917677Seric 	/* save log info */
2927878Seric 	if (LogLevel > 15)
2937878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
2947677Seric # endif LOG
29540934Srick 	fflush(tfp);
29651920Seric 	return;
2974632Seric }
2984632Seric /*
2994632Seric **  RUNQUEUE -- run the jobs in the queue.
3004632Seric **
3014632Seric **	Gets the stuff out of the queue in some presumably logical
3024632Seric **	order and processes them.
3034632Seric **
3044632Seric **	Parameters:
30524941Seric **		forkflag -- TRUE if the queue scanning should be done in
30624941Seric **			a child process.  We double-fork so it is not our
30724941Seric **			child and we don't have to clean up after it.
3084632Seric **
3094632Seric **	Returns:
3104632Seric **		none.
3114632Seric **
3124632Seric **	Side Effects:
3134632Seric **		runs things in the mail queue.
3144632Seric */
3154632Seric 
3164639Seric runqueue(forkflag)
3174639Seric 	bool forkflag;
3184632Seric {
31924953Seric 	extern bool shouldqueue();
32024953Seric 
3217466Seric 	/*
32224953Seric 	**  If no work will ever be selected, don't even bother reading
32324953Seric 	**  the queue.
32424953Seric 	*/
32524953Seric 
32651920Seric 	CurrentLA = getla();	/* get load average */
32740934Srick 
32824953Seric 	if (shouldqueue(-100000000L))
32924953Seric 	{
33024953Seric 		if (Verbose)
33124953Seric 			printf("Skipping queue run -- load average too high\n");
33224953Seric 
33324953Seric 		if (forkflag)
33424953Seric 			return;
33524953Seric 		finis();
33624953Seric 	}
33724953Seric 
33824953Seric 	/*
3397466Seric 	**  See if we want to go off and do other useful work.
3407466Seric 	*/
3414639Seric 
3424639Seric 	if (forkflag)
3434639Seric 	{
3447943Seric 		int pid;
3457943Seric 
3467943Seric 		pid = dofork();
3477943Seric 		if (pid != 0)
3484639Seric 		{
34946928Sbostic 			extern void reapchild();
35025184Seric 
3517943Seric 			/* parent -- pick up intermediate zombie */
35225184Seric #ifndef SIGCHLD
3539377Seric 			(void) waitfor(pid);
35425184Seric #else SIGCHLD
35525184Seric 			(void) signal(SIGCHLD, reapchild);
35625184Seric #endif SIGCHLD
3577690Seric 			if (QueueIntvl != 0)
3589348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3594639Seric 			return;
3604639Seric 		}
3617943Seric 		/* child -- double fork */
36225184Seric #ifndef SIGCHLD
3637943Seric 		if (fork() != 0)
3647943Seric 			exit(EX_OK);
36525184Seric #else SIGCHLD
36625184Seric 		(void) signal(SIGCHLD, SIG_DFL);
36725184Seric #endif SIGCHLD
3684639Seric 	}
36924941Seric 
37040934Srick 	setproctitle("running queue: %s", QueueDir);
37124941Seric 
3727876Seric # ifdef LOG
3737876Seric 	if (LogLevel > 11)
3747943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
3757876Seric # endif LOG
3764639Seric 
3777466Seric 	/*
37810205Seric 	**  Release any resources used by the daemon code.
37910205Seric 	*/
38010205Seric 
38110205Seric # ifdef DAEMON
38210205Seric 	clrdaemon();
38310205Seric # endif DAEMON
38410205Seric 
38510205Seric 	/*
38627175Seric 	**  Make sure the alias database is open.
38727175Seric 	*/
38827175Seric 
38927175Seric 	initaliases(AliasFile, FALSE);
39027175Seric 
39127175Seric 	/*
3927466Seric 	**  Start making passes through the queue.
3937466Seric 	**	First, read and sort the entire queue.
3947466Seric 	**	Then, process the work in that order.
3957466Seric 	**		But if you take too long, start over.
3967466Seric 	*/
3977466Seric 
3987943Seric 	/* order the existing work requests */
39924954Seric 	(void) orderq(FALSE);
4007690Seric 
4017943Seric 	/* process them once at a time */
4027943Seric 	while (WorkQ != NULL)
4034639Seric 	{
4047943Seric 		WORK *w = WorkQ;
4057881Seric 
4067943Seric 		WorkQ = WorkQ->w_next;
4077943Seric 		dowork(w);
4087943Seric 		free(w->w_name);
4097943Seric 		free((char *) w);
4104639Seric 	}
41129866Seric 
41229866Seric 	/* exit without the usual cleanup */
41329866Seric 	exit(ExitStat);
4144634Seric }
4154634Seric /*
4164632Seric **  ORDERQ -- order the work queue.
4174632Seric **
4184632Seric **	Parameters:
41924941Seric **		doall -- if set, include everything in the queue (even
42024941Seric **			the jobs that cannot be run because the load
42124941Seric **			average is too high).  Otherwise, exclude those
42224941Seric **			jobs.
4234632Seric **
4244632Seric **	Returns:
42510121Seric **		The number of request in the queue (not necessarily
42610121Seric **		the number of requests in WorkQ however).
4274632Seric **
4284632Seric **	Side Effects:
4294632Seric **		Sets WorkQ to the queue of available work, in order.
4304632Seric */
4314632Seric 
43225687Seric # define NEED_P		001
43325687Seric # define NEED_T		002
4344632Seric 
43524941Seric orderq(doall)
43624941Seric 	bool doall;
4374632Seric {
4386625Sglickman 	register struct direct *d;
4394632Seric 	register WORK *w;
4406625Sglickman 	DIR *f;
4414632Seric 	register int i;
44225687Seric 	WORK wlist[QUEUESIZE+1];
44310070Seric 	int wn = -1;
4444632Seric 	extern workcmpf();
4454632Seric 
4464632Seric 	/* clear out old WorkQ */
4474632Seric 	for (w = WorkQ; w != NULL; )
4484632Seric 	{
4494632Seric 		register WORK *nw = w->w_next;
4504632Seric 
4514632Seric 		WorkQ = nw;
4524632Seric 		free(w->w_name);
4534632Seric 		free((char *) w);
4544632Seric 		w = nw;
4554632Seric 	}
4564632Seric 
4574632Seric 	/* open the queue directory */
4588148Seric 	f = opendir(".");
4594632Seric 	if (f == NULL)
4604632Seric 	{
4618148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
46210070Seric 		return (0);
4634632Seric 	}
4644632Seric 
4654632Seric 	/*
4664632Seric 	**  Read the work directory.
4674632Seric 	*/
4684632Seric 
46910070Seric 	while ((d = readdir(f)) != NULL)
4704632Seric 	{
4719377Seric 		FILE *cf;
4724632Seric 		char lbuf[MAXNAME];
4734632Seric 
4744632Seric 		/* is this an interesting entry? */
4757812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
4764632Seric 			continue;
4774632Seric 
47810070Seric 		/* yes -- open control file (if not too many files) */
47925687Seric 		if (++wn >= QUEUESIZE)
48010070Seric 			continue;
4818148Seric 		cf = fopen(d->d_name, "r");
4824632Seric 		if (cf == NULL)
4834632Seric 		{
4847055Seric 			/* this may be some random person sending hir msgs */
4857055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
48610090Seric 			if (tTd(41, 2))
48710090Seric 				printf("orderq: cannot open %s (%d)\n",
48810090Seric 					d->d_name, errno);
4897055Seric 			errno = 0;
49010090Seric 			wn--;
4914632Seric 			continue;
4924632Seric 		}
49325687Seric 		w = &wlist[wn];
49425687Seric 		w->w_name = newstr(d->d_name);
4954632Seric 
49625027Seric 		/* make sure jobs in creation don't clog queue */
49725687Seric 		w->w_pri = 0x7fffffff;
49825687Seric 		w->w_ctime = 0;
49925027Seric 
5004632Seric 		/* extract useful information */
50125687Seric 		i = NEED_P | NEED_T;
50225687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5034632Seric 		{
50424954Seric 			extern long atol();
50524954Seric 
50624941Seric 			switch (lbuf[0])
5074632Seric 			{
50824941Seric 			  case 'P':
50925687Seric 				w->w_pri = atol(&lbuf[1]);
51025687Seric 				i &= ~NEED_P;
5114632Seric 				break;
51225013Seric 
51325013Seric 			  case 'T':
51425687Seric 				w->w_ctime = atol(&lbuf[1]);
51525687Seric 				i &= ~NEED_T;
51625013Seric 				break;
5174632Seric 			}
5184632Seric 		}
5194632Seric 		(void) fclose(cf);
52024953Seric 
52125687Seric 		if (!doall && shouldqueue(w->w_pri))
52224953Seric 		{
52324953Seric 			/* don't even bother sorting this job in */
52424953Seric 			wn--;
52524953Seric 		}
5264632Seric 	}
5276625Sglickman 	(void) closedir(f);
52810090Seric 	wn++;
5294632Seric 
5304632Seric 	/*
5314632Seric 	**  Sort the work directory.
5324632Seric 	*/
5334632Seric 
53425687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
5354632Seric 
5364632Seric 	/*
5374632Seric 	**  Convert the work list into canonical form.
5389377Seric 	**	Should be turning it into a list of envelopes here perhaps.
5394632Seric 	*/
5404632Seric 
54124981Seric 	WorkQ = NULL;
54225687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
5434632Seric 	{
5444632Seric 		w = (WORK *) xalloc(sizeof *w);
5454632Seric 		w->w_name = wlist[i].w_name;
5464632Seric 		w->w_pri = wlist[i].w_pri;
54725013Seric 		w->w_ctime = wlist[i].w_ctime;
54824981Seric 		w->w_next = WorkQ;
54924981Seric 		WorkQ = w;
5504632Seric 	}
5514632Seric 
5527677Seric 	if (tTd(40, 1))
5534632Seric 	{
5544632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
5555037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
5564632Seric 	}
55710070Seric 
55810090Seric 	return (wn);
5594632Seric }
5604632Seric /*
5617677Seric **  WORKCMPF -- compare function for ordering work.
5624632Seric **
5634632Seric **	Parameters:
5644632Seric **		a -- the first argument.
5654632Seric **		b -- the second argument.
5664632Seric **
5674632Seric **	Returns:
56824981Seric **		-1 if a < b
56924981Seric **		 0 if a == b
57024981Seric **		+1 if a > b
5714632Seric **
5724632Seric **	Side Effects:
5734632Seric **		none.
5744632Seric */
5754632Seric 
5764632Seric workcmpf(a, b)
5775037Seric 	register WORK *a;
5785037Seric 	register WORK *b;
5794632Seric {
58025013Seric 	long pa = a->w_pri + a->w_ctime;
58125013Seric 	long pb = b->w_pri + b->w_ctime;
58224941Seric 
58324941Seric 	if (pa == pb)
5844632Seric 		return (0);
58524941Seric 	else if (pa > pb)
58624981Seric 		return (1);
58724981Seric 	else
58810121Seric 		return (-1);
5894632Seric }
5904632Seric /*
5914632Seric **  DOWORK -- do a work request.
5924632Seric **
5934632Seric **	Parameters:
5944632Seric **		w -- the work request to be satisfied.
5954632Seric **
5964632Seric **	Returns:
5974632Seric **		none.
5984632Seric **
5994632Seric **	Side Effects:
6004632Seric **		The work request is satisfied if possible.
6014632Seric */
6024632Seric 
6034632Seric dowork(w)
6044632Seric 	register WORK *w;
6054632Seric {
6064632Seric 	register int i;
60724941Seric 	extern bool shouldqueue();
60851920Seric 	extern bool readqf();
6094632Seric 
6107677Seric 	if (tTd(40, 1))
6115037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
6124632Seric 
6134632Seric 	/*
61424941Seric 	**  Ignore jobs that are too expensive for the moment.
6154632Seric 	*/
6164632Seric 
61724941Seric 	if (shouldqueue(w->w_pri))
6184632Seric 	{
61924941Seric 		if (Verbose)
62024967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
6214632Seric 		return;
6224632Seric 	}
6234632Seric 
62424941Seric 	/*
62524941Seric 	**  Fork for work.
62624941Seric 	*/
62724941Seric 
62824941Seric 	if (ForkQueueRuns)
62924941Seric 	{
63024941Seric 		i = fork();
63124941Seric 		if (i < 0)
63224941Seric 		{
63324941Seric 			syserr("dowork: cannot fork");
63424941Seric 			return;
63524941Seric 		}
63624941Seric 	}
63724941Seric 	else
63824941Seric 	{
63924941Seric 		i = 0;
64024941Seric 	}
64124941Seric 
6424632Seric 	if (i == 0)
6434632Seric 	{
6444632Seric 		/*
6454632Seric 		**  CHILD
6468148Seric 		**	Lock the control file to avoid duplicate deliveries.
6478148Seric 		**		Then run the file as though we had just read it.
6487350Seric 		**	We save an idea of the temporary name so we
6497350Seric 		**		can recover on interrupt.
6504632Seric 		*/
6514632Seric 
6527763Seric 		/* set basic modes, etc. */
6537356Seric 		(void) alarm(0);
65425612Seric 		clearenvelope(CurEnv, FALSE);
6554632Seric 		QueueRun = TRUE;
6569377Seric 		ErrorMode = EM_MAIL;
6578148Seric 		CurEnv->e_id = &w->w_name[2];
6587876Seric # ifdef LOG
6597876Seric 		if (LogLevel > 11)
6607881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
6617881Seric 			       getpid());
6627876Seric # endif LOG
6637763Seric 
6647763Seric 		/* don't use the headers from sendmail.cf... */
6657763Seric 		CurEnv->e_header = NULL;
6667763Seric 
66751920Seric 		/* read the queue control file -- return if locked */
66851920Seric 		if (!readqf(CurEnv))
6696980Seric 		{
67024941Seric 			if (ForkQueueRuns)
67124941Seric 				exit(EX_OK);
67224941Seric 			else
67324941Seric 				return;
6746980Seric 		}
6756980Seric 
6769338Seric 		CurEnv->e_flags |= EF_INQUEUE;
6779377Seric 		eatheader(CurEnv);
6786980Seric 
6796980Seric 		/* do the delivery */
6809338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
6819282Seric 			sendall(CurEnv, SM_DELIVER);
6826980Seric 
6836980Seric 		/* finish up and exit */
68424941Seric 		if (ForkQueueRuns)
68524941Seric 			finis();
68624941Seric 		else
68724941Seric 			dropenvelope(CurEnv);
6884632Seric 	}
68924941Seric 	else
69024941Seric 	{
69124941Seric 		/*
69224941Seric 		**  Parent -- pick up results.
69324941Seric 		*/
6944632Seric 
69524941Seric 		errno = 0;
69624941Seric 		(void) waitfor(i);
69724941Seric 	}
6984632Seric }
6994632Seric /*
7004632Seric **  READQF -- read queue file and set up environment.
7014632Seric **
7024632Seric **	Parameters:
7039377Seric **		e -- the envelope of the job to run.
7044632Seric **
7054632Seric **	Returns:
70651920Seric **		TRUE if it successfully read the queue file.
70751920Seric **		FALSE otherwise.
7084632Seric **
7094632Seric **	Side Effects:
71051920Seric **		The queue file is returned locked.
7114632Seric */
7124632Seric 
71351920Seric bool
71451920Seric readqf(e)
7159377Seric 	register ENVELOPE *e;
7164632Seric {
71717477Seric 	char *qf;
71817477Seric 	register FILE *qfp;
7197785Seric 	char buf[MAXFIELD];
7209348Seric 	extern char *fgetfolded();
72124954Seric 	extern long atol();
72240973Sbostic 	int gotctluser = 0;
72340934Srick 	int fd;
72451937Seric # ifdef LOCKF
72551937Seric 	struct flock lfd;
72651937Seric # endif
7274632Seric 
7284632Seric 	/*
72917468Seric 	**  Read and process the file.
7304632Seric 	*/
7314632Seric 
73217477Seric 	qf = queuename(e, 'q');
73351937Seric 	qfp = fopen(qf, "r+");
73417477Seric 	if (qfp == NULL)
73517477Seric 	{
73640934Srick 		if (errno != ENOENT)
73740934Srick 			syserr("readqf: no control file %s", qf);
73851920Seric 		return FALSE;
73917477Seric 	}
74040934Srick 
74151835Seric # ifdef LOCKF
74251937Seric 	lfd.l_type = F_WRLCK;
74351937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
74451937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
74551835Seric # else
74640934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
74751835Seric # endif
74840934Srick 	{
74940934Srick 		/* being processed by another queuer */
75040934Srick 		if (Verbose)
75141636Srick 			printf("%s: locked\n", CurEnv->e_id);
75251920Seric # ifdef LOG
75351920Seric 		if (LogLevel > 9)
75451920Seric 			syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
75540934Srick # endif LOG
75640934Srick 		(void) fclose(qfp);
75751920Seric 		return FALSE;
75840934Srick 	}
75940934Srick 
76051920Seric 	/* save this lock */
76151920Seric 	e->e_lockfp = qfp;
76251920Seric 
76340934Srick 	/* do basic system initialization */
76440934Srick 	initsys();
76540934Srick 
76617477Seric 	FileName = qf;
7679377Seric 	LineNumber = 0;
76851920Seric 	if (Verbose)
7699377Seric 		printf("\nRunning %s\n", e->e_id);
77017468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
7714632Seric 	{
77226504Seric 		if (tTd(40, 4))
77326504Seric 			printf("+++++ %s\n", buf);
7744632Seric 		switch (buf[0])
7754632Seric 		{
77640973Sbostic 		  case 'C':		/* specify controlling user */
77740973Sbostic 			setctluser(&buf[1]);
77840973Sbostic 			gotctluser = 1;
77940973Sbostic 			break;
78040973Sbostic 
7814632Seric 		  case 'R':		/* specify recipient */
7829618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
7834632Seric 			break;
7844632Seric 
78525687Seric 		  case 'E':		/* specify error recipient */
78625687Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue);
78725687Seric 			break;
78825687Seric 
7894632Seric 		  case 'H':		/* header */
79051920Seric 			(void) chompheader(&buf[1], FALSE);
7914632Seric 			break;
7924632Seric 
79310108Seric 		  case 'M':		/* message */
79410108Seric 			e->e_message = newstr(&buf[1]);
79510108Seric 			break;
79610108Seric 
7974632Seric 		  case 'S':		/* sender */
798*53182Seric 			setsender(newstr(&buf[1]), CurEnv);
7994632Seric 			break;
8004632Seric 
8014632Seric 		  case 'D':		/* data file name */
8029377Seric 			e->e_df = newstr(&buf[1]);
8039544Seric 			e->e_dfp = fopen(e->e_df, "r");
8049544Seric 			if (e->e_dfp == NULL)
8059377Seric 				syserr("readqf: cannot open %s", e->e_df);
8064632Seric 			break;
8074632Seric 
8087860Seric 		  case 'T':		/* init time */
80924941Seric 			e->e_ctime = atol(&buf[1]);
8104632Seric 			break;
8114632Seric 
8124634Seric 		  case 'P':		/* message priority */
81325008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
8144634Seric 			break;
8154634Seric 
81624941Seric 		  case '\0':		/* blank line; ignore */
81724941Seric 			break;
81824941Seric 
8194632Seric 		  default:
82024941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
82124941Seric 				LineNumber, buf);
8224632Seric 			break;
8234632Seric 		}
82440973Sbostic 		/*
82540973Sbostic 		**  The `C' queue file command operates on the next line,
82640973Sbostic 		**  so we use "gotctluser" to maintain state as follows:
82740973Sbostic 		**      0 - no controlling user,
82840973Sbostic 		**      1 - controlling user has been set but not used,
82940973Sbostic 		**      2 - controlling user must be used on next iteration.
83040973Sbostic 		*/
83140973Sbostic 		if (gotctluser == 1)
83240973Sbostic 			gotctluser++;
83340973Sbostic 		else if (gotctluser == 2)
83440973Sbostic 		{
83540973Sbostic 			clrctluser();
83640973Sbostic 			gotctluser = 0;
83740973Sbostic 		}
8384632Seric 	}
8399377Seric 
84040973Sbostic 	/* clear controlling user in case we break out prematurely */
84140973Sbostic 	clrctluser();
84240973Sbostic 
8439377Seric 	FileName = NULL;
84424941Seric 
84524941Seric 	/*
84624941Seric 	**  If we haven't read any lines, this queue file is empty.
84724941Seric 	**  Arrange to remove it without referencing any null pointers.
84824941Seric 	*/
84924941Seric 
85024941Seric 	if (LineNumber == 0)
85124941Seric 	{
85224941Seric 		errno = 0;
85324941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
85424941Seric 	}
85551920Seric 	return TRUE;
8564632Seric }
8574632Seric /*
8589630Seric **  PRINTQUEUE -- print out a representation of the mail queue
8599630Seric **
8609630Seric **	Parameters:
8619630Seric **		none.
8629630Seric **
8639630Seric **	Returns:
8649630Seric **		none.
8659630Seric **
8669630Seric **	Side Effects:
8679630Seric **		Prints a listing of the mail queue on the standard output.
8689630Seric */
8695182Seric 
8709630Seric printqueue()
8719630Seric {
8729630Seric 	register WORK *w;
8739630Seric 	FILE *f;
87410070Seric 	int nrequests;
8759630Seric 	char buf[MAXLINE];
87640973Sbostic 	char cbuf[MAXLINE];
8779630Seric 
8789630Seric 	/*
8799630Seric 	**  Read and order the queue.
8809630Seric 	*/
8819630Seric 
88224941Seric 	nrequests = orderq(TRUE);
8839630Seric 
8849630Seric 	/*
8859630Seric 	**  Print the work list that we have read.
8869630Seric 	*/
8879630Seric 
8889630Seric 	/* first see if there is anything */
88910070Seric 	if (nrequests <= 0)
8909630Seric 	{
89110070Seric 		printf("Mail queue is empty\n");
8929630Seric 		return;
8939630Seric 	}
8949630Seric 
89551920Seric 	CurrentLA = getla();	/* get load average */
89640934Srick 
89710096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
89825687Seric 	if (nrequests > QUEUESIZE)
89925687Seric 		printf(", only %d printed", QUEUESIZE);
90024979Seric 	if (Verbose)
90125032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
90224979Seric 	else
90324979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
9049630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
9059630Seric 	{
9069630Seric 		struct stat st;
90710070Seric 		auto time_t submittime = 0;
90810070Seric 		long dfsize = -1;
90910108Seric 		char message[MAXLINE];
91051937Seric # ifdef LOCKF
91151937Seric 		struct flock lfd;
91251937Seric # endif
91324941Seric 		extern bool shouldqueue();
9149630Seric 
91517468Seric 		f = fopen(w->w_name, "r");
91617468Seric 		if (f == NULL)
91717468Seric 		{
91817468Seric 			errno = 0;
91917468Seric 			continue;
92017468Seric 		}
9219630Seric 		printf("%7s", w->w_name + 2);
92251835Seric # ifdef LOCKF
92351937Seric 		lfd.l_type = F_RDLCK;
92451937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
92551937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
92651835Seric # else
92740934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
92851835Seric # endif
92910070Seric 			printf("*");
93024941Seric 		else if (shouldqueue(w->w_pri))
93124941Seric 			printf("X");
93210070Seric 		else
93310070Seric 			printf(" ");
93410070Seric 		errno = 0;
93517468Seric 
93610108Seric 		message[0] = '\0';
93740973Sbostic 		cbuf[0] = '\0';
9389630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
9399630Seric 		{
9409630Seric 			fixcrlf(buf, TRUE);
9419630Seric 			switch (buf[0])
9429630Seric 			{
94310108Seric 			  case 'M':	/* error message */
94423098Seric 				(void) strcpy(message, &buf[1]);
94510108Seric 				break;
94610108Seric 
9479630Seric 			  case 'S':	/* sender name */
94824979Seric 				if (Verbose)
94925027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
95025027Seric 					    w->w_pri, ctime(&submittime) + 4,
95124979Seric 					    &buf[1]);
95224979Seric 				else
95324979Seric 					printf("%8ld %.16s %.45s", dfsize,
95424979Seric 					    ctime(&submittime), &buf[1]);
95510108Seric 				if (message[0] != '\0')
95625027Seric 					printf("\n\t\t (%.60s)", message);
9579630Seric 				break;
95851920Seric 
95940973Sbostic 			  case 'C':	/* controlling user */
96040973Sbostic 				if (strlen(buf) < MAXLINE-3)	/* sanity */
96140973Sbostic 					(void) strcat(buf, ") ");
96240973Sbostic 				cbuf[0] = cbuf[1] = '(';
96340973Sbostic 				(void) strncpy(&cbuf[2], &buf[1], MAXLINE-1);
96440973Sbostic 				cbuf[MAXLINE-1] = '\0';
96540973Sbostic 				break;
9669630Seric 
9679630Seric 			  case 'R':	/* recipient name */
96840973Sbostic 				if (cbuf[0] != '\0') {
96940973Sbostic 					/* prepend controlling user to `buf' */
97040973Sbostic 					(void) strncat(cbuf, &buf[1],
97140973Sbostic 					              MAXLINE-strlen(cbuf));
97240973Sbostic 					cbuf[MAXLINE-1] = '\0';
97340973Sbostic 					(void) strcpy(buf, cbuf);
97440973Sbostic 					cbuf[0] = '\0';
97540973Sbostic 				}
97624979Seric 				if (Verbose)
97725027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
97824979Seric 				else
97924979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
9809630Seric 				break;
9819630Seric 
9829630Seric 			  case 'T':	/* creation time */
98324941Seric 				submittime = atol(&buf[1]);
9849630Seric 				break;
98510070Seric 
98610070Seric 			  case 'D':	/* data file name */
98710070Seric 				if (stat(&buf[1], &st) >= 0)
98810070Seric 					dfsize = st.st_size;
98910070Seric 				break;
9909630Seric 			}
9919630Seric 		}
99210070Seric 		if (submittime == (time_t) 0)
99310070Seric 			printf(" (no control file)");
9949630Seric 		printf("\n");
99523098Seric 		(void) fclose(f);
9969630Seric 	}
9979630Seric }
9989630Seric 
9995182Seric # endif QUEUE
100017468Seric /*
100117468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
100217468Seric **
100317468Seric **	Assigns an id code if one does not already exist.
100417468Seric **	This code is very careful to avoid trashing existing files
100517468Seric **	under any circumstances.
100617468Seric **
100717468Seric **	Parameters:
100817468Seric **		e -- envelope to build it in/from.
100917468Seric **		type -- the file type, used as the first character
101017468Seric **			of the file name.
101117468Seric **
101217468Seric **	Returns:
101317468Seric **		a pointer to the new file name (in a static buffer).
101417468Seric **
101517468Seric **	Side Effects:
101651920Seric **		If no id code is already assigned, queuename will
101751920Seric **		assign an id code, create a qf file, and leave a
101851920Seric **		locked, open-for-write file pointer in the envelope.
101917468Seric */
102017468Seric 
102117468Seric char *
102217468Seric queuename(e, type)
102317468Seric 	register ENVELOPE *e;
102417468Seric 	char type;
102517468Seric {
102617468Seric 	static char buf[MAXNAME];
102717468Seric 	static int pid = -1;
102817468Seric 	char c1 = 'A';
102917468Seric 	char c2 = 'A';
103017468Seric 
103117468Seric 	if (e->e_id == NULL)
103217468Seric 	{
103317468Seric 		char qf[20];
103417468Seric 
103517468Seric 		/* find a unique id */
103617468Seric 		if (pid != getpid())
103717468Seric 		{
103817468Seric 			/* new process -- start back at "AA" */
103917468Seric 			pid = getpid();
104017468Seric 			c1 = 'A';
104117468Seric 			c2 = 'A' - 1;
104217468Seric 		}
104317468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
104417468Seric 
104517468Seric 		while (c1 < '~' || c2 < 'Z')
104617468Seric 		{
104717468Seric 			int i;
104851937Seric # ifdef LOCKF
104951937Seric 			struct flock lfd;
105051937Seric # endif
105117468Seric 
105217468Seric 			if (c2 >= 'Z')
105317468Seric 			{
105417468Seric 				c1++;
105517468Seric 				c2 = 'A' - 1;
105617468Seric 			}
105740934Srick 			qf[2] = c1;
105840934Srick 			qf[3] = ++c2;
105917468Seric 			if (tTd(7, 20))
106040934Srick 				printf("queuename: trying \"%s\"\n", qf);
106117468Seric 
106240934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
106351920Seric 			if (i < 0)
106451920Seric 			{
106551920Seric 				if (errno == EEXIST)
106651920Seric 					continue;
106751920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
106851920Seric 					qf, QueueDir);
106951920Seric 				exit(EX_UNAVAILABLE);
107051920Seric 			}
107151920Seric # ifdef LOCKF
107251937Seric 			lfd.l_type = F_WRLCK;
107351937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
107451937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
107551920Seric # else
107651920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
107751920Seric # endif
107851920Seric 			{
107951920Seric 				e->e_lockfp = fdopen(i, "w");
108040934Srick 				break;
108117468Seric 			}
108251920Seric 
108351920Seric 			/* a reader got the file; abandon it and try again */
108451920Seric 			(void) close(i);
108517468Seric 		}
108617468Seric 		if (c1 >= '~' && c2 >= 'Z')
108717468Seric 		{
108817468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
108917468Seric 				qf, QueueDir);
109017468Seric 			exit(EX_OSERR);
109117468Seric 		}
109217468Seric 		e->e_id = newstr(&qf[2]);
109317468Seric 		define('i', e->e_id, e);
109417468Seric 		if (tTd(7, 1))
109517468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
109617468Seric # ifdef LOG
109717468Seric 		if (LogLevel > 16)
109817468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
109917468Seric # endif LOG
110017468Seric 	}
110117468Seric 
110217468Seric 	if (type == '\0')
110317468Seric 		return (NULL);
110417468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
110517468Seric 	if (tTd(7, 2))
110617468Seric 		printf("queuename: %s\n", buf);
110717468Seric 	return (buf);
110817468Seric }
110917468Seric /*
111017468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
111117468Seric **
111217468Seric **	Parameters:
111317468Seric **		e -- the envelope to unlock.
111417468Seric **
111517468Seric **	Returns:
111617468Seric **		none
111717468Seric **
111817468Seric **	Side Effects:
111917468Seric **		unlocks the queue for `e'.
112017468Seric */
112117468Seric 
112217468Seric unlockqueue(e)
112317468Seric 	ENVELOPE *e;
112417468Seric {
112551920Seric 	/* if there is a lock file in the envelope, close it */
112651920Seric 	if (e->e_lockfp != NULL)
112751920Seric 		fclose(e->e_lockfp);
112851920Seric 	e->e_lockfp = NULL;
112951920Seric 
113017468Seric 	/* remove the transcript */
113117468Seric # ifdef LOG
113217468Seric 	if (LogLevel > 19)
113317468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
113417468Seric # endif LOG
113517468Seric 	if (!tTd(51, 4))
113617468Seric 		xunlink(queuename(e, 'x'));
113717468Seric 
113817468Seric }
113940973Sbostic /*
114040973Sbostic **  GETCTLUSER -- return controlling user if mailing to prog or file
114140973Sbostic **
114240973Sbostic **	Check for a "|" or "/" at the beginning of the address.  If
114340973Sbostic **	found, return a controlling username.
114440973Sbostic **
114540973Sbostic **	Parameters:
114640973Sbostic **		a - the address to check out
114740973Sbostic **
114840973Sbostic **	Returns:
114940973Sbostic **		Either NULL, if we werent mailing to a program or file,
115040973Sbostic **		or a controlling user name (possibly in getpwuid's
115140973Sbostic **		static buffer).
115240973Sbostic **
115340973Sbostic **	Side Effects:
115440973Sbostic **		none.
115540973Sbostic */
115640973Sbostic 
115740973Sbostic char *
115840973Sbostic getctluser(a)
115940973Sbostic 	ADDRESS *a;
116040973Sbostic {
116140973Sbostic 	extern ADDRESS *getctladdr();
116240973Sbostic 	struct passwd *pw;
116340973Sbostic 	char *retstr;
116440973Sbostic 
116540973Sbostic 	/*
116640973Sbostic 	**  Get unquoted user for file, program or user.name check.
116740973Sbostic 	**  N.B. remove this code block to always emit controlling
116840973Sbostic 	**  addresses (at the expense of backward compatibility).
116940973Sbostic 	*/
117040973Sbostic 
117140973Sbostic 	{
117240973Sbostic 		char buf[MAXNAME];
117340973Sbostic 		(void) strncpy(buf, a->q_paddr, MAXNAME);
117440973Sbostic 		buf[MAXNAME-1] = '\0';
117540973Sbostic 		stripquotes(buf, TRUE);
117640973Sbostic 
117740973Sbostic 		if (buf[0] != '|' && buf[0] != '/')
117840973Sbostic 			return((char *)NULL);
117940973Sbostic 	}
118040973Sbostic 
118140973Sbostic 	a = getctladdr(a);		/* find controlling address */
118240973Sbostic 
118340973Sbostic 	if (a != NULL && a->q_uid != 0 && (pw = getpwuid(a->q_uid)) != NULL)
118440973Sbostic 		retstr = pw->pw_name;
118540973Sbostic 	else				/* use default user */
118640973Sbostic 		retstr = DefUser;
118740973Sbostic 
118840973Sbostic 	if (tTd(40, 5))
118940973Sbostic 		printf("Set controlling user for `%s' to `%s'\n",
119040973Sbostic 		       (a == NULL)? "<null>": a->q_paddr, retstr);
119140973Sbostic 
119240973Sbostic 	return(retstr);
119340973Sbostic }
119440973Sbostic /*
119540973Sbostic **  SETCTLUSER - sets `CtlUser' to controlling user
119640973Sbostic **  CLRCTLUSER - clears controlling user (no params, nothing returned)
119740973Sbostic **
119840973Sbostic **	These routines manipulate `CtlUser'.
119940973Sbostic **
120040973Sbostic **	Parameters:
120140973Sbostic **		str  - controlling user as passed to setctluser()
120240973Sbostic **
120340973Sbostic **	Returns:
120440973Sbostic **		None.
120540973Sbostic **
120640973Sbostic **	Side Effects:
120740973Sbostic **		`CtlUser' is changed.
120840973Sbostic */
120940973Sbostic 
121040973Sbostic static char CtlUser[MAXNAME];
121140973Sbostic 
121240973Sbostic setctluser(str)
121340973Sbostic register char *str;
121440973Sbostic {
121540973Sbostic 	(void) strncpy(CtlUser, str, MAXNAME);
121640973Sbostic 	CtlUser[MAXNAME-1] = '\0';
121740973Sbostic }
121840973Sbostic 
121940973Sbostic clrctluser()
122040973Sbostic {
122140973Sbostic 	CtlUser[0] = '\0';
122240973Sbostic }
122340973Sbostic 
122440973Sbostic /*
122540973Sbostic **  SETCTLADDR -- create a controlling address
122640973Sbostic **
122740973Sbostic **	If global variable `CtlUser' is set and we are given a valid
122840973Sbostic **	address, make that address a controlling address; change the
122940973Sbostic **	`q_uid', `q_gid', and `q_ruser' fields and set QGOODUID.
123040973Sbostic **
123140973Sbostic **	Parameters:
123240973Sbostic **		a - address for which control uid/gid info may apply
123340973Sbostic **
123440973Sbostic **	Returns:
123540973Sbostic **		None.
123640973Sbostic **
123740973Sbostic **	Side Effects:
123840973Sbostic **		Fills in uid/gid fields in address and sets QGOODUID
123940973Sbostic **		flag if appropriate.
124040973Sbostic */
124140973Sbostic 
124240973Sbostic setctladdr(a)
124340973Sbostic 	ADDRESS *a;
124440973Sbostic {
124540973Sbostic 	struct passwd *pw;
124640973Sbostic 
124740973Sbostic 	/*
124840973Sbostic 	**  If there is no current controlling user, or we were passed a
124940973Sbostic 	**  NULL addr ptr or we already have a controlling user, return.
125040973Sbostic 	*/
125140973Sbostic 
125240973Sbostic 	if (CtlUser[0] == '\0' || a == NULL || a->q_ruser)
125340973Sbostic 		return;
125440973Sbostic 
125540973Sbostic 	/*
125640973Sbostic 	**  Set up addr fields for controlling user.  If `CtlUser' is no
125740973Sbostic 	**  longer valid, use the default user/group.
125840973Sbostic 	*/
125940973Sbostic 
126040973Sbostic 	if ((pw = getpwnam(CtlUser)) != NULL)
126140973Sbostic 	{
126240973Sbostic 		if (a->q_home)
126340973Sbostic 			free(a->q_home);
126440973Sbostic 		a->q_home = newstr(pw->pw_dir);
126540973Sbostic 		a->q_uid = pw->pw_uid;
126640973Sbostic 		a->q_gid = pw->pw_gid;
126740973Sbostic 		a->q_ruser = newstr(CtlUser);
126840973Sbostic 	}
126940973Sbostic 	else
127040973Sbostic 	{
127140973Sbostic 		a->q_uid = DefUid;
127240973Sbostic 		a->q_gid = DefGid;
127340973Sbostic 		a->q_ruser = newstr(DefUser);
127440973Sbostic 	}
127540973Sbostic 
127640973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
127740973Sbostic 
127840973Sbostic 	if (tTd(40, 5))
127940973Sbostic 		printf("Restored controlling user for `%s' to `%s'\n",
128040973Sbostic 		       a->q_paddr, a->q_ruser);
128140973Sbostic }
1282