xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 46928)
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*46928Sbostic static char sccsid[] = "@(#)queue.c	5.31 (Berkeley) 03/02/91 (with queueing)";
1433731Sbostic #else
15*46928Sbostic static char sccsid[] = "@(#)queue.c	5.31 (Berkeley) 03/02/91 (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>
254632Seric 
2633731Sbostic # ifdef QUEUE
274632Seric 
284632Seric /*
299377Seric **  Work queue.
309377Seric */
319377Seric 
329377Seric struct work
339377Seric {
349377Seric 	char		*w_name;	/* name of control file */
359377Seric 	long		w_pri;		/* priority of message, see below */
3625013Seric 	time_t		w_ctime;	/* creation time of message */
379377Seric 	struct work	*w_next;	/* next in queue */
389377Seric };
399377Seric 
409377Seric typedef struct work	WORK;
4140934Srick extern int la;
429377Seric 
439377Seric WORK	*WorkQ;			/* queue of things to be done */
449377Seric /*
454632Seric **  QUEUEUP -- queue a message up for future transmission.
464632Seric **
474632Seric **	Parameters:
486980Seric **		e -- the envelope to queue up.
496999Seric **		queueall -- if TRUE, queue all addresses, rather than
506999Seric **			just those with the QQUEUEUP flag set.
519377Seric **		announce -- if TRUE, tell when you are queueing up.
524632Seric **
534632Seric **	Returns:
5440934Srick **		locked FILE* to q file
554632Seric **
564632Seric **	Side Effects:
579377Seric **		The current request are saved in a control file.
584632Seric */
594632Seric 
6040934Srick FILE *
619377Seric queueup(e, queueall, announce)
626980Seric 	register ENVELOPE *e;
636999Seric 	bool queueall;
649377Seric 	bool announce;
654632Seric {
667812Seric 	char *qf;
6741636Srick 	char buf[MAXLINE], tf[MAXLINE];
687812Seric 	register FILE *tfp;
694632Seric 	register HDR *h;
705007Seric 	register ADDRESS *q;
7110173Seric 	MAILER nullmailer;
7241636Srick 	int fd, ret;
734632Seric 
745037Seric 	/*
7517477Seric 	**  Create control file.
765037Seric 	*/
774632Seric 
7841636Srick 	do {
7941636Srick 		strcpy(tf, queuename(e, 't'));
8041636Srick 		fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
8141636Srick 		if (fd < 0) {
8241636Srick 			if ( errno != EEXIST) {
8341636Srick 				syserr("queueup: cannot create temp file %s",
8441636Srick 					tf);
8541636Srick 				return NULL;
8641636Srick 			}
8741636Srick 		} else {
8841636Srick 			if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
8941636Srick 				if (errno != EWOULDBLOCK)
9041636Srick 					syserr("cannot flock(%s)", tf);
9141636Srick 				close(fd);
9241636Srick 				fd = -1;
9341636Srick 			}
9441636Srick 		}
9541636Srick 	} while (fd < 0);
9641636Srick 
9740934Srick 	tfp = fdopen(fd, "w");
984632Seric 
997677Seric 	if (tTd(40, 1))
10017468Seric 		printf("queueing %s\n", e->e_id);
1014632Seric 
1024632Seric 	/*
1036980Seric 	**  If there is no data file yet, create one.
1046980Seric 	*/
1056980Seric 
1066980Seric 	if (e->e_df == NULL)
1076980Seric 	{
1086980Seric 		register FILE *dfp;
1099389Seric 		extern putbody();
1106980Seric 
1117812Seric 		e->e_df = newstr(queuename(e, 'd'));
11240934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
11340934Srick 		if (fd < 0)
1146980Seric 		{
1156980Seric 			syserr("queueup: cannot create %s", e->e_df);
1167812Seric 			(void) fclose(tfp);
11740934Srick 			return NULL;
1186980Seric 		}
11940934Srick 		dfp = fdopen(fd, "w");
12010173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1217009Seric 		(void) fclose(dfp);
1229389Seric 		e->e_putbody = putbody;
1236980Seric 	}
1246980Seric 
1256980Seric 	/*
1264632Seric 	**  Output future work requests.
12725687Seric 	**	Priority and creation time should be first, since
12825687Seric 	**	they are required by orderq.
1294632Seric 	*/
1304632Seric 
1319377Seric 	/* output message priority */
1329377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1339377Seric 
1349630Seric 	/* output creation time */
1359630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1369630Seric 
1374632Seric 	/* output name of data file */
1387812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1394632Seric 
14010108Seric 	/* message from envelope, if it exists */
14110108Seric 	if (e->e_message != NULL)
14210108Seric 		fprintf(tfp, "M%s\n", e->e_message);
14310108Seric 
1444632Seric 	/* output name of sender */
1457812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1464632Seric 
1474632Seric 	/* output list of recipient addresses */
1486980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1494632Seric 	{
1507763Seric 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
1517763Seric 			       bitset(QQUEUEUP, q->q_flags))
1528245Seric 		{
15340973Sbostic 			char *ctluser, *getctluser();
15440973Sbostic 
15540973Sbostic 			if ((ctluser = getctluser(q)) != NULL)
15640973Sbostic 				fprintf(tfp, "C%s\n", ctluser);
1577812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
1589377Seric 			if (announce)
1599377Seric 			{
1609377Seric 				e->e_to = q->q_paddr;
1619377Seric 				message(Arpa_Info, "queued");
1629377Seric 				if (LogLevel > 4)
1639377Seric 					logdelivery("queued");
1649377Seric 				e->e_to = NULL;
1659377Seric 			}
1669387Seric 			if (tTd(40, 1))
1679387Seric 			{
1689387Seric 				printf("queueing ");
1699387Seric 				printaddr(q, FALSE);
1709387Seric 			}
1718245Seric 		}
1724632Seric 	}
1734632Seric 
17425687Seric 	/* output list of error recipients */
17525687Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
17625687Seric 	{
17726504Seric 		if (!bitset(QDONTSEND, q->q_flags))
17840973Sbostic 		{
17940973Sbostic 			char *ctluser, *getctluser();
18040973Sbostic 
18140973Sbostic 			if ((ctluser = getctluser(q)) != NULL)
18240973Sbostic 				fprintf(tfp, "C%s\n", ctluser);
18326504Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
18440973Sbostic 		}
18525687Seric 	}
18625687Seric 
1879377Seric 	/*
1889377Seric 	**  Output headers for this message.
1899377Seric 	**	Expand macros completely here.  Queue run will deal with
1909377Seric 	**	everything as absolute headers.
1919377Seric 	**		All headers that must be relative to the recipient
1929377Seric 	**		can be cracked later.
19310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
19410173Seric 	**	no effect on the addresses as they are output.
1959377Seric 	*/
1969377Seric 
19710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
19810173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
19910349Seric 	nullmailer.m_eol = "\n";
20010173Seric 
20116147Seric 	define('g', "\001f", e);
2026980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2034632Seric 	{
20410686Seric 		extern bool bitzerop();
20510686Seric 
20612015Seric 		/* don't output null headers */
2074632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2084632Seric 			continue;
20912015Seric 
21012015Seric 		/* don't output resent headers on non-resent messages */
21112015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
21212015Seric 			continue;
21312015Seric 
21412015Seric 		/* output this header */
2157812Seric 		fprintf(tfp, "H");
21612015Seric 
21712015Seric 		/* if conditional, output the set of conditions */
21810686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
21910686Seric 		{
22010686Seric 			int j;
22110686Seric 
22223098Seric 			(void) putc('?', tfp);
22310686Seric 			for (j = '\0'; j <= '\177'; j++)
22410686Seric 				if (bitnset(j, h->h_mflags))
22523098Seric 					(void) putc(j, tfp);
22623098Seric 			(void) putc('?', tfp);
22710686Seric 		}
22812015Seric 
22912015Seric 		/* output the header: expand macros, convert addresses */
2307763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2317763Seric 		{
2327763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2338236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2347763Seric 		}
2358245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2369348Seric 		{
2379348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
23810173Seric 				 &nullmailer);
2399348Seric 		}
2407763Seric 		else
2418245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2424632Seric 	}
2434632Seric 
2444632Seric 	/*
2454632Seric 	**  Clean up.
2464632Seric 	*/
2474632Seric 
24817468Seric 	qf = queuename(e, 'q');
24941636Srick 	if (rename(tf, qf) < 0)
25041636Srick 		syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
25141636Srick 	errno = 0;
2527391Seric 
2537677Seric # ifdef LOG
2547677Seric 	/* save log info */
2557878Seric 	if (LogLevel > 15)
2567878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
2577677Seric # endif LOG
25840934Srick 	fflush(tfp);
25940934Srick 	return tfp;
2604632Seric }
2614632Seric /*
2624632Seric **  RUNQUEUE -- run the jobs in the queue.
2634632Seric **
2644632Seric **	Gets the stuff out of the queue in some presumably logical
2654632Seric **	order and processes them.
2664632Seric **
2674632Seric **	Parameters:
26824941Seric **		forkflag -- TRUE if the queue scanning should be done in
26924941Seric **			a child process.  We double-fork so it is not our
27024941Seric **			child and we don't have to clean up after it.
2714632Seric **
2724632Seric **	Returns:
2734632Seric **		none.
2744632Seric **
2754632Seric **	Side Effects:
2764632Seric **		runs things in the mail queue.
2774632Seric */
2784632Seric 
2794639Seric runqueue(forkflag)
2804639Seric 	bool forkflag;
2814632Seric {
28224953Seric 	extern bool shouldqueue();
28324953Seric 
2847466Seric 	/*
28524953Seric 	**  If no work will ever be selected, don't even bother reading
28624953Seric 	**  the queue.
28724953Seric 	*/
28824953Seric 
28940934Srick 	la = getla();	/* get load average */
29040934Srick 
29124953Seric 	if (shouldqueue(-100000000L))
29224953Seric 	{
29324953Seric 		if (Verbose)
29424953Seric 			printf("Skipping queue run -- load average too high\n");
29524953Seric 
29624953Seric 		if (forkflag)
29724953Seric 			return;
29824953Seric 		finis();
29924953Seric 	}
30024953Seric 
30124953Seric 	/*
3027466Seric 	**  See if we want to go off and do other useful work.
3037466Seric 	*/
3044639Seric 
3054639Seric 	if (forkflag)
3064639Seric 	{
3077943Seric 		int pid;
3087943Seric 
3097943Seric 		pid = dofork();
3107943Seric 		if (pid != 0)
3114639Seric 		{
312*46928Sbostic 			extern void reapchild();
31325184Seric 
3147943Seric 			/* parent -- pick up intermediate zombie */
31525184Seric #ifndef SIGCHLD
3169377Seric 			(void) waitfor(pid);
31725184Seric #else SIGCHLD
31825184Seric 			(void) signal(SIGCHLD, reapchild);
31925184Seric #endif SIGCHLD
3207690Seric 			if (QueueIntvl != 0)
3219348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3224639Seric 			return;
3234639Seric 		}
3247943Seric 		/* child -- double fork */
32525184Seric #ifndef SIGCHLD
3267943Seric 		if (fork() != 0)
3277943Seric 			exit(EX_OK);
32825184Seric #else SIGCHLD
32925184Seric 		(void) signal(SIGCHLD, SIG_DFL);
33025184Seric #endif SIGCHLD
3314639Seric 	}
33224941Seric 
33340934Srick 	setproctitle("running queue: %s", QueueDir);
33424941Seric 
3357876Seric # ifdef LOG
3367876Seric 	if (LogLevel > 11)
3377943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
3387876Seric # endif LOG
3394639Seric 
3407466Seric 	/*
34110205Seric 	**  Release any resources used by the daemon code.
34210205Seric 	*/
34310205Seric 
34410205Seric # ifdef DAEMON
34510205Seric 	clrdaemon();
34610205Seric # endif DAEMON
34710205Seric 
34810205Seric 	/*
34927175Seric 	**  Make sure the alias database is open.
35027175Seric 	*/
35127175Seric 
35227175Seric 	initaliases(AliasFile, FALSE);
35327175Seric 
35427175Seric 	/*
3557466Seric 	**  Start making passes through the queue.
3567466Seric 	**	First, read and sort the entire queue.
3577466Seric 	**	Then, process the work in that order.
3587466Seric 	**		But if you take too long, start over.
3597466Seric 	*/
3607466Seric 
3617943Seric 	/* order the existing work requests */
36224954Seric 	(void) orderq(FALSE);
3637690Seric 
3647943Seric 	/* process them once at a time */
3657943Seric 	while (WorkQ != NULL)
3664639Seric 	{
3677943Seric 		WORK *w = WorkQ;
3687881Seric 
3697943Seric 		WorkQ = WorkQ->w_next;
3707943Seric 		dowork(w);
3717943Seric 		free(w->w_name);
3727943Seric 		free((char *) w);
3734639Seric 	}
37429866Seric 
37529866Seric 	/* exit without the usual cleanup */
37629866Seric 	exit(ExitStat);
3774634Seric }
3784634Seric /*
3794632Seric **  ORDERQ -- order the work queue.
3804632Seric **
3814632Seric **	Parameters:
38224941Seric **		doall -- if set, include everything in the queue (even
38324941Seric **			the jobs that cannot be run because the load
38424941Seric **			average is too high).  Otherwise, exclude those
38524941Seric **			jobs.
3864632Seric **
3874632Seric **	Returns:
38810121Seric **		The number of request in the queue (not necessarily
38910121Seric **		the number of requests in WorkQ however).
3904632Seric **
3914632Seric **	Side Effects:
3924632Seric **		Sets WorkQ to the queue of available work, in order.
3934632Seric */
3944632Seric 
39525687Seric # define NEED_P		001
39625687Seric # define NEED_T		002
3974632Seric 
39824941Seric orderq(doall)
39924941Seric 	bool doall;
4004632Seric {
4016625Sglickman 	register struct direct *d;
4024632Seric 	register WORK *w;
4036625Sglickman 	DIR *f;
4044632Seric 	register int i;
40525687Seric 	WORK wlist[QUEUESIZE+1];
40610070Seric 	int wn = -1;
4074632Seric 	extern workcmpf();
4084632Seric 
4094632Seric 	/* clear out old WorkQ */
4104632Seric 	for (w = WorkQ; w != NULL; )
4114632Seric 	{
4124632Seric 		register WORK *nw = w->w_next;
4134632Seric 
4144632Seric 		WorkQ = nw;
4154632Seric 		free(w->w_name);
4164632Seric 		free((char *) w);
4174632Seric 		w = nw;
4184632Seric 	}
4194632Seric 
4204632Seric 	/* open the queue directory */
4218148Seric 	f = opendir(".");
4224632Seric 	if (f == NULL)
4234632Seric 	{
4248148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
42510070Seric 		return (0);
4264632Seric 	}
4274632Seric 
4284632Seric 	/*
4294632Seric 	**  Read the work directory.
4304632Seric 	*/
4314632Seric 
43210070Seric 	while ((d = readdir(f)) != NULL)
4334632Seric 	{
4349377Seric 		FILE *cf;
4354632Seric 		char lbuf[MAXNAME];
4364632Seric 
4374632Seric 		/* is this an interesting entry? */
4387812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
4394632Seric 			continue;
4404632Seric 
44110070Seric 		/* yes -- open control file (if not too many files) */
44225687Seric 		if (++wn >= QUEUESIZE)
44310070Seric 			continue;
4448148Seric 		cf = fopen(d->d_name, "r");
4454632Seric 		if (cf == NULL)
4464632Seric 		{
4477055Seric 			/* this may be some random person sending hir msgs */
4487055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
44910090Seric 			if (tTd(41, 2))
45010090Seric 				printf("orderq: cannot open %s (%d)\n",
45110090Seric 					d->d_name, errno);
4527055Seric 			errno = 0;
45310090Seric 			wn--;
4544632Seric 			continue;
4554632Seric 		}
45625687Seric 		w = &wlist[wn];
45725687Seric 		w->w_name = newstr(d->d_name);
4584632Seric 
45925027Seric 		/* make sure jobs in creation don't clog queue */
46025687Seric 		w->w_pri = 0x7fffffff;
46125687Seric 		w->w_ctime = 0;
46225027Seric 
4634632Seric 		/* extract useful information */
46425687Seric 		i = NEED_P | NEED_T;
46525687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
4664632Seric 		{
46724954Seric 			extern long atol();
46824954Seric 
46924941Seric 			switch (lbuf[0])
4704632Seric 			{
47124941Seric 			  case 'P':
47225687Seric 				w->w_pri = atol(&lbuf[1]);
47325687Seric 				i &= ~NEED_P;
4744632Seric 				break;
47525013Seric 
47625013Seric 			  case 'T':
47725687Seric 				w->w_ctime = atol(&lbuf[1]);
47825687Seric 				i &= ~NEED_T;
47925013Seric 				break;
4804632Seric 			}
4814632Seric 		}
4824632Seric 		(void) fclose(cf);
48324953Seric 
48425687Seric 		if (!doall && shouldqueue(w->w_pri))
48524953Seric 		{
48624953Seric 			/* don't even bother sorting this job in */
48724953Seric 			wn--;
48824953Seric 		}
4894632Seric 	}
4906625Sglickman 	(void) closedir(f);
49110090Seric 	wn++;
4924632Seric 
4934632Seric 	/*
4944632Seric 	**  Sort the work directory.
4954632Seric 	*/
4964632Seric 
49725687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
4984632Seric 
4994632Seric 	/*
5004632Seric 	**  Convert the work list into canonical form.
5019377Seric 	**	Should be turning it into a list of envelopes here perhaps.
5024632Seric 	*/
5034632Seric 
50424981Seric 	WorkQ = NULL;
50525687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
5064632Seric 	{
5074632Seric 		w = (WORK *) xalloc(sizeof *w);
5084632Seric 		w->w_name = wlist[i].w_name;
5094632Seric 		w->w_pri = wlist[i].w_pri;
51025013Seric 		w->w_ctime = wlist[i].w_ctime;
51124981Seric 		w->w_next = WorkQ;
51224981Seric 		WorkQ = w;
5134632Seric 	}
5144632Seric 
5157677Seric 	if (tTd(40, 1))
5164632Seric 	{
5174632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
5185037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
5194632Seric 	}
52010070Seric 
52110090Seric 	return (wn);
5224632Seric }
5234632Seric /*
5247677Seric **  WORKCMPF -- compare function for ordering work.
5254632Seric **
5264632Seric **	Parameters:
5274632Seric **		a -- the first argument.
5284632Seric **		b -- the second argument.
5294632Seric **
5304632Seric **	Returns:
53124981Seric **		-1 if a < b
53224981Seric **		 0 if a == b
53324981Seric **		+1 if a > b
5344632Seric **
5354632Seric **	Side Effects:
5364632Seric **		none.
5374632Seric */
5384632Seric 
5394632Seric workcmpf(a, b)
5405037Seric 	register WORK *a;
5415037Seric 	register WORK *b;
5424632Seric {
54325013Seric 	long pa = a->w_pri + a->w_ctime;
54425013Seric 	long pb = b->w_pri + b->w_ctime;
54524941Seric 
54624941Seric 	if (pa == pb)
5474632Seric 		return (0);
54824941Seric 	else if (pa > pb)
54924981Seric 		return (1);
55024981Seric 	else
55110121Seric 		return (-1);
5524632Seric }
5534632Seric /*
5544632Seric **  DOWORK -- do a work request.
5554632Seric **
5564632Seric **	Parameters:
5574632Seric **		w -- the work request to be satisfied.
5584632Seric **
5594632Seric **	Returns:
5604632Seric **		none.
5614632Seric **
5624632Seric **	Side Effects:
5634632Seric **		The work request is satisfied if possible.
5644632Seric */
5654632Seric 
5664632Seric dowork(w)
5674632Seric 	register WORK *w;
5684632Seric {
5694632Seric 	register int i;
57024941Seric 	extern bool shouldqueue();
5714632Seric 
5727677Seric 	if (tTd(40, 1))
5735037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
5744632Seric 
5754632Seric 	/*
57624941Seric 	**  Ignore jobs that are too expensive for the moment.
5774632Seric 	*/
5784632Seric 
57924941Seric 	if (shouldqueue(w->w_pri))
5804632Seric 	{
58124941Seric 		if (Verbose)
58224967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
5834632Seric 		return;
5844632Seric 	}
5854632Seric 
58624941Seric 	/*
58724941Seric 	**  Fork for work.
58824941Seric 	*/
58924941Seric 
59024941Seric 	if (ForkQueueRuns)
59124941Seric 	{
59224941Seric 		i = fork();
59324941Seric 		if (i < 0)
59424941Seric 		{
59524941Seric 			syserr("dowork: cannot fork");
59624941Seric 			return;
59724941Seric 		}
59824941Seric 	}
59924941Seric 	else
60024941Seric 	{
60124941Seric 		i = 0;
60224941Seric 	}
60324941Seric 
6044632Seric 	if (i == 0)
6054632Seric 	{
60640934Srick 		FILE *qflock, *readqf();
6074632Seric 		/*
6084632Seric 		**  CHILD
6098148Seric 		**	Lock the control file to avoid duplicate deliveries.
6108148Seric 		**		Then run the file as though we had just read it.
6117350Seric 		**	We save an idea of the temporary name so we
6127350Seric 		**		can recover on interrupt.
6134632Seric 		*/
6144632Seric 
6157763Seric 		/* set basic modes, etc. */
6167356Seric 		(void) alarm(0);
61725612Seric 		clearenvelope(CurEnv, FALSE);
6184632Seric 		QueueRun = TRUE;
6199377Seric 		ErrorMode = EM_MAIL;
6208148Seric 		CurEnv->e_id = &w->w_name[2];
6217876Seric # ifdef LOG
6227876Seric 		if (LogLevel > 11)
6237881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
6247881Seric 			       getpid());
6257876Seric # endif LOG
6267763Seric 
6277763Seric 		/* don't use the headers from sendmail.cf... */
6287763Seric 		CurEnv->e_header = NULL;
6297763Seric 
63040934Srick 		/* read the queue control file */
63140934Srick 		/*  and lock the control file during processing */
63240934Srick 		if ((qflock=readqf(CurEnv, TRUE)) == NULL)
6336980Seric 		{
63424941Seric 			if (ForkQueueRuns)
63524941Seric 				exit(EX_OK);
63624941Seric 			else
63724941Seric 				return;
6386980Seric 		}
6396980Seric 
6409338Seric 		CurEnv->e_flags |= EF_INQUEUE;
6419377Seric 		eatheader(CurEnv);
6426980Seric 
6436980Seric 		/* do the delivery */
6449338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
6459282Seric 			sendall(CurEnv, SM_DELIVER);
6466980Seric 
6476980Seric 		/* finish up and exit */
64824941Seric 		if (ForkQueueRuns)
64924941Seric 			finis();
65024941Seric 		else
65124941Seric 			dropenvelope(CurEnv);
65241636Srick 		fclose(qflock);
6534632Seric 	}
65424941Seric 	else
65524941Seric 	{
65624941Seric 		/*
65724941Seric 		**  Parent -- pick up results.
65824941Seric 		*/
6594632Seric 
66024941Seric 		errno = 0;
66124941Seric 		(void) waitfor(i);
66224941Seric 	}
6634632Seric }
6644632Seric /*
6654632Seric **  READQF -- read queue file and set up environment.
6664632Seric **
6674632Seric **	Parameters:
6689377Seric **		e -- the envelope of the job to run.
6699630Seric **		full -- if set, read in all information.  Otherwise just
6709630Seric **			read in info needed for a queue print.
6714632Seric **
6724632Seric **	Returns:
67340934Srick **		FILE * pointing to flock()ed fd so it can be closed
67440934Srick **		after the mail is delivered
6754632Seric **
6764632Seric **	Side Effects:
6774632Seric **		cf is read and created as the current job, as though
6784632Seric **		we had been invoked by argument.
6794632Seric */
6804632Seric 
68140934Srick FILE *
68217477Seric readqf(e, full)
6839377Seric 	register ENVELOPE *e;
6849630Seric 	bool full;
6854632Seric {
68617477Seric 	char *qf;
68717477Seric 	register FILE *qfp;
6887785Seric 	char buf[MAXFIELD];
6899348Seric 	extern char *fgetfolded();
69024954Seric 	extern long atol();
69140973Sbostic 	int gotctluser = 0;
69240934Srick 	int fd;
6934632Seric 
6944632Seric 	/*
69517468Seric 	**  Read and process the file.
6964632Seric 	*/
6974632Seric 
69817477Seric 	qf = queuename(e, 'q');
69917477Seric 	qfp = fopen(qf, "r");
70017477Seric 	if (qfp == NULL)
70117477Seric 	{
70240934Srick 		if (errno != ENOENT)
70340934Srick 			syserr("readqf: no control file %s", qf);
70440934Srick 		return NULL;
70517477Seric 	}
70640934Srick 
70740934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
70840934Srick 	{
70940934Srick # ifdef LOG
71040934Srick 		/* being processed by another queuer */
71140934Srick 		if (Verbose)
71241636Srick 			printf("%s: locked\n", CurEnv->e_id);
71340934Srick # endif LOG
71440934Srick 		(void) fclose(qfp);
71540934Srick 		return NULL;
71640934Srick 	}
71740934Srick 
71840934Srick 	/* do basic system initialization */
71940934Srick 	initsys();
72040934Srick 
72117477Seric 	FileName = qf;
7229377Seric 	LineNumber = 0;
7239630Seric 	if (Verbose && full)
7249377Seric 		printf("\nRunning %s\n", e->e_id);
72517468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
7264632Seric 	{
72726504Seric 		if (tTd(40, 4))
72826504Seric 			printf("+++++ %s\n", buf);
7294632Seric 		switch (buf[0])
7304632Seric 		{
73140973Sbostic 		  case 'C':		/* specify controlling user */
73240973Sbostic 			setctluser(&buf[1]);
73340973Sbostic 			gotctluser = 1;
73440973Sbostic 			break;
73540973Sbostic 
7364632Seric 		  case 'R':		/* specify recipient */
7379618Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
7384632Seric 			break;
7394632Seric 
74025687Seric 		  case 'E':		/* specify error recipient */
74125687Seric 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_errorqueue);
74225687Seric 			break;
74325687Seric 
7444632Seric 		  case 'H':		/* header */
7459630Seric 			if (full)
7469630Seric 				(void) chompheader(&buf[1], FALSE);
7474632Seric 			break;
7484632Seric 
74910108Seric 		  case 'M':		/* message */
75010108Seric 			e->e_message = newstr(&buf[1]);
75110108Seric 			break;
75210108Seric 
7534632Seric 		  case 'S':		/* sender */
7544634Seric 			setsender(newstr(&buf[1]));
7554632Seric 			break;
7564632Seric 
7574632Seric 		  case 'D':		/* data file name */
7589630Seric 			if (!full)
7599630Seric 				break;
7609377Seric 			e->e_df = newstr(&buf[1]);
7619544Seric 			e->e_dfp = fopen(e->e_df, "r");
7629544Seric 			if (e->e_dfp == NULL)
7639377Seric 				syserr("readqf: cannot open %s", e->e_df);
7644632Seric 			break;
7654632Seric 
7667860Seric 		  case 'T':		/* init time */
76724941Seric 			e->e_ctime = atol(&buf[1]);
7684632Seric 			break;
7694632Seric 
7704634Seric 		  case 'P':		/* message priority */
77125008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
7724634Seric 			break;
7734634Seric 
77424941Seric 		  case '\0':		/* blank line; ignore */
77524941Seric 			break;
77624941Seric 
7774632Seric 		  default:
77824941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
77924941Seric 				LineNumber, buf);
7804632Seric 			break;
7814632Seric 		}
78240973Sbostic 		/*
78340973Sbostic 		**  The `C' queue file command operates on the next line,
78440973Sbostic 		**  so we use "gotctluser" to maintain state as follows:
78540973Sbostic 		**      0 - no controlling user,
78640973Sbostic 		**      1 - controlling user has been set but not used,
78740973Sbostic 		**      2 - controlling user must be used on next iteration.
78840973Sbostic 		*/
78940973Sbostic 		if (gotctluser == 1)
79040973Sbostic 			gotctluser++;
79140973Sbostic 		else if (gotctluser == 2)
79240973Sbostic 		{
79340973Sbostic 			clrctluser();
79440973Sbostic 			gotctluser = 0;
79540973Sbostic 		}
7964632Seric 	}
7979377Seric 
79840973Sbostic 	/* clear controlling user in case we break out prematurely */
79940973Sbostic 	clrctluser();
80040973Sbostic 
8019377Seric 	FileName = NULL;
80224941Seric 
80324941Seric 	/*
80424941Seric 	**  If we haven't read any lines, this queue file is empty.
80524941Seric 	**  Arrange to remove it without referencing any null pointers.
80624941Seric 	*/
80724941Seric 
80824941Seric 	if (LineNumber == 0)
80924941Seric 	{
81024941Seric 		errno = 0;
81124941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
81224941Seric 	}
81340934Srick 	return qfp;
8144632Seric }
8154632Seric /*
8169630Seric **  PRINTQUEUE -- print out a representation of the mail queue
8179630Seric **
8189630Seric **	Parameters:
8199630Seric **		none.
8209630Seric **
8219630Seric **	Returns:
8229630Seric **		none.
8239630Seric **
8249630Seric **	Side Effects:
8259630Seric **		Prints a listing of the mail queue on the standard output.
8269630Seric */
8275182Seric 
8289630Seric printqueue()
8299630Seric {
8309630Seric 	register WORK *w;
8319630Seric 	FILE *f;
83210070Seric 	int nrequests;
8339630Seric 	char buf[MAXLINE];
83440973Sbostic 	char cbuf[MAXLINE];
8359630Seric 
8369630Seric 	/*
8379630Seric 	**  Read and order the queue.
8389630Seric 	*/
8399630Seric 
84024941Seric 	nrequests = orderq(TRUE);
8419630Seric 
8429630Seric 	/*
8439630Seric 	**  Print the work list that we have read.
8449630Seric 	*/
8459630Seric 
8469630Seric 	/* first see if there is anything */
84710070Seric 	if (nrequests <= 0)
8489630Seric 	{
84910070Seric 		printf("Mail queue is empty\n");
8509630Seric 		return;
8519630Seric 	}
8529630Seric 
85340934Srick 	la = getla();	/* get load average */
85440934Srick 
85510096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
85625687Seric 	if (nrequests > QUEUESIZE)
85725687Seric 		printf(", only %d printed", QUEUESIZE);
85824979Seric 	if (Verbose)
85925032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
86024979Seric 	else
86124979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
8629630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
8639630Seric 	{
8649630Seric 		struct stat st;
86510070Seric 		auto time_t submittime = 0;
86610070Seric 		long dfsize = -1;
86710108Seric 		char message[MAXLINE];
86824941Seric 		extern bool shouldqueue();
8699630Seric 
87017468Seric 		f = fopen(w->w_name, "r");
87117468Seric 		if (f == NULL)
87217468Seric 		{
87317468Seric 			errno = 0;
87417468Seric 			continue;
87517468Seric 		}
8769630Seric 		printf("%7s", w->w_name + 2);
87740934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
87810070Seric 			printf("*");
87924941Seric 		else if (shouldqueue(w->w_pri))
88024941Seric 			printf("X");
88110070Seric 		else
88210070Seric 			printf(" ");
88310070Seric 		errno = 0;
88417468Seric 
88510108Seric 		message[0] = '\0';
88640973Sbostic 		cbuf[0] = '\0';
8879630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
8889630Seric 		{
8899630Seric 			fixcrlf(buf, TRUE);
8909630Seric 			switch (buf[0])
8919630Seric 			{
89210108Seric 			  case 'M':	/* error message */
89323098Seric 				(void) strcpy(message, &buf[1]);
89410108Seric 				break;
89510108Seric 
8969630Seric 			  case 'S':	/* sender name */
89724979Seric 				if (Verbose)
89825027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
89925027Seric 					    w->w_pri, ctime(&submittime) + 4,
90024979Seric 					    &buf[1]);
90124979Seric 				else
90224979Seric 					printf("%8ld %.16s %.45s", dfsize,
90324979Seric 					    ctime(&submittime), &buf[1]);
90410108Seric 				if (message[0] != '\0')
90525027Seric 					printf("\n\t\t (%.60s)", message);
9069630Seric 				break;
90740973Sbostic 			  case 'C':	/* controlling user */
90840973Sbostic 				if (strlen(buf) < MAXLINE-3)	/* sanity */
90940973Sbostic 					(void) strcat(buf, ") ");
91040973Sbostic 				cbuf[0] = cbuf[1] = '(';
91140973Sbostic 				(void) strncpy(&cbuf[2], &buf[1], MAXLINE-1);
91240973Sbostic 				cbuf[MAXLINE-1] = '\0';
91340973Sbostic 				break;
9149630Seric 
9159630Seric 			  case 'R':	/* recipient name */
91640973Sbostic 				if (cbuf[0] != '\0') {
91740973Sbostic 					/* prepend controlling user to `buf' */
91840973Sbostic 					(void) strncat(cbuf, &buf[1],
91940973Sbostic 					              MAXLINE-strlen(cbuf));
92040973Sbostic 					cbuf[MAXLINE-1] = '\0';
92140973Sbostic 					(void) strcpy(buf, cbuf);
92240973Sbostic 					cbuf[0] = '\0';
92340973Sbostic 				}
92424979Seric 				if (Verbose)
92525027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
92624979Seric 				else
92724979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
9289630Seric 				break;
9299630Seric 
9309630Seric 			  case 'T':	/* creation time */
93124941Seric 				submittime = atol(&buf[1]);
9329630Seric 				break;
93310070Seric 
93410070Seric 			  case 'D':	/* data file name */
93510070Seric 				if (stat(&buf[1], &st) >= 0)
93610070Seric 					dfsize = st.st_size;
93710070Seric 				break;
9389630Seric 			}
9399630Seric 		}
94010070Seric 		if (submittime == (time_t) 0)
94110070Seric 			printf(" (no control file)");
9429630Seric 		printf("\n");
94323098Seric 		(void) fclose(f);
9449630Seric 	}
9459630Seric }
9469630Seric 
9475182Seric # endif QUEUE
94817468Seric /*
94917468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
95017468Seric **
95117468Seric **	Assigns an id code if one does not already exist.
95217468Seric **	This code is very careful to avoid trashing existing files
95317468Seric **	under any circumstances.
95417468Seric **
95517468Seric **	Parameters:
95617468Seric **		e -- envelope to build it in/from.
95717468Seric **		type -- the file type, used as the first character
95817468Seric **			of the file name.
95917468Seric **
96017468Seric **	Returns:
96117468Seric **		a pointer to the new file name (in a static buffer).
96217468Seric **
96317468Seric **	Side Effects:
96440934Srick **		Will create the qf file if no id code is
96517468Seric **		already assigned.  This will cause the envelope
96617468Seric **		to be modified.
96717468Seric */
96817468Seric 
96917468Seric char *
97017468Seric queuename(e, type)
97117468Seric 	register ENVELOPE *e;
97217468Seric 	char type;
97317468Seric {
97417468Seric 	static char buf[MAXNAME];
97517468Seric 	static int pid = -1;
97617468Seric 	char c1 = 'A';
97717468Seric 	char c2 = 'A';
97817468Seric 
97917468Seric 	if (e->e_id == NULL)
98017468Seric 	{
98117468Seric 		char qf[20];
98217468Seric 
98317468Seric 		/* find a unique id */
98417468Seric 		if (pid != getpid())
98517468Seric 		{
98617468Seric 			/* new process -- start back at "AA" */
98717468Seric 			pid = getpid();
98817468Seric 			c1 = 'A';
98917468Seric 			c2 = 'A' - 1;
99017468Seric 		}
99117468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
99217468Seric 
99317468Seric 		while (c1 < '~' || c2 < 'Z')
99417468Seric 		{
99517468Seric 			int i;
99617468Seric 
99717468Seric 			if (c2 >= 'Z')
99817468Seric 			{
99917468Seric 				c1++;
100017468Seric 				c2 = 'A' - 1;
100117468Seric 			}
100240934Srick 			qf[2] = c1;
100340934Srick 			qf[3] = ++c2;
100417468Seric 			if (tTd(7, 20))
100540934Srick 				printf("queuename: trying \"%s\"\n", qf);
100617468Seric 
100740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
100840934Srick 			if (i < 0) {
100940934Srick 				if (errno != EEXIST) {
101036233Skarels 					syserr("queuename: Cannot create \"%s\" in \"%s\"",
101140934Srick 						qf, QueueDir);
101236233Skarels 					exit(EX_UNAVAILABLE);
101336233Skarels 				}
101440934Srick 			} else {
101540934Srick 				(void) close(i);
101640934Srick 				break;
101717468Seric 			}
101817468Seric 		}
101917468Seric 		if (c1 >= '~' && c2 >= 'Z')
102017468Seric 		{
102117468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
102217468Seric 				qf, QueueDir);
102317468Seric 			exit(EX_OSERR);
102417468Seric 		}
102517468Seric 		e->e_id = newstr(&qf[2]);
102617468Seric 		define('i', e->e_id, e);
102717468Seric 		if (tTd(7, 1))
102817468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
102917468Seric # ifdef LOG
103017468Seric 		if (LogLevel > 16)
103117468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
103217468Seric # endif LOG
103317468Seric 	}
103417468Seric 
103517468Seric 	if (type == '\0')
103617468Seric 		return (NULL);
103717468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
103817468Seric 	if (tTd(7, 2))
103917468Seric 		printf("queuename: %s\n", buf);
104017468Seric 	return (buf);
104117468Seric }
104217468Seric /*
104317468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
104417468Seric **
104517468Seric **	Parameters:
104617468Seric **		e -- the envelope to unlock.
104717468Seric **
104817468Seric **	Returns:
104917468Seric **		none
105017468Seric **
105117468Seric **	Side Effects:
105217468Seric **		unlocks the queue for `e'.
105317468Seric */
105417468Seric 
105517468Seric unlockqueue(e)
105617468Seric 	ENVELOPE *e;
105717468Seric {
105817468Seric 	/* remove the transcript */
105917468Seric # ifdef LOG
106017468Seric 	if (LogLevel > 19)
106117468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
106217468Seric # endif LOG
106317468Seric 	if (!tTd(51, 4))
106417468Seric 		xunlink(queuename(e, 'x'));
106517468Seric 
106617468Seric }
106740973Sbostic /*
106840973Sbostic **  GETCTLUSER -- return controlling user if mailing to prog or file
106940973Sbostic **
107040973Sbostic **	Check for a "|" or "/" at the beginning of the address.  If
107140973Sbostic **	found, return a controlling username.
107240973Sbostic **
107340973Sbostic **	Parameters:
107440973Sbostic **		a - the address to check out
107540973Sbostic **
107640973Sbostic **	Returns:
107740973Sbostic **		Either NULL, if we werent mailing to a program or file,
107840973Sbostic **		or a controlling user name (possibly in getpwuid's
107940973Sbostic **		static buffer).
108040973Sbostic **
108140973Sbostic **	Side Effects:
108240973Sbostic **		none.
108340973Sbostic */
108440973Sbostic 
108540973Sbostic char *
108640973Sbostic getctluser(a)
108740973Sbostic 	ADDRESS *a;
108840973Sbostic {
108940973Sbostic 	extern ADDRESS *getctladdr();
109040973Sbostic 	struct passwd *pw;
109140973Sbostic 	char *retstr;
109240973Sbostic 
109340973Sbostic 	/*
109440973Sbostic 	**  Get unquoted user for file, program or user.name check.
109540973Sbostic 	**  N.B. remove this code block to always emit controlling
109640973Sbostic 	**  addresses (at the expense of backward compatibility).
109740973Sbostic 	*/
109840973Sbostic 
109940973Sbostic 	{
110040973Sbostic 		char buf[MAXNAME];
110140973Sbostic 		(void) strncpy(buf, a->q_paddr, MAXNAME);
110240973Sbostic 		buf[MAXNAME-1] = '\0';
110340973Sbostic 		stripquotes(buf, TRUE);
110440973Sbostic 
110540973Sbostic 		if (buf[0] != '|' && buf[0] != '/')
110640973Sbostic 			return((char *)NULL);
110740973Sbostic 	}
110840973Sbostic 
110940973Sbostic 	a = getctladdr(a);		/* find controlling address */
111040973Sbostic 
111140973Sbostic 	if (a != NULL && a->q_uid != 0 && (pw = getpwuid(a->q_uid)) != NULL)
111240973Sbostic 		retstr = pw->pw_name;
111340973Sbostic 	else				/* use default user */
111440973Sbostic 		retstr = DefUser;
111540973Sbostic 
111640973Sbostic 	if (tTd(40, 5))
111740973Sbostic 		printf("Set controlling user for `%s' to `%s'\n",
111840973Sbostic 		       (a == NULL)? "<null>": a->q_paddr, retstr);
111940973Sbostic 
112040973Sbostic 	return(retstr);
112140973Sbostic }
112240973Sbostic /*
112340973Sbostic **  SETCTLUSER - sets `CtlUser' to controlling user
112440973Sbostic **  CLRCTLUSER - clears controlling user (no params, nothing returned)
112540973Sbostic **
112640973Sbostic **	These routines manipulate `CtlUser'.
112740973Sbostic **
112840973Sbostic **	Parameters:
112940973Sbostic **		str  - controlling user as passed to setctluser()
113040973Sbostic **
113140973Sbostic **	Returns:
113240973Sbostic **		None.
113340973Sbostic **
113440973Sbostic **	Side Effects:
113540973Sbostic **		`CtlUser' is changed.
113640973Sbostic */
113740973Sbostic 
113840973Sbostic static char CtlUser[MAXNAME];
113940973Sbostic 
114040973Sbostic setctluser(str)
114140973Sbostic register char *str;
114240973Sbostic {
114340973Sbostic 	(void) strncpy(CtlUser, str, MAXNAME);
114440973Sbostic 	CtlUser[MAXNAME-1] = '\0';
114540973Sbostic }
114640973Sbostic 
114740973Sbostic clrctluser()
114840973Sbostic {
114940973Sbostic 	CtlUser[0] = '\0';
115040973Sbostic }
115140973Sbostic 
115240973Sbostic /*
115340973Sbostic **  SETCTLADDR -- create a controlling address
115440973Sbostic **
115540973Sbostic **	If global variable `CtlUser' is set and we are given a valid
115640973Sbostic **	address, make that address a controlling address; change the
115740973Sbostic **	`q_uid', `q_gid', and `q_ruser' fields and set QGOODUID.
115840973Sbostic **
115940973Sbostic **	Parameters:
116040973Sbostic **		a - address for which control uid/gid info may apply
116140973Sbostic **
116240973Sbostic **	Returns:
116340973Sbostic **		None.
116440973Sbostic **
116540973Sbostic **	Side Effects:
116640973Sbostic **		Fills in uid/gid fields in address and sets QGOODUID
116740973Sbostic **		flag if appropriate.
116840973Sbostic */
116940973Sbostic 
117040973Sbostic setctladdr(a)
117140973Sbostic 	ADDRESS *a;
117240973Sbostic {
117340973Sbostic 	struct passwd *pw;
117440973Sbostic 
117540973Sbostic 	/*
117640973Sbostic 	**  If there is no current controlling user, or we were passed a
117740973Sbostic 	**  NULL addr ptr or we already have a controlling user, return.
117840973Sbostic 	*/
117940973Sbostic 
118040973Sbostic 	if (CtlUser[0] == '\0' || a == NULL || a->q_ruser)
118140973Sbostic 		return;
118240973Sbostic 
118340973Sbostic 	/*
118440973Sbostic 	**  Set up addr fields for controlling user.  If `CtlUser' is no
118540973Sbostic 	**  longer valid, use the default user/group.
118640973Sbostic 	*/
118740973Sbostic 
118840973Sbostic 	if ((pw = getpwnam(CtlUser)) != NULL)
118940973Sbostic 	{
119040973Sbostic 		if (a->q_home)
119140973Sbostic 			free(a->q_home);
119240973Sbostic 		a->q_home = newstr(pw->pw_dir);
119340973Sbostic 		a->q_uid = pw->pw_uid;
119440973Sbostic 		a->q_gid = pw->pw_gid;
119540973Sbostic 		a->q_ruser = newstr(CtlUser);
119640973Sbostic 	}
119740973Sbostic 	else
119840973Sbostic 	{
119940973Sbostic 		a->q_uid = DefUid;
120040973Sbostic 		a->q_gid = DefGid;
120140973Sbostic 		a->q_ruser = newstr(DefUser);
120240973Sbostic 	}
120340973Sbostic 
120440973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
120540973Sbostic 
120640973Sbostic 	if (tTd(40, 5))
120740973Sbostic 		printf("Restored controlling user for `%s' to `%s'\n",
120840973Sbostic 		       a->q_paddr, a->q_ruser);
120940973Sbostic }
1210