Download | Plain Text | No Line Numbers


  1. diff -Naur a/ext/standard/mail.c b/ext/standard/mail.c
  2. --- a/ext/standard/mail.c 2021-01-08 11:11:26.957990999 +0100
  3. +++ b/ext/standard/mail.c 2021-01-08 11:11:52.106807019 +0100
  4. @@ -54,6 +54,13 @@
  5. } \
  6. continue; \
  7. } \
  8. + else if (str[pos] == '\n' && (str[pos + 1] == ' ' || str[pos + 1] == '\t')) { \
  9. + pos += 1; \
  10. + while (str[pos + 1] == ' ' || str[pos + 1] == '\t') { \
  11. + pos++; \
  12. + } \
  13. + continue; \
  14. + } \
  15.  
  16. extern zend_long php_getuid(void);
  17.  
  18. @@ -198,6 +205,47 @@
  19. }
  20.  
  21.  
  22. +static long
  23. +count_recipients(const char *str, int len, int skip_field)
  24. +{
  25. + long recipients = 0;
  26. + int got_field, i;
  27. +
  28. + if (str == NULL || len <= 0)
  29. + return 0;
  30. +
  31. + got_field = skip_field;
  32. + for (i = 0; str[i]; i++) {
  33. + /* search for mime-fields
  34. + * either at beginning or after '\n' of the string
  35. + */
  36. + if (!got_field &&
  37. + (!strncasecmp(&str[i], "To: ", strlen("To: ")) ||
  38. + !strncasecmp(&str[i], "Cc: ", strlen("Cc: ")) ||
  39. + !strncasecmp(&str[i], "Bcc: ", strlen("Bcc: "))
  40. + )) {
  41. + if (i == 0 || (i > 0 && str[i - 1] == '\n'))
  42. + got_field = 1;
  43. + }
  44. +
  45. + /* search for every '@', don't stop at long headers */
  46. + if (got_field) {
  47. + if (str[i] == '@')
  48. + recipients++;
  49. + else if (str[i] == '\n')
  50. + if (i == len - 1 || (str[i + 1] != ' ' && str[i + 1] != '\t'))
  51. + got_field = 0;
  52. + }
  53. +
  54. + /* message body starts here */
  55. + if (i > 0 && str[i - 1] == '\n' && str[i] == '\n')
  56. + break;
  57. + }
  58. +
  59. + return recipients;
  60. +}
  61. +
  62. +
  63. /* {{{ Send an email message */
  64. PHP_FUNCTION(mail)
  65. {
  66. @@ -210,6 +258,8 @@
  67. size_t subject_len, i;
  68. char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
  69. char *to_r, *subject_r;
  70. + long recipients = 0;
  71. + long max_recipients = INI_INT("sendmail_max_recipients");
  72.  
  73. ZEND_PARSE_PARAMETERS_START(3, 5)
  74. Z_PARAM_PATH(to, to_len)
  75. @@ -233,6 +283,19 @@
  76. }
  77. }
  78.  
  79. + /* count recipients */
  80. + if (max_recipients > 0) {
  81. + recipients += count_recipients(to, to_len, 1);
  82. + if (headers_str) {
  83. + recipients += count_recipients(ZSTR_VAL(headers_str), ZSTR_LEN(headers_str), 0);
  84. + }
  85. + if (recipients > max_recipients) {
  86. + php_error_docref(NULL, E_WARNING, "Max recipients reached, mail not sent.");
  87. + RETVAL_FALSE;
  88. + goto end;
  89. + }
  90. + }
  91. +
  92. if (to_len > 0) {
  93. to_r = estrndup(to, to_len);
  94. for (; to_len; to_len--) {
  95. @@ -248,7 +311,9 @@
  96. * To prevent these separators from being replaced with a space, we use the
  97. * SKIP_LONG_HEADER_SEP to skip over them. */
  98. SKIP_LONG_HEADER_SEP(to_r, i);
  99. - to_r[i] = ' ';
  100. + php_error_docref(NULL, E_WARNING, "Disallowed characters in mail parameters, mail not sent.");
  101. + RETVAL_FALSE;
  102. + goto end;
  103. }
  104. }
  105. } else {
  106. @@ -266,7 +331,9 @@
  107. for (i = 0; subject_r[i]; i++) {
  108. if (iscntrl((unsigned char) subject_r[i])) {
  109. SKIP_LONG_HEADER_SEP(subject_r, i);
  110. - subject_r[i] = ' ';
  111. + php_error_docref(NULL, E_WARNING, "Disallowed characters in mail parameters, mail not sent.");
  112. + RETVAL_FALSE;
  113. + goto end;
  114. }
  115. }
  116. } else {
  117. @@ -285,6 +352,7 @@
  118. RETVAL_FALSE;
  119. }
  120.  
  121. +end:
  122. if (headers_str) {
  123. zend_string_release_ex(headers_str, 0);
  124. }
  125. @@ -393,10 +461,23 @@
  126. } \
  127. return val; \
  128.  
  129. + zval *zhttphost;
  130. + char *httphost = NULL;
  131. + if ((mail_log && *mail_log) || PG(mail_x_header)) {
  132. + if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) &&
  133. + (zhttphost = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZEND_STRL("HTTP_HOST"))) != NULL &&
  134. + Z_TYPE_P(zhttphost) == IS_STRING &&
  135. + Z_STRLEN_P(zhttphost) > 0) {
  136. + httphost = Z_STRVAL_P(zhttphost);
  137. + } else {
  138. + httphost = "";
  139. + }
  140. + }
  141. +
  142. if (mail_log && *mail_log) {
  143. char *logline;
  144.  
  145. - spprintf(&logline, 0, "mail() on [%s:%d]: To: %s -- Headers: %s -- Subject: %s", zend_get_executed_filename(), zend_get_executed_lineno(), to, hdr ? hdr : "", subject);
  146. + spprintf(&logline, 0, "mail() on [%s:%d]: To: %s -- HTTP-Host: %s -- Headers: %s -- Subject: %s", zend_get_executed_filename(), zend_get_executed_lineno(), to, httphost, hdr ? hdr : "", subject);
  147.  
  148. if (hdr) {
  149. php_mail_log_crlf_to_spaces(logline);
  150. @@ -432,9 +513,9 @@
  151. f = php_basename(tmp, strlen(tmp), NULL, 0);
  152.  
  153. if (headers != NULL && *headers) {
  154. - spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\n%s", php_getuid(), ZSTR_VAL(f), headers);
  155. + spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\nX-PHP-HTTP-Host: %s\r\n%s", php_getuid(), tmp, httphost, headers);
  156. } else {
  157. - spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
  158. + spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\nX-PHP-HTTP-Host: %s", php_getuid(), tmp, httphost);
  159. }
  160. hdr = ahdr;
  161. zend_string_release_ex(f, 0);
  162. diff -Naur a/main/main.c b/main/main.c
  163. --- a/main/main.c 2021-01-08 11:11:27.123989785 +0100
  164. +++ b/main/main.c 2021-01-08 11:11:52.107807012 +0100
  165. @@ -728,6 +728,7 @@
  166. PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision)
  167. PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL)
  168. PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL)
  169. + PHP_INI_ENTRY("sendmail_max_recipients", "5", PHP_INI_ALL, NULL)
  170. PHP_INI_ENTRY("mail.force_extra_parameters",NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnChangeMailForceExtra)
  171. PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL)
  172. PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL)
  173.