mail.c 15 KB

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