Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/mail.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Rasmus Lerdorf <rasmus@php.net>                              |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#include <stdlib.h>
16
#include <ctype.h>
17
#include <stdio.h>
18
#include <time.h>
19
#include "php.h"
20
#include "ext/standard/info.h"
21
#include "ext/standard/php_string.h"
22
#include "ext/standard/basic_functions.h"
23
#include "ext/date/php_date.h"
24
#include "zend_smart_str.h"
25
26
#ifdef HAVE_SYS_WAIT_H
27
#include <sys/wait.h>
28
#endif
29
30
#ifdef HAVE_SYSEXITS_H
31
# include <sysexits.h>
32
#endif
33
#ifdef HAVE_SYS_SYSEXITS_H
34
# include <sys/sysexits.h>
35
#endif
36
37
#if PHP_SIGCHILD
38
# include <signal.h>
39
#endif
40
41
#include "php_syslog.h"
42
#include "php_mail.h"
43
#include "php_ini.h"
44
#include "exec.h"
45
46
#ifdef PHP_WIN32
47
# include "win32/sendmail.h"
48
#endif
49
50
#define SKIP_LONG_HEADER_SEP(str, pos)                                  \
51
0
  if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) { \
52
0
    pos += 2;                                           \
53
0
    while (str[pos + 1] == ' ' || str[pos + 1] == '\t') {                     \
54
0
      pos++;                                            \
55
0
    }                                                \
56
0
    continue;                                           \
57
0
  }                                                  \
