Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_ssl_hardened.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
35#include "nilorea/n_list.h"
36#include "nilorea/n_str.h"
37#include "nilorea/n_log.h"
38#include "nilorea/n_network.h"
40#include "nilorea/n_signals.h"
41
42#ifdef __windows__
43#define realpath(rel, abs) _fullpath(abs, rel, MAX_PATH)
44#endif
45
46char* port = NULL;
47char* addr = NULL;
48char* key = NULL;
49char* cert = NULL;
50char* ca_file = NULL;
51char* root_dir = NULL;
52LIST* routes = NULL;
55#define MAX_HTTP_REQUEST_SIZE_LIMIT 1048576
56#define CONNECTION_TIMEOUT_SECONDS 10
57int ssl_verify = 0;
58int max_connections = 0; /* 0 = unlimited */
59bool done = 0;
60
61NETWORK *server = NULL,
62 *netw = NULL;
65#define SECURITY_HEADERS \
66 "Strict-Transport-Security: max-age=31536000\r\n" \
67 "X-Content-Type-Options: nosniff\r\n" \
68 "X-Frame-Options: DENY\r\n" \
69 "Content-Security-Policy: default-src 'self'\r\n" \
70 "Referrer-Policy: strict-origin-when-cross-origin\r\n" \
71 "Permissions-Policy: geolocation=(), camera=(), microphone=()\r\n"
72
73void usage(void) {
74 fprintf(stderr,
75 " -p 'port' : set the https server port\n"
76 " -k 'key file' : SSL key file path\n"
77 " -c 'cert file' : SSL certificate file path\n"
78 " -A 'CA file' : optional, CA certificate file for chain verification\n"
79 " -e : optional, enable SSL peer certificate verification\n"
80 " -a 'address name/ip' : optional, specify where to bind interface\n"
81 " -i 'ipmode' : optional, force 'ipv4' or 'ipv6', default supports both\n"
82 " -s 'size' : optional, maximum http request size (default: %d, max: %d)\n"
83 " -n 'count' : optional, exit after count connections (0 = unlimited, default)\n"
84 " -d 'html root' : optional, specify a different http root dir (default: ./DATAS/)\n"
85 " -v : version\n"
86 " -h : help\n"
87 " -V 'log level' : optional, set the log level (default: LOG_ERR)\n",
89}
90
91void process_args(int argc_nb, char** argv_ptr, char** addr_ptr, char** port_ptr, char** key_ptr, char** cert_ptr, char** ca_file_ptr, int* ssl_verify_ptr, LIST* routes_ptr, int* ip_version_ptr, int* max_http_request_size_ptr, char** root_dir_ptr) {
92 int getoptret = 0,
93 log_level = LOG_ERR; /* default log level */
94
95 if (argc_nb == 1) {
96 fprintf(stderr, "No arguments given, help:\n");
97 usage();
98 exit(1);
99 }
100 while ((getoptret = getopt(argc_nb, argv_ptr, "hven:s:V:p:i:a:r:k:c:s:d:A:")) != EOF) {
101 switch (getoptret) {
102 case 'n':
103 max_connections = atoi(optarg);
104 break;
105 case 'i':
106 if (!strcmp("v4", optarg)) {
107 (*ip_version_ptr) = NETWORK_IPV4;
108 n_log(LOG_NOTICE, "IPV4 selected");
109 } else if (!strcmp("v6", optarg)) {
110 (*ip_version_ptr) = NETWORK_IPV6;
111 n_log(LOG_NOTICE, "IPV6 selected");
112 } else {
113 n_log(LOG_NOTICE, "IPV4/6 selected");
114 }
115 break;
116 case 'v':
117 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
118 exit(1);
119 case 'V':
120 if (!strncmp("LOG_NULL", optarg, 8)) {
122 } else {
123 if (!strncmp("LOG_NOTICE", optarg, 10)) {
125 } else {
126 if (!strncmp("LOG_INFO", optarg, 8)) {
128 } else {
129 if (!strncmp("LOG_ERR", optarg, 7)) {
131 } else {
132 if (!strncmp("LOG_DEBUG", optarg, 9)) {
134 } else {
135 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
136 exit(-1);
137 }
138 }
139 }
140 }
141 }
142 break;
143 case 'e':
144 (*ssl_verify_ptr) = 1;
145 break;
146 case 'p':
147 (*port_ptr) = strdup(optarg);
148 break;
149 case 'r':
150 list_push(routes_ptr, strdup(optarg), &free);
151 break;
152 case 'a':
153 (*addr_ptr) = strdup(optarg);
154 break;
155 case 'k':
156 (*key_ptr) = strdup(optarg);
157 break;
158 case 'c':
159 (*cert_ptr) = strdup(optarg);
160 break;
161 case 'A':
162 (*ca_file_ptr) = strdup(optarg);
163 break;
164 case 's': {
165 int val = atoi(optarg);
166 if (val <= 0 || val > MAX_HTTP_REQUEST_SIZE_LIMIT) {
167 fprintf(stderr, "Invalid max http request size: %s (must be 1-%d)\n", optarg, MAX_HTTP_REQUEST_SIZE_LIMIT);
168 exit(1);
169 }
170 (*max_http_request_size_ptr) = val;
171 break;
172 }
173 case 'd':
174 (*root_dir_ptr) = strdup(optarg);
175 break;
176 default:
177 case '?': {
178 if (optopt == 'd') {
179 fprintf(stderr, "\n Missing html root directory\n");
180 }
181 if (optopt == 's') {
182 fprintf(stderr, "\n Missing max http size string\n");
183 }
184 if (optopt == 'k') {
185 fprintf(stderr, "\n Missing key file string\n");
186 }
187 if (optopt == 'c') {
188 fprintf(stderr, "\n Missing certificate file string\n");
189 }
190 if (optopt == 'A') {
191 fprintf(stderr, "\n Missing CA file string\n");
192 }
193 if (optopt == 'r') {
194 fprintf(stderr, "\n Missing route string\n");
195 }
196 if (optopt == 'a') {
197 fprintf(stderr, "\n Missing binding host/addr string\n");
198 }
199 if (optopt == 'i') {
200 fprintf(stderr, "\n Missing ip version (v4 or v6) string \n");
201 } else if (optopt == 'V') {
202 fprintf(stderr, "\n Missing log level string\n");
203 } else if (optopt == 'p') {
204 fprintf(stderr, "\n Missing port\n");
205 } else if (optopt != 's') {
206 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
207 }
208 usage();
209 exit(1);
210 }
211 case 'h': {
212 usage();
213 exit(1);
214 }
215 }
216 }
218} /* void process_args( ... ) */
219
235 __n_assert(ssl_netw, return FALSE);
236 __n_assert(ssl_netw->ctx, return FALSE);
237
238 /* Enforce TLS 1.2 minimum */
239#if OPENSSL_VERSION_NUMBER >= 0x10101000L
240 if (!SSL_CTX_set_min_proto_version(ssl_netw->ctx, TLS1_2_VERSION)) {
241 n_log(LOG_ERR, "Failed to set minimum TLS version to 1.2");
242 return FALSE;
243 }
244#else
245 SSL_CTX_set_options(ssl_netw->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
246#endif
247
248 /* Security options:
249 * - NO_COMPRESSION: prevents CRIME attack
250 * - CIPHER_SERVER_PREFERENCE: server chooses cipher order
251 * - NO_SESSION_RESUMPTION_ON_RENEGOTIATION: prevent renegotiation abuse
252 * - NO_TICKET: disable stateless session tickets for better forward secrecy
253 * - SINGLE_DH_USE / SINGLE_ECDH_USE: fresh keys for each handshake */
254 SSL_CTX_set_options(ssl_netw->ctx,
255 SSL_OP_NO_COMPRESSION |
256 SSL_OP_CIPHER_SERVER_PREFERENCE |
257 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
258 SSL_OP_NO_TICKET |
259 SSL_OP_SINGLE_DH_USE |
260 SSL_OP_SINGLE_ECDH_USE);
261
262 /* Strong cipher suites: AEAD ciphers with forward secrecy only */
263 const char* cipher_list = "ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:!aNULL:!eNULL:!MD5:!DSS:!RC4:!3DES:!SEED:!IDEA:!CAMELLIA";
264 if (!SSL_CTX_set_cipher_list(ssl_netw->ctx, cipher_list)) {
265 n_log(LOG_ERR, "Failed to set cipher list");
266 return FALSE;
267 }
268
269 /* TLS 1.3 cipher suites (if supported) */
270#if OPENSSL_VERSION_NUMBER >= 0x10101000L
271 SSL_CTX_set_ciphersuites(ssl_netw->ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256");
272#endif
273
274 /* Configure ECDH: prefer X25519 and P-256 curves */
275#if OPENSSL_VERSION_NUMBER >= 0x10101000L
276 if (!SSL_CTX_set1_groups_list(ssl_netw->ctx, "X25519:P-256:P-384")) {
277 n_log(LOG_ERR, "Failed to set ECDH groups");
278 return FALSE;
279 }
280#elif OPENSSL_VERSION_NUMBER >= 0x10002000L
281 SSL_CTX_set_ecdh_auto(ssl_netw->ctx, 1);
282#endif
283
284 /* Session cache: use server-side cache with a short timeout */
285 SSL_CTX_set_session_cache_mode(ssl_netw->ctx, SSL_SESS_CACHE_SERVER);
286 SSL_CTX_set_timeout(ssl_netw->ctx, 300); /* 5 minutes */
287
288 n_log(LOG_INFO, "SSL context hardened: TLS 1.2+, strong ciphers, no compression");
289 return TRUE;
290} /* ssl_harden_context */
291
298int set_socket_timeout(SOCKET sock, int timeout_seconds) {
299#ifndef __windows__
300 struct timeval tv;
301 tv.tv_sec = timeout_seconds;
302 tv.tv_usec = 0;
303 if (setsockopt((int)sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
304 n_log(LOG_ERR, "Failed to set SO_RCVTIMEO: %s", strerror(errno));
305 return FALSE;
306 }
307 if (setsockopt((int)sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
308 n_log(LOG_ERR, "Failed to set SO_SNDTIMEO: %s", strerror(errno));
309 return FALSE;
310 }
311#else
312 DWORD tv = (DWORD)(timeout_seconds * 1000);
313 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) < 0) {
314 n_log(LOG_ERR, "Failed to set SO_RCVTIMEO");
315 return FALSE;
316 }
317 if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv)) < 0) {
318 n_log(LOG_ERR, "Failed to set SO_SNDTIMEO");
319 return FALSE;
320 }
321#endif
322 return TRUE;
323}
324
338int validate_path(const char* requested_path, const char* root, char* resolved, size_t resolved_size) {
339 char resolved_root[PATH_MAX] = "";
340 char resolved_request[PATH_MAX] = "";
341
342 /* Resolve the root directory */
343 if (!realpath(root, resolved_root)) {
344 n_log(LOG_ERR, "Cannot resolve root directory: %s (%s)", root, strerror(errno));
345 return FALSE;
346 }
347
348 /* Resolve the requested path */
349 if (!realpath(requested_path, resolved_request)) {
350 /* File doesn't exist - that's a 404, not a security issue,
351 * but we still need to check the parent for traversal attempts.
352 * If the file simply doesn't exist, return FALSE and let caller 404. */
353 return FALSE;
354 }
355
356 /* Ensure the resolved request starts with the resolved root */
357 size_t root_len = strlen(resolved_root);
358 if (strncmp(resolved_request, resolved_root, root_len) != 0) {
359 n_log(LOG_ERR, "Path traversal attempt blocked: %s (resolved: %s, root: %s)",
360 requested_path, resolved_request, resolved_root);
361 return FALSE;
362 }
363 /* After the root prefix, must be '/' or end of string (exact root match) */
364 if (resolved_request[root_len] != '/' && resolved_request[root_len] != '\0') {
365 n_log(LOG_ERR, "Path traversal attempt blocked (prefix match): %s", requested_path);
366 return FALSE;
367 }
368
369 strncpy(resolved, resolved_request, resolved_size - 1);
370 resolved[resolved_size - 1] = '\0';
371 return TRUE;
372}
373
374/* Exit handling */
375void action_on_sig(int recvd_signal) {
376 (void)recvd_signal;
377#ifndef __windows__
378 static int nb_sigterm = 0;
379 switch (recvd_signal) {
380 case (SIGABRT):
381 n_log(LOG_ERR, "Caught SIGABRT !");
382 break;
383 case (SIGINT):
384 n_log(LOG_ERR, "Caught SIGINT !");
385 break;
386 case (SIGBUS):
387 n_log(LOG_ERR, "Caught SIGBUS !");
388 break;
389 case (SIGFPE):
390 n_log(LOG_ERR, "Caught SIGFPE !");
391 break;
392 case (SIGSEGV):
393 n_log(LOG_ERR, "Caught SIGSEGV !");
394 break;
395 case (SIGSYS):
396 n_log(LOG_ERR, "Caught SIGSYS !");
397 break;
398 case (SIGTERM):
399 nb_sigterm++;
400 if (nb_sigterm >= 2) {
401 n_log(LOG_ERR, "Caught too much SIGTERM, trying _exit() !!");
402 _exit(-1);
403 }
404 n_log(LOG_ERR, "Caught %d SIGTERM, exiting now !!", nb_sigterm);
405 exit(-1);
406 case (SIGUSR1):
407 done = TRUE;
408 n_log(LOG_ERR, "Caught SIGUSR1 !");
409 break;
410 case (SIGUSR2):
411 done = TRUE;
412 n_log(LOG_ERR, "Caught SIGUSR2 !");
413 break;
414 case (SIGHUP):
415 n_log(LOG_NOTICE, "Caught SIGHUP !");
416 break;
417 default:
418 n_log(LOG_ERR, "Caught unknow signal %d", recvd_signal);
419 break;
420 }
421#endif
422} /* action_on_sig() */
423
424/* Build a response with security headers prepended to any additional headers */
425int build_secure_response(N_STR** response, int status_code, const char* content_type, char* extra_headers, N_STR* body) {
426 char headers[2048] = "";
427 snprintf(headers, sizeof(headers), "%s%s", SECURITY_HEADERS, extra_headers ? extra_headers : "");
428 return netw_build_http_response(response, status_code, "server", content_type, headers, body);
429}
430
431// Function to handle different URLs and return appropriate responses
432void handle_request(NETWORK* netw_ptr, LIST* routes_ptr) {
433 __n_assert(netw_ptr, return);
434 __n_assert(routes_ptr, return);
435
436 bool found = 0;
437 char** split_results = NULL;
438 char* http_url = NULL;
439 N_STR* dynamic_request_answer = NULL;
440
441 /* Heap allocation instead of stack alloca to prevent stack overflow */
442 char* http_buffer = NULL;
443 Malloc(http_buffer, char, (size_t)(max_http_request_size + 1));
444 __n_assert(http_buffer, netw_close(&netw_ptr); return);
445
446 /* Set socket timeout to prevent slow-loris attacks */
448
449 // SSL_read reads up to max_http_request_size bytes (variable-length HTTP request).
450 // recv_ssl_data is intentionally NOT used here: it loops until the entire buffer is
451 // full, which would block indefinitely since HTTP requests are smaller than the buffer.
452 int ssl_read_ret = SSL_read(netw_ptr->ssl, http_buffer, max_http_request_size);
453 if (ssl_read_ret <= 0) {
454 int ssl_error = SSL_get_error(netw_ptr->ssl, ssl_read_ret);
455 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
456 n_log(LOG_INFO, "SSL_read: peer closed the connection cleanly");
457 } else if (ssl_error == SSL_ERROR_SYSCALL && errno == EAGAIN) {
458 n_log(LOG_INFO, "SSL_read: connection timed out");
459 } else {
460 n_log(LOG_ERR, "SSL_read failed with SSL error %d", ssl_error);
461 }
462 Free(http_buffer);
463 netw_close(&netw_ptr);
464 return;
465 }
466 http_buffer[ssl_read_ret] = '\0';
467
468 // Extract URL from the request
469 char url[4096] = "";
470 netw_get_url_from_http_request(http_buffer, url, sizeof(url));
471 n_log(LOG_DEBUG, "url: %s", url);
472
473 // Handle the request based on the URL
474 N_STR* origin = new_nstr(32);
475 nstrprintf(origin, "%s:" SOCKET_SIZE_FORMAT, _str(netw_ptr->link.ip), netw_ptr->link.sock);
476
477 NETWORK_HTTP_INFO http_request = netw_extract_http_info(http_buffer);
478 N_STR* http_body = NULL;
479
480 split_results = split(url, "?", 0);
481 if (!split_results || !split_results[0]) {
482 http_body = char_to_nstr("<html><body><h1>Bad Request</h1></body></html>");
483 if (build_secure_response(&dynamic_request_answer, 400, netw_guess_http_content_type(url), "", http_body) == FALSE) {
484 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
485 }
486 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
487 } else {
488 http_url = split_results[0];
489 n_log(LOG_INFO, "%s: %s %s request...", _nstr(origin), http_request.type, url);
490 if (strcmp("OPTIONS", http_request.type) == 0) {
491 if (build_secure_response(&dynamic_request_answer, 200, netw_guess_http_content_type(url), "Allow: OPTIONS, GET, POST\r\n", NULL) == FALSE) {
492 n_log(LOG_ERR, "couldn't build an OPTION answer for %s", url);
493 }
494 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
495 } else if (strcmp("GET", http_request.type) == 0) {
496 char system_url[4096] = "";
497 const char* effective_root = root_dir ? root_dir : "./DATAS";
498 snprintf(system_url, sizeof(system_url), "%s%s", effective_root, http_url);
499
500 n_log(LOG_DEBUG, "%s: searching for file %s...", _nstr(origin), system_url);
501
502 /* Path traversal protection: validate resolved path is within root */
503 char validated_path[PATH_MAX] = "";
504 if (!validate_path(system_url, effective_root, validated_path, sizeof(validated_path))) {
505 /* Either file doesn't exist or path escapes root */
506 struct stat st;
507 if (stat(system_url, &st) == 0) {
508 /* File exists but outside root: forbidden */
509 n_log(LOG_ERR, "%s: path traversal blocked for %s", _nstr(origin), url);
510 http_body = char_to_nstr("<html><body><h1>403 Forbidden</h1></body></html>");
511 if (build_secure_response(&dynamic_request_answer, 403, "text/html", "", http_body) == FALSE) {
512 n_log(LOG_ERR, "couldn't build a Forbidden answer for %s", url);
513 }
514 } else {
515 /* File doesn't exist: 404 */
516 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
517 if (build_secure_response(&dynamic_request_answer, 404, netw_guess_http_content_type(url), "", http_body) == FALSE) {
518 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
519 }
520 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
521 }
522 } else {
523 struct stat st;
524 if (stat(validated_path, &st) == 0 && S_ISREG(st.st_mode)) {
525 n_log(LOG_DEBUG, "%s: file %s found !", _nstr(origin), validated_path);
526 http_body = file_to_nstr(validated_path);
527 if (!http_body) {
528 http_body = char_to_nstr("<html><body><h1>Internal Server Error</h1></body></html>");
529 if (build_secure_response(&dynamic_request_answer, 500, netw_guess_http_content_type(url), "", http_body) == FALSE) {
530 n_log(LOG_ERR, "couldn't build an Internal Server Error answer for %s", url);
531 }
532 n_log(LOG_ERR, "%s: %s %s 500", _nstr(origin), http_request.type, url);
533 } else {
534 if (build_secure_response(&dynamic_request_answer, 200, netw_guess_http_content_type(url), "", http_body) == FALSE) {
535 n_log(LOG_ERR, "couldn't build an http answer for %s", url);
536 }
537 n_log(LOG_INFO, "%s: %s %s 200", _nstr(origin), http_request.type, url);
538 }
539 } else if (stat(validated_path, &st) == 0 && S_ISDIR(st.st_mode)) {
540 /* Directory: check for index.html and redirect */
541 char index_path[PATH_MAX + 16] = "";
542 size_t url_len = strlen(http_url);
543 const char* url_slash = (url_len > 0 && http_url[url_len - 1] == '/') ? "" : "/";
544 /* realpath never has a trailing slash, always add '/' for filesystem path */
545 size_t vp_len = strlen(validated_path);
546 if (vp_len + sizeof("/index.html") > sizeof(index_path)) {
547 n_log(LOG_ERR, "path too long for index lookup: %s", validated_path);
548 http_body = char_to_nstr("<html><body><h1>414 URI Too Long</h1></body></html>");
549 if (build_secure_response(&dynamic_request_answer, 414, "text/html", "", http_body) == FALSE) {
550 n_log(LOG_ERR, "couldn't build a URI Too Long answer for %s", url);
551 }
552 } else {
553 snprintf(index_path, sizeof(index_path), "%s/index.html", validated_path);
554 char validated_index[PATH_MAX] = "";
555 struct stat idx_st;
556 if (validate_path(index_path, effective_root, validated_index, sizeof(validated_index)) &&
557 stat(validated_index, &idx_st) == 0 && S_ISREG(idx_st.st_mode)) {
558 char location_header[4096] = "";
559 snprintf(location_header, sizeof(location_header), "Location: %s%sindex.html\r\n", http_url, url_slash);
560 if (build_secure_response(&dynamic_request_answer, 301, "text/html", location_header, NULL) == FALSE) {
561 n_log(LOG_ERR, "couldn't build a redirect answer for %s", url);
562 }
563 n_log(LOG_INFO, "%s: %s %s 301 -> %s%sindex.html", _nstr(origin), http_request.type, url, http_url, url_slash);
564 } else {
565 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
566 if (build_secure_response(&dynamic_request_answer, 404, netw_guess_http_content_type(url), "", http_body) == FALSE) {
567 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
568 }
569 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
570 }
571 }
572 } else {
573 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
574 if (build_secure_response(&dynamic_request_answer, 404, netw_guess_http_content_type(url), "", http_body) == FALSE) {
575 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
576 }
577 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
578 }
579 }
580 } else if (strcmp("POST", http_request.type) == 0) {
581 // Parse virtual route
582 found = 0;
583 list_foreach(node, routes_ptr) {
584 if (strcmp(node->ptr, http_url) == 0) {
585 // Handle 200 OK from virtual route
586 HASH_TABLE* post_data = netw_parse_post_data(http_request.body);
587 if (post_data) {
588 HT_FOREACH(hnode, post_data,
589 {
590 n_log(LOG_DEBUG, "%s: POST DATA: %s=%s", _nstr(origin), hnode->key, (char*)hnode->data.ptr);
591 });
592 destroy_ht(&post_data);
593 }
594 http_body = char_to_nstr("{\"status\":\"ok\"}");
595 if (build_secure_response(&dynamic_request_answer, 200, "application/json", "", http_body) == FALSE) {
596 n_log(LOG_ERR, "couldn't build a route 200 answer for %s", url);
597 }
598 found = 1;
599 n_log(LOG_INFO, "%s: %s virtual:%s 200", _nstr(origin), http_request.type, url);
600 break;
601 }
602 }
603 if (!found) {
604 http_body = char_to_nstr("<html><body><h1>404 Not Found</h1></body></html>");
605 if (build_secure_response(&dynamic_request_answer, 404, netw_guess_http_content_type(url), "", http_body) == FALSE) {
606 n_log(LOG_ERR, "couldn't build a NOT FOUND answer for %s", url);
607 }
608 n_log(LOG_ERR, "%s: %s %s 404", _nstr(origin), http_request.type, url);
609 }
610 } else {
611 http_body = char_to_nstr("<html><body><h1>400 Bad Request</h1></body></html>");
612 if (build_secure_response(&dynamic_request_answer, 400, netw_guess_http_content_type(url), "", http_body) == FALSE) {
613 n_log(LOG_ERR, "couldn't build a Bad Request answer for %s", url);
614 }
615 n_log(LOG_ERR, "%s: %s %s 400", _nstr(origin), http_request.type, url);
616 }
617 free_split_result(&split_results);
618 }
619 if (dynamic_request_answer) {
620 if (dynamic_request_answer->written > UINT32_MAX) {
621 n_log(LOG_ERR, "response too large to send for %s: %s %s (size: %zu)", _nstr(origin), http_request.type, url, dynamic_request_answer->written);
622 } else if (send_ssl_data(netw_ptr, dynamic_request_answer->data, (uint32_t)dynamic_request_answer->written) < 0) {
623 n_log(LOG_ERR, "failed to send response for %s: %s %s", _nstr(origin), http_request.type, url);
624 }
625 free_nstr(&dynamic_request_answer);
626 } else {
627 n_log(LOG_ERR, "couldn't build an answer for %s: %s %s", _nstr(origin), http_request.type, url);
628 }
629 netw_info_destroy(http_request);
630 free_nstr(&origin);
631 free_nstr(&http_body);
632 Free(http_buffer);
633} /* handle_request */
634
636typedef struct NETWORK_SSL_THREAD_PARAMS {
638 NETWORK* netw;
640 LIST* routes;
642
643void* ssl_network_thread(void* params) {
644 __n_assert(params, return NULL);
646 handle_request(ssl_params->netw, ssl_params->routes);
647 netw_close(&ssl_params->netw);
648 Free(ssl_params);
649 return NULL;
650}
651
652int main(int argc, char* argv[]) {
653 int exit_code = 0;
654 THREAD_POOL* thread_pool = NULL;
656 __n_assert(routes, n_log(LOG_ERR, "could not allocate list !"); exit(1));
657
658 /* processing args and set log_level */
660
661 if (!port) {
662 n_log(LOG_ERR, "No port given. Exiting.");
663 exit_code = 1;
664 goto clean_and_exit;
665 }
666 if (!key) {
667 n_log(LOG_ERR, "No key given. Exiting.");
668 exit_code = 1;
669 goto clean_and_exit;
670 }
671 if (!cert) {
672 n_log(LOG_ERR, "No certificate given. Exiting.");
673 exit_code = 1;
674 goto clean_and_exit;
675 }
676
677#ifndef __windows__
678 errno = 0;
679 signal(SIGPIPE, SIG_IGN);
680 /* initializing signal catching */
681 struct sigaction signal_catcher;
682
683 /* quit on sig */
684 signal_catcher.sa_handler = action_on_sig;
685 sigemptyset(&signal_catcher.sa_mask);
686 signal_catcher.sa_flags = 0;
687
688 sigaction(SIGTERM, &signal_catcher, NULL);
689 sigaction(SIGUSR1, &signal_catcher, NULL);
690#endif
691
692 long int cores = get_nb_cpu_cores();
693 int nb_active_threads = (cores > 0) ? (int)cores : 1;
694 int nb_waiting_threads = 10 * nb_active_threads;
695 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
696 thread_pool = new_thread_pool((size_t)nb_active_threads, (size_t)nb_waiting_threads);
697
698 n_log(LOG_INFO, "Creating listening network for %s:%s %d", _str(addr), _str(port), ip_version);
699 /* create listening network */
700 if (netw_make_listening(&server, addr, port, SOMAXCONN, ip_version) == FALSE) {
701 n_log(LOG_ERR, "Fatal error with network initialization");
702 exit(-1);
703 }
704
705 if (ca_file) {
706 n_log(LOG_INFO, "Using SSL with certificate chain verification (CA: %s)", ca_file);
708 } else {
710 }
711
712 /* Harden the SSL context after crypto setup */
713 if (ssl_harden_context(server) == FALSE) {
714 n_log(LOG_ERR, "Failed to harden SSL context. Exiting.");
715 exit_code = 1;
716 goto clean_and_exit;
717 }
718
719 if (ssl_verify) {
720 n_log(LOG_INFO, "SSL peer certificate verification enabled");
722 }
723 int accepted_count = 0;
724 while (!done) {
725 n_log(LOG_DEBUG, "Blocking on accept...");
726 /* get any accepted client on a network */
727 int return_code = 0;
728 netw = netw_accept_from_ex(server, 0, 0, 0, &return_code);
729 if (!netw) {
730 if (return_code == EINTR) {
731 n_log(LOG_INFO, "accept exited after catching a signal");
732 goto clean_and_exit;
733 } else {
734 n_log(LOG_ERR, "error on accept, NULL netw returned !");
735 }
736 } else {
737 n_log(LOG_INFO, "accepted SSL connection on socket %d", netw->link.sock);
738 NETWORK_SSL_THREAD_PARAMS* netw_ssl_params = NULL;
739 Malloc(netw_ssl_params, NETWORK_SSL_THREAD_PARAMS, 1);
740 netw_ssl_params->netw = netw;
741 netw_ssl_params->routes = routes;
742 if (add_threaded_process(thread_pool, &ssl_network_thread, (void*)netw_ssl_params, DIRECT_PROC) == FALSE) {
743 n_log(LOG_ERR, "Error adding client management to thread pool");
744 }
745 accepted_count++;
746 if (max_connections > 0 && accepted_count >= max_connections) {
747 n_log(LOG_NOTICE, "reached %d connections, exiting", max_connections);
748 break;
749 }
750 }
751 }
752clean_and_exit:
753 if (thread_pool) {
756 }
758 netw_unload();
762 FreeNoLog(key);
766 exit(exit_code);
767}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:38
int max_http_request_size
int max_connections
bool done
char * root_dir
char * ca_file
NETWORK * server
char * key
int ssl_verify
int ip_version
LIST * routes
void * ssl_network_thread(void *params)
char * addr
char * cert
void action_on_sig(int recvd_signal)
void handle_request(NETWORK *netw_ptr, LIST *routes_ptr)
char * port
int ssl_harden_context(NETWORK *ssl_netw)
Harden the SSL context with modern security settings.
#define CONNECTION_TIMEOUT_SECONDS
#define SECURITY_HEADERS
Network for managing connections.
LIST * routes
virtual routes for the server
#define MAX_HTTP_REQUEST_SIZE_LIMIT
int set_socket_timeout(SOCKET sock, int timeout_seconds)
Set socket read/write timeout to prevent slow-loris attacks.
NETWORK * netw
network to use for the receiving thread
int validate_path(const char *requested_path, const char *root, char *resolved, size_t resolved_size)
Validate that a resolved path stays within the document root.
int build_secure_response(N_STR **response, int status_code, const char *content_type, char *extra_headers, N_STR *body)
structure of a NETWORK_SSL_THREAD_PARAMS
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:271
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define _str(__PTR)
define true
Definition n_common.h:192
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2234
#define HT_FOREACH(__ITEM_, __HASH_,...)
ForEach macro helper.
Definition n_hash.h:215
structure of a hash table
Definition n_hash.h:137
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:227
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:88
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:547
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:36
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:74
Structure of a generic LIST container.
Definition n_list.h:58
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:206
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
char ** split(const char *str, const char *delim, int empty)
split the strings into a an array of char *pointer , ended by a NULL one.
Definition n_str.c:912
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:286
int free_split_result(char ***tab)
Free a split result allocated array.
Definition n_str.c:1008
A box including a string and his lenght.
Definition n_str.h:60
char * ip
ip of the connected socket
Definition n_network.h:244
N_SOCKET link
networking socket
Definition n_network.h:326
char * type
Type of request.
Definition n_network.h:389
char * body
Pointer to the body data.
Definition n_network.h:387
SOCKET sock
a normal socket
Definition n_network.h:242
SSL_CTX * ctx
SSL context holder.
Definition n_network.h:316
SSL * ssl
SSL handle.
Definition n_network.h:318
ssize_t send_ssl_data(void *netw, char *buf, uint32_t n)
send data onto the socket
Definition n_network.c:3512
int netw_ssl_set_verify(NETWORK *netw, int enable)
enable or disable SSL peer certificate verification
Definition n_network.c:1606
int netw_get_url_from_http_request(const char *request, char *url, size_t size)
Helper function to extract the URL from the HTTP request line.
Definition n_network.c:4338
int netw_set_crypto(NETWORK *netw, char *key, char *certificate)
activate SSL encryption on selected network, using key and certificate
Definition n_network.c:1321
#define NETWORK_IPV6
Flag to force IPV6
Definition n_network.h:51
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
Definition n_network.c:2240
#define NETWORK_IPV4
Flag to force IPV4
Definition n_network.h:49
int netw_build_http_response(N_STR **http_response, int status_code, const char *server_name, const char *content_type, char *additional_headers, N_STR *body)
function to dynamically generate an HTTP response
Definition n_network.c:4578
#define SOCKET_SIZE_FORMAT
socket associated printf style
Definition n_network.h:90
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
Definition n_network.c:2713
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
Definition n_network.h:47
int SOCKET
default socket declaration
Definition n_network.h:88
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2041
int netw_set_crypto_chain(NETWORK *netw, char *key, char *certificate, char *ca_file)
activate SSL encryption using key/certificate files and a CA file for chain verification
Definition n_network.c:1473
NETWORK_HTTP_INFO netw_extract_http_info(char *request)
extract a lot of informations, mostly as pointers, and populate a NETWORK_HTTP_INFO structure
Definition n_network.c:4258
int netw_info_destroy(NETWORK_HTTP_INFO http_request)
destroy a NETWORK_HTTP_INFO loaded informations
Definition n_network.c:4325
HASH_TABLE * netw_parse_post_data(const char *post_data)
Function to parse POST data.
Definition n_network.c:4412
const char * netw_guess_http_content_type(const char *url)
function to guess the content type based on URL extension
Definition n_network.c:4458
Structure of a NETWORK.
Definition n_network.h:258
structure for splitting HTTP requests
Definition n_network.h:381
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
long int get_nb_cpu_cores()
get number of core of current system
#define DIRECT_PROC
processing mode for added func, direct start, not queued
Structure of a thread pool.
List structures and definitions.
Generic log system.
Network Engine.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.
N_STR and string function declaration.
Thread pool declaration.