From 13b32a48b15d0fcfc3b8b9e0691a782f590e165f Mon Sep 17 00:00:00 2001 From: tyrolyean Date: Wed, 29 Apr 2020 03:00:12 +0200 Subject: [PATCH] Added boundary retrieval Signed-off-by: tyrolyean --- include/attach.h | 3 ++- include/mail.h | 2 ++ include/tools.h | 2 ++ src/attach.c | 24 ++++++++++++++++++++++-- src/tools.c | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/include/attach.h b/include/attach.h index 599fceb..4db4ac2 100644 --- a/include/attach.h +++ b/include/attach.h @@ -25,7 +25,8 @@ struct email_t mail_from_text(char* message, size_t length); void redetect_body_head(struct email_t* mail); - +void unravel_multipart_mail(struct email_t* mail, char* boundary, + size_t boundary_len); char* attach_files(char* message, size_t len); diff --git a/include/mail.h b/include/mail.h index c0ea07b..328e9fa 100644 --- a/include/mail.h +++ b/include/mail.h @@ -39,4 +39,6 @@ struct email_t{ int append_header(struct email_t* mail, const char* key, const char* value); int append_to_header(struct email_t* mail, const char* pair); +#define MULTIPART_MIME "multipart/" + #endif /* MAIL_H */ diff --git a/include/tools.h b/include/tools.h index 75c2f4e..60494eb 100644 --- a/include/tools.h +++ b/include/tools.h @@ -29,4 +29,6 @@ char* search_header_key(struct email_t* mail, const char* key); char* get_value_from_key(size_t* val_len, size_t key_offset, struct email_t* mail); +char* get_multipart_boundary(char* content_type, size_t content_len, + size_t* boundary_len); #endif /* TOOLS_H */ diff --git a/src/attach.c b/src/attach.c index de1659f..54c481a 100644 --- a/src/attach.c +++ b/src/attach.c @@ -48,8 +48,21 @@ struct email_t mail_from_text(char* message, size_t length){ char * mime_type = get_value_from_key(&value_length, cont_type - mail.message, &mail); if(mime_type != NULL){ - printf("Found message mime type: [%.*s]\n", - (int)value_length, mime_type); + if(strncasecmp(mime_type, MULTIPART_MIME, + strlen(MULTIPART_MIME)) == 0){ + /* We have multiple messages cramped inside this + * one. Let's unravel em! + */ + size_t bd_len = 0; + char* bd = get_multipart_boundary( + mime_type, value_length, &bd_len); + if(bd != NULL){ + printf("Received boundary: [%.*s]\n", + (int)bd_len, bd); + } + unravel_multipart_mail(&mail, bd, bd_len); + + } } } @@ -93,6 +106,13 @@ void redetect_body_head(struct email_t* mail){ } +void unravel_multipart_mail(struct email_t* mail, char* boundary, + size_t boundary_len){ + + return; + +} + /* Message is required to be a null terminated string, length is the mail body. * One may leave something behind the body. len is without the '\0' * Attempts to replace files inside the email with links to it on a webserver diff --git a/src/tools.c b/src/tools.c index 7657cc7..f076ddf 100644 --- a/src/tools.c +++ b/src/tools.c @@ -113,3 +113,40 @@ char* get_value_from_key(size_t* val_len, size_t key_offset, return val; } + +char* get_multipart_boundary(char* content_type, size_t content_len, + size_t* boundary_len){ + + if(content_type == NULL || boundary_len == NULL){ + return NULL; + } + + char* bd = "boundary"; + char* boundary_begin = NULL; + size_t bd_len = strlen(bd); + long boundary_offset = -1; + + for(size_t i = 0; i < (content_len - bd_len);i++){ + if(strncasecmp(&content_type[i], bd, bd_len) == 0){ + boundary_offset = i+bd_len; + break; + } + } + + if(boundary_offset < 0){ + *boundary_len = 0; + return NULL; + } + + for(size_t i = boundary_offset; i < content_len;i++){ + if(content_type[i] == '"' && boundary_begin == NULL){ + boundary_begin = &content_type[i+1]; + }else if(content_type[i] == '"'){ + *boundary_len = &content_type[i] - boundary_begin ; + return boundary_begin; + } + } + + *boundary_len = 0; + return NULL; +}