Compare commits
2 commits
1312510074
...
fdc2ddf53b
Author | SHA1 | Date | |
---|---|---|---|
fdc2ddf53b | |||
0524358cef |
11 changed files with 154 additions and 13 deletions
|
@ -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);
|
||||||
|
|
|
@ -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
37
include/mail.h
Normal 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 */
|
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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
77
src/mail.c
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
24
src/main.c
24
src/main.c
|
@ -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. */
|
||||||
|
@ -50,6 +54,10 @@ int main(int argc, char* argv[]){
|
||||||
case 'o':
|
case 'o':
|
||||||
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. */
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -69,6 +88,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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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){
|
||||||
|
|
Loading…
Reference in a new issue