xref: /csrg-svn/usr.sbin/sendmail/src/queue.c (revision 10090)
1 # include "sendmail.h"
2 # include <sys/stat.h>
3 # include <dir.h>
4 # include <signal.h>
5 # include <errno.h>
6 
7 # ifndef QUEUE
8 SCCSID(@(#)queue.c	3.62		01/03/83	(no queueing));
9 # else QUEUE
10 
11 SCCSID(@(#)queue.c	3.62		01/03/83);
12 
13 /*
14 **  Work queue.
15 */
16 
17 struct work
18 {
19 	char		*w_name;	/* name of control file */
20 	long		w_pri;		/* priority of message, see below */
21 	struct work	*w_next;	/* next in queue */
22 };
23 
24 typedef struct work	WORK;
25 
26 WORK	*WorkQ;			/* queue of things to be done */
27 /*
28 **  QUEUEUP -- queue a message up for future transmission.
29 **
30 **	Parameters:
31 **		e -- the envelope to queue up.
32 **		queueall -- if TRUE, queue all addresses, rather than
33 **			just those with the QQUEUEUP flag set.
34 **		announce -- if TRUE, tell when you are queueing up.
35 **
36 **	Returns:
37 **		none.
38 **
39 **	Side Effects:
40 **		The current request are saved in a control file.
41 */
42 
43 queueup(e, queueall, announce)
44 	register ENVELOPE *e;
45 	bool queueall;
46 	bool announce;
47 {
48 	char *tf;
49 	char *qf;
50 	char buf[MAXLINE];
51 	register FILE *tfp;
52 	register HDR *h;
53 	register ADDRESS *q;
54 
55 	/*
56 	**  Create control file.
57 	*/
58 
59 	tf = newstr(queuename(e, 't'));
60 	tfp = fopen(tf, "w");
61 	if (tfp == NULL)
62 	{
63 		syserr("queueup: cannot create temp file %s", tf);
64 		return;
65 	}
66 	(void) chmod(tf, FileMode);
67 
68 # ifdef DEBUG
69 	if (tTd(40, 1))
70 		printf("queueing in %s\n", tf);
71 # endif DEBUG
72 
73 	/*
74 	**  If there is no data file yet, create one.
75 	*/
76 
77 	if (e->e_df == NULL)
78 	{
79 		register FILE *dfp;
80 		extern putbody();
81 
82 		e->e_df = newstr(queuename(e, 'd'));
83 		dfp = fopen(e->e_df, "w");
84 		if (dfp == NULL)
85 		{
86 			syserr("queueup: cannot create %s", e->e_df);
87 			(void) fclose(tfp);
88 			return;
89 		}
90 		(void) chmod(e->e_df, FileMode);
91 		(*e->e_putbody)(dfp, ProgMailer, FALSE, e, FALSE);
92 		(void) fclose(dfp);
93 		e->e_putbody = putbody;
94 	}
95 
96 	/*
97 	**  Output future work requests.
98 	**	Priority should be first, since it is read by orderq.
99 	*/
100 
101 	/* output message priority */
102 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
103 
104 	/* output creation time */
105 	fprintf(tfp, "T%ld\n", e->e_ctime);
106 
107 	/* output name of data file */
108 	fprintf(tfp, "D%s\n", e->e_df);
109 
110 	/* output name of sender */
111 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
112 
113 	/* output list of recipient addresses */
114 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
115 	{
116 		if (queueall ? !bitset(QDONTSEND, q->q_flags) :
117 			       bitset(QQUEUEUP, q->q_flags))
118 		{
119 			fprintf(tfp, "R%s\n", q->q_paddr);
120 			if (announce)
121 			{
122 				e->e_to = q->q_paddr;
123 				message(Arpa_Info, "queued");
124 				if (LogLevel > 4)
125 					logdelivery("queued");
126 				e->e_to = NULL;
127 			}
128 #ifdef DEBUG
129 			if (tTd(40, 1))
130 			{
131 				printf("queueing ");
132 				printaddr(q, FALSE);
133 			}
134 #endif DEBUG
135 		}
136 	}
137 
138 	/*
139 	**  Output headers for this message.
140 	**	Expand macros completely here.  Queue run will deal with
141 	**	everything as absolute headers.
142 	**		All headers that must be relative to the recipient
143 	**		can be cracked later.
144 	*/
145 
146 	define('g', "$f", e);
147 	for (h = e->e_header; h != NULL; h = h->h_link)
148 	{
149 		if (h->h_value == NULL || h->h_value[0] == '\0')
150 			continue;
151 		fprintf(tfp, "H");
152 		if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags))
153 			mfdecode(h->h_mflags, tfp);
154 		if (bitset(H_DEFAULT, h->h_flags))
155 		{
156 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
157 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
158 		}
159 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
160 		{
161 			commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags),
162 				 (MAILER *) NULL, FALSE);
163 		}
164 		else
165 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
166 	}
167 
168 	/*
169 	**  Clean up.
170 	*/
171 
172 	(void) fclose(tfp);
173 	qf = queuename(e, 'q');
174 	holdsigs();
175 	(void) unlink(qf);
176 	if (link(tf, qf) < 0)
177 		syserr("cannot link(%s, %s), df=%s", tf, qf, e->e_df);
178 	else
179 		(void) unlink(tf);
180 	rlsesigs();
181 
182 # ifdef LOG
183 	/* save log info */
184 	if (LogLevel > 15)
185 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
186 # endif LOG
187 }
188 /*
189 **  RUNQUEUE -- run the jobs in the queue.
190 **
191 **	Gets the stuff out of the queue in some presumably logical
192 **	order and processes them.
193 **
194 **	Parameters:
195 **		none.
196 **
197 **	Returns:
198 **		none.
199 **
200 **	Side Effects:
201 **		runs things in the mail queue.
202 */
203 
204 runqueue(forkflag)
205 	bool forkflag;
206 {
207 	/*
208 	**  See if we want to go off and do other useful work.
209 	*/
210 
211 	if (forkflag)
212 	{
213 		int pid;
214 
215 		pid = dofork();
216 		if (pid != 0)
217 		{
218 			/* parent -- pick up intermediate zombie */
219 			(void) waitfor(pid);
220 			if (QueueIntvl != 0)
221 				(void) setevent(QueueIntvl, runqueue, TRUE);
222 			return;
223 		}
224 		/* child -- double fork */
225 		if (fork() != 0)
226 			exit(EX_OK);
227 	}
228 # ifdef LOG
229 	if (LogLevel > 11)
230 		syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid());
231 # endif LOG
232 
233 	/*
234 	**  Start making passes through the queue.
235 	**	First, read and sort the entire queue.
236 	**	Then, process the work in that order.
237 	**		But if you take too long, start over.
238 	*/
239 
240 	/* order the existing work requests */
241 	(void) orderq();
242 
243 	/* process them once at a time */
244 	while (WorkQ != NULL)
245 	{
246 		WORK *w = WorkQ;
247 
248 		WorkQ = WorkQ->w_next;
249 		dowork(w);
250 		free(w->w_name);
251 		free((char *) w);
252 	}
253 	finis();
254 }
255 /*
256 **  ORDERQ -- order the work queue.
257 **
258 **	Parameters:
259 **		none.
260 **
261 **	Returns:
262 **		none.
263 **
264 **	Side Effects:
265 **		Sets WorkQ to the queue of available work, in order.
266 */
267 
268 # define WLSIZE		120	/* max size of worklist per sort */
269 
270 orderq()
271 {
272 	register struct direct *d;
273 	register WORK *w;
274 	register WORK **wp;		/* parent of w */
275 	DIR *f;
276 	register int i;
277 	WORK wlist[WLSIZE];
278 	int wn = -1;
279 	extern workcmpf();
280 
281 	/* clear out old WorkQ */
282 	for (w = WorkQ; w != NULL; )
283 	{
284 		register WORK *nw = w->w_next;
285 
286 		WorkQ = nw;
287 		free(w->w_name);
288 		free((char *) w);
289 		w = nw;
290 	}
291 
292 	/* open the queue directory */
293 	f = opendir(".");
294 	if (f == NULL)
295 	{
296 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
297 		return (0);
298 	}
299 
300 	/*
301 	**  Read the work directory.
302 	*/
303 
304 	while ((d = readdir(f)) != NULL)
305 	{
306 		FILE *cf;
307 		char lbuf[MAXNAME];
308 
309 		/* is this an interesting entry? */
310 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
311 			continue;
312 
313 		/* yes -- open control file (if not too many files) */
314 		if (++wn >= WLSIZE)
315 			continue;
316 		cf = fopen(d->d_name, "r");
317 		if (cf == NULL)
318 		{
319 			/* this may be some random person sending hir msgs */
320 			/* syserr("orderq: cannot open %s", cbuf); */
321 #ifdef DEBUG
322 			if (tTd(41, 2))
323 				printf("orderq: cannot open %s (%d)\n",
324 					d->d_name, errno);
325 #endif DEBUG
326 			errno = 0;
327 			wn--;
328 			continue;
329 		}
330 		wlist[wn].w_name = newstr(d->d_name);
331 
332 		/* extract useful information */
333 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
334 		{
335 			if (lbuf[0] == 'P')
336 			{
337 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
338 				break;
339 			}
340 		}
341 		(void) fclose(cf);
342 	}
343 	(void) closedir(f);
344 	wn++;
345 
346 	/*
347 	**  Sort the work directory.
348 	*/
349 
350 	qsort(wlist, wn, sizeof *wlist, workcmpf);
351 
352 	/*
353 	**  Convert the work list into canonical form.
354 	**	Should be turning it into a list of envelopes here perhaps.
355 	*/
356 
357 	wp = &WorkQ;
358 	for (i = 0; i < wn; i++)
359 	{
360 		w = (WORK *) xalloc(sizeof *w);
361 		w->w_name = wlist[i].w_name;
362 		w->w_pri = wlist[i].w_pri;
363 		w->w_next = NULL;
364 		*wp = w;
365 		wp = &w->w_next;
366 	}
367 
368 # ifdef DEBUG
369 	if (tTd(40, 1))
370 	{
371 		for (w = WorkQ; w != NULL; w = w->w_next)
372 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
373 	}
374 # endif DEBUG
375 
376 	return (wn);
377 }
378 /*
379 **  WORKCMPF -- compare function for ordering work.
380 **
381 **	Parameters:
382 **		a -- the first argument.
383 **		b -- the second argument.
384 **
385 **	Returns:
386 **		-1 if a < b
387 **		0 if a == b
388 **		1 if a > b
389 **
390 **	Side Effects:
391 **		none.
392 */
393 
394 # define PRIFACT	1800		/* bytes each priority point is worth */
395 
396 workcmpf(a, b)
397 	register WORK *a;
398 	register WORK *b;
399 {
400 	if (a->w_pri == b->w_pri)
401 		return (0);
402 	else if (a->w_pri > b->w_pri)
403 		return (1);
404 	else
405 		return (-1);
406 }
407 /*
408 **  DOWORK -- do a work request.
409 **
410 **	Parameters:
411 **		w -- the work request to be satisfied.
412 **
413 **	Returns:
414 **		none.
415 **
416 **	Side Effects:
417 **		The work request is satisfied if possible.
418 */
419 
420 dowork(w)
421 	register WORK *w;
422 {
423 	register int i;
424 
425 # ifdef DEBUG
426 	if (tTd(40, 1))
427 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
428 # endif DEBUG
429 
430 	/*
431 	**  Fork for work.
432 	*/
433 
434 	i = fork();
435 	if (i < 0)
436 	{
437 		syserr("dowork: cannot fork");
438 		return;
439 	}
440 
441 	if (i == 0)
442 	{
443 		/*
444 		**  CHILD
445 		**	Lock the control file to avoid duplicate deliveries.
446 		**		Then run the file as though we had just read it.
447 		**	We save an idea of the temporary name so we
448 		**		can recover on interrupt.
449 		*/
450 
451 		/* set basic modes, etc. */
452 		(void) alarm(0);
453 		CurEnv->e_flags &= ~EF_FATALERRS;
454 		QueueRun = TRUE;
455 		SendMode = SM_DELIVER;
456 		ErrorMode = EM_MAIL;
457 		CurEnv->e_id = &w->w_name[2];
458 # ifdef LOG
459 		if (LogLevel > 11)
460 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id,
461 			       getpid());
462 # endif LOG
463 
464 		/* don't use the headers from sendmail.cf... */
465 		CurEnv->e_header = NULL;
466 		(void) chompheader("from: $q", TRUE);
467 
468 		/* create the link to the control file during processing */
469 		if (link(w->w_name, queuename(CurEnv, 'l')) < 0)
470 		{
471 			/* being processed by another queuer */
472 # ifdef LOG
473 			if (LogLevel > 4)
474 				syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id);
475 # endif LOG
476 			exit(EX_OK);
477 		}
478 
479 		/* do basic system initialization */
480 		initsys();
481 
482 		/* read the queue control file */
483 		readqf(CurEnv, TRUE);
484 		CurEnv->e_flags |= EF_INQUEUE;
485 		eatheader(CurEnv);
486 
487 		/* do the delivery */
488 		if (!bitset(EF_FATALERRS, CurEnv->e_flags))
489 			sendall(CurEnv, SM_DELIVER);
490 
491 		/* if still not sent, perhaps we should time out.... */
492 # ifdef DEBUG
493 		if (tTd(40, 3))
494 			printf("curtime=%ld, TimeOut=%ld\n", curtime(),
495 					     CurEnv->e_ctime + TimeOut);
496 # endif DEBUG
497 		if (curtime() > CurEnv->e_ctime + TimeOut)
498 			CurEnv->e_flags |= EF_TIMEOUT;
499 
500 		/* finish up and exit */
501 		finis();
502 	}
503 
504 	/*
505 	**  Parent -- pick up results.
506 	*/
507 
508 	errno = 0;
509 	(void) waitfor(i);
510 }
511 /*
512 **  READQF -- read queue file and set up environment.
513 **
514 **	Parameters:
515 **		e -- the envelope of the job to run.
516 **		full -- if set, read in all information.  Otherwise just
517 **			read in info needed for a queue print.
518 **
519 **	Returns:
520 **		none.
521 **
522 **	Side Effects:
523 **		cf is read and created as the current job, as though
524 **		we had been invoked by argument.
525 */
526 
527 readqf(e, full)
528 	register ENVELOPE *e;
529 	bool full;
530 {
531 	register FILE *f;
532 	char buf[MAXFIELD];
533 	extern char *fgetfolded();
534 	register char *p;
535 
536 	/*
537 	**  Open the file created by queueup.
538 	*/
539 
540 	p = queuename(e, 'q');
541 	f = fopen(p, "r");
542 	if (f == NULL)
543 	{
544 		syserr("readqf: no control file %s", p);
545 		return;
546 	}
547 	FileName = p;
548 	LineNumber = 0;
549 
550 	/*
551 	**  Read and process the file.
552 	*/
553 
554 	if (Verbose && full)
555 		printf("\nRunning %s\n", e->e_id);
556 	while (fgetfolded(buf, sizeof buf, f) != NULL)
557 	{
558 		switch (buf[0])
559 		{
560 		  case 'R':		/* specify recipient */
561 			sendtolist(&buf[1], (ADDRESS *) NULL, &e->e_sendqueue);
562 			break;
563 
564 		  case 'H':		/* header */
565 			if (full)
566 				(void) chompheader(&buf[1], FALSE);
567 			break;
568 
569 		  case 'S':		/* sender */
570 			setsender(newstr(&buf[1]));
571 			break;
572 
573 		  case 'D':		/* data file name */
574 			if (!full)
575 				break;
576 			e->e_df = newstr(&buf[1]);
577 			e->e_dfp = fopen(e->e_df, "r");
578 			if (e->e_dfp == NULL)
579 				syserr("readqf: cannot open %s", e->e_df);
580 			break;
581 
582 		  case 'T':		/* init time */
583 			(void) sscanf(&buf[1], "%ld", &e->e_ctime);
584 			break;
585 
586 		  case 'P':		/* message priority */
587 			(void) sscanf(&buf[1], "%ld", &e->e_msgpriority);
588 
589 			/* make sure that big things get sent eventually */
590 			e->e_msgpriority -= WKTIMEFACT;
591 			break;
592 
593 		  case 'M':		/* define macro */
594 			if (full)
595 				define(buf[1], newstr(&buf[2]), e);
596 			break;
597 
598 		  default:
599 			syserr("readqf(%s): bad line \"%s\"", e->e_id, buf);
600 			break;
601 		}
602 	}
603 
604 	FileName = NULL;
605 }
606 /*
607 **  TIMEOUT -- process timeout on queue file.
608 **
609 **	Parameters:
610 **		e -- the envelope that timed out.
611 **
612 **	Returns:
613 **		none.
614 **
615 **	Side Effects:
616 **		Returns a message to the sender saying that this
617 **		message has timed out.
618 */
619 
620 timeout(e)
621 	register ENVELOPE *e;
622 {
623 	char buf[MAXLINE];
624 	extern char *pintvl();
625 
626 # ifdef DEBUG
627 	if (tTd(40, 3))
628 		printf("timeout(%s)\n", e->e_id);
629 # endif DEBUG
630 	e->e_to = NULL;
631 	message(Arpa_Info, "Message has timed out");
632 
633 	/* return message to sender */
634 	(void) sprintf(buf, "Cannot send mail for %s", pintvl(TimeOut, FALSE));
635 	(void) returntosender(buf, &e->e_from, TRUE);
636 
637 	/* arrange to remove files from queue */
638 	e->e_flags |= EF_CLRQUEUE;
639 }
640 /*
641 **  PRINTQUEUE -- print out a representation of the mail queue
642 **
643 **	Parameters:
644 **		none.
645 **
646 **	Returns:
647 **		none.
648 **
649 **	Side Effects:
650 **		Prints a listing of the mail queue on the standard output.
651 */
652 
653 printqueue()
654 {
655 	register WORK *w;
656 	FILE *f;
657 	int nrequests;
658 	char buf[MAXLINE];
659 
660 	/*
661 	**  Read and order the queue.
662 	*/
663 
664 	nrequests = orderq();
665 
666 	/*
667 	**  Print the work list that we have read.
668 	*/
669 
670 	/* first see if there is anything */
671 	if (nrequests <= 0)
672 	{
673 		printf("Mail queue is empty\n");
674 		return;
675 	}
676 
677 	printf("\t\tMail Queue (%d requests", nrequests);
678 	if (nrequests > WLSIZE)
679 		printf(", only %d printed", WLSIZE);
680 	printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
681 	for (w = WorkQ; w != NULL; w = w->w_next)
682 	{
683 		struct stat st;
684 		char lf[20];
685 		auto time_t submittime = 0;
686 		long dfsize = -1;
687 
688 		printf("%7s", w->w_name + 2);
689 		strcpy(lf, w->w_name);
690 		lf[0] = 'l';
691 		if (stat(lf, &st) >= 0)
692 			printf("*");
693 		else
694 			printf(" ");
695 		errno = 0;
696 		f = fopen(w->w_name, "r");
697 		if (f == NULL)
698 		{
699 			printf(" (finished)\n");
700 			errno = 0;
701 			continue;
702 		}
703 		while (fgets(buf, sizeof buf, f) != NULL)
704 		{
705 			fixcrlf(buf, TRUE);
706 			switch (buf[0])
707 			{
708 			  case 'S':	/* sender name */
709 				printf("%8d %.16s %.40s", dfsize,
710 					ctime(&submittime), &buf[1]);
711 				break;
712 
713 			  case 'R':	/* recipient name */
714 				printf("\n\t\t\t\t  %.40s", &buf[1]);
715 				break;
716 
717 			  case 'T':	/* creation time */
718 				sscanf(&buf[1], "%ld", &submittime);
719 				break;
720 
721 			  case 'D':	/* data file name */
722 				if (stat(&buf[1], &st) >= 0)
723 					dfsize = st.st_size;
724 				break;
725 			}
726 		}
727 		if (submittime == (time_t) 0)
728 			printf(" (no control file)");
729 		printf("\n");
730 		fclose(f);
731 	}
732 }
733 
734 # endif QUEUE
735