xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 7613)
1 # include "sendmail.h"
2 # ifdef LOG
3 # include <syslog.h>
4 # endif LOG
5 
6 SCCSID(@(#)err.c	3.25		07/31/82);
7 
8 /*
9 **  SYSERR -- Print error message.
10 **
11 **	Prints an error message via printf to the diagnostic
12 **	output.  If LOG is defined, it logs it also.
13 **
14 **	Parameters:
15 **		f -- the format string
16 **		a, b, c, d, e -- parameters
17 **
18 **	Returns:
19 **		none
20 **
21 **	Side Effects:
22 **		increments Errors.
23 **		sets ExitStat.
24 */
25 
26 # ifdef lint
27 int	sys_nerr;
28 char	*sys_errlist[];
29 # endif lint
30 static char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
31 
32 /*VARARGS1*/
33 syserr(fmt, a, b, c, d, e)
34 	char *fmt;
35 {
36 	extern char Arpa_Syserr[];
37 	char *saveto = CurEnv->e_to;
38 
39 	/* format and output the error message */
40 	fmtmsg(MsgBuf, NULL, Arpa_Syserr, fmt, a, b, c, d, e);
41 	putmsg(MsgBuf);
42 
43 	/* mark the error as having occured */
44 	Errors++;
45 	FatalErrors = TRUE;
46 
47 	/* determine exit status if not already set */
48 	if (ExitStat == EX_OK)
49 	{
50 		if (errno == 0)
51 			ExitStat = EX_SOFTWARE;
52 		else
53 			ExitStat = EX_OSERR;
54 	}
55 
56 # ifdef LOG
57 	syslog(LOG_ERR, "%s->%s: %s", CurEnv->e_from.q_paddr, CurEnv->e_to, &MsgBuf[4]);
58 # endif LOG
59 	errno = 0;
60 }
61 /*
62 **  USRERR -- Signal user error.
63 **
64 **	This is much like syserr except it is for user errors.
65 **
66 **	Parameters:
67 **		fmt, a, b, c, d -- printf strings
68 **
69 **	Returns:
70 **		none
71 **
72 **	Side Effects:
73 **		increments Errors.
74 */
75 
76 /*VARARGS1*/
77 usrerr(fmt, a, b, c, d, e)
78 	char *fmt;
79 {
80 	extern char SuprErrs;
81 	extern char Arpa_Usrerr[];
82 
83 	if (SuprErrs)
84 		return;
85 	Errors++;
86 	FatalErrors = TRUE;
87 
88 	fmtmsg(MsgBuf, CurEnv->e_to, Arpa_Usrerr, fmt, a, b, c, d, e);
89 	putmsg(MsgBuf);
90 }
91 /*
92 **  MESSAGE -- print message (not necessarily an error)
93 **
94 **	Parameters:
95 **		num -- the default ARPANET error number (in ascii)
96 **		msg -- the message (printf fmt) -- if it begins
97 **			with a digit, this number overrides num.
98 **		a, b, c, d, e -- printf arguments
99 **
100 **	Returns:
101 **		none
102 **
103 **	Side Effects:
104 **		none.
105 */
106 
107 /*VARARGS2*/
108 message(num, msg, a, b, c, d, e)
109 	register char *num;
110 	register char *msg;
111 {
112 	errno = 0;
113 	fmtmsg(MsgBuf, CurEnv->e_to, num, msg, a, b, c, d, e);
114 	putmsg(MsgBuf);
115 }
116 /*
117 **  PUTMSG -- output error message to transcript and channel
118 **
119 **	Parameters:
120 **		msg -- message to output (in SMTP format).
121 **
122 **	Returns:
123 **		none.
124 **
125 **	Side Effects:
126 **		Outputs msg to the transcript.
127 **		If appropriate, outputs it to the channel.
128 **		Deletes SMTP reply code number as appropriate.
129 */
130 
131 putmsg(msg)
132 	char *msg;
133 {
134 	/* output to transcript */
135 	fprintf(Xscript, "%s\n", Smtp ? msg : &msg[4]);
136 
137 	/* output to channel if appropriate */
138 	if (!HoldErrs && (Verbose || msg[0] != '0'))
139 	{
140 		(void) fflush(stdout);
141 		if (ArpaMode)
142 			fprintf(OutChannel, "%s\r\n", msg);
143 		else
144 			fprintf(OutChannel, "%s\n", &msg[4]);
145 		(void) fflush(OutChannel);
146 	}
147 }
148 /*
149 **  FMTMSG -- format a message into buffer.
150 **
151 **	Parameters:
152 **		eb -- error buffer to get result.
153 **		to -- the recipient tag for this message.
154 **		num -- arpanet error number.
155 **		fmt -- format of string.
156 **		a, b, c, d, e -- arguments.
157 **
158 **	Returns:
159 **		none.
160 **
161 **	Side Effects:
162 **		none.
163 */
164 
165 /*VARARGS4*/
166 static
167 fmtmsg(eb, to, num, fmt, a, b, c, d, e)
168 	register char *eb;
169 	char *to;
170 	char *num;
171 	char *fmt;
172 {
173 	char del;
174 
175 	/* output the reply code */
176 	if (isdigit(*fmt))
177 	{
178 		num = fmt;
179 		fmt += 4;
180 	}
181 	if (num[3] == '-')
182 		del = '-';
183 	else
184 		del = ' ';
185 	(void) sprintf(eb, "%3.3s%c", num, del);
186 	eb += 4;
187 
188 	/* output the "to" person */
189 	if (to != NULL && to[0] != '\0')
190 	{
191 		(void) sprintf(eb, "%s... ", to);
192 		while (*eb != '\0')
193 			*eb++ &= 0177;
194 	}
195 
196 	/* output the message */
197 	(void) sprintf(eb, fmt, a, b, c, d, e);
198 	while (*eb != '\0')
199 		*eb++ &= 0177;
200 
201 	/* output the error code, if any */
202 	if (errno != 0)
203 	{
204 		extern int sys_nerr;
205 		extern char *sys_errlist[];
206 		if (errno < sys_nerr && errno > 0)
207 			(void) sprintf(eb, ": %s", sys_errlist[errno]);
208 		else
209 			(void) sprintf(eb, ": error %d", errno);
210 		eb += strlen(eb);
211 	}
212 }
213