1c2aa98e2SPeter Wemm /* 2ffb83623SGregory Neil Shapiro * Copyright (c) 1998-2007 Sendmail, Inc. and its suppliers. 306f25ae9SGregory Neil Shapiro * All rights reserved. 4c2aa98e2SPeter Wemm * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5c2aa98e2SPeter Wemm * Copyright (c) 1988, 1993 6c2aa98e2SPeter Wemm * The Regents of the University of California. All rights reserved. 7c2aa98e2SPeter Wemm * 8c2aa98e2SPeter Wemm * By using this file, you agree to the terms and conditions set 9c2aa98e2SPeter Wemm * forth in the LICENSE file which can be found at the top level of 10c2aa98e2SPeter Wemm * the sendmail distribution. 11c2aa98e2SPeter Wemm * 12c2aa98e2SPeter Wemm */ 13c2aa98e2SPeter Wemm 1406f25ae9SGregory Neil Shapiro #include <sendmail.h> 1540266059SGregory Neil Shapiro 16ffb83623SGregory Neil Shapiro SM_RCSID("@(#)$Id: util.c,v 8.413 2007/09/26 23:29:11 ca Exp $") 1740266059SGregory Neil Shapiro 18d0cef73dSGregory Neil Shapiro #include <sm/sendmail.h> 19c2aa98e2SPeter Wemm #include <sysexits.h> 2040266059SGregory Neil Shapiro #include <sm/xtrap.h> 2106f25ae9SGregory Neil Shapiro 2240266059SGregory Neil Shapiro /* 23e92d3f3fSGregory Neil Shapiro ** NEWSTR -- Create a copy of a C string 24e92d3f3fSGregory Neil Shapiro ** 25e92d3f3fSGregory Neil Shapiro ** Parameters: 26e92d3f3fSGregory Neil Shapiro ** s -- the string to copy. 27e92d3f3fSGregory Neil Shapiro ** 28e92d3f3fSGregory Neil Shapiro ** Returns: 29e92d3f3fSGregory Neil Shapiro ** pointer to newly allocated string. 30e92d3f3fSGregory Neil Shapiro */ 31e92d3f3fSGregory Neil Shapiro 32e92d3f3fSGregory Neil Shapiro char * 33e92d3f3fSGregory Neil Shapiro newstr(s) 34e92d3f3fSGregory Neil Shapiro const char *s; 35e92d3f3fSGregory Neil Shapiro { 36e92d3f3fSGregory Neil Shapiro size_t l; 37e92d3f3fSGregory Neil Shapiro char *n; 38e92d3f3fSGregory Neil Shapiro 39e92d3f3fSGregory Neil Shapiro l = strlen(s); 40e92d3f3fSGregory Neil Shapiro SM_ASSERT(l + 1 > l); 41e92d3f3fSGregory Neil Shapiro n = xalloc(l + 1); 42e92d3f3fSGregory Neil Shapiro sm_strlcpy(n, s, l + 1); 43e92d3f3fSGregory Neil Shapiro return n; 44e92d3f3fSGregory Neil Shapiro } 45e92d3f3fSGregory Neil Shapiro 46e92d3f3fSGregory Neil Shapiro /* 47c2aa98e2SPeter Wemm ** ADDQUOTES -- Adds quotes & quote bits to a string. 48c2aa98e2SPeter Wemm ** 4940266059SGregory Neil Shapiro ** Runs through a string and adds backslashes and quote bits. 50c2aa98e2SPeter Wemm ** 51c2aa98e2SPeter Wemm ** Parameters: 52c2aa98e2SPeter Wemm ** s -- the string to modify. 5340266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate result 54c2aa98e2SPeter Wemm ** 55c2aa98e2SPeter Wemm ** Returns: 56c2aa98e2SPeter Wemm ** pointer to quoted string. 57c2aa98e2SPeter Wemm */ 58c2aa98e2SPeter Wemm 59c2aa98e2SPeter Wemm char * 6040266059SGregory Neil Shapiro addquotes(s, rpool) 61c2aa98e2SPeter Wemm char *s; 6240266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 63c2aa98e2SPeter Wemm { 64c2aa98e2SPeter Wemm int len = 0; 65c2aa98e2SPeter Wemm char c; 66c2aa98e2SPeter Wemm char *p = s, *q, *r; 67c2aa98e2SPeter Wemm 68c2aa98e2SPeter Wemm if (s == NULL) 69c2aa98e2SPeter Wemm return NULL; 70c2aa98e2SPeter Wemm 71c2aa98e2SPeter Wemm /* Find length of quoted string */ 72c2aa98e2SPeter Wemm while ((c = *p++) != '\0') 73c2aa98e2SPeter Wemm { 74c2aa98e2SPeter Wemm len++; 75c2aa98e2SPeter Wemm if (c == '\\' || c == '"') 76c2aa98e2SPeter Wemm len++; 77c2aa98e2SPeter Wemm } 78c2aa98e2SPeter Wemm 7940266059SGregory Neil Shapiro q = r = sm_rpool_malloc_x(rpool, len + 3); 80c2aa98e2SPeter Wemm p = s; 81c2aa98e2SPeter Wemm 82c2aa98e2SPeter Wemm /* add leading quote */ 83c2aa98e2SPeter Wemm *q++ = '"'; 84c2aa98e2SPeter Wemm while ((c = *p++) != '\0') 85c2aa98e2SPeter Wemm { 86c2aa98e2SPeter Wemm /* quote \ or " */ 87c2aa98e2SPeter Wemm if (c == '\\' || c == '"') 88c2aa98e2SPeter Wemm *q++ = '\\'; 89c2aa98e2SPeter Wemm *q++ = c; 90c2aa98e2SPeter Wemm } 91c2aa98e2SPeter Wemm *q++ = '"'; 92c2aa98e2SPeter Wemm *q = '\0'; 93c2aa98e2SPeter Wemm return r; 94c2aa98e2SPeter Wemm } 9513bd1963SGregory Neil Shapiro 9613bd1963SGregory Neil Shapiro /* 97b6bacd31SGregory Neil Shapiro ** STRIPBACKSLASH -- Strip all leading backslashes from a string, provided 98b6bacd31SGregory Neil Shapiro ** the following character is alpha-numerical. 9913bd1963SGregory Neil Shapiro ** 10013bd1963SGregory Neil Shapiro ** This is done in place. 10113bd1963SGregory Neil Shapiro ** 10213bd1963SGregory Neil Shapiro ** Parameters: 10313bd1963SGregory Neil Shapiro ** s -- the string to strip. 10413bd1963SGregory Neil Shapiro ** 10513bd1963SGregory Neil Shapiro ** Returns: 10613bd1963SGregory Neil Shapiro ** none. 10713bd1963SGregory Neil Shapiro */ 10813bd1963SGregory Neil Shapiro 10913bd1963SGregory Neil Shapiro void 11013bd1963SGregory Neil Shapiro stripbackslash(s) 11113bd1963SGregory Neil Shapiro char *s; 11213bd1963SGregory Neil Shapiro { 11313bd1963SGregory Neil Shapiro char *p, *q, c; 11413bd1963SGregory Neil Shapiro 11513bd1963SGregory Neil Shapiro if (s == NULL || *s == '\0') 11613bd1963SGregory Neil Shapiro return; 11713bd1963SGregory Neil Shapiro p = q = s; 11813bd1963SGregory Neil Shapiro while (*p == '\\' && (p[1] == '\\' || (isascii(p[1]) && isalnum(p[1])))) 11913bd1963SGregory Neil Shapiro p++; 12013bd1963SGregory Neil Shapiro do 12113bd1963SGregory Neil Shapiro { 12213bd1963SGregory Neil Shapiro c = *q++ = *p++; 12313bd1963SGregory Neil Shapiro } while (c != '\0'); 12413bd1963SGregory Neil Shapiro } 12513bd1963SGregory Neil Shapiro 12640266059SGregory Neil Shapiro /* 127c2aa98e2SPeter Wemm ** RFC822_STRING -- Checks string for proper RFC822 string quoting. 128c2aa98e2SPeter Wemm ** 129c2aa98e2SPeter Wemm ** Runs through a string and verifies RFC822 special characters 130c2aa98e2SPeter Wemm ** are only found inside comments, quoted strings, or backslash 131c2aa98e2SPeter Wemm ** escaped. Also verified balanced quotes and parenthesis. 132c2aa98e2SPeter Wemm ** 133c2aa98e2SPeter Wemm ** Parameters: 134c2aa98e2SPeter Wemm ** s -- the string to modify. 135c2aa98e2SPeter Wemm ** 136c2aa98e2SPeter Wemm ** Returns: 13740266059SGregory Neil Shapiro ** true iff the string is RFC822 compliant, false otherwise. 138c2aa98e2SPeter Wemm */ 139c2aa98e2SPeter Wemm 140c2aa98e2SPeter Wemm bool 141c2aa98e2SPeter Wemm rfc822_string(s) 142c2aa98e2SPeter Wemm char *s; 143c2aa98e2SPeter Wemm { 14440266059SGregory Neil Shapiro bool quoted = false; 145c2aa98e2SPeter Wemm int commentlev = 0; 146c2aa98e2SPeter Wemm char *c = s; 147c2aa98e2SPeter Wemm 148c2aa98e2SPeter Wemm if (s == NULL) 14940266059SGregory Neil Shapiro return false; 150c2aa98e2SPeter Wemm 151c2aa98e2SPeter Wemm while (*c != '\0') 152c2aa98e2SPeter Wemm { 153c2aa98e2SPeter Wemm /* escaped character */ 154c2aa98e2SPeter Wemm if (*c == '\\') 155c2aa98e2SPeter Wemm { 156c2aa98e2SPeter Wemm c++; 157c2aa98e2SPeter Wemm if (*c == '\0') 15840266059SGregory Neil Shapiro return false; 159c2aa98e2SPeter Wemm } 160c2aa98e2SPeter Wemm else if (commentlev == 0 && *c == '"') 161c2aa98e2SPeter Wemm quoted = !quoted; 162c2aa98e2SPeter Wemm else if (!quoted) 163c2aa98e2SPeter Wemm { 164c2aa98e2SPeter Wemm if (*c == ')') 165c2aa98e2SPeter Wemm { 166c2aa98e2SPeter Wemm /* unbalanced ')' */ 167c2aa98e2SPeter Wemm if (commentlev == 0) 16840266059SGregory Neil Shapiro return false; 169c2aa98e2SPeter Wemm else 170c2aa98e2SPeter Wemm commentlev--; 171c2aa98e2SPeter Wemm } 172c2aa98e2SPeter Wemm else if (*c == '(') 173c2aa98e2SPeter Wemm commentlev++; 174c2aa98e2SPeter Wemm else if (commentlev == 0 && 175c2aa98e2SPeter Wemm strchr(MustQuoteChars, *c) != NULL) 17640266059SGregory Neil Shapiro return false; 177c2aa98e2SPeter Wemm } 178c2aa98e2SPeter Wemm c++; 179c2aa98e2SPeter Wemm } 18040266059SGregory Neil Shapiro 181c2aa98e2SPeter Wemm /* unbalanced '"' or '(' */ 18240266059SGregory Neil Shapiro return !quoted && commentlev == 0; 183c2aa98e2SPeter Wemm } 184d0cef73dSGregory Neil Shapiro 18540266059SGregory Neil Shapiro /* 186065a643dSPeter Wemm ** SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string 187065a643dSPeter Wemm ** 18806f25ae9SGregory Neil Shapiro ** Arbitrarily shorten (in place) an RFC822 string and rebalance 189065a643dSPeter Wemm ** comments and quotes. 190065a643dSPeter Wemm ** 191065a643dSPeter Wemm ** Parameters: 192065a643dSPeter Wemm ** string -- the string to shorten 193065a643dSPeter Wemm ** length -- the maximum size, 0 if no maximum 194065a643dSPeter Wemm ** 195065a643dSPeter Wemm ** Returns: 19640266059SGregory Neil Shapiro ** true if string is changed, false otherwise 197065a643dSPeter Wemm ** 198065a643dSPeter Wemm ** Side Effects: 199065a643dSPeter Wemm ** Changes string in place, possibly resulting 200065a643dSPeter Wemm ** in a shorter string. 201065a643dSPeter Wemm */ 202065a643dSPeter Wemm 203065a643dSPeter Wemm bool 204065a643dSPeter Wemm shorten_rfc822_string(string, length) 205065a643dSPeter Wemm char *string; 206065a643dSPeter Wemm size_t length; 207065a643dSPeter Wemm { 20840266059SGregory Neil Shapiro bool backslash = false; 20940266059SGregory Neil Shapiro bool modified = false; 21040266059SGregory Neil Shapiro bool quoted = false; 211065a643dSPeter Wemm size_t slen; 212065a643dSPeter Wemm int parencount = 0; 213065a643dSPeter Wemm char *ptr = string; 214065a643dSPeter Wemm 215065a643dSPeter Wemm /* 216065a643dSPeter Wemm ** If have to rebalance an already short enough string, 217065a643dSPeter Wemm ** need to do it within allocated space. 218065a643dSPeter Wemm */ 219193538b7SGregory Neil Shapiro 220065a643dSPeter Wemm slen = strlen(string); 221065a643dSPeter Wemm if (length == 0 || slen < length) 222065a643dSPeter Wemm length = slen; 223065a643dSPeter Wemm 224065a643dSPeter Wemm while (*ptr != '\0') 225065a643dSPeter Wemm { 226065a643dSPeter Wemm if (backslash) 227065a643dSPeter Wemm { 22840266059SGregory Neil Shapiro backslash = false; 229065a643dSPeter Wemm goto increment; 230065a643dSPeter Wemm } 231065a643dSPeter Wemm 232065a643dSPeter Wemm if (*ptr == '\\') 23340266059SGregory Neil Shapiro backslash = true; 234065a643dSPeter Wemm else if (*ptr == '(') 235065a643dSPeter Wemm { 236065a643dSPeter Wemm if (!quoted) 237065a643dSPeter Wemm parencount++; 238065a643dSPeter Wemm } 239065a643dSPeter Wemm else if (*ptr == ')') 240065a643dSPeter Wemm { 241065a643dSPeter Wemm if (--parencount < 0) 242065a643dSPeter Wemm parencount = 0; 243065a643dSPeter Wemm } 244065a643dSPeter Wemm 245065a643dSPeter Wemm /* Inside a comment, quotes don't matter */ 246065a643dSPeter Wemm if (parencount <= 0 && *ptr == '"') 247065a643dSPeter Wemm quoted = !quoted; 248065a643dSPeter Wemm 249065a643dSPeter Wemm increment: 250065a643dSPeter Wemm /* Check for sufficient space for next character */ 25106f25ae9SGregory Neil Shapiro if (length - (ptr - string) <= (size_t) ((backslash ? 1 : 0) + 252065a643dSPeter Wemm parencount + 253065a643dSPeter Wemm (quoted ? 1 : 0))) 254065a643dSPeter Wemm { 255065a643dSPeter Wemm /* Not enough, backtrack */ 256065a643dSPeter Wemm if (*ptr == '\\') 25740266059SGregory Neil Shapiro backslash = false; 258065a643dSPeter Wemm else if (*ptr == '(' && !quoted) 259065a643dSPeter Wemm parencount--; 260065a643dSPeter Wemm else if (*ptr == '"' && parencount == 0) 26140266059SGregory Neil Shapiro quoted = false; 262065a643dSPeter Wemm break; 263065a643dSPeter Wemm } 264065a643dSPeter Wemm ptr++; 265065a643dSPeter Wemm } 266065a643dSPeter Wemm 267065a643dSPeter Wemm /* Rebalance */ 268065a643dSPeter Wemm while (parencount-- > 0) 269065a643dSPeter Wemm { 270065a643dSPeter Wemm if (*ptr != ')') 271065a643dSPeter Wemm { 27240266059SGregory Neil Shapiro modified = true; 273065a643dSPeter Wemm *ptr = ')'; 274065a643dSPeter Wemm } 275065a643dSPeter Wemm ptr++; 276065a643dSPeter Wemm } 277065a643dSPeter Wemm if (quoted) 278065a643dSPeter Wemm { 279065a643dSPeter Wemm if (*ptr != '"') 280065a643dSPeter Wemm { 28140266059SGregory Neil Shapiro modified = true; 282065a643dSPeter Wemm *ptr = '"'; 283065a643dSPeter Wemm } 284065a643dSPeter Wemm ptr++; 285065a643dSPeter Wemm } 286065a643dSPeter Wemm if (*ptr != '\0') 287065a643dSPeter Wemm { 28840266059SGregory Neil Shapiro modified = true; 289065a643dSPeter Wemm *ptr = '\0'; 290065a643dSPeter Wemm } 291065a643dSPeter Wemm return modified; 292065a643dSPeter Wemm } 293d0cef73dSGregory Neil Shapiro 29440266059SGregory Neil Shapiro /* 295065a643dSPeter Wemm ** FIND_CHARACTER -- find an unquoted character in an RFC822 string 296065a643dSPeter Wemm ** 297065a643dSPeter Wemm ** Find an unquoted, non-commented character in an RFC822 298065a643dSPeter Wemm ** string and return a pointer to its location in the 299065a643dSPeter Wemm ** string. 300065a643dSPeter Wemm ** 301065a643dSPeter Wemm ** Parameters: 302065a643dSPeter Wemm ** string -- the string to search 303065a643dSPeter Wemm ** character -- the character to find 304065a643dSPeter Wemm ** 305065a643dSPeter Wemm ** Returns: 306065a643dSPeter Wemm ** pointer to the character, or 307065a643dSPeter Wemm ** a pointer to the end of the line if character is not found 308065a643dSPeter Wemm */ 309065a643dSPeter Wemm 310065a643dSPeter Wemm char * 311065a643dSPeter Wemm find_character(string, character) 312065a643dSPeter Wemm char *string; 31306f25ae9SGregory Neil Shapiro int character; 314065a643dSPeter Wemm { 31540266059SGregory Neil Shapiro bool backslash = false; 31640266059SGregory Neil Shapiro bool quoted = false; 317065a643dSPeter Wemm int parencount = 0; 318065a643dSPeter Wemm 319065a643dSPeter Wemm while (string != NULL && *string != '\0') 320065a643dSPeter Wemm { 321065a643dSPeter Wemm if (backslash) 322065a643dSPeter Wemm { 32340266059SGregory Neil Shapiro backslash = false; 324065a643dSPeter Wemm if (!quoted && character == '\\' && *string == '\\') 325065a643dSPeter Wemm break; 326065a643dSPeter Wemm string++; 327065a643dSPeter Wemm continue; 328065a643dSPeter Wemm } 329065a643dSPeter Wemm switch (*string) 330065a643dSPeter Wemm { 331065a643dSPeter Wemm case '\\': 33240266059SGregory Neil Shapiro backslash = true; 333065a643dSPeter Wemm break; 334065a643dSPeter Wemm 335065a643dSPeter Wemm case '(': 336065a643dSPeter Wemm if (!quoted) 337065a643dSPeter Wemm parencount++; 338065a643dSPeter Wemm break; 339065a643dSPeter Wemm 340065a643dSPeter Wemm case ')': 341065a643dSPeter Wemm if (--parencount < 0) 342065a643dSPeter Wemm parencount = 0; 343065a643dSPeter Wemm break; 344065a643dSPeter Wemm } 345065a643dSPeter Wemm 346065a643dSPeter Wemm /* Inside a comment, nothing matters */ 347065a643dSPeter Wemm if (parencount > 0) 348065a643dSPeter Wemm { 349065a643dSPeter Wemm string++; 350065a643dSPeter Wemm continue; 351065a643dSPeter Wemm } 352065a643dSPeter Wemm 353065a643dSPeter Wemm if (*string == '"') 354065a643dSPeter Wemm quoted = !quoted; 355065a643dSPeter Wemm else if (*string == character && !quoted) 356065a643dSPeter Wemm break; 357065a643dSPeter Wemm string++; 358065a643dSPeter Wemm } 359065a643dSPeter Wemm 360065a643dSPeter Wemm /* Return pointer to the character */ 361065a643dSPeter Wemm return string; 362065a643dSPeter Wemm } 36340266059SGregory Neil Shapiro 36440266059SGregory Neil Shapiro /* 36540266059SGregory Neil Shapiro ** CHECK_BODYTYPE -- check bodytype parameter 366c2aa98e2SPeter Wemm ** 36740266059SGregory Neil Shapiro ** Parameters: 36840266059SGregory Neil Shapiro ** bodytype -- bodytype parameter 36940266059SGregory Neil Shapiro ** 37040266059SGregory Neil Shapiro ** Returns: 37140266059SGregory Neil Shapiro ** BODYTYPE_* according to parameter 37240266059SGregory Neil Shapiro ** 37340266059SGregory Neil Shapiro */ 37440266059SGregory Neil Shapiro 37540266059SGregory Neil Shapiro int 37640266059SGregory Neil Shapiro check_bodytype(bodytype) 37740266059SGregory Neil Shapiro char *bodytype; 37840266059SGregory Neil Shapiro { 37940266059SGregory Neil Shapiro /* check body type for legality */ 38040266059SGregory Neil Shapiro if (bodytype == NULL) 38140266059SGregory Neil Shapiro return BODYTYPE_NONE; 38240266059SGregory Neil Shapiro if (sm_strcasecmp(bodytype, "7BIT") == 0) 38340266059SGregory Neil Shapiro return BODYTYPE_7BIT; 38440266059SGregory Neil Shapiro if (sm_strcasecmp(bodytype, "8BITMIME") == 0) 38540266059SGregory Neil Shapiro return BODYTYPE_8BITMIME; 38640266059SGregory Neil Shapiro return BODYTYPE_ILLEGAL; 38740266059SGregory Neil Shapiro } 38840266059SGregory Neil Shapiro 38940266059SGregory Neil Shapiro /* 39040266059SGregory Neil Shapiro ** TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..." 39140266059SGregory Neil Shapiro ** 39240266059SGregory Neil Shapiro ** Parameters: 39340266059SGregory Neil Shapiro ** str -- string to truncate 39440266059SGregory Neil Shapiro ** len -- maximum length (including '\0') (0 for unlimited) 39540266059SGregory Neil Shapiro ** delim -- delimiter character 39640266059SGregory Neil Shapiro ** 39740266059SGregory Neil Shapiro ** Returns: 39840266059SGregory Neil Shapiro ** None. 39940266059SGregory Neil Shapiro */ 40040266059SGregory Neil Shapiro 40140266059SGregory Neil Shapiro void 40240266059SGregory Neil Shapiro truncate_at_delim(str, len, delim) 40340266059SGregory Neil Shapiro char *str; 40440266059SGregory Neil Shapiro size_t len; 40540266059SGregory Neil Shapiro int delim; 40640266059SGregory Neil Shapiro { 40740266059SGregory Neil Shapiro char *p; 40840266059SGregory Neil Shapiro 40940266059SGregory Neil Shapiro if (str == NULL || len == 0 || strlen(str) < len) 41040266059SGregory Neil Shapiro return; 41140266059SGregory Neil Shapiro 41240266059SGregory Neil Shapiro *(str + len - 1) = '\0'; 41340266059SGregory Neil Shapiro while ((p = strrchr(str, delim)) != NULL) 41440266059SGregory Neil Shapiro { 41540266059SGregory Neil Shapiro *p = '\0'; 41640266059SGregory Neil Shapiro if (p - str + 4 < len) 41740266059SGregory Neil Shapiro { 418a7ec597cSGregory Neil Shapiro *p++ = (char) delim; 41940266059SGregory Neil Shapiro *p = '\0'; 42040266059SGregory Neil Shapiro (void) sm_strlcat(str, "...", len); 42140266059SGregory Neil Shapiro return; 42240266059SGregory Neil Shapiro } 42340266059SGregory Neil Shapiro } 42440266059SGregory Neil Shapiro 42540266059SGregory Neil Shapiro /* Couldn't find a place to append "..." */ 42640266059SGregory Neil Shapiro if (len > 3) 42740266059SGregory Neil Shapiro (void) sm_strlcpy(str, "...", len); 42840266059SGregory Neil Shapiro else 42940266059SGregory Neil Shapiro str[0] = '\0'; 43040266059SGregory Neil Shapiro } 431d0cef73dSGregory Neil Shapiro 43240266059SGregory Neil Shapiro /* 43340266059SGregory Neil Shapiro ** XALLOC -- Allocate memory, raise an exception on error 434c2aa98e2SPeter Wemm ** 435c2aa98e2SPeter Wemm ** Parameters: 436c2aa98e2SPeter Wemm ** sz -- size of area to allocate. 437c2aa98e2SPeter Wemm ** 438c2aa98e2SPeter Wemm ** Returns: 439c2aa98e2SPeter Wemm ** pointer to data region. 440c2aa98e2SPeter Wemm ** 44140266059SGregory Neil Shapiro ** Exceptions: 44240266059SGregory Neil Shapiro ** SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory 44340266059SGregory Neil Shapiro ** 444c2aa98e2SPeter Wemm ** Side Effects: 445c2aa98e2SPeter Wemm ** Memory is allocated. 446c2aa98e2SPeter Wemm */ 447c2aa98e2SPeter Wemm 448c2aa98e2SPeter Wemm char * 44940266059SGregory Neil Shapiro #if SM_HEAP_CHECK 45040266059SGregory Neil Shapiro xalloc_tagged(sz, file, line) 45140266059SGregory Neil Shapiro register int sz; 45240266059SGregory Neil Shapiro char *file; 45340266059SGregory Neil Shapiro int line; 45440266059SGregory Neil Shapiro #else /* SM_HEAP_CHECK */ 455c2aa98e2SPeter Wemm xalloc(sz) 456c2aa98e2SPeter Wemm register int sz; 45740266059SGregory Neil Shapiro #endif /* SM_HEAP_CHECK */ 458c2aa98e2SPeter Wemm { 459c2aa98e2SPeter Wemm register char *p; 460c2aa98e2SPeter Wemm 4614e4196cbSGregory Neil Shapiro SM_REQUIRE(sz >= 0); 4624e4196cbSGregory Neil Shapiro 463c2aa98e2SPeter Wemm /* some systems can't handle size zero mallocs */ 464c2aa98e2SPeter Wemm if (sz <= 0) 465c2aa98e2SPeter Wemm sz = 1; 466c2aa98e2SPeter Wemm 46740266059SGregory Neil Shapiro /* scaffolding for testing error handling code */ 46840266059SGregory Neil Shapiro sm_xtrap_raise_x(&SmHeapOutOfMemory); 46940266059SGregory Neil Shapiro 47040266059SGregory Neil Shapiro p = sm_malloc_tagged((unsigned) sz, file, line, sm_heap_group()); 471c2aa98e2SPeter Wemm if (p == NULL) 472c2aa98e2SPeter Wemm { 47340266059SGregory Neil Shapiro sm_exc_raise_x(&SmHeapOutOfMemory); 474c2aa98e2SPeter Wemm } 47506f25ae9SGregory Neil Shapiro return p; 476c2aa98e2SPeter Wemm } 477d0cef73dSGregory Neil Shapiro 47840266059SGregory Neil Shapiro /* 479c2aa98e2SPeter Wemm ** COPYPLIST -- copy list of pointers. 480c2aa98e2SPeter Wemm ** 48140266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for lists of 482c2aa98e2SPeter Wemm ** pointers. 483c2aa98e2SPeter Wemm ** 484c2aa98e2SPeter Wemm ** Parameters: 485c2aa98e2SPeter Wemm ** list -- list of pointers to copy. 486c2aa98e2SPeter Wemm ** Must be NULL terminated. 48740266059SGregory Neil Shapiro ** copycont -- if true, copy the contents of the vector 488c2aa98e2SPeter Wemm ** (which must be a string) also. 48940266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage, 49040266059SGregory Neil Shapiro ** or NULL 491c2aa98e2SPeter Wemm ** 492c2aa98e2SPeter Wemm ** Returns: 493c2aa98e2SPeter Wemm ** a copy of 'list'. 494c2aa98e2SPeter Wemm */ 495c2aa98e2SPeter Wemm 496c2aa98e2SPeter Wemm char ** 49740266059SGregory Neil Shapiro copyplist(list, copycont, rpool) 498c2aa98e2SPeter Wemm char **list; 499c2aa98e2SPeter Wemm bool copycont; 50040266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 501c2aa98e2SPeter Wemm { 502c2aa98e2SPeter Wemm register char **vp; 503c2aa98e2SPeter Wemm register char **newvp; 504c2aa98e2SPeter Wemm 505c2aa98e2SPeter Wemm for (vp = list; *vp != NULL; vp++) 506c2aa98e2SPeter Wemm continue; 507c2aa98e2SPeter Wemm 508c2aa98e2SPeter Wemm vp++; 509c2aa98e2SPeter Wemm 510d0cef73dSGregory Neil Shapiro newvp = (char **) sm_rpool_malloc_x(rpool, (vp - list) * sizeof(*vp)); 511d0cef73dSGregory Neil Shapiro memmove((char *) newvp, (char *) list, (int) (vp - list) * sizeof(*vp)); 512c2aa98e2SPeter Wemm 513c2aa98e2SPeter Wemm if (copycont) 514c2aa98e2SPeter Wemm { 515c2aa98e2SPeter Wemm for (vp = newvp; *vp != NULL; vp++) 51640266059SGregory Neil Shapiro *vp = sm_rpool_strdup_x(rpool, *vp); 517c2aa98e2SPeter Wemm } 518c2aa98e2SPeter Wemm 51906f25ae9SGregory Neil Shapiro return newvp; 520c2aa98e2SPeter Wemm } 521d0cef73dSGregory Neil Shapiro 52240266059SGregory Neil Shapiro /* 523c2aa98e2SPeter Wemm ** COPYQUEUE -- copy address queue. 524c2aa98e2SPeter Wemm ** 52540266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for address queues; 52606f25ae9SGregory Neil Shapiro ** addresses marked as QS_IS_DEAD() aren't copied 527c2aa98e2SPeter Wemm ** 528c2aa98e2SPeter Wemm ** Parameters: 529c2aa98e2SPeter Wemm ** addr -- list of address structures to copy. 53040266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage 531c2aa98e2SPeter Wemm ** 532c2aa98e2SPeter Wemm ** Returns: 533c2aa98e2SPeter Wemm ** a copy of 'addr'. 534c2aa98e2SPeter Wemm */ 535c2aa98e2SPeter Wemm 536c2aa98e2SPeter Wemm ADDRESS * 53740266059SGregory Neil Shapiro copyqueue(addr, rpool) 538c2aa98e2SPeter Wemm ADDRESS *addr; 53940266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 540c2aa98e2SPeter Wemm { 541c2aa98e2SPeter Wemm register ADDRESS *newaddr; 542c2aa98e2SPeter Wemm ADDRESS *ret; 543c2aa98e2SPeter Wemm register ADDRESS **tail = &ret; 544c2aa98e2SPeter Wemm 545c2aa98e2SPeter Wemm while (addr != NULL) 546c2aa98e2SPeter Wemm { 54706f25ae9SGregory Neil Shapiro if (!QS_IS_DEAD(addr->q_state)) 548c2aa98e2SPeter Wemm { 54940266059SGregory Neil Shapiro newaddr = (ADDRESS *) sm_rpool_malloc_x(rpool, 550d0cef73dSGregory Neil Shapiro sizeof(*newaddr)); 551c2aa98e2SPeter Wemm STRUCTCOPY(*addr, *newaddr); 552c2aa98e2SPeter Wemm *tail = newaddr; 553c2aa98e2SPeter Wemm tail = &newaddr->q_next; 554c2aa98e2SPeter Wemm } 555c2aa98e2SPeter Wemm addr = addr->q_next; 556c2aa98e2SPeter Wemm } 557c2aa98e2SPeter Wemm *tail = NULL; 558c2aa98e2SPeter Wemm 559c2aa98e2SPeter Wemm return ret; 560c2aa98e2SPeter Wemm } 561d0cef73dSGregory Neil Shapiro 56240266059SGregory Neil Shapiro /* 56306f25ae9SGregory Neil Shapiro ** LOG_SENDMAIL_PID -- record sendmail pid and command line. 56406f25ae9SGregory Neil Shapiro ** 56506f25ae9SGregory Neil Shapiro ** Parameters: 56606f25ae9SGregory Neil Shapiro ** e -- the current envelope. 56706f25ae9SGregory Neil Shapiro ** 56806f25ae9SGregory Neil Shapiro ** Returns: 56906f25ae9SGregory Neil Shapiro ** none. 57006f25ae9SGregory Neil Shapiro ** 57106f25ae9SGregory Neil Shapiro ** Side Effects: 57240266059SGregory Neil Shapiro ** writes pidfile, logs command line. 573e92d3f3fSGregory Neil Shapiro ** keeps file open and locked to prevent overwrite of active file 57406f25ae9SGregory Neil Shapiro */ 57506f25ae9SGregory Neil Shapiro 576e92d3f3fSGregory Neil Shapiro static SM_FILE_T *Pidf = NULL; 577e92d3f3fSGregory Neil Shapiro 57806f25ae9SGregory Neil Shapiro void 57906f25ae9SGregory Neil Shapiro log_sendmail_pid(e) 58006f25ae9SGregory Neil Shapiro ENVELOPE *e; 58106f25ae9SGregory Neil Shapiro { 58206f25ae9SGregory Neil Shapiro long sff; 58394c01205SGregory Neil Shapiro char pidpath[MAXPATHLEN]; 58440266059SGregory Neil Shapiro extern char *CommandLineArgs; 58506f25ae9SGregory Neil Shapiro 58606f25ae9SGregory Neil Shapiro /* write the pid to the log file for posterity */ 587e92d3f3fSGregory Neil Shapiro sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK; 58806f25ae9SGregory Neil Shapiro if (TrustedUid != 0 && RealUid == TrustedUid) 58906f25ae9SGregory Neil Shapiro sff |= SFF_OPENASROOT; 590d0cef73dSGregory Neil Shapiro expand(PidFile, pidpath, sizeof(pidpath), e); 591e92d3f3fSGregory Neil Shapiro Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff); 592e92d3f3fSGregory Neil Shapiro if (Pidf == NULL) 59306f25ae9SGregory Neil Shapiro { 594e92d3f3fSGregory Neil Shapiro if (errno == EWOULDBLOCK) 595e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID, 596e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: file in use by another process", 597e92d3f3fSGregory Neil Shapiro pidpath); 598e92d3f3fSGregory Neil Shapiro else 599e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID, 600e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: %s", 60140266059SGregory Neil Shapiro pidpath, sm_errstring(errno)); 60206f25ae9SGregory Neil Shapiro } 60306f25ae9SGregory Neil Shapiro else 60406f25ae9SGregory Neil Shapiro { 605e92d3f3fSGregory Neil Shapiro PidFilePid = getpid(); 606193538b7SGregory Neil Shapiro 60706f25ae9SGregory Neil Shapiro /* write the process id on line 1 */ 608e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n", 609e92d3f3fSGregory Neil Shapiro (long) PidFilePid); 61006f25ae9SGregory Neil Shapiro 61106f25ae9SGregory Neil Shapiro /* line 2 contains all command line flags */ 612e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n", 61340266059SGregory Neil Shapiro CommandLineArgs); 61406f25ae9SGregory Neil Shapiro 615e92d3f3fSGregory Neil Shapiro /* flush */ 616e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(Pidf, SM_TIME_DEFAULT); 617e92d3f3fSGregory Neil Shapiro 618e92d3f3fSGregory Neil Shapiro /* 619e92d3f3fSGregory Neil Shapiro ** Leave pid file open until process ends 620e92d3f3fSGregory Neil Shapiro ** so it's not overwritten by another 621e92d3f3fSGregory Neil Shapiro ** process. 622e92d3f3fSGregory Neil Shapiro */ 62306f25ae9SGregory Neil Shapiro } 62440266059SGregory Neil Shapiro if (LogLevel > 9) 62540266059SGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs); 62606f25ae9SGregory Neil Shapiro } 627e92d3f3fSGregory Neil Shapiro 628e92d3f3fSGregory Neil Shapiro /* 629e92d3f3fSGregory Neil Shapiro ** CLOSE_SENDMAIL_PID -- close sendmail pid file 630e92d3f3fSGregory Neil Shapiro ** 631e92d3f3fSGregory Neil Shapiro ** Parameters: 632e92d3f3fSGregory Neil Shapiro ** none. 633e92d3f3fSGregory Neil Shapiro ** 634e92d3f3fSGregory Neil Shapiro ** Returns: 635e92d3f3fSGregory Neil Shapiro ** none. 636e92d3f3fSGregory Neil Shapiro */ 637e92d3f3fSGregory Neil Shapiro 638e92d3f3fSGregory Neil Shapiro void 639e92d3f3fSGregory Neil Shapiro close_sendmail_pid() 640e92d3f3fSGregory Neil Shapiro { 641e92d3f3fSGregory Neil Shapiro if (Pidf == NULL) 642e92d3f3fSGregory Neil Shapiro return; 643e92d3f3fSGregory Neil Shapiro 644e92d3f3fSGregory Neil Shapiro (void) sm_io_close(Pidf, SM_TIME_DEFAULT); 645e92d3f3fSGregory Neil Shapiro Pidf = NULL; 646e92d3f3fSGregory Neil Shapiro } 647e92d3f3fSGregory Neil Shapiro 64840266059SGregory Neil Shapiro /* 64906f25ae9SGregory Neil Shapiro ** SET_DELIVERY_MODE -- set and record the delivery mode 65006f25ae9SGregory Neil Shapiro ** 65106f25ae9SGregory Neil Shapiro ** Parameters: 65206f25ae9SGregory Neil Shapiro ** mode -- delivery mode 65306f25ae9SGregory Neil Shapiro ** e -- the current envelope. 65406f25ae9SGregory Neil Shapiro ** 65506f25ae9SGregory Neil Shapiro ** Returns: 65606f25ae9SGregory Neil Shapiro ** none. 65706f25ae9SGregory Neil Shapiro ** 65806f25ae9SGregory Neil Shapiro ** Side Effects: 65940266059SGregory Neil Shapiro ** sets {deliveryMode} macro 66006f25ae9SGregory Neil Shapiro */ 66106f25ae9SGregory Neil Shapiro 66206f25ae9SGregory Neil Shapiro void 66306f25ae9SGregory Neil Shapiro set_delivery_mode(mode, e) 66406f25ae9SGregory Neil Shapiro int mode; 66506f25ae9SGregory Neil Shapiro ENVELOPE *e; 66606f25ae9SGregory Neil Shapiro { 66706f25ae9SGregory Neil Shapiro char buf[2]; 66806f25ae9SGregory Neil Shapiro 66906f25ae9SGregory Neil Shapiro e->e_sendmode = (char) mode; 67006f25ae9SGregory Neil Shapiro buf[0] = (char) mode; 67106f25ae9SGregory Neil Shapiro buf[1] = '\0'; 67240266059SGregory Neil Shapiro macdefine(&e->e_macro, A_TEMP, macid("{deliveryMode}"), buf); 67306f25ae9SGregory Neil Shapiro } 674d0cef73dSGregory Neil Shapiro 67540266059SGregory Neil Shapiro /* 67640266059SGregory Neil Shapiro ** SET_OP_MODE -- set and record the op mode 67740266059SGregory Neil Shapiro ** 67840266059SGregory Neil Shapiro ** Parameters: 67940266059SGregory Neil Shapiro ** mode -- op mode 68040266059SGregory Neil Shapiro ** e -- the current envelope. 68140266059SGregory Neil Shapiro ** 68240266059SGregory Neil Shapiro ** Returns: 68340266059SGregory Neil Shapiro ** none. 68440266059SGregory Neil Shapiro ** 68540266059SGregory Neil Shapiro ** Side Effects: 68640266059SGregory Neil Shapiro ** sets {opMode} macro 68740266059SGregory Neil Shapiro */ 68840266059SGregory Neil Shapiro 68940266059SGregory Neil Shapiro void 69040266059SGregory Neil Shapiro set_op_mode(mode) 69140266059SGregory Neil Shapiro int mode; 69240266059SGregory Neil Shapiro { 69340266059SGregory Neil Shapiro char buf[2]; 69440266059SGregory Neil Shapiro extern ENVELOPE BlankEnvelope; 69540266059SGregory Neil Shapiro 69640266059SGregory Neil Shapiro OpMode = (char) mode; 69740266059SGregory Neil Shapiro buf[0] = (char) mode; 69840266059SGregory Neil Shapiro buf[1] = '\0'; 69940266059SGregory Neil Shapiro macdefine(&BlankEnvelope.e_macro, A_TEMP, MID_OPMODE, buf); 70040266059SGregory Neil Shapiro } 701d0cef73dSGregory Neil Shapiro 70240266059SGregory Neil Shapiro /* 703c2aa98e2SPeter Wemm ** PRINTAV -- print argument vector. 704c2aa98e2SPeter Wemm ** 705c2aa98e2SPeter Wemm ** Parameters: 706e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer. 707c2aa98e2SPeter Wemm ** av -- argument vector. 708c2aa98e2SPeter Wemm ** 709c2aa98e2SPeter Wemm ** Returns: 710c2aa98e2SPeter Wemm ** none. 711c2aa98e2SPeter Wemm ** 712c2aa98e2SPeter Wemm ** Side Effects: 713c2aa98e2SPeter Wemm ** prints av. 714c2aa98e2SPeter Wemm */ 715c2aa98e2SPeter Wemm 716c2aa98e2SPeter Wemm void 717e92d3f3fSGregory Neil Shapiro printav(fp, av) 718e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp; 719d0cef73dSGregory Neil Shapiro char **av; 720c2aa98e2SPeter Wemm { 721c2aa98e2SPeter Wemm while (*av != NULL) 722c2aa98e2SPeter Wemm { 723c2aa98e2SPeter Wemm if (tTd(0, 44)) 72440266059SGregory Neil Shapiro sm_dprintf("\n\t%08lx=", (unsigned long) *av); 725c2aa98e2SPeter Wemm else 726e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, ' '); 727d0cef73dSGregory Neil Shapiro if (tTd(0, 99)) 728d0cef73dSGregory Neil Shapiro sm_dprintf("%s", str2prt(*av++)); 729d0cef73dSGregory Neil Shapiro else 730e92d3f3fSGregory Neil Shapiro xputs(fp, *av++); 731c2aa98e2SPeter Wemm } 732e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\n'); 733c2aa98e2SPeter Wemm } 734d0cef73dSGregory Neil Shapiro 73540266059SGregory Neil Shapiro /* 736c2aa98e2SPeter Wemm ** XPUTS -- put string doing control escapes. 737c2aa98e2SPeter Wemm ** 738c2aa98e2SPeter Wemm ** Parameters: 739e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer. 740c2aa98e2SPeter Wemm ** s -- string to put. 741c2aa98e2SPeter Wemm ** 742c2aa98e2SPeter Wemm ** Returns: 743c2aa98e2SPeter Wemm ** none. 744c2aa98e2SPeter Wemm ** 745c2aa98e2SPeter Wemm ** Side Effects: 746c2aa98e2SPeter Wemm ** output to stdout 747c2aa98e2SPeter Wemm */ 748c2aa98e2SPeter Wemm 749c2aa98e2SPeter Wemm void 750e92d3f3fSGregory Neil Shapiro xputs(fp, s) 751e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp; 752d0cef73dSGregory Neil Shapiro const char *s; 753c2aa98e2SPeter Wemm { 754d0cef73dSGregory Neil Shapiro int c; 755d0cef73dSGregory Neil Shapiro struct metamac *mp; 75640266059SGregory Neil Shapiro bool shiftout = false; 757c2aa98e2SPeter Wemm extern struct metamac MetaMacros[]; 75840266059SGregory Neil Shapiro static SM_DEBUG_T DebugANSI = SM_DEBUG_INITIALIZER("ANSI", 75940266059SGregory Neil Shapiro "@(#)$Debug: ANSI - enable reverse video in debug output $"); 76040266059SGregory Neil Shapiro 76140266059SGregory Neil Shapiro /* 76240266059SGregory Neil Shapiro ** TermEscape is set here, rather than in main(), 76340266059SGregory Neil Shapiro ** because ANSI mode can be turned on or off at any time 76440266059SGregory Neil Shapiro ** if we are in -bt rule testing mode. 76540266059SGregory Neil Shapiro */ 76640266059SGregory Neil Shapiro 76740266059SGregory Neil Shapiro if (sm_debug_unknown(&DebugANSI)) 76840266059SGregory Neil Shapiro { 76940266059SGregory Neil Shapiro if (sm_debug_active(&DebugANSI, 1)) 77040266059SGregory Neil Shapiro { 77140266059SGregory Neil Shapiro TermEscape.te_rv_on = "\033[7m"; 772d0cef73dSGregory Neil Shapiro TermEscape.te_normal = "\033[0m"; 77340266059SGregory Neil Shapiro } 77440266059SGregory Neil Shapiro else 77540266059SGregory Neil Shapiro { 77640266059SGregory Neil Shapiro TermEscape.te_rv_on = ""; 777d0cef73dSGregory Neil Shapiro TermEscape.te_normal = ""; 77840266059SGregory Neil Shapiro } 77940266059SGregory Neil Shapiro } 780c2aa98e2SPeter Wemm 781c2aa98e2SPeter Wemm if (s == NULL) 782c2aa98e2SPeter Wemm { 783e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s<null>%s", 784d0cef73dSGregory Neil Shapiro TermEscape.te_rv_on, TermEscape.te_normal); 785c2aa98e2SPeter Wemm return; 786c2aa98e2SPeter Wemm } 787c2aa98e2SPeter Wemm while ((c = (*s++ & 0377)) != '\0') 788c2aa98e2SPeter Wemm { 789c2aa98e2SPeter Wemm if (shiftout) 790c2aa98e2SPeter Wemm { 791e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 792d0cef73dSGregory Neil Shapiro TermEscape.te_normal); 79340266059SGregory Neil Shapiro shiftout = false; 794c2aa98e2SPeter Wemm } 795d0cef73dSGregory Neil Shapiro if (!isascii(c) && !tTd(84, 1)) 796c2aa98e2SPeter Wemm { 797c2aa98e2SPeter Wemm if (c == MATCHREPL) 798c2aa98e2SPeter Wemm { 799e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, 80040266059SGregory Neil Shapiro "%s$", 80140266059SGregory Neil Shapiro TermEscape.te_rv_on); 80240266059SGregory Neil Shapiro shiftout = true; 803c2aa98e2SPeter Wemm if (*s == '\0') 804c2aa98e2SPeter Wemm continue; 805c2aa98e2SPeter Wemm c = *s++ & 0377; 806c2aa98e2SPeter Wemm goto printchar; 807c2aa98e2SPeter Wemm } 808c2aa98e2SPeter Wemm if (c == MACROEXPAND || c == MACRODEXPAND) 809c2aa98e2SPeter Wemm { 810e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, 81140266059SGregory Neil Shapiro "%s$", 81240266059SGregory Neil Shapiro TermEscape.te_rv_on); 813c2aa98e2SPeter Wemm if (c == MACRODEXPAND) 814e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, 81540266059SGregory Neil Shapiro SM_TIME_DEFAULT, '&'); 81640266059SGregory Neil Shapiro shiftout = true; 817c2aa98e2SPeter Wemm if (*s == '\0') 818c2aa98e2SPeter Wemm continue; 819c2aa98e2SPeter Wemm if (strchr("=~&?", *s) != NULL) 820e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, 82140266059SGregory Neil Shapiro SM_TIME_DEFAULT, 82240266059SGregory Neil Shapiro *s++); 823c2aa98e2SPeter Wemm if (bitset(0200, *s)) 824e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 82540266059SGregory Neil Shapiro SM_TIME_DEFAULT, 82640266059SGregory Neil Shapiro "{%s}", 82740266059SGregory Neil Shapiro macname(bitidx(*s++))); 828c2aa98e2SPeter Wemm else 829e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 83040266059SGregory Neil Shapiro SM_TIME_DEFAULT, 83140266059SGregory Neil Shapiro "%c", 83240266059SGregory Neil Shapiro *s++); 833c2aa98e2SPeter Wemm continue; 834c2aa98e2SPeter Wemm } 835c2aa98e2SPeter Wemm for (mp = MetaMacros; mp->metaname != '\0'; mp++) 836c2aa98e2SPeter Wemm { 83740266059SGregory Neil Shapiro if (bitidx(mp->metaval) == c) 838c2aa98e2SPeter Wemm { 839e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 84040266059SGregory Neil Shapiro SM_TIME_DEFAULT, 84140266059SGregory Neil Shapiro "%s$%c", 842c2aa98e2SPeter Wemm TermEscape.te_rv_on, 843c2aa98e2SPeter Wemm mp->metaname); 84440266059SGregory Neil Shapiro shiftout = true; 845c2aa98e2SPeter Wemm break; 846c2aa98e2SPeter Wemm } 847c2aa98e2SPeter Wemm } 848c2aa98e2SPeter Wemm if (c == MATCHCLASS || c == MATCHNCLASS) 849c2aa98e2SPeter Wemm { 850c2aa98e2SPeter Wemm if (bitset(0200, *s)) 851e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 85240266059SGregory Neil Shapiro SM_TIME_DEFAULT, 85340266059SGregory Neil Shapiro "{%s}", 85440266059SGregory Neil Shapiro macname(bitidx(*s++))); 855c2aa98e2SPeter Wemm else if (*s != '\0') 856e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 85740266059SGregory Neil Shapiro SM_TIME_DEFAULT, 85840266059SGregory Neil Shapiro "%c", 85940266059SGregory Neil Shapiro *s++); 860c2aa98e2SPeter Wemm } 861c2aa98e2SPeter Wemm if (mp->metaname != '\0') 862c2aa98e2SPeter Wemm continue; 863c2aa98e2SPeter Wemm 864c2aa98e2SPeter Wemm /* unrecognized meta character */ 865e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%sM-", 86640266059SGregory Neil Shapiro TermEscape.te_rv_on); 86740266059SGregory Neil Shapiro shiftout = true; 868c2aa98e2SPeter Wemm c &= 0177; 869c2aa98e2SPeter Wemm } 870c2aa98e2SPeter Wemm printchar: 871c2aa98e2SPeter Wemm if (isprint(c)) 872c2aa98e2SPeter Wemm { 873e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c); 874c2aa98e2SPeter Wemm continue; 875c2aa98e2SPeter Wemm } 876c2aa98e2SPeter Wemm 877c2aa98e2SPeter Wemm /* wasn't a meta-macro -- find another way to print it */ 878c2aa98e2SPeter Wemm switch (c) 879c2aa98e2SPeter Wemm { 880c2aa98e2SPeter Wemm case '\n': 881c2aa98e2SPeter Wemm c = 'n'; 882c2aa98e2SPeter Wemm break; 883c2aa98e2SPeter Wemm 884c2aa98e2SPeter Wemm case '\r': 885c2aa98e2SPeter Wemm c = 'r'; 886c2aa98e2SPeter Wemm break; 887c2aa98e2SPeter Wemm 888c2aa98e2SPeter Wemm case '\t': 889c2aa98e2SPeter Wemm c = 't'; 890c2aa98e2SPeter Wemm break; 891c2aa98e2SPeter Wemm } 892c2aa98e2SPeter Wemm if (!shiftout) 893c2aa98e2SPeter Wemm { 894e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 89540266059SGregory Neil Shapiro TermEscape.te_rv_on); 89640266059SGregory Neil Shapiro shiftout = true; 897c2aa98e2SPeter Wemm } 898c2aa98e2SPeter Wemm if (isprint(c)) 899c2aa98e2SPeter Wemm { 900e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\\'); 901e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c); 902c2aa98e2SPeter Wemm } 903d0cef73dSGregory Neil Shapiro else if (tTd(84, 2)) 904d0cef73dSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %o ", c); 905d0cef73dSGregory Neil Shapiro else if (tTd(84, 1)) 906d0cef73dSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, " %#x ", c); 907d0cef73dSGregory Neil Shapiro else if (!isascii(c) && !tTd(84, 1)) 908c2aa98e2SPeter Wemm { 909e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '^'); 910e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c ^ 0100); 911c2aa98e2SPeter Wemm } 912c2aa98e2SPeter Wemm } 913c2aa98e2SPeter Wemm if (shiftout) 914e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 915d0cef73dSGregory Neil Shapiro TermEscape.te_normal); 916e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(fp, SM_TIME_DEFAULT); 917c2aa98e2SPeter Wemm } 918d0cef73dSGregory Neil Shapiro 91940266059SGregory Neil Shapiro /* 920c2aa98e2SPeter Wemm ** MAKELOWER -- Translate a line into lower case 921c2aa98e2SPeter Wemm ** 922c2aa98e2SPeter Wemm ** Parameters: 923c2aa98e2SPeter Wemm ** p -- the string to translate. If NULL, return is 924c2aa98e2SPeter Wemm ** immediate. 925c2aa98e2SPeter Wemm ** 926c2aa98e2SPeter Wemm ** Returns: 927c2aa98e2SPeter Wemm ** none. 928c2aa98e2SPeter Wemm ** 929c2aa98e2SPeter Wemm ** Side Effects: 930c2aa98e2SPeter Wemm ** String pointed to by p is translated to lower case. 931c2aa98e2SPeter Wemm */ 932c2aa98e2SPeter Wemm 933c2aa98e2SPeter Wemm void 934c2aa98e2SPeter Wemm makelower(p) 935c2aa98e2SPeter Wemm register char *p; 936c2aa98e2SPeter Wemm { 937c2aa98e2SPeter Wemm register char c; 938c2aa98e2SPeter Wemm 939c2aa98e2SPeter Wemm if (p == NULL) 940c2aa98e2SPeter Wemm return; 941c2aa98e2SPeter Wemm for (; (c = *p) != '\0'; p++) 942c2aa98e2SPeter Wemm if (isascii(c) && isupper(c)) 943c2aa98e2SPeter Wemm *p = tolower(c); 944c2aa98e2SPeter Wemm } 945d0cef73dSGregory Neil Shapiro 94640266059SGregory Neil Shapiro /* 947c2aa98e2SPeter Wemm ** FIXCRLF -- fix <CR><LF> in line. 948c2aa98e2SPeter Wemm ** 949c2aa98e2SPeter Wemm ** Looks for the <CR><LF> combination and turns it into the 950c2aa98e2SPeter Wemm ** UNIX canonical <NL> character. It only takes one line, 951c2aa98e2SPeter Wemm ** i.e., it is assumed that the first <NL> found is the end 952c2aa98e2SPeter Wemm ** of the line. 953c2aa98e2SPeter Wemm ** 954c2aa98e2SPeter Wemm ** Parameters: 955c2aa98e2SPeter Wemm ** line -- the line to fix. 956c2aa98e2SPeter Wemm ** stripnl -- if true, strip the newline also. 957c2aa98e2SPeter Wemm ** 958c2aa98e2SPeter Wemm ** Returns: 959c2aa98e2SPeter Wemm ** none. 960c2aa98e2SPeter Wemm ** 961c2aa98e2SPeter Wemm ** Side Effects: 962c2aa98e2SPeter Wemm ** line is changed in place. 963c2aa98e2SPeter Wemm */ 964c2aa98e2SPeter Wemm 965c2aa98e2SPeter Wemm void 966c2aa98e2SPeter Wemm fixcrlf(line, stripnl) 967c2aa98e2SPeter Wemm char *line; 968c2aa98e2SPeter Wemm bool stripnl; 969c2aa98e2SPeter Wemm { 970c2aa98e2SPeter Wemm register char *p; 971c2aa98e2SPeter Wemm 972c2aa98e2SPeter Wemm p = strchr(line, '\n'); 973c2aa98e2SPeter Wemm if (p == NULL) 974c2aa98e2SPeter Wemm return; 975c2aa98e2SPeter Wemm if (p > line && p[-1] == '\r') 976c2aa98e2SPeter Wemm p--; 977c2aa98e2SPeter Wemm if (!stripnl) 978c2aa98e2SPeter Wemm *p++ = '\n'; 979c2aa98e2SPeter Wemm *p = '\0'; 980c2aa98e2SPeter Wemm } 981d0cef73dSGregory Neil Shapiro 98240266059SGregory Neil Shapiro /* 983c2aa98e2SPeter Wemm ** PUTLINE -- put a line like fputs obeying SMTP conventions 984c2aa98e2SPeter Wemm ** 985c2aa98e2SPeter Wemm ** This routine always guarantees outputing a newline (or CRLF, 986c2aa98e2SPeter Wemm ** as appropriate) at the end of the string. 987c2aa98e2SPeter Wemm ** 988c2aa98e2SPeter Wemm ** Parameters: 989c2aa98e2SPeter Wemm ** l -- line to put. 990c2aa98e2SPeter Wemm ** mci -- the mailer connection information. 991c2aa98e2SPeter Wemm ** 992c2aa98e2SPeter Wemm ** Returns: 9934e4196cbSGregory Neil Shapiro ** true iff line was written successfully 994c2aa98e2SPeter Wemm ** 995c2aa98e2SPeter Wemm ** Side Effects: 99640266059SGregory Neil Shapiro ** output of l to mci->mci_out. 997c2aa98e2SPeter Wemm */ 998c2aa98e2SPeter Wemm 9994e4196cbSGregory Neil Shapiro bool 1000c2aa98e2SPeter Wemm putline(l, mci) 1001c2aa98e2SPeter Wemm register char *l; 1002c2aa98e2SPeter Wemm register MCI *mci; 1003c2aa98e2SPeter Wemm { 10044e4196cbSGregory Neil Shapiro return putxline(l, strlen(l), mci, PXLF_MAPFROM); 1005c2aa98e2SPeter Wemm } 1006d0cef73dSGregory Neil Shapiro 100740266059SGregory Neil Shapiro /* 1008c2aa98e2SPeter Wemm ** PUTXLINE -- putline with flags bits. 1009c2aa98e2SPeter Wemm ** 1010c2aa98e2SPeter Wemm ** This routine always guarantees outputing a newline (or CRLF, 1011c2aa98e2SPeter Wemm ** as appropriate) at the end of the string. 1012c2aa98e2SPeter Wemm ** 1013c2aa98e2SPeter Wemm ** Parameters: 1014c2aa98e2SPeter Wemm ** l -- line to put. 1015c2aa98e2SPeter Wemm ** len -- the length of the line. 1016c2aa98e2SPeter Wemm ** mci -- the mailer connection information. 1017c2aa98e2SPeter Wemm ** pxflags -- flag bits: 1018c2aa98e2SPeter Wemm ** PXLF_MAPFROM -- map From_ to >From_. 1019c2aa98e2SPeter Wemm ** PXLF_STRIP8BIT -- strip 8th bit. 1020c2aa98e2SPeter Wemm ** PXLF_HEADER -- map bare newline in header to newline space. 1021605302a5SGregory Neil Shapiro ** PXLF_NOADDEOL -- don't add an EOL if one wasn't present. 1022d0cef73dSGregory Neil Shapiro ** PXLF_STRIPMQUOTE -- strip METAQUOTE bytes. 1023c2aa98e2SPeter Wemm ** 1024c2aa98e2SPeter Wemm ** Returns: 10254e4196cbSGregory Neil Shapiro ** true iff line was written successfully 1026c2aa98e2SPeter Wemm ** 1027c2aa98e2SPeter Wemm ** Side Effects: 102840266059SGregory Neil Shapiro ** output of l to mci->mci_out. 1029c2aa98e2SPeter Wemm */ 1030c2aa98e2SPeter Wemm 1031d0cef73dSGregory Neil Shapiro 1032d0cef73dSGregory Neil Shapiro #define PUTX(limit) \ 1033d0cef73dSGregory Neil Shapiro do \ 1034d0cef73dSGregory Neil Shapiro { \ 1035d0cef73dSGregory Neil Shapiro quotenext = false; \ 1036d0cef73dSGregory Neil Shapiro while (l < limit) \ 1037d0cef73dSGregory Neil Shapiro { \ 1038d0cef73dSGregory Neil Shapiro unsigned char c = (unsigned char) *l++; \ 1039d0cef73dSGregory Neil Shapiro \ 1040d0cef73dSGregory Neil Shapiro if (bitset(PXLF_STRIPMQUOTE, pxflags) && \ 1041d0cef73dSGregory Neil Shapiro !quotenext && c == METAQUOTE) \ 1042d0cef73dSGregory Neil Shapiro { \ 1043d0cef73dSGregory Neil Shapiro quotenext = true; \ 1044d0cef73dSGregory Neil Shapiro continue; \ 1045d0cef73dSGregory Neil Shapiro } \ 1046d0cef73dSGregory Neil Shapiro quotenext = false; \ 1047d0cef73dSGregory Neil Shapiro if (strip8bit) \ 1048d0cef73dSGregory Neil Shapiro c &= 0177; \ 1049d0cef73dSGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, \ 1050d0cef73dSGregory Neil Shapiro c) == SM_IO_EOF) \ 1051d0cef73dSGregory Neil Shapiro { \ 1052d0cef73dSGregory Neil Shapiro dead = true; \ 1053d0cef73dSGregory Neil Shapiro break; \ 1054d0cef73dSGregory Neil Shapiro } \ 1055d0cef73dSGregory Neil Shapiro if (TrafficLogFile != NULL) \ 1056d0cef73dSGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, \ 1057d0cef73dSGregory Neil Shapiro SM_TIME_DEFAULT, \ 1058d0cef73dSGregory Neil Shapiro c); \ 1059d0cef73dSGregory Neil Shapiro } \ 1060d0cef73dSGregory Neil Shapiro } while (0) 1061d0cef73dSGregory Neil Shapiro 10624e4196cbSGregory Neil Shapiro bool 1063c2aa98e2SPeter Wemm putxline(l, len, mci, pxflags) 1064c2aa98e2SPeter Wemm register char *l; 1065c2aa98e2SPeter Wemm size_t len; 1066c2aa98e2SPeter Wemm register MCI *mci; 1067c2aa98e2SPeter Wemm int pxflags; 1068c2aa98e2SPeter Wemm { 1069c2aa98e2SPeter Wemm register char *p, *end; 1070d0cef73dSGregory Neil Shapiro int slop; 1071d0cef73dSGregory Neil Shapiro bool dead, quotenext, strip8bit; 1072c2aa98e2SPeter Wemm 1073c2aa98e2SPeter Wemm /* strip out 0200 bits -- these can look like TELNET protocol */ 1074d0cef73dSGregory Neil Shapiro strip8bit = bitset(MCIF_7BIT, mci->mci_flags) || 1075d0cef73dSGregory Neil Shapiro bitset(PXLF_STRIP8BIT, pxflags); 1076d0cef73dSGregory Neil Shapiro dead = false; 1077d0cef73dSGregory Neil Shapiro slop = 0; 1078c2aa98e2SPeter Wemm 1079c2aa98e2SPeter Wemm end = l + len; 1080c2aa98e2SPeter Wemm do 1081c2aa98e2SPeter Wemm { 1082605302a5SGregory Neil Shapiro bool noeol = false; 1083605302a5SGregory Neil Shapiro 1084c2aa98e2SPeter Wemm /* find the end of the line */ 1085c2aa98e2SPeter Wemm p = memchr(l, '\n', end - l); 1086c2aa98e2SPeter Wemm if (p == NULL) 1087605302a5SGregory Neil Shapiro { 1088c2aa98e2SPeter Wemm p = end; 1089605302a5SGregory Neil Shapiro noeol = true; 1090605302a5SGregory Neil Shapiro } 1091c2aa98e2SPeter Wemm 1092c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 109340266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 109440266059SGregory Neil Shapiro "%05d >>> ", (int) CurrentPid); 1095c2aa98e2SPeter Wemm 1096c2aa98e2SPeter Wemm /* check for line overflow */ 1097c2aa98e2SPeter Wemm while (mci->mci_mailer->m_linelimit > 0 && 1098c2aa98e2SPeter Wemm (p - l + slop) > mci->mci_mailer->m_linelimit) 1099c2aa98e2SPeter Wemm { 1100c2aa98e2SPeter Wemm register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 1101c2aa98e2SPeter Wemm 1102c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 && 1103c2aa98e2SPeter Wemm bitnset(M_XDOT, mci->mci_mailer->m_flags)) 1104c2aa98e2SPeter Wemm { 110540266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 110640266059SGregory Neil Shapiro '.') == SM_IO_EOF) 110740266059SGregory Neil Shapiro dead = true; 1108c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 110940266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 111040266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.'); 1111c2aa98e2SPeter Wemm } 1112c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 && 1113c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) && 1114c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 && 1115c2aa98e2SPeter Wemm bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 1116c2aa98e2SPeter Wemm { 111740266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 111840266059SGregory Neil Shapiro '>') == SM_IO_EOF) 111940266059SGregory Neil Shapiro dead = true; 1120c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 112140266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 112240266059SGregory Neil Shapiro SM_TIME_DEFAULT, 112340266059SGregory Neil Shapiro '>'); 1124c2aa98e2SPeter Wemm } 112506f25ae9SGregory Neil Shapiro if (dead) 112606f25ae9SGregory Neil Shapiro break; 112706f25ae9SGregory Neil Shapiro 1128d0cef73dSGregory Neil Shapiro PUTX(q); 112906f25ae9SGregory Neil Shapiro if (dead) 113006f25ae9SGregory Neil Shapiro break; 113106f25ae9SGregory Neil Shapiro 1132d0cef73dSGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 1133d0cef73dSGregory Neil Shapiro '!') == SM_IO_EOF || 113440266059SGregory Neil Shapiro sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT, 1135d0cef73dSGregory Neil Shapiro mci->mci_mailer->m_eol) == SM_IO_EOF || 1136d0cef73dSGregory Neil Shapiro sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 1137d0cef73dSGregory Neil Shapiro ' ') == SM_IO_EOF) 113806f25ae9SGregory Neil Shapiro { 113940266059SGregory Neil Shapiro dead = true; 114006f25ae9SGregory Neil Shapiro break; 114106f25ae9SGregory Neil Shapiro } 1142c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 1143c2aa98e2SPeter Wemm { 114440266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, 114540266059SGregory Neil Shapiro SM_TIME_DEFAULT, 114640266059SGregory Neil Shapiro "!\n%05d >>> ", 114740266059SGregory Neil Shapiro (int) CurrentPid); 1148c2aa98e2SPeter Wemm } 1149c2aa98e2SPeter Wemm slop = 1; 1150c2aa98e2SPeter Wemm } 1151c2aa98e2SPeter Wemm 115206f25ae9SGregory Neil Shapiro if (dead) 115306f25ae9SGregory Neil Shapiro break; 115406f25ae9SGregory Neil Shapiro 1155c2aa98e2SPeter Wemm /* output last part */ 1156c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 && 1157ffb83623SGregory Neil Shapiro bitnset(M_XDOT, mci->mci_mailer->m_flags) && 1158ffb83623SGregory Neil Shapiro !bitset(MCIF_INLONGLINE, mci->mci_flags)) 1159c2aa98e2SPeter Wemm { 116040266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '.') == 116140266059SGregory Neil Shapiro SM_IO_EOF) 1162193538b7SGregory Neil Shapiro { 11634e4196cbSGregory Neil Shapiro dead = true; 11644e4196cbSGregory Neil Shapiro break; 1165193538b7SGregory Neil Shapiro } 1166c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 116740266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 116840266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.'); 1169c2aa98e2SPeter Wemm } 1170c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 && 1171c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) && 1172c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 && 1173ffb83623SGregory Neil Shapiro bitnset(M_ESCFROM, mci->mci_mailer->m_flags) && 1174ffb83623SGregory Neil Shapiro !bitset(MCIF_INLONGLINE, mci->mci_flags)) 1175c2aa98e2SPeter Wemm { 117640266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '>') == 117740266059SGregory Neil Shapiro SM_IO_EOF) 1178193538b7SGregory Neil Shapiro { 11794e4196cbSGregory Neil Shapiro dead = true; 11804e4196cbSGregory Neil Shapiro break; 1181193538b7SGregory Neil Shapiro } 1182c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 118340266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 118440266059SGregory Neil Shapiro SM_TIME_DEFAULT, '>'); 1185c2aa98e2SPeter Wemm } 1186d0cef73dSGregory Neil Shapiro PUTX(p); 118706f25ae9SGregory Neil Shapiro if (dead) 118806f25ae9SGregory Neil Shapiro break; 118906f25ae9SGregory Neil Shapiro 1190c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 119140266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, SM_TIME_DEFAULT, 119240266059SGregory Neil Shapiro '\n'); 1193ffb83623SGregory Neil Shapiro if ((!bitset(PXLF_NOADDEOL, pxflags) || !noeol)) 1194ffb83623SGregory Neil Shapiro { 1195ffb83623SGregory Neil Shapiro mci->mci_flags &= ~MCIF_INLONGLINE; 1196ffb83623SGregory Neil Shapiro if (sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT, 119740266059SGregory Neil Shapiro mci->mci_mailer->m_eol) == SM_IO_EOF) 1198193538b7SGregory Neil Shapiro { 11994e4196cbSGregory Neil Shapiro dead = true; 12004e4196cbSGregory Neil Shapiro break; 1201193538b7SGregory Neil Shapiro } 1202ffb83623SGregory Neil Shapiro } 1203ffb83623SGregory Neil Shapiro else 1204ffb83623SGregory Neil Shapiro mci->mci_flags |= MCIF_INLONGLINE; 1205ffb83623SGregory Neil Shapiro 1206c2aa98e2SPeter Wemm if (l < end && *l == '\n') 1207c2aa98e2SPeter Wemm { 1208c2aa98e2SPeter Wemm if (*++l != ' ' && *l != '\t' && *l != '\0' && 1209c2aa98e2SPeter Wemm bitset(PXLF_HEADER, pxflags)) 1210c2aa98e2SPeter Wemm { 121140266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 121240266059SGregory Neil Shapiro ' ') == SM_IO_EOF) 1213193538b7SGregory Neil Shapiro { 12144e4196cbSGregory Neil Shapiro dead = true; 12154e4196cbSGregory Neil Shapiro break; 1216193538b7SGregory Neil Shapiro } 121740266059SGregory Neil Shapiro 1218c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 121940266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 122040266059SGregory Neil Shapiro SM_TIME_DEFAULT, ' '); 1221c2aa98e2SPeter Wemm } 1222c2aa98e2SPeter Wemm } 122340266059SGregory Neil Shapiro 1224c2aa98e2SPeter Wemm } while (l < end); 12254e4196cbSGregory Neil Shapiro return !dead; 1226c2aa98e2SPeter Wemm } 12274e4196cbSGregory Neil Shapiro 122840266059SGregory Neil Shapiro /* 1229c2aa98e2SPeter Wemm ** XUNLINK -- unlink a file, doing logging as appropriate. 1230c2aa98e2SPeter Wemm ** 1231c2aa98e2SPeter Wemm ** Parameters: 1232c2aa98e2SPeter Wemm ** f -- name of file to unlink. 1233c2aa98e2SPeter Wemm ** 1234c2aa98e2SPeter Wemm ** Returns: 123540266059SGregory Neil Shapiro ** return value of unlink() 1236c2aa98e2SPeter Wemm ** 1237c2aa98e2SPeter Wemm ** Side Effects: 1238c2aa98e2SPeter Wemm ** f is unlinked. 1239c2aa98e2SPeter Wemm */ 1240c2aa98e2SPeter Wemm 124140266059SGregory Neil Shapiro int 1242c2aa98e2SPeter Wemm xunlink(f) 1243c2aa98e2SPeter Wemm char *f; 1244c2aa98e2SPeter Wemm { 1245c2aa98e2SPeter Wemm register int i; 124640266059SGregory Neil Shapiro int save_errno; 1247c2aa98e2SPeter Wemm 1248c2aa98e2SPeter Wemm if (LogLevel > 98) 124940266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "unlink %s", f); 1250c2aa98e2SPeter Wemm 1251c2aa98e2SPeter Wemm i = unlink(f); 125240266059SGregory Neil Shapiro save_errno = errno; 1253c2aa98e2SPeter Wemm if (i < 0 && LogLevel > 97) 125440266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s: unlink-fail %d", 1255c2aa98e2SPeter Wemm f, errno); 125640266059SGregory Neil Shapiro if (i >= 0) 125740266059SGregory Neil Shapiro SYNC_DIR(f, false); 125840266059SGregory Neil Shapiro errno = save_errno; 125940266059SGregory Neil Shapiro return i; 1260c2aa98e2SPeter Wemm } 1261d0cef73dSGregory Neil Shapiro 126240266059SGregory Neil Shapiro /* 1263c2aa98e2SPeter Wemm ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 1264c2aa98e2SPeter Wemm ** 1265c2aa98e2SPeter Wemm ** Parameters: 1266c2aa98e2SPeter Wemm ** buf -- place to put the input line. 1267c2aa98e2SPeter Wemm ** siz -- size of buf. 1268c2aa98e2SPeter Wemm ** fp -- file to read from. 1269c2aa98e2SPeter Wemm ** timeout -- the timeout before error occurs. 1270c2aa98e2SPeter Wemm ** during -- what we are trying to read (for error messages). 1271c2aa98e2SPeter Wemm ** 1272c2aa98e2SPeter Wemm ** Returns: 127340266059SGregory Neil Shapiro ** NULL on error (including timeout). This may also leave 1274c2aa98e2SPeter Wemm ** buf containing a null string. 1275c2aa98e2SPeter Wemm ** buf otherwise. 1276c2aa98e2SPeter Wemm */ 1277c2aa98e2SPeter Wemm 127806f25ae9SGregory Neil Shapiro 1279c2aa98e2SPeter Wemm char * 1280c2aa98e2SPeter Wemm sfgets(buf, siz, fp, timeout, during) 1281c2aa98e2SPeter Wemm char *buf; 1282c2aa98e2SPeter Wemm int siz; 128340266059SGregory Neil Shapiro SM_FILE_T *fp; 1284c2aa98e2SPeter Wemm time_t timeout; 1285c2aa98e2SPeter Wemm char *during; 1286c2aa98e2SPeter Wemm { 1287c2aa98e2SPeter Wemm register char *p; 12882e43090eSPeter Wemm int save_errno; 128940266059SGregory Neil Shapiro int io_timeout; 129040266059SGregory Neil Shapiro 129140266059SGregory Neil Shapiro SM_REQUIRE(siz > 0); 129240266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL); 1293c2aa98e2SPeter Wemm 1294c2aa98e2SPeter Wemm if (fp == NULL) 1295c2aa98e2SPeter Wemm { 1296c2aa98e2SPeter Wemm buf[0] = '\0'; 129740266059SGregory Neil Shapiro errno = EBADF; 1298c2aa98e2SPeter Wemm return NULL; 1299c2aa98e2SPeter Wemm } 1300c2aa98e2SPeter Wemm 130140266059SGregory Neil Shapiro /* try to read */ 130240266059SGregory Neil Shapiro p = NULL; 130340266059SGregory Neil Shapiro errno = 0; 130440266059SGregory Neil Shapiro 130540266059SGregory Neil Shapiro /* convert the timeout to sm_io notation */ 130640266059SGregory Neil Shapiro io_timeout = (timeout <= 0) ? SM_TIME_DEFAULT : timeout * 1000; 130740266059SGregory Neil Shapiro while (!sm_io_eof(fp) && !sm_io_error(fp)) 1308c2aa98e2SPeter Wemm { 130940266059SGregory Neil Shapiro errno = 0; 131040266059SGregory Neil Shapiro p = sm_io_fgets(fp, io_timeout, buf, siz); 131140266059SGregory Neil Shapiro if (p == NULL && errno == EAGAIN) 1312c2aa98e2SPeter Wemm { 131340266059SGregory Neil Shapiro /* The sm_io_fgets() call timedout */ 1314c2aa98e2SPeter Wemm if (LogLevel > 1) 1315c2aa98e2SPeter Wemm sm_syslog(LOG_NOTICE, CurEnv->e_id, 1316c2aa98e2SPeter Wemm "timeout waiting for input from %.100s during %s", 131740266059SGregory Neil Shapiro CURHOSTNAME, 1318c2aa98e2SPeter Wemm during); 1319c2aa98e2SPeter Wemm buf[0] = '\0'; 1320c2aa98e2SPeter Wemm #if XDEBUG 1321c2aa98e2SPeter Wemm checkfd012(during); 132206f25ae9SGregory Neil Shapiro #endif /* XDEBUG */ 1323c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 132440266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, 132540266059SGregory Neil Shapiro SM_TIME_DEFAULT, 132640266059SGregory Neil Shapiro "%05d <<< [TIMEOUT]\n", 132740266059SGregory Neil Shapiro (int) CurrentPid); 132840266059SGregory Neil Shapiro errno = ETIMEDOUT; 132906f25ae9SGregory Neil Shapiro return NULL; 1330c2aa98e2SPeter Wemm } 1331c2aa98e2SPeter Wemm if (p != NULL || errno != EINTR) 1332c2aa98e2SPeter Wemm break; 133340266059SGregory Neil Shapiro (void) sm_io_clearerr(fp); 1334c2aa98e2SPeter Wemm } 13352e43090eSPeter Wemm save_errno = errno; 1336c2aa98e2SPeter Wemm 1337c2aa98e2SPeter Wemm /* clean up the books and exit */ 1338c2aa98e2SPeter Wemm LineNumber++; 1339c2aa98e2SPeter Wemm if (p == NULL) 1340c2aa98e2SPeter Wemm { 1341c2aa98e2SPeter Wemm buf[0] = '\0'; 1342c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 134340266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 134440266059SGregory Neil Shapiro "%05d <<< [EOF]\n", 134540266059SGregory Neil Shapiro (int) CurrentPid); 13462e43090eSPeter Wemm errno = save_errno; 134706f25ae9SGregory Neil Shapiro return NULL; 1348c2aa98e2SPeter Wemm } 1349c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 135040266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 135140266059SGregory Neil Shapiro "%05d <<< %s", (int) CurrentPid, buf); 1352c2aa98e2SPeter Wemm if (SevenBitInput) 1353c2aa98e2SPeter Wemm { 1354c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++) 1355c2aa98e2SPeter Wemm *p &= ~0200; 1356c2aa98e2SPeter Wemm } 1357c2aa98e2SPeter Wemm else if (!HasEightBits) 1358c2aa98e2SPeter Wemm { 1359c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++) 1360c2aa98e2SPeter Wemm { 1361c2aa98e2SPeter Wemm if (bitset(0200, *p)) 1362c2aa98e2SPeter Wemm { 136340266059SGregory Neil Shapiro HasEightBits = true; 1364c2aa98e2SPeter Wemm break; 1365c2aa98e2SPeter Wemm } 1366c2aa98e2SPeter Wemm } 1367c2aa98e2SPeter Wemm } 136806f25ae9SGregory Neil Shapiro return buf; 1369c2aa98e2SPeter Wemm } 1370d0cef73dSGregory Neil Shapiro 13718774250cSGregory Neil Shapiro /* 137240266059SGregory Neil Shapiro ** FGETFOLDED -- like fgets, but knows about folded lines. 1373c2aa98e2SPeter Wemm ** 1374c2aa98e2SPeter Wemm ** Parameters: 1375c2aa98e2SPeter Wemm ** buf -- place to put result. 1376d0cef73dSGregory Neil Shapiro ** np -- pointer to bytes available; will be updated with 1377d0cef73dSGregory Neil Shapiro ** the actual buffer size (not number of bytes filled) 1378d0cef73dSGregory Neil Shapiro ** on return. 1379c2aa98e2SPeter Wemm ** f -- file to read from. 1380c2aa98e2SPeter Wemm ** 1381c2aa98e2SPeter Wemm ** Returns: 138240266059SGregory Neil Shapiro ** input line(s) on success, NULL on error or SM_IO_EOF. 1383c2aa98e2SPeter Wemm ** This will normally be buf -- unless the line is too 138440266059SGregory Neil Shapiro ** long, when it will be sm_malloc_x()ed. 1385c2aa98e2SPeter Wemm ** 1386c2aa98e2SPeter Wemm ** Side Effects: 1387c2aa98e2SPeter Wemm ** buf gets lines from f, with continuation lines (lines 1388c2aa98e2SPeter Wemm ** with leading white space) appended. CRLF's are mapped 1389c2aa98e2SPeter Wemm ** into single newlines. Any trailing NL is stripped. 1390c2aa98e2SPeter Wemm */ 1391c2aa98e2SPeter Wemm 1392c2aa98e2SPeter Wemm char * 1393d0cef73dSGregory Neil Shapiro fgetfolded(buf, np, f) 1394c2aa98e2SPeter Wemm char *buf; 1395d0cef73dSGregory Neil Shapiro int *np; 139640266059SGregory Neil Shapiro SM_FILE_T *f; 1397c2aa98e2SPeter Wemm { 1398c2aa98e2SPeter Wemm register char *p = buf; 1399c2aa98e2SPeter Wemm char *bp = buf; 1400c2aa98e2SPeter Wemm register int i; 1401d0cef73dSGregory Neil Shapiro int n; 1402c2aa98e2SPeter Wemm 1403d0cef73dSGregory Neil Shapiro SM_REQUIRE(np != NULL); 1404d0cef73dSGregory Neil Shapiro n = *np; 140540266059SGregory Neil Shapiro SM_REQUIRE(n > 0); 140640266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL); 140740266059SGregory Neil Shapiro if (f == NULL) 140840266059SGregory Neil Shapiro { 140940266059SGregory Neil Shapiro buf[0] = '\0'; 141040266059SGregory Neil Shapiro errno = EBADF; 141140266059SGregory Neil Shapiro return NULL; 141240266059SGregory Neil Shapiro } 141340266059SGregory Neil Shapiro 1414c2aa98e2SPeter Wemm n--; 141540266059SGregory Neil Shapiro while ((i = sm_io_getc(f, SM_TIME_DEFAULT)) != SM_IO_EOF) 1416c2aa98e2SPeter Wemm { 1417c2aa98e2SPeter Wemm if (i == '\r') 1418c2aa98e2SPeter Wemm { 141940266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT); 1420c2aa98e2SPeter Wemm if (i != '\n') 1421c2aa98e2SPeter Wemm { 142240266059SGregory Neil Shapiro if (i != SM_IO_EOF) 142340266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT, 142440266059SGregory Neil Shapiro i); 1425c2aa98e2SPeter Wemm i = '\r'; 1426c2aa98e2SPeter Wemm } 1427c2aa98e2SPeter Wemm } 1428c2aa98e2SPeter Wemm if (--n <= 0) 1429c2aa98e2SPeter Wemm { 1430c2aa98e2SPeter Wemm /* allocate new space */ 1431c2aa98e2SPeter Wemm char *nbp; 1432c2aa98e2SPeter Wemm int nn; 1433c2aa98e2SPeter Wemm 1434c2aa98e2SPeter Wemm nn = (p - bp); 1435c2aa98e2SPeter Wemm if (nn < MEMCHUNKSIZE) 1436c2aa98e2SPeter Wemm nn *= 2; 1437c2aa98e2SPeter Wemm else 1438c2aa98e2SPeter Wemm nn += MEMCHUNKSIZE; 143940266059SGregory Neil Shapiro nbp = sm_malloc_x(nn); 144006f25ae9SGregory Neil Shapiro memmove(nbp, bp, p - bp); 1441c2aa98e2SPeter Wemm p = &nbp[p - bp]; 1442c2aa98e2SPeter Wemm if (bp != buf) 14438774250cSGregory Neil Shapiro sm_free(bp); 1444c2aa98e2SPeter Wemm bp = nbp; 1445c2aa98e2SPeter Wemm n = nn - (p - bp); 1446d0cef73dSGregory Neil Shapiro *np = nn; 1447c2aa98e2SPeter Wemm } 1448c2aa98e2SPeter Wemm *p++ = i; 1449c2aa98e2SPeter Wemm if (i == '\n') 1450c2aa98e2SPeter Wemm { 1451c2aa98e2SPeter Wemm LineNumber++; 145240266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT); 145340266059SGregory Neil Shapiro if (i != SM_IO_EOF) 145440266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT, i); 1455c2aa98e2SPeter Wemm if (i != ' ' && i != '\t') 1456c2aa98e2SPeter Wemm break; 1457c2aa98e2SPeter Wemm } 1458c2aa98e2SPeter Wemm } 1459c2aa98e2SPeter Wemm if (p == bp) 146006f25ae9SGregory Neil Shapiro return NULL; 1461c2aa98e2SPeter Wemm if (p[-1] == '\n') 1462c2aa98e2SPeter Wemm p--; 1463c2aa98e2SPeter Wemm *p = '\0'; 146406f25ae9SGregory Neil Shapiro return bp; 1465c2aa98e2SPeter Wemm } 1466d0cef73dSGregory Neil Shapiro 146740266059SGregory Neil Shapiro /* 1468c2aa98e2SPeter Wemm ** CURTIME -- return current time. 1469c2aa98e2SPeter Wemm ** 1470c2aa98e2SPeter Wemm ** Parameters: 1471c2aa98e2SPeter Wemm ** none. 1472c2aa98e2SPeter Wemm ** 1473c2aa98e2SPeter Wemm ** Returns: 1474c2aa98e2SPeter Wemm ** the current time. 1475c2aa98e2SPeter Wemm */ 1476c2aa98e2SPeter Wemm 1477c2aa98e2SPeter Wemm time_t 1478c2aa98e2SPeter Wemm curtime() 1479c2aa98e2SPeter Wemm { 1480c2aa98e2SPeter Wemm auto time_t t; 1481c2aa98e2SPeter Wemm 1482c2aa98e2SPeter Wemm (void) time(&t); 148306f25ae9SGregory Neil Shapiro return t; 1484c2aa98e2SPeter Wemm } 1485d0cef73dSGregory Neil Shapiro 148640266059SGregory Neil Shapiro /* 1487c2aa98e2SPeter Wemm ** ATOBOOL -- convert a string representation to boolean. 1488c2aa98e2SPeter Wemm ** 148940266059SGregory Neil Shapiro ** Defaults to false 1490c2aa98e2SPeter Wemm ** 1491c2aa98e2SPeter Wemm ** Parameters: 149240266059SGregory Neil Shapiro ** s -- string to convert. Takes "tTyY", empty, and NULL as true, 1493c2aa98e2SPeter Wemm ** others as false. 1494c2aa98e2SPeter Wemm ** 1495c2aa98e2SPeter Wemm ** Returns: 1496c2aa98e2SPeter Wemm ** A boolean representation of the string. 1497c2aa98e2SPeter Wemm */ 1498c2aa98e2SPeter Wemm 1499c2aa98e2SPeter Wemm bool 1500c2aa98e2SPeter Wemm atobool(s) 1501c2aa98e2SPeter Wemm register char *s; 1502c2aa98e2SPeter Wemm { 1503c2aa98e2SPeter Wemm if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 150440266059SGregory Neil Shapiro return true; 150540266059SGregory Neil Shapiro return false; 1506c2aa98e2SPeter Wemm } 1507d0cef73dSGregory Neil Shapiro 150840266059SGregory Neil Shapiro /* 1509c2aa98e2SPeter Wemm ** ATOOCT -- convert a string representation to octal. 1510c2aa98e2SPeter Wemm ** 1511c2aa98e2SPeter Wemm ** Parameters: 1512c2aa98e2SPeter Wemm ** s -- string to convert. 1513c2aa98e2SPeter Wemm ** 1514c2aa98e2SPeter Wemm ** Returns: 1515c2aa98e2SPeter Wemm ** An integer representing the string interpreted as an 1516c2aa98e2SPeter Wemm ** octal number. 1517c2aa98e2SPeter Wemm */ 1518c2aa98e2SPeter Wemm 1519c2aa98e2SPeter Wemm int 1520c2aa98e2SPeter Wemm atooct(s) 1521c2aa98e2SPeter Wemm register char *s; 1522c2aa98e2SPeter Wemm { 1523c2aa98e2SPeter Wemm register int i = 0; 1524c2aa98e2SPeter Wemm 1525c2aa98e2SPeter Wemm while (*s >= '0' && *s <= '7') 1526c2aa98e2SPeter Wemm i = (i << 3) | (*s++ - '0'); 152706f25ae9SGregory Neil Shapiro return i; 1528c2aa98e2SPeter Wemm } 1529d0cef73dSGregory Neil Shapiro 153040266059SGregory Neil Shapiro /* 1531c2aa98e2SPeter Wemm ** BITINTERSECT -- tell if two bitmaps intersect 1532c2aa98e2SPeter Wemm ** 1533c2aa98e2SPeter Wemm ** Parameters: 1534c2aa98e2SPeter Wemm ** a, b -- the bitmaps in question 1535c2aa98e2SPeter Wemm ** 1536c2aa98e2SPeter Wemm ** Returns: 153740266059SGregory Neil Shapiro ** true if they have a non-null intersection 153840266059SGregory Neil Shapiro ** false otherwise 1539c2aa98e2SPeter Wemm */ 1540c2aa98e2SPeter Wemm 1541c2aa98e2SPeter Wemm bool 1542c2aa98e2SPeter Wemm bitintersect(a, b) 154306f25ae9SGregory Neil Shapiro BITMAP256 a; 154406f25ae9SGregory Neil Shapiro BITMAP256 b; 1545c2aa98e2SPeter Wemm { 1546c2aa98e2SPeter Wemm int i; 1547c2aa98e2SPeter Wemm 1548c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof(int); --i >= 0; ) 1549193538b7SGregory Neil Shapiro { 1550c2aa98e2SPeter Wemm if ((a[i] & b[i]) != 0) 155140266059SGregory Neil Shapiro return true; 1552193538b7SGregory Neil Shapiro } 155340266059SGregory Neil Shapiro return false; 1554c2aa98e2SPeter Wemm } 1555d0cef73dSGregory Neil Shapiro 155640266059SGregory Neil Shapiro /* 1557c2aa98e2SPeter Wemm ** BITZEROP -- tell if a bitmap is all zero 1558c2aa98e2SPeter Wemm ** 1559c2aa98e2SPeter Wemm ** Parameters: 1560c2aa98e2SPeter Wemm ** map -- the bit map to check 1561c2aa98e2SPeter Wemm ** 1562c2aa98e2SPeter Wemm ** Returns: 156340266059SGregory Neil Shapiro ** true if map is all zero. 156440266059SGregory Neil Shapiro ** false if there are any bits set in map. 1565c2aa98e2SPeter Wemm */ 1566c2aa98e2SPeter Wemm 1567c2aa98e2SPeter Wemm bool 1568c2aa98e2SPeter Wemm bitzerop(map) 156906f25ae9SGregory Neil Shapiro BITMAP256 map; 1570c2aa98e2SPeter Wemm { 1571c2aa98e2SPeter Wemm int i; 1572c2aa98e2SPeter Wemm 1573c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof(int); --i >= 0; ) 1574193538b7SGregory Neil Shapiro { 1575c2aa98e2SPeter Wemm if (map[i] != 0) 157640266059SGregory Neil Shapiro return false; 1577193538b7SGregory Neil Shapiro } 157840266059SGregory Neil Shapiro return true; 1579c2aa98e2SPeter Wemm } 1580d0cef73dSGregory Neil Shapiro 158140266059SGregory Neil Shapiro /* 1582c2aa98e2SPeter Wemm ** STRCONTAINEDIN -- tell if one string is contained in another 1583c2aa98e2SPeter Wemm ** 1584c2aa98e2SPeter Wemm ** Parameters: 158540266059SGregory Neil Shapiro ** icase -- ignore case? 1586c2aa98e2SPeter Wemm ** a -- possible substring. 1587c2aa98e2SPeter Wemm ** b -- possible superstring. 1588c2aa98e2SPeter Wemm ** 1589c2aa98e2SPeter Wemm ** Returns: 159040266059SGregory Neil Shapiro ** true if a is contained in b (case insensitive). 159140266059SGregory Neil Shapiro ** false otherwise. 1592c2aa98e2SPeter Wemm */ 1593c2aa98e2SPeter Wemm 1594c2aa98e2SPeter Wemm bool 159540266059SGregory Neil Shapiro strcontainedin(icase, a, b) 159640266059SGregory Neil Shapiro bool icase; 1597c2aa98e2SPeter Wemm register char *a; 1598c2aa98e2SPeter Wemm register char *b; 1599c2aa98e2SPeter Wemm { 1600c2aa98e2SPeter Wemm int la; 1601c2aa98e2SPeter Wemm int lb; 1602c2aa98e2SPeter Wemm int c; 1603c2aa98e2SPeter Wemm 1604c2aa98e2SPeter Wemm la = strlen(a); 1605c2aa98e2SPeter Wemm lb = strlen(b); 1606c2aa98e2SPeter Wemm c = *a; 160740266059SGregory Neil Shapiro if (icase && isascii(c) && isupper(c)) 1608c2aa98e2SPeter Wemm c = tolower(c); 1609c2aa98e2SPeter Wemm for (; lb-- >= la; b++) 1610c2aa98e2SPeter Wemm { 161140266059SGregory Neil Shapiro if (icase) 161240266059SGregory Neil Shapiro { 161340266059SGregory Neil Shapiro if (*b != c && 161440266059SGregory Neil Shapiro isascii(*b) && isupper(*b) && tolower(*b) != c) 1615c2aa98e2SPeter Wemm continue; 161640266059SGregory Neil Shapiro if (sm_strncasecmp(a, b, la) == 0) 161740266059SGregory Neil Shapiro return true; 1618c2aa98e2SPeter Wemm } 161940266059SGregory Neil Shapiro else 162040266059SGregory Neil Shapiro { 162140266059SGregory Neil Shapiro if (*b != c) 162240266059SGregory Neil Shapiro continue; 162340266059SGregory Neil Shapiro if (strncmp(a, b, la) == 0) 162440266059SGregory Neil Shapiro return true; 1625c2aa98e2SPeter Wemm } 162640266059SGregory Neil Shapiro } 162740266059SGregory Neil Shapiro return false; 162840266059SGregory Neil Shapiro } 1629d0cef73dSGregory Neil Shapiro 163040266059SGregory Neil Shapiro /* 1631c2aa98e2SPeter Wemm ** CHECKFD012 -- check low numbered file descriptors 1632c2aa98e2SPeter Wemm ** 1633c2aa98e2SPeter Wemm ** File descriptors 0, 1, and 2 should be open at all times. 1634c2aa98e2SPeter Wemm ** This routine verifies that, and fixes it if not true. 1635c2aa98e2SPeter Wemm ** 1636c2aa98e2SPeter Wemm ** Parameters: 1637c2aa98e2SPeter Wemm ** where -- a tag printed if the assertion failed 1638c2aa98e2SPeter Wemm ** 1639c2aa98e2SPeter Wemm ** Returns: 1640c2aa98e2SPeter Wemm ** none 1641c2aa98e2SPeter Wemm */ 1642c2aa98e2SPeter Wemm 1643c2aa98e2SPeter Wemm void 1644c2aa98e2SPeter Wemm checkfd012(where) 1645c2aa98e2SPeter Wemm char *where; 1646c2aa98e2SPeter Wemm { 1647c2aa98e2SPeter Wemm #if XDEBUG 1648c2aa98e2SPeter Wemm register int i; 1649c2aa98e2SPeter Wemm 1650c2aa98e2SPeter Wemm for (i = 0; i < 3; i++) 1651c2aa98e2SPeter Wemm fill_fd(i, where); 1652c2aa98e2SPeter Wemm #endif /* XDEBUG */ 1653c2aa98e2SPeter Wemm } 1654d0cef73dSGregory Neil Shapiro 165540266059SGregory Neil Shapiro /* 1656c2aa98e2SPeter Wemm ** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging 1657c2aa98e2SPeter Wemm ** 1658c2aa98e2SPeter Wemm ** Parameters: 1659c2aa98e2SPeter Wemm ** fd -- file descriptor to check. 1660c2aa98e2SPeter Wemm ** where -- tag to print on failure. 1661c2aa98e2SPeter Wemm ** 1662c2aa98e2SPeter Wemm ** Returns: 1663c2aa98e2SPeter Wemm ** none. 1664c2aa98e2SPeter Wemm */ 1665c2aa98e2SPeter Wemm 1666c2aa98e2SPeter Wemm void 1667c2aa98e2SPeter Wemm checkfdopen(fd, where) 1668c2aa98e2SPeter Wemm int fd; 1669c2aa98e2SPeter Wemm char *where; 1670c2aa98e2SPeter Wemm { 1671c2aa98e2SPeter Wemm #if XDEBUG 1672c2aa98e2SPeter Wemm struct stat st; 1673c2aa98e2SPeter Wemm 1674c2aa98e2SPeter Wemm if (fstat(fd, &st) < 0 && errno == EBADF) 1675c2aa98e2SPeter Wemm { 1676c2aa98e2SPeter Wemm syserr("checkfdopen(%d): %s not open as expected!", fd, where); 167740266059SGregory Neil Shapiro printopenfds(true); 1678c2aa98e2SPeter Wemm } 167906f25ae9SGregory Neil Shapiro #endif /* XDEBUG */ 1680c2aa98e2SPeter Wemm } 1681d0cef73dSGregory Neil Shapiro 168240266059SGregory Neil Shapiro /* 1683c2aa98e2SPeter Wemm ** CHECKFDS -- check for new or missing file descriptors 1684c2aa98e2SPeter Wemm ** 1685c2aa98e2SPeter Wemm ** Parameters: 1686c2aa98e2SPeter Wemm ** where -- tag for printing. If null, take a base line. 1687c2aa98e2SPeter Wemm ** 1688c2aa98e2SPeter Wemm ** Returns: 1689c2aa98e2SPeter Wemm ** none 1690c2aa98e2SPeter Wemm ** 1691c2aa98e2SPeter Wemm ** Side Effects: 1692c2aa98e2SPeter Wemm ** If where is set, shows changes since the last call. 1693c2aa98e2SPeter Wemm */ 1694c2aa98e2SPeter Wemm 1695c2aa98e2SPeter Wemm void 1696c2aa98e2SPeter Wemm checkfds(where) 1697c2aa98e2SPeter Wemm char *where; 1698c2aa98e2SPeter Wemm { 1699c2aa98e2SPeter Wemm int maxfd; 1700c2aa98e2SPeter Wemm register int fd; 170140266059SGregory Neil Shapiro bool printhdr = true; 1702c2aa98e2SPeter Wemm int save_errno = errno; 170306f25ae9SGregory Neil Shapiro static BITMAP256 baseline; 1704c2aa98e2SPeter Wemm extern int DtableSize; 1705c2aa98e2SPeter Wemm 1706193538b7SGregory Neil Shapiro if (DtableSize > BITMAPBITS) 1707193538b7SGregory Neil Shapiro maxfd = BITMAPBITS; 1708c2aa98e2SPeter Wemm else 1709c2aa98e2SPeter Wemm maxfd = DtableSize; 1710c2aa98e2SPeter Wemm if (where == NULL) 1711c2aa98e2SPeter Wemm clrbitmap(baseline); 1712c2aa98e2SPeter Wemm 1713c2aa98e2SPeter Wemm for (fd = 0; fd < maxfd; fd++) 1714c2aa98e2SPeter Wemm { 1715c2aa98e2SPeter Wemm struct stat stbuf; 1716c2aa98e2SPeter Wemm 1717c2aa98e2SPeter Wemm if (fstat(fd, &stbuf) < 0 && errno != EOPNOTSUPP) 1718c2aa98e2SPeter Wemm { 1719c2aa98e2SPeter Wemm if (!bitnset(fd, baseline)) 1720c2aa98e2SPeter Wemm continue; 1721c2aa98e2SPeter Wemm clrbitn(fd, baseline); 1722c2aa98e2SPeter Wemm } 1723c2aa98e2SPeter Wemm else if (!bitnset(fd, baseline)) 1724c2aa98e2SPeter Wemm setbitn(fd, baseline); 1725c2aa98e2SPeter Wemm else 1726c2aa98e2SPeter Wemm continue; 1727c2aa98e2SPeter Wemm 1728c2aa98e2SPeter Wemm /* file state has changed */ 1729c2aa98e2SPeter Wemm if (where == NULL) 1730c2aa98e2SPeter Wemm continue; 1731c2aa98e2SPeter Wemm if (printhdr) 1732c2aa98e2SPeter Wemm { 1733c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id, 1734c2aa98e2SPeter Wemm "%s: changed fds:", 1735c2aa98e2SPeter Wemm where); 173640266059SGregory Neil Shapiro printhdr = false; 1737c2aa98e2SPeter Wemm } 173840266059SGregory Neil Shapiro dumpfd(fd, true, true); 1739c2aa98e2SPeter Wemm } 1740c2aa98e2SPeter Wemm errno = save_errno; 1741c2aa98e2SPeter Wemm } 1742d0cef73dSGregory Neil Shapiro 174340266059SGregory Neil Shapiro /* 1744c2aa98e2SPeter Wemm ** PRINTOPENFDS -- print the open file descriptors (for debugging) 1745c2aa98e2SPeter Wemm ** 1746c2aa98e2SPeter Wemm ** Parameters: 1747c2aa98e2SPeter Wemm ** logit -- if set, send output to syslog; otherwise 1748c2aa98e2SPeter Wemm ** print for debugging. 1749c2aa98e2SPeter Wemm ** 1750c2aa98e2SPeter Wemm ** Returns: 1751c2aa98e2SPeter Wemm ** none. 1752c2aa98e2SPeter Wemm */ 1753c2aa98e2SPeter Wemm 175406f25ae9SGregory Neil Shapiro #if NETINET || NETINET6 1755c2aa98e2SPeter Wemm # include <arpa/inet.h> 175606f25ae9SGregory Neil Shapiro #endif /* NETINET || NETINET6 */ 1757c2aa98e2SPeter Wemm 1758c2aa98e2SPeter Wemm void 1759c2aa98e2SPeter Wemm printopenfds(logit) 1760c2aa98e2SPeter Wemm bool logit; 1761c2aa98e2SPeter Wemm { 1762c2aa98e2SPeter Wemm register int fd; 1763c2aa98e2SPeter Wemm extern int DtableSize; 1764c2aa98e2SPeter Wemm 1765c2aa98e2SPeter Wemm for (fd = 0; fd < DtableSize; fd++) 176640266059SGregory Neil Shapiro dumpfd(fd, false, logit); 1767c2aa98e2SPeter Wemm } 1768d0cef73dSGregory Neil Shapiro 176940266059SGregory Neil Shapiro /* 1770c2aa98e2SPeter Wemm ** DUMPFD -- dump a file descriptor 1771c2aa98e2SPeter Wemm ** 1772c2aa98e2SPeter Wemm ** Parameters: 1773c2aa98e2SPeter Wemm ** fd -- the file descriptor to dump. 1774c2aa98e2SPeter Wemm ** printclosed -- if set, print a notification even if 1775c2aa98e2SPeter Wemm ** it is closed; otherwise print nothing. 1776e92d3f3fSGregory Neil Shapiro ** logit -- if set, use sm_syslog instead of sm_dprintf() 177740266059SGregory Neil Shapiro ** 177840266059SGregory Neil Shapiro ** Returns: 177940266059SGregory Neil Shapiro ** none. 1780c2aa98e2SPeter Wemm */ 1781c2aa98e2SPeter Wemm 1782c2aa98e2SPeter Wemm void 1783c2aa98e2SPeter Wemm dumpfd(fd, printclosed, logit) 1784c2aa98e2SPeter Wemm int fd; 1785c2aa98e2SPeter Wemm bool printclosed; 1786c2aa98e2SPeter Wemm bool logit; 1787c2aa98e2SPeter Wemm { 1788c2aa98e2SPeter Wemm register char *p; 1789c2aa98e2SPeter Wemm char *hp; 1790c2aa98e2SPeter Wemm #ifdef S_IFSOCK 1791c2aa98e2SPeter Wemm SOCKADDR sa; 179206f25ae9SGregory Neil Shapiro #endif /* S_IFSOCK */ 1793c2aa98e2SPeter Wemm auto SOCKADDR_LEN_T slen; 1794c2aa98e2SPeter Wemm int i; 1795c2aa98e2SPeter Wemm #if STAT64 > 0 1796c2aa98e2SPeter Wemm struct stat64 st; 179706f25ae9SGregory Neil Shapiro #else /* STAT64 > 0 */ 1798c2aa98e2SPeter Wemm struct stat st; 179906f25ae9SGregory Neil Shapiro #endif /* STAT64 > 0 */ 1800c2aa98e2SPeter Wemm char buf[200]; 1801c2aa98e2SPeter Wemm 1802c2aa98e2SPeter Wemm p = buf; 180340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "%3d: ", fd); 1804c2aa98e2SPeter Wemm p += strlen(p); 1805c2aa98e2SPeter Wemm 1806c2aa98e2SPeter Wemm if ( 1807c2aa98e2SPeter Wemm #if STAT64 > 0 1808c2aa98e2SPeter Wemm fstat64(fd, &st) 180906f25ae9SGregory Neil Shapiro #else /* STAT64 > 0 */ 1810c2aa98e2SPeter Wemm fstat(fd, &st) 181106f25ae9SGregory Neil Shapiro #endif /* STAT64 > 0 */ 1812c2aa98e2SPeter Wemm < 0) 1813c2aa98e2SPeter Wemm { 1814c2aa98e2SPeter Wemm if (errno != EBADF) 1815c2aa98e2SPeter Wemm { 181640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 181740266059SGregory Neil Shapiro "CANNOT STAT (%s)", 181840266059SGregory Neil Shapiro sm_errstring(errno)); 1819c2aa98e2SPeter Wemm goto printit; 1820c2aa98e2SPeter Wemm } 1821c2aa98e2SPeter Wemm else if (printclosed) 1822c2aa98e2SPeter Wemm { 182340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CLOSED"); 1824c2aa98e2SPeter Wemm goto printit; 1825c2aa98e2SPeter Wemm } 1826c2aa98e2SPeter Wemm return; 1827c2aa98e2SPeter Wemm } 1828c2aa98e2SPeter Wemm 1829605302a5SGregory Neil Shapiro i = fcntl(fd, F_GETFL, 0); 1830c2aa98e2SPeter Wemm if (i != -1) 1831c2aa98e2SPeter Wemm { 183240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "fl=0x%x, ", i); 1833c2aa98e2SPeter Wemm p += strlen(p); 1834c2aa98e2SPeter Wemm } 1835c2aa98e2SPeter Wemm 183640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "mode=%o: ", 183740266059SGregory Neil Shapiro (int) st.st_mode); 1838c2aa98e2SPeter Wemm p += strlen(p); 1839c2aa98e2SPeter Wemm switch (st.st_mode & S_IFMT) 1840c2aa98e2SPeter Wemm { 1841c2aa98e2SPeter Wemm #ifdef S_IFSOCK 1842c2aa98e2SPeter Wemm case S_IFSOCK: 184340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "SOCK "); 1844c2aa98e2SPeter Wemm p += strlen(p); 1845d0cef73dSGregory Neil Shapiro memset(&sa, '\0', sizeof(sa)); 1846d0cef73dSGregory Neil Shapiro slen = sizeof(sa); 1847c2aa98e2SPeter Wemm if (getsockname(fd, &sa.sa, &slen) < 0) 184840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)", 184940266059SGregory Neil Shapiro sm_errstring(errno)); 1850c2aa98e2SPeter Wemm else 1851c2aa98e2SPeter Wemm { 1852c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa); 185306f25ae9SGregory Neil Shapiro if (hp == NULL) 185406f25ae9SGregory Neil Shapiro { 185506f25ae9SGregory Neil Shapiro /* EMPTY */ 185606f25ae9SGregory Neil Shapiro /* do nothing */ 185706f25ae9SGregory Neil Shapiro } 185806f25ae9SGregory Neil Shapiro # if NETINET 185906f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET) 186040266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 186140266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port)); 186206f25ae9SGregory Neil Shapiro # endif /* NETINET */ 186306f25ae9SGregory Neil Shapiro # if NETINET6 186406f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6) 186540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 186640266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port)); 186706f25ae9SGregory Neil Shapiro # endif /* NETINET6 */ 1868c2aa98e2SPeter Wemm else 186940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 187040266059SGregory Neil Shapiro "%s", hp); 1871c2aa98e2SPeter Wemm } 1872c2aa98e2SPeter Wemm p += strlen(p); 187340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "->"); 1874c2aa98e2SPeter Wemm p += strlen(p); 1875d0cef73dSGregory Neil Shapiro slen = sizeof(sa); 1876c2aa98e2SPeter Wemm if (getpeername(fd, &sa.sa, &slen) < 0) 187740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)", 187840266059SGregory Neil Shapiro sm_errstring(errno)); 1879c2aa98e2SPeter Wemm else 1880c2aa98e2SPeter Wemm { 1881c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa); 188206f25ae9SGregory Neil Shapiro if (hp == NULL) 188306f25ae9SGregory Neil Shapiro { 188406f25ae9SGregory Neil Shapiro /* EMPTY */ 188506f25ae9SGregory Neil Shapiro /* do nothing */ 188606f25ae9SGregory Neil Shapiro } 188706f25ae9SGregory Neil Shapiro # if NETINET 188806f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET) 188940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 189040266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port)); 189106f25ae9SGregory Neil Shapiro # endif /* NETINET */ 189206f25ae9SGregory Neil Shapiro # if NETINET6 189306f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6) 189440266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 189540266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port)); 189606f25ae9SGregory Neil Shapiro # endif /* NETINET6 */ 1897c2aa98e2SPeter Wemm else 189840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 189940266059SGregory Neil Shapiro "%s", hp); 1900c2aa98e2SPeter Wemm } 1901c2aa98e2SPeter Wemm break; 190206f25ae9SGregory Neil Shapiro #endif /* S_IFSOCK */ 1903c2aa98e2SPeter Wemm 1904c2aa98e2SPeter Wemm case S_IFCHR: 190540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CHR: "); 1906c2aa98e2SPeter Wemm p += strlen(p); 1907c2aa98e2SPeter Wemm goto defprint; 1908c2aa98e2SPeter Wemm 190940266059SGregory Neil Shapiro #ifdef S_IFBLK 1910c2aa98e2SPeter Wemm case S_IFBLK: 191140266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "BLK: "); 1912c2aa98e2SPeter Wemm p += strlen(p); 1913c2aa98e2SPeter Wemm goto defprint; 191440266059SGregory Neil Shapiro #endif /* S_IFBLK */ 1915c2aa98e2SPeter Wemm 1916c2aa98e2SPeter Wemm #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) 1917c2aa98e2SPeter Wemm case S_IFIFO: 191840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "FIFO: "); 1919c2aa98e2SPeter Wemm p += strlen(p); 1920c2aa98e2SPeter Wemm goto defprint; 192106f25ae9SGregory Neil Shapiro #endif /* defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) */ 1922c2aa98e2SPeter Wemm 1923c2aa98e2SPeter Wemm #ifdef S_IFDIR 1924c2aa98e2SPeter Wemm case S_IFDIR: 192540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "DIR: "); 1926c2aa98e2SPeter Wemm p += strlen(p); 1927c2aa98e2SPeter Wemm goto defprint; 192806f25ae9SGregory Neil Shapiro #endif /* S_IFDIR */ 1929c2aa98e2SPeter Wemm 1930c2aa98e2SPeter Wemm #ifdef S_IFLNK 1931c2aa98e2SPeter Wemm case S_IFLNK: 193240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "LNK: "); 1933c2aa98e2SPeter Wemm p += strlen(p); 1934c2aa98e2SPeter Wemm goto defprint; 193506f25ae9SGregory Neil Shapiro #endif /* S_IFLNK */ 1936c2aa98e2SPeter Wemm 1937c2aa98e2SPeter Wemm default: 1938c2aa98e2SPeter Wemm defprint: 193940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 194040266059SGregory Neil Shapiro "dev=%d/%d, ino=%llu, nlink=%d, u/gid=%d/%d, ", 1941c2aa98e2SPeter Wemm major(st.st_dev), minor(st.st_dev), 194240266059SGregory Neil Shapiro (ULONGLONG_T) st.st_ino, 194306f25ae9SGregory Neil Shapiro (int) st.st_nlink, (int) st.st_uid, 194406f25ae9SGregory Neil Shapiro (int) st.st_gid); 194540266059SGregory Neil Shapiro p += strlen(p); 194640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "size=%llu", 194740266059SGregory Neil Shapiro (ULONGLONG_T) st.st_size); 1948c2aa98e2SPeter Wemm break; 1949c2aa98e2SPeter Wemm } 1950c2aa98e2SPeter Wemm 1951c2aa98e2SPeter Wemm printit: 1952c2aa98e2SPeter Wemm if (logit) 1953c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv ? CurEnv->e_id : NULL, 1954c2aa98e2SPeter Wemm "%.800s", buf); 1955c2aa98e2SPeter Wemm else 1956e92d3f3fSGregory Neil Shapiro sm_dprintf("%s\n", buf); 1957c2aa98e2SPeter Wemm } 1958d0cef73dSGregory Neil Shapiro 195940266059SGregory Neil Shapiro /* 1960c2aa98e2SPeter Wemm ** SHORTEN_HOSTNAME -- strip local domain information off of hostname. 1961c2aa98e2SPeter Wemm ** 1962c2aa98e2SPeter Wemm ** Parameters: 1963c2aa98e2SPeter Wemm ** host -- the host to shorten (stripped in place). 1964c2aa98e2SPeter Wemm ** 1965c2aa98e2SPeter Wemm ** Returns: 196640266059SGregory Neil Shapiro ** place where string was truncated, NULL if not truncated. 1967c2aa98e2SPeter Wemm */ 1968c2aa98e2SPeter Wemm 1969602a2b1bSGregory Neil Shapiro char * 1970c2aa98e2SPeter Wemm shorten_hostname(host) 1971c2aa98e2SPeter Wemm char host[]; 1972c2aa98e2SPeter Wemm { 1973c2aa98e2SPeter Wemm register char *p; 1974c2aa98e2SPeter Wemm char *mydom; 1975c2aa98e2SPeter Wemm int i; 197640266059SGregory Neil Shapiro bool canon = false; 1977c2aa98e2SPeter Wemm 1978c2aa98e2SPeter Wemm /* strip off final dot */ 197940266059SGregory Neil Shapiro i = strlen(host); 198040266059SGregory Neil Shapiro p = &host[(i == 0) ? 0 : i - 1]; 1981c2aa98e2SPeter Wemm if (*p == '.') 1982c2aa98e2SPeter Wemm { 1983c2aa98e2SPeter Wemm *p = '\0'; 198440266059SGregory Neil Shapiro canon = true; 1985c2aa98e2SPeter Wemm } 1986c2aa98e2SPeter Wemm 1987c2aa98e2SPeter Wemm /* see if there is any domain at all -- if not, we are done */ 1988c2aa98e2SPeter Wemm p = strchr(host, '.'); 1989c2aa98e2SPeter Wemm if (p == NULL) 1990602a2b1bSGregory Neil Shapiro return NULL; 1991c2aa98e2SPeter Wemm 1992c2aa98e2SPeter Wemm /* yes, we have a domain -- see if it looks like us */ 1993c2aa98e2SPeter Wemm mydom = macvalue('m', CurEnv); 1994c2aa98e2SPeter Wemm if (mydom == NULL) 1995c2aa98e2SPeter Wemm mydom = ""; 1996c2aa98e2SPeter Wemm i = strlen(++p); 199740266059SGregory Neil Shapiro if ((canon ? sm_strcasecmp(p, mydom) 199840266059SGregory Neil Shapiro : sm_strncasecmp(p, mydom, i)) == 0 && 1999c2aa98e2SPeter Wemm (mydom[i] == '.' || mydom[i] == '\0')) 2000602a2b1bSGregory Neil Shapiro { 2001c2aa98e2SPeter Wemm *--p = '\0'; 2002602a2b1bSGregory Neil Shapiro return p; 2003602a2b1bSGregory Neil Shapiro } 2004602a2b1bSGregory Neil Shapiro return NULL; 2005c2aa98e2SPeter Wemm } 2006d0cef73dSGregory Neil Shapiro 200740266059SGregory Neil Shapiro /* 2008c2aa98e2SPeter Wemm ** PROG_OPEN -- open a program for reading 2009c2aa98e2SPeter Wemm ** 2010c2aa98e2SPeter Wemm ** Parameters: 2011c2aa98e2SPeter Wemm ** argv -- the argument list. 2012c2aa98e2SPeter Wemm ** pfd -- pointer to a place to store the file descriptor. 2013c2aa98e2SPeter Wemm ** e -- the current envelope. 2014c2aa98e2SPeter Wemm ** 2015c2aa98e2SPeter Wemm ** Returns: 2016c2aa98e2SPeter Wemm ** pid of the process -- -1 if it failed. 2017c2aa98e2SPeter Wemm */ 2018c2aa98e2SPeter Wemm 20198774250cSGregory Neil Shapiro pid_t 2020c2aa98e2SPeter Wemm prog_open(argv, pfd, e) 2021c2aa98e2SPeter Wemm char **argv; 2022c2aa98e2SPeter Wemm int *pfd; 2023c2aa98e2SPeter Wemm ENVELOPE *e; 2024c2aa98e2SPeter Wemm { 20258774250cSGregory Neil Shapiro pid_t pid; 202606f25ae9SGregory Neil Shapiro int save_errno; 202740266059SGregory Neil Shapiro int sff; 202840266059SGregory Neil Shapiro int ret; 2029c2aa98e2SPeter Wemm int fdv[2]; 2030c2aa98e2SPeter Wemm char *p, *q; 203194c01205SGregory Neil Shapiro char buf[MAXPATHLEN]; 2032c2aa98e2SPeter Wemm extern int DtableSize; 2033c2aa98e2SPeter Wemm 2034c2aa98e2SPeter Wemm if (pipe(fdv) < 0) 2035c2aa98e2SPeter Wemm { 2036c2aa98e2SPeter Wemm syserr("%s: cannot create pipe for stdout", argv[0]); 2037c2aa98e2SPeter Wemm return -1; 2038c2aa98e2SPeter Wemm } 2039c2aa98e2SPeter Wemm pid = fork(); 2040c2aa98e2SPeter Wemm if (pid < 0) 2041c2aa98e2SPeter Wemm { 2042c2aa98e2SPeter Wemm syserr("%s: cannot fork", argv[0]); 204306f25ae9SGregory Neil Shapiro (void) close(fdv[0]); 204406f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2045c2aa98e2SPeter Wemm return -1; 2046c2aa98e2SPeter Wemm } 2047c2aa98e2SPeter Wemm if (pid > 0) 2048c2aa98e2SPeter Wemm { 2049c2aa98e2SPeter Wemm /* parent */ 205006f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2051c2aa98e2SPeter Wemm *pfd = fdv[0]; 2052c2aa98e2SPeter Wemm return pid; 2053c2aa98e2SPeter Wemm } 2054c2aa98e2SPeter Wemm 20558774250cSGregory Neil Shapiro /* Reset global flags */ 20568774250cSGregory Neil Shapiro RestartRequest = NULL; 205740266059SGregory Neil Shapiro RestartWorkGroup = false; 20588774250cSGregory Neil Shapiro ShutdownRequest = NULL; 20598774250cSGregory Neil Shapiro PendingSignal = 0; 206040266059SGregory Neil Shapiro CurrentPid = getpid(); 206140266059SGregory Neil Shapiro 206240266059SGregory Neil Shapiro /* 206340266059SGregory Neil Shapiro ** Initialize exception stack and default exception 206440266059SGregory Neil Shapiro ** handler for child process. 206540266059SGregory Neil Shapiro */ 206640266059SGregory Neil Shapiro 206740266059SGregory Neil Shapiro sm_exc_newthread(fatal_error); 206840266059SGregory Neil Shapiro 206940266059SGregory Neil Shapiro /* child -- close stdin */ 207040266059SGregory Neil Shapiro (void) close(0); 20718774250cSGregory Neil Shapiro 2072c2aa98e2SPeter Wemm /* stdout goes back to parent */ 207306f25ae9SGregory Neil Shapiro (void) close(fdv[0]); 2074c2aa98e2SPeter Wemm if (dup2(fdv[1], 1) < 0) 2075c2aa98e2SPeter Wemm { 2076c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stdout", argv[0]); 2077c2aa98e2SPeter Wemm _exit(EX_OSERR); 2078c2aa98e2SPeter Wemm } 207906f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2080c2aa98e2SPeter Wemm 2081c2aa98e2SPeter Wemm /* stderr goes to transcript if available */ 2082c2aa98e2SPeter Wemm if (e->e_xfp != NULL) 2083c2aa98e2SPeter Wemm { 208406f25ae9SGregory Neil Shapiro int xfd; 208506f25ae9SGregory Neil Shapiro 208640266059SGregory Neil Shapiro xfd = sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL); 208706f25ae9SGregory Neil Shapiro if (xfd >= 0 && dup2(xfd, 2) < 0) 2088c2aa98e2SPeter Wemm { 2089c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stderr", argv[0]); 2090c2aa98e2SPeter Wemm _exit(EX_OSERR); 2091c2aa98e2SPeter Wemm } 2092c2aa98e2SPeter Wemm } 2093c2aa98e2SPeter Wemm 2094c2aa98e2SPeter Wemm /* this process has no right to the queue file */ 2095c2aa98e2SPeter Wemm if (e->e_lockfp != NULL) 2096af9557fdSGregory Neil Shapiro { 2097af9557fdSGregory Neil Shapiro int fd; 2098af9557fdSGregory Neil Shapiro 2099af9557fdSGregory Neil Shapiro fd = sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL); 2100af9557fdSGregory Neil Shapiro if (fd >= 0) 2101af9557fdSGregory Neil Shapiro (void) close(fd); 2102af9557fdSGregory Neil Shapiro else 2103af9557fdSGregory Neil Shapiro syserr("%s: lockfp does not have a fd", argv[0]); 2104af9557fdSGregory Neil Shapiro } 210506f25ae9SGregory Neil Shapiro 210606f25ae9SGregory Neil Shapiro /* chroot to the program mailer directory, if defined */ 210706f25ae9SGregory Neil Shapiro if (ProgMailer != NULL && ProgMailer->m_rootdir != NULL) 210806f25ae9SGregory Neil Shapiro { 2109d0cef73dSGregory Neil Shapiro expand(ProgMailer->m_rootdir, buf, sizeof(buf), e); 211006f25ae9SGregory Neil Shapiro if (chroot(buf) < 0) 211106f25ae9SGregory Neil Shapiro { 211206f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chroot(%s)", buf); 211306f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 211406f25ae9SGregory Neil Shapiro } 211506f25ae9SGregory Neil Shapiro if (chdir("/") < 0) 211606f25ae9SGregory Neil Shapiro { 211706f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chdir(/)"); 211806f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 211906f25ae9SGregory Neil Shapiro } 212006f25ae9SGregory Neil Shapiro } 2121c2aa98e2SPeter Wemm 2122c2aa98e2SPeter Wemm /* run as default user */ 2123c2aa98e2SPeter Wemm endpwent(); 212440266059SGregory Neil Shapiro sm_mbdb_terminate(); 21254e4196cbSGregory Neil Shapiro #if _FFR_MEMSTAT 21264e4196cbSGregory Neil Shapiro (void) sm_memstat_close(); 21274e4196cbSGregory Neil Shapiro #endif /* _FFR_MEMSTAT */ 2128c2aa98e2SPeter Wemm if (setgid(DefGid) < 0 && geteuid() == 0) 212906f25ae9SGregory Neil Shapiro { 2130c2aa98e2SPeter Wemm syserr("prog_open: setgid(%ld) failed", (long) DefGid); 213106f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 213206f25ae9SGregory Neil Shapiro } 2133c2aa98e2SPeter Wemm if (setuid(DefUid) < 0 && geteuid() == 0) 213406f25ae9SGregory Neil Shapiro { 2135c2aa98e2SPeter Wemm syserr("prog_open: setuid(%ld) failed", (long) DefUid); 213606f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 213706f25ae9SGregory Neil Shapiro } 2138c2aa98e2SPeter Wemm 2139c2aa98e2SPeter Wemm /* run in some directory */ 2140c2aa98e2SPeter Wemm if (ProgMailer != NULL) 2141c2aa98e2SPeter Wemm p = ProgMailer->m_execdir; 2142c2aa98e2SPeter Wemm else 2143c2aa98e2SPeter Wemm p = NULL; 2144c2aa98e2SPeter Wemm for (; p != NULL; p = q) 2145c2aa98e2SPeter Wemm { 2146c2aa98e2SPeter Wemm q = strchr(p, ':'); 2147c2aa98e2SPeter Wemm if (q != NULL) 2148c2aa98e2SPeter Wemm *q = '\0'; 2149d0cef73dSGregory Neil Shapiro expand(p, buf, sizeof(buf), e); 2150c2aa98e2SPeter Wemm if (q != NULL) 2151c2aa98e2SPeter Wemm *q++ = ':'; 2152c2aa98e2SPeter Wemm if (buf[0] != '\0' && chdir(buf) >= 0) 2153c2aa98e2SPeter Wemm break; 2154c2aa98e2SPeter Wemm } 2155c2aa98e2SPeter Wemm if (p == NULL) 2156c2aa98e2SPeter Wemm { 2157c2aa98e2SPeter Wemm /* backup directories */ 2158c2aa98e2SPeter Wemm if (chdir("/tmp") < 0) 2159c2aa98e2SPeter Wemm (void) chdir("/"); 2160c2aa98e2SPeter Wemm } 2161c2aa98e2SPeter Wemm 216240266059SGregory Neil Shapiro /* Check safety of program to be run */ 216340266059SGregory Neil Shapiro sff = SFF_ROOTOK|SFF_EXECOK; 216440266059SGregory Neil Shapiro if (!bitnset(DBS_RUNWRITABLEPROGRAM, DontBlameSendmail)) 216540266059SGregory Neil Shapiro sff |= SFF_NOGWFILES|SFF_NOWWFILES; 216640266059SGregory Neil Shapiro if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH, DontBlameSendmail)) 216740266059SGregory Neil Shapiro sff |= SFF_NOPATHCHECK; 216840266059SGregory Neil Shapiro else 216940266059SGregory Neil Shapiro sff |= SFF_SAFEDIRPATH; 217040266059SGregory Neil Shapiro ret = safefile(argv[0], DefUid, DefGid, DefUser, sff, 0, NULL); 217140266059SGregory Neil Shapiro if (ret != 0) 217240266059SGregory Neil Shapiro sm_syslog(LOG_INFO, e->e_id, 217340266059SGregory Neil Shapiro "Warning: prog_open: program %s unsafe: %s", 217440266059SGregory Neil Shapiro argv[0], sm_errstring(ret)); 217540266059SGregory Neil Shapiro 2176c2aa98e2SPeter Wemm /* arrange for all the files to be closed */ 2177e92d3f3fSGregory Neil Shapiro sm_close_on_exec(STDERR_FILENO + 1, DtableSize); 2178c2aa98e2SPeter Wemm 2179c2aa98e2SPeter Wemm /* now exec the process */ 218006f25ae9SGregory Neil Shapiro (void) execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron); 2181c2aa98e2SPeter Wemm 2182c2aa98e2SPeter Wemm /* woops! failed */ 218306f25ae9SGregory Neil Shapiro save_errno = errno; 2184c2aa98e2SPeter Wemm syserr("%s: cannot exec", argv[0]); 218506f25ae9SGregory Neil Shapiro if (transienterror(save_errno)) 2186c2aa98e2SPeter Wemm _exit(EX_OSERR); 2187c2aa98e2SPeter Wemm _exit(EX_CONFIG); 2188c2aa98e2SPeter Wemm return -1; /* avoid compiler warning on IRIX */ 2189c2aa98e2SPeter Wemm } 2190d0cef73dSGregory Neil Shapiro 219140266059SGregory Neil Shapiro /* 2192c2aa98e2SPeter Wemm ** GET_COLUMN -- look up a Column in a line buffer 2193c2aa98e2SPeter Wemm ** 2194c2aa98e2SPeter Wemm ** Parameters: 2195c2aa98e2SPeter Wemm ** line -- the raw text line to search. 2196c2aa98e2SPeter Wemm ** col -- the column number to fetch. 2197c2aa98e2SPeter Wemm ** delim -- the delimiter between columns. If null, 2198c2aa98e2SPeter Wemm ** use white space. 2199c2aa98e2SPeter Wemm ** buf -- the output buffer. 2200c2aa98e2SPeter Wemm ** buflen -- the length of buf. 2201c2aa98e2SPeter Wemm ** 2202c2aa98e2SPeter Wemm ** Returns: 2203c2aa98e2SPeter Wemm ** buf if successful. 2204c2aa98e2SPeter Wemm ** NULL otherwise. 2205c2aa98e2SPeter Wemm */ 2206c2aa98e2SPeter Wemm 2207c2aa98e2SPeter Wemm char * 2208c2aa98e2SPeter Wemm get_column(line, col, delim, buf, buflen) 2209c2aa98e2SPeter Wemm char line[]; 2210c2aa98e2SPeter Wemm int col; 221106f25ae9SGregory Neil Shapiro int delim; 2212c2aa98e2SPeter Wemm char buf[]; 2213c2aa98e2SPeter Wemm int buflen; 2214c2aa98e2SPeter Wemm { 2215c2aa98e2SPeter Wemm char *p; 2216c2aa98e2SPeter Wemm char *begin, *end; 2217c2aa98e2SPeter Wemm int i; 2218c2aa98e2SPeter Wemm char delimbuf[4]; 2219c2aa98e2SPeter Wemm 222006f25ae9SGregory Neil Shapiro if ((char) delim == '\0') 2221d0cef73dSGregory Neil Shapiro (void) sm_strlcpy(delimbuf, "\n\t ", sizeof(delimbuf)); 2222c2aa98e2SPeter Wemm else 2223c2aa98e2SPeter Wemm { 222406f25ae9SGregory Neil Shapiro delimbuf[0] = (char) delim; 2225c2aa98e2SPeter Wemm delimbuf[1] = '\0'; 2226c2aa98e2SPeter Wemm } 2227c2aa98e2SPeter Wemm 2228c2aa98e2SPeter Wemm p = line; 2229c2aa98e2SPeter Wemm if (*p == '\0') 2230c2aa98e2SPeter Wemm return NULL; /* line empty */ 223106f25ae9SGregory Neil Shapiro if (*p == (char) delim && col == 0) 2232c2aa98e2SPeter Wemm return NULL; /* first column empty */ 2233c2aa98e2SPeter Wemm 2234c2aa98e2SPeter Wemm begin = line; 2235c2aa98e2SPeter Wemm 223606f25ae9SGregory Neil Shapiro if (col == 0 && (char) delim == '\0') 2237c2aa98e2SPeter Wemm { 2238c2aa98e2SPeter Wemm while (*begin != '\0' && isascii(*begin) && isspace(*begin)) 2239c2aa98e2SPeter Wemm begin++; 2240c2aa98e2SPeter Wemm } 2241c2aa98e2SPeter Wemm 2242c2aa98e2SPeter Wemm for (i = 0; i < col; i++) 2243c2aa98e2SPeter Wemm { 2244c2aa98e2SPeter Wemm if ((begin = strpbrk(begin, delimbuf)) == NULL) 2245c2aa98e2SPeter Wemm return NULL; /* no such column */ 2246c2aa98e2SPeter Wemm begin++; 224706f25ae9SGregory Neil Shapiro if ((char) delim == '\0') 2248c2aa98e2SPeter Wemm { 2249c2aa98e2SPeter Wemm while (*begin != '\0' && isascii(*begin) && isspace(*begin)) 2250c2aa98e2SPeter Wemm begin++; 2251c2aa98e2SPeter Wemm } 2252c2aa98e2SPeter Wemm } 2253c2aa98e2SPeter Wemm 2254c2aa98e2SPeter Wemm end = strpbrk(begin, delimbuf); 2255c2aa98e2SPeter Wemm if (end == NULL) 2256c2aa98e2SPeter Wemm i = strlen(begin); 2257c2aa98e2SPeter Wemm else 2258c2aa98e2SPeter Wemm i = end - begin; 2259c2aa98e2SPeter Wemm if (i >= buflen) 2260c2aa98e2SPeter Wemm i = buflen - 1; 226140266059SGregory Neil Shapiro (void) sm_strlcpy(buf, begin, i + 1); 2262c2aa98e2SPeter Wemm return buf; 2263c2aa98e2SPeter Wemm } 2264d0cef73dSGregory Neil Shapiro 226540266059SGregory Neil Shapiro /* 2266c2aa98e2SPeter Wemm ** CLEANSTRCPY -- copy string keeping out bogus characters 2267c2aa98e2SPeter Wemm ** 2268c2aa98e2SPeter Wemm ** Parameters: 2269c2aa98e2SPeter Wemm ** t -- "to" string. 2270c2aa98e2SPeter Wemm ** f -- "from" string. 2271c2aa98e2SPeter Wemm ** l -- length of space available in "to" string. 2272c2aa98e2SPeter Wemm ** 2273c2aa98e2SPeter Wemm ** Returns: 2274c2aa98e2SPeter Wemm ** none. 2275c2aa98e2SPeter Wemm */ 2276c2aa98e2SPeter Wemm 2277c2aa98e2SPeter Wemm void 2278c2aa98e2SPeter Wemm cleanstrcpy(t, f, l) 2279c2aa98e2SPeter Wemm register char *t; 2280c2aa98e2SPeter Wemm register char *f; 2281c2aa98e2SPeter Wemm int l; 2282c2aa98e2SPeter Wemm { 2283c2aa98e2SPeter Wemm /* check for newlines and log if necessary */ 228440266059SGregory Neil Shapiro (void) denlstring(f, true, true); 2285c2aa98e2SPeter Wemm 228606f25ae9SGregory Neil Shapiro if (l <= 0) 228706f25ae9SGregory Neil Shapiro syserr("!cleanstrcpy: length == 0"); 228806f25ae9SGregory Neil Shapiro 2289c2aa98e2SPeter Wemm l--; 2290c2aa98e2SPeter Wemm while (l > 0 && *f != '\0') 2291c2aa98e2SPeter Wemm { 2292c2aa98e2SPeter Wemm if (isascii(*f) && 2293c2aa98e2SPeter Wemm (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL)) 2294c2aa98e2SPeter Wemm { 2295c2aa98e2SPeter Wemm l--; 2296c2aa98e2SPeter Wemm *t++ = *f; 2297c2aa98e2SPeter Wemm } 2298c2aa98e2SPeter Wemm f++; 2299c2aa98e2SPeter Wemm } 2300c2aa98e2SPeter Wemm *t = '\0'; 2301c2aa98e2SPeter Wemm } 2302d0cef73dSGregory Neil Shapiro 230340266059SGregory Neil Shapiro /* 2304c2aa98e2SPeter Wemm ** DENLSTRING -- convert newlines in a string to spaces 2305c2aa98e2SPeter Wemm ** 2306c2aa98e2SPeter Wemm ** Parameters: 2307c2aa98e2SPeter Wemm ** s -- the input string 2308c2aa98e2SPeter Wemm ** strict -- if set, don't permit continuation lines. 2309c2aa98e2SPeter Wemm ** logattacks -- if set, log attempted attacks. 2310c2aa98e2SPeter Wemm ** 2311c2aa98e2SPeter Wemm ** Returns: 2312c2aa98e2SPeter Wemm ** A pointer to a version of the string with newlines 2313c2aa98e2SPeter Wemm ** mapped to spaces. This should be copied. 2314c2aa98e2SPeter Wemm */ 2315c2aa98e2SPeter Wemm 2316c2aa98e2SPeter Wemm char * 2317c2aa98e2SPeter Wemm denlstring(s, strict, logattacks) 2318c2aa98e2SPeter Wemm char *s; 2319c2aa98e2SPeter Wemm bool strict; 2320c2aa98e2SPeter Wemm bool logattacks; 2321c2aa98e2SPeter Wemm { 2322c2aa98e2SPeter Wemm register char *p; 2323c2aa98e2SPeter Wemm int l; 2324c2aa98e2SPeter Wemm static char *bp = NULL; 2325c2aa98e2SPeter Wemm static int bl = 0; 2326c2aa98e2SPeter Wemm 2327c2aa98e2SPeter Wemm p = s; 2328c2aa98e2SPeter Wemm while ((p = strchr(p, '\n')) != NULL) 2329c2aa98e2SPeter Wemm if (strict || (*++p != ' ' && *p != '\t')) 2330c2aa98e2SPeter Wemm break; 2331c2aa98e2SPeter Wemm if (p == NULL) 2332c2aa98e2SPeter Wemm return s; 2333c2aa98e2SPeter Wemm 2334c2aa98e2SPeter Wemm l = strlen(s) + 1; 2335c2aa98e2SPeter Wemm if (bl < l) 2336c2aa98e2SPeter Wemm { 2337c2aa98e2SPeter Wemm /* allocate more space */ 233840266059SGregory Neil Shapiro char *nbp = sm_pmalloc_x(l); 233940266059SGregory Neil Shapiro 2340c2aa98e2SPeter Wemm if (bp != NULL) 23418774250cSGregory Neil Shapiro sm_free(bp); 234240266059SGregory Neil Shapiro bp = nbp; 2343c2aa98e2SPeter Wemm bl = l; 2344c2aa98e2SPeter Wemm } 234540266059SGregory Neil Shapiro (void) sm_strlcpy(bp, s, l); 2346c2aa98e2SPeter Wemm for (p = bp; (p = strchr(p, '\n')) != NULL; ) 2347c2aa98e2SPeter Wemm *p++ = ' '; 2348c2aa98e2SPeter Wemm 2349c2aa98e2SPeter Wemm if (logattacks) 2350c2aa98e2SPeter Wemm { 2351d0cef73dSGregory Neil Shapiro sm_syslog(LOG_NOTICE, CurEnv ? CurEnv->e_id : NULL, 2352c2aa98e2SPeter Wemm "POSSIBLE ATTACK from %.100s: newline in string \"%s\"", 2353c2aa98e2SPeter Wemm RealHostName == NULL ? "[UNKNOWN]" : RealHostName, 2354c2aa98e2SPeter Wemm shortenstring(bp, MAXSHORTSTR)); 2355c2aa98e2SPeter Wemm } 2356c2aa98e2SPeter Wemm 2357c2aa98e2SPeter Wemm return bp; 2358c2aa98e2SPeter Wemm } 2359739ac4d4SGregory Neil Shapiro 2360739ac4d4SGregory Neil Shapiro /* 2361739ac4d4SGregory Neil Shapiro ** STRREPLNONPRT -- replace "unprintable" characters in a string with subst 2362739ac4d4SGregory Neil Shapiro ** 2363739ac4d4SGregory Neil Shapiro ** Parameters: 2364739ac4d4SGregory Neil Shapiro ** s -- string to manipulate (in place) 2365739ac4d4SGregory Neil Shapiro ** subst -- character to use as replacement 2366739ac4d4SGregory Neil Shapiro ** 2367739ac4d4SGregory Neil Shapiro ** Returns: 2368739ac4d4SGregory Neil Shapiro ** true iff string did not contain "unprintable" characters 2369739ac4d4SGregory Neil Shapiro */ 2370739ac4d4SGregory Neil Shapiro 2371739ac4d4SGregory Neil Shapiro bool 2372739ac4d4SGregory Neil Shapiro strreplnonprt(s, c) 2373739ac4d4SGregory Neil Shapiro char *s; 2374739ac4d4SGregory Neil Shapiro int c; 2375739ac4d4SGregory Neil Shapiro { 2376739ac4d4SGregory Neil Shapiro bool ok; 2377739ac4d4SGregory Neil Shapiro 2378739ac4d4SGregory Neil Shapiro ok = true; 2379739ac4d4SGregory Neil Shapiro if (s == NULL) 2380739ac4d4SGregory Neil Shapiro return ok; 2381739ac4d4SGregory Neil Shapiro while (*s != '\0') 2382739ac4d4SGregory Neil Shapiro { 2383739ac4d4SGregory Neil Shapiro if (!(isascii(*s) && isprint(*s))) 2384739ac4d4SGregory Neil Shapiro { 2385739ac4d4SGregory Neil Shapiro *s = c; 2386739ac4d4SGregory Neil Shapiro ok = false; 2387739ac4d4SGregory Neil Shapiro } 2388739ac4d4SGregory Neil Shapiro ++s; 2389739ac4d4SGregory Neil Shapiro } 2390739ac4d4SGregory Neil Shapiro return ok; 2391739ac4d4SGregory Neil Shapiro } 2392739ac4d4SGregory Neil Shapiro 239340266059SGregory Neil Shapiro /* 2394c2aa98e2SPeter Wemm ** PATH_IS_DIR -- check to see if file exists and is a directory. 2395c2aa98e2SPeter Wemm ** 2396c2aa98e2SPeter Wemm ** There are some additional checks for security violations in 2397c2aa98e2SPeter Wemm ** here. This routine is intended to be used for the host status 2398c2aa98e2SPeter Wemm ** support. 2399c2aa98e2SPeter Wemm ** 2400c2aa98e2SPeter Wemm ** Parameters: 2401c2aa98e2SPeter Wemm ** pathname -- pathname to check for directory-ness. 2402c2aa98e2SPeter Wemm ** createflag -- if set, create directory if needed. 2403c2aa98e2SPeter Wemm ** 2404c2aa98e2SPeter Wemm ** Returns: 240540266059SGregory Neil Shapiro ** true -- if the indicated pathname is a directory 240640266059SGregory Neil Shapiro ** false -- otherwise 2407c2aa98e2SPeter Wemm */ 2408c2aa98e2SPeter Wemm 2409a7ec597cSGregory Neil Shapiro bool 2410c2aa98e2SPeter Wemm path_is_dir(pathname, createflag) 2411c2aa98e2SPeter Wemm char *pathname; 2412c2aa98e2SPeter Wemm bool createflag; 2413c2aa98e2SPeter Wemm { 2414c2aa98e2SPeter Wemm struct stat statbuf; 2415c2aa98e2SPeter Wemm 2416c2aa98e2SPeter Wemm #if HASLSTAT 2417c2aa98e2SPeter Wemm if (lstat(pathname, &statbuf) < 0) 241806f25ae9SGregory Neil Shapiro #else /* HASLSTAT */ 2419c2aa98e2SPeter Wemm if (stat(pathname, &statbuf) < 0) 242006f25ae9SGregory Neil Shapiro #endif /* HASLSTAT */ 2421c2aa98e2SPeter Wemm { 2422c2aa98e2SPeter Wemm if (errno != ENOENT || !createflag) 242340266059SGregory Neil Shapiro return false; 2424c2aa98e2SPeter Wemm if (mkdir(pathname, 0755) < 0) 242540266059SGregory Neil Shapiro return false; 242640266059SGregory Neil Shapiro return true; 2427c2aa98e2SPeter Wemm } 2428c2aa98e2SPeter Wemm if (!S_ISDIR(statbuf.st_mode)) 2429c2aa98e2SPeter Wemm { 2430c2aa98e2SPeter Wemm errno = ENOTDIR; 243140266059SGregory Neil Shapiro return false; 2432c2aa98e2SPeter Wemm } 2433c2aa98e2SPeter Wemm 2434c2aa98e2SPeter Wemm /* security: don't allow writable directories */ 2435c2aa98e2SPeter Wemm if (bitset(S_IWGRP|S_IWOTH, statbuf.st_mode)) 2436c2aa98e2SPeter Wemm { 2437c2aa98e2SPeter Wemm errno = EACCES; 243840266059SGregory Neil Shapiro return false; 2439c2aa98e2SPeter Wemm } 244040266059SGregory Neil Shapiro return true; 2441c2aa98e2SPeter Wemm } 2442d0cef73dSGregory Neil Shapiro 244340266059SGregory Neil Shapiro /* 2444c2aa98e2SPeter Wemm ** PROC_LIST_ADD -- add process id to list of our children 2445c2aa98e2SPeter Wemm ** 2446c2aa98e2SPeter Wemm ** Parameters: 2447c2aa98e2SPeter Wemm ** pid -- pid to add to list. 244806f25ae9SGregory Neil Shapiro ** task -- task of pid. 244906f25ae9SGregory Neil Shapiro ** type -- type of process. 245040266059SGregory Neil Shapiro ** count -- number of processes. 245140266059SGregory Neil Shapiro ** other -- other information for this type. 2452c2aa98e2SPeter Wemm ** 2453c2aa98e2SPeter Wemm ** Returns: 2454c2aa98e2SPeter Wemm ** none 245540266059SGregory Neil Shapiro ** 245640266059SGregory Neil Shapiro ** Side Effects: 245740266059SGregory Neil Shapiro ** May increase CurChildren. May grow ProcList. 2458c2aa98e2SPeter Wemm */ 2459c2aa98e2SPeter Wemm 246040266059SGregory Neil Shapiro typedef struct procs PROCS_T; 246140266059SGregory Neil Shapiro 246240266059SGregory Neil Shapiro struct procs 246340266059SGregory Neil Shapiro { 246440266059SGregory Neil Shapiro pid_t proc_pid; 246540266059SGregory Neil Shapiro char *proc_task; 246640266059SGregory Neil Shapiro int proc_type; 246740266059SGregory Neil Shapiro int proc_count; 246840266059SGregory Neil Shapiro int proc_other; 2469e92d3f3fSGregory Neil Shapiro SOCKADDR proc_hostaddr; 247040266059SGregory Neil Shapiro }; 247140266059SGregory Neil Shapiro 247240266059SGregory Neil Shapiro static PROCS_T *volatile ProcListVec = NULL; 2473c2aa98e2SPeter Wemm static int ProcListSize = 0; 2474c2aa98e2SPeter Wemm 2475c2aa98e2SPeter Wemm void 2476e92d3f3fSGregory Neil Shapiro proc_list_add(pid, task, type, count, other, hostaddr) 2477c2aa98e2SPeter Wemm pid_t pid; 2478065a643dSPeter Wemm char *task; 247906f25ae9SGregory Neil Shapiro int type; 248040266059SGregory Neil Shapiro int count; 248140266059SGregory Neil Shapiro int other; 2482e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr; 2483c2aa98e2SPeter Wemm { 2484c2aa98e2SPeter Wemm int i; 2485c2aa98e2SPeter Wemm 2486c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2487c2aa98e2SPeter Wemm { 2488065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2489c2aa98e2SPeter Wemm break; 2490c2aa98e2SPeter Wemm } 2491c2aa98e2SPeter Wemm if (i >= ProcListSize) 2492c2aa98e2SPeter Wemm { 2493c2aa98e2SPeter Wemm /* probe the existing vector to avoid growing infinitely */ 2494c2aa98e2SPeter Wemm proc_list_probe(); 2495c2aa98e2SPeter Wemm 2496c2aa98e2SPeter Wemm /* now scan again */ 2497c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2498c2aa98e2SPeter Wemm { 2499065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2500c2aa98e2SPeter Wemm break; 2501c2aa98e2SPeter Wemm } 2502c2aa98e2SPeter Wemm } 2503c2aa98e2SPeter Wemm if (i >= ProcListSize) 2504c2aa98e2SPeter Wemm { 2505c2aa98e2SPeter Wemm /* grow process list */ 2506d0cef73dSGregory Neil Shapiro int chldwasblocked; 250740266059SGregory Neil Shapiro PROCS_T *npv; 2508c2aa98e2SPeter Wemm 250940266059SGregory Neil Shapiro SM_ASSERT(ProcListSize < INT_MAX - PROC_LIST_SEG); 2510d0cef73dSGregory Neil Shapiro npv = (PROCS_T *) sm_pmalloc_x((sizeof(*npv)) * 251106f25ae9SGregory Neil Shapiro (ProcListSize + PROC_LIST_SEG)); 2512d0cef73dSGregory Neil Shapiro 2513d0cef73dSGregory Neil Shapiro /* Block SIGCHLD so reapchild() doesn't mess with us */ 2514d0cef73dSGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD); 2515c2aa98e2SPeter Wemm if (ProcListSize > 0) 2516c2aa98e2SPeter Wemm { 251706f25ae9SGregory Neil Shapiro memmove(npv, ProcListVec, 251840266059SGregory Neil Shapiro ProcListSize * sizeof(PROCS_T)); 25198774250cSGregory Neil Shapiro sm_free(ProcListVec); 2520c2aa98e2SPeter Wemm } 252140266059SGregory Neil Shapiro 252240266059SGregory Neil Shapiro /* XXX just use memset() to initialize this part? */ 2523c2aa98e2SPeter Wemm for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++) 2524065a643dSPeter Wemm { 2525065a643dSPeter Wemm npv[i].proc_pid = NO_PID; 2526065a643dSPeter Wemm npv[i].proc_task = NULL; 252706f25ae9SGregory Neil Shapiro npv[i].proc_type = PROC_NONE; 2528065a643dSPeter Wemm } 2529c2aa98e2SPeter Wemm i = ProcListSize; 2530c2aa98e2SPeter Wemm ProcListSize += PROC_LIST_SEG; 2531c2aa98e2SPeter Wemm ProcListVec = npv; 2532d0cef73dSGregory Neil Shapiro if (chldwasblocked == 0) 2533d0cef73dSGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD); 2534c2aa98e2SPeter Wemm } 2535065a643dSPeter Wemm ProcListVec[i].proc_pid = pid; 253640266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task); 253706f25ae9SGregory Neil Shapiro ProcListVec[i].proc_type = type; 253840266059SGregory Neil Shapiro ProcListVec[i].proc_count = count; 253940266059SGregory Neil Shapiro ProcListVec[i].proc_other = other; 2540e92d3f3fSGregory Neil Shapiro if (hostaddr != NULL) 2541e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr = *hostaddr; 2542e92d3f3fSGregory Neil Shapiro else 2543e92d3f3fSGregory Neil Shapiro memset(&ProcListVec[i].proc_hostaddr, 0, 2544e92d3f3fSGregory Neil Shapiro sizeof(ProcListVec[i].proc_hostaddr)); 2545065a643dSPeter Wemm 2546065a643dSPeter Wemm /* if process adding itself, it's not a child */ 254740266059SGregory Neil Shapiro if (pid != CurrentPid) 254840266059SGregory Neil Shapiro { 254940266059SGregory Neil Shapiro SM_ASSERT(CurChildren < INT_MAX); 2550c2aa98e2SPeter Wemm CurChildren++; 2551c2aa98e2SPeter Wemm } 255240266059SGregory Neil Shapiro } 2553d0cef73dSGregory Neil Shapiro 255440266059SGregory Neil Shapiro /* 2555065a643dSPeter Wemm ** PROC_LIST_SET -- set pid task in process list 2556065a643dSPeter Wemm ** 2557065a643dSPeter Wemm ** Parameters: 2558065a643dSPeter Wemm ** pid -- pid to set 2559065a643dSPeter Wemm ** task -- task of pid 2560065a643dSPeter Wemm ** 2561065a643dSPeter Wemm ** Returns: 2562065a643dSPeter Wemm ** none. 2563065a643dSPeter Wemm */ 2564065a643dSPeter Wemm 2565065a643dSPeter Wemm void 2566065a643dSPeter Wemm proc_list_set(pid, task) 2567065a643dSPeter Wemm pid_t pid; 2568065a643dSPeter Wemm char *task; 2569065a643dSPeter Wemm { 2570065a643dSPeter Wemm int i; 2571065a643dSPeter Wemm 2572065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++) 2573065a643dSPeter Wemm { 2574065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid) 2575065a643dSPeter Wemm { 257640266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task); 2577065a643dSPeter Wemm break; 2578065a643dSPeter Wemm } 2579065a643dSPeter Wemm } 2580065a643dSPeter Wemm } 2581d0cef73dSGregory Neil Shapiro 258240266059SGregory Neil Shapiro /* 2583c2aa98e2SPeter Wemm ** PROC_LIST_DROP -- drop pid from process list 2584c2aa98e2SPeter Wemm ** 2585c2aa98e2SPeter Wemm ** Parameters: 2586c2aa98e2SPeter Wemm ** pid -- pid to drop 258740266059SGregory Neil Shapiro ** st -- process status 258840266059SGregory Neil Shapiro ** other -- storage for proc_other (return). 2589c2aa98e2SPeter Wemm ** 2590c2aa98e2SPeter Wemm ** Returns: 259140266059SGregory Neil Shapiro ** none. 259240266059SGregory Neil Shapiro ** 259340266059SGregory Neil Shapiro ** Side Effects: 259440266059SGregory Neil Shapiro ** May decrease CurChildren, CurRunners, or 259540266059SGregory Neil Shapiro ** set RestartRequest or ShutdownRequest. 25968774250cSGregory Neil Shapiro ** 25978774250cSGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 25988774250cSGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 25998774250cSGregory Neil Shapiro ** DOING. 2600c2aa98e2SPeter Wemm */ 2601c2aa98e2SPeter Wemm 260240266059SGregory Neil Shapiro void 260340266059SGregory Neil Shapiro proc_list_drop(pid, st, other) 2604c2aa98e2SPeter Wemm pid_t pid; 260540266059SGregory Neil Shapiro int st; 260640266059SGregory Neil Shapiro int *other; 2607c2aa98e2SPeter Wemm { 2608c2aa98e2SPeter Wemm int i; 260906f25ae9SGregory Neil Shapiro int type = PROC_NONE; 2610c2aa98e2SPeter Wemm 2611c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2612c2aa98e2SPeter Wemm { 2613065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid) 2614c2aa98e2SPeter Wemm { 2615065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 261606f25ae9SGregory Neil Shapiro type = ProcListVec[i].proc_type; 261740266059SGregory Neil Shapiro if (other != NULL) 261840266059SGregory Neil Shapiro *other = ProcListVec[i].proc_other; 26194e4196cbSGregory Neil Shapiro if (CurChildren > 0) 26204e4196cbSGregory Neil Shapiro CurChildren--; 2621c2aa98e2SPeter Wemm break; 2622c2aa98e2SPeter Wemm } 2623c2aa98e2SPeter Wemm } 262406f25ae9SGregory Neil Shapiro 262506f25ae9SGregory Neil Shapiro 262640266059SGregory Neil Shapiro if (type == PROC_CONTROL && WIFEXITED(st)) 262740266059SGregory Neil Shapiro { 262840266059SGregory Neil Shapiro /* if so, see if we need to restart or shutdown */ 262940266059SGregory Neil Shapiro if (WEXITSTATUS(st) == EX_RESTART) 263040266059SGregory Neil Shapiro RestartRequest = "control socket"; 263140266059SGregory Neil Shapiro else if (WEXITSTATUS(st) == EX_SHUTDOWN) 263240266059SGregory Neil Shapiro ShutdownRequest = "control socket"; 2633c2aa98e2SPeter Wemm } 263440266059SGregory Neil Shapiro else if (type == PROC_QUEUE_CHILD && !WIFSTOPPED(st) && 263540266059SGregory Neil Shapiro ProcListVec[i].proc_other > -1) 263640266059SGregory Neil Shapiro { 263740266059SGregory Neil Shapiro /* restart this persistent runner */ 263840266059SGregory Neil Shapiro mark_work_group_restart(ProcListVec[i].proc_other, st); 263940266059SGregory Neil Shapiro } 264040266059SGregory Neil Shapiro else if (type == PROC_QUEUE) 264140266059SGregory Neil Shapiro CurRunners -= ProcListVec[i].proc_count; 264240266059SGregory Neil Shapiro } 2643d0cef73dSGregory Neil Shapiro 264440266059SGregory Neil Shapiro /* 2645c2aa98e2SPeter Wemm ** PROC_LIST_CLEAR -- clear the process list 2646c2aa98e2SPeter Wemm ** 2647c2aa98e2SPeter Wemm ** Parameters: 2648c2aa98e2SPeter Wemm ** none. 2649c2aa98e2SPeter Wemm ** 2650c2aa98e2SPeter Wemm ** Returns: 2651c2aa98e2SPeter Wemm ** none. 265240266059SGregory Neil Shapiro ** 265340266059SGregory Neil Shapiro ** Side Effects: 265440266059SGregory Neil Shapiro ** Sets CurChildren to zero. 2655c2aa98e2SPeter Wemm */ 2656c2aa98e2SPeter Wemm 2657c2aa98e2SPeter Wemm void 2658c2aa98e2SPeter Wemm proc_list_clear() 2659c2aa98e2SPeter Wemm { 2660c2aa98e2SPeter Wemm int i; 2661c2aa98e2SPeter Wemm 2662065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */ 2663065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++) 2664065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 2665c2aa98e2SPeter Wemm CurChildren = 0; 2666c2aa98e2SPeter Wemm } 2667d0cef73dSGregory Neil Shapiro 266840266059SGregory Neil Shapiro /* 2669c2aa98e2SPeter Wemm ** PROC_LIST_PROBE -- probe processes in the list to see if they still exist 2670c2aa98e2SPeter Wemm ** 2671c2aa98e2SPeter Wemm ** Parameters: 2672c2aa98e2SPeter Wemm ** none 2673c2aa98e2SPeter Wemm ** 2674c2aa98e2SPeter Wemm ** Returns: 2675c2aa98e2SPeter Wemm ** none 267640266059SGregory Neil Shapiro ** 267740266059SGregory Neil Shapiro ** Side Effects: 267840266059SGregory Neil Shapiro ** May decrease CurChildren. 2679c2aa98e2SPeter Wemm */ 2680c2aa98e2SPeter Wemm 2681c2aa98e2SPeter Wemm void 2682c2aa98e2SPeter Wemm proc_list_probe() 2683c2aa98e2SPeter Wemm { 26844e4196cbSGregory Neil Shapiro int i, children; 26854e4196cbSGregory Neil Shapiro int chldwasblocked; 26864e4196cbSGregory Neil Shapiro pid_t pid; 26874e4196cbSGregory Neil Shapiro 26884e4196cbSGregory Neil Shapiro children = 0; 26894e4196cbSGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD); 2690c2aa98e2SPeter Wemm 2691065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */ 2692065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++) 2693c2aa98e2SPeter Wemm { 26944e4196cbSGregory Neil Shapiro pid = ProcListVec[i].proc_pid; 26954e4196cbSGregory Neil Shapiro if (pid == NO_PID || pid == CurrentPid) 2696c2aa98e2SPeter Wemm continue; 26974e4196cbSGregory Neil Shapiro if (kill(pid, 0) < 0) 2698c2aa98e2SPeter Wemm { 2699c2aa98e2SPeter Wemm if (LogLevel > 3) 2700c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id, 2701c2aa98e2SPeter Wemm "proc_list_probe: lost pid %d", 2702065a643dSPeter Wemm (int) ProcListVec[i].proc_pid); 2703065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 270440266059SGregory Neil Shapiro SM_FREE_CLR(ProcListVec[i].proc_task); 2705c2aa98e2SPeter Wemm CurChildren--; 2706c2aa98e2SPeter Wemm } 27074e4196cbSGregory Neil Shapiro else 27084e4196cbSGregory Neil Shapiro { 27094e4196cbSGregory Neil Shapiro ++children; 27104e4196cbSGregory Neil Shapiro } 2711c2aa98e2SPeter Wemm } 2712c2aa98e2SPeter Wemm if (CurChildren < 0) 2713c2aa98e2SPeter Wemm CurChildren = 0; 27144e4196cbSGregory Neil Shapiro if (chldwasblocked == 0) 27154e4196cbSGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD); 2716af9557fdSGregory Neil Shapiro if (LogLevel > 10 && children != CurChildren && CurrentPid == DaemonPid) 27174e4196cbSGregory Neil Shapiro { 27184e4196cbSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID, 27194e4196cbSGregory Neil Shapiro "proc_list_probe: found %d children, expected %d", 27204e4196cbSGregory Neil Shapiro children, CurChildren); 27214e4196cbSGregory Neil Shapiro } 2722c2aa98e2SPeter Wemm } 272340266059SGregory Neil Shapiro 272440266059SGregory Neil Shapiro /* 2725065a643dSPeter Wemm ** PROC_LIST_DISPLAY -- display the process list 2726065a643dSPeter Wemm ** 2727065a643dSPeter Wemm ** Parameters: 2728065a643dSPeter Wemm ** out -- output file pointer 272940266059SGregory Neil Shapiro ** prefix -- string to output in front of each line. 2730065a643dSPeter Wemm ** 2731065a643dSPeter Wemm ** Returns: 2732065a643dSPeter Wemm ** none. 2733065a643dSPeter Wemm */ 2734065a643dSPeter Wemm 2735065a643dSPeter Wemm void 273640266059SGregory Neil Shapiro proc_list_display(out, prefix) 273740266059SGregory Neil Shapiro SM_FILE_T *out; 273840266059SGregory Neil Shapiro char *prefix; 2739065a643dSPeter Wemm { 2740065a643dSPeter Wemm int i; 2741065a643dSPeter Wemm 2742065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++) 2743065a643dSPeter Wemm { 2744065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2745065a643dSPeter Wemm continue; 2746065a643dSPeter Wemm 274740266059SGregory Neil Shapiro (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "%s%d %s%s\n", 274840266059SGregory Neil Shapiro prefix, 274940266059SGregory Neil Shapiro (int) ProcListVec[i].proc_pid, 2750065a643dSPeter Wemm ProcListVec[i].proc_task != NULL ? 2751065a643dSPeter Wemm ProcListVec[i].proc_task : "(unknown)", 2752065a643dSPeter Wemm (OpMode == MD_SMTP || 2753065a643dSPeter Wemm OpMode == MD_DAEMON || 2754065a643dSPeter Wemm OpMode == MD_ARPAFTP) ? "\r" : ""); 2755065a643dSPeter Wemm } 2756065a643dSPeter Wemm } 275740266059SGregory Neil Shapiro 275840266059SGregory Neil Shapiro /* 275940266059SGregory Neil Shapiro ** PROC_LIST_SIGNAL -- send a signal to a type of process in the list 276013058a91SGregory Neil Shapiro ** 276113058a91SGregory Neil Shapiro ** Parameters: 276240266059SGregory Neil Shapiro ** type -- type of process to signal 276340266059SGregory Neil Shapiro ** signal -- the type of signal to send 276413058a91SGregory Neil Shapiro ** 276540266059SGregory Neil Shapiro ** Results: 276640266059SGregory Neil Shapiro ** none. 2767c2aa98e2SPeter Wemm ** 276840266059SGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 276940266059SGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 277040266059SGregory Neil Shapiro ** DOING. 2771c2aa98e2SPeter Wemm */ 2772c2aa98e2SPeter Wemm 277340266059SGregory Neil Shapiro void 277440266059SGregory Neil Shapiro proc_list_signal(type, signal) 277540266059SGregory Neil Shapiro int type; 277640266059SGregory Neil Shapiro int signal; 2777c2aa98e2SPeter Wemm { 277840266059SGregory Neil Shapiro int chldwasblocked; 277940266059SGregory Neil Shapiro int alrmwasblocked; 278040266059SGregory Neil Shapiro int i; 278140266059SGregory Neil Shapiro pid_t mypid = getpid(); 2782c2aa98e2SPeter Wemm 278340266059SGregory Neil Shapiro /* block these signals so that we may signal cleanly */ 278440266059SGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD); 278540266059SGregory Neil Shapiro alrmwasblocked = sm_blocksignal(SIGALRM); 278640266059SGregory Neil Shapiro 278740266059SGregory Neil Shapiro /* Find all processes of type and send signal */ 278840266059SGregory Neil Shapiro for (i = 0; i < ProcListSize; i++) 278940266059SGregory Neil Shapiro { 279040266059SGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID || 279140266059SGregory Neil Shapiro ProcListVec[i].proc_pid == mypid) 279240266059SGregory Neil Shapiro continue; 279340266059SGregory Neil Shapiro if (ProcListVec[i].proc_type != type) 279440266059SGregory Neil Shapiro continue; 279540266059SGregory Neil Shapiro (void) kill(ProcListVec[i].proc_pid, signal); 2796c2aa98e2SPeter Wemm } 2797c2aa98e2SPeter Wemm 279840266059SGregory Neil Shapiro /* restore the signals */ 279940266059SGregory Neil Shapiro if (alrmwasblocked == 0) 280040266059SGregory Neil Shapiro (void) sm_releasesignal(SIGALRM); 280140266059SGregory Neil Shapiro if (chldwasblocked == 0) 280240266059SGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD); 2803c2aa98e2SPeter Wemm } 2804e92d3f3fSGregory Neil Shapiro 2805e92d3f3fSGregory Neil Shapiro /* 2806e92d3f3fSGregory Neil Shapiro ** COUNT_OPEN_CONNECTIONS 2807e92d3f3fSGregory Neil Shapiro ** 2808e92d3f3fSGregory Neil Shapiro ** Parameters: 2809e92d3f3fSGregory Neil Shapiro ** hostaddr - ClientAddress 2810e92d3f3fSGregory Neil Shapiro ** 2811e92d3f3fSGregory Neil Shapiro ** Returns: 2812e92d3f3fSGregory Neil Shapiro ** the number of open connections for this client 2813e92d3f3fSGregory Neil Shapiro ** 2814e92d3f3fSGregory Neil Shapiro */ 2815e92d3f3fSGregory Neil Shapiro 2816e92d3f3fSGregory Neil Shapiro int 2817e92d3f3fSGregory Neil Shapiro count_open_connections(hostaddr) 2818e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr; 2819e92d3f3fSGregory Neil Shapiro { 2820e92d3f3fSGregory Neil Shapiro int i, n; 2821e92d3f3fSGregory Neil Shapiro 2822e92d3f3fSGregory Neil Shapiro if (hostaddr == NULL) 2823e92d3f3fSGregory Neil Shapiro return 0; 2824ffb83623SGregory Neil Shapiro 2825ffb83623SGregory Neil Shapiro /* 2826ffb83623SGregory Neil Shapiro ** Initialize to 1 instead of 0 because this code gets called 2827ffb83623SGregory Neil Shapiro ** before proc_list_add() gets called, so we (the daemon child 2828ffb83623SGregory Neil Shapiro ** for this connection) don't count ourselves. 2829ffb83623SGregory Neil Shapiro */ 2830ffb83623SGregory Neil Shapiro 2831ffb83623SGregory Neil Shapiro n = 1; 2832e92d3f3fSGregory Neil Shapiro for (i = 0; i < ProcListSize; i++) 2833e92d3f3fSGregory Neil Shapiro { 2834e92d3f3fSGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID) 2835e92d3f3fSGregory Neil Shapiro continue; 2836e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family != 2837e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sa.sa_family) 2838e92d3f3fSGregory Neil Shapiro continue; 2839e92d3f3fSGregory Neil Shapiro #if NETINET 2840e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET && 2841e92d3f3fSGregory Neil Shapiro (hostaddr->sin.sin_addr.s_addr == 2842e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sin.sin_addr.s_addr)) 2843e92d3f3fSGregory Neil Shapiro n++; 2844e92d3f3fSGregory Neil Shapiro #endif /* NETINET */ 2845e92d3f3fSGregory Neil Shapiro #if NETINET6 2846e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET6 && 2847e92d3f3fSGregory Neil Shapiro IN6_ARE_ADDR_EQUAL(&(hostaddr->sin6.sin6_addr), 2848e92d3f3fSGregory Neil Shapiro &(ProcListVec[i].proc_hostaddr.sin6.sin6_addr))) 2849e92d3f3fSGregory Neil Shapiro n++; 2850e92d3f3fSGregory Neil Shapiro #endif /* NETINET6 */ 2851e92d3f3fSGregory Neil Shapiro } 2852e92d3f3fSGregory Neil Shapiro return n; 2853e92d3f3fSGregory Neil Shapiro } 2854