Compare commits

...

2 commits

Author SHA1 Message Date
fdc2ddf53b
Added ability to announce presence
Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
2020-04-29 00:32:25 +02:00
0524358cef
Added instance id option to mark messages
Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
2020-04-29 00:08:34 +02:00
11 changed files with 154 additions and 13 deletions

View file

@ -21,15 +21,7 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
struct email_t{ #include "mail.h"
char* message;
/* From the below values you can say pretty much anything about the
* message. The line delimiting body and header is left out. The header
* len includes the last \r\n of the header. If header len is zero,
* there was no clear distinction between header and body...
*/
size_t header_len, body_offset, message_length;
};
struct email_t mail_from_text(char* message, size_t length); struct email_t mail_from_text(char* message, size_t length);
void redetect_body_head(struct email_t* mail); void redetect_body_head(struct email_t* mail);

View file

@ -26,4 +26,6 @@ extern uint16_t listen_port, forward_port;
extern int abort_on_pgp, abort_on_dkim; extern int abort_on_pgp, abort_on_dkim;
extern char* instance_id;
#endif /* CONFIG_H */ #endif /* CONFIG_H */

37
include/mail.h Normal file
View file

@ -0,0 +1,37 @@
/*
* mail.h - Mail editing toolset
* The author licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef MAIL_H
#define MAIL_H
#include <stdint.h>
#include <stddef.h>
struct email_t{
char* message;
/* From the below values you can say pretty much anything about the
* message. The line delimiting body and header is left out. The header
* len includes the last \r\n of the header. If header len is zero,
* there was no clear distinction between header and body...
*/
size_t header_len, body_offset, message_length;
};
int append_header(struct email_t* mail, const char* key, const char* value);
int append_to_header(struct email_t* mail, const char* pair);
#endif /* MAIL_H */

View file

@ -20,7 +20,7 @@
#include <stddef.h> #include <stddef.h>
const char* insert_string(char * destination, const char* source, char* insert_string(char * destination, const char* source,
size_t dest_orig_len, size_t offset); size_t dest_orig_len, size_t offset);

View file

@ -19,6 +19,7 @@
#include "attach.h" #include "attach.h"
#include "detect.h" #include "detect.h"
#include "config.h" #include "config.h"
#include "mail.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -94,6 +95,10 @@ char* attach_files(char* message, size_t len){
return email.message; return email.message;
} }
/* Now we can start the real work! */
/* Announce our presence via header */
append_header(&email,"X-Mailattached", instance_id);
return email.message; return email.message;
} }

View file

@ -14,7 +14,11 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
#include "config.h" #include "config.h"
#include <stddef.h>
uint16_t listen_port = 4269, forward_port = 4270; uint16_t listen_port = 4269, forward_port = 4270;
int abort_on_pgp = true, abort_on_dkim = true; int abort_on_pgp = true, abort_on_dkim = true;
char* instance_id = NULL;

View file

