1c2aa98e2SPeter Wemm /*
25dd76dd0SGregory Neil Shapiro * Copyright (c) 1998-2007, 2009 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro * All rights reserved.
4c2aa98e2SPeter Wemm * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5c2aa98e2SPeter Wemm * Copyright (c) 1988, 1993
6c2aa98e2SPeter Wemm * The Regents of the University of California. All rights reserved.
7c2aa98e2SPeter Wemm *
8c2aa98e2SPeter Wemm * By using this file, you agree to the terms and conditions set
9c2aa98e2SPeter Wemm * forth in the LICENSE file which can be found at the top level of
10c2aa98e2SPeter Wemm * the sendmail distribution.
11c2aa98e2SPeter Wemm *
12c2aa98e2SPeter Wemm */
13c2aa98e2SPeter Wemm
1406f25ae9SGregory Neil Shapiro #include <sendmail.h>
1540266059SGregory Neil Shapiro
164313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: util.c,v 8.427 2013-11-22 20:51:57 ca Exp $")
1740266059SGregory Neil Shapiro
18d0cef73dSGregory Neil Shapiro #include <sm/sendmail.h>
1940266059SGregory Neil Shapiro #include <sm/xtrap.h>
202fb4f839SGregory Neil Shapiro #if USE_EAI
212fb4f839SGregory Neil Shapiro # include <sm/ixlen.h>
222fb4f839SGregory Neil Shapiro #endif
2306f25ae9SGregory Neil Shapiro
2440266059SGregory Neil Shapiro /*
25e92d3f3fSGregory Neil Shapiro ** NEWSTR -- Create a copy of a C string
26e92d3f3fSGregory Neil Shapiro **
27e92d3f3fSGregory Neil Shapiro ** Parameters:
282fb4f839SGregory Neil Shapiro ** s -- the string to copy. [A]
29e92d3f3fSGregory Neil Shapiro **
30e92d3f3fSGregory Neil Shapiro ** Returns:
31e92d3f3fSGregory Neil Shapiro ** pointer to newly allocated string.
32e92d3f3fSGregory Neil Shapiro */
33e92d3f3fSGregory Neil Shapiro
34e92d3f3fSGregory Neil Shapiro char *
352fb4f839SGregory Neil Shapiro #if SM_HEAP_CHECK > 2
362fb4f839SGregory Neil Shapiro newstr_tagged(s, tag, line, group)
372fb4f839SGregory Neil Shapiro const char *s;
382fb4f839SGregory Neil Shapiro char *tag;
392fb4f839SGregory Neil Shapiro int line;
402fb4f839SGregory Neil Shapiro int group;
412fb4f839SGregory Neil Shapiro #else
42e92d3f3fSGregory Neil Shapiro newstr(s)
43e92d3f3fSGregory Neil Shapiro const char *s;
442fb4f839SGregory Neil Shapiro # define tag "newstr"
452fb4f839SGregory Neil Shapiro # define line 0
462fb4f839SGregory Neil Shapiro # define group 0
472fb4f839SGregory Neil Shapiro #endif
48e92d3f3fSGregory Neil Shapiro {
49e92d3f3fSGregory Neil Shapiro size_t l;
50e92d3f3fSGregory Neil Shapiro char *n;
51e92d3f3fSGregory Neil Shapiro
52e92d3f3fSGregory Neil Shapiro l = strlen(s);
53e92d3f3fSGregory Neil Shapiro SM_ASSERT(l + 1 > l);
542fb4f839SGregory Neil Shapiro n = sm_malloc_tagged_x(l + 1, tag, line, group);
55e92d3f3fSGregory Neil Shapiro sm_strlcpy(n, s, l + 1);
56e92d3f3fSGregory Neil Shapiro return n;
57e92d3f3fSGregory Neil Shapiro }
582fb4f839SGregory Neil Shapiro #if SM_HEAP_CHECK <= 2
592fb4f839SGregory Neil Shapiro # undef tag
602fb4f839SGregory Neil Shapiro # undef line
612fb4f839SGregory Neil Shapiro # undef group
622fb4f839SGregory Neil Shapiro #endif
63e92d3f3fSGregory Neil Shapiro
64e92d3f3fSGregory Neil Shapiro /*
65c2aa98e2SPeter Wemm ** ADDQUOTES -- Adds quotes & quote bits to a string.
66c2aa98e2SPeter Wemm **
6740266059SGregory Neil Shapiro ** Runs through a string and adds backslashes and quote bits.
68c2aa98e2SPeter Wemm **
69c2aa98e2SPeter Wemm ** Parameters:
702fb4f839SGregory Neil Shapiro ** s -- the string to modify. [A]
7140266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate result
72c2aa98e2SPeter Wemm **
73c2aa98e2SPeter Wemm ** Returns:
74c2aa98e2SPeter Wemm ** pointer to quoted string.
75c2aa98e2SPeter Wemm */
76c2aa98e2SPeter Wemm
77c2aa98e2SPeter Wemm char *
addquotes(s,rpool)7840266059SGregory Neil Shapiro addquotes(s, rpool)
79c2aa98e2SPeter Wemm char *s;
8040266059SGregory Neil Shapiro SM_RPOOL_T *rpool;
81c2aa98e2SPeter Wemm {
82c2aa98e2SPeter Wemm int len = 0;
83c2aa98e2SPeter Wemm char c;
84c2aa98e2SPeter Wemm char *p = s, *q, *r;
85c2aa98e2SPeter Wemm
86c2aa98e2SPeter Wemm if (s == NULL)
87c2aa98e2SPeter Wemm return NULL;
88c2aa98e2SPeter Wemm
89c2aa98e2SPeter Wemm /* Find length of quoted string */
90c2aa98e2SPeter Wemm while ((c = *p++) != '\0')
91c2aa98e2SPeter Wemm {
92c2aa98e2SPeter Wemm len++;
93c2aa98e2SPeter Wemm if (c == '\\' || c == '"')
94c2aa98e2SPeter Wemm len++;
95c2aa98e2SPeter Wemm }
96c2aa98e2SPeter Wemm
9740266059SGregory Neil Shapiro q = r = sm_rpool_malloc_x(rpool, len + 3);
98c2aa98e2SPeter Wemm p = s;
99c2aa98e2SPeter Wemm
100c2aa98e2SPeter Wemm /* add leading quote */
101c2aa98e2SPeter Wemm *q++ = '"';
102c2aa98e2SPeter Wemm while ((c = *p++) != '\0')
103c2aa98e2SPeter Wemm {
104c2aa98e2SPeter Wemm /* quote \ or " */
105c2aa98e2SPeter Wemm if (c == '\\' || c == '"')
106c2aa98e2SPeter Wemm *q++ = '\\';
107c2aa98e2SPeter Wemm *q++ = c;
108c2aa98e2SPeter Wemm }
109c2aa98e2SPeter Wemm *q++ = '"';
110c2aa98e2SPeter Wemm *q = '\0';
111c2aa98e2SPeter Wemm return r;
112c2aa98e2SPeter Wemm }
11313bd1963SGregory Neil Shapiro
11413bd1963SGregory Neil Shapiro /*
115b6bacd31SGregory Neil Shapiro ** STRIPBACKSLASH -- Strip all leading backslashes from a string, provided
116b6bacd31SGregory Neil Shapiro ** the following character is alpha-numerical.
11713bd1963SGregory Neil Shapiro ** This is done in place.
11813bd1963SGregory Neil Shapiro **
1192fb4f839SGregory Neil Shapiro ** XXX: This may be a problem for EAI?
1202fb4f839SGregory Neil Shapiro **
12113bd1963SGregory Neil Shapiro ** Parameters:
12213bd1963SGregory Neil Shapiro ** s -- the string to strip.
12313bd1963SGregory Neil Shapiro **
12413bd1963SGregory Neil Shapiro ** Returns:
12513bd1963SGregory Neil Shapiro ** none.
12613bd1963SGregory Neil Shapiro */
12713bd1963SGregory Neil Shapiro
12813bd1963SGregory Neil Shapiro void
stripbackslash(s)12913bd1963SGregory Neil Shapiro stripbackslash(s)
13013bd1963SGregory Neil Shapiro char *s;
13113bd1963SGregory Neil Shapiro {
13213bd1963SGregory Neil Shapiro char *p, *q, c;
13313bd1963SGregory Neil Shapiro
1342fb4f839SGregory Neil Shapiro if (SM_IS_EMPTY(s))
13513bd1963SGregory Neil Shapiro return;
13613bd1963SGregory Neil Shapiro p = q = s;
13713bd1963SGregory Neil Shapiro while (*p == '\\' && (p[1] == '\\' || (isascii(p[1]) && isalnum(p[1]))))
13813bd1963SGregory Neil Shapiro p++;
13913bd1963SGregory Neil Shapiro do
14013bd1963SGregory Neil Shapiro {
14113bd1963SGregory Neil Shapiro c = *q++ = *p++;
14213bd1963SGregory Neil Shapiro } while (c != '\0');
14313bd1963SGregory Neil Shapiro }
14413bd1963SGregory Neil Shapiro
14540266059SGregory Neil Shapiro /*
146c2aa98e2SPeter Wemm ** RFC822_STRING -- Checks string for proper RFC822 string quoting.
147c2aa98e2SPeter Wemm **
148c2aa98e2SPeter Wemm ** Runs through a string and verifies RFC822 special characters
149c2aa98e2SPeter Wemm ** are only found inside comments, quoted strings, or backslash
150c2aa98e2SPeter Wemm ** escaped. Also verified balanced quotes and parenthesis.
151c2aa98e2SPeter Wemm **
1522fb4f839SGregory Neil Shapiro ** XXX: This may be a problem for EAI? MustQuoteChars is used.
1532fb4f839SGregory Neil Shapiro ** If this returns false, current callers just invoke addquotes().
1542fb4f839SGregory Neil Shapiro **
155c2aa98e2SPeter Wemm ** Parameters:
1562fb4f839SGregory Neil Shapiro ** s -- the string to modify. [A]
157c2aa98e2SPeter Wemm **
158c2aa98e2SPeter Wemm ** Returns:
15940266059SGregory Neil Shapiro ** true iff the string is RFC822 compliant, false otherwise.
160c2aa98e2SPeter Wemm */
161c2aa98e2SPeter Wemm
162c2aa98e2SPeter Wemm bool
rfc822_string(s)163c2aa98e2SPeter Wemm rfc822_string(s)
164c2aa98e2SPeter Wemm char *s;
165c2aa98e2SPeter Wemm {
16640266059SGregory Neil Shapiro bool quoted = false;
167c2aa98e2SPeter Wemm int commentlev = 0;
168c2aa98e2SPeter Wemm char *c = s;
169c2aa98e2SPeter Wemm
170c2aa98e2SPeter Wemm if (s == NULL)
17140266059SGregory Neil Shapiro return false;
172c2aa98e2SPeter Wemm
173c2aa98e2SPeter Wemm while (*c != '\0')
174c2aa98e2SPeter Wemm {
175c2aa98e2SPeter Wemm /* escaped character */
176c2aa98e2SPeter Wemm if (*c == '\\')
177c2aa98e2SPeter Wemm {
178c2aa98e2SPeter Wemm c++;
179c2aa98e2SPeter Wemm if (*c == '\0')
18040266059SGregory Neil Shapiro return false;
181c2aa98e2SPeter Wemm }
182c2aa98e2SPeter Wemm else if (commentlev == 0 && *c == '"')
183c2aa98e2SPeter Wemm quoted = !quoted;
184c2aa98e2SPeter Wemm else if (!quoted)
185c2aa98e2SPeter Wemm {
186c2aa98e2SPeter Wemm if (*c == ')')
187c2aa98e2SPeter Wemm {
188c2aa98e2SPeter Wemm /* unbalanced ')' */
189c2aa98e2SPeter Wemm if (commentlev == 0)
19040266059SGregory Neil Shapiro return false;
191c2aa98e2SPeter Wemm else
192c2aa98e2SPeter Wemm commentlev--;
193c2aa98e2SPeter Wemm }
194c2aa98e2SPeter Wemm else if (*c == '(')
195c2aa98e2SPeter Wemm commentlev++;
196c2aa98e2SPeter Wemm else if (commentlev == 0 &&
197c2aa98e2SPeter Wemm strchr(MustQuoteChars, *c) != NULL)
19840266059SGregory Neil Shapiro return false;
199c2aa98e2SPeter Wemm }
200c2aa98e2SPeter Wemm c++;
201c2aa98e2SPeter Wemm }
20240266059SGregory Neil Shapiro
203c2aa98e2SPeter Wemm /* unbalanced '"' or '(' */
20440266059SGregory Neil Shapiro return !quoted && commentlev == 0;
205c2aa98e2SPeter Wemm }
206d0cef73dSGregory Neil Shapiro
20740266059SGregory Neil Shapiro /*
208065a643dSPeter Wemm ** SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string
209065a643dSPeter Wemm **
21006f25ae9SGregory Neil Shapiro ** Arbitrarily shorten (in place) an RFC822 string and rebalance
211065a643dSPeter Wemm ** comments and quotes.
212065a643dSPeter Wemm **
213065a643dSPeter Wemm ** Parameters:
2142fb4f839SGregory Neil Shapiro ** string -- the string to shorten [A]
215065a643dSPeter Wemm ** length -- the maximum size, 0 if no maximum
216065a643dSPeter Wemm **
217065a643dSPeter Wemm ** Returns:
21840266059SGregory Neil Shapiro ** true if string is changed, false otherwise
219065a643dSPeter Wemm **
220065a643dSPeter Wemm ** Side Effects:
221065a643dSPeter Wemm ** Changes string in place, possibly resulting
222065a643dSPeter Wemm ** in a shorter string.
223065a643dSPeter Wemm */
224065a643dSPeter Wemm
225065a643dSPeter Wemm bool
shorten_rfc822_string(string,length)226065a643dSPeter Wemm shorten_rfc822_string(string, length)
227065a643dSPeter Wemm char *string;
228065a643dSPeter Wemm size_t length;
229065a643dSPeter Wemm {
23040266059SGregory Neil Shapiro bool backslash = false;
23140266059SGregory Neil Shapiro bool modified = false;
23240266059SGregory Neil Shapiro bool quoted = false;
233065a643dSPeter Wemm size_t slen;
234065a643dSPeter Wemm int parencount = 0;
235065a643dSPeter Wemm char *ptr = string;
236065a643dSPeter Wemm
237065a643dSPeter Wemm /*
238065a643dSPeter Wemm ** If have to rebalance an already short enough string,
239065a643dSPeter Wemm ** need to do it within allocated space.
240065a643dSPeter Wemm */
241193538b7SGregory Neil Shapiro
242065a643dSPeter Wemm slen = strlen(string);
243065a643dSPeter Wemm if (length == 0 || slen < length)
244065a643dSPeter Wemm length = slen;
245065a643dSPeter Wemm
246065a643dSPeter Wemm while (*ptr != '\0')
247065a643dSPeter Wemm {
248065a643dSPeter Wemm if (backslash)
249065a643dSPeter Wemm {
25040266059SGregory Neil Shapiro backslash = false;
251065a643dSPeter Wemm goto increment;
252065a643dSPeter Wemm }
253065a643dSPeter Wemm
254065a643dSPeter Wemm if (*ptr == '\\')
25540266059SGregory Neil Shapiro backslash = true;
256065a643dSPeter Wemm else if (*ptr == '(')
257065a643dSPeter Wemm {
258065a643dSPeter Wemm if (!quoted)
259065a643dSPeter Wemm parencount++;
260065a643dSPeter Wemm }
261065a643dSPeter Wemm else if (*ptr == ')')
262065a643dSPeter Wemm {
263065a643dSPeter Wemm if (--parencount < 0)
264065a643dSPeter Wemm parencount = 0;
265065a643dSPeter Wemm }
266065a643dSPeter Wemm
267065a643dSPeter Wemm /* Inside a comment, quotes don't matter */
268065a643dSPeter Wemm if (parencount <= 0 && *ptr == '"')
269065a643dSPeter Wemm quoted = !quoted;
270065a643dSPeter Wemm
271065a643dSPeter Wemm increment:
272065a643dSPeter Wemm /* Check for sufficient space for next character */
27306f25ae9SGregory Neil Shapiro if (length - (ptr - string) <= (size_t) ((backslash ? 1 : 0) +
274065a643dSPeter Wemm parencount +
275065a643dSPeter Wemm (quoted ? 1 : 0)))
276065a643dSPeter Wemm {
277065a643dSPeter Wemm /* Not enough, backtrack */
278065a643dSPeter Wemm if (*ptr == '\\')
27940266059SGregory Neil Shapiro backslash = false;
280065a643dSPeter Wemm else if (*ptr == '(' && !quoted)
281065a643dSPeter Wemm parencount--;
282065a643dSPeter Wemm else if (*ptr == '"' && parencount == 0)
28340266059SGregory Neil Shapiro quoted = false;
284065a643dSPeter Wemm break;
285065a643dSPeter Wemm }
286065a643dSPeter Wemm ptr++;
287065a643dSPeter Wemm }
288065a643dSPeter Wemm
289065a643dSPeter Wemm /* Rebalance */
290065a643dSPeter Wemm while (parencount-- > 0)
291065a643dSPeter Wemm {
292065a643dSPeter Wemm if (*ptr != ')')
293065a643dSPeter Wemm {
29440266059SGregory Neil Shapiro modified = true;
295065a643dSPeter Wemm *ptr = ')';
296065a643dSPeter Wemm }
297065a643dSPeter Wemm ptr++;
298065a643dSPeter Wemm }
299065a643dSPeter Wemm if (quoted)
300065a643dSPeter Wemm {
301065a643dSPeter Wemm if (*ptr != '"')
302065a643dSPeter Wemm {
30340266059SGregory Neil Shapiro modified = true;
304065a643dSPeter Wemm *ptr = '"';
305065a643dSPeter Wemm }
306065a643dSPeter Wemm ptr++;
307065a643dSPeter Wemm }
308065a643dSPeter Wemm if (*ptr != '\0')
309065a643dSPeter Wemm {
31040266059SGregory Neil Shapiro modified = true;
311065a643dSPeter Wemm *ptr = '\0';
312065a643dSPeter Wemm }
313065a643dSPeter Wemm return modified;
314065a643dSPeter Wemm }
315d0cef73dSGregory Neil Shapiro
31640266059SGregory Neil Shapiro /*
317065a643dSPeter Wemm ** FIND_CHARACTER -- find an unquoted character in an RFC822 string
318065a643dSPeter Wemm **
319065a643dSPeter Wemm ** Find an unquoted, non-commented character in an RFC822
3202fb4f839SGregory Neil Shapiro ** string and return a pointer to its location in the string.
321065a643dSPeter Wemm **
322065a643dSPeter Wemm ** Parameters:
3232fb4f839SGregory Neil Shapiro ** string -- the string to search [A]
324065a643dSPeter Wemm ** character -- the character to find
325065a643dSPeter Wemm **
326065a643dSPeter Wemm ** Returns:
327065a643dSPeter Wemm ** pointer to the character, or
328065a643dSPeter Wemm ** a pointer to the end of the line if character is not found
329065a643dSPeter Wemm */
330065a643dSPeter Wemm
331065a643dSPeter Wemm char *
find_character(string,character)332065a643dSPeter Wemm find_character(string, character)
333065a643dSPeter Wemm char *string;
33406f25ae9SGregory Neil Shapiro int character;
335065a643dSPeter Wemm {
33640266059SGregory Neil Shapiro bool backslash = false;
33740266059SGregory Neil Shapiro bool quoted = false;
338065a643dSPeter Wemm int parencount = 0;
339065a643dSPeter Wemm
340065a643dSPeter Wemm while (string != NULL && *string != '\0')
341065a643dSPeter Wemm {
342065a643dSPeter Wemm if (backslash)
343065a643dSPeter Wemm {
34440266059SGregory Neil Shapiro backslash = false;
345065a643dSPeter Wemm if (!quoted && character == '\\' && *string == '\\')
346065a643dSPeter Wemm break;
347065a643dSPeter Wemm string++;
348065a643dSPeter Wemm continue;
349065a643dSPeter Wemm }
350065a643dSPeter Wemm switch (*string)
351065a643dSPeter Wemm {
352065a643dSPeter Wemm case '\\':
35340266059SGregory Neil Shapiro backslash = true;
354065a643dSPeter Wemm break;
355065a643dSPeter Wemm
356065a643dSPeter Wemm case '(':
357065a643dSPeter Wemm if (!quoted)
358065a643dSPeter Wemm parencount++;
359065a643dSPeter Wemm break;
360065a643dSPeter Wemm
361065a643dSPeter Wemm case ')':
362065a643dSPeter Wemm if (--parencount < 0)
363065a643dSPeter Wemm parencount = 0;
364065a643dSPeter Wemm break;
365065a643dSPeter Wemm }
366065a643dSPeter Wemm
367065a643dSPeter Wemm /* Inside a comment, nothing matters */
368065a643dSPeter Wemm if (parencount > 0)
369065a643dSPeter Wemm {
370065a643dSPeter Wemm string++;
371065a643dSPeter Wemm continue;
372065a643dSPeter Wemm }
373065a643dSPeter Wemm
374065a643dSPeter Wemm if (*string == '"')
375065a643dSPeter Wemm quoted = !quoted;
376065a643dSPeter Wemm else if (*string == character && !quoted)
377065a643dSPeter Wemm break;
378065a643dSPeter Wemm string++;
379065a643dSPeter Wemm }
380065a643dSPeter Wemm
381065a643dSPeter Wemm /* Return pointer to the character */
382065a643dSPeter Wemm return string;
383065a643dSPeter Wemm }
38440266059SGregory Neil Shapiro
38540266059SGregory Neil Shapiro /*
38640266059SGregory Neil Shapiro ** CHECK_BODYTYPE -- check bodytype parameter
387c2aa98e2SPeter Wemm **
38840266059SGregory Neil Shapiro ** Parameters:
38940266059SGregory Neil Shapiro ** bodytype -- bodytype parameter
39040266059SGregory Neil Shapiro **
39140266059SGregory Neil Shapiro ** Returns:
39240266059SGregory Neil Shapiro ** BODYTYPE_* according to parameter
39340266059SGregory Neil Shapiro **
39440266059SGregory Neil Shapiro */
39540266059SGregory Neil Shapiro
39640266059SGregory Neil Shapiro int
check_bodytype(bodytype)39740266059SGregory Neil Shapiro check_bodytype(bodytype)
39840266059SGregory Neil Shapiro char *bodytype;
39940266059SGregory Neil Shapiro {
40040266059SGregory Neil Shapiro /* check body type for legality */
40140266059SGregory Neil Shapiro if (bodytype == NULL)
40240266059SGregory Neil Shapiro return BODYTYPE_NONE;
4032fb4f839SGregory Neil Shapiro if (SM_STRCASEEQ(bodytype, "7bit"))
40440266059SGregory Neil Shapiro return BODYTYPE_7BIT;
4052fb4f839SGregory Neil Shapiro if (SM_STRCASEEQ(bodytype, "8bitmime"))
40640266059SGregory Neil Shapiro return BODYTYPE_8BITMIME;
40740266059SGregory Neil Shapiro return BODYTYPE_ILLEGAL;
40840266059SGregory Neil Shapiro }
40940266059SGregory Neil Shapiro
41040266059SGregory Neil Shapiro /*
41140266059SGregory Neil Shapiro ** TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..."
41240266059SGregory Neil Shapiro **
41340266059SGregory Neil Shapiro ** Parameters:
4142fb4f839SGregory Neil Shapiro ** str -- string to truncate [A]
41540266059SGregory Neil Shapiro ** len -- maximum length (including '\0') (0 for unlimited)
41640266059SGregory Neil Shapiro ** delim -- delimiter character
41740266059SGregory Neil Shapiro **
41840266059SGregory Neil Shapiro ** Returns:
41940266059SGregory Neil Shapiro ** None.
42040266059SGregory Neil Shapiro */
42140266059SGregory Neil Shapiro
42240266059SGregory Neil Shapiro void
truncate_at_delim(str,len,delim)42340266059SGregory Neil Shapiro truncate_at_delim(str, len, delim)
42440266059SGregory Neil Shapiro char *str;
42540266059SGregory Neil Shapiro size_t len;
42640266059SGregory Neil Shapiro int delim;
42740266059SGregory Neil Shapiro {
42840266059SGregory Neil Shapiro char *p;
42940266059SGregory Neil Shapiro
43040266059SGregory Neil Shapiro if (str == NULL || len == 0 || strlen(str) < len)
43140266059SGregory Neil Shapiro return;
43240266059SGregory Neil Shapiro
43340266059SGregory Neil Shapiro *(str + len - 1) = '\0';
43440266059SGregory Neil Shapiro while ((p = strrchr(str, delim)) != NULL)
43540266059SGregory Neil Shapiro {
43640266059SGregory Neil Shapiro *p = '\0';
43740266059SGregory Neil Shapiro if (p - str + 4 < len)
43840266059SGregory Neil Shapiro {
439a7ec597cSGregory Neil Shapiro *p++ = (char) delim;
44040266059SGregory Neil Shapiro *p = '\0';
44140266059SGregory Neil Shapiro (void) sm_strlcat(str, "...", len);
44240266059SGregory Neil Shapiro return;
44340266059SGregory Neil Shapiro }
44440266059SGregory Neil Shapiro }
44540266059SGregory Neil Shapiro
44640266059SGregory Neil Shapiro /* Couldn't find a place to append "..." */
44740266059SGregory Neil Shapiro if (len > 3)
44840266059SGregory Neil Shapiro (void) sm_strlcpy(str, "...", len);
44940266059SGregory Neil Shapiro else
45040266059SGregory Neil Shapiro str[0] = '\0';
45140266059SGregory Neil Shapiro }
452d0cef73dSGregory Neil Shapiro
45340266059SGregory Neil Shapiro /*
45440266059SGregory Neil Shapiro ** XALLOC -- Allocate memory, raise an exception on error
455c2aa98e2SPeter Wemm **
456c2aa98e2SPeter Wemm ** Parameters:
457c2aa98e2SPeter Wemm ** sz -- size of area to allocate.
458c2aa98e2SPeter Wemm **
459c2aa98e2SPeter Wemm ** Returns:
460c2aa98e2SPeter Wemm ** pointer to data region.
461c2aa98e2SPeter Wemm **
46240266059SGregory Neil Shapiro ** Exceptions:
46340266059SGregory Neil Shapiro ** SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory
46440266059SGregory Neil Shapiro **
465c2aa98e2SPeter Wemm ** Side Effects:
466c2aa98e2SPeter Wemm ** Memory is allocated.
467c2aa98e2SPeter Wemm */
468c2aa98e2SPeter Wemm
469c2aa98e2SPeter Wemm char *
47040266059SGregory Neil Shapiro #if SM_HEAP_CHECK
xalloc_tagged(sz,file,line)47140266059SGregory Neil Shapiro xalloc_tagged(sz, file, line)
47240266059SGregory Neil Shapiro register int sz;
47340266059SGregory Neil Shapiro char *file;
47440266059SGregory Neil Shapiro int line;
47540266059SGregory Neil Shapiro #else /* SM_HEAP_CHECK */
476c2aa98e2SPeter Wemm xalloc(sz)
477c2aa98e2SPeter Wemm register int sz;
47840266059SGregory Neil Shapiro #endif /* SM_HEAP_CHECK */
479c2aa98e2SPeter Wemm {
480c2aa98e2SPeter Wemm register char *p;
481c2aa98e2SPeter Wemm
4824e4196cbSGregory Neil Shapiro SM_REQUIRE(sz >= 0);
4834e4196cbSGregory Neil Shapiro
484c2aa98e2SPeter Wemm /* some systems can't handle size zero mallocs */
485c2aa98e2SPeter Wemm if (sz <= 0)
486c2aa98e2SPeter Wemm sz = 1;
487c2aa98e2SPeter Wemm
48840266059SGregory Neil Shapiro /* scaffolding for testing error handling code */
48940266059SGregory Neil Shapiro sm_xtrap_raise_x(&SmHeapOutOfMemory);
49040266059SGregory Neil Shapiro
49140266059SGregory Neil Shapiro p = sm_malloc_tagged((unsigned) sz, file, line, sm_heap_group());
492c2aa98e2SPeter Wemm if (p == NULL)
493c2aa98e2SPeter Wemm {
49440266059SGregory Neil Shapiro sm_exc_raise_x(&SmHeapOutOfMemory);
495c2aa98e2SPeter Wemm }
49606f25ae9SGregory Neil Shapiro return p;
497c2aa98e2SPeter Wemm }
498d0cef73dSGregory Neil Shapiro
49940266059SGregory Neil Shapiro /*
500c2aa98e2SPeter Wemm ** COPYPLIST -- copy list of pointers.
501c2aa98e2SPeter Wemm **
50240266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for lists of
503c2aa98e2SPeter Wemm ** pointers.
504c2aa98e2SPeter Wemm **
505c2aa98e2SPeter Wemm ** Parameters:
506c2aa98e2SPeter Wemm ** list -- list of pointers to copy.
507c2aa98e2SPeter Wemm ** Must be NULL terminated.
50840266059SGregory Neil Shapiro ** copycont -- if true, copy the contents of the vector
509c2aa98e2SPeter Wemm ** (which must be a string) also.
51040266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage,
51140266059SGregory Neil Shapiro ** or NULL
512c2aa98e2SPeter Wemm **
513c2aa98e2SPeter Wemm ** Returns:
514c2aa98e2SPeter Wemm ** a copy of 'list'.
515c2aa98e2SPeter Wemm */
516c2aa98e2SPeter Wemm
517c2aa98e2SPeter Wemm char **
copyplist(list,copycont,rpool)51840266059SGregory Neil Shapiro copyplist(list, copycont, rpool)
519c2aa98e2SPeter Wemm char **list;
520c2aa98e2SPeter Wemm bool copycont;
52140266059SGregory Neil Shapiro SM_RPOOL_T *rpool;
522c2aa98e2SPeter Wemm {
523c2aa98e2SPeter Wemm register char **vp;
524c2aa98e2SPeter Wemm register char **newvp;
525c2aa98e2SPeter Wemm
526c2aa98e2SPeter Wemm for (vp = list; *vp != NULL; vp++)
527c2aa98e2SPeter Wemm continue;
528c2aa98e2SPeter Wemm
529c2aa98e2SPeter Wemm vp++;
530c2aa98e2SPeter Wemm
5312fb4f839SGregory Neil Shapiro /*
5322fb4f839SGregory Neil Shapiro ** Hack: rpool is NULL if invoked from readcf(),
5332fb4f839SGregory Neil Shapiro ** so "ignore" the allocation by setting group to 0.
5342fb4f839SGregory Neil Shapiro */
5352fb4f839SGregory Neil Shapiro
5362fb4f839SGregory Neil Shapiro newvp = (char **) sm_rpool_malloc_tagged_x(rpool,
5372fb4f839SGregory Neil Shapiro (vp - list) * sizeof(*vp), "copyplist", 0,
5382fb4f839SGregory Neil Shapiro NULL == rpool ? 0 : 1);
539d0cef73dSGregory Neil Shapiro memmove((char *) newvp, (char *) list, (int) (vp - list) * sizeof(*vp));
540c2aa98e2SPeter Wemm
541c2aa98e2SPeter Wemm if (copycont)
542c2aa98e2SPeter Wemm {
543c2aa98e2SPeter Wemm for (vp = newvp; *vp != NULL; vp++)
5442fb4f839SGregory Neil Shapiro *vp = sm_rpool_strdup_tagged_x(rpool, *vp,
5452fb4f839SGregory Neil Shapiro "copyplist", 0, NULL == rpool ? 0 : 1);
546c2aa98e2SPeter Wemm }
547c2aa98e2SPeter Wemm
54806f25ae9SGregory Neil Shapiro return newvp;
549c2aa98e2SPeter Wemm }
550d0cef73dSGregory Neil Shapiro
55140266059SGregory Neil Shapiro /*
552c2aa98e2SPeter Wemm ** COPYQUEUE -- copy address queue.
553c2aa98e2SPeter Wemm **
55440266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for address queues;
55506f25ae9SGregory Neil Shapiro ** addresses marked as QS_IS_DEAD() aren't copied
556c2aa98e2SPeter Wemm **
557c2aa98e2SPeter Wemm ** Parameters:
558c2aa98e2SPeter Wemm ** addr -- list of address structures to copy.
55940266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage
560c2aa98e2SPeter Wemm **
561c2aa98e2SPeter Wemm ** Returns:
562c2aa98e2SPeter Wemm ** a copy of 'addr'.
563c2aa98e2SPeter Wemm */
564c2aa98e2SPeter Wemm
565c2aa98e2SPeter Wemm ADDRESS *
copyqueue(addr,rpool)56640266059SGregory Neil Shapiro copyqueue(addr, rpool)
567c2aa98e2SPeter Wemm ADDRESS *addr;
56840266059SGregory Neil Shapiro SM_RPOOL_T *rpool;
569c2aa98e2SPeter Wemm {
570c2aa98e2SPeter Wemm register ADDRESS *newaddr;
571c2aa98e2SPeter Wemm ADDRESS *ret;
572c2aa98e2SPeter Wemm register ADDRESS **tail = &ret;
573c2aa98e2SPeter Wemm
574c2aa98e2SPeter Wemm while (addr != NULL)
575c2aa98e2SPeter Wemm {
57606f25ae9SGregory Neil Shapiro if (!QS_IS_DEAD(addr->q_state))
577c2aa98e2SPeter Wemm {
57840266059SGregory Neil Shapiro newaddr = (ADDRESS *) sm_rpool_malloc_x(rpool,
579d0cef73dSGregory Neil Shapiro sizeof(*newaddr));
580c2aa98e2SPeter Wemm STRUCTCOPY(*addr, *newaddr);
581c2aa98e2SPeter Wemm *tail = newaddr;
582c2aa98e2SPeter Wemm tail = &newaddr->q_next;
583c2aa98e2SPeter Wemm }
584c2aa98e2SPeter Wemm addr = addr->q_next;
585c2aa98e2SPeter Wemm }
586c2aa98e2SPeter Wemm *tail = NULL;
587c2aa98e2SPeter Wemm
588c2aa98e2SPeter Wemm return ret;
589c2aa98e2SPeter Wemm }
590d0cef73dSGregory Neil Shapiro
59140266059SGregory Neil Shapiro /*
59206f25ae9SGregory Neil Shapiro ** LOG_SENDMAIL_PID -- record sendmail pid and command line.
59306f25ae9SGregory Neil Shapiro **
59406f25ae9SGregory Neil Shapiro ** Parameters:
59506f25ae9SGregory Neil Shapiro ** e -- the current envelope.
59606f25ae9SGregory Neil Shapiro **
59706f25ae9SGregory Neil Shapiro ** Returns:
59806f25ae9SGregory Neil Shapiro ** none.
59906f25ae9SGregory Neil Shapiro **
60006f25ae9SGregory Neil Shapiro ** Side Effects:
60140266059SGregory Neil Shapiro ** writes pidfile, logs command line.
602e92d3f3fSGregory Neil Shapiro ** keeps file open and locked to prevent overwrite of active file
60306f25ae9SGregory Neil Shapiro */
60406f25ae9SGregory Neil Shapiro
605e92d3f3fSGregory Neil Shapiro static SM_FILE_T *Pidf = NULL;
606e92d3f3fSGregory Neil Shapiro
60706f25ae9SGregory Neil Shapiro void
log_sendmail_pid(e)60806f25ae9SGregory Neil Shapiro log_sendmail_pid(e)
60906f25ae9SGregory Neil Shapiro ENVELOPE *e;
61006f25ae9SGregory Neil Shapiro {
61106f25ae9SGregory Neil Shapiro long sff;
61294c01205SGregory Neil Shapiro char pidpath[MAXPATHLEN];
61340266059SGregory Neil Shapiro extern char *CommandLineArgs;
61406f25ae9SGregory Neil Shapiro
61506f25ae9SGregory Neil Shapiro /* write the pid to the log file for posterity */
616e92d3f3fSGregory Neil Shapiro sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK;
61706f25ae9SGregory Neil Shapiro if (TrustedUid != 0 && RealUid == TrustedUid)
61806f25ae9SGregory Neil Shapiro sff |= SFF_OPENASROOT;
619d0cef73dSGregory Neil Shapiro expand(PidFile, pidpath, sizeof(pidpath), e);
620e92d3f3fSGregory Neil Shapiro Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff);
621e92d3f3fSGregory Neil Shapiro if (Pidf == NULL)
62206f25ae9SGregory Neil Shapiro {
623e92d3f3fSGregory Neil Shapiro if (errno == EWOULDBLOCK)
624e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID,
625e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: file in use by another process",
626e92d3f3fSGregory Neil Shapiro pidpath);
627e92d3f3fSGregory Neil Shapiro else
628e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID,
629e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: %s",
63040266059SGregory Neil Shapiro pidpath, sm_errstring(errno));
63106f25ae9SGregory Neil Shapiro }
63206f25ae9SGregory Neil Shapiro else
63306f25ae9SGregory Neil Shapiro {
634e92d3f3fSGregory Neil Shapiro PidFilePid = getpid();
635193538b7SGregory Neil Shapiro
63606f25ae9SGregory Neil Shapiro /* write the process id on line 1 */
637e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n",
638e92d3f3fSGregory Neil Shapiro (long) PidFilePid);
63906f25ae9SGregory Neil Shapiro
64006f25ae9SGregory Neil Shapiro /* line 2 contains all command line flags */
641e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n",
64240266059SGregory Neil Shapiro CommandLineArgs);
64306f25ae9SGregory Neil Shapiro
644e92d3f3fSGregory Neil Shapiro /* flush */
645e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(Pidf, SM_TIME_DEFAULT);
646e92d3f3fSGregory Neil Shapiro
647e92d3f3fSGregory Neil Shapiro /*
648e92d3f3fSGregory Neil Shapiro ** Leave pid file open until process ends
649e92d3f3fSGregory Neil Shapiro ** so it's not overwritten by another
650e92d3f3fSGregory Neil Shapiro ** process.
651e92d3f3fSGregory Neil Shapiro */
65206f25ae9SGregory Neil Shapiro }
65340266059SGregory Neil Shapiro if (LogLevel > 9)
65440266059SGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs);
65506f25ae9SGregory Neil Shapiro }
656e92d3f3fSGregory Neil Shapiro
657e92d3f3fSGregory Neil Shapiro /*
658e92d3f3fSGregory Neil Shapiro ** CLOSE_SENDMAIL_PID -- close sendmail pid file
659e92d3f3fSGregory Neil Shapiro **
660e92d3f3fSGregory Neil Shapiro ** Parameters:
661e92d3f3fSGregory Neil Shapiro ** none.
662e92d3f3fSGregory Neil Shapiro **
663e92d3f3fSGregory Neil Shapiro ** Returns:
664e92d3f3fSGregory Neil Shapiro ** none.
665e92d3f3fSGregory Neil Shapiro */
666e92d3f3fSGregory Neil Shapiro
667e92d3f3fSGregory Neil Shapiro void
close_sendmail_pid()668e92d3f3fSGregory Neil Shapiro close_sendmail_pid()
669e92d3f3fSGregory Neil Shapiro {
670e92d3f3fSGregory Neil Shapiro if (Pidf == NULL)
671e92d3f3fSGregory Neil Shapiro return;
672e92d3f3fSGregory Neil Shapiro
673e92d3f3fSGregory Neil Shapiro (void) sm_io_close(Pidf, SM_TIME_DEFAULT);
674e92d3f3fSGregory Neil Shapiro Pidf = NULL;
675e92d3f3fSGregory Neil Shapiro }
676e92d3f3fSGregory Neil Shapiro
67740266059SGregory Neil Shapiro /*
67806f25ae9SGregory Neil Shapiro ** SET_DELIVERY_MODE -- set and record the delivery mode
67906f25ae9SGregory Neil Shapiro **
68006f25ae9SGregory Neil Shapiro ** Parameters:
68106f25ae9SGregory Neil Shapiro ** mode -- delivery mode
68206f25ae9SGregory Neil Shapiro ** e -- the current envelope.
68306f25ae9SGregory Neil Shapiro **
68406f25ae9SGregory Neil Shapiro ** Returns:
68506f25ae9SGregory Neil Shapiro ** none.
68606f25ae9SGregory Neil Shapiro **
68706f25ae9SGregory Neil Shapiro ** Side Effects:
68840266059SGregory Neil Shapiro ** sets {deliveryMode} macro
68906f25ae9SGregory Neil Shapiro */
69006f25ae9SGregory Neil Shapiro
69106f25ae9SGregory Neil Shapiro void
set_delivery_mode(mode,e)69206f25ae9SGregory Neil Shapiro set_delivery_mode(mode, e)
69306f25ae9SGregory Neil Shapiro int mode;
69406f25ae9SGregory Neil Shapiro ENVELOPE *e;
69506f25ae9SGregory Neil Shapiro {
69606f25ae9SGregory Neil Shapiro char buf[2];
69706f25ae9SGregory Neil Shapiro
69806f25ae9SGregory Neil Shapiro e->e_sendmode = (char) mode;
69906f25ae9SGregory Neil Shapiro buf[0] = (char) mode;
70006f25ae9SGregory Neil Shapiro buf[1] = '\0';
70140266059SGregory Neil Shapiro macdefine(&e->e_macro, A_TEMP, macid("{deliveryMode}"), buf);
70206f25ae9SGregory Neil Shapiro }
703d0cef73dSGregory Neil Shapiro
70440266059SGregory Neil Shapiro /*
70540266059SGregory Neil Shapiro ** SET_OP_MODE -- set and record the op mode
70640266059SGregory Neil Shapiro **
70740266059SGregory Neil Shapiro ** Parameters:
70840266059SGregory Neil Shapiro ** mode -- op mode
70940266059SGregory Neil Shapiro ** e -- the current envelope.
71040266059SGregory Neil Shapiro **
71140266059SGregory Neil Shapiro ** Returns:
71240266059SGregory Neil Shapiro ** none.
71340266059SGregory Neil Shapiro **
71440266059SGregory Neil Shapiro ** Side Effects:
71540266059SGregory Neil Shapiro ** sets {opMode} macro
71640266059SGregory Neil Shapiro */
71740266059SGregory Neil Shapiro
71840266059SGregory Neil Shapiro void
set_op_mode(mode)71940266059SGregory Neil Shapiro set_op_mode(mode)
72040266059SGregory Neil Shapiro int mode;
72140266059SGregory Neil Shapiro {
72240266059SGregory Neil Shapiro char buf[2];
72340266059SGregory Neil Shapiro extern ENVELOPE BlankEnvelope;
72440266059SGregory Neil Shapiro
72540266059SGregory Neil Shapiro OpMode = (char) mode;
72640266059SGregory Neil Shapiro buf[0] = (char) mode;
72740266059SGregory Neil Shapiro buf[1] = '\0';
72840266059SGregory Neil Shapiro macdefine(&BlankEnvelope.e_macro, A_TEMP, MID_OPMODE, buf);
72940266059SGregory Neil Shapiro }
730d0cef73dSGregory Neil Shapiro
73140266059SGregory Neil Shapiro /*
732c2aa98e2SPeter Wemm ** PRINTAV -- print argument vector.
733c2aa98e2SPeter Wemm **
734c2aa98e2SPeter Wemm ** Parameters:
735e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer.
736c2aa98e2SPeter Wemm ** av -- argument vector.
737c2aa98e2SPeter Wemm **
738c2aa98e2SPeter Wemm ** Returns:
739c2aa98e2SPeter Wemm ** none.
740c2aa98e2SPeter Wemm **
741c2aa98e2SPeter Wemm ** Side Effects:
742c2aa98e2SPeter Wemm ** prints av.
743c2aa98e2SPeter Wemm */
744c2aa98e2SPeter Wemm
745c2aa98e2SPeter Wemm void
printav(fp,av)746e92d3f3fSGregory Neil Shapiro printav(fp, av)
747e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp;
748d0cef73dSGregory Neil Shapiro char **av;
749c2aa98e2SPeter Wemm {
750c2aa98e2SPeter Wemm while (*av != NULL)
751c2aa98e2SPeter Wemm {
7522fb4f839SGregory Neil Shapiro #if _FFR_8BITENVADDR
7532fb4f839SGregory Neil Shapiro if (tTd(0, 9))
7542fb4f839SGregory Neil Shapiro {
7552fb4f839SGregory Neil Shapiro char *cp;
7562fb4f839SGregory Neil Shapiro unsigned char v;
7572fb4f839SGregory Neil Shapiro
7582fb4f839SGregory Neil Shapiro for (cp = *av++; *cp != '\0'; cp++) {
7592fb4f839SGregory Neil Shapiro v = (unsigned char)(*cp & 0x00ff);
760*d39bd2c1SGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, v);
7612fb4f839SGregory Neil Shapiro # if 0
7622fb4f839SGregory Neil Shapiro if (isascii(v) && isprint(v))
763*d39bd2c1SGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT,
764*d39bd2c1SGregory Neil Shapiro v);
7652fb4f839SGregory Neil Shapiro else
766*d39bd2c1SGregory Neil Shapiro (void) sm_io_fprintf(fp,
767*d39bd2c1SGregory Neil Shapiro SM_TIME_DEFAULT, "\\x%hhx", v);
7682fb4f839SGregory Neil Shapiro # endif
7692fb4f839SGregory Neil Shapiro }
7702fb4f839SGregory Neil Shapiro if (*av != NULL)
771*d39bd2c1SGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, ' ');
7722fb4f839SGregory Neil Shapiro continue;
7732fb4f839SGregory Neil Shapiro }
7742fb4f839SGregory Neil Shapiro #endif /* _FFR_8BITENVADDR */
775c2aa98e2SPeter Wemm if (tTd(0, 44))
776*d39bd2c1SGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
777*d39bd2c1SGregory Neil Shapiro "\n\t%08lx=", (unsigned long) *av);
778c2aa98e2SPeter Wemm else
779e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, ' ');
780d0cef73dSGregory Neil Shapiro if (tTd(0, 99))
781*d39bd2c1SGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
782*d39bd2c1SGregory Neil Shapiro "%s", str2prt(*av++));
783d0cef73dSGregory Neil Shapiro else
784e92d3f3fSGregory Neil Shapiro xputs(fp, *av++);
785c2aa98e2SPeter Wemm }
7862fb4f839SGregory Neil Shapiro
7872fb4f839SGregory Neil Shapiro /* don't print this if invoked directly (not via xputs())? */
788e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\n');
789c2aa98e2SPeter Wemm }
790d0cef73dSGregory Neil Shapiro
79140266059SGregory Neil Shapiro /*
792c2aa98e2SPeter Wemm ** XPUTS -- put string doing control escapes.
793c2aa98e2SPeter Wemm **
794c2aa98e2SPeter Wemm ** Parameters:
795e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer.
7962fb4f839SGregory Neil Shapiro ** s -- string to put. [A]
797c2aa98e2SPeter Wemm **
798c2aa98e2SPeter Wemm ** Returns:
799c2aa98e2SPeter Wemm ** none.
800c2aa98e2SPeter Wemm **
801c2aa98e2SPeter Wemm ** Side Effects:
802c2aa98e2SPeter Wemm ** output to stdout
803c2aa98e2SPeter Wemm */
804c2aa98e2SPeter Wemm
805c2aa98e2SPeter Wemm void
xputs(fp,s)806e92d3f3fSGregory Neil Shapiro xputs(fp, s)
807e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp;
808d0cef73dSGregory Neil Shapiro const char *s;
809c2aa98e2SPeter Wemm {
810d0cef73dSGregory Neil Shapiro int c;
811d0cef73dSGregory Neil Shapiro struct metamac *mp;
81240266059SGregory Neil Shapiro bool shiftout = false;
813c2aa98e2SPeter Wemm extern struct metamac MetaMacros[];
81440266059SGregory Neil Shapiro static SM_DEBUG_T DebugANSI = SM_DEBUG_INITIALIZER("ANSI",
81540266059SGregory Neil Shapiro "@(#)$Debug: ANSI - enable reverse video in debug output $");
8162fb4f839SGregory Neil Shapiro #if _FFR_8BITENVADDR
8172fb4f839SGregory Neil Shapiro if (tTd(0, 9))
8182fb4f839SGregory Neil Shapiro {
8192fb4f839SGregory Neil Shapiro char *av[2];
8202fb4f839SGregory Neil Shapiro
8212fb4f839SGregory Neil Shapiro av[0] = (char *) s;
8222fb4f839SGregory Neil Shapiro av[1] = NULL;
8232fb4f839SGregory Neil Shapiro printav(fp, av);
8242fb4f839SGregory Neil Shapiro return;
8252fb4f839SGregory Neil Shapiro }
8262fb4f839SGregory Neil Shapiro #endif
82740266059SGregory Neil Shapiro
82840266059SGregory Neil Shapiro /*
82940266059SGregory Neil Shapiro ** TermEscape is set here, rather than in main(),
83040266059SGregory Neil Shapiro ** because ANSI mode can be turned on or off at any time
83140266059SGregory Neil Shapiro ** if we are in -bt rule testing mode.
83240266059SGregory Neil Shapiro */
83340266059SGregory Neil Shapiro
83440266059SGregory Neil Shapiro if (sm_debug_unknown(&DebugANSI))
83540266059SGregory Neil Shapiro {
83640266059SGregory Neil Shapiro if (sm_debug_active(&DebugANSI, 1))
83740266059SGregory Neil Shapiro {
83840266059SGregory Neil Shapiro TermEscape.te_rv_on = "\033[7m";
839d0cef73dSGregory Neil Shapiro TermEscape.te_normal = "\033[0m";
84040266059SGregory Neil Shapiro }
84140266059SGregory Neil Shapiro else
84240266059SGregory Neil Shapiro {
84340266059SGregory Neil Shapiro TermEscape.te_rv_on = "";
844d0cef73dSGregory Neil Shapiro TermEscape.te_normal = "";
84540266059SGregory Neil Shapiro }
84640266059SGregory Neil Shapiro }
847c2aa98e2SPeter Wemm
848c2aa98e2SPeter Wemm if (s == NULL)
849c2aa98e2SPeter Wemm {
850e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s<null>%s",
851d0cef73dSGregory Neil Shapiro TermEscape.te_rv_on, TermEscape.te_normal);
852c2aa98e2SPeter Wemm return;
853c2aa98e2SPeter Wemm }
854c2aa98e2SPeter Wemm while ((c = (*s++ & 0377)) != '\0')
855c2aa98e2SPeter Wemm {
856c2aa98e2SPeter Wemm if (shiftout)
857c2aa98e2SPeter Wemm {
858e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
859d0cef73dSGregory Neil Shapiro TermEscape.te_normal);
86040266059SGregory Neil Shapiro shiftout = false;
861c2aa98e2SPeter Wemm }
862d0cef73dSGregory Neil Shapiro if (!isascii(c) && !tTd(84, 1))
863c2aa98e2SPeter Wemm {
864c2aa98e2SPeter Wemm if (c == MATCHREPL)
865c2aa98e2SPeter Wemm {
866e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
86740266059SGregory Neil Shapiro "%s$",
86840266059SGregory Neil Shapiro TermEscape.te_rv_on);
86940266059SGregory Neil Shapiro shiftout = true;
870c2aa98e2SPeter Wemm if (*s == '\0')
871c2aa98e2SPeter Wemm continue;
872c2aa98e2SPeter Wemm c = *s++ & 0377;
873c2aa98e2SPeter Wemm goto printchar;
874c2aa98e2SPeter Wemm }
875c2aa98e2SPeter Wemm if (c == MACROEXPAND || c == MACRODEXPAND)
876c2aa98e2SPeter Wemm {
877e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
87840266059SGregory Neil Shapiro "%s$",
87940266059SGregory Neil Shapiro TermEscape.te_rv_on);
880c2aa98e2SPeter Wemm if (c == MACRODEXPAND)
881e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp,
88240266059SGregory Neil Shapiro SM_TIME_DEFAULT, '&');
88340266059SGregory Neil Shapiro shiftout = true;
884c2aa98e2SPeter Wemm if (*s == '\0')
885c2aa98e2SPeter Wemm continue;
886c2aa98e2SPeter Wemm if (strchr("=~&?", *s) != NULL)
887e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp,
88840266059SGregory Neil Shapiro SM_TIME_DEFAULT,
88940266059SGregory Neil Shapiro *s++);
890c2aa98e2SPeter Wemm if (bitset(0200, *s))
891e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp,
89240266059SGregory Neil Shapiro SM_TIME_DEFAULT,
89340266059SGregory Neil Shapiro "{%s}",
89440266059SGregory Neil Shapiro macname(bitidx(*s++)));
895c2aa98e2SPeter Wemm else
896e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp,
89740266059SGregory Neil Shapiro SM_TIME_DEFAULT,
89840266059SGregory Neil Shapiro "%c",
89940266059SGregory Neil Shapiro *s++);
900c2aa98e2SPeter Wemm continue;
901c2aa98e2SPeter Wemm }
902c2aa98e2SPeter Wemm for (mp = MetaMacros; mp->metaname != '\0'; mp++)
903c2aa98e2SPeter Wemm {
90440266059SGregory Neil Shapiro if (bitidx(mp->metaval) == c)
905c2aa98e2SPeter Wemm {
906e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp,
90740266059SGregory Neil Shapiro SM_TIME_DEFAULT,
90840266059SGregory Neil Shapiro "%s$%c",
909c2aa98e2SPeter Wemm TermEscape.te_rv_on,
910c2aa98e2SPeter Wemm mp->metaname);
91140266059SGregory Neil Shapiro shiftout = true;
912c2aa98e2SPeter Wemm break;
913c2aa98e2SPeter Wemm }
914c2aa98e2SPeter Wemm }
915c2aa98e2SPeter Wemm if (c == MATCHCLASS || c == MATCHNCLASS)
916c2aa98e2SPeter Wemm {
917c2aa98e2SPeter Wemm if (bitset(0200, *s))
918e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp,
91940266059SGregory Neil Shapiro SM_TIME_DEFAULT,
92040266059SGregory Neil Shapiro "{%s}",
92140266059SGregory Neil Shapiro macname(bitidx(*s++)));
922c2aa98e2SPeter Wemm else if (*s != '\0')
923e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp,
92440266059SGregory Neil Shapiro SM_TIME_DEFAULT,
92540266059SGregory Neil Shapiro "%c",
92640266059SGregory Neil Shapiro *s++);
927c2aa98e2SPeter Wemm }
928c2aa98e2SPeter Wemm if (mp->metaname != '\0')
929c2aa98e2SPeter Wemm continue;
930c2aa98e2SPeter Wemm
931c2aa98e2SPeter Wemm /* unrecognized meta character */
932e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%sM-",
93340266059SGregory Neil Shapiro TermEscape.te_rv_on);
93440266059SGregory Neil Shapiro shiftout = true;
935c2aa98e2SPeter Wemm c &= 0177;
936c2aa98e2SPeter Wemm }
937c2aa98e2SPeter Wemm printchar:
9389bd497b8SGregory Neil Shapiro if (isascii(c) && isprint(c))
939c2aa98e2SPeter Wemm {
940e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
941c2aa98e2SPeter Wemm continue;
942c2aa98e2SPeter Wemm }
943c2aa98e2SPeter Wemm
944c2aa98e2SPeter Wemm /* wasn't a meta-macro -- find another way to print it */
945c2aa98e2SPeter Wemm switch (c)
946c2aa98e2SPeter Wemm {
947c2aa98e2SPeter Wemm case '\n':
948c2aa98e2SPeter Wemm c = 'n';
949c2aa98e2SPeter Wemm break;
950c2aa98e2SPeter Wemm
951c2aa98e2SPeter Wemm case '\r':
952c2aa98e2SPeter Wemm c = 'r';
953c2aa98e2SPeter Wemm break;
954c2aa98e2SPeter Wemm
955c2aa98e2SPeter Wemm case '\t':
956c2aa98e2SPeter Wemm c = 't';
957c2aa98e2SPeter Wemm break;
958c2aa98e2SPeter Wemm }
959c2aa98e2SPeter Wemm if (!shiftout)
960c2aa98e2SPeter Wemm {
961e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
96240266059SGregory Neil Shapiro TermEscape.te_rv_on);
96340266059SGregory Neil Shapiro shiftout = true;
964c2aa98e2SPeter Wemm }
9659bd497b8SGregory Neil Shapiro if (isascii(c) && isprint(c))
966c2aa98e2SPeter Wemm {
967e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\\');
968e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
969c2aa98e2SPeter Wemm }
970d0cef73dSGregory Neil Shapiro else if (tTd(84, 2))
971d0cef73dSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %o ", c);
972d0cef73dSGregory Neil Shapiro else if (tTd(84, 1))
973d0cef73dSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %#x ", c);
974d0cef73dSGregory Neil Shapiro else if (!isascii(c) && !tTd(84, 1))
975c2aa98e2SPeter Wemm {
976e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '^');
977e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c ^ 0100);
978c2aa98e2SPeter Wemm }
979c2aa98e2SPeter Wemm }
980c2aa98e2SPeter Wemm if (shiftout)
981e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
982d0cef73dSGregory Neil Shapiro TermEscape.te_normal);
983e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(fp, SM_TIME_DEFAULT);
984c2aa98e2SPeter Wemm }
985d0cef73dSGregory Neil Shapiro
98640266059SGregory Neil Shapiro /*
9872fb4f839SGregory Neil Shapiro ** MAKELOWER_A -- Translate a line into lower case
988c2aa98e2SPeter Wemm **
989c2aa98e2SPeter Wemm ** Parameters:
9902fb4f839SGregory Neil Shapiro ** pp -- pointer to the string to translate (modified in place if possible). [A]
9912fb4f839SGregory Neil Shapiro ** rpool -- rpool to use to reallocate string if needed
9922fb4f839SGregory Neil Shapiro ** (if NULL: uses sm_malloc() internally)
993c2aa98e2SPeter Wemm **
994c2aa98e2SPeter Wemm ** Returns:
9952fb4f839SGregory Neil Shapiro ** lower cased string
996c2aa98e2SPeter Wemm **
997c2aa98e2SPeter Wemm ** Side Effects:
9982fb4f839SGregory Neil Shapiro ** String pointed to by pp is translated to lower case if possible.
999c2aa98e2SPeter Wemm */
1000c2aa98e2SPeter Wemm
10012fb4f839SGregory Neil Shapiro char *
makelower_a(pp,rpool)10022fb4f839SGregory Neil Shapiro makelower_a(pp, rpool)
10032fb4f839SGregory Neil Shapiro char **pp;
10042fb4f839SGregory Neil Shapiro SM_RPOOL_T *rpool;
1005c2aa98e2SPeter Wemm {
10062fb4f839SGregory Neil Shapiro char c;
10072fb4f839SGregory Neil Shapiro char *orig, *p;
1008c2aa98e2SPeter Wemm
10092fb4f839SGregory Neil Shapiro SM_REQUIRE(pp != NULL);
10102fb4f839SGregory Neil Shapiro p = *pp;
1011c2aa98e2SPeter Wemm if (p == NULL)
10122fb4f839SGregory Neil Shapiro return p;
10132fb4f839SGregory Neil Shapiro orig = p;
10142fb4f839SGregory Neil Shapiro
10152fb4f839SGregory Neil Shapiro #if USE_EAI
10162fb4f839SGregory Neil Shapiro if (!addr_is_ascii(p))
10172fb4f839SGregory Neil Shapiro {
10182fb4f839SGregory Neil Shapiro char *new;
10192fb4f839SGregory Neil Shapiro
10202fb4f839SGregory Neil Shapiro new = sm_lowercase(p);
10212fb4f839SGregory Neil Shapiro if (new == p)
10222fb4f839SGregory Neil Shapiro return p;
10232fb4f839SGregory Neil Shapiro *pp = sm_rpool_strdup_tagged_x(rpool, new, "makelower", 0, 1);
10242fb4f839SGregory Neil Shapiro return *pp;
10252fb4f839SGregory Neil Shapiro }
10262fb4f839SGregory Neil Shapiro #endif /* USE_EAI */
1027c2aa98e2SPeter Wemm for (; (c = *p) != '\0'; p++)
1028c2aa98e2SPeter Wemm if (isascii(c) && isupper(c))
1029c2aa98e2SPeter Wemm *p = tolower(c);
10302fb4f839SGregory Neil Shapiro return orig;
10312fb4f839SGregory Neil Shapiro }
10322fb4f839SGregory Neil Shapiro
10332fb4f839SGregory Neil Shapiro #if 0
10342fb4f839SGregory Neil Shapiro makelower: Optimization for EAI case?
10352fb4f839SGregory Neil Shapiro
10362fb4f839SGregory Neil Shapiro unsigned char ch;
10372fb4f839SGregory Neil Shapiro
10382fb4f839SGregory Neil Shapiro while ((ch = (unsigned char)*str) != '\0' && ch < 127)
10392fb4f839SGregory Neil Shapiro {
10402fb4f839SGregory Neil Shapiro if (isascii(c) && isupper(c))
10412fb4f839SGregory Neil Shapiro *str = tolower(ch);
10422fb4f839SGregory Neil Shapiro ++str;
10432fb4f839SGregory Neil Shapiro }
10442fb4f839SGregory Neil Shapiro if ('\0' == ch)
10452fb4f839SGregory Neil Shapiro return orig;
10462fb4f839SGregory Neil Shapiro handle UTF-8 case: invoke sm_lowercase() etc
10472fb4f839SGregory Neil Shapiro #endif /* 0 */
10482fb4f839SGregory Neil Shapiro
10492fb4f839SGregory Neil Shapiro /*
10502fb4f839SGregory Neil Shapiro ** MAKELOWER_BUF -- Translate a line into lower case
10512fb4f839SGregory Neil Shapiro **
10522fb4f839SGregory Neil Shapiro ** Parameters:
10532fb4f839SGregory Neil Shapiro ** str -- string to translate. [A]
10542fb4f839SGregory Neil Shapiro ** buf -- where to place lower case version.
10552fb4f839SGregory Neil Shapiro ** buflen -- size of buf
10562fb4f839SGregory Neil Shapiro **
10572fb4f839SGregory Neil Shapiro ** Returns:
10582fb4f839SGregory Neil Shapiro ** nothing
10592fb4f839SGregory Neil Shapiro **
10602fb4f839SGregory Neil Shapiro ** Side Effects:
10612fb4f839SGregory Neil Shapiro ** String pointed to by str is translated to lower case if possible.
10622fb4f839SGregory Neil Shapiro **
10632fb4f839SGregory Neil Shapiro ** Note:
10642fb4f839SGregory Neil Shapiro ** if str is lower cased in place and str == buf (same pointer),
10652fb4f839SGregory Neil Shapiro ** then it is not explicitly copied.
10662fb4f839SGregory Neil Shapiro */
10672fb4f839SGregory Neil Shapiro
10682fb4f839SGregory Neil Shapiro void
makelower_buf(str,buf,buflen)10692fb4f839SGregory Neil Shapiro makelower_buf(str, buf, buflen)
10702fb4f839SGregory Neil Shapiro char *str;
10712fb4f839SGregory Neil Shapiro char *buf;
10722fb4f839SGregory Neil Shapiro int buflen;
10732fb4f839SGregory Neil Shapiro {
10742fb4f839SGregory Neil Shapiro char *lower;
10752fb4f839SGregory Neil Shapiro
10762fb4f839SGregory Neil Shapiro SM_REQUIRE(buf != NULL);
10772fb4f839SGregory Neil Shapiro if (str == NULL)
10782fb4f839SGregory Neil Shapiro return;
10792fb4f839SGregory Neil Shapiro lower = makelower_a(&str, NULL);
10802fb4f839SGregory Neil Shapiro if (lower != str || str != buf)
10812fb4f839SGregory Neil Shapiro {
10822fb4f839SGregory Neil Shapiro sm_strlcpy(buf, lower, buflen);
10832fb4f839SGregory Neil Shapiro if (lower != str)
10842fb4f839SGregory Neil Shapiro SM_FREE(lower);
10852fb4f839SGregory Neil Shapiro }
10862fb4f839SGregory Neil Shapiro return;
1087c2aa98e2SPeter Wemm }
1088d0cef73dSGregory Neil Shapiro
108940266059SGregory Neil Shapiro /*
1090*d39bd2c1SGregory Neil Shapiro ** FIXCRLF -- fix CRLF in line.
1091c2aa98e2SPeter Wemm **
10922fb4f839SGregory Neil Shapiro ** XXX: Could this be a problem for EAI? That is, can there
10932fb4f839SGregory Neil Shapiro ** be a string with \n and the previous octet is \n
10942fb4f839SGregory Neil Shapiro ** but is part of a UTF8 "char"?
10952fb4f839SGregory Neil Shapiro **
1096*d39bd2c1SGregory Neil Shapiro ** Looks for the CRLF combination and turns it into the
1097*d39bd2c1SGregory Neil Shapiro ** UNIX canonical LF character. It only takes one line,
1098*d39bd2c1SGregory Neil Shapiro ** i.e., it is assumed that the first LF found is the end
1099c2aa98e2SPeter Wemm ** of the line.
1100c2aa98e2SPeter Wemm **
1101c2aa98e2SPeter Wemm ** Parameters:
11022fb4f839SGregory Neil Shapiro ** line -- the line to fix. [A]
1103c2aa98e2SPeter Wemm ** stripnl -- if true, strip the newline also.
1104c2aa98e2SPeter Wemm **
1105c2aa98e2SPeter Wemm ** Returns:
1106c2aa98e2SPeter Wemm ** none.
1107c2aa98e2SPeter Wemm **
1108c2aa98e2SPeter Wemm ** Side Effects:
1109c2aa98e2SPeter Wemm ** line is changed in place.
1110c2aa98e2SPeter Wemm */
1111c2aa98e2SPeter Wemm
1112c2aa98e2SPeter Wemm void
fixcrlf(line,stripnl)1113c2aa98e2SPeter Wemm fixcrlf(line, stripnl)
1114c2aa98e2SPeter Wemm char *line;
1115c2aa98e2SPeter Wemm bool stripnl;
1116c2aa98e2SPeter Wemm {
1117c2aa98e2SPeter Wemm register char *p;
1118c2aa98e2SPeter Wemm
1119c2aa98e2SPeter Wemm p = strchr(line, '\n');
1120c2aa98e2SPeter Wemm if (p == NULL)
1121c2aa98e2SPeter Wemm return;
1122c2aa98e2SPeter Wemm if (p > line && p[-1] == '\r')
1123c2aa98e2SPeter Wemm p--;
1124c2aa98e2SPeter Wemm if (!stripnl)
1125c2aa98e2SPeter Wemm *p++ = '\n';
1126c2aa98e2SPeter Wemm *p = '\0';
1127c2aa98e2SPeter Wemm }
1128d0cef73dSGregory Neil Shapiro
112940266059SGregory Neil Shapiro /*
1130c2aa98e2SPeter Wemm ** PUTLINE -- put a line like fputs obeying SMTP conventions
1131c2aa98e2SPeter Wemm **
1132*d39bd2c1SGregory Neil Shapiro ** This routine always guarantees outputting a newline (or CRLF,
1133c2aa98e2SPeter Wemm ** as appropriate) at the end of the string.
1134c2aa98e2SPeter Wemm **
1135c2aa98e2SPeter Wemm ** Parameters:
11362fb4f839SGregory Neil Shapiro ** l -- line to put. (should be [x])
1137c2aa98e2SPeter Wemm ** mci -- the mailer connection information.
1138c2aa98e2SPeter Wemm **
1139c2aa98e2SPeter Wemm ** Returns:
11404e4196cbSGregory Neil Shapiro ** true iff line was written successfully
1141c2aa98e2SPeter Wemm **
1142c2aa98e2SPeter Wemm ** Side Effects:
114340266059SGregory Neil Shapiro ** output of l to mci->mci_out.
1144c2aa98e2SPeter Wemm */
1145c2aa98e2SPeter Wemm
11464e4196cbSGregory Neil Shapiro bool
putline(l,mci)1147c2aa98e2SPeter Wemm putline(l, mci)
1148c2aa98e2SPeter Wemm register char *l;
1149c2aa98e2SPeter Wemm register MCI *mci;
1150c2aa98e2SPeter Wemm {
11514e4196cbSGregory Neil Shapiro return putxline(l, strlen(l), mci, PXLF_MAPFROM);
1152c2aa98e2SPeter Wemm }
1153d0cef73dSGregory Neil Shapiro
115440266059SGregory Neil Shapiro /*
1155c2aa98e2SPeter Wemm ** PUTXLINE -- putline with flags bits.
1156c2aa98e2SPeter Wemm **
1157*d39bd2c1SGregory Neil Shapiro ** This routine always guarantees outputting a newline (or CRLF,
1158c2aa98e2SPeter Wemm ** as appropriate) at the end of the string.
1159c2aa98e2SPeter Wemm **
1160c2aa98e2SPeter Wemm ** Parameters:
11612fb4f839SGregory Neil Shapiro ** l -- line to put. (should be [x])
1162c2aa98e2SPeter Wemm ** len -- the length of the line.
1163c2aa98e2SPeter Wemm ** mci -- the mailer connection information.
1164c2aa98e2SPeter Wemm ** pxflags -- flag bits:
1165c2aa98e2SPeter Wemm ** PXLF_MAPFROM -- map From_ to >From_.
1166c2aa98e2SPeter Wemm ** PXLF_STRIP8BIT -- strip 8th bit.
1167c2aa98e2SPeter Wemm ** PXLF_HEADER -- map bare newline in header to newline space.
1168605302a5SGregory Neil Shapiro ** PXLF_NOADDEOL -- don't add an EOL if one wasn't present.
1169d0cef73dSGregory Neil Shapiro ** PXLF_STRIPMQUOTE -- strip METAQUOTE bytes.
1170c2aa98e2SPeter Wemm **
1171c2aa98e2SPeter Wemm ** Returns:
11724e4196cbSGregory Neil Shapiro ** true iff line was written successfully
1173c2aa98e2SPeter Wemm **
1174c2aa98e2SPeter Wemm ** Side Effects:
117540266059SGregory Neil Shapiro ** output of l to mci->mci_out.
1176c2aa98e2SPeter Wemm */
1177c2aa98e2SPeter Wemm
1178d0cef73dSGregory Neil Shapiro #define PUTX(limit) \
1179d0cef73dSGregory Neil Shapiro do \
1180d0cef73dSGregory Neil Shapiro { \
1181d0cef73dSGregory Neil Shapiro quotenext = false; \
1182d0cef73dSGregory Neil Shapiro while (l < limit) \
1183d0cef73dSGregory Neil Shapiro { \
1184d0cef73dSGregory Neil Shapiro unsigned char c = (unsigned char) *l++; \
1185d0cef73dSGregory Neil Shapiro \
1186d0cef73dSGregory Neil Shapiro if (bitset(PXLF_STRIPMQUOTE, pxflags) && \
1187d0cef73dSGregory Neil Shapiro !quotenext && c == METAQUOTE) \
1188d0cef73dSGregory Neil Shapiro { \
1189d0cef73dSGregory Neil Shapiro quotenext = true; \
1190d0cef73dSGregory Neil Shapiro continue; \
1191d0cef73dSGregory Neil Shapiro } \
1192d0cef73dSGregory Neil Shapiro quotenext = false; \
1193d0cef73dSGregory Neil Shapiro if (strip8bit) \
1194d0cef73dSGregory Neil Shapiro c &= 0177; \
1195d0cef73dSGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, \
1196d0cef73dSGregory Neil Shapiro c) == SM_IO_EOF) \
1197d0cef73dSGregory Neil Shapiro { \
1198d0cef73dSGregory Neil Shapiro dead = true; \
1199d0cef73dSGregory Neil Shapiro break; \
1200d0cef73dSGregory Neil Shapiro } \
1201d0cef73dSGregory Neil Shapiro if (TrafficLogFile != NULL) \
1202d0cef73dSGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, \
1203d0cef73dSGregory Neil Shapiro SM_TIME_DEFAULT, \
1204d0cef73dSGregory Neil Shapiro c); \
1205d0cef73dSGregory Neil Shapiro } \
1206d0cef73dSGregory Neil Shapiro } while (0)
1207d0cef73dSGregory Neil Shapiro
12084e4196cbSGregory Neil Shapiro bool
putxline(l,len,mci,pxflags)1209c2aa98e2SPeter Wemm putxline(l, len, mci, pxflags)
1210c2aa98e2SPeter Wemm register char *l;
1211c2aa98e2SPeter Wemm size_t len;
1212c2aa98e2SPeter Wemm register MCI *mci;
1213c2aa98e2SPeter Wemm int pxflags;
1214c2aa98e2SPeter Wemm {
1215c2aa98e2SPeter Wemm register char *p, *end;
1216d0cef73dSGregory Neil Shapiro int slop;
1217d0cef73dSGregory Neil Shapiro bool dead, quotenext, strip8bit;
1218c2aa98e2SPeter Wemm
1219c2aa98e2SPeter Wemm /* strip out 0200 bits -- these can look like TELNET protocol */
1220d0cef73dSGregory Neil Shapiro strip8bit = bitset(MCIF_7BIT, mci->mci_flags) ||
1221d0cef73dSGregory Neil Shapiro bitset(PXLF_STRIP8BIT, pxflags);
1222d0cef73dSGregory Neil Shapiro dead = false;
1223d0cef73dSGregory Neil Shapiro slop = 0;
1224c2aa98e2SPeter Wemm
1225c2aa98e2SPeter Wemm end = l + len;
1226c2aa98e2SPeter Wemm do
1227c2aa98e2SPeter Wemm {
1228605302a5SGregory Neil Shapiro bool noeol = false;
1229605302a5SGregory Neil Shapiro
1230c2aa98e2SPeter Wemm /* find the end of the line */
1231c2aa98e2SPeter Wemm p = memchr(l, '\n', end - l);
1232c2aa98e2SPeter Wemm if (p == NULL)
1233605302a5SGregory Neil Shapiro {
1234c2aa98e2SPeter Wemm p = end;
1235605302a5SGregory Neil Shapiro noeol = true;
1236605302a5SGregory Neil Shapiro }
1237c2aa98e2SPeter Wemm
1238c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
123940266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
124040266059SGregory Neil Shapiro "%05d >>> ", (int) CurrentPid);
1241c2aa98e2SPeter Wemm
1242c2aa98e2SPeter Wemm /* check for line overflow */
1243c2aa98e2SPeter Wemm while (mci->mci_mailer->m_linelimit > 0 &&
1244c2aa98e2SPeter Wemm (p - l + slop) > mci->mci_mailer->m_linelimit)
1245c2aa98e2SPeter Wemm {
1246c2aa98e2SPeter Wemm register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
1247c2aa98e2SPeter Wemm
1248c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 &&
1249c2aa98e2SPeter Wemm bitnset(M_XDOT, mci->mci_mailer->m_flags))
1250c2aa98e2SPeter Wemm {
125140266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
125240266059SGregory Neil Shapiro '.') == SM_IO_EOF)
125340266059SGregory Neil Shapiro dead = true;
1254c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
125540266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile,
125640266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.');
1257c2aa98e2SPeter Wemm }
1258c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 &&
1259c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) &&
1260c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 &&
1261c2aa98e2SPeter Wemm bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
1262c2aa98e2SPeter Wemm {
126340266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
126440266059SGregory Neil Shapiro '>') == SM_IO_EOF)
126540266059SGregory Neil Shapiro dead = true;
1266c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
126740266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile,
126840266059SGregory Neil Shapiro SM_TIME_DEFAULT,
126940266059SGregory Neil Shapiro '>');
1270c2aa98e2SPeter Wemm }
127106f25ae9SGregory Neil Shapiro if (dead)
127206f25ae9SGregory Neil Shapiro break;
127306f25ae9SGregory Neil Shapiro
1274d0cef73dSGregory Neil Shapiro PUTX(q);
127506f25ae9SGregory Neil Shapiro if (dead)
127606f25ae9SGregory Neil Shapiro break;
127706f25ae9SGregory Neil Shapiro
1278d0cef73dSGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1279d0cef73dSGregory Neil Shapiro '!') == SM_IO_EOF ||
128040266059SGregory Neil Shapiro sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
1281d0cef73dSGregory Neil Shapiro mci->mci_mailer->m_eol) == SM_IO_EOF ||
1282d0cef73dSGregory Neil Shapiro sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1283d0cef73dSGregory Neil Shapiro ' ') == SM_IO_EOF)
128406f25ae9SGregory Neil Shapiro {
128540266059SGregory Neil Shapiro dead = true;
128606f25ae9SGregory Neil Shapiro break;
128706f25ae9SGregory Neil Shapiro }
1288c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
1289c2aa98e2SPeter Wemm {
129040266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile,
129140266059SGregory Neil Shapiro SM_TIME_DEFAULT,
129240266059SGregory Neil Shapiro "!\n%05d >>> ",
129340266059SGregory Neil Shapiro (int) CurrentPid);
1294c2aa98e2SPeter Wemm }
1295c2aa98e2SPeter Wemm slop = 1;
1296c2aa98e2SPeter Wemm }
1297c2aa98e2SPeter Wemm
129806f25ae9SGregory Neil Shapiro if (dead)
129906f25ae9SGregory Neil Shapiro break;
130006f25ae9SGregory Neil Shapiro
1301c2aa98e2SPeter Wemm /* output last part */
1302c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 &&
1303ffb83623SGregory Neil Shapiro bitnset(M_XDOT, mci->mci_mailer->m_flags) &&
1304ffb83623SGregory Neil Shapiro !bitset(MCIF_INLONGLINE, mci->mci_flags))
1305c2aa98e2SPeter Wemm {
130640266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '.') ==
130740266059SGregory Neil Shapiro SM_IO_EOF)
1308193538b7SGregory Neil Shapiro {
13094e4196cbSGregory Neil Shapiro dead = true;
13104e4196cbSGregory Neil Shapiro break;
1311193538b7SGregory Neil Shapiro }
1312c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
131340266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile,
131440266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.');
1315c2aa98e2SPeter Wemm }
1316c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 &&
1317c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) &&
1318c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 &&
1319ffb83623SGregory Neil Shapiro bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
1320ffb83623SGregory Neil Shapiro !bitset(MCIF_INLONGLINE, mci->mci_flags))
1321c2aa98e2SPeter Wemm {
132240266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '>') ==
132340266059SGregory Neil Shapiro SM_IO_EOF)
1324193538b7SGregory Neil Shapiro {
13254e4196cbSGregory Neil Shapiro dead = true;
13264e4196cbSGregory Neil Shapiro break;
1327193538b7SGregory Neil Shapiro }
1328c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
132940266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile,
133040266059SGregory Neil Shapiro SM_TIME_DEFAULT, '>');
1331c2aa98e2SPeter Wemm }
1332d0cef73dSGregory Neil Shapiro PUTX(p);
133306f25ae9SGregory Neil Shapiro if (dead)
133406f25ae9SGregory Neil Shapiro break;
133506f25ae9SGregory Neil Shapiro
1336c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
133740266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, SM_TIME_DEFAULT,
133840266059SGregory Neil Shapiro '\n');
1339ffb83623SGregory Neil Shapiro if ((!bitset(PXLF_NOADDEOL, pxflags) || !noeol))
1340ffb83623SGregory Neil Shapiro {
1341ffb83623SGregory Neil Shapiro mci->mci_flags &= ~MCIF_INLONGLINE;
1342ffb83623SGregory Neil Shapiro if (sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
134340266059SGregory Neil Shapiro mci->mci_mailer->m_eol) == SM_IO_EOF)
1344193538b7SGregory Neil Shapiro {
13454e4196cbSGregory Neil Shapiro dead = true;
13464e4196cbSGregory Neil Shapiro break;
1347193538b7SGregory Neil Shapiro }
1348ffb83623SGregory Neil Shapiro }
1349ffb83623SGregory Neil Shapiro else
1350ffb83623SGregory Neil Shapiro mci->mci_flags |= MCIF_INLONGLINE;
1351ffb83623SGregory Neil Shapiro
1352c2aa98e2SPeter Wemm if (l < end && *l == '\n')
1353c2aa98e2SPeter Wemm {
1354c2aa98e2SPeter Wemm if (*++l != ' ' && *l != '\t' && *l != '\0' &&
1355c2aa98e2SPeter Wemm bitset(PXLF_HEADER, pxflags))
1356c2aa98e2SPeter Wemm {
135740266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
135840266059SGregory Neil Shapiro ' ') == SM_IO_EOF)
1359193538b7SGregory Neil Shapiro {
13604e4196cbSGregory Neil Shapiro dead = true;
13614e4196cbSGregory Neil Shapiro break;
1362193538b7SGregory Neil Shapiro }
136340266059SGregory Neil Shapiro
1364c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
136540266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile,
136640266059SGregory Neil Shapiro SM_TIME_DEFAULT, ' ');
1367c2aa98e2SPeter Wemm }
1368c2aa98e2SPeter Wemm }
136940266059SGregory Neil Shapiro
1370c2aa98e2SPeter Wemm } while (l < end);
13714e4196cbSGregory Neil Shapiro return !dead;
1372c2aa98e2SPeter Wemm }
13734e4196cbSGregory Neil Shapiro
137440266059SGregory Neil Shapiro /*
1375c2aa98e2SPeter Wemm ** XUNLINK -- unlink a file, doing logging as appropriate.
1376c2aa98e2SPeter Wemm **
1377c2aa98e2SPeter Wemm ** Parameters:
1378c2aa98e2SPeter Wemm ** f -- name of file to unlink.
1379c2aa98e2SPeter Wemm **
1380c2aa98e2SPeter Wemm ** Returns:
138140266059SGregory Neil Shapiro ** return value of unlink()
1382c2aa98e2SPeter Wemm **
1383c2aa98e2SPeter Wemm ** Side Effects:
1384c2aa98e2SPeter Wemm ** f is unlinked.
1385c2aa98e2SPeter Wemm */
1386c2aa98e2SPeter Wemm
138740266059SGregory Neil Shapiro int
xunlink(f)1388c2aa98e2SPeter Wemm xunlink(f)
1389c2aa98e2SPeter Wemm char *f;
1390c2aa98e2SPeter Wemm {
1391c2aa98e2SPeter Wemm register int i;
139240266059SGregory Neil Shapiro int save_errno;
1393c2aa98e2SPeter Wemm
1394c2aa98e2SPeter Wemm if (LogLevel > 98)
139540266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "unlink %s", f);
1396c2aa98e2SPeter Wemm
1397c2aa98e2SPeter Wemm i = unlink(f);
139840266059SGregory Neil Shapiro save_errno = errno;
1399c2aa98e2SPeter Wemm if (i < 0 && LogLevel > 97)
140040266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s: unlink-fail %d",
1401c2aa98e2SPeter Wemm f, errno);
140240266059SGregory Neil Shapiro if (i >= 0)
140340266059SGregory Neil Shapiro SYNC_DIR(f, false);
140440266059SGregory Neil Shapiro errno = save_errno;
140540266059SGregory Neil Shapiro return i;
1406c2aa98e2SPeter Wemm }
1407d0cef73dSGregory Neil Shapiro
140840266059SGregory Neil Shapiro /*
1409c2aa98e2SPeter Wemm ** SFGETS -- "safe" fgets -- times out and ignores random interrupts.
1410c2aa98e2SPeter Wemm **
1411c2aa98e2SPeter Wemm ** Parameters:
1412c2aa98e2SPeter Wemm ** buf -- place to put the input line.
14132fb4f839SGregory Neil Shapiro ** (can be [A], but should be [x])
1414c2aa98e2SPeter Wemm ** siz -- size of buf.
1415c2aa98e2SPeter Wemm ** fp -- file to read from.
1416c2aa98e2SPeter Wemm ** timeout -- the timeout before error occurs.
1417c2aa98e2SPeter Wemm ** during -- what we are trying to read (for error messages).
1418c2aa98e2SPeter Wemm **
1419c2aa98e2SPeter Wemm ** Returns:
142040266059SGregory Neil Shapiro ** NULL on error (including timeout). This may also leave
1421c2aa98e2SPeter Wemm ** buf containing a null string.
1422c2aa98e2SPeter Wemm ** buf otherwise.
1423c2aa98e2SPeter Wemm */
1424c2aa98e2SPeter Wemm
1425c2aa98e2SPeter Wemm char *
sfgets(buf,siz,fp,timeout,during)1426c2aa98e2SPeter Wemm sfgets(buf, siz, fp, timeout, during)
1427c2aa98e2SPeter Wemm char *buf;
1428c2aa98e2SPeter Wemm int siz;
142940266059SGregory Neil Shapiro SM_FILE_T *fp;
1430c2aa98e2SPeter Wemm time_t timeout;
1431c2aa98e2SPeter Wemm char *during;
1432c2aa98e2SPeter Wemm {
1433c2aa98e2SPeter Wemm register char *p;
1434552d4955SGregory Neil Shapiro int save_errno, io_timeout, l;
143540266059SGregory Neil Shapiro
143640266059SGregory Neil Shapiro SM_REQUIRE(siz > 0);
143740266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL);
1438c2aa98e2SPeter Wemm
1439c2aa98e2SPeter Wemm if (fp == NULL)
1440c2aa98e2SPeter Wemm {
1441c2aa98e2SPeter Wemm buf[0] = '\0';
144240266059SGregory Neil Shapiro errno = EBADF;
1443c2aa98e2SPeter Wemm return NULL;
1444c2aa98e2SPeter Wemm }
1445c2aa98e2SPeter Wemm
144640266059SGregory Neil Shapiro /* try to read */
1447552d4955SGregory Neil Shapiro l = -1;
144840266059SGregory Neil Shapiro errno = 0;
144940266059SGregory Neil Shapiro
145040266059SGregory Neil Shapiro /* convert the timeout to sm_io notation */
145140266059SGregory Neil Shapiro io_timeout = (timeout <= 0) ? SM_TIME_DEFAULT : timeout * 1000;
145240266059SGregory Neil Shapiro while (!sm_io_eof(fp) && !sm_io_error(fp))
1453c2aa98e2SPeter Wemm {
145440266059SGregory Neil Shapiro errno = 0;
1455552d4955SGregory Neil Shapiro l = sm_io_fgets(fp, io_timeout, buf, siz);
1456552d4955SGregory Neil Shapiro if (l < 0 && errno == EAGAIN)
1457c2aa98e2SPeter Wemm {
145840266059SGregory Neil Shapiro /* The sm_io_fgets() call timedout */
1459c2aa98e2SPeter Wemm if (LogLevel > 1)
1460c2aa98e2SPeter Wemm sm_syslog(LOG_NOTICE, CurEnv->e_id,
1461c2aa98e2SPeter Wemm "timeout waiting for input from %.100s during %s",
146240266059SGregory Neil Shapiro CURHOSTNAME,
1463c2aa98e2SPeter Wemm during);
1464c2aa98e2SPeter Wemm buf[0] = '\0';
1465c2aa98e2SPeter Wemm checkfd012(during);
1466c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
146740266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile,
146840266059SGregory Neil Shapiro SM_TIME_DEFAULT,
146940266059SGregory Neil Shapiro "%05d <<< [TIMEOUT]\n",
147040266059SGregory Neil Shapiro (int) CurrentPid);
147140266059SGregory Neil Shapiro errno = ETIMEDOUT;
147206f25ae9SGregory Neil Shapiro return NULL;
1473c2aa98e2SPeter Wemm }
1474552d4955SGregory Neil Shapiro if (l >= 0 || errno != EINTR)
1475c2aa98e2SPeter Wemm break;
147640266059SGregory Neil Shapiro (void) sm_io_clearerr(fp);
1477c2aa98e2SPeter Wemm }
14782e43090eSPeter Wemm save_errno = errno;
1479c2aa98e2SPeter Wemm
1480c2aa98e2SPeter Wemm /* clean up the books and exit */
1481c2aa98e2SPeter Wemm LineNumber++;
1482552d4955SGregory Neil Shapiro if (l < 0)
1483c2aa98e2SPeter Wemm {
1484c2aa98e2SPeter Wemm buf[0] = '\0';
1485c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
148640266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
148740266059SGregory Neil Shapiro "%05d <<< [EOF]\n",
148840266059SGregory Neil Shapiro (int) CurrentPid);
14892e43090eSPeter Wemm errno = save_errno;
149006f25ae9SGregory Neil Shapiro return NULL;
1491c2aa98e2SPeter Wemm }
1492c2aa98e2SPeter Wemm if (TrafficLogFile != NULL)
149340266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
149440266059SGregory Neil Shapiro "%05d <<< %s", (int) CurrentPid, buf);
1495c2aa98e2SPeter Wemm if (SevenBitInput)
1496c2aa98e2SPeter Wemm {
1497c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++)
1498c2aa98e2SPeter Wemm *p &= ~0200;
1499c2aa98e2SPeter Wemm }
1500c2aa98e2SPeter Wemm else if (!HasEightBits)
1501c2aa98e2SPeter Wemm {
1502c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++)
1503c2aa98e2SPeter Wemm {
1504c2aa98e2SPeter Wemm if (bitset(0200, *p))
1505c2aa98e2SPeter Wemm {
150640266059SGregory Neil Shapiro HasEightBits = true;
1507c2aa98e2SPeter Wemm break;
1508c2aa98e2SPeter Wemm }
1509c2aa98e2SPeter Wemm }
1510c2aa98e2SPeter Wemm }
151106f25ae9SGregory Neil Shapiro return buf;
1512c2aa98e2SPeter Wemm }
1513d0cef73dSGregory Neil Shapiro
15148774250cSGregory Neil Shapiro /*
151540266059SGregory Neil Shapiro ** FGETFOLDED -- like fgets, but knows about folded lines.
1516c2aa98e2SPeter Wemm **
1517c2aa98e2SPeter Wemm ** Parameters:
1518c2aa98e2SPeter Wemm ** buf -- place to put result.
15192fb4f839SGregory Neil Shapiro ** (can be [A], but should be [x])
1520d0cef73dSGregory Neil Shapiro ** np -- pointer to bytes available; will be updated with
1521d0cef73dSGregory Neil Shapiro ** the actual buffer size (not number of bytes filled)
1522d0cef73dSGregory Neil Shapiro ** on return.
1523c2aa98e2SPeter Wemm ** f -- file to read from.
1524c2aa98e2SPeter Wemm **
1525c2aa98e2SPeter Wemm ** Returns:
152640266059SGregory Neil Shapiro ** input line(s) on success, NULL on error or SM_IO_EOF.
1527c2aa98e2SPeter Wemm ** This will normally be buf -- unless the line is too
152840266059SGregory Neil Shapiro ** long, when it will be sm_malloc_x()ed.
1529c2aa98e2SPeter Wemm **
1530c2aa98e2SPeter Wemm ** Side Effects:
1531c2aa98e2SPeter Wemm ** buf gets lines from f, with continuation lines (lines
1532c2aa98e2SPeter Wemm ** with leading white space) appended. CRLF's are mapped
1533*d39bd2c1SGregory Neil Shapiro ** into single newlines. Any trailing LF is stripped.
15345b0945b5SGregory Neil Shapiro ** Increases LineNumber for each line.
1535c2aa98e2SPeter Wemm */
1536c2aa98e2SPeter Wemm
1537c2aa98e2SPeter Wemm char *
fgetfolded(buf,np,f)1538d0cef73dSGregory Neil Shapiro fgetfolded(buf, np, f)
1539c2aa98e2SPeter Wemm char *buf;
1540d0cef73dSGregory Neil Shapiro int *np;
154140266059SGregory Neil Shapiro SM_FILE_T *f;
1542c2aa98e2SPeter Wemm {
1543c2aa98e2SPeter Wemm register char *p = buf;
1544c2aa98e2SPeter Wemm char *bp = buf;
1545c2aa98e2SPeter Wemm register int i;
1546d0cef73dSGregory Neil Shapiro int n;
1547c2aa98e2SPeter Wemm
1548d0cef73dSGregory Neil Shapiro SM_REQUIRE(np != NULL);
1549d0cef73dSGregory Neil Shapiro n = *np;
155040266059SGregory Neil Shapiro SM_REQUIRE(n > 0);
155140266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL);
155240266059SGregory Neil Shapiro if (f == NULL)
155340266059SGregory Neil Shapiro {
155440266059SGregory Neil Shapiro buf[0] = '\0';
155540266059SGregory Neil Shapiro errno = EBADF;
155640266059SGregory Neil Shapiro return NULL;
155740266059SGregory Neil Shapiro }
155840266059SGregory Neil Shapiro
1559c2aa98e2SPeter Wemm n--;
156040266059SGregory Neil Shapiro while ((i = sm_io_getc(f, SM_TIME_DEFAULT)) != SM_IO_EOF)
1561c2aa98e2SPeter Wemm {
1562c2aa98e2SPeter Wemm if (i == '\r')
1563c2aa98e2SPeter Wemm {
156440266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT);
1565c2aa98e2SPeter Wemm if (i != '\n')
1566c2aa98e2SPeter Wemm {
156740266059SGregory Neil Shapiro if (i != SM_IO_EOF)
156840266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT,
156940266059SGregory Neil Shapiro i);
1570c2aa98e2SPeter Wemm i = '\r';
1571c2aa98e2SPeter Wemm }
1572c2aa98e2SPeter Wemm }
1573c2aa98e2SPeter Wemm if (--n <= 0)
1574c2aa98e2SPeter Wemm {
1575c2aa98e2SPeter Wemm /* allocate new space */
1576c2aa98e2SPeter Wemm char *nbp;
1577c2aa98e2SPeter Wemm int nn;
1578c2aa98e2SPeter Wemm
1579c2aa98e2SPeter Wemm nn = (p - bp);
1580c2aa98e2SPeter Wemm if (nn < MEMCHUNKSIZE)
1581c2aa98e2SPeter Wemm nn *= 2;
1582c2aa98e2SPeter Wemm else
1583c2aa98e2SPeter Wemm nn += MEMCHUNKSIZE;
158440266059SGregory Neil Shapiro nbp = sm_malloc_x(nn);
158506f25ae9SGregory Neil Shapiro memmove(nbp, bp, p - bp);
1586c2aa98e2SPeter Wemm p = &nbp[p - bp];
1587c2aa98e2SPeter Wemm if (bp != buf)
15888774250cSGregory Neil Shapiro sm_free(bp);
1589c2aa98e2SPeter Wemm bp = nbp;
1590c2aa98e2SPeter Wemm n = nn - (p - bp);
1591d0cef73dSGregory Neil Shapiro *np = nn;
1592c2aa98e2SPeter Wemm }
1593c2aa98e2SPeter Wemm *p++ = i;
1594c2aa98e2SPeter Wemm if (i == '\n')
1595c2aa98e2SPeter Wemm {
1596c2aa98e2SPeter Wemm LineNumber++;
159740266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT);
159840266059SGregory Neil Shapiro if (i != SM_IO_EOF)
159940266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT, i);
1600c2aa98e2SPeter Wemm if (i != ' ' && i != '\t')
1601c2aa98e2SPeter Wemm break;
1602c2aa98e2SPeter Wemm }
1603c2aa98e2SPeter Wemm }
1604c2aa98e2SPeter Wemm if (p == bp)
160506f25ae9SGregory Neil Shapiro return NULL;
1606c2aa98e2SPeter Wemm if (p[-1] == '\n')
1607c2aa98e2SPeter Wemm p--;
1608c2aa98e2SPeter Wemm *p = '\0';
160906f25ae9SGregory Neil Shapiro return bp;
1610c2aa98e2SPeter Wemm }
1611d0cef73dSGregory Neil Shapiro
161240266059SGregory Neil Shapiro /*
1613c2aa98e2SPeter Wemm ** CURTIME -- return current time.
1614c2aa98e2SPeter Wemm **
1615c2aa98e2SPeter Wemm ** Parameters:
1616c2aa98e2SPeter Wemm ** none.
1617c2aa98e2SPeter Wemm **
1618c2aa98e2SPeter Wemm ** Returns:
1619c2aa98e2SPeter Wemm ** the current time.
1620c2aa98e2SPeter Wemm */
1621c2aa98e2SPeter Wemm
1622c2aa98e2SPeter Wemm time_t
curtime()1623c2aa98e2SPeter Wemm curtime()
1624c2aa98e2SPeter Wemm {
1625c2aa98e2SPeter Wemm auto time_t t;
1626c2aa98e2SPeter Wemm
1627c2aa98e2SPeter Wemm (void) time(&t);
162806f25ae9SGregory Neil Shapiro return t;
1629c2aa98e2SPeter Wemm }
1630d0cef73dSGregory Neil Shapiro
163140266059SGregory Neil Shapiro /*
1632c2aa98e2SPeter Wemm ** ATOBOOL -- convert a string representation to boolean.
1633c2aa98e2SPeter Wemm **
163440266059SGregory Neil Shapiro ** Defaults to false
1635c2aa98e2SPeter Wemm **
1636c2aa98e2SPeter Wemm ** Parameters:
163740266059SGregory Neil Shapiro ** s -- string to convert. Takes "tTyY", empty, and NULL as true,
1638c2aa98e2SPeter Wemm ** others as false.
1639c2aa98e2SPeter Wemm **
1640c2aa98e2SPeter Wemm ** Returns:
1641c2aa98e2SPeter Wemm ** A boolean representation of the string.
1642c2aa98e2SPeter Wemm */
1643c2aa98e2SPeter Wemm
1644c2aa98e2SPeter Wemm bool
atobool(s)1645c2aa98e2SPeter Wemm atobool(s)
1646c2aa98e2SPeter Wemm register char *s;
1647c2aa98e2SPeter Wemm {
16482fb4f839SGregory Neil Shapiro if (SM_IS_EMPTY(s) || strchr("tTyY", *s) != NULL)
164940266059SGregory Neil Shapiro return true;
165040266059SGregory Neil Shapiro return false;
1651c2aa98e2SPeter Wemm }
1652d0cef73dSGregory Neil Shapiro
165340266059SGregory Neil Shapiro /*
1654c2aa98e2SPeter Wemm ** ATOOCT -- convert a string representation to octal.
1655c2aa98e2SPeter Wemm **
1656c2aa98e2SPeter Wemm ** Parameters:
1657c2aa98e2SPeter Wemm ** s -- string to convert.
1658c2aa98e2SPeter Wemm **
1659c2aa98e2SPeter Wemm ** Returns:
1660c2aa98e2SPeter Wemm ** An integer representing the string interpreted as an
1661c2aa98e2SPeter Wemm ** octal number.
1662c2aa98e2SPeter Wemm */
1663c2aa98e2SPeter Wemm
1664c2aa98e2SPeter Wemm int
atooct(s)1665c2aa98e2SPeter Wemm atooct(s)
1666c2aa98e2SPeter Wemm register char *s;
1667c2aa98e2SPeter Wemm {
1668c2aa98e2SPeter Wemm register int i = 0;
1669c2aa98e2SPeter Wemm
1670c2aa98e2SPeter Wemm while (*s >= '0' && *s <= '7')
1671c2aa98e2SPeter Wemm i = (i << 3) | (*s++ - '0');
167206f25ae9SGregory Neil Shapiro return i;
1673c2aa98e2SPeter Wemm }
1674d0cef73dSGregory Neil Shapiro
167540266059SGregory Neil Shapiro /*
1676c2aa98e2SPeter Wemm ** BITINTERSECT -- tell if two bitmaps intersect
1677c2aa98e2SPeter Wemm **
1678c2aa98e2SPeter Wemm ** Parameters:
1679c2aa98e2SPeter Wemm ** a, b -- the bitmaps in question
1680c2aa98e2SPeter Wemm **
1681c2aa98e2SPeter Wemm ** Returns:
168240266059SGregory Neil Shapiro ** true if they have a non-null intersection
168340266059SGregory Neil Shapiro ** false otherwise
1684c2aa98e2SPeter Wemm */
1685c2aa98e2SPeter Wemm
1686c2aa98e2SPeter Wemm bool
bitintersect(a,b)1687c2aa98e2SPeter Wemm bitintersect(a, b)
168806f25ae9SGregory Neil Shapiro BITMAP256 a;
168906f25ae9SGregory Neil Shapiro BITMAP256 b;
1690c2aa98e2SPeter Wemm {
1691c2aa98e2SPeter Wemm int i;
1692c2aa98e2SPeter Wemm
1693c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
1694193538b7SGregory Neil Shapiro {
1695c2aa98e2SPeter Wemm if ((a[i] & b[i]) != 0)
169640266059SGregory Neil Shapiro return true;
1697193538b7SGregory Neil Shapiro }
169840266059SGregory Neil Shapiro return false;
1699c2aa98e2SPeter Wemm }
1700d0cef73dSGregory Neil Shapiro
170140266059SGregory Neil Shapiro /*
1702c2aa98e2SPeter Wemm ** BITZEROP -- tell if a bitmap is all zero
1703c2aa98e2SPeter Wemm **
1704c2aa98e2SPeter Wemm ** Parameters:
1705c2aa98e2SPeter Wemm ** map -- the bit map to check
1706c2aa98e2SPeter Wemm **
1707c2aa98e2SPeter Wemm ** Returns:
170840266059SGregory Neil Shapiro ** true if map is all zero.
170940266059SGregory Neil Shapiro ** false if there are any bits set in map.
1710c2aa98e2SPeter Wemm */
1711c2aa98e2SPeter Wemm
1712c2aa98e2SPeter Wemm bool
bitzerop(map)1713c2aa98e2SPeter Wemm bitzerop(map)
171406f25ae9SGregory Neil Shapiro BITMAP256 map;
1715c2aa98e2SPeter Wemm {
1716c2aa98e2SPeter Wemm int i;
1717c2aa98e2SPeter Wemm
1718c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
1719193538b7SGregory Neil Shapiro {
1720c2aa98e2SPeter Wemm if (map[i] != 0)
172140266059SGregory Neil Shapiro return false;
1722193538b7SGregory Neil Shapiro }
172340266059SGregory Neil Shapiro return true;
1724c2aa98e2SPeter Wemm }
1725d0cef73dSGregory Neil Shapiro
172640266059SGregory Neil Shapiro /*
1727c2aa98e2SPeter Wemm ** STRCONTAINEDIN -- tell if one string is contained in another
1728c2aa98e2SPeter Wemm **
1729c2aa98e2SPeter Wemm ** Parameters:
173040266059SGregory Neil Shapiro ** icase -- ignore case?
17312fb4f839SGregory Neil Shapiro ** a -- possible substring. [A]
17322fb4f839SGregory Neil Shapiro ** b -- possible superstring. [A]
17332fb4f839SGregory Neil Shapiro ** (both must be the same format: X or Q)
1734c2aa98e2SPeter Wemm **
1735c2aa98e2SPeter Wemm ** Returns:
173640266059SGregory Neil Shapiro ** true if a is contained in b (case insensitive).
173740266059SGregory Neil Shapiro ** false otherwise.
1738c2aa98e2SPeter Wemm */
1739c2aa98e2SPeter Wemm
1740c2aa98e2SPeter Wemm bool
strcontainedin(icase,a,b)174140266059SGregory Neil Shapiro strcontainedin(icase, a, b)
174240266059SGregory Neil Shapiro bool icase;
1743c2aa98e2SPeter Wemm register char *a;
1744c2aa98e2SPeter Wemm register char *b;
1745c2aa98e2SPeter Wemm {
1746c2aa98e2SPeter Wemm int la;
1747c2aa98e2SPeter Wemm int lb;
1748c2aa98e2SPeter Wemm int c;
1749c2aa98e2SPeter Wemm
1750c2aa98e2SPeter Wemm la = strlen(a);
1751c2aa98e2SPeter Wemm lb = strlen(b);
1752c2aa98e2SPeter Wemm c = *a;
175340266059SGregory Neil Shapiro if (icase && isascii(c) && isupper(c))
1754c2aa98e2SPeter Wemm c = tolower(c);
1755c2aa98e2SPeter Wemm for (; lb-- >= la; b++)
1756c2aa98e2SPeter Wemm {
175740266059SGregory Neil Shapiro if (icase)
175840266059SGregory Neil Shapiro {
175940266059SGregory Neil Shapiro if (*b != c &&
176040266059SGregory Neil Shapiro isascii(*b) && isupper(*b) && tolower(*b) != c)
1761c2aa98e2SPeter Wemm continue;
176240266059SGregory Neil Shapiro if (sm_strncasecmp(a, b, la) == 0)
176340266059SGregory Neil Shapiro return true;
1764c2aa98e2SPeter Wemm }
176540266059SGregory Neil Shapiro else
176640266059SGregory Neil Shapiro {
176740266059SGregory Neil Shapiro if (*b != c)
176840266059SGregory Neil Shapiro continue;
176940266059SGregory Neil Shapiro if (strncmp(a, b, la) == 0)
177040266059SGregory Neil Shapiro return true;
1771c2aa98e2SPeter Wemm }
177240266059SGregory Neil Shapiro }
177340266059SGregory Neil Shapiro return false;
177440266059SGregory Neil Shapiro }
1775d0cef73dSGregory Neil Shapiro
1776*d39bd2c1SGregory Neil Shapiro #if XDEBUG
177740266059SGregory Neil Shapiro /*
1778c2aa98e2SPeter Wemm ** CHECKFD012 -- check low numbered file descriptors
1779c2aa98e2SPeter Wemm **
1780c2aa98e2SPeter Wemm ** File descriptors 0, 1, and 2 should be open at all times.
1781c2aa98e2SPeter Wemm ** This routine verifies that, and fixes it if not true.
1782c2aa98e2SPeter Wemm **
1783c2aa98e2SPeter Wemm ** Parameters:
1784c2aa98e2SPeter Wemm ** where -- a tag printed if the assertion failed
1785c2aa98e2SPeter Wemm **
1786c2aa98e2SPeter Wemm ** Returns:
1787c2aa98e2SPeter Wemm ** none
1788c2aa98e2SPeter Wemm */
1789c2aa98e2SPeter Wemm
1790c2aa98e2SPeter Wemm void
checkfd012(where)1791c2aa98e2SPeter Wemm checkfd012(where)
1792c2aa98e2SPeter Wemm char *where;
1793c2aa98e2SPeter Wemm {
1794c2aa98e2SPeter Wemm register int i;
1795c2aa98e2SPeter Wemm
1796c2aa98e2SPeter Wemm for (i = 0; i < 3; i++)
1797c2aa98e2SPeter Wemm fill_fd(i, where);
1798c2aa98e2SPeter Wemm }
1799d0cef73dSGregory Neil Shapiro
180040266059SGregory Neil Shapiro /*
1801c2aa98e2SPeter Wemm ** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging
1802c2aa98e2SPeter Wemm **
1803c2aa98e2SPeter Wemm ** Parameters:
1804c2aa98e2SPeter Wemm ** fd -- file descriptor to check.
1805c2aa98e2SPeter Wemm ** where -- tag to print on failure.
1806c2aa98e2SPeter Wemm **
1807c2aa98e2SPeter Wemm ** Returns:
1808c2aa98e2SPeter Wemm ** none.
1809c2aa98e2SPeter Wemm */
1810c2aa98e2SPeter Wemm
1811c2aa98e2SPeter Wemm void
checkfdopen(fd,where)1812c2aa98e2SPeter Wemm checkfdopen(fd, where)
1813c2aa98e2SPeter Wemm int fd;
1814c2aa98e2SPeter Wemm char *where;
1815c2aa98e2SPeter Wemm {
1816c2aa98e2SPeter Wemm struct stat st;
1817c2aa98e2SPeter Wemm
1818c2aa98e2SPeter Wemm if (fstat(fd, &st) < 0 && errno == EBADF)
1819c2aa98e2SPeter Wemm {
1820c2aa98e2SPeter Wemm syserr("checkfdopen(%d): %s not open as expected!", fd, where);
182140266059SGregory Neil Shapiro printopenfds(true);
1822c2aa98e2SPeter Wemm }
1823c2aa98e2SPeter Wemm }
1824*d39bd2c1SGregory Neil Shapiro #endif /* XDEBUG */
1825d0cef73dSGregory Neil Shapiro
182640266059SGregory Neil Shapiro /*
1827c2aa98e2SPeter Wemm ** CHECKFDS -- check for new or missing file descriptors
1828c2aa98e2SPeter Wemm **
1829c2aa98e2SPeter Wemm ** Parameters:
1830c2aa98e2SPeter Wemm ** where -- tag for printing. If null, take a base line.
1831c2aa98e2SPeter Wemm **
1832c2aa98e2SPeter Wemm ** Returns:
1833c2aa98e2SPeter Wemm ** none
1834c2aa98e2SPeter Wemm **
1835c2aa98e2SPeter Wemm ** Side Effects:
1836c2aa98e2SPeter Wemm ** If where is set, shows changes since the last call.
1837c2aa98e2SPeter Wemm */
1838c2aa98e2SPeter Wemm
1839c2aa98e2SPeter Wemm void
checkfds(where)1840c2aa98e2SPeter Wemm checkfds(where)
1841c2aa98e2SPeter Wemm char *where;
1842c2aa98e2SPeter Wemm {
1843c2aa98e2SPeter Wemm int maxfd;
1844c2aa98e2SPeter Wemm register int fd;
184540266059SGregory Neil Shapiro bool printhdr = true;
1846c2aa98e2SPeter Wemm int save_errno = errno;
184706f25ae9SGregory Neil Shapiro static BITMAP256 baseline;
1848c2aa98e2SPeter Wemm extern int DtableSize;
1849c2aa98e2SPeter Wemm
1850193538b7SGregory Neil Shapiro if (DtableSize > BITMAPBITS)
1851193538b7SGregory Neil Shapiro maxfd = BITMAPBITS;
1852c2aa98e2SPeter Wemm else
1853c2aa98e2SPeter Wemm maxfd = DtableSize;
1854c2aa98e2SPeter Wemm if (where == NULL)
1855c2aa98e2SPeter Wemm clrbitmap(baseline);
1856c2aa98e2SPeter Wemm
1857c2aa98e2SPeter Wemm for (fd = 0; fd < maxfd; fd++)
1858c2aa98e2SPeter Wemm {
1859c2aa98e2SPeter Wemm struct stat stbuf;
1860c2aa98e2SPeter Wemm
1861c2aa98e2SPeter Wemm if (fstat(fd, &stbuf) < 0 && errno != EOPNOTSUPP)
1862c2aa98e2SPeter Wemm {
1863c2aa98e2SPeter Wemm if (!bitnset(fd, baseline))
1864c2aa98e2SPeter Wemm continue;
1865c2aa98e2SPeter Wemm clrbitn(fd, baseline);
1866c2aa98e2SPeter Wemm }
1867c2aa98e2SPeter Wemm else if (!bitnset(fd, baseline))
1868c2aa98e2SPeter Wemm setbitn(fd, baseline);
1869c2aa98e2SPeter Wemm else
1870c2aa98e2SPeter Wemm continue;
1871c2aa98e2SPeter Wemm
1872c2aa98e2SPeter Wemm /* file state has changed */
1873c2aa98e2SPeter Wemm if (where == NULL)
1874c2aa98e2SPeter Wemm continue;
1875c2aa98e2SPeter Wemm if (printhdr)
1876c2aa98e2SPeter Wemm {
1877c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id,
1878c2aa98e2SPeter Wemm "%s: changed fds:",
1879c2aa98e2SPeter Wemm where);
188040266059SGregory Neil Shapiro printhdr = false;
1881c2aa98e2SPeter Wemm }
188240266059SGregory Neil Shapiro dumpfd(fd, true, true);
1883c2aa98e2SPeter Wemm }
1884c2aa98e2SPeter Wemm errno = save_errno;
1885c2aa98e2SPeter Wemm }
1886d0cef73dSGregory Neil Shapiro
188740266059SGregory Neil Shapiro /*
1888c2aa98e2SPeter Wemm ** PRINTOPENFDS -- print the open file descriptors (for debugging)
1889c2aa98e2SPeter Wemm **
1890c2aa98e2SPeter Wemm ** Parameters:
1891c2aa98e2SPeter Wemm ** logit -- if set, send output to syslog; otherwise
1892c2aa98e2SPeter Wemm ** print for debugging.
1893c2aa98e2SPeter Wemm **
1894c2aa98e2SPeter Wemm ** Returns:
1895c2aa98e2SPeter Wemm ** none.
1896c2aa98e2SPeter Wemm */
1897c2aa98e2SPeter Wemm
189806f25ae9SGregory Neil Shapiro #if NETINET || NETINET6
1899c2aa98e2SPeter Wemm # include <arpa/inet.h>
19005b0945b5SGregory Neil Shapiro #endif
1901c2aa98e2SPeter Wemm
1902c2aa98e2SPeter Wemm void
printopenfds(logit)1903c2aa98e2SPeter Wemm printopenfds(logit)
1904c2aa98e2SPeter Wemm bool logit;
1905c2aa98e2SPeter Wemm {
1906c2aa98e2SPeter Wemm register int fd;
1907c2aa98e2SPeter Wemm extern int DtableSize;
1908c2aa98e2SPeter Wemm
1909c2aa98e2SPeter Wemm for (fd = 0; fd < DtableSize; fd++)
191040266059SGregory Neil Shapiro dumpfd(fd, false, logit);
1911c2aa98e2SPeter Wemm }
1912d0cef73dSGregory Neil Shapiro
191340266059SGregory Neil Shapiro /*
1914c2aa98e2SPeter Wemm ** DUMPFD -- dump a file descriptor
1915c2aa98e2SPeter Wemm **
1916c2aa98e2SPeter Wemm ** Parameters:
1917c2aa98e2SPeter Wemm ** fd -- the file descriptor to dump.
1918c2aa98e2SPeter Wemm ** printclosed -- if set, print a notification even if
1919c2aa98e2SPeter Wemm ** it is closed; otherwise print nothing.
1920e92d3f3fSGregory Neil Shapiro ** logit -- if set, use sm_syslog instead of sm_dprintf()
192140266059SGregory Neil Shapiro **
192240266059SGregory Neil Shapiro ** Returns:
192340266059SGregory Neil Shapiro ** none.
1924c2aa98e2SPeter Wemm */
1925c2aa98e2SPeter Wemm
1926c2aa98e2SPeter Wemm void
dumpfd(fd,printclosed,logit)1927c2aa98e2SPeter Wemm dumpfd(fd, printclosed, logit)
1928c2aa98e2SPeter Wemm int fd;
1929c2aa98e2SPeter Wemm bool printclosed;
1930c2aa98e2SPeter Wemm bool logit;
1931c2aa98e2SPeter Wemm {
1932c2aa98e2SPeter Wemm register char *p;
1933c2aa98e2SPeter Wemm char *hp;
1934c2aa98e2SPeter Wemm #ifdef S_IFSOCK
1935c2aa98e2SPeter Wemm SOCKADDR sa;
19365b0945b5SGregory Neil Shapiro #endif
1937c2aa98e2SPeter Wemm auto SOCKADDR_LEN_T slen;
1938c2aa98e2SPeter Wemm int i;
1939c2aa98e2SPeter Wemm #if STAT64 > 0
1940c2aa98e2SPeter Wemm struct stat64 st;
19415b0945b5SGregory Neil Shapiro #else
1942c2aa98e2SPeter Wemm struct stat st;
19435b0945b5SGregory Neil Shapiro #endif
1944c2aa98e2SPeter Wemm char buf[200];
1945c2aa98e2SPeter Wemm
1946c2aa98e2SPeter Wemm p = buf;
194740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "%3d: ", fd);
1948c2aa98e2SPeter Wemm p += strlen(p);
1949c2aa98e2SPeter Wemm
1950c2aa98e2SPeter Wemm if (
1951c2aa98e2SPeter Wemm #if STAT64 > 0
1952c2aa98e2SPeter Wemm fstat64(fd, &st)
19535b0945b5SGregory Neil Shapiro #else
1954c2aa98e2SPeter Wemm fstat(fd, &st)
19555b0945b5SGregory Neil Shapiro #endif
1956c2aa98e2SPeter Wemm < 0)
1957c2aa98e2SPeter Wemm {
1958c2aa98e2SPeter Wemm if (errno != EBADF)
1959c2aa98e2SPeter Wemm {
196040266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
196140266059SGregory Neil Shapiro "CANNOT STAT (%s)",
196240266059SGregory Neil Shapiro sm_errstring(errno));
1963c2aa98e2SPeter Wemm goto printit;
1964c2aa98e2SPeter Wemm }
1965c2aa98e2SPeter Wemm else if (printclosed)
1966c2aa98e2SPeter Wemm {
196740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CLOSED");
1968c2aa98e2SPeter Wemm goto printit;
1969c2aa98e2SPeter Wemm }
1970c2aa98e2SPeter Wemm return;
1971c2aa98e2SPeter Wemm }
1972c2aa98e2SPeter Wemm
1973605302a5SGregory Neil Shapiro i = fcntl(fd, F_GETFL, 0);
1974c2aa98e2SPeter Wemm if (i != -1)
1975c2aa98e2SPeter Wemm {
197640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "fl=0x%x, ", i);
1977c2aa98e2SPeter Wemm p += strlen(p);
1978c2aa98e2SPeter Wemm }
1979c2aa98e2SPeter Wemm
198040266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "mode=%o: ",
1981da7d7b9cSGregory Neil Shapiro (unsigned int) st.st_mode);
1982c2aa98e2SPeter Wemm p += strlen(p);
1983c2aa98e2SPeter Wemm switch (st.st_mode & S_IFMT)
1984c2aa98e2SPeter Wemm {
1985c2aa98e2SPeter Wemm #ifdef S_IFSOCK
1986c2aa98e2SPeter Wemm case S_IFSOCK:
198740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "SOCK ");
1988c2aa98e2SPeter Wemm p += strlen(p);
1989d0cef73dSGregory Neil Shapiro memset(&sa, '\0', sizeof(sa));
1990d0cef73dSGregory Neil Shapiro slen = sizeof(sa);
1991c2aa98e2SPeter Wemm if (getsockname(fd, &sa.sa, &slen) < 0)
199240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
199340266059SGregory Neil Shapiro sm_errstring(errno));
1994c2aa98e2SPeter Wemm else
1995c2aa98e2SPeter Wemm {
1996c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa);
199706f25ae9SGregory Neil Shapiro if (hp == NULL)
199806f25ae9SGregory Neil Shapiro {
199906f25ae9SGregory Neil Shapiro /* EMPTY */
200006f25ae9SGregory Neil Shapiro /* do nothing */
200106f25ae9SGregory Neil Shapiro }
200206f25ae9SGregory Neil Shapiro # if NETINET
200306f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET)
200440266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
200540266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port));
20065b0945b5SGregory Neil Shapiro # endif
200706f25ae9SGregory Neil Shapiro # if NETINET6
200806f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6)
200940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
201040266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port));
20115b0945b5SGregory Neil Shapiro # endif
2012c2aa98e2SPeter Wemm else
201340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
201440266059SGregory Neil Shapiro "%s", hp);
2015c2aa98e2SPeter Wemm }
2016c2aa98e2SPeter Wemm p += strlen(p);
201740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "->");
2018c2aa98e2SPeter Wemm p += strlen(p);
2019d0cef73dSGregory Neil Shapiro slen = sizeof(sa);
2020c2aa98e2SPeter Wemm if (getpeername(fd, &sa.sa, &slen) < 0)
202140266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
202240266059SGregory Neil Shapiro sm_errstring(errno));
2023c2aa98e2SPeter Wemm else
2024c2aa98e2SPeter Wemm {
2025c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa);
202606f25ae9SGregory Neil Shapiro if (hp == NULL)
202706f25ae9SGregory Neil Shapiro {
202806f25ae9SGregory Neil Shapiro /* EMPTY */
202906f25ae9SGregory Neil Shapiro /* do nothing */
203006f25ae9SGregory Neil Shapiro }
203106f25ae9SGregory Neil Shapiro # if NETINET
203206f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET)
203340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
203440266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port));
20355b0945b5SGregory Neil Shapiro # endif
203606f25ae9SGregory Neil Shapiro # if NETINET6
203706f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6)
203840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
203940266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port));
20405b0945b5SGregory Neil Shapiro # endif
2041c2aa98e2SPeter Wemm else
204240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
204340266059SGregory Neil Shapiro "%s", hp);
2044c2aa98e2SPeter Wemm }
2045c2aa98e2SPeter Wemm break;
204606f25ae9SGregory Neil Shapiro #endif /* S_IFSOCK */
2047c2aa98e2SPeter Wemm
2048c2aa98e2SPeter Wemm case S_IFCHR:
204940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CHR: ");
2050c2aa98e2SPeter Wemm p += strlen(p);
2051c2aa98e2SPeter Wemm goto defprint;
2052c2aa98e2SPeter Wemm
205340266059SGregory Neil Shapiro #ifdef S_IFBLK
2054c2aa98e2SPeter Wemm case S_IFBLK:
205540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "BLK: ");
2056c2aa98e2SPeter Wemm p += strlen(p);
2057c2aa98e2SPeter Wemm goto defprint;
20585b0945b5SGregory Neil Shapiro #endif
2059c2aa98e2SPeter Wemm
2060c2aa98e2SPeter Wemm #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
2061c2aa98e2SPeter Wemm case S_IFIFO:
206240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "FIFO: ");
2063c2aa98e2SPeter Wemm p += strlen(p);
2064c2aa98e2SPeter Wemm goto defprint;
20655b0945b5SGregory Neil Shapiro #endif
2066c2aa98e2SPeter Wemm
2067c2aa98e2SPeter Wemm #ifdef S_IFDIR
2068c2aa98e2SPeter Wemm case S_IFDIR:
206940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "DIR: ");
2070c2aa98e2SPeter Wemm p += strlen(p);
2071c2aa98e2SPeter Wemm goto defprint;
20725b0945b5SGregory Neil Shapiro #endif
2073c2aa98e2SPeter Wemm
2074c2aa98e2SPeter Wemm #ifdef S_IFLNK
2075c2aa98e2SPeter Wemm case S_IFLNK:
207640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "LNK: ");
2077c2aa98e2SPeter Wemm p += strlen(p);
2078c2aa98e2SPeter Wemm goto defprint;
20795b0945b5SGregory Neil Shapiro #endif
2080c2aa98e2SPeter Wemm
2081c2aa98e2SPeter Wemm default:
2082c2aa98e2SPeter Wemm defprint:
208340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p),
2084da7d7b9cSGregory Neil Shapiro "dev=%ld/%ld, ino=%llu, nlink=%d, u/gid=%ld/%ld, ",
2085da7d7b9cSGregory Neil Shapiro (long) major(st.st_dev), (long) minor(st.st_dev),
208640266059SGregory Neil Shapiro (ULONGLONG_T) st.st_ino,
2087da7d7b9cSGregory Neil Shapiro (int) st.st_nlink, (long) st.st_uid,
2088da7d7b9cSGregory Neil Shapiro (long) st.st_gid);
208940266059SGregory Neil Shapiro p += strlen(p);
209040266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "size=%llu",
209140266059SGregory Neil Shapiro (ULONGLONG_T) st.st_size);
2092c2aa98e2SPeter Wemm break;
2093c2aa98e2SPeter Wemm }
2094c2aa98e2SPeter Wemm
2095c2aa98e2SPeter Wemm printit:
2096c2aa98e2SPeter Wemm if (logit)
2097c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv ? CurEnv->e_id : NULL,
2098c2aa98e2SPeter Wemm "%.800s", buf);
2099c2aa98e2SPeter Wemm else
2100e92d3f3fSGregory Neil Shapiro sm_dprintf("%s\n", buf);
2101c2aa98e2SPeter Wemm }
2102d0cef73dSGregory Neil Shapiro
210340266059SGregory Neil Shapiro /*
2104c2aa98e2SPeter Wemm ** SHORTEN_HOSTNAME -- strip local domain information off of hostname.
2105c2aa98e2SPeter Wemm **
2106c2aa98e2SPeter Wemm ** Parameters:
2107c2aa98e2SPeter Wemm ** host -- the host to shorten (stripped in place).
21082fb4f839SGregory Neil Shapiro ** [EAI: matched against $m: must be same format;
21092fb4f839SGregory Neil Shapiro ** conversion needed?]
2110c2aa98e2SPeter Wemm **
2111c2aa98e2SPeter Wemm ** Returns:
211240266059SGregory Neil Shapiro ** place where string was truncated, NULL if not truncated.
2113c2aa98e2SPeter Wemm */
2114c2aa98e2SPeter Wemm
2115602a2b1bSGregory Neil Shapiro char *
shorten_hostname(host)2116c2aa98e2SPeter Wemm shorten_hostname(host)
2117c2aa98e2SPeter Wemm char host[];
2118c2aa98e2SPeter Wemm {
2119c2aa98e2SPeter Wemm register char *p;
2120c2aa98e2SPeter Wemm char *mydom;
2121c2aa98e2SPeter Wemm int i;
212240266059SGregory Neil Shapiro bool canon = false;
2123c2aa98e2SPeter Wemm
2124c2aa98e2SPeter Wemm /* strip off final dot */
212540266059SGregory Neil Shapiro i = strlen(host);
212640266059SGregory Neil Shapiro p = &host[(i == 0) ? 0 : i - 1];
2127c2aa98e2SPeter Wemm if (*p == '.')
2128c2aa98e2SPeter Wemm {
2129c2aa98e2SPeter Wemm *p = '\0';
213040266059SGregory Neil Shapiro canon = true;
2131c2aa98e2SPeter Wemm }
2132c2aa98e2SPeter Wemm
2133c2aa98e2SPeter Wemm /* see if there is any domain at all -- if not, we are done */
2134c2aa98e2SPeter Wemm p = strchr(host, '.');
2135c2aa98e2SPeter Wemm if (p == NULL)
2136602a2b1bSGregory Neil Shapiro return NULL;
2137c2aa98e2SPeter Wemm
2138c2aa98e2SPeter Wemm /* yes, we have a domain -- see if it looks like us */
2139c2aa98e2SPeter Wemm mydom = macvalue('m', CurEnv);
2140c2aa98e2SPeter Wemm if (mydom == NULL)
2141c2aa98e2SPeter Wemm mydom = "";
2142c2aa98e2SPeter Wemm i = strlen(++p);
214340266059SGregory Neil Shapiro if ((canon ? sm_strcasecmp(p, mydom)
214440266059SGregory Neil Shapiro : sm_strncasecmp(p, mydom, i)) == 0 &&
2145c2aa98e2SPeter Wemm (mydom[i] == '.' || mydom[i] == '\0'))
2146602a2b1bSGregory Neil Shapiro {
2147c2aa98e2SPeter Wemm *--p = '\0';
2148602a2b1bSGregory Neil Shapiro return p;
2149602a2b1bSGregory Neil Shapiro }
2150602a2b1bSGregory Neil Shapiro return NULL;
2151c2aa98e2SPeter Wemm }
2152d0cef73dSGregory Neil Shapiro
215340266059SGregory Neil Shapiro /*
2154c2aa98e2SPeter Wemm ** PROG_OPEN -- open a program for reading
2155c2aa98e2SPeter Wemm **
2156c2aa98e2SPeter Wemm ** Parameters:
2157c2aa98e2SPeter Wemm ** argv -- the argument list.
2158c2aa98e2SPeter Wemm ** pfd -- pointer to a place to store the file descriptor.
2159c2aa98e2SPeter Wemm ** e -- the current envelope.
2160c2aa98e2SPeter Wemm **
2161c2aa98e2SPeter Wemm ** Returns:
2162c2aa98e2SPeter Wemm ** pid of the process -- -1 if it failed.
2163c2aa98e2SPeter Wemm */
2164c2aa98e2SPeter Wemm
21658774250cSGregory Neil Shapiro pid_t
prog_open(argv,pfd,e)2166c2aa98e2SPeter Wemm prog_open(argv, pfd, e)
2167c2aa98e2SPeter Wemm char **argv;
2168c2aa98e2SPeter Wemm int *pfd;
2169c2aa98e2SPeter Wemm ENVELOPE *e;
2170c2aa98e2SPeter Wemm {
21718774250cSGregory Neil Shapiro pid_t pid;
217206f25ae9SGregory Neil Shapiro int save_errno;
217340266059SGregory Neil Shapiro int sff;
217440266059SGregory Neil Shapiro int ret;
2175c2aa98e2SPeter Wemm int fdv[2];
2176c2aa98e2SPeter Wemm char *p, *q;
217794c01205SGregory Neil Shapiro char buf[MAXPATHLEN];
2178c2aa98e2SPeter Wemm extern int DtableSize;
2179c2aa98e2SPeter Wemm
2180c2aa98e2SPeter Wemm if (pipe(fdv) < 0)
2181c2aa98e2SPeter Wemm {
2182c2aa98e2SPeter Wemm syserr("%s: cannot create pipe for stdout", argv[0]);
2183c2aa98e2SPeter Wemm return -1;
2184c2aa98e2SPeter Wemm }
2185c2aa98e2SPeter Wemm pid = fork();
2186c2aa98e2SPeter Wemm if (pid < 0)
2187c2aa98e2SPeter Wemm {
2188c2aa98e2SPeter Wemm syserr("%s: cannot fork", argv[0]);
218906f25ae9SGregory Neil Shapiro (void) close(fdv[0]);
219006f25ae9SGregory Neil Shapiro (void) close(fdv[1]);
2191c2aa98e2SPeter Wemm return -1;
2192c2aa98e2SPeter Wemm }
2193c2aa98e2SPeter Wemm if (pid > 0)
2194c2aa98e2SPeter Wemm {
2195c2aa98e2SPeter Wemm /* parent */
219606f25ae9SGregory Neil Shapiro (void) close(fdv[1]);
2197c2aa98e2SPeter Wemm *pfd = fdv[0];
2198c2aa98e2SPeter Wemm return pid;
2199c2aa98e2SPeter Wemm }
2200c2aa98e2SPeter Wemm
22018774250cSGregory Neil Shapiro /* Reset global flags */
22028774250cSGregory Neil Shapiro RestartRequest = NULL;
220340266059SGregory Neil Shapiro RestartWorkGroup = false;
22048774250cSGregory Neil Shapiro ShutdownRequest = NULL;
22058774250cSGregory Neil Shapiro PendingSignal = 0;
220640266059SGregory Neil Shapiro CurrentPid = getpid();
220740266059SGregory Neil Shapiro
220840266059SGregory Neil Shapiro /*
220940266059SGregory Neil Shapiro ** Initialize exception stack and default exception
221040266059SGregory Neil Shapiro ** handler for child process.
221140266059SGregory Neil Shapiro */
221240266059SGregory Neil Shapiro
221340266059SGregory Neil Shapiro sm_exc_newthread(fatal_error);
221440266059SGregory Neil Shapiro
221540266059SGregory Neil Shapiro /* child -- close stdin */
221640266059SGregory Neil Shapiro (void) close(0);
22178774250cSGregory Neil Shapiro
2218c2aa98e2SPeter Wemm /* stdout goes back to parent */
221906f25ae9SGregory Neil Shapiro (void) close(fdv[0]);
2220c2aa98e2SPeter Wemm if (dup2(fdv[1], 1) < 0)
2221c2aa98e2SPeter Wemm {
2222c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stdout", argv[0]);
2223c2aa98e2SPeter Wemm _exit(EX_OSERR);
2224c2aa98e2SPeter Wemm }
222506f25ae9SGregory Neil Shapiro (void) close(fdv[1]);
2226c2aa98e2SPeter Wemm
2227c2aa98e2SPeter Wemm /* stderr goes to transcript if available */
2228c2aa98e2SPeter Wemm if (e->e_xfp != NULL)
2229c2aa98e2SPeter Wemm {
223006f25ae9SGregory Neil Shapiro int xfd;
223106f25ae9SGregory Neil Shapiro
223240266059SGregory Neil Shapiro xfd = sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL);
223306f25ae9SGregory Neil Shapiro if (xfd >= 0 && dup2(xfd, 2) < 0)
2234c2aa98e2SPeter Wemm {
2235c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stderr", argv[0]);
2236c2aa98e2SPeter Wemm _exit(EX_OSERR);
2237c2aa98e2SPeter Wemm }
2238c2aa98e2SPeter Wemm }
2239c2aa98e2SPeter Wemm
2240c2aa98e2SPeter Wemm /* this process has no right to the queue file */
2241c2aa98e2SPeter Wemm if (e->e_lockfp != NULL)
2242af9557fdSGregory Neil Shapiro {
2243af9557fdSGregory Neil Shapiro int fd;
2244af9557fdSGregory Neil Shapiro
2245af9557fdSGregory Neil Shapiro fd = sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL);
2246af9557fdSGregory Neil Shapiro if (fd >= 0)
2247af9557fdSGregory Neil Shapiro (void) close(fd);
2248af9557fdSGregory Neil Shapiro else
2249af9557fdSGregory Neil Shapiro syserr("%s: lockfp does not have a fd", argv[0]);
2250af9557fdSGregory Neil Shapiro }
225106f25ae9SGregory Neil Shapiro
225206f25ae9SGregory Neil Shapiro /* chroot to the program mailer directory, if defined */
225306f25ae9SGregory Neil Shapiro if (ProgMailer != NULL && ProgMailer->m_rootdir != NULL)
225406f25ae9SGregory Neil Shapiro {
2255d0cef73dSGregory Neil Shapiro expand(ProgMailer->m_rootdir, buf, sizeof(buf), e);
225606f25ae9SGregory Neil Shapiro if (chroot(buf) < 0)
225706f25ae9SGregory Neil Shapiro {
225806f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chroot(%s)", buf);
225906f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL);
226006f25ae9SGregory Neil Shapiro }
226106f25ae9SGregory Neil Shapiro if (chdir("/") < 0)
226206f25ae9SGregory Neil Shapiro {
226306f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chdir(/)");
226406f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL);
226506f25ae9SGregory Neil Shapiro }
226606f25ae9SGregory Neil Shapiro }
2267c2aa98e2SPeter Wemm
2268c2aa98e2SPeter Wemm /* run as default user */
2269c2aa98e2SPeter Wemm endpwent();
227040266059SGregory Neil Shapiro sm_mbdb_terminate();
22714e4196cbSGregory Neil Shapiro #if _FFR_MEMSTAT
22724e4196cbSGregory Neil Shapiro (void) sm_memstat_close();
22735b0945b5SGregory Neil Shapiro #endif
2274c2aa98e2SPeter Wemm if (setgid(DefGid) < 0 && geteuid() == 0)
227506f25ae9SGregory Neil Shapiro {
2276c2aa98e2SPeter Wemm syserr("prog_open: setgid(%ld) failed", (long) DefGid);
227706f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL);
227806f25ae9SGregory Neil Shapiro }
2279c2aa98e2SPeter Wemm if (setuid(DefUid) < 0 && geteuid() == 0)
228006f25ae9SGregory Neil Shapiro {
2281c2aa98e2SPeter Wemm syserr("prog_open: setuid(%ld) failed", (long) DefUid);
228206f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL);
228306f25ae9SGregory Neil Shapiro }
2284c2aa98e2SPeter Wemm
2285c2aa98e2SPeter Wemm /* run in some directory */
2286c2aa98e2SPeter Wemm if (ProgMailer != NULL)
2287c2aa98e2SPeter Wemm p = ProgMailer->m_execdir;
2288c2aa98e2SPeter Wemm else
2289c2aa98e2SPeter Wemm p = NULL;
2290c2aa98e2SPeter Wemm for (; p != NULL; p = q)
2291c2aa98e2SPeter Wemm {
2292c2aa98e2SPeter Wemm q = strchr(p, ':');
2293c2aa98e2SPeter Wemm if (q != NULL)
2294c2aa98e2SPeter Wemm *q = '\0';
2295d0cef73dSGregory Neil Shapiro expand(p, buf, sizeof(buf), e);
2296c2aa98e2SPeter Wemm if (q != NULL)
2297c2aa98e2SPeter Wemm *q++ = ':';
2298c2aa98e2SPeter Wemm if (buf[0] != '\0' && chdir(buf) >= 0)
2299c2aa98e2SPeter Wemm break;
2300c2aa98e2SPeter Wemm }
2301c2aa98e2SPeter Wemm if (p == NULL)
2302c2aa98e2SPeter Wemm {
2303c2aa98e2SPeter Wemm /* backup directories */
2304c2aa98e2SPeter Wemm if (chdir("/tmp") < 0)
2305c2aa98e2SPeter Wemm (void) chdir("/");
2306c2aa98e2SPeter Wemm }
2307c2aa98e2SPeter Wemm
230840266059SGregory Neil Shapiro /* Check safety of program to be run */
230940266059SGregory Neil Shapiro sff = SFF_ROOTOK|SFF_EXECOK;
231040266059SGregory Neil Shapiro if (!bitnset(DBS_RUNWRITABLEPROGRAM, DontBlameSendmail))
231140266059SGregory Neil Shapiro sff |= SFF_NOGWFILES|SFF_NOWWFILES;
231240266059SGregory Neil Shapiro if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH, DontBlameSendmail))
231340266059SGregory Neil Shapiro sff |= SFF_NOPATHCHECK;
231440266059SGregory Neil Shapiro else
231540266059SGregory Neil Shapiro sff |= SFF_SAFEDIRPATH;
231640266059SGregory Neil Shapiro ret = safefile(argv[0], DefUid, DefGid, DefUser, sff, 0, NULL);
231740266059SGregory Neil Shapiro if (ret != 0)
231840266059SGregory Neil Shapiro sm_syslog(LOG_INFO, e->e_id,
231940266059SGregory Neil Shapiro "Warning: prog_open: program %s unsafe: %s",
232040266059SGregory Neil Shapiro argv[0], sm_errstring(ret));
232140266059SGregory Neil Shapiro
2322c2aa98e2SPeter Wemm /* arrange for all the files to be closed */
2323e92d3f3fSGregory Neil Shapiro sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
2324c2aa98e2SPeter Wemm
2325c2aa98e2SPeter Wemm /* now exec the process */
232606f25ae9SGregory Neil Shapiro (void) execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron);
2327c2aa98e2SPeter Wemm
2328c2aa98e2SPeter Wemm /* woops! failed */
232906f25ae9SGregory Neil Shapiro save_errno = errno;
2330c2aa98e2SPeter Wemm syserr("%s: cannot exec", argv[0]);
233106f25ae9SGregory Neil Shapiro if (transienterror(save_errno))
2332c2aa98e2SPeter Wemm _exit(EX_OSERR);
2333c2aa98e2SPeter Wemm _exit(EX_CONFIG);
2334c2aa98e2SPeter Wemm return -1; /* avoid compiler warning on IRIX */
2335c2aa98e2SPeter Wemm }
2336d0cef73dSGregory Neil Shapiro
233740266059SGregory Neil Shapiro /*
2338c2aa98e2SPeter Wemm ** GET_COLUMN -- look up a Column in a line buffer
2339c2aa98e2SPeter Wemm **
2340c2aa98e2SPeter Wemm ** Parameters:
23412fb4f839SGregory Neil Shapiro ** line -- the raw text line to search. [A]
2342c2aa98e2SPeter Wemm ** col -- the column number to fetch.
2343c2aa98e2SPeter Wemm ** delim -- the delimiter between columns. If null,
2344c2aa98e2SPeter Wemm ** use white space.
2345c2aa98e2SPeter Wemm ** buf -- the output buffer.
2346c2aa98e2SPeter Wemm ** buflen -- the length of buf.
2347c2aa98e2SPeter Wemm **
2348c2aa98e2SPeter Wemm ** Returns:
2349c2aa98e2SPeter Wemm ** buf if successful.
2350c2aa98e2SPeter Wemm ** NULL otherwise.
2351c2aa98e2SPeter Wemm */
2352c2aa98e2SPeter Wemm
2353c2aa98e2SPeter Wemm char *
get_column(line,col,delim,buf,buflen)2354c2aa98e2SPeter Wemm get_column(line, col, delim, buf, buflen)
2355c2aa98e2SPeter Wemm char line[];
2356c2aa98e2SPeter Wemm int col;
235706f25ae9SGregory Neil Shapiro int delim;
2358c2aa98e2SPeter Wemm char buf[];
2359c2aa98e2SPeter Wemm int buflen;
2360c2aa98e2SPeter Wemm {
2361c2aa98e2SPeter Wemm char *p;
2362c2aa98e2SPeter Wemm char *begin, *end;
2363c2aa98e2SPeter Wemm int i;
2364c2aa98e2SPeter Wemm char delimbuf[4];
2365c2aa98e2SPeter Wemm
236606f25ae9SGregory Neil Shapiro if ((char) delim == '\0')
2367d0cef73dSGregory Neil Shapiro (void) sm_strlcpy(delimbuf, "\n\t ", sizeof(delimbuf));
2368c2aa98e2SPeter Wemm else
2369c2aa98e2SPeter Wemm {
237006f25ae9SGregory Neil Shapiro delimbuf[0] = (char) delim;
2371c2aa98e2SPeter Wemm delimbuf[1] = '\0';
2372c2aa98e2SPeter Wemm }
2373c2aa98e2SPeter Wemm
2374c2aa98e2SPeter Wemm p = line;
2375c2aa98e2SPeter Wemm if (*p == '\0')
2376c2aa98e2SPeter Wemm return NULL; /* line empty */
237706f25ae9SGregory Neil Shapiro if (*p == (char) delim && col == 0)
2378c2aa98e2SPeter Wemm return NULL; /* first column empty */
2379c2aa98e2SPeter Wemm
2380c2aa98e2SPeter Wemm begin = line;
2381c2aa98e2SPeter Wemm
238206f25ae9SGregory Neil Shapiro if (col == 0 && (char) delim == '\0')
2383c2aa98e2SPeter Wemm {
23845b0945b5SGregory Neil Shapiro while (*begin != '\0' && SM_ISSPACE(*begin))
2385c2aa98e2SPeter Wemm begin++;
2386c2aa98e2SPeter Wemm }
2387c2aa98e2SPeter Wemm
2388c2aa98e2SPeter Wemm for (i = 0; i < col; i++)
2389c2aa98e2SPeter Wemm {
2390c2aa98e2SPeter Wemm if ((begin = strpbrk(begin, delimbuf)) == NULL)
2391c2aa98e2SPeter Wemm return NULL; /* no such column */
2392c2aa98e2SPeter Wemm begin++;
239306f25ae9SGregory Neil Shapiro if ((char) delim == '\0')
2394c2aa98e2SPeter Wemm {
23955b0945b5SGregory Neil Shapiro while (*begin != '\0' && SM_ISSPACE(*begin))
2396c2aa98e2SPeter Wemm begin++;
2397c2aa98e2SPeter Wemm }
2398c2aa98e2SPeter Wemm }
2399c2aa98e2SPeter Wemm
2400c2aa98e2SPeter Wemm end = strpbrk(begin, delimbuf);
2401c2aa98e2SPeter Wemm if (end == NULL)
2402c2aa98e2SPeter Wemm i = strlen(begin);
2403c2aa98e2SPeter Wemm else
2404c2aa98e2SPeter Wemm i = end - begin;
2405c2aa98e2SPeter Wemm if (i >= buflen)
2406c2aa98e2SPeter Wemm i = buflen - 1;
240740266059SGregory Neil Shapiro (void) sm_strlcpy(buf, begin, i + 1);
2408c2aa98e2SPeter Wemm return buf;
2409c2aa98e2SPeter Wemm }
2410d0cef73dSGregory Neil Shapiro
241140266059SGregory Neil Shapiro /*
2412c2aa98e2SPeter Wemm ** CLEANSTRCPY -- copy string keeping out bogus characters
24132fb4f839SGregory Neil Shapiro ** XXX: This may be a problem for EAI?
2414c2aa98e2SPeter Wemm **
2415c2aa98e2SPeter Wemm ** Parameters:
2416c2aa98e2SPeter Wemm ** t -- "to" string.
24172fb4f839SGregory Neil Shapiro ** f -- "from" string. [A]
2418c2aa98e2SPeter Wemm ** l -- length of space available in "to" string.
2419c2aa98e2SPeter Wemm **
2420c2aa98e2SPeter Wemm ** Returns:
2421c2aa98e2SPeter Wemm ** none.
2422c2aa98e2SPeter Wemm */
2423c2aa98e2SPeter Wemm
2424c2aa98e2SPeter Wemm void
cleanstrcpy(t,f,l)2425c2aa98e2SPeter Wemm cleanstrcpy(t, f, l)
2426c2aa98e2SPeter Wemm register char *t;
2427c2aa98e2SPeter Wemm register char *f;
2428c2aa98e2SPeter Wemm int l;
2429c2aa98e2SPeter Wemm {
2430c2aa98e2SPeter Wemm /* check for newlines and log if necessary */
243140266059SGregory Neil Shapiro (void) denlstring(f, true, true);
2432c2aa98e2SPeter Wemm
243306f25ae9SGregory Neil Shapiro if (l <= 0)
243406f25ae9SGregory Neil Shapiro syserr("!cleanstrcpy: length == 0");
243506f25ae9SGregory Neil Shapiro
2436c2aa98e2SPeter Wemm l--;
2437c2aa98e2SPeter Wemm while (l > 0 && *f != '\0')
2438c2aa98e2SPeter Wemm {
2439c2aa98e2SPeter Wemm if (isascii(*f) &&
2440c2aa98e2SPeter Wemm (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
2441c2aa98e2SPeter Wemm {
2442c2aa98e2SPeter Wemm l--;
2443c2aa98e2SPeter Wemm *t++ = *f;
2444c2aa98e2SPeter Wemm }
2445c2aa98e2SPeter Wemm f++;
2446c2aa98e2SPeter Wemm }
2447c2aa98e2SPeter Wemm *t = '\0';
2448c2aa98e2SPeter Wemm }
2449d0cef73dSGregory Neil Shapiro
245040266059SGregory Neil Shapiro /*
2451c2aa98e2SPeter Wemm ** DENLSTRING -- convert newlines in a string to spaces
2452c2aa98e2SPeter Wemm **
2453c2aa98e2SPeter Wemm ** Parameters:
24542fb4f839SGregory Neil Shapiro ** s -- the input string [A]
2455c2aa98e2SPeter Wemm ** strict -- if set, don't permit continuation lines.
2456c2aa98e2SPeter Wemm ** logattacks -- if set, log attempted attacks.
2457c2aa98e2SPeter Wemm **
2458c2aa98e2SPeter Wemm ** Returns:
2459c2aa98e2SPeter Wemm ** A pointer to a version of the string with newlines
2460c2aa98e2SPeter Wemm ** mapped to spaces. This should be copied.
2461c2aa98e2SPeter Wemm */
2462c2aa98e2SPeter Wemm
2463c2aa98e2SPeter Wemm char *
denlstring(s,strict,logattacks)2464c2aa98e2SPeter Wemm denlstring(s, strict, logattacks)
2465c2aa98e2SPeter Wemm char *s;
2466c2aa98e2SPeter Wemm bool strict;
2467c2aa98e2SPeter Wemm bool logattacks;
2468c2aa98e2SPeter Wemm {
2469c2aa98e2SPeter Wemm register char *p;
2470c2aa98e2SPeter Wemm int l;
2471c2aa98e2SPeter Wemm static char *bp = NULL;
2472c2aa98e2SPeter Wemm static int bl = 0;
2473c2aa98e2SPeter Wemm
2474c2aa98e2SPeter Wemm p = s;
2475c2aa98e2SPeter Wemm while ((p = strchr(p, '\n')) != NULL)
2476c2aa98e2SPeter Wemm if (strict || (*++p != ' ' && *p != '\t'))
2477c2aa98e2SPeter Wemm break;
2478c2aa98e2SPeter Wemm if (p == NULL)
2479c2aa98e2SPeter Wemm return s;
2480c2aa98e2SPeter Wemm
2481c2aa98e2SPeter Wemm l = strlen(s) + 1;
2482c2aa98e2SPeter Wemm if (bl < l)
2483c2aa98e2SPeter Wemm {
2484c2aa98e2SPeter Wemm /* allocate more space */
248540266059SGregory Neil Shapiro char *nbp = sm_pmalloc_x(l);
248640266059SGregory Neil Shapiro
2487c2aa98e2SPeter Wemm if (bp != NULL)
24888774250cSGregory Neil Shapiro sm_free(bp);
248940266059SGregory Neil Shapiro bp = nbp;
2490c2aa98e2SPeter Wemm bl = l;
2491c2aa98e2SPeter Wemm }
249240266059SGregory Neil Shapiro (void) sm_strlcpy(bp, s, l);
2493c2aa98e2SPeter Wemm for (p = bp; (p = strchr(p, '\n')) != NULL; )
2494c2aa98e2SPeter Wemm *p++ = ' ';
2495c2aa98e2SPeter Wemm
2496c2aa98e2SPeter Wemm if (logattacks)
2497c2aa98e2SPeter Wemm {
2498d0cef73dSGregory Neil Shapiro sm_syslog(LOG_NOTICE, CurEnv ? CurEnv->e_id : NULL,
2499c2aa98e2SPeter Wemm "POSSIBLE ATTACK from %.100s: newline in string \"%s\"",
2500c2aa98e2SPeter Wemm RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
2501c2aa98e2SPeter Wemm shortenstring(bp, MAXSHORTSTR));
2502c2aa98e2SPeter Wemm }
2503c2aa98e2SPeter Wemm
2504c2aa98e2SPeter Wemm return bp;
2505c2aa98e2SPeter Wemm }
2506739ac4d4SGregory Neil Shapiro
2507739ac4d4SGregory Neil Shapiro /*
2508739ac4d4SGregory Neil Shapiro ** STRREPLNONPRT -- replace "unprintable" characters in a string with subst
2509739ac4d4SGregory Neil Shapiro **
2510739ac4d4SGregory Neil Shapiro ** Parameters:
25112fb4f839SGregory Neil Shapiro ** s -- string to manipulate (in place) [A]
25122fb4f839SGregory Neil Shapiro ** c -- character to use as replacement
2513739ac4d4SGregory Neil Shapiro **
2514739ac4d4SGregory Neil Shapiro ** Returns:
2515739ac4d4SGregory Neil Shapiro ** true iff string did not contain "unprintable" characters
2516739ac4d4SGregory Neil Shapiro */
2517739ac4d4SGregory Neil Shapiro
2518739ac4d4SGregory Neil Shapiro bool
strreplnonprt(s,c)2519739ac4d4SGregory Neil Shapiro strreplnonprt(s, c)
2520739ac4d4SGregory Neil Shapiro char *s;
2521739ac4d4SGregory Neil Shapiro int c;
2522739ac4d4SGregory Neil Shapiro {
2523739ac4d4SGregory Neil Shapiro bool ok;
2524739ac4d4SGregory Neil Shapiro
2525739ac4d4SGregory Neil Shapiro ok = true;
2526739ac4d4SGregory Neil Shapiro if (s == NULL)
2527739ac4d4SGregory Neil Shapiro return ok;
2528739ac4d4SGregory Neil Shapiro while (*s != '\0')
2529739ac4d4SGregory Neil Shapiro {
2530739ac4d4SGregory Neil Shapiro if (!(isascii(*s) && isprint(*s)))
2531739ac4d4SGregory Neil Shapiro {
2532739ac4d4SGregory Neil Shapiro *s = c;
2533739ac4d4SGregory Neil Shapiro ok = false;
2534739ac4d4SGregory Neil Shapiro }
2535739ac4d4SGregory Neil Shapiro ++s;
2536739ac4d4SGregory Neil Shapiro }
2537739ac4d4SGregory Neil Shapiro return ok;
2538739ac4d4SGregory Neil Shapiro }
2539739ac4d4SGregory Neil Shapiro
254040266059SGregory Neil Shapiro /*
2541c2aa98e2SPeter Wemm ** PATH_IS_DIR -- check to see if file exists and is a directory.
2542c2aa98e2SPeter Wemm **
2543c2aa98e2SPeter Wemm ** There are some additional checks for security violations in
2544c2aa98e2SPeter Wemm ** here. This routine is intended to be used for the host status
2545c2aa98e2SPeter Wemm ** support.
2546c2aa98e2SPeter Wemm **
2547c2aa98e2SPeter Wemm ** Parameters:
25482fb4f839SGregory Neil Shapiro ** pathname -- pathname to check for directory-ness. [x]
2549c2aa98e2SPeter Wemm ** createflag -- if set, create directory if needed.
2550c2aa98e2SPeter Wemm **
2551c2aa98e2SPeter Wemm ** Returns:
255240266059SGregory Neil Shapiro ** true -- if the indicated pathname is a directory
255340266059SGregory Neil Shapiro ** false -- otherwise
2554c2aa98e2SPeter Wemm */
2555c2aa98e2SPeter Wemm
2556a7ec597cSGregory Neil Shapiro bool
path_is_dir(pathname,createflag)2557c2aa98e2SPeter Wemm path_is_dir(pathname, createflag)
2558c2aa98e2SPeter Wemm char *pathname;
2559c2aa98e2SPeter Wemm bool createflag;
2560c2aa98e2SPeter Wemm {
2561c2aa98e2SPeter Wemm struct stat statbuf;
2562c2aa98e2SPeter Wemm
2563c2aa98e2SPeter Wemm #if HASLSTAT
2564c2aa98e2SPeter Wemm if (lstat(pathname, &statbuf) < 0)
25655b0945b5SGregory Neil Shapiro #else
2566c2aa98e2SPeter Wemm if (stat(pathname, &statbuf) < 0)
25675b0945b5SGregory Neil Shapiro #endif
2568c2aa98e2SPeter Wemm {
2569c2aa98e2SPeter Wemm if (errno != ENOENT || !createflag)
257040266059SGregory Neil Shapiro return false;
2571c2aa98e2SPeter Wemm if (mkdir(pathname, 0755) < 0)
257240266059SGregory Neil Shapiro return false;
257340266059SGregory Neil Shapiro return true;
2574c2aa98e2SPeter Wemm }
2575c2aa98e2SPeter Wemm if (!S_ISDIR(statbuf.st_mode))
2576c2aa98e2SPeter Wemm {
2577c2aa98e2SPeter Wemm errno = ENOTDIR;
257840266059SGregory Neil Shapiro return false;
2579c2aa98e2SPeter Wemm }
2580c2aa98e2SPeter Wemm
2581c2aa98e2SPeter Wemm /* security: don't allow writable directories */
2582c2aa98e2SPeter Wemm if (bitset(S_IWGRP|S_IWOTH, statbuf.st_mode))
2583c2aa98e2SPeter Wemm {
2584c2aa98e2SPeter Wemm errno = EACCES;
258540266059SGregory Neil Shapiro return false;
2586c2aa98e2SPeter Wemm }
258740266059SGregory Neil Shapiro return true;
2588c2aa98e2SPeter Wemm }
2589d0cef73dSGregory Neil Shapiro
259040266059SGregory Neil Shapiro /*
2591c2aa98e2SPeter Wemm ** PROC_LIST_ADD -- add process id to list of our children
2592c2aa98e2SPeter Wemm **
2593c2aa98e2SPeter Wemm ** Parameters:
2594c2aa98e2SPeter Wemm ** pid -- pid to add to list.
259506f25ae9SGregory Neil Shapiro ** task -- task of pid.
259606f25ae9SGregory Neil Shapiro ** type -- type of process.
259740266059SGregory Neil Shapiro ** count -- number of processes.
259840266059SGregory Neil Shapiro ** other -- other information for this type.
2599c2aa98e2SPeter Wemm **
2600c2aa98e2SPeter Wemm ** Returns:
2601c2aa98e2SPeter Wemm ** none
260240266059SGregory Neil Shapiro **
260340266059SGregory Neil Shapiro ** Side Effects:
260440266059SGregory Neil Shapiro ** May increase CurChildren. May grow ProcList.
2605c2aa98e2SPeter Wemm */
2606c2aa98e2SPeter Wemm
260740266059SGregory Neil Shapiro typedef struct procs PROCS_T;
260840266059SGregory Neil Shapiro
260940266059SGregory Neil Shapiro struct procs
261040266059SGregory Neil Shapiro {
261140266059SGregory Neil Shapiro pid_t proc_pid;
261240266059SGregory Neil Shapiro char *proc_task;
261340266059SGregory Neil Shapiro int proc_type;
261440266059SGregory Neil Shapiro int proc_count;
261540266059SGregory Neil Shapiro int proc_other;
2616e92d3f3fSGregory Neil Shapiro SOCKADDR proc_hostaddr;
261740266059SGregory Neil Shapiro };
261840266059SGregory Neil Shapiro
261940266059SGregory Neil Shapiro static PROCS_T *volatile ProcListVec = NULL;
2620c2aa98e2SPeter Wemm static int ProcListSize = 0;
2621c2aa98e2SPeter Wemm
2622c2aa98e2SPeter Wemm void
proc_list_add(pid,task,type,count,other,hostaddr)2623e92d3f3fSGregory Neil Shapiro proc_list_add(pid, task, type, count, other, hostaddr)
2624c2aa98e2SPeter Wemm pid_t pid;
2625065a643dSPeter Wemm char *task;
262606f25ae9SGregory Neil Shapiro int type;
262740266059SGregory Neil Shapiro int count;
262840266059SGregory Neil Shapiro int other;
2629e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr;
2630c2aa98e2SPeter Wemm {
2631c2aa98e2SPeter Wemm int i;
2632c2aa98e2SPeter Wemm
2633c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++)
2634c2aa98e2SPeter Wemm {
2635065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID)
2636c2aa98e2SPeter Wemm break;
2637c2aa98e2SPeter Wemm }
2638c2aa98e2SPeter Wemm if (i >= ProcListSize)
2639c2aa98e2SPeter Wemm {
2640c2aa98e2SPeter Wemm /* probe the existing vector to avoid growing infinitely */
2641c2aa98e2SPeter Wemm proc_list_probe();
2642c2aa98e2SPeter Wemm
2643c2aa98e2SPeter Wemm /* now scan again */
2644c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++)
2645c2aa98e2SPeter Wemm {
2646065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID)
2647c2aa98e2SPeter Wemm break;
2648c2aa98e2SPeter Wemm }
2649c2aa98e2SPeter Wemm }
2650c2aa98e2SPeter Wemm if (i >= ProcListSize)
2651c2aa98e2SPeter Wemm {
2652c2aa98e2SPeter Wemm /* grow process list */
2653d0cef73dSGregory Neil Shapiro int chldwasblocked;
265440266059SGregory Neil Shapiro PROCS_T *npv;
2655c2aa98e2SPeter Wemm
265640266059SGregory Neil Shapiro SM_ASSERT(ProcListSize < INT_MAX - PROC_LIST_SEG);
2657d0cef73dSGregory Neil Shapiro npv = (PROCS_T *) sm_pmalloc_x((sizeof(*npv)) *
265806f25ae9SGregory Neil Shapiro (ProcListSize + PROC_LIST_SEG));
2659d0cef73dSGregory Neil Shapiro
2660d0cef73dSGregory Neil Shapiro /* Block SIGCHLD so reapchild() doesn't mess with us */
2661d0cef73dSGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD);
2662c2aa98e2SPeter Wemm if (ProcListSize > 0)
2663c2aa98e2SPeter Wemm {
266406f25ae9SGregory Neil Shapiro memmove(npv, ProcListVec,
266540266059SGregory Neil Shapiro ProcListSize * sizeof(PROCS_T));
26668774250cSGregory Neil Shapiro sm_free(ProcListVec);
2667c2aa98e2SPeter Wemm }
266840266059SGregory Neil Shapiro
266940266059SGregory Neil Shapiro /* XXX just use memset() to initialize this part? */
2670c2aa98e2SPeter Wemm for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++)
2671065a643dSPeter Wemm {
2672065a643dSPeter Wemm npv[i].proc_pid = NO_PID;
2673065a643dSPeter Wemm npv[i].proc_task = NULL;
267406f25ae9SGregory Neil Shapiro npv[i].proc_type = PROC_NONE;
2675065a643dSPeter Wemm }
2676c2aa98e2SPeter Wemm i = ProcListSize;
2677c2aa98e2SPeter Wemm ProcListSize += PROC_LIST_SEG;
2678c2aa98e2SPeter Wemm ProcListVec = npv;
2679d0cef73dSGregory Neil Shapiro if (chldwasblocked == 0)
2680d0cef73dSGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD);
2681c2aa98e2SPeter Wemm }
2682065a643dSPeter Wemm ProcListVec[i].proc_pid = pid;
268340266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task);
268406f25ae9SGregory Neil Shapiro ProcListVec[i].proc_type = type;
268540266059SGregory Neil Shapiro ProcListVec[i].proc_count = count;
268640266059SGregory Neil Shapiro ProcListVec[i].proc_other = other;
2687e92d3f3fSGregory Neil Shapiro if (hostaddr != NULL)
2688e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr = *hostaddr;
2689e92d3f3fSGregory Neil Shapiro else
2690e92d3f3fSGregory Neil Shapiro memset(&ProcListVec[i].proc_hostaddr, 0,
2691e92d3f3fSGregory Neil Shapiro sizeof(ProcListVec[i].proc_hostaddr));
2692065a643dSPeter Wemm
2693065a643dSPeter Wemm /* if process adding itself, it's not a child */
269440266059SGregory Neil Shapiro if (pid != CurrentPid)
269540266059SGregory Neil Shapiro {
269640266059SGregory Neil Shapiro SM_ASSERT(CurChildren < INT_MAX);
2697c2aa98e2SPeter Wemm CurChildren++;
2698c2aa98e2SPeter Wemm }
269940266059SGregory Neil Shapiro }
2700d0cef73dSGregory Neil Shapiro
270140266059SGregory Neil Shapiro /*
2702065a643dSPeter Wemm ** PROC_LIST_SET -- set pid task in process list
2703065a643dSPeter Wemm **
2704065a643dSPeter Wemm ** Parameters:
2705065a643dSPeter Wemm ** pid -- pid to set
2706065a643dSPeter Wemm ** task -- task of pid
2707065a643dSPeter Wemm **
2708065a643dSPeter Wemm ** Returns:
2709065a643dSPeter Wemm ** none.
2710065a643dSPeter Wemm */
2711065a643dSPeter Wemm
2712065a643dSPeter Wemm void
proc_list_set(pid,task)2713065a643dSPeter Wemm proc_list_set(pid, task)
2714065a643dSPeter Wemm pid_t pid;
2715065a643dSPeter Wemm char *task;
2716065a643dSPeter Wemm {
2717065a643dSPeter Wemm int i;
2718065a643dSPeter Wemm
2719065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++)
2720065a643dSPeter Wemm {
2721065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid)
2722065a643dSPeter Wemm {
272340266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task);
2724065a643dSPeter Wemm break;
2725065a643dSPeter Wemm }
2726065a643dSPeter Wemm }
2727065a643dSPeter Wemm }
2728d0cef73dSGregory Neil Shapiro
272940266059SGregory Neil Shapiro /*
2730c2aa98e2SPeter Wemm ** PROC_LIST_DROP -- drop pid from process list
2731c2aa98e2SPeter Wemm **
2732c2aa98e2SPeter Wemm ** Parameters:
2733c2aa98e2SPeter Wemm ** pid -- pid to drop
273440266059SGregory Neil Shapiro ** st -- process status
273540266059SGregory Neil Shapiro ** other -- storage for proc_other (return).
2736c2aa98e2SPeter Wemm **
2737c2aa98e2SPeter Wemm ** Returns:
273840266059SGregory Neil Shapiro ** none.
273940266059SGregory Neil Shapiro **
274040266059SGregory Neil Shapiro ** Side Effects:
274140266059SGregory Neil Shapiro ** May decrease CurChildren, CurRunners, or
274240266059SGregory Neil Shapiro ** set RestartRequest or ShutdownRequest.
27438774250cSGregory Neil Shapiro **
27448774250cSGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
27458774250cSGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
27468774250cSGregory Neil Shapiro ** DOING.
2747c2aa98e2SPeter Wemm */
2748c2aa98e2SPeter Wemm
274940266059SGregory Neil Shapiro void
proc_list_drop(pid,st,other)275040266059SGregory Neil Shapiro proc_list_drop(pid, st, other)
2751c2aa98e2SPeter Wemm pid_t pid;
275240266059SGregory Neil Shapiro int st;
275340266059SGregory Neil Shapiro int *other;
2754c2aa98e2SPeter Wemm {
2755c2aa98e2SPeter Wemm int i;
275606f25ae9SGregory Neil Shapiro int type = PROC_NONE;
2757c2aa98e2SPeter Wemm
2758c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++)
2759c2aa98e2SPeter Wemm {
2760065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid)
2761c2aa98e2SPeter Wemm {
2762065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID;
276306f25ae9SGregory Neil Shapiro type = ProcListVec[i].proc_type;
276440266059SGregory Neil Shapiro if (other != NULL)
276540266059SGregory Neil Shapiro *other = ProcListVec[i].proc_other;
27664e4196cbSGregory Neil Shapiro if (CurChildren > 0)
27674e4196cbSGregory Neil Shapiro CurChildren--;
2768c2aa98e2SPeter Wemm break;
2769c2aa98e2SPeter Wemm }
2770c2aa98e2SPeter Wemm }
277106f25ae9SGregory Neil Shapiro
277240266059SGregory Neil Shapiro if (type == PROC_CONTROL && WIFEXITED(st))
277340266059SGregory Neil Shapiro {
277440266059SGregory Neil Shapiro /* if so, see if we need to restart or shutdown */
277540266059SGregory Neil Shapiro if (WEXITSTATUS(st) == EX_RESTART)
277640266059SGregory Neil Shapiro RestartRequest = "control socket";
277740266059SGregory Neil Shapiro else if (WEXITSTATUS(st) == EX_SHUTDOWN)
277840266059SGregory Neil Shapiro ShutdownRequest = "control socket";
2779c2aa98e2SPeter Wemm }
278040266059SGregory Neil Shapiro else if (type == PROC_QUEUE_CHILD && !WIFSTOPPED(st) &&
278140266059SGregory Neil Shapiro ProcListVec[i].proc_other > -1)
278240266059SGregory Neil Shapiro {
278340266059SGregory Neil Shapiro /* restart this persistent runner */
278440266059SGregory Neil Shapiro mark_work_group_restart(ProcListVec[i].proc_other, st);
278540266059SGregory Neil Shapiro }
278640266059SGregory Neil Shapiro else if (type == PROC_QUEUE)
2787ba00ec3dSGregory Neil Shapiro {
278840266059SGregory Neil Shapiro CurRunners -= ProcListVec[i].proc_count;
2789ba00ec3dSGregory Neil Shapiro
2790ba00ec3dSGregory Neil Shapiro /* CHK_CUR_RUNNERS() can't be used here: uses syslog() */
2791ba00ec3dSGregory Neil Shapiro if (CurRunners < 0)
2792ba00ec3dSGregory Neil Shapiro CurRunners = 0;
2793ba00ec3dSGregory Neil Shapiro }
279440266059SGregory Neil Shapiro }
2795d0cef73dSGregory Neil Shapiro
279640266059SGregory Neil Shapiro /*
2797c2aa98e2SPeter Wemm ** PROC_LIST_CLEAR -- clear the process list
2798c2aa98e2SPeter Wemm **
2799c2aa98e2SPeter Wemm ** Parameters:
2800c2aa98e2SPeter Wemm ** none.
2801c2aa98e2SPeter Wemm **
2802c2aa98e2SPeter Wemm ** Returns:
2803c2aa98e2SPeter Wemm ** none.
280440266059SGregory Neil Shapiro **
280540266059SGregory Neil Shapiro ** Side Effects:
280640266059SGregory Neil Shapiro ** Sets CurChildren to zero.
2807c2aa98e2SPeter Wemm */
2808c2aa98e2SPeter Wemm
2809c2aa98e2SPeter Wemm void
proc_list_clear()2810c2aa98e2SPeter Wemm proc_list_clear()
2811c2aa98e2SPeter Wemm {
2812c2aa98e2SPeter Wemm int i;
2813c2aa98e2SPeter Wemm
2814065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */
2815065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++)
2816065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID;
2817c2aa98e2SPeter Wemm CurChildren = 0;
2818c2aa98e2SPeter Wemm }
2819d0cef73dSGregory Neil Shapiro
282040266059SGregory Neil Shapiro /*
2821c2aa98e2SPeter Wemm ** PROC_LIST_PROBE -- probe processes in the list to see if they still exist
2822c2aa98e2SPeter Wemm **
2823c2aa98e2SPeter Wemm ** Parameters:
2824c2aa98e2SPeter Wemm ** none
2825c2aa98e2SPeter Wemm **
2826c2aa98e2SPeter Wemm ** Returns:
2827c2aa98e2SPeter Wemm ** none
282840266059SGregory Neil Shapiro **
282940266059SGregory Neil Shapiro ** Side Effects:
283040266059SGregory Neil Shapiro ** May decrease CurChildren.
2831c2aa98e2SPeter Wemm */
2832c2aa98e2SPeter Wemm
2833c2aa98e2SPeter Wemm void
proc_list_probe()2834c2aa98e2SPeter Wemm proc_list_probe()
2835c2aa98e2SPeter Wemm {
28364e4196cbSGregory Neil Shapiro int i, children;
28374e4196cbSGregory Neil Shapiro int chldwasblocked;
28384e4196cbSGregory Neil Shapiro pid_t pid;
28394e4196cbSGregory Neil Shapiro
28404e4196cbSGregory Neil Shapiro children = 0;
28414e4196cbSGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD);
2842c2aa98e2SPeter Wemm
2843065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */
2844065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++)
2845c2aa98e2SPeter Wemm {
28464e4196cbSGregory Neil Shapiro pid = ProcListVec[i].proc_pid;
28474e4196cbSGregory Neil Shapiro if (pid == NO_PID || pid == CurrentPid)
2848c2aa98e2SPeter Wemm continue;
28494e4196cbSGregory Neil Shapiro if (kill(pid, 0) < 0)
2850c2aa98e2SPeter Wemm {
2851c2aa98e2SPeter Wemm if (LogLevel > 3)
2852c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id,
2853c2aa98e2SPeter Wemm "proc_list_probe: lost pid %d",
2854065a643dSPeter Wemm (int) ProcListVec[i].proc_pid);
2855065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID;
28565b0945b5SGregory Neil Shapiro SM_FREE(ProcListVec[i].proc_task);
2857ba00ec3dSGregory Neil Shapiro
2858ba00ec3dSGregory Neil Shapiro if (ProcListVec[i].proc_type == PROC_QUEUE)
2859ba00ec3dSGregory Neil Shapiro {
2860ba00ec3dSGregory Neil Shapiro CurRunners -= ProcListVec[i].proc_count;
2861ba00ec3dSGregory Neil Shapiro CHK_CUR_RUNNERS("proc_list_probe", i,
2862ba00ec3dSGregory Neil Shapiro ProcListVec[i].proc_count);
2863ba00ec3dSGregory Neil Shapiro }
2864ba00ec3dSGregory Neil Shapiro
2865c2aa98e2SPeter Wemm CurChildren--;
2866c2aa98e2SPeter Wemm }
28674e4196cbSGregory Neil Shapiro else
28684e4196cbSGregory Neil Shapiro {
28694e4196cbSGregory Neil Shapiro ++children;
28704e4196cbSGregory Neil Shapiro }
2871c2aa98e2SPeter Wemm }
2872c2aa98e2SPeter Wemm if (CurChildren < 0)
2873c2aa98e2SPeter Wemm CurChildren = 0;
28744e4196cbSGregory Neil Shapiro if (chldwasblocked == 0)
28754e4196cbSGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD);
2876af9557fdSGregory Neil Shapiro if (LogLevel > 10 && children != CurChildren && CurrentPid == DaemonPid)
28774e4196cbSGregory Neil Shapiro {
28784e4196cbSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID,
28794e4196cbSGregory Neil Shapiro "proc_list_probe: found %d children, expected %d",
28804e4196cbSGregory Neil Shapiro children, CurChildren);
28814e4196cbSGregory Neil Shapiro }
2882c2aa98e2SPeter Wemm }
288340266059SGregory Neil Shapiro
288440266059SGregory Neil Shapiro /*
2885065a643dSPeter Wemm ** PROC_LIST_DISPLAY -- display the process list
2886065a643dSPeter Wemm **
2887065a643dSPeter Wemm ** Parameters:
2888065a643dSPeter Wemm ** out -- output file pointer
288940266059SGregory Neil Shapiro ** prefix -- string to output in front of each line.
2890065a643dSPeter Wemm **
2891065a643dSPeter Wemm ** Returns:
2892065a643dSPeter Wemm ** none.
2893065a643dSPeter Wemm */
2894065a643dSPeter Wemm
2895065a643dSPeter Wemm void
proc_list_display(out,prefix)289640266059SGregory Neil Shapiro proc_list_display(out, prefix)
289740266059SGregory Neil Shapiro SM_FILE_T *out;
289840266059SGregory Neil Shapiro char *prefix;
2899065a643dSPeter Wemm {
2900065a643dSPeter Wemm int i;
2901065a643dSPeter Wemm
2902065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++)
2903065a643dSPeter Wemm {
2904065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID)
2905065a643dSPeter Wemm continue;
2906065a643dSPeter Wemm
290740266059SGregory Neil Shapiro (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "%s%d %s%s\n",
290840266059SGregory Neil Shapiro prefix,
290940266059SGregory Neil Shapiro (int) ProcListVec[i].proc_pid,
2910065a643dSPeter Wemm ProcListVec[i].proc_task != NULL ?
2911065a643dSPeter Wemm ProcListVec[i].proc_task : "(unknown)",
2912065a643dSPeter Wemm (OpMode == MD_SMTP ||
2913065a643dSPeter Wemm OpMode == MD_DAEMON ||
2914065a643dSPeter Wemm OpMode == MD_ARPAFTP) ? "\r" : "");
2915065a643dSPeter Wemm }
2916065a643dSPeter Wemm }
291740266059SGregory Neil Shapiro
291840266059SGregory Neil Shapiro /*
291940266059SGregory Neil Shapiro ** PROC_LIST_SIGNAL -- send a signal to a type of process in the list
292013058a91SGregory Neil Shapiro **
292113058a91SGregory Neil Shapiro ** Parameters:
292240266059SGregory Neil Shapiro ** type -- type of process to signal
292340266059SGregory Neil Shapiro ** signal -- the type of signal to send
292413058a91SGregory Neil Shapiro **
2925*d39bd2c1SGregory Neil Shapiro ** Returns:
292640266059SGregory Neil Shapiro ** none.
2927c2aa98e2SPeter Wemm **
292840266059SGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
292940266059SGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
293040266059SGregory Neil Shapiro ** DOING.
2931c2aa98e2SPeter Wemm */
2932c2aa98e2SPeter Wemm
293340266059SGregory Neil Shapiro void
proc_list_signal(type,signal)293440266059SGregory Neil Shapiro proc_list_signal(type, signal)
293540266059SGregory Neil Shapiro int type;
293640266059SGregory Neil Shapiro int signal;
2937c2aa98e2SPeter Wemm {
293840266059SGregory Neil Shapiro int chldwasblocked;
293940266059SGregory Neil Shapiro int alrmwasblocked;
294040266059SGregory Neil Shapiro int i;
294140266059SGregory Neil Shapiro pid_t mypid = getpid();
2942c2aa98e2SPeter Wemm
294340266059SGregory Neil Shapiro /* block these signals so that we may signal cleanly */
294440266059SGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD);
294540266059SGregory Neil Shapiro alrmwasblocked = sm_blocksignal(SIGALRM);
294640266059SGregory Neil Shapiro
294740266059SGregory Neil Shapiro /* Find all processes of type and send signal */
294840266059SGregory Neil Shapiro for (i = 0; i < ProcListSize; i++)
294940266059SGregory Neil Shapiro {
295040266059SGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID ||
295140266059SGregory Neil Shapiro ProcListVec[i].proc_pid == mypid)
295240266059SGregory Neil Shapiro continue;
295340266059SGregory Neil Shapiro if (ProcListVec[i].proc_type != type)
295440266059SGregory Neil Shapiro continue;
295540266059SGregory Neil Shapiro (void) kill(ProcListVec[i].proc_pid, signal);
2956c2aa98e2SPeter Wemm }
2957c2aa98e2SPeter Wemm
295840266059SGregory Neil Shapiro /* restore the signals */
295940266059SGregory Neil Shapiro if (alrmwasblocked == 0)
296040266059SGregory Neil Shapiro (void) sm_releasesignal(SIGALRM);
296140266059SGregory Neil Shapiro if (chldwasblocked == 0)
296240266059SGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD);
2963c2aa98e2SPeter Wemm }
2964e92d3f3fSGregory Neil Shapiro
2965e92d3f3fSGregory Neil Shapiro /*
2966e92d3f3fSGregory Neil Shapiro ** COUNT_OPEN_CONNECTIONS
2967e92d3f3fSGregory Neil Shapiro **
2968e92d3f3fSGregory Neil Shapiro ** Parameters:
2969e92d3f3fSGregory Neil Shapiro ** hostaddr - ClientAddress
2970e92d3f3fSGregory Neil Shapiro **
2971e92d3f3fSGregory Neil Shapiro ** Returns:
2972e92d3f3fSGregory Neil Shapiro ** the number of open connections for this client
2973e92d3f3fSGregory Neil Shapiro **
2974e92d3f3fSGregory Neil Shapiro */
2975e92d3f3fSGregory Neil Shapiro
2976e92d3f3fSGregory Neil Shapiro int
count_open_connections(hostaddr)2977e92d3f3fSGregory Neil Shapiro count_open_connections(hostaddr)
2978e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr;
2979e92d3f3fSGregory Neil Shapiro {
2980e92d3f3fSGregory Neil Shapiro int i, n;
2981e92d3f3fSGregory Neil Shapiro
2982e92d3f3fSGregory Neil Shapiro if (hostaddr == NULL)
2983e92d3f3fSGregory Neil Shapiro return 0;
2984ffb83623SGregory Neil Shapiro
2985ffb83623SGregory Neil Shapiro /*
2986e3793f76SGregory Neil Shapiro ** This code gets called before proc_list_add() gets called,
2987e3793f76SGregory Neil Shapiro ** so we (the daemon child for this connection) have not yet
2988e3793f76SGregory Neil Shapiro ** counted ourselves. Hence initialize the counter to 1
2989e3793f76SGregory Neil Shapiro ** instead of 0 to compensate.
2990ffb83623SGregory Neil Shapiro */
2991ffb83623SGregory Neil Shapiro
2992ffb83623SGregory Neil Shapiro n = 1;
2993e92d3f3fSGregory Neil Shapiro for (i = 0; i < ProcListSize; i++)
2994e92d3f3fSGregory Neil Shapiro {
2995e92d3f3fSGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID)
2996e92d3f3fSGregory Neil Shapiro continue;
2997e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family !=
2998e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sa.sa_family)
2999e92d3f3fSGregory Neil Shapiro continue;
3000e92d3f3fSGregory Neil Shapiro #if NETINET
3001e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET &&
3002e92d3f3fSGregory Neil Shapiro (hostaddr->sin.sin_addr.s_addr ==
3003e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sin.sin_addr.s_addr))
3004e92d3f3fSGregory Neil Shapiro n++;
30055b0945b5SGregory Neil Shapiro #endif
3006e92d3f3fSGregory Neil Shapiro #if NETINET6
3007e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET6 &&
3008e92d3f3fSGregory Neil Shapiro IN6_ARE_ADDR_EQUAL(&(hostaddr->sin6.sin6_addr),
3009e92d3f3fSGregory Neil Shapiro &(ProcListVec[i].proc_hostaddr.sin6.sin6_addr)))
3010e92d3f3fSGregory Neil Shapiro n++;
30115b0945b5SGregory Neil Shapiro #endif
3012e92d3f3fSGregory Neil Shapiro }
3013e92d3f3fSGregory Neil Shapiro return n;
3014e92d3f3fSGregory Neil Shapiro }
3015ba00ec3dSGregory Neil Shapiro
3016da7d7b9cSGregory Neil Shapiro #if _FFR_XCNCT
3017da7d7b9cSGregory Neil Shapiro /*
3018da7d7b9cSGregory Neil Shapiro ** XCONNECT -- get X-CONNECT info
3019da7d7b9cSGregory Neil Shapiro **
3020da7d7b9cSGregory Neil Shapiro ** Parameters:
3021da7d7b9cSGregory Neil Shapiro ** inchannel -- FILE to check
3022da7d7b9cSGregory Neil Shapiro **
3023da7d7b9cSGregory Neil Shapiro ** Returns:
3024*d39bd2c1SGregory Neil Shapiro ** >0 if X-CONNECT/PROXY was used successfully (D_XCNCT*)
3025*d39bd2c1SGregory Neil Shapiro ** 0 if X-CONNECT/PROXY was not given
3026da7d7b9cSGregory Neil Shapiro ** -1 on error
3027*d39bd2c1SGregory Neil Shapiro ** -2 PROXY UNKNOWN
3028*d39bd2c1SGregory Neil Shapiro */
3029*d39bd2c1SGregory Neil Shapiro
3030*d39bd2c1SGregory Neil Shapiro /*
3031*d39bd2c1SGregory Neil Shapiro ** HA proxy version 1:
3032*d39bd2c1SGregory Neil Shapiro **
3033*d39bd2c1SGregory Neil Shapiro ** PROXY TCP[4|6] IPv[4|6]-src-addr IPv[4|6]-dst-addr src-port dst-port\r\n
3034*d39bd2c1SGregory Neil Shapiro ** PROXY UNKNOWN ...
3035*d39bd2c1SGregory Neil Shapiro ** examples:
3036*d39bd2c1SGregory Neil Shapiro ** "PROXY TCP4 255.255.255.255 255.255.255.255 65535 65535\r\n"
3037*d39bd2c1SGregory Neil Shapiro ** "PROXY TCP6 ffff:f...f:ffff ffff:f...f:ffff 65535 65535\r\n"
3038*d39bd2c1SGregory Neil Shapiro ** "PROXY UNKNOWN\r\n"
3039da7d7b9cSGregory Neil Shapiro */
3040da7d7b9cSGregory Neil Shapiro
3041da7d7b9cSGregory Neil Shapiro int
xconnect(inchannel)3042da7d7b9cSGregory Neil Shapiro xconnect(inchannel)
3043da7d7b9cSGregory Neil Shapiro SM_FILE_T *inchannel;
3044da7d7b9cSGregory Neil Shapiro {
3045da7d7b9cSGregory Neil Shapiro int r, i;
3046da7d7b9cSGregory Neil Shapiro char *p, *b, delim, inp[MAXINPLINE];
3047da7d7b9cSGregory Neil Shapiro SOCKADDR addr;
3048da7d7b9cSGregory Neil Shapiro char **pvp;
3049da7d7b9cSGregory Neil Shapiro char pvpbuf[PSBUFSIZE];
3050da7d7b9cSGregory Neil Shapiro char *peerhostname; /* name of SMTP peer or "localhost" */
3051da7d7b9cSGregory Neil Shapiro extern ENVELOPE BlankEnvelope;
3052*d39bd2c1SGregory Neil Shapiro #if _FFR_HAPROXY
3053*d39bd2c1SGregory Neil Shapiro int haproxy = AF_UNSPEC;
3054*d39bd2c1SGregory Neil Shapiro # define HAPROXY "PROXY "
3055*d39bd2c1SGregory Neil Shapiro # define HAPROXYLEN (sizeof(HAPROXY) - 1)
3056*d39bd2c1SGregory Neil Shapiro #endif
3057da7d7b9cSGregory Neil Shapiro
3058da7d7b9cSGregory Neil Shapiro #define XCONNECT "X-CONNECT "
3059da7d7b9cSGregory Neil Shapiro #define XCNNCTLEN (sizeof(XCONNECT) - 1)
3060da7d7b9cSGregory Neil Shapiro
3061da7d7b9cSGregory Neil Shapiro /* Ask the ruleset whether to use x-connect */
3062da7d7b9cSGregory Neil Shapiro pvp = NULL;
3063da7d7b9cSGregory Neil Shapiro peerhostname = RealHostName;
3064da7d7b9cSGregory Neil Shapiro if (peerhostname == NULL)
3065da7d7b9cSGregory Neil Shapiro peerhostname = "localhost";
3066da7d7b9cSGregory Neil Shapiro r = rscap("x_connect", peerhostname,
3067da7d7b9cSGregory Neil Shapiro anynet_ntoa(&RealHostAddr), &BlankEnvelope,
3068da7d7b9cSGregory Neil Shapiro &pvp, pvpbuf, sizeof(pvpbuf));
3069da7d7b9cSGregory Neil Shapiro if (tTd(75, 8))
3070da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: rscap=%d", r);
3071da7d7b9cSGregory Neil Shapiro if (r == EX_UNAVAILABLE)
3072da7d7b9cSGregory Neil Shapiro return 0;
3073da7d7b9cSGregory Neil Shapiro if (r != EX_OK)
3074da7d7b9cSGregory Neil Shapiro {
3075da7d7b9cSGregory Neil Shapiro /* ruleset error */
3076da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: rscap=%d", r);
3077da7d7b9cSGregory Neil Shapiro return 0;
3078da7d7b9cSGregory Neil Shapiro }
3079da7d7b9cSGregory Neil Shapiro if (pvp != NULL && pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
3080da7d7b9cSGregory Neil Shapiro {
3081da7d7b9cSGregory Neil Shapiro /* $#: no x-connect */
3082da7d7b9cSGregory Neil Shapiro if (tTd(75, 7))
3083da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: nope");
3084da7d7b9cSGregory Neil Shapiro return 0;
3085da7d7b9cSGregory Neil Shapiro }
3086da7d7b9cSGregory Neil Shapiro
3087*d39bd2c1SGregory Neil Shapiro #if _FFR_HAPROXY
3088*d39bd2c1SGregory Neil Shapiro if (pvp != NULL && pvp[0] != NULL && strcasecmp(pvp[0], "haproxy1") == 0)
3089*d39bd2c1SGregory Neil Shapiro {
3090*d39bd2c1SGregory Neil Shapiro haproxy = AF_LOCAL;
3091*d39bd2c1SGregory Neil Shapiro }
3092*d39bd2c1SGregory Neil Shapiro #endif
3093*d39bd2c1SGregory Neil Shapiro
30945b0945b5SGregory Neil Shapiro # if _FFR_XCNCT > 1
30955b0945b5SGregory Neil Shapiro if (pvp != NULL && pvp[0] != NULL &&
30965b0945b5SGregory Neil Shapiro pvp[0][0] == '2' && pvp[0][1] == '2' && pvp[0][2] == '0')
30975b0945b5SGregory Neil Shapiro {
30985b0945b5SGregory Neil Shapiro char *hostname; /* my hostname ($j) */
30995b0945b5SGregory Neil Shapiro
31005b0945b5SGregory Neil Shapiro hostname = macvalue('j', &BlankEnvelope);
31015b0945b5SGregory Neil Shapiro if (tTd(75, 7))
31025b0945b5SGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect=%s", pvp[0]);
31035b0945b5SGregory Neil Shapiro message("220-%s %s", hostname != NULL ? hostname : "xconnect",
31045b0945b5SGregory Neil Shapiro pvp[1] != NULL ? pvp[1] : "waiting for xconnect");
31055b0945b5SGregory Neil Shapiro sm_io_flush(OutChannel, SM_TIME_DEFAULT);
31065b0945b5SGregory Neil Shapiro }
31075b0945b5SGregory Neil Shapiro # endif
31085b0945b5SGregory Neil Shapiro
3109da7d7b9cSGregory Neil Shapiro p = sfgets(inp, sizeof(inp), InChannel, TimeOuts.to_nextcommand, "pre");
3110da7d7b9cSGregory Neil Shapiro if (tTd(75, 6))
3111da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: input=%s", p);
3112*d39bd2c1SGregory Neil Shapiro #if _FFR_HAPROXY
3113*d39bd2c1SGregory Neil Shapiro if (AF_UNSPEC != haproxy)
3114*d39bd2c1SGregory Neil Shapiro {
3115*d39bd2c1SGregory Neil Shapiro if (p == NULL || strncasecmp(p, HAPROXY, HAPROXYLEN) != 0)
3116*d39bd2c1SGregory Neil Shapiro return -1;
3117*d39bd2c1SGregory Neil Shapiro p += HAPROXYLEN;
3118*d39bd2c1SGregory Neil Shapiro # define HAPUNKNOWN "UNKNOWN"
3119*d39bd2c1SGregory Neil Shapiro # define HAPUNKNOWNLEN (sizeof(HAPUNKNOWN) - 1)
3120*d39bd2c1SGregory Neil Shapiro if (strncasecmp(p, HAPUNKNOWN, HAPUNKNOWNLEN) == 0)
3121*d39bd2c1SGregory Neil Shapiro {
3122*d39bd2c1SGregory Neil Shapiro /* how to handle this? */
3123*d39bd2c1SGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "haproxy: input=%s, status=ignored", p);
3124*d39bd2c1SGregory Neil Shapiro return -2;
3125*d39bd2c1SGregory Neil Shapiro }
3126*d39bd2c1SGregory Neil Shapiro # define HAPTCP4 "TCP4 "
3127*d39bd2c1SGregory Neil Shapiro # define HAPTCP6 "TCP6 "
3128*d39bd2c1SGregory Neil Shapiro # define HAPTCPNLEN (sizeof(HAPTCP4) - 1)
3129*d39bd2c1SGregory Neil Shapiro if (strncasecmp(p, HAPTCP4, HAPTCPNLEN) == 0)
3130*d39bd2c1SGregory Neil Shapiro haproxy = AF_INET;
3131*d39bd2c1SGregory Neil Shapiro # if NETINET6
3132*d39bd2c1SGregory Neil Shapiro if (strncasecmp(p, HAPTCP6, HAPTCPNLEN) == 0)
3133*d39bd2c1SGregory Neil Shapiro haproxy = AF_INET6;
3134*d39bd2c1SGregory Neil Shapiro # endif
3135*d39bd2c1SGregory Neil Shapiro if (AF_LOCAL != haproxy)
3136*d39bd2c1SGregory Neil Shapiro {
3137*d39bd2c1SGregory Neil Shapiro p += HAPTCPNLEN;
3138*d39bd2c1SGregory Neil Shapiro goto getip;
3139*d39bd2c1SGregory Neil Shapiro }
3140*d39bd2c1SGregory Neil Shapiro return -1;
3141*d39bd2c1SGregory Neil Shapiro }
3142*d39bd2c1SGregory Neil Shapiro #endif
3143da7d7b9cSGregory Neil Shapiro if (p == NULL || strncasecmp(p, XCONNECT, XCNNCTLEN) != 0)
3144da7d7b9cSGregory Neil Shapiro return -1;
3145da7d7b9cSGregory Neil Shapiro p += XCNNCTLEN;
31465b0945b5SGregory Neil Shapiro while (SM_ISSPACE(*p))
3147da7d7b9cSGregory Neil Shapiro p++;
3148da7d7b9cSGregory Neil Shapiro
3149*d39bd2c1SGregory Neil Shapiro #if _FFR_HAPROXY
3150*d39bd2c1SGregory Neil Shapiro getip:
3151*d39bd2c1SGregory Neil Shapiro #endif
3152da7d7b9cSGregory Neil Shapiro /* parameters: IPAddress [Hostname[ M]] */
3153da7d7b9cSGregory Neil Shapiro b = p;
3154da7d7b9cSGregory Neil Shapiro while (*p != '\0' && isascii(*p) &&
3155da7d7b9cSGregory Neil Shapiro (isalnum(*p) || *p == '.' || *p== ':'))
3156da7d7b9cSGregory Neil Shapiro p++;
3157da7d7b9cSGregory Neil Shapiro delim = *p;
3158da7d7b9cSGregory Neil Shapiro *p = '\0';
3159da7d7b9cSGregory Neil Shapiro
3160da7d7b9cSGregory Neil Shapiro memset(&addr, '\0', sizeof(addr));
3161da7d7b9cSGregory Neil Shapiro addr.sin.sin_addr.s_addr = inet_addr(b);
3162da7d7b9cSGregory Neil Shapiro if (addr.sin.sin_addr.s_addr != INADDR_NONE)
3163da7d7b9cSGregory Neil Shapiro {
3164da7d7b9cSGregory Neil Shapiro addr.sa.sa_family = AF_INET;
3165da7d7b9cSGregory Neil Shapiro memcpy(&RealHostAddr, &addr, sizeof(addr));
3166da7d7b9cSGregory Neil Shapiro if (tTd(75, 2))
3167da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: addr=%s",
3168da7d7b9cSGregory Neil Shapiro anynet_ntoa(&RealHostAddr));
3169da7d7b9cSGregory Neil Shapiro }
3170da7d7b9cSGregory Neil Shapiro # if NETINET6
3171da7d7b9cSGregory Neil Shapiro else if ((r = inet_pton(AF_INET6, b, &addr.sin6.sin6_addr)) == 1)
3172da7d7b9cSGregory Neil Shapiro {
3173da7d7b9cSGregory Neil Shapiro addr.sa.sa_family = AF_INET6;
3174da7d7b9cSGregory Neil Shapiro memcpy(&RealHostAddr, &addr, sizeof(addr));
3175da7d7b9cSGregory Neil Shapiro }
31765b0945b5SGregory Neil Shapiro # endif
3177da7d7b9cSGregory Neil Shapiro else
3178da7d7b9cSGregory Neil Shapiro return -1;
3179*d39bd2c1SGregory Neil Shapiro #if _FFR_HAPROXY
3180*d39bd2c1SGregory Neil Shapiro if (AF_UNSPEC != haproxy)
3181*d39bd2c1SGregory Neil Shapiro {
3182*d39bd2c1SGregory Neil Shapiro /*
3183*d39bd2c1SGregory Neil Shapiro ** dst-addr and dst-port are ignored because
3184*d39bd2c1SGregory Neil Shapiro ** they are not really worth to check:
3185*d39bd2c1SGregory Neil Shapiro ** IPv[4|6]-dst-addr: must be one of "our" addresses,
3186*d39bd2c1SGregory Neil Shapiro ** dst-port: must be the DaemonPort.
3187*d39bd2c1SGregory Neil Shapiro ** We also do not check whether
3188*d39bd2c1SGregory Neil Shapiro ** haproxy == addr.sa.sa_family
3189*d39bd2c1SGregory Neil Shapiro */
3190*d39bd2c1SGregory Neil Shapiro
3191*d39bd2c1SGregory Neil Shapiro if (' ' == delim)
3192*d39bd2c1SGregory Neil Shapiro {
3193*d39bd2c1SGregory Neil Shapiro b = ++p;
3194*d39bd2c1SGregory Neil Shapiro while (*p != '\0' && !SM_ISSPACE(*p))
3195*d39bd2c1SGregory Neil Shapiro ++p;
3196*d39bd2c1SGregory Neil Shapiro if (*p != '\0' && SM_ISSPACE(*p))
3197*d39bd2c1SGregory Neil Shapiro ++p;
3198*d39bd2c1SGregory Neil Shapiro if (*p != '\0')
3199*d39bd2c1SGregory Neil Shapiro {
3200*d39bd2c1SGregory Neil Shapiro unsigned short port;
3201*d39bd2c1SGregory Neil Shapiro
3202*d39bd2c1SGregory Neil Shapiro port = htons(atoi(p));
3203*d39bd2c1SGregory Neil Shapiro if (AF_INET == haproxy)
3204*d39bd2c1SGregory Neil Shapiro RealHostAddr.sin.sin_port = port;
3205*d39bd2c1SGregory Neil Shapiro # if NETINET6
3206*d39bd2c1SGregory Neil Shapiro if (AF_INET6 == haproxy)
3207*d39bd2c1SGregory Neil Shapiro RealHostAddr.sin6.sin6_port = port;
3208*d39bd2c1SGregory Neil Shapiro # endif
3209*d39bd2c1SGregory Neil Shapiro }
3210*d39bd2c1SGregory Neil Shapiro }
3211*d39bd2c1SGregory Neil Shapiro SM_FREE(RealHostName);
3212*d39bd2c1SGregory Neil Shapiro return D_XCNCT;
3213*d39bd2c1SGregory Neil Shapiro }
3214*d39bd2c1SGregory Neil Shapiro #endif
3215da7d7b9cSGregory Neil Shapiro
3216da7d7b9cSGregory Neil Shapiro /* more parameters? */
3217da7d7b9cSGregory Neil Shapiro if (delim != ' ')
3218da7d7b9cSGregory Neil Shapiro return D_XCNCT;
3219da7d7b9cSGregory Neil Shapiro
3220da7d7b9cSGregory Neil Shapiro for (b = ++p, i = 0;
3221da7d7b9cSGregory Neil Shapiro *p != '\0' && isascii(*p) && (isalnum(*p) || *p == '.' || *p == '-');
3222da7d7b9cSGregory Neil Shapiro p++, i++)
3223da7d7b9cSGregory Neil Shapiro ;
3224da7d7b9cSGregory Neil Shapiro if (i == 0)
3225da7d7b9cSGregory Neil Shapiro return D_XCNCT;
3226da7d7b9cSGregory Neil Shapiro delim = *p;
32272fb4f839SGregory Neil Shapiro if (i > MAXNAME) /* EAI:ok */
32282fb4f839SGregory Neil Shapiro b[MAXNAME] = '\0'; /* EAI:ok */
3229da7d7b9cSGregory Neil Shapiro else
3230da7d7b9cSGregory Neil Shapiro b[i] = '\0';
32315b0945b5SGregory Neil Shapiro SM_FREE(RealHostName);
3232da7d7b9cSGregory Neil Shapiro RealHostName = newstr(b);
3233da7d7b9cSGregory Neil Shapiro if (tTd(75, 2))
3234da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: host=%s", b);
3235da7d7b9cSGregory Neil Shapiro *p = delim;
3236da7d7b9cSGregory Neil Shapiro
3237da7d7b9cSGregory Neil Shapiro b = p;
3238da7d7b9cSGregory Neil Shapiro if (*p != ' ')
3239da7d7b9cSGregory Neil Shapiro return D_XCNCT;
3240da7d7b9cSGregory Neil Shapiro
32415b0945b5SGregory Neil Shapiro while (*p != '\0' && SM_ISSPACE(*p))
3242da7d7b9cSGregory Neil Shapiro p++;
3243da7d7b9cSGregory Neil Shapiro
3244da7d7b9cSGregory Neil Shapiro if (tTd(75, 4))
3245da7d7b9cSGregory Neil Shapiro {
3246da7d7b9cSGregory Neil Shapiro char *e;
3247da7d7b9cSGregory Neil Shapiro
3248da7d7b9cSGregory Neil Shapiro e = strpbrk(p, "\r\n");
3249da7d7b9cSGregory Neil Shapiro if (e != NULL)
3250da7d7b9cSGregory Neil Shapiro *e = '\0';
3251da7d7b9cSGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "x-connect: rest=%s", p);
3252da7d7b9cSGregory Neil Shapiro }
3253da7d7b9cSGregory Neil Shapiro if (*p == 'M')
3254da7d7b9cSGregory Neil Shapiro return D_XCNCT_M;
3255da7d7b9cSGregory Neil Shapiro
3256da7d7b9cSGregory Neil Shapiro return D_XCNCT;
3257da7d7b9cSGregory Neil Shapiro }
3258da7d7b9cSGregory Neil Shapiro #endif /* _FFR_XCNCT */
3259