--- embedaddon/mini_sendmail/mini_sendmail.c 2013/07/22 00:39:41 1.1.1.1.2.1 +++ embedaddon/mini_sendmail/mini_sendmail.c 2016/10/18 13:48:41 1.1.1.1.2.2 @@ -1,6 +1,6 @@ /* mini_sendmail - accept email on behalf of real sendmail ** -** Copyright © 1999 by Jef Poskanzer . +** Copyright © 1999,2015 by Jef Poskanzer . ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without @@ -44,6 +44,7 @@ #include #include #include +#include #include #ifdef DO_RECEIVED @@ -93,6 +94,10 @@ static void sigcatch( int sig ); static void show_error( char* cause ); +/* Do overlapping strcpy safely, by using memmove. */ +#define ol_strcpy(dst,src) memmove(dst,src,strlen(src)+1) + + int main( int argc, char** argv ) { @@ -175,7 +180,7 @@ main( int argc, char** argv ) /* Strip off any angle brackets in the from address. */ while ( from[0] == '<' ) - (void) strcpy( from, &from[1] ); + (void) ol_strcpy( from, &from[1] ); while ( from[strlen(from)-1] == '>' ) from[strlen(from)-1] = '\0'; @@ -349,7 +354,7 @@ make_received( char* from, char* username, char* hostn tmP = localtime( &t ); (void) strftime( timestamp, sizeof(timestamp), "%a, %d %b %Y %T %Z", tmP ); received_size = - 500 + strlen( from ) + strlen( hostname ) * 2 + strlen( VERSION ) + + 500 + strlen( from ) + strlen( hostname ) * 2 + strlen( MINI_SENDMAIL_VERSION ) + strlen( timestamp ) + strlen( username ); received = (char*) malloc( received_size ); if ( received == (char*) 0 ) @@ -360,7 +365,7 @@ make_received( char* from, char* username, char* hostn (void) snprintf( received, received_size, "Received: (from %s)\n\tby %s (%s);\n\t%s\n\t(sender %s@%s)\n", - from, hostname, VERSION, timestamp, username, hostname ); + from, hostname, MINI_SENDMAIL_VERSION, timestamp, username, hostname ); return received; } #endif /* DO_RECEIVED */ @@ -383,7 +388,7 @@ parse_for_recipients( char* message ) #define ST_RECIPS 7 char* cp; char* bcc; - char* recip; + char* recip = (char*) 0; state = ST_BOL; bcc = (char*) 0; @@ -512,7 +517,7 @@ parse_for_recipients( char* message ) if ( bcc != (char*) 0 ) { /* Elide the Bcc: line, and reset cp. */ - (void) strcpy( bcc, cp + 1 ); + (void) ol_strcpy( bcc, cp + 1 ); cp = bcc - 1; bcc = (char*) 0; } @@ -542,6 +547,7 @@ add_recipient( char* recipient, int len ) } /* Strip off any angle brackets. */ +/* while ( len > 0 && *recipient == '<' ) { ++recipient; @@ -551,6 +557,23 @@ add_recipient( char* recipient, int len ) --len; (void) snprintf( buf, sizeof(buf), "RCPT TO:<%.*s>", len, recipient ); +*/ + if (len > 0 && recipient[len-1] == '>' ) + { + /* "" or: "Full Name " */ + while (len > 0 && *recipient != '<' ) + { + ++recipient; + --len; + } + (void) snprintf( buf, sizeof(buf), "RCPT TO:%.*s", len, recipient ); + } + else + { + /* name@domain */ + (void) snprintf( buf, sizeof(buf), "RCPT TO:<%.*s>", len, recipient ); + } + send_command( buf ); status = read_response(); if ( status != 250 && status != 251 ) @@ -572,23 +595,23 @@ static int open_client_socket( void ) { #ifdef USE_IPV6 - struct sockaddr_in6 sa; + struct sockaddr_in6 sa_in; #else /* USE_IPV6 */ - struct sockaddr_in sa; + struct sockaddr_in sa_in; #endif /* USE_IPV6 */ int sa_len, sock_family, sock_type, sock_protocol; int sockfd; sock_type = SOCK_STREAM; sock_protocol = 0; - sa_len = sizeof(sa); - (void) memset( (void*) &sa, 0, sa_len ); + sa_len = sizeof(sa_in); + (void) memset( (void*) &sa_in, 0, sa_len ); #ifdef USE_IPV6 { #ifdef DO_MINUS_SP - struct sockaddr_in sa4; + struct sockaddr_in sa_in4; struct addrinfo hints; char portstr[10]; int gaierr; @@ -601,15 +624,15 @@ open_client_socket( void ) sock_family = PF_INET6; #ifdef DO_MINUS_SP - (void) memset( (void*) &sa4, 0, sizeof(sa4) ); - if ( inet_pton( AF_INET, server, (void*) &sa4.sin_addr ) == 1 ) + (void) memset( (void*) &sa_in4, 0, sizeof(sa_in4) ); + if ( inet_pton( AF_INET, server, (void*) &sa_in4.sin_addr ) == 1 ) { sock_family = PF_INET; - sa4.sin_port = htons( port ); - sa_len = sizeof(sa4); - (void) memmove( &sa, &sa4, sa_len ); + sa_in4.sin_port = htons( port ); + sa_len = sizeof(sa_in4); + (void) memmove( &sa_in, &sa_in4, sa_len ); } - else if ( inet_pton( AF_INET6, server, (void*) &sa.sin6_addr ) != 1 ) + else if ( inet_pton( AF_INET6, server, (void*) &sa_in.sin6_addr ) != 1 ) { #ifdef DO_DNS (void) memset( &hints, 0, sizeof(hints) ); @@ -645,11 +668,11 @@ open_client_socket( void ) /* If there's an IPv4 address, use that, otherwise try IPv6. */ if ( aiv4 != (struct addrinfo*) 0 ) { - if ( sizeof(sa) < aiv4->ai_addrlen ) + if ( sizeof(sa_in) < aiv4->ai_addrlen ) { (void) fprintf( stderr, "%s - sockaddr too small (%lu < %lu)\n", - server, (unsigned long) sizeof(sa), + server, (unsigned long) sizeof(sa_in), (unsigned long) aiv4->ai_addrlen ); exit( 1 ); } @@ -657,16 +680,16 @@ open_client_socket( void ) sock_type = aiv4->ai_socktype; sock_protocol = aiv4->ai_protocol; sa_len = aiv4->ai_addrlen; - (void) memmove( &sa, aiv4->ai_addr, sa_len ); + (void) memmove( &sa_in, aiv4->ai_addr, sa_len ); goto ok; } if ( aiv6 != (struct addrinfo*) 0 ) { - if ( sizeof(sa) < aiv6->ai_addrlen ) + if ( sizeof(sa_in) < aiv6->ai_addrlen ) { (void) fprintf( stderr, "%s - sockaddr too small (%lu < %lu)\n", - server, (unsigned long) sizeof(sa), + server, (unsigned long) sizeof(sa_in), (unsigned long) aiv6->ai_addrlen ); exit( 1 ); } @@ -674,7 +697,7 @@ open_client_socket( void ) sock_type = aiv6->ai_socktype; sock_protocol = aiv6->ai_protocol; sa_len = aiv6->ai_addrlen; - (void) memmove( &sa, aiv6->ai_addr, sa_len ); + (void) memmove( &sa_in, aiv6->ai_addr, sa_len ); goto ok; } @@ -691,11 +714,11 @@ open_client_socket( void ) #endif /* DO_DNS */ } #else /* DO_MINUS_SP */ - sa.sin6_addr = in6addr_any; - sa.sin6_port = htons( SMTP_PORT ); + sa_in.sin6_addr = in6addr_any; + sa_in.sin6_port = htons( SMTP_PORT ); #endif /* DO_MINUS_SP */ - sa.sin6_family = sock_family; + sa_in.sin6_family = sock_family; } @@ -711,9 +734,9 @@ open_client_socket( void ) sock_family = PF_INET; #ifdef DO_MINUS_SP - sa.sin_addr.s_addr = inet_addr( server ); - sa.sin_port = htons( port ); - if ( (int32_t) sa.sin_addr.s_addr == -1 ) + sa_in.sin_addr.s_addr = inet_addr( server ); + sa_in.sin_port = htons( port ); + if ( (int32_t) sa_in.sin_addr.s_addr == -1 ) { #ifdef DO_DNS he = gethostbyname( server ); @@ -725,7 +748,7 @@ open_client_socket( void ) exit( 1 ); } sock_family = he->h_addrtype; - (void) memmove( &sa.sin_addr, he->h_addr, he->h_length ); + (void) memmove( &sa_in.sin_addr, he->h_addr, he->h_length ); #else /* DO_DNS */ (void) fprintf( stderr, "%s: bad server IP address %s\n", argv0, server ); @@ -733,11 +756,11 @@ open_client_socket( void ) #endif /* DO_DNS */ } #else /* DO_MINUS_SP */ - (void) memmove( &sa.sin_addr, local_addr, sizeof(local_addr) ); - sa.sin_port = htons( SMTP_PORT ); + (void) memmove( &sa_in.sin_addr, local_addr, sizeof(local_addr) ); + sa_in.sin_port = htons( SMTP_PORT ); #endif /* DO_MINUS_SP */ - sa.sin_family = sock_family; + sa_in.sin_family = sock_family; } #endif /* USE_IPV6 */ @@ -746,7 +769,7 @@ open_client_socket( void ) if ( sockfd < 0 ) show_error( "socket" ); - if ( connect( sockfd, (struct sockaddr*) &sa, sa_len ) < 0 ) + if ( connect( sockfd, (struct sockaddr*) &sa_in, sa_len ) < 0 ) show_error( "connect" ); return sockfd;