xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 55173)
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*55173Seric static char sccsid[] = "@(#)queue.c	5.45 (Berkeley) 07/13/92 (with queueing)";
1433731Sbostic #else
15*55173Seric static char sccsid[] = "@(#)queue.c	5.45 (Berkeley) 07/13/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;
7854975Seric 	static ADDRESS *nullctladdr = NULL;
7951920Seric 	char buf[MAXLINE], tf[MAXLINE];
8053400Seric 	extern char *macvalue();
8154974Seric 	extern ADDRESS *getctladdr();
824632Seric 
835037Seric 	/*
8454975Seric 	**  If we don't have nullctladdr, create one
8554975Seric 	*/
8654975Seric 
8754975Seric 	if (nullctladdr == NULL)
8854975Seric 	{
8954975Seric 		nullctladdr = (ADDRESS *) xalloc(sizeof *nullctladdr);
9054975Seric 		bzero((char *) nullctladdr, sizeof nullctladdr);
9154975Seric 	}
9254975Seric 
9354975Seric 	/*
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 
20654975Seric 			ctladdr = getctladdr(q);
20754975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
20854975Seric 				ctladdr = nullctladdr;
20954975Seric 			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 
23854975Seric 			ctladdr = getctladdr(q);
23954975Seric 			if (ctladdr == NULL && q->q_alias != NULL)
24054975Seric 				ctladdr = nullctladdr;
24154975Seric 			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 		{
30055012Seric 			commaize(h, h->h_value, tfp,
30155012Seric 				 bitset(EF_OLDSTYLE, e->e_flags),
30255012Seric 				 &nullmailer, e);
3039348Seric 		}
3047763Seric 		else
3058245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3064632Seric 	}
3074632Seric 
3084632Seric 	/*
3094632Seric 	**  Clean up.
3104632Seric 	*/
3114632Seric 
31251920Seric 	if (!newid)
31351920Seric 	{
31451920Seric 		qf = queuename(e, 'q');
31551920Seric 		if (rename(tf, qf) < 0)
31651920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
31751920Seric 		if (e->e_lockfp != NULL)
31851920Seric 			(void) fclose(e->e_lockfp);
31951920Seric 		e->e_lockfp = tfp;
32051920Seric 	}
32151920Seric 	else
32251920Seric 		qf = tf;
32341636Srick 	errno = 0;
3247391Seric 
3257677Seric # ifdef LOG
3267677Seric 	/* save log info */
3277878Seric 	if (LogLevel > 15)
3287878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
3297677Seric # endif LOG
33040934Srick 	fflush(tfp);
33151920Seric 	return;
3324632Seric }
33354974Seric 
33454974Seric printctladdr(a, tfp)
33554974Seric 	ADDRESS *a;
33654974Seric 	FILE *tfp;
33754974Seric {
33854974Seric 	char *u;
33954974Seric 	struct passwd *pw;
34054974Seric 	extern struct passwd *getpwuid();
34154974Seric 
34254974Seric 	if (a == NULL)
34354974Seric 	{
34454974Seric 		fprintf(tfp, "C\n");
34554974Seric 		return;
34654974Seric 	}
34754974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
34854974Seric 		u = DefUser;
34954974Seric 	else
35054974Seric 		u = pw->pw_name;
35154974Seric 	fprintf(tfp, "C%s\n", u);
35254974Seric }
35354974Seric 
3544632Seric /*
3554632Seric **  RUNQUEUE -- run the jobs in the queue.
3564632Seric **
3574632Seric **	Gets the stuff out of the queue in some presumably logical
3584632Seric **	order and processes them.
3594632Seric **
3604632Seric **	Parameters:
36124941Seric **		forkflag -- TRUE if the queue scanning should be done in
36224941Seric **			a child process.  We double-fork so it is not our
36324941Seric **			child and we don't have to clean up after it.
3644632Seric **
3654632Seric **	Returns:
3664632Seric **		none.
3674632Seric **
3684632Seric **	Side Effects:
3694632Seric **		runs things in the mail queue.
3704632Seric */
3714632Seric 
37255012Seric runqueue(forkflag, e)
3734639Seric 	bool forkflag;
37455012Seric 	register ENVELOPE *e;
3754632Seric {
37624953Seric 	extern bool shouldqueue();
37724953Seric 
3787466Seric 	/*
37924953Seric 	**  If no work will ever be selected, don't even bother reading
38024953Seric 	**  the queue.
38124953Seric 	*/
38224953Seric 
38351920Seric 	CurrentLA = getla();	/* get load average */
38440934Srick 
38524953Seric 	if (shouldqueue(-100000000L))
38624953Seric 	{
38724953Seric 		if (Verbose)
38824953Seric 			printf("Skipping queue run -- load average too high\n");
38924953Seric 
39024953Seric 		if (forkflag)
39124953Seric 			return;
39224953Seric 		finis();
39324953Seric 	}
39424953Seric 
39524953Seric 	/*
3967466Seric 	**  See if we want to go off and do other useful work.
3977466Seric 	*/
3984639Seric 
3994639Seric 	if (forkflag)
4004639Seric 	{
4017943Seric 		int pid;
4027943Seric 
4037943Seric 		pid = dofork();
4047943Seric 		if (pid != 0)
4054639Seric 		{
40646928Sbostic 			extern void reapchild();
40725184Seric 
4087943Seric 			/* parent -- pick up intermediate zombie */
40925184Seric #ifndef SIGCHLD
4109377Seric 			(void) waitfor(pid);
41125184Seric #else SIGCHLD
41225184Seric 			(void) signal(SIGCHLD, reapchild);
41325184Seric #endif SIGCHLD
4147690Seric 			if (QueueIntvl != 0)
4159348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4164639Seric 			return;
4174639Seric 		}
4187943Seric 		/* child -- double fork */
41925184Seric #ifndef SIGCHLD
4207943Seric 		if (fork() != 0)
4217943Seric 			exit(EX_OK);
42225184Seric #else SIGCHLD
42325184Seric 		(void) signal(SIGCHLD, SIG_DFL);
42425184Seric #endif SIGCHLD
4254639Seric 	}
42624941Seric 
42740934Srick 	setproctitle("running queue: %s", QueueDir);
42824941Seric 
4297876Seric # ifdef LOG
4307876Seric 	if (LogLevel > 11)
4317943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
4327876Seric # endif LOG
4334639Seric 
4347466Seric 	/*
43510205Seric 	**  Release any resources used by the daemon code.
43610205Seric 	*/
43710205Seric 
43810205Seric # ifdef DAEMON
43910205Seric 	clrdaemon();
44010205Seric # endif DAEMON
44110205Seric 
44210205Seric 	/*
44327175Seric 	**  Make sure the alias database is open.
44427175Seric 	*/
44527175Seric 
44655012Seric 	initaliases(AliasFile, FALSE, e);
44727175Seric 
44827175Seric 	/*
4497466Seric 	**  Start making passes through the queue.
4507466Seric 	**	First, read and sort the entire queue.
4517466Seric 	**	Then, process the work in that order.
4527466Seric 	**		But if you take too long, start over.
4537466Seric 	*/
4547466Seric 
4557943Seric 	/* order the existing work requests */
45624954Seric 	(void) orderq(FALSE);
4577690Seric 
4587943Seric 	/* process them once at a time */
4597943Seric 	while (WorkQ != NULL)
4604639Seric 	{
4617943Seric 		WORK *w = WorkQ;
4627881Seric 
4637943Seric 		WorkQ = WorkQ->w_next;
46455012Seric 		dowork(w, e);
4657943Seric 		free(w->w_name);
4667943Seric 		free((char *) w);
4674639Seric 	}
46829866Seric 
46929866Seric 	/* exit without the usual cleanup */
47029866Seric 	exit(ExitStat);
4714634Seric }
4724634Seric /*
4734632Seric **  ORDERQ -- order the work queue.
4744632Seric **
4754632Seric **	Parameters:
47624941Seric **		doall -- if set, include everything in the queue (even
47724941Seric **			the jobs that cannot be run because the load
47824941Seric **			average is too high).  Otherwise, exclude those
47924941Seric **			jobs.
4804632Seric **
4814632Seric **	Returns:
48210121Seric **		The number of request in the queue (not necessarily
48310121Seric **		the number of requests in WorkQ however).
4844632Seric **
4854632Seric **	Side Effects:
4864632Seric **		Sets WorkQ to the queue of available work, in order.
4874632Seric */
4884632Seric 
48925687Seric # define NEED_P		001
49025687Seric # define NEED_T		002
4914632Seric 
49224941Seric orderq(doall)
49324941Seric 	bool doall;
4944632Seric {
4956625Sglickman 	register struct direct *d;
4964632Seric 	register WORK *w;
4976625Sglickman 	DIR *f;
4984632Seric 	register int i;
49925687Seric 	WORK wlist[QUEUESIZE+1];
50010070Seric 	int wn = -1;
5014632Seric 	extern workcmpf();
5024632Seric 
5034632Seric 	/* clear out old WorkQ */
5044632Seric 	for (w = WorkQ; w != NULL; )
5054632Seric 	{
5064632Seric 		register WORK *nw = w->w_next;
5074632Seric 
5084632Seric 		WorkQ = nw;
5094632Seric 		free(w->w_name);
5104632Seric 		free((char *) w);
5114632Seric 		w = nw;
5124632Seric 	}
5134632Seric 
5144632Seric 	/* open the queue directory */
5158148Seric 	f = opendir(".");
5164632Seric 	if (f == NULL)
5174632Seric 	{
5188148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
51910070Seric 		return (0);
5204632Seric 	}
5214632Seric 
5224632Seric 	/*
5234632Seric 	**  Read the work directory.
5244632Seric 	*/
5254632Seric 
52610070Seric 	while ((d = readdir(f)) != NULL)
5274632Seric 	{
5289377Seric 		FILE *cf;
5294632Seric 		char lbuf[MAXNAME];
5304632Seric 
5314632Seric 		/* is this an interesting entry? */
5327812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5334632Seric 			continue;
5344632Seric 
53510070Seric 		/* yes -- open control file (if not too many files) */
53625687Seric 		if (++wn >= QUEUESIZE)
53710070Seric 			continue;
5388148Seric 		cf = fopen(d->d_name, "r");
5394632Seric 		if (cf == NULL)
5404632Seric 		{
5417055Seric 			/* this may be some random person sending hir msgs */
5427055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
54310090Seric 			if (tTd(41, 2))
54410090Seric 				printf("orderq: cannot open %s (%d)\n",
54510090Seric 					d->d_name, errno);
5467055Seric 			errno = 0;
54710090Seric 			wn--;
5484632Seric 			continue;
5494632Seric 		}
55025687Seric 		w = &wlist[wn];
55125687Seric 		w->w_name = newstr(d->d_name);
5524632Seric 
55325027Seric 		/* make sure jobs in creation don't clog queue */
55425687Seric 		w->w_pri = 0x7fffffff;
55525687Seric 		w->w_ctime = 0;
55625027Seric 
5574632Seric 		/* extract useful information */
55825687Seric 		i = NEED_P | NEED_T;
55925687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5604632Seric 		{
56124954Seric 			extern long atol();
56224954Seric 
56324941Seric 			switch (lbuf[0])
5644632Seric 			{
56524941Seric 			  case 'P':
56625687Seric 				w->w_pri = atol(&lbuf[1]);
56725687Seric 				i &= ~NEED_P;
5684632Seric 				break;
56925013Seric 
57025013Seric 			  case 'T':
57125687Seric 				w->w_ctime = atol(&lbuf[1]);
57225687Seric 				i &= ~NEED_T;
57325013Seric 				break;
5744632Seric 			}
5754632Seric 		}
5764632Seric 		(void) fclose(cf);
57724953Seric 
57825687Seric 		if (!doall && shouldqueue(w->w_pri))
57924953Seric 		{
58024953Seric 			/* don't even bother sorting this job in */
58124953Seric 			wn--;
58224953Seric 		}
5834632Seric 	}
5846625Sglickman 	(void) closedir(f);
58510090Seric 	wn++;
5864632Seric 
5874632Seric 	/*
5884632Seric 	**  Sort the work directory.
5894632Seric 	*/
5904632Seric 
59125687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
5924632Seric 
5934632Seric 	/*
5944632Seric 	**  Convert the work list into canonical form.
5959377Seric 	**	Should be turning it into a list of envelopes here perhaps.
5964632Seric 	*/
5974632Seric 
59824981Seric 	WorkQ = NULL;
59925687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
6004632Seric 	{
6014632Seric 		w = (WORK *) xalloc(sizeof *w);
6024632Seric 		w->w_name = wlist[i].w_name;
6034632Seric 		w->w_pri = wlist[i].w_pri;
60425013Seric 		w->w_ctime = wlist[i].w_ctime;
60524981Seric 		w->w_next = WorkQ;
60624981Seric 		WorkQ = w;
6074632Seric 	}
6084632Seric 
6097677Seric 	if (tTd(40, 1))
6104632Seric 	{
6114632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
6125037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
6134632Seric 	}
61410070Seric 
61510090Seric 	return (wn);
6164632Seric }
6174632Seric /*
6187677Seric **  WORKCMPF -- compare function for ordering work.
6194632Seric **
6204632Seric **	Parameters:
6214632Seric **		a -- the first argument.
6224632Seric **		b -- the second argument.
6234632Seric **
6244632Seric **	Returns:
62524981Seric **		-1 if a < b
62624981Seric **		 0 if a == b
62724981Seric **		+1 if a > b
6284632Seric **
6294632Seric **	Side Effects:
6304632Seric **		none.
6314632Seric */
6324632Seric 
6334632Seric workcmpf(a, b)
6345037Seric 	register WORK *a;
6355037Seric 	register WORK *b;
6364632Seric {
63725013Seric 	long pa = a->w_pri + a->w_ctime;
63825013Seric 	long pb = b->w_pri + b->w_ctime;
63924941Seric 
64024941Seric 	if (pa == pb)
6414632Seric 		return (0);
64224941Seric 	else if (pa > pb)
64324981Seric 		return (1);
64424981Seric 	else
64510121Seric 		return (-1);
6464632Seric }
6474632Seric /*
6484632Seric **  DOWORK -- do a work request.
6494632Seric **
6504632Seric **	Parameters:
6514632Seric **		w -- the work request to be satisfied.
6524632Seric **
6534632Seric **	Returns:
6544632Seric **		none.
6554632Seric **
6564632Seric **	Side Effects:
6574632Seric **		The work request is satisfied if possible.
6584632Seric */
6594632Seric 
66055012Seric dowork(w, e)
6614632Seric 	register WORK *w;
66255012Seric 	register ENVELOPE *e;
6634632Seric {
6644632Seric 	register int i;
66524941Seric 	extern bool shouldqueue();
66651920Seric 	extern bool readqf();
6674632Seric 
6687677Seric 	if (tTd(40, 1))
6695037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
6704632Seric 
6714632Seric 	/*
67224941Seric 	**  Ignore jobs that are too expensive for the moment.
6734632Seric 	*/
6744632Seric 
67524941Seric 	if (shouldqueue(w->w_pri))
6764632Seric 	{
67724941Seric 		if (Verbose)
67824967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
6794632Seric 		return;
6804632Seric 	}
6814632Seric 
68224941Seric 	/*
68324941Seric 	**  Fork for work.
68424941Seric 	*/
68524941Seric 
68624941Seric 	if (ForkQueueRuns)
68724941Seric 	{
68824941Seric 		i = fork();
68924941Seric 		if (i < 0)
69024941Seric 		{
69124941Seric 			syserr("dowork: cannot fork");
69224941Seric 			return;
69324941Seric 		}
69424941Seric 	}
69524941Seric 	else
69624941Seric 	{
69724941Seric 		i = 0;
69824941Seric 	}
69924941Seric 
7004632Seric 	if (i == 0)
7014632Seric 	{
7024632Seric 		/*
7034632Seric 		**  CHILD
7048148Seric 		**	Lock the control file to avoid duplicate deliveries.
7058148Seric 		**		Then run the file as though we had just read it.
7067350Seric 		**	We save an idea of the temporary name so we
7077350Seric 		**		can recover on interrupt.
7084632Seric 		*/
7094632Seric 
7107763Seric 		/* set basic modes, etc. */
7117356Seric 		(void) alarm(0);
71255012Seric 		clearenvelope(e, FALSE);
7134632Seric 		QueueRun = TRUE;
7149377Seric 		ErrorMode = EM_MAIL;
71555012Seric 		e->e_id = &w->w_name[2];
7167876Seric # ifdef LOG
7177876Seric 		if (LogLevel > 11)
71855012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
7197881Seric 			       getpid());
7207876Seric # endif LOG
7217763Seric 
7227763Seric 		/* don't use the headers from sendmail.cf... */
72355012Seric 		e->e_header = NULL;
7247763Seric 
72551920Seric 		/* read the queue control file -- return if locked */
72655012Seric 		if (!readqf(e))
7276980Seric 		{
72824941Seric 			if (ForkQueueRuns)
72924941Seric 				exit(EX_OK);
73024941Seric 			else
73124941Seric 				return;
7326980Seric 		}
7336980Seric 
73455012Seric 		e->e_flags |= EF_INQUEUE;
73555012Seric 		eatheader(e);
7366980Seric 
7376980Seric 		/* do the delivery */
73855012Seric 		if (!bitset(EF_FATALERRS, e->e_flags))
73955012Seric 			sendall(e, SM_DELIVER);
7406980Seric 
7416980Seric 		/* finish up and exit */
74224941Seric 		if (ForkQueueRuns)
74324941Seric 			finis();
74424941Seric 		else
74555012Seric 			dropenvelope(e);
7464632Seric 	}
74724941Seric 	else
74824941Seric 	{
74924941Seric 		/*
75024941Seric 		**  Parent -- pick up results.
75124941Seric 		*/
7524632Seric 
75324941Seric 		errno = 0;
75424941Seric 		(void) waitfor(i);
75524941Seric 	}
7564632Seric }
7574632Seric /*
7584632Seric **  READQF -- read queue file and set up environment.
7594632Seric **
7604632Seric **	Parameters:
7619377Seric **		e -- the envelope of the job to run.
7624632Seric **
7634632Seric **	Returns:
76451920Seric **		TRUE if it successfully read the queue file.
76551920Seric **		FALSE otherwise.
7664632Seric **
7674632Seric **	Side Effects:
76851920Seric **		The queue file is returned locked.
7694632Seric */
7704632Seric 
77151920Seric bool
77251920Seric readqf(e)
7739377Seric 	register ENVELOPE *e;
7744632Seric {
77517477Seric 	char *qf;
77617477Seric 	register FILE *qfp;
77754974Seric 	ADDRESS *ctladdr;
7787785Seric 	char buf[MAXFIELD];
7799348Seric 	extern char *fgetfolded();
78024954Seric 	extern long atol();
78154974Seric 	extern ADDRESS *setctluser();
78251937Seric # ifdef LOCKF
78351937Seric 	struct flock lfd;
78451937Seric # endif
7854632Seric 
7864632Seric 	/*
78717468Seric 	**  Read and process the file.
7884632Seric 	*/
7894632Seric 
79017477Seric 	qf = queuename(e, 'q');
79151937Seric 	qfp = fopen(qf, "r+");
79217477Seric 	if (qfp == NULL)
79317477Seric 	{
79440934Srick 		if (errno != ENOENT)
79540934Srick 			syserr("readqf: no control file %s", qf);
79651920Seric 		return FALSE;
79717477Seric 	}
79840934Srick 
79951835Seric # ifdef LOCKF
80051937Seric 	lfd.l_type = F_WRLCK;
80151937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
80251937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
80351835Seric # else
80440934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
80551835Seric # endif
80640934Srick 	{
80740934Srick 		/* being processed by another queuer */
80840934Srick 		if (Verbose)
80955012Seric 			printf("%s: locked\n", e->e_id);
81051920Seric # ifdef LOG
811*55173Seric 		if (LogLevel > 10)
81255012Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
81340934Srick # endif LOG
81440934Srick 		(void) fclose(qfp);
81551920Seric 		return FALSE;
81640934Srick 	}
81740934Srick 
81851920Seric 	/* save this lock */
81951920Seric 	e->e_lockfp = qfp;
82051920Seric 
82140934Srick 	/* do basic system initialization */
82255012Seric 	initsys(e);
82340934Srick 
82417477Seric 	FileName = qf;
8259377Seric 	LineNumber = 0;
82651920Seric 	if (Verbose)
8279377Seric 		printf("\nRunning %s\n", e->e_id);
82854974Seric 	ctladdr = NULL;
82917468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
8304632Seric 	{
83126504Seric 		if (tTd(40, 4))
83226504Seric 			printf("+++++ %s\n", buf);
8334632Seric 		switch (buf[0])
8344632Seric 		{
83540973Sbostic 		  case 'C':		/* specify controlling user */
83654974Seric 			ctladdr = setctluser(&buf[1]);
83740973Sbostic 			break;
83840973Sbostic 
8394632Seric 		  case 'R':		/* specify recipient */
84055012Seric 			sendtolist(&buf[1], ctladdr, &e->e_sendqueue, e);
8414632Seric 			break;
8424632Seric 
84325687Seric 		  case 'E':		/* specify error recipient */
84455012Seric 			sendtolist(&buf[1], ctladdr, &e->e_errorqueue, e);
84525687Seric 			break;
84625687Seric 
8474632Seric 		  case 'H':		/* header */
84855012Seric 			(void) chompheader(&buf[1], FALSE, e);
8494632Seric 			break;
8504632Seric 
85110108Seric 		  case 'M':		/* message */
85210108Seric 			e->e_message = newstr(&buf[1]);
85310108Seric 			break;
85410108Seric 
8554632Seric 		  case 'S':		/* sender */
85655012Seric 			setsender(newstr(&buf[1]), e);
8574632Seric 			break;
8584632Seric 
8594632Seric 		  case 'D':		/* data file name */
8609377Seric 			e->e_df = newstr(&buf[1]);
8619544Seric 			e->e_dfp = fopen(e->e_df, "r");
8629544Seric 			if (e->e_dfp == NULL)
8639377Seric 				syserr("readqf: cannot open %s", e->e_df);
8644632Seric 			break;
8654632Seric 
8667860Seric 		  case 'T':		/* init time */
86724941Seric 			e->e_ctime = atol(&buf[1]);
8684632Seric 			break;
8694632Seric 
8704634Seric 		  case 'P':		/* message priority */
87125008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
8724634Seric 			break;
8734634Seric 
87453400Seric 		  case '$':		/* define macro */
87553400Seric 			define(buf[1], newstr(&buf[2]), e);
87653400Seric 			break;
87753400Seric 
87824941Seric 		  case '\0':		/* blank line; ignore */
87924941Seric 			break;
88024941Seric 
8814632Seric 		  default:
88224941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
88324941Seric 				LineNumber, buf);
8844632Seric 			break;
8854632Seric 		}
8864632Seric 	}
8879377Seric 
8889377Seric 	FileName = NULL;
88924941Seric 
89024941Seric 	/*
89124941Seric 	**  If we haven't read any lines, this queue file is empty.
89224941Seric 	**  Arrange to remove it without referencing any null pointers.
89324941Seric 	*/
89424941Seric 
89524941Seric 	if (LineNumber == 0)
89624941Seric 	{
89724941Seric 		errno = 0;
89824941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
89924941Seric 	}
90051920Seric 	return TRUE;
9014632Seric }
9024632Seric /*
9039630Seric **  PRINTQUEUE -- print out a representation of the mail queue
9049630Seric **
9059630Seric **	Parameters:
9069630Seric **		none.
9079630Seric **
9089630Seric **	Returns:
9099630Seric **		none.
9109630Seric **
9119630Seric **	Side Effects:
9129630Seric **		Prints a listing of the mail queue on the standard output.
9139630Seric */
9145182Seric 
9159630Seric printqueue()
9169630Seric {
9179630Seric 	register WORK *w;
9189630Seric 	FILE *f;
91910070Seric 	int nrequests;
9209630Seric 	char buf[MAXLINE];
9219630Seric 
9229630Seric 	/*
9239630Seric 	**  Read and order the queue.
9249630Seric 	*/
9259630Seric 
92624941Seric 	nrequests = orderq(TRUE);
9279630Seric 
9289630Seric 	/*
9299630Seric 	**  Print the work list that we have read.
9309630Seric 	*/
9319630Seric 
9329630Seric 	/* first see if there is anything */
93310070Seric 	if (nrequests <= 0)
9349630Seric 	{
93510070Seric 		printf("Mail queue is empty\n");
9369630Seric 		return;
9379630Seric 	}
9389630Seric 
93951920Seric 	CurrentLA = getla();	/* get load average */
94040934Srick 
94110096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
94225687Seric 	if (nrequests > QUEUESIZE)
94325687Seric 		printf(", only %d printed", QUEUESIZE);
94424979Seric 	if (Verbose)
94525032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
94624979Seric 	else
94724979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
9489630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
9499630Seric 	{
9509630Seric 		struct stat st;
95110070Seric 		auto time_t submittime = 0;
95210070Seric 		long dfsize = -1;
95310108Seric 		char message[MAXLINE];
95451937Seric # ifdef LOCKF
95551937Seric 		struct flock lfd;
95651937Seric # endif
95724941Seric 		extern bool shouldqueue();
9589630Seric 
95917468Seric 		f = fopen(w->w_name, "r");
96017468Seric 		if (f == NULL)
96117468Seric 		{
96217468Seric 			errno = 0;
96317468Seric 			continue;
96417468Seric 		}
9659630Seric 		printf("%7s", w->w_name + 2);
96651835Seric # ifdef LOCKF
96751937Seric 		lfd.l_type = F_RDLCK;
96851937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
96951937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
97051835Seric # else
97140934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
97251835Seric # endif
97310070Seric 			printf("*");
97424941Seric 		else if (shouldqueue(w->w_pri))
97524941Seric 			printf("X");
97610070Seric 		else
97710070Seric 			printf(" ");
97810070Seric 		errno = 0;
97917468Seric 
98010108Seric 		message[0] = '\0';
9819630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
9829630Seric 		{
98353400Seric 			register int i;
98453400Seric 
9859630Seric 			fixcrlf(buf, TRUE);
9869630Seric 			switch (buf[0])
9879630Seric 			{
98810108Seric 			  case 'M':	/* error message */
98953400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
99053400Seric 					i = sizeof message;
99153400Seric 				bcopy(&buf[1], message, i);
99253400Seric 				message[i] = '\0';
99310108Seric 				break;
99410108Seric 
9959630Seric 			  case 'S':	/* sender name */
99624979Seric 				if (Verbose)
99725027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
99825027Seric 					    w->w_pri, ctime(&submittime) + 4,
99924979Seric 					    &buf[1]);
100024979Seric 				else
100124979Seric 					printf("%8ld %.16s %.45s", dfsize,
100224979Seric 					    ctime(&submittime), &buf[1]);
100310108Seric 				if (message[0] != '\0')
100425027Seric 					printf("\n\t\t (%.60s)", message);
10059630Seric 				break;
100651920Seric 
100740973Sbostic 			  case 'C':	/* controlling user */
100854974Seric 				if (Verbose)
100954975Seric 					printf("\n\t\t\t\t     (---%.34s---)", &buf[1]);
101040973Sbostic 				break;
10119630Seric 
10129630Seric 			  case 'R':	/* recipient name */
101324979Seric 				if (Verbose)
101425027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
101524979Seric 				else
101624979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
10179630Seric 				break;
10189630Seric 
10199630Seric 			  case 'T':	/* creation time */
102024941Seric 				submittime = atol(&buf[1]);
10219630Seric 				break;
102210070Seric 
102310070Seric 			  case 'D':	/* data file name */
102410070Seric 				if (stat(&buf[1], &st) >= 0)
102510070Seric 					dfsize = st.st_size;
102610070Seric 				break;
10279630Seric 			}
10289630Seric 		}
102910070Seric 		if (submittime == (time_t) 0)
103010070Seric 			printf(" (no control file)");
10319630Seric 		printf("\n");
103223098Seric 		(void) fclose(f);
10339630Seric 	}
10349630Seric }
10359630Seric 
10365182Seric # endif QUEUE
103717468Seric /*
103817468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
103917468Seric **
104017468Seric **	Assigns an id code if one does not already exist.
104117468Seric **	This code is very careful to avoid trashing existing files
104217468Seric **	under any circumstances.
104317468Seric **
104417468Seric **	Parameters:
104517468Seric **		e -- envelope to build it in/from.
104617468Seric **		type -- the file type, used as the first character
104717468Seric **			of the file name.
104817468Seric **
104917468Seric **	Returns:
105017468Seric **		a pointer to the new file name (in a static buffer).
105117468Seric **
105217468Seric **	Side Effects:
105351920Seric **		If no id code is already assigned, queuename will
105451920Seric **		assign an id code, create a qf file, and leave a
105551920Seric **		locked, open-for-write file pointer in the envelope.
105617468Seric */
105717468Seric 
105817468Seric char *
105917468Seric queuename(e, type)
106017468Seric 	register ENVELOPE *e;
106117468Seric 	char type;
106217468Seric {
106317468Seric 	static char buf[MAXNAME];
106417468Seric 	static int pid = -1;
106517468Seric 	char c1 = 'A';
106617468Seric 	char c2 = 'A';
106717468Seric 
106817468Seric 	if (e->e_id == NULL)
106917468Seric 	{
107017468Seric 		char qf[20];
107117468Seric 
107217468Seric 		/* find a unique id */
107317468Seric 		if (pid != getpid())
107417468Seric 		{
107517468Seric 			/* new process -- start back at "AA" */
107617468Seric 			pid = getpid();
107717468Seric 			c1 = 'A';
107817468Seric 			c2 = 'A' - 1;
107917468Seric 		}
108017468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
108117468Seric 
108217468Seric 		while (c1 < '~' || c2 < 'Z')
108317468Seric 		{
108417468Seric 			int i;
108551937Seric # ifdef LOCKF
108651937Seric 			struct flock lfd;
108751937Seric # endif
108817468Seric 
108917468Seric 			if (c2 >= 'Z')
109017468Seric 			{
109117468Seric 				c1++;
109217468Seric 				c2 = 'A' - 1;
109317468Seric 			}
109440934Srick 			qf[2] = c1;
109540934Srick 			qf[3] = ++c2;
109617468Seric 			if (tTd(7, 20))
109740934Srick 				printf("queuename: trying \"%s\"\n", qf);
109817468Seric 
109940934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
110051920Seric 			if (i < 0)
110151920Seric 			{
110251920Seric 				if (errno == EEXIST)
110351920Seric 					continue;
110451920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
110551920Seric 					qf, QueueDir);
110651920Seric 				exit(EX_UNAVAILABLE);
110751920Seric 			}
110851920Seric # ifdef LOCKF
110951937Seric 			lfd.l_type = F_WRLCK;
111051937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
111151937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
111251920Seric # else
111351920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
111451920Seric # endif
111551920Seric 			{
111651920Seric 				e->e_lockfp = fdopen(i, "w");
111740934Srick 				break;
111817468Seric 			}
111951920Seric 
112051920Seric 			/* a reader got the file; abandon it and try again */
112151920Seric 			(void) close(i);
112217468Seric 		}
112317468Seric 		if (c1 >= '~' && c2 >= 'Z')
112417468Seric 		{
112517468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
112617468Seric 				qf, QueueDir);
112717468Seric 			exit(EX_OSERR);
112817468Seric 		}
112917468Seric 		e->e_id = newstr(&qf[2]);
113017468Seric 		define('i', e->e_id, e);
113117468Seric 		if (tTd(7, 1))
113217468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
113317468Seric # ifdef LOG
113417468Seric 		if (LogLevel > 16)
113517468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
113617468Seric # endif LOG
113717468Seric 	}
113817468Seric 
113917468Seric 	if (type == '\0')
114017468Seric 		return (NULL);
114117468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
114217468Seric 	if (tTd(7, 2))
114317468Seric 		printf("queuename: %s\n", buf);
114417468Seric 	return (buf);
114517468Seric }
114617468Seric /*
114717468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
114817468Seric **
114917468Seric **	Parameters:
115017468Seric **		e -- the envelope to unlock.
115117468Seric **
115217468Seric **	Returns:
115317468Seric **		none
115417468Seric **
115517468Seric **	Side Effects:
115617468Seric **		unlocks the queue for `e'.
115717468Seric */
115817468Seric 
115917468Seric unlockqueue(e)
116017468Seric 	ENVELOPE *e;
116117468Seric {
116251920Seric 	/* if there is a lock file in the envelope, close it */
116351920Seric 	if (e->e_lockfp != NULL)
116451920Seric 		fclose(e->e_lockfp);
116551920Seric 	e->e_lockfp = NULL;
116651920Seric 
116717468Seric 	/* remove the transcript */
116817468Seric # ifdef LOG
116917468Seric 	if (LogLevel > 19)
117017468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
117117468Seric # endif LOG
117217468Seric 	if (!tTd(51, 4))
117317468Seric 		xunlink(queuename(e, 'x'));
117417468Seric 
117517468Seric }
117640973Sbostic /*
117754974Seric **  SETCTLUSER -- create a controlling address
117840973Sbostic **
117954974Seric **	Create a fake "address" given only a local login name; this is
118054974Seric **	used as a "controlling user" for future recipient addresses.
118140973Sbostic **
118240973Sbostic **	Parameters:
118354974Seric **		user -- the user name of the controlling user.
118440973Sbostic **
118540973Sbostic **	Returns:
118654974Seric **		An address descriptor for the controlling user.
118740973Sbostic **
118840973Sbostic **	Side Effects:
118940973Sbostic **		none.
119040973Sbostic */
119140973Sbostic 
119254974Seric ADDRESS *
119354974Seric setctluser(user)
119454974Seric 	char *user;
119540973Sbostic {
119654974Seric 	register ADDRESS *a;
119740973Sbostic 	struct passwd *pw;
119840973Sbostic 
119940973Sbostic 	/*
120054974Seric 	**  See if this clears our concept of controlling user.
120140973Sbostic 	*/
120240973Sbostic 
120354974Seric 	if (user == NULL || *user == '\0')
120454974Seric 		return NULL;
120540973Sbostic 
120640973Sbostic 	/*
120754974Seric 	**  Set up addr fields for controlling user.
120840973Sbostic 	*/
120940973Sbostic 
121054974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
121154974Seric 	bzero((char *) a, sizeof *a);
121254974Seric 	if ((pw = getpwnam(user)) != NULL)
121340973Sbostic 	{
121440973Sbostic 		a->q_home = newstr(pw->pw_dir);
121540973Sbostic 		a->q_uid = pw->pw_uid;
121640973Sbostic 		a->q_gid = pw->pw_gid;
121754974Seric 		a->q_ruser = newstr(user);
121840973Sbostic 	}
121940973Sbostic 	else
122040973Sbostic 	{
122140973Sbostic 		a->q_uid = DefUid;
122240973Sbostic 		a->q_gid = DefGid;
122340973Sbostic 		a->q_ruser = newstr(DefUser);
122440973Sbostic 	}
122540973Sbostic 
122640973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
122754974Seric 	return a;
122840973Sbostic }
1229