xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 64731)
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*64731Seric static char sccsid[] = "@(#)err.c	8.12 (Berkeley) 10/21/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 */
9364725Seric 	if (olderrno == EMFILE)
94*64731Seric 	{
9564725Seric 		printopenfds(TRUE);
96*64731Seric 		mci_dump_all(TRUE);
97*64731Seric 	}
9858690Seric 	if (panic)
9959156Seric 	{
10059156Seric #ifdef XLA
10159156Seric 		xla_all_end();
10259156Seric #endif
10358690Seric 		exit(EX_OSERR);
10459156Seric 	}
105295Seric 	errno = 0;
1067762Seric 	if (QuickAbort)
1077762Seric 		longjmp(TopFrame, 2);
108295Seric }
109295Seric /*
110295Seric **  USRERR -- Signal user error.
111295Seric **
112295Seric **	This is much like syserr except it is for user errors.
113295Seric **
114295Seric **	Parameters:
115295Seric **		fmt, a, b, c, d -- printf strings
116295Seric **
117295Seric **	Returns:
1184084Seric **		none
1197762Seric **		Through TopFrame if QuickAbort is set.
120295Seric **
121295Seric **	Side Effects:
1221514Seric **		increments Errors.
123295Seric */
124295Seric 
125295Seric /*VARARGS1*/
12658824Seric void
12757642Seric #ifdef __STDC__
12860094Seric usrerr(const char *fmt, ...)
12957642Seric #else
13057642Seric usrerr(fmt, va_alist)
13160094Seric 	const char *fmt;
13257642Seric 	va_dcl
13357642Seric #endif
134295Seric {
13556852Seric 	VA_LOCAL_DECL
136295Seric 	extern char SuprErrs;
13716901Seric 	extern int errno;
138295Seric 
139295Seric 	if (SuprErrs)
1404084Seric 		return;
141295Seric 
14256852Seric 	VA_START(fmt);
14358524Seric 	fmtmsg(MsgBuf, CurEnv->e_to, "501", 0, fmt, ap);
14456852Seric 	VA_END;
1459389Seric 	puterrmsg(MsgBuf);
1468239Seric 
14751951Seric # ifdef LOG
14858020Seric 	if (LogLevel > 3 && LogUsrErrs)
14951951Seric 		syslog(LOG_NOTICE, "%s: %s",
15051951Seric 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
15151951Seric 			&MsgBuf[4]);
15256795Seric # endif /* LOG */
15351951Seric 
1547762Seric 	if (QuickAbort)
1557762Seric 		longjmp(TopFrame, 1);
1564063Seric }
1574063Seric /*
1584063Seric **  MESSAGE -- print message (not necessarily an error)
1594063Seric **
1604063Seric **	Parameters:
16159581Seric **		msg -- the message (printf fmt) -- it can begin with
16259581Seric **			an SMTP reply code.  If not, 050 is assumed.
1634063Seric **		a, b, c, d, e -- printf arguments
1644063Seric **
1654063Seric **	Returns:
1664063Seric **		none
1674063Seric **
1684063Seric **	Side Effects:
1694063Seric **		none.
1704063Seric */
1714063Seric 
1724084Seric /*VARARGS2*/
17358826Seric void
17457642Seric #ifdef __STDC__
17560094Seric message(const char *msg, ...)
17657642Seric #else
17758151Seric message(msg, va_alist)
17860094Seric 	const char *msg;
17957642Seric 	va_dcl
18057642Seric #endif
1814063Seric {
18256852Seric 	VA_LOCAL_DECL
18356852Seric 
1844711Seric 	errno = 0;
18556852Seric 	VA_START(msg);
18658151Seric 	fmtmsg(MsgBuf, CurEnv->e_to, "050", 0, msg, ap);
18756852Seric 	VA_END;
18863753Seric 	putoutmsg(MsgBuf, FALSE);
1897613Seric }
1907613Seric /*
1918239Seric **  NMESSAGE -- print message (not necessarily an error)
1928239Seric **
1938239Seric **	Just like "message" except it never puts the to... tag on.
1948239Seric **
1958239Seric **	Parameters:
1968239Seric **		num -- the default ARPANET error number (in ascii)
1978239Seric **		msg -- the message (printf fmt) -- if it begins
19824943Seric **			with three digits, this number overrides num.
1998239Seric **		a, b, c, d, e -- printf arguments
2008239Seric **
2018239Seric **	Returns:
2028239Seric **		none
2038239Seric **
2048239Seric **	Side Effects:
2058239Seric **		none.
2068239Seric */
2078239Seric 
2088239Seric /*VARARGS2*/
20958826Seric void
21057642Seric #ifdef __STDC__
21160094Seric nmessage(const char *msg, ...)
21257642Seric #else
21358151Seric nmessage(msg, va_alist)
21460094Seric 	const char *msg;
21557642Seric 	va_dcl
21657642Seric #endif
2178239Seric {
21856852Seric 	VA_LOCAL_DECL
21956852Seric 
2208239Seric 	errno = 0;
22156852Seric 	VA_START(msg);
22258151Seric 	fmtmsg(MsgBuf, (char *) NULL, "050", 0, msg, ap);
22356852Seric 	VA_END;
22463753Seric 	putoutmsg(MsgBuf, FALSE);
2258239Seric }
2268239Seric /*
22763753Seric **  PUTOUTMSG -- output error message to transcript and channel
2287613Seric **
2297613Seric **	Parameters:
2307613Seric **		msg -- message to output (in SMTP format).
2319108Seric **		holdmsg -- if TRUE, don't output a copy of the message to
2329108Seric **			our output channel.
2337613Seric **
2347613Seric **	Returns:
2357613Seric **		none.
2367613Seric **
2377613Seric **	Side Effects:
2387613Seric **		Outputs msg to the transcript.
2397613Seric **		If appropriate, outputs it to the channel.
2407613Seric **		Deletes SMTP reply code number as appropriate.
2417613Seric */
2424711Seric 
24363753Seric putoutmsg(msg, holdmsg)
2447613Seric 	char *msg;
2459108Seric 	bool holdmsg;
2467613Seric {
24764249Seric 	/* display for debugging */
24864249Seric 	if (tTd(54, 8))
24964249Seric 		printf("--- %s%s\n", msg, holdmsg ? " (held)" : "");
25064249Seric 
25114900Seric 	/* output to transcript if serious */
25263848Seric 	if (CurEnv->e_xfp != NULL && strchr("456", msg[0]) != NULL)
25314900Seric 		fprintf(CurEnv->e_xfp, "%s\n", msg);
2544711Seric 
2554711Seric 	/* output to channel if appropriate */
25660421Seric 	if (holdmsg || (!Verbose && msg[0] == '0'))
25760421Seric 		return;
25860421Seric 
25963848Seric 	/* map warnings to something SMTP can handle */
26063848Seric 	if (msg[0] == '6')
26163848Seric 		msg[0] = '5';
26263848Seric 
26360421Seric 	(void) fflush(stdout);
26460421Seric 	if (OpMode == MD_SMTP)
26560421Seric 		fprintf(OutChannel, "%s\r\n", msg);
26660421Seric 	else
26760421Seric 		fprintf(OutChannel, "%s\n", &msg[4]);
26863753Seric 	if (TrafficLogFile != NULL)
26963753Seric 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(),
27063753Seric 			OpMode == MD_SMTP ? msg : &msg[4]);
27160421Seric 	if (msg[3] == ' ')
27260421Seric 		(void) fflush(OutChannel);
27360421Seric 	if (!ferror(OutChannel))
27460421Seric 		return;
27560421Seric 
27664499Seric 	/*
27764499Seric 	**  Error on output -- if reporting lost channel, just ignore it.
27864499Seric 	**  Also, ignore errors from QUIT response (221 message) -- some
27964499Seric 	**	rude servers don't read result.
28064499Seric 	*/
28164499Seric 
28264499Seric 	if (feof(InChannel) || ferror(InChannel) || strncmp(msg, "221", 3) == 0)
28360421Seric 		return;
28460421Seric 
28560421Seric 	/* can't call syserr, 'cause we are using MsgBuf */
28660421Seric 	HoldErrs = TRUE;
28760283Seric #ifdef LOG
28860421Seric 	if (LogLevel > 0)
28960421Seric 		syslog(LOG_CRIT,
29064499Seric 			"%s: SYSERR: putoutmsg (%s): error on output channel sending \"%s\": %m",
29160421Seric 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
29264123Seric 			CurHostName == NULL ? "NO-HOST" : CurHostName,
29364123Seric 			msg);
29460283Seric #endif
2959389Seric }
2969389Seric /*
29763753Seric **  PUTERRMSG -- like putoutmsg, but does special processing for error messages
2989389Seric **
2999389Seric **	Parameters:
3009389Seric **		msg -- the message to output.
3019389Seric **
3029389Seric **	Returns:
3039389Seric **		none.
3049389Seric **
3059389Seric **	Side Effects:
3069389Seric **		Sets the fatal error bit in the envelope as appropriate.
3079389Seric */
3088239Seric 
3099389Seric puterrmsg(msg)
3109389Seric 	char *msg;
3119389Seric {
31263848Seric 	char msgcode = msg[0];
31363848Seric 
3149389Seric 	/* output the message as usual */
31563753Seric 	putoutmsg(msg, HoldErrs);
3169389Seric 
3179389Seric 	/* signal the error */
31863848Seric 	if (msgcode == '6')
31963848Seric 	{
32063848Seric 		/* notify the postmaster */
32163848Seric 		CurEnv->e_flags |= EF_PM_NOTIFY;
32263848Seric 	}
32363848Seric 	else
32463848Seric 	{
32563848Seric 		Errors++;
32663848Seric 		if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
32763848Seric 			CurEnv->e_flags |= EF_FATALERRS;
32863848Seric 	}
3294711Seric }
3304711Seric /*
3314711Seric **  FMTMSG -- format a message into buffer.
3324711Seric **
3334711Seric **	Parameters:
3344711Seric **		eb -- error buffer to get result.
3354711Seric **		to -- the recipient tag for this message.
3364711Seric **		num -- arpanet error number.
33716901Seric **		en -- the error number to display.
3384711Seric **		fmt -- format of string.
3394711Seric **		a, b, c, d, e -- arguments.
3404711Seric **
3414711Seric **	Returns:
3424711Seric **		none.
3434711Seric **
3444711Seric **	Side Effects:
3454711Seric **		none.
3464711Seric */
3474063Seric 
34846928Sbostic static void
34956852Seric fmtmsg(eb, to, num, eno, fmt, ap)
3504711Seric 	register char *eb;
3514711Seric 	char *to;
3524711Seric 	char *num;
35316904Seric 	int eno;
3544711Seric 	char *fmt;
35556852Seric 	va_list ap;
3564711Seric {
3574711Seric 	char del;
35859596Seric 	char *meb;
3594711Seric 
3604711Seric 	/* output the reply code */
36124943Seric 	if (isdigit(fmt[0]) && isdigit(fmt[1]) && isdigit(fmt[2]))
3624577Seric 	{
3634711Seric 		num = fmt;
3644711Seric 		fmt += 4;
3654711Seric 	}
3664711Seric 	if (num[3] == '-')
3674711Seric 		del = '-';
3684711Seric 	else
3694711Seric 		del = ' ';
3704711Seric 	(void) sprintf(eb, "%3.3s%c", num, del);
3714711Seric 	eb += 4;
3724063Seric 
3739372Seric 	/* output the file name and line number */
3749372Seric 	if (FileName != NULL)
3759372Seric 	{
3769372Seric 		(void) sprintf(eb, "%s: line %d: ", FileName, LineNumber);
3779372Seric 		eb += strlen(eb);
3789372Seric 	}
3799372Seric 
3804711Seric 	/* output the "to" person */
3814711Seric 	if (to != NULL && to[0] != '\0')
3824711Seric 	{
3834711Seric 		(void) sprintf(eb, "%s... ", to);
3845201Seric 		while (*eb != '\0')
3855201Seric 			*eb++ &= 0177;
3864711Seric 	}
3874711Seric 
38859596Seric 	meb = eb;
38959596Seric 
3904711Seric 	/* output the message */
39156852Seric 	(void) vsprintf(eb, fmt, ap);
3925201Seric 	while (*eb != '\0')
3935201Seric 		*eb++ &= 0177;
3944711Seric 
3954711Seric 	/* output the error code, if any */
39616904Seric 	if (eno != 0)
3974711Seric 	{
39816904Seric 		(void) sprintf(eb, ": %s", errstring(eno));
3994711Seric 		eb += strlen(eb);
4004577Seric 	}
40159596Seric 
40264695Seric 	if (num[0] == '5' || (CurEnv->e_message == NULL && num[0] == '4'))
40364695Seric 	{
40464695Seric 		if (CurEnv->e_message != NULL)
40564695Seric 			free(CurEnv->e_message);
40659596Seric 		CurEnv->e_message = newstr(meb);
40764695Seric 	}
408295Seric }
40915136Seric /*
41015136Seric **  ERRSTRING -- return string description of error code
41115136Seric **
41215136Seric **	Parameters:
41315136Seric **		errno -- the error number to translate
41415136Seric **
41515136Seric **	Returns:
41615136Seric **		A string description of errno.
41715136Seric **
41815136Seric **	Side Effects:
41915136Seric **		none.
42015136Seric */
42115136Seric 
42260089Seric const char *
42315136Seric errstring(errno)
42415136Seric 	int errno;
42515136Seric {
42663839Seric 	static char buf[MAXLINE];
42763839Seric # ifndef ERRLIST_PREDEFINED
42863839Seric 	extern char *sys_errlist[];
42915136Seric 	extern int sys_nerr;
43063839Seric # endif
43124943Seric # ifdef SMTP
43224943Seric 	extern char *SmtpPhase;
43356795Seric # endif /* SMTP */
43415136Seric 
43524943Seric # ifdef DAEMON
43652107Seric # ifdef ETIMEDOUT
43724943Seric 	/*
43824943Seric 	**  Handle special network error codes.
43924943Seric 	**
44024943Seric 	**	These are 4.2/4.3bsd specific; they should be in daemon.c.
44124943Seric 	*/
44224943Seric 
44324943Seric 	switch (errno)
44424943Seric 	{
44524943Seric 	  case ETIMEDOUT:
44624943Seric 	  case ECONNRESET:
44724943Seric 		(void) strcpy(buf, sys_errlist[errno]);
44824943Seric 		if (SmtpPhase != NULL)
44924943Seric 		{
45024943Seric 			(void) strcat(buf, " during ");
45124943Seric 			(void) strcat(buf, SmtpPhase);
45224943Seric 		}
45325050Seric 		if (CurHostName != NULL)
45424943Seric 		{
45524943Seric 			(void) strcat(buf, " with ");
45625050Seric 			(void) strcat(buf, CurHostName);
45724943Seric 		}
45824943Seric 		return (buf);
45924943Seric 
46024943Seric 	  case EHOSTDOWN:
46125050Seric 		if (CurHostName == NULL)
46224943Seric 			break;
46325050Seric 		(void) sprintf(buf, "Host %s is down", CurHostName);
46424943Seric 		return (buf);
46524943Seric 
46624943Seric 	  case ECONNREFUSED:
46725050Seric 		if (CurHostName == NULL)
46824943Seric 			break;
46925050Seric 		(void) sprintf(buf, "Connection refused by %s", CurHostName);
47024943Seric 		return (buf);
47125526Smiriam 
47263993Seric 	  case EOPENTIMEOUT:
47363993Seric 		return "Timeout on file open";
47463993Seric 
47557736Seric # ifdef NAMED_BIND
47663993Seric 	  case HOST_NOT_FOUND + E_DNSBASE:
47758010Seric 		return ("Name server: host not found");
47858010Seric 
47963993Seric 	  case TRY_AGAIN + E_DNSBASE:
48058010Seric 		return ("Name server: host name lookup failure");
48158010Seric 
48263993Seric 	  case NO_RECOVERY + E_DNSBASE:
48358010Seric 		return ("Name server: non-recoverable error");
48458010Seric 
48563993Seric 	  case NO_DATA + E_DNSBASE:
48658010Seric 		return ("Name server: no data known for name");
48757736Seric # endif
48824943Seric 	}
48952107Seric # endif
49052107Seric # endif
49124943Seric 
49215136Seric 	if (errno > 0 && errno < sys_nerr)
49315136Seric 		return (sys_errlist[errno]);
49415136Seric 
49515136Seric 	(void) sprintf(buf, "Error %d", errno);
49615136Seric 	return (buf);
49715136Seric }
498