mail.c 12 KB

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