Only in php-5.2.5.mail: build-log diff -rup php-5.2.5/ext/standard/mail.c php-5.2.5.mail/ext/standard/mail.c --- php-5.2.5/ext/standard/mail.c 2007-10-04 08:31:11.000000000 -0500 +++ php-5.2.5.mail/ext/standard/mail.c 2007-12-20 07:04:27.000000000 -0600 @@ -23,6 +23,8 @@ #include #include "php.h" #include "ext/standard/info.h" +#include "ext/standard/php_string.h" +#include "ext/standard/basic_functions.h" #if HAVE_SYSEXITS_H #include @@ -54,6 +56,8 @@ continue; \ } \ +extern long php_getuid(void); + #define MAIL_ASCIIZ_CHECK(str, len) \ p = str; \ e = p + len; \ @@ -200,22 +204,68 @@ PHPAPI int php_mail(char *to, char *subj int ret; char *sendmail_path = INI_STR("sendmail_path"); char *sendmail_cmd = NULL; + char *mail_log = INI_STR("mail.log"); +// zend_bool mail_x_header = INI_BOOL("mail.add_x_header"); + char *hdr = headers; + char *exec_filename = zend_get_executed_filename(TSRMLS_C); + char error_time_str[128]; + struct tm tmbuf; + time_t error_time; + + time(&error_time); + strftime(error_time_str, sizeof(error_time_str), "%d-%b-%Y %H:%M:%S", php_localtime_r(&error_time, &tmbuf)); + + +#define MAIL_RET(val) \ + if (hdr != headers) { \ + efree(hdr); \ + } \ + return val; \ + + if (mail_log) { + char *tmp; + int l = spprintf(&tmp, 0, "[%s] mail() on [%s:%d]: To: %s -- Headers: %s\n", error_time_str, exec_filename, zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : ""); + if (hdr) { /* find all \r\n instances and replace them with spaces, so a log line is always one line long */ + char *p = tmp; + while ((p = strpbrk(p, "\r\n"))) { + *p = ' '; + } + tmp[l - 1] = '\n'; + } + _php_error_log(3, tmp, mail_log, NULL TSRMLS_CC); + efree(tmp); + } + if (INI_BOOL("mail.add_x_header") == 1) { + size_t f_len = 0; + char *f = 0; + char *tmp = exec_filename; + php_basename(tmp, strlen(tmp), NULL, 0, &f, &f_len); + + if (headers != NULL) { + spprintf(&headers, 0, "%s\r\nX-PHP-Originating-Script: %ld:%s\r\n", headers, php_getuid(), f); + } else { + spprintf(&headers, 0, "X-PHP-Originating-Script: %ld:%s\r\n", php_getuid(), f); + } + efree(f); + efree(tmp); + } + efree(exec_filename); if (!sendmail_path) { #if (defined PHP_WIN32 || defined NETWARE) /* handle old style win smtp sending */ - if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, headers, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) { + if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) { if (tsm_errmsg) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg); efree(tsm_errmsg); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err)); } - return 0; + MAIL_RET(0); } - return 1; + MAIL_RET(1); #else - return 0; + MAIL_RET(0); #endif } if (extra_cmd != NULL) { @@ -241,12 +291,12 @@ PHPAPI int php_mail(char *to, char *subj if (EACCES == errno) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path); pclose(sendmail); - return 0; + MAIL_RET(0); } #endif fprintf(sendmail, "To: %s\n", to); fprintf(sendmail, "Subject: %s\n", subject); - if (headers != NULL) { + if (hdr != NULL) { fprintf(sendmail, "%s\n", headers); } fprintf(sendmail, "\n%s\n", message); @@ -263,16 +313,16 @@ PHPAPI int php_mail(char *to, char *subj #endif #endif { - return 0; + MAIL_RET(0); } else { - return 1; + MAIL_RET(1); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path); - return 0; + MAIL_RET(0); } - return 1; /* never reached */ + MAIL_RET(1); /* never reached */ } /* }}} */ Only in php-5.2.5.mail: mail.php diff -rup php-5.2.5/main/main.c php-5.2.5.mail/main/main.c --- php-5.2.5/main/main.c 2007-10-18 08:11:30.000000000 -0500 +++ php-5.2.5.mail/main/main.c 2007-12-19 09:40:10.000000000 -0600 @@ -441,6 +441,8 @@ PHP_INI_BEGIN() PHP_INI_ENTRY("SMTP", "localhost",PHP_INI_ALL, NULL) PHP_INI_ENTRY("smtp_port", "25", PHP_INI_ALL, NULL) + STD_PHP_INI_BOOLEAN("mail.add_x_header", "0", PHP_INI_SYSTEM, OnUpdateBool, mail_x_header, php_core_globals, core_globals) + STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM, OnUpdateString, mail_log, php_core_globals, core_globals) PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit) PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) diff -rup php-5.2.5/main/php_globals.h php-5.2.5.mail/main/php_globals.h --- php-5.2.5/main/php_globals.h 2007-07-24 09:21:36.000000000 -0500 +++ php-5.2.5.mail/main/php_globals.h 2007-12-19 09:40:10.000000000 -0600 @@ -161,6 +161,9 @@ struct _php_core_globals { #endif long max_input_nesting_level; zend_bool in_user_include; + + zend_bool mail_x_header; + char *mail_log; }; Only in php-5.2.5.mail: php_errorlog diff -rup php-5.2.5/php.ini-dist php-5.2.5.mail/php.ini-dist --- php-5.2.5/php.ini-dist 2007-08-21 18:24:18.000000000 -0500 +++ php-5.2.5.mail/php.ini-dist 2007-12-19 09:40:10.000000000 -0600 @@ -710,6 +710,12 @@ smtp_port = 25 ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters = +; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename +mail.add_x_header = On + +; Log all mail() calls including the full path of the script, line #, to address and headers +mail.log = + [SQL] sql.safe_mode = Off diff -rup php-5.2.5/php.ini-recommended php-5.2.5.mail/php.ini-recommended --- php-5.2.5/php.ini-recommended 2007-08-21 18:24:18.000000000 -0500 +++ php-5.2.5.mail/php.ini-recommended 2007-12-19 09:40:10.000000000 -0600 @@ -760,6 +760,12 @@ smtp_port = 25 ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters = +; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename +mail.add_x_header = On + +; Log all mail() calls including the full path of the script, line #, to address and headers +mail.log = + [SQL] sql.safe_mode = Off