58
59
extern zend_long php_getuid(void);
60
61
typedef enum {
62
  NO_HEADER_ERROR,
63
  CONTAINS_LF_ONLY,
64
  CONTAINS_CR_ONLY,
65
  CONTAINS_CRLF,
66
  CONTAINS_NULL
67
} php_mail_header_value_error_type;
68
69
static php_mail_header_value_error_type php_mail_build_headers_check_field_value(const zend_string *value)
70
0
{
71
0
  size_t len = 0;
72
73
  /* https://tools.ietf.org/html/rfc2822#section-2.2.1 */
74
  /* https://tools.ietf.org/html/rfc2822#section-2.2.3 */
75
0
  while (len < ZSTR_LEN(value)) {
76
0
    if (*(ZSTR_VAL(value)+len) == '\r') {
77
0
      if (*(ZSTR_VAL(value)+len+1) != '\n') {
78
0
        return CONTAINS_CR_ONLY;
79
0
      }
80
81
0
      if (ZSTR_LEN(value) - len >= 3
82
0
        && (*(ZSTR_VAL(value)+len+2) == ' ' || *(ZSTR_VAL(value)+len+2) == '\t')) {
83
0
        len += 3;
84
0
        continue;
85
0
      }
86
87
0
      return CONTAINS_CRLF;
88
0
    }
89
    /**
90
     * The RFC does not allow using LF alone for folding. However, LF is
91
     * often treated similarly to CRLF, and there are likely many user
92
     * environments that use LF for folding.
93
     * Therefore, considering such an environment, folding with LF alone
94
     * is allowed.
95
     */
96
0
    if (*(ZSTR_VAL(value)+len) == '\n') {
97
0
      if (ZSTR_LEN(value) - len >= 2
98
0
        && (*(ZSTR_VAL(value)+len+1) == ' ' || *(ZSTR_VAL(value)+len+1) == '\t')) {
99
0
        len += 2;
100
0
        continue;
101
0
      }
102
0
      return CONTAINS_LF_ONLY;
103
0
    }
104
0
    if (*(ZSTR_VAL(value)+len) == '\0') {
105
0
      return CONTAINS_NULL;
106
0
    }
107
0
    len++;
108
0
  }
109
0
  return NO_HEADER_ERROR;
110
0
}
111
112
static zend_result php_mail_build_headers_check_field_name(const zend_string *key)
113
0
{
114
0
  size_t len = 0;
115
116
  /* https://tools.ietf.org/html/rfc2822#section-2.2 */
117
0
  while (len < ZSTR_LEN(key)) {
118
0
    if (*(ZSTR_VAL(key)+len) < 33 || *(ZSTR_VAL(key)+len) > 126 || *(ZSTR_VAL(key)+len) == ':') {
119
0
      return FAILURE;
120
0
    }
121
0
    len++;
122
0
  }
123
0
  return SUCCESS;
124
0
}
125
126
127
static void php_mail_build_headers_elems(smart_str *s, const zend_string *key, zval *val);
128
129
static void php_mail_build_headers_elem(smart_str *s, const zend_string *key, zval *val)
130
0
{
131
0
  switch(Z_TYPE_P(val)) {
132
0
    case IS_STRING:
133
0
      if (php_mail_build_headers_check_field_name(key) != SUCCESS) {
134
0
        zend_value_error("Header name \"%s\" contains invalid characters", ZSTR_VAL(key));
135
0
        return;
136
0
      }
137
138
0
      zend_string *str_value = Z_STR_P(val);
139
0
      php_mail_header_value_error_type error_type = php_mail_build_headers_check_field_value(str_value);
140
0
      switch (error_type) {
141
0
        case NO_HEADER_ERROR:
142
0
          break;
143
0
        case CONTAINS_LF_ONLY:
144
0
          zend_value_error("Header \"%s\" contains LF character that is not allowed in the header", ZSTR_VAL(key));
145
0
          return;
146
0
        case CONTAINS_CR_ONLY:
147
0
          zend_value_error("Header \"%s\" contains CR character that is not allowed in the header", ZSTR_VAL(key));
148
0
          return;
149
0
        case CONTAINS_CRLF:
150
0
          zend_value_error("Header \"%s\" contains CRLF characters that are used as a line separator and are not allowed in the header", ZSTR_VAL(key));
151
0
          return;
152
0
        case CONTAINS_NULL:
153
0
          zend_value_error("Header \"%s\" contains NULL character that is not allowed in the header", ZSTR_VAL(key));
154
0
          return;
155
0
        default:
156
          // fallback
157
0
          zend_value_error("Header \"%s\" has invalid format, or contains invalid characters", ZSTR_VAL(key));
158
0
          return;
159
0
      }
160
0
      smart_str_append(s, key);
161
0
      smart_str_appendl(s, ": ", 2);
162
0
      smart_str_append(s, str_value);
163
0
      smart_str_appendl(s, "\r\n", 2);
164
0
      break;
165
0
    case IS_ARRAY:
166
0
      php_mail_build_headers_elems(s, key, val);
167
0
      break;
168
0
    default:
169
0
      zend_type_error("Header \"%s\" must be of type array|string, %s given", ZSTR_VAL(key), zend_zval_value_name(val));
170
0
  }
171
0
}
172
173
174
static void php_mail_build_headers_elems(smart_str *s, const zend_string *key, zval *val)
175
0
{
176
0
  zend_string *tmp_key;
177
0
  zval *tmp_val;
178
179
0
  ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(val), tmp_key, tmp_val) {
180
0
    if (tmp_key) {
181
0
      zend_type_error("Header \"%s\" must only contain numeric keys, \"%s\" found", ZSTR_VAL(key), ZSTR_VAL(tmp_key));
182
0
      break;
183
0
    }
184
0
    ZVAL_DEREF(tmp_val);
185
0
    if (Z_TYPE_P(tmp_val) != IS_STRING) {
186
0
      zend_type_error("Header \"%s\" must only contain values of type string, %s found", ZSTR_VAL(key), zend_zval_value_name(tmp_val));
187
0
      break;
188
0
    }
189
0
    php_mail_build_headers_elem(s, key, tmp_val);
190
0
  } ZEND_HASH_FOREACH_END();
