-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.cpp
More file actions
170 lines (146 loc) · 4.81 KB
/
mail.cpp
File metadata and controls
170 lines (146 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details at
http://www.gnu.org/copyleft/gpl.html
AUTOR: Ángel Luis Perales Gómez
*/
#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
using namespace std;
int sock; /*Socket inseguro*/
SSL *ssl; /*Socket seguro*/
SSL_CTX *ctx;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char buf[BUFSIZ+1];
int len;
//char *host_id="65.55.172.254"; //hotmail también puede ser "smtp.live.com"
char *host_id="smtp.gmail.com"; //gmail
//char *host_id="smtp.um.es"; //Universidad de Murcia
//TODO
//Implementar métodos de encode y decode en base64
char *from_id="AQUI EL CORREO EN BASE64";
char *password="AQUI LA PASSWORD EN BASE64";
char *to_id="CORREO DEL DESTINATARIO sin base64";
char *asunto="Prueba\r\n";
char cuerpo[100]="CORREO\r\n";
/*=====Enviar una cadena al socket=====*/
void send_socket(char *s)
{
write(sock,s,strlen(s));
write(1,s,strlen(s));
}
/*=====Enviar una cadena al socket cifrado=====*/
void send_SSLsocket(char *s)
{
write(1,s,strlen(s));
SSL_write(ssl,s,strlen(s));
}
//=====Leer una cadena desde el socket=====*/
void read_socket()
{
len = read(sock,buf,BUFSIZ);
write(1,buf,len);
}
//=====Leer una cadena desde el socket cifrado=====*/
void read_SSLsocket()
{
len = SSL_read(ssl,buf,BUFSIZ);
write(1,buf,len);
}
/*=====MAIN=====*/
int main(int argc, char* argv[])
{
/*====Variables necesarias para la conexión SSL=====*/
SSL_library_init ();
SSL_load_error_strings();
ctx=SSL_CTX_new(SSLv3_client_method());
ssl = SSL_new(ctx);
/*=====Creamos el socket=====*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock==-1)
{
perror("No se puede crear al socket");
exit(1);
}
else
cout << "socket creado\n";
/*=====Verificamos el host=====*/
server.sin_family = AF_INET;
hp = gethostbyname(host_id);
if (hp==(struct hostent *) 0)
{
fprintf(stderr, "%s: host desconocido\n", host_id);
exit(2);
}
/*=====Conectamos con el puerto SMTP (25)=====*/
cout << "Host ID: " << host_id << "\n";
//cout << "IP: " << inet_ntoa(hp->h_addr) << "\n";
memcpy((char *) &(server.sin_addr), (char *) hp->h_addr, hp->h_length);
server.sin_port=htons(25); /* SMTP PORT */
if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1)
{
perror("conectado socket");
exit(1);
}
else
cout << "Conectado!\n";
//PROTOCOLO SMTP http://es.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
//Autenticación en SMTP http://www.fehcom.de/qmail/smtpauth.html
read_socket(); /* Primero se comunica con nosotros el Servidor */
send_socket("EHLO\r\n"); /* Le respondemos */
read_socket(); /*Leemos su respuesta */
send_socket("STARTTLS"); /*Iniciamos el TLS */
send_socket("\r\n");
read_socket();
SSL_set_fd(ssl, sock); /*Asociamos el socket a la conexión ssl*/
SSL_connect(ssl);
send_SSLsocket("EHLO\r\n"); /*Al estar sobre conexión segura tenemos que empezar la comunicación desde 0*/
read_SSLsocket();
send_SSLsocket("AUTH LOGIN\r\n");
read_SSLsocket(); /*Aquí el servidor nos devolverá "username: " cifrado en base64*/
send_SSLsocket("%s\r\n",from_id);
read_SSLsocket(); /*Aquí el servidor nos devolverá "password: " cifrado en base64*/
send_SSLsocket("%s\r\n",password);
read_SSLsocket(); /*Si todo ha ido correcto nos autenticaremos en el servidor SMTP*/
send_SSLsocket("MAIL FROM: <"); /*Correo de*/
send_SSLsocket(from_id);
send_SSLsocket(">\r\n");
read_SSLsocket(); /* Todo OK */
/*send_SSLsocket("VRFY "); ¿OBSOLETO?
send_SSLsocket(from_id);
send_SSLsocket("\r\n");
read_SSLsocket(); // Todo OK */
send_SSLsocket("RCPT TO: <"); /*Correo para */
send_SSLsocket(to_id);
send_SSLsocket(">\r\n");
read_SSLsocket(); // Remitente OK */
send_SSLsocket("DATA\r\n");// Cuerpo del mensaje */
read_SSLsocket();
send_SSLsocket("Subject: ");
send_SSLsocket(asunto);
//read_SSLsocket(); // Remitente OK*/
send_SSLsocket(cuerpo);
send_SSLsocket(".\r\n");
read_SSLsocket();
send_SSLsocket("QUIT\r\n"); /* Salimos */
read_SSLsocket(); // Salimos */
//=====Salimos y finalizamos=====*/
close(sock);
exit(0);
}