1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2024 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 /* Miscellaneous string-handling functions. Some are not required for
11 utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
18 #ifndef COMPILE_UTILITY
19 /*************************************************
20 * Test for IP address *
21 *************************************************/
23 /* This used just to be a regular expression, but with IPv6 things are a bit
24 more complicated. If the address contains a colon, it is assumed to be a v6
25 address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present,
26 and maskptr is not NULL, its offset is placed there.
30 maskptr NULL if no mask is permitted to follow
31 otherwise, points to an int where the offset of '/' is placed
32 if there is no / followed by trailing digits, *maskptr is set 0
33 errp NULL if no diagnostic information is required, and if the netmask
34 length should not be checked. Otherwise it is set pointing to a short
37 Returns: 0 if the string is not a textual representation of an IP address
38 4 if it is an IPv4 address
39 6 if it is an IPv6 address
41 The legacy string_is_ip_address() function follows below.
45 string_is_ip_addressX(const uschar * ip_addr, int * maskptr, const uschar ** errp)
47 uschar * slash, * percent, * endp = NULL;
49 const uschar * addr = NULL;
51 union { /* we do not need this, but inet_pton() needs a place for storage */
56 /* If there is a slash, but we didn't request a (optional) netmask,
57 we return failure, as we do if the mask isn't a pure numerical value,
58 or if it is negative. The actual length is checked later, once we know
59 the address family. */
61 if (slash = Ustrchr(ip_addr, '/'))
67 if (errp) *errp = US"netmask found, but not requested";
71 mask = Ustrtol(slash+1, &rest, 10);
72 if (*rest || mask < 0)
74 if (errp) *errp = US"netmask not numeric or <0";
78 *maskptr = slash - ip_addr; /* offset of the slash */
82 *maskptr = 0; /* no slash found */
84 /* The interface-ID suffix (%<id>) is optional (for IPv6). If it
85 exists, we check it syntactically. Later, if we know the address
86 family is IPv4, we might reject it.
87 The interface-ID is mutually exclusive with the netmask, to the
88 best of my knowledge. */
90 if (percent = Ustrchr(ip_addr, '%'))
94 if (errp) *errp = US"interface-ID and netmask are mutually exclusive";
97 for (uschar *p = percent+1; *p; p++)
98 if (!isalnum(*p) && !ispunct(*p))
100 if (errp) *errp = US"interface-ID must match [[:alnum:][:punct:]]";
106 /* inet_pton() can't parse netmasks and interface IDs, so work on a shortened copy
107 allocated on the current stack */
111 ptrdiff_t l = endp - ip_addr;
114 if (errp) *errp = US"rudiculous long ip address string";
117 addr = string_copyn(ip_addr, l);
122 af = Ustrchr(addr, ':') ? AF_INET6 : AF_INET;
123 if (!inet_pton(af, CCS addr, &sa))
125 if (errp) *errp = af == AF_INET6 ? US"IP address string not parsable as IPv6"
126 : US"IP address string not parsable IPv4";
130 /* we do not check the values of the mask here, as
131 this is done on the callers side (but I don't understand why), so
132 actually I'd like to do it here, but it breaks at least testcase 0002 */
137 if (errp && mask > 128)
139 *errp = US"IPv6 netmask value must not be >128";
146 if (errp) *errp = US"IPv4 address string must not have an interface-ID";
149 if (errp && mask > 32)
151 *errp = US"IPv4 netmask value must not be >32";
156 if (errp) *errp = US"unknown address family (should not happen)";
163 string_is_ip_address(const uschar * ip_addr, int * maskptr)
165 return string_is_ip_addressX(ip_addr, maskptr, NULL);
168 #endif /* COMPILE_UTILITY */
171 /*************************************************
172 * Format message size *
173 *************************************************/
175 /* Convert a message size in bytes to printing form, rounding
176 according to the magnitude of the number. A value of zero causes
177 a string of spaces to be returned.
180 size the message size in bytes
181 buffer where to put the answer
183 Returns: pointer to the buffer
184 a string of exactly 5 characters is normally returned
188 string_format_size(int size, uschar *buffer)
190 if (size == 0) Ustrcpy(buffer, US" ");
191 else if (size < 1024) sprintf(CS buffer, "%5d", size);
192 else if (size < 10*1024)
193 sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
194 else if (size < 1024*1024)
195 sprintf(CS buffer, "%4dK", (size + 512)/1024);
196 else if (size < 10*1024*1024)
197 sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
199 sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
205 #ifndef COMPILE_UTILITY
206 /*************************************************
207 * Convert a number to base 62 format *
208 *************************************************/
210 /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
211 BASE_62 is actually 36. Always return exactly 6 characters plus a NUL, in a
212 static area. This is enough for a 32b input, for 62 (for 64b we would want 11+nul);
213 but with 36 we lose half the input range of a 32b input.
215 Argument: a long integer
216 Returns: pointer to base 62 string
220 string_base62_32(unsigned long int value)
222 static uschar yield[7];
223 uschar * p = yield + sizeof(yield) - 1;
227 *--p = base62_chars[value % BASE_62];
234 string_base62_64(unsigned long int value)
236 static uschar yield[12];
237 uschar * p = yield + sizeof(yield) - 1;
242 *--p = base62_chars[value % BASE_62];
249 #endif /* COMPILE_UTILITY */
253 /*************************************************
254 * Interpret escape sequence *
255 *************************************************/
257 /* This function is called from several places where escape sequences are to be
258 interpreted in strings.
261 pp points a pointer to the initiating "\" in the string;
262 the pointer gets updated to point to the final character
263 If the backslash is the last character in the string, it
265 Returns: the value of the character escape
269 string_interpret_escape(const uschar **pp)
271 #ifdef COMPILE_UTILITY
272 const uschar *hex_digits= CUS"0123456789abcdef";
275 const uschar *p = *pp;
277 if (ch == '\0') return **pp;
278 if (isdigit(ch) && ch != '8' && ch != '9')
281 if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
283 ch = ch * 8 + *(++p) - '0';
284 if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
285 ch = ch * 8 + *(++p) - '0';
290 case 'b': ch = '\b'; break;
291 case 'f': ch = '\f'; break;
292 case 'n': ch = '\n'; break;
293 case 'r': ch = '\r'; break;
294 case 't': ch = '\t'; break;
295 case 'v': ch = '\v'; break;
301 Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
302 if (isxdigit(p[1])) ch = ch * 16 +
303 Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
313 #ifndef COMPILE_UTILITY
314 /*************************************************
315 * Ensure string is printable *
316 *************************************************/
318 /* This function is called for critical strings. It checks for any
319 non-printing characters, and if any are found, it makes a new copy
320 of the string with suitable escape sequences. It is most often called by the
321 macro string_printing(), which sets flags to 0.
325 flags Bit 0: convert tabs. Bit 1: convert spaces.
327 Returns: string with non-printers encoded as printing sequences
331 string_printing2(const uschar *s, int flags)
333 int nonprintcount = 0;
342 || flags & SP_TAB && c == '\t'
343 || flags & SP_SPACE && c == ' '
348 if (nonprintcount == 0) return s;
350 /* Get a new block of store guaranteed big enough to hold the
353 tt = ss = store_get(length + nonprintcount * 3 + 1, s);
355 /* Copy everything, escaping non printers. */
361 && (!(flags & SP_TAB) || c != '\t')
362 && (!(flags & SP_SPACE) || c != ' ')
370 case '\n': *tt++ = 'n'; break;
371 case '\r': *tt++ = 'r'; break;
372 case '\b': *tt++ = 'b'; break;
373 case '\v': *tt++ = 'v'; break;
374 case '\f': *tt++ = 'f'; break;
375 case '\t': *tt++ = 't'; break;
376 default: sprintf(CS tt, "%03o", *t); tt += 3; break;
384 #endif /* COMPILE_UTILITY */
386 /*************************************************
387 * Undo printing escapes in string *
388 *************************************************/
390 /* This function is the reverse of string_printing2. It searches for
391 backslash characters and if any are found, it makes a new copy of the
392 string with escape sequences parsed. Otherwise it returns the original
398 Returns: string with printing escapes parsed back
402 string_unprinting(uschar *s)
404 uschar *p, *q, *r, *ss;
407 p = Ustrchr(s, '\\');
410 len = Ustrlen(s) + 1;
411 ss = store_get(len, s);
425 *q++ = string_interpret_escape((const uschar **)&p);
430 r = Ustrchr(p, '\\');
456 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
457 && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
458 /*************************************************
459 * Copy and save string *
460 *************************************************/
463 Argument: string to copy
464 Returns: copy of string in new store with the same taint status
468 string_copy_function(const uschar * s)
470 return string_copy_taint(s, s);
473 /* As above, but explicitly specifying the result taint status
477 string_copy_taint_function(const uschar * s, const void * proto_mem)
479 return string_copy_taint(s, proto_mem);
484 /*************************************************
485 * Copy and save string, given length *
486 *************************************************/
488 /* It is assumed the data contains no zeros. A zero is added
493 n number of characters
495 Returns: copy of string in new store
499 string_copyn_function(const uschar * s, int n)
501 return string_copyn(s, n);
506 /*************************************************
507 * Copy and save string in malloc'd store *
508 *************************************************/
510 /* This function assumes that memcpy() is faster than strcpy().
512 Argument: string to copy
513 Returns: copy of string in new store
517 string_copy_malloc(const uschar * s)
519 int len = Ustrlen(s) + 1;
520 uschar * ss = store_malloc(len);
527 /*************************************************
528 * Copy string if long, inserting newlines *
529 *************************************************/
531 /* If the given string is longer than 75 characters, it is copied, and within
532 the copy, certain space characters are converted into newlines.
534 Argument: pointer to the string
535 Returns: pointer to the possibly altered string
539 string_split_message(uschar * msg)
543 if (!msg || Ustrlen(msg) <= 75) return msg;
544 s = ss = msg = string_copy(msg);
549 while (i < 75 && *ss && *ss != '\n') ss++, i++;
561 if (t[-1] == ':') { tt = t; break; }
566 if (!tt) /* Can't split behind - try ahead */
571 if (*t == ' ' || *t == '\n')
577 if (!tt) break; /* Can't find anywhere to split */
588 /*************************************************
589 * Copy returned DNS domain name, de-escaping *
590 *************************************************/
592 /* If a domain name contains top-bit characters, some resolvers return
593 the fully qualified name with those characters turned into escapes. The
594 convention is a backslash followed by _decimal_ digits. We convert these
595 back into the original binary values. This will be relevant when
596 allow_utf8_domains is set true and UTF-8 characters are used in domain
597 names. Backslash can also be used to escape other characters, though we
598 shouldn't come across them in domain names.
600 Argument: the domain name string
601 Returns: copy of string in new store, de-escaped
605 string_copy_dnsdomain(uschar * s)
608 uschar * ss = yield = store_get(Ustrlen(s) + 1, GET_TAINTED); /* always treat as tainted */
614 else if (isdigit(s[1]))
616 *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
628 #ifndef COMPILE_UTILITY
629 /*************************************************
630 * Copy space-terminated or quoted string *
631 *************************************************/
633 /* This function copies from a string until its end, or until whitespace is
634 encountered, unless the string begins with a double quote, in which case the
635 terminating quote is sought, and escaping within the string is done. The length
636 of a de-quoted string can be no longer than the original, since escaping always
637 turns n characters into 1 character.
639 Argument: pointer to the pointer to the first character, which gets updated
640 Returns: the new string
644 string_dequote(const uschar ** sptr)
646 const uschar * s = * sptr;
649 /* First find the end of the string */
656 while (*s && *s != '\"')
658 if (*s == '\\') (void)string_interpret_escape(&s);
664 /* Get enough store to copy into */
666 t = yield = store_get(s - *sptr + 1, *sptr);
672 while (*s && !isspace(*s)) *t++ = *s++;
676 while (*s && *s != '\"')
678 *t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
684 /* Update the pointer and return the terminated copy */
690 #endif /* COMPILE_UTILITY */
694 /*************************************************
695 * Format a string and save it *
696 *************************************************/
698 /* The formatting is done by string_vformat, which checks the length of
699 everything. Taint is taken from the worst of the arguments.
702 format a printf() format - deliberately char * rather than uschar *
703 because it will most usually be a literal string
704 func caller, for debug
705 line caller, for debug
706 ... arguments for format
708 Returns: pointer to fresh piece of store containing sprintf'ed string
712 string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
714 #ifdef COMPILE_UTILITY
715 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
716 gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
721 unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
726 g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
731 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
732 "string_sprintf expansion was longer than %d; format string was (%s)\n"
733 " called from %s %d\n",
734 STRING_SPRINTF_BUFFER_SIZE, format, func, line);
736 #ifdef COMPILE_UTILITY
737 return string_copyn(g->s, g->ptr);
739 gstring_release_unused(g);
740 return string_from_gstring(g);
746 /*************************************************
747 * Case-independent strncmp() function *
748 *************************************************/
754 n number of characters to compare
756 Returns: < 0, = 0, or > 0, according to the comparison
760 strncmpic(const uschar * s, const uschar * t, int n)
764 int c = tolower(*s++) - tolower(*t++);
771 /*************************************************
772 * Case-independent strcmp() function *
773 *************************************************/
780 Returns: < 0, = 0, or > 0, according to the comparison
784 strcmpic(const uschar * s, const uschar * t)
788 int c = tolower(*s++) - tolower(*t++);
789 if (c != 0) return c;
795 /*************************************************
796 * Case-independent strstr() function *
797 *************************************************/
799 /* The third argument specifies whether whitespace is required
800 to follow the matched string.
804 t substring to search for
805 space_follows if TRUE, match only if whitespace follows
807 Returns: pointer to substring in string, or NULL if not found
811 strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
813 const uschar * p = t;
814 const uschar * yield = NULL;
815 int cl = tolower(*p);
816 int cu = toupper(*p);
820 if (*s == cl || *s == cu)
822 if (!yield) yield = s;
825 if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
846 strstric(uschar * s, uschar * t, BOOL space_follows)
848 return US strstric_c(s, t, space_follows);
852 #ifdef COMPILE_UTILITY
853 /* Dummy version for this function; it should never be called */
855 gstring_grow(gstring * g, int count)
863 #ifndef COMPILE_UTILITY
864 /*************************************************
865 * Get next string from separated list *
866 *************************************************/
868 /* Leading and trailing space is removed from each item. The separator in the
869 list is controlled by the int pointed to by the separator argument as follows:
871 If the value is > 0 it is used as the separator. This is typically used for
872 sublists such as slash-separated options. The value is always a printing
875 (If the value is actually > UCHAR_MAX there is only one item in the list.
876 This is used for some cases when called via functions that sometimes
877 plough through lists, and sometimes are given single items.)
879 If the value is <= 0, the string is inspected for a leading <x, where x is an
880 ispunct() or an iscntrl() character. If found, x is used as the separator. If
883 (a) if separator == 0, ':' is used
884 (b) if separator <0, -separator is used
886 In all cases the value of the separator that is used is written back to the
887 int so that it is used on subsequent calls as we progress through the list.
889 A literal ispunct() separator can be represented in an item by doubling, but
890 there is no way to include an iscntrl() separator as part of the data.
893 listptr points to a pointer to the current start of the list; the
894 pointer gets updated to point after the end of the next item
895 separator a pointer to the separator character in an int (see above)
896 buffer where to put a copy of the next string in the list; or
897 NULL if the next string is returned in new memory
898 Note that if the list is tainted then a provided buffer must be
899 also (else we trap, with a message referencing the callsite).
900 If we do the allocation, taint is handled there.
901 buflen when buffer is not NULL, the size of buffer; otherwise ignored
903 func caller, for debug
904 line caller, for debug
906 Returns: pointer to buffer, containing the next substring,
907 or NULL if no more substrings
911 string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
912 int buflen, const uschar * func, int line)
914 int sep = *separator;
915 const uschar * s = *listptr;
920 /* This allows for a fixed specified separator to be an iscntrl() character,
921 but at the time of implementation, this is never the case. However, it's best
922 to be conservative. */
924 while (isspace(*s) && *s != sep) s++;
926 /* A change of separator is permitted (assuming untainted source),
927 so look for a leading '<' followed by an allowed character. */
931 if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
936 debug_printf("attempt to use tainted change-of-seperator spec (%s %d)\n",
937 config_filename, config_lineno);
939 while (isspace(*s) && *s != sep) s++;
942 sep = sep ? -sep : ':';
946 /* An empty string has no list elements */
948 if (!*s) return NULL;
950 /* Note whether whether or not the separator is an iscntrl() character. */
952 sep_is_special = iscntrl(sep);
954 /* Handle the case when a buffer is provided. */
955 /*XXX need to also deal with qouted-requirements mismatch */
960 if (is_tainted(s) && !is_tainted(buffer))
961 die_tainted(US"string_nextinlist", func, line);
964 if (*s == sep && (*++s != sep || sep_is_special)) break;
965 if (p < buflen - 1) buffer[p++] = *s;
967 while (p > 0 && isspace(buffer[p-1])) p--;
971 /* Handle the case when a buffer is not provided. */
977 /* We know that *s != 0 at this point. However, it might be pointing to a
978 separator, which could indicate an empty string, or (if an ispunct()
979 character) could be doubled to indicate a separator character as data at the
980 start of a string. Avoid getting working memory for an empty item. */
983 if (*++s != sep || sep_is_special)
986 return string_copy(US"");
989 /* Not an empty string; the first character is guaranteed to be a data
995 for (ss = s + 1; *ss && *ss != sep; ) ss++;
996 g = string_catn(g, s, ss-s);
998 if (!*s || *++s != sep || sep_is_special) break;
1001 /* Trim trailing spaces from the returned string */
1003 /* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
1004 while ( g->ptr > 0 && isspace(g->s[g->ptr-1])
1005 && (g->ptr == 1 || g->s[g->ptr-2] != '\\') )
1007 buffer = string_from_gstring(g);
1008 gstring_release_unused_trc(g, CCS func, line);
1011 /* Update the current pointer and return the new string */
1018 static const uschar *
1019 Ustrnchr(const uschar * s, int c, unsigned * len)
1021 unsigned siz = *len;
1024 if (!*s) return NULL;
1037 /************************************************
1038 * Add element to separated list *
1039 ************************************************/
1040 /* This function is used to build a list, returning an allocated null-terminated
1041 growable string. The given element has any embedded separator characters
1044 Despite having the same growable-string interface as string_cat() the list is
1045 always returned null-terminated.
1048 list expanding-string for the list that is being built, or NULL
1049 if this is a new list that has no contents yet
1050 sep list separator character
1051 ele new element to be appended to the list
1053 Returns: pointer to the start of the list, changed if copied for expansion.
1057 string_append_listele(gstring * list, uschar sep, const uschar * ele)
1061 if (list && list->ptr)
1062 list = string_catn(list, &sep, 1);
1064 while((sp = Ustrchr(ele, sep)))
1066 list = string_catn(list, ele, sp-ele+1);
1067 list = string_catn(list, &sep, 1);
1070 list = string_cat(list, ele);
1071 (void) string_from_gstring(list);
1077 string_append_listele_n(gstring * list, uschar sep, const uschar * ele,
1082 if (list && list->ptr)
1083 list = string_catn(list, &sep, 1);
1085 while((sp = Ustrnchr(ele, sep, &len)))
1087 list = string_catn(list, ele, sp-ele+1);
1088 list = string_catn(list, &sep, 1);
1092 list = string_catn(list, ele, len);
1093 (void) string_from_gstring(list);
1099 /* Listmaker that takes a format string and args for the element.
1100 A flag arg is required to handle embedded sep chars in the (expanded) element;
1101 if false then no check is done */
1104 string_append_listele_fmt(gstring * list, uschar sep, BOOL check,
1105 const char * fmt, ...)
1111 if (list && list->ptr)
1113 list = string_catn(list, &sep, 1);
1120 list = string_vformat_trc(list, US __FUNCTION__, __LINE__,
1121 STRING_SPRINTF_BUFFER_SIZE, SVFMT_REBUFFER|SVFMT_EXTEND, fmt, ap);
1124 (void) string_from_gstring(list);
1126 /* if the appended element turns out to have an embedded sep char, rewind
1127 and do the lazy-coded separate string method */
1129 if (!check || !Ustrchr(&list->s[start], sep))
1133 g = string_vformat_trc(NULL, US __FUNCTION__, __LINE__,
1134 STRING_SPRINTF_BUFFER_SIZE, SVFMT_REBUFFER|SVFMT_EXTEND, fmt, ap);
1138 return string_append_listele_n(list, sep, g->s, g->ptr);
1142 /* A slightly-bogus listmaker utility; the separator is a string so
1143 can be multiple chars - there is no checking for the element content
1144 containing any of the separator. */
1147 string_append2_listele_n(gstring * list, const uschar * sepstr,
1148 const uschar * ele, unsigned len)
1150 if (list && list->ptr)
1151 list = string_cat(list, sepstr);
1153 list = string_catn(list, ele, len);
1154 (void) string_from_gstring(list);
1160 /************************************************/
1161 /* Add more space to a growable-string. The caller should check
1162 first if growth is required. The gstring struct is modified on
1163 return; specifically, the string-base-pointer may have been changed.
1166 g the growable-string
1167 count amount needed for g->ptr to increase by
1171 gstring_grow(gstring * g, int count)
1174 int oldsize = g->size;
1176 /* Mostly, string_cat() is used to build small strings of a few hundred
1177 characters at most. There are times, however, when the strings are very much
1178 longer (for example, a lookup that returns a vast number of alias addresses).
1179 To try to keep things reasonable, we use increments whose size depends on the
1180 existing length of the string. */
1182 unsigned inc = oldsize < 4096 ? 127 : 1023;
1184 if (g->ptr < 0 || g->ptr > g->size || g->size >= INT_MAX/2)
1185 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1186 "internal error in gstring_grow (ptr %d size %d)", g->ptr, g->size);
1188 if (count <= 0) return;
1190 if (count >= INT_MAX/2 - g->ptr)
1191 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1192 "internal error in gstring_grow (ptr %d count %d)", g->ptr, count);
1194 g->size = (p + count + inc + 1) & ~inc; /* one for a NUL */
1196 /* Try to extend an existing allocation. If the result of calling
1197 store_extend() is false, either there isn't room in the current memory block,
1198 or this string is not the top item on the dynamic store stack. We then have
1199 to get a new chunk of store and copy the old string. When building large
1200 strings, it is helpful to call store_release() on the old string, to release
1201 memory blocks that have become empty. (The block will be freed if the string
1202 is at its start.) However, we can do this only if we know that the old string
1203 was the last item on the dynamic memory stack. This is the case if it matches
1206 if (!store_extend(g->s, oldsize, g->size))
1207 g->s = store_newblock(g->s, g->size, p);
1212 /*************************************************
1213 * Add chars to string *
1214 *************************************************/
1215 /* This function is used when building up strings of unknown length. Room is
1216 always left for a terminating zero to be added to the string that is being
1217 built. This function does not require the string that is being added to be NUL
1218 terminated, because the number of characters to add is given explicitly. It is
1219 sometimes called to extract parts of other strings.
1222 g growable-string that is being built, or NULL if not assigned yet
1223 s points to characters to add
1224 count count of characters to add; must not exceed the length of s, if s
1227 Returns: growable string, changed if copied for expansion.
1228 Note that a NUL is not added, though space is left for one. This is
1229 because string_cat() is often called multiple times to build up a
1230 string - there's no point adding the NUL till the end.
1231 NULL is a possible return.
1234 /* coverity[+alloc] */
1237 string_catn(gstring * g, const uschar * s, int count)
1242 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1243 "internal error in string_catn (count %d)", count);
1244 if (count == 0) return g;
1246 /*debug_printf("string_catn '%.*s'\n", count, s);*/
1249 unsigned inc = count < 4096 ? 127 : 1023;
1250 unsigned size = ((count + inc) & ~inc) + 1; /* round up requested count */
1251 g = string_get_tainted(size, s);
1253 else if (!g->s) /* should not happen */
1255 g->s = string_copyn(s, count);
1257 g->size = count; /*XXX suboptimal*/
1260 else if (is_incompatible(g->s, s))
1262 /* debug_printf("rebuf A\n"); */
1263 gstring_rebuffer(g, s);
1266 if (g->ptr < 0 || g->ptr > g->size)
1267 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1268 "internal error in string_catn (ptr %d size %d)", g->ptr, g->size);
1271 if (count >= g->size - p)
1272 gstring_grow(g, count);
1274 /* Because we always specify the exact number of characters to copy, we can
1275 use memcpy(), which is likely to be more efficient than strncopy() because the
1276 latter has to check for zero bytes. */
1278 memcpy(g->s + p, s, count);
1285 /*************************************************
1286 * Append strings to another string *
1287 *************************************************/
1289 /* This function can be used to build a string from many other strings.
1290 It calls string_cat() to do the dirty work.
1293 g growable-string that is being built, or NULL if not yet assigned
1294 count the number of strings to append
1295 ... "count" uschar* arguments, which must be valid zero-terminated
1298 Returns: growable string, changed if copied for expansion.
1299 The string is not zero-terminated - see string_cat() above.
1302 __inline__ gstring *
1303 string_append(gstring * g, int count, ...)
1307 va_start(ap, count);
1310 uschar * t = va_arg(ap, uschar *);
1311 g = string_cat(g, t);
1321 /*************************************************
1322 * Format a string with length checks *
1323 *************************************************/
1325 /* This function is used to format a string with checking of the length of the
1326 output for all conversions. It protects Exim from absent-mindedness when
1327 calling functions like debug_printf and string_sprintf, and elsewhere. There
1328 are two different entry points to what is actually the same function, depending
1329 on whether the variable length list of data arguments are given explicitly or
1332 The formats are the usual printf() ones, with some omissions (never used) and
1333 three additions for strings: %S forces lower case, %T forces upper case, and
1334 %#s or %#S prints nothing for a NULL string. Without the # "NULL" is printed
1335 (useful in debugging). There is also the addition of %D and %M, which insert
1336 the date in the form used for datestamped log files.
1339 buffer a buffer in which to put the formatted string
1340 buflen the length of the buffer
1341 format the format string - deliberately char * and not uschar *
1342 ... or ap variable list of supplementary arguments
1344 Returns: TRUE if the result fitted in the buffer
1348 string_format_trc(uschar * buffer, int buflen,
1349 const uschar * func, unsigned line, const char * format, ...)
1351 gstring g = { .size = buflen, .ptr = 0, .s = buffer }, * gp;
1353 va_start(ap, format);
1354 gp = string_vformat_trc(&g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1364 /* Build or append to a growing-string, sprintf-style.
1368 func called-from function name, for debug
1369 line called-from file line number, for debug
1370 limit maximum string size
1372 format printf-like format string
1373 ap variable-args pointer
1376 SVFMT_EXTEND buffer can be created or extended as needed
1377 SVFMT_REBUFFER buffer can be recopied to tainted mem as needed
1378 SVFMT_TAINT_NOCHK do not check inputs for taint
1380 If the "extend" flag is true, the string passed in can be NULL,
1381 empty, or non-empty. Growing is subject to an overall limit given
1382 by the limit argument.
1384 If the "extend" flag is false, the string passed in may not be NULL,
1385 will not be grown, and is usable in the original place after return.
1386 The return value can be NULL to signify overflow.
1388 Field width: decimal digits, or *
1389 Precision: dot, followed by decimal digits or *
1390 Length modifiers: h L l ll z
1391 Conversion specifiers: n d o u x X p f e E g G % c s S T W V Y D M H Z b
1392 Alternate-form: #: s/Y/b are silent about a null string
1394 Returns the possibly-new (if copy for growth or taint-handling was needed)
1395 string, not nul-terminated.
1399 string_vformat_trc(gstring * g, const uschar * func, unsigned line,
1400 unsigned size_limit, unsigned flags, const char * format, va_list ap)
1402 enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
1404 int width, precision, off, lim, need;
1405 const char * fp = format; /* Deliberately not unsigned */
1407 string_datestamp_offset = -1; /* Datestamp not inserted */
1408 string_datestamp_length = 0; /* Datestamp not inserted */
1409 string_datestamp_type = 0; /* Datestamp not inserted */
1411 #ifdef COMPILE_UTILITY
1412 assert(!(flags & SVFMT_EXTEND));
1416 /* Ensure we have a string, to save on checking later */
1417 if (!g) g = string_get(16);
1419 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, format))
1421 #ifndef MACRO_PREDEF
1422 if (!(flags & SVFMT_REBUFFER))
1423 die_tainted(US"string_vformat", func, line);
1425 /* debug_printf("rebuf B\n"); */
1426 gstring_rebuffer(g, format);
1428 #endif /*!COMPILE_UTILITY*/
1430 lim = g->size - 1; /* leave one for a nul */
1431 off = g->ptr; /* remember initial offset in gstring */
1433 /* Scan the format and handle the insertions */
1437 int length = L_NORMAL;
1440 const char *null = "NULL"; /* ) These variables */
1441 const char *item_start, *s; /* ) are deliberately */
1442 char newformat[16]; /* ) not unsigned */
1443 char * gp = CS g->s + g->ptr; /* ) */
1445 /* Non-% characters just get copied verbatim */
1449 /* Avoid string_copyn() due to COMPILE_UTILITY */
1450 if ((need = g->ptr + 1) > lim)
1452 if (!(flags & SVFMT_EXTEND) || need > size_limit) return NULL;
1456 g->s[g->ptr++] = (uschar) *fp++;
1460 /* Deal with % characters. Pick off the width and precision, for checking
1461 strings, skipping over the flag and modifier characters. */
1464 width = precision = -1;
1466 if (strchr("-+ #0", *(++fp)) != NULL)
1468 if (*fp == '#') null = "";
1472 if (isdigit((uschar)*fp))
1474 width = *fp++ - '0';
1475 while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
1477 else if (*fp == '*')
1479 width = va_arg(ap, int);
1486 precision = va_arg(ap, int);
1490 for (precision = 0; isdigit((uschar)*fp); fp++)
1491 precision = precision*10 + *fp - '0';
1493 /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
1496 { fp++; length = L_SHORT; }
1497 else if (*fp == 'L')
1498 { fp++; length = L_LONGDOUBLE; }
1499 else if (*fp == 'l')
1501 { fp += 2; length = L_LONGLONG; }
1503 { fp++; length = L_LONG; }
1504 else if (*fp == 'z')
1505 { fp++; length = L_SIZE; }
1507 /* Handle each specific format type. */
1512 nptr = va_arg(ap, int *);
1513 *nptr = g->ptr - off;
1521 width = length > L_LONG ? 24 : 12;
1522 if ((need = g->ptr + width) > lim)
1524 if (!(flags & SVFMT_EXTEND) || need >= size_limit) return NULL;
1525 gstring_grow(g, width);
1527 gp = CS g->s + g->ptr;
1529 strncpy(newformat, item_start, fp - item_start);
1530 newformat[fp - item_start] = 0;
1532 /* Short int is promoted to int when passing through ..., so we must use
1533 int for va_arg(). */
1539 g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break;
1541 g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break;
1543 g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break;
1545 g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break;
1552 if ((need = g->ptr + 24) > lim)
1554 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1555 gstring_grow(g, 24);
1557 gp = CS g->s + g->ptr;
1559 /* sprintf() saying "(nil)" for a null pointer seems unreliable.
1560 Handle it explicitly. */
1561 if ((ptr = va_arg(ap, void *)))
1563 strncpy(newformat, item_start, fp - item_start);
1564 newformat[fp - item_start] = 0;
1565 g->ptr += sprintf(gp, newformat, ptr);
1568 g->ptr += sprintf(gp, "(nil)");
1572 /* %f format is inherently insecure if the numbers that it may be
1573 handed are unknown (e.g. 1e300). However, in Exim, %f is used for
1574 printing load averages, and these are actually stored as integers
1575 (load average * 1000) so the size of the numbers is constrained.
1576 It is also used for formatting sending rates, where the simplicity
1577 of the format prevents overflow. */
1584 if (precision < 0) precision = 6;
1585 if ((need = g->ptr + precision + 8) > lim)
1587 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1588 gstring_grow(g, precision+8);
1590 gp = CS g->s + g->ptr;
1592 strncpy(newformat, item_start, fp - item_start);
1593 newformat[fp-item_start] = 0;
1594 if (length == L_LONGDOUBLE)
1595 g->ptr += sprintf(gp, newformat, va_arg(ap, long double));
1597 g->ptr += sprintf(gp, newformat, va_arg(ap, double));
1603 if ((need = g->ptr + 1) > lim)
1605 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1609 g->s[g->ptr++] = (uschar) '%';
1613 if ((need = g->ptr + 1) > lim)
1615 if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1619 g->s[g->ptr++] = (uschar) va_arg(ap, int);
1622 case 'D': /* Insert daily datestamp for log file names */
1623 s = CS tod_stamp(tod_log_datestamp_daily);
1624 string_datestamp_offset = g->ptr; /* Passed back via global */
1625 string_datestamp_length = Ustrlen(s); /* Passed back via global */
1626 string_datestamp_type = tod_log_datestamp_daily;
1627 slen = string_datestamp_length;
1630 case 'M': /* Insert monthly datestamp for log file names */
1631 s = CS tod_stamp(tod_log_datestamp_monthly);
1632 string_datestamp_offset = g->ptr; /* Passed back via global */
1633 string_datestamp_length = Ustrlen(s); /* Passed back via global */
1634 string_datestamp_type = tod_log_datestamp_monthly;
1635 slen = string_datestamp_length;
1638 case 'Y': /* gstring pointer */
1640 gstring * zg = va_arg(ap, gstring *);
1641 if (zg) { s = CS zg->s; slen = gstring_length(zg); }
1642 else { s = null; slen = Ustrlen(s); }
1643 goto INSERT_GSTRING;
1645 #ifndef COMPILE_UTILITY
1646 case 'b': /* blob pointer, carrying a string */
1648 blob * b = va_arg(ap, blob *);
1649 if (b) { s = CS b->data; slen = b->len; }
1650 else { s = null; slen = Ustrlen(s); }
1651 goto INSERT_GSTRING;
1654 case 'V': /* string; maybe convert ascii-art to UTF-8 chars */
1656 gstring * zg = NULL;
1657 s = va_arg(ap, char *);
1658 if (IS_DEBUG(D_noutf8))
1660 zg = string_catn(zg, CUS (*s == 'K' ? "|" : s), 1);
1662 for ( ; *s; s++) switch (*s)
1664 case '\\': zg = string_catn(zg, US UTF8_UP_RIGHT, 3); break;
1665 case '/': zg = string_catn(zg, US UTF8_DOWN_RIGHT, 3); break;
1667 case '_': zg = string_catn(zg, US UTF8_HORIZ, 3); break;
1668 case '|': zg = string_catn(zg, US UTF8_VERT, 3); break;
1669 case 'K': zg = string_catn(zg, US UTF8_VERT_RIGHT, 3); break;
1670 case '<': zg = string_catn(zg, US UTF8_LEFT_TRIANGLE, 3); break;
1671 case '>': zg = string_catn(zg, US UTF8_RIGHT_TRIANGLE, 3); break;
1672 default: zg = string_catn(zg, CUS s, 1); break;
1678 slen = gstring_length(zg);
1679 goto INSERT_GSTRING;
1682 case 'W': /* Maybe mark up ctrls, spaces & newlines */
1683 s = va_arg(ap, char *);
1684 if (s && !IS_DEBUG(D_noutf8))
1686 gstring * zg = NULL;
1689 /* If a precision was given, we can handle embedded NULs. Take it as
1690 applying to the input and expand it for the transformed result */
1692 for ( ; precision >= 0 || *s; s++)
1693 if (p >= 0 && --p < 0)
1698 zg = string_catn(zg, CUS UTF8_LIGHT_SHADE, 3);
1699 if (precision >= 0) precision += 2;
1702 zg = string_catn(zg, CUS UTF8_L_ARROW_HOOK "\n", 4);
1703 if (precision >= 0) precision += 3;
1707 { /* base of UTF8 symbols for ASCII control chars */
1708 uschar ctrl_symbol[3] = {[0]=0xe2, [1]=0x90, [2]=0x80};
1709 ctrl_symbol[2] |= *s;
1710 zg = string_catn(zg, ctrl_symbol, 3);
1711 if (precision >= 0) precision += 2;
1714 zg = string_catn(zg, CUS s, 1);
1717 if (zg) { s = CS zg->s; slen = gstring_length(zg); }
1718 else { s = ""; slen = 0; }
1725 goto INSERT_GSTRING;
1727 case 'Z': /* pdkim-style "quoteprint" */
1729 gstring * zg = NULL;
1730 int p = precision; /* If given, we can handle embedded NULs */
1732 s = va_arg(ap, char *);
1733 for ( ; precision >= 0 || *s; s++)
1734 if (p >= 0 && --p < 0)
1738 case ' ' : zg = string_catn(zg, US"{SP}", 4); break;
1739 case '\t': zg = string_catn(zg, US"{TB}", 4); break;
1740 case '\r': zg = string_catn(zg, US"{CR}", 4); break;
1741 case '\n': zg = string_catn(zg, US"{LF}", 4); break;
1742 case '{' : zg = string_catn(zg, US"{BO}", 4); break;
1743 case '}' : zg = string_catn(zg, US"{BC}", 4); break;
1747 if ( (u < 32) || (u > 127) )
1748 zg = string_fmt_append(zg, "{%02x}", u);
1750 zg = string_catn(zg, US s, 1);
1754 if (zg) { s = CS zg->s; precision = slen = gstring_length(zg); }
1755 else { s = ""; slen = 0; }
1757 goto INSERT_GSTRING;
1759 case 'H': /* pdkim-style "hexprint" */
1761 s = va_arg(ap, char *);
1762 if (precision < 0) break; /* precision must be given */
1765 gstring * zg = NULL;
1766 for (int p = precision; p > 0; p--)
1767 zg = string_fmt_append(zg, "%02x", * US s++);
1769 if (zg) { s = CS zg->s; precision = slen = gstring_length(zg); }
1770 else { s = ""; slen = 0; }
1773 { s = "<NULL>"; precision = slen = 6; }
1775 goto INSERT_GSTRING;
1779 case 'S': /* Forces *lower* case */
1780 case 'T': /* Forces *upper* case */
1781 s = va_arg(ap, char *);
1786 INSERT_GSTRING: /* Come to from %Y above */
1788 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
1789 if (flags & SVFMT_REBUFFER)
1791 /* debug_printf("%s %d: untainted workarea, tainted %%s :- rebuffer\n", __FUNCTION__, __LINE__); */
1792 gstring_rebuffer(g, s);
1793 gp = CS g->s + g->ptr;
1795 #ifndef MACRO_PREDEF
1797 die_tainted(US"string_vformat", func, line);
1800 INSERT_STRING: /* Come to from %D or %M above */
1803 BOOL truncated = FALSE;
1805 /* If the width is specified, check that there is a precision
1806 set; if not, set it to the width to prevent overruns of long
1811 if (precision < 0) precision = width;
1814 /* If a width is not specified and the precision is specified, set
1815 the width to the precision, or the string length if shorter. */
1817 else if (precision >= 0)
1818 width = precision < slen ? precision : slen;
1820 /* If neither are specified, set them both to the string length. */
1823 width = precision = slen;
1825 if ((need = g->ptr + width) >= size_limit || !(flags & SVFMT_EXTEND))
1827 if (g->ptr == lim) return NULL;
1831 width = precision = lim - g->ptr - 1;
1832 if (width < 0) width = 0;
1833 if (precision < 0) precision = 0;
1836 else if (need > lim)
1838 gstring_grow(g, width);
1840 gp = CS g->s + g->ptr;
1843 g->ptr += sprintf(gp, "%*.*s", width, precision, s);
1845 while (*gp) { *gp = tolower(*gp); gp++; }
1846 else if (fp[-1] == 'T')
1847 while (*gp) { *gp = toupper(*gp); gp++; }
1849 if (truncated) return NULL;
1853 /* Some things are never used in Exim; also catches junk. */
1856 strncpy(newformat, item_start, fp - item_start);
1857 newformat[fp-item_start] = 0;
1858 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
1859 "in \"%s\" in \"%s\"", newformat, format);
1864 if (g->ptr > g->size)
1865 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1866 "string_format internal error: caller %s %d", func, line);
1872 #ifndef COMPILE_UTILITY
1873 /*************************************************
1874 * Generate an "open failed" message *
1875 *************************************************/
1877 /* This function creates a message after failure to open a file. It includes a
1878 string supplied as data, adds the strerror() text, and if the failure was
1879 "Permission denied", reads and includes the euid and egid.
1882 format a text format string - deliberately not uschar *
1883 func caller, for debug
1884 line caller, for debug
1885 ... arguments for the format string
1887 Returns: a message, in dynamic store
1891 string_open_failed_trc(const uschar * func, unsigned line,
1892 const char * format, ...)
1895 gstring * g = string_get(1024);
1897 g = string_catn(g, US"failed to open ", 15);
1899 /* Use the checked formatting routine to ensure that the buffer
1900 does not overflow. It should not, since this is called only for internally
1901 specified messages. If it does, the message just gets truncated, and there
1902 doesn't seem much we can do about that. */
1904 va_start(ap, format);
1905 (void) string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1906 SVFMT_REBUFFER, format, ap);
1909 g = string_catn(g, US": ", 2);
1910 g = string_cat(g, US strerror(errno));
1912 if (errno == EACCES)
1914 int save_errno = errno;
1915 g = string_fmt_append(g, " (euid=%ld egid=%ld)",
1916 (long int)geteuid(), (long int)getegid());
1919 gstring_release_unused(g);
1920 return string_from_gstring(g);
1927 /* qsort(3), currently used to sort the environment variables
1928 for -bP environment output, needs a function to compare two pointers to string
1929 pointers. Here it is. */
1932 string_compare_by_pointer(const void *a, const void *b)
1934 return Ustrcmp(* CUSS a, * CUSS b);
1936 #endif /* COMPILE_UTILITY */
1941 /*************************************************
1942 **************************************************
1943 * Stand-alone test program *
1944 **************************************************
1945 *************************************************/
1952 printf("Testing is_ip_address\n");
1955 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1958 buffer[Ustrlen(buffer) - 1] = 0;
1959 printf("%d\n", string_is_ip_address(buffer, NULL));
1960 printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
1963 printf("Testing string_nextinlist\n");
1965 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1967 uschar *list = buffer;
1975 sep1 = sep2 = list[1];
1982 uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
1983 uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
1985 if (item1 == NULL && item2 == NULL) break;
1986 if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
1988 printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
1989 (item1 == NULL)? "NULL" : CS item1,
1990 (item2 == NULL)? "NULL" : CS item2);
1993 else printf(" \"%s\"\n", CS item1);
1997 /* This is a horrible lash-up, but it serves its purpose. */
1999 printf("Testing string_format\n");
2001 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
2004 long long llargs[3];
2010 BOOL countset = FASE;
2014 buffer[Ustrlen(buffer) - 1] = 0;
2016 s = Ustrchr(buffer, ',');
2017 if (s == NULL) s = buffer + Ustrlen(buffer);
2019 Ustrncpy(format, buffer, s - buffer);
2020 format[s-buffer] = 0;
2027 s = Ustrchr(ss, ',');
2028 if (s == NULL) s = ss + Ustrlen(ss);
2032 Ustrncpy(outbuf, ss, s-ss);
2033 if (Ustrchr(outbuf, '.') != NULL)
2036 dargs[n++] = Ustrtod(outbuf, NULL);
2038 else if (Ustrstr(outbuf, "ll") != NULL)
2041 llargs[n++] = strtoull(CS outbuf, NULL, 10);
2045 args[n++] = (void *)Uatoi(outbuf);
2049 else if (Ustrcmp(ss, "*") == 0)
2051 args[n++] = (void *)(&count);
2057 uschar *sss = malloc(s - ss + 1);
2058 Ustrncpy(sss, ss, s-ss);
2065 if (!dflag && !llflag)
2066 printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
2067 args[0], args[1], args[2])? "True" : "False");
2070 printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
2071 dargs[0], dargs[1], dargs[2])? "True" : "False");
2073 else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
2074 llargs[0], llargs[1], llargs[2])? "True" : "False");
2076 printf("%s\n", CS outbuf);
2077 if (countset) printf("count=%d\n", count);
2084 /* End of string.c */