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