191
0
}
192
193
0
#define PHP_MAIL_BUILD_HEADER_CHECK(target, s, key, val) \
194
0
do { \
195
0
  if (Z_TYPE_P(val) == IS_STRING) { \
196
0
    php_mail_build_headers_elem(&s, key, val); \
197
0
  } else if (Z_TYPE_P(val) == IS_ARRAY) { \
198
0
    if (zend_string_equals_literal_ci(key, target)) { \
199
0
      zend_type_error("Header \"%s\" must be of type string, array given", target); \
200
0
      break; \
201
0
    } \
202
0
    php_mail_build_headers_elems(&s, key, val); \
203
0
  } else { \
204
0
    zend_type_error("Header \"%s\" must be of type array|string, %s given", ZSTR_VAL(key), zend_zval_value_name(val)); \
205
0
  } \
206
0
} while(0)
207
208
PHPAPI zend_string *php_mail_build_headers(const HashTable *headers)
209
0
{
210
0
  zend_ulong idx;
211
0
  zend_string *key;
212
0
  zval *val;
213
0
  smart_str s = {0};
214
215
0
  ZEND_HASH_FOREACH_KEY_VAL(headers, idx, key, val) {
216
0
    if (!key) {
217
0
      zend_type_error("Header name cannot be numeric, " ZEND_LONG_FMT " given", idx);
218
0
      smart_str_free(&s);
219
0
      return NULL;
220
0
    }
221
0
    ZVAL_DEREF(val);
222
    /* https://tools.ietf.org/html/rfc2822#section-3.6 */
223
0
    if (zend_string_equals_literal_ci(key, "orig-date")) {
224
0
      PHP_MAIL_BUILD_HEADER_CHECK("orig-date", s, key, val);
225
0
    } else if (zend_string_equals_literal_ci(key, "from")) {
226
0
      PHP_MAIL_BUILD_HEADER_CHECK("from", s, key, val);
227
0
    } else if (zend_string_equals_literal_ci(key, "sender")) {
228
0
      PHP_MAIL_BUILD_HEADER_CHECK("sender", s, key, val);
229
0
    } else if (zend_string_equals_literal_ci(key, "reply-to")) {
230
0
      PHP_MAIL_BUILD_HEADER_CHECK("reply-to", s, key, val);
231
0
    } else if (zend_string_equals_literal_ci(key, "to")) {
232
0
      zend_value_error("The additional headers cannot contain the \"To\" header");
233
0
    } else if (zend_string_equals_literal_ci(key, "cc")) {
234
0
      PHP_MAIL_BUILD_HEADER_CHECK("cc", s, key, val);
235
0
    } else if (zend_string_equals_literal_ci(key, "bcc")) {
236
0
      PHP_MAIL_BUILD_HEADER_CHECK("bcc", s, key, val);
237
0
    } else if (zend_string_equals_literal_ci(key, "message-id")) {
238
0
      PHP_MAIL_BUILD_HEADER_CHECK("message-id", s, key, val);
239
0
    } else if (zend_string_equals_literal_ci(key, "references")) {
240
0
      PHP_MAIL_BUILD_HEADER_CHECK("references", s, key, val);
241
0
    } else if (zend_string_equals_literal_ci(key, "in-reply-to")) {
242
0
      PHP_MAIL_BUILD_HEADER_CHECK("in-reply-to", s, key, val);
243
0
    } else if (zend_string_equals_literal_ci(key, "subject")) {
244
0
      zend_value_error("The additional headers cannot contain the \"Subject\" header");
245
0
    } else {
246
0
      php_mail_build_headers_elem(&s, key, val);
247
0
    }
248
249
0
    if (EG(exception)) {
250
0
      smart_str_free(&s);
251
0
      return NULL;
252
0
    }
253
0
  } ZEND_HASH_FOREACH_END();
254
255
  /* Remove the last \r\n */
256
0
  if (s.s) s.s->len -= 2;
257
0
  smart_str_0(&s);
258
259
0
  return s.s;
260
0
}
261
262
263
/* {{{ Send an email message */
264
PHP_FUNCTION(mail)
265
0
{
266
0
  char *to=NULL, *message=NULL;
267
0
  char *subject=NULL;
268
0
  zend_string *extra_cmd=NULL;
269
0
  zend_string *headers_str = NULL;
270
0
  HashTable *headers_ht = NULL;
271
0
  size_t to_len, message_len;
272
0
  size_t subject_len, i;
273
0
  char *to_r, *subject_r;
274
275
0
  ZEND_PARSE_PARAMETERS_START(3, 5)
276
0
    Z_PARAM_PATH(to, to_len)
277
0
    Z_PARAM_PATH(subject, subject_len)
278
0
    Z_PARAM_PATH(message, message_len)
279
0
    Z_PARAM_OPTIONAL
280
0
    Z_PARAM_ARRAY_HT_OR_STR(headers_ht, headers_str)
281
0
    Z_PARAM_PATH_STR(extra_cmd)
282
0
  ZEND_PARSE_PARAMETERS_END();
283
284
0
  if (headers_str) {
285
0
    if (UNEXPECTED(zend_str_has_nul_byte(headers_str))) {
286
0
      zend_argument_value_error(4, "must not contain any null bytes");
287
0
      RETURN_THROWS();
288
0
    }
289
0
    headers_str = php_trim(headers_str, NULL, 0, 2);
290
0
  } else if (headers_ht) {
291
0
    headers_str = php_mail_build_headers(headers_ht);
292
0
    if (EG(exception)) {
293
0
      RETURN_THROWS();
294
0
    }
295
0
  }
296
297
0
  if (to_len > 0) {
298
0
    to_r = estrndup(to, to_len);
299
0
    for (; to_len; to_len--) {
300
0
      if (!isspace((unsigned char) to_r[to_len - 1])) {
301
0
        break;
302
0
      }
303
0
      to_r[to_len - 1] = '\0';
304
0
    }
305
0
    for (i = 0; to_r[i]; i++) {
306
0
      if (iscntrl((unsigned char) to_r[i])) {
307
        /* According to RFC 822, section 3.1.1 long headers may be separated into
308
         * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
309
         * To prevent these separators from being replaced with a space, we use the
310
         * SKIP_LONG_HEADER_SEP to skip over them. */
311
0
        SKIP_LONG_HEADER_SEP(to_r, i);
312
0
        to_r[i] = ' ';
313
0
      }
314
0
    }
315
0
  } else {
316
0
    to_r = to;
317
0
  }
318
319
0
  if (subject_len > 0) {
320
0
    subject_r = estrndup(subject, subject_len);
321
0
    for (; subject_len; subject_len--) {
322
0
      if (!isspace((unsigned char) subject_r[subject_len - 1])) {
323
0
        break;
324
0
      }
325
0
      subject_r[subject_len - 1] = '\0';
326
0
    }
327
0
    for (i = 0; subject_r[i]; i++) {
328
0
      if (iscntrl((unsigned char) subject_r[i])) {
329
0
        SKIP_LONG_HEADER_SEP(subject_r, i);
330
0
        subject_r[i] = ' ';
331
0
      }
332
0
    }
333
0
  } else {
334
0
    subject_r = subject;
335
0
  }
336
337
0
  zend_string *force_extra_parameters = zend_ini_str_literal("mail.force_extra_parameters");
338
0
  if (force_extra_parameters && ZSTR_LEN(force_extra_parameters) > 0) {
339
0
    extra_cmd = php_escape_shell_cmd(force_extra_parameters);
340
0
  } else if (extra_cmd) {
341
0
    extra_cmd = php_escape_shell_cmd(extra_cmd);
342
0
  }
343
344
0
  if (php_mail(to_r, subject_r, message, headers_str && ZSTR_LEN(headers_str) ? ZSTR_VAL(headers_str) : NULL, extra_cmd)) {
345
0
    RETVAL_TRUE;
346
0
  } else {
347
0
    RETVAL_FALSE;
348
0
  }
349
350
0
  if (headers_str) {
351
0
    zend_string_release_ex(headers_str, 0);
352
0
  }
353
354
0
  if (extra_cmd) {
355
0
    zend_string_release_ex(extra_cmd, 0);
356
0
  }
357
0
  if (to_r != to) {
358
0
    efree(to_r);
359
0
  }
360
0
  if (subject_r != subject) {
361
0
    efree(subject_r);
362
0
  }
363
0
}
364
/* }}} */
365
366
367
0
static void php_mail_log_crlf_to_spaces(char *message) {
368
  /* Find all instances of carriage returns or line feeds and
369
   * replace them with spaces. Thus, a log line is always one line
370
   * long
371
   */
372
0
  char *p = message;
373
0
  while ((p = strpbrk(p, "\r\n"))) {
374
0
    *p = ' ';
375
0
  }
376
0
}
377
378
0
static void php_mail_log_to_syslog(char *message) {
379
  /* Write 'message' to syslog. */
380
0
#ifdef HAVE_SYSLOG_H
381
0
  php_syslog(LOG_NOTICE, "%s", message);
382
0
#endif
383
0
}
384
385
386
0
static void php_mail_log_to_file(const zend_string *filename, const char *message, size_t message_size) {
387
  /* Write 'message' to the given file. */
388
0
  uint32_t flags = REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
389
0
  php_stream *stream = php_stream_open_wrapper(ZSTR_VAL(filename), "a", flags, NULL);
390
0
  if (stream) {
391
0
    php_stream_write(stream, message, message_size);
392
0
    php_stream_close(stream);
393
0
  }
394
0
}
395
396
397
0
static int php_mail_detect_multiple_crlf(const char *hdr) {
398
  /* This function detects multiple/malformed multiple newlines. */
399
400
0
  if (!hdr || !strlen(hdr)) {
401
0
    return 0;
402
0
  }
403
404
  /* Should not have any newlines at the beginning. */
405
  /* RFC 2822 2.2. Header Fields */
406
0
  if (*hdr < 33 || *hdr > 126 || *hdr == ':') {
407
0
    return 1;
408
0
  }
409
410
0
  while(*hdr) {
411
0
    if (*hdr == '\r') {
412
0
      if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) {
413
        /* Malformed or multiple newlines. */
414
0
        return 1;
415
0
      } else {
416
0
        hdr += 2;
417
0
      }
418
0
    } else if (*hdr == '\n') {
419
0
      if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') {
420
        /* Malformed or multiple newlines. */
421
0
        return 1;
422
0
      } else {
423
0
        hdr += 2;
424
0
      }
425
0
    } else {
426
0
      hdr++;
427
0
    }
