xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 59156)
122705Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822705Sdist 
922705Sdist #ifndef lint
10*59156Seric static char sccsid[] = "@(#)err.c	6.13 (Berkeley) 04/18/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 
414084Seric # ifdef lint
424084Seric int	sys_nerr;
434084Seric char	*sys_errlist[];
444084Seric # endif lint
4510147Seric char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
464084Seric 
4746928Sbostic static void fmtmsg();
4846928Sbostic 
4958824Seric void
50295Seric /*VARARGS1*/
5157642Seric #ifdef __STDC__
5257642Seric syserr(char *fmt, ...)
5357642Seric #else
5457642Seric syserr(fmt, va_alist)
55295Seric 	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)
94*59156Seric 	{
95*59156Seric #ifdef XLA
96*59156Seric 		xla_all_end();
97*59156Seric #endif
9858690Seric 		exit(EX_OSERR);
99*59156Seric 	}
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__
12357642Seric usrerr(char *fmt, ...)
12457642Seric #else
12557642Seric usrerr(fmt, va_alist)
126295Seric 	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:
1564063Seric **		num -- the default ARPANET error number (in ascii)
1574063Seric **		msg -- the message (printf fmt) -- if it begins
1584063Seric **			with a digit, this number overrides num.
1594063Seric **		a, b, c, d, e -- printf arguments
1604063Seric **
1614063Seric **	Returns:
1624063Seric **		none
1634063Seric **
1644063Seric **	Side Effects:
1654063Seric **		none.
1664063Seric */
1674063Seric 
1684084Seric /*VARARGS2*/
16958826Seric void
17057642Seric #ifdef __STDC__
17158151Seric message(char *msg, ...)
17257642Seric #else
17358151Seric message(msg, va_alist)
17456852Seric 	char *msg;
17557642Seric 	va_dcl
17657642Seric #endif
1774063Seric {
17856852Seric 	VA_LOCAL_DECL
17956852Seric 
1804711Seric 	errno = 0;
18156852Seric 	VA_START(msg);
18258151Seric 	fmtmsg(MsgBuf, CurEnv->e_to, "050", 0, msg, ap);
18356852Seric 	VA_END;
1849108Seric 	putmsg(MsgBuf, FALSE);
1857613Seric }
1867613Seric /*
1878239Seric **  NMESSAGE -- print message (not necessarily an error)
1888239Seric **
1898239Seric **	Just like "message" except it never puts the to... tag on.
1908239Seric **
1918239Seric **	Parameters:
1928239Seric **		num -- the default ARPANET error number (in ascii)
1938239Seric **		msg -- the message (printf fmt) -- if it begins
19424943Seric **			with three digits, this number overrides num.
1958239Seric **		a, b, c, d, e -- printf arguments
1968239Seric **
1978239Seric **	Returns:
1988239Seric **		none
1998239Seric **
2008239Seric **	Side Effects:
2018239Seric **		none.
2028239Seric */
2038239Seric 
2048239Seric /*VARARGS2*/
20558826Seric void
20657642Seric #ifdef __STDC__
20758151Seric nmessage(char *msg, ...)
20857642Seric #else
20958151Seric nmessage(msg, va_alist)
21056852Seric 	char *msg;
21157642Seric 	va_dcl
21257642Seric #endif
2138239Seric {
21456852Seric 	VA_LOCAL_DECL
21556852Seric 
2168239Seric 	errno = 0;
21756852Seric 	VA_START(msg);
21858151Seric 	fmtmsg(MsgBuf, (char *) NULL, "050", 0, msg, ap);
21956852Seric 	VA_END;
2209108Seric 	putmsg(MsgBuf, FALSE);
2218239Seric }
2228239Seric /*
2237613Seric **  PUTMSG -- output error message to transcript and channel
2247613Seric **
2257613Seric **	Parameters:
2267613Seric **		msg -- message to output (in SMTP format).
2279108Seric **		holdmsg -- if TRUE, don't output a copy of the message to
2289108Seric **			our output channel.
2297613Seric **
2307613Seric **	Returns:
2317613Seric **		none.
2327613Seric **
2337613Seric **	Side Effects:
2347613Seric **		Outputs msg to the transcript.
2357613Seric **		If appropriate, outputs it to the channel.
2367613Seric **		Deletes SMTP reply code number as appropriate.
2377613Seric */
2384711Seric 
2399108Seric putmsg(msg, holdmsg)
2407613Seric 	char *msg;
2419108Seric 	bool holdmsg;
2427613Seric {
24314900Seric 	/* output to transcript if serious */
24414900Seric 	if (CurEnv->e_xfp != NULL && (msg[0] == '4' || msg[0] == '5'))
24514900Seric 		fprintf(CurEnv->e_xfp, "%s\n", msg);
2464711Seric 
2474711Seric 	/* output to channel if appropriate */
2489108Seric 	if (!holdmsg && (Verbose || msg[0] != '0'))
2494063Seric 	{
2507275Seric 		(void) fflush(stdout);
25152220Seric 		if (OpMode == MD_SMTP)
2527613Seric 			fprintf(OutChannel, "%s\r\n", msg);
2534711Seric 		else
2547613Seric 			fprintf(OutChannel, "%s\n", &msg[4]);
2554711Seric 		(void) fflush(OutChannel);
25659065Seric 		if (ferror(OutChannel))
25759065Seric 		{
25859065Seric 			HoldErrs = TRUE;
25959065Seric 			syserr("putmsg: error on output channel");
26059065Seric 		}
2614063Seric 	}
2629389Seric }
2639389Seric /*
2649389Seric **  PUTERRMSG -- like putmsg, but does special processing for error messages
2659389Seric **
2669389Seric **	Parameters:
2679389Seric **		msg -- the message to output.
2689389Seric **
2699389Seric **	Returns:
2709389Seric **		none.
2719389Seric **
2729389Seric **	Side Effects:
2739389Seric **		Sets the fatal error bit in the envelope as appropriate.
2749389Seric */
2758239Seric 
2769389Seric puterrmsg(msg)
2779389Seric 	char *msg;
2789389Seric {
2799389Seric 	/* output the message as usual */
2809389Seric 	putmsg(msg, HoldErrs);
2819389Seric 
2829389Seric 	/* signal the error */
2839389Seric 	Errors++;
2849389Seric 	if (msg[0] == '5')
2859336Seric 		CurEnv->e_flags |= EF_FATALERRS;
2864711Seric }
2874711Seric /*
2884711Seric **  FMTMSG -- format a message into buffer.
2894711Seric **
2904711Seric **	Parameters:
2914711Seric **		eb -- error buffer to get result.
2924711Seric **		to -- the recipient tag for this message.
2934711Seric **		num -- arpanet error number.
29416901Seric **		en -- the error number to display.
2954711Seric **		fmt -- format of string.
2964711Seric **		a, b, c, d, e -- arguments.
2974711Seric **
2984711Seric **	Returns:
2994711Seric **		none.
3004711Seric **
3014711Seric **	Side Effects:
3024711Seric **		none.
3034711Seric */
3044063Seric 
30546928Sbostic static void
30656852Seric fmtmsg(eb, to, num, eno, fmt, ap)
3074711Seric 	register char *eb;
3084711Seric 	char *to;
3094711Seric 	char *num;
31016904Seric 	int eno;
3114711Seric 	char *fmt;
31256852Seric 	va_list ap;
3134711Seric {
3144711Seric 	char del;
3154711Seric 
3164711Seric 	/* output the reply code */
31724943Seric 	if (isdigit(fmt[0]) && isdigit(fmt[1]) && isdigit(fmt[2]))
3184577Seric 	{
3194711Seric 		num = fmt;
3204711Seric 		fmt += 4;
3214711Seric 	}
3224711Seric 	if (num[3] == '-')
3234711Seric 		del = '-';
3244711Seric 	else
3254711Seric 		del = ' ';
3264711Seric 	(void) sprintf(eb, "%3.3s%c", num, del);
3274711Seric 	eb += 4;
3284063Seric 
3299372Seric 	/* output the file name and line number */
3309372Seric 	if (FileName != NULL)
3319372Seric 	{
3329372Seric 		(void) sprintf(eb, "%s: line %d: ", FileName, LineNumber);
3339372Seric 		eb += strlen(eb);
3349372Seric 	}
3359372Seric 
3364711Seric 	/* output the "to" person */
3374711Seric 	if (to != NULL && to[0] != '\0')
3384711Seric 	{
3394711Seric 		(void) sprintf(eb, "%s... ", to);
3405201Seric 		while (*eb != '\0')
3415201Seric 			*eb++ &= 0177;
3424711Seric 	}
3434711Seric 
3444711Seric 	/* output the message */
34556852Seric 	(void) vsprintf(eb, fmt, ap);
3465201Seric 	while (*eb != '\0')
3475201Seric 		*eb++ &= 0177;
3484711Seric 
3494711Seric 	/* output the error code, if any */
35016904Seric 	if (eno != 0)
3514711Seric 	{
35215136Seric 		extern char *errstring();
35315136Seric 
35416904Seric 		(void) sprintf(eb, ": %s", errstring(eno));
3554711Seric 		eb += strlen(eb);
3564577Seric 	}
357295Seric }
35815136Seric /*
35915136Seric **  ERRSTRING -- return string description of error code
36015136Seric **
36115136Seric **	Parameters:
36215136Seric **		errno -- the error number to translate
36315136Seric **
36415136Seric **	Returns:
36515136Seric **		A string description of errno.
36615136Seric **
36715136Seric **	Side Effects:
36815136Seric **		none.
36915136Seric */
37015136Seric 
37115136Seric char *
37215136Seric errstring(errno)
37315136Seric 	int errno;
37415136Seric {
37515136Seric 	extern char *sys_errlist[];
37615136Seric 	extern int sys_nerr;
37757232Seric 	static char buf[MAXLINE];
37824943Seric # ifdef SMTP
37924943Seric 	extern char *SmtpPhase;
38056795Seric # endif /* SMTP */
38115136Seric 
38224943Seric # ifdef DAEMON
38352107Seric # ifdef ETIMEDOUT
38424943Seric 	/*
38524943Seric 	**  Handle special network error codes.
38624943Seric 	**
38724943Seric 	**	These are 4.2/4.3bsd specific; they should be in daemon.c.
38824943Seric 	*/
38924943Seric 
39024943Seric 	switch (errno)
39124943Seric 	{
39224943Seric 	  case ETIMEDOUT:
39324943Seric 	  case ECONNRESET:
39424943Seric 		(void) strcpy(buf, sys_errlist[errno]);
39524943Seric 		if (SmtpPhase != NULL)
39624943Seric 		{
39724943Seric 			(void) strcat(buf, " during ");
39824943Seric 			(void) strcat(buf, SmtpPhase);
39924943Seric 		}
40025050Seric 		if (CurHostName != NULL)
40124943Seric 		{
40224943Seric 			(void) strcat(buf, " with ");
40325050Seric 			(void) strcat(buf, CurHostName);
40424943Seric 		}
40524943Seric 		return (buf);
40624943Seric 
40724943Seric 	  case EHOSTDOWN:
40825050Seric 		if (CurHostName == NULL)
40924943Seric 			break;
41025050Seric 		(void) sprintf(buf, "Host %s is down", CurHostName);
41124943Seric 		return (buf);
41224943Seric 
41324943Seric 	  case ECONNREFUSED:
41425050Seric 		if (CurHostName == NULL)
41524943Seric 			break;
41625050Seric 		(void) sprintf(buf, "Connection refused by %s", CurHostName);
41724943Seric 		return (buf);
41825526Smiriam 
41957736Seric # ifdef NAMED_BIND
42058010Seric 	  case HOST_NOT_FOUND + MAX_ERRNO:
42158010Seric 		return ("Name server: host not found");
42258010Seric 
42358010Seric 	  case TRY_AGAIN + MAX_ERRNO:
42458010Seric 		return ("Name server: host name lookup failure");
42558010Seric 
42658010Seric 	  case NO_RECOVERY + MAX_ERRNO:
42758010Seric 		return ("Name server: non-recoverable error");
42858010Seric 
42958010Seric 	  case NO_DATA + MAX_ERRNO:
43058010Seric 		return ("Name server: no data known for name");
43157736Seric # endif
43224943Seric 	}
43352107Seric # endif
43452107Seric # endif
43524943Seric 
43615136Seric 	if (errno > 0 && errno < sys_nerr)
43715136Seric 		return (sys_errlist[errno]);
43815136Seric 
43915136Seric 	(void) sprintf(buf, "Error %d", errno);
44015136Seric 	return (buf);
44115136Seric }
442