2 * Copyright (c) 1998-2007 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: util.c,v 8.413 2007/09/26 23:29:11 ca Exp $")
18 #include <sm/sendmail.h>
23 ** NEWSTR -- Create a copy of a C string
26 ** s -- the string to copy.
29 ** pointer to newly allocated string.
42 sm_strlcpy(n
, s
, l
+ 1);
47 ** ADDQUOTES -- Adds quotes & quote bits to a string.
49 ** Runs through a string and adds backslashes and quote bits.
52 ** s -- the string to modify.
53 ** rpool -- resource pool from which to allocate result
56 ** pointer to quoted string.
71 /* Find length of quoted string */
72 while ((c
= *p
++) != '\0')
75 if (c
== '\\' || c
== '"')
79 q
= r
= sm_rpool_malloc_x(rpool
, len
+ 3);
82 /* add leading quote */
84 while ((c
= *p
++) != '\0')
87 if (c
== '\\' || c
== '"')
97 ** STRIPBACKSLASH -- Strip all leading backslashes from a string, provided
98 ** the following character is alpha-numerical.
100 ** This is done in place.
103 ** s -- the string to strip.
115 if (s
== NULL
|| *s
== '\0')
118 while (*p
== '\\' && (p
[1] == '\\' || (isascii(p
[1]) && isalnum(p
[1]))))
127 ** RFC822_STRING -- Checks string for proper RFC822 string quoting.
129 ** Runs through a string and verifies RFC822 special characters
130 ** are only found inside comments, quoted strings, or backslash
131 ** escaped. Also verified balanced quotes and parenthesis.
134 ** s -- the string to modify.
137 ** true iff the string is RFC822 compliant, false otherwise.
153 /* escaped character */
160 else if (commentlev
== 0 && *c
== '"')
174 else if (commentlev
== 0 &&
175 strchr(MustQuoteChars
, *c
) != NULL
)
181 /* unbalanced '"' or '(' */
182 return !quoted
&& commentlev
== 0;
186 ** SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string
188 ** Arbitrarily shorten (in place) an RFC822 string and rebalance
189 ** comments and quotes.
192 ** string -- the string to shorten
193 ** length -- the maximum size, 0 if no maximum
196 ** true if string is changed, false otherwise
199 ** Changes string in place, possibly resulting
200 ** in a shorter string.
204 shorten_rfc822_string(string
, length
)
208 bool backslash
= false;
209 bool modified
= false;
216 ** If have to rebalance an already short enough string,
217 ** need to do it within allocated space.
220 slen
= strlen(string
);
221 if (length
== 0 || slen
< length
)
234 else if (*ptr
== '(')
239 else if (*ptr
== ')')
241 if (--parencount
< 0)
245 /* Inside a comment, quotes don't matter */
246 if (parencount
<= 0 && *ptr
== '"')
250 /* Check for sufficient space for next character */
251 if (length
- (ptr
- string
) <= (size_t) ((backslash
? 1 : 0) +
255 /* Not enough, backtrack */
258 else if (*ptr
== '(' && !quoted
)
260 else if (*ptr
== '"' && parencount
== 0)
268 while (parencount
-- > 0)
295 ** FIND_CHARACTER -- find an unquoted character in an RFC822 string
297 ** Find an unquoted, non-commented character in an RFC822
298 ** string and return a pointer to its location in the
302 ** string -- the string to search
303 ** character -- the character to find
306 ** pointer to the character, or
307 ** a pointer to the end of the line if character is not found
311 find_character(string
, character
)
315 bool backslash
= false;
319 while (string
!= NULL
&& *string
!= '\0')
324 if (!quoted
&& character
== '\\' && *string
== '\\')
341 if (--parencount
< 0)
346 /* Inside a comment, nothing matters */
355 else if (*string
== character
&& !quoted
)
360 /* Return pointer to the character */
365 ** CHECK_BODYTYPE -- check bodytype parameter
368 ** bodytype -- bodytype parameter
371 ** BODYTYPE_* according to parameter
376 check_bodytype(bodytype
)
379 /* check body type for legality */
380 if (bodytype
== NULL
)
381 return BODYTYPE_NONE
;
382 if (sm_strcasecmp(bodytype
, "7BIT") == 0)
383 return BODYTYPE_7BIT
;
384 if (sm_strcasecmp(bodytype
, "8BITMIME") == 0)
385 return BODYTYPE_8BITMIME
;
386 return BODYTYPE_ILLEGAL
;
390 ** TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..."
393 ** str -- string to truncate
394 ** len -- maximum length (including '\0') (0 for unlimited)
395 ** delim -- delimiter character
402 truncate_at_delim(str
, len
, delim
)
409 if (str
== NULL
|| len
== 0 || strlen(str
) < len
)
412 *(str
+ len
- 1) = '\0';
413 while ((p
= strrchr(str
, delim
)) != NULL
)
416 if (p
- str
+ 4 < len
)
420 (void) sm_strlcat(str
, "...", len
);
425 /* Couldn't find a place to append "..." */
427 (void) sm_strlcpy(str
, "...", len
);
433 ** XALLOC -- Allocate memory, raise an exception on error
436 ** sz -- size of area to allocate.
439 ** pointer to data region.
442 ** SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory
445 ** Memory is allocated.
450 xalloc_tagged(sz
, file
, line
)
454 #else /* SM_HEAP_CHECK */
457 #endif /* SM_HEAP_CHECK */
463 /* some systems can't handle size zero mallocs */
467 /* scaffolding for testing error handling code */
468 sm_xtrap_raise_x(&SmHeapOutOfMemory
);
470 p
= sm_malloc_tagged((unsigned) sz
, file
, line
, sm_heap_group());
473 sm_exc_raise_x(&SmHeapOutOfMemory
);
479 ** COPYPLIST -- copy list of pointers.
481 ** This routine is the equivalent of strdup for lists of
485 ** list -- list of pointers to copy.
486 ** Must be NULL terminated.
487 ** copycont -- if true, copy the contents of the vector
488 ** (which must be a string) also.
489 ** rpool -- resource pool from which to allocate storage,
497 copyplist(list
, copycont
, rpool
)
503 register char **newvp
;
505 for (vp
= list
; *vp
!= NULL
; vp
++)
510 newvp
= (char **) sm_rpool_malloc_x(rpool
, (vp
- list
) * sizeof(*vp
));
511 memmove((char *) newvp
, (char *) list
, (int) (vp
- list
) * sizeof(*vp
));
515 for (vp
= newvp
; *vp
!= NULL
; vp
++)
516 *vp
= sm_rpool_strdup_x(rpool
, *vp
);
523 ** COPYQUEUE -- copy address queue.
525 ** This routine is the equivalent of strdup for address queues;
526 ** addresses marked as QS_IS_DEAD() aren't copied
529 ** addr -- list of address structures to copy.
530 ** rpool -- resource pool from which to allocate storage
537 copyqueue(addr
, rpool
)
541 register ADDRESS
*newaddr
;
543 register ADDRESS
**tail
= &ret
;
547 if (!QS_IS_DEAD(addr
->q_state
))
549 newaddr
= (ADDRESS
*) sm_rpool_malloc_x(rpool
,
551 STRUCTCOPY(*addr
, *newaddr
);
553 tail
= &newaddr
->q_next
;
563 ** LOG_SENDMAIL_PID -- record sendmail pid and command line.
566 ** e -- the current envelope.
572 ** writes pidfile, logs command line.
573 ** keeps file open and locked to prevent overwrite of active file
576 static SM_FILE_T
*Pidf
= NULL
;
583 char pidpath
[MAXPATHLEN
];
584 extern char *CommandLineArgs
;
586 /* write the pid to the log file for posterity */
587 sff
= SFF_NOLINK
|SFF_ROOTOK
|SFF_REGONLY
|SFF_CREAT
|SFF_NBLOCK
;
588 if (TrustedUid
!= 0 && RealUid
== TrustedUid
)
589 sff
|= SFF_OPENASROOT
;
590 expand(PidFile
, pidpath
, sizeof(pidpath
), e
);
591 Pidf
= safefopen(pidpath
, O_WRONLY
|O_TRUNC
, FileMode
, sff
);
594 if (errno
== EWOULDBLOCK
)
595 sm_syslog(LOG_ERR
, NOQID
,
596 "unable to write pid to %s: file in use by another process",
599 sm_syslog(LOG_ERR
, NOQID
,
600 "unable to write pid to %s: %s",
601 pidpath
, sm_errstring(errno
));
605 PidFilePid
= getpid();
607 /* write the process id on line 1 */
608 (void) sm_io_fprintf(Pidf
, SM_TIME_DEFAULT
, "%ld\n",
611 /* line 2 contains all command line flags */
612 (void) sm_io_fprintf(Pidf
, SM_TIME_DEFAULT
, "%s\n",
616 (void) sm_io_flush(Pidf
, SM_TIME_DEFAULT
);
619 ** Leave pid file open until process ends
620 ** so it's not overwritten by another
625 sm_syslog(LOG_INFO
, NOQID
, "started as: %s", CommandLineArgs
);
629 ** CLOSE_SENDMAIL_PID -- close sendmail pid file
644 (void) sm_io_close(Pidf
, SM_TIME_DEFAULT
);
649 ** SET_DELIVERY_MODE -- set and record the delivery mode
652 ** mode -- delivery mode
653 ** e -- the current envelope.
659 ** sets {deliveryMode} macro
663 set_delivery_mode(mode
, e
)
669 e
->e_sendmode
= (char) mode
;
670 buf
[0] = (char) mode
;
672 macdefine(&e
->e_macro
, A_TEMP
, macid("{deliveryMode}"), buf
);
676 ** SET_OP_MODE -- set and record the op mode
680 ** e -- the current envelope.
686 ** sets {opMode} macro
694 extern ENVELOPE BlankEnvelope
;
696 OpMode
= (char) mode
;
697 buf
[0] = (char) mode
;
699 macdefine(&BlankEnvelope
.e_macro
, A_TEMP
, MID_OPMODE
, buf
);
703 ** PRINTAV -- print argument vector.
706 ** fp -- output file pointer.
707 ** av -- argument vector.
724 sm_dprintf("\n\t%08lx=", (unsigned long) *av
);
726 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, ' ');
728 sm_dprintf("%s", str2prt(*av
++));
732 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, '\n');
736 ** XPUTS -- put string doing control escapes.
739 ** fp -- output file pointer.
740 ** s -- string to put.
756 bool shiftout
= false;
757 extern struct metamac MetaMacros
[];
758 static SM_DEBUG_T DebugANSI
= SM_DEBUG_INITIALIZER("ANSI",
759 "@(#)$Debug: ANSI - enable reverse video in debug output $");
762 ** TermEscape is set here, rather than in main(),
763 ** because ANSI mode can be turned on or off at any time
764 ** if we are in -bt rule testing mode.
767 if (sm_debug_unknown(&DebugANSI
))
769 if (sm_debug_active(&DebugANSI
, 1))
771 TermEscape
.te_rv_on
= "\033[7m";
772 TermEscape
.te_normal
= "\033[0m";
776 TermEscape
.te_rv_on
= "";
777 TermEscape
.te_normal
= "";
783 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, "%s<null>%s",
784 TermEscape
.te_rv_on
, TermEscape
.te_normal
);
787 while ((c
= (*s
++ & 0377)) != '\0')
791 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, "%s",
792 TermEscape
.te_normal
);
795 if (!isascii(c
) && !tTd(84, 1))
799 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
,
801 TermEscape
.te_rv_on
);
808 if (c
== MACROEXPAND
|| c
== MACRODEXPAND
)
810 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
,
812 TermEscape
.te_rv_on
);
813 if (c
== MACRODEXPAND
)
814 (void) sm_io_putc(fp
,
815 SM_TIME_DEFAULT
, '&');
819 if (strchr("=~&?", *s
) != NULL
)
820 (void) sm_io_putc(fp
,
823 if (bitset(0200, *s
))
824 (void) sm_io_fprintf(fp
,
827 macname(bitidx(*s
++)));
829 (void) sm_io_fprintf(fp
,
835 for (mp
= MetaMacros
; mp
->metaname
!= '\0'; mp
++)
837 if (bitidx(mp
->metaval
) == c
)
839 (void) sm_io_fprintf(fp
,
848 if (c
== MATCHCLASS
|| c
== MATCHNCLASS
)
850 if (bitset(0200, *s
))
851 (void) sm_io_fprintf(fp
,
854 macname(bitidx(*s
++)));
856 (void) sm_io_fprintf(fp
,
861 if (mp
->metaname
!= '\0')
864 /* unrecognized meta character */
865 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, "%sM-",
866 TermEscape
.te_rv_on
);
873 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, c
);
877 /* wasn't a meta-macro -- find another way to print it */
894 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, "%s",
895 TermEscape
.te_rv_on
);
900 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, '\\');
901 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, c
);
904 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, " %o ", c
);
906 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, " %#x ", c
);
907 else if (!isascii(c
) && !tTd(84, 1))
909 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, '^');
910 (void) sm_io_putc(fp
, SM_TIME_DEFAULT
, c
^ 0100);
914 (void) sm_io_fprintf(fp
, SM_TIME_DEFAULT
, "%s",
915 TermEscape
.te_normal
);
916 (void) sm_io_flush(fp
, SM_TIME_DEFAULT
);
920 ** MAKELOWER -- Translate a line into lower case
923 ** p -- the string to translate. If NULL, return is
930 ** String pointed to by p is translated to lower case.
941 for (; (c
= *p
) != '\0'; p
++)
942 if (isascii(c
) && isupper(c
))
947 ** FIXCRLF -- fix <CR><LF> in line.
949 ** Looks for the <CR><LF> combination and turns it into the
950 ** UNIX canonical <NL> character. It only takes one line,
951 ** i.e., it is assumed that the first <NL> found is the end
955 ** line -- the line to fix.
956 ** stripnl -- if true, strip the newline also.
962 ** line is changed in place.
966 fixcrlf(line
, stripnl
)
972 p
= strchr(line
, '\n');
975 if (p
> line
&& p
[-1] == '\r')
983 ** PUTLINE -- put a line like fputs obeying SMTP conventions
985 ** This routine always guarantees outputing a newline (or CRLF,
986 ** as appropriate) at the end of the string.
990 ** mci -- the mailer connection information.
993 ** true iff line was written successfully
996 ** output of l to mci->mci_out.
1004 return putxline(l
, strlen(l
), mci
, PXLF_MAPFROM
);
1008 ** PUTXLINE -- putline with flags bits.
1010 ** This routine always guarantees outputing a newline (or CRLF,
1011 ** as appropriate) at the end of the string.
1014 ** l -- line to put.
1015 ** len -- the length of the line.
1016 ** mci -- the mailer connection information.
1017 ** pxflags -- flag bits:
1018 ** PXLF_MAPFROM -- map From_ to >From_.
1019 ** PXLF_STRIP8BIT -- strip 8th bit.
1020 ** PXLF_HEADER -- map bare newline in header to newline space.
1021 ** PXLF_NOADDEOL -- don't add an EOL if one wasn't present.
1022 ** PXLF_STRIPMQUOTE -- strip METAQUOTE bytes.
1025 ** true iff line was written successfully
1028 ** output of l to mci->mci_out.
1032 #define PUTX(limit) \
1035 quotenext = false; \
1038 unsigned char c = (unsigned char) *l++; \
1040 if (bitset(PXLF_STRIPMQUOTE, pxflags) && \
1041 !quotenext && c == METAQUOTE) \
1046 quotenext = false; \
1049 if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, \
1055 if (TrafficLogFile != NULL) \
1056 (void) sm_io_putc(TrafficLogFile, \
1063 putxline(l
, len
, mci
, pxflags
)
1069 register char *p
, *end
;
1071 bool dead
, quotenext
, strip8bit
;
1073 /* strip out 0200 bits -- these can look like TELNET protocol */
1074 strip8bit
= bitset(MCIF_7BIT
, mci
->mci_flags
) ||
1075 bitset(PXLF_STRIP8BIT
, pxflags
);
1084 /* find the end of the line */
1085 p
= memchr(l
, '\n', end
- l
);
1092 if (TrafficLogFile
!= NULL
)
1093 (void) sm_io_fprintf(TrafficLogFile
, SM_TIME_DEFAULT
,
1094 "%05d >>> ", (int) CurrentPid
);
1096 /* check for line overflow */
1097 while (mci
->mci_mailer
->m_linelimit
> 0 &&
1098 (p
- l
+ slop
) > mci
->mci_mailer
->m_linelimit
)
1100 register char *q
= &l
[mci
->mci_mailer
->m_linelimit
- slop
- 1];
1102 if (l
[0] == '.' && slop
== 0 &&
1103 bitnset(M_XDOT
, mci
->mci_mailer
->m_flags
))
1105 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
,
1108 if (TrafficLogFile
!= NULL
)
1109 (void) sm_io_putc(TrafficLogFile
,
1110 SM_TIME_DEFAULT
, '.');
1112 else if (l
[0] == 'F' && slop
== 0 &&
1113 bitset(PXLF_MAPFROM
, pxflags
) &&
1114 strncmp(l
, "From ", 5) == 0 &&
1115 bitnset(M_ESCFROM
, mci
->mci_mailer
->m_flags
))
1117 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
,
1120 if (TrafficLogFile
!= NULL
)
1121 (void) sm_io_putc(TrafficLogFile
,
1132 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
,
1133 '!') == SM_IO_EOF
||
1134 sm_io_fputs(mci
->mci_out
, SM_TIME_DEFAULT
,
1135 mci
->mci_mailer
->m_eol
) == SM_IO_EOF
||
1136 sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
,
1142 if (TrafficLogFile
!= NULL
)
1144 (void) sm_io_fprintf(TrafficLogFile
,
1155 /* output last part */
1156 if (l
[0] == '.' && slop
== 0 &&
1157 bitnset(M_XDOT
, mci
->mci_mailer
->m_flags
) &&
1158 !bitset(MCIF_INLONGLINE
, mci
->mci_flags
))
1160 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
, '.') ==
1166 if (TrafficLogFile
!= NULL
)
1167 (void) sm_io_putc(TrafficLogFile
,
1168 SM_TIME_DEFAULT
, '.');
1170 else if (l
[0] == 'F' && slop
== 0 &&
1171 bitset(PXLF_MAPFROM
, pxflags
) &&
1172 strncmp(l
, "From ", 5) == 0 &&
1173 bitnset(M_ESCFROM
, mci
->mci_mailer
->m_flags
) &&
1174 !bitset(MCIF_INLONGLINE
, mci
->mci_flags
))
1176 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
, '>') ==
1182 if (TrafficLogFile
!= NULL
)
1183 (void) sm_io_putc(TrafficLogFile
,
1184 SM_TIME_DEFAULT
, '>');
1190 if (TrafficLogFile
!= NULL
)
1191 (void) sm_io_putc(TrafficLogFile
, SM_TIME_DEFAULT
,
1193 if ((!bitset(PXLF_NOADDEOL
, pxflags
) || !noeol
))
1195 mci
->mci_flags
&= ~MCIF_INLONGLINE
;
1196 if (sm_io_fputs(mci
->mci_out
, SM_TIME_DEFAULT
,
1197 mci
->mci_mailer
->m_eol
) == SM_IO_EOF
)
1204 mci
->mci_flags
|= MCIF_INLONGLINE
;
1206 if (l
< end
&& *l
== '\n')
1208 if (*++l
!= ' ' && *l
!= '\t' && *l
!= '\0' &&
1209 bitset(PXLF_HEADER
, pxflags
))
1211 if (sm_io_putc(mci
->mci_out
, SM_TIME_DEFAULT
,
1218 if (TrafficLogFile
!= NULL
)
1219 (void) sm_io_putc(TrafficLogFile
,
1220 SM_TIME_DEFAULT
, ' ');
1229 ** XUNLINK -- unlink a file, doing logging as appropriate.
1232 ** f -- name of file to unlink.
1235 ** return value of unlink()
1249 sm_syslog(LOG_DEBUG
, CurEnv
->e_id
, "unlink %s", f
);
1253 if (i
< 0 && LogLevel
> 97)
1254 sm_syslog(LOG_DEBUG
, CurEnv
->e_id
, "%s: unlink-fail %d",
1263 ** SFGETS -- "safe" fgets -- times out and ignores random interrupts.
1266 ** buf -- place to put the input line.
1267 ** siz -- size of buf.
1268 ** fp -- file to read from.
1269 ** timeout -- the timeout before error occurs.
1270 ** during -- what we are trying to read (for error messages).
1273 ** NULL on error (including timeout). This may also leave
1274 ** buf containing a null string.
1280 sfgets(buf
, siz
, fp
, timeout
, during
)
1291 SM_REQUIRE(siz
> 0);
1292 SM_REQUIRE(buf
!= NULL
);
1305 /* convert the timeout to sm_io notation */
1306 io_timeout
= (timeout
<= 0) ? SM_TIME_DEFAULT
: timeout
* 1000;
1307 while (!sm_io_eof(fp
) && !sm_io_error(fp
))
1310 p
= sm_io_fgets(fp
, io_timeout
, buf
, siz
);
1311 if (p
== NULL
&& errno
== EAGAIN
)
1313 /* The sm_io_fgets() call timedout */
1315 sm_syslog(LOG_NOTICE
, CurEnv
->e_id
,
1316 "timeout waiting for input from %.100s during %s",
1323 if (TrafficLogFile
!= NULL
)
1324 (void) sm_io_fprintf(TrafficLogFile
,
1326 "%05d <<< [TIMEOUT]\n",
1331 if (p
!= NULL
|| errno
!= EINTR
)
1333 (void) sm_io_clearerr(fp
);
1337 /* clean up the books and exit */
1342 if (TrafficLogFile
!= NULL
)
1343 (void) sm_io_fprintf(TrafficLogFile
, SM_TIME_DEFAULT
,
1349 if (TrafficLogFile
!= NULL
)
1350 (void) sm_io_fprintf(TrafficLogFile
, SM_TIME_DEFAULT
,
1351 "%05d <<< %s", (int) CurrentPid
, buf
);
1354 for (p
= buf
; *p
!= '\0'; p
++)
1357 else if (!HasEightBits
)
1359 for (p
= buf
; *p
!= '\0'; p
++)
1361 if (bitset(0200, *p
))
1363 HasEightBits
= true;
1372 ** FGETFOLDED -- like fgets, but knows about folded lines.
1375 ** buf -- place to put result.
1376 ** np -- pointer to bytes available; will be updated with
1377 ** the actual buffer size (not number of bytes filled)
1379 ** f -- file to read from.
1382 ** input line(s) on success, NULL on error or SM_IO_EOF.
1383 ** This will normally be buf -- unless the line is too
1384 ** long, when it will be sm_malloc_x()ed.
1387 ** buf gets lines from f, with continuation lines (lines
1388 ** with leading white space) appended. CRLF's are mapped
1389 ** into single newlines. Any trailing NL is stripped.
1393 fgetfolded(buf
, np
, f
)
1398 register char *p
= buf
;
1403 SM_REQUIRE(np
!= NULL
);
1406 SM_REQUIRE(buf
!= NULL
);
1415 while ((i
= sm_io_getc(f
, SM_TIME_DEFAULT
)) != SM_IO_EOF
)
1419 i
= sm_io_getc(f
, SM_TIME_DEFAULT
);
1423 (void) sm_io_ungetc(f
, SM_TIME_DEFAULT
,
1430 /* allocate new space */
1435 if (nn
< MEMCHUNKSIZE
)
1439 nbp
= sm_malloc_x(nn
);
1440 memmove(nbp
, bp
, p
- bp
);
1452 i
= sm_io_getc(f
, SM_TIME_DEFAULT
);
1454 (void) sm_io_ungetc(f
, SM_TIME_DEFAULT
, i
);
1455 if (i
!= ' ' && i
!= '\t')
1468 ** CURTIME -- return current time.
1474 ** the current time.
1487 ** ATOBOOL -- convert a string representation to boolean.
1489 ** Defaults to false
1492 ** s -- string to convert. Takes "tTyY", empty, and NULL as true,
1496 ** A boolean representation of the string.
1503 if (s
== NULL
|| *s
== '\0' || strchr("tTyY", *s
) != NULL
)
1509 ** ATOOCT -- convert a string representation to octal.
1512 ** s -- string to convert.
1515 ** An integer representing the string interpreted as an
1525 while (*s
>= '0' && *s
<= '7')
1526 i
= (i
<< 3) | (*s
++ - '0');
1531 ** BITINTERSECT -- tell if two bitmaps intersect
1534 ** a, b -- the bitmaps in question
1537 ** true if they have a non-null intersection
1548 for (i
= BITMAPBYTES
/ sizeof(int); --i
>= 0; )
1550 if ((a
[i
] & b
[i
]) != 0)
1557 ** BITZEROP -- tell if a bitmap is all zero
1560 ** map -- the bit map to check
1563 ** true if map is all zero.
1564 ** false if there are any bits set in map.
1573 for (i
= BITMAPBYTES
/ sizeof(int); --i
>= 0; )
1582 ** STRCONTAINEDIN -- tell if one string is contained in another
1585 ** icase -- ignore case?
1586 ** a -- possible substring.
1587 ** b -- possible superstring.
1590 ** true if a is contained in b (case insensitive).
1595 strcontainedin(icase
, a
, b
)
1607 if (icase
&& isascii(c
) && isupper(c
))
1609 for (; lb
-- >= la
; b
++)
1614 isascii(*b
) && isupper(*b
) && tolower(*b
) != c
)
1616 if (sm_strncasecmp(a
, b
, la
) == 0)
1623 if (strncmp(a
, b
, la
) == 0)
1631 ** CHECKFD012 -- check low numbered file descriptors
1633 ** File descriptors 0, 1, and 2 should be open at all times.
1634 ** This routine verifies that, and fixes it if not true.
1637 ** where -- a tag printed if the assertion failed
1650 for (i
= 0; i
< 3; i
++)
1656 ** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging
1659 ** fd -- file descriptor to check.
1660 ** where -- tag to print on failure.
1667 checkfdopen(fd
, where
)
1674 if (fstat(fd
, &st
) < 0 && errno
== EBADF
)
1676 syserr("checkfdopen(%d): %s not open as expected!", fd
, where
);
1683 ** CHECKFDS -- check for new or missing file descriptors
1686 ** where -- tag for printing. If null, take a base line.
1692 ** If where is set, shows changes since the last call.
1701 bool printhdr
= true;
1702 int save_errno
= errno
;
1703 static BITMAP256 baseline
;
1704 extern int DtableSize
;
1706 if (DtableSize
> BITMAPBITS
)
1711 clrbitmap(baseline
);
1713 for (fd
= 0; fd
< maxfd
; fd
++)
1717 if (fstat(fd
, &stbuf
) < 0 && errno
!= EOPNOTSUPP
)
1719 if (!bitnset(fd
, baseline
))
1721 clrbitn(fd
, baseline
);
1723 else if (!bitnset(fd
, baseline
))
1724 setbitn(fd
, baseline
);
1728 /* file state has changed */
1733 sm_syslog(LOG_DEBUG
, CurEnv
->e_id
,
1738 dumpfd(fd
, true, true);
1744 ** PRINTOPENFDS -- print the open file descriptors (for debugging)
1747 ** logit -- if set, send output to syslog; otherwise
1748 ** print for debugging.
1754 #if NETINET || NETINET6
1755 # include <arpa/inet.h>
1756 #endif /* NETINET || NETINET6 */
1763 extern int DtableSize
;
1765 for (fd
= 0; fd
< DtableSize
; fd
++)
1766 dumpfd(fd
, false, logit
);
1770 ** DUMPFD -- dump a file descriptor
1773 ** fd -- the file descriptor to dump.
1774 ** printclosed -- if set, print a notification even if
1775 ** it is closed; otherwise print nothing.
1776 ** logit -- if set, use sm_syslog instead of sm_dprintf()
1783 dumpfd(fd
, printclosed
, logit
)
1792 #endif /* S_IFSOCK */
1793 auto SOCKADDR_LEN_T slen
;
1797 #else /* STAT64 > 0 */
1799 #endif /* STAT64 > 0 */
1803 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "%3d: ", fd
);
1809 #else /* STAT64 > 0 */
1811 #endif /* STAT64 > 0 */
1816 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1818 sm_errstring(errno
));
1821 else if (printclosed
)
1823 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "CLOSED");
1829 i
= fcntl(fd
, F_GETFL
, 0);
1832 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "fl=0x%x, ", i
);
1836 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "mode=%o: ",
1839 switch (st
.st_mode
& S_IFMT
)
1843 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "SOCK ");
1845 memset(&sa
, '\0', sizeof(sa
));
1847 if (getsockname(fd
, &sa
.sa
, &slen
) < 0)
1848 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "(%s)",
1849 sm_errstring(errno
));
1852 hp
= hostnamebyanyaddr(&sa
);
1859 else if (sa
.sa
.sa_family
== AF_INET
)
1860 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1861 "%s/%d", hp
, ntohs(sa
.sin
.sin_port
));
1862 # endif /* NETINET */
1864 else if (sa
.sa
.sa_family
== AF_INET6
)
1865 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1866 "%s/%d", hp
, ntohs(sa
.sin6
.sin6_port
));
1867 # endif /* NETINET6 */
1869 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1873 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "->");
1876 if (getpeername(fd
, &sa
.sa
, &slen
) < 0)
1877 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "(%s)",
1878 sm_errstring(errno
));
1881 hp
= hostnamebyanyaddr(&sa
);
1888 else if (sa
.sa
.sa_family
== AF_INET
)
1889 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1890 "%s/%d", hp
, ntohs(sa
.sin
.sin_port
));
1891 # endif /* NETINET */
1893 else if (sa
.sa
.sa_family
== AF_INET6
)
1894 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1895 "%s/%d", hp
, ntohs(sa
.sin6
.sin6_port
));
1896 # endif /* NETINET6 */
1898 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1902 #endif /* S_IFSOCK */
1905 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "CHR: ");
1911 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "BLK: ");
1914 #endif /* S_IFBLK */
1916 #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
1918 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "FIFO: ");
1921 #endif /* defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) */
1925 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "DIR: ");
1928 #endif /* S_IFDIR */
1932 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "LNK: ");
1935 #endif /* S_IFLNK */
1939 (void) sm_snprintf(p
, SPACELEFT(buf
, p
),
1940 "dev=%d/%d, ino=%llu, nlink=%d, u/gid=%d/%d, ",
1941 major(st
.st_dev
), minor(st
.st_dev
),
1942 (ULONGLONG_T
) st
.st_ino
,
1943 (int) st
.st_nlink
, (int) st
.st_uid
,
1946 (void) sm_snprintf(p
, SPACELEFT(buf
, p
), "size=%llu",
1947 (ULONGLONG_T
) st
.st_size
);
1953 sm_syslog(LOG_DEBUG
, CurEnv
? CurEnv
->e_id
: NULL
,
1956 sm_dprintf("%s\n", buf
);
1960 ** SHORTEN_HOSTNAME -- strip local domain information off of hostname.
1963 ** host -- the host to shorten (stripped in place).
1966 ** place where string was truncated, NULL if not truncated.
1970 shorten_hostname(host
)
1978 /* strip off final dot */
1980 p
= &host
[(i
== 0) ? 0 : i
- 1];
1987 /* see if there is any domain at all -- if not, we are done */
1988 p
= strchr(host
, '.');
1992 /* yes, we have a domain -- see if it looks like us */
1993 mydom
= macvalue('m', CurEnv
);
1997 if ((canon
? sm_strcasecmp(p
, mydom
)
1998 : sm_strncasecmp(p
, mydom
, i
)) == 0 &&
1999 (mydom
[i
] == '.' || mydom
[i
] == '\0'))
2008 ** PROG_OPEN -- open a program for reading
2011 ** argv -- the argument list.
2012 ** pfd -- pointer to a place to store the file descriptor.
2013 ** e -- the current envelope.
2016 ** pid of the process -- -1 if it failed.
2020 prog_open(argv
, pfd
, e
)
2031 char buf
[MAXPATHLEN
];
2032 extern int DtableSize
;
2036 syserr("%s: cannot create pipe for stdout", argv
[0]);
2042 syserr("%s: cannot fork", argv
[0]);
2043 (void) close(fdv
[0]);
2044 (void) close(fdv
[1]);
2050 (void) close(fdv
[1]);
2055 /* Reset global flags */
2056 RestartRequest
= NULL
;
2057 RestartWorkGroup
= false;
2058 ShutdownRequest
= NULL
;
2060 CurrentPid
= getpid();
2063 ** Initialize exception stack and default exception
2064 ** handler for child process.
2067 sm_exc_newthread(fatal_error
);
2069 /* child -- close stdin */
2072 /* stdout goes back to parent */
2073 (void) close(fdv
[0]);
2074 if (dup2(fdv
[1], 1) < 0)
2076 syserr("%s: cannot dup2 for stdout", argv
[0]);
2079 (void) close(fdv
[1]);
2081 /* stderr goes to transcript if available */
2082 if (e
->e_xfp
!= NULL
)
2086 xfd
= sm_io_getinfo(e
->e_xfp
, SM_IO_WHAT_FD
, NULL
);
2087 if (xfd
>= 0 && dup2(xfd
, 2) < 0)
2089 syserr("%s: cannot dup2 for stderr", argv
[0]);
2094 /* this process has no right to the queue file */
2095 if (e
->e_lockfp
!= NULL
)
2099 fd
= sm_io_getinfo(e
->e_lockfp
, SM_IO_WHAT_FD
, NULL
);
2103 syserr("%s: lockfp does not have a fd", argv
[0]);
2106 /* chroot to the program mailer directory, if defined */
2107 if (ProgMailer
!= NULL
&& ProgMailer
->m_rootdir
!= NULL
)
2109 expand(ProgMailer
->m_rootdir
, buf
, sizeof(buf
), e
);
2110 if (chroot(buf
) < 0)
2112 syserr("prog_open: cannot chroot(%s)", buf
);
2117 syserr("prog_open: cannot chdir(/)");
2122 /* run as default user */
2124 sm_mbdb_terminate();
2126 (void) sm_memstat_close();
2127 #endif /* _FFR_MEMSTAT */
2128 if (setgid(DefGid
) < 0 && geteuid() == 0)
2130 syserr("prog_open: setgid(%ld) failed", (long) DefGid
);
2133 if (setuid(DefUid
) < 0 && geteuid() == 0)
2135 syserr("prog_open: setuid(%ld) failed", (long) DefUid
);
2139 /* run in some directory */
2140 if (ProgMailer
!= NULL
)
2141 p
= ProgMailer
->m_execdir
;
2144 for (; p
!= NULL
; p
= q
)
2149 expand(p
, buf
, sizeof(buf
), e
);
2152 if (buf
[0] != '\0' && chdir(buf
) >= 0)
2157 /* backup directories */
2158 if (chdir("/tmp") < 0)
2162 /* Check safety of program to be run */
2163 sff
= SFF_ROOTOK
|SFF_EXECOK
;
2164 if (!bitnset(DBS_RUNWRITABLEPROGRAM
, DontBlameSendmail
))
2165 sff
|= SFF_NOGWFILES
|SFF_NOWWFILES
;
2166 if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH
, DontBlameSendmail
))
2167 sff
|= SFF_NOPATHCHECK
;
2169 sff
|= SFF_SAFEDIRPATH
;
2170 ret
= safefile(argv
[0], DefUid
, DefGid
, DefUser
, sff
, 0, NULL
);
2172 sm_syslog(LOG_INFO
, e
->e_id
,
2173 "Warning: prog_open: program %s unsafe: %s",
2174 argv
[0], sm_errstring(ret
));
2176 /* arrange for all the files to be closed */
2177 sm_close_on_exec(STDERR_FILENO
+ 1, DtableSize
);
2179 /* now exec the process */
2180 (void) execve(argv
[0], (ARGV_T
) argv
, (ARGV_T
) UserEnviron
);
2184 syserr("%s: cannot exec", argv
[0]);
2185 if (transienterror(save_errno
))
2188 return -1; /* avoid compiler warning on IRIX */
2192 ** GET_COLUMN -- look up a Column in a line buffer
2195 ** line -- the raw text line to search.
2196 ** col -- the column number to fetch.
2197 ** delim -- the delimiter between columns. If null,
2199 ** buf -- the output buffer.
2200 ** buflen -- the length of buf.
2203 ** buf if successful.
2208 get_column(line
, col
, delim
, buf
, buflen
)
2220 if ((char) delim
== '\0')
2221 (void) sm_strlcpy(delimbuf
, "\n\t ", sizeof(delimbuf
));
2224 delimbuf
[0] = (char) delim
;
2230 return NULL
; /* line empty */
2231 if (*p
== (char) delim
&& col
== 0)
2232 return NULL
; /* first column empty */
2236 if (col
== 0 && (char) delim
== '\0')
2238 while (*begin
!= '\0' && isascii(*begin
) && isspace(*begin
))
2242 for (i
= 0; i
< col
; i
++)
2244 if ((begin
= strpbrk(begin
, delimbuf
)) == NULL
)
2245 return NULL
; /* no such column */
2247 if ((char) delim
== '\0')
2249 while (*begin
!= '\0' && isascii(*begin
) && isspace(*begin
))
2254 end
= strpbrk(begin
, delimbuf
);
2261 (void) sm_strlcpy(buf
, begin
, i
+ 1);
2266 ** CLEANSTRCPY -- copy string keeping out bogus characters
2269 ** t -- "to" string.
2270 ** f -- "from" string.
2271 ** l -- length of space available in "to" string.
2278 cleanstrcpy(t
, f
, l
)
2283 /* check for newlines and log if necessary */
2284 (void) denlstring(f
, true, true);
2287 syserr("!cleanstrcpy: length == 0");
2290 while (l
> 0 && *f
!= '\0')
2293 (isalnum(*f
) || strchr("!#$%&'*+-./^_`{|}~", *f
) != NULL
))
2304 ** DENLSTRING -- convert newlines in a string to spaces
2307 ** s -- the input string
2308 ** strict -- if set, don't permit continuation lines.
2309 ** logattacks -- if set, log attempted attacks.
2312 ** A pointer to a version of the string with newlines
2313 ** mapped to spaces. This should be copied.
2317 denlstring(s
, strict
, logattacks
)
2324 static char *bp
= NULL
;
2328 while ((p
= strchr(p
, '\n')) != NULL
)
2329 if (strict
|| (*++p
!= ' ' && *p
!= '\t'))
2337 /* allocate more space */
2338 char *nbp
= sm_pmalloc_x(l
);
2345 (void) sm_strlcpy(bp
, s
, l
);
2346 for (p
= bp
; (p
= strchr(p
, '\n')) != NULL
; )
2351 sm_syslog(LOG_NOTICE
, CurEnv
? CurEnv
->e_id
: NULL
,
2352 "POSSIBLE ATTACK from %.100s: newline in string \"%s\"",
2353 RealHostName
== NULL
? "[UNKNOWN]" : RealHostName
,
2354 shortenstring(bp
, MAXSHORTSTR
));
2361 ** STRREPLNONPRT -- replace "unprintable" characters in a string with subst
2364 ** s -- string to manipulate (in place)
2365 ** subst -- character to use as replacement
2368 ** true iff string did not contain "unprintable" characters
2383 if (!(isascii(*s
) && isprint(*s
)))
2394 ** PATH_IS_DIR -- check to see if file exists and is a directory.
2396 ** There are some additional checks for security violations in
2397 ** here. This routine is intended to be used for the host status
2401 ** pathname -- pathname to check for directory-ness.
2402 ** createflag -- if set, create directory if needed.
2405 ** true -- if the indicated pathname is a directory
2406 ** false -- otherwise
2410 path_is_dir(pathname
, createflag
)
2414 struct stat statbuf
;
2417 if (lstat(pathname
, &statbuf
) < 0)
2418 #else /* HASLSTAT */
2419 if (stat(pathname
, &statbuf
) < 0)
2420 #endif /* HASLSTAT */
2422 if (errno
!= ENOENT
|| !createflag
)
2424 if (mkdir(pathname
, 0755) < 0)
2428 if (!S_ISDIR(statbuf
.st_mode
))
2434 /* security: don't allow writable directories */
2435 if (bitset(S_IWGRP
|S_IWOTH
, statbuf
.st_mode
))
2444 ** PROC_LIST_ADD -- add process id to list of our children
2447 ** pid -- pid to add to list.
2448 ** task -- task of pid.
2449 ** type -- type of process.
2450 ** count -- number of processes.
2451 ** other -- other information for this type.
2457 ** May increase CurChildren. May grow ProcList.
2460 typedef struct procs PROCS_T
;
2469 SOCKADDR proc_hostaddr
;
2472 static PROCS_T
*volatile ProcListVec
= NULL
;
2473 static int ProcListSize
= 0;
2476 proc_list_add(pid
, task
, type
, count
, other
, hostaddr
)
2486 for (i
= 0; i
< ProcListSize
; i
++)
2488 if (ProcListVec
[i
].proc_pid
== NO_PID
)
2491 if (i
>= ProcListSize
)
2493 /* probe the existing vector to avoid growing infinitely */
2496 /* now scan again */
2497 for (i
= 0; i
< ProcListSize
; i
++)
2499 if (ProcListVec
[i
].proc_pid
== NO_PID
)
2503 if (i
>= ProcListSize
)
2505 /* grow process list */
2509 SM_ASSERT(ProcListSize
< INT_MAX
- PROC_LIST_SEG
);
2510 npv
= (PROCS_T
*) sm_pmalloc_x((sizeof(*npv
)) *
2511 (ProcListSize
+ PROC_LIST_SEG
));
2513 /* Block SIGCHLD so reapchild() doesn't mess with us */
2514 chldwasblocked
= sm_blocksignal(SIGCHLD
);
2515 if (ProcListSize
> 0)
2517 memmove(npv
, ProcListVec
,
2518 ProcListSize
* sizeof(PROCS_T
));
2519 sm_free(ProcListVec
);
2522 /* XXX just use memset() to initialize this part? */
2523 for (i
= ProcListSize
; i
< ProcListSize
+ PROC_LIST_SEG
; i
++)
2525 npv
[i
].proc_pid
= NO_PID
;
2526 npv
[i
].proc_task
= NULL
;
2527 npv
[i
].proc_type
= PROC_NONE
;
2530 ProcListSize
+= PROC_LIST_SEG
;
2532 if (chldwasblocked
== 0)
2533 (void) sm_releasesignal(SIGCHLD
);
2535 ProcListVec
[i
].proc_pid
= pid
;
2536 PSTRSET(ProcListVec
[i
].proc_task
, task
);
2537 ProcListVec
[i
].proc_type
= type
;
2538 ProcListVec
[i
].proc_count
= count
;
2539 ProcListVec
[i
].proc_other
= other
;
2540 if (hostaddr
!= NULL
)
2541 ProcListVec
[i
].proc_hostaddr
= *hostaddr
;
2543 memset(&ProcListVec
[i
].proc_hostaddr
, 0,
2544 sizeof(ProcListVec
[i
].proc_hostaddr
));
2546 /* if process adding itself, it's not a child */
2547 if (pid
!= CurrentPid
)
2549 SM_ASSERT(CurChildren
< INT_MAX
);
2555 ** PROC_LIST_SET -- set pid task in process list
2558 ** pid -- pid to set
2559 ** task -- task of pid
2566 proc_list_set(pid
, task
)
2572 for (i
= 0; i
< ProcListSize
; i
++)
2574 if (ProcListVec
[i
].proc_pid
== pid
)
2576 PSTRSET(ProcListVec
[i
].proc_task
, task
);
2583 ** PROC_LIST_DROP -- drop pid from process list
2586 ** pid -- pid to drop
2587 ** st -- process status
2588 ** other -- storage for proc_other (return).
2594 ** May decrease CurChildren, CurRunners, or
2595 ** set RestartRequest or ShutdownRequest.
2597 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2598 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2603 proc_list_drop(pid
, st
, other
)
2609 int type
= PROC_NONE
;
2611 for (i
= 0; i
< ProcListSize
; i
++)
2613 if (ProcListVec
[i
].proc_pid
== pid
)
2615 ProcListVec
[i
].proc_pid
= NO_PID
;
2616 type
= ProcListVec
[i
].proc_type
;
2618 *other
= ProcListVec
[i
].proc_other
;
2619 if (CurChildren
> 0)
2626 if (type
== PROC_CONTROL
&& WIFEXITED(st
))
2628 /* if so, see if we need to restart or shutdown */
2629 if (WEXITSTATUS(st
) == EX_RESTART
)
2630 RestartRequest
= "control socket";
2631 else if (WEXITSTATUS(st
) == EX_SHUTDOWN
)
2632 ShutdownRequest
= "control socket";
2634 else if (type
== PROC_QUEUE_CHILD
&& !WIFSTOPPED(st
) &&
2635 ProcListVec
[i
].proc_other
> -1)
2637 /* restart this persistent runner */
2638 mark_work_group_restart(ProcListVec
[i
].proc_other
, st
);
2640 else if (type
== PROC_QUEUE
)
2641 CurRunners
-= ProcListVec
[i
].proc_count
;
2645 ** PROC_LIST_CLEAR -- clear the process list
2654 ** Sets CurChildren to zero.
2662 /* start from 1 since 0 is the daemon itself */
2663 for (i
= 1; i
< ProcListSize
; i
++)
2664 ProcListVec
[i
].proc_pid
= NO_PID
;
2669 ** PROC_LIST_PROBE -- probe processes in the list to see if they still exist
2678 ** May decrease CurChildren.
2689 chldwasblocked
= sm_blocksignal(SIGCHLD
);
2691 /* start from 1 since 0 is the daemon itself */
2692 for (i
= 1; i
< ProcListSize
; i
++)
2694 pid
= ProcListVec
[i
].proc_pid
;
2695 if (pid
== NO_PID
|| pid
== CurrentPid
)
2697 if (kill(pid
, 0) < 0)
2700 sm_syslog(LOG_DEBUG
, CurEnv
->e_id
,
2701 "proc_list_probe: lost pid %d",
2702 (int) ProcListVec
[i
].proc_pid
);
2703 ProcListVec
[i
].proc_pid
= NO_PID
;
2704 SM_FREE_CLR(ProcListVec
[i
].proc_task
);
2712 if (CurChildren
< 0)
2714 if (chldwasblocked
== 0)
2715 (void) sm_releasesignal(SIGCHLD
);
2716 if (LogLevel
> 10 && children
!= CurChildren
&& CurrentPid
== DaemonPid
)
2718 sm_syslog(LOG_ERR
, NOQID
,
2719 "proc_list_probe: found %d children, expected %d",
2720 children
, CurChildren
);
2725 ** PROC_LIST_DISPLAY -- display the process list
2728 ** out -- output file pointer
2729 ** prefix -- string to output in front of each line.
2736 proc_list_display(out
, prefix
)
2742 for (i
= 0; i
< ProcListSize
; i
++)
2744 if (ProcListVec
[i
].proc_pid
== NO_PID
)
2747 (void) sm_io_fprintf(out
, SM_TIME_DEFAULT
, "%s%d %s%s\n",
2749 (int) ProcListVec
[i
].proc_pid
,
2750 ProcListVec
[i
].proc_task
!= NULL
?
2751 ProcListVec
[i
].proc_task
: "(unknown)",
2752 (OpMode
== MD_SMTP
||
2753 OpMode
== MD_DAEMON
||
2754 OpMode
== MD_ARPAFTP
) ? "\r" : "");
2759 ** PROC_LIST_SIGNAL -- send a signal to a type of process in the list
2762 ** type -- type of process to signal
2763 ** signal -- the type of signal to send
2768 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2769 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2774 proc_list_signal(type
, signal
)
2781 pid_t mypid
= getpid();
2783 /* block these signals so that we may signal cleanly */
2784 chldwasblocked
= sm_blocksignal(SIGCHLD
);
2785 alrmwasblocked
= sm_blocksignal(SIGALRM
);
2787 /* Find all processes of type and send signal */
2788 for (i
= 0; i
< ProcListSize
; i
++)
2790 if (ProcListVec
[i
].proc_pid
== NO_PID
||
2791 ProcListVec
[i
].proc_pid
== mypid
)
2793 if (ProcListVec
[i
].proc_type
!= type
)
2795 (void) kill(ProcListVec
[i
].proc_pid
, signal
);
2798 /* restore the signals */
2799 if (alrmwasblocked
== 0)
2800 (void) sm_releasesignal(SIGALRM
);
2801 if (chldwasblocked
== 0)
2802 (void) sm_releasesignal(SIGCHLD
);
2806 ** COUNT_OPEN_CONNECTIONS
2809 ** hostaddr - ClientAddress
2812 ** the number of open connections for this client
2817 count_open_connections(hostaddr
)
2822 if (hostaddr
== NULL
)
2826 ** Initialize to 1 instead of 0 because this code gets called
2827 ** before proc_list_add() gets called, so we (the daemon child
2828 ** for this connection) don't count ourselves.
2832 for (i
= 0; i
< ProcListSize
; i
++)
2834 if (ProcListVec
[i
].proc_pid
== NO_PID
)
2836 if (hostaddr
->sa
.sa_family
!=
2837 ProcListVec
[i
].proc_hostaddr
.sa
.sa_family
)
2840 if (hostaddr
->sa
.sa_family
== AF_INET
&&
2841 (hostaddr
->sin
.sin_addr
.s_addr
==
2842 ProcListVec
[i
].proc_hostaddr
.sin
.sin_addr
.s_addr
))
2844 #endif /* NETINET */
2846 if (hostaddr
->sa
.sa_family
== AF_INET6
&&
2847 IN6_ARE_ADDR_EQUAL(&(hostaddr
->sin6
.sin6_addr
),
2848 &(ProcListVec
[i
].proc_hostaddr
.sin6
.sin6_addr
)))
2850 #endif /* NETINET6 */