1c2aa98e2SPeter Wemm /* 2e92d3f3fSGregory Neil Shapiro * Copyright (c) 1998-2004 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 16e92d3f3fSGregory Neil Shapiro SM_RCSID("@(#)$Id: util.c,v 8.382 2004/03/26 19:01:10 ca Exp $") 1740266059SGregory Neil Shapiro 18c2aa98e2SPeter Wemm #include <sysexits.h> 1940266059SGregory Neil Shapiro #include <sm/xtrap.h> 2006f25ae9SGregory Neil Shapiro 2140266059SGregory Neil Shapiro /* 22e92d3f3fSGregory Neil Shapiro ** NEWSTR -- Create a copy of a C string 23e92d3f3fSGregory Neil Shapiro ** 24e92d3f3fSGregory Neil Shapiro ** Parameters: 25e92d3f3fSGregory Neil Shapiro ** s -- the string to copy. 26e92d3f3fSGregory Neil Shapiro ** 27e92d3f3fSGregory Neil Shapiro ** Returns: 28e92d3f3fSGregory Neil Shapiro ** pointer to newly allocated string. 29e92d3f3fSGregory Neil Shapiro */ 30e92d3f3fSGregory Neil Shapiro 31e92d3f3fSGregory Neil Shapiro char * 32e92d3f3fSGregory Neil Shapiro newstr(s) 33e92d3f3fSGregory Neil Shapiro const char *s; 34e92d3f3fSGregory Neil Shapiro { 35e92d3f3fSGregory Neil Shapiro size_t l; 36e92d3f3fSGregory Neil Shapiro char *n; 37e92d3f3fSGregory Neil Shapiro 38e92d3f3fSGregory Neil Shapiro l = strlen(s); 39e92d3f3fSGregory Neil Shapiro SM_ASSERT(l + 1 > l); 40e92d3f3fSGregory Neil Shapiro n = xalloc(l + 1); 41e92d3f3fSGregory Neil Shapiro sm_strlcpy(n, s, l + 1); 42e92d3f3fSGregory Neil Shapiro return n; 43e92d3f3fSGregory Neil Shapiro } 44e92d3f3fSGregory Neil Shapiro 45e92d3f3fSGregory Neil Shapiro /* 46c2aa98e2SPeter Wemm ** ADDQUOTES -- Adds quotes & quote bits to a string. 47c2aa98e2SPeter Wemm ** 4840266059SGregory Neil Shapiro ** Runs through a string and adds backslashes and quote bits. 49c2aa98e2SPeter Wemm ** 50c2aa98e2SPeter Wemm ** Parameters: 51c2aa98e2SPeter Wemm ** s -- the string to modify. 5240266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate result 53c2aa98e2SPeter Wemm ** 54c2aa98e2SPeter Wemm ** Returns: 55c2aa98e2SPeter Wemm ** pointer to quoted string. 56c2aa98e2SPeter Wemm */ 57c2aa98e2SPeter Wemm 58c2aa98e2SPeter Wemm char * 5940266059SGregory Neil Shapiro addquotes(s, rpool) 60c2aa98e2SPeter Wemm char *s; 6140266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 62c2aa98e2SPeter Wemm { 63c2aa98e2SPeter Wemm int len = 0; 64c2aa98e2SPeter Wemm char c; 65c2aa98e2SPeter Wemm char *p = s, *q, *r; 66c2aa98e2SPeter Wemm 67c2aa98e2SPeter Wemm if (s == NULL) 68c2aa98e2SPeter Wemm return NULL; 69c2aa98e2SPeter Wemm 70c2aa98e2SPeter Wemm /* Find length of quoted string */ 71c2aa98e2SPeter Wemm while ((c = *p++) != '\0') 72c2aa98e2SPeter Wemm { 73c2aa98e2SPeter Wemm len++; 74c2aa98e2SPeter Wemm if (c == '\\' || c == '"') 75c2aa98e2SPeter Wemm len++; 76c2aa98e2SPeter Wemm } 77c2aa98e2SPeter Wemm 7840266059SGregory Neil Shapiro q = r = sm_rpool_malloc_x(rpool, len + 3); 79c2aa98e2SPeter Wemm p = s; 80c2aa98e2SPeter Wemm 81c2aa98e2SPeter Wemm /* add leading quote */ 82c2aa98e2SPeter Wemm *q++ = '"'; 83c2aa98e2SPeter Wemm while ((c = *p++) != '\0') 84c2aa98e2SPeter Wemm { 85c2aa98e2SPeter Wemm /* quote \ or " */ 86c2aa98e2SPeter Wemm if (c == '\\' || c == '"') 87c2aa98e2SPeter Wemm *q++ = '\\'; 88c2aa98e2SPeter Wemm *q++ = c; 89c2aa98e2SPeter Wemm } 90c2aa98e2SPeter Wemm *q++ = '"'; 91c2aa98e2SPeter Wemm *q = '\0'; 92c2aa98e2SPeter Wemm return r; 93c2aa98e2SPeter Wemm } 9413bd1963SGregory Neil Shapiro 9513bd1963SGregory Neil Shapiro /* 9613bd1963SGregory Neil Shapiro ** STRIPBACKSLASH -- Strip leading backslash from a string. 9713bd1963SGregory Neil Shapiro ** 9813bd1963SGregory Neil Shapiro ** This is done in place. 9913bd1963SGregory Neil Shapiro ** 10013bd1963SGregory Neil Shapiro ** Parameters: 10113bd1963SGregory Neil Shapiro ** s -- the string to strip. 10213bd1963SGregory Neil Shapiro ** 10313bd1963SGregory Neil Shapiro ** Returns: 10413bd1963SGregory Neil Shapiro ** none. 10513bd1963SGregory Neil Shapiro */ 10613bd1963SGregory Neil Shapiro 10713bd1963SGregory Neil Shapiro void 10813bd1963SGregory Neil Shapiro stripbackslash(s) 10913bd1963SGregory Neil Shapiro char *s; 11013bd1963SGregory Neil Shapiro { 11113bd1963SGregory Neil Shapiro char *p, *q, c; 11213bd1963SGregory Neil Shapiro 11313bd1963SGregory Neil Shapiro if (s == NULL || *s == '\0') 11413bd1963SGregory Neil Shapiro return; 11513bd1963SGregory Neil Shapiro p = q = s; 11613bd1963SGregory Neil Shapiro while (*p == '\\' && (p[1] == '\\' || (isascii(p[1]) && isalnum(p[1])))) 11713bd1963SGregory Neil Shapiro p++; 11813bd1963SGregory Neil Shapiro do 11913bd1963SGregory Neil Shapiro { 12013bd1963SGregory Neil Shapiro c = *q++ = *p++; 12113bd1963SGregory Neil Shapiro } while (c != '\0'); 12213bd1963SGregory Neil Shapiro } 12313bd1963SGregory Neil Shapiro 12440266059SGregory Neil Shapiro /* 125c2aa98e2SPeter Wemm ** RFC822_STRING -- Checks string for proper RFC822 string quoting. 126c2aa98e2SPeter Wemm ** 127c2aa98e2SPeter Wemm ** Runs through a string and verifies RFC822 special characters 128c2aa98e2SPeter Wemm ** are only found inside comments, quoted strings, or backslash 129c2aa98e2SPeter Wemm ** escaped. Also verified balanced quotes and parenthesis. 130c2aa98e2SPeter Wemm ** 131c2aa98e2SPeter Wemm ** Parameters: 132c2aa98e2SPeter Wemm ** s -- the string to modify. 133c2aa98e2SPeter Wemm ** 134c2aa98e2SPeter Wemm ** Returns: 13540266059SGregory Neil Shapiro ** true iff the string is RFC822 compliant, false otherwise. 136c2aa98e2SPeter Wemm */ 137c2aa98e2SPeter Wemm 138c2aa98e2SPeter Wemm bool 139c2aa98e2SPeter Wemm rfc822_string(s) 140c2aa98e2SPeter Wemm char *s; 141c2aa98e2SPeter Wemm { 14240266059SGregory Neil Shapiro bool quoted = false; 143c2aa98e2SPeter Wemm int commentlev = 0; 144c2aa98e2SPeter Wemm char *c = s; 145c2aa98e2SPeter Wemm 146c2aa98e2SPeter Wemm if (s == NULL) 14740266059SGregory Neil Shapiro return false; 148c2aa98e2SPeter Wemm 149c2aa98e2SPeter Wemm while (*c != '\0') 150c2aa98e2SPeter Wemm { 151c2aa98e2SPeter Wemm /* escaped character */ 152c2aa98e2SPeter Wemm if (*c == '\\') 153c2aa98e2SPeter Wemm { 154c2aa98e2SPeter Wemm c++; 155c2aa98e2SPeter Wemm if (*c == '\0') 15640266059SGregory Neil Shapiro return false; 157c2aa98e2SPeter Wemm } 158c2aa98e2SPeter Wemm else if (commentlev == 0 && *c == '"') 159c2aa98e2SPeter Wemm quoted = !quoted; 160c2aa98e2SPeter Wemm else if (!quoted) 161c2aa98e2SPeter Wemm { 162c2aa98e2SPeter Wemm if (*c == ')') 163c2aa98e2SPeter Wemm { 164c2aa98e2SPeter Wemm /* unbalanced ')' */ 165c2aa98e2SPeter Wemm if (commentlev == 0) 16640266059SGregory Neil Shapiro return false; 167c2aa98e2SPeter Wemm else 168c2aa98e2SPeter Wemm commentlev--; 169c2aa98e2SPeter Wemm } 170c2aa98e2SPeter Wemm else if (*c == '(') 171c2aa98e2SPeter Wemm commentlev++; 172c2aa98e2SPeter Wemm else if (commentlev == 0 && 173c2aa98e2SPeter Wemm strchr(MustQuoteChars, *c) != NULL) 17440266059SGregory Neil Shapiro return false; 175c2aa98e2SPeter Wemm } 176c2aa98e2SPeter Wemm c++; 177c2aa98e2SPeter Wemm } 17840266059SGregory Neil Shapiro 179c2aa98e2SPeter Wemm /* unbalanced '"' or '(' */ 18040266059SGregory Neil Shapiro return !quoted && commentlev == 0; 181c2aa98e2SPeter Wemm } 18240266059SGregory Neil Shapiro /* 183065a643dSPeter Wemm ** SHORTEN_RFC822_STRING -- Truncate and rebalance an RFC822 string 184065a643dSPeter Wemm ** 18506f25ae9SGregory Neil Shapiro ** Arbitrarily shorten (in place) an RFC822 string and rebalance 186065a643dSPeter Wemm ** comments and quotes. 187065a643dSPeter Wemm ** 188065a643dSPeter Wemm ** Parameters: 189065a643dSPeter Wemm ** string -- the string to shorten 190065a643dSPeter Wemm ** length -- the maximum size, 0 if no maximum 191065a643dSPeter Wemm ** 192065a643dSPeter Wemm ** Returns: 19340266059SGregory Neil Shapiro ** true if string is changed, false otherwise 194065a643dSPeter Wemm ** 195065a643dSPeter Wemm ** Side Effects: 196065a643dSPeter Wemm ** Changes string in place, possibly resulting 197065a643dSPeter Wemm ** in a shorter string. 198065a643dSPeter Wemm */ 199065a643dSPeter Wemm 200065a643dSPeter Wemm bool 201065a643dSPeter Wemm shorten_rfc822_string(string, length) 202065a643dSPeter Wemm char *string; 203065a643dSPeter Wemm size_t length; 204065a643dSPeter Wemm { 20540266059SGregory Neil Shapiro bool backslash = false; 20640266059SGregory Neil Shapiro bool modified = false; 20740266059SGregory Neil Shapiro bool quoted = false; 208065a643dSPeter Wemm size_t slen; 209065a643dSPeter Wemm int parencount = 0; 210065a643dSPeter Wemm char *ptr = string; 211065a643dSPeter Wemm 212065a643dSPeter Wemm /* 213065a643dSPeter Wemm ** If have to rebalance an already short enough string, 214065a643dSPeter Wemm ** need to do it within allocated space. 215065a643dSPeter Wemm */ 216193538b7SGregory Neil Shapiro 217065a643dSPeter Wemm slen = strlen(string); 218065a643dSPeter Wemm if (length == 0 || slen < length) 219065a643dSPeter Wemm length = slen; 220065a643dSPeter Wemm 221065a643dSPeter Wemm while (*ptr != '\0') 222065a643dSPeter Wemm { 223065a643dSPeter Wemm if (backslash) 224065a643dSPeter Wemm { 22540266059SGregory Neil Shapiro backslash = false; 226065a643dSPeter Wemm goto increment; 227065a643dSPeter Wemm } 228065a643dSPeter Wemm 229065a643dSPeter Wemm if (*ptr == '\\') 23040266059SGregory Neil Shapiro backslash = true; 231065a643dSPeter Wemm else if (*ptr == '(') 232065a643dSPeter Wemm { 233065a643dSPeter Wemm if (!quoted) 234065a643dSPeter Wemm parencount++; 235065a643dSPeter Wemm } 236065a643dSPeter Wemm else if (*ptr == ')') 237065a643dSPeter Wemm { 238065a643dSPeter Wemm if (--parencount < 0) 239065a643dSPeter Wemm parencount = 0; 240065a643dSPeter Wemm } 241065a643dSPeter Wemm 242065a643dSPeter Wemm /* Inside a comment, quotes don't matter */ 243065a643dSPeter Wemm if (parencount <= 0 && *ptr == '"') 244065a643dSPeter Wemm quoted = !quoted; 245065a643dSPeter Wemm 246065a643dSPeter Wemm increment: 247065a643dSPeter Wemm /* Check for sufficient space for next character */ 24806f25ae9SGregory Neil Shapiro if (length - (ptr - string) <= (size_t) ((backslash ? 1 : 0) + 249065a643dSPeter Wemm parencount + 250065a643dSPeter Wemm (quoted ? 1 : 0))) 251065a643dSPeter Wemm { 252065a643dSPeter Wemm /* Not enough, backtrack */ 253065a643dSPeter Wemm if (*ptr == '\\') 25440266059SGregory Neil Shapiro backslash = false; 255065a643dSPeter Wemm else if (*ptr == '(' && !quoted) 256065a643dSPeter Wemm parencount--; 257065a643dSPeter Wemm else if (*ptr == '"' && parencount == 0) 25840266059SGregory Neil Shapiro quoted = false; 259065a643dSPeter Wemm break; 260065a643dSPeter Wemm } 261065a643dSPeter Wemm ptr++; 262065a643dSPeter Wemm } 263065a643dSPeter Wemm 264065a643dSPeter Wemm /* Rebalance */ 265065a643dSPeter Wemm while (parencount-- > 0) 266065a643dSPeter Wemm { 267065a643dSPeter Wemm if (*ptr != ')') 268065a643dSPeter Wemm { 26940266059SGregory Neil Shapiro modified = true; 270065a643dSPeter Wemm *ptr = ')'; 271065a643dSPeter Wemm } 272065a643dSPeter Wemm ptr++; 273065a643dSPeter Wemm } 274065a643dSPeter Wemm if (quoted) 275065a643dSPeter Wemm { 276065a643dSPeter Wemm if (*ptr != '"') 277065a643dSPeter Wemm { 27840266059SGregory Neil Shapiro modified = true; 279065a643dSPeter Wemm *ptr = '"'; 280065a643dSPeter Wemm } 281065a643dSPeter Wemm ptr++; 282065a643dSPeter Wemm } 283065a643dSPeter Wemm if (*ptr != '\0') 284065a643dSPeter Wemm { 28540266059SGregory Neil Shapiro modified = true; 286065a643dSPeter Wemm *ptr = '\0'; 287065a643dSPeter Wemm } 288065a643dSPeter Wemm return modified; 289065a643dSPeter Wemm } 29040266059SGregory Neil Shapiro /* 291065a643dSPeter Wemm ** FIND_CHARACTER -- find an unquoted character in an RFC822 string 292065a643dSPeter Wemm ** 293065a643dSPeter Wemm ** Find an unquoted, non-commented character in an RFC822 294065a643dSPeter Wemm ** string and return a pointer to its location in the 295065a643dSPeter Wemm ** string. 296065a643dSPeter Wemm ** 297065a643dSPeter Wemm ** Parameters: 298065a643dSPeter Wemm ** string -- the string to search 299065a643dSPeter Wemm ** character -- the character to find 300065a643dSPeter Wemm ** 301065a643dSPeter Wemm ** Returns: 302065a643dSPeter Wemm ** pointer to the character, or 303065a643dSPeter Wemm ** a pointer to the end of the line if character is not found 304065a643dSPeter Wemm */ 305065a643dSPeter Wemm 306065a643dSPeter Wemm char * 307065a643dSPeter Wemm find_character(string, character) 308065a643dSPeter Wemm char *string; 30906f25ae9SGregory Neil Shapiro int character; 310065a643dSPeter Wemm { 31140266059SGregory Neil Shapiro bool backslash = false; 31240266059SGregory Neil Shapiro bool quoted = false; 313065a643dSPeter Wemm int parencount = 0; 314065a643dSPeter Wemm 315065a643dSPeter Wemm while (string != NULL && *string != '\0') 316065a643dSPeter Wemm { 317065a643dSPeter Wemm if (backslash) 318065a643dSPeter Wemm { 31940266059SGregory Neil Shapiro backslash = false; 320065a643dSPeter Wemm if (!quoted && character == '\\' && *string == '\\') 321065a643dSPeter Wemm break; 322065a643dSPeter Wemm string++; 323065a643dSPeter Wemm continue; 324065a643dSPeter Wemm } 325065a643dSPeter Wemm switch (*string) 326065a643dSPeter Wemm { 327065a643dSPeter Wemm case '\\': 32840266059SGregory Neil Shapiro backslash = true; 329065a643dSPeter Wemm break; 330065a643dSPeter Wemm 331065a643dSPeter Wemm case '(': 332065a643dSPeter Wemm if (!quoted) 333065a643dSPeter Wemm parencount++; 334065a643dSPeter Wemm break; 335065a643dSPeter Wemm 336065a643dSPeter Wemm case ')': 337065a643dSPeter Wemm if (--parencount < 0) 338065a643dSPeter Wemm parencount = 0; 339065a643dSPeter Wemm break; 340065a643dSPeter Wemm } 341065a643dSPeter Wemm 342065a643dSPeter Wemm /* Inside a comment, nothing matters */ 343065a643dSPeter Wemm if (parencount > 0) 344065a643dSPeter Wemm { 345065a643dSPeter Wemm string++; 346065a643dSPeter Wemm continue; 347065a643dSPeter Wemm } 348065a643dSPeter Wemm 349065a643dSPeter Wemm if (*string == '"') 350065a643dSPeter Wemm quoted = !quoted; 351065a643dSPeter Wemm else if (*string == character && !quoted) 352065a643dSPeter Wemm break; 353065a643dSPeter Wemm string++; 354065a643dSPeter Wemm } 355065a643dSPeter Wemm 356065a643dSPeter Wemm /* Return pointer to the character */ 357065a643dSPeter Wemm return string; 358065a643dSPeter Wemm } 35940266059SGregory Neil Shapiro 36040266059SGregory Neil Shapiro /* 36140266059SGregory Neil Shapiro ** CHECK_BODYTYPE -- check bodytype parameter 362c2aa98e2SPeter Wemm ** 36340266059SGregory Neil Shapiro ** Parameters: 36440266059SGregory Neil Shapiro ** bodytype -- bodytype parameter 36540266059SGregory Neil Shapiro ** 36640266059SGregory Neil Shapiro ** Returns: 36740266059SGregory Neil Shapiro ** BODYTYPE_* according to parameter 36840266059SGregory Neil Shapiro ** 36940266059SGregory Neil Shapiro */ 37040266059SGregory Neil Shapiro 37140266059SGregory Neil Shapiro int 37240266059SGregory Neil Shapiro check_bodytype(bodytype) 37340266059SGregory Neil Shapiro char *bodytype; 37440266059SGregory Neil Shapiro { 37540266059SGregory Neil Shapiro /* check body type for legality */ 37640266059SGregory Neil Shapiro if (bodytype == NULL) 37740266059SGregory Neil Shapiro return BODYTYPE_NONE; 37840266059SGregory Neil Shapiro if (sm_strcasecmp(bodytype, "7BIT") == 0) 37940266059SGregory Neil Shapiro return BODYTYPE_7BIT; 38040266059SGregory Neil Shapiro if (sm_strcasecmp(bodytype, "8BITMIME") == 0) 38140266059SGregory Neil Shapiro return BODYTYPE_8BITMIME; 38240266059SGregory Neil Shapiro return BODYTYPE_ILLEGAL; 38340266059SGregory Neil Shapiro } 38440266059SGregory Neil Shapiro 38540266059SGregory Neil Shapiro #if _FFR_BESTMX_BETTER_TRUNCATION || _FFR_DNSMAP_MULTI 38640266059SGregory Neil Shapiro /* 38740266059SGregory Neil Shapiro ** TRUNCATE_AT_DELIM -- truncate string at a delimiter and append "..." 38840266059SGregory Neil Shapiro ** 38940266059SGregory Neil Shapiro ** Parameters: 39040266059SGregory Neil Shapiro ** str -- string to truncate 39140266059SGregory Neil Shapiro ** len -- maximum length (including '\0') (0 for unlimited) 39240266059SGregory Neil Shapiro ** delim -- delimiter character 39340266059SGregory Neil Shapiro ** 39440266059SGregory Neil Shapiro ** Returns: 39540266059SGregory Neil Shapiro ** None. 39640266059SGregory Neil Shapiro */ 39740266059SGregory Neil Shapiro 39840266059SGregory Neil Shapiro void 39940266059SGregory Neil Shapiro truncate_at_delim(str, len, delim) 40040266059SGregory Neil Shapiro char *str; 40140266059SGregory Neil Shapiro size_t len; 40240266059SGregory Neil Shapiro int delim; 40340266059SGregory Neil Shapiro { 40440266059SGregory Neil Shapiro char *p; 40540266059SGregory Neil Shapiro 40640266059SGregory Neil Shapiro if (str == NULL || len == 0 || strlen(str) < len) 40740266059SGregory Neil Shapiro return; 40840266059SGregory Neil Shapiro 40940266059SGregory Neil Shapiro *(str + len - 1) = '\0'; 41040266059SGregory Neil Shapiro while ((p = strrchr(str, delim)) != NULL) 41140266059SGregory Neil Shapiro { 41240266059SGregory Neil Shapiro *p = '\0'; 41340266059SGregory Neil Shapiro if (p - str + 4 < len) 41440266059SGregory Neil Shapiro { 415a7ec597cSGregory Neil Shapiro *p++ = (char) delim; 41640266059SGregory Neil Shapiro *p = '\0'; 41740266059SGregory Neil Shapiro (void) sm_strlcat(str, "...", len); 41840266059SGregory Neil Shapiro return; 41940266059SGregory Neil Shapiro } 42040266059SGregory Neil Shapiro } 42140266059SGregory Neil Shapiro 42240266059SGregory Neil Shapiro /* Couldn't find a place to append "..." */ 42340266059SGregory Neil Shapiro if (len > 3) 42440266059SGregory Neil Shapiro (void) sm_strlcpy(str, "...", len); 42540266059SGregory Neil Shapiro else 42640266059SGregory Neil Shapiro str[0] = '\0'; 42740266059SGregory Neil Shapiro } 42840266059SGregory Neil Shapiro #endif /* _FFR_BESTMX_BETTER_TRUNCATION || _FFR_DNSMAP_MULTI */ 42940266059SGregory Neil Shapiro /* 43040266059SGregory Neil Shapiro ** XALLOC -- Allocate memory, raise an exception on error 431c2aa98e2SPeter Wemm ** 432c2aa98e2SPeter Wemm ** Parameters: 433c2aa98e2SPeter Wemm ** sz -- size of area to allocate. 434c2aa98e2SPeter Wemm ** 435c2aa98e2SPeter Wemm ** Returns: 436c2aa98e2SPeter Wemm ** pointer to data region. 437c2aa98e2SPeter Wemm ** 43840266059SGregory Neil Shapiro ** Exceptions: 43940266059SGregory Neil Shapiro ** SmHeapOutOfMemory (F:sm.heap) -- cannot allocate memory 44040266059SGregory Neil Shapiro ** 441c2aa98e2SPeter Wemm ** Side Effects: 442c2aa98e2SPeter Wemm ** Memory is allocated. 443c2aa98e2SPeter Wemm */ 444c2aa98e2SPeter Wemm 445c2aa98e2SPeter Wemm char * 44640266059SGregory Neil Shapiro #if SM_HEAP_CHECK 44740266059SGregory Neil Shapiro xalloc_tagged(sz, file, line) 44840266059SGregory Neil Shapiro register int sz; 44940266059SGregory Neil Shapiro char *file; 45040266059SGregory Neil Shapiro int line; 45140266059SGregory Neil Shapiro #else /* SM_HEAP_CHECK */ 452c2aa98e2SPeter Wemm xalloc(sz) 453c2aa98e2SPeter Wemm register int sz; 45440266059SGregory Neil Shapiro #endif /* SM_HEAP_CHECK */ 455c2aa98e2SPeter Wemm { 456c2aa98e2SPeter Wemm register char *p; 457c2aa98e2SPeter Wemm 458c2aa98e2SPeter Wemm /* some systems can't handle size zero mallocs */ 459c2aa98e2SPeter Wemm if (sz <= 0) 460c2aa98e2SPeter Wemm sz = 1; 461c2aa98e2SPeter Wemm 46240266059SGregory Neil Shapiro /* scaffolding for testing error handling code */ 46340266059SGregory Neil Shapiro sm_xtrap_raise_x(&SmHeapOutOfMemory); 46440266059SGregory Neil Shapiro 46540266059SGregory Neil Shapiro p = sm_malloc_tagged((unsigned) sz, file, line, sm_heap_group()); 466c2aa98e2SPeter Wemm if (p == NULL) 467c2aa98e2SPeter Wemm { 46840266059SGregory Neil Shapiro sm_exc_raise_x(&SmHeapOutOfMemory); 469c2aa98e2SPeter Wemm } 47006f25ae9SGregory Neil Shapiro return p; 471c2aa98e2SPeter Wemm } 47240266059SGregory Neil Shapiro /* 473c2aa98e2SPeter Wemm ** COPYPLIST -- copy list of pointers. 474c2aa98e2SPeter Wemm ** 47540266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for lists of 476c2aa98e2SPeter Wemm ** pointers. 477c2aa98e2SPeter Wemm ** 478c2aa98e2SPeter Wemm ** Parameters: 479c2aa98e2SPeter Wemm ** list -- list of pointers to copy. 480c2aa98e2SPeter Wemm ** Must be NULL terminated. 48140266059SGregory Neil Shapiro ** copycont -- if true, copy the contents of the vector 482c2aa98e2SPeter Wemm ** (which must be a string) also. 48340266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage, 48440266059SGregory Neil Shapiro ** or NULL 485c2aa98e2SPeter Wemm ** 486c2aa98e2SPeter Wemm ** Returns: 487c2aa98e2SPeter Wemm ** a copy of 'list'. 488c2aa98e2SPeter Wemm */ 489c2aa98e2SPeter Wemm 490c2aa98e2SPeter Wemm char ** 49140266059SGregory Neil Shapiro copyplist(list, copycont, rpool) 492c2aa98e2SPeter Wemm char **list; 493c2aa98e2SPeter Wemm bool copycont; 49440266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 495c2aa98e2SPeter Wemm { 496c2aa98e2SPeter Wemm register char **vp; 497c2aa98e2SPeter Wemm register char **newvp; 498c2aa98e2SPeter Wemm 499c2aa98e2SPeter Wemm for (vp = list; *vp != NULL; vp++) 500c2aa98e2SPeter Wemm continue; 501c2aa98e2SPeter Wemm 502c2aa98e2SPeter Wemm vp++; 503c2aa98e2SPeter Wemm 50440266059SGregory Neil Shapiro newvp = (char **) sm_rpool_malloc_x(rpool, (vp - list) * sizeof *vp); 50506f25ae9SGregory Neil Shapiro memmove((char *) newvp, (char *) list, (int) (vp - list) * sizeof *vp); 506c2aa98e2SPeter Wemm 507c2aa98e2SPeter Wemm if (copycont) 508c2aa98e2SPeter Wemm { 509c2aa98e2SPeter Wemm for (vp = newvp; *vp != NULL; vp++) 51040266059SGregory Neil Shapiro *vp = sm_rpool_strdup_x(rpool, *vp); 511c2aa98e2SPeter Wemm } 512c2aa98e2SPeter Wemm 51306f25ae9SGregory Neil Shapiro return newvp; 514c2aa98e2SPeter Wemm } 51540266059SGregory Neil Shapiro /* 516c2aa98e2SPeter Wemm ** COPYQUEUE -- copy address queue. 517c2aa98e2SPeter Wemm ** 51840266059SGregory Neil Shapiro ** This routine is the equivalent of strdup for address queues; 51906f25ae9SGregory Neil Shapiro ** addresses marked as QS_IS_DEAD() aren't copied 520c2aa98e2SPeter Wemm ** 521c2aa98e2SPeter Wemm ** Parameters: 522c2aa98e2SPeter Wemm ** addr -- list of address structures to copy. 52340266059SGregory Neil Shapiro ** rpool -- resource pool from which to allocate storage 524c2aa98e2SPeter Wemm ** 525c2aa98e2SPeter Wemm ** Returns: 526c2aa98e2SPeter Wemm ** a copy of 'addr'. 527c2aa98e2SPeter Wemm */ 528c2aa98e2SPeter Wemm 529c2aa98e2SPeter Wemm ADDRESS * 53040266059SGregory Neil Shapiro copyqueue(addr, rpool) 531c2aa98e2SPeter Wemm ADDRESS *addr; 53240266059SGregory Neil Shapiro SM_RPOOL_T *rpool; 533c2aa98e2SPeter Wemm { 534c2aa98e2SPeter Wemm register ADDRESS *newaddr; 535c2aa98e2SPeter Wemm ADDRESS *ret; 536c2aa98e2SPeter Wemm register ADDRESS **tail = &ret; 537c2aa98e2SPeter Wemm 538c2aa98e2SPeter Wemm while (addr != NULL) 539c2aa98e2SPeter Wemm { 54006f25ae9SGregory Neil Shapiro if (!QS_IS_DEAD(addr->q_state)) 541c2aa98e2SPeter Wemm { 54240266059SGregory Neil Shapiro newaddr = (ADDRESS *) sm_rpool_malloc_x(rpool, 54340266059SGregory Neil Shapiro sizeof *newaddr); 544c2aa98e2SPeter Wemm STRUCTCOPY(*addr, *newaddr); 545c2aa98e2SPeter Wemm *tail = newaddr; 546c2aa98e2SPeter Wemm tail = &newaddr->q_next; 547c2aa98e2SPeter Wemm } 548c2aa98e2SPeter Wemm addr = addr->q_next; 549c2aa98e2SPeter Wemm } 550c2aa98e2SPeter Wemm *tail = NULL; 551c2aa98e2SPeter Wemm 552c2aa98e2SPeter Wemm return ret; 553c2aa98e2SPeter Wemm } 55440266059SGregory Neil Shapiro /* 55506f25ae9SGregory Neil Shapiro ** LOG_SENDMAIL_PID -- record sendmail pid and command line. 55606f25ae9SGregory Neil Shapiro ** 55706f25ae9SGregory Neil Shapiro ** Parameters: 55806f25ae9SGregory Neil Shapiro ** e -- the current envelope. 55906f25ae9SGregory Neil Shapiro ** 56006f25ae9SGregory Neil Shapiro ** Returns: 56106f25ae9SGregory Neil Shapiro ** none. 56206f25ae9SGregory Neil Shapiro ** 56306f25ae9SGregory Neil Shapiro ** Side Effects: 56440266059SGregory Neil Shapiro ** writes pidfile, logs command line. 565e92d3f3fSGregory Neil Shapiro ** keeps file open and locked to prevent overwrite of active file 56606f25ae9SGregory Neil Shapiro */ 56706f25ae9SGregory Neil Shapiro 568e92d3f3fSGregory Neil Shapiro static SM_FILE_T *Pidf = NULL; 569e92d3f3fSGregory Neil Shapiro 57006f25ae9SGregory Neil Shapiro void 57106f25ae9SGregory Neil Shapiro log_sendmail_pid(e) 57206f25ae9SGregory Neil Shapiro ENVELOPE *e; 57306f25ae9SGregory Neil Shapiro { 57406f25ae9SGregory Neil Shapiro long sff; 57594c01205SGregory Neil Shapiro char pidpath[MAXPATHLEN]; 57640266059SGregory Neil Shapiro extern char *CommandLineArgs; 57706f25ae9SGregory Neil Shapiro 57806f25ae9SGregory Neil Shapiro /* write the pid to the log file for posterity */ 579e92d3f3fSGregory Neil Shapiro sff = SFF_NOLINK|SFF_ROOTOK|SFF_REGONLY|SFF_CREAT|SFF_NBLOCK; 58006f25ae9SGregory Neil Shapiro if (TrustedUid != 0 && RealUid == TrustedUid) 58106f25ae9SGregory Neil Shapiro sff |= SFF_OPENASROOT; 58206f25ae9SGregory Neil Shapiro expand(PidFile, pidpath, sizeof pidpath, e); 583e92d3f3fSGregory Neil Shapiro Pidf = safefopen(pidpath, O_WRONLY|O_TRUNC, FileMode, sff); 584e92d3f3fSGregory Neil Shapiro if (Pidf == NULL) 58506f25ae9SGregory Neil Shapiro { 586e92d3f3fSGregory Neil Shapiro if (errno == EWOULDBLOCK) 587e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID, 588e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: file in use by another process", 589e92d3f3fSGregory Neil Shapiro pidpath); 590e92d3f3fSGregory Neil Shapiro else 591e92d3f3fSGregory Neil Shapiro sm_syslog(LOG_ERR, NOQID, 592e92d3f3fSGregory Neil Shapiro "unable to write pid to %s: %s", 59340266059SGregory Neil Shapiro pidpath, sm_errstring(errno)); 59406f25ae9SGregory Neil Shapiro } 59506f25ae9SGregory Neil Shapiro else 59606f25ae9SGregory Neil Shapiro { 597e92d3f3fSGregory Neil Shapiro PidFilePid = getpid(); 598193538b7SGregory Neil Shapiro 59906f25ae9SGregory Neil Shapiro /* write the process id on line 1 */ 600e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%ld\n", 601e92d3f3fSGregory Neil Shapiro (long) PidFilePid); 60206f25ae9SGregory Neil Shapiro 60306f25ae9SGregory Neil Shapiro /* line 2 contains all command line flags */ 604e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(Pidf, SM_TIME_DEFAULT, "%s\n", 60540266059SGregory Neil Shapiro CommandLineArgs); 60606f25ae9SGregory Neil Shapiro 607e92d3f3fSGregory Neil Shapiro /* flush */ 608e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(Pidf, SM_TIME_DEFAULT); 609e92d3f3fSGregory Neil Shapiro 610e92d3f3fSGregory Neil Shapiro /* 611e92d3f3fSGregory Neil Shapiro ** Leave pid file open until process ends 612e92d3f3fSGregory Neil Shapiro ** so it's not overwritten by another 613e92d3f3fSGregory Neil Shapiro ** process. 614e92d3f3fSGregory Neil Shapiro */ 61506f25ae9SGregory Neil Shapiro } 61640266059SGregory Neil Shapiro if (LogLevel > 9) 61740266059SGregory Neil Shapiro sm_syslog(LOG_INFO, NOQID, "started as: %s", CommandLineArgs); 61806f25ae9SGregory Neil Shapiro } 619e92d3f3fSGregory Neil Shapiro 620e92d3f3fSGregory Neil Shapiro /* 621e92d3f3fSGregory Neil Shapiro ** CLOSE_SENDMAIL_PID -- close sendmail pid file 622e92d3f3fSGregory Neil Shapiro ** 623e92d3f3fSGregory Neil Shapiro ** Parameters: 624e92d3f3fSGregory Neil Shapiro ** none. 625e92d3f3fSGregory Neil Shapiro ** 626e92d3f3fSGregory Neil Shapiro ** Returns: 627e92d3f3fSGregory Neil Shapiro ** none. 628e92d3f3fSGregory Neil Shapiro */ 629e92d3f3fSGregory Neil Shapiro 630e92d3f3fSGregory Neil Shapiro void 631e92d3f3fSGregory Neil Shapiro close_sendmail_pid() 632e92d3f3fSGregory Neil Shapiro { 633e92d3f3fSGregory Neil Shapiro if (Pidf == NULL) 634e92d3f3fSGregory Neil Shapiro return; 635e92d3f3fSGregory Neil Shapiro 636e92d3f3fSGregory Neil Shapiro (void) sm_io_close(Pidf, SM_TIME_DEFAULT); 637e92d3f3fSGregory Neil Shapiro Pidf = NULL; 638e92d3f3fSGregory Neil Shapiro } 639e92d3f3fSGregory Neil Shapiro 64040266059SGregory Neil Shapiro /* 64106f25ae9SGregory Neil Shapiro ** SET_DELIVERY_MODE -- set and record the delivery mode 64206f25ae9SGregory Neil Shapiro ** 64306f25ae9SGregory Neil Shapiro ** Parameters: 64406f25ae9SGregory Neil Shapiro ** mode -- delivery mode 64506f25ae9SGregory Neil Shapiro ** e -- the current envelope. 64606f25ae9SGregory Neil Shapiro ** 64706f25ae9SGregory Neil Shapiro ** Returns: 64806f25ae9SGregory Neil Shapiro ** none. 64906f25ae9SGregory Neil Shapiro ** 65006f25ae9SGregory Neil Shapiro ** Side Effects: 65140266059SGregory Neil Shapiro ** sets {deliveryMode} macro 65206f25ae9SGregory Neil Shapiro */ 65306f25ae9SGregory Neil Shapiro 65406f25ae9SGregory Neil Shapiro void 65506f25ae9SGregory Neil Shapiro set_delivery_mode(mode, e) 65606f25ae9SGregory Neil Shapiro int mode; 65706f25ae9SGregory Neil Shapiro ENVELOPE *e; 65806f25ae9SGregory Neil Shapiro { 65906f25ae9SGregory Neil Shapiro char buf[2]; 66006f25ae9SGregory Neil Shapiro 66106f25ae9SGregory Neil Shapiro e->e_sendmode = (char) mode; 66206f25ae9SGregory Neil Shapiro buf[0] = (char) mode; 66306f25ae9SGregory Neil Shapiro buf[1] = '\0'; 66440266059SGregory Neil Shapiro macdefine(&e->e_macro, A_TEMP, macid("{deliveryMode}"), buf); 66506f25ae9SGregory Neil Shapiro } 66640266059SGregory Neil Shapiro /* 66740266059SGregory Neil Shapiro ** SET_OP_MODE -- set and record the op mode 66840266059SGregory Neil Shapiro ** 66940266059SGregory Neil Shapiro ** Parameters: 67040266059SGregory Neil Shapiro ** mode -- op mode 67140266059SGregory Neil Shapiro ** e -- the current envelope. 67240266059SGregory Neil Shapiro ** 67340266059SGregory Neil Shapiro ** Returns: 67440266059SGregory Neil Shapiro ** none. 67540266059SGregory Neil Shapiro ** 67640266059SGregory Neil Shapiro ** Side Effects: 67740266059SGregory Neil Shapiro ** sets {opMode} macro 67840266059SGregory Neil Shapiro */ 67940266059SGregory Neil Shapiro 68040266059SGregory Neil Shapiro void 68140266059SGregory Neil Shapiro set_op_mode(mode) 68240266059SGregory Neil Shapiro int mode; 68340266059SGregory Neil Shapiro { 68440266059SGregory Neil Shapiro char buf[2]; 68540266059SGregory Neil Shapiro extern ENVELOPE BlankEnvelope; 68640266059SGregory Neil Shapiro 68740266059SGregory Neil Shapiro OpMode = (char) mode; 68840266059SGregory Neil Shapiro buf[0] = (char) mode; 68940266059SGregory Neil Shapiro buf[1] = '\0'; 69040266059SGregory Neil Shapiro macdefine(&BlankEnvelope.e_macro, A_TEMP, MID_OPMODE, buf); 69140266059SGregory Neil Shapiro } 69240266059SGregory Neil Shapiro /* 693c2aa98e2SPeter Wemm ** PRINTAV -- print argument vector. 694c2aa98e2SPeter Wemm ** 695c2aa98e2SPeter Wemm ** Parameters: 696e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer. 697c2aa98e2SPeter Wemm ** av -- argument vector. 698c2aa98e2SPeter Wemm ** 699c2aa98e2SPeter Wemm ** Returns: 700c2aa98e2SPeter Wemm ** none. 701c2aa98e2SPeter Wemm ** 702c2aa98e2SPeter Wemm ** Side Effects: 703c2aa98e2SPeter Wemm ** prints av. 704c2aa98e2SPeter Wemm */ 705c2aa98e2SPeter Wemm 706c2aa98e2SPeter Wemm void 707e92d3f3fSGregory Neil Shapiro printav(fp, av) 708e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp; 709c2aa98e2SPeter Wemm register char **av; 710c2aa98e2SPeter Wemm { 711c2aa98e2SPeter Wemm while (*av != NULL) 712c2aa98e2SPeter Wemm { 713c2aa98e2SPeter Wemm if (tTd(0, 44)) 71440266059SGregory Neil Shapiro sm_dprintf("\n\t%08lx=", (unsigned long) *av); 715c2aa98e2SPeter Wemm else 716e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, ' '); 717e92d3f3fSGregory Neil Shapiro xputs(fp, *av++); 718c2aa98e2SPeter Wemm } 719e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\n'); 720c2aa98e2SPeter Wemm } 72140266059SGregory Neil Shapiro /* 722c2aa98e2SPeter Wemm ** XPUTS -- put string doing control escapes. 723c2aa98e2SPeter Wemm ** 724c2aa98e2SPeter Wemm ** Parameters: 725e92d3f3fSGregory Neil Shapiro ** fp -- output file pointer. 726c2aa98e2SPeter Wemm ** s -- string to put. 727c2aa98e2SPeter Wemm ** 728c2aa98e2SPeter Wemm ** Returns: 729c2aa98e2SPeter Wemm ** none. 730c2aa98e2SPeter Wemm ** 731c2aa98e2SPeter Wemm ** Side Effects: 732c2aa98e2SPeter Wemm ** output to stdout 733c2aa98e2SPeter Wemm */ 734c2aa98e2SPeter Wemm 735c2aa98e2SPeter Wemm void 736e92d3f3fSGregory Neil Shapiro xputs(fp, s) 737e92d3f3fSGregory Neil Shapiro SM_FILE_T *fp; 738c2aa98e2SPeter Wemm register const char *s; 739c2aa98e2SPeter Wemm { 740c2aa98e2SPeter Wemm register int c; 741c2aa98e2SPeter Wemm register struct metamac *mp; 74240266059SGregory Neil Shapiro bool shiftout = false; 743c2aa98e2SPeter Wemm extern struct metamac MetaMacros[]; 74440266059SGregory Neil Shapiro static SM_DEBUG_T DebugANSI = SM_DEBUG_INITIALIZER("ANSI", 74540266059SGregory Neil Shapiro "@(#)$Debug: ANSI - enable reverse video in debug output $"); 74640266059SGregory Neil Shapiro 74740266059SGregory Neil Shapiro /* 74840266059SGregory Neil Shapiro ** TermEscape is set here, rather than in main(), 74940266059SGregory Neil Shapiro ** because ANSI mode can be turned on or off at any time 75040266059SGregory Neil Shapiro ** if we are in -bt rule testing mode. 75140266059SGregory Neil Shapiro */ 75240266059SGregory Neil Shapiro 75340266059SGregory Neil Shapiro if (sm_debug_unknown(&DebugANSI)) 75440266059SGregory Neil Shapiro { 75540266059SGregory Neil Shapiro if (sm_debug_active(&DebugANSI, 1)) 75640266059SGregory Neil Shapiro { 75740266059SGregory Neil Shapiro TermEscape.te_rv_on = "\033[7m"; 75840266059SGregory Neil Shapiro TermEscape.te_rv_off = "\033[0m"; 75940266059SGregory Neil Shapiro } 76040266059SGregory Neil Shapiro else 76140266059SGregory Neil Shapiro { 76240266059SGregory Neil Shapiro TermEscape.te_rv_on = ""; 76340266059SGregory Neil Shapiro TermEscape.te_rv_off = ""; 76440266059SGregory Neil Shapiro } 76540266059SGregory Neil Shapiro } 766c2aa98e2SPeter Wemm 767c2aa98e2SPeter Wemm if (s == NULL) 768c2aa98e2SPeter Wemm { 769e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s<null>%s", 77040266059SGregory Neil Shapiro TermEscape.te_rv_on, TermEscape.te_rv_off); 771c2aa98e2SPeter Wemm return; 772c2aa98e2SPeter Wemm } 773c2aa98e2SPeter Wemm while ((c = (*s++ & 0377)) != '\0') 774c2aa98e2SPeter Wemm { 775c2aa98e2SPeter Wemm if (shiftout) 776c2aa98e2SPeter Wemm { 777e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 77840266059SGregory Neil Shapiro TermEscape.te_rv_off); 77940266059SGregory Neil Shapiro shiftout = false; 780c2aa98e2SPeter Wemm } 781c2aa98e2SPeter Wemm if (!isascii(c)) 782c2aa98e2SPeter Wemm { 783c2aa98e2SPeter Wemm if (c == MATCHREPL) 784c2aa98e2SPeter Wemm { 785e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, 78640266059SGregory Neil Shapiro "%s$", 78740266059SGregory Neil Shapiro TermEscape.te_rv_on); 78840266059SGregory Neil Shapiro shiftout = true; 789c2aa98e2SPeter Wemm if (*s == '\0') 790c2aa98e2SPeter Wemm continue; 791c2aa98e2SPeter Wemm c = *s++ & 0377; 792c2aa98e2SPeter Wemm goto printchar; 793c2aa98e2SPeter Wemm } 794c2aa98e2SPeter Wemm if (c == MACROEXPAND || c == MACRODEXPAND) 795c2aa98e2SPeter Wemm { 796e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, 79740266059SGregory Neil Shapiro "%s$", 79840266059SGregory Neil Shapiro TermEscape.te_rv_on); 799c2aa98e2SPeter Wemm if (c == MACRODEXPAND) 800e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, 80140266059SGregory Neil Shapiro SM_TIME_DEFAULT, '&'); 80240266059SGregory Neil Shapiro shiftout = true; 803c2aa98e2SPeter Wemm if (*s == '\0') 804c2aa98e2SPeter Wemm continue; 805c2aa98e2SPeter Wemm if (strchr("=~&?", *s) != NULL) 806e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, 80740266059SGregory Neil Shapiro SM_TIME_DEFAULT, 80840266059SGregory Neil Shapiro *s++); 809c2aa98e2SPeter Wemm if (bitset(0200, *s)) 810e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 81140266059SGregory Neil Shapiro SM_TIME_DEFAULT, 81240266059SGregory Neil Shapiro "{%s}", 81340266059SGregory Neil Shapiro macname(bitidx(*s++))); 814c2aa98e2SPeter Wemm else 815e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 81640266059SGregory Neil Shapiro SM_TIME_DEFAULT, 81740266059SGregory Neil Shapiro "%c", 81840266059SGregory Neil Shapiro *s++); 819c2aa98e2SPeter Wemm continue; 820c2aa98e2SPeter Wemm } 821c2aa98e2SPeter Wemm for (mp = MetaMacros; mp->metaname != '\0'; mp++) 822c2aa98e2SPeter Wemm { 82340266059SGregory Neil Shapiro if (bitidx(mp->metaval) == c) 824c2aa98e2SPeter Wemm { 825e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 82640266059SGregory Neil Shapiro SM_TIME_DEFAULT, 82740266059SGregory Neil Shapiro "%s$%c", 828c2aa98e2SPeter Wemm TermEscape.te_rv_on, 829c2aa98e2SPeter Wemm mp->metaname); 83040266059SGregory Neil Shapiro shiftout = true; 831c2aa98e2SPeter Wemm break; 832c2aa98e2SPeter Wemm } 833c2aa98e2SPeter Wemm } 834c2aa98e2SPeter Wemm if (c == MATCHCLASS || c == MATCHNCLASS) 835c2aa98e2SPeter Wemm { 836c2aa98e2SPeter Wemm if (bitset(0200, *s)) 837e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 83840266059SGregory Neil Shapiro SM_TIME_DEFAULT, 83940266059SGregory Neil Shapiro "{%s}", 84040266059SGregory Neil Shapiro macname(bitidx(*s++))); 841c2aa98e2SPeter Wemm else if (*s != '\0') 842e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, 84340266059SGregory Neil Shapiro SM_TIME_DEFAULT, 84440266059SGregory Neil Shapiro "%c", 84540266059SGregory Neil Shapiro *s++); 846c2aa98e2SPeter Wemm } 847c2aa98e2SPeter Wemm if (mp->metaname != '\0') 848c2aa98e2SPeter Wemm continue; 849c2aa98e2SPeter Wemm 850c2aa98e2SPeter Wemm /* unrecognized meta character */ 851e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%sM-", 85240266059SGregory Neil Shapiro TermEscape.te_rv_on); 85340266059SGregory Neil Shapiro shiftout = true; 854c2aa98e2SPeter Wemm c &= 0177; 855c2aa98e2SPeter Wemm } 856c2aa98e2SPeter Wemm printchar: 857c2aa98e2SPeter Wemm if (isprint(c)) 858c2aa98e2SPeter Wemm { 859e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c); 860c2aa98e2SPeter Wemm continue; 861c2aa98e2SPeter Wemm } 862c2aa98e2SPeter Wemm 863c2aa98e2SPeter Wemm /* wasn't a meta-macro -- find another way to print it */ 864c2aa98e2SPeter Wemm switch (c) 865c2aa98e2SPeter Wemm { 866c2aa98e2SPeter Wemm case '\n': 867c2aa98e2SPeter Wemm c = 'n'; 868c2aa98e2SPeter Wemm break; 869c2aa98e2SPeter Wemm 870c2aa98e2SPeter Wemm case '\r': 871c2aa98e2SPeter Wemm c = 'r'; 872c2aa98e2SPeter Wemm break; 873c2aa98e2SPeter Wemm 874c2aa98e2SPeter Wemm case '\t': 875c2aa98e2SPeter Wemm c = 't'; 876c2aa98e2SPeter Wemm break; 877c2aa98e2SPeter Wemm } 878c2aa98e2SPeter Wemm if (!shiftout) 879c2aa98e2SPeter Wemm { 880e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 88140266059SGregory Neil Shapiro TermEscape.te_rv_on); 88240266059SGregory Neil Shapiro shiftout = true; 883c2aa98e2SPeter Wemm } 884c2aa98e2SPeter Wemm if (isprint(c)) 885c2aa98e2SPeter Wemm { 886e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '\\'); 887e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c); 888c2aa98e2SPeter Wemm } 889c2aa98e2SPeter Wemm else 890c2aa98e2SPeter Wemm { 891e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, '^'); 892e92d3f3fSGregory Neil Shapiro (void) sm_io_putc(fp, SM_TIME_DEFAULT, c ^ 0100); 893c2aa98e2SPeter Wemm } 894c2aa98e2SPeter Wemm } 895c2aa98e2SPeter Wemm if (shiftout) 896e92d3f3fSGregory Neil Shapiro (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s", 89740266059SGregory Neil Shapiro TermEscape.te_rv_off); 898e92d3f3fSGregory Neil Shapiro (void) sm_io_flush(fp, SM_TIME_DEFAULT); 899c2aa98e2SPeter Wemm } 90040266059SGregory Neil Shapiro /* 901c2aa98e2SPeter Wemm ** MAKELOWER -- Translate a line into lower case 902c2aa98e2SPeter Wemm ** 903c2aa98e2SPeter Wemm ** Parameters: 904c2aa98e2SPeter Wemm ** p -- the string to translate. If NULL, return is 905c2aa98e2SPeter Wemm ** immediate. 906c2aa98e2SPeter Wemm ** 907c2aa98e2SPeter Wemm ** Returns: 908c2aa98e2SPeter Wemm ** none. 909c2aa98e2SPeter Wemm ** 910c2aa98e2SPeter Wemm ** Side Effects: 911c2aa98e2SPeter Wemm ** String pointed to by p is translated to lower case. 912c2aa98e2SPeter Wemm */ 913c2aa98e2SPeter Wemm 914c2aa98e2SPeter Wemm void 915c2aa98e2SPeter Wemm makelower(p) 916c2aa98e2SPeter Wemm register char *p; 917c2aa98e2SPeter Wemm { 918c2aa98e2SPeter Wemm register char c; 919c2aa98e2SPeter Wemm 920c2aa98e2SPeter Wemm if (p == NULL) 921c2aa98e2SPeter Wemm return; 922c2aa98e2SPeter Wemm for (; (c = *p) != '\0'; p++) 923c2aa98e2SPeter Wemm if (isascii(c) && isupper(c)) 924c2aa98e2SPeter Wemm *p = tolower(c); 925c2aa98e2SPeter Wemm } 92640266059SGregory Neil Shapiro /* 927c2aa98e2SPeter Wemm ** FIXCRLF -- fix <CR><LF> in line. 928c2aa98e2SPeter Wemm ** 929c2aa98e2SPeter Wemm ** Looks for the <CR><LF> combination and turns it into the 930c2aa98e2SPeter Wemm ** UNIX canonical <NL> character. It only takes one line, 931c2aa98e2SPeter Wemm ** i.e., it is assumed that the first <NL> found is the end 932c2aa98e2SPeter Wemm ** of the line. 933c2aa98e2SPeter Wemm ** 934c2aa98e2SPeter Wemm ** Parameters: 935c2aa98e2SPeter Wemm ** line -- the line to fix. 936c2aa98e2SPeter Wemm ** stripnl -- if true, strip the newline also. 937c2aa98e2SPeter Wemm ** 938c2aa98e2SPeter Wemm ** Returns: 939c2aa98e2SPeter Wemm ** none. 940c2aa98e2SPeter Wemm ** 941c2aa98e2SPeter Wemm ** Side Effects: 942c2aa98e2SPeter Wemm ** line is changed in place. 943c2aa98e2SPeter Wemm */ 944c2aa98e2SPeter Wemm 945c2aa98e2SPeter Wemm void 946c2aa98e2SPeter Wemm fixcrlf(line, stripnl) 947c2aa98e2SPeter Wemm char *line; 948c2aa98e2SPeter Wemm bool stripnl; 949c2aa98e2SPeter Wemm { 950c2aa98e2SPeter Wemm register char *p; 951c2aa98e2SPeter Wemm 952c2aa98e2SPeter Wemm p = strchr(line, '\n'); 953c2aa98e2SPeter Wemm if (p == NULL) 954c2aa98e2SPeter Wemm return; 955c2aa98e2SPeter Wemm if (p > line && p[-1] == '\r') 956c2aa98e2SPeter Wemm p--; 957c2aa98e2SPeter Wemm if (!stripnl) 958c2aa98e2SPeter Wemm *p++ = '\n'; 959c2aa98e2SPeter Wemm *p = '\0'; 960c2aa98e2SPeter Wemm } 96140266059SGregory Neil Shapiro /* 962c2aa98e2SPeter Wemm ** PUTLINE -- put a line like fputs obeying SMTP conventions 963c2aa98e2SPeter Wemm ** 964c2aa98e2SPeter Wemm ** This routine always guarantees outputing a newline (or CRLF, 965c2aa98e2SPeter Wemm ** as appropriate) at the end of the string. 966c2aa98e2SPeter Wemm ** 967c2aa98e2SPeter Wemm ** Parameters: 968c2aa98e2SPeter Wemm ** l -- line to put. 969c2aa98e2SPeter Wemm ** mci -- the mailer connection information. 970c2aa98e2SPeter Wemm ** 971c2aa98e2SPeter Wemm ** Returns: 972c2aa98e2SPeter Wemm ** none 973c2aa98e2SPeter Wemm ** 974c2aa98e2SPeter Wemm ** Side Effects: 97540266059SGregory Neil Shapiro ** output of l to mci->mci_out. 976c2aa98e2SPeter Wemm */ 977c2aa98e2SPeter Wemm 978c2aa98e2SPeter Wemm void 979c2aa98e2SPeter Wemm putline(l, mci) 980c2aa98e2SPeter Wemm register char *l; 981c2aa98e2SPeter Wemm register MCI *mci; 982c2aa98e2SPeter Wemm { 983c2aa98e2SPeter Wemm putxline(l, strlen(l), mci, PXLF_MAPFROM); 984c2aa98e2SPeter Wemm } 98540266059SGregory Neil Shapiro /* 986c2aa98e2SPeter Wemm ** PUTXLINE -- putline with flags bits. 987c2aa98e2SPeter Wemm ** 988c2aa98e2SPeter Wemm ** This routine always guarantees outputing a newline (or CRLF, 989c2aa98e2SPeter Wemm ** as appropriate) at the end of the string. 990c2aa98e2SPeter Wemm ** 991c2aa98e2SPeter Wemm ** Parameters: 992c2aa98e2SPeter Wemm ** l -- line to put. 993c2aa98e2SPeter Wemm ** len -- the length of the line. 994c2aa98e2SPeter Wemm ** mci -- the mailer connection information. 995c2aa98e2SPeter Wemm ** pxflags -- flag bits: 996c2aa98e2SPeter Wemm ** PXLF_MAPFROM -- map From_ to >From_. 997c2aa98e2SPeter Wemm ** PXLF_STRIP8BIT -- strip 8th bit. 998c2aa98e2SPeter Wemm ** PXLF_HEADER -- map bare newline in header to newline space. 999605302a5SGregory Neil Shapiro ** PXLF_NOADDEOL -- don't add an EOL if one wasn't present. 1000c2aa98e2SPeter Wemm ** 1001c2aa98e2SPeter Wemm ** Returns: 1002c2aa98e2SPeter Wemm ** none 1003c2aa98e2SPeter Wemm ** 1004c2aa98e2SPeter Wemm ** Side Effects: 100540266059SGregory Neil Shapiro ** output of l to mci->mci_out. 1006c2aa98e2SPeter Wemm */ 1007c2aa98e2SPeter Wemm 1008c2aa98e2SPeter Wemm void 1009c2aa98e2SPeter Wemm putxline(l, len, mci, pxflags) 1010c2aa98e2SPeter Wemm register char *l; 1011c2aa98e2SPeter Wemm size_t len; 1012c2aa98e2SPeter Wemm register MCI *mci; 1013c2aa98e2SPeter Wemm int pxflags; 1014c2aa98e2SPeter Wemm { 101540266059SGregory Neil Shapiro bool dead = false; 1016c2aa98e2SPeter Wemm register char *p, *end; 1017c2aa98e2SPeter Wemm int slop = 0; 1018c2aa98e2SPeter Wemm 1019c2aa98e2SPeter Wemm /* strip out 0200 bits -- these can look like TELNET protocol */ 1020c2aa98e2SPeter Wemm if (bitset(MCIF_7BIT, mci->mci_flags) || 1021c2aa98e2SPeter Wemm bitset(PXLF_STRIP8BIT, pxflags)) 1022c2aa98e2SPeter Wemm { 1023c2aa98e2SPeter Wemm register char svchar; 1024c2aa98e2SPeter Wemm 1025c2aa98e2SPeter Wemm for (p = l; (svchar = *p) != '\0'; ++p) 1026c2aa98e2SPeter Wemm if (bitset(0200, svchar)) 1027c2aa98e2SPeter Wemm *p = svchar &~ 0200; 1028c2aa98e2SPeter Wemm } 1029c2aa98e2SPeter Wemm 1030c2aa98e2SPeter Wemm end = l + len; 1031c2aa98e2SPeter Wemm do 1032c2aa98e2SPeter Wemm { 1033605302a5SGregory Neil Shapiro bool noeol = false; 1034605302a5SGregory Neil Shapiro 1035c2aa98e2SPeter Wemm /* find the end of the line */ 1036c2aa98e2SPeter Wemm p = memchr(l, '\n', end - l); 1037c2aa98e2SPeter Wemm if (p == NULL) 1038605302a5SGregory Neil Shapiro { 1039c2aa98e2SPeter Wemm p = end; 1040605302a5SGregory Neil Shapiro noeol = true; 1041605302a5SGregory Neil Shapiro } 1042c2aa98e2SPeter Wemm 1043c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 104440266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 104540266059SGregory Neil Shapiro "%05d >>> ", (int) CurrentPid); 1046c2aa98e2SPeter Wemm 1047c2aa98e2SPeter Wemm /* check for line overflow */ 1048c2aa98e2SPeter Wemm while (mci->mci_mailer->m_linelimit > 0 && 1049c2aa98e2SPeter Wemm (p - l + slop) > mci->mci_mailer->m_linelimit) 1050c2aa98e2SPeter Wemm { 1051c2aa98e2SPeter Wemm char *l_base = l; 1052c2aa98e2SPeter Wemm register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 1053c2aa98e2SPeter Wemm 1054c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 && 1055c2aa98e2SPeter Wemm bitnset(M_XDOT, mci->mci_mailer->m_flags)) 1056c2aa98e2SPeter Wemm { 105740266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 105840266059SGregory Neil Shapiro '.') == SM_IO_EOF) 105940266059SGregory Neil Shapiro dead = true; 1060193538b7SGregory Neil Shapiro else 1061193538b7SGregory Neil Shapiro { 1062193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 106340266059SGregory Neil Shapiro DataProgress = true; 1064193538b7SGregory Neil Shapiro } 1065c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 106640266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 106740266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.'); 1068c2aa98e2SPeter Wemm } 1069c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 && 1070c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) && 1071c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 && 1072c2aa98e2SPeter Wemm bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 1073c2aa98e2SPeter Wemm { 107440266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 107540266059SGregory Neil Shapiro '>') == SM_IO_EOF) 107640266059SGregory Neil Shapiro dead = true; 1077193538b7SGregory Neil Shapiro else 1078193538b7SGregory Neil Shapiro { 1079193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 108040266059SGregory Neil Shapiro DataProgress = true; 1081193538b7SGregory Neil Shapiro } 1082c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 108340266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 108440266059SGregory Neil Shapiro SM_TIME_DEFAULT, 108540266059SGregory Neil Shapiro '>'); 1086c2aa98e2SPeter Wemm } 108706f25ae9SGregory Neil Shapiro if (dead) 108806f25ae9SGregory Neil Shapiro break; 108906f25ae9SGregory Neil Shapiro 1090c2aa98e2SPeter Wemm while (l < q) 1091c2aa98e2SPeter Wemm { 109240266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 109340266059SGregory Neil Shapiro (unsigned char) *l++) == SM_IO_EOF) 109406f25ae9SGregory Neil Shapiro { 109540266059SGregory Neil Shapiro dead = true; 109606f25ae9SGregory Neil Shapiro break; 1097c2aa98e2SPeter Wemm } 1098193538b7SGregory Neil Shapiro else 1099193538b7SGregory Neil Shapiro { 110006f25ae9SGregory Neil Shapiro /* record progress for DATA timeout */ 110140266059SGregory Neil Shapiro DataProgress = true; 110206f25ae9SGregory Neil Shapiro } 1103193538b7SGregory Neil Shapiro } 110406f25ae9SGregory Neil Shapiro if (dead) 110506f25ae9SGregory Neil Shapiro break; 110606f25ae9SGregory Neil Shapiro 110740266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '!') == 110840266059SGregory Neil Shapiro SM_IO_EOF || 110940266059SGregory Neil Shapiro sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT, 111040266059SGregory Neil Shapiro mci->mci_mailer->m_eol) == 111140266059SGregory Neil Shapiro SM_IO_EOF || 111240266059SGregory Neil Shapiro sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, ' ') == 111340266059SGregory Neil Shapiro SM_IO_EOF) 111406f25ae9SGregory Neil Shapiro { 111540266059SGregory Neil Shapiro dead = true; 111606f25ae9SGregory Neil Shapiro break; 111706f25ae9SGregory Neil Shapiro } 1118193538b7SGregory Neil Shapiro else 1119193538b7SGregory Neil Shapiro { 112006f25ae9SGregory Neil Shapiro /* record progress for DATA timeout */ 112140266059SGregory Neil Shapiro DataProgress = true; 1122193538b7SGregory Neil Shapiro } 1123c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 1124c2aa98e2SPeter Wemm { 1125c2aa98e2SPeter Wemm for (l = l_base; l < q; l++) 112640266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 112740266059SGregory Neil Shapiro SM_TIME_DEFAULT, 112840266059SGregory Neil Shapiro (unsigned char)*l); 112940266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, 113040266059SGregory Neil Shapiro SM_TIME_DEFAULT, 113140266059SGregory Neil Shapiro "!\n%05d >>> ", 113240266059SGregory Neil Shapiro (int) CurrentPid); 1133c2aa98e2SPeter Wemm } 1134c2aa98e2SPeter Wemm slop = 1; 1135c2aa98e2SPeter Wemm } 1136c2aa98e2SPeter Wemm 113706f25ae9SGregory Neil Shapiro if (dead) 113806f25ae9SGregory Neil Shapiro break; 113906f25ae9SGregory Neil Shapiro 1140c2aa98e2SPeter Wemm /* output last part */ 1141c2aa98e2SPeter Wemm if (l[0] == '.' && slop == 0 && 1142c2aa98e2SPeter Wemm bitnset(M_XDOT, mci->mci_mailer->m_flags)) 1143c2aa98e2SPeter Wemm { 114440266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '.') == 114540266059SGregory Neil Shapiro SM_IO_EOF) 114606f25ae9SGregory Neil Shapiro break; 1147193538b7SGregory Neil Shapiro else 1148193538b7SGregory Neil Shapiro { 1149193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 115040266059SGregory Neil Shapiro DataProgress = true; 1151193538b7SGregory Neil Shapiro } 1152c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 115340266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 115440266059SGregory Neil Shapiro SM_TIME_DEFAULT, '.'); 1155c2aa98e2SPeter Wemm } 1156c2aa98e2SPeter Wemm else if (l[0] == 'F' && slop == 0 && 1157c2aa98e2SPeter Wemm bitset(PXLF_MAPFROM, pxflags) && 1158c2aa98e2SPeter Wemm strncmp(l, "From ", 5) == 0 && 1159c2aa98e2SPeter Wemm bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 1160c2aa98e2SPeter Wemm { 116140266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, '>') == 116240266059SGregory Neil Shapiro SM_IO_EOF) 116306f25ae9SGregory Neil Shapiro break; 1164193538b7SGregory Neil Shapiro else 1165193538b7SGregory Neil Shapiro { 1166193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 116740266059SGregory Neil Shapiro DataProgress = true; 1168193538b7SGregory Neil Shapiro } 1169c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 117040266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 117140266059SGregory Neil Shapiro SM_TIME_DEFAULT, '>'); 1172c2aa98e2SPeter Wemm } 1173c2aa98e2SPeter Wemm for ( ; l < p; ++l) 1174c2aa98e2SPeter Wemm { 1175c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 117640266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 117740266059SGregory Neil Shapiro SM_TIME_DEFAULT, 117840266059SGregory Neil Shapiro (unsigned char)*l); 117940266059SGregory Neil Shapiro if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT, 118040266059SGregory Neil Shapiro (unsigned char) *l) == SM_IO_EOF) 118106f25ae9SGregory Neil Shapiro { 118240266059SGregory Neil Shapiro dead = true; 118306f25ae9SGregory Neil Shapiro break; 1184c2aa98e2SPeter Wemm } 1185193538b7SGregory Neil Shapiro else 1186193538b7SGregory Neil Shapiro { 118706f25ae9SGregory Neil Shapiro /* record progress for DATA timeout */ 118840266059SGregory Neil Shapiro DataProgress = true; 118906f25ae9SGregory Neil Shapiro } 1190193538b7SGregory Neil Shapiro } 119106f25ae9SGregory Neil Shapiro if (dead) 119206f25ae9SGregory Neil Shapiro break; 119306f25ae9SGregory Neil Shapiro 1194c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 119540266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, SM_TIME_DEFAULT, 119640266059SGregory Neil Shapiro '\n'); 1197605302a5SGregory Neil Shapiro if ((!bitset(PXLF_NOADDEOL, pxflags) || !noeol) && 1198605302a5SGregory Neil Shapiro sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT, 119940266059SGregory Neil Shapiro mci->mci_mailer->m_eol) == SM_IO_EOF) 120006f25ae9SGregory Neil Shapiro break; 1201193538b7SGregory Neil Shapiro else 1202193538b7SGregory Neil Shapiro { 1203193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 120440266059SGregory Neil Shapiro DataProgress = true; 1205193538b7SGregory 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) 121306f25ae9SGregory Neil Shapiro break; 1214193538b7SGregory Neil Shapiro else 1215193538b7SGregory Neil Shapiro { 1216193538b7SGregory Neil Shapiro /* record progress for DATA timeout */ 121740266059SGregory Neil Shapiro DataProgress = true; 1218193538b7SGregory Neil Shapiro } 121940266059SGregory Neil Shapiro 1220c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 122140266059SGregory Neil Shapiro (void) sm_io_putc(TrafficLogFile, 122240266059SGregory Neil Shapiro SM_TIME_DEFAULT, ' '); 1223c2aa98e2SPeter Wemm } 1224c2aa98e2SPeter Wemm } 122540266059SGregory Neil Shapiro 122640266059SGregory Neil Shapiro /* record progress for DATA timeout */ 122740266059SGregory Neil Shapiro DataProgress = true; 1228c2aa98e2SPeter Wemm } while (l < end); 1229c2aa98e2SPeter Wemm } 123040266059SGregory Neil Shapiro /* 1231c2aa98e2SPeter Wemm ** XUNLINK -- unlink a file, doing logging as appropriate. 1232c2aa98e2SPeter Wemm ** 1233c2aa98e2SPeter Wemm ** Parameters: 1234c2aa98e2SPeter Wemm ** f -- name of file to unlink. 1235c2aa98e2SPeter Wemm ** 1236c2aa98e2SPeter Wemm ** Returns: 123740266059SGregory Neil Shapiro ** return value of unlink() 1238c2aa98e2SPeter Wemm ** 1239c2aa98e2SPeter Wemm ** Side Effects: 1240c2aa98e2SPeter Wemm ** f is unlinked. 1241c2aa98e2SPeter Wemm */ 1242c2aa98e2SPeter Wemm 124340266059SGregory Neil Shapiro int 1244c2aa98e2SPeter Wemm xunlink(f) 1245c2aa98e2SPeter Wemm char *f; 1246c2aa98e2SPeter Wemm { 1247c2aa98e2SPeter Wemm register int i; 124840266059SGregory Neil Shapiro int save_errno; 1249c2aa98e2SPeter Wemm 1250c2aa98e2SPeter Wemm if (LogLevel > 98) 125140266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "unlink %s", f); 1252c2aa98e2SPeter Wemm 1253c2aa98e2SPeter Wemm i = unlink(f); 125440266059SGregory Neil Shapiro save_errno = errno; 1255c2aa98e2SPeter Wemm if (i < 0 && LogLevel > 97) 125640266059SGregory Neil Shapiro sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s: unlink-fail %d", 1257c2aa98e2SPeter Wemm f, errno); 125840266059SGregory Neil Shapiro if (i >= 0) 125940266059SGregory Neil Shapiro SYNC_DIR(f, false); 126040266059SGregory Neil Shapiro errno = save_errno; 126140266059SGregory Neil Shapiro return i; 1262c2aa98e2SPeter Wemm } 126340266059SGregory Neil Shapiro /* 1264c2aa98e2SPeter Wemm ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 1265c2aa98e2SPeter Wemm ** 1266c2aa98e2SPeter Wemm ** Parameters: 1267c2aa98e2SPeter Wemm ** buf -- place to put the input line. 1268c2aa98e2SPeter Wemm ** siz -- size of buf. 1269c2aa98e2SPeter Wemm ** fp -- file to read from. 1270c2aa98e2SPeter Wemm ** timeout -- the timeout before error occurs. 1271c2aa98e2SPeter Wemm ** during -- what we are trying to read (for error messages). 1272c2aa98e2SPeter Wemm ** 1273c2aa98e2SPeter Wemm ** Returns: 127440266059SGregory Neil Shapiro ** NULL on error (including timeout). This may also leave 1275c2aa98e2SPeter Wemm ** buf containing a null string. 1276c2aa98e2SPeter Wemm ** buf otherwise. 1277c2aa98e2SPeter Wemm */ 1278c2aa98e2SPeter Wemm 127906f25ae9SGregory Neil Shapiro 1280c2aa98e2SPeter Wemm char * 1281c2aa98e2SPeter Wemm sfgets(buf, siz, fp, timeout, during) 1282c2aa98e2SPeter Wemm char *buf; 1283c2aa98e2SPeter Wemm int siz; 128440266059SGregory Neil Shapiro SM_FILE_T *fp; 1285c2aa98e2SPeter Wemm time_t timeout; 1286c2aa98e2SPeter Wemm char *during; 1287c2aa98e2SPeter Wemm { 1288c2aa98e2SPeter Wemm register char *p; 12892e43090eSPeter Wemm int save_errno; 129040266059SGregory Neil Shapiro int io_timeout; 129140266059SGregory Neil Shapiro 129240266059SGregory Neil Shapiro SM_REQUIRE(siz > 0); 129340266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL); 1294c2aa98e2SPeter Wemm 1295c2aa98e2SPeter Wemm if (fp == NULL) 1296c2aa98e2SPeter Wemm { 1297c2aa98e2SPeter Wemm buf[0] = '\0'; 129840266059SGregory Neil Shapiro errno = EBADF; 1299c2aa98e2SPeter Wemm return NULL; 1300c2aa98e2SPeter Wemm } 1301c2aa98e2SPeter Wemm 130240266059SGregory Neil Shapiro /* try to read */ 130340266059SGregory Neil Shapiro p = NULL; 130440266059SGregory Neil Shapiro errno = 0; 130540266059SGregory Neil Shapiro 130640266059SGregory Neil Shapiro /* convert the timeout to sm_io notation */ 130740266059SGregory Neil Shapiro io_timeout = (timeout <= 0) ? SM_TIME_DEFAULT : timeout * 1000; 130840266059SGregory Neil Shapiro while (!sm_io_eof(fp) && !sm_io_error(fp)) 1309c2aa98e2SPeter Wemm { 131040266059SGregory Neil Shapiro errno = 0; 131140266059SGregory Neil Shapiro p = sm_io_fgets(fp, io_timeout, buf, siz); 131240266059SGregory Neil Shapiro if (p == NULL && errno == EAGAIN) 1313c2aa98e2SPeter Wemm { 131440266059SGregory Neil Shapiro /* The sm_io_fgets() call timedout */ 1315c2aa98e2SPeter Wemm if (LogLevel > 1) 1316c2aa98e2SPeter Wemm sm_syslog(LOG_NOTICE, CurEnv->e_id, 1317c2aa98e2SPeter Wemm "timeout waiting for input from %.100s during %s", 131840266059SGregory Neil Shapiro CURHOSTNAME, 1319c2aa98e2SPeter Wemm during); 1320c2aa98e2SPeter Wemm buf[0] = '\0'; 1321c2aa98e2SPeter Wemm #if XDEBUG 1322c2aa98e2SPeter Wemm checkfd012(during); 132306f25ae9SGregory Neil Shapiro #endif /* XDEBUG */ 1324c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 132540266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, 132640266059SGregory Neil Shapiro SM_TIME_DEFAULT, 132740266059SGregory Neil Shapiro "%05d <<< [TIMEOUT]\n", 132840266059SGregory Neil Shapiro (int) CurrentPid); 132940266059SGregory Neil Shapiro errno = ETIMEDOUT; 133006f25ae9SGregory Neil Shapiro return NULL; 1331c2aa98e2SPeter Wemm } 1332c2aa98e2SPeter Wemm if (p != NULL || errno != EINTR) 1333c2aa98e2SPeter Wemm break; 133440266059SGregory Neil Shapiro (void) sm_io_clearerr(fp); 1335c2aa98e2SPeter Wemm } 13362e43090eSPeter Wemm save_errno = errno; 1337c2aa98e2SPeter Wemm 1338c2aa98e2SPeter Wemm /* clean up the books and exit */ 1339c2aa98e2SPeter Wemm LineNumber++; 1340c2aa98e2SPeter Wemm if (p == NULL) 1341c2aa98e2SPeter Wemm { 1342c2aa98e2SPeter Wemm buf[0] = '\0'; 1343c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 134440266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 134540266059SGregory Neil Shapiro "%05d <<< [EOF]\n", 134640266059SGregory Neil Shapiro (int) CurrentPid); 13472e43090eSPeter Wemm errno = save_errno; 134806f25ae9SGregory Neil Shapiro return NULL; 1349c2aa98e2SPeter Wemm } 1350c2aa98e2SPeter Wemm if (TrafficLogFile != NULL) 135140266059SGregory Neil Shapiro (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT, 135240266059SGregory Neil Shapiro "%05d <<< %s", (int) CurrentPid, buf); 1353c2aa98e2SPeter Wemm if (SevenBitInput) 1354c2aa98e2SPeter Wemm { 1355c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++) 1356c2aa98e2SPeter Wemm *p &= ~0200; 1357c2aa98e2SPeter Wemm } 1358c2aa98e2SPeter Wemm else if (!HasEightBits) 1359c2aa98e2SPeter Wemm { 1360c2aa98e2SPeter Wemm for (p = buf; *p != '\0'; p++) 1361c2aa98e2SPeter Wemm { 1362c2aa98e2SPeter Wemm if (bitset(0200, *p)) 1363c2aa98e2SPeter Wemm { 136440266059SGregory Neil Shapiro HasEightBits = true; 1365c2aa98e2SPeter Wemm break; 1366c2aa98e2SPeter Wemm } 1367c2aa98e2SPeter Wemm } 1368c2aa98e2SPeter Wemm } 136906f25ae9SGregory Neil Shapiro return buf; 1370c2aa98e2SPeter Wemm } 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. 1376c2aa98e2SPeter Wemm ** n -- bytes available. 1377c2aa98e2SPeter Wemm ** f -- file to read from. 1378c2aa98e2SPeter Wemm ** 1379c2aa98e2SPeter Wemm ** Returns: 138040266059SGregory Neil Shapiro ** input line(s) on success, NULL on error or SM_IO_EOF. 1381c2aa98e2SPeter Wemm ** This will normally be buf -- unless the line is too 138240266059SGregory Neil Shapiro ** long, when it will be sm_malloc_x()ed. 1383c2aa98e2SPeter Wemm ** 1384c2aa98e2SPeter Wemm ** Side Effects: 1385c2aa98e2SPeter Wemm ** buf gets lines from f, with continuation lines (lines 1386c2aa98e2SPeter Wemm ** with leading white space) appended. CRLF's are mapped 1387c2aa98e2SPeter Wemm ** into single newlines. Any trailing NL is stripped. 1388c2aa98e2SPeter Wemm */ 1389c2aa98e2SPeter Wemm 1390c2aa98e2SPeter Wemm char * 1391c2aa98e2SPeter Wemm fgetfolded(buf, n, f) 1392c2aa98e2SPeter Wemm char *buf; 1393c2aa98e2SPeter Wemm register int n; 139440266059SGregory Neil Shapiro SM_FILE_T *f; 1395c2aa98e2SPeter Wemm { 1396c2aa98e2SPeter Wemm register char *p = buf; 1397c2aa98e2SPeter Wemm char *bp = buf; 1398c2aa98e2SPeter Wemm register int i; 1399c2aa98e2SPeter Wemm 140040266059SGregory Neil Shapiro SM_REQUIRE(n > 0); 140140266059SGregory Neil Shapiro SM_REQUIRE(buf != NULL); 140240266059SGregory Neil Shapiro if (f == NULL) 140340266059SGregory Neil Shapiro { 140440266059SGregory Neil Shapiro buf[0] = '\0'; 140540266059SGregory Neil Shapiro errno = EBADF; 140640266059SGregory Neil Shapiro return NULL; 140740266059SGregory Neil Shapiro } 140840266059SGregory Neil Shapiro 1409c2aa98e2SPeter Wemm n--; 141040266059SGregory Neil Shapiro while ((i = sm_io_getc(f, SM_TIME_DEFAULT)) != SM_IO_EOF) 1411c2aa98e2SPeter Wemm { 1412c2aa98e2SPeter Wemm if (i == '\r') 1413c2aa98e2SPeter Wemm { 141440266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT); 1415c2aa98e2SPeter Wemm if (i != '\n') 1416c2aa98e2SPeter Wemm { 141740266059SGregory Neil Shapiro if (i != SM_IO_EOF) 141840266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT, 141940266059SGregory Neil Shapiro i); 1420c2aa98e2SPeter Wemm i = '\r'; 1421c2aa98e2SPeter Wemm } 1422c2aa98e2SPeter Wemm } 1423c2aa98e2SPeter Wemm if (--n <= 0) 1424c2aa98e2SPeter Wemm { 1425c2aa98e2SPeter Wemm /* allocate new space */ 1426c2aa98e2SPeter Wemm char *nbp; 1427c2aa98e2SPeter Wemm int nn; 1428c2aa98e2SPeter Wemm 1429c2aa98e2SPeter Wemm nn = (p - bp); 1430c2aa98e2SPeter Wemm if (nn < MEMCHUNKSIZE) 1431c2aa98e2SPeter Wemm nn *= 2; 1432c2aa98e2SPeter Wemm else 1433c2aa98e2SPeter Wemm nn += MEMCHUNKSIZE; 143440266059SGregory Neil Shapiro nbp = sm_malloc_x(nn); 143506f25ae9SGregory Neil Shapiro memmove(nbp, bp, p - bp); 1436c2aa98e2SPeter Wemm p = &nbp[p - bp]; 1437c2aa98e2SPeter Wemm if (bp != buf) 14388774250cSGregory Neil Shapiro sm_free(bp); 1439c2aa98e2SPeter Wemm bp = nbp; 1440c2aa98e2SPeter Wemm n = nn - (p - bp); 1441c2aa98e2SPeter Wemm } 1442c2aa98e2SPeter Wemm *p++ = i; 1443c2aa98e2SPeter Wemm if (i == '\n') 1444c2aa98e2SPeter Wemm { 1445c2aa98e2SPeter Wemm LineNumber++; 144640266059SGregory Neil Shapiro i = sm_io_getc(f, SM_TIME_DEFAULT); 144740266059SGregory Neil Shapiro if (i != SM_IO_EOF) 144840266059SGregory Neil Shapiro (void) sm_io_ungetc(f, SM_TIME_DEFAULT, i); 1449c2aa98e2SPeter Wemm if (i != ' ' && i != '\t') 1450c2aa98e2SPeter Wemm break; 1451c2aa98e2SPeter Wemm } 1452c2aa98e2SPeter Wemm } 1453c2aa98e2SPeter Wemm if (p == bp) 145406f25ae9SGregory Neil Shapiro return NULL; 1455c2aa98e2SPeter Wemm if (p[-1] == '\n') 1456c2aa98e2SPeter Wemm p--; 1457c2aa98e2SPeter Wemm *p = '\0'; 145806f25ae9SGregory Neil Shapiro return bp; 1459c2aa98e2SPeter Wemm } 146040266059SGregory Neil Shapiro /* 1461c2aa98e2SPeter Wemm ** CURTIME -- return current time. 1462c2aa98e2SPeter Wemm ** 1463c2aa98e2SPeter Wemm ** Parameters: 1464c2aa98e2SPeter Wemm ** none. 1465c2aa98e2SPeter Wemm ** 1466c2aa98e2SPeter Wemm ** Returns: 1467c2aa98e2SPeter Wemm ** the current time. 1468c2aa98e2SPeter Wemm */ 1469c2aa98e2SPeter Wemm 1470c2aa98e2SPeter Wemm time_t 1471c2aa98e2SPeter Wemm curtime() 1472c2aa98e2SPeter Wemm { 1473c2aa98e2SPeter Wemm auto time_t t; 1474c2aa98e2SPeter Wemm 1475c2aa98e2SPeter Wemm (void) time(&t); 147606f25ae9SGregory Neil Shapiro return t; 1477c2aa98e2SPeter Wemm } 147840266059SGregory Neil Shapiro /* 1479c2aa98e2SPeter Wemm ** ATOBOOL -- convert a string representation to boolean. 1480c2aa98e2SPeter Wemm ** 148140266059SGregory Neil Shapiro ** Defaults to false 1482c2aa98e2SPeter Wemm ** 1483c2aa98e2SPeter Wemm ** Parameters: 148440266059SGregory Neil Shapiro ** s -- string to convert. Takes "tTyY", empty, and NULL as true, 1485c2aa98e2SPeter Wemm ** others as false. 1486c2aa98e2SPeter Wemm ** 1487c2aa98e2SPeter Wemm ** Returns: 1488c2aa98e2SPeter Wemm ** A boolean representation of the string. 1489c2aa98e2SPeter Wemm */ 1490c2aa98e2SPeter Wemm 1491c2aa98e2SPeter Wemm bool 1492c2aa98e2SPeter Wemm atobool(s) 1493c2aa98e2SPeter Wemm register char *s; 1494c2aa98e2SPeter Wemm { 1495c2aa98e2SPeter Wemm if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 149640266059SGregory Neil Shapiro return true; 149740266059SGregory Neil Shapiro return false; 1498c2aa98e2SPeter Wemm } 149940266059SGregory Neil Shapiro /* 1500c2aa98e2SPeter Wemm ** ATOOCT -- convert a string representation to octal. 1501c2aa98e2SPeter Wemm ** 1502c2aa98e2SPeter Wemm ** Parameters: 1503c2aa98e2SPeter Wemm ** s -- string to convert. 1504c2aa98e2SPeter Wemm ** 1505c2aa98e2SPeter Wemm ** Returns: 1506c2aa98e2SPeter Wemm ** An integer representing the string interpreted as an 1507c2aa98e2SPeter Wemm ** octal number. 1508c2aa98e2SPeter Wemm */ 1509c2aa98e2SPeter Wemm 1510c2aa98e2SPeter Wemm int 1511c2aa98e2SPeter Wemm atooct(s) 1512c2aa98e2SPeter Wemm register char *s; 1513c2aa98e2SPeter Wemm { 1514c2aa98e2SPeter Wemm register int i = 0; 1515c2aa98e2SPeter Wemm 1516c2aa98e2SPeter Wemm while (*s >= '0' && *s <= '7') 1517c2aa98e2SPeter Wemm i = (i << 3) | (*s++ - '0'); 151806f25ae9SGregory Neil Shapiro return i; 1519c2aa98e2SPeter Wemm } 152040266059SGregory Neil Shapiro /* 1521c2aa98e2SPeter Wemm ** BITINTERSECT -- tell if two bitmaps intersect 1522c2aa98e2SPeter Wemm ** 1523c2aa98e2SPeter Wemm ** Parameters: 1524c2aa98e2SPeter Wemm ** a, b -- the bitmaps in question 1525c2aa98e2SPeter Wemm ** 1526c2aa98e2SPeter Wemm ** Returns: 152740266059SGregory Neil Shapiro ** true if they have a non-null intersection 152840266059SGregory Neil Shapiro ** false otherwise 1529c2aa98e2SPeter Wemm */ 1530c2aa98e2SPeter Wemm 1531c2aa98e2SPeter Wemm bool 1532c2aa98e2SPeter Wemm bitintersect(a, b) 153306f25ae9SGregory Neil Shapiro BITMAP256 a; 153406f25ae9SGregory Neil Shapiro BITMAP256 b; 1535c2aa98e2SPeter Wemm { 1536c2aa98e2SPeter Wemm int i; 1537c2aa98e2SPeter Wemm 1538c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1539193538b7SGregory Neil Shapiro { 1540c2aa98e2SPeter Wemm if ((a[i] & b[i]) != 0) 154140266059SGregory Neil Shapiro return true; 1542193538b7SGregory Neil Shapiro } 154340266059SGregory Neil Shapiro return false; 1544c2aa98e2SPeter Wemm } 154540266059SGregory Neil Shapiro /* 1546c2aa98e2SPeter Wemm ** BITZEROP -- tell if a bitmap is all zero 1547c2aa98e2SPeter Wemm ** 1548c2aa98e2SPeter Wemm ** Parameters: 1549c2aa98e2SPeter Wemm ** map -- the bit map to check 1550c2aa98e2SPeter Wemm ** 1551c2aa98e2SPeter Wemm ** Returns: 155240266059SGregory Neil Shapiro ** true if map is all zero. 155340266059SGregory Neil Shapiro ** false if there are any bits set in map. 1554c2aa98e2SPeter Wemm */ 1555c2aa98e2SPeter Wemm 1556c2aa98e2SPeter Wemm bool 1557c2aa98e2SPeter Wemm bitzerop(map) 155806f25ae9SGregory Neil Shapiro BITMAP256 map; 1559c2aa98e2SPeter Wemm { 1560c2aa98e2SPeter Wemm int i; 1561c2aa98e2SPeter Wemm 1562c2aa98e2SPeter Wemm for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1563193538b7SGregory Neil Shapiro { 1564c2aa98e2SPeter Wemm if (map[i] != 0) 156540266059SGregory Neil Shapiro return false; 1566193538b7SGregory Neil Shapiro } 156740266059SGregory Neil Shapiro return true; 1568c2aa98e2SPeter Wemm } 156940266059SGregory Neil Shapiro /* 1570c2aa98e2SPeter Wemm ** STRCONTAINEDIN -- tell if one string is contained in another 1571c2aa98e2SPeter Wemm ** 1572c2aa98e2SPeter Wemm ** Parameters: 157340266059SGregory Neil Shapiro ** icase -- ignore case? 1574c2aa98e2SPeter Wemm ** a -- possible substring. 1575c2aa98e2SPeter Wemm ** b -- possible superstring. 1576c2aa98e2SPeter Wemm ** 1577c2aa98e2SPeter Wemm ** Returns: 157840266059SGregory Neil Shapiro ** true if a is contained in b (case insensitive). 157940266059SGregory Neil Shapiro ** false otherwise. 1580c2aa98e2SPeter Wemm */ 1581c2aa98e2SPeter Wemm 1582c2aa98e2SPeter Wemm bool 158340266059SGregory Neil Shapiro strcontainedin(icase, a, b) 158440266059SGregory Neil Shapiro bool icase; 1585c2aa98e2SPeter Wemm register char *a; 1586c2aa98e2SPeter Wemm register char *b; 1587c2aa98e2SPeter Wemm { 1588c2aa98e2SPeter Wemm int la; 1589c2aa98e2SPeter Wemm int lb; 1590c2aa98e2SPeter Wemm int c; 1591c2aa98e2SPeter Wemm 1592c2aa98e2SPeter Wemm la = strlen(a); 1593c2aa98e2SPeter Wemm lb = strlen(b); 1594c2aa98e2SPeter Wemm c = *a; 159540266059SGregory Neil Shapiro if (icase && isascii(c) && isupper(c)) 1596c2aa98e2SPeter Wemm c = tolower(c); 1597c2aa98e2SPeter Wemm for (; lb-- >= la; b++) 1598c2aa98e2SPeter Wemm { 159940266059SGregory Neil Shapiro if (icase) 160040266059SGregory Neil Shapiro { 160140266059SGregory Neil Shapiro if (*b != c && 160240266059SGregory Neil Shapiro isascii(*b) && isupper(*b) && tolower(*b) != c) 1603c2aa98e2SPeter Wemm continue; 160440266059SGregory Neil Shapiro if (sm_strncasecmp(a, b, la) == 0) 160540266059SGregory Neil Shapiro return true; 1606c2aa98e2SPeter Wemm } 160740266059SGregory Neil Shapiro else 160840266059SGregory Neil Shapiro { 160940266059SGregory Neil Shapiro if (*b != c) 161040266059SGregory Neil Shapiro continue; 161140266059SGregory Neil Shapiro if (strncmp(a, b, la) == 0) 161240266059SGregory Neil Shapiro return true; 1613c2aa98e2SPeter Wemm } 161440266059SGregory Neil Shapiro } 161540266059SGregory Neil Shapiro return false; 161640266059SGregory Neil Shapiro } 161740266059SGregory Neil Shapiro /* 1618c2aa98e2SPeter Wemm ** CHECKFD012 -- check low numbered file descriptors 1619c2aa98e2SPeter Wemm ** 1620c2aa98e2SPeter Wemm ** File descriptors 0, 1, and 2 should be open at all times. 1621c2aa98e2SPeter Wemm ** This routine verifies that, and fixes it if not true. 1622c2aa98e2SPeter Wemm ** 1623c2aa98e2SPeter Wemm ** Parameters: 1624c2aa98e2SPeter Wemm ** where -- a tag printed if the assertion failed 1625c2aa98e2SPeter Wemm ** 1626c2aa98e2SPeter Wemm ** Returns: 1627c2aa98e2SPeter Wemm ** none 1628c2aa98e2SPeter Wemm */ 1629c2aa98e2SPeter Wemm 1630c2aa98e2SPeter Wemm void 1631c2aa98e2SPeter Wemm checkfd012(where) 1632c2aa98e2SPeter Wemm char *where; 1633c2aa98e2SPeter Wemm { 1634c2aa98e2SPeter Wemm #if XDEBUG 1635c2aa98e2SPeter Wemm register int i; 1636c2aa98e2SPeter Wemm 1637c2aa98e2SPeter Wemm for (i = 0; i < 3; i++) 1638c2aa98e2SPeter Wemm fill_fd(i, where); 1639c2aa98e2SPeter Wemm #endif /* XDEBUG */ 1640c2aa98e2SPeter Wemm } 164140266059SGregory Neil Shapiro /* 1642c2aa98e2SPeter Wemm ** CHECKFDOPEN -- make sure file descriptor is open -- for extended debugging 1643c2aa98e2SPeter Wemm ** 1644c2aa98e2SPeter Wemm ** Parameters: 1645c2aa98e2SPeter Wemm ** fd -- file descriptor to check. 1646c2aa98e2SPeter Wemm ** where -- tag to print on failure. 1647c2aa98e2SPeter Wemm ** 1648c2aa98e2SPeter Wemm ** Returns: 1649c2aa98e2SPeter Wemm ** none. 1650c2aa98e2SPeter Wemm */ 1651c2aa98e2SPeter Wemm 1652c2aa98e2SPeter Wemm void 1653c2aa98e2SPeter Wemm checkfdopen(fd, where) 1654c2aa98e2SPeter Wemm int fd; 1655c2aa98e2SPeter Wemm char *where; 1656c2aa98e2SPeter Wemm { 1657c2aa98e2SPeter Wemm #if XDEBUG 1658c2aa98e2SPeter Wemm struct stat st; 1659c2aa98e2SPeter Wemm 1660c2aa98e2SPeter Wemm if (fstat(fd, &st) < 0 && errno == EBADF) 1661c2aa98e2SPeter Wemm { 1662c2aa98e2SPeter Wemm syserr("checkfdopen(%d): %s not open as expected!", fd, where); 166340266059SGregory Neil Shapiro printopenfds(true); 1664c2aa98e2SPeter Wemm } 166506f25ae9SGregory Neil Shapiro #endif /* XDEBUG */ 1666c2aa98e2SPeter Wemm } 166740266059SGregory Neil Shapiro /* 1668c2aa98e2SPeter Wemm ** CHECKFDS -- check for new or missing file descriptors 1669c2aa98e2SPeter Wemm ** 1670c2aa98e2SPeter Wemm ** Parameters: 1671c2aa98e2SPeter Wemm ** where -- tag for printing. If null, take a base line. 1672c2aa98e2SPeter Wemm ** 1673c2aa98e2SPeter Wemm ** Returns: 1674c2aa98e2SPeter Wemm ** none 1675c2aa98e2SPeter Wemm ** 1676c2aa98e2SPeter Wemm ** Side Effects: 1677c2aa98e2SPeter Wemm ** If where is set, shows changes since the last call. 1678c2aa98e2SPeter Wemm */ 1679c2aa98e2SPeter Wemm 1680c2aa98e2SPeter Wemm void 1681c2aa98e2SPeter Wemm checkfds(where) 1682c2aa98e2SPeter Wemm char *where; 1683c2aa98e2SPeter Wemm { 1684c2aa98e2SPeter Wemm int maxfd; 1685c2aa98e2SPeter Wemm register int fd; 168640266059SGregory Neil Shapiro bool printhdr = true; 1687c2aa98e2SPeter Wemm int save_errno = errno; 168806f25ae9SGregory Neil Shapiro static BITMAP256 baseline; 1689c2aa98e2SPeter Wemm extern int DtableSize; 1690c2aa98e2SPeter Wemm 1691193538b7SGregory Neil Shapiro if (DtableSize > BITMAPBITS) 1692193538b7SGregory Neil Shapiro maxfd = BITMAPBITS; 1693c2aa98e2SPeter Wemm else 1694c2aa98e2SPeter Wemm maxfd = DtableSize; 1695c2aa98e2SPeter Wemm if (where == NULL) 1696c2aa98e2SPeter Wemm clrbitmap(baseline); 1697c2aa98e2SPeter Wemm 1698c2aa98e2SPeter Wemm for (fd = 0; fd < maxfd; fd++) 1699c2aa98e2SPeter Wemm { 1700c2aa98e2SPeter Wemm struct stat stbuf; 1701c2aa98e2SPeter Wemm 1702c2aa98e2SPeter Wemm if (fstat(fd, &stbuf) < 0 && errno != EOPNOTSUPP) 1703c2aa98e2SPeter Wemm { 1704c2aa98e2SPeter Wemm if (!bitnset(fd, baseline)) 1705c2aa98e2SPeter Wemm continue; 1706c2aa98e2SPeter Wemm clrbitn(fd, baseline); 1707c2aa98e2SPeter Wemm } 1708c2aa98e2SPeter Wemm else if (!bitnset(fd, baseline)) 1709c2aa98e2SPeter Wemm setbitn(fd, baseline); 1710c2aa98e2SPeter Wemm else 1711c2aa98e2SPeter Wemm continue; 1712c2aa98e2SPeter Wemm 1713c2aa98e2SPeter Wemm /* file state has changed */ 1714c2aa98e2SPeter Wemm if (where == NULL) 1715c2aa98e2SPeter Wemm continue; 1716c2aa98e2SPeter Wemm if (printhdr) 1717c2aa98e2SPeter Wemm { 1718c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id, 1719c2aa98e2SPeter Wemm "%s: changed fds:", 1720c2aa98e2SPeter Wemm where); 172140266059SGregory Neil Shapiro printhdr = false; 1722c2aa98e2SPeter Wemm } 172340266059SGregory Neil Shapiro dumpfd(fd, true, true); 1724c2aa98e2SPeter Wemm } 1725c2aa98e2SPeter Wemm errno = save_errno; 1726c2aa98e2SPeter Wemm } 172740266059SGregory Neil Shapiro /* 1728c2aa98e2SPeter Wemm ** PRINTOPENFDS -- print the open file descriptors (for debugging) 1729c2aa98e2SPeter Wemm ** 1730c2aa98e2SPeter Wemm ** Parameters: 1731c2aa98e2SPeter Wemm ** logit -- if set, send output to syslog; otherwise 1732c2aa98e2SPeter Wemm ** print for debugging. 1733c2aa98e2SPeter Wemm ** 1734c2aa98e2SPeter Wemm ** Returns: 1735c2aa98e2SPeter Wemm ** none. 1736c2aa98e2SPeter Wemm */ 1737c2aa98e2SPeter Wemm 173806f25ae9SGregory Neil Shapiro #if NETINET || NETINET6 1739c2aa98e2SPeter Wemm # include <arpa/inet.h> 174006f25ae9SGregory Neil Shapiro #endif /* NETINET || NETINET6 */ 1741c2aa98e2SPeter Wemm 1742c2aa98e2SPeter Wemm void 1743c2aa98e2SPeter Wemm printopenfds(logit) 1744c2aa98e2SPeter Wemm bool logit; 1745c2aa98e2SPeter Wemm { 1746c2aa98e2SPeter Wemm register int fd; 1747c2aa98e2SPeter Wemm extern int DtableSize; 1748c2aa98e2SPeter Wemm 1749c2aa98e2SPeter Wemm for (fd = 0; fd < DtableSize; fd++) 175040266059SGregory Neil Shapiro dumpfd(fd, false, logit); 1751c2aa98e2SPeter Wemm } 175240266059SGregory Neil Shapiro /* 1753c2aa98e2SPeter Wemm ** DUMPFD -- dump a file descriptor 1754c2aa98e2SPeter Wemm ** 1755c2aa98e2SPeter Wemm ** Parameters: 1756c2aa98e2SPeter Wemm ** fd -- the file descriptor to dump. 1757c2aa98e2SPeter Wemm ** printclosed -- if set, print a notification even if 1758c2aa98e2SPeter Wemm ** it is closed; otherwise print nothing. 1759e92d3f3fSGregory Neil Shapiro ** logit -- if set, use sm_syslog instead of sm_dprintf() 176040266059SGregory Neil Shapiro ** 176140266059SGregory Neil Shapiro ** Returns: 176240266059SGregory Neil Shapiro ** none. 1763c2aa98e2SPeter Wemm */ 1764c2aa98e2SPeter Wemm 1765c2aa98e2SPeter Wemm void 1766c2aa98e2SPeter Wemm dumpfd(fd, printclosed, logit) 1767c2aa98e2SPeter Wemm int fd; 1768c2aa98e2SPeter Wemm bool printclosed; 1769c2aa98e2SPeter Wemm bool logit; 1770c2aa98e2SPeter Wemm { 1771c2aa98e2SPeter Wemm register char *p; 1772c2aa98e2SPeter Wemm char *hp; 1773c2aa98e2SPeter Wemm #ifdef S_IFSOCK 1774c2aa98e2SPeter Wemm SOCKADDR sa; 177506f25ae9SGregory Neil Shapiro #endif /* S_IFSOCK */ 1776c2aa98e2SPeter Wemm auto SOCKADDR_LEN_T slen; 1777c2aa98e2SPeter Wemm int i; 1778c2aa98e2SPeter Wemm #if STAT64 > 0 1779c2aa98e2SPeter Wemm struct stat64 st; 178006f25ae9SGregory Neil Shapiro #else /* STAT64 > 0 */ 1781c2aa98e2SPeter Wemm struct stat st; 178206f25ae9SGregory Neil Shapiro #endif /* STAT64 > 0 */ 1783c2aa98e2SPeter Wemm char buf[200]; 1784c2aa98e2SPeter Wemm 1785c2aa98e2SPeter Wemm p = buf; 178640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "%3d: ", fd); 1787c2aa98e2SPeter Wemm p += strlen(p); 1788c2aa98e2SPeter Wemm 1789c2aa98e2SPeter Wemm if ( 1790c2aa98e2SPeter Wemm #if STAT64 > 0 1791c2aa98e2SPeter Wemm fstat64(fd, &st) 179206f25ae9SGregory Neil Shapiro #else /* STAT64 > 0 */ 1793c2aa98e2SPeter Wemm fstat(fd, &st) 179406f25ae9SGregory Neil Shapiro #endif /* STAT64 > 0 */ 1795c2aa98e2SPeter Wemm < 0) 1796c2aa98e2SPeter Wemm { 1797c2aa98e2SPeter Wemm if (errno != EBADF) 1798c2aa98e2SPeter Wemm { 179940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 180040266059SGregory Neil Shapiro "CANNOT STAT (%s)", 180140266059SGregory Neil Shapiro sm_errstring(errno)); 1802c2aa98e2SPeter Wemm goto printit; 1803c2aa98e2SPeter Wemm } 1804c2aa98e2SPeter Wemm else if (printclosed) 1805c2aa98e2SPeter Wemm { 180640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CLOSED"); 1807c2aa98e2SPeter Wemm goto printit; 1808c2aa98e2SPeter Wemm } 1809c2aa98e2SPeter Wemm return; 1810c2aa98e2SPeter Wemm } 1811c2aa98e2SPeter Wemm 1812605302a5SGregory Neil Shapiro i = fcntl(fd, F_GETFL, 0); 1813c2aa98e2SPeter Wemm if (i != -1) 1814c2aa98e2SPeter Wemm { 181540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "fl=0x%x, ", i); 1816c2aa98e2SPeter Wemm p += strlen(p); 1817c2aa98e2SPeter Wemm } 1818c2aa98e2SPeter Wemm 181940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "mode=%o: ", 182040266059SGregory Neil Shapiro (int) st.st_mode); 1821c2aa98e2SPeter Wemm p += strlen(p); 1822c2aa98e2SPeter Wemm switch (st.st_mode & S_IFMT) 1823c2aa98e2SPeter Wemm { 1824c2aa98e2SPeter Wemm #ifdef S_IFSOCK 1825c2aa98e2SPeter Wemm case S_IFSOCK: 182640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "SOCK "); 1827c2aa98e2SPeter Wemm p += strlen(p); 182806f25ae9SGregory Neil Shapiro memset(&sa, '\0', sizeof sa); 1829c2aa98e2SPeter Wemm slen = sizeof sa; 1830c2aa98e2SPeter Wemm if (getsockname(fd, &sa.sa, &slen) < 0) 183140266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)", 183240266059SGregory Neil Shapiro sm_errstring(errno)); 1833c2aa98e2SPeter Wemm else 1834c2aa98e2SPeter Wemm { 1835c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa); 183606f25ae9SGregory Neil Shapiro if (hp == NULL) 183706f25ae9SGregory Neil Shapiro { 183806f25ae9SGregory Neil Shapiro /* EMPTY */ 183906f25ae9SGregory Neil Shapiro /* do nothing */ 184006f25ae9SGregory Neil Shapiro } 184106f25ae9SGregory Neil Shapiro # if NETINET 184206f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET) 184340266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 184440266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port)); 184506f25ae9SGregory Neil Shapiro # endif /* NETINET */ 184606f25ae9SGregory Neil Shapiro # if NETINET6 184706f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6) 184840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 184940266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port)); 185006f25ae9SGregory Neil Shapiro # endif /* NETINET6 */ 1851c2aa98e2SPeter Wemm else 185240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 185340266059SGregory Neil Shapiro "%s", hp); 1854c2aa98e2SPeter Wemm } 1855c2aa98e2SPeter Wemm p += strlen(p); 185640266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "->"); 1857c2aa98e2SPeter Wemm p += strlen(p); 1858c2aa98e2SPeter Wemm slen = sizeof sa; 1859c2aa98e2SPeter Wemm if (getpeername(fd, &sa.sa, &slen) < 0) 186040266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "(%s)", 186140266059SGregory Neil Shapiro sm_errstring(errno)); 1862c2aa98e2SPeter Wemm else 1863c2aa98e2SPeter Wemm { 1864c2aa98e2SPeter Wemm hp = hostnamebyanyaddr(&sa); 186506f25ae9SGregory Neil Shapiro if (hp == NULL) 186606f25ae9SGregory Neil Shapiro { 186706f25ae9SGregory Neil Shapiro /* EMPTY */ 186806f25ae9SGregory Neil Shapiro /* do nothing */ 186906f25ae9SGregory Neil Shapiro } 187006f25ae9SGregory Neil Shapiro # if NETINET 187106f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET) 187240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 187340266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin.sin_port)); 187406f25ae9SGregory Neil Shapiro # endif /* NETINET */ 187506f25ae9SGregory Neil Shapiro # if NETINET6 187606f25ae9SGregory Neil Shapiro else if (sa.sa.sa_family == AF_INET6) 187740266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 187840266059SGregory Neil Shapiro "%s/%d", hp, ntohs(sa.sin6.sin6_port)); 187906f25ae9SGregory Neil Shapiro # endif /* NETINET6 */ 1880c2aa98e2SPeter Wemm else 188140266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 188240266059SGregory Neil Shapiro "%s", hp); 1883c2aa98e2SPeter Wemm } 1884c2aa98e2SPeter Wemm break; 188506f25ae9SGregory Neil Shapiro #endif /* S_IFSOCK */ 1886c2aa98e2SPeter Wemm 1887c2aa98e2SPeter Wemm case S_IFCHR: 188840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "CHR: "); 1889c2aa98e2SPeter Wemm p += strlen(p); 1890c2aa98e2SPeter Wemm goto defprint; 1891c2aa98e2SPeter Wemm 189240266059SGregory Neil Shapiro #ifdef S_IFBLK 1893c2aa98e2SPeter Wemm case S_IFBLK: 189440266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "BLK: "); 1895c2aa98e2SPeter Wemm p += strlen(p); 1896c2aa98e2SPeter Wemm goto defprint; 189740266059SGregory Neil Shapiro #endif /* S_IFBLK */ 1898c2aa98e2SPeter Wemm 1899c2aa98e2SPeter Wemm #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) 1900c2aa98e2SPeter Wemm case S_IFIFO: 190140266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "FIFO: "); 1902c2aa98e2SPeter Wemm p += strlen(p); 1903c2aa98e2SPeter Wemm goto defprint; 190406f25ae9SGregory Neil Shapiro #endif /* defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) */ 1905c2aa98e2SPeter Wemm 1906c2aa98e2SPeter Wemm #ifdef S_IFDIR 1907c2aa98e2SPeter Wemm case S_IFDIR: 190840266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "DIR: "); 1909c2aa98e2SPeter Wemm p += strlen(p); 1910c2aa98e2SPeter Wemm goto defprint; 191106f25ae9SGregory Neil Shapiro #endif /* S_IFDIR */ 1912c2aa98e2SPeter Wemm 1913c2aa98e2SPeter Wemm #ifdef S_IFLNK 1914c2aa98e2SPeter Wemm case S_IFLNK: 191540266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "LNK: "); 1916c2aa98e2SPeter Wemm p += strlen(p); 1917c2aa98e2SPeter Wemm goto defprint; 191806f25ae9SGregory Neil Shapiro #endif /* S_IFLNK */ 1919c2aa98e2SPeter Wemm 1920c2aa98e2SPeter Wemm default: 1921c2aa98e2SPeter Wemm defprint: 192240266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), 192340266059SGregory Neil Shapiro "dev=%d/%d, ino=%llu, nlink=%d, u/gid=%d/%d, ", 1924c2aa98e2SPeter Wemm major(st.st_dev), minor(st.st_dev), 192540266059SGregory Neil Shapiro (ULONGLONG_T) st.st_ino, 192606f25ae9SGregory Neil Shapiro (int) st.st_nlink, (int) st.st_uid, 192706f25ae9SGregory Neil Shapiro (int) st.st_gid); 192840266059SGregory Neil Shapiro p += strlen(p); 192940266059SGregory Neil Shapiro (void) sm_snprintf(p, SPACELEFT(buf, p), "size=%llu", 193040266059SGregory Neil Shapiro (ULONGLONG_T) st.st_size); 1931c2aa98e2SPeter Wemm break; 1932c2aa98e2SPeter Wemm } 1933c2aa98e2SPeter Wemm 1934c2aa98e2SPeter Wemm printit: 1935c2aa98e2SPeter Wemm if (logit) 1936c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv ? CurEnv->e_id : NULL, 1937c2aa98e2SPeter Wemm "%.800s", buf); 1938c2aa98e2SPeter Wemm else 1939e92d3f3fSGregory Neil Shapiro sm_dprintf("%s\n", buf); 1940c2aa98e2SPeter Wemm } 194140266059SGregory Neil Shapiro /* 1942c2aa98e2SPeter Wemm ** SHORTEN_HOSTNAME -- strip local domain information off of hostname. 1943c2aa98e2SPeter Wemm ** 1944c2aa98e2SPeter Wemm ** Parameters: 1945c2aa98e2SPeter Wemm ** host -- the host to shorten (stripped in place). 1946c2aa98e2SPeter Wemm ** 1947c2aa98e2SPeter Wemm ** Returns: 194840266059SGregory Neil Shapiro ** place where string was truncated, NULL if not truncated. 1949c2aa98e2SPeter Wemm */ 1950c2aa98e2SPeter Wemm 1951602a2b1bSGregory Neil Shapiro char * 1952c2aa98e2SPeter Wemm shorten_hostname(host) 1953c2aa98e2SPeter Wemm char host[]; 1954c2aa98e2SPeter Wemm { 1955c2aa98e2SPeter Wemm register char *p; 1956c2aa98e2SPeter Wemm char *mydom; 1957c2aa98e2SPeter Wemm int i; 195840266059SGregory Neil Shapiro bool canon = false; 1959c2aa98e2SPeter Wemm 1960c2aa98e2SPeter Wemm /* strip off final dot */ 196140266059SGregory Neil Shapiro i = strlen(host); 196240266059SGregory Neil Shapiro p = &host[(i == 0) ? 0 : i - 1]; 1963c2aa98e2SPeter Wemm if (*p == '.') 1964c2aa98e2SPeter Wemm { 1965c2aa98e2SPeter Wemm *p = '\0'; 196640266059SGregory Neil Shapiro canon = true; 1967c2aa98e2SPeter Wemm } 1968c2aa98e2SPeter Wemm 1969c2aa98e2SPeter Wemm /* see if there is any domain at all -- if not, we are done */ 1970c2aa98e2SPeter Wemm p = strchr(host, '.'); 1971c2aa98e2SPeter Wemm if (p == NULL) 1972602a2b1bSGregory Neil Shapiro return NULL; 1973c2aa98e2SPeter Wemm 1974c2aa98e2SPeter Wemm /* yes, we have a domain -- see if it looks like us */ 1975c2aa98e2SPeter Wemm mydom = macvalue('m', CurEnv); 1976c2aa98e2SPeter Wemm if (mydom == NULL) 1977c2aa98e2SPeter Wemm mydom = ""; 1978c2aa98e2SPeter Wemm i = strlen(++p); 197940266059SGregory Neil Shapiro if ((canon ? sm_strcasecmp(p, mydom) 198040266059SGregory Neil Shapiro : sm_strncasecmp(p, mydom, i)) == 0 && 1981c2aa98e2SPeter Wemm (mydom[i] == '.' || mydom[i] == '\0')) 1982602a2b1bSGregory Neil Shapiro { 1983c2aa98e2SPeter Wemm *--p = '\0'; 1984602a2b1bSGregory Neil Shapiro return p; 1985602a2b1bSGregory Neil Shapiro } 1986602a2b1bSGregory Neil Shapiro return NULL; 1987c2aa98e2SPeter Wemm } 198840266059SGregory Neil Shapiro /* 1989c2aa98e2SPeter Wemm ** PROG_OPEN -- open a program for reading 1990c2aa98e2SPeter Wemm ** 1991c2aa98e2SPeter Wemm ** Parameters: 1992c2aa98e2SPeter Wemm ** argv -- the argument list. 1993c2aa98e2SPeter Wemm ** pfd -- pointer to a place to store the file descriptor. 1994c2aa98e2SPeter Wemm ** e -- the current envelope. 1995c2aa98e2SPeter Wemm ** 1996c2aa98e2SPeter Wemm ** Returns: 1997c2aa98e2SPeter Wemm ** pid of the process -- -1 if it failed. 1998c2aa98e2SPeter Wemm */ 1999c2aa98e2SPeter Wemm 20008774250cSGregory Neil Shapiro pid_t 2001c2aa98e2SPeter Wemm prog_open(argv, pfd, e) 2002c2aa98e2SPeter Wemm char **argv; 2003c2aa98e2SPeter Wemm int *pfd; 2004c2aa98e2SPeter Wemm ENVELOPE *e; 2005c2aa98e2SPeter Wemm { 20068774250cSGregory Neil Shapiro pid_t pid; 200706f25ae9SGregory Neil Shapiro int save_errno; 200840266059SGregory Neil Shapiro int sff; 200940266059SGregory Neil Shapiro int ret; 2010c2aa98e2SPeter Wemm int fdv[2]; 2011c2aa98e2SPeter Wemm char *p, *q; 201294c01205SGregory Neil Shapiro char buf[MAXPATHLEN]; 2013c2aa98e2SPeter Wemm extern int DtableSize; 2014c2aa98e2SPeter Wemm 2015c2aa98e2SPeter Wemm if (pipe(fdv) < 0) 2016c2aa98e2SPeter Wemm { 2017c2aa98e2SPeter Wemm syserr("%s: cannot create pipe for stdout", argv[0]); 2018c2aa98e2SPeter Wemm return -1; 2019c2aa98e2SPeter Wemm } 2020c2aa98e2SPeter Wemm pid = fork(); 2021c2aa98e2SPeter Wemm if (pid < 0) 2022c2aa98e2SPeter Wemm { 2023c2aa98e2SPeter Wemm syserr("%s: cannot fork", argv[0]); 202406f25ae9SGregory Neil Shapiro (void) close(fdv[0]); 202506f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2026c2aa98e2SPeter Wemm return -1; 2027c2aa98e2SPeter Wemm } 2028c2aa98e2SPeter Wemm if (pid > 0) 2029c2aa98e2SPeter Wemm { 2030c2aa98e2SPeter Wemm /* parent */ 203106f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2032c2aa98e2SPeter Wemm *pfd = fdv[0]; 2033c2aa98e2SPeter Wemm return pid; 2034c2aa98e2SPeter Wemm } 2035c2aa98e2SPeter Wemm 20368774250cSGregory Neil Shapiro /* Reset global flags */ 20378774250cSGregory Neil Shapiro RestartRequest = NULL; 203840266059SGregory Neil Shapiro RestartWorkGroup = false; 20398774250cSGregory Neil Shapiro ShutdownRequest = NULL; 20408774250cSGregory Neil Shapiro PendingSignal = 0; 204140266059SGregory Neil Shapiro CurrentPid = getpid(); 204240266059SGregory Neil Shapiro 204340266059SGregory Neil Shapiro /* 204440266059SGregory Neil Shapiro ** Initialize exception stack and default exception 204540266059SGregory Neil Shapiro ** handler for child process. 204640266059SGregory Neil Shapiro */ 204740266059SGregory Neil Shapiro 204840266059SGregory Neil Shapiro sm_exc_newthread(fatal_error); 204940266059SGregory Neil Shapiro 205040266059SGregory Neil Shapiro /* child -- close stdin */ 205140266059SGregory Neil Shapiro (void) close(0); 20528774250cSGregory Neil Shapiro 2053c2aa98e2SPeter Wemm /* stdout goes back to parent */ 205406f25ae9SGregory Neil Shapiro (void) close(fdv[0]); 2055c2aa98e2SPeter Wemm if (dup2(fdv[1], 1) < 0) 2056c2aa98e2SPeter Wemm { 2057c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stdout", argv[0]); 2058c2aa98e2SPeter Wemm _exit(EX_OSERR); 2059c2aa98e2SPeter Wemm } 206006f25ae9SGregory Neil Shapiro (void) close(fdv[1]); 2061c2aa98e2SPeter Wemm 2062c2aa98e2SPeter Wemm /* stderr goes to transcript if available */ 2063c2aa98e2SPeter Wemm if (e->e_xfp != NULL) 2064c2aa98e2SPeter Wemm { 206506f25ae9SGregory Neil Shapiro int xfd; 206606f25ae9SGregory Neil Shapiro 206740266059SGregory Neil Shapiro xfd = sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL); 206806f25ae9SGregory Neil Shapiro if (xfd >= 0 && dup2(xfd, 2) < 0) 2069c2aa98e2SPeter Wemm { 2070c2aa98e2SPeter Wemm syserr("%s: cannot dup2 for stderr", argv[0]); 2071c2aa98e2SPeter Wemm _exit(EX_OSERR); 2072c2aa98e2SPeter Wemm } 2073c2aa98e2SPeter Wemm } 2074c2aa98e2SPeter Wemm 2075c2aa98e2SPeter Wemm /* this process has no right to the queue file */ 2076c2aa98e2SPeter Wemm if (e->e_lockfp != NULL) 207740266059SGregory Neil Shapiro (void) close(sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL)); 207806f25ae9SGregory Neil Shapiro 207906f25ae9SGregory Neil Shapiro /* chroot to the program mailer directory, if defined */ 208006f25ae9SGregory Neil Shapiro if (ProgMailer != NULL && ProgMailer->m_rootdir != NULL) 208106f25ae9SGregory Neil Shapiro { 208206f25ae9SGregory Neil Shapiro expand(ProgMailer->m_rootdir, buf, sizeof buf, e); 208306f25ae9SGregory Neil Shapiro if (chroot(buf) < 0) 208406f25ae9SGregory Neil Shapiro { 208506f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chroot(%s)", buf); 208606f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 208706f25ae9SGregory Neil Shapiro } 208806f25ae9SGregory Neil Shapiro if (chdir("/") < 0) 208906f25ae9SGregory Neil Shapiro { 209006f25ae9SGregory Neil Shapiro syserr("prog_open: cannot chdir(/)"); 209106f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 209206f25ae9SGregory Neil Shapiro } 209306f25ae9SGregory Neil Shapiro } 2094c2aa98e2SPeter Wemm 2095c2aa98e2SPeter Wemm /* run as default user */ 2096c2aa98e2SPeter Wemm endpwent(); 209740266059SGregory Neil Shapiro sm_mbdb_terminate(); 2098c2aa98e2SPeter Wemm if (setgid(DefGid) < 0 && geteuid() == 0) 209906f25ae9SGregory Neil Shapiro { 2100c2aa98e2SPeter Wemm syserr("prog_open: setgid(%ld) failed", (long) DefGid); 210106f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 210206f25ae9SGregory Neil Shapiro } 2103c2aa98e2SPeter Wemm if (setuid(DefUid) < 0 && geteuid() == 0) 210406f25ae9SGregory Neil Shapiro { 2105c2aa98e2SPeter Wemm syserr("prog_open: setuid(%ld) failed", (long) DefUid); 210606f25ae9SGregory Neil Shapiro exit(EX_TEMPFAIL); 210706f25ae9SGregory Neil Shapiro } 2108c2aa98e2SPeter Wemm 2109c2aa98e2SPeter Wemm /* run in some directory */ 2110c2aa98e2SPeter Wemm if (ProgMailer != NULL) 2111c2aa98e2SPeter Wemm p = ProgMailer->m_execdir; 2112c2aa98e2SPeter Wemm else 2113c2aa98e2SPeter Wemm p = NULL; 2114c2aa98e2SPeter Wemm for (; p != NULL; p = q) 2115c2aa98e2SPeter Wemm { 2116c2aa98e2SPeter Wemm q = strchr(p, ':'); 2117c2aa98e2SPeter Wemm if (q != NULL) 2118c2aa98e2SPeter Wemm *q = '\0'; 2119c2aa98e2SPeter Wemm expand(p, buf, sizeof buf, e); 2120c2aa98e2SPeter Wemm if (q != NULL) 2121c2aa98e2SPeter Wemm *q++ = ':'; 2122c2aa98e2SPeter Wemm if (buf[0] != '\0' && chdir(buf) >= 0) 2123c2aa98e2SPeter Wemm break; 2124c2aa98e2SPeter Wemm } 2125c2aa98e2SPeter Wemm if (p == NULL) 2126c2aa98e2SPeter Wemm { 2127c2aa98e2SPeter Wemm /* backup directories */ 2128c2aa98e2SPeter Wemm if (chdir("/tmp") < 0) 2129c2aa98e2SPeter Wemm (void) chdir("/"); 2130c2aa98e2SPeter Wemm } 2131c2aa98e2SPeter Wemm 213240266059SGregory Neil Shapiro /* Check safety of program to be run */ 213340266059SGregory Neil Shapiro sff = SFF_ROOTOK|SFF_EXECOK; 213440266059SGregory Neil Shapiro if (!bitnset(DBS_RUNWRITABLEPROGRAM, DontBlameSendmail)) 213540266059SGregory Neil Shapiro sff |= SFF_NOGWFILES|SFF_NOWWFILES; 213640266059SGregory Neil Shapiro if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH, DontBlameSendmail)) 213740266059SGregory Neil Shapiro sff |= SFF_NOPATHCHECK; 213840266059SGregory Neil Shapiro else 213940266059SGregory Neil Shapiro sff |= SFF_SAFEDIRPATH; 214040266059SGregory Neil Shapiro ret = safefile(argv[0], DefUid, DefGid, DefUser, sff, 0, NULL); 214140266059SGregory Neil Shapiro if (ret != 0) 214240266059SGregory Neil Shapiro sm_syslog(LOG_INFO, e->e_id, 214340266059SGregory Neil Shapiro "Warning: prog_open: program %s unsafe: %s", 214440266059SGregory Neil Shapiro argv[0], sm_errstring(ret)); 214540266059SGregory Neil Shapiro 2146c2aa98e2SPeter Wemm /* arrange for all the files to be closed */ 2147e92d3f3fSGregory Neil Shapiro sm_close_on_exec(STDERR_FILENO + 1, DtableSize); 2148c2aa98e2SPeter Wemm 2149c2aa98e2SPeter Wemm /* now exec the process */ 215006f25ae9SGregory Neil Shapiro (void) execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron); 2151c2aa98e2SPeter Wemm 2152c2aa98e2SPeter Wemm /* woops! failed */ 215306f25ae9SGregory Neil Shapiro save_errno = errno; 2154c2aa98e2SPeter Wemm syserr("%s: cannot exec", argv[0]); 215506f25ae9SGregory Neil Shapiro if (transienterror(save_errno)) 2156c2aa98e2SPeter Wemm _exit(EX_OSERR); 2157c2aa98e2SPeter Wemm _exit(EX_CONFIG); 2158c2aa98e2SPeter Wemm return -1; /* avoid compiler warning on IRIX */ 2159c2aa98e2SPeter Wemm } 216040266059SGregory Neil Shapiro /* 2161c2aa98e2SPeter Wemm ** GET_COLUMN -- look up a Column in a line buffer 2162c2aa98e2SPeter Wemm ** 2163c2aa98e2SPeter Wemm ** Parameters: 2164c2aa98e2SPeter Wemm ** line -- the raw text line to search. 2165c2aa98e2SPeter Wemm ** col -- the column number to fetch. 2166c2aa98e2SPeter Wemm ** delim -- the delimiter between columns. If null, 2167c2aa98e2SPeter Wemm ** use white space. 2168c2aa98e2SPeter Wemm ** buf -- the output buffer. 2169c2aa98e2SPeter Wemm ** buflen -- the length of buf. 2170c2aa98e2SPeter Wemm ** 2171c2aa98e2SPeter Wemm ** Returns: 2172c2aa98e2SPeter Wemm ** buf if successful. 2173c2aa98e2SPeter Wemm ** NULL otherwise. 2174c2aa98e2SPeter Wemm */ 2175c2aa98e2SPeter Wemm 2176c2aa98e2SPeter Wemm char * 2177c2aa98e2SPeter Wemm get_column(line, col, delim, buf, buflen) 2178c2aa98e2SPeter Wemm char line[]; 2179c2aa98e2SPeter Wemm int col; 218006f25ae9SGregory Neil Shapiro int delim; 2181c2aa98e2SPeter Wemm char buf[]; 2182c2aa98e2SPeter Wemm int buflen; 2183c2aa98e2SPeter Wemm { 2184c2aa98e2SPeter Wemm char *p; 2185c2aa98e2SPeter Wemm char *begin, *end; 2186c2aa98e2SPeter Wemm int i; 2187c2aa98e2SPeter Wemm char delimbuf[4]; 2188c2aa98e2SPeter Wemm 218906f25ae9SGregory Neil Shapiro if ((char) delim == '\0') 219040266059SGregory Neil Shapiro (void) sm_strlcpy(delimbuf, "\n\t ", sizeof delimbuf); 2191c2aa98e2SPeter Wemm else 2192c2aa98e2SPeter Wemm { 219306f25ae9SGregory Neil Shapiro delimbuf[0] = (char) delim; 2194c2aa98e2SPeter Wemm delimbuf[1] = '\0'; 2195c2aa98e2SPeter Wemm } 2196c2aa98e2SPeter Wemm 2197c2aa98e2SPeter Wemm p = line; 2198c2aa98e2SPeter Wemm if (*p == '\0') 2199c2aa98e2SPeter Wemm return NULL; /* line empty */ 220006f25ae9SGregory Neil Shapiro if (*p == (char) delim && col == 0) 2201c2aa98e2SPeter Wemm return NULL; /* first column empty */ 2202c2aa98e2SPeter Wemm 2203c2aa98e2SPeter Wemm begin = line; 2204c2aa98e2SPeter Wemm 220506f25ae9SGregory Neil Shapiro if (col == 0 && (char) delim == '\0') 2206c2aa98e2SPeter Wemm { 2207c2aa98e2SPeter Wemm while (*begin != '\0' && isascii(*begin) && isspace(*begin)) 2208c2aa98e2SPeter Wemm begin++; 2209c2aa98e2SPeter Wemm } 2210c2aa98e2SPeter Wemm 2211c2aa98e2SPeter Wemm for (i = 0; i < col; i++) 2212c2aa98e2SPeter Wemm { 2213c2aa98e2SPeter Wemm if ((begin = strpbrk(begin, delimbuf)) == NULL) 2214c2aa98e2SPeter Wemm return NULL; /* no such column */ 2215c2aa98e2SPeter Wemm begin++; 221606f25ae9SGregory Neil Shapiro if ((char) delim == '\0') 2217c2aa98e2SPeter Wemm { 2218c2aa98e2SPeter Wemm while (*begin != '\0' && isascii(*begin) && isspace(*begin)) 2219c2aa98e2SPeter Wemm begin++; 2220c2aa98e2SPeter Wemm } 2221c2aa98e2SPeter Wemm } 2222c2aa98e2SPeter Wemm 2223c2aa98e2SPeter Wemm end = strpbrk(begin, delimbuf); 2224c2aa98e2SPeter Wemm if (end == NULL) 2225c2aa98e2SPeter Wemm i = strlen(begin); 2226c2aa98e2SPeter Wemm else 2227c2aa98e2SPeter Wemm i = end - begin; 2228c2aa98e2SPeter Wemm if (i >= buflen) 2229c2aa98e2SPeter Wemm i = buflen - 1; 223040266059SGregory Neil Shapiro (void) sm_strlcpy(buf, begin, i + 1); 2231c2aa98e2SPeter Wemm return buf; 2232c2aa98e2SPeter Wemm } 223340266059SGregory Neil Shapiro /* 2234c2aa98e2SPeter Wemm ** CLEANSTRCPY -- copy string keeping out bogus characters 2235c2aa98e2SPeter Wemm ** 2236c2aa98e2SPeter Wemm ** Parameters: 2237c2aa98e2SPeter Wemm ** t -- "to" string. 2238c2aa98e2SPeter Wemm ** f -- "from" string. 2239c2aa98e2SPeter Wemm ** l -- length of space available in "to" string. 2240c2aa98e2SPeter Wemm ** 2241c2aa98e2SPeter Wemm ** Returns: 2242c2aa98e2SPeter Wemm ** none. 2243c2aa98e2SPeter Wemm */ 2244c2aa98e2SPeter Wemm 2245c2aa98e2SPeter Wemm void 2246c2aa98e2SPeter Wemm cleanstrcpy(t, f, l) 2247c2aa98e2SPeter Wemm register char *t; 2248c2aa98e2SPeter Wemm register char *f; 2249c2aa98e2SPeter Wemm int l; 2250c2aa98e2SPeter Wemm { 2251c2aa98e2SPeter Wemm /* check for newlines and log if necessary */ 225240266059SGregory Neil Shapiro (void) denlstring(f, true, true); 2253c2aa98e2SPeter Wemm 225406f25ae9SGregory Neil Shapiro if (l <= 0) 225506f25ae9SGregory Neil Shapiro syserr("!cleanstrcpy: length == 0"); 225606f25ae9SGregory Neil Shapiro 2257c2aa98e2SPeter Wemm l--; 2258c2aa98e2SPeter Wemm while (l > 0 && *f != '\0') 2259c2aa98e2SPeter Wemm { 2260c2aa98e2SPeter Wemm if (isascii(*f) && 2261c2aa98e2SPeter Wemm (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL)) 2262c2aa98e2SPeter Wemm { 2263c2aa98e2SPeter Wemm l--; 2264c2aa98e2SPeter Wemm *t++ = *f; 2265c2aa98e2SPeter Wemm } 2266c2aa98e2SPeter Wemm f++; 2267c2aa98e2SPeter Wemm } 2268c2aa98e2SPeter Wemm *t = '\0'; 2269c2aa98e2SPeter Wemm } 227040266059SGregory Neil Shapiro /* 2271c2aa98e2SPeter Wemm ** DENLSTRING -- convert newlines in a string to spaces 2272c2aa98e2SPeter Wemm ** 2273c2aa98e2SPeter Wemm ** Parameters: 2274c2aa98e2SPeter Wemm ** s -- the input string 2275c2aa98e2SPeter Wemm ** strict -- if set, don't permit continuation lines. 2276c2aa98e2SPeter Wemm ** logattacks -- if set, log attempted attacks. 2277c2aa98e2SPeter Wemm ** 2278c2aa98e2SPeter Wemm ** Returns: 2279c2aa98e2SPeter Wemm ** A pointer to a version of the string with newlines 2280c2aa98e2SPeter Wemm ** mapped to spaces. This should be copied. 2281c2aa98e2SPeter Wemm */ 2282c2aa98e2SPeter Wemm 2283c2aa98e2SPeter Wemm char * 2284c2aa98e2SPeter Wemm denlstring(s, strict, logattacks) 2285c2aa98e2SPeter Wemm char *s; 2286c2aa98e2SPeter Wemm bool strict; 2287c2aa98e2SPeter Wemm bool logattacks; 2288c2aa98e2SPeter Wemm { 2289c2aa98e2SPeter Wemm register char *p; 2290c2aa98e2SPeter Wemm int l; 2291c2aa98e2SPeter Wemm static char *bp = NULL; 2292c2aa98e2SPeter Wemm static int bl = 0; 2293c2aa98e2SPeter Wemm 2294c2aa98e2SPeter Wemm p = s; 2295c2aa98e2SPeter Wemm while ((p = strchr(p, '\n')) != NULL) 2296c2aa98e2SPeter Wemm if (strict || (*++p != ' ' && *p != '\t')) 2297c2aa98e2SPeter Wemm break; 2298c2aa98e2SPeter Wemm if (p == NULL) 2299c2aa98e2SPeter Wemm return s; 2300c2aa98e2SPeter Wemm 2301c2aa98e2SPeter Wemm l = strlen(s) + 1; 2302c2aa98e2SPeter Wemm if (bl < l) 2303c2aa98e2SPeter Wemm { 2304c2aa98e2SPeter Wemm /* allocate more space */ 230540266059SGregory Neil Shapiro char *nbp = sm_pmalloc_x(l); 230640266059SGregory Neil Shapiro 2307c2aa98e2SPeter Wemm if (bp != NULL) 23088774250cSGregory Neil Shapiro sm_free(bp); 230940266059SGregory Neil Shapiro bp = nbp; 2310c2aa98e2SPeter Wemm bl = l; 2311c2aa98e2SPeter Wemm } 231240266059SGregory Neil Shapiro (void) sm_strlcpy(bp, s, l); 2313c2aa98e2SPeter Wemm for (p = bp; (p = strchr(p, '\n')) != NULL; ) 2314c2aa98e2SPeter Wemm *p++ = ' '; 2315c2aa98e2SPeter Wemm 2316c2aa98e2SPeter Wemm if (logattacks) 2317c2aa98e2SPeter Wemm { 2318c2aa98e2SPeter Wemm sm_syslog(LOG_NOTICE, CurEnv->e_id, 2319c2aa98e2SPeter Wemm "POSSIBLE ATTACK from %.100s: newline in string \"%s\"", 2320c2aa98e2SPeter Wemm RealHostName == NULL ? "[UNKNOWN]" : RealHostName, 2321c2aa98e2SPeter Wemm shortenstring(bp, MAXSHORTSTR)); 2322c2aa98e2SPeter Wemm } 2323c2aa98e2SPeter Wemm 2324c2aa98e2SPeter Wemm return bp; 2325c2aa98e2SPeter Wemm } 2326739ac4d4SGregory Neil Shapiro 2327739ac4d4SGregory Neil Shapiro /* 2328739ac4d4SGregory Neil Shapiro ** STRREPLNONPRT -- replace "unprintable" characters in a string with subst 2329739ac4d4SGregory Neil Shapiro ** 2330739ac4d4SGregory Neil Shapiro ** Parameters: 2331739ac4d4SGregory Neil Shapiro ** s -- string to manipulate (in place) 2332739ac4d4SGregory Neil Shapiro ** subst -- character to use as replacement 2333739ac4d4SGregory Neil Shapiro ** 2334739ac4d4SGregory Neil Shapiro ** Returns: 2335739ac4d4SGregory Neil Shapiro ** true iff string did not contain "unprintable" characters 2336739ac4d4SGregory Neil Shapiro */ 2337739ac4d4SGregory Neil Shapiro 2338739ac4d4SGregory Neil Shapiro bool 2339739ac4d4SGregory Neil Shapiro strreplnonprt(s, c) 2340739ac4d4SGregory Neil Shapiro char *s; 2341739ac4d4SGregory Neil Shapiro int c; 2342739ac4d4SGregory Neil Shapiro { 2343739ac4d4SGregory Neil Shapiro bool ok; 2344739ac4d4SGregory Neil Shapiro 2345739ac4d4SGregory Neil Shapiro ok = true; 2346739ac4d4SGregory Neil Shapiro if (s == NULL) 2347739ac4d4SGregory Neil Shapiro return ok; 2348739ac4d4SGregory Neil Shapiro while (*s != '\0') 2349739ac4d4SGregory Neil Shapiro { 2350739ac4d4SGregory Neil Shapiro if (!(isascii(*s) && isprint(*s))) 2351739ac4d4SGregory Neil Shapiro { 2352739ac4d4SGregory Neil Shapiro *s = c; 2353739ac4d4SGregory Neil Shapiro ok = false; 2354739ac4d4SGregory Neil Shapiro } 2355739ac4d4SGregory Neil Shapiro ++s; 2356739ac4d4SGregory Neil Shapiro } 2357739ac4d4SGregory Neil Shapiro return ok; 2358739ac4d4SGregory Neil Shapiro } 2359739ac4d4SGregory Neil Shapiro 236040266059SGregory Neil Shapiro /* 236140266059SGregory Neil Shapiro ** STR2PRT -- convert "unprintable" characters in a string to \oct 236240266059SGregory Neil Shapiro ** 236340266059SGregory Neil Shapiro ** Parameters: 236440266059SGregory Neil Shapiro ** s -- string to convert 236540266059SGregory Neil Shapiro ** 236640266059SGregory Neil Shapiro ** Returns: 236740266059SGregory Neil Shapiro ** converted string. 236840266059SGregory Neil Shapiro ** This is a static local buffer, string must be copied 236940266059SGregory Neil Shapiro ** before this function is called again! 237040266059SGregory Neil Shapiro */ 237140266059SGregory Neil Shapiro 237240266059SGregory Neil Shapiro char * 237340266059SGregory Neil Shapiro str2prt(s) 237440266059SGregory Neil Shapiro char *s; 237540266059SGregory Neil Shapiro { 237640266059SGregory Neil Shapiro int l; 237740266059SGregory Neil Shapiro char c, *h; 237840266059SGregory Neil Shapiro bool ok; 237940266059SGregory Neil Shapiro static int len = 0; 238040266059SGregory Neil Shapiro static char *buf = NULL; 238140266059SGregory Neil Shapiro 238240266059SGregory Neil Shapiro if (s == NULL) 238340266059SGregory Neil Shapiro return NULL; 238440266059SGregory Neil Shapiro ok = true; 238540266059SGregory Neil Shapiro for (h = s, l = 1; *h != '\0'; h++, l++) 238640266059SGregory Neil Shapiro { 238740266059SGregory Neil Shapiro if (*h == '\\') 238840266059SGregory Neil Shapiro { 238940266059SGregory Neil Shapiro ++l; 239040266059SGregory Neil Shapiro ok = false; 239140266059SGregory Neil Shapiro } 239240266059SGregory Neil Shapiro else if (!(isascii(*h) && isprint(*h))) 239340266059SGregory Neil Shapiro { 239440266059SGregory Neil Shapiro l += 3; 239540266059SGregory Neil Shapiro ok = false; 239640266059SGregory Neil Shapiro } 239740266059SGregory Neil Shapiro } 239840266059SGregory Neil Shapiro if (ok) 239940266059SGregory Neil Shapiro return s; 240040266059SGregory Neil Shapiro if (l > len) 240140266059SGregory Neil Shapiro { 240240266059SGregory Neil Shapiro char *nbuf = sm_pmalloc_x(l); 240340266059SGregory Neil Shapiro 240440266059SGregory Neil Shapiro if (buf != NULL) 240540266059SGregory Neil Shapiro sm_free(buf); 240640266059SGregory Neil Shapiro len = l; 240740266059SGregory Neil Shapiro buf = nbuf; 240840266059SGregory Neil Shapiro } 240940266059SGregory Neil Shapiro for (h = buf; *s != '\0' && l > 0; s++, l--) 241040266059SGregory Neil Shapiro { 241140266059SGregory Neil Shapiro c = *s; 241240266059SGregory Neil Shapiro if (isascii(c) && isprint(c) && c != '\\') 241340266059SGregory Neil Shapiro { 241440266059SGregory Neil Shapiro *h++ = c; 241540266059SGregory Neil Shapiro } 241640266059SGregory Neil Shapiro else 241740266059SGregory Neil Shapiro { 241840266059SGregory Neil Shapiro *h++ = '\\'; 241940266059SGregory Neil Shapiro --l; 242040266059SGregory Neil Shapiro switch (c) 242140266059SGregory Neil Shapiro { 242240266059SGregory Neil Shapiro case '\\': 242340266059SGregory Neil Shapiro *h++ = '\\'; 242440266059SGregory Neil Shapiro break; 242540266059SGregory Neil Shapiro case '\t': 242640266059SGregory Neil Shapiro *h++ = 't'; 242740266059SGregory Neil Shapiro break; 242840266059SGregory Neil Shapiro case '\n': 242940266059SGregory Neil Shapiro *h++ = 'n'; 243040266059SGregory Neil Shapiro break; 243140266059SGregory Neil Shapiro case '\r': 243240266059SGregory Neil Shapiro *h++ = 'r'; 243340266059SGregory Neil Shapiro break; 243440266059SGregory Neil Shapiro default: 2435323f6dcbSGregory Neil Shapiro (void) sm_snprintf(h, l, "%03o", 2436323f6dcbSGregory Neil Shapiro (unsigned int)((unsigned char) c)); 243740266059SGregory Neil Shapiro 243840266059SGregory Neil Shapiro /* 243940266059SGregory Neil Shapiro ** XXX since l is unsigned this may 244040266059SGregory Neil Shapiro ** wrap around if the calculation is screwed 244140266059SGregory Neil Shapiro ** up... 244240266059SGregory Neil Shapiro */ 244340266059SGregory Neil Shapiro 244440266059SGregory Neil Shapiro l -= 2; 244540266059SGregory Neil Shapiro h += 3; 244640266059SGregory Neil Shapiro break; 244740266059SGregory Neil Shapiro } 244840266059SGregory Neil Shapiro } 244940266059SGregory Neil Shapiro } 245040266059SGregory Neil Shapiro *h = '\0'; 245140266059SGregory Neil Shapiro buf[len - 1] = '\0'; 245240266059SGregory Neil Shapiro return buf; 245340266059SGregory Neil Shapiro } 245440266059SGregory Neil Shapiro /* 2455c2aa98e2SPeter Wemm ** PATH_IS_DIR -- check to see if file exists and is a directory. 2456c2aa98e2SPeter Wemm ** 2457c2aa98e2SPeter Wemm ** There are some additional checks for security violations in 2458c2aa98e2SPeter Wemm ** here. This routine is intended to be used for the host status 2459c2aa98e2SPeter Wemm ** support. 2460c2aa98e2SPeter Wemm ** 2461c2aa98e2SPeter Wemm ** Parameters: 2462c2aa98e2SPeter Wemm ** pathname -- pathname to check for directory-ness. 2463c2aa98e2SPeter Wemm ** createflag -- if set, create directory if needed. 2464c2aa98e2SPeter Wemm ** 2465c2aa98e2SPeter Wemm ** Returns: 246640266059SGregory Neil Shapiro ** true -- if the indicated pathname is a directory 246740266059SGregory Neil Shapiro ** false -- otherwise 2468c2aa98e2SPeter Wemm */ 2469c2aa98e2SPeter Wemm 2470a7ec597cSGregory Neil Shapiro bool 2471c2aa98e2SPeter Wemm path_is_dir(pathname, createflag) 2472c2aa98e2SPeter Wemm char *pathname; 2473c2aa98e2SPeter Wemm bool createflag; 2474c2aa98e2SPeter Wemm { 2475c2aa98e2SPeter Wemm struct stat statbuf; 2476c2aa98e2SPeter Wemm 2477c2aa98e2SPeter Wemm #if HASLSTAT 2478c2aa98e2SPeter Wemm if (lstat(pathname, &statbuf) < 0) 247906f25ae9SGregory Neil Shapiro #else /* HASLSTAT */ 2480c2aa98e2SPeter Wemm if (stat(pathname, &statbuf) < 0) 248106f25ae9SGregory Neil Shapiro #endif /* HASLSTAT */ 2482c2aa98e2SPeter Wemm { 2483c2aa98e2SPeter Wemm if (errno != ENOENT || !createflag) 248440266059SGregory Neil Shapiro return false; 2485c2aa98e2SPeter Wemm if (mkdir(pathname, 0755) < 0) 248640266059SGregory Neil Shapiro return false; 248740266059SGregory Neil Shapiro return true; 2488c2aa98e2SPeter Wemm } 2489c2aa98e2SPeter Wemm if (!S_ISDIR(statbuf.st_mode)) 2490c2aa98e2SPeter Wemm { 2491c2aa98e2SPeter Wemm errno = ENOTDIR; 249240266059SGregory Neil Shapiro return false; 2493c2aa98e2SPeter Wemm } 2494c2aa98e2SPeter Wemm 2495c2aa98e2SPeter Wemm /* security: don't allow writable directories */ 2496c2aa98e2SPeter Wemm if (bitset(S_IWGRP|S_IWOTH, statbuf.st_mode)) 2497c2aa98e2SPeter Wemm { 2498c2aa98e2SPeter Wemm errno = EACCES; 249940266059SGregory Neil Shapiro return false; 2500c2aa98e2SPeter Wemm } 250140266059SGregory Neil Shapiro return true; 2502c2aa98e2SPeter Wemm } 250340266059SGregory Neil Shapiro /* 2504c2aa98e2SPeter Wemm ** PROC_LIST_ADD -- add process id to list of our children 2505c2aa98e2SPeter Wemm ** 2506c2aa98e2SPeter Wemm ** Parameters: 2507c2aa98e2SPeter Wemm ** pid -- pid to add to list. 250806f25ae9SGregory Neil Shapiro ** task -- task of pid. 250906f25ae9SGregory Neil Shapiro ** type -- type of process. 251040266059SGregory Neil Shapiro ** count -- number of processes. 251140266059SGregory Neil Shapiro ** other -- other information for this type. 2512c2aa98e2SPeter Wemm ** 2513c2aa98e2SPeter Wemm ** Returns: 2514c2aa98e2SPeter Wemm ** none 251540266059SGregory Neil Shapiro ** 251640266059SGregory Neil Shapiro ** Side Effects: 251740266059SGregory Neil Shapiro ** May increase CurChildren. May grow ProcList. 2518c2aa98e2SPeter Wemm */ 2519c2aa98e2SPeter Wemm 252040266059SGregory Neil Shapiro typedef struct procs PROCS_T; 252140266059SGregory Neil Shapiro 252240266059SGregory Neil Shapiro struct procs 252340266059SGregory Neil Shapiro { 252440266059SGregory Neil Shapiro pid_t proc_pid; 252540266059SGregory Neil Shapiro char *proc_task; 252640266059SGregory Neil Shapiro int proc_type; 252740266059SGregory Neil Shapiro int proc_count; 252840266059SGregory Neil Shapiro int proc_other; 2529e92d3f3fSGregory Neil Shapiro SOCKADDR proc_hostaddr; 253040266059SGregory Neil Shapiro }; 253140266059SGregory Neil Shapiro 253240266059SGregory Neil Shapiro static PROCS_T *volatile ProcListVec = NULL; 2533c2aa98e2SPeter Wemm static int ProcListSize = 0; 2534c2aa98e2SPeter Wemm 2535c2aa98e2SPeter Wemm void 2536e92d3f3fSGregory Neil Shapiro proc_list_add(pid, task, type, count, other, hostaddr) 2537c2aa98e2SPeter Wemm pid_t pid; 2538065a643dSPeter Wemm char *task; 253906f25ae9SGregory Neil Shapiro int type; 254040266059SGregory Neil Shapiro int count; 254140266059SGregory Neil Shapiro int other; 2542e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr; 2543c2aa98e2SPeter Wemm { 2544c2aa98e2SPeter Wemm int i; 2545c2aa98e2SPeter Wemm 2546c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2547c2aa98e2SPeter Wemm { 2548065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2549c2aa98e2SPeter Wemm break; 2550c2aa98e2SPeter Wemm } 2551c2aa98e2SPeter Wemm if (i >= ProcListSize) 2552c2aa98e2SPeter Wemm { 2553c2aa98e2SPeter Wemm /* probe the existing vector to avoid growing infinitely */ 2554c2aa98e2SPeter Wemm proc_list_probe(); 2555c2aa98e2SPeter Wemm 2556c2aa98e2SPeter Wemm /* now scan again */ 2557c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2558c2aa98e2SPeter Wemm { 2559065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2560c2aa98e2SPeter Wemm break; 2561c2aa98e2SPeter Wemm } 2562c2aa98e2SPeter Wemm } 2563c2aa98e2SPeter Wemm if (i >= ProcListSize) 2564c2aa98e2SPeter Wemm { 2565c2aa98e2SPeter Wemm /* grow process list */ 256640266059SGregory Neil Shapiro PROCS_T *npv; 2567c2aa98e2SPeter Wemm 256840266059SGregory Neil Shapiro SM_ASSERT(ProcListSize < INT_MAX - PROC_LIST_SEG); 256940266059SGregory Neil Shapiro npv = (PROCS_T *) sm_pmalloc_x((sizeof *npv) * 257006f25ae9SGregory Neil Shapiro (ProcListSize + PROC_LIST_SEG)); 2571c2aa98e2SPeter Wemm if (ProcListSize > 0) 2572c2aa98e2SPeter Wemm { 257306f25ae9SGregory Neil Shapiro memmove(npv, ProcListVec, 257440266059SGregory Neil Shapiro ProcListSize * sizeof (PROCS_T)); 25758774250cSGregory Neil Shapiro sm_free(ProcListVec); 2576c2aa98e2SPeter Wemm } 257740266059SGregory Neil Shapiro 257840266059SGregory Neil Shapiro /* XXX just use memset() to initialize this part? */ 2579c2aa98e2SPeter Wemm for (i = ProcListSize; i < ProcListSize + PROC_LIST_SEG; i++) 2580065a643dSPeter Wemm { 2581065a643dSPeter Wemm npv[i].proc_pid = NO_PID; 2582065a643dSPeter Wemm npv[i].proc_task = NULL; 258306f25ae9SGregory Neil Shapiro npv[i].proc_type = PROC_NONE; 2584065a643dSPeter Wemm } 2585c2aa98e2SPeter Wemm i = ProcListSize; 2586c2aa98e2SPeter Wemm ProcListSize += PROC_LIST_SEG; 2587c2aa98e2SPeter Wemm ProcListVec = npv; 2588c2aa98e2SPeter Wemm } 2589065a643dSPeter Wemm ProcListVec[i].proc_pid = pid; 259040266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task); 259106f25ae9SGregory Neil Shapiro ProcListVec[i].proc_type = type; 259240266059SGregory Neil Shapiro ProcListVec[i].proc_count = count; 259340266059SGregory Neil Shapiro ProcListVec[i].proc_other = other; 2594e92d3f3fSGregory Neil Shapiro if (hostaddr != NULL) 2595e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr = *hostaddr; 2596e92d3f3fSGregory Neil Shapiro else 2597e92d3f3fSGregory Neil Shapiro memset(&ProcListVec[i].proc_hostaddr, 0, 2598e92d3f3fSGregory Neil Shapiro sizeof(ProcListVec[i].proc_hostaddr)); 2599065a643dSPeter Wemm 2600065a643dSPeter Wemm /* if process adding itself, it's not a child */ 260140266059SGregory Neil Shapiro if (pid != CurrentPid) 260240266059SGregory Neil Shapiro { 260340266059SGregory Neil Shapiro SM_ASSERT(CurChildren < INT_MAX); 2604c2aa98e2SPeter Wemm CurChildren++; 2605c2aa98e2SPeter Wemm } 260640266059SGregory Neil Shapiro } 260740266059SGregory Neil Shapiro /* 2608065a643dSPeter Wemm ** PROC_LIST_SET -- set pid task in process list 2609065a643dSPeter Wemm ** 2610065a643dSPeter Wemm ** Parameters: 2611065a643dSPeter Wemm ** pid -- pid to set 2612065a643dSPeter Wemm ** task -- task of pid 2613065a643dSPeter Wemm ** 2614065a643dSPeter Wemm ** Returns: 2615065a643dSPeter Wemm ** none. 2616065a643dSPeter Wemm */ 2617065a643dSPeter Wemm 2618065a643dSPeter Wemm void 2619065a643dSPeter Wemm proc_list_set(pid, task) 2620065a643dSPeter Wemm pid_t pid; 2621065a643dSPeter Wemm char *task; 2622065a643dSPeter Wemm { 2623065a643dSPeter Wemm int i; 2624065a643dSPeter Wemm 2625065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++) 2626065a643dSPeter Wemm { 2627065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid) 2628065a643dSPeter Wemm { 262940266059SGregory Neil Shapiro PSTRSET(ProcListVec[i].proc_task, task); 2630065a643dSPeter Wemm break; 2631065a643dSPeter Wemm } 2632065a643dSPeter Wemm } 2633065a643dSPeter Wemm } 263440266059SGregory Neil Shapiro /* 2635c2aa98e2SPeter Wemm ** PROC_LIST_DROP -- drop pid from process list 2636c2aa98e2SPeter Wemm ** 2637c2aa98e2SPeter Wemm ** Parameters: 2638c2aa98e2SPeter Wemm ** pid -- pid to drop 263940266059SGregory Neil Shapiro ** st -- process status 264040266059SGregory Neil Shapiro ** other -- storage for proc_other (return). 2641c2aa98e2SPeter Wemm ** 2642c2aa98e2SPeter Wemm ** Returns: 264340266059SGregory Neil Shapiro ** none. 264440266059SGregory Neil Shapiro ** 264540266059SGregory Neil Shapiro ** Side Effects: 264640266059SGregory Neil Shapiro ** May decrease CurChildren, CurRunners, or 264740266059SGregory Neil Shapiro ** set RestartRequest or ShutdownRequest. 26488774250cSGregory Neil Shapiro ** 26498774250cSGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 26508774250cSGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 26518774250cSGregory Neil Shapiro ** DOING. 2652c2aa98e2SPeter Wemm */ 2653c2aa98e2SPeter Wemm 265440266059SGregory Neil Shapiro void 265540266059SGregory Neil Shapiro proc_list_drop(pid, st, other) 2656c2aa98e2SPeter Wemm pid_t pid; 265740266059SGregory Neil Shapiro int st; 265840266059SGregory Neil Shapiro int *other; 2659c2aa98e2SPeter Wemm { 2660c2aa98e2SPeter Wemm int i; 266106f25ae9SGregory Neil Shapiro int type = PROC_NONE; 2662c2aa98e2SPeter Wemm 2663c2aa98e2SPeter Wemm for (i = 0; i < ProcListSize; i++) 2664c2aa98e2SPeter Wemm { 2665065a643dSPeter Wemm if (ProcListVec[i].proc_pid == pid) 2666c2aa98e2SPeter Wemm { 2667065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 266806f25ae9SGregory Neil Shapiro type = ProcListVec[i].proc_type; 266940266059SGregory Neil Shapiro if (other != NULL) 267040266059SGregory Neil Shapiro *other = ProcListVec[i].proc_other; 2671c2aa98e2SPeter Wemm break; 2672c2aa98e2SPeter Wemm } 2673c2aa98e2SPeter Wemm } 2674c2aa98e2SPeter Wemm if (CurChildren > 0) 2675c2aa98e2SPeter Wemm CurChildren--; 267606f25ae9SGregory Neil Shapiro 267706f25ae9SGregory Neil Shapiro 267840266059SGregory Neil Shapiro if (type == PROC_CONTROL && WIFEXITED(st)) 267940266059SGregory Neil Shapiro { 268040266059SGregory Neil Shapiro /* if so, see if we need to restart or shutdown */ 268140266059SGregory Neil Shapiro if (WEXITSTATUS(st) == EX_RESTART) 268240266059SGregory Neil Shapiro RestartRequest = "control socket"; 268340266059SGregory Neil Shapiro else if (WEXITSTATUS(st) == EX_SHUTDOWN) 268440266059SGregory Neil Shapiro ShutdownRequest = "control socket"; 2685c2aa98e2SPeter Wemm } 268640266059SGregory Neil Shapiro else if (type == PROC_QUEUE_CHILD && !WIFSTOPPED(st) && 268740266059SGregory Neil Shapiro ProcListVec[i].proc_other > -1) 268840266059SGregory Neil Shapiro { 268940266059SGregory Neil Shapiro /* restart this persistent runner */ 269040266059SGregory Neil Shapiro mark_work_group_restart(ProcListVec[i].proc_other, st); 269140266059SGregory Neil Shapiro } 269240266059SGregory Neil Shapiro else if (type == PROC_QUEUE) 269340266059SGregory Neil Shapiro CurRunners -= ProcListVec[i].proc_count; 269440266059SGregory Neil Shapiro } 269540266059SGregory Neil Shapiro /* 2696c2aa98e2SPeter Wemm ** PROC_LIST_CLEAR -- clear the process list 2697c2aa98e2SPeter Wemm ** 2698c2aa98e2SPeter Wemm ** Parameters: 2699c2aa98e2SPeter Wemm ** none. 2700c2aa98e2SPeter Wemm ** 2701c2aa98e2SPeter Wemm ** Returns: 2702c2aa98e2SPeter Wemm ** none. 270340266059SGregory Neil Shapiro ** 270440266059SGregory Neil Shapiro ** Side Effects: 270540266059SGregory Neil Shapiro ** Sets CurChildren to zero. 2706c2aa98e2SPeter Wemm */ 2707c2aa98e2SPeter Wemm 2708c2aa98e2SPeter Wemm void 2709c2aa98e2SPeter Wemm proc_list_clear() 2710c2aa98e2SPeter Wemm { 2711c2aa98e2SPeter Wemm int i; 2712c2aa98e2SPeter Wemm 2713065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */ 2714065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++) 2715065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 2716c2aa98e2SPeter Wemm CurChildren = 0; 2717c2aa98e2SPeter Wemm } 271840266059SGregory Neil Shapiro /* 2719c2aa98e2SPeter Wemm ** PROC_LIST_PROBE -- probe processes in the list to see if they still exist 2720c2aa98e2SPeter Wemm ** 2721c2aa98e2SPeter Wemm ** Parameters: 2722c2aa98e2SPeter Wemm ** none 2723c2aa98e2SPeter Wemm ** 2724c2aa98e2SPeter Wemm ** Returns: 2725c2aa98e2SPeter Wemm ** none 272640266059SGregory Neil Shapiro ** 272740266059SGregory Neil Shapiro ** Side Effects: 272840266059SGregory Neil Shapiro ** May decrease CurChildren. 2729c2aa98e2SPeter Wemm */ 2730c2aa98e2SPeter Wemm 2731c2aa98e2SPeter Wemm void 2732c2aa98e2SPeter Wemm proc_list_probe() 2733c2aa98e2SPeter Wemm { 2734c2aa98e2SPeter Wemm int i; 2735c2aa98e2SPeter Wemm 2736065a643dSPeter Wemm /* start from 1 since 0 is the daemon itself */ 2737065a643dSPeter Wemm for (i = 1; i < ProcListSize; i++) 2738c2aa98e2SPeter Wemm { 2739065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2740c2aa98e2SPeter Wemm continue; 2741065a643dSPeter Wemm if (kill(ProcListVec[i].proc_pid, 0) < 0) 2742c2aa98e2SPeter Wemm { 2743c2aa98e2SPeter Wemm if (LogLevel > 3) 2744c2aa98e2SPeter Wemm sm_syslog(LOG_DEBUG, CurEnv->e_id, 2745c2aa98e2SPeter Wemm "proc_list_probe: lost pid %d", 2746065a643dSPeter Wemm (int) ProcListVec[i].proc_pid); 2747065a643dSPeter Wemm ProcListVec[i].proc_pid = NO_PID; 274840266059SGregory Neil Shapiro SM_FREE_CLR(ProcListVec[i].proc_task); 2749c2aa98e2SPeter Wemm CurChildren--; 2750c2aa98e2SPeter Wemm } 2751c2aa98e2SPeter Wemm } 2752c2aa98e2SPeter Wemm if (CurChildren < 0) 2753c2aa98e2SPeter Wemm CurChildren = 0; 2754c2aa98e2SPeter Wemm } 275540266059SGregory Neil Shapiro 275640266059SGregory Neil Shapiro /* 2757065a643dSPeter Wemm ** PROC_LIST_DISPLAY -- display the process list 2758065a643dSPeter Wemm ** 2759065a643dSPeter Wemm ** Parameters: 2760065a643dSPeter Wemm ** out -- output file pointer 276140266059SGregory Neil Shapiro ** prefix -- string to output in front of each line. 2762065a643dSPeter Wemm ** 2763065a643dSPeter Wemm ** Returns: 2764065a643dSPeter Wemm ** none. 2765065a643dSPeter Wemm */ 2766065a643dSPeter Wemm 2767065a643dSPeter Wemm void 276840266059SGregory Neil Shapiro proc_list_display(out, prefix) 276940266059SGregory Neil Shapiro SM_FILE_T *out; 277040266059SGregory Neil Shapiro char *prefix; 2771065a643dSPeter Wemm { 2772065a643dSPeter Wemm int i; 2773065a643dSPeter Wemm 2774065a643dSPeter Wemm for (i = 0; i < ProcListSize; i++) 2775065a643dSPeter Wemm { 2776065a643dSPeter Wemm if (ProcListVec[i].proc_pid == NO_PID) 2777065a643dSPeter Wemm continue; 2778065a643dSPeter Wemm 277940266059SGregory Neil Shapiro (void) sm_io_fprintf(out, SM_TIME_DEFAULT, "%s%d %s%s\n", 278040266059SGregory Neil Shapiro prefix, 278140266059SGregory Neil Shapiro (int) ProcListVec[i].proc_pid, 2782065a643dSPeter Wemm ProcListVec[i].proc_task != NULL ? 2783065a643dSPeter Wemm ProcListVec[i].proc_task : "(unknown)", 2784065a643dSPeter Wemm (OpMode == MD_SMTP || 2785065a643dSPeter Wemm OpMode == MD_DAEMON || 2786065a643dSPeter Wemm OpMode == MD_ARPAFTP) ? "\r" : ""); 2787065a643dSPeter Wemm } 2788065a643dSPeter Wemm } 278940266059SGregory Neil Shapiro 279040266059SGregory Neil Shapiro /* 279140266059SGregory Neil Shapiro ** PROC_LIST_SIGNAL -- send a signal to a type of process in the list 279213058a91SGregory Neil Shapiro ** 279313058a91SGregory Neil Shapiro ** Parameters: 279440266059SGregory Neil Shapiro ** type -- type of process to signal 279540266059SGregory Neil Shapiro ** signal -- the type of signal to send 279613058a91SGregory Neil Shapiro ** 279740266059SGregory Neil Shapiro ** Results: 279840266059SGregory Neil Shapiro ** none. 2799c2aa98e2SPeter Wemm ** 280040266059SGregory Neil Shapiro ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD 280140266059SGregory Neil Shapiro ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE 280240266059SGregory Neil Shapiro ** DOING. 2803c2aa98e2SPeter Wemm */ 2804c2aa98e2SPeter Wemm 280540266059SGregory Neil Shapiro void 280640266059SGregory Neil Shapiro proc_list_signal(type, signal) 280740266059SGregory Neil Shapiro int type; 280840266059SGregory Neil Shapiro int signal; 2809c2aa98e2SPeter Wemm { 281040266059SGregory Neil Shapiro int chldwasblocked; 281140266059SGregory Neil Shapiro int alrmwasblocked; 281240266059SGregory Neil Shapiro int i; 281340266059SGregory Neil Shapiro pid_t mypid = getpid(); 2814c2aa98e2SPeter Wemm 281540266059SGregory Neil Shapiro /* block these signals so that we may signal cleanly */ 281640266059SGregory Neil Shapiro chldwasblocked = sm_blocksignal(SIGCHLD); 281740266059SGregory Neil Shapiro alrmwasblocked = sm_blocksignal(SIGALRM); 281840266059SGregory Neil Shapiro 281940266059SGregory Neil Shapiro /* Find all processes of type and send signal */ 282040266059SGregory Neil Shapiro for (i = 0; i < ProcListSize; i++) 282140266059SGregory Neil Shapiro { 282240266059SGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID || 282340266059SGregory Neil Shapiro ProcListVec[i].proc_pid == mypid) 282440266059SGregory Neil Shapiro continue; 282540266059SGregory Neil Shapiro if (ProcListVec[i].proc_type != type) 282640266059SGregory Neil Shapiro continue; 282740266059SGregory Neil Shapiro (void) kill(ProcListVec[i].proc_pid, signal); 2828c2aa98e2SPeter Wemm } 2829c2aa98e2SPeter Wemm 283040266059SGregory Neil Shapiro /* restore the signals */ 283140266059SGregory Neil Shapiro if (alrmwasblocked == 0) 283240266059SGregory Neil Shapiro (void) sm_releasesignal(SIGALRM); 283340266059SGregory Neil Shapiro if (chldwasblocked == 0) 283440266059SGregory Neil Shapiro (void) sm_releasesignal(SIGCHLD); 2835c2aa98e2SPeter Wemm } 2836e92d3f3fSGregory Neil Shapiro 2837e92d3f3fSGregory Neil Shapiro /* 2838e92d3f3fSGregory Neil Shapiro ** COUNT_OPEN_CONNECTIONS 2839e92d3f3fSGregory Neil Shapiro ** 2840e92d3f3fSGregory Neil Shapiro ** Parameters: 2841e92d3f3fSGregory Neil Shapiro ** hostaddr - ClientAddress 2842e92d3f3fSGregory Neil Shapiro ** 2843e92d3f3fSGregory Neil Shapiro ** Returns: 2844e92d3f3fSGregory Neil Shapiro ** the number of open connections for this client 2845e92d3f3fSGregory Neil Shapiro ** 2846e92d3f3fSGregory Neil Shapiro */ 2847e92d3f3fSGregory Neil Shapiro 2848e92d3f3fSGregory Neil Shapiro int 2849e92d3f3fSGregory Neil Shapiro count_open_connections(hostaddr) 2850e92d3f3fSGregory Neil Shapiro SOCKADDR *hostaddr; 2851e92d3f3fSGregory Neil Shapiro { 2852e92d3f3fSGregory Neil Shapiro int i, n; 2853e92d3f3fSGregory Neil Shapiro 2854e92d3f3fSGregory Neil Shapiro if (hostaddr == NULL) 2855e92d3f3fSGregory Neil Shapiro return 0; 2856e92d3f3fSGregory Neil Shapiro n = 0; 2857e92d3f3fSGregory Neil Shapiro for (i = 0; i < ProcListSize; i++) 2858e92d3f3fSGregory Neil Shapiro { 2859e92d3f3fSGregory Neil Shapiro if (ProcListVec[i].proc_pid == NO_PID) 2860e92d3f3fSGregory Neil Shapiro continue; 2861e92d3f3fSGregory Neil Shapiro 2862e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family != 2863e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sa.sa_family) 2864e92d3f3fSGregory Neil Shapiro continue; 2865e92d3f3fSGregory Neil Shapiro #if NETINET 2866e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET && 2867e92d3f3fSGregory Neil Shapiro (hostaddr->sin.sin_addr.s_addr == 2868e92d3f3fSGregory Neil Shapiro ProcListVec[i].proc_hostaddr.sin.sin_addr.s_addr)) 2869e92d3f3fSGregory Neil Shapiro n++; 2870e92d3f3fSGregory Neil Shapiro #endif /* NETINET */ 2871e92d3f3fSGregory Neil Shapiro #if NETINET6 2872e92d3f3fSGregory Neil Shapiro if (hostaddr->sa.sa_family == AF_INET6 && 2873e92d3f3fSGregory Neil Shapiro IN6_ARE_ADDR_EQUAL(&(hostaddr->sin6.sin6_addr), 2874e92d3f3fSGregory Neil Shapiro &(ProcListVec[i].proc_hostaddr.sin6.sin6_addr))) 2875e92d3f3fSGregory Neil Shapiro n++; 2876e92d3f3fSGregory Neil Shapiro #endif /* NETINET6 */ 2877e92d3f3fSGregory Neil Shapiro } 2878e92d3f3fSGregory Neil Shapiro return n; 2879e92d3f3fSGregory Neil Shapiro } 2880