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