postfix content filter poc

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

postfix content filter poc

Gökhan Alkan

I try to develop poc code for postfix content filtering. All i need is read the 10025/tcp and send all data which i can read to 10026/tcp. Below is my poc code. But i try to send email it doesn't work and postfix show me "451 4.3.0 Error: queue file write error".


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>


#define PORT 10025
#define REMOTE_PORT 10026
#define REMOTE_IP "0.0.0.0"


int main(int argc, const char **argv)
{

        int addr_len, result, yes = 1;
        int server_sock = 0, client_sock = 0, sock_fd = 0;
        char buff[1025];

        struct sockaddr_in serv_addr, client_addr, remote_addr;


        if ((server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
                perror("socket");
                exit(EXIT_FAILURE);
        }

        if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { 
                perror("setsockopt"); 
                exit(1); 
        }  

        memset(buff, 0x0, sizeof(buff));
        memset(&serv_addr, 0x0, sizeof(serv_addr));
        memset(&client_addr, 0x0, sizeof(client_addr));
        memset(&remote_addr, 0x0, sizeof(remote_addr));

        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(PORT);
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

        remote_addr.sin_family = AF_INET;
        remote_addr.sin_port = htons(REMOTE_PORT); 


        if ((bind(server_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) {
                perror("bind");
                exit(EXIT_FAILURE);
        }

        if (listen(server_sock, 8) < 0) {
                perror("listen");
                exit(EXIT_FAILURE);     
        }       

        fcntl(server_sock, F_SETFL, O_NONBLOCK);
        fcntl(client_sock, F_SETFL, O_NONBLOCK); 

        addr_len = sizeof(client_addr);
        client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addr_len);
        sock_fd = socket(AF_INET, SOCK_STREAM, 0);      

        while(1) {
                result = recv(client_sock, buff, sizeof(buff),0 );
                inet_pton(AF_INET, REMOTE_IP, &remote_addr.sin_addr);
                connect(sock_fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
                send(sock_fd, buff, (sizeof(buff)-1), 0);
        }


        shutdown(client_sock, SHUT_RDWR);

        close(server_sock);
        close(client_sock);
        close(sock_fd);

        return 0;
}

And here is my postfix master.cf configuration.

smtp inet n - n - 20 smtpd -o smtpd_proxy_filter=0.0.0.0:10025 -o smtpd_client_connection_count_limit=10

0.0.0.0:10026 inet n - n - - smtpd -o smtpd_authorized_xforward_hosts=127.0.0.0/8 -o smtpd_client_restrictions= -o smtpd_helo_restrictions= -o smtpd_sender_restrictions= -o smtpd_recipient_restrictions=permit_mynetworks,reject -o smtpd_data_restrictions= -o mynetworks=127.0.0.0/8 -o receive_override_options=no_unknown_recipient_checks


Best regards