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