1N/A/*
1N/A * Copyright (c) 1998-2007, 2009 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
1N/A * Copyright (c) 1988, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#include <sendmail.h>
1N/A
1N/ASM_RCSID("@(#)$Id: util.c,v 8.416 2009/12/18 17:05:26 ca Exp $")
1N/A
1N/A#include <sm/sendmail.h>
1N/A#include <sysexits.h>
1N/A#include <sm/xtrap.h>
1N/A
1N/A/*
1N/A** NEWSTR -- Create a copy of a C string
1N/A**
1N/A** Parameters:
1N/A** s -- the string to copy.
1N/A**
1N/A** Returns:
1N/A** pointer to newly allocated string.
1N/A*/
1N/A
1N/Achar *
1N/Anewstr(s)
1N/A const char *s;
1N/A{
1N/A size_t l;
1N/A char *n;
1N/A
1N/A l = strlen(s);
1N/A SM_ASSERT(l + 1 > l);
1N/A n = xalloc(l + 1);
1N/A sm_strlcpy(n, s, l + 1);
1N/A return n;
1N/A}
1N/A
1N/A/*
1N/A** ADDQUOTES -- Adds quotes & quote bits to a string.
1N/A**
1N/A** Runs through a string and adds backslashes and quote bits.
1N/A**
1N/A** Parameters:
1N/A** s -- the string to modify.
1N/A** rpool -- resource pool from which to allocate result
1N/A**
1N/A** Returns:
1N/A** pointer to quoted string.
1N/A*/
1N/A
1N/Achar *
1N/Aaddquotes(s, rpool)
1N/A char *s;
1N/A SM_RPOOL_T *rpool;
1N/A{
1N/A int len = 0;
1N/A char c;
1N/A char *p = s, *q, *r;
1N/A
1N/A if (s == NULL)
1N/A return NULL;
1N/A
1N/A /* Find length of quoted string */
1N/A while ((c = *p++) != '\0')
1N/A {
1N/A len++;
1N/A if (c == '\\' || c == '"')
1N/A len++;
1N/A }
1N/A
1N/A q = r = sm_rpool_malloc_x(rpool, len + 3);
1N/A p = s;
1N/A
1N/A /* add leading quote */
1N/A *q++ = '"';
1N/A while ((c = *p++) != '\0')
1N/A {
1N/A /* quote \ or " */
1N/A if (c == '\\' || c == '"')
1N/A *q++ = '\\';
1N/A *q++ = c;
1N/A }
1N/A *q++ = '"';
1N/A *q = '\0';
1N/A return r;
1N/A}
1N/A
1N/A/*
1N/A** STRIPBACKSLASH -- Strip all leading backslashes from a string, provided
1N/A** the following character is alpha-numerical.
1N/A**
1N/A** This is done in place.
1N/A**
1N/A** Parameters:
1N/A** s -- the string to strip.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Astripbackslash(s)
1N/A char *s;
1N/A{
1N/A char *p, *q, c;
1N/A
1N/A if (s == NULL || *s == '\0')
1N/A return;
1N/A p = q = s;
1N/A while (*p == '\\' && (p[1] == '\\' || (isascii(p[1]) && isalnum(p[1]))))
1N/A p++;
1N/A do
1N/A {
1N/A c = *q++ = *p++;
1N/A } while (c != '\0');
1N/A}
1N/A
1N/A/*
1N/A** RFC822_STRING -- Checks string for proper RFC822 string quoting.
1N/A**
1N/A** Runs through a string and verifies RFC822 special characters
1N/A** are only found inside comments, quoted strings, or backslash
1N/A** escaped. Also verified balanced quotes and parenthesis.
1N/A**
1N/A** Parameters:
1N/A** s -- the string to modify.
1N/A**
1N/A** Returns:
1N/A** true iff the string is RFC822 compliant, false otherwise.
1N/A*/
1N/A
1N/Abool
1N/Arfc822_string(s)
1N/A char *s;
1N/A{
1N/A bool quoted = false;
1N/A int commentlev = 0;
1N/A char *c = s;
1N/A
1N/A if (s == NULL)
1N/A return false;
1N/A
1N/A while (*c != '\0')
1N/A {
1N/A /* escaped character */
1N/A if (*c == '\\')
1N/A {
1N/A c++;
1N/A if (*c == '\0')
1N/A return false;
1N/A }
1N/A else if (commentlev == 0 && *c == '"')
1N/A quoted = !quoted;
1N/A else if (!quoted)
1N/A {
1N/A if (*c == ')')
1N/A {
1N/A /* unbalanced ')' */
1N/A if (commentlev == 0)
1N/A return false;
1N/A else
1N/A commentlev--;
1N/A }
1N/A else if (*c == '(')
1N/A commentlev++;
1N/A else if (commentlev == 0 &&
1N/A strchr(MustQuoteChars, *c) != NULL)
1N/A return false;
1N/A }
1N/A c++;
1N/A }
1N/A
1N/A /* unbalanced '"' or '(' */
1N/A return !quoted && commentlev == 0;
1N/A}
1N/A
1N/A/*
1N/A** SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string
1N/A**
1N/A** Arbitrarily shorten (in place) an RFC822 string and rebalance
1N/A** comments and quotes.
1N/A**
1N/A** Parameters:
1N/A** string -- the string to shorten
1N/A** length -- the maximum size, 0 if no maximum
1N/A**
1N/A** Returns:
1N/A** true if string is changed, false otherwise
1N/A**
1N/A** Side Effects:
1N/A** Changes string in place, possibly resulting
1N/A** in a shorter string.
1N/A*/
1N/A
1N/Abool
1N/Ashorten_rfc822_string(string, length)
1N/A char *string;
1N/A size_t length;
1N/A{
1N/A bool backslash = false;
1N/A bool modified = false;
1N/A bool quoted = false;
1N/A size_t slen;
1N/A int parencount = 0;
1N/A char *ptr = string;
1N/A
1N/A /*
1N/A ** If have to rebalance an already short enough string,
1N/A ** need to do it within allocated space.
1N/A */
1N/A
1N/A slen = strlen(string);
1N/A if (length == 0 || slen < length)
1N/A length = slen;
1N/A
1N/A while (*ptr != '\0')
1N/A {
1N/A if (backslash)
1N/A {
1N/A backslash = false;
1N/A goto increment;
1N/A }
1N/A
1N/A if (*ptr == '\\')
1N/A backslash = true;
1N/A else if (*ptr == '(')
1N/A {
1N/A if (!quoted)
1N/A parencount++;
1N/A }
1N/A else if (*ptr == ')')
1N/A {
1N/A if (--parencount < 0)
1N/A parencount = 0;
1N/A }
1N/A
1N/A /* Inside a comment, quotes don't matter */
1N/A if (parencount <= 0 && *ptr == '"')
1N/A quoted = !quoted;
1N/A
1N/Aincrement:
1N/A /* Check for sufficient space for next character */
1N/A if (length - (ptr - string) <= (size_t) ((backslash ? 1 : 0) +
1N/A parencount +
1N/A (quoted ? 1 : 0)))
1N/A {
1N/A /* Not enough, backtrack */
1N/A if (*ptr == '\\')
1N/A backslash = false;
1N/A else if (*ptr == '(' && !quoted)
1N/A parencount--;
1N/A else if (*ptr == '"' && parencount == 0)
1N/A quoted = false;
1N/A break;
1N/A }
1N/A ptr++;
1N/A }
1N/A
1N/A /* Rebalance */
1N/A while (parencount-- > 0)
1N/A {
1N/A if (*ptr != ')')
1N/A {
1N/A modified = true;
1N/A *ptr = ')';
1N/A }
1N/A ptr++;
1N/A }
1N/A if (quoted)
1N/A {
1N/A if (*ptr != '"')
1N/A {
1N/A modified = true;
1N/A *ptr = '"';
1N/A }
1N/A ptr++;
1N/A }
1N/A if (*ptr != '\0')
1N/A {
1N/A modified = true;
1N/A *ptr = '\0';
1N/A }
1N/A return modified;
1N/A}
1N/A
1N/A/*
1N/A** FIND_CHARACTER -- find an unquoted character in an RFC822 string
1N/A**
1N/A** Find an unquoted, non-commented character in an RFC822
1N/A** string and return a pointer to its location in the
1N/A** string.
1N/A**
1N/A** Parameters:
1N/A** string -- the string to search
1N/A** character -- the character to find
1N/A**
1N/A** Returns:
1N/A** pointer to the character, or
1N/A** a pointer to the end of the line if character is not found
1N/A*/
1N/A
1N/Achar *
1N/Afind_character(string, character)
1N/A char *string;
1N/A int character;
1N/A{
1N/A bool backslash = false;
1N/A bool quoted = false;
1N/A int parencount = 0;
1N/A
1N/A while (string != NULL && *string != '\0')
1N/A {
1N/A if (backslash)
1N/A {
1N/A backslash = false;
1N/A if (!quoted && character == '\\' && *string == '\\')
1N/A break;
1N/A string++;
1N/A continue;
1N/A }
1N/A switch (*string)
1N/A {
1N/A case '\\':
1N/A backslash = true;
1N/A break;
1N/A
1N/A case '(':
1N/A if (!quoted)
1N/A parencount++;
1N/A break;
1N/A
1N/A case ')':
1N/A if (--parencount < 0)
1N/A parencount = 0;
1N/A break;
1N/A }
1N/A
1N/A /* Inside a comment, nothing matters */
1N/A if (parencount > 0)
1N/A {
1N/A string++;
1N/A continue;
1N/A }
1N/A
1N/A if (*string == '"')
1N/A quoted = !quoted;
1N/A else if (*string == character && !quoted)
1N/A break;
1N/A string++;
1N/A }
1N/A
1N/A /* Return pointer to the character */
1N/A return string;
1N/A}
1N/A
1N/A/*
1N/A** CHECK_BODYTYPE -- check bodytype parameter
1N/A**
1N/A** Parameters:
1N/A** bodytype -- bodytype parameter
1N/A**
1N/A** Returns:
1N/A** BODYTYPE_* according to parameter
1N/A**
1N/A*/
1N/A
1N/Aint
1N/Acheck_bodytype(bodytype)
1N/A char *bodytype;
1N/A{
1N/A /* check body type for legality */
1N/A if (bodytype == NULL)
1N/A return BODYTYPE_NONE;
1N/A if (sm_strcasecmp(bodytype, "7BIT") == 0)
1N/A return BODYTYPE_7BIT;
1N/A if (sm_strcasecmp(bodytype, "8BITMIME") == 0)
1N/A return BODYTYPE_8BITMIME;
1N/A return BODYTYPE_ILLEGAL;
1N/A}
1N/A
1N/A/*
1N/A** TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..."
1N/A**
1N/A** Parameters:
1N/A** str -- string to truncate
1N/A** len -- maximum length (including '\0') (0 for unlimited)
1N/A** delim -- delimiter character
1N/A**
1N/A** Returns:
1N/A** None.
1N/A*/
1N/A
1N/Avoid
1N/Atruncate_at_delim(str, len, delim)
1N/A char *str;
1N/A size_t len;
1N/A int delim;
1N/A{
1N/A char *p;
1N/A
1N/A if (str == NULL || len == 0 || strlen(str) < len)
1N/A return;
1N/A
1N/A *(str + len - 1) = '\0';
1N/A while ((p = strrchr(str, delim)) != NULL)
1N/A {
1N/A *p = '\0';
1N/A if (p - str + 4 < len)
1N/A {
1N/A *p++ = (char) delim;
1N/A *p = '\0';
1N/A (void) sm_strlcat(str, "...", len);
1N/A return;
1N/A }
1N/A }
1N/A
1N/A /* Couldn't find a place to append "..." */
1N/A if (len > 3)
1N/A (void) sm_strlcpy(str, "...", len);
1N/A else
1N/A str[0] = '\0';
1N/A}
1N/A
1N/A/*
1N/A** XALLOC -- Allocate memory, raise an exception on error
1N/A**
1N/A** Parameters:
1N/A** sz -- size of area to allocate.
1N/A**
1N/A** Returns:
1N/A** pointer to data region.
1N/A**
1N/A** Exceptions:
1N/A** SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory
1N/A**
1N/A** Side Effects:
1N/A** Memory is allocated.
1N/A*/
1N/A
1N/Achar *
1N/A#if SM_HEAP_CHECK
1N/Axalloc_tagged(sz, file, line)
1N/A register int sz;
1N/A char *file;
1N/A int line;
1N/A#else /* SM_HEAP_CHECK */
1N/Axalloc(sz)
1N/A register int sz;
1N/A#endif /* SM_HEAP_CHECK */
1N/A{
1N/A register char *p;
1N/A
1N/A SM_REQUIRE(sz >= 0);
1N/A
1N/A /* some systems can't handle size zero mallocs */
1N/A if (sz <= 0)
1N/A sz = 1;
1N/A
1N/A /* scaffolding for testing error handling code */
1N/A sm_xtrap_raise_x(&SmHeapOutOfMemory);
1N/A
1N/A p = sm_malloc_tagged((unsigned) sz, file, line, sm_heap_group());
1N/A if (p == NULL)
1N/A {
1N/A sm_exc_raise_x(&SmHeapOutOfMemory);
1N/A }
1N/A return p;
1N/A}
1N/A
1N/A/*
1N/A** COPYPLIST -- copy list of pointers.
1N/A**
1N/A** This routine is the equivalent of strdup for lists of
1N/A** pointers.
1N/A**
1N/A** Parameters:
1N/A** list -- list of pointers to copy.
1N/A** Must be NULL terminated.
1N/A** copycont -- if true, copy the contents of the vector
1N/A** (which must be a string) also.
1N/A** rpool -- resource pool from which to allocate storage,
1N/A** or NULL
1N/A**
1N/A** Returns:
1N/A** a copy of 'list'.
1N/A*/
1N/A
1N/Achar **
1N/Acopyplist(list, copycont, rpool)
1N/A char **list;
1N/A bool copycont;
1N/A SM_RPOOL_T *rpool;
1N/A{
1N/A register char **vp;
1N/A register char **newvp;
1N/A
1N/A for (vp = list; *vp != NULL; vp++)
1N/A continue;
1N/A
1N/A vp++;
1N/A
1N/A newvp = (char **) sm_rpool_malloc_x(rpool, (vp - list) * sizeof(*vp));
1N/A memmove((char *) newvp, (char *) list, (int) (vp - list) * sizeof(*vp));
1N/A
1N/A if (copycont)
1N/A {
1N/A for (vp = newvp; *vp != NULL; vp++)
1N/A *vp = sm_rpool_strdup_x(rpool, *vp);
1N/A }
1N/A
1N/A return newvp;
1N/A}
1N/A
1N/A/*
1N/A** COPYQUEUE -- copy address queue.
1N/A**
1N/A** This routine is the equivalent of strdup for address queues;
1N/A** addresses marked as QS_IS_DEAD() aren't copied
1N/A**
1N/A** Parameters:
1N/A** addr -- list of address structures to copy.
1N/A** rpool -- resource pool from which to allocate storage
1N/A**
1N/A** Returns:
1N/A** a copy of 'addr'.
1N/A*/
1N/A
1N/AADDRESS *
1N/Acopyqueue(addr, rpool)
1N/A ADDRESS *addr;
1N/A SM_RPOOL_T *rpool;
1N/A{
1N/A register ADDRESS *newaddr;
1N/A ADDRESS *ret;
1N/A register ADDRESS **tail = &ret;
1N/A
1N/A while (addr != NULL)
1N/A {
1N/A if (!QS_IS_DEAD(addr->q_state))
1N/A {
1N/A newaddr = (ADDRESS *) sm_rpool_malloc_x(rpool,
1N/A sizeof(*newaddr));
1N/A STRUCTCOPY(*addr, *newaddr);
1N/A *tail = newaddr;
1N/A tail = &newaddr->q_next;
1N/A }
1N/A addr = addr->q_next;
1N/A }
1N/A *tail = NULL;
1N/A
1N/A return ret;
1N/A}
1N/A
1N/A/*
1N/A** LOG_SENDMAIL_PID -- record sendmail pid and command line.
1N/A**
1N/A** Parameters:
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** writes pidfile, logs command line.
1N/A** keeps file open and locked to prevent overwrite of active file
1N/A*/
1N/A
1N/Astatic SM_FILE_T *Pidf = NULL;
1N/A
1N/Avoid
1N/Alog_sendmail_pid(e)
1N/A ENVELOPE *e;
1N/A{
1N/A long sff;
1N/A char pidpath[MAXPATHLEN];
1N/A extern char *CommandLineArgs;
1N/A
1N/A /* write the pid to the log file for posterity */
1N/A sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK;
1N/A if (TrustedUid != 0 && RealUid == TrustedUid)
1N/A sff |= SFF_OPENASROOT;
1N/A expand(PidFile, pidpath, sizeof(pidpath), e);
1N/A Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff);
1N/A if (Pidf == NULL)
1N/A {
1N/A if (errno == EWOULDBLOCK)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "unable to write pid to %s: file in use by another process",
1N/A pidpath);
1N/A else
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "unable to write pid to %s: %s",
1N/A pidpath, sm_errstring(errno));
1N/A }
1N/A else
1N/A {
1N/A PidFilePid = getpid();
1N/A
1N/A /* write the process id on line 1 */
1N/A (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n",
1N/A (long) PidFilePid);
1N/A
1N/A /* line 2 contains all command line flags */
1N/A (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n",
1N/A CommandLineArgs);
1N/A
1N/A /* flush */
1N/A (void) sm_io_flush(Pidf, SM_TIME_DEFAULT);
1N/A
1N/A /*
1N/A ** Leave pid file open until process ends
1N/A ** so it's not overwritten by another
1N/A ** process.
1N/A */
1N/A }
1N/A if (LogLevel > 9)
1N/A sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs);
1N/A}
1N/A
1N/A/*
1N/A** CLOSE_SENDMAIL_PID -- close sendmail pid file
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Aclose_sendmail_pid()
1N/A{
1N/A if (Pidf == NULL)
1N/A return;
1N/A
1N/A (void) sm_io_close(Pidf, SM_TIME_DEFAULT);
1N/A Pidf = NULL;
1N/A}
1N/A
1N/A/*
1N/A** SET_DELIVERY_MODE -- set and record the delivery mode
1N/A**
1N/A** Parameters:
1N/A** mode -- delivery mode
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** sets {deliveryMode} macro
1N/A*/
1N/A
1N/Avoid
1N/Aset_delivery_mode(mode, e)
1N/A int mode;
1N/A ENVELOPE *e;
1N/A{
1N/A char buf[2];
1N/A
1N/A e->e_sendmode = (char) mode;
1N/A buf[0] = (char) mode;
1N/A buf[1] = '\0';
1N/A macdefine(&e->e_macro, A_TEMP, macid("{deliveryMode}"), buf);
1N/A}
1N/A
1N/A/*
1N/A** SET_OP_MODE -- set and record the op mode
1N/A**
1N/A** Parameters:
1N/A** mode -- op mode
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** sets {opMode} macro
1N/A*/
1N/A
1N/Avoid
1N/Aset_op_mode(mode)
1N/A int mode;
1N/A{
1N/A char buf[2];
1N/A extern ENVELOPE BlankEnvelope;
1N/A
1N/A OpMode = (char) mode;
1N/A buf[0] = (char) mode;
1N/A buf[1] = '\0';
1N/A macdefine(&BlankEnvelope.e_macro, A_TEMP, MID_OPMODE, buf);
1N/A}
1N/A
1N/A/*
1N/A** PRINTAV -- print argument vector.
1N/A**
1N/A** Parameters:
1N/A** fp -- output file pointer.
1N/A** av -- argument vector.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** prints av.
1N/A*/
1N/A
1N/Avoid
1N/Aprintav(fp, av)
1N/A SM_FILE_T *fp;
1N/A char **av;
1N/A{
1N/A while (*av != NULL)
1N/A {
1N/A if (tTd(0, 44))
1N/A sm_dprintf("\n\t%08lx=", (unsigned long) *av);
1N/A else
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, ' ');
1N/A if (tTd(0, 99))
1N/A sm_dprintf("%s", str2prt(*av++));
1N/A else
1N/A xputs(fp, *av++);
1N/A }
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\n');
1N/A}
1N/A
1N/A/*
1N/A** XPUTS -- put string doing control escapes.
1N/A**
1N/A** Parameters:
1N/A** fp -- output file pointer.
1N/A** s -- string to put.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** output to stdout
1N/A*/
1N/A
1N/Avoid
1N/Axputs(fp, s)
1N/A SM_FILE_T *fp;
1N/A const char *s;
1N/A{
1N/A int c;
1N/A struct metamac *mp;
1N/A bool shiftout = false;
1N/A extern struct metamac MetaMacros[];
1N/A static SM_DEBUG_T DebugANSI = SM_DEBUG_INITIALIZER("ANSI",
1N/A "@(#)$Debug: ANSI - enable reverse video in debug output $");
1N/A
1N/A /*
1N/A ** TermEscape is set here, rather than in main(),
1N/A ** because ANSI mode can be turned on or off at any time
1N/A ** if we are in -bt rule testing mode.
1N/A */
1N/A
1N/A if (sm_debug_unknown(&DebugANSI))
1N/A {
1N/A if (sm_debug_active(&DebugANSI, 1))
1N/A {
1N/A TermEscape.te_rv_on = "\033[7m";
1N/A TermEscape.te_normal = "\033[0m";
1N/A }
1N/A else
1N/A {
1N/A TermEscape.te_rv_on = "";
1N/A TermEscape.te_normal = "";
1N/A }
1N/A }
1N/A
1N/A if (s == NULL)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s<null>%s",
1N/A TermEscape.te_rv_on, TermEscape.te_normal);
1N/A return;
1N/A }
1N/A while ((c = (*s++ & 0377)) != '\0')
1N/A {
1N/A if (shiftout)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
1N/A TermEscape.te_normal);
1N/A shiftout = false;
1N/A }
1N/A if (!isascii(c) && !tTd(84, 1))
1N/A {
1N/A if (c == MATCHREPL)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "%s$",
1N/A TermEscape.te_rv_on);
1N/A shiftout = true;
1N/A if (*s == '\0')
1N/A continue;
1N/A c = *s++ & 0377;
1N/A goto printchar;
1N/A }
1N/A if (c == MACROEXPAND || c == MACRODEXPAND)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "%s$",
1N/A TermEscape.te_rv_on);
1N/A if (c == MACRODEXPAND)
1N/A (void) sm_io_putc(fp,
1N/A SM_TIME_DEFAULT, '&');
1N/A shiftout = true;
1N/A if (*s == '\0')
1N/A continue;
1N/A if (strchr("=~&?", *s) != NULL)
1N/A (void) sm_io_putc(fp,
1N/A SM_TIME_DEFAULT,
1N/A *s++);
1N/A if (bitset(0200, *s))
1N/A (void) sm_io_fprintf(fp,
1N/A SM_TIME_DEFAULT,
1N/A "{%s}",
1N/A macname(bitidx(*s++)));
1N/A else
1N/A (void) sm_io_fprintf(fp,
1N/A SM_TIME_DEFAULT,
1N/A "%c",
1N/A *s++);
1N/A continue;
1N/A }
1N/A for (mp = MetaMacros; mp->metaname != '\0'; mp++)
1N/A {
1N/A if (bitidx(mp->metaval) == c)
1N/A {
1N/A (void) sm_io_fprintf(fp,
1N/A SM_TIME_DEFAULT,
1N/A "%s$%c",
1N/A TermEscape.te_rv_on,
1N/A mp->metaname);
1N/A shiftout = true;
1N/A break;
1N/A }
1N/A }
1N/A if (c == MATCHCLASS || c == MATCHNCLASS)
1N/A {
1N/A if (bitset(0200, *s))
1N/A (void) sm_io_fprintf(fp,
1N/A SM_TIME_DEFAULT,
1N/A "{%s}",
1N/A macname(bitidx(*s++)));
1N/A else if (*s != '\0')
1N/A (void) sm_io_fprintf(fp,
1N/A SM_TIME_DEFAULT,
1N/A "%c",
1N/A *s++);
1N/A }
1N/A if (mp->metaname != '\0')
1N/A continue;
1N/A
1N/A /* unrecognized meta character */
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%sM-",
1N/A TermEscape.te_rv_on);
1N/A shiftout = true;
1N/A c &= 0177;
1N/A }
1N/A printchar:
1N/A if (isascii(c) && isprint(c))
1N/A {
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
1N/A continue;
1N/A }
1N/A
1N/A /* wasn't a meta-macro -- find another way to print it */
1N/A switch (c)
1N/A {
1N/A case '\n':
1N/A c = 'n';
1N/A break;
1N/A
1N/A case '\r':
1N/A c = 'r';
1N/A break;
1N/A
1N/A case '\t':
1N/A c = 't';
1N/A break;
1N/A }
1N/A if (!shiftout)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
1N/A TermEscape.te_rv_on);
1N/A shiftout = true;
1N/A }
1N/A if (isascii(c) && isprint(c))
1N/A {
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\\');
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, c);
1N/A }
1N/A else if (tTd(84, 2))
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %o ", c);
1N/A else if (tTd(84, 1))
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %#x ", c);
1N/A else if (!isascii(c) && !tTd(84, 1))
1N/A {
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, '^');
1N/A (void) sm_io_putc(fp, SM_TIME_DEFAULT, c ^ 0100);
1N/A }
1N/A }
1N/A if (shiftout)
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
1N/A TermEscape.te_normal);
1N/A (void) sm_io_flush(fp, SM_TIME_DEFAULT);
1N/A}
1N/A
1N/A/*
1N/A** MAKELOWER -- Translate a line into lower case
1N/A**
1N/A** Parameters:
1N/A** p -- the string to translate. If NULL, return is
1N/A** immediate.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** String pointed to by p is translated to lower case.
1N/A*/
1N/A
1N/Avoid
1N/Amakelower(p)
1N/A register char *p;
1N/A{
1N/A register char c;
1N/A
1N/A if (p == NULL)
1N/A return;
1N/A for (; (c = *p) != '\0'; p++)
1N/A if (isascii(c) && isupper(c))
1N/A *p = tolower(c);
1N/A}
1N/A
1N/A/*
1N/A** FIXCRLF -- fix <CR><LF> in line.
1N/A**
1N/A** Looks for the <CR><LF> combination and turns it into the
1N/A** UNIX canonical <NL> character. It only takes one line,
1N/A** i.e., it is assumed that the first <NL> found is the end
1N/A** of the line.
1N/A**
1N/A** Parameters:
1N/A** line -- the line to fix.
1N/A** stripnl -- if true, strip the newline also.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** line is changed in place.
1N/A*/
1N/A
1N/Avoid
1N/Afixcrlf(line, stripnl)
1N/A char *line;
1N/A bool stripnl;
1N/A{
1N/A register char *p;
1N/A
1N/A p = strchr(line, '\n');
1N/A if (p == NULL)
1N/A return;
1N/A if (p > line && p[-1] == '\r')
1N/A p--;
1N/A if (!stripnl)
1N/A *p++ = '\n';
1N/A *p = '\0';
1N/A}
1N/A
1N/A/*
1N/A** PUTLINE -- put a line like fputs obeying SMTP conventions
1N/A**
1N/A** This routine always guarantees outputing a newline (or CRLF,
1N/A** as appropriate) at the end of the string.
1N/A**
1N/A** Parameters:
1N/A** l -- line to put.
1N/A** mci -- the mailer connection information.
1N/A**
1N/A** Returns:
1N/A** true iff line was written successfully
1N/A**
1N/A** Side Effects:
1N/A** output of l to mci->mci_out.
1N/A*/
1N/A
1N/Abool
1N/Aputline(l, mci)
1N/A register char *l;
1N/A register MCI *mci;
1N/A{
1N/A return putxline(l, strlen(l), mci, PXLF_MAPFROM);
1N/A}
1N/A
1N/A/*
1N/A** PUTXLINE -- putline with flags bits.
1N/A**
1N/A** This routine always guarantees outputing a newline (or CRLF,
1N/A** as appropriate) at the end of the string.
1N/A**
1N/A** Parameters:
1N/A** l -- line to put.
1N/A** len -- the length of the line.
1N/A** mci -- the mailer connection information.
1N/A** pxflags -- flag bits:
1N/A** PXLF_MAPFROM -- map From_ to >From_.
1N/A** PXLF_STRIP8BIT -- strip 8th bit.
1N/A** PXLF_HEADER -- map bare newline in header to newline space.
1N/A** PXLF_NOADDEOL -- don't add an EOL if one wasn't present.
1N/A** PXLF_STRIPMQUOTE -- strip METAQUOTE bytes.
1N/A**
1N/A** Returns:
1N/A** true iff line was written successfully
1N/A**
1N/A** Side Effects:
1N/A** output of l to mci->mci_out.
1N/A*/
1N/A
1N/A
1N/A#define PUTX(limit) \
1N/A do \
1N/A { \
1N/A quotenext = false; \
1N/A while (l < limit) \
1N/A { \
1N/A unsigned char c = (unsigned char) *l++; \
1N/A \
1N/A if (bitset(PXLF_STRIPMQUOTE, pxflags) && \
1N/A !quotenext && c == METAQUOTE) \
1N/A { \
1N/A quotenext = true; \
1N/A continue; \
1N/A } \
1N/A quotenext = false; \
1N/A if (strip8bit) \
1N/A c &= 0177; \
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, \
1N/A c) == SM_IO_EOF) \
1N/A { \
1N/A dead = true; \
1N/A break; \
1N/A } \
1N/A if (TrafficLogFile != NULL) \
1N/A (void) sm_io_putc(TrafficLogFile, \
1N/A SM_TIME_DEFAULT, \
1N/A c); \
1N/A } \
1N/A } while (0)
1N/A
1N/Abool
1N/Aputxline(l, len, mci, pxflags)
1N/A register char *l;
1N/A size_t len;
1N/A register MCI *mci;
1N/A int pxflags;
1N/A{
1N/A register char *p, *end;
1N/A int slop;
1N/A bool dead, quotenext, strip8bit;
1N/A
1N/A /* strip out 0200 bits -- these can look like TELNET protocol */
1N/A strip8bit = bitset(MCIF_7BIT, mci->mci_flags) ||
1N/A bitset(PXLF_STRIP8BIT, pxflags);
1N/A dead = false;
1N/A slop = 0;
1N/A
1N/A end = l + len;
1N/A do
1N/A {
1N/A bool noeol = false;
1N/A
1N/A /* find the end of the line */
1N/A p = memchr(l, '\n', end - l);
1N/A if (p == NULL)
1N/A {
1N/A p = end;
1N/A noeol = true;
1N/A }
1N/A
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
1N/A "%05d >>> ", (int) CurrentPid);
1N/A
1N/A /* check for line overflow */
1N/A while (mci->mci_mailer->m_linelimit > 0 &&
1N/A (p - l + slop) > mci->mci_mailer->m_linelimit)
1N/A {
1N/A register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1];
1N/A
1N/A if (l[0] == '.' && slop == 0 &&
1N/A bitnset(M_XDOT, mci->mci_mailer->m_flags))
1N/A {
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1N/A '.') == SM_IO_EOF)
1N/A dead = true;
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile,
1N/A SM_TIME_DEFAULT, '.');
1N/A }
1N/A else if (l[0] == 'F' && slop == 0 &&
1N/A bitset(PXLF_MAPFROM, pxflags) &&
1N/A strncmp(l, "From ", 5) == 0 &&
1N/A bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
1N/A {
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1N/A '>') == SM_IO_EOF)
1N/A dead = true;
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile,
1N/A SM_TIME_DEFAULT,
1N/A '>');
1N/A }
1N/A if (dead)
1N/A break;
1N/A
1N/A PUTX(q);
1N/A if (dead)
1N/A break;
1N/A
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1N/A '!') == SM_IO_EOF ||
1N/A sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
1N/A mci->mci_mailer->m_eol) == SM_IO_EOF ||
1N/A sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1N/A ' ') == SM_IO_EOF)
1N/A {
1N/A dead = true;
1N/A break;
1N/A }
1N/A if (TrafficLogFile != NULL)
1N/A {
1N/A (void) sm_io_fprintf(TrafficLogFile,
1N/A SM_TIME_DEFAULT,
1N/A "!\n%05d >>> ",
1N/A (int) CurrentPid);
1N/A }
1N/A slop = 1;
1N/A }
1N/A
1N/A if (dead)
1N/A break;
1N/A
1N/A /* output last part */
1N/A if (l[0] == '.' && slop == 0 &&
1N/A bitnset(M_XDOT, mci->mci_mailer->m_flags) &&
1N/A !bitset(MCIF_INLONGLINE, mci->mci_flags))
1N/A {
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '.') ==
1N/A SM_IO_EOF)
1N/A {
1N/A dead = true;
1N/A break;
1N/A }
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile,
1N/A SM_TIME_DEFAULT, '.');
1N/A }
1N/A else if (l[0] == 'F' && slop == 0 &&
1N/A bitset(PXLF_MAPFROM, pxflags) &&
1N/A strncmp(l, "From ", 5) == 0 &&
1N/A bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
1N/A !bitset(MCIF_INLONGLINE, mci->mci_flags))
1N/A {
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '>') ==
1N/A SM_IO_EOF)
1N/A {
1N/A dead = true;
1N/A break;
1N/A }
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile,
1N/A SM_TIME_DEFAULT, '>');
1N/A }
1N/A PUTX(p);
1N/A if (dead)
1N/A break;
1N/A
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile, SM_TIME_DEFAULT,
1N/A '\n');
1N/A if ((!bitset(PXLF_NOADDEOL, pxflags) || !noeol))
1N/A {
1N/A mci->mci_flags &= ~MCIF_INLONGLINE;
1N/A if (sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
1N/A mci->mci_mailer->m_eol) == SM_IO_EOF)
1N/A {
1N/A dead = true;
1N/A break;
1N/A }
1N/A }
1N/A else
1N/A mci->mci_flags |= MCIF_INLONGLINE;
1N/A
1N/A if (l < end && *l == '\n')
1N/A {
1N/A if (*++l != ' ' && *l != '\t' && *l != '\0' &&
1N/A bitset(PXLF_HEADER, pxflags))
1N/A {
1N/A if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
1N/A ' ') == SM_IO_EOF)
1N/A {
1N/A dead = true;
1N/A break;
1N/A }
1N/A
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_putc(TrafficLogFile,
1N/A SM_TIME_DEFAULT, ' ');
1N/A }
1N/A }
1N/A
1N/A } while (l < end);
1N/A return !dead;
1N/A}
1N/A
1N/A/*
1N/A** XUNLINK -- unlink a file, doing logging as appropriate.
1N/A**
1N/A** Parameters:
1N/A** f -- name of file to unlink.
1N/A**
1N/A** Returns:
1N/A** return value of unlink()
1N/A**
1N/A** Side Effects:
1N/A** f is unlinked.
1N/A*/
1N/A
1N/Aint
1N/Axunlink(f)
1N/A char *f;
1N/A{
1N/A register int i;
1N/A int save_errno;
1N/A
1N/A if (LogLevel > 98)
1N/A sm_syslog(LOG_DEBUG, CurEnv->e_id, "unlink %s", f);
1N/A
1N/A i = unlink(f);
1N/A save_errno = errno;
1N/A if (i < 0 && LogLevel > 97)
1N/A sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s: unlink-fail %d",
1N/A f, errno);
1N/A if (i >= 0)
1N/A SYNC_DIR(f, false);
1N/A errno = save_errno;
1N/A return i;
1N/A}
1N/A
1N/A/*
1N/A** SFGETS -- "safe" fgets -- times out and ignores random interrupts.
1N/A**
1N/A** Parameters:
1N/A** buf -- place to put the input line.
1N/A** siz -- size of buf.
1N/A** fp -- file to read from.
1N/A** timeout -- the timeout before error occurs.
1N/A** during -- what we are trying to read (for error messages).
1N/A**
1N/A** Returns:
1N/A** NULL on error (including timeout). This may also leave
1N/A** buf containing a null string.
1N/A** buf otherwise.
1N/A*/
1N/A
1N/A
1N/Achar *
1N/Asfgets(buf, siz, fp, timeout, during)
1N/A char *buf;
1N/A int siz;
1N/A SM_FILE_T *fp;
1N/A time_t timeout;
1N/A char *during;
1N/A{
1N/A register char *p;
1N/A int save_errno;
1N/A int io_timeout;
1N/A
1N/A SM_REQUIRE(siz > 0);
1N/A SM_REQUIRE(buf != NULL);
1N/A
1N/A if (fp == NULL)
1N/A {
1N/A buf[0] = '\0';
1N/A errno = EBADF;
1N/A return NULL;
1N/A }
1N/A
1N/A /* try to read */
1N/A p = NULL;
1N/A errno = 0;
1N/A
1N/A /* convert the timeout to sm_io notation */
1N/A io_timeout = (timeout <= 0) ? SM_TIME_DEFAULT : timeout * 1000;
1N/A while (!sm_io_eof(fp) && !sm_io_error(fp))
1N/A {
1N/A errno = 0;
1N/A p = sm_io_fgets(fp, io_timeout, buf, siz);
1N/A if (p == NULL && errno == EAGAIN)
1N/A {
1N/A /* The sm_io_fgets() call timedout */
1N/A if (LogLevel > 1)
1N/A sm_syslog(LOG_NOTICE, CurEnv->e_id,
1N/A "timeout waiting for input from %.100s during %s",
1N/A CURHOSTNAME,
1N/A during);
1N/A buf[0] = '\0';
1N/A#if XDEBUG
1N/A checkfd012(during);
1N/A#endif /* XDEBUG */
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_fprintf(TrafficLogFile,
1N/A SM_TIME_DEFAULT,
1N/A "%05d <<< [TIMEOUT]\n",
1N/A (int) CurrentPid);
1N/A errno = ETIMEDOUT;
1N/A return NULL;
1N/A }
1N/A if (p != NULL || errno != EINTR)
1N/A break;
1N/A (void) sm_io_clearerr(fp);
1N/A }
1N/A save_errno = errno;
1N/A
1N/A /* clean up the books and exit */
1N/A LineNumber++;
1N/A if (p == NULL)
1N/A {
1N/A buf[0] = '\0';
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
1N/A "%05d <<< [EOF]\n",
1N/A (int) CurrentPid);
1N/A errno = save_errno;
1N/A return NULL;
1N/A }
1N/A if (TrafficLogFile != NULL)
1N/A (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
1N/A "%05d <<< %s", (int) CurrentPid, buf);
1N/A if (SevenBitInput)
1N/A {
1N/A for (p = buf; *p != '\0'; p++)
1N/A *p &= ~0200;
1N/A }
1N/A else if (!HasEightBits)
1N/A {
1N/A for (p = buf; *p != '\0'; p++)
1N/A {
1N/A if (bitset(0200, *p))
1N/A {
1N/A HasEightBits = true;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A return buf;
1N/A}
1N/A
1N/A/*
1N/A** FGETFOLDED -- like fgets, but knows about folded lines.
1N/A**
1N/A** Parameters:
1N/A** buf -- place to put result.
1N/A** np -- pointer to bytes available; will be updated with
1N/A** the actual buffer size (not number of bytes filled)
1N/A** on return.
1N/A** f -- file to read from.
1N/A**
1N/A** Returns:
1N/A** input line(s) on success, NULL on error or SM_IO_EOF.
1N/A** This will normally be buf -- unless the line is too
1N/A** long, when it will be sm_malloc_x()ed.
1N/A**
1N/A** Side Effects:
1N/A** buf gets lines from f, with continuation lines (lines
1N/A** with leading white space) appended. CRLF's are mapped
1N/A** into single newlines. Any trailing NL is stripped.
1N/A*/
1N/A
1N/Achar *
1N/Afgetfolded(buf, np, f)
1N/A char *buf;
1N/A int *np;
1N/A SM_FILE_T *f;
1N/A{
1N/A register char *p = buf;
1N/A char *bp = buf;
1N/A register int i;
1N/A int n;
1N/A
1N/A SM_REQUIRE(np != NULL);
1N/A n = *np;
1N/A SM_REQUIRE(n > 0);
1N/A SM_REQUIRE(buf != NULL);
1N/A if (f == NULL)
1N/A {
1N/A buf[0] = '\0';
1N/A errno = EBADF;
1N/A return NULL;
1N/A }
1N/A
1N/A n--;
1N/A while ((i = sm_io_getc(f, SM_TIME_DEFAULT)) != SM_IO_EOF)
1N/A {
1N/A if (i == '\r')
1N/A {
1N/A i = sm_io_getc(f, SM_TIME_DEFAULT);
1N/A if (i != '\n')
1N/A {
1N/A if (i != SM_IO_EOF)
1N/A (void) sm_io_ungetc(f, SM_TIME_DEFAULT,
1N/A i);
1N/A i = '\r';
1N/A }
1N/A }
1N/A if (--n <= 0)
1N/A {
1N/A /* allocate new space */
1N/A char *nbp;
1N/A int nn;
1N/A
1N/A nn = (p - bp);
1N/A if (nn < MEMCHUNKSIZE)
1N/A nn *= 2;
1N/A else
1N/A nn += MEMCHUNKSIZE;
1N/A nbp = sm_malloc_x(nn);
1N/A memmove(nbp, bp, p - bp);
1N/A p = &nbp[p - bp];
1N/A if (bp != buf)
1N/A sm_free(bp);
1N/A bp = nbp;
1N/A n = nn - (p - bp);
1N/A *np = nn;
1N/A }
1N/A *p++ = i;
1N/A if (i == '\n')
1N/A {
1N/A LineNumber++;
1N/A i = sm_io_getc(f, SM_TIME_DEFAULT);
1N/A if (i != SM_IO_EOF)
1N/A (void) sm_io_ungetc(f, SM_TIME_DEFAULT, i);
1N/A if (i != ' ' && i != '\t')
1N/A break;
1N/A }
1N/A }
1N/A if (p == bp)
1N/A return NULL;
1N/A if (p[-1] == '\n')
1N/A p--;
1N/A *p = '\0';
1N/A return bp;
1N/A}
1N/A
1N/A/*
1N/A** CURTIME -- return current time.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** the current time.
1N/A*/
1N/A
1N/Atime_t
1N/Acurtime()
1N/A{
1N/A auto time_t t;
1N/A
1N/A (void) time(&t);
1N/A return t;
1N/A}
1N/A
1N/A/*
1N/A** ATOBOOL -- convert a string representation to boolean.
1N/A**
1N/A** Defaults to false
1N/A**
1N/A** Parameters:
1N/A** s -- string to convert. Takes "tTyY", empty, and NULL as true,
1N/A** others as false.
1N/A**
1N/A** Returns:
1N/A** A boolean representation of the string.
1N/A*/
1N/A
1N/Abool
1N/Aatobool(s)
1N/A register char *s;
1N/A{
1N/A if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
1N/A return true;
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** ATOOCT -- convert a string representation to octal.
1N/A**
1N/A** Parameters:
1N/A** s -- string to convert.
1N/A**
1N/A** Returns:
1N/A** An integer representing the string interpreted as an
1N/A** octal number.
1N/A*/
1N/A
1N/Aint
1N/Aatooct(s)
1N/A register char *s;
1N/A{
1N/A register int i = 0;
1N/A
1N/A while (*s >= '0' && *s <= '7')
1N/A i = (i << 3) | (*s++ - '0');
1N/A return i;
1N/A}
1N/A
1N/A/*
1N/A** BITINTERSECT -- tell if two bitmaps intersect
1N/A**
1N/A** Parameters:
1N/A** a, b -- the bitmaps in question
1N/A**
1N/A** Returns:
1N/A** true if they have a non-null intersection
1N/A** false otherwise
1N/A*/
1N/A
1N/Abool
1N/Abitintersect(a, b)
1N/A BITMAP256 a;
1N/A BITMAP256 b;
1N/A{
1N/A int i;
1N/A
1N/A for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
1N/A {
1N/A if ((a[i] & b[i]) != 0)
1N/A return true;
1N/A }
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** BITZEROP -- tell if a bitmap is all zero
1N/A**
1N/A** Parameters:
1N/A** map -- the bit map to check
1N/A**
1N/A** Returns:
1N/A** true if map is all zero.
1N/A** false if there are any bits set in map.
1N/A*/
1N/A
1N/Abool
1N/Abitzerop(map)
1N/A BITMAP256 map;
1N/A{
1N/A int i;
1N/A
1N/A for (i = BITMAPBYTES / sizeof(int); --i >= 0; )
1N/A {
1N/A if (map[i] != 0)
1N/A return false;
1N/A }
1N/A return true;
1N/A}
1N/A
1N/A/*
1N/A** STRCONTAINEDIN -- tell if one string is contained in another
1N/A**
1N/A** Parameters:
1N/A** icase -- ignore case?
1N/A** a -- possible substring.
1N/A** b -- possible superstring.
1N/A**
1N/A** Returns:
1N/A** true if a is contained in b (case insensitive).
1N/A** false otherwise.
1N/A*/
1N/A
1N/Abool
1N/Astrcontainedin(icase, a, b)
1N/A bool icase;
1N/A register char *a;
1N/A register char *b;
1N/A{
1N/A int la;
1N/A int lb;
1N/A int c;
1N/A
1N/A la = strlen(a);
1N/A lb = strlen(b);
1N/A c = *a;
1N/A if (icase && isascii(c) && isupper(c))
1N/A c = tolower(c);
1N/A for (; lb-- >= la; b++)
1N/A {
1N/A if (icase)
1N/A {
1N/A if (*b != c &&
1N/A isascii(*b) && isupper(*b) && tolower(*b) != c)
1N/A continue;
1N/A if (sm_strncasecmp(a, b, la) == 0)
1N/A return true;
1N/A }
1N/A else
1N/A {
1N/A if (*b != c)
1N/A continue;
1N/A if (strncmp(a, b, la) == 0)
1N/A return true;
1N/A }
1N/A }
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** CHECKFD012 -- check low numbered file descriptors
1N/A**
1N/A** File descriptors 0, 1, and 2 should be open at all times.
1N/A** This routine verifies that, and fixes it if not true.
1N/A**
1N/A** Parameters:
1N/A** where -- a tag printed if the assertion failed
1N/A**
1N/A** Returns:
1N/A** none
1N/A*/
1N/A
1N/Avoid
1N/Acheckfd012(where)
1N/A char *where;
1N/A{
1N/A#if XDEBUG
1N/A register int i;
1N/A
1N/A for (i = 0; i < 3; i++)
1N/A fill_fd(i, where);
1N/A#endif /* XDEBUG */
1N/A}
1N/A
1N/A/*
1N/A** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging
1N/A**
1N/A** Parameters:
1N/A** fd -- file descriptor to check.
1N/A** where -- tag to print on failure.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Acheckfdopen(fd, where)
1N/A int fd;
1N/A char *where;
1N/A{
1N/A#if XDEBUG
1N/A struct stat st;
1N/A
1N/A if (fstat(fd, &st) < 0 && errno == EBADF)
1N/A {
1N/A syserr("checkfdopen(%d): %s not open as expected!", fd, where);
1N/A printopenfds(true);
1N/A }
1N/A#endif /* XDEBUG */
1N/A}
1N/A
1N/A/*
1N/A** CHECKFDS -- check for new or missing file descriptors
1N/A**
1N/A** Parameters:
1N/A** where -- tag for printing. If null, take a base line.
1N/A**
1N/A** Returns:
1N/A** none
1N/A**
1N/A** Side Effects:
1N/A** If where is set, shows changes since the last call.
1N/A*/
1N/A
1N/Avoid
1N/Acheckfds(where)
1N/A char *where;
1N/A{
1N/A int maxfd;
1N/A register int fd;
1N/A bool printhdr = true;
1N/A int save_errno = errno;
1N/A static BITMAP256 baseline;
1N/A extern int DtableSize;
1N/A
1N/A if (DtableSize > BITMAPBITS)
1N/A maxfd = BITMAPBITS;
1N/A else
1N/A maxfd = DtableSize;
1N/A if (where == NULL)
1N/A clrbitmap(baseline);
1N/A
1N/A for (fd = 0; fd < maxfd; fd++)
1N/A {
1N/A struct stat stbuf;
1N/A
1N/A if (fstat(fd, &stbuf) < 0 && errno != EOPNOTSUPP)
1N/A {
1N/A if (!bitnset(fd, baseline))
1N/A continue;
1N/A clrbitn(fd, baseline);
1N/A }
1N/A else if (!bitnset(fd, baseline))
1N/A setbitn(fd, baseline);
1N/A else
1N/A continue;
1N/A
1N/A /* file state has changed */
1N/A if (where == NULL)
1N/A continue;
1N/A if (printhdr)
1N/A {
1N/A sm_syslog(LOG_DEBUG, CurEnv->e_id,
1N/A "%s: changed fds:",
1N/A where);
1N/A printhdr = false;
1N/A }
1N/A dumpfd(fd, true, true);
1N/A }
1N/A errno = save_errno;
1N/A}
1N/A
1N/A/*
1N/A** PRINTOPENFDS -- print the open file descriptors (for debugging)
1N/A**
1N/A** Parameters:
1N/A** logit -- if set, send output to syslog; otherwise
1N/A** print for debugging.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/A#if NETINET || NETINET6
1N/A# include <arpa/inet.h>
1N/A#endif /* NETINET || NETINET6 */
1N/A
1N/Avoid
1N/Aprintopenfds(logit)
1N/A bool logit;
1N/A{
1N/A register int fd;
1N/A extern int DtableSize;
1N/A
1N/A for (fd = 0; fd < DtableSize; fd++)
1N/A dumpfd(fd, false, logit);
1N/A}
1N/A
1N/A/*
1N/A** DUMPFD -- dump a file descriptor
1N/A**
1N/A** Parameters:
1N/A** fd -- the file descriptor to dump.
1N/A** printclosed -- if set, print a notification even if
1N/A** it is closed; otherwise print nothing.
1N/A** logit -- if set, use sm_syslog instead of sm_dprintf()
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Adumpfd(fd, printclosed, logit)
1N/A int fd;
1N/A bool printclosed;
1N/A bool logit;
1N/A{
1N/A register char *p;
1N/A char *hp;
1N/A#ifdef S_IFSOCK
1N/A SOCKADDR sa;
1N/A#endif /* S_IFSOCK */
1N/A auto SOCKADDR_LEN_T slen;
1N/A int i;
1N/A#if STAT64 > 0
1N/A struct stat64 st;
1N/A#else /* STAT64 > 0 */
1N/A struct stat st;
1N/A#endif /* STAT64 > 0 */
1N/A char buf[200];
1N/A
1N/A p = buf;
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "%3d: ", fd);
1N/A p += strlen(p);
1N/A
1N/A if (
1N/A#if STAT64 > 0
1N/A fstat64(fd, &st)
1N/A#else /* STAT64 > 0 */
1N/A fstat(fd, &st)
1N/A#endif /* STAT64 > 0 */
1N/A < 0)
1N/A {
1N/A if (errno != EBADF)
1N/A {
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "CANNOT STAT (%s)",
1N/A sm_errstring(errno));
1N/A goto printit;
1N/A }
1N/A else if (printclosed)
1N/A {
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "CLOSED");
1N/A goto printit;
1N/A }
1N/A return;
1N/A }
1N/A
1N/A i = fcntl(fd, F_GETFL, 0);
1N/A if (i != -1)
1N/A {
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "fl=0x%x, ", i);
1N/A p += strlen(p);
1N/A }
1N/A
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "mode=%o: ",
1N/A (int) st.st_mode);
1N/A p += strlen(p);
1N/A switch (st.st_mode & S_IFMT)
1N/A {
1N/A#ifdef S_IFSOCK
1N/A case S_IFSOCK:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "SOCK ");
1N/A p += strlen(p);
1N/A memset(&sa, '\0', sizeof(sa));
1N/A slen = sizeof(sa);
1N/A if (getsockname(fd, &sa.sa, &slen) < 0)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
1N/A sm_errstring(errno));
1N/A else
1N/A {
1N/A hp = hostnamebyanyaddr(&sa);
1N/A if (hp == NULL)
1N/A {
1N/A /* EMPTY */
1N/A /* do nothing */
1N/A }
1N/A# if NETINET
1N/A else if (sa.sa.sa_family == AF_INET)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s/%d", hp, ntohs(sa.sin.sin_port));
1N/A# endif /* NETINET */
1N/A# if NETINET6
1N/A else if (sa.sa.sa_family == AF_INET6)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s/%d", hp, ntohs(sa.sin6.sin6_port));
1N/A# endif /* NETINET6 */
1N/A else
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s", hp);
1N/A }
1N/A p += strlen(p);
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "->");
1N/A p += strlen(p);
1N/A slen = sizeof(sa);
1N/A if (getpeername(fd, &sa.sa, &slen) < 0)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)",
1N/A sm_errstring(errno));
1N/A else
1N/A {
1N/A hp = hostnamebyanyaddr(&sa);
1N/A if (hp == NULL)
1N/A {
1N/A /* EMPTY */
1N/A /* do nothing */
1N/A }
1N/A# if NETINET
1N/A else if (sa.sa.sa_family == AF_INET)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s/%d", hp, ntohs(sa.sin.sin_port));
1N/A# endif /* NETINET */
1N/A# if NETINET6
1N/A else if (sa.sa.sa_family == AF_INET6)
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s/%d", hp, ntohs(sa.sin6.sin6_port));
1N/A# endif /* NETINET6 */
1N/A else
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "%s", hp);
1N/A }
1N/A break;
1N/A#endif /* S_IFSOCK */
1N/A
1N/A case S_IFCHR:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "CHR: ");
1N/A p += strlen(p);
1N/A goto defprint;
1N/A
1N/A#ifdef S_IFBLK
1N/A case S_IFBLK:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "BLK: ");
1N/A p += strlen(p);
1N/A goto defprint;
1N/A#endif /* S_IFBLK */
1N/A
1N/A#if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK)
1N/A case S_IFIFO:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "FIFO: ");
1N/A p += strlen(p);
1N/A goto defprint;
1N/A#endif /* defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) */
1N/A
1N/A#ifdef S_IFDIR
1N/A case S_IFDIR:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "DIR: ");
1N/A p += strlen(p);
1N/A goto defprint;
1N/A#endif /* S_IFDIR */
1N/A
1N/A#ifdef S_IFLNK
1N/A case S_IFLNK:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "LNK: ");
1N/A p += strlen(p);
1N/A goto defprint;
1N/A#endif /* S_IFLNK */
1N/A
1N/A default:
1N/Adefprint:
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p),
1N/A "dev=%d/%d, ino=%llu, nlink=%d, u/gid=%d/%d, ",
1N/A major(st.st_dev), minor(st.st_dev),
1N/A (ULONGLONG_T) st.st_ino,
1N/A (int) st.st_nlink, (int) st.st_uid,
1N/A (int) st.st_gid);
1N/A p += strlen(p);
1N/A (void) sm_snprintf(p, SPACELEFT(buf, p), "size=%llu",
1N/A (ULONGLONG_T) st.st_size);
1N/A break;
1N/A }
1N/A
1N/Aprintit:
1N/A if (logit)
1N/A sm_syslog(LOG_DEBUG, CurEnv ? CurEnv->e_id : NULL,
1N/A "%.800s", buf);
1N/A else
1N/A sm_dprintf("%s\n", buf);
1N/A}
1N/A
1N/A/*
1N/A** SHORTEN_HOSTNAME -- strip local domain information off of hostname.
1N/A**
1N/A** Parameters:
1N/A** host -- the host to shorten (stripped in place).
1N/A**
1N/A** Returns:
1N/A** place where string was truncated, NULL if not truncated.
1N/A*/
1N/A
1N/Achar *
1N/Ashorten_hostname(host)
1N/A char host[];
1N/A{
1N/A register char *p;
1N/A char *mydom;
1N/A int i;
1N/A bool canon = false;
1N/A
1N/A /* strip off final dot */
1N/A i = strlen(host);
1N/A p = &host[(i == 0) ? 0 : i - 1];
1N/A if (*p == '.')
1N/A {
1N/A *p = '\0';
1N/A canon = true;
1N/A }
1N/A
1N/A /* see if there is any domain at all -- if not, we are done */
1N/A p = strchr(host, '.');
1N/A if (p == NULL)
1N/A return NULL;
1N/A
1N/A /* yes, we have a domain -- see if it looks like us */
1N/A mydom = macvalue('m', CurEnv);
1N/A if (mydom == NULL)
1N/A mydom = "";
1N/A i = strlen(++p);
1N/A if ((canon ? sm_strcasecmp(p, mydom)
1N/A : sm_strncasecmp(p, mydom, i)) == 0 &&
1N/A (mydom[i] == '.' || mydom[i] == '\0'))
1N/A {
1N/A *--p = '\0';
1N/A return p;
1N/A }
1N/A return NULL;
1N/A}
1N/A
1N/A/*
1N/A** PROG_OPEN -- open a program for reading
1N/A**
1N/A** Parameters:
1N/A** argv -- the argument list.
1N/A** pfd -- pointer to a place to store the file descriptor.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** pid of the process -- -1 if it failed.
1N/A*/
1N/A
1N/Apid_t
1N/Aprog_open(argv, pfd, e)
1N/A char **argv;
1N/A int *pfd;
1N/A ENVELOPE *e;
1N/A{
1N/A pid_t pid;
1N/A int save_errno;
1N/A int sff;
1N/A int ret;
1N/A int fdv[2];
1N/A char *p, *q;
1N/A char buf[MAXPATHLEN];
1N/A extern int DtableSize;
1N/A
1N/A if (pipe(fdv) < 0)
1N/A {
1N/A syserr("%s: cannot create pipe for stdout", argv[0]);
1N/A return -1;
1N/A }
1N/A pid = fork();
1N/A if (pid < 0)
1N/A {
1N/A syserr("%s: cannot fork", argv[0]);
1N/A (void) close(fdv[0]);
1N/A (void) close(fdv[1]);
1N/A return -1;
1N/A }
1N/A if (pid > 0)
1N/A {
1N/A /* parent */
1N/A (void) close(fdv[1]);
1N/A *pfd = fdv[0];
1N/A return pid;
1N/A }
1N/A
1N/A /* Reset global flags */
1N/A RestartRequest = NULL;
1N/A RestartWorkGroup = false;
1N/A ShutdownRequest = NULL;
1N/A PendingSignal = 0;
1N/A CurrentPid = getpid();
1N/A
1N/A /*
1N/A ** Initialize exception stack and default exception
1N/A ** handler for child process.
1N/A */
1N/A
1N/A sm_exc_newthread(fatal_error);
1N/A
1N/A /* child -- close stdin */
1N/A (void) close(0);
1N/A
1N/A /* stdout goes back to parent */
1N/A (void) close(fdv[0]);
1N/A if (dup2(fdv[1], 1) < 0)
1N/A {
1N/A syserr("%s: cannot dup2 for stdout", argv[0]);
1N/A _exit(EX_OSERR);
1N/A }
1N/A (void) close(fdv[1]);
1N/A
1N/A /* stderr goes to transcript if available */
1N/A if (e->e_xfp != NULL)
1N/A {
1N/A int xfd;
1N/A
1N/A xfd = sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL);
1N/A if (xfd >= 0 && dup2(xfd, 2) < 0)
1N/A {
1N/A syserr("%s: cannot dup2 for stderr", argv[0]);
1N/A _exit(EX_OSERR);
1N/A }
1N/A }
1N/A
1N/A /* this process has no right to the queue file */
1N/A if (e->e_lockfp != NULL)
1N/A {
1N/A int fd;
1N/A
1N/A fd = sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL);
1N/A if (fd >= 0)
1N/A (void) close(fd);
1N/A else
1N/A syserr("%s: lockfp does not have a fd", argv[0]);
1N/A }
1N/A
1N/A /* chroot to the program mailer directory, if defined */
1N/A if (ProgMailer != NULL && ProgMailer->m_rootdir != NULL)
1N/A {
1N/A expand(ProgMailer->m_rootdir, buf, sizeof(buf), e);
1N/A if (chroot(buf) < 0)
1N/A {
1N/A syserr("prog_open: cannot chroot(%s)", buf);
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A if (chdir("/") < 0)
1N/A {
1N/A syserr("prog_open: cannot chdir(/)");
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A }
1N/A
1N/A /* run as default user */
1N/A endpwent();
1N/A sm_mbdb_terminate();
1N/A#if _FFR_MEMSTAT
1N/A (void) sm_memstat_close();
1N/A#endif /* _FFR_MEMSTAT */
1N/A if (setgid(DefGid) < 0 && geteuid() == 0)
1N/A {
1N/A syserr("prog_open: setgid(%ld) failed", (long) DefGid);
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A if (setuid(DefUid) < 0 && geteuid() == 0)
1N/A {
1N/A syserr("prog_open: setuid(%ld) failed", (long) DefUid);
1N/A exit(EX_TEMPFAIL);
1N/A }
1N/A
1N/A /* run in some directory */
1N/A if (ProgMailer != NULL)
1N/A p = ProgMailer->m_execdir;
1N/A else
1N/A p = NULL;
1N/A for (; p != NULL; p = q)
1N/A {
1N/A q = strchr(p, ':');
1N/A if (q != NULL)
1N/A *q = '\0';
1N/A expand(p, buf, sizeof(buf), e);
1N/A if (q != NULL)
1N/A *q++ = ':';
1N/A if (buf[0] != '\0' && chdir(buf) >= 0)
1N/A break;
1N/A }
1N/A if (p == NULL)
1N/A {
1N/A /* backup directories */
1N/A if (chdir("/tmp") < 0)
1N/A (void) chdir("/");
1N/A }
1N/A
1N/A /* Check safety of program to be run */
1N/A sff = SFF_ROOTOK|SFF_EXECOK;
1N/A if (!bitnset(DBS_RUNWRITABLEPROGRAM, DontBlameSendmail))
1N/A sff |= SFF_NOGWFILES|SFF_NOWWFILES;
1N/A if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH, DontBlameSendmail))
1N/A sff |= SFF_NOPATHCHECK;
1N/A else
1N/A sff |= SFF_SAFEDIRPATH;
1N/A ret = safefile(argv[0], DefUid, DefGid, DefUser, sff, 0, NULL);
1N/A if (ret != 0)
1N/A sm_syslog(LOG_INFO, e->e_id,
1N/A "Warning: prog_open: program %s unsafe: %s",
1N/A argv[0], sm_errstring(ret));
1N/A
1N/A /* arrange for all the files to be closed */
1N/A sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
1N/A
1N/A /* now exec the process */
1N/A (void) execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron);
1N/A
1N/A /* woops! failed */
1N/A save_errno = errno;
1N/A syserr("%s: cannot exec", argv[0]);
1N/A if (transienterror(save_errno))
1N/A _exit(EX_OSERR);
1N/A _exit(EX_CONFIG);
1N/A return -1; /* avoid compiler warning on IRIX */
1N/A}
1N/A
1N/A/*
1N/A** GET_COLUMN -- look up a Column in a line buffer
1N/A**
1N/A** Parameters:
1N/A** line -- the raw text line to search.
1N/A** col -- the column number to fetch.
1N/A** delim -- the delimiter between columns. If null,
1N/A** use white space.
1N/A** buf -- the output buffer.
1N/A** buflen -- the length of buf.
1N/A**
1N/A** Returns:
1N/A** buf if successful.
1N/A** NULL otherwise.
1N/A*/
1N/A
1N/Achar *
1N/Aget_column(line, col, delim, buf, buflen)
1N/A char line[];
1N/A int col;
1N/A int delim;
1N/A char buf[];
1N/A int buflen;
1N/A{
1N/A char *p;
1N/A char *begin, *end;
1N/A int i;
1N/A char delimbuf[4];
1N/A
1N/A if ((char) delim == '\0')
1N/A (void) sm_strlcpy(delimbuf, "\n\t ", sizeof(delimbuf));
1N/A else
1N/A {
1N/A delimbuf[0] = (char) delim;
1N/A delimbuf[1] = '\0';
1N/A }
1N/A
1N/A p = line;
1N/A if (*p == '\0')
1N/A return NULL; /* line empty */
1N/A if (*p == (char) delim && col == 0)
1N/A return NULL; /* first column empty */
1N/A
1N/A begin = line;
1N/A
1N/A if (col == 0 && (char) delim == '\0')
1N/A {
1N/A while (*begin != '\0' && isascii(*begin) && isspace(*begin))
1N/A begin++;
1N/A }
1N/A
1N/A for (i = 0; i < col; i++)
1N/A {
1N/A if ((begin = strpbrk(begin, delimbuf)) == NULL)
1N/A return NULL; /* no such column */
1N/A begin++;
1N/A if ((char) delim == '\0')
1N/A {
1N/A while (*begin != '\0' && isascii(*begin) && isspace(*begin))
1N/A begin++;
1N/A }
1N/A }
1N/A
1N/A end = strpbrk(begin, delimbuf);
1N/A if (end == NULL)
1N/A i = strlen(begin);
1N/A else
1N/A i = end - begin;
1N/A if (i >= buflen)
1N/A i = buflen - 1;
1N/A (void) sm_strlcpy(buf, begin, i + 1);
1N/A return buf;
1N/A}
1N/A
1N/A/*
1N/A** CLEANSTRCPY -- copy string keeping out bogus characters
1N/A**
1N/A** Parameters:
1N/A** t -- "to" string.
1N/A** f -- "from" string.
1N/A** l -- length of space available in "to" string.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Acleanstrcpy(t, f, l)
1N/A register char *t;
1N/A register char *f;
1N/A int l;
1N/A{
1N/A /* check for newlines and log if necessary */
1N/A (void) denlstring(f, true, true);
1N/A
1N/A if (l <= 0)
1N/A syserr("!cleanstrcpy: length == 0");
1N/A
1N/A l--;
1N/A while (l > 0 && *f != '\0')
1N/A {
1N/A if (isascii(*f) &&
1N/A (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL))
1N/A {
1N/A l--;
1N/A *t++ = *f;
1N/A }
1N/A f++;
1N/A }
1N/A *t = '\0';
1N/A}
1N/A
1N/A/*
1N/A** DENLSTRING -- convert newlines in a string to spaces
1N/A**
1N/A** Parameters:
1N/A** s -- the input string
1N/A** strict -- if set, don't permit continuation lines.
1N/A** logattacks -- if set, log attempted attacks.
1N/A**
1N/A** Returns:
1N/A** A pointer to a version of the string with newlines
1N/A** mapped to spaces. This should be copied.
1N/A*/
1N/A
1N/Achar *
1N/Adenlstring(s, strict, logattacks)
1N/A char *s;
1N/A bool strict;
1N/A bool logattacks;
1N/A{
1N/A register char *p;
1N/A int l;
1N/A static char *bp = NULL;
1N/A static int bl = 0;
1N/A
1N/A p = s;
1N/A while ((p = strchr(p, '\n')) != NULL)
1N/A if (strict || (*++p != ' ' && *p != '\t'))
1N/A break;
1N/A if (p == NULL)
1N/A return s;
1N/A
1N/A l = strlen(s) + 1;
1N/A if (bl < l)
1N/A {
1N/A /* allocate more space */
1N/A char *nbp = sm_pmalloc_x(l);
1N/A
1N/A if (bp != NULL)
1N/A sm_free(bp);
1N/A bp = nbp;
1N/A bl = l;
1N/A }
1N/A (void) sm_strlcpy(bp, s, l);
1N/A for (p = bp; (p = strchr(p, '\n')) != NULL; )
1N/A *p++ = ' ';
1N/A
1N/A if (logattacks)
1N/A {
1N/A sm_syslog(LOG_NOTICE, CurEnv ? CurEnv->e_id : NULL,
1N/A "POSSIBLE ATTACK from %.100s: newline in string \"%s\"",
1N/A RealHostName == NULL ? "[UNKNOWN]" : RealHostName,
1N/A shortenstring(bp, MAXSHORTSTR));
1N/A }
1N/A
1N/A return bp;
1N/A}
1N/A
1N/A/*
1N/A** STRREPLNONPRT -- replace "unprintable" characters in a string with subst
1N/A**
1N/A** Parameters:
1N/A** s -- string to manipulate (in place)
1N/A** subst -- character to use as replacement
1N/A**
1N/A** Returns:
1N/A** true iff string did not contain "unprintable" characters
1N/A*/
1N/A
1N/Abool
1N/Astrreplnonprt(s, c)
1N/A char *s;
1N/A int c;
1N/A{
1N/A bool ok;
1N/A
1N/A ok = true;
1N/A if (s == NULL)
1N/A return ok;
1N/A while (*s != '\0')
1N/A {
1N/A if (!(isascii(*s) && isprint(*s)))
1N/A {
1N/A *s = c;
1N/A ok = false;
1N/A }
1N/A ++s;
1N/A }
1N/A return ok;
1N/A}
1N/A
1N/A/*
1N/A** PATH_IS_DIR -- check to see if file exists and is a directory.
1N/A**
1N/A** There are some additional checks for security violations in
1N/A** here. This routine is intended to be used for the host status
1N/A** support.
1N/A**
1N/A** Parameters:
1N/A** pathname -- pathname to check for directory-ness.
1N/A** createflag -- if set, create directory if needed.
1N/A**
1N/A** Returns:
1N/A** true -- if the indicated pathname is a directory
1N/A** false -- otherwise
1N/A*/
1N/A
1N/Abool
1N/Apath_is_dir(pathname, createflag)
1N/A char *pathname;
1N/A bool createflag;
1N/A{
1N/A struct stat statbuf;
1N/A
1N/A#if HASLSTAT
1N/A if (lstat(pathname, &statbuf) < 0)
1N/A#else /* HASLSTAT */
1N/A if (stat(pathname, &statbuf) < 0)
1N/A#endif /* HASLSTAT */
1N/A {
1N/A if (errno != ENOENT || !createflag)
1N/A return false;
1N/A if (mkdir(pathname, 0755) < 0)
1N/A return false;
1N/A return true;
1N/A }
1N/A if (!S_ISDIR(statbuf.st_mode))
1N/A {
1N/A errno = ENOTDIR;
1N/A return false;
1N/A }
1N/A
1N/A /* security: don't allow writable directories */
1N/A if (bitset(S_IWGRP|S_IWOTH, statbuf.st_mode))
1N/A {
1N/A errno = EACCES;
1N/A return false;
1N/A }
1N/A return true;
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_ADD -- add process id to list of our children
1N/A**
1N/A** Parameters:
1N/A** pid -- pid to add to list.
1N/A** task -- task of pid.
1N/A** type -- type of process.
1N/A** count -- number of processes.
1N/A** other -- other information for this type.
1N/A**
1N/A** Returns:
1N/A** none
1N/A**
1N/A** Side Effects:
1N/A** May increase CurChildren. May grow ProcList.
1N/A*/
1N/A
1N/Atypedef struct procs PROCS_T;
1N/A
1N/Astruct procs
1N/A{
1N/A pid_t proc_pid;
1N/A char *proc_task;
1N/A int proc_type;
1N/A int proc_count;
1N/A int proc_other;
1N/A SOCKADDR proc_hostaddr;
1N/A};
1N/A
1N/Astatic PROCS_T *volatile ProcListVec = NULL;
1N/Astatic int ProcListSize = 0;
1N/A
1N/Avoid
1N/Aproc_list_add(pid, task, type, count, other, hostaddr)
1N/A pid_t pid;
1N/A char *task;
1N/A int type;
1N/A int count;
1N/A int other;
1N/A SOCKADDR *hostaddr;
1N/A{
1N/A int i;
1N/A
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == NO_PID)
1N/A break;
1N/A }
1N/A if (i >= ProcListSize)
1N/A {
1N/A /* probe the existing vector to avoid growing infinitely */
1N/A proc_list_probe();
1N/A
1N/A /* now scan again */
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == NO_PID)
1N/A break;
1N/A }
1N/A }
1N/A if (i >= ProcListSize)
1N/A {
1N/A /* grow process list */
1N/A int chldwasblocked;
1N/A PROCS_T *npv;
1N/A
1N/A SM_ASSERT(ProcListSize < INT_MAX - PROC_LIST_SEG);
1N/A npv = (PROCS_T *) sm_pmalloc_x((sizeof(*npv)) *
1N/A (ProcListSize + PROC_LIST_SEG));
1N/A
1N/A /* Block SIGCHLD so reapchild() doesn't mess with us */
1N/A chldwasblocked = sm_blocksignal(SIGCHLD);
1N/A if (ProcListSize > 0)
1N/A {
1N/A memmove(npv, ProcListVec,
1N/A ProcListSize * sizeof(PROCS_T));
1N/A sm_free(ProcListVec);
1N/A }
1N/A
1N/A /* XXX just use memset() to initialize this part? */
1N/A for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++)
1N/A {
1N/A npv[i].proc_pid = NO_PID;
1N/A npv[i].proc_task = NULL;
1N/A npv[i].proc_type = PROC_NONE;
1N/A }
1N/A i = ProcListSize;
1N/A ProcListSize += PROC_LIST_SEG;
1N/A ProcListVec = npv;
1N/A if (chldwasblocked == 0)
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A }
1N/A ProcListVec[i].proc_pid = pid;
1N/A PSTRSET(ProcListVec[i].proc_task, task);
1N/A ProcListVec[i].proc_type = type;
1N/A ProcListVec[i].proc_count = count;
1N/A ProcListVec[i].proc_other = other;
1N/A if (hostaddr != NULL)
1N/A ProcListVec[i].proc_hostaddr = *hostaddr;
1N/A else
1N/A memset(&ProcListVec[i].proc_hostaddr, 0,
1N/A sizeof(ProcListVec[i].proc_hostaddr));
1N/A
1N/A /* if process adding itself, it's not a child */
1N/A if (pid != CurrentPid)
1N/A {
1N/A SM_ASSERT(CurChildren < INT_MAX);
1N/A CurChildren++;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_SET -- set pid task in process list
1N/A**
1N/A** Parameters:
1N/A** pid -- pid to set
1N/A** task -- task of pid
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_set(pid, task)
1N/A pid_t pid;
1N/A char *task;
1N/A{
1N/A int i;
1N/A
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == pid)
1N/A {
1N/A PSTRSET(ProcListVec[i].proc_task, task);
1N/A break;
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_DROP -- drop pid from process list
1N/A**
1N/A** Parameters:
1N/A** pid -- pid to drop
1N/A** st -- process status
1N/A** other -- storage for proc_other (return).
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** May decrease CurChildren, CurRunners, or
1N/A** set RestartRequest or ShutdownRequest.
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_drop(pid, st, other)
1N/A pid_t pid;
1N/A int st;
1N/A int *other;
1N/A{
1N/A int i;
1N/A int type = PROC_NONE;
1N/A
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == pid)
1N/A {
1N/A ProcListVec[i].proc_pid = NO_PID;
1N/A type = ProcListVec[i].proc_type;
1N/A if (other != NULL)
1N/A *other = ProcListVec[i].proc_other;
1N/A if (CurChildren > 0)
1N/A CurChildren--;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A
1N/A if (type == PROC_CONTROL && WIFEXITED(st))
1N/A {
1N/A /* if so, see if we need to restart or shutdown */
1N/A if (WEXITSTATUS(st) == EX_RESTART)
1N/A RestartRequest = "control socket";
1N/A else if (WEXITSTATUS(st) == EX_SHUTDOWN)
1N/A ShutdownRequest = "control socket";
1N/A }
1N/A else if (type == PROC_QUEUE_CHILD && !WIFSTOPPED(st) &&
1N/A ProcListVec[i].proc_other > -1)
1N/A {
1N/A /* restart this persistent runner */
1N/A mark_work_group_restart(ProcListVec[i].proc_other, st);
1N/A }
1N/A else if (type == PROC_QUEUE)
1N/A CurRunners -= ProcListVec[i].proc_count;
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_CLEAR -- clear the process list
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Sets CurChildren to zero.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_clear()
1N/A{
1N/A int i;
1N/A
1N/A /* start from 1 since 0 is the daemon itself */
1N/A for (i = 1; i < ProcListSize; i++)
1N/A ProcListVec[i].proc_pid = NO_PID;
1N/A CurChildren = 0;
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_PROBE -- probe processes in the list to see if they still exist
1N/A**
1N/A** Parameters:
1N/A** none
1N/A**
1N/A** Returns:
1N/A** none
1N/A**
1N/A** Side Effects:
1N/A** May decrease CurChildren.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_probe()
1N/A{
1N/A int i, children;
1N/A int chldwasblocked;
1N/A pid_t pid;
1N/A
1N/A children = 0;
1N/A chldwasblocked = sm_blocksignal(SIGCHLD);
1N/A
1N/A /* start from 1 since 0 is the daemon itself */
1N/A for (i = 1; i < ProcListSize; i++)
1N/A {
1N/A pid = ProcListVec[i].proc_pid;
1N/A if (pid == NO_PID || pid == CurrentPid)
1N/A continue;
1N/A if (kill(pid, 0) < 0)
1N/A {
1N/A if (LogLevel > 3)
1N/A sm_syslog(LOG_DEBUG, CurEnv->e_id,
1N/A "proc_list_probe: lost pid %d",
1N/A (int) ProcListVec[i].proc_pid);
1N/A ProcListVec[i].proc_pid = NO_PID;
1N/A SM_FREE_CLR(ProcListVec[i].proc_task);
1N/A CurChildren--;
1N/A }
1N/A else
1N/A {
1N/A ++children;
1N/A }
1N/A }
1N/A if (CurChildren < 0)
1N/A CurChildren = 0;
1N/A if (chldwasblocked == 0)
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A if (LogLevel > 10 && children != CurChildren && CurrentPid == DaemonPid)
1N/A {
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "proc_list_probe: found %d children, expected %d",
1N/A children, CurChildren);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_DISPLAY -- display the process list
1N/A**
1N/A** Parameters:
1N/A** out -- output file pointer
1N/A** prefix -- string to output in front of each line.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_display(out, prefix)
1N/A SM_FILE_T *out;
1N/A char *prefix;
1N/A{
1N/A int i;
1N/A
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == NO_PID)
1N/A continue;
1N/A
1N/A (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "%s%d %s%s\n",
1N/A prefix,
1N/A (int) ProcListVec[i].proc_pid,
1N/A ProcListVec[i].proc_task != NULL ?
1N/A ProcListVec[i].proc_task : "(unknown)",
1N/A (OpMode == MD_SMTP ||
1N/A OpMode == MD_DAEMON ||
1N/A OpMode == MD_ARPAFTP) ? "\r" : "");
1N/A }
1N/A}
1N/A
1N/A/*
1N/A** PROC_LIST_SIGNAL -- send a signal to a type of process in the list
1N/A**
1N/A** Parameters:
1N/A** type -- type of process to signal
1N/A** signal -- the type of signal to send
1N/A**
1N/A** Results:
1N/A** none.
1N/A**
1N/A** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1N/A** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1N/A** DOING.
1N/A*/
1N/A
1N/Avoid
1N/Aproc_list_signal(type, signal)
1N/A int type;
1N/A int signal;
1N/A{
1N/A int chldwasblocked;
1N/A int alrmwasblocked;
1N/A int i;
1N/A pid_t mypid = getpid();
1N/A
1N/A /* block these signals so that we may signal cleanly */
1N/A chldwasblocked = sm_blocksignal(SIGCHLD);
1N/A alrmwasblocked = sm_blocksignal(SIGALRM);
1N/A
1N/A /* Find all processes of type and send signal */
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == NO_PID ||
1N/A ProcListVec[i].proc_pid == mypid)
1N/A continue;
1N/A if (ProcListVec[i].proc_type != type)
1N/A continue;
1N/A (void) kill(ProcListVec[i].proc_pid, signal);
1N/A }
1N/A
1N/A /* restore the signals */
1N/A if (alrmwasblocked == 0)
1N/A (void) sm_releasesignal(SIGALRM);
1N/A if (chldwasblocked == 0)
1N/A (void) sm_releasesignal(SIGCHLD);
1N/A}
1N/A
1N/A/*
1N/A** COUNT_OPEN_CONNECTIONS
1N/A**
1N/A** Parameters:
1N/A** hostaddr - ClientAddress
1N/A**
1N/A** Returns:
1N/A** the number of open connections for this client
1N/A**
1N/A*/
1N/A
1N/Aint
1N/Acount_open_connections(hostaddr)
1N/A SOCKADDR *hostaddr;
1N/A{
1N/A int i, n;
1N/A
1N/A if (hostaddr == NULL)
1N/A return 0;
1N/A
1N/A /*
1N/A ** This code gets called before proc_list_add() gets called,
1N/A ** so we (the daemon child for this connection) have not yet
1N/A ** counted ourselves. Hence initialize the counter to 1
1N/A ** instead of 0 to compensate.
1N/A */
1N/A
1N/A n = 1;
1N/A for (i = 0; i < ProcListSize; i++)
1N/A {
1N/A if (ProcListVec[i].proc_pid == NO_PID)
1N/A continue;
1N/A if (hostaddr->sa.sa_family !=
1N/A ProcListVec[i].proc_hostaddr.sa.sa_family)
1N/A continue;
1N/A#if NETINET
1N/A if (hostaddr->sa.sa_family == AF_INET &&
1N/A (hostaddr->sin.sin_addr.s_addr ==
1N/A ProcListVec[i].proc_hostaddr.sin.sin_addr.s_addr))
1N/A n++;
1N/A#endif /* NETINET */
1N/A#if NETINET6
1N/A if (hostaddr->sa.sa_family == AF_INET6 &&
1N/A IN6_ARE_ADDR_EQUAL(&(hostaddr->sin6.sin6_addr),
1N/A &(ProcListVec[i].proc_hostaddr.sin6.sin6_addr)))
1N/A n++;
1N/A#endif /* NETINET6 */
1N/A }
1N/A return n;
1N/A}