-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlite.c
More file actions
322 lines (256 loc) · 9.69 KB
/
lite.c
File metadata and controls
322 lines (256 loc) · 9.69 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "../allog/allog.h"
#define BACKLOG 1024 // The maximum number of unserviced connection requests that may be queued
#define MAX_LINE_LEN 1024
extern char **environ;
int get_listen_sock(char*);
void service_request(SSL*);
void serve_static(SSL*, char *);
void serve_dynamic(SSL*, char *);
void response_400(SSL *ssl);
void response_404(SSL *ssl);
const char *get_content_type(const char*);
log_obj *server_logger;
void init_logs(log_obj **logger)
{
*logger = new_log_obj(DEBUG);
struct file_handler *std = new_stream_handler(stderr);
struct file_handler *log_file = new_file_handler("file.log");
struct log_handler *sys = new_syslog_handler();
add_handler(*logger, std);
add_handler(*logger, log_file);
add_handler(*logger, sys);
}
int main(int argc, char *argv[])
{
init_logs(&server_logger);
// The web-server software takes the port on which to listen for connections as the sole command line argument
if (argc != 2) {
log_message(server_logger, ERROR, "usage: 'lite <port>'");
exit(EXIT_FAILURE);
}
// We then create the socket that listens on the supplied port number
int listen_sock = get_listen_sock(argv[1]);
// If creation of the listening socket fails, then the retured descriptor value is -1 in which case we terminate the program
if (listen_sock == -1) {
log_message(server_logger, ERROR, "error: Creation of listening socket failed");
exit(EXIT_FAILURE);
}
log_message(server_logger, INFO, "Now listening on port: %s", argv[1]);
// We initialize the SSL library (which implements the Transport Layer Security (TLS) protocol
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
while (1) {
// We create the data structure to store the address of the client connecting to the server
struct sockaddr_storage client_addr;
socklen_t client_len = sizeof(client_addr);
// We accept connections on our listening socket
int client_sock = accept(listen_sock, (struct sockaddr*) &client_addr, &client_len);
// If accept() fails (i.e. returns -1) for whatever reason we print an error message and terminate the program
if (client_sock == -1) {
log_message(server_logger, ERROR, "error: accept() failed");
exit(EXIT_FAILURE);
}
// We then print the details of the client whose connection request we have accepted
char client_hostname[MAX_LINE_LEN], client_port[MAX_LINE_LEN];
getnameinfo((struct sockaddr*) &client_addr, client_len, client_hostname, MAX_LINE_LEN, client_port, MAX_LINE_LEN, 0);
log_message(server_logger, INFO,"Connected to %s, %s", client_hostname, client_port);
// Once connected to a client we try to establish a secure connection before responding to any request
// We create an SSL context which functions as a sort of factory for creating SSL objects
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
if (!ctx) {
log_message(server_logger, ERROR, "error: SSL_CTL_new() failed");
exit(EXIT_FAILURE);
}
// We set the newly created context to use our self-signed certificate
if (!SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) || !SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM)) {
log_message(server_logger, ERROR, "SSL_CTL_use_certificate_file() failed");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
// We then create a new SSL object
SSL *ssl = SSL_new(ctx);
if (!ssl) {
log_message(server_logger, ERROR, "error: SSL_new() failed");
exit(EXIT_FAILURE);
}
// And then link the newly created SSL object to use the socket of the accepted connection
SSL_set_fd(ssl, client_sock);
// We can now accept connections on the SSL object
if (SSL_accept(ssl) <= 0) {
log_message(server_logger, ERROR, "SSL_accept() failed");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
log_message(server_logger, INFO, "Secure connection using %s", SSL_get_cipher(ssl));
// We then service client requests on the encrypted communication channel
service_request(ssl);
// All resources are freed once the request is serviced
SSL_shutdown(ssl);
close(client_sock);
SSL_free(ssl);
}
exit(EXIT_SUCCESS);
}
// get_listen_sock() creates the socket that shall listen for incoming connections and binds it to the specified port number
int get_listen_sock(char *port)
{
int listen_sock, optval=1;
struct addrinfo hints, *p, *address_list;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM; // TCP sockets to be used
hints.ai_family = AF_INET; // IPV4 supported
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; // Accept requests on any of available IP addresses
hints.ai_flags |= AI_NUMERICSERV; // Web service specified as a port no
int gai_err;
if ((gai_err = getaddrinfo(NULL, port, &hints, &address_list)) != 0) {
log_message(server_logger, CRITICAL, "getaddrinfo() failed: %s", gai_strerror(gai_err));
exit(EXIT_FAILURE);
}
for (p = address_list; p; p = p->ai_next) {
if ((listen_sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
continue;
// We ensure that the listening socket is reusable immediately after the program terminates
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const void*)&optval, sizeof(int));
if ((bind(listen_sock, p->ai_addr, p->ai_addrlen)) == 0)
break;
close(listen_sock);
}
freeaddrinfo(address_list);
//If the entire address list is exhausted without a successful bind, p shall have a null value
if (!p)
return -1;
//The socket is made into a listening socket
if ((listen(listen_sock, BACKLOG)) < 0) {
close(listen_sock);
return -1;
}
return listen_sock;
}
void service_request(SSL *ssl)
{
char inp_buffer[4096];
SSL_read(ssl, inp_buffer, 4096);
char *str = strtok(inp_buffer, "\r\n");
char method[64], uri[1024], version[64];
sscanf(str, "%s %s %s", method, uri, version);
if (strcmp(method, "GET") == 0) {
if (strcmp(uri, "/") == 0) {
serve_static(ssl, "sample_site/""index.html");
} else if (strncmp(uri, "/cgi-bin", 8) == 0) {
serve_dynamic(ssl, uri);
} else {
char url[1024];
strcat(url, "sample_site/");
strcat(url, strtok(uri, "/"));
serve_static(ssl, url);
}
} else {
response_400(ssl);
}
}
void serve_static(SSL *ssl, char *resource_name)
{
FILE *resource = fopen(resource_name, "r");
if (resource == NULL) {
log_message(server_logger, ERROR, "fopen(%s) failed\n", resource_name);
response_404(ssl);
return;
}
char string_buffer[1024];
sprintf(string_buffer, "HTTP/1.0 200 0K\r\n");
SSL_write(ssl, string_buffer, strlen(string_buffer));
sprintf(string_buffer, "Connection: close\r\n");
SSL_write(ssl, string_buffer, strlen(string_buffer));
int content_len = 0;
while (fgetc(resource) != EOF) {
content_len += 1;
}
rewind(resource);
sprintf(string_buffer, "Content-Length: %d\r\n", content_len);
SSL_write(ssl, string_buffer, strlen(string_buffer));
const char *ct = get_content_type(resource_name);
sprintf(string_buffer, "Content-Type: %s\r\n", ct);
SSL_write(ssl, string_buffer, strlen(string_buffer));
sprintf(string_buffer, "Server: lite-server\r\n");
SSL_write(ssl, string_buffer, strlen(string_buffer));
sprintf(string_buffer, "\r\n");
SSL_write(ssl, string_buffer, strlen(string_buffer));
int read_items;
while ((read_items = fread(string_buffer, 1, 1024, resource)) != 0) {
SSL_write(ssl, string_buffer, read_items);
}
fclose(resource);
}
void serve_dynamic(SSL *ssl, char *uri)
{
char *script_name = strtok(uri, "?");
script_name++;
char *cgi_args = strtok(NULL, "?");
char buf[4096], *empty_list[] = {NULL};
sprintf(buf, "HTTP/1.0 200 OK\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "Server: lite-server\r\n");
SSL_write(ssl, buf, strlen(buf));
int fail = 0;
int write_back = fileno(tmpfile());
if (fork() == 0) {
setenv("QUERY_STRING", cgi_args, 1);
dup2(write_back, STDOUT_FILENO);
fail = execve(script_name, empty_list, environ);
}
if (fail == -1)
log_message(server_logger, ERROR, "execve() failed: %s\n", strerror(errno));
int status;
wait(&status);
char outp_buf[4096];
lseek(write_back, 0, SEEK_SET);
read(write_back, outp_buf, 4096);
SSL_write(ssl, outp_buf, strlen(outp_buf));
}
void response_400(SSL *ssl)
{
const char *c400 = "HTTP/1.1 400 Bad Request\r\n" "Connection: close\r\n" "Content-Length: 11\r\n\r\nBad Request";
SSL_write(ssl, c400, strlen(c400));
}
void response_404(SSL *ssl)
{
const char *c404 = "HTTP/1.1 400 Not Found\r\n" "Connection: close\r\n" "Content-Length: 9\r\n\r\nNot Found";
SSL_write(ssl, c404, strlen(c404));
}
const char *get_content_type(const char* path)
{
const char *last_dot = strchr(path, '.');
if (last_dot) {
if (strcmp(last_dot, ".css") == 0) return "text/css";
if (strcmp(last_dot, ".csv") == 0) return "text/csv";
if (strcmp(last_dot, ".gif") == 0) return "image/gif";
if (strcmp(last_dot, ".htm") == 0) return "text/html";
if (strcmp(last_dot, ".html") == 0) return "text/html";
if (strcmp(last_dot, ".ico") == 0) return "image/x-icon";
if (strcmp(last_dot, ".jpeg") == 0) return "image/jpeg";
if (strcmp(last_dot, ".jpg") == 0) return "image/jpeg";
if (strcmp(last_dot, ".js") == 0) return "application/javascript";
if (strcmp(last_dot, ".json") == 0) return "application/json";
if (strcmp(last_dot, ".png") == 0) return "image/png";
if (strcmp(last_dot, ".pdf") == 0) return "application/pdf";
if (strcmp(last_dot, ".svg") == 0) return "image/svg";
if (strcmp(last_dot, ".txt") == 0) return "text/plain";
}
return "application/octet-stream";
}