PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - mail.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 107 147 72.8 %
Date: 2015-08-27 Functions: 5 8 62.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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. */
      77           7 : PHP_FUNCTION(ezmlm_hash)
      78             : {
      79           7 :         char *str = NULL;
      80           7 :         unsigned int h = 5381;
      81             :         int j, str_len;
      82             : 
      83           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
      84           5 :                 return;
      85             :         }
      86             : 
      87          42 :         for (j = 0; j < str_len; j++) {
      88          40 :                 h = (h + (h << 5)) ^ (unsigned long) (unsigned char) tolower(str[j]);
      89             :         }
      90             : 
      91           2 :         h = (h % 53);
      92             : 
      93           2 :         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 */
      99          33 : PHP_FUNCTION(mail)
     100             : {
     101          33 :         char *to=NULL, *message=NULL, *headers=NULL, *headers_trimmed=NULL;
     102          33 :         char *subject=NULL, *extra_cmd=NULL;
     103          33 :         int to_len, message_len, headers_len = 0;
     104          33 :         int subject_len, extra_cmd_len = 0, i;
     105          33 :         char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
     106             :         char *to_r, *subject_r;
     107             :         char *p, *e;
     108             : 
     109          33 :         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           2 :                 return;
     111             :         }
     112             : 
     113             :         /* ASCIIZ check */
     114          31 :         MAIL_ASCIIZ_CHECK(to, to_len);
     115          31 :         MAIL_ASCIIZ_CHECK(subject, subject_len);
     116          31 :         MAIL_ASCIIZ_CHECK(message, message_len);
     117          31 :         if (headers) {
     118          23 :                 MAIL_ASCIIZ_CHECK(headers, headers_len);
     119          23 :                 headers_trimmed = php_trim(headers, headers_len, NULL, 0, NULL, 2 TSRMLS_CC);
     120             :         }
     121          31 :         if (extra_cmd) {
     122           1 :                 MAIL_ASCIIZ_CHECK(extra_cmd, extra_cmd_len);
     123             :         }
     124             : 
     125          31 :         if (to_len > 0) {
     126          31 :                 to_r = estrndup(to, to_len);
     127          31 :                 for (; to_len; to_len--) {
     128          31 :                         if (!isspace((unsigned char) to_r[to_len - 1])) {
     129          31 :                                 break;
     130             :                         }
     131           0 :                         to_r[to_len - 1] = '\0';
     132             :                 }
     133         527 :                 for (i = 0; to_r[i]; i++) {
     134         496 :                         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           0 :                                 SKIP_LONG_HEADER_SEP(to_r, i);
     140           0 :                                 to_r[i] = ' ';
     141             :                         }
     142             :                 }
     143             :         } else {
     144           0 :                 to_r = to;
     145             :         }
     146             : 
     147          31 :         if (subject_len > 0) {
     148          31 :                 subject_r = estrndup(subject, subject_len);
     149          31 :                 for (; subject_len; subject_len--) {
     150          31 :                         if (!isspace((unsigned char) subject_r[subject_len - 1])) {
     151          31 :                                 break;
     152             :                         }
     153           0 :                         subject_r[subject_len - 1] = '\0';
     154             :                 }
     155         403 :                 for (i = 0; subject_r[i]; i++) {
     156         372 :                         if (iscntrl((unsigned char) subject_r[i])) {
     157           0 :                                 SKIP_LONG_HEADER_SEP(subject_r, i);
     158           0 :                                 subject_r[i] = ' ';
     159             :                         }
     160             :                 }
     161             :         } else {
     162           0 :                 subject_r = subject;
     163             :         }
     164             : 
     165          31 :         if (force_extra_parameters) {
     166           1 :                 extra_cmd = php_escape_shell_cmd(force_extra_parameters);
     167          30 :         } else if (extra_cmd) {
     168           1 :                 extra_cmd = php_escape_shell_cmd(extra_cmd);
     169             :         }
     170             : 
     171          31 :         if (php_mail(to_r, subject_r, message, headers_trimmed, extra_cmd TSRMLS_CC)) {
     172          15 :                 RETVAL_TRUE;
     173             :         } else {
     174          16 :                 RETVAL_FALSE;
     175             :         }
     176             : 
     177          31 :         if (headers_trimmed) {
     178          23 :                 efree(headers_trimmed);
     179             :         }
     180             : 
     181          31 :         if (extra_cmd) {
     182           2 :                 efree (extra_cmd);
     183             :         }
     184          31 :         if (to_r != to) {
     185          31 :                 efree(to_r);
     186             :         }
     187          31 :         if (subject_r != subject) {
     188          31 :                 efree(subject_r);
     189             :         }
     190             : }
     191             : /* }}} */
     192             : 
     193             : 
     194           0 : 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           0 :         char *p = message;
     200           0 :         while ((p = strpbrk(p, "\r\n"))) {
     201           0 :                 *p = ' ';
     202             :         }
     203           0 : }
     204             : 
     205           0 : void php_mail_log_to_syslog(char *message) {
     206             :         /* Write 'message' to syslog. */
     207             : #ifdef HAVE_SYSLOG_H
     208           0 :         php_syslog(LOG_NOTICE, "%s", message);
     209             : #endif
     210           0 : }
     211             : 
     212             : 
     213           0 : void php_mail_log_to_file(char *filename, char *message, size_t message_size TSRMLS_DC) {
     214             :         /* Write 'message' to the given file. */
     215           0 :         uint flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
     216           0 :         php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL);
     217           0 :         if (stream) {
     218           0 :                 php_stream_write(stream, message, message_size);
     219           0 :                 php_stream_close(stream);
     220             :         }
     221           0 : }
     222             : 
     223             : 
     224          39 : static int php_mail_detect_multiple_crlf(char *hdr) {
     225             :         /* This function detects multiple/malformed multiple newlines. */
     226             :         size_t len;
     227             : 
     228          39 :         if (!hdr || !strlen(hdr)) {
     229           0 :                 return 0;
     230             :         }
     231             : 
     232             :         /* Should not have any newlines at the beginning. */
     233             :         /* RFC 2822 2.2. Header Fields */
     234          39 :         if (*hdr < 33 || *hdr > 126 || *hdr == ':') {
     235           6 :                 return 1;
     236             :         }
     237             : 
     238        1691 :         while(*hdr) {
     239        1631 :                 if (*hdr == '\r') {
     240           9 :                         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           3 :                                 return 1;
     243             :                         } else {
     244           6 :                                 hdr += 2;
     245             :                         }
     246        1622 :                 } else if (*hdr == '\n') {
     247          34 :                         if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') {
     248             :                                 /* Malformed or multiple newlines. */
     249           3 :                                 return 1;
     250             :                         } else {
     251          31 :                                 hdr += 2;
     252             :                         }
     253             :                 } else {
     254        1588 :                         hdr++;
     255             :                 }
     256             :         }
     257             : 
     258          27 :         return 0;
     259             : }
     260             : 
     261             : 
     262             : /* {{{ php_mail
     263             :  */
     264          46 : 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          46 :         char *sendmail_path = INI_STR("sendmail_path");
     273          46 :         char *sendmail_cmd = NULL;
     274          46 :         char *mail_log = INI_STR("mail.log");
     275          46 :         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          46 :         if (mail_log && *mail_log) {
     287             :                 char *tmp;
     288           0 :                 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           0 :                 if (hdr) {
     291           0 :                         php_mail_log_crlf_to_spaces(tmp);
     292             :                 }
     293             : 
     294           0 :                 if (!strcmp(mail_log, "syslog")) {
     295             :                         /* Drop the final space when logging to syslog. */
     296           0 :                         tmp[l - 1] = 0;
     297           0 :                         php_mail_log_to_syslog(tmp);
     298             :                 }
     299             :                 else {
     300             :                         /* Convert the final space to a newline when logging to file. */
     301           0 :                         tmp[l - 1] = '\n';
     302           0 :                         php_mail_log_to_file(mail_log, tmp, l TSRMLS_CC);
     303             :                 }
     304             : 
     305           0 :                 efree(tmp);
     306             :         }
     307             : 
     308          46 :         if (PG(mail_x_header)) {
     309           2 :                 const char *tmp = zend_get_executed_filename(TSRMLS_C);
     310             :                 char *f;
     311             :                 size_t f_len;
     312             : 
     313           2 :                 php_basename(tmp, strlen(tmp), NULL, 0,&f, &f_len TSRMLS_CC);
     314             : 
     315           2 :                 if (headers != NULL && *headers) {
     316           0 :                         spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n%s", php_getuid(TSRMLS_C), f, headers);
     317             :                 } else {
     318           2 :                         spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s", php_getuid(TSRMLS_C), f);
     319             :                 }
     320           2 :                 efree(f);
     321             :         }
     322             : 
     323          46 :         if (hdr && php_mail_detect_multiple_crlf(hdr)) {
     324          12 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Multiple or malformed newlines found in additional_header");
     325          12 :                 MAIL_RET(0);
     326             :         }
     327             : 
     328          34 :         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           0 :                 MAIL_RET(0);
     343             : #endif
     344             :         }
     345          34 :         if (extra_cmd != NULL) {
     346           2 :                 spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
     347             :         } else {
     348          32 :                 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          34 :         errno = 0;
     368          34 :         sendmail = popen(sendmail_cmd, "w");
     369             : #endif
     370          34 :         if (extra_cmd != NULL) {
     371           2 :                 efree (sendmail_cmd);
     372             :         }
     373             : 
     374          34 :         if (sendmail) {
     375             : #ifndef PHP_WIN32
     376          34 :                 if (EACCES == errno) {
     377           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
     378           0 :                         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           0 :                         MAIL_RET(0);
     387             :                 }
     388             : #endif
     389          34 :                 fprintf(sendmail, "To: %s\n", to);
     390          34 :                 fprintf(sendmail, "Subject: %s\n", subject);
     391          34 :                 if (hdr != NULL) {
     392          27 :                         fprintf(sendmail, "%s\n", hdr);
     393             :                 }
     394          34 :                 fprintf(sendmail, "\n%s\n", message);
     395          34 :                 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          34 :                 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           4 :                         MAIL_RET(0);
     416             :                 } else {
     417          30 :                         MAIL_RET(1);
     418             :                 }
     419             :         } else {
     420           0 :                 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           0 :                 MAIL_RET(0);
     427             :         }
     428             : 
     429             :         MAIL_RET(1); /* never reached */
     430             : }
     431             : /* }}} */
     432             : 
     433             : /* {{{ PHP_MINFO_FUNCTION
     434             :  */
     435         149 : PHP_MINFO_FUNCTION(mail)
     436             : {
     437         149 :         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         149 :         php_info_print_table_row(2, "Path to sendmail", sendmail_path);
     447             : #endif
     448         149 : }
     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             :  */

Generated by: LCOV version 1.10

Generated at Thu, 27 Aug 2015 06:06:20 +0000 (9 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.