1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)err.c 6.22 (Berkeley) 05/24/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 # ifdef lint 42 int sys_nerr; 43 char *sys_errlist[]; 44 # endif lint 45 char MsgBuf[BUFSIZ*2]; /* text of most recent message */ 46 47 static void fmtmsg(); 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 putmsg(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 putmsg(MsgBuf, FALSE); 220 } 221 /* 222 ** PUTMSG -- 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 putmsg(msg, holdmsg) 239 char *msg; 240 bool holdmsg; 241 { 242 /* output to transcript if serious */ 243 if (CurEnv->e_xfp != NULL && (msg[0] == '4' || msg[0] == '5')) 244 fprintf(CurEnv->e_xfp, "%s\n", msg); 245 246 /* output to channel if appropriate */ 247 if (!holdmsg && (Verbose || msg[0] != '0')) 248 { 249 (void) fflush(stdout); 250 if (OpMode == MD_SMTP) 251 fprintf(OutChannel, "%s\r\n", msg); 252 else 253 fprintf(OutChannel, "%s\n", &msg[4]); 254 if (msg[3] == ' ') 255 (void) fflush(OutChannel); 256 if (ferror(OutChannel)) 257 { 258 /* can't call syserr, 'cause we are using MsgBuf */ 259 HoldErrs = TRUE; 260 #ifdef LOG 261 if (LogLevel > 0) 262 syslog(LOG_CRIT, 263 "%s: SYSERR: putmsg (%s): error on output channel sending \"%s\"", 264 CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id, 265 CurHostName, msg); 266 #endif 267 } 268 } 269 } 270 /* 271 ** PUTERRMSG -- like putmsg, but does special processing for error messages 272 ** 273 ** Parameters: 274 ** msg -- the message to output. 275 ** 276 ** Returns: 277 ** none. 278 ** 279 ** Side Effects: 280 ** Sets the fatal error bit in the envelope as appropriate. 281 */ 282 283 puterrmsg(msg) 284 char *msg; 285 { 286 /* output the message as usual */ 287 putmsg(msg, HoldErrs); 288 289 /* signal the error */ 290 Errors++; 291 if (msg[0] == '5') 292 CurEnv->e_flags |= EF_FATALERRS; 293 } 294 /* 295 ** FMTMSG -- format a message into buffer. 296 ** 297 ** Parameters: 298 ** eb -- error buffer to get result. 299 ** to -- the recipient tag for this message. 300 ** num -- arpanet error number. 301 ** en -- the error number to display. 302 ** fmt -- format of string. 303 ** a, b, c, d, e -- arguments. 304 ** 305 ** Returns: 306 ** none. 307 ** 308 ** Side Effects: 309 ** none. 310 */ 311 312 static void 313 fmtmsg(eb, to, num, eno, fmt, ap) 314 register char *eb; 315 char *to; 316 char *num; 317 int eno; 318 char *fmt; 319 va_list ap; 320 { 321 char del; 322 char *meb; 323 324 /* output the reply code */ 325 if (isdigit(fmt[0]) && isdigit(fmt[1]) && isdigit(fmt[2])) 326 { 327 num = fmt; 328 fmt += 4; 329 } 330 if (num[3] == '-') 331 del = '-'; 332 else 333 del = ' '; 334 (void) sprintf(eb, "%3.3s%c", num, del); 335 eb += 4; 336 337 /* output the file name and line number */ 338 if (FileName != NULL) 339 { 340 (void) sprintf(eb, "%s: line %d: ", FileName, LineNumber); 341 eb += strlen(eb); 342 } 343 344 /* output the "to" person */ 345 if (to != NULL && to[0] != '\0') 346 { 347 (void) sprintf(eb, "%s... ", to); 348 while (*eb != '\0') 349 *eb++ &= 0177; 350 } 351 352 meb = eb; 353 354 /* output the message */ 355 (void) vsprintf(eb, fmt, ap); 356 while (*eb != '\0') 357 *eb++ &= 0177; 358 359 /* output the error code, if any */ 360 if (eno != 0) 361 { 362 (void) sprintf(eb, ": %s", errstring(eno)); 363 eb += strlen(eb); 364 } 365 366 if (CurEnv->e_message == NULL && strchr("45", num[0]) != NULL) 367 CurEnv->e_message = newstr(meb); 368 } 369 /* 370 ** ERRSTRING -- return string description of error code 371 ** 372 ** Parameters: 373 ** errno -- the error number to translate 374 ** 375 ** Returns: 376 ** A string description of errno. 377 ** 378 ** Side Effects: 379 ** none. 380 */ 381 382 const char * 383 errstring(errno) 384 int errno; 385 { 386 extern const char *const sys_errlist[]; 387 extern int sys_nerr; 388 static char buf[MAXLINE]; 389 # ifdef SMTP 390 extern char *SmtpPhase; 391 # endif /* SMTP */ 392 393 # ifdef DAEMON 394 # ifdef ETIMEDOUT 395 /* 396 ** Handle special network error codes. 397 ** 398 ** These are 4.2/4.3bsd specific; they should be in daemon.c. 399 */ 400 401 switch (errno) 402 { 403 case ETIMEDOUT: 404 case ECONNRESET: 405 (void) strcpy(buf, sys_errlist[errno]); 406 if (SmtpPhase != NULL) 407 { 408 (void) strcat(buf, " during "); 409 (void) strcat(buf, SmtpPhase); 410 } 411 if (CurHostName != NULL) 412 { 413 (void) strcat(buf, " with "); 414 (void) strcat(buf, CurHostName); 415 } 416 return (buf); 417 418 case EHOSTDOWN: 419 if (CurHostName == NULL) 420 break; 421 (void) sprintf(buf, "Host %s is down", CurHostName); 422 return (buf); 423 424 case ECONNREFUSED: 425 if (CurHostName == NULL) 426 break; 427 (void) sprintf(buf, "Connection refused by %s", CurHostName); 428 return (buf); 429 430 # ifdef NAMED_BIND 431 case HOST_NOT_FOUND + MAX_ERRNO: 432 return ("Name server: host not found"); 433 434 case TRY_AGAIN + MAX_ERRNO: 435 return ("Name server: host name lookup failure"); 436 437 case NO_RECOVERY + MAX_ERRNO: 438 return ("Name server: non-recoverable error"); 439 440 case NO_DATA + MAX_ERRNO: 441 return ("Name server: no data known for name"); 442 # endif 443 } 444 # endif 445 # endif 446 447 if (errno > 0 && errno < sys_nerr) 448 return (sys_errlist[errno]); 449 450 (void) sprintf(buf, "Error %d", errno); 451 return (buf); 452 } 453