2011-11-14 15:57:52 +01:00
/*
* parser . c , SJ
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <fcntl.h>
# include <unistd.h>
# include <piler.h>
2012-06-01 14:25:49 +02:00
struct _state parse_message ( struct session_data * sdata , int take_into_pieces , struct __config * cfg ) {
2011-11-14 15:57:52 +01:00
FILE * f ;
2012-06-06 14:29:03 +02:00
int i , len ;
char * p , buf [ MAXBUFSIZE ] , puf [ SMALLBUFSIZE ] ;
2012-08-21 21:57:39 +02:00
char writebuffer [ MAXBUFSIZE ] , abuffer [ MAXBUFSIZE ] ;
2011-11-14 15:57:52 +01:00
struct _state state ;
2011-11-16 14:47:47 +01:00
init_state ( & state ) ;
2011-11-14 15:57:52 +01:00
2012-01-03 00:19:43 +01:00
f = fopen ( sdata - > filename , " r " ) ;
2011-11-14 15:57:52 +01:00
if ( ! f ) {
syslog ( LOG_PRIORITY , " %s: cannot open " , sdata - > ttmpfile ) ;
return state ;
}
2011-11-19 21:25:44 +01:00
2012-06-06 14:29:03 +02:00
if ( sdata - > num_of_rcpt_to > 0 ) {
for ( i = 0 ; i < sdata - > num_of_rcpt_to ; i + + ) {
extractEmail ( sdata - > rcptto [ i ] , puf ) ;
if ( strlen ( puf ) > 5 ) {
p = strstr ( puf , cfg - > hostid ) ;
if ( p & & * ( p - 1 ) = = ' . ' ) {
* ( p - 1 ) = ' ' ;
* p = ' \0 ' ;
len = strlen ( puf ) ;
if ( does_it_seem_like_an_email_address ( puf ) = = 1 ) {
if ( cfg - > verbosity > = _LOG_DEBUG ) syslog ( LOG_PRIORITY , " %s: processing rcpt to address: *%s* " , sdata - > ttmpfile , puf ) ;
2012-09-04 14:49:56 +02:00
if ( state . tolen < MAXBUFSIZE - len - 1 ) {
2012-06-06 14:29:03 +02:00
if ( is_string_on_list ( state . rcpt , puf ) = = 0 ) {
append_list ( & ( state . rcpt ) , puf ) ;
2012-09-04 14:49:56 +02:00
memcpy ( & ( state . b_to [ state . tolen ] ) , puf , len ) ;
state . tolen + = len ;
2012-06-06 14:29:03 +02:00
2012-09-04 14:49:56 +02:00
if ( state . tolen < MAXBUFSIZE - len - 1 ) {
2012-06-06 14:29:03 +02:00
split_email_address ( puf ) ;
2012-09-04 14:49:56 +02:00
memcpy ( & ( state . b_to [ state . tolen ] ) , puf , len ) ;
state . tolen + = len ;
2012-06-06 14:29:03 +02:00
}
}
}
}
}
}
}
}
2012-06-01 14:25:49 +02:00
if ( take_into_pieces = = 1 ) {
state . mfd = open ( sdata - > tmpframe , O_CREAT | O_RDWR , S_IRUSR | S_IWUSR ) ;
if ( state . mfd = = - 1 ) {
syslog ( LOG_PRIORITY , " %s: cannot open frame file: %s " , sdata - > ttmpfile , sdata - > tmpframe ) ;
fclose ( f ) ;
return state ;
}
2011-11-14 15:57:52 +01:00
}
2011-11-19 21:25:44 +01:00
while ( fgets ( buf , sizeof ( buf ) - 1 , f ) ) {
2012-08-21 21:57:39 +02:00
parse_line ( buf , & state , sdata , take_into_pieces , & writebuffer [ 0 ] , sizeof ( writebuffer ) , & abuffer [ 0 ] , sizeof ( abuffer ) , cfg ) ;
}
if ( take_into_pieces = = 1 & & state . writebufpos > 0 ) {
2012-09-28 21:48:20 +02:00
if ( sdata - > ms_journal = = 1 ) remove_trailing_journal_boundary ( sdata , & state , & writebuffer [ 0 ] ) ;
2012-09-28 10:34:04 +02:00
2012-08-21 21:57:39 +02:00
len = write ( state . mfd , writebuffer , state . writebufpos ) ;
memset ( writebuffer , 0 , sizeof ( writebuffer ) ) ;
state . writebufpos = 0 ;
2012-06-01 14:25:49 +02:00
}
if ( take_into_pieces = = 1 ) {
close ( state . mfd ) ; state . mfd = 0 ;
2011-11-19 21:25:44 +01:00
}
2012-09-28 21:48:20 +02:00
if ( sdata - > ms_journal = = 1 ) {
sdata - > tot_len - = sdata - > journal_envelope_length + sdata - > journal_bottom_length ;
}
2011-11-14 15:57:52 +01:00
fclose ( f ) ;
2012-01-03 00:19:43 +01:00
return state ;
}
void post_parse ( struct session_data * sdata , struct _state * state , struct __config * cfg ) {
2012-09-09 23:16:09 +02:00
int i , len , rec = 0 ;
2012-01-03 00:19:43 +01:00
char * p ;
free_list ( state - > boundaries ) ;
free_list ( state - > rcpt ) ;
2012-01-14 09:53:26 +01:00
free_list ( state - > rcpt_domain ) ;
2012-10-05 14:27:03 +02:00
free_list ( state - > journal_recipient ) ;
2011-11-19 21:25:44 +01:00
2012-01-03 00:19:43 +01:00
trimBuffer ( state - > b_subject ) ;
fixupEncodedHeaderLine ( state - > b_subject ) ;
2011-11-28 14:21:14 +01:00
2011-11-14 15:57:52 +01:00
2012-01-03 00:19:43 +01:00
if ( sdata - > internal_sender = = 0 ) sdata - > direction = DIRECTION_INCOMING ;
else {
if ( sdata - > internal_recipient = = 1 ) sdata - > direction = DIRECTION_INTERNAL ;
if ( sdata - > external_recipient = = 1 ) sdata - > direction = DIRECTION_OUTGOING ;
if ( sdata - > internal_recipient = = 1 & & sdata - > external_recipient = = 1 ) sdata - > direction = DIRECTION_INTERNAL_AND_OUTGOING ;
}
2011-11-19 21:25:44 +01:00
2011-11-28 14:21:14 +01:00
2012-01-03 00:19:43 +01:00
for ( i = 1 ; i < = state - > n_attachments ; i + + ) {
digest_file ( state - > attachments [ i ] . internalname , & ( state - > attachments [ i ] . digest [ 0 ] ) ) ;
fixupEncodedHeaderLine ( state - > attachments [ i ] . filename ) ;
2011-12-30 15:52:59 +01:00
2012-01-03 00:19:43 +01:00
if ( cfg - > verbosity > = _LOG_DEBUG ) syslog ( LOG_PRIORITY , " %s: attachment list: i:%d, name=*%s*, type: *%s*, size: %d, int.name: %s, digest: %s " , sdata - > ttmpfile , i , state - > attachments [ i ] . filename , state - > attachments [ i ] . type , state - > attachments [ i ] . size , state - > attachments [ i ] . internalname , state - > attachments [ i ] . digest ) ;
p = determine_attachment_type ( state - > attachments [ i ] . filename , state - > attachments [ i ] . type ) ;
2011-12-30 15:52:59 +01:00
len = strlen ( p ) ;
2012-01-03 00:19:43 +01:00
if ( strlen ( sdata - > attachments ) < SMALLBUFSIZE - len - 1 & & ! strstr ( sdata - > attachments , p ) ) memcpy ( & ( sdata - > attachments [ strlen ( sdata - > attachments ) ] ) , p , len ) ;
2012-09-07 15:08:50 +02:00
if ( state - > attachments [ i ] . dumped = = 1 ) {
2012-09-09 23:16:09 +02:00
rec = 0 ;
if ( state - > bodylen < BIGBUFSIZE - 1024 ) extract_attachment_content ( sdata , state , state - > attachments [ i ] . aname , get_attachment_extractor_by_filename ( state - > attachments [ i ] . filename ) , & rec ) ;
2012-09-07 15:08:50 +02:00
unlink ( state - > attachments [ i ] . aname ) ;
}
2011-11-19 21:25:44 +01:00
}
2012-07-06 13:02:40 +02:00
if ( state - > message_id [ 0 ] = = 0 ) {
if ( cfg - > archive_emails_not_having_message_id = = 1 )
2012-09-26 15:26:30 +02:00
snprintf ( state - > message_id , SMALLBUFSIZE - 1 , " %s " , sdata - > ttmpfile ) ;
2012-07-06 13:02:40 +02:00
else snprintf ( state - > message_id , SMALLBUFSIZE - 1 , " null " ) ;
}
2011-11-14 15:57:52 +01:00
}
2012-08-21 21:57:39 +02:00
int parse_line ( char * buf , struct _state * state , struct session_data * sdata , int take_into_pieces , char * writebuffer , int writebuffersize , char * abuffer , int abuffersize , struct __config * cfg ) {
2012-01-14 09:53:26 +01:00
char * p , * q , puf [ SMALLBUFSIZE ] ;
2012-09-07 15:08:50 +02:00
unsigned char b64buffer [ MAXBUFSIZE ] ;
2012-10-01 21:09:11 +02:00
int t , x , n , n64 , len , writelen , b64_len , boundary_line = 0 ;
2011-11-14 15:57:52 +01:00
2012-07-23 16:51:36 +02:00
if ( cfg - > debug = = 1 ) printf ( " line: %s " , buf ) ;
2011-11-14 15:57:52 +01:00
state - > line_num + + ;
2011-11-19 21:25:44 +01:00
len = strlen ( buf ) ;
2011-12-13 17:05:22 +01:00
if ( state - > is_1st_header = = 1 & & ( strncmp ( buf , " Received: by piler " , strlen ( " Received: by piler " ) ) = = 0 | | strncmp ( buf , " X-piler-id: " , strlen ( " X-piler-id: " ) ) = = 0 ) ) {
sdata - > restored_copy = 1 ;
}
2012-01-26 14:35:51 +01:00
if ( state - > is_1st_header = = 1 & & * ( cfg - > spam_header_line ) ! = ' \0 ' & & strncmp ( buf , cfg - > spam_header_line , strlen ( cfg - > spam_header_line ) ) = = 0 ) {
sdata - > spam_message = 1 ;
}
2012-09-28 21:48:20 +02:00
if ( state - > is_1st_header = = 1 & & sdata - > ms_journal = = 0 & & strncmp ( buf , " X-MS-Journal-Report: " , strlen ( " X-MS-Journal-Report: " ) ) = = 0 ) {
sdata - > ms_journal = 1 ;
2012-09-03 10:06:34 +02:00
memset ( state - > message_id , 0 , SMALLBUFSIZE ) ;
2012-09-03 11:08:12 +02:00
memset ( state - > b_from , 0 , SMALLBUFSIZE ) ;
memset ( state - > b_from_domain , 0 , SMALLBUFSIZE ) ;
2012-09-03 10:06:34 +02:00
}
2011-11-22 12:31:54 +01:00
2012-09-28 21:48:20 +02:00
if ( sdata - > ms_journal_dropped = = 0 ) {
sdata - > journal_envelope_length + = len ;
}
2011-11-22 12:31:54 +01:00
if ( state - > message_rfc822 = = 0 & & ( buf [ 0 ] = = ' \r ' | | buf [ 0 ] = = ' \n ' ) ) {
state - > message_state = MSG_BODY ;
if ( state - > is_header = = 1 ) state - > is_header = 0 ;
state - > is_1st_header = 0 ;
2011-11-19 21:25:44 +01:00
}
2012-09-28 21:48:20 +02:00
if ( sdata - > ms_journal = = 1 & & strncasecmp ( buf , " Received: " , strlen ( " Received: " ) ) = = 0 ) {
2012-09-03 11:08:12 +02:00
state - > is_1st_header = 1 ;
state - > is_header = 1 ;
memset ( state - > b_body , 0 , BIGBUFSIZE ) ;
state - > bodylen = 0 ;
memset ( state - > b_subject , 0 , MAXBUFSIZE ) ;
}
2011-11-22 12:31:54 +01:00
2012-06-01 14:25:49 +02:00
if ( take_into_pieces = = 1 ) {
if ( state - > message_state = = MSG_BODY & & state - > fd ! = - 1 & & is_item_on_string ( state - > boundaries , buf ) = = 0 ) {
2012-08-21 21:57:39 +02:00
//n = write(state->fd, buf, len); // WRITE
if ( len + state - > abufpos > abuffersize - 1 ) {
2012-09-07 15:08:50 +02:00
n = write ( state - > fd , abuffer , state - > abufpos ) ;
if ( state - > b64fd ! = - 1 ) {
abuffer [ state - > abufpos ] = ' \0 ' ;
n64 = base64_decode_attachment_buffer ( abuffer , state - > abufpos , & b64buffer [ 0 ] , sizeof ( b64buffer ) ) ;
n64 = write ( state - > b64fd , b64buffer , n64 ) ;
}
state - > abufpos = 0 ; memset ( abuffer , 0 , abuffersize ) ;
2012-08-21 21:57:39 +02:00
}
memcpy ( abuffer + state - > abufpos , buf , len ) ; state - > abufpos + = len ;
2012-06-01 14:25:49 +02:00
state - > attachments [ state - > n_attachments ] . size + = len ;
}
else {
state - > saved_size + = len ;
2012-08-21 21:57:39 +02:00
//n = write(state->mfd, buf, len); // WRITE
if ( len + state - > writebufpos > writebuffersize - 1 ) {
2012-09-28 21:48:20 +02:00
if ( sdata - > ms_journal = = 1 ) remove_trailing_journal_boundary ( sdata , state , writebuffer ) ;
2012-08-21 21:57:39 +02:00
n = write ( state - > mfd , writebuffer , state - > writebufpos ) ; state - > writebufpos = 0 ; memset ( writebuffer , 0 , writebuffersize ) ;
}
memcpy ( writebuffer + state - > writebufpos , buf , len ) ; state - > writebufpos + = len ;
2012-06-01 14:25:49 +02:00
}
2011-11-19 21:25:44 +01:00
}
2011-11-22 12:31:54 +01:00
if ( state - > message_state = = MSG_BODY & & state - > has_to_dump = = 1 & & state - > pushed_pointer = = 0 ) {
//printf("####name: %s, type: %s, base64: %d\n", state->filename, state->type, state->base64);
state - > pushed_pointer = 1 ;
// this is a real attachment to dump
2012-09-14 15:03:00 +02:00
if ( state - > base64 = = 1 & & strlen ( state - > filename ) > 4 & & strlen ( state - > type ) > 3 & & state - > n_attachments < MAX_ATTACHMENTS - 1 ) {
2011-11-22 12:31:54 +01:00
state - > n_attachments + + ;
snprintf ( state - > attachments [ state - > n_attachments ] . filename , TINYBUFSIZE - 1 , " %s " , state - > filename ) ;
snprintf ( state - > attachments [ state - > n_attachments ] . type , TINYBUFSIZE - 1 , " %s " , state - > type ) ;
snprintf ( state - > attachments [ state - > n_attachments ] . internalname , TINYBUFSIZE - 1 , " %s.a%d " , sdata - > ttmpfile , state - > n_attachments ) ;
2012-09-07 15:08:50 +02:00
snprintf ( state - > attachments [ state - > n_attachments ] . aname , TINYBUFSIZE - 1 , " %s.a%d.bin " , sdata - > ttmpfile , state - > n_attachments ) ;
2011-11-22 12:31:54 +01:00
//printf("DUMP FILE: %s\n", state->attachments[state->n_attachments].internalname);
2012-06-01 14:25:49 +02:00
if ( take_into_pieces = = 1 ) {
state - > fd = open ( state - > attachments [ state - > n_attachments ] . internalname , O_CREAT | O_RDWR , S_IRUSR | S_IWUSR ) ;
2012-09-07 15:08:50 +02:00
2012-09-09 23:16:09 +02:00
p = get_attachment_extractor_by_filename ( state - > attachments [ state - > n_attachments ] . filename ) ;
snprintf ( state - > attachments [ state - > n_attachments ] . shorttype , TINYBUFSIZE - 1 , " %s " , p ) ;
if ( strcmp ( " other " , p ) ) {
2012-09-07 15:08:50 +02:00
state - > b64fd = open ( state - > attachments [ state - > n_attachments ] . aname , O_CREAT | O_RDWR , S_IRUSR | S_IWUSR ) ;
state - > attachments [ state - > n_attachments ] . dumped = 1 ;
}
2012-09-09 23:16:09 +02:00
2012-06-01 14:25:49 +02:00
if ( state - > fd = = - 1 ) {
2011-11-23 12:24:21 +01:00
2012-06-01 14:25:49 +02:00
state - > attachments [ state - > n_attachments ] . size = 0 ;
2012-09-07 15:08:50 +02:00
state - > attachments [ state - > n_attachments ] . dumped = 0 ;
2012-06-01 14:25:49 +02:00
memset ( state - > attachments [ state - > n_attachments ] . type , 0 , TINYBUFSIZE ) ;
2012-09-09 23:16:09 +02:00
memset ( state - > attachments [ state - > n_attachments ] . shorttype , 0 , TINYBUFSIZE ) ;
memset ( state - > attachments [ state - > n_attachments ] . aname , 0 , TINYBUFSIZE ) ;
2012-06-01 14:25:49 +02:00
memset ( state - > attachments [ state - > n_attachments ] . filename , 0 , TINYBUFSIZE ) ;
memset ( state - > attachments [ state - > n_attachments ] . internalname , 0 , TINYBUFSIZE ) ;
memset ( state - > attachments [ state - > n_attachments ] . digest , 0 , 2 * DIGEST_LENGTH + 1 ) ;
2011-11-23 12:24:21 +01:00
2012-09-09 23:16:09 +02:00
2012-06-01 14:25:49 +02:00
syslog ( LOG_PRIORITY , " %s: error opening %s " , sdata - > ttmpfile , state - > attachments [ state - > n_attachments ] . internalname ) ;
2011-11-23 12:24:21 +01:00
2012-06-01 14:25:49 +02:00
state - > n_attachments - - ;
state - > has_to_dump = 0 ;
}
else {
snprintf ( puf , sizeof ( puf ) - 1 , " ATTACHMENT_POINTER_%s.a%d_XXX_PILER " , sdata - > ttmpfile , state - > n_attachments ) ;
2012-08-21 21:57:39 +02:00
//n = write(state->mfd, puf, strlen(puf)); // WRITE
writelen = strlen ( puf ) ;
if ( writelen + state - > writebufpos > writebuffersize - 1 ) {
n = write ( state - > mfd , writebuffer , state - > writebufpos ) ; state - > writebufpos = 0 ; memset ( writebuffer , 0 , writebuffersize ) ;
}
memcpy ( writebuffer + state - > writebufpos , puf , writelen ) ; state - > writebufpos + = writelen ;
2012-06-01 14:25:49 +02:00
}
2011-11-23 12:24:21 +01:00
}
2012-06-01 14:25:49 +02:00
2011-11-22 12:31:54 +01:00
}
else {
state - > has_to_dump = 0 ;
}
}
2011-11-14 15:57:52 +01:00
if ( * buf = = ' . ' & & * ( buf + 1 ) = = ' . ' ) buf + + ;
/* undefined message state */
if ( state - > is_header = = 1 & & buf [ 0 ] ! = ' ' & & buf [ 0 ] ! = ' \t ' & & strchr ( buf , ' : ' ) ) state - > message_state = MSG_UNDEF ;
/* skip empty lines */
if ( state - > message_rfc822 = = 0 & & ( buf [ 0 ] = = ' \r ' | | buf [ 0 ] = = ' \n ' ) ) {
return 0 ;
}
2012-10-01 21:09:11 +02:00
t = trimBuffer ( buf ) ;
2011-11-14 15:57:52 +01:00
/* skip the first line, if it's a "From <email address> date" format */
if ( state - > line_num = = 1 & & strncmp ( buf , " From " , 5 ) = = 0 ) return 0 ;
if ( state - > is_header = = 0 & & buf [ 0 ] ! = ' ' & & buf [ 0 ] ! = ' \t ' ) state - > message_state = MSG_BODY ;
if ( ( state - > content_type_is_set = = 0 | | state - > is_header = = 1 ) & & strncasecmp ( buf , " Content-Type: " , strlen ( " Content-Type: " ) ) = = 0 ) state - > message_state = MSG_CONTENT_TYPE ;
else if ( strncasecmp ( buf , " Content-Transfer-Encoding: " , strlen ( " Content-Transfer-Encoding: " ) ) = = 0 ) state - > message_state = MSG_CONTENT_TRANSFER_ENCODING ;
else if ( strncasecmp ( buf , " Content-Disposition: " , strlen ( " Content-Disposition: " ) ) = = 0 ) state - > message_state = MSG_CONTENT_DISPOSITION ;
if ( state - > message_state = = MSG_CONTENT_TYPE | | state - > message_state = = MSG_CONTENT_TRANSFER_ENCODING ) state - > is_header = 1 ;
/* header checks */
if ( state - > is_header = = 1 ) {
2011-11-16 14:47:47 +01:00
if ( strncasecmp ( buf , " From: " , strlen ( " From: " ) ) = = 0 ) state - > message_state = MSG_FROM ;
2011-11-14 15:57:52 +01:00
else if ( strncasecmp ( buf , " To: " , 3 ) = = 0 ) state - > message_state = MSG_TO ;
else if ( strncasecmp ( buf , " Cc: " , 3 ) = = 0 ) state - > message_state = MSG_CC ;
2012-10-08 21:06:48 +02:00
else if ( strncasecmp ( buf , " Bcc: " , 4 ) = = 0 ) state - > message_state = MSG_CC ;
2011-11-14 15:57:52 +01:00
else if ( strncasecmp ( buf , " Message-Id: " , 11 ) = = 0 ) state - > message_state = MSG_MESSAGE_ID ;
2012-02-08 23:14:28 +01:00
else if ( strncasecmp ( buf , " References: " , 11 ) = = 0 ) state - > message_state = MSG_REFERENCES ;
2011-11-14 15:57:52 +01:00
else if ( strncasecmp ( buf , " Subject: " , strlen ( " Subject: " ) ) = = 0 ) state - > message_state = MSG_SUBJECT ;
2012-09-03 10:06:34 +02:00
else if ( strncasecmp ( buf , " Recipient: " , strlen ( " Recipient: " ) ) = = 0 ) state - > message_state = MSG_RECIPIENT ;
2011-11-14 15:57:52 +01:00
else if ( strncasecmp ( buf , " Date: " , strlen ( " Date: " ) ) = = 0 & & sdata - > sent = = 0 ) sdata - > sent = parse_date_header ( buf ) ;
2012-09-28 10:34:04 +02:00
else if ( strncasecmp ( buf , " Received: " , strlen ( " Received: " ) ) = = 0 ) state - > message_state = MSG_RECEIVED ;
2011-11-14 15:57:52 +01:00
if ( state - > message_state = = MSG_MESSAGE_ID & & state - > message_id [ 0 ] = = 0 ) {
p = strchr ( buf + 11 , ' ' ) ;
if ( p ) p = buf + 12 ;
else p = buf + 11 ;
snprintf ( state - > message_id , SMALLBUFSIZE - 1 , " %s " , p ) ;
}
/* we are interested in only From:, To:, Subject:, Received:, Content-*: header lines */
if ( state - > message_state < = 0 ) return 0 ;
}
if ( ( p = strcasestr ( buf , " boundary " ) ) ) {
x = extract_boundary ( p , state ) ;
}
2012-09-28 21:48:20 +02:00
if ( state - > message_state = = MSG_BODY & & sdata - > ms_journal = = 1 & & strncasecmp ( buf , " Recipient: " , strlen ( " Recipient: " ) ) = = 0 ) {
2012-09-03 11:08:12 +02:00
state - > is_header = 1 ;
state - > is_1st_header = 1 ;
state - > message_state = MSG_RECIPIENT ;
}
2012-10-08 21:06:48 +02:00
if ( state - > message_state = = MSG_BODY & & sdata - > ms_journal = = 1 & & strncasecmp ( buf , " Bcc: " , 4 ) = = 0 ) {
state - > is_header = 1 ;
state - > is_1st_header = 1 ;
state - > message_state = MSG_CC ;
}
2012-09-03 10:06:34 +02:00
if ( state - > message_state = = MSG_RECIPIENT ) {
p = strstr ( buf , " Expanded: " ) ;
if ( p ) * p = ' \0 ' ;
}
2012-09-28 21:48:20 +02:00
if ( state - > message_state = = MSG_RECEIVED & & sdata - > ms_journal = = 1 & & sdata - > ms_journal_dropped = = 0 ) {
sdata - > ms_journal_dropped = 1 ;
sdata - > journal_envelope_length - = len ;
2012-09-28 10:34:04 +02:00
state - > writebufpos = 0 ; memset ( writebuffer , 0 , writebuffersize ) ;
2012-09-28 21:48:20 +02:00
memcpy ( writebuffer + state - > writebufpos , buf , strlen ( buf ) ) ; state - > writebufpos + = strlen ( buf ) ;
2012-10-01 21:09:11 +02:00
if ( t = = 2 ) {
memcpy ( writebuffer + state - > writebufpos , " \r \n " , 2 ) ; state - > writebufpos + = 2 ;
} else {
memcpy ( writebuffer + state - > writebufpos , " \n " , 1 ) ; state - > writebufpos + + ;
}
2012-09-28 10:34:04 +02:00
}
2012-02-08 23:14:28 +01:00
if ( state - > is_1st_header = = 1 & & state - > message_state = = MSG_REFERENCES ) {
if ( strncasecmp ( buf , " References: " , 11 ) = = 0 ) parse_reference ( state , buf + 11 ) ;
else parse_reference ( state , buf ) ;
}
2011-11-28 14:21:14 +01:00
if ( state - > is_1st_header = = 1 & & state - > message_state = = MSG_SUBJECT & & strlen ( state - > b_subject ) + strlen ( buf ) < MAXBUFSIZE - 1 ) {
if ( state - > b_subject [ 0 ] = = ' \0 ' ) {
2012-10-02 15:21:16 +02:00
p = & buf [ 0 ] ;
if ( strncmp ( buf , " Subject: " , strlen ( " Subject: " ) ) = = 0 ) p + = strlen ( " Subject: " ) ;
strncat ( state - > b_subject , p , MAXBUFSIZE - 1 ) ;
2011-11-28 14:21:14 +01:00
}
else {
p = strrchr ( state - > b_subject , ' ' ) ;
if ( p & & ( strcasestr ( p + 1 , " ?Q? " ) | | strcasestr ( p + 1 , " ?B? " ) ) ) {
strncat ( state - > b_subject , buf + 1 , MAXBUFSIZE - 1 ) ;
}
else strncat ( state - > b_subject , buf , MAXBUFSIZE - 1 ) ;
}
}
2012-02-07 17:21:23 +01:00
if ( state - > is_1st_header = = 1 ) {
fixupEncodedHeaderLine ( buf ) ;
}
2011-11-16 14:47:47 +01:00
2011-11-14 15:57:52 +01:00
/* Content-type: checking */
if ( state - > message_state = = MSG_CONTENT_TYPE ) {
state - > message_rfc822 = 0 ;
/* extract Content type */
p = strchr ( buf , ' : ' ) ;
if ( p ) {
p + + ;
if ( * p = = ' ' | | * p = = ' \t ' ) p + + ;
2011-11-22 12:31:54 +01:00
snprintf ( state - > type , TINYBUFSIZE - 1 , " %s " , p ) ;
2011-11-14 15:57:52 +01:00
state - > content_type_is_set = 1 ;
2011-11-22 12:31:54 +01:00
p = strchr ( state - > type , ' ; ' ) ;
2011-11-14 15:57:52 +01:00
if ( p ) * p = ' \0 ' ;
}
if ( strcasestr ( buf , " text/plain " ) | |
strcasestr ( buf , " multipart/mixed " ) | |
strcasestr ( buf , " multipart/alternative " ) | |
strcasestr ( buf , " multipart/report " ) | |
strcasestr ( buf , " message/delivery-status " ) | |
strcasestr ( buf , " text/rfc822-headers " ) | |
strcasestr ( buf , " message/rfc822 " ) | |
strcasestr ( buf , " application/ms-tnef " )
) {
2011-11-16 14:47:47 +01:00
state - > textplain = 1 ;
2011-11-14 15:57:52 +01:00
}
else if ( strcasestr ( buf , " text/html " ) ) {
2011-11-16 14:47:47 +01:00
state - > texthtml = 1 ;
2011-11-14 15:57:52 +01:00
}
2011-11-16 14:47:47 +01:00
/* switch (back) to header mode if we encounterd an attachment with "message/rfc822" content-type */
2011-11-14 15:57:52 +01:00
if ( strcasestr ( buf , " message/rfc822 " ) ) {
state - > message_rfc822 = 1 ;
state - > is_header = 1 ;
}
if ( strcasestr ( buf , " charset " ) & & strcasestr ( buf , " UTF-8 " ) ) state - > utf8 = 1 ;
2011-11-19 21:25:44 +01:00
}
2011-11-14 15:57:52 +01:00
2011-11-22 12:31:54 +01:00
if ( ( state - > message_state = = MSG_CONTENT_TYPE | | state - > message_state = = MSG_CONTENT_DISPOSITION ) & & strlen ( state - > filename ) < 5 ) {
extractNameFromHeaderLine ( buf , " name " , state - > filename ) ;
2011-11-16 14:47:47 +01:00
}
2011-11-14 15:57:52 +01:00
if ( state - > message_state = = MSG_CONTENT_TRANSFER_ENCODING ) {
2011-11-22 12:31:54 +01:00
if ( strcasestr ( buf , " base64 " ) ) state - > base64 = 1 ;
2011-11-14 15:57:52 +01:00
if ( strcasestr ( buf , " quoted-printable " ) ) state - > qp = 1 ;
}
2011-11-16 14:47:47 +01:00
2011-11-22 12:31:54 +01:00
/* boundary check, and reset variables */
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
boundary_line = is_item_on_string ( state - > boundaries , buf ) ;
2011-11-14 15:57:52 +01:00
if ( ! strstr ( buf , " boundary= " ) & & ! strstr ( buf , " boundary = " ) & & boundary_line = = 1 ) {
state - > content_type_is_set = 0 ;
2011-11-16 14:47:47 +01:00
if ( state - > has_to_dump = = 1 ) {
2012-08-21 21:57:39 +02:00
if ( take_into_pieces = = 1 & & state - > fd ! = - 1 ) {
if ( state - > abufpos > 0 ) {
2012-09-07 15:08:50 +02:00
n = write ( state - > fd , abuffer , state - > abufpos ) ;
if ( state - > b64fd ! = - 1 ) {
abuffer [ state - > abufpos ] = ' \0 ' ;
n64 = base64_decode_attachment_buffer ( abuffer , state - > abufpos , & b64buffer [ 0 ] , sizeof ( b64buffer ) ) ;
n64 = write ( state - > b64fd , b64buffer , n64 ) ;
}
state - > abufpos = 0 ; memset ( abuffer , 0 , abuffersize ) ;
2012-08-21 21:57:39 +02:00
}
close ( state - > fd ) ;
2012-09-07 15:08:50 +02:00
close ( state - > b64fd ) ;
2012-08-21 21:57:39 +02:00
}
2011-11-14 15:57:52 +01:00
state - > fd = - 1 ;
2012-09-07 15:08:50 +02:00
state - > b64fd = - 1 ;
2011-11-16 14:47:47 +01:00
}
2011-11-14 15:57:52 +01:00
2011-11-22 12:31:54 +01:00
state - > has_to_dump = 1 ;
2011-11-14 15:57:52 +01:00
state - > base64 = 0 ; state - > textplain = 0 ; state - > texthtml = state - > octetstream = 0 ;
state - > skip_html = 0 ;
state - > utf8 = 0 ;
state - > qp = 0 ;
state - > realbinary = 0 ;
2011-11-19 21:25:44 +01:00
state - > pushed_pointer = 0 ;
2011-11-22 12:31:54 +01:00
memset ( state - > filename , 0 , TINYBUFSIZE ) ;
memset ( state - > type , 0 , TINYBUFSIZE ) ;
state - > message_state = MSG_UNDEF ;
2011-11-14 15:57:52 +01:00
return 0 ;
}
if ( boundary_line = = 1 ) { return 0 ; }
/* end of boundary check */
2011-11-28 14:21:14 +01:00
/* skip irrelevant headers */
2012-09-03 10:06:34 +02:00
if ( state - > is_header = = 1 & & state - > message_state ! = MSG_FROM & & state - > message_state ! = MSG_TO & & state - > message_state ! = MSG_CC & & state - > message_state ! = MSG_RECIPIENT ) return 0 ;
2011-11-14 15:57:52 +01:00
2011-11-19 21:25:44 +01:00
/* don't process body if it's not a text or html part */
if ( state - > message_state = = MSG_BODY & & state - > textplain = = 0 & & state - > texthtml = = 0 ) return 0 ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
if ( state - > base64 = = 1 & & state - > message_state = = MSG_BODY ) {
b64_len = decodeBase64 ( buf ) ;
fixupBase64EncodedLine ( buf , state ) ;
}
2011-11-14 15:57:52 +01:00
2012-07-23 16:20:01 +02:00
/* remove all HTML tags */
2011-11-14 15:57:52 +01:00
if ( state - > texthtml = = 1 & & state - > message_state = = MSG_BODY ) markHTML ( buf , state ) ;
2011-12-07 15:24:52 +01:00
if ( state - > message_state = = MSG_BODY & & state - > qp = = 1 ) {
fixupSoftBreakInQuotedPritableLine ( buf , state ) ; // 2011.12.07
decodeQP ( buf ) ;
2011-11-14 15:57:52 +01:00
}
2012-07-23 16:20:01 +02:00
/* I believe that we can live without this function call */
//decodeURL(buf);
2011-11-14 15:57:52 +01:00
if ( state - > texthtml = = 1 ) decodeHTML ( buf ) ;
2011-12-07 15:24:52 +01:00
/* encode the body if it's not utf-8 encoded */
if ( state - > message_state = = MSG_BODY & & state - > utf8 ! = 1 ) utf8_encode ( ( unsigned char * ) buf ) ;
2011-11-14 15:57:52 +01:00
translateLine ( ( unsigned char * ) buf , state ) ;
reassembleToken ( buf ) ;
if ( state - > is_header = = 1 ) p = strchr ( buf , ' ' ) ;
else p = buf ;
2012-09-03 11:08:12 +02:00
//printf("a: %d/%d/%d/%d %s\n", state->is_1st_header, state->is_header, state->message_rfc822, state->message_state, buf);
2011-11-28 14:21:14 +01:00
2011-11-14 15:57:52 +01:00
do {
2011-11-16 14:47:47 +01:00
memset ( puf , 0 , sizeof ( puf ) ) ;
p = split ( p , ' ' , puf , sizeof ( puf ) - 1 ) ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
if ( puf [ 0 ] = = ' \0 ' ) continue ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
degenerateToken ( ( unsigned char * ) puf ) ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
if ( puf [ 0 ] = = ' \0 ' ) continue ;
2011-11-14 15:57:52 +01:00
2011-11-28 14:21:14 +01:00
strncat ( puf , " " , sizeof ( puf ) - 1 ) ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
if ( strncasecmp ( puf , " http:// " , 7 ) = = 0 | | strncasecmp ( puf , " https:// " , 8 ) = = 0 ) fixURL ( puf ) ;
2011-11-14 15:57:52 +01:00
2012-01-28 20:52:13 +01:00
if ( state - > is_header = = 0 & & strncmp ( puf , " __URL__ " , 7 ) & & ( puf [ 0 ] = = ' ' | | strlen ( puf ) > MAX_WORD_LEN | | isHexNumber ( puf ) ) ) continue ;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
len = strlen ( puf ) ;
2011-11-14 15:57:52 +01:00
2012-02-07 17:21:23 +01:00
if ( state - > message_state = = MSG_FROM & & state - > is_1st_header = = 1 & & strlen ( state - > b_from ) < SMALLBUFSIZE - len - 1 ) {
2011-11-16 14:47:47 +01:00
memcpy ( & ( state - > b_from [ strlen ( state - > b_from ) ] ) , puf , len ) ;
2011-11-14 15:57:52 +01:00
2012-02-07 17:21:23 +01:00
if ( does_it_seem_like_an_email_address ( puf ) = = 1 ) {
q = strchr ( puf , ' @ ' ) ;
2012-10-02 15:21:16 +02:00
if ( q ) {
memcpy ( & ( state - > b_from_domain [ strlen ( state - > b_from_domain ) ] ) , q + 1 , len ) ;
2012-10-03 23:32:44 +02:00
if ( strstr ( sdata - > mailfrom , " <> " ) ) {
snprintf ( sdata - > fromemail , SMALLBUFSIZE - 1 , " %s " , puf ) ;
sdata - > fromemail [ len - 1 ] = ' \0 ' ;
}
2012-10-02 15:21:16 +02:00
}
2012-02-07 17:21:23 +01:00
if ( is_email_address_on_my_domains ( puf , cfg ) = = 1 ) sdata - > internal_sender = 1 ;
2012-01-14 09:53:26 +01:00
2012-02-07 17:21:23 +01:00
if ( strlen ( state - > b_from ) < SMALLBUFSIZE - len - 1 ) {
split_email_address ( puf ) ;
memcpy ( & ( state - > b_from [ strlen ( state - > b_from ) ] ) , puf , len ) ;
}
}
2012-01-03 00:19:43 +01:00
}
2012-09-03 10:06:34 +02:00
else if ( ( state - > message_state = = MSG_TO | | state - > message_state = = MSG_CC | | state - > message_state = = MSG_RECIPIENT ) & & state - > is_1st_header = = 1 & & state - > tolen < MAXBUFSIZE - len - 1 ) {
2012-10-05 14:27:03 +02:00
strtolower ( puf ) ;
if ( state - > message_state = = MSG_RECIPIENT & & is_string_on_list ( state - > journal_recipient , puf ) = = 0 ) {
append_list ( & ( state - > journal_recipient ) , puf ) ;
memcpy ( & ( state - > b_journal_to [ state - > journaltolen ] ) , puf , len ) ;
memcpy ( & ( state - > b_journal_to [ state - > journaltolen ] ) , puf , len ) ;
if ( cfg - > verbosity > = _LOG_DEBUG ) syslog ( LOG_PRIORITY , " %s: journal rcpt: '%s' " , sdata - > ttmpfile , puf ) ;
}
2011-11-14 15:57:52 +01:00
2011-11-28 14:21:14 +01:00
if ( is_string_on_list ( state - > rcpt , puf ) = = 0 ) {
append_list ( & ( state - > rcpt ) , puf ) ;
2012-08-21 21:57:39 +02:00
memcpy ( & ( state - > b_to [ state - > tolen ] ) , puf , len ) ;
state - > tolen + = len ;
2012-01-03 00:19:43 +01:00
2012-02-07 17:21:23 +01:00
if ( does_it_seem_like_an_email_address ( puf ) = = 1 ) {
if ( is_email_address_on_my_domains ( puf , cfg ) = = 1 ) sdata - > internal_recipient = 1 ;
else sdata - > external_recipient = 1 ;
2012-01-03 00:19:43 +01:00
2012-02-07 17:21:23 +01:00
q = strchr ( puf , ' @ ' ) ;
if ( q ) {
if ( is_string_on_list ( state - > rcpt_domain , q + 1 ) = = 0 ) {
append_list ( & ( state - > rcpt_domain ) , q + 1 ) ;
memcpy ( & ( state - > b_to_domain [ strlen ( state - > b_to_domain ) ] ) , q + 1 , strlen ( q + 1 ) ) ;
}
2012-01-14 09:53:26 +01:00
}
2012-08-21 21:57:39 +02:00
if ( state - > tolen < MAXBUFSIZE - len - 1 ) {
2012-02-07 17:21:23 +01:00
split_email_address ( puf ) ;
2012-08-21 21:57:39 +02:00
memcpy ( & ( state - > b_to [ state - > tolen ] ) , puf , len ) ;
state - > tolen + = len ;
2012-02-07 17:21:23 +01:00
}
}
2011-11-28 14:21:14 +01:00
}
2012-10-05 14:27:03 +02:00
2011-11-28 14:21:14 +01:00
}
2012-08-22 08:15:53 +02:00
else if ( state - > message_state = = MSG_BODY & & len > = cfg - > min_word_len & & state - > bodylen < BIGBUFSIZE - len - 1 ) {
2012-08-21 21:57:39 +02:00
memcpy ( & ( state - > b_body [ state - > bodylen ] ) , puf , len ) ;
state - > bodylen + = len ;
}
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
} while ( p ) ;
2011-11-14 15:57:52 +01:00
return 0 ;
}