xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 64123)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)err.c	8.7 (Berkeley) 08/06/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <errno.h>
15 # include <netdb.h>
16 
17 /*
18 **  SYSERR -- Print error message.
19 **
20 **	Prints an error message via printf to the diagnostic
21 **	output.  If LOG is defined, it logs it also.
22 **
23 **	If the first character of the syserr message is `!' it will
24 **	log this as an ALERT message and exit immediately.  This can
25 **	leave queue files in an indeterminate state, so it should not
26 **	be used lightly.
27 **
28 **	Parameters:
29 **		f -- the format string
30 **		a, b, c, d, e -- parameters
31 **
32 **	Returns:
33 **		none
34 **		Through TopFrame if QuickAbort is set.
35 **
36 **	Side Effects:
37 **		increments Errors.
38 **		sets ExitStat.
39 */
40 
41 char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
42 
43 static void	fmtmsg();
44 
45 #if defined(NAMED_BIND) && !defined(NO_DATA)
46 # define NO_DATA	NO_ADDRESS
47 #endif
48 
49 void
50 /*VARARGS1*/
51 #ifdef __STDC__
52 syserr(const char *fmt, ...)
53 #else
54 syserr(fmt, va_alist)
55 	const char *fmt;
56 	va_dcl
57 #endif
58 {
59 	register char *p;
60 	int olderrno = errno;
61 	bool panic;
62 	VA_LOCAL_DECL
63 
64 	panic = *fmt == '!';
65 	if (panic)
66 		fmt++;
67 
68 	/* format and output the error message */
69 	if (olderrno == 0)
70 		p = "554";
71 	else
72 		p = "451";
73 	VA_START(fmt);
74 	fmtmsg(MsgBuf, (char *) NULL, p, olderrno, fmt, ap);
75 	VA_END;
76 	puterrmsg(MsgBuf);
77 
78 	/* determine exit status if not already set */
79 	if (ExitStat == EX_OK)
80 	{
81 		if (olderrno == 0)
82 			ExitStat = EX_SOFTWARE;
83 		else
84 			ExitStat = EX_OSERR;
85 	}
86 
87 # ifdef LOG
88 	if (LogLevel > 0)
89 		syslog(panic ? LOG_ALERT : LOG_CRIT, "%s: SYSERR: %s",
90 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
91 			&MsgBuf[4]);
92 # endif /* LOG */
93 	if (panic)
94 	{
95 #ifdef XLA
96 		xla_all_end();
97 #endif
98 		exit(EX_OSERR);
99 	}
100 	errno = 0;
101 	if (QuickAbort)
102 		longjmp(TopFrame, 2);
103 }
104 /*
105 **  USRERR -- Signal user error.
106 **
107 **	This is much like syserr except it is for user errors.
108 **
109 **	Parameters:
110 **		fmt, a, b, c, d -- printf strings
111 **
112 **	Returns:
113 **		none
114 **		Through TopFrame if QuickAbort is set.
115 **
116 **	Side Effects:
117 **		increments Errors.
118 */
119 
120 /*VARARGS1*/
121 void
122 #ifdef __STDC__
123 usrerr(const char *fmt, ...)
124 #else
125 usrerr(fmt, va_alist)
126 	const char *fmt;
127 	va_dcl
128 #endif
129 {
130 	VA_LOCAL_DECL
131 	extern char SuprErrs;
132 	extern int errno;
133 
134 	if (SuprErrs)
135 		return;
136 
137 	VA_START(fmt);
138 	fmtmsg(MsgBuf, CurEnv->e_to, "501", 0, fmt, ap);
139 	VA_END;
140 	puterrmsg(MsgBuf);
141 
142 # ifdef LOG
143 	if (LogLevel > 3 && LogUsrErrs)
144 		syslog(LOG_NOTICE, "%s: %s",
145 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
146 			&MsgBuf[4]);
147 # endif /* LOG */
148 
149 	if (QuickAbort)
150 		longjmp(TopFrame, 1);
151 }
152 /*
153 **  MESSAGE -- print message (not necessarily an error)
154 **
155 **	Parameters:
156 **		msg -- the message (printf fmt) -- it can begin with
157 **			an SMTP reply code.  If not, 050 is assumed.
158 **		a, b, c, d, e -- printf arguments
159 **
160 **	Returns:
161 **		none
162 **
163 **	Side Effects:
164 **		none.
165 */
166 
167 /*VARARGS2*/
168 void
169 #ifdef __STDC__
170 message(const char *msg, ...)
171 #else
172 message(msg, va_alist)
173 	const char *msg;
174 	va_dcl
175 #endif
176 {
177 	VA_LOCAL_DECL
178 
179 	errno = 0;
180 	VA_START(msg);
181 	fmtmsg(MsgBuf, CurEnv->e_to, "050", 0, msg, ap);
182 	VA_END;
183 	putoutmsg(MsgBuf, FALSE);
184 }
185 /*
186 **  NMESSAGE -- print message (not necessarily an error)
187 **
188 **	Just like "message" except it never puts the to... tag on.
189 **
190 **	Parameters:
191 **		num -- the default ARPANET error number (in ascii)
192 **		msg -- the message (printf fmt) -- if it begins
193 **			with three digits, this number overrides num.
194 **		a, b, c, d, e -- printf arguments
195 **
196 **	Returns:
197 **		none
198 **
199 **	Side Effects:
200 **		none.
201 */
202 
203 /*VARARGS2*/
204 void
205 #ifdef __STDC__
206 nmessage(const char *msg, ...)
207 #else
208 nmessage(msg, va_alist)
209 	const char *msg;
210 	va_dcl
211 #endif
212 {
213 	VA_LOCAL_DECL
214 
215 	errno = 0;
216 	VA_START(msg);
217 	fmtmsg(MsgBuf, (char *) NULL, "050", 0, msg, ap);
218 	VA_END;
219 	putoutmsg(MsgBuf, FALSE);
220 }
221 /*
222 **  PUTOUTMSG -- output error message to transcript and channel
223 **
224 **	Parameters:
225 **		msg -- message to output (in SMTP format).
226 **		holdmsg -- if TRUE, don't output a copy of the message to
227 **			our output channel.
228 **
229 **	Returns:
230 **		none.
231 **
232 **	Side Effects:
233 **		Outputs msg to the transcript.
234 **		If appropriate, outputs it to the channel.
235 **		Deletes SMTP reply code number as appropriate.
236 */
237 
238 putoutmsg(msg, holdmsg)
239 	char *msg;
240 	bool holdmsg;
241 {
242 	/* output to transcript if serious */
243 	if (CurEnv->e_xfp != NULL && strchr("456", msg[0]) != NULL)
244 		fprintf(CurEnv->e_xfp, "%s\n", msg);
245 
246 	/* output to channel if appropriate */
247 	if (holdmsg || (!Verbose && msg[0] == '0'))
248 		return;
249 
250 	/* map warnings to something SMTP can handle */
251 	if (msg[0] == '6')
252 		msg[0] = '5';
253 
254 	(void) fflush(stdout);
255 	if (OpMode == MD_SMTP)
256 		fprintf(OutChannel, "%s\r\n", msg);
257 	else
258 		fprintf(OutChannel, "%s\n", &msg[4]);
259 	if (TrafficLogFile != NULL)
260 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(),
261 			OpMode == MD_SMTP ? msg : &msg[4]);
262 	if (msg[3] == ' ')
263 		(void) fflush(OutChannel);
264 	if (!ferror(OutChannel))
265 		return;
266 
267 	/* error on output -- if reporting lost channel, just ignore it */
268 	if (feof(InChannel) || ferror(InChannel))
269 		return;
270 
271 	/* can't call syserr, 'cause we are using MsgBuf */
272 	HoldErrs = TRUE;
273 #ifdef LOG
274 	if (LogLevel > 0)
275 		syslog(LOG_CRIT,
276 			"%s: SYSERR: putoutmsg (%s): error on output channel sending \"%s\"",
277 			CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id,
278 			CurHostName == NULL ? "NO-HOST" : CurHostName,
279 			msg);
280 #endif
281 }
282 /*
283 **  PUTERRMSG -- like putoutmsg, but does special processing for error messages
284 **
285 **	Parameters:
286 **		msg -- the message to output.
287 **
288 **	Returns:
289 **		none.
290 **
291 **	Side Effects:
292 **		Sets the fatal error bit in the envelope as appropriate.
293 */
294 
295 puterrmsg(msg)
296 	char *msg;
297 {
298 	char msgcode = msg[0];
299 
300 	/* output the message as usual */
301 	putoutmsg(msg, HoldErrs);
302 
303 	/* signal the error */
304 	if (msgcode == '6')
305 	{
306 		/* notify the postmaster */
307 		CurEnv->e_flags |= EF_PM_NOTIFY;
308 	}
309 	else
310 	{
311 		Errors++;
312 		if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
313 			CurEnv->e_flags |= EF_FATALERRS;
314 	}
315 }
316 /*
317 **  FMTMSG -- format a message into buffer.
318 **
319 **	Parameters:
320 **		eb -- error buffer to get result.
321 **		to -- the recipient tag for this message.
322 **		num -- arpanet error number.
323 **		en -- the error number to display.
324 **		fmt -- format of string.
325 **		a, b, c, d, e -- arguments.
326 **
327 **	Returns:
328 **		none.
329 **
330 **	Side Effects:
331 **		none.
332 */
333 
334 static void
335 fmtmsg(eb, to, num, eno, fmt, ap)
336 	register char *eb;
337 	char *to;
338 	char *num;
339 	int eno;
340 	char *fmt;
341 	va_list ap;
342 {
343 	char del;
344 	char *meb;
345 
346 	/* output the reply code */
347 	if (isdigit(fmt[0]) && isdigit(fmt[1]) && isdigit(fmt[2]))
348 	{
349 		num = fmt;
350 		fmt += 4;
351 	}
352 	if (num[3] == '-')
353 		del = '-';
354 	else
355 		del = ' ';
356 	(void) sprintf(eb, "%3.3s%c", num, del);
357 	eb += 4;
358 
359 	/* output the file name and line number */
360 	if (FileName != NULL)
361 	{
362 		(void) sprintf(eb, "%s: line %d: ", FileName, LineNumber);
363 		eb += strlen(eb);
364 	}
365 
366 	/* output the "to" person */
367 	if (to != NULL && to[0] != '\0')
368 	{
369 		(void) sprintf(eb, "%s... ", to);
370 		while (*eb != '\0')
371 			*eb++ &= 0177;
372 	}
373 
374 	meb = eb;
375 
376 	/* output the message */
377 	(void) vsprintf(eb, fmt, ap);
378 	while (*eb != '\0')
379 		*eb++ &= 0177;
380 
381 	/* output the error code, if any */
382 	if (eno != 0)
383 	{
384 		(void) sprintf(eb, ": %s", errstring(eno));
385 		eb += strlen(eb);
386 	}
387 
388 	if (CurEnv->e_message == NULL && strchr("45", num[0]) != NULL)
389 		CurEnv->e_message = newstr(meb);
390 }
391 /*
392 **  ERRSTRING -- return string description of error code
393 **
394 **	Parameters:
395 **		errno -- the error number to translate
396 **
397 **	Returns:
398 **		A string description of errno.
399 **
400 **	Side Effects:
401 **		none.
402 */
403 
404 const char *
405 errstring(errno)
406 	int errno;
407 {
408 	static char buf[MAXLINE];
409 # ifndef ERRLIST_PREDEFINED
410 	extern char *sys_errlist[];
411 	extern int sys_nerr;
412 # endif
413 # ifdef SMTP
414 	extern char *SmtpPhase;
415 # endif /* SMTP */
416 
417 # ifdef DAEMON
418 # ifdef ETIMEDOUT
419 	/*
420 	**  Handle special network error codes.
421 	**
422 	**	These are 4.2/4.3bsd specific; they should be in daemon.c.
423 	*/
424 
425 	switch (errno)
426 	{
427 	  case ETIMEDOUT:
428 	  case ECONNRESET:
429 		(void) strcpy(buf, sys_errlist[errno]);
430 		if (SmtpPhase != NULL)
431 		{
432 			(void) strcat(buf, " during ");
433 			(void) strcat(buf, SmtpPhase);
434 		}
435 		if (CurHostName != NULL)
436 		{
437 			(void) strcat(buf, " with ");
438 			(void) strcat(buf, CurHostName);
439 		}
440 		return (buf);
441 
442 	  case EHOSTDOWN:
443 		if (CurHostName == NULL)
444 			break;
445 		(void) sprintf(buf, "Host %s is down", CurHostName);
446 		return (buf);
447 
448 	  case ECONNREFUSED:
449 		if (CurHostName == NULL)
450 			break;
451 		(void) sprintf(buf, "Connection refused by %s", CurHostName);
452 		return (buf);
453 
454 	  case EOPENTIMEOUT:
455 		return "Timeout on file open";
456 
457 # ifdef NAMED_BIND
458 	  case HOST_NOT_FOUND + E_DNSBASE:
459 		return ("Name server: host not found");
460 
461 	  case TRY_AGAIN + E_DNSBASE:
462 		return ("Name server: host name lookup failure");
463 
464 	  case NO_RECOVERY + E_DNSBASE:
465 		return ("Name server: non-recoverable error");
466 
467 	  case NO_DATA + E_DNSBASE:
468 		return ("Name server: no data known for name");
469 # endif
470 	}
471 # endif
472 # endif
473 
474 	if (errno > 0 && errno < sys_nerr)
475 		return (sys_errlist[errno]);
476 
477 	(void) sprintf(buf, "Error %d", errno);
478 	return (buf);
479 }
480