/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 | | /* {{{ proto bool mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]]) |
252 | | Send an email message */ |
253 | | PHP_FUNCTION(mail) |
254 | 0 | { |
255 | 0 | char *to=NULL, *message=NULL; |
256 | 0 | char *subject=NULL; |
257 | 0 | zend_string *extra_cmd=NULL; |
258 | 0 | zend_string *headers_str = NULL; |
259 | 0 | HashTable *headers_ht = NULL; |
260 | 0 | size_t to_len, message_len; |
261 | 0 | size_t subject_len, i; |
262 | 0 | char *force_extra_parameters = INI_STR("mail.force_extra_parameters"); |
263 | 0 | char *to_r, *subject_r; |
264 | 0 | char *p, *e; |
265 | |
|
266 | 0 | ZEND_PARSE_PARAMETERS_START(3, 5) |
267 | 0 | Z_PARAM_STRING(to, to_len) |
268 | 0 | Z_PARAM_STRING(subject, subject_len) |
269 | 0 | Z_PARAM_STRING(message, message_len) |
270 | 0 | Z_PARAM_OPTIONAL |
271 | 0 | Z_PARAM_STR_OR_ARRAY_HT(headers_str, headers_ht) |
272 | 0 | Z_PARAM_STR(extra_cmd) |
273 | 0 | ZEND_PARSE_PARAMETERS_END(); |
274 | | |
275 | | /* ASCIIZ check */ |
276 | 0 | MAIL_ASCIIZ_CHECK(to, to_len); |
277 | 0 | MAIL_ASCIIZ_CHECK(subject, subject_len); |
278 | 0 | MAIL_ASCIIZ_CHECK(message, message_len); |
279 | 0 | if (headers_str) { |
280 | 0 | MAIL_ASCIIZ_CHECK(ZSTR_VAL(headers_str), ZSTR_LEN(headers_str)); |
281 | 0 | headers_str = php_trim(headers_str, NULL, 0, 2); |
282 | 0 | } else if (headers_ht) { |
283 | 0 | headers_str = php_mail_build_headers(headers_ht); |
284 | 0 | } |
285 | |
|
286 | 0 | if (extra_cmd) { |
287 | 0 | MAIL_ASCIIZ_CHECK(ZSTR_VAL(extra_cmd), ZSTR_LEN(extra_cmd)); |
288 | 0 | } |
289 | |
|
290 | 0 | if (to_len > 0) { |
291 | 0 | to_r = estrndup(to, to_len); |
292 | 0 | for (; to_len; to_len--) { |
293 | 0 | if (!isspace((unsigned char) to_r[to_len - 1])) { |
294 | 0 | break; |
295 | 0 | } |
296 | 0 | to_r[to_len - 1] = '\0'; |
297 | 0 | } |
298 | 0 | for (i = 0; to_r[i]; i++) { |
299 | 0 | if (iscntrl((unsigned char) to_r[i])) { |
300 | | /* According to RFC 822, section 3.1.1 long headers may be separated into |
301 | | * parts using CRLF followed at least one linear-white-space character ('\t' or ' '). |
302 | | * To prevent these separators from being replaced with a space, we use the |
303 | | * SKIP_LONG_HEADER_SEP to skip over them. */ |
304 | 0 | SKIP_LONG_HEADER_SEP(to_r, i); |
305 | 0 | to_r[i] = ' '; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | } else { |
309 | 0 | to_r = to; |
310 | 0 | } |
311 | |
|
312 | 0 | if (subject_len > 0) { |
313 | 0 | subject_r = estrndup(subject, subject_len); |
314 | 0 | for (; subject_len; subject_len--) { |
315 | 0 | if (!isspace((unsigned char) subject_r[subject_len - 1])) { |
316 | 0 | break; |
317 | 0 | } |
318 | 0 | subject_r[subject_len - 1] = '\0'; |
319 | 0 | } |
320 | 0 | for (i = 0; subject_r[i]; i++) { |
321 | 0 | if (iscntrl((unsigned char) subject_r[i])) { |
322 | 0 | SKIP_LONG_HEADER_SEP(subject_r, i); |
323 | 0 | subject_r[i] = ' '; |
324 | 0 | } |
325 | 0 | } |
326 | 0 | } else { |
327 | 0 | subject_r = subject; |
328 | 0 | } |
329 | |
|
330 | 0 | if (force_extra_parameters) { |
331 | 0 | extra_cmd = php_escape_shell_cmd(force_extra_parameters); |
332 | 0 | } else if (extra_cmd) { |
333 | 0 | extra_cmd = php_escape_shell_cmd(ZSTR_VAL(extra_cmd)); |
334 | 0 | } |
335 | |
|
336 | 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)) { |
337 | 0 | RETVAL_TRUE; |
338 | 0 | } else { |
339 | 0 | RETVAL_FALSE; |
340 | 0 | } |
341 | |
|
342 | 0 | if (headers_str) { |
343 | 0 | zend_string_release_ex(headers_str, 0); |
344 | 0 | } |
345 | |
|
346 | 0 | if (extra_cmd) { |
347 | 0 | zend_string_release_ex(extra_cmd, 0); |
348 | 0 | } |
349 | 0 | if (to_r != to) { |
350 | 0 | efree(to_r); |
351 | 0 | } |
352 | 0 | if (subject_r != subject) { |
353 | 0 | efree(subject_r); |
354 | 0 | } |
355 | 0 | } |
356 | | /* }}} */ |
357 | | |
358 | | |
359 | 0 | void php_mail_log_crlf_to_spaces(char *message) { |
360 | | /* Find all instances of carriage returns or line feeds and |
361 | | * replace them with spaces. Thus, a log line is always one line |
362 | | * long |
363 | | */ |
364 | 0 | char *p = message; |
365 | 0 | while ((p = strpbrk(p, "\r\n"))) { |
366 | 0 | *p = ' '; |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | 0 | void php_mail_log_to_syslog(char *message) { |
371 | | /* Write 'message' to syslog. */ |
372 | 0 | #ifdef HAVE_SYSLOG_H |
373 | 0 | php_syslog(LOG_NOTICE, "%s", message); |
374 | 0 | #endif |
375 | 0 | } |
376 | | |
377 | | |
378 | 0 | void php_mail_log_to_file(char *filename, char *message, size_t message_size) { |
379 | | /* Write 'message' to the given file. */ |
380 | 0 | uint32_t flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR; |
381 | 0 | php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL); |
382 | 0 | if (stream) { |
383 | 0 | php_stream_write(stream, message, message_size); |
384 | 0 | php_stream_close(stream); |
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | | |
389 | 0 | static int php_mail_detect_multiple_crlf(const char *hdr) { |
390 | | /* This function detects multiple/malformed multiple newlines. */ |
391 | |
|
392 | 0 | if (!hdr || !strlen(hdr)) { |
393 | 0 | return 0; |
394 | 0 | } |
395 | | |
396 | | /* Should not have any newlines at the beginning. */ |
397 | | /* RFC 2822 2.2. Header Fields */ |
398 | 0 | if (*hdr < 33 || *hdr > 126 || *hdr == ':') { |
399 | 0 | return 1; |
400 | 0 | } |
401 | | |
402 | 0 | while(*hdr) { |
403 | 0 | if (*hdr == '\r') { |
404 | 0 | if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) { |
405 | | /* Malformed or multiple newlines. */ |
406 | 0 | return 1; |
407 | 0 | } else { |
408 | 0 | hdr += 2; |
409 | 0 | } |
410 | 0 | } else if (*hdr == '\n') { |
411 | 0 | if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') { |
412 | | /* Malformed or multiple newlines. */ |
413 | 0 | return 1; |
414 | 0 | } else { |
415 | 0 | hdr += 2; |
416 | 0 | } |
417 | 0 | } else { |
418 | 0 | hdr++; |
419 | 0 | } |
420 | 0 | } |
421 | |
|
422 | 0 | return 0; |
423 | 0 | } |
424 | | |
425 | | |
426 | | /* {{{ php_mail |
427 | | */ |
428 | | PHPAPI int php_mail(const char *to, const char *subject, const char *message, const char *headers, const char *extra_cmd) |
429 | 0 | { |
430 | | #ifdef PHP_WIN32 |
431 | | int tsm_err; |
432 | | char *tsm_errmsg = NULL; |
433 | | #endif |
434 | 0 | FILE *sendmail; |
435 | 0 | int ret; |
436 | 0 | char *sendmail_path = INI_STR("sendmail_path"); |
437 | 0 | char *sendmail_cmd = NULL; |
438 | 0 | char *mail_log = INI_STR("mail.log"); |
439 | 0 | const char *hdr = headers; |
440 | 0 | char *ahdr = NULL; |
441 | | #if PHP_SIGCHILD |
442 | | void (*sig_handler)() = NULL; |
443 | | #endif |
444 | |
|
445 | 0 | #define MAIL_RET(val) \ |
446 | 0 | if (ahdr != NULL) { \ |
447 | 0 | efree(ahdr); \ |
448 | 0 | } \ |
449 | 0 | return val; \ |
450 | 0 |
|
451 | 0 | if (mail_log && *mail_log) { |
452 | 0 | char *logline; |
453 | |
|
454 | 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); |
455 | |
|
456 | 0 | if (hdr) { |
457 | 0 | php_mail_log_crlf_to_spaces(logline); |
458 | 0 | } |
459 | |
|
460 | 0 | if (!strcmp(mail_log, "syslog")) { |
461 | 0 | php_mail_log_to_syslog(logline); |
462 | 0 | } else { |
463 | | /* Add date when logging to file */ |
464 | 0 | char *tmp; |
465 | 0 | time_t curtime; |
466 | 0 | zend_string *date_str; |
467 | 0 | size_t len; |
468 | | |
469 | |
|
470 | 0 | time(&curtime); |
471 | 0 | date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1); |
472 | 0 | len = spprintf(&tmp, 0, "[%s] %s%s", date_str->val, logline, PHP_EOL); |
473 | |
|
474 | 0 | php_mail_log_to_file(mail_log, tmp, len); |
475 | |
|
476 | 0 | zend_string_free(date_str); |
477 | 0 | efree(tmp); |
478 | 0 | } |
479 | |
|
480 | 0 | efree(logline); |
481 | 0 | } |
482 | |
|
483 | 0 | if (PG(mail_x_header)) { |
484 | 0 | const char *tmp = zend_get_executed_filename(); |
485 | 0 | zend_string *f; |
486 | |
|
487 | 0 | f = php_basename(tmp, strlen(tmp), NULL, 0); |
488 | |
|
489 | 0 | if (headers != NULL && *headers) { |
490 | 0 | spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\r\n%s", php_getuid(), ZSTR_VAL(f), headers); |
491 | 0 | } else { |
492 | 0 | spprintf(&ahdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f)); |
493 | 0 | } |
494 | 0 | hdr = ahdr; |
495 | 0 | zend_string_release_ex(f, 0); |
496 | 0 | } |
497 | |
|
498 | 0 | if (hdr && php_mail_detect_multiple_crlf(hdr)) { |
499 | 0 | php_error_docref(NULL, E_WARNING, "Multiple or malformed newlines found in additional_header"); |
500 | 0 | MAIL_RET(0); |
501 | 0 | } |
502 | |
|
503 | 0 | if (!sendmail_path) { |
504 | | #ifdef PHP_WIN32 |
505 | | /* handle old style win smtp sending */ |
506 | | if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL) == FAILURE) { |
507 | | if (tsm_errmsg) { |
508 | | php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg); |
509 | | efree(tsm_errmsg); |
510 | | } else { |
511 | | php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err)); |
512 | | } |
513 | | MAIL_RET(0); |
514 | | } |
515 | | MAIL_RET(1); |
516 | | #else |
517 | 0 | MAIL_RET(0); |
518 | 0 | #endif |
519 | 0 | } |
520 | 0 | if (extra_cmd != NULL) { |
521 | 0 | spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd); |
522 | 0 | } else { |
523 | 0 | sendmail_cmd = sendmail_path; |
524 | 0 | } |
525 | |
|
526 | | #if PHP_SIGCHILD |
527 | | /* Set signal handler of SIGCHLD to default to prevent other signal handlers |
528 | | * from being called and reaping the return code when our child exits. |
529 | | * The original handler needs to be restored after pclose() */ |
530 | | sig_handler = (void *)signal(SIGCHLD, SIG_DFL); |
531 | | if (sig_handler == SIG_ERR) { |
532 | | sig_handler = NULL; |
533 | | } |
534 | | #endif |
535 | |
|
536 | | #ifdef PHP_WIN32 |
537 | | sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL); |
538 | | #else |
539 | | /* Since popen() doesn't indicate if the internal fork() doesn't work |
540 | | * (e.g. the shell can't be executed) we explicitly set it to 0 to be |
541 | | * sure we don't catch any older errno value. */ |
542 | 0 | errno = 0; |
543 | 0 | sendmail = popen(sendmail_cmd, "w"); |
544 | 0 | #endif |
545 | 0 | if (extra_cmd != NULL) { |
546 | 0 | efree (sendmail_cmd); |
547 | 0 | } |
548 | |
|
549 | 0 | if (sendmail) { |
550 | 0 | #ifndef PHP_WIN32 |
551 | 0 | if (EACCES == errno) { |
552 | 0 | php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path); |
553 | 0 | pclose(sendmail); |
554 | | #if PHP_SIGCHILD |
555 | | /* Restore handler in case of error on Windows |
556 | | Not sure if this applicable on Win but just in case. */ |
557 | | if (sig_handler) { |
558 | | signal(SIGCHLD, sig_handler); |
559 | | } |
560 | | #endif |
561 | 0 | MAIL_RET(0); |
562 | 0 | } |
563 | 0 | #endif |
564 | 0 | fprintf(sendmail, "To: %s\r\n", to); |
565 | 0 | fprintf(sendmail, "Subject: %s\r\n", subject); |
566 | 0 | if (hdr != NULL) { |
567 | 0 | fprintf(sendmail, "%s\r\n", hdr); |
568 | 0 | } |
569 | 0 | fprintf(sendmail, "\r\n%s\r\n", message); |
570 | 0 | ret = pclose(sendmail); |
571 | |
|
572 | | #if PHP_SIGCHILD |
573 | | if (sig_handler) { |
574 | | signal(SIGCHLD, sig_handler); |
575 | | } |
576 | | #endif |
577 | |
|
578 | | #ifdef PHP_WIN32 |
579 | | if (ret == -1) |
580 | | #else |
581 | 0 | #if defined(EX_TEMPFAIL) |
582 | 0 | if ((ret != EX_OK)&&(ret != EX_TEMPFAIL)) |
583 | | #elif defined(EX_OK) |
584 | | if (ret != EX_OK) |
585 | | #else |
586 | | if (ret != 0) |
587 | | #endif |
588 | 0 | #endif |
589 | 0 | { |
590 | 0 | MAIL_RET(0); |
591 | 0 | } else { |
592 | 0 | MAIL_RET(1); |
593 | 0 | } |
594 | 0 | } else { |
595 | 0 | php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path); |
596 | | #if PHP_SIGCHILD |
597 | | if (sig_handler) { |
598 | | signal(SIGCHLD, sig_handler); |
599 | | } |
600 | | #endif |
601 | 0 | MAIL_RET(0); |
602 | 0 | } |
603 | |
|
604 | 0 | MAIL_RET(1); /* never reached */ |
605 | 0 | } |
606 | | /* }}} */ |
607 | | |
608 | | /* {{{ PHP_MINFO_FUNCTION |
609 | | */ |
610 | | PHP_MINFO_FUNCTION(mail) |
611 | 0 | { |
612 | 0 | char *sendmail_path = INI_STR("sendmail_path"); |
613 | |
|
614 | | #ifdef PHP_WIN32 |
615 | | if (!sendmail_path) { |
616 | | php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled"); |
617 | | } else { |
618 | | php_info_print_table_row(2, "Path to sendmail", sendmail_path); |
619 | | } |
620 | | #else |
621 | 0 | php_info_print_table_row(2, "Path to sendmail", sendmail_path); |
622 | 0 | #endif |
623 | 0 | } |
624 | | /* }}} */ |