| 1 | /* $Cambridge: exim/src/src/string.c,v 1.15 2009/11/16 19:50:37 nm4 Exp $ */ |
| 2 | |
| 3 | /************************************************* |
| 4 | * Exim - an Internet mail transport agent * |
| 5 | *************************************************/ |
| 6 | |
| 7 | /* Copyright (c) University of Cambridge 1995 - 2009 */ |
| 8 | /* See the file NOTICE for conditions of use and distribution. */ |
| 9 | |
| 10 | /* Miscellaneous string-handling functions. Some are not required for |
| 11 | utilities and tests, and are cut out by the COMPILE_UTILITY macro. */ |
| 12 | |
| 13 | |
| 14 | #include "exim.h" |
| 15 | |
| 16 | |
| 17 | #ifndef COMPILE_UTILITY |
| 18 | /************************************************* |
| 19 | * Test for IP address * |
| 20 | *************************************************/ |
| 21 | |
| 22 | /* This used just to be a regular expression, but with IPv6 things are a bit |
| 23 | more complicated. If the address contains a colon, it is assumed to be a v6 |
| 24 | address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present, |
| 25 | and maskptr is not NULL, its offset is placed there. |
| 26 | |
| 27 | Arguments: |
| 28 | s a string |
| 29 | maskptr NULL if no mask is permitted to follow |
| 30 | otherwise, points to an int where the offset of '/' is placed |
| 31 | if there is no / followed by trailing digits, *maskptr is set 0 |
| 32 | |
| 33 | Returns: 0 if the string is not a textual representation of an IP address |
| 34 | 4 if it is an IPv4 address |
| 35 | 6 if it is an IPv6 address |
| 36 | */ |
| 37 | |
| 38 | int |
| 39 | string_is_ip_address(uschar *s, int *maskptr) |
| 40 | { |
| 41 | int i; |
| 42 | int yield = 4; |
| 43 | |
| 44 | /* If an optional mask is permitted, check for it. If found, pass back the |
| 45 | offset. */ |
| 46 | |
| 47 | if (maskptr != NULL) |
| 48 | { |
| 49 | uschar *ss = s + Ustrlen(s); |
| 50 | *maskptr = 0; |
| 51 | if (s != ss && isdigit(*(--ss))) |
| 52 | { |
| 53 | while (ss > s && isdigit(ss[-1])) ss--; |
| 54 | if (ss > s && *(--ss) == '/') *maskptr = ss - s; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* A colon anywhere in the string => IPv6 address */ |
| 59 | |
| 60 | if (Ustrchr(s, ':') != NULL) |
| 61 | { |
| 62 | BOOL had_double_colon = FALSE; |
| 63 | BOOL v4end = FALSE; |
| 64 | int count = 0; |
| 65 | |
| 66 | yield = 6; |
| 67 | |
| 68 | /* An IPv6 address must start with hex digit or double colon. A single |
| 69 | colon is invalid. */ |
| 70 | |
| 71 | if (*s == ':' && *(++s) != ':') return 0; |
| 72 | |
| 73 | /* Now read up to 8 components consisting of up to 4 hex digits each. There |
| 74 | may be one and only one appearance of double colon, which implies any number |
| 75 | of binary zero bits. The number of preceding components is held in count. */ |
| 76 | |
| 77 | for (count = 0; count < 8; count++) |
| 78 | { |
| 79 | /* If the end of the string is reached before reading 8 components, the |
| 80 | address is valid provided a double colon has been read. This also applies |
| 81 | if we hit the / that introduces a mask or the % that introduces the |
| 82 | interface specifier (scope id) of a link-local address. */ |
| 83 | |
| 84 | if (*s == 0 || *s == '%' || *s == '/') return had_double_colon? yield : 0; |
| 85 | |
| 86 | /* If a component starts with an additional colon, we have hit a double |
| 87 | colon. This is permitted to appear once only, and counts as at least |
| 88 | one component. The final component may be of this form. */ |
| 89 | |
| 90 | if (*s == ':') |
| 91 | { |
| 92 | if (had_double_colon) return 0; |
| 93 | had_double_colon = TRUE; |
| 94 | s++; |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | /* If the remainder of the string contains a dot but no colons, we |
| 99 | can expect a trailing IPv4 address. This is valid if either there has |
| 100 | been no double-colon and this is the 7th component (with the IPv4 address |
| 101 | being the 7th & 8th components), OR if there has been a double-colon |
| 102 | and fewer than 6 components. */ |
| 103 | |
| 104 | if (Ustrchr(s, ':') == NULL && Ustrchr(s, '.') != NULL) |
| 105 | { |
| 106 | if ((!had_double_colon && count != 6) || |
| 107 | (had_double_colon && count > 6)) return 0; |
| 108 | v4end = TRUE; |
| 109 | yield = 6; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | /* Check for at least one and not more than 4 hex digits for this |
| 114 | component. */ |
| 115 | |
| 116 | if (!isxdigit(*s++)) return 0; |
| 117 | if (isxdigit(*s) && isxdigit(*(++s)) && isxdigit(*(++s))) s++; |
| 118 | |
| 119 | /* If the component is terminated by colon and there is more to |
| 120 | follow, skip over the colon. If there is no more to follow the address is |
| 121 | invalid. */ |
| 122 | |
| 123 | if (*s == ':' && *(++s) == 0) return 0; |
| 124 | } |
| 125 | |
| 126 | /* If about to handle a trailing IPv4 address, drop through. Otherwise |
| 127 | all is well if we are at the end of the string or at the mask or at a percent |
| 128 | sign, which introduces the interface specifier (scope id) of a link local |
| 129 | address. */ |
| 130 | |
| 131 | if (!v4end) |
| 132 | return (*s == 0 || *s == '%' || |
| 133 | (*s == '/' && maskptr != NULL && *maskptr != 0))? yield : 0; |
| 134 | } |
| 135 | |
| 136 | /* Test for IPv4 address, which may be the tail-end of an IPv6 address. */ |
| 137 | |
| 138 | for (i = 0; i < 4; i++) |
| 139 | { |
| 140 | if (i != 0 && *s++ != '.') return 0; |
| 141 | if (!isdigit(*s++)) return 0; |
| 142 | if (isdigit(*s) && isdigit(*(++s))) s++; |
| 143 | } |
| 144 | |
| 145 | return (*s == 0 || (*s == '/' && maskptr != NULL && *maskptr != 0))? |
| 146 | yield : 0; |
| 147 | } |
| 148 | #endif /* COMPILE_UTILITY */ |
| 149 | |
| 150 | |
| 151 | /************************************************* |
| 152 | * Format message size * |
| 153 | *************************************************/ |
| 154 | |
| 155 | /* Convert a message size in bytes to printing form, rounding |
| 156 | according to the magnitude of the number. A value of zero causes |
| 157 | a string of spaces to be returned. |
| 158 | |
| 159 | Arguments: |
| 160 | size the message size in bytes |
| 161 | buffer where to put the answer |
| 162 | |
| 163 | Returns: pointer to the buffer |
| 164 | a string of exactly 5 characters is normally returned |
| 165 | */ |
| 166 | |
| 167 | uschar * |
| 168 | string_format_size(int size, uschar *buffer) |
| 169 | { |
| 170 | if (size == 0) Ustrcpy(CS buffer, " "); |
| 171 | else if (size < 1024) sprintf(CS buffer, "%5d", size); |
| 172 | else if (size < 10*1024) |
| 173 | sprintf(CS buffer, "%4.1fK", (double)size / 1024.0); |
| 174 | else if (size < 1024*1024) |
| 175 | sprintf(CS buffer, "%4dK", (size + 512)/1024); |
| 176 | else if (size < 10*1024*1024) |
| 177 | sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0)); |
| 178 | else |
| 179 | sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024)); |
| 180 | return buffer; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | #ifndef COMPILE_UTILITY |
| 186 | /************************************************* |
| 187 | * Convert a number to base 62 format * |
| 188 | *************************************************/ |
| 189 | |
| 190 | /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of |
| 191 | BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a |
| 192 | static area. |
| 193 | |
| 194 | Argument: a long integer |
| 195 | Returns: pointer to base 62 string |
| 196 | */ |
| 197 | |
| 198 | uschar * |
| 199 | string_base62(unsigned long int value) |
| 200 | { |
| 201 | static uschar yield[7]; |
| 202 | uschar *p = yield + sizeof(yield) - 1; |
| 203 | *p = 0; |
| 204 | while (p > yield) |
| 205 | { |
| 206 | *(--p) = base62_chars[value % BASE_62]; |
| 207 | value /= BASE_62; |
| 208 | } |
| 209 | return yield; |
| 210 | } |
| 211 | #endif /* COMPILE_UTILITY */ |
| 212 | |
| 213 | |
| 214 | |
| 215 | #ifndef COMPILE_UTILITY |
| 216 | /************************************************* |
| 217 | * Interpret escape sequence * |
| 218 | *************************************************/ |
| 219 | |
| 220 | /* This function is called from several places where escape sequences are to be |
| 221 | interpreted in strings. |
| 222 | |
| 223 | Arguments: |
| 224 | pp points a pointer to the initiating "\" in the string; |
| 225 | the pointer gets updated to point to the final character |
| 226 | Returns: the value of the character escape |
| 227 | */ |
| 228 | |
| 229 | int |
| 230 | string_interpret_escape(uschar **pp) |
| 231 | { |
| 232 | int ch; |
| 233 | uschar *p = *pp; |
| 234 | ch = *(++p); |
| 235 | if (isdigit(ch) && ch != '8' && ch != '9') |
| 236 | { |
| 237 | ch -= '0'; |
| 238 | if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') |
| 239 | { |
| 240 | ch = ch * 8 + *(++p) - '0'; |
| 241 | if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') |
| 242 | ch = ch * 8 + *(++p) - '0'; |
| 243 | } |
| 244 | } |
| 245 | else switch(ch) |
| 246 | { |
| 247 | case 'n': ch = '\n'; break; |
| 248 | case 'r': ch = '\r'; break; |
| 249 | case 't': ch = '\t'; break; |
| 250 | case 'x': |
| 251 | ch = 0; |
| 252 | if (isxdigit(p[1])) |
| 253 | { |
| 254 | ch = ch * 16 + |
| 255 | Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; |
| 256 | if (isxdigit(p[1])) ch = ch * 16 + |
| 257 | Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | *pp = p; |
| 262 | return ch; |
| 263 | } |
| 264 | #endif /* COMPILE_UTILITY */ |
| 265 | |
| 266 | |
| 267 | |
| 268 | #ifndef COMPILE_UTILITY |
| 269 | /************************************************* |
| 270 | * Ensure string is printable * |
| 271 | *************************************************/ |
| 272 | |
| 273 | /* This function is called for critical strings. It checks for any |
| 274 | non-printing characters, and if any are found, it makes a new copy |
| 275 | of the string with suitable escape sequences. It is most often called by the |
| 276 | macro string_printing(), which sets allow_tab TRUE. |
| 277 | |
| 278 | Arguments: |
| 279 | s the input string |
| 280 | allow_tab TRUE to allow tab as a printing character |
| 281 | |
| 282 | Returns: string with non-printers encoded as printing sequences |
| 283 | */ |
| 284 | |
| 285 | uschar * |
| 286 | string_printing2(uschar *s, BOOL allow_tab) |
| 287 | { |
| 288 | int nonprintcount = 0; |
| 289 | int length = 0; |
| 290 | uschar *t = s; |
| 291 | uschar *ss, *tt; |
| 292 | |
| 293 | while (*t != 0) |
| 294 | { |
| 295 | int c = *t++; |
| 296 | if (!mac_isprint(c) || (!allow_tab && c == '\t')) nonprintcount++; |
| 297 | length++; |
| 298 | } |
| 299 | |
| 300 | if (nonprintcount == 0) return s; |
| 301 | |
| 302 | /* Get a new block of store guaranteed big enough to hold the |
| 303 | expanded string. */ |
| 304 | |
| 305 | ss = store_get(length + nonprintcount * 4 + 1); |
| 306 | |
| 307 | /* Copy everying, escaping non printers. */ |
| 308 | |
| 309 | t = s; |
| 310 | tt = ss; |
| 311 | |
| 312 | while (*t != 0) |
| 313 | { |
| 314 | int c = *t; |
| 315 | if (mac_isprint(c) && (allow_tab || c != '\t')) *tt++ = *t++; else |
| 316 | { |
| 317 | *tt++ = '\\'; |
| 318 | switch (*t) |
| 319 | { |
| 320 | case '\n': *tt++ = 'n'; break; |
| 321 | case '\r': *tt++ = 'r'; break; |
| 322 | case '\b': *tt++ = 'b'; break; |
| 323 | case '\v': *tt++ = 'v'; break; |
| 324 | case '\f': *tt++ = 'f'; break; |
| 325 | case '\t': *tt++ = 't'; break; |
| 326 | default: sprintf(CS tt, "%03o", *t); tt += 3; break; |
| 327 | } |
| 328 | t++; |
| 329 | } |
| 330 | } |
| 331 | *tt = 0; |
| 332 | return ss; |
| 333 | } |
| 334 | #endif /* COMPILE_UTILITY */ |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | /************************************************* |
| 340 | * Copy and save string * |
| 341 | *************************************************/ |
| 342 | |
| 343 | /* This function assumes that memcpy() is faster than strcpy(). |
| 344 | |
| 345 | Argument: string to copy |
| 346 | Returns: copy of string in new store |
| 347 | */ |
| 348 | |
| 349 | uschar * |
| 350 | string_copy(uschar *s) |
| 351 | { |
| 352 | int len = Ustrlen(s) + 1; |
| 353 | uschar *ss = store_get(len); |
| 354 | memcpy(ss, s, len); |
| 355 | return ss; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | /************************************************* |
| 361 | * Copy and save string in malloc'd store * |
| 362 | *************************************************/ |
| 363 | |
| 364 | /* This function assumes that memcpy() is faster than strcpy(). |
| 365 | |
| 366 | Argument: string to copy |
| 367 | Returns: copy of string in new store |
| 368 | */ |
| 369 | |
| 370 | uschar * |
| 371 | string_copy_malloc(uschar *s) |
| 372 | { |
| 373 | int len = Ustrlen(s) + 1; |
| 374 | uschar *ss = store_malloc(len); |
| 375 | memcpy(ss, s, len); |
| 376 | return ss; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | /************************************************* |
| 382 | * Copy, lowercase and save string * |
| 383 | *************************************************/ |
| 384 | |
| 385 | /* |
| 386 | Argument: string to copy |
| 387 | Returns: copy of string in new store, with letters lowercased |
| 388 | */ |
| 389 | |
| 390 | uschar * |
| 391 | string_copylc(uschar *s) |
| 392 | { |
| 393 | uschar *ss = store_get(Ustrlen(s) + 1); |
| 394 | uschar *p = ss; |
| 395 | while (*s != 0) *p++ = tolower(*s++); |
| 396 | *p = 0; |
| 397 | return ss; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | /************************************************* |
| 403 | * Copy and save string, given length * |
| 404 | *************************************************/ |
| 405 | |
| 406 | /* It is assumed the data contains no zeros. A zero is added |
| 407 | onto the end. |
| 408 | |
| 409 | Arguments: |
| 410 | s string to copy |
| 411 | n number of characters |
| 412 | |
| 413 | Returns: copy of string in new store |
| 414 | */ |
| 415 | |
| 416 | uschar * |
| 417 | string_copyn(uschar *s, int n) |
| 418 | { |
| 419 | uschar *ss = store_get(n + 1); |
| 420 | Ustrncpy(ss, s, n); |
| 421 | ss[n] = 0; |
| 422 | return ss; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /************************************************* |
| 427 | * Copy, lowercase, and save string, given length * |
| 428 | *************************************************/ |
| 429 | |
| 430 | /* It is assumed the data contains no zeros. A zero is added |
| 431 | onto the end. |
| 432 | |
| 433 | Arguments: |
| 434 | s string to copy |
| 435 | n number of characters |
| 436 | |
| 437 | Returns: copy of string in new store, with letters lowercased |
| 438 | */ |
| 439 | |
| 440 | uschar * |
| 441 | string_copynlc(uschar *s, int n) |
| 442 | { |
| 443 | uschar *ss = store_get(n + 1); |
| 444 | uschar *p = ss; |
| 445 | while (n-- > 0) *p++ = tolower(*s++); |
| 446 | *p = 0; |
| 447 | return ss; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | |
| 452 | /************************************************* |
| 453 | * Copy string if long, inserting newlines * |
| 454 | *************************************************/ |
| 455 | |
| 456 | /* If the given string is longer than 75 characters, it is copied, and within |
| 457 | the copy, certain space characters are converted into newlines. |
| 458 | |
| 459 | Argument: pointer to the string |
| 460 | Returns: pointer to the possibly altered string |
| 461 | */ |
| 462 | |
| 463 | uschar * |
| 464 | string_split_message(uschar *msg) |
| 465 | { |
| 466 | uschar *s, *ss; |
| 467 | |
| 468 | if (msg == NULL || Ustrlen(msg) <= 75) return msg; |
| 469 | s = ss = msg = string_copy(msg); |
| 470 | |
| 471 | for (;;) |
| 472 | { |
| 473 | int i = 0; |
| 474 | while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++; |
| 475 | if (*ss == 0) break; |
| 476 | if (*ss == '\n') |
| 477 | s = ++ss; |
| 478 | else |
| 479 | { |
| 480 | uschar *t = ss + 1; |
| 481 | uschar *tt = NULL; |
| 482 | while (--t > s + 35) |
| 483 | { |
| 484 | if (*t == ' ') |
| 485 | { |
| 486 | if (t[-1] == ':') { tt = t; break; } |
| 487 | if (tt == NULL) tt = t; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (tt == NULL) /* Can't split behind - try ahead */ |
| 492 | { |
| 493 | t = ss + 1; |
| 494 | while (*t != 0) |
| 495 | { |
| 496 | if (*t == ' ' || *t == '\n') |
| 497 | { tt = t; break; } |
| 498 | t++; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | if (tt == NULL) break; /* Can't find anywhere to split */ |
| 503 | *tt = '\n'; |
| 504 | s = ss = tt+1; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return msg; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | |
| 513 | /************************************************* |
| 514 | * Copy returned DNS domain name, de-escaping * |
| 515 | *************************************************/ |
| 516 | |
| 517 | /* If a domain name contains top-bit characters, some resolvers return |
| 518 | the fully qualified name with those characters turned into escapes. The |
| 519 | convention is a backslash followed by _decimal_ digits. We convert these |
| 520 | back into the original binary values. This will be relevant when |
| 521 | allow_utf8_domains is set true and UTF-8 characters are used in domain |
| 522 | names. Backslash can also be used to escape other characters, though we |
| 523 | shouldn't come across them in domain names. |
| 524 | |
| 525 | Argument: the domain name string |
| 526 | Returns: copy of string in new store, de-escaped |
| 527 | */ |
| 528 | |
| 529 | uschar * |
| 530 | string_copy_dnsdomain(uschar *s) |
| 531 | { |
| 532 | uschar *yield; |
| 533 | uschar *ss = yield = store_get(Ustrlen(s) + 1); |
| 534 | |
| 535 | while (*s != 0) |
| 536 | { |
| 537 | if (*s != '\\') |
| 538 | { |
| 539 | *ss++ = *s++; |
| 540 | } |
| 541 | else if (isdigit(s[1])) |
| 542 | { |
| 543 | *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0'; |
| 544 | s += 4; |
| 545 | } |
| 546 | else if (*(++s) != 0) |
| 547 | { |
| 548 | *ss++ = *s++; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | *ss = 0; |
| 553 | return yield; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | #ifndef COMPILE_UTILITY |
| 558 | /************************************************* |
| 559 | * Copy space-terminated or quoted string * |
| 560 | *************************************************/ |
| 561 | |
| 562 | /* This function copies from a string until its end, or until whitespace is |
| 563 | encountered, unless the string begins with a double quote, in which case the |
| 564 | terminating quote is sought, and escaping within the string is done. The length |
| 565 | of a de-quoted string can be no longer than the original, since escaping always |
| 566 | turns n characters into 1 character. |
| 567 | |
| 568 | Argument: pointer to the pointer to the first character, which gets updated |
| 569 | Returns: the new string |
| 570 | */ |
| 571 | |
| 572 | uschar * |
| 573 | string_dequote(uschar **sptr) |
| 574 | { |
| 575 | uschar *s = *sptr; |
| 576 | uschar *t, *yield; |
| 577 | |
| 578 | /* First find the end of the string */ |
| 579 | |
| 580 | if (*s != '\"') |
| 581 | { |
| 582 | while (*s != 0 && !isspace(*s)) s++; |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | s++; |
| 587 | while (*s != 0 && *s != '\"') |
| 588 | { |
| 589 | if (*s == '\\') (void)string_interpret_escape(&s); |
| 590 | s++; |
| 591 | } |
| 592 | if (*s != 0) s++; |
| 593 | } |
| 594 | |
| 595 | /* Get enough store to copy into */ |
| 596 | |
| 597 | t = yield = store_get(s - *sptr + 1); |
| 598 | s = *sptr; |
| 599 | |
| 600 | /* Do the copy */ |
| 601 | |
| 602 | if (*s != '\"') |
| 603 | { |
| 604 | while (*s != 0 && !isspace(*s)) *t++ = *s++; |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | s++; |
| 609 | while (*s != 0 && *s != '\"') |
| 610 | { |
| 611 | if (*s == '\\') *t++ = string_interpret_escape(&s); |
| 612 | else *t++ = *s; |
| 613 | s++; |
| 614 | } |
| 615 | if (*s != 0) s++; |
| 616 | } |
| 617 | |
| 618 | /* Update the pointer and return the terminated copy */ |
| 619 | |
| 620 | *sptr = s; |
| 621 | *t = 0; |
| 622 | return yield; |
| 623 | } |
| 624 | #endif /* COMPILE_UTILITY */ |
| 625 | |
| 626 | |
| 627 | |
| 628 | /************************************************* |
| 629 | * Format a string and save it * |
| 630 | *************************************************/ |
| 631 | |
| 632 | /* The formatting is done by string_format, which checks the length of |
| 633 | everything. |
| 634 | |
| 635 | Arguments: |
| 636 | format a printf() format - deliberately char * rather than uschar * |
| 637 | because it will most usually be a literal string |
| 638 | ... arguments for format |
| 639 | |
| 640 | Returns: pointer to fresh piece of store containing sprintf'ed string |
| 641 | */ |
| 642 | |
| 643 | uschar * |
| 644 | string_sprintf(const char *format, ...) |
| 645 | { |
| 646 | va_list ap; |
| 647 | uschar buffer[STRING_SPRINTF_BUFFER_SIZE]; |
| 648 | va_start(ap, format); |
| 649 | if (!string_vformat(buffer, sizeof(buffer), format, ap)) |
| 650 | log_write(0, LOG_MAIN|LOG_PANIC_DIE, |
| 651 | "string_sprintf expansion was longer than %d", sizeof(buffer)); |
| 652 | va_end(ap); |
| 653 | return string_copy(buffer); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | |
| 658 | /************************************************* |
| 659 | * Case-independent strncmp() function * |
| 660 | *************************************************/ |
| 661 | |
| 662 | /* |
| 663 | Arguments: |
| 664 | s first string |
| 665 | t second string |
| 666 | n number of characters to compare |
| 667 | |
| 668 | Returns: < 0, = 0, or > 0, according to the comparison |
| 669 | */ |
| 670 | |
| 671 | int |
| 672 | strncmpic(const uschar *s, const uschar *t, int n) |
| 673 | { |
| 674 | while (n--) |
| 675 | { |
| 676 | int c = tolower(*s++) - tolower(*t++); |
| 677 | if (c) return c; |
| 678 | } |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | /************************************************* |
| 684 | * Case-independent strcmp() function * |
| 685 | *************************************************/ |
| 686 | |
| 687 | /* |
| 688 | Arguments: |
| 689 | s first string |
| 690 | t second string |
| 691 | |
| 692 | Returns: < 0, = 0, or > 0, according to the comparison |
| 693 | */ |
| 694 | |
| 695 | int |
| 696 | strcmpic(const uschar *s, const uschar *t) |
| 697 | { |
| 698 | while (*s != 0) |
| 699 | { |
| 700 | int c = tolower(*s++) - tolower(*t++); |
| 701 | if (c != 0) return c; |
| 702 | } |
| 703 | return *t; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /************************************************* |
| 708 | * Case-independent strstr() function * |
| 709 | *************************************************/ |
| 710 | |
| 711 | /* The third argument specifies whether whitespace is required |
| 712 | to follow the matched string. |
| 713 | |
| 714 | Arguments: |
| 715 | s string to search |
| 716 | t substring to search for |
| 717 | space_follows if TRUE, match only if whitespace follows |
| 718 | |
| 719 | Returns: pointer to substring in string, or NULL if not found |
| 720 | */ |
| 721 | |
| 722 | uschar * |
| 723 | strstric(uschar *s, uschar *t, BOOL space_follows) |
| 724 | { |
| 725 | uschar *p = t; |
| 726 | uschar *yield = NULL; |
| 727 | int cl = tolower(*p); |
| 728 | int cu = toupper(*p); |
| 729 | |
| 730 | while (*s) |
| 731 | { |
| 732 | if (*s == cl || *s == cu) |
| 733 | { |
| 734 | if (yield == NULL) yield = s; |
| 735 | if (*(++p) == 0) |
| 736 | { |
| 737 | if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield; |
| 738 | yield = NULL; |
| 739 | p = t; |
| 740 | } |
| 741 | cl = tolower(*p); |
| 742 | cu = toupper(*p); |
| 743 | s++; |
| 744 | } |
| 745 | else if (yield != NULL) |
| 746 | { |
| 747 | yield = NULL; |
| 748 | p = t; |
| 749 | cl = tolower(*p); |
| 750 | cu = toupper(*p); |
| 751 | } |
| 752 | else s++; |
| 753 | } |
| 754 | return NULL; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | |
| 759 | #ifndef COMPILE_UTILITY |
| 760 | /************************************************* |
| 761 | * Get next string from separated list * |
| 762 | *************************************************/ |
| 763 | |
| 764 | /* Leading and trailing space is removed from each item. The separator in the |
| 765 | list is controlled by the int pointed to by the separator argument as follows: |
| 766 | |
| 767 | If the value is > 0 it is used as the separator. This is typically used for |
| 768 | sublists such as slash-separated options. The value is always a printing |
| 769 | character. |
| 770 | |
| 771 | (If the value is actually > UCHAR_MAX there is only one item in the list. |
| 772 | This is used for some cases when called via functions that sometimes |
| 773 | plough through lists, and sometimes are given single items.) |
| 774 | |
| 775 | If the value is <= 0, the string is inspected for a leading <x, where x is an |
| 776 | ispunct() or an iscntrl() character. If found, x is used as the separator. If |
| 777 | not found: |
| 778 | |
| 779 | (a) if separator == 0, ':' is used |
| 780 | (b) if separator <0, -separator is used |
| 781 | |
| 782 | In all cases the value of the separator that is used is written back to the |
| 783 | int so that it is used on subsequent calls as we progress through the list. |
| 784 | |
| 785 | A literal ispunct() separator can be represented in an item by doubling, but |
| 786 | there is no way to include an iscntrl() separator as part of the data. |
| 787 | |
| 788 | Arguments: |
| 789 | listptr points to a pointer to the current start of the list; the |
| 790 | pointer gets updated to point after the end of the next item |
| 791 | separator a pointer to the separator character in an int (see above) |
| 792 | buffer where to put a copy of the next string in the list; or |
| 793 | NULL if the next string is returned in new memory |
| 794 | buflen when buffer is not NULL, the size of buffer; otherwise ignored |
| 795 | |
| 796 | Returns: pointer to buffer, containing the next substring, |
| 797 | or NULL if no more substrings |
| 798 | */ |
| 799 | |
| 800 | uschar * |
| 801 | string_nextinlist(uschar **listptr, int *separator, uschar *buffer, int buflen) |
| 802 | { |
| 803 | register int sep = *separator; |
| 804 | register uschar *s = *listptr; |
| 805 | BOOL sep_is_special; |
| 806 | |
| 807 | if (s == NULL) return NULL; |
| 808 | |
| 809 | /* This allows for a fixed specified separator to be an iscntrl() character, |
| 810 | but at the time of implementation, this is never the case. However, it's best |
| 811 | to be conservative. */ |
| 812 | |
| 813 | while (isspace(*s) && *s != sep) s++; |
| 814 | |
| 815 | /* A change of separator is permitted, so look for a leading '<' followed by an |
| 816 | allowed character. */ |
| 817 | |
| 818 | if (sep <= 0) |
| 819 | { |
| 820 | if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1]))) |
| 821 | { |
| 822 | sep = s[1]; |
| 823 | s += 2; |
| 824 | while (isspace(*s) && *s != sep) s++; |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | sep = (sep == 0)? ':' : -sep; |
| 829 | } |
| 830 | *separator = sep; |
| 831 | } |
| 832 | |
| 833 | /* An empty string has no list elements */ |
| 834 | |
| 835 | if (*s == 0) return NULL; |
| 836 | |
| 837 | /* Note whether whether or not the separator is an iscntrl() character. */ |
| 838 | |
| 839 | sep_is_special = iscntrl(sep); |
| 840 | |
| 841 | /* Handle the case when a buffer is provided. */ |
| 842 | |
| 843 | if (buffer != NULL) |
| 844 | { |
| 845 | register int p = 0; |
| 846 | for (; *s != 0; s++) |
| 847 | { |
| 848 | if (*s == sep && (*(++s) != sep || sep_is_special)) break; |
| 849 | if (p < buflen - 1) buffer[p++] = *s; |
| 850 | } |
| 851 | while (p > 0 && isspace(buffer[p-1])) p--; |
| 852 | buffer[p] = 0; |
| 853 | } |
| 854 | |
| 855 | /* Handle the case when a buffer is not provided. */ |
| 856 | |
| 857 | else |
| 858 | { |
| 859 | int size = 0; |
| 860 | int ptr = 0; |
| 861 | uschar *ss; |
| 862 | |
| 863 | /* We know that *s != 0 at this point. However, it might be pointing to a |
| 864 | separator, which could indicate an empty string, or (if an ispunct() |
| 865 | character) could be doubled to indicate a separator character as data at the |
| 866 | start of a string. Avoid getting working memory for an empty item. */ |
| 867 | |
| 868 | if (*s == sep) |
| 869 | { |
| 870 | s++; |
| 871 | if (*s != sep || sep_is_special) |
| 872 | { |
| 873 | *listptr = s; |
| 874 | return string_copy(US""); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /* Not an empty string; the first character is guaranteed to be a data |
| 879 | character. */ |
| 880 | |
| 881 | for (;;) |
| 882 | { |
| 883 | for (ss = s + 1; *ss != 0 && *ss != sep; ss++); |
| 884 | buffer = string_cat(buffer, &size, &ptr, s, ss-s); |
| 885 | s = ss; |
| 886 | if (*s == 0 || *(++s) != sep || sep_is_special) break; |
| 887 | } |
| 888 | while (ptr > 0 && isspace(buffer[ptr-1])) ptr--; |
| 889 | buffer[ptr] = 0; |
| 890 | } |
| 891 | |
| 892 | /* Update the current pointer and return the new string */ |
| 893 | |
| 894 | *listptr = s; |
| 895 | return buffer; |
| 896 | } |
| 897 | #endif /* COMPILE_UTILITY */ |
| 898 | |
| 899 | |
| 900 | |
| 901 | #ifndef COMPILE_UTILITY |
| 902 | /************************************************* |
| 903 | * Add chars to string * |
| 904 | *************************************************/ |
| 905 | |
| 906 | /* This function is used when building up strings of unknown length. Room is |
| 907 | always left for a terminating zero to be added to the string that is being |
| 908 | built. This function does not require the string that is being added to be NUL |
| 909 | terminated, because the number of characters to add is given explicitly. It is |
| 910 | sometimes called to extract parts of other strings. |
| 911 | |
| 912 | Arguments: |
| 913 | string points to the start of the string that is being built, or NULL |
| 914 | if this is a new string that has no contents yet |
| 915 | size points to a variable that holds the current capacity of the memory |
| 916 | block (updated if changed) |
| 917 | ptr points to a variable that holds the offset at which to add |
| 918 | characters, updated to the new offset |
| 919 | s points to characters to add |
| 920 | count count of characters to add; must not exceed the length of s, if s |
| 921 | is a C string |
| 922 | |
| 923 | If string is given as NULL, *size and *ptr should both be zero. |
| 924 | |
| 925 | Returns: pointer to the start of the string, changed if copied for expansion. |
| 926 | Note that a NUL is not added, though space is left for one. This is |
| 927 | because string_cat() is often called multiple times to build up a |
| 928 | string - there's no point adding the NUL till the end. |
| 929 | */ |
| 930 | |
| 931 | uschar * |
| 932 | string_cat(uschar *string, int *size, int *ptr, const uschar *s, int count) |
| 933 | { |
| 934 | int p = *ptr; |
| 935 | |
| 936 | if (p + count >= *size) |
| 937 | { |
| 938 | int oldsize = *size; |
| 939 | |
| 940 | /* Mostly, string_cat() is used to build small strings of a few hundred |
| 941 | characters at most. There are times, however, when the strings are very much |
| 942 | longer (for example, a lookup that returns a vast number of alias addresses). |
| 943 | To try to keep things reasonable, we use increments whose size depends on the |
| 944 | existing length of the string. */ |
| 945 | |
| 946 | int inc = (oldsize < 4096)? 100 : 1024; |
| 947 | while (*size <= p + count) *size += inc; |
| 948 | |
| 949 | /* New string */ |
| 950 | |
| 951 | if (string == NULL) string = store_get(*size); |
| 952 | |
| 953 | /* Try to extend an existing allocation. If the result of calling |
| 954 | store_extend() is false, either there isn't room in the current memory block, |
| 955 | or this string is not the top item on the dynamic store stack. We then have |
| 956 | to get a new chunk of store and copy the old string. When building large |
| 957 | strings, it is helpful to call store_release() on the old string, to release |
| 958 | memory blocks that have become empty. (The block will be freed if the string |
| 959 | is at its start.) However, we can do this only if we know that the old string |
| 960 | was the last item on the dynamic memory stack. This is the case if it matches |
| 961 | store_last_get. */ |
| 962 | |
| 963 | else if (!store_extend(string, oldsize, *size)) |
| 964 | { |
| 965 | BOOL release_ok = store_last_get[store_pool] == string; |
| 966 | uschar *newstring = store_get(*size); |
| 967 | memcpy(newstring, string, p); |
| 968 | if (release_ok) store_release(string); |
| 969 | string = newstring; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* Because we always specify the exact number of characters to copy, we can |
| 974 | use memcpy(), which is likely to be more efficient than strncopy() because the |
| 975 | latter has to check for zero bytes. */ |
| 976 | |
| 977 | memcpy(string + p, s, count); |
| 978 | *ptr = p + count; |
| 979 | return string; |
| 980 | } |
| 981 | #endif /* COMPILE_UTILITY */ |
| 982 | |
| 983 | |
| 984 | |
| 985 | #ifndef COMPILE_UTILITY |
| 986 | /************************************************* |
| 987 | * Append strings to another string * |
| 988 | *************************************************/ |
| 989 | |
| 990 | /* This function can be used to build a string from many other strings. |
| 991 | It calls string_cat() to do the dirty work. |
| 992 | |
| 993 | Arguments: |
| 994 | string points to the start of the string that is being built, or NULL |
| 995 | if this is a new string that has no contents yet |
| 996 | size points to a variable that holds the current capacity of the memory |
| 997 | block (updated if changed) |
| 998 | ptr points to a variable that holds the offset at which to add |
| 999 | characters, updated to the new offset |
| 1000 | count the number of strings to append |
| 1001 | ... "count" uschar* arguments, which must be valid zero-terminated |
| 1002 | C strings |
| 1003 | |
| 1004 | Returns: pointer to the start of the string, changed if copied for expansion. |
| 1005 | The string is not zero-terminated - see string_cat() above. |
| 1006 | */ |
| 1007 | |
| 1008 | uschar * |
| 1009 | string_append(uschar *string, int *size, int *ptr, int count, ...) |
| 1010 | { |
| 1011 | va_list ap; |
| 1012 | int i; |
| 1013 | |
| 1014 | va_start(ap, count); |
| 1015 | for (i = 0; i < count; i++) |
| 1016 | { |
| 1017 | uschar *t = va_arg(ap, uschar *); |
| 1018 | string = string_cat(string, size, ptr, t, Ustrlen(t)); |
| 1019 | } |
| 1020 | va_end(ap); |
| 1021 | |
| 1022 | return string; |
| 1023 | } |
| 1024 | #endif |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | /************************************************* |
| 1029 | * Format a string with length checks * |
| 1030 | *************************************************/ |
| 1031 | |
| 1032 | /* This function is used to format a string with checking of the length of the |
| 1033 | output for all conversions. It protects Exim from absent-mindedness when |
| 1034 | calling functions like debug_printf and string_sprintf, and elsewhere. There |
| 1035 | are two different entry points to what is actually the same function, depending |
| 1036 | on whether the variable length list of data arguments are given explicitly or |
| 1037 | as a va_list item. |
| 1038 | |
| 1039 | The formats are the usual printf() ones, with some omissions (never used) and |
| 1040 | two additions for strings: %S forces lower case, and %#s or %#S prints nothing |
| 1041 | for a NULL string. Without the # "NULL" is printed (useful in debugging). There |
| 1042 | is also the addition of %D and %M, which insert the date in the form used for |
| 1043 | datestamped log files. |
| 1044 | |
| 1045 | Arguments: |
| 1046 | buffer a buffer in which to put the formatted string |
| 1047 | buflen the length of the buffer |
| 1048 | format the format string - deliberately char * and not uschar * |
| 1049 | ... or ap variable list of supplementary arguments |
| 1050 | |
| 1051 | Returns: TRUE if the result fitted in the buffer |
| 1052 | */ |
| 1053 | |
| 1054 | BOOL |
| 1055 | string_format(uschar *buffer, int buflen, const char *format, ...) |
| 1056 | { |
| 1057 | BOOL yield; |
| 1058 | va_list ap; |
| 1059 | va_start(ap, format); |
| 1060 | yield = string_vformat(buffer, buflen, format, ap); |
| 1061 | va_end(ap); |
| 1062 | return yield; |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | BOOL |
| 1067 | string_vformat(uschar *buffer, int buflen, const char *format, va_list ap) |
| 1068 | { |
| 1069 | enum { L_NORMAL, L_SHORT, L_LONG, L_LONGLONG, L_LONGDOUBLE }; |
| 1070 | |
| 1071 | BOOL yield = TRUE; |
| 1072 | int width, precision; |
| 1073 | const char *fp = format; /* Deliberately not unsigned */ |
| 1074 | uschar *p = buffer; |
| 1075 | uschar *last = buffer + buflen - 1; |
| 1076 | |
| 1077 | string_datestamp_offset = -1; /* Datestamp not inserted */ |
| 1078 | string_datestamp_length = 0; /* Datestamp not inserted */ |
| 1079 | string_datestamp_type = 0; /* Datestamp not inserted */ |
| 1080 | |
| 1081 | /* Scan the format and handle the insertions */ |
| 1082 | |
| 1083 | while (*fp != 0) |
| 1084 | { |
| 1085 | int length = L_NORMAL; |
| 1086 | int *nptr; |
| 1087 | int slen; |
| 1088 | const char *null = "NULL"; /* ) These variables */ |
| 1089 | const char *item_start, *s; /* ) are deliberately */ |
| 1090 | char newformat[16]; /* ) not unsigned */ |
| 1091 | |
| 1092 | /* Non-% characters just get copied verbatim */ |
| 1093 | |
| 1094 | if (*fp != '%') |
| 1095 | { |
| 1096 | if (p >= last) { yield = FALSE; break; } |
| 1097 | *p++ = (uschar)*fp++; |
| 1098 | continue; |
| 1099 | } |
| 1100 | |
| 1101 | /* Deal with % characters. Pick off the width and precision, for checking |
| 1102 | strings, skipping over the flag and modifier characters. */ |
| 1103 | |
| 1104 | item_start = fp; |
| 1105 | width = precision = -1; |
| 1106 | |
| 1107 | if (strchr("-+ #0", *(++fp)) != NULL) |
| 1108 | { |
| 1109 | if (*fp == '#') null = ""; |
| 1110 | fp++; |
| 1111 | } |
| 1112 | |
| 1113 | if (isdigit((uschar)*fp)) |
| 1114 | { |
| 1115 | width = *fp++ - '0'; |
| 1116 | while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0'; |
| 1117 | } |
| 1118 | else if (*fp == '*') |
| 1119 | { |
| 1120 | width = va_arg(ap, int); |
| 1121 | fp++; |
| 1122 | } |
| 1123 | |
| 1124 | if (*fp == '.') |
| 1125 | { |
| 1126 | if (*(++fp) == '*') |
| 1127 | { |
| 1128 | precision = va_arg(ap, int); |
| 1129 | fp++; |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | precision = 0; |
| 1134 | while (isdigit((uschar)*fp)) |
| 1135 | precision = precision*10 + *fp++ - '0'; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* Skip over 'h', 'L', 'l', and 'll', remembering the item length */ |
| 1140 | |
| 1141 | if (*fp == 'h') |
| 1142 | { fp++; length = L_SHORT; } |
| 1143 | else if (*fp == 'L') |
| 1144 | { fp++; length = L_LONGDOUBLE; } |
| 1145 | else if (*fp == 'l') |
| 1146 | { |
| 1147 | if (fp[1] == 'l') |
| 1148 | { |
| 1149 | fp += 2; |
| 1150 | length = L_LONGLONG; |
| 1151 | } |
| 1152 | else |
| 1153 | { |
| 1154 | fp++; |
| 1155 | length = L_LONG; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | /* Handle each specific format type. */ |
| 1160 | |
| 1161 | switch (*fp++) |
| 1162 | { |
| 1163 | case 'n': |
| 1164 | nptr = va_arg(ap, int *); |
| 1165 | *nptr = p - buffer; |
| 1166 | break; |
| 1167 | |
| 1168 | case 'd': |
| 1169 | case 'o': |
| 1170 | case 'u': |
| 1171 | case 'x': |
| 1172 | case 'X': |
| 1173 | if (p >= last - ((length > L_LONG)? 24 : 12)) |
| 1174 | { yield = FALSE; goto END_FORMAT; } |
| 1175 | strncpy(newformat, item_start, fp - item_start); |
| 1176 | newformat[fp - item_start] = 0; |
| 1177 | |
| 1178 | /* Short int is promoted to int when passing through ..., so we must use |
| 1179 | int for va_arg(). */ |
| 1180 | |
| 1181 | switch(length) |
| 1182 | { |
| 1183 | case L_SHORT: |
| 1184 | case L_NORMAL: sprintf(CS p, newformat, va_arg(ap, int)); break; |
| 1185 | case L_LONG: sprintf(CS p, newformat, va_arg(ap, long int)); break; |
| 1186 | case L_LONGLONG: sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break; |
| 1187 | } |
| 1188 | while (*p) p++; |
| 1189 | break; |
| 1190 | |
| 1191 | case 'p': |
| 1192 | if (p >= last - 24) { yield = FALSE; goto END_FORMAT; } |
| 1193 | strncpy(newformat, item_start, fp - item_start); |
| 1194 | newformat[fp - item_start] = 0; |
| 1195 | sprintf(CS p, newformat, va_arg(ap, void *)); |
| 1196 | while (*p) p++; |
| 1197 | break; |
| 1198 | |
| 1199 | /* %f format is inherently insecure if the numbers that it may be |
| 1200 | handed are unknown (e.g. 1e300). However, in Exim, %f is used for |
| 1201 | printing load averages, and these are actually stored as integers |
| 1202 | (load average * 1000) so the size of the numbers is constrained. |
| 1203 | It is also used for formatting sending rates, where the simplicity |
| 1204 | of the format prevents overflow. */ |
| 1205 | |
| 1206 | case 'f': |
| 1207 | case 'e': |
| 1208 | case 'E': |
| 1209 | case 'g': |
| 1210 | case 'G': |
| 1211 | if (precision < 0) precision = 6; |
| 1212 | if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; } |
| 1213 | strncpy(newformat, item_start, fp - item_start); |
| 1214 | newformat[fp-item_start] = 0; |
| 1215 | if (length == L_LONGDOUBLE) |
| 1216 | sprintf(CS p, newformat, va_arg(ap, long double)); |
| 1217 | else |
| 1218 | sprintf(CS p, newformat, va_arg(ap, double)); |
| 1219 | while (*p) p++; |
| 1220 | break; |
| 1221 | |
| 1222 | /* String types */ |
| 1223 | |
| 1224 | case '%': |
| 1225 | if (p >= last) { yield = FALSE; goto END_FORMAT; } |
| 1226 | *p++ = '%'; |
| 1227 | break; |
| 1228 | |
| 1229 | case 'c': |
| 1230 | if (p >= last) { yield = FALSE; goto END_FORMAT; } |
| 1231 | *p++ = va_arg(ap, int); |
| 1232 | break; |
| 1233 | |
| 1234 | case 'D': /* Insert daily datestamp for log file names */ |
| 1235 | s = CS tod_stamp(tod_log_datestamp_daily); |
| 1236 | string_datestamp_offset = p - buffer; /* Passed back via global */ |
| 1237 | string_datestamp_length = Ustrlen(s); /* Passed back via global */ |
| 1238 | string_datestamp_type = tod_log_datestamp_daily; |
| 1239 | slen = string_datestamp_length; |
| 1240 | goto INSERT_STRING; |
| 1241 | |
| 1242 | case 'M': /* Insert monthly datestamp for log file names */ |
| 1243 | s = CS tod_stamp(tod_log_datestamp_monthly); |
| 1244 | string_datestamp_offset = p - buffer; /* Passed back via global */ |
| 1245 | string_datestamp_length = Ustrlen(s); /* Passed back via global */ |
| 1246 | string_datestamp_type = tod_log_datestamp_monthly; |
| 1247 | slen = string_datestamp_length; |
| 1248 | goto INSERT_STRING; |
| 1249 | |
| 1250 | case 's': |
| 1251 | case 'S': /* Forces *lower* case */ |
| 1252 | s = va_arg(ap, char *); |
| 1253 | |
| 1254 | if (s == NULL) s = null; |
| 1255 | slen = Ustrlen(s); |
| 1256 | |
| 1257 | INSERT_STRING: /* Come to from %D or %M above */ |
| 1258 | |
| 1259 | /* If the width is specified, check that there is a precision |
| 1260 | set; if not, set it to the width to prevent overruns of long |
| 1261 | strings. */ |
| 1262 | |
| 1263 | if (width >= 0) |
| 1264 | { |
| 1265 | if (precision < 0) precision = width; |
| 1266 | } |
| 1267 | |
| 1268 | /* If a width is not specified and the precision is specified, set |
| 1269 | the width to the precision, or the string length if shorted. */ |
| 1270 | |
| 1271 | else if (precision >= 0) |
| 1272 | { |
| 1273 | width = (precision < slen)? precision : slen; |
| 1274 | } |
| 1275 | |
| 1276 | /* If neither are specified, set them both to the string length. */ |
| 1277 | |
| 1278 | else width = precision = slen; |
| 1279 | |
| 1280 | /* Check string space, and add the string to the buffer if ok. If |
| 1281 | not OK, add part of the string (debugging uses this to show as |
| 1282 | much as possible). */ |
| 1283 | |
| 1284 | if (p == last) |
| 1285 | { |
| 1286 | yield = FALSE; |
| 1287 | goto END_FORMAT; |
| 1288 | } |
| 1289 | if (p >= last - width) |
| 1290 | { |
| 1291 | yield = FALSE; |
| 1292 | width = precision = last - p - 1; |
| 1293 | if (width < 0) width = 0; |
| 1294 | if (precision < 0) precision = 0; |
| 1295 | } |
| 1296 | sprintf(CS p, "%*.*s", width, precision, s); |
| 1297 | if (fp[-1] == 'S') |
| 1298 | while (*p) { *p = tolower(*p); p++; } |
| 1299 | else |
| 1300 | while (*p) p++; |
| 1301 | if (!yield) goto END_FORMAT; |
| 1302 | break; |
| 1303 | |
| 1304 | /* Some things are never used in Exim; also catches junk. */ |
| 1305 | |
| 1306 | default: |
| 1307 | strncpy(newformat, item_start, fp - item_start); |
| 1308 | newformat[fp-item_start] = 0; |
| 1309 | log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type " |
| 1310 | "in \"%s\" in \"%s\"", newformat, format); |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | /* Ensure string is complete; return TRUE if got to the end of the format */ |
| 1316 | |
| 1317 | END_FORMAT: |
| 1318 | |
| 1319 | *p = 0; |
| 1320 | return yield; |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | #ifndef COMPILE_UTILITY |
| 1326 | /************************************************* |
| 1327 | * Generate an "open failed" message * |
| 1328 | *************************************************/ |
| 1329 | |
| 1330 | /* This function creates a message after failure to open a file. It includes a |
| 1331 | string supplied as data, adds the strerror() text, and if the failure was |
| 1332 | "Permission denied", reads and includes the euid and egid. |
| 1333 | |
| 1334 | Arguments: |
| 1335 | eno the value of errno after the failure |
| 1336 | format a text format string - deliberately not uschar * |
| 1337 | ... arguments for the format string |
| 1338 | |
| 1339 | Returns: a message, in dynamic store |
| 1340 | */ |
| 1341 | |
| 1342 | uschar * |
| 1343 | string_open_failed(int eno, const char *format, ...) |
| 1344 | { |
| 1345 | va_list ap; |
| 1346 | uschar buffer[1024]; |
| 1347 | |
| 1348 | Ustrcpy(buffer, "failed to open "); |
| 1349 | va_start(ap, format); |
| 1350 | |
| 1351 | /* Use the checked formatting routine to ensure that the buffer |
| 1352 | does not overflow. It should not, since this is called only for internally |
| 1353 | specified messages. If it does, the message just gets truncated, and there |
| 1354 | doesn't seem much we can do about that. */ |
| 1355 | |
| 1356 | (void)string_vformat(buffer+15, sizeof(buffer) - 15, format, ap); |
| 1357 | |
| 1358 | return (eno == EACCES)? |
| 1359 | string_sprintf("%s: %s (euid=%ld egid=%ld)", buffer, strerror(eno), |
| 1360 | (long int)geteuid(), (long int)getegid()) : |
| 1361 | string_sprintf("%s: %s", buffer, strerror(eno)); |
| 1362 | } |
| 1363 | #endif /* COMPILE_UTILITY */ |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | #ifndef COMPILE_UTILITY |
| 1368 | /************************************************* |
| 1369 | * Generate local prt for logging * |
| 1370 | *************************************************/ |
| 1371 | |
| 1372 | /* This function is a subroutine for use in string_log_address() below. |
| 1373 | |
| 1374 | Arguments: |
| 1375 | addr the address being logged |
| 1376 | yield the current dynamic buffer pointer |
| 1377 | sizeptr points to current size |
| 1378 | ptrptr points to current insert pointer |
| 1379 | |
| 1380 | Returns: the new value of the buffer pointer |
| 1381 | */ |
| 1382 | |
| 1383 | static uschar * |
| 1384 | string_get_localpart(address_item *addr, uschar *yield, int *sizeptr, |
| 1385 | int *ptrptr) |
| 1386 | { |
| 1387 | if (testflag(addr, af_include_affixes) && addr->prefix != NULL) |
| 1388 | yield = string_cat(yield, sizeptr, ptrptr, addr->prefix, |
| 1389 | Ustrlen(addr->prefix)); |
| 1390 | yield = string_cat(yield, sizeptr, ptrptr, addr->local_part, |
| 1391 | Ustrlen(addr->local_part)); |
| 1392 | if (testflag(addr, af_include_affixes) && addr->suffix != NULL) |
| 1393 | yield = string_cat(yield, sizeptr, ptrptr, addr->suffix, |
| 1394 | Ustrlen(addr->suffix)); |
| 1395 | return yield; |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | /************************************************* |
| 1400 | * Generate log address list * |
| 1401 | *************************************************/ |
| 1402 | |
| 1403 | /* This function generates a list consisting of an address and its parents, for |
| 1404 | use in logging lines. For saved onetime aliased addresses, the onetime parent |
| 1405 | field is used. If the address was delivered by a transport with rcpt_include_ |
| 1406 | affixes set, the af_include_affixes bit will be set in the address. In that |
| 1407 | case, we include the affixes here too. |
| 1408 | |
| 1409 | Arguments: |
| 1410 | addr bottom (ultimate) address |
| 1411 | all_parents if TRUE, include all parents |
| 1412 | success TRUE for successful delivery |
| 1413 | |
| 1414 | Returns: a string in dynamic store |
| 1415 | */ |
| 1416 | |
| 1417 | uschar * |
| 1418 | string_log_address(address_item *addr, BOOL all_parents, BOOL success) |
| 1419 | { |
| 1420 | int size = 64; |
| 1421 | int ptr = 0; |
| 1422 | BOOL add_topaddr = TRUE; |
| 1423 | uschar *yield = store_get(size); |
| 1424 | address_item *topaddr; |
| 1425 | |
| 1426 | /* Find the ultimate parent */ |
| 1427 | |
| 1428 | for (topaddr = addr; topaddr->parent != NULL; topaddr = topaddr->parent); |
| 1429 | |
| 1430 | /* We start with just the local part for pipe, file, and reply deliveries, and |
| 1431 | for successful local deliveries from routers that have the log_as_local flag |
| 1432 | set. File deliveries from filters can be specified as non-absolute paths in |
| 1433 | cases where the transport is goin to complete the path. If there is an error |
| 1434 | before this happens (expansion failure) the local part will not be updated, and |
| 1435 | so won't necessarily look like a path. Add extra text for this case. */ |
| 1436 | |
| 1437 | if (testflag(addr, af_pfr) || |
| 1438 | (success && |
| 1439 | addr->router != NULL && addr->router->log_as_local && |
| 1440 | addr->transport != NULL && addr->transport->info->local)) |
| 1441 | { |
| 1442 | if (testflag(addr, af_file) && addr->local_part[0] != '/') |
| 1443 | yield = string_cat(yield, &size, &ptr, CUS"save ", 5); |
| 1444 | yield = string_get_localpart(addr, yield, &size, &ptr); |
| 1445 | } |
| 1446 | |
| 1447 | /* Other deliveries start with the full address. It we have split it into local |
| 1448 | part and domain, use those fields. Some early failures can happen before the |
| 1449 | splitting is done; in those cases use the original field. */ |
| 1450 | |
| 1451 | else |
| 1452 | { |
| 1453 | if (addr->local_part != NULL) |
| 1454 | { |
| 1455 | yield = string_get_localpart(addr, yield, &size, &ptr); |
| 1456 | yield = string_cat(yield, &size, &ptr, US"@", 1); |
| 1457 | yield = string_cat(yield, &size, &ptr, addr->domain, |
| 1458 | Ustrlen(addr->domain) ); |
| 1459 | } |
| 1460 | else |
| 1461 | { |
| 1462 | yield = string_cat(yield, &size, &ptr, addr->address, Ustrlen(addr->address)); |
| 1463 | } |
| 1464 | yield[ptr] = 0; |
| 1465 | |
| 1466 | /* If the address we are going to print is the same as the top address, |
| 1467 | and all parents are not being included, don't add on the top address. First |
| 1468 | of all, do a caseless comparison; if this succeeds, do a caseful comparison |
| 1469 | on the local parts. */ |
| 1470 | |
| 1471 | if (strcmpic(yield, topaddr->address) == 0 && |
| 1472 | Ustrncmp(yield, topaddr->address, Ustrchr(yield, '@') - yield) == 0 && |
| 1473 | addr->onetime_parent == NULL && |
| 1474 | (!all_parents || addr->parent == NULL || addr->parent == topaddr)) |
| 1475 | add_topaddr = FALSE; |
| 1476 | } |
| 1477 | |
| 1478 | /* If all parents are requested, or this is a local pipe/file/reply, and |
| 1479 | there is at least one intermediate parent, show it in brackets, and continue |
| 1480 | with all of them if all are wanted. */ |
| 1481 | |
| 1482 | if ((all_parents || testflag(addr, af_pfr)) && |
| 1483 | addr->parent != NULL && |
| 1484 | addr->parent != topaddr) |
| 1485 | { |
| 1486 | uschar *s = US" ("; |
| 1487 | address_item *addr2; |
| 1488 | for (addr2 = addr->parent; addr2 != topaddr; addr2 = addr2->parent) |
| 1489 | { |
| 1490 | yield = string_cat(yield, &size, &ptr, s, 2); |
| 1491 | yield = string_cat(yield, &size, &ptr, addr2->address, Ustrlen(addr2->address)); |
| 1492 | if (!all_parents) break; |
| 1493 | s = US", "; |
| 1494 | } |
| 1495 | yield = string_cat(yield, &size, &ptr, US")", 1); |
| 1496 | } |
| 1497 | |
| 1498 | /* Add the top address if it is required */ |
| 1499 | |
| 1500 | if (add_topaddr) |
| 1501 | { |
| 1502 | yield = string_cat(yield, &size, &ptr, US" <", 2); |
| 1503 | |
| 1504 | if (addr->onetime_parent == NULL) |
| 1505 | yield = string_cat(yield, &size, &ptr, topaddr->address, |
| 1506 | Ustrlen(topaddr->address)); |
| 1507 | else |
| 1508 | yield = string_cat(yield, &size, &ptr, addr->onetime_parent, |
| 1509 | Ustrlen(addr->onetime_parent)); |
| 1510 | |
| 1511 | yield = string_cat(yield, &size, &ptr, US">", 1); |
| 1512 | } |
| 1513 | |
| 1514 | yield[ptr] = 0; /* string_cat() leaves space */ |
| 1515 | return yield; |
| 1516 | } |
| 1517 | #endif /* COMPILE_UTILITY */ |
| 1518 | |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | /************************************************* |
| 1524 | ************************************************** |
| 1525 | * Stand-alone test program * |
| 1526 | ************************************************** |
| 1527 | *************************************************/ |
| 1528 | |
| 1529 | #ifdef STAND_ALONE |
| 1530 | int main(void) |
| 1531 | { |
| 1532 | uschar buffer[256]; |
| 1533 | |
| 1534 | printf("Testing is_ip_address\n"); |
| 1535 | |
| 1536 | while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) |
| 1537 | { |
| 1538 | int offset; |
| 1539 | buffer[Ustrlen(buffer) - 1] = 0; |
| 1540 | printf("%d\n", string_is_ip_address(buffer, NULL)); |
| 1541 | printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer); |
| 1542 | } |
| 1543 | |
| 1544 | printf("Testing string_nextinlist\n"); |
| 1545 | |
| 1546 | while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) |
| 1547 | { |
| 1548 | uschar *list = buffer; |
| 1549 | uschar *lp1, *lp2; |
| 1550 | uschar item[256]; |
| 1551 | int sep1 = 0; |
| 1552 | int sep2 = 0; |
| 1553 | |
| 1554 | if (*list == '<') |
| 1555 | { |
| 1556 | sep1 = sep2 = list[1]; |
| 1557 | list += 2; |
| 1558 | } |
| 1559 | |
| 1560 | lp1 = lp2 = list; |
| 1561 | for (;;) |
| 1562 | { |
| 1563 | uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item)); |
| 1564 | uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0); |
| 1565 | |
| 1566 | if (item1 == NULL && item2 == NULL) break; |
| 1567 | if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0) |
| 1568 | { |
| 1569 | printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n", |
| 1570 | (item1 == NULL)? "NULL" : CS item1, |
| 1571 | (item2 == NULL)? "NULL" : CS item2); |
| 1572 | break; |
| 1573 | } |
| 1574 | else printf(" \"%s\"\n", CS item1); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | /* This is a horrible lash-up, but it serves its purpose. */ |
| 1579 | |
| 1580 | printf("Testing string_format\n"); |
| 1581 | |
| 1582 | while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) |
| 1583 | { |
| 1584 | void *args[3]; |
| 1585 | long long llargs[3]; |
| 1586 | double dargs[3]; |
| 1587 | int dflag = 0; |
| 1588 | int llflag = 0; |
| 1589 | int n = 0; |
| 1590 | int count; |
| 1591 | int countset = 0; |
| 1592 | uschar format[256]; |
| 1593 | uschar outbuf[256]; |
| 1594 | uschar *s; |
| 1595 | buffer[Ustrlen(buffer) - 1] = 0; |
| 1596 | |
| 1597 | s = Ustrchr(buffer, ','); |
| 1598 | if (s == NULL) s = buffer + Ustrlen(buffer); |
| 1599 | |
| 1600 | Ustrncpy(format, buffer, s - buffer); |
| 1601 | format[s-buffer] = 0; |
| 1602 | |
| 1603 | if (*s == ',') s++; |
| 1604 | |
| 1605 | while (*s != 0) |
| 1606 | { |
| 1607 | uschar *ss = s; |
| 1608 | s = Ustrchr(ss, ','); |
| 1609 | if (s == NULL) s = ss + Ustrlen(ss); |
| 1610 | |
| 1611 | if (isdigit(*ss)) |
| 1612 | { |
| 1613 | Ustrncpy(outbuf, ss, s-ss); |
| 1614 | if (Ustrchr(outbuf, '.') != NULL) |
| 1615 | { |
| 1616 | dflag = 1; |
| 1617 | dargs[n++] = Ustrtod(outbuf, NULL); |
| 1618 | } |
| 1619 | else if (Ustrstr(outbuf, "ll") != NULL) |
| 1620 | { |
| 1621 | llflag = 1; |
| 1622 | llargs[n++] = strtoull(CS outbuf, NULL, 10); |
| 1623 | } |
| 1624 | else |
| 1625 | { |
| 1626 | args[n++] = (void *)Uatoi(outbuf); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | else if (Ustrcmp(ss, "*") == 0) |
| 1631 | { |
| 1632 | args[n++] = (void *)(&count); |
| 1633 | countset = 1; |
| 1634 | } |
| 1635 | |
| 1636 | else |
| 1637 | { |
| 1638 | uschar *sss = malloc(s - ss + 1); |
| 1639 | Ustrncpy(sss, ss, s-ss); |
| 1640 | args[n++] = sss; |
| 1641 | } |
| 1642 | |
| 1643 | if (*s == ',') s++; |
| 1644 | } |
| 1645 | |
| 1646 | if (!dflag && !llflag) |
| 1647 | printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format, |
| 1648 | args[0], args[1], args[2])? "True" : "False"); |
| 1649 | |
| 1650 | else if (dflag) |
| 1651 | printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format, |
| 1652 | dargs[0], dargs[1], dargs[2])? "True" : "False"); |
| 1653 | |
| 1654 | else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format, |
| 1655 | llargs[0], llargs[1], llargs[2])? "True" : "False"); |
| 1656 | |
| 1657 | printf("%s\n", CS outbuf); |
| 1658 | if (countset) printf("count=%d\n", count); |
| 1659 | } |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | #endif |
| 1664 | |
| 1665 | /* End of string.c */ |