-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash-service.c
388 lines (324 loc) · 10.7 KB
/
hash-service.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
/*
* hash-service: a service for hashing FTP streams
*
* PROTOCOL: Connect to command port (8556)
* -> ASCII, line based protocol
* Only supported commands:
* HASH [port]\r\n
* where [port] is a valid port number.
*
* QUIT
* Disconnects.
*
* the hash service will connect to the port, listen,
* and sha256 hash the incoming TCP stream.
* The hash service does not support multiple hashing jobs within
* the same TCP session.
*
* The final sha256 hash will be sent on a newline as an ascii hex string
* (lowercase) after a 201 status response
*
* 201 Success\r\n
* d390b8237b393875d968c7e5703685d37782d1b5c26b2d30839c3115ba1f3e73\r\n
*
* Status responses:
* Format [STATUS CODE] String Description
* 500 Bad Request
* 400 Internal Error
* 401 No Data
* 201 Success
*
*
* Known issues:
*
* -> connect() takes far too long to time out by default,
* so commanding the server to hash on a port that isn't open takes
* over a minute to return an error code
* https://stackoverflow.com/a/2597669
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <strings.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include "sha256.h"
char* server_ip;
char* hash_to_string(const unsigned char*);
void hash_file(const char *filename, unsigned char* hash) {
SHA256_CTX ctx;
sha256_init(&ctx);
FILE* file;
if ((file = fopen(filename, "r")) == NULL) {
perror("hash-service: error opening file in hash_file:");
}
unsigned char read_buf[64];
int read_size;
while((read_size = fread(read_buf, 1, sizeof(read_buf), file)) != 0) {
sha256_update(&ctx, read_buf, read_size);
}
sha256_final(&ctx, hash);
if (fclose(file) != 0) {
perror("hash-service: error closing file in hash_file");
}
}
int hash_fd(int fd, unsigned char* hash) {
SHA256_CTX ctx;
sha256_init(&ctx);
unsigned char read_buf[64];
int read_size;
while((read_size = read(fd, read_buf, sizeof(read_buf))) > 0) {
sha256_update(&ctx, read_buf, read_size);
}
if (read_size < 0) {
return -1;
}
sha256_final(&ctx, hash);
return 0;
}
char* hash_to_string(const unsigned char *hash) {
static char str[65];
bzero(str, sizeof(str));
char* write_ptr = str;
for(int i = 0; i < 32; i++) {
sprintf(write_ptr, "%02x", (uint8_t)hash[i]);
write_ptr += 2;
}
return str;
}
void populate_sockaddr(struct sockaddr_in * addr, int port, char* ip) {
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
inet_aton(ip, &addr->sin_addr);
}
typedef struct {
char line[1024];
char* endptr;
char* readptr;
} LineBuffer;
void init_line_buffer(LineBuffer* buf) {
buf->endptr = buf->line;
buf->readptr = NULL;
}
//purge current line from buffer and shift in rest of buffer
void consume_line(LineBuffer* buf) {
memcpy(buf->line, buf->endptr, 1024 - (buf->endptr - buf->line));
}
//buffer line from socket buffer
//returns 0 if no line found, 1 if line found
int get_sock_line(LineBuffer* buf, char* read_buf, size_t read_buf_size) {
if (buf->readptr == NULL) {
buf->readptr = read_buf;
}
for (; buf->readptr < read_buf + read_buf_size - 1; buf->readptr++) {
//reset ptr if it goes beyond the buffer
if (buf->endptr - buf->line >= sizeof(buf->line)) {
buf->endptr = buf->line;
}
//detect \r\n
if (*buf->readptr == '\r' && *(buf->readptr+1) == '\n') {
buf->readptr++;
*buf->endptr = '\0';
buf->endptr = buf->line;
return 1;
}
*buf->endptr++ = *buf->readptr;
}
buf->readptr = NULL;
return 0;
}
void* control_session(void* arg) {
int sock = *(int*)arg;
LineBuffer line_buffer;
init_line_buffer(&line_buffer);
char buf[1024];
int read_sz;
while ((read_sz = recv(sock, buf, sizeof(buf), 0)) > 0) {
while(get_sock_line(&line_buffer, buf, read_sz)) {
char* syntax_err = "500 Bad Request\r\n";
char* internal_err = "400 Internal Error\r\n";
char* nodata_err = "401 No Data\r\n";
char* success = "200 Success\r\n";
if (strcmp("QUIT", line_buffer.line) == 0) {
read_sz = 0;
goto end;
}
if (strcmp("HASH", strtok(line_buffer.line, " "))) {
if (send(sock, syntax_err, strlen(syntax_err), 0) < 0) {
perror("hash-service: error sending 500 Bad Request");
}
continue;
}
char* arg;
if ((arg = strtok(NULL, " ")) == NULL) {
if (send(sock, syntax_err, strlen(syntax_err), 0) < 0) {
perror("hash-service: error sending 500 Bad Request");
}
continue;
}
char* endptr;
int port = strtol(arg, &endptr, 10);
if (*endptr != '\0') {
if (send(sock, syntax_err, strlen(syntax_err), 0) < 0) {
perror("hash-service: error sending 500 Bad Request");
}
continue;
}
//connect to port and commence hashing!
int ftp_socket = socket(AF_INET, SOCK_STREAM, 0);
if (ftp_socket == -1) {
perror("hash-service: ftp socket creation failed");
if (send(sock, internal_err, strlen(internal_err), 0) < 0) {
perror("hash-service: error sending 400 Internal Error");
}
continue;
}
struct sockaddr_in addr;
populate_sockaddr(&addr, port, server_ip);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if (setsockopt (ftp_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0) {
perror("hash-service: setsockopt failed");
}
/*
if (setsockopt (ftp_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0) {
perror("hash-service: setsockopt failed");
}
*/
if (connect(ftp_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("hash-service: ftp connection failed");
if (send(sock, internal_err, strlen(internal_err), 0) < 0) {
perror("hash-service: error sending 400 Internal Error");
}
continue;
}
unsigned char hash[32];
if (hash_fd(ftp_socket, hash)) {
switch (errno) {
case EWOULDBLOCK:
fprintf(stderr, "ftp connection timed out\n");
break;
default:
perror("hash-service: ftp connection");
}
if (send(sock, nodata_err, strlen(nodata_err), 0) < 0) {
perror("hash-service: error sending 401 No Data");
}
if(close(ftp_socket)) {
perror("hash-service: error closing ftp socket");
}
continue;
}
char hashdump[70];
sprintf(hashdump, "%s\r\n", hash_to_string(hash));
if (send(sock, success, strlen(success), 0) < 0) {
perror("hash-service: error sending 200 Success");
}
if (send(sock, hashdump, strlen(hashdump), 0) < 0) {
perror("hash-service: error sending hexdump");
}
if(close(ftp_socket)) {
perror("hash-service: error closing ftp socket");
}
}
}
end:
switch (read_sz) {
case 0:
printf("Client disconnected\n");
break;
default:
switch (errno) {
case EWOULDBLOCK:
fprintf(stderr, "Client timed out\n");
break;
default:
perror("hash-service: control socket receive failed");
}
}
return NULL;
}
void run_server(int upload_port) {
//wait for connection from client
int client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (client_sockfd == -1) {
perror("hash-service: socket create failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in addr;
populate_sockaddr(&addr, upload_port, "0.0.0.0");
if (bind(client_sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("hash-service: bind failed");
exit(EXIT_FAILURE);
}
if (listen(client_sockfd, 5) < 0) {
perror("hash-service: listen failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in in_addr;
socklen_t in_len = sizeof(in_addr);
int insocket;
while ((insocket =
accept(client_sockfd, (struct sockaddr*)&in_addr, &in_len)) != -1) {
//configure timeouts on new socket
struct timeval timeout;
timeout.tv_sec = 90;
timeout.tv_usec = 0;
if (setsockopt (insocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0) {
perror("hash-service: setsockopt failed");
}
if (setsockopt (insocket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0) {
perror("hash-service: setsockopt failed");
}
pthread_t control_thread;
printf("Creating control thread for client at %s:%d\n",
inet_ntoa(in_addr.sin_addr), ntohs(in_addr.sin_port));
if (pthread_create(&control_thread, NULL, control_session, &insocket)) {
perror("hash-service: pthread_create failed");
}
else {
void* retval;
if (pthread_join(control_thread, &retval) < 0) {
perror("hash-service: pthread_join failed");
}
printf("Thread terminated\n");
}
if (close(insocket) < 0) {
perror("hash-service: close failed");
}
}
perror("hash-service: accept failed");
}
int main(int argc, char* argv[]) {
int port = 8009;
server_ip = "127.0.0.1";
switch (argc) {
case 3:
server_ip = argv[2];
case 2:;
char* endptr;
port = strtol(argv[1], &endptr, 10);
if (*endptr != '\0') {
fprintf(stderr, "Error: invalid port\n");
exit(EXIT_FAILURE);
}
case 1:
break;
default:
fprintf(stderr, "Usage: hash-service [port=8009] [server_ip=127.0.0.1]\n");
exit(EXIT_FAILURE);
}
run_server(port);
return 0;
}