@@ -333,6 +333,37 @@ PHPAPI char *GetSMErrorText(int index)
333
333
}
334
334
}
335
335
336
+ /* strtok_r like, but recognizes quoted-strings */
337
+ static char * find_address (char * list , char * * state )
338
+ {
339
+ zend_bool in_quotes = 0 ;
340
+ char * p = list ;
341
+
342
+ if (list == NULL ) {
343
+ if (* state == NULL ) {
344
+ return NULL ;
345
+ }
346
+ p = list = * state ;
347
+ }
348
+ * state = NULL ;
349
+ while ((p = strpbrk (p , ",\"\\" )) != NULL ) {
350
+ if (* p == '\\' && in_quotes ) {
351
+ if (p [1 ] == '\0' ) {
352
+ /* invalid address; let SMTP server deal with it */
353
+ break ;
354
+ }
355
+ p ++ ;
356
+ } else if (* p == '"' ) {
357
+ in_quotes = !in_quotes ;
358
+ } else if (* p == ',' && !in_quotes ) {
359
+ * p = '\0' ;
360
+ * state = p + 1 ;
361
+ break ;
362
+ }
363
+ p ++ ;
364
+ }
365
+ return list ;
366
+ }
336
367
337
368
/*********************************************************************
338
369
// Name: SendText
@@ -357,7 +388,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
357
388
{
358
389
int res ;
359
390
char * p ;
360
- char * tempMailTo , * token ;
391
+ char * tempMailTo , * token , * token_state ;
361
392
const char * pos1 , * pos2 ;
362
393
char * server_response = NULL ;
363
394
char * stripped_header = NULL ;
@@ -411,7 +442,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
411
442
412
443
tempMailTo = estrdup (mailTo );
413
444
/* Send mail to all rcpt's */
414
- token = strtok (tempMailTo , "," );
445
+ token = find_address (tempMailTo , & token_state );
415
446
while (token != NULL )
416
447
{
417
448
SMTP_SKIP_SPACE (token );
@@ -425,14 +456,14 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
425
456
efree (tempMailTo );
426
457
return (res );
427
458
}
428
- token = strtok (NULL , "," );
459
+ token = find_address (NULL , & token_state );
429
460
}
430
461
efree (tempMailTo );
431
462
432
463
if (mailCc && * mailCc ) {
433
464
tempMailTo = estrdup (mailCc );
434
465
/* Send mail to all rcpt's */
435
- token = strtok (tempMailTo , "," );
466
+ token = find_address (tempMailTo , & token_state );
436
467
while (token != NULL )
437
468
{
438
469
SMTP_SKIP_SPACE (token );
@@ -446,7 +477,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
446
477
efree (tempMailTo );
447
478
return (res );
448
479
}
449
- token = strtok (NULL , "," );
480
+ token = find_address (NULL , & token_state );
450
481
}
451
482
efree (tempMailTo );
452
483
}
@@ -472,7 +503,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
472
503
tempMailTo = estrndup (pos1 , pos2 - pos1 );
473
504
}
474
505
475
- token = strtok (tempMailTo , "," );
506
+ token = find_address (tempMailTo , & token_state );
476
507
while (token != NULL )
477
508
{
478
509
SMTP_SKIP_SPACE (token );
@@ -486,7 +517,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
486
517
efree (tempMailTo );
487
518
return (res );
488
519
}
489
- token = strtok (NULL , "," );
520
+ token = find_address (NULL ,& token_state );
490
521
}
491
522
efree (tempMailTo );
492
523
}
@@ -497,7 +528,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
497
528
if (mailBcc && * mailBcc ) {
498
529
tempMailTo = estrdup (mailBcc );
499
530
/* Send mail to all rcpt's */
500
- token = strtok (tempMailTo , "," );
531
+ token = find_address (tempMailTo , & token_state );
501
532
while (token != NULL )
502
533
{
503
534
SMTP_SKIP_SPACE (token );
@@ -511,7 +542,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
511
542
efree (tempMailTo );
512
543
return (res );
513
544
}
514
- token = strtok (NULL , "," );
545
+ token = find_address (NULL , & token_state );
515
546
}
516
547
efree (tempMailTo );
517
548
}
@@ -545,7 +576,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
545
576
}
546
577
}
547
578
548
- token = strtok (tempMailTo , "," );
579
+ token = find_address (tempMailTo , & token_state );
549
580
while (token != NULL )
550
581
{
551
582
SMTP_SKIP_SPACE (token );
@@ -559,7 +590,7 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, char *
559
590
efree (tempMailTo );
560
591
return (res );
561
592
}
562
- token = strtok (NULL , "," );
593
+ token = find_address (NULL , & token_state );
563
594
}
564
595
efree (tempMailTo );
565
596
@@ -979,6 +1010,46 @@ static unsigned long GetAddr(LPSTR szHost)
979
1010
return (lAddr );
980
1011
} /* end GetAddr() */
981
1012
1013
+ /* returns the contents of an angle-addr (caller needs to efree) or NULL */
1014
+ static char * get_angle_addr (char * address )
1015
+ {
1016
+ zend_bool in_quotes = 0 ;
1017
+ char * p1 = address , * p2 ;
1018
+
1019
+ while ((p1 = strpbrk (p1 , "<\"\\" )) != NULL ) {
1020
+ if (* p1 == '\\' && in_quotes ) {
1021
+ if (p1 [1 ] == '\0' ) {
1022
+ /* invalid address; let SMTP server deal with it */
1023
+ return NULL ;
1024
+ }
1025
+ p1 ++ ;
1026
+ } else if (* p1 == '"' ) {
1027
+ in_quotes = !in_quotes ;
1028
+ } else if (* p1 == '<' && !in_quotes ) {
1029
+ break ;
1030
+ }
1031
+ p1 ++ ;
1032
+ }
1033
+ if (p1 == NULL ) return NULL ;
1034
+ p2 = ++ p1 ;
1035
+ while ((p2 = strpbrk (p2 , ">\"\\" )) != NULL ) {
1036
+ if (* p2 == '\\' && in_quotes ) {
1037
+ if (p2 [1 ] == '\0' ) {
1038
+ /* invalid address; let SMTP server deal with it */
1039
+ return NULL ;
1040
+ }
1041
+ p2 ++ ;
1042
+ } else if (* p2 == '"' ) {
1043
+ in_quotes = !in_quotes ;
1044
+ } else if (* p2 == '>' && !in_quotes ) {
1045
+ break ;
1046
+ }
1047
+ p2 ++ ;
1048
+ }
1049
+ if (p2 == NULL ) return NULL ;
1050
+
1051
+ return estrndup (p1 , p2 - p1 );
1052
+ }
982
1053
983
1054
/*********************************************************************
984
1055
// Name: int FormatEmailAddress
@@ -994,13 +1065,12 @@ static unsigned long GetAddr(LPSTR szHost)
994
1065
// History:
995
1066
//********************************************************************/
996
1067
static int FormatEmailAddress (char * Buf , char * EmailAddress , char * FormatString ) {
997
- char * tmpAddress1 , * tmpAddress2 ;
1068
+ char * tmpAddress ;
998
1069
int result ;
999
1070
1000
- if ( (tmpAddress1 = strchr (EmailAddress , '<' )) && (tmpAddress2 = strchr (tmpAddress1 , '>' )) ) {
1001
- * tmpAddress2 = 0 ; // terminate the string temporarily.
1002
- result = snprintf (Buf , MAIL_BUFFER_SIZE , FormatString , tmpAddress1 + 1 );
1003
- * tmpAddress2 = '>' ; // put it back the way it was.
1071
+ if ((tmpAddress = get_angle_addr (EmailAddress )) != NULL ) {
1072
+ result = snprintf (Buf , MAIL_BUFFER_SIZE , FormatString , tmpAddress );
1073
+ efree (tmpAddress );
1004
1074
return result ;
1005
1075
}
1006
1076
return snprintf (Buf , MAIL_BUFFER_SIZE , FormatString , EmailAddress );
0 commit comments