1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
|
/*
* Copyright (c) 1998 Sendmail, Inc. All rights reserved.
* Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#ifndef lint
static char sccsid[] = "@(#)err.c 8.74 (Berkeley) 6/4/1998";
#endif /* not lint */
# include "sendmail.h"
# include <errno.h>
/*
** SYSERR -- Print error message.
**
** Prints an error message via printf to the diagnostic output.
**
** If the first character of the syserr message is `!' it will
** log this as an ALERT message and exit immediately. This can
** leave queue files in an indeterminate state, so it should not
** be used lightly.
**
** Parameters:
** fmt -- the format string. If it does not begin with
** a three-digit SMTP reply code, either 554 or
** 451 is assumed depending on whether errno
** is set.
** (others) -- parameters
**
** Returns:
** none
** Through TopFrame if QuickAbort is set.
**
** Side Effects:
** increments Errors.
** sets ExitStat.
*/
char MsgBuf[BUFSIZ*2]; /* text of most recent message */
char HeldMessageBuf[sizeof MsgBuf]; /* for held messages */
extern void putoutmsg __P((char *, bool, bool));
extern void puterrmsg __P((char *));
static void fmtmsg __P((char *, const char *, const char *, int, const char *, va_list));
#if NAMED_BIND && !defined(NO_DATA)
# define NO_DATA NO_ADDRESS
#endif
void
/*VARARGS1*/
#ifdef __STDC__
syserr(const char *fmt, ...)
#else
syserr(fmt, va_alist)
const char *fmt;
va_dcl
#endif
{
register char *p;
int olderrno = errno;
bool panic;
char *uname;
struct passwd *pw;
char ubuf[80];
VA_LOCAL_DECL
panic = *fmt == '!';
if (panic)
{
fmt++;
HoldErrs = FALSE;
}
/* format and output the error message */
if (olderrno == 0)
p = "554";
else
p = "451";
VA_START(fmt);
fmtmsg(MsgBuf, (char *) NULL, p, olderrno, fmt, ap);
VA_END;
puterrmsg(MsgBuf);
/* save this message for mailq printing */
if (!panic && CurEnv != NULL)
{
if (CurEnv->e_message != NULL)
free(CurEnv->e_message);
CurEnv->e_message = newstr(MsgBuf + 4);
}
/* determine exit status if not already set */
if (ExitStat == EX_OK)
{
if (olderrno == 0)
ExitStat = EX_SOFTWARE;
else
ExitStat = EX_OSERR;
if (tTd(54, 1))
printf("syserr: ExitStat = %d\n", ExitStat);
}
pw = sm_getpwuid(getuid());
if (pw != NULL)
uname = pw->pw_name;
else
{
uname = ubuf;
snprintf(ubuf, sizeof ubuf, "UID%d", getuid());
}
if (LogLevel > 0)
sm_syslog(panic ? LOG_ALERT : LOG_CRIT,
CurEnv == NULL ? NOQID : CurEnv->e_id,
"SYSERR(%s): %.900s",
uname, &MsgBuf[4]);
switch (olderrno)
{
case EBADF:
case ENFILE:
case EMFILE:
case ENOTTY:
#ifdef EFBIG
case EFBIG:
#endif
#ifdef ESPIPE
case ESPIPE:
#endif
#ifdef EPIPE
case EPIPE:
#endif
#ifdef ENOBUFS
case ENOBUFS:
#endif
#ifdef ESTALE
case ESTALE:
#endif
printopenfds(TRUE);
mci_dump_all(TRUE);
break;
}
if (panic)
{
#ifdef XLA
xla_all_end();
#endif
if (tTd(0, 1))
abort();
exit(EX_OSERR);
}
errno = 0;
if (QuickAbort)
longjmp(TopFrame, 2);
}
/*
** USRERR -- Signal user error.
**
** This is much like syserr except it is for user errors.
**
** Parameters:
** fmt -- the format string. If it does not begin with
** a three-digit SMTP reply code, 501 is assumed.
** (others) -- printf strings
**
** Returns:
** none
** Through TopFrame if QuickAbort is set.
**
** Side Effects:
** increments Errors.
*/
/*VARARGS1*/
void
#ifdef __STDC__
usrerr(const char *fmt, ...)
#else
usrerr(fmt, va_alist)
const char *fmt;
va_dcl
#endif
{
VA_LOCAL_DECL
if (SuprErrs)
return;
VA_START(fmt);
fmtmsg(MsgBuf, CurEnv->e_to, "501", 0, fmt, ap);
VA_END;
/* save this message for mailq printing */
switch (MsgBuf[0])
{
case '4':
case '8':
if (CurEnv->e_message != NULL)
break;
/* fall through.... */
case '5':
case '6':
if (CurEnv->e_message != NULL)
free(CurEnv->e_message);
if (MsgBuf[0] == '6')
{
char buf[MAXLINE];
snprintf(buf, sizeof buf, "Postmaster warning: %.*s",
sizeof buf - 22, MsgBuf + 4);
CurEnv->e_message = newstr(buf);
}
else
{
CurEnv->e_message = newstr(MsgBuf + 4);
}
break;
}
puterrmsg(MsgBuf);
if (LogLevel > 3 && LogUsrErrs)
sm_syslog(LOG_NOTICE, CurEnv->e_id,
"%.900s",
&MsgBuf[4]);
if (QuickAbort)
longjmp(TopFrame, 1);
}
/*
** MESSAGE -- print message (not necessarily an error)
**
** Parameters:
** msg -- the message (printf fmt) -- it can begin with
** an SMTP reply code. If not, 050 is assumed.
** (others) -- printf arguments
**
** Returns:
** none
**
** Side Effects:
** none.
*/
/*VARARGS1*/
void
#ifdef __STDC__
message(const char *msg, ...)
#else
message(msg, va_alist)
const char *msg;
va_dcl
#endif
{
VA_LOCAL_DECL
errno = 0;
VA_START(msg);
fmtmsg(MsgBuf, CurEnv->e_to, "050", 0, msg, ap);
VA_END;
putoutmsg(MsgBuf, FALSE, FALSE);
/* save this message for mailq printing */
switch (MsgBuf[0])
{
case '4':
case '8':
if (CurEnv->e_message != NULL)
break;
/* fall through.... */
case '5':
if (CurEnv->e_message != NULL)
free(CurEnv->e_message);
CurEnv->e_message = newstr(MsgBuf + 4);
break;
}
}
/*
** NMESSAGE -- print message (not necessarily an error)
**
** Just like "message" except it never puts the to... tag on.
**
** Parameters:
** msg -- the message (printf fmt) -- if it begins
** with a three digit SMTP reply code, that is used,
** otherwise 050 is assumed.
** (others) -- printf arguments
**
** Returns:
** none
**
** Side Effects:
** none.
*/
/*VARARGS1*/
void
#ifdef __STDC__
nmessage(const char *msg, ...)
#else
nmessage(msg, va_alist)
const char *msg;
va_dcl
#endif
{
VA_LOCAL_DECL
errno = 0;
VA_START(msg);
fmtmsg(MsgBuf, (char *) NULL, "050", 0, msg, ap);
VA_END;
putoutmsg(MsgBuf, FALSE, FALSE);
/* save this message for mailq printing */
switch (MsgBuf[0])
{
case '4':
case '8':
if (CurEnv->e_message != NULL)
break;
/* fall through.... */
case '5':
if (CurEnv->e_message != NULL)
free(CurEnv->e_message);
CurEnv->e_message = newstr(MsgBuf + 4);
break;
}
}
/*
** PUTOUTMSG -- output error message to transcript and channel
**
** Parameters:
** msg -- message to output (in SMTP format).
** holdmsg -- if TRUE, don't output a copy of the message to
** our output channel.
** heldmsg -- if TRUE, this is a previously held message;
** don't log it to the transcript file.
**
** Returns:
** none.
**
** Side Effects:
** Outputs msg to the transcript.
** If appropriate, outputs it to the channel.
** Deletes SMTP reply code number as appropriate.
*/
void
putoutmsg(msg, holdmsg, heldmsg)
char *msg;
bool holdmsg;
bool heldmsg;
{
char msgcode = msg[0];
/* display for debugging */
if (tTd(54, 8))
printf("--- %s%s%s\n", msg, holdmsg ? " (hold)" : "",
heldmsg ? " (held)" : "");
/* map warnings to something SMTP can handle */
if (msgcode == '6')
msg[0] = '5';
else if (msgcode == '8')
msg[0] = '4';
/* output to transcript if serious */
if (!heldmsg && CurEnv != NULL && CurEnv->e_xfp != NULL &&
strchr("45", msg[0]) != NULL)
fprintf(CurEnv->e_xfp, "%s\n", msg);
if (LogLevel >= 15 && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
sm_syslog(LOG_INFO, CurEnv->e_id,
"--> %s%s",
msg, holdmsg ? " (held)" : "");
if (msgcode == '8')
msg[0] = '0';
/* output to channel if appropriate */
if (!Verbose && msg[0] == '0')
return;
if (holdmsg)
{
/* save for possible future display */
msg[0] = msgcode;
snprintf(HeldMessageBuf, sizeof HeldMessageBuf, "%s", msg);
return;
}
(void) fflush(stdout);
if (OutChannel == NULL)
return;
/* if DisConnected, OutChannel now points to the transcript */
if (!DisConnected &&
(OpMode == MD_SMTP || OpMode == MD_DAEMON || OpMode == MD_ARPAFTP))
fprintf(OutChannel, "%s\r\n", msg);
else
fprintf(OutChannel, "%s\n", &msg[4]);
if (TrafficLogFile != NULL)
fprintf(TrafficLogFile, "%05d >>> %s\n", (int) getpid(),
(OpMode == MD_SMTP || OpMode == MD_DAEMON) ? msg : &msg[4]);
if (msg[3] == ' ')
(void) fflush(OutChannel);
if (!ferror(OutChannel) || DisConnected)
return;
/*
** Error on output -- if reporting lost channel, just ignore it.
** Also, ignore errors from QUIT response (221 message) -- some
** rude servers don't read result.
*/
if (InChannel == NULL || feof(InChannel) || ferror(InChannel) ||
strncmp(msg, "221", 3) == 0)
return;
/* can't call syserr, 'cause we are using MsgBuf */
HoldErrs = TRUE;
if (LogLevel > 0)
sm_syslog(LOG_CRIT, CurEnv->e_id,
"SYSERR: putoutmsg (%s): error on output channel sending \"%s\": %s",
CurHostName == NULL ? "NO-HOST" : CurHostName,
shortenstring(msg, MAXSHORTSTR), errstring(errno));
}
/*
** PUTERRMSG -- like putoutmsg, but does special processing for error messages
**
** Parameters:
** msg -- the message to output.
**
** Returns:
** none.
**
** Side Effects:
** Sets the fatal error bit in the envelope as appropriate.
*/
void
puterrmsg(msg)
char *msg;
{
char msgcode = msg[0];
/* output the message as usual */
putoutmsg(msg, HoldErrs, FALSE);
/* be careful about multiple error messages */
if (OnlyOneError)
HoldErrs = TRUE;
/* signal the error */
Errors++;
if (CurEnv == NULL)
return;
if (msgcode == '6')
{
/* notify the postmaster */
CurEnv->e_flags |= EF_PM_NOTIFY;
}
else if (msgcode == '5' && bitset(EF_GLOBALERRS, CurEnv->e_flags))
{
/* mark long-term fatal errors */
CurEnv->e_flags |= EF_FATALERRS;
}
}
/*
** FMTMSG -- format a message into buffer.
**
** Parameters:
** eb -- error buffer to get result.
** to -- the recipient tag for this message.
** num -- arpanet error number.
** en -- the error number to display.
** fmt -- format of string.
** a, b, c, d, e -- arguments.
**
** Returns:
** none.
**
** Side Effects:
** none.
*/
static void
fmtmsg(eb, to, num, eno, fmt, ap)
register char *eb;
const char *to;
const char *num;
int eno;
const char *fmt;
va_list ap;
{
char del;
int l;
int spaceleft = sizeof MsgBuf;
/* output the reply code */
if (isascii(fmt[0]) && isdigit(fmt[0]) &&
isascii(fmt[1]) && isdigit(fmt[1]) &&
isascii(fmt[2]) && isdigit(fmt[2]))
{
num = fmt;
fmt += 4;
}
if (num[3] == '-')
del = '-';
else
del = ' ';
(void) snprintf(eb, spaceleft, "%3.3s%c", num, del);
eb += 4;
spaceleft -= 4;
/* output the file name and line number */
if (FileName != NULL)
{
(void) snprintf(eb, spaceleft, "%s: line %d: ",
shortenstring(FileName, 83), LineNumber);
eb += (l = strlen(eb));
spaceleft -= l;
}
/* output the "to" person */
if (to != NULL && to[0] != '\0' &&
strncmp(num, "551", 3) != 0 &&
strncmp(num, "251", 3) != 0)
{
(void) snprintf(eb, spaceleft, "%s... ",
shortenstring(to, MAXSHORTSTR));
spaceleft -= strlen(eb);
while (*eb != '\0')
*eb++ &= 0177;
}
/* output the message */
(void) vsnprintf(eb, spaceleft, fmt, ap);
spaceleft -= strlen(eb);
while (*eb != '\0')
*eb++ &= 0177;
/* output the error code, if any */
if (eno != 0)
(void) snprintf(eb, spaceleft, ": %s", errstring(eno));
}
/*
** BUFFER_ERRORS -- arrange to buffer future error messages
**
** Parameters:
** none
**
** Returns:
** none.
*/
void
buffer_errors()
{
HeldMessageBuf[0] = '\0';
HoldErrs = TRUE;
}
/*
** FLUSH_ERRORS -- flush the held error message buffer
**
** Parameters:
** print -- if set, print the message, otherwise just
** delete it.
**
** Returns:
** none.
*/
void
flush_errors(print)
bool print;
{
if (print && HeldMessageBuf[0] != '\0')
putoutmsg(HeldMessageBuf, FALSE, TRUE);
HeldMessageBuf[0] = '\0';
HoldErrs = FALSE;
}
/*
** ERRSTRING -- return string description of error code
**
** Parameters:
** errnum -- the error number to translate
**
** Returns:
** A string description of errnum.
**
** Side Effects:
** none.
*/
const char *
errstring(errnum)
int errnum;
{
char *dnsmsg;
char *bp;
static char buf[MAXLINE];
# if !HASSTRERROR && !defined(ERRLIST_PREDEFINED)
extern char *sys_errlist[];
extern int sys_nerr;
# endif
# if SMTP
extern char *SmtpPhase;
# endif /* SMTP */
/*
** Handle special network error codes.
**
** These are 4.2/4.3bsd specific; they should be in daemon.c.
*/
dnsmsg = NULL;
switch (errnum)
{
# if defined(DAEMON) && defined(ETIMEDOUT)
case ETIMEDOUT:
case ECONNRESET:
bp = buf;
#if HASSTRERROR
snprintf(bp, SPACELEFT(buf, bp), "%s", strerror(errnum));
#else
if (errnum >= 0 && errnum < sys_nerr)
snprintf(bp, SPACELEFT(buf, bp), "%s", sys_errlist[errnum]);
else
snprintf(bp, SPACELEFT(buf, bp), "Error %d", errnum);
#endif
bp += strlen(bp);
if (CurHostName != NULL)
{
if (errnum == ETIMEDOUT)
{
snprintf(bp, SPACELEFT(buf, bp), " with ");
bp += strlen(bp);
}
else
{
bp = buf;
snprintf(bp, SPACELEFT(buf, bp),
"Connection reset by ");
bp += strlen(bp);
}
snprintf(bp, SPACELEFT(buf, bp), "%s",
shortenstring(CurHostName, MAXSHORTSTR));
bp += strlen(buf);
}
if (SmtpPhase != NULL)
{
snprintf(bp, SPACELEFT(buf, bp), " during %s",
SmtpPhase);
}
return (buf);
case EHOSTDOWN:
if (CurHostName == NULL)
break;
(void) snprintf(buf, sizeof buf, "Host %s is down",
shortenstring(CurHostName, MAXSHORTSTR));
return (buf);
case ECONNREFUSED:
if (CurHostName == NULL)
break;
(void) snprintf(buf, sizeof buf, "Connection refused by %s",
shortenstring(CurHostName, MAXSHORTSTR));
return (buf);
# endif
# if NAMED_BIND
case HOST_NOT_FOUND + E_DNSBASE:
dnsmsg = "host not found";
break;
case TRY_AGAIN + E_DNSBASE:
dnsmsg = "host name lookup failure";
break;
case NO_RECOVERY + E_DNSBASE:
dnsmsg = "non-recoverable error";
break;
case NO_DATA + E_DNSBASE:
dnsmsg = "no data known";
break;
# endif
case EPERM:
/* SunOS gives "Not owner" -- this is the POSIX message */
return "Operation not permitted";
/*
** Error messages used internally in sendmail.
*/
case E_SM_OPENTIMEOUT:
return "Timeout on file open";
case E_SM_NOSLINK:
return "Symbolic links not allowed";
case E_SM_NOHLINK:
return "Hard links not allowed";
case E_SM_REGONLY:
return "Regular files only";
case E_SM_ISEXEC:
return "Executable files not allowed";
case E_SM_WWDIR:
return "World writable directory";
case E_SM_GWDIR:
return "Group writable directory";
case E_SM_FILECHANGE:
return "File changed after open";
case E_SM_WWFILE:
return "World writable file";
case E_SM_GWFILE:
return "Group writable file";
}
if (dnsmsg != NULL)
{
bp = buf;
strcpy(bp, "Name server: ");
bp += strlen(bp);
if (CurHostName != NULL)
{
snprintf(bp, SPACELEFT(buf, bp), "%s: ",
shortenstring(CurHostName, MAXSHORTSTR));
bp += strlen(bp);
}
snprintf(bp, SPACELEFT(buf, bp), "%s", dnsmsg);
return buf;
}
#if HASSTRERROR
return strerror(errnum);
#else
if (errnum > 0 && errnum < sys_nerr)
return (sys_errlist[errnum]);
(void) snprintf(buf, sizeof buf, "Error %d", errnum);
return (buf);
#endif
}
|