xref: /original-bsd/usr.sbin/sendmail/src/queue.c (revision 4e1ffb20)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 # include "sendmail.h"
10 
11 #ifndef lint
12 #ifdef QUEUE
13 static char sccsid[] = "@(#)queue.c	8.43 (Berkeley) 07/23/94 (with queueing)";
14 #else
15 static char sccsid[] = "@(#)queue.c	8.43 (Berkeley) 07/23/94 (without queueing)";
16 #endif
17 #endif /* not lint */
18 
19 # include <errno.h>
20 # include <pwd.h>
21 # include <dirent.h>
22 
23 # ifdef QUEUE
24 
25 /*
26 **  Work queue.
27 */
28 
29 struct work
30 {
31 	char		*w_name;	/* name of control file */
32 	long		w_pri;		/* priority of message, see below */
33 	time_t		w_ctime;	/* creation time of message */
34 	struct work	*w_next;	/* next in queue */
35 };
36 
37 typedef struct work	WORK;
38 
39 WORK	*WorkQ;			/* queue of things to be done */
40 /*
41 **  QUEUEUP -- queue a message up for future transmission.
42 **
43 **	Parameters:
44 **		e -- the envelope to queue up.
45 **		queueall -- if TRUE, queue all addresses, rather than
46 **			just those with the QQUEUEUP flag set.
47 **		announce -- if TRUE, tell when you are queueing up.
48 **
49 **	Returns:
50 **		none.
51 **
52 **	Side Effects:
53 **		The current request are saved in a control file.
54 **		The queue file is left locked.
55 */
56 
57 queueup(e, queueall, announce)
58 	register ENVELOPE *e;
59 	bool queueall;
60 	bool announce;
61 {
62 	char *qf;
63 	register FILE *tfp;
64 	register HDR *h;
65 	register ADDRESS *q;
66 	int fd;
67 	int i;
68 	bool newid;
69 	register char *p;
70 	MAILER nullmailer;
71 	MCI mcibuf;
72 	char buf[MAXLINE], tf[MAXLINE];
73 
74 	/*
75 	**  Create control file.
76 	*/
77 
78 	newid = (e->e_id == NULL) || !bitset(EF_INQUEUE, e->e_flags);
79 
80 	/* if newid, queuename will create a locked qf file in e->lockfp */
81 	strcpy(tf, queuename(e, 't'));
82 	tfp = e->e_lockfp;
83 	if (tfp == NULL)
84 		newid = FALSE;
85 
86 	/* if newid, just write the qf file directly (instead of tf file) */
87 	if (!newid)
88 	{
89 		/* get a locked tf file */
90 		for (i = 0; i < 128; i++)
91 		{
92 			fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode);
93 			if (fd < 0)
94 			{
95 				if (errno != EEXIST)
96 					break;
97 #ifdef LOG
98 				if (LogLevel > 0 && (i % 32) == 0)
99 					syslog(LOG_ALERT, "queueup: cannot create %s, uid=%d: %s",
100 						tf, geteuid(), errstring(errno));
101 #endif
102 			}
103 			else
104 			{
105 				if (lockfile(fd, tf, NULL, LOCK_EX|LOCK_NB))
106 					break;
107 #ifdef LOG
108 				else if (LogLevel > 0 && (i % 32) == 0)
109 					syslog(LOG_ALERT, "queueup: cannot lock %s: %s",
110 						tf, errstring(errno));
111 #endif
112 				close(fd);
113 			}
114 
115 			if ((i % 32) == 31)
116 			{
117 				/* save the old temp file away */
118 				(void) rename(tf, queuename(e, 'T'));
119 			}
120 			else
121 				sleep(i % 32);
122 		}
123 		if (fd < 0 || (tfp = fdopen(fd, "w")) == NULL)
124 		{
125 			printopenfds(TRUE);
126 			syserr("!queueup: cannot create queue temp file %s, uid=%d",
127 				tf, geteuid());
128 		}
129 	}
130 
131 	if (tTd(40, 1))
132 		printf("\n>>>>> queueing %s%s >>>>>\n", e->e_id,
133 			newid ? " (new id)" : "");
134 	if (tTd(40, 9))
135 	{
136 		printf("  tfp=");
137 		dumpfd(fileno(tfp), TRUE, FALSE);
138 		printf("  lockfp=");
139 		if (e->e_lockfp == NULL)
140 			printf("NULL\n");
141 		else
142 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
143 	}
144 
145 	/*
146 	**  If there is no data file yet, create one.
147 	*/
148 
149 	if (e->e_df == NULL)
150 	{
151 		register FILE *dfp;
152 		extern putbody();
153 
154 		e->e_df = queuename(e, 'd');
155 		e->e_df = newstr(e->e_df);
156 		fd = open(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode);
157 		if (fd < 0 || (dfp = fdopen(fd, "w")) == NULL)
158 			syserr("!queueup: cannot create data temp file %s, uid=%d",
159 				e->e_df, geteuid());
160 		bzero(&mcibuf, sizeof mcibuf);
161 		mcibuf.mci_out = dfp;
162 		mcibuf.mci_mailer = FileMailer;
163 		(*e->e_putbody)(&mcibuf, e, NULL);
164 		(void) xfclose(dfp, "queueup dfp", e->e_id);
165 		e->e_putbody = putbody;
166 	}
167 
168 	/*
169 	**  Output future work requests.
170 	**	Priority and creation time should be first, since
171 	**	they are required by orderq.
172 	*/
173 
174 	/* output message priority */
175 	fprintf(tfp, "P%ld\n", e->e_msgpriority);
176 
177 	/* output creation time */
178 	fprintf(tfp, "T%ld\n", e->e_ctime);
179 
180 	/* output type and name of data file */
181 	if (e->e_bodytype != NULL)
182 		fprintf(tfp, "B%s\n", e->e_bodytype);
183 	fprintf(tfp, "D%s\n", e->e_df);
184 
185 	/* message from envelope, if it exists */
186 	if (e->e_message != NULL)
187 		fprintf(tfp, "M%s\n", e->e_message);
188 
189 	/* send various flag bits through */
190 	p = buf;
191 	if (bitset(EF_WARNING, e->e_flags))
192 		*p++ = 'w';
193 	if (bitset(EF_RESPONSE, e->e_flags))
194 		*p++ = 'r';
195 	if (bitset(EF_HAS8BIT, e->e_flags))
196 		*p++ = '8';
197 	*p++ = '\0';
198 	if (buf[0] != '\0')
199 		fprintf(tfp, "F%s\n", buf);
200 
201 	/* $r and $s and $_ macro values */
202 	if ((p = macvalue('r', e)) != NULL)
203 		fprintf(tfp, "$r%s\n", p);
204 	if ((p = macvalue('s', e)) != NULL)
205 		fprintf(tfp, "$s%s\n", p);
206 	if ((p = macvalue('_', e)) != NULL)
207 		fprintf(tfp, "$_%s\n", p);
208 
209 	/* output name of sender */
210 	fprintf(tfp, "S%s\n", e->e_from.q_paddr);
211 
212 	/* output list of error recipients */
213 	printctladdr(NULL, NULL);
214 	for (q = e->e_errorqueue; q != NULL; q = q->q_next)
215 	{
216 		if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
217 		{
218 			printctladdr(q, tfp);
219 			fprintf(tfp, "E%s\n", q->q_paddr);
220 		}
221 	}
222 
223 	/* output list of recipient addresses */
224 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
225 	{
226 		if (bitset(QQUEUEUP, q->q_flags) ||
227 		    (queueall && !bitset(QDONTSEND|QBADADDR|QSENT, q->q_flags)))
228 		{
229 			printctladdr(q, tfp);
230 			fprintf(tfp, "R%s\n", q->q_paddr);
231 			if (announce)
232 			{
233 				e->e_to = q->q_paddr;
234 				message("queued");
235 				if (LogLevel > 8)
236 					logdelivery(NULL, NULL, "queued", NULL, e);
237 				e->e_to = NULL;
238 			}
239 			if (tTd(40, 1))
240 			{
241 				printf("queueing ");
242 				printaddr(q, FALSE);
243 			}
244 		}
245 	}
246 
247 	/*
248 	**  Output headers for this message.
249 	**	Expand macros completely here.  Queue run will deal with
250 	**	everything as absolute headers.
251 	**		All headers that must be relative to the recipient
252 	**		can be cracked later.
253 	**	We set up a "null mailer" -- i.e., a mailer that will have
254 	**	no effect on the addresses as they are output.
255 	*/
256 
257 	bzero((char *) &nullmailer, sizeof nullmailer);
258 	nullmailer.m_re_rwset = nullmailer.m_rh_rwset =
259 			nullmailer.m_se_rwset = nullmailer.m_sh_rwset = -1;
260 	nullmailer.m_eol = "\n";
261 	bzero(&mcibuf, sizeof mcibuf);
262 	mcibuf.mci_mailer = &nullmailer;
263 	mcibuf.mci_out = tfp;
264 
265 	define('g', "\201f", e);
266 	for (h = e->e_header; h != NULL; h = h->h_link)
267 	{
268 		extern bool bitzerop();
269 
270 		/* don't output null headers */
271 		if (h->h_value == NULL || h->h_value[0] == '\0')
272 			continue;
273 
274 		/* don't output resent headers on non-resent messages */
275 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
276 			continue;
277 
278 		/* expand macros; if null, don't output header at all */
279 		if (bitset(H_DEFAULT, h->h_flags))
280 		{
281 			(void) expand(h->h_value, buf, &buf[sizeof buf], e);
282 			if (buf[0] == '\0')
283 				continue;
284 		}
285 
286 		/* output this header */
287 		fprintf(tfp, "H");
288 
289 		/* if conditional, output the set of conditions */
290 		if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags))
291 		{
292 			int j;
293 
294 			(void) putc('?', tfp);
295 			for (j = '\0'; j <= '\177'; j++)
296 				if (bitnset(j, h->h_mflags))
297 					(void) putc(j, tfp);
298 			(void) putc('?', tfp);
299 		}
300 
301 		/* output the header: expand macros, convert addresses */
302 		if (bitset(H_DEFAULT, h->h_flags))
303 		{
304 			fprintf(tfp, "%s: %s\n", h->h_field, buf);
305 		}
306 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
307 		{
308 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
309 			FILE *savetrace = TrafficLogFile;
310 
311 			TrafficLogFile = NULL;
312 
313 			if (bitset(H_FROM, h->h_flags))
314 				oldstyle = FALSE;
315 
316 			commaize(h, h->h_value, oldstyle, &mcibuf, e);
317 
318 			TrafficLogFile = savetrace;
319 		}
320 		else
321 			fprintf(tfp, "%s: %s\n", h->h_field, h->h_value);
322 	}
323 
324 	/*
325 	**  Clean up.
326 	*/
327 
328 	if (fflush(tfp) < 0 || fsync(fileno(tfp)) < 0 || ferror(tfp))
329 	{
330 		if (newid)
331 			syserr("!552 Error writing control file %s", tf);
332 		else
333 			syserr("!452 Error writing control file %s", tf);
334 	}
335 
336 	if (!newid)
337 	{
338 		/* rename (locked) tf to be (locked) qf */
339 		qf = queuename(e, 'q');
340 		if (rename(tf, qf) < 0)
341 			syserr("cannot rename(%s, %s), df=%s, uid=%d",
342 				tf, qf, e->e_df, geteuid());
343 
344 		/* close and unlock old (locked) qf */
345 		if (e->e_lockfp != NULL)
346 			(void) xfclose(e->e_lockfp, "queueup lockfp", e->e_id);
347 		e->e_lockfp = tfp;
348 	}
349 	else
350 		qf = tf;
351 	errno = 0;
352 	e->e_flags |= EF_INQUEUE;
353 
354 # ifdef LOG
355 	/* save log info */
356 	if (LogLevel > 79)
357 		syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df);
358 # endif /* LOG */
359 
360 	if (tTd(40, 1))
361 		printf("<<<<< done queueing %s <<<<<\n\n", e->e_id);
362 	return;
363 }
364 
365 printctladdr(a, tfp)
366 	register ADDRESS *a;
367 	FILE *tfp;
368 {
369 	char *uname;
370 	register struct passwd *pw;
371 	register ADDRESS *q;
372 	uid_t uid;
373 	static ADDRESS *lastctladdr;
374 	static uid_t lastuid;
375 
376 	/* initialization */
377 	if (a == NULL || a->q_alias == NULL || tfp == NULL)
378 	{
379 		if (lastctladdr != NULL && tfp != NULL)
380 			fprintf(tfp, "C\n");
381 		lastctladdr = NULL;
382 		lastuid = 0;
383 		return;
384 	}
385 
386 	/* find the active uid */
387 	q = getctladdr(a);
388 	if (q == NULL)
389 		uid = 0;
390 	else
391 		uid = q->q_uid;
392 	a = a->q_alias;
393 
394 	/* check to see if this is the same as last time */
395 	if (lastctladdr != NULL && uid == lastuid &&
396 	    strcmp(lastctladdr->q_paddr, a->q_paddr) == 0)
397 		return;
398 	lastuid = uid;
399 	lastctladdr = a;
400 
401 	if (uid == 0 || (pw = getpwuid(uid)) == NULL)
402 		uname = "";
403 	else
404 		uname = pw->pw_name;
405 
406 	fprintf(tfp, "C%s:%s\n", uname, a->q_paddr);
407 }
408 
409 /*
410 **  RUNQUEUE -- run the jobs in the queue.
411 **
412 **	Gets the stuff out of the queue in some presumably logical
413 **	order and processes them.
414 **
415 **	Parameters:
416 **		forkflag -- TRUE if the queue scanning should be done in
417 **			a child process.  We double-fork so it is not our
418 **			child and we don't have to clean up after it.
419 **
420 **	Returns:
421 **		none.
422 **
423 **	Side Effects:
424 **		runs things in the mail queue.
425 */
426 
427 ENVELOPE	QueueEnvelope;		/* the queue run envelope */
428 
429 runqueue(forkflag)
430 	bool forkflag;
431 {
432 	register ENVELOPE *e;
433 	extern ENVELOPE BlankEnvelope;
434 
435 	/*
436 	**  If no work will ever be selected, don't even bother reading
437 	**  the queue.
438 	*/
439 
440 	CurrentLA = getla();	/* get load average */
441 
442 	if (shouldqueue(0L, curtime()))
443 	{
444 		if (Verbose)
445 			printf("Skipping queue run -- load average too high\n");
446 		if (forkflag && QueueIntvl != 0)
447 			(void) setevent(QueueIntvl, runqueue, TRUE);
448 		return;
449 	}
450 
451 	/*
452 	**  See if we want to go off and do other useful work.
453 	*/
454 
455 	if (forkflag)
456 	{
457 		int pid;
458 #ifdef SIGCHLD
459 		extern void reapchild();
460 
461 		(void) setsignal(SIGCHLD, reapchild);
462 #endif
463 
464 		pid = dofork();
465 		if (pid != 0)
466 		{
467 			/* parent -- pick up intermediate zombie */
468 #ifndef SIGCHLD
469 			(void) waitfor(pid);
470 #endif /* SIGCHLD */
471 			if (QueueIntvl != 0)
472 				(void) setevent(QueueIntvl, runqueue, TRUE);
473 			return;
474 		}
475 		/* child -- double fork */
476 #ifndef SIGCHLD
477 		if (fork() != 0)
478 			exit(EX_OK);
479 #else /* SIGCHLD */
480 		(void) setsignal(SIGCHLD, SIG_DFL);
481 #endif /* SIGCHLD */
482 	}
483 
484 	setproctitle("running queue: %s", QueueDir);
485 
486 # ifdef LOG
487 	if (LogLevel > 69)
488 		syslog(LOG_DEBUG, "runqueue %s, pid=%d, forkflag=%d",
489 			QueueDir, getpid(), forkflag);
490 # endif /* LOG */
491 
492 	/*
493 	**  Release any resources used by the daemon code.
494 	*/
495 
496 # ifdef DAEMON
497 	clrdaemon();
498 # endif /* DAEMON */
499 
500 	/* force it to run expensive jobs */
501 	NoConnect = FALSE;
502 
503 	/*
504 	**  Create ourselves an envelope
505 	*/
506 
507 	CurEnv = &QueueEnvelope;
508 	e = newenvelope(&QueueEnvelope, CurEnv);
509 	e->e_flags = BlankEnvelope.e_flags;
510 
511 	/*
512 	**  Make sure the alias database is open.
513 	*/
514 
515 	initmaps(FALSE, e);
516 
517 	/*
518 	**  Start making passes through the queue.
519 	**	First, read and sort the entire queue.
520 	**	Then, process the work in that order.
521 	**		But if you take too long, start over.
522 	*/
523 
524 	/* order the existing work requests */
525 	(void) orderq(FALSE);
526 
527 	/* process them once at a time */
528 	while (WorkQ != NULL)
529 	{
530 		WORK *w = WorkQ;
531 
532 		WorkQ = WorkQ->w_next;
533 
534 		/*
535 		**  Ignore jobs that are too expensive for the moment.
536 		*/
537 
538 		if (shouldqueue(w->w_pri, w->w_ctime))
539 		{
540 			if (Verbose)
541 				printf("\nSkipping %s\n", w->w_name + 2);
542 		}
543 		else
544 		{
545 			pid_t pid;
546 			extern pid_t dowork();
547 
548 			pid = dowork(w->w_name + 2, ForkQueueRuns, FALSE, e);
549 			errno = 0;
550 			if (pid != 0)
551 				(void) waitfor(pid);
552 		}
553 		free(w->w_name);
554 		free((char *) w);
555 	}
556 
557 	/* exit without the usual cleanup */
558 	e->e_id = NULL;
559 	finis();
560 }
561 /*
562 **  ORDERQ -- order the work queue.
563 **
564 **	Parameters:
565 **		doall -- if set, include everything in the queue (even
566 **			the jobs that cannot be run because the load
567 **			average is too high).  Otherwise, exclude those
568 **			jobs.
569 **
570 **	Returns:
571 **		The number of request in the queue (not necessarily
572 **		the number of requests in WorkQ however).
573 **
574 **	Side Effects:
575 **		Sets WorkQ to the queue of available work, in order.
576 */
577 
578 # define NEED_P		001
579 # define NEED_T		002
580 # define NEED_R		004
581 # define NEED_S		010
582 
583 orderq(doall)
584 	bool doall;
585 {
586 	register struct dirent *d;
587 	register WORK *w;
588 	DIR *f;
589 	register int i;
590 	WORK wlist[QUEUESIZE+1];
591 	int wn = -1;
592 	extern workcmpf();
593 
594 	if (tTd(41, 1))
595 	{
596 		printf("orderq:\n");
597 		if (QueueLimitId != NULL)
598 			printf("\tQueueLimitId = %s\n", QueueLimitId);
599 		if (QueueLimitSender != NULL)
600 			printf("\tQueueLimitSender = %s\n", QueueLimitSender);
601 		if (QueueLimitRecipient != NULL)
602 			printf("\tQueueLimitRecipient = %s\n", QueueLimitRecipient);
603 	}
604 
605 	/* clear out old WorkQ */
606 	for (w = WorkQ; w != NULL; )
607 	{
608 		register WORK *nw = w->w_next;
609 
610 		WorkQ = nw;
611 		free(w->w_name);
612 		free((char *) w);
613 		w = nw;
614 	}
615 
616 	/* open the queue directory */
617 	f = opendir(".");
618 	if (f == NULL)
619 	{
620 		syserr("orderq: cannot open \"%s\" as \".\"", QueueDir);
621 		return (0);
622 	}
623 
624 	/*
625 	**  Read the work directory.
626 	*/
627 
628 	while ((d = readdir(f)) != NULL)
629 	{
630 		FILE *cf;
631 		register char *p;
632 		char lbuf[MAXNAME];
633 		extern bool strcontainedin();
634 
635 		/* is this an interesting entry? */
636 		if (d->d_name[0] != 'q' || d->d_name[1] != 'f')
637 			continue;
638 
639 		if (QueueLimitId != NULL &&
640 		    !strcontainedin(QueueLimitId, d->d_name))
641 			continue;
642 
643 		/*
644 		**  Check queue name for plausibility.  This handles
645 		**  both old and new type ids.
646 		*/
647 
648 		p = d->d_name + 2;
649 		if (isupper(p[0]) && isupper(p[2]))
650 			p += 3;
651 		else if (isupper(p[1]))
652 			p += 2;
653 		else
654 			p = d->d_name;
655 		for (i = 0; isdigit(*p); p++)
656 			i++;
657 		if (i < 5 || *p != '\0')
658 		{
659 			if (Verbose)
660 				printf("orderq: bogus qf name %s\n", d->d_name);
661 #ifdef LOG
662 			if (LogLevel > 3)
663 				syslog(LOG_CRIT, "orderq: bogus qf name %s",
664 					d->d_name);
665 #endif
666 			if (strlen(d->d_name) >= MAXNAME)
667 				d->d_name[MAXNAME - 1] = '\0';
668 			strcpy(lbuf, d->d_name);
669 			lbuf[0] = 'Q';
670 			(void) rename(d->d_name, lbuf);
671 			continue;
672 		}
673 
674 		/* yes -- open control file (if not too many files) */
675 		if (++wn >= QUEUESIZE)
676 			continue;
677 
678 		cf = fopen(d->d_name, "r");
679 		if (cf == NULL)
680 		{
681 			/* this may be some random person sending hir msgs */
682 			/* syserr("orderq: cannot open %s", cbuf); */
683 			if (tTd(41, 2))
684 				printf("orderq: cannot open %s (%d)\n",
685 					d->d_name, errno);
686 			errno = 0;
687 			wn--;
688 			continue;
689 		}
690 		w = &wlist[wn];
691 		w->w_name = newstr(d->d_name);
692 
693 		/* make sure jobs in creation don't clog queue */
694 		w->w_pri = 0x7fffffff;
695 		w->w_ctime = 0;
696 
697 		/* extract useful information */
698 		i = NEED_P | NEED_T;
699 		if (QueueLimitSender != NULL)
700 			i |= NEED_S;
701 		if (QueueLimitRecipient != NULL)
702 			i |= NEED_R;
703 		while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL)
704 		{
705 			extern long atol();
706 			extern bool strcontainedin();
707 
708 			switch (lbuf[0])
709 			{
710 			  case 'P':
711 				w->w_pri = atol(&lbuf[1]);
712 				i &= ~NEED_P;
713 				break;
714 
715 			  case 'T':
716 				w->w_ctime = atol(&lbuf[1]);
717 				i &= ~NEED_T;
718 				break;
719 
720 			  case 'R':
721 				if (QueueLimitRecipient != NULL &&
722 				    strcontainedin(QueueLimitRecipient, &lbuf[1]))
723 					i &= ~NEED_R;
724 				break;
725 
726 			  case 'S':
727 				if (QueueLimitSender != NULL &&
728 				    strcontainedin(QueueLimitSender, &lbuf[1]))
729 					i &= ~NEED_S;
730 				break;
731 			}
732 		}
733 		(void) fclose(cf);
734 
735 		if ((!doall && shouldqueue(w->w_pri, w->w_ctime)) ||
736 		    bitset(NEED_R|NEED_S, i))
737 		{
738 			/* don't even bother sorting this job in */
739 			wn--;
740 		}
741 	}
742 	(void) closedir(f);
743 	wn++;
744 
745 	/*
746 	**  Sort the work directory.
747 	*/
748 
749 	qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf);
750 
751 	/*
752 	**  Convert the work list into canonical form.
753 	**	Should be turning it into a list of envelopes here perhaps.
754 	*/
755 
756 	WorkQ = NULL;
757 	for (i = min(wn, QUEUESIZE); --i >= 0; )
758 	{
759 		w = (WORK *) xalloc(sizeof *w);
760 		w->w_name = wlist[i].w_name;
761 		w->w_pri = wlist[i].w_pri;
762 		w->w_ctime = wlist[i].w_ctime;
763 		w->w_next = WorkQ;
764 		WorkQ = w;
765 	}
766 
767 	if (tTd(40, 1))
768 	{
769 		for (w = WorkQ; w != NULL; w = w->w_next)
770 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
771 	}
772 
773 	return (wn);
774 }
775 /*
776 **  WORKCMPF -- compare function for ordering work.
777 **
778 **	Parameters:
779 **		a -- the first argument.
780 **		b -- the second argument.
781 **
782 **	Returns:
783 **		-1 if a < b
784 **		 0 if a == b
785 **		+1 if a > b
786 **
787 **	Side Effects:
788 **		none.
789 */
790 
791 workcmpf(a, b)
792 	register WORK *a;
793 	register WORK *b;
794 {
795 	long pa = a->w_pri;
796 	long pb = b->w_pri;
797 
798 	if (pa == pb)
799 		return (0);
800 	else if (pa > pb)
801 		return (1);
802 	else
803 		return (-1);
804 }
805 /*
806 **  DOWORK -- do a work request.
807 **
808 **	Parameters:
809 **		id -- the ID of the job to run.
810 **		forkflag -- if set, run this in background.
811 **		requeueflag -- if set, reinstantiate the queue quickly.
812 **			This is used when expanding aliases in the queue.
813 **			If forkflag is also set, it doesn't wait for the
814 **			child.
815 **		e - the envelope in which to run it.
816 **
817 **	Returns:
818 **		process id of process that is running the queue job.
819 **
820 **	Side Effects:
821 **		The work request is satisfied if possible.
822 */
823 
824 pid_t
825 dowork(id, forkflag, requeueflag, e)
826 	char *id;
827 	bool forkflag;
828 	bool requeueflag;
829 	register ENVELOPE *e;
830 {
831 	register pid_t pid;
832 	extern bool readqf();
833 
834 	if (tTd(40, 1))
835 		printf("dowork(%s)\n", id);
836 
837 	/*
838 	**  Fork for work.
839 	*/
840 
841 	if (forkflag)
842 	{
843 		pid = fork();
844 		if (pid < 0)
845 		{
846 			syserr("dowork: cannot fork");
847 			return 0;
848 		}
849 		else if (pid > 0)
850 		{
851 			/* parent -- clean out connection cache */
852 			mci_flush(FALSE, NULL);
853 		}
854 	}
855 	else
856 	{
857 		pid = 0;
858 	}
859 
860 	if (pid == 0)
861 	{
862 		/*
863 		**  CHILD
864 		**	Lock the control file to avoid duplicate deliveries.
865 		**		Then run the file as though we had just read it.
866 		**	We save an idea of the temporary name so we
867 		**		can recover on interrupt.
868 		*/
869 
870 		/* set basic modes, etc. */
871 		(void) alarm(0);
872 		clearenvelope(e, FALSE);
873 		e->e_flags |= EF_QUEUERUN|EF_GLOBALERRS;
874 		e->e_errormode = EM_MAIL;
875 		e->e_id = id;
876 		GrabTo = UseErrorsTo = FALSE;
877 		ExitStat = EX_OK;
878 		if (forkflag)
879 		{
880 			disconnect(1, e);
881 			OpMode = MD_DELIVER;
882 		}
883 # ifdef LOG
884 		if (LogLevel > 76)
885 			syslog(LOG_DEBUG, "%s: dowork, pid=%d", e->e_id,
886 			       getpid());
887 # endif /* LOG */
888 
889 		/* don't use the headers from sendmail.cf... */
890 		e->e_header = NULL;
891 
892 		/* read the queue control file -- return if locked */
893 		if (!readqf(e))
894 		{
895 			if (tTd(40, 4))
896 				printf("readqf(%s) failed\n", e->e_id);
897 			if (forkflag)
898 				exit(EX_OK);
899 			else
900 				return 0;
901 		}
902 
903 		e->e_flags |= EF_INQUEUE;
904 		eatheader(e, requeueflag);
905 
906 		if (requeueflag)
907 			queueup(e, TRUE, FALSE);
908 
909 		/* do the delivery */
910 		sendall(e, SM_DELIVER);
911 
912 		/* finish up and exit */
913 		if (forkflag)
914 			finis();
915 		else
916 			dropenvelope(e);
917 	}
918 	e->e_id = NULL;
919 	return pid;
920 }
921 /*
922 **  READQF -- read queue file and set up environment.
923 **
924 **	Parameters:
925 **		e -- the envelope of the job to run.
926 **
927 **	Returns:
928 **		TRUE if it successfully read the queue file.
929 **		FALSE otherwise.
930 **
931 **	Side Effects:
932 **		The queue file is returned locked.
933 */
934 
935 bool
936 readqf(e)
937 	register ENVELOPE *e;
938 {
939 	register FILE *qfp;
940 	ADDRESS *ctladdr;
941 	struct stat st;
942 	char *bp;
943 	char qf[20];
944 	char buf[MAXLINE];
945 	extern long atol();
946 	extern ADDRESS *setctluser();
947 
948 	/*
949 	**  Read and process the file.
950 	*/
951 
952 	strcpy(qf, queuename(e, 'q'));
953 	qfp = fopen(qf, "r+");
954 	if (qfp == NULL)
955 	{
956 		if (tTd(40, 8))
957 			printf("readqf(%s): fopen failure (%s)\n",
958 				qf, errstring(errno));
959 		if (errno != ENOENT)
960 			syserr("readqf: no control file %s", qf);
961 		return FALSE;
962 	}
963 
964 	if (!lockfile(fileno(qfp), qf, NULL, LOCK_EX|LOCK_NB))
965 	{
966 		/* being processed by another queuer */
967 		if (tTd(40, 8))
968 			printf("readqf(%s): locked\n", qf);
969 		if (Verbose)
970 			printf("%s: locked\n", e->e_id);
971 # ifdef LOG
972 		if (LogLevel > 19)
973 			syslog(LOG_DEBUG, "%s: locked", e->e_id);
974 # endif /* LOG */
975 		(void) fclose(qfp);
976 		return FALSE;
977 	}
978 
979 	/*
980 	**  Check the queue file for plausibility to avoid attacks.
981 	*/
982 
983 	if (fstat(fileno(qfp), &st) < 0)
984 	{
985 		/* must have been being processed by someone else */
986 		if (tTd(40, 8))
987 			printf("readqf(%s): fstat failure (%s)\n",
988 				qf, errstring(errno));
989 		fclose(qfp);
990 		return FALSE;
991 	}
992 
993 	if (st.st_uid != geteuid())
994 	{
995 # ifdef LOG
996 		if (LogLevel > 0)
997 		{
998 			syslog(LOG_ALERT, "%s: bogus queue file, uid=%d, mode=%o",
999 				e->e_id, st.st_uid, st.st_mode);
1000 		}
1001 # endif /* LOG */
1002 		if (tTd(40, 8))
1003 			printf("readqf(%s): bogus file\n", qf);
1004 		rename(qf, queuename(e, 'Q'));
1005 		fclose(qfp);
1006 		return FALSE;
1007 	}
1008 
1009 	if (st.st_size == 0)
1010 	{
1011 		/* must be a bogus file -- just remove it */
1012 		(void) unlink(qf);
1013 		fclose(qfp);
1014 		return FALSE;
1015 	}
1016 
1017 	if (st.st_nlink == 0)
1018 	{
1019 		/*
1020 		**  Race condition -- we got a file just as it was being
1021 		**  unlinked.  Just assume it is zero length.
1022 		*/
1023 
1024 		fclose(qfp);
1025 		return FALSE;
1026 	}
1027 
1028 	/* good file -- save this lock */
1029 	e->e_lockfp = qfp;
1030 
1031 	/* do basic system initialization */
1032 	initsys(e);
1033 	define('i', e->e_id, e);
1034 
1035 	LineNumber = 0;
1036 	e->e_flags |= EF_GLOBALERRS;
1037 	OpMode = MD_DELIVER;
1038 	if (Verbose)
1039 		printf("\nRunning %s\n", e->e_id);
1040 	ctladdr = NULL;
1041 	while ((bp = fgetfolded(buf, sizeof buf, qfp)) != NULL)
1042 	{
1043 		register char *p;
1044 		struct stat st;
1045 
1046 		if (tTd(40, 4))
1047 			printf("+++++ %s\n", bp);
1048 		switch (bp[0])
1049 		{
1050 		  case 'C':		/* specify controlling user */
1051 			ctladdr = setctluser(&bp[1]);
1052 			break;
1053 
1054 		  case 'R':		/* specify recipient */
1055 			(void) sendtolist(&bp[1], ctladdr, &e->e_sendqueue, e);
1056 			break;
1057 
1058 		  case 'E':		/* specify error recipient */
1059 			(void) sendtolist(&bp[1], ctladdr, &e->e_errorqueue, e);
1060 			break;
1061 
1062 		  case 'H':		/* header */
1063 			(void) chompheader(&bp[1], FALSE, e);
1064 			break;
1065 
1066 		  case 'M':		/* message */
1067 			/* ignore this; we want a new message next time */
1068 			break;
1069 
1070 		  case 'S':		/* sender */
1071 			setsender(newstr(&bp[1]), e, NULL, TRUE);
1072 			break;
1073 
1074 		  case 'B':		/* body type */
1075 			e->e_bodytype = newstr(&bp[1]);
1076 			break;
1077 
1078 		  case 'D':		/* data file name */
1079 			e->e_df = newstr(&bp[1]);
1080 			e->e_dfp = fopen(e->e_df, "r");
1081 			if (e->e_dfp == NULL)
1082 			{
1083 				syserr("readqf: cannot open %s", e->e_df);
1084 				e->e_msgsize = -1;
1085 			}
1086 			else if (fstat(fileno(e->e_dfp), &st) >= 0)
1087 				e->e_msgsize = st.st_size;
1088 			break;
1089 
1090 		  case 'T':		/* init time */
1091 			e->e_ctime = atol(&bp[1]);
1092 			break;
1093 
1094 		  case 'P':		/* message priority */
1095 			e->e_msgpriority = atol(&bp[1]) + WkTimeFact;
1096 			break;
1097 
1098 		  case 'F':		/* flag bits */
1099 			for (p = &bp[1]; *p != '\0'; p++)
1100 			{
1101 				switch (*p)
1102 				{
1103 				  case 'w':	/* warning sent */
1104 					e->e_flags |= EF_WARNING;
1105 					break;
1106 
1107 				  case 'r':	/* response */
1108 					e->e_flags |= EF_RESPONSE;
1109 					break;
1110 
1111 				  case '8':	/* has 8 bit data */
1112 					e->e_flags |= EF_HAS8BIT;
1113 					break;
1114 				}
1115 			}
1116 			break;
1117 
1118 		  case '$':		/* define macro */
1119 			define(bp[1], newstr(&bp[2]), e);
1120 			break;
1121 
1122 		  case '\0':		/* blank line; ignore */
1123 			break;
1124 
1125 		  default:
1126 			syserr("readqf: %s: line %d: bad line \"%s\"",
1127 				qf, LineNumber, bp);
1128 			fclose(qfp);
1129 			rename(qf, queuename(e, 'Q'));
1130 			return FALSE;
1131 		}
1132 
1133 		if (bp != buf)
1134 			free(bp);
1135 	}
1136 
1137 	/*
1138 	**  If we haven't read any lines, this queue file is empty.
1139 	**  Arrange to remove it without referencing any null pointers.
1140 	*/
1141 
1142 	if (LineNumber == 0)
1143 	{
1144 		errno = 0;
1145 		e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE;
1146 	}
1147 	return TRUE;
1148 }
1149 /*
1150 **  PRINTQUEUE -- print out a representation of the mail queue
1151 **
1152 **	Parameters:
1153 **		none.
1154 **
1155 **	Returns:
1156 **		none.
1157 **
1158 **	Side Effects:
1159 **		Prints a listing of the mail queue on the standard output.
1160 */
1161 
1162 printqueue()
1163 {
1164 	register WORK *w;
1165 	FILE *f;
1166 	int nrequests;
1167 	char buf[MAXLINE];
1168 
1169 	/*
1170 	**  Check for permission to print the queue
1171 	*/
1172 
1173 	if (bitset(PRIV_RESTRICTMAILQ, PrivacyFlags) && RealUid != 0)
1174 	{
1175 		struct stat st;
1176 # ifdef NGROUPS
1177 		int n;
1178 		GIDSET_T gidset[NGROUPS];
1179 # endif
1180 
1181 		if (stat(QueueDir, &st) < 0)
1182 		{
1183 			syserr("Cannot stat %s", QueueDir);
1184 			return;
1185 		}
1186 # ifdef NGROUPS
1187 		n = getgroups(NGROUPS, gidset);
1188 		while (--n >= 0)
1189 		{
1190 			if (gidset[n] == st.st_gid)
1191 				break;
1192 		}
1193 		if (n < 0)
1194 # else
1195 		if (RealGid != st.st_gid)
1196 # endif
1197 		{
1198 			usrerr("510 You are not permitted to see the queue");
1199 			setstat(EX_NOPERM);
1200 			return;
1201 		}
1202 	}
1203 
1204 	/*
1205 	**  Read and order the queue.
1206 	*/
1207 
1208 	nrequests = orderq(TRUE);
1209 
1210 	/*
1211 	**  Print the work list that we have read.
1212 	*/
1213 
1214 	/* first see if there is anything */
1215 	if (nrequests <= 0)
1216 	{
1217 		printf("Mail queue is empty\n");
1218 		return;
1219 	}
1220 
1221 	CurrentLA = getla();	/* get load average */
1222 
1223 	printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s");
1224 	if (nrequests > QUEUESIZE)
1225 		printf(", only %d printed", QUEUESIZE);
1226 	if (Verbose)
1227 		printf(")\n--Q-ID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n");
1228 	else
1229 		printf(")\n--Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n");
1230 	for (w = WorkQ; w != NULL; w = w->w_next)
1231 	{
1232 		struct stat st;
1233 		auto time_t submittime = 0;
1234 		long dfsize = -1;
1235 		int flags = 0;
1236 		char message[MAXLINE];
1237 		char bodytype[MAXNAME];
1238 
1239 		printf("%8s", w->w_name + 2);
1240 		f = fopen(w->w_name, "r");
1241 		if (f == NULL)
1242 		{
1243 			printf(" (job completed)\n");
1244 			errno = 0;
1245 			continue;
1246 		}
1247 		if (!lockfile(fileno(f), w->w_name, NULL, LOCK_SH|LOCK_NB))
1248 			printf("*");
1249 		else if (shouldqueue(w->w_pri, w->w_ctime))
1250 			printf("X");
1251 		else
1252 			printf(" ");
1253 		errno = 0;
1254 
1255 		message[0] = bodytype[0] = '\0';
1256 		while (fgets(buf, sizeof buf, f) != NULL)
1257 		{
1258 			register int i;
1259 			register char *p;
1260 
1261 			fixcrlf(buf, TRUE);
1262 			switch (buf[0])
1263 			{
1264 			  case 'M':	/* error message */
1265 				if ((i = strlen(&buf[1])) >= sizeof message)
1266 					i = sizeof message - 1;
1267 				bcopy(&buf[1], message, i);
1268 				message[i] = '\0';
1269 				break;
1270 
1271 			  case 'B':	/* body type */
1272 				if ((i = strlen(&buf[1])) >= sizeof bodytype)
1273 					i = sizeof bodytype - 1;
1274 				bcopy(&buf[1], bodytype, i);
1275 				bodytype[i] = '\0';
1276 				break;
1277 
1278 			  case 'S':	/* sender name */
1279 				if (Verbose)
1280 					printf("%8ld %10ld%c%.12s %.38s",
1281 					    dfsize,
1282 					    w->w_pri,
1283 					    bitset(EF_WARNING, flags) ? '+' : ' ',
1284 					    ctime(&submittime) + 4,
1285 					    &buf[1]);
1286 				else
1287 					printf("%8ld %.16s %.45s", dfsize,
1288 					    ctime(&submittime), &buf[1]);
1289 				if (message[0] != '\0' || bodytype[0] != '\0')
1290 				{
1291 					printf("\n    %10.10s", bodytype);
1292 					if (message[0] != '\0')
1293 						printf("   (%.60s)", message);
1294 				}
1295 				break;
1296 
1297 			  case 'C':	/* controlling user */
1298 				if (Verbose)
1299 					printf("\n\t\t\t\t      (---%.34s---)",
1300 						&buf[1]);
1301 				break;
1302 
1303 			  case 'R':	/* recipient name */
1304 				if (Verbose)
1305 					printf("\n\t\t\t\t\t  %.38s", &buf[1]);
1306 				else
1307 					printf("\n\t\t\t\t   %.45s", &buf[1]);
1308 				break;
1309 
1310 			  case 'T':	/* creation time */
1311 				submittime = atol(&buf[1]);
1312 				break;
1313 
1314 			  case 'D':	/* data file name */
1315 				if (stat(&buf[1], &st) >= 0)
1316 					dfsize = st.st_size;
1317 				break;
1318 
1319 			  case 'F':	/* flag bits */
1320 				for (p = &buf[1]; *p != '\0'; p++)
1321 				{
1322 					switch (*p)
1323 					{
1324 					  case 'w':
1325 						flags |= EF_WARNING;
1326 						break;
1327 					}
1328 				}
1329 			}
1330 		}
1331 		if (submittime == (time_t) 0)
1332 			printf(" (no control file)");
1333 		printf("\n");
1334 		(void) fclose(f);
1335 	}
1336 }
1337 
1338 # endif /* QUEUE */
1339 /*
1340 **  QUEUENAME -- build a file name in the queue directory for this envelope.
1341 **
1342 **	Assigns an id code if one does not already exist.
1343 **	This code is very careful to avoid trashing existing files
1344 **	under any circumstances.
1345 **
1346 **	Parameters:
1347 **		e -- envelope to build it in/from.
1348 **		type -- the file type, used as the first character
1349 **			of the file name.
1350 **
1351 **	Returns:
1352 **		a pointer to the new file name (in a static buffer).
1353 **
1354 **	Side Effects:
1355 **		If no id code is already assigned, queuename will
1356 **		assign an id code, create a qf file, and leave a
1357 **		locked, open-for-write file pointer in the envelope.
1358 */
1359 
1360 char *
1361 queuename(e, type)
1362 	register ENVELOPE *e;
1363 	int type;
1364 {
1365 	static int pid = -1;
1366 	static char c0;
1367 	static char c1;
1368 	static char c2;
1369 	time_t now;
1370 	struct tm *tm;
1371 	static char buf[MAXNAME];
1372 
1373 	if (e->e_id == NULL)
1374 	{
1375 		char qf[20];
1376 
1377 		/* find a unique id */
1378 		if (pid != getpid())
1379 		{
1380 			/* new process -- start back at "AA" */
1381 			pid = getpid();
1382 			now = curtime();
1383 			tm = localtime(&now);
1384 			c0 = 'A' + tm->tm_hour;
1385 			c1 = 'A';
1386 			c2 = 'A' - 1;
1387 		}
1388 		(void) sprintf(qf, "qf%cAA%05d", c0, pid);
1389 
1390 		while (c1 < '~' || c2 < 'Z')
1391 		{
1392 			int i;
1393 
1394 			if (c2 >= 'Z')
1395 			{
1396 				c1++;
1397 				c2 = 'A' - 1;
1398 			}
1399 			qf[3] = c1;
1400 			qf[4] = ++c2;
1401 			if (tTd(7, 20))
1402 				printf("queuename: trying \"%s\"\n", qf);
1403 
1404 			i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode);
1405 			if (i < 0)
1406 			{
1407 				if (errno == EEXIST)
1408 					continue;
1409 				syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
1410 					qf, QueueDir, geteuid());
1411 				exit(EX_UNAVAILABLE);
1412 			}
1413 			if (lockfile(i, qf, NULL, LOCK_EX|LOCK_NB))
1414 			{
1415 				e->e_lockfp = fdopen(i, "w");
1416 				break;
1417 			}
1418 
1419 			/* a reader got the file; abandon it and try again */
1420 			(void) close(i);
1421 		}
1422 		if (c1 >= '~' && c2 >= 'Z')
1423 		{
1424 			syserr("queuename: Cannot create \"%s\" in \"%s\" (euid=%d)",
1425 				qf, QueueDir, geteuid());
1426 			exit(EX_OSERR);
1427 		}
1428 		e->e_id = newstr(&qf[2]);
1429 		define('i', e->e_id, e);
1430 		if (tTd(7, 1))
1431 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
1432 		if (tTd(7, 9))
1433 		{
1434 			printf("  lockfd=");
1435 			dumpfd(fileno(e->e_lockfp), TRUE, FALSE);
1436 		}
1437 # ifdef LOG
1438 		if (LogLevel > 93)
1439 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
1440 # endif /* LOG */
1441 	}
1442 
1443 	if (type == '\0')
1444 		return (NULL);
1445 	(void) sprintf(buf, "%cf%s", type, e->e_id);
1446 	if (tTd(7, 2))
1447 		printf("queuename: %s\n", buf);
1448 	return (buf);
1449 }
1450 /*
1451 **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1452 **
1453 **	Parameters:
1454 **		e -- the envelope to unlock.
1455 **
1456 **	Returns:
1457 **		none
1458 **
1459 **	Side Effects:
1460 **		unlocks the queue for `e'.
1461 */
1462 
1463 unlockqueue(e)
1464 	ENVELOPE *e;
1465 {
1466 	if (tTd(51, 4))
1467 		printf("unlockqueue(%s)\n", e->e_id);
1468 
1469 	/* if there is a lock file in the envelope, close it */
1470 	if (e->e_lockfp != NULL)
1471 		xfclose(e->e_lockfp, "unlockqueue", e->e_id);
1472 	e->e_lockfp = NULL;
1473 
1474 	/* don't create a queue id if we don't already have one */
1475 	if (e->e_id == NULL)
1476 		return;
1477 
1478 	/* remove the transcript */
1479 # ifdef LOG
1480 	if (LogLevel > 87)
1481 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
1482 # endif /* LOG */
1483 	if (!tTd(51, 104))
1484 		xunlink(queuename(e, 'x'));
1485 
1486 }
1487 /*
1488 **  SETCTLUSER -- create a controlling address
1489 **
1490 **	Create a fake "address" given only a local login name; this is
1491 **	used as a "controlling user" for future recipient addresses.
1492 **
1493 **	Parameters:
1494 **		user -- the user name of the controlling user.
1495 **
1496 **	Returns:
1497 **		An address descriptor for the controlling user.
1498 **
1499 **	Side Effects:
1500 **		none.
1501 */
1502 
1503 ADDRESS *
1504 setctluser(user)
1505 	char *user;
1506 {
1507 	register ADDRESS *a;
1508 	struct passwd *pw;
1509 	char *p;
1510 
1511 	/*
1512 	**  See if this clears our concept of controlling user.
1513 	*/
1514 
1515 	if (user == NULL || *user == '\0')
1516 		return NULL;
1517 
1518 	/*
1519 	**  Set up addr fields for controlling user.
1520 	*/
1521 
1522 	a = (ADDRESS *) xalloc(sizeof *a);
1523 	bzero((char *) a, sizeof *a);
1524 
1525 	p = strchr(user, ':');
1526 	if (p != NULL)
1527 		*p++ = '\0';
1528 	if (*user != '\0' && (pw = getpwnam(user)) != NULL)
1529 	{
1530 		if (strcmp(pw->pw_dir, "/") == 0)
1531 			a->q_home = "";
1532 		else
1533 			a->q_home = newstr(pw->pw_dir);
1534 		a->q_uid = pw->pw_uid;
1535 		a->q_gid = pw->pw_gid;
1536 		a->q_user = newstr(user);
1537 		a->q_flags |= QGOODUID;
1538 	}
1539 	else
1540 	{
1541 		a->q_user = newstr(DefUser);
1542 	}
1543 
1544 	a->q_flags |= QPRIMARY;		/* flag as a "ctladdr"  */
1545 	a->q_mailer = LocalMailer;
1546 	if (p == NULL)
1547 		a->q_paddr = a->q_user;
1548 	else
1549 		a->q_paddr = newstr(p);
1550 	return a;
1551 }
1552