xref: /PHP-7.0/win32/sendmail.c (revision 69b48f83)
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_lc) {
278 				zend_string_free(headers_lc);
279 			}
280 			*error = W32_SM_SENDMAIL_FROM_NOT_SET;
281 			return FAILURE;
282 		}
283 	}
284 
285 	/* attempt to connect with mail host */
286 	*error = MailConnect();
287 	if (*error != 0) {
288 		if (RPath) {
289 			efree(RPath);
290 		}
291 		if (headers) {
292 			zend_string_free(headers_trim);
293 			zend_string_free(headers_lc);
294 		}
295 		/* 128 is safe here, the specifier in snprintf isn't longer than that */
296 		if (NULL == (*error_message = ecalloc(1, HOST_NAME_LEN + 128))) {
297 			return FAILURE;
298 		}
299 		snprintf(*error_message, HOST_NAME_LEN + 128,
300 			"Failed to connect to mailserver at \"%s\" port %d, verify your \"SMTP\" "
301 			"and \"smtp_port\" setting in php.ini or use ini_set()",
302 			PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
303 		return FAILURE;
304 	} else {
305 		ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers ? ZSTR_VAL(headers_trim) : NULL, headers ? ZSTR_VAL(headers_lc) : NULL, error_message);
306 		TSMClose();
307 		if (RPath) {
308 			efree(RPath);
309 		}
310 		if (headers) {
311 			zend_string_free(headers_trim);
312 			zend_string_free(headers_lc);
313 		}
314 		if (ret != SUCCESS) {
315 			*error = ret;
316 			return FAILURE;
317 		}
318 		return SUCCESS;
319 	}
320 }
321 
322 //********************************************************************
323 // Name:  TSendMail::~TSendMail
324 // Input:
325 // Output:
326 // Description: DESTRUCTOR
327 // Author/Date:  jcar 20/9/96
328 // History:
329 //********************************************************************/
TSMClose()330 PHPAPI void TSMClose()
331 {
332 	Post("QUIT\r\n");
333 	Ack(NULL);
334 	/* to guarantee that the cleanup is not made twice and
335 	   compomise the rest of the application if sockets are used
336 	   elesewhere
337 	*/
338 
339 	shutdown(PW32G(mail_socket), 0);
340 	closesocket(PW32G(mail_socket));
341 }
342 
343 
344 /*********************************************************************
345 // Name:  char *GetSMErrorText
346 // Input:   Error index returned by the menber functions
347 // Output:  pointer to a string containing the error description
348 // Description:
349 // Author/Date:  jcar 20/9/96
350 // History:
351 //*******************************************************************/
GetSMErrorText(int index)352 PHPAPI char *GetSMErrorText(int index)
353 {
354 	if (MIN_ERROR_INDEX <= index && index < MAX_ERROR_INDEX) {
355 		return (ErrorMessages[index]);
356 
357 	} else {
358 		return (ErrorMessages[UNKNOWN_ERROR]);
359 
360 	}
361 }
362 
363 PHPAPI zend_string *php_str_to_str(char *haystack, size_t length, char *needle,
364 		size_t needle_len, char *str, size_t str_len);
365 
366 
367 /*********************************************************************
368 // Name:  SendText
369 // Input:       1) RPath:   return path of the message
370 //                                  Is used to fill the "Return-Path" and the
371 //                                  "X-Sender" fields of the message.
372 //                  2) Subject: Subject field of the message. If NULL is given
373 //                                  the subject is set to "No Subject"
374 //                  3) mailTo:  Destination address
375 //                  4) data:        Null terminated string containing the data to be send.
376 //                  5,6) headers of the message. Note that the second
377 //                  parameter, headers_lc, is actually a lowercased version of
378 //                  headers. The should match exactly (in terms of length),
379 //                  only differ in case
380 // Output:      Error code or SUCCESS
381 // Description:
382 // Author/Date:  jcar 20/9/96
383 // History:
384 //*******************************************************************/
SendText(char * RPath,char * Subject,char * mailTo,char * mailCc,char * mailBcc,char * data,char * headers,char * headers_lc,char ** error_message)385 static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char *mailBcc, char *data,
386 			 char *headers, char *headers_lc, char **error_message)
387 {
388 	int res;
389 	char *p;
390 	char *tempMailTo, *token, *pos1, *pos2;
391 	char *server_response = NULL;
392 	char *stripped_header  = NULL;
393 	zend_string *data_cln;
394 
395 	/* check for NULL parameters */
396 	if (data == NULL)
397 		return (BAD_MSG_CONTENTS);
398 	if (mailTo == NULL)
399 		return (BAD_MSG_DESTINATION);
400 	if (RPath == NULL)
401 		return (BAD_MSG_RPATH);
402 
403 	/* simple checks for the mailto address */
404 	/* have ampersand ? */
405 	/* mfischer, 20020514: I commented this out because it really
406 	   seems bogus. Only a username for example may still be a
407 	   valid address at the destination system.
408 	if (strchr(mailTo, '@') == NULL)
409 		return (BAD_MSG_DESTINATION);
410 	*/
411 
412 	snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host));
413 
414 	/* in the beginning of the dialog */
415 	/* attempt reconnect if the first Post fail */
416 	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
417 		int err = MailConnect();
418 		if (0 != err) {
419 			return (FAILED_TO_SEND);
420 		}
421 
422 		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
423 			return (res);
424 		}
425 	}
426 	if ((res = Ack(&server_response)) != SUCCESS) {
427 		SMTP_ERROR_RESPONSE(server_response);
428 		return (res);
429 	}
430 
431 	SMTP_SKIP_SPACE(RPath);
432 	FormatEmailAddress(PW32G(mail_buffer), RPath, "MAIL FROM:<%s>\r\n");
433 	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
434 		return (res);
435 	}
436 	if ((res = Ack(&server_response)) != SUCCESS) {
437 		SMTP_ERROR_RESPONSE(server_response);
438 		return W32_SM_SENDMAIL_FROM_MALFORMED;
439 	}
440 
441 	tempMailTo = estrdup(mailTo);
442 	/* Send mail to all rcpt's */
443 	token = strtok(tempMailTo, ",");
444 	while (token != NULL)
445 	{
446 		SMTP_SKIP_SPACE(token);
447 		FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
448 		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
449 			efree(tempMailTo);
450 			return (res);
451 		}
452 		if ((res = Ack(&server_response)) != SUCCESS) {
453 			SMTP_ERROR_RESPONSE(server_response);
454 			efree(tempMailTo);
455 			return (res);
456 		}
457 		token = strtok(NULL, ",");
458 	}
459 	efree(tempMailTo);
460 
461 	if (mailCc && *mailCc) {
462 		tempMailTo = estrdup(mailCc);
463 		/* Send mail to all rcpt's */
464 		token = strtok(tempMailTo, ",");
465 		while (token != NULL)
466 		{
467 			SMTP_SKIP_SPACE(token);
468 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
469 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
470 				efree(tempMailTo);
471 				return (res);
472 			}
473 			if ((res = Ack(&server_response)) != SUCCESS) {
474 				SMTP_ERROR_RESPONSE(server_response);
475 				efree(tempMailTo);
476 				return (res);
477 			}
478 			token = strtok(NULL, ",");
479 		}
480 		efree(tempMailTo);
481 	}
482 	/* Send mail to all Cc rcpt's */
483 	else if (headers && (pos1 = strstr(headers_lc, "cc:")) && ((pos1 == headers_lc) || (*(pos1-1) == '\n'))) {
484 		/* Real offset is memaddress from the original headers + difference of
485 		 * string found in the lowercase headrs + 3 characters to jump over
486 		 * the cc: */
487 		pos1 = headers + (pos1 - headers_lc) + 3;
488 		if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
489 			tempMailTo = estrndup(pos1, strlen(pos1));
490 		} else {
491 			tempMailTo = estrndup(pos1, pos2 - pos1);
492 		}
493 
494 		token = strtok(tempMailTo, ",");
495 		while (token != NULL)
496 		{
497 			SMTP_SKIP_SPACE(token);
498 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
499 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
500 				efree(tempMailTo);
501 				return (res);
502 			}
503 			if ((res = Ack(&server_response)) != SUCCESS) {
504 				SMTP_ERROR_RESPONSE(server_response);
505 				efree(tempMailTo);
506 				return (res);
507 			}
508 			token = strtok(NULL, ",");
509 		}
510 		efree(tempMailTo);
511 	}
512 
513 	/* Send mail to all Bcc rcpt's
514 	   This is basically a rip of the Cc code above.
515 	   Just don't forget to remove the Bcc: from the header afterwards. */
516 	if (mailBcc && *mailBcc) {
517 		tempMailTo = estrdup(mailBcc);
518 		/* Send mail to all rcpt's */
519 		token = strtok(tempMailTo, ",");
520 		while (token != NULL)
521 		{
522 			SMTP_SKIP_SPACE(token);
523 			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
524 			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
525 				efree(tempMailTo);
526 				return (res);
527 			}
528 			if ((res = Ack(&server_response)) != SUCCESS) {
529 				SMTP_ERROR_RESPONSE(server_response);
530 				efree(tempMailTo);
531 				return (res);
532 			}
533 			token = strtok(NULL, ",");
534 		}
535 		efree(tempMailTo);
536 	}
537 	else if (headers) {
538 		if ((pos1 = strstr(headers_lc, "bcc:")) && (pos1 == headers_lc || *(pos1-1) == '\n')) {
539 			/* Real offset is memaddress from the original headers + difference of
540 			 * string found in the lowercase headrs + 4 characters to jump over
541 			 * the bcc: */
542 			pos1 = headers + (pos1 - headers_lc) + 4;
543 			if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
544 				tempMailTo = estrndup(pos1, strlen(pos1));
545 				/* Later, when we remove the Bcc: out of the
546 				   header we know it was the last thing. */
547 				pos2 = pos1;
548 			} else {
549 				tempMailTo = estrndup(pos1, pos2 - pos1);
550 			}
551 
552 			token = strtok(tempMailTo, ",");
553 			while (token != NULL)
554 			{
555 				SMTP_SKIP_SPACE(token);
556 				FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
557 				if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
558 					efree(tempMailTo);
559 					return (res);
560 				}
561 				if ((res = Ack(&server_response)) != SUCCESS) {
562 					SMTP_ERROR_RESPONSE(server_response);
563 					efree(tempMailTo);
564 					return (res);
565 				}
566 				token = strtok(NULL, ",");
567 			}
568 			efree(tempMailTo);
569 
570 			/* Now that we've identified that we've a Bcc list,
571 			   remove it from the current header. */
572 			if (NULL == (stripped_header = ecalloc(1, strlen(headers)))) {
573 				return OUT_OF_MEMORY;
574 			}
575 			/* headers = point to string start of header
576 			   pos1    = pointer IN headers where the Bcc starts
577 			   '4'     = Length of the characters 'bcc:'
578 			   Because we've added +4 above for parsing the Emails
579 			   we've to subtract them here. */
580 			memcpy(stripped_header, headers, pos1 - headers - 4);
581 			if (pos1 != pos2) {
582 				/* if pos1 != pos2 , pos2 points to the rest of the headers.
583 				   Since pos1 != pos2 if "\r\n" was found, we know those characters
584 				   are there and so we jump over them (else we would generate a new header
585 				   which would look like "\r\n\r\n". */
586 				memcpy(stripped_header + (pos1 - headers - 4), pos2 + 2, strlen(pos2) - 2);
587 			}
588 		}
589 	}
590 
591 	/* Simplify the code that we create a copy of stripped_header no matter if
592 	   we actually strip something or not. So we've a single efree() later. */
593 	if (headers && !stripped_header) {
594 		if (NULL == (stripped_header = estrndup(headers, strlen(headers)))) {
595 			return OUT_OF_MEMORY;
596 		}
597 	}
598 
599 	if ((res = Post("DATA\r\n")) != SUCCESS) {
600 		if (stripped_header) {
601 			efree(stripped_header);
602 		}
603 		return (res);
604 	}
605 	if ((res = Ack(&server_response)) != SUCCESS) {
606 		SMTP_ERROR_RESPONSE(server_response);
607 		if (stripped_header) {
608 			efree(stripped_header);
609 		}
610 		return (res);
611 	}
612 
613 	/* send message header */
614 	if (Subject == NULL) {
615 		res = PostHeader(RPath, "No Subject", mailTo, stripped_header);
616 	} else {
617 		res = PostHeader(RPath, Subject, mailTo, stripped_header);
618 	}
619 	if (stripped_header) {
620 		efree(stripped_header);
621 	}
622 	if (res != SUCCESS) {
623 		return (res);
624 	}
625 
626 	/* Escape \n. sequences
627 	 * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter
628 	 * uses ZVAL as it's parameters */
629 	data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1,
630 					PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1);
631 	if (!data_cln) {
632 		data_cln = ZSTR_EMPTY_ALLOC();
633 	}
634 
635 	/* send message contents in 1024 chunks */
636 	{
637 		char c, *e2, *e = ZSTR_VAL(data_cln) + ZSTR_LEN(data_cln);
638 		p = ZSTR_VAL(data_cln);
639 
640 		while (e - p > 1024) {
641 			e2 = p + 1024;
642 			c = *e2;
643 			*e2 = '\0';
644 			if ((res = Post(p)) != SUCCESS) {
645 				zend_string_free(data_cln);
646 				return(res);
647 			}
648 			*e2 = c;
649 			p = e2;
650 		}
651 		if ((res = Post(p)) != SUCCESS) {
652 			zend_string_free(data_cln);
653 			return(res);
654 		}
655 	}
656 
657 	zend_string_free(data_cln);
658 
659 	/*send termination dot */
660 	if ((res = Post("\r\n.\r\n")) != SUCCESS)
661 		return (res);
662 	if ((res = Ack(&server_response)) != SUCCESS) {
663 		SMTP_ERROR_RESPONSE(server_response);
664 		return (res);
665 	}
666 
667 	return (SUCCESS);
668 }
669 
addToHeader(char ** header_buffer,const char * specifier,char * string)670 static int addToHeader(char **header_buffer, const char *specifier, char *string)
671 {
672 	if (NULL == (*header_buffer = erealloc(*header_buffer, strlen(*header_buffer) + strlen(specifier) + strlen(string) + 1))) {
673 		return 0;
674 	}
675 	sprintf(*header_buffer + strlen(*header_buffer), specifier, string);
676 	return 1;
677 }
678 
679 /*********************************************************************
680 // Name:  PostHeader
681 // Input:       1) return path
682 //              2) Subject
683 //              3) destination address
684 //              4) headers
685 // Output:      Error code or Success
686 // Description:
687 // Author/Date:  jcar 20/9/96
688 // History:
689 //********************************************************************/
PostHeader(char * RPath,char * Subject,char * mailTo,char * xheaders)690 static int PostHeader(char *RPath, char *Subject, char *mailTo, char *xheaders)
691 {
692 	/* Print message header according to RFC 822 */
693 	/* Return-path, Received, Date, From, Subject, Sender, To, cc */
694 
695 	int res;
696 	char *header_buffer;
697 	char *headers_lc = NULL;
698 	size_t i;
699 
700 	if (xheaders) {
701 		size_t headers_lc_len;
702 
703 		if (NULL == (headers_lc = estrdup(xheaders))) {
704 			return OUT_OF_MEMORY;
705 		}
706 
707 		headers_lc_len = strlen(headers_lc);
708 
709 		for (i = 0; i < headers_lc_len; i++) {
710 			headers_lc[i] = tolower(headers_lc[i]);
711 		}
712 	}
713 
714 	header_buffer = ecalloc(1, MAIL_BUFFER_SIZE);
715 
716 	if (!xheaders || !strstr(headers_lc, "date:")) {
717 		time_t tNow = time(NULL);
718 		zend_string *dt = php_format_date("r", 1, tNow, 1);
719 
720 		snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", ZSTR_VAL(dt));
721 		zend_string_free(dt);
722 	}
723 
724 	if (!headers_lc || !strstr(headers_lc, "from:")) {
725 		if (!addToHeader(&header_buffer, "From: %s\r\n", RPath)) {
726 			goto PostHeader_outofmem;
727 		}
728 	}
729 	if (!addToHeader(&header_buffer, "Subject: %s\r\n", Subject)) {
730 		goto PostHeader_outofmem;
731 	}
732 
733 	/* Only add the To: field from the $to parameter if isn't in the custom headers */
734 	if ((headers_lc && (!strstr(headers_lc, "\r\nto:") && (strncmp(headers_lc, "to:", 3) != 0))) || !headers_lc) {
735 		if (!addToHeader(&header_buffer, "To: %s\r\n", mailTo)) {
736 			goto PostHeader_outofmem;
737 		}
738 	}
739 	if (xheaders) {
740 		if (!addToHeader(&header_buffer, "%s\r\n", xheaders)) {
741 			goto PostHeader_outofmem;
742 		}
743 	}
744 
745 	if (headers_lc) {
746 		efree(headers_lc);
747 	}
748 	if ((res = Post(header_buffer)) != SUCCESS) {
749 		efree(header_buffer);
750 		return (res);
751 	}
752 	efree(header_buffer);
753 
754 	if ((res = Post("\r\n")) != SUCCESS) {
755 		return (res);
756 	}
757 
758 	return (SUCCESS);
759 
760 PostHeader_outofmem:
761 	if (headers_lc) {
762 		efree(headers_lc);
763 	}
764 	return OUT_OF_MEMORY;
765 }
766 
767 
768 
769 /*********************************************************************
770 // Name:  MailConnect
771 // Input:   None
772 // Output:  None
773 // Description: Connect to the mail host and receive the welcome message.
774 // Author/Date:  jcar 20/9/96
775 // History:
776 //********************************************************************/
MailConnect()777 static int MailConnect()
778 {
779 
780 	int res, namelen;
781 	short portnum;
782 	struct hostent *ent;
783 	IN_ADDR addr;
784 #ifdef HAVE_IPV6
785 	IN6_ADDR addr6;
786 #endif
787 	SOCKADDR_IN sock_in;
788 
789 #if SENDMAIL_DEBUG
790 return 0;
791 #endif
792 
793 	/* Create Socket */
794 	if ((PW32G(mail_socket) = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
795 		return (FAILED_TO_OBTAIN_SOCKET_HANDLE);
796 	}
797 
798 	/* Get our own host name */
799 	if (gethostname(PW32G(mail_local_host), HOST_NAME_LEN)) {
800 		closesocket(PW32G(mail_socket));
801 		return (FAILED_TO_GET_HOSTNAME);
802 	}
803 
804 	ent = gethostbyname(PW32G(mail_local_host));
805 
806 	if (!ent) {
807 		closesocket(PW32G(mail_socket));
808 		return (FAILED_TO_GET_HOSTNAME);
809 	}
810 
811 	namelen = (int)strlen(ent->h_name);
812 
813 #ifdef HAVE_IPV6
814 	if (inet_pton(AF_INET, ent->h_name, &addr) == 1 || inet_pton(AF_INET6, ent->h_name, &addr6) == 1)
815 #else
816 	if (inet_pton(AF_INET, ent->h_name, &addr) == 1)
817 #endif
818 	{
819 		if (namelen + 2 >= HOST_NAME_LEN) {
820 			closesocket(PW32G(mail_socket));
821 			return (FAILED_TO_GET_HOSTNAME);
822 		}
823 
824 		strcpy(PW32G(mail_local_host), "[");
825 		strcpy(PW32G(mail_local_host) + 1, ent->h_name);
826 		strcpy(PW32G(mail_local_host) + namelen + 1, "]");
827 	} else {
828 		if (namelen >= HOST_NAME_LEN) {
829 			closesocket(PW32G(mail_socket));
830 			return (FAILED_TO_GET_HOSTNAME);
831 		}
832 
833 		strcpy(PW32G(mail_local_host), ent->h_name);
834 	}
835 
836 	/* Resolve the servers IP */
837 	/*
838 	if (!isdigit(PW32G(mail_host)[0])||!gethostbyname(PW32G(mail_host)))
839 	{
840 		return (FAILED_TO_RESOLVE_HOST);
841 	}
842 	*/
843 
844 	portnum = (short) INI_INT("smtp_port");
845 	if (!portnum) {
846 		portnum = 25;
847 	}
848 
849 	/* Connect to server */
850 	sock_in.sin_family = AF_INET;
851 	sock_in.sin_port = htons(portnum);
852 	sock_in.sin_addr.S_un.S_addr = GetAddr(PW32G(mail_host));
853 
854 	if (connect(PW32G(mail_socket), (LPSOCKADDR) & sock_in, sizeof(sock_in))) {
855 		closesocket(PW32G(mail_socket));
856 		return (FAILED_TO_CONNECT);
857 	}
858 
859 	/* receive Server welcome message */
860 	res = Ack(NULL);
861 	return (res);
862 }
863 
864 
865 /*********************************************************************
866 // Name:  Post
867 // Input:
868 // Output:
869 // Description:
870 // Author/Date:  jcar 20/9/96
871 // History:
872 //********************************************************************/
Post(LPCSTR msg)873 static int Post(LPCSTR msg)
874 {
875 	int len = (int)strlen(msg);
876 	int slen;
877 	int index = 0;
878 
879 #if SENDMAIL_DEBUG
880 	if (msg)
881 		printf("POST: '%s'\n", msg);
882 	return (SUCCESS);
883 #endif
884 
885 	while (len > 0) {
886 		if ((slen = send(PW32G(mail_socket), msg + index, len, 0)) < 1)
887 			return (FAILED_TO_SEND);
888 		len -= slen;
889 		index += slen;
890 	}
891 	return (SUCCESS);
892 }
893 
894 
895 
896 /*********************************************************************
897 // Name:  Ack
898 // Input:
899 // Output:
900 // Description:
901 // Get the response from the server. We only want to know if the
902 // last command was successful.
903 // Author/Date:  jcar 20/9/96
904 // History:
905 //********************************************************************/
Ack(char ** server_response)906 static int Ack(char **server_response)
907 {
908 	ZEND_TLS char buf[MAIL_BUFFER_SIZE];
909 	int rlen;
910 	int Index = 0;
911 	int Received = 0;
912 
913 #if SENDMAIL_DEBUG
914 	return (SUCCESS);
915 #endif
916 
917 again:
918 
919 	if ((rlen = recv(PW32G(mail_socket), buf + Index, ((MAIL_BUFFER_SIZE) - 1) - Received, 0)) < 1) {
920 		return (FAILED_TO_RECEIVE);
921 	}
922 	Received += rlen;
923 	buf[Received] = 0;
924 	/*err_msg   fprintf(stderr,"Received: (%d bytes) %s", rlen, buf + Index); */
925 
926 	/* Check for newline */
927 	Index += rlen;
928 
929 	/* SMPT RFC says \r\n is the only valid line ending, who are we to argue ;)
930 	 * The response code must contain at least 5 characters ex. 220\r\n */
931 	if (Received < 5 || buf[Received - 1] != '\n' || buf[Received - 2] != '\r') {
932 		goto again;
933 	}
934 
935 	if (buf[0] > '3') {
936 		/* If we've a valid pointer, return the SMTP server response so the error message contains more information */
937 		if (server_response) {
938 			int dec = 0;
939 			/* See if we have something like \r, \n, \r\n or \n\r at the end of the message and chop it off */
940 			if (Received > 2) {
941 				if (buf[Received-1] == '\n' || buf[Received-1] == '\r') {
942 					dec++;
943 					if (buf[Received-2] == '\r' || buf[Received-2] == '\n') {
944 						dec++;
945 					}
946 				}
947 
948 			}
949 			*server_response = estrndup(buf, Received - dec);
950 		}
951 		return (SMTP_SERVER_ERROR);
952 	}
953 
954 	return (SUCCESS);
955 }
956 
957 
958 /*********************************************************************
959 // Name:  unsigned long GetAddr (LPSTR szHost)
960 // Input:
961 // Output:
962 // Description: Given a string, it will return an IP address.
963 //   - first it tries to convert the string directly
964 //   - if that fails, it tries o resolve it as a hostname
965 //
966 // WARNING: gethostbyname() is a blocking function
967 // Author/Date:  jcar 20/9/96
968 // History:
969 //********************************************************************/
GetAddr(LPSTR szHost)970 static unsigned long GetAddr(LPSTR szHost)
971 {
972 	LPHOSTENT lpstHost;
973 	u_long lAddr = INADDR_ANY;
974 
975 	/* check that we have a string */
976 	if (*szHost) {
977 
978 		/* check for a dotted-IP address string */
979 		lAddr = inet_addr(szHost);
980 
981 		/* If not an address, then try to resolve it as a hostname */
982 		if ((lAddr == INADDR_NONE) && (strcmp(szHost, "255.255.255.255"))) {
983 
984 			lpstHost = gethostbyname(szHost);
985 			if (lpstHost) {		/* success */
986 				lAddr = *((u_long FAR *) (lpstHost->h_addr));
987 			} else {
988 				lAddr = INADDR_ANY;		/* failure */
989 			}
990 		}
991 	}
992 	return (lAddr);
993 } /* end GetAddr() */
994 
995 
996 /*********************************************************************
997 // Name:  int FormatEmailAddress
998 // Input:
999 // Output:
1000 // Description: Formats the email address to remove any content ouside
1001 //   of the angle brackets < > as per RFC 2821.
1002 //
1003 //   Returns the invalidly formatted mail address if the < > are
1004 //   unbalanced (the SMTP server should reject it if it's out of spec.)
1005 //
1006 // Author/Date:  garretts 08/18/2009
1007 // History:
1008 //********************************************************************/
FormatEmailAddress(char * Buf,char * EmailAddress,char * FormatString)1009 static int FormatEmailAddress(char* Buf, char* EmailAddress, char* FormatString) {
1010 	char *tmpAddress1, *tmpAddress2;
1011 	int result;
1012 
1013 	if( (tmpAddress1 = strchr(EmailAddress, '<')) && (tmpAddress2 = strchr(tmpAddress1, '>'))  ) {
1014 		*tmpAddress2 = 0; // terminate the string temporarily.
1015 		result = snprintf(Buf, MAIL_BUFFER_SIZE, FormatString , tmpAddress1+1);
1016 		*tmpAddress2 = '>'; // put it back the way it was.
1017 		return result;
1018 	}
1019 	return snprintf(Buf, MAIL_BUFFER_SIZE , FormatString , EmailAddress );
1020 } /* end FormatEmailAddress() */
1021 
1022 /*
1023  * Local variables:
1024  * tab-width: 4
1025  * c-basic-offset: 4
1026  * End:
1027  * vim600: sw=4 ts=4 fdm=marker
1028  * vim<600: sw=4 ts=4
1029  */
1030