fix: proxy-protocol (CVE-2023-41227) Bug 3031
[exim.git] / src / src / string.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 /* Miscellaneous string-handling functions. Some are not required for
10 utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
11
12
13 #include "exim.h"
14 #include <assert.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   errp      NULL if no diagnostic information is required, and if the netmask
33             length should not be checked. Otherwise it is set pointing to a short
34             descriptive text.
35
36 Returns:    0 if the string is not a textual representation of an IP address
37             4 if it is an IPv4 address
38             6 if it is an IPv6 address
39
40 The legacy string_is_ip_address() function follows below.
41 */
42 int
43 string_is_ip_addressX(const uschar *ip_addr, int *maskptr, const uschar **errp) {
44   struct addrinfo hints;
45   struct addrinfo *res;
46
47   uschar *slash, *percent;
48
49   uschar *endp = 0;
50   long int mask = 0;
51   const uschar *addr = 0;
52
53   /* If there is a slash, but we didn't request a (optional) netmask,
54   we return failure, as we do if the mask isn't a pure numerical value,
55   or if it is negative. The actual length is checked later, once we know
56   the address family. */
57   if (slash = Ustrchr(ip_addr, '/'))
58   {
59     if (!maskptr)
60     {
61       if (errp) *errp = "netmask found, but not requested";
62       return 0;
63     }
64
65     uschar *rest;
66     mask = Ustrtol(slash+1, &rest, 10);
67     if (*rest || mask < 0)
68     {
69       if (errp) *errp = "netmask not numeric or <0";
70       return 0;
71     }
72
73     *maskptr = slash - ip_addr;     /* offset of the slash */
74     endp = slash;
75   } else if (maskptr) *maskptr = 0; /* no slash found */
76
77   /* The interface-ID suffix (%<id>) is optional (for IPv6). If it
78   exists, we check it syntactically. Later, if we know the address
79   family is IPv4, we might reject it.
80   The interface-ID is mutually exclusive with the netmask, to the
81   best of my knowledge. */
82   if (percent = Ustrchr(ip_addr, '%'))
83   {
84     if (slash)
85     {
86       if (errp) *errp = "interface-ID and netmask are mutually exclusive";
87       return 0;
88     }
89     for (uschar *p = percent+1; *p; p++)
90         if (!isalnum(*p) && !ispunct(*p))
91         {
92           if (errp) *errp = "interface-ID must match [[:alnum:][:punct:]]";
93           return 0;
94         }
95     endp = percent;
96   }
97
98   /* inet_pton() can't parse netmasks and interface IDs, so work on a shortened copy
99   allocated on the current stack */
100   if (endp) {
101     ptrdiff_t l = endp - ip_addr;
102     if (l > 255)
103     {
104       if (errp) *errp = "rudiculous long ip address string";
105       return 0;
106     }
107     addr = alloca(l+1); /* *BSD does not have strndupa() */
108     Ustrncpy((uschar *)addr, ip_addr, l);
109     ((uschar*)addr)[l] = '\0';
110   } else addr = ip_addr;
111
112   int af;
113   union { /* we do not need this, but inet_pton() needs a place for storage */
114     struct in_addr sa4;
115     struct in6_addr sa6;
116   } sa;
117
118   af = Ustrchr(addr, ':') ? AF_INET6 : AF_INET;
119   if (!inet_pton(af, addr, &sa))
120   {
121     if (errp) *errp = af == AF_INET6 ? "IP address string not parsable as IPv6"
122                                      : "IP address string not parsable IPv4";
123     return 0;
124   }
125   /* we do not check the values of the mask here, as
126   this is done on the callers side (but I don't understand why), so
127   actually I'd like to do it here, but it breaks at least 0002 */
128   switch (af)
129   {
130     case AF_INET6:
131         if (errp && mask > 128)
132         {
133           *errp = "IPv6 netmask value must not be >128";
134           return 0;
135         }
136         return 6;
137     case AF_INET:
138         if (percent)
139         {
140           if (errp) *errp = "IPv4 address string must not have an interface-ID";
141           return 0;
142         }
143         if (errp && mask > 32) {
144           *errp = "IPv4 netmask value must not be >32";
145           return 0;
146         }
147         return 4;
148     default:
149         if (errp) *errp = "unknown address family (should not happen)";
150         return 0;
151  }
152 }
153
154 int
155 string_is_ip_address(const uschar *ip_addr, int *maskptr) {
156   return string_is_ip_addressX(ip_addr, maskptr, 0);
157 }
158
159 #endif  /* COMPILE_UTILITY */
160
161
162 /*************************************************
163 *              Format message size               *
164 *************************************************/
165
166 /* Convert a message size in bytes to printing form, rounding
167 according to the magnitude of the number. A value of zero causes
168 a string of spaces to be returned.
169
170 Arguments:
171   size        the message size in bytes
172   buffer      where to put the answer
173
174 Returns:      pointer to the buffer
175               a string of exactly 5 characters is normally returned
176 */
177
178 uschar *
179 string_format_size(int size, uschar *buffer)
180 {
181 if (size == 0) Ustrcpy(buffer, US"     ");
182 else if (size < 1024) sprintf(CS buffer, "%5d", size);
183 else if (size < 10*1024)
184   sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
185 else if (size < 1024*1024)
186   sprintf(CS buffer, "%4dK", (size + 512)/1024);
187 else if (size < 10*1024*1024)
188   sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
189 else
190   sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
191 return buffer;
192 }
193
194
195
196 #ifndef COMPILE_UTILITY
197 /*************************************************
198 *       Convert a number to base 62 format       *
199 *************************************************/
200
201 /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
202 BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a
203 static area.
204
205 Argument: a long integer
206 Returns:  pointer to base 62 string
207 */
208
209 uschar *
210 string_base62(unsigned long int value)
211 {
212 static uschar yield[7];
213 uschar *p = yield + sizeof(yield) - 1;
214 *p = 0;
215 while (p > yield)
216   {
217   *(--p) = base62_chars[value % BASE_62];
218   value /= BASE_62;
219   }
220 return yield;
221 }
222 #endif  /* COMPILE_UTILITY */
223
224
225
226 /*************************************************
227 *          Interpret escape sequence             *
228 *************************************************/
229
230 /* This function is called from several places where escape sequences are to be
231 interpreted in strings.
232
233 Arguments:
234   pp       points a pointer to the initiating "\" in the string;
235            the pointer gets updated to point to the final character
236            If the backslash is the last character in the string, it
237            is not interpreted.
238 Returns:   the value of the character escape
239 */
240
241 int
242 string_interpret_escape(const uschar **pp)
243 {
244 #ifdef COMPILE_UTILITY
245 const uschar *hex_digits= CUS"0123456789abcdef";
246 #endif
247 int ch;
248 const uschar *p = *pp;
249 ch = *(++p);
250 if (ch == '\0') return **pp;
251 if (isdigit(ch) && ch != '8' && ch != '9')
252   {
253   ch -= '0';
254   if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
255     {
256     ch = ch * 8 + *(++p) - '0';
257     if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
258       ch = ch * 8 + *(++p) - '0';
259     }
260   }
261 else switch(ch)
262   {
263   case 'b':  ch = '\b'; break;
264   case 'f':  ch = '\f'; break;
265   case 'n':  ch = '\n'; break;
266   case 'r':  ch = '\r'; break;
267   case 't':  ch = '\t'; break;
268   case 'v':  ch = '\v'; break;
269   case 'x':
270   ch = 0;
271   if (isxdigit(p[1]))
272     {
273     ch = ch * 16 +
274       Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
275     if (isxdigit(p[1])) ch = ch * 16 +
276       Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
277     }
278   break;
279   }
280 *pp = p;
281 return ch;
282 }
283
284
285
286 #ifndef COMPILE_UTILITY
287 /*************************************************
288 *          Ensure string is printable            *
289 *************************************************/
290
291 /* This function is called for critical strings. It checks for any
292 non-printing characters, and if any are found, it makes a new copy
293 of the string with suitable escape sequences. It is most often called by the
294 macro string_printing(), which sets flags to 0.
295
296 Arguments:
297   s             the input string
298   flags         Bit 0: convert tabs.  Bit 1: convert spaces.
299
300 Returns:        string with non-printers encoded as printing sequences
301 */
302
303 const uschar *
304 string_printing2(const uschar *s, int flags)
305 {
306 int nonprintcount = 0;
307 int length = 0;
308 const uschar *t = s;
309 uschar *ss, *tt;
310
311 while (*t)
312   {
313   int c = *t++;
314   if (  !mac_isprint(c)
315      || flags & SP_TAB && c == '\t'
316      || flags & SP_SPACE && c == ' '
317      ) nonprintcount++;
318   length++;
319   }
320
321 if (nonprintcount == 0) return s;
322
323 /* Get a new block of store guaranteed big enough to hold the
324 expanded string. */
325
326 tt = ss = store_get(length + nonprintcount * 3 + 1, s);
327
328 /* Copy everything, escaping non printers. */
329
330 for (t = s; *t; )
331   {
332   int c = *t;
333   if (  mac_isprint(c)
334      && (!(flags & SP_TAB) || c != '\t')
335      && (!(flags & SP_SPACE) || c != ' ')
336      )
337     *tt++ = *t++;
338   else
339     {
340     *tt++ = '\\';
341     switch (*t)
342       {
343       case '\n': *tt++ = 'n'; break;
344       case '\r': *tt++ = 'r'; break;
345       case '\b': *tt++ = 'b'; break;
346       case '\v': *tt++ = 'v'; break;
347       case '\f': *tt++ = 'f'; break;
348       case '\t': *tt++ = 't'; break;
349       default: sprintf(CS tt, "%03o", *t); tt += 3; break;
350       }
351     t++;
352     }
353   }
354 *tt = 0;
355 return ss;
356 }
357 #endif  /* COMPILE_UTILITY */
358
359 /*************************************************
360 *        Undo printing escapes in string         *
361 *************************************************/
362
363 /* This function is the reverse of string_printing2.  It searches for
364 backslash characters and if any are found, it makes a new copy of the
365 string with escape sequences parsed.  Otherwise it returns the original
366 string.
367
368 Arguments:
369   s             the input string
370
371 Returns:        string with printing escapes parsed back
372 */
373
374 uschar *
375 string_unprinting(uschar *s)
376 {
377 uschar *p, *q, *r, *ss;
378 int len, off;
379
380 p = Ustrchr(s, '\\');
381 if (!p) return s;
382
383 len = Ustrlen(s) + 1;
384 ss = store_get(len, s);
385
386 q = ss;
387 off = p - s;
388 if (off)
389   {
390   memcpy(q, s, off);
391   q += off;
392   }
393
394 while (*p)
395   {
396   if (*p == '\\')
397     {
398     *q++ = string_interpret_escape((const uschar **)&p);
399     p++;
400     }
401   else
402     {
403     r = Ustrchr(p, '\\');
404     if (!r)
405       {
406       off = Ustrlen(p);
407       memcpy(q, p, off);
408       p += off;
409       q += off;
410       break;
411       }
412     else
413       {
414       off = r - p;
415       memcpy(q, p, off);
416       q += off;
417       p = r;
418       }
419     }
420   }
421 *q = '\0';
422
423 return ss;
424 }
425
426
427
428
429 #if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
430         && !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
431 /*************************************************
432 *            Copy and save string                *
433 *************************************************/
434
435 /*
436 Argument: string to copy
437 Returns:  copy of string in new store with the same taint status
438 */
439
440 uschar *
441 string_copy_function(const uschar * s)
442 {
443 return string_copy_taint(s, s);
444 }
445
446 /* As above, but explicitly specifying the result taint status
447 */
448
449 uschar *
450 string_copy_taint_function(const uschar * s, const void * proto_mem)
451 {
452 return string_copy_taint(s, proto_mem);
453 }
454
455
456
457 /*************************************************
458 *       Copy and save string, given length       *
459 *************************************************/
460
461 /* It is assumed the data contains no zeros. A zero is added
462 onto the end.
463
464 Arguments:
465   s         string to copy
466   n         number of characters
467
468 Returns:    copy of string in new store
469 */
470
471 uschar *
472 string_copyn_function(const uschar * s, int n)
473 {
474 return string_copyn(s, n);
475 }
476 #endif
477
478
479 /*************************************************
480 *     Copy and save string in malloc'd store     *
481 *************************************************/
482
483 /* This function assumes that memcpy() is faster than strcpy().
484
485 Argument: string to copy
486 Returns:  copy of string in new store
487 */
488
489 uschar *
490 string_copy_malloc(const uschar * s)
491 {
492 int len = Ustrlen(s) + 1;
493 uschar * ss = store_malloc(len);
494 memcpy(ss, s, len);
495 return ss;
496 }
497
498
499
500 /*************************************************
501 *    Copy string if long, inserting newlines     *
502 *************************************************/
503
504 /* If the given string is longer than 75 characters, it is copied, and within
505 the copy, certain space characters are converted into newlines.
506
507 Argument:  pointer to the string
508 Returns:   pointer to the possibly altered string
509 */
510
511 uschar *
512 string_split_message(uschar * msg)
513 {
514 uschar *s, *ss;
515
516 if (!msg || Ustrlen(msg) <= 75) return msg;
517 s = ss = msg = string_copy(msg);
518
519 for (;;)
520   {
521   int i = 0;
522   while (i < 75 && *ss && *ss != '\n') ss++, i++;
523   if (!*ss) break;
524   if (*ss == '\n')
525     s = ++ss;
526   else
527     {
528     uschar * t = ss + 1;
529     uschar * tt = NULL;
530     while (--t > s + 35)
531       {
532       if (*t == ' ')
533         {
534         if (t[-1] == ':') { tt = t; break; }
535         if (!tt) tt = t;
536         }
537       }
538
539     if (!tt)          /* Can't split behind - try ahead */
540       {
541       t = ss + 1;
542       while (*t)
543         {
544         if (*t == ' ' || *t == '\n')
545           { tt = t; break; }
546         t++;
547         }
548       }
549
550     if (!tt) break;   /* Can't find anywhere to split */
551     *tt = '\n';
552     s = ss = tt+1;
553     }
554   }
555
556 return msg;
557 }
558
559
560
561 /*************************************************
562 *   Copy returned DNS domain name, de-escaping   *
563 *************************************************/
564
565 /* If a domain name contains top-bit characters, some resolvers return
566 the fully qualified name with those characters turned into escapes. The
567 convention is a backslash followed by _decimal_ digits. We convert these
568 back into the original binary values. This will be relevant when
569 allow_utf8_domains is set true and UTF-8 characters are used in domain
570 names. Backslash can also be used to escape other characters, though we
571 shouldn't come across them in domain names.
572
573 Argument:   the domain name string
574 Returns:    copy of string in new store, de-escaped
575 */
576
577 uschar *
578 string_copy_dnsdomain(uschar * s)
579 {
580 uschar * yield;
581 uschar * ss = yield = store_get(Ustrlen(s) + 1, GET_TAINTED);   /* always treat as tainted */
582
583 while (*s)
584   {
585   if (*s != '\\')
586     *ss++ = *s++;
587   else if (isdigit(s[1]))
588     {
589     *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
590     s += 4;
591     }
592   else if (*++s)
593     *ss++ = *s++;
594   }
595
596 *ss = 0;
597 return yield;
598 }
599
600
601 #ifndef COMPILE_UTILITY
602 /*************************************************
603 *     Copy space-terminated or quoted string     *
604 *************************************************/
605
606 /* This function copies from a string until its end, or until whitespace is
607 encountered, unless the string begins with a double quote, in which case the
608 terminating quote is sought, and escaping within the string is done. The length
609 of a de-quoted string can be no longer than the original, since escaping always
610 turns n characters into 1 character.
611
612 Argument:  pointer to the pointer to the first character, which gets updated
613 Returns:   the new string
614 */
615
616 uschar *
617 string_dequote(const uschar ** sptr)
618 {
619 const uschar * s = * sptr;
620 uschar * t, * yield;
621
622 /* First find the end of the string */
623
624 if (*s != '\"')
625   while (*s && !isspace(*s)) s++;
626 else
627   {
628   s++;
629   while (*s && *s != '\"')
630     {
631     if (*s == '\\') (void)string_interpret_escape(&s);
632     s++;
633     }
634   if (*s) s++;
635   }
636
637 /* Get enough store to copy into */
638
639 t = yield = store_get(s - *sptr + 1, *sptr);
640 s = *sptr;
641
642 /* Do the copy */
643
644 if (*s != '\"')
645   while (*s && !isspace(*s)) *t++ = *s++;
646 else
647   {
648   s++;
649   while (*s && *s != '\"')
650     {
651     *t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
652     s++;
653     }
654   if (*s) s++;
655   }
656
657 /* Update the pointer and return the terminated copy */
658
659 *sptr = s;
660 *t = 0;
661 return yield;
662 }
663 #endif  /* COMPILE_UTILITY */
664
665
666
667 /*************************************************
668 *          Format a string and save it           *
669 *************************************************/
670
671 /* The formatting is done by string_vformat, which checks the length of
672 everything.  Taint is taken from the worst of the arguments.
673
674 Arguments:
675   format    a printf() format - deliberately char * rather than uschar *
676               because it will most usually be a literal string
677   func      caller, for debug
678   line      caller, for debug
679   ...       arguments for format
680
681 Returns:    pointer to fresh piece of store containing sprintf'ed string
682 */
683
684 uschar *
685 string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
686 {
687 #ifdef COMPILE_UTILITY
688 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
689 gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
690 gstring * g = &gs;
691 unsigned flags = 0;
692 #else
693 gstring * g = NULL;
694 unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
695 #endif
696
697 va_list ap;
698 va_start(ap, line);
699 g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
700         flags, format, ap);
701 va_end(ap);
702
703 if (!g)
704   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
705     "string_sprintf expansion was longer than %d; format string was (%s)\n"
706     " called from %s %d\n",
707     STRING_SPRINTF_BUFFER_SIZE, format, func, line);
708
709 #ifdef COMPILE_UTILITY
710 return string_copyn(g->s, g->ptr);
711 #else
712 gstring_release_unused(g);
713 return string_from_gstring(g);
714 #endif
715 }
716
717
718
719 /*************************************************
720 *         Case-independent strncmp() function    *
721 *************************************************/
722
723 /*
724 Arguments:
725   s         first string
726   t         second string
727   n         number of characters to compare
728
729 Returns:    < 0, = 0, or > 0, according to the comparison
730 */
731
732 int
733 strncmpic(const uschar * s, const uschar * t, int n)
734 {
735 while (n--)
736   {
737   int c = tolower(*s++) - tolower(*t++);
738   if (c) return c;
739   }
740 return 0;
741 }
742
743
744 /*************************************************
745 *         Case-independent strcmp() function     *
746 *************************************************/
747
748 /*
749 Arguments:
750   s         first string
751   t         second string
752
753 Returns:    < 0, = 0, or > 0, according to the comparison
754 */
755
756 int
757 strcmpic(const uschar * s, const uschar * t)
758 {
759 while (*s)
760   {
761   int c = tolower(*s++) - tolower(*t++);
762   if (c != 0) return c;
763   }
764 return *t;
765 }
766
767
768 /*************************************************
769 *         Case-independent strstr() function     *
770 *************************************************/
771
772 /* The third argument specifies whether whitespace is required
773 to follow the matched string.
774
775 Arguments:
776   s              string to search
777   t              substring to search for
778   space_follows  if TRUE, match only if whitespace follows
779
780 Returns:         pointer to substring in string, or NULL if not found
781 */
782
783 const uschar *
784 strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
785 {
786 const uschar * p = t;
787 const uschar * yield = NULL;
788 int cl = tolower(*p);
789 int cu = toupper(*p);
790
791 while (*s)
792   {
793   if (*s == cl || *s == cu)
794     {
795     if (!yield) yield = s;
796     if (!*++p)
797       {
798       if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
799       yield = NULL;
800       p = t;
801       }
802     cl = tolower(*p);
803     cu = toupper(*p);
804     s++;
805     }
806   else if (yield)
807     {
808     yield = NULL;
809     p = t;
810     cl = tolower(*p);
811     cu = toupper(*p);
812     }
813   else s++;
814   }
815 return NULL;
816 }
817
818 uschar *
819 strstric(uschar * s, uschar * t, BOOL space_follows)
820 {
821 return US strstric_c(s, t, space_follows);
822 }
823
824
825 #ifdef COMPILE_UTILITY
826 /* Dummy version for this function; it should never be called */
827 static void
828 gstring_grow(gstring * g, int count)
829 {
830 assert(FALSE);
831 }
832 #endif
833
834
835
836 #ifndef COMPILE_UTILITY
837 /*************************************************
838 *       Get next string from separated list      *
839 *************************************************/
840
841 /* Leading and trailing space is removed from each item. The separator in the
842 list is controlled by the int pointed to by the separator argument as follows:
843
844   If the value is > 0 it is used as the separator. This is typically used for
845   sublists such as slash-separated options. The value is always a printing
846   character.
847
848     (If the value is actually > UCHAR_MAX there is only one item in the list.
849     This is used for some cases when called via functions that sometimes
850     plough through lists, and sometimes are given single items.)
851
852   If the value is <= 0, the string is inspected for a leading <x, where x is an
853   ispunct() or an iscntrl() character. If found, x is used as the separator. If
854   not found:
855
856       (a) if separator == 0, ':' is used
857       (b) if separator <0, -separator is used
858
859   In all cases the value of the separator that is used is written back to the
860   int so that it is used on subsequent calls as we progress through the list.
861
862 A literal ispunct() separator can be represented in an item by doubling, but
863 there is no way to include an iscntrl() separator as part of the data.
864
865 Arguments:
866   listptr    points to a pointer to the current start of the list; the
867              pointer gets updated to point after the end of the next item
868   separator  a pointer to the separator character in an int (see above)
869   buffer     where to put a copy of the next string in the list; or
870                NULL if the next string is returned in new memory
871              Note that if the list is tainted then a provided buffer must be
872              also (else we trap, with a message referencing the callsite).
873              If we do the allocation, taint is handled there.
874   buflen     when buffer is not NULL, the size of buffer; otherwise ignored
875
876   func       caller, for debug
877   line       caller, for debug
878
879 Returns:     pointer to buffer, containing the next substring,
880              or NULL if no more substrings
881 */
882
883 uschar *
884 string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
885   int buflen, const uschar * func, int line)
886 {
887 int sep = *separator;
888 const uschar * s = *listptr;
889 BOOL sep_is_special;
890
891 if (!s) return NULL;
892
893 /* This allows for a fixed specified separator to be an iscntrl() character,
894 but at the time of implementation, this is never the case. However, it's best
895 to be conservative. */
896
897 while (isspace(*s) && *s != sep) s++;
898
899 /* A change of separator is permitted, so look for a leading '<' followed by an
900 allowed character. */
901
902 if (sep <= 0)
903   {
904   if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
905     {
906     sep = s[1];
907     if (*++s) ++s;
908     while (isspace(*s) && *s != sep) s++;
909     }
910   else
911     sep = sep ? -sep : ':';
912   *separator = sep;
913   }
914
915 /* An empty string has no list elements */
916
917 if (!*s) return NULL;
918
919 /* Note whether whether or not the separator is an iscntrl() character. */
920
921 sep_is_special = iscntrl(sep);
922
923 /* Handle the case when a buffer is provided. */
924 /*XXX need to also deal with qouted-requirements mismatch */
925
926 if (buffer)
927   {
928   int p = 0;
929   if (is_tainted(s) && !is_tainted(buffer))
930     die_tainted(US"string_nextinlist", func, line);
931   for (; *s; s++)
932     {
933     if (*s == sep && (*(++s) != sep || sep_is_special)) break;
934     if (p < buflen - 1) buffer[p++] = *s;
935     }
936   while (p > 0 && isspace(buffer[p-1])) p--;
937   buffer[p] = '\0';
938   }
939
940 /* Handle the case when a buffer is not provided. */
941
942 else
943   {
944   gstring * g = NULL;
945
946   /* We know that *s != 0 at this point. However, it might be pointing to a
947   separator, which could indicate an empty string, or (if an ispunct()
948   character) could be doubled to indicate a separator character as data at the
949   start of a string. Avoid getting working memory for an empty item. */
950
951   if (*s == sep)
952     if (*++s != sep || sep_is_special)
953       {
954       *listptr = s;
955       return string_copy(US"");
956       }
957
958   /* Not an empty string; the first character is guaranteed to be a data
959   character. */
960
961   for (;;)
962     {
963     const uschar * ss;
964     for (ss = s + 1; *ss && *ss != sep; ) ss++;
965     g = string_catn(g, s, ss-s);
966     s = ss;
967     if (!*s || *++s != sep || sep_is_special) break;
968     }
969
970   /* Trim trailing spaces from the returned string */
971
972   /* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
973   while (  g->ptr > 0 && isspace(g->s[g->ptr-1])
974         && (g->ptr == 1 || g->s[g->ptr-2] != '\\') )
975     g->ptr--;
976   buffer = string_from_gstring(g);
977   gstring_release_unused_trc(g, CCS func, line);
978   }
979
980 /* Update the current pointer and return the new string */
981
982 *listptr = s;
983 return buffer;
984 }
985
986
987 static const uschar *
988 Ustrnchr(const uschar * s, int c, unsigned * len)
989 {
990 unsigned siz = *len;
991 while (siz)
992   {
993   if (!*s) return NULL;
994   if (*s == c)
995     {
996     *len = siz;
997     return s;
998     }
999   s++;
1000   siz--;
1001   }
1002 return NULL;
1003 }
1004
1005
1006 /************************************************
1007 *       Add element to separated list           *
1008 ************************************************/
1009 /* This function is used to build a list, returning an allocated null-terminated
1010 growable string. The given element has any embedded separator characters
1011 doubled.
1012
1013 Despite having the same growable-string interface as string_cat() the list is
1014 always returned null-terminated.
1015
1016 Arguments:
1017   list  expanding-string for the list that is being built, or NULL
1018         if this is a new list that has no contents yet
1019   sep   list separator character
1020   ele   new element to be appended to the list
1021
1022 Returns:  pointer to the start of the list, changed if copied for expansion.
1023 */
1024
1025 gstring *
1026 string_append_listele(gstring * list, uschar sep, const uschar * ele)
1027 {
1028 uschar * sp;
1029
1030 if (list && list->ptr)
1031   list = string_catn(list, &sep, 1);
1032
1033 while((sp = Ustrchr(ele, sep)))
1034   {
1035   list = string_catn(list, ele, sp-ele+1);
1036   list = string_catn(list, &sep, 1);
1037   ele = sp+1;
1038   }
1039 list = string_cat(list, ele);
1040 (void) string_from_gstring(list);
1041 return list;
1042 }
1043
1044
1045 gstring *
1046 string_append_listele_n(gstring * list, uschar sep, const uschar * ele,
1047  unsigned len)
1048 {
1049 const uschar * sp;
1050
1051 if (list && list->ptr)
1052   list = string_catn(list, &sep, 1);
1053
1054 while((sp = Ustrnchr(ele, sep, &len)))
1055   {
1056   list = string_catn(list, ele, sp-ele+1);
1057   list = string_catn(list, &sep, 1);
1058   ele = sp+1;
1059   len--;
1060   }
1061 list = string_catn(list, ele, len);
1062 (void) string_from_gstring(list);
1063 return list;
1064 }
1065
1066
1067
1068 /* A slightly-bogus listmaker utility; the separator is a string so
1069 can be multiple chars - there is no checking for the element content
1070 containing any of the separator. */
1071
1072 gstring *
1073 string_append2_listele_n(gstring * list, const uschar * sepstr,
1074  const uschar * ele, unsigned len)
1075 {
1076 if (list && list->ptr)
1077   list = string_cat(list, sepstr);
1078
1079 list = string_catn(list, ele, len);
1080 (void) string_from_gstring(list);
1081 return list;
1082 }
1083
1084
1085
1086 /************************************************/
1087 /* Add more space to a growable-string.  The caller should check
1088 first if growth is required.  The gstring struct is modified on
1089 return; specifically, the string-base-pointer may have been changed.
1090
1091 Arguments:
1092   g             the growable-string
1093   count         amount needed for g->ptr to increase by
1094 */
1095
1096 static void
1097 gstring_grow(gstring * g, int count)
1098 {
1099 int p = g->ptr;
1100 int oldsize = g->size;
1101
1102 /* Mostly, string_cat() is used to build small strings of a few hundred
1103 characters at most. There are times, however, when the strings are very much
1104 longer (for example, a lookup that returns a vast number of alias addresses).
1105 To try to keep things reasonable, we use increments whose size depends on the
1106 existing length of the string. */
1107
1108 unsigned inc = oldsize < 4096 ? 127 : 1023;
1109
1110 if (g->ptr < 0 || g->ptr > g->size || g->size >= INT_MAX/2)
1111   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1112     "internal error in gstring_grow (ptr %d size %d)", g->ptr, g->size);
1113
1114 if (count <= 0) return;
1115
1116 if (count >= INT_MAX/2 - g->ptr)
1117   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1118     "internal error in gstring_grow (ptr %d count %d)", g->ptr, count);
1119
1120 g->size = (p + count + inc + 1) & ~inc;         /* one for a NUL */
1121
1122 /* Try to extend an existing allocation. If the result of calling
1123 store_extend() is false, either there isn't room in the current memory block,
1124 or this string is not the top item on the dynamic store stack. We then have
1125 to get a new chunk of store and copy the old string. When building large
1126 strings, it is helpful to call store_release() on the old string, to release
1127 memory blocks that have become empty. (The block will be freed if the string
1128 is at its start.) However, we can do this only if we know that the old string
1129 was the last item on the dynamic memory stack. This is the case if it matches
1130 store_last_get. */
1131
1132 if (!store_extend(g->s, oldsize, g->size))
1133   g->s = store_newblock(g->s, g->size, p);
1134 }
1135
1136
1137
1138 /*************************************************
1139 *             Add chars to string                *
1140 *************************************************/
1141 /* This function is used when building up strings of unknown length. Room is
1142 always left for a terminating zero to be added to the string that is being
1143 built. This function does not require the string that is being added to be NUL
1144 terminated, because the number of characters to add is given explicitly. It is
1145 sometimes called to extract parts of other strings.
1146
1147 Arguments:
1148   g        growable-string that is being built, or NULL if not assigned yet
1149   s        points to characters to add
1150   count    count of characters to add; must not exceed the length of s, if s
1151              is a C string.
1152
1153 Returns:   growable string, changed if copied for expansion.
1154            Note that a NUL is not added, though space is left for one. This is
1155            because string_cat() is often called multiple times to build up a
1156            string - there's no point adding the NUL till the end.
1157            NULL is a possible return.
1158
1159 */
1160 /* coverity[+alloc] */
1161
1162 gstring *
1163 string_catn(gstring * g, const uschar * s, int count)
1164 {
1165 int p;
1166
1167 if (count < 0)
1168   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1169     "internal error in string_catn (count %d)", count);
1170 if (count == 0) return g;
1171
1172 /*debug_printf("string_catn '%.*s'\n", count, s);*/
1173 if (!g)
1174   {
1175   unsigned inc = count < 4096 ? 127 : 1023;
1176   unsigned size = ((count + inc) &  ~inc) + 1;  /* round up requested count */
1177   g = string_get_tainted(size, s);
1178   }
1179 else if (!g->s)                 /* should not happen */
1180   {
1181   g->s = string_copyn(s, count);
1182   g->ptr = count;
1183   g->size = count;      /*XXX suboptimal*/
1184   return g;
1185   }
1186 else if (is_incompatible(g->s, s))
1187   {
1188 /* debug_printf("rebuf A\n"); */
1189   gstring_rebuffer(g, s);
1190   }
1191
1192 if (g->ptr < 0 || g->ptr > g->size)
1193   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1194     "internal error in string_catn (ptr %d size %d)", g->ptr, g->size);
1195
1196 p = g->ptr;
1197 if (count >= g->size - p)
1198   gstring_grow(g, count);
1199
1200 /* Because we always specify the exact number of characters to copy, we can
1201 use memcpy(), which is likely to be more efficient than strncopy() because the
1202 latter has to check for zero bytes. */
1203
1204 memcpy(g->s + p, s, count);
1205 g->ptr = p + count;
1206 return g;
1207 }
1208
1209
1210 gstring *
1211 string_cat(gstring * g, const uschar * s)
1212 {
1213 return string_catn(g, s, Ustrlen(s));
1214 }
1215
1216
1217
1218 /*************************************************
1219 *        Append strings to another string        *
1220 *************************************************/
1221
1222 /* This function can be used to build a string from many other strings.
1223 It calls string_cat() to do the dirty work.
1224
1225 Arguments:
1226   g        growable-string that is being built, or NULL if not yet assigned
1227   count    the number of strings to append
1228   ...      "count" uschar* arguments, which must be valid zero-terminated
1229              C strings
1230
1231 Returns:   growable string, changed if copied for expansion.
1232            The string is not zero-terminated - see string_cat() above.
1233 */
1234
1235 __inline__ gstring *
1236 string_append(gstring * g, int count, ...)
1237 {
1238 va_list ap;
1239
1240 va_start(ap, count);
1241 while (count-- > 0)
1242   {
1243   uschar * t = va_arg(ap, uschar *);
1244   g = string_cat(g, t);
1245   }
1246 va_end(ap);
1247
1248 return g;
1249 }
1250 #endif
1251
1252
1253
1254 /*************************************************
1255 *        Format a string with length checks      *
1256 *************************************************/
1257
1258 /* This function is used to format a string with checking of the length of the
1259 output for all conversions. It protects Exim from absent-mindedness when
1260 calling functions like debug_printf and string_sprintf, and elsewhere. There
1261 are two different entry points to what is actually the same function, depending
1262 on whether the variable length list of data arguments are given explicitly or
1263 as a va_list item.
1264
1265 The formats are the usual printf() ones, with some omissions (never used) and
1266 three additions for strings: %S forces lower case, %T forces upper case, and
1267 %#s or %#S prints nothing for a NULL string. Without the # "NULL" is printed
1268 (useful in debugging). There is also the addition of %D and %M, which insert
1269 the date in the form used for datestamped log files.
1270
1271 Arguments:
1272   buffer       a buffer in which to put the formatted string
1273   buflen       the length of the buffer
1274   format       the format string - deliberately char * and not uschar *
1275   ... or ap    variable list of supplementary arguments
1276
1277 Returns:       TRUE if the result fitted in the buffer
1278 */
1279
1280 BOOL
1281 string_format_trc(uschar * buffer, int buflen,
1282   const uschar * func, unsigned line, const char * format, ...)
1283 {
1284 gstring g = { .size = buflen, .ptr = 0, .s = buffer }, * gp;
1285 va_list ap;
1286 va_start(ap, format);
1287 gp = string_vformat_trc(&g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1288         0, format, ap);
1289 va_end(ap);
1290 g.s[g.ptr] = '\0';
1291 return !!gp;
1292 }
1293
1294
1295
1296
1297 /* Build or append to a growing-string, sprintf-style.
1298
1299 Arguments:
1300         g       a growable-string
1301         func    called-from function name, for debug
1302         line    called-from file line number, for debug
1303         limit   maximum string size
1304         flags   see below
1305         format  printf-like format string
1306         ap      variable-args pointer
1307
1308 Flags:
1309         SVFMT_EXTEND            buffer can be created or exteded as needed
1310         SVFMT_REBUFFER          buffer can be recopied to tainted mem as needed
1311         SVFMT_TAINT_NOCHK       do not check inputs for taint
1312
1313 If the "extend" flag is true, the string passed in can be NULL,
1314 empty, or non-empty.  Growing is subject to an overall limit given
1315 by the limit argument.
1316
1317 If the "extend" flag is false, the string passed in may not be NULL,
1318 will not be grown, and is usable in the original place after return.
1319 The return value can be NULL to signify overflow.
1320
1321 Returns the possibly-new (if copy for growth or taint-handling was needed)
1322 string, not nul-terminated.
1323 */
1324
1325 gstring *
1326 string_vformat_trc(gstring * g, const uschar * func, unsigned line,
1327   unsigned size_limit, unsigned flags, const char * format, va_list ap)
1328 {
1329 enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
1330
1331 int width, precision, off, lim, need;
1332 const char * fp = format;       /* Deliberately not unsigned */
1333
1334 string_datestamp_offset = -1;   /* Datestamp not inserted */
1335 string_datestamp_length = 0;    /* Datestamp not inserted */
1336 string_datestamp_type = 0;      /* Datestamp not inserted */
1337
1338 #ifdef COMPILE_UTILITY
1339 assert(!(flags & SVFMT_EXTEND));
1340 assert(g);
1341 #else
1342
1343 /* Ensure we have a string, to save on checking later */
1344 if (!g) g = string_get(16);
1345
1346 if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, format))
1347   {
1348 #ifndef MACRO_PREDEF
1349   if (!(flags & SVFMT_REBUFFER))
1350     die_tainted(US"string_vformat", func, line);
1351 #endif
1352 /* debug_printf("rebuf B\n"); */
1353   gstring_rebuffer(g, format);
1354   }
1355 #endif  /*!COMPILE_UTILITY*/
1356
1357 lim = g->size - 1;      /* leave one for a nul */
1358 off = g->ptr;           /* remember initial offset in gstring */
1359
1360 /* Scan the format and handle the insertions */
1361
1362 while (*fp)
1363   {
1364   int length = L_NORMAL;
1365   int * nptr;
1366   int slen;
1367   const char *null = "NULL";            /* ) These variables */
1368   const char *item_start, *s;           /* ) are deliberately */
1369   char newformat[16];                   /* ) not unsigned */
1370   char * gp = CS g->s + g->ptr;         /* ) */
1371
1372   /* Non-% characters just get copied verbatim */
1373
1374   if (*fp != '%')
1375     {
1376     /* Avoid string_copyn() due to COMPILE_UTILITY */
1377     if ((need = g->ptr + 1) > lim)
1378       {
1379       if (!(flags & SVFMT_EXTEND) || need > size_limit) return NULL;
1380       gstring_grow(g, 1);
1381       lim = g->size - 1;
1382       }
1383     g->s[g->ptr++] = (uschar) *fp++;
1384     continue;
1385     }
1386
1387   /* Deal with % characters. Pick off the width and precision, for checking
1388   strings, skipping over the flag and modifier characters. */
1389
1390   item_start = fp;
1391   width = precision = -1;
1392
1393   if (strchr("-+ #0", *(++fp)) != NULL)
1394     {
1395     if (*fp == '#') null = "";
1396     fp++;
1397     }
1398
1399   if (isdigit((uschar)*fp))
1400     {
1401     width = *fp++ - '0';
1402     while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
1403     }
1404   else if (*fp == '*')
1405     {
1406     width = va_arg(ap, int);
1407     fp++;
1408     }
1409
1410   if (*fp == '.')
1411     if (*(++fp) == '*')
1412       {
1413       precision = va_arg(ap, int);
1414       fp++;
1415       }
1416     else
1417       for (precision = 0; isdigit((uschar)*fp); fp++)
1418         precision = precision*10 + *fp - '0';
1419
1420   /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
1421
1422   if (*fp == 'h')
1423     { fp++; length = L_SHORT; }
1424   else if (*fp == 'L')
1425     { fp++; length = L_LONGDOUBLE; }
1426   else if (*fp == 'l')
1427     if (fp[1] == 'l')
1428       { fp += 2; length = L_LONGLONG; }
1429     else
1430       { fp++; length = L_LONG; }
1431   else if (*fp == 'z')
1432     { fp++; length = L_SIZE; }
1433
1434   /* Handle each specific format type. */
1435
1436   switch (*fp++)
1437     {
1438     case 'n':
1439       nptr = va_arg(ap, int *);
1440       *nptr = g->ptr - off;
1441       break;
1442
1443     case 'd':
1444     case 'o':
1445     case 'u':
1446     case 'x':
1447     case 'X':
1448       width = length > L_LONG ? 24 : 12;
1449       if ((need = g->ptr + width) > lim)
1450         {
1451         if (!(flags & SVFMT_EXTEND) || need >= size_limit) return NULL;
1452         gstring_grow(g, width);
1453         lim = g->size - 1;
1454         gp = CS g->s + g->ptr;
1455         }
1456       strncpy(newformat, item_start, fp - item_start);
1457       newformat[fp - item_start] = 0;
1458
1459       /* Short int is promoted to int when passing through ..., so we must use
1460       int for va_arg(). */
1461
1462       switch(length)
1463         {
1464         case L_SHORT:
1465         case L_NORMAL:
1466           g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break;
1467         case L_LONG:
1468           g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break;
1469         case L_LONGLONG:
1470           g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break;
1471         case L_SIZE:
1472           g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break;
1473         }
1474       break;
1475
1476     case 'p':
1477       {
1478       void * ptr;
1479       if ((need = g->ptr + 24) > lim)
1480         {
1481         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1482         gstring_grow(g, 24);
1483         lim = g->size - 1;
1484         gp = CS g->s + g->ptr;
1485         }
1486       /* sprintf() saying "(nil)" for a null pointer seems unreliable.
1487       Handle it explicitly. */
1488       if ((ptr = va_arg(ap, void *)))
1489         {
1490         strncpy(newformat, item_start, fp - item_start);
1491         newformat[fp - item_start] = 0;
1492         g->ptr += sprintf(gp, newformat, ptr);
1493         }
1494       else
1495         g->ptr += sprintf(gp, "(nil)");
1496       }
1497     break;
1498
1499     /* %f format is inherently insecure if the numbers that it may be
1500     handed are unknown (e.g. 1e300). However, in Exim, %f is used for
1501     printing load averages, and these are actually stored as integers
1502     (load average * 1000) so the size of the numbers is constrained.
1503     It is also used for formatting sending rates, where the simplicity
1504     of the format prevents overflow. */
1505
1506     case 'f':
1507     case 'e':
1508     case 'E':
1509     case 'g':
1510     case 'G':
1511       if (precision < 0) precision = 6;
1512       if ((need = g->ptr + precision + 8) > lim)
1513         {
1514         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1515         gstring_grow(g, precision+8);
1516         lim = g->size - 1;
1517         gp = CS g->s + g->ptr;
1518         }
1519       strncpy(newformat, item_start, fp - item_start);
1520       newformat[fp-item_start] = 0;
1521       if (length == L_LONGDOUBLE)
1522         g->ptr += sprintf(gp, newformat, va_arg(ap, long double));
1523       else
1524         g->ptr += sprintf(gp, newformat, va_arg(ap, double));
1525       break;
1526
1527     /* String types */
1528
1529     case '%':
1530       if ((need = g->ptr + 1) > lim)
1531         {
1532         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1533         gstring_grow(g, 1);
1534         lim = g->size - 1;
1535         }
1536       g->s[g->ptr++] = (uschar) '%';
1537       break;
1538
1539     case 'c':
1540       if ((need = g->ptr + 1) > lim)
1541         {
1542         if (!(flags & SVFMT_EXTEND || need >= size_limit)) return NULL;
1543         gstring_grow(g, 1);
1544         lim = g->size - 1;
1545         }
1546       g->s[g->ptr++] = (uschar) va_arg(ap, int);
1547       break;
1548
1549     case 'D':                   /* Insert daily datestamp for log file names */
1550       s = CS tod_stamp(tod_log_datestamp_daily);
1551       string_datestamp_offset = g->ptr;         /* Passed back via global */
1552       string_datestamp_length = Ustrlen(s);     /* Passed back via global */
1553       string_datestamp_type = tod_log_datestamp_daily;
1554       slen = string_datestamp_length;
1555       goto INSERT_STRING;
1556
1557     case 'M':                   /* Insert monthly datestamp for log file names */
1558       s = CS tod_stamp(tod_log_datestamp_monthly);
1559       string_datestamp_offset = g->ptr;         /* Passed back via global */
1560       string_datestamp_length = Ustrlen(s);     /* Passed back via global */
1561       string_datestamp_type = tod_log_datestamp_monthly;
1562       slen = string_datestamp_length;
1563       goto INSERT_STRING;
1564
1565     case 's':
1566     case 'S':                   /* Forces *lower* case */
1567     case 'T':                   /* Forces *upper* case */
1568       s = va_arg(ap, char *);
1569
1570       if (!s) s = null;
1571       slen = Ustrlen(s);
1572
1573       if (!(flags & SVFMT_TAINT_NOCHK) && is_incompatible(g->s, s))
1574         if (flags & SVFMT_REBUFFER)
1575           {
1576 /* debug_printf("%s %d: untainted workarea, tainted %%s :- rebuffer\n", __FUNCTION__, __LINE__); */
1577           gstring_rebuffer(g, s);
1578           gp = CS g->s + g->ptr;
1579           }
1580 #ifndef MACRO_PREDEF
1581         else
1582           die_tainted(US"string_vformat", func, line);
1583 #endif
1584
1585     INSERT_STRING:              /* Come to from %D or %M above */
1586
1587       {
1588       BOOL truncated = FALSE;
1589
1590       /* If the width is specified, check that there is a precision
1591       set; if not, set it to the width to prevent overruns of long
1592       strings. */
1593
1594       if (width >= 0)
1595         {
1596         if (precision < 0) precision = width;
1597         }
1598
1599       /* If a width is not specified and the precision is specified, set
1600       the width to the precision, or the string length if shorted. */
1601
1602       else if (precision >= 0)
1603         width = precision < slen ? precision : slen;
1604
1605       /* If neither are specified, set them both to the string length. */
1606
1607       else
1608         width = precision = slen;
1609
1610       if ((need = g->ptr + width) >= size_limit || !(flags & SVFMT_EXTEND))
1611         {
1612         if (g->ptr == lim) return NULL;
1613         if (need > lim)
1614           {
1615           truncated = TRUE;
1616           width = precision = lim - g->ptr - 1;
1617           if (width < 0) width = 0;
1618           if (precision < 0) precision = 0;
1619           }
1620         }
1621       else if (need > lim)
1622         {
1623         gstring_grow(g, width);
1624         lim = g->size - 1;
1625         gp = CS g->s + g->ptr;
1626         }
1627
1628       g->ptr += sprintf(gp, "%*.*s", width, precision, s);
1629       if (fp[-1] == 'S')
1630         while (*gp) { *gp = tolower(*gp); gp++; }
1631       else if (fp[-1] == 'T')
1632         while (*gp) { *gp = toupper(*gp); gp++; }
1633
1634       if (truncated) return NULL;
1635       break;
1636       }
1637
1638     /* Some things are never used in Exim; also catches junk. */
1639
1640     default:
1641       strncpy(newformat, item_start, fp - item_start);
1642       newformat[fp-item_start] = 0;
1643       log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
1644         "in \"%s\" in \"%s\"", newformat, format);
1645       break;
1646     }
1647   }
1648
1649 if (g->ptr > g->size)
1650   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1651     "string_format internal error: caller %s %d", func, line);
1652 return g;
1653 }
1654
1655
1656
1657 #ifndef COMPILE_UTILITY
1658 /*************************************************
1659 *       Generate an "open failed" message        *
1660 *************************************************/
1661
1662 /* This function creates a message after failure to open a file. It includes a
1663 string supplied as data, adds the strerror() text, and if the failure was
1664 "Permission denied", reads and includes the euid and egid.
1665
1666 Arguments:
1667   format        a text format string - deliberately not uschar *
1668   func          caller, for debug
1669   line          caller, for debug
1670   ...           arguments for the format string
1671
1672 Returns:        a message, in dynamic store
1673 */
1674
1675 uschar *
1676 string_open_failed_trc(const uschar * func, unsigned line,
1677   const char * format, ...)
1678 {
1679 va_list ap;
1680 gstring * g = string_get(1024);
1681
1682 g = string_catn(g, US"failed to open ", 15);
1683
1684 /* Use the checked formatting routine to ensure that the buffer
1685 does not overflow. It should not, since this is called only for internally
1686 specified messages. If it does, the message just gets truncated, and there
1687 doesn't seem much we can do about that. */
1688
1689 va_start(ap, format);
1690 (void) string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
1691         SVFMT_REBUFFER, format, ap);
1692 va_end(ap);
1693
1694 g = string_catn(g, US": ", 2);
1695 g = string_cat(g, US strerror(errno));
1696
1697 if (errno == EACCES)
1698   {
1699   int save_errno = errno;
1700   g = string_fmt_append(g, " (euid=%ld egid=%ld)",
1701     (long int)geteuid(), (long int)getegid());
1702   errno = save_errno;
1703   }
1704 gstring_release_unused(g);
1705 return string_from_gstring(g);
1706 }
1707
1708
1709
1710
1711
1712 /* qsort(3), currently used to sort the environment variables
1713 for -bP environment output, needs a function to compare two pointers to string
1714 pointers. Here it is. */
1715
1716 int
1717 string_compare_by_pointer(const void *a, const void *b)
1718 {
1719 return Ustrcmp(* CUSS a, * CUSS b);
1720 }
1721 #endif /* COMPILE_UTILITY */
1722
1723
1724
1725
1726 /*************************************************
1727 **************************************************
1728 *             Stand-alone test program           *
1729 **************************************************
1730 *************************************************/
1731
1732 #ifdef STAND_ALONE
1733 int main(void)
1734 {
1735 uschar buffer[256];
1736
1737 printf("Testing is_ip_address\n");
1738 store_init();
1739
1740 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1741   {
1742   int offset;
1743   buffer[Ustrlen(buffer) - 1] = 0;
1744   printf("%d\n", string_is_ip_address(buffer, NULL));
1745   printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
1746   }
1747
1748 printf("Testing string_nextinlist\n");
1749
1750 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1751   {
1752   uschar *list = buffer;
1753   uschar *lp1, *lp2;
1754   uschar item[256];
1755   int sep1 = 0;
1756   int sep2 = 0;
1757
1758   if (*list == '<')
1759     {
1760     sep1 = sep2 = list[1];
1761     list += 2;
1762     }
1763
1764   lp1 = lp2 = list;
1765   for (;;)
1766     {
1767     uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
1768     uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
1769
1770     if (item1 == NULL && item2 == NULL) break;
1771     if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
1772       {
1773       printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
1774         (item1 == NULL)? "NULL" : CS item1,
1775         (item2 == NULL)? "NULL" : CS item2);
1776       break;
1777       }
1778     else printf("  \"%s\"\n", CS item1);
1779     }
1780   }
1781
1782 /* This is a horrible lash-up, but it serves its purpose. */
1783
1784 printf("Testing string_format\n");
1785
1786 while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
1787   {
1788   void *args[3];
1789   long long llargs[3];
1790   double dargs[3];
1791   int dflag = 0;
1792   int llflag = 0;
1793   int n = 0;
1794   int count;
1795   int countset = 0;
1796   uschar format[256];
1797   uschar outbuf[256];
1798   uschar *s;
1799   buffer[Ustrlen(buffer) - 1] = 0;
1800
1801   s = Ustrchr(buffer, ',');
1802   if (s == NULL) s = buffer + Ustrlen(buffer);
1803
1804   Ustrncpy(format, buffer, s - buffer);
1805   format[s-buffer] = 0;
1806
1807   if (*s == ',') s++;
1808
1809   while (*s != 0)
1810     {
1811     uschar *ss = s;
1812     s = Ustrchr(ss, ',');
1813     if (s == NULL) s = ss + Ustrlen(ss);
1814
1815     if (isdigit(*ss))
1816       {
1817       Ustrncpy(outbuf, ss, s-ss);
1818       if (Ustrchr(outbuf, '.') != NULL)
1819         {
1820         dflag = 1;
1821         dargs[n++] = Ustrtod(outbuf, NULL);
1822         }
1823       else if (Ustrstr(outbuf, "ll") != NULL)
1824         {
1825         llflag = 1;
1826         llargs[n++] = strtoull(CS outbuf, NULL, 10);
1827         }
1828       else
1829         {
1830         args[n++] = (void *)Uatoi(outbuf);
1831         }
1832       }
1833
1834     else if (Ustrcmp(ss, "*") == 0)
1835       {
1836       args[n++] = (void *)(&count);
1837       countset = 1;
1838       }
1839
1840     else
1841       {
1842       uschar *sss = malloc(s - ss + 1);
1843       Ustrncpy(sss, ss, s-ss);
1844       args[n++] = sss;
1845       }
1846
1847     if (*s == ',') s++;
1848     }
1849
1850   if (!dflag && !llflag)
1851     printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1852       args[0], args[1], args[2])? "True" : "False");
1853
1854   else if (dflag)
1855     printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1856       dargs[0], dargs[1], dargs[2])? "True" : "False");
1857
1858   else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
1859     llargs[0], llargs[1], llargs[2])? "True" : "False");
1860
1861   printf("%s\n", CS outbuf);
1862   if (countset) printf("count=%d\n", count);
1863   }
1864
1865 return 0;
1866 }
1867 #endif
1868
1869 /* End of string.c */