-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
153 lines (148 loc) · 3.63 KB
/
main.c
File metadata and controls
153 lines (148 loc) · 3.63 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
#include "args.h"
#include "encryption.h"
#include "networking.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef DEFAULT_PORT
#define DEFAULT_PORT 43337
#endif
#define ENCRYPTED_FILE_EXT ".enc"
int filefd, sockfd, recvfd;
int bytes_received;
int status;
char address[INET_ADDRSTRLEN];
struct sockaddr_in addr_in;
unsigned char key[SHA256_DIGEST_LENGTH];
char filename_encrypted[256];
char buf[1024];
int
main(int argc, char **argv)
{
/* Parse arguments */
struct u_option o = {.net = {.addr = "0", .port = DEFAULT_PORT}};
parse_args(argc, argv, &o);
if (o.enc.algo != NONE) {
get_key(key);
o.enc.key = key;
strncpy(filename_encrypted, o.filename, sizeof(filename_encrypted));
strncat(filename_encrypted, ENCRYPTED_FILE_EXT,
sizeof(filename_encrypted) - sizeof(ENCRYPTED_FILE_EXT));
}
/* Initialize socket */
sockfd = initialize_socket();
addr_in = initialize_addr_in(o.net.addr, o.net.port);
if (sockfd < 0) {
perror("cannot create socket");
return 0;
}
/* Select SEND or RECEIVE */
switch (o.action) {
case SEND:
/* Connect to server */
if (connect(sockfd, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0) {
close(sockfd);
perror("cannot connect to address");
return 2;
}
if (inet_ntop(AF_INET, &addr_in.sin_addr, address, INET_ADDRSTRLEN) ==
NULL) {
perror("cannot get address");
return 2;
}
printf("Connected to address: %s\n", address);
switch (o.input) {
case STDIN:
while (fgets(buf, sizeof(buf), stdin) != NULL) {
send(sockfd, buf, strlen(buf), 0);
}
break;
case READ_FILE:
switch (o.enc.algo) {
case AES256:
file_encrypt(o.enc.key, o.filename, filename_encrypted);
if (sendfile_name(filename_encrypted, sockfd) < 0) {
perror("file cannot be sent");
}
unlink(filename_encrypted);
break;
default:
if (sendfile_name(o.filename, sockfd) < 0) {
perror("file cannot be sent");
}
break;
}
break;
}
shutdown(sockfd, SHUT_WR);
break;
case RECEIVE:
/* Start server */
recvfd = start_server(sockfd, addr_in);
if (recvfd < 0) {
close(sockfd);
perror("cannot start server");
return 2;
}
/* Output mode */
switch (o.output) {
case STDOUT:
puts("------------------------");
while (1) {
bytes_received = recv(recvfd, buf, sizeof(buf), 0);
if (bytes_received < 0) {
perror("could not write socket to stdout");
return 2;
} else if (bytes_received == 0) {
puts("Connection closed by peer");
break;
}
write(STDOUT_FILENO, buf, bytes_received);
}
break;
case SAVE_FILE:
if (o.enc.algo == NONE) {
filefd = open(o.filename, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR);
} else {
filefd = open(filename_encrypted, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR);
}
if (filefd < 0) {
perror("cannot open file");
break;
}
if (write_socket_to_file(recvfd, filefd) < 0) {
perror("cannot write socket to file");
}
close(filefd);
if (o.enc.algo == AES256) {
if (file_decrypt(o.enc.key, filename_encrypted, o.filename) <
0) {
puts("decrypt file failed");
unlink(o.filename);
fputs("Do you want to remove encrypted file? [y/n]",
stdout);
fgets(buf, sizeof(buf), stdin);
if (strncmp(buf, "y\n", 2) == 0) {
unlink(filename_encrypted);
}
return 1;
}
unlink(filename_encrypted);
}
break;
}
shutdown(recvfd, SHUT_RD);
close(recvfd);
break;
}
close(sockfd);
return 0;
}