From 88d4cb81b5340028fb3c60de362f8ffa1e6508ae Mon Sep 17 00:00:00 2001 From: Mikhail Novosyolov Date: Mon, 23 May 2022 20:58:36 +0300 Subject: [PATCH] Implement reading params in FCGI in C + fix verifying IP address if the first character is a bad one. --- webserver/doskast-trigger-connect.c | 41 +++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/webserver/doskast-trigger-connect.c b/webserver/doskast-trigger-connect.c index e52c1ce..69aba21 100644 --- a/webserver/doskast-trigger-connect.c +++ b/webserver/doskast-trigger-connect.c @@ -2,12 +2,16 @@ #include #include #include +#include #include #include -#include -#include #include #include +// https://abf.io/import/cgilib +#include +// https://abf.io/import/fcgi +#include +#include // allow to redefine as gcc -D... #ifndef DIR @@ -27,9 +31,21 @@ _verify_ip(char *ip){ if (!( (ip[i] == '.') || /* IPv4 dots */ (ip[i] == ':') || /* IPv6 */ (ip[i] == '/') || /* XXX Is it needed for IPv6? */ - (isalnum(ip[i]) > 0) /* Numbers (v4 and v6) and letters (v6) */ + (isalnum(ip[i]) != 0) /* Numbers (v4 and v6) and letters (v6) */ )) { - rc = i; + rc = 1; + break; + } + } + return rc; +} + +int +_verify_geometry(char *value){ + int rc = 0; + for (int i = 0; i < strlen(value); i++){ + if (isdigit(value[i]) == 0) { + rc = 1; break; } } @@ -79,7 +95,14 @@ main(){ fprintf(stderr, "Cannot chdir and chroot into %s\n", DIR); goto out; } + s_cgi *cgi; while(FCGI_Accept() >= 0) { + cgi = cgiInit(); + if (cgi == NULL) { + fprintf(stderr, "%s\n", "Error initializaing CGI"); + http_code = HTTP_ERROR; + goto fcgi_out; + } char *ip = getenv("REMOTE_ADDR"); if (ip == NULL) { fprintf(stderr, "%s\n", "Env REMOTE_ADDR is not set"); @@ -92,6 +115,8 @@ main(){ http_code = HTTP_BAD_REQUEST; goto fcgi_out; } + char *cgi_width = cgiGetValue(cgi, "width"); + char *cgi_height = cgiGetValue(cgi, "height"); char *hex; int max_try = 10; for (int i = 1; i <= max_try; i++) { @@ -115,15 +140,21 @@ main(){ http_code = HTTP_ERROR; goto fcgi_out; } - fprintf(d, "%s", ip); + fprintf(d, "ip=%s\n", ip); + if ( (cgi_width != NULL) && (_verify_geometry(cgi_width) == 0) ) + fprintf(d, "width=%s\n", cgi_width); + if ( (cgi_height != NULL) && (_verify_geometry(cgi_height) == 0) ) + fprintf(d, "height=%s\n", cgi_height); rc = fclose(d); if (rc != 0) { fprintf(stderr, "Error closing file %s/%s\n", DIR, hex); http_code = HTTP_ERROR; goto fcgi_out; } + fprintf(stderr, "Closed file %s/%s\n", DIR, hex); http_code = HTTP_OK; fcgi_out: + cgiFree(cgi); printf("Status: %d\n", http_code); printf("Content-Type: text/plain; charset=utf-8\n"); if (http_code == HTTP_OK) {