1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2014 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Rasmus Lerdorf <rasmus@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include "php.h"
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_string.h"
27 #include "ext/standard/basic_functions.h"
28
29 #if HAVE_SYSEXITS_H
30 #include <sysexits.h>
31 #endif
32 #if HAVE_SYS_SYSEXITS_H
33 #include <sys/sysexits.h>
34 #endif
35
36 #if PHP_SIGCHILD
37 #if HAVE_SIGNAL_H
38 #include <signal.h>
39 #endif
40 #endif
41
42 #include "php_syslog.h"
43 #include "php_mail.h"
44 #include "php_ini.h"
45 #include "php_string.h"
46 #include "exec.h"
47
48 #ifdef PHP_WIN32
49 #include "win32/sendmail.h"
50 #endif
51
52 #ifdef NETWARE
53 #define EX_OK 0 /* successful termination */
54 #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
55 #endif
56
57 #define SKIP_LONG_HEADER_SEP(str, pos) \
58 if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) { \
59 pos += 2; \
60 while (str[pos + 1] == ' ' || str[pos + 1] == '\t') { \
61 pos++; \
62 } \
63 continue; \
64 } \
65
66 #define MAIL_ASCIIZ_CHECK(str, len) \
67 p = str; \
68 e = p + len; \
69 while ((p = memchr(p, '\0', (e - p)))) { \
70 *p = ' '; \
71 } \
72
73 extern long php_getuid(TSRMLS_D);
74
75 /* {{{ proto int ezmlm_hash(string addr)
76 Calculate EZMLM list hash value. */
PHP_FUNCTION(ezmlm_hash)77 PHP_FUNCTION(ezmlm_hash)
78 {
79 char *str = NULL;
80 unsigned int h = 5381;
81 int j, str_len;
82
83 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
84 return;
85 }
86
87 for (j = 0; j < str_len; j++) {
88 h = (h + (h << 5)) ^ (unsigned long) (unsigned char) tolower(str[j]);
89 }
90
91 h = (h % 53);
92
93 RETURN_LONG((int) h);
94 }
95 /* }}} */
96
97 /* {{{ proto int mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
98 Send an email message */
PHP_FUNCTION(mail)99 PHP_FUNCTION(mail)
100 {
101 char *to=NULL, *message=NULL, *headers=NULL, *headers_trimmed=NULL;
102 char *subject=NULL, *extra_cmd=NULL;
103 int to_len, message_len, headers_len = 0;
104 int subject_len, extra_cmd_len = 0, i;
105 char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
106 char *to_r, *subject_r;
107 char *p, *e;
108
109 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss", &to, &to_len, &subject, &subject_len, &message, &message_len, &headers, &headers_len, &extra_cmd, &extra_cmd_len) == FAILURE) {
110 return;
111 }
112
113 /* ASCIIZ check */
114 MAIL_ASCIIZ_CHECK(to, to_len);
115 MAIL_ASCIIZ_CHECK(subject, subject_len);
116 MAIL_ASCIIZ_CHECK(message, message_len);
117 if (headers) {
118 MAIL_ASCIIZ_CHECK(headers, headers_len);
119 headers_trimmed = php_trim(headers, headers_len, NULL, 0, NULL, 2 TSRMLS_CC);
120 }
121 if (extra_cmd) {
122 MAIL_ASCIIZ_CHECK(extra_cmd, extra_cmd_len);
123 }
124
125 if (to_len > 0) {
126 to_r = estrndup(to, to_len);
127 for (; to_len; to_len--) {
128 if (!isspace((unsigned char) to_r[to_len - 1])) {
129 break;
130 }
131 to_r[to_len - 1] = '\0';
132 }
133 for (i = 0; to_r[i]; i++) {
134 if (iscntrl((unsigned char) to_r[i])) {
135 /* According to RFC 822, section 3.1.1 long headers may be separated into
136 * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
137 * To prevent these separators from being replaced with a space, we use the
138 * SKIP_LONG_HEADER_SEP to skip over them. */
139 SKIP_LONG_HEADER_SEP(to_r, i);
140 to_r[i] = ' ';
141 }
142 }
143 } else {
144 to_r = to;
145 }
146
147 if (subject_len > 0) {
148 subject_r = estrndup(subject, subject_len);
149 for (; subject_len; subject_len--) {
150 if (!isspace((unsigned char) subject_r[subject_len - 1])) {
151 break;
152 }
153 subject_r[subject_len - 1] = '\0';
154 }
155 for (i = 0; subject_r[i]; i++) {
156 if (iscntrl((unsigned char) subject_r[i])) {
157 SKIP_LONG_HEADER_SEP(subject_r, i);
158 subject_r[i] = ' ';
159 }
160 }
161 } else {
162 subject_r = subject;
163 }
164
165 if (force_extra_parameters) {
166 extra_cmd = php_escape_shell_cmd(force_extra_parameters);
167 } else if (extra_cmd) {
168 extra_cmd = php_escape_shell_cmd(extra_cmd);
169 }
170
171 if (php_mail(to_r, subject_r, message, headers_trimmed, extra_cmd TSRMLS_CC)) {
172 RETVAL_TRUE;
173 } else {
174 RETVAL_FALSE;
175 }
176
177 if (headers_trimmed) {
178 efree(headers_trimmed);
179 }
180
181 if (extra_cmd) {
182 efree (extra_cmd);
183 }
184 if (to_r != to) {
185 efree(to_r);
186 }
187 if (subject_r != subject) {
188 efree(subject_r);
189 }
190 }
191 /* }}} */
192
193
php_mail_log_crlf_to_spaces(char * message)194 void php_mail_log_crlf_to_spaces(char *message) {
195 /* Find all instances of carriage returns or line feeds and
196 * replace them with spaces. Thus, a log line is always one line
197 * long
198 */
199 char *p = message;
200 while ((p = strpbrk(p, "\r\n"))) {
201 *p = ' ';
202 }
203 }
204
php_mail_log_to_syslog(char * message)205 void php_mail_log_to_syslog(char *message) {
206 /* Write 'message' to syslog. */
207 #ifdef HAVE_SYSLOG_H
208 php_syslog(LOG_NOTICE, "%s", message);
209 #endif
210 }
211
212
php_mail_log_to_file(char * filename,char * message,size_t message_size TSRMLS_DC)213 void php_mail_log_to_file(char *filename, char *message, size_t message_size TSRMLS_DC) {
214 /* Write 'message' to the given file. */
215 uint flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
216 php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL);
217 if (stream) {
218 php_stream_write(stream, message, message_size);
219 php_stream_close(stream);
220 }
221 }
222
223
php_mail_detect_multiple_crlf(char * hdr)224 static int php_mail_detect_multiple_crlf(char *hdr) {
225 /* This function detects multiple/malformed multiple newlines. */
226 size_t len;
227
228 if (!hdr || !strlen(hdr)) {
229 return 0;
230 }
231
232 /* Should not have any newlines at the beginning. */
233 /* RFC 2822 2.2. Header Fields */
234 if (*hdr < 33 || *hdr > 126 || *hdr == ':') {
235 return 1;
236 }
237
238 while(*hdr) {
239 if (*hdr == '\r') {
240 if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) {
241 /* Malformed or multiple newlines. */
242 return 1;
243 } else {
244 hdr += 2;
245 }
246 } else if (*hdr == '\n') {
247 if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') {
248 /* Malformed or multiple newlines. */
249 return 1;
250 } else {
251 hdr += 2;
252 }
253 } else {
254 hdr++;
255 }
256 }
257
258 return 0;
259 }
260
261
262 /* {{{ php_mail
263 */
php_mail(char * to,char * subject,char * message,char * headers,char * extra_cmd TSRMLS_DC)264 PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC)
265 {
266 #if (defined PHP_WIN32 || defined NETWARE)
267 int tsm_err;
268 char *tsm_errmsg = NULL;
269 #endif
270 FILE *sendmail;
271 int ret;
272 char *sendmail_path = INI_STR("sendmail_path");
273 char *sendmail_cmd = NULL;
274 char *mail_log = INI_STR("mail.log");
275 char *hdr = headers;
276 #if PHP_SIGCHILD
277 void (*sig_handler)() = NULL;
278 #endif
279
280 #define MAIL_RET(val) \
281 if (hdr != headers) { \
282 efree(hdr); \
283 } \
284 return val; \
285
286 if (mail_log && *mail_log) {
287 char *tmp;
288 int l = spprintf(&tmp, 0, "mail() on [%s:%d]: To: %s -- Headers: %s\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");
289
290 if (hdr) {
291 php_mail_log_crlf_to_spaces(tmp);
292 }
293
294 if (!strcmp(mail_log, "syslog")) {
295 /* Drop the final space when logging to syslog. */
296 tmp[l - 1] = 0;
297 php_mail_log_to_syslog(tmp);
298 }
299 else {
300 /* Convert the final space to a newline when logging to file. */
301 tmp[l - 1] = '\n';
302 php_mail_log_to_file(mail_log, tmp, l TSRMLS_CC);
303 }
304
305 efree(tmp);
306 }
307
308 if (PG(mail_x_header)) {
309 const char *tmp = zend_get_executed_filename(TSRMLS_C);
310 char *f;
311 size_t f_len;
312
313 php_basename(tmp, strlen(tmp), NULL, 0,&f, &f_len TSRMLS_CC);
314
315 if (headers != NULL && *headers) {
316 spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n%s", php_getuid(TSRMLS_C), f, headers);
317 } else {
318 spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s", php_getuid(TSRMLS_C), f);
319 }
320 efree(f);
321 }
322
323 if (hdr && php_mail_detect_multiple_crlf(hdr)) {
324 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Multiple or malformed newlines found in additional_header");
325 MAIL_RET(0);
326 }
327
328 if (!sendmail_path) {
329 #if (defined PHP_WIN32 || defined NETWARE)
330 /* handle old style win smtp sending */
331 if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) {
332 if (tsm_errmsg) {
333 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg);
334 efree(tsm_errmsg);
335 } else {
336 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err));
337 }
338 MAIL_RET(0);
339 }
340 MAIL_RET(1);
341 #else
342 MAIL_RET(0);
343 #endif
344 }
345 if (extra_cmd != NULL) {
346 spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
347 } else {
348 sendmail_cmd = sendmail_path;
349 }
350
351 #if PHP_SIGCHILD
352 /* Set signal handler of SIGCHLD to default to prevent other signal handlers
353 * from being called and reaping the return code when our child exits.
354 * The original handler needs to be restored after pclose() */
355 sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
356 if (sig_handler == SIG_ERR) {
357 sig_handler = NULL;
358 }
359 #endif
360
361 #ifdef PHP_WIN32
362 sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL TSRMLS_CC);
363 #else
364 /* Since popen() doesn't indicate if the internal fork() doesn't work
365 * (e.g. the shell can't be executed) we explicitly set it to 0 to be
366 * sure we don't catch any older errno value. */
367 errno = 0;
368 sendmail = popen(sendmail_cmd, "w");
369 #endif
370 if (extra_cmd != NULL) {
371 efree (sendmail_cmd);
372 }
373
374 if (sendmail) {
375 #ifndef PHP_WIN32
376 if (EACCES == errno) {
377 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
378 pclose(sendmail);
379 #if PHP_SIGCHILD
380 /* Restore handler in case of error on Windows
381 Not sure if this applicable on Win but just in case. */
382 if (sig_handler) {
383 signal(SIGCHLD, sig_handler);
384 }
385 #endif
386 MAIL_RET(0);
387 }
388 #endif
389 fprintf(sendmail, "To: %s\n", to);
390 fprintf(sendmail, "Subject: %s\n", subject);
391 if (hdr != NULL) {
392 fprintf(sendmail, "%s\n", hdr);
393 }
394 fprintf(sendmail, "\n%s\n", message);
395 ret = pclose(sendmail);
396
397 #if PHP_SIGCHILD
398 if (sig_handler) {
399 signal(SIGCHLD, sig_handler);
400 }
401 #endif
402
403 #ifdef PHP_WIN32
404 if (ret == -1)
405 #else
406 #if defined(EX_TEMPFAIL)
407 if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
408 #elif defined(EX_OK)
409 if (ret != EX_OK)
410 #else
411 if (ret != 0)
412 #endif
413 #endif
414 {
415 MAIL_RET(0);
416 } else {
417 MAIL_RET(1);
418 }
419 } else {
420 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
421 #if PHP_SIGCHILD
422 if (sig_handler) {
423 signal(SIGCHLD, sig_handler);
424 }
425 #endif
426 MAIL_RET(0);
427 }
428
429 MAIL_RET(1); /* never reached */
430 }
431 /* }}} */
432
433 /* {{{ PHP_MINFO_FUNCTION
434 */
PHP_MINFO_FUNCTION(mail)435 PHP_MINFO_FUNCTION(mail)
436 {
437 char *sendmail_path = INI_STR("sendmail_path");
438
439 #ifdef PHP_WIN32
440 if (!sendmail_path) {
441 php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
442 } else {
443 php_info_print_table_row(2, "Path to sendmail", sendmail_path);
444 }
445 #else
446 php_info_print_table_row(2, "Path to sendmail", sendmail_path);
447 #endif
448 }
449 /* }}} */
450
451 /*
452 * Local variables:
453 * tab-width: 4
454 * c-basic-offset: 4
455 * End:
456 * vim600: sw=4 ts=4 fdm=marker
457 * vim<600: sw=4 ts=4
458 */
459