-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server.c
More file actions
516 lines (430 loc) · 17.3 KB
/
proxy_server.c
File metadata and controls
516 lines (430 loc) · 17.3 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// File: server.c
// Created April 7, 2014
// Michael Baptist - mbaptist@ucsc.edu
/*******
NAME
proxy_server -- creates a communication path between a client
and a server using TCP.
SYNOPSIS
proxy_server [Port]
DESCRIPTION
This program accepts the port number as it's arguments,
and while running, if contacted by a client, it contacts
the server by forwarding the clients http request. It then
attempts to recieve the request data from the server and
relay it back to the clients browser.
OPERANDS
The only operand is a valid unused port number. If no port
number is input to the program, the program will exit with
an error.
EXIT STATUS
0 No errors were detected.
1 One or more errors were detected and error messages were
printed.
errno If a man page call detects an error, perror prints a sys
message and exits with return value of errno.
******/
#include <stdio.h> // standard I/O
#include <stdlib.h> // standard library
#include <string.h> // conatains memset
#include <sys/types.h> // prerequisite typedefs
#include <errno.h> // error numbers for sys calls
#include <sys/socket.h> // struct sockaddr; system prototypes and consts
#include <netdb.h> // network info lookup prototypes and stuctures
#include <netinet/in.h> // struct sockaddr_in; byte ordering macros
#include <arpa/inet.h> // utility function prototypes
#include <unistd.h> // uni-standard lib.h
#include <sys/select.h> // used for select io multiplexing.
#include <time.h> // for the random numbers
#include <sys/ioctl.h> // allows nonblocking sockets.
#include <pthread.h> // allows for threaded server.
#include <openssl/bio.h> // ssl lib 1
#include <openssl/ssl.h> // ssl lib 2
#include <openssl/err.h> // ssl lib 3
// comment this out to turn on debug print statements.
//#define NDEBUG NDEBUG
#include "utils.h"
#include "http_parser.h"
#include "http_response.h"
#include "log.h"
#include "siteblock.h"
#define SUCCESS 0
#define FAILURE 1
#define TRUE 1
#define FALSE 0
#define BUFLEN 8*1024
typedef struct sockaddr sockaddr;
typedef struct sockaddr_in sockaddr_in;
static uint8_t exit_status = SUCCESS;
// handle a client request gets the data from the server and sends it.
void *handle_client_request(void *clisock);
// closes a client socket and removes it from an fd_set
void close_client(int clisock, fd_set *master);
// setups up thread-safe OpenSSL
int thread_setup(void);
// cleans up thread-safe OpenSSL
int thread_cleanup(void);
int main(int argc, char **argv) {
//initial error checking
if (argc != 2) {
fprintf(stderr, "Error: Include Listening Port Number.\n");
fprintf(stderr, "Usage: %s [PORT]\n", argv[0]);
exit_status = FAILURE;
return FAILURE;
}
char *endptr = NULL;
int portnum = (int)strtol(argv[1], &endptr, 10);
if (0 == portnum || *endptr != '\0') {
// error handling not valid port number
fprintf(stderr, "Error: Invalid Port Number: %s\n", argv[1]);
exit_status = FAILURE;
return FAILURE;
} else {
DEBUGF("Server creates connections on port number: %d\n", portnum);
}
//create socket for the server
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket < 0) {
perror("ERROR: SOCKET CORRUPT ");
return errno;
} else {
DEBUGF("Server Socket: %d\n", listen_socket);
}
// uncomment this if you want nonblocking sockets.
// commented means that threads will block on accept and read and such.
/*
int rc = 0;
int on = 1;
rc = ioctl(listen_socket, FIONBIO, (char *)&on);
if (rc < 0) {
perror("ioctl() failed");
close(listen_socket);
exit(FAILURE);
}
*/
// bind the new socket the port passed into the command line.
sockaddr_in my_serv;
my_serv.sin_family = AF_INET;
my_serv.sin_port = htons(portnum);
my_serv.sin_addr.s_addr = htonl(INADDR_ANY);
memset(my_serv.sin_zero, '\0', sizeof(my_serv.sin_zero));
sockaddr_in *my_serv_ref = &my_serv;
int er_chk = bind(listen_socket, (sockaddr*)my_serv_ref, sizeof(my_serv));
if (er_chk < 0) {
perror("Error: Socket to Address bind failure ");
return errno;
} else {
DEBUGF("Bind Success.\n");
}
// make the new socket a listening socket.
int lis_er_chk = listen(listen_socket, 10);
if (lis_er_chk < 0) {
perror("Error: connection failure. ");
return errno;
}
// Setup openSSL library calls.
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
thread_setup();
// loop forever to accept new connections.
while (1) {
DEBUGF("Main thread waiting for connections on listening port.\n");
// data structure to hold client ip and port info.
sockaddr_in client;
uint clen = (uint)sizeof(client);
// accept the new client
int client_socket = accept(listen_socket, (sockaddr *)&client, &clen);
if (client_socket == -1) {
fprintf(stderr, "Error: accept(2) failed.\n");
} else {
DEBUGF("New connection. IP: %s. Port: %hu.\n", inet_ntoa(client.sin_addr), client.sin_port);
}
// set up thread for new connection
pthread_t thread_ID;
int i = pthread_create(&thread_ID, NULL,
handle_client_request,(void *)&client_socket);
if (i == EAGAIN) {
fprintf(stderr, "Error: Insufficient resources to create another \
thread, or a system imposed limit on \
the number of threads was encountered. \
The latter case may occur in two ways:\
the RLIMIT_NPROC soft resource limit (set \
via setrlimit(2)), which limits the number of \
process for a real user ID, was reached; \
or the kernel's system-wide limit on \
the number of threads, /proc/sys/kernel\
/threads-max, was reached.\n");
} else if (i == EINVAL) {
fprintf(stderr, "Error: Invalid settings in attr.\n");
} else if (i == EPERM) {
fprintf(stderr, "Error: No permission to set the scheduling\
policy and parameters specified in attr.\n");
}
}
// program never gets here, but if there was an exit
// server this would close the server.
close(listen_socket);
thread_cleanup();
return exit_status;
}
// THREAD FUNCTION
void *handle_client_request(void *c) {
DEBUGF("New pthread created to handle client.\n");
// get client socket from the parameter c
int *clisock = (int *)c;
int client_socket = *clisock;
// get peer name
sockaddr_in client_info;
uint clen = (uint)sizeof(client_info);
int gpn_check = getpeername(client_socket, (sockaddr *)&client_info, &clen);
if (gpn_check < 0) {
fprintf(stderr, "getpeername(2) failed. Client info couldn't be \
gathered aborting connection.");
return (void *)FAILURE;
} else {
DEBUGF("client address: %s, port: %hu\n",
inet_ntoa(client_info.sin_addr), client_info.sin_port);
}
// create MAIN timeout for if the connection goes dead for a while then
// exit the thread. here i can use alarm() and signal(SIGARLRM, handler)
// or the timer method from the forums.
// create fd_set to use for the timeout.
fd_set master; // master list of file descriptors.
FD_ZERO(&master);
FD_SET(client_socket, &master); // add listening socket to master list.
// recv http message from browser.
char buf[BUFLEN];
bzero(buf, sizeof(buf));
read(client_socket, buf, sizeof(buf));
DEBUGF("HTTP MESSAGE\n%s", buf);
// log time of received message
char timestamp[64];
get_current_time_formatted(timestamp, sizeof(timestamp));
DEBUGF("%s\n", timestamp);
// parse http message.
size_t numlines = count_newlines(buf, sizeof(buf));
DEBUGF("newlines:%d\n", numlines);
char *httpstrs[numlines];
null_array(httpstrs, numlines);
parse_http_header(httpstrs, buf, numlines);
// parse http line.
// look for http command and version number
char *httptokens[100];
null_array(httptokens, 100);
parse_http_header_line(httptokens, httpstrs[0], 100);
// check if allowed server and supported msg (GET HEAD POST only)
// respond with 501 if bad msg
char command[16];
char uri[1024];
char http_version[16];
if (strncmp("GET", httptokens[0], sizeof(httptokens[0])) == 0 ||
strncmp("POST", httptokens[0], sizeof(httptokens[0])) == 0 ||
strncmp("HEAD", httptokens[0], sizeof(httptokens[0])) == 0) {
DEBUGF("%s command.\n", httptokens[0]);
bcopy(httptokens[0], command, strlen(httptokens[0]));
bcopy(httptokens[1], uri, strlen(httptokens[1]));
bcopy(httptokens[2], http_version, strlen(httptokens[2]));
free_parse_allocs(httptokens, 100);
} else {
//log_request(timestamp, httptokens[0], httptokens[2], inet_ntoa(client_info.sin_addr), httptokens[1], NULL, "Filtered", "HTTP command not supported");
fprintf(stderr, "%s command not supported.\n", httptokens[0]);
char *response = http_response(501);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
free_parse_allocs(httptokens, 100);
free_parse_allocs(httpstrs, numlines);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
// check for missing fields in the http msg
// respond with 400 if bad
// UNIMPLMENTED
// get the host and get the server and ip of host.
null_array(httptokens, 100);
parse_http_header_line(httptokens, httpstrs[1], 100);
DEBUGF("HOST: %s\n", httptokens[1]);
// check the parental_controls log if the site is allowed.
if (allowed_site(httptokens[1]) == -1) {
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, NULL, "Filtered", "Site Blocked");
fprintf(stderr, "Error: site not allowed by proxy.\n");
char *response = http_response(403);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
free_parse_allocs(httpstrs, numlines);
free_parse_allocs(httptokens, 100);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
// do the dns lookup.
struct hostent *host = gethostbyname(httptokens[1]);
if (host == NULL) {
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, NULL, "Filtered", "DNS Failed");
fprintf(stderr, "Error: gethostbyname(3) failed\n");
free_parse_allocs(httpstrs, numlines);
free_parse_allocs(httptokens, 100);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
free_parse_allocs(httptokens, 100);
// get info into sockaddr_in struct.
sockaddr_in host_addr;
bzero((char*)&host_addr,sizeof(host_addr));
host_addr.sin_port = htons(80);
host_addr.sin_family = AF_INET;
bcopy((char*)host->h_addr,(char*)&host_addr.sin_addr.s_addr,host->h_length);
// save server address for logging.
char *serv_addr_str = inet_ntoa(host_addr.sin_addr);
DEBUGF("IP: %s, Port: %hu.\n", serv_addr_str, host_addr.sin_port);
// create socket to forward the request.
DEBUGF("Thread creating socket to connect to server.\n");
int forward_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (forward_socket < 0) {
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, serv_addr_str, "Filtered", "Socket(2) Failed");
fprintf(stderr, "Error: creating socket to forward the request"
"has failed. Aborting request.\n");
free_parse_allocs(httpstrs, numlines);
char *response = http_response(500);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
// connect the to the server using the new socket.
DEBUGF("Thread attempting to connect to the server, on socket %d.\n", forward_socket);
int servcon = connect(forward_socket, (sockaddr *)&host_addr, sizeof(sockaddr));
if (servcon < 0) {
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, serv_addr_str, "Filtered", "Connection to server failed");
fprintf(stderr, "Error: Connection Failed. Aborting request.\n");
free_parse_allocs(httpstrs, numlines);
close(forward_socket);
char *response = http_response(500);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
// Setup SSL context.
DEBUGF("Setting up SSL Context.\n");
BIO *bio;
SSL *ssl;
SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
// setup the new connection
DEBUGF("Setting up a new connection using the current OpenSSL Context.\n");
ssl = SSL_new(ctx);
bio = BIO_new_socket(forward_socket, BIO_NOCLOSE);
SSL_set_bio(ssl, bio, bio);
// forward the request to the server.
DEBUGF("Forwarding client http request to the server.\n");
BIO_write(bio, buf, sizeof(buf));
free_parse_allocs(httpstrs, numlines);
// get the response from the server.
DEBUGF("Replying to client response by forwarding server response to client.\n");
int ssl_wchk = 0;
char forwarder[500];
for (;;) {
ssl_wchk = BIO_read(bio, forwarder, 499);
if (ssl_wchk <= 0) break;
int wc = send(client_socket, forwarder, sizeof(forwarder), MSG_NOSIGNAL);
if (wc < 0) {
//log_request(timestamp, command, http_version, inet_ntoa(clien t_info.sin_addr), uri, serv_addr_str, "Forwarding", "Server Forwarding Faile d");
fprintf(stderr, "Error: request forward from real server failed .\n");
fprintf(stderr, "Error: Aborting request.\n");
BIO_free_all(bio);
SSL_CTX_free(ctx);
char *response = http_response(500);
send(client_socket, response, strlen(response), MSG_NOSIGNAL);
free(response);
SSL_shutdown(ssl);
close(forward_socket);
close_client(client_socket, &master);
pthread_exit((void *)FAILURE);
}
}
// close the connection and free the ssl context.
BIO_free_all(bio);
SSL_CTX_free(ctx);
//log_request(timestamp, command, http_version, inet_ntoa(client_info.sin_addr), uri, serv_addr_str, "Forwarded", NULL);
SSL_shutdown(ssl);
close(forward_socket);
close_client(client_socket, &master);
pthread_exit((void *)SUCCESS);
}
void close_client(int clisock, fd_set *master) {
DEBUGF("closing client socket: %d.\n", clisock);
close(clisock);
FD_CLR(clisock, master);
}
/***********************************************************************
*Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
*************************************************************************/
/* Example source code to show one way to set the necessary OpenSSL locking
* callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with
* libcurl built to use OpenSSL.
*
* This is not a complete stand-alone example.
*
* Author: Jeremy Brown
*/
#define MUTEX_TYPE pthread_mutex_t
#define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
#define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
#define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
#define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
#define THREAD_ID pthread_self( )
void handle_error(const char *file, int lineno, const char *msg){
fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
ERR_print_errors_fp(stderr);
/* exit(-1); */
}
/* This array will store all of the mutexes available to OpenSSL. */
static MUTEX_TYPE *mutex_buf= NULL;
static void locking_function(int mode, int n, const char * file, int line)
{
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(mutex_buf[n]);
else
MUTEX_UNLOCK(mutex_buf[n]);
}
static unsigned long id_function(void)
{
return ((unsigned long)THREAD_ID);
}
int thread_setup(void)
{
int i;
mutex_buf = malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
if (!mutex_buf)
return 0;
for (i = 0; i < CRYPTO_num_locks( ); i++)
MUTEX_SETUP(mutex_buf[i]);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
return 1;
}
int thread_cleanup(void)
{
int i;
if (!mutex_buf)
return 0;
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
for (i = 0; i < CRYPTO_num_locks( ); i++)
MUTEX_CLEANUP(mutex_buf[i]);
free(mutex_buf);
mutex_buf = NULL;
return 1;
}