@ -58,6 +58,7 @@ bool detect_dkim(struct email_t* mail){
if(strcasestr(mail->message, dkim_signatures[i]) != NULL if(strcasestr(mail->message, dkim_signatures[i]) != NULL
&& strcasestr(mail->message, dkim_signatures[i]) && strcasestr(mail->message, dkim_signatures[i])
<= (mail->message+mail->header_len)){ <= (mail->message+mail->header_len)){
/* DKIM only happens inside the header, not the body! */
points++; points++;
} }

77
src/mail.c Normal file
View file

@ -0,0 +1,77 @@
/*
* mail.c - Mail editing toolset
* The author licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "mail.h"
#include "tools.h"
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
int append_header(struct email_t* mail, const char* key, const char* value){
if(mail == NULL || key == NULL || value == NULL){
return -1;
}
const char* in_between = ": ";
size_t len = strlen(key) + strlen(value) + strlen(in_between) + 1;
char* buffer = malloc(len);
if(buffer == NULL){
return -1;
}
memset(buffer, 0, len);
strcat(buffer, key);
strcat(buffer, in_between);
strcat(buffer, value);
if(append_to_header(mail, buffer) < 0){
free(buffer);
return -1;
}
free(buffer);
return 0;
}
int append_to_header(struct email_t* mail, const char* pair){
if(pair == NULL || mail == NULL){
return -1;
}
char *buffer = malloc(strlen(pair) + 3);
if(buffer == NULL){
return -1;
}
memset(buffer, 0, strlen(pair) + 3);
buffer[0] = '\r';
buffer[1] = '\n';
strcat(buffer, pair);
mail->message=insert_string(mail->message, buffer, strlen(buffer),
mail->header_len);
free(buffer);
return 0;
}

View file

@ -7,6 +7,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "network.h" #include "network.h"
#include "config.h" #include "config.h"
@ -27,12 +30,13 @@ int main(int argc, char* argv[]){
{"noabort-dkim",no_argument, &abort_on_dkim,0}, {"noabort-dkim",no_argument, &abort_on_dkim,0},
{"in-port", required_argument, 0, 'i'}, {"in-port", required_argument, 0, 'i'},
{"out-port", required_argument, 0, 'o'}, {"out-port", required_argument, 0, 'o'},
{"instance-id", required_argument, 0, 'n'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
/* getopt_long stores the option index here. */ /* getopt_long stores the option index here. */
int option_index = 0; int option_index = 0;
c = getopt_long (argc, argv, "i:o:pd", c = getopt_long (argc, argv, "n:i:o:pd",
long_options, &option_index); long_options, &option_index);
/* Detect the end of the options. */ /* Detect the end of the options. */
@ -51,6 +55,10 @@ int main(int argc, char* argv[]){
forward_port = atoi(optarg); forward_port = atoi(optarg);
break; break;
case 'n':
instance_id = optarg;
break;
case '?': case '?':
/* getopt_long already printed an error message. */ /* getopt_long already printed an error message. */
return EXIT_FAILURE; return EXIT_FAILURE;
@ -61,6 +69,17 @@ int main(int argc, char* argv[]){
} }
} }
/* If not already specified, populate the instance ID with the sytem
* hostname.
*/
if(instance_id == NULL){
instance_id = malloc(HOST_NAME_MAX + 1);
memset(instance_id, 0, HOST_NAME_MAX + 1);
if(gethostname(instance_id, HOST_NAME_MAX) < 0){
perror("gethostname failed! set instance id manually");
}
}
printf("Incoming port: %u outgoing port: %u on loopback interface\n", printf("Incoming port: %u outgoing port: %u on loopback interface\n",
listen_port, forward_port); listen_port, forward_port);
@ -70,6 +89,9 @@ int main(int argc, char* argv[]){
printf("Ignoring DKIM signed messages: %s\n", printf("Ignoring DKIM signed messages: %s\n",
abort_on_dkim ? "true" : "false"); abort_on_dkim ? "true" : "false");
printf("Instance id for messages: %s\n",
instance_id);
if(init_net() < 0){ if(init_net() < 0){
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View file

@ -248,7 +248,8 @@ int init_net(){
serveraddr.sin_port = htons((unsigned short)listen_port); serveraddr.sin_port = htons((unsigned short)listen_port);
if (bind(parentfd, (struct sockaddr *) &serveraddr, if (bind(parentfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0) { sizeof(serveraddr)) < 0) {
perror("ERROR on binding"); perror("ERROR on binding");
return -1; return -1;
} }

View file

@ -24,7 +24,7 @@
/* Takes a string destination and inserts at the given point offset the string /* Takes a string destination and inserts at the given point offset the string
* in source. Really more like a stringcat with shifting the end. * in source. Really more like a stringcat with shifting the end.
* dest_orig_len is without '\0'*/ * dest_orig_len is without '\0'*/
const char* insert_string(char * destination, const char* source, char* insert_string(char * destination, const char* source,
size_t dest_orig_len, size_t offset){ size_t dest_orig_len, size_t offset){
if(source == NULL|| dest_orig_len < offset){ if(source == NULL|| dest_orig_len < offset){