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