xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 54975)
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*54975Seric static char sccsid[] = "@(#)queue.c	5.42 (Berkeley) 07/12/92 (with queueing)";
1433731Sbostic #else
15*54975Seric static char sccsid[] = "@(#)queue.c	5.42 (Berkeley) 07/12/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;
7553400Seric 	register char *p;
7610173Seric 	MAILER nullmailer;
7754974Seric 	ADDRESS *lastctladdr;
78*54975Seric 	static ADDRESS *nullctladdr = NULL;
7951920Seric 	char buf[MAXLINE], tf[MAXLINE];
8053400Seric 	extern char *macvalue();
8154974Seric 	extern ADDRESS *getctladdr();
824632Seric 
835037Seric 	/*
84*54975Seric 	**  If we don't have nullctladdr, create one
85*54975Seric 	*/
86*54975Seric 
87*54975Seric 	if (nullctladdr == NULL)
88*54975Seric 	{
89*54975Seric 		nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr);
90*54975Seric 		bzero((char *) nullctladdr, sizeof nullctladdr);
91*54975Seric 	}
92*54975Seric 
93*54975Seric 	/*
9417477Seric 	**  Create control file.
955037Seric 	*/
964632Seric 
9751920Seric 	newid = (e->e_id == NULL);
9851920Seric 	strcpy(tf, queuename(e, 't'));
9951920Seric 	tfp = e->e_lockfp;
10051920Seric 	if (tfp == NULL)
10151920Seric 		newid = FALSE;
10251920Seric 	if (newid)
10351835Seric 	{
10451920Seric 		tfp = e->e_lockfp;
10551920Seric 	}
10651920Seric 	else
10751920Seric 	{
10851920Seric 		/* get a locked tf file */
10951920Seric 		for (i = 100; --i >= 0; )
11051835Seric 		{
11151937Seric # ifdef LOCKF
11251937Seric 			struct flock lfd;
11351937Seric # endif
11451937Seric 
11551920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
11651920Seric 			if (fd < 0)
11751835Seric 			{
11851920Seric 				if (errno == EEXIST)
11951920Seric 					continue;
12051920Seric 				syserr("queueup: cannot create temp file %s", tf);
12151920Seric 				return;
12241636Srick 			}
12351835Seric # ifdef LOCKF
12451937Seric 			lfd.l_type = F_WRLCK;
12551937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
12651937Seric 			if (fcntl(fd, F_SETLK, &lfd) >= 0)
12751920Seric 				break;
12851920Seric 			if (errno != EACCES && errno != EAGAIN)
12951920Seric 				syserr("cannot lockf(%s)", tf);
13051835Seric # else
13151920Seric 			if (flock(fd, LOCK_EX|LOCK_NB) >= 0)
13251920Seric 				break;
13351920Seric 			if (errno != EWOULDBLOCK)
13451920Seric 				syserr("cannot flock(%s)", tf);
13551835Seric # endif
13651920Seric 			close(fd);
13741636Srick 		}
13841636Srick 
13951920Seric 		tfp = fdopen(fd, "w");
14051920Seric 	}
1414632Seric 
1427677Seric 	if (tTd(40, 1))
14317468Seric 		printf("queueing %s\n", e->e_id);
1444632Seric 
1454632Seric 	/*
1466980Seric 	**  If there is no data file yet, create one.
1476980Seric 	*/
1486980Seric 
1496980Seric 	if (e->e_df == NULL)
1506980Seric 	{
1516980Seric 		register FILE *dfp;
1529389Seric 		extern putbody();
1536980Seric 
1547812Seric 		e->e_df = newstr(queuename(e, 'd'));
15540934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
15640934Srick 		if (fd < 0)
1576980Seric 		{
1586980Seric 			syserr("queueup: cannot create %s", e->e_df);
15951920Seric 			if (!newid)
16051920Seric 				(void) fclose(tfp);
16151920Seric 			return;
1626980Seric 		}
16340934Srick 		dfp = fdopen(fd, "w");
16410173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1657009Seric 		(void) fclose(dfp);
1669389Seric 		e->e_putbody = putbody;
1676980Seric 	}
1686980Seric 
1696980Seric 	/*
1704632Seric 	**  Output future work requests.
17125687Seric 	**	Priority and creation time should be first, since
17225687Seric 	**	they are required by orderq.
1734632Seric 	*/
1744632Seric 
1759377Seric 	/* output message priority */
1769377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1779377Seric 
1789630Seric 	/* output creation time */
1799630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1809630Seric 
1814632Seric 	/* output name of data file */
1827812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1834632Seric 
18410108Seric 	/* message from envelope, if it exists */
18510108Seric 	if (e->e_message != NULL)
18610108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18710108Seric 
18853400Seric 	/* $r and $s macro values */
18953400Seric 	if ((p = macvalue('r', e)) != NULL)
19053400Seric 		fprintf(tfp, "$r%s\n", p);
19153400Seric 	if ((p = macvalue('s', e)) != NULL)
19253400Seric 		fprintf(tfp, "$s%s\n", p);
19353400Seric 
1944632Seric 	/* output name of sender */
1957812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1964632Seric 
1974632Seric 	/* output list of recipient addresses */
19854974Seric 	lastctladdr = NULL;
1996980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2004632Seric 	{
20147284Seric 		if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) :
2027763Seric 			       bitset(QQUEUEUP, q->q_flags))
2038245Seric 		{
20454974Seric 			ADDRESS *ctladdr;
20540973Sbostic 
206*54975Seric 			ctladdr = getctladdr(q);
207*54975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
208*54975Seric 				ctladdr = nullctladdr;
209*54975Seric 			if (ctladdr != lastctladdr)
21054974Seric 			{
21154974Seric 				printctladdr(ctladdr, tfp);
21254974Seric 				lastctladdr = ctladdr;
21354974Seric 			}
2147812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2159377Seric 			if (announce)
2169377Seric 			{
2179377Seric 				e->e_to = q->q_paddr;
2189377Seric 				message(Arpa_Info, "queued");
2199377Seric 				if (LogLevel > 4)
22054967Seric 					logdelivery("queued", e);
2219377Seric 				e->e_to = NULL;
2229377Seric 			}
2239387Seric 			if (tTd(40, 1))
2249387Seric 			{
2259387Seric 				printf("queueing ");
2269387Seric 				printaddr(q, FALSE);
2279387Seric 			}
2288245Seric 		}
2294632Seric 	}
2304632Seric 
23125687Seric 	/* output list of error recipients */
23225687Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
23325687Seric 	{
23426504Seric 		if (!bitset(QDONTSEND, q->q_flags))
23540973Sbostic 		{
23654974Seric 			ADDRESS *ctladdr;
23740973Sbostic 
238*54975Seric 			ctladdr = getctladdr(q);
239*54975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
240*54975Seric 				ctladdr = nullctladdr;
241*54975Seric 			if (ctladdr != lastctladdr)
24254974Seric 			{
24354974Seric 				printctladdr(ctladdr, tfp);
24454974Seric 				lastctladdr = ctladdr;
24554974Seric 			}
24626504Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
24740973Sbostic 		}
24825687Seric 	}
24925687Seric 
2509377Seric 	/*
2519377Seric 	**  Output headers for this message.
2529377Seric 	**	Expand macros completely here.  Queue run will deal with
2539377Seric 	**	everything as absolute headers.
2549377Seric 	**		All headers that must be relative to the recipient
2559377Seric 	**		can be cracked later.
25610173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
25710173Seric 	**	no effect on the addresses as they are output.
2589377Seric 	*/
2599377Seric 
26010686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
26110173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
26210349Seric 	nullmailer.m_eol = "\n";
26310173Seric 
26416147Seric 	define('g', "\001f", e);
2656980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2664632Seric 	{
26710686Seric 		extern bool bitzerop();
26810686Seric 
26912015Seric 		/* don't output null headers */
2704632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2714632Seric 			continue;
27212015Seric 
27312015Seric 		/* don't output resent headers on non-resent messages */
27412015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
27512015Seric 			continue;
27612015Seric 
27712015Seric 		/* output this header */
2787812Seric 		fprintf(tfp, "H");
27912015Seric 
28012015Seric 		/* if conditional, output the set of conditions */
28110686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
28210686Seric 		{
28310686Seric 			int j;
28410686Seric 
28523098Seric 			(void) putc('?', tfp);
28610686Seric 			for (j = '\0'; j <= '\177'; j++)
28710686Seric 				if (bitnset(j, h->h_mflags))
28823098Seric 					(void) putc(j, tfp);
28923098Seric 			(void) putc('?', tfp);
29010686Seric 		}
29112015Seric 
29212015Seric 		/* output the header: expand macros, convert addresses */
2937763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2947763Seric 		{
2957763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2968236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2977763Seric 		}
2988245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2999348Seric 		{
3009348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
30110173Seric 				 &nullmailer);
3029348Seric 		}
3037763Seric 		else
3048245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3054632Seric 	}
3064632Seric 
3074632Seric 	/*
3084632Seric 	**  Clean up.
3094632Seric 	*/
3104632Seric 
31151920Seric 	if (!newid)
31251920Seric 	{
31351920Seric 		qf = queuename(e, 'q');
31451920Seric 		if (rename(tf, qf) < 0)
31551920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31651920Seric 		if (e->e_lockfp != NULL)
31751920Seric 			(void) fclose(e->e_lockfp);
31851920Seric 		e->e_lockfp = tfp;
31951920Seric 	}
32051920Seric 	else
32151920Seric 		qf = tf;
32241636Srick 	errno = 0;
3237391Seric 
3247677Seric # ifdef LOG
3257677Seric 	/* save log info */
3267878Seric 	if (LogLevel > 15)
3277878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
3287677Seric # endif LOG
32940934Srick 	fflush(tfp);
33051920Seric 	return;
3314632Seric }
33254974Seric 
33354974Seric printctladdr(a, tfp)
33454974Seric 	ADDRESS *a;
33554974Seric 	FILE *tfp;
33654974Seric {
33754974Seric 	char *u;
33854974Seric 	struct passwd *pw;
33954974Seric 	extern struct passwd *getpwuid();
34054974Seric 
34154974Seric 	if (a == NULL)
34254974Seric 	{
34354974Seric 		fprintf(tfp, "C\n");
34454974Seric 		return;
34554974Seric 	}
34654974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
34754974Seric 		u = DefUser;
34854974Seric 	else
34954974Seric 		u = pw->pw_name;
35054974Seric 	fprintf(tfp, "C%s\n", u);
35154974Seric }
35254974Seric 
3534632Seric /*
3544632Seric **  RUNQUEUE -- run the jobs in the queue.
3554632Seric **
3564632Seric **	Gets the stuff out of the queue in some presumably logical
3574632Seric **	order and processes them.
3584632Seric **
3594632Seric **	Parameters:
36024941Seric **		forkflag -- TRUE if the queue scanning should be done in
36124941Seric **			a child process.  We double-fork so it is not our
36224941Seric **			child and we don't have to clean up after it.
3634632Seric **
3644632Seric **	Returns:
3654632Seric **		none.
3664632Seric **
3674632Seric **	Side Effects:
3684632Seric **		runs things in the mail queue.
3694632Seric */
3704632Seric 
3714639Seric runqueue(forkflag)
3724639Seric 	bool forkflag;
3734632Seric {
37424953Seric 	extern bool shouldqueue();
37524953Seric 
3767466Seric 	/*
37724953Seric 	**  If no work will ever be selected, don't even bother reading
37824953Seric 	**  the queue.
37924953Seric 	*/
38024953Seric 
38151920Seric 	CurrentLA = getla();	/* get load average */
38240934Srick 
38324953Seric 	if (shouldqueue(-100000000L))
38424953Seric 	{
38524953Seric 		if (Verbose)
38624953Seric 			printf("Skipping queue run -- load average too high\n");
38724953Seric 
38824953Seric 		if (forkflag)
38924953Seric 			return;
39024953Seric 		finis();
39124953Seric 	}
39224953Seric 
39324953Seric 	/*
3947466Seric 	**  See if we want to go off and do other useful work.
3957466Seric 	*/
3964639Seric 
3974639Seric 	if (forkflag)
3984639Seric 	{
3997943Seric 		int pid;
4007943Seric 
4017943Seric 		pid = dofork();
4027943Seric 		if (pid != 0)
4034639Seric 		{
40446928Sbostic 			extern void reapchild();
40525184Seric 
4067943Seric 			/* parent -- pick up intermediate zombie */
40725184Seric #ifndef SIGCHLD
4089377Seric 			(void) waitfor(pid);
40925184Seric #else SIGCHLD
41025184Seric 			(void) signal(SIGCHLD, reapchild);
41125184Seric #endif SIGCHLD
4127690Seric 			if (QueueIntvl != 0)
4139348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4144639Seric 			return;
4154639Seric 		}
4167943Seric 		/* child -- double fork */
41725184Seric #ifndef SIGCHLD
4187943Seric 		if (fork() != 0)
4197943Seric 			exit(EX_OK);
42025184Seric #else SIGCHLD
42125184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42225184Seric #endif SIGCHLD
4234639Seric 	}
42424941Seric 
42540934Srick 	setproctitle("running queue: %s", QueueDir);
42624941Seric 
4277876Seric # ifdef LOG
4287876Seric 	if (LogLevel > 11)
4297943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
4307876Seric # endif LOG
4314639Seric 
4327466Seric 	/*
43310205Seric 	**  Release any resources used by the daemon code.
43410205Seric 	*/
43510205Seric 
43610205Seric # ifdef DAEMON
43710205Seric 	clrdaemon();
43810205Seric # endif DAEMON
43910205Seric 
44010205Seric 	/*
44127175Seric 	**  Make sure the alias database is open.
44227175Seric 	*/
44327175Seric 
44427175Seric 	initaliases(AliasFile, FALSE);
44527175Seric 
44627175Seric 	/*
4477466Seric 	**  Start making passes through the queue.
4487466Seric 	**	First, read and sort the entire queue.
4497466Seric 	**	Then, process the work in that order.
4507466Seric 	**		But if you take too long, start over.
4517466Seric 	*/
4527466Seric 
4537943Seric 	/* order the existing work requests */
45424954Seric 	(void) orderq(FALSE);
4557690Seric 
4567943Seric 	/* process them once at a time */
4577943Seric 	while (WorkQ != NULL)
4584639Seric 	{
4597943Seric 		WORK *w = WorkQ;
4607881Seric 
4617943Seric 		WorkQ = WorkQ->w_next;
4627943Seric 		dowork(w);
4637943Seric 		free(w->w_name);
4647943Seric 		free((char *) w);
4654639Seric 	}
46629866Seric 
46729866Seric 	/* exit without the usual cleanup */
46829866Seric 	exit(ExitStat);
4694634Seric }
4704634Seric /*
4714632Seric **  ORDERQ -- order the work queue.
4724632Seric **
4734632Seric **	Parameters:
47424941Seric **		doall -- if set, include everything in the queue (even
47524941Seric **			the jobs that cannot be run because the load
47624941Seric **			average is too high).  Otherwise, exclude those
47724941Seric **			jobs.
4784632Seric **
4794632Seric **	Returns:
48010121Seric **		The number of request in the queue (not necessarily
48110121Seric **		the number of requests in WorkQ however).
4824632Seric **
4834632Seric **	Side Effects:
4844632Seric **		Sets WorkQ to the queue of available work, in order.
4854632Seric */
4864632Seric 
48725687Seric # define NEED_P		001
48825687Seric # define NEED_T		002
4894632Seric 
49024941Seric orderq(doall)
49124941Seric 	bool doall;
4924632Seric {
4936625Sglickman 	register struct direct *d;
4944632Seric 	register WORK *w;
4956625Sglickman 	DIR *f;
4964632Seric 	register int i;
49725687Seric 	WORK wlist[QUEUESIZE+1];
49810070Seric 	int wn = -1;
4994632Seric 	extern workcmpf();
5004632Seric 
5014632Seric 	/* clear out old WorkQ */
5024632Seric 	for (w = WorkQ; w != NULL; )
5034632Seric 	{
5044632Seric 		register WORK *nw = w->w_next;
5054632Seric 
5064632Seric 		WorkQ = nw;
5074632Seric 		free(w->w_name);
5084632Seric 		free((char *) w);
5094632Seric 		w = nw;
5104632Seric 	}
5114632Seric 
5124632Seric 	/* open the queue directory */
5138148Seric 	f = opendir(".");
5144632Seric 	if (f == NULL)
5154632Seric 	{
5168148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
51710070Seric 		return (0);
5184632Seric 	}
5194632Seric 
5204632Seric 	/*
5214632Seric 	**  Read the work directory.
5224632Seric 	*/
5234632Seric 
52410070Seric 	while ((d = readdir(f)) != NULL)
5254632Seric 	{
5269377Seric 		FILE *cf;
5274632Seric 		char lbuf[MAXNAME];
5284632Seric 
5294632Seric 		/* is this an interesting entry? */
5307812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5314632Seric 			continue;
5324632Seric 
53310070Seric 		/* yes -- open control file (if not too many files) */
53425687Seric 		if (++wn >= QUEUESIZE)
53510070Seric 			continue;
5368148Seric 		cf = fopen(d->d_name, "r");
5374632Seric 		if (cf == NULL)
5384632Seric 		{
5397055Seric 			/* this may be some random person sending hir msgs */
5407055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
54110090Seric 			if (tTd(41, 2))
54210090Seric 				printf("orderq: cannot open %s (%d)\n",
54310090Seric 					d->d_name, errno);
5447055Seric 			errno = 0;
54510090Seric 			wn--;
5464632Seric 			continue;
5474632Seric 		}
54825687Seric 		w = &wlist[wn];
54925687Seric 		w->w_name = newstr(d->d_name);
5504632Seric 
55125027Seric 		/* make sure jobs in creation don't clog queue */
55225687Seric 		w->w_pri = 0x7fffffff;
55325687Seric 		w->w_ctime = 0;
55425027Seric 
5554632Seric 		/* extract useful information */
55625687Seric 		i = NEED_P | NEED_T;
55725687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5584632Seric 		{
55924954Seric 			extern long atol();
56024954Seric 
56124941Seric 			switch (lbuf[0])
5624632Seric 			{
56324941Seric 			  case 'P':
56425687Seric 				w->w_pri = atol(&lbuf[1]);
56525687Seric 				i &= ~NEED_P;
5664632Seric 				break;
56725013Seric 
56825013Seric 			  case 'T':
56925687Seric 				w->w_ctime = atol(&lbuf[1]);
57025687Seric 				i &= ~NEED_T;
57125013Seric 				break;
5724632Seric 			}
5734632Seric 		}
5744632Seric 		(void) fclose(cf);
57524953Seric 
57625687Seric 		if (!doall && shouldqueue(w->w_pri))
57724953Seric 		{
57824953Seric 			/* don't even bother sorting this job in */
57924953Seric 			wn--;
58024953Seric 		}
5814632Seric 	}
5826625Sglickman 	(void) closedir(f);
58310090Seric 	wn++;
5844632Seric 
5854632Seric 	/*
5864632Seric 	**  Sort the work directory.
5874632Seric 	*/
5884632Seric 
58925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
5904632Seric 
5914632Seric 	/*
5924632Seric 	**  Convert the work list into canonical form.
5939377Seric 	**	Should be turning it into a list of envelopes here perhaps.
5944632Seric 	*/
5954632Seric 
59624981Seric 	WorkQ = NULL;
59725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
5984632Seric 	{
5994632Seric 		w = (WORK *) xalloc(sizeof *w);
6004632Seric 		w->w_name = wlist[i].w_name;
6014632Seric 		w->w_pri = wlist[i].w_pri;
60225013Seric 		w->w_ctime = wlist[i].w_ctime;
60324981Seric 		w->w_next = WorkQ;
60424981Seric 		WorkQ = w;
6054632Seric 	}
6064632Seric 
6077677Seric 	if (tTd(40, 1))
6084632Seric 	{
6094632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6105037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6114632Seric 	}
61210070Seric 
61310090Seric 	return (wn);
6144632Seric }
6154632Seric /*
6167677Seric **  WORKCMPF -- compare function for ordering work.
6174632Seric **
6184632Seric **	Parameters:
6194632Seric **		a -- the first argument.
6204632Seric **		b -- the second argument.
6214632Seric **
6224632Seric **	Returns:
62324981Seric **		-1 if a < b
62424981Seric **		 0 if a == b
62524981Seric **		+1 if a > b
6264632Seric **
6274632Seric **	Side Effects:
6284632Seric **		none.
6294632Seric */
6304632Seric 
6314632Seric workcmpf(a, b)
6325037Seric 	register WORK *a;
6335037Seric 	register WORK *b;
6344632Seric {
63525013Seric 	long pa = a->w_pri + a->w_ctime;
63625013Seric 	long pb = b->w_pri + b->w_ctime;
63724941Seric 
63824941Seric 	if (pa == pb)
6394632Seric 		return (0);
64024941Seric 	else if (pa > pb)
64124981Seric 		return (1);
64224981Seric 	else
64310121Seric 		return (-1);
6444632Seric }
6454632Seric /*
6464632Seric **  DOWORK -- do a work request.
6474632Seric **
6484632Seric **	Parameters:
6494632Seric **		w -- the work request to be satisfied.
6504632Seric **
6514632Seric **	Returns:
6524632Seric **		none.
6534632Seric **
6544632Seric **	Side Effects:
6554632Seric **		The work request is satisfied if possible.
6564632Seric */
6574632Seric 
6584632Seric dowork(w)
6594632Seric 	register WORK *w;
6604632Seric {
6614632Seric 	register int i;
66224941Seric 	extern bool shouldqueue();
66351920Seric 	extern bool readqf();
6644632Seric 
6657677Seric 	if (tTd(40, 1))
6665037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
6674632Seric 
6684632Seric 	/*
66924941Seric 	**  Ignore jobs that are too expensive for the moment.
6704632Seric 	*/
6714632Seric 
67224941Seric 	if (shouldqueue(w->w_pri))
6734632Seric 	{
67424941Seric 		if (Verbose)
67524967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
6764632Seric 		return;
6774632Seric 	}
6784632Seric 
67924941Seric 	/*
68024941Seric 	**  Fork for work.
68124941Seric 	*/
68224941Seric 
68324941Seric 	if (ForkQueueRuns)
68424941Seric 	{
68524941Seric 		i = fork();
68624941Seric 		if (i < 0)
68724941Seric 		{
68824941Seric 			syserr("dowork: cannot fork");
68924941Seric 			return;
69024941Seric 		}
69124941Seric 	}
69224941Seric 	else
69324941Seric 	{
69424941Seric 		i = 0;
69524941Seric 	}
69624941Seric 
6974632Seric 	if (i == 0)
6984632Seric 	{
6994632Seric 		/*
7004632Seric 		**  CHILD
7018148Seric 		**	Lock the control file to avoid duplicate deliveries.
7028148Seric 		**		Then run the file as though we had just read it.
7037350Seric 		**	We save an idea of the temporary name so we
7047350Seric 		**		can recover on interrupt.
7054632Seric 		*/
7064632Seric 
7077763Seric 		/* set basic modes, etc. */
7087356Seric 		(void) alarm(0);
70925612Seric 		clearenvelope(CurEnv, FALSE);
7104632Seric 		QueueRun = TRUE;
7119377Seric 		ErrorMode = EM_MAIL;
7128148Seric 		CurEnv->e_id = &w->w_name[2];
7137876Seric # ifdef LOG
7147876Seric 		if (LogLevel > 11)
7157881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
7167881Seric 			       getpid());
7177876Seric # endif LOG
7187763Seric 
7197763Seric 		/* don't use the headers from sendmail.cf... */
7207763Seric 		CurEnv->e_header = NULL;
7217763Seric 
72251920Seric 		/* read the queue control file -- return if locked */
72351920Seric 		if (!readqf(CurEnv))
7246980Seric 		{
72524941Seric 			if (ForkQueueRuns)
72624941Seric 				exit(EX_OK);
72724941Seric 			else
72824941Seric 				return;
7296980Seric 		}
7306980Seric 
7319338Seric 		CurEnv->e_flags |= EF_INQUEUE;
7329377Seric 		eatheader(CurEnv);
7336980Seric 
7346980Seric 		/* do the delivery */
7359338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
7369282Seric 			sendall(CurEnv, SM_DELIVER);
7376980Seric 
7386980Seric 		/* finish up and exit */
73924941Seric 		if (ForkQueueRuns)
74024941Seric 			finis();
74124941Seric 		else
74224941Seric 			dropenvelope(CurEnv);
7434632Seric 	}
74424941Seric 	else
74524941Seric 	{
74624941Seric 		/*
74724941Seric 		**  Parent -- pick up results.
74824941Seric 		*/
7494632Seric 
75024941Seric 		errno = 0;
75124941Seric 		(void) waitfor(i);
75224941Seric 	}
7534632Seric }
7544632Seric /*
7554632Seric **  READQF -- read queue file and set up environment.
7564632Seric **
7574632Seric **	Parameters:
7589377Seric **		e -- the envelope of the job to run.
7594632Seric **
7604632Seric **	Returns:
76151920Seric **		TRUE if it successfully read the queue file.
76251920Seric **		FALSE otherwise.
7634632Seric **
7644632Seric **	Side Effects:
76551920Seric **		The queue file is returned locked.
7664632Seric */
7674632Seric 
76851920Seric bool
76951920Seric readqf(e)
7709377Seric 	register ENVELOPE *e;
7714632Seric {
77217477Seric 	char *qf;
77317477Seric 	register FILE *qfp;
77454974Seric 	int fd;
77554974Seric 	ADDRESS *ctladdr;
7767785Seric 	char buf[MAXFIELD];
7779348Seric 	extern char *fgetfolded();
77824954Seric 	extern long atol();
77954974Seric 	extern ADDRESS *setctluser();
78051937Seric # ifdef LOCKF
78151937Seric 	struct flock lfd;
78251937Seric # endif
7834632Seric 
7844632Seric 	/*
78517468Seric 	**  Read and process the file.
7864632Seric 	*/
7874632Seric 
78817477Seric 	qf = queuename(e, 'q');
78951937Seric 	qfp = fopen(qf, "r+");
79017477Seric 	if (qfp == NULL)
79117477Seric 	{
79240934Srick 		if (errno != ENOENT)
79340934Srick 			syserr("readqf: no control file %s", qf);
79451920Seric 		return FALSE;
79517477Seric 	}
79640934Srick 
79751835Seric # ifdef LOCKF
79851937Seric 	lfd.l_type = F_WRLCK;
79951937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
80051937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
80151835Seric # else
80240934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
80351835Seric # endif
80440934Srick 	{
80540934Srick 		/* being processed by another queuer */
80640934Srick 		if (Verbose)
80741636Srick 			printf("%s: locked\n", CurEnv->e_id);
80851920Seric # ifdef LOG
80951920Seric 		if (LogLevel > 9)
81051920Seric 			syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
81140934Srick # endif LOG
81240934Srick 		(void) fclose(qfp);
81351920Seric 		return FALSE;
81440934Srick 	}
81540934Srick 
81651920Seric 	/* save this lock */
81751920Seric 	e->e_lockfp = qfp;
81851920Seric 
81940934Srick 	/* do basic system initialization */
82040934Srick 	initsys();
82140934Srick 
82217477Seric 	FileName = qf;
8239377Seric 	LineNumber = 0;
82451920Seric 	if (Verbose)
8259377Seric 		printf("\nRunning %s\n", e->e_id);
82654974Seric 	ctladdr = NULL;
82717468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
8284632Seric 	{
82926504Seric 		if (tTd(40, 4))
83026504Seric 			printf("+++++ %s\n", buf);
8314632Seric 		switch (buf[0])
8324632Seric 		{
83340973Sbostic 		  case 'C':		/* specify controlling user */
83454974Seric 			ctladdr = setctluser(&buf[1]);
83540973Sbostic 			break;
83640973Sbostic 
8374632Seric 		  case 'R':		/* specify recipient */
83854974Seric 			sendtolist(&buf[1], ctladdr, &e->e_sendqueue);
8394632Seric 			break;
8404632Seric 
84125687Seric 		  case 'E':		/* specify error recipient */
84254974Seric 			sendtolist(&buf[1], ctladdr, &e->e_errorqueue);
84325687Seric 			break;
84425687Seric 
8454632Seric 		  case 'H':		/* header */
84651920Seric 			(void) chompheader(&buf[1], FALSE);
8474632Seric 			break;
8484632Seric 
84910108Seric 		  case 'M':		/* message */
85010108Seric 			e->e_message = newstr(&buf[1]);
85110108Seric 			break;
85210108Seric 
8534632Seric 		  case 'S':		/* sender */
85453182Seric 			setsender(newstr(&buf[1]), CurEnv);
8554632Seric 			break;
8564632Seric 
8574632Seric 		  case 'D':		/* data file name */
8589377Seric 			e->e_df = newstr(&buf[1]);
8599544Seric 			e->e_dfp = fopen(e->e_df, "r");
8609544Seric 			if (e->e_dfp == NULL)
8619377Seric 				syserr("readqf: cannot open %s", e->e_df);
8624632Seric 			break;
8634632Seric 
8647860Seric 		  case 'T':		/* init time */
86524941Seric 			e->e_ctime = atol(&buf[1]);
8664632Seric 			break;
8674632Seric 
8684634Seric 		  case 'P':		/* message priority */
86925008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
8704634Seric 			break;
8714634Seric 
87253400Seric 		  case '$':		/* define macro */
87353400Seric 			define(buf[1], newstr(&buf[2]), e);
87453400Seric 			break;
87553400Seric 
87624941Seric 		  case '\0':		/* blank line; ignore */
87724941Seric 			break;
87824941Seric 
8794632Seric 		  default:
88024941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
88124941Seric 				LineNumber, buf);
8824632Seric 			break;
8834632Seric 		}
8844632Seric 	}
8859377Seric 
8869377Seric 	FileName = NULL;
88724941Seric 
88824941Seric 	/*
88924941Seric 	**  If we haven't read any lines, this queue file is empty.
89024941Seric 	**  Arrange to remove it without referencing any null pointers.
89124941Seric 	*/
89224941Seric 
89324941Seric 	if (LineNumber == 0)
89424941Seric 	{
89524941Seric 		errno = 0;
89624941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
89724941Seric 	}
89851920Seric 	return TRUE;
8994632Seric }
9004632Seric /*
9019630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9029630Seric **
9039630Seric **	Parameters:
9049630Seric **		none.
9059630Seric **
9069630Seric **	Returns:
9079630Seric **		none.
9089630Seric **
9099630Seric **	Side Effects:
9109630Seric **		Prints a listing of the mail queue on the standard output.
9119630Seric */
9125182Seric 
9139630Seric printqueue()
9149630Seric {
9159630Seric 	register WORK *w;
9169630Seric 	FILE *f;
91710070Seric 	int nrequests;
9189630Seric 	char buf[MAXLINE];
9199630Seric 
9209630Seric 	/*
9219630Seric 	**  Read and order the queue.
9229630Seric 	*/
9239630Seric 
92424941Seric 	nrequests = orderq(TRUE);
9259630Seric 
9269630Seric 	/*
9279630Seric 	**  Print the work list that we have read.
9289630Seric 	*/
9299630Seric 
9309630Seric 	/* first see if there is anything */
93110070Seric 	if (nrequests <= 0)
9329630Seric 	{
93310070Seric 		printf("Mail queue is empty\n");
9349630Seric 		return;
9359630Seric 	}
9369630Seric 
93751920Seric 	CurrentLA = getla();	/* get load average */
93840934Srick 
93910096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
94025687Seric 	if (nrequests > QUEUESIZE)
94125687Seric 		printf(", only %d printed", QUEUESIZE);
94224979Seric 	if (Verbose)
94325032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
94424979Seric 	else
94524979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
9469630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
9479630Seric 	{
9489630Seric 		struct stat st;
94910070Seric 		auto time_t submittime = 0;
95010070Seric 		long dfsize = -1;
95110108Seric 		char message[MAXLINE];
95251937Seric # ifdef LOCKF
95351937Seric 		struct flock lfd;
95451937Seric # endif
95524941Seric 		extern bool shouldqueue();
9569630Seric 
95717468Seric 		f = fopen(w->w_name, "r");
95817468Seric 		if (f == NULL)
95917468Seric 		{
96017468Seric 			errno = 0;
96117468Seric 			continue;
96217468Seric 		}
9639630Seric 		printf("%7s", w->w_name + 2);
96451835Seric # ifdef LOCKF
96551937Seric 		lfd.l_type = F_RDLCK;
96651937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
96751937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
96851835Seric # else
96940934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
97051835Seric # endif
97110070Seric 			printf("*");
97224941Seric 		else if (shouldqueue(w->w_pri))
97324941Seric 			printf("X");
97410070Seric 		else
97510070Seric 			printf(" ");
97610070Seric 		errno = 0;
97717468Seric 
97810108Seric 		message[0] = '\0';
9799630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
9809630Seric 		{
98153400Seric 			register int i;
98253400Seric 
9839630Seric 			fixcrlf(buf, TRUE);
9849630Seric 			switch (buf[0])
9859630Seric 			{
98610108Seric 			  case 'M':	/* error message */
98753400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
98853400Seric 					i = sizeof message;
98953400Seric 				bcopy(&buf[1], message, i);
99053400Seric 				message[i] = '\0';
99110108Seric 				break;
99210108Seric 
9939630Seric 			  case 'S':	/* sender name */
99424979Seric 				if (Verbose)
99525027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
99625027Seric 					    w->w_pri, ctime(&submittime) + 4,
99724979Seric 					    &buf[1]);
99824979Seric 				else
99924979Seric 					printf("%8ld %.16s %.45s", dfsize,
100024979Seric 					    ctime(&submittime), &buf[1]);
100110108Seric 				if (message[0] != '\0')
100225027Seric 					printf("\n\t\t (%.60s)", message);
10039630Seric 				break;
100451920Seric 
100540973Sbostic 			  case 'C':	/* controlling user */
100654974Seric 				if (Verbose)
1007*54975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
100840973Sbostic 				break;
10099630Seric 
10109630Seric 			  case 'R':	/* recipient name */
101124979Seric 				if (Verbose)
101225027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
101324979Seric 				else
101424979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
10159630Seric 				break;
10169630Seric 
10179630Seric 			  case 'T':	/* creation time */
101824941Seric 				submittime = atol(&buf[1]);
10199630Seric 				break;
102010070Seric 
102110070Seric 			  case 'D':	/* data file name */
102210070Seric 				if (stat(&buf[1], &st) >= 0)
102310070Seric 					dfsize = st.st_size;
102410070Seric 				break;
10259630Seric 			}
10269630Seric 		}
102710070Seric 		if (submittime == (time_t) 0)
102810070Seric 			printf(" (no control file)");
10299630Seric 		printf("\n");
103023098Seric 		(void) fclose(f);
10319630Seric 	}
10329630Seric }
10339630Seric 
10345182Seric # endif QUEUE
103517468Seric /*
103617468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
103717468Seric **
103817468Seric **	Assigns an id code if one does not already exist.
103917468Seric **	This code is very careful to avoid trashing existing files
104017468Seric **	under any circumstances.
104117468Seric **
104217468Seric **	Parameters:
104317468Seric **		e -- envelope to build it in/from.
104417468Seric **		type -- the file type, used as the first character
104517468Seric **			of the file name.
104617468Seric **
104717468Seric **	Returns:
104817468Seric **		a pointer to the new file name (in a static buffer).
104917468Seric **
105017468Seric **	Side Effects:
105151920Seric **		If no id code is already assigned, queuename will
105251920Seric **		assign an id code, create a qf file, and leave a
105351920Seric **		locked, open-for-write file pointer in the envelope.
105417468Seric */
105517468Seric 
105617468Seric char *
105717468Seric queuename(e, type)
105817468Seric 	register ENVELOPE *e;
105917468Seric 	char type;
106017468Seric {
106117468Seric 	static char buf[MAXNAME];
106217468Seric 	static int pid = -1;
106317468Seric 	char c1 = 'A';
106417468Seric 	char c2 = 'A';
106517468Seric 
106617468Seric 	if (e->e_id == NULL)
106717468Seric 	{
106817468Seric 		char qf[20];
106917468Seric 
107017468Seric 		/* find a unique id */
107117468Seric 		if (pid != getpid())
107217468Seric 		{
107317468Seric 			/* new process -- start back at "AA" */
107417468Seric 			pid = getpid();
107517468Seric 			c1 = 'A';
107617468Seric 			c2 = 'A' - 1;
107717468Seric 		}
107817468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
107917468Seric 
108017468Seric 		while (c1 < '~' || c2 < 'Z')
108117468Seric 		{
108217468Seric 			int i;
108351937Seric # ifdef LOCKF
108451937Seric 			struct flock lfd;
108551937Seric # endif
108617468Seric 
108717468Seric 			if (c2 >= 'Z')
108817468Seric 			{
108917468Seric 				c1++;
109017468Seric 				c2 = 'A' - 1;
109117468Seric 			}
109240934Srick 			qf[2] = c1;
109340934Srick 			qf[3] = ++c2;
109417468Seric 			if (tTd(7, 20))
109540934Srick 				printf("queuename: trying \"%s\"\n", qf);
109617468Seric 
109740934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
109851920Seric 			if (i < 0)
109951920Seric 			{
110051920Seric 				if (errno == EEXIST)
110151920Seric 					continue;
110251920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
110351920Seric 					qf, QueueDir);
110451920Seric 				exit(EX_UNAVAILABLE);
110551920Seric 			}
110651920Seric # ifdef LOCKF
110751937Seric 			lfd.l_type = F_WRLCK;
110851937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
110951937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
111051920Seric # else
111151920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
111251920Seric # endif
111351920Seric 			{
111451920Seric 				e->e_lockfp = fdopen(i, "w");
111540934Srick 				break;
111617468Seric 			}
111751920Seric 
111851920Seric 			/* a reader got the file; abandon it and try again */
111951920Seric 			(void) close(i);
112017468Seric 		}
112117468Seric 		if (c1 >= '~' && c2 >= 'Z')
112217468Seric 		{
112317468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
112417468Seric 				qf, QueueDir);
112517468Seric 			exit(EX_OSERR);
112617468Seric 		}
112717468Seric 		e->e_id = newstr(&qf[2]);
112817468Seric 		define('i', e->e_id, e);
112917468Seric 		if (tTd(7, 1))
113017468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
113117468Seric # ifdef LOG
113217468Seric 		if (LogLevel > 16)
113317468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
113417468Seric # endif LOG
113517468Seric 	}
113617468Seric 
113717468Seric 	if (type == '\0')
113817468Seric 		return (NULL);
113917468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
114017468Seric 	if (tTd(7, 2))
114117468Seric 		printf("queuename: %s\n", buf);
114217468Seric 	return (buf);
114317468Seric }
114417468Seric /*
114517468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
114617468Seric **
114717468Seric **	Parameters:
114817468Seric **		e -- the envelope to unlock.
114917468Seric **
115017468Seric **	Returns:
115117468Seric **		none
115217468Seric **
115317468Seric **	Side Effects:
115417468Seric **		unlocks the queue for `e'.
115517468Seric */
115617468Seric 
115717468Seric unlockqueue(e)
115817468Seric 	ENVELOPE *e;
115917468Seric {
116051920Seric 	/* if there is a lock file in the envelope, close it */
116151920Seric 	if (e->e_lockfp != NULL)
116251920Seric 		fclose(e->e_lockfp);
116351920Seric 	e->e_lockfp = NULL;
116451920Seric 
116517468Seric 	/* remove the transcript */
116617468Seric # ifdef LOG
116717468Seric 	if (LogLevel > 19)
116817468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
116917468Seric # endif LOG
117017468Seric 	if (!tTd(51, 4))
117117468Seric 		xunlink(queuename(e, 'x'));
117217468Seric 
117317468Seric }
117440973Sbostic /*
117554974Seric **  SETCTLUSER -- create a controlling address
117640973Sbostic **
117754974Seric **	Create a fake "address" given only a local login name; this is
117854974Seric **	used as a "controlling user" for future recipient addresses.
117940973Sbostic **
118040973Sbostic **	Parameters:
118154974Seric **		user -- the user name of the controlling user.
118240973Sbostic **
118340973Sbostic **	Returns:
118454974Seric **		An address descriptor for the controlling user.
118540973Sbostic **
118640973Sbostic **	Side Effects:
118740973Sbostic **		none.
118840973Sbostic */
118940973Sbostic 
119054974Seric ADDRESS *
119154974Seric setctluser(user)
119254974Seric 	char *user;
119340973Sbostic {
119454974Seric 	register ADDRESS *a;
119540973Sbostic 	struct passwd *pw;
119640973Sbostic 
119740973Sbostic 	/*
119854974Seric 	**  See if this clears our concept of controlling user.
119940973Sbostic 	*/
120040973Sbostic 
120154974Seric 	if (user == NULL || *user == '\0')
120254974Seric 		return NULL;
120340973Sbostic 
120440973Sbostic 	/*
120554974Seric 	**  Set up addr fields for controlling user.
120640973Sbostic 	*/
120740973Sbostic 
120854974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
120954974Seric 	bzero((char *) a, sizeof *a);
121054974Seric 	if ((pw = getpwnam(user)) != NULL)
121140973Sbostic 	{
121240973Sbostic 		a->q_home = newstr(pw->pw_dir);
121340973Sbostic 		a->q_uid = pw->pw_uid;
121440973Sbostic 		a->q_gid = pw->pw_gid;
121554974Seric 		a->q_ruser = newstr(user);
121640973Sbostic 	}
121740973Sbostic 	else
121840973Sbostic 	{
121940973Sbostic 		a->q_uid = DefUid;
122040973Sbostic 		a->q_gid = DefGid;
122140973Sbostic 		a->q_ruser = newstr(DefUser);
122240973Sbostic 	}
122340973Sbostic 
122440973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
122554974Seric 	return a;
122640973Sbostic }
1227