Implement reading params in FCGI in C

+ fix verifying IP address if the first character is a bad one.
master
Mikhail Novosyolov 2 years ago
parent db1677df78
commit 88d4cb81b5
  1. 41
      webserver/doskast-trigger-connect.c

@ -2,12 +2,16 @@
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcgios.h>
#include <fcgi_stdio.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
// https://abf.io/import/cgilib
#include <cgi.h>
// https://abf.io/import/fcgi
#include <fcgios.h>
#include <fcgi_stdio.h>
// 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) {

Loading…
Cancel
Save