428
0
  }
429
430
0
  return 0;
431
0
}
432
433
434
/* {{{ php_mail */
435
PHPAPI bool php_mail(const char *to, const char *subject, const char *message, const char *headers, const zend_string *extra_cmd)
436
0
{
437
0
  FILE *sendmail;
438
0
  const char *sendmail_path = zend_ini_string_literal("sendmail_path");
439
0
  char *sendmail_cmd = NULL;
440
0
  const zend_string *mail_log = zend_ini_str_literal("mail.log");
441
0
  const char *hdr = headers;
442
0
  char *ahdr = NULL;
443
#if PHP_SIGCHILD
444
  void (*sig_handler)(int) = NULL;
445
#endif
446
447
0
#define MAIL_RET(val) \
448
0
  if (ahdr != NULL) { \
449
0
    efree(ahdr); \
450
0
  } \
451
0
  return val; \
452
0
453
0
  if (mail_log && ZSTR_LEN(mail_log)) {
454
0
    char *logline;
455
456
0
    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);
457
458
0
    if (hdr) {
459
0
      php_mail_log_crlf_to_spaces(logline);
460
0
    }
461
462
0
    if (zend_string_equals_literal(mail_log, "syslog")) {
463
0
      php_mail_log_to_syslog(logline);
464
0
    } else {
465
      /* Add date when logging to file */
466
0
      char *tmp;
467
0
      time_t curtime;
468
0
      zend_string *date_str;
469
0
      size_t len;
470
471
472
0
      time(&curtime);
473
0
      date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1);
474
0
      len = spprintf(&tmp, 0, "[%s] %s%s", date_str->val, logline, PHP_EOL);
475
476
0
      php_mail_log_to_file(mail_log, tmp, len);
477
478
0
      zend_string_free(date_str);
479
0
      efree(tmp);
480
0
    }
