forked from ritwik12/Virtual-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl.c
408 lines (358 loc) · 13.1 KB
/
ssl.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include "decode.c"
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 4096
#endif
void display_header(SSL* ssl, int n){
char resp[BUFFER_SIZE]="";
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char from[BUFFER_SIZE]="";
char subject[BUFFER_SIZE]="";
char date[BUFFER_SIZE]="";
char* temp_num = calloc(4, sizeof(char));
char* temp = calloc(BUFFER_SIZE, sizeof(char));
sprintf(temp_num, "%d", n);
printf(" N°: %s ", temp_num);
strcpy(buff, "from fetch ");
strcat(buff, temp_num);
strcat(buff, " (body[header.fields (from date subject)])\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
while(strcmp(temp,"from OK Success") != 0){
SSL_read(ssl,resp_buff, sizeof (resp_buff));
strcat(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
}
temp =strtok(resp, "\r\n");
for(int i = 0; i < 3; i++)
{
temp =strtok(NULL, "\r\n");
if(temp[0]=='F') strcpy(from, temp);
else if(temp[0]=='S') strcpy(subject, temp);
else if(temp[0]=='D'){
strncpy(date, temp, 31);
}
}
printf("%s\n",from);
printf(" %s\n",subject);
printf(" %s \n",date);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "str store ");
strcat(buff, temp_num);
strcat(buff, " flags unseen\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff)); //fetch
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
}
int display_list(SSL *ssl){
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char* temp = calloc(BUFFER_SIZE, sizeof(char));
int* new_messages = calloc(100,sizeof(int));
int total_unseen = 0;
strcpy(buff, "sel select inbox\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "sea search (unseen)\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
int i = 0;
strcpy(temp,resp_buff);
strtok(temp, " "); //*
temp = strtok(NULL, " "); //SEARCH
while(strcmp(temp = strtok(NULL, " "), "OK") != 0 ) {
new_messages[i++] = atoi(temp);
total_unseen++;
}
bzero(resp_buff,sizeof(resp_buff));
printf("You have %d new email(s)\n", total_unseen);
printf(" _____________________________________________________________________________________________________________________________________________\n");
printf("| New emails |\n");
printf("|_____________________________________________________________________________________________________________________________________________|\n");
int num=0;
while(new_messages[num] != 0){
display_header(ssl, new_messages[num++]);
printf("|___________________________________________________________________________________________________________________________________________|\n");
}
return total_unseen;
}
void display_email(SSL *ssl, int n){
char resp[BUFFER_SIZE]="";
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char* num=calloc(10, sizeof(int));
char* temp = calloc(BUFFER_SIZE, sizeof(char));
sprintf(num, "%d", n);
printf("\n\n");
printf("_____________________________________________________________________________________________________________________________________________\n");
display_header(ssl, n);
strcpy(buff, "display fetch ");
strcat(buff, num);
strcat(buff, " body[text]\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff)); //fetch
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
while(strcmp(temp,"display OK Success") != 0){
SSL_read(ssl,resp_buff, sizeof (resp_buff));
strcat(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
}
temp = strtok(resp, "\r\n");
temp = strtok(NULL,"\r\n");
while(strcmp(temp, ")") != 0 ){
printf("%s\n",temp);
temp = strtok(NULL, "\r\n");
}
bzero(resp_buff,sizeof(resp_buff));
printf("_____________________________________________________________________________________________________________________\n");
}
void IMAP_request(SSL *ssl){
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char from[BUFFER_SIZE]="";
char authlog[BUFFER_SIZE]="";
char authpass[BUFFER_SIZE]="";
char decodepass[BUFFER_SIZE]="";
char* temp = calloc(BUFFER_SIZE, sizeof(char));
FILE* config_email = NULL;
config_email = fopen("config_email", "r+");
fgets(from,BUFFER_SIZE,config_email);
fgets(authlog,BUFFER_SIZE,config_email);
fgets(authpass,BUFFER_SIZE,config_email);
from[strlen(from) - 1] = '\0';
authlog[strlen(authlog) - 1] = '\0';
authpass[strlen(authpass) - 1] = '\0';
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff,"log login ");
strcat(buff,from);
strcat(buff," ");
strcat(decodepass, (char*)b64_decode(authpass, strlen(authpass)));
strcat(buff,decodepass);
strcat(buff,"\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
char line[BUFFER_SIZE] = "";
display_list(ssl);
while(strcmp(temp,"quit")!=0){
if(strcmp(temp,"list")==0){
display_list(ssl);
}
if(strcmp(temp, "show")==0){
strncpy(temp, line+5, strlen(line)-1);
int n = atoi(temp);
display_email(ssl, n);
}
if(strcmp(temp, "help")==0){
printf("___________________________________________________\n");
printf("User commands : |\n");
printf("list display list of emails |\n");
printf("show (n°email) display a specific email |\n");
printf("quit close the connection |\n");
printf("help diplay commands list |\n");
printf("___________________________________________________|\n");
}
printf("Please enter a command.\nEnter 'help' to display command list.(Enter 'quit' to close your connection)\n");
fgets(line, BUFFER_SIZE, stdin);
strncpy(temp, line, 4);
}
strcpy(buff, "LOGOUT\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
}
int SMTP_request(SSL *ssl, char* to, char* title, char* body){
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char from[BUFFER_SIZE]="";
char authlog[BUFFER_SIZE]="";
char authpass[BUFFER_SIZE]="";
FILE* config_email = NULL;
config_email = fopen("config_email", "r+");
fgets(from,BUFFER_SIZE,config_email);
fgets(authlog,BUFFER_SIZE,config_email);
fgets(authpass,BUFFER_SIZE,config_email);
from[strlen(from) - 1] = '\0';
authlog[strlen(authlog) - 1] = '\0';
authpass[strlen(authpass) - 1] = '\0';
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff, "EHLO localhost \r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "AUTH LOGIN \r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff,"");
strcat(buff,authlog);
strcat(buff,"\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff,"");
strcat(buff,authpass);
strcat(buff,"\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff, "MAIL FROM: <");
strcat(buff,from);
strcat(buff,">\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
if (strstr(resp_buff, "OK")== NULL)
return 1;
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "RCPT TO: <");
strcat(buff,to);
strcat(buff,">\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "DATA\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(buff, "Subject: ");
strcat(buff,title);
strcat(buff,"\r\n");
strcat(buff,body);
strcat(buff,"\r\n.\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));
strcpy(buff, "QUIT\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
return 0;
}
const char* get_ip_adress(const char* target_domain){
const char* target_ip;
struct in_addr *host_address;
struct hostent *raw_list = gethostbyname(target_domain);
int i;
for(i=0; raw_list->h_addr_list[i] != 0; i++) {
host_address = (struct in_addr *)raw_list->h_addr_list[i];
target_ip = inet_ntoa(*host_address);
}
return target_ip;
}
int connect_to_server(const char* server_address, int portno) {
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
struct sockaddr_in addr;
memset(&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(portno);
if (inet_pton(AF_INET, get_ip_adress(server_address), &addr.sin_addr) == 1) {
connect(socket_fd, (struct sockaddr*)&addr, sizeof (addr));
}
return socket_fd;
}
int ssl_connect(char *arg) {
BIO *obj_out = NULL;
const SSL_METHOD *method;
SSL_CTX *ctx;
SSL *ssl;
int connceted_fd = 0;
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();
obj_out = BIO_new_fp(stdout, BIO_NOCLOSE);
if (SSL_library_init() < 0) {
BIO_printf(obj_out, "OpenSSL not initialize");
} else {
method = SSLv23_client_method();
if ((ctx = SSL_CTX_new(method)) == NULL) {
BIO_printf(obj_out, "OpenSSL context not initialize");
} else {
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
ssl = SSL_new(ctx);
if(strcmp(arg,"send")==0){
connceted_fd = connect_to_server("smtp.gmail.com",465);
}else if (strcmp(arg,"read")==0){
connceted_fd = connect_to_server("imap.gmail.com",993);
}
if (connceted_fd != 0) {
SSL_set_fd(ssl, connceted_fd);
if (SSL_connect(ssl) != 1) {
BIO_printf(obj_out, "SLL session not created");
} else {
if(strcmp(arg,"send")==0){
//Initialize
char to[BUFFER_SIZE] ="";
char title[BUFFER_SIZE]="";
char body[BUFFER_SIZE]="";
char line[BUFFER_SIZE]="";
//Asking infos to user (to, title,body)
printf("To :");
scanf("%s", to);
printf("Subject :");
scanf("%s", title);
printf("Write your email and type 'finish' on one line to end your email: \n\n");
fgets(line,BUFFER_SIZE,stdin);
while(strcmp(line,"finish\n")!=0){
strcat(body,line);
fgets(line,BUFFER_SIZE,stdin);
}
printf("\nSending email ... ");
if(SMTP_request(ssl,to,title,body)==0){
system("say email sent");
printf("Email sent \xE2\x9C\x93\n");
}else{
system("say Error. Please verify your email adress and password.");
printf("Error \xe2\x9C\x97\nPlease verify your email adress and password.\n");
}
}else if (strcmp(arg,"read")==0){
IMAP_request(ssl);
}
}
}
}
}
return (EXIT_SUCCESS);
}