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.40 (Berkeley) 03/25/95 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)usersmtp.c	8.40 (Berkeley) 03/25/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) < 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-3") == 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 		 (e->e_bodytype != NULL &&
354 		  strcasecmp(e->e_bodytype, "7bit") == 0))
355 	{
356 		/* just pass it through */
357 	}
358 	else if (bitset(MM_CVTMIME, MimeMode) &&
359 		 (e->e_bodytype != NULL || !bitset(MM_PASS8BIT, MimeMode)))
360 	{
361 		/* must convert from 8bit MIME format to 7bit encoded */
362 		mci->mci_flags |= MCIF_CVT8TO7;
363 	}
364 	else if (!bitset(MM_PASS8BIT, MimeMode))
365 	{
366 		/* cannot just send a 8-bit version */
367 		usrerr("%s does not support 8BITMIME", mci->mci_host);
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 || REPLYTYPE(r) == 4)
426 	{
427 		mci->mci_exitstat = EX_TEMPFAIL;
428 		mci->mci_errno = errno;
429 		smtpquit(m, mci, e);
430 		return EX_TEMPFAIL;
431 	}
432 	else if (r == 250)
433 	{
434 		return EX_OK;
435 	}
436 	else if (r == 501 || r == 553)
437 	{
438 		/* syntax error in arguments */
439 		smtpquit(m, mci, e);
440 		return EX_DATAERR;
441 	}
442 	else if (r == 552)
443 	{
444 		/* signal service unavailable */
445 		smtpquit(m, mci, e);
446 		return EX_UNAVAILABLE;
447 	}
448 
449 #ifdef LOG
450 	if (LogLevel > 1)
451 	{
452 		syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s",
453 			e->e_id, mci->mci_host, SmtpReplyBuffer);
454 	}
455 #endif
456 
457 	/* protocol error -- close up */
458 	smtpquit(m, mci, e);
459 	return EX_PROTOCOL;
460 }
461 /*
462 **  SMTPRCPT -- designate recipient.
463 **
464 **	Parameters:
465 **		to -- address of recipient.
466 **		m -- the mailer we are sending to.
467 **		mci -- the connection info for this transaction.
468 **		e -- the envelope for this transaction.
469 **
470 **	Returns:
471 **		exit status corresponding to recipient status.
472 **
473 **	Side Effects:
474 **		Sends the mail via SMTP.
475 */
476 
477 smtprcpt(to, m, mci, e)
478 	ADDRESS *to;
479 	register MAILER *m;
480 	MCI *mci;
481 	ENVELOPE *e;
482 {
483 	register int r;
484 	char optbuf[MAXLINE];
485 
486 	strcpy(optbuf, "");
487 	if (bitset(MCIF_DSN, mci->mci_flags))
488 	{
489 		/* NOTIFY= parameter */
490 		if (bitset(QHASNOTIFY, to->q_flags))
491 		{
492 			bool firstone = TRUE;
493 
494 			strcat(optbuf, " NOTIFY=");
495 			if (bitset(QPINGONSUCCESS, to->q_flags))
496 			{
497 				strcat(optbuf, "SUCCESS");
498 				firstone = FALSE;
499 			}
500 			if (bitset(QPINGONFAILURE, to->q_flags))
501 			{
502 				if (!firstone)
503 					strcat(optbuf, ",");
504 				strcat(optbuf, "FAILURE");
505 				firstone = FALSE;
506 			}
507 			if (bitset(QPINGONDELAY, to->q_flags))
508 			{
509 				if (!firstone)
510 					strcat(optbuf, ",");
511 				strcat(optbuf, "DELAY");
512 				firstone = FALSE;
513 			}
514 			if (firstone)
515 				strcat(optbuf, "NEVER");
516 		}
517 
518 		/* ORCPT= parameter */
519 		if (to->q_orcpt != NULL)
520 		{
521 			strcat(optbuf, " ORCPT=");
522 			strcat(optbuf, to->q_orcpt);
523 		}
524 	}
525 
526 	smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
527 
528 	SmtpPhase = mci->mci_phase = "client RCPT";
529 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
530 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
531 	setstatus(to, SmtpReplyBuffer);
532 	if (r < 0 || REPLYTYPE(r) == 4)
533 		return (EX_TEMPFAIL);
534 	else if (REPLYTYPE(r) == 2)
535 		return (EX_OK);
536 	else if (r == 550 || r == 551 || r == 553)
537 		return (EX_NOUSER);
538 	else if (r == 552 || r == 554)
539 		return (EX_UNAVAILABLE);
540 
541 #ifdef LOG
542 	if (LogLevel > 1)
543 	{
544 		syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s",
545 			e->e_id, mci->mci_host, SmtpReplyBuffer);
546 	}
547 #endif
548 
549 	return (EX_PROTOCOL);
550 }
551 /*
552 **  SMTPDATA -- send the data and clean up the transaction.
553 **
554 **	Parameters:
555 **		m -- mailer being sent to.
556 **		e -- the envelope for this message.
557 **
558 **	Returns:
559 **		exit status corresponding to DATA command.
560 **
561 **	Side Effects:
562 **		none.
563 */
564 
565 static jmp_buf	CtxDataTimeout;
566 static void	datatimeout();
567 
568 smtpdata(m, mci, e)
569 	struct mailer *m;
570 	register MCI *mci;
571 	register ENVELOPE *e;
572 {
573 	register int r;
574 	register EVENT *ev;
575 	time_t timeout;
576 
577 	/*
578 	**  Send the data.
579 	**	First send the command and check that it is ok.
580 	**	Then send the data.
581 	**	Follow it up with a dot to terminate.
582 	**	Finally get the results of the transaction.
583 	*/
584 
585 	/* send the command and check ok to proceed */
586 	smtpmessage("DATA", m, mci);
587 	SmtpPhase = mci->mci_phase = "client DATA 354";
588 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
589 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
590 	if (r < 0 || REPLYTYPE(r) == 4)
591 	{
592 		smtpquit(m, mci, e);
593 		return (EX_TEMPFAIL);
594 	}
595 	else if (r == 554)
596 	{
597 		smtprset(m, mci, e);
598 		return (EX_UNAVAILABLE);
599 	}
600 	else if (r != 354)
601 	{
602 #ifdef LOG
603 		if (LogLevel > 1)
604 		{
605 			syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s",
606 				e->e_id, mci->mci_host, SmtpReplyBuffer);
607 		}
608 #endif
609 		smtprset(m, mci, e);
610 		return (EX_PROTOCOL);
611 	}
612 
613 	/*
614 	**  Set timeout around data writes.  Make it at least large
615 	**  enough for DNS timeouts on all recipients plus some fudge
616 	**  factor.  The main thing is that it should not be infinite.
617 	*/
618 
619 	if (setjmp(CtxDataTimeout) != 0)
620 	{
621 		mci->mci_errno = errno;
622 		mci->mci_exitstat = EX_TEMPFAIL;
623 		mci->mci_state = MCIS_ERROR;
624 		syserr("451 timeout writing message to %s", mci->mci_host);
625 		smtpquit(m, mci, e);
626 		return EX_TEMPFAIL;
627 	}
628 
629 	timeout = e->e_msgsize / 16;
630 	if (timeout < (time_t) 60)
631 		timeout = (time_t) 60;
632 	timeout += e->e_nrcpts * 90;
633 	ev = setevent(timeout, datatimeout, 0);
634 
635 	/*
636 	**  Output the actual message.
637 	*/
638 
639 	(*e->e_puthdr)(mci, e->e_header, e);
640 	(*e->e_putbody)(mci, e, NULL);
641 
642 	/*
643 	**  Cleanup after sending message.
644 	*/
645 
646 	clrevent(ev);
647 
648 	if (ferror(mci->mci_out))
649 	{
650 		/* error during processing -- don't send the dot */
651 		mci->mci_errno = EIO;
652 		mci->mci_exitstat = EX_IOERR;
653 		mci->mci_state = MCIS_ERROR;
654 		smtpquit(m, mci, e);
655 		return EX_IOERR;
656 	}
657 
658 	/* terminate the message */
659 	fprintf(mci->mci_out, ".%s", m->m_eol);
660 	if (TrafficLogFile != NULL)
661 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
662 	if (Verbose)
663 		nmessage(">>> .");
664 
665 	/* check for the results of the transaction */
666 	SmtpPhase = mci->mci_phase = "client DATA 250";
667 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
668 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
669 	if (r < 0)
670 	{
671 		smtpquit(m, mci, e);
672 		return (EX_TEMPFAIL);
673 	}
674 	mci->mci_state = MCIS_OPEN;
675 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
676 	if (REPLYTYPE(r) == 4)
677 		return (EX_TEMPFAIL);
678 	else if (r == 250)
679 		return (EX_OK);
680 	else if (r == 552 || r == 554)
681 		return (EX_UNAVAILABLE);
682 #ifdef LOG
683 	if (LogLevel > 1)
684 	{
685 		syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s",
686 			e->e_id, mci->mci_host, SmtpReplyBuffer);
687 	}
688 #endif
689 	return (EX_PROTOCOL);
690 }
691 
692 
693 static void
694 datatimeout()
695 {
696 	longjmp(CtxDataTimeout, 1);
697 }
698 /*
699 **  SMTPQUIT -- close the SMTP connection.
700 **
701 **	Parameters:
702 **		m -- a pointer to the mailer.
703 **
704 **	Returns:
705 **		none.
706 **
707 **	Side Effects:
708 **		sends the final protocol and closes the connection.
709 */
710 
711 smtpquit(m, mci, e)
712 	register MAILER *m;
713 	register MCI *mci;
714 	ENVELOPE *e;
715 {
716 	bool oldSuprErrs = SuprErrs;
717 
718 	/*
719 	**	Suppress errors here -- we may be processing a different
720 	**	job when we do the quit connection, and we don't want the
721 	**	new job to be penalized for something that isn't it's
722 	**	problem.
723 	*/
724 
725 	SuprErrs = TRUE;
726 
727 	/* send the quit message if we haven't gotten I/O error */
728 	if (mci->mci_state != MCIS_ERROR)
729 	{
730 		SmtpPhase = "client QUIT";
731 		smtpmessage("QUIT", m, mci);
732 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
733 		SuprErrs = oldSuprErrs;
734 		if (mci->mci_state == MCIS_CLOSED)
735 		{
736 			SuprErrs = oldSuprErrs;
737 			return;
738 		}
739 	}
740 
741 	/* now actually close the connection and pick up the zombie */
742 	(void) endmailer(mci, e, NULL);
743 
744 	SuprErrs = oldSuprErrs;
745 }
746 /*
747 **  SMTPRSET -- send a RSET (reset) command
748 */
749 
750 smtprset(m, mci, e)
751 	register MAILER *m;
752 	register MCI *mci;
753 	ENVELOPE *e;
754 {
755 	int r;
756 
757 	SmtpPhase = "client RSET";
758 	smtpmessage("RSET", m, mci);
759 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
760 	if (r < 0)
761 		mci->mci_state = MCIS_ERROR;
762 	else if (REPLYTYPE(r) == 2)
763 	{
764 		mci->mci_state = MCIS_OPEN;
765 		return;
766 	}
767 	smtpquit(m, mci, e);
768 }
769 /*
770 **  SMTPPROBE -- check the connection state
771 */
772 
773 smtpprobe(mci)
774 	register MCI *mci;
775 {
776 	int r;
777 	MAILER *m = mci->mci_mailer;
778 	extern ENVELOPE BlankEnvelope;
779 	ENVELOPE *e = &BlankEnvelope;
780 
781 	SmtpPhase = "client probe";
782 	smtpmessage("RSET", m, mci);
783 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
784 	if (r < 0 || REPLYTYPE(r) != 2)
785 		smtpquit(m, mci, e);
786 	return r;
787 }
788 /*
789 **  REPLY -- read arpanet reply
790 **
791 **	Parameters:
792 **		m -- the mailer we are reading the reply from.
793 **		mci -- the mailer connection info structure.
794 **		e -- the current envelope.
795 **		timeout -- the timeout for reads.
796 **		pfunc -- processing function for second and subsequent
797 **			lines of response -- if null, no special
798 **			processing is done.
799 **
800 **	Returns:
801 **		reply code it reads.
802 **
803 **	Side Effects:
804 **		flushes the mail file.
805 */
806 
807 reply(m, mci, e, timeout, pfunc)
808 	MAILER *m;
809 	MCI *mci;
810 	ENVELOPE *e;
811 	time_t timeout;
812 	void (*pfunc)();
813 {
814 	register char *bufp;
815 	register int r;
816 	bool firstline = TRUE;
817 	char junkbuf[MAXLINE];
818 
819 	if (mci->mci_out != NULL)
820 		(void) fflush(mci->mci_out);
821 
822 	if (tTd(18, 1))
823 		printf("reply\n");
824 
825 	/*
826 	**  Read the input line, being careful not to hang.
827 	*/
828 
829 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
830 	{
831 		register char *p;
832 		extern time_t curtime();
833 
834 		/* actually do the read */
835 		if (e->e_xfp != NULL)
836 			(void) fflush(e->e_xfp);	/* for debugging */
837 
838 		/* if we are in the process of closing just give the code */
839 		if (mci->mci_state == MCIS_CLOSED)
840 			return (SMTPCLOSING);
841 
842 		if (mci->mci_out != NULL)
843 			fflush(mci->mci_out);
844 
845 		/* get the line from the other side */
846 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
847 		mci->mci_lastuse = curtime();
848 
849 		if (p == NULL)
850 		{
851 			bool oldholderrs;
852 			extern char MsgBuf[];		/* err.c */
853 
854 			/* if the remote end closed early, fake an error */
855 			if (errno == 0)
856 # ifdef ECONNRESET
857 				errno = ECONNRESET;
858 # else /* ECONNRESET */
859 				errno = EPIPE;
860 # endif /* ECONNRESET */
861 
862 			mci->mci_errno = errno;
863 			mci->mci_exitstat = EX_TEMPFAIL;
864 			oldholderrs = HoldErrs;
865 			HoldErrs = TRUE;
866 			usrerr("451 reply: read error from %s", mci->mci_host);
867 
868 			/* if debugging, pause so we can see state */
869 			if (tTd(18, 100))
870 				pause();
871 			mci->mci_state = MCIS_ERROR;
872 			smtpquit(m, mci, e);
873 #ifdef XDEBUG
874 			{
875 				char wbuf[MAXLINE];
876 				char *p = wbuf;
877 				if (e->e_to != NULL)
878 				{
879 					sprintf(p, "%s... ", e->e_to);
880 					p += strlen(p);
881 				}
882 				sprintf(p, "reply(%s) during %s",
883 					mci->mci_host, SmtpPhase);
884 				checkfd012(wbuf);
885 			}
886 #endif
887 			HoldErrs = oldholderrs;
888 			return (-1);
889 		}
890 		fixcrlf(bufp, TRUE);
891 
892 		/* EHLO failure is not a real error */
893 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
894 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
895 		{
896 			/* serious error -- log the previous command */
897 			if (SmtpNeedIntro)
898 			{
899 				/* inform user who we are chatting with */
900 				fprintf(CurEnv->e_xfp,
901 					"... while talking to %s:\n",
902 					CurHostName);
903 				SmtpNeedIntro = FALSE;
904 			}
905 			if (SmtpMsgBuffer[0] != '\0')
906 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
907 			SmtpMsgBuffer[0] = '\0';
908 
909 			/* now log the message as from the other side */
910 			fprintf(e->e_xfp, "<<< %s\n", bufp);
911 		}
912 
913 		/* display the input for verbose mode */
914 		if (Verbose)
915 			nmessage("050 %s", bufp);
916 
917 		/* process the line */
918 		if (pfunc != NULL)
919 			(*pfunc)(bufp, firstline, m, mci, e);
920 
921 		firstline = FALSE;
922 
923 		/* if continuation is required, we can go on */
924 		if (bufp[3] == '-')
925 			continue;
926 
927 		/* ignore improperly formated input */
928 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
929 			continue;
930 
931 		/* decode the reply code */
932 		r = atoi(bufp);
933 
934 		/* extra semantics: 0xx codes are "informational" */
935 		if (r >= 100)
936 			break;
937 	}
938 
939 	/*
940 	**  Now look at SmtpReplyBuffer -- only care about the first
941 	**  line of the response from here on out.
942 	*/
943 
944 	/* save temporary failure messages for posterity */
945 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
946 		(void) strcpy(SmtpError, SmtpReplyBuffer);
947 
948 	/* reply code 421 is "Service Shutting Down" */
949 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
950 	{
951 		/* send the quit protocol */
952 		mci->mci_state = MCIS_SSD;
953 		smtpquit(m, mci, e);
954 	}
955 
956 	return (r);
957 }
958 /*
959 **  SMTPMESSAGE -- send message to server
960 **
961 **	Parameters:
962 **		f -- format
963 **		m -- the mailer to control formatting.
964 **		a, b, c -- parameters
965 **
966 **	Returns:
967 **		none.
968 **
969 **	Side Effects:
970 **		writes message to mci->mci_out.
971 */
972 
973 /*VARARGS1*/
974 #ifdef __STDC__
975 smtpmessage(char *f, MAILER *m, MCI *mci, ...)
976 #else
977 smtpmessage(f, m, mci, va_alist)
978 	char *f;
979 	MAILER *m;
980 	MCI *mci;
981 	va_dcl
982 #endif
983 {
984 	VA_LOCAL_DECL
985 
986 	VA_START(mci);
987 	(void) vsprintf(SmtpMsgBuffer, f, ap);
988 	VA_END;
989 
990 	if (tTd(18, 1) || Verbose)
991 		nmessage(">>> %s", SmtpMsgBuffer);
992 	if (TrafficLogFile != NULL)
993 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
994 	if (mci->mci_out != NULL)
995 	{
996 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
997 			m == NULL ? "\r\n" : m->m_eol);
998 	}
999 	else if (tTd(18, 1))
1000 	{
1001 		printf("smtpmessage: NULL mci_out\n");
1002 	}
1003 }
1004 
1005 # endif /* SMTP */
1006