481
482
0
    efree(logline);
483
0
  }
484
485
0
  if (EG(exception)) {
486
0
    MAIL_RET(false);
487
0
  }
488
489
0
  const char *line_sep;
490
0
  zend_string *cr_lf_mode = PG(mail_cr_lf_mode);
491
  
492
0
  if (cr_lf_mode && !zend_string_equals_literal(cr_lf_mode, "crlf")) {
493
0
    if (zend_string_equals_literal(cr_lf_mode, "lf")) {
494
0
      line_sep = "\n";
495
0
    } else if (zend_string_equals_literal(cr_lf_mode, "mixed")) {
496
0
      line_sep = "\n";
497
0
    } else if (zend_string_equals_literal(cr_lf_mode, "os")) {
498
#ifdef PHP_WIN32
499
      line_sep = "\r\n";
500
#else
501
0
      line_sep = "\n";
502
0
#endif
503
0
    } else {
504
0
      ZEND_ASSERT(0 && "Unexpected cr_lf_mode value");
505
0
    }
506
0
  } else {
507
    /* CRLF is default mode, but respect mail.mixed_lf_and_crlf for backward compatibility */
508
0
    line_sep = PG(mail_mixed_lf_and_crlf) ? "\n" : "\r\n";
509
0
  }
510
511
0
  if (PG(mail_x_header)) {
512
0
    const char *tmp = zend_get_executed_filename();
513
0
    zend_string *f;
514
515
0
    f = php_basename(tmp, strlen(tmp), NULL, 0);
516
517
0
    if (headers != NULL && *headers) {
518
0
      spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s%s%s", php_getuid(), ZSTR_VAL(f), line_sep, headers);
519
0
    } else {
520
0
      spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
521
0
    }
522
0
    hdr = ahdr;
523
0
    zend_string_release_ex(f, 0);
524
0
  }
