// s0t4ipv6@shellcode.com.ar
// cl0ak-mailbox noversion
//

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>			// Seed for randomize mail
#include <ctype.h>

#define LEN_name        30 + 1  	// @
#define LEN_domain      25      	// without dot com dot ar
#define DEFAULT_PORT	25 		// SMTP on a different port?
#define	SMTP_SERVER	"mothership"	// Define your hax0r relay

volatile int chk=0;

char *_randomail() {
        int     complete=0;
        int     where;
        char    *email;

        // Set seed
        srand( (unsigned int)time(NULL) );
        email= (char *)calloc(LEN_name + LEN_domain + 7, sizeof(char));

        while (complete != LEN_name + LEN_domain){
                where = (int ) (rand() % 127) ;
                if (isxdigit(where)){
                        memcpy(email+complete, (char *)&where, 1);
                        // i need a fancy snail
                        if ( complete == LEN_name )
                                { complete++; *(email+complete)='@'; }
                        complete++;
                        }
        }
        // Now... my dotcomdotar
        memcpy(email+complete, ".com.ar", 7);
	return(email);
}

_body(int sckt, int file, char *namefile) {
	char 	*content,
		quit[]=".\r\nquit\r\n";
	int 	charge;

	if ( (charge=lseek(file,SEEK_SET,SEEK_END)) <= 0) 
		{ perror("lseek()"); exit(0); }

	if ( (content=(char*)calloc(charge, sizeof(char)) ) == NULL )	// I like the redundance...
		{ perror("calloc()"); exit(0); }
	
	lseek(file,0,SEEK_SET);

	if ( read(file,content,charge) > 0 ) 
		{ fprintf(stderr," -><content of file %s>", namefile); }
		 else 
	 	 { fprintf (stderr," -><file %s empty>", namefile); }

	if ( (send(sckt,content,charge,0)) < 0)
		{ perror("send()"); exit(0); }

	fprintf (stderr,"\n ->.\n ->quit\n");

	if ( (send(sckt,quit,strlen(quit),0)) < 0)
		{ perror("send()"); exit(0); }

}

_seqwrite (int y, int socket, char *from, char *rcpt,  char *subj) {
	char	*array[6], *seend;
	int	zise=0;

	array[0]="helo cl0aking\n";
	array[1]="mail from: <%s>\n";
	array[2]="rcpt to: <%s>\n";
	array[3]="data\n";
	array[4]="SUBJECT: %s\n";
	zise=strlen(array[y]);
	seend=array[y];

	switch(y) {
		case 1:	
		 zise+=strlen(from);
		 if ( (seend=(char*)calloc(zise + 1, sizeof(char))) == NULL )	// ... continue
			{ perror("calloc()"); exit(0); } 
		 snprintf(seend,zise, array[y], from);
		 break;
		case 2: 
		 zise+=strlen(rcpt);
		 if ( (seend=(char*)calloc(zise + 1, sizeof(char))) == NULL )
			{ perror("calloc()"); exit(0); } 
		 snprintf(seend,zise, array[y], rcpt);
		 break;
		case 4: 
		 zise+=strlen(subj);
		 if ( (seend=(char*)calloc(zise + 1, sizeof(char))) == NULL )
			{ perror("calloc()"); exit(0); } 
		 snprintf(seend,zise, array[y], subj);
		 break;
	}

	fprintf(stderr,"\n ->%s",seend);
	if ( (send(socket, seend ,strlen(seend),0)) < 0)
		{ perror("send()"); exit(0); }
	sleep(1);
	chk++;

}

main(int argc, char *argv[]) {
int 	port=DEFAULT_PORT, 
	fd, x=0,
	socketfd;
char 	result,
	*IPaddr,
	*destmail, *srcmail, *filemsg,
	*subject="Cl0ak-mailbox";

struct sockaddr_in 	meta;
struct hostent 		*addr;

if (argc < 4)
 {
  fprintf (stdout, "cl0ak-mailbox noversion -- by s0t4ipv6 <s0t4ipv6@shellcode.com.ar>\n", argv[0]);
  fprintf (stdout, "Usage: %s <src_mail>|<random> <dst_mail> [subject] <msg_textfile>\n", argv[0]);
  exit (-1);
 }

// check fucking args...
if ( (destmail=(char*)malloc(strlen(argv[2]))) == NULL || 
     (srcmail= (char*)malloc(strlen(argv[1]))) == NULL )
	{ perror("malloc()"); exit(0); }

destmail=argv[2]; srcmail=argv[1];
if (argv[4]) { 
	if ( (filemsg=(char*)malloc(strlen(argv[4]))) == NULL )
		{ perror("malloc()"); exit(0); }
	if ( (subject=(char*)malloc(strlen(argv[3]))) == NULL )
		{ perror("malloc()"); exit(0); }
	filemsg=argv[4]; subject=argv[3];
	} else {
		if ( (filemsg=(char*)malloc(strlen(argv[3]))) == NULL )
			{ perror("malloc()"); exit(0); }
		filemsg=argv[3];
	}

if ( (fd=open (filemsg,O_RDONLY)) < 0 )
	{ perror ("open()"); exit(0);}
// Condition RAndomiZe source email here
if ( strncmp(srcmail, "random", 6) == 0) { srcmail=_randomail(); }

IPaddr=SMTP_SERVER;
if ((addr=gethostbyname(IPaddr))==NULL) { perror("Gethostbyname()"); exit(0); }	

socketfd= socket(PF_INET,SOCK_STREAM, 0);
if (!socketfd) { perror ("socket()"); exit(0); }

meta.sin_family=PF_INET;
meta.sin_port=htons(port);
meta.sin_addr=*((struct in_addr*)addr->h_addr);

fprintf (stdout,"Cl0acking mailbox %s...\n",destmail);
fprintf (stdout," [...]\n\n");

if (connect (socketfd, (struct sockaddr *) &meta, sizeof (meta)) < 0)
    { perror("Connect()"); exit (0); }
	while (read(socketfd, &result, 1)) {
		write(1,&result,1);
		if ( ((int)result == 13) ) { 
			// alarm here
			read (socketfd, &result, 1); 
			_seqwrite(x, socketfd, srcmail, destmail, subject); 
			x++; 
			if (chk == 5) { 
				_body(socketfd,fd,filemsg); 
				close(socketfd);
				close(fd);
				fprintf (stdout,"\n [...]\nSucessfull...\n");
				exit(0);
				}
			}
	
	}
}	// main crash....
	// EOF

