From 9adb4791df81549c73d7ec88fd1f9746d6b67898 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 17 Sep 2021 18:53:08 +0200 Subject: [PATCH] Added basic support for Necronda backend --- src/client.c | 35 ++++++++++++++++--------- src/lib/fastcgi.c | 65 ++++++++++++++++++++++++++++------------------- src/lib/fastcgi.h | 8 +++++- src/lib/uri.c | 44 ++++++++++++++++++++++---------- 4 files changed, 100 insertions(+), 52 deletions(-) diff --git a/src/client.c b/src/client.c index b588f4a..5d4c667 100644 --- a/src/client.c +++ b/src/client.c @@ -67,7 +67,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int int use_fastcgi = 0; int use_rev_proxy = 0; int p_len; - fastcgi_conn php_fpm = {.socket = 0, .req_id = 0}; + fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0}; http_status custom_status; http_res res; @@ -372,35 +372,46 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int content_length = ftell(file); fseek(file, 0, SEEK_SET); } else { + int mode; + if (strcmp(uri.filename + strlen(uri.filename) - 4, ".ncr") == 0) { + mode = FASTCGI_NECRONDA; + } else if (strcmp(uri.filename + strlen(uri.filename) - 4, ".php") == 0) { + mode = FASTCGI_PHP; + } else { + res.status = http_get_status(500); + print(ERR_STR "Invalid FastCGI extension: %s" CLR_STR, uri.filename); + goto respond; + } + struct stat statbuf; stat(uri.filename, &statbuf); char *last_modified = http_format_date(statbuf.st_mtime, buf0, sizeof(buf0)); http_add_header_field(&res.hdr, "Last-Modified", last_modified); res.status = http_get_status(200); - if (fastcgi_init(&php_fpm, client_num, req_num, client, &req, &uri) != 0) { + if (fastcgi_init(&fcgi_conn, mode, client_num, req_num, client, &req, &uri) != 0) { res.status = http_get_status(503); - sprintf(err_msg, "Unable to communicate with PHP-FPM."); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); goto respond; } char *client_content_length = http_get_header_field(&req.hdr, "Content-Length"); if (client_content_length != NULL) { unsigned long client_content_len = strtoul(client_content_length, NULL, 10); - ret = fastcgi_receive(&php_fpm, client, client_content_len); + ret = fastcgi_receive(&fcgi_conn, client, client_content_len); if (ret != 0) { if (ret < 0) { goto abort; } else { - sprintf(err_msg, "Unable to communicate with PHP-FPM."); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); } res.status = http_get_status(502); goto respond; } } - fastcgi_close_stdin(&php_fpm); + fastcgi_close_stdin(&fcgi_conn); - ret = fastcgi_header(&php_fpm, &res, err_msg); + ret = fastcgi_header(&fcgi_conn, &res, err_msg); if (ret != 0) { if (ret < 0) { goto abort; @@ -562,7 +573,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int int chunked = transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0; int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & FASTCGI_COMPRESS); - fastcgi_send(&php_fpm, client, flags); + fastcgi_send(&fcgi_conn, client, flags); } else if (use_rev_proxy) { char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding"); int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL; @@ -589,10 +600,10 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int uri_free(&uri); abort: - if (php_fpm.socket != 0) { - shutdown(php_fpm.socket, SHUT_RDWR); - close(php_fpm.socket); - php_fpm.socket = 0; + if (fcgi_conn.socket != 0) { + shutdown(fcgi_conn.socket, SHUT_RDWR); + close(fcgi_conn.socket); + fcgi_conn.socket = 0; } http_free_req(&req); http_free_res(&res); diff --git a/src/lib/fastcgi.c b/src/lib/fastcgi.c index 86cc8ee..04f8c50 100644 --- a/src/lib/fastcgi.c +++ b/src/lib/fastcgi.c @@ -49,7 +49,7 @@ char *fastcgi_add_param(char *buf, const char *key, const char *value) { return ptr; } -int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_num, const sock *client, +int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client, const http_req *req, const http_uri *uri) { unsigned short req_id = (client_num & 0xFFF) << 4; if (client_num == 0) { @@ -57,20 +57,27 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n } else { req_id |= req_num & 0xF; } + conn->mode = mode; conn->req_id = req_id; conn->out_buf = NULL; conn->out_off = 0; - int php_fpm = socket(AF_UNIX, SOCK_STREAM, 0); - if (php_fpm < 0) { + int fcgi_sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (fcgi_sock < 0) { print(ERR_STR "Unable to create unix socket: %s" CLR_STR, strerror(errno)); return -1; } - conn->socket = php_fpm; + conn->socket = fcgi_sock; - struct sockaddr_un php_fpm_addr = {AF_UNIX, PHP_FPM_SOCKET}; - if (connect(conn->socket, (struct sockaddr *) &php_fpm_addr, sizeof(php_fpm_addr)) < 0) { - print(ERR_STR "Unable to connect to unix socket of PHP-FPM: %s" CLR_STR, strerror(errno)); + struct sockaddr_un sock_addr = {AF_UNIX}; + if (conn->mode == FASTCGI_NECRONDA) { + snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path) - 1, "%s", NECRONDA_BACKEND_SOCKET); + } else if (conn->mode == FASTCGI_PHP) { + snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path) - 1, "%s", PHP_FPM_SOCKET); + } + + if (connect(conn->socket, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) { + print(ERR_STR "Unable to connect to unix socket of FastCGI socket: %s" CLR_STR, strerror(errno)); return -1; } @@ -90,7 +97,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n {.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0} }; if (send(conn->socket, &begin, sizeof(begin), 0) != sizeof(begin)) { - print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno)); return -2; } @@ -173,7 +180,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n header.contentLengthB0 = param_len & 0xFF; memcpy(param_buf, &header, sizeof(header)); if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) { - print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno)); return -2; } @@ -181,7 +188,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n header.contentLengthB1 = 0; header.contentLengthB0 = 0; if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) { - print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno)); return -2; } @@ -201,7 +208,7 @@ int fastcgi_close_stdin(fastcgi_conn *conn) { }; if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) { - print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno)); return -2; } @@ -285,13 +292,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) { ret = recv(conn->socket, &header, sizeof(header), 0); if (ret < 0) { res->status = http_get_status(502); - sprintf(err_msg, "Unable to communicate with PHP-FPM."); - print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno)); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); + print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno)); return 1; } else if (ret != sizeof(header)) { res->status = http_get_status(502); - sprintf(err_msg, "Unable to communicate with PHP-FPM."); - print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); + print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR); return 1; } req_id = (header.requestIdB1 << 8) | header.requestIdB0; @@ -300,14 +307,14 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) { ret = recv(conn->socket, content, content_len + header.paddingLength, 0); if (ret < 0) { res->status = http_get_status(502); - sprintf(err_msg, "Unable to communicate with PHP-FPM."); - print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno)); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); + print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno)); free(content); return 1; } else if (ret != (content_len + header.paddingLength)) { res->status = http_get_status(502); - sprintf(err_msg, "Unable to communicate with PHP-FPM."); - print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR); + sprintf(err_msg, "Unable to communicate with FastCGI socket."); + print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR); free(content); return 1; } @@ -331,7 +338,10 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) { free(content); return 1; } else if (header.type == FCGI_STDERR) { - err = err || fastcgi_php_error(content, content_len, err_msg); + // TODO implement Necronda backend error handling + if (conn->mode == FASTCGI_PHP) { + err = err || fastcgi_php_error(content, content_len, err_msg); + } } else if (header.type == FCGI_STDOUT) { break; } else { @@ -417,10 +427,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) { while (1) { ret = recv(conn->socket, &header, sizeof(header), 0); if (ret < 0) { - print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno)); return -1; } else if (ret != sizeof(header)) { - print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR); + print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR); return -1; } @@ -430,11 +440,11 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) { ptr = content; ret = recv(conn->socket, content, content_len + header.paddingLength, 0); if (ret < 0) { - print(ERR_STR "Unable to receive from PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno)); free(content); return -1; } else if (ret != (content_len + header.paddingLength)) { - print(ERR_STR "Unable to receive from PHP-FPM" CLR_STR); + print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR); free(content); return -1; } @@ -467,7 +477,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) { return 0; } else if (header.type == FCGI_STDERR) { - fastcgi_php_error(content, content_len, buf0); + // TODO implement Necronda backend error handling + if (conn->mode == FASTCGI_PHP) { + fastcgi_php_error(content, content_len, buf0); + } } else if (header.type == FCGI_STDOUT) { unsigned long avail_in, avail_out; out: @@ -532,7 +545,7 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) { if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err; if (send(conn->socket, buf, ret, 0) != ret) { err: - print(ERR_STR "Unable to send to PHP-FPM: %s" CLR_STR, strerror(errno)); + print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno)); return -2; } } diff --git a/src/lib/fastcgi.h b/src/lib/fastcgi.h index 4807b34..e8c3445 100644 --- a/src/lib/fastcgi.h +++ b/src/lib/fastcgi.h @@ -17,11 +17,17 @@ #define FASTCGI_COMPRESS_BR 4 #define FASTCGI_COMPRESS 6 +#define FASTCGI_PHP 1 +#define FASTCGI_NECRONDA 2 + #ifndef PHP_FPM_SOCKET # define PHP_FPM_SOCKET "/var/run/php-fpm/php-fpm.sock" #endif +#define NECRONDA_BACKEND_SOCKET "/var/run/necronda/necronda-backend.sock" + typedef struct { + int mode; int socket; unsigned short req_id; char *out_buf; @@ -31,7 +37,7 @@ typedef struct { char *fastcgi_add_param(char *buf, const char *key, const char *value); -int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_num, const sock *client, +int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client, const http_req *req, const http_uri *uri); int fastcgi_close_stdin(fastcgi_conn *conn); diff --git a/src/lib/uri.c b/src/lib/uri.c index 82c38fa..bf879a6 100644 --- a/src/lib/uri.c +++ b/src/lib/uri.c @@ -30,6 +30,7 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo char buf1[1024]; char buf2[1024]; char buf3[1024]; + char buf4[1024]; int p_len; uri->webroot = NULL; uri->req_path = NULL; @@ -99,12 +100,18 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo while (1) { sprintf(buf0, "%s%s", uri->webroot, uri->path); - p_len = snprintf(buf1, sizeof(buf1), "%s.php", buf0); + p_len = snprintf(buf1, sizeof(buf1), "%s.ncr", buf0); if (p_len < 0 || p_len >= sizeof(buf1)) return -1; - p_len = snprintf(buf2, sizeof(buf2), "%s.html", buf0); + p_len = snprintf(buf2, sizeof(buf2), "%s.php", buf0); if (p_len < 0 || p_len >= sizeof(buf2)) return -1; + p_len = snprintf(buf3, sizeof(buf3), "%s.html", buf0); + if (p_len < 0 || p_len >= sizeof(buf3)) return -1; - if (strlen(uri->path) <= 1 || path_exists(buf0) || path_is_file(buf1) || path_is_file(buf2)) { + if (strlen(uri->path) <= 1 || + path_exists(buf0) || + path_is_file(buf1) || + path_is_file(buf2) || + path_is_file(buf3)) { break; } @@ -112,37 +119,42 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo parent_dir: ptr = strrchr(uri->path, '/'); size = (long) strlen(ptr); - sprintf(buf3, "%.*s%s", (int) size, ptr, uri->pathinfo); - strcpy(uri->pathinfo, buf3); + sprintf(buf4, "%.*s%s", (int) size, ptr, uri->pathinfo); + strcpy(uri->pathinfo, buf4); ptr[0] = 0; } if (uri->pathinfo[0] != 0) { - sprintf(buf3, "%s", uri->pathinfo + 1); - strcpy(uri->pathinfo, buf3); + sprintf(buf4, "%s", uri->pathinfo + 1); + strcpy(uri->pathinfo, buf4); } if (path_is_file(buf0)) { uri->filename = malloc(strlen(buf0) + 1); strcpy(uri->filename, buf0); long len = (long) strlen(uri->path); - if (strcmp(uri->path + len - 5, ".html") == 0) { - uri->path[len - 5] = 0; - } else if (strcmp(uri->path + len - 4, ".php") == 0) { + if (strcmp(uri->path + len - 4, ".ncr") == 0 || strcmp(uri->path + len - 4, ".php") == 0) { uri->path[len - 4] = 0; uri->is_static = 0; + } else if (strcmp(uri->path + len - 5, ".html") == 0) { + uri->path[len - 5] = 0; } } else if (path_is_file(buf1)) { uri->is_static = 0; uri->filename = malloc(strlen(buf1) + 1); strcpy(uri->filename, buf1); } else if (path_is_file(buf2)) { + uri->is_static = 0; uri->filename = malloc(strlen(buf2) + 1); strcpy(uri->filename, buf2); + } else if (path_is_file(buf3)) { + uri->filename = malloc(strlen(buf3) + 1); + strcpy(uri->filename, buf3); } else { uri->is_dir = 1; strcpy(uri->path + strlen(uri->path), "/"); - sprintf(buf1, "%s%sindex.php", uri->webroot, uri->path); - sprintf(buf2, "%s%sindex.html", uri->webroot, uri->path); + sprintf(buf1, "%s%sindex.ncr", uri->webroot, uri->path); + sprintf(buf2, "%s%sindex.php", uri->webroot, uri->path); + sprintf(buf3, "%s%sindex.html", uri->webroot, uri->path); if (path_is_file(buf1)) { uri->filename = malloc(strlen(buf1) + 1); strcpy(uri->filename, buf1); @@ -150,6 +162,10 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo } else if (path_is_file(buf2)) { uri->filename = malloc(strlen(buf2) + 1); strcpy(uri->filename, buf2); + uri->is_static = 0; + } else if (path_is_file(buf3)) { + uri->filename = malloc(strlen(buf3) + 1); + strcpy(uri->filename, buf3); } else { if (dir_mode == URI_DIR_MODE_FORBIDDEN) { uri->is_static = 1; @@ -169,7 +185,9 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo if (strcmp(uri->path + strlen(uri->path) - 5, "index") == 0) { uri->path[strlen(uri->path) - 5] = 0; } - if (strcmp(uri->pathinfo, "index.php") == 0 || strcmp(uri->pathinfo, "index.html") == 0) { + if (strcmp(uri->pathinfo, "index.ncr") == 0 || + strcmp(uri->pathinfo, "index.php") == 0 || + strcmp(uri->pathinfo, "index.html") == 0) { uri->pathinfo[0] = 0; }