xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 54974)
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*54974Seric static char sccsid[] = "@(#)queue.c	5.41 (Berkeley) 07/12/92 (with queueing)";
1433731Sbostic #else
15*54974Seric static char sccsid[] = "@(#)queue.c	5.41 (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;
77*54974Seric 	ADDRESS *lastctladdr;
7851920Seric 	char buf[MAXLINE], tf[MAXLINE];
7953400Seric 	extern char *macvalue();
80*54974Seric 	extern ADDRESS *getctladdr();
814632Seric 
825037Seric 	/*
8317477Seric 	**  Create control file.
845037Seric 	*/
854632Seric 
8651920Seric 	newid = (e->e_id == NULL);
8751920Seric 	strcpy(tf, queuename(e, 't'));
8851920Seric 	tfp = e->e_lockfp;
8951920Seric 	if (tfp == NULL)
9051920Seric 		newid = FALSE;
9151920Seric 	if (newid)
9251835Seric 	{
9351920Seric 		tfp = e->e_lockfp;
9451920Seric 	}
9551920Seric 	else
9651920Seric 	{
9751920Seric 		/* get a locked tf file */
9851920Seric 		for (i = 100; --i >= 0; )
9951835Seric 		{
10051937Seric # ifdef LOCKF
10151937Seric 			struct flock lfd;
10251937Seric # endif
10351937Seric 
10451920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
10551920Seric 			if (fd < 0)
10651835Seric 			{
10751920Seric 				if (errno == EEXIST)
10851920Seric 					continue;
10951920Seric 				syserr("queueup: cannot create temp file %s", tf);
11051920Seric 				return;
11141636Srick 			}
11251835Seric # ifdef LOCKF
11351937Seric 			lfd.l_type = F_WRLCK;
11451937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
11551937Seric 			if (fcntl(fd, F_SETLK, &lfd) >= 0)
11651920Seric 				break;
11751920Seric 			if (errno != EACCES && errno != EAGAIN)
11851920Seric 				syserr("cannot lockf(%s)", tf);
11951835Seric # else
12051920Seric 			if (flock(fd, LOCK_EX|LOCK_NB) >= 0)
12151920Seric 				break;
12251920Seric 			if (errno != EWOULDBLOCK)
12351920Seric 				syserr("cannot flock(%s)", tf);
12451835Seric # endif
12551920Seric 			close(fd);
12641636Srick 		}
12741636Srick 
12851920Seric 		tfp = fdopen(fd, "w");
12951920Seric 	}
1304632Seric 
1317677Seric 	if (tTd(40, 1))
13217468Seric 		printf("queueing %s\n", e->e_id);
1334632Seric 
1344632Seric 	/*
1356980Seric 	**  If there is no data file yet, create one.
1366980Seric 	*/
1376980Seric 
1386980Seric 	if (e->e_df == NULL)
1396980Seric 	{
1406980Seric 		register FILE *dfp;
1419389Seric 		extern putbody();
1426980Seric 
1437812Seric 		e->e_df = newstr(queuename(e, 'd'));
14440934Srick 		fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode);
14540934Srick 		if (fd < 0)
1466980Seric 		{
1476980Seric 			syserr("queueup: cannot create %s", e->e_df);
14851920Seric 			if (!newid)
14951920Seric 				(void) fclose(tfp);
15051920Seric 			return;
1516980Seric 		}
15240934Srick 		dfp = fdopen(fd, "w");
15310173Seric 		(*e->e_putbody)(dfp, ProgMailer, e);
1547009Seric 		(void) fclose(dfp);
1559389Seric 		e->e_putbody = putbody;
1566980Seric 	}
1576980Seric 
1586980Seric 	/*
1594632Seric 	**  Output future work requests.
16025687Seric 	**	Priority and creation time should be first, since
16125687Seric 	**	they are required by orderq.
1624632Seric 	*/
1634632Seric 
1649377Seric 	/* output message priority */
1659377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1669377Seric 
1679630Seric 	/* output creation time */
1689630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1699630Seric 
1704632Seric 	/* output name of data file */
1717812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1724632Seric 
17310108Seric 	/* message from envelope, if it exists */
17410108Seric 	if (e->e_message != NULL)
17510108Seric 		fprintf(tfp, "M%s\n", e->e_message);
17610108Seric 
17753400Seric 	/* $r and $s macro values */
17853400Seric 	if ((p = macvalue('r', e)) != NULL)
17953400Seric 		fprintf(tfp, "$r%s\n", p);
18053400Seric 	if ((p = macvalue('s', e)) != NULL)
18153400Seric 		fprintf(tfp, "$s%s\n", p);
18253400Seric 
1834632Seric 	/* output name of sender */
1847812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
1854632Seric 
1864632Seric 	/* output list of recipient addresses */
187*54974Seric 	lastctladdr = NULL;
1886980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1894632Seric 	{
19047284Seric 		if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) :
1917763Seric 			       bitset(QQUEUEUP, q->q_flags))
1928245Seric 		{
193*54974Seric 			ADDRESS *ctladdr;
19440973Sbostic 
195*54974Seric 			if ((ctladdr = getctladdr(q)) != lastctladdr)
196*54974Seric 			{
197*54974Seric 				printctladdr(ctladdr, tfp);
198*54974Seric 				lastctladdr = ctladdr;
199*54974Seric 			}
2007812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2019377Seric 			if (announce)
2029377Seric 			{
2039377Seric 				e->e_to = q->q_paddr;
2049377Seric 				message(Arpa_Info, "queued");
2059377Seric 				if (LogLevel > 4)
20654967Seric 					logdelivery("queued", e);
2079377Seric 				e->e_to = NULL;
2089377Seric 			}
2099387Seric 			if (tTd(40, 1))
2109387Seric 			{
2119387Seric 				printf("queueing ");
2129387Seric 				printaddr(q, FALSE);
2139387Seric 			}
2148245Seric 		}
2154632Seric 	}
2164632Seric 
21725687Seric 	/* output list of error recipients */
21825687Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
21925687Seric 	{
22026504Seric 		if (!bitset(QDONTSEND, q->q_flags))
22140973Sbostic 		{
222*54974Seric 			ADDRESS *ctladdr;
22340973Sbostic 
224*54974Seric 			if ((ctladdr = getctladdr(q)) != lastctladdr)
225*54974Seric 			{
226*54974Seric 				printctladdr(ctladdr, tfp);
227*54974Seric 				lastctladdr = ctladdr;
228*54974Seric 			}
22926504Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
23040973Sbostic 		}
23125687Seric 	}
23225687Seric 
2339377Seric 	/*
2349377Seric 	**  Output headers for this message.
2359377Seric 	**	Expand macros completely here.  Queue run will deal with
2369377Seric 	**	everything as absolute headers.
2379377Seric 	**		All headers that must be relative to the recipient
2389377Seric 	**		can be cracked later.
23910173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
24010173Seric 	**	no effect on the addresses as they are output.
2419377Seric 	*/
2429377Seric 
24310686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
24410173Seric 	nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1;
24510349Seric 	nullmailer.m_eol = "\n";
24610173Seric 
24716147Seric 	define('g', "\001f", e);
2486980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2494632Seric 	{
25010686Seric 		extern bool bitzerop();
25110686Seric 
25212015Seric 		/* don't output null headers */
2534632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2544632Seric 			continue;
25512015Seric 
25612015Seric 		/* don't output resent headers on non-resent messages */
25712015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
25812015Seric 			continue;
25912015Seric 
26012015Seric 		/* output this header */
2617812Seric 		fprintf(tfp, "H");
26212015Seric 
26312015Seric 		/* if conditional, output the set of conditions */
26410686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
26510686Seric 		{
26610686Seric 			int j;
26710686Seric 
26823098Seric 			(void) putc('?', tfp);
26910686Seric 			for (j = '\0'; j <= '\177'; j++)
27010686Seric 				if (bitnset(j, h->h_mflags))
27123098Seric 					(void) putc(j, tfp);
27223098Seric 			(void) putc('?', tfp);
27310686Seric 		}
27412015Seric 
27512015Seric 		/* output the header: expand macros, convert addresses */
2767763Seric 		if (bitset(H_DEFAULT, h->h_flags))
2777763Seric 		{
2787763Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
2798236Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
2807763Seric 		}
2818245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
2829348Seric 		{
2839348Seric 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
28410173Seric 				 &nullmailer);
2859348Seric 		}
2867763Seric 		else
2878245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
2884632Seric 	}
2894632Seric 
2904632Seric 	/*
2914632Seric 	**  Clean up.
2924632Seric 	*/
2934632Seric 
29451920Seric 	if (!newid)
29551920Seric 	{
29651920Seric 		qf = queuename(e, 'q');
29751920Seric 		if (rename(tf, qf) < 0)
29851920Seric 			syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df);
29951920Seric 		if (e->e_lockfp != NULL)
30051920Seric 			(void) fclose(e->e_lockfp);
30151920Seric 		e->e_lockfp = tfp;
30251920Seric 	}
30351920Seric 	else
30451920Seric 		qf = tf;
30541636Srick 	errno = 0;
3067391Seric 
3077677Seric # ifdef LOG
3087677Seric 	/* save log info */
3097878Seric 	if (LogLevel > 15)
3107878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
3117677Seric # endif LOG
31240934Srick 	fflush(tfp);
31351920Seric 	return;
3144632Seric }
315*54974Seric 
316*54974Seric printctladdr(a, tfp)
317*54974Seric 	ADDRESS *a;
318*54974Seric 	FILE *tfp;
319*54974Seric {
320*54974Seric 	char *u;
321*54974Seric 	struct passwd *pw;
322*54974Seric 	extern struct passwd *getpwuid();
323*54974Seric 
324*54974Seric 	if (a == NULL)
325*54974Seric 	{
326*54974Seric 		fprintf(tfp, "C\n");
327*54974Seric 		return;
328*54974Seric 	}
329*54974Seric 	if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL)
330*54974Seric 		u = DefUser;
331*54974Seric 	else
332*54974Seric 		u = pw->pw_name;
333*54974Seric 	fprintf(tfp, "C%s\n", u);
334*54974Seric }
335*54974Seric 
3364632Seric /*
3374632Seric **  RUNQUEUE -- run the jobs in the queue.
3384632Seric **
3394632Seric **	Gets the stuff out of the queue in some presumably logical
3404632Seric **	order and processes them.
3414632Seric **
3424632Seric **	Parameters:
34324941Seric **		forkflag -- TRUE if the queue scanning should be done in
34424941Seric **			a child process.  We double-fork so it is not our
34524941Seric **			child and we don't have to clean up after it.
3464632Seric **
3474632Seric **	Returns:
3484632Seric **		none.
3494632Seric **
3504632Seric **	Side Effects:
3514632Seric **		runs things in the mail queue.
3524632Seric */
3534632Seric 
3544639Seric runqueue(forkflag)
3554639Seric 	bool forkflag;
3564632Seric {
35724953Seric 	extern bool shouldqueue();
35824953Seric 
3597466Seric 	/*
36024953Seric 	**  If no work will ever be selected, don't even bother reading
36124953Seric 	**  the queue.
36224953Seric 	*/
36324953Seric 
36451920Seric 	CurrentLA = getla();	/* get load average */
36540934Srick 
36624953Seric 	if (shouldqueue(-100000000L))
36724953Seric 	{
36824953Seric 		if (Verbose)
36924953Seric 			printf("Skipping queue run -- load average too high\n");
37024953Seric 
37124953Seric 		if (forkflag)
37224953Seric 			return;
37324953Seric 		finis();
37424953Seric 	}
37524953Seric 
37624953Seric 	/*
3777466Seric 	**  See if we want to go off and do other useful work.
3787466Seric 	*/
3794639Seric 
3804639Seric 	if (forkflag)
3814639Seric 	{
3827943Seric 		int pid;
3837943Seric 
3847943Seric 		pid = dofork();
3857943Seric 		if (pid != 0)
3864639Seric 		{
38746928Sbostic 			extern void reapchild();
38825184Seric 
3897943Seric 			/* parent -- pick up intermediate zombie */
39025184Seric #ifndef SIGCHLD
3919377Seric 			(void) waitfor(pid);
39225184Seric #else SIGCHLD
39325184Seric 			(void) signal(SIGCHLD, reapchild);
39425184Seric #endif SIGCHLD
3957690Seric 			if (QueueIntvl != 0)
3969348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
3974639Seric 			return;
3984639Seric 		}
3997943Seric 		/* child -- double fork */
40025184Seric #ifndef SIGCHLD
4017943Seric 		if (fork() != 0)
4027943Seric 			exit(EX_OK);
40325184Seric #else SIGCHLD
40425184Seric 		(void) signal(SIGCHLD, SIG_DFL);
40525184Seric #endif SIGCHLD
4064639Seric 	}
40724941Seric 
40840934Srick 	setproctitle("running queue: %s", QueueDir);
40924941Seric 
4107876Seric # ifdef LOG
4117876Seric 	if (LogLevel > 11)
4127943Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
4137876Seric # endif LOG
4144639Seric 
4157466Seric 	/*
41610205Seric 	**  Release any resources used by the daemon code.
41710205Seric 	*/
41810205Seric 
41910205Seric # ifdef DAEMON
42010205Seric 	clrdaemon();
42110205Seric # endif DAEMON
42210205Seric 
42310205Seric 	/*
42427175Seric 	**  Make sure the alias database is open.
42527175Seric 	*/
42627175Seric 
42727175Seric 	initaliases(AliasFile, FALSE);
42827175Seric 
42927175Seric 	/*
4307466Seric 	**  Start making passes through the queue.
4317466Seric 	**	First, read and sort the entire queue.
4327466Seric 	**	Then, process the work in that order.
4337466Seric 	**		But if you take too long, start over.
4347466Seric 	*/
4357466Seric 
4367943Seric 	/* order the existing work requests */
43724954Seric 	(void) orderq(FALSE);
4387690Seric 
4397943Seric 	/* process them once at a time */
4407943Seric 	while (WorkQ != NULL)
4414639Seric 	{
4427943Seric 		WORK *w = WorkQ;
4437881Seric 
4447943Seric 		WorkQ = WorkQ->w_next;
4457943Seric 		dowork(w);
4467943Seric 		free(w->w_name);
4477943Seric 		free((char *) w);
4484639Seric 	}
44929866Seric 
45029866Seric 	/* exit without the usual cleanup */
45129866Seric 	exit(ExitStat);
4524634Seric }
4534634Seric /*
4544632Seric **  ORDERQ -- order the work queue.
4554632Seric **
4564632Seric **	Parameters:
45724941Seric **		doall -- if set, include everything in the queue (even
45824941Seric **			the jobs that cannot be run because the load
45924941Seric **			average is too high).  Otherwise, exclude those
46024941Seric **			jobs.
4614632Seric **
4624632Seric **	Returns:
46310121Seric **		The number of request in the queue (not necessarily
46410121Seric **		the number of requests in WorkQ however).
4654632Seric **
4664632Seric **	Side Effects:
4674632Seric **		Sets WorkQ to the queue of available work, in order.
4684632Seric */
4694632Seric 
47025687Seric # define NEED_P		001
47125687Seric # define NEED_T		002
4724632Seric 
47324941Seric orderq(doall)
47424941Seric 	bool doall;
4754632Seric {
4766625Sglickman 	register struct direct *d;
4774632Seric 	register WORK *w;
4786625Sglickman 	DIR *f;
4794632Seric 	register int i;
48025687Seric 	WORK wlist[QUEUESIZE+1];
48110070Seric 	int wn = -1;
4824632Seric 	extern workcmpf();
4834632Seric 
4844632Seric 	/* clear out old WorkQ */
4854632Seric 	for (w = WorkQ; w != NULL; )
4864632Seric 	{
4874632Seric 		register WORK *nw = w->w_next;
4884632Seric 
4894632Seric 		WorkQ = nw;
4904632Seric 		free(w->w_name);
4914632Seric 		free((char *) w);
4924632Seric 		w = nw;
4934632Seric 	}
4944632Seric 
4954632Seric 	/* open the queue directory */
4968148Seric 	f = opendir(".");
4974632Seric 	if (f == NULL)
4984632Seric 	{
4998148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
50010070Seric 		return (0);
5014632Seric 	}
5024632Seric 
5034632Seric 	/*
5044632Seric 	**  Read the work directory.
5054632Seric 	*/
5064632Seric 
50710070Seric 	while ((d = readdir(f)) != NULL)
5084632Seric 	{
5099377Seric 		FILE *cf;
5104632Seric 		char lbuf[MAXNAME];
5114632Seric 
5124632Seric 		/* is this an interesting entry? */
5137812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
5144632Seric 			continue;
5154632Seric 
51610070Seric 		/* yes -- open control file (if not too many files) */
51725687Seric 		if (++wn >= QUEUESIZE)
51810070Seric 			continue;
5198148Seric 		cf = fopen(d->d_name, "r");
5204632Seric 		if (cf == NULL)
5214632Seric 		{
5227055Seric 			/* this may be some random person sending hir msgs */
5237055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
52410090Seric 			if (tTd(41, 2))
52510090Seric 				printf("orderq: cannot open %s (%d)\n",
52610090Seric 					d->d_name, errno);
5277055Seric 			errno = 0;
52810090Seric 			wn--;
5294632Seric 			continue;
5304632Seric 		}
53125687Seric 		w = &wlist[wn];
53225687Seric 		w->w_name = newstr(d->d_name);
5334632Seric 
53425027Seric 		/* make sure jobs in creation don't clog queue */
53525687Seric 		w->w_pri = 0x7fffffff;
53625687Seric 		w->w_ctime = 0;
53725027Seric 
5384632Seric 		/* extract useful information */
53925687Seric 		i = NEED_P | NEED_T;
54025687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
5414632Seric 		{
54224954Seric 			extern long atol();
54324954Seric 
54424941Seric 			switch (lbuf[0])
5454632Seric 			{
54624941Seric 			  case 'P':
54725687Seric 				w->w_pri = atol(&lbuf[1]);
54825687Seric 				i &= ~NEED_P;
5494632Seric 				break;
55025013Seric 
55125013Seric 			  case 'T':
55225687Seric 				w->w_ctime = atol(&lbuf[1]);
55325687Seric 				i &= ~NEED_T;
55425013Seric 				break;
5554632Seric 			}
5564632Seric 		}
5574632Seric 		(void) fclose(cf);
55824953Seric 
55925687Seric 		if (!doall && shouldqueue(w->w_pri))
56024953Seric 		{
56124953Seric 			/* don't even bother sorting this job in */
56224953Seric 			wn--;
56324953Seric 		}
5644632Seric 	}
5656625Sglickman 	(void) closedir(f);
56610090Seric 	wn++;
5674632Seric 
5684632Seric 	/*
5694632Seric 	**  Sort the work directory.
5704632Seric 	*/
5714632Seric 
57225687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
5734632Seric 
5744632Seric 	/*
5754632Seric 	**  Convert the work list into canonical form.
5769377Seric 	**	Should be turning it into a list of envelopes here perhaps.
5774632Seric 	*/
5784632Seric 
57924981Seric 	WorkQ = NULL;
58025687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
5814632Seric 	{
5824632Seric 		w = (WORK *) xalloc(sizeof *w);
5834632Seric 		w->w_name = wlist[i].w_name;
5844632Seric 		w->w_pri = wlist[i].w_pri;
58525013Seric 		w->w_ctime = wlist[i].w_ctime;
58624981Seric 		w->w_next = WorkQ;
58724981Seric 		WorkQ = w;
5884632Seric 	}
5894632Seric 
5907677Seric 	if (tTd(40, 1))
5914632Seric 	{
5924632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
5935037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
5944632Seric 	}
59510070Seric 
59610090Seric 	return (wn);
5974632Seric }
5984632Seric /*
5997677Seric **  WORKCMPF -- compare function for ordering work.
6004632Seric **
6014632Seric **	Parameters:
6024632Seric **		a -- the first argument.
6034632Seric **		b -- the second argument.
6044632Seric **
6054632Seric **	Returns:
60624981Seric **		-1 if a < b
60724981Seric **		 0 if a == b
60824981Seric **		+1 if a > b
6094632Seric **
6104632Seric **	Side Effects:
6114632Seric **		none.
6124632Seric */
6134632Seric 
6144632Seric workcmpf(a, b)
6155037Seric 	register WORK *a;
6165037Seric 	register WORK *b;
6174632Seric {
61825013Seric 	long pa = a->w_pri + a->w_ctime;
61925013Seric 	long pb = b->w_pri + b->w_ctime;
62024941Seric 
62124941Seric 	if (pa == pb)
6224632Seric 		return (0);
62324941Seric 	else if (pa > pb)
62424981Seric 		return (1);
62524981Seric 	else
62610121Seric 		return (-1);
6274632Seric }
6284632Seric /*
6294632Seric **  DOWORK -- do a work request.
6304632Seric **
6314632Seric **	Parameters:
6324632Seric **		w -- the work request to be satisfied.
6334632Seric **
6344632Seric **	Returns:
6354632Seric **		none.
6364632Seric **
6374632Seric **	Side Effects:
6384632Seric **		The work request is satisfied if possible.
6394632Seric */
6404632Seric 
6414632Seric dowork(w)
6424632Seric 	register WORK *w;
6434632Seric {
6444632Seric 	register int i;
64524941Seric 	extern bool shouldqueue();
64651920Seric 	extern bool readqf();
6474632Seric 
6487677Seric 	if (tTd(40, 1))
6495037Seric 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
6504632Seric 
6514632Seric 	/*
65224941Seric 	**  Ignore jobs that are too expensive for the moment.
6534632Seric 	*/
6544632Seric 
65524941Seric 	if (shouldqueue(w->w_pri))
6564632Seric 	{
65724941Seric 		if (Verbose)
65824967Seric 			printf("\nSkipping %s\n", w->w_name + 2);
6594632Seric 		return;
6604632Seric 	}
6614632Seric 
66224941Seric 	/*
66324941Seric 	**  Fork for work.
66424941Seric 	*/
66524941Seric 
66624941Seric 	if (ForkQueueRuns)
66724941Seric 	{
66824941Seric 		i = fork();
66924941Seric 		if (i < 0)
67024941Seric 		{
67124941Seric 			syserr("dowork: cannot fork");
67224941Seric 			return;
67324941Seric 		}
67424941Seric 	}
67524941Seric 	else
67624941Seric 	{
67724941Seric 		i = 0;
67824941Seric 	}
67924941Seric 
6804632Seric 	if (i == 0)
6814632Seric 	{
6824632Seric 		/*
6834632Seric 		**  CHILD
6848148Seric 		**	Lock the control file to avoid duplicate deliveries.
6858148Seric 		**		Then run the file as though we had just read it.
6867350Seric 		**	We save an idea of the temporary name so we
6877350Seric 		**		can recover on interrupt.
6884632Seric 		*/
6894632Seric 
6907763Seric 		/* set basic modes, etc. */
6917356Seric 		(void) alarm(0);
69225612Seric 		clearenvelope(CurEnv, FALSE);
6934632Seric 		QueueRun = TRUE;
6949377Seric 		ErrorMode = EM_MAIL;
6958148Seric 		CurEnv->e_id = &w->w_name[2];
6967876Seric # ifdef LOG
6977876Seric 		if (LogLevel > 11)
6987881Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
6997881Seric 			       getpid());
7007876Seric # endif LOG
7017763Seric 
7027763Seric 		/* don't use the headers from sendmail.cf... */
7037763Seric 		CurEnv->e_header = NULL;
7047763Seric 
70551920Seric 		/* read the queue control file -- return if locked */
70651920Seric 		if (!readqf(CurEnv))
7076980Seric 		{
70824941Seric 			if (ForkQueueRuns)
70924941Seric 				exit(EX_OK);
71024941Seric 			else
71124941Seric 				return;
7126980Seric 		}
7136980Seric 
7149338Seric 		CurEnv->e_flags |= EF_INQUEUE;
7159377Seric 		eatheader(CurEnv);
7166980Seric 
7176980Seric 		/* do the delivery */
7189338Seric 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
7199282Seric 			sendall(CurEnv, SM_DELIVER);
7206980Seric 
7216980Seric 		/* finish up and exit */
72224941Seric 		if (ForkQueueRuns)
72324941Seric 			finis();
72424941Seric 		else
72524941Seric 			dropenvelope(CurEnv);
7264632Seric 	}
72724941Seric 	else
72824941Seric 	{
72924941Seric 		/*
73024941Seric 		**  Parent -- pick up results.
73124941Seric 		*/
7324632Seric 
73324941Seric 		errno = 0;
73424941Seric 		(void) waitfor(i);
73524941Seric 	}
7364632Seric }
7374632Seric /*
7384632Seric **  READQF -- read queue file and set up environment.
7394632Seric **
7404632Seric **	Parameters:
7419377Seric **		e -- the envelope of the job to run.
7424632Seric **
7434632Seric **	Returns:
74451920Seric **		TRUE if it successfully read the queue file.
74551920Seric **		FALSE otherwise.
7464632Seric **
7474632Seric **	Side Effects:
74851920Seric **		The queue file is returned locked.
7494632Seric */
7504632Seric 
75151920Seric bool
75251920Seric readqf(e)
7539377Seric 	register ENVELOPE *e;
7544632Seric {
75517477Seric 	char *qf;
75617477Seric 	register FILE *qfp;
757*54974Seric 	int fd;
758*54974Seric 	ADDRESS *ctladdr;
7597785Seric 	char buf[MAXFIELD];
7609348Seric 	extern char *fgetfolded();
76124954Seric 	extern long atol();
762*54974Seric 	extern ADDRESS *setctluser();
76351937Seric # ifdef LOCKF
76451937Seric 	struct flock lfd;
76551937Seric # endif
7664632Seric 
7674632Seric 	/*
76817468Seric 	**  Read and process the file.
7694632Seric 	*/
7704632Seric 
77117477Seric 	qf = queuename(e, 'q');
77251937Seric 	qfp = fopen(qf, "r+");
77317477Seric 	if (qfp == NULL)
77417477Seric 	{
77540934Srick 		if (errno != ENOENT)
77640934Srick 			syserr("readqf: no control file %s", qf);
77751920Seric 		return FALSE;
77817477Seric 	}
77940934Srick 
78051835Seric # ifdef LOCKF
78151937Seric 	lfd.l_type = F_WRLCK;
78251937Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
78351937Seric 	if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0)
78451835Seric # else
78540934Srick 	if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0)
78651835Seric # endif
78740934Srick 	{
78840934Srick 		/* being processed by another queuer */
78940934Srick 		if (Verbose)
79041636Srick 			printf("%s: locked\n", CurEnv->e_id);
79151920Seric # ifdef LOG
79251920Seric 		if (LogLevel > 9)
79351920Seric 			syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
79440934Srick # endif LOG
79540934Srick 		(void) fclose(qfp);
79651920Seric 		return FALSE;
79740934Srick 	}
79840934Srick 
79951920Seric 	/* save this lock */
80051920Seric 	e->e_lockfp = qfp;
80151920Seric 
80240934Srick 	/* do basic system initialization */
80340934Srick 	initsys();
80440934Srick 
80517477Seric 	FileName = qf;
8069377Seric 	LineNumber = 0;
80751920Seric 	if (Verbose)
8089377Seric 		printf("\nRunning %s\n", e->e_id);
809*54974Seric 	ctladdr = NULL;
81017468Seric 	while (fgetfolded(buf, sizeof buf, qfp) != NULL)
8114632Seric 	{
81226504Seric 		if (tTd(40, 4))
81326504Seric 			printf("+++++ %s\n", buf);
8144632Seric 		switch (buf[0])
8154632Seric 		{
81640973Sbostic 		  case 'C':		/* specify controlling user */
817*54974Seric 			ctladdr = setctluser(&buf[1]);
81840973Sbostic 			break;
81940973Sbostic 
8204632Seric 		  case 'R':		/* specify recipient */
821*54974Seric 			sendtolist(&buf[1], ctladdr, &e->e_sendqueue);
8224632Seric 			break;
8234632Seric 
82425687Seric 		  case 'E':		/* specify error recipient */
825*54974Seric 			sendtolist(&buf[1], ctladdr, &e->e_errorqueue);
82625687Seric 			break;
82725687Seric 
8284632Seric 		  case 'H':		/* header */
82951920Seric 			(void) chompheader(&buf[1], FALSE);
8304632Seric 			break;
8314632Seric 
83210108Seric 		  case 'M':		/* message */
83310108Seric 			e->e_message = newstr(&buf[1]);
83410108Seric 			break;
83510108Seric 
8364632Seric 		  case 'S':		/* sender */
83753182Seric 			setsender(newstr(&buf[1]), CurEnv);
8384632Seric 			break;
8394632Seric 
8404632Seric 		  case 'D':		/* data file name */
8419377Seric 			e->e_df = newstr(&buf[1]);
8429544Seric 			e->e_dfp = fopen(e->e_df, "r");
8439544Seric 			if (e->e_dfp == NULL)
8449377Seric 				syserr("readqf: cannot open %s", e->e_df);
8454632Seric 			break;
8464632Seric 
8477860Seric 		  case 'T':		/* init time */
84824941Seric 			e->e_ctime = atol(&buf[1]);
8494632Seric 			break;
8504632Seric 
8514634Seric 		  case 'P':		/* message priority */
85225008Seric 			e->e_msgpriority = atol(&buf[1]) + WkTimeFact;
8534634Seric 			break;
8544634Seric 
85553400Seric 		  case '$':		/* define macro */
85653400Seric 			define(buf[1], newstr(&buf[2]), e);
85753400Seric 			break;
85853400Seric 
85924941Seric 		  case '\0':		/* blank line; ignore */
86024941Seric 			break;
86124941Seric 
8624632Seric 		  default:
86324941Seric 			syserr("readqf(%s:%d): bad line \"%s\"", e->e_id,
86424941Seric 				LineNumber, buf);
8654632Seric 			break;
8664632Seric 		}
8674632Seric 	}
8689377Seric 
8699377Seric 	FileName = NULL;
87024941Seric 
87124941Seric 	/*
87224941Seric 	**  If we haven't read any lines, this queue file is empty.
87324941Seric 	**  Arrange to remove it without referencing any null pointers.
87424941Seric 	*/
87524941Seric 
87624941Seric 	if (LineNumber == 0)
87724941Seric 	{
87824941Seric 		errno = 0;
87924941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
88024941Seric 	}
88151920Seric 	return TRUE;
8824632Seric }
8834632Seric /*
8849630Seric **  PRINTQUEUE -- print out a representation of the mail queue
8859630Seric **
8869630Seric **	Parameters:
8879630Seric **		none.
8889630Seric **
8899630Seric **	Returns:
8909630Seric **		none.
8919630Seric **
8929630Seric **	Side Effects:
8939630Seric **		Prints a listing of the mail queue on the standard output.
8949630Seric */
8955182Seric 
8969630Seric printqueue()
8979630Seric {
8989630Seric 	register WORK *w;
8999630Seric 	FILE *f;
90010070Seric 	int nrequests;
9019630Seric 	char buf[MAXLINE];
9029630Seric 
9039630Seric 	/*
9049630Seric 	**  Read and order the queue.
9059630Seric 	*/
9069630Seric 
90724941Seric 	nrequests = orderq(TRUE);
9089630Seric 
9099630Seric 	/*
9109630Seric 	**  Print the work list that we have read.
9119630Seric 	*/
9129630Seric 
9139630Seric 	/* first see if there is anything */
91410070Seric 	if (nrequests <= 0)
9159630Seric 	{
91610070Seric 		printf("Mail queue is empty\n");
9179630Seric 		return;
9189630Seric 	}
9199630Seric 
92051920Seric 	CurrentLA = getla();	/* get load average */
92140934Srick 
92210096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
92325687Seric 	if (nrequests > QUEUESIZE)
92425687Seric 		printf(", only %d printed", QUEUESIZE);
92524979Seric 	if (Verbose)
92625032Seric 		printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
92724979Seric 	else
92824979Seric 		printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
9299630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
9309630Seric 	{
9319630Seric 		struct stat st;
93210070Seric 		auto time_t submittime = 0;
93310070Seric 		long dfsize = -1;
93410108Seric 		char message[MAXLINE];
93551937Seric # ifdef LOCKF
93651937Seric 		struct flock lfd;
93751937Seric # endif
93824941Seric 		extern bool shouldqueue();
9399630Seric 
94017468Seric 		f = fopen(w->w_name, "r");
94117468Seric 		if (f == NULL)
94217468Seric 		{
94317468Seric 			errno = 0;
94417468Seric 			continue;
94517468Seric 		}
9469630Seric 		printf("%7s", w->w_name + 2);
94751835Seric # ifdef LOCKF
94851937Seric 		lfd.l_type = F_RDLCK;
94951937Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
95051937Seric 		if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK)
95151835Seric # else
95240934Srick 		if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0)
95351835Seric # endif
95410070Seric 			printf("*");
95524941Seric 		else if (shouldqueue(w->w_pri))
95624941Seric 			printf("X");
95710070Seric 		else
95810070Seric 			printf(" ");
95910070Seric 		errno = 0;
96017468Seric 
96110108Seric 		message[0] = '\0';
9629630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
9639630Seric 		{
96453400Seric 			register int i;
96553400Seric 
9669630Seric 			fixcrlf(buf, TRUE);
9679630Seric 			switch (buf[0])
9689630Seric 			{
96910108Seric 			  case 'M':	/* error message */
97053400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
97153400Seric 					i = sizeof message;
97253400Seric 				bcopy(&buf[1], message, i);
97353400Seric 				message[i] = '\0';
97410108Seric 				break;
97510108Seric 
9769630Seric 			  case 'S':	/* sender name */
97724979Seric 				if (Verbose)
97825027Seric 					printf("%8ld %10ld %.12s %.38s", dfsize,
97925027Seric 					    w->w_pri, ctime(&submittime) + 4,
98024979Seric 					    &buf[1]);
98124979Seric 				else
98224979Seric 					printf("%8ld %.16s %.45s", dfsize,
98324979Seric 					    ctime(&submittime), &buf[1]);
98410108Seric 				if (message[0] != '\0')
98525027Seric 					printf("\n\t\t (%.60s)", message);
9869630Seric 				break;
98751920Seric 
98840973Sbostic 			  case 'C':	/* controlling user */
989*54974Seric 				if (Verbose)
990*54974Seric 					printf("\n\t\t\t\t\t (---%.30s---)", &buf[1]);
99140973Sbostic 				break;
9929630Seric 
9939630Seric 			  case 'R':	/* recipient name */
99424979Seric 				if (Verbose)
99525027Seric 					printf("\n\t\t\t\t\t %.38s", &buf[1]);
99624979Seric 				else
99724979Seric 					printf("\n\t\t\t\t  %.45s", &buf[1]);
9989630Seric 				break;
9999630Seric 
10009630Seric 			  case 'T':	/* creation time */
100124941Seric 				submittime = atol(&buf[1]);
10029630Seric 				break;
100310070Seric 
100410070Seric 			  case 'D':	/* data file name */
100510070Seric 				if (stat(&buf[1], &st) >= 0)
100610070Seric 					dfsize = st.st_size;
100710070Seric 				break;
10089630Seric 			}
10099630Seric 		}
101010070Seric 		if (submittime == (time_t) 0)
101110070Seric 			printf(" (no control file)");
10129630Seric 		printf("\n");
101323098Seric 		(void) fclose(f);
10149630Seric 	}
10159630Seric }
10169630Seric 
10175182Seric # endif QUEUE
101817468Seric /*
101917468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
102017468Seric **
102117468Seric **	Assigns an id code if one does not already exist.
102217468Seric **	This code is very careful to avoid trashing existing files
102317468Seric **	under any circumstances.
102417468Seric **
102517468Seric **	Parameters:
102617468Seric **		e -- envelope to build it in/from.
102717468Seric **		type -- the file type, used as the first character
102817468Seric **			of the file name.
102917468Seric **
103017468Seric **	Returns:
103117468Seric **		a pointer to the new file name (in a static buffer).
103217468Seric **
103317468Seric **	Side Effects:
103451920Seric **		If no id code is already assigned, queuename will
103551920Seric **		assign an id code, create a qf file, and leave a
103651920Seric **		locked, open-for-write file pointer in the envelope.
103717468Seric */
103817468Seric 
103917468Seric char *
104017468Seric queuename(e, type)
104117468Seric 	register ENVELOPE *e;
104217468Seric 	char type;
104317468Seric {
104417468Seric 	static char buf[MAXNAME];
104517468Seric 	static int pid = -1;
104617468Seric 	char c1 = 'A';
104717468Seric 	char c2 = 'A';
104817468Seric 
104917468Seric 	if (e->e_id == NULL)
105017468Seric 	{
105117468Seric 		char qf[20];
105217468Seric 
105317468Seric 		/* find a unique id */
105417468Seric 		if (pid != getpid())
105517468Seric 		{
105617468Seric 			/* new process -- start back at "AA" */
105717468Seric 			pid = getpid();
105817468Seric 			c1 = 'A';
105917468Seric 			c2 = 'A' - 1;
106017468Seric 		}
106117468Seric 		(void) sprintf(qf, "qfAA%05d", pid);
106217468Seric 
106317468Seric 		while (c1 < '~' || c2 < 'Z')
106417468Seric 		{
106517468Seric 			int i;
106651937Seric # ifdef LOCKF
106751937Seric 			struct flock lfd;
106851937Seric # endif
106917468Seric 
107017468Seric 			if (c2 >= 'Z')
107117468Seric 			{
107217468Seric 				c1++;
107317468Seric 				c2 = 'A' - 1;
107417468Seric 			}
107540934Srick 			qf[2] = c1;
107640934Srick 			qf[3] = ++c2;
107717468Seric 			if (tTd(7, 20))
107840934Srick 				printf("queuename: trying \"%s\"\n", qf);
107917468Seric 
108040934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
108151920Seric 			if (i < 0)
108251920Seric 			{
108351920Seric 				if (errno == EEXIST)
108451920Seric 					continue;
108551920Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\"",
108651920Seric 					qf, QueueDir);
108751920Seric 				exit(EX_UNAVAILABLE);
108851920Seric 			}
108951920Seric # ifdef LOCKF
109051937Seric 			lfd.l_type = F_WRLCK;
109151937Seric 			lfd.l_whence = lfd.l_start = lfd.l_len = 0;
109251937Seric 			if (fcntl(i, F_SETLK, &lfd) >= 0)
109351920Seric # else
109451920Seric 			if (flock(i, LOCK_EX|LOCK_NB) >= 0)
109551920Seric # endif
109651920Seric 			{
109751920Seric 				e->e_lockfp = fdopen(i, "w");
109840934Srick 				break;
109917468Seric 			}
110051920Seric 
110151920Seric 			/* a reader got the file; abandon it and try again */
110251920Seric 			(void) close(i);
110317468Seric 		}
110417468Seric 		if (c1 >= '~' && c2 >= 'Z')
110517468Seric 		{
110617468Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
110717468Seric 				qf, QueueDir);
110817468Seric 			exit(EX_OSERR);
110917468Seric 		}
111017468Seric 		e->e_id = newstr(&qf[2]);
111117468Seric 		define('i', e->e_id, e);
111217468Seric 		if (tTd(7, 1))
111317468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
111417468Seric # ifdef LOG
111517468Seric 		if (LogLevel > 16)
111617468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
111717468Seric # endif LOG
111817468Seric 	}
111917468Seric 
112017468Seric 	if (type == '\0')
112117468Seric 		return (NULL);
112217468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
112317468Seric 	if (tTd(7, 2))
112417468Seric 		printf("queuename: %s\n", buf);
112517468Seric 	return (buf);
112617468Seric }
112717468Seric /*
112817468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
112917468Seric **
113017468Seric **	Parameters:
113117468Seric **		e -- the envelope to unlock.
113217468Seric **
113317468Seric **	Returns:
113417468Seric **		none
113517468Seric **
113617468Seric **	Side Effects:
113717468Seric **		unlocks the queue for `e'.
113817468Seric */
113917468Seric 
114017468Seric unlockqueue(e)
114117468Seric 	ENVELOPE *e;
114217468Seric {
114351920Seric 	/* if there is a lock file in the envelope, close it */
114451920Seric 	if (e->e_lockfp != NULL)
114551920Seric 		fclose(e->e_lockfp);
114651920Seric 	e->e_lockfp = NULL;
114751920Seric 
114817468Seric 	/* remove the transcript */
114917468Seric # ifdef LOG
115017468Seric 	if (LogLevel > 19)
115117468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
115217468Seric # endif LOG
115317468Seric 	if (!tTd(51, 4))
115417468Seric 		xunlink(queuename(e, 'x'));
115517468Seric 
115617468Seric }
115740973Sbostic /*
1158*54974Seric **  SETCTLUSER -- create a controlling address
115940973Sbostic **
1160*54974Seric **	Create a fake "address" given only a local login name; this is
1161*54974Seric **	used as a "controlling user" for future recipient addresses.
116240973Sbostic **
116340973Sbostic **	Parameters:
1164*54974Seric **		user -- the user name of the controlling user.
116540973Sbostic **
116640973Sbostic **	Returns:
1167*54974Seric **		An address descriptor for the controlling user.
116840973Sbostic **
116940973Sbostic **	Side Effects:
117040973Sbostic **		none.
117140973Sbostic */
117240973Sbostic 
1173*54974Seric ADDRESS *
1174*54974Seric setctluser(user)
1175*54974Seric 	char *user;
117640973Sbostic {
1177*54974Seric 	register ADDRESS *a;
117840973Sbostic 	struct passwd *pw;
117940973Sbostic 
118040973Sbostic 	/*
1181*54974Seric 	**  See if this clears our concept of controlling user.
118240973Sbostic 	*/
118340973Sbostic 
1184*54974Seric 	if (user == NULL || *user == '\0')
1185*54974Seric 		return NULL;
118640973Sbostic 
118740973Sbostic 	/*
1188*54974Seric 	**  Set up addr fields for controlling user.
118940973Sbostic 	*/
119040973Sbostic 
1191*54974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
1192*54974Seric 	bzero((char *) a, sizeof *a);
1193*54974Seric 	if ((pw = getpwnam(user)) != NULL)
119440973Sbostic 	{
119540973Sbostic 		a->q_home = newstr(pw->pw_dir);
119640973Sbostic 		a->q_uid = pw->pw_uid;
119740973Sbostic 		a->q_gid = pw->pw_gid;
1198*54974Seric 		a->q_ruser = newstr(user);
119940973Sbostic 	}
120040973Sbostic 	else
120140973Sbostic 	{
120240973Sbostic 		a->q_uid = DefUid;
120340973Sbostic 		a->q_gid = DefGid;
120440973Sbostic 		a->q_ruser = newstr(DefUser);
120540973Sbostic 	}
120640973Sbostic 
120740973Sbostic 	a->q_flags |= QGOODUID;		/* flag as a "ctladdr"  */
1208*54974Seric 	return a;
120940973Sbostic }
1210