xref: /illumos-gate/usr/src/cmd/sendmail/src/err.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sendmail.h>
157c478bd9Sstevel@tonic-gate 
16*058561cbSjbeck SM_RCSID("@(#)$Id: err.c,v 8.196 2006/11/10 23:14:08 ca Exp $")
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if LDAPMAP
197c478bd9Sstevel@tonic-gate # include <lber.h>
207c478bd9Sstevel@tonic-gate # include <ldap.h>			/* for LDAP error codes */
217c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate static void	putoutmsg __P((char *, bool, bool));
247c478bd9Sstevel@tonic-gate static void	puterrmsg __P((char *));
257c478bd9Sstevel@tonic-gate static char	*fmtmsg __P((char *, const char *, const char *, const char *,
267c478bd9Sstevel@tonic-gate 			     int, const char *, va_list));
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate **  FATAL_ERROR -- handle a fatal exception
307c478bd9Sstevel@tonic-gate **
317c478bd9Sstevel@tonic-gate **	This function is installed as the default exception handler
327c478bd9Sstevel@tonic-gate **	in the main sendmail process, and in all child processes
337c478bd9Sstevel@tonic-gate **	that we create.  Its job is to handle exceptions that are not
347c478bd9Sstevel@tonic-gate **	handled at a lower level.
357c478bd9Sstevel@tonic-gate **
367c478bd9Sstevel@tonic-gate **	The theory is that unhandled exceptions will be 'fatal' class
377c478bd9Sstevel@tonic-gate **	exceptions (with an "F:" prefix), such as the out-of-memory
387c478bd9Sstevel@tonic-gate **	exception "F:sm.heap".  As such, they are handled by exiting
397c478bd9Sstevel@tonic-gate **	the process in exactly the same way that xalloc() in Sendmail 8.10
407c478bd9Sstevel@tonic-gate **	exits the process when it fails due to lack of memory:
417c478bd9Sstevel@tonic-gate **	we call syserr with a message beginning with "!".
427c478bd9Sstevel@tonic-gate **
437c478bd9Sstevel@tonic-gate **	Parameters:
447c478bd9Sstevel@tonic-gate **		exc -- exception which is terminating this process
457c478bd9Sstevel@tonic-gate **
467c478bd9Sstevel@tonic-gate **	Returns:
477c478bd9Sstevel@tonic-gate **		none
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate void
fatal_error(exc)517c478bd9Sstevel@tonic-gate fatal_error(exc)
527c478bd9Sstevel@tonic-gate 	SM_EXC_T *exc;
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	static char buf[256];
557c478bd9Sstevel@tonic-gate 	SM_FILE_T f;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/*
587c478bd9Sstevel@tonic-gate 	**  This function may be called when the heap is exhausted.
597c478bd9Sstevel@tonic-gate 	**  The following code writes the message for 'exc' into our
607c478bd9Sstevel@tonic-gate 	**  static buffer without allocating memory or raising exceptions.
617c478bd9Sstevel@tonic-gate 	*/
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	sm_strio_init(&f, buf, sizeof(buf));
647c478bd9Sstevel@tonic-gate 	sm_exc_write(exc, &f);
657c478bd9Sstevel@tonic-gate 	(void) sm_io_flush(&f, SM_TIME_DEFAULT);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/*
687c478bd9Sstevel@tonic-gate 	**  Terminate the process after logging an error and cleaning up.
697c478bd9Sstevel@tonic-gate 	**  Problems:
707c478bd9Sstevel@tonic-gate 	**  - syserr decides what class of error this is by looking at errno.
717c478bd9Sstevel@tonic-gate 	**    That's no good; we should look at the exc structure.
727c478bd9Sstevel@tonic-gate 	**  - The cleanup code should be moved out of syserr
737c478bd9Sstevel@tonic-gate 	**    and into individual exception handlers
747c478bd9Sstevel@tonic-gate 	**    that are part of the module they clean up after.
757c478bd9Sstevel@tonic-gate 	*/
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	errno = ENOMEM;
787c478bd9Sstevel@tonic-gate 	syserr("!%s", buf);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate **  SYSERR -- Print error message.
837c478bd9Sstevel@tonic-gate **
847c478bd9Sstevel@tonic-gate **	Prints an error message via sm_io_printf to the diagnostic output.
857c478bd9Sstevel@tonic-gate **
867c478bd9Sstevel@tonic-gate **	If the first character of the syserr message is `!' it will
877c478bd9Sstevel@tonic-gate **	log this as an ALERT message and exit immediately.  This can
887c478bd9Sstevel@tonic-gate **	leave queue files in an indeterminate state, so it should not
897c478bd9Sstevel@tonic-gate **	be used lightly.
907c478bd9Sstevel@tonic-gate **
917c478bd9Sstevel@tonic-gate **	If the first character of the syserr message is '!' or '@'
927c478bd9Sstevel@tonic-gate **	then syserr knows that the process is about to be terminated,
937c478bd9Sstevel@tonic-gate **	so the SMTP reply code defaults to 421.  Otherwise, the
947c478bd9Sstevel@tonic-gate **	reply code defaults to 451 or 554, depending on errno.
957c478bd9Sstevel@tonic-gate **
967c478bd9Sstevel@tonic-gate **	Parameters:
977c478bd9Sstevel@tonic-gate **		fmt -- the format string.  An optional '!' or '@',
987c478bd9Sstevel@tonic-gate **			followed by an optional three-digit SMTP
997c478bd9Sstevel@tonic-gate **			reply code, followed by message text.
1007c478bd9Sstevel@tonic-gate **		(others) -- parameters
1017c478bd9Sstevel@tonic-gate **
1027c478bd9Sstevel@tonic-gate **	Returns:
1037c478bd9Sstevel@tonic-gate **		none
1047c478bd9Sstevel@tonic-gate **		Raises E:mta.quickabort if QuickAbort is set.
1057c478bd9Sstevel@tonic-gate **
1067c478bd9Sstevel@tonic-gate **	Side Effects:
1077c478bd9Sstevel@tonic-gate **		increments Errors.
1087c478bd9Sstevel@tonic-gate **		sets ExitStat.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate char		MsgBuf[BUFSIZ*2];	/* text of most recent message */
112*058561cbSjbeck static char	HeldMessageBuf[sizeof(MsgBuf)];	/* for held messages */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #if NAMED_BIND && !defined(NO_DATA)
1157c478bd9Sstevel@tonic-gate # define NO_DATA	NO_ADDRESS
1167c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND && !defined(NO_DATA) */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate void
1197c478bd9Sstevel@tonic-gate /*VARARGS1*/
1207c478bd9Sstevel@tonic-gate #ifdef __STDC__
syserr(const char * fmt,...)1217c478bd9Sstevel@tonic-gate syserr(const char *fmt, ...)
1227c478bd9Sstevel@tonic-gate #else /* __STDC__ */
1237c478bd9Sstevel@tonic-gate syserr(fmt, va_alist)
1247c478bd9Sstevel@tonic-gate 	const char *fmt;
1257c478bd9Sstevel@tonic-gate 	va_dcl
1267c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	register char *p;
1297c478bd9Sstevel@tonic-gate 	int save_errno = errno;
1307c478bd9Sstevel@tonic-gate 	bool panic;
1317c478bd9Sstevel@tonic-gate 	bool exiting;
1327c478bd9Sstevel@tonic-gate 	char *user;
1337c478bd9Sstevel@tonic-gate 	char *enhsc;
1347c478bd9Sstevel@tonic-gate 	char *errtxt;
1357c478bd9Sstevel@tonic-gate 	struct passwd *pw;
1367c478bd9Sstevel@tonic-gate 	char ubuf[80];
1377c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	switch (*fmt)
1407c478bd9Sstevel@tonic-gate 	{
1417c478bd9Sstevel@tonic-gate 	  case '!':
1427c478bd9Sstevel@tonic-gate 		++fmt;
1437c478bd9Sstevel@tonic-gate 		panic = true;
1447c478bd9Sstevel@tonic-gate 		exiting = true;
1457c478bd9Sstevel@tonic-gate 		break;
1467c478bd9Sstevel@tonic-gate 	  case '@':
1477c478bd9Sstevel@tonic-gate 		++fmt;
1487c478bd9Sstevel@tonic-gate 		panic = false;
1497c478bd9Sstevel@tonic-gate 		exiting = true;
1507c478bd9Sstevel@tonic-gate 		break;
1517c478bd9Sstevel@tonic-gate 	  default:
1527c478bd9Sstevel@tonic-gate 		panic = false;
1537c478bd9Sstevel@tonic-gate 		exiting = false;
1547c478bd9Sstevel@tonic-gate 		break;
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/* format and output the error message */
1587c478bd9Sstevel@tonic-gate 	if (exiting)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		**  Since we are terminating the process,
1627c478bd9Sstevel@tonic-gate 		**  we are aborting the entire SMTP session,
1637c478bd9Sstevel@tonic-gate 		**  rather than just the current transaction.
1647c478bd9Sstevel@tonic-gate 		*/
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		p = "421";
1677c478bd9Sstevel@tonic-gate 		enhsc = "4.0.0";
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	else if (save_errno == 0)
1707c478bd9Sstevel@tonic-gate 	{
1717c478bd9Sstevel@tonic-gate 		p = "554";
1727c478bd9Sstevel@tonic-gate 		enhsc = "5.0.0";
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	else
1757c478bd9Sstevel@tonic-gate 	{
1767c478bd9Sstevel@tonic-gate 		p = "451";
1777c478bd9Sstevel@tonic-gate 		enhsc = "4.0.0";
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, fmt);
1807c478bd9Sstevel@tonic-gate 	errtxt = fmtmsg(MsgBuf, (char *) NULL, p, enhsc, save_errno, fmt, ap);
1817c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
1827c478bd9Sstevel@tonic-gate 	puterrmsg(MsgBuf);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* save this message for mailq printing */
1857c478bd9Sstevel@tonic-gate 	if (!panic && CurEnv != NULL)
1867c478bd9Sstevel@tonic-gate 	{
1877c478bd9Sstevel@tonic-gate 		char *nmsg = sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
1907c478bd9Sstevel@tonic-gate 			sm_free(CurEnv->e_message);
1917c478bd9Sstevel@tonic-gate 		CurEnv->e_message = nmsg;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* determine exit status if not already set */
1957c478bd9Sstevel@tonic-gate 	if (ExitStat == EX_OK)
1967c478bd9Sstevel@tonic-gate 	{
1977c478bd9Sstevel@tonic-gate 		if (save_errno == 0)
1987c478bd9Sstevel@tonic-gate 			ExitStat = EX_SOFTWARE;
1997c478bd9Sstevel@tonic-gate 		else
2007c478bd9Sstevel@tonic-gate 			ExitStat = EX_OSERR;
2017c478bd9Sstevel@tonic-gate 		if (tTd(54, 1))
2027c478bd9Sstevel@tonic-gate 			sm_dprintf("syserr: ExitStat = %d\n", ExitStat);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	pw = sm_getpwuid(RealUid);
2067c478bd9Sstevel@tonic-gate 	if (pw != NULL)
2077c478bd9Sstevel@tonic-gate 		user = pw->pw_name;
2087c478bd9Sstevel@tonic-gate 	else
2097c478bd9Sstevel@tonic-gate 	{
2107c478bd9Sstevel@tonic-gate 		user = ubuf;
211*058561cbSjbeck 		(void) sm_snprintf(ubuf, sizeof(ubuf), "UID%d", (int) RealUid);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (LogLevel > 0)
2157c478bd9Sstevel@tonic-gate 		sm_syslog(panic ? LOG_ALERT : LOG_CRIT,
2167c478bd9Sstevel@tonic-gate 			  CurEnv == NULL ? NOQID : CurEnv->e_id,
2177c478bd9Sstevel@tonic-gate 			  "SYSERR(%s): %.900s",
2187c478bd9Sstevel@tonic-gate 			  user, errtxt);
2197c478bd9Sstevel@tonic-gate 	switch (save_errno)
2207c478bd9Sstevel@tonic-gate 	{
2217c478bd9Sstevel@tonic-gate 	  case EBADF:
2227c478bd9Sstevel@tonic-gate 	  case ENFILE:
2237c478bd9Sstevel@tonic-gate 	  case EMFILE:
2247c478bd9Sstevel@tonic-gate 	  case ENOTTY:
2257c478bd9Sstevel@tonic-gate #ifdef EFBIG
2267c478bd9Sstevel@tonic-gate 	  case EFBIG:
2277c478bd9Sstevel@tonic-gate #endif /* EFBIG */
2287c478bd9Sstevel@tonic-gate #ifdef ESPIPE
2297c478bd9Sstevel@tonic-gate 	  case ESPIPE:
2307c478bd9Sstevel@tonic-gate #endif /* ESPIPE */
2317c478bd9Sstevel@tonic-gate #ifdef EPIPE
2327c478bd9Sstevel@tonic-gate 	  case EPIPE:
2337c478bd9Sstevel@tonic-gate #endif /* EPIPE */
2347c478bd9Sstevel@tonic-gate #ifdef ENOBUFS
2357c478bd9Sstevel@tonic-gate 	  case ENOBUFS:
2367c478bd9Sstevel@tonic-gate #endif /* ENOBUFS */
2377c478bd9Sstevel@tonic-gate #ifdef ESTALE
2387c478bd9Sstevel@tonic-gate 	  case ESTALE:
2397c478bd9Sstevel@tonic-gate #endif /* ESTALE */
2407c478bd9Sstevel@tonic-gate 		printopenfds(true);
2417c478bd9Sstevel@tonic-gate 		mci_dump_all(smioout, true);
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (panic)
2457c478bd9Sstevel@tonic-gate 	{
2467c478bd9Sstevel@tonic-gate #if XLA
2477c478bd9Sstevel@tonic-gate 		xla_all_end();
2487c478bd9Sstevel@tonic-gate #endif /* XLA */
2497c478bd9Sstevel@tonic-gate 		sync_queue_time();
2507c478bd9Sstevel@tonic-gate 		if (tTd(0, 1))
2517c478bd9Sstevel@tonic-gate 			abort();
2527c478bd9Sstevel@tonic-gate 		exit(EX_OSERR);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	errno = 0;
2557c478bd9Sstevel@tonic-gate 	if (QuickAbort)
2567c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&EtypeQuickAbort, 2);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate **  USRERR -- Signal user error.
2607c478bd9Sstevel@tonic-gate **
2617c478bd9Sstevel@tonic-gate **	This is much like syserr except it is for user errors.
2627c478bd9Sstevel@tonic-gate **
2637c478bd9Sstevel@tonic-gate **	Parameters:
2647c478bd9Sstevel@tonic-gate **		fmt -- the format string.  If it does not begin with
2657c478bd9Sstevel@tonic-gate **			a three-digit SMTP reply code, 550 is assumed.
2667c478bd9Sstevel@tonic-gate **		(others) -- sm_io_printf strings
2677c478bd9Sstevel@tonic-gate **
2687c478bd9Sstevel@tonic-gate **	Returns:
2697c478bd9Sstevel@tonic-gate **		none
2707c478bd9Sstevel@tonic-gate **		Raises E:mta.quickabort if QuickAbort is set.
2717c478bd9Sstevel@tonic-gate **
2727c478bd9Sstevel@tonic-gate **	Side Effects:
2737c478bd9Sstevel@tonic-gate **		increments Errors.
2747c478bd9Sstevel@tonic-gate */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*VARARGS1*/
2777c478bd9Sstevel@tonic-gate void
2787c478bd9Sstevel@tonic-gate #ifdef __STDC__
usrerr(const char * fmt,...)2797c478bd9Sstevel@tonic-gate usrerr(const char *fmt, ...)
2807c478bd9Sstevel@tonic-gate #else /* __STDC__ */
2817c478bd9Sstevel@tonic-gate usrerr(fmt, va_alist)
2827c478bd9Sstevel@tonic-gate 	const char *fmt;
2837c478bd9Sstevel@tonic-gate 	va_dcl
2847c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	char *enhsc;
2877c478bd9Sstevel@tonic-gate 	char *errtxt;
2887c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (fmt[0] == '5' || fmt[0] == '6')
2917c478bd9Sstevel@tonic-gate 		enhsc = "5.0.0";
2927c478bd9Sstevel@tonic-gate 	else if (fmt[0] == '4' || fmt[0] == '8')
2937c478bd9Sstevel@tonic-gate 		enhsc = "4.0.0";
2947c478bd9Sstevel@tonic-gate 	else if (fmt[0] == '2')
2957c478bd9Sstevel@tonic-gate 		enhsc = "2.0.0";
2967c478bd9Sstevel@tonic-gate 	else
2977c478bd9Sstevel@tonic-gate 		enhsc = NULL;
2987c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, fmt);
2997c478bd9Sstevel@tonic-gate 	errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "550", enhsc, 0, fmt, ap);
3007c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (SuprErrs)
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/* save this message for mailq printing */
3067c478bd9Sstevel@tonic-gate 	switch (MsgBuf[0])
3077c478bd9Sstevel@tonic-gate 	{
3087c478bd9Sstevel@tonic-gate 	  case '4':
3097c478bd9Sstevel@tonic-gate 	  case '8':
3107c478bd9Sstevel@tonic-gate 		if (CurEnv->e_message != NULL)
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	  case '5':
3167c478bd9Sstevel@tonic-gate 	  case '6':
3177c478bd9Sstevel@tonic-gate 		if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
3187c478bd9Sstevel@tonic-gate 			sm_free(CurEnv->e_message);
3197c478bd9Sstevel@tonic-gate 		if (MsgBuf[0] == '6')
3207c478bd9Sstevel@tonic-gate 		{
3217c478bd9Sstevel@tonic-gate 			char buf[MAXLINE];
3227c478bd9Sstevel@tonic-gate 
323*058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf),
3247c478bd9Sstevel@tonic-gate 					   "Postmaster warning: %.*s",
325*058561cbSjbeck 					   (int) sizeof(buf) - 22, errtxt);
3267c478bd9Sstevel@tonic-gate 			CurEnv->e_message =
3277c478bd9Sstevel@tonic-gate 				sm_rpool_strdup_x(CurEnv->e_rpool, buf);
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 		else
3307c478bd9Sstevel@tonic-gate 		{
3317c478bd9Sstevel@tonic-gate 			CurEnv->e_message =
3327c478bd9Sstevel@tonic-gate 				sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		break;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	puterrmsg(MsgBuf);
3387c478bd9Sstevel@tonic-gate 	if (LogLevel > 3 && LogUsrErrs)
3397c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_NOTICE, CurEnv->e_id, "%.900s", errtxt);
3407c478bd9Sstevel@tonic-gate 	if (QuickAbort)
3417c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&EtypeQuickAbort, 1);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate **  USRERRENH -- Signal user error.
3457c478bd9Sstevel@tonic-gate **
3467c478bd9Sstevel@tonic-gate **	Same as usrerr but with enhanced status code.
3477c478bd9Sstevel@tonic-gate **
3487c478bd9Sstevel@tonic-gate **	Parameters:
3497c478bd9Sstevel@tonic-gate **		enhsc -- the enhanced status code.
3507c478bd9Sstevel@tonic-gate **		fmt -- the format string.  If it does not begin with
3517c478bd9Sstevel@tonic-gate **			a three-digit SMTP reply code, 550 is assumed.
3527c478bd9Sstevel@tonic-gate **		(others) -- sm_io_printf strings
3537c478bd9Sstevel@tonic-gate **
3547c478bd9Sstevel@tonic-gate **	Returns:
3557c478bd9Sstevel@tonic-gate **		none
3567c478bd9Sstevel@tonic-gate **		Raises E:mta.quickabort if QuickAbort is set.
3577c478bd9Sstevel@tonic-gate **
3587c478bd9Sstevel@tonic-gate **	Side Effects:
3597c478bd9Sstevel@tonic-gate **		increments Errors.
3607c478bd9Sstevel@tonic-gate */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*VARARGS1*/
3637c478bd9Sstevel@tonic-gate void
3647c478bd9Sstevel@tonic-gate #ifdef __STDC__
usrerrenh(char * enhsc,const char * fmt,...)3657c478bd9Sstevel@tonic-gate usrerrenh(char *enhsc, const char *fmt, ...)
3667c478bd9Sstevel@tonic-gate #else /* __STDC__ */
3677c478bd9Sstevel@tonic-gate usrerrenh(enhsc, fmt, va_alist)
3687c478bd9Sstevel@tonic-gate 	char *enhsc;
3697c478bd9Sstevel@tonic-gate 	const char *fmt;
3707c478bd9Sstevel@tonic-gate 	va_dcl
3717c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	char *errtxt;
3747c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (enhsc == NULL || *enhsc == '\0')
3777c478bd9Sstevel@tonic-gate 	{
3787c478bd9Sstevel@tonic-gate 		if (fmt[0] == '5' || fmt[0] == '6')
3797c478bd9Sstevel@tonic-gate 			enhsc = "5.0.0";
3807c478bd9Sstevel@tonic-gate 		else if (fmt[0] == '4' || fmt[0] == '8')
3817c478bd9Sstevel@tonic-gate 			enhsc = "4.0.0";
3827c478bd9Sstevel@tonic-gate 		else if (fmt[0] == '2')
3837c478bd9Sstevel@tonic-gate 			enhsc = "2.0.0";
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, fmt);
3867c478bd9Sstevel@tonic-gate 	errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "550", enhsc, 0, fmt, ap);
3877c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (SuprErrs)
3907c478bd9Sstevel@tonic-gate 		return;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* save this message for mailq printing */
3937c478bd9Sstevel@tonic-gate 	switch (MsgBuf[0])
3947c478bd9Sstevel@tonic-gate 	{
3957c478bd9Sstevel@tonic-gate 	  case '4':
3967c478bd9Sstevel@tonic-gate 	  case '8':
3977c478bd9Sstevel@tonic-gate 		if (CurEnv->e_message != NULL)
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	  case '5':
4037c478bd9Sstevel@tonic-gate 	  case '6':
4047c478bd9Sstevel@tonic-gate 		if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
4057c478bd9Sstevel@tonic-gate 			sm_free(CurEnv->e_message);
4067c478bd9Sstevel@tonic-gate 		if (MsgBuf[0] == '6')
4077c478bd9Sstevel@tonic-gate 		{
4087c478bd9Sstevel@tonic-gate 			char buf[MAXLINE];
4097c478bd9Sstevel@tonic-gate 
410*058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf),
4117c478bd9Sstevel@tonic-gate 					   "Postmaster warning: %.*s",
412*058561cbSjbeck 					   (int) sizeof(buf) - 22, errtxt);
4137c478bd9Sstevel@tonic-gate 			CurEnv->e_message =
4147c478bd9Sstevel@tonic-gate 				sm_rpool_strdup_x(CurEnv->e_rpool, buf);
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		else
4177c478bd9Sstevel@tonic-gate 		{
4187c478bd9Sstevel@tonic-gate 			CurEnv->e_message =
4197c478bd9Sstevel@tonic-gate 				sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	puterrmsg(MsgBuf);
4257c478bd9Sstevel@tonic-gate 	if (LogLevel > 3 && LogUsrErrs)
4267c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_NOTICE, CurEnv->e_id, "%.900s", errtxt);
4277c478bd9Sstevel@tonic-gate 	if (QuickAbort)
4287c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&EtypeQuickAbort, 1);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate **  MESSAGE -- print message (not necessarily an error)
4327c478bd9Sstevel@tonic-gate **
4337c478bd9Sstevel@tonic-gate **	Parameters:
4347c478bd9Sstevel@tonic-gate **		msg -- the message (sm_io_printf fmt) -- it can begin with
4357c478bd9Sstevel@tonic-gate **			an SMTP reply code.  If not, 050 is assumed.
4367c478bd9Sstevel@tonic-gate **		(others) -- sm_io_printf arguments
4377c478bd9Sstevel@tonic-gate **
4387c478bd9Sstevel@tonic-gate **	Returns:
4397c478bd9Sstevel@tonic-gate **		none
4407c478bd9Sstevel@tonic-gate **
4417c478bd9Sstevel@tonic-gate **	Side Effects:
4427c478bd9Sstevel@tonic-gate **		none.
4437c478bd9Sstevel@tonic-gate */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*VARARGS1*/
4467c478bd9Sstevel@tonic-gate void
4477c478bd9Sstevel@tonic-gate #ifdef __STDC__
message(const char * msg,...)4487c478bd9Sstevel@tonic-gate message(const char *msg, ...)
4497c478bd9Sstevel@tonic-gate #else /* __STDC__ */
4507c478bd9Sstevel@tonic-gate message(msg, va_alist)
4517c478bd9Sstevel@tonic-gate 	const char *msg;
4527c478bd9Sstevel@tonic-gate 	va_dcl
4537c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	char *errtxt;
4567c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	errno = 0;
4597c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, msg);
4607c478bd9Sstevel@tonic-gate 	errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "050", (char *) NULL, 0, msg, ap);
4617c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
4627c478bd9Sstevel@tonic-gate 	putoutmsg(MsgBuf, false, false);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/* save this message for mailq printing */
4657c478bd9Sstevel@tonic-gate 	switch (MsgBuf[0])
4667c478bd9Sstevel@tonic-gate 	{
4677c478bd9Sstevel@tonic-gate 	  case '4':
4687c478bd9Sstevel@tonic-gate 	  case '8':
4697c478bd9Sstevel@tonic-gate 		if (CurEnv->e_message != NULL)
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	  case '5':
4747c478bd9Sstevel@tonic-gate 		if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
4757c478bd9Sstevel@tonic-gate 			sm_free(CurEnv->e_message);
4767c478bd9Sstevel@tonic-gate 		CurEnv->e_message =
4777c478bd9Sstevel@tonic-gate 			sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate **  NMESSAGE -- print message (not necessarily an error)
4837c478bd9Sstevel@tonic-gate **
4847c478bd9Sstevel@tonic-gate **	Just like "message" except it never puts the to... tag on.
4857c478bd9Sstevel@tonic-gate **
4867c478bd9Sstevel@tonic-gate **	Parameters:
4877c478bd9Sstevel@tonic-gate **		msg -- the message (sm_io_printf fmt) -- if it begins
4887c478bd9Sstevel@tonic-gate **			with a three digit SMTP reply code, that is used,
4897c478bd9Sstevel@tonic-gate **			otherwise 050 is assumed.
4907c478bd9Sstevel@tonic-gate **		(others) -- sm_io_printf arguments
4917c478bd9Sstevel@tonic-gate **
4927c478bd9Sstevel@tonic-gate **	Returns:
4937c478bd9Sstevel@tonic-gate **		none
4947c478bd9Sstevel@tonic-gate **
4957c478bd9Sstevel@tonic-gate **	Side Effects:
4967c478bd9Sstevel@tonic-gate **		none.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*VARARGS1*/
5007c478bd9Sstevel@tonic-gate void
5017c478bd9Sstevel@tonic-gate #ifdef __STDC__
nmessage(const char * msg,...)5027c478bd9Sstevel@tonic-gate nmessage(const char *msg, ...)
5037c478bd9Sstevel@tonic-gate #else /* __STDC__ */
5047c478bd9Sstevel@tonic-gate nmessage(msg, va_alist)
5057c478bd9Sstevel@tonic-gate 	const char *msg;
5067c478bd9Sstevel@tonic-gate 	va_dcl
5077c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	char *errtxt;
5107c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	errno = 0;
5137c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, msg);
5147c478bd9Sstevel@tonic-gate 	errtxt = fmtmsg(MsgBuf, (char *) NULL, "050",
5157c478bd9Sstevel@tonic-gate 			(char *) NULL, 0, msg, ap);
5167c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
5177c478bd9Sstevel@tonic-gate 	putoutmsg(MsgBuf, false, false);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/* save this message for mailq printing */
5207c478bd9Sstevel@tonic-gate 	switch (MsgBuf[0])
5217c478bd9Sstevel@tonic-gate 	{
5227c478bd9Sstevel@tonic-gate 	  case '4':
5237c478bd9Sstevel@tonic-gate 	  case '8':
5247c478bd9Sstevel@tonic-gate 		if (CurEnv->e_message != NULL)
5257c478bd9Sstevel@tonic-gate 			break;
5267c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	  case '5':
5297c478bd9Sstevel@tonic-gate 		if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
5307c478bd9Sstevel@tonic-gate 			sm_free(CurEnv->e_message);
531*058561cbSjbeck 		CurEnv->e_message = sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate **  PUTOUTMSG -- output error message to transcript and channel
5377c478bd9Sstevel@tonic-gate **
5387c478bd9Sstevel@tonic-gate **	Parameters:
5397c478bd9Sstevel@tonic-gate **		msg -- message to output (in SMTP format).
5407c478bd9Sstevel@tonic-gate **		holdmsg -- if true, don't output a copy of the message to
5417c478bd9Sstevel@tonic-gate **			our output channel.
5427c478bd9Sstevel@tonic-gate **		heldmsg -- if true, this is a previously held message;
5437c478bd9Sstevel@tonic-gate **			don't log it to the transcript file.
5447c478bd9Sstevel@tonic-gate **
5457c478bd9Sstevel@tonic-gate **	Returns:
5467c478bd9Sstevel@tonic-gate **		none.
5477c478bd9Sstevel@tonic-gate **
5487c478bd9Sstevel@tonic-gate **	Side Effects:
5497c478bd9Sstevel@tonic-gate **		Outputs msg to the transcript.
5507c478bd9Sstevel@tonic-gate **		If appropriate, outputs it to the channel.
5517c478bd9Sstevel@tonic-gate **		Deletes SMTP reply code number as appropriate.
5527c478bd9Sstevel@tonic-gate */
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate static void
putoutmsg(msg,holdmsg,heldmsg)5557c478bd9Sstevel@tonic-gate putoutmsg(msg, holdmsg, heldmsg)
5567c478bd9Sstevel@tonic-gate 	char *msg;
5577c478bd9Sstevel@tonic-gate 	bool holdmsg;
5587c478bd9Sstevel@tonic-gate 	bool heldmsg;
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	char msgcode = msg[0];
561*058561cbSjbeck 	char *errtxt = msg;
562*058561cbSjbeck 	char *id;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* display for debugging */
5657c478bd9Sstevel@tonic-gate 	if (tTd(54, 8))
5667c478bd9Sstevel@tonic-gate 		sm_dprintf("--- %s%s%s\n", msg, holdmsg ? " (hold)" : "",
5677c478bd9Sstevel@tonic-gate 			heldmsg ? " (held)" : "");
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/* map warnings to something SMTP can handle */
5707c478bd9Sstevel@tonic-gate 	if (msgcode == '6')
5717c478bd9Sstevel@tonic-gate 		msg[0] = '5';
5727c478bd9Sstevel@tonic-gate 	else if (msgcode == '8')
5737c478bd9Sstevel@tonic-gate 		msg[0] = '4';
574*058561cbSjbeck 	id = (CurEnv != NULL) ? CurEnv->e_id : NULL;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/* output to transcript if serious */
5777c478bd9Sstevel@tonic-gate 	if (!heldmsg && CurEnv != NULL && CurEnv->e_xfp != NULL &&
5787c478bd9Sstevel@tonic-gate 	    strchr("45", msg[0]) != NULL)
5797c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(CurEnv->e_xfp, SM_TIME_DEFAULT, "%s\n",
5807c478bd9Sstevel@tonic-gate 				     msg);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (LogLevel > 14 && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
583*058561cbSjbeck 		sm_syslog(LOG_INFO, id,
5847c478bd9Sstevel@tonic-gate 			  "--- %s%s%s", msg, holdmsg ? " (hold)" : "",
5857c478bd9Sstevel@tonic-gate 			  heldmsg ? " (held)" : "");
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (msgcode == '8')
5887c478bd9Sstevel@tonic-gate 		msg[0] = '0';
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* output to channel if appropriate */
5917c478bd9Sstevel@tonic-gate 	if (!Verbose && msg[0] == '0')
5927c478bd9Sstevel@tonic-gate 		return;
5937c478bd9Sstevel@tonic-gate 	if (holdmsg)
5947c478bd9Sstevel@tonic-gate 	{
5957c478bd9Sstevel@tonic-gate 		/* save for possible future display */
5967c478bd9Sstevel@tonic-gate 		msg[0] = msgcode;
5977c478bd9Sstevel@tonic-gate 		if (HeldMessageBuf[0] == '5' && msgcode == '4')
5987c478bd9Sstevel@tonic-gate 			return;
599*058561cbSjbeck 		(void) sm_strlcpy(HeldMessageBuf, msg, sizeof(HeldMessageBuf));
6007c478bd9Sstevel@tonic-gate 		return;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (OutChannel == NULL)
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* find actual text of error (after SMTP status codes) */
6097c478bd9Sstevel@tonic-gate 	if (ISSMTPREPLY(errtxt))
6107c478bd9Sstevel@tonic-gate 	{
6117c478bd9Sstevel@tonic-gate 		int l;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		errtxt += 4;
6147c478bd9Sstevel@tonic-gate 		l = isenhsc(errtxt, ' ');
6157c478bd9Sstevel@tonic-gate 		if (l <= 0)
6167c478bd9Sstevel@tonic-gate 			l = isenhsc(errtxt, '\0');
6177c478bd9Sstevel@tonic-gate 		if (l > 0)
6187c478bd9Sstevel@tonic-gate 			errtxt += l + 1;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* if DisConnected, OutChannel now points to the transcript */
6227c478bd9Sstevel@tonic-gate 	if (!DisConnected &&
6237c478bd9Sstevel@tonic-gate 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON || OpMode == MD_ARPAFTP))
6247c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(OutChannel, SM_TIME_DEFAULT, "%s\r\n",
6257c478bd9Sstevel@tonic-gate 				     msg);
6267c478bd9Sstevel@tonic-gate 	else
6277c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(OutChannel, SM_TIME_DEFAULT, "%s\n",
6287c478bd9Sstevel@tonic-gate 				     errtxt);
6297c478bd9Sstevel@tonic-gate 	if (TrafficLogFile != NULL)
6307c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
6317c478bd9Sstevel@tonic-gate 				     "%05d >>> %s\n", (int) CurrentPid,
6327c478bd9Sstevel@tonic-gate 				     (OpMode == MD_SMTP || OpMode == MD_DAEMON)
6337c478bd9Sstevel@tonic-gate 					? msg : errtxt);
6347c478bd9Sstevel@tonic-gate #if !PIPELINING
6357c478bd9Sstevel@tonic-gate 	/* XXX can't flush here for SMTP pipelining */
6367c478bd9Sstevel@tonic-gate 	if (msg[3] == ' ')
6377c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
6387c478bd9Sstevel@tonic-gate 	if (!sm_io_error(OutChannel) || DisConnected)
6397c478bd9Sstevel@tonic-gate 		return;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	**  Error on output -- if reporting lost channel, just ignore it.
6437c478bd9Sstevel@tonic-gate 	**  Also, ignore errors from QUIT response (221 message) -- some
6447c478bd9Sstevel@tonic-gate 	**	rude servers don't read result.
6457c478bd9Sstevel@tonic-gate 	*/
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (InChannel == NULL || sm_io_eof(InChannel) ||
6487c478bd9Sstevel@tonic-gate 	    sm_io_error(InChannel) || strncmp(msg, "221", 3) == 0)
6497c478bd9Sstevel@tonic-gate 		return;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* can't call syserr, 'cause we are using MsgBuf */
6527c478bd9Sstevel@tonic-gate 	HoldErrs = true;
6537c478bd9Sstevel@tonic-gate 	if (LogLevel > 0)
654*058561cbSjbeck 		sm_syslog(LOG_CRIT, id,
6557c478bd9Sstevel@tonic-gate 			  "SYSERR: putoutmsg (%s): error on output channel sending \"%s\": %s",
6567c478bd9Sstevel@tonic-gate 			  CURHOSTNAME,
6577c478bd9Sstevel@tonic-gate 			  shortenstring(msg, MAXSHORTSTR), sm_errstring(errno));
6587c478bd9Sstevel@tonic-gate #endif /* !PIPELINING */
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate **  PUTERRMSG -- like putoutmsg, but does special processing for error messages
6627c478bd9Sstevel@tonic-gate **
6637c478bd9Sstevel@tonic-gate **	Parameters:
6647c478bd9Sstevel@tonic-gate **		msg -- the message to output.
6657c478bd9Sstevel@tonic-gate **
6667c478bd9Sstevel@tonic-gate **	Returns:
6677c478bd9Sstevel@tonic-gate **		none.
6687c478bd9Sstevel@tonic-gate **
6697c478bd9Sstevel@tonic-gate **	Side Effects:
6707c478bd9Sstevel@tonic-gate **		Sets the fatal error bit in the envelope as appropriate.
6717c478bd9Sstevel@tonic-gate */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static void
puterrmsg(msg)6747c478bd9Sstevel@tonic-gate puterrmsg(msg)
6757c478bd9Sstevel@tonic-gate 	char *msg;
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	char msgcode = msg[0];
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* output the message as usual */
6807c478bd9Sstevel@tonic-gate 	putoutmsg(msg, HoldErrs, false);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* be careful about multiple error messages */
6837c478bd9Sstevel@tonic-gate 	if (OnlyOneError)
6847c478bd9Sstevel@tonic-gate 		HoldErrs = true;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* signal the error */
6877c478bd9Sstevel@tonic-gate 	Errors++;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (CurEnv == NULL)
6907c478bd9Sstevel@tonic-gate 		return;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (msgcode == '6')
6937c478bd9Sstevel@tonic-gate 	{
6947c478bd9Sstevel@tonic-gate 		/* notify the postmaster */
6957c478bd9Sstevel@tonic-gate 		CurEnv->e_flags |= EF_PM_NOTIFY;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	else if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
6987c478bd9Sstevel@tonic-gate 	{
6997c478bd9Sstevel@tonic-gate 		/* mark long-term fatal errors */
7007c478bd9Sstevel@tonic-gate 		CurEnv->e_flags |= EF_FATALERRS;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate **  ISENHSC -- check whether a string contains an enhanced status code
7057c478bd9Sstevel@tonic-gate **
7067c478bd9Sstevel@tonic-gate **	Parameters:
7077c478bd9Sstevel@tonic-gate **		s -- string with possible enhanced status code.
7087c478bd9Sstevel@tonic-gate **		delim -- delim for enhanced status code.
7097c478bd9Sstevel@tonic-gate **
7107c478bd9Sstevel@tonic-gate **	Returns:
7117c478bd9Sstevel@tonic-gate **		0  -- no enhanced status code.
7127c478bd9Sstevel@tonic-gate **		>4 -- length of enhanced status code.
7137c478bd9Sstevel@tonic-gate **
7147c478bd9Sstevel@tonic-gate **	Side Effects:
7157c478bd9Sstevel@tonic-gate **		none.
7167c478bd9Sstevel@tonic-gate */
7177c478bd9Sstevel@tonic-gate int
isenhsc(s,delim)7187c478bd9Sstevel@tonic-gate isenhsc(s, delim)
7197c478bd9Sstevel@tonic-gate 	const char *s;
7207c478bd9Sstevel@tonic-gate 	int delim;
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	int l, h;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (s == NULL)
7257c478bd9Sstevel@tonic-gate 		return 0;
7267c478bd9Sstevel@tonic-gate 	if (!((*s == '2' || *s == '4' || *s == '5') && s[1] == '.'))
7277c478bd9Sstevel@tonic-gate 		return 0;
7287c478bd9Sstevel@tonic-gate 	h = 0;
7297c478bd9Sstevel@tonic-gate 	l = 2;
7307c478bd9Sstevel@tonic-gate 	while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
7317c478bd9Sstevel@tonic-gate 		++h;
7327c478bd9Sstevel@tonic-gate 	if (h == 0 || s[l + h] != '.')
7337c478bd9Sstevel@tonic-gate 		return 0;
7347c478bd9Sstevel@tonic-gate 	l += h + 1;
7357c478bd9Sstevel@tonic-gate 	h = 0;
7367c478bd9Sstevel@tonic-gate 	while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
7377c478bd9Sstevel@tonic-gate 		++h;
7387c478bd9Sstevel@tonic-gate 	if (h == 0 || s[l + h] != delim)
7397c478bd9Sstevel@tonic-gate 		return 0;
7407c478bd9Sstevel@tonic-gate 	return l + h;
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate /*
7437c478bd9Sstevel@tonic-gate **  EXTENHSC -- check and extract an enhanced status code
7447c478bd9Sstevel@tonic-gate **
7457c478bd9Sstevel@tonic-gate **	Parameters:
7467c478bd9Sstevel@tonic-gate **		s -- string with possible enhanced status code.
7477c478bd9Sstevel@tonic-gate **		delim -- delim for enhanced status code.
7487c478bd9Sstevel@tonic-gate **		e -- pointer to storage for enhanced status code.
7497c478bd9Sstevel@tonic-gate **			must be != NULL and have space for at least
7507c478bd9Sstevel@tonic-gate **			10 characters ([245].[0-9]{1,3}.[0-9]{1,3})
7517c478bd9Sstevel@tonic-gate **
7527c478bd9Sstevel@tonic-gate **	Returns:
7537c478bd9Sstevel@tonic-gate **		0  -- no enhanced status code.
7547c478bd9Sstevel@tonic-gate **		>4 -- length of enhanced status code.
7557c478bd9Sstevel@tonic-gate **
7567c478bd9Sstevel@tonic-gate **	Side Effects:
7577c478bd9Sstevel@tonic-gate **		fills e with enhanced status code.
7587c478bd9Sstevel@tonic-gate */
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate int
extenhsc(s,delim,e)7617c478bd9Sstevel@tonic-gate extenhsc(s, delim, e)
7627c478bd9Sstevel@tonic-gate 	const char *s;
7637c478bd9Sstevel@tonic-gate 	int delim;
7647c478bd9Sstevel@tonic-gate 	char *e;
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	int l, h;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (s == NULL)
7697c478bd9Sstevel@tonic-gate 		return 0;
7707c478bd9Sstevel@tonic-gate 	if (!((*s == '2' || *s == '4' || *s == '5') && s[1] == '.'))
7717c478bd9Sstevel@tonic-gate 		return 0;
7727c478bd9Sstevel@tonic-gate 	h = 0;
7737c478bd9Sstevel@tonic-gate 	l = 2;
7747c478bd9Sstevel@tonic-gate 	e[0] = s[0];
7757c478bd9Sstevel@tonic-gate 	e[1] = '.';
7767c478bd9Sstevel@tonic-gate 	while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
7777c478bd9Sstevel@tonic-gate 	{
7787c478bd9Sstevel@tonic-gate 		e[l + h] = s[l + h];
7797c478bd9Sstevel@tonic-gate 		++h;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	if (h == 0 || s[l + h] != '.')
7827c478bd9Sstevel@tonic-gate 		return 0;
7837c478bd9Sstevel@tonic-gate 	e[l + h] = '.';
7847c478bd9Sstevel@tonic-gate 	l += h + 1;
7857c478bd9Sstevel@tonic-gate 	h = 0;
7867c478bd9Sstevel@tonic-gate 	while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
7877c478bd9Sstevel@tonic-gate 	{
7887c478bd9Sstevel@tonic-gate 		e[l + h] = s[l + h];
7897c478bd9Sstevel@tonic-gate 		++h;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	if (h == 0 || s[l + h] != delim)
7927c478bd9Sstevel@tonic-gate 		return 0;
7937c478bd9Sstevel@tonic-gate 	e[l + h] = '\0';
7947c478bd9Sstevel@tonic-gate 	return l + h;
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate **  FMTMSG -- format a message into buffer.
7987c478bd9Sstevel@tonic-gate **
7997c478bd9Sstevel@tonic-gate **	Parameters:
8007c478bd9Sstevel@tonic-gate **		eb -- error buffer to get result -- MUST BE MsgBuf.
8017c478bd9Sstevel@tonic-gate **		to -- the recipient tag for this message.
8027c478bd9Sstevel@tonic-gate **		num -- default three digit SMTP reply code.
8037c478bd9Sstevel@tonic-gate **		enhsc -- enhanced status code.
8047c478bd9Sstevel@tonic-gate **		en -- the error number to display.
8057c478bd9Sstevel@tonic-gate **		fmt -- format of string.
8067c478bd9Sstevel@tonic-gate **		ap -- arguments for fmt.
8077c478bd9Sstevel@tonic-gate **
8087c478bd9Sstevel@tonic-gate **	Returns:
8097c478bd9Sstevel@tonic-gate **		pointer to error text beyond status codes.
8107c478bd9Sstevel@tonic-gate **
8117c478bd9Sstevel@tonic-gate **	Side Effects:
8127c478bd9Sstevel@tonic-gate **		none.
8137c478bd9Sstevel@tonic-gate */
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate static char *
fmtmsg(eb,to,num,enhsc,eno,fmt,ap)8167c478bd9Sstevel@tonic-gate fmtmsg(eb, to, num, enhsc, eno, fmt, ap)
8177c478bd9Sstevel@tonic-gate 	register char *eb;
8187c478bd9Sstevel@tonic-gate 	const char *to;
8197c478bd9Sstevel@tonic-gate 	const char *num;
8207c478bd9Sstevel@tonic-gate 	const char *enhsc;
8217c478bd9Sstevel@tonic-gate 	int eno;
8227c478bd9Sstevel@tonic-gate 	const char *fmt;
8237c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	char del;
8267c478bd9Sstevel@tonic-gate 	int l;
827*058561cbSjbeck 	int spaceleft = sizeof(MsgBuf);
8287c478bd9Sstevel@tonic-gate 	char *errtxt;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/* output the reply code */
8317c478bd9Sstevel@tonic-gate 	if (ISSMTPCODE(fmt))
8327c478bd9Sstevel@tonic-gate 	{
8337c478bd9Sstevel@tonic-gate 		num = fmt;
8347c478bd9Sstevel@tonic-gate 		fmt += 4;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	if (num[3] == '-')
8377c478bd9Sstevel@tonic-gate 		del = '-';
8387c478bd9Sstevel@tonic-gate 	else
8397c478bd9Sstevel@tonic-gate 		del = ' ';
8407c478bd9Sstevel@tonic-gate 	if (SoftBounce && num[0] == '5')
8417c478bd9Sstevel@tonic-gate 	{
8427c478bd9Sstevel@tonic-gate 		/* replace 5 by 4 */
8437c478bd9Sstevel@tonic-gate 		(void) sm_snprintf(eb, spaceleft, "4%2.2s%c", num + 1, del);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	else
846*058561cbSjbeck 		(void) sm_snprintf(eb, spaceleft, "%3.3s%c", num, del);
8477c478bd9Sstevel@tonic-gate 	eb += 4;
8487c478bd9Sstevel@tonic-gate 	spaceleft -= 4;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if ((l = isenhsc(fmt, ' ' )) > 0 && l < spaceleft - 4)
8517c478bd9Sstevel@tonic-gate 	{
8527c478bd9Sstevel@tonic-gate 		/* copy enh.status code including trailing blank */
8537c478bd9Sstevel@tonic-gate 		l++;
8547c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(eb, fmt, l + 1);
8557c478bd9Sstevel@tonic-gate 		eb += l;
8567c478bd9Sstevel@tonic-gate 		spaceleft -= l;
8577c478bd9Sstevel@tonic-gate 		fmt += l;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	else if ((l = isenhsc(enhsc, '\0')) > 0 && l < spaceleft - 4)
8607c478bd9Sstevel@tonic-gate 	{
8617c478bd9Sstevel@tonic-gate 		/* copy enh.status code */
8627c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(eb, enhsc, l + 1);
8637c478bd9Sstevel@tonic-gate 		eb[l] = ' ';
8647c478bd9Sstevel@tonic-gate 		eb[++l] = '\0';
8657c478bd9Sstevel@tonic-gate 		eb += l;
8667c478bd9Sstevel@tonic-gate 		spaceleft -= l;
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	if (SoftBounce && eb[-l] == '5')
8697c478bd9Sstevel@tonic-gate 	{
8707c478bd9Sstevel@tonic-gate 		/* replace 5 by 4 */
8717c478bd9Sstevel@tonic-gate 		eb[-l] = '4';
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 	errtxt = eb;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* output the file name and line number */
8767c478bd9Sstevel@tonic-gate 	if (FileName != NULL)
8777c478bd9Sstevel@tonic-gate 	{
8787c478bd9Sstevel@tonic-gate 		(void) sm_snprintf(eb, spaceleft, "%s: line %d: ",
8797c478bd9Sstevel@tonic-gate 				   shortenstring(FileName, 83), LineNumber);
8807c478bd9Sstevel@tonic-gate 		eb += (l = strlen(eb));
8817c478bd9Sstevel@tonic-gate 		spaceleft -= l;
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	**  output the "to" address only if it is defined and one of the
8867c478bd9Sstevel@tonic-gate 	**  following codes is used:
8877c478bd9Sstevel@tonic-gate 	**  050 internal notices, e.g., alias expansion
8887c478bd9Sstevel@tonic-gate 	**  250 Ok
8897c478bd9Sstevel@tonic-gate 	**  252 Cannot VRFY user, but will accept message and attempt delivery
8907c478bd9Sstevel@tonic-gate 	**  450 Requested mail action not taken: mailbox unavailable
8917c478bd9Sstevel@tonic-gate 	**  550 Requested action not taken: mailbox unavailable
8927c478bd9Sstevel@tonic-gate 	**  553 Requested action not taken: mailbox name not allowed
8937c478bd9Sstevel@tonic-gate 	**
8947c478bd9Sstevel@tonic-gate 	**  Notice: this still isn't "the right thing", this code shouldn't
8957c478bd9Sstevel@tonic-gate 	**	(indirectly) depend on CurEnv->e_to.
8967c478bd9Sstevel@tonic-gate 	*/
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	if (to != NULL && to[0] != '\0' &&
8997c478bd9Sstevel@tonic-gate 	    (strncmp(num, "050", 3) == 0 ||
9007c478bd9Sstevel@tonic-gate 	     strncmp(num, "250", 3) == 0 ||
9017c478bd9Sstevel@tonic-gate 	     strncmp(num, "252", 3) == 0 ||
9027c478bd9Sstevel@tonic-gate 	     strncmp(num, "450", 3) == 0 ||
9037c478bd9Sstevel@tonic-gate 	     strncmp(num, "550", 3) == 0 ||
9047c478bd9Sstevel@tonic-gate 	     strncmp(num, "553", 3) == 0))
9057c478bd9Sstevel@tonic-gate 	{
9067c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(eb, spaceleft, 2,
9077c478bd9Sstevel@tonic-gate 				   shortenstring(to, MAXSHORTSTR), "... ");
9087c478bd9Sstevel@tonic-gate 		spaceleft -= strlen(eb);
9097c478bd9Sstevel@tonic-gate 		while (*eb != '\0')
9107c478bd9Sstevel@tonic-gate 			*eb++ &= 0177;
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/* output the message */
9147c478bd9Sstevel@tonic-gate 	(void) sm_vsnprintf(eb, spaceleft, fmt, ap);
9157c478bd9Sstevel@tonic-gate 	spaceleft -= strlen(eb);
9167c478bd9Sstevel@tonic-gate 	while (*eb != '\0')
9177c478bd9Sstevel@tonic-gate 		*eb++ &= 0177;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/* output the error code, if any */
9207c478bd9Sstevel@tonic-gate 	if (eno != 0)
9217c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(eb, spaceleft, 2, ": ", sm_errstring(eno));
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	return errtxt;
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate **  BUFFER_ERRORS -- arrange to buffer future error messages
9277c478bd9Sstevel@tonic-gate **
9287c478bd9Sstevel@tonic-gate **	Parameters:
9297c478bd9Sstevel@tonic-gate **		none
9307c478bd9Sstevel@tonic-gate **
9317c478bd9Sstevel@tonic-gate **	Returns:
9327c478bd9Sstevel@tonic-gate **		none.
9337c478bd9Sstevel@tonic-gate */
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate void
buffer_errors()9367c478bd9Sstevel@tonic-gate buffer_errors()
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	HeldMessageBuf[0] = '\0';
9397c478bd9Sstevel@tonic-gate 	HoldErrs = true;
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate **  FLUSH_ERRORS -- flush the held error message buffer
9437c478bd9Sstevel@tonic-gate **
9447c478bd9Sstevel@tonic-gate **	Parameters:
9457c478bd9Sstevel@tonic-gate **		print -- if set, print the message, otherwise just
9467c478bd9Sstevel@tonic-gate **			delete it.
9477c478bd9Sstevel@tonic-gate **
9487c478bd9Sstevel@tonic-gate **	Returns:
9497c478bd9Sstevel@tonic-gate **		none.
9507c478bd9Sstevel@tonic-gate */
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate void
flush_errors(print)9537c478bd9Sstevel@tonic-gate flush_errors(print)
9547c478bd9Sstevel@tonic-gate 	bool print;
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	if (print && HeldMessageBuf[0] != '\0')
9577c478bd9Sstevel@tonic-gate 		putoutmsg(HeldMessageBuf, false, true);
9587c478bd9Sstevel@tonic-gate 	HeldMessageBuf[0] = '\0';
9597c478bd9Sstevel@tonic-gate 	HoldErrs = false;
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate /*
9627c478bd9Sstevel@tonic-gate **  SM_ERRSTRING -- return string description of error code
9637c478bd9Sstevel@tonic-gate **
9647c478bd9Sstevel@tonic-gate **	Parameters:
9657c478bd9Sstevel@tonic-gate **		errnum -- the error number to translate
9667c478bd9Sstevel@tonic-gate **
9677c478bd9Sstevel@tonic-gate **	Returns:
9687c478bd9Sstevel@tonic-gate **		A string description of errnum.
9697c478bd9Sstevel@tonic-gate **
9707c478bd9Sstevel@tonic-gate **	Side Effects:
9717c478bd9Sstevel@tonic-gate **		none.
9727c478bd9Sstevel@tonic-gate */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate const char *
sm_errstring(errnum)9757c478bd9Sstevel@tonic-gate sm_errstring(errnum)
9767c478bd9Sstevel@tonic-gate 	int errnum;
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	char *dnsmsg;
9797c478bd9Sstevel@tonic-gate 	char *bp;
9807c478bd9Sstevel@tonic-gate 	static char buf[MAXLINE];
9817c478bd9Sstevel@tonic-gate #if HASSTRERROR
9827c478bd9Sstevel@tonic-gate 	char *err;
9837c478bd9Sstevel@tonic-gate 	char errbuf[30];
9847c478bd9Sstevel@tonic-gate #endif /* HASSTRERROR */
9857c478bd9Sstevel@tonic-gate #if !HASSTRERROR && !defined(ERRLIST_PREDEFINED)
9867c478bd9Sstevel@tonic-gate 	extern char *sys_errlist[];
9877c478bd9Sstevel@tonic-gate 	extern int sys_nerr;
9887c478bd9Sstevel@tonic-gate #endif /* !HASSTRERROR && !defined(ERRLIST_PREDEFINED) */
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/*
9917c478bd9Sstevel@tonic-gate 	**  Handle special network error codes.
9927c478bd9Sstevel@tonic-gate 	**
9937c478bd9Sstevel@tonic-gate 	**	These are 4.2/4.3bsd specific; they should be in daemon.c.
9947c478bd9Sstevel@tonic-gate 	*/
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	dnsmsg = NULL;
9977c478bd9Sstevel@tonic-gate 	switch (errnum)
9987c478bd9Sstevel@tonic-gate 	{
9997c478bd9Sstevel@tonic-gate 	  case ETIMEDOUT:
10007c478bd9Sstevel@tonic-gate 	  case ECONNRESET:
10017c478bd9Sstevel@tonic-gate 		bp = buf;
10027c478bd9Sstevel@tonic-gate #if HASSTRERROR
10037c478bd9Sstevel@tonic-gate 		err = strerror(errnum);
10047c478bd9Sstevel@tonic-gate 		if (err == NULL)
10057c478bd9Sstevel@tonic-gate 		{
1006*058561cbSjbeck 			(void) sm_snprintf(errbuf, sizeof(errbuf),
10077c478bd9Sstevel@tonic-gate 					   "Error %d", errnum);
10087c478bd9Sstevel@tonic-gate 			err = errbuf;
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bp, err, SPACELEFT(buf, bp));
10117c478bd9Sstevel@tonic-gate #else /* HASSTRERROR */
10127c478bd9Sstevel@tonic-gate 		if (errnum >= 0 && errnum < sys_nerr)
10137c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(bp, sys_errlist[errnum],
10147c478bd9Sstevel@tonic-gate 					  SPACELEFT(buf, bp));
10157c478bd9Sstevel@tonic-gate 		else
10167c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
10177c478bd9Sstevel@tonic-gate 				"Error %d", errnum);
10187c478bd9Sstevel@tonic-gate #endif /* HASSTRERROR */
10197c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
10207c478bd9Sstevel@tonic-gate 		if (CurHostName != NULL)
10217c478bd9Sstevel@tonic-gate 		{
10227c478bd9Sstevel@tonic-gate 			if (errnum == ETIMEDOUT)
10237c478bd9Sstevel@tonic-gate 			{
10247c478bd9Sstevel@tonic-gate 				(void) sm_snprintf(bp, SPACELEFT(buf, bp),
10257c478bd9Sstevel@tonic-gate 					" with ");
10267c478bd9Sstevel@tonic-gate 				bp += strlen(bp);
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 			else
10297c478bd9Sstevel@tonic-gate 			{
10307c478bd9Sstevel@tonic-gate 				bp = buf;
10317c478bd9Sstevel@tonic-gate 				(void) sm_snprintf(bp, SPACELEFT(buf, bp),
10327c478bd9Sstevel@tonic-gate 					"Connection reset by ");
10337c478bd9Sstevel@tonic-gate 				bp += strlen(bp);
10347c478bd9Sstevel@tonic-gate 			}
10357c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(bp,
10367c478bd9Sstevel@tonic-gate 					shortenstring(CurHostName, MAXSHORTSTR),
10377c478bd9Sstevel@tonic-gate 					SPACELEFT(buf, bp));
10387c478bd9Sstevel@tonic-gate 			bp += strlen(buf);
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 		if (SmtpPhase != NULL)
10417c478bd9Sstevel@tonic-gate 		{
10427c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
10437c478bd9Sstevel@tonic-gate 				" during %s", SmtpPhase);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		return buf;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	  case EHOSTDOWN:
10487c478bd9Sstevel@tonic-gate 		if (CurHostName == NULL)
10497c478bd9Sstevel@tonic-gate 			break;
1050*058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf), "Host %s is down",
10517c478bd9Sstevel@tonic-gate 			shortenstring(CurHostName, MAXSHORTSTR));
10527c478bd9Sstevel@tonic-gate 		return buf;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	  case ECONNREFUSED:
10557c478bd9Sstevel@tonic-gate 		if (CurHostName == NULL)
10567c478bd9Sstevel@tonic-gate 			break;
1057*058561cbSjbeck 		(void) sm_strlcpyn(buf, sizeof(buf), 2, "Connection refused by ",
10587c478bd9Sstevel@tonic-gate 			shortenstring(CurHostName, MAXSHORTSTR));
10597c478bd9Sstevel@tonic-gate 		return buf;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate #if NAMED_BIND
10627c478bd9Sstevel@tonic-gate 	  case HOST_NOT_FOUND + E_DNSBASE:
10637c478bd9Sstevel@tonic-gate 		dnsmsg = "host not found";
10647c478bd9Sstevel@tonic-gate 		break;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	  case TRY_AGAIN + E_DNSBASE:
10677c478bd9Sstevel@tonic-gate 		dnsmsg = "host name lookup failure";
10687c478bd9Sstevel@tonic-gate 		break;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	  case NO_RECOVERY + E_DNSBASE:
10717c478bd9Sstevel@tonic-gate 		dnsmsg = "non-recoverable error";
10727c478bd9Sstevel@tonic-gate 		break;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	  case NO_DATA + E_DNSBASE:
10757c478bd9Sstevel@tonic-gate 		dnsmsg = "no data known";
10767c478bd9Sstevel@tonic-gate 		break;
10777c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	  case EPERM:
10807c478bd9Sstevel@tonic-gate 		/* SunOS gives "Not owner" -- this is the POSIX message */
10817c478bd9Sstevel@tonic-gate 		return "Operation not permitted";
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	**  Error messages used internally in sendmail.
10857c478bd9Sstevel@tonic-gate 	*/
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	  case E_SM_OPENTIMEOUT:
10887c478bd9Sstevel@tonic-gate 		return "Timeout on file open";
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	  case E_SM_NOSLINK:
10917c478bd9Sstevel@tonic-gate 		return "Symbolic links not allowed";
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	  case E_SM_NOHLINK:
10947c478bd9Sstevel@tonic-gate 		return "Hard links not allowed";
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	  case E_SM_REGONLY:
10977c478bd9Sstevel@tonic-gate 		return "Regular files only";
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	  case E_SM_ISEXEC:
11007c478bd9Sstevel@tonic-gate 		return "Executable files not allowed";
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	  case E_SM_WWDIR:
11037c478bd9Sstevel@tonic-gate 		return "World writable directory";
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	  case E_SM_GWDIR:
11067c478bd9Sstevel@tonic-gate 		return "Group writable directory";
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	  case E_SM_FILECHANGE:
11097c478bd9Sstevel@tonic-gate 		return "File changed after open";
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	  case E_SM_WWFILE:
11127c478bd9Sstevel@tonic-gate 		return "World writable file";
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	  case E_SM_GWFILE:
11157c478bd9Sstevel@tonic-gate 		return "Group writable file";
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	  case E_SM_GRFILE:
11187c478bd9Sstevel@tonic-gate 		return "Group readable file";
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	  case E_SM_WRFILE:
11217c478bd9Sstevel@tonic-gate 		return "World readable file";
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (dnsmsg != NULL)
11257c478bd9Sstevel@tonic-gate 	{
11267c478bd9Sstevel@tonic-gate 		bp = buf;
1127*058561cbSjbeck 		bp += sm_strlcpy(bp, "Name server: ", sizeof(buf));
11287c478bd9Sstevel@tonic-gate 		if (CurHostName != NULL)
11297c478bd9Sstevel@tonic-gate 		{
11307c478bd9Sstevel@tonic-gate 			(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2,
11317c478bd9Sstevel@tonic-gate 				shortenstring(CurHostName, MAXSHORTSTR), ": ");
11327c478bd9Sstevel@tonic-gate 			bp += strlen(bp);
11337c478bd9Sstevel@tonic-gate 		}
11347c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bp, dnsmsg, SPACELEFT(buf, bp));
11357c478bd9Sstevel@tonic-gate 		return buf;
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate #if LDAPMAP
11397c478bd9Sstevel@tonic-gate 	if (errnum >= E_LDAPBASE)
11407c478bd9Sstevel@tonic-gate 		return ldap_err2string(errnum - E_LDAPBASE);
11417c478bd9Sstevel@tonic-gate #endif /* LDAPMAP */
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate #if HASSTRERROR
11447c478bd9Sstevel@tonic-gate 	err = strerror(errnum);
11457c478bd9Sstevel@tonic-gate 	if (err == NULL)
11467c478bd9Sstevel@tonic-gate 	{
1147*058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf), "Error %d", errnum);
11487c478bd9Sstevel@tonic-gate 		return buf;
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 	return err;
11517c478bd9Sstevel@tonic-gate #else /* HASSTRERROR */
11527c478bd9Sstevel@tonic-gate 	if (errnum > 0 && errnum < sys_nerr)
11537c478bd9Sstevel@tonic-gate 		return sys_errlist[errnum];
11547c478bd9Sstevel@tonic-gate 
1155*058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf), "Error %d", errnum);
11567c478bd9Sstevel@tonic-gate 	return buf;
11577c478bd9Sstevel@tonic-gate #endif /* HASSTRERROR */
11587c478bd9Sstevel@tonic-gate }
1159