ill mail the whole program to uall
Actually it is a milter which decrypts the incoming mail messages...
<br />
<br />
<br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <unistd.h><br />
#include <string.h><br />
#include <pthread.h><br />
#include <syslog.h><br />
#include <sys/types.h><br />
#include <sys/socket.h><br />
#include <netinet/in.h><br />
#include <arpa/inet.h><br />
#include <sys/stat.h><br />
#include <fcntl.h><br />
<br />
#include <glib.h><br />
#include <gmime/gmime.h><br />
<br />
#include "libmilter/mfapi.h"<br />
<br />
#include "gmp.h"<br />
<br />
<br />
<br />
<br />
static void usage( void );<br />
static sfsistat sample_connect( SMFICTX* ctx, char* connhost, _SOCK_ADDR* connaddr );<br />
static sfsistat sample_helo( SMFICTX* ctx, char* helohost );<br />
static sfsistat sample_envfrom( SMFICTX* ctx, char** fromargs );<br />
static sfsistat sample_envrcpt( SMFICTX* ctx, char** rcptargs );<br />
static sfsistat sample_header( SMFICTX* ctx, char* name, char* value );<br />
static sfsistat sample_eoh( SMFICTX* ctx );<br />
static sfsistat sample_body( SMFICTX* ctx, unsigned char* bytes, size_t len );<br />
static sfsistat sample_eom( SMFICTX* ctx );<br />
static sfsistat sample_abort( SMFICTX* ctx );<br />
static sfsistat sample_close( SMFICTX* ctx );<br />
static void thread_data_destructor( void* data );<br />
<br />
int smfi_replacebody(<br />
SMFICTX *ctx,<br />
unsigned char *bodyp,<br />
int bodylen<br />
);<br />
<br />
<br />
int smfi_chgheader(<br />
SMFICTX *ctx,<br />
char *name,<br />
int hdridx,<br />
char *value<br />
);<br />
<br />
<br />
<br />
<br />
static char* argv0;<br />
static pthread_key_t key;<br />
static int connections;<br />
static char* data_buffer, *contentTypeBuffer;<br />
void *ret;
size_t size;<br />
int flag=0;<br />
<br />
GMimeMessage *message;<br />
GMimeStream *stream;<br />
GMimeParser *parser;<br />
GMimeMultipart *multipart;<br />
GMimePart *mime_part,*mime_part0;<br />
GMimeContentType *contentType,*contentType1;<br />
<br />
static struct smfiDesc smfilter =<br />
{<br />
"SAMPLE", <br />
SMFI_VERSION, <br />
SMFIF_CHGBODY | SMFIF_CHGHDRS, <br />
sample_connect, <br />
sample_helo, <br />
sample_envfrom, <br />
sample_envrcpt, <br />
sample_header, <br />
sample_eoh, <br />
sample_body, <br />
sample_eom, <br />
sample_abort, <br />
sample_close <br />
};<br />
<br />
<br />
int<br />
main( int argc, char** argv )<br />
{<br />
char* sockarg;<br />
<br />
<br />
<br />
g_mime_init (0);<br />
<br />
message = g_mime_message_new(FALSE);<br />
<br />
<br />
argv0 = strrchr( argv[0], '/' );<br />
if ( argv0 != (char*) 0 )<br />
++argv0;<br />
else<br />
argv0 = argv[0];<br />
<br />
connections = 0;<br />
<br />
if ( argc != 2 )<br />
usage();<br />
sockarg = argv[1];<br />
<br />
if ( pthread_key_create( &key, &thread_data_destructor ) )<br />
{<br />
perror( "pthread_key_create" );<br />
exit( 1 );<br />
}<br />
<br />
openlog( argv0, 0, LOG_MAIL );<br />
<br />
(void) smfi_setconn( sockarg );<br />
if ( smfi_register( smfilter ) == MI_FAILURE )<br />
{<br />
(void) fprintf( stderr, "%s: register failed\n", argv0 );<br />
exit( 1 );<br />
}<br />
if ( smfi_main() == MI_FAILURE )<br />
{<br />
(void) fprintf( stderr, "%s: milter failed\n", argv0 );<br />
exit( 1 );<br />
}<br />
<br />
syslog( LOG_INFO, "exitting - %d connections", connections );<br />
(void) pthread_key_delete( key );<br />
closelog();<br />
<br />
<br />
exit( 0 );<br />
}<br />
<br />
<br />
static void<br />
usage( void )<br />
{<br />
(void) fprintf( stderr, "usage: %s socket\n", argv0 );<br />
exit( 1 );<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
struct thread_data {<br />
int junk;<br />
};<br />
<br />
<br />
struct connection_data {<br />
int connection_num;<br />
int messages;<br />
int recipients;<br />
int headers;<br />
int bodyblocks;<br />
size_t bodybytes;<br />
int aborts;<br />
};<br />
<br />
<br />
<br />
static sfsistat<br />
sample_connect( SMFICTX* ctx, char* connhost, _SOCK_ADDR* connaddr )<br />
{<br />
struct thread_data* td;<br />
struct connection_data* cd;<br />
struct sockaddr_in* sin;<br />
<br />
td = (struct thread_data*) pthread_getspecific( key );<br />
if ( td == (struct thread_data*) 0 )<br />
{<br />
td = (struct thread_data*) malloc( sizeof(struct thread_data) );<br />
if ( td == (struct thread_data*) 0 )<br />
return SMFIS_TEMPFAIL;<br />
if ( pthread_setspecific( key, (void*) td ) )<br />
{<br />
free( (void*) td );<br />
return SMFIS_TEMPFAIL;<br />
}<br />
}<br />
<br />
cd = (struct connection_data*) malloc( sizeof(struct connection_data) );<br />
if ( cd == (struct connection_data*) 0 )<br />
return SMFIS_TEMPFAIL;<br />
(void) smfi_setpriv( ctx, (void*) cd );<br />
cd->connection_num = ++connections;<br />
<br />
sin = (struct sockaddr_in*) connaddr;<br />
syslog( LOG_INFO, "conn# %d - sample_connect( \"%s\" [%s] )", cd->connection_num, connhost, inet_ntoa( sin->sin_addr ) );<br />
<br />
cd->messages = 0;<br />
cd->aborts = 0;<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_helo( SMFICTX* ctx, char* helohost )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_helo( \"%s\" )", cd->connection_num, helohost );<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_envfrom( SMFICTX* ctx, char** fromargs )<br />
{<br />
data_buffer=(char*)malloc(100000000);<br />
<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_envfrom( \"%s\" )", cd->connection_num, fromargs[0] );<br />
<br />
<br />
++cd->messages;<br />
cd->recipients = 0;<br />
cd->headers = 0;<br />
cd->bodyblocks = 0;<br />
cd->bodybytes = 0;<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_envrcpt( SMFICTX* ctx, char** rcptargs )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_envrcpt( \"%s\" )", cd->connection_num, rcptargs[0] );<br />
<br />
++cd->recipients;<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_header( SMFICTX* ctx, char* name, char* value )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_header( \"%s\", \"%s\" )", cd->connection_num, name, value );<br />
<br />
++cd->headers;<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_eoh( SMFICTX* ctx )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_eoh()", cd->connection_num );<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_body( SMFICTX* ctx, unsigned char* bytes, size_t len )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
<br />
strncat(data_buffer, bytes, len);<br />
<br />
++cd->bodyblocks;<br />
cd->bodybytes += len;<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_eom( SMFICTX* ctx )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_eom(), %d recipients, %d headers, %d bodyblocks with %d bytes", cd->connection_num, cd->recipients, cd->headers, cd->bodyblocks, (int) cd->bodybytes );<br />
<br />
<br />
<br />
<br />
<br />
<br />
stream = g_mime_stream_mem_new_with_buffer (data_buffer, strlen(data_buffer) );<br />
<br />
<br />
parser = g_mime_parser_new_with_stream (stream);<br />
<br />
<br />
g_object_unref (stream);<br />
<br />
<br />
message = g_mime_parser_construct_message (parser);<br />
<br />
<br />
g_object_unref (parser);<br />
<br />
<br />
gboolean *is_html;<br />
const char *content = g_mime_message_get_body ( message , FALSE , is_html);<br />
strncpy(data_buffer,content,strlen(content));<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
syslog( LOG_INFO, "conn# %d - body = %s", cd->connection_num, content );<br />
<br />
int noOfBlocks = strlen(data_buffer)/308;<br />
syslog( LOG_INFO, "conn# %d - body = %d no of blocks = %d", cd->connection_num, strlen(data_buffer), noOfBlocks );<br />
<br />
mpz_t bigbody,encbody,decbody,n,d,e;<br />
<br />
<br />
<br />
mpz_init_set_str (e, "5", 10);<br />
<br />
mpz_init_set_str (n, "81072118814004638617775947138239917744589505176471539666269773100315781097546894455560658571445286325125766632676152452856383447352821337178984474781813475276075048299705214136395672502245955793627681061360075723595075874516950614187016991430293887968216015097675955399134514678660046996064181098182893072761", 10);<br />
<br />
mpz_init_set_str (d, "32428847525601855447110378855295967097835802070588615866507909240126312439018757782224263428578114530050306653070460981142553378941128534871593789912725382636961637010791821646038659077539848411532121133465246631106852802485583932352673149866783600963924490576147444236696494852521901563664557493382382127469", 10);<br />
<br />
<br />
<br />
char *STR;<br />
STR=(char*)malloc(1000);<br />
<br />
<br />
mpz_t array[10];<br />
mpz_array_init (array, 10, 308);<br />
mpz_t array1[10];<br />
mpz_array_init (array1, 10, 100000);<br />
<br />
char substring[400];<br />
int i=0,lower=0,upper=308;<br />
while(i<noOfBlocks)<br />
{ <br />
<br />
<br />
<br />
<br />
syslog( LOG_INFO, "conn# %d - i = %d", cd->connection_num, i );<br />
strncpy(substring, data_buffer + lower, upper);<br />
*(substring+308)='\0';<br />
syslog( LOG_INFO, "conn# %d - substring = %s", cd->connection_num, substring );<br />
lower=upper;<br />
upper=upper+308;<br />
<br />
<br />
<br />
<br />
<br />
mpz_init_set_str(array[i],substring,10);<br />
<br />
<br />
<br />
<br />
mpz_get_str (STR, 10, array[i]);<br />
strcat(STR,"\0");<br />
syslog( LOG_INFO, "conn# %d - Encrypted-Biginteger = %s", cd->connection_num, STR );<br />
<br />
<br />
<br />
mpz_init(array1[i]);<br />
mpz_powm (array1[i], array[i], d, n);<br />
<br />
<br />
mpz_get_str (STR, 10, array1[i]);<br />
syslog( LOG_INFO, "conn# %d - Decrypted-Biginteger = %s", cd->connection_num, STR );<br />
<br />
size_t length;<br />
void *ret;<br />
char *decrypted;<br />
decrypted=(char*)malloc(1000);<br />
<br />
ret=mpz_export (decrypted, &length, 1,sizeof(data_buffer[0]), 0, 0, array1[i]);<br />
<br />
strncpy(decrypted,ret,length);<br />
*(decrypted+length)='\0';<br />
<br />
<br />
syslog( LOG_INFO, "conn# %d - size decrypted = %d Decrypted = %s", cd->connection_num,length, (char *)ret );<br />
fflush(stdout);<br />
<br />
free(decrypted);<br />
fflush(stdin);<br />
fflush(stdout);<br />
i++;<br />
<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_abort( SMFICTX* ctx )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_abort(), %d recipients, %d headers, %d bodyblocks with %d bytes", cd->connection_num, cd->recipients, cd->headers, cd->bodyblocks, (int) cd->bodybytes );<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
<br />
static sfsistat<br />
sample_close( SMFICTX* ctx )<br />
{<br />
struct connection_data* cd = (struct connection_data*) smfi_getpriv( ctx );<br />
<br />
syslog( LOG_INFO, "conn# %d - sample_close(), %d messages", cd->connection_num, cd->messages );<br />
<br />
if ( cd != (struct connection_data*) 0 )<br />
{<br />
(void) smfi_setpriv( ctx, (void*) 0 );<br />
free( (void*) cd );<br />
}<br />
<br />
return SMFIS_CONTINUE;<br />
}<br />
<br />
<br />
static void<br />
thread_data_destructor( void* data )<br />
{<br />
struct thread_data* td = (struct thread_data*) data;<br />
<br />
free( (void*) td );<br />
}
|