525
526
0
  if (hdr && php_mail_detect_multiple_crlf(hdr)) {
527
0
    php_error_docref(NULL, E_WARNING, "Multiple or malformed newlines found in additional_header");
528
0
    MAIL_RET(false);
529
0
  }
530
531
0
  if (!sendmail_path) {
532
#ifdef PHP_WIN32
533
    int tsm_err;
534
    char *tsm_errmsg = NULL;
535
536
    /* handle old style win smtp sending */
537
    if (TSendMail(zend_ini_string_literal("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message) == FAILURE) {
538
      if (tsm_errmsg) {
539
        php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg);
540
        efree(tsm_errmsg);
541
      } else {
542
        php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err));
543
      }
544
      MAIL_RET(false);
545
    }
546
    MAIL_RET(true);
547
#else
548
0
    MAIL_RET(false);
549
0
#endif
550
0
  }
551
0
  if (extra_cmd != NULL) {
552
0
    spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, ZSTR_VAL(extra_cmd));
553
0
  } else {
554
0
    sendmail_cmd = (char*)sendmail_path;
555
0
  }
556
557
#if PHP_SIGCHILD
558
  /* Set signal handler of SIGCHLD to default to prevent other signal handlers
559
   * from being called and reaping the return code when our child exits.
560
   * The original handler needs to be restored after pclose() */
561
  sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
562
  if (sig_handler == SIG_ERR) {
563
    sig_handler = NULL;
564
  }
565
#endif
566
567
#ifdef PHP_WIN32
568
  sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL);
569
#else
570
  /* Since popen() doesn't indicate if the internal fork() doesn't work
571
   * (e.g. the shell can't be executed) we explicitly set it to 0 to be
572
   * sure we don't catch any older errno value. */
573
0
  errno = 0;
574
0
  sendmail = popen(sendmail_cmd, "w");
575
0
#endif
576
0
  if (extra_cmd != NULL) {
577
0
    efree(sendmail_cmd);
578
0
  }
