xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 64123)
122705Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822705Sdist 
922705Sdist #ifndef lint
10*64123Seric static char sccsid[] = "@(#)err.c	8.7 (Berkeley) 08/06/93";
1133729Sbostic #endif /* not lint */
1222705Sdist 
133311Seric # include "sendmail.h"
1424943Seric # include <errno.h>
1525526Smiriam # include <netdb.h>
16295Seric 
17295Seric /*
181514Seric **  SYSERR -- Print error message.
19295Seric **
20295Seric **	Prints an error message via printf to the diagnostic
21295Seric **	output.  If LOG is defined, it logs it also.
22295Seric **
2358690Seric **	If the first character of the syserr message is `!' it will
2458690Seric **	log this as an ALERT message and exit immediately.  This can
2558690Seric **	leave queue files in an indeterminate state, so it should not
2658690Seric **	be used lightly.
2758690Seric **
28295Seric **	Parameters:
29295Seric **		f -- the format string
30295Seric **		a, b, c, d, e -- parameters
31295Seric **
32295Seric **	Returns:
334084Seric **		none
347762Seric **		Through TopFrame if QuickAbort is set.
35295Seric **
36295Seric **	Side Effects:
371514Seric **		increments Errors.
381514Seric **		sets ExitStat.
39295Seric */
40295Seric 
4110147Seric char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
424084Seric 
4363969Seric static void	fmtmsg();
4446928Sbostic 
4563969Seric #if defined(NAMED_BIND) && !defined(NO_DATA)
4663969Seric # define NO_DATA	NO_ADDRESS
4763969Seric #endif
4863969Seric 
4958824Seric void
50295Seric /*VARARGS1*/
5157642Seric #ifdef __STDC__
5260094Seric syserr(const char *fmt, ...)
5357642Seric #else
5457642Seric syserr(fmt, va_alist)
5560094Seric 	const char *fmt;
5657642Seric 	va_dcl
5757642Seric #endif
58295Seric {
5916901Seric 	register char *p;
6016901Seric 	int olderrno = errno;
6158690Seric 	bool panic;
6256852Seric 	VA_LOCAL_DECL
63295Seric 
6458690Seric 	panic = *fmt == '!';
6558690Seric 	if (panic)
6658690Seric 		fmt++;
6758690Seric 
687525Seric 	/* format and output the error message */
6916901Seric 	if (olderrno == 0)
7058151Seric 		p = "554";
717957Seric 	else
7258151Seric 		p = "451";
7356852Seric 	VA_START(fmt);
7456852Seric 	fmtmsg(MsgBuf, (char *) NULL, p, olderrno, fmt, ap);
7556852Seric 	VA_END;
769389Seric 	puterrmsg(MsgBuf);
774063Seric 
78295Seric 	/* determine exit status if not already set */
79295Seric 	if (ExitStat == EX_OK)
80295Seric 	{
8116901Seric 		if (olderrno == 0)
82295Seric 			ExitStat = EX_SOFTWARE;
83295Seric 		else
841598Seric 			ExitStat = EX_OSERR;
85295Seric 	}
86295Seric 
87295Seric # ifdef LOG
887674Seric 	if (LogLevel > 0)
8958690Seric 		syslog(panic ? LOG_ALERT : LOG_CRIT, "%s: SYSERR: %s",
9025277Seric 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
9125277Seric 			&MsgBuf[4]);
9256795Seric # endif /* LOG */
9358690Seric 	if (panic)
9459156Seric 	{
9559156Seric #ifdef XLA
9659156Seric 		xla_all_end();
9759156Seric #endif
9858690Seric 		exit(EX_OSERR);
9959156Seric 	}
100295Seric 	errno = 0;
1017762Seric 	if (QuickAbort)
1027762Seric 		longjmp(TopFrame, 2);
103295Seric }
104295Seric /*
105295Seric **  USRERR -- Signal user error.
106295Seric **
107295Seric **	This is much like syserr except it is for user errors.
108295Seric **
109295Seric **	Parameters:
110295Seric **		fmt, a, b, c, d -- printf strings
111295Seric **
112295Seric **	Returns:
1134084Seric **		none
1147762Seric **		Through TopFrame if QuickAbort is set.
115295Seric **
116295Seric **	Side Effects:
1171514Seric **		increments Errors.
118295Seric */
119295Seric 
120295Seric /*VARARGS1*/
12158824Seric void
12257642Seric #ifdef __STDC__
12360094Seric usrerr(const char *fmt, ...)
12457642Seric #else
12557642Seric usrerr(fmt, va_alist)
12660094Seric 	const char *fmt;
12757642Seric 	va_dcl
12857642Seric #endif
129295Seric {
13056852Seric 	VA_LOCAL_DECL
131295Seric 	extern char SuprErrs;
13216901Seric 	extern int errno;
133295Seric 
134295Seric 	if (SuprErrs)
1354084Seric 		return;
136295Seric 
13756852Seric 	VA_START(fmt);
13858524Seric 	fmtmsg(MsgBuf, CurEnv->e_to, "501", 0, fmt, ap);
13956852Seric 	VA_END;
1409389Seric 	puterrmsg(MsgBuf);
1418239Seric 
14251951Seric # ifdef LOG
14358020Seric 	if (LogLevel > 3 && LogUsrErrs)
14451951Seric 		syslog(LOG_NOTICE, "%s: %s",
14551951Seric 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
14651951Seric 			&MsgBuf[4]);
14756795Seric # endif /* LOG */
14851951Seric 
1497762Seric 	if (QuickAbort)
1507762Seric 		longjmp(TopFrame, 1);
1514063Seric }
1524063Seric /*
1534063Seric **  MESSAGE -- print message (not necessarily an error)
1544063Seric **
1554063Seric **	Parameters:
15659581Seric **		msg -- the message (printf fmt) -- it can begin with
15759581Seric **			an SMTP reply code.  If not, 050 is assumed.
1584063Seric **		a, b, c, d, e -- printf arguments
1594063Seric **
1604063Seric **	Returns:
1614063Seric **		none
1624063Seric **
1634063Seric **	Side Effects:
1644063Seric **		none.
1654063Seric */
1664063Seric 
1674084Seric /*VARARGS2*/
16858826Seric void
16957642Seric #ifdef __STDC__
17060094Seric message(const char *msg, ...)
17157642Seric #else
17258151Seric message(msg, va_alist)
17360094Seric 	const char *msg;
17457642Seric 	va_dcl
17557642Seric #endif
1764063Seric {
17756852Seric 	VA_LOCAL_DECL
17856852Seric 
1794711Seric 	errno = 0;
18056852Seric 	VA_START(msg);
18158151Seric 	fmtmsg(MsgBuf, CurEnv->e_to, "050", 0, msg, ap);
18256852Seric 	VA_END;
18363753Seric 	putoutmsg(MsgBuf, FALSE);
1847613Seric }
1857613Seric /*
1868239Seric **  NMESSAGE -- print message (not necessarily an error)
1878239Seric **
1888239Seric **	Just like "message" except it never puts the to... tag on.
1898239Seric **
1908239Seric **	Parameters:
1918239Seric **		num -- the default ARPANET error number (in ascii)
1928239Seric **		msg -- the message (printf fmt) -- if it begins
19324943Seric **			with three digits, this number overrides num.
1948239Seric **		a, b, c, d, e -- printf arguments
1958239Seric **
1968239Seric **	Returns:
1978239Seric **		none
1988239Seric **
1998239Seric **	Side Effects:
2008239Seric **		none.
2018239Seric */
2028239Seric 
2038239Seric /*VARARGS2*/
20458826Seric void
20557642Seric #ifdef __STDC__
20660094Seric nmessage(const char *msg, ...)
20757642Seric #else
20858151Seric nmessage(msg, va_alist)
20960094Seric 	const char *msg;
21057642Seric 	va_dcl
21157642Seric #endif
2128239Seric {
21356852Seric 	VA_LOCAL_DECL
21456852Seric 
2158239Seric 	errno = 0;
21656852Seric 	VA_START(msg);
21758151Seric 	fmtmsg(MsgBuf, (char *) NULL, "050", 0, msg, ap);
21856852Seric 	VA_END;
21963753Seric 	putoutmsg(MsgBuf, FALSE);
2208239Seric }
2218239Seric /*
22263753Seric **  PUTOUTMSG -- output error message to transcript and channel
2237613Seric **
2247613Seric **	Parameters:
2257613Seric **		msg -- message to output (in SMTP format).
2269108Seric **		holdmsg -- if TRUE, don't output a copy of the message to
2279108Seric **			our output channel.
2287613Seric **
2297613Seric **	Returns:
2307613Seric **		none.
2317613Seric **
2327613Seric **	Side Effects:
2337613Seric **		Outputs msg to the transcript.
2347613Seric **		If appropriate, outputs it to the channel.
2357613Seric **		Deletes SMTP reply code number as appropriate.
2367613Seric */
2374711Seric 
23863753Seric putoutmsg(msg, holdmsg)
2397613Seric 	char *msg;
2409108Seric 	bool holdmsg;
2417613Seric {
24214900Seric 	/* output to transcript if serious */
24363848Seric 	if (CurEnv->e_xfp != NULL && strchr("456", msg[0]) != NULL)
24414900Seric 		fprintf(CurEnv->e_xfp, "%s\n", msg);
2454711Seric 
2464711Seric 	/* output to channel if appropriate */
24760421Seric 	if (holdmsg || (!Verbose && msg[0] == '0'))
24860421Seric 		return;
24960421Seric 
25063848Seric 	/* map warnings to something SMTP can handle */
25163848Seric 	if (msg[0] == '6')
25263848Seric 		msg[0] = '5';
25363848Seric 
25460421Seric 	(void) fflush(stdout);
25560421Seric 	if (OpMode == MD_SMTP)
25660421Seric 		fprintf(OutChannel, "%s\r\n", msg);
25760421Seric 	else
25860421Seric 		fprintf(OutChannel, "%s\n", &msg[4]);
25963753Seric 	if (TrafficLogFile != NULL)
26063753Seric 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(),
26163753Seric 			OpMode == MD_SMTP ? msg : &msg[4]);
26260421Seric 	if (msg[3] == ' ')
26360421Seric 		(void) fflush(OutChannel);
26460421Seric 	if (!ferror(OutChannel))
26560421Seric 		return;
26660421Seric 
26760421Seric 	/* error on output -- if reporting lost channel, just ignore it */
26860421Seric 	if (feof(InChannel) || ferror(InChannel))
26960421Seric 		return;
27060421Seric 
27160421Seric 	/* can't call syserr, 'cause we are using MsgBuf */
27260421Seric 	HoldErrs = TRUE;
27360283Seric #ifdef LOG
27460421Seric 	if (LogLevel > 0)
27560421Seric 		syslog(LOG_CRIT,
27663753Seric 			"%s: SYSERR: putoutmsg (%s): error on output channel sending \"%s\"",
27760421Seric 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
278*64123Seric 			CurHostName == NULL ? "NO-HOST" : CurHostName,
279*64123Seric 			msg);
28060283Seric #endif
2819389Seric }
2829389Seric /*
28363753Seric **  PUTERRMSG -- like putoutmsg, but does special processing for error messages
2849389Seric **
2859389Seric **	Parameters:
2869389Seric **		msg -- the message to output.
2879389Seric **
2889389Seric **	Returns:
2899389Seric **		none.
2909389Seric **
2919389Seric **	Side Effects:
2929389Seric **		Sets the fatal error bit in the envelope as appropriate.
2939389Seric */
2948239Seric 
2959389Seric puterrmsg(msg)
2969389Seric 	char *msg;
2979389Seric {
29863848Seric 	char msgcode = msg[0];
29963848Seric 
3009389Seric 	/* output the message as usual */
30163753Seric 	putoutmsg(msg, HoldErrs);
3029389Seric 
3039389Seric 	/* signal the error */
30463848Seric 	if (msgcode == '6')
30563848Seric 	{
30663848Seric 		/* notify the postmaster */
30763848Seric 		CurEnv->e_flags |= EF_PM_NOTIFY;
30863848Seric 	}
30963848Seric 	else
31063848Seric 	{
31163848Seric 		Errors++;
31263848Seric 		if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
31363848Seric 			CurEnv->e_flags |= EF_FATALERRS;
31463848Seric 	}
3154711Seric }
3164711Seric /*
3174711Seric **  FMTMSG -- format a message into buffer.
3184711Seric **
3194711Seric **	Parameters:
3204711Seric **		eb -- error buffer to get result.
3214711Seric **		to -- the recipient tag for this message.
3224711Seric **		num -- arpanet error number.
32316901Seric **		en -- the error number to display.
3244711Seric **		fmt -- format of string.
3254711Seric **		a, b, c, d, e -- arguments.
3264711Seric **
3274711Seric **	Returns:
3284711Seric **		none.
3294711Seric **
3304711Seric **	Side Effects:
3314711Seric **		none.
3324711Seric */
3334063Seric 
33446928Sbostic static void
33556852Seric fmtmsg(eb, to, num, eno, fmt, ap)
3364711Seric 	register char *eb;
3374711Seric 	char *to;
3384711Seric 	char *num;
33916904Seric 	int eno;
3404711Seric 	char *fmt;
34156852Seric 	va_list ap;
3424711Seric {
3434711Seric 	char del;
34459596Seric 	char *meb;
3454711Seric 
3464711Seric 	/* output the reply code */
34724943Seric 	if (isdigit(fmt[0]) && isdigit(fmt[1]) && isdigit(fmt[2]))
3484577Seric 	{
3494711Seric 		num = fmt;
3504711Seric 		fmt += 4;
3514711Seric 	}
3524711Seric 	if (num[3] == '-')
3534711Seric 		del = '-';
3544711Seric 	else
3554711Seric 		del = ' ';
3564711Seric 	(void) sprintf(eb, "%3.3s%c", num, del);
3574711Seric 	eb += 4;
3584063Seric 
3599372Seric 	/* output the file name and line number */
3609372Seric 	if (FileName != NULL)
3619372Seric 	{
3629372Seric 		(void) sprintf(eb, "%s: line %d: ", FileName, LineNumber);
3639372Seric 		eb += strlen(eb);
3649372Seric 	}
3659372Seric 
3664711Seric 	/* output the "to" person */
3674711Seric 	if (to != NULL && to[0] != '\0')
3684711Seric 	{
3694711Seric 		(void) sprintf(eb, "%s... ", to);
3705201Seric 		while (*eb != '\0')
3715201Seric 			*eb++ &= 0177;
3724711Seric 	}
3734711Seric 
37459596Seric 	meb = eb;
37559596Seric 
3764711Seric 	/* output the message */
37756852Seric 	(void) vsprintf(eb, fmt, ap);
3785201Seric 	while (*eb != '\0')
3795201Seric 		*eb++ &= 0177;
3804711Seric 
3814711Seric 	/* output the error code, if any */
38216904Seric 	if (eno != 0)
3834711Seric 	{
38416904Seric 		(void) sprintf(eb, ": %s", errstring(eno));
3854711Seric 		eb += strlen(eb);
3864577Seric 	}
38759596Seric 
38859734Seric 	if (CurEnv->e_message == NULL && strchr("45", num[0]) != NULL)
38959596Seric 		CurEnv->e_message = newstr(meb);
390295Seric }
39115136Seric /*
39215136Seric **  ERRSTRING -- return string description of error code
39315136Seric **
39415136Seric **	Parameters:
39515136Seric **		errno -- the error number to translate
39615136Seric **
39715136Seric **	Returns:
39815136Seric **		A string description of errno.
39915136Seric **
40015136Seric **	Side Effects:
40115136Seric **		none.
40215136Seric */
40315136Seric 
40460089Seric const char *
40515136Seric errstring(errno)
40615136Seric 	int errno;
40715136Seric {
40863839Seric 	static char buf[MAXLINE];
40963839Seric # ifndef ERRLIST_PREDEFINED
41063839Seric 	extern char *sys_errlist[];
41115136Seric 	extern int sys_nerr;
41263839Seric # endif
41324943Seric # ifdef SMTP
41424943Seric 	extern char *SmtpPhase;
41556795Seric # endif /* SMTP */
41615136Seric 
41724943Seric # ifdef DAEMON
41852107Seric # ifdef ETIMEDOUT
41924943Seric 	/*
42024943Seric 	**  Handle special network error codes.
42124943Seric 	**
42224943Seric 	**	These are 4.2/4.3bsd specific; they should be in daemon.c.
42324943Seric 	*/
42424943Seric 
42524943Seric 	switch (errno)
42624943Seric 	{
42724943Seric 	  case ETIMEDOUT:
42824943Seric 	  case ECONNRESET:
42924943Seric 		(void) strcpy(buf, sys_errlist[errno]);
43024943Seric 		if (SmtpPhase != NULL)
43124943Seric 		{
43224943Seric 			(void) strcat(buf, " during ");
43324943Seric 			(void) strcat(buf, SmtpPhase);
43424943Seric 		}
43525050Seric 		if (CurHostName != NULL)
43624943Seric 		{
43724943Seric 			(void) strcat(buf, " with ");
43825050Seric 			(void) strcat(buf, CurHostName);
43924943Seric 		}
44024943Seric 		return (buf);
44124943Seric 
44224943Seric 	  case EHOSTDOWN:
44325050Seric 		if (CurHostName == NULL)
44424943Seric 			break;
44525050Seric 		(void) sprintf(buf, "Host %s is down", CurHostName);
44624943Seric 		return (buf);
44724943Seric 
44824943Seric 	  case ECONNREFUSED:
44925050Seric 		if (CurHostName == NULL)
45024943Seric 			break;
45125050Seric 		(void) sprintf(buf, "Connection refused by %s", CurHostName);
45224943Seric 		return (buf);
45325526Smiriam 
45463993Seric 	  case EOPENTIMEOUT:
45563993Seric 		return "Timeout on file open";
45663993Seric 
45757736Seric # ifdef NAMED_BIND
45863993Seric 	  case HOST_NOT_FOUND + E_DNSBASE:
45958010Seric 		return ("Name server: host not found");
46058010Seric 
46163993Seric 	  case TRY_AGAIN + E_DNSBASE:
46258010Seric 		return ("Name server: host name lookup failure");
46358010Seric 
46463993Seric 	  case NO_RECOVERY + E_DNSBASE:
46558010Seric 		return ("Name server: non-recoverable error");
46658010Seric 
46763993Seric 	  case NO_DATA + E_DNSBASE:
46858010Seric 		return ("Name server: no data known for name");
46957736Seric # endif
47024943Seric 	}
47152107Seric # endif
47252107Seric # endif
47324943Seric 
47415136Seric 	if (errno > 0 && errno < sys_nerr)
47515136Seric 		return (sys_errlist[errno]);
47615136Seric 
47715136Seric 	(void) sprintf(buf, "Error %d", errno);
47815136Seric 	return (buf);
47915136Seric }
480