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 SMTP
13 static char sccsid[] = "@(#)usersmtp.c	8.13 (Berkeley) 10/24/93 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)usersmtp.c	8.13 (Berkeley) 10/24/93 (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 	SmtpNeedIntro = TRUE;
83 	switch (mci->mci_state)
84 	{
85 	  case MCIS_ACTIVE:
86 		/* need to clear old information */
87 		smtprset(m, mci, e);
88 		/* fall through */
89 
90 	  case MCIS_OPEN:
91 		return;
92 
93 	  case MCIS_ERROR:
94 	  case MCIS_SSD:
95 		/* shouldn't happen */
96 		smtpquit(m, mci, e);
97 		/* fall through */
98 
99 	  case MCIS_CLOSED:
100 		syserr("451 smtpinit: state CLOSED");
101 		return;
102 
103 	  case MCIS_OPENING:
104 		break;
105 	}
106 
107 	mci->mci_state = MCIS_OPENING;
108 
109 	/*
110 	**  Get the greeting message.
111 	**	This should appear spontaneously.  Give it five minutes to
112 	**	happen.
113 	*/
114 
115 	SmtpPhase = mci->mci_phase = "client greeting";
116 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
117 	r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check);
118 	if (r < 0 || REPLYTYPE(r) == 4)
119 		goto tempfail1;
120 	if (REPLYTYPE(r) != 2)
121 		goto unavailable;
122 
123 	/*
124 	**  Send the HELO command.
125 	**	My mother taught me to always introduce myself.
126 	*/
127 
128 	if (bitnset(M_ESMTP, m->m_flags))
129 		mci->mci_flags |= MCIF_ESMTP;
130 
131 tryhelo:
132 	if (bitset(MCIF_ESMTP, mci->mci_flags))
133 	{
134 		smtpmessage("EHLO %s", m, mci, MyHostName);
135 		SmtpPhase = mci->mci_phase = "client EHLO";
136 	}
137 	else
138 	{
139 		smtpmessage("HELO %s", m, mci, MyHostName);
140 		SmtpPhase = mci->mci_phase = "client HELO";
141 	}
142 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
143 	r = reply(m, mci, e, TimeOuts.to_helo, helo_options);
144 	if (r < 0)
145 		goto tempfail1;
146 	else if (REPLYTYPE(r) == 5)
147 	{
148 		if (bitset(MCIF_ESMTP, mci->mci_flags))
149 		{
150 			/* try old SMTP instead */
151 			mci->mci_flags &= ~MCIF_ESMTP;
152 			goto tryhelo;
153 		}
154 		goto unavailable;
155 	}
156 	else if (REPLYTYPE(r) != 2)
157 		goto tempfail1;
158 
159 	/*
160 	**  Check to see if we actually ended up talking to ourself.
161 	**  This means we didn't know about an alias or MX, or we managed
162 	**  to connect to an echo server.
163 	**
164 	**	If this code remains at all, "CheckLoopBack" should be
165 	**	a mailer flag.  This is a MAYBENEXTRELEASE feature.
166 	*/
167 
168 	p = strchr(&SmtpReplyBuffer[4], ' ');
169 	if (p != NULL)
170 		*p = '\0';
171 	if (CheckLoopBack && 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 	mci->mci_state = MCIS_OPEN;
196 	return;
197 
198   tempfail1:
199   tempfail2:
200 	mci->mci_exitstat = EX_TEMPFAIL;
201 	if (mci->mci_errno == 0)
202 		mci->mci_errno = errno;
203 	if (mci->mci_state != MCIS_CLOSED)
204 		smtpquit(m, mci, e);
205 	return;
206 
207   unavailable:
208 	mci->mci_exitstat = EX_UNAVAILABLE;
209 	mci->mci_errno = errno;
210 	smtpquit(m, mci, e);
211 	return;
212 }
213 /*
214 **  ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol
215 **
216 **
217 **	Parameters:
218 **		line -- the response line.
219 **		m -- the mailer.
220 **		mci -- the mailer connection info.
221 **		e -- the envelope.
222 **
223 **	Returns:
224 **		none.
225 */
226 
227 void
228 esmtp_check(line, m, mci, e)
229 	char *line;
230 	MAILER *m;
231 	register MCI *mci;
232 	ENVELOPE *e;
233 {
234 	if (strlen(line) < 5)
235 		return;
236 	line += 4;
237 	if (strncmp(line, "ESMTP ", 6) == 0)
238 		mci->mci_flags |= MCIF_ESMTP;
239 }
240 /*
241 **  HELO_OPTIONS -- process the options on a HELO line.
242 **
243 **	Parameters:
244 **		line -- the response line.
245 **		m -- the mailer.
246 **		mci -- the mailer connection info.
247 **		e -- the envelope.
248 **
249 **	Returns:
250 **		none.
251 */
252 
253 void
254 helo_options(line, m, mci, e)
255 	char *line;
256 	MAILER *m;
257 	register MCI *mci;
258 	ENVELOPE *e;
259 {
260 	register char *p;
261 
262 	if (strlen(line) < 5)
263 		return;
264 	line += 4;
265 	p = strchr(line, ' ');
266 	if (p != NULL)
267 		*p++ = '\0';
268 	if (strcasecmp(line, "size") == 0)
269 	{
270 		mci->mci_flags |= MCIF_SIZE;
271 		if (p != NULL)
272 			mci->mci_maxsize = atol(p);
273 	}
274 	else if (strcasecmp(line, "8bitmime") == 0)
275 		mci->mci_flags |= MCIF_8BITMIME;
276 	else if (strcasecmp(line, "expn") == 0)
277 		mci->mci_flags |= MCIF_EXPN;
278 }
279 /*
280 **  SMTPMAILFROM -- send MAIL command
281 **
282 **	Parameters:
283 **		m -- the mailer.
284 **		mci -- the mailer connection structure.
285 **		e -- the envelope (including the sender to specify).
286 */
287 
288 smtpmailfrom(m, mci, e)
289 	struct mailer *m;
290 	MCI *mci;
291 	ENVELOPE *e;
292 {
293 	int r;
294 	char buf[MAXNAME];
295 	char optbuf[MAXLINE];
296 
297 	if (tTd(18, 2))
298 		printf("smtpmailfrom: CurHost=%s\n", CurHostName);
299 
300 	/* set up appropriate options to include */
301 	if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0)
302 		sprintf(optbuf, " SIZE=%ld", e->e_msgsize);
303 	else
304 		strcpy(optbuf, "");
305 
306 	/*
307 	**  Send the MAIL command.
308 	**	Designates the sender.
309 	*/
310 
311 	mci->mci_state = MCIS_ACTIVE;
312 
313 	if (bitset(EF_RESPONSE, e->e_flags) &&
314 	    !bitnset(M_NO_NULL_FROM, m->m_flags))
315 		(void) strcpy(buf, "");
316 	else
317 		expand("\201g", buf, &buf[sizeof buf - 1], e);
318 	if (e->e_from.q_mailer == LocalMailer ||
319 	    !bitnset(M_FROMPATH, m->m_flags))
320 	{
321 		smtpmessage("MAIL From:<%s>%s", m, mci, buf, optbuf);
322 	}
323 	else
324 	{
325 		smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
326 			buf[0] == '@' ? ',' : ':', buf, optbuf);
327 	}
328 	SmtpPhase = mci->mci_phase = "client MAIL";
329 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
330 	r = reply(m, mci, e, TimeOuts.to_mail, NULL);
331 	if (r < 0 || REPLYTYPE(r) == 4)
332 	{
333 		mci->mci_exitstat = EX_TEMPFAIL;
334 		mci->mci_errno = errno;
335 		smtpquit(m, mci, e);
336 		return EX_TEMPFAIL;
337 	}
338 	else if (r == 250)
339 	{
340 		mci->mci_exitstat = EX_OK;
341 		return EX_OK;
342 	}
343 	else if (r == 552)
344 	{
345 		/* signal service unavailable */
346 		mci->mci_exitstat = EX_UNAVAILABLE;
347 		smtpquit(m, mci, e);
348 		return EX_UNAVAILABLE;
349 	}
350 
351 #ifdef LOG
352 	if (LogLevel > 1)
353 	{
354 		syslog(LOG_CRIT, "%s: SMTP MAIL protocol error: %s",
355 			e->e_id, SmtpReplyBuffer);
356 	}
357 #endif
358 
359 	/* protocol error -- close up */
360 	smtpquit(m, mci, e);
361 	mci->mci_exitstat = EX_PROTOCOL;
362 	return EX_PROTOCOL;
363 }
364 /*
365 **  SMTPRCPT -- designate recipient.
366 **
367 **	Parameters:
368 **		to -- address of recipient.
369 **		m -- the mailer we are sending to.
370 **		mci -- the connection info for this transaction.
371 **		e -- the envelope for this transaction.
372 **
373 **	Returns:
374 **		exit status corresponding to recipient status.
375 **
376 **	Side Effects:
377 **		Sends the mail via SMTP.
378 */
379 
380 smtprcpt(to, m, mci, e)
381 	ADDRESS *to;
382 	register MAILER *m;
383 	MCI *mci;
384 	ENVELOPE *e;
385 {
386 	register int r;
387 
388 	smtpmessage("RCPT To:<%s>", m, mci, to->q_user);
389 
390 	SmtpPhase = mci->mci_phase = "client RCPT";
391 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
392 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
393 	if (r < 0 || REPLYTYPE(r) == 4)
394 		return (EX_TEMPFAIL);
395 	else if (REPLYTYPE(r) == 2)
396 		return (EX_OK);
397 	else if (r == 550 || r == 551 || r == 553)
398 		return (EX_NOUSER);
399 	else if (r == 552 || r == 554)
400 		return (EX_UNAVAILABLE);
401 
402 #ifdef LOG
403 	if (LogLevel > 1)
404 	{
405 		syslog(LOG_CRIT, "%s: SMTP RCPT protocol error: %s",
406 			e->e_id, SmtpReplyBuffer);
407 	}
408 #endif
409 
410 	return (EX_PROTOCOL);
411 }
412 /*
413 **  SMTPDATA -- send the data and clean up the transaction.
414 **
415 **	Parameters:
416 **		m -- mailer being sent to.
417 **		e -- the envelope for this message.
418 **
419 **	Returns:
420 **		exit status corresponding to DATA command.
421 **
422 **	Side Effects:
423 **		none.
424 */
425 
426 static jmp_buf	CtxDataTimeout;
427 static int	datatimeout();
428 
429 smtpdata(m, mci, e)
430 	struct mailer *m;
431 	register MCI *mci;
432 	register ENVELOPE *e;
433 {
434 	register int r;
435 	register EVENT *ev;
436 	time_t timeout;
437 
438 	/*
439 	**  Send the data.
440 	**	First send the command and check that it is ok.
441 	**	Then send the data.
442 	**	Follow it up with a dot to terminate.
443 	**	Finally get the results of the transaction.
444 	*/
445 
446 	/* send the command and check ok to proceed */
447 	smtpmessage("DATA", m, mci);
448 	SmtpPhase = mci->mci_phase = "client DATA 354";
449 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
450 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
451 	if (r < 0 || REPLYTYPE(r) == 4)
452 	{
453 		smtpquit(m, mci, e);
454 		return (EX_TEMPFAIL);
455 	}
456 	else if (r == 554)
457 	{
458 		smtprset(m, mci, e);
459 		return (EX_UNAVAILABLE);
460 	}
461 	else if (r != 354)
462 	{
463 #ifdef LOG
464 		if (LogLevel > 1)
465 		{
466 			syslog(LOG_CRIT, "%s: SMTP DATA-1 protocol error: %s",
467 				e->e_id, SmtpReplyBuffer);
468 		}
469 #endif
470 		smtprset(m, mci, e);
471 		return (EX_PROTOCOL);
472 	}
473 
474 	/*
475 	**  Set timeout around data writes.  Make it at least large
476 	**  enough for DNS timeouts on all recipients plus some fudge
477 	**  factor.  The main thing is that it should not be infinite.
478 	*/
479 
480 	if (setjmp(CtxDataTimeout) != 0)
481 	{
482 		mci->mci_errno = errno;
483 		mci->mci_exitstat = EX_TEMPFAIL;
484 		mci->mci_state = MCIS_ERROR;
485 		syserr("451 timeout writing message to %s", mci->mci_host);
486 		smtpquit(m, mci, e);
487 		return EX_TEMPFAIL;
488 	}
489 
490 	timeout = e->e_msgsize / 16;
491 	if (timeout < (time_t) 60)
492 		timeout = (time_t) 60;
493 	timeout += e->e_nrcpts * 90;
494 	ev = setevent(timeout, datatimeout, 0);
495 
496 	/* now output the actual message */
497 	(*e->e_puthdr)(mci->mci_out, m, e);
498 	putline("\n", mci->mci_out, m);
499 	(*e->e_putbody)(mci->mci_out, m, e, NULL);
500 
501 	clrevent(ev);
502 
503 	if (ferror(mci->mci_out))
504 	{
505 		/* error during processing -- don't send the dot */
506 		mci->mci_errno = EIO;
507 		mci->mci_exitstat = EX_IOERR;
508 		mci->mci_state = MCIS_ERROR;
509 		smtpquit(m, mci, e);
510 		return EX_IOERR;
511 	}
512 
513 	/* terminate the message */
514 	fprintf(mci->mci_out, ".%s", m->m_eol);
515 	if (TrafficLogFile != NULL)
516 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
517 	if (Verbose)
518 		nmessage(">>> .");
519 
520 	/* check for the results of the transaction */
521 	SmtpPhase = mci->mci_phase = "client DATA 250";
522 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
523 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
524 	if (r < 0)
525 	{
526 		smtpquit(m, mci, e);
527 		return (EX_TEMPFAIL);
528 	}
529 	mci->mci_state = MCIS_OPEN;
530 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
531 	if (REPLYTYPE(r) == 4)
532 		return (EX_TEMPFAIL);
533 	else if (r == 250)
534 		return (EX_OK);
535 	else if (r == 552 || r == 554)
536 		return (EX_UNAVAILABLE);
537 #ifdef LOG
538 	if (LogLevel > 1)
539 	{
540 		syslog(LOG_CRIT, "%s: SMTP DATA-2 protocol error: %s",
541 			e->e_id, SmtpReplyBuffer);
542 	}
543 #endif
544 	return (EX_PROTOCOL);
545 }
546 
547 
548 static int
549 datatimeout()
550 {
551 	longjmp(CtxDataTimeout, 1);
552 }
553 /*
554 **  SMTPQUIT -- close the SMTP connection.
555 **
556 **	Parameters:
557 **		m -- a pointer to the mailer.
558 **
559 **	Returns:
560 **		none.
561 **
562 **	Side Effects:
563 **		sends the final protocol and closes the connection.
564 */
565 
566 smtpquit(m, mci, e)
567 	register MAILER *m;
568 	register MCI *mci;
569 	ENVELOPE *e;
570 {
571 	int i;
572 
573 	/* send the quit message if we haven't gotten I/O error */
574 	if (mci->mci_state != MCIS_ERROR)
575 	{
576 		SmtpPhase = "client QUIT";
577 		smtpmessage("QUIT", m, mci);
578 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
579 		if (mci->mci_state == MCIS_CLOSED)
580 			return;
581 	}
582 
583 	/* now actually close the connection and pick up the zombie */
584 	i = endmailer(mci, e, m->m_argv);
585 	if (i != EX_OK)
586 		syserr("451 smtpquit %s: stat %d", m->m_argv[0], i);
587 }
588 /*
589 **  SMTPRSET -- send a RSET (reset) command
590 */
591 
592 smtprset(m, mci, e)
593 	register MAILER *m;
594 	register MCI *mci;
595 	ENVELOPE *e;
596 {
597 	int r;
598 
599 	SmtpPhase = "client RSET";
600 	smtpmessage("RSET", m, mci);
601 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
602 	if (r < 0)
603 		mci->mci_state = MCIS_ERROR;
604 	else if (REPLYTYPE(r) == 2)
605 	{
606 		mci->mci_state = MCIS_OPEN;
607 		return;
608 	}
609 	smtpquit(m, mci, e);
610 }
611 /*
612 **  SMTPPROBE -- check the connection state
613 */
614 
615 smtpprobe(mci)
616 	register MCI *mci;
617 {
618 	int r;
619 	MAILER *m = mci->mci_mailer;
620 	extern ENVELOPE BlankEnvelope;
621 	ENVELOPE *e = &BlankEnvelope;
622 
623 	SmtpPhase = "client probe";
624 	smtpmessage("RSET", m, mci);
625 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
626 	if (r < 0 || REPLYTYPE(r) != 2)
627 		smtpquit(m, mci, e);
628 	return r;
629 }
630 /*
631 **  REPLY -- read arpanet reply
632 **
633 **	Parameters:
634 **		m -- the mailer we are reading the reply from.
635 **		mci -- the mailer connection info structure.
636 **		e -- the current envelope.
637 **		timeout -- the timeout for reads.
638 **		pfunc -- processing function for second and subsequent
639 **			lines of response -- if null, no special
640 **			processing is done.
641 **
642 **	Returns:
643 **		reply code it reads.
644 **
645 **	Side Effects:
646 **		flushes the mail file.
647 */
648 
649 reply(m, mci, e, timeout, pfunc)
650 	MAILER *m;
651 	MCI *mci;
652 	ENVELOPE *e;
653 	time_t timeout;
654 	void (*pfunc)();
655 {
656 	register char *bufp;
657 	register int r;
658 	bool firstline = TRUE;
659 	char junkbuf[MAXLINE];
660 
661 	if (mci->mci_out != NULL)
662 		(void) fflush(mci->mci_out);
663 
664 	if (tTd(18, 1))
665 		printf("reply\n");
666 
667 	/*
668 	**  Read the input line, being careful not to hang.
669 	*/
670 
671 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
672 	{
673 		register char *p;
674 		extern time_t curtime();
675 
676 		/* actually do the read */
677 		if (e->e_xfp != NULL)
678 			(void) fflush(e->e_xfp);	/* for debugging */
679 
680 		/* if we are in the process of closing just give the code */
681 		if (mci->mci_state == MCIS_CLOSED)
682 			return (SMTPCLOSING);
683 
684 		if (mci->mci_out != NULL)
685 			fflush(mci->mci_out);
686 
687 		/* get the line from the other side */
688 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
689 		mci->mci_lastuse = curtime();
690 
691 		if (p == NULL)
692 		{
693 			bool oldholderrs;
694 			extern char MsgBuf[];		/* err.c */
695 
696 			/* if the remote end closed early, fake an error */
697 			if (errno == 0)
698 # ifdef ECONNRESET
699 				errno = ECONNRESET;
700 # else /* ECONNRESET */
701 				errno = EPIPE;
702 # endif /* ECONNRESET */
703 
704 			mci->mci_errno = errno;
705 			mci->mci_exitstat = EX_TEMPFAIL;
706 			oldholderrs = HoldErrs;
707 			HoldErrs = TRUE;
708 			usrerr("451 reply: read error from %s", mci->mci_host);
709 
710 			/* if debugging, pause so we can see state */
711 			if (tTd(18, 100))
712 				pause();
713 			mci->mci_state = MCIS_ERROR;
714 			smtpquit(m, mci, e);
715 #ifdef XDEBUG
716 			{
717 				char wbuf[MAXLINE];
718 				char *p = wbuf;
719 				if (e->e_to != NULL)
720 				{
721 					sprintf(p, "%s... ", e->e_to);
722 					p += strlen(p);
723 				}
724 				sprintf(p, "reply(%s) during %s",
725 					mci->mci_host, SmtpPhase);
726 				checkfd012(wbuf);
727 			}
728 #endif
729 			HoldErrs = oldholderrs;
730 			return (-1);
731 		}
732 		fixcrlf(bufp, TRUE);
733 
734 		/* EHLO failure is not a real error */
735 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
736 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
737 		{
738 			/* serious error -- log the previous command */
739 			if (SmtpNeedIntro)
740 			{
741 				/* inform user who we are chatting with */
742 				fprintf(CurEnv->e_xfp,
743 					"... while talking to %s:\n",
744 					CurHostName);
745 				SmtpNeedIntro = FALSE;
746 			}
747 			if (SmtpMsgBuffer[0] != '\0')
748 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
749 			SmtpMsgBuffer[0] = '\0';
750 
751 			/* now log the message as from the other side */
752 			fprintf(e->e_xfp, "<<< %s\n", bufp);
753 		}
754 
755 		/* display the input for verbose mode */
756 		if (Verbose)
757 			nmessage("050 %s", bufp);
758 
759 		/* process the line */
760 		if (pfunc != NULL && !firstline)
761 			(*pfunc)(bufp, m, mci, e);
762 
763 		firstline = FALSE;
764 
765 		/* if continuation is required, we can go on */
766 		if (bufp[3] == '-')
767 			continue;
768 
769 		/* ignore improperly formated input */
770 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
771 			continue;
772 
773 		/* decode the reply code */
774 		r = atoi(bufp);
775 
776 		/* extra semantics: 0xx codes are "informational" */
777 		if (r >= 100)
778 			break;
779 	}
780 
781 	/*
782 	**  Now look at SmtpReplyBuffer -- only care about the first
783 	**  line of the response from here on out.
784 	*/
785 
786 	/* save temporary failure messages for posterity */
787 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
788 		(void) strcpy(SmtpError, SmtpReplyBuffer);
789 
790 	/* reply code 421 is "Service Shutting Down" */
791 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
792 	{
793 		/* send the quit protocol */
794 		mci->mci_state = MCIS_SSD;
795 		smtpquit(m, mci, e);
796 	}
797 
798 	return (r);
799 }
800 /*
801 **  SMTPMESSAGE -- send message to server
802 **
803 **	Parameters:
804 **		f -- format
805 **		m -- the mailer to control formatting.
806 **		a, b, c -- parameters
807 **
808 **	Returns:
809 **		none.
810 **
811 **	Side Effects:
812 **		writes message to mci->mci_out.
813 */
814 
815 /*VARARGS1*/
816 #ifdef __STDC__
817 smtpmessage(char *f, MAILER *m, MCI *mci, ...)
818 #else
819 smtpmessage(f, m, mci, va_alist)
820 	char *f;
821 	MAILER *m;
822 	MCI *mci;
823 	va_dcl
824 #endif
825 {
826 	VA_LOCAL_DECL
827 
828 	VA_START(mci);
829 	(void) vsprintf(SmtpMsgBuffer, f, ap);
830 	VA_END;
831 
832 	if (tTd(18, 1) || Verbose)
833 		nmessage(">>> %s", SmtpMsgBuffer);
834 	if (TrafficLogFile != NULL)
835 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
836 	if (mci->mci_out != NULL)
837 	{
838 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
839 			m == NULL ? "\r\n" : m->m_eol);
840 	}
841 	else if (tTd(18, 1))
842 	{
843 		printf("smtpmessage: NULL mci_out\n");
844 	}
845 }
846 
847 # endif /* SMTP */
848