xref: /PHP-7.1/win32/sendmail.c (revision 6c631ccf)
1 /*
2  *    PHP Sendmail for Windows.
3  *
4  *  This file is rewriten specificly for PHPFI.  Some functionality
5  *  has been removed (MIME and file attachments).  This code was
6  *  modified from code based on code written by Jarle Aase.
7  *
8  *  This class is based on the original code by Jarle Aase, see bellow:
9  *  wSendmail.cpp  It has been striped of some functionality to match
10  *  the requirements of phpfi.
11  *
12  *  Very simple SMTP Send-mail program for sending command-line level
13  *  emails and CGI-BIN form response for the Windows platform.
14  *
15  *  The complete wSendmail package with source code can be located
16  *  from http://www.jgaa.com
17  *
18  */
19 
20 /* $Id$ */
21 
22 #include "php.h"				/*php specific */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifndef NETWARE
26 #include <winsock2.h>
27 #include "time.h"
28 # include <Ws2tcpip.h>
29 #else	/* NETWARE */
30 #include <netware/sendmail_nw.h>
31 #endif	/* NETWARE */
32 #include <string.h>
33 #include <math.h>
34 #ifndef NETWARE
35 #include <malloc.h>
36 #include <memory.h>
37 #include <winbase.h>
38 #endif	/* NETWARE */
39 #include "sendmail.h"
40 #include "php_ini.h"
41 #include "inet.h"
42 
43 #include "php_win32_globals.h"
44 
45 #if HAVE_PCRE || HAVE_BUNDLED_PCRE
46 #include "ext/pcre/php_pcre.h"
47 #endif
48 
49 #include "ext/standard/php_string.h"
50 #include "ext/date/php_date.h"
51 
52 #define SENDMAIL_DEBUG 0
53 
54 /*enum
55    {
56    DO_CONNECT = WM_USER +1
57    };
58  */
59 
60 /* '*error_message' has to be passed around from php_mail() */
61 #define SMTP_ERROR_RESPONSE_SPEC	"SMTP server response: %s"
62 /* Convinient way to handle error messages from the SMTP server.
63    response is ecalloc()d in Ack() itself and efree()d here
64    because the content is in *error_message now */
65 #define SMTP_ERROR_RESPONSE(response)	{ \
66 											if (response && error_message) { \
67 												if (NULL != (*error_message = ecalloc(1, sizeof(SMTP_ERROR_RESPONSE_SPEC) + strlen(response)))) { \
68 													snprintf(*error_message, sizeof(SMTP_ERROR_RESPONSE_SPEC) + strlen(response), SMTP_ERROR_RESPONSE_SPEC, response); \
69 												} \
70 												efree(response); \
71 											} \
72 										}
73 #define SMTP_SKIP_SPACE(str)	{ while (isspace(*str)) { str++; } }
74 
75 
76 char seps[] = " ,\t\n";
77 #ifndef NETWARE
78 char *php_mailer = "PHP 7 WIN32";
79 #else
80 char *php_mailer = "PHP 7 NetWare";
81 #endif	/* NETWARE */
82 
83 /* Error messages */
84 static char *ErrorMessages[] =
85 {
86 	{"Success"}, /* 0 */
87 	{"Bad arguments from form"}, /* 1 */
88 	{"Unable to open temporary mailfile for read"},
89 	{"Failed to Start Sockets"},
90 	{"Failed to Resolve Host"},
91 	{"Failed to obtain socket handle"}, /* 5 */
92 	{"Failed to connect to mailserver, verify your \"SMTP\" setting in php.ini"},
93 	{"Failed to Send"},
94 	{"Failed to Receive"},
95 	{"Server Error"},
96 	{"Failed to resolve the host IP name"}, /* 10 */
97 	{"Out of memory"},
98 	{"Unknown error"},
99 	{"Bad Message Contents"},
100 	{"Bad Message Subject"},
101 	{"Bad Message destination"}, /* 15 */
102 	{"Bad Message Return Path"},
103 	{"Bad Mail Host"},
104 	{"Bad Message File"},
105 	{"\"sendmail_from\" not set in php.ini or custom \"From:\" header missing"},
106 	{"Mailserver rejected our \"sendmail_from\" setting"}, /* 20 */
107 	{"Error while trimming mail header with PCRE, please file a bug report at http://bugs.php.net/"} /* 21 */
108 };
109 
110 /* This pattern converts all single occurrences of \n (Unix)
111  * withour a leading \r to \r\n and all occurrences of \r (Mac)
112  * without a trailing \n to \r\n
113  * Thx to Nibbler from ircnet/#linuxger
114  */
115 #define PHP_WIN32_MAIL_UNIFY_PATTERN	"/(\r\n?)|\n/"
116 #define PHP_WIN32_MAIL_UNIFY_REPLACE	"\r\n"
117 
118 /* This pattern removes \r\n from the start of the string,
119  * \r\n from the end of the string and also makes sure every line
120  * is only wrapped with a single \r\n (thus reduces multiple
121  * occurrences of \r\n between lines to a single \r\n) */
122 #define PHP_WIN32_MAIL_RMVDBL_PATTERN	"/^\r\n|(\r\n)+$/m"
123 #define PHP_WIN32_MAIL_RMVDBL_REPLACE	""
124 
125 /* This pattern escapes \n. inside the message body. It prevents
126  * premature end of message if \n.\n or \r\n.\r\n is encountered
127  * and ensures that \n. sequences are properly displayed in the
128  * message body. */
129 #define PHP_WIN32_MAIL_DOT_PATTERN	"\n."
130 #define PHP_WIN32_MAIL_DOT_REPLACE	"\n.."
131 
132 /* This function is meant to unify the headers passed to to mail()
133  * This means, use PCRE to transform single occurrences of \n or \r in \r\n
134  * As a second step we also eleminate all \r\n occurrences which are:
135  * 1) At the start of the header
136  * 2) At the end of the header
137  * 3) Two or more occurrences in the header are removed so only one is left
138  *
139  * Returns NULL on error, or the new char* buffer on success.
140  * You have to take care and efree() the buffer on your own.
141  */
php_win32_mail_trim_header(char * header)142 static zend_string *php_win32_mail_trim_header(char *header)
143 {
144 
145 #if HAVE_PCRE || HAVE_BUNDLED_PCRE
146 
147 	zend_string *result, *result2;
148 	zval replace;
149 	zend_string *regex;
150 
151 	if (!header) {
152 		return NULL;
153 	}
154 
155 	ZVAL_STRINGL(&replace, PHP_WIN32_MAIL_UNIFY_REPLACE, strlen(PHP_WIN32_MAIL_UNIFY_REPLACE));
156 	regex = zend_string_init(PHP_WIN32_MAIL_UNIFY_PATTERN, sizeof(PHP_WIN32_MAIL_UNIFY_PATTERN)-1, 0);
157 
158 	result = php_pcre_replace(regex,
159 				  NULL, header, (int)strlen(header),
160 				  &replace,
161 				  0,
162 				  -1,
163 				  NULL);
164 
165 	zval_ptr_dtor(&replace);
166 	zend_string_release(regex);
167 
168 	if (NULL == result) {
169 		return NULL;
170 	}
171 
172 	ZVAL_STRING(&replace, PHP_WIN32_MAIL_RMVDBL_PATTERN);
173 	regex = zend_string_init(PHP_WIN32_MAIL_RMVDBL_PATTERN, sizeof(PHP_WIN32_MAIL_RMVDBL_PATTERN)-1, 0);
174 
175 	result2 = php_pcre_replace(regex,
176 				   result, ZSTR_VAL(result), (int)ZSTR_LEN(result),
177 				   &replace,
178 				  0,
179 				  -1,
180 				  NULL);
181 	zval_ptr_dtor(&replace);
182 	zend_string_release(regex);
183 	zend_string_release(result);
184 
185 	return result2;
186 #else
187 	/* In case we don't have PCRE support (for whatever reason...) simply do nothing and return the unmodified header */
188 	return estrdup(header);
189 #endif
190 }
191 
192 /*********************************************************************
193 // Name:  TSendMail
194 // Input:   1) host:    Name of the mail host where the SMTP server resides
195 //                      max accepted length of name = 256
196 //          2) appname: Name of the application to use in the X-mailer
197 //                      field of the message. if NULL is given the application
198 //                      name is used as given by the GetCommandLine() function
199 //                      max accespted length of name = 100
200 // Output:  1) error:   Returns the error code if something went wrong or
201 //                      SUCCESS otherwise.
202 //
203 //  See SendText() for additional args!
204 //********************************************************************/
TSendMail(char * host,int * error,char ** error_message,char * headers,char * Subject,char * mailTo,char * data,char * mailCc,char * mailBcc,char * mailRPath)205 PHPAPI int TSendMail(char *host, int *error, char **error_message,
206 			  char *headers, char *Subject, char *mailTo, char *data,
207 			  char *mailCc, char *mailBcc, char *mailRPath)
208 {
209 	int ret;
210 	char *RPath = NULL;
211 	zend_string *headers_lc = NULL, *headers_trim = NULL; /* headers_lc is only created if we've a header at all */
212 	char *pos1 = NULL, *pos2 = NULL;
213 
214 	if (host == NULL) {
215 		*error = BAD_MAIL_HOST;
216 		return FAILURE;
217 	} else if (strlen(host) >= HOST_NAME_LEN) {
218 		*error = BAD_MAIL_HOST;
219 		return FAILURE;
220 	} else {
221 		strcpy(PW32G(mail_host), host);
222 	}
223 
224 	if (headers) {
225 		char *pos = NULL;
226 
227 		/* Use PCRE to trim the header into the right format */
228 		if (NULL == (headers_trim = php_win32_mail_trim_header(headers))) {
229 			*error = W32_SM_PCRE_ERROR;
230 			return FAILURE;
231 		}
232 
233 		/* Create a lowercased header for all the searches so we're finally case
234 		 * insensitive when searching for a pattern. */
235 		headers_lc = zend_string_tolower(headers_trim);
236 	}
237 
238 	/* Fall back to sendmail_from php.ini setting */
239 	if (mailRPath && *mailRPath) {
240 		RPath = estrdup(mailRPath);
241 	} else if (INI_STR("sendmail_from")) {
242 		RPath = estrdup(INI_STR("sendmail_from"));
243 	} else if (headers_lc) {
244 		int found = 0;
245 		char *lookup = ZSTR_VAL(headers_lc);
246 
247 		while (lookup) {
248 			pos1 = strstr(lookup, "from:");
249 
250 			if (!pos1) {
251 				break;
252 			} else if (pos1 != ZSTR_VAL(headers_lc) && *(pos1-1) != '\n') {
253 				if (strlen(pos1) >= sizeof("from:")) {
254 					lookup = pos1 + sizeof("from:");
255 					continue;
256 				} else {
257 					break;
258 				}
259 			}
260 
261 			found = 1;
262 
263 			/* Real offset is memaddress from the original headers + difference of
264 			 * string found in the lowercase headrs + 5 characters to jump over
265 			 * the from: */
266 			pos1 = headers + (pos1 - lookup) + 5;
267 			if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
268 				RPath = estrndup(pos1, strlen(pos1));
269 			} else {
270 				RPath = estrndup(pos1, pos2 - pos1);
271 			}
272 
273 			break;
274 		}
275 
276 		if (!found) {
277 			if (headers) {
278 				zend_string_release(headers_trim);
279 				zend_string_release(headers_lc);
280 			}
281 			*error = W32_SM_SENDMAIL_FROM_NOT_SET;
282 			return FAILURE;
283 		}
284 	}
285 
286 	/* attempt to connect with mail host */
287 	*error = MailConnect();
288 	if (*error != 0) {
289 		if (RPath) {
290 			efree(RPath);
291 		}
292 		if (headers) {
293 			zend_string_release(headers_trim);
294 			zend_string_release(headers_lc);
295 		}
296 		/* 128 is safe here, the specifier in snprintf isn't longer than that */
297 		if (NULL == (*error_message = ecalloc(1, HOST_NAME_LEN + 128))) {
298 			return FAILURE;
299 		}
300 		snprintf(*error_message, HOST_NAME_LEN + 128,
301 			"Failed to connect to mailserver at \"%s\" port %d, verify your \"SMTP\" "
302 			"and \"smtp_port\" setting in php.ini or use ini_set()",
303 			PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
304 		return FAILURE;
305 	} else {
306 		ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers ? ZSTR_VAL(headers_trim) : NULL, headers ? ZSTR_VAL(headers_lc) : NULL, error_message);
307 		TSMClose();
308 		if (RPath) {
309 			efree(RPath);
310 		}
311 		if (headers) {
312 			zend_string_release(headers_trim);
313 			zend_string_release(headers_lc);
314 		}
315 		if (ret != SUCCESS) {
316 			*error = ret;
317 			return FAILURE;
318 		}
319 		return SUCCESS;
320 	}
321 }
322 
323 //********************************************************************
324 // Name:  TSendMail::~TSendMail
325 // Input:
326 // Output:
327 // Description: DESTRUCTOR
328 // Author/Date:  jcar 20/9/96
329 // History:
330 //********************************************************************/
TSMClose()331 PHPAPI void TSMClose()
332 {
333 	Post("QUIT\r\n");
334 	Ack(NULL);
335 	/* to guarantee that the cleanup is not made twice and
336 	   compomise the rest of the application if sockets are used
337 	   elesewhere
338 	*/
339 
340 	shutdown(PW32G(mail_socket), 0);
341 	closesocket(PW32G(mail_socket));
342 }
343 
344 
345 /*********************************************************************
346 // Name:  char *GetSMErrorText
347 // Input:   Error index returned by the menber functions
348 // Output:  pointer to a string containing the error description
349 // Description:
350 // Author/Date:  jcar 20/9/96
351 // History:
352 //*******************************************************************/
GetSMErrorText(int index)353 PHPAPI char *GetSMErrorText(int index)
354 {
355 	if (MIN_ERROR_INDEX <= index && index < MAX_ERROR_INDEX) {
356 		return (ErrorMessages[index]);
357 
358 	} else {
359 		return (ErrorMessages[UNKNOWN_ERROR]);
360 
361 	}
362 }
363 
364 PHPAPI zend_string *php_str_to_str(char *haystack, size_t length, char *needle,
365 		size_t needle_len, char *str, size_t str_len);
366 
367 
368 /*********************************************************************
369 // Name:  SendText
370 // Input:       1) RPath:   return path of the message
371 //                                  Is used to fill the "Return-Path" and the
372 //                                  "X-Sender" fields of the message.
373 //                  2) Subject: Subject field of the message. If NULL is given
374 //                                  the subject is set to "No Subject"
375 //                  3) mailTo:  Destination address
376 //                  4) data:        Null terminated string containing the data to be send.
377 //                  5,6) headers of the message. Note that the second
378 //                  parameter, headers_lc, is actually a lowercased version of
379 //                  headers. The should match exactly (in terms of length),
380 //                  only differ in case
381 // Output:      Error code or SUCCESS
382 // Description:
383 // Author/Date:  jcar 20/9/96
384 // History:
385 //*******************************************************************/
SendText(char * RPath,char * Subject,char * mailTo,char * mailCc,char * mailBcc,char * data,char * headers,char * headers_lc,char ** error_message)386 static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char *mailBcc, char *data,
387 			 char *headers, char *headers_lc, char **error_message)
388 {
389 	int res;
390 	char *p;
391 	char *tempMailTo, *token, *pos1, *pos2;
392 	char *server_response = NULL;
393 	char *stripped_header  = NULL;
394 	zend_string *data_cln;
395 
396 	/* check for NULL parameters */
397 	if (data == NULL)
398 		return (BAD_MSG_CONTENTS);
399 	if (mailTo == NULL)
400 		return (BAD_MSG_DESTINATION);
401 	if (RPath == NULL)
402 		return (BAD_MSG_RPATH);
403 
404 	/* simple checks for the mailto address */
405 	/* have ampersand ? */
406 	/* mfischer, 20020514: I commented this out because it really
407 	   seems bogus. Only a username for example may still be a
408 	   valid address at the destination system.
409 	if (strchr(mailTo, '@') == NULL)
410 		return (BAD_MSG_DESTINATION);
411 	*/
412 
413 	snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host));
414 
415 	/* in the beginning of the dialog */
416 	/* attempt reconnect if the first Post fail */
417 	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
418 		int err = MailConnect();
419 		if (0 != err) {
420 			return (FAILED_TO_SEND);
421 		}
422 
423 		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
424 			return (res);
425 		}
426 	}
427 	if ((res = Ack(&server_response)) != SUCCESS) {
428 		SMTP_ERROR_RESPONSE(server_response);
429 		return (res);
430 	}
431 
432 	SMTP_SKIP_SPACE(RPath);
433 	FormatEmailAddress(PW32G(mail_buffer), RPath, "MAIL FROM:<%s>\r\n");
434 	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
435 		return (res);
436 	}
437 	if ((res = Ack(&server_response)) != SUCCESS) {
438 		SMTP_ERROR_RESPONSE(server_response);
439 		return W32_SM_SENDMAIL_FROM_MALFORMED;
440 	}
441 
442 	tempMailTo = estrdup(mailTo);
443 	/* Send mail to all rcpt's */
444 	token = strtok(tempMailTo, ",");
445 	while (token != NULL)
446 	{
447 		SMTP_SKIP_SPACE(token);
448 		FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
449 		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
450 			efree(tempMailTo);
451 			return (res);
452 		}
453 		if ((res = Ack(&server_response)) != SUCCESS) {
454 			SMTP_ERROR_RESPONSE(server_response);
455 			efree(tempMailTo);
456 			return (res);
457 		}
458 		token = strtok(NULL, ",");
459 	}
460 	efree(tempMailTo);
461 
462 	if (mailCc && *mailCc) {
463 		tempMailTo = estrdup(mailCc);
464 		/* Send mail to all rcpt's */
465 		token = strtok(tempMailTo, ",");
466 		while (token != NULL)
467 		{
468 			SMTP_SKIP_SPACE(token);
469 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
470 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
471 				efree(tempMailTo);
472 				return (res);
473 			}
474 			if ((res = Ack(&server_response)) != SUCCESS) {
475 				SMTP_ERROR_RESPONSE(server_response);
476 				efree(tempMailTo);
477 				return (res);
478 			}
479 			token = strtok(NULL, ",");
480 		}
481 		efree(tempMailTo);
482 	}
483 	/* Send mail to all Cc rcpt's */
484 	else if (headers && (pos1 = strstr(headers_lc, "cc:")) && ((pos1 == headers_lc) || (*(pos1-1) == '\n'))) {
485 		/* Real offset is memaddress from the original headers + difference of
486 		 * string found in the lowercase headrs + 3 characters to jump over
487 		 * the cc: */
488 		pos1 = headers + (pos1 - headers_lc) + 3;
489 		if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
490 			tempMailTo = estrndup(pos1, strlen(pos1));
491 		} else {
492 			tempMailTo = estrndup(pos1, pos2 - pos1);
493 		}
494 
495 		token = strtok(tempMailTo, ",");
496 		while (token != NULL)
497 		{
498 			SMTP_SKIP_SPACE(token);
499 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
500 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
501 				efree(tempMailTo);
502 				return (res);
503 			}
504 			if ((res = Ack(&server_response)) != SUCCESS) {
505 				SMTP_ERROR_RESPONSE(server_response);
506 				efree(tempMailTo);
507 				return (res);
508 			}
509 			token = strtok(NULL, ",");
510 		}
511 		efree(tempMailTo);
512 	}
513 
514 	/* Send mail to all Bcc rcpt's
515 	   This is basically a rip of the Cc code above.
516 	   Just don't forget to remove the Bcc: from the header afterwards. */
517 	if (mailBcc && *mailBcc) {
518 		tempMailTo = estrdup(mailBcc);
519 		/* Send mail to all rcpt's */
520 		token = strtok(tempMailTo, ",");
521 		while (token != NULL)
522 		{
523 			SMTP_SKIP_SPACE(token);
524 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
525 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
526 				efree(tempMailTo);
527 				return (res);
528 			}
529 			if ((res = Ack(&server_response)) != SUCCESS) {
530 				SMTP_ERROR_RESPONSE(server_response);
531 				efree(tempMailTo);
532 				return (res);
533 			}
534 			token = strtok(NULL, ",");
535 		}
536 		efree(tempMailTo);
537 	}
538 	else if (headers) {
539 		if ((pos1 = strstr(headers_lc, "bcc:")) && (pos1 == headers_lc || *(pos1-1) == '\n')) {
540 			/* Real offset is memaddress from the original headers + difference of
541 			 * string found in the lowercase headrs + 4 characters to jump over
542 			 * the bcc: */
543 			pos1 = headers + (pos1 - headers_lc) + 4;
544 			if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
545 				tempMailTo = estrndup(pos1, strlen(pos1));
546 				/* Later, when we remove the Bcc: out of the
547 				   header we know it was the last thing. */
548 				pos2 = pos1;
549 			} else {
550 				tempMailTo = estrndup(pos1, pos2 - pos1);
551 			}
552 
553 			token = strtok(tempMailTo, ",");
554 			while (token != NULL)
555 			{
556 				SMTP_SKIP_SPACE(token);
557 				FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
558 				if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
559 					efree(tempMailTo);
560 					return (res);
561 				}
562 				if ((res = Ack(&server_response)) != SUCCESS) {
563 					SMTP_ERROR_RESPONSE(server_response);
564 					efree(tempMailTo);
565 					return (res);
566 				}
567 				token = strtok(NULL, ",");
568 			}
569 			efree(tempMailTo);
570 
571 			/* Now that we've identified that we've a Bcc list,
572 			   remove it from the current header. */
573 			if (NULL == (stripped_header = ecalloc(1, strlen(headers)))) {
574 				return OUT_OF_MEMORY;
575 			}
576 			/* headers = point to string start of header
577 			   pos1    = pointer IN headers where the Bcc starts
578 			   '4'     = Length of the characters 'bcc:'
579 			   Because we've added +4 above for parsing the Emails
580 			   we've to subtract them here. */
581 			memcpy(stripped_header, headers, pos1 - headers - 4);
582 			if (pos1 != pos2) {
583 				/* if pos1 != pos2 , pos2 points to the rest of the headers.
584 				   Since pos1 != pos2 if "\r\n" was found, we know those characters
585 				   are there and so we jump over them (else we would generate a new header
586 				   which would look like "\r\n\r\n". */
587 				memcpy(stripped_header + (pos1 - headers - 4), pos2 + 2, strlen(pos2) - 2);
588 			}
589 		}
590 	}
591 
592 	/* Simplify the code that we create a copy of stripped_header no matter if
593 	   we actually strip something or not. So we've a single efree() later. */
594 	if (headers && !stripped_header) {
595 		if (NULL == (stripped_header = estrndup(headers, strlen(headers)))) {
596 			return OUT_OF_MEMORY;
597 		}
598 	}
599 
600 	if ((res = Post("DATA\r\n")) != SUCCESS) {
601 		if (stripped_header) {
602 			efree(stripped_header);
603 		}
604 		return (res);
605 	}
606 	if ((res = Ack(&server_response)) != SUCCESS) {
607 		SMTP_ERROR_RESPONSE(server_response);
608 		if (stripped_header) {
609 			efree(stripped_header);
610 		}
611 		return (res);
612 	}
613 
614 	/* send message header */
615 	if (Subject == NULL) {
616 		res = PostHeader(RPath, "No Subject", mailTo, stripped_header);
617 	} else {
618 		res = PostHeader(RPath, Subject, mailTo, stripped_header);
619 	}
620 	if (stripped_header) {
621 		efree(stripped_header);
622 	}
623 	if (res != SUCCESS) {
624 		return (res);
625 	}
626 
627 	/* Escape \n. sequences
628 	 * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter
629 	 * uses ZVAL as it's parameters */
630 	data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1,
631 					PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1);
632 	if (!data_cln) {
633 		data_cln = ZSTR_EMPTY_ALLOC();
634 	}
635 
636 	/* send message contents in 1024 chunks */
637 	{
638 		char c, *e2, *e = ZSTR_VAL(data_cln) + ZSTR_LEN(data_cln);
639 		p = ZSTR_VAL(data_cln);
640 
641 		while (e - p > 1024) {
642 			e2 = p + 1024;
643 			c = *e2;
644 			*e2 = '\0';
645 			if ((res = Post(p)) != SUCCESS) {
646 				zend_string_free(data_cln);
647 				return(res);
648 			}
649 			*e2 = c;
650 			p = e2;
651 		}
652 		if ((res = Post(p)) != SUCCESS) {
653 			zend_string_free(data_cln);
654 			return(res);
655 		}
656 	}
657 
658 	zend_string_free(data_cln);
659 
660 	/*send termination dot */
661 	if ((res = Post("\r\n.\r\n")) != SUCCESS)
662 		return (res);
663 	if ((res = Ack(&server_response)) != SUCCESS) {
664 		SMTP_ERROR_RESPONSE(server_response);
665 		return (res);
666 	}
667 
668 	return (SUCCESS);
669 }
670 
addToHeader(char ** header_buffer,const char * specifier,char * string)671 static int addToHeader(char **header_buffer, const char *specifier, char *string)
672 {
673 	if (NULL == (*header_buffer = erealloc(*header_buffer, strlen(*header_buffer) + strlen(specifier) + strlen(string) + 1))) {
674 		return 0;
675 	}
676 	sprintf(*header_buffer + strlen(*header_buffer), specifier, string);
677 	return 1;
678 }
679 
680 /*********************************************************************
681 // Name:  PostHeader
682 // Input:       1) return path
683 //              2) Subject
684 //              3) destination address
685 //              4) headers
686 // Output:      Error code or Success
687 // Description:
688 // Author/Date:  jcar 20/9/96
689 // History:
690 //********************************************************************/
PostHeader(char * RPath,char * Subject,char * mailTo,char * xheaders)691 static int PostHeader(char *RPath, char *Subject, char *mailTo, char *xheaders)
692 {
693 	/* Print message header according to RFC 822 */
694 	/* Return-path, Received, Date, From, Subject, Sender, To, cc */
695 
696 	int res;
697 	char *header_buffer;
698 	char *headers_lc = NULL;
699 	size_t i;
700 
701 	if (xheaders) {
702 		size_t headers_lc_len;
703 
704 		if (NULL == (headers_lc = estrdup(xheaders))) {
705 			return OUT_OF_MEMORY;
706 		}
707 
708 		headers_lc_len = strlen(headers_lc);
709 
710 		for (i = 0; i < headers_lc_len; i++) {
711 			headers_lc[i] = tolower(headers_lc[i]);
712 		}
713 	}
714 
715 	header_buffer = ecalloc(1, MAIL_BUFFER_SIZE);
716 
717 	if (!xheaders || !strstr(headers_lc, "date:")) {
718 		time_t tNow = time(NULL);
719 		zend_string *dt = php_format_date("r", 1, tNow, 1);
720 
721 		snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", ZSTR_VAL(dt));
722 		zend_string_free(dt);
723 	}
724 
725 	if (!headers_lc || !strstr(headers_lc, "from:")) {
726 		if (!addToHeader(&header_buffer, "From: %s\r\n", RPath)) {
727 			goto PostHeader_outofmem;
728 		}
729 	}
730 	if (!addToHeader(&header_buffer, "Subject: %s\r\n", Subject)) {
731 		goto PostHeader_outofmem;
732 	}
733 
734 	/* Only add the To: field from the $to parameter if isn't in the custom headers */
735 	if ((headers_lc && (!strstr(headers_lc, "\r\nto:") && (strncmp(headers_lc, "to:", 3) != 0))) || !headers_lc) {
736 		if (!addToHeader(&header_buffer, "To: %s\r\n", mailTo)) {
737 			goto PostHeader_outofmem;
738 		}
739 	}
740 	if (xheaders) {
741 		if (!addToHeader(&header_buffer, "%s\r\n", xheaders)) {
742 			goto PostHeader_outofmem;
743 		}
744 	}
745 
746 	if (headers_lc) {
747 		efree(headers_lc);
748 	}
749 	if ((res = Post(header_buffer)) != SUCCESS) {
750 		efree(header_buffer);
751 		return (res);
752 	}
753 	efree(header_buffer);
754 
755 	if ((res = Post("\r\n")) != SUCCESS) {
756 		return (res);
757 	}
758 
759 	return (SUCCESS);
760 
761 PostHeader_outofmem:
762 	if (headers_lc) {
763 		efree(headers_lc);
764 	}
765 	return OUT_OF_MEMORY;
766 }
767 
768 
769 
770 /*********************************************************************
771 // Name:  MailConnect
772 // Input:   None
773 // Output:  None
774 // Description: Connect to the mail host and receive the welcome message.
775 // Author/Date:  jcar 20/9/96
776 // History:
777 //********************************************************************/
MailConnect()778 static int MailConnect()
779 {
780 
781 	int res, namelen;
782 	short portnum;
783 	struct hostent *ent;
784 	IN_ADDR addr;
785 #ifdef HAVE_IPV6
786 	IN6_ADDR addr6;
787 #endif
788 	SOCKADDR_IN sock_in;
789 
790 #if SENDMAIL_DEBUG
791 return 0;
792 #endif
793 
794 	/* Create Socket */
795 	if ((PW32G(mail_socket) = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
796 		return (FAILED_TO_OBTAIN_SOCKET_HANDLE);
797 	}
798 
799 	/* Get our own host name */
800 	if (gethostname(PW32G(mail_local_host), HOST_NAME_LEN)) {
801 		closesocket(PW32G(mail_socket));
802 		return (FAILED_TO_GET_HOSTNAME);
803 	}
804 
805 	ent = gethostbyname(PW32G(mail_local_host));
806 
807 	if (!ent) {
808 		closesocket(PW32G(mail_socket));
809 		return (FAILED_TO_GET_HOSTNAME);
810 	}
811 
812 	namelen = (int)strlen(ent->h_name);
813 
814 #ifdef HAVE_IPV6
815 	if (inet_pton(AF_INET, ent->h_name, &addr) == 1 || inet_pton(AF_INET6, ent->h_name, &addr6) == 1)
816 #else
817 	if (inet_pton(AF_INET, ent->h_name, &addr) == 1)
818 #endif
819 	{
820 		if (namelen + 2 >= HOST_NAME_LEN) {
821 			closesocket(PW32G(mail_socket));
822 			return (FAILED_TO_GET_HOSTNAME);
823 		}
824 
825 		strcpy(PW32G(mail_local_host), "[");
826 		strcpy(PW32G(mail_local_host) + 1, ent->h_name);
827 		strcpy(PW32G(mail_local_host) + namelen + 1, "]");
828 	} else {
829 		if (namelen >= HOST_NAME_LEN) {
830 			closesocket(PW32G(mail_socket));
831 			return (FAILED_TO_GET_HOSTNAME);
832 		}
833 
834 		strcpy(PW32G(mail_local_host), ent->h_name);
835 	}
836 
837 	/* Resolve the servers IP */
838 	/*
839 	if (!isdigit(PW32G(mail_host)[0])||!gethostbyname(PW32G(mail_host)))
840 	{
841 		return (FAILED_TO_RESOLVE_HOST);
842 	}
843 	*/
844 
845 	portnum = (short) INI_INT("smtp_port");
846 	if (!portnum) {
847 		portnum = 25;
848 	}
849 
850 	/* Connect to server */
851 	sock_in.sin_family = AF_INET;
852 	sock_in.sin_port = htons(portnum);
853 	sock_in.sin_addr.S_un.S_addr = GetAddr(PW32G(mail_host));
854 
855 	if (connect(PW32G(mail_socket), (LPSOCKADDR) & sock_in, sizeof(sock_in))) {
856 		closesocket(PW32G(mail_socket));
857 		return (FAILED_TO_CONNECT);
858 	}
859 
860 	/* receive Server welcome message */
861 	res = Ack(NULL);
862 	return (res);
863 }
864 
865 
866 /*********************************************************************
867 // Name:  Post
868 // Input:
869 // Output:
870 // Description:
871 // Author/Date:  jcar 20/9/96
872 // History:
873 //********************************************************************/
Post(LPCSTR msg)874 static int Post(LPCSTR msg)
875 {
876 	int len = (int)strlen(msg);
877 	int slen;
878 	int index = 0;
879 
880 #if SENDMAIL_DEBUG
881 	if (msg)
882 		printf("POST: '%s'\n", msg);
883 	return (SUCCESS);
884 #endif
885 
886 	while (len > 0) {
887 		if ((slen = send(PW32G(mail_socket), msg + index, len, 0)) < 1)
888 			return (FAILED_TO_SEND);
889 		len -= slen;
890 		index += slen;
891 	}
892 	return (SUCCESS);
893 }
894 
895 
896 
897 /*********************************************************************
898 // Name:  Ack
899 // Input:
900 // Output:
901 // Description:
902 // Get the response from the server. We only want to know if the
903 // last command was successful.
904 // Author/Date:  jcar 20/9/96
905 // History:
906 //********************************************************************/
Ack(char ** server_response)907 static int Ack(char **server_response)
908 {
909 	ZEND_TLS char buf[MAIL_BUFFER_SIZE];
910 	int rlen;
911 	int Index = 0;
912 	int Received = 0;
913 
914 #if SENDMAIL_DEBUG
915 	return (SUCCESS);
916 #endif
917 
918 again:
919 
920 	if ((rlen = recv(PW32G(mail_socket), buf + Index, ((MAIL_BUFFER_SIZE) - 1) - Received, 0)) < 1) {
921 		return (FAILED_TO_RECEIVE);
922 	}
923 	Received += rlen;
924 	buf[Received] = 0;
925 	/*err_msg   fprintf(stderr,"Received: (%d bytes) %s", rlen, buf + Index); */
926 
927 	/* Check for newline */
928 	Index += rlen;
929 
930 	/* SMPT RFC says \r\n is the only valid line ending, who are we to argue ;)
931 	 * The response code must contain at least 5 characters ex. 220\r\n */
932 	if (Received < 5 || buf[Received - 1] != '\n' || buf[Received - 2] != '\r') {
933 		goto again;
934 	}
935 
936 	if (buf[0] > '3') {
937 		/* If we've a valid pointer, return the SMTP server response so the error message contains more information */
938 		if (server_response) {
939 			int dec = 0;
940 			/* See if we have something like \r, \n, \r\n or \n\r at the end of the message and chop it off */
941 			if (Received > 2) {
942 				if (buf[Received-1] == '\n' || buf[Received-1] == '\r') {
943 					dec++;
944 					if (buf[Received-2] == '\r' || buf[Received-2] == '\n') {
945 						dec++;
946 					}
947 				}
948 
949 			}
950 			*server_response = estrndup(buf, Received - dec);
951 		}
952 		return (SMTP_SERVER_ERROR);
953 	}
954 
955 	return (SUCCESS);
956 }
957 
958 
959 /*********************************************************************
960 // Name:  unsigned long GetAddr (LPSTR szHost)
961 // Input:
962 // Output:
963 // Description: Given a string, it will return an IP address.
964 //   - first it tries to convert the string directly
965 //   - if that fails, it tries o resolve it as a hostname
966 //
967 // WARNING: gethostbyname() is a blocking function
968 // Author/Date:  jcar 20/9/96
969 // History:
970 //********************************************************************/
GetAddr(LPSTR szHost)971 static unsigned long GetAddr(LPSTR szHost)
972 {
973 	LPHOSTENT lpstHost;
974 	u_long lAddr = INADDR_ANY;
975 
976 	/* check that we have a string */
977 	if (*szHost) {
978 
979 		/* check for a dotted-IP address string */
980 		lAddr = inet_addr(szHost);
981 
982 		/* If not an address, then try to resolve it as a hostname */
983 		if ((lAddr == INADDR_NONE) && (strcmp(szHost, "255.255.255.255"))) {
984 
985 			lpstHost = gethostbyname(szHost);
986 			if (lpstHost) {		/* success */
987 				lAddr = *((u_long FAR *) (lpstHost->h_addr));
988 			} else {
989 				lAddr = INADDR_ANY;		/* failure */
990 			}
991 		}
992 	}
993 	return (lAddr);
994 } /* end GetAddr() */
995 
996 
997 /*********************************************************************
998 // Name:  int FormatEmailAddress
999 // Input:
1000 // Output:
1001 // Description: Formats the email address to remove any content ouside
1002 //   of the angle brackets < > as per RFC 2821.
1003 //
1004 //   Returns the invalidly formatted mail address if the < > are
1005 //   unbalanced (the SMTP server should reject it if it's out of spec.)
1006 //
1007 // Author/Date:  garretts 08/18/2009
1008 // History:
1009 //********************************************************************/
FormatEmailAddress(char * Buf,char * EmailAddress,char * FormatString)1010 static int FormatEmailAddress(char* Buf, char* EmailAddress, char* FormatString) {
1011 	char *tmpAddress1, *tmpAddress2;
1012 	int result;
1013 
1014 	if( (tmpAddress1 = strchr(EmailAddress, '<')) && (tmpAddress2 = strchr(tmpAddress1, '>'))  ) {
1015 		*tmpAddress2 = 0; // terminate the string temporarily.
1016 		result = snprintf(Buf, MAIL_BUFFER_SIZE, FormatString , tmpAddress1+1);
1017 		*tmpAddress2 = '>'; // put it back the way it was.
1018 		return result;
1019 	}
1020 	return snprintf(Buf, MAIL_BUFFER_SIZE , FormatString , EmailAddress );
1021 } /* end FormatEmailAddress() */
1022 
1023 /*
1024  * Local variables:
1025  * tab-width: 4
1026  * c-basic-offset: 4
1027  * End:
1028  * vim600: sw=4 ts=4 fdm=marker
1029  * vim<600: sw=4 ts=4
1030  */
1031