579
580
0
  if (sendmail) {
581
0
    int ret;
582
0
#ifndef PHP_WIN32
583
0
    if (EACCES == errno) {
584
0
      php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
585
0
      pclose(sendmail);
586
#if PHP_SIGCHILD
587
      /* Restore handler in case of error on Windows
588
         Not sure if this applicable on Win but just in case. */
589
      if (sig_handler) {
590
        signal(SIGCHLD, sig_handler);
591
      }
592
#endif
593
0
      MAIL_RET(false);
594
0
    }
595
0
#endif
596
0
    fprintf(sendmail, "To: %s%s", to, line_sep);
597
0
    fprintf(sendmail, "Subject: %s%s", subject, line_sep);
598
0
    if (hdr != NULL) {
599
0
      fprintf(sendmail, "%s%s", hdr, line_sep);
600
0
    }
601
602
0
    fprintf(sendmail, "%s", line_sep);
603
    
604
0
    if (cr_lf_mode && zend_string_equals_literal(cr_lf_mode, "lf")) {
605
0
      char *converted_message = NULL;
606
0
      size_t msg_len = strlen(message);
607
0
      size_t new_len = 0;
608
609
0
      if (msg_len > 0) {
610
0
        for (size_t i = 0; i < msg_len - 1; ++i) {
611
0
          if (message[i] == '\r' && message[i + 1] == '\n') {
612
0
            ++new_len;
613
0
          }
614
0
        }
615
616
0
        if (new_len == 0) {
617
0
          fprintf(sendmail, "%s", message);
618
0
        } else {
619
0
          converted_message = emalloc(msg_len - new_len + 1);
620
0
          size_t j = 0;
621
0
          for (size_t i = 0; i < msg_len; ++i) {
622
0
            if (i < msg_len - 1 && message[i] == '\r' && message[i + 1] == '\n') {
623
0
              converted_message[j++] = '\n';
624
0
              ++i; /* skip LF part */
625
0
            } else {
626
0
              converted_message[j++] = message[i];
627
0
            }
628
0
          }
629
630
0
          converted_message[j] = '\0';
631
0
          fprintf(sendmail, "%s", converted_message);
632
0
          efree(converted_message);
633
0
        }
634
0
      }
635
0
    } else {
636
0
      fprintf(sendmail, "%s", message);
637
0
    }
638
639
0
    fprintf(sendmail, "%s", line_sep);
640
#ifdef PHP_WIN32
641
    ret = pclose(sendmail);
642
643
#if PHP_SIGCHILD
644
    if (sig_handler) {
645
      signal(SIGCHLD, sig_handler);
646
    }
647
#endif
648
#else
649
0
    int wstatus = pclose(sendmail);
650
#if PHP_SIGCHILD
651
    if (sig_handler) {
652
      signal(SIGCHLD, sig_handler);
653
    }
654
#endif
655
    /* Determine the wait(2) exit status */
656
0
    if (wstatus == -1) {
657
0
      php_error_docref(NULL, E_WARNING, "Sendmail pclose failed %d (%s)", errno, strerror(errno));
658
0
      MAIL_RET(false);
659
0
    } else if (WIFSIGNALED(wstatus)) {
660
0
      php_error_docref(NULL, E_WARNING, "Sendmail killed by signal %d (%s)", WTERMSIG(wstatus), strsignal(WTERMSIG(wstatus)));
661
0
      MAIL_RET(false);
662
0
    } else {
663
0
      if (WIFEXITED(wstatus)) {
664
0
        ret = WEXITSTATUS(wstatus);
665
0
      } else {
666
0
        php_error_docref(NULL, E_WARNING, "Sendmail did not exit");
667
0
        MAIL_RET(false);
668
0
      }
669
0
    }
670
0
#endif
671
672
0
#if defined(EX_TEMPFAIL)
673
0
    if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
674
#elif defined(EX_OK)
675
    if (ret != EX_OK)
676
#else
677
    if (ret != 0)
678
#endif
679
0
    {
680
0
      php_error_docref(NULL, E_WARNING, "Sendmail exited with non-zero exit code %d", ret);
681
0
      MAIL_RET(false);
682
0
    } else {
683
0
      MAIL_RET(true);
684
0
    }
685
0
  } else {
686
0
    php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
687
#if PHP_SIGCHILD
688
    if (sig_handler) {
689
      signal(SIGCHLD, sig_handler);
690
    }
691
#endif
692
0
    MAIL_RET(false);
693
0
  }
694
695
0
  MAIL_RET(true); /* never reached */
696
0
}
697
/* }}} */
698
699
/* {{{ PHP_MINFO_FUNCTION */
700
PHP_MINFO_FUNCTION(mail)
701
0
{
702
0
  const char *sendmail_path = zend_ini_string_literal("sendmail_path");
703
704
#ifdef PHP_WIN32
705
  if (!sendmail_path) {
706
    php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
707
  } else {
708
    php_info_print_table_row(2, "Path to sendmail", sendmail_path);
709
  }
710
#else
711
0
  php_info_print_table_row(2, "Path to sendmail", sendmail_path);
712
0
#endif
713
0
}
714
/* }}} */