1 /*
2  * Copyright (c) 1983, 1995 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 SMTP
13 static char sccsid[] = "@(#)usersmtp.c	8.51 (Berkeley) 04/29/95 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)usersmtp.c	8.51 (Berkeley) 04/29/95 (without SMTP)";
16 #endif
17 #endif /* not lint */
18 
19 # include <sysexits.h>
20 # include <errno.h>
21 
22 # ifdef SMTP
23 
24 /*
25 **  USERSMTP -- run SMTP protocol from the user end.
26 **
27 **	This protocol is described in RFC821.
28 */
29 
30 #define REPLYTYPE(r)	((r) / 100)		/* first digit of reply code */
31 #define REPLYCLASS(r)	(((r) / 10) % 10)	/* second digit of reply code */
32 #define SMTPCLOSING	421			/* "Service Shutting Down" */
33 
34 char	SmtpMsgBuffer[MAXLINE];		/* buffer for commands */
35 char	SmtpReplyBuffer[MAXLINE];	/* buffer for replies */
36 char	SmtpError[MAXLINE] = "";	/* save failure error messages */
37 int	SmtpPid;			/* pid of mailer */
38 bool	SmtpNeedIntro;			/* need "while talking" in transcript */
39 
40 #ifdef __STDC__
41 extern	smtpmessage(char *f, MAILER *m, MCI *mci, ...);
42 #endif
43 /*
44 **  SMTPINIT -- initialize SMTP.
45 **
46 **	Opens the connection and sends the initial protocol.
47 **
48 **	Parameters:
49 **		m -- mailer to create connection to.
50 **		pvp -- pointer to parameter vector to pass to
51 **			the mailer.
52 **
53 **	Returns:
54 **		none.
55 **
56 **	Side Effects:
57 **		creates connection and sends initial protocol.
58 */
59 
60 smtpinit(m, mci, e)
61 	struct mailer *m;
62 	register MCI *mci;
63 	ENVELOPE *e;
64 {
65 	register int r;
66 	register char *p;
67 	extern void esmtp_check();
68 	extern void helo_options();
69 
70 	if (tTd(18, 1))
71 	{
72 		printf("smtpinit ");
73 		mci_dump(mci, FALSE);
74 	}
75 
76 	/*
77 	**  Open the connection to the mailer.
78 	*/
79 
80 	SmtpError[0] = '\0';
81 	CurHostName = mci->mci_host;		/* XXX UGLY XXX */
82 	if (CurHostName == NULL)
83 		CurHostName = MyHostName;
84 	SmtpNeedIntro = TRUE;
85 	switch (mci->mci_state)
86 	{
87 	  case MCIS_ACTIVE:
88 		/* need to clear old information */
89 		smtprset(m, mci, e);
90 		/* fall through */
91 
92 	  case MCIS_OPEN:
93 		return;
94 
95 	  case MCIS_ERROR:
96 	  case MCIS_SSD:
97 		/* shouldn't happen */
98 		smtpquit(m, mci, e);
99 		/* fall through */
100 
101 	  case MCIS_CLOSED:
102 		syserr("451 smtpinit: state CLOSED");
103 		return;
104 
105 	  case MCIS_OPENING:
106 		break;
107 	}
108 
109 	mci->mci_state = MCIS_OPENING;
110 
111 	/*
112 	**  Get the greeting message.
113 	**	This should appear spontaneously.  Give it five minutes to
114 	**	happen.
115 	*/
116 
117 	SmtpPhase = mci->mci_phase = "client greeting";
118 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
119 	r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check);
120 	if (r < 0 || REPLYTYPE(r) == 4)
121 		goto tempfail1;
122 	if (REPLYTYPE(r) != 2)
123 		goto unavailable;
124 
125 	/*
126 	**  Send the HELO command.
127 	**	My mother taught me to always introduce myself.
128 	*/
129 
130 	if (bitnset(M_ESMTP, m->m_flags))
131 		mci->mci_flags |= MCIF_ESMTP;
132 
133 tryhelo:
134 	if (bitset(MCIF_ESMTP, mci->mci_flags))
135 	{
136 		smtpmessage("EHLO %s", m, mci, MyHostName);
137 		SmtpPhase = mci->mci_phase = "client EHLO";
138 	}
139 	else
140 	{
141 		smtpmessage("HELO %s", m, mci, MyHostName);
142 		SmtpPhase = mci->mci_phase = "client HELO";
143 	}
144 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
145 	r = reply(m, mci, e, TimeOuts.to_helo, helo_options);
146 	if (r < 0)
147 		goto tempfail1;
148 	else if (REPLYTYPE(r) == 5)
149 	{
150 		if (bitset(MCIF_ESMTP, mci->mci_flags))
151 		{
152 			/* try old SMTP instead */
153 			mci->mci_flags &= ~MCIF_ESMTP;
154 			goto tryhelo;
155 		}
156 		goto unavailable;
157 	}
158 	else if (REPLYTYPE(r) != 2)
159 		goto tempfail1;
160 
161 	/*
162 	**  Check to see if we actually ended up talking to ourself.
163 	**  This means we didn't know about an alias or MX, or we managed
164 	**  to connect to an echo server.
165 	*/
166 
167 	p = strchr(&SmtpReplyBuffer[4], ' ');
168 	if (p != NULL)
169 		*p = '\0';
170 	if (!bitnset(M_NOLOOPCHECK, m->m_flags) &&
171 	    strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0)
172 	{
173 		syserr("553 %s config error: mail loops back to myself",
174 			MyHostName);
175 		mci->mci_exitstat = EX_CONFIG;
176 		mci->mci_errno = 0;
177 		smtpquit(m, mci, e);
178 		return;
179 	}
180 
181 	/*
182 	**  If this is expected to be another sendmail, send some internal
183 	**  commands.
184 	*/
185 
186 	if (bitnset(M_INTERNAL, m->m_flags))
187 	{
188 		/* tell it to be verbose */
189 		smtpmessage("VERB", m, mci);
190 		r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
191 		if (r < 0)
192 			goto tempfail2;
193 	}
194 
195 	if (mci->mci_state != MCIS_CLOSED)
196 	{
197 		mci->mci_state = MCIS_OPEN;
198 		return;
199 	}
200 
201 	/* got a 421 error code during startup */
202 
203   tempfail1:
204   tempfail2:
205 	mci->mci_exitstat = EX_TEMPFAIL;
206 	if (mci->mci_errno == 0)
207 		mci->mci_errno = errno;
208 	if (mci->mci_state != MCIS_CLOSED)
209 		smtpquit(m, mci, e);
210 	return;
211 
212   unavailable:
213 	mci->mci_exitstat = EX_UNAVAILABLE;
214 	mci->mci_errno = errno;
215 	smtpquit(m, mci, e);
216 	return;
217 }
218 /*
219 **  ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol
220 **
221 **	Parameters:
222 **		line -- the response line.
223 **		firstline -- set if this is the first line of the reply.
224 **		m -- the mailer.
225 **		mci -- the mailer connection info.
226 **		e -- the envelope.
227 **
228 **	Returns:
229 **		none.
230 */
231 
232 void
233 esmtp_check(line, firstline, m, mci, e)
234 	char *line;
235 	bool firstline;
236 	MAILER *m;
237 	register MCI *mci;
238 	ENVELOPE *e;
239 {
240 	register char *l;
241 
242 	for (l = line; (l = strchr(++l, 'E')) != NULL; )
243 	{
244 		if (strncmp(l, "ESMTP ", 6) == 0)
245 		{
246 			mci->mci_flags |= MCIF_ESMTP;
247 			break;
248 		}
249 	}
250 	for (l = line; (l = strchr(++l, '8')) != NULL; )
251 	{
252 		if (strncmp(l, "8BIT OK", 7) == 0)
253 		{
254 			mci->mci_flags |= MCIF_8BITOK;
255 			break;
256 		}
257 	}
258 }
259 /*
260 **  HELO_OPTIONS -- process the options on a HELO line.
261 **
262 **	Parameters:
263 **		line -- the response line.
264 **		firstline -- set if this is the first line of the reply.
265 **		m -- the mailer.
266 **		mci -- the mailer connection info.
267 **		e -- the envelope.
268 **
269 **	Returns:
270 **		none.
271 */
272 
273 void
274 helo_options(line, firstline, m, mci, e)
275 	char *line;
276 	bool firstline;
277 	MAILER *m;
278 	register MCI *mci;
279 	ENVELOPE *e;
280 {
281 	register char *p;
282 
283 	if (firstline)
284 		return;
285 
286 	if (strlen(line) < (SIZE_T) 5)
287 		return;
288 	line += 4;
289 	p = strchr(line, ' ');
290 	if (p != NULL)
291 		*p++ = '\0';
292 	if (strcasecmp(line, "size") == 0)
293 	{
294 		mci->mci_flags |= MCIF_SIZE;
295 		if (p != NULL)
296 			mci->mci_maxsize = atol(p);
297 	}
298 	else if (strcasecmp(line, "8bitmime") == 0)
299 	{
300 		mci->mci_flags |= MCIF_8BITMIME;
301 		mci->mci_flags &= ~MCIF_7BIT;
302 	}
303 	else if (strcasecmp(line, "expn") == 0)
304 		mci->mci_flags |= MCIF_EXPN;
305 	else if (strcasecmp(line, "x-dsn-03") == 0)
306 		mci->mci_flags |= MCIF_DSN;
307 }
308 /*
309 **  SMTPMAILFROM -- send MAIL command
310 **
311 **	Parameters:
312 **		m -- the mailer.
313 **		mci -- the mailer connection structure.
314 **		e -- the envelope (including the sender to specify).
315 */
316 
317 smtpmailfrom(m, mci, e)
318 	struct mailer *m;
319 	MCI *mci;
320 	ENVELOPE *e;
321 {
322 	int r;
323 	char *bufp;
324 	char *bodytype;
325 	char buf[MAXNAME + 1];
326 	char optbuf[MAXLINE];
327 
328 	if (tTd(18, 2))
329 		printf("smtpmailfrom: CurHost=%s\n", CurHostName);
330 
331 	/* set up appropriate options to include */
332 	if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0)
333 		sprintf(optbuf, " SIZE=%ld", e->e_msgsize);
334 	else
335 		strcpy(optbuf, "");
336 
337 	bodytype = e->e_bodytype;
338 	if (bitset(MCIF_8BITMIME, mci->mci_flags))
339 	{
340 		if (bodytype == NULL &&
341 		    bitset(MM_MIME8BIT, MimeMode) &&
342 		    bitset(EF_HAS8BIT, e->e_flags) &&
343 		    !bitnset(M_8BITS, m->m_flags))
344 			bodytype = "8BITMIME";
345 		if (bodytype != NULL)
346 		{
347 			strcat(optbuf, " BODY=");
348 			strcat(optbuf, bodytype);
349 		}
350 	}
351 	else if (bitnset(M_8BITS, m->m_flags) ||
352 		 !bitset(EF_HAS8BIT, e->e_flags))
353 	{
354 		/* just pass it through */
355 	}
356 	else if (bitset(MM_CVTMIME, MimeMode) &&
357 		 (!bitset(MM_PASS8BIT, MimeMode) ||
358 		  bitset(EF_IS_MIME, e->e_flags)))
359 	{
360 		/* must convert from 8bit MIME format to 7bit encoded */
361 		mci->mci_flags |= MCIF_CVT8TO7;
362 	}
363 	else if (!bitset(MM_PASS8BIT, MimeMode))
364 	{
365 		/* cannot just send a 8-bit version */
366 		usrerr("%s does not support 8BITMIME", mci->mci_host);
367 		mci->mci_status = "5.6.3";
368 		return EX_DATAERR;
369 	}
370 
371 	if (bitset(MCIF_DSN, mci->mci_flags))
372 	{
373 		if (e->e_envid != NULL)
374 		{
375 			strcat(optbuf, " ENVID=");
376 			strcat(optbuf, e->e_envid);
377 		}
378 
379 		/* RET= parameter */
380 		if (bitset(EF_RET_PARAM, e->e_flags))
381 		{
382 			strcat(optbuf, " RET=");
383 			if (bitset(EF_NO_BODY_RETN, e->e_flags))
384 				strcat(optbuf, "HDRS");
385 			else
386 				strcat(optbuf, "FULL");
387 		}
388 	}
389 
390 	/*
391 	**  Send the MAIL command.
392 	**	Designates the sender.
393 	*/
394 
395 	mci->mci_state = MCIS_ACTIVE;
396 
397 	if (bitset(EF_RESPONSE, e->e_flags) &&
398 	    !bitnset(M_NO_NULL_FROM, m->m_flags))
399 		(void) strcpy(buf, "");
400 	else
401 		expand("\201g", buf, sizeof buf, e);
402 	if (buf[0] == '<')
403 	{
404 		/* strip off <angle brackets> (put back on below) */
405 		bufp = &buf[strlen(buf) - 1];
406 		if (*bufp == '>')
407 			*bufp = '\0';
408 		bufp = &buf[1];
409 	}
410 	else
411 		bufp = buf;
412 	if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) ||
413 	    !bitnset(M_FROMPATH, m->m_flags))
414 	{
415 		smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf);
416 	}
417 	else
418 	{
419 		smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
420 			*bufp == '@' ? ',' : ':', bufp, optbuf);
421 	}
422 	SmtpPhase = mci->mci_phase = "client MAIL";
423 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
424 	r = reply(m, mci, e, TimeOuts.to_mail, NULL);
425 	if (r < 0 || r == 421)
426 	{
427 		/* communications failure/service shutting down */
428 		mci->mci_exitstat = EX_TEMPFAIL;
429 		mci->mci_errno = errno;
430 		smtpquit(m, mci, e);
431 		return EX_TEMPFAIL;
432 	}
433 	else if (REPLYTYPE(r) == 4)
434 	{
435 		return EX_TEMPFAIL;
436 	}
437 	else if (r == 250)
438 	{
439 		return EX_OK;
440 	}
441 	else if (r == 501)
442 	{
443 		/* syntax error in arguments */
444 		mci->mci_status = "5.5.2";
445 		return EX_DATAERR;
446 	}
447 	else if (r == 553)
448 	{
449 		/* mailbox name not allowed */
450 		mci->mci_status = "5.1.3";
451 		return EX_DATAERR;
452 	}
453 	else if (r == 552)
454 	{
455 		/* exceeded storage allocation */
456 		mci->mci_status = "5.2.2";
457 		return EX_UNAVAILABLE;
458 	}
459 
460 #ifdef LOG
461 	if (LogLevel > 1)
462 	{
463 		syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s",
464 			e->e_id, mci->mci_host, SmtpReplyBuffer);
465 	}
466 #endif
467 
468 	/* protocol error -- close up */
469 	smtpquit(m, mci, e);
470 	return EX_PROTOCOL;
471 }
472 /*
473 **  SMTPRCPT -- designate recipient.
474 **
475 **	Parameters:
476 **		to -- address of recipient.
477 **		m -- the mailer we are sending to.
478 **		mci -- the connection info for this transaction.
479 **		e -- the envelope for this transaction.
480 **
481 **	Returns:
482 **		exit status corresponding to recipient status.
483 **
484 **	Side Effects:
485 **		Sends the mail via SMTP.
486 */
487 
488 smtprcpt(to, m, mci, e)
489 	ADDRESS *to;
490 	register MAILER *m;
491 	MCI *mci;
492 	ENVELOPE *e;
493 {
494 	register int r;
495 	char optbuf[MAXLINE];
496 	extern char *smtptodsn();
497 
498 	strcpy(optbuf, "");
499 	if (bitset(MCIF_DSN, mci->mci_flags))
500 	{
501 		/* NOTIFY= parameter */
502 		if (bitset(QHASNOTIFY, to->q_flags) &&
503 		    bitset(QPRIMARY, to->q_flags))
504 		{
505 			bool firstone = TRUE;
506 
507 			strcat(optbuf, " NOTIFY=");
508 			if (bitset(QPINGONSUCCESS, to->q_flags))
509 			{
510 				strcat(optbuf, "SUCCESS");
511 				firstone = FALSE;
512 			}
513 			if (bitset(QPINGONFAILURE, to->q_flags))
514 			{
515 				if (!firstone)
516 					strcat(optbuf, ",");
517 				strcat(optbuf, "FAILURE");
518 				firstone = FALSE;
519 			}
520 			if (bitset(QPINGONDELAY, to->q_flags))
521 			{
522 				if (!firstone)
523 					strcat(optbuf, ",");
524 				strcat(optbuf, "DELAY");
525 				firstone = FALSE;
526 			}
527 			if (firstone)
528 				strcat(optbuf, "NEVER");
529 		}
530 
531 		/* ORCPT= parameter */
532 		if (to->q_orcpt != NULL)
533 		{
534 			strcat(optbuf, " ORCPT=");
535 			strcat(optbuf, to->q_orcpt);
536 		}
537 	}
538 
539 	smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
540 
541 	SmtpPhase = mci->mci_phase = "client RCPT";
542 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
543 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
544 	to->q_rstatus = newstr(SmtpReplyBuffer);
545 	to->q_status = smtptodsn(r);
546 	if (r < 0 || REPLYTYPE(r) == 4)
547 		return EX_TEMPFAIL;
548 	else if (REPLYTYPE(r) == 2)
549 		return EX_OK;
550 	else if (r == 550 || r == 551 || r == 553)
551 		return EX_NOUSER;
552 	else if (r == 552 || r == 554)
553 		return EX_UNAVAILABLE;
554 
555 #ifdef LOG
556 	if (LogLevel > 1)
557 	{
558 		syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s",
559 			e->e_id, mci->mci_host, SmtpReplyBuffer);
560 	}
561 #endif
562 
563 	return (EX_PROTOCOL);
564 }
565 /*
566 **  SMTPDATA -- send the data and clean up the transaction.
567 **
568 **	Parameters:
569 **		m -- mailer being sent to.
570 **		e -- the envelope for this message.
571 **
572 **	Returns:
573 **		exit status corresponding to DATA command.
574 **
575 **	Side Effects:
576 **		none.
577 */
578 
579 static jmp_buf	CtxDataTimeout;
580 static void	datatimeout();
581 
582 smtpdata(m, mci, e)
583 	struct mailer *m;
584 	register MCI *mci;
585 	register ENVELOPE *e;
586 {
587 	register int r;
588 	register EVENT *ev;
589 	time_t timeout;
590 
591 	/*
592 	**  Send the data.
593 	**	First send the command and check that it is ok.
594 	**	Then send the data.
595 	**	Follow it up with a dot to terminate.
596 	**	Finally get the results of the transaction.
597 	*/
598 
599 	/* send the command and check ok to proceed */
600 	smtpmessage("DATA", m, mci);
601 	SmtpPhase = mci->mci_phase = "client DATA 354";
602 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
603 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
604 	if (r < 0 || REPLYTYPE(r) == 4)
605 	{
606 		smtpquit(m, mci, e);
607 		return (EX_TEMPFAIL);
608 	}
609 	else if (r == 554)
610 	{
611 		smtprset(m, mci, e);
612 		return (EX_UNAVAILABLE);
613 	}
614 	else if (r != 354)
615 	{
616 #ifdef LOG
617 		if (LogLevel > 1)
618 		{
619 			syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s",
620 				e->e_id, mci->mci_host, SmtpReplyBuffer);
621 		}
622 #endif
623 		smtprset(m, mci, e);
624 		return (EX_PROTOCOL);
625 	}
626 
627 	/*
628 	**  Set timeout around data writes.  Make it at least large
629 	**  enough for DNS timeouts on all recipients plus some fudge
630 	**  factor.  The main thing is that it should not be infinite.
631 	*/
632 
633 	if (setjmp(CtxDataTimeout) != 0)
634 	{
635 		mci->mci_errno = errno;
636 		mci->mci_exitstat = EX_TEMPFAIL;
637 		mci->mci_state = MCIS_ERROR;
638 		syserr("451 timeout writing message to %s", mci->mci_host);
639 		smtpquit(m, mci, e);
640 		return EX_TEMPFAIL;
641 	}
642 
643 	timeout = e->e_msgsize / 16;
644 	if (timeout < (time_t) 600)
645 		timeout = (time_t) 600;
646 	timeout += e->e_nrcpts * 300;
647 	ev = setevent(timeout, datatimeout, 0);
648 
649 	/*
650 	**  Output the actual message.
651 	*/
652 
653 	(*e->e_puthdr)(mci, e->e_header, e);
654 	(*e->e_putbody)(mci, e, NULL);
655 
656 	/*
657 	**  Cleanup after sending message.
658 	*/
659 
660 	clrevent(ev);
661 
662 	if (ferror(mci->mci_out))
663 	{
664 		/* error during processing -- don't send the dot */
665 		mci->mci_errno = EIO;
666 		mci->mci_exitstat = EX_IOERR;
667 		mci->mci_state = MCIS_ERROR;
668 		smtpquit(m, mci, e);
669 		return EX_IOERR;
670 	}
671 
672 	/* terminate the message */
673 	fprintf(mci->mci_out, ".%s", m->m_eol);
674 	if (TrafficLogFile != NULL)
675 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
676 	if (Verbose)
677 		nmessage(">>> .");
678 
679 	/* check for the results of the transaction */
680 	SmtpPhase = mci->mci_phase = "client DATA 250";
681 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
682 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
683 	if (r < 0)
684 	{
685 		smtpquit(m, mci, e);
686 		return (EX_TEMPFAIL);
687 	}
688 	mci->mci_state = MCIS_OPEN;
689 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
690 	if (REPLYTYPE(r) == 4)
691 		return (EX_TEMPFAIL);
692 	else if (r == 250)
693 		return (EX_OK);
694 	else if (r == 552 || r == 554)
695 		return (EX_UNAVAILABLE);
696 #ifdef LOG
697 	if (LogLevel > 1)
698 	{
699 		syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s",
700 			e->e_id, mci->mci_host, SmtpReplyBuffer);
701 	}
702 #endif
703 	return (EX_PROTOCOL);
704 }
705 
706 
707 static void
708 datatimeout()
709 {
710 	longjmp(CtxDataTimeout, 1);
711 }
712 /*
713 **  SMTPQUIT -- close the SMTP connection.
714 **
715 **	Parameters:
716 **		m -- a pointer to the mailer.
717 **
718 **	Returns:
719 **		none.
720 **
721 **	Side Effects:
722 **		sends the final protocol and closes the connection.
723 */
724 
725 smtpquit(m, mci, e)
726 	register MAILER *m;
727 	register MCI *mci;
728 	ENVELOPE *e;
729 {
730 	bool oldSuprErrs = SuprErrs;
731 
732 	/*
733 	**	Suppress errors here -- we may be processing a different
734 	**	job when we do the quit connection, and we don't want the
735 	**	new job to be penalized for something that isn't it's
736 	**	problem.
737 	*/
738 
739 	SuprErrs = TRUE;
740 
741 	/* send the quit message if we haven't gotten I/O error */
742 	if (mci->mci_state != MCIS_ERROR)
743 	{
744 		SmtpPhase = "client QUIT";
745 		smtpmessage("QUIT", m, mci);
746 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
747 		SuprErrs = oldSuprErrs;
748 		if (mci->mci_state == MCIS_CLOSED)
749 		{
750 			SuprErrs = oldSuprErrs;
751 			return;
752 		}
753 	}
754 
755 	/* now actually close the connection and pick up the zombie */
756 	(void) endmailer(mci, e, NULL);
757 
758 	SuprErrs = oldSuprErrs;
759 }
760 /*
761 **  SMTPRSET -- send a RSET (reset) command
762 */
763 
764 smtprset(m, mci, e)
765 	register MAILER *m;
766 	register MCI *mci;
767 	ENVELOPE *e;
768 {
769 	int r;
770 
771 	SmtpPhase = "client RSET";
772 	smtpmessage("RSET", m, mci);
773 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
774 	if (r < 0)
775 		mci->mci_state = MCIS_ERROR;
776 	else if (REPLYTYPE(r) == 2)
777 	{
778 		mci->mci_state = MCIS_OPEN;
779 		return;
780 	}
781 	smtpquit(m, mci, e);
782 }
783 /*
784 **  SMTPPROBE -- check the connection state
785 */
786 
787 smtpprobe(mci)
788 	register MCI *mci;
789 {
790 	int r;
791 	MAILER *m = mci->mci_mailer;
792 	extern ENVELOPE BlankEnvelope;
793 	ENVELOPE *e = &BlankEnvelope;
794 
795 	SmtpPhase = "client probe";
796 	smtpmessage("RSET", m, mci);
797 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
798 	if (r < 0 || REPLYTYPE(r) != 2)
799 		smtpquit(m, mci, e);
800 	return r;
801 }
802 /*
803 **  REPLY -- read arpanet reply
804 **
805 **	Parameters:
806 **		m -- the mailer we are reading the reply from.
807 **		mci -- the mailer connection info structure.
808 **		e -- the current envelope.
809 **		timeout -- the timeout for reads.
810 **		pfunc -- processing function called on each line of response.
811 **			If null, no special processing is done.
812 **
813 **	Returns:
814 **		reply code it reads.
815 **
816 **	Side Effects:
817 **		flushes the mail file.
818 */
819 
820 reply(m, mci, e, timeout, pfunc)
821 	MAILER *m;
822 	MCI *mci;
823 	ENVELOPE *e;
824 	time_t timeout;
825 	void (*pfunc)();
826 {
827 	register char *bufp;
828 	register int r;
829 	bool firstline = TRUE;
830 	char junkbuf[MAXLINE];
831 
832 	if (mci->mci_out != NULL)
833 		(void) fflush(mci->mci_out);
834 
835 	if (tTd(18, 1))
836 		printf("reply\n");
837 
838 	/*
839 	**  Read the input line, being careful not to hang.
840 	*/
841 
842 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
843 	{
844 		register char *p;
845 		extern time_t curtime();
846 
847 		/* actually do the read */
848 		if (e->e_xfp != NULL)
849 			(void) fflush(e->e_xfp);	/* for debugging */
850 
851 		/* if we are in the process of closing just give the code */
852 		if (mci->mci_state == MCIS_CLOSED)
853 			return (SMTPCLOSING);
854 
855 		if (mci->mci_out != NULL)
856 			fflush(mci->mci_out);
857 
858 		/* get the line from the other side */
859 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
860 		mci->mci_lastuse = curtime();
861 
862 		if (p == NULL)
863 		{
864 			bool oldholderrs;
865 			extern char MsgBuf[];		/* err.c */
866 
867 			/* if the remote end closed early, fake an error */
868 			if (errno == 0)
869 # ifdef ECONNRESET
870 				errno = ECONNRESET;
871 # else /* ECONNRESET */
872 				errno = EPIPE;
873 # endif /* ECONNRESET */
874 
875 			mci->mci_errno = errno;
876 			mci->mci_exitstat = EX_TEMPFAIL;
877 			oldholderrs = HoldErrs;
878 			HoldErrs = TRUE;
879 			usrerr("451 reply: read error from %s", mci->mci_host);
880 
881 			/* if debugging, pause so we can see state */
882 			if (tTd(18, 100))
883 				pause();
884 			mci->mci_state = MCIS_ERROR;
885 			smtpquit(m, mci, e);
886 #ifdef XDEBUG
887 			{
888 				char wbuf[MAXLINE];
889 				char *p = wbuf;
890 				if (e->e_to != NULL)
891 				{
892 					sprintf(p, "%s... ", e->e_to);
893 					p += strlen(p);
894 				}
895 				sprintf(p, "reply(%s) during %s",
896 					mci->mci_host, SmtpPhase);
897 				checkfd012(wbuf);
898 			}
899 #endif
900 			HoldErrs = oldholderrs;
901 			return (-1);
902 		}
903 		fixcrlf(bufp, TRUE);
904 
905 		/* EHLO failure is not a real error */
906 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
907 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
908 		{
909 			/* serious error -- log the previous command */
910 			if (SmtpNeedIntro)
911 			{
912 				/* inform user who we are chatting with */
913 				fprintf(CurEnv->e_xfp,
914 					"... while talking to %s:\n",
915 					CurHostName);
916 				SmtpNeedIntro = FALSE;
917 			}
918 			if (SmtpMsgBuffer[0] != '\0')
919 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
920 			SmtpMsgBuffer[0] = '\0';
921 
922 			/* now log the message as from the other side */
923 			fprintf(e->e_xfp, "<<< %s\n", bufp);
924 		}
925 
926 		/* display the input for verbose mode */
927 		if (Verbose)
928 			nmessage("050 %s", bufp);
929 
930 		/* process the line */
931 		if (pfunc != NULL)
932 			(*pfunc)(bufp, firstline, m, mci, e);
933 
934 		firstline = FALSE;
935 
936 		/* if continuation is required, we can go on */
937 		if (bufp[3] == '-')
938 			continue;
939 
940 		/* ignore improperly formated input */
941 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
942 			continue;
943 
944 		/* decode the reply code */
945 		r = atoi(bufp);
946 
947 		/* extra semantics: 0xx codes are "informational" */
948 		if (r >= 100)
949 			break;
950 	}
951 
952 	/*
953 	**  Now look at SmtpReplyBuffer -- only care about the first
954 	**  line of the response from here on out.
955 	*/
956 
957 	/* save temporary failure messages for posterity */
958 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
959 		(void) strcpy(SmtpError, SmtpReplyBuffer);
960 
961 	/* reply code 421 is "Service Shutting Down" */
962 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
963 	{
964 		/* send the quit protocol */
965 		mci->mci_state = MCIS_SSD;
966 		smtpquit(m, mci, e);
967 	}
968 
969 	return (r);
970 }
971 /*
972 **  SMTPMESSAGE -- send message to server
973 **
974 **	Parameters:
975 **		f -- format
976 **		m -- the mailer to control formatting.
977 **		a, b, c -- parameters
978 **
979 **	Returns:
980 **		none.
981 **
982 **	Side Effects:
983 **		writes message to mci->mci_out.
984 */
985 
986 /*VARARGS1*/
987 #ifdef __STDC__
988 smtpmessage(char *f, MAILER *m, MCI *mci, ...)
989 #else
990 smtpmessage(f, m, mci, va_alist)
991 	char *f;
992 	MAILER *m;
993 	MCI *mci;
994 	va_dcl
995 #endif
996 {
997 	VA_LOCAL_DECL
998 
999 	VA_START(mci);
1000 	(void) vsprintf(SmtpMsgBuffer, f, ap);
1001 	VA_END;
1002 
1003 	if (tTd(18, 1) || Verbose)
1004 		nmessage(">>> %s", SmtpMsgBuffer);
1005 	if (TrafficLogFile != NULL)
1006 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
1007 	if (mci->mci_out != NULL)
1008 	{
1009 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
1010 			m == NULL ? "\r\n" : m->m_eol);
1011 	}
1012 	else if (tTd(18, 1))
1013 	{
1014 		printf("smtpmessage: NULL mci_out\n");
1015 	}
1016 }
1017 
1018 # endif /* SMTP */
1019