xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 67547)
122708Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362528Sbostic  * Copyright (c) 1988, 1993
462528Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822708Sdist 
933731Sbostic # include "sendmail.h"
1022708Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef QUEUE
13*67547Seric static char sccsid[] = "@(#)queue.c	8.43 (Berkeley) 07/23/94 (with queueing)";
1433731Sbostic #else
15*67547Seric static char sccsid[] = "@(#)queue.c	8.43 (Berkeley) 07/23/94 (without queueing)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
194632Seric # include <errno.h>
2040973Sbostic # include <pwd.h>
2157736Seric # include <dirent.h>
224632Seric 
2333731Sbostic # ifdef QUEUE
244632Seric 
254632Seric /*
269377Seric **  Work queue.
279377Seric */
289377Seric 
299377Seric struct work
309377Seric {
319377Seric 	char		*w_name;	/* name of control file */
329377Seric 	long		w_pri;		/* priority of message, see below */
3325013Seric 	time_t		w_ctime;	/* creation time of message */
349377Seric 	struct work	*w_next;	/* next in queue */
359377Seric };
369377Seric 
379377Seric typedef struct work	WORK;
389377Seric 
399377Seric WORK	*WorkQ;			/* queue of things to be done */
409377Seric /*
414632Seric **  QUEUEUP -- queue a message up for future transmission.
424632Seric **
434632Seric **	Parameters:
446980Seric **		e -- the envelope to queue up.
456999Seric **		queueall -- if TRUE, queue all addresses, rather than
466999Seric **			just those with the QQUEUEUP flag set.
479377Seric **		announce -- if TRUE, tell when you are queueing up.
484632Seric **
494632Seric **	Returns:
5051920Seric **		none.
514632Seric **
524632Seric **	Side Effects:
539377Seric **		The current request are saved in a control file.
5451920Seric **		The queue file is left locked.
554632Seric */
564632Seric 
579377Seric queueup(e, queueall, announce)
586980Seric 	register ENVELOPE *e;
596999Seric 	bool queueall;
609377Seric 	bool announce;
614632Seric {
627812Seric 	char *qf;
637812Seric 	register FILE *tfp;
644632Seric 	register HDR *h;
655007Seric 	register ADDRESS *q;
6651920Seric 	int fd;
6751920Seric 	int i;
6851920Seric 	bool newid;
6953400Seric 	register char *p;
7010173Seric 	MAILER nullmailer;
7165870Seric 	MCI mcibuf;
7251920Seric 	char buf[MAXLINE], tf[MAXLINE];
734632Seric 
745037Seric 	/*
7517477Seric 	**  Create control file.
765037Seric 	*/
774632Seric 
7864745Seric 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
7964277Seric 
8064277Seric 	/* if newid, queuename will create a locked qf file in e->lockfp */
8151920Seric 	strcpy(tf, queuename(e, 't'));
8251920Seric 	tfp = e->e_lockfp;
8351920Seric 	if (tfp == NULL)
8451920Seric 		newid = FALSE;
8564277Seric 
8664277Seric 	/* if newid, just write the qf file directly (instead of tf file) */
8764745Seric 	if (!newid)
8851835Seric 	{
8951920Seric 		/* get a locked tf file */
9064070Seric 		for (i = 0; i < 128; i++)
9151835Seric 		{
9251920Seric 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
9351920Seric 			if (fd < 0)
9451835Seric 			{
9564070Seric 				if (errno != EEXIST)
9664070Seric 					break;
9764070Seric #ifdef LOG
9864070Seric 				if (LogLevel > 0 && (i % 32) == 0)
9965090Seric 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
10065090Seric 						tf, geteuid(), errstring(errno));
10164070Seric #endif
10241636Srick 			}
10365090Seric 			else
10465090Seric 			{
10565090Seric 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
10665090Seric 					break;
10764070Seric #ifdef LOG
10865090Seric 				else if (LogLevel > 0 && (i % 32) == 0)
10965090Seric 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
11065090Seric 						tf, errstring(errno));
11164070Seric #endif
11265090Seric 				close(fd);
11365090Seric 			}
11458689Seric 
11564070Seric 			if ((i % 32) == 31)
11664070Seric 			{
11764070Seric 				/* save the old temp file away */
11864070Seric 				(void) rename(tf, queuename(e, 'T'));
11964070Seric 			}
12064070Seric 			else
12164070Seric 				sleep(i % 32);
12241636Srick 		}
12364724Seric 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
12464921Seric 		{
12564921Seric 			printopenfds(TRUE);
12665090Seric 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
12765090Seric 				tf, geteuid());
12864921Seric 		}
12951920Seric 	}
1304632Seric 
1317677Seric 	if (tTd(40, 1))
13264745Seric 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
13364745Seric 			newid ? " (new id)" : "");
13464745Seric 	if (tTd(40, 9))
13564745Seric 	{
13664745Seric 		printf("  tfp=");
13764745Seric 		dumpfd(fileno(tfp), TRUE, FALSE);
13864745Seric 		printf("  lockfp=");
13964745Seric 		if (e->e_lockfp == NULL)
14064745Seric 			printf("NULL\n");
14164745Seric 		else
14264745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
14364745Seric 	}
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 
15464086Seric 		e->e_df = queuename(e, 'd');
15564086Seric 		e->e_df = newstr(e->e_df);
15666891Seric 		fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
15764724Seric 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
15865090Seric 			syserr("!queueup: cannot create data temp file %s, uid=%d",
15965090Seric 				e->e_df, geteuid());
16065870Seric 		bzero(&mcibuf, sizeof mcibuf);
16165870Seric 		mcibuf.mci_out = dfp;
16265870Seric 		mcibuf.mci_mailer = FileMailer;
16365870Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
16458680Seric 		(void) xfclose(dfp, "queueup dfp", e->e_id);
1659389Seric 		e->e_putbody = putbody;
1666980Seric 	}
1676980Seric 
1686980Seric 	/*
1694632Seric 	**  Output future work requests.
17025687Seric 	**	Priority and creation time should be first, since
17125687Seric 	**	they are required by orderq.
1724632Seric 	*/
1734632Seric 
1749377Seric 	/* output message priority */
1759377Seric 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
1769377Seric 
1779630Seric 	/* output creation time */
1789630Seric 	fprintf(tfp, "T%ld\n", e->e_ctime);
1799630Seric 
18059093Seric 	/* output type and name of data file */
18159093Seric 	if (e->e_bodytype != NULL)
18259093Seric 		fprintf(tfp, "B%s\n", e->e_bodytype);
1837812Seric 	fprintf(tfp, "D%s\n", e->e_df);
1844632Seric 
18510108Seric 	/* message from envelope, if it exists */
18610108Seric 	if (e->e_message != NULL)
18710108Seric 		fprintf(tfp, "M%s\n", e->e_message);
18810108Seric 
18958737Seric 	/* send various flag bits through */
19058737Seric 	p = buf;
19158737Seric 	if (bitset(EF_WARNING, e->e_flags))
19258737Seric 		*p++ = 'w';
19358737Seric 	if (bitset(EF_RESPONSE, e->e_flags))
19458737Seric 		*p++ = 'r';
195*67547Seric 	if (bitset(EF_HAS8BIT, e->e_flags))
196*67547Seric 		*p++ = '8';
19758737Seric 	*p++ = '\0';
19858737Seric 	if (buf[0] != '\0')
19958737Seric 		fprintf(tfp, "F%s\n", buf);
20058737Seric 
20158957Seric 	/* $r and $s and $_ macro values */
20253400Seric 	if ((p = macvalue('r', e)) != NULL)
20353400Seric 		fprintf(tfp, "$r%s\n", p);
20453400Seric 	if ((p = macvalue('s', e)) != NULL)
20553400Seric 		fprintf(tfp, "$s%s\n", p);
20658957Seric 	if ((p = macvalue('_', e)) != NULL)
20758957Seric 		fprintf(tfp, "$_%s\n", p);
20853400Seric 
2094632Seric 	/* output name of sender */
2107812Seric 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
2114632Seric 
21255360Seric 	/* output list of error recipients */
21359670Seric 	printctladdr(NULL, NULL);
21455360Seric 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
21555360Seric 	{
21658680Seric 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
21755360Seric 		{
21859113Seric 			printctladdr(q, tfp);
21955360Seric 			fprintf(tfp, "E%s\n", q->q_paddr);
22055360Seric 		}
22155360Seric 	}
22255360Seric 
2234632Seric 	/* output list of recipient addresses */
2246980Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2254632Seric 	{
22658250Seric 		if (bitset(QQUEUEUP, q->q_flags) ||
22758680Seric 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
2288245Seric 		{
22959113Seric 			printctladdr(q, tfp);
2307812Seric 			fprintf(tfp, "R%s\n", q->q_paddr);
2319377Seric 			if (announce)
2329377Seric 			{
2339377Seric 				e->e_to = q->q_paddr;
23458151Seric 				message("queued");
23558020Seric 				if (LogLevel > 8)
23664771Seric 					logdelivery(NULL, NULL, "queued", NULL, e);
2379377Seric 				e->e_to = NULL;
2389377Seric 			}
2399387Seric 			if (tTd(40, 1))
2409387Seric 			{
2419387Seric 				printf("queueing ");
2429387Seric 				printaddr(q, FALSE);
2439387Seric 			}
2448245Seric 		}
2454632Seric 	}
2464632Seric 
2479377Seric 	/*
2489377Seric 	**  Output headers for this message.
2499377Seric 	**	Expand macros completely here.  Queue run will deal with
2509377Seric 	**	everything as absolute headers.
2519377Seric 	**		All headers that must be relative to the recipient
2529377Seric 	**		can be cracked later.
25310173Seric 	**	We set up a "null mailer" -- i.e., a mailer that will have
25410173Seric 	**	no effect on the addresses as they are output.
2559377Seric 	*/
2569377Seric 
25710686Seric 	bzero((char *) &nullmailer, sizeof nullmailer);
25858020Seric 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
25965584Seric 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
26010349Seric 	nullmailer.m_eol = "\n";
26165870Seric 	bzero(&mcibuf, sizeof mcibuf);
26265870Seric 	mcibuf.mci_mailer = &nullmailer;
26365870Seric 	mcibuf.mci_out = tfp;
26410173Seric 
26558050Seric 	define('g', "\201f", e);
2666980Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2674632Seric 	{
26810686Seric 		extern bool bitzerop();
26910686Seric 
27012015Seric 		/* don't output null headers */
2714632Seric 		if (h->h_value == NULL || h->h_value[0] == '\0')
2724632Seric 			continue;
27312015Seric 
27412015Seric 		/* don't output resent headers on non-resent messages */
27512015Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
27612015Seric 			continue;
27712015Seric 
27865267Seric 		/* expand macros; if null, don't output header at all */
27965267Seric 		if (bitset(H_DEFAULT, h->h_flags))
28065267Seric 		{
28165267Seric 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
28265267Seric 			if (buf[0] == '\0')
28365267Seric 				continue;
28465267Seric 		}
28565267Seric 
28612015Seric 		/* output this header */
2877812Seric 		fprintf(tfp, "H");
28812015Seric 
28912015Seric 		/* if conditional, output the set of conditions */
29010686Seric 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
29110686Seric 		{
29210686Seric 			int j;
29310686Seric 
29423098Seric 			(void) putc('?', tfp);
29510686Seric 			for (j = '\0'; j <= '\177'; j++)
29610686Seric 				if (bitnset(j, h->h_mflags))
29723098Seric 					(void) putc(j, tfp);
29823098Seric 			(void) putc('?', tfp);
29910686Seric 		}
30012015Seric 
30112015Seric 		/* output the header: expand macros, convert addresses */
3027763Seric 		if (bitset(H_DEFAULT, h->h_flags))
3037763Seric 		{
30465267Seric 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
3057763Seric 		}
3068245Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
3079348Seric 		{
30858737Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
30963753Seric 			FILE *savetrace = TrafficLogFile;
31058737Seric 
31163753Seric 			TrafficLogFile = NULL;
31263753Seric 
31358737Seric 			if (bitset(H_FROM, h->h_flags))
31458737Seric 				oldstyle = FALSE;
31558737Seric 
31665870Seric 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
31763753Seric 
31863753Seric 			TrafficLogFile = savetrace;
3199348Seric 		}
3207763Seric 		else
3218245Seric 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
3224632Seric 	}
3234632Seric 
3244632Seric 	/*
3254632Seric 	**  Clean up.
3264632Seric 	*/
3274632Seric 
32864762Seric 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
32958732Seric 	{
33058732Seric 		if (newid)
33158732Seric 			syserr("!552 Error writing control file %s", tf);
33258732Seric 		else
33358732Seric 			syserr("!452 Error writing control file %s", tf);
33458732Seric 	}
33558732Seric 
33651920Seric 	if (!newid)
33751920Seric 	{
33864277Seric 		/* rename (locked) tf to be (locked) qf */
33951920Seric 		qf = queuename(e, 'q');
34051920Seric 		if (rename(tf, qf) < 0)
34165090Seric 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
34265090Seric 				tf, qf, e->e_df, geteuid());
34364277Seric 
34464277Seric 		/* close and unlock old (locked) qf */
34551920Seric 		if (e->e_lockfp != NULL)
34658680Seric 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
34751920Seric 		e->e_lockfp = tfp;
34851920Seric 	}
34951920Seric 	else
35051920Seric 		qf = tf;
35141636Srick 	errno = 0;
35264745Seric 	e->e_flags |= EF_INQUEUE;
3537391Seric 
3547677Seric # ifdef LOG
3557677Seric 	/* save log info */
35658020Seric 	if (LogLevel > 79)
3577878Seric 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
35856795Seric # endif /* LOG */
35964307Seric 
36064307Seric 	if (tTd(40, 1))
36164307Seric 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
36251920Seric 	return;
3634632Seric }
36454974Seric 
36554974Seric printctladdr(a, tfp)
36659113Seric 	register ADDRESS *a;
36754974Seric 	FILE *tfp;
36854974Seric {
36959113Seric 	char *uname;
37059113Seric 	register struct passwd *pw;
37159113Seric 	register ADDRESS *q;
37259113Seric 	uid_t uid;
37359113Seric 	static ADDRESS *lastctladdr;
37459113Seric 	static uid_t lastuid;
37554974Seric 
37659113Seric 	/* initialization */
37763850Seric 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
37854974Seric 	{
37959670Seric 		if (lastctladdr != NULL && tfp != NULL)
38059113Seric 			fprintf(tfp, "C\n");
38159113Seric 		lastctladdr = NULL;
38259113Seric 		lastuid = 0;
38354974Seric 		return;
38454974Seric 	}
38559113Seric 
38659113Seric 	/* find the active uid */
38759113Seric 	q = getctladdr(a);
38859113Seric 	if (q == NULL)
38959113Seric 		uid = 0;
39054974Seric 	else
39159113Seric 		uid = q->q_uid;
39263850Seric 	a = a->q_alias;
39359113Seric 
39459113Seric 	/* check to see if this is the same as last time */
39559113Seric 	if (lastctladdr != NULL && uid == lastuid &&
39659113Seric 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
39759113Seric 		return;
39859113Seric 	lastuid = uid;
39959113Seric 	lastctladdr = a;
40059113Seric 
40159113Seric 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
40259270Seric 		uname = "";
40359113Seric 	else
40459113Seric 		uname = pw->pw_name;
40559113Seric 
40659113Seric 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
40754974Seric }
40854974Seric 
4094632Seric /*
4104632Seric **  RUNQUEUE -- run the jobs in the queue.
4114632Seric **
4124632Seric **	Gets the stuff out of the queue in some presumably logical
4134632Seric **	order and processes them.
4144632Seric **
4154632Seric **	Parameters:
41624941Seric **		forkflag -- TRUE if the queue scanning should be done in
41724941Seric **			a child process.  We double-fork so it is not our
41824941Seric **			child and we don't have to clean up after it.
4194632Seric **
4204632Seric **	Returns:
4214632Seric **		none.
4224632Seric **
4234632Seric **	Side Effects:
4244632Seric **		runs things in the mail queue.
4254632Seric */
4264632Seric 
42755360Seric ENVELOPE	QueueEnvelope;		/* the queue run envelope */
42855360Seric 
42955360Seric runqueue(forkflag)
4304639Seric 	bool forkflag;
4314632Seric {
43255360Seric 	register ENVELOPE *e;
43355360Seric 	extern ENVELOPE BlankEnvelope;
43424953Seric 
4357466Seric 	/*
43624953Seric 	**  If no work will ever be selected, don't even bother reading
43724953Seric 	**  the queue.
43824953Seric 	*/
43924953Seric 
44051920Seric 	CurrentLA = getla();	/* get load average */
44140934Srick 
44258132Seric 	if (shouldqueue(0L, curtime()))
44324953Seric 	{
44424953Seric 		if (Verbose)
44524953Seric 			printf("Skipping queue run -- load average too high\n");
44658107Seric 		if (forkflag && QueueIntvl != 0)
44758107Seric 			(void) setevent(QueueIntvl, runqueue, TRUE);
44855360Seric 		return;
44924953Seric 	}
45024953Seric 
45124953Seric 	/*
4527466Seric 	**  See if we want to go off and do other useful work.
4537466Seric 	*/
4544639Seric 
4554639Seric 	if (forkflag)
4564639Seric 	{
4577943Seric 		int pid;
45865223Seric #ifdef SIGCHLD
45965223Seric 		extern void reapchild();
4607943Seric 
46165223Seric 		(void) setsignal(SIGCHLD, reapchild);
46265223Seric #endif
46365223Seric 
4647943Seric 		pid = dofork();
4657943Seric 		if (pid != 0)
4664639Seric 		{
4677943Seric 			/* parent -- pick up intermediate zombie */
46825184Seric #ifndef SIGCHLD
4699377Seric 			(void) waitfor(pid);
47056795Seric #endif /* SIGCHLD */
4717690Seric 			if (QueueIntvl != 0)
4729348Seric 				(void) setevent(QueueIntvl, runqueue, TRUE);
4734639Seric 			return;
4744639Seric 		}
4757943Seric 		/* child -- double fork */
47625184Seric #ifndef SIGCHLD
4777943Seric 		if (fork() != 0)
4787943Seric 			exit(EX_OK);
47956795Seric #else /* SIGCHLD */
48064035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
48156795Seric #endif /* SIGCHLD */
4824639Seric 	}
48324941Seric 
48440934Srick 	setproctitle("running queue: %s", QueueDir);
48524941Seric 
4867876Seric # ifdef LOG
48758020Seric 	if (LogLevel > 69)
48855360Seric 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
48955360Seric 			QueueDir, getpid(), forkflag);
49056795Seric # endif /* LOG */
4914639Seric 
4927466Seric 	/*
49310205Seric 	**  Release any resources used by the daemon code.
49410205Seric 	*/
49510205Seric 
49610205Seric # ifdef DAEMON
49710205Seric 	clrdaemon();
49856795Seric # endif /* DAEMON */
49910205Seric 
50064658Seric 	/* force it to run expensive jobs */
50164658Seric 	NoConnect = FALSE;
50264658Seric 
50310205Seric 	/*
50455360Seric 	**  Create ourselves an envelope
50555360Seric 	*/
50655360Seric 
50755360Seric 	CurEnv = &QueueEnvelope;
50858179Seric 	e = newenvelope(&QueueEnvelope, CurEnv);
50955360Seric 	e->e_flags = BlankEnvelope.e_flags;
51055360Seric 
51155360Seric 	/*
51227175Seric 	**  Make sure the alias database is open.
51327175Seric 	*/
51427175Seric 
51560537Seric 	initmaps(FALSE, e);
51627175Seric 
51727175Seric 	/*
5187466Seric 	**  Start making passes through the queue.
5197466Seric 	**	First, read and sort the entire queue.
5207466Seric 	**	Then, process the work in that order.
5217466Seric 	**		But if you take too long, start over.
5227466Seric 	*/
5237466Seric 
5247943Seric 	/* order the existing work requests */
52524954Seric 	(void) orderq(FALSE);
5267690Seric 
5277943Seric 	/* process them once at a time */
5287943Seric 	while (WorkQ != NULL)
5294639Seric 	{
5307943Seric 		WORK *w = WorkQ;
5317881Seric 
5327943Seric 		WorkQ = WorkQ->w_next;
53358884Seric 
53458884Seric 		/*
53558884Seric 		**  Ignore jobs that are too expensive for the moment.
53658884Seric 		*/
53758884Seric 
53858884Seric 		if (shouldqueue(w->w_pri, w->w_ctime))
53958884Seric 		{
54058884Seric 			if (Verbose)
54158884Seric 				printf("\nSkipping %s\n", w->w_name + 2);
54258884Seric 		}
54358930Seric 		else
54458930Seric 		{
54564296Seric 			pid_t pid;
54664296Seric 			extern pid_t dowork();
54764296Seric 
54864296Seric 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
54964296Seric 			errno = 0;
55066316Seric 			if (pid != 0)
55166316Seric 				(void) waitfor(pid);
55258930Seric 		}
5537943Seric 		free(w->w_name);
5547943Seric 		free((char *) w);
5554639Seric 	}
55629866Seric 
55729866Seric 	/* exit without the usual cleanup */
55855467Seric 	e->e_id = NULL;
55955467Seric 	finis();
5604634Seric }
5614634Seric /*
5624632Seric **  ORDERQ -- order the work queue.
5634632Seric **
5644632Seric **	Parameters:
56524941Seric **		doall -- if set, include everything in the queue (even
56624941Seric **			the jobs that cannot be run because the load
56724941Seric **			average is too high).  Otherwise, exclude those
56824941Seric **			jobs.
5694632Seric **
5704632Seric **	Returns:
57110121Seric **		The number of request in the queue (not necessarily
57210121Seric **		the number of requests in WorkQ however).
5734632Seric **
5744632Seric **	Side Effects:
5754632Seric **		Sets WorkQ to the queue of available work, in order.
5764632Seric */
5774632Seric 
57825687Seric # define NEED_P		001
57925687Seric # define NEED_T		002
58058318Seric # define NEED_R		004
58158318Seric # define NEED_S		010
5824632Seric 
58324941Seric orderq(doall)
58424941Seric 	bool doall;
5854632Seric {
58660219Seric 	register struct dirent *d;
5874632Seric 	register WORK *w;
5886625Sglickman 	DIR *f;
5894632Seric 	register int i;
59025687Seric 	WORK wlist[QUEUESIZE+1];
59110070Seric 	int wn = -1;
5924632Seric 	extern workcmpf();
5934632Seric 
59458318Seric 	if (tTd(41, 1))
59558318Seric 	{
59658318Seric 		printf("orderq:\n");
59758318Seric 		if (QueueLimitId != NULL)
59858318Seric 			printf("\tQueueLimitId = %s\n", QueueLimitId);
59958318Seric 		if (QueueLimitSender != NULL)
60058318Seric 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
60158318Seric 		if (QueueLimitRecipient != NULL)
60258318Seric 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
60358318Seric 	}
60458318Seric 
6054632Seric 	/* clear out old WorkQ */
6064632Seric 	for (w = WorkQ; w != NULL; )
6074632Seric 	{
6084632Seric 		register WORK *nw = w->w_next;
6094632Seric 
6104632Seric 		WorkQ = nw;
6114632Seric 		free(w->w_name);
6124632Seric 		free((char *) w);
6134632Seric 		w = nw;
6144632Seric 	}
6154632Seric 
6164632Seric 	/* open the queue directory */
6178148Seric 	f = opendir(".");
6184632Seric 	if (f == NULL)
6194632Seric 	{
6208148Seric 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
62110070Seric 		return (0);
6224632Seric 	}
6234632Seric 
6244632Seric 	/*
6254632Seric 	**  Read the work directory.
6264632Seric 	*/
6274632Seric 
62810070Seric 	while ((d = readdir(f)) != NULL)
6294632Seric 	{
6309377Seric 		FILE *cf;
63164492Seric 		register char *p;
6324632Seric 		char lbuf[MAXNAME];
63358318Seric 		extern bool strcontainedin();
6344632Seric 
6354632Seric 		/* is this an interesting entry? */
6367812Seric 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
6374632Seric 			continue;
6384632Seric 
63958318Seric 		if (QueueLimitId != NULL &&
64058318Seric 		    !strcontainedin(QueueLimitId, d->d_name))
64158318Seric 			continue;
64258318Seric 
64358722Seric 		/*
64458722Seric 		**  Check queue name for plausibility.  This handles
64558722Seric 		**  both old and new type ids.
64658722Seric 		*/
64758722Seric 
64864492Seric 		p = d->d_name + 2;
64964492Seric 		if (isupper(p[0]) && isupper(p[2]))
65064492Seric 			p += 3;
65164492Seric 		else if (isupper(p[1]))
65264492Seric 			p += 2;
65364492Seric 		else
65464492Seric 			p = d->d_name;
65564492Seric 		for (i = 0; isdigit(*p); p++)
65664492Seric 			i++;
65764492Seric 		if (i < 5 || *p != '\0')
65858020Seric 		{
65958020Seric 			if (Verbose)
66058020Seric 				printf("orderq: bogus qf name %s\n", d->d_name);
66158020Seric #ifdef LOG
66258020Seric 			if (LogLevel > 3)
66359615Seric 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
66458020Seric 					d->d_name);
66558020Seric #endif
66658020Seric 			if (strlen(d->d_name) >= MAXNAME)
66758020Seric 				d->d_name[MAXNAME - 1] = '\0';
66858020Seric 			strcpy(lbuf, d->d_name);
66958020Seric 			lbuf[0] = 'Q';
67058020Seric 			(void) rename(d->d_name, lbuf);
67158020Seric 			continue;
67258020Seric 		}
67358020Seric 
67410070Seric 		/* yes -- open control file (if not too many files) */
67525687Seric 		if (++wn >= QUEUESIZE)
67610070Seric 			continue;
67758318Seric 
6788148Seric 		cf = fopen(d->d_name, "r");
6794632Seric 		if (cf == NULL)
6804632Seric 		{
6817055Seric 			/* this may be some random person sending hir msgs */
6827055Seric 			/* syserr("orderq: cannot open %s", cbuf); */
68310090Seric 			if (tTd(41, 2))
68410090Seric 				printf("orderq: cannot open %s (%d)\n",
68510090Seric 					d->d_name, errno);
6867055Seric 			errno = 0;
68710090Seric 			wn--;
6884632Seric 			continue;
6894632Seric 		}
69025687Seric 		w = &wlist[wn];
69125687Seric 		w->w_name = newstr(d->d_name);
6924632Seric 
69325027Seric 		/* make sure jobs in creation don't clog queue */
69425687Seric 		w->w_pri = 0x7fffffff;
69525687Seric 		w->w_ctime = 0;
69625027Seric 
6974632Seric 		/* extract useful information */
69825687Seric 		i = NEED_P | NEED_T;
69958318Seric 		if (QueueLimitSender != NULL)
70058318Seric 			i |= NEED_S;
70158318Seric 		if (QueueLimitRecipient != NULL)
70258318Seric 			i |= NEED_R;
70325687Seric 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
7044632Seric 		{
70524954Seric 			extern long atol();
70658318Seric 			extern bool strcontainedin();
70724954Seric 
70824941Seric 			switch (lbuf[0])
7094632Seric 			{
71024941Seric 			  case 'P':
71125687Seric 				w->w_pri = atol(&lbuf[1]);
71225687Seric 				i &= ~NEED_P;
7134632Seric 				break;
71425013Seric 
71525013Seric 			  case 'T':
71625687Seric 				w->w_ctime = atol(&lbuf[1]);
71725687Seric 				i &= ~NEED_T;
71825013Seric 				break;
71958318Seric 
72058318Seric 			  case 'R':
72158318Seric 				if (QueueLimitRecipient != NULL &&
72258318Seric 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
72358318Seric 					i &= ~NEED_R;
72458318Seric 				break;
72558318Seric 
72658318Seric 			  case 'S':
72758318Seric 				if (QueueLimitSender != NULL &&
72858318Seric 				    strcontainedin(QueueLimitSender, &lbuf[1]))
72958318Seric 					i &= ~NEED_S;
73058318Seric 				break;
7314632Seric 			}
7324632Seric 		}
7334632Seric 		(void) fclose(cf);
73424953Seric 
73558318Seric 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
73658318Seric 		    bitset(NEED_R|NEED_S, i))
73724953Seric 		{
73824953Seric 			/* don't even bother sorting this job in */
73924953Seric 			wn--;
74024953Seric 		}
7414632Seric 	}
7426625Sglickman 	(void) closedir(f);
74310090Seric 	wn++;
7444632Seric 
7454632Seric 	/*
7464632Seric 	**  Sort the work directory.
7474632Seric 	*/
7484632Seric 
74925687Seric 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
7504632Seric 
7514632Seric 	/*
7524632Seric 	**  Convert the work list into canonical form.
7539377Seric 	**	Should be turning it into a list of envelopes here perhaps.
7544632Seric 	*/
7554632Seric 
75624981Seric 	WorkQ = NULL;
75725687Seric 	for (i = min(wn, QUEUESIZE); --i >= 0; )
7584632Seric 	{
7594632Seric 		w = (WORK *) xalloc(sizeof *w);
7604632Seric 		w->w_name = wlist[i].w_name;
7614632Seric 		w->w_pri = wlist[i].w_pri;
76225013Seric 		w->w_ctime = wlist[i].w_ctime;
76324981Seric 		w->w_next = WorkQ;
76424981Seric 		WorkQ = w;
7654632Seric 	}
7664632Seric 
7677677Seric 	if (tTd(40, 1))
7684632Seric 	{
7694632Seric 		for (w = WorkQ; w != NULL; w = w->w_next)
7705037Seric 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
7714632Seric 	}
77210070Seric 
77310090Seric 	return (wn);
7744632Seric }
7754632Seric /*
7767677Seric **  WORKCMPF -- compare function for ordering work.
7774632Seric **
7784632Seric **	Parameters:
7794632Seric **		a -- the first argument.
7804632Seric **		b -- the second argument.
7814632Seric **
7824632Seric **	Returns:
78324981Seric **		-1 if a < b
78424981Seric **		 0 if a == b
78524981Seric **		+1 if a > b
7864632Seric **
7874632Seric **	Side Effects:
7884632Seric **		none.
7894632Seric */
7904632Seric 
7914632Seric workcmpf(a, b)
7925037Seric 	register WORK *a;
7935037Seric 	register WORK *b;
7944632Seric {
79557438Seric 	long pa = a->w_pri;
79657438Seric 	long pb = b->w_pri;
79724941Seric 
79824941Seric 	if (pa == pb)
7994632Seric 		return (0);
80024941Seric 	else if (pa > pb)
80124981Seric 		return (1);
80224981Seric 	else
80310121Seric 		return (-1);
8044632Seric }
8054632Seric /*
8064632Seric **  DOWORK -- do a work request.
8074632Seric **
8084632Seric **	Parameters:
80958884Seric **		id -- the ID of the job to run.
81058884Seric **		forkflag -- if set, run this in background.
81158924Seric **		requeueflag -- if set, reinstantiate the queue quickly.
81258924Seric **			This is used when expanding aliases in the queue.
81361094Seric **			If forkflag is also set, it doesn't wait for the
81461094Seric **			child.
81558884Seric **		e - the envelope in which to run it.
8164632Seric **
8174632Seric **	Returns:
81864296Seric **		process id of process that is running the queue job.
8194632Seric **
8204632Seric **	Side Effects:
8214632Seric **		The work request is satisfied if possible.
8224632Seric */
8234632Seric 
82464296Seric pid_t
82558924Seric dowork(id, forkflag, requeueflag, e)
82658884Seric 	char *id;
82758884Seric 	bool forkflag;
82858924Seric 	bool requeueflag;
82955012Seric 	register ENVELOPE *e;
8304632Seric {
83164296Seric 	register pid_t pid;
83251920Seric 	extern bool readqf();
8334632Seric 
8347677Seric 	if (tTd(40, 1))
83558884Seric 		printf("dowork(%s)\n", id);
8364632Seric 
8374632Seric 	/*
83824941Seric 	**  Fork for work.
83924941Seric 	*/
84024941Seric 
84158884Seric 	if (forkflag)
84224941Seric 	{
84364296Seric 		pid = fork();
84464296Seric 		if (pid < 0)
84524941Seric 		{
84624941Seric 			syserr("dowork: cannot fork");
84764296Seric 			return 0;
84824941Seric 		}
84964839Seric 		else if (pid > 0)
85064839Seric 		{
85164839Seric 			/* parent -- clean out connection cache */
85264839Seric 			mci_flush(FALSE, NULL);
85364839Seric 		}
85424941Seric 	}
85524941Seric 	else
85624941Seric 	{
85764296Seric 		pid = 0;
85824941Seric 	}
85924941Seric 
86064296Seric 	if (pid == 0)
8614632Seric 	{
8624632Seric 		/*
8634632Seric 		**  CHILD
8648148Seric 		**	Lock the control file to avoid duplicate deliveries.
8658148Seric 		**		Then run the file as though we had just read it.
8667350Seric 		**	We save an idea of the temporary name so we
8677350Seric 		**		can recover on interrupt.
8684632Seric 		*/
8694632Seric 
8707763Seric 		/* set basic modes, etc. */
8717356Seric 		(void) alarm(0);
87255012Seric 		clearenvelope(e, FALSE);
87364554Seric 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
87458734Seric 		e->e_errormode = EM_MAIL;
87558884Seric 		e->e_id = id;
87664765Seric 		GrabTo = UseErrorsTo = FALSE;
87766316Seric 		ExitStat = EX_OK;
87863846Seric 		if (forkflag)
87964554Seric 		{
88064296Seric 			disconnect(1, e);
88164554Seric 			OpMode = MD_DELIVER;
88264554Seric 		}
8837876Seric # ifdef LOG
88458020Seric 		if (LogLevel > 76)
88555012Seric 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
8867881Seric 			       getpid());
88756795Seric # endif /* LOG */
8887763Seric 
8897763Seric 		/* don't use the headers from sendmail.cf... */
89055012Seric 		e->e_header = NULL;
8917763Seric 
89251920Seric 		/* read the queue control file -- return if locked */
89365200Seric 		if (!readqf(e))
8946980Seric 		{
89558884Seric 			if (tTd(40, 4))
89658884Seric 				printf("readqf(%s) failed\n", e->e_id);
89758884Seric 			if (forkflag)
89824941Seric 				exit(EX_OK);
89924941Seric 			else
90066316Seric 				return 0;
9016980Seric 		}
9026980Seric 
90355012Seric 		e->e_flags |= EF_INQUEUE;
90458929Seric 		eatheader(e, requeueflag);
9056980Seric 
90658924Seric 		if (requeueflag)
90758924Seric 			queueup(e, TRUE, FALSE);
90858924Seric 
9096980Seric 		/* do the delivery */
91058915Seric 		sendall(e, SM_DELIVER);
9116980Seric 
9126980Seric 		/* finish up and exit */
91358884Seric 		if (forkflag)
91424941Seric 			finis();
91524941Seric 		else
91655012Seric 			dropenvelope(e);
9174632Seric 	}
91864296Seric 	e->e_id = NULL;
91964296Seric 	return pid;
9204632Seric }
9214632Seric /*
9224632Seric **  READQF -- read queue file and set up environment.
9234632Seric **
9244632Seric **	Parameters:
9259377Seric **		e -- the envelope of the job to run.
9264632Seric **
9274632Seric **	Returns:
92851920Seric **		TRUE if it successfully read the queue file.
92951920Seric **		FALSE otherwise.
9304632Seric **
9314632Seric **	Side Effects:
93251920Seric **		The queue file is returned locked.
9334632Seric */
9344632Seric 
93551920Seric bool
93665200Seric readqf(e)
9379377Seric 	register ENVELOPE *e;
9384632Seric {
93917477Seric 	register FILE *qfp;
94054974Seric 	ADDRESS *ctladdr;
94156400Seric 	struct stat st;
94257135Seric 	char *bp;
94358915Seric 	char qf[20];
94457135Seric 	char buf[MAXLINE];
94524954Seric 	extern long atol();
94654974Seric 	extern ADDRESS *setctluser();
9474632Seric 
9484632Seric 	/*
94917468Seric 	**  Read and process the file.
9504632Seric 	*/
9514632Seric 
95258915Seric 	strcpy(qf, queuename(e, 'q'));
95351937Seric 	qfp = fopen(qf, "r+");
95417477Seric 	if (qfp == NULL)
95517477Seric 	{
95658884Seric 		if (tTd(40, 8))
95758884Seric 			printf("readqf(%s): fopen failure (%s)\n",
95858884Seric 				qf, errstring(errno));
95940934Srick 		if (errno != ENOENT)
96040934Srick 			syserr("readqf: no control file %s", qf);
96151920Seric 		return FALSE;
96217477Seric 	}
96340934Srick 
96464335Seric 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
96564277Seric 	{
96664277Seric 		/* being processed by another queuer */
96764277Seric 		if (tTd(40, 8))
96864277Seric 			printf("readqf(%s): locked\n", qf);
96964277Seric 		if (Verbose)
97064277Seric 			printf("%s: locked\n", e->e_id);
97164277Seric # ifdef LOG
97264277Seric 		if (LogLevel > 19)
97364277Seric 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
97464277Seric # endif /* LOG */
97564277Seric 		(void) fclose(qfp);
97664277Seric 		return FALSE;
97764277Seric 	}
97864277Seric 
97956400Seric 	/*
98056400Seric 	**  Check the queue file for plausibility to avoid attacks.
98156400Seric 	*/
98256400Seric 
98356400Seric 	if (fstat(fileno(qfp), &st) < 0)
98456400Seric 	{
98556400Seric 		/* must have been being processed by someone else */
98658884Seric 		if (tTd(40, 8))
98758884Seric 			printf("readqf(%s): fstat failure (%s)\n",
98858884Seric 				qf, errstring(errno));
98956400Seric 		fclose(qfp);
99056400Seric 		return FALSE;
99156400Seric 	}
99256400Seric 
99364137Seric 	if (st.st_uid != geteuid())
99456400Seric 	{
99556400Seric # ifdef LOG
99656400Seric 		if (LogLevel > 0)
99756400Seric 		{
99856400Seric 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
99956400Seric 				e->e_id, st.st_uid, st.st_mode);
100056400Seric 		}
100156795Seric # endif /* LOG */
100258884Seric 		if (tTd(40, 8))
100358884Seric 			printf("readqf(%s): bogus file\n", qf);
100464277Seric 		rename(qf, queuename(e, 'Q'));
100556400Seric 		fclose(qfp);
100656400Seric 		return FALSE;
100756400Seric 	}
100856400Seric 
100964277Seric 	if (st.st_size == 0)
101040934Srick 	{
101164277Seric 		/* must be a bogus file -- just remove it */
101264277Seric 		(void) unlink(qf);
101364277Seric 		fclose(qfp);
101451920Seric 		return FALSE;
101540934Srick 	}
101640934Srick 
101764277Seric 	if (st.st_nlink == 0)
101859101Seric 	{
101964277Seric 		/*
102064277Seric 		**  Race condition -- we got a file just as it was being
102164277Seric 		**  unlinked.  Just assume it is zero length.
102264277Seric 		*/
102364277Seric 
102459101Seric 		fclose(qfp);
102559101Seric 		return FALSE;
102659101Seric 	}
102759101Seric 
102864277Seric 	/* good file -- save this lock */
102951920Seric 	e->e_lockfp = qfp;
103051920Seric 
103140934Srick 	/* do basic system initialization */
103255012Seric 	initsys(e);
103365164Seric 	define('i', e->e_id, e);
103440934Srick 
10359377Seric 	LineNumber = 0;
103663850Seric 	e->e_flags |= EF_GLOBALERRS;
103763850Seric 	OpMode = MD_DELIVER;
103851920Seric 	if (Verbose)
10399377Seric 		printf("\nRunning %s\n", e->e_id);
104054974Seric 	ctladdr = NULL;
104157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
10424632Seric 	{
104358737Seric 		register char *p;
104457529Seric 		struct stat st;
104557529Seric 
104626504Seric 		if (tTd(40, 4))
104757135Seric 			printf("+++++ %s\n", bp);
104857135Seric 		switch (bp[0])
10494632Seric 		{
105040973Sbostic 		  case 'C':		/* specify controlling user */
105157135Seric 			ctladdr = setctluser(&bp[1]);
105240973Sbostic 			break;
105340973Sbostic 
10544632Seric 		  case 'R':		/* specify recipient */
105558082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
10564632Seric 			break;
10574632Seric 
105825687Seric 		  case 'E':		/* specify error recipient */
105958082Seric 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
106025687Seric 			break;
106125687Seric 
10624632Seric 		  case 'H':		/* header */
106357135Seric 			(void) chompheader(&bp[1], FALSE, e);
10644632Seric 			break;
10654632Seric 
106610108Seric 		  case 'M':		/* message */
106764705Seric 			/* ignore this; we want a new message next time */
106810108Seric 			break;
106910108Seric 
10704632Seric 		  case 'S':		/* sender */
107158704Seric 			setsender(newstr(&bp[1]), e, NULL, TRUE);
10724632Seric 			break;
10734632Seric 
107459093Seric 		  case 'B':		/* body type */
107559093Seric 			e->e_bodytype = newstr(&bp[1]);
107659093Seric 			break;
107759093Seric 
10784632Seric 		  case 'D':		/* data file name */
107957135Seric 			e->e_df = newstr(&bp[1]);
10809544Seric 			e->e_dfp = fopen(e->e_df, "r");
10819544Seric 			if (e->e_dfp == NULL)
108258020Seric 			{
10839377Seric 				syserr("readqf: cannot open %s", e->e_df);
108458020Seric 				e->e_msgsize = -1;
108558020Seric 			}
108658020Seric 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
108757529Seric 				e->e_msgsize = st.st_size;
10884632Seric 			break;
10894632Seric 
10907860Seric 		  case 'T':		/* init time */
109157135Seric 			e->e_ctime = atol(&bp[1]);
10924632Seric 			break;
10934632Seric 
10944634Seric 		  case 'P':		/* message priority */
109557135Seric 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
10964634Seric 			break;
10974634Seric 
109858737Seric 		  case 'F':		/* flag bits */
109958737Seric 			for (p = &bp[1]; *p != '\0'; p++)
110058737Seric 			{
110158737Seric 				switch (*p)
110258737Seric 				{
110358737Seric 				  case 'w':	/* warning sent */
110458737Seric 					e->e_flags |= EF_WARNING;
110558737Seric 					break;
110658737Seric 
110758737Seric 				  case 'r':	/* response */
110858737Seric 					e->e_flags |= EF_RESPONSE;
110958737Seric 					break;
1110*67547Seric 
1111*67547Seric 				  case '8':	/* has 8 bit data */
1112*67547Seric 					e->e_flags |= EF_HAS8BIT;
1113*67547Seric 					break;
111458737Seric 				}
111558737Seric 			}
111658737Seric 			break;
111758737Seric 
111853400Seric 		  case '$':		/* define macro */
111957135Seric 			define(bp[1], newstr(&bp[2]), e);
112053400Seric 			break;
112153400Seric 
112224941Seric 		  case '\0':		/* blank line; ignore */
112324941Seric 			break;
112424941Seric 
11254632Seric 		  default:
112665960Seric 			syserr("readqf: %s: line %d: bad line \"%s\"",
112765200Seric 				qf, LineNumber, bp);
112863753Seric 			fclose(qfp);
112963753Seric 			rename(qf, queuename(e, 'Q'));
113063753Seric 			return FALSE;
11314632Seric 		}
113257135Seric 
113357135Seric 		if (bp != buf)
113457135Seric 			free(bp);
11354632Seric 	}
11369377Seric 
113724941Seric 	/*
113824941Seric 	**  If we haven't read any lines, this queue file is empty.
113924941Seric 	**  Arrange to remove it without referencing any null pointers.
114024941Seric 	*/
114124941Seric 
114224941Seric 	if (LineNumber == 0)
114324941Seric 	{
114424941Seric 		errno = 0;
114524941Seric 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
114624941Seric 	}
114751920Seric 	return TRUE;
11484632Seric }
11494632Seric /*
11509630Seric **  PRINTQUEUE -- print out a representation of the mail queue
11519630Seric **
11529630Seric **	Parameters:
11539630Seric **		none.
11549630Seric **
11559630Seric **	Returns:
11569630Seric **		none.
11579630Seric **
11589630Seric **	Side Effects:
11599630Seric **		Prints a listing of the mail queue on the standard output.
11609630Seric */
11615182Seric 
11629630Seric printqueue()
11639630Seric {
11649630Seric 	register WORK *w;
11659630Seric 	FILE *f;
116610070Seric 	int nrequests;
11679630Seric 	char buf[MAXLINE];
11689630Seric 
11699630Seric 	/*
117058250Seric 	**  Check for permission to print the queue
117158250Seric 	*/
117258250Seric 
117364333Seric 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
117458250Seric 	{
117558250Seric 		struct stat st;
117658523Seric # ifdef NGROUPS
117758523Seric 		int n;
117863937Seric 		GIDSET_T gidset[NGROUPS];
117958523Seric # endif
118058250Seric 
118158517Seric 		if (stat(QueueDir, &st) < 0)
118258250Seric 		{
118358250Seric 			syserr("Cannot stat %s", QueueDir);
118458250Seric 			return;
118558250Seric 		}
118658523Seric # ifdef NGROUPS
118758523Seric 		n = getgroups(NGROUPS, gidset);
118858523Seric 		while (--n >= 0)
118958523Seric 		{
119058523Seric 			if (gidset[n] == st.st_gid)
119158523Seric 				break;
119258523Seric 		}
119358523Seric 		if (n < 0)
119458523Seric # else
119563787Seric 		if (RealGid != st.st_gid)
119658523Seric # endif
119758250Seric 		{
119858250Seric 			usrerr("510 You are not permitted to see the queue");
119958250Seric 			setstat(EX_NOPERM);
120058250Seric 			return;
120158250Seric 		}
120258250Seric 	}
120358250Seric 
120458250Seric 	/*
12059630Seric 	**  Read and order the queue.
12069630Seric 	*/
12079630Seric 
120824941Seric 	nrequests = orderq(TRUE);
12099630Seric 
12109630Seric 	/*
12119630Seric 	**  Print the work list that we have read.
12129630Seric 	*/
12139630Seric 
12149630Seric 	/* first see if there is anything */
121510070Seric 	if (nrequests <= 0)
12169630Seric 	{
121710070Seric 		printf("Mail queue is empty\n");
12189630Seric 		return;
12199630Seric 	}
12209630Seric 
122151920Seric 	CurrentLA = getla();	/* get load average */
122240934Srick 
122310096Seric 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
122425687Seric 	if (nrequests > QUEUESIZE)
122525687Seric 		printf(", only %d printed", QUEUESIZE);
122624979Seric 	if (Verbose)
122758716Seric 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
122824979Seric 	else
122958716Seric 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
12309630Seric 	for (w = WorkQ; w != NULL; w = w->w_next)
12319630Seric 	{
12329630Seric 		struct stat st;
123310070Seric 		auto time_t submittime = 0;
123410070Seric 		long dfsize = -1;
123558737Seric 		int flags = 0;
123610108Seric 		char message[MAXLINE];
123759093Seric 		char bodytype[MAXNAME];
12389630Seric 
123964355Seric 		printf("%8s", w->w_name + 2);
124017468Seric 		f = fopen(w->w_name, "r");
124117468Seric 		if (f == NULL)
124217468Seric 		{
124364355Seric 			printf(" (job completed)\n");
124417468Seric 			errno = 0;
124517468Seric 			continue;
124617468Seric 		}
124764335Seric 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
124810070Seric 			printf("*");
124957438Seric 		else if (shouldqueue(w->w_pri, w->w_ctime))
125024941Seric 			printf("X");
125110070Seric 		else
125210070Seric 			printf(" ");
125310070Seric 		errno = 0;
125417468Seric 
125559093Seric 		message[0] = bodytype[0] = '\0';
12569630Seric 		while (fgets(buf, sizeof buf, f) != NULL)
12579630Seric 		{
125853400Seric 			register int i;
125958737Seric 			register char *p;
126053400Seric 
12619630Seric 			fixcrlf(buf, TRUE);
12629630Seric 			switch (buf[0])
12639630Seric 			{
126410108Seric 			  case 'M':	/* error message */
126553400Seric 				if ((i = strlen(&buf[1])) >= sizeof message)
126658737Seric 					i = sizeof message - 1;
126753400Seric 				bcopy(&buf[1], message, i);
126853400Seric 				message[i] = '\0';
126910108Seric 				break;
127010108Seric 
127159093Seric 			  case 'B':	/* body type */
127259093Seric 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
127359093Seric 					i = sizeof bodytype - 1;
127459093Seric 				bcopy(&buf[1], bodytype, i);
127559093Seric 				bodytype[i] = '\0';
127659093Seric 				break;
127759093Seric 
12789630Seric 			  case 'S':	/* sender name */
127924979Seric 				if (Verbose)
128058737Seric 					printf("%8ld %10ld%c%.12s %.38s",
128158737Seric 					    dfsize,
128258737Seric 					    w->w_pri,
128358737Seric 					    bitset(EF_WARNING, flags) ? '+' : ' ',
128458737Seric 					    ctime(&submittime) + 4,
128524979Seric 					    &buf[1]);
128624979Seric 				else
128724979Seric 					printf("%8ld %.16s %.45s", dfsize,
128824979Seric 					    ctime(&submittime), &buf[1]);
128959093Seric 				if (message[0] != '\0' || bodytype[0] != '\0')
129059093Seric 				{
129159093Seric 					printf("\n    %10.10s", bodytype);
129259093Seric 					if (message[0] != '\0')
129359093Seric 						printf("   (%.60s)", message);
129459093Seric 				}
12959630Seric 				break;
129651920Seric 
129740973Sbostic 			  case 'C':	/* controlling user */
129854974Seric 				if (Verbose)
129958716Seric 					printf("\n\t\t\t\t      (---%.34s---)",
130058716Seric 						&buf[1]);
130140973Sbostic 				break;
13029630Seric 
13039630Seric 			  case 'R':	/* recipient name */
130424979Seric 				if (Verbose)
130558716Seric 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
130624979Seric 				else
130758716Seric 					printf("\n\t\t\t\t   %.45s", &buf[1]);
13089630Seric 				break;
13099630Seric 
13109630Seric 			  case 'T':	/* creation time */
131124941Seric 				submittime = atol(&buf[1]);
13129630Seric 				break;
131310070Seric 
131410070Seric 			  case 'D':	/* data file name */
131510070Seric 				if (stat(&buf[1], &st) >= 0)
131610070Seric 					dfsize = st.st_size;
131710070Seric 				break;
131858737Seric 
131958737Seric 			  case 'F':	/* flag bits */
132058737Seric 				for (p = &buf[1]; *p != '\0'; p++)
132158737Seric 				{
132258737Seric 					switch (*p)
132358737Seric 					{
132458737Seric 					  case 'w':
132558737Seric 						flags |= EF_WARNING;
132658737Seric 						break;
132758737Seric 					}
132858737Seric 				}
13299630Seric 			}
13309630Seric 		}
133110070Seric 		if (submittime == (time_t) 0)
133210070Seric 			printf(" (no control file)");
13339630Seric 		printf("\n");
133423098Seric 		(void) fclose(f);
13359630Seric 	}
13369630Seric }
13379630Seric 
133856795Seric # endif /* QUEUE */
133917468Seric /*
134017468Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
134117468Seric **
134217468Seric **	Assigns an id code if one does not already exist.
134317468Seric **	This code is very careful to avoid trashing existing files
134417468Seric **	under any circumstances.
134517468Seric **
134617468Seric **	Parameters:
134717468Seric **		e -- envelope to build it in/from.
134817468Seric **		type -- the file type, used as the first character
134917468Seric **			of the file name.
135017468Seric **
135117468Seric **	Returns:
135217468Seric **		a pointer to the new file name (in a static buffer).
135317468Seric **
135417468Seric **	Side Effects:
135551920Seric **		If no id code is already assigned, queuename will
135651920Seric **		assign an id code, create a qf file, and leave a
135751920Seric **		locked, open-for-write file pointer in the envelope.
135817468Seric */
135917468Seric 
136017468Seric char *
136117468Seric queuename(e, type)
136217468Seric 	register ENVELOPE *e;
136359700Seric 	int type;
136417468Seric {
136517468Seric 	static int pid = -1;
136658741Seric 	static char c0;
136758741Seric 	static char c1;
136858741Seric 	static char c2;
136958716Seric 	time_t now;
137058716Seric 	struct tm *tm;
137158689Seric 	static char buf[MAXNAME];
137217468Seric 
137317468Seric 	if (e->e_id == NULL)
137417468Seric 	{
137517468Seric 		char qf[20];
137617468Seric 
137717468Seric 		/* find a unique id */
137817468Seric 		if (pid != getpid())
137917468Seric 		{
138017468Seric 			/* new process -- start back at "AA" */
138117468Seric 			pid = getpid();
138258716Seric 			now = curtime();
138358716Seric 			tm = localtime(&now);
138458716Seric 			c0 = 'A' + tm->tm_hour;
138517468Seric 			c1 = 'A';
138617468Seric 			c2 = 'A' - 1;
138717468Seric 		}
138858716Seric 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
138917468Seric 
139017468Seric 		while (c1 < '~' || c2 < 'Z')
139117468Seric 		{
139217468Seric 			int i;
139317468Seric 
139417468Seric 			if (c2 >= 'Z')
139517468Seric 			{
139617468Seric 				c1++;
139717468Seric 				c2 = 'A' - 1;
139817468Seric 			}
139958716Seric 			qf[3] = c1;
140058716Seric 			qf[4] = ++c2;
140117468Seric 			if (tTd(7, 20))
140240934Srick 				printf("queuename: trying \"%s\"\n", qf);
140317468Seric 
140440934Srick 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
140551920Seric 			if (i < 0)
140651920Seric 			{
140751920Seric 				if (errno == EEXIST)
140851920Seric 					continue;
140964705Seric 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
141064705Seric 					qf, QueueDir, geteuid());
141151920Seric 				exit(EX_UNAVAILABLE);
141251920Seric 			}
141364335Seric 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
141451920Seric 			{
141551920Seric 				e->e_lockfp = fdopen(i, "w");
141640934Srick 				break;
141717468Seric 			}
141851920Seric 
141951920Seric 			/* a reader got the file; abandon it and try again */
142051920Seric 			(void) close(i);
142117468Seric 		}
142217468Seric 		if (c1 >= '~' && c2 >= 'Z')
142317468Seric 		{
142464705Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
142564705Seric 				qf, QueueDir, geteuid());
142617468Seric 			exit(EX_OSERR);
142717468Seric 		}
142817468Seric 		e->e_id = newstr(&qf[2]);
142917468Seric 		define('i', e->e_id, e);
143017468Seric 		if (tTd(7, 1))
143117468Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
143264745Seric 		if (tTd(7, 9))
143364745Seric 		{
143464745Seric 			printf("  lockfd=");
143564745Seric 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
143664745Seric 		}
143717468Seric # ifdef LOG
143858020Seric 		if (LogLevel > 93)
143917468Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
144056795Seric # endif /* LOG */
144117468Seric 	}
144217468Seric 
144317468Seric 	if (type == '\0')
144417468Seric 		return (NULL);
144517468Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
144617468Seric 	if (tTd(7, 2))
144717468Seric 		printf("queuename: %s\n", buf);
144817468Seric 	return (buf);
144917468Seric }
145017468Seric /*
145117468Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
145217468Seric **
145317468Seric **	Parameters:
145417468Seric **		e -- the envelope to unlock.
145517468Seric **
145617468Seric **	Returns:
145717468Seric **		none
145817468Seric **
145917468Seric **	Side Effects:
146017468Seric **		unlocks the queue for `e'.
146117468Seric */
146217468Seric 
146317468Seric unlockqueue(e)
146417468Seric 	ENVELOPE *e;
146517468Seric {
146658680Seric 	if (tTd(51, 4))
146758680Seric 		printf("unlockqueue(%s)\n", e->e_id);
146858680Seric 
146951920Seric 	/* if there is a lock file in the envelope, close it */
147051920Seric 	if (e->e_lockfp != NULL)
147158680Seric 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
147251920Seric 	e->e_lockfp = NULL;
147351920Seric 
147458728Seric 	/* don't create a queue id if we don't already have one */
147558728Seric 	if (e->e_id == NULL)
147658728Seric 		return;
147758728Seric 
147817468Seric 	/* remove the transcript */
147917468Seric # ifdef LOG
148058020Seric 	if (LogLevel > 87)
148117468Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
148256795Seric # endif /* LOG */
148358680Seric 	if (!tTd(51, 104))
148417468Seric 		xunlink(queuename(e, 'x'));
148517468Seric 
148617468Seric }
148740973Sbostic /*
148854974Seric **  SETCTLUSER -- create a controlling address
148940973Sbostic **
149054974Seric **	Create a fake "address" given only a local login name; this is
149154974Seric **	used as a "controlling user" for future recipient addresses.
149240973Sbostic **
149340973Sbostic **	Parameters:
149454974Seric **		user -- the user name of the controlling user.
149540973Sbostic **
149640973Sbostic **	Returns:
149754974Seric **		An address descriptor for the controlling user.
149840973Sbostic **
149940973Sbostic **	Side Effects:
150040973Sbostic **		none.
150140973Sbostic */
150240973Sbostic 
150354974Seric ADDRESS *
150454974Seric setctluser(user)
150554974Seric 	char *user;
150640973Sbostic {
150754974Seric 	register ADDRESS *a;
150840973Sbostic 	struct passwd *pw;
150959113Seric 	char *p;
151040973Sbostic 
151140973Sbostic 	/*
151254974Seric 	**  See if this clears our concept of controlling user.
151340973Sbostic 	*/
151440973Sbostic 
151563850Seric 	if (user == NULL || *user == '\0')
151663850Seric 		return NULL;
151740973Sbostic 
151840973Sbostic 	/*
151954974Seric 	**  Set up addr fields for controlling user.
152040973Sbostic 	*/
152140973Sbostic 
152254974Seric 	a = (ADDRESS *) xalloc(sizeof *a);
152354974Seric 	bzero((char *) a, sizeof *a);
152459113Seric 
152559113Seric 	p = strchr(user, ':');
152659113Seric 	if (p != NULL)
152759113Seric 		*p++ = '\0';
152859270Seric 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
152940973Sbostic 	{
153065822Seric 		if (strcmp(pw->pw_dir, "/") == 0)
153165822Seric 			a->q_home = "";
153265822Seric 		else
153365822Seric 			a->q_home = newstr(pw->pw_dir);
153440973Sbostic 		a->q_uid = pw->pw_uid;
153540973Sbostic 		a->q_gid = pw->pw_gid;
153657642Seric 		a->q_user = newstr(user);
153759270Seric 		a->q_flags |= QGOODUID;
153840973Sbostic 	}
153940973Sbostic 	else
154040973Sbostic 	{
154157642Seric 		a->q_user = newstr(DefUser);
154240973Sbostic 	}
154340973Sbostic 
154459270Seric 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
154556328Seric 	a->q_mailer = LocalMailer;
154659113Seric 	if (p == NULL)
154759113Seric 		a->q_paddr = a->q_user;
154859113Seric 	else
154959113Seric 		a->q_paddr = newstr(p);
155054974Seric 	return a;
155140973Sbostic }
1552