2 * Copyright (c) 1998-2003, 2010 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: err.c,v 8.205 2010/02/03 23:22:41 ca Exp $")
20 # include <ldap.h> /* for LDAP error codes */
23 static void putoutmsg __P((char *, bool, bool));
24 static void puterrmsg __P((char *));
25 static char *fmtmsg __P((char *, const char *, const char *, const char *,
26 int, const char *, va_list));
29 ** FATAL_ERROR -- handle a fatal exception
31 ** This function is installed as the default exception handler
32 ** in the main sendmail process, and in all child processes
33 ** that we create. Its job is to handle exceptions that are not
34 ** handled at a lower level.
36 ** The theory is that unhandled exceptions will be 'fatal' class
37 ** exceptions (with an "F:" prefix), such as the out-of-memory
38 ** exception "F:sm.heap". As such, they are handled by exiting
39 ** the process in exactly the same way that xalloc() in Sendmail 8.10
40 ** exits the process when it fails due to lack of memory:
41 ** we call syserr with a message beginning with "!".
44 ** exc -- exception which is terminating this process
58 ** This function may be called when the heap is exhausted.
59 ** The following code writes the message for 'exc' into our
60 ** static buffer without allocating memory or raising exceptions.
63 sm_strio_init(&f, buf, sizeof(buf));
64 sm_exc_write(exc, &f);
65 (void) sm_io_flush(&f, SM_TIME_DEFAULT);
68 ** Terminate the process after logging an error and cleaning up.
70 ** - syserr decides what class of error this is by looking at errno.
71 ** That's no good; we should look at the exc structure.
72 ** - The cleanup code should be moved out of syserr
73 ** and into individual exception handlers
74 ** that are part of the module they clean up after.
82 ** SYSERR -- Print error message.
84 ** Prints an error message via sm_io_printf to the diagnostic output.
86 ** If the first character of the syserr message is `!' it will
87 ** log this as an ALERT message and exit immediately. This can
88 ** leave queue files in an indeterminate state, so it should not
91 ** If the first character of the syserr message is '!' or '@'
92 ** then syserr knows that the process is about to be terminated,
93 ** so the SMTP reply code defaults to 421. Otherwise, the
94 ** reply code defaults to 451 or 554, depending on errno.
97 ** fmt -- the format string. An optional '!' or '@',
98 ** followed by an optional three-digit SMTP
99 ** reply code, followed by message text.
100 ** (others) -- parameters
104 ** Raises E:mta.quickabort if QuickAbort is set.
107 ** increments Errors.
111 char MsgBuf[BUFSIZ*2]; /* text of most recent message */
112 static char HeldMessageBuf[sizeof(MsgBuf)]; /* for held messages */
114 #if NAMED_BIND && !defined(NO_DATA)
115 # define NO_DATA NO_ADDRESS
116 #endif /* NAMED_BIND && !defined(NO_DATA) */
121 syserr(const char *fmt, ...)
123 syserr(fmt, va_alist)
126 #endif /* __STDC__ */
129 int save_errno = errno;
157 /* format and output the error message */
161 ** Since we are terminating the process,
162 ** we are aborting the entire SMTP session,
163 ** rather than just the current transaction.
169 else if (save_errno == 0)
179 SM_VA_START(ap, fmt);
180 errtxt = fmtmsg(MsgBuf, (char *) NULL, p, enhsc, save_errno, fmt, ap);
184 /* save this message for mailq printing */
185 if (!panic && CurEnv != NULL)
187 char *nmsg = sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
189 if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
190 sm_free(CurEnv->e_message);
191 CurEnv->e_message = nmsg;
194 /* determine exit status if not already set */
195 if (ExitStat == EX_OK)
198 ExitStat = EX_SOFTWARE;
202 sm_dprintf("syserr: ExitStat = %d\n", ExitStat);
205 pw = sm_getpwuid(RealUid);
211 (void) sm_snprintf(ubuf, sizeof(ubuf), "UID%d", (int) RealUid);
215 sm_syslog(panic ? LOG_ALERT : LOG_CRIT,
216 CurEnv == NULL ? NOQID : CurEnv->e_id,
217 "SYSERR(%s): %.900s",
241 mci_dump_all(smioout, true);
256 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
259 ** USRERR -- Signal user error.
261 ** This is much like syserr except it is for user errors.
264 ** fmt -- the format string. If it does not begin with
265 ** a three-digit SMTP reply code, 550 is assumed.
266 ** (others) -- sm_io_printf strings
270 ** Raises E:mta.quickabort if QuickAbort is set.
273 ** increments Errors.
279 usrerr(const char *fmt, ...)
281 usrerr(fmt, va_alist)
284 #endif /* __STDC__ */
290 if (fmt[0] == '5' || fmt[0] == '6')
292 else if (fmt[0] == '4' || fmt[0] == '8')
294 else if (fmt[0] == '2')
298 SM_VA_START(ap, fmt);
299 errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "550", enhsc, 0, fmt, ap);
305 /* save this message for mailq printing */
310 if (CurEnv->e_message != NULL)
317 if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
318 sm_free(CurEnv->e_message);
319 if (MsgBuf[0] == '6')
323 (void) sm_snprintf(buf, sizeof(buf),
324 "Postmaster warning: %.*s",
325 (int) sizeof(buf) - 22, errtxt);
327 sm_rpool_strdup_x(CurEnv->e_rpool, buf);
332 sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
338 if (LogLevel > 3 && LogUsrErrs)
339 sm_syslog(LOG_NOTICE, CurEnv->e_id, "%.900s", errtxt);
341 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
344 ** USRERRENH -- Signal user error.
346 ** Same as usrerr but with enhanced status code.
349 ** enhsc -- the enhanced status code.
350 ** fmt -- the format string. If it does not begin with
351 ** a three-digit SMTP reply code, 550 is assumed.
352 ** (others) -- sm_io_printf strings
356 ** Raises E:mta.quickabort if QuickAbort is set.
359 ** increments Errors.
365 usrerrenh(char *enhsc, const char *fmt, ...)
367 usrerrenh(enhsc, fmt, va_alist)
371 #endif /* __STDC__ */
376 if (enhsc == NULL || *enhsc == '\0')
378 if (fmt[0] == '5' || fmt[0] == '6')
380 else if (fmt[0] == '4' || fmt[0] == '8')
382 else if (fmt[0] == '2')
385 SM_VA_START(ap, fmt);
386 errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "550", enhsc, 0, fmt, ap);
392 /* save this message for mailq printing */
397 if (CurEnv->e_message != NULL)
404 if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
405 sm_free(CurEnv->e_message);
406 if (MsgBuf[0] == '6')
410 (void) sm_snprintf(buf, sizeof(buf),
411 "Postmaster warning: %.*s",
412 (int) sizeof(buf) - 22, errtxt);
414 sm_rpool_strdup_x(CurEnv->e_rpool, buf);
419 sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
425 if (LogLevel > 3 && LogUsrErrs)
426 sm_syslog(LOG_NOTICE, CurEnv->e_id, "%.900s", errtxt);
428 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
432 ** MESSAGE -- print message (not necessarily an error)
435 ** msg -- the message (sm_io_printf fmt) -- it can begin with
436 ** an SMTP reply code. If not, 050 is assumed.
437 ** (others) -- sm_io_printf arguments
449 message(const char *msg, ...)
451 message(msg, va_alist)
454 #endif /* __STDC__ */
460 SM_VA_START(ap, msg);
461 errtxt = fmtmsg(MsgBuf, CurEnv->e_to, "050", (char *) NULL, 0, msg, ap);
463 putoutmsg(MsgBuf, false, false);
465 /* save this message for mailq printing */
470 if (CurEnv->e_message != NULL)
475 if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
476 sm_free(CurEnv->e_message);
477 CurEnv->e_message = sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
484 ** NMESSAGE -- print message (not necessarily an error)
486 ** Just like "message" except it never puts the to... tag on.
489 ** msg -- the message (sm_io_printf fmt) -- if it begins
490 ** with a three digit SMTP reply code, that is used,
491 ** otherwise 050 is assumed.
492 ** (others) -- sm_io_printf arguments
504 nmessage(const char *msg, ...)
506 nmessage(msg, va_alist)
509 #endif /* __STDC__ */
515 SM_VA_START(ap, msg);
516 errtxt = fmtmsg(MsgBuf, (char *) NULL, "050",
517 (char *) NULL, 0, msg, ap);
519 putoutmsg(MsgBuf, false, false);
521 /* save this message for mailq printing */
526 if (CurEnv->e_message != NULL)
531 if (CurEnv->e_rpool == NULL && CurEnv->e_message != NULL)
532 sm_free(CurEnv->e_message);
533 CurEnv->e_message = sm_rpool_strdup_x(CurEnv->e_rpool, errtxt);
538 ** PUTOUTMSG -- output error message to transcript and channel
541 ** msg -- message to output (in SMTP format).
542 ** holdmsg -- if true, don't output a copy of the message to
543 ** our output channel.
544 ** heldmsg -- if true, this is a previously held message;
545 ** don't log it to the transcript file.
551 ** Outputs msg to the transcript.
552 ** If appropriate, outputs it to the channel.
553 ** Deletes SMTP reply code number as appropriate.
557 putoutmsg(msg, holdmsg, heldmsg)
562 char msgcode = msg[0];
566 /* display for debugging */
568 sm_dprintf("--- %s%s%s\n", msg, holdmsg ? " (hold)" : "",
569 heldmsg ? " (held)" : "");
571 /* map warnings to something SMTP can handle */
574 else if (msgcode == '8')
576 id = (CurEnv != NULL) ? CurEnv->e_id : NULL;
578 /* output to transcript if serious */
579 if (!heldmsg && CurEnv != NULL && CurEnv->e_xfp != NULL &&
580 strchr("45", msg[0]) != NULL)
581 (void) sm_io_fprintf(CurEnv->e_xfp, SM_TIME_DEFAULT, "%s\n",
584 if (LogLevel > 14 && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
585 sm_syslog(LOG_INFO, id,
586 "--- %s%s%s", msg, holdmsg ? " (hold)" : "",
587 heldmsg ? " (held)" : "");
592 /* output to channel if appropriate */
593 if (!Verbose && msg[0] == '0')
597 /* save for possible future display */
599 if (HeldMessageBuf[0] == '5' && msgcode == '4')
601 (void) sm_strlcpy(HeldMessageBuf, msg, sizeof(HeldMessageBuf));
605 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
607 if (OutChannel == NULL)
610 /* find actual text of error (after SMTP status codes) */
611 if (ISSMTPREPLY(errtxt))
616 l = isenhsc(errtxt, ' ');
618 l = isenhsc(errtxt, '\0');
623 /* if DisConnected, OutChannel now points to the transcript */
625 (OpMode == MD_SMTP || OpMode == MD_DAEMON || OpMode == MD_ARPAFTP))
626 (void) sm_io_fprintf(OutChannel, SM_TIME_DEFAULT, "%s\r\n",
629 (void) sm_io_fprintf(OutChannel, SM_TIME_DEFAULT, "%s\n",
631 if (TrafficLogFile != NULL)
632 (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
633 "%05d >>> %s\n", (int) CurrentPid,
634 (OpMode == MD_SMTP || OpMode == MD_DAEMON)
637 /* XXX can't flush here for SMTP pipelining */
639 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
640 if (!sm_io_error(OutChannel) || DisConnected)
644 ** Error on output -- if reporting lost channel, just ignore it.
645 ** Also, ignore errors from QUIT response (221 message) -- some
646 ** rude servers don't read result.
649 if (InChannel == NULL || sm_io_eof(InChannel) ||
650 sm_io_error(InChannel) || strncmp(msg, "221", 3) == 0)
653 /* can't call syserr, 'cause we are using MsgBuf */
656 sm_syslog(LOG_CRIT, id,
657 "SYSERR: putoutmsg (%s): error on output channel sending \"%s\": %s",
659 shortenstring(msg, MAXSHORTSTR), sm_errstring(errno));
660 #endif /* !PIPELINING */
663 ** PUTERRMSG -- like putoutmsg, but does special processing for error messages
666 ** msg -- the message to output.
672 ** Sets the fatal error bit in the envelope as appropriate.
679 char msgcode = msg[0];
681 /* output the message as usual */
682 putoutmsg(msg, HoldErrs, false);
684 /* be careful about multiple error messages */
688 /* signal the error */
696 /* notify the postmaster */
697 CurEnv->e_flags |= EF_PM_NOTIFY;
699 else if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
701 /* mark long-term fatal errors */
702 CurEnv->e_flags |= EF_FATALERRS;
706 ** ISENHSC -- check whether a string contains an enhanced status code
709 ** s -- string with possible enhanced status code.
710 ** delim -- delim for enhanced status code.
713 ** 0 -- no enhanced status code.
714 ** >4 -- length of enhanced status code.
728 if (!((*s == '2' || *s == '4' || *s == '5') && s[1] == '.'))
732 while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
734 if (h == 0 || s[l + h] != '.')
738 while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
740 if (h == 0 || s[l + h] != delim)
745 ** EXTENHSC -- check and extract an enhanced status code
748 ** s -- string with possible enhanced status code.
749 ** delim -- delim for enhanced status code.
750 ** e -- pointer to storage for enhanced status code.
751 ** must be != NULL and have space for at least
752 ** 10 characters ([245].[0-9]{1,3}.[0-9]{1,3})
755 ** 0 -- no enhanced status code.
756 ** >4 -- length of enhanced status code.
759 ** fills e with enhanced status code.
763 extenhsc(s, delim, e)
772 if (!((*s == '2' || *s == '4' || *s == '5') && s[1] == '.'))
778 while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
783 if (h == 0 || s[l + h] != '.')
788 while (h < 3 && isascii(s[l + h]) && isdigit(s[l + h]))
793 if (h == 0 || s[l + h] != delim)
799 ** FMTMSG -- format a message into buffer.
802 ** eb -- error buffer to get result -- MUST BE MsgBuf.
803 ** to -- the recipient tag for this message.
804 ** num -- default three digit SMTP reply code.
805 ** enhsc -- enhanced status code.
806 ** en -- the error number to display.
807 ** fmt -- format of string.
808 ** ap -- arguments for fmt.
811 ** pointer to error text beyond status codes.
818 fmtmsg(eb, to, num, enhsc, eno, fmt, ap)
829 int spaceleft = sizeof(MsgBuf);
832 /* output the reply code */
842 if (SoftBounce && num[0] == '5')
845 (void) sm_snprintf(eb, spaceleft, "4%2.2s%c", num + 1, del);
848 (void) sm_snprintf(eb, spaceleft, "%3.3s%c", num, del);
852 if ((l = isenhsc(fmt, ' ' )) > 0 && l < spaceleft - 4)
854 /* copy enh.status code including trailing blank */
856 (void) sm_strlcpy(eb, fmt, l + 1);
861 else if ((l = isenhsc(enhsc, '\0')) > 0 && l < spaceleft - 4)
863 /* copy enh.status code */
864 (void) sm_strlcpy(eb, enhsc, l + 1);
870 if (SoftBounce && eb[-l] == '5')
877 /* output the file name and line number */
878 if (FileName != NULL)
880 (void) sm_snprintf(eb, spaceleft, "%s: line %d: ",
881 shortenstring(FileName, 83), LineNumber);
882 eb += (l = strlen(eb));
887 ** output the "to" address only if it is defined and one of the
888 ** following codes is used:
889 ** 050 internal notices, e.g., alias expansion
891 ** 252 Cannot VRFY user, but will accept message and attempt delivery
892 ** 450 Requested mail action not taken: mailbox unavailable
893 ** 550 Requested action not taken: mailbox unavailable
894 ** 553 Requested action not taken: mailbox name not allowed
896 ** Notice: this still isn't "the right thing", this code shouldn't
897 ** (indirectly) depend on CurEnv->e_to.
900 if (to != NULL && to[0] != '\0' &&
901 (strncmp(num, "050", 3) == 0 ||
902 strncmp(num, "250", 3) == 0 ||
903 strncmp(num, "252", 3) == 0 ||
904 strncmp(num, "450", 3) == 0 ||
905 strncmp(num, "550", 3) == 0 ||
906 strncmp(num, "553", 3) == 0))
908 (void) sm_strlcpyn(eb, spaceleft, 2,
909 shortenstring(to, MAXSHORTSTR), "... ");
910 spaceleft -= strlen(eb);
915 /* output the message */
916 (void) sm_vsnprintf(eb, spaceleft, fmt, ap);
917 spaceleft -= strlen(eb);
921 /* output the error code, if any */
923 (void) sm_strlcpyn(eb, spaceleft, 2, ": ", sm_errstring(eno));
928 ** BUFFER_ERRORS -- arrange to buffer future error messages
940 HeldMessageBuf[0] = '\0';
944 ** FLUSH_ERRORS -- flush the held error message buffer
947 ** print -- if set, print the message, otherwise just
958 if (print && HeldMessageBuf[0] != '\0')
959 putoutmsg(HeldMessageBuf, false, true);
960 HeldMessageBuf[0] = '\0';
964 ** SM_ERRSTRING -- return string description of error code
967 ** errnum -- the error number to translate
970 ** A string description of errnum.
982 static char buf[MAXLINE];
986 #endif /* HASSTRERROR */
987 #if !HASSTRERROR && !defined(ERRLIST_PREDEFINED)
988 extern char *sys_errlist[];
990 #endif /* !HASSTRERROR && !defined(ERRLIST_PREDEFINED) */
993 ** Handle special network error codes.
995 ** These are 4.2/4.3bsd specific; they should be in daemon.c.
1005 err = strerror(errnum);
1008 (void) sm_snprintf(errbuf, sizeof(errbuf),
1009 "Error %d", errnum);
1012 (void) sm_strlcpy(bp, err, SPACELEFT(buf, bp));
1013 #else /* HASSTRERROR */
1014 if (errnum >= 0 && errnum < sys_nerr)
1015 (void) sm_strlcpy(bp, sys_errlist[errnum],
1016 SPACELEFT(buf, bp));
1018 (void) sm_snprintf(bp, SPACELEFT(buf, bp),
1019 "Error %d", errnum);
1020 #endif /* HASSTRERROR */
1022 if (CurHostName != NULL)
1024 if (errnum == ETIMEDOUT)
1026 (void) sm_snprintf(bp, SPACELEFT(buf, bp),
1033 (void) sm_snprintf(bp, SPACELEFT(buf, bp),
1034 "Connection reset by ");
1037 (void) sm_strlcpy(bp,
1038 shortenstring(CurHostName, MAXSHORTSTR),
1039 SPACELEFT(buf, bp));
1042 if (SmtpPhase != NULL)
1044 (void) sm_snprintf(bp, SPACELEFT(buf, bp),
1045 " during %s", SmtpPhase);
1050 if (CurHostName == NULL)
1052 (void) sm_snprintf(buf, sizeof(buf), "Host %s is down",
1053 shortenstring(CurHostName, MAXSHORTSTR));
1057 if (CurHostName == NULL)
1059 (void) sm_strlcpyn(buf, sizeof(buf), 2, "Connection refused by ",
1060 shortenstring(CurHostName, MAXSHORTSTR));
1064 case HOST_NOT_FOUND + E_DNSBASE:
1065 dnsmsg = "host not found";
1068 case TRY_AGAIN + E_DNSBASE:
1069 dnsmsg = "host name lookup failure";
1072 case NO_RECOVERY + E_DNSBASE:
1073 dnsmsg = "non-recoverable error";
1076 case NO_DATA + E_DNSBASE:
1077 dnsmsg = "no data known";
1079 #endif /* NAMED_BIND */
1082 /* SunOS gives "Not owner" -- this is the POSIX message */
1083 return "Operation not permitted";
1086 ** Error messages used internally in sendmail.
1089 case E_SM_OPENTIMEOUT:
1090 return "Timeout on file open";
1093 return "Symbolic links not allowed";
1096 return "Hard links not allowed";
1099 return "Regular files only";
1102 return "Executable files not allowed";
1105 return "World writable directory";
1108 return "Group writable directory";
1110 case E_SM_FILECHANGE:
1111 return "File changed after open";
1114 return "World writable file";
1117 return "Group writable file";
1120 return "Group readable file";
1123 return "World readable file";
1129 bp += sm_strlcpy(bp, "Name server: ", sizeof(buf));
1130 if (CurHostName != NULL)
1132 (void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2,
1133 shortenstring(CurHostName, MAXSHORTSTR), ": ");
1136 (void) sm_strlcpy(bp, dnsmsg, SPACELEFT(buf, bp));
1141 if (errnum >= E_LDAPBASE)
1142 return ldap_err2string(errnum - E_LDAPBASE);
1143 #endif /* LDAPMAP */
1146 err = strerror(errnum);
1149 (void) sm_snprintf(buf, sizeof(buf), "Error %d", errnum);
1153 #else /* HASSTRERROR */
1154 if (errnum > 0 && errnum < sys_nerr)
1155 return sys_errlist[errnum];
1157 (void) sm_snprintf(buf, sizeof(buf), "Error %d", errnum);
1159 #endif /* HASSTRERROR */